codeparrot/xlcost-text-to-code huggingface dataset:
Count ways to partition a string such that both parts have equal distinct characters | Java implementation to count the number of ways to partition the String such that each partition have same number of distinct characters in the String ; Function to count the distinct characters in the String ; Frequency of each character ; Loop to count the frequency of each character of the String ; If frequency is greater than 0 then the character occured ; Function to count the number of ways to partition the String such that each partition have same number of distinct character ; Loop to choose the partition index for the String ; Divide in two parts ; Check whether number of distinct characters are equal ; Driver Code
class GFG { static int distinctChars ( String s ) { int freq [ ] = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } static int waysToSplit ( String s ) { int n = s . length ( ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { String left = s . substring ( 0 , i ) ; String right = s . substring ( i , n ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { String s = " ababa " ; System . out . print ( waysToSplit ( s ) ) ; } }
Count ways to partition a string such that both parts have equal distinct characters | Java implementation to count the number of ways to partition the string such that each partition have same number of distinct characters in the string ; Function to count the number of ways to partition the string such that each partition have same number of distinct character ; Prefix and suffix array for distinct character from start and end ; To check whether a character has appeared till ith index ; Calculating prefix array ; Character appears for the first time in string ; Character is visited ; Resetting seen for suffix calculation ; Calculating the suffix array ; Character appears for the first time ; This character has now appeared ; Loop to calculate the number partition points in the string ; Check whether number of distinct characters are equal ; Driver Code
class GFG { static int waysToSplit ( String s ) { int n = s . length ( ) ; int answer = 0 ; int prefix [ ] = new int [ n ] ; int suffix [ ] = new int [ n ] ; int seen [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int prev = ( i - 1 >= 0 ? prefix [ i - 1 ] : 0 ) ; if ( seen [ s . charAt ( i ) - ' a ' ] == 0 ) { prefix [ i ] += ( prev + 1 ) ; } else prefix [ i ] = prev ; seen [ s . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) seen [ i ] = 0 ; suffix [ n - 1 ] = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int prev = suffix [ i ] ; if ( seen [ s . charAt ( i ) - ' a ' ] == 0 ) { suffix [ i - 1 ] += ( prev + 1 ) ; } else suffix [ i - 1 ] = prev ; seen [ s . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( prefix [ i ] == suffix [ i ] ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { String s = " ababa " ; System . out . println ( waysToSplit ( s ) ) ; } }
Counts paths from a point to reach Origin : Memory Optimized | Java implementation to count the paths from a points to origin ; Function to count the paths from a point to the origin ; Base Case when the point is already at origin ; Base Case when the point is on the x or y - axis ; Loop to fill all the position as 1 in the array ; Loop to count the number of paths from a point to origin in bottom - up manner ; Driver Code ; Function Call
class GFG { static long Count_Paths ( int x , int y ) { if ( x == 0 && y == 0 ) return 0 ; if ( x == 0 y == 0 ) return 1 ; int [ ] dp = new int [ Math . max ( x , y ) + 1 ] ; int p = Math . max ( x , y ) , q = Math . min ( x , y ) ; for ( int i = 0 ; i <= p ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i <= q ; i ++ ) for ( int j = 1 ; j <= p ; j ++ ) dp [ j ] += dp [ j - 1 ] ; return dp [ p ] ; } public static void main ( String [ ] args ) { int x = 3 , y = 3 ; System . out . print ( " Number ▁ of ▁ Paths ▁ " + Count_Paths ( x , y ) ) ; } }
Find the numbers present at Kth level of a Fibonacci Binary Tree | Java program to print the Fibonacci numbers present at K - th level of a Binary Tree ; Initializing the max value ; Array to store all the fibonacci numbers ; Function to generate fibonacci numbers using Dynamic Programming ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous two numbers in the series and store it ; Function to print the Fibonacci numbers present at Kth level of a Binary Tree ; Finding the left and right index ; Iterating and printing the numbers ; Driver code ; Precomputing Fibonacci numbers
import java . util . * ; class GFG { static final int MAX_SIZE = 100005 ; static int [ ] fib = new int [ MAX_SIZE + 1 ] ; static void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static void printLevel ( int level ) { int left_index = ( int ) Math . pow ( 2 , level - 1 ) ; int right_index = ( int ) ( Math . pow ( 2 , level ) - 1 ) ; for ( int i = left_index ; i <= right_index ; i ++ ) { System . out . print ( fib [ i - 1 ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; } }
Count the number of ways to divide N in k groups incrementally | Java implementation to count the number of ways to divide N in groups such that each group has K number of elements ; Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; Base Case ; If N is divides completely into less than k groups ; Put all possible values greater equal to prev ; Function to count the number of ways to divide the number N ; Driver Code
import java . util . * ; class GFG { static int calculate ( int pos , int prev , int left , int k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; int answer = 0 ; for ( int i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return answer ; } static int countWaystoDivide ( int n , int k ) { return calculate ( 0 , 1 , n , k ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 4 ; System . out . print ( countWaystoDivide ( N , K ) ) ; } }
Count the number of ways to divide N in k groups incrementally | Java implementation to count the number of ways to divide N in groups such that each group has K number of elements ; DP Table ; Function to count the number of ways to divide the number N in groups such that each group has K number of elements ; Base Case ; if N is divides completely into less than k groups ; If the subproblem has been solved , use the value ; put all possible values greater equal to prev ; Function to count the number of ways to divide the number N in groups ; Initialize DP Table as - 1 ; Driver Code
import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 500 ] [ 500 ] [ 500 ] ; static int calculate ( int pos , int prev , int left , int k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; if ( dp [ pos ] [ prev ] [ left ] != - 1 ) return dp [ pos ] [ prev ] [ left ] ; int answer = 0 ; for ( int i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return dp [ pos ] [ prev ] [ left ] = answer ; } static int countWaystoDivide ( int n , int k ) { for ( int i = 0 ; i < 500 ; i ++ ) { for ( int j = 0 ; j < 500 ; j ++ ) { for ( int l = 0 ; l < 500 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return calculate ( 0 , 1 , n , k ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 4 ; System . out . print ( countWaystoDivide ( N , K ) ) ; } }
Maximum score possible after performing given operations on an Array | Java program to find the maximum score after given operations ; Function to calculate maximum score recursively ; Base case ; Sum of array in range ( l , r ) ; If the operation is even - numbered the score is decremented ; Exploring all paths , by removing leftmost and rightmost element and selecting the maximum value ; Function to find the max score ; Prefix sum array ; Calculating prefix_sum ; Driver code
import java . util . * ; class GFG { static int maxScore ( int l , int r , int prefix_sum [ ] , int num ) { if ( l > r ) return 0 ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; return current_sum + Math . max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ; } static int findMaxScore ( int a [ ] , int n ) { int prefix_sum [ ] = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; int A [ ] = { 1 , 2 , 3 , 4 , 2 , 6 } ; System . out . print ( findMaxScore ( A , n ) ) ; } }
Finding powers of any number P in N ! | Java program to find the power of P in N ! ; Map to store all the prime factors of P ; Function to find the prime factors of N im Map ; Clear map ; Check for factors of 2 ; Find all the prime factors ; If i is a factors then increase the frequency of i ; Function to find the power of prime number P in N ! ; Loop until temp <= N ; Add the number of numbers divisible by N ; Each time multiply temp by P ; Returns ans ; Function that find the powers of any P in N ! ; Find all prime factors of number P ; To store the powers of all prime factors ; Traverse the map ; Prime factor and corres . powers ; Find power of prime factor primeFac ; Divide frequency by facPow ; Store the power of primeFac ^ facPow ; Return the minimum element in Power array ; Driver 's Code ; Function to find power of P in N !
import java . util . * ; class GFG { static HashMap < Integer , Integer > Map = new HashMap < Integer , Integer > ( ) ; static void findPrimeFactors ( int N ) { int i ; Map . clear ( ) ; while ( N % 2 == 0 ) { if ( Map . containsKey ( 2 ) ) Map . put ( 2 , Map . get ( 2 ) + 1 ) ; else Map . put ( 2 , 1 ) ; N /= 2 ; } for ( i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { while ( N % i == 0 ) { if ( Map . containsKey ( i ) ) Map . put ( i , Map . get ( i ) + 1 ) ; else Map . put ( i , 1 ) ; N /= i ; } } if ( N > 2 ) { if ( Map . containsKey ( N ) ) Map . put ( N , Map . get ( N ) + 1 ) ; else Map . put ( N , 1 ) ; } } static int PowInFactN ( int N , int P ) { int ans = 0 ; int temp = P ; while ( temp <= N ) { ans += N / temp ; temp = temp * P ; } return ans ; } static int findPowers ( int N , int P ) { findPrimeFactors ( P ) ; Vector < Integer > Powers = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : Map . entrySet ( ) ) { int primeFac = it . getKey ( ) ; int facPow = it . getValue ( ) ; int p = PowInFactN ( N , primeFac ) ; p /= facPow ; Powers . add ( p ) ; } return Collections . min ( Powers ) ; } public static void main ( String [ ] args ) { int N = 24 , P = 4 ; System . out . print ( findPowers ( N , P ) ) ; } }
Longest common subarray in the given two arrays | Java program to DP approach to above solution ; Function to find the maximum length of equal subarray ; Auxiliary dp [ ] [ ] array ; Updating the dp [ ] [ ] table in Bottom Up approach ; If A [ i ] is equal to B [ i ] then dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; Find maximum of all the values in dp [ ] [ ] array to get the maximum length ; Update the length ; Return the maximum length ; Driver Code ; Function call to find maximum length of subarray
class GFG { static int FindMaxLength ( int A [ ] , int B [ ] , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( A [ i ] == B [ j ] ) dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; } } int maxm = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { maxm = Math . max ( maxm , dp [ i ] [ j ] ) ; } } return maxm ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 8 , 2 , 1 } ; int B [ ] = { 8 , 2 , 1 , 4 , 7 } ; int n = A . length ; int m = B . length ; System . out . print ( FindMaxLength ( A , B , n , m ) ) ; } }
Maximum contiguous decreasing sequence obtained by removing any one element | Java program to find maximum length of decreasing sequence by removing at most one element ; Function to find the maximum length ; Initialise maximum length to 1 ; Initialise left [ ] to find the length of decreasing sequence from left to right ; Initialise right [ ] to find the length of decreasing sequence from right to left ; Initially store 1 at each index of left and right array ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the right array ; Store the length of longest continuous decreasing sequence in maximum ; Iterate over the array arr [ ] to store length of decreasing sequence that can be obtained at every index in the left array ; Check if we can obtain a longer decreasing sequence after removal of any element from the array arr [ ] with the help of left [ ] & right [ ] ; Return maximum length of sequence ; Driver code ; Function calling
class GFG { static int maxLength ( int [ ] a , int n ) { int maximum = 1 ; int left [ ] = new int [ n ] ; int right [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = 1 ; right [ i ] = 1 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { right [ i ] = right [ i + 1 ] + 1 ; } maximum = Math . max ( maximum , right [ i ] ) ; } for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { left [ i ] = left [ i - 1 ] + 1 ; } } if ( n > 2 ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i - 1 ] > a [ i + 1 ] ) { maximum = Math . max ( maximum , left [ i - 1 ] + right [ i + 1 ] ) ; } } } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 7 , 3 , 5 , 2 , 9 } ; int n = arr . length ; System . out . println ( maxLength ( arr , n ) ) ; } }
Longest alternating subsequence in terms of positive and negative integers | Java program to find the length of longest alternate subsequence ; LAS [ i ] [ pos ] array to find the length of LAS till index i by including or excluding element arr [ i ] on the basis of value of pos ; Base Case ; If current element is positive and pos is 1 Include the current element and change pos to 0 ; Recurr for the next iteration ; If current element is negative and pos is o Include the current element and change pos to 1 ; Recurr for the next iteration ; If current element is excluded , reccur for next iteration ; Driver 's Code ; Print LAS
class GFG { static int LAS [ ] [ ] = new int [ 1000 ] [ 2 ] ; static int solve ( int arr [ ] , int n , int i , int pos ) { if ( i == n ) return 0 ; if ( LAS [ i ] [ pos ] == 1 ) return LAS [ i ] [ pos ] ; int inc = 0 , exc = 0 ; if ( arr [ i ] > 0 && pos == 1 ) { pos = 0 ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } else if ( arr [ i ] < 0 && pos == 0 ) { pos = 1 ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } exc = solve ( arr , n , i + 1 , pos ) ; LAS [ i ] [ pos ] = Math . max ( inc , exc ) ; return LAS [ i ] [ pos ] ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 } ; int n = arr . length ; System . out . println ( Math . max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ; } }
Number of subsequences with negative product | Java implementation of the approach ; Function to return the count of all the subsequences with negative product ; To store the count of positive elements in the array ; To store the count of negative elements in the array ; If the current element is positive ; If the current element is negative ; For all the positive elements of the array ; For all the negative elements of the array ; Driver code
import java . util . * ; class GFG { static int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 4 , - 1 , 6 } ; int n = arr . length ; System . out . print ( cntSubSeq ( arr , n ) ) ; } }
Find minimum number of steps to reach the end of String | Java implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valiINT_MAXd move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code
class GFG { final static int INT_MAX = Integer . MAX_VALUE ; static int minSteps ( String str , int n , int k ) { if ( str . charAt ( n - 1 ) == '0' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int dp [ ] = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str . charAt ( i + k ) == '1' ) steps = Math . min ( steps , dp [ i + k ] ) ; if ( str . charAt ( i + 1 ) == '1' ) steps = Math . min ( steps , dp [ i + 1 ] ) ; if ( str . charAt ( i + 2 ) == '1' ) steps = Math . min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return - 1 ; return dp [ 0 ] ; } public static void main ( String [ ] args ) { String str = "101000011" ; int n = str . length ( ) ; int k = 5 ; System . out . println ( minSteps ( str , n , k ) ) ; } }
Length of longest Palindromic Subsequence of even length with no two adjacent characters same | Java implementation of above approach ; To store the values of subproblems ; Function to find the Longest Palindromic subsequence of even length with no two adjacent characters same ; Base cases If the String length is 1 return 0 ; If the String length is 2 ; Check if the characters match ; If the value with given parameters is previously calculated ; If the first and last character of the String matches with the given character ; Remove the first and last character and call the function for all characters ; If it does not match ; Then find the first and last index of given character in the given String ; Take the subString from i to j and call the function with subString and the given character ; Store the answer for future use ; Driver code ; Check for all starting characters
import java . util . * ; class GFG { static Map < String , Integer > dp = new HashMap < > ( ) ; static Integer solve ( char [ ] s , char c ) { if ( s . length == 1 ) return 0 ; if ( s . length == 2 ) { if ( s [ 0 ] == s [ 1 ] && s [ 0 ] == c ) return 1 ; else return 0 ; } if ( dp . containsKey ( String . valueOf ( s ) + " ▁ " + c ) ) return dp . get ( String . valueOf ( s ) + " ▁ " + c ) ; Integer ans = 0 ; if ( s [ 0 ] == s [ s . length - 1 ] && s [ 0 ] == c ) { for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) if ( c1 != c ) ans = Math . max ( ans , 1 + solve ( Arrays . copyOfRange ( s , 1 , s . length - 1 ) , c1 ) ) ; } else { for ( Integer i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == c ) { for ( Integer j = s . length - 1 ; j > i ; j -- ) if ( s [ j ] == c ) { if ( j == i ) break ; ans = solve ( Arrays . copyOfRange ( s , i , j + 1 ) , c ) ; break ; } break ; } } } dp . put ( String . valueOf ( s ) + " ▁ " + c , ans ) ; return dp . get ( String . valueOf ( s ) + " ▁ " + c ) ; } public static void main ( String [ ] args ) { String s = " abscrcdba " ; Integer ma = 0 ; for ( char c1 = ' a ' ; c1 <= ' z ' ; c1 ++ ) ma = Math . max ( ma , solve ( s . toCharArray ( ) , c1 ) * 2 ) ; System . out . print ( ma + "NEW_LINE"); } }
Eggs dropping puzzle | Set 2 | Java implementation of the approach ; Function to return the minimum number of trials needed in the worst case with n eggs and k floors ; Fill all the entries in table using optimal substructure property ; Return the minimum number of moves ; Driver code
class GFG { static int eggDrop ( int n , int k ) { int dp [ ] [ ] = new int [ k + 1 ] [ n + 1 ] ; int x = 0 ; while ( dp [ x ] [ n ] < k ) { x ++ ; for ( int i = 1 ; i <= n ; i ++ ) dp [ x ] [ i ] = dp [ x - 1 ] [ i - 1 ] + dp [ x - 1 ] [ i ] + 1 ; } return x ; } public static void main ( String args [ ] ) { int n = 2 , k = 36 ; System . out . println ( eggDrop ( n , k ) ) ; } }
Maximum length of Strictly Increasing Sub | Java implementation of the approach ; Function to return the maximum length of strictly increasing subarray after removing atmost one element ; Create two arrays pre and pos ; Find out the contribution of the current element in array [ 0 , i ] and update pre [ i ] ; Find out the contribution of the current element in array [ N - 1 , i ] and update pos [ i ] ; Calculate the maximum length of the stricly increasing subarray without removing any element ; Calculate the maximum length of the strictly increasing subarray after removing the current element ; Driver code
class GFG { static int maxIncSubarr ( int a [ ] , int n ) { int pre [ ] = new int [ n ] ; int pos [ ] = new int [ n ] ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } int ans = 0 ; l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = Math . max ( ans , l ) ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = Math . max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int n = arr . length ; System . out . println ( maxIncSubarr ( arr , n ) ) ; } }
Number of square matrices with all 1 s | Java program to return the number of square submatrices with all 1 s ; Function to return the number of square submatrices with all 1 s ; Initialize count variable ; If a [ i ] [ j ] is equal to 0 ; Calculate number of square submatrices ending at ( i , j ) ; Calculate the sum of the array ; Driver code
class GFG { final static int n = 3 ; final static int m = 3 ; static int countSquareMatrices ( int a [ ] [ ] , int N , int M ) { int count = 0 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == 0 ) continue ; a [ i ] [ j ] = Math . min ( Math . min ( a [ i - 1 ] [ j ] , a [ i ] [ j - 1 ] ) , a [ i - 1 ] [ j - 1 ] ) + 1 ; } } for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) count += a [ i ] [ j ] ; return count ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 0 , 1 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } } ; System . out . println ( countSquareMatrices ( arr , n , m ) ) ; } }