Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • 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

Learn everything about programming

HackerEarth Two Arrays problem solution

YASH PAL, 31 July 2024
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

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