codeparrot/xlcost-text-to-code huggingface dataset:
Check if the Left View of the given tree is sorted or not | C # implementation to check if the left view of the given tree is sorted or not ; Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; Queue to hold values ; Variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; Variable for initial level ; Checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code
using System ; using System . Collections . Generic ; class GFG { class node { public int val ; public node right , left ; } ; static node newnode ( int key ) { node temp = new node ( ) ; temp . val = key ; temp . right = null ; temp . left = null ; return temp ; } static void func ( node root ) { Queue < node > q = new Queue < node > ( ) ; bool t = true ; q . Enqueue ( root ) ; int i = - 1 , j = - 1 ; while ( q . Count != 0 ) { int h = q . Count ; while ( h > 0 ) { root = q . Peek ( ) ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . Enqueue ( root . left ) ; } if ( root . right != null ) { q . Enqueue ( root . right ) ; } h = h - 1 ; q . Dequeue ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) Console . Write ( " true " + " STRNEWLINE " ) ; else Console . Write ( " false " + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { node root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ; } }
Divide a sorted array in K parts with sum of difference of max and min minimized in each part | C # program to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Storing differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ; Driver code
using System ; class GFG { static int calculate_minimum_split ( int n , int [ ] a , int k ) { int [ ] p = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; Array . Sort ( p ) ; Array . Reverse ( p ) ; int min_sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; int res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } static void Main ( ) { int [ ] arr = { 4 , 8 , 15 , 16 , 23 , 42 } ; int k = 3 ; int n = arr . Length ; Console . Write ( calculate_minimum_split ( n , arr , k ) ) ; } }
Sort an Array of dates in ascending order using Custom Comparator | C # implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator ; Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int myCompare ( string date1 , string date2 ) { string day1 = date1 . Substring ( 0 , 2 ) ; string month1 = date1 . Substring ( 3 , 2 ) ; string year1 = date1 . Substring ( 6 , 4 ) ; string day2 = date2 . Substring ( 0 , 2 ) ; string month2 = date2 . Substring ( 3 , 2 ) ; string year2 = date2 . Substring ( 6 , 4 ) ; return string . Compare ( year1 , year2 ) ; return string . Compare ( month1 , month2 ) ; return string . Compare ( day1 , day2 ) ; } static void printDatesAscending ( List < string > arr ) { arr . Sort ( myCompare ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) Console . WriteLine ( arr [ i ] ) ; } static public void Main ( ) { List < string > arr = new List < string > ( ) ; arr . Add ( "25-08-1996" ) ; arr . Add ( "03-08-1970" ) ; arr . Add ( "09-04-1994" ) ; arr . Add ( "29-08-1996" ) ; arr . Add ( "14-02-1972" ) ; printDatesAscending ( arr ) ; } }
Sort an Array of Strings according to the number of Vowels in them | C # implementation of the approach ; Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( str [ i ] ) ) Check for vowel ; Function to sort the array according to the number of the vowels ; Vector to store the number of vowels with respective elements ; Inserting number of vowels with respective strings in the vector pair ; Sort the vector , this will sort the pair according to the number of vowels ; Print the sorted vector content ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool isVowel ( char ch ) { ch = char . ToUpper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } static int countVowels ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . Length ; i ++ ) ++ count ; return count ; } static void sortArr ( string [ ] arr , int n ) { List < Tuple < int , string > > vp = new List < Tuple < int , string > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . Add ( new Tuple < int , string > ( countVowels ( arr [ i ] ) , arr [ i ] ) ) ; } vp . Sort ( ) ; for ( int i = 0 ; i < vp . Count ; i ++ ) Console . Write ( vp [ i ] . Item2 + " ▁ " ) ; } static void Main ( ) { string [ ] arr = { " lmno " , " pqrst " , " aeiou " , " xyz " } ; int n = arr . Length ; sortArr ( arr , n ) ; } }
Find the minimum cost to cross the River | C # implementation of the approach ; Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
using System ; class GFG { static long minimumCost ( long [ ] price , int n ) { Array . Sort ( price ) ; long totalCost = 0 ; for ( int i = n - 1 ; i > 1 ; i -= 2 ) { if ( i == 2 ) { totalCost += price [ 2 ] + price [ 0 ] ; } else { long price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; long price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += Math . Min ( price_first , price_second ) ; } } if ( n == 1 ) { totalCost += price [ 0 ] ; } else { totalCost += price [ 1 ] ; } return totalCost ; } public static void Main ( ) { long [ ] price = { 30 , 40 , 60 , 70 } ; int n = price . Length ; Console . WriteLine ( minimumCost ( price , n ) ) ; } }
Minimize the sum of differences of consecutive elements after removing exactly K elements | C # implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; Driver function ; input values ; calling the required function ;
using System ; class GFG { static int N = 100 ; static int [ , ] dp = new int [ N , N ] ; static int [ , ] vis = new int [ N , N ] ; static int findSum ( int [ ] arr , int n , int k , int l , int r ) { if ( ( l ) + ( n - 1 - r ) == k ) return arr [ r ] - arr [ l ] ; if ( vis [ l , r ] == 1 ) return dp [ l , r ] ; vis [ l , r ] = 1 ; dp [ l , r ] = Math . Min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; return dp [ l , r ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findSum ( arr , n , k , 0 , n - 1 ) ) ; } }
Find Kth element in an array containing odd elements first and then even elements | C # implementation of the approach ; Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
using System ; class GFG { static int getNumber ( int n , int k ) { int pos ; if ( ( n % 2 ) == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } static public void Main ( ) { int n = 8 , k = 5 ; Console . Write ( getNumber ( n , k ) ) ; } }
All unique combinations whose sum equals to K | C # implementation of the approach ; Function to find all unique combination of given elements such that their sum is K ; If a unique combination is found ; For all other combinations ; Check if the sum exceeds K ; Check if it is repeated or not ; Take the element into the combination ; Recursive call ; Remove element from the combination ; Function to find all combination of the given elements ; Sort the given elements ; To store combination ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static void unique_combination ( int l , int sum , int K , List < int > local , List < int > A ) { if ( sum == K ) { Console . Write ( " { " ) ; for ( int i = 0 ; i < local . Count ; i ++ ) { if ( i != 0 ) Console . Write ( " ▁ " ) ; Console . Write ( local [ i ] ) ; if ( i != local . Count - 1 ) Console . Write ( " , ▁ " ) ; } Console . WriteLine ( " } " ) ; return ; } for ( int i = l ; i < A . Count ; i ++ ) { if ( sum + A [ i ] > K ) continue ; if ( i > l && A [ i ] == A [ i - 1 ] ) continue ; local . Add ( A [ i ] ) ; unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) ; local . RemoveAt ( local . Count - 1 ) ; } } static void Combination ( List < int > A , int K ) { A . Sort ( ) ; List < int > local = new List < int > ( ) ; unique_combination ( 0 , 0 , K , local , A ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 1 , 2 , 7 , 6 , 1 , 5 } ; List < int > A = new List < int > ( arr ) ; int K = 8 ; Combination ( A , K ) ; } }
Find a triplet in an array whose sum is closest to a given number | C # implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; Update the closestSum ; Return the closest sum found ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int solution ( ArrayList arr , int x ) { int closestSum = int . MaxValue ; for ( int i = 0 ; i < arr . Count ; i ++ ) { for ( int j = i + 1 ; j < arr . Count ; j ++ ) { for ( int k = j + 1 ; k < arr . Count ; k ++ ) { if ( Math . Abs ( x - closestSum ) > Math . Abs ( x - ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ) ) { closestSum = ( ( int ) arr [ i ] + ( int ) arr [ j ] + ( int ) arr [ k ] ) ; } } } } return closestSum ; } public static void Main ( string [ ] args ) { ArrayList arr = new ArrayList ( ) { - 1 , 2 , 1 , - 4 } ; int x = 1 ; Console . Write ( solution ( arr , x ) ) ; } }
Build original array from the given sub | C # implementation of the approach ; Function to add edge to graph ; Function to calculate indegrees of all the vertices ; If there is an edge from i to x then increment indegree of x ; Function to perform topological sort ; Push every node to the queue which has no incoming edge ; Since edge u is removed , update the indegrees of all the nodes which had an incoming edge from u ; Function to generate the array from the given sub - sequences ; Create the graph from the input sub - sequences ; Add edge between every two consecutive elements of the given sub - sequences ; Get the indegrees for all the vertices ; Get the topological order of the created graph ; Driver code ; Size of the required array ; Given sub - sequences of the array ; Get the resultant array as vector ; Printing the array
using System ; using System . Collections . Generic ; class GFG { static void addEdge ( List < List < int > > adj , int u , int v ) { adj [ u ] . Add ( v ) ; } static void getindeg ( List < List < int > > adj , int V , List < int > indeg ) { for ( int i = 0 ; i < V ; i ++ ) { foreach ( int x in adj [ i ] ) { indeg [ x ] ++ ; } } } static List < int > topo ( List < List < int > > adj , int V , List < int > indeg ) { Queue < int > q = new Queue < int > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) { q . Enqueue ( i ) ; } } List < int > res = new List < int > ( ) ; while ( q . Count > 0 ) { int u = q . Dequeue ( ) ; res . Add ( u ) ; foreach ( int x in adj [ u ] ) { indeg [ x ] -- ; if ( indeg [ x ] == 0 ) { q . Enqueue ( x ) ; } } } return res ; } static List < int > makearray ( List < List < int > > v , int V ) { List < List < int > > adj = new List < List < int > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = 0 ; j < v [ i ] . Count - 1 ; j ++ ) { addEdge ( adj , v [ i ] [ j ] , v [ i ] [ j + 1 ] ) ; } } List < int > indeg = new List < int > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { indeg . Add ( 0 ) ; } getindeg ( adj , V , indeg ) ; List < int > res = topo ( adj , V , indeg ) ; return res ; } static public void Main ( ) { int n = 10 ; List < List < int > > subseqs = new List < List < int > > ( ) ; subseqs . Add ( new List < int > ( ) { 9 , 1 , 2 , 8 , 3 } ) ; subseqs . Add ( new List < int > ( ) { 6 , 1 , 2 } ) ; subseqs . Add ( new List < int > ( ) { 9 , 6 , 3 , 4 } ) ; subseqs . Add ( new List < int > ( ) { 5 , 2 , 7 } ) ; subseqs . Add ( new List < int > ( ) { 0 , 9 , 5 , 4 } ) ; List < int > res = makearray ( subseqs , n ) ; foreach ( int x in res ) { Console . Write ( x + " ▁ " ) ; } } }
Sum of Semi | C # implementation of the above approach ; Vector to store the primes ; Create a boolean array " prime [ 0 . . n ] " ; Initialize along prime values to be true ; If prime [ p ] is not changed then it is a prime ; Update along multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked ; Print all prime numbers ; Function to return the semi - prime sum ; Variable to store the sum of semi - primes ; Iterate over the prime values ; Break the loop once the product exceeds N ; Add valid products which are less than or equal to N each product is a semi - prime number ; Driver code
using System ; using System . Collections . Generic ; class GFG { static List < long > pr = new List < long > ( ) ; static bool [ ] prime = new bool [ 10000000 + 1 ] ; static void sieve ( long n ) { for ( int i = 2 ; i <= n ; i += 1 ) { prime [ i ] = true ; } for ( int p = 2 ; ( int ) p * ( int ) p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = ( int ) p * ( int ) p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . Add ( ( long ) p ) ; } static long SemiPrimeSum ( long N ) { long ans = 0 ; for ( int i = 0 ; i < pr . Count ; i += 1 ) { for ( int j = i ; j < pr . Count ; j += 1 ) { if ( ( long ) pr [ i ] * ( long ) pr [ j ] > N ) break ; ans += ( long ) pr [ i ] * ( long ) pr [ j ] ; } } return ans ; } public static void Main ( String [ ] args ) { long N = 6 ; sieve ( N ) ; Console . WriteLine ( SemiPrimeSum ( N ) ) ; } }
Compress the array into Ranges | C # program to compress the array ranges ; Function to compress the array ranges ; start iteration from the ith array element ; loop until arr [ i + 1 ] == arr [ i ] and increment j ; if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element ; increment i for next iteration ; print the consecutive range found ; move i jump directly to j + 1 ; Driver code
using System ; class GFG { static void compressArr ( int [ ] arr , int n ) { int i = 0 , j = 0 ; Array . Sort ( arr ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { Console . Write ( arr [ i ] + " ▁ " ) ; i ++ ; } else { Console . Write ( arr [ i ] + " - " + arr [ j ] + " ▁ " ) ; i = j + 1 ; } } } public static void Main ( ) { int n = 7 ; int [ ] arr = { 1 , 3 , 4 , 5 , 6 , 9 , 10 } ; compressArr ( arr , n ) ; } }
Remove elements to make array sorted | C # implementation of the approach ; Function to sort the array by removing misplaced elements ; brr [ ] is used to store the sorted array elements ; Print the sorted array ; Driver code
using System ; class GFG { static void removeElements ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; int l = 1 ; brr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( brr [ l - 1 ] <= arr [ i ] ) { brr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) Console . Write ( brr [ i ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = arr . Length ; removeElements ( arr , n ) ; } }
Remove elements to make array sorted | C # implementation of the approach ; Function to sort the array by removing misplaced elements ; l stores the index ; Print the sorted array ; Driver code
using System ; class GFG { public static void removeElements ( int [ ] arr , int n ) { int l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ l - 1 ] <= arr [ i ] ) { arr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = arr . Length ; removeElements ( arr , n ) ; } }
Find number from its divisors | C # implementation of the approach ; Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of a number ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call
using System ; using System . Collections . Generic ; class GFG { static int findX ( int [ ] a , int n ) { Array . Sort ( a ) ; int x = a [ 0 ] * a [ n - 1 ] ; List < int > vec = new List < int > ( ) ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { vec . Add ( i ) ; if ( ( x / i ) != i ) vec . Add ( x / i ) ; } } vec . Sort ( ) ; if ( vec . Count != n ) { return - 1 ; } else { int i = 0 ; foreach ( int it in vec ) { if ( a [ i ++ ] != it ) return - 1 ; } } return x ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 5 , 4 , 10 } ; int n = a . Length ; Console . Write ( findX ( a , n ) ) ; } }
Program to print an array in Pendulum Arrangement with constant space | C # implementation of the approach ; Function to print the Pendulum arrangement of the given array ; Sort the array sort ( arr , arr + n ) ; ; pos stores the index of the last element of the array ; odd stores the last odd index in the array ; Move all odd index positioned elements to the right ; Shift the elements by one position from odd to pos ; Reverse the element from 0 to ( n - 1 ) / 2 ; Printing the pendulum arrangement ; Driver code
using System ; class GFG { static void pendulumArrangement ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int odd , temp , p , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; p = odd ; while ( p != pos ) { arr [ p ] = arr [ p + 1 ] ; p ++ ; } arr [ p ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } int start = 0 , end = ( n - 1 ) / 2 ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 11 , 2 , 4 , 55 , 6 , 8 } ; int n = arr . Length ; pendulumArrangement ( arr , n ) ; } }
Find all the pairs with given sum in a BST | Set 2 | C # program to implement the above approach ; A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code
using System . Collections . Generic ; using System ; class GFG { public class Node { public int data ; public Node left , right , root ; public Node ( int data ) { this . data = data ; } } public static Node AddNode ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } public static void TargetPair ( Node node , int tar ) { List < Node > LeftList = new List < Node > ( ) ; List < Node > RightList = new List < Node > ( ) ; Node curr_left = node ; Node curr_right = node ; while ( curr_left != null curr_right != null LeftList . Count > 0 && RightList . Count > 0 ) { while ( curr_left != null ) { LeftList . Add ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . Add ( curr_right ) ; curr_right = curr_right . right ; } Node LeftNode = LeftList [ LeftList . Count - 1 ] ; Node RightNode = RightList [ RightList . Count - 1 ] ; int leftVal = LeftNode . data ; int rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . RemoveAt ( RightList . Count - 1 ) ; curr_right = RightNode . left ; } else { Console . WriteLine ( LeftNode . data + " ▁ " + RightNode . data ) ; RightList . RemoveAt ( RightList . Count - 1 ) ; LeftList . RemoveAt ( LeftList . Count - 1 ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } public static void Main ( String [ ] b ) { Node root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; } }
Minimum number greater than the maximum of array which cannot be formed using the numbers in the array | C # implementation of the approach ; Function that returns the minimum number greater than maximum of the array that cannot be formed using the elements of the array ; Sort the given array ; Maximum number in the array ; table [ i ] will store the minimum number of elements from the array to form i ; Calculate the minimum number of elements from the array required to form the numbers from 1 to ( 2 * max ) ; If there exists a number greater than the maximum element of the array that can be formed using the numbers of array ; Driver code
using System ; class GFG { static int findNumber ( int [ ] arr , int n ) { Array . Sort ( arr ) ; int max = arr [ n - 1 ] ; int [ ] table = new int [ ( 2 * max ) + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = int . MaxValue ; int ans = - 1 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { int res = table [ i - arr [ j ] ] ; if ( res != int . MaxValue && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == int . MaxValue ) { ans = i ; break ; } } return ans ; } public static void Main ( ) { int [ ] arr = { 6 , 7 , 15 } ; int n = arr . Length ; Console . WriteLine ( findNumber ( arr , n ) ) ; } }