codeparrot/xlcost-text-to-code huggingface dataset:
Minimum total sum from the given two arrays | C # program for the above approach ; Function that prints minimum sum after selecting N elements ; Initialise the dp array ; Base Case ; Adding the element of array a if previous element is also from array a ; Adding the element of array a if previous element is from array b ; Adding the element of array b if previous element is from array a with an extra penalty of integer C ; Adding the element of array b if previous element is also from array b ; Print the minimum sum ; Driver Code ; Given array arr1 [ ] and arr2 [ ] ; Given cost ; Function call
using System ; class GFG { static void minimumSum ( int [ ] a , int [ ] b , int c , int n ) { int [ , ] dp = new int [ n , 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i , j ] = ( int ) 1e6 ; } } dp [ 0 , 0 ] = a [ 0 ] ; dp [ 0 , 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 0 ] + a [ i ] ) ; dp [ i , 0 ] = Math . Min ( dp [ i , 0 ] , dp [ i - 1 , 1 ] + a [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 0 ] + b [ i ] + c ) ; dp [ i , 1 ] = Math . Min ( dp [ i , 1 ] , dp [ i - 1 , 1 ] + b [ i ] ) ; } Console . Write ( Math . Min ( dp [ n - 1 , 0 ] , dp [ n - 1 , 1 ] ) + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr1 = { 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 } ; int [ ] arr2 = { 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 } ; int C = 2 ; int N = arr1 . Length ; minimumSum ( arr1 , arr2 , C , N ) ; } }
Minimum flips required in a binary string such that all K | C # program to find the minimum numbers of flips required in a binary string such that all substrings of size K has atleast one 1 ; Function to calculate and return the minimum number of flips to make string valid ; Stores the count of required flips ; Stores the last index of '1' in the string ; Check for the first substring of length K ; If i - th character is '1' ; If the substring had no '1' ; Increase the count of required flips ; Flip the last index of the window ; Update the last index which contains 1 ; Check for remaining substrings ; If last_idx does not belong to current window make it - 1 ; If the last character of the current substring is '1' , then update last_idx to i + k - 1 ; ; If last_idx == - 1 , then the current substring has no 1 ; Increase the count of flips ; Update the last index of the current window ; Store the last index of current window as the index of last '1' in the string ; Return the number of operations ; Driver Code
using System ; using System . Text ; class GFG { public static int minimumMoves ( String s , int K ) { StringBuilder S = new StringBuilder ( s ) ; int N = S . Length ; int ops = 0 ; int last_idx = - 1 ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == '1' ) last_idx = i ; } if ( last_idx == - 1 ) { ++ ops ; S . Insert ( K - 1 , '1' ) ; last_idx = K - 1 ; } for ( int i = 1 ; i < N - K + 1 ; i ++ ) { if ( last_idx < i ) last_idx = - 1 ; if ( S [ i + K - 1 ] == '1' ) last_idx = i + K - 1 ; if ( last_idx == - 1 ) { ++ ops ; S . Insert ( i + K - 1 , '1' ) ; last_idx = i + K - 1 ; } } return ops ; } public static void Main ( String [ ] args ) { String S = "001010000" ; int K = 3 ; Console . WriteLine ( minimumMoves ( S , K ) ) ; } }
Lexicographically smallest string which differs from given strings at exactly K indices | C # program for the above approach ; Function to find the String which differ at exactly K positions ; Initialise s3 as s2 ; Number of places at which s3 differ from s2 ; Minimum possible value is Math . Ceiling ( d / 2 ) if it is not possible then - 1 ; Case 2 when K is less equal d ; X show the modification such that this position only differ from only one String ; Modify the position such that this differ from both S1 & S2 & decrease the T at each step ; Finding the character which is different from both S1 and S2 ; After we done T start modification to meet our requirement for X type ; Resultant String ; Case 1 when K > d In first step , modify all the character which are not same in S1 and S3 ; Finding character which is different from both S1 and S2 ; Our requirement not satisfied by performing step 1. We need to modify the position which matches in both String ; Finding the character which is different from both S1 and S2 ; Resultant String ; Driver Code ; Given two Strings ; Function Call
using System ; class GFG { static char [ ] arr = { ' a ' , ' b ' , ' c ' } ; static void findString ( int n , int k , char [ ] s1 , char [ ] s2 ) { char [ ] s3 = s2 ; int d = 0 ; for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) d ++ ; } if ( ( d + 1 ) / 2 > k ) { Console . Write ( " - 1" + " STRNEWLINE " ) ; return ; } else { if ( k <= d ) { int X = d - k ; int T = 2 * k - d ; for ( int i = 0 ; i < s3 . Length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( T > 0 ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s2 [ i ] ) { s3 [ i ] = arr [ j ] ; T -- ; break ; } } } else if ( X > 0 ) { s3 [ i ] = s1 [ i ] ; X -- ; } } } Console . Write ( new String ( s3 ) + " STRNEWLINE " ) ; } else { for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] != s3 [ i ] ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } for ( int i = 0 ; i < s1 . Length ; i ++ ) { if ( s1 [ i ] == s3 [ i ] && k > 0 ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } Console . Write ( new String ( s3 ) + " STRNEWLINE " ) ; } } } public static void Main ( String [ ] args ) { int N = 4 , k = 2 ; String S1 = " zzyy " ; String S2 = " zxxy " ; findString ( N , k , S1 . ToCharArray ( ) , S2 . ToCharArray ( ) ) ; } }
Count of pairs of integers whose difference of squares is equal to N | C # program for the above approach ; Function to find the integral solutions of the given equation ; Initialise count to 0 ; Iterate till sqrt ( N ) ; If divisor 's pair sum is even ; Print the total possible solutions ; Driver code ; Given number N ; Function Call
using System ; class GFG { static void findSolutions ( int N ) { int count = 0 ; for ( int i = 1 ; i <= Math . Sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( ( i + N / i ) % 2 == 0 ) { count ++ ; } } } Console . Write ( 4 * count ) ; } public static void Main ( String [ ] args ) { int N = 80 ; findSolutions ( N ) ; } }
Find the integral roots of a given Cubic equation | C # program for the above approach ; Function to find the value at x of the given equation ; Find the value equation at x ; Return the value of ans ; Function to find the integral solution of the given equation ; Initialise start and end ; Implement Binary Search ; Find mid ; Find the value of f ( x ) using current mid ; Check if current mid satisfy the equation ; Print mid and return ; Print " NA " if not found any integral solution ; Driver Code ; Function Call
using System ; class GFG { static long check ( int A , int B , int C , int D , long x ) { long ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } static void findSolution ( int A , int B , int C , int D , int E ) { long start = 0 , end = 100000 ; long mid , ans ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { Console . WriteLine ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } Console . Write ( " NA " ) ; } public static void Main ( ) { int A = 1 , B = 0 , C = 0 ; int D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ; } }
Count of ordered triplets ( R , G , B ) in a given original string | C # code for the above program ; function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver Code
using System ; class GFG { static int countTriplets ( String color ) { int result = 0 , Blue_Count = 0 ; int Red_Count = 0 ; int len = color . Length ; int i ; for ( i = 0 ; i < len ; i ++ ) { if ( color [ i ] == ' B ' ) Blue_Count ++ ; } for ( i = 0 ; i < len ; i ++ ) { if ( color [ i ] == ' B ' ) Blue_Count -- ; if ( color [ i ] == ' R ' ) Red_Count ++ ; if ( color [ i ] == ' G ' ) result += Red_Count * Blue_Count ; } return result ; } public static void Main ( string [ ] args ) { string color = " RRGGBBRGGBB " ; Console . WriteLine ( countTriplets ( color ) ) ; } }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C # program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Variable to store the sum of cubes ; Loop to find the number K ; If C is just greater then N , then break the loop ; Driver code
using System ; class GFG { static int naive_find_x ( int N ) { int c = 0 , i ; for ( i = 1 ; i < N ; i ++ ) { c += i * i * i ; if ( c >= N ) break ; } return i ; } public static void Main ( String [ ] args ) { int N = 100 ; Console . WriteLine ( naive_find_x ( N ) ) ; } }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | C # program to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Function to determine the minimum value of K such that the sum of cubes of first K natural number is greater than or equal to N ; Left bound ; Right bound ; Variable to store the answer ; Applying binary search ; Calculating mid value of the range ; If the sum of cubes of first mid natural numbers is greater than equal to N iterate the left half ; Sum of cubes of first mid natural numbers is less than N , then move to the right segment ; Driver code
using System ; class GFG { static int binary_searched_find_x ( int k ) { int l = 0 ; int r = k ; int ans = 0 ; while ( l <= r ) { int mid = l + ( r - l ) / 2 ; if ( Math . Pow ( ( ( mid * ( mid + 1 ) ) / 2 ) , 2 ) >= k ) { ans = mid ; r = mid - 1 ; } else { l = mid + 1 ; } } return ans ; } public static void Main ( ) { int N = 100 ; Console . Write ( binary_searched_find_x ( N ) ) ; } }
Length of the longest ZigZag subarray of the given array | C # implementation to find the length of longest zigzag subarray of the given array ; Function to find the length of longest zigZag contiguous subarray ; ' max ' to store the length of longest zigZag subarray ; ' len ' to store the lengths of longest zigZag subarray at different instants of time ; Traverse the array from the beginning ; Check if ' max ' length is less than the length of the current zigzag subarray . If true , then update ' max ' ; Reset ' len ' to 1 as from this element , again the length of the new zigzag subarray is being calculated ; Comparing the length of the last zigzag subarray with ' max ' ; Return required maximum length ; Driver Code
using System ; class GFG { static int lenOflongZigZagArr ( int [ ] a , int n ) { int max = 1 , len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && ( a [ i ] < a [ i + 1 ] ) ) len ++ ; else if ( i % 2 == 1 && ( a [ i ] > a [ i + 1 ] ) ) len ++ ; else { if ( max < len ) max = len ; len = 1 ; } } if ( max < len ) max = len ; return max ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . Length ; Console . WriteLine ( lenOflongZigZagArr ( arr , n ) ) ; } }
Check if a given number is a Perfect square using Binary Search | C # program to check if a given number is Perfect square using Binary Search ; Function to check for perfect square number ; Find the mid value from start and last ; Check if we got the number which is square root of the perfect square number N ; If the square ( mid ) is greater than N it means only lower values then mid will be possibly the square root of N ; If the square ( mid ) is less than N it means only higher values then mid will be possibly the square root of N ; Driver code
using System ; class GFG { public static int checkPerfectSquare ( int N , int start , int last ) { int mid = ( start + last ) / 2 ; if ( start > last ) { return - 1 ; } if ( mid * mid == N ) { return mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } public static int Main ( ) { int N = 65 ; Console . Write ( checkPerfectSquare ( N , 1 , N ) ) ; return 0 ; } }
Maximum count number of valley elements in a subarray of size K | C # implementation to find the maximum number of valley elements in the subarrays of size K ; Function to find the valley elements in the array which contains in the subarrays of the size K ; Increment min_point if element at index i is smaller than element at index i + 1 and i - 1 ; final_point to maintain maximum of min points of subarray ; Iterate over array from kth element ; Leftmost element of subarray ; Rightmost element of subarray ; If new subarray have greater number of min points than previous subarray , then final_point is modified ; Max minimum points in subarray of size k ; Driver Code
using System ; class GFG { static void minpoint ( int [ ] arr , int n , int k ) { int min_point = 0 ; for ( int i = 1 ; i < k - 1 ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) min_point += 1 ; } int final_point = min_point ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] && arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) min_point -= 1 ; if ( arr [ i - 1 ] < arr [ i ] && arr [ i - 1 ] < arr [ i - 2 ] ) min_point += 1 ; if ( min_point > final_point ) final_point = min_point ; } Console . WriteLine ( final_point ) ; } public static void Main ( string [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 } ; int n = arr . Length ; int k = 4 ; minpoint ( arr , n , k ) ; } }
For each lowercase English alphabet find the count of strings having these alphabets | C # program to find count of Strings for each letter [ a - z ] in english alphabet ; Function to find the countStrings for each letter [ a - z ] ; Initialize result as zero ; Mark all letter as not visited ; Loop through each Strings ; Increment the global counter for current character of String ; Instead of re - initialising bool vector every time we just reset all visited letter to false ; Print count for each letter ; Driver code ; Given array of Strings ; Call the countStrings function
using System ; class GFG { static void CountStrings ( String [ ] str ) { int size = str . Length ; int [ ] count = new int [ 26 ] ; bool [ ] visited = new bool [ 26 ] ; for ( int i = 0 ; i < size ; ++ i ) { for ( int j = 0 ; j < str [ i ] . Length ; ++ j ) { if ( visited [ str [ i ] [ j ] - ' a ' ] == false ) count [ str [ i ] [ j ] - ' a ' ] ++ ; visited [ str [ i ] [ j ] - ' a ' ] = true ; } for ( int j = 0 ; j < str [ i ] . Length ; ++ j ) { visited [ str [ i ] [ j ] - ' a ' ] = false ; } } for ( int i = 0 ; i < 26 ; ++ i ) { Console . Write ( count [ i ] + " ▁ " ) ; } } public static void Main ( String [ ] args ) { String [ ] str = { " i " , " will " , " practice " , " everyday " } ; CountStrings ( str ) ; } }
Find subarray of Length K with Maximum Peak | C # implementation to Find subarray of Length K with Maximum Peak ; Function to find the subarray ; Make prefix array to store the prefix sum of peak count ; Count peak for previous index ; Check if this element is a peak ; Increment the count ; Check if number of peak in the sub array whose l = i is greater or not ; Print the result ; Driver code
using System ; class GFG { static void findSubArray ( int [ ] a , int n , int k ) { int [ ] pref = new int [ n ] ; pref [ 0 ] = 0 ; for ( int i = 1 ; i < n - 1 ; ++ i ) { pref [ i ] = pref [ i - 1 ] ; if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) { pref [ i ] ++ ; } } int peak = 0 ; int left = 0 ; for ( int i = 0 ; i + k - 1 < n ; ++ i ) { if ( pref [ i + k - 2 ] - pref [ i ] > peak ) { peak = pref [ i + k - 2 ] - pref [ i ] ; left = i ; } } Console . Write ( " Left ▁ = ▁ " + ( left + 1 ) + " STRNEWLINE " ) ; Console . Write ( " Right ▁ = ▁ " + ( left + k ) + " STRNEWLINE " ) ; Console . Write ( " Peak ▁ = ▁ " + peak + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 2 , 1 } ; int n = arr . Length ; int k = 3 ; findSubArray ( arr , n , k ) ; } }
Sort integers in array according to their distance from the element K | C # implementation to Sort the integers in array according to their distance from given element K present in the array ; Function to get sorted array based on their distance from given integer K ; List to store respective elements with their distance from integer K ; Find the position of integer K ; Insert the elements with their distance from K in vector ; Element at distance 0 ; Elements at left side of K ; Elements at right side of K ; Print the vector content in sorted order ; Sort elements at same distance ; Print elements at distance i from K ; Driver Code
using System ; using System . Collections . Generic ; class GFG { static void distanceSort ( int [ ] arr , int K , int n ) { List < int > [ ] vd = new List < int > [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { vd [ i ] = new List < int > ( ) ; } int pos = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == K ) { pos = i ; break ; } } int j = pos + 1 ; i = pos - 1 ; vd [ 0 ] . Add ( arr [ pos ] ) ; while ( i >= 0 ) { vd [ pos - i ] . Add ( arr [ i ] ) ; -- i ; } while ( j < n ) { vd [ j - pos ] . Add ( arr [ j ] ) ; ++ j ; } for ( i = 0 ; i <= Math . Max ( pos , n - pos - 1 ) ; ++ i ) { vd [ i ] . Sort ( ) ; for ( j = 0 ; j < vd [ i ] . Count ; j ++ ) { int element = ( int ) vd [ i ] [ j ] ; Console . Write ( element + " ▁ " ) ; } } } public static void Main ( String [ ] args ) { int [ ] arr = { 14 , 1101 , 10 , 35 , 0 } ; int K = 35 ; int n = arr . Length ; distanceSort ( arr , K , n ) ; } }
Find Quotient and Remainder of two integer without using division operators | C # implementation to Find Quotient and Remainder of two integer without using / and % operator using Binary search ; Function to the quotient and remainder ; Check if start is greater than the end ; Calculate mid ; Check if n is greater than divisor then increment the mid by 1 ; Check if n is less than 0 then decrement the mid by 1 ; Check if n equals to divisor ; Return the readonly answer ; Recursive calls ; Driver code
using System ; public class GFG { static int [ ] find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return new int [ ] { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return new int [ ] { mid , n } ; } return find ( dividend , divisor , start , end ) ; } static int [ ] divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } public static void Main ( String [ ] args ) { int dividend = 10 , divisor = 3 ; int [ ] ans = divide ( dividend , divisor ) ; Console . Write ( ans [ 0 ] + " , ▁ " ) ; Console . Write ( ans [ 1 ] + " STRNEWLINE " ) ; } }
Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | C # implementation to count the number of unordered triplets such that the numbers are in increasing order and the product of them is less than or equal to integer X ; Function to count the number of triplets ; Iterate through all the triplets ; Rearrange the numbers in ascending order ; Check if the necessary conditions satisfy ; Increment count ; Return the answer ; Driver code
using System ; class GFG { static int countTriplets ( int [ ] a , int n , int x ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { int [ ] temp = { a [ i ] , a [ j ] , a [ k ] } ; Array . Sort ( temp ) ; if ( temp [ 0 ] < temp [ 1 ] && temp [ 1 ] < temp [ 2 ] && temp [ 0 ] * temp [ 1 ] * temp [ 2 ] <= x ) answer ++ ; } } } return answer ; } public static void Main ( ) { int [ ] A = { 3 , 2 , 5 , 7 } ; int N = A . Length ; int X = 42 ; Console . WriteLine ( countTriplets ( A , N , X ) ) ; } }
Largest value of x such that axx is N | C # implementation of the above approach ; Function to find log_b ( a ) ; Set two pointer for binary search ; Calculating number of digits of a * mid ^ mid in base b ; If number of digits > n we can simply ignore it and decrease our pointer ; If number of digits <= n , we can go higher to reach value exactly equal to n ; Return the largest value of x ; Driver Code
using System ; class GFG { static int log ( int a , int b ) { return ( int ) ( Math . Log10 ( a ) / Math . Log10 ( b ) ) ; } static int get ( int a , int b , int n ) { int lo = 0 , hi = ( int ) 1e6 ; int ans = 0 ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int dig = ( int ) Math . Ceiling ( ( double ) ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } public static void Main ( String [ ] args ) { int a = 2 , b = 2 , n = 6 ; Console . Write ( get ( a , b , n ) + " STRNEWLINE " ) ; } }
Maximum size square Sub | C # program for the above approach ; Function to find the maximum size of matrix with sum <= K ; N size of rows and M size of cols ; To store the prefix sum of matrix ; Create prefix sum ; Traverse each rows ; Update the prefix sum till index i x j ; To store the maximum size of matrix with sum <= K ; Traverse the sum matrix ; Index out of bound ; Maximum possible size of matrix ; Binary Search ; Find middle index ; Check whether sum <= K or not If Yes check for other half of the search ; Else check it in first half ; Update the maximum size matrix ; Print the readonly answer ; Driver Code ; Given target sum ; Function Call
using System ; class GFG { static void findMaxMatrixSize ( int [ , ] arr , int K ) { int i , j ; int n = arr . GetLength ( 0 ) ; int m = arr . GetLength ( 1 ) ; int [ , ] sum = new int [ n + 1 , m + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) { sum [ i , j ] = 0 ; continue ; } sum [ i , j ] = arr [ i - 1 , j - 1 ] + sum [ i - 1 , j ] + sum [ i , j - 1 ] - sum [ i - 1 , j - 1 ] ; } } int ans = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( i + ans - 1 > n j + ans - 1 > m ) break ; int mid , lo = ans ; int hi = Math . Min ( n - i + 1 , m - j + 1 ) ; while ( lo < hi ) { mid = ( hi + lo + 1 ) / 2 ; if ( sum [ i + mid - 1 , j + mid - 1 ] + sum [ i - 1 , j - 1 ] - sum [ i + mid - 1 , j - 1 ] - sum [ i - 1 , j + mid - 1 ] <= K ) { lo = mid ; } else { hi = mid - 1 ; } } ans = Math . Max ( ans , lo ) ; } } Console . Write ( ans + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { int [ , ] arr = { { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } } ; int K = 4 ; findMaxMatrixSize ( arr , K ) ; } }
Rank of all elements in a Stream in descending order when they arrive | C # program to rank of all elements in a Stream in descending order when they arrive ; FindRank function to find rank ; Rank of first element is always 1 ; Iterate over array ; As element let say its rank is 1 ; Element is compared with previous elements ; If greater than previous than rank is incremented ; print rank ; Driver code ; array named arr ; length of arr
using System ; class GFG { static void FindRank ( int [ ] arr , int length ) { Console . Write ( "1" + " ▁ " ) ; for ( int i = 1 ; i < arr . Length ; i ++ ) { int rank = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) rank ++ ; } Console . Write ( rank + " ▁ " ) ; } } public static void Main ( ) { int [ ] arr = { 88 , 14 , 69 , 30 , 29 , 89 } ; int len = arr . Length ; FindRank ( arr , len ) ; } }