codeparrot/xlcost-text-to-code huggingface dataset:
Minimum adjacent swaps to group similar characters together | Function to find minimum adjacent swaps required to make all the same character adjacent ; Initialize answer ; Create a 2D array of size 26 ; Traverse the String ; Get character ; Append the current index in the corresponding vector ; Traverse each character from a to z ; Add difference of adjacent index ; Return answer ; Given String ; Size of String ; Function Call
function minSwaps ( S , n ) { let swaps = 0 ; let arr = new Array ( 26 ) ; for ( let i = 0 ; i < arr . length ; i ++ ) arr [ i ] = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { let pos = S [ i ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ; arr [ pos ] . push ( i ) ; } for ( let ch = ' ' . charCodeAt ( 0 ) ; ch <= ' ' . charCodeAt ( 0 ) ; ++ ch ) { let pos = ch - ' ' . charCodeAt ( 0 ) ; for ( let i = 1 ; i < arr [ pos ] . length ; ++ i ) { swaps += Math . abs ( arr [ pos ] [ i ] - arr [ pos ] [ i - 1 ] - 1 ) ; } } return swaps ; } let S = " " ; let N = S . length ; document . write ( minSwaps ( S , N ) ) ;
Construct a graph which does not contain any pair of adjacent nodes with same value | Function that prints the edges of the generated graph ; First print connections stored in store [ ] ; Check if there is more than one occurrence of 1 st unique element ; Print all other occurrence of 1 st unique element with second unique element ; Function to construct the graph such that the every adjacent nodes have different value ; Stores pair of edges formed ; Stores first unique occurrence ; Check for the second unique occurrence ; Store indices of 2 nd unique occurrence ; To check if arr has only 1 unique element or not ; Store the connections of all unique elements with Node 1 ; If value at node ( i + 1 ) is same as value at Node 1 then store its indices ; If count is zero then it 's not  possible to construct the graph ; If more than 1 unique element is present ; Print the edges ; Driver Code ; Given array having node values ; Function Call
function printConnections ( store , ind , ind1 ) { for ( let pr = 0 ; pr < store . length ; pr ++ ) { document . write ( store [ pr ] [ 0 ] + " " + store [ pr ] [ 1 ] + " " ) ; } if ( ind . length != 0 ) { for ( let x = 0 ; x < ind . length ; x ++ ) { document . write ( ind1 + " " + ( ind [ x ] + 1 ) + " " ) ; } } } function constructGraph ( arr , N ) { let ind = [ ] ; let store = [ ] ; let x = arr [ 0 ] ; let count = 0 ; let ind1 = - 1 ; for ( let i = 1 ; i <= N - 1 ; ++ i ) { if ( arr [ i ] != x ) { ind1 = i + 1 ; count ++ ; store . push ( [ 1 , i + 1 ] ) ; } else { ind . push ( i ) ; } } if ( count == 0 ) { document . write ( " " ) ; } else { document . write ( " " + " " ) ; printConnections ( store , ind , ind1 ) ; } } let N = 5 ; let arr = [ ' ' , ' ' , ' ' , ' ' , ' ' ] ; constructGraph ( arr , N ) ;
Minimum substring flips required to convert given binary string to another | Function that finds the minimum number of operations required such that string A and B are the same ; Stores the count of steps ; Stores the last index whose bits are not same ; Iterate until both string are unequal ; Check till end of string to find rightmost unequals bit ; Update the last index ; Flipping characters up to the last index ; Flip the bit ; Increasing steps by one ; Print the count of steps ; Given strings A and B ; Function Call
function findMinimumOperations ( a , b ) { var step = 0 ; var last_index = 0 ; while ( a !== b ) { for ( var i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] !== b [ i ] ) { last_index = i ; } } var ch = a . split ( " " ) ; for ( var i = 0 ; i <= last_index ; i ++ ) { if ( ch [ i ] === " " ) { ch [ i ] = " " ; } else { ch [ i ] = " " ; } } a = ch . join ( " " ) ; step ++ ; } document . write ( step ) ; } var A = " " ; var B = " " ; findMinimumOperations ( A , B ) ;
Count all disjoint pairs having absolute difference at least K from a given array | Function to check if it is possible to form M pairs with abs diff at least K ; Traverse the array over [ 0 , M ] ; If valid index ; Return 1 ; Function to count distinct pairs with absolute difference atleasr K ; Stores the count of all possible pairs ; Initialize left and right ; Sort the array ; Perform Binary Search ; Find the value of mid ; Check valid index ; Update ans ; Prvar the answer ; Given array arr ; Given difference K ; Size of the array ; Function call
function isValid ( arr , n , m , d ) { for ( i = 0 ; i < m ; i ++ ) { if ( Math . abs ( arr [ n - m + i ] - arr [ i ] ) < d ) { return 0 ; } } return 1 ; } function countPairs ( arr , N , K ) { var ans = 0 ; var left = 0 , right = N / 2 + 1 ; arr . sort ( ) ; while ( left < right ) { var mid = parseInt ( ( left + right ) / 2 ) ; if ( isValid ( arr , N , mid , K ) == 1 ) { ans = mid ; left = mid + 1 ; } else right = mid - 1 ; } document . write ( ans ) ; } var arr = [ 1 , 3 , 3 , 5 ] ; var K = 2 ; var N = arr . length ; countPairs ( arr , N , K ) ;
Minimum common element in subarrays of all possible lengths | 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
function max_distance ( a , temp , n ) { var mp = new Map ( ) ; for ( var i = 1 ; i <= n ; i ++ ) { temp [ i ] = - 1 ; } for ( var i = 0 ; i < n ; i ++ ) { if ( ! mp . has ( a [ i ] ) ) temp [ a [ i ] ] = i + 1 ; else temp [ a [ i ] ] = Math . max ( temp [ a [ i ] ] , i - mp [ a [ i ] ] ) ; mp . set ( a [ i ] , i ) ; } for ( var i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] != - 1 ) temp [ i ] = Math . max ( temp [ i ] , n - mp . get ( i ) ) ; } } function min_comm_ele ( a , ans , temp , n ) { max_distance ( a , temp , n ) ; for ( var i = 1 ; i <= n ; i ++ ) { ans [ i ] = - 1 ; } for ( var i = 1 ; i <= n ; i ++ ) { if ( ans [ temp [ i ] ] == - 1 ) ans [ temp [ i ] ] = i ; } for ( var 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 ] ) ; } document . write ( ans [ i ] + " " ) ; } } var N = 6 ; var a = [ 1 , 3 , 4 , 5 , 6 , 7 ] ; var temp = new Array ( 100 ) , ans = Array ( 100 ) ; min_comm_ele ( a , ans , temp , N ) ;
Find a triplet in an array such that arr [ i ] arr [ k ] and i < j < k | 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 ; Prlet a triplet ; If no triplet found ; Driver Code
function FindTrip ( arr , N ) { for ( let i = 1 ; i < N - 1 ; i ++ ) { let p = arr [ i - 1 ] ; let q = arr [ i ] ; let r = arr [ i + 1 ] ; if ( p < q && q > r ) { document . write ( ( i - 1 ) + " " + ( i ) + " " + ( i + 1 ) ) ; return ; } } document . write ( - 1 ) ; } let arr = [ 2 , 1 , 4 , 3 ] ; let N = arr . length ; FindTrip ( arr , N ) ;
Count subarrays having each distinct element occurring at least twice | 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
function cntSubarrays ( arr , N ) { var cntSub = 0 ; var cntUnique = 0 ; var cntFreq = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { for ( var j = i ; j < N ; j ++ ) { if ( cntFreq . has ( arr [ j ] ) ) cntFreq . set ( arr [ j ] , cntFreq . get ( arr [ j ] ) + 1 ) else cntFreq . set ( arr [ j ] , 1 ) ; if ( cntFreq . get ( arr [ j ] ) == 1 ) { cntUnique ++ ; } else if ( cntFreq . get ( arr [ j ] ) == 2 ) { cntUnique -- ; } if ( cntUnique == 0 ) { cntSub ++ ; } } cntFreq = new Map ( ) ; cntUnique = 0 ; } return cntSub ; } var arr = [ 1 , 1 , 2 , 2 , 2 ] ; var N = arr . length ; document . write ( cntSubarrays ( arr , N ) ) ;
Check if permutation of a given string can be made palindromic by removing at most K characters | 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
function checkPalinK ( str , K ) { var N = str . length ; var cntFreq = Array ( 256 ) . fill ( 0 ) ; var i ; for ( i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] ] += 1 ; } var cntOddFreq = 0 ; for ( i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % 2 == 1 ) { cntOddFreq ++ ; } } if ( cntOddFreq <= ( K + 1 ) ) { return true ; } return false ; } var str = " " ; var K = 2 ; if ( checkPalinK ( str , K ) ) { document . write ( " " ) ; } else { document . write ( " " ) ; }
Maximum number of elements that can be removed such that MEX of the given array remains unchanged | 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 ; Prlet the maximum numbers that can be removed ; Given array ; Size of the array ; Function Call
function countRemovableElem ( arr , N ) { let hash = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { hash [ i ] = 0 ; } let mex = N + 1 ; for ( let i = 0 ; i < N ; i ++ ) { if ( arr [ i ] <= N ) hash [ arr [ i ] ] = 1 ; } for ( let i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] == 0 ) { mex = i ; break ; } } document . write ( N - ( mex - 1 ) ) ; } let arr = [ 2 , 3 , 5 , 1 , 6 ] ; let N = arr . length ; countRemovableElem ( arr , N ) ;
Print all positions of a given string having count of smaller characters equal on both sides | 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
function printIndexes ( str ) { var N = str . length ; var cntFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { cntFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; } var cntLeftFreq = Array ( 256 ) . fill ( 0 ) ; for ( var i = 0 ; i < N ; i ++ ) { var cntLeft = 0 ; var cntRight = 0 ; for ( var j = str [ i ] . charCodeAt ( 0 ) - 1 ; j >= 0 ; j -- ) { cntLeft += cntLeftFreq [ j ] ; cntRight += cntFreq [ j ] - cntLeftFreq [ j ] ; } cntLeftFreq [ str [ i ] . charCodeAt ( 0 ) ] ++ ; if ( cntLeft == cntRight && cntLeft != 0 ) { document . write ( i + " " ) ; } } } var str = " " ; printIndexes ( str ) ;
Count three | 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
function Count_Number ( x ) { let ans = 0 ; if ( x % 99 != 0 ) { ans = - 1 ; } else { let diff = x / 99 ; for ( let i = 1 ; i < 10 ; i ++ ) { for ( let j = 1 ; j < 10 ; j ++ ) { if ( ( i - j ) == diff ) { ans += 10 ; } } } } return ans ; } let x = 792 ; document . write ( Count_Number ( x ) ) ;
Check if a point having maximum X and Y coordinates exists or not | Initialize INF as infinity ; 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 ; Given array of points ; Function Call
let INF = Number . MAX_VALUE ; function findMaxPoint ( arr , i , n ) { if ( i == n ) return [ INF , INF ] ; let flag = true ; for ( let 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 ) ; } function findMaxPoints ( arr , n ) { let ans = findMaxPoint ( arr , 0 , n ) ; if ( ans [ 0 ] == INF ) { document . write ( - 1 ) ; } else { document . write ( " " + ans [ 0 ] + " " + ans [ 1 ] + " " ) ; } } let arr = [ [ 1 , 2 ] , [ 2 , 1 ] , [ 3 , 4 ] , [ 4 , 3 ] , [ 5 , 5 ] ] ; let N = arr . length ; findMaxPoints ( arr , N ) ;
Search insert position of K in a sorted array | 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
function find_index ( arr , n , K ) { for ( let i = 0 ; i < n ; i ++ ) if ( arr [ i ] == K ) return i ; else if ( arr [ i ] > K ) return i ; return n ; } let arr = [ 1 , 3 , 5 , 6 ] ; let n = arr . length ; let K = 2 ; document . write ( find_index ( arr , n , K ) ) ;
Count subarrays for every array element in which they are the minimum | Set 2 | 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
function minSubarray ( arr , N ) { var m = new Map ( ) ; for ( var i = 0 ; i < N ; i ++ ) { var mini = 1000000000 ; for ( var j = i ; j < N ; j ++ ) { mini = Math . min ( mini , arr [ j ] ) ; if ( m . has ( mini ) ) m . set ( mini , m . get ( mini ) + 1 ) else m . set ( mini , 1 ) } } for ( var i = 0 ; i < N ; i ++ ) { document . write ( m . get ( arr [ i ] ) + " " ) ; } } var arr = [ 3 , 2 , 1 , 4 ] ; var N = arr . length ; minSubarray ( arr , N ) ;
Count subarrays for every array element in which they are the minimum | Set 2 | 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
function minSubarray ( arr , N ) { let result = new Array ( N ) ; for ( let i = 0 ; i < N ; i ++ ) result [ i ] = 0 ; let l = [ ] ; let r = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { let count = 1 ; while ( l . length != 0 && l [ l . length - 1 ] [ 0 ] > arr [ i ] ) { count += l [ l . length - 1 ] [ 1 ] ; l . pop ( ) ; } l . push ( [ arr [ i ] , count ] ) ; result [ i ] = count ; } for ( let i = N - 1 ; i >= 0 ; i -- ) { let count = 1 ; while ( r . length != 0 && r [ r . length - 1 ] [ 0 ] >= arr [ i ] ) { count += r [ r . length - 1 ] [ 1 ] ; r . pop ( ) ; } r . push ( [ arr [ i ] , count ] ) ; result [ i ] *= count ; } for ( let i = 0 ; i < N ; i ++ ) { document . write ( result [ i ] + " " ) ; } } let arr = [ 3 , 2 , 1 , 4 ] ; let N = arr . length ; minSubarray ( arr , N ) ;
Count Full Prime numbers in a given range | 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 prlet 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
function isPrime ( num ) { if ( num <= 1 ) return false ; for ( let i = 2 ; i * i <= num ; i ++ ) if ( num % i == 0 ) return false ; return true ; } function isFulPrime ( n ) { if ( ! isPrime ( n ) ) return false ; else { while ( n > 0 ) { let rem = n % 10 ; if ( ! ( rem == 2 rem == 3 rem == 5 rem == 7 ) ) return false ; n = Math . floor ( n / 10 ) ; } } return true ; } function countFulPrime ( L , R ) { let cnt = 0 ; for ( let i = L ; i <= R ; i ++ ) { if ( ( i % 2 ) != 0 && isFulPrime ( i ) ) { cnt ++ ; } } return cnt ; } let L = 1 , R = 100 ; let ans = 0 ; if ( L < 3 ) ans ++ ; document . write ( ans + countFulPrime ( L , R ) ) ;
Print alternate elements of an array | Function to print Alternate elements of the given array ; Print elements at odd positions ; If currIndex stores even index or odd position ; Driver Code
function printAlter ( arr , N ) { for ( var currIndex = 0 ; currIndex < N ; currIndex ++ ) { if ( currIndex % 2 == 0 ) { document . write ( arr [ currIndex ] + " " ) ; } } } var arr = [ 1 , 2 , 3 , 4 , 5 ] var N = arr . length ; printAlter ( arr , N ) ;
Minimize deletions in a Binary String to remove all subsequences of the form "0101" | 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
function findmin ( s ) { let n = s . length ; let i , j , maximum = 0 ; var incr = new Array ( n + 1 ) ; incr . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { incr [ i + 1 ] = incr [ i ] ; if ( s [ i ] == ' ' ) { 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 ; } let S = " " ; let minimum = findmin ( S ) ; document . write ( minimum + ' ' ) ;
Length of longest subarray with product equal to a power of 2 | 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 ; Given arr [ ] ; Function Call
function isPower ( x ) { return ( x && ( ! ( x & ( x - 1 ) ) ) ) ; } function maximumlength ( arr , N ) { var max_length = 0 ; var max_len_subarray = 0 ; for ( var i = 0 ; i < N ; i ++ ) { if ( isPower ( arr [ i ] ) == 1 ) { max_length ++ ; max_len_subarray = Math . max ( max_length , max_len_subarray ) ; } else { max_length = 0 ; } } document . write ( max_len_subarray ) ; } var arr = [ 2 , 5 , 4 , 6 , 8 , 8 , 2 ] ; var N = arr . length ; maximumlength ( arr , N ) ;
Length of smallest subarray to be removed such that the remaining array is sorted | 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
function findLengthOfShortestSubarray ( arr , N ) { let minlength = Number . MAX_VALUE ; let left = 0 ; let 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 ) ; let j = right ; for ( let 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 ; } let arr = [ 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 ] ; let N = arr . length ; document . write ( ( findLengthOfShortestSubarray ( arr , N ) ) ) ;
Smallest number to be subtracted to convert given number to a palindrome | 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
function minSub ( N ) { let count = 0 ; while ( N >= 0 ) { let num = N ; let rev = 0 ; while ( num != 0 ) { let digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = Math . floor ( num / 10 ) ; } if ( N == rev ) { break ; } count ++ ; N -- ; } document . write ( count ) ; } let N = 3456 ; minSub ( N ) ;
Count subarrays of atleast size 3 forming a Geometric Progression ( GP ) | 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 ; Given array arr [ ] ; Function Call
function numberOfGP ( L , N ) { if ( N <= 2 ) return 0 ; let count = 0 ; let res = 0 ; for ( let 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 ; } let arr = [ 1 , 2 , 4 , 8 , 16 , 24 ] ; let N = arr . length ; document . write ( numberOfGP ( arr , N ) ) ;
Count even length subarrays having bitwise XOR equal to 0 | 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
function cntSubarr ( arr , N ) { var res = 0 ; var prefixXor = 0 ; var i , j ; for ( i = 0 ; i < N - 1 ; i ++ ) { prefixXor = arr [ i ] ; for ( j = i + 1 ; j < N ; j ++ ) { prefixXor ^= arr [ j ] ; if ( prefixXor == 0 && ( j - i + 1 ) % 2 == 0 ) { res ++ ; } } } return res ; } var arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ] ; var N = arr . length ; document . write ( cntSubarr ( arr , N ) ) ;
Count even length subarrays having bitwise XOR equal to 0 | Javascript 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
let M = 1000000 ; function cntSubXor ( arr , N ) { let prefixXor = 0 ; let Even = Array . from ( { length : M } , ( _ , i ) => 0 ) ; let Odd = Array . from ( { length : M } , ( _ , i ) => 0 ) ; let cntSub = 0 ; Odd [ 0 ] = 1 ; for ( let i = 0 ; i < N ; i ++ ) { prefixXor = Math . floor ( prefixXor ^ arr [ i ] ) ; if ( i % 2 == 1 ) { cntSub += Odd [ prefixXor ] ; Odd [ prefixXor ] ++ ; } else { cntSub += Even [ prefixXor ] ; Even [ prefixXor ] ++ ; } } return cntSub ; } let arr = [ 2 , 2 , 3 , 3 , 6 , 7 , 8 ] ; let N = arr . length ; document . write ( cntSubXor ( arr , N ) ) ;