codeparrot/xlcost-text-to-code huggingface dataset:
Sort the Queue using Recursion | C ++ implementation of the approach ; Function to push element in last by popping from front until size becomes 0 ; Base condition ; pop front element and push this last in a queue ; Recursive call for pushing element ; Function to push an element in the queue while maintaining the sorted order ; Base condition ; If current element is less than the element at the front ; Call stack with front of queue ; Recursive call for inserting a front element of the queue to the last ; Push front element into last in a queue ; Recursive call for pushing element in a queue ; Function to sort the given queue using recursion ; Return if queue is empty ; Get the front element which will be stored in this variable throughout the recursion stack ; Remove the front element ; Recursive call ; Push the current element into the queue according to the sorting order ; Driver code ; Push elements to the queue ; Sort the queue ; Print the elements of the queue after sorting
#include <bits/stdc++.h> NEW_LINE using namespace std ; void FrontToLast ( queue < int > & q , int qsize ) { if ( qsize <= 0 ) return ; q . push ( q . front ( ) ) ; q . pop ( ) ; FrontToLast ( q , qsize - 1 ) ; } void pushInQueue ( queue < int > & q , int temp , int qsize ) { if ( q . empty ( ) qsize == 0 ) { q . push ( temp ) ; return ; } else if ( temp <= q . front ( ) ) { q . push ( temp ) ; FrontToLast ( q , qsize ) ; } else { q . push ( q . front ( ) ) ; q . pop ( ) ; pushInQueue ( q , temp , qsize - 1 ) ; } } void sortQueue ( queue < int > & q ) { if ( q . empty ( ) ) return ; int temp = q . front ( ) ; q . pop ( ) ; sortQueue ( q ) ; pushInQueue ( q , temp , q . size ( ) ) ; } int main ( ) { queue < int > qu ; qu . push ( 10 ) ; qu . push ( 7 ) ; qu . push ( 16 ) ; qu . push ( 9 ) ; qu . push ( 20 ) ; qu . push ( 5 ) ; sortQueue ( qu ) ; while ( ! qu . empty ( ) ) { cout << qu . front ( ) << " ▁ " ; qu . pop ( ) ; } }
Sort the character array based on ASCII % N | C ++ implementation of the approach ; A utility function to swap two elements ; This function takes last element as pivot , places the pivot element at its correct position in sorted array , and places all smaller ( smaller than pivot ) to left of pivot and all greater elements to right of pivot ; pivot ; Index of smaller element ; If current element is smaller than or equal to pivot Instead of values , ASCII % m values are compared ; Increment index of smaller element ; swap ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print the given array ; Driver code ; Sort the given array ; Print the sorted array
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( char * a , char * b ) { char t = * a ; * a = * b ; * b = t ; } int partition ( char arr [ ] , int low , int high , int mod ) { char pivot = arr [ high ] ; int i = ( low - 1 ) ; int piv = pivot % mod ; for ( int j = low ; j <= high - 1 ; j ++ ) { int a = arr [ j ] % mod ; if ( a <= piv ) { i ++ ; swap ( & arr [ i ] , & arr [ j ] ) ; } } swap ( & arr [ i + 1 ] , & arr [ high ] ) ; return ( i + 1 ) ; } void quickSort ( char arr [ ] , int low , int high , int mod ) { if ( low < high ) { int pi = partition ( arr , low , high , mod ) ; quickSort ( arr , low , pi - 1 , mod ) ; quickSort ( arr , pi + 1 , high , mod ) ; } } void printArray ( char arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { char arr [ ] = { ' g ' , ' e ' , ' e ' , ' k ' , ' s ' } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int mod = 8 ; quickSort ( arr , 0 , n - 1 , mod ) ; printArray ( arr , n ) ; return 0 ; }
Two nodes of a BST are swapped , correct the BST | Set | C ++ implementation of the above approach ; A binary tree node has data , pointer to left child and a pointer to right child ; Utility function for insertion sort ; Utility function to create a vector with inorder traversal of a binary tree ; Base cases ; Recursive call for left subtree ; Insert node into vector ; Recursive call for right subtree ; Function to exchange data for incorrect nodes ; Base cases ; Recursive call to find the node in left subtree ; Check if current node is incorrect and exchange ; Recursive call to find the node in right subtree ; Primary function to fix the two nodes ; Vector to store the inorder traversal of tree ; Function call to insert nodes into vector ; create a copy of the vector ; Sort the original vector thereby making it a valid BST 's inorder ; Traverse through both vectors and compare misplaced values in original BST ; Find the mismatched values and interchange them ; Find and exchange the data of the two nodes ; As it given only two values are wrong we don 't need to check further ; Return the root of corrected BST ; A utility function to print Inorder traversal ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left , * right ; node ( int x ) { data = x ; left = right = NULL ; } } ; void insertionSort ( vector < int > & v , int n ) { int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = v [ i ] ; j = i - 1 ; while ( j >= 0 && v [ j ] > key ) { v [ j + 1 ] = v [ j ] ; j = j - 1 ; } v [ j + 1 ] = key ; } } void inorder ( node * root , vector < int > & v ) { if ( ! root ) return ; inorder ( root -> left , v ) ; v . push_back ( root -> data ) ; inorder ( root -> right , v ) ; } void find ( node * root , int res , int res2 ) { if ( ! root ) { return ; } find ( root -> left , res , res2 ) ; if ( root -> data == res ) { root -> data = res2 ; } else if ( root -> data == res2 ) { root -> data = res ; } find ( root -> right , res , res2 ) ; } struct node * correctBST ( struct node * root ) { vector < int > v ; inorder ( root , v ) ; vector < int > v1 = v ; insertionSort ( v , v . size ( ) ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v [ i ] != v1 [ i ] ) { find ( root , v1 [ i ] , v [ i ] ) ; break ; } } return root ; } void printInorder ( struct node * node ) { if ( node == NULL ) return ; printInorder ( node -> left ) ; printf ( " % d ▁ " , node -> data ) ; printInorder ( node -> right ) ; } int main ( ) { struct node * root = new node ( 6 ) ; root -> left = new node ( 10 ) ; root -> right = new node ( 2 ) ; root -> left -> left = new node ( 1 ) ; root -> left -> right = new node ( 3 ) ; root -> right -> right = new node ( 12 ) ; root -> right -> left = new node ( 7 ) ; printf ( " Inorder ▁ Traversal ▁ of ▁ the " ) ; printf ( " original ▁ tree ▁ STRNEWLINE " ) ; printInorder ( root ) ; correctBST ( root ) ; printf ( " Inorder Traversal of the " printf ( " fixed ▁ tree ▁ STRNEWLINE " ) ; printInorder ( root ) ; return 0 ; }
Iterative selection sort for linked list | C ++ implementation of the approach ; Linked List Node ; Utility function to create a new Linked List Node ; Function to sort a linked list using selection sort algorithm by swapping the next pointers ; While b is not the last node ; While d is pointing to a valid node ; If d appears immediately after b ; Case 1 : b is the head of the linked list ; Move d before b ; Swap b and d pointers ; Update the head ; Skip to the next element as it is already in order ; Case 2 : b is not the head of the linked list ; Move d before b ; Swap b and d pointers ; Skip to the next element as it is already in order ; If b and d have some non - zero number of nodes in between them ; Case 3 : b is the head of the linked list ; Swap b -> next and d -> next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update the head ; Case 4 : b is not the head of the linked list ; Swap b -> next and d -> next ; Swap b and d pointers ; Skip to the next element as it is already in order ; Update c and skip to the next element as it is already in order ; Function to print the list ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; Node * next ; } ; Node * newNode ( int val ) { Node * temp = new Node ; temp -> data = val ; temp -> next = NULL ; return temp ; } Node * selectionSort ( Node * head ) { Node * a , * b , * c , * d , * r ; a = b = head ; while ( b -> next ) { c = d = b -> next ; while ( d ) { if ( b -> data > d -> data ) { if ( b -> next == d ) { if ( b == head ) { b -> next = d -> next ; d -> next = b ; r = b ; b = d ; d = r ; c = d ; head = b ; d = d -> next ; } else { b -> next = d -> next ; d -> next = b ; a -> next = d ; r = b ; b = d ; d = r ; c = d ; d = d -> next ; } } else { if ( b == head ) { r = b -> next ; b -> next = d -> next ; d -> next = r ; c -> next = b ; r = b ; b = d ; d = r ; c = d ; d = d -> next ; head = b ; } else { r = b -> next ; b -> next = d -> next ; d -> next = r ; c -> next = b ; a -> next = d ; r = b ; b = d ; d = r ; c = d ; d = d -> next ; } } } else { c = d ; d = d -> next ; } } a = b ; b = b -> next ; } return head ; } void printList ( Node * head ) { while ( head ) { cout << head -> data << " ▁ " ; head = head -> next ; } } int main ( ) { Node * head = newNode ( 5 ) ; head -> next = newNode ( 4 ) ; head -> next -> next = newNode ( 3 ) ; head = selectionSort ( head ) ; printList ( head ) ; return 0 ; }
Find original numbers from gcd ( ) every pair | C ++ implementation of the approach ; Utility function to print the contents of an array ; Function to find the required numbers ; Sort array in decreasing order ; Count frequency of each element ; Size of the resultant array ; Store the highest element in the resultant array ; Decrement the frequency of that element ; Compute GCD ; Decrement GCD value by 2 ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } void findNumbers ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int freq [ arr [ 0 ] + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int size = sqrt ( n ) ; int brr [ size ] = { 0 } , x , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ arr [ i ] ] > 0 ) { brr [ l ] = arr [ i ] ; freq [ brr [ l ] ] -- ; l ++ ; for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { x = __gcd ( arr [ i ] , brr [ j ] ) ; freq [ x ] -= 2 ; } } } } printArr ( brr , size ) ; } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 5 , 5 , 5 , 7 , 10 , 12 , 2 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findNumbers ( arr , n ) ; return 0 ; }
Bitwise AND of N binary strings | C ++ implementation of the above approach ; Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ' 0 's  in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; string strBitwiseAND ( string * arr , int n ) { string res ; int smallest_size = INT_MAX ; int largest_size = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { reverse ( arr [ i ] . begin ( ) , arr [ i ] . end ( ) ) ; smallest_size = min ( smallest_size , ( int ) arr [ i ] . size ( ) ) ; largest_size = max ( largest_size , ( int ) arr [ i ] . size ( ) ) ; } for ( int i = 0 ; i < smallest_size ; i ++ ) { bool all_ones = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] [ i ] == '0' ) { all_ones = false ; break ; } } res += ( all_ones ? '1' : '0' ) ; } for ( int i = 0 ; i < largest_size - smallest_size ; i ++ ) res += '0' ; reverse ( res . begin ( ) , res . end ( ) ) ; return res ; } int main ( ) { string arr [ ] = { "101" , "110110" , "111" } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << strBitwiseAND ( arr , n ) ; return 0 ; }
Print all paths from a given source to a destination using BFS | C ++ program to print all paths of source to destination in given graph ; utility function for printing the found path in graph ; utility function to check if current vertex is already present in path ; utility function for finding paths in graph from source to destination ; create a queue which stores the paths ; path vector to store the current path ; if last vertex is the desired destination then print the path ; traverse to all the nodes connected to current vertex and push new path to queue ; driver program ; number of vertices ; construct a graph ; function for finding the paths
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printpath ( vector < int > & path ) { int size = path . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) cout << path [ i ] << " ▁ " ; cout << endl ; } int isNotVisited ( int x , vector < int > & path ) { int size = path . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) if ( path [ i ] == x ) return 0 ; return 1 ; } void findpaths ( vector < vector < int > > & g , int src , int dst , int v ) { queue < vector < int > > q ; vector < int > path ; path . push_back ( src ) ; q . push ( path ) ; while ( ! q . empty ( ) ) { path = q . front ( ) ; q . pop ( ) ; int last = path [ path . size ( ) - 1 ] ; if ( last == dst ) printpath ( path ) ; for ( int i = 0 ; i < g [ last ] . size ( ) ; i ++ ) { if ( isNotVisited ( g [ last ] [ i ] , path ) ) { vector < int > newpath ( path ) ; newpath . push_back ( g [ last ] [ i ] ) ; q . push ( newpath ) ; } } } } int main ( ) { vector < vector < int > > g ; int v = 4 ; g . resize ( 4 ) ; g [ 0 ] . push_back ( 3 ) ; g [ 0 ] . push_back ( 1 ) ; g [ 0 ] . push_back ( 2 ) ; g [ 1 ] . push_back ( 3 ) ; g [ 2 ] . push_back ( 0 ) ; g [ 2 ] . push_back ( 1 ) ; int src = 2 , dst = 3 ; cout << " path ▁ from ▁ src ▁ " << src << " ▁ to ▁ dst ▁ " << dst << " ▁ are ▁ STRNEWLINE " ; findpaths ( g , src , dst , v ) ; return 0 ; }
Rearrange Odd and Even values in Alternate Fashion in Ascending Order | C ++ implementation of the above approach ; Sort the array ; vector < int > v1 ; to insert even values vector < int > v2 ; to insert odd values ; Set flag to true if first element is even ; Start rearranging array ; If first element is even ; Else , first element is Odd ; Print the rearranged array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void AlternateRearrange ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) v1 . push_back ( arr [ i ] ) ; else v2 . push_back ( arr [ i ] ) ; int index = 0 , i = 0 , j = 0 ; bool flag = false ; if ( arr [ 0 ] % 2 == 0 ) flag = true ; while ( index < n ) { if ( flag == true ) { arr [ index ++ ] = v1 [ i ++ ] ; flag = ! flag ; } else { arr [ index ++ ] = v2 [ j ++ ] ; flag = ! flag ; } } for ( i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 9 , 8 , 13 , 2 , 19 , 14 } ; int n = sizeof ( arr ) / sizeof ( int ) ; AlternateRearrange ( arr , n ) ; return 0 ; }
Convert given array to Arithmetic Progression by adding an element | C ++ implementation of the approach ; Function to return the number to be added ; If difference of the current consecutive elements is different from the common difference ; If number has already been chosen then it 's not possible  to add another number ; If the current different is twice the common difference then a number can be added midway from current and previous element ; Number has been chosen ; It 's not possible to maintain  the common difference ; Return last element + common difference if no element is chosen and the array is already in AP ; Else return the chosen number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getNumToAdd ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int d = arr [ 1 ] - arr [ 0 ] ; int numToAdd = -1 ; bool numAdded = false ; for ( int i = 2 ; i < n ; i ++ ) { int diff = arr [ i ] - arr [ i - 1 ] ; if ( diff != d ) { if ( numAdded ) return -1 ; if ( diff == 2 * d ) { numToAdd = arr [ i ] - d ; numAdded = true ; } else return -1 ; } } if ( numToAdd == -1 ) return ( arr [ n - 1 ] + d ) ; return numToAdd ; } int main ( ) { int arr [ ] = { 1 , 3 , 5 , 7 , 11 , 13 , 15 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << getNumToAdd ( arr , n ) ; }
Minimum Numbers of cells that are connected with the smallest path between 3 given cells | C ++ implementation of the approach ; Function to return the minimum cells that are connected via the minimum length path ; Array to store column number of the given cells ; Sort cells in ascending order of row number ; Middle row number ; Set pair to store required cells ; Range of column number ; Store all cells of middle row within column number range ; Final step to store all the column number of 1 st and 3 rd cell upto MidRow ; Driver Function ; vector pair to store X , Y , Z
#include <bits/stdc++.h> NEW_LINE using namespace std ; int Minimum_Cells ( vector < pair < int , int > > v ) { int col [ 3 ] , i , j ; for ( i = 0 ; i < 3 ; i ++ ) { int column_number = v [ i ] . second ; col [ i ] = column_number ; } sort ( col , col + 3 ) ; sort ( v . begin ( ) , v . end ( ) ) ; int MidRow = v [ 1 ] . first ; set < pair < int , int > > s ; int Maxcol = col [ 2 ] , MinCol = col [ 0 ] ; for ( i = MinCol ; i <= Maxcol ; i ++ ) { s . insert ( { MidRow , i } ) ; } for ( i = 0 ; i < 3 ; i ++ ) { if ( v [ i ] . first == MidRow ) continue ; for ( j = min ( v [ i ] . first , MidRow ) ; j <= max ( v [ i ] . first , MidRow ) ; j ++ ) { s . insert ( { j , v [ i ] . second } ) ; } } return s . size ( ) ; } int main ( ) { vector < pair < int , int > > v = { { 0 , 0 } , { 1 , 1 } , { 2 , 2 } } ; cout << Minimum_Cells ( v ) ; return 0 ; }
Get maximum items when other items of total cost of an item are free | C ++ implementation of the above approach ; Function to count the total number of items ; Sort the prices ; Choose the last element ; Initial count of item ; Sum to keep track of the total price of free items ; If total is less than or equal to z then we will add 1 to the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int items ( int n , int a [ ] ) { sort ( a , a + n ) ; int z = a [ n - 1 ] ; int x = 1 ; int s = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { s += a [ i ] ; if ( s <= z ) x += 1 ; else break ; } return x ; } int main ( ) { int n = 5 ; int a [ ] = { 5 , 3 , 1 , 5 , 6 } ; cout << items ( n , a ) ; }
Sorting array elements with set bits equal to K | C ++ program for sorting array elements with set bits equal to K ; Function to sort elements with set bits equal to k ; initialise two vectors ; first vector contains indices of required element ; second vector contains required elements ; sorting the elements in second vector ; replacing the elements with k set bits with the sorted elements ; printing the new sorted array elements ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void sortWithSetbits ( int arr [ ] , int n , int k ) { vector < int > v1 , v2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( __builtin_popcount ( arr [ i ] ) == k ) { v1 . push_back ( i ) ; v2 . push_back ( arr [ i ] ) ; } } sort ( v2 . begin ( ) , v2 . end ( ) ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) arr [ v1 [ i ] ] = v2 [ i ] ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 14 , 255 , 1 , 7 , 13 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 3 ; sortWithSetbits ( arr , n , k ) ; return 0 ; }
Minimum boxes required to carry all gifts | CPP implementation of above approach ; Function to return number of boxes ; Sort the boxes in ascending order ; Try to fit smallest box with current heaviest box ( from right side ) ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numBoxes ( int A [ ] , int n , int K ) { sort ( A , A + n ) ; int i = 0 , j = n - 1 ; int ans = 0 ; while ( i <= j ) { ans ++ ; if ( A [ i ] + A [ j ] <= K ) i ++ ; j -- ; } return ans ; } int main ( ) { int A [ ] = { 3 , 2 , 2 , 1 } , K = 3 ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; cout << numBoxes ( A , n , K ) ; return 0 ; }
Count triplets in a sorted doubly linked list whose product is equal to a given value x | C ++ implementation to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; structure of node of doubly linked list ; function to count triplets in a sorted doubly linked list whose product is equal to a given value ' x ' ; unordered_map ' um ' implemented as hash table ; insert the < node data , node pointer > tuple in ' um ' ; generate all possible pairs ; p_product = product of elements in the current pair ; if ' x / p _ product ' is present in ' um ' and either of the two nodes are not equal to the ' um [ x / p _ product ] ' node ; increment count ; required count of triplets division by 3 as each triplet is counted 3 times ; A utility function to insert a new node at the beginning of doubly linked list ; allocate node ; put in the data ; Driver program to test above functions ; start with an empty doubly linked list ; insert values in sorted order
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * next , * prev ; } ; int countTriplets ( struct Node * head , int x ) { struct Node * ptr , * ptr1 , * ptr2 ; int count = 0 ; unordered_map < int , Node * > um ; for ( ptr = head ; ptr != NULL ; ptr = ptr -> next ) um [ ptr -> data ] = ptr ; for ( ptr1 = head ; ptr1 != NULL ; ptr1 = ptr1 -> next ) for ( ptr2 = ptr1 -> next ; ptr2 != NULL ; ptr2 = ptr2 -> next ) { int p_product = ( ptr1 -> data * ptr2 -> data ) ; if ( um . find ( x / p_product ) != um . end ( ) && um [ x / p_product ] != ptr1 && um [ x / p_product ] != ptr2 ) count ++ ; } return ( count / 3 ) ; } void insert ( struct Node * * head , int data ) { struct Node * temp = new Node ( ) ; temp -> data = data ; temp -> next = temp -> prev = NULL ; if ( ( * head ) == NULL ) ( * head ) = temp ; else { temp -> next = * head ; ( * head ) -> prev = temp ; ( * head ) = temp ; } } int main ( ) { struct Node * head = NULL ; insert ( & head , 9 ) ; insert ( & head , 8 ) ; insert ( & head , 6 ) ; insert ( & head , 5 ) ; insert ( & head , 4 ) ; insert ( & head , 2 ) ; insert ( & head , 1 ) ; int x = 8 ; cout << " Count ▁ = ▁ " << countTriplets ( head , x ) ; return 0 ; }
Maximize the profit by selling at | C ++ implementation of above approach : ; Function to find profit ; Calculating profit for each gadget ; sort the profit array in descending order ; variable to calculate total profit ; check for best M profits ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int solve ( int N , int M , int cp [ ] , int sp [ ] ) { int profit [ N ] ; for ( int i = 0 ; i < N ; i ++ ) profit [ i ] = sp [ i ] - cp [ i ] ; sort ( profit , profit + N , greater < int > ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( profit [ i ] > 0 ) sum += profit [ i ] ; else break ; } return sum ; } int main ( ) { int N = 5 , M = 3 ; int CP [ ] = { 5 , 10 , 35 , 7 , 23 } ; int SP [ ] = { 11 , 10 , 0 , 9 , 19 } ; cout << solve ( N , M , CP , SP ) ; return 0 ; }
Find the largest number that can be formed with the given digits | C ++ program to generate largest possible number with given digits ; Function to generate largest possible number with given digits ; sort the given array in descending order ; generate the number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxNum ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int num = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { num = num * 10 + arr [ i ] ; } return num ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << findMaxNum ( arr , n ) ; return 0 ; }