Header Ad

HackerEarth In an array problem solution

In this HackerEarth In an array problem solution, You are given an array A of size N, where the ith integer of the array is A[i] and its value ranges between 1 and 1000 inclusive. You are required to complete the following task:

Assume that you are provided with 3 additional numbers K, X, and Y. Your task is to report the number of unordered pairs of elements (i,j) from this array, such that (1 <= i < j <= N), (A[i] + A[j])%K = X, and (A[i] x A[j])%K = Y.



HackerEarth In an array problem solution


HackerEarth In an array problem solution.

import java.io.*;
import java.util.*;
public final class checkpoint_b
{
static BufferedReader br;
static FastScanner sc;
static PrintWriter out;
static Random rnd=new Random();
static long[] cnt;
static int maxn=(int)(1005);

static void init(String curr) throws Exception
{
br=new BufferedReader(new FileReader(new File("in"+curr+".txt")));
sc=new FastScanner(br);
out=new PrintWriter(new FileWriter("out"+curr+".txt"));
}

public static void main(String args[]) throws Exception
{
init(args[0]);int n=sc.nextInt(),k=sc.nextInt(),x=sc.nextInt(),y=sc.nextInt();int[] a=new int[n];cnt=new long[maxn];
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();cnt[a[i]]++;
}
long res=0;
for(int i=1;i<maxn;i++)
{
for(int j=i;j<maxn;j++)
{
int val1=(i+j)%k,val2=(i*j)%k;
if(val1==x && val2==y)
{
if(i==j)
{
long curr=(cnt[i]*(cnt[i]-1L))/2L;res=res+curr;
}
else
{
long curr=(cnt[i]*cnt[j]);res=res+curr;
}
}
}
}
out.println(res);out.close();
}
}
class FastScanner
{
BufferedReader in;
StringTokenizer st;

public FastScanner(BufferedReader in) {
this.in = in;
}

public String nextToken() throws Exception {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}

public String next() throws Exception {
return nextToken().toString();
}

public int nextInt() throws Exception {
return Integer.parseInt(nextToken());
}

public long nextLong() throws Exception {
return Long.parseLong(nextToken());
}

public double nextDouble() throws Exception {
return Double.parseDouble(nextToken());
}
}


Post a Comment

0 Comments