codeparrot/xlcost-text-to-code huggingface dataset:
Minimum number of jumps to reach end | C ++ program to find Minimum number of jumps to reach end ; Returns Minimum number of jumps to reach end ; jumps [ 0 ] will hold the result ; Minimum number of jumps needed to reach last element from last elements itself is always 0 ; Start from the second element , move from right to left and construct the jumps [ ] array where jumps [ i ] represents minimum number of jumps needed to reach arr [ m - 1 ] from arr [ i ] ; If arr [ i ] is 0 then arr [ n - 1 ] can 't be reached from here ; If we can direcly reach to the end point from here then jumps [ i ] is 1 ; Otherwise , to find out the minimum number of jumps needed to reach arr [ n - 1 ] , check all the points reachable from here and jumps [ ] value for those points ; initialize min value ; following loop checks with all reachable points and takes the minimum ; Handle overflow ; or INT_MAX ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minJumps ( int arr [ ] , int n ) { int * jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = INT_MAX ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = INT_MAX ; for ( int j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) min = jumps [ j ] ; } if ( min != INT_MAX ) jumps [ i ] = min + 1 ; else jumps [ i ] = min ; } } return jumps [ 0 ] ; } int main ( ) { int arr [ ] = { 1 , 3 , 6 , 1 , 0 , 9 } ; int size = sizeof ( arr ) / sizeof ( int ) ; cout << " Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach " << " ▁ end ▁ is ▁ " << minJumps ( arr , size ) ; return 0 ; }
Count N |  ; Stores the dp states ; Check if a number is a prime or not ; Function to generate all prime numbers that are less than or equal to n ; Base cases . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of as non - prime ; Function to find the count of N - digit numbers such that the sum of digits is a prime number ; If end of array is reached ; If the sum is equal to a prime number ; Otherwise ; If the dp - states are already computed ; If index = 1 , any digit from [ 1 - 9 ] can be placed . If N = 1 , 0 also can be placed . ; Otherwise , any digit from [ 0 - 9 ] can be placed . ; Return the answer . ; Driver Code ; Initializing dp array with - 1 ; Initializing prime array to true ; Find all primes less than or equal to 1000 , which is sufficient for N upto 100 ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 1000 ] ; bool prime [ 1005 ] ; void SieveOfEratosthenes ( int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } int countOfNumbers ( int index , int sum , int N ) { if ( index == N + 1 ) { if ( prime [ sum ] == true ) { return 1 ; } return 0 ; } int & val = dp [ index ] [ sum ] ; if ( val != -1 ) { return val ; } val = 0 ; if ( index == 1 ) { for ( int digit = ( N == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } else { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { val += countOfNumbers ( index + 1 , sum + digit , N ) ; } } return val ; } int main ( ) { memset ( dp , -1 , sizeof dp ) ; memset ( prime , true , sizeof ( prime ) ) ; SieveOfEratosthenes ( 1000 ) ; int N = 6 ; cout << countOfNumbers ( 1 , 0 , N ) ; return 0 ; }
Maximum number of groups that can receive fresh donuts distributed in batches of size K | C ++ program for the above approach ; Stores the result of the same recursive calls ; Recursive function to find the maximum number of groups that will receive fresh donuts ; Store the result for the current state ; Store the key and check if it is present in the hashmap ; If already calculated ; If left is 0 ; Traverse the array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Otherwise , traverse the given array [ ] arr ; Decrement arr [ i ] ; Update the maximum number of groups ; Increment arr [ i ] by 1 ; Memoize the result and return it ; Function to find the maximum number of groups that will receive fresh donuts ; Stores count of remainder by K ; Traverse the array [ ] arr ; Store the maximum number of groups ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; map < string , int > memo ; int dfs ( int V [ ] , int left , int K ) { int q = 0 ; string key = " " ; for ( int i = 0 ; i < K ; i ++ ) { key = key + to_string ( V [ i ] ) ; } key += to_string ( left ) ; if ( memo . find ( key ) != memo . end ( ) ) return memo [ key ] ; else if ( left == 0 ) { for ( int i = 1 ; i < K ; ++ i ) if ( V [ i ] > 0 ) { V [ i ] -- ; q = max ( q , 1 + dfs ( V , K - i , K ) ) ; V [ i ] ++ ; } } else { for ( int i = 1 ; i < K ; ++ i ) { if ( V [ i ] > 0 ) { V [ i ] -- ; int nleft = i <= left ? left - i : K + left - i ; q = max ( q , dfs ( V , nleft , K ) ) ; V [ i ] ++ ; } } } if ( memo . find ( key ) != memo . end ( ) ) memo [ key ] = q ; else memo [ key ] = q ; return q ; } int maxGroups ( int K , int arr [ ] ) { int V [ K ] ; memset ( V , 0 , sizeof ( V ) ) ; for ( int i = 0 ; i < 6 ; i ++ ) V [ arr [ i ] % K ] ++ ; int ans = V [ 0 ] + dfs ( V , 0 , K ) ; return ans ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 3 ; cout << maxGroups ( K , arr ) ; return 0 ; }
Maximum Sum Alternating Subarray | C ++ implementation for the above approach ; Function to find the maximum alternating sum of a subarray for the given array ; Traverse the array ; Store sum of subarrays starting at even indices ; Update sum ; Traverse the array ; Store sum of subarrays starting at odd indices ; Update sum ; Driver code ; Given Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int alternatingSum ( int arr [ ] , int n ) { int sum = 0 ; int sumSoFar = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = max ( sum , sumSoFar ) ; } sumSoFar = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumSoFar -= arr [ i ] ; } else { sumSoFar = max ( sumSoFar + arr [ i ] , arr [ i ] ) ; } sum = max ( sum , sumSoFar ) ; } return sum ; } int main ( ) { int arr [ ] = { -4 , -10 , 3 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int ans = alternatingSum ( arr , n ) ; cout << ans << endl ; return 0 ; }
Smallest subarray with sum greater than a given value | O ( n ) solution for finding smallest subarray with sum greater than x ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; int smallestSubWithSum ( int arr [ ] , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } int main ( ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; ( res1 == n1 + 1 ) ? cout << " Not ▁ possible STRNEWLINE " : cout << res1 << endl ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; ( res2 == n2 + 1 ) ? cout << " Not ▁ possible STRNEWLINE " : cout << res2 << endl ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; ( res3 == n3 + 1 ) ? cout << " Not ▁ possible STRNEWLINE " : cout << res3 << endl ; return 0 ; }
Sum of nodes on the longest path from root to leaf node | C ++ implementation to find the sum of nodes on the longest path from root to leaf node ; Node of a binary tree ; function to get a new node ; allocate memory for the node ; put in the data ; function to find the sum of nodes on the longest path from root to leaf node ; if true , then we have traversed a root to leaf path ; update maximum length and maximum sum according to the given conditions ; recur for left subtree ; recur for right subtree ; utility function to find the sum of nodes on the longest path from root to leaf node ; if tree is NULL , then sum is 0 ; finding the maximum sum ' maxSum ' for the maximum length root to leaf path ; required maximum sum ; Driver program to test above ; binary tree formation ; 4 ; / \ ; 2 5 ; / \ / \ ; 7 1 2 3 ; 6
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; Node * getNode ( int data ) { Node * newNode = ( Node * ) malloc ( sizeof ( Node ) ) ; newNode -> data = data ; newNode -> left = newNode -> right = NULL ; return newNode ; } void sumOfLongRootToLeafPath ( Node * root , int sum , int len , int & maxLen , int & maxSum ) { if ( ! root ) { if ( maxLen < len ) { maxLen = len ; maxSum = sum ; } else if ( maxLen == len && maxSum < sum ) maxSum = sum ; return ; } sumOfLongRootToLeafPath ( root -> left , sum + root -> data , len + 1 , maxLen , maxSum ) ; sumOfLongRootToLeafPath ( root -> right , sum + root -> data , len + 1 , maxLen , maxSum ) ; } int sumOfLongRootToLeafPathUtil ( Node * root ) { if ( ! root ) return 0 ; int maxSum = INT_MIN , maxLen = 0 ; sumOfLongRootToLeafPath ( root , 0 , 0 , maxLen , maxSum ) ; return maxSum ; } int main ( ) { Node * root = getNode ( 4 ) ; root -> left = getNode ( 2 ) ; root -> right = getNode ( 5 ) ; root -> left -> left = getNode ( 7 ) ; root -> left -> right = getNode ( 1 ) ; root -> right -> left = getNode ( 2 ) ; root -> right -> right = getNode ( 3 ) ; root -> left -> right -> left = getNode ( 6 ) ; cout << " Sum ▁ = ▁ " << sumOfLongRootToLeafPathUtil ( root ) ; return 0 ; }
Count L | C ++ program for the above approach ; Fumction to find the number of arrays of length L such that each element divides the next element ; Stores the number of sequences of length i that ends with j ; Initialize 2D array dp [ ] [ ] as 0 ; Base Case ; Iterate over the range [ 0 , l ] ; Iterate for all multiples of j ; Incrementing dp [ i + 1 ] [ k ] by dp [ i ] [ j ] as the next element is multiple of j ; Stores the number of arrays ; Add all array of length L that ends with i ; Return the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfArrays ( int n , int l ) { int dp [ l + 1 ] [ n + 1 ] ; memset ( dp , 0 , sizeof dp ) ; dp [ 0 ] [ 1 ] = 1 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int k = j ; k <= n ; k += j ) { dp [ i + 1 ] [ k ] += dp [ i ] [ j ] ; } } } int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { ans += dp [ l ] [ i ] ; } return ans ; } int main ( ) { int N = 2 , L = 4 ; cout << numberOfArrays ( N , L ) ; return 0 ; }
Count minimum steps to get the given desired array | C ++ program to count minimum number of operations to get the given target array ; Returns count of minimum operations to convert a zero array to target array with increment and doubling operations . This function computes count by doing reverse steps , i . e . , convert target to zero array . ; Initialize result ( Count of minimum moves ) ; Keep looping while all elements of target don 't become 0. ; To store count of zeroes in current target array ; To find first odd element ; If odd number found ; If 0 , then increment zero_count ; All numbers are 0 ; All numbers are even ; Divide the whole array by 2 and increment result ; Make all odd numbers even by subtracting one and increment result . ; Driver program to test above functions
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinOperations ( unsigned int target [ ] , int n ) { int result = 0 ; while ( 1 ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( target [ i ] & 1 ) break ; else if ( target [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; if ( i == n ) { for ( int j = 0 ; j < n ; j ++ ) target [ j ] = target [ j ] / 2 ; result ++ ; } for ( int j = i ; j < n ; j ++ ) { if ( target [ j ] & 1 ) { target [ j ] -- ; result ++ ; } } } } int main ( ) { unsigned int arr [ ] = { 16 , 16 , 16 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ number ▁ of ▁ steps ▁ required ▁ to ▁ " " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " << countMinOperations ( arr , n ) ; return 0 ; }
Maximize sum of odd | C ++ program for the above approach ; Function to find the maximum sum of array elements chosen by Player A according to the given criteria ; Store the key ; Corner Case ; Check if all the elements can be taken or not ; If the difference is less than or equal to the available chances then pick all numbers ; Find the sum of array elements over the range [ start , N ] ; If yes then return that value ; Traverse over the range [ 1 , 2 * M ] ; Sum of elements for Player A ; Even chance sum can be obtained by subtracting the odd chances sum - total and picking up the maximum from that ; Storing the value in dictionary ; Return the maximum sum of odd chances ; Driver code ; Stores the precomputed values ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int recursiveChoosing ( int arr [ ] , int start , int M , map < pair < int , int > , int > dp , int N ) { pair < int , int > key ( start , M ) ; if ( start >= N ) { return 0 ; } if ( N - start <= 2 * M ) { int Sum = 0 ; for ( int i = start ; i < N ; i ++ ) { Sum = Sum + arr [ i ] ; } return Sum ; } int sum = 0 ; for ( int i = start ; i < N ; i ++ ) { sum = sum + arr [ i ] ; } int total = sum ; if ( dp . find ( key ) != dp . end ( ) ) { return dp [ key ] ; } int psa = 0 ; for ( int x = 1 ; x < 2 * M + 1 ; x ++ ) { int psb = recursiveChoosing ( arr , start + x , max ( x , M ) , dp , N ) ; psa = max ( psa , total - psb ) ; } dp [ key ] = psa ; return dp [ key ] ; } int main ( ) { int arr [ ] = { 2 , 7 , 9 , 4 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; map < pair < int , int > , int > dp ; cout << recursiveChoosing ( arr , 0 , 1 , dp , N ) ; return 0 ; }
Number of subsets with product less than k | CPP to find the count subset having product less than k ; declare four vector for dividing array into two halves and storing product value of possible subsets for them ; ignore element greater than k and divide array into 2 halves ; ignore element if greater than k ; generate all subsets for 1 st half ( vect1 ) ; push only in case subset product is less than equal to k ; generate all subsets for 2 nd half ( vect2 ) ; push only in case subset product is less than equal to k ; sort subset2 ; for null subset decrement the value of count ; return count ; driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubset ( long long int arr [ ] , int n , long long int k ) { vector < long long int > vect1 , vect2 , subset1 , subset2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > k ) continue ; if ( i <= n / 2 ) vect1 . push_back ( arr [ i ] ) ; else vect2 . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < ( 1 << vect1 . size ( ) ) ; i ++ ) { long long value = 1 ; for ( int j = 0 ; j < vect1 . size ( ) ; j ++ ) { if ( i & ( 1 << j ) ) value *= vect1 [ j ] ; } if ( value <= k ) subset1 . push_back ( value ) ; } for ( int i = 0 ; i < ( 1 << vect2 . size ( ) ) ; i ++ ) { long long value = 1 ; for ( int j = 0 ; j < vect2 . size ( ) ; j ++ ) { if ( i & ( 1 << j ) ) value *= vect2 [ j ] ; } if ( value <= k ) subset2 . push_back ( value ) ; } sort ( subset2 . begin ( ) , subset2 . end ( ) ) ; long long count = 0 ; for ( int i = 0 ; i < subset1 . size ( ) ; i ++ ) count += upper_bound ( subset2 . begin ( ) , subset2 . end ( ) , ( k / subset1 [ i ] ) ) - subset2 . begin ( ) ; count -- ; return count ; } int main ( ) { long long int arr [ ] = { 4 , 2 , 3 , 6 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; long long int k = 25 ; cout << findSubset ( arr , n , k ) ; return 0 ; }
Maximum Sum Subsequence made up of consecutive elements of different parity | C ++ program for the above approach ; Function to find the maximum sum of subsequence with consecutive terms having different parity ; Base Case ; Store the parity of number at the ith position ; If the dp state has already been calculated , return it ; If the array is traversed and no element has been selected yet then select the current element ; If the parity of the current and previously selected element are different , then select the current element ; Skip the current element and move to the next element ; Return the result ; Function to calculate the maximum sum subsequence with consecutive terms having different parity ; Initialize the dp [ ] array with - 1 ; Initially the prev value is set to say 2 , as the first element can anyways be selected ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 100 ] [ 3 ] ; int maxSum ( int * arr , int i , int n , int prev , bool is_selected ) { if ( i == n ) { return 0 ; } int cur = abs ( arr [ i ] ) % 2 ; if ( dp [ i ] [ prev ] != -1 ) { return dp [ i ] [ prev ] ; } if ( i == n - 1 && is_selected == 0 ) return dp [ i ] [ prev ] = arr [ i ] ; if ( cur != prev ) { dp [ i ] [ prev ] = arr [ i ] + maxSum ( arr , i + 1 , n , cur , 1 ) ; } dp [ i ] [ prev ] = max ( dp [ i ] [ prev ] , maxSum ( arr , i + 1 , n , prev , is_selected ) ) ; return dp [ i ] [ prev ] ; } void maxSumUtil ( int arr [ ] , int n ) { memset ( dp , -1 , sizeof ( dp ) ) ; cout << maxSum ( arr , 0 , n , 2 , 0 ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 6 , 8 , -5 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSumUtil ( arr , N ) ; return 0 ; }
Find minimum number of merge operations to make an array palindrome | C ++ program to find number of operations to make an array palindrome ; Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinOps ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 4 , 5 , 9 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " << findMinOps ( arr , n ) << endl ; return 0 ; }
Find the smallest positive integer value that cannot be represented as sum of any subset of a given array | C ++ program to find the smallest positive value that cannot be represented as sum of subsets of a given sorted array ; Returns the smallest number that cannot be represented as sum of subset of elements from set represented by sorted array arr [ 0. . n - 1 ] ; Initialize result ; Traverse the array and increment ' res ' if arr [ i ] is smaller than or equal to ' res ' . ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSmallest ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } int main ( ) { int arr1 [ ] = { 1 , 3 , 4 , 5 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; cout << findSmallest ( arr1 , n1 ) << endl ; int arr2 [ ] = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << findSmallest ( arr2 , n2 ) << endl ; int arr3 [ ] = { 1 , 1 , 1 , 1 } ; int n3 = sizeof ( arr3 ) / sizeof ( arr3 [ 0 ] ) ; cout << findSmallest ( arr3 , n3 ) << endl ; int arr4 [ ] = { 1 , 1 , 3 , 4 } ; int n4 = sizeof ( arr4 ) / sizeof ( arr4 [ 0 ] ) ; cout << findSmallest ( arr4 , n4 ) << endl ; return 0 ; }
Count distinct possible Bitwise XOR values of subsets of an array | C ++ program for the above approach ; Stores the Bitwise XOR of every possible subset ; Function to generate all combinations of subsets and store their Bitwise XOR in set S ; If the end of the subset is reached ; Stores the Bitwise XOR of the current subset ; Iterate comb [ ] to find XOR ; Insert the Bitwise XOR of R elements ; Otherwise , iterate to generate all possible subsets ; Recursive call for next index ; Function to find the size of the set having Bitwise XOR of all the subsets of the given array ; Iterate ove the given array ; Generate all possible subsets ; Print the size of the set ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < int > s ; void countXOR ( int arr [ ] , int comb [ ] , int start , int end , int index , int r ) { if ( index == r ) { int new_xor = 0 ; for ( int j = 0 ; j < r ; j ++ ) { new_xor ^= comb [ j ] ; } s . insert ( new_xor ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { comb [ index ] = arr [ i ] ; countXOR ( arr , comb , i + 1 , end , index + 1 , r ) ; } } void maxSizeSet ( int arr [ ] , int N ) { for ( int r = 2 ; r <= N ; r ++ ) { int comb [ r + 1 ] ; countXOR ( arr , comb , 0 , N - 1 , 0 , r ) ; } cout << s . size ( ) << endl ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxSizeSet ( arr , N ) ; return 0 ; }
Size of The Subarray With Maximum Sum | C ++ program to print length of the largest contiguous array sum ; Function to find maximum subarray sum ; Driver program to test maxSubArraySum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } return ( end - start + 1 ) ; } int main ( ) { int a [ ] = { -2 , -3 , 4 , -1 , -2 , 1 , 5 , -3 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << maxSubArraySum ( a , n ) ; return 0 ; }
Longest subarray of an array which is a subsequence in another array | C ++ program for the above approach ; Function to find the length of the longest subarray in arr1 [ ] which is a subsequence in arr2 [ ] ; Length of the required longest subarray ; Initialize DP [ ] array ; Traverse array arr1 [ ] ; Traverse array arr2 [ ] ; arr1 [ i - 1 ] contributes to the length of the subarray ; Otherwise ; Find the maximum value present in DP [ ] [ ] ; Return the result ; Driver Code ; Function call to find the length of the longest required subarray
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongSubarrSeq ( int arr1 [ ] , int arr2 [ ] , int M , int N ) { int maxL = 0 ; int DP [ M + 1 ] [ N + 1 ] ; for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( arr1 [ i - 1 ] == arr2 [ j - 1 ] ) { DP [ i ] [ j ] = 1 + DP [ i - 1 ] [ j - 1 ] ; } else { DP [ i ] [ j ] = DP [ i ] [ j - 1 ] ; } } } for ( int i = 1 ; i <= M ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { maxL = max ( maxL , DP [ i ] [ j ] ) ; } } return maxL ; } int main ( ) { int arr1 [ ] = { 4 , 2 , 3 , 1 , 5 , 6 } ; int M = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int arr2 [ ] = { 3 , 1 , 4 , 6 , 5 , 2 } ; int N = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << LongSubarrSeq ( arr1 , arr2 , M , N ) << endl ; return 0 ; }