codeparrot/xlcost-text-to-code huggingface dataset:
Array range queries over range queries | Java program to perform range queries over range queries . ; For prefix sum array ; This function is used to apply square root decomposition in the record array ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Function to print the resultant array ; Driver code ; If query is of type 2 then function call to record_func ; If query is of type 1 then simply add1 to the record array ; Merging the value of the block in the record array ; If query is of type 1 then the array elements are over - written by the record array ; The prefix sum of the array ; Printing the resultant array
public class GFG { static final int max = 10000 ; static void update ( int arr [ ] , int l ) { arr [ l ] += arr [ l - 1 ] ; } static void record_func ( int block_size , int block [ ] , int record [ ] , int l , int r , int value ) { while ( l < r && l % block_size != 0 && l != 0 ) { record [ l ] += value ; l ++ ; } while ( l + block_size <= r + 1 ) { block [ l / block_size ] += value ; l += block_size ; } while ( l <= r ) { record [ l ] += value ; l ++ ; } } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int arr [ ] = new int [ n ] , record [ ] = new int [ m ] ; int block_size = ( int ) Math . sqrt ( m ) ; int block [ ] = new int [ max ] ; int command [ ] [ ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( command [ i ] [ 0 ] == 2 ) { int x = i / ( block_size ) ; record_func ( block_size , block , record , command [ i ] [ 1 ] - 1 , command [ i ] [ 2 ] - 1 , ( block [ x ] + record [ i ] + 1 ) ) ; } else { record [ i ] ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { int check = ( i / block_size ) ; record [ i ] += block [ check ] ; } for ( int i = 0 ; i < m ; i ++ ) { if ( command [ i ] [ 0 ] == 1 ) { arr [ command [ i ] [ 1 ] - 1 ] += record [ i ] ; if ( ( command [ i ] [ 2 ] - 1 ) < n - 1 ) { arr [ ( command [ i ] [ 2 ] ) ] -= record [ i ] ; } } } for ( int i = 1 ; i < n ; i ++ ) { update ( arr , i ) ; } print ( arr , n ) ; } }
Array range queries over range queries | Java program to perform range queries over range queries . ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add element of BITree to sum ; Move index to parent node in getSum View ; Function to update the BITree ; Driver code ; BITree for query of type 2 ; BITree for query of type 1 ; Input the queries in a 2D matrix ; If query is of type 2 then function call to update with BITree ; If query is of type 1 then function call to update with BITree2
import java . io . * ; import java . util . * ; class GFG { static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] = ( val + BITree [ index ] ) ; index = ( index + ( index & ( - index ) ) ) ; } return ; } static int [ ] constructBITree ( int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; return BITree ; } static int getSum ( int BITree [ ] , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum = ( sum + BITree [ index ] ) ; index -= index & ( - index ) ; } return sum ; } static void update ( int BITree [ ] , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; return ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int temp [ ] = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ ] [ ] q = new int [ 6 ] [ 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = temp [ j ++ ] ; q [ i ] [ 1 ] = temp [ j ++ ] ; q [ i ] [ 2 ] = temp [ j ++ ] ; } int [ ] BITree = constructBITree ( m ) ; int [ ] BITree2 = constructBITree ( n ) ; Scanner sc = new Scanner ( System . in ) ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = sc . nextInt ( ) ; q [ i ] [ 1 ] = sc . nextInt ( ) ; q [ i ] [ 2 ] = sc . nextInt ( ) ; } for ( int i = m ; i >= 1 ; i -- ) if ( q [ i ] [ 0 ] == 2 ) update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , 1 ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 2 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , val ) ; } } for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 1 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree2 , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , n , ( val + 1 ) ) ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( getSum ( BITree2 , i - 1 ) + " ▁ " ) ; } }
Array range queries for searching an element | Java program to determine if the element exists for different range queries ; Structure to represent a query range ; Find the root of the group containing the element at index x ; merge the two groups containing elements at indices x and y into one group ; make n subsets with every element as its root ; consecutive elements equal in value are merged into one single group ; Driver code ; check if the current element in consideration is equal to x or not if it is equal , then x exists in the range ; Print if x exists or not
import java . util . * ; class GFG { static class Query { int L , R , X ; public Query ( int L , int R , int X ) { this . L = L ; this . R = R ; this . X = X ; } } ; static int maxn = 100 ; static int [ ] root = new int [ maxn ] ; static int find ( int x ) { if ( x == root [ x ] ) return x ; else return root [ x ] = find ( root [ x ] ) ; } static void uni ( int x , int y ) { int p = find ( x ) , q = find ( y ) ; if ( p != q ) { root [ p ] = root [ q ] ; } } static void initialize ( int a [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < n ; i ++ ) root [ i ] = i ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] == a [ i - 1 ] ) uni ( i , i - 1 ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 1 , 5 , 4 , 5 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 2 , 2 ) , new Query ( 1 , 4 , 1 ) , new Query ( 2 , 4 , 5 ) } ; int m = q . length ; initialize ( a , n , q , m ) ; for ( int i = 0 ; i < m ; i ++ ) { int flag = 0 ; int l = q [ i ] . L , r = q [ i ] . R , x = q [ i ] . X ; int p = r ; while ( p >= l ) { if ( a [ p ] == x ) { flag = 1 ; break ; } p = find ( p ) - 1 ; } if ( flag != 0 ) System . out . println ( x + " ▁ exists ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; else System . out . println ( x + " ▁ does ▁ not ▁ exist ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; } } }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Java program for the above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; Iterate over 1 to a to find distinct pairs ; For each integer from 1 to a b / n integers exists such that pair sum is divisible by n ; If ( i % n + b % n ) >= n one more pair is possible ; Return answer ; Driver code
class GFG { static int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; for ( int i = 1 ; i <= a ; i ++ ) { ans += b / n ; ans += ( i % n + b % n ) >= n ? 1 : 0 ; } return ans ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . println ( findCountOfPairs ( a , b , n ) ) ; } }
Count of pairs from 1 to a and 1 to b whose sum is divisible by N | Java implementation of the above approach ; Function to find the distinct pairs from 1 - a & 1 - b such that their sum is divisible by n . ; if first element is bigger then swap ; count is store the number of pair . ; we use temp for breaking a loop . ; count when a is greater . ; Count when a is smaller but b is greater ; Count when a and b both are smaller ; breaking condition ; For storing The pair in count . ; return the number of pairs . ; Driver code
class GFG { public static int findCountOfPairs ( int a , int b , int n ) { if ( a > b ) { int temp = a ; a = b ; b = temp ; } int temp = 1 , count = 0 ; for ( int i = n ; temp > 0 ; i += n ) { if ( a >= i ) { temp = i - 1 ; } else if ( b >= i ) { temp = a ; } else if ( i > b ) { temp = a - ( i - b ) + 1 ; } if ( temp > 0 ) { count += temp ; } } return count ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . print ( findCountOfPairs ( a , b , n ) ) ; } }
Array range queries for elements with frequency same as value | Java Program to answer Q queries to find number of times an element x appears x times in a Query subarray ; Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries
import java . util . HashMap ; import java . util . Map ; class GFG { static int solveQuery ( int start , int end , int arr [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = start ; i <= end ; i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; int count = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) if ( entry . getKey ( ) == entry . getValue ( ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = A . length ; int [ ] [ ] queries = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i ] [ 0 ] ; int end = queries [ i ] [ 1 ] ; System . out . println ( " Answer ▁ for ▁ Query ▁ " + ( i + 1 ) + " ▁ = ▁ " + solveQuery ( start , end , A ) ) ; } } }
Buy minimum items without change and given coins | Java implementation of above approach ; See if we can buy less than 10 items Using 10 Rs coins and one r Rs coin ; We can always buy 10 items ; Driver Code
import java . util . * ; class GFG { static int minItems ( int k , int r ) { for ( int i = 1 ; i < 10 ; i ++ ) if ( ( i * k - r ) % 10 == 0 || ( i * k ) % 10 == 0 ) return i ; return 10 ; } public static void main ( String args [ ] ) { int k = 15 ; int r = 2 ; System . out . println ( minItems ( k , r ) ) ; } }
Number of indexes with equal elements in given range | Java program to count the number of indexes in range L R such that Ai = Ai + 1 ; function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query
class GFG { static int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = a . length ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( a , n , L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( a , n , L , R ) ) ; } }
Diagonal Sum of a Binary Tree | Java Program to calculate the sum of diagonal nodes . ; Node Structure ; to map the node with level - index ; Function to create new node ; recursvise function to calculate sum of elements where level - index is same . ; if there is no child then return ; add the element in the group of node whose level - index is equal ; left child call ; right child call ; Function call ; for different values of level - index add te sum of those node to answer ; Driver code ; build binary tree ; Function Call ; print the daigonal sums
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static HashMap < Integer , Integer > grid = new HashMap < > ( ) ; static Node newNode ( int data ) { Node Node = new Node ( ) ; Node . data = data ; Node . left = Node . right = null ; return Node ; } static void addConsideringGrid ( Node root , int level , int index ) { if ( root == null ) return ; if ( grid . containsKey ( level - index ) ) grid . put ( level - index , grid . get ( level - index ) + ( root . data ) ) ; else grid . put ( level - index , root . data ) ; addConsideringGrid ( root . left , level + 1 , index - 1 ) ; addConsideringGrid ( root . right , level + 1 , index + 1 ) ; } static Vector < Integer > diagonalSum ( Node root ) { grid . clear ( ) ; addConsideringGrid ( root , 0 , 0 ) ; Vector < Integer > ans = new Vector < > ( ) ; for ( Map . Entry < Integer , Integer > x : grid . entrySet ( ) ) { ans . add ( x . getValue ( ) ) ; } return ans ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 9 ) ; root . left . right = newNode ( 6 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . right = newNode ( 7 ) ; root . right . left . left = newNode ( 12 ) ; root . left . right . left = newNode ( 11 ) ; root . left . left . right = newNode ( 10 ) ; Vector < Integer > v = diagonalSum ( root ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; } }
Number of indexes with equal elements in given range | Java program to count the number of indexes in range L R such that Ai = Ai + 1 ; Array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query
class GFG { public static int N = 1000 ; static int prefixans [ ] = new int [ 1000 ] ; public static void countIndex ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } public static int answer_query ( int l , int r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = 9 ; countIndex ( a , n ) ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( L , R ) ) ; } }
Total numbers with no repeated digits in a range | Java implementation of brute force solution . ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to find total number in the given range which has no repeated digit ; Traversing through the range ; Add 1 to the answer if i has no repeated digit else 0 ; Driver Code ; Calling the calculate
import java . util . LinkedHashSet ; class GFG { static int repeated_digit ( int n ) { LinkedHashSet < Integer > s = new LinkedHashSet < > ( ) ; while ( n != 0 ) { int d = n % 10 ; if ( s . contains ( d ) ) { return 0 ; } s . add ( d ) ; n = n / 10 ; } return 1 ; } static int calculate ( int L , int R ) { int answer = 0 ; for ( int i = L ; i < R + 1 ; ++ i ) { answer = answer + repeated_digit ( i ) ; } return answer ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; System . out . println ( calculate ( L , R ) ) ; } }
Minimum swaps required to make a binary string alternating | Java implementation of the approach ; returns the minimum number of swaps of a binary string passed as the argument to make it alternating ; counts number of zeroes at odd and even positions ; counts number of ones at odd and even positions ; alternating string starts with 0 ; alternating string starts with 1 ; calculates the minimum number of swaps ; Driver code
class GFG { static int countMinSwaps ( String st ) { int min_swaps = 0 ; int odd_0 = 0 , even_0 = 0 ; int odd_1 = 0 , even_1 = 0 ; int n = st . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st . charAt ( i ) == '1' ) even_1 ++ ; else even_0 ++ ; } else { if ( st . charAt ( i ) == '1' ) odd_1 ++ ; else odd_0 ++ ; } } int cnt_swaps_1 = Math . min ( even_0 , odd_1 ) ; int cnt_swaps_2 = Math . min ( even_1 , odd_0 ) ; return Math . min ( cnt_swaps_1 , cnt_swaps_2 ) ; } public static void main ( String [ ] args ) { String st = "000111" ; System . out . println ( countMinSwaps ( st ) ) ; } }
Total numbers with no repeated digits in a range | Java implementation of above idea ; Maximum ; Prefix Array ; Function to check if the given number has repeated digit or not ; Traversing through each digit ; if the digit is present more than once in the number ; return 0 if the number has repeated digit ; return 1 if the number has no repeated digit ; Function to pre calculate the Prefix array ; Traversing through the numbers from 2 to MAX ; Generating the Prefix array ; Calclute Function ; Answer ; Driver Code ; Pre - calculating the Prefix array . ; Calling the calculate function to find the total number of number which has no repeated digit
import java . util . * ; class GFG { static int MAX = 100 ; static Vector < Integer > Prefix = new Vector < > ( ) ; static int repeated_digit ( int n ) { HashSet < Integer > a = new HashSet < > ( ) ; int d ; while ( n != 0 ) { d = n % 10 ; if ( a . contains ( d ) ) return 0 ; a . add ( d ) ; n /= 10 ; } return 1 ; } static void pre_calculations ( ) { Prefix . add ( 0 ) ; Prefix . add ( repeated_digit ( 1 ) ) ; for ( int i = 2 ; i < MAX + 1 ; i ++ ) Prefix . add ( repeated_digit ( i ) + Prefix . elementAt ( i - 1 ) ) ; } static int calculate ( int L , int R ) { return Prefix . elementAt ( R ) - Prefix . elementAt ( L - 1 ) ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; pre_calculations ( ) ; System . out . println ( calculate ( L , R ) ) ; } }
Difference Array | Range update query in O ( 1 ) | Java code to demonstrate Difference Array ; Creates a diff array D [ ] for A [ ] and returns it after filling initial values . ; We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; Does range update ; Prints updated Array ; Note that A [ 0 ] or D [ 0 ] decides values of rest of the elements . ; Driver Code ; Array to be updated ; Create and fill difference Array We use one extra space because update ( l , r , x ) updates D [ r + 1 ] ; After below update ( l , r , x ) , the elements should become 20 , 15 , 20 , 40 ; After below updates , the array should become 30 , 35 , 70 , 60
class GFG { static void initializeDiffArray ( int A [ ] , int D [ ] ) { int n = A . length ; D [ 0 ] = A [ 0 ] ; D [ n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) D [ i ] = A [ i ] - A [ i - 1 ] ; } static void update ( int D [ ] , int l , int r , int x ) { D [ l ] += x ; D [ r + 1 ] -= x ; } static int printArray ( int A [ ] , int D [ ] ) { for ( int i = 0 ; i < A . length ; i ++ ) { if ( i == 0 ) A [ i ] = D [ i ] ; else A [ i ] = D [ i ] + A [ i - 1 ] ; System . out . print ( A [ i ] + " ▁ " ) ; } System . out . println ( ) ; return 0 ; } public static void main ( String [ ] args ) { int A [ ] = { 10 , 5 , 20 , 40 } ; int n = A . length ; int D [ ] = new int [ n + 1 ] ; initializeDiffArray ( A , D ) ; update ( D , 0 , 1 , 10 ) ; printArray ( A , D ) ; update ( D , 1 , 3 , 20 ) ; update ( D , 2 , 2 , 30 ) ; printArray ( A , D ) ; } }
Find the value of the function Y = ( X ^ 6 + X ^ 2 + 9894845 ) % 971 | Java implementation of above approach ; computing ( a ^ b ) % c ; Driver code
class GFG { static long modpow ( long base , long exp , long modulus ) { base %= modulus ; long result = 1 ; while ( exp > 0 ) { if ( ( exp & 1 ) > 0 ) result = ( result * base ) % modulus ; base = ( base * base ) % modulus ; exp >>= 1 ; } return result ; } public static void main ( String [ ] args ) { long n = 654654 ; long mod = 971 ; System . out . println ( ( ( modpow ( n , 6 , mod ) + modpow ( n , 2 , mod ) ) % mod + 355 ) % mod ) ; } }
Largest Sum Contiguous Subarray | Java program to print largest contiguous array sum ; Driver program to test maxSubArraySum
import java . io . * ; import java . util . * ; class Kadane { static int maxSubArraySum ( int a [ ] ) { int size = a . length ; int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void main ( String [ ] args ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySum ( a ) ) ; } }
Place N ^ 2 numbers in matrix such that every row has an equal sum | Java program to distribute n ^ 2 numbers to n people ; 2D array for storing the final result ; using modulo to go to the firs column after the last column ; making a 2D array containing numbers ; Driver Code
public class Numbers { public static int [ ] [ ] solve ( int [ ] [ ] arr , int n ) { int [ ] [ ] ans = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i ] [ j ] = arr [ j ] [ ( i + j ) % n ] ; } } return ans ; } public static void show ( int [ ] [ ] arr , int n ) { int [ ] [ ] res = solve ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( res [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static int [ ] [ ] makeArray ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; int c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = c ++ ; } return arr ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] [ ] arr = makeArray ( n ) ; show ( arr , n ) ; } }
Largest Sum Contiguous Subarray |  ; Do not compare for all elements . Compare only when max_ending_here > 0
static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = a [ 0 ] , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; }
Minimum rooms for m events of n batches with given schedule | java program to find the minimum number of rooms required ; Returns minimum number of rooms required to perform classes of n groups in m slots with given schedule . ; Store number of class happening in empty slot ; initialize all values to zero ; Number of rooms required is equal to maximum classes happening in a particular slot . ; Driver Code
class GFG { static int findMinRooms ( String slots [ ] , int n , int m ) { int counts [ ] = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) counts [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( slots [ i ] . charAt ( j ) == '1' ) counts [ j ] ++ ; int max = - 1 ; for ( int i = 0 ; i < m ; i ++ ) if ( max < counts [ i ] ) max = counts [ i ] ; return max ; } public static void main ( String args [ ] ) { int n = 3 , m = 7 ; String slots [ ] = { "0101011" , "0011001" , "0110111" } ; System . out . println ( findMinRooms ( slots , n , m ) ) ; } }