codeparrot/xlcost-text-to-code huggingface dataset:
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | C # program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int moduloEquality ( int [ ] A , int [ ] B , int n , int m ) { Dictionary < int , int > mapA = new Dictionary < int , int > ( ) ; Dictionary < int , int > mapB = new Dictionary < int , int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . ContainsKey ( A [ i ] ) ) { mapA [ A [ i ] ] = mapA [ A [ i ] ] + 1 ; } else { mapA . Add ( A [ i ] , 1 ) ; } if ( mapB . ContainsKey ( B [ i ] ) ) { mapB [ B [ i ] ] = mapB [ B [ i ] ] + 1 ; } else { mapB . Add ( B [ i ] , 1 ) ; } } HashSet < int > possibleValues = new HashSet < int > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . Add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Int32 . MaxValue ; foreach ( int it in possibleValues ) { bool possible = true ; foreach ( KeyValuePair < int , int > it2 in mapA ) { if ( it2 . Value != mapB [ ( it2 . Key + it ) % m ] ) { possible = false ; break ; } } if ( possible ) { ans = Math . Min ( ans , it ) ; } } return ans ; } static void Main ( ) { int n = 4 ; int m = 3 ; int [ ] A = { 0 , 0 , 2 , 1 } ; int [ ] B = { 2 , 0 , 1 , 1 } ; Console . WriteLine ( moduloEquality ( A , B , n , m ) ) ; } }
Count all indices of cyclic regular parenthesis | C # program for the above approach ; Function to find all indices which cyclic shift leads to get balanced parenthesis ; Create auxiliary array ; Finding prefix sum and minimum element ; Update the minimum element ; Check if count of ' ( ' and ' ) ' are equal ; Find count of minimum element ; Find the frequency of mn ; Return the count ; Driver code ; Given string S ; length of the string S
using System ; class GFG { static int countCyclicShifts ( string S , int n ) { int [ ] aux = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( S [ i ] == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . Min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } public static void Main ( string [ ] args ) { string S = " ) ( ) ( " ; int N = S . Length ; Console . Write ( countCyclicShifts ( S , N ) ) ; } }
Maximize Profit by trading stocks based on given rate per day | C # program for the above approach ; Function to find the maximum profit ; Start from the last day ; Traverse and keep adding the profit until a day with price of stock higher than currentDay is obtained ; Set this day as currentDay with maximum cost of stock currently ; Return the profit ; Driver Code ; Given array of prices ; Function call
using System ; class GFG { static int maxProfit ( int [ ] prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } public static void Main ( String [ ] args ) { int [ ] prices = { 2 , 3 , 5 } ; int N = prices . Length ; Console . Write ( maxProfit ( prices , N ) ) ; } }
Check given string is oddly palindrome or not | Set 2 | C # program for the above approach ; Function to check if String formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic String is not compared ; If stack is empty then return true ; Driver Code ; Given String
using System ; using System . Collections . Generic ; class GFG { static bool isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; bool lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < char > s = new Stack < char > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . Push ( str [ i ] ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . Count > 0 ) { if ( s . Peek ( ) == str [ i ] ) s . Pop ( ) ; else break ; i = i + 2 ; } if ( s . Count == 0 ) return true ; return false ; } public static void Main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Check given string is oddly palindrome or not | Set 2 | C # program for the above approach ; Functions checks if characters at odd index of the String forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call
using System ; class GFG { static bool isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str [ left ] != str [ right ] ) return false ; left += 2 ; right -= 2 ; } return true ; } public static void Main ( String [ ] args ) { int n = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) Console . Write ( " Yes STRNEWLINE " ) ; else Console . Write ( " No STRNEWLINE " ) ; } }
Remove minimum characters from string to split it into three substrings under given constraints | C # program for the above approach ; Function that counts minimum character that must be removed ; Length of string ; Create prefix array ; Initialize first position ; Fill prefix array ; Initialise maxi ; Check all the possibilities by putting i and j at different position & find maximum among them ; Print the characters to be removed ; Driver Code ; Given String ; Function call
using System ; class GFG { static void min_remove ( string str ) { int N = str . Length ; int [ ] prefix_a = new int [ N + 1 ] ; int [ ] prefix_b = new int [ N + 1 ] ; int [ ] prefix_c = new int [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( int ) ( ( str [ i - 1 ] == ' a ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( int ) ( ( str [ i - 1 ] == ' b ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( int ) ( ( str [ i - 1 ] == ' c ' ) ? 1 : 0 ) ; } int maxi = Int32 . MinValue ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = Math . Max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } Console . WriteLine ( ( N - maxi ) ) ; } public static void Main ( ) { string str = " aaaabaaxccac " ; min_remove ( str ) ; } }
Create an array of size N with sum S such that no subarray exists with sum S or S | C # program for the above approach ; Function to create an array with N elements with sum as S such that the given conditions satisfy ; Check if the solution exists ; Print the array as print ( n - 1 ) elements of array as 2 ; Print the last element of the array ; Print the value of k ; If solution doesnot exists ; Driver Code ; Given N and sum S ; Function call
using System ; class GFG { static void createArray ( int n , int s ) { if ( 2 * n <= s ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { Console . Write ( 2 + " ▁ " ) ; s -= 2 ; } Console . WriteLine ( s ) ; Console . WriteLine ( 1 ) ; } else Console . Write ( " - 1" ) ; } public static void Main ( ) { int N = 1 ; int S = 4 ; createArray ( N , S ) ; } }
Maximize partitions such that no two substrings have any common character | C # program to find the maximum number of partitions possible such that no two subStrings have common character ; Function to calculate and return the maximum number of partitions ; r : Stores the maximum number of partitions k : Stores the ending index of the partition ; Stores the last index of every unique character of the String ; Traverse the String and store the last index of every character ; Store the last index of the first character from map ; Update K to find the end of partition ; Otherwise , the end of partition is found ; Increment r ; Update k for the next partition ; Add the last partition ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int maximum_partition ( String str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; Dictionary < char , int > m = new Dictionary < char , int > ( ) ; for ( i = str . Length - 1 ; i >= 0 ; i -- ) { if ( ! m . ContainsKey ( str [ i ] ) ) { m . Add ( str [ i ] , i ) ; } } i = 0 ; k = m [ str [ i ] ] ; for ( i = 0 ; i < str . Length ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . Max ( k , m [ str [ i ] ] ) ; } else { r = r + 1 ; c = 1 ; k = Math . Max ( k , m [ str [ i ] ] ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } public static void Main ( String [ ] args ) { String str = " ababcbacadefegdehijhklij " ; Console . Write ( maximum_partition ( str ) ) ; } }
Minimize steps required to move all 1 's in a matrix to a given index | C # program to calculate the minimum steps required to reach a given cell from all cells consisting of 1 's ; Function to calculate and return the minimum number of steps required to move all 1 s to ( X , Y ) ; Iterate the given matrix ; Update the answer with minimum moves required for the given element to reach the given index ; Return the number of steps ; Driver code ; Given matrix ; Given position ; Function call
using System ; class GFG { static int findAns ( int [ , ] mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i , j ] == 1 ) { ans += Math . Abs ( x - i ) + Math . Abs ( y - j ) ; } } } return ans ; } public static void Main ( String [ ] args ) { int [ , ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; Console . Write ( findAns ( mat , x , y , mat . GetLength ( 0 ) , mat . GetLength ( 1 ) ) + " STRNEWLINE " ) ; } }
Count of all possible reverse bitonic subarrays | C # program for the above approach ; Function that counts all the reverse bitonic subarray in arr [ ] ; To store the count of reverse bitonic subarray ; Iterate the array and select the starting element ; Iterate for selecting the ending element for subarray ; Subarray arr [ i to j ] ; For 1 length , increment the count and continue ; For decreasing subarray ; Check if only decreasing ; For increasing subarray ; Print the total count of subarrays ; Driver Code ; Given array arr [ ] ; Function Call
using System ; class GFG { static void countReversebitonic ( int [ ] arr , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < arr [ k ] && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } Console . Write ( c ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 3 , 1 , 4 } ; int N = arr . Length ; countReversebitonic ( arr , N ) ; } }
Longest increasing sequence by the boundary elements of an Array | C # program to print the longest increasing subsequence from the boundary elements of an array ; Function to return the length of longest Increasing subsequence ; Set pointers at both ends ; Stores the recent value added to the subsequence ; Stores the length of the subsequence ; Check if both elements can be added to the subsequence ; Check if the element on the left can be added to the subsequence only ; Check if the element on the right can be added to the subsequence only ; If none of the values can be added to the subsequence ; Driver Code ; Length of array
using System ; class GFG { static int longestSequence ( int n , int [ ] arr ) { int l = 0 , r = n - 1 ; int prev = int . MinValue ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 1 , 4 , 2 } ; int n = arr . Length ; Console . Write ( longestSequence ( n , arr ) ) ; } }
Total number of cells covered in a matrix after D days | C # implementation to find the total number of cells covered in a matrix after D days ; Function to return the total infected cells after d days ; Top extension ; Bottom extension ; Left extension ; Right extension ; Calculating the cells in each quadrilateral ; Sum all of them to get total cells in each quadrilateral ; Add the singleblocks along the lines of top , down , left , right ; Return the ans ; Driver code ; Dimensions of cell ; Starting coordinates ; Number of days ; Function call
using System ; class GFG { static int solve ( int n , int m , int x , int y , int d ) { int top = Math . Min ( d , x - 1 ) ; int down = Math . Min ( d , n - x ) ; int left = Math . Min ( d , y - 1 ) ; int right = Math . Min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } public static void Main ( String [ ] args ) { int n = 10 , m = 10 ; int x = 7 , y = 8 ; int d = 4 ; d -- ; Console . WriteLine ( solve ( n , m , x , y , d ) ) ; } }
Smallest subsequence with sum of absolute difference of consecutive elements maximized | C # program to find smallest subsequence with sum of absolute difference of consecutive elements maximized ; Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of readonly subsequence ; Print the subsequence ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { List < int > req = new List < int > ( ) ; req . Add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . Add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . Add ( arr [ i ] ) ; } req . Add ( arr [ n - 1 ] ) ; Console . Write ( req . Count + " STRNEWLINE " ) ; foreach ( int x in req ) Console . Write ( x + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . Length ; getSubsequence ( arr , n ) ; } }
Minimum steps to convert all paths in matrix from top left to bottom right as palindromic paths | C # implementation to find the minimum number of changes required such that every path from top left to the bottom right are palindromic paths ; Function to find the minimum number of the changes required for the every path to be palindromic ; count variable for maintaining total changes . ; left and right variables for keeping distance values from cell ( 0 , 0 ) and ( N - 1 , M - 1 ) respectively . ; Iterating over the matrix ; Finding minimum number of changes required . ; Minimum no . of changes will be the the minimum no . of different values and we will assume to make them equals to value with maximum frequency element ; Moving ahead with greater distance ; Driver Code ; Function Call
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static int M = 3 ; static int N = 3 ; static int minchanges ( int [ , ] mat ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { Dictionary < int , int > mp = new Dictionary < int , int > ( ) ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { if ( mp . ContainsKey ( mat [ i , j ] ) ) { mp [ mat [ i , j ] ] ++ ; } else { mp [ mat [ i , j ] ] = 1 ; } totalsize ++ ; } else if ( i + j == right ) { if ( mp . ContainsKey ( mat [ i , j ] ) ) { mp [ mat [ i , j ] ] ++ ; } else { mp [ mat [ i , j ] ] = 1 ; } totalsize ++ ; } } } int changes = 0 ; foreach ( KeyValuePair < int , int > itr in mp ) { changes = Math . Max ( changes , itr . Value ) ; } count += totalsize - changes ; left ++ ; right -- ; } return count ; } public static void Main ( string [ ] args ) { int [ , ] mat = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; Console . Write ( minchanges ( mat ) ) ; } }
Count of longest possible subarrays with sum not divisible by K | C # program for the above problem ; Function to find the count of longest subarrays with sum not divisible by K ; Sum of all elements in an array ; If overall sum is not divisible then return 1 , as only one subarray of size n is possible ; Index of the first number not divisible by K ; Index of the last number not divisible by K ; Subarray doesn 't exist ; Sum of the window ; Calculate the sum of rest of the windows of size len ; Driver Code
using System ; class GFG { static int CountLongestSubarrays ( int [ ] arr , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . Max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 2 , 2 , 3 } ; int n = arr . Length ; int k = 3 ; Console . WriteLine ( CountLongestSubarrays ( arr , n , k ) ) ; } }
Count of triplets in a given Array having GCD K | C # program to count the number of triplets in the array with GCD equal to K ; Frequency array ; mul [ i ] stores the count of multiples of i ; cnt [ i ] stores the count of triplets with gcd = i ; Return nC3 ; Function to count and return the number of triplets in the array with GCD equal to K ; Store frequency of array elements ; Store the multiples of i present in the array ; Count triplets with gcd equal to any multiple of i ; Remove all triplets which have gcd equal to a multiple of i ; Driver code
using System ; class GFG { static int MAXN = 1000001 ; static int [ ] freq = new int [ MAXN ] ; static int [ ] mul = new int [ MAXN ] ; static int [ ] cnt = new int [ MAXN ] ; static int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } static void count_triplet ( int [ ] arr , int N , int K ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( i = 1 ; i <= 1000000 ; i ++ ) { for ( j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( i = 1000000 ; i >= 1 ; i -- ) { for ( j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } Console . Write ( " Number ▁ of ▁ triplets ▁ " + " with ▁ GCD ▁ " + K ) ; Console . Write ( " ▁ are ▁ " + cnt [ K ] ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; } }
Check if a subsequence of length K with odd sum exists | C # program to check if a subsequence of length K with odd sum exists in the given array ; Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code
using System ; class GFG { static bool isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void Main ( ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . Length ; int K = 3 ; Console . Write ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }
Path traversed using exactly M coins in K jumps | C # program to print the path using exactly K jumps and M coins ; Function that print the path using exactly K jumps and M coins ; If no path exists ; It decides which box to be jump ; It decides whether to jump on left side or to jump on right side ; Print the path ; Driver Code ; Function Call
using System ; class GFG { static void print_path ( int N , int jump , int coin ) { if ( jump > coin || jump * ( N - 1 ) < coin ) { Console . WriteLine ( " - 1" ) ; } else { int pos = 1 ; while ( jump > 0 ) { int tmp = Math . Min ( N - 1 , coin - ( jump - 1 ) ) ; if ( pos + tmp <= N ) { pos += tmp ; } else { pos -= tmp ; } Console . Write ( pos + " ▁ " ) ; coin -= tmp ; jump -= 1 ; } } } public static void Main ( String [ ] args ) { int N = 5 , K = 4 , M = 12 ; print_path ( N , K , M ) ; } }
Minimum salary hike for each employee such that no employee feels unfair | C # program for the above approach ; Function that print minimum hike ; Insert INF at begin and end of array ; To store hike of each employee ; for Type 1 employee ; For Type 2 employee ; For Type 3 employee ; For Type 4 employee ; Print the min hike for each employee ; Driver Code ; Given array of rating of employees ; Function Call
using System ; using System . Collections . Generic ; class GFG { static readonly int INF = 10000009 ; static void findMinHike ( List < int > arr , int n ) { arr . Insert ( 0 , INF ) ; arr . Add ( INF ) ; int [ ] hike = new int [ n + 2 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] <= arr [ i + 1 ] ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] >= arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { hike [ i ] = Math . Max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { Console . Write ( hike [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { List < int > arr = new List < int > ( ) ; arr . Add ( 5 ) ; arr . Add ( 3 ) ; arr . Add ( 4 ) ; arr . Add ( 2 ) ; arr . Add ( 1 ) ; arr . Add ( 6 ) ; findMinHike ( arr , arr . Count ) ; } }