codeparrot/xlcost-text-to-code huggingface dataset:
Edit Distance | DP | A Space efficient Dynamic Programming based C ++ program to find minimum number operations to convert str1 to str2 ; Create a DP array to memoize result of previous computations ; Base condition when second string is empty then we remove all characters ; Start filling the DP This loop run for every character in second string ; This loop compares the char from second string with first string characters ; if first string is empty then we have to perform add character operation to get second string ; if character from both string is same then we do not perform any operation . here i % 2 is for bound the row number . ; if character from both string is not same then we take the minimum from three specified operation ; after complete fill the DP array if the len2 is even then we end up in the 0 th row else we end up in the 1 th row so we take len2 % 2 to get row ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void EditDistDP ( string str1 , string str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; int DP [ 2 ] [ len1 + 1 ] ; memset ( DP , 0 , sizeof DP ) ; for ( int i = 0 ; i <= len1 ; i ++ ) DP [ 0 ] [ i ] = i ; for ( int i = 1 ; i <= len2 ; i ++ ) { for ( int j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 ] [ j ] = i ; else if ( str1 [ j - 1 ] == str2 [ i - 1 ] ) { DP [ i % 2 ] [ j ] = DP [ ( i - 1 ) % 2 ] [ j - 1 ] ; } else { DP [ i % 2 ] [ j ] = 1 + min ( DP [ ( i - 1 ) % 2 ] [ j ] , min ( DP [ i % 2 ] [ j - 1 ] , DP [ ( i - 1 ) % 2 ] [ j - 1 ] ) ) ; } } } cout << DP [ len2 % 2 ] [ len1 ] << endl ; } int main ( ) { string str1 = " food " ; string str2 = " money " ; EditDistDP ( str1 , str2 ) ; return 0 ; }
Sum of Bitwise XOR of elements of an array with all elements of another array | C ++ Program for the above approach ; Function to calculate sum of Bitwise XOR of elements of arr [ ] with k ; Initialize sum to be zero ; Iterate over each set bit ; Stores contribution of i - th bet to the sum ; If the i - th bit is set ; Stores count of elements whose i - th bit is not set ; Update value ; Update value ; Add value to sum ; Move to the next power of two ; Stores the count of elements whose i - th bit is set ; Initialize count to 0 for all positions ; Traverse the array ; Iterate over each bit ; If the i - th bit is set ; Increase count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int xorSumOfArray ( int arr [ ] , int n , int k , int count [ ] ) { int sum = 0 ; int p = 1 ; for ( int i = 0 ; i < 31 ; i ++ ) { int val = 0 ; if ( ( k & ( 1 << i ) ) != 0 ) { int not_set = n - count [ i ] ; val = ( ( not_set ) * p ) ; } else { val = ( count [ i ] * p ) ; } sum += val ; p = ( p * 2 ) ; } return sum ; } void sumOfXors ( int arr [ ] , int n , int queries [ ] , int q ) { int count [ 32 ] ; memset ( count , 0 , sizeof ( count ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 31 ; j ++ ) { if ( arr [ i ] & ( 1 << j ) ) count [ j ] ++ ; } } for ( int i = 0 ; i < q ; i ++ ) { int k = queries [ i ] ; cout << xorSumOfArray ( arr , n , k , count ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 5 , 2 , 3 } ; int queries [ ] = { 3 , 8 , 7 } ; int n = sizeof ( arr ) / sizeof ( int ) ; int q = sizeof ( queries ) / sizeof ( int ) ; sumOfXors ( arr , n , queries , q ) ; return 0 ; }
Factor Tree of a given Number | C ++ program to construct Factor Tree for a given number ; Tree node ; Utility function to create a new tree Node ; Constructs factor tree for given value and stores root of tree at given reference . ; the number is factorized ; If we found a factor , we construct left and right subtrees and return . Since we traverse factors starting from smaller to greater , left child will always have smaller factor ; Iterative method to find the height of Binary Tree ; Base Case ; Print front of queue and remove it from queue ; driver program ;
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { struct Node * left , * right ; int key ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } void createFactorTree ( struct Node * * node_ref , int v ) { ( * node_ref ) = newNode ( v ) ; for ( int i = 2 ; i < v / 2 ; i ++ ) { if ( v % i != 0 ) continue ; createFactorTree ( & ( ( * node_ref ) -> left ) , i ) ; createFactorTree ( & ( ( * node_ref ) -> right ) , v / i ) ; return ; } } void printLevelOrder ( Node * root ) { if ( root == NULL ) return ; queue < Node * > q ; q . push ( root ) ; while ( q . empty ( ) == false ) { Node * node = q . front ( ) ; cout << node -> key << " ▁ " ; q . pop ( ) ; if ( node -> left != NULL ) q . push ( node -> left ) ; if ( node -> right != NULL ) q . push ( node -> right ) ; } } int main ( ) { int val = 48 ; struct Node * root = NULL ; createFactorTree ( & root , val ) ; cout << " Level ▁ order ▁ traversal ▁ of ▁ " " constructed ▁ factor ▁ tree " ; printLevelOrder ( root ) ; return 0 ; }
Minimum time required to schedule K processes | C ++ program for the above approach ; Function to execute k processes that can be gained in minimum amount of time ; Stores all the array elements ; Push all the elements to the priority queue ; Stores the required result ; Loop while the queue is not empty and K is positive ; Store the top element from the pq ; Add it to the answer ; Divide it by 2 and push it back to the pq ; Print the answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void executeProcesses ( int A [ ] , int N , int K ) { priority_queue < int > pq ; for ( int i = 0 ; i < N ; i ++ ) { pq . push ( A [ i ] ) ; } int ans = 0 ; while ( ! pq . empty ( ) && K > 0 ) { int top = pq . top ( ) ; pq . pop ( ) ; ans ++ ; K = K - top ; top = top / 2 ; pq . push ( top ) ; } cout << ans ; } int main ( ) { int A [ ] = { 3 , 1 , 7 , 4 , 2 } ; int K = 15 ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; executeProcesses ( A , N , K ) ; return 0 ; }
Maximum height of an elevation possible such that adjacent matrix cells have a difference of at most height 1 | C ++ program for the above approach ; Utility function to find the matrix having the maximum height ; Stores index pairs for bfs ; Stores info about the visited cells ; Traverse the matrix ; Breadth First Search ; x & y are the row & column of current cell ; Check all the 4 adjacent cells and marking them as visited if not visited yet also marking their height as 1 + height of cell ( x , y ) ; Function to find the matrix having the maximum height ; Stores output matrix ; Calling the helper function ; Print the final output matrix ; Driver Code ; Given matrix ; Function call to find the matrix having the maximum height
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define M  3 NEW_LINE #define N  3 NEW_LINE void findHeightMatrixUtil ( int mat [ ] [ N ] , int height [ M ] [ N ] ) { queue < pair < int , int > > q ; int vis [ M ] [ N ] = { 0 } ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { q . push ( { i , j } ) ; height [ i ] [ j ] = 0 ; vis [ i ] [ j ] = 1 ; } } } while ( q . empty ( ) == 0 ) { pair < int , int > k = q . front ( ) ; q . pop ( ) ; int x = k . first , y = k . second ; if ( x > 0 && vis [ x - 1 ] [ y ] == 0 ) { height [ x - 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x - 1 ] [ y ] = 1 ; q . push ( { x - 1 , y } ) ; } if ( y > 0 && vis [ x ] [ y - 1 ] == 0 ) { height [ x ] [ y - 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y - 1 ] = 1 ; q . push ( { x , y - 1 } ) ; } if ( x < M - 1 && vis [ x + 1 ] [ y ] == 0 ) { height [ x + 1 ] [ y ] = height [ x ] [ y ] + 1 ; vis [ x + 1 ] [ y ] = 1 ; q . push ( { x + 1 , y } ) ; } if ( y < N - 1 && vis [ x ] [ y + 1 ] == 0 ) { height [ x ] [ y + 1 ] = height [ x ] [ y ] + 1 ; vis [ x ] [ y + 1 ] = 1 ; q . push ( { x , y + 1 } ) ; } } } void findHeightMatrix ( int mat [ ] [ N ] ) { int height [ M ] [ N ] ; findHeightMatrixUtil ( mat , height ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) cout << height [ i ] [ j ] << " ▁ " ; cout << endl ; } } int main ( ) { int mat [ ] [ N ] = { { 0 , 0 } , { 0 , 1 } } ; findHeightMatrix ( mat ) ; return 0 ; }
Median of all nodes from a given range in a Binary Search Tree ( BST ) | C ++ program for the above approach ; Tree Node structure ; Function to create a new BST node ; Function to insert a new node with given key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Return the node pointer ; Function to find all the nodes that lies over the range [ node1 , node2 ] ; If the tree is empty , return ; Traverse for the left subtree ; If a second node is found , then update the flag as false ; Traverse the right subtree ; Function to find the median of all the values in the given BST that lies over the range [ node1 , node2 ] ; Stores all the nodes in the range [ node1 , node2 ] ; Store the size of the array ; Print the median of array based on the size of array ; Driver Code ; Given BST
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { struct Node * left , * right ; int key ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return temp ; } Node * insertNode ( Node * node , int key ) { if ( node == NULL ) return newNode ( key ) ; if ( key < node -> key ) node -> left = insertNode ( node -> left , key ) ; else if ( key > node -> key ) node -> right = insertNode ( node -> right , key ) ; return node ; } void getIntermediateNodes ( Node * root , vector < int > & interNodes , int node1 , int node2 ) { if ( root == NULL ) return ; getIntermediateNodes ( root -> left , interNodes , node1 , node2 ) ; if ( root -> key <= node2 and root -> key >= node1 ) { interNodes . push_back ( root -> key ) ; } getIntermediateNodes ( root -> right , interNodes , node1 , node2 ) ; } float findMedian ( Node * root , int node1 , int node2 ) { vector < int > interNodes ; getIntermediateNodes ( root , interNodes , node1 , node2 ) ; int nSize = interNodes . size ( ) ; return ( nSize % 2 == 1 ) ? ( float ) interNodes [ nSize / 2 ] : ( float ) ( interNodes [ ( nSize - 1 ) / 2 ] + interNodes [ nSize / 2 ] ) / 2 ; } int main ( ) { struct Node * root = NULL ; root = insertNode ( root , 8 ) ; insertNode ( root , 3 ) ; insertNode ( root , 1 ) ; insertNode ( root , 6 ) ; insertNode ( root , 4 ) ; insertNode ( root , 11 ) ; insertNode ( root , 15 ) ; cout << findMedian ( root , 3 , 11 ) ; return 0 ; }
Count number of pairs ( i , j ) up to N that can be made equal on multiplying with a pair from the range [ 1 , N / 2 ] | C ++ program for the above approach ; Function to compute totient of all numbers smaller than or equal to N ; Iterate over the range [ 2 , N ] ; If phi [ p ] is not computed already , then p is prime ; Phi of a prime number p is ( p - 1 ) ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Function to count the pairs ( i , j ) from the range [ 1 , N ] , satisfying the given condition ; Stores the counts of first and second type of pairs respectively ; Count of first type of pairs ; Stores the phi or totient values ; Calculate the Phi values ; Iterate over the range [ N / 2 + 1 , N ] ; Update the value of cnt_type2 ; Print the total count ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void computeTotient ( int N , int phi [ ] ) { for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } void countPairs ( int N ) { int cnt_type1 = 0 , cnt_type2 = 0 ; int half_N = N / 2 ; cnt_type1 = ( half_N * ( half_N - 1 ) ) / 2 ; int phi [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { phi [ i ] = i ; } computeTotient ( N , phi ) ; for ( int i = ( N / 2 ) + 1 ; i <= N ; i ++ ) cnt_type2 += ( i - phi [ i ] - 1 ) ; cout << cnt_type1 + cnt_type2 ; } int main ( ) { int N = 6 ; countPairs ( N ) ; return 0 ; }
Sum of subsets nearest to K possible from two given arrays | C ++ program of the above approach ; Stores the sum closest to K ; Stores the minimum absolute difference ; Function to choose the elements from the array B [ ] ; If absolute difference is less then minimum value ; Update the minimum value ; Update the value of ans ; If absolute difference between curr and K is equal to minimum ; Update the value of ans ; If i is greater than M - 1 ; Includes the element B [ i ] once ; Includes the element B [ i ] twice ; Excludes the element B [ i ] ; Function to find a subset sum whose sum is closest to K ; Traverse the array A [ ] ; Function Call ; Return the ans ; Driver Code ; Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = INT_MAX ; int mini = INT_MAX ; void findClosestTarget ( int i , int curr , int B [ ] , int M , int K ) { if ( abs ( curr - K ) < mini ) { mini = abs ( curr - K ) ; ans = curr ; } if ( abs ( curr - K ) == mini ) { ans = min ( ans , curr ) ; } if ( i >= M ) return ; findClosestTarget ( i + 1 , curr + B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr + 2 * B [ i ] , B , M , K ) ; findClosestTarget ( i + 1 , curr , B , M , K ) ; } int findClosest ( int A [ ] , int B [ ] , int N , int M , int K ) { for ( int i = 0 ; i < N ; i ++ ) { findClosestTarget ( 0 , A [ i ] , B , M , K ) ; } return ans ; } int main ( ) { int A [ ] = { 2 , 3 } ; int B [ ] = { 4 , 5 , 30 } ; int N = sizeof ( A ) / sizeof ( A [ 0 ] ) ; int M = sizeof ( B ) / sizeof ( B [ 0 ] ) ; int K = 18 ; cout << findClosest ( A , B , N , M , K ) ; return 0 ; }
Check if every node can me made accessible from a node of a Tree by at most N / 2 given operations | C ++ program for the above approach ; Function to check if there is a node in tree from where all other nodes are accessible or not ; Store the indegree of every node ; Store the nodes having indegree equal to 0 ; Traverse the array ; If the indegree of i - th node is 0 ; Increment count0 by 1 ; If the number of operations needed is at most floor ( n / 2 ) ; Otherwise ; Driver Code ; Given number of nodes ; Given Directed Tree
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findNode ( map < int , int > mp , int n ) { int a [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = mp [ i + 1 ] ; } int count0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count0 ++ ; } } count0 -= 1 ; if ( count0 <= floor ( ( ( double ) n ) / ( ( double ) 2 ) ) ) { cout << " Yes " ; } else cout << " No " ; } int main ( ) { int N = 3 ; map < int , int > mp ; mp [ 1 ] = 0 ; mp [ 2 ] = 2 ; mp [ 3 ] = 0 ; findNode ( mp , N ) ; }
Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | C ++ program for above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int convert ( vector < int > p ) { int ans = 0 ; for ( int i : p ) { ans = ( ans << 1 ) | i ; } return ans ; } int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } void checkGoodMatrix ( vector < vector < int > > mat ) { vector < int > P ; vector < int > S ; vector < int > MR ; vector < int > MC ; for ( int i = 0 ; i < mat . size ( ) ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . size ( ) ; j ++ ) { if ( i == j ) P . push_back ( mat [ i ] [ j ] ) ; if ( i + j == mat . size ( ) - 1 ) S . push_back ( mat [ i ] [ j ] ) ; if ( i == floor ( ( mat . size ( ) - 1 ) / 2 ) ) MR . push_back ( mat [ i ] [ j ] ) ; if ( j == floor ( ( mat . size ( ) - 1 ) / 2 ) ) MC . push_back ( mat [ i ] [ j ] ) ; } } reverse ( S . begin ( ) , S . end ( ) ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) cout << " Yes " ; else cout << " No " ; } int main ( ) { vector < vector < int > > mat = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; }
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path | C ++ program for the above approach ; Function to assign values to nodes of the tree s . t . sum of values of nodes of path between any 2 nodes is not divisible by length of path ; Stores the adjacency list ; Create a adjacency list ; Stores whether node is visited or not ; Stores the node values ; Variable used to assign values to the nodes alternatively to the parent child ; Declare a queue ; Push the 1 st node ; Assign K value to this node ; Dequeue the node ; Mark it as visited ; Upgrade the value of K ; Assign K to the child nodes ; If the child is unvisited ; Enqueue the child ; Assign K to the child ; Print the value assigned to the nodes ; Driver Code ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void assignValues ( int Edges [ ] [ 2 ] , int n ) { vector < int > tree [ n + 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; tree [ u ] . push_back ( v ) ; tree [ v ] . push_back ( u ) ; } vector < bool > visited ( n + 1 , false ) ; vector < int > answer ( n + 1 ) ; int K = 1 ; queue < int > q ; q . push ( 1 ) ; answer [ 1 ] = K ; while ( ! q . empty ( ) ) { int node = q . front ( ) ; q . pop ( ) ; visited [ node ] = true ; K = ( ( answer [ node ] == 1 ) ? 2 : 1 ) ; for ( auto child : tree [ node ] ) { if ( ! visited [ child ] ) { q . push ( child ) ; answer [ child ] = K ; } } } for ( int i = 1 ; i <= n ; i ++ ) { cout << answer [ i ] << " ▁ " ; } } int main ( ) { int N = 11 ; int Edges [ ] [ 2 ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 1 , 5 } , { 2 , 6 } , { 2 , 10 } , { 10 , 11 } , { 3 , 7 } , { 4 , 8 } , { 5 , 9 } } ; assignValues ( Edges , N ) ; return 0 ; }
Number of full binary trees such that each node is product of its children | C ++ program to find number of full binary tree such that each node is product of its children . ; Return the number of all possible full binary tree with given product property . ; Finding the minimum and maximum values in given array . ; Marking the presence of each array element and initialising the number of possible full binary tree for each integer equal to 1 because single node will also contribute as a full binary tree . ; From minimum value to maximum value of array finding the number of all possible Full Binary Trees . ; Find if value present in the array ; For each multiple of i , less than equal to maximum value of array ; If multiple is not present in the array then continue . ; Finding the number of possible Full binary trees for multiple j by multiplying number of possible Full binary tree from the number i and number of possible Full binary tree from i / j . ; Condition for possiblity when left chid became right child and vice versa . ; Driven Program
#include <bits/stdc++.h> NEW_LINE using namespace std ; int numoffbt ( int arr [ ] , int n ) { int maxvalue = INT_MIN , minvalue = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { maxvalue = max ( maxvalue , arr [ i ] ) ; minvalue = min ( minvalue , arr [ i ] ) ; } int mark [ maxvalue + 2 ] ; int value [ maxvalue + 2 ] ; memset ( mark , 0 , sizeof ( mark ) ) ; memset ( value , 0 , sizeof ( value ) ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ arr [ i ] ] = 1 ; value [ arr [ i ] ] = 1 ; } int ans = 0 ; for ( int i = minvalue ; i <= maxvalue ; i ++ ) { if ( mark [ i ] ) { for ( int j = i + i ; j <= maxvalue && j / i <= i ; j += i ) { if ( ! mark [ j ] ) continue ; value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; if ( i != j / i ) value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; } } ans += value [ i ] ; } return ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 4 , 6 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << numoffbt ( arr , n ) << endl ; return 0 ; }
Minimum prime numbers required to be subtracted to make all array elements equal | C ++ program for the above approach ; Stores the sieve of prime numbers ; Function that performs the Sieve of Eratosthenes ; Initialize all numbers as prime ; Iterate over the range [ 2 , 1000 ] ; If the current element is a prime number ; Mark all its multiples as false ; Function to find the minimum number of subtraction of primes numbers required to make all array elements the same ; Perform sieve of eratosthenes ; Find the minimum value ; Stores the value to each array element should be reduced ; If an element exists with value ( M + 1 ) ; Stores the minimum count of subtraction of prime numbers ; Traverse the array ; If D is equal to 0 ; If D is a prime number ; Increase count by 1 ; If D is an even number ; Increase count by 2 ; If D - 2 is prime ; Increase count by 2 ; Otherwise , increase count by 3 ; Driver Code
#include <bits/stdc++.h> NEW_LINE #define limit  100000 NEW_LINE using namespace std ; bool prime [ limit + 1 ] ; void sieve ( ) { memset ( prime , true , sizeof ( prime ) ) ; for ( int p = 2 ; p * p <= limit ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= limit ; i += p ) prime [ i ] = false ; } } } int findOperations ( int arr [ ] , int n ) { sieve ( ) ; int minm = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { minm = min ( minm , arr [ i ] ) ; } int val = minm ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == minm + 1 ) { val = minm - 2 ; break ; } } int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int D = arr [ i ] - val ; if ( D == 0 ) { continue ; } else if ( prime [ D ] == true ) { cnt += 1 ; } else if ( D % 2 == 0 ) { cnt += 2 ; } else { if ( prime [ D - 2 ] == true ) { cnt += 2 ; } else { cnt += 3 ; } } } return cnt ; } int main ( ) { int arr [ ] = { 7 , 10 , 4 , 5 } ; int N = 4 ; cout << findOperations ( arr , N ) ; return 0 ; }
Print all unique digits present in concatenation of all array elements in the order of their occurrence | C ++ program for the above approach ; Function to print long unique elements ; Reverse the list ; Traverse the list ; Function which check for all unique digits ; Stores the final number ; Stores the count of unique digits ; Converting string to long longer to remove leading zeros ; Stores count of digits ; Iterate over the digits of N ; Retrieve the last digit of N ; Increase the count of the last digit ; Remove the last digit of N ; Converting string to long longer again ; Iterate over the digits of N ; Retrieve the last digit of N ; If the value of this digit is not visited ; If its frequency is 1 ( unique ) ; Mark the digit visited ; Remove the last digit of N ; Passing this list to print long the reversed list ; Function to concatenate array elements ; Stores the concatenated number ; Traverse the array ; Convert to equivalent string ; Concatenate the string ; Passing string to checkUnique function ; Driver Code ; Function call to prlong long unique digits present in the concatenation of array elements
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnique ( vector < long long > lis ) { reverse ( lis . begin ( ) , lis . end ( ) ) ; for ( long long i : lis ) cout << i << " ▁ " ; } void checkUnique ( string st ) { vector < long long > lis ; long long res = 0 ; long long N = stoll ( st ) ; vector < long long > cnt ( 10 , 0 ) , cnt1 ( 10 , 0 ) ; while ( N > 0 ) { long long rem = N % 10 ; cnt [ rem ] += 1 ; N = N / 10 ; } N = stoll ( st ) ; while ( N > 0 ) { long long rem = N % 10 ; if ( cnt1 [ rem ] == 0 ) { if ( cnt [ rem ] == 1 ) lis . push_back ( rem ) ; } cnt1 [ rem ] = 1 ; N = N / 10 ; } printUnique ( lis ) ; } void combineArray ( vector < long long > lis ) { string st = " " ; for ( long long el : lis ) { string ee = to_string ( el ) ; st = st + ee ; } checkUnique ( st ) ; } int main ( ) { vector < long long > arr = { 122 , 474 , 612 , 932 } ; combineArray ( arr ) ; return 0 ; }