codeparrot/xlcost-text-to-code huggingface dataset:
Minimum common element in subarrays of all possible lengths | Java program to implement the above approach ; Function to find maximum distance between every two element ; Stores index of last occurence of each array element ; Initialize temp [ ] with - 1 ; Traverse the array ; If array element has not occurred previously ; Update index in temp ; Otherwise ; Compare temp [ a [ i ] ] with distance from its previous occurence and store the maximum ; Compare temp [ i ] with distance of its last occurence from the end of the array and store the maximum ; Function to find the minimum common element in subarrays of all possible lengths ; Function call to find a the maximum distance between every pair of repetition ; Initialize ans [ ] to - 1 ; Check if subarray of length temp [ i ] contains i as one of the common elements ; Find the minimum of all common elements ; Driver Code
import java . util . * ; class GFG { static void max_distance ( int a [ ] , int temp [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { temp [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == null ) temp [ a [ i ] ] = i + 1 ; else temp [ a [ i ] ] = Math . max ( temp [ a [ i ] ] , i - mp . getOrDefault ( a [ i ] , 0 ) ) ; mp . put ( a [ i ] , i ) ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] != - 1 ) temp [ i ] = Math . max ( temp [ i ] , n - mp . getOrDefault ( i , 0 ) ) ; } } static void min_comm_ele ( int a [ ] , int ans [ ] , int temp [ ] , int n ) { max_distance ( a , temp , n ) ; for ( int i = 1 ; i <= n ; i ++ ) { ans [ i ] = - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] >= 0 && ans [ temp [ i ] ] == - 1 ) ans [ temp [ i ] ] = i ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( i > 1 && ans [ i - 1 ] != - 1 ) { if ( ans [ i ] == - 1 ) ans [ i ] = ans [ i - 1 ] ; else ans [ i ] = Math . min ( ans [ i ] , ans [ i - 1 ] ) ; } System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 6 ; int a [ ] = { 1 , 3 , 4 , 5 , 6 , 7 } ; int [ ] temp = new int [ 100 ] ; Arrays . fill ( temp , 0 ) ; int [ ] ans = new int [ 100 ] ; Arrays . fill ( ans , 0 ) ; min_comm_ele ( a , ans , temp , N ) ; } }
Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | Java program to implement the above approach ; Function to find a triplet that satisfy the conditions ; Traverse the given array ; Stores current element ; Stores element just before the current element ; Stores element just after the current element ; Check the given conditions ; Print a triplet ; If no triplet found ; Driver Code
import java . util . * ; class GFG { static void FindTrip ( int arr [ ] , int N ) { for ( int i = 1 ; i < N - 1 ; i ++ ) { int p = arr [ i - 1 ] ; int q = arr [ i ] ; int r = arr [ i + 1 ] ; if ( p < q && q > r ) { System . out . println ( ( i - 1 ) + " ▁ " + ( i ) + " ▁ " + ( i + 1 ) ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 4 , 3 } ; int N = arr . length ; FindTrip ( arr , N ) ; } }
Count subarrays having each distinct element occurring at least twice | Java program to implement the above approach ; Function to get the count of subarrays having each element occurring at least twice ; Stores count of subarrays having each distinct element occurring at least twice ; Stores count of unique elements in a subarray ; Store frequency of each element of a subarray ; Traverse the given array ; Count frequency and check conditions for each subarray ; Update frequency ; Check if frequency of arr [ j ] equal to 1 ; Update Count of unique elements ; Update count of unique elements ; If each element of subarray occurs at least twice ; Update cntSub ; Remove all elements from the subarray ; Update cntUnique ; Driver Code
import java . util . * ; class GFG { static int cntSubarrays ( int arr [ ] , int N ) { int cntSub = 0 ; int cntUnique = 0 ; Map < Integer , Integer > cntFreq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j ++ ) { cntFreq . put ( arr [ j ] , cntFreq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( cntFreq . get ( arr [ j ] ) == 1 ) { cntUnique ++ ; } else if ( cntFreq . get ( arr [ j ] ) == 2 ) { cntUnique -- ; } if ( cntUnique == 0 ) { cntSub ++ ; } } cntFreq . clear ( ) ; cntUnique = 0 ; } return cntSub ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 2 , 2 } ; int N = arr . length ; System . out . println ( cntSubarrays ( arr , N ) ) ; } }
Check if permutation of a given string can be made palindromic by removing at most K characters | Java program to implement the above approach ; Function to check if the string satisfies the given conditions or not ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; Stores count of distinct character whose frequency is odd ; Traverse the cntFreq [ ] array . ; If frequency of character i is odd ; Update cntOddFreq ; If count of distinct character having odd frequency is <= K + 1 ; Driver Code ; If str satisfy the given conditions
import java . util . * ; class GFG { public static boolean checkPalinK ( String str , int K ) { int N = str . length ( ) ; int cntFreq [ ] = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ str . charAt ( i ) ] ++ ; } int cntOddFreq = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % 2 == 1 ) { cntOddFreq ++ ; } } if ( cntOddFreq <= ( K + 1 ) ) { return true ; } return false ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int K = 2 ; if ( checkPalinK ( str , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Maximum number of elements that can be removed such that MEX of the given array remains unchanged | Java program for the above approach ; Function to find the maximum number of elements that can be removed ; Initialize hash [ ] with 0 s ; Initialize MEX ; Set hash [ i ] = 1 , if i is present in arr [ ] ; Find MEX from the hash ; Print the maximum numbers that can be removed ; Driver Code ; Given array ; Size of the array ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void countRemovableElem ( int [ ] arr , int N ) { int [ ] hash = new int [ N + 1 ] ; Arrays . fill ( hash , 0 ) ; int mex = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= N ) hash [ arr [ i ] ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] == 0 ) { mex = i ; break ; } } System . out . println ( N - ( mex - 1 ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 6 } ; int N = arr . length ; countRemovableElem ( arr , N ) ; } }
Print all positions of a given string having count of smaller characters equal on both sides | Java program to implement the above approach ; Function to find indexes of the given String that satisfy the condition ; Stores length of given String ; Stores frequency of each character of str ; Update frequency of current character ; cntLeftFreq [ i ] Stores frequency of characters present on the left side of index i . ; Traverse the given String ; Stores count of smaller characters on left side of i . ; Stores count of smaller characters on Right side of i . ; Traverse smaller characters on left side of index i . ; Update cntLeft ; Update cntRight ; Update cntLeftFreq [ str [ i ] ] ; If count of smaller elements on both sides equal ; Print current index ; ; Driver Code
import java . util . * ; class GFG { static void printIndexes ( char [ ] str ) { int N = str . length ; int [ ] cntFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] ] ++ ; } int [ ] cntLeftFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { int cntLeft = 0 ; int cntRight = 0 ; for ( int j = str [ i ] - 1 ; j >= 0 ; j -- ) { cntLeft += cntLeftFreq [ j ] ; cntRight += cntFreq [ j ] - cntLeftFreq [ j ] ; } cntLeftFreq [ str [ i ] ] ++ ; if ( cntLeft == cntRight && cntLeft != 0 ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String str = " aabacdabbb " ; printIndexes ( str . toCharArray ( ) ) ; } }
Count three | Java program to implement the above approach ; Function to count three - digit numbers having difference x with its reverse ; If x is not multiple of 99 ; No solution exists ; Generate all possible pairs of digits [ 1 , 9 ] ; If any pair is obtained with difference x / 99 ; Increase count ; Return the count ; Driver Code
import java . io . * ; import java . util . Arrays ; class GFG { static int Count_Number ( int x ) { int ans = 0 ; if ( x % 99 != 0 ) { ans = - 1 ; } else { int diff = x / 99 ; for ( int i = 1 ; i < 10 ; i ++ ) { for ( int j = 1 ; j < 10 ; j ++ ) { if ( ( i - j ) == diff ) { ans += 10 ; } } } } return ans ; } public static void main ( String [ ] args ) { int x = 792 ; System . out . println ( Count_Number ( x ) ) ; } }
Check if a point having maximum X and Y coordinates exists or not | Java program for the above approach ; Initialize INF as inifnity ; Function to return the point having maximum X and Y coordinates ; Base Case ; Stores if valid point exists ; If point arr [ i ] is valid ; Check for the same point ; Check for a valid point ; If current point is the required point ; Otherwise ; Function to find the required point ; Stores the point with maximum X and Y - coordinates ; If no required point exists ; Driver Code ; Given array of points ; Function Call
import java . io . * ; class GFG { static int INF = Integer . MAX_VALUE ; static int [ ] findMaxPoint ( int arr [ ] [ ] , int i , int n ) { if ( i == n ) return new int [ ] { INF , INF } ; boolean flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == i ) continue ; if ( arr [ j ] [ 0 ] >= arr [ i ] [ 0 ] arr [ j ] [ 1 ] >= arr [ i ] [ 1 ] ) { flag = false ; break ; } } if ( flag ) return arr [ i ] ; return findMaxPoint ( arr , i + 1 , n ) ; } static void findMaxPoints ( int arr [ ] [ ] , int n ) { int ans [ ] = findMaxPoint ( arr , 0 , n ) ; if ( ans [ 0 ] == INF ) { System . out . println ( - 1 ) ; } else { System . out . println ( " ( " + ans [ 0 ] + " ▁ " + ans [ 1 ] + " ) " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; int N = arr . length ; findMaxPoints ( arr , N ) ; } }
Check if a point having maximum X and Y coordinates exists or not | Java program for the above approach ; Function to find the point having max X and Y coordinates ; Initialize maxX and maxY ; Length of the given array ; Get maximum X & Y coordinates ; Check if the required point i . e . , ( maxX , maxY ) is present ; If point with maximum X and Y coordinates is present ; If no such point exists ; Driver Code ; Given array of points ; Print answer
import java . io . * ; class GFG { static void findMaxPoint ( int arr [ ] [ ] ) { int maxX = Integer . MIN_VALUE ; int maxY = Integer . MIN_VALUE ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { maxX = Math . max ( maxX , arr [ i ] [ 0 ] ) ; maxY = Math . max ( maxY , arr [ i ] [ 1 ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( maxX == arr [ i ] [ 0 ] && maxY == arr [ i ] [ 1 ] ) { System . out . println ( " ( " + maxX + " , ▁ " + maxY + " ) " ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; findMaxPoint ( arr ) ; } }
Search insert position of K in a sorted array | Java program for the above approach ; Function to find insert position of K ; Traverse the array ; If K is found ; If current array element exceeds K ; If all elements are smaller than K ; Driver Code
import java . io . * ; class GFG { static int find_index ( int [ ] arr , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == K ) return i ; else if ( arr [ i ] > K ) return i ; return n ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = arr . length ; int K = 2 ; System . out . println ( find_index ( arr , n , K ) ) ; } }
Count subarrays for every array element in which they are the minimum | Set 2 | Java program for the above approach ; Function to calculate total number of sub - arrays for each element where that element is occurring as the minimum element ; Map for storing the number of sub - arrays for each element ; Traverse over all possible subarrays ; Minimum in each subarray ; Print the result ; Driver Code ; Function Call
import java . util . * ; class GFG { static void minSubarray ( int [ ] arr , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int mini = Integer . MAX_VALUE ; for ( int j = i ; j < N ; j ++ ) { mini = Math . min ( mini , arr [ j ] ) ; if ( mp . containsKey ( mini ) ) { mp . put ( mini , mp . get ( mini ) + 1 ) ; } else { mp . put ( mini , 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( mp . get ( arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int N = arr . length ; minSubarray ( arr , N ) ; } }
Count subarrays for every array element in which they are the minimum | Set 2 | Java program for the above approach ; Function to count subarrays for each element where it is the minimum ; For the length of strictly larger numbers on the left of A [ i ] ; Storing x in result [ i ] ; For the length of strictly larger numbers on the right of A [ i ] ; Store x * y in result array ; Print the result ; Driver Code ; Function Call
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void minSubarray ( int [ ] arr , int N ) { int [ ] result = new int [ N ] ; Stack < pair > l = new Stack < > ( ) ; Stack < pair > r = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int count = 1 ; while ( ! l . isEmpty ( ) && l . peek ( ) . first > arr [ i ] ) { count += l . peek ( ) . second ; l . pop ( ) ; } l . add ( new pair ( arr [ i ] , count ) ) ; result [ i ] = count ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { int count = 1 ; while ( ! r . isEmpty ( ) && r . peek ( ) . first >= arr [ i ] ) { count += r . peek ( ) . second ; r . pop ( ) ; } r . add ( new pair ( arr [ i ] , count ) ) ; result [ i ] *= count ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( result [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int N = arr . length ; minSubarray ( arr , N ) ; } }
Count Full Prime numbers in a given range | Java program to implement the above approach ; Function to check if a number is prime or not ; If a divisor of n exists ; Function to check if a number is Full Prime or not ; If n is not a prime ; Otherwise ; Extract digit ; If any digit of n is non - prime ; Function to print count of Full Primes in a range [ L , R ] ; Stores count of full primes ; Check if i is full prime ; Driver Code ; Stores count of full primes
import java . io . * ; import java . util . * ; class GFG { static boolean isPrime ( int num ) { if ( num <= 1 ) return false ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( num % i == 0 ) return false ; return true ; } static boolean isFulPrime ( int n ) { if ( ! isPrime ( n ) ) return false ; else { while ( n > 0 ) { int rem = n % 10 ; if ( ! ( rem == 2 rem == 3 rem == 5 rem == 7 ) ) return false ; n = n / 10 ; } } return true ; } static int countFulPrime ( int L , int R ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( ( i % 2 ) != 0 && isFulPrime ( i ) ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; int ans = 0 ; if ( L < 3 ) ans ++ ; System . out . println ( ans + countFulPrime ( L , R ) ) ; } }
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 ; If currIndex stores even index or odd position ; Driver Code
import java . io . * ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex ++ ) { if ( currIndex % 2 == 0 ) { 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 ) ; } }
Minimize deletions in a Binary String to remove all subsequences of the form "0101" | Java Program to implement the above approach ; Function to find minimum characters to be removed such that no subsequence of the form "0101" exists in the string ; Stores the partial sums ; Calculate partial sums ; Setting endpoints and deleting characters indices . ; Return count of deleted characters ; Driver Code
import java . io . * ; class GFG { static int findmin ( String s ) { int n = s . length ( ) ; int i , j , maximum = 0 ; int [ ] incr = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { incr [ i + 1 ] = incr [ i ] ; if ( s . charAt ( i ) == '0' ) { incr [ i + 1 ] ++ ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { maximum = Math . max ( maximum , incr [ i ] + j - i + 1 - ( incr [ j + 1 ] - incr [ i ] ) + incr [ n ] - incr [ j + 1 ] ) ; } } return n - maximum ; } public static void main ( String [ ] args ) { String S = "0110100110" ; int minimum = findmin ( S ) ; System . out . println ( minimum ) ; } }
Length of longest subarray with product equal to a power of 2 | Java program for the above approach ; Function to check whether a number is power of 2 or not ; Function to find maximum length subarray having product of element as a perfect power of 2 ; Stores current subarray length ; Stores maximum subarray length ; Traverse the given array ; If arr [ i ] is power of 2 ; Increment max_length ; Update max_len_subarray ; Otherwise ; Print the maximum length ; Driver Code ; Given arr [ ] ; Function Call
import java . util . * ; class GFG { public static boolean isPower ( int x ) { if ( x != 0 && ( x & ( x - 1 ) ) == 0 ) return true ; return false ; } public static void maximumlength ( int arr [ ] , int N ) { int max_length = 0 ; int max_len_subarray = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( isPower ( arr [ i ] ) ) { max_length ++ ; max_len_subarray = Math . max ( max_length , max_len_subarray ) ; } else { max_length = 0 ; } } System . out . println ( max_len_subarray ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 5 , 4 , 6 , 8 , 8 , 2 } ; int N = arr . length ; maximumlength ( arr , N ) ; } }
Length of smallest subarray to be removed such that the remaining array is sorted | Java program for the above approach ; Find the length of the shortest subarray ; To store the result ; Calculate the possible length of the sorted subarray from left ; Array is sorted ; Calculate the possible length of the sorted subarray from left ; Update the result ; Calculate the possible length in the middle we can delete and update the result ; Update the result ; Return the result ; Driver Code ; Function call
import java . util . * ; class GFG { static int findLengthOfShortestSubarray ( int arr [ ] , int N ) { int minlength = Integer . MAX_VALUE ; int left = 0 ; int right = N - 1 ; while ( left < right && arr [ left + 1 ] >= arr [ left ] ) { left ++ ; } if ( left == N - 1 ) return 0 ; while ( right > left && arr [ right - 1 ] <= arr [ right ] ) { right -- ; } minlength = Math . min ( N - left - 1 , right ) ; int j = right ; for ( int i = 0 ; i < left + 1 ; i ++ ) { if ( arr [ i ] <= arr [ j ] ) { minlength = Math . min ( minlength , j - i - 1 ) ; } else if ( j < N - 1 ) { j ++ ; } else { break ; } } return minlength ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 } ; int N = arr . length ; System . out . print ( ( findLengthOfShortestSubarray ( arr , N ) ) ) ; } }
Smallest number to be subtracted to convert given number to a palindrome | Java program for the above approach ; Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call
import java . util . * ; class GFG { static void minSub ( int N ) { int count = 0 ; while ( N >= 0 ) { int num = N ; int rev = 0 ; while ( num != 0 ) { int digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = num / 10 ; } if ( N == rev ) { break ; } count ++ ; N -- ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 3456 ; minSub ( N ) ; } }
Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | Java program for the above approach ; Function to count all the subarrays of size at least 3 forming GP ; If array size is less than 3 ; Stores the count of subarray ; Stores the count of subarray for each iteration ; Traverse the array ; Check if L [ i ] forms GP ; Otherwise , update count to 0 ; Update the final count ; Return the final count ; Driver Code ; Given array arr [ ] ; Function Call
import java . util . * ; class GFG { static int numberOfGP ( int L [ ] , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int i = 2 ; i < N ; ++ i ) { if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 24 } ; int N = arr . length ; System . out . print ( numberOfGP ( arr , N ) ) ; } }
Count even length subarrays having bitwise XOR equal to 0 | Java program to implement the above approach ; Function to count the number of even - length subarrays having Bitwise XOR equal to 0 ; Stores the count of required subarrays ; Stores prefix - XOR of arr [ i , i + 1 , ... N - 1 ] ; Traverse the array ; Calculate the prefix - XOR of current subarray ; Check if XOR of the current subarray is 0 and length is even ; Driver Code
import java . io . * ; class GFG { static int cntSubarr ( int arr [ ] , int N ) { int res = 0 ; int prefixXor = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixXor = arr [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { prefixXor ^= arr [ j ] ; if ( prefixXor == 0 && ( j - i + 1 ) % 2 == 0 ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . length ; System . out . println ( cntSubarr ( arr , N ) ) ; } }
Count even length subarrays having bitwise XOR equal to 0 | Java program to implement the above approach ; Function to get the count of even length subarrays having bitwise xor 0 ; Stores prefix - xor of the given array ; Stores prefix - xor at even index of the array . ; Stores prefix - xor at odd index of the array . ; Stores count of subarrays that satisfy the condition ; length from 0 index to odd index is even ; Traverse the array . ; Take prefix - xor ; If index is odd ; Calculate pairs ; Increment prefix - xor at odd index ; Calculate pairs ; Increment prefix - xor at odd index ; Driver Code
import java . util . * ; class GFG { static final int M = 1000000 ; static int cntSubXor ( int arr [ ] , int N ) { int prefixXor = 0 ; int [ ] Even = new int [ M ] ; int [ ] Odd = new int [ M ] ; int cntSub = 0 ; Odd [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { prefixXor ^= arr [ i ] ; if ( i % 2 == 1 ) { cntSub += Odd [ prefixXor ] ; Odd [ prefixXor ] ++ ; } else { cntSub += Even [ prefixXor ] ; Even [ prefixXor ] ++ ; } } return cntSub ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . length ; System . out . print ( cntSubXor ( arr , N ) ) ; } }