Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth In an array problem solution

YASH PAL, 31 July 2024
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());
}
}
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes