codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the Sum of the given array using given operations | C ++ Program to maximise the sum of the given array ; Comparator to sort the array in ascending order ; Function to maximise the sum of the given array ; Stores { A [ i ] , B [ i ] } pairs ; Sort in descending order of the values in the array A [ ] ; Stores the maximum sum ; If B [ i ] is equal to 0 ; Simply add A [ i ] to the sum ; Add the highest K numbers ; Subtract from the sum ; Return the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( pair < int , int > p1 , pair < int , int > p2 ) { return p1 . first > p2 . first ; } int maximiseScore ( int A [ ] , int B [ ] , int K , int N ) { vector < pair < int , int > > pairs ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { pairs [ i ] = make_pair ( A [ i ] , B [ i ] ) ; } sort ( pairs . begin ( ) , pairs . end ( ) , compare ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pairs [ i ] . second == 0 ) { sum += pairs [ i ] . first ; } else if ( pairs [ i ] . second == 1 ) { if ( K > 0 ) { sum += pairs [ i ] . first ; K -- ; } else { sum -= pairs [ i ] . first ; } } } return sum ; } int main ( ) { int A [ ] = { 5 , 4 , 6 , 2 , 8 } ; int B [ ] = { 1 , 0 , 1 , 1 , 0 } ; int K = 2 ; int N = sizeof ( A ) / sizeof ( int ) ; cout << maximiseScore ( A , B , K , N ) ; return 0 ; }
Minimize Cost to sort a String in Increasing Order of Frequencies of Characters | C ++ program to implement above approach ; For a single character ; Stores count of repetitions of a character ; If repeating character ; Otherwise ; Store frequency ; Reset count ; Insert the last character block ; Sort the frequencies ; Stores the minimum cost of all operations ; Store the absolute difference of i - th frequencies of ordered and unordered sequences ; Return the minimum cost ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int sortString ( string S ) { vector < int > sorted , original ; bool insert = false ; if ( S . length ( ) == 1 ) { cout << 0 << endl ; } int curr = 1 ; for ( int i = 0 ; i < ( S . length ( ) - 1 ) ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) { curr += 1 ; insert = false ; } else { sorted . push_back ( curr ) ; original . push_back ( curr ) ; curr = 1 ; insert = true ; } } if ( ( S [ ( S . length ( ) - 1 ) ] != S [ ( S . length ( ) - 2 ) ] ) insert == false ) { sorted . push_back ( curr ) ; original . push_back ( curr ) ; } sort ( sorted . begin ( ) , sorted . end ( ) ) ; int t_cost = 0 ; for ( int i = 0 ; i < sorted . size ( ) ; i ++ ) { t_cost += abs ( sorted [ i ] - original [ i ] ) ; } return ( t_cost / 2 ) ; } int main ( ) { string S = " aabbcccdeffffggghhhhhii " ; cout << sortString ( S ) ; return 0 ; }
Sort numbers based on count of letters required to represent them in words | C ++ program to sort the strings based on the numbers of letters required to represent them ; letters [ i ] stores the count of letters required to represent the digit i ; Function to return the sum of letters required to represent N ; Function to sort the array according to the sum of letters to represent n ; Vector to store the digit sum with respective elements ; Inserting digit sum with elements in the vector pair ; Making the vector pair ; Sort the vector , this will sort the pair according to the sum of letters to represent n ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int letters [ ] = { 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 } ; int sumOfLetters ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += letters [ n % 10 ] ; n = n / 10 ; } return sum ; } void sortArr ( int arr [ ] , int n ) { vector < pair < int , int > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( sumOfLetters ( 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 , 31 , 18 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Check if the Left View of the given tree is sorted or not | C ++ implementation to Check if the Left View of the given tree is Sorted or not ; Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; queue to hold values ; variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; variable for initial level ; checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int val ; struct node * right , * left ; } ; struct node * newnode ( int key ) { struct node * temp = new node ; temp -> val = key ; temp -> right = NULL ; temp -> left = NULL ; return temp ; } void func ( node * root ) { queue < node * > q ; bool t = true ; q . push ( root ) ; int i = -1 , j = -1 , k = -1 ; while ( ! q . empty ( ) ) { int h = q . size ( ) ; while ( h > 0 ) { root = q . front ( ) ; if ( i == -1 ) { j = root -> val ; } if ( i == -2 ) { if ( j <= root -> val ) { j = root -> val ; i = -3 ; } else { t = false ; break ; } } if ( root -> left != NULL ) { q . push ( root -> left ) ; } if ( root -> right != NULL ) { q . push ( root -> right ) ; } h = h - 1 ; q . pop ( ) ; } i = -2 ; if ( t == false ) { break ; } } if ( t ) cout << " true " << endl ; else cout << " false " << endl ; } int main ( ) { struct node * root = newnode ( 10 ) ; root -> right = newnode ( 50 ) ; root -> right -> right = newnode ( 15 ) ; root -> left = newnode ( 20 ) ; root -> left -> left = newnode ( 50 ) ; root -> left -> right = newnode ( 23 ) ; root -> right -> left = newnode ( 10 ) ; func ( root ) ; return 0 ; }
Divide a sorted array in K parts with sum of difference of max and min minimized in each part | C ++ program to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Function to find the minimum sum of differences possible for the given array when the array is divided into K subarrays ; Array to store the differences between two adjacent elements ; Iterating through the array ; Storing differences to p ; Sorting p in descending order ; Sum of the first k - 1 values of p ; Computing the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int calculate_minimum_split ( int n , int a [ ] , int k ) { int p [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; sort ( p , p + n - 1 , greater < int > ( ) ) ; int min_sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; int res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } int main ( ) { int arr [ 6 ] = { 4 , 8 , 15 , 16 , 23 , 42 } ; int k = 3 ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << calculate_minimum_split ( n , arr , k ) ; }
Sort an Array of dates in ascending order using Custom Comparator | C ++ implementation to sort the array of dates in the form of " DD - MM - YYYY " using custom comparator ; Comparator to sort the array of dates ; Condition to check the year ; Condition to check the month ; Condition to check the day ; Function that prints the dates in ascensding order ; Sort the dates using library sort function with custom Comparator ; Loop to print the dates ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int myCompare ( string date1 , string date2 ) { string day1 = date1 . substr ( 0 , 2 ) ; string month1 = date1 . substr ( 3 , 2 ) ; string year1 = date1 . substr ( 6 , 4 ) ; string day2 = date2 . substr ( 0 , 2 ) ; string month2 = date2 . substr ( 3 , 2 ) ; string year2 = date2 . substr ( 6 , 4 ) ; if ( year1 < year2 ) return 1 ; if ( year1 > year2 ) return 0 ; if ( month1 < month2 ) return 1 ; if ( month1 > month2 ) return 0 ; if ( day1 < day2 ) return 1 ; if ( day1 > day2 ) return 0 ; } void printDatesAscending ( vector < string > arr ) { sort ( arr . begin ( ) , arr . end ( ) , myCompare ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) cout << arr [ i ] << " STRNEWLINE " ; } int main ( ) { vector < string > arr ; arr . push_back ( "25-08-1996" ) ; arr . push_back ( "03-08-1970" ) ; arr . push_back ( "09-04-1994" ) ; arr . push_back ( "29-08-1996" ) ; arr . push_back ( "14-02-1972" ) ; printDatesAscending ( arr ) ; return 0 ; }
Sort an Array of Strings according to the number of Vowels in them | C ++ implementation of the approach ; Function to check the Vowel ; Returns count of vowels in str ; if ( isVowel ( str [ i ] ) ) Check for vowel ; Function to sort the array according to the number of the vowels ; Vector to store the number of vowels with respective elements ; Inserting number of vowels with respective strings in the vector pair ; Sort the vector , this will sort the pair according to the number of vowels ; Print the sorted vector content ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isVowel ( char ch ) { ch = toupper ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } int countVowels ( string str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) ++ count ; return count ; } void sortArr ( string arr [ ] , int n ) { vector < pair < int , string > > vp ; for ( int i = 0 ; i < n ; i ++ ) { vp . push_back ( make_pair ( countVowels ( arr [ i ] ) , arr [ i ] ) ) ; } sort ( vp . begin ( ) , vp . end ( ) ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) cout << vp [ i ] . second << " ▁ " ; } int main ( ) { string arr [ ] = { " lmno " , " pqrst " , " aeiou " , " xyz " } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortArr ( arr , n ) ; return 0 ; }
Find the minimum cost to cross the River | C ++ implementation of the approach ; Function to return the minimum cost ; Sort the price array ; Calculate minimum price of n - 2 most costly person ; Both the ways as discussed above ; Calculate the minimum price of the two cheapest person ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE int minimumCost ( ll price [ ] , int n ) { sort ( price , price + n ) ; ll totalCost = 0 ; for ( int i = n - 1 ; i > 1 ; i -= 2 ) { if ( i == 2 ) { totalCost += price [ 2 ] + price [ 0 ] ; } else { ll price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; ll price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += min ( price_first , price_second ) ; } } if ( n == 1 ) { totalCost += price [ 0 ] ; } else { totalCost += price [ 1 ] ; } return totalCost ; } int main ( ) { ll price [ ] = { 30 , 40 , 60 , 70 } ; int n = sizeof ( price ) / sizeof ( price [ 0 ] ) ; cout << minimumCost ( price , n ) ; return 0 ; }
Minimize the sum of differences of consecutive elements after removing exactly K elements | C ++ implementation of the above approach . ; states of DP ; function to find minimum sum ; base - case ; if state is solved before , return ; marking the state as solved ; recurrence relation ; driver function ; input values ; callin the required function ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define N  100 NEW_LINE #define INF  1000000 NEW_LINE int dp [ N ] [ N ] ; bool vis [ N ] [ N ] ; int findSum ( int * arr , int n , int k , int l , int r ) { if ( ( l ) + ( n - 1 - r ) == k ) return arr [ r ] - arr [ l ] ; if ( vis [ l ] [ r ] ) return dp [ l ] [ r ] ; vis [ l ] [ r ] = 1 ; return dp [ l ] [ r ] = min ( findSum ( arr , n , k , l , r - 1 ) , findSum ( arr , n , k , l + 1 , r ) ) ; } int32_t main ( ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = sizeof ( arr ) / sizeof ( int ) ; cout << findSum ( arr , n , k , 0 , n - 1 ) ; }
Find Kth element in an array containing odd elements first and then even elements | C ++ implementation of the approach ; Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getNumber ( int n , int k ) { int pos ; if ( n % 2 == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } int main ( ) { int n = 8 , k = 5 ; cout << getNumber ( n , k ) ; return 0 ; }
All unique combinations whose sum equals to K | C ++ implementation of the approach ; Function to find all unique combination of given elements such that their sum is K ; If a unique combination is found ; For all other combinations ; Check if the sum exceeds K ; Check if it is repeated or not ; Take the element into the combination ; Recursive call ; Remove element from the combination ; Function to find all combination of the given elements ; Sort the given elements ; To store combination ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void unique_combination ( int l , int sum , int K , vector < int > & local , vector < int > & A ) { if ( sum == K ) { cout << " { " ; for ( int i = 0 ; i < local . size ( ) ; i ++ ) { if ( i != 0 ) cout << " ▁ " ; cout << local [ i ] ; if ( i != local . size ( ) - 1 ) cout << " , ▁ " ; } cout << " } " << endl ; return ; } for ( int i = l ; i < A . size ( ) ; i ++ ) { if ( sum + A [ i ] > K ) continue ; if ( i > l and A [ i ] == A [ i - 1 ] ) continue ; local . push_back ( A [ i ] ) ; unique_combination ( i + 1 , sum + A [ i ] , K , local , A ) ; local . pop_back ( ) ; } } void Combination ( vector < int > A , int K ) { sort ( A . begin ( ) , A . end ( ) ) ; vector < int > local ; unique_combination ( 0 , 0 , K , local , A ) ; } int main ( ) { vector < int > A = { 10 , 1 , 2 , 7 , 6 , 1 , 5 } ; int K = 8 ; Combination ( A , K ) ; return 0 ; }
Find the longest string that can be made up of other strings from the array | C ++ implementation of the approach ; Comparator to sort the string by their lengths in decreasing order ; Function that returns true if string s can be made up of by other two string from the array after concatenating one after another ; If current string has been processed before ; If current string is found in the map and it is not the string under consideration ; Split the string into two contiguous sub - strings ; If left sub - string is found in the map and the right sub - string can be made from the strings from the given array ; If everything failed , we return false ; Function to return the longest string that can made be made up from the other string of the given array ; Put all the strings in the map ; Sort the string in decreasing order of their lengths ; Starting from the longest string ; If current string can be made up from other strings ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( string s1 , string s2 ) { return s1 . size ( ) > s2 . size ( ) ; } bool canbuildword ( string & s , bool isoriginalword , map < string , bool > & mp ) { if ( mp . find ( s ) != mp . end ( ) && mp [ s ] == 0 ) return false ; if ( mp . find ( s ) != mp . end ( ) && mp [ s ] == 1 && isoriginalword == 0 ) { return true ; } for ( int i = 1 ; i < s . length ( ) ; i ++ ) { string left = s . substr ( 0 , i ) ; string right = s . substr ( i ) ; if ( mp . find ( left ) != mp . end ( ) && mp [ left ] == 1 && canbuildword ( right , 0 , mp ) ) { return true ; } } mp [ s ] = 0 ; return false ; } string printlongestword ( vector < string > listofwords ) { map < string , bool > mp ; for ( string s : listofwords ) { mp [ s ] = 1 ; } sort ( listofwords . begin ( ) , listofwords . end ( ) , compare ) ; for ( string s : listofwords ) { if ( canbuildword ( s , 1 , mp ) ) return s ; } return " - 1" ; } int main ( ) { vector < string > listofwords = { " geeks " , " for " , " geeksfor " , " geeksforgeeks " } ; cout << printlongestword ( listofwords ) ; return 0 ; }
Find a triplet in an array whose sum is closest to a given number | C ++ implementation of the approach ; Function to return the sum of a triplet which is closest to x ; To store the closest sum ; Run three nested loops each loop for each element of triplet ; update the closestSum ; Return the closest sum found ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solution ( vector < int > & arr , int x ) { int closestSum = INT_MAX ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < arr . size ( ) ; j ++ ) { for ( int k = j + 1 ; k < arr . size ( ) ; k ++ ) { if ( abs ( x - closestSum ) > abs ( x - ( arr [ i ] + arr [ j ] + arr [ k ] ) ) ) closestSum = ( arr [ i ] + arr [ j ] + arr [ k ] ) ; } } } return closestSum ; } int main ( ) { vector < int > arr = { -1 , 2 , 1 , -4 } ; int x = 1 ; cout << solution ( arr , x ) ; return 0 ; }
Build original array from the given sub | C ++ implementation of the approach ; Function to add edge to graph ; Function to calculate indegrees of all the vertices ; If there is an edge from i to x then increment indegree of x ; Function to perform topological sort ; Push every node to the queue which has no incoming edge ; Since edge u is removed , update the indegrees of all the nodes which had an incoming edge from u ; Function to generate the array from the given sub - sequences ; Create the graph from the input sub - sequences ; Add edge between every two consecutive elements of the given sub - sequences ; Get the indegrees for all the vertices ; Get the topological order of the created graph ; Driver code ; Size of the required array ; Given sub - sequences of the array ; Get the resultant array as vector ; Printing the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void addEdge ( vector < int > adj [ ] , int u , int v ) { adj [ u ] . push_back ( v ) ; } void getindeg ( vector < int > adj [ ] , int V , vector < int > & indeg ) { for ( int i = 0 ; i < V ; i ++ ) { for ( auto x : adj [ i ] ) { indeg [ x ] ++ ; } } } vector < int > topo ( vector < int > adj [ ] , int V , vector < int > & indeg ) { queue < int > q ; for ( int i = 0 ; i < V ; i ++ ) { if ( indeg [ i ] == 0 ) q . push ( i ) ; } vector < int > res ; while ( ! q . empty ( ) ) { int u = q . front ( ) ; q . pop ( ) ; res . push_back ( u ) ; for ( auto x : adj [ u ] ) { indeg [ x ] -- ; if ( indeg [ x ] == 0 ) q . push ( x ) ; } } return res ; } vector < int > makearray ( vector < vector < int > > v , int V ) { vector < int > adj [ V ] ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = 0 ; j < v [ i ] . size ( ) - 1 ; j ++ ) { addEdge ( adj , v [ i ] [ j ] , v [ i ] [ j + 1 ] ) ; } } vector < int > indeg ( V , 0 ) ; getindeg ( adj , V , indeg ) ; vector < int > res = topo ( adj , V , indeg ) ; return res ; } int main ( ) { int n = 10 ; vector < vector < int > > subseqs { { 9 , 1 , 2 , 8 , 3 } , { 6 , 1 , 2 } , { 9 , 6 , 3 , 4 } , { 5 , 2 , 7 } , { 0 , 9 , 5 , 4 } } ; vector < int > res = makearray ( subseqs , n ) ; for ( auto x : res ) { cout << x << " ▁ " ; } return 0 ; }
Sum of Semi | C ++ implementation of the approach ; Vector to store the primes ; Create a boolean array " prime [ 0 . . n ] " ; Initialize all prime values to be true ; 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 ; Print all prime numbers ; Function to return the semi - prime sum ; Variable to store the sum of semi - primes ; Iterate over the prime values ; Break the loop once the product exceeds N ; Add valid products which are less than or equal to N each product is a semi - prime number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define ll  long long int NEW_LINE vector < ll > pr ; bool prime [ 10000000 + 1 ] ; void sieve ( ll n ) { for ( int i = 2 ; i <= n ; i += 1 ) { prime [ i ] = 1 ; } for ( ll p = 2 ; ( ll ) p * ( ll ) p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( ll i = ( ll ) p * ( ll ) p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( ll p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . push_back ( p ) ; } ll SemiPrimeSum ( ll N ) { ll ans = 0 ; for ( int i = 0 ; i < pr . size ( ) ; i += 1 ) { for ( int j = i ; j < pr . size ( ) ; j += 1 ) { if ( ( ll ) pr [ i ] * ( ll ) pr [ j ] > N ) break ; ans += ( ll ) pr [ i ] * ( ll ) pr [ j ] ; } } return ans ; } int main ( ) { ll N = 6 ; sieve ( N ) ; cout << SemiPrimeSum ( N ) ; return 0 ; }
Compress the array into Ranges | C ++ program to compress the array ranges ; Function to compress the array ranges ; start iteration from the ith array element ; loop until arr [ i + 1 ] == arr [ i ] and increment j ; if the program do not enter into the above while loop this means that ( i + 1 ) th element is not consecutive to i th element ; increment i for next iteration ; print the consecutive range found ; move i jump directly to j + 1 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void compressArr ( int arr [ ] , int n ) { int i = 0 , j = 0 ; sort ( arr , arr + n ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { cout << arr [ i ] << " ▁ " ; i ++ ; } else { cout << arr [ i ] << " - " << arr [ j ] << " ▁ " ; i = j + 1 ; } } } int main ( ) { int n = 7 ; int arr [ n ] = { 1 , 3 , 4 , 5 , 6 , 9 , 10 } ; compressArr ( arr , n ) ; }
Remove elements to make array sorted | C ++ implementation of the approach ; Function to sort the array by removing misplaced elements ; brr [ ] is used to store the sorted array elements ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeElements ( int arr [ ] , int n ) { int brr [ n ] , l = 1 ; brr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( brr [ l - 1 ] <= arr [ i ] ) { brr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) cout << brr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; removeElements ( arr , n ) ; return 0 ; }
Remove elements to make array sorted | C ++ implementation of the approach ; Function to sort the array by removing misplaced elements ; l stores the index ; Print the sorted array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void removeElements ( int arr [ ] , int n ) { int l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ l - 1 ] <= arr [ i ] ) { arr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; removeElements ( arr , n ) ; return 0 ; }
Find number from its divisors | C ++ implementation of the approach ; Function that returns X ; Sort the given array ; Get the possible X ; Container to store divisors ; Find the divisors of x ; Check if divisor ; sort the vec because a is sorted and we have to compare all the elements ; if size of both vectors is not same then we are sure that both vectors can 't be equal ; Check if a and vec have same elements in them ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findX ( int a [ ] , int n ) { sort ( a , a + n ) ; int x = a [ 0 ] * a [ n - 1 ] ; vector < int > vec ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { vec . push_back ( i ) ; if ( ( x / i ) != i ) vec . push_back ( x / i ) ; } } sort ( vec . begin ( ) , vec . end ( ) ) ; if ( vec . size ( ) != n ) return -1 ; else { int i = 0 ; for ( auto it : vec ) { if ( a [ i ++ ] != it ) return -1 ; } } return x ; } int main ( ) { int a [ ] = { 2 , 5 , 4 , 10 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << findX ( a , n ) ; return 0 ; }