codeparrot/xlcost-text-to-code huggingface dataset:
Symmetric Tree ( Mirror Image of itself ) | C # program to check is binary tree is symmetric or not ; A Binary Tree Node ; returns true if trees with roots as root1 and root2 are mirror ; if both trees are empty , then they are mirror image ; For two trees to be mirror images , the following three conditions must be true 1 - Their root node 's key must be same  2 - left subtree of left tree and right  subtree of right tree have to be mirror images  3 - right subtree of left tree and left subtree  of right tree have to be mirror images ; if none of the above conditions is true then root1 and root2 are mirror images ; returns true if the tree is symmetric i . e mirror image of itself ; check if tree is mirror of itself ; Driver Code ; Let us construct the Tree shown in the above figure
using System ; class Node { public int key ; public Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class GFG { Node root ; Boolean isMirror ( Node node1 , Node node2 ) { if ( node1 == null && node2 == null ) return true ; if ( node1 != null && node2 != null && node1 . key == node2 . key ) return ( isMirror ( node1 . left , node2 . right ) && isMirror ( node1 . right , node2 . left ) ) ; return false ; } Boolean isSymmetric ( ) { return isMirror ( root , root ) ; } static public void Main ( String [ ] args ) { GFG tree = new GFG ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . right . right = new Node ( 3 ) ; Boolean output = tree . isSymmetric ( ) ; if ( output == true ) Console . WriteLine ( " Symmetric " ) ; else Console . WriteLine ( " Not ▁ symmetric " ) ; } }
Minimize cost to convert all occurrences of each distinct character to lowercase or uppercase | C # program for the above approach ; Function to find the minimum cost to convert all distinct characters to either uppercase or lowercase ; Store the size of the string ; Stores the frequency of lowercase & uppercase characters respectively ; Traverse the string S ; Update uppercase frequency of s [ i ] ; Otherwise , update lowercase frequency of s [ i ] ; Stores if the i - th character should be lowercase or not ; Iterate over the range [ 0 , 25 ] ; If the character is present in the string ; Store the cost to convert every occurence of i to uppercase and lowercase ; Update result [ i ] to 1 if lowercase cost is less ; Traverse the string S ; Store the index of the character ; Convert the current character to uppercase or lowercase according to the condition ; Update s [ i ] ; Update s [ i ] ; Print the modified string ; Driver Code
using System ; class GFG { static void minimumCost ( string str , int L , int U ) { int N = str . Length ; char [ ] s = str . ToCharArray ( ) ; string ans = " " ; int [ ] lowerFreq = new int [ 26 ] ; int [ ] upperFreq = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( char . IsUpper ( s [ i ] ) ) upperFreq [ s [ i ] - ' A ' ] ++ ; else lowerFreq [ s [ i ] - ' a ' ] ++ ; } int [ ] result = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( lowerFreq [ i ] != 0 upperFreq [ i ] != 0 ) { int costToUpper = U * lowerFreq [ i ] ; int costToLower = L * upperFreq [ i ] ; if ( costToLower < costToUpper ) { result [ i ] = 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { int index = 0 ; if ( char . IsLower ( s [ i ] ) ) index = s [ i ] - ' a ' ; else index = s [ i ] - ' A ' ; if ( result [ index ] == 1 ) { s [ i ] = char . ToLower ( s [ i ] ) ; } else { s [ i ] = char . ToUpper ( s [ i ] ) ; } } Console . WriteLine ( new string ( s ) ) ; } public static void Main ( string [ ] args ) { string S = " aabbAA " ; int L = 1 , U = 1 ; minimumCost ( S , L , U ) ; } }
Check if a matrix can be converted to another by repeatedly adding any value to X consecutive elements in a row or column | C # program for the above approach ; Function to check whether Matrix A [ ] [ ] can be transformed to Matrix B [ ] [ ] or not ; Traverse the matrix to perform horizontal operations ; Calculate difference ; Update next X elements ; Traverse the matrix to perform vertical operations ; Calculate difference ; Update next K elements ; A [ i ] [ j ] is not equal to B [ i ] [ j ] ; Conversion is not possible ; Conversion is possible ; Driver Code ; Input
using System ; using System . Collections . Generic ; class GFG { static int Check ( int [ , ] A , int [ , ] B , int M , int N , int X ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j <= N - X ; j ++ ) { if ( A [ i , j ] != B [ i , j ] ) { int diff = B [ i , j ] - A [ i , j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i , j + k ] = A [ i , j + k ] + diff ; } } } } for ( int i = 0 ; i <= M - X ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i , j ] != B [ i , j ] ) { int diff = B [ i , j ] - A [ i , j ] ; for ( int k = 0 ; k < X ; k ++ ) { A [ i + k , j ] = A [ i + k , j ] + diff ; } } } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i , j ] != B [ i , j ] ) { return 0 ; } } } return 1 ; } public static void Main ( ) { int M = 2 , N = 2 , X = 2 ; int [ , ] A = { { 0 , 0 } , { 0 , 0 } } ; int [ , ] B = { { 1 , 2 } , { 0 , 1 } } ; if ( Check ( A , B , M , N , X ) == 1 ) { Console . WriteLine ( " Yes " ) ; } else { Console . WriteLine ( " No " ) ; } } }
Kth smallest element in a row | C # program for kth largest element in a 2d array sorted row - wise and column - wise ; A structure to store entry of heap . The entry contains value from 2D array , row and column numbers of the value ; Value to be stored ; Row number of value in 2D array ; Column number of value in 2D array ; A utility function to swap two HeapNode items . ; A utility function to minheapify the node harr [ i ] of a heap stored in harr [ ] ; This function returns kth smallest element in a 2D array [ , ] mat ; k must be greater than 0 and smaller than n * n ; Create a min heap of elements from first row of 2D array ; Get current heap root ; Get next value from column of root 's  value. If the value stored at root was  last value in its column, then assign  INFINITE as next value ; Update heap root with next value ; Heapify root ; Return the value at last extracted root ; Driver code
using System ; class GFG { class HeapNode { public int val ; public int r ; public int c ; public HeapNode ( int val , int r , int c ) { this . val = val ; this . c = c ; this . r = r ; } } static void swap ( int i , int min , HeapNode [ ] arr ) { HeapNode temp = arr [ i ] ; arr [ i ] = arr [ min ] ; arr [ min ] = temp ; } static void minHeapify ( HeapNode [ ] harr , int i , int heap_size ) { int l = 2 * i + 1 ; int r = 2 * i + 2 ; int min = i ; if ( l < heap_size && harr [ l ] . val < harr [ i ] . val ) { min = l ; } if ( r < heap_size && harr [ r ] . val = 0 ) { minHeapify ( harr , i , n ) ; i -- ; } } public static int kthSmallest ( int [ , ] mat , int n , int k ) { if ( k n * n ) { return int . MaxValue ; } HeapNode [ ] harr = new HeapNode [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { harr [ i ] = new HeapNode ( mat [ 0 , i ] , 0 , i ) ; } buildHeap ( harr , n ) ; HeapNode hr = new HeapNode ( 0 , 0 , 0 ) ; for ( int i = 1 ; i < k ; i ++ ) { hr = harr [ 0 ] ; int nextVal = hr . r < n - 1 ? mat [ hr . r + 1 , hr . c ] : int . MaxValue ; harr [ 0 ] = new HeapNode ( nextVal , hr . r + 1 , hr . c ) ; minHeapify ( harr , 0 , n ) ; } return hr . val ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 25 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; int res = kthSmallest ( mat , 4 , 7 ) ; Console . Write ( "7th ▁ smallest ▁ element ▁ is ▁ " + res ) ; } }
Make all array elements equal by reducing array elements to half minimum number of times | C # program for the above approach ; Function to find minimum number of operations ; Initialize map ; Traverse the array ; Divide current array element until it reduces to 1 ; Traverse the map ; Find the maximum element having frequency equal to N ; Stores the minimum number of operations required ; Count operations required to convert current element to mx ; Print the answer ; Driver code ; Given array ; Size of the array
using System ; using System . Collections . Generic ; class GFG { static void minOperations ( int [ ] arr , int N ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res > 0 ) { if ( mp . ContainsKey ( res ) ) { mp [ res ] ++ ; } else { mp [ res ] = 1 ; } res /= 2 ; } } int mx = 1 ; foreach ( KeyValuePair < int , int > it in mp ) { if ( it . Value == N ) { mx = it . Key ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int res = arr [ i ] ; while ( res != mx ) { ans ++ ; res /= 2 ; } } Console . Write ( ans ) ; } static void Main ( ) { int [ ] arr = { 3 , 1 , 1 , 3 } ; int N = arr . Length ; minOperations ( arr , N ) ; } }
Find the player to reach at least N by multiplying with any value from given range | C # program for the above approach ; Function to find the winner ; Backtrack from N to 1 ; Driver Code
using System ; class GFG { static char Winner ( int N ) { bool player = true ; while ( N > 1 ) { int den = ( player ) ? 9 : 2 ; int X = N / den ; int Y = N % den ; N = ( Y > 0 ) ? X + 1 : X ; player = ! player ; } if ( player ) return ' B ' ; else return ' A ' ; } static public void Main ( ) { int N = 10 ; Console . WriteLine ( Winner ( N ) ) ; } }
Program to find largest element in an array | C # Program to find maximum in arr [ ] ; Method to find maximum in arr [ ] ; Initialize maximum element ; Traverse array elements from second and compare every element with current max ; Driver method
using System ; class GFG { static int [ ] arr = { 10 , 324 , 45 , 90 , 9808 } ; static int largest ( ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . Length ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } public static void Main ( ) { Console . WriteLine ( " Largest ▁ in ▁ given ▁ " + " array ▁ is ▁ " + largest ( ) ) ; } }
Minimize steps required to reach the value N | C # program for the above approach ; Function to find the minimum steps required to reach N by either moving i steps forward or 1 steps backward ; Stores the required count ; IF total moves required is less than double of N ; Update steps ; Steps required to reach N ; Update steps ; Driver code ; Given value of N
using System ; class GFG { static void minimumsteps ( int N ) { int steps = 0 ; while ( steps * ( steps + 1 ) < 2 * N ) { steps ++ ; } if ( steps * ( steps + 1 ) / 2 == N + 1 ) { steps ++ ; } Console . WriteLine ( steps ) ; } static public void Main ( ) { int N = 18 ; minimumsteps ( N ) ; } }
Program to find largest element in an array | C # program to find maximum in arr [ ] of size n ; returns maximum in arr [ ] of size n ; Driver code
using System ; using System . Linq ; public class GFG { static int largest ( int [ ] arr , int n ) { return arr . Max ( ) ; } static public void Main ( ) { int [ ] arr = { 10 , 324 , 45 , 90 , 9808 } ; int n = arr . Length ; Console . WriteLine ( largest ( arr , n ) ) ; } }
Find the largest three distinct elements in an array | C # code to find largest three elements in an array ; It uses Tuned Quicksort with ; avg . case Time complexity = O ( nLogn ) ; to handle duplicate values ; Driver code
using System ; class GFG { void find3largest ( int [ ] arr ) { Array . Sort ( arr ) ; int n = arr . Length ; int check = 0 , count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( count < 4 ) { if ( check != arr [ n - i ] ) { Console . Write ( arr [ n - i ] + " ▁ " ) ; check = arr [ n - i ] ; count ++ ; } } else break ; } } public static void Main ( ) { GFG obj = new GFG ( ) ; int [ ] arr = { 12 , 45 , 1 , - 1 , 45 , 54 , 23 , 5 , 0 , - 10 } ; obj . find3largest ( arr ) ; } }
Minimum characters required to be removed to make frequency of each character unique | C # program to implement the above approach ; Function to find the minimum count of characters required to be deleted to make frequencies of all characters unique ; Stores frequency of each distinct character of str ; Store frequency of each distinct character such that the largest frequency is present at the top ; Stores minimum count of characters required to be deleted to make frequency of each character unique ; Traverse the String ; Update frequency of str [ i ] ; Traverse the map ; Insert current frequency into pq ; Traverse the priority_queue ; Stores topmost element of pq ; Pop the topmost element ; If pq is empty ; Return cntChar ; If frequent and topmost element of pq are equal ; If frequency of the topmost element is greater than 1 ; Insert the decremented value of frequent ; Update cntChar ; Driver Code ; Stores length of str
using System ; using System . Collections . Generic ; class GFG { static int minCntCharDeletionsfrequency ( char [ ] str , int N ) { Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; List < int > pq = new List < int > ( ) ; int cntChar = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . ContainsKey ( str [ i ] ) ) { mp [ str [ i ] ] ++ ; } else { mp . Add ( str [ i ] , 1 ) ; } } foreach ( KeyValuePair < char , int > it in mp ) { pq . Add ( it . Value ) ; } pq . Sort ( ) ; pq . Reverse ( ) ; while ( pq . Count != 0 ) { pq . Sort ( ) ; pq . Reverse ( ) ; int frequent = pq [ 0 ] ; pq . RemoveAt ( 0 ) ; if ( pq . Count == 0 ) { return cntChar ; } if ( frequent == pq [ 0 ] ) { if ( frequent > 1 ) { pq . Add ( frequent - 1 ) ; pq . Sort ( ) ; pq . Reverse ( ) ; } cntChar ++ ; } } return cntChar ; } public static void Main ( String [ ] args ) { String str = " abbbcccd " ; int N = str . Length ; Console . Write ( minCntCharDeletionsfrequency ( str . ToCharArray ( ) , N ) ) ; } }
Replace specified matrix elements such that no two adjacent elements are equal | C # program for the above approach ; Function to check if current position is safe or not ; Directions for adjacent cells ; Check if any adjacent cell is same ; Current index is valid ; Recursive function for backtracking ; Free cell ; All positions covered ; If position is valid for 1 ; If position is valid for 2 ; Recursive call for next unoccupied position ; If above conditions fails ; Function to print valid matrix ; Driver Code ; Give dimensions ; Function call
using System ; class GFG { public static bool issafe ( char [ , ] v , int i , int j , int n , int m , char ch ) { int [ ] rowN = { 1 , - 1 , 0 , 0 } ; int [ ] colN = { 0 , 0 , 1 , - 1 } ; for ( int k = 0 ; k < 4 ; k ++ ) { if ( i + rowN [ k ] >= 0 && i + rowN [ k ] < n && j + colN [ k ] >= 0 && j + colN [ k ] < m && v [ ( i + rowN [ k ] ) , ( j + colN [ k ] ) ] == ch ) { return false ; } } return true ; } public static bool place ( char [ , ] v , int n , int m ) { int i = 0 , j = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( v [ i , j ] == ' F ' ) { break ; } } if ( j != m ) { break ; } } if ( i == n && j == m ) { return true ; } if ( issafe ( v , i , j , n , m , '1' ) ) { v [ i , j ] = '1' ; if ( place ( v , n , m ) ) { return true ; } v [ i , j ] = ' F ' ; } if ( issafe ( v , i , j , n , m , '2' ) ) { v [ i , j ] = '2' ; if ( place ( v , n , m ) ) { return true ; } v [ i , j ] = ' F ' ; } return false ; } public static void printMatrix ( char [ , ] arr , int n , int m ) { place ( arr , n , m ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { Console . Write ( arr [ i , j ] ) ; } Console . WriteLine ( ) ; } } public static void Main ( ) { char [ , ] arr = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } , } ; int n = 4 , m = 4 ; printMatrix ( arr , n , m ) ; } }
Program for Mean and median of an unsorted array | C # program to find mean and median of an array ; Function for calculating mean ; Function for calculating median ; First we sort the array ; check for even case ; Driver Code ; Function call
using System ; class GFG { public static double findMean ( int [ ] a , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static double findMedian ( int [ ] a , int n ) { Array . Sort ( a ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } public static void Main ( ) { int [ ] a = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . Length ; Console . Write ( " Mean ▁ = ▁ " + findMean ( a , n ) + " STRNEWLINE " ) ; Console . Write ( " Median ▁ = ▁ " + findMedian ( a , n ) + " STRNEWLINE " ) ; } }
Find sum of all left leaves in a given Binary Tree | C # program to find sum of all left leaves ; A binary tree node ; A constructor to create a new Node ; Return the sum of left leaf nodes ; Using a stack_ for Depth - First Traversal of the tree ; sum holds the sum of all the left leaves ; Check if currentNode 's left  child is a leaf node ; if currentNode is a leaf , add its data to the sum ; Driver Code
using System ; using System . Collections . Generic ; class GFG { public class Node { public int key ; public Node left , right ; public Node ( int key_ ) { this . key = key_ ; this . left = null ; this . right = null ; } } ; static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Stack < Node > stack_ = new Stack < Node > ( ) ; stack_ . Push ( root ) ; int sum = 0 ; while ( stack_ . Count > 0 ) { Node currentNode = stack_ . Peek ( ) ; stack_ . Pop ( ) ; if ( currentNode . left != null ) { stack_ . Push ( currentNode . left ) ; if ( currentNode . left . left == null && currentNode . left . right == null ) { sum = sum + currentNode . left . key ; } } if ( currentNode . right != null ) stack_ . Push ( currentNode . right ) ; } return sum ; } public static void Main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; Console . Write ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + " STRNEWLINE " ) ; } }
Find a pair with sum N having minimum absolute difference | C # program to implement the above approach ; Function to find the value of X and Y having minimum value of Math . abs ( X - Y ) ; If N is an odd number ; If N is an even number ; Driver Code
using System ; class GFG { static void findXandYwithminABSX_Y ( int N ) { if ( N % 2 == 1 ) { Console . Write ( ( N / 2 ) + " ▁ " + ( N / 2 + 1 ) ) ; } else { Console . Write ( ( N / 2 - 1 ) + " ▁ " + ( N / 2 + 1 ) ) ; } } public static void Main ( ) { int N = 12 ; findXandYwithminABSX_Y ( N ) ; } }
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence | C # program for the above approach ; Function to find the maximum sum subsequence ; Base Case ; If current state is already calculated then use it ; If current element is positive ; Update ans and recursively call with update value of flag ; Else current element is negative ; Update ans and recursively call with update value of flag ; Return maximum sum subsequence ; Function that finds the maximum sum of element of the subsequence with alternate + ve and - ve signs ; Create auxiliary array dp [ ] [ ] ; Initialize dp [ ] [ ] ; Function Call ; Driver Code ; Given array arr [ ] ; Function call
using System ; class GFG { static int findMax ( int [ ] a , int [ , ] dp , int i , int flag ) { if ( i == ( int ) a . Length ) { return 0 ; } if ( dp [ i , flag ] != - 1 ) { return dp [ i , flag ] ; } int ans ; if ( flag == 0 ) { ans = Math . Max ( findMax ( a , dp , i + 1 , 0 ) , a [ i ] + findMax ( a , dp , i + 1 , 1 ) ) ; } else { ans = Math . Max ( findMax ( a , dp , i + 1 , 1 ) , - 1 * a [ i ] + findMax ( a , dp , i + 1 , 0 ) ) ; } return dp [ i , flag ] = ans ; } static void findMaxSumUtil ( int [ ] arr , int N ) { int [ , ] dp = new int [ N , 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = - 1 ; } } Console . WriteLine ( findMax ( arr , dp , 0 , 0 ) ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 1 , 2 } ; int N = arr . Length ; findMaxSumUtil ( arr , N ) ; } }
Find the player to last modify a string such that even number of consonants and no vowels are left in the string | C # program for the above approach ; Function to find a winner of the game if both the player plays optimally ; Stores the count of vowels and consonants ; Traverse the String ; Check if character is vowel ; Increment vowels count ; Otherwise increment the consonants count ; Check if Player B wins ; Check if Player A wins ; Check if Player A wins ; If game ends in a Draw ; Driver Code ; Given String s ; Function Call
using System ; class GFG { static void findWinner ( char [ ] s ) { int vowels_count = 0 , consonants_count = 0 ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { vowels_count ++ ; } else { consonants_count ++ ; } } if ( vowels_count == 0 ) { if ( consonants_count % 2 == 0 ) { Console . Write ( " Player ▁ B " ) ; } else { Console . Write ( " Player ▁ A " ) ; } } else if ( vowels_count == 1 && consonants_count % 2 != 0 ) { Console . Write ( " Player ▁ A " ) ; } else { Console . Write ( " D " ) ; } } public static void Main ( String [ ] args ) { String s = " abcd " ; findWinner ( s . ToCharArray ( ) ) ; } }
Lexicographically smallest permutation of size A having B integers exceeding all preceding integers | C # program for the above approach ; Function to find lexicographically smallest permutation of [ 1 , A ] having B integers greater than all the previous elements ; Initial array ; Resultant array ; Append the first ( B - 1 ) elements to the resultant array ; Append the maximum from the initial array ; Copy the remaining elements ; Print the answer ; Driver Code
using System . Collections . Generic ; using System ; class GFG { static void getSmallestArray ( int A , int B ) { List < int > arr = new List < int > ( ) ; for ( int i = 0 ; i < A ; i ++ ) arr . Add ( i + 1 ) ; List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < B - 1 ; i ++ ) ans . Add ( arr [ i ] ) ; ans . Add ( arr [ A - 1 ] ) ; for ( int i = B ; i < A ; i ++ ) ans . Add ( arr [ i - 1 ] ) ; for ( int i = 0 ; i < A ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; } public static void Main ( ) { int A = 3 ; int B = 2 ; getSmallestArray ( A , B ) ; } }
k smallest elements in same order using O ( 1 ) extra space | C # for printing smallest k numbers in order ; Function to print smallest k numbers in arr [ 0. . n - 1 ] ; For each arr [ i ] find whether it is a part of n - smallest with insertion sort concept ; Find largest from top n - element ; If largest is greater than arr [ i ] shift all element one place left ; make arr [ k - 1 ] = arr [ i ] ; print result ; Driver function
using System ; public class GfG { public static void printSmall ( int [ ] arr , int n , int k ) { for ( int i = k ; i < n ; ++ i ) { int max_var = arr [ k - 1 ] ; int pos = k - 1 ; for ( int j = k - 2 ; j >= 0 ; j -- ) { if ( arr [ j ] > max_var ) { max_var = arr [ j ] ; pos = j ; } } if ( max_var > arr [ i ] ) { int j = pos ; while ( j < k - 1 ) { arr [ j ] = arr [ j + 1 ] ; j ++ ; } arr [ k - 1 ] = arr [ i ] ; } } for ( int i = 0 ; i < k ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = 10 ; int k = 5 ; printSmall ( arr , n , k ) ; } }