codeparrot/xlcost-text-to-code huggingface dataset:
Print all numbers whose set of prime factors is a subset of the set of the prime factors of X | Java program to implement the above approach ; Function to print all the numbers ; Iterate for every element in the array ; Find the gcd ; Iterate till gcd is 1 of number and x ; Divide the number by gcd ; Find the new gcdg ; If the number is 1 at the end then print the number ; If no numbers have been there ; Drivers code
class GFG { static void printNumbers ( int a [ ] , int n , int x ) { boolean flag = false ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; int g = __gcd ( num , x ) ; while ( g != 1 ) { num /= g ; g = __gcd ( num , x ) ; } if ( num == 1 ) { flag = true ; System . out . print ( a [ i ] + " ▁ " ) ; } } if ( ! flag ) System . out . println ( " There ▁ are ▁ no ▁ such ▁ numbers " ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int x = 60 ; int a [ ] = { 2 , 5 , 10 , 7 , 17 } ; int n = a . length ; printNumbers ( a , n , x ) ; } }
Find the final radiations of each Radiated Stations | Java implementation of the approach ; Function to print the final radiations ; Function to create the array of the resultant radiations ; Resultant radiations ; Declaring index counter for left and right radiation ; Effective radiation for left and right case ; Radiation for i - th station ; Radiation increment for left stations ; Radiation increment for right stations ; Print the resultant radiation for each of the stations ; Driver code ; 1 - based indexing
class GFG { static void print ( int rStation [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( rStation [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } static void radiated_Station ( int station [ ] , int n ) { int rStation [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { int li = i - 1 , ri = i + 1 ; int lRad = station [ i ] - 1 , rRad = station [ i ] - 1 ; rStation [ i ] += station [ i ] ; while ( li >= 1 && lRad >= 1 ) { rStation [ li -- ] += lRad -- ; } while ( ri <= n && rRad >= 1 ) { rStation [ ri ++ ] += rRad -- ; } } print ( rStation , n ) ; } public static void main ( String [ ] args ) { int station [ ] = { 0 , 7 , 9 , 12 , 2 , 5 } ; int n = station . length - 1 ; radiated_Station ( station , n ) ; } }
Program to find the maximum difference between the index of any two different numbers | Java implementation of the approach ; Function to return the maximum difference ; Iteratively check from back ; Different numbers ; Iteratively check from the beginning ; Different numbers ; Driver code
class GFG { static int findMaximumDiff ( int [ ] a , int n ) { int ind1 = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( a [ 0 ] != a [ i ] ) { ind1 = i ; break ; } } int ind2 = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ n - 1 ] != a [ i ] ) { ind2 = ( n - 1 - i ) ; break ; } } return Math . max ( ind1 , ind2 ) ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 2 , 3 , 2 , 3 } ; int n = a . length ; System . out . println ( findMaximumDiff ( a , n ) ) ; } }
Find the Kth node in the DFS traversal of a given subtree in a Tree | Java program to find the Kth node in the DFS traversal of the subtree of given vertex V in a Tree ; To store nodes ; To store the current index of vertex in DFS ; To store the starting index and ending index of vertex in the DFS traversal array ; To store the DFS of vertex 1 ; Function to add edge between two nodes ; Initialize the vectors ; Function to perform DFS of a vertex 1. stores the DFS of the vertex 1 in vector p , 2. store the start index of DFS of every vertex 3. store the end index of DFS of every vertex ; store staring index of node ch ; store ending index ; Function to find the Kth node in DFS of vertex V ; Check if kth number exits or not ; Driver code ; Number of nodes ; Add edges ; Store DFS of 1 st node
import java . util . * ; class GFG { static int N = 100005 ; static int n ; static ArrayList < ArrayList < Integer > > tree = new ArrayList < > ( ) ; static int currentIdx ; static int [ ] startIdx ; static int [ ] endIdx ; static int [ ] p ; static void Add_edge ( int u , int v ) { tree . get ( u ) . add ( v ) ; tree . get ( v ) . add ( u ) ; } static void intisalise ( ) { startIdx = new int [ n + 1 ] ; endIdx = new int [ n + 1 ] ; p = new int [ n + 1 ] ; } static void Dfs ( int ch , int par ) { p [ currentIdx ] = ch ; startIdx [ ch ] = currentIdx ++ ; for ( int c : tree . get ( ch ) ) { if ( c != par ) Dfs ( c , ch ) ; } endIdx [ ch ] = currentIdx - 1 ; } static int findNode ( int v , int k ) { k += startIdx [ v ] - 1 ; if ( k <= endIdx [ v ] ) return p [ k ] ; return - 1 ; } public static void main ( String [ ] args ) { n = 9 ; for ( int i = 0 ; i <= n ; i ++ ) tree . add ( new ArrayList < Integer > ( ) ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 1 , 3 ) ; Add_edge ( 1 , 4 ) ; Add_edge ( 3 , 5 ) ; Add_edge ( 3 , 7 ) ; Add_edge ( 5 , 6 ) ; Add_edge ( 5 , 8 ) ; Add_edge ( 7 , 9 ) ; intisalise ( ) ; Dfs ( 1 , 0 ) ; int v = 3 , k = 4 ; System . out . println ( findNode ( v , k ) ) ; } }
Sum of the series Kn + ( K ( n | Java implementation of above approach ; Function to return sum ; Driver code
class GFG { static int sum ( int k , int n ) { int sum = ( int ) ( Math . pow ( k , n + 1 ) - Math . pow ( k - 1 , n + 1 ) ) ; return sum ; } public static void main ( String args [ ] ) { int n = 3 ; int K = 3 ; System . out . print ( sum ( K , n ) ) ; } }
Check whether factorial of N is divisible by sum of first N natural numbers | Function to check whether a number is prime or not . ; Count variable to store the number of factors of ' num ' ; Counting the number of factors ; If number is prime return true ; Function to check for divisibility ; if ' n ' equals 1 then divisibility is possible ; Else check whether ' n + 1' is prime or not ; If ' n + 1' is prime then ' n ! ' is not divisible by ' n * ( n + 1 ) / 2' ; else divisibility occurs ; Driver Code ; Test for n = 3 ; Test for n = 4
class GfG { static boolean is_prime ( int num ) { int count = 0 ; for ( int i = 1 ; i * i <= ( num ) ; i ++ ) { if ( ( num ) % i == 0 ) { if ( i * i != ( num ) ) count += 2 ; else count ++ ; } } if ( count == 2 ) return true ; else return false ; } static String is_divisible ( int n ) { if ( n == 1 ) { return " YES " ; } else { if ( is_prime ( n + 1 ) ) return " NO " ; else return " YES " ; } } public static void main ( String [ ] args ) { int n ; n = 3 ; System . out . println ( is_divisible ( n ) ) ; n = 4 ; System . out . println ( is_divisible ( n ) ) ; } }
Number of subarrays having sum of the form k ^ m , m >= 0 | Java implementation of the above approach ; partial_sum ; Function to count number of sub - arrays whose sum is k ^ p where p >= 0 ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; b = k ^ p , p >= 0 ; k ^ m can be maximum equal to 10 ^ 14. ; If m [ a + b ] = c , then add c to the current sum . ; Increase count of prefix sum . ; Driver code
import java . util . * ; class GFG { static final int MAX = 100005 ; static long [ ] partial_sum ( long [ ] prefix_sum , int [ ] arr , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { prefix_sum [ i ] = ( prefix_sum [ i - 1 ] + arr [ i - 1 ] ) ; } return prefix_sum ; } static int countSubarrays ( int [ ] arr , int n , int k ) { long [ ] prefix_sum = new long [ MAX ] ; prefix_sum [ 0 ] = 0 ; prefix_sum = partial_sum ( prefix_sum , arr , n ) ; int sum ; if ( k == 1 ) { sum = 0 ; HashMap < Long , Integer > m = new HashMap < > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( m . containsKey ( prefix_sum [ i ] + 1 ) ) sum += m . get ( prefix_sum [ i ] + 1 ) ; if ( m . containsKey ( prefix_sum [ i ] ) ) m . put ( prefix_sum [ i ] , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . put ( prefix_sum [ i ] , 1 ) ; } return sum ; } if ( k == - 1 ) { sum = 0 ; HashMap < Long , Integer > m = new HashMap < > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { if ( m . containsKey ( prefix_sum [ i ] + 1 ) ) sum += m . get ( prefix_sum [ i ] + 1 ) ; if ( m . containsKey ( prefix_sum [ i ] - 1 ) ) sum += m . get ( prefix_sum [ i ] - 1 ) ; if ( m . containsKey ( prefix_sum [ i ] ) ) m . put ( prefix_sum [ i ] , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . put ( prefix_sum [ i ] , 1 ) ; } return sum ; } sum = 0 ; long b , l = 100000000000000L ; HashMap < Long , Integer > m = new HashMap < > ( ) ; for ( int i = n ; i >= 0 ; i -- ) { b = 1 ; while ( true ) { if ( b > l ) break ; if ( m . containsKey ( prefix_sum [ i ] + b ) ) sum += m . get ( prefix_sum [ i ] + b ) ; b *= k ; } if ( m . containsKey ( prefix_sum [ i ] ) ) m . put ( ( prefix_sum [ i ] ) , m . get ( prefix_sum [ i ] ) + 1 ) ; else m . put ( ( prefix_sum [ i ] ) , 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 2 } ; int n = arr . length ; int k = 2 ; System . out . print ( countSubarrays ( arr , n , k ) ) ; } }
Given two binary strings perform operation until B > 0 and print the result | Java implementation of the approach ; Function to return the required result ; Reverse the strings ; Count the number of set bits in b ; To store the powers of 2 ; power [ i ] = pow ( 2 , i ) % mod ; To store the final answer ; Add power [ i ] to the ans after multiplying it with the number of set bits in b ; Divide by 2 means right shift b >> 1 if b has 1 at right most side than number of set bits will get decreased ; If no more set bits in b i . e . b = 0 ; Return the required answer ;  ; Driver code
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int BitOperations ( String a , int n , String b , int m ) { char [ ] ch1 = a . toCharArray ( ) ; reverse ( ch1 ) ; a = new String ( ch1 ) ; char [ ] ch2 = b . toCharArray ( ) ; reverse ( ch2 ) ; b = new String ( ch2 ) ; int c = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b . charAt ( i ) == '1' ) c ++ ; int [ ] power = new int [ n ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) power [ i ] = ( power [ i - 1 ] * 2 ) % mod ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) == '1' ) { ans += c * power [ i ] ; if ( ans >= mod ) ans %= mod ; } if ( b . charAt ( i ) == '1' ) c -- ; if ( c == 0 ) break ; } return ans ; } static void reverse ( char a [ ] ) { int i , k , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { String a = "1001" , b = "10101" ; int n = a . length ( ) , m = b . length ( ) ; System . out . println ( BitOperations ( a , n , b , m ) ) ; } }
Print prime numbers with prime sum of digits in an array | Java implementation of the above approach ; Function to store the primes ; Function to return the sum of digits ; Function to print additive primes ; If the number is prime ; Check if it 's digit sum is prime ; Driver code
import java . util . Arrays ; class GFG { static void sieve ( int maxEle , int prime [ ] ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( int i = 2 ; i * i <= maxEle ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j <= maxEle ; j += i ) prime [ j ] = 1 ; } } } static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n = n / 10 ; } return sum ; } static void printAdditivePrime ( int arr [ ] , int n ) { int maxEle = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int prime [ ] = new int [ maxEle + 1 ] ; sieve ( maxEle , prime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == 0 ) { int sum = digitSum ( arr [ i ] ) ; if ( prime [ sum ] == 0 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 6 , 11 , 12 , 18 , 7 } ; int n = a . length ; printAdditivePrime ( a , n ) ; } }
Program to find nth term of the series 1 4 15 24 45 60 92 | Java implementation of the above approach ; function to calculate nth term of the series ; variable nth will store the nth term of series ; if n is even ; if n is odd ; return nth term ; Driver code
class GFG { static long nthTerm ( long n ) { long nth ; if ( n % 2 == 0 ) nth = 2 * ( ( n * n ) - n ) ; else nth = ( 2 * n * n ) - n ; return nth ; } public static void main ( String [ ] args ) { long n ; n = 5 ; System . out . println ( nthTerm ( n ) ) ; n = 25 ; System . out . println ( nthTerm ( n ) ) ; n = 25000000 ; System . out . println ( nthTerm ( n ) ) ; n = 250000007 ; System . out . println ( nthTerm ( n ) ) ; } }
Find the Nth term of the series 9 , 45 , 243 , 1377 | Java implementation of the approach ; Function to return the nth term of the given series ; nth term ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { static int nthTerm ( int n ) { int An = ( ( int ) Math . pow ( 1 , n ) + ( int ) Math . pow ( 2 , n ) ) * ( int ) Math . pow ( 3 , n ) ; return An ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( nthTerm ( n ) ) ; } }
Count the numbers < N which have equal number of divisors as K | Java implementation of the approach ; Function to return the count of the divisors of a number ; Count the number of 2 s that divide n ; n must be odd at this point . So we can skip one element ; While i divides n ; This condition is to handle the case when n is a prime number > 2 ; Count the total elements that have divisors exactly equal to as that of k 's ; Exclude k from the result if it is smaller than n . ; Driver code
public class GFG { static int countDivisors ( int n ) { int x = 0 , ans = 1 ; while ( n % 2 == 0 ) { x ++ ; n = n / 2 ; } ans = ans * ( x + 1 ) ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { x = 0 ; while ( n % i == 0 ) { x ++ ; n = n / i ; } ans = ans * ( x + 1 ) ; } if ( n > 2 ) ans = ans * 2 ; return ans ; } static int getTotalCount ( int n , int k ) { int k_count = countDivisors ( k ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( k_count == countDivisors ( i ) ) count ++ ; if ( k < n ) count = count - 1 ; return count ; } public static void main ( String [ ] args ) { int n = 500 , k = 6 ; System . out . println ( getTotalCount ( n , k ) ) ; } }
Find the nth term of the series 0 , 8 , 64 , 216 , 512 , . . . | Java implementation of the approach ; Function to return the nth term of the given series ; Common difference and first term ; First term ; nth term ; nth term of the given series ; Driver code
import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { static int nthTerm ( int n ) { int d = 2 ; int a1 = 0 ; int An = a1 + ( n - 1 ) * d ; return ( int ) Math . pow ( An , 3 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( nthTerm ( n ) ) ; } }
Count the number of intervals in which a given value lies | Java program to count the number of intervals in which a given value lies ; Function to count the number of intervals in which a given value lies ; Variables to store overall minimum and maximum of the intervals ; Variables to store start and end of an interval ; Frequency array to keep track of how many of the given intervals an element lies in ; Constructing the frequency array ; Driver code ; length of the array
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static final int MAX_VAL = 200000 ; static int countIntervals ( int [ ] [ ] arr , int V , int N ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; int li , ri ; int [ ] freq = new int [ MAX_VAL ] ; for ( int i = 0 ; i < N ; i ++ ) { li = arr [ i ] [ 0 ] ; freq [ li ] = freq [ li ] + 1 ; ri = arr [ i ] [ 1 ] ; freq [ ri + 1 ] = freq [ ri + 1 ] - 1 ; if ( li < min ) min = li ; if ( ri > max ) max = ri ; } for ( int i = min ; i <= max ; i ++ ) freq [ i ] = freq [ i ] + freq [ i - 1 ] ; return freq [ V ] ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 1 , 10 } , { 5 , 10 } , { 15 , 25 } , { 7 , 12 } , { 20 , 25 } } ; int V = 7 ; int N = arr . length ; System . out . println ( countIntervals ( arr , V , N ) ) ; } }
Split N ^ 2 numbers into N groups of equal sum | Java implementation of the above approach ; Function to print N groups of equal sum ; No . of Groups ; n / 2 pairs ; Driver code
import java . io . * ; class GFG { static void printGroups ( int n ) { int x = 1 ; int y = n * n ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n / 2 ; j ++ ) { System . out . print ( " { ▁ " + x + " , ▁ " + y + " } ▁ " ) ; x ++ ; y -- ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printGroups ( n ) ; } }
Program to find the Break Even Point | Java program to find Break Even Point ; Function to calculate Break Even Point ; Calculating number of articles to be sold ; Driver Code
import java . io . * ; import java . lang . * ; class GFG { public static int breakEvenPoint ( int exp1 , int S , int M ) { double earn = S - M ; double exp = exp1 ; double res = Math . ceil ( exp / earn ) ; int res1 = ( int ) res ; return res1 ; } public static void main ( String [ ] args ) { int exp = 3550 , S = 90 , M = 65 ; System . out . println ( breakEvenPoint ( exp , S , M ) ) ; } }
Minimize the value of N by applying the given operations | Java implementation of the above approach ; function to return the product of distinct prime factors of a number ; find distinct prime ; Driver code
import java . util . * ; class solution { static int minimum ( int n ) { int product = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; product = product * i ; } } if ( n >= 2 ) product = product * n ; return product ; } public static void main ( String arr [ ] ) { int n = 20 ; System . out . println ( minimum ( n ) ) ; } }
N digit numbers divisible by 5 formed from the M digits | Java program to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; Function to find the count of all possible N digit numbers which are divisible by 5 formed from M digits ; If it is not possible to form n digit number from the given m digits without repetition ; If both zero and five exists ; Remaining N - 1 iterations ; Remaining N - 1 iterations ; Driver code
class GFG { static int numbers ( int n , int arr [ ] , int m ) { int isZero = 0 , isFive = 0 ; int result = 0 ; if ( m < n ) { return - 1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( arr [ i ] == 0 ) { isZero = 1 ; } if ( arr [ i ] == 5 ) { isFive = 1 ; } } if ( isZero == 1 && isFive == 1 ) { result = 2 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else if ( isZero == 1 isFive == 1 ) { result = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { result = result * ( -- m ) ; } } else { result = - 1 ; } return result ; } public static void main ( String [ ] args ) { int n = 3 , m = 6 ; int arr [ ] = { 2 , 3 , 5 , 6 , 7 , 9 } ; System . out . println ( numbers ( n , arr , m ) ) ; } }
Program to find the smallest element among three elements | Java implementation to find the smallest of three elements
import java . io . * ; class GFG { public static void main ( String [ ] args ) { int a = 5 , b = 7 , c = 10 ; if ( a <= b && a <= c ) System . out . println ( a + " ▁ is ▁ the ▁ smallest " ) ; else if ( b <= a && b <= c ) System . out . println ( b + " ▁ is ▁ the ▁ smallest " ) ; else System . out . println ( c + " ▁ is ▁ the ▁ smallest " ) ; } }
Find subsequences with maximum Bitwise AND and Bitwise OR | Java implementation of the above approach ; Function to find the maximum sum ; Maximum AND is maximum element ; Maximum OR is bitwise OR of all . ; Driver code
import java . util . Arrays ; class GFG { static void maxSum ( int [ ] a , int n ) { int maxAnd = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int maxOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxOR |= a [ i ] ; } System . out . println ( ( maxAnd + maxOR ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] a = { 3 , 5 , 6 , 1 } ; maxSum ( a , n ) ; } }
Sum of minimum elements of all subarrays | Java implementation of above approach ;  ; Function to return required minimum sum ; getting number of element strictly larger than A [ i ] on Left . ; get elements from stack until element greater than A [ i ] found ; getting number of element larger than A [ i ] on Right . ; get elements from stack until element greater or equal to A [ i ] found ; calculating required resultult ; Driver Code ; function call to get required result
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int sumSubarrayMins ( int A [ ] , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack < pair > s1 = new Stack < pair > ( ) ; Stack < pair > s2 = new Stack < pair > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int cnt = 1 ; while ( ! s1 . isEmpty ( ) && ( s1 . peek ( ) . first ) > A [ i ] ) { cnt += s1 . peek ( ) . second ; s1 . pop ( ) ; } s1 . push ( new pair ( A [ i ] , cnt ) ) ; left [ i ] = cnt ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { int cnt = 1 ; while ( ! s2 . isEmpty ( ) && ( s2 . peek ( ) . first ) >= A [ i ] ) { cnt += s2 . peek ( ) . second ; s2 . pop ( ) ; } s2 . push ( new pair ( A [ i ] , cnt ) ) ; right [ i ] = cnt ; } int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) result = ( result + A [ i ] * left [ i ] * right [ i ] ) ; return result ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 1 , 2 , 4 } ; int n = A . length ; System . out . println ( sumSubarrayMins ( A , n ) ) ; } }
Minimum and Maximum element of an array which is divisible by a given number k | Java implementation of the above approach ; Function to find the minimum element ; Function to find the maximum element ; Driver code
class GFG { static int getMin ( int arr [ ] , int n , int k ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . min ( res , arr [ i ] ) ; } } return res ; } static int getMax ( int arr [ ] , int n , int k ) { int res = Integer . MIN_VALUE ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . max ( res , arr [ i ] ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 1230 , 45 , 67 , 1 } ; int k = 10 ; int n = arr . length ; System . out . println ( " Minimum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMin ( arr , n , k ) ) ; System . out . println ( " Maximum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMax ( arr , n , k ) ) ; } }
Print a number containing K digits with digital root D | Java implementation of the above approach ; Function to find a number ; If d is 0 k has to be 1 ; Print k - 1 zeroes ; Driver code
import java . io . * ; class GFG { static void printNumberWithDR ( int k , int d ) { if ( d == 0 && k != 1 ) System . out . print ( " - 1" ) ; else { System . out . print ( d ) ; k -- ; while ( k -- > 0 ) System . out . print ( "0" ) ; } } public static void main ( String [ ] args ) { int k = 4 , d = 4 ; printNumberWithDR ( k , d ) ; } }