codeparrot/xlcost-text-to-code huggingface dataset:
Find array elements with frequencies in range [ l , r ] | C # program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findElements ( int [ ] arr , int n , int l , int r ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int a = 0 ; if ( ! mp . ContainsKey ( arr [ i ] ) ) { a = 1 ; } else { a = mp [ arr [ i ] ] + 1 ; } if ( ! mp . ContainsKey ( arr [ i ] ) ) { mp . Add ( arr [ i ] , a ) ; } else { mp . Remove ( arr [ i ] ) ; mp . Add ( arr [ i ] , a ) ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) && l <= mp [ arr [ i ] ] && ( mp [ arr [ i ] ] <= r ) ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = arr . Length ; int l = 2 , r = 3 ; findElements ( arr , n , l , r ) ; } }
Count triplets ( a , b , c ) such that a + b , b + c and a + c are all divisible by K | C # implementation of the approach ; Function returns the count of the triplets ; iterate for all triples pairs ( i , j , l ) ; if the condition is satisfied ; Driver code
using System ; class GFG { static long count_triples ( int n , int k ) { int i = 0 , j = 0 , l = 0 ; int count = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { for ( l = 1 ; l <= n ; l ++ ) { if ( ( i + j ) % k == 0 && ( i + l ) % k == 0 && ( j + l ) % k == 0 ) count ++ ; } } } return count ; } public static void Main ( ) { int n = 3 ; int k = 2 ; long ans = count_triples ( n , k ) ; Console . WriteLine ( ans ) ; } }
kth smallest / largest in a small range unsorted array | C # program of kth smallest / largest in a small range unsorted array ; Storing counts of elements ; Traverse hash array build above until we reach k - th smallest element . ; Driver code
using System ; using System . Linq ; class GFG { static int maxs = 1000001 ; static int kthSmallestLargest ( int [ ] arr , int n , int k ) { int max_val = arr . Max ( ) ; int [ ] hash = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i <= max_val ; i ++ ) { while ( hash [ i ] > 0 ) { count ++ ; if ( count == k ) { return i ; } hash [ i ] -- ; } } return - 1 ; } public static void Main ( ) { int [ ] arr = { 11 , 6 , 2 , 9 , 4 , 3 , 16 } ; int n = arr . Length , k = 3 ; Console . WriteLine ( " kth ▁ smallest ▁ number ▁ is : ▁ " + kthSmallestLargest ( arr , n , k ) ) ; } }
Sum and Product of minimum and maximum element of an Array | C # program for the above approach ; Function to get sum ; Function to get product ; Driver code ; sorting the array ; min element would be the element at 0 th index of array after sorting ; max element would be the element at ( n - 1 ) th index of array after sorting
using System ; class GFG { static int findSum ( int minEle , int maxEle ) { return minEle + maxEle ; } static int findProduct ( int minEle , int maxEle ) { return minEle * maxEle ; } public static void Main ( ) { int [ ] arr = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . Length ; Array . Sort ( arr ) ; int minEle = arr [ 0 ] ; int maxEle = arr [ n - 1 ] ; Console . WriteLine ( " Sum ▁ = ▁ " + findSum ( minEle , maxEle ) ) ; Console . WriteLine ( " Product ▁ = ▁ " + findProduct ( minEle , maxEle ) ) ; } }
Meta Binary Search | One | C # implementation of above approach ; Function to show the working of Meta binary search ; int lg = 0 ; Set number of bits to represent largest array index ; This is redundant and will cause error while ( ( 1 << lg ) < n - 1 ) { lg += 1 ; } ; Incrementally construct the index of the target value ; find the element in one direction and update position ; if element found return pos otherwise - 1 ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int bsearch ( List < int > A , int key_to_search ) { int n = ( int ) A . Count ; int lg = ( int ) Math . Log ( n - 1 , 2.0 ) + 1 ; int pos = 0 ; for ( int i = lg - 1 ; i >= 0 ; i -- ) { if ( A [ pos ] == key_to_search ) { return pos ; } int new_pos = pos | ( 1 << i ) ; if ( ( new_pos < n ) && ( A [ new_pos ] <= key_to_search ) ) { pos = new_pos ; } } return ( ( A [ pos ] == key_to_search ) ? pos : - 1 ) ; } static public void Main ( ) { List < int > A = new List < int > ( ) ; int [ ] arr = { - 2 , 10 , 100 , 250 , 32315 } ; for ( int i = 0 ; i < arr . Length ; i ++ ) { A . Add ( arr [ i ] ) ; } Console . WriteLine ( bsearch ( A , 10 ) ) ; } }
Queries to check if a number lies in N ranges of L | C # program to check if the number lies in given range ; Function that answers every query ; container to store all range ; hash the L and R ; Push the element to container and hash the L and R ; sort the elements in container ; each query ; get the number same or greater than integer ; if it lies ; check if greater is hashed as 2 ; else check if greater is hashed as 1 ; Lower bound implementation ; Driver Code ; function call to answer queries
using System ; using System . Collections . Generic ; class GFG { static void answerQueries ( int [ , ] a , int n , int [ ] queries , int q ) { List < int > v = new List < int > ( ) ; Dictionary < int , int > mpp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . Add ( a [ i , 0 ] ) ; if ( ! mpp . ContainsKey ( a [ i , 0 ] ) ) mpp . Add ( a [ i , 0 ] , 1 ) ; v . Add ( a [ i , 1 ] ) ; if ( ! mpp . ContainsKey ( a [ i , 1 ] ) ) mpp . Add ( a [ i , 1 ] , 2 ) ; } v . Sort ( ) ; for ( int i = 0 ; i < q ; i ++ ) { int num = queries [ i ] ; int ind = lowerBound ( v , num ) ; if ( ind >= 0 && v [ ind ] == num ) Console . WriteLine ( " Yes " ) ; else { if ( ind >= 0 && mpp [ v [ ind ] ] == 2 ) Console . WriteLine ( " Yes " ) ; Console . WriteLine ( " No " ) ; } } } static int lowerBound ( List < int > array , int value ) { int low = 0 ; int high = array . Count ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 5 , 6 } , { 1 , 3 } , { 8 , 10 } } ; int n = 3 ; int [ ] queries = { 2 , 3 , 4 , 7 } ; int q = queries . Length ; answerQueries ( a , n , queries , q ) ; } }
Median of two sorted arrays of different sizes | Set 1 ( Linear ) | A C # program to find median of two sorted arrays of unequal sizes ; This function returns median of a [ ] and b [ ] . Assumptions in this function : Both a [ ] and b [ ] are sorted arrays ; Current index of i / p array a [ ] ; Current index of i / p array b [ ] ; Below is to handle the case where all elements of a [ ] are smaller than smallest ( or first ) element of b [ ] or a [ ] is empty ; Below is to handle case where all elements of b [ ] are smaller than smallest ( or first ) element of a [ ] or b [ ] is empty ; Below is to handle the case where sum of number of elements of the arrays is even ; Below is to handle the case where sum of number of elements of the arrays is odd ; Driver Code
using System ; class GFG { static float findmedian ( int [ ] a , int n1 , int [ ] b , int n2 ) { int i = 0 ; int j = 0 ; int k ; int m1 = - 1 , m2 = - 1 ; for ( k = 0 ; k <= ( n1 + n2 ) / 2 ; k ++ ) { if ( i < n1 && j < n2 ) { if ( a [ i ] < b [ j ] ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } else { m2 = m1 ; m1 = b [ j ] ; j ++ ; } } else if ( i == n1 ) { m2 = m1 ; m1 = b [ j ] ; j ++ ; } else if ( j == n2 ) { m2 = m1 ; m1 = a [ i ] ; i ++ ; } } if ( ( n1 + n2 ) % 2 == 0 ) { return ( m1 + m2 ) * ( float ) 1.0 / 2 ; } return m1 ; } public static void Main ( ) { int [ ] a = { 1 , 12 , 15 , 26 , 38 } ; int [ ] b = { 2 , 13 , 24 } ; int n1 = a . Length ; int n2 = b . Length ; Console . WriteLine ( findmedian ( a , n1 , b , n2 ) ) ; } }
Next Smaller Element | Simple C # program to print next smaller elements in a given array ; prints element and NSE pair for all elements of arr [ ] of size n ; driver code
using System ; class GFG { static void printNSE ( int [ ] arr , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { next = arr [ j ] ; break ; } } Console . WriteLine ( arr [ i ] + " ▁ - - ▁ " + next ) ; } } public static void Main ( ) { int [ ] arr = { 11 , 13 , 21 , 3 } ; int n = arr . Length ; printNSE ( arr , n ) ; } }
Longest subarray such that the difference of max and min is at | C # code to find longest subarray with difference between max and min as at - most 1. ; longest constant range length ; first number ; If we see same number ; If we see different number , but same as previous . ; If number is neither same as previous nor as current . ; Driver Code
using System ; class GFG { public static int longestSubarray ( int [ ] input ) { int prev = - 1 ; int current , next ; int prevCount = 0 , currentCount = 1 ; int longest = 1 ; current = input [ 0 ] ; for ( int i = 1 ; i < input . Length ; i ++ ) { next = input [ i ] ; if ( next == current ) { currentCount ++ ; } else if ( next == prev ) { prevCount += currentCount ; prev = current ; current = next ; currentCount = 1 ; } else { longest = Math . Max ( longest , currentCount + prevCount ) ; prev = current ; prevCount = currentCount ; current = next ; currentCount = 1 ; } } return Math . Max ( longest , currentCount + prevCount ) ; } public static void Main ( String [ ] args ) { int [ ] input = { 5 , 5 , 6 , 7 , 6 } ; Console . WriteLine ( longestSubarray ( input ) ) ; } }
Reverse tree path | C # program to Reverse Tree path ; A Binary Tree Node ; class for int values ; ' data ' is input . We need to reverse path from root to data . ' level ' is current level . ' temp ' that stores path nodes . ' nextpos ' used to pick next item for reversing . ; return null if root null ; Final condition if the node is found then ; store the value in it 's level ; change the root value with the current next element of the map ; increment in k for the next element ; store the data in perticular level ; We go to right only when left does not contain given data . This way we make sure that correct path node is stored in temp [ ] ; If current node is part of the path , then do reversing . ; return null if not element found ; Reverse Tree path ; store per level data ; it is for replacing the data ; reverse tree path ; INORDER ; Utility function to create a new tree node ; Driver Code ; Let us create binary tree shown in above diagram ; 7 / \ 6 5 / \ / \ 4 3 2 1 ; Reverse Tree Path ; Traverse inorder
using System ; using System . Collections . Generic ; class GFG { public class Node { public int data ; public Node left , right ; } public class INT { public int data ; } public static Node reverseTreePathUtil ( Node root , int data , IDictionary < int , int > temp , int level , INT nextpos ) { if ( root == null ) { return null ; } if ( data == root . data ) { temp [ level ] = root . data ; root . data = temp [ nextpos . data ] ; nextpos . data ++ ; return root ; } temp [ level ] = root . data ; Node left , right = null ; left = reverseTreePathUtil ( root . left , data , temp , level + 1 , nextpos ) ; if ( left == null ) { right = reverseTreePathUtil ( root . right , data , temp , level + 1 , nextpos ) ; } if ( left != null right != null ) { root . data = temp [ nextpos . data ] ; nextpos . data ++ ; return ( left != null ? left : right ) ; } return null ; } public static void reverseTreePath ( Node root , int data ) { IDictionary < int , int > temp = new Dictionary < int , int > ( ) ; INT nextpos = new INT ( ) ; nextpos . data = 0 ; reverseTreePathUtil ( root , data , temp , 0 , nextpos ) ; } public static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; Console . Write ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } public static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void Main ( string [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; int data = 4 ; reverseTreePath ( root , data ) ; inorder ( root ) ; } }
Longest Subarray with first element greater than or equal to Last element | C # program to find length of the longest array with first element smaller than or equal to last element . ; Search function for searching the first element of the subarray which is greater or equal to the last element ( num ) ; Returns length of the longest array with first element smaller than the last element . ; Search space for the potential first elements . ; It will store the Indexes of the elements of search space in the original array . ; Initially the search space is empty . ; We will add an ith element in the search space if the search space is empty or if the ith element is greater than the last element of the search space . ; we will search for the index first element in the search space and we will use it find the index of it in the original array . ; Update the answer if the length of the subarray is greater than the previously calculated lengths . ; Driver code
using System ; class GFG { static int binarySearch ( int [ ] searchSpace , int s , int e , int num ) { int ans = 0 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( searchSpace [ mid ] >= num ) { ans = mid ; e = mid - 1 ; } else s = mid + 1 ; } return ans ; } static int longestSubArr ( int [ ] arr , int n ) { int [ ] searchSpace = new int [ n ] ; int [ ] index = new int [ n ] ; int j = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( j == 0 searchSpace [ j - 1 ] < arr [ i ] ) { searchSpace [ j ] = arr [ i ] ; index [ j ] = i ; j ++ ; } int idx = binarySearch ( searchSpace , 0 , j - 1 , arr [ i ] ) ; ans = Math . Max ( ans , i - index [ idx ] + 1 ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { - 5 , - 1 , 7 , 5 , 1 , - 2 } ; int n = arr . Length ; Console . Write ( longestSubArr ( arr , n ) ) ; } }
Print all pairs with given sum | C # program to find triplets in a given array whose sum is equal to given sum . ; Driver code
using System ; public class SumOfPairs { public void pairedElements ( int [ ] arr , int sum ) { int low = 0 ; int high = arr . Length - 1 ; while ( low < high ) { if ( arr [ low ] + arr [ high ] == sum ) { Console . WriteLine ( " The ▁ pair ▁ is ▁ : ▁ ( " + arr [ low ] + " , ▁ " + arr [ high ] + " ) " ) ; } if ( arr [ low ] + arr [ high ] > sum ) { high -- ; } else { low ++ ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , - 2 , 6 , 8 , 9 , 11 } ; Array . Sort ( arr ) ; SumOfPairs sp = new SumOfPairs ( ) ; sp . pairedElements ( arr , 6 ) ; } }
Check if a string is suffix of another | C # program to find if a string is suffix of another ; Driver code ; Test case - sensitive implementation of endsWith function
using System ; class GFG { static bool isSuffix ( string s1 , string s2 ) { int n1 = s1 . Length , n2 = s2 . Length ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 [ n1 - i - 1 ] != s2 [ n2 - i - 1 ] ) return false ; return true ; } public static void Main ( ) { string s1 = " geeks " , s2 = " geeksforgeeks " ; bool result = isSuffix ( s1 , s2 ) ; if ( result ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if all occurrences of a character appear together | C # program to find if all occurrences of a character appear together in a string . ; function to find if all occurrences of a character appear together in a string . ; To indicate if one or more occurrences of ' c ' are seen or not . ; Traverse given string ; If current character is same as c , we first check if c is already seen . ; If this is very first appearance of c , we traverse all consecutive occurrences . ; To indicate that character is seen once . ; Driver code
using System ; public class GFG { static bool checkIfAllTogether ( string s , char c ) { static bool checkIfAllTogether ( string s , char c ) { bool oneSeen = false ; int i = 0 , n = s . Length ; while ( i < n ) { if ( s [ i ] == c ) { if ( oneSeen == true ) return false ; while ( i < n && s [ i ] == c ) i ++ ; oneSeen = true ; } else i ++ ; } return true ; } public static void Main ( ) { string s = "110029" ; if ( checkIfAllTogether ( s , '1' ) ) Console . Write ( " Yes " ) ; else Console . Write ( " No " ) ; } }
Front and Back Search in unsorted array | C # program to implement front and back search ; Start searching from both ends ; Keep searching while two indexes do not cross . ; Driver Code
using System ; public class GFG { static bool search ( int [ ] arr , int n , int x ) { int front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } static public void Main ( ) { int [ ] arr = { 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 } ; int x = 130 ; int n = arr . Length ; if ( search ( arr , n , x ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Even | C # program to find max ( X , Y ) / min ( X , Y ) after P turns ; Driver code ; 1 st test case ; 2 nd test case
using System ; class GFG { public static int findValue ( int X , int Y , int P ) { if ( P % 2 == 0 ) return ( Math . Max ( X , Y ) / Math . Min ( X , Y ) ) ; else return ( Math . Max ( 2 * X , Y ) / Math . Min ( 2 * X , Y ) ) ; } public static void Main ( ) { int X = 1 , Y = 2 , P = 1 ; Console . WriteLine ( findValue ( X , Y , P ) ) ; X = 3 ; Y = 7 ; P = 2 ; Console . WriteLine ( findValue ( X , Y , P ) ) ; } }
The painter 's partition problem | A DP based C # program for painter 's partition problem ; function to calculate sum between two indices in array ; bottom up tabular dp ; initialize table ; base cases k = 1 ; n = 1 ; 2 to k partitions for ( int i = 2 ; i <= k ; i ++ ) { 2 to n boards ; track minimum ; i - 1 th separator before position arr [ p = 1. . j ] ; required ; Driver code ; Calculate size of array .
using System ; class GFG { static int sum ( int [ ] arr , int from , int to ) { int total = 0 ; for ( int i = from ; i <= to ; i ++ ) total += arr [ i ] ; return total ; } static int findMax ( int [ ] arr , int n , int k ) { int [ , ] dp = new int [ k + 1 , n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ 1 , i ] = sum ( arr , 0 , i - 1 ) ; for ( int i = 1 ; i <= k ; i ++ ) dp [ i , 1 ] = arr [ 0 ] ; for ( int j = 2 ; j <= n ; j ++ ) { int best = int . MaxValue ; for ( int p = 1 ; p <= j ; p ++ ) best = Math . Min ( best , Math . Max ( dp [ i - 1 , p ] , sum ( arr , p , j - 1 ) ) ) ; dp [ i , j ] = best ; } } return dp [ k , n ] ; } public static void ( ) { int [ ] arr = { 10 , 20 , 60 , 50 , 30 , 40 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( findMax ( arr , n , k ) ) ; } }
Counting cross lines in an array | C # program to count cross line in array ; function return count of cross line in an array ; Move elements of arr [ 0. . i - 1 ] , that are greater than key , to one position ahead of their current position ; increment cross line by one ; Driver code
using System ; class GFG { static int countCrossLine ( int [ ] arr , int n ) { int count_crossline = 0 ; int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; count_crossline ++ ; } arr [ j + 1 ] = key ; } return count_crossline ; } public static void Main ( ) { int [ ] arr = new int [ ] { 4 , 3 , 1 , 2 } ; int n = arr . Length ; Console . Write ( countCrossLine ( arr , n ) ) ; } }
Recursive Programs to find Minimum and Maximum elements of array | Recursive C # program to find maximum ; function to return maximum element using recursion ; if size = 0 means whole array has been traversed ; Driver code ; Function calling
using System ; class GFG { public static int findMaxRec ( int [ ] A , int n ) { if ( n == 1 ) return A [ 0 ] ; return Math . Max ( A [ n - 1 ] , findMaxRec ( A , n - 1 ) ) ; } static public void Main ( ) { int [ ] A = { 1 , 4 , 45 , 6 , - 50 , 10 , 2 } ; int n = A . Length ; Console . WriteLine ( findMaxRec ( A , n ) ) ; } }
Program to remove vowels from a String | C # program to remove vowels from a String ; Driver code
using System ; using System . Text . RegularExpressions ; class GFG { static String remVowel ( String str ) { str = Regex . Replace ( str , " [ aeiouAEIOU ] " , " " ) ; return str ; } public static void Main ( ) { String str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; Console . WriteLine ( remVowel ( str ) ) ; } }
Maximum money that can be collected by both the players in a game of removal of coins | C # program for the above approach ; Function to find the maximum score obtained by the players A and B ; Sort the array in descending order ; Stores the maximum amount of money obtained by A ; Stores the maximum amount of money obtained by B ; If the value of N is 1 ; Update the amountA ; Print the amount of money obtained by both players ; Update the amountA ; Update the amountB ; Traverse the array arr [ ] ; If i is an odd number ; Update the amountB ; Update the amountA ; Print the amount of money obtained by both the players ; Driver Code
using System ; class GFG { static void findAmountPlayers ( int [ ] arr , int N ) { Array . Sort ( arr ) ; Array . Reverse ( arr ) ; int amountA = 0 ; int amountB = 0 ; if ( N == 1 ) { amountA += arr [ 0 ] ; Console . WriteLine ( " ( A ▁ : ▁ " + amountA + " ) , ▁ " + " ( B ▁ : ▁ " + amountB + " ) " ) ; return ; } amountA = arr [ 0 ] ; amountB = arr [ 1 ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { amountB += arr [ i ] ; } else { amountA += arr [ i ] ; } } Console . WriteLine ( " ( A ▁ : ▁ " + amountA + " ) " ) ; Console . WriteLine ( " ( B ▁ : ▁ " + amountB + " ) " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 1 , 1 } ; int N = arr . Length ; findAmountPlayers ( arr , N ) ; } }
Sort an array of 0 s , 1 s , 2 s and 3 s | C # program for the above approach ; Function to sort the array having array element only 0 , 1 , 2 , and 3 ; Iterate until mid <= j ; If arr [ mid ] is 0 ; Swap integers at indices i and mid ; Increment i ; Increment mid ; Otherwise if the value of arr [ mid ] is 3 ; Swap arr [ mid ] and arr [ j ] ; Decrement j ; Otherwise if the value of arr [ mid ] is either 1 or 2 ; Increment the value of mid ; Iterate until i <= j ; If arr [ i ] the value of is 2 ; Swap arr [ i ] and arr [ j ] ; Decrement j ; Otherwise , increment i ; Print the sorted array arr [ ] ; Driver Code
using System ; class GFG { static void sortArray ( int [ ] arr , int N ) { int i = 0 , j = N - 1 , mid = 0 ; while ( mid <= j ) { if ( arr [ mid ] == 0 ) { int temp = arr [ i ] ; arr [ i ] = arr [ mid ] ; arr [ mid ] = temp ; i ++ ; mid ++ ; } else if ( arr [ mid ] == 3 ) { int temp = arr [ mid ] ; arr [ mid ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else if ( arr [ mid ] == 1 arr [ mid ] == 2 ) { mid ++ ; } } while ( i <= j ) { if ( arr [ i ] == 2 ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j -- ; } else { i ++ ; } } for ( int k = 0 ; k < N ; k ++ ) { Console . Write ( arr [ k ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 3 , 2 , 1 , 0 , 2 , 3 , 1 , 0 } ; int N = arr . Length ; sortArray ( arr , N ) ; } }