βœ…Duplicates at a distance k (Contest)

Duplicates at a distance k easy Time Limit: 2 sec Memory Limit: 128000 kB

Problem Statement :

Given an array of N integers that may contain duplicate elements and an integer K, find if there exists a pair of integers (i, j) such that i < j and arr[i]=arr[j] and i and j are exactly k distance apart i.e ( j - i ) = k. Input The first line of input contains two integers N and K, next line contains N space-separated integers.

Constraints:- 1 < = N < = 105 1 < = K < = N 1 < = Arr[i] < = 109 Output Print 1 if there exist elements else print 0. Example Sample Input: 4 3 1 2 2 1

Sample Output: 1

Explanation:- Pair at index 1, 4 is the required answer so output=1

link:https://my.newtonschool.co/playground/code/uy3q6zsybdn4

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc= new Scanner(System.in);
        int N=sc.nextInt();
        int K=sc.nextInt();
        if(N!=1){
        int arr[]=new int[N];
        for(int i=0;i<N;i++){
            arr[i]=sc.nextInt();

        }
        int found=0;
        for(int i=0;i<arr.length-K;i++){
            if(arr[i]==arr[i+K]){
                found=1;
                break;
            }
        }

        if(found==1){
            System.out.print(1);
        }else{
            System.out.print(0);
        }
        }
    }
}

//method 02

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
    public static void main (String[] args) {
        // Your code here
        Scanner sc = new Scanner (System.in);
        int n = sc.nextInt();
        int k = sc.nextInt();
        int[]arr = new int[n];
        for(int i = 0 ;i<n;i++){
            arr[i] = sc.nextInt();
        }
        boolean f = false;
        for(int j  = 0;j<n;j++){
            if(j+k < n){
                if(arr[j] == arr[j+k]){
                    f = true;
                }
            }
        }
        if(f == true){
            System.out.println("1");
        }else{
            System.out.println("0");
        }
    }
}
//method 03
import java.io.*;
import java.util.*;

class Main {
public static void main (String[] args) throws Exception{
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
String s1[]=b.readLine().split(" ");
int n=Integer.parseInt(s1[0]);
int k=Integer.parseInt(s1[1]);
int a[]=new int[n];
HashMap<Integer,Integer> map=new HashMap<>();
String s2[]=b.readLine().split(" ");
int ans=0;
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(s2[i]);
if(map.containsKey(a[i])){
if(i-map.get(a[i])==k){
ans=1;
}
else if(i-map.get(a[i])>k){
map.put(a[i],i);
}
}
else{
map.put(a[i],i);
}
}
for(int i=0;i<n;i++){
}
System.out.println(ans);
}
}

Last updated