codeparrot/xlcost-text-to-code huggingface dataset:
Find the Rotation Count in Rotated Sorted array | C # program to find number of rotations in a sorted and rotated array . ; Returns count of rotations for an array which is first sorted in ascending order , then rotated ; This condition is needed to handle the case when array is not rotated at all ; If there is only one element left ; Find mid ; Check if element ( mid + 1 ) is minimum element . Consider the cases like { 3 , 4 , 5 , 1 , 2 } ; Check if mid itself is minimum element ; Decide whether we need to go to left half or right half ; Driver program to test above functions
using System ; class BinarySearch { static int countRotations ( int [ ] arr , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } public static void Main ( ) { int [ ] arr = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = arr . Length ; Console . WriteLine ( countRotations ( arr , 0 , n - 1 ) ) ; } }
Quickly find multiple left rotations of an array | Set 1 | C # implementation of left rotation of an array K number of times ; Fills temp [ ] with two copies of arr [ ] ; Store arr [ ] elements at i and i + n ; Function to left rotate an array k time ; Starting position of array after k rotations in temp [ ] will be k % n ; Print array after k rotations ; Driver program
using System ; class LeftRotate { static void preprocess ( int [ ] arr , int n , int [ ] temp ) { for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } static void leftRotate ( int [ ] arr , int n , int k , int [ ] temp ) { int start = k % n ; for ( int i = start ; i < start + n ; i ++ ) Console . Write ( temp [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . Length ; int [ ] temp = new int [ 2 * n ] ; preprocess ( arr , n , temp ) ; int k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ; } }
Arrange the array such that upon performing given operations an increasing order is obtained | C # program to find the desired output after performing given operations ; Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; Insert last element at ith position ; Print desired Array ; Driver code ; Given Array
using System ; class GFG { public static void Desired_Array ( int [ ] v ) { int n = v . Length ; Array . Sort ( v ) ; int i = n - 1 ; while ( i > 0 ) { int p = v [ n - 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { v [ j ] = v [ j - 1 ] ; } v [ i ] = p ; i -- ; } for ( int x = 0 ; x < v . Length ; x ++ ) { Console . Write ( v [ x ] + " ▁ " ) ; } Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] v = { 1 , 2 , 3 , 4 , 5 } ; Desired_Array ( v ) ; int [ ] v1 = { 1 , 12 , 2 , 10 , 4 , 16 , 6 } ; Desired_Array ( v1 ) ; } }
Quickly find multiple left rotations of an array | Set 1 | C # implementation of left rotation of an array K number of times ; Function to left rotate an array k times ; Print array after k rotations ; Driver Code
using System ; class GFG { static void leftRotate ( int [ ] arr , int n , int k ) { for ( int i = k ; i < k + n ; i ++ ) Console . Write ( arr [ i % n ] + " ▁ " ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . Length ; int k = 2 ; leftRotate ( arr , n , k ) ; Console . WriteLine ( ) ; k = 3 ; leftRotate ( arr , n , k ) ; Console . WriteLine ( ) ; k = 4 ; leftRotate ( arr , n , k ) ; Console . WriteLine ( ) ; } }
How to sort an array in a single loop ? | C # code to sort an array of integers with the help of single loop ; Function for Sorting the array using a single loop ; Finding the length of array ' arr ' ; Sorting using a single loop ; Checking the condition for two simultaneous elements of the array ; Swapping the elements . ; updating the value of j = - 1 so after getting updated for j ++ in the loop it becomes 0 and the loop begins from the start . ; Driver Code ; Declaring an integer array of size 11. ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
using System ; class GFG { public static int [ ] sortArrays ( int [ ] arr ) { int length = arr . Length ; for ( int j = 0 ; j < length - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { int temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 99 , 9 , 8 , 7 , 6 , 0 , 5 , 4 , 3 } ; Console . WriteLine ( " Original ▁ array : ▁ " + String . Join ( " , ▁ " , arr ) ) ; arr = sortArrays ( arr ) ; Console . WriteLine ( " Sorted ▁ array : ▁ " + String . Join ( " , ▁ " , arr ) ) ; } }
How to sort an array in a single loop ? | C # code to sort an array of Strings with the help of single loop ; Function for Sorting the array using a single loop ; Finding the length of array ' arr ' ; Sorting using a single loop ; Type Conversion of char to int . ; Comparing the ascii code . ; Swapping of the characters ; Declaring main method ; Declaring a String ; Declaring a character array to store characters of geeks in it . ; Printing the original Array . ; Sorting the array using a single loop ; Printing the sorted array .
using System ; class GFG { public static char [ ] sortArrays ( char [ ] arr ) { int length = arr . Length ; for ( int j = 0 ; j < arr . Length - 1 ; j ++ ) { int d1 = arr [ j ] ; int d2 = arr [ j + 1 ] ; if ( d1 > d2 ) { char temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; j = - 1 ; } } return arr ; } public static void Main ( String [ ] args ) { String geeks = " GEEKSFORGEEKS " ; char [ ] arr = geeks . ToCharArray ( ) ; Console . WriteLine ( " Original ▁ array : ▁ [ " + String . Join ( " , ▁ " , arr ) + " ] " ) ; arr = sortArrays ( arr ) ; Console . WriteLine ( " Sorted ▁ array : ▁ [ " + String . Join ( " , ▁ " , arr ) + " ] " ) ; } }
Convert given Matrix into sorted Spiral Matrix | C # program to Convert given Matrix into sorted Spiral Matrix ; Function to convert the array to Spiral ; For Array pointer ; k - starting row index m - ending row index l - starting column index n - ending column index ; Print the first row from the remaining rows ; Print the last column from the remaining columns ; Print the last row from the remaining rows ; Print the first column from the remaining columns ; Function to convert 2D array to 1D array ; Store value 2D Matrix To 1D array ; Function to print the Matrix ; Print Spiral Matrix ; Function to sort the array ; Sort array Using InBuilt Function ; Function to Convert given Matrix into sorted Spiral Matrix ; Driver code
using System ; class GFG { static int MAX = 1000 ; static void ToSpiral ( int m , int n , int [ ] Sorted , int [ , ] a ) { int index = 0 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k , i ] = Sorted [ index ] ; index ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i , n - 1 ] = Sorted [ index ] ; index ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 , i ] = Sorted [ index ] ; index ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i , l ] = Sorted [ index ] ; index ++ ; } l ++ ; } } } public static int [ ] convert2Dto1D ( int [ , ] y , int m , int n ) { int index = 0 ; int [ ] x = new int [ m * n ] ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { x [ index ] = y [ i , j ] ; index ++ ; } } return x ; } public static void PrintMatrix ( int [ , ] a , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { Console . Write ( a [ i , j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } public static int [ ] SortArray ( int [ ] x ) { Array . Sort ( x ) ; return x ; } public static void convertMatrixToSortedSpiral ( int [ , ] y , int m , int n ) { int [ , ] a = new int [ MAX , MAX ] ; int [ ] x = new int [ m * n ] ; x = convert2Dto1D ( y , m , n ) ; x = SortArray ( x ) ; ToSpiral ( m , n , x , a ) ; PrintMatrix ( a , m , n ) ; } public static void Main ( String [ ] args ) { int m = 4 , n = 3 ; int [ , ] y = { { 2 , 5 , 12 } , { 22 , 45 , 55 } , { 1 , 6 , 8 } , { 13 , 56 , 10 } } ; convertMatrixToSortedSpiral ( y , m , n ) ; } }
Reversal algorithm for right rotation of an array | C # program for right rotation of an array ( Reversal Algorithm ) ; Function to reverse arr [ ] from index start to end ; Function to right rotate arr [ ] of size n by d ; Function to print an array ; Driver code
using System ; class GFG { static void reverseArray ( int [ ] arr , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void rightRotate ( int [ ] arr , int d , int n ) { reverseArray ( arr , 0 , n - 1 ) ; reverseArray ( arr , 0 , d - 1 ) ; reverseArray ( arr , d , n - 1 ) ; } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = arr . Length ; int k = 3 ; rightRotate ( arr , k , n ) ; printArray ( arr , n ) ; } }
Print the nodes at odd levels of a tree | Iterative C # program to print odd level nodes ; Iterative method to do level order traversal line by line ; Base Case ; Create an empty queue for level order traversal ; Enqueue root and initialize level as odd ; nodeCount ( queue size ) indicates number of nodes at current level . ; Dequeue all nodes of current level and Enqueue all nodes of next level ; Utility method to create a node ; Driver code
using System ; using System . Collections . Generic ; public class GfG { public class Node { public int data ; public Node left , right ; } static void printOddNodes ( Node root ) { if ( root == null ) return ; Queue < Node > q = new Queue < Node > ( ) ; q . Enqueue ( root ) ; bool isOdd = true ; while ( true ) { int nodeCount = q . Count ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . Peek ( ) ; if ( isOdd == true ) Console . Write ( node . data + " ▁ " ) ; q . Dequeue ( ) ; if ( node . left != null ) q . Enqueue ( node . left ) ; if ( node . right != null ) q . Enqueue ( node . right ) ; nodeCount -- ; } isOdd = ! isOdd ; } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void Main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; printOddNodes ( root ) ; } }
Find a rotation with maximum hamming distance | C # program to Find another array such that the hamming distance from the original array is maximum ; Return the maximum hamming distance of a rotation ; arr [ ] to brr [ ] two times so that we can traverse through all rotations . ; We know hamming distance with 0 rotation would be 0. ; We try other rotations one by one and compute Hamming distance of every rotation ; We can never get more than n . ; driver code
using System ; class GFG { static int maxHamming ( int [ ] arr , int n ) { int [ ] brr = new int [ 2 * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; int maxHam = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int currHam = 0 ; for ( int j = i , k = 0 ; j < ( i + n ) ; j ++ , k ++ ) if ( brr [ j ] != arr [ k ] ) currHam ++ ; if ( currHam == n ) return n ; maxHam = Math . Max ( maxHam , currHam ) ; } return maxHam ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 6 , 8 } ; int n = arr . Length ; Console . Write ( maxHamming ( arr , n ) ) ; } }
Print left rotation of array in O ( n ) time and O ( 1 ) space | C # implementation of left rotation of an array K number of times ; Function to leftRotate array multiple times ; To get the starting point of rotated array ; Prints the rotated array from start position ; Driver Code ; Function Call ; Function Call ; Function Call
using System ; class GFG { static void leftRotate ( int [ ] arr , int n , int k ) { int mod = k % n ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ ( i + mod ) % n ] + " ▁ " ) ; Console . WriteLine ( ) ; } static public void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . Length ; int k = 2 ; leftRotate ( arr , n , k ) ; k = 3 ; leftRotate ( arr , n , k ) ; k = 4 ; leftRotate ( arr , n , k ) ; } }
Find element at given index after a number of rotations | C # code to rotate an array and answer the index query ; Function to compute the element at given index ; Range [ left ... right ] ; Rotation will not have any effect ; Returning new element ; Driver Code ; No . of rotations ; Ranges according to 0 - based indexing
using System ; class GFG { static int findElement ( int [ ] arr , int [ , ] ranges , int rotations , int index ) { for ( int i = rotations - 1 ; i >= 0 ; i -- ) { int left = ranges [ i , 0 ] ; int right = ranges [ i , 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int rotations = 2 ; int [ , ] ranges = { { 0 , 2 } , { 0 , 3 } } ; int index = 1 ; Console . Write ( findElement ( arr , ranges , rotations , index ) ) ; } }
Sort an alphanumeric string such that the positions of alphabets and numbers remain unchanged | C # implementation of the approach ; Function that returns the string s in sorted form such that the positions of alphabets and numeric digits remain unchanged ; String to character array ; Sort the array ; Count of alphabets and numbers ; Get the index from where the alphabets start ; Now replace the string with sorted string ; If the position was occupied by an alphabet then replace it with alphabet ; Else replace it with a number ; Return the sorted string ; Driver code
using System ; class GFG { static string sort ( string s ) { char [ ] c = new char [ s . Length + 1 ] ; c = s . ToCharArray ( ) ; Array . Sort ( c ) ; int al_c = 0 , nu_c = 0 ; while ( c [ al_c ] < 97 ) al_c ++ ; for ( int i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] < 97 ) s = s . Substring ( 0 , i ) + c [ nu_c ++ ] + s . Substring ( i + 1 ) ; else s = s . Substring ( 0 , i ) + c [ al_c ++ ] + s . Substring ( i + 1 ) ; } return s ; } public static void Main ( ) { string s = " d4c3b2a1" ; Console . WriteLine ( sort ( s ) ) ; } }
Split the array and add the first part to the end | C # program to split array and move first part to end . ; Rotate array by 1. ; Driver code
using System ; class GFG { public static void splitArr ( int [ ] arr , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ 0 ] ; for ( int j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } public static void Main ( ) { int [ ] arr = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int position = 2 ; splitArr ( arr , 6 , position ) ; for ( int i = 0 ; i < n ; ++ i ) Console . Write ( arr [ i ] + " ▁ " ) ; } }
Split the array and add the first part to the end | C # program to split array and move first part to end . ; Function to spilt array and move first part to end ; make a temporary array with double the size ; copy array element in to new array twice ; Driver code
using System ; class GFG { public static void SplitAndAdd ( int [ ] A , int length , int rotation ) { int [ ] tmp = new int [ length * 2 ] ; Array . Copy ( A , 0 , tmp , 0 , length ) ; Array . Copy ( A , 0 , tmp , length , length ) ; for ( int i = rotation ; i < rotation + length ; i ++ ) { A [ i - rotation ] = tmp [ i ] ; } } public static void Main ( string [ ] args ) { int [ ] arr = new int [ ] { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . Length ; int position = 2 ; SplitAndAdd ( arr , n , position ) ; for ( int i = 0 ; i < n ; ++ i ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Shell | C # implementation of Shell - Metzner Sort ; Function to swap two elements ; Function to sort arr [ ] using Shell Metzner sort ; Declare variables ; Set initial step size to the size of the array ; Step size decreases by half each time ; k is the upper limit for j ; j is the starting point ; i equals to smaller value ; l equals to larger value ; Compare and swap arr [ i ] with arr [ l ] ; Decrease smaller value by step size ; Increment the lower limit of i ; Function to print the contents of an array ; Driver code ; Sort the array using Shell Metzner Sort ; Print the sorted array
using System ; class GFG { static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort_shell_metzner ( int [ ] arr , int n ) { int i , j , k , l , m , temp ; m = n ; while ( m > 0 ) { m /= 2 ; k = n - m ; j = 0 ; do { i = j ; do { l = i + m ; if ( l < n && arr [ i ] > arr [ l ] ) { swap ( arr , i , l ) ; i -= m ; } else { break ; } } while ( i >= 0 ) ; j ++ ; } while ( j <= k ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 0 , - 2 , 8 , 5 , 1 } ; int n = arr . Length ; sort_shell_metzner ( arr , n ) ; printArray ( arr , n ) ; } }
Rearrange an array such that arr [ i ] = i | C # program for above approach ; Function to tranform the array ; Iterate over the array ; Checf is any ar [ j ] exists such that ar [ j ] is equal to i ; Iterate over array ; If not present ; Print the output ; Driver Code ; Function Call
using System ; class GFG { static void fixArray ( int [ ] ar , int n ) { int i , j , temp ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ar [ j ] == i ) { temp = ar [ j ] ; ar [ j ] = ar [ i ] ; ar [ i ] = temp ; break ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( ar [ i ] != i ) { ar [ i ] = - 1 ; } } Console . WriteLine ( " Array ▁ after ▁ Rearranging " ) ; for ( i = 0 ; i < n ; i ++ ) { Console . Write ( ar [ i ] + " ▁ " ) ; } } static void Main ( ) { int [ ] ar = { - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 } ; int n = ar . Length ; fixArray ( ar , n ) ; } }
Print all full nodes in a Binary Tree | C # program to find the all full nodes in a given binary tree ; A binary tree node ; Traverses given tree in Inorder fashion and prints all nodes that have both children as non - empty . ; Driver program to test above function
using System ; class Node { public int data ; public Node left , right ; public Node ( int data ) { left = right = null ; this . data = data ; } } ; public class FullNodes { static void findFullNode ( Node root ) { if ( root != null ) { findFullNode ( root . left ) ; if ( root . left != null && root . right != null ) Console . Write ( root . data + " ▁ " ) ; findFullNode ( root . right ) ; } } public static void Main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; findFullNode ( root ) ; } }
Find the k largest numbers after deleting the given elements |  ; Find k maximum element from arr [ 0. . m - 1 ] after deleting elements from del [ 0. . n - 1 ] ; Hash Map of the numbers to be deleted ; Increment the count of del [ i ] ; Initializing the largestElement ; Search if the element is present ; Decrement its frequency ; If the frequency becomes 0 , erase it from the map ; Else compare it largestElement ; Print top k elements in the heap ; Print top k elements in the heap ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findElementsAfterDel ( int [ ] arr , int m , int [ ] del , int n , int k ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . ContainsKey ( del [ i ] ) ) { mp [ del [ i ] ] ++ ; } else { mp . Add ( del [ i ] , 1 ) ; } } List < int > heap = new List < int > ( ) ; for ( int i = 0 ; i < m ; ++ i ) { if ( mp . ContainsKey ( arr [ i ] ) ) { mp [ arr [ i ] ] -- ; if ( mp [ arr [ i ] ] == 0 ) { mp . Remove ( arr [ i ] ) ; } } else { heap . Add ( arr [ i ] ) ; } } heap . Sort ( ) ; heap . Reverse ( ) ; for ( int i = 0 ; i < k ; ++ i ) { Console . Write ( heap [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] array = { 5 , 12 , 33 , 4 , 56 , 12 , 20 } ; int m = array . Length ; int [ ] del = { 12 , 56 , 5 } ; int n = del . Length ; int k = 3 ; findElementsAfterDel ( array , m , del , n , k ) ; } }
K | C # implementation of the above approach ; Set to store the unique substring ; String to create each substring ; Adding to set ; Converting set into a list ; Sorting the strings int the list into lexicographical order ; Printing kth substring ; Driver Code
using System ; using System . Collections . Generic ; using System . Collections ; class GFG { public static void kThLexString ( string st , int k , int n ) { HashSet < string > z = new HashSet < string > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { string pp = " " ; for ( int j = i ; j < i + k ; j ++ ) { if ( j >= n ) break ; pp += st [ j ] ; z . Add ( pp ) ; } } ArrayList fin = new ArrayList ( ) ; foreach ( string s in z ) { fin . Add ( s ) ; } fin . Sort ( ) ; Console . Write ( fin [ k - 1 ] ) ; } public static void Main ( string [ ] args ) { string s = " geeksforgeeks " ; int k = 5 ; int n = s . Length ; kThLexString ( s , k , n ) ; } }
Rearrange array such that arr [ i ] >= arr [ j ] if i is even and arr [ i ] <= arr [ j ] if i is odd and j < i | C # program to rearrange the array as per the given condition ; Function to rearrange the array ; total even positions ; total odd positions ; copy original array in an auxiliary array ; sort the auxiliary array ; Fill up odd position in original array ; Fill up even positions in original array ; display array ; Driver Code
using System ; public class GfG { public static void rearrangeArr ( int [ ] arr , int n ) { int evenPos = n / 2 ; int oddPos = n - evenPos ; int [ ] tempArr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) tempArr [ i ] = arr [ i ] ; Array . Sort ( tempArr ) ; int j = oddPos - 1 ; for ( int i = 0 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j -- ; } j = oddPos ; for ( int i = 1 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j ++ ; } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int size = 7 ; rearrangeArr ( arr , size ) ; } }