codeparrot/xlcost-text-to-code huggingface dataset:
Count of distinct power of prime factor of N | Java program for the above approach ; Function to count the number of distinct positive power of prime factor of integer N ; Iterate for all prime factor ; If it is a prime factor , count the total number of times it divides n . ; Find the Number of distinct possible positive numbers ; Return the final count ; Driver code ; Given Number N ; Function Call
import java . util . * ; class GFG { static int countFac ( int n ) { int m = n ; int count = 0 ; for ( int i = 2 ; ( i * i ) <= m ; ++ i ) { int total = 0 ; while ( n % i == 0 ) { n /= i ; ++ total ; } int temp = 0 ; for ( int j = 1 ; ( temp + j ) <= total ; ++ j ) { temp += j ; ++ count ; } } if ( n != 1 ) ++ count ; return count ; } public static void main ( String [ ] args ) { int N = 24 ; System . out . println ( countFac ( N ) ) ; } }
Minimum total sum from the given two arrays | Java 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
import java . util . * ; 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 ] ) ; } System . out . print ( Math . min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) + "NEW_LINE"); } 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 | Java 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
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 . charAt ( i ) == '1' ) last_idx = i ; } if ( last_idx == - 1 ) { ++ ops ; S . setCharAt ( 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 . charAt ( i + K - 1 ) == '1' ) last_idx = i + K - 1 ; if ( last_idx == - 1 ) { ++ ops ; S . setCharAt ( i + K - 1 , '1' ) ; last_idx = i + K - 1 ; } } return ops ; } public static void main ( String [ ] args ) { String S = "001010000" ; int K = 3 ; System . out . println ( minimumMoves ( S , K ) ) ; } }
Lexicographically smallest string which differs from given strings at exactly K indices | Java 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 . ceil ( 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
import java . util . * ; 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 ) { System . out . print ( " - 1" + "NEW_LINE"); 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 -- ; } } } System . out . print ( new String ( s3 ) + "NEW_LINE"); } 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 ; } } } } System . out . print ( new String ( s3 ) + "NEW_LINE"); } } } 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 | Java 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
import java . util . * ; 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 ++ ; } } } System . out . print ( 4 * count ) ; } public static void main ( String [ ] args ) { int N = 80 ; findSolutions ( N ) ; } }
Find the integral roots of a given Cubic equation | Java 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
import java . util . * ; 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 ) { System . out . println ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } System . out . println ( " NA " ) ; } public static void main ( String args [ ] ) { 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 | Java code for the above program ; function to count the ordered triplets ( R , G , B ) ; count the B ( blue ) colour ; Driver Code
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 . charAt ( i ) == ' B ' ) Blue_Count ++ ; } for ( i = 0 ; i < len ; i ++ ) { if ( color . charAt ( i ) == ' B ' ) Blue_Count -- ; if ( color . charAt ( i ) == ' R ' ) Red_Count ++ ; if ( color . charAt ( i ) == ' G ' ) result += Red_Count * Blue_Count ; } return result ; } public static void main ( String [ ] args ) { String color = " RRGGBBRGGBB " ; System . out . println ( countTriplets ( color ) ) ; } }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | Java 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
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 ; System . out . println ( naive_find_x ( N ) ) ; } }
Minimum value of K such that sum of cubes of first K natural number is greater than equal to N | Java 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
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 ( String [ ] args ) { int N = 100 ; System . out . println ( binary_searched_find_x ( N ) ) ; } }
Length of the longest ZigZag subarray of the given array | Java 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
import java . io . * ; import java . util . * ; 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 ; System . out . println ( lenOfLongZigZagArr ( arr , n ) ) ; } }
Check if a given number is a Perfect square using Binary Search | Java 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
import java . util . * ; class GFG { static int checkPerfectSquare ( long N , long start , long last ) { long mid = ( start + last ) / 2 ; if ( start > last ) { return - 1 ; } if ( mid * mid == N ) { return ( int ) mid ; } else if ( mid * mid > N ) { return checkPerfectSquare ( N , start , mid - 1 ) ; } else { return checkPerfectSquare ( N , mid + 1 , last ) ; } } public static void main ( String [ ] args ) { long N = 65 ; System . out . println ( checkPerfectSquare ( N , 1 , N ) ) ; } }
Maximum count number of valley elements in a subarray of size K | Java 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
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 ; } System . out . println ( 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 | Java 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 boolean vector every time we just reset all visited letter to false ; Print count for each letter ; Driver program ; Given array of Strings ; Call the countStrings function
class GFG { static void CountStrings ( String [ ] str ) { int size = str . length ; int [ ] count = new int [ 26 ] ; boolean [ ] visited = new boolean [ 26 ] ; for ( int i = 0 ; i < size ; ++ i ) { for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { if ( visited [ str [ i ] . charAt ( j ) - ' a ' ] == false ) count [ str [ i ] . charAt ( j ) - ' a ' ] ++ ; visited [ str [ i ] . charAt ( j ) - ' a ' ] = true ; } for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { visited [ str [ i ] . charAt ( j ) - ' a ' ] = false ; } } for ( int i = 0 ; i < 26 ; ++ i ) { System . out . print ( count [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { String [ ] str = { " i " , " will " , " practice " , " everyday " } ; CountStrings ( str ) ; } }
Find subarray of Length K with Maximum Peak | Java 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
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 , 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 ; } System . out . print ( " Left ▁ = ▁ " + ( left + 1 ) + "NEW_LINE"); System . out . print ( " Right ▁ = ▁ " + ( left + k ) + "NEW_LINE"); System . out . print ( " Peak ▁ = ▁ " + peak + "NEW_LINE"); } 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 | Java 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 ; Vector 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
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static void distanceSort ( int arr [ ] , int K , int n ) { Vector vd [ ] = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { vd [ i ] = new Vector ( ) ; } int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == K ) { pos = i ; break ; } } int i = pos - 1 , j = 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 ) { Collections . sort ( vd [ i ] ) ; for ( j = 0 ; j < vd [ i ] . size ( ) ; j ++ ) { int element = ( int ) vd [ i ] . get ( j ) ; System . out . print ( element + " ▁ " ) ; } } } public static void main ( String s [ ] ) { 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 | JAVA 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 final answer ; Recursive calls ; Driver code
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 ) ; System . out . print ( ans [ 0 ] + " , ▁ " ) ; System . out . print ( ans [ 1 ] + "NEW_LINE"); } }
Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X | Java 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
import java . util . * ; 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 ++ ) { Vector < Integer > temp = new Vector < > ( ) ; temp . add ( a [ i ] ) ; temp . add ( a [ j ] ) ; temp . add ( a [ k ] ) ; Collections . sort ( temp ) ; if ( temp . get ( 0 ) < temp . get ( 1 ) && temp . get ( 1 ) < temp . get ( 2 ) && temp . get ( 0 ) * temp . get ( 1 ) * temp . get ( 2 ) <= x ) answer ++ ; } } } return answer ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 5 , 7 } ; int N = A . length ; int X = 42 ; System . out . println ( countTriplets ( A , N , X ) ) ; } }
Largest value of x such that axx is N | Java 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
import java . util . * ; 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 . ceil ( ( 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 ; System . out . print ( get ( a , b , n ) + "NEW_LINE"); } }
Maximum size square Sub | Java 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 final answer ; Driver Code ; Given target sum ; Function Call
import java . util . * ; class GFG { static void findMaxMatrixSize ( int [ ] [ ] arr , int K ) { int i , j ; int n = arr . length ; int m = arr [ 0 ] . length ; 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 ) ; } } System . out . print ( ans + "NEW_LINE"); } 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 ) ; } }