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
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Two Arrays problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Two Arrays problem solution Let’s define the matching number for two arrays X and Y, each with size n, as the number of pairs of indices (i,j) such that Xi = Yj (1 <= i, j <= n).
You are given an integer K and two arrays A and B with sizes N and M respectively. Find the number of ways to pick two subarrays with equal size, one from array A and the other from array B, such that the matching number for these two subarrays is >= K.
Note: a subarray consists of one or more consecutive elements of an array.
HackerEarth Two Arrays problem solution

HackerEarth Two Arrays problem solution.

import java.io.*;
import java.util.*;
import java.math.*;
import java.util.concurrent.*;

public final class sol
{
static BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
static FastScanner sc=new FastScanner(br);
static PrintWriter out=new PrintWriter(System.out);
static Random rnd=new Random();
static int[][] arr;
static int n,m,k;

static int get_sum(int i,int j,int len)
{
return arr[i][j]-arr[i][j-len]-arr[i-len][j]+arr[i-len][j-len];
}

public static void main(String args[]) throws Exception
{
n=sc.nextInt();m=sc.nextInt();k=sc.nextInt();int[] a=new int[n+1],b=new int[m+1];

for(int i=1;i<=n;i++)
{
a[i]=sc.nextInt();
}

for(int i=1;i<=m;i++)
{
b[i]=sc.nextInt();
}

arr=new int[n+1][m+1];

for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
arr[i][j]=(a[i]==b[j]?1:0);
}
}

long res=0;

for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
arr[i][j]+=arr[i][j-1];
}
}

for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
arr[i][j]+=arr[i-1][j];
}
}

for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
int low=1,high=Math.min(i,j);

while(low<high)
{
int mid=(low+high)>>1;

if(get_sum(i,j,mid)>=k)
{
high=mid;
}

else
{
low=mid+1;
}
}

if(get_sum(i,j,low)>=k)
{
res+=(Math.min(i,j)-low+1);
}
}
}

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 *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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