codeparrot/xlcost-text-to-code huggingface dataset:
Find two vertices of an isosceles triangle in which there is rectangle with opposite corners ( 0 , 0 ) and ( X , Y ) | C ++ program to find two vertices of an isosceles triangle in which there is rectangle with opposite side ( 0 , 0 ) and ( x , y ) ; Function to find two vertices of an isosceles triangle in which there is rectangle with opposite side ( 0 , 0 ) and ( x , y ) ; Required value ; ; print x1 and y1 ; print x2 and y3 ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Vertices ( int x , int y ) { int val = abs ( x ) + abs ( y ) ; cout << val * ( x < 0 ? -1 : 1 ) << " ▁ 0 ▁ " ; cout << "0 ▁ " << val * ( y < 0 ? -1 : 1 ) ; } int main ( ) { int x = 3 , y = 3 ; Vertices ( x , y ) ; return 0 ; }
Find a subarray whose sum is divisible by size of the array | C ++ implementation of above approach ; Function to find a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Generating all sub - arrays ; If the sum of the sub - array [ i : j ] is a multiple of N ; If the function reaches here it means there are no subarrays with sum as a multiple of N ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CheckSubarray ( int arr [ ] , int N ) { int presum [ N + 1 ] = { 0 } ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 1 ; i <= N ; i += 1 ) { for ( int j = i ; j <= N ; j += 1 ) { if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) { cout << i - 1 << " ▁ " << j - 1 ; return ; } } } cout << -1 ; } int main ( ) { int arr [ ] = { 7 , 5 , 3 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; CheckSubarray ( arr , N ) ; return 0 ; }
Find a subarray whose sum is divisible by size of the array | C ++ implementation of above approach ; Function to check is there exists a subarray whose sum is a multiple of N ; Prefix sum array to store cumulative sum ; Single state dynamic programming relation for prefix sum array ; Modulo class vector ; Storing the index value in the modulo class vector ; If there exists a sub - array with starting index equal to zero ; In this class , there are more than two presums % N Hence difference of any two subarrays would be a multiple of N ; 0 based indexing ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void CheckSubarray ( int arr [ ] , int N ) { int presum [ N + 1 ] = { 0 } ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } vector < int > moduloclass [ N ] ; for ( int i = 1 ; i <= N ; i += 1 ) { moduloclass [ presum [ i ] % N ] . push_back ( i - 1 ) ; } if ( moduloclass [ 0 ] . size ( ) > 0 ) { cout << 0 << " ▁ " << moduloclass [ 0 ] [ 0 ] ; return ; } for ( int i = 1 ; i < N ; i += 1 ) { if ( moduloclass [ i ] . size ( ) >= 2 ) { cout << moduloclass [ i ] [ 0 ] + 1 << " ▁ " << moduloclass [ i ] [ 1 ] ; return ; } } } int main ( ) { int arr [ ] = { 7 , 3 , 5 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; CheckSubarray ( arr , N ) ; return 0 ; }
Smallest number greater or equals to N such that it has no odd positioned bit set | C ++ implementation of the above approach ; Function to count the total bits ; Iterate and find the number of set bits ; Right shift the number by 1 ; Function to find the nearest number ; Count the total number of bits ; To get the position ; If the last set bit is at odd position then answer will always be a number with the left bit set ; Set all the even bits which are possible ; If the number still is less than N ; Return the number by setting the next even set bit ; If we have reached this position it means tempsum > n hence turn off even bits to get the first possible number ; Turn off the bit ; If it gets lower than N then set it and return that number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countBits ( int n ) { int count = 0 ; while ( n ) { count ++ ; n >>= 1 ; } return count ; } int findNearestNumber ( int n ) { int cnt = countBits ( n ) ; cnt -= 1 ; if ( cnt % 2 ) { return 1 << ( cnt + 1 ) ; } else { int tempnum = 0 ; for ( int i = 0 ; i <= cnt ; i += 2 ) tempnum += 1 << i ; if ( tempnum < n ) { return ( 1 << ( cnt + 2 ) ) ; } else if ( tempnum == n ) return n ; for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum -= ( 1 << i ) ; if ( tempnum < n ) return tempnum += ( 1 << i ) ; } } } int main ( ) { int n = 19 ; cout << findNearestNumber ( n ) ; }
Count of numbers whose 0 th and Nth bits are set | C ++ implementation of the approach ; Function to return the count of n - bit numbers whose 0 th and nth bits are set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countNum ( int n ) { if ( n == 1 ) return 1 ; int count = pow ( 2 , n - 2 ) ; return count ; } int main ( ) { int n = 3 ; cout << countNum ( n ) ; return 0 ; }
Find a number containing N | C ++ implementation of the approach ; Function to return the string generated by appending "10" n - 1 times ; Initialising string as empty ; Function to return the decimal equivalent of the given binary string ; Initializing base value to 1 i . e 2 ^ 0 ; Function that calls the constructString and binarytodecimal and returns the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE string constructString ( ll n ) { string s = " " ; for ( ll i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } ll binaryToDecimal ( string n ) { string num = n ; ll dec_value = 0 ; ll base = 1 ; ll len = num . length ( ) ; for ( ll i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } ll findNumber ( ll n ) { string s = constructString ( n - 1 ) ; ll num = binaryToDecimal ( s ) ; return num ; } int main ( ) { ll n = 4 ; cout << findNumber ( n ) ; return 0 ; }
Bitonic point in the given linked list | C ++ implementation of the approach ; Node for linked list ; Function to insert a node at the head of the linked list ; Function to return the bitonic of the given linked list ; If list is empty ; If list contains only a single node ; Invalid bitonic sequence ; If current node is the bitonic point ; Get to the next node in the list ; Nodes must be in descending starting from here ; Out of order node ; Get to the next node in the list ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int data ; Node * next ; } ; Node * push ( Node * * head_ref , int data ) { Node * new_node = new Node ; new_node -> data = data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int bitonic_point ( Node * node ) { if ( node == NULL ) return -1 ; if ( node -> next == NULL ) return -1 ; if ( node -> data > node -> next -> data ) return -1 ; while ( node -> next != NULL ) { if ( node -> data > node -> next -> data ) break ; node = node -> next ; } int bitonicPoint = node -> data ; while ( node -> next != NULL ) { if ( node -> data < node -> next -> data ) return -1 ; node = node -> next ; } return bitonicPoint ; } int main ( ) { Node * head = NULL ; push ( & head , 100 ) ; push ( & head , 201 ) ; push ( & head , 399 ) ; push ( & head , 490 ) ; push ( & head , 377 ) ; push ( & head , 291 ) ; push ( & head , 100 ) ; cout << bitonic_point ( head ) ; return 0 ; }
Find the minimum number of elements that should be removed to make an array good | C ++ program to remove minimum elements to make the given array good ; Function to remove minimum elements to make the given array good ; To store count of each subsequence ; Increase the count of subsequence [ 0 ] ; If Previous element subsequence count is greater than zero then increment subsequence count of current element and decrement subsequence count of the previous element . ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MinRemove ( int a [ ] , int n , int k ) { vector < int > cnt ( k , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) cnt [ 0 ] ++ ; else if ( cnt [ a [ i ] - 1 ] > 0 ) { cnt [ a [ i ] - 1 ] -- ; cnt [ a [ i ] ] ++ ; } } return n - ( k * cnt [ k - 1 ] ) ; } int main ( ) { int a [ ] = { 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 } , k = 5 ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << MinRemove ( a , n , k ) ; return 0 ; }
Print first N Mosaic numbers | C ++ implementation of the approach ; Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Function to print first N Mosaic numbers ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } void nMosaicNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) cout << mosaic ( i ) << " ▁ " ; } int main ( ) { int n = 10 ; nMosaicNumbers ( n ) ; return 0 ; }
Count of nodes which are at a distance X from root and leaves | C ++ implementation of the approach ; Function to find the count of the required nodes ; Height of the complete binary tree with n nodes ; If X > height then no node can be present at that level ; Corner case ; Maximum total nodes that are possible in complete binary tree with height h ; Nodes at the last level ; To store the count of nodes x dist away from root ; To store the count of nodes x dist away from leaf ; If X = h then print nodes at last level else nodes at Xth level ; 2 ^ X ; Number of left leaf nodes at ( h - 1 ) th level observe that if nodes are not present at last level then there are a / 2 leaf nodes at ( h - 1 ) th level ; If X = h then print leaf nodes at the last h level + leaf nodes at ( h - 1 ) th level ; First calculate nodes for leaves present at height h ; Then calculate nodes for leaves present at height h - 1 ; Add both the resuls ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countNodes ( int N , int X ) { int height = floor ( log2 ( N ) ) ; if ( X > height ) { cout << "0 STRNEWLINE 0" ; return ; } if ( N == 1 ) { cout << "1 STRNEWLINE 1" ; return ; } int max_total_nodes = ( 1 << ( height + 1 ) ) - 1 ; int nodes_last_level = ( 1 << height ) - ( max_total_nodes - N ) ; int from_root ; int from_leaf ; if ( X == height ) from_root = nodes_last_level ; else from_root = 1 << X ; int left_leaf_nodes = ( ( 1 << height ) - nodes_last_level ) / 2 ; if ( X == 0 ) { from_leaf = nodes_last_level + left_leaf_nodes ; } else { int i = X ; while ( nodes_last_level > 1 && i > 0 ) { nodes_last_level = ceil ( ( float ) nodes_last_level / ( float ) 2 ) ; i -- ; } from_leaf = nodes_last_level ; i = X ; while ( left_leaf_nodes > 1 && i > 0 ) { left_leaf_nodes = ceil ( ( float ) left_leaf_nodes / ( float ) 2 ) ; i -- ; } from_leaf += left_leaf_nodes ; } cout << from_root << endl << from_leaf ; } int main ( ) { int N = 38 , X = 3 ; countNodes ( N , X ) ; return 0 ; }
Number of ways of writing N as a sum of 4 squares | C ++ implementation of above approach ; Number of ways of writing n as a sum of 4 squares ; sum of odd and even factor ; iterate from 1 to the number ; if i is the factor of n ; if factor is even ; if factor is odd ; n / i is also a factor ; if factor is even ; if factor is odd ; if n is odd ; if n is even ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sum_of_4_squares ( int n ) { int i , odd = 0 , even = 0 ; for ( i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } int main ( ) { int n = 4 ; cout << sum_of_4_squares ( n ) ; return 0 ; }
Find the Nth Mosaic number | C ++ implementation of the approach ; Function to return the nth mosaic number ; Iterate from 2 to the number ; If i is the factor of n ; Find the count where i ^ count is a factor of n ; Divide the number by i ; Increase the count ; Multiply the answer with count and i ; Return the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } int main ( ) { int n = 36 ; cout << mosaic ( n ) ; return 0 ; }
Seating arrangement of N boys sitting around a round table such that two particular boys sit together | C ++ implementation of the approach ; Function to return the total count of ways ; Find ( n - 1 ) factorial ; Return ( n - 1 ) ! * 2 ! ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Total_Ways ( int n ) { int fac = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } int main ( ) { int n = 5 ; cout << Total_Ways ( n ) ; return 0 ; }
Find the maximum number of elements divisible by 3 | C ++ program to find the maximum number of elements divisible by 3 ; Function to find the maximum number of elements divisible by 3 ; To store frequency of each number ; Store modulo value ; Store frequency ; Add numbers with zero modulo to answer ; Find minimum of elements with modulo frequency one and zero ; Add k to the answer ; Remove them from frequency ; Add numbers possible with remaining frequency ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int MaxNumbers ( int a [ ] , int n ) { int fre [ 3 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } int ans = fre [ 0 ] ; int k = min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += fre [ 1 ] / 3 + fre [ 2 ] / 3 ; return ans ; } int main ( ) { int a [ ] = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << MaxNumbers ( a , n ) ; return 0 ; }
Count pairs with set bits sum equal to K | C ++ implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Sum of set bits in both the integers ; If current pair satisfies the given condition ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unsigned int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int pairs ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = countSetBits ( arr [ i ] ) + countSetBits ( arr [ j ] ) ; if ( sum == k ) count ++ ; } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; cout << pairs ( arr , n , k ) ; return 0 ; }
Count pairs with set bits sum equal to K | C ++ implementation of the approach ; Function to return the count of set bits in n ; Function to return the count of required pairs ; To store the count ; Frequency array ; If current pair satisfies the given condition ; ( arr [ i ] , arr [ i ] ) cannot be a valid pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  32 NEW_LINE unsigned int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; } int pairs ( int arr [ ] , int n , int k ) { int count = 0 ; int f [ MAX + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) f [ countSetBits ( arr [ i ] ) ] ++ ; for ( int i = 0 ; i <= MAX ; i ++ ) { for ( int j = i ; j <= MAX ; j ++ ) { if ( i + j == k ) { if ( i == j ) count += ( ( f [ i ] * ( f [ i ] - 1 ) ) / 2 ) ; else count += ( f [ i ] * f [ j ] ) ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 4 ; cout << pairs ( arr , n , k ) ; return 0 ; }
Print Lower Hessenberg matrix of order N | C ++ implementation of the approach ; Function to print the Lower Hessenberg matrix of order n ; If element is above super - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void LowerHessenbergMatrix ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j > i + 1 ) cout << '0' << " ▁ " ; else cout << rand ( ) % 10 << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 4 ; LowerHessenbergMatrix ( n ) ; return 0 ; }
Print Upper Hessenberg matrix of order N | C ++ implementation of the approach ; Function to print the Upper Hessenberg matrix of order n ; If element is below sub - diagonal then print 0 ; Print a random digit for every non - zero element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void UpperHessenbergMatrix ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i > j + 1 ) cout << '0' << " ▁ " ; else cout << rand ( ) % 10 << " ▁ " ; } cout << " STRNEWLINE " ; } } int main ( ) { int n = 4 ; UpperHessenbergMatrix ( n ) ; return 0 ; }
Find the total number of composite factor for a given number | C ++ implementation of the approach ; Function to return the count of prime factors of n ; Initialise array with 0 ; Stored i value into an array ; Every non - zero value at a [ i ] denotes that i is a factor of n ; Find if i is prime ; If i is a factor of n and i is not prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int composite_factors ( int n ) { int count = 0 ; int i , j ; int a [ n + 1 ] = { 0 } ; for ( i = 1 ; i <= n ; ++ i ) { if ( n % i == 0 ) { a [ i ] = i ; } } for ( i = 2 ; i <= n ; i ++ ) { j = 2 ; int p = 1 ; while ( j < a [ i ] ) { if ( a [ i ] % j == 0 ) { p = 0 ; break ; } j ++ ; } if ( p == 0 && a [ i ] != 0 ) { count ++ ; } } return count ; } int main ( ) { int n = 100 ; cout << composite_factors ( n ) ; return 0 ; }
Sum of all mersenne numbers present in an array | C ++ implementation of the approach ; Function that returns true if n is a Mersenne number ; Function to return the sum of all the Mersenne numbers from the given array ; To store the required sum ; If current element is a Mersenne number ; Driver code
#include <iostream> NEW_LINE using namespace std ; int isMersenne ( int n ) { while ( n != 0 ) { int r = n % 2 ; if ( r == 0 ) return false ; n /= 2 ; } return true ; } int sumOfMersenne ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && isMersenne ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } int main ( ) { int arr [ ] = { 17 , 6 , 7 , 63 , 3 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << ( sumOfMersenne ( arr , n ) ) ; return 0 ; }
Sum of all perfect numbers present in an array | C ++ implementation of the above approach ; Function to return the sum of all the proper factors of n ; f is the factor of n ; Function to return the required sum ; To store the sum ; If current element is non - zero and equal to the sum of proper factors of itself ; Driver code
#include <iostream> NEW_LINE using namespace std ; int sumOfFactors ( int n ) { int sum = 0 ; for ( int f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } int getSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } int main ( ) { int arr [ 10 ] = { 17 , 6 , 10 , 6 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ( getSum ( arr , n ) ) ; return 0 ; }
Check if two numbers have same number of digits | C ++ implementation of the approach ; Function that return true if A and B have same number of digits ; Both must be 0 now if they had same lengths ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sameLength ( int A , int B ) { while ( A > 0 && B > 0 ) { A = A / 10 ; B = B / 10 ; } if ( A == 0 && B == 0 ) return true ; return false ; } int main ( ) { int A = 21 , B = 1 ; if ( sameLength ( A , B ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Find a way to fill matrix with 1 ' s ▁ and ▁ 0' s in blank positions | C ++ implementation of the approach ; Function to generate and print the required matrix ; Replace the ' . ' ; Toggle number ; For each row , change the starting number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  2 NEW_LINE #define M  2 NEW_LINE void Matrix ( char a [ N ] [ M ] ) { char ch = '1' ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == ' . ' ) a [ i ] [ j ] = ch ; ch = ( ch == '1' ) ? '0' : '1' ; cout << a [ i ] [ j ] << " ▁ " ; } cout << endl ; if ( i % 2 == 0 ) ch = '0' ; else ch = '1' ; } } int main ( ) { char a [ N ] [ M ] = { { ' . ' , ' _ ' } , { ' _ ' , ' . ' } } ; Matrix ( a ) ; return 0 ; }
Queries to check whether all the elements can be made positive by flipping signs exactly K times | C ++ implementation of the approach ; To store the count of negative integers ; Check if zero exists ; Function to find the count of negative integers and check if zero exists in the array ; Function that returns true if array elements can be made positive by changing signs exactly k times ; Driver code ; Pre - processing ; Perform queries
#include <bits/stdc++.h> NEW_LINE using namespace std ; int cnt_neg ; bool exists_zero ; void preProcess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) cnt_neg ++ ; if ( arr [ i ] == 0 ) exists_zero = true ; } } bool isPossible ( int k ) { if ( ! exists_zero ) { if ( k >= cnt_neg and ( k - cnt_neg ) % 2 == 0 ) return true ; else return false ; } else { if ( k >= cnt_neg ) return true ; else return false ; } } int main ( ) { int arr [ ] = { -1 , 2 , -3 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( int ) ; preProcess ( arr , n ) ; int queries [ ] = { 1 , 2 , 3 , 4 } ; int q = sizeof ( queries ) / sizeof ( int ) ; for ( int i = 0 ; i < q ; i ++ ) { if ( isPossible ( queries [ i ] ) ) cout << " Yes " << endl ; else cout << " No " << endl ; } return 0 ; }
Sum of XOR of all sub | C ++ implementation of above approach ; Sum of XOR of all K length sub - array of an array ; If the length of the array is less than k ; Array that will store xor values of subarray from 1 to i ; Traverse through the array ; If i is greater than zero , store xor of all the elements from 0 to i ; If it is the first element ; If i is greater than k ; Xor of values from 0 to i ; Now to find subarray of length k that ends at i , xor sum with x [ i - k ] ; Add the xor of elements from i - k + 1 to i ; Return the resultant sum ; ; Driver code
#include <iostream> NEW_LINE using namespace std ; int FindXorSum ( int arr [ ] , int k , int n ) { if ( n < k ) return 0 ; int x [ n ] = { 0 } ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) x [ i ] = x [ i - 1 ] ^ arr [ i ] ; else x [ i ] = arr [ i ] ; if ( i >= k - 1 ) { int sum = 0 ; sum = x [ i ] ; if ( i - k > -1 ) sum ^= x [ i - k ] ; result += sum ; } } return result ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 2 ; cout << FindXorSum ( arr , k , n ) << endl ; return 0 ; }