codeparrot/xlcost-text-to-code huggingface dataset:
Maximum items that can be bought from the cost Array based on given conditions | C ++ program to find the maximum number of items that can be bought from the given cost array ; Function to find the maximum number of items that can be bought from the given cost array ; Sort the given array ; Variables to store the prefix sum , answer and the counter variables ; Initializing the first element of the prefix array ; If we can buy at least one item ; Iterating through the first K items and finding the prefix sum ; Check the number of items that can be bought ; Finding the prefix sum for the remaining elements ; Check the number of iteams that can be bought ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int number ( int a [ ] , int n , int p , int k ) { sort ( a , a + n ) ; int pre [ n ] = { 0 } , val , i , j , ans = 0 ; pre [ 0 ] = a [ 0 ] ; if ( pre [ 0 ] <= p ) ans = 1 ; for ( i = 1 ; i < k - 1 ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i ] ; if ( pre [ i ] <= p ) ans = i + 1 ; } pre [ k - 1 ] = a [ k - 1 ] ; for ( i = k - 1 ; i < n ; i ++ ) { if ( i >= k ) { pre [ i ] += pre [ i - k ] + a [ i ] ; } if ( pre [ i ] <= p ) ans = i + 1 ; } return ans ; } int main ( ) { int n = 5 ; int arr [ ] = { 2 , 4 , 3 , 5 , 7 } ; int p = 11 ; int k = 2 ; cout << number ( arr , n , p , k ) << endl ; return 0 ; }
Sort all special primes in their relative positions | C ++ implementation of the approach ; Function for the Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; Function to sort the special primes in their relative positions ; Maximum element from the array ; prime [ i ] will be true if i is a prime ; To store the special primes from the array ; If current element is a special prime ; Add it to the ArrayList and set arr [ i ] to - 1 ; Sort the special primes ; Position of a special prime ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sieveOfEratosthenes ( bool prime [ ] , int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } void sortSpecialPrimes ( int arr [ ] , int n ) { int maxVal = * max_element ( arr , arr + n ) ; bool prime [ maxVal + 1 ] ; memset ( prime , true , sizeof ( prime ) ) ; sieveOfEratosthenes ( prime , maxVal ) ; vector < int > list ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] && prime [ arr [ i ] - 2 ] ) { list . push_back ( arr [ i ] ) ; arr [ i ] = -1 ; } } sort ( list . begin ( ) , list . end ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == -1 ) cout << list [ j ++ ] << " ▁ " ; else cout << arr [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 31 , 5 , 2 , 1 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; sortSpecialPrimes ( arr , n ) ; return 0 ; }
Find the Maximum possible Sum for the given conditions | C ++ program to find the maximum possible Sum for the given conditions ; Function to find the maximum possible sum for the given conditions ; Sorting the array ; Variable to store the answer ; Iterating through the array ; If the value is greater than 0 ; If the value becomes 0 then break the loop because all the weights after this index will be 0 ; Print profit ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxProfit ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } int main ( ) { int arr [ ] = { 6 , 6 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << maxProfit ( arr , n ) ; return 0 ; }
Check if the array can be sorted only if the elements on given positions can be swapped | C ++ program to check if the array can be sorted only if the elements on the given positions can be swapped ; Function to check if the array can be sorted only if the elements on the given positions can be swapped ; Creating an array for marking the positions ; Iterating through the array and mark the positions ; Iterating through the given array ; If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos ; Sorting the required segment ; Checking if the vector is sorted or not ; Print yes if it is sorted ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void check_vector ( vector < int > A , int n , vector < int > p ) { vector < int > pos ( A . size ( ) ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) { pos [ p [ i ] - 1 ] = 1 ; } int flag = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos [ i ] == 0 ) continue ; int j = i ; while ( j < n && pos [ j ] ) ++ j ; sort ( A . begin ( ) + i , A . begin ( ) + j + 1 ) ; i = j ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) cout << " Yes " ; else cout << " No " ; } int main ( ) { vector < int > A { 3 , 2 , 1 } ; vector < int > p { 1 , 2 } ; check_vector ( A , A . size ( ) , p ) ; return 0 ; }
Sort an array of strings based on the given order | C ++ program to sort an array of strings based on the given order ; For storing priority of each character ; Custom comparator function for sort ; Loop through the minimum size between two words ; Check if the characters at position i are different , then the word containing lower valued character is smaller ; When loop breaks without returning , it means the prefix of both words were same till the execution of the loop . Now , the word with the smaller size will occur before in sorted order ; Function to print the new sorted array of strings ; Mapping each character to its occurrence position ; Sorting with custom sort function ; Printing the sorted order of words ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_map < char , int > mp ; bool comp ( string & a , string & b ) { for ( int i = 0 ; i < min ( a . size ( ) , b . size ( ) ) ; i ++ ) { if ( mp [ a [ i ] ] != mp [ b [ i ] ] ) return mp [ a [ i ] ] < mp [ b [ i ] ] ; } return ( a . size ( ) < b . size ( ) ) ; } void printSorted ( vector < string > words , string order ) { for ( int i = 0 ; i < order . size ( ) ; i ++ ) mp [ order [ i ] ] = i ; sort ( words . begin ( ) , words . end ( ) , comp ) ; for ( auto x : words ) cout << x << " ▁ " ; } int main ( ) { vector < string > words = { " word " , " world " , " row " } ; string order = { " worldabcefghijkmnpqstuvxyz " } ; printSorted ( words , order ) ; return 0 ; }
Sort elements of an array in increasing order of absolute difference of adjacent elements | C ++ implementation to sort the elements of the array in such a way that absolute difference between the adjacent elements are in increasing order ; Function to sort the elements of the array by difference ; Sorting the array ; Array to store the elements of the array ; Iterating over the length of the array to include each middle element of array ; Appending the middle element of the array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortDiff ( vector < int > arr , int n ) { sort ( arr . begin ( ) , arr . end ( ) ) ; vector < int > out ; while ( n > 0 ) { out . push_back ( arr [ n / 2 ] ) ; arr . erase ( arr . begin ( ) + n / 2 ) ; n = n - 1 ; } for ( auto i : out ) cout << i << " ▁ " ; } int main ( ) { vector < int > a = { 8 , 1 , 2 , 3 , 0 } ; int n = 5 ; sortDiff ( a , n ) ; }
Minimum cost of choosing the array element | C ++ program for the above approach ; Function that find the minimum cost of selecting array element ; Sorting the given array in increasing order ; To store the prefix sum of arr [ ] ; Update the pref [ ] to find the cost selecting array element by selecting at most M element ; Print the pref [ ] for the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumCost ( int arr [ ] , int N , int M ) { sort ( arr , arr + N ) ; int pref [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = arr [ i ] + pref [ i - 1 ] ; } for ( int i = M ; i < N ; i ++ ) { pref [ i ] += pref [ i - M ] ; } for ( int i = 0 ; i < N ; i ++ ) { cout << pref [ i ] << ' ▁ ' ; } } int main ( ) { int arr [ ] = { 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 } ; int M = 2 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minimumCost ( arr , N , M ) ; return 0 ; }
Sort an Array alphabetically when each number is converted into words | C ++ program to sort an array of integers alphabetically ; Variable to store the word form of units digit and up to twenty ; Variable to store the word form of tens digit ; Function to convert a two digit number to the word by using the above defined arrays ; If n is more than 19 , divide it ; If n is non - zero ; Function to print a given number in words ; Stores the word representation of the given number n ; Handles digits at ten millions and hundred millions places ; Handles digits at hundred thousands and one millions places ; Handles digits at thousands and tens thousands places ; Handles digit at hundreds places ; Call the above function to convert the number into words ; Function to sort the array according to the albhabetical order ; Vector to store the number in words with respective elements ; Inserting number in words with respective elements in vector pair ; Sort the vector , this will sort the pair according to the alphabetical order . ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string one [ ] = { " " , " one ▁ " , " two ▁ " , " three ▁ " , " four ▁ " , " five ▁ " , " six ▁ " , " seven ▁ " , " eight ▁ " , " nine ▁ " , " ten ▁ " , " eleven ▁ " , " twelve ▁ " , " thirteen ▁ " , " fourteen ▁ " , " fifteen ▁ " , " sixteen ▁ " , " seventeen ▁ " , " eighteen ▁ " , " nineteen ▁ " } ; string ten [ ] = { " " , " " , " twenty ▁ " , " thirty ▁ " , " forty ▁ " , " fifty ▁ " , " sixty ▁ " , " seventy ▁ " , " eighty ▁ " , " ninety ▁ " } ; string numToWords ( int n , string s ) { string str = " " ; if ( n > 19 ) str += ten [ n / 10 ] + one [ n % 10 ] ; else str += one [ n ] ; if ( n ) str += s ; return str ; } string convertToWords ( int n ) { string out ; out += numToWords ( ( n / 10000000 ) , " crore ▁ " ) ; out += numToWords ( ( ( n / 100000 ) % 100 ) , " lakh ▁ " ) ; out += numToWords ( ( ( n / 1000 ) % 100 ) , " thousand ▁ " ) ; out += numToWords ( ( ( n / 100 ) % 10 ) , " hundred ▁ " ) ; if ( n > 100 && n % 100 ) out += " and ▁ " ; out += numToWords ( ( n % 100 ) , " " ) ; return out ; } void sortArr ( int arr [ ] , int n ) { vector < pair < string , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( convertToWords ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { int arr [ ] = { 12 , 10 , 102 , 31 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Maximum Possible Rating of a Coding Contest | C ++ program to find the Maximum Possible Rating of a Coding Contest ; Function to sort all problems descending to upvotes ; Function to return maximum rating ; Declaring vector of pairs ; Each pair represents a problem with its points and upvotes ; Step ( 1 ) - Sort problems by their upvotes value in decreasing order ; Declaring min_heap or priority queue to track of the problem with minimum points . ; Step ( 2 ) - Loop for i = 0 to K - 1 and do accordingly ; Step ( 3 ) - Loop for i = K to N - 1 and do accordingly ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Comparator ( pair < int , int > p1 , pair < int , int > p2 ) { return p1 . second > p2 . second ; } int FindMaxRating ( int N , int Point [ ] , int Upvote [ ] , int K ) { vector < pair < int , int > > vec ; for ( int i = 0 ; i < N ; i ++ ) { vec . push_back ( make_pair ( Point [ i ] , Upvote [ i ] ) ) ; } sort ( vec . begin ( ) , vec . end ( ) , Comparator ) ; priority_queue < int , vector < int > , greater < int > > pq ; int total_points = 0 , max_rating = 0 ; for ( int i = 0 ; i < K ; i ++ ) { total_points = total_points + vec [ i ] . first ; max_rating = max ( max_rating , total_points * vec [ i ] . second ) ; pq . push ( vec [ i ] . first ) ; } for ( int i = K ; i < N ; i ++ ) { if ( pq . top ( ) < vec [ i ] . first ) { total_points = total_points - pq . top ( ) + vec [ i ] . first ; max_rating = max ( max_rating , total_points * vec [ i ] . second ) ; pq . pop ( ) ; pq . push ( vec [ i ] . first ) ; } } return max_rating ; } int main ( ) { int Point [ ] = { 2 , 10 , 3 , 1 , 5 , 8 } ; int Upvote [ ] = { 5 , 4 , 3 , 9 , 7 , 2 } ; int N = sizeof ( Point ) / sizeof ( Point [ 0 ] ) ; int K = 2 ; cout << " Maximum ▁ Rating ▁ of ▁ Coding ▁ Contest ▁ is : ▁ " << FindMaxRating ( N , Point , Upvote , K ) ; return 0 ; }
Find sum of all unique elements in the array for K queries | C ++ implementation to find the sum of all unique elements of the array after Q queries ; Function to find the sum of unique elements after Q Query ; Updating the array after processing each query ; Making it to 0 - indexing ; Iterating over the array to get the final array ; Variable to store the sum ; Hash to maintain perviously occured elements ; Loop to find the maximum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int uniqueSum ( int A [ ] , int R [ ] [ 2 ] , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int ans = 0 ; unordered_set < int > s ; for ( int i = 0 ; i < N ; ++ i ) { if ( s . find ( A [ i ] ) == s . end ( ) ) ans += A [ i ] ; s . insert ( A [ i ] ) ; } return ans ; } int main ( ) { int A [ ] = { 0 , 0 , 0 , 0 , 0 , 0 } ; int R [ ] [ 2 ] = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } , { 3 , 3 } } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( R ) / sizeof ( R [ 0 ] ) ; cout << uniqueSum ( A , R , N , M ) ; return 0 ; }
Find the Kth pair in ordered list of all possible sorted pairs of the Array | C ++ program to find the K - th pair in a lexicographically sorted array ; Function to find the k - th pair ; Sorting the array ; Iterating through the array ; Finding the number of same elements ; Checking if N * T is less than the remaining K . If it is , then arr [ i ] is the first element in the required pair ; Printing the K - th pair ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kthpair ( int n , int k , int arr [ ] ) { int i , t ; sort ( arr , arr + n ) ; -- k ; for ( i = 0 ; i < n ; i += t ) { for ( t = 1 ; arr [ i ] == arr [ i + t ] ; ++ t ) ; if ( t * n > k ) break ; k = k - t * n ; } cout << arr [ i ] << ' ▁ ' << arr [ k / t ] ; } int main ( ) { int n = 3 , k = 2 ; int arr [ n ] = { 3 , 1 , 5 } ; kthpair ( n , k , arr ) ; }
Minimum characters to be replaced to make frequency of all characters same | C ++ implementation to find the Minimum characters to be replaced to make frequency of all characters same ; Function to find the Minimum operations to convert given string to another with equal frequencies of characters ; Frequency of characters ; Loop to find the Frequency of each character ; Sort in decreasing order based on frequency ; Maximum possible answer ; Loop to find the minimum operations required such that frequency of every character is equal ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( string s ) { int freq [ 26 ] = { 0 } ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s [ i ] - ' A ' ] ++ ; } sort ( freq , freq + 26 , greater < int > ( ) ) ; int answer = n ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( n % i == 0 ) { int x = n / i ; int y = 0 ; for ( int j = 0 ; j < i ; j ++ ) { y += min ( freq [ j ] , x ) ; } answer = min ( answer , n - y ) ; } } return answer ; } int main ( ) { string s = " BBC " ; cout << minOperations ( s ) ; return 0 ; }
Check if it is possible to sort an array with conditional swapping of elements at distance K | CPP implementation to Check if it is possible to sort an array with conditional swapping of elements at distance K ; Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool fun ( int arr [ ] , int n , int k ) { vector < int > v ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . push_back ( arr [ j ] ) ; } sort ( v . begin ( ) , v . end ( ) ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v [ x ] ; x ++ ; } v . clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } int main ( ) { int arr [ ] = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; if ( fun ( arr , n , K ) ) cout << " yes " << endl ; else cout << " no " << endl ; return 0 ; }
Sort an Array based on the absolute difference of adjacent elements | C ++ implementation of the above approach ; Function that arrange the array such that all absolute difference between adjacent element are sorted ; To store the resultant array ; Sorting the given array in ascending order ; Variable to represent left and right ends of the given array ; Traversing the answer array in reverse order and arrange the array elements from arr [ ] in reverse order ; Inserting elements in zig - zag manner ; Displaying the resultant array ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortedAdjacentDifferences ( int arr [ ] , int n ) { int ans [ n ] ; sort ( arr + 0 , arr + n ) ; int l = 0 , r = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i % 2 ) { ans [ i ] = arr [ l ] ; l ++ ; } else { ans [ i ] = arr [ r ] ; r -- ; } } for ( int i = 0 ; i < n ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , -2 , 4 , 8 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortedAdjacentDifferences ( arr , n ) ; return 0 ; }
Minimum steps to convert an Array into permutation of numbers from 1 to N | C ++ program to find minimum number of steps to convert a given sequence into a permutation ; Function to find minimum number of steps to convert a given sequence into a permutation ; Sort the given array ; To store the required minimum number of operations ; Find the operations on each step ; Return the answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int get_permutation ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result += abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } int main ( ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << get_permutation ( arr , n ) ; return 0 ; }
Minimum increment or decrement required to sort the array | Top | C ++ program of the above approach ; Dp array to memoized the value recursive call ; Function to find the minimum increment or decrement needed to make the array sorted ; If only one element is present , then arr [ ] is sorted ; If dp [ N ] [ maxE ] is precalculated , then return the result ; Iterate from minE to maxE which placed at previous index ; Update the answer according to recurrence relation ; Memoized the value for dp [ N ] [ maxE ] ; Return the final result ; Driver Code ; Find the minimum and maximum element from the arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1000 ] [ 1000 ] ; int minimumIncDec ( int arr [ ] , int N , int maxE , int minE ) { if ( N == 0 ) { return 0 ; } if ( dp [ N ] [ maxE ] ) return dp [ N ] [ maxE ] ; int ans = INT_MAX ; for ( int k = minE ; k <= maxE ; k ++ ) { int x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = min ( ans , x + abs ( arr [ N - 1 ] - k ) ) ; } dp [ N ] [ maxE ] = ans ; return dp [ N ] [ maxE ] ; } int main ( ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int minE = * min_element ( arr , arr + N ) ; int maxE = * max_element ( arr , arr + N ) ; cout << minimumIncDec ( arr , N , maxE , minE ) ; return 0 ; }
Sort the major diagonal of the matrix | C ++ implementation to sort the major diagonal of the matrix ; Function to sort the major diagonal of the matrix ; Loop to find the ith minimum element from the major diagonal ; Loop to find the minimum element from the unsorted matrix ; Swap to put the minimum element at the beginning of the major diagonal of matrix ; Loop to print the matrix ; Driven Code ; Sort the major Diagonal
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortDiagonal ( int a [ 2 ] [ 2 ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a [ i ] [ i ] , a [ pos ] [ pos ] ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) cout << a [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int a [ 2 ] [ 2 ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; return 0 ; }
Minimum cost to make an Array a permutation of first N natural numbers | C ++ program to calculate minimum cost to make an Array a permutation of first N natural numbers ; Function to calculate minimum cost for making permutation of size N ; sorting the array in ascending order ; To store the required answer ; Traverse the whole array ; Return the required answer ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int make_permutation ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += abs ( i + 1 - arr [ i ] ) ; return ans ; } int main ( ) { int arr [ ] = { 5 , 3 , 8 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << make_permutation ( arr , n ) ; }
Find maximum sum from top to bottom row with no adjacent diagonal elements | C ++ implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver Code ; Function to find maximum path
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maxSum ( vector < vector < int > > & V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { vector < pair < int , int > > aux ; for ( int j = 0 ; j < m ; ++ j ) { aux . push_back ( { V [ i + 1 ] [ j ] , j } ) ; } sort ( aux . begin ( ) , aux . end ( ) ) ; reverse ( aux . begin ( ) , aux . end ( ) ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux [ k ] . second - j == 0 || abs ( aux [ k ] . second - j ) > 1 ) { V [ i ] [ j ] += aux [ k ] . first ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } int main ( ) { vector < vector < int > > V { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . size ( ) ; int m = V [ 0 ] . size ( ) ; cout << maxSum ( V , n , m ) ; return 0 ; }