codeparrot/xlcost-text-to-code huggingface dataset:
Search an Element in Doubly Circular Linked List | C # Program to Reverse a List using Data Swapping ; Structure of a Node ; Function to insert a node at the end ; If the list is empty , create a single node circular and doubly list ; Find last node / ; Create Node dynamically ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Function to display the circular doubly linked list ; Function to search the particular element from the list ; Declare the temp variable ; Declare other control variable for the searching ; If start is null return - 1 ; Move the temp pointer until , temp . next doesn 't move  start address (Circular Fashion) ; Increment count for location ; If it is found raise the flag and break the loop ; Increment temp pointer ; Check whether last element in the list content the value if contain , raise a flag and increment count ; If flag is true , then element found , else not ; Driver code ; Start with the empty list / ; Insert 4. So linked list becomes 4. null ; Insert 5. So linked list becomes 4.5 ; Insert 7. So linked list becomes 4.5 . 7 ; Insert 8. So linked list becomes 4.5 . 7.8 ; Insert 6. So linked list becomes 4.5 . 7.8 . 6
using System ; class GFG { public class Node { public int data ; public Node next ; public Node prev ; } ; static Node insertNode ( Node start , int value ) { Node new_node = new Node ( ) ; if ( start == null ) { new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return new_node ; } Node last = ( start ) . prev ; new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return start ; } static void displayList ( Node start ) { Node temp = start ; while ( temp . next != start ) { Console . Write ( " { 0 } ▁ " , temp . data ) ; temp = temp . next ; } Console . Write ( " { 0 } ▁ " , temp . data ) ; } static int searchList ( Node start , int search ) { Node temp = start ; int count = 0 , flag = 0 , value ; if ( temp == null ) return - 1 ; else { while ( temp . next != start ) { count ++ ; if ( temp . data == search ) { flag = 1 ; count -- ; break ; } temp = temp . next ; } if ( temp . data == search ) { count ++ ; flag = 1 ; } if ( flag == 1 ) Console . WriteLine ( " STRNEWLINE " + search + " ▁ found ▁ at ▁ location ▁ " + count ) ; else Console . WriteLine ( " STRNEWLINE " + search + " ▁ not ▁ found " ) ; } return - 1 ; } public static void Main ( String [ ] args ) { Node start = null ; start = insertNode ( start , 4 ) ; start = insertNode ( start , 5 ) ; start = insertNode ( start , 7 ) ; start = insertNode ( start , 8 ) ; start = insertNode ( start , 6 ) ; Console . Write ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; displayList ( start ) ; searchList ( start , 5 ) ; } }
Reverse a doubly circular linked list | C # implementation to revesre a doubly circular linked list ; structure of a node of linked list ; function to create and return a new node ; Function to insert at the end ; If the list is empty , create a single node circular and doubly list ; Find last node / ; Start is going to be next of new_node ; Make new node previous of start ; Make last preivous of new node ; Make new node next of old last ; Uitlity function to revesre a doubly circular linked list ; Initialize a new head pointer ; get pointer to the the last node ; set ' curr ' to last node ; traverse list in backward direction ; insert ' curr ' at the end of the list starting with the ' new _ head ' pointer ; head pointer of the reversed list ; function to display a doubly circular list in forward and backward direction ; Driver code
using System ; class GFG { public class Node { public int data ; public Node next , prev ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; return newNode ; } static Node insertEnd ( Node head , Node new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } Node last = ( head ) . prev ; new_node . next = head ; ( head ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } static Node reverse ( Node head ) { if ( head == null ) return null ; Node new_head = null ; Node last = head . prev ; Node curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } static void display ( Node head ) { if ( head == null ) return ; Node temp = head ; Console . Write ( " Forward ▁ direction : ▁ " ) ; while ( temp . next != head ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . next ; } Console . Write ( temp . data + " ▁ " ) ; Node last = head . prev ; temp = last ; Console . Write ( " STRNEWLINE Backward ▁ direction : ▁ " ) ; while ( temp . prev != last ) { Console . Write ( temp . data + " ▁ " ) ; temp = temp . prev ; } Console . Write ( temp . data + " ▁ " ) ; } public static void Main ( String [ ] args ) { Node head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; Console . Write ( " Current ▁ list : STRNEWLINE " ) ; display ( head ) ; head = reverse ( head ) ; Console . Write ( " STRNEWLINE STRNEWLINE Reversed ▁ list : STRNEWLINE " ) ; display ( head ) ; } }
Sqrt ( or Square Root ) Decomposition | Set 2 ( LCA of Tree in O ( sqrt ( height ) ) time ) | Naive C # implementation to find LCA in a tree . ; Stores depth for each node ; Stores first parent for each node ; Marking parent for each node ; Marking depth for each node ; Propogating marking down the tree ; A dummy node ; Precalclating 1 ) depth . 2 ) parent . for each node ; Time Complexity : O ( Height of tree ) recursively jumps one node above till both the nodes become equal ; Driver Code ; Adding edges to the tree
using System ; using System . Collections ; class GFG { static int MAXN = 1001 ; static int [ ] depth = new int [ MAXN ] ; static int [ ] parent = new int [ MAXN ] ; static ArrayList [ ] adj = new ArrayList [ MAXN ] ; static void addEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static void dfs ( int cur , int prev ) { parent [ cur ] = prev ; depth [ cur ] = depth [ prev ] + 1 ; for ( int i = 0 ; i < adj [ cur ] . Count ; i ++ ) if ( ( int ) adj [ cur ] [ i ] != prev ) dfs ( ( int ) adj [ cur ] [ i ] , cur ) ; } static void preprocess ( ) { depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } static int LCANaive ( int u , int v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { int temp = u ; u = v ; v = temp ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } public static void Main ( string [ ] args ) { for ( int i = 0 ; i < MAXN ; i ++ ) adj [ i ] = new ArrayList ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; preprocess ( ) ; Console . WriteLine ( " LCA ( 11 , ▁ 8 ) ▁ : ▁ " + LCANaive ( 11 , 8 ) ) ; Console . WriteLine ( " LCA ( 3 , ▁ 13 ) ▁ : ▁ " + LCANaive ( 3 , 13 ) ) ; } }
Sqrt ( or Square Root ) Decomposition | Set 2 ( LCA of Tree in O ( sqrt ( height ) ) time ) | C # program to find LCA using Sqrt decomposition ; block size = Math . Sqrt ( height ) ; stores depth for each node ; stores first parent for each node ; stores first ancestor in previous block ; precalculating the required parameters associated with every node ; marking depth of cur node ; marking parent of cur node ; making jump_parent of cur node ; if it is first node of the block then its jump_parent is its cur parent ; if it is not the first node of this block then its jump_parent is jump_parent of its parent ; propogating the marking down the subtree ; using sqrt decomposition trick ; maintaining depth [ v ] > depth [ u ] ; climb to its jump parent ; u and v have same jump_parent ; precalclating 1 ) depth . 2 ) parent . 3 ) jump_parent for each node ; Driver code ; adding edges to the tree ; here we are directly taking height = 4 according to the given tree but we can pre - calculate height = max depth in one more dfs
using System ; using System . Collections . Generic ; public class GFG { static readonly int MAXN = 1001 ; static int block_sz ; static int [ ] depth = new int [ MAXN ] ; static int [ ] parent = new int [ MAXN ] ; static int [ ] jump_parent = new int [ MAXN ] ; static List < int > [ ] adj = new List < int > [ MAXN ] ; static void addEdge ( int u , int v ) { adj [ u ] . Add ( v ) ; adj [ v ] . Add ( u ) ; } static int LCANaive ( int u , int v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { int t = u ; u = v ; v = t ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } static void dfs ( int cur , int prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] = prev ; if ( depth [ cur ] % block_sz == 0 ) jump_parent [ cur ] = parent [ cur ] ; else jump_parent [ cur ] = jump_parent [ prev ] ; for ( int i = 0 ; i < adj [ cur ] . Count ; ++ i ) if ( adj [ cur ] [ i ] != prev ) dfs ( adj [ cur ] [ i ] , cur ) ; } static int LCASQRT ( int u , int v ) { while ( jump_parent [ u ] != jump_parent [ v ] ) { if ( depth [ u ] > depth [ v ] ) { int t = u ; u = v ; v = t ; } v = jump_parent [ v ] ; } return LCANaive ( u , v ) ; } static void preprocess ( int height ) { block_sz = ( int ) Math . Sqrt ( height ) ; depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } public static void Main ( String [ ] args ) { for ( int i = 0 ; i < adj . Length ; i ++ ) adj [ i ] = new List < int > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; int height = 4 ; preprocess ( height ) ; Console . Write ( " LCA ( 11,8 ) ▁ : ▁ " + LCASQRT ( 11 , 8 ) + " STRNEWLINE " ) ; Console . Write ( " LCA ( 3,13 ) ▁ : ▁ " + LCASQRT ( 3 , 13 ) + " STRNEWLINE " ) ; } }
Expected Number of Trials to get N Consecutive Heads | C # implementation of the above approach ; Driver Code ; Formula for number of trails for N consecutive heads
using System ; class GFG { public static void Main ( ) { int N = 3 ; Console . Write ( Math . Pow ( 2 , N + 1 ) - 2 ) ; } }
Find and Count total factors of co | C # implementation of the above approach ; Function to return the count of numbers which are divisible by both A and B in the range [ 1 , N ] in constant time ; Compute the count of numbers divisible by A in the range [ 1 , N ] ; Compute the count of numbers divisible by B in the range [ 1 , N ] ; Adding the counts which are divisible by A and B ; The above value might contain repeated values which are divisible by both A and B . Therefore , the count of numbers which are divisible by both A and B are found ; The count computed above is subtracted to compute the readonly count ; Function to return the sum of numbers which are divisible by both A and B in the range [ 1 , N ] ; Set to store the numbers so that the numbers are not repeated ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to find the numbers which are divisible by A and insert them into the set ; For loop to iterate through the set and find the sum ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int countOfNum ( int n , int a , int b ) { int cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = n / a ; cnt_of_b = n / b ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = n / ( a * b ) ; sum = sum - cnt_of_ab ; return sum ; } static int sumOfNum ( int n , int a , int b ) { int i ; int sum = 0 ; HashSet < int > ans = new HashSet < int > ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . Add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . Add ( i ) ; } foreach ( int it in ans ) { sum = sum + it ; } return sum ; } public static void Main ( String [ ] args ) { int N = 88 ; int A = 11 ; int B = 8 ; int count = countOfNum ( N , A , B ) ; int sumofnum = sumOfNum ( N , A , B ) ; Console . Write ( sumofnum % count ) ; } }
Find Range Value of the Expression | C # implementation of the approach ; Function to return the value of the given expression ; Value of the first term ; Value of the last term ; Driver code ; Get the result
using System ; public class GFG { static double get ( double L , double R ) { double x = 1.0 / L ; double y = 1.0 / ( R + 1.0 ) ; return ( x - y ) ; } public static void Main ( String [ ] args ) { int L = 6 , R = 12 ; double ans = get ( L , R ) ; Console . Write ( " { 0 : F2 } " , ans ) ; } }
Find next greater element with no consecutive 1 in it 's binary representation | C # implementation of the approach ; To store the pre - computed integers ; Function that returns true if the binary representation of x contains consecutive 1 s ; To store the previous bit ; Check whether the previous bit and the current bit are both 1 ; Update previous bit ; Go to the next bit ; Function to pre - compute the valid numbers from 0 to MAX ; Store all the numbers which do not have consecutive 1 s ; Function to return the minimum number greater than n which does not contain consecutive 1 s ; Search for the next greater element with no consecutive 1 s ; Function to perform the queries ; Driver code ; Pre - compute the numbers ; Perform the queries
using System ; using System . Collections . Generic ; class GFG { static int MAX = 100000 ; static List < int > v = new List < int > ( ) ; static int upper_bound ( List < int > ar , int k ) { int s = 0 ; int e = ar . Count ; while ( s != e ) { int mid = s + e >> 1 ; if ( ar [ mid ] <= k ) { s = mid + 1 ; } else { e = mid ; } } if ( s == ar . Count ) { return - 1 ; } return s ; } static int consecutiveOnes ( int x ) { int p = 0 ; while ( x > 0 ) { if ( x % 2 == 1 && p == 1 ) { return 1 ; } p = x % 2 ; x /= 2 ; } return 0 ; } static void preCompute ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) { if ( consecutiveOnes ( i ) == 0 ) { v . Add ( i ) ; } } } static int nextValid ( int n ) { int it = upper_bound ( v , n ) ; int val = v [ it ] ; return val ; } static void performQueries ( int [ ] queries , int q ) { for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( nextValid ( queries [ i ] ) ) ; } } static public void Main ( ) { int [ ] queries = { 4 , 6 } ; int q = queries . Length ; preCompute ( ) ; performQueries ( queries , q ) ; } }
Minimum given operations required to convert a given binary string to all 1 's | C # implementation of the approach ; Function to return the number of operations required ; ctr will store the number of consecutive ones at the end of the given binary string ; Loop to find number of 1 s at the end of the string ; If the current character is 1 ; If we encounter the first 0 from the LSB position then we 'll break the loop ; Number of operations required is ( l - ctr ) ; Function to remove leading zeroes from the string ; Loop until s [ i ] becomes not equal to 1 ; If we reach the end of the string , it means that string contains only 0 's ; Return the string without leading zeros ; Driver code ; Removing the leading zeroes
using System ; class GFG { static int changeToOnes ( String str ) { int i , l , ctr = 0 ; l = str . Length ; for ( i = l - 1 ; i >= 0 ; i -- ) { if ( str [ i ] == '1' ) ctr ++ ; else break ; } return l - ctr ; } static String removeZeroesFromFront ( String str ) { String s ; int i = 0 ; while ( i < str . Length && str [ i ] == '0' ) i ++ ; if ( i == str . Length ) s = "0" ; else s = str . Substring ( i , str . Length - i ) ; return s ; } public static void Main ( String [ ] args ) { String str = "10010111" ; str = removeZeroesFromFront ( str ) ; Console . WriteLine ( changeToOnes ( str ) ) ; } }
Minimum deletions required such that any number X will occur exactly X times | C # Implementation of above approach ; Function to return the minimum deletions required ; To store the frequency of the array elements ; Store frequency of each element ; To store the minimum deletions required ; Value ; It 's frequency ; If number less than or equal to it 's frequency ; Delete extra occurrences ; Delete every occurrence of x ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int MinDeletion ( int [ ] a , int n ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . ContainsKey ( a [ i ] ) ) { var val = mp [ a [ i ] ] ; mp . Remove ( a [ i ] ) ; mp . Add ( a [ i ] , val + 1 ) ; } else { mp . Add ( a [ i ] , 1 ) ; } } int ans = 0 ; foreach ( KeyValuePair < int , int > i in mp ) { int x = i . Key ; int frequency = i . Value ; if ( x <= frequency ) { ans += ( frequency - x ) ; } else ans += frequency ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = a . Length ; Console . WriteLine ( MinDeletion ( a , n ) ) ; } }
Concatenate strings in any order to get Maximum Number of " AB " | C # implementation of above approach ; Function to find maximum number of ABs ; variable A , B , AB for count strings that end with ' A ' but not end with ' B ' , ' B ' but does not end with ' A ' and ' B ' and ends with ' A ' respectively . ; ' AB ' is already present in string before concatenate them ; count of strings that begins with ' B ' and ends with 'A ; count of strings that begins with ' B ' but does not end with ' A ' ; count of strings that ends with ' A ' but not end with ' B ' ; updating the value of ans and add extra count of ' AB ' ; Driver Code
using System ; class GFG { static int maxCountAB ( string [ ] s , int n ) { int A = 0 , B = 0 , BA = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { string S = s [ i ] ; int L = S . Length ; for ( int j = 0 ; j < L - 1 ; j ++ ) { if ( S [ j ] == ' A ' && S [ j + 1 ] == ' B ' ) { ans ++ ; } } if ( S [ 0 ] == ' B ' && S [ L - 1 ] == ' A ' ) BA ++ ; else if ( S [ 0 ] == ' B ' ) B ++ ; else if ( S [ L - 1 ] == ' A ' ) A ++ ; } if ( BA == 0 ) ans += Math . Min ( B , A ) ; else if ( A + B == 0 ) ans += BA - 1 ; else ans += BA + Math . Min ( B , A ) ; return ans ; } public static void Main ( ) { string [ ] s = { " ABCA " , " BOOK " , " BAND " } ; int n = s . Length ; Console . WriteLine ( maxCountAB ( s , n ) ) ; } }
Minimum operations to make sum of neighbouring elements <= X | C # implementation of the approach ; Function to return the minimum number of operations required ; To store total operations required ; First make all elements equal to x which are currenctly greater ; Left scan the array ; Update the current element such that neighbouring sum is < x ; Driver code
using System ; class GFG { static int MinOperations ( int n , int x , int [ ] arr ) { int total = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] > x ) { int difference = arr [ i ] - x ; total = total + difference ; arr [ i ] = x ; } } for ( int i = 1 ; i < n ; ++ i ) { int LeftNeigbouringSum = arr [ i ] + arr [ i - 1 ] ; if ( LeftNeigbouringSum > x ) { int current_diff = LeftNeigbouringSum - x ; arr [ i ] = Math . Max ( 0 , arr [ i ] - current_diff ) ; total = total + current_diff ; } } return total ; } public static void Main ( String [ ] args ) { int X = 1 ; int [ ] arr = { 1 , 6 , 1 , 2 , 0 , 4 } ; int N = arr . Length ; Console . WriteLine ( MinOperations ( N , X , arr ) ) ; } }
Find the repeating and the missing number using two equations | C # implementation of the approach ; Function to print the required numbers ; Sum of first n natural numbers ; Sum of squares of first n natural numbers ; To store the sum and sum of squares of the array elements ; Driver code
using System ; public class GFG { static void findNumbers ( int [ ] arr , int n ) { int sumN = ( n * ( n + 1 ) ) / 2 ; int sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sum = 0 , sumSq = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; sumSq += ( int ) Math . Pow ( arr [ i ] , 2 ) ; } int B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; int A = sum - sumN + B ; Console . WriteLine ( " A ▁ = ▁ " + A + " B = " } public static void Main ( ) { int [ ] arr = { 1 , 2 , 2 , 3 , 4 } ; int n = arr . Length ; findNumbers ( arr , n ) ; } }
Lexicographically smallest string with given string as prefix | C # program for the above approach ; Function to find the whether the string temp starts with str or not ; Base Case ; Check for the corresponding characters in temp & str ; Function to find lexicographic smallest string consisting of the string str as prefix ; Sort the given array string arr [ ] ; If the i - th string contains given string as a prefix , then print the result ; If no string exists then return " - 1" ; Driver Code
using System ; class GFG { static bool is_prefix ( string temp , string str ) { if ( temp . Length < str . Length ) return false ; else { for ( int i = 0 ; i < str . Length ; i ++ ) { if ( str [ i ] != temp [ i ] ) return false ; } return true ; } } static string lexicographicallyString ( string [ ] input , int n , string str ) { Array . Sort ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { string temp = input [ i ] ; if ( is_prefix ( temp , str ) ) { return temp ; } } return " - 1" ; } public static void Main ( ) { string [ ] arr = { " apple " , " appe " , " apl " , " aapl " , " appax " } ; string S = " app " ; int N = 5 ; Console . WriteLine ( lexicographicallyString ( arr , N , S ) ) ; } }
Rearrange Array to find K using Binary Search algorithm without sorting | C # program for the above approach ; Function to rearrange the array ; Stores the rearranged array ; Stores whether the arrangement is possible or not ; Update K with the position of K ; Stores all elements lesser than and greater than in vector smaller and greater respectively ; Traverse the array [ ] arr ; If arr [ i ] is less than arr [ K ] ; Else ; Iterate unil low is less than or equal to high ; Stores mid point ; If mid is equal to K ; If mid is less than K ; If mid is greater than K ; If f is - 1 ; Iterate in the range [ 1 , N ] ; If ans [ i ] is equal to - 1 ; Print the rearranged array ; Driver code ; Input ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static void Rearrange ( int [ ] arr , int K , int N ) { int [ ] ans = new int [ N + 1 ] ; int f = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = - 1 ; } for ( int i = 0 ; i < arr . Length ; i ++ ) { if ( arr [ i ] == K ) { K = i ; break ; } } List < int > smaller = new List < int > ( ) ; List < int > greater = new List < int > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ K ] ) smaller . Add ( arr [ i ] ) ; else if ( arr [ i ] > arr [ K ] ) greater . Add ( arr [ i ] ) ; } int low = 0 , high = N - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid == K ) { ans [ mid ] = arr [ K ] ; f = 1 ; break ; } else if ( mid < K ) { if ( smaller . Count == 0 ) { break ; } ans [ mid ] = smaller [ smaller . Count - 1 ] ; smaller . RemoveAt ( smaller . Count - 1 ) ; low = mid + 1 ; } else { if ( greater . Count == 0 ) { break ; } ans [ mid ] = greater [ greater . Count - 1 ] ; greater . RemoveAt ( greater . Count - 1 ) ; high = mid - 1 ; } } if ( f == - 1 ) { Console . WriteLine ( - 1 ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ans [ i ] == - 1 ) { if ( smaller . Count > 0 ) { ans [ i ] = smaller [ smaller . Count - 1 ] ; smaller . RemoveAt ( smaller . Count - 1 ) ; } else if ( greater . Count > 0 ) { ans [ i ] = greater [ greater . Count - 1 ] ; greater . RemoveAt ( greater . Count - 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) Console . Write ( ans [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 2 , 5 , 3 , 8 } ; int K = 7 ; int N = arr . Length ; Rearrange ( arr , K , N ) ; } }