"Smallest length string with repeated replacement of two distinct adjacent | Java program to find smallest possible length of a string of only three characters ; Program to find length of reduced string in a string made of three characters . ; A memoized function find result recursively . a , b and c are counts of ' a ' s , ' b ' s and ' c ' s in str ; If this subproblem is already evaluated ; If this subproblem is already evaluated ; If there is only one type of character ; If only two types of characters are present ; If all types of characters are present . Try combining all pairs . ; Returns smallest possible length with given operation allowed . ; Counting occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; Initialize DP [ ] [ ] entries as - 1 ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int MAX_LEN = 110 ; static int [ ] [ ] [ ] DP = new int [ MAX_LEN ] [ MAX_LEN ] [ MAX_LEN ] ; static int length ( int a , int b , int c ) { if ( a < 0 b < 0 c < 0 ) if ( DP [ a ] [ b ] != - 1 ) return DP [ a ] [ b ] ; if ( a == 0 && b == 0 ) return ( DP [ a ] [ b ] = c ) ; if ( a == 0 && c == 0 ) return ( DP [ a ] [ b ] = b ) ; if ( b == 0 && c == 0 ) return ( DP [ a ] [ b ] = a ) ; if ( a == 0 ) return ( DP [ a ] [ b ] = length ( a + 1 , b - 1 , c - 1 ) ) ; if ( b == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b + 1 , c - 1 ) ) ; if ( c == 0 ) return ( DP [ a ] [ b ] = length ( a - 1 , b - 1 , c + 1 ) ) ; DP [ a ] [ b ] = Math . min ( length ( a - 1 , b - 1 , c + 1 ) , Math . min ( length ( a - 1 , b + 1 , c - 1 ) , length ( a + 1 , b - 1 , c - 1 ) ) ) ; return DP [ a ] [ b ] ; } static int stringReduction ( String str ) { int n = str . length ( ) ; int [ ] count = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i <= count [ 0 ] ; ++ i ) for ( int j = 0 ; j < count [ 1 ] ; ++ j ) for ( int k = 0 ; k < count [ 2 ] ; ++ k ) DP [ i ] [ j ] [ k ] = - 1 ; return length ( count [ 0 ] , count [ 1 ] , count [ 2 ] ) ; } public static void main ( String [ ] args ) { String str = " abcbbaacb " ; System . out . println ( stringReduction ( str ) ) ; } }


"Smallest length string with repeated replacement of two distinct adjacent | Java program to find smallest possible length of a string of only three characters ; Returns smallest possible length with given operation allowed . ; Counint occurrences of three different characters ' a ' , ' b ' and ' c ' in str ; If all characters are same . ; If all characters are present even number of times or all are present odd number of times . ; Answer is 1 for all other cases . ; Driver code"
How can the above be solved in Java?

public class GFG { static int stringReduction ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } if ( count [ 0 ] == n count [ 1 ] == n count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) { return 2 ; } return 1 ; } public static void main ( String [ ] args ) { String str = " abcbbaacb " ; System . out . println ( stringReduction ( str ) ) ; } }


"Smallest missing non | Java program to implement the above approach ; Function to print the smallest missing non - negative integer up to every array indices ; Stores the smallest missing non - negative integers between start index to current index ; Store the boolean value to check smNonNeg present between start index to each index of the array ; Traverse the array ; Since output always lies in the range [ 0 , N - 1 ] ; Check if smNonNeg is present between start index and current index or not ; Print smallest missing non - negative integer ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . Arrays ; class GFG { static void smlstNonNeg ( int arr [ ] , int N ) { int smNonNeg = 0 ; Boolean [ ] hash = new Boolean [ N + 1 ] ; Arrays . fill ( hash , false ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } System . out . print ( smNonNeg + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 3 , 5 } ; int N = arr . length ; smlstNonNeg ( arr , N ) ; } }


"Smallest multiple of 3 which consists of three given non | Java implementation of the approach ; Function to return the minimum number divisible by 3 formed by the given digits ; Sort the given array in ascending ; Check if any single digit is divisible by 3 ; Check if any two digit number formed by the given digits is divisible by 3 starting from the minimum ; Generate the two digit number ; If none of the above is true , we can form three digit number by taking a [ 0 ] three times . ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; public class GFG { static int printSmallest ( int a [ ] ) { int sum , sum1 ; Arrays . sort ( a ) ; int i , j , k , num ; for ( i = 0 ; i < 3 ; i ++ ) { if ( a [ i ] % 3 == 0 ) { return a [ i ] ; } } for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { num = ( a [ i ] * 10 ) + a [ j ] ; if ( num % 3 == 0 ) { return num ; } } } return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 7 , 1 } ; System . out . println ( printSmallest ( arr ) ) ; } }


"Smallest multiple of N with exactly N digits in its Binary number representation | Java program to find smallest multiple of n with exactly N digits in Binary Number System . ; Function to find smallest multiple of n with exactly N digits in Binary Number System . ; Driver code"
How can the above be solved in Java?

class GFG { static void smallestNumber ( int N ) { System . out . print ( N * Math . ceil ( Math . pow ( 2 , ( N - 1 ) ) / N ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; smallestNumber ( N ) ; } }


"Smallest n digit number divisible by given three numbers | Java program to find smallest n digit number which is divisible by x , y and z . ;  ; LCM for x , y , z ; returns smallest n digit number divisible by x , y and z ; find the LCM ; find power of 10 for least number ; reminder after ; If smallest number itself divides lcm . ; add lcm - reminder number for next n digit number ; this condition check the n digit number is possible or not if it is possible it return the number else return 0 ; driver code ; if number is possible then it print the number"
How can the above be solved in Java?

import java . io . * ; public class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) { return a ; } else { return __gcd ( b , a % b ) ; } } static int LCM ( int x , int y , int z ) { int ans = ( ( x * y ) / ( __gcd ( x , y ) ) ) ; return ( ( z * ans ) / ( __gcd ( ans , z ) ) ) ; } static int findDivisible ( int n , int x , int y , int z ) { int lcm = LCM ( x , y , z ) ; int ndigitnumber = ( int ) Math . pow ( 10 , n - 1 ) ; int reminder = ndigitnumber % lcm ; if ( reminder == 0 ) return ndigitnumber ; ndigitnumber += lcm - reminder ; if ( ndigitnumber < Math . pow ( 10 , n ) ) return ndigitnumber ; else return 0 ; } static public void main ( String [ ] args ) { int n = 4 , x = 2 , y = 3 , z = 5 ; int res = findDivisible ( n , x , y , z ) ; if ( res != 0 ) System . out . println ( res ) ; else System . out . println ( " Not ▁ possible " ) ; } }


"Smallest non | Java implementation of the approach ; Function to return the length of the smallest substring divisible by 2 ^ k ; To store the final answer ; Left pointer ; Right pointer ; Count of the number of zeros and ones in the current substring ; Loop for two pointers ; Condition satisfied ; Updated the answer ; Update the pointer and count ; Update the pointer and count ; Driver code"
How can the above be solved in Java?

class GFG { static final int INT_MAX = Integer . MAX_VALUE ; static int findLength ( String s , int k ) { int ans = INT_MAX ; int l = 0 ; int r = 0 ; int cnt_zero = 0 , cnt_one = 0 ; while ( l < s . length ( ) && r <= s . length ( ) ) { if ( cnt_zero >= k && cnt_one >= 1 ) { ans = Math . min ( ans , r - l ) ; l ++ ; if ( s . charAt ( l - 1 ) == '0' ) cnt_zero -- ; else cnt_one -- ; } else { if ( r == s . length ( ) ) break ; if ( s . charAt ( r ) == '0' ) cnt_zero ++ ; else cnt_one ++ ; r ++ ; } } if ( ans == INT_MAX ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String s = "100" ; int k = 2 ; System . out . println ( findLength ( s , k ) ) ; } }


"Smallest number S such that N is a factor of S factorial or S ! | Java Program to find factorial that N belongs to ; Calculate prime factors for a given number ; Container for prime factors ; Iterate from 2 to i ^ 2 finding all factors ; If we still have a remainder it is also a prime factor ; Calculate occurrence of an element in factorial of a number ; Iterate through prime factors ; Check if factorial contains less occurrences of prime factor ; Method to binary search 1 to N ; Prime factors are not in the factorial Increase the lowerbound ; We have reached smallest occurrence ; Smaller factorial satisfying requirements may exist , decrease upperbound ; Calculate prime factors and search ; Driver method"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . Iterator ; import java . util . Set ; class Test { static HashMap < Long , Integer > primeFactors ( long num ) { HashMap < Long , Integer > ans = new HashMap < Long , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( containsKey ( key ) ) { return super . get ( key ) ; } return 0 ; } } ; for ( long i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { num /= i ; ans . put ( i , ans . get ( i ) + 1 ) ; } } if ( num > 1 ) ans . put ( num , ans . get ( num ) + 1 ) ; ; return ans ; } static long legendre ( long factor , long num ) { long count = 0 , fac2 = factor ; while ( num >= factor ) { count += num / factor ; factor *= fac2 ; } return count ; } static boolean possible ( HashMap < Long , Integer > factors , long num ) { Set < Long > s = factors . keySet ( ) ; Iterator < Long > itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { long temp = itr . next ( ) ; if ( legendre ( temp , num ) < factors . get ( temp ) ) return false ; } return true ; } static long search ( long start , long end , HashMap < Long , Integer > factors ) { long mid = ( start + end ) / 2 ; if ( ! possible ( factors , mid ) ) return search ( mid + 1 , end , factors ) ; if ( start == mid ) return mid ; return search ( start , mid , factors ) ; } static long findFact ( long num ) { HashMap < Long , Integer > factors = primeFactors ( num ) ; return search ( 1 , num , factors ) ; } public static void main ( String args [ ] ) { System . out . println ( findFact ( 6 ) ) ; System . out . println ( findFact ( 997587429953L ) ) ; } }


"Smallest number containing all possible N length permutations using digits 0 to D | Java Program to find the minimum length string consisting of all permutations of length N of D digits ; Initialize hashset to see if all the possible permutations are present in the min length string ; To keep min length string ; Generate the required string ; Iterate over all the possible character ; Append to make a new string ; If the new string is not visited ; Add in hashset ; Call the dfs function on the last d characters ; Base case ; Append '0' n - 1 times ; Call the DFS Function ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . lang . * ; class GeeksforGeeks { static Set < String > visited ; static StringBuilder ans ; public static void dfs ( String curr , int D ) { for ( int x = 0 ; x < D ; ++ x ) { String neighbour = curr + x ; if ( ! visited . contains ( neighbour ) ) { visited . add ( neighbour ) ; dfs ( neighbour . substring ( 1 ) , D ) ; ans . append ( x ) ; } } } public static String reqString ( int N , int D ) { if ( N == 1 && D == 1 ) return "0" ; visited = new HashSet < > ( ) ; ans = new StringBuilder ( ) ; StringBuilder sb = new StringBuilder ( ) ; for ( int i = 0 ; i < N - 1 ; ++ i ) { sb . append ( "0" ) ; } String start = sb . toString ( ) ; dfs ( start , D ) ; ans . append ( start ) ; return new String ( ans ) ; } public static void main ( String args [ ] ) { int N = 2 ; int D = 2 ; System . out . println ( reqString ( N , D ) ) ; } }


"Smallest number dividing minimum number of elements in the Array | Java implementation of the approach ; Function to return the smallest number that divides minimum number of elements ; m stores the maximum in the array ; Frequency table ; Loop to factorize ; sqrt factorization of the numbers ; Finding the smallest number with zero multiples ; Driver code"
How can the above be solved in Java?

class GFG { static int findMin ( int arr [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; int cnt [ ] = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else { cnt [ j ] ++ ; cnt [ arr [ i ] / j ] ++ ; } } } } for ( int i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 12 , 6 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }


"Smallest number dividing minimum number of elements in the array | Set 2 | Java implementation of the approach ; Function to return the smallest number that divides minimum number of elements in the given array ; m stores the maximum in the array ; Frequency array ; Sieve ; Incrementing j ; If no multiples of j are in the array ; Driver code"
How can the above be solved in Java?

class GFG { static int findMin ( int arr [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; int freq [ ] = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; for ( int i = 1 ; i <= m + 1 ; i ++ ) { int j = i ; int cnt = 0 ; while ( j <= m ) { cnt += freq [ j ] ; j += i ; } if ( cnt == 0 ) return i ; } return m + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 12 , 6 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }


"Smallest number divisible by first n numbers | Java program to find the smallest number evenly divisible by all numbers 1 to n ; Function for gcd ; Function returns the lcm of first n numbers ; Driver program to test the above function"
How can the above be solved in Java?

class GFG { static long gcd ( long a , long b ) { if ( a % b != 0 ) return gcd ( b , a % b ) ; else return b ; } static long lcm ( long n ) { long ans = 1 ; for ( long i = 1 ; i <= n ; i ++ ) ans = ( ans * i ) / ( gcd ( ans , i ) ) ; return ans ; } public static void main ( String [ ] args ) { long n = 20 ; System . out . println ( lcm ( n ) ) ; } }


"Smallest number greater or equals to N such that it has no odd positioned bit set | Java implementation of the approach ; Function to count the total bits ; Iterate and find the number of set bits ; Right shift the number by 1 ; Function to find the nearest number ; Count the total number of bits ; To get the position ; If the last set bit is at odd position then answer will always be a number with the left bit set ; Set all the even bits which are possible ; If the number still is less than N ; Return the number by setting the next even set bit ; If we have reached this position it means tempsum > n hence turn off even bits to get the first possible number ; Turn off the bit ; If it gets lower than N then set it and return that number ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } static int findNearestNumber ( int n ) { int cnt = countBits ( n ) ; cnt -= 1 ; if ( cnt % 2 == 1 ) { return 1 << ( cnt + 1 ) ; } else { int tempnum = 0 ; for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum += 1 << i ; } if ( tempnum < n ) { return ( 1 << ( cnt + 2 ) ) ; } else if ( tempnum == n ) { return n ; } for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum -= ( 1 << i ) ; if ( tempnum < n ) { return tempnum += ( 1 << i ) ; } } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int n = 19 ; System . out . println ( findNearestNumber ( n ) ) ; } }


"Smallest number greater than X which is K | Java program to find the smallest K periodic integer greater than X ; Function to find the smallest K periodic integer greater than X ; Stores the number in a temporary String ; Set X [ i ] = X [ i - k ] for i > k ; Start from the current index ; Loop upto N change X [ j ] to X [ i ] ; Return X if current Value is greater than original value ; Find the first digit not equal to 9 ; Increment X [ i ] ; Set POS to current index ; Change X [ i ] to 0 for all indices from POS + 1 to K ; Set X [ i ] = X [ i - k ] for i > k ; Loop upto N change X [ j ] to X [ i ] ; Return the final String ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String Kperiodicinteger ( char [ ] X , int N , int K ) { String temp = String . valueOf ( X ) ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( String . valueOf ( X ) . compareTo ( temp ) >= 0 ) { return String . valueOf ( X ) ; } int POS = 0 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return String . valueOf ( X ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; String X = "1215" ; System . out . print ( Kperiodicinteger ( X . toCharArray ( ) , N , K ) ) ; } }


"Smallest number greater than n that can be represented as a sum of distinct power of k | Java implementation of the above approach ; Function to find the smallest number greater than or equal to n represented as the sum of distinct powers of k ; Vector P to store the base k representation of the number ; If the representation is >= 2 , then this power of k has to be added once again and then increase the next power of k and make the current power 0 ; Reduce all the lower power of k to 0 ; Check if the most significant bit also satisfy the above conditions ; Converting back from the k - nary representation to decimal form . ; Driver code"
How can the above be solved in Java?

 class GFG { static void greaterK ( int n , int k ) { int [ ] p = new int [ String . valueOf ( n ) . length ( ) + 2 ] ; int index = 0 ; int x = n ; while ( x > 0 ) { p [ index ] = ( int ) ( x % k ) ; x /= k ; index ++ ; } int idx = 0 ; for ( int i = 0 ; i < p . length - 1 ; ++ i ) { if ( p [ i ] >= 2 ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; for ( int j = idx ; j < i ; ++ j ) { p [ j ] = 0 ; } idx = i + 1 ; } if ( p [ i ] == k ) { p [ i ] = 0 ; p [ i + 1 ] ++ ; } } int j = p . length - 1 ; if ( p [ j ] >= 2 ) { p [ index ] = 1 ; index ++ ; } int ans = 0 ; for ( int i = p . length - 1 ; i >= 0 ; -- i ) { ans = ans * k + p [ i ] ; } System . out . print ( ans + "
"); } public static void main ( String [ ] args ) { int n = 29 , k = 7 ; greaterK ( n , k ) ; } }


"Smallest number greater than or equal to N divisible by K | Java implementation of the above approach ; Function to find the smallest number greater than or equal to N that is divisible by k ; Driver Code"
How can the above be solved in Java?

public class GFG { static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static void main ( String [ ] args ) { int N = 45 , K = 6 ; System . out . println ( " Smallest ▁ number ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " ▁ + ▁ K ▁ + ▁ " is " + findNum(N, K)); } }


"Smallest number greater than or equal to N using only digits 1 to K | Java program to find the smallest number greater than or equal to N which is made up of first K digits ; Function to count the digits greater than K ; Function to print the list ; Function to find the number greater than or equal to n , which is only made of first k digits ; If the number itself satisfy the conditions ; Check digit from back ; If digit > K is present previously and current digit is less than K ; If current digit is greater than K ; If an extra digit needs to be added ; Print the number ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int CountGreater ( int n , int k ) { int a = 0 ; while ( n > 0 ) { if ( ( n % 10 ) > k ) { a ++ ; } n = n / 10 ; } return a ; } static void PrintList ( List < Integer > ans ) { for ( int it : ans ) System . out . print ( it ) ; } static void getNumber ( int n , int k ) { int count = CountGreater ( n , k ) ; if ( count == 0 ) { System . out . print ( n ) ; return ; } List < Integer > ans = new LinkedList < > ( ) ; boolean changed = false ; while ( n > 0 ) { int digit = n % 10 ; if ( changed == true ) { ans . add ( 0 , digit ) ; } else { if ( count == 0 && digit < k ) { ans . add ( 0 , digit + 1 ) ; changed = true ; } else { ans . add ( 0 , 1 ) ; if ( digit > k ) { count -- ; } } } n = n / 10 ; } if ( changed == false ) { ans . add ( 0 , 1 ) ; } PrintList ( ans ) ; return ; } public static void main ( String [ ] args ) { int N = 51234 ; int K = 4 ; getNumber ( N , K ) ; } }


"Smallest number greater than or equal to N which is divisible by its non | Java program for the above approach ; Function to find the smallest number greater than or equal to N such that it is divisible by its non - zero digits ; Iterate in range [ N , N + 2520 ] ; To check if the current number satisfies the given condition ; Store the number in a temporary variable ; Loop until temp > 0 ; Check only for non zero digits ; Extract the current digit ; If number is divisible by current digit or not ; Otherwise , set possible to 0 ; Break out of the loop ; Divide by 10 ; Driver code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { int possible = 1 ; int temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { System . out . println ( i ) ; return ; } } } public static void main ( String [ ] args ) { int N = 31 ; findSmallestNumber ( N ) ; } }


"Smallest number k such that the product of digits of k is equal to n | Java implementation to find smallest number k such that the product of digits of k is equal to n ; function to find smallest number k such that the product of digits of k is equal to n ; if ' n ' is a single digit number , then it is the required number ; stack the store the digits ; repeatedly divide ' n ' by the numbers from 9 to 2 until all the numbers are used or ' n ' > 1 ; save the digit ' i ' that divides ' n ' onto the stack ; if true , then no number ' k ' can be formed ; pop digits from the stack ' digits ' and add them to ' k ' ; required smallest number ; Driver program to test above"
How can the above be solved in Java?

import java . util . Stack ; public class GFG { static long smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) { return n ; } Stack < Integer > digits = new Stack < > ( ) ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . push ( i ) ; n = n / i ; } } if ( n != 1 ) { return - 1 ; } long k = 0 ; while ( ! digits . empty ( ) ) { k = k * 10 + digits . peek ( ) ; digits . pop ( ) ; } return k ; } static public void main ( String [ ] args ) { int n = 100 ; System . out . println ( smallestNumber ( n ) ) ; } }


"Smallest number not less than N which is divisible by all digits of N | Java program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static int LCM ( int A , int B ) { return ( A * B / __gcd ( A , B ) ) ; } static void findSmallestNumber ( int X ) { int lcm = 1 ; int temp = X ; while ( temp > 0 ) { int last = temp % 10 ; temp /= 10 ; if ( last == 0 ) continue ; lcm = LCM ( lcm , last ) ; } int answer = ( ( X + lcm - 1 ) / lcm ) * lcm ; System . out . print ( answer ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int X = 280 ; findSmallestNumber ( X ) ; } }


"Smallest number possible by repeatedly multiplying with K or 2 exactly N times starting from 1 | Java program for the above approach ; Function to find the minimum value of X after increment X by K or twice value of X in each of N operations ; Iterate over the range [ 1 , N ] ; If the value of X is less than equal to K ; Otherwise ; Return the minimum value of X ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int minPossibleValue ( int N , int K , int X ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } public static void main ( String [ ] args ) { int N = 7 , K = 4 , X = 1 ; System . out . println ( minPossibleValue ( N , K , X ) ) ; } }


"Smallest number possible by swapping adjacent even odd pairs | Java program to find the smallest number possible by swapping adjacent digits of different parity ; Function to return the smallest number possible ; Arrays to store odd and even digits in the order their appearance in the given String ; Insert the odd and even digits ; Pointer to odd digit ; Pointer to even digit ; In case number of even and odd digits are not equal If odd digits are remaining ; If even digits are remaining ; Removal of leading 0 's ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String findAns ( String s ) { int digit ; Vector < Integer > odd = new Vector < Integer > ( ) ; Vector < Integer > even = new Vector < Integer > ( ) ; for ( char c : s . toCharArray ( ) ) { digit = c - '0' ; if ( digit % 2 == 1 ) odd . add ( digit ) ; else even . add ( digit ) ; } int i = 0 ; int j = 0 ; String ans = " " ; while ( i < odd . size ( ) && j < even . size ( ) ) { if ( odd . get ( i ) < even . get ( j ) ) ans += ( char ) ( odd . get ( i ++ ) + '0' ) ; else ans += ( char ) ( even . get ( j ++ ) + '0' ) ; } while ( i < odd . size ( ) ) ans += ( char ) ( odd . get ( i ++ ) + '0' ) ; while ( j < even . size ( ) ) ans += ( char ) ( even . get ( j ++ ) + '0' ) ; while ( ans . charAt ( 0 ) == '0' ) { ans = ans . substring ( 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String s = "894687536" ; System . out . print ( findAns ( s ) ) ; } }


"Smallest number required to be added to M to make it divisible by N | Java program for the above approach ; Function to find the smallest number greater than or equal to N , that is divisible by k ; Function to find the smallest number required to be added to to M to make it divisible by N ; Stores the smallest multiple of N , greater than or equal to M ; Return the result ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

class GFG { public static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static int findSmallest ( int M , int N ) { int x = findNum ( M , N ) ; return x - M ; } public static void main ( String args [ ] ) { int M = 100 , N = 28 ; System . out . println ( findSmallest ( M , N ) ) ; } }


"Smallest number that can replace all | Java program for the above approach ; Function to find the value of K to minimize the value of maximum absolute difference between adjacent elements ; Stores the maximum and minimum among array elements that are adjacent to " - 1" ; Traverse the given array arr [ ] ; If arr [ i ] is - 1 & arr [ i + 1 ] is not - 1 ; If arr [ i + 1 ] is - 1 & arr [ i ] is not - 1 ; If all array element is - 1 ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static void findMissingValue ( int arr [ ] , int N ) { int minE = Integer . MAX_VALUE , maxE = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Integer . MAX_VALUE && maxE == Integer . MIN_VALUE ) { System . out . println ( "0" ) ; } else { System . out . println ( ( minE + maxE ) / 2 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , - 1 , - 1 , 5 } ; int N = arr . length ; findMissingValue ( arr , N ) ; } }


"Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal | Java program for the above approach ; Utility function to find the answer ; Stores the frequencies of array elements ; Stores the possible values of X ; Generate possible positive values of X ; Initialize answer to MAX value ; Flag to check if the current element of the set can be considered ; If the frequency of an element in A [ ] is not equal to that in B [ ] after the operation ; Current set element cannot be considered ; Update minimum value of X ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { HashMap < Integer , Integer > mapA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mapB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . containsKey ( A [ i ] ) ) { mapA . put ( A [ i ] , mapA . get ( A [ i ] ) + 1 ) ; } else { mapA . put ( A [ i ] , 1 ) ; } if ( mapB . containsKey ( B [ i ] ) ) { mapB . put ( B [ i ] , mapB . get ( B [ i ] ) + 1 ) ; } else { mapB . put ( B [ i ] , 1 ) ; } } HashSet < Integer > possibleValues = new HashSet < Integer > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Integer . MAX_VALUE ; for ( int it : possibleValues ) { boolean possible = true ; for ( Map . Entry < Integer , Integer > it2 : mapA . entrySet ( ) ) { if ( it2 . getValue ( ) != mapB . get ( ( it2 . getKey ( ) + it ) % m ) ) { possible = false ; break ; } } if ( possible ) { ans = Math . min ( ans , it ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; System . out . print ( moduloEquality ( A , B , n , m ) + "
"); } }


"Smallest number to be subtracted to convert given number to a palindrome | Java program for the above approach ; Function to evaluate minimum subtraction required to make a number palindrome ; Counts number of subtractions required ; Run a loop till N >= 0 ; Store the current number ; Store reverse of current number ; Reverse the number ; Check if N is palindrome ; Increment the counter ; Reduce the number by 1 ; Print the result ; Driver Code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void minSub ( int N ) { int count = 0 ; while ( N >= 0 ) { int num = N ; int rev = 0 ; while ( num != 0 ) { int digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = num / 10 ; } if ( N == rev ) { break ; } count ++ ; N -- ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 3456 ; minSub ( N ) ; } }


"Smallest number to make Array sum at most K by dividing each element | Java program to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Function to find the smallest number such that the sum of the array becomes less than or equal to K when every element of the array is divided by that number ; Binary search between 1 and 10 ^ 9 ; Calculating the new sum after dividing every element by mid ; If after dividing by mid , if the new sum is less than or equal to limit move low to mid + 1 ; Else , move mid + 1 to high ; Returning the minimum number ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findMinDivisor ( int arr [ ] , int n , int limit ) { int low = 0 , high = 1000000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += Math . ceil ( ( double ) arr [ i ] / ( double ) mid ) ; } if ( sum <= limit ) high = mid ; else low = mid + 1 ; } return low ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 9 } ; int N = arr . length ; int K = 6 ; System . out . println ( findMinDivisor ( arr , N , K ) ) ; } }


"Smallest number to multiply to convert floating point to natural | Java program to find the smallest number to multiply to convert a floating point number into natural number . ; Finding GCD of two number ; Returns smallest integer k such that k * str becomes natural . str is an input floating point number ; Find size of string representing a floating point number . ; Below is used to find denominator in fraction form . ; Used to find value of count_after_dot ; To find numerator in fraction form of given number . For example , for 30.25 , numerator would be 3025. ; If there was no dot , then number is already a natural . ; Find denominator in fraction form . For example , for 30.25 , denominator is 100 ; Result is denominator divided by GCD - of - numerator - and - denominator . For example , for 30.25 , result is 100 / GCD ( 3025 , 100 ) = 100 / 25 = 4 ; Driver code"
How can the above be solved in Java?

class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findnum ( String str ) { int n = str . length ( ) ; int count_after_dot = 0 ; boolean dot_seen = false ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != ' . ' ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; int dem = ( int ) Math . pow ( 10 , count_after_dot ) ; return ( dem / gcd ( num , dem ) ) ; } public static void main ( String [ ] args ) { String str = "5.125" ; System . out . print ( findnum ( str ) ) ; } }


"Smallest number which is not coprime with any element of an array | Java program for the above approach ; Function check if a number is prime or not ; Corner cases ; Check if n is divisible by 2 or 3 ; Check for every 6 th number . The above checking allows to skip middle 5 numbers ; Function to store primes in an array ; Function to calculate GCD of two numbers ; Function to find the smallest number which is not coprime with any element of the array arr [ ] ; Function call to fill the prime numbers ; Stores the answer ; Generate all non - empty subsets of the primes [ ] array ; Stores product of the primes ; Checks if temp is coprime with the array or not ; Check if the product temp is not coprime with the whole array ; If the product is not co - prime with the array ; Prlong the answer ; Driver code ; Given array ; Stores the size of the array"
How can the above be solved in Java?

import java . util . * ; class GFG { static long MAX = 50 ; static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void findPrime ( ArrayList < Long > primes ) { for ( long i = 2 ; i <= MAX ; i ++ ) { if ( isPrime ( i ) ) primes . add ( i ) ; } } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void findMinimumNumber ( long [ ] arr , long N ) { ArrayList < Long > primes = new ArrayList < Long > ( ) ; findPrime ( primes ) ; long ans = 2147483647 ; int n = primes . size ( ) ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { long temp = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) > 0 ) { temp *= primes . get ( j ) ; } } boolean check = true ; for ( long k = 0 ; k < N ; k ++ ) { if ( gcd ( temp , arr [ ( int ) k ] ) == 1l ) { check = false ; break ; } } if ( check == true ) ans = Math . min ( ans , temp ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { long [ ] arr = { 3 , 4 , 6 , 7 , 8 , 9 , 10 } ; long N = arr . length ; findMinimumNumber ( arr , N ) ; } }


"Smallest number whose product with N has sum of digits equal to that of N | Java program for the above approach ; Function to find the minimum integer having sum of digits of a number multiplied by n equal to sum of digits of n ; Initialize answer ; Convert string to character array ; Find sum of digits of N ; Multiply N with x ; Sum of digits of the new number ; If condition satisfies ; Print answer ; Driver Code ; Function call"
How can the above be solved in Java?

class GFG { static void find_num ( String n ) { int ans = 0 ; char [ ] digitsOfN = n . toCharArray ( ) ; int sumOfDigitsN = 0 ; for ( char c : digitsOfN ) { sumOfDigitsN += Integer . parseInt ( Character . toString ( c ) ) ; } for ( int x = 11 ; x > 0 ; x ++ ) { int newNum = x * Integer . parseInt ( n ) ; int tempSumDigits = 0 ; char [ ] temp = Integer . toString ( newNum ) . toCharArray ( ) ; for ( char c : temp ) { tempSumDigits += Integer . parseInt ( Character . toString ( c ) ) ; } if ( tempSumDigits == sumOfDigitsN ) { ans = x ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String N = "3029" ; find_num ( N ) ; } }


"Smallest number whose set bits are maximum in a given range | Java program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } public static void main ( String [ ] args ) { int l = 1 ; int r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . println ( countMaxSetBits ( l , r ) ) ; } }


"Smallest number whose set bits are maximum in a given range | Java program to find number whose set bits are maximum among ' l ' and ' r ' ; Returns smallest number whose set bits are maximum in given range . ; Initialize the maximum count and final answer as ' num ' ; Traverse for every bit of ' i ' number ; If count is greater than previous calculated max_count , update it ; Driver code"
How can the above be solved in Java?

class gfg { static int countMaxSetBits ( int left , int right ) { int max_count = - 1 , num = 0 ; for ( int i = left ; i <= right ; ++ i ) { int temp = i , cnt = 0 ; while ( temp > 0 ) { if ( temp % 2 == 1 ) ++ cnt ; temp >>= 1 ; } if ( cnt > max_count ) { max_count = cnt ; num = i ; } } return num ; } public static void main ( String [ ] args ) { int l = 1 , r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . print ( countMaxSetBits ( l , r ) ) ; } }


"Smallest number whose square has N digits | Java program for above approach ; Function to return smallest number whose square has N digits ; Calculate N - th term of the series ; Driver code"
How can the above be solved in Java?

class GFG { static int smallestNum ( int N ) { float x = ( float ) ( Math . pow ( 10 , ( N - 1 ) / 2.0 ) ) ; return ( int ) ( Math . ceil ( x ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . print ( smallestNum ( N ) ) ; } }


"Smallest number whose sum of digits is N and every digit occurring at most K times | Java program for the above approach ; Function to find the smallest number whose sum of digits is N and each digit occurring at most K times ; Maximum sum possible using each digit at most K times ; Append smallest number into the resultant string ; Iterate over the range [ 9 , 1 ] ; If the count of the digit is K , then update count and check for the next digit ; If N is greater than current digit ; Subtract i from N ; Insert i as a digit ; Insert remaining N as a digit ; Increment count ; Reverse the string ; Print the answer ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findSmallestNumberPossible ( int N , int K ) { if ( N > 45 * K ) { System . out . print ( " - 1" ) ; return ; } StringBuilder res = new StringBuilder ( ) ; int count = 0 ; for ( int i = 9 ; i >= 1 { if ( count == K ) { i -- ; count = 0 ; } if ( N > i ) { N -= i ; res . append ( ( char ) ( '0' + i ) ) ; } else { res . append ( ( char ) ( '0' + N ) ) ; N = 0 ; break ; } count ++ ; } res . reverse ( ) ; System . out . print ( res . toString ( ) ) ; } public static void main ( String [ ] args ) { int N = 25 , K = 3 ; findSmallestNumberPossible ( N , K ) ; } }


"Smallest number whose sum of digits is square of N | Java implementation of the above approach ; Function to return smallest number whose sum of digits is n ^ 2 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int smallestNum ( int n ) { return ( int ) ( ( n * n % 9 + 1 ) * Math . pow ( 10 , n * n / 9 ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }


"Smallest number with given sum of digits and sum of square of digits | Java program to find the Smallest number with given sum of digits and sum of square of digits ; Top down dp to find minimum number of digits with given sum of dits a and sum of square of digits as b ; Invalid condition ; Number of digits satisfied ; Memoization ; Initialize ans as maximum as we have to find the minimum number of digits ; Check for all possible combinations of digits ; recurrence call ; If the combination of digits cannot give sum as a and sum of square of digits as b ; Returns the minimum number of digits ; Function to print the digits that gives sum as a and sum of square of digits as b ; initialize the dp array as - 1 ; base condition ; function call to get the minimum number of digits ; When there does not exists any number ; Printing the digits from the most significant digit ; Trying all combinations ; checking conditions for minimum digits ; Driver Code ; Function call to print the smallest number"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static int dp [ ] [ ] = new int [ 901 ] [ 8101 ] ; static int minimumNumberOfDigits ( int a , int b ) { if ( a > b a < 0 b < 0 a > 900 b > 8100 ) { return - 1 ; } if ( a == 0 && b == 0 ) { return 0 ; } if ( dp [ a ] [ b ] != - 1 ) { return dp [ a ] [ b ] ; } int ans = 101 ; for ( int i = 9 ; i >= 1 ; i -- ) { int k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) ; if ( k != - 1 ) { ans = Math . min ( ans , k + 1 ) ; } } return dp [ a ] [ b ] = ans ; } static void printSmallestNumber ( int a , int b ) { for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } dp [ 0 ] [ 0 ] = 0 ; int k = minimumNumberOfDigits ( a , b ) ; if ( k == - 1 k > 100 ) { System . out . println ( " - 1" ) ; } else { while ( a > 0 && b > 0 ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( a >= i && b >= i * i && 1 + dp [ a - i ] [ b - i * i ] == dp [ a ] [ b ] ) { System . out . print ( i ) ; a -= i ; b -= i * i ; break ; } } } } } public static void main ( String args [ ] ) { int a = 18 , b = 162 ; printSmallestNumber ( a , b ) ; } }


"Smallest number with sum of digits as N and divisible by 10 ^ N | Java program to find smallest number to find smallest number with N as sum of digits and divisible by 10 ^ N . ; If N = 0 the string will be 0 ; If n is not perfectly divisible by 9 output the remainder ; Print 9 N / 9 times ; Append N zero 's to the number so  as to make it divisible by 10^N ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void digitsNum ( int N ) { if ( N == 0 ) System . out . println ( "0" ) ; if ( N % 9 != 0 ) System . out . print ( ( N % 9 ) ) ; for ( int i = 1 ; i <= ( N / 9 ) ; ++ i ) System . out . print ( "9" ) ; for ( int i = 1 ; i <= N ; ++ i ) System . out . print ( "0" ) ; System . out . print ( " " ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( " The ▁ number ▁ is ▁ : ▁ " ) ; digitsNum ( N ) ; } }


"Smallest occurring element in each subsequence | Java program for the above approach ; Function that count the subsequence such that each element as the minimum element in the subsequence ; Store index in a map ; Sort the array ; To store count of subsequence ; Traverse the array ; Store count of subsequence ; Print the count of subsequence ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void solve ( int arr [ ] , int N ) { HashMap < Integer , Integer > M = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M . put ( i , arr [ i ] ) ; } Arrays . sort ( arr ) ; HashMap < Integer , Integer > Count = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Count . put ( arr [ i ] , ( int ) Math . pow ( 2 , N - i - 1 ) ) ; } for ( Map . Entry < Integer , Integer > m : M . entrySet ( ) ) { System . out . print ( Count . get ( m . getValue ( ) ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 1 } ; int N = arr . length ; solve ( arr , N ) ; } }


"Smallest odd digits number not less than N | Java program to print the smallest integer not less than N with all odd digits ; function to check if all digits are odd of a given number ; iterate for all digits ; if ( ( n % 10 ) % 2 == 0 ) if digit is even ; all digits are odd ; function to return the smallest number with all digits odd ; iterate till we find a number with all digits odd ; Driver Code"
How can the above be solved in Java?

class Geeks { static int check_digits ( int n ) { while ( n > 0 ) { return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) > 0 ) return i ; } public static void main ( String args [ ] ) { int N = 2397 ; System . out . println ( smallest_number ( N ) ) ; } }


"Smallest odd digits number not less than N | Java program to print the smallest integer not less than N with all odd digits ; function to return the smallest number with all digits odd ; convert the number to string to perform operations ; find out the first even number ; if no even numbers are there , than n is the answer ; add all digits till first even ; increase the even digit by 1 ; add 1 to the right of the even number ; Driver Code"
How can the above be solved in Java?

public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s . charAt ( i ) - '0' ) ; } num = num * 10 + ( s . charAt ( index ) - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallestNumber ( N ) ) ; } }


"Smallest odd number with N digits | Java implementation of the approach ; Function to return smallest odd with n digits ; Driver code"
How can the above be solved in Java?

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 ) ) ; } }


"Smallest odd number with even sum of digits from the given number N | Java program to find the smallest odd number with even sum of digits from the given number N ; Function to find the smallest odd number whose sum of digits is even from the given string ; Converting the given string to a list of digits ; An empty array to store the digits ; For loop to iterate through each digit ; If the given digit is odd then the digit is appended to the array b ; Sorting the list of digits ; If the size of the list is greater than 1 then a 2 digit smallest odd number is returned . Since the sum of two odd digits is always even ; Else , - 1 is returned ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static int smallest ( String s ) { int [ ] a = new int [ s . length ( ) ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ i ] = s . charAt ( i ) - '0' ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 != 0 ) b . add ( a [ i ] ) ; } Collections . sort ( b ) ; if ( b . size ( ) > 1 ) return ( b . get ( 0 ) ) * 10 + ( b . get ( 1 ) ) ; return - 1 ; } public static void main ( String [ ] args ) { System . out . print ( smallest ( "15470" ) ) ; } }


"Smallest of three integers without comparison operators | Java implementation of above approach ; Function to find minimum of x and y ; Function to find minimum of 3 numbers x , y and z ; Driver code"
How can the above be solved in Java?

class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( ( Integer . SIZE / 8 ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . min ( x , Math . min ( y , z ) ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . println ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }


"Smallest of three integers without comparison operators | Java program of above approach ; Using division operator to find minimum of three numbers ; Same as " if ▁ ( y ▁ < ▁ x ) " ; Driver code"
How can the above be solved in Java?

class GfG { static int smallest ( int x , int y , int z ) { if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ; return ( ( x / z ) != 1 ) ? x : z ; } public static void main ( String [ ] args ) { int x = 78 , y = 88 , z = 68 ; System . out . printf ( " Minimum ▁ of ▁ 3 ▁ numbers " + " ▁ is ▁ % d " , smallest ( x , y , z ) ) ; } }


"Smallest of three integers without comparison operators | Java program to find Smallest of three integers without comparison operators ; Driver code"
How can the above be solved in Java?

class GFG { static int smallest ( int x , int y , int z ) { int c = 0 ; while ( x != 0 && y != 0 && z != 0 ) { x -- ; y -- ; z -- ; c ++ ; } return c ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . printf ( " Minimum ▁ of ▁ 3" + " ▁ numbers ▁ is ▁ % d " , smallest ( x , y , z ) ) ; } }


"Smallest pair of indices with product of subarray co | Java program for the above approach ; Function to calculate GCD of two integers ; Recursively calculate GCD ; Function to find the lexicographically smallest pair of indices whose product is co - prime with the product of the subarrays on its left and right ; Stores the suffix product of array elements ; Set 0 / 1 if pair satisfies the given condition or not ; Initialize array right_prod [ ] ; Update the suffix product ; Stores product of all elements ; Stores the product of subarray in between the pair of indices ; Iterate through every pair of indices ( i , j ) ; Store product of A [ i , j ] ; Check if product is co - prime to product of either the left or right subarrays ; If no such pair is found , then print - 1 ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void findPair ( int A [ ] , int N ) { int right_prod [ ] = new int [ N ] ; int flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; int total_prod = right_prod [ 0 ] ; int product ; for ( int i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( int j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; System . out . println ( " ( " + ( i - 1 ) + " , ▁ " + ( j + 1 ) + " ) " ) ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 3 , 7 } ; int N = arr . length ; findPair ( arr , N ) ; } }


"Smallest pair of integers with minimum difference whose Bitwise XOR is N | Java program for the above approach ; Function to find the numbers A and B whose Bitwise XOR is N and the difference between them is minimum ; Find the MSB of the N ; Find the value of B ; Find the value of A ; Print the result ; Driver Code"
How can the above be solved in Java?

public class MyClass { static void findAandB ( int N ) { int K = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; int B = ( 1 << K ) ; int A = B ^ N ; System . out . println ( A + " ▁ " + B ) ; } public static void main ( String args [ ] ) { int N = 26 ; findAandB ( N ) ; } }


"Smallest perfect cube in an array | Java implementation of the approach ; Function that returns true if n is a perfect cube ; Takes the sqrt of the number ; Checks if it is a perfect cube number ; Function to return the smallest perfect cube from the array ; Stores the minimum of all the perfect cubes from the array ; Traverse all elements in the array ; Store the minimum if current element is a perfect cube ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean checkPerfectcube ( int n ) { int d = ( int ) Math . cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int smallestPerfectCube ( int a [ ] , int n ) { int mini = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . min ( a [ i ] , mini ) ; } } return mini ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = a . length ; System . out . print ( smallestPerfectCube ( a , n ) ) ; } }


"Smallest perfect power of 2 greater than n ( without using arithmetic operators ) | JAVA Code for Smallest perfect power of 2 greater than n ; Function to find smallest perfect power of 2 greater than n ; To store perfect power of 2 ; bitwise left shift by 1 ; bitwise right shift by 1 ; Required perfect power of 2 ; Driver program"
How can the above be solved in Java?

import java . util . * ; class GFG { static int perfectPowerOf2 ( int n ) { int per_pow = 1 ; while ( n > 0 ) { per_pow = per_pow << 1 ; n = n >> 1 ; } return per_pow ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( " Perfect ▁ power ▁ of ▁ 2 ▁ greater ▁ than ▁ " + n + " : ▁ " + perfectPowerOf2 ( n ) ) ; } }


"Smallest positive integer K such that all array elements can be made equal by incrementing or decrementing by at most K | Java program for the above approach ; Function to find smallest integer K such that incrementing or decrementing each element by K at most once makes all elements equal ; Store distinct array elements ; Traverse the array , A [ ] ; Count elements into the set ; If M is greater than 3 ; If M is equal to 3 ; Stores the first smallest element ; Stores the second smallest element ; Stores the largest element ; IF difference between B_2 and B_1 is equal to B_3 and B_2 ; If M is equal to 2 ; Stores the smallest element ; Stores the largest element ; If difference is an even ; If M is equal to 1 ; Driver code ; Given array ; Given size ; Print the required smallest integer"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void findMinKToMakeAllEqual ( int N , int A [ ] ) { Set < Integer > B = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { B . add ( A [ i ] ) ; } ArrayList < Integer > b = new ArrayList < Integer > ( B ) ; int M = b . size ( ) ; int i = 0 ; if ( M > 3 ) { System . out . print ( " - 1" ) ; } else if ( M == 3 ) { int B_1 = b . get ( i ++ ) ; int B_2 = b . get ( i ++ ) ; int B_3 = b . get ( i ++ ) ; if ( B_2 - B_1 == B_3 - B_2 ) { System . out . print ( B_2 - B_1 ) ; } else { System . out . print ( " - 1" ) ; } } else if ( M == 2 ) { int B_1 = b . get ( i ++ ) ; int B_2 = b . get ( i ++ ) ; if ( ( B_2 - B_1 ) % 2 == 0 ) { System . out . print ( ( B_2 - B_1 ) / 2 ) ; } else { System . out . print ( B_2 - B_1 ) ; } } else { System . out . print ( 0 ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 3 , 5 , 1 } ; int N = A . length ; findMinKToMakeAllEqual ( N , A ) ; } }


"Smallest positive integer X satisfying the given equation | Java Program to implement the above approach ; Function to find out the smallest positive integer for the equation ; Stores the minimum ; Iterate till K ; Check if n is divisible by i ; Return the answer ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findMinSoln ( int n , int k ) { int minSoln = Integer . MAX_VALUE ; for ( int i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } public static void main ( String [ ] args ) { int n = 4 , k = 6 ; System . out . println ( findMinSoln ( n , k ) ) ; } }


"Smallest positive integer that divides all array elements to generate quotients with sum not exceeding K | Java program to implement the above approach ; Function to find the smallest positive integer that divides array elements to get the sum <= K ; Stores minimum possible of the smallest positive integer satisfying the condition ; Stores maximum possible of the smallest positive integer satisfying the condition ; Apply binary search over the range [ left , right ] ; Stores middle element of left and right ; Stores sum of array elements ; Traverse the array ; Update sum ; If sum is greater than K ; Update left ; Update right ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findSmallestInteger ( int arr [ ] , int N , int K ) { int left = 1 ; int right = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( arr [ i ] + mid - 1 ) / mid ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 9 } ; int N = arr . length ; int K = 6 ; System . out . println ( findSmallestInteger ( arr , N , K ) ) ; } }


"Smallest positive integer that does not divide any elements of the given array | Java program for the above approach ; Function to find the smallest number which doesn 't divides any integer in the given array arr[] ; Traverse the array arr [ ] ; Maximum array element ; Initialize variable ; Traverse from 2 to max ; Stores if any such integer is found or not ; If any array element is divisible by j ; Smallest integer ; Print the answer ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static void smallestNumber ( int arr [ ] , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = Math . max ( maxi , arr [ i ] ) ; } int ans = - 1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { boolean flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 6 , 9 , 2 } ; int N = arr . length ; smallestNumber ( arr , N ) ; } }


"Smallest positive number made up of non | Java program for the above approach ; Function to find smallest positive number made up of non - repeating digits whose sum of its digits is equal to n ; No such number exists ; Stores the required answer ; Store the digit at unit 's place ; Iterate until n > digit ; Push digit at the start of res ; Decrement n by digit ; Decrement digit by 1 ; Push the remaining number as the starting digit ; Print the required number ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void result ( int n ) { if ( n > 45 ) { System . out . print ( - 1 ) ; return ; } String res = " " ; int digit = 9 ; while ( n > digit ) { res = ( char ) ( '0' + digit ) + res ; n -= digit ; digit -= 1 ; } if ( n > 0 ) { res = ( char ) ( '0' + n ) + res ; } System . out . print ( res ) ; } public static void main ( String [ ] args ) { int N = 19 ; result ( N ) ; } }


"Smallest power of 2 consisting of N digits | Java Program of the above approach ; Function to return smallest power of 2 consisting of N digits ; calculate log2 N indirectly using log ( ) method ; Driver Code"
How can the above be solved in Java?

class GFG { static int smallestNum ( int n ) { double power = log2 ( 10 ) ; return ( int ) Math . ceil ( ( n - 1 ) * power ) ; } static double log2 ( int N ) { return ( Math . log ( N ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }


"Smallest power of 2 consisting of N digits | Java program to implement the above approach ; Function to return smallest power of 2 with N digits ; Iterate through all powers of 2 ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int smallestNum ( int n ) { int res = 1 ; for ( int i = 2 ; ; i *= 2 ) { int length = ( int ) ( Math . log10 ( i ) ) + 1 ; if ( length == n ) return ( int ) ( Math . log ( i ) / Math . log ( 2 ) ) ; } } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( smallestNum ( n ) ) ; } }


"Smallest prefix to be deleted such that remaining array can be rearranged to form a sorted array | Java program for above approach ; Function to find the minimum length of prefix required to be deleted ; Stores index to be returned ; Iterate until previous element is <= current index ; Decrementing index ; Return index ; Driver code ; Given arr [ ] ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . io . * ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinLength ( arr ) ) ; } }


"Smallest prime divisor of a number | Java program to count the number of subarrays that having 1 ; Function to find the smallest divisor ; if divisible by 2 ; iterate from 3 to sqrt ( n ) ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } public static void main ( String [ ] args ) { int n = 31 ; System . out . println ( smallestDivisor ( n ) ) ; } }


"Smallest prime number missing in an array | Java implementation of the above approach ; this store all prime number upto 10 ^ 5 this function find all prime ; use sieve to find prime ; if number is prime then store it in prime vector ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver code ; find smallest prime which is not present"
How can the above be solved in Java?

import java . util . * ; class GFG { static Vector < Integer > findPrime ( int MAX ) { boolean pm [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < pm . length ; i ++ ) pm [ i ] = true ; pm [ 0 ] = pm [ 1 ] = false ; for ( int i = 2 ; i <= MAX ; i ++ ) if ( pm [ i ] ) for ( int j = 2 * i ; j <= MAX ; j += i ) pm [ j ] = false ; Vector < Integer > prime = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . add ( i ) ; return prime ; } static int max_element ( int arr [ ] ) { int max = arr [ 0 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) max = Math . max ( max , arr [ i ] ) ; return max ; } static int findSmallest ( int arr [ ] , int n ) { int MAX = max_element ( arr ) ; Vector < Integer > prime = findPrime ( MAX ) ; Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) s . add ( arr [ i ] ) ; long ans = - 1 ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { if ( ! s . contains ( prime . get ( i ) ) ) { ans = ( prime . get ( i ) ) ; break ; } } return ( int ) ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 0 , 1 , 2 , 7 } ; int n = arr . length ; if ( findSmallest ( arr , n ) == - 1 ) System . out . print ( " No ▁ prime ▁ number ▁ missing " ) ; else System . out . print ( findSmallest ( arr , n ) ) ; } }


"Smallest root of the equation x ^ 2 + s ( x ) * x | Java program to find smallest value of root of an equation under given constraints . ; function to check if the sum of digits is equal to the summation assumed ; calculate the sum of digit ; function to find the largest root possible . ; iterate for all possible sum of digits . ; check if discriminent is a perfect square . ; check if discriminent is a perfect square and if it as perefect root of the equation ; function returns answer ; driver program t"
How can the above be solved in Java?

class GFG { static boolean check ( long a , long b ) { long c = 0 ; while ( a != 0 ) { c = c + a % 10 ; a = a / 10 ; } return ( c == b ) ; } static long root ( long n ) { boolean found = false ; long mx = ( long ) 1E18 ; for ( long i = 0 ; i <= 90 ; i ++ ) { long s = i * i + 4 * n ; long sq = ( long ) Math . sqrt ( s ) ; if ( sq * sq == s && check ( ( sq - i ) / 2 , i ) ) { found = true ; mx = Math . min ( mx , ( sq - i ) / 2 ) ; } } if ( found ) return mx ; else return - 1 ; } public static void main ( String [ ] args ) { long n = 110 ; System . out . println ( root ( n ) ) ; } }


"Smallest square formed with given rectangles | JavaProgram to find the area of the smallest square which can be formed with rectangles of given dimensions ; Recursive function to return gcd of a and b ; Everything divides 0 ; Base case ; a is greater ; Function to find the area of the smallest square ; the length or breadth or side cannot be negative ; LCM of length and breadth ; squaring to get the area ; Driver code"
How can the above be solved in Java?

class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int squarearea ( int l , int b ) { if ( l < 0 b < 0 ) return - 1 ; int n = ( l * b ) / gcd ( l , b ) ; return n * n ; } public static void main ( String [ ] args ) { int l = 6 , b = 4 ; System . out . println ( squarearea ( l , b ) ) ; } }


"Smallest string divisible by two given strings | Java program for above approach ; Function to calculate GCD of two numbers ; Function to calculate LCM of two numbers ; Function to find the smallest string which is divisible by strings S and T ; Store the length of both strings ; Store LCM of n and m ; Temporary strings to store concatenated strings ; Concatenate s1 ( l / n ) times ; Concatenate t1 ( l / m ) times ; If s1 and t1 are equal ; Otherwise , print - 1 ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } static void findSmallestString ( String s , String t ) { int n = s . length ( ) , m = t . length ( ) ; int l = lcm ( n , m ) ; String s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 . equals ( t1 ) ) { System . out . println ( s1 ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { String S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; } }


"Smallest string obtained by removing all occurrences of 01 and 11 from Binary String | Set 2 | Java program for the above approach ; Function to find the length of the smallest string possible by removing substrings "01" and "11" ; Stores the length of the smallest string ; Traverse the string S ; If st is greater than 0 and S [ i ] is '1' ; Delete the last character and decrement st by 1 ; Otherwise ; Increment st by 1 ; Return the answer in st ; Driver code ; Input ; Function call"
How can the above be solved in Java?

public class GFG_JAVA { static int shortestString ( String S , int N ) { int st = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( st > 0 && S . charAt ( i ) == '1' ) { st -- ; } else { st ++ ; } } return st ; } public static void main ( String [ ] args ) { String S = "1010" ; int N = S . length ( ) ; System . out . println ( shortestString ( S , N ) ) ; } }


"Smallest string without any multiplication sign that represents the product of two given numbers | Java program for the above approach ; Function to find the string which evaluates to the product of A and B ; Stores the result ; 2 ^ log <= B && 2 ^ ( log + 1 ) > B ; Update res to res += A X 2 ^ log ; Update res to res += A X 2 ^ 0 ; Find the remainder ; If remainder is not equal to 0 ; Return the resultant string ; Function to find the minimum length representation of A * B ; Find representation of form A << k1 + A << k2 + ... + A << kn ; Find representation of form B << k1 + B << k2 + ... + B << kn ; Compare the length of the representations ; Driver Code ; Product A X B ; Function Call"
How can the above be solved in Java?

class GFG { public static String len ( long A , long B ) { String res = " " ; long log = 0 ; do { log = ( long ) ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( log != 0 ) { res += A + " < < " + log ; } else { res += A ; break ; } B = B - ( long ) Math . pow ( 2 , log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( log != 0 ) ; return res ; } public static void minimumString ( long A , long B ) { String res1 = len ( A , B ) ; String res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { System . out . println ( res2 ) ; } else { System . out . println ( res1 ) ; } } public static void main ( String args [ ] ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; } }


"Smallest subarray containing minimum and maximum values | Java implementation of above approach ; Function to return length of smallest subarray containing both maximum and minimum value ; find maximum and minimum values in the array ; iterate over the array and set answer to smallest difference between position of maximum and position of minimum value ; last occurrence of minValue ; last occurrence of maxValue ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minSubarray ( int A [ ] , int n ) { int minValue = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] < minValue ) minValue = A [ i ] ; } int maxValue = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > maxValue ) maxValue = A [ i ] ; } int pos_min = - 1 , pos_max = - 1 , ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . min ( ans , Math . abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = A . length ; System . out . println ( minSubarray ( A , n ) ) ; } }


"Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Java program for the above approach ; Function to find the smallest subarray sum greater than or equal to target ; Base Case ; If sum of the array is less than target ; If target is equal to the sum of the array ; Required condition ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static int smallSumSubset ( List < Integer > data , int target , int maxVal ) { int sum = 0 ; for ( Integer i : data ) sum += i ; if ( target <= 0 ) return 0 ; else if ( sum < target ) return maxVal ; else if ( sum == target ) return data . size ( ) ; else if ( data . get ( 0 ) >= target ) return 1 ; else if ( data . get ( 0 ) < target ) { List < Integer > temp = new ArrayList < > ( ) ; for ( int i = 1 ; i < data . size ( ) ; i ++ ) temp . add ( data . get ( i ) ) ; return Math . min ( smallSumSubset ( temp , target , maxVal ) , 1 + smallSumSubset ( temp , target - data . get ( 0 ) , maxVal ) ) ; } return - 1 ; } public static void main ( String [ ] args ) { List < Integer > data = Arrays . asList ( 3 , 1 , 7 , 1 , 2 ) ; int target = 11 ; int val = smallSumSubset ( data , target , data . size ( ) + 1 ) ; if ( val > data . size ( ) ) System . out . println ( - 1 ) ; else System . out . println ( val ) ; } }


"Smallest subarray from a given Array with sum greater than or equal to K | Set 2 | Java program for the above approach ; Function to find the smallest subarray with sum greater than or equal target ; DP table to store the computed subproblems ; Initialize first column with 0 ; Initialize first row with 0 ; Check for invalid condition ; Fill up the dp table ; Print the minimum length ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minlt ( int [ ] arr , int target , int n ) { int [ ] [ ] dp = new int [ arr . length + 1 ] [ target + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; for ( int i = 0 ; i < arr . length + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 ] [ j ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= arr . length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , ( dp [ i ] [ j - arr [ i - 1 ] ] ) != Integer . MAX_VALUE ? ( dp [ i ] [ j - arr [ i - 1 ] ] + 1 ) : Integer . MAX_VALUE ) ; } } } if ( dp [ arr . length ] [ target ] == Integer . MAX_VALUE ) { return - 1 ; } else { return dp [ arr . length ] [ target ] ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . length ; System . out . print ( minlt ( arr , target , n ) ) ; } }


"Smallest subarray having an element with frequency greater than that of other elements | Java program for the above approach ; Function to find subarray ; If the array has only one element , then there is no answer . ; Array to store the last occurrences of the elements of the array . ; To maintain the length ; Variables to store start and end indices ; Check if element is occurring for the second time in the array ; Find distance between last and current index of the element . ; If the current distance is less then len update len and set ' start ' and ' end ' ; Set the last occurrence of current element to be ' i ' . ; If flag is equal to 0 , it means there is no answer . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static void FindSubarray ( int [ ] arr , int n ) { if ( n == 1 ) { System . out . println ( " No ▁ such ▁ subarray ! " ) ; } int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , - 1 ) ; vis [ arr [ 0 ] ] = 0 ; int len = Integer . MAX_VALUE , flag = 0 ; int start = 0 , end = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int t = arr [ i ] ; if ( vis [ t ] != - 1 ) { int distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) System . out . println ( " No ▁ such ▁ subarray ! " ) ; else { for ( int i = start ; i <= end ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 4 , 5 } ; int n = arr . length ; FindSubarray ( arr , n ) ; } }


"Smallest subarray which upon repetition gives the original array | Java program for the above approach ; Function to print the array ; Function to find the smallest subarray ; Corner Case ; Initialize the auxiliary subarray ; Push the first 2 elements into the subarray brr [ ] ; Iterate over the length of subarray ; If array can be divided into subarray of i equal length ; Check if on repeating the current subarray gives the original array or not ; Subarray found ; Add current element into subarray ; No subarray found ; Driver Code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printArray ( Vector < Integer > brr ) { for ( int it : brr ) { System . out . print ( it + " ▁ " ) ; } } static void RepeatingSubarray ( int arr [ ] , int N ) { if ( N < 2 ) { System . out . print ( " - 1" ) ; } Vector < Integer > brr = new Vector < Integer > ( ) ; brr . add ( arr [ 0 ] ) ; brr . add ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { boolean a = false ; int n = brr . size ( ) ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr . get ( K ) ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . add ( arr [ i ] ) ; } System . out . print ( " - 1" ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = arr . length ; RepeatingSubarray ( arr , N ) ; } }


"Smallest subarray whose product leaves remainder K when divided by size of the array | Java program to implement the above approach ; Function to find the subarray of minimum length ; Initialize the minimum subarray size to N + 1 ; Generate all possible subarray ; Initialize the product ; Find the product ; Return the minimum size of subarray ; Driver code ; Given array"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findsubArray ( int arr [ ] , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 } ; int N = arr . length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) System . out . println ( answer ) ; else System . out . println ( " - 1" ) ; } }


"Smallest subarray with GCD as 1 | Segment Tree | Java implementation of the approach ; Array to store segment - tree ; Function to build segment - tree to answer range GCD queries ; Base - case ; Mid element of the range ; Merging the result of left and right sub - tree ; Function to perform range GCD queries ; Base - cases ; Mid - element ; Calling left and right child ; Function to find the required length ; Building the segment tree ; Two pointer variables ; To store the final answer ; Looping ; Incrementing j till we don 't get  a gcd value of 1 ; Updating the final answer ; Incrementing i ; Updating j ; Returning the final answer ; Driver code"
How can the above be solved in Java?

class GFG { static int maxLen = 30 ; static int [ ] seg = new int [ 3 * maxLen ] ; static int build ( int l , int r , int in , int [ ] arr ) { if ( l == r ) return seg [ in ] = arr [ l ] ; int mid = ( l + r ) / 2 ; return seg [ in ] = __gcd ( build ( l , mid , 2 * in + 1 , arr ) , build ( mid + 1 , r , 2 * in + 2 , arr ) ) ; } static int query ( int l , int r , int l1 , int r1 , int in ) { if ( l1 <= l && r <= r1 ) return seg [ in ] ; if ( l > r1 r < l1 ) return 0 ; int mid = ( l + r ) / 2 ; return __gcd ( query ( l , mid , l1 , r1 , 2 * in + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * in + 2 ) ) ; } static int findLen ( int [ ] arr , int n ) { build ( 0 , n - 1 , 0 , arr ) ; int i = 0 , j = 0 ; int ans = Integer . MAX_VALUE ; while ( i < n ) { while ( j < n && query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = Math . min ( ( j - i + 1 ) , ans ) ; i ++ ; j = Math . max ( j , i ) ; } if ( ans == Integer . MAX_VALUE ) return - 1 ; else return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 } ; int n = arr . length ; System . out . println ( findLen ( arr , n ) ) ; } }


"Smallest subarray with all occurrences of a most frequent element | Java implementation to find smallest subarray with all occurrences of a most frequent element ; To store left most occurrence of elements ; To store counts of elements ; To store maximum frequency ; To store length and starting index of smallest result window ; First occurrence of an element , store the index ; increase the frequency of elements ; Find maximum repeated element and store its last occurrence and first occurrence ; length of subsegment ; select subsegment of smallest size ; Print the subsegment with all occurrences of a most frequent element ; Driver program"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GfG { static void smallestSubsegment ( int a [ ] , int n ) { HashMap < Integer , Integer > left = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; int mx = 0 ; int mn = - 1 , strindex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count . get ( x ) == null ) { left . put ( x , i ) ; count . put ( x , 1 ) ; } else count . put ( x , count . get ( x ) + 1 ) ; if ( count . get ( x ) > mx ) { mx = count . get ( x ) ; mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } else if ( ( count . get ( x ) == mx ) && ( i - left . get ( x ) + 1 < mn ) ) { mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 2 , 1 } ; int n = A . length ; smallestSubsegment ( A , n ) ; } }


"Smallest subarray with k distinct numbers | Java program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Consider every element as starting point . ; Find the smallest window starting with arr [ i ] and containing exactly k distinct elements . ; There are less than k distinct elements now , so no need to continue . ; If there was no window with k distinct elements ( k is greater than total distinct elements ) ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }


"Smallest subarray with k distinct numbers | Java program to find minimum range that contains exactly k distinct numbers . ; Prints the minimum range that contains exactly k distinct numbers . ; Initially left and right side is - 1 and - 1 , number of distinct elements are zero and range is n . ; Initialize right side ; Increment right side . ; If number of distinct elements less than k . ; If distinct elements are equal to k and length is less than previous length . ; If number of distinct elements less than k , then break . ; If distinct elements equals to k then try to increment left side . ; Increment left side . ; It is same as explained in above loop . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size ( ) < k ) hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . size ( ) == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size ( ) < k ) break ; while ( hm . size ( ) == k ) { if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; i ++ ; if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }


"Smallest subarray with sum greater than a given value |  ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize length of smallest subarray as n + 1 ; Pick every element as starting point ; Initialize sum starting with current start ; If first element itself is greater ; Try different ending points for curremt start ; add last element to current sum ; If sum becomes more than x and length of this subarray is smaller than current smallest length , update the smallest length ( or result ) ; Driver program to test above functions"
How can the above be solved in Java?

class SmallestSubArraySum { static int smallestSubWithSum ( int arr [ ] , int n , int x ) { int min_len = n + 1 ; for ( int start = 0 ; start < n ; start ++ ) { int curr_sum = arr [ start ] ; if ( curr_sum > x ) return 1 ; for ( int end = start + 1 ; end < n ; end ++ ) { curr_sum += arr [ end ] ; if ( curr_sum > x && ( end - start + 1 ) < min_len ) min_len = ( end - start + 1 ) ; } } return min_len ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res1 ) ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res2 ) ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res3 ) ; } }


"Smallest subarray with sum greater than a given value | O ( n ) solution for finding smallest subarray with sum greater than x ; Returns length of smallest subarray with sum greater than x . If there is no subarray with given sum , then returns n + 1 ; Initialize current sum and minimum length ; Initialize starting and ending indexes ; Keep adding array elements while current sum is smaller than or equal to x ; If current sum becomes greater than x . ; Update minimum length if needed ; remove starting elements ; Driver program to test above functions"
How can the above be solved in Java?

class SmallestSubArraySum { static int smallestSubWithSum ( int arr [ ] , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res1 ) ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res2 ) ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res3 ) ; } }


"Smallest submatrix required to be removed such that sum of the remaining matrix is divisible by K | Java program to implement the above approach ; Function to find the length of the smallest subarray to be removed such that sum of elements is equal to S % K ; Remainder when total_sum is divided by K ; Stores curr_remainder and the most recent index at which curr_remainder has occured ; Stores required answer ; Add current element to curr_sum and take mod ; Update current remainder index ; If mod already exists in map the subarray exists ; Update res ; If not possible ; Return the result ; Function to find the smallest submatrix rqured to be deleted to make the sum of the matrix divisible by K ; Stores the sum of element of the matrix ; Traverse the matrix mat [ ] [ ] ; Update S ; Stores smallest area need to be deleted to get sum divisible by K ; Stores leftmost column of each matrix ; Stores rightmost column of each matrix ; Stores number of coulmns deleted of a matrix ; Store area of the deleted matrix ; prefixRowSum [ i ] : Store sum of sub matrix whose topmost left and bottommost right position is ( 0 , left ) ( i , right ) ; Iterate over all possible values of ( left , right ) ; Initialize all possible values of prefixRowSum [ ] to 0 ; Traverse each row from left to right column ; Update row_sum [ i ] ; ; Update width ; If no submatrix of the length ( right - left + 1 ) found to get the required output ; Update area ; If area is less than min_area ; Update min_area ; Driver Code ; Stores number of rows in the matrix ; Stores number of column in the matrix"
How can the above be solved in Java?

import java . util . * ; class GFG { static int removeSmallestSubarray ( int arr [ ] , int S , int n , int k ) { int target_remainder = S % k ; HashMap < Integer , Integer > map1 = new HashMap < > ( ) ; map1 . put ( 0 , - 1 ) ; int curr_remainder = 0 ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 . put ( curr_remainder , i ) ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . containsKey ( mod ) ) { res = Math . min ( res , i - map1 . get ( mod ) ) ; } } if ( res == Integer . MAX_VALUE res == n ) { res = - 1 ; } return res ; } static int smstSubmatDeleted ( int [ ] [ ] mat , int N , int M , int K ) { int S = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) S += mat [ i ] [ j ] ; } int min_area = N * M ; int left = 0 ; int right = 0 ; int width ; int area ; int [ ] prefixRowSum = new int [ N ] ; for ( left = 0 ; left < M ; left ++ ) { Arrays . fill ( prefixRowSum , 0 ) ; for ( right = left ; right < M ; right ++ ) { for ( int i = 0 ; i < N ; i ++ ) { prefixRowSum [ i ] += mat [ i ] [ right ] ; } width = removeSmallestSubarray ( prefixRowSum , S , N , K ) ; if ( width != - 1 ) { area = ( right - left + 1 ) * ( width ) ; if ( area < min_area ) { min_area = area ; } } } } return min_area ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 6 , 2 , 6 } , { 3 , 2 , 8 } , { 2 , 5 , 3 } } ; int K = 3 ; int N = mat . length ; int M = mat [ 0 ] . length ; System . out . print ( smstSubmatDeleted ( mat , N , M , K ) ) ; } }


"Smallest submatrix with Kth maximum XOR | Java Program for above approach ; Function to print smallest index of Kth maximum Xor value of submatrices ; Dimensions of matrix ; Stores XOR values for every index ; Min heap to find the kth maximum XOR value ; Stores indices for corresponding XOR values ; Traversing matrix to calculate XOR values ; Insert calculated value in Min Heap ; If size exceeds k ; Remove the minimum ; Store smallest index containing xor [ i ] [ j ] ; Stores the kth maximum element ; Print the required index ; Driver Code ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static void smallestPosition ( int m [ ] [ ] , int k ) { int n = m . length ; int mm = m [ 0 ] . length ; int [ ] [ ] xor = new int [ n ] [ mm ] ; PriorityQueue < Integer > minHeap = new PriorityQueue < > ( ) ; Map < Integer , int [ ] > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xor [ i - 1 ] [ j ] : 0 ; int b = j - 1 >= 0 ? xor [ i ] [ j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xor [ i - 1 ] [ j - 1 ] : 0 ; xor [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c ; minHeap . add ( xor [ i ] [ j ] ) ; if ( minHeap . size ( ) > k ) { minHeap . poll ( ) ; } if ( ! map . containsKey ( xor [ i ] [ j ] ) ) map . put ( xor [ i ] [ j ] , new int [ ] { i , j } ) ; } } int kth_max_e = minHeap . poll ( ) ; System . out . println ( ( map . get ( kth_max_e ) [ 0 ] + 1 ) + " ▁ " + ( map . get ( kth_max_e ) [ 1 ] + 1 ) ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k ) ; } }


"Smallest subsequence having GCD equal to GCD of given array | Java program to implement the above approach ; Function to calculate gcd of two numbers ; Function to print the smallest subsequence that satisfies the condition ; Stores gcd of the array . ; Traverse the given array ; Update gcdArr ; Traverse the given array . ; If current element equal to gcd of array . ; Generate all possible pairs . ; If gcd of current pair equal to gcdArr ; Print current pair of the array ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSmallSub ( int [ ] arr , int N ) { int gcdArr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { gcdArr = gcd ( gcdArr , arr [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == gcdArr ) { System . out . print ( arr [ i ] + " ▁ " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( gcd ( arr [ i ] , arr [ j ] ) == gcdArr ) { System . out . print ( arr [ i ] + " ▁ " + arr [ j ] ) ; return ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 12 } ; int N = arr . length ; printSmallSub ( arr , N ) ; } }


"Smallest subsequence with sum of absolute difference of consecutive elements maximized | Java program to find smallest subsequence with sum of absolute difference of consecutive elements maximized ; Function to print the smallest subsequence and its sum ; Final subsequence ; First element is a default endpoint ; Iterating through the array ; Check for monotonically increasing endpoint ; Check for monotonically decreasing endpoint ; Last element is a default endpoint ; Length of final subsequence ; Print the subsequence ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { Vector < Integer > req = new Vector < Integer > ( ) ; req . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . add ( arr [ i ] ) ; } req . add ( arr [ n - 1 ] ) ; System . out . print ( req . size ( ) + "
"); for ( int x : req ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . length ; getSubsequence ( arr , n ) ; } }


"Smallest subset of maximum sum possible by splitting array into two subsets | Java program for above approach ; Function to split array elements into two subsets having sum of the smaller subset maximized ; Stores the size of the array ; Stores the frequency of array elements ; Stores the total sum of the array ; Stores the sum of the resultant set ; Stores if it is possible to split the array that satisfies the conditions ; Stores the elements of the first subset ; Traverse the array arr [ ] ; Increment total sum ; Increment count of arr [ i ] ; Sort the array arr [ ] ; Stores the index of the last element of the array ; Traverse the array arr [ ] ; Stores the frequency of arr [ i ] ; If frq + ans . size ( ) is at most remaining size ; Append arr [ i ] to ans ; Decrement totSum by arr [ i ] ; Increment s by arr [ i ] ; Otherwise , decrement i by frq ; If s is greater than totSum ; Mark flag 1 ; If flag is equal to 1 ; Print the arrList ans ; Otherwise , print " - 1" ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findSubset ( int [ ] arr ) { int N = arr . length ; Map < Integer , Integer > map = new HashMap < > ( ) ; int totSum = 0 ; int s = 0 ; int flag = 0 ; ArrayList < Integer > ans = new ArrayList < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { totSum += arr [ i ] ; map . put ( arr [ i ] , map . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } Arrays . sort ( arr ) ; int i = N - 1 ; while ( i >= 0 ) { int frq = map . get ( arr [ i ] ) ; if ( ( frq + ans . size ( ) ) < ( N - ( frq + ans . size ( ) ) ) ) { for ( int k = 0 ; k < frq ; k ++ ) { ans . add ( arr [ i ] ) ; totSum -= arr [ i ] ; s += arr [ i ] ; i -- ; } } else { i -= frq ; } if ( s > totSum ) { flag = 1 ; break ; } } if ( flag == 1 ) { for ( i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 2 , 4 , 1 , 2 } ; findSubset ( arr ) ; } }