codeparrot/xlcost-text-to-code huggingface dataset:
Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase | C ++ program for the above approach ; Function to find the minimum cost to convert all distinct characters to either uppercase or lowercase ; Store the size of the string ; Stores the frequency of lowercase & uppercase characters respectively ; Traverse the string S ; Update uppercase frequency of s [ i ] ; Otherwise , update lowercase frequency of s [ i ] ; Stores if the i - th character should be lowercase or not ; Iterate over the range [ 0 , 25 ] ; If the character is present in the string ; Store the cost to convert every occurence of i to uppercase and lowercase ; Update result [ i ] to 1 if lowercase cost is less ; Traverse the string S ; Store the index of the character ; Convert the current character to uppercase or lowercase according to the condition ; Update s [ i ] ; Update s [ i ] ; Print the modified string ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void minimumCost ( string s , int L , int U ) { int N = s . size ( ) ; string ans = " " ; int lowerFreq [ 26 ] = { 0 } ; int upperFreq [ 26 ] = { 0 } ; for ( int i = 0 ; i < N ; i ++ ) { if ( isupper ( s [ i ] ) ) upperFreq [ s [ i ] - ' A ' ] ++ ; else lowerFreq [ s [ i ] - ' a ' ] ++ ; } int result [ 26 ] = { 0 } ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( lowerFreq [ i ] != 0 upperFreq [ i ] != 0 ) { int costToUpper = U * lowerFreq [ i ] ; int costToLower = L * upperFreq [ i ] ; if ( costToLower < costToUpper ) { result [ i ] = 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { int index = 0 ; if ( islower ( s [ i ] ) ) index = s [ i ] - ' a ' ; else index = s [ i ] - ' A ' ; if ( result [ index ] == 1 ) { s [ i ] = tolower ( s [ i ] ) ; } else { s [ i ] = toupper ( s [ i ] ) ; } } cout << s ; } int main ( ) { string S = " aabbAA " ; int L = 1 , U = 1 ; minimumCost ( S , L , U ) ; return 0 ; }
Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | C ++ Program for the above approach ; Function to check whether Matrix A [ ] [ ] can be transformed to Matrix B [ ] [ ] or not ; Traverse the matrix to perform horizontal operations ; Calculate difference ; Update next X elements ; Traverse the matrix to perform vertical operations ; Calculate difference ; Update next K elements ; A [ i ] [ j ] is not equal to B [ i ] [ j ] ; Conversion is not possible ; Conversion is possible ; Driver Code ; Input
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool Check ( int A [ ] [ 2 ] , int B [ ] [ 2 ] , int M , int N , int X ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j <= N - X ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { int diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i ] [ j + k ] = A [ i ] [ j + k ] + diff ; } } } } for ( int i = 0 ; i <= M - X ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { int diff = B [ i ] [ j ] - A [ i ] [ j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i + k ] [ j ] = A [ i + k ] [ j ] + diff ; } } } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { return 0 ; } } } return 1 ; } int main ( ) { int M = 2 , N = 2 , X = 2 ; int A [ 2 ] [ 2 ] = { { 0 , 0 } , { 0 , 0 } } ; int B [ 2 ] [ 2 ] = { { 1 , 2 } , { 0 , 1 } } ; if ( Check ( A , B , M , N , X ) ) { cout << " Yes " << endl ; } else { cout << " No " << endl ; } return 0 ; }
Kth smallest element in a row | kth largest element in a 2d array sorted row - wise and column - wise ; A structure to store entry of heap . The entry contains value from 2D array , row and column numbers of the value ; value to be stored ; Row number of value in 2D array ; Column number of value in 2D array ; A utility function to swap two HeapNode items . ; A utility function to minheapify the node harr [ i ] of a heap stored in harr [ ] ; This function returns kth smallest element in a 2D array mat [ ] [ ] ; k must be greater than 0 and smaller than n * n ; Create a min heap of elements from first row of 2D array ; Get current heap root ; Get next value from column of root 's value. If  the value stored at root was last value in its  column, then assign INFINITE as next value ; Update heap root with next value ; Heapify root ; Return the value at last extracted root ; driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct HeapNode { int val ; int r ; int c ; } ; void swap ( HeapNode * x , HeapNode * y ) { HeapNode z = * x ; * x = * y ; * y = z ; } void minHeapify ( HeapNode harr [ ] , int i , int heap_size ) { int l = i * 2 + 1 ; int r = i * 2 + 2 ; if ( l < heap_size && r < heap_size && harr [ l ] . val < harr [ i ] . val && harr [ r ] . val < harr [ i ] . val ) { HeapNode temp = harr [ r ] ; harr [ r ] = harr [ i ] ; harr [ i ] = harr [ l ] ; harr [ l ] = temp ; minHeapify ( harr , l , heap_size ) ; minHeapify ( harr , r , heap_size ) ; } if ( l < heap_size && harr [ l ] . val < harr [ i ] . val ) { HeapNode temp = harr [ i ] ; harr [ i ] = harr [ l ] ; harr [ l ] = temp ; minHeapify ( harr , l , heap_size ) ; } } int kthSmallest ( int mat [ 4 ] [ 4 ] , int n , int k ) { if ( k < 0 && k >= n * n ) return INT_MAX ; HeapNode harr [ n ] ; for ( int i = 0 ; i < n ; i ++ ) harr [ i ] = { mat [ 0 ] [ i ] , 0 , i } ; HeapNode hr ; for ( int i = 0 ; i < k ; i ++ ) { hr = harr [ 0 ] ; int nextval = ( hr . r < ( n - 1 ) ) ? mat [ hr . r + 1 ] [ hr . c ] : INT_MAX ; harr [ 0 ] = { nextval , ( hr . r ) + 1 , hr . c } ; minHeapify ( harr , 0 , n ) ; } return hr . val ; } int main ( ) { int mat [ 4 ] [ 4 ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 25 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } , } ; cout << "7th ▁ smallest ▁ element ▁ is ▁ " << kthSmallest ( mat , 4 , 7 ) ; return 0 ; }
Make all array elements equal by reducing array elements to half minimum number of times | C ++ program for the above approach ; Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Driver Code ; Given array ; Size of the array
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minOperations ( int arr [ ] , int N ) { map < int , int > mp ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res ) { mp [ res ] ++ ; res /= 2 ; } } int mx = 1 ; for ( auto it : mp ) { if ( it . second == N ) { mx = it . first ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res != mx ) { ans ++ ; res /= 2 ; } } cout << ans ; } int main ( ) { int arr [ ] = { 3 , 1 , 1 , 3 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minOperations ( arr , N ) ; }
Find the player to reach at least N by multiplying with any value from given range | C ++ program for the above approach ; Function to find the winner ; Backtrack from N to 1 ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; char Winner ( int N ) { bool player = true ; while ( N > 1 ) { int den = ( player ) ? 9 : 2 ; int X = N / den , Y = N % den ; N = ( Y ) ? X + 1 : X ; player = ! player ; } if ( player ) return ' B ' ; else return ' A ' ; } int main ( ) { int N = 10 ; cout << Winner ( N ) ; return 0 ; }
Program to find largest element in an array | C ++ program to find maximum in arr [ ] of size n ; Method to find maximum in arr [ ] ; Initialize maximum element ; Traverse array elements from second and compare every element with current max ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largest ( int arr [ ] , int n ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } int main ( ) { int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " Largest ▁ in ▁ given ▁ array ▁ is ▁ " << largest ( arr , n ) ; return 0 ; }
Minimize steps required to reach the value N | C ++ program for the above approach ; Function to find the minimum steps required to reach N by either moving i steps forward or 1 steps backward ; Stores the required count ; IF total moves required is less than double of N ; Update steps ; Steps required to reach N ; Update steps ; Driver Code ; Given value of N
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumsteps ( int N ) { int steps = 0 ; while ( steps * ( steps + 1 ) < 2 * N ) { steps ++ ; } if ( steps * ( steps + 1 ) / 2 == N + 1 ) { steps ++ ; } cout << steps ; } int main ( ) { int N = 18 ; minimumsteps ( N ) ; }
Program to find largest element in an array | C ++ program to find maximum in arr [ ] of size n ; returns maximum in arr [ ] of size n ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int largest ( int arr [ ] , int n ) { return * max_element ( arr , arr + n ) ; } int main ( ) { int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << largest ( arr , n ) ; return 0 ; }
Find the largest three distinct elements in an array | C ++ code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void find3largest ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int check = 0 , count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( count < 4 ) { if ( check != arr [ n - i ] ) { cout << arr [ n - i ] << " ▁ " ; check = arr [ n - i ] ; count ++ ; } } else break ; } } int main ( ) { int arr [ ] = { 12 , 45 , 1 , -1 , 45 , 54 , 23 , 5 , 0 , -10 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; find3largest ( arr , n ) ; }
Minimum characters required to be removed to make frequency of each character unique | C ++ program to implement the above approach ; Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the string ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minCntCharDeletionsfrequency ( string & str , int N ) { unordered_map < char , int > mp ; priority_queue < int > pq ; int cntChar = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mp [ str [ i ] ] ++ ; } for ( auto it : mp ) { pq . push ( it . second ) ; } while ( ! pq . empty ( ) ) { int frequent = pq . top ( ) ; pq . pop ( ) ; if ( pq . empty ( ) ) { return cntChar ; } if ( frequent == pq . top ( ) ) { if ( frequent > 1 ) { pq . push ( frequent - 1 ) ; } cntChar ++ ; } } return cntChar ; } int main ( ) { string str = " abbbcccd " ; int N = str . length ( ) ; cout << minCntCharDeletionsfrequency ( str , N ) ; return 0 ; }
Replace specified matrix elements such that no two adjacent elements are equal | C ++ program for the above approach ; Function to check if current position is safe or not ; Directions for adjacent cells ; Check if any adjacent cell is same ; Current index is valid ; Recursive function for backtracking ; Free cell ; All positions covered ; If position is valid for 1 ; If position is valid for 2 ; Recursive call for next unoccupied position ; If above conditions fails ; Function to print valid matrix ; Driver Code ; Give dimensions ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool issafe ( vector < vector < char > > & v , int i , int j , int n , int m , char ch ) { int rowN [ ] = { 1 , -1 , 0 , 0 } ; int colN [ ] = { 0 , 0 , 1 , -1 } ; for ( int k = 0 ; k < 4 ; k ++ ) { if ( i + rowN [ k ] >= 0 && i + rowN [ k ] < n && j + colN [ k ] >= 0 && j + colN [ k ] < m && v [ i + rowN [ k ] ] [ j + colN [ k ] ] == ch ) { return false ; } } return true ; } bool place ( vector < vector < char > > & v , int n , int m ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( v [ i ] [ j ] == ' F ' ) { break ; } } if ( j != m ) { break ; } } if ( i == n && j == m ) { return true ; } if ( issafe ( v , i , j , n , m , '1' ) ) { v [ i ] [ j ] = '1' ; if ( place ( v , n , m ) ) { return true ; } v [ i ] [ j ] = ' F ' ; } if ( issafe ( v , i , j , n , m , '2' ) ) { v [ i ] [ j ] = '2' ; if ( place ( v , n , m ) ) { return true ; } v [ i ] [ j ] = ' F ' ; } return false ; } void printMatrix ( vector < vector < char > > arr , int n , int m ) { place ( arr , n , m ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { cout << arr [ i ] [ j ] ; } cout << endl ; } } int main ( ) { vector < vector < char > > arr = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } , } ; int n = 4 , m = 4 ; printMatrix ( arr , n , m ) ; return 0 ; }
Program for Mean and median of an unsorted array | CPP program to find mean and median of an array ; Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } double findMedian ( int a [ ] , int n ) { sort ( a , a + n ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } int main ( ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << " Mean ▁ = ▁ " << findMean ( a , n ) << endl ; cout << " Median ▁ = ▁ " << findMedian ( a , n ) << endl ; return 0 ; }
Find sum of all left leaves in a given Binary Tree | C ++ program to find sum of all left leaves ; A binary tree node ; A constructor to create a new Node ; Return the sum of left leaf nodes ; Using a stack_ for Depth - First Traversal of the tree ; sum holds the sum of all the left leaves ; Check if currentNode 's left  child is a leaf node ; if currentNode is a leaf , add its data to the sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; class Node { public : int key ; Node * left , * right ; Node ( int key_ ) { key = key_ ; left = NULL ; right = NULL ; } } ; int sumOfLeftLeaves ( Node * root ) { if ( root == NULL ) return 0 ; stack < Node * > stack_ ; stack_ . push ( root ) ; int sum = 0 ; while ( stack_ . size ( ) > 0 ) { Node * currentNode = stack_ . top ( ) ; stack_ . pop ( ) ; if ( currentNode -> left != NULL ) { stack_ . push ( currentNode -> left ) ; if ( currentNode -> left -> left == NULL && currentNode -> left -> right == NULL ) { sum = sum + currentNode -> left -> key ; } } if ( currentNode -> right != NULL ) stack_ . push ( currentNode -> right ) ; } return sum ; } int main ( ) { Node * root = new Node ( 20 ) ; root -> left = new Node ( 9 ) ; root -> right = new Node ( 49 ) ; root -> right -> left = new Node ( 23 ) ; root -> right -> right = new Node ( 52 ) ; root -> right -> right -> left = new Node ( 50 ) ; root -> left -> left = new Node ( 5 ) ; root -> left -> right = new Node ( 12 ) ; root -> left -> right -> right = new Node ( 12 ) ; cout << " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " << sumOfLeftLeaves ( root ) << endl ; return 0 ; }
Find a pair with sum N having minimum absolute difference | C ++ program to implement the above approach ; Function to find the value of X and Y having minimum value of abs ( X - Y ) ; If N is an odd number ; If N is an even number ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findXandYwithminABSX_Y ( int N ) { if ( N % 2 == 1 ) { cout << ( N / 2 ) << " ▁ " << ( N / 2 + 1 ) ; } else { cout << ( N / 2 - 1 ) << " ▁ " << ( N / 2 + 1 ) ; } } int main ( ) { int N = 12 ; findXandYwithminABSX_Y ( N ) ; }
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | C ++ program for the above approach ; Function to find the maximum sum subsequence ; Base Case ; If current state is already calculated then use it ; If current element is positive ; Update ans and recursively call with update value of flag ; Else current element is negative ; Update ans and recursively call with update value of flag ; Return maximum sum subsequence ; Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs ; Create auxiliary array dp [ ] [ ] ; Initialize dp [ ] [ ] ; Function Call ; Driver Code ; Given array arr [ ] ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMax ( vector < int > & a , int dp [ ] [ 2 ] , int i , int flag ) { if ( i == ( int ) a . size ( ) ) { return 0 ; } if ( dp [ i ] [ flag ] != -1 ) { return dp [ i ] [ flag ] ; } int ans ; if ( flag == 0 ) { ans = max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) ) ; } else { ans = max ( findMax ( a , dp , i + 1 , 1 ) , -1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) ) ; } return dp [ i ] [ flag ] = ans ; } void findMaxSumUtil ( vector < int > & arr , int N ) { int dp [ N ] [ 2 ] ; memset ( dp , -1 , sizeof dp ) ; cout << findMax ( arr , dp , 0 , 0 ) ; } int main ( ) { vector < int > arr = { 1 , 2 , 3 , 4 , 1 , 2 } ; int N = arr . size ( ) ; findMaxSumUtil ( arr , N ) ; return 0 ; }
Find the player to last modify a string such that even number of consonants and no vowels are left in the string | C ++ program for the above approach ; Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the string ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Driver Code ; Given string s ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findWinner ( string s ) { int vowels_count = 0 , consonants_count = 0 ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { vowels_count ++ ; } else { consonants_count ++ ; } } if ( vowels_count == 0 ) { if ( consonants_count % 2 == 0 ) { cout << " Player ▁ B " ; } else { cout << " Player ▁ A " ; } } else if ( vowels_count == 1 && consonants_count % 2 != 0 ) { cout << " Player ▁ A " ; } else { cout << " D " ; } } int main ( ) { string s = " abcd " ; findWinner ( s ) ; return 0 ; }
Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | C ++ program for the above approach ; Function to find lexicographically smallest permutation of [ 1 , A ] having B integers greater than all the previous elements ; Initial array ; Resultant array ; Append the first ( B - 1 ) elements to the resultant array ; Append the maximum from the initial array ; Copy the remaining elements ; Return the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > getSmallestArray ( int A , int B ) { vector < int > arr ( A ) ; for ( int i = 0 ; i < A ; i ++ ) arr [ i ] = i + 1 ; vector < int > ans ( A ) ; for ( int i = 0 ; i < B - 1 ; i ++ ) ans [ i ] = arr [ i ] ; ans [ B - 1 ] = arr [ A - 1 ] ; for ( int i = B ; i < A ; i ++ ) ans [ i ] = arr [ i - 1 ] ; return ans ; } int main ( ) { int A = 3 ; int B = 2 ; vector < int > ans = getSmallestArray ( A , B ) ; for ( auto i : ans ) cout << i << " ▁ " ; }
k smallest elements in same order using O ( 1 ) extra space | CPP for printing smallest k numbers in order ; Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; find largest from first k - elements ; if largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver program
#include <algorithm> NEW_LINE #include <iostream> NEW_LINE using namespace std ; void printSmall ( int arr [ ] , int n , int k ) { for ( int i = k ; i < n ; ++ i ) { int max_var = arr [ k - 1 ] ; int pos = k - 1 ; for ( int j = k - 2 ; j >= 0 ; j -- ) { if ( arr [ j ] > max_var ) { max_var = arr [ j ] ; pos = j ; } } if ( max_var > arr [ i ] ) { int j = pos ; while ( j < k - 1 ) { arr [ j ] = arr [ j + 1 ] ; j ++ ; } arr [ k - 1 ] = arr [ i ] ; } } for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int k = 5 ; printSmall ( arr , n , k ) ; return 0 ; }
Find k pairs with smallest sums in two arrays | C ++ program to prints first k pairs with least sum from two arrays . ; Function to find k pairs with least sum such that one elemennt of a pair is from arr1 [ ] and other element is from arr2 [ ] ; Stores current index in arr2 [ ] for every element of arr1 [ ] . Initially all values are considered 0. Here current index is the index before which all elements are considered as part of output . ; Initialize current pair sum as infinite ; To pick next pair , traverse for all elements of arr1 [ ] , for every element , find corresponding current element in arr2 [ ] and pick minimum of all formed pairs . ; Check if current element of arr1 [ ] plus element of array2 to be used gives minimum sum ; Update index that gives minimum ; update minimum sum ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { cout << " k ▁ pairs ▁ don ' t ▁ exist " ; return ; } int index2 [ n1 ] ; memset ( index2 , 0 , sizeof ( index2 ) ) ; while ( k > 0 ) { int min_sum = INT_MAX ; int min_index = 0 ; for ( int i1 = 0 ; i1 < n1 ; i1 ++ ) { if ( index2 [ i1 ] < n2 && arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) { min_index = i1 ; min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] ; } } cout << " ( " << arr1 [ min_index ] << " , ▁ " << arr2 [ index2 [ min_index ] ] << " ) ▁ " ; index2 [ min_index ] ++ ; k -- ; } } int main ( ) { int arr1 [ ] = { 1 , 3 , 11 } ; int n1 = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int arr2 [ ] = { 2 , 4 , 8 } ; int n2 = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; int k = 4 ; kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) ; return 0 ; }
Maximize count of strings of length 3 that can be formed from N 1 s and M 0 s | C ++ program for the above approach ; Function that counts the number of strings of length 3 that can be made with given m 0 s and n 1 s ; Print the count of strings ; Driver Code ; Given count of 1 s and 0 s ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void number_of_strings ( int N , int M ) { cout << min ( N , min ( M , ( N + M ) / 3 ) ) ; } int main ( ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; return 0 ; }