codeparrot/xlcost-text-to-code huggingface dataset:
Count Inversions in an array | Set 1 ( Using Merge Sort ) | C ++ program to Count Inversions in an array ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int getInvCount ( int arr [ ] , int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; } int main ( ) { int arr [ ] = { 1 , 20 , 6 , 4 , 5 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << " ▁ Number ▁ of ▁ inversions ▁ are ▁ " << getInvCount ( arr , n ) ; return 0 ; }
Find if there is a path between two vertices in an undirected graph | C ++ program to check if there is exist a path between two vertices of an undirected graph . ; function to add an edge to graph ; A BFS based function to check whether d is reachable from s . ; Base case ; Mark all the vertices as not visited ; Create a queue for BFS ; Mark the current node as visited and enqueue it ; Dequeue a vertex from queue and print it ; Get all adjacent vertices of the dequeued vertex s If a adjacent has not been visited , then mark it visited and enqueue it ; If this adjacent node is the destination node , then return true ; Else , continue to do BFS ; If BFS is complete without visiting d ; Driver program to test methods of graph class ; Create a graph in the above diagram
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > adj ; void addEdge ( int v , int w ) { adj [ v ] . push_back ( w ) ; adj [ w ] . push_back ( v ) ; } bool isReachable ( int s , int d ) { if ( s == d ) return true ; int n = ( int ) adj . size ( ) ; vector < bool > visited ( n , false ) ; queue < int > q ; visited [ s ] = true ; q . push ( s ) ; while ( ! q . empty ( ) ) { s = q . front ( ) ; q . pop ( ) ; for ( auto x : adj [ s ] ) { if ( x == d ) return true ; if ( ! visited [ x ] ) { visited [ x ] = true ; q . push ( x ) ; } } } return false ; } int main ( ) { int n = 4 ; adj = vector < vector < int > > ( n ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; addEdge ( 3 , 3 ) ; int u = 1 , v = 3 ; if ( isReachable ( u , v ) ) cout << " There is a path from " ▁ < < ▁ u ▁ < < ▁ " to " ▁ < < ▁ v ; STRNEWLINE TABSYMBOL else STRNEWLINE TABSYMBOL TABSYMBOL cout ▁ < < ▁ " There is no path from " ▁ < < ▁ u ▁ < < ▁ " to " return 0 ; }
Range queries for alternatively addition and subtraction on given Array | C ++ program for the above approach ; Structure to represent a range query ; Function to find the result of alternatively adding and subtracting elements in the range [ L , R ] ; A boolean variable flag to alternatively add and subtract ; Iterate from [ L , R ] ; if flag is false , then add & toggle the flag ; if flag is true subtract and toggle the flag ; Return the final result ; Function to find the value for each query ; Iterate for each query ; Driver Code ; Given array ; Given Queries ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Query { int L , R ; } ; int findResultUtil ( int arr [ ] , int L , int R ) { int result = 0 ; bool flag = false ; for ( int i = L ; i <= R ; i ++ ) { if ( flag == false ) { result = result + arr [ i ] ; flag = true ; } else { result = result - arr [ i ] ; flag = false ; } } return result ; } void findResult ( int arr [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { cout << findResultUtil ( arr , q [ i ] . L , q [ i ] . R ) << " ▁ " ; } } int main ( ) { int arr [ ] = { 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; Query q [ ] = { { 2 , 5 } , { 6 , 8 } , { 1 , 7 } , { 4 , 8 } , { 0 , 5 } } ; int m = sizeof ( q ) / sizeof ( q [ 0 ] ) ; findResult ( arr , n , q , m ) ; return 0 ; }
Two elements whose sum is closest to zero | C ++ code to find Two elements whose sum is closest to zero ; Array should have at least two elements ; Initialization of values ; Driver Code
# include <bits/stdc++.h> NEW_LINE # include <stdlib.h> NEW_LINE # include <math.h> NEW_LINE using namespace std ; void minAbsSumPair ( int arr [ ] , int arr_size ) { int inv_count = 0 ; int l , r , min_sum , sum , min_l , min_r ; if ( arr_size < 2 ) { cout << " Invalid ▁ Input " ; return ; } min_l = 0 ; min_r = 1 ; min_sum = arr [ 0 ] + arr [ 1 ] ; for ( l = 0 ; l < arr_size - 1 ; l ++ ) { for ( r = l + 1 ; r < arr_size ; r ++ ) { sum = arr [ l ] + arr [ r ] ; if ( abs ( min_sum ) > abs ( sum ) ) { min_sum = sum ; min_l = l ; min_r = r ; } } } cout << " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " << arr [ min_l ] << " ▁ and ▁ " << arr [ min_r ] ; } int main ( ) { int arr [ ] = { 1 , 60 , -10 , 70 , -80 , 85 } ; minAbsSumPair ( arr , 6 ) ; return 0 ; }
Maximize sum of topmost elements of S stacks by popping at most N elements | C ++ Program to maximize the sum of top of the stack values of S stacks by popping at most N elements ; Function for computing the maximum sum at the top of the stacks after popping at most N elements from S stack ; Constructing a dp matrix of dimensions ( S + 1 ) x ( N + 1 ) ; Initialize all states ; Loop over all i stacks ; Store the maximum of popping j elements up to the current stack by popping k elements from current stack and j - k elements from all previous stacks combined ; Store the maximum sum of popping N elements across all stacks ; dp [ S ] [ N ] has the maximum sum ; Driver Program ; Number of stacks ; Length of each stack ; Maximum elements that can be popped
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumSum ( int S , int M , int N , vector < vector < int > > & stacks ) { int dp [ S + 1 ] [ N + 1 ] ; memset ( dp , INT_MIN , sizeof ( dp ) ) ; for ( int i = 0 ; i < S ; i ++ ) { for ( int j = 0 ; j <= N ; j ++ ) { for ( int k = 0 ; k <= min ( j , M ) ; k ++ ) { dp [ i + 1 ] [ j ] = max ( dp [ i + 1 ] [ j ] , stacks [ i ] [ k ] + dp [ i ] [ j - k ] ) ; } } } int result = INT_MIN ; for ( int i = 0 ; i <= N ; i ++ ) { result = max ( result , dp [ S ] [ i ] ) ; } return result ; } int main ( ) { int S = 2 ; int M = 4 ; vector < vector < int > > stacks = { { 2 , 6 , 4 , 5 } , { 1 , 6 , 15 , 10 } } ; int N = 3 ; cout << maximumSum ( S , M , N , stacks ) ; return 0 ; }
Abstraction of Binary Search | C ++ program for the above example ; Function to find X such that it is less than the target value and function is f ( x ) = x ^ 2 ; Initialise start and end ; Loop till start <= end ; Find the mid ; Check for the left half ; Store the result ; Reinitialize the start point ; Check for the right half ; Print the maximum value of x such that x ^ 2 is less than the targetValue ; Driver Code ; Given targetValue ; ; Function Call
#include " bits / stdc + + . h " NEW_LINE using namespace std ; void findX ( int targetValue ) { int start = 0 , end = targetValue ; int mid , result ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; if ( mid * mid <= targetValue ) { result = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } cout << result << endl ; } int main ( ) { int targetValue = 81 ; findX ( targetValue ) ; }
Two elements whose sum is closest to zero | C ++ implementation using STL ; Modified to sort by abolute values ; Absolute value shows how close it is to zero ; if found an even close value update min and store the index ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool compare ( int x , int y ) { return abs ( x ) < abs ( y ) ; } void findMinSum ( int arr [ ] , int n ) { sort ( arr , arr + n , compare ) ; int min = INT_MAX , x , y ; for ( int i = 1 ; i < n ; i ++ ) { if ( abs ( arr [ i - 1 ] + arr [ i ] ) <= min ) { min = abs ( arr [ i - 1 ] + arr [ i ] ) ; x = i - 1 ; y = i ; } } cout << " The ▁ two ▁ elements ▁ whose ▁ sum ▁ is ▁ minimum ▁ are ▁ " << arr [ x ] << " ▁ and ▁ " << arr [ y ] ; } int main ( ) { int arr [ ] = { 1 , 60 , -10 , 70 , -80 , 85 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMinSum ( arr , n ) ; return 0 ; }
Probability of getting all possible values on throwing N dices | C ++ Program to calculate the probability of all the possible values that can be obtained throwing N dices ; Store the probabilities ; Precompute the probabilities for values possible using 1 dice ; Compute the probabilies for all values from 2 to N ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void dicesSum ( int n ) { vector < map < int , double > > dp ( n + 1 ) ; dp [ 1 ] = { { 1 , 1 / 6.0 } , { 2 , 1 / 6.0 } , { 3 , 1 / 6.0 } , { 4 , 1 / 6.0 } , { 5 , 1 / 6.0 } , { 6 , 1 / 6.0 } } ; for ( int i = 2 ; i <= n ; i ++ ) { for ( auto a1 : dp [ i - 1 ] ) { for ( auto a2 : dp [ 1 ] ) { dp [ i ] [ a1 . first + a2 . first ] += a1 . second * a2 . second ; } } } for ( auto a : dp [ n ] ) { cout << a . first << " ▁ " << setprecision ( 2 ) << a . second << endl ; } } int main ( ) { int n = 2 ; dicesSum ( n ) ; return 0 ; }
Convert undirected connected graph to strongly connected directed graph | C ++ program for the above approach ; To store the assigned Edges ; Flag variable to check Bridges ; Function to implement DFS Traversal ; Mark the current node as visited ; Update the order of node v ; Update the bridge_detect for node v ; Traverse the adjacency list of Node v ; Ignores if same edge is traversed ; Ignores the edge u -- > v as v -- > u is already processed ; Finds a back Edges , cycle present ; Update the bridge_detect [ v ] ; Else DFS traversal for current node in the adjacency list ; Update the bridge_detect [ v ] ; Store the current directed Edge ; Condition for Bridges ; Return flag ; Function to print the direction of edges to make graph SCCs ; Arrays to store the visited , bridge_detect and order of Nodes ; DFS Traversal from vertex 1 ; If flag is zero , then Bridge is present in the graph ; Else print the direction of Edges assigned ; Function to create graph ; Traverse the Edges ; Push the edges in an adjacency list ; Driver Code ; N vertices and M Edges ; To create Adjacency List ; Create an undirected graph ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < pair < int , int > > ans ; int flag = 1 ; int dfs ( vector < int > adj [ ] , int * order , int * bridge_detect , bool * mark , int v , int l ) { mark [ v ] = 1 ; order [ v ] = order [ l ] + 1 ; bridge_detect [ v ] = order [ v ] ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { int u = adj [ v ] [ i ] ; if ( u == l ) { continue ; } if ( order [ v ] < order [ u ] ) { continue ; } if ( mark [ u ] ) { bridge_detect [ v ] = min ( order [ u ] , bridge_detect [ v ] ) ; } else { dfs ( adj , order , bridge_detect , mark , u , v ) ; } bridge_detect [ v ] = min ( bridge_detect [ u ] , bridge_detect [ v ] ) ; ans . push_back ( make_pair ( v , u ) ) ; } if ( bridge_detect [ v ] == order [ v ] && l != 0 ) { flag = 0 ; } return flag ; } void convert ( vector < int > adj [ ] , int n ) { int order [ n ] = { 0 } ; int bridge_detect [ n ] = { 0 } ; bool mark [ n ] ; memset ( mark , false , sizeof ( mark ) ) ; int flag = dfs ( adj , order , bridge_detect , mark , 1 , 0 ) ; if ( flag == 0 ) { cout << " - 1" ; } else { for ( auto & it : ans ) { cout << it . first << " - > " << it . second << ' ' ; } } } void createGraph ( int Edges [ ] [ 2 ] , vector < int > adj [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } } int main ( ) { int N = 5 , M = 6 ; int Edges [ M ] [ 2 ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 2 } , { 1 , 4 } , { 2 , 3 } , { 3 , 4 } } ; vector < int > adj [ N ] ; createGraph ( Edges , adj , M ) ; convert ( adj , N ) ; return 0 ; }
Shortest Un | CPP program to find shortest subarray which is unsorted . ; bool function for checking an array elements are in increasing . ; bool function for checking an array elements are in decreasing . ; increasing and decreasing are two functions . if function return true value then print 0 otherwise 3. ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool increasing ( int a [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] >= a [ i + 1 ] ) return false ; return true ; } bool decreasing ( int a [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] < a [ i + 1 ] ) return false ; return true ; } int shortestUnsorted ( int a [ ] , int n ) { if ( increasing ( a , n ) == true || decreasing ( a , n ) == true ) return 0 ; else return 3 ; } int main ( ) { int ar [ ] = { 7 , 9 , 10 , 8 , 11 } ; int n = sizeof ( ar ) / sizeof ( ar [ 0 ] ) ; cout << shortestUnsorted ( ar , n ) ; return 0 ; }
Find the maximum path sum between two leaves of a binary tree | C ++ program to find maximum path sum between two leaves of a binary tree ; A binary tree node ; Utility function to allocate memory for a new node ; Utility function to find maximum of two integers ; A utility function to find the maximum sum between anytwo leaves . This function calculates two values : 1 ) Maximum path sum between two leaves which is stored in res . 2 ) The maximum root to leaf path sum which is returned . If one side of root is empty , then it returns INT_MIN ; Base cases ; Find maximum sum in left and right subtree . Also find maximum root to leaf sums in left and right subtrees and store them in ls and rs ; If both left and right children exist ; Update result if needed ; Return maxium possible value for root being on one side ; If any of the two children is empty , return root sum for root being on one side ; The main function which returns sum of the maximum sum path between two leaves . This function mainly uses maxPathSumUtil ( ) ; -- - for test case -- - 7 / \ Null - 3 ( case - 1 ) value of res will be INT_MIN but the answer is 4 , which is returned by the function maxPathSumUtil ( ) . ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int data ; struct Node * left , * right ; } ; struct Node * newNode ( int data ) { struct Node * node = new ( struct Node ) ; node -> data = data ; node -> left = node -> right = NULL ; return ( node ) ; } int max ( int a , int b ) { return ( a >= b ) ? a : b ; } int maxPathSumUtil ( struct Node * root , int & res ) { if ( root == NULL ) return 0 ; if ( ! root -> left && ! root -> right ) return root -> data ; int ls = maxPathSumUtil ( root -> left , res ) ; int rs = maxPathSumUtil ( root -> right , res ) ; if ( root -> left && root -> right ) { res = max ( res , ls + rs + root -> data ) ; return max ( ls , rs ) + root -> data ; } return ( ! root -> left ) ? rs + root -> data : ls + root -> data ; } int maxPathSum ( struct Node * root ) { int res = INT_MIN ; int val = maxPathSumUtil ( root , res ) ; if ( res == INT_MIN ) { return val ; } return res ; } int main ( ) { struct Node * root = newNode ( -15 ) ; root -> left = newNode ( 5 ) ; root -> right = newNode ( 6 ) ; root -> left -> left = newNode ( -8 ) ; root -> left -> right = newNode ( 1 ) ; root -> left -> left -> left = newNode ( 2 ) ; root -> left -> left -> right = newNode ( 6 ) ; root -> right -> left = newNode ( 3 ) ; root -> right -> right = newNode ( 9 ) ; root -> right -> right -> right = newNode ( 0 ) ; root -> right -> right -> right -> left = newNode ( 4 ) ; root -> right -> right -> right -> right = newNode ( -1 ) ; root -> right -> right -> right -> right -> left = newNode ( 10 ) ; cout << " Max ▁ pathSum ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ is ▁ " << maxPathSum ( root ) ; return 0 ; }
Minimum number of swaps required to sort an array | C ++ program to find minimum number of swaps required to sort an array ; Function returns the minimum number of swaps required to sort the array ; Create an array of pairs where first element is array element and second element is position of first element ; Sort the array by array element values to get right position of every element as second element of pair . ; To keep track of visited elements . Initialize all elements as not visited or false . ; Initialize result ; Traverse array elements ; already swapped and corrected or already present at correct pos ; find out the number of node in this cycle and add in ans ; move to next node ; Update answer by adding current cycle . ; Return result ; Driver program to test the above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minSwaps ( int arr [ ] , int n ) { pair < int , int > arrPos [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arrPos [ i ] . first = arr [ i ] ; arrPos [ i ] . second = i ; } sort ( arrPos , arrPos + n ) ; vector < bool > vis ( n , false ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] arrPos [ i ] . second == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = 1 ; j = arrPos [ j ] . second ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } int main ( ) { int arr [ ] = { 1 , 5 , 4 , 3 , 2 } ; int n = ( sizeof ( arr ) / sizeof ( int ) ) ; cout << minSwaps ( arr , n ) ; return 0 ; }
Minimum number of swaps required to sort an array | C ++ program to find minimum number of swaps required to sort an array ; swap function ; indexOf function ; Return the minimum number of swaps required to sort the array ; This is checking whether the current element is at the right place or not ; Swap the current element with the right index so that arr [ 0 ] to arr [ i ] is sorted ; Driver Code ; Output will be 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( vector < int > & arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } int indexOf ( vector < int > & arr , int ele ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return -1 ; } int minSwaps ( vector < int > arr , int N ) { int ans = 0 ; vector < int > temp ( arr . begin ( ) , arr . end ( ) ) ; sort ( temp . begin ( ) , temp . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } int main ( ) { vector < int > a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . size ( ) ; cout << minSwaps ( a , n ) ; }
Minimum number of swaps required to sort an array | C ++ program to find minimum number of swaps required to sort an array ; Return the minimum number of swaps required to sort the array ; Hashmap which stores the indexes of the input array ; This is checking whether the current element is at the right place or not ; If not , swap this element with the index of the element which should come here ; Update the indexes in the hashmap accordingly ; Driver class ; Driver program to test the above function ; Output will be 5
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( vector < int > & arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } int minSwaps ( vector < int > arr , int N ) { int ans = 0 ; vector < int > temp = arr ; map < int , int > h ; sort ( temp . begin ( ) , temp . end ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { h [ arr [ i ] ] = i ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; int init = arr [ i ] ; swap ( arr , i , h [ temp [ i ] ] ) ; h [ init ] = h [ temp [ i ] ] ; h [ temp [ i ] ] = i ; } } return ans ; } int main ( ) { vector < int > a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . size ( ) ; cout << minSwaps ( a , n ) ; }
Union and Intersection of two sorted arrays | C ++ program to find union of two sorted arrays ; Function prints union of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Print remaining elements of the larger array ; Driver program to test above function
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) cout << arr1 [ i ++ ] << " ▁ " ; else if ( arr2 [ j ] < arr1 [ i ] ) cout << arr2 [ j ++ ] << " ▁ " ; else { cout << arr2 [ j ++ ] << " ▁ " ; i ++ ; } } while ( i < m ) cout << arr1 [ i ++ ] << " ▁ " ; while ( j < n ) cout << arr2 [ j ++ ] << " ▁ " ; } int main ( ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; printUnion ( arr1 , arr2 , m , n ) ; return 0 ; }
Check if N can be converted to the form K power K by the given operation | C ++ implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Fcuntion to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7
#include <bits/stdc++.h> NEW_LINE using namespace std ; unordered_set < int > kPowKform ; int dp [ 100005 ] ; int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . count ( n ) ) return 1 ; if ( dp [ n ] != -1 ) return dp [ n ] ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) ) { answer = 1 ; break ; } } x /= 10 ; } return dp [ n ] = answer ; } void canBeConverted ( int n ) { memset ( dp , -1 , sizeof ( dp ) ) ; if ( func ( n ) ) cout << " Yes " ; else cout << " No " ; } int main ( ) { int N = 13 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . insert ( val ) ; } canBeConverted ( N ) ; return 0 ; }
Number of ways to arrange N numbers which are in a range from 1 to K under given constraints . | C ++ program to calculate Number of ways to arrange N numbers under given constraints . ; For favourable nodes ( ending at Q ) ; For Non - favourable nodes ( NOT ending at Q ) ; Function to print Total number of ways ; If the First number and the last number is same . ; DP approach to find current state with the help of previous state . ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; class element { public : int A ; int B ; } ; void NumberOfWays ( int n , int k , int p , int q ) { element * dp = new element [ n ] ; if ( p == q ) { dp [ 0 ] . A = 1 ; dp [ 0 ] . B = 0 ; } else { dp [ 0 ] . A = 0 ; dp [ 0 ] . B = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] . A = dp [ i - 1 ] . B ; dp [ i ] . B = ( dp [ i - 1 ] . A * ( k - 1 ) ) + ( dp [ i - 1 ] . B * ( k - 2 ) ) ; } cout << dp [ n - 1 ] . A << endl ; return ; } int main ( ) { int N = 5 ; int K = 3 ; int P = 2 ; int Q = 1 ; NumberOfWays ( N , K , P , Q ) ; }
Union and Intersection of two sorted arrays | C ++ program to find intersection of two sorted arrays ; Function prints Intersection of arr1 [ ] and arr2 [ ] m is the number of elements in arr1 [ ] n is the number of elements in arr2 [ ] ; Driver program to test above function ; Function calling
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { cout << arr2 [ j ] << " ▁ " ; i ++ ; j ++ ; } } } int main ( ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = sizeof ( arr1 ) / sizeof ( arr1 [ 0 ] ) ; int n = sizeof ( arr2 ) / sizeof ( arr2 [ 0 ] ) ; printIntersection ( arr1 , arr2 , m , n ) ; return 0 ; }