codeparrot/xlcost-text-to-code huggingface dataset:
Find Nth root of a number using Bisection method | C ++ program for above approach ; Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; double f ( double x , int p , double num ) { return pow ( x , p ) - num ; } double f_prime ( double x , int p ) { return p * pow ( x , p - 1 ) ; } double root ( double num , int p ) { double left = - num ; double right = num ; double x ; while ( true ) { x = ( left + right ) / 2.0 ; double value = f ( x , p , num ) ; double prime = f_prime ( x , p ) ; if ( value * prime <= 0 ) left = x ; else right = x ; if ( value < 0.000001 && value >= 0 ) { return x ; } } } int main ( ) { double P = 1234321 ; int N = 2 ; double ans = root ( P , N ) ; cout << ans ; }
Count of permutations of an Array having maximum MEXs sum of prefix arrays | C ++ program for the above approach ; To calculate the factorial ; To return the number of permutations of an array with maximum MEXs sum of prefix array ; Map to store the frequency of each element ; Running a loop from i = 0 to i < n ; If continuity breaks , then break the loop ; Considering choices available to be filled at this position , i . e . mp [ i ] ; Decrement the count of remaining right elements ; Adding all permutations of the elements present to the right of the point where continuity breaks . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int factorial ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } int countPermutations ( int ar [ ] , int n ) { unordered_map < int , int > mp ; int ans = 1 , cnt = n ; for ( int i = 0 ; i < n ; i ++ ) { mp [ ar [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp [ i ] == 0 ) { break ; } ans = ( ans * mp [ i ] ) ; cnt -- ; } ans = ans * factorial ( cnt ) ; return ans ; } int main ( ) { int arr [ ] = { 1 , 0 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPermutations ( arr , N ) ; }
Convert 0 to N by adding 1 or multiplying by 2 in minimum steps | C ++ program for above approach ; Function to count number of set bits in N ; Stores the count of set bits ; If N is odd , then it a set bit ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumAdditionOperation ( unsigned long long int N ) { int count = 0 ; while ( N ) { if ( N & 1 == 1 ) { count ++ ; } N = N >> 1 ; } return count ; } int main ( ) { int N = 6 ; cout << minimumAdditionOperation ( N ) ; return 0 ; }
Count of unordered pairs of semi | C ++ program of the above approach ; Stores the count of distinct prime number in factor of current number ; Function to return the vector of semi prime numbers in range [ 1 , N ] ; Count of distinct prime number in the factor of current number using Sieve of Eratosthenes ; If current number is prime ; Stores the semi prime numbers ; If p has 2 distinct prime factors ; Return vector ; Function to count unordered pairs of semi prime numbers with prime sum ; Stores the final count ; Loop to iterate over al the l unordered pairs ; If sum of current semi prime numbers is a prime number ; Return answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxn = 100000 ; int prime [ maxn ] = { } ; vector < int > semiPrimes ( int N ) { for ( int p = 2 ; p <= maxn ; p ++ ) { if ( prime [ p ] == 0 ) { for ( int i = 2 * p ; i <= maxn ; i += p ) prime [ i ] ++ ; } } vector < int > sPrimes ; for ( int p = 2 ; p <= N ; p ++ ) if ( prime [ p ] == 2 ) sPrimes . push_back ( p ) ; return sPrimes ; } int countPairs ( vector < int > semiPrimes ) { int cnt = 0 ; for ( int i = 0 ; i < semiPrimes . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < semiPrimes . size ( ) ; j ++ ) { if ( prime [ semiPrimes [ i ] + semiPrimes [ j ] ] == 0 ) { cnt ++ ; } } } return cnt ; } int main ( ) { int N = 100 ; cout << countPairs ( semiPrimes ( N ) ) ; return 0 ; }
Minimum divide by 2 operations required to make GCD odd for given Array | C ++ program for the above approach ; Function to find the minimum number of operations to make the GCD of the array odd ; Stores the minimum operations required ; Stores the powers of two for the current array element ; Dividing by 2 ; Increment the count ; Update the minimum operation required ; Return the result required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumOperations ( int arr [ ] , int N ) { int mini = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; count ++ ; } if ( mini > count ) { mini = count ; } } return mini ; } int main ( ) { int arr [ ] = { 4 , 6 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumOperations ( arr , N ) ; return 0 ; }
Minimum size of the array with MEX as A and XOR of the array elements as B | C ++ program for the above approach ; Function to find the minimum size of array with given MEX and XOR ; Find the XOR of values from 0 to A - 1 ; If A is a multiple of 4 ; If A % 4 gives remainder 1 ; If A % 4 gives remainder 2 ; Initializing array size by A ; If XOR of all values of array is equal to B ; If the required integer to be added is equal to A ; Else any integer can be added ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumSizeArr ( int A , int B ) { int currXor = 0 ; int reminder = ( A - 1 ) % 4 ; if ( reminder == 0 ) currXor = A - 1 ; else if ( reminder == 1 ) currXor = 1 ; else if ( reminder == 2 ) currXor = A ; int minSize = A ; if ( currXor == B ) return minSize ; else if ( currXor ^ B == A ) return minSize + 2 ; else return minSize + 1 ; } int main ( ) { int A = 1 ; int B = 999 ; cout << minimumSizeArr ( A , B ) ; return 0 ; }
Count of triplets till N whose product is at most N | C ++ program for the above approach ; Function to find number of triplets ( A , B , C ) having A * B * C <= N ; Stores the count of triplets ; Iterate a loop fixing the value of A ; Iterate a loop fixing the value of A ; Find the total count of triplets and add it to cnt ; Return the total triplets formed ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int N ) { int cnt = 0 ; for ( int A = 1 ; A <= N ; ++ A ) { for ( int B = 1 ; B <= N / A ; ++ B ) { cnt += N / ( A * B ) ; } } return cnt ; } int main ( ) { int N = 2 ; cout << countTriplets ( N ) ; return 0 ; }
Largest integer upto N having greatest prime factor greater than its square root | C ++ program for the above approach ; Stores the Greatest Prime Factor ; Modified Sieve to find the Greatest Prime Factor of all integers in the range [ 1 , maxn ] ; Initialize the array with 0 ; Iterate through all values of i ; If i is not a prime number ; Update the multiples of i ; Function to find integer in the range [ 1 , N ] such that its Greatest Prime factor is greater than its square root ; Iterate through all values of i in the range [ N , 1 ] ; If greatest prime factor of i is greater than its sqare root ; Return answer ; If no valid integer exist ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int maxn = 100001 ; int gpf [ maxn ] ; void modifiedSieve ( ) { memset ( gpf , 0 , sizeof ( gpf ) ) ; gpf [ 0 ] = 0 ; gpf [ 1 ] = 1 ; for ( int i = 2 ; i < maxn ; i ++ ) { if ( gpf [ i ] > 0 ) continue ; for ( int j = i ; j < maxn ; j += i ) { gpf [ j ] = max ( i , gpf [ j ] ) ; } } } int greatestValidInt ( int N ) { modifiedSieve ( ) ; for ( int i = N ; i > 0 ; i -- ) { if ( gpf [ i ] > sqrt ( i ) ) { return i ; } } return -1 ; } int main ( ) { int N = 25 ; cout << greatestValidInt ( N ) ; return 0 ; }
Count of pass required to visit same index again by moving arr [ i ] to index arr [ i ] | C ++ program for the above approach ; Function to find the number of moves required to visit the same index again for every array element ; Make given array 0 index based ; Stores the number of moves ; Store index value ; Update the value of cnt ; Make a pass ; Print the value of cnt ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void numberOfPasses ( vector < int > arr , int N ) { for ( int i = 0 ; i < N ; ++ i ) { -- arr [ i ] ; } for ( int i = 0 ; i < N ; ++ i ) { int cnt = 0 ; int k = i ; do { ++ cnt ; k = arr [ k ] ; } while ( k != i ) ; cout << cnt << " ▁ " ; } } int main ( ) { vector < int > arr { 4 , 6 , 2 , 1 , 5 , 3 } ; int N = arr . size ( ) ; numberOfPasses ( arr , N ) ; return 0 ; }
Find subfactorial of a number | / C ++ program for the above approach ; Function to find the subfactorial of the number ; Initialize variables ; Iterating over range N ; Fact variable store factorial of the i ; If count is even ; Increase the value of count by 1 ; Driver Code
#include <iostream> NEW_LINE using namespace std ; double subfactorial ( int N ) { double res = 0 , fact = 1 ; int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = fact * i ; if ( count % 2 == 0 ) res = res - ( 1 / fact ) ; else res = res + ( 1 / fact ) ; count ++ ; } return fact * ( 1 + res ) ; } int main ( ) { int N = 4 ; cout << subfactorial ( N ) ; return 0 ; }
Minimize operations to delete all elements of permutation A by removing a subsequence having order as array B | C ++ program for the above approach ; Function to find the minimum number of operations to delete all elements of permutation A in order described by B ; Stores the count of operations ; Stores the index of current integer in B to be deleted ; Loop to iterate over all values of B ; Stores the current index in A ; Iterate over all values A ; If current integer of B and A equal , increment the index of the current integer of B ; As the permutation A has been traversed completelly , increment the count of operations by 1 ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int A [ ] , int B [ ] , int N ) { int cnt = 0 ; int i = 0 ; while ( i < N ) { int j = 0 ; while ( j < N && i < N ) { if ( B [ i ] == A [ j ] ) { i ++ ; } j ++ ; } cnt ++ ; } return cnt ; } int main ( ) { int A [ ] = { 2 , 4 , 6 , 1 , 5 , 3 } ; int B [ ] = { 6 , 5 , 4 , 2 , 3 , 1 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << minOperations ( A , B , N ) ; return 0 ; }
Count of pair of integers ( x , y ) such that difference between square of x and y is a perfect square | C ++ program for the above approach ; Function to find number of pairs ( x , y ) such that x ^ 2 - y is a square number ; Stores the count of total pairs ; Iterate q value 1 to sqrt ( N ) ; Maximum possible value of p is min ( 2 * N - q , N / q ) ; P must be greater than or equal to q ; Total number of pairs are ; Adding all valid pairs to res ; Return total no of pairs ( x , y ) ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int N ) { int res = 0 ; for ( int q = 1 ; q * q <= N ; q ++ ) { int maxP = min ( 2 * N - q , N / q ) ; if ( maxP < q ) continue ; int cnt = maxP - q + 1 ; res += ( cnt / 2 + ( cnt & 1 ) ) ; } return res ; } int main ( ) { int N = 3 ; cout << countPairs ( N ) ; return 0 ; }
Maximum size of subset such that product of all subset elements is a factor of N | C ++ program for the above approach ; Function to find the maximum size of the subset such that the product of subset elements is a factor of N ; Base Case ; Stores maximum size of valid subset ; Traverse the given array ; If N % arr [ i ] = 0 , include arr [ i ] in a subset and recursively call for the remaining array integers ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximizeSubset ( int N , int arr [ ] , int M , int x = 0 ) { if ( x == M ) { return 0 ; } int ans = 0 ; for ( int i = x ; i < M ; i ++ ) { if ( N % arr [ i ] == 0 ) { ans = max ( ans , maximizeSubset ( N / arr [ i ] , arr , M , x + 1 ) + 1 ) ; } } return ans ; } int main ( ) { int N = 64 ; int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 32 } ; int M = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maximizeSubset ( N , arr , M ) ; return 0 ; }
Count of distinct sums formed by N numbers taken form range [ L , R ] | C ++ program for the above approach ; Function to find total number of different sums of N numbers in the range [ L , R ] ; To store minimum possible sum with N numbers with all as L ; To store maximum possible sum with N numbers with all as R ; All other numbers in between maxSum and minSum can also be formed so numbers in this range is the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDistinctSums ( int N , int L , int R ) { int minSum = L * N ; int maxSum = R * N ; return maxSum - minSum + 1 ; } int main ( ) { int N = 2 , L = 1 , R = 3 ; cout << countDistinctSums ( N , L , R ) ; return 0 ; }
Make the Array sum 0 by using either ceil or floor on each element | C ++ program for the above approach ; Function to modify the array element such that the sum is close to 0 ; Stores the modified elements ; Stores the sum of array element ; Stores minimum size of the array ; Traverse the array and find the sum ; If sum is positive ; Find the minimum number of elements that must be changed ; Iterate until M elements are modified or the array end ; Update the current array elements to its floor ; Print the resultant array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void setSumtoZero ( double arr [ ] , int N ) { int A [ N ] ; int sum = 0 ; int m = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { sum += ceil ( arr [ i ] ) ; A [ i ] = ceil ( arr [ i ] ) ; } if ( sum > 0 ) { m = min ( sum , N ) ; for ( int i = 0 ; i < N && m > 0 ; i ++ ) { A [ i ] = floor ( arr [ i ] ) ; if ( A [ i ] != floor ( arr [ i ] ) ) m -- ; } } for ( int i = 0 ; i < N ; i ++ ) { cout << A [ i ] << " ▁ " ; } } int main ( ) { double arr [ ] = { -2 , -2 , 4.5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; setSumtoZero ( arr , N ) ; return 0 ; }
Find first undeleted integer from K to N in given unconnected Graph after performing Q queries | C ++ program for the above approach ; Function to perform th Get operation of disjoint set union ; Function to perform the union operation of dijoint set union ; Update the graph [ a ] as b ; Function to perform given queries on set of vertices initially not connected ; Stores the vertices ; Mark every vertices rightmost vertex as i ; Traverse the queries array ; Check if it is first type of the givan query ; Get the parent of a ; Print the answer for the second query ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Get ( vector < int > & graph , int a ) { return graph [ a ] = ( graph [ a ] == a ? a : Get ( graph , graph [ a ] ) ) ; } void Union ( vector < int > & graph , int a , int b ) { a = Get ( graph , a ) ; b = Get ( graph , b ) ; graph [ a ] = b ; } void Queries ( vector < pair < int , int > > & queries , int N , int M ) { vector < int > graph ( N + 2 ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { graph [ i ] = i ; } for ( auto query : queries ) { if ( query . first == 1 ) { Union ( graph , query . second , query . second + 1 ) ; } else { int a = Get ( graph , query . second ) ; if ( a == N + 1 ) cout << -1 << " ▁ " ; else cout << graph [ a ] << " ▁ " ; } } } int main ( ) { int N = 5 ; vector < pair < int , int > > queries { { 2 , 1 } , { 1 , 1 } , { 2 , 1 } , { 2 , 3 } } ; int Q = queries . size ( ) ; Queries ( queries , N , Q ) ; return 0 ; }
Finding the Nth term in a sequence formed by removing digit K from natural numbers | C ++ implementation for the above approach ; Denotes the digit place ; Method to convert any number to binary equivalent ; denotes the current digits place ; If current digit is >= K increment its value by 1 ; Else add the digit as it is ; Move to the next digit ; Driver code
#include <iostream> NEW_LINE using namespace std ; long long convertToBase9 ( long long n ) { long long ans = 0 ; long long a = 1 ; while ( n > 0 ) { ans += ( a * ( n % 9 ) ) ; a *= 10 ; n /= 9 ; } return ans ; } long long getNthnumber ( long long base9 , long long K ) { long long ans = 0 ; long long a = 1 ; while ( base9 > 0 ) { int cur = base9 % 10 ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 /= 10 ; a *= 10 ; } return ans ; } int main ( ) { long long N = 10 , K = 1 ; long long base9 = convertToBase9 ( N ) ; cout << getNthnumber ( base9 , K ) ; return 0 ; }
Check if given array can be rearranged such that mean is equal to median | C ++ program for the above approach ; Function to return true or false if size of array is odd ; To prevent overflow ; If element is greater than mid , then it can only be present in right subarray ; If element is smaller than mid , then it can only be present in left subarray ; Else the element is present at the middle then return 1 ; when element is not present in array then return 0 ; Function to return true or false if size of array is even ; Calculating Candidate Median ; If Candidate Median if greater than Mean then decrement j ; If Candidate Median if less than Mean then increment i ; If Candidate Median if equal to Mean then return 1 ; when No candidate found for mean ; Function to return true if Mean can be equal to any candidate median otherwise return false ; Calculating Mean ; If N is Odd ; If N is even ; Driver Code
#include <iostream> NEW_LINE using namespace std ; bool binarySearch ( float arr [ ] , int size , float key ) { int low = 0 , high = size - 1 , mid ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( key > arr [ mid ] ) low = mid + 1 ; else if ( key < arr [ mid ] ) high = mid - 1 ; else return 1 ; } return 0 ; } bool twoPointers ( float arr [ ] , int N , float mean ) { int i = 0 , j = N - 1 ; while ( i < j ) { float temp = ( arr [ i ] + arr [ j ] ) / 2 ; if ( temp > mean ) j -- ; else if ( temp < mean ) i ++ ; else return 1 ; } return 0 ; } bool checkArray ( float arr [ ] , int N ) { float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; float mean = sum / N ; if ( N & 1 ) return binarySearch ( arr , N , mean ) ; else return twoPointers ( arr , N , mean ) ; } int main ( ) { float arr [ ] = { 1.0 , 3.0 , 6.0 , 9.0 , 12.0 , 32.0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkArray ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Count of distinct integers belonging to first N terms of at least one of given GPs | C ++ program for the above approach ; Function to find the count of distinct integers that belong to the first N terms of at least one of them is GP ; Stores the integers that occur in GPs in a set data - structure ; Stores the current integer of the first GP ; Iterate first N terms of first GP ; Insert the ith term of GP in S ; Stores the current integer of the second GP ; Iterate first N terms of second GP ; Return Answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int UniqueGeometricTerms ( int N , int a1 , int r1 , int a2 , int r2 ) { set < int > S ; long long p1 = a1 ; for ( int i = 0 ; i < N ; i ++ ) { S . insert ( p1 ) ; p1 = ( long long ) ( p1 * r1 ) ; } long long p2 = a2 ; for ( int i = 0 ; i < N ; i ++ ) { S . insert ( p2 ) ; p2 = ( long long ) ( p2 * r2 ) ; } return S . size ( ) ; } int main ( ) { int N = 5 ; int a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; cout << UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ; return 0 ; }
Minimize operations to make all array elements | C ++ program for the above approach ; Function to find minimum the number of operations required to make all the array elements to - 1 ; Stores the array elements with their corresponding indices ; Push the array element and it 's index ; Sort the elements according to it 's first value ; Stores the minimum number of operations required ; Traverse until vp is not empty ; Stores the first value of vp ; Stores the second value of vp ; Update the minCnt ; Pop the back element from the vp until the first value is same as val and difference between indices is less than K ; Return the minCnt ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int arr [ ] , int N , int K ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < N ; i ++ ) { vp . push_back ( { arr [ i ] , i } ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; int minCnt = 0 ; while ( ! vp . empty ( ) ) { int val , ind ; val = vp . back ( ) . first ; ind = vp . back ( ) . second ; minCnt ++ ; while ( ! vp . empty ( ) && vp . back ( ) . first == val && ind - vp . back ( ) . second + 1 <= K ) vp . pop_back ( ) ; } return minCnt ; } int main ( ) { int arr [ ] = { 18 , 11 , 18 , 11 , 18 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minOperations ( arr , N , K ) ; return 0 ; }
Find array sum after incrementing by K adjacent elements of every positive element M times | C ++ program for the above approach ; Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array arr [ ] ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void nearestLeft ( int arr [ ] , int N , vector < int > & steps ) { int L = - N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } void nearestRight ( int arr [ ] , int N , vector < int > & steps ) { int R = 2 * N ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = min ( steps [ i ] , R - i ) ; } } int findSum ( int arr [ ] , int N , int M , int K ) { vector < int > steps ( N ) ; int sum = accumulate ( arr , arr + N , 0 ) ; if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( int i = 0 ; i < N ; i ++ ) sum += 2 * K * max ( 0 , M - steps [ i ] ) ; return sum ; } int main ( ) { int arr [ ] = { 0 , 1 , 0 , 1 , 0 , 0 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = 2 ; int K = 1 ; cout << findSum ( arr , N , M , K ) ; return 0 ; }