codeparrot/xlcost-text-to-code huggingface dataset:
Reduce sum of same | C ++ program for the above approach ; Function to check if elements of B [ ] can be rearranged such that A [ i ] + B [ i ] <= X ; Checks the given condition ; Sort A [ ] in ascending order ; Sort B [ ] in descending order ; Traverse the arrays A [ ] and B [ ] ; If A [ i ] + B [ i ] exceeds X ; Rearrangement not possible , set flag to false ; If flag is true ; Otherwise ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrange ( int A [ ] , int B [ ] , int N , int X ) { bool flag = true ; sort ( A , A + N ) ; sort ( B , B + N , greater < int > ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ i ] > X ) { flag = false ; break ; } } if ( flag ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 1 , 2 } ; int X = 4 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; rearrange ( A , B , N , X ) ; return 0 ; }
Minimum increments or decrements by D required to make all array elements equal | C ++ program for the above approach ; Function to find minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Sort the array ; Traverse the array ; If difference between two consecutive elements are not divisible by D ; Store minimum count of operations required to make all array elements equal by incrementing or decrementing array elements by d ; Stores middle element of the array ; Traverse the array ; Update count ; Driver Code ; Given N & D ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numOperation ( int arr [ ] , int N , int D ) { sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( ( arr [ i + 1 ] - arr [ i ] ) % D != 0 ) { cout << " - 1" ; return ; } } int count = 0 ; int mid = arr [ N / 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { count += abs ( mid - arr [ i ] ) / D ; } cout << count ; } int main ( ) { int N = 4 , D = 2 ; int arr [ ] = { 2 , 4 , 6 , 8 } ; numOperation ( arr , N , D ) ; }
Maximize sum of second minimums of each K length partitions of the array | C ++ program for the above approach ; Function to find the maximum sum of second smallest of each partition of size K ; Sort the array A [ ] in ascending order ; Store the maximum sum of second smallest of each partition of size K ; Select every ( K - 1 ) th element as second smallest element ; Update sum ; Print the maximum sum ; Driver Code ; Given size of partitions ; Given array A [ ] ; Size of the given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSum ( int A [ ] , int N , int K ) { sort ( A , A + N ) ; int sum = 0 ; for ( int i = N / K ; i < N ; i += K - 1 ) { sum += A [ i ] ; } cout << sum ; } int main ( ) { int K = 4 ; int A [ ] = { 2 , 3 , 1 , 4 , 7 , 5 , 6 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; findSum ( A , N , K ) ; return 0 ; }
Minimize remaining array element by removing pairs and replacing them with their average | C ++ program to implement the above approach ; Function to find the smallest element left in the array by removing pairs and inserting their average ; Stores array elements such that the largest element present at top of PQ ; Traverse the array ; Insert arr [ i ] into PQ ; Iterate over elements of PQ while count of elements in PQ greater than 1 ; Stores largest element of PQ ; Pop the largest element of PQ ; Stores largest element of PQ ; Pop the largest element of PQ ; Insert the ceil value of average of top1 and top2 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSmallestNumLeft ( int arr [ ] , int N ) { priority_queue < int > PQ ; for ( int i = 0 ; i < N ; i ++ ) { PQ . push ( arr [ i ] ) ; } while ( PQ . size ( ) > 1 ) { int top1 = PQ . top ( ) ; PQ . pop ( ) ; int top2 = PQ . top ( ) ; PQ . pop ( ) ; PQ . push ( ( top1 + top2 + 1 ) / 2 ) ; } return PQ . top ( ) ; } int main ( ) { int arr [ ] = { 30 , 16 , 40 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findSmallestNumLeft ( arr , N ) ; return 0 ; }
Find the array element from indices not divisible by K having largest composite product of digits | C ++ program to implement the above approach ; Function to check if a number is a composite number or not ; Corner cases ; Check if number is divisible by 2 or 3 ; Check if number is a multiple of any other prime number ; Function to calculate the product of digits of a number ; Stores the product of digits ; Extract digits of a number ; Calculate product of digits ; Function to check if the product of digits of a number is a composite number or not ; Stores product of digits ; If product of digits is equal to 1 ; If product of digits is not prime ; Function to find the number with largest composite product of digits from the indices not divisible by k from the given array ; Traverse the array ; If index is divisible by k ; Check if product of digits is a composite number or not ; Sort the products ; Driver Code
#include <bits/stdc++.h> NEW_LINE #include <vector> NEW_LINE using namespace std ; bool isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } int digitProduct ( int number ) { int product = 1 ; while ( number > 0 ) { product *= ( number % 10 ) ; number /= 10 ; } return product ; } bool compositedigitProduct ( int num ) { int res = digitProduct ( num ) ; if ( res == 1 ) { return false ; } if ( isComposite ( res ) ) { return true ; } return false ; } int largestCompositeDigitProduct ( int a [ ] , int n , int k ) { vector < pair < int , int > > pq ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i % k ) == 0 ) { continue ; } if ( compositedigitProduct ( a [ i ] ) ) { int b = digitProduct ( a [ i ] ) ; pq . push_back ( make_pair ( b , a [ i ] ) ) ; } } sort ( pq . begin ( ) , pq . end ( ) ) ; return pq . back ( ) . second ; } int main ( ) { int arr [ ] = { 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; int ans = largestCompositeDigitProduct ( arr , n , k ) ; cout << ans << endl ; return 0 ; }
Split array into K | C ++ implementation for above approach ; Function to find the minimum sum of 2 nd smallest elements of each subset ; Sort the array ; Stores minimum sum of second elements of each subset ; Traverse first K 2 nd smallest elements ; Update their sum ; Print the sum ; Driver Code ; Given Array ; Given size of the array ; Given subset lengths
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimum ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int ans = 0 ; for ( int i = 1 ; i < 2 * ( N / K ) ; i += 2 ) { ans += arr [ i ] ; } cout << ans ; } int main ( ) { int arr [ ] = { 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; findMinimum ( arr , N , K ) ; return 0 ; }
Find two non | C ++ program for the above approach ; Function to check if two non - intersecting subarrays with equal sum exists or not ; Sort the given array ; Traverse the array ; Check for duplicate elements ; If no duplicate element is present in the array ; Driver Code ; Given array ; Size of array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSubarrays ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int i = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cout << " YES " << endl ; return ; } } cout << " NO " << endl ; } int main ( ) { int arr [ ] = { 4 , 3 , 0 , 1 , 2 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findSubarrays ( arr , N ) ; return 0 ; }
Queries to check if sweets of given type can be eaten on given day or not | C ++ program for the above approach ; Function to find queries to check if a sweet of given type can be eaten on a given day or not ; Stores pairs { priority , quantity } of each sweet - type ; Sorting the order of sweets in decreasing order of their priority ; Stores the window { min_days , max_days } for each sweet - type ; Variables to calculate the windows ; Traverse the array ; Calculating maximum and minimum number of days required to complete the sweets of the current type ; Creating the window and storing it ; Traversing the queries ; x : Type of sweet , y : Day ; Find the window for the sweet of type x ; If the given day lies within the window ; Driver Code ; Quantites of sweets of each type ; Priorites of each type sweet ; Maximum sweet of one type that can be eaten on a day ; Queries ; Calculating number of types
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sweetTypeOnGivenDay ( int a [ ] , int b [ ] , int n , int k , vector < pair < int , int > > & q ) { vector < pair < int , pair < int , int > > > v ; for ( int i = 0 ; i < n ; i ++ ) v . push_back ( { b [ i ] , { a [ i ] , i + 1 } } ) ; sort ( v . begin ( ) , v . end ( ) , greater < pair < int , pair < int , int > > > ( ) ) ; map < int , pair < int , int > > mp ; int lowerbound = 0 , upperbound = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int maxi_days = v [ i ] . second . first ; int mini_days = v [ i ] . second . first / k ; if ( v [ i ] . second . first % k != 0 ) mini_days ++ ; upperbound += maxi_days ; mp [ v [ i ] . second . second ] = { lowerbound , upperbound } ; lowerbound += mini_days ; } for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int x = q [ i ] . first , y = q [ i ] . second ; int e = mp [ x ] . first ; int f = mp [ x ] . second ; if ( y >= e && y <= f ) cout << " Yes " << " ▁ " ; else cout << " No " << " ▁ " ; } } int main ( ) { int A [ ] = { 6 , 3 , 7 , 5 , 2 } ; int B [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 3 ; vector < pair < int , int > > Queries = { { 4 , 4 } , { 3 , 16 } , { 2 , 7 } } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; sweetTypeOnGivenDay ( A , B , n , K , Queries ) ; }
Selection Sort VS Bubble Sort | C ++ program for the above approach ; Function for bubble sort ; Iterate from 1 to n - 1 ; Iterate from 0 to n - i - 1 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; void Bubble_Sort ( int arr [ ] , int n ) { bool flag ; for ( int i = 1 ; i < n ; ++ i ) { flag = false ; for ( int j = 0 ; j <= ( n - i - 1 ) ; ++ j ) { if ( arr [ j ] > arr [ j + 1 ] ) { swap ( arr [ j ] , arr [ j + 1 ] ) ; flag = true ; } } if ( flag == false ) break ; } } int main ( ) { int n = 5 ; int arr [ 5 ] = { 2 , 0 , 1 , 4 , 3 } ; Bubble_Sort ( arr , n ) ; cout << " The ▁ Sorted ▁ Array ▁ by ▁ using ▁ Bubble ▁ Sort ▁ is ▁ : ▁ " ; for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Print all array elements appearing more than N / K times | C ++ program to implement the above approach ; Function to print all array elements whose frequency is greater than N / K ; Sort the array , arr [ ] ; Traverse the array ; Stores frequency of arr [ i ] ; Traverse array elements which is equal to arr [ i ] ; Update cnt ; Update i ; If frequency of arr [ i ] is greater than ( N / K ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void NDivKWithFreq ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; for ( int i = 0 ; i < N ; ) { int cnt = 1 ; while ( ( i + 1 ) < N && arr [ i ] == arr [ i + 1 ] ) { cnt ++ ; i ++ ; } if ( cnt > ( N / K ) ) { cout << arr [ i ] << " ▁ " ; } i ++ ; } } int main ( ) { int arr [ ] = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; return 0 ; }
Sort M elements of given circular array starting from index K | C ++ program for the above approach ; Function to print the circular array ; Print the array ; Function to sort m elements of diven circular array starting from index k ; Traverse M elements ; Iterate from index k to k + m - 1 ; Check if the next element in the circular array is less than the current element ; Swap current element with the next element ; Print the circular array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printCircularArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { cout << arr [ i ] << " ▁ " ; } } void sortCircularArray ( int arr [ ] , int n , int k , int m ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = k ; j < k + m - 1 ; j ++ ) { if ( arr [ j % n ] > arr [ ( j + 1 ) % n ] ) { swap ( arr [ j % n ] , arr [ ( j + 1 ) % n ] ) ; } } } printCircularArray ( arr , n ) ; } int main ( ) { int arr [ ] = { 4 , 1 , 6 , 5 , 3 } ; int K = 2 , M = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortCircularArray ( arr , N , K , M ) ; return 0 ; }
Minimize cost required to complete all processes | C ++ program to implement the above approach ; Function to find minimum cost required to complete all the process ; Sort the array on descending order of Y ; Stores length of array ; Stores minimum cost required to complete all the process ; Stores minimum cost to initiate any process ; Traverse the array ; If minCostInit is less than the cost to initiate the process ; Update minCost ; Update minCostInit ; Update minCostInit ; Return minCost ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool func ( pair < int , int > i1 , pair < int , int > i2 ) { return ( i1 . first - i1 . second < i2 . first - i2 . second ) ; } int minimumCostReqToCompthePrcess ( vector < pair < int , int > > arr ) { sort ( arr . begin ( ) , arr . end ( ) , func ) ; int n = arr . size ( ) ; int minCost = 0 ; int minCostInit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] . second > minCostInit ) { minCost += ( arr [ i ] . second - minCostInit ) ; minCostInit = arr [ i ] . second ; } minCostInit -= arr [ i ] . first ; } return minCost ; } int main ( ) { vector < pair < int , int > > arr = { { 1 , 2 } , { 2 , 4 } , { 4 , 8 } } ; cout << ( minimumCostReqToCompthePrcess ( arr ) ) ; }
Split array into equal length subsets with maximum sum of Kth largest element of each subset | C ++ program to implement the above approach ; Function to find the maximum sum of Kth largest element of M equal length partition ; Stores sum of K_th largest element of equal length partitions ; If N is not divisible by M ; Stores length of each partition ; If K is greater than length of partition ; Sort array in descending porder ; Traverse the array ; Update maxSum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumKthLargestsumPart ( int arr [ ] , int N , int M , int K ) { int maxSum = 0 ; if ( N % M ) return -1 ; int sz = ( N / M ) ; if ( K > sz ) return -1 ; sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 1 ; i <= M ; i ++ ) { maxSum += arr [ i * K - 1 ] ; } return maxSum ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int M = 2 ; int K = 1 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximumKthLargestsumPart ( arr , N , M , K ) ; return 0 ; }
Minimize difference between maximum and minimum array elements by exactly K removals | C ++ program for the above approach ; Function to minimize the difference of the maximum and minimum array elements by removing K elements ; Base Condition ; Sort the array ; Initialize left and right pointers ; Iterate for K times ; Removing right element to reduce the difference ; Removing the left element to reduce the difference ; Print the minimum difference ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void minimumRange ( int arr [ ] , int N , int K ) { if ( K >= N ) { cout << 0 ; return ; } sort ( arr , arr + N ) ; int left = 0 , right = N - 1 , i ; for ( i = 0 ; i < K ; i ++ ) { if ( arr [ right - 1 ] - arr [ left ] < arr [ right ] - arr [ left + 1 ] ) right -- ; else left ++ ; } cout << arr [ right ] - arr [ left ] ; } int main ( ) { int arr [ ] = { 5 , 10 , 12 , 14 , 21 , 54 , 61 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 4 ; minimumRange ( arr , N , K ) ; return 0 ; }
Split array into K subsets to maximize sum of their second largest elements | C ++ program to implement the above approach ; Function to split array into K subsets having maximum sum of their second maximum elements ; Sort the array ; Stores the maximum possible sum of second maximums ; Add second maximum of current subset ; Proceed to the second maximum of next subset ; Print the maximum sum obtained ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void splitArray ( int arr [ ] , int n , int K ) { sort ( arr , arr + n ) ; int i = n - 1 ; int result = 0 ; while ( K -- ) { result += arr [ i - 1 ] ; i -= 2 ; } cout << result ; } int main ( ) { int arr [ ] = { 1 , 3 , 1 , 5 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; splitArray ( arr , N , K ) ; return 0 ; }
Count subsequences for every array element in which they are the maximum | C ++ program for the above approach ; Function to merge the subarrays arr [ l . . m ] and arr [ m + 1 , . . r ] based on indices [ ] ; If a [ indices [ l ] ] is less than a [ indices [ j ] ] , add indice [ l ] to temp ; Else add indices [ j ] ; Add remaining elements ; Add remainging elements ; Recursive function to divide the array into to parts ; Recursive call for elements before mid ; Recursive call for elements after mid ; Merge the two sorted arrays ; Function to find the number of subsequences for each element ; Sorting the indices according to array arr [ ] ; Array to store output numbers ; Initialize subseq ; B [ i ] is 2 ^ i ; Doubling the subsequences ; Print the final output , array B [ ] ; Driver Code ; Given array ; Given length ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void merge ( int * indices , int * a , int l , int mid , int r ) { int temp_ind [ r - l + 1 ] , j = mid + 1 ; int i = 0 , temp_l = l , k ; while ( l <= mid && j <= r ) { if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) temp_ind [ i ++ ] = indices [ l ++ ] ; else temp_ind [ i ++ ] = indices [ j ++ ] ; } while ( l <= mid ) temp_ind [ i ++ ] = indices [ l ++ ] ; while ( j <= r ) temp_ind [ i ++ ] = indices [ j ++ ] ; for ( k = 0 ; k < i ; k ++ ) indices [ temp_l ++ ] = temp_ind [ k ] ; } void divide ( int * indices , int * a , int l , int r ) { if ( l >= r ) return ; int mid = l / 2 + r / 2 ; divide ( indices , a , l , mid ) ; divide ( indices , a , mid + 1 , r ) ; merge ( indices , a , l , mid , r ) ; } void noOfSubsequences ( int arr [ ] , int N ) { int indices [ N ] , i ; for ( i = 0 ; i < N ; i ++ ) indices [ i ] = i ; divide ( indices , arr , 0 , N - 1 ) ; int B [ N ] ; int subseq = 1 ; for ( i = 0 ; i < N ; i ++ ) { B [ indices [ i ] ] = subseq ; subseq *= 2 ; } for ( i = 0 ; i < N ; i ++ ) cout << B [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 2 , 3 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; noOfSubsequences ( arr , N ) ; return 0 ; }
Find the order of execution of given N processes in Round Robin Scheduling | C ++ program for the above approach ; Function to sort the array order [ ] on the basis of the array freq [ ] ; If order [ i ] th is less than order [ temp ] th process ; Otherwise ; Add the left half to tempOrder [ ] ; Add right half to tempOrder [ ] ; Copy the tempOrder [ ] array to order [ ] array ; Utility function to sort the array order [ ] on the basis of freq [ ] ; Base Case ; Divide array into 2 parts ; Sort the left array ; Sort the right array ; Merge the sorted arrays ; Function to find the order of processes in which execution occurs ; Store the frequency ; Find elements in array freq [ ] ; Store the order of completion of processes ; Initialize order [ i ] as i ; Function Call to find the order of execution ; Print order of completion of processes ; Driver Code ; Burst Time of the processes ; Quantum Time ; Function Call
#include <iostream> NEW_LINE using namespace std ; void merge ( int * order , int * freq , int i , int mid , int j ) { int tempOrder [ j - i + 1 ] ; int temp = mid + 1 , index = -1 ; while ( i <= mid && temp <= j ) { if ( freq [ order [ i ] ] <= freq [ order [ temp ] ] ) { tempOrder [ ++ index ] = order [ i ++ ] ; } else { tempOrder [ ++ index ] = order [ temp ++ ] ; } } while ( i <= mid ) { tempOrder [ ++ index ] = order [ i ++ ] ; } while ( temp <= j ) { tempOrder [ ++ index ] = order [ temp ++ ] ; } for ( index ; index >= 0 ; index -- ) { order [ j -- ] = tempOrder [ index ] ; } } void divide ( int * order , int * freq , int i , int j ) { if ( i >= j ) return ; int mid = i / 2 + j / 2 ; divide ( order , freq , i , mid ) ; divide ( order , freq , mid + 1 , j ) ; merge ( order , freq , i , mid , j ) ; } void orderProcesses ( int A [ ] , int N , int q ) { int i = 0 ; int freq [ N ] ; for ( i = 0 ; i < N ; i ++ ) { freq [ i ] = ( A [ i ] / q ) + ( A [ i ] % q > 0 ) ; } int order [ 4 ] ; for ( i = 0 ; i < N ; i ++ ) { order [ i ] = i ; } divide ( order , freq , 0 , N - 1 ) ; for ( i = 0 ; i < N ; i ++ ) { cout << order [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 3 , 7 , 4 } ; int Q = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; orderProcesses ( arr , N , Q ) ; return 0 ; }
Maximize cost to empty given array by repetitively removing K array elements | C ++ program to implement the above approach ; Function to find the maximum cost to remove all array elements ; Stores maximum cost to remove array elements ; Sort array in ascending order ; Traverse the array ; Update maxCost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCostToRemove ( int arr [ ] , int N , int K ) { int maxCost = 0 ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N ; i += K ) { maxCost += arr [ i + 1 ] ; } return maxCost ; } int main ( ) { int arr [ ] = { 1 , 3 , 4 , 1 , 5 , 1 , 5 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 4 ; cout << maxCostToRemove ( arr , N , K ) ; }