codeparrot/xlcost-text-to-code huggingface dataset:
Count number of triplets with product equal to given number | Set 2 | C ++ implementation of above approach ; Function to count such triplets ; Sort the array ; three pointer technique ; Calculate the product of a triplet ; Check if that product is greater than m , decrement mid ; Check if that product is smaller than m , increment start ; Check if that product is equal to m , decrement mid , increment start and increment the count of pairs ; Drivers code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countTriplets ( int arr [ ] , int n , int m ) { int count = 0 ; sort ( arr , arr + n ) ; int end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { int start = 0 , mid = end - 1 ; while ( start < mid ) { long int prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int m = 1 ; cout << countTriplets ( arr , n , m ) ; return 0 ; }
Equally divide into two sets such that one set has maximum distinct elements | C ++ program to equally divide n elements into two sets such that second set has maximum distinct elements . ; Insert all the resources in the set There will be unique resources in the set ; return minimum of distinct resources and n / 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distribution ( int arr [ ] , int n ) { set < int , greater < int > > resources ; for ( int i = 0 ; i < n ; i ++ ) resources . insert ( arr [ i ] ) ; int m = resources . size ( ) ; return min ( m , n / 2 ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << distribution ( arr , n ) << endl ; return 0 ; }
Sort 3 numbers | C ++ program to sort an array of size 3 ; Insert arr [ 1 ] ; Insert arr [ 2 ] ; Driver Code
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; int sort3 ( int arr [ ] ) { if ( arr [ 1 ] < arr [ 0 ] ) swap ( arr [ 0 ] , arr [ 1 ] ) ; if ( arr [ 2 ] < arr [ 1 ] ) { swap ( arr [ 1 ] , arr [ 2 ] ) ; if ( arr [ 1 ] < arr [ 0 ] ) swap ( arr [ 1 ] , arr [ 0 ] ) ; } } int main ( ) { int a [ ] = { 10 , 12 , 5 } ; sort3 ( a ) ; for ( int i = 0 ; i < 3 ; i ++ ) cout << a [ i ] << " ▁ " ; return 0 ; }
Merge Sort with O ( 1 ) extra space merge and O ( n lg n ) time [ Unsigned Integers Only ] | C ++ program to sort an array using merge sort such that merge operation takes O ( 1 ) extra space . ; Obtaining actual values ; Recursive merge sort with extra parameter , naxele ; This functions finds max element and calls recursive merge sort . ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void merge ( int arr [ ] , int beg , int mid , int end , int maxele ) { int i = beg ; int j = mid + 1 ; int k = beg ; while ( i <= mid && j <= end ) { if ( arr [ i ] % maxele <= arr [ j ] % maxele ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } else { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } } while ( i <= mid ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } while ( j <= end ) { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } for ( int i = beg ; i <= end ; i ++ ) arr [ i ] = arr [ i ] / maxele ; } void mergeSortRec ( int arr [ ] , int beg , int end , int maxele ) { if ( beg < end ) { int mid = ( beg + end ) / 2 ; mergeSortRec ( arr , beg , mid , maxele ) ; mergeSortRec ( arr , mid + 1 , end , maxele ) ; merge ( arr , beg , mid , end , maxele ) ; } } void mergeSort ( int arr [ ] , int n ) { int maxele = * max_element ( arr , arr + n ) + 1 ; mergeSortRec ( arr , 0 , n - 1 , maxele ) ; } int main ( ) { int arr [ ] = { 999 , 612 , 589 , 856 , 56 , 945 , 243 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; mergeSort ( arr , n ) ; cout << " Sorted ▁ array ▁ STRNEWLINE " ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Print triplets with sum less than k | C ++ program to print triplets with sum smaller than a given value ; Sort input array ; Every iteration of loop counts triplet with first element as arr [ i ] . ; Initialize other two elements as corner elements of subarray arr [ j + 1. . k ] ; Use Meet in the Middle concept ; If sum of current triplet is more or equal , move right corner to look for smaller values ; Else move left corner ; This is important . For current i and j , there are total k - j third elements . ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printTriplets ( int arr [ ] , int n , int sum ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { for ( int x = j + 1 ; x <= k ; x ++ ) cout << arr [ i ] << " , ▁ " << arr [ j ] << " , ▁ " << arr [ x ] << endl ; j ++ ; } } } } int main ( ) { int arr [ ] = { 5 , 1 , 3 , 4 , 7 } ; int n = sizeof arr / sizeof arr [ 0 ] ; int sum = 12 ; printTriplets ( arr , n , sum ) ; return 0 ; }
Sort string of characters using Stack | C ++ program to sort string of characters using stack ; Function to print the characters in sorted order ; Primary stack ; Secondary stack ; Append first character ; Iterate for all character in string ; i - th character ASCII ; stack 's top element ASCII  ; If greater or equal to top element then push to stack ; If smaller , then push all element to the temporary stack ; Push all greater elements ; Push operation ; Push till the stack is not - empty ; Push the i - th character ; Push the tempstack back to stack ; Print the stack in reverse order ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printSorted ( string s , int l ) { stack < char > Stack ; stack < char > tempstack ; Stack . push ( s [ 0 ] ) ; for ( int i = 1 ; i < l ; i ++ ) { int a = s [ i ] ; int b = Stack . top ( ) ; if ( ( a - b ) >= 1 or ( a == b ) ) Stack . push ( s [ i ] ) ; else if ( ( b - a ) >= 1 ) { while ( ( b - a ) >= 1 ) { tempstack . push ( Stack . top ( ) ) ; Stack . pop ( ) ; if ( Stack . size ( ) > 0 ) b = Stack . top ( ) ; else break ; } Stack . push ( s [ i ] ) ; while ( tempstack . size ( ) > 0 ) { Stack . push ( tempstack . top ( ) ) ; tempstack . pop ( ) ; } } } string answer ; while ( Stack . size ( ) > 0 ) { answer = Stack . top ( ) + answer ; Stack . pop ( ) ; } cout << answer << endl ; } int main ( ) { string s = " geeksforgeeks " ; int l = s . length ( ) ; printSorted ( s , l ) ; return 0 ; }
Check whether an array can be fit into another array rearranging the elements in the array | C ++ Program to check whether an array can be fit into another array with given condition . ; Returns true if the array A can be fit into array B , otherwise false ; Sort both the arrays ; Iterate over the loop and check whether every array element of A is less than or equal to its corresponding array element of B ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool checkFittingArrays ( int A [ ] , int B [ ] , int N ) { sort ( A , A + N ) ; sort ( B , B + N ) ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] > B [ i ] ) return false ; return true ; } int main ( ) { int A [ ] = { 7 , 5 , 3 , 2 } ; int B [ ] = { 5 , 4 , 8 , 7 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( checkFittingArrays ( A , B , N ) ) cout << " YES " ; else cout << " NO " ; return 0 ; }
Maximise the number of toys that can be purchased with amount K | C ++ Program to maximize the number of toys with K amount ; This functions returns the required number of toys ; sort the cost array ; Check if we can buy ith toy or not ; Increment count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximum_toys ( int cost [ ] , int N , int K ) { int count = 0 , sum = 0 ; sort ( cost , cost + N ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( sum + cost [ i ] <= K ) { sum = sum + cost [ i ] ; count ++ ; } } return count ; } int main ( ) { int K = 50 ; int cost [ ] = { 1 , 12 , 5 , 111 , 200 , 1000 , 10 , 9 , 12 , 15 } ; int N = sizeof ( cost ) / sizeof ( cost [ 0 ] ) ; cout << maximum_toys ( cost , N , K ) << endl ; return 0 ; }
Find if k bookings possible with given arrival and departure times | C ++ code implementation of the above approach ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string areBookingsPossible ( int A [ ] , int B [ ] , int K , int N ) { sort ( A , A + N ) ; sort ( B , B + N ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( i + K < N && A [ i + K ] < B [ i ] ) { return " No " ; } } return " Yes " ; } int main ( ) { int arrival [ ] = { 1 , 2 , 3 } ; int departure [ ] = { 2 , 3 , 4 } ; int N = sizeof ( arrival ) / sizeof ( arrival [ 0 ] ) ; int K = 1 ; cout << ( areBookingsPossible ( arrival , departure , K , N ) ) ; return 0 ; }
Insertion Sort by Swapping Elements | Recursive CPP program to sort an array by swapping elements ; Utility function to print a Vector ; Function to perform Insertion Sort recursively ; General Case Sort V till second last element and then insert last element into V ; Insertion step ; Insert V [ i ] into list 0. . i - 1 ; Swap V [ j ] and V [ j - 1 ] ; Decrement j ; Driver Code
#include <iostream> NEW_LINE #include <vector> NEW_LINE using namespace std ; using Vector = vector < int > ; void printVector ( const Vector & V ) { for ( auto e : V ) { cout << e << " ▁ " ; } cout << endl ; } void insertionSortRecursive ( Vector & V , int N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; int j = N - 1 ; while ( j > 0 and V [ j ] < V [ j - 1 ] ) { swap ( V [ j ] , V [ j - 1 ] ) ; j -= 1 ; } } int main ( ) { Vector A = { 9 , 8 , 7 , 5 , 2 , 1 , 2 , 3 } ; cout << " Array : ▁ " << endl ; printVector ( A ) ; cout << " After ▁ Sorting ▁ : " << endl ; insertionSortRecursive ( A , A . size ( ) ) ; printVector ( A ) ; return 0 ; }
Check if given array is almost sorted ( elements are at | CPP program to find whether given array almost sorted or not ; function for checking almost sort ; One by one compare adjacents . ; check whether resultant is sorted or not ; is resultant is sorted return true ; driver function
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool almostSort ( int A [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { swap ( A [ i ] , A [ i + 1 ] ) ; i ++ ; } } for ( int i = 0 ; i < n - 1 ; i ++ ) if ( A [ i ] > A [ i + 1 ] ) return false ; return true ; } int main ( ) { int A [ ] = { 1 , 3 , 2 , 4 , 6 , 5 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( almostSort ( A , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Efficiently merging two sorted arrays with O ( 1 ) extra space | Merging two sorted arrays with O ( 1 ) extra space ; Function to find next gap . ; comparing elements in the first array . ; comparing elements in both arrays . ; comparing elements in the second array . ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } void merge ( int * arr1 , int * arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) swap ( arr1 [ i ] , arr1 [ i + gap ] ) ; for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) swap ( arr1 [ i ] , arr2 [ j ] ) ; if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) swap ( arr2 [ j ] , arr2 [ j + gap ] ) ; } } } int main ( ) { int a1 [ ] = { 10 , 27 , 38 , 43 , 82 } ; int a2 [ ] = { 3 , 9 } ; int n = sizeof ( a1 ) / sizeof ( int ) ; int m = sizeof ( a2 ) / sizeof ( int ) ; merge ( a1 , a2 , n , m ) ; printf ( " First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) printf ( " % d ▁ " , a1 [ i ] ) ; printf ( " Second Array : " for ( int i = 0 ; i < m ; i ++ ) printf ( " % d ▁ " , a2 [ i ] ) ; printf ( " STRNEWLINE " ) ; return 0 ; }
Merge two sorted arrays | C ++ program to merge two sorted arrays / ; Merge arr1 [ 0. . n1 - 1 ] and arr2 [ 0. . n2 - 1 ] into arr3 [ 0. . n1 + n2 - 1 ] ; Traverse both array ; Check if current element of first array is smaller than current element of second array . If yes , store first array element and increment first array index . Otherwise do same with second array ; Store remaining elements of first array ; Store remaining elements of second array ; Driver code
#include <iostream> NEW_LINE using namespace std ; void mergeArrays ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 , int arr3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } int main ( ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int arr2 [ ] = { 2 , 4 , 6 , 8 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int arr3 [ n1 + n2 ] ; mergeArrays ( arr1 , arr2 , n1 , n2 , arr3 ) ; cout << " Array ▁ after ▁ merging " << endl ; for ( int i = 0 ; i < n1 + n2 ; i ++ ) cout << arr3 [ i ] << " ▁ " ; return 0 ; }
Sort array after converting elements to their squares | C ++ program to Sort square of the numbers of the array ; Function to sort an square array ; First convert each array elements into its square ; Sort an array using " sort ▁ STL ▁ function ▁ " ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortSquares ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; sort ( arr , arr + n ) ; } int main ( ) { int arr [ ] = { -6 , -3 , -1 , 2 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Before ▁ sort ▁ " << endl ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; sortSquares ( arr , n ) ; cout << " After Sort " for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Sort an array when two halves are sorted | C ++ program to Merge Two Sorted Halves Of Array Into Single Sorted Array ; Merge two sorted halves of Array into single sorted array ; Temp Array store sorted resultant array ; First Find the point where array is divide into two half ; If Given array is all - ready sorted ; Merge two sorted arrays in single sorted array ; Copy the remaining elements of A [ i to half_ ! ] ; Copy the remaining elements of A [ half_ ! to n ] ; Driver code ; Print sorted Array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeTwoHalf ( int A [ ] , int n ) { int temp [ n ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { half_i = i + 1 ; break ; } } if ( half_i == 0 ) return ; int i = 0 , j = half_i , k = 0 ; while ( i < half_i && j < n ) { if ( A [ i ] < A [ j ] ) temp [ k ++ ] = A [ i ++ ] ; else temp [ k ++ ] = A [ j ++ ] ; } while ( i < half_i ) temp [ k ++ ] = A [ i ++ ] ; while ( j < n ) temp [ k ++ ] = A [ j ++ ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = temp [ i ] ; } 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 ; }
Chocolate Distribution Problem | C ++ program to solve chocolate distribution problem ; arr [ 0. . n - 1 ] represents sizes of packets m is number of students . Returns minimum difference between maximum and minimum values of distribution . ; if there are no chocolates or number of students is 0 ; Sort the given packets ; Number of students cannot be more than number of packets ; Largest number of chocolates ; Find the subarray of size m such that difference between last ( maximum in case of sorted ) and first ( minimum in case of sorted ) elements of subarray is minimum . ; Driver program to test above function ; int m = 7 ; Number of students
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMinDiff ( int arr [ ] , int n , int m ) { if ( m == 0 n == 0 ) return 0 ; sort ( arr , arr + n ) ; if ( n < m ) return -1 ; int min_diff = INT_MAX ; for ( int i = 0 ; i + m - 1 < n ; i ++ ) { int diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } int main ( ) { int arr [ ] = { 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Minimum ▁ difference ▁ is ▁ " << findMinDiff ( arr , n , m ) ; return 0 ; }
Absolute distinct count in a sorted array | C ++ program to find absolute distinct count of an array in O ( n ) time . ; The function returns number of distinct absolute values among the elements of the array ; Note that set keeps only one copy even if we try to insert multiple values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctCount ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( abs ( arr [ i ] ) ) ; return s . size ( ) ; } int main ( ) { int arr [ ] = { -2 , -1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " << distinctCount ( arr , n ) ; return 0 ; }
Absolute distinct count in a sorted array | C ++ program to find absolute distinct count of an array using O ( 1 ) space . ; The function returns return number of distinct absolute values among the elements of the array ; initialize count as number of elements ; Remove duplicate elements from the left of the current window ( i , j ) and also decrease the count ; Remove duplicate elements from the right of the current window ( i , j ) and also decrease the count ; break if only one element is left ; Now look for the zero sum pair in current window ( i , j ) ; decrease the count if ( positive , negative ) pair is encountered ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int distinctCount ( int arr [ ] , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) count -- , i ++ ; while ( i != j && arr [ j ] == arr [ j - 1 ] ) count -- , j -- ; if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ , j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } int main ( ) { int arr [ ] = { -2 , -1 , 0 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " << distinctCount ( arr , n ) ; return 0 ; }
Sorting Strings using Bubble Sort | C ++ implementation ; Sorting strings using bubble sort ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX  100 NEW_LINE void sortStrings ( char arr [ ] [ MAX ] , int n ) { char temp [ MAX ] ; for ( int j = 0 ; j < n - 1 ; j ++ ) { for ( int i = j + 1 ; i < n ; i ++ ) { if ( strcmp ( arr [ j ] , arr [ i ] ) > 0 ) { strcpy ( temp , arr [ j ] ) ; strcpy ( arr [ j ] , arr [ i ] ) ; strcpy ( arr [ i ] , temp ) ; } } } } int main ( ) { char arr [ ] [ MAX ] = { " GeeksforGeeks " , " Quiz " , " Practice " , " Gblogs " , " Coding " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortStrings ( arr , n ) ; printf ( " Strings ▁ in ▁ sorted ▁ order ▁ are ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) printf ( " String % d is % s " , i + 1 , arr [ i ] ) ; return 0 ; }
Sort an almost sorted array where only two elements are swapped | C ++ program to sort using one swap ; This function sorts an array that can be sorted by single swap ; Traverse the given array from rightmost side ; Check if arr [ i ] is not in order ; Find the other element to be swapped with arr [ i ] ; Swap the pair ; A utility function to print an array of size n ; Driver program to test insertion sort
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; void sortByOneSwap ( int arr [ ] , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] < arr [ i - 1 ] ) { int j = i - 1 ; while ( j >= 0 && arr [ i ] < arr [ j ] ) j -- ; swap ( arr [ i ] , arr [ j + 1 ] ) ; break ; } } } void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int arr [ ] = { 10 , 30 , 20 , 40 , 50 , 60 , 70 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Given ▁ array ▁ is ▁ STRNEWLINE " ; printArray ( arr , n ) ; sortByOneSwap ( arr , n ) ; cout << " Sorted ▁ array ▁ is ▁ STRNEWLINE " ; printArray ( arr , n ) ; return 0 ; }
Pancake sorting | C ++ program to sort array using pancake sort ; Reverses arr [ 0. . i ] ; Returns index of the maximum element in arr [ 0. . n - 1 ] ; The main function that sorts given array using flip operations ; Start from the complete array and one by one reduce current size by one ; Find index of the maximum element in arr [ 0. . curr_size - 1 ] ; Move the maximum element to end of current array if it 's not already  at the end ; To move at the end , first move maximum number to beginning ; Now move the maximum number to end by reversing current array ; A utility function to print n array of size n ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void flip ( int arr [ ] , int i ) { int temp , start = 0 ; while ( start < i ) { temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i -- ; } } int findMax ( int arr [ ] , int n ) { int mi , i ; for ( mi = 0 , i = 0 ; i < n ; ++ i ) if ( arr [ i ] > arr [ mi ] ) mi = i ; return mi ; } void pancakeSort ( int * arr , int n ) { for ( int curr_size = n ; curr_size > 1 ; -- curr_size ) { int mi = findMax ( arr , curr_size ) ; if ( mi != curr_size - 1 ) { flip ( arr , mi ) ; flip ( arr , curr_size - 1 ) ; } } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 23 , 10 , 20 , 11 , 12 , 6 , 7 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; pancakeSort ( arr , n ) ; cout << " Sorted ▁ Array ▁ " << endl ; printArray ( arr , n ) ; return 0 ; }
Lexicographically smallest numeric string having odd digit counts | C ++ program for the above approach ; Function to construct lexicographically smallest numeric string having an odd count of each characters ; Stores the resultant string ; If N is even ; Otherwise ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string genString ( int N ) { string ans = " " ; if ( N % 2 == 0 ) { ans = string ( N - 1 , '1' ) + '2' ; } else { ans = string ( N , '1' ) ; } return ans ; } int main ( ) { int N = 5 ; cout << genString ( N ) ; return 0 ; }
Modify given string such that odd and even indices is lexicographically largest and smallest | C ++ program for the above approach ; Function to modify the given string satisfying the given criteria ; Traverse the string S ; If i is even ; If the S [ i ] is ' a ' , then change S [ i ] to ' b ' ; Otherwise , change S [ i ] to ' a ' ; If S [ i ] is ' z ' , then change S [ i ] to ' y ' ; Otherwise , change S [ i ] to ' z ' ; Return the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string performOperation ( string S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S [ i ] = ' b ' ; } else { S [ i ] = ' a ' ; } } else { if ( S [ i ] == ' z ' ) { S [ i ] = ' y ' ; } else { S [ i ] = ' z ' ; } } } return S ; } int main ( ) { string S = " giad " ; int N = S . size ( ) ; cout << performOperation ( S , N ) ; return 0 ; }