codeparrot/xlcost-text-to-code huggingface dataset:
Count triplets from an array which can form quadratic equations with real roots | C ++ program for the above approach ; Function to count the number of triplets ( a , b , c ) such that the equation ax ^ 2 + bx + c = 0 has real roots ; Sort the array in ascending order ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Traverse the given array ; If values of a and c are equal to b ; Increment a ; Decrement c ; Condition for having real roots for a quadratic equation ; If b lies in between a and c ; Update count ; Update count ; Increment a ; Decrement c ; For each pair two values are possible of a and c ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { int a = 0 , c = N - 1 ; int d = arr [ b ] * arr [ b ] / 4 ; while ( a < c ) { if ( a == b ) { a ++ ; continue ; } if ( c == b ) { c -- ; continue ; } if ( arr [ a ] * arr <= d ) { if ( a < b && b < c ) { count += c - a - 1 ; } else { count += c - a ; } a ++ ; } else { c -- ; } } } return count * 2 ; } int main ( ) { int arr [ ] = { 3 , 6 , 10 , 13 , 21 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getCount ( arr , N ) ; return 0 ; }
Maximize difference between the sum of absolute differences of each element with the remaining array | C ++ program for the above approach ; Function to maximize difference of the sum of absolute difference of an element with the rest of the elements in the array ; Sort the array in ascending order ; Stores prefix sum at any instant ; Store the total array sum ; Initialize minimum and maximum absolute difference ; Traverse the array to find the total array sum ; Traverse the array arr [ ] ; Store the number of elements to its left ; Store the number of elements to its right ; Update the sum of elements on its left ; Store the absolute difference sum ; Update the Minimum ; Update the Maximum ; Update sum of elements on its left ; Print the result ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxDifference ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int Leftsum = 0 ; int Totalsum = 0 ; int Min = INT_MAX , Max = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) Totalsum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int leftNumbers = i ; int rightNumbers = n - i - 1 ; Totalsum = Totalsum - arr [ i ] ; int sum = ( leftNumbers * arr [ i ] ) - Leftsum + Totalsum - ( rightNumbers * arr [ i ] ) ; Min = min ( Min , sum ) ; Max = max ( Max , sum ) ; Leftsum += arr [ i ] ; } cout << Max - Min ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxDifference ( arr , N ) ; return 0 ; }
Minimum pairs required to be removed such that the array does not contain any pair with sum K | C ++ 14 program to implement the above approach ; Function to find the maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Stores maximum count of pairs required to be removed such that no pairs exist whose sum equal to K ; Base Case ; Sort the array ; Stores index of left pointer ; Stores index of right pointer ; Stores sum of left and right pointer ; If s equal to k ; Update cntPairs ; Update left ; Update right ; If s > k ; Update right ; Update left ; Return the cntPairs ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxcntPairsSumKRemoved ( vector < int > arr , int k ) { int cntPairs = 0 ; if ( arr . size ( ) <= 1 ) return cntPairs ; sort ( arr . begin ( ) , arr . end ( ) ) ; int left = 0 ; int right = arr . size ( ) - 1 ; while ( left < right ) { int s = arr [ left ] + arr [ right ] ; if ( s == k ) { cntPairs += 1 ; left += 1 ; right -= 1 ; } else if ( s > k ) right -= 1 ; else left += 1 ; } return cntPairs ; } int main ( ) { vector < int > arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; cout << ( maxcntPairsSumKRemoved ( arr , K ) ) ; return 0 ; }
Minimize cost to split an array into K subsets such that the cost of each element is its product with its position in the subset | C ++ program to implement the above approach ; Function to find the minimum cost to split array into K subsets ; Sort the array in descending order ; Stores minimum cost to split the array into K subsets ; Stores position of elements of a subset ; Iterate over the range [ 1 , N ] ; Calculate the cost to select X - th element of every subset ; Update min_cost ; Update X ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getMinCost ( int * arr , int n , int k ) { sort ( arr , arr + n , greater < int > ( ) ) ; int min_cost = 0 ; int X = 0 ; for ( int i = 0 ; i < n ; i += k ) { for ( int j = i ; j < i + k && j < n ; j ++ ) { min_cost += arr [ j ] * ( X + 1 ) ; } X ++ ; } return min_cost ; } int main ( ) { int arr [ ] = { 9 , 20 , 7 , 8 } ; int K = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getMinCost ( arr , N , K ) << endl ; }
Minimize difference between the largest and smallest array elements by K replacements | C ++ program for the above approach ; Function to find minimum difference between largest and smallest element after K replacements ; Sort array in ascending order ; Minimum difference ; Check for all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Length of array ; Prints the minimum possible difference
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDiff ( int A [ ] , int K , int n ) { sort ( A , A + n ) ; if ( n <= K ) return 0 ; int mindiff = A [ n - 1 ] - A [ 0 ] ; if ( K == 0 ) return mindiff ; for ( int i = 0 , j = n - 1 - K ; j < n ; ) { mindiff = min ( mindiff , A [ j ] - A [ i ] ) ; i ++ ; j ++ ; } return mindiff ; } int main ( ) { int A [ ] = { -1 , 3 , -1 , 8 , 5 , 4 } ; int K = 3 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minDiff ( A , K , n ) ; return 0 ; }
Minimize difference between the largest and smallest array elements by K replacements | C ++ program for above approach ; Function to find minimum difference between the largest and smallest element after K replacements ; Create a MaxHeap ; Create a MinHeap ; Update maxHeap and MinHeap with highest and smallest K elements respectively ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Insert current element into the MaxHeap ; If maxHeap size exceeds K + 1 ; Remove top element ; Store all max element from maxHeap ; Store all min element from minHeap ; Generating all K + 1 possibilities ; Return answer ; Driver Code ; Given array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDiff ( int A [ ] , int K , int N ) { if ( N <= K + 1 ) return 0 ; priority_queue < int , vector < int > , greater < int > > maxHeap ; priority_queue < int > minHeap ; for ( int i = 0 ; i < N ; i ++ ) { maxHeap . push ( A [ i ] ) ; if ( maxHeap . size ( ) > K + 1 ) maxHeap . pop ( ) ; minHeap . push ( A [ i ] ) ; if ( minHeap . size ( ) > K + 1 ) minHeap . pop ( ) ; } vector < int > maxList ; while ( maxHeap . size ( ) > 0 ) { maxList . push_back ( maxHeap . top ( ) ) ; maxHeap . pop ( ) ; } vector < int > minList ; while ( minHeap . size ( ) > 0 ) { minList . push_back ( minHeap . top ( ) ) ; minHeap . pop ( ) ; } int mindiff = INT_MAX ; for ( int i = 0 ; i <= K ; i ++ ) { mindiff = min ( mindiff , maxList [ i ] - minList [ K - i ] ) ; } return mindiff ; } int main ( ) { int A [ ] = { -1 , 3 , -1 , 8 , 5 , 4 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 3 ; cout << minDiff ( A , K , N ) ; return 0 ; }
Check if all K | C ++ program to implement the above approach ; Function to check all subset - sums of K - length subsets in A [ ] is greater that that in the array B [ ] or not ; Sort the array in ascending order ; Sort the array in descending order ; Stores sum of first K elements of A [ ] ; Stores sum of first K elements of B [ ] ; Traverse both the arrays ; Update sum1 ; Update sum2 ; If sum1 exceeds sum2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkSubsetSum ( int A [ ] , int B [ ] , int N , int K ) { sort ( A , A + N ) ; sort ( B , B + N , greater < int > ( ) ) ; int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum1 += A [ i ] ; sum2 += B [ i ] ; } if ( sum1 > sum2 ) { return true ; } return false ; } int main ( ) { int A [ ] = { 12 , 11 , 10 , 13 } ; int B [ ] = { 7 , 10 , 6 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int K = 3 ; if ( checkSubsetSum ( A , B , N , K ) ) { cout << " YES " ; } else { cout << " NO " ; } return 0 ; }
Sort given array to descending | C ++ program to implement the above approach ; Function to sort first K array elements in descending and last N - K in ascending order ; Sort the array in descending order ; Sort last ( N - K ) array elements in ascending order ; Print array elements ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortArrayInDescAsc ( int arr [ ] , int N , int K ) { sort ( arr , arr + N , greater < int > ( ) ) ; sort ( arr + K , arr + N ) ; for ( int i = 0 ; i < N ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 7 , 6 , 8 , 9 , 0 , 1 , 2 , 2 , 1 , 8 , 9 , 6 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 6 ; sortArrayInDescAsc ( arr , N , K ) ; }
Median of all non | C ++ program to implement the above approach ; Function to calculate the median of all possible subsets by given operations ; Stores sum of elements of arr [ ] ; Traverse the array arr [ ] ; Update sum ; Sort the array ; DP [ i ] [ j ] : Stores total number of ways to form the sum j by either selecting ith element or not selecting ith item . ; Initialize all the DP states ; Base case ; Fill dp [ i ] [ 0 ] ; Base case ; Fill all the DP states based on the mentioned DP relation ; If j is greater than or equal to arr [ i ] ; Update dp [ i ] [ j ] ; Update dp [ i ] [ j ] ; Stores all possible subset sum ; Traverse all possible subset sum ; Stores count of subsets whose sum is j ; Itearate over the range [ 1 , M ] ; Insert j into sumSub ; Stores middle element of sumSub ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMedianOfsubSum ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } sort ( arr , arr + N ) ; int dp [ N ] [ sum + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } dp [ 0 ] [ arr [ 0 ] ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j >= arr [ i ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - arr [ i ] ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } vector < int > sumSub ; for ( int j = 1 ; j <= sum ; j ++ ) { int M = dp [ N - 1 ] [ j ] ; for ( int i = 1 ; i <= M ; i ++ ) { sumSub . push_back ( j ) ; } } int mid = sumSub [ sumSub . size ( ) / 2 ] ; return mid ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMedianOfsubSum ( arr , N ) ; return 0 ; }
Reduce array to a single element by repeatedly replacing adjacent unequal pairs with their maximum | C ++ program for the above approach ; Function to print the index from where the operation can be started ; Initialize B [ ] ; Initialize save ; Make B [ ] equals to arr [ ] ; Sort the array B [ ] ; Traverse from N - 1 to 1 ; If B [ i ] & B [ i - 1 ] are unequal ; If all elements are same ; If arr [ 1 ] is maximum element ; If arr [ N - 1 ] is maximum element ; Find the maximum element ; Driver Code ; Given array arr [ ] ; Length of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printIndex ( int arr [ ] , int N ) { int B [ N ] ; int save = -1 ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = arr [ i ] ; } sort ( B , B + N ) ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( B [ i ] != B [ i - 1 ] ) { save = B [ i ] ; break ; } } if ( save == -1 ) { cout << -1 << endl ; return ; } if ( save == arr [ 0 ] && save != arr [ 1 ] ) { cout << 1 ; } else if ( save == arr [ N - 1 ] && save != arr [ N - 2 ] ) { cout << N ; } for ( int i = 1 ; i < N - 1 ; i ++ ) { if ( save == arr [ i ] && ( save != arr [ i - 1 ] save != arr [ i + 1 ] ) ) { cout << i + 1 ; break ; } } } int main ( ) { int arr [ ] = { 5 , 3 , 4 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printIndex ( arr , N ) ; return 0 ; }
Lexicographically smallest subsequence possible by removing a character from given string | C ++ program for the above approach ; Function to find the lexicographically smallest subsequence of length N - 1 ; Generate all subsequence of length N - 1 ; Store main value of string str ; Erasing element at position i ; Sort the vector ; Print first element of vector ; Driver Code ; Given string S ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void firstSubsequence ( string s ) { vector < string > allsubseq ; string k ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { k = s ; k . erase ( i , 1 ) ; allsubseq . push_back ( k ) ; } sort ( allsubseq . begin ( ) , allsubseq . end ( ) ) ; cout << allsubseq [ 0 ] ; } int main ( ) { string S = " geeksforgeeks " ; firstSubsequence ( S ) ; return 0 ; }
Maximum even sum subsequence of length K | C ++ program to implement the above approach ; Function to find the maximum even sum of any subsequence of length K ; If count of elements is less than K ; Stores maximum even subsequence sum ; Stores Even numbers ; Stores Odd numbers ; Traverse the array ; If current element is an odd number ; Insert odd number ; Insert even numbers ; Sort Odd [ ] array ; Sort Even [ ] array ; Stores current index Of Even [ ] array ; Stores current index Of Odd [ ] array ; If K is odd ; If count of elements in Even [ ] >= 1 ; Update maxSum ; Update i ; If count of elements in Even [ ] array is 0. ; Update K ; If count of elements in Even [ ] and odd [ ] >= 2 ; Update maxSum ; Update j . ; Update maxSum ; Update i ; Update K ; If count of elements in Even [ ] array >= 2. ; Update maxSum ; Update i . ; Update K . ; If count of elements in Odd [ ] array >= 1 ; Update maxSum ; Update i . ; Update K . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int evenSumK ( int arr [ ] , int N , int K ) { if ( K > N ) { return -1 ; } int maxSum = 0 ; vector < int > Even ; vector < int > Odd ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 ) { Odd . push_back ( arr [ i ] ) ; } else { Even . push_back ( arr [ i ] ) ; } } sort ( Odd . begin ( ) , Odd . end ( ) ) ; sort ( Even . begin ( ) , Even . end ( ) ) ; int i = Even . size ( ) - 1 ; int j = Odd . size ( ) - 1 ; while ( K > 0 ) { if ( K % 2 == 1 ) { if ( i >= 0 ) { maxSum += Even [ i ] ; i -- ; } else { return -1 ; } K -- ; } else if ( i >= 1 && j >= 1 ) { if ( Even [ i ] + Even [ i - 1 ] <= Odd [ j ] + Odd [ j - 1 ] ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; } else { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; } K -= 2 ; } else if ( i >= 1 ) { maxSum += Even [ i ] + Even [ i - 1 ] ; i -= 2 ; K -= 2 ; } else if ( j >= 1 ) { maxSum += Odd [ j ] + Odd [ j - 1 ] ; j -= 2 ; K -= 2 ; } } return maxSum ; } int main ( ) { int arr [ ] = { 2 , 4 , 10 , 3 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << evenSumK ( arr , N , K ) ; }
Program to find weighted median of a given array | C ++ program for the above approach ; Function to calculate weighted median ; Store pr of arr [ i ] and W [ i ] ; Sort the list of pr w . r . t . to their arr [ ] values ; If N is odd ; Traverse the set pr from left to right ; Update sums ; If sum becomes > 0.5 ; If N is even ; For lower median traverse the set pr from left ; Update sums ; When sum >= 0.5 ; For upper median traverse the set pr from right ; Update sums ; When sum >= 0.5 ; Driver Code ; Given array arr [ ] ; Given weights W [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void weightedMedian ( vector < int > arr , vector < float > W ) { vector < pair < int , float > > pr ; for ( int index = 0 ; index < arr . size ( ) ; index ++ ) pr . push_back ( { arr [ index ] , W [ index ] } ) ; sort ( pr . begin ( ) , pr . end ( ) ) ; if ( arr . size ( ) % 2 != 0 ) { float sums = 0 ; for ( auto element : pr ) { sums += element . second ; if ( sums > 0.5 ) cout << " The ▁ Weighted ▁ Median ▁ is ▁ element ▁ " << element . first << endl ; } } else { float sums = 0 ; for ( auto element : pr ) { sums += element . second ; if ( sums >= 0.5 ) { cout << " Lower ▁ Weighted ▁ Median ▁ is ▁ element ▁ " << element . first << endl ; break ; } } sums = 0 ; for ( int index = pr . size ( ) - 1 ; index >= 0 ; index -- ) { int element = pr [ index ] . first ; float weight = pr [ index ] . second ; sums += weight ; if ( sums >= 0.5 ) { cout << " Upper ▁ Weighted ▁ Median ▁ is ▁ element ▁ " << element ; break ; } } } } int main ( ) { vector < int > arr = { 4 , 1 , 3 , 2 } ; vector < float > W = { 0.25 , 0.49 , 0.25 , 0.01 } ; weightedMedian ( arr , W ) ; }
Kth smallest element from an array of intervals | C ++ Program to implement the above approach ; Function to get the Kth smallest element from an array of intervals ; Store all the intervals so that it returns the minimum element in O ( 1 ) ; Insert all Intervals into the MinHeap ; Stores the count of popped elements ; Iterate over MinHeap ; Stores minimum element from all remaining intervals ; Remove minimum element ; Check if the minimum of the current interval is less than the maximum of the current interval ; Insert new interval ; Driver Code ; Intervals given ; Size of the arr
#include <bits/stdc++.h> NEW_LINE using namespace std ; int KthSmallestNum ( pair < int , int > arr [ ] , int n , int k ) { priority_queue < pair < int , int > , vector < pair < int , int > > , greater < pair < int , int > > > pq ; for ( int i = 0 ; i < n ; i ++ ) { pq . push ( { arr [ i ] . first , arr [ i ] . second } ) ; } int cnt = 1 ; while ( cnt < k ) { pair < int , int > interval = pq . top ( ) ; pq . pop ( ) ; if ( interval . first < interval . second ) { pq . push ( { interval . first + 1 , interval . second } ) ; } cnt ++ ; } return pq . top ( ) . first ; } int main ( ) { pair < int , int > arr [ ] = { { 5 , 11 } , { 10 , 15 } , { 12 , 20 } } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 12 ; cout << KthSmallestNum ( arr , n , k ) ; }
Maximum Manhattan distance between a distinct pair from N coordinates | C ++ program for the above approach ; Function to calculate the maximum Manhattan distance ; Stores the maximum distance ; Find Manhattan distance using the formula | x1 - x2 | + | y1 - y2 | ; Updating the maximum ; Driver Code ; Given Co - ordinates ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaxDist ( vector < pair < int , int > > & A , int N ) { int maximum = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { sum = abs ( A [ i ] . first - A [ j ] . first ) + abs ( A [ i ] . second - A [ j ] . second ) ; maximum = max ( maximum , sum ) ; } } cout << maximum ; } int main ( ) { int N = 3 ; vector < pair < int , int > > A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; MaxDist ( A , N ) ; return 0 ; }
Longest increasing subsequence which forms a subarray in the sorted representation of the array | C ++ program to implement the above approach ; Function to find the length of the longest increasing sorted sequence ; Stores the count of all elements ; Store the original array ; Sort the array ; If adjacent element are not same ; Increment count ; Store frequency of each element ; Initialize a DP array ; Iterate over the array ar [ ] ; Length of the longest increasing sorted sequence ; Iterate over the array ; Current element ; If the element has been encountered the first time ; If all the x - 1 previous elements have already appeared ; Otherwise ; If all x - 1 elements have already been encountered ; Increment the count of the current element ; Update maximum subsequence size ; Return the maximum subsequence size ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int LongestSequence ( int a [ ] , int n ) { map < int , int > m ; int ar [ n + 1 ] , i , j ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = a [ i - 1 ] ; } sort ( a , a + n ) ; int c = 1 ; m [ a [ 0 ] ] = c ; for ( i = 1 ; i <= n ; i ++ ) { if ( a [ i ] != a [ i - 1 ] ) { c ++ ; m [ a [ i ] ] = c ; } } map < int , int > cnt ; int dp [ n + 1 ] [ 3 ] = { 0 } ; cnt [ 0 ] = 0 ; for ( i = 1 ; i <= n ; i ++ ) { ar [ i ] = m [ ar [ i ] ] ; cnt [ ar [ i ] ] ++ ; } int ans = 0 , x ; for ( i = 1 ; i <= n ; i ++ ) { x = ar [ i ] ; if ( dp [ x ] [ 0 ] == 0 ) { if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 1 ] ; dp [ x ] [ 2 ] = dp [ x - 1 ] [ 1 ] ; } else { dp [ x ] [ 1 ] = dp [ x - 1 ] [ 0 ] ; } } dp [ x ] [ 2 ] = max ( dp [ x - 1 ] [ 0 ] , dp [ x ] [ 2 ] ) ; if ( dp [ x - 1 ] [ 0 ] == cnt [ x - 1 ] ) { dp [ x ] [ 2 ] = max ( dp [ x ] [ 2 ] , dp [ x - 1 ] [ 1 ] ) ; } for ( j = 0 ; j < 3 ; j ++ ) { dp [ x ] [ j ] ++ ; ans = max ( ans , dp [ x ] [ j ] ) ; } } return ans ; } int main ( ) { int arr [ ] = { 2 , 6 , 4 , 8 , 2 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << LongestSequence ( arr , N ) ; return 0 ; }
Maximize the sum of Kth column of a Matrix | C ++ program to implement the above approach ; Function to maximize the Kth column sum ; Store all the elements of the resultant matrix of size N * N ; Store value of each elements of the matrix ; Fill all the columns < K ; Fill all the columns >= K ; Function to print the matrix ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int * * findMatrix ( int N , int K ) { int * * mat = ( int * * ) malloc ( N * sizeof ( int * ) ) ; for ( int i = 0 ; i < N ; ++ i ) { mat [ i ] = ( int * ) malloc ( N * sizeof ( int ) ) ; } int element = 1 ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < K - 1 ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = K - 1 ; j < N ; ++ j ) { mat [ i ] [ j ] = element ++ ; } } return mat ; } void printMatrix ( int * * mat , int N ) { for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { cout << mat [ i ] [ j ] << " ▁ " ; } cout << endl ; } } int main ( ) { int N = 3 , K = 2 ; int * * mat = findMatrix ( N , K ) ; printMatrix ( mat , N ) ; }
Range sum queries based on given conditions | C ++ program for the above approach ; Function to calculate the sum between the given range as per value of m ; Stores the sum ; Condition for a to print the sum between ranges [ a , b ] ; Return sum ; Function to precalculate the sum of both the vectors ; Make Prefix sum array ; Function to compute the result for each query ; Take a dummy vector and copy the element of arr in brr ; Sort the dummy vector ; Compute prefix sum of both vectors ; Performs operations ; Function Call to find sum ; Function Call to find sum ; Driver Code ; Given arr [ ] ; Number of queries ; Given queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int range_sum ( vector < int > & arr , int a , int b ) { int sum = 0 ; if ( a - 2 < 0 ) sum = arr [ b - 1 ] ; else sum = arr [ b - 1 ] - arr [ a - 2 ] ; return sum ; } void precompute_sum ( vector < int > & arr , vector < int > & brr ) { int N = ( int ) arr . size ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; brr [ i ] = brr [ i ] + brr [ i - 1 ] ; } } void find_sum ( vector < int > & arr , int q , int Queries [ ] [ 3 ] ) { vector < int > brr ( arr ) ; int N = ( int ) arr . size ( ) ; sort ( brr . begin ( ) , brr . end ( ) ) ; precompute_sum ( arr , brr ) ; for ( int i = 0 ; i < q ; i ++ ) { int m = Queries [ i ] [ 0 ] ; int a = Queries [ i ] [ 1 ] ; int b = Queries [ i ] [ 2 ] ; if ( m == 1 ) { cout << range_sum ( arr , a , b ) << ' ▁ ' ; } else if ( m == 2 ) { cout << range_sum ( brr , a , b ) << ' ▁ ' ; } } } int main ( ) { vector < int > arr = { 0 , 6 , 4 , 2 , 7 , 2 , 7 } ; int Q = 1 ; int Queries [ ] [ 3 ] = { { 2 , 3 , 6 } } ; find_sum ( arr , Q , Queries ) ; return 0 ; }