codeparrot/xlcost-text-to-code huggingface dataset:
Count array elements with rank not exceeding K | C ++ program for the above approach ; Function to find count of array elements with rank less than or equal to k ; Initialize rank and position ; Sort the given array ; Traverse array from right to left ; Update rank with position , if adjacent elements are unequal ; Return position - 1 , if rank greater than k ; Increase position ; Driver Code ; Given array ; Given K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int rankLessThanK ( int * arr , int k , int n ) { int rank = 1 ; int position = 1 ; sort ( arr , arr + n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i == n - 1 arr [ i ] != arr [ i + 1 ] ) { rank = position ; if ( rank > k ) return position - 1 ; } position ++ ; } return n ; } int main ( ) { int arr [ 5 ] = { 2 , 2 , 3 , 4 , 5 } ; int N = 5 ; int K = 4 ; cout << rankLessThanK ( arr , K , N ) ; return 0 ; }
Remove Sub | C ++ program for the above approach ; Function to remove sub - directories from the given lists dir ; Store final result ; Sort the given directories ; Insert 1 st directory ; Iterating in directory ; Current directory ; Our previous valid directory ; Find length of previous directory ; If subdirectory is found ; Else store it in result valid directory ; Driver Code ; Given lists of directories dir [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void eraseSubdirectory ( vector < string > & dir ) { vector < string > res ; sort ( dir . begin ( ) , dir . end ( ) ) ; res . push_back ( dir [ 0 ] ) ; cout << " { " << dir [ 0 ] << " , ▁ " ; for ( int i = 1 ; i < dir . size ( ) ; i ++ ) { string curr = dir [ i ] ; string prev = res . back ( ) ; int l = prev . length ( ) ; if ( curr . length ( ) > l && curr [ l ] == ' / ' && prev == curr . substr ( 0 , l ) ) continue ; res . push_back ( curr ) ; cout << curr << " , ▁ " ; } cout << " } STRNEWLINE " ; } int main ( ) { vector < string > dir = { " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " } ; eraseSubdirectory ( dir ) ; return 0 ; }
Maximize count of pairs ( i , j ) from two arrays having element from first array not exceeding that from second array | C ++ program for the above approach ; Function to return the maximum number of required pairs ; Max Heap to add values of arar2 [ ] ; Sort the array arr1 [ ] ; Push all arr2 [ ] into Max Heap ; Initialize the ans ; Traverse the arr1 [ ] in decreasing order ; Remove element until a required pair is found ; Return maximum number of pairs ; Driver Code ; Given arrays ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numberOfPairs ( int arr1 [ ] , int n , int arr2 [ ] , int m ) { priority_queue < int > pq ; int i , j ; sort ( arr1 , arr1 + n ) ; for ( j = 0 ; j < m ; j ++ ) { pq . push ( arr2 [ j ] ) ; } int ans = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( pq . top ( ) >= 2 * arr1 [ i ] ) { ans ++ ; pq . pop ( ) ; } } return ans ; } int main ( ) { int arr1 [ ] = { 3 , 1 , 2 } ; int arr2 [ ] = { 3 , 4 , 2 , 1 } ; int N = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int M = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; cout << numberOfPairs ( arr1 , N , arr2 , M ) ; return 0 ; }
Minimum number of operations to convert a given sequence into a Geometric Progression | Set 2 | C ++ program for above approach ; Function to find minimum cost ; Sort the array ; Maximum possible common ratios ; Iterate over all possible common ratios ; Calculate operations required for the current common ratio ; Calculate minimum cost ; Driver Code ; Given N ; Given arr [ ] ; Function Calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minCost ( int arr [ ] , int n ) { if ( n == 1 ) { cout << 0 << endl ; return ; } sort ( arr , arr + n ) ; float raised = 1 / float ( n - 1 ) ; float temp = pow ( arr [ n - 1 ] , raised ) ; int r = round ( temp ) + 1 ; int i , j , min_cost = INT_MAX ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = min ( min_cost , curr_cost ) ; common_ratio = j ; } } cout << min_cost << ' ▁ ' ; cout << common_ratio << ' ▁ ' ; } int main ( ) { int N = 6 ; int arr [ ] = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; return 0 ; }
Mth bit in Nth binary string from a sequence generated by the given operations | C ++ program for above approach ; Function to calculate N Fibonacci numbers ; Function to find the mth bit in the string Sn ; Base case ; Length of left half ; Length of the right half ; Recursive check in the left half ; Recursive check in the right half ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define maxN  10 NEW_LINE void calculateFib ( int fib [ ] , int n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( int x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } int find_mth_bit ( int n , int m , int fib [ ] ) { if ( n <= 1 ) { return n ; } int len_left = fib [ n - 2 ] ; int len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } void find_mth_bitUtil ( int n , int m ) { int fib [ maxN ] ; calculateFib ( fib , maxN ) ; int ans = find_mth_bit ( n , m , fib ) ; cout << ans << ' ▁ ' ; } int main ( ) { int n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ; return 0 ; }
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix | C ++ program to implement the above approach ; Function to count the minimum number of adjacent swaps ; Stores the size of the given matrix ; Stores the count of zero at the end of each row ; Traverse the given matrix ; Count of 0 s at the end of the ith row ; Stores the count of swaps ; Traverse the cntZero array ; If count of zero in the i - th row < ( N - i - 1 ) ; Stores the index of the row where count of zero > ( N - i - 1 ) ; If no row found that satisfy the condition ; Swap the adjacent row ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minAdjSwaps ( vector < vector < int > > & mat ) { int N = mat . size ( ) ; vector < int > cntZero ( N , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 && mat [ i ] [ j ] == 0 ; j -- ) { cntZero [ i ] ++ ; } } int cntSwaps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntZero [ i ] < ( N - i - 1 ) ) { int First = i ; while ( First < N && cntZero [ First ] < ( N - i - 1 ) ) { First ++ ; } if ( First == N ) { return -1 ; } while ( First > i ) { swap ( cntZero [ First ] , cntZero [ First - 1 ] ) ; First -- ; cntSwaps ++ ; } } } return cntSwaps ; } int main ( ) { vector < vector < int > > mat = { { 0 , 0 , 2 } , { 3 , 1 , 0 } , { 4 , 0 , 0 } } ; cout << minAdjSwaps ( mat ) ; }
Largest area in a grid unbounded by towers | C ++ Program for the above approach ; Function to calculate the largest area unguarded by towers ; Sort the x - coordinates of the list ; Sort the y - coordinates of the list ; dx -- > maximum uncovered tiles in x coordinates ; dy -- > maximum uncovered tiles in y coordinates ; Calculate the maximum uncovered distances for both x and y coordinates ; Largest unguarded area is max ( dx ) - 1 * max ( dy ) - 1 ; Driver Code ; Length and width of the grid ; No of guard towers ; Array to store the x and y coordinates ; Function call
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void maxArea ( int point_x [ ] , int point_y [ ] , int n , int length , int width ) { sort ( point_x , point_x + n ) ; sort ( point_y , point_y + n ) ; int dx = point_x [ 0 ] ; int dy = point_y [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dx = max ( dx , point_x [ i ] - point_x [ i - 1 ] ) ; dy = max ( dy , point_y [ i ] - point_y [ i - 1 ] ) ; } dx = max ( dx , ( length + 1 ) - point_x [ n - 1 ] ) ; dy = max ( dy , ( width + 1 ) - point_y [ n - 1 ] ) ; cout << ( dx - 1 ) * ( dy - 1 ) ; cout << endl ; } int main ( ) { int length = 15 , width = 8 ; int n = 3 ; int point_x [ ] = { 3 , 11 , 8 } ; int point_y [ ] = { 8 , 2 , 6 } ; maxArea ( point_x , point_y , n , length , width ) ; return 0 ; }
Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K | C ++ program for the above approach ; Function to find the cost of splitting the arrays into subarray with cost less than K ; Store the possible splits ; Stores the cost of each split ; Stores the count of even numbers ; Stores the count of odd numbers ; Count of odd & even elements ; Check the required conditions for a valid split ; Sort the cost of splits ; Find the minimum split costs adding up to sum less than K ; Driver Code ; Given array and K ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int make_cuts ( int arr [ ] , int n , int K ) { int ans = 0 ; vector < int > poss ; int ce = 0 ; int co = 0 ; for ( int x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . push_back ( abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } sort ( poss . begin ( ) , poss . end ( ) ) ; for ( int x : poss ) { if ( K >= x ) { ans ++ ; K -= x ; } else break ; } return ans ; } int main ( ) { int N = 6 ; int K = 4 ; int arr [ ] = { 1 , 2 , 5 , 10 , 15 , 20 } ; cout << make_cuts ( arr , N , K ) ; }
Cost of rearranging the array such that no element exceeds the sum of its adjacent elements | C ++ program to implement the above approach ; Function to check if given elements can be arranged such that sum of its neighbours is strictly greater ; Initialize the total cost ; Storing the original index of elements in a hashmap ; Sort the given array ; Check if a given condition is satisfies or not ; First number ; Add the cost to overall cost ; Last number ; Add the cost to overall cost ; Add the cost to overall cost ; Printing the cost ; Driver Code ; Given array ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void Arrange ( int arr [ ] , int n ) { int cost = 0 ; unordered_map < int , int > index ; for ( int i = 0 ; i < n ; i ++ ) { index [ arr [ i ] ] = i ; } sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { cout << " - 1" ; return ; } else { cost += abs ( index [ arr [ i ] ] - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { cout << " - 1" ; return ; } else { cost += abs ( index [ arr [ i ] ] - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { cout << " - 1" ; return ; } else { cost += abs ( index [ arr [ i ] ] - i ) ; } } } cout << cost ; return ; } int main ( ) { int arr [ ] = { 2 , 4 , 5 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Arrange ( arr , N ) ; return 0 ; }
XOR of all possible pairwise sum from two given Arrays | C ++ Program to implement the above approach ; Function to calculate the sum of XOR of the sum of every pair ; Stores the XOR of sums of every pair ; Iterate to generate all possible pairs ; Update XOR ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int XorSum ( int A [ ] , int B [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { ans = ans ^ ( A [ i ] + B [ j ] ) ; } } return ans ; } int main ( ) { int A [ ] = { 4 , 6 , 0 , 0 , 3 , 3 } ; int B [ ] = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = sizeof A / sizeof A [ 0 ] ; cout << XorSum ( A , B , N ) << endl ; return 0 ; }
Maximum non | C ++ program for the above approach ; Function to find maximum distinct character after removing K character ; Freq implemented as hash table to store frequency of each character ; Store frequency of each character ; Insert each frequency in v ; Sort the freq of character in non - decreasing order ; Traverse the vector ; Update v [ i ] and k ; If K is still not 0 ; Store the final answer ; Count this character if freq 1 ; Return count of distinct characters ; Driver Code ; Given string ; Given k ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxDistinctChar ( string s , int n , int k ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ s [ i ] ] ++ ; vector < int > v ; for ( auto it = freq . begin ( ) ; it != freq . end ( ) ; it ++ ) { v . push_back ( it -> second ) ; } sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = min ( v [ i ] - 1 , k ) ; v [ i ] -= mn ; k -= mn ; } if ( k > 0 ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = min ( v [ i ] , k ) ; v [ i ] -= mn ; k -= mn ; } } int res = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( v [ i ] == 1 ) res ++ ; return res ; } int main ( ) { string S = " geeksforgeeks " ; int N = S . size ( ) ; int K = 1 ; cout << maxDistinctChar ( S , N , K ) ; return 0 ; }
Split N as the sum of K numbers satisfying the given conditions | C ++ Program to implement the above approach ; Vector to store prime numbers ; Function to generate prime numbers using SieveOfEratosthenes ; Boolean array to store primes ; If p is a prime ; Mark all its multiples as non - prime ; Print all prime numbers ; Function to generate n as the sum of k numbers where atleast K - 1 are distinct and are product of 2 primes ; Stores the product of every pair of prime number ; Sort the products ; If sum exceeds n ; Otherwise , print the k required numbers ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > primes ; void SieveOfEratosthenes ( ) { bool prime [ 10005 ] ; memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . push_back ( p ) ; } void generate ( int n , int k ) { vector < long long > prod ; SieveOfEratosthenes ( ) ; int l = primes . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i + 1 ; j < l ; j ++ ) { if ( primes [ i ] * primes [ j ] > 0 ) prod . push_back ( primes [ i ] * primes [ j ] ) ; } } sort ( prod . begin ( ) , prod . end ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) sum += prod [ i ] ; if ( sum > n ) cout << " - 1" ; else { for ( int i = 0 ; i < k - 1 ; i ++ ) { cout << prod [ i ] << " , ▁ " ; } cout << n - sum << " STRNEWLINE " ; } } int main ( ) { int n = 52 , k = 5 ; generate ( n , k ) ; return 0 ; }
Maximum modified Array sum possible by choosing elements as per the given conditions | C ++ program for the above approach ; Function that finds the maximum sum of the array elements according to the given condition ; Sort the array ; Take the max value from the array ; Iterate from the end of the array ; Check for the number had come before or not ; Take the value which is not occured already ; Change max to new value ; Return the maximum sum ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE #define ll  long long NEW_LINE using namespace std ; ll findMaxValue ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; ll ans = arr [ n - 1 ] ; ll maxPossible = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( maxPossible > 0 ) { if ( arr [ i ] >= maxPossible ) { ans += ( maxPossible - 1 ) ; maxPossible = maxPossible - 1 ; } else { maxPossible = arr [ i ] ; ans += maxPossible ; } } } return ans ; } int main ( ) { int arr [ ] = { 4 , 4 , 1 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxValue ( arr , n ) ; }
Minimum cost to sort an Array such that swapping X and Y costs XY | C ++ program for the above approach ; Function returns the minimum cost to sort the given array ; Create array of pairs in which 1 st element is the array element and 2 nd element is index of first ; Initialize the total cost ; Sort the array with respect to array value ; Initialize the overall minimum which is the 1 st element ; To keep track of visited elements create a visited array & initialize all elements as not visited ; Iterate over every element of the array ; If the element is visited or in the sorted position , and check for next element ; Create a vector which stores all elements of a cycle ; It covers all the elements of a cycle ; If cycle is found then the swapping is required ; Initialize local minimum with 1 st element of the vector as it contains the smallest element in the beginning ; Stores the cost with using only local minimum value . ; Stores the cost of using both local minimum and overall minimum ; Update the result2 ; Store the minimum of the two result to total cost ; Return the minimum cost ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int n ) { pair < int , int > sorted [ n ] ; int total_cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sorted [ i ] . first = arr [ i ] ; sorted [ i ] . second = i ; } sort ( sorted , sorted + n ) ; int overall_minimum = sorted [ 0 ] . first ; bool vis [ n ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] && sorted [ i ] . second == i ) continue ; vector < int > v ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; v . push_back ( sorted [ j ] . first ) ; j = sorted [ j ] . second ; } if ( v . size ( ) > 0 ) { int local_minimum = v [ 0 ] , result1 = 0 , result2 = 0 ; for ( int k = 1 ; k < v . size ( ) ; k ++ ) result1 += ( local_minimum * v [ k ] ) ; for ( int k = 0 ; k < v . size ( ) ; k ++ ) result2 += ( overall_minimum * v [ k ] ) ; result2 += ( overall_minimum * local_minimum ) ; total_cost += min ( result1 , result2 ) ; } } return total_cost ; } int main ( ) { int arr [ ] = { 1 , 8 , 9 , 7 , 6 } ; int n = ( sizeof ( arr ) / sizeof ( int ) ) ; cout << minCost ( arr , n ) ; return 0 ; }
Maximize the last Array element as per the given conditions | C ++ Program to implement the above approach ; Function to find the maximum possible value that can be placed at the last index ; Sort array in ascending order ; If the first element is not equal to 1 ; Traverse the array to make difference between adjacent elements <= 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximizeFinalElement ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } int main ( ) { int n = 4 ; int arr [ ] = { 3 , 1 , 3 , 4 } ; int max = maximizeFinalElement ( arr , n ) ; cout << max ; return 0 ; }
Find the point on X | C ++ Program to implement the above approach ; Function to find median of the array ; Sort the given array ; If number of elements are even ; Return the first median ; Otherwise ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findLeastDist ( int A [ ] , int N ) { sort ( A , A + N ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } int main ( ) { int A [ ] = { 4 , 1 , 5 , 10 , 2 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << " ( " << findLeastDist ( A , N ) << " , ▁ " << 0 << " ) " ; return 0 ; }
Maximum sum of any submatrix of a Matrix which is sorted row | C ++ program for the above approach ; Function that finds the maximum Sub - Matrix Sum ; Number of rows in the matrix ; Number of columns in the matrix ; dp [ ] [ ] matrix to store the results of each iteration ; Base Case - The largest element in the matrix ; To stores the final result ; Find the max sub matrix sum for the last row ; Check whether the current sub - array yields maximum sum ; Calculate the max sub matrix sum for the last column ; Check whether the current sub - array yields maximum sum ; Build the dp [ ] [ ] matrix from bottom to the top row ; Update sum at each cell in dp [ ] [ ] ; Update the maximum sum ; Return the maximum sum ; Driver Code ; Given matrix mat [ ] [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSubMatSum ( vector < vector < int > > mat ) { int n = mat . size ( ) ; int m = mat [ 0 ] . size ( ) ; int i , j ; int dp [ n ] [ m ] ; dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ] ; int res = dp [ n - 1 ] [ m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 ] [ i ] = mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] ; res = max ( res , dp [ n - 1 ] [ i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] [ m - 1 ] = mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] ; res = max ( res , dp [ i ] [ m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i ] [ j ] = mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] ; res = max ( res , dp [ i ] [ j ] ) ; } } return res ; } int main ( ) { vector < vector < int > > mat ; mat = { { -6 , -4 , -1 } , { -3 , 2 , 4 } , { 2 , 5 , 8 } } ; cout << maxSubMatSum ( mat ) ; return 0 ; }
Check if two arrays can be made equal by reversing subarrays multiple times | C ++ implementation to check if two arrays can be made equal ; Function to check if array B can be made equal to array A ; sort both the arrays ; Check if both the arrays are equal or not ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool canMadeEqual ( 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 [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 3 , 2 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( canMadeEqual ( A , B , n ) ) cout << " Yes " ; else cout << " No " ; return 0 ; }
Minimize difference between maximum and minimum of Array by at most K replacements | C ++ program of the approach ; Function to find minimum difference between the maximum and the minimum elements arr [ ] by at most K replacements ; Check if turns are more than or equal to n - 1 then simply return zero ; Sort the array ; Set difference as the maximum possible difference ; Iterate over the array to track the minimum difference in k turns ; Return the answer ; Driver Code ; Given array arr [ ] ; Given K replacements ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxMinDifference ( int arr [ ] , int n , int k ) { if ( k >= n - 1 ) return 0 ; sort ( arr , arr + n ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; for ( int i = k , j = n - 1 ; i >= 0 ; -- i , -- j ) { ans = min ( arr [ j ] - arr [ i ] , ans ) ; } return ans ; } int main ( ) { int arr [ ] = { 1 , 4 , 6 , 11 , 15 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int K = 3 ; cout << maxMinDifference ( arr , N , K ) ; return 0 ; }
Maximum possible Array sum after performing given operations | C ++ implementation to find the maximum possible sum of array after performing given operations ; Function to get maximum sum after q operations ; priority queue to get maximum sum ; Push pair , value and 1 in the priority queue ; Push pair , value ( to be replaced ) and number of elements ( to be replaced ) ; Add top n elements from the priority queue to get max sum ; pr is the pair pr . first is the value and pr . second is the occurrence ; pop from the priority queue ; Add value to answer ; Update n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void max_sum ( int ar [ ] , int n , int q , int x [ ] , int y [ ] ) { int ans = 0 , i ; priority_queue < pair < int , int > > pq ; for ( i = 0 ; i < n ; i ++ ) pq . push ( { ar [ i ] , 1 } ) ; for ( i = 0 ; i < q ; i ++ ) pq . push ( { y [ i ] , x [ i ] } ) ; while ( n > 0 ) { auto pr = pq . top ( ) ; pq . pop ( ) ; ans += pr . first * min ( n , pr . second ) ; n -= pr . second ; } cout << ans << " STRNEWLINE " ; } int main ( ) { int ar [ ] = { 200 , 100 , 200 , 300 } ; int n = ( sizeof ar ) / ( sizeof ar [ 0 ] ) ; int q = 2 ; int x [ ] = { 2 , 3 } ; int y [ ] = { 100 , 90 } ; max_sum ( ar , n , q , x , y ) ; return 0 ; }