codeparrot/xlcost-text-to-code huggingface dataset:
Minimum partitions of maximum size 2 and sum limited by given value | C ++ program to count minimum number of partitions of size 2 and sum smaller than or equal to given key . ; sort the array ; if sum of ith smaller and jth larger element is less than key , then pack both numbers in a set otherwise pack the jth larger number alone in the set ; After ending of loop i will contain minimum number of sets ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int minimumSets ( int arr [ ] , int n , int key ) { int i , j ; sort ( arr , arr + n ) ; for ( i = 0 , j = n - 1 ; i <= j ; ++ i ) if ( arr [ i ] + arr [ j ] <= key ) j -- ; return i ; } int main ( ) { int arr [ ] = { 3 , 5 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int key = 5 ; cout << minimumSets ( arr , n , key ) ; return 0 ; }
Bubble Sort On Doubly Linked List | CPP program to sort a doubly linked list using bubble sort ; structure of a node ; Function to insert a node at the beginning of a linked list ; Function to print nodes in a given linked list ; Bubble sort the given linked list ; Checking for empty list ; Driver code ; start with empty linked list ; Create linked list from the array arr [ ] . Created linked list will be 1 -> 11 -> 2 -> 56 -> 12 ; print list before sorting ; sort the linked list ; print list after sorting
#include <iostream> NEW_LINE using namespace std ; struct Node { int data ; Node * prev ; Node * next ; } ; void insertAtTheBegin ( struct Node * * start_ref , int data ) { struct Node * ptr1 = new Node ; ptr1 -> data = data ; ptr1 -> next = * start_ref ; if ( * start_ref != NULL ) ( * start_ref ) -> prev = ptr1 ; * start_ref = ptr1 ; } void printList ( struct Node * start ) { struct Node * temp = start ; cout << endl ; while ( temp != NULL ) { cout << temp -> data << " ▁ " ; temp = temp -> next ; } } void bubbleSort ( struct Node * start ) { int swapped , i ; struct Node * ptr1 ; struct Node * lptr = NULL ; if ( start == NULL ) return ; do { swapped = 0 ; ptr1 = start ; while ( ptr1 -> next != lptr ) { if ( ptr1 -> data > ptr1 -> next -> data ) { swap ( ptr1 -> data , ptr1 -> next -> data ) ; swapped = 1 ; } ptr1 = ptr1 -> next ; } lptr = ptr1 ; } while ( swapped ) ; } int main ( ) { int arr [ ] = { 12 , 56 , 2 , 11 , 1 , 90 } ; int list_size , i ; struct Node * start = NULL ; for ( i = 0 ; i < 6 ; i ++ ) insertAtTheBegin ( & start , arr [ i ] ) ; printf ( " Linked list before sorting " printList ( start ) ; bubbleSort ( start ) ; printf ( " Linked list after sorting " printList ( start ) ; return 0 ; }
Substring Sort | C ++ Program to sort substrings ; sort the input array ; repeated length should be the same string ; two different strings with the same length input array cannot be sorted ; validate that each string is a substring of the following one ; get first element ; The array is valid and sorted print the strings in order ; Test 1 ; Test 2
#include <bits/stdc++.h> NEW_LINE using namespace std ; void substringSort ( string arr [ ] , int n , int maxLen ) { int count [ maxLen ] ; string sortedArr [ maxLen ] ; for ( int i = 0 ; i < maxLen ; i ++ ) { count [ i ] = 0 ; sortedArr [ i ] = " " ; } for ( int i = 0 ; i < n ; i ++ ) { string s = arr [ i ] ; int len = s . length ( ) ; if ( count [ len - 1 ] == 0 ) { sortedArr [ len - 1 ] = s ; count [ len - 1 ] = 1 ; } else if ( sortedArr [ len - 1 ] == s ) { count [ len - 1 ] ++ ; } else { cout << " Cannot ▁ be ▁ sorted " ; return ; } } int index = 0 ; while ( count [ index ] == 0 ) index ++ ; int prev = index ; string prevString = sortedArr [ prev ] ; index ++ ; for ( ; index < maxLen ; index ++ ) { if ( count [ index ] != 0 ) { string current = sortedArr [ index ] ; if ( current . find ( prevString ) != string :: npos ) { prev = index ; prevString = current ; } else { cout << " Cannot ▁ be ▁ sorted " ; return ; } } } for ( int i = 0 ; i < maxLen ; i ++ ) { string s = sortedArr [ i ] ; for ( int j = 0 ; j < count [ i ] ; j ++ ) { cout << s << endl ; } } } int main ( ) { int maxLen = 100 ; string arr1 [ ] = { " d " , " zddsaaz " , " ds " , " ddsaa " , " dds " , " dds " } ; substringSort ( arr1 , sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) , maxLen ) ; string arr2 [ ] = { " for " , " rof " } ; substringSort ( arr2 , sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) , maxLen ) ; return 0 ; }
Number of paths from source to destination in a directed acyclic graph | C ++ program for Number of paths from one vertex to another vertex in a Directed Acyclic Graph ; to make graph ; function to add edge in graph ; there is path from a to b . ; function to make topological sorting ; insert all vertices which don 't have any parent. ; using kahn 's algorithm  for topological sorting ; insert front element of queue to vector ; go through all it 's childs ; whenever the frequency is zero then add this vertex to queue . ; Function that returns the number of paths ; make topological sorting ; to store required answer . ; answer from destination to destination is 1. ; traverse in reverse order ; Driver code ; here vertices are numbered from 0 to n - 1. ; to count number of vertex which don 't  have any parents. ; to add all edges of graph ; Function that returns the number of paths
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAXN  1000005 NEW_LINE vector < int > v [ MAXN ] ; void add_edge ( int a , int b , int fre [ ] ) { v [ a ] . push_back ( b ) ; fre [ b ] ++ ; } vector < int > topological_sorting ( int fre [ ] , int n ) { queue < int > q ; for ( int i = 0 ; i < n ; i ++ ) if ( ! fre [ i ] ) q . push ( i ) ; vector < int > l ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; l . push_back ( u ) ; for ( int i = 0 ; i < v [ u ] . size ( ) ; i ++ ) { fre [ v [ u ] [ i ] ] -- ; if ( ! fre [ v [ u ] [ i ] ] ) q . push ( v [ u ] [ i ] ) ; } } return l ; } int numberofPaths ( int source , int destination , int n , int fre [ ] ) { vector < int > s = topological_sorting ( fre , n ) ; int dp [ n ] = { 0 } ; dp [ destination ] = 1 ; for ( int i = s . size ( ) - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < v [ s [ i ] ] . size ( ) ; j ++ ) { dp [ s [ i ] ] += dp [ v [ s [ i ] ] [ j ] ] ; } } return dp ; } int main ( ) { int n = 5 ; int source = 0 , destination = 4 ; int fre [ n ] = { 0 } ; add_edge ( 0 , 1 , fre ) ; add_edge ( 0 , 2 , fre ) ; add_edge ( 0 , 3 , fre ) ; add_edge ( 0 , 4 , fre ) ; add_edge ( 2 , 3 , fre ) ; add_edge ( 3 , 4 , fre ) ; cout << numberofPaths ( source , destination , n , fre ) ; }
Print all the pairs that contains the positive and negative values of an element | CPP program to find pairs of positive and negative values present in an array . ; Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printPairs ( int arr [ ] , int n ) { bool pair_exists = false ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( binary_search ( arr , arr + n , - arr [ i ] ) ) { cout << arr [ i ] << " , ▁ " << - arr [ i ] << endl ; pair_exists = true ; } } else break ; } if ( pair_exists == false ) cout << " No ▁ such ▁ pair ▁ exists " ; } int main ( ) { int arr [ ] = { 4 , 8 , 9 , -4 , 1 , -1 , -8 , -9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printPairs ( arr , n ) ; return 0 ; }
Multiset Equivalence Problem | C ++ program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool areSame ( vector < int > & a , vector < int > & b ) { sort ( a . begin ( ) , a . end ( ) ) ; sort ( b . begin ( ) , b . end ( ) ) ; return ( a == b ) ; } int main ( ) { vector < int > a ( { 7 , 7 , 5 } ) , b ( { 7 , 5 , 5 } ) ; if ( areSame ( a , b ) ) cout << " Yes STRNEWLINE " ; else cout << " No STRNEWLINE " ; return 0 ; }
Minimum number of edges between two vertices of a Graph | C ++ program to find minimum edge between given two vertex of Graph ; function for finding minimum no . of edge using BFS ; visited [ n ] for keeping track of visited node in BFS ; Initialize distances as 0 ; queue to do BFS . ; update distance for i ; function for addition of edge ; Driver function ; To store adjacency list of graph
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minEdgeBFS ( vector < int > edges [ ] , int u , int v , int n ) { vector < bool > visited ( n , 0 ) ; vector < int > distance ( n , 0 ) ; queue < int > Q ; distance [ u ] = 0 ; Q . push ( u ) ; visited [ u ] = true ; while ( ! Q . empty ( ) ) { int x = Q . front ( ) ; Q . pop ( ) ; for ( int i = 0 ; i < edges [ x ] . size ( ) ; i ++ ) { if ( visited [ edges [ x ] [ i ] ] ) continue ; distance [ edges [ x ] [ i ] ] = distance [ x ] + 1 ; Q . push ( edges [ x ] [ i ] ) ; visited [ edges [ x ] [ i ] ] = 1 ; } } return distance [ v ] ; } void addEdge ( vector < int > edges [ ] , int u , int v ) { edges [ u ] . push_back ( v ) ; edges [ v ] . push_back ( u ) ; } int main ( ) { int n = 9 ; vector < int > edges [ 9 ] ; addEdge ( edges , 0 , 1 ) ; addEdge ( edges , 0 , 7 ) ; addEdge ( edges , 1 , 7 ) ; addEdge ( edges , 1 , 2 ) ; addEdge ( edges , 2 , 3 ) ; addEdge ( edges , 2 , 5 ) ; addEdge ( edges , 2 , 8 ) ; addEdge ( edges , 3 , 4 ) ; addEdge ( edges , 3 , 5 ) ; addEdge ( edges , 4 , 5 ) ; addEdge ( edges , 5 , 6 ) ; addEdge ( edges , 6 , 7 ) ; addEdge ( edges , 7 , 8 ) ; int u = 0 ; int v = 5 ; cout << minEdgeBFS ( edges , u , v , n ) ; return 0 ; }
Minimum swaps so that binary search can be applied | CPP program to find Minimum number of swaps to get the position of the element if we provide an unsorted array to binary search . ; Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimumSwaps ( int * arr , int n , int k ) { int pos , num_min , num_max , need_minimum , need_maximum , swaps ; num_min = num_max = need_minimum = 0 ; need_maximum = swaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < k ) num_min ++ ; else if ( arr [ i ] > k ) num_max ++ ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == k ) { pos = i ; break ; } } int left , right , mid ; left = 0 ; right = n - 1 ; while ( left <= right ) { mid = ( left + right ) / 2 ; if ( arr [ mid ] == k ) { break ; } else if ( arr [ mid ] > k ) { if ( pos > mid ) need_minimum ++ ; else num_min -- ; left = mid + 1 ; } else { if ( pos < mid ) need_maximum ++ ; else num_max -- ; right = mid - 1 ; } } if ( need_minimum > need_maximum ) { swaps = swaps + need_maximum ; num_min = num_min - need_maximum ; need_minimum = need_minimum - need_maximum ; need_maximum = 0 ; } else { swaps = swaps + need_minimum ; num_max = num_max - need_minimum ; need_maximum = need_maximum - need_minimum ; need_minimum = 0 ; } if ( need_maximum > num_max need_minimum > num_min ) return -1 ; else return ( swaps + need_maximum + need_minimum ) ; } int main ( ) { int arr [ ] = { 3 , 10 , 6 , 7 , 2 , 5 , 4 } , k = 4 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMinimumSwaps ( arr , n , k ) ; }
Stable sort for descending order | C ++ program to demonstrate descending order stable sort using greater < > ( ) . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int main ( ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; stable_sort ( arr , arr + n , greater < int > ( ) ) ; cout << " Array ▁ after ▁ sorting ▁ : ▁ STRNEWLINE " ; for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Check if both halves of the string have at least one different character | C ++ implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver function
#include <algorithm> NEW_LINE #include <cstring> NEW_LINE #include <iostream> NEW_LINE #include <string> NEW_LINE using namespace std ; bool function ( char str [ ] ) { int l = strlen ( str ) ; sort ( str , str + ( l / 2 ) ) ; sort ( str + ( l / 2 ) , str + l ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } int main ( ) { char str [ ] = " abcasdsabcae " ; if ( function ( str ) ) cout << " Yes , ▁ both ▁ halves ▁ differ ▁ by " << " ▁ at ▁ least ▁ one ▁ character " ; else cout << " No , ▁ both ▁ halves ▁ do " << " ▁ not ▁ differ ▁ at ▁ all " ; return 0 ; }
Number of visible boxes after putting one inside another | CPP program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumBox ( int arr [ ] , int n ) { queue < int > q ; queue < int > q ; sort ( arr , arr + n ) ; q . push ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . front ( ) ; if ( arr [ i ] >= 2 * now ) q . pop ( ) ; q . push ( arr [ i ] ) ; } return q . size ( ) ; } int main ( ) { int arr [ ] = { 4 , 1 , 2 , 8 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minimumBox ( arr , n ) << endl ; return 0 ; }
Sort a binary array using one traversal | CPP program to sort a binary array ; if number is smaller than 1 then swap it with j - th number ; Driver code
#include <iostream> NEW_LINE using namespace std ; void sortBinaryArray ( int a [ ] , int n ) { int j = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 ) { j ++ ; swap ( a [ i ] , a [ j ] ) ; } } } int main ( ) { int a [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; sortBinaryArray ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " ▁ " ; return 0 ; }
Smallest element in an array that is repeated exactly ' k ' times . | C ++ program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MAX = 1000 ; int function findDuplicate ( $ arr , $ n , $ k ) { int res = MAX + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) count += 1 ; if ( count == k ) res = min ( res , arr [ i ] ) ; } } return res ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = sizeof ( arr ) / ( sizeof ( arr [ 0 ] ) ) ; cout << findDuplicate ( arr , n , k ) ; return 0 ; }
Smallest element in an array that is repeated exactly ' k ' times . | C ++ program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDuplicate ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int i = 0 ; while ( i < n ) { int j , count = 1 ; for ( j = i + 1 ; j < n && arr [ j ] == arr [ i ] ; j ++ ) count ++ ; if ( count == k ) return arr [ i ] ; i = j ; } return -1 ; } int main ( ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = sizeof ( arr ) / ( sizeof ( arr [ 0 ] ) ) ; cout << findDuplicate ( arr , n , k ) ; return 0 ; }
Longest Common Prefix using Sorting | C ++ program to find longest common prefix of given array of words . ; If size is 0 , return empty string ; Sort the given array ; Find the minimum length from first and last string ; find the common prefix between the first and last string ; Driver Code
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; string longestCommonPrefix ( string ar [ ] , int n ) { if ( n == 0 ) return " " ; if ( n == 1 ) return ar [ 0 ] ; sort ( ar , ar + n ) ; int en = min ( ar [ 0 ] . size ( ) , ar [ n - 1 ] . size ( ) ) ; string first = ar [ 0 ] , last = ar [ n - 1 ] ; int i = 0 ; while ( i < en && first [ i ] == last [ i ] ) i ++ ; string pre = first . substr ( 0 , i ) ; return pre ; } int main ( ) { string ar [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << " The ▁ longest ▁ common ▁ prefix ▁ is : ▁ " << longestCommonPrefix ( ar , n ) ; return 0 ; }
Find the minimum and maximum amount to buy all N candies | C ++ implementation to find the minimum and maximum amount ; function to find the maximum and the minimum cost required ; Sort the array ; print the minimum cost ; print the maximum cost ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find ( vector < int > arr , int n , int k ) { sort ( arr . begin ( ) , arr . end ( ) ) ; int b = ceil ( n / k * 1.0 ) ; int min_sum = 0 , max_sum = 0 ; for ( int i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( int i = 2 ; i < arr . size ( ) ; i ++ ) max_sum += arr [ i ] ; cout << " minimum ▁ " << min_sum << endl ; cout << " maximum ▁ " << max_sum << endl ; } int main ( ) { vector < int > arr = { 3 , 2 , 1 , 4 } ; int n = arr . size ( ) ; int k = 2 ; find ( arr , n , k ) ; }
Check if it is possible to sort an array with conditional swapping of adjacent allowed | C ++ program to check if we can sort an array with adjacent swaps allowed ; Returns true if it is possible to sort else false / ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkForSorting ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) swap ( arr [ i ] , arr [ i + 1 ] ) ; else return false ; } } return true ; } int main ( ) { int arr [ ] = { 1 , 0 , 3 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( checkForSorting ( arr , n ) ) cout << " Yes " ; else cout << " No " ; }
Sort an array of strings according to string lengths | C ++ program to sort an Array of Strings according to their lengths ; Function to print the sorted array of string ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Driver function ; Function to perform sorting ; Calling the function to print result
#include <iostream> NEW_LINE using namespace std ; void printArraystring ( string , int ) ; void sort ( string s [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { string temp = s [ i ] ; int j = i - 1 ; while ( j >= 0 && temp . length ( ) < s [ j ] . length ( ) ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } void printArraystring ( string str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << str [ i ] << " ▁ " ; } int main ( ) { string arr [ ] = { " GeeksforGeeks " , " I " , " from " , " am " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sort ( arr , n ) ; printArraystring ( arr , n ) ; return 0 ; }
Hoare 's vs Lomuto partition scheme in QuickSort | C ++ implementation of QuickSort using Hoare 's  partition scheme.  ; This function takes first element as pivot , and places all the elements smaller than the pivot on the left side and all the elements greater than the pivot on the right side . It returns the index of the last element on the smaller side ; Find leftmost element greater than or equal to pivot ; Find rightmost element smaller than or equal to pivot ; If two pointers met . ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print an array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int partition ( int arr [ ] , int low , int high ) { int pivot = arr [ low ] ; int i = low - 1 , j = high + 1 ; while ( true ) { do { i ++ ; } while ( arr [ i ] < pivot ) ; do { j -- ; } while ( arr [ j ] > pivot ) ; if ( i >= j ) return j ; swap ( arr [ i ] , arr [ j ] ) ; } } void quickSort ( int arr [ ] , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi ) ; quickSort ( arr , pi + 1 , high ) ; } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , arr [ i ] ) ; printf ( " STRNEWLINE " ) ; } int main ( ) { int arr [ ] = { 10 , 7 , 8 , 9 , 1 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; quickSort ( arr , 0 , n - 1 ) ; printf ( " Sorted ▁ array : ▁ STRNEWLINE " ) ; printArray ( arr , n ) ; return 0 ; }
K | C ++ program to find the Kth smallest element after removing some integer from first n natural number . ; Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest element after removing  array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ksmallest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; if ( k < arr [ 0 ] ) return k ; if ( k == arr [ 0 ] ) return arr [ 0 ] + 1 ; if ( k > arr [ n - 1 ] ) return k + n ; if ( arr [ 0 ] == 1 ) k -- ; else k -= ( arr [ 0 ] - 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { int c = arr [ i ] - arr [ i - 1 ] - 1 ; if ( k <= c ) return arr [ i - 1 ] + k ; else k -= c ; } return arr [ n - 1 ] + k ; } int main ( ) { int k = 1 ; int arr [ ] = { 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ksmallest ( arr , n , k ) ; return 0 ; }