codeparrot/xlcost-text-to-code huggingface dataset:
Search for an element in a Mountain Array | CPP program for the above approach ; Function to find the index of the peak element in the array ; Stores left most index in which the peak element can be found ; Stores right most index in which the peak element can be found ; Stores mid of left and right ; If element at mid is less than element at ( mid + 1 ) ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; If X is greater than mid ; Update left ; Update right ; Function to perform binary search in an a subarray if elements of the subarray are in an ascending order ; Stores mid of left and right ; If X found at mid ; Update right ; Update left ; Function to find the smallest index of X ; Stores index of peak element in array ; Stores index of X in the array ; If X greater than or equal to first element of array and less than the peak element ; Update res ; If element not found on left side of peak element ; Update res ; Print res ; Driver Code ; Given X ; Given array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findPeak ( vector < int > arr ) { int left = 0 ; int right = arr . size ( ) - 1 ; while ( left < right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ mid ] < arr [ mid + 1 ] ) { left = mid + 1 ; } else { right = mid ; } } return left ; } int BS ( int X , int left , int right , vector < int > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ mid ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { left = mid + 1 ; } else { right = mid - 1 ; } } return -1 ; } int reverseBS ( int X , int left , int right , vector < int > arr ) { while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( arr [ mid ] == X ) { return mid ; } else if ( X > arr [ mid ] ) { right = mid - 1 ; } else { left = mid + 1 ; } } return -1 ; } void findInMA ( int X , vector < int > mountainArr ) { int peakIndex = findPeak ( mountainArr ) ; int res = -1 ; if ( X >= mountainArr [ 0 ] && X <= mountainArr [ peakIndex ] ) { res = BS ( X , 0 , peakIndex , mountainArr ) ; } if ( res == -1 ) { res = reverseBS ( X , peakIndex + 1 , mountainArr . size ( ) - 1 , mountainArr ) ; } cout << res << endl ; } int main ( ) { int X = 3 ; vector < int > list { 1 , 2 , 3 , 4 , 5 , 3 , 1 } ; findInMA ( X , list ) ; }
Rearrange given array to obtain positive prefix sums at exactly X indices | C ++ program for the above approach ; Function to rearrange the array according to the given condition ; Using 2 nd operation making all values positive ; Sort the array ; Assign K = N - K ; Count number of zeros ; If number of zeros if greater ; Using 2 nd operation convert it into one negative ; Using 2 nd operation convert it into one negative ; Print the array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrange ( int * a , int n , int x ) { for ( int i = 0 ; i < n ; i ++ ) a [ i ] = abs ( a [ i ] ) ; sort ( a , a + n ) ; x = n - x ; int z = count ( a , a + n , 0 ) ; if ( x > n - z ) { cout << " - 1 STRNEWLINE " ; return ; } for ( int i = 0 ; i < n && x > 0 ; i += 2 ) { a [ i ] = - a [ i ] ; x -- ; } for ( int i = n - 1 ; i >= 0 && x > 0 ; i -- ) { if ( a [ i ] > 0 ) { a [ i ] = - a [ i ] ; x -- ; } } for ( int i = 0 ; i < n ; i ++ ) { cout << a [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 0 , -2 , 4 , 5 , -3 } ; int K = 3 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrange ( arr , N , K ) ; return 0 ; }
Maximize product of a strictly increasing or decreasing subarray | C ++ program to implement the above approach ; Function to find the maximum product of subarray in the array , arr [ ] ; Maximum positive product ending at the i - th index ; Minimum negative product ending at the current index ; Maximum product up to i - th index ; Check if an array element is positive or not ; Traverse the array ; If current element is positive ; Update max_ending_here ; Update min_ending_here ; Update flag ; If current element is 0 , reset the start index of subarray ; Update max_ending_here ; Update min_ending_here ; If current element is negative ; Stores max_ending_here ; Update max_ending_here ; Update min_ending_here ; Update max_so_far , if needed ; If no array elements is positive and max_so_far is 0 ; Function to find the maximum product of either increasing subarray or the decreasing subarray ; Stores start index of either increasing subarray or the decreasing subarray ; Initially assume maxProd to be 1 ; Traverse the array ; Store the longest either increasing subarray or the decreasing subarray whose start index is i ; Check for increasing subarray ; Insert elements of increasing subarray ; Check for decreasing subarray ; Insert elements of decreasing subarray ; Stores maximum subarray product of current increasing or decreasing subarray ; Update maxProd ; Update i ; Finally print maxProd ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubarrayProduct ( vector < int > arr , int n ) { int max_ending_here = 1 ; int min_ending_here = 1 ; int max_so_far = 0 ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { max_ending_here = max_ending_here * arr [ i ] ; min_ending_here = min ( min_ending_here * arr [ i ] , 1 ) ; flag = 1 ; } else if ( arr [ i ] == 0 ) { max_ending_here = 1 ; min_ending_here = 1 ; } else { int temp = max_ending_here ; max_ending_here = max ( min_ending_here * arr [ i ] , 1 ) ; min_ending_here = temp * arr [ i ] ; } if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } if ( flag == 0 && max_so_far == 0 ) return 0 ; return max_so_far ; } int findMaxProduct ( int * a , int n ) { int i = 0 ; int maxProd = -1e9 ; while ( i < n ) { vector < int > v ; v . push_back ( a [ i ] ) ; if ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] < a [ i + 1 ] ) { v . push_back ( a [ i + 1 ] ) ; i += 1 ; } } else if ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { while ( i < n - 1 && a [ i ] > a [ i + 1 ] ) { v . push_back ( a [ i + 1 ] ) ; i += 1 ; } } int prod = maxSubarrayProduct ( v , v . size ( ) ) ; maxProd = max ( maxProd , prod ) ; i ++ ; } return maxProd ; } int main ( ) { int arr [ ] = { 1 , 2 , 10 , 8 , 1 , 100 , 101 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxProduct ( arr , N ) ; return 0 ; }
Number of connected components of a graph ( using Disjoint Set Union ) | C ++ program for the above approach ; Stores the parent of each vertex ; Function to find the topmost parent of vertex a ; If current vertex is the topmost vertex ; Otherwise , set topmost vertex of its parent as its topmost vertex ; Function to connect the component having vertex a with the component having vertex b ; Connect edges ; Function to find unique top most parents ; Traverse all vertices ; Insert all topmost vertices obtained ; Print count of connected components ; Function to print answer ; Setting parent to itself ; Traverse all edges ; Print answer ; Driver Code ; Given N ; Given edges ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int parent [ 1000000 ] ; int root ( int a ) { if ( a == parent [ a ] ) { return a ; } return parent [ a ] = root ( parent [ a ] ) ; } void connect ( int a , int b ) { a = root ( a ) ; b = root ( b ) ; if ( a != b ) { parent [ b ] = a ; } } void connectedComponents ( int n ) { set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { s . insert ( root ( parent [ i ] ) ) ; } cout << s . size ( ) << ' ' ; } void printAnswer ( int N , vector < vector < int > > edges ) { for ( int i = 0 ; i <= N ; i ++ ) { parent [ i ] = i ; } for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { connect ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; } connectedComponents ( N ) ; } int main ( ) { int N = 8 ; vector < vector < int > > edges = { { 1 , 0 } , { 0 , 2 } , { 5 , 3 } , { 3 , 4 } , { 6 , 7 } } ; printAnswer ( N , edges ) ; return 0 ; }
Remove all zero | C ++ program for the above approach ; Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeZeroRowCol ( vector < vector < int > > & arr ) { int n = arr . size ( ) ; int col [ n + 1 ] = { 0 } ; int row [ n + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { col [ j ] += ( arr [ i ] [ j ] == 1 ) ; count += ( arr [ i ] [ j ] == 1 ) ; } row [ i ] = count ; } for ( int i = 0 ; i < n ; ++ i ) { if ( row [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < n ; ++ j ) { if ( col [ j ] != 0 ) cout << arr [ i ] [ j ] ; } cout << " STRNEWLINE " ; } } int main ( ) { vector < vector < int > > arr = { { 1 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; removeZeroRowCol ( arr ) ; return 0 ; }
Find a pair of overlapping intervals from a given Set | C ++ program to implement the above approach ; Function to find a pair ( i , j ) such that i - th interval lies within the j - th interval ; Store interval and index of the interval in the form of { { l , r } , index } ; Traverse the array , arr [ ] [ ] ; Stores l - value of the interval ; Stores r - value of the interval ; Push current interval and index into tup ; Sort the vector based on l - value of the intervals ; Stores r - value of current interval ; Stores index of current interval ; Traverse the vector , tup [ ] ; Stores l - value of previous interval ; Stores l - value of current interval ; If Q and R are equal ; Print the index of interval ; Stores r - value of current interval ; If T is less than or equal to curr ; Update curr ; Update currPos ; If such intervals found ; Driver Code ; Given l - value of segments ; Given r - value of segments ; Given size ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findOverlapSegement ( int N , int a [ ] , int b [ ] ) { vector < pair < pair < int , int > , int > > tup ; for ( int i = 0 ; i < N ; i ++ ) { int x , y ; x = a [ i ] ; y = b [ i ] ; tup . push_back ( pair < pair < int , int > , int > ( pair < int , int > ( x , y ) , i ) ) ; } sort ( tup . begin ( ) , tup . end ( ) ) ; int curr = tup [ 0 ] . first . second ; int currPos = tup [ 0 ] . second ; for ( int i = 1 ; i < N ; i ++ ) { int Q = tup [ i - 1 ] . first . first ; int R = tup [ i ] . first . first ; if ( Q == R ) { if ( tup [ i - 1 ] . first . second < tup [ i ] . first . second ) cout << tup [ i - 1 ] . second << ' ▁ ' << tup [ i ] . second ; else cout << tup [ i ] . second << ' ▁ ' << tup [ i - 1 ] . second ; return ; } int T = tup [ i ] . first . second ; if ( T <= curr ) { cout << tup [ i ] . second << ' ▁ ' << currPos ; return ; } else { curr = T ; currPos = tup [ i ] . second ; } } cout << " - 1 ▁ - 1" ; } int main ( ) { int a [ ] = { 1 , 2 , 3 , 2 , 2 } ; int b [ ] = { 5 , 10 , 10 , 2 , 15 } ; int N = sizeof ( a ) / sizeof ( int ) ; findOverlapSegement ( N , a , b ) ; }
Replace each node of a Binary Tree with the sum of all the nodes present in its diagonal | CPP program to implement the above approach ; Structure of a tree node ; Function to replace each node with the sum of nodes at the same diagonal ; IF root is NULL ; Replace nodes ; Traverse the left subtree ; Traverse the right subtree ; Function to find the sum of all the nodes at each diagonal of the tree ; If root is not NULL ; If update sum of nodes at current diagonal ; Traverse the left subtree ; Traverse the right subtree ; Function to print the nodes of the tree using level order traversal ; Stores node at each level of the tree ; Stores count of nodes at current level ; Stores front element of the queue ; Insert left subtree ; Insert right subtree ; Update length ; Driver Code ; Build tree ; Store sum of nodes at each diagonal of the tree ; Find sum of nodes at each diagonal of the tree ; Replace nodes with the sum of nodes at the same diagonal ; Print tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; struct TreeNode * left , * right ; TreeNode ( int x ) { val = x ; left = NULL ; right = NULL ; } } ; void replaceDiag ( TreeNode * root , int d , unordered_map < int , int > & diagMap ) { if ( ! root ) return ; root -> val = diagMap [ d ] ; replaceDiag ( root -> left , d + 1 , diagMap ) ; replaceDiag ( root -> right , d , diagMap ) ; } void getDiagSum ( TreeNode * root , int d , unordered_map < int , int > & diagMap ) { if ( ! root ) return ; if ( diagMap [ d ] > 0 ) diagMap [ d ] += root -> val ; else diagMap [ d ] = root -> val ; getDiagSum ( root -> left , d + 1 , diagMap ) ; getDiagSum ( root -> right , d , diagMap ) ; } void levelOrder ( TreeNode * root ) { queue < TreeNode * > q ; q . push ( root ) ; while ( true ) { int length = q . size ( ) ; if ( ! length ) break ; while ( length ) { auto temp = q . front ( ) ; q . pop ( ) ; cout << temp -> val << " ▁ " ; if ( temp -> left ) q . push ( temp -> left ) ; if ( temp -> right ) q . push ( temp -> right ) ; length -= 1 ; } } } int main ( ) { TreeNode * root = new TreeNode ( 5 ) ; root -> left = new TreeNode ( 6 ) ; root -> right = new TreeNode ( 3 ) ; root -> left -> left = new TreeNode ( 4 ) ; root -> left -> right = new TreeNode ( 9 ) ; root -> right -> right = new TreeNode ( 2 ) ; unordered_map < int , int > diagMap ; getDiagSum ( root , 0 , diagMap ) ; replaceDiag ( root , 0 , diagMap ) ; levelOrder ( root ) ; return 0 ; }
Count maximum concatenation of pairs from given array that are divisible by 3 | C ++ program to implement the above approach ; Function to count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Stores count pairs whose concatenation is divisible by 3 and each element can be present in at most one pair ; Check if an element present in any pair or not ; Generate all possible pairs ; If the element already present in a pair ; If the element already present in a pair ; If concatenation of elements is divisible by 3 ; Update ans ; Mark i is True ; Mark j is True ; Driver Code ; To display the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countDivBy3InArray ( int arr [ ] , int N ) { int ans = 0 ; bool taken [ N ] ; memset ( taken , false , sizeof ( taken ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( taken [ i ] == true ) { continue ; } for ( int j = i + 1 ; j < N ; j ++ ) { if ( taken [ j ] == true ) { continue ; } if ( stoi ( to_string ( arr [ i ] ) + to_string ( arr [ j ] ) ) % 3 == 0 || stoi ( to_string ( arr [ j ] ) + to_string ( arr [ i ] ) ) % 3 == 0 ) { ans += 1 ; taken [ i ] = true ; taken [ j ] = true ; } } } return ans ; } int main ( ) { int arr [ ] = { 5 , 3 , 2 , 8 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countDivBy3InArray ( arr , N ) ; return 0 ; }
Maximum average of subtree values in a given Binary Tree | C ++ program for the above approach ; Structure of the Tree node ; Stores the result ; Function for finding maximum subtree average ; Checks if current node is not NULL and doesn 't have any children ; Stores sum of its subtree in index 0 and count number of nodes in index 1 ; Traverse all children of the current node ; Recursively calculate max average of subtrees among its children ; Increment sum by sum of its child 's subtree ; Increment number of nodes by its child 's node ; Increment sum by current node 's value ; Increment number of nodes by one ; Take maximum of ans and current node 's average ; Finally return pair of { sum , count } ; Driver Code ; Given tree ; Function call ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct TreeNode { int val ; vector < TreeNode * > children ; TreeNode ( int v ) { val = v ; } } ; double ans = 0.0 ; vector < int > MaxAverage ( TreeNode * root ) { if ( root != NULL && root -> children . size ( ) == 0 ) { ans = max ( ans , ( root -> val ) * ( 1.0 ) ) ; return { root -> val , 1 } ; } vector < int > childResult ( 2 ) ; for ( TreeNode * child : root -> children ) { vector < int > childTotal = MaxAverage ( child ) ; childResult [ 0 ] = childResult [ 0 ] + childTotal [ 0 ] ; childResult [ 1 ] = childResult [ 1 ] + childTotal [ 1 ] ; } int sum = childResult [ 0 ] + root -> val ; int count = childResult [ 1 ] + 1 ; ans = max ( ans , sum / count * 1.0 ) ; return { sum , count } ; } int main ( ) { TreeNode * root = new TreeNode ( 20 ) ; TreeNode * left = new TreeNode ( 12 ) ; TreeNode * right = new TreeNode ( 18 ) ; root -> children . push_back ( left ) ; root -> children . push_back ( right ) ; left -> children . push_back ( new TreeNode ( 11 ) ) ; left -> children . push_back ( new TreeNode ( 3 ) ) ; right -> children . push_back ( new TreeNode ( 15 ) ) ; right -> children . push_back ( new TreeNode ( 8 ) ) ; MaxAverage ( root ) ; printf ( " % 0.1f STRNEWLINE " , ans ) ; }
Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | C ++ program for the above approach ; Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If true , print Yes ; Otherwise , print No ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canTransformStrings ( string A , string B ) { int n1 = A . size ( ) ; int n2 = B . size ( ) ; int count1A = 0 , count1B = 0 ; int odd1A = 0 , odd1B = 0 ; int even1A = 0 , even1B = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( A [ i ] == '1' ) count1A ++ ; else { if ( count1A & 1 ) odd1A ++ ; else even1A ++ ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( B [ i ] == '1' ) count1B ++ ; else { if ( count1B & 1 ) odd1B ++ ; else even1B ++ ; } } if ( count1A == count1B && odd1A == odd1B && even1A == even1B ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { string A = "10011" , B = "11100" ; canTransformStrings ( A , B ) ; return 0 ; }
Smallest element present in every subarray of all possible lengths | C ++ program of the above approach ; Function to print the common elements for all subarray lengths ; Function to find and store the minimum element present in all subarrays of all lengths from 1 to n ; Skip lengths for which answer [ i ] is - 1 ; Initialize minimum as the first element where answer [ i ] is not - 1 ; Updating the answer array ; If answer [ i ] is - 1 , then minimum can be substituted in that place ; Find minimum answer ; Function to find the minimum number corresponding to every subarray of length K , for every K from 1 to N ; Stores the minimum common elements for all subarray lengths ; Initialize with - 1. ; Find for every element , the minimum length such that the number is present in every subsequence of that particular length or more ; To store first occurence and gaps between occurences ; To cover the distance between last occurence and the end of the array ; To find the distance between any two occurences ; Update and store the answer ; Print the required answer ; Function to find the smallest element common in all subarrays for every possible subarray lengths ; Initializing indices array ; Store the numbers present in the array ; Push the index in the indices [ A [ i ] ] and also store the numbers in set to get the numbers present in input array ; Function call to calculate length of subarray for which a number is present in every subarray of that length ; Driver Code ; Given array ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printAnswer ( int answer [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { cout << answer [ i ] << " ▁ " ; } } void updateAnswerArray ( int answer [ ] , int N ) { int i = 0 ; while ( answer [ i ] == -1 ) i ++ ; int minimum = answer [ i ] ; while ( i <= N ) { if ( answer [ i ] == -1 ) answer [ i ] = minimum ; else answer [ i ] = min ( minimum , answer [ i ] ) ; minimum = min ( minimum , answer [ i ] ) ; i ++ ; } } void lengthOfSubarray ( vector < int > indices [ ] , set < int > st , int N ) { int answer [ N + 1 ] ; memset ( answer , -1 , sizeof ( answer ) ) ; for ( auto itr : st ) { int start = -1 ; int gap = -1 ; indices [ itr ] . push_back ( N ) ; for ( int i = 0 ; i < indices [ itr ] . size ( ) ; i ++ ) { gap = max ( gap , indices [ itr ] [ i ] - start ) ; start = indices [ itr ] [ i ] ; } if ( answer [ gap ] == -1 ) answer [ gap ] = itr ; } updateAnswerArray ( answer , N ) ; printAnswer ( answer , N ) ; } void smallestPresentNumber ( int arr [ ] , int N ) { vector < int > indices [ N + 1 ] ; set < int > elements ; for ( int i = 0 ; i < N ; i ++ ) { indices [ arr [ i ] ] . push_back ( i ) ; elements . insert ( arr [ i ] ) ; } lengthOfSubarray ( indices , elements , N ) ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 3 , 2 , 3 , 1 , 3 , 2 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; smallestPresentNumber ( arr , N ) ; return ( 0 ) ; }
Minimize remaining array element by removing pairs and replacing them by their absolute difference | C ++ program to implement the above approach ; Function to find the smallest element left in the array by the given operations ; Base Case ; If this subproblem has occurred previously ; Including i - th array element into the first subset ; If i - th array element is not selected ; Update dp [ i ] [ sum ] ; Utility function to find smallest element left in the array by the given operations ; Stores sum of the array elements ; Traverse the array ; Update total ; Stores overlapping subproblems ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int smallestLeft ( int arr [ ] , int total , int sum , int i , vector < vector < int > > & dp ) { if ( i == 0 ) { return abs ( total - 2 * sum ) ; } if ( dp [ i ] [ sum ] != -1 ) return dp [ i ] [ sum ] ; int X = smallestLeft ( arr , total , sum + arr [ i - 1 ] , i - 1 , dp ) ; int Y = smallestLeft ( arr , total , sum , i - 1 , dp ) ; return dp [ i ] [ sum ] = min ( X , Y ) ; } int UtilSmallestElement ( int arr [ ] , int N ) { int total = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total += arr [ i ] ; } vector < vector < int > > dp ( N + 1 , vector < int > ( total , -1 ) ) ; cout << smallestLeft ( arr , total , 0 , N , dp ) ; } int main ( ) { int arr [ ] = { 2 , 7 , 4 , 1 , 8 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; UtilSmallestElement ( arr , N ) ; return 0 ; }