codeparrot/xlcost-text-to-code huggingface dataset:
Print list items containing all characters of a given word | C ++ program to print all strings that contain all characters of a word ; prints list items having all characters of word ; Since calloc is used , map [ ] is initialized as 0 ; Set the values in map ; Get the length of given word ; Check each item of list if has all characters of word ; unset the bit so that strings like sss not printed ; Set the values in map for next item ; Driver code
#include <bits/stdc++.h> NEW_LINE #include <stdio.h> NEW_LINE #include <string.h> NEW_LINE using namespace std ; # define NO_OF_CHARS  256 NEW_LINE void print ( char list [ ] [ 50 ] , char * word , int list_size ) { int * map = new int [ ( sizeof ( int ) * NO_OF_CHARS ) ] ; int i , j , count , word_size ; for ( i = 0 ; * ( word + i ) ; i ++ ) map [ * ( word + i ) ] = 1 ; word_size = strlen ( word ) ; for ( i = 0 ; i < list_size ; i ++ ) { for ( j = 0 , count = 0 ; * ( list [ i ] + j ) ; j ++ ) { if ( map [ * ( list [ i ] + j ) ] ) { count ++ ; map [ * ( list [ i ] + j ) ] = 0 ; } } if ( count == word_size ) cout << list [ i ] << endl ; for ( j = 0 ; * ( word + j ) ; j ++ ) map [ * ( word + j ) ] = 1 ; } } int main ( ) { char str [ ] = " sun " ; char list [ ] [ 50 ] = { " geeksforgeeks " , " unsorted " , " sunday " , " just " , " sss " } ; print ( list , str , 5 ) ; return 0 ; }
Given a string , find its first non | C ++ program to find first non - repeating character ; Returns an array of size 256 containing count of characters in the passed char array ; The function returns index of first non - repeating character in a string . If all characters are repeating then returns - 1 ; To avoid memory leak ; Driver program to test above function
#include <iostream> NEW_LINE using namespace std ; #define NO_OF_CHARS  256 NEW_LINE int * getCharCountArray ( char * str ) { int * count = ( int * ) calloc ( sizeof ( int ) , NO_OF_CHARS ) ; int i ; for ( i = 0 ; * ( str + i ) ; i ++ ) count [ * ( str + i ) ] ++ ; return count ; } int firstNonRepeating ( char * str ) { int * count = getCharCountArray ( str ) ; int index = -1 , i ; for ( i = 0 ; * ( str + i ) ; i ++ ) { if ( count [ * ( str + i ) ] == 1 ) { index = i ; break ; } } free ( count ) ; return index ; } int main ( ) { char str [ ] = " geeksforgeeks " ; int index = firstNonRepeating ( str ) ; if ( index == -1 ) cout << " Either ▁ all ▁ characters ▁ are ▁ repeating ▁ or ▁ " " string ▁ is ▁ empty " ; else cout << " First ▁ non - repeating ▁ character ▁ is ▁ " << str [ index ] ; getchar ( ) ; return 0 ; }
Divide a string in N equal parts | C ++ program to divide a string in n equal parts ; Function to print n equal parts of str ; Check if string can be divided in n equal parts ; Calculate the size of parts to find the division points ; Driver code ; length od string is 28 ; Print 4 equal parts of the string
#include <iostream> NEW_LINE #include <string.h> NEW_LINE using namespace std ; class gfg { public : void divideString ( char str [ ] , int n ) { int str_size = strlen ( str ) ; int i ; int part_size ; if ( str_size % n != 0 ) { cout << " Invalid ▁ Input : ▁ String ▁ size " ; cout << " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ; return ; } part_size = str_size / n ; for ( i = 0 ; i < str_size ; i ++ ) { if ( i % part_size == 0 ) cout << endl ; cout << str [ i ] ; } } } ; int main ( ) { gfg g ; char str [ ] = " a _ simple _ divide _ string _ quest " ; g . divideString ( str , 4 ) ; return 0 ; }
Boggle ( Find all possible words in a board of characters ) | Set 1 | C ++ program for Boggle game ; Let the given dictionary be following ; A given function to check if a given string is present in dictionary . The implementation is naive for simplicity . As per the question dictionary is given to us . ; Linearly search all words ; A recursive function to print all words present on boggle ; Mark current cell as visited and append current character to str ; If str is present in dictionary , then print it ; Traverse 8 adjacent cells of boggle [ i ] [ j ] ; Erase current character from string and mark visited of current cell as false ; Prints all words present in dictionary . ; Mark all characters as not visited ; Initialize current string ; Consider every character and look for all words starting with this character ; Driver program to test above function
#include <cstring> NEW_LINE #include <iostream> NEW_LINE using namespace std ; #define M  3 NEW_LINE #define N  3 NEW_LINE string dictionary [ ] = { " GEEKS " , " FOR " , " QUIZ " , " GO " } ; int n = sizeof ( dictionary ) / sizeof ( dictionary [ 0 ] ) ; bool isWord ( string & str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . compare ( dictionary [ i ] ) == 0 ) return true ; return false ; } void findWordsUtil ( char boggle [ M ] [ N ] , bool visited [ M ] [ N ] , int i , int j , string & str ) { visited [ i ] [ j ] = true ; str = str + boggle [ i ] [ j ] ; if ( isWord ( str ) ) cout << str << endl ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row ] [ col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str . erase ( str . length ( ) - 1 ) ; visited [ i ] [ j ] = false ; } void findWords ( char boggle [ M ] [ N ] ) { bool visited [ M ] [ N ] = { { false } } ; string str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } int main ( ) { char boggle [ M ] [ N ] = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; cout << " Following ▁ words ▁ of ▁ dictionary ▁ are ▁ present STRNEWLINE " ; findWords ( boggle ) ; return 0 ; }
Print all paths from a source point to all the 4 corners of a Matrix | C ++ program for the above approach ; Function to check if we reached on of the entry / exit ( corner ) point . ; Function to check if the index is within the matrix boundary . ; Recursive helper function ; If any corner is reached push the string t into ans and return ; For all the four directions ; The new ith index ; The new jth index ; The direction R / L / U / D ; If the new cell is within the matrix boundary and it is not previously visited in same path ; Mark the new cell as visited ; Store the direction ; Recur ; Backtrack to explore other paths ; Function to find all possible paths ; Create a direction array for all the four directions ; Stores the result ; Driver Code ; Initializing the variables ; Function Call ; Print the result
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct direction { int x , y ; char c ; } ; bool isCorner ( int i , int j , int M , int N ) { if ( ( i == 0 && j == 0 ) || ( i == 0 && j == N - 1 ) || ( i == M - 1 && j == N - 1 ) || ( i == M - 1 && j == 0 ) ) return true ; return false ; } bool isValid ( int i , int j , int M , int N ) { if ( i < 0 i > = M j < 0 j > = N ) return false ; return true ; } void solve ( int i , int j , int M , int N , direction dir [ ] , vector < vector < int > > & maze , string & t , vector < string > & ans ) { if ( isCorner ( i , j , M , N ) ) { ans . push_back ( t ) ; return ; } for ( int k = 0 ; k < 4 ; k ++ ) { int x = i + dir [ k ] . x ; int y = j + dir [ k ] . y ; char c = dir [ k ] . c ; if ( isValid ( x , y , M , N ) && maze [ x ] [ y ] == 1 ) { maze [ x ] [ y ] = 0 ; t . push_back ( c ) ; solve ( x , y , M , N , dir , maze , t , ans ) ; t . pop_back ( ) ; maze [ x ] [ y ] = 1 ; } } return ; } vector < string > possiblePaths ( vector < int > src , vector < vector < int > > & maze ) { direction dir [ 4 ] = { { -1 , 0 , ' U ' } , { 0 , 1 , ' R ' } , { 1 , 0 , ' D ' } , { 0 , -1 , ' L ' } } ; string temp ; vector < string > ans ; solve ( src [ 0 ] , src [ 1 ] , maze . size ( ) , maze [ 0 ] . size ( ) , dir , maze , temp , ans ) ; return ans ; } int main ( ) { vector < vector < int > > maze = { { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 1 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 } , { 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 } , { 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 0 , 1 } , } ; vector < int > src = { 4 , 2 } ; vector < string > paths = possiblePaths ( src , maze ) ; if ( paths . size ( ) == 0 ) { cout << " No ▁ Possible ▁ Paths " ; return 0 ; } for ( int i = 0 ; i < paths . size ( ) ; i ++ ) cout << paths [ i ] << endl ; return 0 ; }
Remove all subtrees consisting only of even valued nodes from a Binary Tree | C ++ program for the above approach ; Node of the tree ; Function to create a new node ; Function to print tree level wise ; Base Case ; Create an empty queue for level order traversal ; Enqueue Root ; Print front of queue and remove it from queue ; If left child is present ; Otherwise ; If right child is present ; Otherwise ; Function to remove subtrees ; Base Case ; Search for required condition in left and right half ; If the node is even and leaf node ; Driver Code ; Function Call ; Print answer
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct node { int data ; struct node * left , * right ; } ; node * newNode ( int key ) { node * temp = new node ; temp -> data = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } void printLevelOrder ( node * root ) { if ( ! root ) return ; queue < node * > q ; q . push ( root ) ; while ( ! q . empty ( ) ) { node * temp = q . front ( ) ; cout << temp -> data << " ▁ " ; q . pop ( ) ; if ( temp -> left != NULL ) { q . push ( temp -> left ) ; } else if ( temp -> right != NULL ) { cout << " NULL ▁ " ; } if ( temp -> right != NULL ) { q . push ( temp -> right ) ; } else if ( temp -> left != NULL ) { cout << " NULL ▁ " ; } } } node * pruneTree ( node * root ) { if ( ! root ) { return NULL ; } root -> left = pruneTree ( root -> left ) ; root -> right = pruneTree ( root -> right ) ; if ( root -> data % 2 == 0 && ! root -> right && ! root -> left ) return NULL ; return root ; } int main ( ) { struct node * root = newNode ( 1 ) ; root -> left = newNode ( 2 ) ; root -> left -> left = newNode ( 8 ) ; root -> left -> right = newNode ( 10 ) ; root -> right = newNode ( 7 ) ; root -> right -> left = newNode ( 12 ) ; root -> right -> right = newNode ( 5 ) ; node * newRoot = pruneTree ( root ) ; printLevelOrder ( newRoot ) ; return 0 ; }
Count total ways to reach destination from source in an undirected Graph | C ++ program to count total number of ways to reach destination in a graph ; Utility Function to count total ways ; Base condition When reach to the destination ; Make vertex visited ; Recursive function , for count ways ; Backtracking Make vertex unvisited ; Return total ways ; Function to count total ways to reach destination ; Loop to make all vertex unvisited , Initially ; Make source visited ; Print total ways
#include <iostream> NEW_LINE using namespace std ; int countWays ( int mtrx [ ] [ 11 ] , int vrtx , int i , int dest , bool visited [ ] ) { if ( i == dest ) { return 1 ; } int total = 0 ; for ( int j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 && ! visited [ j ] ) { visited [ j ] = true ; total += countWays ( mtrx , vrtx , j , dest , visited ) ; visited [ j ] = false ; } } return total ; } int totalWays ( int mtrx [ ] [ 11 ] , int vrtx , int src , int dest ) { bool visited [ vrtx ] ; for ( int i = 0 ; i < vrtx ; i ++ ) { visited [ i ] = false ; } visited [ src ] = true ; return countWays ( mtrx , vrtx , src , dest , visited ) ; } int main ( ) { int vrtx = 11 ; int mtrx [ 11 ] [ 11 ] = { { 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 } , { 1 , 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 1 , 1 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } } ; int src = 3 ; int dest = 9 ; cout << totalWays ( mtrx , vrtx , src - 1 , dest - 1 ) ; return 0 ; }
Print path from given Source to Destination in 2 | C ++ program for printing a path from given source to destination ; Function to print the path ; Base condition ; Pop stores elements ; Recursive call for printing stack In reverse order ; Function to store the path into The stack , if path exist ; Base condition ; Push current elements ; Condition to check whether reach to the Destination or not ; Increment ' x ' ordinate of source by ( 2 * x + y ) Keeping ' y ' constant ; Increment ' y ' ordinate of source by ( 2 * y + x ) Keeping ' x ' constant ; Pop current elements form stack ; If no path exists ; Utility function to check whether path exist or not ; To store x co - ordinate ; To store y co - ordinate ; Function to find the path ; Print - 1 , if path doesn 't exist ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void printExistPath ( stack < int > sx , stack < int > sy , int last ) { if ( sx . empty ( ) || sy . empty ( ) ) { return ; } int x = sx . top ( ) ; int y = sy . top ( ) ; sx . pop ( ) ; sy . pop ( ) ; printExistPath ( sx , sy , last ) ; if ( sx . size ( ) == last - 1 ) { cout << " ( " << x << " , ▁ " << y << " ) " ; } else { cout << " ( " << x << " , ▁ " << y << " ) ▁ - > ▁ " ; } } bool storePath ( int srcX , int srcY , int destX , int destY , stack < int > & sx , stack < int > & sy ) { if ( srcX > destX srcY > destY ) { return false ; } sx . push ( srcX ) ; sy . push ( srcY ) ; if ( srcX == destX && srcY == destY ) { printExistPath ( sx , sy , sx . size ( ) ) ; return true ; } if ( storePath ( ( 2 * srcX ) + srcY , srcY , destX , destY , sx , sy ) ) { return true ; } if ( storePath ( srcX , ( 2 * srcY ) + srcX , destX , destY , sx , sy ) ) { return true ; } sx . pop ( ) ; sy . pop ( ) ; return false ; } bool isPathExist ( int srcX , int srcY , int destX , int destY ) { stack < int > sx ; stack < int > sy ; return storePath ( srcX , srcY , destX , destY , sx , sy ) ; } void printPath ( int srcX , int srcY , int destX , int destY ) { if ( ! isPathExist ( srcX , srcY , destX , destY ) ) { cout << " - 1" ; } } int main ( ) { int srcX = 5 , srcY = 8 ; int destX = 83 , destY = 21 ; printPath ( srcX , srcY , destX , destY ) ; }
Count even paths in Binary Tree | C ++ program for the above approach ; A Tree node ; Utility function to create a new node ; Utility function to count the even path in a given Binary tree ; Base Condition , when node pointer becomes null or node value is odd ; Increment count when encounter leaf node with all node value even ; Left recursive call , and save the value of count ; Right recursive call , and return value of count ; Function to count the even paths in a given Binary tree ; Function call with count = 0 ; Driver Code ; Tree ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; struct Node { int key ; struct Node * left , * right ; } ; Node * newNode ( int key ) { Node * temp = new Node ; temp -> key = key ; temp -> left = temp -> right = NULL ; return ( temp ) ; } int evenPaths ( struct Node * node , int count ) { if ( node == NULL || ( node -> key % 2 != 0 ) ) { return count ; } if ( ! node -> left && ! node -> right ) { count ++ ; } count = evenPaths ( node -> left , count ) ; return evenPaths ( node -> right , count ) ; } int countEvenPaths ( struct Node * node ) { return evenPaths ( node , 0 ) ; } int main ( ) { Node * root = newNode ( 12 ) ; root -> left = newNode ( 13 ) ; root -> right = newNode ( 12 ) ; root -> right -> left = newNode ( 14 ) ; root -> right -> right = newNode ( 16 ) ; root -> right -> left -> left = newNode ( 21 ) ; root -> right -> left -> right = newNode ( 22 ) ; root -> right -> right -> left = newNode ( 22 ) ; root -> right -> right -> right = newNode ( 24 ) ; root -> right -> right -> right -> left = newNode ( 8 ) ; cout << countEvenPaths ( root ) ; return 0 ; }
Sum of subsets of all the subsets of an array | O ( 3 ^ N ) | C ++ implementation of the approach ; To store the final ans ; Function to sum of all subsets of a given array ; Base case ; Recursively calling subsetSum ; Function to generate the subsets ; Base - case ; Finding the sum of all the subsets of the generated subset ; Recursively accepting and rejecting the current number ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int ans = 0 ; vector < int > c ; subsetGen ( arr , 0 , n , ans , c ) ; cout << ans ; return 0 ; } void subsetSum ( vector < int > & c , int i , int & ans , int curr ) { if ( i == c . size ( ) ) { ans += curr ; return ; } subsetSum ( c , i + 1 , ans , curr + c [ i ] ) ; subsetSum ( c , i + 1 , ans , curr ) ; } void subsetGen ( int * arr , int i , int n , int & ans , vector < int > & c ) { if ( i == n ) { subsetSum ( c , 0 , ans , 0 ) ; return ; } subsetGen ( arr , i + 1 , n , ans , c ) ; c . push_back ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , ans , c ) ; c . pop_back ( ) ; } int main ( ) { int arr [ ] = { 1 , 1 } ; int n = sizeof ( arr ) / sizeof ( int ) ;
Print the DFS traversal step | C ++ program to print the complete DFS - traversal of graph using back - tracking ; Function to print the complete DFS - traversal ; Check if all th node is visited or not and count unvisited nodes ; If all the node is visited return ; ; Mark not visited node as visited ; Track the current edge ; Print the node ; Check for not visited node and proceed with it . ; call the DFs function if not visited ; Backtrack through the last visited nodes ; Function to call the DFS function which prints the DFS - traversal stepwise ; Create a array of visited node ; Vector to track last visited road ; Initialize all the node with false ; call the function ; Function to insert edges in Graph ; Driver Code ; number of nodes and edges in the graph ; Function call to create the graph ; Call the function to print
#include <bits/stdc++.h> NEW_LINE using namespace std ; const int N = 1000 ; vector < int > adj [ N ] ; void dfsUtil ( int u , int node , bool visited [ ] , vector < pair < int , int > > road_used , int parent , int it ) { int c = 0 ; for ( int i = 0 ; i < node ; i ++ ) if ( visited [ i ] ) c ++ ; if ( c == node ) return ; visited [ u ] = true ; road_used . push_back ( { parent , u } ) ; cout << u << " ▁ " ; for ( int x : adj [ u ] ) { if ( ! visited [ x ] ) dfsUtil ( x , node , visited , road_used , u , it + 1 ) ; } for ( auto y : road_used ) if ( y . second == u ) dfsUtil ( y . first , node , visited , road_used , u , it + 1 ) ; } void dfs ( int node ) { bool visited [ node ] ; vector < pair < int , int > > road_used ; for ( int i = 0 ; i < node ; i ++ ) visited [ i ] = false ; dfsUtil ( 0 , node , visited , road_used , -1 , 0 ) ; } void insertEdge ( int u , int v ) { adj [ u ] . push_back ( v ) ; adj [ v ] . push_back ( u ) ; } int main ( ) { int node = 11 , edge = 13 ; insertEdge ( 0 , 1 ) ; insertEdge ( 0 , 2 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 2 , 9 ) ; insertEdge ( 6 , 7 ) ; insertEdge ( 6 , 8 ) ; insertEdge ( 7 , 8 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 3 , 9 ) ; insertEdge ( 3 , 10 ) ; insertEdge ( 9 , 10 ) ; dfs ( node ) ; return 0 ; }
Find Maximum number possible by doing at | C ++ program to find maximum integer possible by doing at - most K swap operations on its digits . ; Function to find maximum integer possible by doing at - most K swap operations on its digits ; Return if no swaps left ; Consider every digit ; Compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaximumNum ( string str , int k , string & max ) { if ( k == 0 ) return ; int n = str . length ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { swap ( str [ i ] , str [ j ] ) ; if ( str . compare ( max ) > 0 ) max = str ; findMaximumNum ( str , k - 1 , max ) ; swap ( str [ i ] , str [ j ] ) ; } } } } int main ( ) { string str = "129814999" ; int k = 4 ; string max = str ; findMaximumNum ( str , k , max ) ; cout << max << endl ; return 0 ; }
Print all possible paths from top left to bottom right of a mXn matrix | C ++ program to Print all possible paths from top left to bottom right of a mXn matrix ; If we reach the bottom of maze , we can only move right ; path . append ( maze [ i ] [ k ] ) ; If we hit this block , it means one path is completed . Add it to paths list and print ; If we reach to the right most corner , we can only move down ; path . append ( maze [ j ] [ k ] ) If we hit this block , it means one path is completed . Add it to paths list and print ; Add current element to the path list path . append ( maze [ i ] [ j ] ) ; Move down in y direction and call findPathsUtil recursively ; Move down in y direction and call findPathsUtil recursively ;  ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < vector < int > > allPaths ; void findPathsUtil ( vector < vector < int > > maze , int m , int n , int i , int j , vector < int > path , int indx ) { if ( i == m - 1 ) { for ( int k = j ; k < n ; k ++ ) { path [ indx + k - j ] = maze [ i ] [ k ] ; } cout << " [ " << path [ 0 ] << " , ▁ " ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { cout << path [ z ] << " , ▁ " ; } cout << path [ path . size ( ) - 1 ] << " ] " << endl ; allPaths . push_back ( path ) ; return ; } if ( j == n - 1 ) { for ( int k = i ; k < m ; k ++ ) { path [ indx + k - i ] = maze [ k ] [ j ] ; } cout << " [ " << path [ 0 ] << " , ▁ " ; for ( int z = 1 ; z < path . size ( ) - 1 ; z ++ ) { cout << path [ z ] << " , ▁ " ; } cout << path [ path . size ( ) - 1 ] << " ] " << endl ; allPaths . push_back ( path ) ; return ; } path [ indx ] = maze [ i ] [ j ] ; findPathsUtil ( maze , m , n , i + 1 , j , path , indx + 1 ) ; findPathsUtil ( maze , m , n , i , j + 1 , path , indx + 1 ) ; } void findPaths ( vector < vector < int > > maze , int m , int n ) { vector < int > path ( m + n - 1 , 0 ) ; findPathsUtil ( maze , m , n , 0 , 0 , path , 0 ) ; } int main ( ) { vector < vector < int > > maze { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPaths ( maze , 3 , 3 ) ; return 0 ; }
Given an array A [ ] and a number x , check for pair in A [ ] with sum as x | * This C ++ program tells if there exists a pair in array whose sum results in x . ; Function to find and print pair ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool chkPair ( int A [ ] , int size , int x ) { for ( int i = 0 ; i < ( size - 1 ) ; i ++ ) { for ( int j = ( i + 1 ) ; j < size ; j ++ ) { if ( A [ i ] + A [ j ] == x ) { cout << " Pair ▁ with ▁ a ▁ given ▁ sum ▁ " << x << " ▁ is ▁ ( " << A [ i ] << " , ▁ " << A [ j ] << " ) " << endl ; return 1 ; } } } return 0 ; } int main ( void ) { int A [ ] = { 0 , -1 , 2 , -3 , 1 } ; int x = -2 ; int size = sizeof ( A ) / sizeof ( A [ 0 ] ) ; if ( chkPair ( A , size , x ) ) { cout << " Valid ▁ pair ▁ exists " << endl ; } else { cout << " No ▁ valid ▁ pair ▁ exists ▁ for ▁ " << x << endl ; } return 0 ; }
Reduce the array by deleting elements which are greater than all elements to its left | C ++ program for the above approach ; Function to implement merging of arr [ ] ; Function to delete all elements which satisfy the condition A [ i ] > A [ i - 1 ] ; Divide array into its subarray ; Getting back merged array with all its right element greater than left one . ; Driver Code ; Given array arr [ ]
#include <bits/stdc++.h> NEW_LINE using namespace std ; vector < int > merge ( vector < int > x , vector < int > y ) { for ( auto i : y ) { if ( x [ x . size ( ) - 1 ] > i ) x . push_back ( i ) ; } return x ; } vector < int > mergeDel ( vector < int > l ) { if ( l . size ( ) == 1 ) return l ; int m = l . size ( ) / 2 ; vector < int > temp1 = { l . begin ( ) + 0 , l . begin ( ) + m } ; vector < int > temp2 = { l . begin ( ) + m , l . end ( ) } ; return merge ( mergeDel ( temp1 ) , mergeDel ( temp2 ) ) ; } int main ( ) { vector < int > arr ( { 5 , 4 , 3 , 2 , 1 } ) ; vector < int > ans = mergeDel ( arr ) ; cout << " [ ▁ " ; for ( auto x : ans ) cout << x << " , ▁ " ; cout << " ] " ; }