codeparrot/xlcost-text-to-code huggingface dataset:
Graph and its representations | A simple representation of graph using STL ; A utility function to add an edge in an undirected graph . ; A utility function to print the adjacency list representation of graph ; Driver code ; Creating a graph with 5 vertices ; Adding edges one by one
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addEdge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } void printGraph ( vector < int > adj [ ] , int V ) { for ( int v = 0 ; v < V ; ++ v ) { cout << " Adjacency list of vertex " << v < < " head " for ( auto x : adj [ v ] ) cout << " - > ▁ " << x ; printf ( " STRNEWLINE " ) ; } } int main ( ) { int V = 5 ; vector < int > adj [ V ] ; addEdge ( adj , 0 , 1 ) ; addEdge ( adj , 0 , 4 ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; printGraph ( adj , V ) ; return 0 ; }
Graph representations using set and hash | A C ++ program to demonstrate adjacency list representation of graphs using sets ;  ; Adds an edge to an undirected graph ; Add an edge from src to dest . A new element is inserted to the adjacent list of src . ; Since graph is undirected , add an edge from dest to src also ; A utility function to print the adjacency list representation of graph ; Searches for a given edge in the graph ; Driver code ; Create the graph given in the above figure ; Print the adjacency list representation of the above graph ; Search the given edge in the graph
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Graph { int V ; set < int , greater < int > > * adjList ; } ; Graph * createGraph ( int V ) { Graph * graph = new Graph ; graph -> V = V ; graph -> adjList = new set < int , greater < int > > [ V ] ; return graph ; } void addEdge ( Graph * graph , int src , int dest ) { graph -> adjList [ src ] . insert ( dest ) ; graph -> adjList [ dest ] . insert ( src ) ; } void printGraph ( Graph * graph ) { for ( int i = 0 ; i < graph -> V ; ++ i ) { set < int , greater < int > > lst = graph -> adjList [ i ] ; cout << endl << " Adjacency ▁ list ▁ of ▁ vertex ▁ " << i << endl ; for ( auto itr = lst . begin ( ) ; itr != lst . end ( ) ; ++ itr ) cout << * itr << " ▁ " ; cout << endl ; } } void searchEdge ( Graph * graph , int src , int dest ) { auto itr = graph -> adjList [ src ] . find ( dest ) ; if ( itr == graph -> adjList [ src ] . end ( ) ) cout << endl << " Edge ▁ from ▁ " << src << " ▁ to ▁ " << dest << " ▁ not ▁ found . " << endl ; else cout << endl << " Edge ▁ from ▁ " << src << " ▁ to ▁ " << dest << " ▁ found . " << endl ; } int main ( ) { int V = 5 ; struct Graph * graph = createGraph ( V ) ; addEdge ( graph , 0 , 1 ) ; addEdge ( graph , 0 , 4 ) ; addEdge ( graph , 1 , 2 ) ; addEdge ( graph , 1 , 3 ) ; addEdge ( graph , 1 , 4 ) ; addEdge ( graph , 2 , 3 ) ; addEdge ( graph , 3 , 4 ) ; printGraph ( graph ) ; searchEdge ( graph , 2 , 1 ) ; searchEdge ( graph , 0 , 3 ) ; return 0 ; }
Minimum initial vertices to traverse whole matrix with given conditions | C ++ program to find minimum initial vertices to reach whole matrix . ; ( n , m ) is current source cell from which we need to do DFS . N and M are total no . of rows and columns . ; Marking the vertex as visited ; If below neighbor is valid and has value less than or equal to current cell 's value ; If right neighbor is valid and has value less than or equal to current cell 's value ; If above neighbor is valid and has value less than or equal to current cell 's value ; If left neighbor is valid and has value less than or equal to current cell 's value ; Storing the cell value and cell indices in a vector . ; Sorting the newly created array according to cell values ; Create a visited array for DFS and initialize it as false . ; Applying dfs for each vertex with highest value ; If the given vertex is not visited then include it in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 100 ; void dfs ( int n , int m , bool visit [ ] [ MAX ] , int adj [ ] [ MAX ] , int N , int M ) { visit [ n ] [ m ] = 1 ; if ( n + 1 < N && adj [ n ] [ m ] >= adj [ n + 1 ] [ m ] && ! visit [ n + 1 ] [ m ] ) dfs ( n + 1 , m , visit , adj , N , M ) ; if ( m + 1 < M && adj [ n ] [ m ] >= adj [ n ] [ m + 1 ] && ! visit [ n ] [ m + 1 ] ) dfs ( n , m + 1 , visit , adj , N , M ) ; if ( n - 1 >= 0 && adj [ n ] [ m ] >= adj [ n - 1 ] [ m ] && ! visit [ n - 1 ] [ m ] ) dfs ( n - 1 , m , visit , adj , N , M ) ; if ( m - 1 >= 0 && adj [ n ] [ m ] >= adj [ n ] [ m - 1 ] && ! visit [ n ] [ m - 1 ] ) dfs ( n , m - 1 , visit , adj , N , M ) ; } void printMinSources ( int adj [ ] [ MAX ] , int N , int M ) { vector < pair < long int , pair < int , int > > > x ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) x . push_back ( make_pair ( adj [ i ] [ j ] , make_pair ( i , j ) ) ) ; sort ( x . begin ( ) , x . end ( ) ) ; bool visit [ N ] [ MAX ] ; memset ( visit , false , sizeof ( visit ) ) ; for ( int i = x . size ( ) - 1 ; i >= 0 ; i -- ) { if ( ! visit [ x [ i ] . second . first ] [ x [ i ] . second . second ] ) { cout << x [ i ] . second . first << " ▁ " << x [ i ] . second . second << endl ; dfs ( x [ i ] . second . first , x [ i ] . second . second , visit , adj , N , M ) ; } } } int main ( ) { int N = 2 , M = 2 ; int adj [ N ] [ MAX ] = { { 3 , 3 } , { 1 , 1 } } ; printMinSources ( adj , N , M ) ; return 0 ; }
Shortest path to reach one prime to other by changing single digit at a time | CPP program to reach a prime number from another by changing single digits and using only prime numbers . ; Finding all 4 digit prime numbers ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Forming a vector of prime numbers ; in1 and in2 are two vertices of graph which are actually indexes in pset [ ] ; Returns true if num1 and num2 differ by single digit . ; To compare the digits ; If the numbers differ only by a single digit return true else false ; Generate all 4 digit ; Create a graph where node numbers are indexes in pset [ ] and there is an edge between two nodes only if they differ by single digit . ; Since graph nodes represent indexes of numbers in pset [ ] , we find indexes of num1 and num2 . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class graph { int V ; list < int > * l ; public : graph ( int V ) { this -> V = V ; l = new list < int > [ V ] ; } void addedge ( int V1 , int V2 ) { l [ V1 ] . push_back ( V2 ) ; l [ V2 ] . push_back ( V1 ) ; } int bfs ( int in1 , int in2 ) ; } ; void SieveOfEratosthenes ( vector < int > & v ) { int n = 9999 ; bool prime [ n + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 1000 ; p <= n ; p ++ ) if ( prime [ p ] ) v . push_back ( p ) ; } int graph :: bfs ( int in1 , int in2 ) { int visited [ V ] ; memset ( visited , 0 , sizeof ( visited ) ) ; queue < int > que ; visited [ in1 ] = 1 ; que . push ( in1 ) ; list < int > :: iterator i ; while ( ! que . empty ( ) ) { int p = que . front ( ) ; que . pop ( ) ; for ( i = l [ p ] . begin ( ) ; i != l [ p ] . end ( ) ; i ++ ) { if ( ! visited [ * i ] ) { visited [ * i ] = visited [ p ] + 1 ; que . push ( * i ) ; } if ( * i == in2 ) { return visited [ * i ] - 1 ; } } } } bool compare ( int num1 , int num2 ) { string s1 = to_string ( num1 ) ; string s2 = to_string ( num2 ) ; int c = 0 ; if ( s1 [ 0 ] != s2 [ 0 ] ) c ++ ; if ( s1 [ 1 ] != s2 [ 1 ] ) c ++ ; if ( s1 [ 2 ] != s2 [ 2 ] ) c ++ ; if ( s1 [ 3 ] != s2 [ 3 ] ) c ++ ; return ( c == 1 ) ; } int shortestPath ( int num1 , int num2 ) { vector < int > pset ; SieveOfEratosthenes ( pset ) ; graph g ( pset . size ( ) ) ; for ( int i = 0 ; i < pset . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pset . size ( ) ; j ++ ) if ( compare ( pset [ i ] , pset [ j ] ) ) g . addedge ( i , j ) ; int in1 , in2 ; for ( int j = 0 ; j < pset . size ( ) ; j ++ ) if ( pset [ j ] == num1 ) in1 = j ; for ( int j = 0 ; j < pset . size ( ) ; j ++ ) if ( pset [ j ] == num2 ) in2 = j ; return g . bfs ( in1 , in2 ) ; } int main ( ) { int num1 = 1033 , num2 = 8179 ; cout << shortestPath ( num1 , num2 ) ; return 0 ; }
Level of Each node in a Tree from source node ( using BFS ) | CPP Program to determine level of each node and print level ; function to determine level of each node starting from x using BFS ; array to store level of each node ; create a queue ; enqueue element x ; initialize level of source node to 0 ; marked it as visited ; do until queue is empty ; get the first element of queue ; dequeue element ; traverse neighbors of node x ; b is neighbor of node x ; if b is not marked already ; enqueue b in queue ; level of b is level of x + 1 ; mark b ; display all nodes and their levels ; Driver Code ; adjacency graph for tree ; call levels function with source as 0
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printLevels ( vector < int > graph [ ] , int V , int x ) { int level [ V ] ; bool marked [ V ] ; queue < int > que ; que . push ( x ) ; level [ x ] = 0 ; marked [ x ] = true ; while ( ! que . empty ( ) ) { x = que . front ( ) ; que . pop ( ) ; for ( int i = 0 ; i < graph [ x ] . size ( ) ; i ++ ) { int b = graph [ x ] [ i ] ; if ( ! marked [ b ] ) { que . push ( b ) ; level [ b ] = level [ x ] + 1 ; marked [ b ] = true ; } } } cout << " Nodes " << " TABSYMBOL " << " Level " << endl ; for ( int i = 0 ; i < V ; i ++ ) cout << " ▁ " << i << " ▁ - - > ▁ " << level [ i ] << endl ; } int main ( ) { int V = 8 ; vector < int > graph [ V ] ; graph [ 0 ] . push_back ( 1 ) ; graph [ 0 ] . push_back ( 2 ) ; graph [ 1 ] . push_back ( 3 ) ; graph [ 1 ] . push_back ( 4 ) ; graph [ 1 ] . push_back ( 5 ) ; graph [ 2 ] . push_back ( 5 ) ; graph [ 2 ] . push_back ( 6 ) ; graph [ 6 ] . push_back ( 7 ) ; printLevels ( graph , V , 0 ) ; return 0 ; }
Construct binary palindrome by repeated appending and trimming | CPP code to form binary palindrome ; function to apply DFS ; set the parent marked ; if the node has not been visited set it and its children marked ; link which digits must be equal ; connect the two indices ; set everything connected to first character as 1 ; driver code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; void dfs ( int parent , int ans [ ] , vector < int > connectchars [ ] ) { ans [ parent ] = 1 ; for ( int i = 0 ; i < connectchars [ parent ] . size ( ) ; i ++ ) { if ( ! ans [ connectchars [ parent ] [ i ] ] ) dfs ( connectchars [ parent ] [ i ] , ans , connectchars ) ; } } void printBinaryPalindrome ( int n , int k ) { int arr [ n ] , ans [ n ] = { 0 } ; vector < int > connectchars [ k ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i % k ; for ( int i = 0 ; i < n / 2 ; i ++ ) { connectchars [ arr [ i ] ] . push_back ( arr [ n - i - 1 ] ) ; connectchars [ arr [ n - i - 1 ] ] . push_back ( arr [ i ] ) ; } dfs ( 0 , ans , connectchars ) ; for ( int i = 0 ; i < n ; i ++ ) cout << ans [ arr [ i ] ] ; } int main ( ) { int n = 10 , k = 4 ; printBinaryPalindrome ( n , k ) ; return 0 ; }
Find n | C ++ program to find n - th node of Postorder Traversal of Binary Tree ; node of tree ; function to create a new node ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; driver code ; prints n - th node found
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * left , * right ; } ; struct Node * createNode ( int item ) { Node * temp = new Node ; temp -> data = item ; temp -> left = NULL ; temp -> right = NULL ; return temp ; } void NthPostordernode ( struct Node * root , int N ) { static int flag = 0 ; if ( root == NULL ) return ; if ( flag <= N ) { NthPostordernode ( root -> left , N ) ; NthPostordernode ( root -> right , N ) ; flag ++ ; if ( flag == N ) cout << root -> data ; } } int main ( ) { struct Node * root = createNode ( 25 ) ; root -> left = createNode ( 20 ) ; root -> right = createNode ( 30 ) ; root -> left -> left = createNode ( 18 ) ; root -> left -> right = createNode ( 22 ) ; root -> right -> left = createNode ( 24 ) ; root -> right -> right = createNode ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; return 0 ; }
DFS for a n | CPP code to perform DFS of given tree : ; DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver program to test above function ; Number of nodes ; Adjacency list ; Designing the tree ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dfs ( vector < int > list [ ] , int node , int arrival ) { cout << node << ' ' ; for ( int i = 0 ; i < list [ node ] . size ( ) ; i ++ ) { if ( list [ node ] [ i ] != arrival ) dfs ( list , list [ node ] [ i ] , node ) ; } } int main ( ) { int nodes = 5 ; vector < int > list [ 10000 ] ; list [ 1 ] . push_back ( 2 ) ; list [ 2 ] . push_back ( 1 ) ; list [ 1 ] . push_back ( 3 ) ; list [ 3 ] . push_back ( 1 ) ; list [ 2 ] . push_back ( 4 ) ; list [ 4 ] . push_back ( 2 ) ; list [ 3 ] . push_back ( 5 ) ; list [ 5 ] . push_back ( 3 ) ; dfs ( list , 1 , 0 ) ; return 0 ; }
Maximum number of edges to be added to a tree so that it stays a Bipartite graph | CPP code to print maximum edges such that Tree remains a Bipartite graph ; To store counts of nodes with two colors ; Increment count of nodes with current color ; Traversing adjacent nodes ; Not recurring for the parent node ; Finds maximum number of edges that can be added without violating Bipartite property . ; Do a DFS to count number of nodes of each color ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; long long count_color [ 2 ] ; void dfs ( vector < int > adj [ ] , int node , int parent , int color ) { count_color [ color ] ++ ; for ( int i = 0 ; i < adj [ node ] . size ( ) ; i ++ ) { if ( adj [ node ] [ i ] != parent ) dfs ( adj , adj [ node ] [ i ] , node , ! color ) ; } } int findMaxEdges ( vector < int > adj [ ] , int n ) { dfs ( adj , 1 , 0 , 0 ) ; return count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ; } int main ( ) { int n = 5 ; vector < int > adj [ n + 1 ] ; adj [ 1 ] . push_back ( 2 ) ; adj [ 1 ] . push_back ( 3 ) ; adj [ 2 ] . push_back ( 4 ) ; adj [ 3 ] . push_back ( 5 ) ; cout << findMaxEdges ( adj , n ) ; return 0 ; }
Min steps to empty an Array by removing a pair each time with sum at most K | C ++ program for the above approach ; Function to count minimum steps ; Function to sort the array ; Run while loop ; Condition to check whether sum exceed the target or not ; Increment the step by 1 ; Return minimum steps ; Driver Code ; Given array arr [ ] ; Given target value ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countMinSteps ( int arr [ ] , int target , int n ) { sort ( arr , arr + n ) ; int minimumSteps = 0 ; int i = 0 , j = n - 1 ; while ( i <= j ) { if ( arr [ i ] + arr [ j ] <= target ) { i ++ ; j -- ; } else { j -- ; } minimumSteps ++ ; } return minimumSteps ; } int main ( ) { int arr [ ] = { 4 , 6 , 2 , 9 , 6 , 5 , 8 , 10 } ; int target = 11 ; int size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countMinSteps ( arr , target , size ) ; return 0 ; }
Sort the strings based on the numbers of matchsticks required to represent them | C ++ implementation to sort the strings with the number of sticks required to represent them ; Stick [ ] stores the count of sticks required to represent the alphabets ; Number [ ] stores the count of sticks required to represent the numerals ; Function that return the count of sticks required to represent the given string str ; Loop to iterate over every character of the string ; Add the count of sticks required to represent the current character ; Function to sort the array according to the number of sticks required to represent it ; Vector to store the number of sticks required with respective strings ; Inserting number of sticks with respective strings ; Sort the vector , ; Print the sorted vector ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sticks [ ] = { 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 } ; int number [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int countSticks ( string str ) { int cnt = 0 ; for ( int i = 0 ; str [ i ] ; i ++ ) { char ch = str [ i ] ; if ( ch >= ' A ' && ch <= ' Z ' ) { cnt += sticks [ ch - ' A ' ] ; } else { cnt += number [ ch - '0' ] ; } } return cnt ; } void sortArr ( string arr [ ] , int n ) { vector < pair < int , string > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( countSticks ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { string arr [ ] = { " GEEKS " , " FOR " , " GEEKSFORGEEKS " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Count number of pairs with positive sum in an array | Naive approach to count pairs with positive sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with positive sum ; Initialize result ; Consider all possible pairs and check their sums ; If arr [ i ] & arr [ j ] form valid pair ; Driver 's Code ; Function call to find the count of pairs
#include <bits/stdc++.h> NEW_LINE using namespace std ; int CountPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] + arr [ j ] > 0 ) count += 1 ; } } return count ; } int main ( ) { int arr [ ] = { -7 , -1 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << CountPairs ( arr , n ) ; return 0 ; }
Find if there exists a direction for ranges such that no two range intersect | C ++ implementation of the approach ; Structure to hold details of each interval ; Function that returns true if the assignment of directions is possible ; Sort the intervals based on velocity ; Test the condition for all intervals with same velocity ; If for any velocity , 3 or more intervals share a common point return false ; Driver code
#include <bits/stdc++.h> NEW_LINE #define MAX  100001 NEW_LINE using namespace std ; typedef struct { int l , r , v ; } interval ; bool cmp ( interval a , interval b ) { return a . v < b . v ; } bool isPossible ( int range [ ] [ 3 ] , int N ) { interval test [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { test [ i ] . l = range [ i ] [ 0 ] ; test [ i ] . r = range [ i ] [ 1 ] ; test [ i ] . v = range [ i ] [ 2 ] ; } sort ( test , test + N , cmp ) ; for ( int i = 0 ; i < N ; i ++ ) { int count [ MAX ] = { 0 } ; int current_velocity = test [ i ] . v ; int j = i ; while ( j < N && test [ j ] . v == current_velocity ) { for ( int k = test [ j ] . l ; k <= test [ j ] . r ; k ++ ) { count [ k ] ++ ; if ( count [ k ] >= 3 ) return false ; } j ++ ; } i = j - 1 ; } return true ; } int main ( ) { int range [ ] [ 3 ] = { { 1 , 2 , 3 } , { 2 , 5 , 1 } , { 3 , 10 , 1 } , { 4 , 4 , 1 } , { 5 , 7 , 10 } } ; int n = sizeof ( range ) / sizeof ( range [ 0 ] ) ; if ( isPossible ( range , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
a | C ++ implementation of the approach ; Function to return the modified string ; To store the previous character ; To store the starting indices of all the groups ; Starting index of the first group ; If the current character and the previous character differ ; Push the starting index of the new group ; The current character now becomes previous ; Sort the first group ; Sort all the remaining groups ; cout << str << endl ; Sort the last group ; Return the modified string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string get_string ( string str , int n ) { char prev = str [ 0 ] ; vector < int > result ; result . push_back ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( isdigit ( str [ i ] ) != isdigit ( prev ) ) { result . push_back ( i ) ; } prev = str [ i ] ; } sort ( str . begin ( ) , str . begin ( ) + result [ 0 ] ) ; for ( int i = 0 ; i < result . size ( ) - 1 ; i ++ ) { sort ( str . begin ( ) + result [ i ] , str . begin ( ) + result [ i + 1 ] ) ; } sort ( str . begin ( ) + result [ result . size ( ) - 1 ] , str . end ( ) ) ; return str ; } int main ( ) { string str = "121geeks21" ; int n = str . length ( ) ; cout << get_string ( str , n ) ; return 0 ; }
Find the kth smallest number with sum of digits as m | C ++ implementation of the approach ; Recursively moving to add all the numbers upto a limit with sum of digits as m ; Max number of digits allowed in a number for this implementation ; Function to return the kth number with sum of digits as m ; The kth smallest number is found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define int  long long NEW_LINE const int N = 2005 ; set < int > ans ; void dfs ( int num , int left , int ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . insert ( num ) ; for ( int i = 0 ; i <= min ( left , 9LL ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } int getKthNum ( int m , int k ) { dfs ( 0 , m , 0 ) ; int ct = 0 ; for ( auto it : ans ) { ct ++ ; if ( ct == k ) { return it ; } } return -1 ; } int main ( ) { int m = 5 , k = 3 ; cout << getKthNum ( m , k ) ; return 0 ; }
Remove minimum elements from the array such that 2 * min becomes more than max | CPP program to remove minimum elements from the array such that 2 * min becomes more than max ; Function to remove minimum elements from the array such that 2 * min becomes more than max ; Sort the array ; To store the required answer ; Traverse from left to right ; Update the answer ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Removal ( vector < int > v , int n ) { sort ( v . begin ( ) , v . end ( ) ) ; int ans = INT_MAX ; for ( vector < int > :: iterator i = v . begin ( ) ; i != v . end ( ) ; i ++ ) { vector < int > :: iterator j = upper_bound ( v . begin ( ) , v . end ( ) , ( 2 * ( * i ) ) ) ; ans = min ( ans , n - ( int ) ( j - i ) ) ; } return ans ; } int main ( ) { vector < int > a = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = a . size ( ) ; cout << Removal ( a , n ) ; return 0 ; }