codeparrot/xlcost-text-to-code huggingface dataset:
Largest sum subarray of size K containing consecutive elements | C ++ program for the above approach ; Function to find the largest sum subarray such that it contains K consecutive elements ; Stores sum of subarray having K consecutive elements ; Stores the maximum sum among all subarrays of size K having consecutive elements ; Traverse the array ; Store K elements of one subarray at a time ; Sort the duplicate array in ascending order ; Checks if elements in subarray are consecutive or not ; Traverse the k elements ; If not consecutive , break ; If flag is true update the maximum sum ; Stores the sum of elements of the current subarray ; Update the max_sum ; Reset curr_sum ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( vector < int > A , int N , int K ) { int curr_sum = 0 ; int max_sum = INT_MIN ; for ( int i = 0 ; i < N - K + 1 ; i ++ ) { vector < int > dupl_arr ( A . begin ( ) + i , A . begin ( ) + i + K ) ; sort ( dupl_arr . begin ( ) , dupl_arr . end ( ) ) ; bool flag = true ; for ( int j = 1 ; j < K ; j ++ ) { if ( dupl_arr [ j ] - dupl_arr [ j - 1 ] != 1 ) { flag = false ; break ; } } if ( flag ) { int temp = 0 ; curr_sum = accumulate ( dupl_arr . begin ( ) , dupl_arr . end ( ) , temp ) ; max_sum = max ( max_sum , curr_sum ) ; curr_sum = 0 ; } } return max_sum ; } int main ( ) { vector < int > arr = { 10 , 12 , 9 , 8 , 10 , 15 , 1 , 3 , 2 } ; int K = 3 ; int N = arr . size ( ) ; cout << maximumSum ( arr , N , K ) ; return 0 ; }
Find the count of Smith Brothers Pairs in a given Array | C ++ program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Iterate through all pairs ; Increment count if there is a smith brother pair ; Driver code ; Preprocessing ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 100 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } bool isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { int p = primes [ i ] ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } bool isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) && abs ( X - Y ) == 1 ; } int countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } int main ( ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countSmithBrotherPairs ( A , N ) << endl ; return 0 ; }
Find the count of Smith Brothers Pairs in a given Array | C ++ program for the above approach ; array to store all prime less than and equal to MAX ; utility function for sieve of sundaram ; Main logic of Sundaram . ; Since 2 is a prime number ; only primes are selected ; Function to check whether a number is a smith number . ; Find sum the digits of prime factors of n ; add its digits of prime factors to pDigitSum . ; one prime factor is still to be summed up ; Now sum the digits of the original number ; return the answer ; Function to check if X and Y are a Smith Brother Pair ; Function to find pairs from array ; Variable to store number of Smith Brothers Pairs ; sort A ; check for consecutive numbers only ; Driver code ; Preprocessing sieve of sundaram ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 10000 ; vector < int > primes ; void sieveSundaram ( ) { bool marked [ MAX / 2 + 100 ] = { 0 } ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . push_back ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . push_back ( 2 * i + 1 ) ; } bool isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes [ i ] <= n / 2 ; i ++ ) { while ( n % primes [ i ] == 0 ) { int p = primes [ i ] ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } bool isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) ; } int countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; sort ( A , A + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( isSmithBrotherPair ( A [ i ] , A [ i + 1 ] ) ) count ++ ; return count ; } int main ( ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << countSmithBrotherPairs ( A , N ) << endl ; return 0 ; }
Count pair sums that are factors of the sum of the array | C ++ program for the above approach ; Function to find the number of pairs whose sums divides the sum of array ; Initialize the totalSum and count as 0 ; Calculate the total sum of array ; Generate all possible pairs ; If the sum is a factor of totalSum or not ; Increment count by 1 ; Print the total count obtained ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int N ) { int count = 0 , totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } cout << count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Maximize average of the ratios of N pairs by M increments | C ++ program for the above approach ; Function to find the change in the ratio in pair after applying operation ; Stores the current ratio ; Stores the new ratio ; Stores the increase in ratio ; Returns the change ; Function to find the maximum average of the ratio of the pairs by applying M increments ; Stores the required result ; Declare a priority queue for storing the increments ; Store the increase in the ratio after applying one operation ; Push the increased value and index value in priority queue ; Store the ratio ; Update the value of sum ; Iterate while M > 0 ; Add the maximum change to the sum ; Remove the element from the priority queue ; Increase the pairs elements by 1 on which operation is applied ; Push the updated change of the pair in priority queue ; Decrease the operation count ; Update the value of the sum by dividing it by N ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double change ( int pass , int total ) { double currentPassRatio , newPassRatio ; double increase ; currentPassRatio = ( ( double ) pass ) / total ; newPassRatio = ( ( double ) ( pass + 1 ) ) / ( total + 1 ) ; increase = newPassRatio - currentPassRatio ; return increase ; } double maximumAverage ( vector < vector < int > > v , int M , int N ) { double sum = 0 ; double increase , average ; priority_queue < pair < double , int > > pq ; for ( int i = 0 ; i < N ; i ++ ) { increase = change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) ; pq . push ( { increase , i } ) ; average = v [ i ] [ 0 ] * 1.0 / v [ i ] [ 1 ] ; sum += average ; } while ( M > 0 ) { sum += pq . top ( ) . first ; int i = pq . top ( ) . second ; pq . pop ( ) ; v [ i ] [ 0 ] += 1 ; v [ i ] [ 1 ] += 1 ; pq . push ( { change ( v [ i ] [ 0 ] , v [ i ] [ 1 ] ) , i } ) ; M -- ; } double ans = sum / N ; return ans ; } int main ( ) { vector < vector < int > > V = { { 1 , 2 } , { 3 , 5 } , { 2 , 2 } } ; int M = 2 ; int N = V . size ( ) ; cout << maximumAverage ( V , M , N ) ; return 0 ; }
Print all numbers that are divisors of N and are co | C ++ program for the above approach ; Function to print all numbers that are divisors of N and are co - prime with the quotient of their division ; Iterate upto square root of N ; If divisors are equal and gcd is 1 , then print only one of them ; Otherwise print both ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnitaryDivisors ( int n ) { for ( int i = 1 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i && __gcd ( i , n / i ) == 1 ) { printf ( " % d ▁ " , i ) ; } else { if ( __gcd ( i , n / i ) == 1 ) { printf ( " % d ▁ % d ▁ " , i , n / i ) ; } } } } } int main ( ) { int N = 12 ; printUnitaryDivisors ( N ) ; return 0 ; }
Maximize K to make given array Palindrome when each element is replaced by its remainder with K | C ++ program for the above approach ; utility function to calculate the GCD of two numbers ; Function to calculate the largest K , replacing all elements of an array A by their modulus with K , makes A a palindromic array ; check if A is palindrome ; A is not palindromic ; K can be infitely large in this case ; variable to store the largest K that makes A palindromic ; return the required answer ; Driver code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } int largestK ( int A [ ] , int N ) { int l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return -1 ; int K = abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( int i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } int main ( ) { int A [ ] = { 1 , 2 , 3 , 2 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << largestK ( A , N ) << endl ; return 0 ; }
Check if N can be represented as sum of distinct powers of 3 | C ++ program for the above approach ; Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; print ; Driven Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool PermuteAndFind ( vector < long > power , int idx , long SumSoFar , int target ) { if ( idx == power . size ( ) ) { if ( SumSoFar == target ) return true ; return false ; } bool select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; bool notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return ( select notselect ) ; } void DistinctPowersOf3 ( int N ) { vector < long > power ( 16 ) ; power [ 0 ] = 1 ; for ( int i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; bool found = PermuteAndFind ( power , 0 , 0L , N ) ; if ( found == true ) { cout << " Yes " ; } else { cout << " No " ; } } int main ( ) { int N = 91 ; DistinctPowersOf3 ( N ) ; return 0 ; }
Check if N can be represented as sum of distinct powers of 3 | C ++ program for the above approach ; Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void DistinctPowersOf3 ( int N ) { while ( N > 0 ) { if ( N % 3 == 2 ) { cout << " No " ; return ; } N /= 3 ; } cout << " Yes " ; } int main ( ) { int N = 91 ; DistinctPowersOf3 ( N ) ; return 0 ; }
Generate an N | C ++ program for the above approach ; Function to print the permutation of size N with absolute difference of adjacent elements in range [ 2 , 4 ] ; If N is less than 4 ; Check if N is even ; Traverse through odd integers ; Update the value of i ; Traverse through even integers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void getPermutation ( int N ) { if ( N <= 3 ) { cout << -1 ; return ; } int i = N ; if ( N % 2 == 0 ) i -- ; while ( i >= 1 ) { cout << i << " ▁ " ; i -= 2 ; } cout << 4 << " ▁ " << 2 << " ▁ " ; i = 6 ; while ( i <= N ) { cout << i << " ▁ " ; i += 2 ; } } int main ( ) { int N = 9 ; getPermutation ( N ) ; return 0 ; }
Count of distinct values till C formed by adding or subtracting A , B , or 0 any number of times | C ++ program for the above approach ; Function to calculate gcd ; Function to find number of possible final values ; Find the gcd of two numbers ; Calculate number of distinct values ; Return values ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } int main ( ) { int A = 2 ; int B = 3 ; int C = 10 ; cout << ( getDistinctValues ( A , B , C ) ) ; return 0 ; }
Find array whose elements are XOR of adjacent elements in given array | C ++ implementation of the above approach ; Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print the array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to print arr [ ]
#include <iostream> NEW_LINE using namespace std ; int * game_with_number ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 10 , 11 , 1 , 2 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int * new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; }
Maximize score of same | C ++ program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Traverse the current subarray ; Finding the score without reversing the subarray ; Calculating the score of the reversed subarray ; Return the score of subarray ; Function to find the subarray with the maximum score ; Stores the maximum score and the starting and the ending point of subarray with maximum score ; Traverse all the subarrays ; Store the score of the current subarray ; Update the maximum score ; Print the maximum score ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int currSubArrayScore ( int * a , int * b , int l , int r ) { int straightScore = 0 ; int reverseScore = 0 ; for ( int i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return max ( straightScore , reverseScore ) ; } void maxScoreSubArray ( int * a , int * b , int n ) { int res = 0 , start = 0 , end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; start = i ; end = j ; } } } cout << res ; } int main ( ) { int A [ ] = { 13 , 4 , 5 } ; int B [ ] = { 10 , 22 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; maxScoreSubArray ( A , B , N ) ; return 0 ; }
Maximize score of same | C ++ program for the above approach ; Function to calculate the score of same - indexed subarrays selected from the arrays a [ ] and b [ ] ; Store the required result ; Iterate in the range [ 0 , N - 1 ] ; Consider the case of odd length subarray ; Update the maximum score ; Expanding the subarray in both directions with equal length so that mid point remains same ; Update both the scores ; Consider the case of even length subarray ; Update both the scores ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxScoreSubArray ( int * a , int * b , int n ) { int res = 0 ; for ( int mid = 0 ; mid < n ; mid ++ ) { int straightScore = a [ mid ] * b [ mid ] , reverseScore = a [ mid ] * a [ mid ] ; int prev = mid - 1 , next = mid + 1 ; res = max ( res , max ( straightScore , reverseScore ) ) ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = max ( res , max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } straightScore = 0 ; reverseScore = 0 ; prev = mid - 1 , next = mid ; while ( prev >= 0 && next < n ) { straightScore += ( a [ prev ] * b [ prev ] + a [ next ] * b [ next ] ) ; reverseScore += ( a [ prev ] * b [ next ] + a [ next ] * b [ prev ] ) ; res = max ( res , max ( straightScore , reverseScore ) ) ; prev -- ; next ++ ; } } cout << res ; } int main ( ) { int A [ ] = { 13 , 4 , 5 } ; int B [ ] = { 10 , 22 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; maxScoreSubArray ( A , B , N ) ; return 0 ; }
Length of the smallest subarray with maximum possible sum | C ++ program for the above approach ; Function to find the minimum length of the subarray whose sum is maximum ; Stores the starting and the ending index of the resultant subarray ; Traverse the array until a non - zero element is encountered ; If the array contains only of 0 s ; Traverse the array in reverse until a non - zero element is encountered ; Return the resultant size of the subarray ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSizeSubarray ( int arr [ ] , int N ) { int i = 0 , j = N - 1 ; while ( i < N and arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 and arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } int main ( ) { int arr [ ] = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumSizeSubarray ( arr , N ) ; return 0 ; }
Count ways to represent N as XOR of distinct integers not exceeding N | C ++ program for the above approach ; Function to count number of ways to represent N as the Bitwise XOR of distinct integers ; Count number of subsets using above - mentioned formula ; Print the resultant count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countXorPartition ( int N ) { double a = pow ( 2 , floor ( N - log ( N + 1 ) / log ( 2 ) ) ) ; cout << a ; } int main ( ) { int N = 5 ; countXorPartition ( N ) ; }
Count numbers less than N whose modulo with A is equal to B | C ++ program for the above approach ; Function to count numbers less than N , whose modulo with A gives B ; If the value of B at least A ; If the value of B is 0 or not ; Stores the resultant count of numbers less than N ; Update the value of ans ; Print the value of ans ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countValues ( int A , int B , int C ) { if ( B >= A ) { cout << 0 ; return ; } if ( B == 0 ) { cout << C / A ; return ; } int ans = C / A ; if ( ans * A + B <= C ) { ans ++ ; } cout << ans ; } int main ( ) { int A = 6 , B = 3 , N = 15 ; countValues ( A , B , N ) ; return 0 ; }
Reduce a number to 1 by performing given operations | Set 3 | C ++ program for the above approach ; Utility function to check if n is power of 2 ; Utility function to find highest power of 2 less than or equal to given number ; Recursive function to find steps needed to reduce a given integer to 1 ; Base Condition ; If the number is a power of 2 ; Else subtract the greatest power of 2 smaller than N from N ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int highestPowerof2 ( int n ) { int p = ( int ) log2 ( n ) ; return ( int ) pow ( 2 , p ) ; } bool isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( ceil ( log2 ( n ) ) == floor ( log2 ( n ) ) ) ; } int reduceToOne ( int N ) { if ( N == 1 ) { return 0 ; } if ( isPowerOfTwo ( N ) == true ) { return 1 + reduceToOne ( N / 2 ) ; } else { return 1 + reduceToOne ( N - highestPowerof2 ( N ) ) ; } } int main ( ) { int N = 7 ; cout << reduceToOne ( N ) << endl ; }
Maximum sum of a subsequence whose Bitwise AND is non | C ++ program for the above approach ; Function to find the maximum sum of a subsequence whose Bitwise AND is non - zero ; Stores the resultant maximum sum of the subsequence ; Iterate over all the bits ; Stores the sum of array elements whose i - th bit is set ; Traverse the array elements ; If the bit is set , then add its value to the sum ; Update the resultant maximum sum ; Return the maximum sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int arr [ ] , int N ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] & ( 1 << bit ) ) { sum += arr [ i ] ; } } ans = max ( ans , sum ) ; } return ans ; } int main ( ) { int arr [ ] = { 5 , 4 , 1 , 7 , 11 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumSum ( arr , N ) ; return 0 ; }
Check if an array can be reduced to at most length K by removal of distinct elements | C ++ program for the above approach ; Function to check if it is possible to reduce the size of the array to K by removing the set of the distinct array elements ; Stores all distinct elements present in the array arr [ ] ; Traverse the given array ; Insert array elements into the set ; Condition for reducing size of the array to at most K ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxCount ( int arr [ ] , int N , int K ) { set < int > st ; for ( int i = 0 ; i < N ; i ++ ) { st . insert ( arr [ i ] ) ; } if ( N - st . size ( ) <= K ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { int arr [ ] = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maxCount ( arr , N , K ) ; return 0 ; }
Maximum element present in the array after performing queries to add K to range of indices [ L , R ] | C ++ program for the above approach ; Function to find the max sum after processing q queries ; Store the cumulative sum ; Store the maximum sum ; Iterate over the range 0 to q ; Variables to extract values from vector ; Iterate over the range [ 1 , n ] ; Calculate cumulative sum ; Calculate maximum sum ; Return the maximum sum after q queries ; Driver code ; Stores the size of array and number of queries ; Stores the sum ; Storing input queries ; Function call to find the maximum sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max_sum ( int a [ ] , vector < pair < pair < int , int > , int > > v , int q , int n ) { int x = 0 ; int m = INT_MIN ; for ( int i = 0 ; i < q ; i ++ ) { int p , q , k ; p = v [ i ] . first . first ; q = v [ i ] . first . second ; k = v [ i ] . second ; a [ p ] += k ; if ( q + 1 <= n ) a [ q + 1 ] -= k ; } for ( int i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = max ( m , x ) ; } return m ; } int main ( ) { int n = 10 , q = 3 ; int a [ n + 5 ] = { 0 } ; vector < pair < pair < int , int > , int > > v ( q ) ; v [ 0 ] . first . first = 1 ; v [ 0 ] . first . second = 5 ; v [ 0 ] . second = 3 ; v [ 1 ] . first . first = 4 ; v [ 1 ] . first . second = 8 ; v [ 1 ] . second = 7 ; v [ 2 ] . first . first = 6 ; v [ 2 ] . first . second = 9 ; v [ 2 ] . second = 1 ; cout << max_sum ( a , v , q , n ) ; return 0 ; }