codeparrot/xlcost-text-to-code huggingface dataset:
Maximum circular subarray sum | C # program for maximum contiguous circular sum problem ; The function returns maximum circular contiguous sum in a [ ] ; Corner Case ; Initialize sum variable which store total sum of the array . ; Initialize every variable with first value of array . ; Concept of Kadane 's Algorithm ; Kadane 's Algorithm to find Maximum subarray  sum. ; Kadane 's Algorithm to find Minimum subarray  sum. ; returning the maximum value ; Driver code
using System ; class GFG { public static int maxCircularSum ( int [ ] a , int n ) { if ( n == 1 ) return a [ 0 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } int curr_max = a [ 0 ] , max_so_far = a [ 0 ] , curr_min = a [ 0 ] , min_so_far = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { curr_max = Math . Max ( curr_max + a [ i ] , a [ i ] ) ; max_so_far = Math . Max ( max_so_far , curr_max ) ; curr_min = Math . Min ( curr_min + a [ i ] , a [ i ] ) ; min_so_far = Math . Min ( min_so_far , curr_min ) ; } if ( min_so_far == sum ) { return max_so_far ; } return Math . Max ( max_so_far , sum - min_so_far ) ; } public static void Main ( ) { int [ ] a = { 11 , 10 , - 20 , 5 , - 3 , - 5 , 8 , - 13 , 10 } ; int n = 9 ; Console . WriteLine ( " Maximum ▁ circular ▁ sum ▁ is ▁ " + maxCircularSum ( a , n ) ) ; } }
Construction of Longest Increasing Subsequence ( N log N ) | C # implementation to find longest increasing subsequence in O ( n Log n ) time . ; Binary search ; Add boundary case , when array n is zero Depend on smart pointers ; initialized with - 1 ; it will always point to empty location ; new smallest value ; arr [ i ] wants to extend largest subsequence ; arr [ i ] wants to be a potential condidate of future subsequence It will replace ceil value in tailIndices ; Driver code
using System ; class GFG { static int GetCeilIndex ( int [ ] arr , int [ ] T , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static int LongestIncreasingSubsequence ( int [ ] arr , int n ) { int [ ] tailIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) tailIndices [ i ] = 0 ; int [ ] prevIndices = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) prevIndices [ i ] = - 1 ; int len = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } Console . Write ( " LIS ▁ of ▁ given ▁ input " ) ; for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; return len ; } public static void Main ( ) { int [ ] arr = { 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 } ; int n = arr . Length ; Console . Write ( " LIS ▁ size STRNEWLINE " + LongestIncreasingSubsequence ( arr , n ) ) ; } }
Maximize sum of consecutive differences in a circular array | C # program to maximize the sum of difference between consecutive elements in circular array ; Return the maximum Sum of difference between consecutive elements . ; Sorting the array . ; Subtracting a1 , a2 , a3 , ... . . , a ( n / 2 ) - 1 , an / 2 twice and adding a ( n / 2 ) + 1 , a ( n / 2 ) + 2 , a ( n / 2 ) + 3 , ... . . , an - 1 , an twice . ; Driver Program
using System ; class MaxSum { static int maxSum ( int [ ] arr , int n ) { int sum = 0 ; Array . Sort ( arr ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { sum -= ( 2 * arr [ i ] ) ; sum += ( 2 * arr [ n - i - 1 ] ) ; } return sum ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 8 } ; int n = arr . Length ; Console . WriteLine ( maxSum ( arr , n ) ) ; } }
Three way partitioning of an array around a given range | C # program to implement three way partitioning around a given range . ; Partitions arr [ 0. . n - 1 ] around [ lowVal . . highVal ] ; Initialize ext available positions for smaller ( than range ) and greater lements ; Traverse elements from left ; If current element is smaller than range , put it on next available smaller position . ; If current element is greater than range , put it on next available greater position . ; Driver code
using System ; class GFG { public static void threeWayPartition ( int [ ] arr , int lowVal , int highVal ) { int n = arr . Length ; int start = 0 , end = n - 1 ; for ( int i = 0 ; i <= end ; ) { if ( arr [ i ] < lowVal ) { int temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i ++ ; } else if ( arr [ i ] > highVal ) { int temp = arr [ end ] ; arr [ end ] = arr [ i ] ; arr [ i ] = temp ; end -- ; } else i ++ ; } } public static void Main ( ) { int [ ] arr = { 1 , 14 , 5 , 20 , 4 , 2 , 54 , 20 , 87 , 98 , 3 , 1 , 32 } ; threeWayPartition ( arr , 10 , 20 ) ; Console . WriteLine ( " Modified ▁ array ▁ " ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } }
Generate all possible sorted arrays from alternate elements of two given sorted arrays | C # Program to generate all possible sorted arrays from alternate elements of two given sorted arrays ; Function to generates and prints all sorted arrays from alternate elements of ' A [ i . . m - 1 ] ' and ' B [ j . . n - 1 ] ' If ' flag ' is true , then current element is to be included from A otherwise from B . ' len ' is the index in output array C [ ] . We print output array each time before including a character from A only if length of output array is greater than 0. We try than all possible combinations ; Include valid element from A ; Print output if there is at least one ' B ' in output array ' C ' ; Recur for all elements of A after current index ; this block works for the very first call to include the first element in the output array ; don 't increment lem  as B is included yet ; include valid element from A and recur ; Include valid element from B and recur ; Wrapper function ; output array ; A utility function to print an array ; Driver Code
using System ; class GenerateArrays { public virtual void generateUtil ( int [ ] A , int [ ] B , int [ ] C , int i , int j , int m , int n , int len , bool flag ) { if ( flag ) { if ( len != 0 ) { printArr ( C , len + 1 ) ; } for ( int k = i ; k < m ; k ++ ) { if ( len == 0 ) { C [ len ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len , ! flag ) ; } else if ( A [ k ] > C [ len ] ) { C [ len + 1 ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , ! flag ) ; } } } else { for ( int l = j ; l < n ; l ++ ) { if ( B [ l ] > C [ len ] ) { C [ len + 1 ] = B [ l ] ; generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , ! flag ) ; } } } } public virtual void generate ( int [ ] A , int [ ] B , int m , int n ) { int [ ] C = new int [ m + n ] ; generateUtil ( A , B , C , 0 , 0 , m , n , 0 , true ) ; } public virtual void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } Console . WriteLine ( " " ) ; } public static void Main ( string [ ] args ) { GenerateArrays generate = new GenerateArrays ( ) ; int [ ] A = new int [ ] { 10 , 15 , 25 } ; int [ ] B = new int [ ] { 5 , 20 , 30 } ; int n = A . Length ; int m = B . Length ; generate . generate ( A , B , n , m ) ; } }
Replace two consecutive equal values with one greater | C # program to replace two elements with equal values with one greater . ; Function to replace consecutive equal elements ; Index in result ; to print new array ; Driver code
using System ; class GFG { static void replace_elements ( int [ ] arr , int n ) { int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arr [ pos ++ ] = arr [ i ] ; while ( pos > 1 && arr [ pos - 2 ] == arr [ pos - 1 ] ) { pos -- ; arr [ pos - 1 ] ++ ; } } for ( int i = 0 ; i < pos ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void Main ( ) { int [ ] arr = { 6 , 4 , 3 , 4 , 3 , 3 , 5 } ; int n = arr . Length ; replace_elements ( arr , n ) ; } }
Rearrange a binary string as alternate x and y occurrences | C # program to arrange given string ; Function which arrange the given string ; Counting number of 0 ' s ▁ and ▁ 1' s in the given string . ; Printing first all 0 ' s ▁ x - times ▁ ▁ and ▁ decrement ▁ count ▁ of ▁ 0' s x - times and do the similar task with '1' ; Driver Code
using System ; class GFG { static void arrangeString ( string str , int x , int y ) { int count_0 = 0 ; int count_1 = 0 ; int len = str . Length ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '0' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 count_1 > 0 ) { for ( int j = 0 ; j < x && count_0 > 0 ; j ++ ) { if ( count_0 > 0 ) { Console . Write ( "0" ) ; count_0 -- ; } } for ( int j = 0 ; j < y && count_1 > 0 ; j ++ ) { if ( count_1 > 0 ) { Console . Write ( "1" ) ; count_1 -- ; } } } } public static void Main ( ) { string str = "01101101101101101000000" ; int x = 1 ; int y = 2 ; arrangeString ( str , x , y ) ; } }
Shuffle 2 n integers as a1 | C # program to shuffle an array in the form of a1 , b1 , a2 , b2 , ... ; function to rearrange the array ; if size is null or odd return because it is not possible to rearrange ; start from the middle index ; each time we will set two elements from the start to the valid position by swapping ; Driver Program
using System ; class GFG { public static void rearrange ( int [ ] arr ) { if ( arr == null arr . Length % 2 == 1 ) return ; int currIdx = ( arr . Length - 1 ) / 2 ; while ( currIdx > 0 ) { int count = currIdx , swapIdx = currIdx ; while ( count -- > 0 ) { int temp = arr [ swapIdx + 1 ] ; arr [ swapIdx + 1 ] = arr [ swapIdx ] ; arr [ swapIdx ] = temp ; swapIdx ++ ; } currIdx -- ; } } public static void Main ( ) { int [ ] arr = { 1 , 3 , 5 , 2 , 4 , 6 } ; rearrange ( arr ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) Console . Write ( " ▁ " + arr [ i ] ) ; } }
Maximum difference between two elements such that larger element appears after the smaller number | C # code to find Maximum difference ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order . Returns 0 if elements are equal ; Driver code ; Function calling
using System ; class GFG { static int maxDiff ( int [ ] arr , int arr_size ) { int max_diff = arr [ 1 ] - arr [ 0 ] ; int i , j ; for ( i = 0 ; i < arr_size ; i ++ ) { for ( j = i + 1 ; j < arr_size ; j ++ ) { if ( arr [ j ] - arr [ i ] > max_diff ) max_diff = arr [ j ] - arr [ i ] ; } } return max_diff ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 90 , 10 , 110 } ; Console . Write ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , 5 ) ) ; } }
Maximum difference between two elements such that larger element appears after the smaller number | C # program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize Result ; Initialize max element from right side ; Driver Code ; Function calling
using System ; class GFG { static int maxDiff ( int [ ] arr , int n ) { int maxDiff = - 1 ; int maxRight = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { int diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } public static void Main ( ) { int [ ] arr = { 1 , 2 , 90 , 10 , 110 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
Maximum difference between two elements such that larger element appears after the smaller number | C # program to find Maximum difference between two elements such that larger element appears after the smaller number ; The function assumes that there are at least two elements in array . The function returns a negative value if the array is sorted in decreasing order and returns 0 if elements are equal ; Initialize diff , current sum and max sum ; Calculate current diff ; Calculate current sum ; Update max sum , if needed ; Driver Code ; Function calling
using System ; class GFG { static int maxDiff ( int [ ] arr , int n ) { int diff = arr [ 1 ] - arr [ 0 ] ; int curr_sum = diff ; int max_sum = curr_sum ; for ( int i = 1 ; i < n - 1 ; i ++ ) { diff = arr [ i + 1 ] - arr [ i ] ; if ( curr_sum > 0 ) curr_sum += diff ; else curr_sum = diff ; if ( curr_sum > max_sum ) max_sum = curr_sum ; } return max_sum ; } public static void Main ( ) { int [ ] arr = { 80 , 2 , 6 , 3 , 100 } ; int n = arr . Length ; Console . WriteLine ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
Find the maximum element in an array which is first increasing and then decreasing | C # program to find maximum element ; function to find the maximum element ; Driver code
using System ; class GFG { static int findMaximum ( int [ ] arr , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void Main ( ) { int [ ] arr = { 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 } ; int n = arr . Length ; Console . Write ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }
Find the maximum element in an array which is first increasing and then decreasing | C # program to find maximum element ; Base Case : Only one element is present in arr [ low . . high ] ; If there are two elements and first is greater then the first element is maximum ; If there are two elements and second is greater then the second element is maximum ; If we reach a point where arr [ mid ] is greater than both of its adjacent elements arr [ mid - 1 ] and arr [ mid + 1 ] , then arr [ mid ] is the maximum element ; If arr [ mid ] is greater than the next element and smaller than the previous element then maximum lies on left side of mid ; when arr [ mid ] is greater than arr [ mid - 1 ] and smaller than arr [ mid + 1 ] ; main function
using System ; class GFG { static int findMaximum ( int [ ] arr , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] > arr [ mid - 1 ] ) return arr [ mid ] ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] < arr [ mid - 1 ] ) return findMaximum ( arr , low , mid - 1 ) ; else return findMaximum ( arr , mid + 1 , high ) ; } public static void Main ( ) { int [ ] arr = { 1 , 3 , 50 , 10 , 9 , 7 , 6 } ; int n = arr . Length ; Console . Write ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }
Count smaller elements on right side |  ; initialize all the counts in countSmaller array as 0 ; Utility function that prints out an array on a line ; Driver function
using System ; class GFG { static void constructLowerArray ( int [ ] arr , int [ ] countSmaller , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } static void printArray ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; Console . WriteLine ( " " ) ; } public static void Main ( ) { int [ ] arr = new int [ ] { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = arr . Length ; int [ ] low = new int [ n ] ; constructLowerArray ( arr , low , n ) ; printArray ( low , n ) ; } }
Find the smallest positive number missing from an unsorted array | Set 1 | C # program to find the smallest positive missing number ; Utility function that puts all non - positive ( 0 and negative ) numbers on left side of arr [ ] and return count of such numbers ; increment count of non - positive integers ; Find the smallest positive missing number in an array that contains all positive integers ; Mark arr [ i ] as visited by making arr [ arr [ i ] - 1 ] negative . Note that 1 is subtracted as index start from 0 and positive numbers start from 1 ; Return the first index value at which is positive ; 1 is added becuase indexes start from 0 ; Find the smallest positive missing number in array that contains both positive and negative integers ; First separate positive and negative numbers ; Shift the array and call findMissingPositive for positive part ; Driver code
using System ; class main { static int segregate ( int [ ] arr , int size ) { int j = 0 , i ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] <= 0 ) { int temp ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } return j ; } static int findMissingPositive ( int [ ] arr , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { if ( Math . Abs ( arr [ i ] ) - 1 < size && arr [ Math . Abs ( arr [ i ] ) - 1 ] > 0 ) arr [ Math . Abs ( arr [ i ] ) - 1 ] = - arr [ Math . Abs ( arr [ i ] ) - 1 ] ; } for ( i = 0 ; i < size ; i ++ ) if ( arr [ i ] > 0 ) return i + 1 ; return size + 1 ; } static int findMissing ( int [ ] arr , int size ) { int shift = segregate ( arr , size ) ; int [ ] arr2 = new int [ size - shift ] ; int j = 0 ; for ( int i = shift ; i < size ; i ++ ) { arr2 [ j ] = arr [ i ] ; j ++ ; } return findMissingPositive ( arr2 , j ) ; } public static void Main ( ) { int [ ] arr = { 0 , 10 , 2 , - 10 , - 20 } ; int arr_size = arr . Length ; int missing = findMissing ( arr , arr_size ) ; Console . WriteLine ( " The ▁ smallest ▁ positive ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }
Find the smallest positive number missing from an unsorted array | Set 1 | C # program for the above approach ; Function for finding the first missing positive number ; Check if 1 is present in array or not ; Loop to check boundary condition and for swapping ; Finding which index has value less than n ; If array has values from 1 to n ; Driver Code
using System ; public class GFG { static int firstMissingPositive ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( arr [ i ] >= 1 && arr [ i ] <= n && arr [ i ] != arr [ arr [ i ] - 1 ] ) { int temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != i + 1 ) return ( i + 1 ) ; return ( n + 1 ) ; } static public void Main ( ) { int [ ] arr = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . Length ; int ans = firstMissingPositive ( arr , n ) ; Console . WriteLine ( ans ) ; } }
Maximum Sum Path in Two Arrays | C # program for Maximum Sum Path in Two Arrays ; Utility function to find maximum of two integers ; This function returns the sum of elements on maximum path from beginning to end ; initialize indexes for ar1 [ ] and ar2 [ ] ; Initialize result and current sum through ar1 [ ] and ar2 [ ] . ; Below 3 loops are similar to merge in merge sort ; Add elements of ar1 [ ] to sum1 ; Add elements of ar2 [ ] to sum2 ; we reached a common point ; Take the maximum of two sums and add to result Also add the common element of array , once ; Update sum1 and sum2 for elements after this intersection point ; update i and j to move to next element of each array ; Add remaining elements of ar1 [ ] ; Add remaining elements of ar2 [ ] ; Add maximum of two sums of remaining elements ; Driver code ; Function call
using System ; class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int maxPathSum ( int [ ] ar1 , int [ ] ar2 , int m , int n ) { int i = 0 , j = 0 ; int result = 0 , sum1 = 0 , sum2 = 0 ; while ( i < m && j < n ) { if ( ar1 [ i ] < ar2 [ j ] ) sum1 += ar1 [ i ++ ] ; else if ( ar1 [ i ] > ar2 [ j ] ) sum2 += ar2 [ j ++ ] ; else { result += max ( sum1 , sum2 ) + ar1 [ i ] ; sum1 = 0 ; sum2 = 0 ; i ++ ; j ++ ; } } while ( i < m ) sum1 += ar1 [ i ++ ] ; while ( j < n ) sum2 += ar2 [ j ++ ] ; result += max ( sum1 , sum2 ) ; return result ; } public static void Main ( ) { int [ ] ar1 = { 2 , 3 , 7 , 10 , 12 , 15 , 30 , 34 } ; int [ ] ar2 = { 1 , 5 , 7 , 8 , 10 , 15 , 16 , 19 } ; int m = ar1 . Length ; int n = ar2 . Length ; Console . Write ( " Maximum ▁ sum ▁ path ▁ is ▁ : " + maxPathSum ( ar1 , ar2 , m , n ) ) ; } }
Smallest greater elements in whole array | Simple C # program to find smallest greater element in whole array for every element . ; Find the closest greater element for arr [ j ] in the entire array . ; Check if arr [ i ] is largest ; Driver code
using System ; class GFG { static void smallestGreater ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int diff = int . MaxValue ; int closest = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] && arr [ j ] - arr [ i ] < diff ) { diff = arr [ j ] - arr [ i ] ; closest = j ; } } if ( closest == - 1 ) Console . Write ( " _ ▁ " ) ; else Console . Write ( arr [ closest ] + " ▁ " ) ; } } public static void Main ( ) { int [ ] ar = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . Length ; smallestGreater ( ar , n ) ; } }
Smallest greater elements in whole array | Efficient C # program to find smallest greater element in whole array for every element . ; lowerbound function ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void smallestGreater ( int [ ] arr , int n ) { HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . Add ( arr [ i ] ) ; } int [ ] newAr = new int [ s . Count ] ; int j = 0 ; foreach ( int p in s ) { newAr [ j ] = p ; j ++ ; } Array . Sort ( newAr ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = lowerBound ( newAr , 0 , newAr . GetLength ( 0 ) , arr [ i ] ) ; if ( temp < n ) Console . Write ( newAr [ temp ] + " ▁ " ) ; else Console . Write ( " _ ▁ " ) ; } } static int lowerBound ( int [ ] array , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low + 1 ; } public static void Main ( String [ ] args ) { int [ ] ar = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . Length ; smallestGreater ( ar , n ) ; } }