codeparrot/xlcost-text-to-code huggingface dataset:
Maximize sum by selecting M elements from the start or end of rows of a Matrix | C ++ program for the above approach ; Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ; Driver Code ; Given N ; Given M ; Given matrix ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; long mElementsWithMaxSum ( vector < vector < int > > matrix , int M , int block , vector < vector < int > > dp ) { if ( block == matrix . size ( ) ) return 0 ; if ( dp [ block ] [ M ] != -1 ) return dp [ block ] [ M ] ; long ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) ; for ( int i = 0 ; i < matrix [ block ] . size ( ) ; i ++ ) { for ( int j = i ; j < matrix [ block ] . size ( ) ; j ++ ) { if ( j - i + 1 <= M ) { ans = max ( ans , matrix [ block ] [ j ] - ( ( i - 1 ) >= 0 ? matrix [ block ] [ i - 1 ] : 0 ) + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) ; } } } return dp [ block ] [ M ] = ans ; } void preComputing ( vector < vector < int > > matrix , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < matrix [ i ] . size ( ) ; j ++ ) { matrix [ i ] [ j ] = ( j > 0 ? matrix [ i ] [ j - 1 ] : 0 ) + matrix [ i ] [ j ] ; } } } void mElementsWithMaxSumUtil ( vector < vector < int > > matrix , int M , int N ) { preComputing ( matrix , N ) ; long sum = 10 ; vector < vector < int > > dp ; dp . resize ( N + 5 ) ; for ( int i = 0 ; i < N + 5 ; i ++ ) for ( int j = 0 ; j < M + 5 ; j ++ ) dp [ i ] . push_back ( -1 ) ; sum += mElementsWithMaxSum ( matrix , M , 0 , dp ) ; cout << sum ; } int main ( ) { int N = 3 ; int M = 4 ; vector < vector < int > > matrix = { { 2 , 3 , 5 } , { -1 , 7 } , { 8 , 10 } } ; mElementsWithMaxSumUtil ( matrix , M , N ) ; }
Find the last remaining element after repeated removal of odd and even indexed elements alternately | C ++ 14 program for the above approach ; Function to calculate the last remaining element from the sequence ; If dp [ n ] is already calculated ; Base Case : ; Recursive call ; Return the value of dp [ n ] ; Driver Code ; Given N ; Stores the ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int lastRemaining ( int n , map < int , int > & dp ) { if ( dp . find ( n ) != dp . end ( ) ) return dp [ n ] ; if ( n == 1 ) return 1 ; else dp [ n ] = 2 * ( 1 + n / 2 - lastRemaining ( n / 2 , dp ) ) ; return dp [ n ] ; } int main ( ) { int N = 5 ; map < int , int > dp ; cout << lastRemaining ( N , dp ) ; return 0 ; }
Maximum subsequence sum possible by multiplying each element by its index | C ++ program for the above approach ; Initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Initialise the dp array with - 1 ; Print the maximum sum possible ; Driver Code ; Given array ; Size of the array ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int dp [ 1005 ] [ 1005 ] ; int maximumSumUtil ( int a [ ] , int index , int count , int n ) { if ( index > n count > n + 1 ) { return 0 ; } if ( dp [ index ] [ count ] != -1 ) return dp [ index ] [ count ] ; int ans1 = maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ; int ans2 = maximumSumUtil ( a , index + 1 , count , n ) ; return ( dp [ index ] [ count ] = max ( ans1 , ans2 ) ) ; } int maximumSum ( int arr [ ] , int N ) { memset ( dp , -1 , sizeof ( dp ) ) ; cout << maximumSumUtil ( arr , 0 , 1 , N - 1 ) ; } int main ( ) { int arr [ ] = { -1 , 2 , -10 , 4 , -20 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; maximumSum ( arr , N ) ; return 0 ; }
Sort an array according to absolute difference with given value | C ++ program to sort an array according absolute difference with x . ; Function to sort an array according absolute difference with x . ; Store values in a map with the difference with X as key ; Update the values of array ; Function to print the array ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void rearrange ( int arr [ ] , int n , int x ) { multimap < int , int > m ; multimap < int , int > :: iterator it ; for ( int i = 0 ; i < n ; i ++ ) m . insert ( make_pair ( abs ( x - arr [ i ] ) , arr [ i ] ) ) ; int i = 0 ; for ( it = m . begin ( ) ; it != m . end ( ) ; it ++ ) arr [ i ++ ] = ( * it ) . second ; } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int arr [ ] = { 10 , 5 , 3 , 9 , 2 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; int x = 7 ; rearrange ( arr , n , x ) ; printArray ( arr , n ) ; return 0 ; }
Minimum Steps to obtain N from 1 by the given operations | C ++ program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Driver Code ; Given N ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > find_sequence ( int n ) { if ( n == 1 ) return { 1 , -1 } ; auto arr = find_sequence ( n - 1 ) ; vector < int > ans = { arr [ 0 ] + 1 , n - 1 } ; if ( n % 2 == 0 ) { vector < int > div_by_2 = find_sequence ( n / 2 ) ; if ( div_by_2 [ 0 ] < ans [ 0 ] ) ans = { div_by_2 [ 0 ] + 1 , n / 2 } ; } if ( n % 3 == 0 ) { vector < int > div_by_3 = find_sequence ( n / 3 ) ; if ( div_by_3 [ 0 ] < ans [ 0 ] ) vector < int > ans = { div_by_3 [ 0 ] + 1 , n / 3 } ; } return ans ; } vector < int > find_solution ( int n ) { auto a = find_sequence ( n ) ; cout << a [ 0 ] << endl ; vector < int > sequence ; sequence . push_back ( n ) ; while ( a [ 1 ] != -1 ) { sequence . push_back ( a [ 1 ] ) ; auto arr = find_sequence ( a [ 1 ] ) ; a [ 1 ] = arr [ 1 ] ; } reverse ( sequence . begin ( ) , sequence . end ( ) ) ; return sequence ; } int main ( ) { int n = 5 ; auto i = find_solution ( n ) ; for ( int j : i ) cout << j << " ▁ " ; }
Count of vessels completely filled after a given time | C ++ program to implement the above approach ; Function to find the number of completely filled vessels ; Store the vessels ; Assuming all water is present in the vessel at the first level ; Store the number of vessel that are completely full ; Traverse all the levels ; Number of vessel at each level is j ; Calculate the exceeded amount of water ; If current vessel has less than 1 unit of water then continue ; One more vessel is full ; If left bottom vessel present ; If right bottom vessel present ; Driver Code ; Number of levels ; Number of seconds ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int n , t ; int FindNoOfFullVessels ( int n , int t ) { double Matrix [ n ] [ n ] ; Matrix [ 0 ] [ 0 ] = t * 1.0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { double exceededwater = Matrix [ i ] [ j ] - 1.0 ; if ( exceededwater < 0 ) continue ; ans ++ ; if ( i + 1 < n ) Matrix [ i + 1 ] [ j ] += exceededwater / 2 ; if ( i + 1 < n && j + 1 < n ) Matrix [ i + 1 ] [ j + 1 ] += exceededwater / 2 ; } } return ans ; } int main ( ) { int N = 3 ; int T = 4 ; cout << FindNoOfFullVessels ( N , T ) << endl ; return 0 ; }
Sort an array in wave form | A C ++ program to sort an array in wave form using a sorting function ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ] . . ; Sort the input array ; Swap adjacent elements ; Driver program to test above function
#include <iostream> NEW_LINE #include <algorithm> NEW_LINE using namespace std ; void swap ( int * x , int * y ) { int temp = * x ; * x = * y ; * y = temp ; } void sortInWave ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = 0 ; i < n - 1 ; i += 2 ) swap ( & arr [ i ] , & arr [ i + 1 ] ) ; } int main ( ) { int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortInWave ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Count of maximum occurring subsequence using only those characters whose indices are in GP | C ++ program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Initialize 1 - D array and 2 - D dp array to 0 ; Iterate till the length of the given string ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given string s ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findMaxTimes ( string S ) { long long int arr [ 26 ] ; long long int dp [ 26 ] [ 26 ] ; memset ( arr , 0 , sizeof ( arr ) ) ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < S . size ( ) ; i ++ ) { int now = S [ i ] - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } long long int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = max ( ans , dp [ i ] [ j ] ) ; } } return ans ; } int main ( ) { string S = " ddee " ; cout << findMaxTimes ( S ) ; return 0 ; }
Sort an array in wave form | A O ( n ) program to sort an input array in wave form ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] >= arr [ 5 ] ... . ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; void swap ( int * x , int * y ) { int temp = * x ; * x = * y ; * y = temp ; } void sortInWave ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( & arr [ i ] , & arr [ i - 1 ] ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( & arr [ i ] , & arr [ i + 1 ] ) ; } } int main ( ) { int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; sortInWave ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; return 0 ; }
Remove all nodes which don 't lie in any path with sum>= k |  ; A Binary Tree Node ; A utility function to create a new Binary Tree node with given data ; print the tree in LVR ( Inorder traversal ) way . ; Main function which truncates the binary tree . ; Base Case ; Recur for left and right subtrees ; If we reach leaf whose data is smaller than sum , we delete the leaf . An important thing to note is a non - leaf node can become leaf when its chilren are deleted . ; Driver program to test above function
#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 = ( struct Node * ) malloc ( sizeof ( struct Node ) ) ; node -> data = data ; node -> left = node -> right = NULL ; return node ; } void print ( struct Node * root ) { if ( root != NULL ) { print ( root -> left ) ; cout << root -> data << " ▁ " ; print ( root -> right ) ; } } struct Node * prune ( struct Node * root , int sum ) { if ( root == NULL ) return NULL ; root -> left = prune ( root -> left , sum - root -> data ) ; root -> right = prune ( root -> right , sum - root -> data ) ; if ( root -> left == NULL && root -> right == NULL ) { if ( root -> data < sum ) { free ( root ) ; return NULL ; } } return root ; } int main ( ) { int k = 45 ; struct Node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> right = newNode ( 3 ) ; root -> left -> left = newNode ( 4 ) ; root -> left -> right = newNode ( 5 ) ; root -> right -> left = newNode ( 6 ) ; root -> right -> right = newNode ( 7 ) ; root -> left -> left -> left = newNode ( 8 ) ; root -> left -> left -> right = newNode ( 9 ) ; root -> left -> right -> left = newNode ( 12 ) ; root -> right -> right -> left = newNode ( 10 ) ; root -> right -> right -> left -> right = newNode ( 11 ) ; root -> left -> left -> right -> left = newNode ( 13 ) ; root -> left -> left -> right -> right = newNode ( 14 ) ; root -> left -> left -> right -> right -> left = newNode ( 15 ) ; cout << " Tree ▁ before ▁ truncation STRNEWLINE " ; print ( root ) ; root = prune ( root , k ) ; cout << " Tree after truncation " ; print ( root ) ; return 0 ; }
Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | C ++ program for the above approach ; Function to count the unique sum ; Initialize array fsum [ ] with 0 ; Initialize array rsum [ ] with 0 ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Driver Code ; Given a number N ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int count_unique_sum ( int n ) { int i , ar [ n + 1 ] , fsum [ n + 1 ] ; int rsum [ n + 1 ] , ans = 1 ; memset ( fsum , 0 , sizeof fsum ) ; memset ( rsum , 0 , sizeof rsum ) ; for ( i = 0 ; i <= n ; i ++ ) { ar [ i ] = i ; } fsum [ 0 ] = ar [ 0 ] ; rsum [ n ] = ar [ n ] ; for ( i = 1 ; i <= n ; i ++ ) { fsum [ i ] = ar [ i ] + fsum [ i - 1 ] ; } for ( i = n - 1 ; i >= 0 ; i -- ) { rsum [ i ] = ar [ i ] + rsum [ i + 1 ] ; } for ( int k = 2 ; k <= n ; k ++ ) { ans += 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ; } return ans ; } int main ( ) { int N = 4 ; cout << count_unique_sum ( N ) ; return 0 ; }
Merge an array of size n into another array of size m + n | C ++ program to Merge an array of size n into another array of size m + n ; Assuming - 1 is filled for the places where element is not available ; Function to move m elements at the end of array mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Driver code ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define NA  -1 NEW_LINE void moveToEnd ( int mPlusN [ ] , int size ) { int j = size - 1 ; for ( int i = size - 1 ; i >= 0 ; i -- ) if ( mPlusN [ i ] != NA ) { mPlusN [ j ] = mPlusN [ i ] ; j -- ; } } int merge ( int mPlusN [ ] , int N [ ] , int m , int n ) { int i = n ; int j = 0 ; int k = 0 ; while ( k < ( m + n ) ) { if ( ( j == n ) || ( i < ( m + n ) && mPlusN [ i ] <= N [ j ] ) ) { mPlusN [ k ] = mPlusN [ i ] ; k ++ ; i ++ ; } else { mPlusN [ k ] = N [ j ] ; k ++ ; j ++ ; } } } void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) cout << arr [ i ] << " ▁ " ; cout << endl ; } int main ( ) { int mPlusN [ ] = { 2 , 8 , NA , NA , NA , 13 , NA , 15 , 20 } ; int N [ ] = { 5 , 7 , 9 , 25 } ; int n = sizeof ( N ) / sizeof ( N [ 0 ] ) ; int m = sizeof ( mPlusN ) / sizeof ( mPlusN [ 0 ] ) - n ; moveToEnd ( mPlusN , m + n ) ; merge ( mPlusN , N , m , n ) ; printArray ( mPlusN , m + n ) ; return 0 ; }
Count of N | C ++ Program to implement the above approach ; Function to find maximum between two numbers ; Function to find minimum between two numbers ; Function to return the count of such numbers ; For 1 - digit numbers , the count is 10 irrespective of K ; dp [ j ] stores the number of such i - digit numbers ending with j ; Stores the results of length i ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find actual count of i - digit numbers ending with j ; Update dp [ ] ; Stores the final answer ; Return the final answer ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int max ( int num1 , int num2 ) { return ( num1 > num2 ) ? num1 : num2 ; } int min ( int num1 , int num2 ) { return ( num1 > num2 ) ? num2 : num1 ; } int getCount ( int n , int k ) { if ( n == 1 ) return 10 ; int dp [ 11 ] = { 0 } ; int next [ 11 ] = { 0 } ; for ( int i = 1 ; i <= 9 ; i ++ ) dp [ i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { int l = max ( 0 , ( j - k ) ) ; int r = min ( 9 , ( j + k ) ) ; next [ l ] += dp [ j ] ; next [ r + 1 ] -= dp [ j ] ; } for ( int j = 1 ; j <= 9 ; j ++ ) next [ j ] += next [ j - 1 ] ; for ( int j = 0 ; j < 10 ; j ++ ) { dp [ j ] = next [ j ] ; next [ j ] = 0 ; } } int count = 0 ; for ( int i = 0 ; i <= 9 ; i ++ ) count += dp [ i ] ; return count ; } int main ( ) { int n = 2 , k = 1 ; cout << getCount ( n , k ) ; return 0 ; }
Sort 1 to N by swapping adjacent elements | CPP program to test whether array can be sorted by swapping adjacent elements using boolean array ; Return true if array can be sorted otherwise false ; Check bool array B and sorts elements for continuous sequence of 1 ; Sort array A from i to j ; Check if array is sorted or not ; Driver program to test sortedAfterSwap ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { int i , j ; for ( i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { j = i ; while ( B [ j ] ) j ++ ; sort ( A + i , A + 1 + j ) ; i = j ; } } for ( i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return false ; } return true ; } int main ( ) { int A [ ] = { 1 , 2 , 5 , 3 , 4 , 6 } ; bool B [ ] = { 0 , 1 , 1 , 1 , 0 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( sortedAfterSwap ( A , B , n ) ) cout << " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else cout << " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; return 0 ; }
Print negative weight cycle in a Directed Graph | C ++ program for the above approach ; Structure to represent a weighted edge in graph ; Structure to represent a directed and weighted graph ; V -> Number of vertices , E -> Number of edges ; Graph is represented as an array of edges ; Creates a new graph with V vertices and E edges ; Function runs Bellman - Ford algorithm and prints negative cycle ( if present ) ; Initialize distances from src to all other vertices as INFINITE and all parent as - 1 ; Relax all edges | V | - 1 times . ; Check for negative - weight cycles ; Store one of the vertex of the negative weight cycle ; To store the cycle vertex ; Reverse cycle [ ] ; Printing the negative cycle ; Driver Code ; Number of vertices in graph ; Number of edges in graph ; Given Graph ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Edge { int src , dest , weight ; } ; struct Graph { int V , E ; struct Edge * edge ; } ; struct Graph * createGraph ( int V , int E ) { struct Graph * graph = new Graph ; graph -> V = V ; graph -> E = E ; graph -> edge = new Edge [ graph -> E ] ; return graph ; } void NegCycleBellmanFord ( struct Graph * graph , int src ) { int V = graph -> V ; int E = graph -> E ; int dist [ V ] ; int parent [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { dist [ i ] = INT_MAX ; parent [ i ] = -1 ; } dist [ src ] = 0 ; for ( int i = 1 ; i <= V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { int u = graph -> edge [ j ] . src ; int v = graph -> edge [ j ] . dest ; int weight = graph -> edge [ j ] . weight ; if ( dist [ u ] != INT_MAX && dist [ u ] + weight < dist [ v ] ) { dist [ v ] = dist [ u ] + weight ; parent [ v ] = u ; } } } int C = -1 ; for ( int i = 0 ; i < E ; i ++ ) { int u = graph -> edge [ i ] . src ; int v = graph -> edge [ i ] . dest ; int weight = graph -> edge [ i ] . weight ; if ( dist [ u ] != INT_MAX && dist [ u ] + weight < dist [ v ] ) { C = v ; break ; } } if ( C != -1 ) { for ( int i = 0 ; i < V ; i ++ ) C = parent [ C ] ; vector < int > cycle ; for ( int v = C ; ; v = parent [ v ] ) { cycle . push_back ( v ) ; if ( v == C && cycle . size ( ) > 1 ) break ; } reverse ( cycle . begin ( ) , cycle . end ( ) ) ; for ( int v : cycle ) cout << v << ' ▁ ' ; cout << endl ; } else cout << " - 1" << endl ; } int main ( ) { int V = 5 ; int E = 5 ; struct Graph * graph = createGraph ( V , E ) ; graph -> edge [ 0 ] . src = 0 ; graph -> edge [ 0 ] . dest = 1 ; graph -> edge [ 0 ] . weight = 1 ; graph -> edge [ 1 ] . src = 1 ; graph -> edge [ 1 ] . dest = 2 ; graph -> edge [ 1 ] . weight = 2 ; graph -> edge [ 2 ] . src = 2 ; graph -> edge [ 2 ] . dest = 3 ; graph -> edge [ 2 ] . weight = 3 ; graph -> edge [ 3 ] . src = 3 ; graph -> edge [ 3 ] . dest = 4 ; graph -> edge [ 3 ] . weight = -3 ; graph -> edge [ 4 ] . src = 4 ; graph -> edge [ 4 ] . dest = 1 ; graph -> edge [ 4 ] . weight = -3 ; NegCycleBellmanFord ( graph , 0 ) ; return 0 ; }
Sort 1 to N by swapping adjacent elements | CPP program to test whether array can be sorted by swapping adjacent elements using boolean array ; Return true if array can be sorted otherwise false ; Check if array is sorted or not ; Driver program to test sortedAfterSwap ( )
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] ) { if ( A [ i ] != i + 1 ) swap ( A [ i ] , A [ i + 1 ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return false ; } return true ; } int main ( ) { int A [ ] = { 1 , 2 , 5 , 3 , 4 , 6 } ; bool B [ ] = { 0 , 1 , 1 , 1 , 0 } ; int n = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( sortedAfterSwap ( A , B , n ) ) cout << " A ▁ can ▁ be ▁ sorted STRNEWLINE " ; else cout << " A ▁ can ▁ not ▁ be ▁ sorted STRNEWLINE " ; return 0 ; }
Sort an array containing two types of elements | CPP program to sort an array with two types of values in one traversal . ; Method for segregation 0 and 1 given input array ; Driver program
#include <bits/stdc++.h> NEW_LINE using namespace std ; void segregate0and1 ( int arr [ ] , int n ) { int type0 = 0 ; int type1 = n - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { swap ( arr [ type0 ] , arr [ type1 ] ) ; type1 -- ; } else { type0 ++ ; } } } int main ( ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; segregate0and1 ( arr , n ) ; for ( int a : arr ) cout << a << " ▁ " ; }
Pentanacci Numbers | A simple recursive program to print Nth Pentanacci number ; Recursive function to find the Nth Pentanacci number ; Function to print the Nth Pentanacci number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int printpentaRec ( int n ) { if ( n == 0 n == 1 n == 2 n == 3 n == 4 ) return 0 ; else if ( n == 5 ) return 1 ; else return ( printpentaRec ( n - 1 ) + printpentaRec ( n - 2 ) + printpentaRec ( n - 3 ) + printpentaRec ( n - 4 ) + printpentaRec ( n - 5 ) ) ; } void printPenta ( int n ) { cout << printpentaRec ( n ) << " STRNEWLINE " ; } int main ( ) { int n = 10 ; printPenta ( n ) ; return 0 ; }