Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth In an array problem solution

YASH PAL, 31 July 202413 February 2026
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 solutions HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes