codeparrot/xlcost-text-to-code huggingface dataset:
Partitions possible such that the minimum element divides all the other elements of the partition | C ++ implementation of the approach ; Function to return the count partitions possible from the given array such that the minimum element of any partition divides all the other elements of that partition ; Initialize the count variable ; Find the minimum element ; Break if no minimum element present ; Increment the count if minimum element present ; Replace all the element divisible by min_elem ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPartitions ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int min_elem = * min_element ( A , A + N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % min_elem == 0 ) A [ i ] = INT_MAX ; } } return count ; } int main ( ) { int arr [ ] = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPartitions ( arr , N ) ; return 0 ; }
Minimum number of swaps to make two binary string equal | C ++ program for the above approach ; Function to calculate min swaps to make binary strings equal ; Count of zero 's ; Count of one 's ; As discussed above ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( string & s1 , string & s2 ) { int c0 = 0 , c1 = 0 ; for ( int i = 0 ; i < s1 . size ( ) ; i ++ ) { if ( s1 [ i ] == '0' && s2 [ i ] == '1' ) { c0 ++ ; } else if ( s1 [ i ] == '1' && s2 [ i ] == '0' ) { c1 ++ ; } } int ans = c0 / 2 + c1 / 2 ; if ( c0 % 2 == 0 && c1 % 2 == 0 ) { return ans ; } else if ( ( c0 + c1 ) % 2 == 0 ) { return ans + 2 ; } else { return -1 ; } } int main ( ) { string s1 = "0011" , s2 = "1111" ; int ans = minSwaps ( s1 , s2 ) ; cout << ans << ' ' ; return 0 ; }
Minimum changes required to make all element in an array equal | C ++ program to find minimum changes required to make all elements of the array equal ; Function to count of minimum changes required to make all elements equal ; Store the count of each element as key value pair in unordered map ; Find the count of maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minChanges ( int arr [ ] , int n ) { unordered_map < int , int > umap ; for ( int i = 0 ; i < n ; i ++ ) { umap [ arr [ i ] ] ++ ; } int maxFreq = 0 ; for ( auto p : umap ) { maxFreq = max ( maxFreq , p . second ) ; } return n - maxFreq ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minChanges ( arr , n ) << ' ' ; return 0 ; }
Minimum operations to make two numbers equal | C ++ implementation of the above approach ; Function to find the minimum no of operations ; find the maximum of two and store it in p ; increase it until it is achievable from given n and m ; Here value added to n and m will be S ( n ) = p - n + p - m ; check whether integer value of n exist by the formula n = ( - 1 + sqrt ( 1 + 8 * S ( n ) ) ) / 2 ; Driver code ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int n , int m ) { int a = 0 , k = 1 ; int p = max ( n , m ) ; while ( n != m ) { float s = ( float ) ( p - n + p - m ) ; float q = ( -1 + sqrt ( 8 * s + 1 ) ) / 2 ; if ( q - floor ( q ) == 0 ) { a = q ; n = m ; } p = p + 1 ; } return a ; } int main ( ) { int n = 1 , m = 3 ; cout << minOperations ( n , m ) ; return 0 ; }
Count of subarrays with sum at least K | C ++ implementation of the approach ; Function to return the number of subarrays with sum atleast k ; To store the right index and the current sum ; To store the number of sub - arrays ; For all left indexes ; Get elements till current sum is less than k ; No such subarray is possible ; Add all possible subarrays ; Remove the left most element ; Return the required answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int k_sum ( int a [ ] , int n , int k ) { int r = 0 , sum = 0 ; int ans = 0 ; for ( int l = 0 ; l < n ; l ++ ) { while ( sum < k ) { if ( r == n ) break ; else { sum += a [ r ] ; r ++ ; } } if ( sum < k ) break ; ans += n - r + 1 ; sum -= a [ l ] ; } return ans ; } int main ( ) { int a [ ] = { 6 , 1 , 2 , 7 } , k = 10 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << k_sum ( a , n , k ) ; return 0 ; }
Find if a crest is present in the index range [ L , R ] of the given array | C ++ implementation of the approach ; Function that returns true if the array contains a crest in the index range [ L , R ] ; To keep track of elements which satisfy the Property ; Property is satisfied for the current element ; Cumulative Sum ; If a crest is present in the given index range ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool hasCrest ( int arr [ ] , int n , int L , int R ) { int present [ n ] = { 0 } ; for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( int i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } int main ( ) { int arr [ ] = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int L = 2 ; int R = 6 ; if ( hasCrest ( arr , N , L , R ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Maximum Sum of Products of two arrays by toggling adjacent bits | C ++ program to find the maximum SoP of two arrays by toggling adjacent bits in the second array ; Function to return Max Sum ; intialParity and finalParity are 0 if total no . of 1 's is even else 1 ; minPositive and maxNegative will store smallest positive and smallest negative integer respectively . ; Count of Initial Parity ; if arr1 [ i ] is positive then add 1 in finalParity to get 1 at arr2 [ i ] ; if both parity are odd or even then return sum ; else add one more 1 or remove 1 ; if minPositive > maxNegative , put 1 at maxNegative and add it to our sum ; else remove minPositive no . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( int arr1 [ ] , int arr2 [ ] , int n ) { int initialParity = 0 , finalParity = 0 ; int sum = 0 , minPositive = INT_MAX , maxNegative = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { initialParity += arr2 [ i ] ; if ( arr1 [ i ] >= 0 ) { finalParity += 1 ; sum += arr1 [ i ] ; minPositive = min ( minPositive , arr1 [ i ] ) ; } else { maxNegative = max ( maxNegative , arr1 [ i ] ) ; } } if ( initialParity % 2 == finalParity % 2 ) { return sum ; } else { if ( minPositive + maxNegative >= 0 ) { return sum + maxNegative ; } else { return sum - minPositive ; } } } int main ( ) { int arr1 [ ] = { 2 , -4 , 5 , 3 } ; int arr2 [ ] = { 0 , 1 , 0 , 1 } ; int n = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << maxSum ( arr1 , arr2 , n ) << endl ; return 0 ; }
Minimum number to be added to all digits of X to make X > Y | C ++ program to find Minimum number to be added to all digits of X to make X > Y ; Function to check if X is lexicographically larger Y ; It is lexicographically larger ; Utility function to check minimum value of d ; If X is already larger do not need to add anything ; Adding d to all elements of X ; If X is larger now print d ; else print d + 1 ; Driver Code ; Taking the numbers as sequences
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool IsLarger ( int X [ ] , int Y [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } int solve ( int X [ ] , int Y [ ] , int n ) { int ans = 0 ; if ( IsLarger ( X , Y , n ) ) { ans = 0 ; } else { int d = Y [ 0 ] - X [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { X [ i ] += d ; } if ( IsLarger ( X , Y , n ) ) { ans = d ; } else { ans = d + 1 ; } } return ans ; } int main ( ) { int X [ ] = { 2 , 3 , 6 , 9 } ; int Y [ ] = { 3 , 4 , 8 , 1 } ; int n = sizeof ( X ) / sizeof ( X [ 0 ] ) ; cout << solve ( X , Y , n ) ; return 0 ; }
Maximum possible Bitwise OR of the two numbers from the range [ L , R ] | C ++ implementation of the approach ; Function to return the maximum bitwise OR of any pair from the given range ; Converting L to its binary representation ; Converting R to its binary representation ; In order to make the number of bits of L and R same ; Push 0 to the MSB ; When ith bit of R is 1 and ith bit of L is 0 ; From MSB side set all bits of L to be 1 ; From ( i + 1 ) th bit , all bits of L changed to be 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long int max_bitwise_or ( long long int L , long long int R ) { vector < long long int > v1 , v2 , v3 ; long long int z = 0 , i , ans = 0 , cnt = 1 ; while ( L > 0 ) { v1 . push_back ( L % 2 ) ; L = L / 2 ; } while ( R > 0 ) { v2 . push_back ( R % 2 ) ; R = R / 2 ; } while ( v1 . size ( ) != v2 . size ( ) ) { v1 . push_back ( 0 ) ; } for ( i = v2 . size ( ) - 1 ; i >= 0 ; i -- ) { if ( v2 [ i ] == 1 && v1 [ i ] == 0 && z == 0 ) { z = 1 ; continue ; } if ( z == 1 ) { v1 [ i ] = 1 ; } } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { v3 . push_back ( v2 [ i ] v1 [ i ] ) ; } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { if ( v3 [ i ] == 1 ) { ans += cnt ; } cnt *= 2 ; } return ans ; } int main ( ) { long long int L = 10 , R = 20 ; cout << max_bitwise_or ( L , R ) ; return 0 ; }
Find the minimum value of X for an expression | C ++ implementation of above approach ; Function to calculate value of X ; Check for both possibilities ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int valueofX ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return sum / n ; } else { int A = sum / n , B = sum / n + 1 ; int ValueA = 0 , ValueB = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } int main ( ) { int n = 7 ; int arr [ 7 ] = { 6 , 9 , 1 , 6 , 1 , 3 , 7 } ; cout << valueofX ( arr , n ) << ' ' ; return 0 ; }
Minimum length String with Sum of the alphabetical values of the characters equal to N | C ++ program to find the Minimum length String with Sum of the alphabetical values of the characters equal to N ; Function to find the minimum length ; Function to find the minimum length String ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minLength ( int n ) { int ans = n / 26 ; if ( n % 26 != 0 ) ans ++ ; return ans ; } string minString ( int n ) { int ans = n / 26 ; string res = " " ; while ( ans -- ) { res = res + " z " ; } if ( n % 26 != 0 ) { res = res + ( char ) ( ( n % 26 ) + 96 ) ; } return res ; } int main ( ) { int n = 50 ; cout << minLength ( n ) << endl << minString ( n ) ; return 0 ; }
Minimum halls required for class scheduling | C ++ implementation of the approach ; Function to return the minimum number of halls required ; Array to store the number of lectures ongoing at time t ; For every lecture increment start point s decrement ( end point + 1 ) ; Perform prefix sum and update the ans to maximum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100001 NEW_LINE int minHalls ( int lectures [ ] [ 2 ] , int n ) { int prefix_sum [ MAX ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i ] [ 0 ] ] ++ ; prefix_sum [ lectures [ i ] [ 1 ] + 1 ] -- ; } int ans = prefix_sum [ 0 ] ; for ( int i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = max ( ans , prefix_sum [ i ] ) ; } return ans ; } int main ( ) { int lectures [ ] [ 2 ] = { { 0 , 5 } , { 1 , 2 } , { 1 , 10 } } ; int n = sizeof ( lectures ) / sizeof ( lectures [ 0 ] ) ; cout << minHalls ( lectures , n ) ; return 0 ; }
Find the minimum capacity of the train required to hold the passengers | C ++ implementation of the approach ; Function to return the minimum capacity required ; To store the minimum capacity ; To store the current capacity of the train ; For every station ; Add the number of people entering the train and subtract the number of people exiting the train to get the current capacity of the train ; Update the minimum capacity ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCapacity ( int enter [ ] , int exit [ ] , int n ) { int minCap = 0 ; int currCap = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currCap = currCap + enter [ i ] - exit [ i ] ; minCap = max ( minCap , currCap ) ; } return minCap ; } int main ( ) { int enter [ ] = { 3 , 5 , 2 , 0 } ; int exit [ ] = { 0 , 2 , 4 , 4 } ; int n = sizeof ( enter ) / sizeof ( enter [ 0 ] ) ; cout << minCapacity ( enter , exit , n ) ; return 0 ; }
Count of numbers in the range [ L , R ] which satisfy the given conditions | C ++ implementation of the approach ; Maximum possible valid number ; To store all the required number from the range [ 1 , MAX ] ; Function that returns true if x satisfies the given conditions ; To store the digits of x ; If current digit appears more than once ; If current digit is greater than 5 ; Put the digit in the map ; Function to generate all the required numbers in the range [ 1 , MAX ] ; Insert first 5 valid numbers ; Inserting 0 externally because 0 cannot be the leading digit in any number ; If x satisfies the given conditions ; Cannot append anymore digit as adding a digit will repeat one of the already present digits ; Append all the valid digits one by one and push the new generated number to the queue ; Append the digit ; Push the newly generated number to the queue ; Function to copmpare two strings which represent a numerical value ; Function to return the count of valid numbers in the range [ l , r ] ; Generate all the valid numbers in the range [ 1 , MAX ] ; To store the count of numbers in the range [ l , r ] ; For every valid number in the range [ 1 , MAX ] ; If current number is within the required range ; If number is equal to either l or r ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  543210 NEW_LINE vector < string > ans ; bool isValidNum ( string x ) { map < int , int > mp ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( mp . find ( x [ i ] - '0' ) != mp . end ( ) ) { return false ; } else if ( x [ i ] - '0' > 5 ) { return false ; } else { mp [ x [ i ] - '0' ] = 1 ; } } return true ; } void generate ( ) { queue < string > q ; q . push ( "1" ) ; q . push ( "2" ) ; q . push ( "3" ) ; q . push ( "4" ) ; q . push ( "5" ) ; bool flag = true ; ans . push_back ( "0" ) ; while ( ! q . empty ( ) ) { string x = q . front ( ) ; q . pop ( ) ; if ( isValidNum ( x ) ) { ans . push_back ( x ) ; } if ( x . length ( ) == 6 ) continue ; for ( int i = 0 ; i <= 5 ; i ++ ) { string z = to_string ( i ) ; string temp = x + z ; q . push ( temp ) ; } } } bool comp ( string a , string b ) { if ( a . size ( ) == b . size ( ) ) return a < b ; else return a . size ( ) < b . size ( ) ; } int findcount ( string l , string r ) { generate ( ) ; int count = 0 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { string a = ans [ i ] ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } int main ( ) { string l = "1" , r = "1000" ; cout << findcount ( l , r ) ; return 0 ; }
Find permutation with maximum remainder Sum | C ++ implementation of the approach ; Function to find the permutation ; Put n at the first index 1 ; Put all the numbers from 2 to n sequentially ; Driver code ; Display the permutation
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > Findpermutation ( int n ) { vector < int > a ( n + 1 ) ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } int main ( ) { int n = 8 ; vector < int > v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) cout << v [ i ] << ' ▁ ' ; return 0 ; }
Lexicographically smallest string of length N and sum K | C ++ implementation of the approach ; Function to return the lexicographically smallest string of length n that satisfies the given condition ; Iteration from the last position in the array ; If k is a positive integer ; ' z ' needs to be inserted ; Add the required character ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string lexo_small ( int n , int k ) { string arr = " " ; for ( int i = 0 ; i < n ; i ++ ) arr += ' a ' ; for ( int i = n - 1 ; i >= 0 ; i -- ) { k -= i ; if ( k >= 0 ) { if ( k >= 26 ) { arr [ i ] = ' z ' ; k -= 26 ; } else { char c = ( char ) ( k + 97 - 1 ) ; arr [ i ] = c ; k -= arr [ i ] - ' a ' + 1 ; } } else break ; k += i ; } return arr ; } int main ( ) { int n = 5 , k = 42 ; string arr = lexo_small ( n , k ) ; cout << arr ; }
Check if string can be rearranged so that every Odd length Substring is Palindrome | C ++ implementation of the above approach ; Function to check is it possible to rearrange the string such that every odd length substring is palindrome ; Length of the string ; To count number of distinct character in string ; To count frequency of each character ; Inserting into set ; Incrementing the frequency ; All characters in the string are same ; There are more than 2 different character in string ; Currently there is 2 different character in string ; Get the frequencies of the characters that present in string ; Difference between their count is less than or equal to 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool IsPossible ( string s ) { int n = s . length ( ) ; set < char > count ; map < char , int > map ; for ( int i = 0 ; i < n ; i ++ ) { count . insert ( s [ i ] ) ; map [ s [ i ] ] += 1 ; } if ( count . size ( ) == 1 ) { return true ; } if ( count . size ( ) > 2 ) { return false ; } auto it = count . begin ( ) ; int x = 0 , y = 0 ; x = map [ * it ] ; it ++ ; y = map [ * it ] ; if ( abs ( x - y ) <= 1 ) { return true ; } return false ; } int main ( ) { string s = " aaaddad " ; if ( IsPossible ( s ) ) cout << " YES STRNEWLINE " ; else cout << " NO STRNEWLINE " ; return 0 ; }
Minimum element left from the array after performing given operations | C ++ implementation of the approach ; Function to return the minimum possible value of the last element left after performing the given operations ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMin ( int arr [ ] , int n ) { int minVal = * min_element ( arr , arr + n ) ; return minVal ; } int main ( ) { int arr [ ] = { 5 , 3 , 1 , 6 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getMin ( arr , n ) ; return 0 ; }
Largest substring with same Characters | CPP program to find largest sub string with same characters ; Function to find largest sub string with same characters ; Traverse the string ; If character is same as previous increment temp value ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Substring ( string s ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { ++ temp ; } else { ans = max ( ans , temp ) ; temp = 1 ; } } ans = max ( ans , temp ) ; return ans ; } int main ( ) { string s = " abcdddddeff " ; cout << Substring ( s ) ; return 0 ; }
Number of balanced parenthesis substrings | CPP program to find number of balanced parenthesis sub strings ; Function to find number of balanced parenthesis sub strings ; To store required answer ; Vector to stores the number of balanced brackets at each depth . ; d stores checks the depth of our sequence For example level of ( ) is 1 and that of ( ( ) ) is 2. ; If open bracket increase depth ; If closing bracket ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Balanced_Substring ( string str , int n ) { int ans = 0 ; vector < int > arr ( n / 2 + 1 , 0 ) ; int d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' ( ' ) d ++ ; else { if ( d == 1 ) { for ( int j = 2 ; j <= n / 2 + 1 && arr [ j ] != 0 ; j ++ ) arr [ j ] = 0 ; } ++ ans ; ans += arr [ d ] ; arr [ d ] ++ ; d -- ; } } return ans ; } int main ( ) { string str = " ( ) ( ) ( ) " ; int n = str . size ( ) ; cout << Balanced_Substring ( str , n ) ; return 0 ; }
Count ways to divide circle using N non | C ++ implementation of the above approach ; Function to calculate x ^ y % mod efficiently ; Initialize the answer ; If power is odd ; Update the answer ; Square the base and half the exponent ; Return the value ; Function to calculate ncr % mod efficiently ; Initialize the answer ; Calculate ncr in O ( r ) ; Multiply with the numerator factor ; Calculate the inverse of factor of denominator ; Multiply with inverse value ; Return answer value ; Function to return the number of non intersecting chords ; define mod value ; Value of C ( 2 n , n ) ; Modulo inverse of ( n + 1 ) ; Multiply with modulo inverse ; Return the answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int power ( long long x , int y , int mod ) { long long res = 1 ; while ( y ) { if ( y & 1 ) res = ( res * x ) % mod ; x = ( x * x ) % mod ; y = ( y >> 1 ) ; } return ( int ) ( res % mod ) ; } int ncr ( int n , int r , int mod ) { long long res = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { res = ( res * ( n - i + 1 ) ) % mod ; int inv = power ( i , mod - 2 , mod ) ; res = ( res * inv ) % mod ; } return ( int ) ( res % mod ) ; } int NoOfChords ( int A ) { int mod = 1e9 + 7 ; long long ans = ncr ( 2 * A , A , mod ) ; int inv = power ( A + 1 , mod - 2 , mod ) ; ans = ( ans * inv ) % mod ; return ( int ) ( ans % mod ) ; } int main ( ) { int N = 2 ; cout << NoOfChords ( N ) ; return 0 ; }
Longest Subarray having strictly positive XOR | C ++ implementation of the approach ; Function to return the length of the longest sub - array having positive XOR ; To store the XOR of all the elements ; To check if all the elements of the array are 0 s ; Take XOR of all the elements ; If any positive value is found the make the checkallzero false ; If complete array is the answer ; If all elements are equal to zero ; Initialize l and r ; First positive value of the array ; Last positive value of the array ; Maximum length among these two subarrays ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int StrictlyPositiveXor ( int A [ ] , int N ) { int allxor = 0 ; bool checkallzero = true ; for ( int i = 0 ; i < N ; i += 1 ) { allxor ^= A [ i ] ; if ( A [ i ] > 0 ) checkallzero = false ; } if ( allxor != 0 ) return N ; if ( checkallzero ) return -1 ; int l = N , r = -1 ; for ( int i = 0 ; i < N ; i += 1 ) { if ( A [ i ] > 0 ) { l = i + 1 ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -= 1 ) { if ( A [ i ] > 0 ) { r = i + 1 ; break ; } } return max ( N - l , r - 1 ) ; } int main ( ) { int A [ ] = { 1 , 0 , 0 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << StrictlyPositiveXor ( A , N ) ; return 0 ; }