codeparrot/xlcost-text-to-code huggingface dataset:
Sort elements by modulo with K | C # implementation of the above approach ; Utility function to print the contents of an array ; Function to sort the array elements based on their modulo with K ; Create K empty vectors ; Update the vectors such that v [ i ] will contain all the elements that give remainder as i when divided by k ; Sorting all the vectors separately ; Replacing the elements in arr [ ] with the required modulo sorted elements ; Add all the elements of the current vector to the array ; Print the sorted array ; Driver Code
using System ; using System . Collections ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void sortWithRemainder ( int [ ] arr , int n , int k ) { ArrayList [ ] v = new ArrayList [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { v [ i ] = new ArrayList ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { v [ arr [ i ] % k ] . Add ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) { v [ i ] . Sort ( ) ; } int j = 0 ; for ( int i = 0 ; i < k ; i ++ ) { foreach ( int x in v [ i ] ) { arr [ j ] = x ; j ++ ; } } printArr ( arr , n ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 } ; int n = arr . Length ; int k = 4 ; sortWithRemainder ( arr , n , k ) ; } }
Concatenate the strings in an order which maximises the occurrence of subsequence " ab " | C # implementation of the approach ; Custom sort function to sort the given string in the order which maximises the final score ; To store the count of occurrences of ' a ' and ' b ' in s1 ; Count the number of occurrences of ' a ' and ' b ' in s1 ; To store the count of occurrences of ' a ' and ' b ' in s2 ; Count the number of occurrences of ' a ' and ' b ' in s2 ; Since the number of subsequences ' ab ' is more when s1 is placed before s2 we return 1 so that s1 occurs before s2 in the combined string ; Function that return the concatenated string as S [ 0 ] + S [ 1 ] + ... + S [ N - 1 ] ; To store the concatenated string ; Concatenate every string in the order of appearance ; Return the concatenated string ; Function to return the maximum required score ; Sort the strings in the order which maximizes the score that we can get ; Get the concatenated string combined string ; Calculate the score of the combined string i . e . the count of occurrences of " ab " as subsequences ; Number of ' a ' has increased by one ; There are count_a number of ' a ' that can form subsequence ' ab ' with this ' b ' ; Driver code
using System ; class GFG { static bool customSort ( string s1 , string s2 ) { int count_a1 = 0 , count_b1 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == ' a ' ) { count_a1 ++ ; } else { count_b1 ++ ; } } int count_a2 = 0 , count_b2 = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s2 [ i ] == ' a ' ) { count_a2 ++ ; } else { count_b2 ++ ; } } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return true ; } else { return false ; } } static string concatenateStrings ( string [ ] S , int N ) { string str = " " ; for ( int i = 0 ; i < N ; i ++ ) { str += S [ i ] ; } return str ; } static int getMaxScore ( string [ ] S , int N ) { Array . Sort ( S ) ; string combined_string = concatenateStrings ( S , N ) ; int final_score = 0 , count_a = 0 ; for ( int i = 0 ; i < combined_string . Length ; i ++ ) { if ( combined_string [ i ] == ' a ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } static public void Main ( ) { string [ ] S = { " aa " , " bb " , " aab " , " bab " } ; int N = S . Length ; Console . WriteLine ( getMaxScore ( S , N ) - 10 ) ; } }
Minimum increment operations to make K elements equal | C # implementation of the approach ; Function to return the minimum number of increment operations required to make any k elements of the array equal ; Sort the array in increasing order ; Calculate the number of operations needed to make 1 st k elements equal to the kth element i . e . the 1 st window ; Answer will be the minimum of all possible k sized windows ; Find the operations needed to make k elements equal to ith element ; Slide the window to the right and subtract increments spent on leftmost element of the previous window ; Add increments needed to make the 1 st k - 1 elements of this window equal to the kth element of the current window ; Driver code
using System ; class geeksforgeeks { static int minOperations ( int [ ] ar , int k ) { Array . Sort ( ar ) ; int opsNeeded = 0 ; for ( int i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } int ans = opsNeeded ; for ( int i = k ; i < ar . Length ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = Math . Min ( ans , opsNeeded ) ; } return ans ; } public static void Main ( ) { int [ ] arr = { 3 , 1 , 9 , 100 } ; int n = arr . Length ; int k = 3 ; Console . Write ( minOperations ( arr , k ) ) ; } }
Lexicographical ordering using Heap Sort | C # implementation to print the string in Lexicographical order ; Used for index in heap ; Predefining the heap array ; Defining formation of the heap ; Iterative heapiFy ; Just swapping if the element is smaller than already stored element ; Swapping the current index with its child ; Moving upward in the heap ; Defining heap sort ; Taking output of the minimum element ; Set first element as a last one ; Decrement of the size of the string ; Initializing the left and right index ; Process of heap sort If root element is minimum than its both of the child then break ; Otherwise checking that the child which one is smaller , swap them with parent element ; Swapping ; Changing the left index and right index ; Utility function ; To heapiFy ; Calling heap sort function ; Driver Code
using System ; class GFG { static int x = - 1 ; static String [ ] heap = new String [ 1000 ] ; static void heapForm ( String k ) { x ++ ; heap [ x ] = k ; int child = x ; String tmp ; int index = x / 2 ; while ( index >= 0 ) { if ( heap [ index ] . CompareTo ( heap [ child ] ) > 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ child ] ; heap [ child ] = tmp ; child = index ; index = index / 2 ; } else { break ; } } } static void heapSort ( ) { int left1 , right1 ; while ( x >= 0 ) { String k ; k = heap [ 0 ] ; Console . Write ( k + " ▁ " ) ; heap [ 0 ] = heap [ x ] ; x = x - 1 ; String tmp ; int index = 0 ; int length = x ; left1 = 1 ; right1 = left1 + 1 ; while ( left1 <= length ) { if ( heap [ index ] . CompareTo ( heap [ left1 ] ) <= 0 && heap [ index ] . CompareTo ( heap [ right1 ] ) <= 0 ) { break ; } else { if ( heap [ left1 ] . CompareTo ( heap [ right1 ] ) < 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ left1 ] ; heap [ left1 ] = tmp ; index = left1 ; } else { tmp = heap [ index ] ; heap [ index ] = heap [ right1 ] ; heap [ right1 ] = tmp ; index = right1 ; } } left1 = 2 * left1 ; right1 = left1 + 1 ; } } } static void sort ( String [ ] k , int n ) { for ( int i = 0 ; i < n ; i ++ ) { heapForm ( k [ i ] ) ; } heapSort ( ) ; } public static void Main ( String [ ] args ) { String [ ] arr = { " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " } ; int n = arr . Length ; sort ( arr , n ) ; } }
Find Kth element in an array containing odd elements first and then even elements | C # implementation of the approach ; Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code
using System ; class GFG { static int getNumber ( int n , int k ) { int [ ] arr = new int [ n ] ; int i = 0 ; int odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } int even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } public static void Main ( String [ ] args ) { int n = 8 , k = 5 ; Console . WriteLine ( getNumber ( n , k ) ) ; } }
Swap Alternate Boundary Pairs | C # implementation of the approach ; Utility function to print the contents of an array ; Function to update the array ; Initialize the pointers ; While there are elements to swap ; Update the pointers ; Print the updated array ; Driver code
using System ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) Console . Write ( arr [ i ] + " ▁ " ) ; } static void UpdateArr ( int [ ] arr , int n ) { int i = 0 , j = n - 1 ; while ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i += 2 ; j -= 2 ; } printArr ( arr , n ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . Length ; UpdateArr ( arr , n ) ; } }
Count permutation such that sequence is non decreasing | C # implementation of the approach ; To store the factorials ; Function to update fact [ ] array such that fact [ i ] = i ! ; 0 ! = 1 ; i ! = i * ( i - 1 ) ! ; Function to return the count of possible permutations ; To store the result ; Sort the array ; Initial size of the block ; Increase the size of block ; Update the result for the previous block ; Reset the size to 1 ; Update the result for the last block ; Driver Code ; Pre - calculating factorials
using System ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int [ ] a , int n ) { int ways = 1 ; Array . Sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } static public void Main ( ) { int [ ] a = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . Length ; pre ( ) ; Console . Write ( CountPermutation ( a , n ) ) ; } }
Smallest element greater than X not present in the array | C # implementation of the approach ; Function to return the smallest element greater than x which is not present in a [ ] ; Sort the array ; Continue until low is less than or equals to high ; Find mid ; If element at mid is less than or equals to searching element ; If mid is equals to searching element ; Increment searching element ; Make high as N - 1 ; Make low as mid + 1 ; Make high as mid - 1 ; Return the next greater element ; Driver code
using System ; class GFG { static int Next_greater ( int [ ] a , int n , int x ) { Array . Sort ( a ) ; int low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } public static void Main ( String [ ] args ) { int [ ] a = { 1 , 5 , 10 , 4 , 7 } ; int x = 4 ; int n = a . Length ; Console . WriteLine ( Next_greater ( a , n , x ) ) ; } }
Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | C # implementation of the approach ; Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code
using System ; class GFG { static void printArrangement ( int [ ] a , int n ) { Array . Sort ( a ) ; int [ ] b = new int [ n ] ; int low = 0 , high = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { Console . Write ( - 1 ) ; return ; } } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( b [ i ] + " ▁ " ) ; } public static void Main ( ) { int [ ] a = { 1 , 4 , 4 , 3 , 2 } ; int n = a . Length ; printArrangement ( a , n ) ; } }
Greatest contiguous sub | C # implementation of the approach ; Function that returns the sub - array ; Data - structure to store all the sub - arrays of size K ; Iterate to find all the sub - arrays ; Store the sub - array elements in the array ; Push the vector in the container ; Sort the vector of elements ; The last sub - array in the sorted order will be the answer ; Driver code ; Get the sub - array
using System ; using System . Collections . Generic ; using System . Linq ; public class GFG { static List < int > findSubarray ( int [ ] a , int k , int n ) { List < List < int > > vec = new List < List < int > > ( ) ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { List < int > temp = new List < int > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { temp . Add ( a [ j ] ) ; } vec . Add ( temp ) ; } vec . OrderBy ( l => l [ 0 ] ) ; return vec [ vec . Count - 1 ] ; } static public void Main ( ) { int [ ] a = { 1 , 4 , 3 , 2 , 5 } ; int k = 4 ; int n = a . Length ; List < int > ans = findSubarray ( a , k , n ) ; foreach ( int it in ans ) { Console . Write ( it + " ▁ " ) ; } } }
Unbounded Fractional Knapsack | C # implementation of the approach ; Function to return the maximum required value ; maxratio will store the maximum value to weight ratio we can have for any item and maxindex will store the index of that element ; Find the maximum ratio ; The item with the maximum value to weight ratio will be put into the knapsack repeatedly until full ; Driver code
using System ; class GFG { static float knapSack ( int W , float [ ] wt , float [ ] val , int n ) { float maxratio = int . MinValue ; int maxindex = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( val [ i ] / wt [ i ] ) > maxratio ) { maxratio = ( val [ i ] / wt [ i ] ) ; maxindex = i ; } } return ( W * maxratio ) ; } public static void Main ( ) { float [ ] val = { 14 , 27 , 44 , 19 } ; float [ ] wt = { 6 , 7 , 9 , 8 } ; int n = val . Length ; int W = 50 ; Console . WriteLine ( knapSack ( W , wt , val , n ) ) ; } }
Sort an array without changing position of negative numbers | C # implementation of above approach ; Function to sort the array such that negative values do not get affected ; Store all non - negative values ; Sort non - negative values ; If current element is non - negative then update it such that all the non - negative values are sorted ; Print the sorted array ; Driver code
using System . Collections . Generic ; using System ; class GFG { static void sortArray ( int [ ] a , int n ) { List < int > ans = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . Add ( a [ i ] ) ; } ans . Sort ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans [ j ] ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) Console . Write ( a [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 2 , - 6 , - 3 , 8 , 4 , 1 } ; int n = arr . Length ; sortArray ( arr , n ) ; } }
Find a triplet in an array whose sum is closest to a given number | C # implementation of the above approach ; Function to return the sum of a triplet which is closest to x ; Sort the array ; To store the closest sum ; Fix the smallest number among the three integers ; Two pointers initially pointing at the last and the element next to the fixed element ; While there could be more pairs to check ; Calculate the sum of the current triplet ; If the sum is more closer than the current closest sum ; If sum is greater then x then decrement the second pointer to get a smaller sum ; Else increment the first pointer to get a larger sum ; Return the closest sum found ; Driver code
using System ; using System . Collections . Generic ; class GFG { static int solution ( List < int > arr , int x ) { arr . Sort ( ) ; int closestSum = int . MaxValue ; for ( int i = 0 ; i < arr . Count - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . Count - 1 ; while ( ptr1 < ptr2 ) { int sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; if ( Math . Abs ( x - sum ) < Math . Abs ( x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return closestSum ; } public static void Main ( String [ ] args ) { int [ ] ar = { - 1 , 2 , 1 , - 4 } ; List < int > arr = new List < int > ( ar ) ; int x = 1 ; Console . WriteLine ( solution ( arr , x ) ) ; } }
Check whether we can sort two arrays by swapping A [ i ] and B [ i ] | C # implementation of above approach ; Function to check whether both the array can be sorted in ( strictly increasing ) ascending order ; Traverse through the array and find out the min and max variable at each position make one array of min variables and another of maximum variable ; Maximum and minimum variable ; Assign min value to B [ i ] and max value to A [ i ] ; Now check whether the array is sorted or not ; Driver code
using System ; class GFG { static bool IsSorted ( int [ ] A , int [ ] B , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = Math . Max ( A [ i ] , B [ i ] ) ; y = Math . Min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } public static void Main ( ) { int [ ] A = { 1 , 4 , 3 , 5 , 7 } ; int [ ] B = { 2 , 2 , 5 , 8 , 9 } ; int n = A . Length ; if ( IsSorted ( A , B , n ) == true ) { Console . Write ( " True " ) ; } else { Console . Write ( " False " ) ; } } }
Find the largest possible k | C # program to find the largest possible k - multiple set ; Function to find the largest possible k - multiple set ; Sort the given array ; To store k - multiple set ; Traverse through the whole array ; Check if x / k is already present or not ; Print the k - multiple set ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class GFG { static void K_multiple ( int [ ] a , int n , int k ) { Array . Sort ( a ) ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] % k == 0 && ! s . Contains ( a [ i ] / k ) ) a [ i ] % k != 0 ) s . Add ( a [ i ] ) ; } foreach ( int i in s ) Console . Write ( i + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 , 10 } ; int k = 2 ; int n = a . Length ; K_multiple ( a , n , k ) ; } }
Maximum water that can be stored between two buildings | C # implementation of the approach ; Return the maximum water that can be stored ; To store the maximum water so far ; Both the pointers are pointing at the first and the last buildings respectively ; While the water can be stored between the currently chosen buildings ; Update maximum water so far and increment i ; Update maximum water so far and decrement j ; Any of the pointers can be updated ( or both ) ; Driver code
using System ; class GFG { static int maxWater ( int [ ] height , int n ) { int max = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { max = Math . Max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { max = Math . Max ( max , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { max = Math . Max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return max ; } static public void Main ( ) { int [ ] height = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . Length ; Console . Write ( maxWater ( height , n ) ) ; } }
Count the maximum number of elements that can be selected from the array | C # implementation of the approach ; Function to return the maximum count of selection possible from the given array following the given process ; Initialize result ; Sorting the array ; Initialize the select variable ; Loop through array ; If selection is possible ; Increment result ; Increment selection variable ; Driver Code
using System ; class GFG { static int maxSelectionCount ( int [ ] a , int n ) { int res = 0 ; Array . Sort ( a ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } public static void Main ( ) { int [ ] arr = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = arr . Length ; Console . WriteLine ( maxSelectionCount ( arr , N ) ) ; } }
Print combinations of distinct numbers which add up to give sum N | C # implementation of the approach ; arr [ ] to store all the distinct elements index - next location in array num - given number reducedNum - reduced number ; Set to store all the distinct elements ; Base condition ; Iterate over all the elements and store it into the set ; Calculate the sum of all the elements of the set ; Compare whether the sum is equal to n or not , if it is equal to n print the numbers ; Find previous number stored in the array ; Store all the numbers recursively into the arr [ ] ; Function to find all the distinct combinations of n ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void findCombinationsUtil ( int [ ] arr , int index , int n , int red_num ) { HashSet < int > s = new HashSet < int > ( ) ; int sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num == 0 ) { for ( int i = 0 ; i < index ; i ++ ) { s . Add ( arr [ i ] ) ; } foreach ( int itr in s ) { sum = sum + itr ; } if ( sum == n ) { foreach ( int i in s ) { Console . Write ( i + " ▁ " ) ; } Console . WriteLine ( ) ; return ; } } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } static void findCombinations ( int n ) { int [ ] a = new int [ n ] ; findCombinationsUtil ( a , 0 , n , n ) ; } public static void Main ( String [ ] arr ) { int n = 7 ; findCombinations ( n ) ; } }
Check whether an array can be made strictly increasing by modifying atmost one element | C # implementation of the approach ; Function that returns true if arr [ ] can be made strictly increasing after modifying at most one element ; To store the number of modifications required to make the array strictly increasing ; Check whether the first element needs to be modify or not ; Loop from 2 nd element to the 2 nd last element ; Check whether arr [ i ] needs to be modified ; Modifying arr [ i ] ; Check if arr [ i ] is equal to any of arr [ i - 1 ] or arr [ i + 1 ] ; Check whether the last element needs to be modify or not ; If more than 1 modification is required ; Driver code
using System ; class GFG { static bool check ( int [ ] arr , int n ) { int modify = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] / 2 ; modify ++ ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] < arr [ i ] && arr [ i + 1 ] < arr [ i ] ) || ( arr [ i - 1 ] > arr [ i ] && arr [ i + 1 ] > arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; modify ++ ; } } if ( arr [ n - 1 ] < arr [ n - 2 ] ) modify ++ ; if ( modify > 1 ) return false ; return true ; } public static void Main ( ) { int [ ] arr = { 2 , 4 , 8 , 6 , 9 , 12 } ; int n = arr . Length ; if ( check ( arr , n ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }
Check if the given matrix is increasing row and column wise | C # implementation of the approach ; Function that returns true if the matrix is strictly increasing ; Check if the matrix is strictly increasing ; Out of bound condition ; Out of bound condition ; Driver code
using System ; class GFG { static int N = 2 ; static int M = 2 ; static Boolean isMatrixInc ( int [ , ] a ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i - 1 >= 0 ) { if ( a [ i , j ] <= a [ i - 1 , j ] ) return false ; } if ( j - 1 >= 0 ) { if ( a [ i , j ] <= a [ i , j - 1 ] ) return false ; } } } return true ; } public static void Main ( String [ ] args ) { int [ , ] a = { { 2 , 10 } , { 11 , 20 } } ; if ( isMatrixInc ( a ) ) Console . WriteLine ( " Yes " ) ; else Console . WriteLine ( " No " ) ; } }