codeparrot/xlcost-text-to-code huggingface dataset:
Sort an array using Bubble Sort without using loops | C ++ program for the above approach ; Function to implement bubble sort without using loops ; Base Case : If array contains a single element ; Base Case : If array contains two elements ; Store the first two elements of the list in variables a and b ; Store remaining elements in the list bs ; Store the list after each recursive call ; If a < b ; Otherwise , if b >= a ; Recursively call for the list less than the last element and and return the newly formed list ; Driver Code ; Print the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > bubble_sort ( vector < int > ar ) { if ( ar . size ( ) <= 1 ) return ar ; if ( ar . size ( ) == 2 ) { if ( ar [ 0 ] < ar [ 1 ] ) return ar ; else return { ar [ 1 ] , ar [ 0 ] } ; } int a = ar [ 0 ] ; int b = ar [ 1 ] ; vector < int > bs ; for ( int i = 2 ; i < ar . size ( ) ; i ++ ) bs . push_back ( ar [ i ] ) ; vector < int > res ; if ( a < b ) { vector < int > temp1 ; temp1 . push_back ( b ) ; for ( int i = 0 ; i < bs . size ( ) ; i ++ ) temp1 . push_back ( bs [ i ] ) ; vector < int > v = bubble_sort ( temp1 ) ; v . insert ( v . begin ( ) , a ) ; res = v ; } else { vector < int > temp1 ; temp1 . push_back ( a ) ; for ( int i = 0 ; i < bs . size ( ) ; i ++ ) temp1 . push_back ( bs [ i ] ) ; vector < int > v = bubble_sort ( temp1 ) ; v . insert ( v . begin ( ) , b ) ; res = v ; } vector < int > pass ; for ( int i = 0 ; i < res . size ( ) - 1 ; i ++ ) pass . push_back ( res [ i ] ) ; vector < int > ans = bubble_sort ( pass ) ; ans . push_back ( res [ res . size ( ) - 1 ] ) ; return ans ; } int main ( ) { vector < int > arr { 1 , 3 , 4 , 5 , 6 , 2 } ; vector < int > res = bubble_sort ( arr ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; }
Modify a given matrix by placing sorted boundary elements in clockwise manner | C ++ program for the above approach ; Function to print the elements of the matrix in row - wise manner ; Function to sort boundary elements of a matrix starting from the outermost to the innermost boundary and place them in a clockwise manner ; k - starting row index m - ending row index l - starting column index n - ending column index i - iterator ; Stores the current boundary elements ; Push the first row ; Push the last column ; Push the last row ; Push the first column ; Sort the boundary elements ; Update the first row ; Update the last column ; Update the last row ; Update the first column ; Print the resultant matrix ; Driver Code ; Given matrix
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printMatrix ( vector < vector < int > > a ) { for ( auto x : a ) { for ( auto y : x ) { cout << y << " ▁ " ; } cout << " STRNEWLINE " ; } } void sortBoundaryWise ( vector < vector < int > > a ) { int i , k = 0 , l = 0 ; int m = a . size ( ) , n = a [ 0 ] . size ( ) ; int n_i , n_k = 0 , n_l = 0 , n_m = m , n_n = n ; while ( k < m && l < n ) { vector < int > boundary ; for ( i = l ; i < n ; ++ i ) { boundary . push_back ( a [ k ] [ i ] ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { boundary . push_back ( a [ i ] [ n - 1 ] ) ; } n -- ; if ( k < m ) { for ( i = n - 1 ; i >= l ; -- i ) { boundary . push_back ( a [ m - 1 ] [ i ] ) ; } m -- ; } if ( l < n ) { for ( i = m - 1 ; i >= k ; -- i ) { boundary . push_back ( a [ i ] [ l ] ) ; } l ++ ; } sort ( boundary . begin ( ) , boundary . end ( ) ) ; int ind = 0 ; for ( i = n_l ; i < n_n ; ++ i ) { a [ n_k ] [ i ] = boundary [ ind ++ ] ; } n_k ++ ; for ( i = n_k ; i < n_m ; ++ i ) { a [ i ] [ n_n - 1 ] = boundary [ ind ++ ] ; } n_n -- ; if ( n_k < n_m ) { for ( i = n_n - 1 ; i >= n_l ; -- i ) { a [ n_m - 1 ] [ i ] = boundary [ ind ++ ] ; } n_m -- ; } if ( n_l < n_n ) { for ( i = n_m - 1 ; i >= n_k ; -- i ) { a [ i ] [ n_l ] = boundary [ ind ++ ] ; } n_l ++ ; } } printMatrix ( a ) ; } int main ( ) { vector < vector < int > > matrix = { { 9 , 7 , 4 , 5 } , { 1 , 6 , 2 , -6 } , { 12 , 20 , 2 , 0 } , { -5 , -6 , 7 , -2 } } ; sortBoundaryWise ( matrix ) ; return 0 ; }
Minimum sum of absolute differences between pairs of a triplet from an array | C ++ Program for the above approach ; Function to find minimum sum of absolute differences of pairs of a triplet ; Sort the array ; Stores the minimum sum ; Traverse the array ; Update the minimum sum ; Print the minimum sum ; Driver Code ; Input ; Function call to find minimum sum of absolute differences of pairs in a triplet
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimum_sum ( int A [ ] , int N ) { sort ( A , A + N ) ; int sum = INT_MAX ; for ( int i = 0 ; i <= N - 3 ; i ++ ) { sum = min ( sum , abs ( A [ i ] - A [ i + 1 ] ) + abs ( A [ i + 1 ] - A [ i + 2 ] ) ) ; } cout << sum ; } int main ( ) { int A [ ] = { 1 , 1 , 2 , 3 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; minimum_sum ( A , N ) ; return 0 ; }
Make all characters of a string same by minimum number of increments or decrements of ASCII values of characters | C ++ program for the above approach ; Function to check if all characters of the string can be made the same ; Sort the string ; Calculate ASCII value of the median character ; Stores the minimum number of operations required to make all characters equal ; Traverse the string ; Calculate absolute value of current character and median character ; Print the minimum number of operations required ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sameChar ( string S , int N ) { sort ( S . begin ( ) , S . end ( ) ) ; int mid = S [ N / 2 ] ; int total_operations = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total_operations += abs ( int ( S [ i ] ) - mid ) ; } cout << total_operations ; } int main ( ) { string S = " geeks " ; int N = S . size ( ) ; sameChar ( S , N ) ; return 0 ; }
Maximum score possible from an array with jumps of at most length K | C ++ program for the above approach ; Function to count the maximum score of an index ; Base Case ; If the value for the current index is pre - calculated ; Calculate maximum score for all the steps in the range from i + 1 to i + k ; Score for index ( i + j ) ; Update dp [ i ] and return the maximum value ; Function to get maximum score possible from the array A [ ] ; Array to store memoization ; Initialize dp [ ] with - 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxScore ( int i , int A [ ] , int K , int N , int dp [ ] ) { if ( i >= N - 1 ) return A [ N - 1 ] ; if ( dp [ i ] != -1 ) return dp [ i ] ; int score = INT_MIN ; for ( int j = 1 ; j <= K ; j ++ ) { score = max ( score , maxScore ( i + j , A , K , N , dp ) ) ; } return dp [ i ] = score + A [ i ] ; } int getScore ( int A [ ] , int N , int K ) { int dp [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] = -1 ; cout << maxScore ( 0 , A , K , N , dp ) ; } int main ( ) { int A [ ] = { 100 , -30 , -50 , -15 , -20 , -30 } ; int K = 3 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; getScore ( A , N , K ) ; return 0 ; }
Maximum score possible from an array with jumps of at most length K | C ++ program for the above approach ; Structure to sort a priority queue on the basis of first element of the pair ; Function to calculate maximum score possible from the array A [ ] ; Stores the score of previous k indices ; Stores the maximum score for current index ; Maximum score at first index ; Traverse the array to calculate maximum score for all indices ; Remove maximum scores for indices less than i - K ; Calculate maximum score for current index ; Push maximum score of current index along with index in maxheap ; Return the maximum score ; Driver Code ; Function call to calculate maximum score from the array A [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct mycomp { bool operator() ( pair < int , int > p1 , pair < int , int > p2 ) { return p1 . first < p2 . first ; } } ; int maxScore ( int A [ ] , int K , int N ) { priority_queue < pair < int , int > , vector < pair < int , int > > , mycomp > maxheap ; int maxScore = 0 ; maxheap . push ( { A [ 0 ] , 0 } ) ; for ( int i = 1 ; i < N ; i ++ ) { while ( maxheap . top ( ) . second < ( i - K ) ) { maxheap . pop ( ) ; } maxScore = A [ i ] + maxheap . top ( ) . first ; maxheap . push ( { maxScore , i } ) ; } return maxScore ; } int main ( ) { int A [ ] = { -44 , -17 , -54 , 79 } ; int K = 2 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << maxScore ( A , K , N ) ; return 0 ; }
Count triplets from an array which can form quadratic equations with real roots | C ++ program for the above approach ; Function to find count of triplets ( a , b , c ) such that the equations ax ^ 2 + bx + c = 0 has real roots ; Stores count of triplets ( a , b , c ) such that ax ^ 2 + bx + c = 0 has real roots ; Base case ; Generate all possible triplets ( a , b , c ) ; If the coefficient of X ^ 2 and X are equal ; If coefficient of X ^ 2 or x are equal to the constant ; Condition for having real roots ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getCount ( int arr [ ] , int N ) { int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { for ( int a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( int c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; int d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getCount ( arr , N ) ; return 0 ; }
Check if all array elements can be reduced to less than X | C ++ program to implement the above approach ; Function to check if all array elements can be reduced to less than X or not ; Checks if all array elements are already a X or not ; Traverse every possible pair ; Calculate GCD of two array elements ; If gcd is a 1 ; If gcd is a X , then a pair is present to reduce all array elements to a X ; If no pair is present with gcd is a X ; Function to check if all array elements area X ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool findAns ( int A [ ] , int N , int X ) { if ( check ( A , X , N ) ) { return true ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int g = __gcd ( A [ i ] , A [ j ] ) ; if ( g != 1 ) { if ( g <= X ) { return true ; } } } } return false ; } bool check ( int A [ ] , int X , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } int main ( ) { int X = 4 ; int A [ ] = { 2 , 1 , 5 , 3 , 6 } ; int N = 5 ; if ( findAns ( A , N , X ) ) { cout << " true " ; } else { cout << " false " ; } }
Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | C ++ program for the above approach ; Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string check ( int a [ ] , int b [ ] , int Na , int Nb , int k , int m ) { if ( Na < k Nb < m ) return " No " ; sort ( a , a + Na ) ; sort ( b , b + Nb ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " Yes " ; } return " No " ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 3 , 4 , 5 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int M = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int X = 2 , Y = 1 ; cout << check ( arr1 , arr2 , N , M , X , Y ) ; return 0 ; }
Partition array into two subsets with minimum Bitwise XOR between their maximum and minimum | C ++ program for the above approach ; Function to split the array into two subset such that the Bitwise XOR between the maximum of one subset and minimum of other is minimum ; Sort the array in increasing order ; Calculating the min Bitwise XOR between consecutive elements ; Return the final minimum Bitwise XOR ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int splitArray ( int arr [ ] , int N ) { sort ( arr , arr + N ) ; int result = INT_MAX ; for ( int i = 1 ; i < N ; i ++ ) { result = min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } int main ( ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << splitArray ( arr , N ) ; return 0 ; }
Sort an array by left shifting digits of array elements | C ++ program for the above approach ; Function to check if an array is sorted in increasing order or not ; Traverse the array ; Function to sort the array by left shifting digits of array elements ; Stores previous array element ; Traverse the array arr [ ] ; Stores current element ; Stores current element in string format ; Left - shift digits of current element in all possible ways ; Left - shift digits of current element by idx ; If temp greater than or equal to prev and temp less than optEle ; Update arr [ i ] ; Update prev ; If arr is in increasing order ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isIncreasing ( vector < int > arr ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } vector < int > sortArr ( vector < int > arr ) { int prev = -1 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int optEle = arr [ i ] ; string strEle = to_string ( arr [ i ] ) ; for ( int idx = 0 ; idx < strEle . size ( ) ; idx ++ ) { string strEle2 = strEle . substr ( idx ) + strEle . substr ( 0 , idx ) ; int temp = stoi ( strEle2 ) ; if ( temp >= prev && temp < optEle ) optEle = temp ; } arr [ i ] = optEle ; prev = arr [ i ] ; } if ( isIncreasing ( arr ) ) return arr ; else { arr = { -1 } ; return arr ; } } int main ( ) { vector < int > arr = { 511 , 321 , 323 , 432 , 433 } ; vector < int > res = sortArr ( arr ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) cout << res [ i ] << " ▁ " ; return 0 ; }
Activity selection problem with K persons | C ++ program for the above approach ; Comparator ; Function to find maximum shops that can be visited by K persons ; Store opening and closing time of shops ; Sort the pair of array ; Stores the result ; Stores current number of persons visiting some shop with their ending time ; Check if current shop can be assigned to a person who 's  already visiting any other shop ; Checks if there is any person whose closing time <= current shop opening time ; Erase previous shop visited by the person satisfying the condition ; Insert new closing time of current shop for the person satisfying a1he condition ; Increment the count by one ; In case if no person have closing time <= current shop opening time but there are some persons left ; Finally print the ans ; Driver Code ; Given starting and ending time ; Given K and N ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compareBy ( const pair < int , int > & a , const pair < int , int > & b ) { if ( a . second != b . second ) return a . second < b . second ; return a . first < b . first ; } int maximumShops ( int * opening , int * closing , int n , int k ) { pair < int , int > a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] . first = opening [ i ] ; a [ i ] . second = closing [ i ] ; } sort ( a , a + n , compareBy ) ; int count = 0 ; multiset < int > st ; for ( int i = 0 ; i < n ; i ++ ) { bool flag = false ; if ( ! st . empty ( ) ) { auto it = st . upper_bound ( a [ i ] . first ) ; if ( it != st . begin ( ) ) { it -- ; if ( * it <= a [ i ] . first ) { st . erase ( it ) ; st . insert ( a [ i ] . second ) ; count ++ ; flag = true ; } } } if ( st . size ( ) < k && flag == false ) { st . insert ( a [ i ] . second ) ; count ++ ; } } return count ; } int main ( ) { int S [ ] = { 1 , 8 , 3 , 2 , 6 } ; int E [ ] = { 5 , 10 , 6 , 5 , 9 } ; int K = 2 , N = sizeof ( S ) / sizeof ( S [ 0 ] ) ; cout << maximumShops ( S , E , N , K ) << endl ; }
Maximize maximum possible subarray sum of an array by swapping with elements from another array | C ++ program for the above approach ; Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver code ; Given arrays and respective lengths ; Calculate maximum subarray sum
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxSum ( int * arr , int * brr , int N , int K ) { vector < int > crr ; for ( int i = 0 ; i < N ; i ++ ) { crr . push_back ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . push_back ( brr [ i ] ) ; } sort ( crr . begin ( ) , crr . end ( ) , greater < int > ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } cout << sum << endl ; } int main ( ) { int arr [ ] = { 7 , 2 , -1 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int brr [ ] = { 1 , 2 , 3 , 2 } ; int K = sizeof ( brr ) / sizeof ( brr [ 0 ] ) ; maxSum ( arr , brr , N , K ) ; }
Count pairs with Bitwise XOR greater than both the elements of the pair | C ++ program for the above approach ; Function that counts the pairs whose Bitwise XOR is greater than both the elements of pair ; Stores the count of pairs ; Generate all possible pairs ; Find the Bitwise XOR ; Find the maximum of two ; If xo < mx , increment count ; Print the value of count ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int xo = ( A [ i ] ^ A [ j ] ) ; int mx = max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } cout << count ; } int main ( ) { int arr [ ] = { 2 , 4 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; countPairs ( arr , N ) ; return 0 ; }
Possible arrangement of persons waiting to sit in a hall | C ++ program for the above approach ; Function to find the arrangement of seating ; Stores the row in which the ith person sits ; Stores the width of seats along with their index or row number ; Sort the array ; Store the seats and row for boy 's seat ; Stores the index of row upto which boys have taken seat ; Iterate the string ; Push the row number at index in vector and heap ; Increment the index to let the next boy in the next minimum width vacant row ; Otherwise ; If girl then take top of element of the max heap ; Pop from queue ; Print the values ; Driver Code ; Given N ; Given arr [ ] ; Given string ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findTheOrder ( int arr [ ] , string s , int N ) { vector < int > ans ; pair < int , int > A [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = { arr [ i ] , i + 1 } ; sort ( A , A + N ) ; priority_queue < pair < int , int > > q ; int index = 0 ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( s [ i ] == '0' ) { ans . push_back ( A [ index ] . second ) ; q . push ( A [ index ] ) ; index ++ ; } else { ans . push_back ( q . top ( ) . second ) ; q . pop ( ) ; } } for ( auto i : ans ) { cout << i << " ▁ " ; } } int main ( ) { int N = 3 ; int arr [ ] = { 2 , 1 , 3 } ; string s = "001011" ; findTheOrder ( arr , s , N ) ; return 0 ; }
Difference between sum of K maximum even and odd array elements | C ++ program for the above approach ; Function to find the absolute difference between sum of first K maximum even and odd numbers ; Stores index from where odd number starts ; Segregate even and odd number ; If current element is even ; Sort in decreasing order even part ; Sort in decreasing order odd part ; Calculate sum of k maximum even number ; Calculate sum of k maximum odd number ; Print the absolute difference ; Driver Code ; Given array arr [ ] ; Size of array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void evenOddDiff ( int a [ ] , int n , int k ) { int j = -1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) { j ++ ; swap ( a [ i ] , a [ j ] ) ; } } j ++ ; sort ( a , a + j , greater < int > ( ) ) ; sort ( a + j , a + n , greater < int > ( ) ) ; int evenSum = 0 , oddSum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { evenSum += a [ i ] ; } for ( int i = j ; i < ( j + k ) ; i ++ ) { oddSum += a [ i ] ; } cout << abs ( evenSum - oddSum ) ; } int main ( ) { int arr [ ] = { 1 , 8 , 3 , 4 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 2 ; evenOddDiff ( arr , N , K ) ; return 0 ; }
Rearrange array to make product of prefix sum array non zero | C ++ program to implement the above approach ; Function to print array elements ; Function to rearrange array that satisfies the given condition ; Stores sum of elements of the given array ; Calculate totalSum ; If the totalSum is equal to 0 ; No possible way to rearrange array ; If totalSum exceeds 0 ; Rearrange the array in descending order ; Otherwise ; Rearrange the array in ascending order ; Driver Code
#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 ] << " ▁ " ; } } void rearrangeArr ( int arr [ ] , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } if ( totalSum == 0 ) { cout << " - 1" << endl ; } else if ( totalSum > 0 ) { sort ( arr , arr + N , greater < int > ( ) ) ; printArr ( arr , N ) ; } else { sort ( arr , arr + N ) ; printArr ( arr , N ) ; } } int main ( ) { int arr [ ] = { 1 , -1 , -2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; rearrangeArr ( arr , N ) ; }
Count ways to distribute exactly one coin to each worker | C ++ program for the above approach ; Function to find number of way to distribute coins giving exactly one coin to each person ; Sort the given arrays ; Start from bigger salary ; Increment the amount ; Reduce amount of valid coins by one each time ; Return the result ; Driver code ; Given two arrays ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int MOD = 1000000007 ; int solve ( vector < int > & values , vector < int > & salary ) { long long ret = 1 ; int amt = 0 ; sort ( values . begin ( ) , values . end ( ) ) ; sort ( salary . begin ( ) , salary . end ( ) ) ; while ( salary . size ( ) ) { while ( values . size ( ) && values . back ( ) >= salary . back ( ) ) { amt ++ ; values . pop_back ( ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . pop_back ( ) ; } return ret ; } int main ( ) { vector < int > values { 1 , 2 } , salary { 2 } ; cout << solve ( values , salary ) ; return 0 ; }
Count of index pairs with equal elements in an array | Set 2 | C ++ program for the above approach ; Function that counts the pair in the array arr [ ] ; Sort the array ; Initialize two pointers ; Add all valid pairs to answer ; Return the answer ; Driver Code ; Given array arr [ ] ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countPairs ( int arr [ ] , int n ) { int ans = 0 ; sort ( arr , arr + n ) ; int left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 2 , 3 , 2 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countPairs ( arr , N ) ; return 0 ; }