codeparrot/xlcost-text-to-code huggingface dataset:
Minimum Sum of a pair at least K distance apart from an Array | C ++ program to implement the above approach ; Function to find the minimum sum of two elements that are atleast K distance apart ; Length of the array ; Iterate over the array ; Initialize the min value ; Iterate from i + k to N ; Find the minimum ; Update the minimum sum ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMinSum ( int A [ ] , int K , int n ) { int minimum_sum = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int mini = INT_MAX ; for ( int j = i + K ; j < n ; j ++ ) mini = min ( mini , A [ j ] ) ; if ( mini == INT_MAX ) continue ; minimum_sum = min ( minimum_sum , A [ i ] + mini ) ; } cout << ( minimum_sum ) ; } int main ( ) { int A [ ] = { 4 , 2 , 5 , 4 , 3 , 2 , 5 } ; int K = 3 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findMinSum ( A , K , n ) ; return 0 ; }
Absolute distinct count in a Linked List | C ++ Program to print the count of distinct absolute values in a linked list ; Node of a singly linked list ; Function to insert a node at the beginning of a singly Linked List ; Allocate node ; Insert the data ; Point to the current head ; Make the current Node the new head ; Function to return number of distinct absolute values present in the linked list ; Driver Code ; Create the head ; Insert Nodes
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; void push ( Node * * head_ref , int new_data ) { Node * new_node = new Node ( ) ; new_node -> data = new_data ; new_node -> next = ( * head_ref ) ; ( * head_ref ) = new_node ; } int distinctCount ( Node * head_1 ) { Node * ptr = head_1 ; unordered_set < int > s ; while ( ptr != NULL ) { s . insert ( abs ( ptr -> data ) ) ; ptr = ptr -> next ; } return s . size ( ) ; } int main ( ) { Node * head1 = NULL ; push ( & head1 , -1 ) ; push ( & head1 , -2 ) ; push ( & head1 , 0 ) ; push ( & head1 , 4 ) ; push ( & head1 , 5 ) ; push ( & head1 , 8 ) ; int k = distinctCount ( head1 ) ; cout << k ; return 0 ; }
Count of all possible pairs having sum of LCM and GCD equal to N | C ++ Program to implement the above approach ; Function to calculate and return LCM of two numbers ; Function to count pairs whose sum of GCD and LCM is equal to N ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lcm ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } int countPair ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( __gcd ( i , j ) + lcm ( i , j ) == N ) { count ++ ; } } } return count ; } int main ( ) { int N = 14 ; cout << countPair ( N ) ; return 0 ; }
Minimize cost to color all the vertices of an Undirected Graph using given operation | C ++ Program to find the minimum cost to color all vertices of an Undirected Graph ; Function to add edge in the given graph ; Function to perform DFS traversal and find the node with minimum cost ; Update the minimum cost ; Recur for all connected nodes ; Function to calculate and return the minimum cost of coloring all vertices of the Undirected Graph ; Marks if a vertex is visited or not ; Perform DFS traversal ; If vertex is not visited ; Update minimum cost ; Return the final cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  10 NEW_LINE vector < int > adj [ MAX ] ; void addEdge ( int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } void dfs ( int v , int cost [ ] , bool vis [ ] , int & min_cost_node ) { vis [ v ] = true ; min_cost_node = min ( min_cost_node , cost [ v - 1 ] ) ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( vis [ adj [ v ] [ i ] ] == false ) { dfs ( adj [ v ] [ i ] , cost , vis , min_cost_node ) ; } } } int minimumCost ( int V , int cost [ ] ) { bool vis [ V + 1 ] ; memset ( vis , false , sizeof ( vis ) ) ; int min_cost = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { if ( ! vis [ i ] ) { int min_cost_node = INT_MAX ; dfs ( i , cost , vis , min_cost_node ) ; min_cost += min_cost_node ; } } return min_cost ; } int main ( ) { int V = 6 , E = 5 ; int cost [ ] = { 12 , 25 , 8 , 11 , 10 , 7 } ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 2 ) ; addEdge ( 2 , 5 ) ; addEdge ( 4 , 6 ) ; int min_cost = minimumCost ( V , cost ) ; cout << min_cost << endl ; return 0 ; }
Maximize count of set bits in a root to leaf path in a binary tree | C ++ Program to implement the above approach ; Node structure ; Pointers to left and right child ; Initialize constructor ; Function to find the maximum count of setbits in a root to leaf ; Check if root is not null ; Update the maximum count of setbits ; Traverse left of binary tree ; Traverse right of the binary tree ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxm = 0 ; struct Node { int val ; Node * left , * right ; Node ( int x ) { val = x ; left = NULL ; right = NULL ; } } ; void maxm_setbits ( Node * root , int ans ) { if ( ! root ) return ; if ( root -> left == NULL && root -> right == NULL ) { ans += __builtin_popcount ( root -> val ) ; maxm = max ( ans , maxm ) ; return ; } maxm_setbits ( root -> left , ans + __builtin_popcount ( root -> val ) ) ; maxm_setbits ( root -> right , ans + __builtin_popcount ( root -> val ) ) ; } int main ( ) { Node * root = new Node ( 15 ) ; root -> left = new Node ( 3 ) ; root -> right = new Node ( 7 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 1 ) ; root -> right -> left = new Node ( 31 ) ; root -> right -> right = new Node ( 9 ) ; maxm_setbits ( root , 0 ) ; cout << maxm << endl ; return 0 ; }
Minimize count of divisions by D to obtain at least K equal array elements | C ++ Program to implement the above approach ; Function to return minimum number of moves required ; Stores the number of moves required to obtain respective values from the given array ; Traverse the array ; Insert 0 into V [ a [ i ] ] as it is the initial state ; Insert the moves required to obtain current a [ i ] ; Traverse v [ ] to obtain minimum count of moves ; Check if there are at least K equal elements for v [ i ] ; Add the sum of minimum K moves ; Update answer ; Return the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinimumMoves ( int n , int k , int d , vector < int > a ) { int MAX = 100000 ; vector < int > v [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; v [ a [ i ] ] . push_back ( 0 ) ; while ( a [ i ] > 0 ) { a [ i ] /= d ; cnt ++ ; v [ a [ i ] ] . push_back ( cnt ) ; } } int ans = INT_MAX ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( v [ i ] . size ( ) >= k ) { int move = 0 ; sort ( v [ i ] . begin ( ) , v [ i ] . end ( ) ) ; for ( int j = 0 ; j < k ; j ++ ) { move += v [ i ] [ j ] ; } ans = min ( ans , move ) ; } } return ans ; } int main ( ) { int N = 5 , K = 3 , D = 2 ; vector < int > A = { 1 , 2 , 3 , 4 , 5 } ; cout << getMinimumMoves ( N , K , D , A ) ; return 0 ; }
Longest subarray with odd product | C ++ Program to find the longest subarray with odd product ; Function to return length of longest subarray with odd product ; If even element is encountered ; Update maximum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Maxlen ( int arr [ ] , int n ) { int ans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count = 0 ; else count ++ ; ans = max ( ans , count ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 7 , 2 } ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << Maxlen ( arr , n ) << endl ; return 0 ; }
Min difference between maximum and minimum element in all Y size subarrays | C ++ program for the above approach ; Function to get the maximum of all the subarrays of size Y ; ith index of maxarr array will be the index upto which Arr [ i ] is maximum ; Stack is used to find the next larger element and keeps track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submax ; Function to get the minimum for all subarrays of size Y ; ith index of minarr array will be the index upto which Arr [ i ] is minimum ; Stack is used to find the next smaller element and keeping track of index of current iteration ; Loop for remaining indexes ; j < i used to keep track whether jth element is inside or outside the window ; Return submin ; Function to get minimum difference ; Create submin and submax arrays ; Store initial difference ; Calculate temporary difference ; Final minimum difference ; Driver Code ; Given array arr [ ] ; Given subarray size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > get_submaxarr ( int * arr , int n , int y ) { int j = 0 ; stack < int > stk ; vector < int > maxarr ( n ) ; stk . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . empty ( ) == false and arr [ i ] > arr [ stk . top ( ) ] ) { maxarr [ stk . top ( ) ] = i - 1 ; stk . pop ( ) ; } stk . push ( i ) ; } while ( ! stk . empty ( ) ) { maxarr [ stk . top ( ) ] = n - 1 ; stk . pop ( ) ; } vector < int > submax ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( maxarr [ j ] < i + y - 1 or j < i ) { j ++ ; } submax . push_back ( arr [ j ] ) ; } return submax ; } vector < int > get_subminarr ( int * arr , int n , int y ) { int j = 0 ; stack < int > stk ; vector < int > minarr ( n ) ; stk . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( stk . empty ( ) == false and arr [ i ] < arr [ stk . top ( ) ] ) { minarr [ stk . top ( ) ] = i ; stk . pop ( ) ; } stk . push ( i ) ; } while ( ! stk . empty ( ) ) { minarr [ stk . top ( ) ] = n ; stk . pop ( ) ; } vector < int > submin ; for ( int i = 0 ; i <= n - y ; i ++ ) { while ( minarr [ j ] <= i + y - 1 or j < i ) { j ++ ; } submin . push_back ( arr [ j ] ) ; } return submin ; } void getMinDifference ( int Arr [ ] , int N , int Y ) { vector < int > submin = get_subminarr ( Arr , N , Y ) ; vector < int > submax = get_submaxarr ( Arr , N , Y ) ; int minn = submax [ 0 ] - submin [ 0 ] ; int b = submax . size ( ) ; for ( int i = 1 ; i < b ; i ++ ) { int dif = submax [ i ] - submin [ i ] ; minn = min ( minn , dif ) ; } cout << minn << " STRNEWLINE " ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 2 } ; int N = sizeof arr / sizeof arr [ 0 ] ; int Y = 4 ; getMinDifference ( arr , N , Y ) ; return 0 ; }
Least root of given quadratic equation for value greater than equal to K | C ++ program for the above approach ; Function to calculate value of quadratic equation for some x ; Function to calculate the minimum value of x such that F ( x ) >= K using binary search ; Start and end value for binary search ; Binary Search ; Computing F ( mid ) and F ( mid - 1 ) ; If F ( mid ) >= K and F ( mid - 1 ) < K return mid ; If F ( mid ) < K go to mid + 1 to end ; If F ( mid ) > K go to start to mid - 1 ; If no such value exist ; Driver Code ; Given coefficients of Equations ; Find minimum value of x
#include <bits/stdc++.h> NEW_LINE using namespace std ; int func ( int A , int B , int C , int x ) { return ( A * x * x + B * x + C ) ; } int findMinx ( int A , int B , int C , int K ) { int start = 1 ; int end = ceil ( sqrt ( K ) ) ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; int x = func ( A , B , C , mid ) ; int Y = func ( A , B , C , mid - 1 ) ; if ( x >= K && Y < K ) { return mid ; } else if ( x < K ) { start = mid + 1 ; } else { end = mid - 1 ; } } return -1 ; } int main ( ) { int A = 3 , B = 4 , C = 5 , K = 6 ; cout << findMinx ( A , B , C , K ) ; return 0 ; }
Find the node at the center of an N | C ++ implementation of the above approach ; To create tree ; Function to store the path from given vertex to the target vertex in a vector path ; If the target node is found , push it into path vector ; To prevent visiting a node already visited ; Recursive call to the neighbours of current node inorder to get the path ; Function to obtain and return the farthest node from a given vertex ; If the current height is maximum so far , then save the current node ; Iterate over all the neighbours of current node ; This is to prevent visiting a already visited node ; Next call will be at 1 height higher than our current height ; Function to add edges ; Reset to - 1 ; Reset to - 1 ; Stores one end of the diameter ; Reset the maxHeight ; Stores the second end of the diameter ; Store the diameter into the vector path ; Diameter is equal to the path between the two farthest nodes leaf1 and leaf2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < int , vector < int > > tree ; bool getDiameterPath ( int vertex , int targetVertex , int parent , vector < int > & path ) { if ( vertex == targetVertex ) { path . push_back ( vertex ) ; return true ; } for ( auto i : tree [ vertex ] ) { if ( i == parent ) continue ; if ( getDiameterPath ( i , targetVertex , vertex , path ) ) { path . push_back ( vertex ) ; return true ; } } return false ; } void farthestNode ( int vertex , int parent , int height , int & maxHeight , int & maxHeightNode ) { if ( height > maxHeight ) { maxHeight = height ; maxHeightNode = vertex ; } for ( auto i : tree [ vertex ] ) { if ( i == parent ) continue ; farthestNode ( i , vertex , height + 1 , maxHeight , maxHeightNode ) ; } } void addedge ( int a , int b ) { tree [ a ] . push_back ( b ) ; tree [ b ] . push_back ( a ) ; } void FindCenter ( int n ) { int maxHeight = -1 ; int maxHeightNode = -1 ; farthestNode ( 0 , -1 , 0 , maxHeight , maxHeightNode ) ; int leaf1 = maxHeightNode ; maxHeight = -1 ; farthestNode ( maxHeightNode , -1 , 0 , maxHeight , maxHeightNode ) ; int leaf2 = maxHeightNode ; vector < int > path ; getDiameterPath ( leaf1 , leaf2 , -1 , path ) ; int pathSize = path . size ( ) ; if ( pathSize % 2 ) { cout << path [ pathSize / 2 ] << endl ; } else { cout << path [ pathSize / 2 ] << " , ▁ " << path [ ( pathSize - 1 ) / 2 ] << endl ; } } int main ( ) { int N = 4 ; addedge ( 1 , 0 ) ; addedge ( 1 , 2 ) ; addedge ( 1 , 3 ) ; FindCenter ( N ) ; return 0 ; }
K | C ++ program to find k - th term of N merged Arithmetic Progressions ; Function to count and return the number of values less than equal to N present in the set ; Check whether j - th bit is set bit or not ; Function to implement Binary Search to find K - th element ; Find middle index of the array ; Search in the left half ; Search in the right half ; If exactly K elements are present ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxm  1000000000 NEW_LINE int count ( vector < int > v , int n ) { int i , odd = 0 , even = 0 ; int j , d , count ; int t = ( int ) 1 << v . size ( ) ; int size = v . size ( ) ; for ( i = 1 ; i < t ; i ++ ) { d = 1 , count = 0 ; for ( j = 0 ; j < size ; j ++ ) { if ( i & ( 1 << j ) ) { d *= v [ j ] ; count ++ ; } } if ( count & 1 ) odd += n / d ; else even += n / d ; } return ( odd - even ) ; } int BinarySearch ( int l , int r , vector < int > v , int key ) { int mid ; while ( r - l > 1 ) { mid = ( l + r ) / 2 ; if ( key <= count ( v , mid ) ) { r = mid ; } else { l = mid ; } } if ( key == count ( v , l ) ) return l ; else return r ; } int main ( ) { int N = 2 , K = 10 ; vector < int > v = { 2 , 3 } ; cout << BinarySearch ( 1 , maxm , v , K ) << endl ; return 0 ; }
Find integral points with minimum distance from given set of integers using BFS | C ++ implementation of above approach ; Function to find points at minimum distance ; Hash to store points that are encountered ; Queue to store initial set of points ; Vector to store integral points ; Using bfs to visit nearest points from already visited points ; Get first element from queue ; Check if ( x - 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x - 1 ) into queue ; Push ( x - 1 ) as new element ; Decrement counter by 1 ; Check if ( x + 1 ) is not encountered so far ; Update hash with this new element ; Insert ( x + 1 ) into queue ; Push ( x + 1 ) as new element ; Decrement counter by 1 ; Print result array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minDistancePoints ( int A [ ] , int K , int n ) { map < int , int > m ; queue < int > q ; for ( int i = 0 ; i < n ; ++ i ) { m [ A [ i ] ] = 1 ; q . push ( A [ i ] ) ; } vector < int > ans ; while ( K > 0 ) { int x = q . front ( ) ; q . pop ( ) ; if ( ! m [ x - 1 ] && K > 0 ) { m [ x - 1 ] = 1 ; q . push ( x - 1 ) ; ans . push_back ( x - 1 ) ; K -- ; } if ( ! m [ x + 1 ] && K > 0 ) { m [ x + 1 ] = 1 ; q . push ( x + 1 ) ; ans . push_back ( x + 1 ) ; K -- ; } } for ( auto i : ans ) cout << i << " ▁ " ; } int main ( ) { int A [ ] = { -1 , 4 , 6 } ; int K = 3 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minDistancePoints ( A , K , n ) ; return 0 ; }
Leftmost Column with atleast one 1 in a row | C ++ implementation to find the Leftmost Column with atleast a 1 in a sorted binary matrix ; Function to search for the leftmost column of the matrix with atleast a 1 in sorted binary matrix ; Loop to iterate over all the rows of the matrix ; Binary Search to find the leftmost occurence of the 1 ; Condition if the column contains the 1 at this position of matrix ; If there is a better solution then update the answer ; Condition if the solution doesn 't exist in the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define N  3 NEW_LINE using namespace std ; int search ( int mat [ ] [ 3 ] , int n , int m ) { int i , a = INT_MAX ; for ( i = 0 ; i < n ; i ++ ) { int low = 0 ; int high = m - 1 ; int mid ; int ans = INT_MAX ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( mat [ i ] [ mid ] == 1 ) { if ( mid == 0 ) { ans = 0 ; break ; } else if ( mat [ i ] [ mid - 1 ] == 0 ) { ans = mid ; break ; } } if ( mat [ i ] [ mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } if ( ans < a ) a = ans ; } if ( a == INT_MAX ) return -1 ; return a + 1 ; } int main ( ) { int mat [ 3 ] [ 3 ] = { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 } ; cout << search ( mat , 3 , 3 ) ; return 0 ; }
Largest index for each distinct character in given string with frequency K | C ++ implementation of the approach ; Function to find largest index for each distinct character occuring exactly K times . ; finding all characters present in S ; Finding all distinct characters in S ; vector to store result for each character ; loop through each lower case English character ; if current character is absent in s ; getting current character ; finding count of character ch in S ; to store max Index encountred so far ; printing required result ; If no such character exists , print - 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSubstring ( string & S , int K , int N ) { int freq [ 26 ] ; memset ( freq , 0 , sizeof freq ) ; for ( int i = 0 ; i < N ; ++ i ) { freq [ S [ i ] - ' a ' ] = 1 ; } vector < pair < char , int > > answer ; for ( int i = 0 ; i < 26 ; ++ i ) { if ( freq [ i ] == 0 ) continue ; char ch = i + 97 ; int count = 0 ; int index = -1 ; for ( int j = 0 ; j < N ; ++ j ) { if ( S [ j ] == ch ) count ++ ; if ( count == K ) index = j ; } answer . push_back ( { ch , index } ) ; } int flag = 0 ; for ( int i = 0 ; i < ( int ) answer . size ( ) ; ++ i ) { if ( answer [ i ] . second > -1 ) { flag = 1 ; cout << answer [ i ] . first << " ▁ " << answer [ i ] . second << endl ; } } if ( flag == 0 ) cout << " - 1" << endl ; } int main ( ) { string S = " cbaabaacbcd " ; int K = 2 ; int N = S . length ( ) ; maxSubstring ( S , K , N ) ; return 0 ; }
Smallest number greater than n that can be represented as a sum of distinct power of k | C ++ implementation of the above approach ; Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; Vector P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; typedef long long ll ; #define pb  push_back NEW_LINE void greaterK ( ll n , ll k ) { vector < ll > p ; ll x = n ; while ( x ) { p . pb ( x % k ) ; x /= k ; } int idx = 0 ; for ( ll i = 0 ; i < ( ll ) p . size ( ) - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( int j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } ll j = ( ll ) p . size ( ) - 1 ; if ( p [ j ] >= 2 ) { for ( auto & i : p ) i = 0 ; p . pb ( 1 ) ; } ll ans = 0 ; for ( ll i = p . size ( ) - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } cout << ans << endl ; } int main ( ) { ll n = 29 , k = 7 ; greaterK ( n , k ) ; return 0 ; }
Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | C ++ implementation of the approach ; Function that returns true if the string can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canBeBalanced ( string s , int n ) { int count = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == ' ( ' ) count ++ ; else count -- ; count_1 = min ( count_1 , count ) ; } if ( count_1 >= -1 && count == 0 ) return true ; return false ; } int main ( ) { string s = " ( ) ) ( ) ( " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Absolute difference between the XOR of Non | C ++ implementation of the approach ; Function to find the absolute difference between the XOR of non - primes and the XOR of primes in the given array ; Find maximum value in the array ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Store the XOR of primes in X1 and the XOR of non primes in X2 ; The number is prime ; The number is non - prime ; Return the absolute difference ; Driver code ; Find the absolute difference
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculateDifference ( int arr [ ] , int n ) { int max_val = * max_element ( arr , arr + n ) ; vector < bool > prime ( max_val + 1 , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int X1 = 1 , X2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { X1 ^= arr [ i ] ; } else if ( arr [ i ] != 1 ) { X2 ^= arr [ i ] ; } } return abs ( X1 - X2 ) ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << calculateDifference ( arr , n ) ; return 0 ; }