codeparrot/xlcost-text-to-code huggingface dataset:
Maximize the number N by inserting given digit at any position | Java program for the above approach ; Function to find the maximum value of N after inserting the digit K ; Convert it into N to string ; Stores the maximum value of N after inserting K ; Iterate till all digits that are not less than K ; Add the current digit to the string result ; Add digit ' K ' to result ; Iterate through all remaining characters ; Add current digit to result ; Print the maximum number formed ; Driver Code
class GFG { public static void maximizeNumber ( int N , int K ) { String s = Integer . toString ( N ) ; int L = s . length ( ) ; String result = " " ; int i = 0 ; while ( ( i < L ) && ( K <= ( ( int ) s . charAt ( i ) - ( int ) '0' ) ) ) { result += ( s . charAt ( i ) ) ; ++ i ; } result += ( ( char ) ( K + ( int ) '0' ) ) ; while ( i < L ) { result += ( s . charAt ( i ) ) ; ++ i ; } System . out . println ( result ) ; } public static void main ( String args [ ] ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; } }
Count of N | Java program for the above approach ; Function to find the value of X to the power Y ; Stores the value of X ^ Y ; If y is odd , multiply x with result ; Update the value of y and x ; Return the result ; Function to count number of arrays having element over the range [ 0 , 2 ^ K - 1 ] with Bitwise AND value 0 having maximum possible sum ; Print the value of N ^ K ; Driver Code
public class GFG { static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( y % 2 != 0 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } static void countArrays ( int N , int K ) { System . out . println ( ( int ) ( power ( N , K ) ) ) ; } public static void main ( String args [ ] ) { int N = 5 , K = 6 ; countArrays ( N , K ) ; } }
Minimum absolute value of ( K – arr [ i ] ) for all possible values of K over the range [ 0 , N – 1 ] | Java program for the above approach ; Functinion to find the minimum absolute value of ( K - arr [ i ] ) for each possible value of K over the range [ 0 , N - 1 ] | ; Traverse the given array arr [ ] ; Stores the mininimum distance to any array element arr [ i ] ; Check if there is no safe position smaller than i ; Check if the current position is between two safe positions ; Take the minimum of two distances ; Check if the current index is a safe position ; Check if there is no safe position greater than i ; Print the minimum distance ; driver code
import java . io . * ; class GFG { public static void minimumDistance ( int arr [ ] , int N ) { int ind = 0 ; int prev = arr [ ind ] ; int s = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { int distance = Integer . MAX_VALUE ; if ( i < arr [ 0 ] ) { distance = arr [ 0 ] - i ; } else if ( i >= prev && ind + 1 < s && i <= arr [ ind + 1 ] ) { distance = Math . min ( i - prev , arr [ ind + 1 ] - i ) ; if ( i == arr [ ind + 1 ] ) { distance = 0 ; prev = arr [ ind + 1 ] ; ind ++ ; } } else { distance = i - prev ; } System . out . print ( distance + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 0 , 4 } ; minimumDistance ( arr , N ) ; } }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Java program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Iterate over the range [ 0 , N ) ; Iterate over the range ; Check for the given condition ; Return the resultant count ; Driver Code
class GFG { public static int countOfPairs ( int arr [ ] , int N , int X ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . length ; System . out . println ( countOfPairs ( arr , N , X ) ) ; } }
Count of pairs in Array such that bitwise AND of XOR of pair and X is 0 | Java program for the above approach ; Function to find the number of pairs that satisfy the given criteria i . e . , i < j and ( arr [ i ] ^ arr [ j ] ) & X is 0 ; Stores the resultant count of pairs ; Initializing the map M ; Populating the map ; Count number of pairs for every element in map using mathematical concept of combination ; As nC2 = n * ( n - 1 ) / 2 ; Return the resultant count ; Driver Code
import java . util . * ; class GFG { static int countOfPairs ( int [ ] arr , int N , int X ) { int count = 0 ; HashMap < Integer , Integer > M = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( M . containsKey ( arr [ i ] & X ) ) M . put ( ( arr [ i ] & X ) , M . get ( arr [ i ] & X ) + 1 ) ; else M . put ( arr [ i ] & X , 1 ) ; } for ( Integer entry : M . keySet ( ) ) { int p = M . get ( entry ) ; count += p * ( p - 1 ) / 2 ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . length ; System . out . print ( countOfPairs ( arr , N , X ) ) ; } }
Count of square submatrices with average at least K | Java program for the above approach ; Function to count submatrixes with average greater than or equals to K ; Stores count of submatrices ; Stores the prefix sum of matrix ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Update the prefix sum ; Iterate over the range [ 1 , N ] ; Iterate over the range [ 1 , M ] ; Iterate until l and r are greater than 0 ; Update count ; Stores sum of submatrix with bottom right corner as ( i , j ) and top left corner as ( l , r ) ; If sum1 is less than or equal to sum2 ; Increment cnt by 1 ; Return cnt as the answer ; Driver Code ; Given Input ; Function Call
import java . util . * ; class GFG { static int MAX = 1000 ; static int cntMatrices ( int [ ] [ ] arr , int N , int M , int K ) { int cnt = 0 ; int [ ] [ ] pre = new int [ N + 1 ] [ M + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { pre [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + pre [ i - 1 ] [ j ] + pre [ i ] [ j - 1 ] - pre [ i - 1 ] [ j - 1 ] ; } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { for ( int l = i , r = j ; l > 0 && r > 0 ; l -- , r -- ) { int sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) ; int sum2 = pre [ i ] [ j ] - pre [ l - 1 ] [ r ] - pre [ l ] [ r - 1 ] + pre [ l - 1 ] [ r - 1 ] ; if ( sum1 <= sum2 ) cnt ++ ; } } } return cnt ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 2 , 2 , 3 } , { 3 , 4 , 5 } , { 4 , 5 , 5 } } ; int K = 4 ; int N = arr . length ; int M = arr [ 0 ] . length ; System . out . println ( cntMatrices ( arr , N , M , K ) ) ; } }
Concatenate the Array of elements into a single element | Java program for the above approach ; Function to find the integer value obtained by joining array elements together ; Stores the resulting integer value ; Traverse the array arr [ ] ; Stores the count of digits of arr [ i ] ; Update ans ; Increment ans by arr [ i ] ; Return the ans ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static int ConcatenateArr ( int [ ] arr , int N ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { int l = ( int ) Math . floor ( Math . log10 ( arr [ i ] ) + 1 ) ; ans = ans * ( int ) Math . pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 23 , 456 } ; int N = arr . length ; System . out . println ( ConcatenateArr ( arr , N ) ) ; } }
Count of integers K in range [ 0 , N ] such that ( K XOR K + 1 ) equals ( K + 2 XOR K + 3 ) | Java Program for the above approach ; Function to count all the integers less than N satisfying the given condition ; Store the count of even numbers less than N + 1 ; Return the count ; Driver Code ; Given Input ; Function Call
import java . io . * ; class GFG { static int countXor ( int N ) { int cnt = ( int ) N / 2 + 1 ; return cnt ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( countXor ( N ) ) ; } }
Maximize absolute displacement from origin by moving on X | Java program for the above approach ; Recursive function to find the maximum absolute displacement from origin by performing the given set of moves ; If i is equal to N ; If S [ i ] is equal to ' L ' ; If S [ i ] is equal to ' R ' ; If S [ i ] is equal to ' ? ' ; Function to find the maximum absolute displacement from the origin ; Return the maximum absolute displacement ; Driver Code ; Input ; Function call
import java . util . * ; class GFG { static int DistRecursion ( String S , int i , int dist ) { char [ ] ch = S . toCharArray ( ) ; if ( i == ch . length ) return Math . abs ( dist ) ; if ( ch [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( ch [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } static int maxDistance ( String S ) { return DistRecursion ( S , 0 , 0 ) ; } public static void main ( String [ ] args ) { String S = " ? RRR ? " ; System . out . print ( maxDistance ( S ) ) ; } }
Count of pairs in range [ P , Q ] with numbers as multiple of R and their product lie in range [ P * Q / 4 , P * Q ] | Java program for above approach ; Function to find the number of pairs such that both the elements are in the range [ P , Q ] and the numbers should be multiple of R , and the product of numbers should lie in the range [ P * Q / 4 , P * Q ] ; Store multiple of r in range of [ P , Q ] ; Itearte in the range [ p , q ] ; Vector to store pair of answer ; Iterate through the vector v ; Iterate in the range [ i + 1 , v . size ( ) - 1 ] ; If pair follow this condition insert the pair in vector ans ; If no pair satisfy the conditions , print - 1 ; Print the pairs which satisfy the given condition ; Driver Code ; Given Input ; Function Call
import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void findPairs ( int p , int q , int r ) { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = p ; i <= q ; i ++ ) { if ( i % r == 0 ) { v . add ( i ) ; } } ArrayList < pair < Integer , Integer > > ans = new ArrayList < > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { if ( v . get ( i ) * v . get ( j ) >= p * q / 4 && v . get ( i ) * v . get ( j ) <= p * q ) { pair < Integer , Integer > x = new pair < > ( ) ; x . first = v . get ( i ) ; x . second = v . get ( j ) ; ans . add ( x ) ; } } } if ( ans . size ( ) == 0 ) { System . out . println ( - 1 ) ; } else { for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . println ( ans . get ( i ) . first + " ▁ " + ans . get ( i ) . second ) ; } } } public static void main ( String [ ] args ) { int p = 14 , q = 30 , r = 5 ; findPairs ( p , q , r ) ; } }
Print rectangular pattern with given center | Java program for the above approach ; Function to print the matrix filled with rectangle pattern having center coordinates are c1 , c2 ; Iterate in the range [ 0 , n - 1 ] ; Iterate in the range [ 0 , n - 1 ] ; Driver code ; Given Input ; Function Call
import java . io . * ; class GFG { static void printRectPattern ( int c1 , int c2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( ( Math . max ( Math . abs ( c1 - i ) , Math . abs ( c2 - j ) ) ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int c1 = 2 ; int c2 = 2 ; int n = 5 ; printRectPattern ( c1 , c2 , n ) ; } }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | Java implementation of the approach ; Function to return the count of required numbers ; If rightmost bit is 0 ; Return the required count ; Driver code ; Call function countNumbers
import java . io . * ; class GFG { static int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( ( i & 1 ) == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int l = 10 , r = 20 ; System . out . println ( countNumbers ( l , r ) ) ; } }
Count of numbers in range [ L , R ] with LSB as 0 in their Binary representation | Java implementation of the approach ; Function to return the count of required numbers ; Count of numbers in range which are divisible by 2 ; Driver Code
import java . io . * ; class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 2 ) - ( l - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int l = 10 ; int r = 20 ; System . out . println ( countNumbers ( l , r ) ) ; } }
Length of longest Fibonacci subarray | package whatever do not write package name here ; A utility function that returns true if x is perfect square ; Returns true if n is a Fibinacci Number , else false ; Here n is Fibinacci if one of 5 * n * n + 4 or 5 * n * n - 4 or both is a perferct square ; Function to find the length of the largest sub - array of an array every element of whose is a Fibonacci number ; Traverse the array arr [ ] ; Check if arr [ i ] is a Fibonacci number ; Stores the maximum length of the Fibonacci number subarray ; Finally , return the maximum length ; Driver code ; Given Input ; Function Cal
import java . io . * ; class GFG { public static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } public static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } public static int contiguousFibonacciNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 8 , 21 , 5 , 3 , 28 , 4 } ; int n = arr . length ; System . out . println ( contiguousFibonacciNumber ( arr , n ) ) ; } }
Minimum number of flips required such that a Binary Matrix doesn 't contain any path from the top left to the bottom right consisting only of 0s | Java program for the above approach ; The four direction coordinates changes from the current cell ; Function that returns true if there exists any path from the top - left to the bottom - right cell of 0 s ; If the bottom - right cell is reached ; Update the cell to 1 ; Traverse in all four directions ; Find the new coordinates ; If the new cell is valid ; Recursively call DFS ; If path exists , then return true ; Return false , if there doesn 't  exists any such path ; Function to flip the minimum number of cells such that there doesn 't exists any such path from (0, 0) to (N - 1, M - 1) cell consisting of 0s ; Case 1 : If no such path exists already ; Case 2 : If there exists only one path ; Case 3 : If there exists two - path ; Driver code
import java . io . * ; class GFG { static int [ ] [ ] direction = { { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } } ; static boolean dfs ( int matrix [ ] [ ] , int i , int j , int N , int M ) { if ( i == N - 1 && j == M - 1 ) { return true ; } matrix [ i ] [ j ] = 1 ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = i + direction [ k ] [ 0 ] ; int newY = j + direction [ k ] [ 1 ] ; if ( newX >= 0 && newX < N && newY >= 0 && newY < M && matrix [ newX ] [ newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } static int solve ( int [ ] [ ] matrix ) { int N = matrix . length ; int M = matrix [ 0 ] . length ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( solve ( mat ) ) ; } }
Maximize product of subarray sum with its maximum element | Java program for the above approach ; Function to find the maximum product of the sum of the subarray with its maximum element ; Traverse the array arr [ ] ; Increment currSum by a [ i ] ; Maximize the value of currMax ; Maximize the value of largestSum ; If currSum goes less than 0 then update currSum = 0 ; Return the resultant value ; Function to maximize the product of the sum of the subarray with its maximum element ; Find the largest sum of the subarray ; Multiply each array element with - 1 ; Find the largest sum of the subarray with negation of all array element ; Return the resultant maximum value ; Driver Code
import java . io . * ; class GFG { static int Kadane ( int arr [ ] , int n ) { int largestSum = 0 , currMax = 0 ; int currSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currSum += arr [ i ] ; currMax = Math . max ( currMax , arr [ i ] ) ; largestSum = Math . max ( largestSum , currMax * currSum ) ; if ( currSum < 0 ) { currMax = 0 ; currSum = 0 ; } } return largestSum ; } static int maximumWeight ( int arr [ ] , int n ) { int largestSum = Kadane ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = - arr [ i ] ; } largestSum = Math . max ( largestSum , Kadane ( arr , n ) ) ; return largestSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 3 , 8 , - 2 , 5 } ; int N = arr . length ; System . out . println ( maximumWeight ( arr , N ) ) ; } }
Partition an array into two subsets with equal count of unique elements | Java program for the above approach ; Function to partition the array into two subsets such that count of unique elements in both subsets is the same ; Stores the subset number for each array elements ; Stores the count of unique array elements ; Stores the frequency of elements ; Traverse the array ; Count of elements having a frequency of 1 ; Check if there exists any element with frequency > 2 ; Count of elements needed to have frequency exactly 1 in each subset ; Initialize all values in the / array ans [ ] as 1 ; Traverse the array ans [ ] ; This array element is a part of first subset ; Half array elements with frequency 1 are part of the second subset ; If count of elements is exactly 1 are odd and has no element with frequency > 2 ; If count of elements that occurs exactly once are even ; Print the result ; If the count of elements has exactly 1 frequency are odd and there is an element with frequency greater than 2 ; Print the result ; Driver Codea
import java . util . HashMap ; class GFG { public static void arrayPartition ( int a [ ] , int n ) { int [ ] ans = new int [ n ] ; int cnt = 0 ; int ind = 0 , flag = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( a [ i ] ) ) { mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . put ( a [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 ) cnt ++ ; if ( mp . get ( a [ i ] ) > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } int p = ( cnt + 1 ) / 2 ; int ans1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp . get ( a [ i ] ) == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { System . out . println ( - 1 + "NEW_LINE"); return ; } if ( cnt % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { if ( ind == i ) System . out . print ( 2 + " ▁ " ) ; else System . out . print ( ans [ i ] + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 4 } ; int N = arr . length ; arrayPartition ( arr , N ) ; } }
Modify a given array by replacing each element with the sum or product of their digits based on a given condition | Java program for the above approach ; Function to modify the given array as per the given conditions ; Traverse the given array arr [ ] ; Initialize the count of even and odd digits ; Initialize temp with the current array element ; For count the number of even digits ; Increment the odd count ; Otherwise ; Divide temp by 10 ; Performe addition ; Performe multiplication ; Otherwise ; Driver Code
import java . io . * ; class GFG { static void evenOdd ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp > 0 ) { if ( ( temp % 10 ) % 2 != 0 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] > 0 ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } System . out . print ( res + " ▁ " ) ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] > 0 ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } System . out . print ( res + " ▁ " ) ; } else System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 113 , 141 , 214 , 3186 } ; int N = arr . length ; evenOdd ( arr , N ) ; } }
Find prime factors of Z such that Z is product of all even numbers till N that are product of two distinct prime numbers | Java implementation of the above approach ; Function to print the prime factorization of the product of all numbers <= N that are even and can be expressed as a product of two distinct prime numbers ; Sieve of Eratosthenese ; Store prime numbers in the range [ 3 , N / 2 ] ; Print the coefficient of 2 in the prime factorization ; Print the coefficients of other primes ; Driver Code ; Input ; Function calling
import java . util . * ; import java . util . HashMap ; class GFG { static void primeFactorization ( int N ) { int [ ] sieve = new int [ N / 2 + 1 ] ; for ( int i = 0 ; i <= N / 2 ; i ++ ) { sieve [ i ] = 0 ; } for ( int i = 2 ; i <= N / 2 ; i ++ ) { if ( sieve [ i ] == 0 ) { for ( int j = i * i ; j <= N / 2 ; j += i ) { sieve [ j ] = 1 ; } } } ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; for ( int i = 3 ; i <= N / 2 ; i ++ ) if ( sieve [ i ] == 0 ) prime . add ( i ) ; int x = prime . size ( ) ; System . out . println ( "2 - > " + x ) ; for ( int i : prime ) System . out . println ( i + " - > 1" ) ; } public static void main ( String args [ ] ) { int N = 18 ; primeFactorization ( N ) ; } }
Sum of the first M elements of Array formed by infinitely concatenating given array | Java program for the above approach ; Function to find the sum of first M numbers formed by the infinite concatenation of the array A [ ] ; Stores the resultant sum ; Iterate over the range [ 0 , M - 1 ] ; Add the value A [ i % N ] to sum ; Return the resultant sum ; Driver Code
import java . io . * ; import java . lang . * ; class GFG { public static int sumOfFirstM ( int A [ ] , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int M = 5 ; int N = arr . length ; System . out . println ( sumOfFirstM ( arr , N , M ) ) ; } }
Check if it is possible to reach M from 0 by given paths | Java program for the above approach ; Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code
import java . io . * ; class GFG { static void canReach0toM ( int [ ] [ ] a , int n , int m ) { int [ ] rightMost = new int [ m + 1 ] ; int [ ] dp = new int [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i ] [ 0 ] ; int b1 = a [ i ] [ 1 ] ; rightMost [ a1 ] = Math . max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = Math . min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = arr . length ; canReach0toM ( arr , N , M ) ; } }