codeparrot/xlcost-text-to-code huggingface dataset:
Longest Common Prefix using Word by Word Matching | C # Program to find the longest common prefix ; A Utility Function to find the common prefix between strings - str1 and str2 ; Compare str1 and str2 ; A Function that returns the longest common prefix from the array of strings ; Driver Code
using System ; class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { String result = " " ; int n1 = str1 . Length , n2 = str2 . Length ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 [ i ] != str2 [ j ] ) { break ; } result += str1 [ i ] ; } return ( result ) ; } static String commonPrefix ( String [ ] arr , int n ) { String prefix = arr [ 0 ] ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { prefix = commonPrefixUtil ( prefix , arr . GetValue ( i ) . ToString ( ) ) ; } return ( prefix ) ; } public static void Main ( ) { String [ ] arr = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . Length ; String ans = commonPrefix ( arr , n ) ; if ( ans . Length > 0 ) { Console . Write ( " The ▁ longest ▁ common ▁ " + " prefix ▁ is ▁ - ▁ " + ans ) ; } else { Console . Write ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; } } }
1 ' s ▁ and ▁ 2' s complement of a Binary Number | C # program to print 1 ' s ▁ and ▁ 2' s complement of a binary number ; Returns '0' for '1' and '1' for '0' ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; for two 's complement go from right to left in  ones complement and if we get 1 make, we make  them 0 and keep going left when we get first  0, make that 1 and go out of loop ; If No break : all are 1 as in 111 or 11111 ; in such case , add extra 1 at beginning ; Driver code
using System ; class GFG { static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static void printOneAndTwosComplement ( String bin ) { int n = bin . Length ; int i ; String ones = " " , twos = " " ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) { ones += flip ( bin [ i ] ) ; } twos = ones ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones [ i ] == '1' ) { twos = twos . Substring ( 0 , i ) + '0' + twos . Substring ( i + 1 , twos . Length - ( i + 1 ) ) ; } else { twos = twos . Substring ( 0 , i ) + '1' + twos . Substring ( i + 1 , twos . Length - ( i + 1 ) ) ; break ; } } if ( i == - 1 ) { twos = '1' + twos ; } Console . WriteLine ( "1 ' s ▁ complement : ▁ " + ones ) ; ; Console . WriteLine ( "2 ' s ▁ complement : ▁ " + twos ) ; } public static void Main ( String [ ] args ) { String bin = "1100" ; printOneAndTwosComplement ( bin ) ; } }
Print Concatenation of Zig | C # Program for above approach ; Function for zig - zag Concatenation ; Check is n is less or equal to 1 ; Iterate rowNum from 0 to n - 1 ; Iterate i till s . length ( ) - 1 ; Check is rowNum is 0 or n - 1 ; Driver Code
using System ; public class GFG { static string zigZagConcat ( string s , int n ) { if ( n <= 1 ) { return s ; } string result = " " ; for ( int rowNum = 0 ; rowNum < n ; rowNum ++ ) { int i = rowNum ; bool up = true ; while ( i < s . Length ) { result += s [ i ] ; if ( rowNum == 0 rowNum == n - 1 ) { i += ( 2 * n - 2 ) ; } else { if ( up ) { i += ( 2 * ( n - rowNum ) - 2 ) ; } else { i += rowNum * 2 ; } up ^= true ; } } } return result ; } static public void Main ( ) { string str = " GEEKSFORGEEKS " ; int n = 3 ; Console . WriteLine ( zigZagConcat ( str , n ) ) ; } }
Print string of odd length in ' X ' format | C # program to print cross pattern ; Function to print given string in cross pattern ; i and j are the indexes of characters to be displayed in the ith iteration i = 0 initially and go upto length of string j = length of string initially in each iteration of i , we increment i and decrement j , we print character only of k == i or k == j ; Driver code
using System ; class GFG { static void pattern ( String str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int j = len - 1 - i ; for ( int k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) Console . Write ( str [ k ] ) ; else Console . Write ( " ▁ " ) ; } Console . WriteLine ( " " ) ; } } public static void Main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . Length ; pattern ( str , len ) ; } }
Recursively print all sentences that can be formed from list of word lists | C # program to print all possible sentences from a list of word list ; A recursive function to print all possible sentences that can be formed from a list of word list ; Add current word to output array ; If this is last word of current output sentence , then print the output sentence ; Recur for next row ; A wrapper over printUtil ( ) ; Create an array to store sentence ; Consider all words for first row as starting points and print all sentences ; Driver program to test above functions
using System ; class GFG { static readonly int R = 3 ; static readonly int C = 3 ; static void printUtil ( String [ , ] arr , int m , int n , String [ ] output ) { output [ m ] = arr [ m , n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) Console . Write ( output [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 , i ] != " " && m < C ) printUtil ( arr , m + 1 , i , output ) ; } static void print ( String [ , ] arr ) { String [ ] output = new String [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 , i ] != " " ) printUtil ( arr , 0 , i , output ) ; } public static void Main ( String [ ] args ) { String [ , ] arr = { { " you " , " we " , " " } , { " have " , " are " , " " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; } }
Check if characters of a given string can be rearranged to form a palindrome | C # implementation to check if characters of a given string can be rearranged to form a palindrome ; function to check whether characters of a string can form a palindrome ; Create a count array and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Driver code
using System ; class GFG { static int NO_OF_CHARS = 256 ; static bool canFormPalindrome ( string str ) { int [ ] count = new int [ NO_OF_CHARS ] ; Array . Fill ( count , 0 ) ; for ( int i = 0 ; i < str . Length ; i ++ ) count [ ( int ) ( str [ i ] ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void Main ( ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Generate n | Java program to generate n - bit Gray codes ; This function generates all n bit Gray codes and prints the generated codes ; Base case ; Recursive case ; Append 0 to the first half ; Append 1 to the second half ; Function to generate the Gray code of N bits ; print contents of arr ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static List < String > generateGray ( int n ) { if ( n <= 0 ) { List < String > temp = new List < String > ( ) ; temp . Add ( "0" ) ; return temp ; } if ( n == 1 ) { List < String > temp = new List < String > ( ) ; temp . Add ( "0" ) ; temp . Add ( "1" ) ; return temp ; } List < String > recAns = generateGray ( n - 1 ) ; List < String > mainAns = new List < String > ( ) ; for ( int i = 0 ; i < recAns . Count ; i ++ ) { String s = recAns [ i ] ; mainAns . Add ( "0" + s ) ; } for ( int i = recAns . Count - 1 ; i >= 0 ; i -- ) { String s = recAns [ i ] ; mainAns . Add ( "1" + s ) ; } return mainAns ; } static void generateGrayarr ( int n ) { List < String > arr = new List < String > ( ) ; arr = generateGray ( n ) ; for ( int i = 0 ; i < arr . Count ; i ++ ) { Console . WriteLine ( arr [ i ] ) ; } } public static void Main ( String [ ] args ) { generateGrayarr ( 3 ) ; } }
Check whether two strings are anagram of each other | C # program to check if two strings are anagrams of each other ; function to check whether two strings are anagram of each other ; Create 2 count arrays and initialize all values as 0 ; For each character in input strings , increment count in the corresponding count array ; If both strings are of different length . Removing this condition will make the program fail for strings like " aaca " and " aca " ; Compare count arrays ; Driver code ; Function Call
using System ; public class GFG { static int NO_OF_CHARS = 256 ; static bool areAnagram ( char [ ] str1 , char [ ] str2 ) { int [ ] count1 = new int [ NO_OF_CHARS ] ; int [ ] count2 = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str1 . Length && i < str2 . Length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . Length != str2 . Length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void Main ( ) { char [ ] str1 = ( " geeksforgeeks " ) . ToCharArray ( ) ; char [ ] str2 = ( " forgeeksgeeks " ) . ToCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) Console . WriteLine ( " The ▁ two ▁ strings ▁ are " + " anagram ▁ of ▁ each ▁ other " ) ; else Console . WriteLine ( " The ▁ two ▁ strings ▁ are ▁ not " + " ▁ anagram ▁ of ▁ each ▁ other " ) ; } }
Find the smallest window in a string containing all characters of another string | C # program to find smallest window containing all characters of a pattern . ; Function to find smallest window containing all characters of ' pat ' ; Check if string ' s ▁ length ▁ is ▁ ▁ less ▁ than ▁ pattern ' s length . If yes then no such window can exist ; Store occurrence ofs characters of pattern ; Start traversing the string Count of characters ; Count occurrence of characters of string ; If string ' s ▁ char ▁ matches ▁ ▁ with ▁ pattern ' s char then increment count ; if all the characters are matched ; Try to minimize the window ; update window size ; If no window found ; Return substring starting from start_index and length min_len ; Driver Method
using System ; class GFG { static int no_of_chars = 256 ; static String findSubString ( String str , String pat ) { int len1 = str . Length ; int len2 = pat . Length ; if ( len1 < len2 ) { Console . WriteLine ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } int [ ] hash_pat = new int [ no_of_chars ] ; int [ ] hash_str = new int [ no_of_chars ] ; for ( int i = 0 ; i < len2 ; i ++ ) hash_pat [ pat [ i ] ] ++ ; int start = 0 , start_index = - 1 , min_len = int . MaxValue ; int count = 0 ; for ( int j = 0 ; j < len1 ; j ++ ) { hash_str [ str [ j ] ] ++ ; if ( hash_str [ str [ j ] ] <= hash_pat [ str [ j ] ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] hash_pat [ str [ start ] ] == 0 ) { if ( hash_str [ str [ start ] ] > hash_pat [ str [ start ] ] ) hash_str [ str [ start ] ] -- ; start ++ ; } int len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == - 1 ) { Console . WriteLine ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } return str . Substring ( start_index , min_len ) ; } public static void Main ( String [ ] args ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; String pat = " tist " ; Console . WriteLine ( " Smallest ▁ window ▁ is ▁ : STRNEWLINE ▁ " + findSubString ( str , pat ) ) ; } }
Remove duplicates from a given string | C # program to remove duplicate character from character array and print in sorted order ; Used as index in the modified string ; Traverse through all characters ; Check if str [ i ] is present before it ; If not present , then add it to result . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static String removeDuplicate ( char [ ] str , int n ) { int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) { if ( str [ i ] == str [ j ] ) { break ; } } if ( j == i ) { str [ index ++ ] = str [ i ] ; } } char [ ] ans = new char [ index ] ; Array . Copy ( str , ans , index ) ; return String . Join ( " " , ans ) ; } public static void Main ( String [ ] args ) { char [ ] str = " geeksforgeeks " . ToCharArray ( ) ; int n = str . Length ; Console . WriteLine ( removeDuplicate ( str , n ) ) ; } }
Check if it ’ s possible to completely fill every container with same ball | C # program for above approach ; A boolean function that returns true if it 's possible  to completely fill the container else return false ; Base Case ; Backtracking ; Function to check the conditions ; Storing frequencies ; Function Call for backtracking ; Driver Code ; Given Input ; Function Call
using System ; using System . Collections . Generic ; class GFG { static bool getans ( int i , List < int > v , int [ ] q ) { if ( i == q . Length ) return true ; for ( int j = 0 ; j < v . Count ; j ++ ) { if ( v [ j ] >= q [ i ] ) { v [ j ] -= q [ i ] ; if ( getans ( i + 1 , v , q ) ) { return true ; } v [ j ] += q [ i ] ; } } return false ; } static void Check ( int [ ] c , int [ ] b ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < b . Length ; i ++ ) m [ b [ i ] ] = 0 ; for ( int i = 0 ; i < b . Length ; i ++ ) { m [ b [ i ] ] ++ ; } List < int > v = new List < int > ( ) ; foreach ( KeyValuePair < int , int > i in m ) { v . Add ( i . Value ) ; } bool check = getans ( 0 , v , c ) ; if ( check ) Console . WriteLine ( " YES " ) ; else Console . WriteLine ( " NO " ) ; } public static void Main ( ) { int [ ] c = { 1 , 3 , 3 } ; int [ ] b = { 2 , 2 , 2 , 2 , 4 , 4 , 4 } ; Check ( c , b ) ; } }
Maximum Bitwise XOR of node values of an Acyclic Graph made up of N given vertices using M edges | C # program for the above approach ; Function to find the maximum Bitwise XOR of any subset of the array of size K ; Number of node must K + 1 for K edges ; Stores the maximum Bitwise XOR ; Generate all subsets of the array ; Finding number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update the maximum Bitwise XOR obtained so far ; Return the maximum XOR ; Driver code
using System ; using System . Linq ; class GFG { static int maximumXOR ( int [ ] arr , int n , int K ) { K ++ ; int maxXor = Int32 . MinValue ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( Convert . ToString ( i , 2 ) . Count ( c => c == '1' ) == K ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . Max ( maxXor , cur_xor ) ; } } return maxXor ; } static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int N = arr . Length ; int M = 2 ; Console . WriteLine ( maximumXOR ( arr , N , M ) ) ; } }
Tower of Hanoi | Set 2 | C # program for the above approach ; Function to print order of movement of N disks across three rods to place all disks on the third rod from the first - rod using binary representation ; Iterate over the range [ 0 , 2 ^ N - 1 ] ; Print the movement of the current rod ; Driver Code
using System ; class GFG { static void TowerOfHanoi ( int N ) { for ( int x = 1 ; x <= Math . Pow ( 2 , N ) - 1 ; x ++ ) { Console . Write ( " Move ▁ from ▁ Rod ▁ " + ( ( x & x - 1 ) % 3 + 1 ) + " ▁ to ▁ Rod ▁ " + ( ( ( x x - 1 ) + 1 ) % 3 + 1 ) + " STRNEWLINE " ) ; } } static void Main ( ) { int N = 3 ; TowerOfHanoi ( N ) ; } }
Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | C # program for the above approach ; Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver Code
using System ; class GFG { static bool isValid ( int [ , ] board , int i , int j , int K ) { if ( board [ i , j ] <= K ) { return true ; } return false ; } static bool findPath ( int [ , ] board , int X , int Y , int M , int N , int K ) { if ( X < 0 X == M Y < 0 Y == N ) { return true ; } if ( isValid ( board , X , Y , K ) ) { int board_XY = board [ X , Y ] ; board [ X , Y ] = int . MaxValue ; if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) || findPath ( board , X - 1 , Y , M , N , K - board_XY ) || findPath ( board , X , Y + 1 , M , N , K - board_XY ) || findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) { return true ; } board [ X , Y ] = board_XY ; } return false ; } public static void Main ( string [ ] args ) { int [ , ] grid = { { 25 , 5 , 25 , 25 , 25 , 25 } , { 25 , 1 , 1 , 5 , 12 , 25 } , { 25 , 1 , 12 , 0 , 15 , 25 } , { 22 , 1 , 11 , 2 , 19 , 15 } , { 25 , 2 , 2 , 1 , 12 , 15 } , { 25 , 9 , 10 , 1 , 11 , 25 } , { 25 , 25 , 25 , 25 , 25 , 25 } } ; int K = 17 ; int M = grid . Length ; int N = grid . GetLength ( 0 ) ; int X = 2 , Y = 3 ; if ( findPath ( grid , X , Y , M , N , K ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Sum of all subsets whose sum is a Perfect Number from a given array | C # program for the above approach ; Function to check is a given number is a perfect number or not ; Stores sum of divisors ; Add all divisors of x to sum_div ; If the sum of divisors is equal to the given number , return true ; Otherwise , return false ; Function to find the sum of all the subsets from an array whose sum is a perfect number ; Stores the total number of subsets , i . e . 2 ^ n ; Consider all numbers from 0 to 2 ^ n - 1 ; Consider array elements from positions of set bits in the binary representation of n ; If sum of chosen elements is a perfect number ; Driver Code
using System ; class GFG { static int isPerfect ( int x ) { int sum_div = 1 ; for ( int i = 2 ; i <= x / 2 ; ++ i ) { if ( x % i == 0 ) { sum_div += i ; } } if ( sum_div == x ) { return 1 ; } else return 0 ; } static void subsetSum ( int [ ] arr , int n ) { long total = 1 << n ; for ( long i = 0 ; i < total ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) sum += arr [ j ] ; if ( isPerfect ( sum ) != 0 ) { Console . Write ( sum + " ▁ " ) ; } } } static public void Main ( ) { int [ ] arr = { 5 , 4 , 6 } ; int N = arr . Length ; subsetSum ( arr , N ) ; } }
Pen Distribution Problem | C # implementation of the above approach ; Recursive function to play Game ; Box is empty , Game Over ! or Both have quit , Game Over ! ; P1 moves ; P2 moves ; Increment X ; Switch moves between P1 and P2 ; Function to find the number of pens remaining in the box and calculate score for each player ; Score of P1 ; Score of P2 ; Initialized to zero ; Move = 0 , P1 ' s ▁ turn ▁ ▁ Move ▁ = ▁ 1 , ▁ P2' s turn ; Has P1 quit ; Has P2 quit ; Recursively continue the game ; Driver Code
using System ; class GFG { static void solve ( int N , int P1 , int P2 , int X , int Move , bool QuitP1 , bool QuitP2 ) { if ( N == 0 || ( QuitP1 && QuitP2 ) ) { Console . WriteLine ( " Number ▁ of ▁ pens ▁ remaining " + " ▁ in ▁ the ▁ box : ▁ " + N ) ; Console . WriteLine ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P1 : ▁ " + P1 ) ; Console . WriteLine ( " Number ▁ of ▁ pens ▁ collected " + " ▁ by ▁ P2 : ▁ " + P2 ) ; return ; } if ( Move == 0 && QuitP1 == false ) { int req_P1 = ( int ) ( Math . Pow ( 2 , X ) ) ; if ( req_P1 <= N ) { P1 += req_P1 ; N -= req_P1 ; } else { QuitP1 = true ; } } else if ( Move == 1 && QuitP2 == false ) { int req_P2 = ( int ) ( Math . Pow ( 3 , X ) ) ; if ( req_P2 <= N ) { P2 += req_P2 ; N -= req_P2 ; } else { QuitP2 = true ; } } X ++ ; Move = ( ( Move == 1 ) ? 0 : 1 ) ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } static void PenGame ( int N ) { int P1 = 0 ; int P2 = 0 ; int X = 0 ; int Move = 0 ; bool QuitP1 = false ; bool QuitP2 = false ; solve ( N , P1 , P2 , X , Move , QuitP1 , QuitP2 ) ; } public static void Main ( ) { int N = 22 ; PenGame ( N ) ; } }
Generate all N digit numbers having absolute difference as K between adjacent digits | C # program for the above approach ; Function that recursively finds the possible numbers and append into ans ; Base Case ; Check the sum of last digit and k less than or equal to 9 or not ; If k == 0 , then subtraction and addition does not make any difference Hence , subtraction is skipped ; Function to call checkUntil function for every integer from 1 to 9 ; check_util function recursively store all numbers starting from i ; Function to print the all numbers which satisfy the conditions ; Driver Code ; Given N and K ; To store the result ; Function call ; Print Resultant Numbers
using System ; using System . Collections . Generic ; class GFG { static void checkUntil ( int num , int K , int N , List < int > ans ) { if ( N == 1 ) { ans . Add ( num ) ; return ; } if ( ( num % 10 + K ) <= 9 ) checkUntil ( 10 * num + ( num % 10 + K ) , K , N - 1 , ans ) ; if ( K > 0 ) { if ( ( num % 10 - K ) >= 0 ) checkUntil ( 10 * num + num % 10 - K , K , N - 1 , ans ) ; } } static void check ( int K , int N , List < int > ans ) { for ( int i = 1 ; i <= 9 ; i ++ ) { checkUntil ( i , K , N , ans ) ; } } static void print ( List < int > ans ) { for ( int i = 0 ; i < ans . Count ; i ++ ) { Console . Write ( ans [ i ] + " , ▁ " ) ; } } public static void Main ( String [ ] args ) { int N = 4 , K = 8 ; List < int > ans = new List < int > ( ) ; ; check ( K , N , ans ) ; print ( ans ) ; } }
Minimum Cost Path in a directed graph via given set of intermediate nodes | C # program to implement the above approach ; Stores minimum - cost of path from source ; Function to Perform BFS on graph g starting from vertex v ; If destination is reached ; Set flag to true ; Visit all the intermediate nodes ; If any intermediate node is not visited ; If all intermediate nodes are visited ; Update the minSum ; Mark the current node visited ; Traverse adjacent nodes ; Mark the neighbour visited ; Find minimum cost path considering the neighbour as the source ; Mark the neighbour unvisited ; Mark the source unvisited ; Driver code ; Stores the graph ; Number of nodes ; Source ; Destination ; Keeps a check on visited and unvisited nodes ; Stores intemediate nodes ; If no path is found
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { class pair { public int first , second ; public pair ( int f , int s ) { this . first = f ; this . second = s ; } } static int minSum = 100000000 ; static void getMinPathSum ( Dictionary < int , ArrayList > graph , bool [ ] visited , ArrayList necessary , int source , int dest , int currSum ) { if ( src == dest ) { bool flag = true ; foreach ( int i in necessary ) { if ( ! visited [ i ] ) { flag = false ; break ; } } if ( flag ) minSum = Math . Min ( minSum , currSum ) ; return ; } else { visited [ src ] = true ; foreach ( pair node in graph ) { if ( ! visited [ node . first ] ) { visited [ node . first ] = true ; getMinPathSum ( graph , visited , necessary , node . first , dest , currSum + node . second ) ; visited [ node . first ] = false ; } } visited [ src ] = false ; } } public static void Main ( string [ ] args ) { Dictionary < int , ArrayList > graph = new Dictionary < int , ArrayList > ( ) ; for ( int i = 0 ; i <= 6 ; i ++ ) graph [ i ] = new ArrayList ( ) ; graph [ 0 ] . Add ( new pair ( 1 , 2 ) ) ; graph [ 0 ] . Add ( new pair ( 2 , 3 ) ) ; graph [ 0 ] . Add ( new pair ( 3 , 2 ) ) ; graph [ 1 ] . Add ( new pair ( 4 , 4 ) ) ; graph [ 1 ] . Add ( new pair ( 0 , 1 ) ) ; graph [ 2 ] . Add ( new pair ( 4 , 5 ) ) ; graph [ 2 ] . Add ( new pair ( 5 , 6 ) ) ; graph [ 3 ] . Add ( new pair ( 5 , 7 ) ) ; graph [ 3 ] . Add ( new pair ( 0 , 1 ) ) ; graph [ 4 ] . Add ( new pair ( 6 , 4 ) ) ; graph [ 5 ] . Add ( new pair ( 4 , 2 ) ) ; graph [ 6 ] . Add ( new pair ( 7 , 11 ) ) ; int n = 7 ; int source = 0 ; int dest = 6 ; bool [ ] visited = new bool [ n ] ; ArrayList necessary = new ArrayList ( ) ; necessary . Add ( 2 ) ; necessary . Add ( 4 ) ; getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; if ( minSum == 100000000 ) Console . WriteLine ( - 1 ) ; else Console . WriteLine ( minSum ) ; } }