codeparrot/xlcost-text-to-code huggingface dataset:
Check if sequence of removed middle elements from an array is sorted or not | C ++ program for the above approach ; Function to check if sequence of removed middle elements from an array is sorted or not ; Points to the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isSortedArray ( int arr [ ] , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } int main ( ) { int arr [ ] = { 4 , 3 , 1 , 2 , 5 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( isSortedArray ( arr , N ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Length of longest strictly increasing subset with each pair of adjacent elements satisfying the condition 2 * A [ i ] â ‰¥ A [ i + 1 ] | C ++ program for the above approach ; Function to find the length of the longest subset satisfying given conditions ; Sort the array in ascending order ; Stores the starting index and maximum length of the required subset ; Pointer to traverse the array ; Iterate while i < n ; Stores end point of current subset ; Store the length of the current subset ; Continue adding elements to the current subset till the condition satisfies ; Increment length of the current subset ; Increment the pointer j ; If length of the current subset exceeds overall maximum length ; Update maxlen ; Update index ; Increment j ; Update i ; Store the starting index of the required subset in i ; Print the required subset ; Print the array element ; Decrement maxlen ; Increment i ; Driver Code ; Given array ; Store the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxLenSubset ( int a [ ] , int n ) { sort ( a , a + n ) ; int index = 0 , maxlen = -1 ; int i = 0 ; while ( i < n ) { int j = i ; int len = 1 ; while ( j < n - 1 ) { if ( 2 * a [ j ] >= a [ j + 1 ] ) { len ++ ; } else break ; j ++ ; } if ( maxlen < len ) { maxlen = len ; index = i ; } j ++ ; i = j ; } i = index ; while ( maxlen > 0 ) { cout << a [ i ] << " ▁ " ; maxlen -- ; i ++ ; } } int main ( ) { int a [ ] = { 3 , 1 , 5 , 11 } ; int n = sizeof ( a ) / sizeof ( int ) ; maxLenSubset ( a , n ) ; return 0 ; }
Maximize count of intersecting line segments | C ++ program for the above approach ; Function to find the maximum number of intersecting line segments possible ; Stores pairs of line segments { X [ i ] , Y [ i ] ) ; Push { X [ i ] , Y [ i ] } into p ; Sort p in ascending order of points on X number line ; Stores the points on Y number line in descending order ; Insert the first Y point from p ; Binary search to find the lower bound of p [ i ] . second ; If lower_bound doesn 't exist ; Insert p [ i ] . second into the set ; Erase the next lower _bound from the set ; Insert p [ i ] . second into the set ; Return the size of the set as the final result ; Driver Code ; Given Input ; Function call to find the maximum number of intersecting line segments
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int N , int X [ ] , int Y [ ] ) { vector < pair < int , int > > p ; for ( int i = 0 ; i < N ; i ++ ) { p . push_back ( { X [ i ] , Y [ i ] } ) ; } sort ( p . begin ( ) , p . end ( ) ) ; set < int , greater < int > > s ; s . insert ( p [ 0 ] . second ) ; for ( int i = 0 ; i < N ; i ++ ) { auto it = s . lower_bound ( p [ i ] . second ) ; if ( it == s . end ( ) ) { s . insert ( p [ i ] . second ) ; } else { s . erase ( * it ) ; s . insert ( p [ i ] . second ) ; } } return s . size ( ) ; } int main ( ) { int N = 3 ; int X [ ] = { 1 , 2 , 0 } ; int Y [ ] = { 2 , 0 , 1 } ; int maxintersection = solve ( N , X , Y ) ; cout << maxintersection ; }
Reduce an array to a single element by repeatedly removing larger element from a pair with absolute difference at most K | C ++ program for the above approach ; Function to check if an array can be reduced to single element by removing maximum element among any chosen pairs ; Sort the array in descending order ; Traverse the array ; If the absolute difference of 2 consecutive array elements is greater than K ; If the array can be reduced to a single element ; Driver Code ; Function Call to check if an array can be reduced to a single element
#include <bits/stdc++.h> NEW_LINE using namespace std ; void canReduceArray ( int arr [ ] , int N , int K ) { sort ( arr , arr + N , greater < int > ( ) ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { cout << " No " ; return ; } } cout << " Yes " ; } int main ( ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 1 ; canReduceArray ( arr , N , K ) ; return 0 ; }
Lexicographically smallest string possible by merging two sorted strings | C ++ program for the above approach ; Function to find lexicographically smallest string possible by merging two sorted strings ; Stores length of string s1 ; Stores length of string s2 ; Pointer to beginning of string1 i . e . , s1 ; Pointer to beginning of string2 i . e . , s2 ; Stores the final string ; Traverse the strings ; Append the smaller of the two current characters ; Append the remaining characters of any of the two strings ; Print the final string ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void mergeStrings ( string s1 , string s2 ) { int len1 = s1 . size ( ) ; int len2 = s2 . size ( ) ; int pntr1 = 0 ; int pntr2 = 0 ; string ans = " " ; while ( pntr1 < len1 && pntr2 < len2 ) { if ( s1 [ pntr1 ] < s2 [ pntr2 ] ) { ans += s1 [ pntr1 ] ; pntr1 ++ ; } else { ans += s2 [ pntr2 ] ; pntr2 ++ ; } } if ( pntr1 < len1 ) { ans += s1 . substr ( pntr1 , len1 ) ; } if ( pntr2 < len2 ) { ans += s2 . substr ( pntr2 , len2 ) ; } cout << ans ; } int main ( ) { string S1 = " abdcdtx " ; string S2 = " achilp " ; mergeStrings ( S1 , S2 ) ; return 0 ; }
Sort a string without altering the position of vowels | C ++ program for the above approach ; Function to sort the string leaving the vowels unchanged ; Length of string S ; Traverse the string S ; Sort the string temp ; Pointer to traverse the sorted string of consonants ; Traverse the string S ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortStr ( string S ) { int N = S . size ( ) ; string temp = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp += S [ i ] ; } if ( temp . size ( ) ) sort ( temp . begin ( ) , temp . end ( ) ) ; int ptr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) S [ i ] = temp [ ptr ++ ] ; } cout << S ; } int main ( ) { string S = " geeksforgeeks " ; sortStr ( S ) ; return 0 ; }
Count pairs from an array whose quotient of division of larger number by the smaller number does not exceed K | C ++ program for the above approach ; Function to count the number having quotient of division of larger element by the smaller element in the pair not exceeding K ; Sort the array in ascending order ; Store the required result ; Traverse the array ; Store the upper bound for the current array element ; Update the number of pairs ; Print the result ; Driver Code ; Given array , arr [ ] ; Store the size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void countPairs ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int high = upper_bound ( arr , arr + n , k * arr [ i ] ) - arr ; ans += high - i - 1 ; } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 9 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 2 ; countPairs ( arr , n , k ) ; return 0 ; }
Kth highest XOR of diagonal elements from a Matrix | C ++ Program to implement the above approach ; Function to find K - th maximum XOR of any diagonal in the matrix ; Number or rows ; Number of columns ; Store XOR of diagonals ; Traverse each diagonal ; Starting column of diagonal ; Count total elements in the diagonal ; Store XOR of current diagonal ; Push XOR of current diagonal ; Sort XOR values of diagonals ; Print the K - th Maximum XOR ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findXOR ( vector < vector < int > > mat , int K ) { int N = mat . size ( ) ; int M = mat [ 0 ] . size ( ) ; vector < int > digXOR ; for ( int l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { int s_col = max ( 0 , l - N ) ; int count = min ( { l , ( M - s_col ) , N } ) ; int currXOR = 0 ; for ( int j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . push_back ( currXOR ) ; } sort ( digXOR . begin ( ) , digXOR . end ( ) ) ; cout << digXOR [ N + M - 1 - K ] ; } int main ( ) { vector < vector < int > > mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int K = 3 ; findXOR ( mat , K ) ; return 0 ; }
Maximum sum of absolute differences between distinct pairs of a triplet from an array | C ++ program for the above approach ; Function to find the maximum sum of absolute differences between distinct pairs of triplet in array ; Stores the maximum sum ; Sort the array in ascending order ; Sum of differences between pairs of the triplet ; Print the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSum ( int arr [ ] , int N ) { int sum ; sort ( arr , arr + N ) ; sum = ( arr [ N - 1 ] - arr [ 0 ] ) + ( arr [ N - 2 ] - arr [ 0 ] ) + ( arr [ N - 1 ] - arr [ N - 2 ] ) ; cout << sum ; } int main ( ) { int arr [ ] = { 1 , 3 , 4 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSum ( arr , N ) ; return 0 ; }
Maximum and minimum sum of Bitwise XOR of pairs from an array | C ++ program for the above approach ; Function to find the required sum ; Keeps the track of the visited array elements ; Stores the result ; Keeps count of visited elements ; Traverse the vector , V ; If n elements are visited , break out of the loop ; Store the pair ( i , j ) and their Bitwise XOR ; If i and j both are unvisited ; Add xorResult to res and mark i and j as visited ; Increment count by 2 ; Return the result ; Function to find the maximum and minimum possible sum of Bitwise XOR of all the pairs from the array ; Stores the XOR of all pairs ( i , j ) ; Store the XOR of all pairs ( i , j ) ; Update Bitwise XOR ; Sort the vector ; Initialize variables to store maximum and minimum possible sums ; Find the minimum sum possible ; Reverse the vector , v ; Find the maximum sum possible ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSum ( vector < pair < int , pair < int , int > > > v , int n ) { unordered_map < int , bool > um ; int res = 0 ; int cnt = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( cnt == n ) break ; int x = v [ i ] . second . first ; int y = v [ i ] . second . second ; int xorResult = v [ i ] . first ; if ( um [ x ] == false && um [ y ] == false ) { res += xorResult ; um [ x ] = true ; um [ y ] = true ; cnt += 2 ; } } return res ; } void findMaxMinSum ( int a [ ] , int n ) { vector < pair < int , pair < int , int > > > v ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xorResult = a [ i ] ^ a [ j ] ; v . push_back ( { xorResult , { a [ i ] , a [ j ] } } ) ; } } sort ( v . begin ( ) , v . end ( ) ) ; int maxi = 0 , mini = 0 ; mini = findSum ( v , n ) ; reverse ( v . begin ( ) , v . end ( ) ) ; maxi = findSum ( v , n ) ; cout << mini << " ▁ " << maxi ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxMinSum ( arr , N ) ; return 0 ; }
Lexicographically smallest permutation having maximum sum of differences between adjacent elements | C ++ program for the above approach ; Function to find the lexicographically smallest permutation of an array such that the sum of the difference between adjacent elements is maximum ; Stores the size of the array ; Sort the given array in increasing order ; Swap the first and last array elements ; Print the required permutation ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maximumSumPermutation ( vector < int > & arr ) { int N = arr . size ( ) ; sort ( arr . begin ( ) , arr . end ( ) ) ; swap ( arr [ 0 ] , arr [ N - 1 ] ) ; for ( int i : arr ) { cout << i << " ▁ " ; } } int main ( ) { vector < int > arr = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; return 0 ; }
Maximize count of 1 s in an array by repeated division of array elements by 2 at most K times | C ++ program for the above approach ; Function to count the maximum number of array elements that can be reduced to 1 by repeatedly dividing array elements by 2 ; Sort the array in ascending order ; Store the count of array elements ; Traverse the array ; Store the number of operations required to reduce arr [ i ] to 1 ; Decrement k by opr ; If k becomes less than 0 , then break out of the loop ; Increment cnt by 1 ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxNumbers ( int arr [ ] , int n , int k ) { sort ( arr , arr + n ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int opr = ceil ( log2 ( arr [ i ] ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } cout << cnt ; } int main ( ) { int arr [ ] = { 5 , 8 , 4 , 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; findMaxNumbers ( arr , N , K ) ; return 0 ; }
Mean of given array after removal of K percent of smallest and largest array elements | C ++ program for the above approach ; Function to calculate the mean of a given array after removal of Kth percent of smallest and largest array elements ; Sort the array ; Find the K - th percent of the array size ; Traverse the array ; Skip the first K - th percent & last K - th percent array elements ; Mean of the rest of elements ; Print mean upto 5 decimal places ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void meanOfRemainingElements ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = sum / ( N - 2 * kthPercent ) ; cout << fixed << setprecision ( 5 ) << mean << endl ; } int main ( ) { int arr [ ] = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; return 0 ; }
Make the array elements equal by performing given operations minimum number of times | C ++ Program for the above approach ; Function to calculate the minimum operations of given type required to make the array elements equal ; Stores the total count of operations ; Traverse the array ; Update difference between pairs of adjacent elements ; Store the maximum count of operations ; Rest of the elements ; Total Operation - Maximum Operation ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minOperation ( int a [ ] , int N ) { int totOps = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { totOps += abs ( a [ i ] - a [ i + 1 ] ) ; } int maxOps = max ( abs ( a [ 0 ] - a [ 1 ] ) , abs ( a [ N - 1 ] - a [ N - 2 ] ) ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxOps = max ( maxOps , abs ( a [ i ] - a [ i - 1 ] ) + abs ( a [ i ] - a [ i + 1 ] ) - abs ( a [ i - 1 ] - a [ i + 1 ] ) ) ; } cout << totOps - maxOps << endl ; } int main ( ) { int arr [ ] = { 1 , -1 , 0 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minOperation ( arr , N ) ; return 0 ; }
Find the segment that overlaps with maximum number of segments | C ++ program for the above approach ; Function to find the segment which overlaps with maximum number of segments ; ' L ' & ' R ' co - ordinates of all segments are stored in lvalues & rvalues ; Assign co - ordinates ; Co - ordinate compression ; Stores the required segment ; Stores the current maximum number of intersections ; Find number of ' R ' coordinates which are less than the ' L ' value of the current segment ; Find number of ' L ' coordinates which are greater than the ' R ' value of the current segment ; Segments excluding ' lesser ' and ' greater ' gives the number of intersections ; Update the current maximum ; Print segment coordinates ; Driver Code ; Given segments ; Size of segments array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void maxIntersection ( int segments [ ] [ 2 ] , int N ) { vector < int > rvalues ( N ) , lvalues ( N ) ; for ( int i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i ] [ 0 ] ; rvalues [ i ] = segments [ i ] [ 1 ] ; } sort ( lvalues . begin ( ) , lvalues . end ( ) ) ; sort ( rvalues . begin ( ) , rvalues . end ( ) ) ; pair < int , int > answer = { -1 , -1 } ; int numIntersections = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int lesser = lower_bound ( rvalues . begin ( ) , rvalues . end ( ) , segments [ i ] [ 0 ] ) - rvalues . begin ( ) ; int greater = max ( 0 , N - ( int ) ( upper_bound ( lvalues . begin ( ) , lvalues . end ( ) , segments [ i ] [ 1 ] ) - lvalues . begin ( ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = { segments [ i ] [ 0 ] , segments [ i ] [ 1 ] } ; numIntersections = ( N - lesser - greater ) ; } } cout << answer . first << " ▁ " << answer . second ; } int main ( ) { int segments [ ] [ 2 ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 6 } } ; int N = sizeof ( segments ) / sizeof ( segments [ 0 ] ) ; maxIntersection ( segments , N ) ; }
Count swaps required to sort an array using Insertion Sort | C ++ Program to implement the above approach ; Stores the sorted array elements ; Function to count the number of swaps required to merge two sorted subarray in a sorted form ; Stores the count of swaps ; Function to count the total number of swaps required to sort the array ; Stores the total count of swaps required ; Find the middle index splitting the two halves ; Count the number of swaps required to sort the left subarray ; Count the number of swaps required to sort the right subarray ; Count the number of swaps required to sort the two sorted subarrays ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int temp [ 100000 ] ; long int merge ( int A [ ] , int left , int mid , int right ) { long int swaps = 0 ; int i = left , j = mid , k = left ; while ( i < mid && j <= right ) { if ( A [ i ] <= A [ j ] ) { temp [ k ] = A [ i ] ; k ++ , i ++ ; } else { temp [ k ] = A [ j ] ; k ++ , j ++ ; swaps += mid - i ; } } while ( i < mid ) { temp [ k ] = A [ i ] ; k ++ , i ++ ; } while ( j <= right ) { temp [ k ] = A [ j ] ; k ++ , j ++ ; } while ( left <= right ) { A [ left ] = temp [ left ] ; left ++ ; } return swaps ; } long int mergeInsertionSwap ( int A [ ] , int left , int right ) { long int swaps = 0 ; if ( left < right ) { int mid = left + ( right - left ) / 2 ; swaps += mergeInsertionSwap ( A , left , mid ) ; swaps += mergeInsertionSwap ( A , mid + 1 , right ) ; swaps += merge ( A , left , mid + 1 , right ) ; } return swaps ; } int main ( ) { int A [ ] = { 2 , 1 , 3 , 1 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << mergeInsertionSwap ( A , 0 , N - 1 ) ; return 0 ; }
Maximum removals possible from an array such that sum of its elements is greater than or equal to that of another array | C ++ program to implement the above approach ; Function to maximize the count of elements required to be removed from arr [ ] such that the sum of arr [ ] is greater than or equal to sum of the array brr [ ] ; Sort the array arr [ ] ; Stores index of smallest element of arr [ ] ; Stores sum of elements of the array arr [ ] ; Traverse the array arr [ ] ; Update sumArr ; Stores sum of elements of the array brr [ ] ; Traverse the array brr [ ] ; Update sumArr ; Stores count of removed elements ; Repeatedly remove the smallest element of arr [ ] ; Update sumArr ; Remove the smallest element ; If the sum of remaining elements in arr [ ] >= sum of brr [ ] ; Update cntRemElem ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxCntRemovedfromArray ( int arr [ ] , int N , int brr [ ] , int M ) { sort ( arr , arr + N ) ; int i = 0 ; int sumArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } int sumBrr = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } int cntRemElem = 0 ; while ( i < N and sumArr > = sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } int main ( ) { int arr [ ] = { 1 , 2 , 4 , 6 } ; int brr [ ] = { 7 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int M = sizeof ( brr ) / sizeof ( brr [ 0 ] ) ; cout << maxCntRemovedfromArray ( arr , N , brr , M ) ; }
Find any two pairs ( a , b ) and ( c , d ) such that a d | C ++ program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( pair < int , int > * arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int a = arr [ i ] . first , b = arr [ i ] . second ; for ( int j = i + 1 ; j < N ; j ++ ) { int c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { cout << " ( " << a << " ▁ " << b << " ) , ▁ ( " << c << " ▁ " << d << " ) STRNEWLINE " ; return ; } } } cout << " NO ▁ SUCH ▁ PAIR ▁ EXIST STRNEWLINE " ; } int main ( ) { pair < int , int > arr [ ] = { { 3 , 7 } , { 21 , 23 } , { 4 , 13 } , { 1 , 2 } , { 7 , -1 } } ; findPair ( arr , 5 ) ; }
Find any two pairs ( a , b ) and ( c , d ) such that a d | C ++ program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPair ( pair < int , int > * arr , int N ) { sort ( arr , arr + N ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { cout << " ( " << arr [ i - 1 ] . first << " ▁ " << b << " ) , ▁ ( " << arr [ i ] . first << " ▁ " << d << " ) " ; return ; } } cout << " NO ▁ SUCH ▁ PAIR ▁ EXIST STRNEWLINE " ; } int main ( ) { pair < int , int > arr [ ] = { { 3 , 7 } , { 21 , 23 } , { 4 , 13 } , { 1 , 2 } , { 7 , -1 } } ; findPair ( arr , 5 ) ; }
Print indices in non | C ++ program for the above approach ; Function to print the order of array elements generating non - decreasing quotient after division by X ; Stores the quotient and the order ; Traverse the array ; Sort the vector ; Print the order ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printOrder ( int order [ ] , int N , int X ) { vector < pair < int , int > > vect ; for ( int i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . push_back ( { order [ i ] / X , i + 1 } ) ; } else { vect . push_back ( { order [ i ] / X + 1 , i + 1 } ) ; } } sort ( vect . begin ( ) , vect . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { cout << vect [ i ] . second << " ▁ " ; } cout << endl ; } int main ( ) { int N = 3 , X = 3 ; int order [ ] = { 2 , 7 , 4 } ; printOrder ( order , N , X ) ; return 0 ; }