codeparrot/xlcost-text-to-code huggingface dataset:
Count nodes within K | C ++ program to count nodes inside K distance range from marked nodes ; Utility bfs method to fill distance vector and returns most distant marked node from node u ; push node u in queue and initialize its distance as 0 ; loop untill all nodes are processed ; if node is marked , update lastMarked variable ; loop over all neighbors of u and update their distance before pushing in queue ; if not given value already ; return last updated marked value ; method returns count of nodes which are in K - distance range from marked nodes ; vertices in a tree are one more than number of edges ; fill vector for graph ; fill boolean array mark from marked array ; vectors to store distances ; first bfs ( from any random node ) to get one distant marked node ; second bfs to get other distant marked node and also dl is filled with distances from first chosen marked node ; third bfs to fill dr by distances from second chosen marked node ; loop over all nodes ; increase res by 1 , if current node has distance less than K from both extreme nodes ; Driver code to test above methods
#include <bits/stdc++.h> NEW_LINE using namespace std ; int bfsWithDistance ( vector < int > g [ ] , bool mark [ ] , int u , vector < int > & dis ) { int lastMarked ; queue < int > q ; q . push ( u ) ; dis [ u ] = 0 ; while ( ! q . empty ( ) ) { u = q . front ( ) ; q . pop ( ) ; if ( mark [ u ] ) lastMarked = u ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] [ i ] ; if ( dis [ v ] == -1 ) { dis [ v ] = dis [ u ] + 1 ; q . push ( v ) ; } } } return lastMarked ; } int nodesKDistanceFromMarked ( int edges [ ] [ 2 ] , int V , int marked [ ] , int N , int K ) { V = V + 1 ; vector < int > g [ V ] ; int u , v ; for ( int i = 0 ; i < ( V - 1 ) ; i ++ ) { u = edges [ i ] [ 0 ] ; v = edges [ i ] [ 1 ] ; g [ u ] . push_back ( v ) ; g [ v ] . push_back ( u ) ; } bool mark [ V ] = { false } ; for ( int i = 0 ; i < N ; i ++ ) mark [ marked [ i ] ] = true ; vector < int > tmp ( V , -1 ) , dl ( V , -1 ) , dr ( V , -1 ) ; u = bfsWithDistance ( g , mark , 0 , tmp ) ; v = bfsWithDistance ( g , mark , u , dl ) ; bfsWithDistance ( g , mark , v , dr ) ; int res = 0 ; for ( int i = 0 ; i < V ; i ++ ) { if ( dl [ i ] <= K && dr [ i ] <= K ) res ++ ; } return res ; } int main ( ) { int edges [ ] [ 2 ] = { { 1 , 0 } , { 0 , 3 } , { 0 , 8 } , { 2 , 3 } , { 3 , 5 } , { 3 , 6 } , { 3 , 7 } , { 4 , 5 } , { 5 , 9 } } ; int V = sizeof ( edges ) / sizeof ( edges [ 0 ] ) ; int marked [ ] = { 1 , 2 , 4 } ; int N = sizeof ( marked ) / sizeof ( marked [ 0 ] ) ; int K = 3 ; cout << nodesKDistanceFromMarked ( edges , V , marked , N , K ) ; return 0 ; }
Check whether a given number is even or odd | A simple C ++ program to check for even or odd ; Returns true if n is even , else odd ; n & 1 is 1 , then odd , else even ; Driver code
#include <iostream> NEW_LINE using namespace std ; bool isEven ( int n ) { return ( ! ( n & 1 ) ) ; } int main ( ) { int n = 101 ; isEven ( n ) ? cout << " Even " : cout << " Odd " ; return 0 ; }
Count distinct occurrences as a subsequence | C / C ++ program to count number of times S appears as a subsequence in T ; T can 't appear as a subsequence in S ; mat [ i ] [ j ] stores the count of occurrences of T ( 1. . i ) in S ( 1. . j ) . ; Initializing first column with all 0 s . An empty string can 't have another string as suhsequence ; Initializing first row with all 1 s . An empty string is subsequence of all . ; Fill mat [ ] [ ] in bottom up manner ; If last characters don 't match, then value  is same as the value without last character  in S. ; Else value is obtained considering two cases . a ) All substrings without last character in S b ) All substrings without last characters in both . ; uncomment this to print matrix mat for ( int i = 1 ; i <= m ; i ++ , cout << endl ) for ( int j = 1 ; j <= n ; j ++ ) cout << mat [ i ] [ j ] << " ▁ " ; ; Driver code to check above method
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findSubsequenceCount ( string S , string T ) { int m = T . length ( ) , n = S . length ( ) ; if ( m > n ) return 0 ; int mat [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( T [ i - 1 ] != S [ j - 1 ] ) mat [ i ] [ j ] = mat [ i ] [ j - 1 ] ; else mat [ i ] [ j ] = mat [ i ] [ j - 1 ] + mat [ i - 1 ] [ j - 1 ] ; } } return mat [ m ] [ n ] ; } int main ( ) { string T = " ge " ; string S = " geeksforgeeks " ; cout << findSubsequenceCount ( S , T ) << endl ; return 0 ; }
3 | C ++ program for 3 - way quick sort ; This function partitions a [ ] in three parts a ) a [ l . . i ] contains all elements smaller than pivot b ) a [ i + 1. . j - 1 ] contains all occurrences of pivot c ) a [ j . . r ] contains all elements greater than pivot ; From left , find the first element greater than or equal to v . This loop will definitely terminate as v is last element ; From right , find the first element smaller than or equal to v ; If i and j cross , then we are done ; Swap , so that smaller goes on left greater goes on right ; Move all same left occurrence of pivot to beginning of array and keep count using p ; Move all same right occurrence of pivot to end of array and keep count using q ; Move pivot element to its correct index ; Move all left same occurrences from beginning to adjacent to arr [ i ] ; Move all right same occurrences from end to adjacent to arr [ i ] ; 3 - way partition based quick sort ; Note that i and j are passed as reference ; Recur ; A utility function to print an array ; Driver code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void partition ( int a [ ] , int l , int r , int & i , int & j ) { i = l - 1 , j = r ; int p = l - 1 , q = r ; int v = a [ r ] ; while ( true ) { while ( a [ ++ i ] < v ) ; while ( v < a [ -- j ] ) if ( j == l ) break ; if ( i >= j ) break ; swap ( a [ i ] , a [ j ] ) ; if ( a [ i ] == v ) { p ++ ; swap ( a [ p ] , a [ i ] ) ; } if ( a [ j ] == v ) { q -- ; swap ( a [ j ] , a [ q ] ) ; } } swap ( a [ i ] , a [ r ] ) ; j = i - 1 ; for ( int k = l ; k < p ; k ++ , j -- ) swap ( a [ k ] , a [ j ] ) ; i = i + 1 ; for ( int k = r - 1 ; k > q ; k -- , i ++ ) swap ( a [ i ] , a [ k ] ) ; } void quicksort ( int a [ ] , int l , int r ) { if ( r <= l ) return ; int i , j ; partition ( a , l , r , i , j ) ; quicksort ( a , l , j ) ; quicksort ( a , i , r ) ; } void printarr ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) printf ( " % d ▁ " , a [ i ] ) ; printf ( " STRNEWLINE " ) ; } int main ( ) { int a [ ] = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = sizeof ( a ) / sizeof ( int ) ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; return 0 ; }
3 | C ++ program for 3 - way quick sort ; A utility function to print an array ; It uses Dutch National Flag Algorithm ; To handle 2 elements ; update i and j ; j = mid ; or high + 1 ; 3 - way partition based quick sort ; if ( low >= high ) 1 or 0 elements ; Note that i and j are passed as reference ; Recur two halves ; Driver Code ; int a [ ] = { 4 , 39 , 54 , 14 , 31 , 89 , 44 , 34 , 59 , 64 , 64 , 11 , 41 } ; int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int a [ ] = { 91 , 82 , 73 , 64 , 55 , 46 , 37 , 28 , 19 , 10 } ; int a [ ] = { 4 , 9 , 4 , 4 , 9 , 1 , 1 , 1 } ; ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void swap ( int * a , int * b ) { int temp = * a ; * a = * b ; * b = temp ; } void printarr ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) printf ( " % d ▁ " , a [ i ] ) ; printf ( " STRNEWLINE " ) ; } void partition ( int a [ ] , int low , int high , int & i , int & j ) { if ( high - low <= 1 ) { if ( a [ high ] < a [ low ] ) swap ( & a [ high ] , & a [ low ] ) ; i = low ; j = high ; return ; } int mid = low ; int pivot = a [ high ] ; while ( mid <= high ) { if ( a [ mid ] < pivot ) swap ( & a [ low ++ ] , & a [ mid ++ ] ) ; else if ( a [ mid ] == pivot ) mid ++ ; else if ( a [ mid ] > pivot ) swap ( & a [ mid ] , & a [ high -- ] ) ; } i = low - 1 ; } void quicksort ( int a [ ] , int low , int high ) { return ; int i , j ; partition ( a , low , high , i , j ) ; quicksort ( a , low , i ) ; quicksort ( a , j , high ) ; } int main ( ) { int a [ ] = { 4 , 9 , 4 , 4 , 1 , 9 , 4 , 4 , 9 , 4 , 4 , 1 , 4 } ; int size = sizeof ( a ) / sizeof ( int ) ; printarr ( a , size ) ; quicksort ( a , 0 , size - 1 ) ; printarr ( a , size ) ; return 0 ; }
Check if a Regular Bracket Sequence can be formed with concatenation of given strings | C ++ program for the above approach ; Function to check possible RBS from the given strings ; Stores the values { sum , min_prefix } ; Iterate over the range ; Stores the total sum ; Stores the minimum prefix ; Check for minimum prefix ; Store these values in vector ; Make two pair vectors pos and neg ; Store values according to the mentioned approach ; Sort the positive vector ; Stores the extra count of open brackets ; No valid bracket sequence can be formed ; Sort the negative vector ; Stores the count of the negative elements ; No valid bracket sequence can be formed ; Check if open is equal to negative ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int checkRBS ( vector < string > S ) { int N = S . size ( ) ; vector < pair < int , int > > v ( N ) ; for ( int i = 0 ; i < N ; ++ i ) { string s = S [ i ] ; int sum = 0 ; int pre = 0 ; for ( char c : s ) { if ( c == ' ( ' ) { ++ sum ; } else { -- sum ; } pre = min ( sum , pre ) ; } v [ i ] = { sum , pre } ; } vector < pair < int , int > > pos ; vector < pair < int , int > > neg ; for ( int i = 0 ; i < N ; ++ i ) { if ( v [ i ] . first >= 0 ) { pos . push_back ( { - v [ i ] . second , v [ i ] . first } ) ; } else { neg . push_back ( { v [ i ] . first - v [ i ] . second , - v [ i ] . first } ) ; } } sort ( pos . begin ( ) , pos . end ( ) ) ; int open = 0 ; for ( auto p : pos ) { if ( open - p . first >= 0 ) { open += p . second ; } else { cout << " No " << " STRNEWLINE " ; return 0 ; } } sort ( neg . begin ( ) , neg . end ( ) ) ; int negative = 0 ; for ( auto p : neg ) { if ( negative - p . first >= 0 ) { negative += p . second ; } else { cout << " No STRNEWLINE " ; return 0 ; } } if ( open != negative ) { cout << " No STRNEWLINE " ; return 0 ; } cout << " Yes STRNEWLINE " ; return 0 ; } int main ( ) { vector < string > arr = { " ) " , " ( ) ( " } ; checkRBS ( arr ) ; return 0 ; }
Maximize count of unique Squares that can be formed with N arbitrary points in coordinate plane | C ++ program for the above approach ; Function to find the maximum number of unique squares that can be formed from the given N points ; Stores the resultant count of squares formed ; Base Case ; Subtract the maximum possible grid size as sqrt ( N ) ; Find the total squares till now for the maximum grid ; A i * i grid contains ( i - 1 ) * ( i - 1 ) + ( i - 2 ) * ( i - 2 ) + ... + 1 * 1 squares ; When N >= len then more squares will be counted ; Return total count of squares ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int maximumUniqueSquares ( int N ) { int ans = 0 ; if ( N < 4 ) { return 0 ; } int len = ( sqrt ( double ( N ) ) ) ; N -= len * len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i * i ; } if ( N >= len ) { N -= len ; for ( int i = 1 ; i < len ; i ++ ) { ans += i ; } } for ( int i = 1 ; i < N ; i ++ ) { ans += i ; } return ans ; } int main ( ) { int N = 9 ; cout << maximumUniqueSquares ( N ) ; return 0 ; }
Lexicographically smallest permutation number up to K having given array as a subsequence | C ++ program for the above approach ; Function to find the lexicographically smallest permutation such that the given array is a subsequence of it ; Stores the missing elements in arr in the range [ 1 , K ] ; Stores if the ith element is present in arr or not ; Loop to mark all integers present in the array as visited ; Loop to insert all the integers not visited into missing ; Append INT_MAX at end in order to prevent going out of bounds ; Pointer to the current element ; Pointer to the missing element ; Stores the required permutation ; Loop to construct the permutation using greedy approach ; If missing element is smaller that the current element insert missing element ; Insert current element ; Print the required Permutation ; Driver Code ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPermutation ( int K , vector < int > arr ) { vector < int > missing ; vector < bool > visited ( K + 1 , 0 ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { visited [ arr [ i ] ] = 1 ; } for ( int i = 1 ; i <= K ; i ++ ) { if ( ! visited [ i ] ) { missing . push_back ( i ) ; } } arr . push_back ( INT_MAX ) ; missing . push_back ( INT_MAX ) ; int p1 = 0 ; int p2 = 0 ; vector < int > ans ; while ( ans . size ( ) < K ) { if ( arr [ p1 ] < missing [ p2 ] ) { ans . push_back ( arr [ p1 ] ) ; p1 ++ ; } else { ans . push_back ( missing [ p2 ] ) ; p2 ++ ; } } for ( int i = 0 ; i < K ; i ++ ) { cout << ans [ i ] << " ▁ " ; } } int main ( ) { int K = 7 ; vector < int > arr = { 6 , 4 , 2 , 1 } ; findPermutation ( K , arr ) ; return 0 ; }
Last element remaining after repeated removal of Array elements at perfect square indices | C ++ program for the above approach ; Function to find last remaining index after repeated removal of perfect square indices ; Initialize the ans variable as N ; Iterate a while loop and discard the possible values ; Total discarded values ; Check if this forms a perfect square ; Decrease answer by 1 ; Subtract the value from the current value of N ; Return the value remained ; Function to find last remaining element after repeated removal of array element at perfect square indices ; Find the remaining index ; Print the element at that index as the result ; Driver Code ; Given input
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findRemainingIndex ( int N ) { int ans = N ; while ( N > 1 ) { int discard = int ( sqrt ( N ) ) ; if ( discard * discard == N ) { ans -- ; } N -= discard ; } return ans ; } void findRemainingElement ( int arr [ ] , int N ) { int remainingIndex = findRemainingIndex ( N ) ; cout << arr [ remainingIndex - 1 ] ; } signed main ( ) { int arr [ ] = { 2 , 3 , 4 , 4 , 2 , 4 , -3 , 1 , 1 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findRemainingElement ( arr , N ) ; return 0 ; }
Minimum time to reach from Node 1 to N if travel is allowed only when node is Green | Import library for Queue ; Function to find min edge ; Initialize queue and push [ 1 , 0 ] ; Create visited array to keep the track if node is visited or not ; Run infinite loop ; If node is N , terminate loop ; Travel adjacent nodes of crnt ; Check whether we visited previously or not ; Push into queue and make as true ; Function to Find time required to reach 1 to N ; Check color of node is red or green ; Add C sec from next green color time ; Add C ; Return the answer ; Driver Code ; Given Input ; Function Call ; Print total time
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minEdge ( vector < vector < int > > X , int N , int M , int T , int C ) { queue < pair < int , int > > Q ; Q . push ( { 1 , 0 } ) ; vector < int > V ( N + 1 , false ) ; int crnt , c ; while ( 1 ) { crnt = Q . front ( ) . first ; c = Q . front ( ) . second ; Q . pop ( ) ; if ( crnt == N ) break ; for ( int _ : X [ crnt ] ) { if ( ! V [ _ ] ) { Q . push ( { _ , c + 1 } ) ; V [ _ ] = true ; } } } return c ; } int findTime ( int T , int C , int c ) { int ans = 0 ; for ( int _ = 0 ; _ < c ; _ ++ ) { if ( ( ans / T ) % 2 ) ans = ( ans / T + 1 ) * T + C ; else ans += C ; } return ans ; } int main ( ) { int N = 5 ; int M = 5 ; int T = 3 ; int C = 5 ; vector < vector < int > > X { { } , { 2 , 3 , 4 } , { 4 , 5 } , { 1 } , { 1 , 2 } , { 2 } } ; int c = minEdge ( X , N , M , T , C ) ; int ans = findTime ( T , C , c ) ; cout << ( ans ) << endl ; return 0 ; }
Lexicographically largest possible by merging two strings by adding one character at a time | C ++ program for the approach ; Recursive bfunction for finding the lexicographically largest string ; If either of the string length is 0 , return the other string ; If s1 is lexicographically larger than s2 ; Take first character of s1 and call the function ; Take first character of s2 and recursively call function for remaining string ; Driver Code ; Given Input ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; string largestMerge ( string s1 , string s2 ) { if ( s1 . size ( ) == 0 || s2 . size ( ) == 0 ) return s1 + s2 ; if ( s1 > s2 ) { return s1 [ 0 ] + largestMerge ( s1 . substr ( 1 ) , s2 ) ; } return s2 [ 0 ] + largestMerge ( s1 , s2 . substr ( 1 ) ) ; } int main ( ) { string s1 = " geeks " ; string s2 = " forgeeks " ; cout << largestMerge ( s1 , s2 ) << endl ; return 0 ; }
Number of open doors | TCS Coding Question | C ++ program for the above approach ; Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countOpenDoors ( int N ) { int doorsOpen = sqrt ( N ) ; return doorsOpen ; } int main ( ) { int N = 100 ; cout << countOpenDoors ( N ) ; return 0 ; }
Minimum number of candies required to distribute among children based on given conditions | C ++ program for the above approach ; Function to count the minimum number of candies that is to be distributed ; Stores total count of candies ; Stores the amount of candies allocated to a student ; If the value of N is 1 ; Initialize with 1 all array element ; Traverse the array arr [ ] ; If arr [ i + 1 ] is greater than arr [ i ] and ans [ i + 1 ] is at most ans [ i ] ; Assign ans [ i ] + 1 to ans [ i + 1 ] ; Iterate until i is atleast 0 ; If arr [ i ] is greater than arr [ i + 1 ] and ans [ i ] is at most ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; If arr [ i ] is equals arr [ i + 1 ] and ans [ i ] is less than ans [ i + 1 ] ; Assign ans [ i + 1 ] + 1 to ans [ i ] ; Increment the sum by ans [ i ] ; Return the resultant sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countCandies ( int arr [ ] , int n ) { int sum = 0 ; int ans [ n ] ; if ( n == 1 ) { return 1 ; } for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i + 1 ] > arr [ i ] && ans [ i + 1 ] <= ans [ i ] ) { ans [ i + 1 ] = ans [ i ] + 1 ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] && ans [ i ] <= ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } else if ( arr [ i ] == arr [ i + 1 ] && ans [ i ] < ans [ i + 1 ] ) { ans [ i ] = ans [ i + 1 ] + 1 ; } sum += ans [ i ] ; } sum += ans [ n - 1 ] ; return sum ; } int main ( ) { int arr [ ] = { 1 , 0 , 2 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << countCandies ( arr , N ) ; return 0 ; }
Maximize profit possible by selling M products such that profit of a product is the number of products left of that supplier | C ++ program for the above approach ; Function to find the maximum profit by selling M number of products ; Initialize a Max - Heap to keep track of the maximum value ; Stores the maximum profit ; Traverse the array and push all the elements in max_heap ; Iterate a loop until M > 0 ; Decrement the value of M by 1 ; Pop the maximum element from the heap ; Update the maxProfit ; Push ( X - 1 ) to max heap ; Print the result ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaximumProfit ( int arr [ ] , int M , int N ) { priority_queue < int > max_heap ; int maxProfit = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_heap . push ( arr [ i ] ) ; while ( M > 0 ) { M -- ; int X = max_heap . top ( ) ; max_heap . pop ( ) ; maxProfit += X ; max_heap . push ( X - 1 ) ; } cout << maxProfit ; } int main ( ) { int arr [ ] = { 4 , 6 } ; int M = 4 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaximumProfit ( arr , M , N ) ; }
Generate an array with K positive numbers such that arr [ i ] is either | C ++ program for the above approach ; Function to generate the resultant sequence of first N natural numbers ; Initialize an array of size N ; Stores the sum of positive and negative elements ; Mark the first N - K elements as negative ; Update the value of arr [ i ] ; Update the value of sumNeg ; Mark the remaining K elements as positive ; Update the value of arr [ i ] ; Update the value of sumPos ; If the sum of the sequence is negative , then print - 1 ; Print the required sequence ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findSequence ( int n , int k ) { int arr [ n ] ; int sumPos = 0 , sumNeg = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { arr [ i ] = - ( i + 1 ) ; sumNeg += arr [ i ] ; } for ( int i = n - k ; i < n ; i ++ ) { arr [ i ] = i + 1 ; sumPos += arr [ i ] ; } if ( abs ( sumNeg ) >= sumPos ) { cout << -1 ; return ; } for ( int i = 0 ; i < n ; i ++ ) cout << arr [ i ] << " ▁ " ; } int main ( ) { int N = 10 , K = 6 ; findSequence ( N , K ) ; return 0 ; }
Print the indices for every row of a grid from which escaping from the grid is possible | C ++ program for the above approach ; Function to find the row index for every row of a matrix from which one can exit the grid after entering from left ; Iterate over the range [ 0 , M - 1 ] ; Stores the direction from which a person enters a cell ; Row index from which one enters the grid ; Column index from which one enters the grid ; Iterate until j is atleast N - 1 ; If Mat [ i ] [ j ] is equal to 1 ; If entry is from left cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from upper cell ; Decrement j by 1 ; Assign ' R ' to dir ; If entry is from right cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from bottom cell ; Increment j by 1 ; Assign ' L ' to dir ; Otherwise , ; If entry is from left cell ; Increment i by 1 ; Assign ' U ' to dir ; If entry is from upper cell ; Increment j by 1 ; Assign ' L ' to dir ; If entry is from right cell ; Decrement i by 1 ; Assign ' D ' to dir ; If entry is from lower cell ; Decrement j by 1 ; Assign ' R ' to dir ; If i or j is less than 0 or i is equal to M or j is equal to N ; If j is equal to N ; Otherwise ; Driver Code ; Input ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findPath ( vector < vector < int > > & arr , int M , int N ) { for ( int row = 0 ; row < M ; row ++ ) { char dir = ' L ' ; int i = row ; int j = 0 ; while ( j < N ) { if ( arr [ i ] [ j ] == 1 ) { if ( dir == ' L ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' U ' ) { j -- ; dir = ' R ' ; } else if ( dir == ' R ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' D ' ) { j ++ ; dir = ' L ' ; } } else { if ( dir == ' L ' ) { i ++ ; dir = ' U ' ; } else if ( dir == ' U ' ) { j ++ ; dir = ' L ' ; } else if ( dir == ' R ' ) { i -- ; dir = ' D ' ; } else if ( dir == ' D ' ) { j -- ; dir = ' R ' ; } } if ( i < 0 i == M j < 0 j == N ) break ; } if ( j == N ) cout << i << " ▁ " ; else cout << -1 << " ▁ " ; } } int main ( ) { vector < vector < int > > arr = { { 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 } } ; int M = arr . size ( ) ; int N = arr [ 0 ] . size ( ) ; findPath ( arr , M , N ) ; }