codeparrot/xlcost-text-to-code huggingface dataset:
Find three element from different three arrays such that a + b + c = sum | Java program to find three element from different three arrays such that a + b + c is equal to given sum ; Function to check if there is an element from each array such that sum of the three elements is equal to given sum . ; Store elements of first array in hash ; sum last two arrays element one by one ; Consider current pair and find if there is an element in a1 [ ] such that these three form a required triplet ; Driver Code
import java . util . * ; class GFG { static boolean findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . add ( a1 [ i ] ) ; } ArrayList < Integer > al = new ArrayList < > ( s ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( al . contains ( sum - a2 [ i ] - a3 [ j ] ) & al . indexOf ( sum - a2 [ i ] - a3 [ j ] ) != al . get ( al . size ( ) - 1 ) ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { int a1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int a2 [ ] = { 2 , 3 , 6 , 1 , 2 } ; int a3 [ ] = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = a1 . length ; int n2 = a2 . length ; int n3 = a3 . length ; if ( findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Count quadruplets with sum K from given array | Java program for the above approach ; Function to return the number of quadruplets having given sum ; Initialize variables ; Initialize answer ; All possible first elements ; All possible second element ; Use map to find the fourth element ; All possible third elements ; Calculate number of valid 4 th elements ; Update the twice_count ; Unordered pairs ; Return answer ; Driver Code ; Given array arr [ ] ; Given sum S ; Function Call
import java . util . * ; class GFG { static int countSum ( int a [ ] , int n , int sum ) { int i , j , k , l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { int req = sum - a [ i ] - a [ j ] ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( k = j + 1 ; k < n ; k ++ ) if ( m . containsKey ( a [ k ] ) ) { m . put ( a [ k ] , m . get ( a [ k ] ) + 1 ) ; } else { m . put ( a [ k ] , 1 ) ; } int twice_count = 0 ; for ( k = j + 1 ; k < n ; k ++ ) { if ( m . containsKey ( req - a [ k ] ) ) twice_count += m . get ( req - a [ k ] ) ; if ( req - a [ k ] == a [ k ] ) twice_count -- ; } count += twice_count / 2 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . length ; System . out . print ( countSum ( arr , N , S ) ) ; } }
Find four elements a , b , c and d in an array such that a + b = c + d | Java Program to find four different elements a , b , c and d of array such that a + b = c + d ; Class to represent a pair ; function to find a , b , c , d such that ( a + b ) = ( c + d ) ; Create an empty Hash to store mapping from sum to pair indexes ; Traverse through all possible pairs of arr [ ] ; If sum of current pair is not in hash , then store it and continue to next pair ; Testing program
import java . io . * ; import java . util . * ; class ArrayElements { class pair { int first , second ; pair ( int f , int s ) { first = f ; second = s ; } } ; boolean findPairs ( int arr [ ] ) { HashMap < Integer , pair > map = new HashMap < Integer , pair > ( ) ; int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . containsKey ( sum ) ) map . put ( sum , new pair ( i , j ) ) ; else { pair p = map . get ( sum ) ; System . out . println ( " ( " + arr [ p . first ] + " , ▁ " + arr [ p . second ] + " ) ▁ and ▁ ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; return true ; } } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 4 , 7 , 1 , 2 , 9 , 8 } ; ArrayElements a = new ArrayElements ( ) ; a . findPairs ( arr ) ; } }
Check if all subarrays contains at least one unique element | Java program for the above approach ; Function to check if all subarrays have at least one unique element ; Generate all subarray ; Store frequency of subarray 's elements ; Traverse the array over the range [ i , N ] ; Update frequency of current subarray in map ; Increment count ; Decrement count ; If all subarrays have at least 1 unique element ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; import java . lang . * ; class GFG { static String check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Map < Integer , Integer > hm = new HashMap < > ( ) ; int count = 0 ; for ( int j = i ; j < n ; j ++ ) { hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . get ( arr [ j ] ) == 1 ) count ++ ; if ( hm . get ( arr [ j ] ) == 2 ) count -- ; if ( count == 0 ) return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 } ; int N = arr . length ; System . out . println ( check ( arr , N ) ) ; } }
Count all disjoint pairs having absolute difference at least K from a given array | Java program for the above approach ; Function to count distinct pairs with absolute difference atleast K ; Track the element that have been paired ; Stores count of distinct pairs ; Pick all elements one by one ; If already visited ; If already visited ; If difference is at least K ; Mark element as visited and increment the count ; Print the final count ; Driver Code ; Given arr [ ] ; Size of array ; Given difference K ; Function Call
import java . util . * ; class GFG { static void countPairsWithDiffK ( int arr [ ] , int N , int K ) { int [ ] vis = new int [ N ] ; Arrays . fill ( vis , 0 ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( vis [ i ] == 1 ) continue ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( vis [ j ] == 1 ) continue ; if ( Math . abs ( arr [ i ] - arr [ j ] ) >= K ) { count ++ ; vis [ i ] = 1 ; vis [ j ] = 1 ; break ; } } } System . out . print ( count ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 3 , 5 } ; int N = arr . length ; int K = 2 ; countPairsWithDiffK ( arr , N , K ) ; } }
Check if two trees are Mirror | A binary tree node ; Given two trees , return true if they are mirror of each other ; Base case : Both empty ; If only one is empty ; Both non - empty , compare them recursively Note that in recursive calls , we pass left of one tree and right of other tree ; Driver code to test above methods
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node a , b ; boolean areMirror ( Node a , Node b ) { if ( a == null && b == null ) return true ; if ( a == null b == null ) return false ; return a . data == b . data && areMirror ( a . left , b . right ) && areMirror ( a . right , b . left ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node a = new Node ( 1 ) ; Node b = new Node ( 1 ) ; a . left = new Node ( 2 ) ; a . right = new Node ( 3 ) ; a . left . left = new Node ( 4 ) ; a . left . right = new Node ( 5 ) ; b . left = new Node ( 3 ) ; b . right = new Node ( 2 ) ; b . right . left = new Node ( 5 ) ; b . right . right = new Node ( 4 ) ; if ( tree . areMirror ( a , b ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the length of largest subarray with 0 sum | A Java program to find maximum length subarray with 0 sum ; Returns length of the maximum length subarray with 0 sum ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; Look this sum in hash table ; Else put this sum in hash table ; Drive method
import java . util . HashMap ; class MaxLenZeroSumSub { static int maxLen ( int arr [ ] ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; Integer prev_i = hM . get ( sum ) ; if ( prev_i != null ) max_len = Math . max ( max_len , i - prev_i ) ; else hM . put ( sum , i ) ; } return max_len ; } public static void main ( String arg [ ] ) { int arr [ ] = { 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 23 } ; System . out . println ( " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ " + maxLen ( arr ) ) ; } }
Print alternate elements of an array | Java program to implement the above approach ; Function to print Alternate elements of the given array ; Print elements at odd positions ; Print elements of array ; Driver Code
import java . io . * ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex += 2 ) { System . out . print ( arr [ currIndex ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; printAlter ( arr , N ) ; } }
Longest Increasing consecutive subsequence | Java program to find length of the longest increasing subsequence whose adjacent element differ by 1 ; function that returns the length of the longest increasing subsequence whose adjacent element differ by 1 ; create hashmap to save latest consequent number as " key " and its length as " value " ; put first element as " key " and its length as " value " ; iterate for all element ; check if last consequent of arr [ i ] exist or not ; put the updated consequent number and increment its value ( length ) ; driver code
import java . util . * ; class lics { static int LongIncrConseqSubseq ( int arr [ ] , int n ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( map . containsKey ( arr [ i ] - 1 ) ) { map . put ( arr [ i ] , map . get ( arr [ i ] - 1 ) + 1 ) ; map . remove ( arr [ i ] - 1 ) ; } else { map . put ( arr [ i ] , 1 ) ; } } return Collections . max ( map . values ( ) ) ; } public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = sc . nextInt ( ) ; System . out . println ( LongIncrConseqSubseq ( arr , n ) ) ; } }
Longest subsequence such that difference between adjacents is one | Set 2 | Java implementation to find longest subsequence such that difference between adjacents is one ; function to find longest subsequence such that difference between adjacents is one ; hash table to map the array element with the length of the longest subsequence of which it is a part of and is the last element of that subsequence ; to store the longest length subsequence ; traverse the array elements ; initialize current length for element arr [ i ] as 0 ; if ' arr [ i ] -1' is in ' um ' and its length of subsequence is greater than ' len ' ; if ' arr [ i ] + 1' is in ' um ' and its length of subsequence is greater than ' len ' ; update arr [ i ] subsequence length in ' um ' ; update longest length ; required longest length subsequence ; Driver Code
import java . util . * ; class GFG { static int longLenSub ( int [ ] arr , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . containsKey ( arr [ i ] - 1 ) && len < um . get ( arr [ i ] - 1 ) ) len = um . get ( arr [ i ] - 1 ) ; if ( um . containsKey ( arr [ i ] + 1 ) && len < um . get ( arr [ i ] + 1 ) ) len = um . get ( arr [ i ] + 1 ) ; um . put ( arr [ i ] , len + 1 ) ; if ( longLen < um . get ( arr [ i ] ) ) longLen = um . get ( arr [ i ] ) ; } return longLen ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( " Longest ▁ length ▁ subsequence ▁ = ▁ " + longLenSub ( arr , n ) ) ; } }
Count nodes having highest value in the path from root to itself in a Binary Tree | Java program for the above approach ; Stores the count of nodes which are maximum in the path from root to the current node ; Binary Tree Node ; Function that performs Inorder Traversal on the Binary Tree ; If root does not exist ; Check if the node satisfies the condition ; Update the maximum value and recursively traverse left and right subtree ; Function that counts the good nodes in the given Binary Tree ; Perform inorder Traversal ; Return the final count ; Function that add the new node in the Binary Tree ; Return the node ; Driver Code ; A Binary Tree 3 / \ 2 5 / \ 4 6 ; Function Call ; Print the count of good nodes
import java . util . * ; class GfG { static int count = 0 ; static class Node { int val ; Node left , right ; } static void find ( Node root , int max ) { if ( root == null ) return ; if ( root . val >= max ) count ++ ; find ( root . left , Math . max ( max , root . val ) ) ; find ( root . right , Math . max ( max , root . val ) ) ; } static int NodesMaxInPath ( Node root ) { find ( root , Integer . MIN_VALUE ) ; return count ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 7 ) ; int answer = NodesMaxInPath ( root ) ; System . out . println ( answer ) ; } }
Longest Consecutive Subsequence | Java program to find longest contiguous subsequence ; Returns length of the longest contiguous subsequence ; Sort the array ; Insert repeated elements only once in the vector ; Find the maximum length by traversing the array ; Check if the current element is equal to previous element + 1 ; Update the maximum ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int findLongestConseqSubseq ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 , count = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; v . add ( 10 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) v . add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( i > 0 && v . get ( i ) == v . get ( i - 1 ) + 1 ) count ++ ; else count = 1 ; ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ " + " contiguous ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
Count smaller primes on the right of each array element | Java Program for the above approach include " bits / stdJava . h " ; Function to check if a number is prime or not ; Function to update a Binary Tree ; Function to find the sum of all the elements which are less than or equal to index ; Function to find the number of smaller primes on the right for every array element ; Iterate the array in backwards ; Calculating the required number of primes ; If current array element is prime ; Update the Fenwick tree ; Driver Code ; Function call
import java . util . * ; class GFG { static int maxn = ( int ) 1e6 + 5 ; static int [ ] BITree = new int [ maxn ] ; static boolean is_prime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void update_bitree ( int BITree [ ] , int index , int value ) { while ( index <= maxn ) { BITree [ index ] += value ; index += ( index & ( - index ) ) ; } } static int sum_bitree ( int BITree [ ] , int index ) { int s = 0 ; while ( index > 0 ) { s += BITree [ index ] ; index -= ( index & ( - index ) ) ; } return s ; } static void countSmallerPrimes ( int BITree [ ] , int ar [ ] , int N ) { int [ ] ans = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { ans [ i ] = sum_bitree ( BITree , ar [ i ] ) ; if ( is_prime ( ar [ i ] ) ) update_bitree ( BITree , ar [ i ] , 1 ) ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 5 , 5 , 17 , 9 , 12 , 15 , 11 , 7 , 39 , 3 } ; int N = ar . length ; countSmallerPrimes ( BITree , ar , N ) ; } }
Longest Consecutive Subsequence | Java program to find longest consecutive subsequence ; Returns length of the longest consecutive subsequence ; Hash all the array elements ; check each possible sequence from the start then update optimal length ; if current element is the starting element of a sequence ; Then check for next elements in the sequence ; update optimal length if this length is more ; Driver Code
import java . io . * ; import java . util . * ; class ArrayElements { static int findLongestConseqSubseq ( int arr [ ] , int n ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) S . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! S . contains ( arr [ i ] - 1 ) ) { int j = arr [ i ] ; while ( S . contains ( j ) ) j ++ ; if ( ans < j - arr [ i ] ) ans = j - arr [ i ] ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
Longest Consecutive Subsequence | Java Program to find longest consecutive subsequence This Program uses Priority Queue ; return the length of the longest subsequence of consecutive integers ; adding element from array to PriorityQueue ; Storing the first element of the Priority Queue This first element is also the smallest element ; Taking a counter variable with value 1 ; Storing value of max as 1 as there will always be one element ; check if current peek element minus previous element is greater then 1 This is done because if it ' s ▁ greater ▁ than ▁ 1 ▁ ▁ then ▁ the ▁ sequence ▁ ▁ doesn ' t start or is broken here ; Store the value of counter to 1 As new sequence may begin ; Update the previous position with the current peek And remove it ; Check if the previous element and peek are same ; Update the previous position with the current peek And remove it ; if the difference between previous element and peek is 1 ; Update the counter These are consecutive elements ; Update the previous position with the current peek And remove it ; Check if current longest subsequence is the greatest ; Store the current subsequence count as max ; Driver Code
import java . io . * ; import java . util . PriorityQueue ; public class Longset_Sub { static int findLongestConseqSubseq ( int arr [ ] , int N ) { PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . add ( arr [ i ] ) ; } int prev = pq . poll ( ) ; int c = 1 ; int max = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( pq . peek ( ) - prev > 1 ) { c = 1 ; prev = pq . poll ( ) ; } else if ( pq . peek ( ) - prev == 0 ) { prev = pq . poll ( ) ; } else { c ++ ; prev = pq . poll ( ) ; } if ( max < c ) { max = c ; } } return max ; } public static void main ( String args [ ] ) throws IOException { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
Kth space | Java program of the above approach ; Function to extract integer at key position in the given String ; StringTokenizer ( ) : Extracts the number at key c_str ( ) : Type cast String to char * ; Driver Code ; Given String ; Given K ; Function call
import java . util . * ; class GFG { static void print_kth_String ( String str , int K ) { StringTokenizer st = new StringTokenizer ( str ) ; int count = 1 ; while ( st . hasMoreTokens ( ) && K > 0 ) { if ( count == K ) System . out . println ( st . nextToken ( ) ) ; count ++ ; st . nextToken ( ) ; } } public static void main ( String [ ] args ) { String s = ( "10 ▁ 20 ▁ 30 ▁ 40" ) ; int K = 2 ; print_kth_String ( s , K ) ; } }
Largest increasing subsequence of consecutive integers | Java implementation of longest continuous increasing subsequence ; Function for LIS ; Initialize result ; iterate through array and find end index of LIS and its Size ; print LIS size ; print LIS after setting start element ; Driver code
import java . util . * ; class GFG { static void findLIS ( int A [ ] , int n ) { Map < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; int LIS_size = 1 ; int LIS_index = 0 ; hash . put ( A [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { hash . put ( A [ i ] , hash . get ( A [ i ] - 1 ) == null ? 1 : hash . get ( A [ i ] - 1 ) + 1 ) ; if ( LIS_size < hash . get ( A [ i ] ) ) { LIS_size = hash . get ( A [ i ] ) ; LIS_index = A [ i ] ; } } System . out . println ( " LIS _ size ▁ = ▁ " + LIS_size ) ; System . out . print ( " LIS ▁ : ▁ " ) ; int start = LIS_index - LIS_size + 1 ; while ( start <= LIS_index ) { System . out . print ( start + " ▁ " ) ; start ++ ; } } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 3 , 7 , 4 , 8 , 5 , 13 , 6 } ; int n = A . length ; findLIS ( A , n ) ; } }
Count subsets having distinct even numbers | Java implementation to count subsets having even numbers only and all are distinct ; function to count the required subsets ; inserting even numbers in the set ' us ' single copy of each number is retained ; counting distinct even numbers ; total count of required subsets ; Driver code
import java . util . * ; class GFG { static int countSubsets ( int arr [ ] , int n ) { HashSet < Integer > us = new HashSet < > ( ) ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . add ( arr [ i ] ) ; even_count = us . size ( ) ; return ( int ) ( Math . pow ( 2 , even_count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = arr . length ; System . out . println ( " Number ▁ of ▁ subsets ▁ = ▁ " + countSubsets ( arr , n ) ) ; } }
Count distinct elements in every window of size k | Simple Java program to count distinct elements in every window of size k ; Counts distinct elements in window of size k ; Traverse the window ; Check if element arr [ i ] exists in arr [ 0. . i - 1 ] ; Counts distinct elements in all windows of size k ; Traverse through every window ; Driver method
import java . util . Arrays ; class Test { static int countWindowDistinct ( int win [ ] , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } static void countDistinct ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) System . out . println ( countWindowDistinct ( Arrays . copyOfRange ( arr , i , arr . length ) , k ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 3 } , k = 4 ; countDistinct ( arr , arr . length , k ) ; } }