codeparrot/xlcost-text-to-code huggingface dataset:
Count the number of non | Java program to count number of non increasing subarrays ; Initialize result ; Initialize length of current non increasing subarray ; Traverse through the array ; If arr [ i + 1 ] is less than or equal to arr [ i ] , then increment length ; Else Update count and reset length ; If last length is more than 1 ; Driver code
class GFG { static int countNonIncreasing ( int arr [ ] , int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] <= arr [ i ] ) len ++ ; else { cnt += ( ( ( len + 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len + 1 ) * len ) / 2 ) ; return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 7 , 1 , 1 } ; int n = arr . length ; System . out . println ( countNonIncreasing ( arr , n ) ) ; } }
Minimum array elements to be changed to make Recaman 's sequence | Java implementation of the approach ; First term of the sequence is always 0 ; Fill remaining terms using recursive formula ; If arr [ i - 1 ] - i is negative or already exists ; Function that returns minimum changes required ; Set to store first n Recaman numbers ; Generate and store first n Recaman numbers ; Insert first n Recaman numbers to set ; If current element of the array is present in the set ; Return the remaining number of elements in the set ; Driver code
import java . util . * ; class GFG { static int recamanGenerator ( int arr [ ] , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } return 0 ; } static int recamanArray ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; int recaman [ ] = new int [ n + 1 ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) s . remove ( arr [ i ] ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = arr . length ; System . out . print ( recamanArray ( arr , n ) ) ; } }
Check if product of array containing prime numbers is a perfect square | Java implementation of the approach ; Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code
import java . util . * ; class GFG { static boolean isPerfectSquare ( int [ ] arr , int n ) { HashMap < Integer , Integer > umap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( umap . containsKey ( arr [ i ] ) ) umap . put ( arr [ i ] , umap . get ( arr [ i ] ) + 1 ) ; else umap . put ( arr [ i ] , 1 ) ; } Iterator < Map . Entry < Integer , Integer > > iterator = umap . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry < Integer , Integer > entry = iterator . next ( ) ; if ( entry . getValue ( ) % 2 == 1 ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 7 , 7 } ; int n = arr . length ; if ( isPerfectSquare ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
Find the good permutation of first N natural numbers | Java implementation of the approach ; Function to print the good permutation of first N natural numbers ; If n is odd ; Otherwise ; Driver code
class GFG { static int printPermutation ( int n ) { if ( n % 2 != 0 ) { System . out . println ( " - 1" ) ; } else for ( int i = 1 ; i <= n / 2 ; i ++ ) { System . out . print ( 2 * i + " ▁ " + ( ( 2 * i ) - 1 ) + " ▁ " ) ; } return n ; } public static void main ( String [ ] args ) { int n = 4 ; printPermutation ( n ) ; } }
Minimum number operations required to convert n to m | Set | Java implementation of the above approach ; Function to find the minimum number of steps ; If n exceeds M ; If N reaches the target ; The minimum of both the states will be the answer ; Driver code
class GFG { static int MAXN = 10000000 ; static int minimumSteps ( int n , int m , int a , int b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return Math . min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } public static void main ( String [ ] args ) { int n = 120 , m = 51840 ; int a = 2 , b = 3 ; System . out . println ( minimumSteps ( n , m , a , b ) ) ; } }
Minimum number of given operation required to convert n to m | Java implementation of the approach ; Function to return the minimum operations required ; Counting all 2 s ; Counting all 3 s ; If q contained only 2 and 3 as the only prime factors then it must be 1 now ; Driver code
class GfG { static int minOperations ( int n , int m ) { if ( m % n != 0 ) return - 1 ; int minOperations = 0 ; int q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return - 1 ; } public static void main ( String [ ] args ) { int n = 120 , m = 51840 ; System . out . println ( minOperations ( n , m ) ) ; } }
Sum of Fibonacci Numbers in a range | Java implementation of the approach ; Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver code
import java . lang . Math ; class GFG { static int fib ( int n ) { double phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return ( int ) Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ; } static int calculateSum ( int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += fib ( i ) ; return sum ; } public static void main ( String [ ] args ) { int l = 4 , r = 8 ; System . out . println ( calculateSum ( l , r ) ) ; } }
Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone | Java Program to find the biggest sphere which is inscribed within a cube which in turn inscribed within a right circular cone ; Function to find the radius of the sphere ; height and radius cannot be negative ; radius of the sphere ; Driver code
import java . lang . Math ; class GFG { static float sphereSide ( float h , float r ) { if ( h < 0 && r < 0 ) return - 1 ; float R = ( float ) ( ( h * r * Math . sqrt ( 2 ) ) / ( h + Math . sqrt ( 2 ) * r ) ) / 2 ; return R ; } public static void main ( String [ ] args ) { float h = 5 , r = 6 ; System . out . println ( sphereSide ( h , r ) ) ; } }
Find the number of ways to divide number into four parts such that a = c and b = d | Java implementation for above approach ; Function to find the number of ways to divide N into four parts such that a = c and b = d ; Driver code
class GFG { static int possibleways ( int n ) { if ( n % 2 == 1 ) return 0 ; else if ( n % 4 == 0 ) return n / 4 - 1 ; else return n / 4 ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( possibleways ( n ) ) ; } }
Count sub | Java implementation of the approach ; Function to count sub - arrays whose product is divisible by K ; Calculate the product of the current sub - array ; If product of the current sub - array is divisible by K ; Driver code
import java . util . * ; class GFG { static int countSubarrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product = 1 ; for ( int x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 8 } ; int n = arr . length ; int K = 4 ; System . out . println ( countSubarrays ( arr , n , K ) ) ; } }
Count sub | Java implementation for above approach ; Segment tree implemented as an array ; Function to build the segment tree ; Function to query product of sub - array [ l . . r ] in O ( log n ) time ; Function to count sub - arrays whose product is divisible by K ; Query segment tree to find product % k of the sub - array [ i . . j ] ; Driver code ; Build the segment tree
class GFG { static int MAX = 100002 ; static long tree [ ] = new long [ 4 * MAX ] ; static void build ( int node , int start , int end , int [ ] arr , int k ) { if ( start == end ) { tree [ node ] = ( 1L * arr [ start ] ) % k ; return ; } int mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } static long query ( int node , int start , int end , int l , int r , int k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } int mid = ( start + end ) >> 1 ; long q1 = query ( 2 * node , start , mid , l , r , k ) ; long q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } static long countSubarrays ( int [ ] arr , int n , int k ) { long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 8 } ; int n = arr . length ; int k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; System . out . println ( countSubarrays ( arr , n , k ) ) ; } }
Find a pair from the given array with maximum nCr value | Java implementation of the approach ; Function to print the pair that gives maximum nCr ; This gives the value of N in nCr ; Case 1 : When N is odd ; Case 2 : When N is even ; Driver code
import java . util . * ; class GFG { static void printMaxValPair ( Vector < Long > v , int n ) { Collections . sort ( v ) ; long N = v . get ( ( int ) n - 1 ) ; if ( N % 2 == 1 ) { long first_maxima = N / 2 ; long second_maxima = first_maxima + 1 ; long ans1 = ( long ) 3e18 , ans2 = ( long ) 3e18 ; long from_left = - 1 , from_right = - 1 ; long from = - 1 ; for ( long i = 0 ; i < n ; ++ i ) { if ( v . get ( ( int ) i ) > first_maxima ) { from = i ; break ; } else { long diff = first_maxima - v . get ( ( int ) i ) ; if ( diff < ans1 ) { ans1 = diff ; from_left = v . get ( ( int ) i ) ; } } } from_right = v . get ( ( int ) from ) ; long diff1 = first_maxima - from_left ; long diff2 = from_right - second_maxima ; if ( diff1 < diff2 ) System . out . println ( N + " ▁ " + from_left ) ; else System . out . println ( N + " ▁ " + from_right ) ; } else { long maxima = N / 2 ; long ans1 = ( int ) 3e18 ; long R = - 1 ; for ( long i = 0 ; i < n - 1 ; ++ i ) { long diff = Math . abs ( v . get ( ( int ) i ) - maxima ) ; if ( diff < ans1 ) { ans1 = diff ; R = v . get ( ( int ) i ) ; } } System . out . println ( N + " ▁ " + R ) ; } } public static void main ( String args [ ] ) { long arr [ ] = { 1 , 1 , 2 , 3 , 6 , 1 } ; Vector < Long > v = new Vector < Long > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) v . add ( arr [ i ] ) ; int n = v . size ( ) ; printMaxValPair ( v , n ) ; } }
Find the number of good permutations | Java implementation of the approach ; Function to return the count of good permutations ; For m = 0 , ans is 1 ; If k is greater than 1 ; If k is greater than 2 ; If k is greater than 3 ; Driver code
class GFG { static int Permutations ( int n , int k ) { int ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( Permutations ( n , k ) ) ; } }
Count integers in a range which are divisible by their euler totient value | Java implementation of the above approach . ; Function to return a ^ n ; Function to return count of integers that satisfy n % phi ( n ) = 0 ; Driver Code
class GFG { static long power ( long a , long n ) { if ( n == 0 ) return 1 ; long p = power ( a , n / 2 ) ; p = p * p ; if ( n % 2 == 1 ) p = p * a ; return p ; } static int countIntegers ( long l , long r ) { long ans = 0 , i = 1 ; long v = power ( 2 , i ) ; while ( v <= r ) { while ( v <= r ) { if ( v >= l ) ans ++ ; v = v * 3 ; } i ++ ; v = power ( 2 , i ) ; } if ( l == 1 ) ans ++ ; return ( int ) ans ; } public static void main ( String [ ] args ) { long l = 12 , r = 21 ; System . out . println ( countIntegers ( l , r ) ) ; } }
Number of pairs from the first N natural numbers whose sum is divisible by K | Java implementation of the approach ; Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs
class GfG { static int findPairCount ( int N , int K ) { int count = 0 ; int rem [ ] = new int [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } public static void main ( String [ ] args ) { int N = 10 , K = 4 ; System . out . println ( findPairCount ( N , K ) ) ; } }
Find the sum of all Truncatable primes below N | Java implementation of the approach ; To check if a number is prime or not ; Sieve of Eratosthenes function to find all prime numbers ; Function to return the sum of all truncatable primes below n ; To store the required sum ; Check every number below n ; Check from right to left ; If number is not prime at any stage ; Check from left to right ; If number is not prime at any stage ; If flag is still true ; Return the required sum ; Driver code
import java . util . * ; class GFG { static final int N = 1000005 ; static boolean prime [ ] = new boolean [ N ] ; static void sieve ( ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } static int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; boolean flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 25 ; sieve ( ) ; System . out . println ( sumTruncatablePrimes ( n ) ) ; } }
Smallest and Largest N | Java implementation of the approach ; Function to print the largest and the smallest n - digit perfect squares ; Smallest n - digit perfect square ; Largest n - digit perfect square ; Driver code
class GFG { static void nDigitPerfectSquares ( int n ) { int smallest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) , 2 ) ; System . out . print ( smallest + " ▁ " ) ; int largest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) - 1 , 2 ) ; System . out . print ( largest ) ; } public static void main ( String args [ ] ) { int n = 4 ; nDigitPerfectSquares ( n ) ; } }
Maximum trace possible for any sub | Java program for the above approach ; Function to return the maximum trace possible for a sub - matrix of the given matrix ; Calculate the trace for each of the sub - matrix with top left corner at cell ( r , s ) ; Update the maximum trace ; Return the maximum trace ; Driver code
class GFG { static int N = 3 ; static int MaxTraceSub ( int mat [ ] [ ] ) { int max_trace = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = Math . max ( trace , max_trace ) ; } } } return max_trace ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 2 , 5 } , { 6 , 10 , 4 } , { 2 , 7 , - 10 } } ; System . out . println ( MaxTraceSub ( mat ) ) ; } }
Check if matrix can be converted to another matrix by transposing square sub | Java implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code
import java . util . * ; class GFG { static final int n = 3 ; static final int m = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int j = 0 ; j < v1 . size ( ) ; j ++ ) { if ( v1 . get ( j ) != v2 . get ( j ) ) { return false ; } } } for ( int j = 1 ; j < m ; j ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) { if ( v1 . get ( i ) != v2 . get ( i ) ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int b [ ] [ ] = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
Last digit of Product of two Large or Small numbers ( a * b ) | Java implementation of the above approach ; Function to print last digit of product a * b ; Driver code
class Solution { public static void lastDigit ( String a , String b ) { int lastDig = ( a . charAt ( a . length ( ) - 1 ) - '0' ) * ( b . charAt ( b . length ( ) - 1 ) - '0' ) ; System . out . println ( lastDig % 10 ) ; } public static void main ( String [ ] args ) { String a = "1234567891233" , b = "1234567891233156" ; lastDigit ( a , b ) ; } }
Smallest and Largest Palindrome with N Digits | Java implementation of the above approach ; Function to print the smallest and largest palindrome with N digits ; Driver Code
class GfG { static void printPalindrome ( int n ) { if ( n == 1 ) { System . out . println ( " Smallest ▁ Palindrome : ▁ 0" ) ; System . out . println ( " Largest ▁ Palindrome : ▁ 9" ) ; } else { System . out . println ( " Smallest ▁ Palindrome : ▁ " + ( int ) ( Math . pow ( 10 , n - 1 ) ) + 1 ) ; System . out . println ( " Largest ▁ Palindrome : ▁ " + ( ( int ) ( Math . pow ( 10 , n ) ) - 1 ) ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printPalindrome ( n ) ; } }
Addition of two numbers without propagating Carry | Java implementation of the approach ; Function to print sum of 2 numbers without propagating carry ; Reverse a ; Reverse b ; Generate sum Since length of both a and b are same , take any one of them . ; Extract digits from a and b and add ; If sum is single digit ; If sum is not single digit reverse sum ; Extract digits from sum and append to result ; Driver code
class GFG { static int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a != 0 ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b != 0 ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a != 0 ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) { res = res * 10 + sum ; } else { temp1 = 0 ; while ( sum != 0 ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum != 0 ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } public static void main ( String [ ] args ) { int a = 7752 , b = 8834 ; System . out . println ( printSum ( a , b ) ) ; } }
Number of digits before the decimal point in the division of two numbers | Java implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code
class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . abs ( a / b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void main ( String args [ ] ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }
Number of digits before the decimal point in the division of two numbers | Java implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Return the required count of digits ; Driver code
class GFG { public static int countDigits ( int a , int b ) { double digits = Math . log10 ( Math . abs ( a ) ) - Math . log10 ( Math . abs ( b ) ) + 1 ; return ( int ) Math . floor ( digits ) ; } public static void main ( String [ ] args ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }
Smallest odd number with N digits | Java implementation of the approach ; Function to return smallest odd with n digits ; Driver code
class Solution { static int smallestOdd ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( smallestOdd ( n ) ) ; } }
Largest Even and Odd N | Java implementation of the approach ; Function to print the largest n - digit even and odd numbers ; Driver code
class GFG { static void findNumbers ( int n ) { int odd = ( int ) Math . pow ( 10 , n ) - 1 ; int even = odd - 1 ; System . out . println ( " Even ▁ = ▁ " + even ) ; System . out . print ( " Odd ▁ = ▁ " + odd ) ; } public static void main ( String args [ ] ) { int n = 4 ; findNumbers ( n ) ; } }
Longest sub | Java implementation of the approach ; Function to return the length of the longest sub - array whose product of elements is 0 ; Driver code
class GFG { static int longestSubArray ( int arr [ ] , int n ) { boolean isZeroPresent = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { isZeroPresent = true ; break ; } } if ( isZeroPresent ) return n ; return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 0 , 1 , 2 , 0 } ; int n = arr . length ; System . out . print ( longestSubArray ( arr , n ) ) ; } }
Smallest Even number with N digits | Java implementation of the approach ; Function to return smallest even number with n digits ; Driver code
class Solution { static int smallestEven ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( smallestEven ( n ) ) ; } }
Maximize profit when divisibility by two numbers have associated profits | Java implementation of the approach ; Function to return the maximum profit ; min ( x , y ) * n / lcm ( a , b ) ; Driver code
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } public static void main ( String [ ] args ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; System . out . println ( maxProfit ( n , a , b , x , y ) ) ; } }