Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

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 solutions

Post navigation

Previous post
Next post

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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