codeparrot/xlcost-text-to-code huggingface dataset:
Substring Sort | C # Program to sort substrings ; sort the input array ; repeated length should be the same string ; two different strings with the same length input array cannot be sorted ; validate that each string is a substring of the following one ; get first element ; The array is valid and sorted print the strings in order ; Test 1 ; Test 2
using System ; class GFG { public static void substringSort ( String [ ] arr , int n , int maxLen ) { int [ ] count = new int [ maxLen ] ; String [ ] sortedArr = new String [ maxLen ] ; for ( int i = 0 ; i < maxLen ; i ++ ) { count [ i ] = 0 ; sortedArr [ i ] = " " ; } for ( int i = 0 ; i < n ; i ++ ) { String s = arr [ i ] ; int len = s . Length ; if ( count [ len - 1 ] == 0 ) { sortedArr [ len - 1 ] = s ; count [ len - 1 ] = 1 ; } else if ( ReferenceEquals ( s , sortedArr [ len - 1 ] ) ) { count [ len - 1 ] ++ ; } else { Console . WriteLine ( " Cannot ▁ be ▁ sorted " ) ; return ; } } int index = 0 ; while ( count [ index ] == 0 ) index ++ ; int prev = index ; String prevString = sortedArr [ prev ] ; index ++ ; for ( ; index < maxLen ; index ++ ) { if ( count [ index ] != 0 ) { String current = sortedArr [ index ] ; if ( current . Contains ( prevString ) ) { prev = index ; prevString = current ; } else { Console . WriteLine ( " Cannot ▁ be ▁ sorted " ) ; return ; } } } for ( int i = 0 ; i < maxLen ; i ++ ) { String s = sortedArr [ i ] ; for ( int j = 0 ; j < count [ i ] ; j ++ ) { Console . WriteLine ( s ) ; } } } public static void Main ( String [ ] args ) { int maxLen = 100 ; String [ ] arr1 = { " d " , " zddsaaz " , " ds " , " ddsaa " , " dds " , " dds " } ; substringSort ( arr1 , arr1 . Length , maxLen ) ; String [ ] arr2 = { " for " , " rof " } ; substringSort ( arr2 , arr2 . Length , maxLen ) ; } }
Print all the pairs that contains the positive and negative values of an element | C # program to find pairs of positive and negative values present in an array . ; Function ; Sort the array ; Traverse the array ; For every arr [ i ] < 0 element , do a binary search for arr [ i ] > 0. ; If found , print the pair . ; Driver code
using System ; public class GFG { static void printPairs ( int [ ] arr , int n ) { bool pair_exists = false ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( Array . BinarySearch ( arr , - arr [ i ] ) != - 1 ) { Console . WriteLine ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } else break ; } if ( pair_exists == false ) Console . WriteLine ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void Main ( ) { int [ ] arr = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . Length ; printPairs ( arr , n ) ; } }
Multiset Equivalence Problem | C # program to check if two given multisets are equivalent ; sort the elements of both multisets ; Return true if both multisets are same . ; Driver code
using System ; using System . Collections . Generic ; class GFG { static bool areSame ( List < int > a , List < int > b ) { a . Sort ( ) ; b . Sort ( ) ; return ( a == b ) ; } public static void Main ( ) { List < int > a = new List < int > { 7 , 7 , 5 } ; List < int > b = new List < int > { 7 , 5 , 5 } ; if ( areSame ( a , b ) ) Console . WriteLine ( " Yes STRNEWLINE " ) ; else Console . WriteLine ( " No STRNEWLINE " ) ; } }
Minimum number of edges between two vertices of a Graph | C # program to find minimum edge between given two vertex of Graph ; Method for finding minimum no . of edge using BFS ; visited [ n ] for keeping track of visited node in BFS ; Initialize distances as 0 ; queue to do BFS . ; Update distance for i ; Method for addition of edge ; Driver code ; To store adjacency list of graph
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int minEdgeBFS ( ArrayList [ ] edges , int u , int v , int n ) { ArrayList visited = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { visited . Add ( false ) ; } ArrayList distance = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { distance . Add ( 0 ) ; } Queue Q = new Queue ( ) ; distance [ u ] = 0 ; Q . Enqueue ( u ) ; visited [ u ] = true ; while ( Q . Count != 0 ) { int x = ( int ) Q . Dequeue ( ) ; for ( int i = 0 ; i < edges [ x ] . Count ; i ++ ) { if ( ( bool ) visited [ ( int ) edges [ x ] [ i ] ] ) continue ; distance [ ( int ) edges [ x ] [ i ] ] = ( int ) distance [ x ] + 1 ; Q . Enqueue ( ( int ) edges [ x ] [ i ] ) ; visited [ ( int ) edges [ x ] [ i ] ] = true ; } } return ( int ) distance [ v ] ; } static void addEdge ( ArrayList [ ] edges , int u , int v ) { edges [ u ] . Add ( v ) ; edges [ v ] . Add ( u ) ; } public static void Main ( string [ ] args ) { int n = 9 ; ArrayList [ ] edges = new ArrayList [ 9 ] ; for ( int i = 0 ; i < 9 ; i ++ ) { edges [ i ] = new ArrayList ( ) ; } addEdge ( edges , 0 , 1 ) ; addEdge ( edges , 0 , 7 ) ; addEdge ( edges , 1 , 7 ) ; addEdge ( edges , 1 , 2 ) ; addEdge ( edges , 2 , 3 ) ; addEdge ( edges , 2 , 5 ) ; addEdge ( edges , 2 , 8 ) ; addEdge ( edges , 3 , 4 ) ; addEdge ( edges , 3 , 5 ) ; addEdge ( edges , 4 , 5 ) ; addEdge ( edges , 5 , 6 ) ; addEdge ( edges , 6 , 7 ) ; addEdge ( edges , 7 , 8 ) ; int u = 0 ; int v = 5 ; Console . Write ( minEdgeBFS ( edges , u , v , n ) ) ; } }
Minimum swaps so that binary search can be applied | C # program to find Minimum number of swaps to get the position of the element if we provide an unsorted array to binary search . ; Function to find minimum swaps . ; Here we are getting number of smaller and greater elements of k . ; Here we are calculating the actual position of k in the array . ; Implementing binary search as per the above - discussed cases . ; If we find the k . ; If we need minimum element swap . ; Else the element is at the right position . ; If we need maximum element swap . ; Else element is at the right position ; Calculating the required swaps . ; If it is impossible . ; Driver function
using System ; public class GFG { static int findMinimumSwaps ( int [ ] arr , int n , int k ) { int pos = 0 , num_min , num_max , need_minimum , need_maximum , swaps ; num_min = num_max = need_minimum = 0 ; need_maximum = swaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < k ) { num_min ++ ; } else if ( arr [ i ] > k ) { num_max ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == k ) { pos = i ; break ; } } int left , right , mid ; left = 0 ; right = n - 1 ; while ( left <= right ) { mid = ( left + right ) / 2 ; if ( arr [ mid ] == k ) { break ; } else if ( arr [ mid ] > k ) { if ( pos > mid ) { need_minimum ++ ; } else { num_min -- ; } left = mid + 1 ; } else { if ( pos < mid ) { need_maximum ++ ; } else { num_max -- ; } right = mid - 1 ; } } if ( need_minimum > need_maximum ) { swaps = swaps + need_maximum ; num_min = num_min - need_maximum ; need_minimum = need_minimum - need_maximum ; need_maximum = 0 ; } else { swaps = swaps + need_minimum ; num_max = num_max - need_minimum ; need_maximum = need_maximum - need_minimum ; need_minimum = 0 ; } if ( need_maximum > num_max need_minimum > num_min ) { return - 1 ; } else { return ( swaps + need_maximum + need_minimum ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 10 , 6 , 7 , 2 , 5 , 4 } ; int k = 4 ; int n = arr . Length ; Console . WriteLine ( findMinimumSwaps ( arr , n , k ) ) ; } }
Stable sort for descending order | C # program to demonstrate descending order ; Driver code
using System ; class GFG { static void reverse ( int [ ] a ) { int i , k , n = a . Length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . Length ; Array . Sort ( arr ) ; reverse ( arr ) ; Console . WriteLine ( " Array ▁ after ▁ sorting ▁ : ▁ STRNEWLINE " ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Check if both halves of the string have at least one different character | C # implementation to check if both halves of the string have at least one different character ; Function which break string into two halves Sorts the two halves separately Compares the two halves return true if any index has non - zero value ; Declaration and initialization of counter array ; Driver code
using System ; class GFG { static Boolean function ( char [ ] str ) { int l = str . Length ; Array . Sort ( str , 0 , ( l / 2 ) ) ; Array . Sort ( str , ( l / 2 ) , l - ( l / 2 ) ) ; for ( int i = 0 ; i < l / 2 ; i ++ ) if ( str [ i ] != str [ l / 2 + i ] ) return true ; return false ; } public static void Main ( String [ ] args ) { char [ ] str = ( " abcasdsabcae " ) . ToCharArray ( ) ; if ( function ( str ) ) Console . WriteLine ( " Yes , ▁ both ▁ halves ▁ differ " + " ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else Console . WriteLine ( " No , ▁ both ▁ halves ▁ do " + " ▁ not ▁ differ ▁ at ▁ all " ) ; } }
Sort an array of 0 s , 1 s and 2 s ( Simple Counting ) | Example input = [ 0 , 1 , 2 , 2 , 0 , 0 ] output = [ 0 , 0 , 0 , 1 , 2 , 2 ]
using System ; using System . Collections . Generic ; class GFG { static int [ ] inputArray = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; static List < int > outputArray = new List < int > ( ) ; static int indexOfOne = 0 ; static void print ( ) { foreach ( int item in inputArray ) if ( item == 2 ) outputArray . Add ( item ) ; else if ( item == 1 ) { outputArray . Insert ( indexOfOne , item ) ; indexOfOne += 1 ; } else if ( item == 0 ) { outputArray . Insert ( 0 , item ) ; indexOfOne += 1 ; } else { Console . WriteLine ( " ▁ wrong ▁ value ▁ - ▁ Aborting ▁ " ) ; continue ; } } public static void Main ( String [ ] args ) { print ( ) ; foreach ( int item in outputArray ) Console . Write ( item + " , ▁ " ) ; } }
Number of visible boxes after putting one inside another | C # program to count number of visible boxes . ; return the minimum number of visible boxes ; New Queue of integers . ; sorting the array ; traversing the array ; checking if current element is greater than or equal to twice of front element ; Pushing each element of array ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int minimumBox ( int [ ] arr , int n ) { Queue < int > q = new Queue < int > ( ) ; Array . Sort ( arr ) ; q . Enqueue ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . Peek ( ) ; if ( arr [ i ] >= 2 * now ) q . Dequeue ( ) ; q . Enqueue ( arr [ i ] ) ; } return q . Count ; } public static void Main ( ) { int [ ] arr = { 4 , 1 , 2 , 8 } ; int n = arr . Length ; Console . WriteLine ( minimumBox ( arr , n ) ) ; } }
Sort a binary array using one traversal | C # Code for Sort a binary array using one traversal ; if number is smaller than 1 then swap it with j - th number ; Driver program to test above function
using System ; class GFG { static void sortBinaryArray ( int [ ] a , int n ) { int j = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 ) { j ++ ; int temp = a [ j ] ; a [ j ] = a [ i ] ; a [ i ] = temp ; } } } public static void Main ( ) { int [ ] a = { 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 } ; int n = a . Length ; sortBinaryArray ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } }
Smallest element in an array that is repeated exactly ' k ' times . | C # program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Since arr [ ] has numbers in range from 1 to MAX ; set count to 1 as number is present once ; If frequency of number is equal to ' k ' ; Driver code
using System ; public class GFG { static int MAX = 1000 ; static int findDuplicate ( int [ ] arr , int n , int k ) { int res = MAX + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) count += 1 ; if ( count == k ) res = Math . Min ( res , arr [ i ] ) ; } } return res ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findDuplicate ( arr , n , k ) ) ; } }
Smallest element in an array that is repeated exactly ' k ' times . | C # program to find smallest number in array that is repeated exactly ' k ' times . ; finds the smallest number in arr [ ] that is repeated k times ; Sort the array ; Find the first element with exactly k occurrences . ; Driver code
using System ; public class GFG { static int findDuplicate ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int i = 0 ; while ( i < n ) { int j , count = 1 ; for ( j = i + 1 ; j < n && arr [ j ] == arr [ i ] ; j ++ ) count ++ ; if ( count == k ) return arr [ i ] ; i = j ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; int n = arr . Length ; Console . WriteLine ( findDuplicate ( arr , n , k ) ) ; } }
Longest Common Prefix using Sorting | C # program to find longest common prefix of given array of words . ; if size is 0 , return empty string ; sort the array of strings ; find the minimum length from first and last string ; find the common prefix between the first and last string ; Driver Function to test other function
using System ; public class GFG { static string longestCommonPrefix ( String [ ] a ) { int size = a . Length ; if ( size == 0 ) return " " ; if ( size == 1 ) return a [ 0 ] ; Array . Sort ( a ) ; int end = Math . Min ( a [ 0 ] . Length , a [ size - 1 ] . Length ) ; int i = 0 ; while ( i < end && a [ 0 ] [ i ] == a [ size - 1 ] [ i ] ) i ++ ; string pre = a [ 0 ] . Substring ( 0 , i ) ; return pre ; } public static void Main ( ) { string [ ] input = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; Console . WriteLine ( " The ▁ longest ▁ Common " + " ▁ Prefix ▁ is ▁ : ▁ " + longestCommonPrefix ( input ) ) ; } }
Find the minimum and maximum amount to buy all N candies | C # implementation to find the minimum and maximum amount ; Function to find the maximum and the minimum cost required ; Sort the array ; Print the minimum cost ; Print the maximum cost ; Driver code ; Function call
using System ; class GFG { static void find ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; int b = ( int ) Math . Ceiling ( n / k * 1.0 ) ; int min_sum = 0 , max_sum = 0 ; for ( int i = 0 ; i < b ; i ++ ) min_sum += arr [ i ] ; for ( int i = 2 ; i < arr . Length ; i ++ ) max_sum += arr [ i ] ; Console . WriteLine ( " minimum ▁ " + min_sum ) ; Console . WriteLine ( " maximum ▁ " + max_sum ) ; } public static void Main ( ) { int [ ] arr = { 3 , 2 , 1 , 4 } ; int n = arr . Length ; int k = 2 ; find ( arr , n , k ) ; } }
Check if it is possible to sort an array with conditional swapping of adjacent allowed | C # program to check if we can sort an array with adjacent swaps allowed ; Returns true if it is possible to sort else false ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver function
using System ; class GFG { static bool checkForSorting ( int [ ] arr , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { else return false ; } } return true ; } public static void Main ( ) { int [ ] arr = { 1 , 0 , 3 , 2 } ; int n = arr . Length ; if ( checkForSorting ( arr , n ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Sort an array of strings according to string lengths | C # program to sort an Array of Strings according to their lengths ; Function to Sort the array of string according to lengths . This function implements Insertion Sort . ; Insert s [ j ] at its correct position ; Function to print the sorted array of string ; Driver function ; Function to perform sorting ; Calling the function to print result
using System ; public class solution { static void sort ( String [ ] s , int n ) { for ( int i = 1 ; i < n ; i ++ ) { String temp = s [ i ] ; int j = i - 1 ; while ( j >= 0 && temp . Length < s [ j ] . Length ) { s [ j + 1 ] = s [ j ] ; j -- ; } s [ j + 1 ] = temp ; } } static void printArraystring ( String [ ] str , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( str [ i ] + " ▁ " ) ; } public static void Main ( ) { String [ ] arr = { " GeeksforGeeks " , " I " , " from " , " am " } ; int n = arr . Length ; sort ( arr , n ) ; printArraystring ( arr , n ) ; } }
Hoare 's vs Lomuto partition scheme in QuickSort | C # implementation of QuickSort using Hoare 's partition scheme ; This function takes first element as pivot , and places all the elements smaller than the pivot on the left side and all the elements greater than the pivot on the right side . It returns the index of the last element on the smaller side ; Find leftmost element greater than or equal to pivot ; Find rightmost element smaller than or equal to pivot ; If two pointers met . ; The main function that implements QuickSort arr [ ] -- > Array to be sorted , low -- > Starting index , high -- > Ending index ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition ; Function to print an array ; Driver Code
using System ; class GFG { static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ low ] ; int i = low - 1 , j = high + 1 ; while ( true ) { do { i ++ ; } while ( arr [ i ] < pivot ) ; do { j -- ; } while ( arr [ j ] > pivot ) ; if ( i >= j ) return j ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } static void quickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi ) ; quickSort ( arr , pi + 1 , high ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( " ▁ " + arr [ i ] ) ; Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] arr = { 10 , 7 , 8 , 9 , 1 , 5 } ; int n = arr . Length ; quickSort ( arr , 0 , n - 1 ) ; Console . WriteLine ( " Sorted ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
K | C # program to find the Kth smallest element after removing some integer from first n natural number . ; Return the K - th smallest element . ; sort ( arr , arr + n ) ; ; Checking if k lies before 1 st element ; If k is the first element of array arr [ ] . ; If k is more than last element ; If first element of array is 1. ; Reducing k by numbers before arr [ 0 ] . ; Finding k 'th smallest  element after removing  array elements. ; Finding count of element between i - th and ( i - 1 ) - th element . ; Driver Code
using System ; class GFG { static int ksmallest ( int [ ] arr , int n , int k ) { Array . Sort ( arr ) ; if ( k < arr [ 0 ] ) return k ; if ( k == arr [ 0 ] ) return arr [ 0 ] + 1 ; if ( k > arr [ n - 1 ] ) return k + n ; if ( arr [ 0 ] == 1 ) k -- ; else k -= ( arr [ 0 ] - 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { int c = arr [ i ] - arr [ i - 1 ] - 1 ; if ( k <= c ) return arr [ i - 1 ] + k ; else k -= c ; } return arr [ n - 1 ] + k ; } static public void Main ( ) { int k = 1 ; int [ ] arr = { 1 } ; int n = arr . Length ; Console . WriteLine ( ksmallest ( arr , n , k ) ) ; } }
Tree Sort | C # program to implement Tree Sort ; Class containing left and right child of current node and key value ; Root of BST ; Constructor ; This method mainly calls insertRec ( ) ; A recursive function to insert a new key in BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; return the root ; A function to do inorder traversal of BST ; Driver Code
using System ; public class GFG { public class Node { public int key ; public Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } Node root ; GFG ( ) { root = null ; } void insert ( int key ) { root = insertRec ( root , key ) ; } Node insertRec ( Node root , int key ) { if ( root == null ) { root = new Node ( key ) ; return root ; } if ( key < root . key ) root . left = insertRec ( root . left , key ) ; else if ( key > root . key ) root . right = insertRec ( root . right , key ) ; return root ; } void inorderRec ( Node root ) { if ( root != null ) { inorderRec ( root . left ) ; Console . Write ( root . key + " ▁ " ) ; inorderRec ( root . right ) ; } } void treeins ( int [ ] arr ) { for ( int i = 0 ; i < arr . Length ; i ++ ) { insert ( arr [ i ] ) ; } } public static void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; int [ ] arr = { 5 , 4 , 7 , 2 , 11 } ; tree . treeins ( arr ) ; tree . inorderRec ( tree . root ) ; } }
Check whether a given number is even or odd | C # program 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
using System ; class GFG { public static bool isEven ( int n ) { if ( ( n & 1 ) == 0 ) return true ; else return false ; } public static void Main ( ) { int n = 101 ; if ( isEven ( n ) == true ) Console . WriteLine ( " Even " ) ; else Console . WriteLine ( " Odd " ) ; } }
Count distinct occurrences as a subsequence | 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 emptystring 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 ++ ) System . out . println ( mat [ i ] [ j ] + " ▁ " ) ; ; Driver code to check above method
using System ; class GFG { static int findSubsequenceCount ( string S , string T ) { int m = T . Length ; int n = S . Length ; if ( m > n ) return 0 ; int [ , ] mat = new int [ 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 ] ; } public static void Main ( ) { string T = " ge " ; string S = " geeksforgeeks " ; Console . WriteLine ( findSubsequenceCount ( S , T ) ) ; } }
QuickSort Tail Call Optimization ( Reducing worst case space to Log n ) | A Simple implementation of QuickSort that makes two recursive calls . ; pi is partitioning index , arr [ p ] is now at right place ; Separately sort elements before partition and after partition
static void quickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi - 1 ) ; quickSort ( arr , pi + 1 , high ) ; } }