codeparrot/xlcost-text-to-code huggingface dataset:
Count majority element in a matrix | C # program to find count of all majority elements in a Matrix ; Function to find count of all majority elements in a Matrix ; Store frequency of elements in matrix ; loop to iteratre through map ; check if frequency is greater than or equal to ( N * M ) / 2 ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static int majorityInMatrix ( int [ , ] arr ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . ContainsKey ( arr [ i , j ] ) ) mp [ arr [ i , j ] ] ++ ; else mp [ arr [ i , j ] ] = 1 ; } } int countMajority = 0 ; Dictionary < int , int > . KeyCollection keyColl = mp . Keys ; foreach ( int i in keyColl ) { if ( mp [ i ] >= ( ( N * M ) / 2 ) ) { countMajority ++ ; } } return countMajority ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 2 } , { 1 , 3 , 2 } , { 1 , 2 , 6 } } ; Console . WriteLine ( majorityInMatrix ( mat ) ) ; } }
Find pair with maximum difference in any column of a Matrix | C # program to find column with max difference of any pair of elements ; Function to find the column with max difference ; Traverse matrix column wise ; Insert elements of column to vector ; calculating difference between maximum and minimum ; Driver Code
using System ; class GFG { static int colMaxDiff ( int [ , ] mat ) { int max_diff = int . MinValue ; for ( int i = 0 ; i < N ; i ++ ) { int max_val = mat [ 0 , i ] , min_val = mat [ 0 , i ] ; for ( int j = 1 ; j < N ; j ++ ) { max_val = Math . Max ( max_val , mat [ j , i ] ) ; min_val = Math . Min ( min_val , mat [ j , i ] ) ; } max_diff = Math . Max ( max_diff , max_val - min_val ) ; } return max_diff ; } public static void Main ( ) { int [ , ] mat = { { 1 , 2 , 3 , 4 , 5 } , { 5 , 3 , 5 , 4 , 0 } , { 5 , 6 , 7 , 8 , 9 } , { 0 , 6 , 3 , 4 , 12 } , { 9 , 7 , 12 , 4 , 3 } } ; Console . WriteLine ( " Max ▁ difference ▁ : ▁ " + colMaxDiff ( mat ) ) ; } }
Find the Missing Number in a sorted array | A binary search based program to find the only missing number in a sorted array of distinct elements within limited range . ; Driver Code
using System ; class GFG { static int search ( int [ ] ar , int size ) { int a = 0 , b = size - 1 ; int mid = 0 ; while ( ( b - a ) > 1 ) { mid = ( a + b ) / 2 ; if ( ( ar [ a ] - a ) != ( ar [ mid ] - mid ) ) b = mid ; else if ( ( ar [ b ] - b ) != ( ar [ mid ] - mid ) ) a = mid ; } return ( ar [ a ] + 1 ) ; } static public void Main ( String [ ] args ) { int [ ] ar = { 1 , 2 , 3 , 4 , 5 , 6 , 8 } ; int size = ar . Length ; Console . WriteLine ( " Missing ▁ number : ▁ " + search ( ar , size ) ) ; } }
Delete array element in given index range [ L | C # code to delete element in given range ; Delete L to R elements ; Return size of Array after delete element ; Driver Code
using System ; class GFG { static int deleteElement ( int [ ] A , int L , int R , int N ) { int i , j = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( i <= L i >= R ) { A [ j ] = A [ i ] ; j ++ ; } } return j ; } public static void Main ( ) { int [ ] A = new int [ ] { 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 } ; int L = 2 , R = 7 ; int n = A . Length ; int res_size = deleteElement ( A , L , R , n ) ; for ( int i = 0 ; i < res_size ; i ++ ) Console . Write ( A [ i ] + " ▁ " ) ; } }
Find the only missing number in a sorted array | C # program to find the only missing element . ; If this is the first element which is not index + 1 , then missing element is mid + 1 ; if this is not the first missing element search in left side ; if it follows index + 1 property then search in right side ; if no element is missing ; Driver code
using System ; class GFG { static int findmissing ( int [ ] ar , int N ) { int l = 0 , r = N - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] != mid + 1 && ar [ mid - 1 ] == mid ) return ( mid + 1 ) ; if ( ar [ mid ] != mid + 1 ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 7 , 8 } ; int N = arr . Length ; Console . WriteLine ( findmissing ( arr , N ) ) ; } }
Find index of first occurrence when an unsorted array is sorted | C # program to find index of first occurrence of x when array is sorted . ; lower_bound returns iterator pointing to first element that does not compare less to x . ; If x is not present return - 1. ;  ; Driver Code
using System ; class GFG { static int findFirst ( int [ ] arr , int n , int x ) { Array . Sort ( arr ) ; int ptr = lowerBound ( arr , 0 , n , x ) ; return ( arr [ ptr ] != x ) ? - 1 : ( ptr ) ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static public void Main ( ) { int x = 20 ; int [ ] arr = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . Length ; Console . Write ( findFirst ( arr , n , x ) ) ; } }
Find index of first occurrence when an unsorted array is sorted | C # program to find index of first occurrence of x when array is sorted . ; Driver main
using System ; public class GFG { static int findFirst ( int [ ] arr , int n , int x ) { int count = 0 ; bool isX = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { isX = true ; } else if ( arr [ i ] < x ) { count ++ ; } } return ( isX == false ) ? - 1 : count ; } public static void Main ( ) { int x = 20 ; int [ ] arr = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . Length ; Console . WriteLine ( findFirst ( arr , n , x ) ) ; } }
Find duplicate in an array in O ( n ) and by using O ( 1 ) extra space | C # code to find the repeated elements in the array where every other is present once ; Function to find duplicate ; Find the intersection point of the slow and fast . ; Find the " entrance " to the cycle . ; Driver Code
using System ; class GFG { public static int findDuplicate ( int [ ] arr ) { int slow = arr [ 0 ] ; int fast = arr [ 0 ] ; do { slow = arr [ slow ] ; fast = arr [ arr [ fast ] ] ; } while ( slow != fast ) ; int ptr1 = arr [ 0 ] ; int ptr2 = slow ; while ( ptr1 != ptr2 ) { ptr1 = arr [ ptr1 ] ; ptr2 = arr [ ptr2 ] ; } return ptr1 ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 2 , 1 } ; Console . WriteLine ( " " + findDuplicate ( arr ) ) ; } }
Number of Larger Elements on right side in a string | C # program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code
using System ; class GFG { static int MAX_CHAR = 26 ; static void printGreaterCount ( string str ) { int len = str . Length ; int [ ] ans = new int [ len ] ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = len - 1 ; i >= 0 ; i -- ) { count [ str [ i ] - ' a ' ] ++ ; for ( int j = str [ i ] - ' a ' + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( int i = 0 ; i < len ; i ++ ) { Console . Write ( ans [ i ] + " ▁ " ) ; } } static void Main ( ) { string str = " abcd " ; printGreaterCount ( str ) ; } }
Print all pairs with given sum | C # implementation of simple method to find count of pairs with given sum . ; Returns number of pairs in arr [ 0. . n - 1 ] with sum equal to ' sum ' ; Store counts of all elements in map m ; Traverse through all elements ; Search if a pair can be formed with arr [ i ] . ; Driver code
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static void printPairs ( int [ ] arr , int n , int sum ) { Dictionary < int , int > m = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = sum - arr [ i ] ; if ( m . ContainsKey ( rem ) ) { int count = m [ rem ] ; for ( int j = 0 ; j < count ; j ++ ) { Console . Write ( " ( " + rem + " , ▁ " + arr [ i ] + " ) " + " STRNEWLINE " ) ; } } if ( m . ContainsKey ( arr [ i ] ) ) { m [ arr [ i ] ] ++ ; } else { m [ arr [ i ] ] = 1 ; } } } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . Length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
Maximum product quadruple ( sub | A O ( n ) C # program to find maximum quadruple in an array . ; Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver Code
using System ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 4 ) { return - 1 ; } int maxA = int . MinValue , maxB = int . MinValue , maxC = int . MinValue , maxD = int . MinValue ; int minA = int . MaxValue , minB = int . MaxValue , minC = int . MaxValue , minD = int . MaxValue ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxD = maxC ; maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxD = maxC ; maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) { maxD = maxC ; maxC = arr [ i ] ; } else if ( arr [ i ] > maxD ) { maxD = arr [ i ] ; } if ( arr [ i ] < minA ) { minD = minC ; minC = minB ; minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) { minD = minC ; minC = minB ; minB = arr [ i ] ; } else if ( arr [ i ] < minC ) { minD = minC ; minC = arr [ i ] ; } else if ( arr [ i ] < minD ) { minD = arr [ i ] ; } } int x = maxA * maxB * maxC * maxD ; int y = minA * minB * minC * minD ; int z = minA * minB * maxA * maxB ; return Math . Max ( x , Math . Max ( y , z ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , - 4 , 3 , - 6 , 7 , 0 } ; int n = arr . Length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) Console . Write ( " No ▁ Quadruple ▁ Exists " ) ; else Console . Write ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
Ways to choose three points with distance between the most distant points <= L | C # program to count ways to choose triplets such that the distance between the farthest points <= L ; Returns the number of triplets with distance between farthest points <= L ; sort to get ordered triplets so that we can find the distance between farthest points belonging to a triplet ; generate and check for all possible triplets : { arr [ i ] , arr [ j ] , arr [ k ] } ; Since the array is sorted the farthest points will be a [ i ] and a [ k ] ; ; Driver Code ; set of n points on the X axis
using System ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Array . Sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { int mostDistantDistance = arr [ k ] - arr [ i ] ; if ( mostDistantDistance <= L ) { ways ++ ; } } } } return ways ; } static public void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . Length ; int L = 3 ; int ans = countTripletsLessThanL ( n , L , arr ) ; Console . WriteLine ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }
Triplets in array with absolute difference less than k | C # program to count triplets with difference less than k . ; Return the lower bound i . e smallest index of element having value greater or equal to value ; Return the number of triplet indices satisfies the three constraints ; sort the array ; for each element from index 2 to n - 1. ; finding the lower bound of arr [ i ] - k . ; If there are at least two elements between lower bound and current element . ; increment the count by lb - i C 2. ; Driver Code
using System ; class GFG { static int binary_lower ( int value , int [ ] arr , int n ) { int start = 0 ; int end = n - 1 ; int ans = - 1 ; int mid ; while ( start <= end ) { mid = ( start + end ) / 2 ; if ( arr [ mid ] >= value ) { end = mid - 1 ; ans = mid ; } else { start = mid + 1 ; } } return ans ; } static int countTriplet ( int [ ] arr , int n , int k ) { int count = 0 ; Array . Sort ( arr ) ; for ( int i = 2 ; i < n ; i ++ ) { int cur = binary_lower ( arr [ i ] - k , arr , n ) ; if ( cur <= i - 2 ) { count += ( ( i - cur ) * ( i - cur - 1 ) ) / 2 ; } } return count ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 2 , 2 , 3 } ; int k = 1 ; int n = arr . Length ; Console . WriteLine ( countTriplet ( arr , n , k ) ) ; } }
Longest Palindromic Substring using Palindromic Tree | Set 3 | C # code for longest Palindromic subString using Palindromic Tree data structure ; store start and end indexes of current Node inclusively ; stores length of subString ; stores insertion Node for all characters a - z ; stores the Maximum Palindromic Suffix Node for the current Node ; two special dummy Nodes as explained above ; stores Node information for constant time access ; Keeps track the current Node while insertion ; Function to insert edge in tree ; Finding X , such that s [ currIndex ] + X + s [ currIndex ] is palindrome . ; Check if s [ currIndex ] + X + s [ currIndex ] is already Present in tree . ; Else Create new node ; ; Setting suffix edge for newly Created Node . ; longest Palindromic suffix for a String of length 1 is a Null String . ; Else ; Driver code ; Imaginary root 's suffix edge points to  itself, since for an imaginary String  of length = -1 has an imaginary suffix  String. Imaginary root. ; null root 's suffix edge points to  Imaginary root, since for a String  of length = 0 has an imaginary suffix String. ; last will be the index of our last subString
using System ; class GFG { static readonly int MAXN = 1000 ; class Node { public int start , end ; public int Length ; public int [ ] insertionEdge = new int [ 26 ] ; public int suffixEdge ; } ; static Node root1 , root2 ; static Node [ ] tree = new Node [ MAXN ] ; static int currNode ; static char [ ] s ; static int ptr ; static void insert ( int currIndex ) { int temp = currNode ; while ( true ) { int currLength = tree [ temp ] . Length ; if ( currIndex - currLength >= 1 && ( s [ currIndex ] == s [ currIndex - currLength - 1 ] ) ) break ; temp = tree [ temp ] . suffixEdge ; } if ( tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] != 0 ) { currNode = tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] ; return ; } ptr ++ ; tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] = ptr ; tree [ ptr ] . end = currIndex ; tree [ ptr ] . Length = tree [ temp ] . Length + 2 ; tree [ ptr ] . start = tree [ ptr ] . end - tree [ ptr ] . Length + 1 ; currNode = ptr ; temp = tree [ temp ] . suffixEdge ; if ( tree [ currNode ] . Length == 1 ) { tree [ currNode ] . suffixEdge = 2 ; return ; } while ( true ) { int currLength = tree [ temp ] . Length ; if ( currIndex - currLength >= 1 && ( s [ currIndex ] == s [ currIndex - currLength - 1 ] ) ) break ; temp = tree [ temp ] . suffixEdge ; } tree [ currNode ] . suffixEdge = tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] ; } public static void Main ( String [ ] args ) { root1 = new Node ( ) ; root1 . Length = - 1 ; root1 . suffixEdge = 1 ; root2 = new Node ( ) ; root2 . Length = 0 ; root2 . suffixEdge = 1 ; for ( int i = 0 ; i < MAXN ; i ++ ) tree [ i ] = new Node ( ) ; tree [ 1 ] = root1 ; tree [ 2 ] = root2 ; ptr = 2 ; currNode = 1 ; s = " forgeeksskeegfor " . ToCharArray ( ) ; for ( int i = 0 ; i < s . Length ; i ++ ) insert ( i ) ; int last = ptr ; for ( int i = tree [ last ] . start ; i <= tree [ last ] . end ; i ++ ) Console . Write ( s [ i ] ) ; } }
Maximum occurring character in a linked list | C # program to count the maximum occurring character in linked list ; Link list node ; counting the frequency of current element p . data ; if current counting is greater than max ; Push a node to linked list . Note that this function changes the head ; Driver code ; Start with the empty list ; this will create a linked list of character " geeksforgeeks "
using System ; class GFG { class Node { public char data ; public Node next ; } ; static Node head_ref ; static char maxChar ( Node head ) { Node p = head ; int max = - 1 ; char res = '0' ; while ( p != null ) { Node q = p . next ; int count = 1 ; while ( q != null ) { if ( p . data == q . data ) count ++ ; q = q . next ; } if ( max < count ) { res = p . data ; max = count ; } p = p . next ; } return res ; } static void push ( char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; } public static void Main ( string [ ] args ) { head_ref = null ; string str = " skeegforskeeg " ; char [ ] st = str . ToCharArray ( ) ; int i ; for ( i = 0 ; i < st . Length ; i ++ ) push ( st [ i ] ) ; Console . Write ( maxChar ( head_ref ) ) ; } }
Find the one missing number in range | C # program to find missing number in a range . ; Find the missing number in a range ; here we xor of all the number ; xor last number ; Driver Code
using System ; using System . Collections . Generic ; using System . Linq ; class GFG { static int missingNum ( int [ ] arr , int n ) { List < int > list = new List < int > ( arr . Length ) ; foreach ( int i in arr ) { list . Add ( i ) ; } int minvalue = list . Min ( ) ; int xornum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xornum ^= ( minvalue ) ^ arr [ i ] ; minvalue ++ ; } return xornum ^ minvalue ; } public static void Main ( String [ ] args ) { int [ ] arr = { 13 , 12 , 11 , 15 } ; int n = arr . Length ; Console . WriteLine ( missingNum ( arr , n ) ) ; } }
Find last index of a character in a string | C # program to find last index of character x in given string . ; Returns last index of x if it is present Else returns - 1. ; Driver code ; String in which char is to be found ; char whose index is to be found
using System ; class GFG { static int findLastIndex ( string str , char x ) { int index = - 1 ; for ( int i = 0 ; i < str . Length ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; } public static void Main ( ) { string str = " geeksforgeeks " ; char x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) Console . WriteLine ( " Character ▁ not ▁ found " ) ; else Console . WriteLine ( " Last ▁ index ▁ is ▁ " + index ) ; } }
Find last index of a character in a string | C # code to find last index character x in given string . ; Returns last index of x if it is present . Else returns - 1. ; Traverse from right ; Driver code
using System ; class GFG { static int findLastIndex ( string str , char x ) { for ( int i = str . Length - 1 ; i >= 0 ; i -- ) if ( str [ i ] == x ) return i ; return - 1 ; } public static void Main ( ) { string str = " geeksforgeeks " ; char x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) Console . WriteLine ( " Character ▁ not ▁ found " ) ; else Console . WriteLine ( " Last ▁ index ▁ is ▁ " + index ) ; } }
Smallest number whose set bits are maximum in a given range | C # program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Driver code
using System ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } static public void Main ( ) { int l = 1 ; int r = 5 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; Console . WriteLine ( countMaxSetBits ( l , r ) ) ; } }
Largest number less than or equal to N in BST ( Iterative Approach ) | C # code to find the largest value smaller than or equal to N ; To create new BST Node ; To insert a new node in BST ; if tree is empty return new node ; if key is less then or greater then node value then recur down the tree ; return the ( unchanged ) node pointer ; Returns largest value smaller than or equal to key . If key is smaller than the smallest , it returns - 1. ; Driver code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; } ; static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static int findFloor ( Node root , int key ) { Node curr = root , ans = null ; while ( curr != null ) { if ( curr . key <= key ) { ans = curr ; curr = curr . right ; } else curr = curr . left ; } if ( ans != null ) return ans . key ; return - 1 ; } public static void Main ( String [ ] args ) { int N = 25 ; Node root = new Node ( ) ; insert ( root , 19 ) ; insert ( root , 2 ) ; insert ( root , 1 ) ; insert ( root , 3 ) ; insert ( root , 12 ) ; insert ( root , 9 ) ; insert ( root , 21 ) ; insert ( root , 19 ) ; insert ( root , 25 ) ; Console . Write ( " { 0 } " , findFloor ( root , N ) ) ; } }
Find if given number is sum of first n natural numbers | C # program for finding s such that sum from 1 to s equals to n ; Function to find no . of elements to be added to get s ; Apply Binary search ; Find mid ; find sum of 1 to mid natural numbers using formula ; If sum is equal to n return mid ; If greater than n do r = mid - 1 ; else do l = mid + 1 ; If not possible , return - 1 ; Drivers code
using System ; public class GFG { static int findS ( int s ) { int l = 1 , r = ( s / 2 ) + 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int sum = mid * ( mid + 1 ) / 2 ; if ( sum == s ) return mid ; else if ( sum > s ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } static public void Main ( ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) Console . WriteLine ( " - 1" ) ; else Console . WriteLine ( n ) ; } }
Check if a Binary String can be sorted in decreasing order by removing non | C # program for the above approach ; Function to sort the given string in decreasing order by removing the non adjacent characters ; Keeps the track whether the string can be sorted or not ; Traverse the given string S ; Check if S [ i ] and S [ i + 1 ] are both '1' ; Traverse the string S from the indices i to 0 ; If S [ j ] and S [ j + 1 ] is equal to 0 ; Mark flag false ; If flag is 0 , then it is not possible to sort the string ; Otherwise ; Driver code
using System ; class GFG { static string canSortString ( string S , int N ) { int flag = 1 ; int i , j ; for ( i = N - 2 ; i >= 0 ; i -- ) { if ( S [ i ] == '1' && S [ i + 1 ] == '1' ) { break ; } } for ( j = i ; j >= 0 ; j -- ) { if ( S [ j ] == '0' && S [ j + 1 ] == '0' ) { flag = 0 ; break ; } } if ( flag == 0 ) { return " No " ; } else { return " Yes " ; } } public static void Main ( string [ ] args ) { string S = "10101011011" ; int N = S . Length ; Console . WriteLine ( canSortString ( S , N ) ) ; } }
Rearrange array elements to maximize the sum of MEX of all prefix arrays | C # program for the above approach ; Function to find the maximum sum of MEX of prefix arrays for any arrangement of the given array ; Stores the final arrangement ; Sort the array in increasing order ; Iterate over the array arr [ ] ; Iterate over the array , arr [ ] and push the remaining occurrences of the elements into ans [ ] ; Print the array , ans [ ] ; Driver Code ; Given array ; Store the size of the array ; Function Call
using System ; class GFG { static void maximumMex ( int [ ] arr , int N ) { int [ ] ans = new int [ 2 * N ] ; Array . Sort ( ans ) ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == 0 arr [ i ] != arr [ i - 1 ] ) { j += 1 ; ans [ j ] = arr [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && arr [ i ] == arr [ i - 1 ] ) { j += 1 ; ans [ j ] = arr [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 0 , 0 } ; int N = arr . Length ; maximumMex ( arr , N ) ; } }