codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array according to absolute difference with a given value " using ▁ constant ▁ extra ▁ space " | C ++ program to sort an array based on absolute difference with a given value x . ; Below lines are similar to insertion sort ; Insert arr [ i ] at correct place ; Function to print the array ; Main Function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void arrange ( int arr [ ] , int n , int x ) { for ( int i = 1 ; i < n ; i ++ ) { int diff = abs ( arr [ i ] - x ) ; int j = i - 1 ; if ( abs ( arr [ j ] - x ) > diff ) { int temp = arr [ i ] ; while ( abs ( arr [ j ] - x ) > diff && j >= 0 ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 5 , 3 , 9 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ; return 0 ; }
Check if a grid can become row | C ++ program to check if we can make a grid of character sorted using adjacent swaps . ; v [ ] is vector of strings . len is length of strings in every row . ; Driver code ; int len = 5 ; Length of strings
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool check ( vector < string > v , int len ) { int n = v . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) sort ( v [ i ] . begin ( ) , v [ i ] . end ( ) ) ; for ( int i = 0 ; i < len - 1 ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( v [ i ] [ j ] > v [ i + 1 ] [ j ] ) return false ; return true ; } int main ( ) { vector < string > v = { " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " } ; check ( v , len ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Sort first half in ascending and second half in descending order | 1 | C ++ program to print first half in ascending order and the second half in descending order ; function to print half of the array in ascending order and the other half in descending order ; sorting the array ; printing first half in ascending order ; printing second half in descending order ; driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOrder ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) cout << arr [ i ] << " ▁ " ; for ( int j = n - 1 ; j >= n / 2 ; j -- ) cout << arr [ j ] << " ▁ " ; } int main ( ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , -1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printOrder ( arr , n ) ; return 0 ; }
Find the Sub | C ++ program to find subarray with sum closest to 0 ; Function to find the subarray ; Pick a starting point ; Consider current starting point as a subarray and update minimum sum and subarray indexes ; Try all subarrays starting with i ; update minimum sum and subarray indexes ; Return starting and ending indexes ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; pair < int , int > findSubArray ( int arr [ ] , int n ) { int start , end , min_sum = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = arr [ i ] ; if ( min_sum > abs ( curr_sum ) ) { min_sum = abs ( curr_sum ) ; start = i ; end = i ; } for ( int j = i + 1 ; j < n ; j ++ ) { curr_sum = curr_sum + arr [ j ] ; if ( min_sum > abs ( curr_sum ) ) { min_sum = abs ( curr_sum ) ; start = i ; end = j ; } } } pair < int , int > p = make_pair ( start , end ) ; return p ; } int main ( ) { int arr [ ] = { 2 , -5 , 4 , -6 , -3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pair < int , int > point = findSubArray ( arr , n ) ; cout << " Subarray ▁ starting ▁ from ▁ " ; cout << point . first << " ▁ to ▁ " << point . second ; return 0 ; }
Find shortest unique prefix for every word in a given list | Set 2 ( Using Sorting ) | C ++ program to print shortest unique prefixes for every word . ; create an array to store the results ; sort the array of strings ; compare the first string with its only right neighbor ; Store the unique prefix of a [ 1 ] from its left neighbor ; compute common prefix of a [ i ] unique from its right neighbor ; compare the new prefix with previous prefix ; store the prefix of a [ i + 1 ] unique from its left neighbour ; compute the unique prefix for the last string in sorted array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < string > uniquePrefix ( vector < string > & a ) { int size = a . size ( ) ; vector < string > res ( size ) ; sort ( a . begin ( ) , a . end ( ) ) ; int j = 0 ; while ( j < min ( a [ 0 ] . length ( ) - 1 , a [ 1 ] . length ( ) - 1 ) ) { if ( a [ 0 ] [ j ] == a [ 1 ] [ j ] ) j ++ ; else break ; } int ind = 0 ; res [ ind ++ ] = a [ 0 ] . substr ( 0 , j + 1 ) ; string temp_prefix = a [ 1 ] . substr ( 0 , j + 1 ) ; for ( int i = 1 ; i < size - 1 ; i ++ ) { j = 0 ; while ( j < min ( a [ i ] . length ( ) - 1 , a [ i + 1 ] . length ( ) - 1 ) ) { if ( a [ i ] [ j ] == a [ i + 1 ] [ j ] ) j ++ ; else break ; } string new_prefix = a [ i ] . substr ( 0 , j + 1 ) ; if ( temp_prefix . length ( ) > new_prefix . length ( ) ) res [ ind ++ ] = temp_prefix ; else res [ ind ++ ] = new_prefix ; temp_prefix = a [ i + 1 ] . substr ( 0 , j + 1 ) ; } j = 0 ; string sec_last = a [ size - 2 ] ; string last = a [ size - 1 ] ; while ( j < min ( sec_last . length ( ) - 1 , last . length ( ) - 1 ) ) { if ( sec_last [ j ] == last [ j ] ) j ++ ; else break ; } res [ ind ] = last . substr ( 0 , j + 1 ) ; return res ; } int main ( ) { vector < string > input = { " zebra " , " dog " , " duck " , " dove " } ; vector < string > output = uniquePrefix ( input ) ; cout << " The ▁ shortest ▁ unique ▁ prefixes ▁ in ▁ sorted ▁ order ▁ are ▁ : ▁ STRNEWLINE " ; for ( auto i : output ) cout << i << ' ▁ ' ; 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 minimum amount to buy all candies ; Buy current candy ; And take k candies for free from the last ; Function to find the maximum amount to buy all candies ; Buy candy with maximum amount ; And get k candies for free from the starting ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinimum ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += arr [ i ] ; n = n - k ; } return res ; } int findMaximum ( int arr [ ] , int n , int k ) { int res = 0 , index = 0 ; for ( int i = n - 1 ; i >= index ; i -- ) { res += arr [ i ] ; index += k ; } return res ; } int main ( ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; sort ( arr , arr + n ) ; cout << findMinimum ( arr , n , k ) << " ▁ " << findMaximum ( arr , n , k ) << endl ; return 0 ; }
Merge two sorted arrays | C ++ program to merge two sorted arrays using maps ; Function to merge arrays ; Declaring a map . using map as a inbuilt tool to store elements in sorted order . ; Inserting values to a map . ; Printing keys of the map . ; Driver Code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeArrays ( int a [ ] , int b [ ] , int n , int m ) { map < int , bool > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] = true ; for ( int i = 0 ; i < m ; i ++ ) mp [ b [ i ] ] = true ; for ( auto i : mp ) cout << i . first << " ▁ " ; } int main ( ) { int a [ ] = { 1 , 3 , 5 , 7 } , b [ ] = { 2 , 4 , 6 , 8 } ; int size = sizeof ( a ) / sizeof ( int ) ; int size1 = sizeof ( b ) / sizeof ( int ) ; mergeArrays ( a , b , size , size1 ) ; return 0 ; }
Print array of strings in sorted order without copying one string into another | C ++ implementation to print array of strings in sorted order without copying one string into another ; function to print strings in sorted order ; Initially the index of the strings are assigned to the ' index [ ] ' ; selection sort technique is applied ; with the help of ' index [ ] ' strings are being compared ; index of the smallest string is placed at the ith index of ' index [ ] ' ; printing strings in sorted order ; Driver program to test above
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printInSortedOrder ( string arr [ ] , int n ) { int index [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) index [ i ] = i ; for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . compare ( arr [ index [ j ] ] ) > 0 ) min = j ; } if ( min != i ) { int temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) cout << arr [ index [ i ] ] << " ▁ " ; } int main ( ) { string arr [ ] = { " geeks " , " quiz " , " geeks " , " for " } ; int n = 4 ; printInSortedOrder ( arr , n ) ; return 0 ; }
Find maximum height pyramid from the given array of objects | C ++ program to find maximum height pyramid from the given object width . ; Returns maximum number of pyramidcal levels n boxes of given widths . ; Sort objects in increasing order of widths ; Total width of previous level and total number of objects in previous level ; Number of object in current level . ; Width of current level . ; Picking the object . So increase current width and number of object . ; If current width and number of object are greater than previous . ; Update previous width , number of object on previous level . ; Reset width of current level , number of object on current level . ; Increment number of level . ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxLevel ( int boxes [ ] , int n ) { sort ( boxes , boxes + n ) ; int prev_width = boxes [ 0 ] ; int prev_count = 1 ; int curr_count = 0 ; int curr_width = 0 ; for ( int i = 1 ; i < n ; i ++ ) { curr_width += boxes [ i ] ; curr_count += 1 ; if ( curr_width > prev_width && curr_count > prev_count ) { prev_width = curr_width ; prev_count = curr_count ; curr_count = 0 ; curr_width = 0 ; ans ++ ; } } return ans ; } int main ( ) { int boxes [ ] = { 10 , 20 , 30 , 50 , 60 , 70 } ; int n = sizeof ( boxes ) / sizeof ( boxes [ 0 ] ) ; cout << maxLevel ( boxes , n ) << endl ; return 0 ; }
Find the largest multiple of 3 from array of digits | Set 2 ( In O ( n ) time and O ( 1 ) space ) | C ++ program to find the largest number that can be mode from elements of the array and is divisible by 3 ; Number of digits ; function to sort array of digits using counts ; Store count of all elements ; Store ; Remove elements from arr [ ] at indexes ind1 and ind2 ; Returns largest multiple of 3 that can be formed using arr [ ] elements . ; Sum of all array element ; Sum is divisible by 3 , no need to delete an element ; Sort array element in increasing order ; Find reminder ; If remainder is '1' , we have to delete either one element of remainder '1' or two elements of remainder '2' ; Traverse array elements ; Store first element of remainder '1' ; If this is first occurrence of remainder 2 ; If second occurrence ; If remainder is '2' , we have to delete either one element of remainder '2' or two elements of remainder '1' ; traverse array elements ; store first element of remainder '2' ; If this is first occurrence of remainder 1 ; If second occurrence ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_SIZE  10 NEW_LINE void sortArrayUsingCounts ( int arr [ ] , int n ) { int count [ MAX_SIZE ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int index = 0 ; for ( int i = 0 ; i < MAX_SIZE ; i ++ ) while ( count [ i ] > 0 ) arr [ index ++ ] = i , count [ i ] -- ; } bool removeAndPrintResult ( int arr [ ] , int n , int ind1 , int ind2 = -1 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) if ( i != ind1 && i != ind2 ) cout << arr [ i ] ; } bool largest3Multiple ( int arr [ ] , int n ) { int sum = accumulate ( arr , arr + n , 0 ) ; if ( sum % 3 == 0 ) return true ; sortArrayUsingCounts ( arr , n ) ; int remainder = sum % 3 ; if ( remainder == 1 ) { int rem_2 [ 2 ] ; rem_2 [ 0 ] = -1 , rem_2 [ 1 ] = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 1 ) { removeAndPrintResult ( arr , n , i ) ; return true ; } if ( arr [ i ] % 3 == 2 ) { if ( rem_2 [ 0 ] == -1 ) rem_2 [ 0 ] = i ; else if ( rem_2 [ 1 ] == -1 ) rem_2 [ 1 ] = i ; } } if ( rem_2 [ 0 ] != -1 && rem_2 [ 1 ] != -1 ) { removeAndPrintResult ( arr , n , rem_2 [ 0 ] , rem_2 [ 1 ] ) ; return true ; } } else if ( remainder == 2 ) { int rem_1 [ 2 ] ; rem_1 [ 0 ] = -1 , rem_1 [ 1 ] = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 2 ) { removeAndPrintResult ( arr , n , i ) ; return true ; } if ( arr [ i ] % 3 == 1 ) { if ( rem_1 [ 0 ] == -1 ) rem_1 [ 0 ] = i ; else if ( rem_1 [ 1 ] == -1 ) rem_1 [ 1 ] = i ; } } if ( rem_1 [ 0 ] != -1 && rem_1 [ 1 ] != -1 ) { removeAndPrintResult ( arr , n , rem_1 [ 0 ] , rem_1 [ 1 ] ) ; return true ; } } cout << " Not ▁ possible " ; return false ; } int main ( ) { int arr [ ] = { 4 , 4 , 1 , 1 , 1 , 3 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; largest3Multiple ( arr , n ) ; return 0 ; }
Minimum swaps to reach permuted array with at most 2 positions left swaps allowed | C ++ program to find minimum number of swaps to reach a permutation with at most 2 left swaps allowed for every element ; This funt merges two sorted arrays and returns inversion count in the arrays . ; i is index for left subarray ; j is index for right subarray ; k is index for resultant merged subarray ; Copy the remaining elements of left subarray ( if there are any ) to temp ; Copy the remaining elements of right subarray ( if there are any ) to temp ; Copy back the merged elements to original array ; An auxiliary recursive function that sorts the input array and returns the number of inversions in the array . ; Divide the array into two parts and call _mergeSortAndCountInv ( ) for each of the parts ; Inversion count will be sum of inversions in left - part , right - part and number of inversions in merging ; Merge the two parts ; This function sorts the input array and returns the number of inversions in the array ; method returns minimum number of swaps to reach permuted array ' arr ' ; loop over all elements to check Invalid permutation condition ; if an element is at distance more than 2 from its actual position then it is not possible to reach permuted array just by swapping with 2 positions left elements so returning - 1 ; If permuted array is not Invalid , then number of Inversion in array will be our final answer ; Driver code to test above methods ; change below example
#include <bits/stdc++.h> NEW_LINE using namespace std ; int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } int mergeSort ( int arr [ ] , int array_size ) { int * temp = ( int * ) malloc ( sizeof ( int ) * array_size ) ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } int minSwapToReachArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] - 1 ) - i > 2 ) return -1 ; } int numOfInversion = mergeSort ( arr , N ) ; return numOfInversion ; } int main ( ) { int arr [ ] = { 1 , 2 , 5 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( int ) ; int res = minSwapToReachArr ( arr , N ) ; if ( res == -1 ) cout << " Not ▁ Possible STRNEWLINE " ; else cout << res << endl ; return 0 ; }
Sort all even numbers in ascending order and then sort all odd numbers in descending order | C ++ program sort array in even and odd manner . The odd numbers are to be sorted in descending order and the even numbers in ascending order ; To do two way sort . First sort even numbers in ascending order , then odd numbers in descending order . ; Make all odd numbers negative ; if ( arr [ i ] & 1 ) Check for odd ; Sort all numbers ; Retaining original array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] *= -1 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= -1 ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; twoWaySort ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Sort all even numbers in ascending order and then sort all odd numbers in descending order | C ++ implementation of the approach ; Utility function to print the contents of the array ; To do two way sort . Make comparator function for the inbuilt sort function of c ++ such that odd numbers are placed before even in descending and ascending order respectively ; If both numbers are even , smaller number should be placed at lower index ; If both numbers are odd larger number should be placed at lower index ; If a is odd and b is even , a should be placed before b ; If b is odd and a is even , b should be placed before a ; Driver code ; Sort the array ; Print the sorted array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } bool compare ( int a , int b ) { if ( a % 2 == 0 && b % 2 == 0 ) return a < b ; if ( a % 2 != 0 && b % 2 != 0 ) return b < a ; if ( a % 2 != 0 ) return true ; return false ; } int main ( ) { int arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; int n = sizeof ( arr ) / sizeof ( int ) ; sort ( arr , arr + n , compare ) ; printArr ( arr , n ) ; return 0 ; }
Possible to form a triangle from array values | C ++ program to find if it is possible to form a triangle from array values ; Method prints possible triangle when array values are taken as sides ; If number of elements are less than 3 , then no triangle is possible ; first sort the array ; loop for all 3 consecutive triplets ; If triplet satisfies triangle condition , break ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPossibleTriangle ( int arr [ ] , int N ) { if ( N < 3 ) return false ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; return false ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 1 , 2 } ; int N = sizeof ( arr ) / sizeof ( int ) ; isPossibleTriangle ( arr , N ) ? cout << " Yes " : cout << " No " ; return 0 ; }
Bucket Sort To Sort an Array with Negative Numbers | C ++ program to sort an array of positive and negative numbers using bucket sort ; Function to sort arr [ ] of size n using bucket sort ; 1 ) Create n empty buckets ; 2 ) Put array elements in different buckets ; int bi = n * arr [ i ] ; Index in bucket ; 3 ) Sort individual buckets ; 4 ) Concatenate all buckets into arr [ ] ; This function mainly splits array into two and then calls bucketSort ( ) for two arrays . ; traverse array elements ; store - Ve elements by converting into + ve element ; store + ve elements ; First store elements of Neg [ ] array by converting into - ve ; store + ve element ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bucketSort ( vector < float > & arr , int n ) { vector < float > b [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { b [ bi ] . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) sort ( b [ i ] . begin ( ) , b [ i ] . end ( ) ) ; int index = 0 ; arr . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < b [ i ] . size ( ) ; j ++ ) arr . push_back ( b [ i ] [ j ] ) ; } void sortMixed ( float arr [ ] , int n ) { vector < float > Neg ; vector < float > Pos ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) Neg . push_back ( -1 * arr [ i ] ) ; else Pos . push_back ( arr [ i ] ) ; } bucketSort ( Neg , ( int ) Neg . size ( ) ) ; bucketSort ( Pos , ( int ) Pos . size ( ) ) ; for ( int i = 0 ; i < Neg . size ( ) ; i ++ ) arr [ i ] = -1 * Neg [ Neg . size ( ) - 1 - i ] ; for ( int j = Neg . size ( ) ; j < n ; j ++ ) arr [ j ] = Pos [ j - Neg . size ( ) ] ; } int main ( ) { float arr [ ] = { -0.897 , 0.565 , 0.656 , -0.1234 , 0 , 0.3434 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortMixed ( arr , n ) ; cout << " Sorted ▁ array ▁ is ▁ STRNEWLINE " ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
K | C ++ program to find the K - th smallest element after removing some integers from natural number . ; Return the K - th smallest element . ; Making an array , and mark all number as unmarked . ; Marking the number present in the given array . ; If j is unmarked , reduce k by 1. ; If k is 0 return j . ; Driven Program
#include <bits/stdc++.h> NEW_LINE #define MAX  1000000 NEW_LINE using namespace std ; int ksmallest ( int arr [ ] , int n , int k ) { int b [ MAX ] ; memset ( b , 0 , sizeof b ) ; for ( int i = 0 ; i < n ; i ++ ) b [ arr [ i ] ] = 1 ; for ( int j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) k -- ; if ( ! k ) return j ; } } int main ( ) { int k = 1 ; int arr [ ] = { 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << ksmallest ( arr , n , k ) ; return 0 ; }
Sort an array when two halves are sorted | C ++ program to Merge two sorted halves of array Into Single Sorted Array ; Sort the given array using sort STL ; Driver code ; Print sorted Array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeTwoHalf ( int A [ ] , int n ) { sort ( A , A + n ) ; } int main ( ) { int A [ ] = { 2 , 3 , 8 , -1 , 7 , 10 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; mergeTwoHalf ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << A [ i ] << " ▁ " ; return 0 ; }
Choose k array elements such that difference of maximum and minimum is minimized | C ++ program to find minimum difference of maximum and minimum of K number . ; Return minimum difference of maximum and minimum of k elements of arr [ 0. . n - 1 ] . ; Sorting the array . ; Find minimum value among all K size subarray . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minDiff ( int arr [ ] , int n , int k ) { int result = INT_MAX ; sort ( arr , arr + n ) ; for ( int i = 0 ; i <= n - k ; i ++ ) result = min ( result , arr [ i + k - 1 ] - arr [ i ] ) ; return result ; } int main ( ) { int arr [ ] = { 10 , 100 , 300 , 200 , 1000 , 20 , 30 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; cout << minDiff ( arr , n , k ) << endl ; return 0 ; }
Sort even | Program to separately sort even - placed and odd placed numbers and place them together in sorted array . ; create evenArr [ ] and oddArr [ ] ; Put elements in oddArr [ ] and evenArr [ ] as per their position ; sort evenArr [ ] in ascending order sort oddArr [ ] in descending order ; Driver Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void bitonicGenerator ( int arr [ ] , int n ) { vector < int > evenArr ; vector < int > oddArr ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( i % 2 ) ) evenArr . push_back ( arr [ i ] ) ; else oddArr . push_back ( arr [ i ] ) ; } sort ( evenArr . begin ( ) , evenArr . end ( ) ) ; sort ( oddArr . begin ( ) , oddArr . end ( ) , greater < int > ( ) ) ; int i = 0 ; for ( int j = 0 ; j < evenArr . size ( ) ; j ++ ) arr [ i ++ ] = evenArr [ j ] ; for ( int j = 0 ; j < oddArr . size ( ) ; j ++ ) arr [ i ++ ] = oddArr [ j ] ; } int main ( ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; bitonicGenerator ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }