codeparrot/xlcost-text-to-code huggingface dataset:
Count subarrays with Prime sum | C # program to count subarrays with Prime sum ; Function to count subarrays with Prime sum ; USE SIEVE TO FIND ALL PRIME NUMBERS LESS THAN OR EQUAL TO max_val Create a boolean array " prime [ 0 . . n ] " . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; Remaining part of SIEVE ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Initialize result ; Traverse through the array ; return answer ; Driver program
class Solution { static int primeSubarrays ( int [ ] A , int n ) { int max_val = ( int ) ( System . Math . Pow ( 10 , 7 ) ) ; bool [ ] prime = new bool [ max_val + 1 ] ; for ( int p = 0 ; p < max_val + 1 ; p ++ ) prime [ p ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { int val = A [ i ] ; for ( int j = i + 1 ; j < n ; ++ j ) { val += A [ j ] ; if ( prime [ val ] ) ++ cnt ; } } return cnt ; } static void Main ( ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int n = A . Length ; System . Console . WriteLine ( primeSubarrays ( A , n ) ) ; } }
Total numbers with no repeated digits in a range | C # implementation of brute force solution . ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate
using System ; using System . Collections . Generic ; class GFG { static int repeated_digit ( int n ) { var s = new HashSet < int > ( ) ; while ( n != 0 ) { int d = n % 10 ; if ( s . Contains ( d ) ) { return 0 ; } s . Add ( d ) ; n = n / 10 ; } return 1 ; } static int calculate ( int L , int R ) { int answer = 0 ; for ( int i = L ; i < R + 1 ; ++ i ) { answer = answer + repeated_digit ( i ) ; } return answer ; } public static void Main ( String [ ] args ) { int L = 1 , R = 100 ; Console . WriteLine ( calculate ( L , R ) ) ; } }
Minimum swaps required to make a binary string alternating | C # implementation of the approach ; returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code
using System ; public class GFG { public static int countMinSwaps ( string st ) { int min_swaps = 0 ; int odd_0 = 0 , even_0 = 0 ; int odd_1 = 0 , even_1 = 0 ; int n = st . Length ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st [ i ] == '1' ) { even_1 ++ ; } else { even_0 ++ ; } } else { if ( st [ i ] == '1' ) { odd_1 ++ ; } else { odd_0 ++ ; } } } int cnt_swaps_1 = Math . Min ( even_0 , odd_1 ) ; int cnt_swaps_2 = Math . Min ( even_1 , odd_0 ) ; return Math . Min ( cnt_swaps_1 , cnt_swaps_2 ) ; } public static void Main ( string [ ] args ) { string st = "000111" ; Console . WriteLine ( countMinSwaps ( st ) ) ; } }
Total numbers with no repeated digits in a range | C # implementation of above idea ; Maximum ; Prefix Array ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to pre calculate the Prefix array ; Traversing through the numbers from 2 to MAX ; Generating the Prefix array ; Calclute Function ; Answer ; Driver Code ; Pre - calculating the Prefix array . ; Calling the calculate function to find the total number of number which has no repeated digit
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100 ; static List < int > Prefix = new List < int > ( ) ; static int repeated_digit ( int n ) { HashSet < int > a = new HashSet < int > ( ) ; int d ; while ( n != 0 ) { d = n % 10 ; if ( a . Contains ( d ) ) return 0 ; a . Add ( d ) ; n /= 10 ; } return 1 ; } static void pre_calculations ( ) { Prefix . Add ( 0 ) ; Prefix . Add ( repeated_digit ( 1 ) ) ; for ( int i = 2 ; i < MAX + 1 ; i ++ ) Prefix . Add ( repeated_digit ( i ) + Prefix [ i - 1 ] ) ; } static int calculate ( int L , int R ) { return Prefix [ R ] - Prefix [ L - 1 ] ; } public static void Main ( String [ ] args ) { int L = 1 , R = 100 ; pre_calculations ( ) ; Console . WriteLine ( calculate ( L , R ) ) ; } }
Difference Array | Range update query in O ( 1 ) | C # code to demonstrate Difference Array ; Creates a diff array D [ ] for A [ ] and returns it after filling initial values . ; We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; Does range update ; Prints updated Array ; Note that A [ 0 ] or D [ 0 ] decides values of rest of the elements . ; Driver Code ; Array to be updated ; Create and fill difference Array We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; After below update ( l , r , x ) , the elements should become 20 , 15 , 20 , 40 ; After below updates , the array should become 30 , 35 , 70 , 60
using System ; class GFG { static void initializeDiffArray ( int [ ] A , int [ ] D ) { int n = A . Length ; D [ 0 ] = A [ 0 ] ; D [ n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) D [ i ] = A [ i ] - A [ i - 1 ] ; } static void update ( int [ ] D , int l , int r , int x ) { D [ l ] += x ; D [ r + 1 ] -= x ; } static int printArray ( int [ ] A , int [ ] D ) { for ( int i = 0 ; i < A . Length ; i ++ ) { if ( i == 0 ) A [ i ] = D [ i ] ; else A [ i ] = D [ i ] + A [ i - 1 ] ; Console . Write ( A [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; return 0 ; } public static void Main ( ) { int [ ] A = { 10 , 5 , 20 , 40 } ; int n = A . Length ; int [ ] D = new int [ n + 1 ] ; initializeDiffArray ( A , D ) ; update ( D , 0 , 1 , 10 ) ; printArray ( A , D ) ; update ( D , 1 , 3 , 20 ) ; update ( D , 2 , 2 , 30 ) ; printArray ( A , D ) ; } }
Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | C # implementation of above approach ; computing ( a ^ b ) % c ; Driver code
using System ; class GFG { static long modpow ( long base1 , long exp , long modulus ) { base1 %= modulus ; long result = 1 ; while ( exp > 0 ) { if ( ( exp & 1 ) > 0 ) result = ( result * base1 ) % modulus ; base1 = ( base1 * base1 ) % modulus ; exp >>= 1 ; } return result ; } public static void Main ( ) { long n = 654654 ; long mod = 971 ; Console . WriteLine ( ( ( modpow ( n , 6 , mod ) + modpow ( n , 2 , mod ) ) % mod + 355 ) % mod ) ; } }
Largest Sum Contiguous Subarray | C # program to print largest contiguous array sum ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a ) { int size = a . Length ; int max_so_far = int . MinValue , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void Main ( ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; Console . Write ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySum ( a ) ) ; } }
Place N ^ 2 numbers in matrix such that every row has an equal sum | C # program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; Using modulo to go to the firs column after the last column ; Making a 2D array containing numbers ; Driver code
using System ; class GFG { static int [ , ] solve ( int [ , ] arr , int n ) { int [ , ] ans = new int [ n , n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i , j ] = arr [ j , ( i + j ) % n ] ; } } return ans ; } static void show ( int [ , ] arr , int n ) { int [ , ] res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( res [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static int [ , ] makeArray ( int n ) { int [ , ] arr = new int [ n , n ] ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) arr [ i , j ] = c ++ ; } return arr ; } static void Main ( ) { int n = 5 ; int [ , ] arr = makeArray ( n ) ; show ( arr , n ) ; } }
Largest Sum Contiguous Subarray |  ; Do not compare for all elements . Compare only when max_ending_here > 0
static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = a [ 0 ] , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; }
Minimum rooms for m events of n batches with given schedule | C # program to find the minimum number of rooms required ; Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store number of class happening in empty slot ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; Driver Code
using System ; class GFG { static int findMinRooms ( string [ ] slots , int n , int m ) { int [ ] counts = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) counts [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( slots [ i ] [ j ] == '1' ) counts [ j ] ++ ; int max = - 1 ; for ( int i = 0 ; i < m ; i ++ ) if ( max < counts [ i ] ) max = counts [ i ] ; return max ; } public static void Main ( ) { int n = 3 , m = 7 ; String [ ] slots = { "0101011" , "0011001" , "0110111" } ; Console . Write ( findMinRooms ( slots , n , m ) ) ; } }
Largest Sum Contiguous Subarray | C # program to print largest contiguous array sum ; Driver code
using System ; class GFG { static int maxSubArraySum ( int [ ] a , int size ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . Max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; } return max_so_far ; } public static void Main ( ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . Length ; Console . Write ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySum ( a , n ) ) ; } }
Largest Sum Contiguous Subarray | C # program to print largest contiguous array sum ; Driver code
using System ; class GFG { static void maxSubArraySum ( int [ ] a , int size ) { int max_so_far = int . MinValue , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } Console . WriteLine ( " Maximum ▁ contiguous ▁ " + " sum ▁ is ▁ " + max_so_far ) ; Console . WriteLine ( " Starting ▁ index ▁ " + start ) ; Console . WriteLine ( " Ending ▁ index ▁ " + end ) ; } public static void Main ( ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . Length ; maxSubArraySum ( a , n ) ; } }
Find minimum number of currency notes and values that sum to given amount | C # program to accept an amount and count number of notes ; function to count and print currency notes ; count notes using Greedy approach ; Print notes ; Driver function
using System ; public class GfG { public static void countCurrency ( int amount ) { int [ ] notes = new int [ ] { 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 } ; int [ ] noteCounter = new int [ 9 ] ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( amount >= notes [ i ] ) { noteCounter [ i ] = amount / notes [ i ] ; amount = amount - noteCounter [ i ] * notes [ i ] ; } } Console . WriteLine ( " Currency ▁ Count ▁ - > " ) ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( noteCounter [ i ] != 0 ) { Console . WriteLine ( notes [ i ] + " ▁ : ▁ " + noteCounter [ i ] ) ; } } } public static void Main ( ) { int amount = 868 ; countCurrency ( amount ) ; } }
Minimum sum by choosing minimum of pairs from array | C # program to minimize the cost of array minimization ; Returns minimum possible sum in array B [ ] ; Driver Code
using System ; using System . Linq ; public class GFG { static int minSum ( int [ ] A , int n ) { int min_val = A . Min ( ) ; return ( min_val * ( n - 1 ) ) ; } static public void Main ( ) { int [ ] A = { 3 , 6 , 2 , 8 , 7 , 5 } ; int n = A . Length ; Console . WriteLine ( minSum ( A , n ) ) ; } }
Program for Next Fit algorithm in Memory Management | C # program for next fit memory management algorithm ; Function to allocate memory to blocks as per Next fit algorithm ; Stores block id of the block allocated to a process ; Initially no block is assigned to any process ; pick each process and find suitable blocks according to its size ad assign to it ; Do not start from beginning ; allocate block j to p [ i ] process ; Reduce available memory in this block . ; mod m will help in traversing the blocks from starting block after we reach the end . ; Driver program
using System ; using System . Linq ; public class GFG { static void NextFit ( int [ ] blockSize , int m , int [ ] processSize , int n ) { int [ ] allocation = new int [ n ] ; int j = 0 ; Enumerable . Repeat ( - 1 , n ) . ToArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ] = j ; blockSize [ j ] -= processSize [ i ] ; break ; } j = ( j + 1 ) % m ; } } Console . Write ( " STRNEWLINE Process ▁ No . TABSYMBOL Process ▁ Size TABSYMBOL Block ▁ no . STRNEWLINE " ) ; for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( i + 1 + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) { Console . Write ( allocation [ i ] + 1 ) ; } else { Console . Write ( " Not ▁ Allocated " ) ; } Console . WriteLine ( " " ) ; } } static public void Main ( ) { int [ ] blockSize = { 5 , 10 , 20 } ; int [ ] processSize = { 10 , 20 , 5 } ; int m = blockSize . Length ; int n = processSize . Length ; NextFit ( blockSize , m , processSize , n ) ; } }
Maximum profit by buying and selling a share at most twice |  ; adding array ; 80 / 30 / / \ 25 / 15 / / \ / 2 10 / \ / 8 ; traversing through array from ( i + 1 ) th position
using System ; using System . Collections . Generic ; class GFG { static void Main ( ) { int [ ] price = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int profit = 0 ; for ( int i = 1 ; i < price . Length ; i ++ ) { int sub = price [ i ] - price [ i - 1 ] ; if ( sub > 0 ) profit += sub ; } Console . WriteLine ( " Maximum ▁ Profit = " + profit ) ; } }
Find if there is a pair in root to a leaf path with sum equals to root 's data | C # program to find if there is a pair in any root to leaf path with sum equals to root 's key. ; A binary tree node has data , pointer to left child and a pointer to right child ; utility that allocates a new node with the given data and null left and right pointers . ; Function to print root to leaf path which satisfies the condition ; Base condition ; Check if current node makes a pair with any of the existing elements in set . ; Insert current node in set ; If result returned by either left or right child is true , return true . ; Remove current node from hash table ; A wrapper over printPathUtil ( ) ; create an empty hash table ; Recursively check in left and right subtrees . ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } ; static Node newnode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static bool printPathUtil ( Node node , HashSet < int > s , int root_data ) { if ( node == null ) return false ; int rem = root_data - node . data ; if ( s . Contains ( rem ) ) return true ; s . Add ( node . data ) ; bool res = printPathUtil ( node . left , s , root_data ) || printPathUtil ( node . right , s , root_data ) ; s . Remove ( node . data ) ; return res ; } static bool isPathSum ( Node root ) { HashSet < int > s = new HashSet < int > ( ) ; return printPathUtil ( root . left , s , root . data ) || printPathUtil ( root . right , s , root . data ) ; } public static void Main ( String [ ] args ) { Node root = newnode ( 8 ) ; root . left = newnode ( 5 ) ; root . right = newnode ( 4 ) ; root . left . left = newnode ( 9 ) ; root . left . right = newnode ( 7 ) ; root . left . right . left = newnode ( 1 ) ; root . left . right . right = newnode ( 12 ) ; root . left . right . right . right = newnode ( 2 ) ; root . right . right = newnode ( 11 ) ; root . right . right . left = newnode ( 3 ) ; Console . Write ( isPathSum ( root ) == true ? " Yes " : " No " ) ; } }
Program for Least Recently Used ( LRU ) Page Replacement algorithm | C # program for page replacement algorithms ; Driver method ; To represent set of current pages . We use an Arraylist ; Insert it into set if not present already which represents page fault ; Check if the set can hold equal pages ; Increment page faults ; Remove the indexes page ; insert the current page
using System ; using System . Collections . Generic ; class LRU { public static void Main ( String [ ] args ) { int capacity = 4 ; int [ ] arr = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; List < int > s = new List < int > ( capacity ) ; int count = 0 ; int page_faults = 0 ; foreach ( int i in arr ) { if ( ! s . Contains ( i ) ) { if ( s . Count == capacity ) { s . RemoveAt ( 0 ) ; s . Insert ( capacity - 1 , i ) ; } else s . Insert ( count , i ) ; page_faults ++ ; ++ count ; } else { s . Remove ( i ) ; s . Insert ( s . Count , i ) ; } } Console . WriteLine ( page_faults ) ; } }
Find the subarray with least average | A Simple C # program to find minimum average subarray ; Prints beginning and ending indexes of subarray of size k with minimum average ; k must be smaller than or equal to n ; Initialize beginning index of result ; Compute sum of first subarray of size k ; Initialize minimum sum as current sum ; Traverse from ( k + 1 ) ' th ▁ element ▁ to ▁ n ' th element ; Add current item and remove first item of previous subarray ; Update result if needed ; Driver method to test the above function ; Subarray size
using System ; class Test { static int [ ] arr = new int [ ] { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; static void findMinAvgSubarray ( int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } Console . Write ( " Subarray ▁ between ▁ [ " + res_index + " , ▁ " + ( res_index + k - 1 ) + " ] ▁ has ▁ minimum ▁ average " ) ; } public static void Main ( ) { int k = 3 ; findMinAvgSubarray ( arr . Length , k ) ; } }
Find element using minimum segments in Seven Segment Display | C # program to find minimum number of segments required ; Precomputed values of segment used by digit 0 to 9. ; Return the number of segments used by x . ; Finding sum of the segment used by each digit of a number . ; Initialising the minimum segment and minimum number index . ; Finding and comparing segment used by each number arr [ i ] . ; If arr [ i ] used less segment then update minimum segment and minimum number . ; Driver program
using System ; class GFG { static int [ ] seg = new int [ 10 ] { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int computeSegment ( int x ) { if ( x == 0 ) return seg [ 0 ] ; int count = 0 ; while ( x > 0 ) { count += seg [ x % 10 ] ; x /= 10 ; } return count ; } static int elementMinSegment ( int [ ] arr , int n ) { int minseg = computeSegment ( arr [ 0 ] ) ; int minindex = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int temp = computeSegment ( arr [ i ] ) ; if ( temp < minseg ) { minseg = temp ; minindex = i ; } } return arr [ minindex ] ; } static public void Main ( ) { int [ ] arr = { 489 , 206 , 745 , 123 , 756 } ; int n = arr . Length ; Console . WriteLine ( elementMinSegment ( arr , n ) ) ; } }
Find the Largest number with given number of digits and sum of digits | C # program to find the largest number that can be formed from given sum of digits and number of digits ; Function to print the largest possible number with digit sum ' s ' and ' m ' number of digits ; If sum of digits is 0 , then a number is possible only if number of digits is 1 ; Sum greater than the maximum possible sum ; Create an array to store digits of result ; Fill from most significant digit to least significant digit ; Fill 9 first to make the number largest ; If remaining sum becomes less than 9 , then fill the remaining sum ; Driver Code
using System ; class GFG { static void findLargest ( int m , int s ) { if ( s == 0 ) { Console . Write ( m == 1 ? " Largest ▁ number ▁ is ▁ 0" : " Not ▁ possible " ) ; return ; } if ( s > 9 * m ) { Console . WriteLine ( " Not ▁ possible " ) ; return ; } int [ ] res = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( s >= 9 ) { res [ i ] = 9 ; s -= 9 ; } else { res [ i ] = s ; s = 0 ; } } Console . Write ( " Largest ▁ number ▁ is ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) Console . Write ( res [ i ] ) ; } static public void Main ( ) { int s = 9 , m = 2 ; findLargest ( m , s ) ; } }
Shortest Superstring Problem | C # program to find shortest superstring using Greedy Approximate Algorithm ; Utility function to calculate minimum of two numbers ; Function to calculate maximum overlap in two given strings ; max will store maximum overlap i . e maximum length of the matching prefix and suffix ; check suffix of str1 matches with prefix of str2 ; compare last i characters in str1 with first i characters in str2 ; Update max and str ; check prefix of str1 matches with suffix of str2 ; compare first i characters in str1 with last i characters in str2 ; pdate max and str ; Function to calculate smallest string that contains each string in the given set as substring . ; run len - 1 times to consider every pair ; To store maximum overlap ; To store array index of strings involved in maximum overlap ; to store resultant string after maximum overlap ; res will store maximum length of the matching prefix and suffix str is passed by reference and will store the resultant string after maximum overlap of arr [ i ] and arr [ j ] , if any . ; Check for maximum overlap ; Ignore last element in next cycle ; If no overlap , append arr [ len ] to arr [ 0 ] ; Copy resultant string to index l ; Copy string at last index to index r ; Driver Code
using System ; class GFG { static String str ; static int min ( int a , int b ) { return ( a < b ) ? a : b ; } static int findOverlappingPair ( String str1 , String str2 ) { int max = Int32 . MinValue ; int len1 = str1 . Length ; int len2 = str2 . Length ; for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . Substring ( len1 - i ) . CompareTo ( str2 . Substring ( 0 , i ) ) == 0 ) { if ( max < i ) { max = i ; str = str1 + str2 . Substring ( i ) ; } } } for ( int i = 1 ; i <= min ( len1 , len2 ) ; i ++ ) { if ( str1 . Substring ( 0 , i ) . CompareTo ( str2 . Substring ( len2 - i ) ) == 0 ) { if ( max < i ) { max = i ; str = str2 + str1 . Substring ( i ) ; } } } return max ; } static String findShortestSuperstring ( String [ ] arr , int len ) { while ( len != 1 ) { int max = Int32 . MinValue ; int l = 0 , r = 0 ; String resStr = " " ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { int res = findOverlappingPair ( arr [ i ] , arr [ j ] ) ; if ( max < res ) { max = res ; resStr = str ; l = i ; r = j ; } } } len -- ; if ( max == Int32 . MinValue ) arr [ 0 ] += arr [ len ] ; else { arr [ l ] = resStr ; arr [ r ] = arr [ len ] ; } } return arr [ 0 ] ; } public static void Main ( String [ ] args ) { String [ ] arr = { " catgc " , " ctaagt " , " gcta " , " ttca " , " atgcatc " } ; int len = arr . Length ; Console . Write ( " The ▁ Shortest ▁ Superstring ▁ is ▁ " + findShortestSuperstring ( arr , len ) ) ; } }