"Nth positive number whose absolute difference of adjacent digits is at most 1 | Java program to find Nth number with absolute difference between all adjacent digits at most 1. ; Return Nth number with absolute difference between all adjacent digits at most 1. ; To store all such numbers ; Enqueue all integers from 1 to 9 in increasing order . ; Perform the operation N times so that we can get all such N numbers . ; Store the front element of queue , in array and pop it from queue . ; If the last digit of dequeued integer is not 0 , then enqueue the next such number . ; Enqueue the next such number ; If the last digit of dequeued integer is not 9 , then enqueue the next such number . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findNthNumber ( int N ) { int [ ] arr = new int [ N + 1 ] ; Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) q . add ( i ) ; for ( int i = 1 ; i <= N ; i ++ ) { arr [ i ] = q . peek ( ) ; q . remove ( ) ; if ( arr [ i ] % 10 != 0 ) q . add ( arr [ i ] * 10 + arr [ i ] % 10 - 1 ) ; q . add ( arr [ i ] * 10 + arr [ i ] % 10 ) ; if ( arr [ i ] % 10 != 9 ) q . add ( arr [ i ] * 10 + arr [ i ] % 10 + 1 ) ; } System . out . println ( arr [ N ] ) ; } public static void main ( String [ ] args ) { int N = 21 ; findNthNumber ( N ) ; } }


"Nth root of a number using log | Java implementation to find the Kth root of a number using log ; Function to find the Kth root of the number using log function ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double kthRoot ( double n , int k ) { return Math . pow ( k , ( ( 1.0 / k ) * ( Math . log ( n ) / Math . log ( k ) ) ) ) ; } public static void main ( String args [ ] ) { double n = 81 ; int k = 4 ; System . out . printf ( " % .6f " , kthRoot ( n , k ) ) ; } }


"Nth term of Ruler Function Series | Java program for the above approach ; Function to count the number of set bits in the number N ; Store the number of setbits ; Update the value of n ; Update the count ; Return the total count ; Function to find the Nth term of the Ruler Function Series ; Store the result ; Print the result ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; System . out . println ( x ) ; } public static void main ( String [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }


"Nth term of a Custom Fibonacci series | Java implementation of the Custom Fibonacci series ; Function to return the nth term of the required sequence ; Driver code"
How can the above be solved in Java?

class GFG { static int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } public static void main ( String [ ] args ) { int a = 10 , b = 17 , n = 3 ; System . out . println ( nth_term ( a , b , n ) ) ; } }


"Nth term of a recurrence relation generated by two given arrays | Java program for the above approach ; Declare T [ ] [ ] as global matrix ; Result matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to multiply two matrices ; Create an auxiliary matrix to store elements of the multiplication matrix ; Iterate over range [ 0 , K ] ; Update temp [ i ] [ j ] ; Update the final matrix ; Function to calculate matrix ^ n using binary exponentaion ; Initialize result matrix and unity matrix ; Function to calculate nth term of general recurrence ; Fill T [ ] [ ] with appropriate value ; Function Call to calculate T ^ n ; Calculate nth term as first element of F * ( T ^ n ) ; Print the result ; Driver Code ; Given Initial terms ; Given coefficients ; Given K ; Given N ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ ] [ ] T = new int [ 2000 ] [ 2000 ] ; static int [ ] [ ] result = new int [ 2000 ] [ 2000 ] ; static void mul_2 ( int K ) { int [ ] [ ] temp = new int [ K + 1 ] [ K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( T [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { T [ i ] [ j ] = temp [ i ] [ j ] ; } } } static void mul_1 ( int K ) { int [ ] [ ] temp = new int [ K + 1 ] [ K + 1 ] ; for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { for ( int k = 1 ; k <= K ; k ++ ) { temp [ i ] [ j ] = ( temp [ i ] [ j ] + ( result [ i ] [ k ] * T [ k ] [ j ] ) % mod ) % mod ; } } } for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { result [ i ] [ j ] = temp [ i ] [ j ] ; } } } static void matrix_pow ( int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) { for ( int j = 1 ; j <= K ; j ++ ) { if ( i == j ) result [ i ] [ j ] = 1 ; } } while ( n > 0 ) { if ( n % 2 == 1 ) mul_1 ( K ) ; mul_2 ( K ) ; n /= 2 ; } } static int NthTerm ( int F [ ] , int C [ ] , int K , int n ) { for ( int i = 1 ; i <= K ; i ++ ) T [ i ] [ K ] = C [ K - i ] ; for ( int i = 1 ; i <= K ; i ++ ) T [ i + 1 ] [ i ] = 1 ; matrix_pow ( K , n ) ; int answer = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { answer += F [ i - 1 ] * result [ i ] [ 1 ] ; } System . out . print ( answer + "
"); return 0 ; } public static void main ( String [ ] args ) { int F [ ] = { 1 , 2 , 3 } ; int C [ ] = { 1 , 1 , 1 } ; int K = 3 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }


"Nth term of a recurrence relation generated by two given arrays | Java program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver Code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static double mod = 1e9 + 7 ; static void NthTerm ( int F [ ] , int C [ ] , int K , int n ) { int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } System . out . println ( ans [ n ] ) ; } public static void main ( String [ ] args ) { int F [ ] = { 0 , 1 } ; int C [ ] = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }


"Nth term of a sequence formed by sum of current term with product of its largest and smallest digit | Java program for the above approach . ; Function to find integer ; Because 1 st integer is K itself ; Initialize min_d and max_d ; Updating min_d and max_d ; Break if min digit is 0 ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int find ( int K , int N ) { N -- ; while ( N -- != 0 ) { int curr_term = K ; int min_d = 9 ; int max_d = 0 ; while ( curr_term > 0 ) { int r = curr_term % 10 ; min_d = Math . min ( min_d , r ) ; max_d = Math . max ( max_d , r ) ; curr_term = curr_term / 10 ; } if ( min_d == 0 ) { break ; } K = K + min_d * max_d ; } return K ; } public static void main ( String [ ] args ) { int K = 487 ; int N = 2 ; System . out . print ( find ( K , N ) + "
"); } }


"Nth term where K + 1 th term is product of Kth term with difference of max and min digit of Kth term | Java implementation to find the value of the given function for the value ; Function to find minimum digit in the decimal representation of N ; Loop to find the minimum digit in the number ; Function to find maximum digit in the decimal representation of N ; Loop to find the maximum digit in the number ; Function to find the value of the given function ; Loop to compute the values of the given number ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static int MIN ( int n ) { int ans = 11 ; while ( n > 0 ) { ans = Math . min ( ans , n % 10 ) ; n /= 10 ; } return ans ; } static int MAX ( int n ) { int ans = - 1 ; while ( n > 0 ) { ans = Math . max ( ans , n % 10 ) ; n /= 10 ; } return ans ; } static void Find_value ( int n , int k ) { k -- ; int x = 0 ; int y = 0 ; while ( k -- > 0 ) { x = MIN ( n ) ; y = MAX ( n ) ; if ( y - x == 0 ) break ; n *= ( y - x ) ; } System . out . print ( n ) ; } public static void main ( String [ ] args ) { int N = 487 , D = 5 ; Find_value ( N , D ) ; } }


"Null Cipher | Java program to decode NULL CIPHER ; Function to decode the message . ; Store the decoded string ; found variable is used to tell that the encoded encoded character is found in that particular word . ; Set found variable to false whenever we find whitespace , meaning that encoded character for new word is not found ; Driver Code"
How can the above be solved in Java?

class GFG { static String decode ( String str ) { String res = " " ; boolean found = false ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) { found = false ; continue ; } if ( ! found ) { if ( ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) || ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) ) { res += Character . toString ( str . charAt ( i ) ) ; found = true ; } } } return res . toLowerCase ( ) ; } public static void main ( String [ ] args ) { String in ; in = " A ▁ Step ▁ by ▁ Step ▁ Guide ▁ for ▁ Placement ▁ Preparation ▁ by ▁ GeeksforGeeks " ; System . out . println ( " Enciphered ▁ Message : ▁ " + decode ( in ) ) ; } }


"Number Theory | Generators of finite cyclic group under addition | A simple Java program to find all generators ; Function to return gcd of a and b ; Print generators of n ; 1 is always a generator ; A number x is generator of GCD is 1 ; Driver program to test above function"
How can the above be solved in Java?

class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printGenerators ( int n ) { System . out . println ( "1 ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) if ( gcd ( i , n ) == 1 ) System . out . println ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; printGenerators ( n ) ; } }


"Number expressed as sum of five consecutive integers | Java Program to check if a number can be expressed as sum of five consecutive integer . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is multiple of 5 ; else print " - 1" . ; Driver Program"
How can the above be solved in Java?

import java . io . * ; class GFG { static void checksum ( int n ) { if ( n % 5 == 0 ) System . out . println ( ( n / 5 - 2 ) + " ▁ " + ( n / 5 - 1 ) + " ▁ " + ( n / 5 ) + " ▁ " + ( n / 5 + 1 ) + " ▁ " + ( n / 5 + 2 ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; checksum ( n ) ; } }


"Number expressed as sum of five consecutive integers | Java Program to check if a number can be expressed as sum of five consecutive integers . ; function to check if a number can be expressed as sum of five consecutive integers . ; if n is 0 ; if n is positive , increment loop by 1. ; if n is negative , decrement loop by 1. ; Running loop from 0 to n - 4 ; check if sum of five consecutive integer is equal to n . ; Driver Program"
How can the above be solved in Java?

import java . io . * ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { System . out . println ( " - 2 ▁ - 1 ▁ 0 ▁ 1 ▁ 2" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { System . out . print ( ( i ) + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) + " ▁ " + ( i + 3 ) + " ▁ " + ( i + 4 ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; checksum ( n ) ; } }


"Number formed after K times repeated addition of smallest divisor of N | Java program to find the Kth number formed after repeated addition of smallest divisor of N ; If N is even ; If N is odd ; Add smallest divisor to N ; Updated N is even ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void FindValue ( int N , int K ) { if ( N % 2 == 0 ) { N = N + 2 * K ; } else { int i ; for ( i = 2 ; i < Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) break ; } N = N + i ; N = N + 2 * ( K - 1 ) ; } System . out . print ( N ) ; } public static void main ( String args [ ] ) { int N = 9 ; int K = 4 ; FindValue ( N , K ) ; } }


"Number formed by adding product of its max and min digit K times | Java program for the above approach ; Function to find the formed number ; K -- ; M ( 1 ) = N ; Check if minimum digit is 0 ; Function that returns the product of maximum and minimum digit of N number . ; Finds the last digit . ; Moves to next digit ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static int formed_no ( int N , int K ) { if ( K == 1 ) { return N ; } int answer = N ; while ( K != 0 ) { int a_current = prod_of_max_min ( answer ) ; if ( a_current == 0 ) break ; answer += a_current ; } return answer ; } static int prod_of_max_min ( int n ) { int largest = 0 ; int smallest = 10 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } return largest * smallest ; } public static void main ( String [ ] args ) { int N = 487 , K = 100000000 ; System . out . println ( formed_no ( N , K ) ) ; } }


"Number formed by deleting digits such that sum of the digits becomes even and the number odd | Java implementation to convert a number into odd number such that digit - sum is odd ; Function to convert a number into odd number such that digit - sum is odd ; Loop to find any first two odd number such that their sum is even and number is odd ; Print the result ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class Main { static void converthenumber ( int n ) { String s = Integer . toString ( n ) ; String res = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' || s . charAt ( i ) == '9' ) res += s . charAt ( i ) ; if ( res . length ( ) == 2 ) break ; } if ( res . length ( ) == 2 ) System . out . println ( res ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 18720 ; converthenumber ( n ) ; } }


"Number formed by flipping all bits to the left of rightmost set bit | Java program to find the integer formed after flipping all bits to the left of the rightmost set bit ; Function to get the total count ; Moving until we get the rightmost set bit ; To get total number of bits in a number ; Function to find the integer formed after flipping all bits to the left of the rightmost set bit ; Find the total count of bits and the rightmost set bit ; XOR given number with the number which has is made up of only totbits set ; To avoid flipping the bits to the right of the set bit , take XOR with the number made up of only set firstbits ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int totCount ; static int firstCount ; static void getTotCount ( int num ) { totCount = 1 ; firstCount = 1 ; int temp = 1 ; while ( ( num & temp ) == 0 ) { temp = temp << 1 ; totCount += 1 ; } firstCount = totCount ; temp = num >> totCount ; while ( temp != 0 ) { totCount += 1 ; temp = temp >> 1 ; } } static int flipBitsFromRightMostSetBit ( int num ) { getTotCount ( num ) ; int num1 = num ^ ( ( 1 << totCount ) - 1 ) ; num1 = num1 ^ ( ( 1 << firstCount ) - 1 ) ; return num1 ; } public static void main ( String [ ] args ) { int n = 120 ; System . out . println ( flipBitsFromRightMostSetBit ( n ) ) ; } }


"Number formed by the rightmost set bit in N | Java implementation of the approach ; Function to return the integer formed by taking the rightmost set bit in n ; n & ( n - 1 ) unsets the first set bit from the right in n ; Take xor with the original number The position of the ' changed ▁ bit ' will be set and rest will be unset ; Driver Code"
How can the above be solved in Java?

class geeks { public static int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( firstSetBit ( n ) ) ; } }


"Number from a range [ L , R ] having Kth minimum cost of conversion to 1 by given operations | Java implementation of the above approach ; Function to calculate the cost ; Base case ; Even condition ; Odd condition ; Return cost ; Function to find Kth element ; Array to store the costs ; Sort the array based on cost ; Driver code ; Given range and6 K ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int func ( int n ) { int count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } static void findKthElement ( int l , int r , int k ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; for ( int i = l ; i <= r ; i ++ ) arr . add ( i ) ; ArrayList < List < Integer > > result = new ArrayList < > ( ) ; for ( int i : arr ) result . add ( Arrays . asList ( i , func ( i ) ) ) ; Collections . sort ( result , ( s1 , s2 ) -> s1 . get ( 1 ) - s2 . get ( 1 ) ) ; System . out . println ( result . get ( k - 1 ) . get ( 0 ) ) ; } public static void main ( String [ ] args ) { int l = 12 ; int r = 15 ; int k = 2 ; findKthElement ( l , r , k ) ; } }


"Number is divisible by 29 or not | Java program to demonstrate above method to check divisibility by 29. ; Returns true if n is divisible by 29 else returns false . ; add the lastdigit * 3 to renaming number until number comes only 2 digit ; return true if number is divisible by 29 another ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isDivisible ( long n ) { while ( n / 100 > 0 ) { int last_digit = ( int ) n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } public static void main ( String [ ] args ) { long n = 348 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Number less than equals to N with maximum product of prime factors | Java program for the above approach ; Function to find the smallest number having a maximum product of prime factors ; Declare the array arr [ ] ; Initialise array with 1 ; Iterate from [ 2 , N ] ; If value at index i is 1 , then i is prime and make update array at index for all multiples of i ; Initialise maxValue ; Find number having maximum product of prime factor <= N ; Find the maximum value ; Driver Code ; Given number ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) arr [ i ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } int maxValue = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . print ( maxPrimefactorNum ( N ) ) ; } }


"Number obtained by reducing sum of digits of 2 N into a single digit | Java program for the above approach ; Function to find the number obtained by reducing sum of digits of 2 ^ N into a single digit ; Stores answers for different values of N ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findNumber ( int N ) { int [ ] ans = { 1 , 2 , 4 , 8 , 7 , 5 } ; return ans [ N % 6 ] ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( findNumber ( N ) ) ; } }


"Number of 0 s and 1 s at prime positions in the given array | Java implementation of the approach ; Function that returns true if n is prime ; Check from 2 to n ; Function to find the count of 0 s and 1 s at prime indices ; To store the count of 0 s and 1 s ; If current 0 is at prime position ; If current 1 is at prime position ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countPrimePosition ( int arr [ ] ) { int c0 = 0 , c1 = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } System . out . println ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; System . out . println ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }


"Number of Antisymmetric Relations on a set of N elements | Java program for the above approach ; Function to calculate the value of x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of antisymmetric relations in a set consisting of N elements ; Print the count of antisymmetric relations ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int antisymmetricRelation ( int N ) { return ( power ( 2 , N ) * power ( 3 , ( N * N - N ) / 2 ) ) % mod ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( antisymmetricRelation ( N ) ) ; } }


"Number of Asymmetric Relations on a set of N elements | Java program for the above approach ; Function to calculate x ^ y modulo ( 10 ^ 9 + 7 ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the final value of x ^ y ; Function to count the number of asymmetric relations in a set consisting of N elements ; Return the resultant count ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { final static int mod = 1000000007 ; public static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } public static int asymmetricRelation ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( asymmetricRelation ( N ) ) ; } }


"Number of Binary Search Trees of height H consisting of H + 1 nodes | Java program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the value of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count the number of of BSTs of height H consisting of ( H + 1 ) nodes ; Driver code"
How can the above be solved in Java?

class GFG { static int mod = 1000000007 ; static long power ( long x , int y ) { long res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static long CountBST ( int H ) { return power ( 2 , H ) ; } public static void main ( String [ ] args ) { int H = 2 ; System . out . print ( CountBST ( H ) ) ; } }


"Number of Co | Java program to count the pairs whose sum of digits is co - prime ; Function to find the elements after doing the sum of digits ; Traverse from a to b ; Find the sum of the digits of the elements in the given range one by one ; Function to count the co - prime pairs ; Function to make the pairs by doing the sum of digits ; Count pairs that are co - primes ; Driver code ; Function to count the pairs"
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 makePairs ( Vector < Integer > pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) pairs . add ( sumOfDigits ) ; } } static int countCoPrime ( int a , int b ) { Vector < Integer > pairs = new Vector < Integer > ( ) ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pairs . size ( ) ; j ++ ) if ( GCD ( pairs . get ( i ) , pairs . get ( j ) ) == 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int a = 12 , b = 15 ; System . out . println ( countCoPrime ( a , b ) ) ; } }


"Number of Co | Java program to count the pairs whose sum of digits is co - prime ; Recursive function to return the frequency of numbers having their sum of digits i ; Returns 1 or 0 ; Returns value of the dp if already stored ; Loop from digit 0 to 9 ; To change the tight to 1 ; Calling the recursive function to find the frequency ; Function to find out frequency of numbers from 1 to N having their sum of digits from 1 to 162 and store them in array ; Number to string conversion ; Calling the recursive function and pushing it into array ; Function to find the pairs ; Calling the formArray function of a - 1 numbers ; Calling the formArray function of b numbers ; Subtracting the frequency of higher number array with lower number array and thus finding the range of numbers from a to b having sum from 1 to 162 ; To find out total number of pairs which are co - prime ; Driver code ; Function to count the pairs"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long recursive ( long idx , long sum , long tight , String st , long [ ] [ ] [ ] dp , long num ) { if ( idx == num ) { return sum == 0 ? 1 : 0 ; } if ( dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] != - 1 ) return dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] ; long newTight ; long ans = 0 ; long d ; for ( d = 0 ; d < 10 ; ++ d ) { newTight = 0 ; if ( tight == 1 && st . charAt ( ( int ) idx ) - '0' < d ) continue ; if ( tight == 1 && st . charAt ( ( int ) idx ) - '0' == d ) newTight = 1 ; if ( sum >= d ) ans += recursive ( idx + 1 , sum - d , ( int ) newTight , st , dp , num ) ; } return dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] = ans ; } static ArrayList < Long > formArray ( long N ) { long [ ] [ ] [ ] dp = new long [ 20 ] [ 2 ] [ 166 ] ; for ( long [ ] [ ] innerRow : dp ) { for ( long [ ] innerInnerRow : innerRow ) { Arrays . fill ( innerInnerRow , - 1 ) ; } } String st = String . valueOf ( N ) ; long num = st . length ( ) ; ArrayList < Long > arr = new ArrayList < Long > ( ) ; for ( int i = 1 ; i <= 162 ; ++ i ) { arr . add ( recursive ( 0 , i , 1 , st , dp , num ) ) ; } return arr ; } static long findPair ( long a , long b ) { ArrayList < Long > arr_smaller = formArray ( a - 1 ) ; ArrayList < Long > arr_greater = formArray ( b ) ; for ( int i = 0 ; i < arr_greater . size ( ) ; ++ i ) { arr_greater . set ( i , arr_greater . get ( i ) - arr_smaller . get ( i ) ) ; } long ans = 0 ; for ( int i = 1 ; i <= 162 ; ++ i ) { for ( int j = i + 1 ; j <= 162 ; ++ j ) { if ( gcd ( i , j ) == 1 ) { ans = ( ans + arr_greater . get ( i - 1 ) * arr_greater . get ( j - 1 ) ) ; } } } return ans ; } public static void main ( String [ ] args ) { long a = 12 , b = 15 ; System . out . println ( findPair ( a , b ) ) ; } }


"Number of Counterclockwise shifts to make a string palindrome | Java program to find counter clockwise shifts to make string palindrome . ; Function to check if given string is palindrome or not . ; Function to find counter clockwise shifts to make string palindrome . ; Pointer to starting of current shifted string . ; Pointer to ending of current shifted string . ; Concatenate string with itself ; To store counterclockwise shifts ; Move left and right pointers one step at a time . ; Check if current shifted string is palindrome or not ; If string is not palindrome then increase count of number of shifts by 1. ; Driver code ."
How can the above be solved in Java?

class GFG { static boolean isPalindrome ( String str , int l , int r ) { while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } static int CyclicShifts ( String str ) { int n = str . length ( ) ; int left = 0 ; int right = n - 1 ; str = str + str ; int cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } public static void main ( String [ ] args ) { String str = " bccbbaab " ; System . out . println ( CyclicShifts ( str ) ) ; } }


"Number of Digits in a ^ b | Java Program to calculate no . of digits in a ^ b ; function to calculate number of digits in a ^ b ; driver program"
How can the above be solved in Java?

class GFG { static int no_of_digit ( int a , int b ) { return ( ( int ) ( b * Math . log10 ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 100 ; System . out . print ( " no . ▁ of ▁ digits ▁ = ▁ " + no_of_digit ( a , b ) ) ; } }


"Number of Distinct Meeting Points on a Circular Road | Java Program to find number of distinct point of meet on a circular road ; Returns the GCD of two number . ; Returns the number of distinct meeting points . ; Find the relative speed . ; convert the negative value to positive . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int gcd ( int a , int b ) { int c = a % b ; while ( c != 0 ) { a = b ; b = c ; c = a % b ; } return b ; } static int numberOfmeet ( int a , int b ) { int ans ; if ( a > b ) ans = a - b ; else ans = b - a ; if ( a < 0 ) a = a * ( - 1 ) ; if ( b < 0 ) b = b * ( - 1 ) ; return ans / gcd ( a , b ) ; } public static void main ( String [ ] args ) { int a = 1 , b = - 1 ; System . out . println ( numberOfmeet ( a , b ) ) ; } }


"Number of GP ( Geometric Progression ) subsequences of size 3 | Java program to count GP subsequences of size 3. ; Returns count of G . P . subsequneces with length 3 and common ratio r ; Hashing to maintain left and right array elements to the main count ; Stores the answer ; Traverse through the elements ; Keep the count in the hash ; Traverse through all elements and find out the number of elements as k1 * k2 ; Keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; If the current element is divisible by k , count elements in left hash . ; Decrease the count in right hash ; Number of right elements ; Calculate the answer ; left count of a [ i ] ; Returns answer ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static long subsequences ( int a [ ] , int n , int r ) { Map < Integer , Integer > left = new HashMap < > ( ) , right = new HashMap < > ( ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) right . put ( a [ i ] , right . getOrDefault ( a [ i ] , 0 ) + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) c1 = left . getOrDefault ( a [ i ] / r , 0 ) ; right . put ( a [ i ] , right . getOrDefault ( a [ i ] , 0 ) - 1 ) ; c2 = right . getOrDefault ( a [ i ] * r , 0 ) ; ans += c1 * c2 ; left . put ( a [ i ] , left . getOrDefault ( a [ i ] , 0 ) + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = a . length ; int r = 3 ; System . out . println ( subsequences ( a , n , r ) ) ; } }


"Number of GP ( Geometric Progression ) subsequences of size 3 | Java program to count GP subsequences of size 3. ; To calculate nCr DP approach ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Returns count of G . P . subsequences with length 3 and common ratio r ; Hashing to maintain left and right array elements to the main count ; Stores the answer ; Traverse through the elements ; IF RATIO IS ONE ; Traverse the count in hash ; Calculating nC3 , where ' n ' is the number of times each number is repeated in the input ; Traverse through all elements and find out the number of elements as k1 * k2 ; Keep the count of left and right elements left is a [ i ] / r and right a [ i ] * r ; If the current element is divisible by k , count elements in left hash . ; Decrease the count in right hash ; Number of right elements ; Calculate the answer ; left count of a [ i ] ; Returns answer ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static long subsequences ( int a [ ] , int n , int r ) { HashMap < Integer , Integer > left = new HashMap < > ( ) ; HashMap < Integer , Integer > right = new HashMap < > ( ) ; long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( right . containsKey ( a [ i ] ) ) { right . put ( a [ i ] , right . get ( a [ i ] ) + 1 ) ; } else { right . put ( a [ i ] , 1 ) ; } if ( r == 1 ) { for ( Map . Entry < Integer , Integer > i : right . entrySet ( ) ) { ans += binomialCoeff ( i . getValue ( ) , 3 ) ; } return ans ; } for ( int i = 0 ; i < n ; i ++ ) { long c1 = 0 , c2 = 0 ; if ( a [ i ] % r == 0 ) if ( left . containsKey ( a [ i ] / r ) ) c1 = left . get ( a [ i ] / r ) ; if ( right . containsKey ( a [ i ] ) ) { right . put ( a [ i ] , right . get ( a [ i ] ) - 1 ) ; } else { right . put ( a [ i ] , - 1 ) ; } if ( right . containsKey ( a [ i ] * r ) ) c2 = right . get ( a [ i ] * r ) ; ans += c1 * c2 ; if ( left . containsKey ( a [ i ] ) ) { left . put ( a [ i ] , left . get ( a [ i ] ) + 1 ) ; } else { left . put ( a [ i ] , 1 } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 2 , 3 , 6 , 9 , 18 , 3 , 9 } ; int n = a . length ; int r = 3 ; System . out . print ( subsequences ( a , n , r ) ) ; } }


"Number of Groups of Sizes Two Or Three Divisible By 3 | Program to find groups of 2 or 3 whose sum is divisible by 3 ; Initialize groups to 0 ; Increment group with specified remainder ; Return groups using the formula ; Driver code"
How can the above be solved in Java?


"Number of Integral Points between Two Points | Java code to find the number of integral points lying on the line joining the two given points ; Class to represent an Integral point on XY plane . ; Utility function to find GCD of two numbers GCD of a and b ; Finds the no . of Integral points between two given points . ; If line joining p and q is parallel to x axis , then count is difference of y values ; If line joining p and q is parallel to y axis , then count is difference of x values ; Driver program to test above"
How can the above be solved in Java?

class GFG { static class Point { int x , y ; Point ( int a , int b ) { this . x = a ; this . y = b ; } } ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getCount ( Point p , Point q ) { if ( p . x == q . x ) return Math . abs ( p . y - q . y ) - 1 ; if ( p . y == q . y ) return Math . abs ( p . x - q . x ) - 1 ; return gcd ( Math . abs ( p . x - q . x ) , Math . abs ( p . y - q . y ) ) - 1 ; } public static void main ( String [ ] args ) { Point p = new Point ( 1 , 9 ) ; Point q = new Point ( 8 , 16 ) ; System . out . println ( " The ▁ number ▁ of ▁ integral ▁ points ▁ between ▁ " + " ( " + p . x + " , ▁ " + p . y + " ) ▁ and ▁ ( " + q . x + " , ▁ " + q . y + " ) ▁ is ▁ " + getCount ( p , q ) ) ; } }


"Number of Irreflexive Relations on a Set | Java program for the above approach ; Function to calculate x ^ y modulo 1000000007 in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If x is divisible by mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the resultant value of x ^ y ; Function to count the number of irreflixive relations in a set consisting of N elements ; Return the resultant count ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int irreflexiveRelation ( int N ) { return power ( 2 , N * N - N ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( irreflexiveRelation ( N ) ) ; } }


"Number of K 's such that the given array can be divided into two sets satisfying the given conditions | Java implementation of the approach ; Function to return the count of K 's such that the array can be divided into two sets containing equal number of elements when all the elements less than K are in one set and the rest of the elements are in the other set ; Sort the given array ; Return number of such Ks ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int two_sets ( int a [ ] , int n ) { Arrays . sort ( a ) ; return a [ n / 2 ] - a [ ( n / 2 ) - 1 ] ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 4 , 6 , 7 , 9 } ; int n = a . length ; System . out . println ( two_sets ( a , n ) ) ; } }


"Number of K length subsequences with minimum sum | Java implementation of the approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Calculate value of Binomial Coefficient in bottom up manner ; Base Cases ; Calculate value using previously stored values ; Function to return the count of valid subsequences ; Sort the array ; Maximum among the minimum K elements ; Y will store the frequency of num in the minimum K elements ; cntX will store the frequency of num in the complete array ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int binomialCoeff ( int n , int k ) { int C [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static int cntSubSeq ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int num = arr [ k - 1 ] ; int Y = 0 ; for ( int i = k - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == num ) Y ++ ; } int cntX = Y ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i ] == num ) cntX ++ ; } return binomialCoeff ( cntX , Y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; System . out . println ( cntSubSeq ( arr , n , k ) ) ; } }


"Number of Larger Elements on right side in a string | Java program to count greater characters on right side of every character . ; Arrays to store result and character counts . ; start from right side of string ; Driver code"
How can the above be solved in Java?

public class GFG { final static int MAX_CHAR = 26 ; static void printGreaterCount ( String str ) { int len = str . length ( ) ; int ans [ ] = new int [ len ] , count [ ] = new int [ MAX_CHAR ] ; for ( int i = len - 1 ; i >= 0 ; i -- ) { count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int j = str . charAt ( i ) - ' a ' + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }


"Number of Larger Elements on right side in a string | Java program to find counts of right greater characters for every character . ; Driver code"
How can the above be solved in Java?

class GFG { static void printGreaterCount ( String str ) { int len = str . length ( ) , right [ ] = new int [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( str . charAt ( i ) < str . charAt ( j ) ) { right [ i ] ++ ; } } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( right [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }


"Number of Longest Increasing Subsequences | Java program for the above approach ; Function to count the number of LIS in the array nums [ ] ; Base Case ; Initialize dp_l array with 1 s ; Initialize dp_c array with 1 s ; If current element is smaller ; Store the maximum element from dp_l ; Stores the count of LIS ; Traverse dp_l and dp_c simultaneously ; Update the count ; Return the count of LIS ; Driver code ; Given array arr [ ] ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int findNumberOfLIS ( int [ ] nums ) { if ( nums . length == 0 ) return 0 ; int n = nums . length ; int [ ] dp_l = new int [ n ] ; Arrays . fill ( dp_l , 1 ) ; int [ ] dp_c = new int [ n ] ; Arrays . fill ( dp_c , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( nums [ i ] <= nums [ j ] ) continue ; if ( dp_l [ j ] + 1 > dp_l [ i ] ) { dp_l [ i ] = dp_l [ j ] + 1 ; dp_c [ i ] = dp_c [ j ] ; } else if ( dp_l [ j ] + 1 == dp_l [ i ] ) dp_c [ i ] += dp_c [ j ] ; } } int max_length = 0 ; for ( int i : dp_l ) max_length = Math . max ( i , max_length ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp_l [ i ] == max_length ) count += dp_c [ i ] ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 4 , 7 } ; System . out . print ( findNumberOfLIS ( arr ) + "
"); } }


"Number of M | Java program for the above approach ; Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; Create an array of size M + 1 ; Base cases ; Fill the dp table ; dp [ j ] will be equal to maximum number of sorted array of size j when elements are taken from 1 to i ; Here dp [ m ] will be equal to the maximum number of sorted arrays when element are taken from 1 to i ; Return the result ; Driver code ; Given Input ; Function Call"
How can the above be solved in Java?

public class Main { static int countSortedArrays ( int n , int m ) { int [ ] dp = new int [ ( m + 1 ) ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . print ( countSortedArrays ( n , m ) ) ; } }


"Number of M | Java program for the above approach ; Function to find the number of M - length sorted arrays possible using numbers from the range [ 1 , N ] ; If size becomes equal to m , that means an array is found ; Include current element , increase size by 1 and remain on the same element as it can be included again ; Exclude current element ; Return the sum obtained in both the cases ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static int countSortedArrays ( int start , int m , int size , int n ) { if ( size == m ) return 1 ; if ( start > n ) return 0 ; int notTaken = 0 , taken = 0 ; taken = countSortedArrays ( start , m , size + 1 , n ) ; notTaken = countSortedArrays ( start + 1 , m , size , n ) ; return taken + notTaken ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . println ( countSortedArrays ( 1 , m , 0 , n ) ) ; } }


"Number of N length sequences whose product is M | Java implementation of the above approach ; Function to calculate the value of ncr effectively ; Initializing the result ; Multiply and divide simultaneously to avoid overflow ; Return the answer ; Function to return the number of sequences of length N such that their product is M ; Hashmap to store the prime factors of M ; Calculate the prime factors of M ; If i divides M it means it is a factor Divide M by i till it could be divided to store the exponent ; Increase the exponent count ; If the number is a prime number greater than sqrt ( M ) ; Initializing the ans ; Multiply the answer for every prime factor ; entry . getValue ( ) represents the exponent of every prime factor ; Return the result ; Driver code"
How can the above be solved in Java?

import java . util . HashMap ; class geeks { public static int nCr ( int n , int r ) { int res = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { res *= ( n - r + i ) ; res /= i ; } return res ; } public static int NoofSequences ( int N , int M ) { HashMap < Integer , Integer > prime = new HashMap < > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( M ) ; i ++ ) { while ( M % i == 0 ) { if ( prime . get ( i ) == null ) prime . put ( i , 1 ) ; else { int x = prime . get ( i ) ; prime . put ( i , ++ x ) ; } M /= i ; } } if ( M > 1 ) { if ( prime . get ( M ) != null ) { int x = prime . get ( M ) ; prime . put ( M , ++ x ) ; } else prime . put ( M , 1 ) ; } int ans = 1 ; for ( HashMap . Entry < Integer , Integer > entry : prime . entrySet ( ) ) { ans *= ( nCr ( N + entry . getValue ( ) - 1 , N - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , M = 6 ; System . out . println ( NoofSequences ( N , M ) ) ; } }


"Number of NGEs to the right |  ; use of stl stack in Java ; push the ( n - 1 ) th index to the stack ; traverse in reverse order ; if no element is greater than arr [ i ] the number of NGEs to right is 0 ; number of NGEs to right of arr [ i ] is one greater than the number of NGEs to right of higher number to its right ; reverse again because values are in reverse order ; returns the vector of number of next greater elements to the right of each index . ; Driver code ; query 1 answered ; query 2 answered ; query 3 answered"
How can the above be solved in Java?

 import java . util . * ; class GFG { static Vector < Integer > no_NGN ( int arr [ ] , int n ) { Vector < Integer > nxt = new Vector < > ( ) ; Stack < Integer > s = new Stack < > ( ) ; nxt . add ( 0 ) ; s . add ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( ! s . isEmpty ( ) && arr [ i ] >= arr [ s . peek ( ) ] ) s . pop ( ) ; if ( s . isEmpty ( ) ) nxt . add ( 0 ) ; else nxt . add ( nxt . get ( n - s . peek ( ) - 1 ) + 1 ) ; s . add ( i ) ; } Collections . reverse ( nxt ) ; return nxt ; } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; Vector < Integer > nxt = no_NGN ( arr , n ) ; System . out . print ( nxt . get ( 3 ) + "
"); System . out . print ( nxt . get ( 6 ) + "
"); System . out . print ( nxt . get ( 1 ) + "
"); } }


"Number of NGEs to the right | Java program to print number of NGEs to the right ; array to store the next greater element index ; Use stack ; push the 0 th index to the stack ; traverse in the loop from 1 - nth index ; iterate till loop is empty ; get the topmost index in the stack ; if the current element is greater then the top index - th element , then this will be the next greatest index of the top index - th element ; initialize the cur index position 's  next greatest as index ; pop the cur index as its greater element has been found ; if not greater then break ; push the i index so that its next greatest can be found ; iterate for all other index left inside stack ; mark it as - 1 as no element in greater then it in right ; function to count the number of next greater numbers to the right ; initializes the next array as 0 ; calls the function to pre - calculate the next greatest element indexes ; if the i - th element has no next greater element to right ; Count of next greater numbers to right . ; answers all queries in O ( 1 ) ; returns the number of next greater elements to the right . ; driver code ; calls the function to count the number of greater elements to the right for every element . ; query 1 answered ; query 2 answered ; query 3 answered"
How can the above be solved in Java?

import java . util . * ; class GFG { static void fillNext ( int next [ ] , int a [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( s . size ( ) > 0 ) { int cur = s . peek ( ) ; if ( a [ cur ] < a [ i ] ) { next [ cur ] = i ; s . pop ( ) ; } else break ; } s . push ( i ) ; } while ( s . size ( ) > 0 ) { int cur = s . peek ( ) ; next [ cur ] = - 1 ; s . pop ( ) ; } } static void count ( int a [ ] , int dp [ ] , int n ) { int next [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) next [ i ] = 0 ; fillNext ( next , a , n ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( next [ i ] == - 1 ) dp [ i ] = 0 ; else dp [ i ] = 1 + dp [ next [ i ] ] ; } } static int answerQuery ( int dp [ ] , int index ) { return dp [ index ] ; } public static void main ( String args [ ] ) { int a [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; int n = a . length ; int dp [ ] = new int [ n ] ; count ( a , dp , n ) ; System . out . println ( answerQuery ( dp , 3 ) ) ; System . out . println ( answerQuery ( dp , 6 ) ) ; System . out . println ( answerQuery ( dp , 1 ) ) ; } }


"Number of Permutations such that no Three Terms forms Increasing Subsequence | Java program to find the nth catalan number ; Returns value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; A Binomial coefficient based function to find nth catalan number in O ( n ) time ; Calculate value of 2 nCn ; return 2 nCn / ( n + 1 ) ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static long binomialCoeff ( long n , long k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( long n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( catalan ( n ) ) ; } }


"Number of Relations that are both Irreflexive and Antisymmetric on a Set | Java program for the above approach ; Function to calculate x ^ y % mod in O ( log y ) ; Stores the result of x ^ y ; Update x if it exceeds mod ; If y is odd , then multiply x with result ; Divide y by 2 ; Update the value of x ; Return the value of x ^ y ; Function to count relations that are irreflexive and antisymmetric in a set consisting of N elements ; Return the resultant count ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( long x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( int ) ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int numberOfRelations ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( numberOfRelations ( N ) ) ; } }


"Number of Simple Graph with N Vertices and M Edges | Java implementation of the approach ; Function to return the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * ... * 1 ] ; Driver Code"
How can the above be solved in Java?

class GFG { static int binomialCoeff ( int n , int k ) { if ( k > n ) return 0 ; int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int N = 5 , M = 1 ; int P = ( N * ( N - 1 ) ) / 2 ; System . out . println ( binomialCoeff ( P , M ) ) ; } }


"Number of Subarrays with positive product | Java implementation of the approach ; Function to return the count of subarrays with negative product ; Replace current element with 1 if it is positive else replace it with - 1 instead ; Take product with previous element to form the prefix product ; Count positive and negative elements in the prefix product array ; Return the required count of subarrays ; Function to return the count of subarrays with positive product ; Total subarrays possible ; Count to subarrays with negative product ; Return the count of subarrays with positive product ; Driver code"
How can the above be solved in Java?

class GFG { static int negProdSubArr ( int arr [ ] , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } static int posProdSubArr ( int arr [ ] , int n ) { int total = ( n * ( n + 1 ) ) / 2 ; int cntNeg = negProdSubArr ( arr , n ) ; return ( total - cntNeg ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . length ; System . out . println ( posProdSubArr ( arr , n ) ) ; } }


"Number of Subsequences with Even and Odd Sum | Set 2 | Java program to find number of Subsequences with Even and Odd Sum ; Function to find number of Subsequences with Even and Odd Sum ; Counting number of odds ; Even count ; Total Subsequences is ( 2 ^ n - 1 ) For NumberOfEvenSubsequences subtract NumberOfOddSubsequences from total ; Driver code ; Calling the function"
How can the above be solved in Java?

import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair countSum ( int arr [ ] , int n ) { int NumberOfOdds = 0 , NumberOfEvens = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) NumberOfOdds ++ ; NumberOfEvens = n - NumberOfOdds ; int NumberOfOddSubsequences = ( 1 << NumberOfEvens ) * ( 1 << ( NumberOfOdds - 1 ) ) ; int NumberOfEvenSubsequences = ( 1 << n ) - 1 - NumberOfOddSubsequences ; return new pair ( NumberOfEvenSubsequences , NumberOfOddSubsequences ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; pair ans = countSum ( arr , n ) ; System . out . print ( " EvenSum ▁ = ▁ " + ans . first ) ; System . out . print ( " ▁ OddSum ▁ = ▁ " + ans . second ) ; } }


"Number of Symmetric Relations on a Set | Java program to count total symmetric relations on a set of natural numbers . ; function find the square of n ; Base case ; Return 2 ^ ( n ( n + 1 ) / 2 ) ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int countSymmetric ( int n ) { if ( n == 0 ) return 1 ; return 1 << ( ( n * ( n + 1 ) ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countSymmetric ( n ) ) ; } }


"Number of Transpositions in a Permutation | Java Program to find the number of transpositions in a permutation ; This array stores which element goes to which position ; This function returns the size of a component cycle ; If it is already visited ; This functio returns the number of transpositions in the permutation ; Initializing visited [ ] array ; building the goesTo [ ] array ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int N = 1000001 ; static int visited [ ] = new int [ N ] ; static int goesTo [ ] = new int [ N ] ; static int dfs ( int i ) { if ( visited [ i ] == 1 ) return 0 ; visited [ i ] = 1 ; int x = dfs ( goesTo [ i ] ) ; return ( x + 1 ) ; } static int noOfTranspositions ( int P [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) visited [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) goesTo [ P [ i ] ] = i + 1 ; int transpositions = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( visited [ i ] == 0 ) { int ans = dfs ( i ) ; transpositions += ans - 1 ; } } return transpositions ; } public static void main ( String [ ] args ) { int permutation [ ] = { 5 , 1 , 4 , 3 , 2 } ; int n = permutation . length ; System . out . println ( noOfTranspositions ( permutation , n ) ) ; } }


"Number of Triangles in an Undirected Graph | Java program to find number of triangles in an Undirected Graph . The program is for adjacency matrix representation of the graph ; Number of vertices in the graph ; Utility function for matrix multiplication ; Utility function to calculate trace of a matrix ( sum of diagnonal elements ) ; Utility function for calculating number of triangles in graph ; To Store graph ^ 2 ; To Store graph ^ 3 ; Initialising aux matrices with 0 ; aux2 is graph ^ 2 now printMatrix ( aux2 ) ; after this multiplication aux3 is graph ^ 3 printMatrix ( aux3 ) ; Driver code"
How can the above be solved in Java?

import java . io . * ; class Directed { int V = 4 ; void multiply ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { C [ i ] [ j ] = 0 ; for ( int k = 0 ; k < V ; k ++ ) { C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] ; } } } } int getTrace ( int graph [ ] [ ] ) { int trace = 0 ; for ( int i = 0 ; i < V ; i ++ ) { trace += graph [ i ] [ i ] ; } return trace ; } int triangleInGraph ( int graph [ ] [ ] ) { int [ ] [ ] aux2 = new int [ V ] [ V ] ; int [ ] [ ] aux3 = new int [ V ] [ V ] ; for ( int i = 0 ; i < V ; ++ i ) { for ( int j = 0 ; j < V ; ++ j ) { aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 ; } } multiply ( graph , graph , aux2 ) ; multiply ( graph , aux2 , aux3 ) ; int trace = getTrace ( aux3 ) ; return trace / 6 ; } public static void main ( String args [ ] ) { Directed obj = new Directed ( ) ; int graph [ ] [ ] = { { 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } } ; System . out . println ( " Total ▁ number ▁ of ▁ Triangle ▁ in ▁ Graph ▁ : ▁ " + obj . triangleInGraph ( graph ) ) ; } }


"Number of Triangles that can be formed given a set of lines in Euclidean Plane | Java program to find the number of triangles that can be formed using a set of lines in Euclidean Plane ; Double variables can ' t ▁ be ▁ checked ▁ precisely ▁ using ▁ ' == ' this function returns true if the double variables are equal ; This function returns the number of triangles for a given set of lines ; Slope array stores the slope of lines ; Slope array is sorted so that all lines with same slope come together ; After sorting slopes , count different slopes . k is index in count [ ] . ; Count of current slope ; Calculating sum1 ( Sum of all slopes ) sum1 = m1 + m2 + ... ; Calculating sum2 . sum2 = m1 * m2 + m2 * m3 + ... ; Needed for sum3 ; Calculating sum3 which gives the final answer m1 * m2 * m3 + m2 * m3 * m4 + ... ; Driver code ; Lines are stored as arrays of a , b and c for ' ax + by = c ' ; n is the number of lines"
How can the above be solved in Java?

import java . util . * ; class GFG { static double EPSILON = 1.0842e-19 ; static boolean compareDoubles ( double A , double B ) { double diff = A - B ; return ( diff < EPSILON ) && ( - diff < EPSILON ) ; } static int numberOfTringles ( int [ ] a , int [ ] b , int [ ] c , int n ) { Vector < Double > slope = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) slope . add ( ( double ) ( a [ i ] * 1.0 ) / b [ i ] ) ; Collections . sort ( slope ) ; int [ ] count = new int [ n ] ; int k = 0 ; int this_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( compareDoubles ( ( double ) slope . get ( i ) , ( double ) slope . get ( i - 1 ) ) ) this_count ++ ; else { count [ k ++ ] = this_count ; this_count = 1 ; } } count [ k ++ ] = this_count ; int sum1 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum1 += count [ i ] ; int sum2 = 0 ; int temp [ ] = new int [ n ] ; for ( int i = 0 ; i < k ; i ++ ) { temp [ i ] = count [ i ] * ( sum1 - count [ i ] ) ; sum2 += temp [ i ] ; } sum2 /= 2 ; int sum3 = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum3 += count [ i ] * ( sum2 - temp [ i ] ) ; sum3 /= 3 ; return sum3 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 4 , 5 , 5 } ; int c [ ] = { 5 , 7 , 8 , 6 } ; int n = a . length ; System . out . println ( " The ▁ number ▁ of ▁ triangles ▁ " + " that ▁ can ▁ be ▁ formed ▁ are : ▁ " + numberOfTringles ( a , b , c , n ) ) ; } }


"Number of Unique BST with a given key | Dynamic Programming | Java code to find number of unique BSTs Dynamic Programming solution ; DP to store the number of unique BST with key i ; Base case ; fill the dp table in top - down approach . ; n - i in right * i - 1 in left ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . Arrays ; class GFG { static int numberOfBST ( int n ) { int dp [ ] = new int [ n + 1 ] ; Arrays . fill ( dp , 0 ) ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { dp [ i ] = dp [ i ] + ( dp [ i - j ] * dp [ j - 1 ] ) ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( " Number ▁ of ▁ structurally ▁ " + " Unique ▁ BST ▁ with ▁ " + n + " ▁ keys ▁ are ▁ : ▁ " + numberOfBST ( n ) ) ; } }


"Number of alternating substrings from a given Binary String | Java program for the above approach ; Function to count number of alternating substrings from a given binary string ; Initialize dp array , where dp [ i ] [ j ] stores the number of alternating strings starts with i and having j elements . ; Traverse the string from the end ; If i is equal to N - 1 ; Otherwise , ; Increment count of substrings starting at i and has 0 in the beginning ; Increment count of substrings starting at i and has 1 in the beginning ; Stores the result ; Iterate in the range [ 0 , N - 1 ] ; Update ans ; Return the ans ; Driver Code ; Given Input ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int countAlternatingSubstrings ( String S , int N ) { int [ ] [ ] dp = new int [ 2 ] [ N ] ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { if ( S . charAt ( i ) == '1' ) dp [ 1 ] [ i ] = 1 ; else dp [ 0 ] [ i ] = 1 ; } else { if ( S . charAt ( i ) == '0' ) dp [ 0 ] [ i ] = 1 + dp [ 1 ] [ i + 1 ] ; else dp [ 1 ] [ i ] = 1 + dp [ 0 ] [ i + 1 ] ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . max ( dp [ 0 ] [ i ] , dp [ 1 ] [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "0010" ; int N = S . length ( ) ; System . out . print ( countAlternatingSubstrings ( S , N ) ) ; } }


"Number of anomalies in an array |  ; Sort the array so that we can apply binary search . ; One by one check every element if it is anomaly or not using binary search . ; If arr [ i ] is not largest element and element just greater than it is within k , then return false . ; If there are more than one occurrences of arr [ i ] , return false . ; If arr [ i ] is not smallest element and just smaller element is not k distance away ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int countAnomalies ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int u = upper_bound ( a , 0 , n , a [ i ] ) ; if ( u < n && a [ u ] - a [ i ] <= k ) continue ; int s = lower_bound ( a , 0 , n , a [ i ] ) ; if ( u - s > 1 ) continue ; if ( s > 0 && a [ s - 1 ] - a [ i ] <= k ) continue ; res ++ ; } return res ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 1 , 8 } , k = 5 ; int n = arr . length ; System . out . print ( countAnomalies ( arr , n , k ) ) ; } }


"Number of anomalies in an array | A simple java solution to count anomalies in an array . ; Driver code"
How can the above be solved in Java?

class GFG { static int countAnomalies ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && Math . abs ( arr [ i ] - arr [ j ] ) <= k ) break ; if ( j == n ) res ++ ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 1 , 8 } , k = 5 ; int n = arr . length ; System . out . println ( countAnomalies ( arr , n , k ) ) ; } }


"Number of array elements derivable from D after performing certain operations | Java program to find the number of array elements which can be derived by perming ( + A , - A , + B , - B ) operations on D ; Function to return gcd of a and b ; Function to Return the number of elements of arr [ ] which can be derived from D by performing ( + A , - A , + B , - B ) ; find the gcd of A and B ; counter stores the number of array elements which can be derived from D ; arr [ i ] can be derived from D only if | arr [ i ] - D | is divisible by gcd of A and B ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findPossibleDerivables ( int arr [ ] , int n , int D , int A , int B ) { int gcdAB = gcd ( A , B ) ; int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( Math . abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 7 , 13 } ; int n = arr . length ; int D = 5 , A = 4 , B = 2 ; System . out . println ( findPossibleDerivables ( arr , n , D , A , B ) ) ; int a [ ] = { 1 , 2 , 3 } ; n = a . length ; D = 6 ; A = 3 ; B = 2 ; System . out . println ( findPossibleDerivables ( a , n , D , A , B ) ) ; } }


"Number of arrays of size N whose elements are positive integers and sum is K | Java Program to find the number of arrays of size N whose elements are positive integers and sum is K ; Return nCr ; C [ 0 ] = 1 ; nC0 is 1 ; Compute next row of pascal triangle using the previous row ; Return the number of array that can be formed of size n and sum equals to k . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int countArray ( int N , int K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } public static void main ( String [ ] args ) { int N = 2 , K = 3 ; System . out . println ( countArray ( N , K ) ) ; } }


"Number of balanced bracket expressions that can be formed from a string | Java program to find number of balanced bracket expressions possible ; Max string length ; Function to check whether index start and end can form a bracket pair or not ; Check for brackets ( ) ; Check for brackets [ ] ; Check for brackets { } ; Function to find number of proper bracket expressions ; If starting index is greater than ending index ; If dp [ start ] [ end ] has already been computed ; Search for the bracket in from next index ; If bracket pair is formed , add number of combination ; If ? comes then all three bracket expressions are possible ; Return answer ; If n is odd , string cannot be balanced ; Driving function"
How can the above be solved in Java?

class GFG { static int MAX = 300 ; static int checkFunc ( int i , int j , String st ) { if ( st . charAt ( i ) == ' ( ' && st . charAt ( j ) == ' ) ' ) return 1 ; if ( st . charAt ( i ) == ' ( ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' ) ' ) return 1 ; if ( st . charAt ( i ) == ' [ ' && st . charAt ( j ) == ' ] ' ) return 1 ; if ( st . charAt ( i ) == ' [ ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' ] ' ) return 1 ; if ( st . charAt ( i ) == ' { ' && st . charAt ( j ) == ' } ' ) return 1 ; if ( st . charAt ( i ) == ' { ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' } ' ) return 1 ; return 0 ; } static int countRec ( int start , int end , int dp [ ] [ ] , String st ) { int sum = 0 ; if ( start > end ) return 1 ; if ( dp [ start ] [ end ] != - 1 ) return dp [ start ] [ end ] ; int i , r = 0 ; for ( i = start + 1 ; i <= end ; i += 2 ) { if ( checkFunc ( start , i , st ) == 1 ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) ; } else if ( st . charAt ( start ) == ' ? ' && st . charAt ( i ) == ' ? ' ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) * 3 ; } } return dp [ start ] [ end ] = sum ; } static int countWays ( String st ) { int n = st . length ( ) ; if ( n % 2 == 1 ) return 0 ; int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; return countRec ( 0 , n - 1 , dp , st ) ; } public static void main ( String [ ] args ) { String st = " ( ? ( [ ? ) ] ? } ? " ; System . out . println ( countWays ( st ) ) ; } }


"Number of balanced bracket subsequence of length 2 and 4 | Java implementation of above approach ; Taking the frequency suffix sum of the number of 2 's present after every index ; Storing the count of subsequence ; Subsequence of length 2 ; Subsequence of length 4 of type 1 1 2 2 ; Subsequence of length 4 of type 1 2 1 2 ; Driver Code"
How can the above be solved in Java?

class GFG { public static void countWays ( int a [ ] , int n ) { int i , j ; long suff [ ] = new long [ n ] ; if ( a [ n - 1 ] == 2 ) suff [ n - 1 ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] == 2 ) suff [ i ] = suff [ i + 1 ] + 1 ; else suff [ i ] = suff [ i + 1 ] ; } long ss = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) ss += suff [ i ] ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && suff [ j ] >= 2 ) { ss += ( suff [ j ] ) * ( suff [ j ] - 1 ) / 2 ; } } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] == 1 && a [ j ] == 1 && ( suff [ i ] - suff [ j ] ) >= 1 && suff [ j ] >= 1 ) { ss += ( suff [ i ] - suff [ j ] ) * suff [ j ] ; } } } System . out . println ( ss ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 1 , 2 , 2 } ; int n = 6 ; countWays ( a , n ) ; } }


"Number of balanced parenthesis substrings | Java program to find number of balanced parenthesis sub strings ; Function to find number of balanced parenthesis sub strings ; To store required answer ; Vector to stores the number of balanced brackets at each depth . ; d stores checks the depth of our sequence For example level of ( ) is 1 and that of ( ( ) ) is 2. ; If open bracket increase depth ; If closing bracket ; Return the required answer ; Driver code ; Function call"
How can the above be solved in Java?

class GFG { public static int Balanced_Substring ( String str , int n ) { int ans = 0 ; int [ ] arr = new int [ n / 2 + 1 ] ; int d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) d ++ ; else { if ( d == 1 ) { for ( int j = 2 ; j <= n / 2 + 1 && arr [ j ] != 0 ; j ++ ) arr [ j ] = 0 ; } ++ ans ; ans += arr [ d ] ; arr [ d ] ++ ; d -- ; } } return ans ; } public static void main ( String [ ] args ) { String str = " ( ) ( ) ( ) " ; int n = str . length ( ) ; System . out . println ( Balanced_Substring ( str , n ) ) ; } }


"Number of binary strings such that there is no substring of length â ‰¥ 3 | Java implementation of the approach ; Function to return the count of all possible binary strings ; Base cases ; dp [ i ] [ j ] is the number of possible strings such that '1' just appeared consecutively j times upto ith index ; Taking previously calculated value ; Taking all the possible cases that can appear at the Nth position ; Driver code"
How can the above be solved in Java?

class GFG { final static long MOD = 1000000007 ; static long countStr ( int N ) { long dp [ ] [ ] = new long [ N + 1 ] [ 3 ] ; dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 0 ] % MOD ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 1 ] % MOD ; } long ans = ( dp [ N ] [ 0 ] + dp [ N ] [ 1 ] + dp [ N ] [ 2 ] ) % MOD ; return ans ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . println ( countStr ( N ) ) ; } }


"Number of buildings facing the sun | Java program to count buildings that can see sunlight . ; Returns count buildings that can see sunlight ; Initialuze result ( Note that first building always sees sunlight ) ; Start traversing element ; If curr_element is maximum or current element is equal , update maximum and increment count ; Driver method"
How can the above be solved in Java?

class Test { static int countBuildings ( int arr [ ] , int n ) { int count = 1 ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 4 , 8 , 2 , 9 } ; System . out . println ( countBuildings ( arr , arr . length ) ) ; } }


"Number of cells a queen can move with obstacles on the chessborad | Java program to find number of cells a queen can move with obstacles on the chessborad ; Return the number of position a Queen can move . ; d11 , d12 , d21 , d22 are for diagnoal distances . r1 , r2 are for vertical distance . c1 , c2 are for horizontal distance . ; Initialise the distance to end of the board . ; For each obstacle find the minimum distance . If obstacle is present in any direction , distance will be updated . ; Driver code ; Chessboard size ; number of obstacles ; Queen x position ; Queen y position ; x position of obstacles ; y position of obstacles"
How can the above be solved in Java?

import java . io . * ; class GFG { static int numberofPosition ( int n , int k , int x , int y , int obstPosx [ ] , int obstPosy [ ] ) { int d11 , d12 , d21 , d22 , r1 , r2 , c1 , c2 ; d11 = Math . min ( x - 1 , y - 1 ) ; d12 = Math . min ( n - x , n - y ) ; d21 = Math . min ( n - x , y - 1 ) ; d22 = Math . min ( x - 1 , n - y ) ; r1 = y - 1 ; r2 = n - y ; c1 = x - 1 ; c2 = n - x ; for ( int i = 0 ; i < k ; i ++ ) { if ( x > obstPosx [ i ] && y > obstPosy [ i ] && x - obstPosx [ i ] == y - obstPosy [ i ] ) d11 = Math . min ( d11 , x - obstPosx [ i ] - 1 ) ; if ( obstPosx [ i ] > x && obstPosy [ i ] > y && obstPosx [ i ] - x == obstPosy [ i ] - y ) d12 = Math . min ( d12 , obstPosx [ i ] - x - 1 ) ; if ( obstPosx [ i ] > x && y > obstPosy [ i ] && obstPosx [ i ] - x == y - obstPosy [ i ] ) d21 = Math . min ( d21 , obstPosx [ i ] - x - 1 ) ; if ( x > obstPosx [ i ] && obstPosy [ i ] > y && x - obstPosx [ i ] == obstPosy [ i ] - y ) d22 = Math . min ( d22 , x - obstPosx [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] < y ) r1 = Math . min ( r1 , y - obstPosy [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] > y ) r2 = Math . min ( r2 , obstPosy [ i ] - y - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] < x ) c1 = Math . min ( c1 , x - obstPosx [ i ] - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] > x ) c2 = Math . min ( c2 , obstPosx [ i ] - x - 1 ) ; } return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2 ; } public static void main ( String [ ] args ) { int n = 8 ; int k = 1 ; int Qposx = 4 ; int Qposy = 4 ; int obstPosx [ ] = { 3 } ; int obstPosy [ ] = { 5 } ; System . out . println ( numberofPosition ( n , k , Qposx , Qposy , obstPosx , obstPosy ) ) ; } }


"Number of cells in a matrix that satisfy the given condition | Java implementation of the approach ; Function to return the number of cells in which mirror can be placed ; Update the row array where row [ i ] [ j ] will store whether the current row i contains all 1 s in the columns starting from j ; Update the column array where col [ i ] [ j ] will store whether the current column j contains all 1 s in the rows starting from i ; To store the required result ; For every cell except the last row and the last column ; If the current cell is not blocked and the light can travel from the next row and the next column then the current cell is valid ; For the last column ; For the last row , note that the last column is not taken into consideration as the bottom right element has already been considered in the last column previously ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int N = 3 ; static int numberOfCells ( int mat [ ] [ ] ) { boolean [ ] [ ] row = new boolean [ N ] [ N ] ; boolean [ ] [ ] col = new boolean [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] [ j ] = ( j + 1 < N ) ? row [ i ] [ j + 1 ] : true ; } else { row [ i ] [ j ] = false ; } } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == 1 ) { col [ i ] [ j ] = ( i + 1 < N ) ? col [ i + 1 ] [ j ] : true ; } else { col [ i ] [ j ] = false ; } } } int cnt = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ i ] [ j ] && col [ i ] [ j ] ) { cnt ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { if ( col [ i ] [ N - 1 ] ) cnt ++ ; } for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ N - 1 ] [ j ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 1 } , { 0 , 1 , 1 } , { 0 , 1 , 1 } } ; System . out . print ( numberOfCells ( mat ) ) ; } }


"Number of cells in the Nth order figure | Java implementation of the approach ; Function to return the number of cells in the nth order figure of the given type ; Driver code"
How can the above be solved in Java?

class GFG { static int cntCells ( int n ) { int cells = ( int ) Math . pow ( n , 2 ) + ( int ) Math . pow ( n - 1 , 2 ) ; return cells ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( cntCells ( n ) ) ; } }


"Number of character corrections in the given strings to make them equal | Java implementation of the approach ; Function to return the count of operations required ; To store the count of operations ; No operation required ; One operation is required when any two characters are equal ; Two operations are required when none of the characters are equal ; Return the minimum count of operations required ; Driver code"
How can the above be solved in Java?

class GFG { static int minOperations ( int n , String a , String b , String c ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char x = a . charAt ( i ) ; char y = b . charAt ( i ) ; char z = c . charAt ( i ) ; if ( x == y && y == z ) ; else if ( x == y y == z x == z ) { ans ++ ; } else { ans += 2 ; } } return ans ; } public static void main ( String [ ] args ) { String a = " place " ; String b = " abcde " ; String c = " plybe " ; int n = a . length ( ) ; System . out . print ( minOperations ( n , a , b , c ) ) ; } }


"Number of chocolates left after k iterations | Java program to find remaining chocolates after k iterations ; Function to find the chocolates left ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int results ( int n , int k ) { return ( int ) Math . round ( Math . pow ( n , ( 1.0 / Math . pow ( 2.0 , k ) ) ) ) ; } public static void main ( String args [ ] ) { int k = 3 , n = 100000000 ; System . out . print ( " Chocolates ▁ left ▁ after ▁ " + k + " ▁ iterations ▁ are ▁ " + results ( n , k ) ) ; } }


"Number of circular tours that visit all petrol pumps | Java Program to find the number of circular tour that visits all petrol pump ; Return the number of pumps from where we can start the journey . ; Making Circular Array . ; for each of the petrol pump . ; If tank is less than 0. ; If starting pump is greater than n , return ans as 0. ; For each of the petrol pump ; Finding the need array ; If need is 0 , increment the count . ; Driver code"
How can the above be solved in Java?

 import java . io . * ; class GFG { static int N = 100 ; public static int count ( int n , int c , int a [ ] , int b [ ] ) { int need [ ] = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } int s = 0 ; int tank = 0 ; for ( int i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = Math . min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; int ans = 1 ; need [ s + n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int id = s + n - i ; need [ id ] = Math . max ( 0 , need [ id + 1 ] + b [ id ] - Math . min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } public static void main ( String args [ ] ) { int n = 3 ; int c = 3 ; int [ ] a = new int [ ] { 3 , 1 , 2 , 0 , 0 , 0 } ; int [ ] b = new int [ ] { 2 , 2 , 2 , 0 , 0 , 0 } ; System . out . print ( count ( n , c , a , b ) + "
"); } }


"Number of closing brackets needed to complete a regular bracket sequence | Java program to find number of closing brackets needed and complete a regular bracket sequence ; Function to find number of closing brackets and complete a regular bracket sequence ; Finding the length of sequence ; Counting opening brackets ; Counting closing brackets ; Checking if at any position the number of closing bracket is more then answer is impossible ; If possible , print ' s ' and required closing brackets . ; Driver code"
How can the above be solved in Java?

class GFG { static void completeSequence ( String s ) { int n = s . length ( ) ; int open = 0 , close = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) open ++ ; else close ++ ; if ( close > open ) { System . out . print ( " IMPOSSIBLE " ) ; return ; } } System . out . print ( s ) ; for ( int i = 0 ; i < open - close ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String [ ] args ) { String s = " ( ( ) ( ( ) ( " ; completeSequence ( s ) ; } }


"Number of co | Java program for the above approach ; Function to check whether given integers are co - prime or not ; Utility function to count number of co - prime pairs ; Traverse the array ; If co - prime ; Increment count ; Return count ; Function to count number of co - prime pairs ; Stores digits in string form ; Sort the list ; Keep two copies of list l ; Generate 2 digit numbers using d1 and d2 ; If current number does not exceed N ; Stores length of list ; Stores number of co - prime pairs ; Print number of co - prime pairs ; Driver Code ; Given value of N , d1 , d2 ; Function call to count number of co - prime pairs"
How can the above be solved in Java?

import java . util . * ; class GFG { static int GCD ( int a , int b ) { return b == 0 ? a : GCD ( b , a % b ) ; } static boolean coprime ( int a , int b ) { if ( GCD ( a , b ) == 1 ) return true ; return false ; } static int numOfPairs ( ArrayList < String > arr , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( coprime ( Integer . parseInt ( arr . get ( i ) ) , Integer . parseInt ( arr . get ( j ) ) ) ) { count = count + 1 ; } } } return count ; } static void noOfCoPrimePairs ( int N , int d1 , int d2 ) { ArrayList < String > l = new ArrayList < String > ( ) ; l . add ( Integer . toString ( d1 ) ) ; l . add ( Integer . toString ( d2 ) ) ; Collections . sort ( l ) ; if ( N < Integer . parseInt ( l . get ( 1 ) ) ) return ; ArrayList < String > total = new ArrayList < String > ( l ) ; ArrayList < String > temp2 = new ArrayList < String > ( l ) ; int flag = 0 ; ArrayList < String > temp3 = new ArrayList < String > ( l ) ; while ( l . get ( 0 ) . length ( ) < 10 ) { for ( int i = 0 ; i < l . size ( ) ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( Integer . parseInt ( l . get ( i ) + temp2 . get ( j ) ) > N ) { flag = 1 ; break ; } total . add ( l . get ( i ) + temp2 . get ( j ) ) ; temp3 . add ( l . get ( i ) + temp2 . get ( j ) ) ; } if ( flag == 1 ) break ; } if ( flag == 1 ) break ; l = temp3 ; temp3 . clear ( ) ; } int lenOfTotal = total . size ( ) ; int ans = numOfPairs ( total , lenOfTotal ) ; System . out . print ( ans ) ; } public static void main ( String args [ ] ) { int N = 30 , d1 = 2 , d2 = 3 ; noOfCoPrimePairs ( N , d1 , d2 ) ; } }


"Number of common base strings for two strings | Java program to count common base strings of s1 and s2 . ; function for finding common divisor ; Checking if ' base ' is base String of ' s1' ; Checking if ' base ' is base String of ' s2' ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isCommonBase ( String base , String s1 , String s2 ) { for ( int j = 0 ; j < s1 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s1 . charAt ( j ) ) { return false ; } } for ( int j = 0 ; j < s2 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s2 . charAt ( j ) ) { return false ; } } return true ; } static int countCommonBases ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; int count = 0 ; for ( int i = 1 ; i <= Math . min ( n1 , n2 ) ; i ++ ) { String base = s1 . substring ( 0 , i ) ; if ( isCommonBase ( base , s1 , s2 ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s1 = " pqrspqrs " ; String s2 = " pqrspqrspqrspqrs " ; System . out . println ( countCommonBases ( s1 , s2 ) ) ; } }


"Number of common digits present in two given numbers | Java program for the above approach ; Function to count number of digits that are common in both N and M ; Stores the count of common digits ; Stores the count of digits of N ; Stores the count of digits of M ; Iterate over the digits of N ; Increment the count of last digit of N ; Update N ; Iterate over the digits of M ; Increment the count of last digit of M ; Update M ; Iterate over the range [ 0 , 9 ] ; If freq1 [ i ] and freq2 [ i ] both exceeds 0 ; Increment count by 1 ; Return the count ; Driver Code ; Input"
How can the above be solved in Java?

import java . util . * ; class GFG { static int CommonDigits ( int N , int M ) { int count = 0 ; int freq1 [ ] = new int [ 10 ] ; int freq2 [ ] = new int [ 10 ] ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = N / 10 ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = M / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 748294 ; int M = 34298156 ; System . out . print ( CommonDigits ( N , M ) ) ; } }


"Number of common tangents between two circles if their centers and radius is given | Java program to find the number of common tangents between the two circles ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } public static void main ( String [ ] args ) { int x1 = - 10 , y1 = 8 ; int x2 = 14 , y2 = - 24 ; int r1 = 30 , r2 = 10 ; int t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) System . out . println ( " There ▁ are ▁ 3 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else if ( t < 0 ) System . out . println ( " There ▁ are ▁ 4 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else System . out . println ( " There ▁ are ▁ 2 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; } }


"Number of compositions of a natural number | Java program to find the total number of compositions of a natural number ; Return 2 raised to power ( n - 1 ) ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static int countCompositions ( int n ) { return 1 << ( n - 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . print ( countCompositions ( n ) ) ; } }


"Number of connected components of a graph ( using Disjoint Set Union ) | Java program for the above approach ; Stores the parent of each vertex ; Function to find the topmost parent of vertex a ; If current vertex is the topmost vertex ; Otherwise , set topmost vertex of its parent as its topmost vertex ; Function to connect the component having vertex a with the component having vertex b ; Connect edges ; Function to find unique top most parents ; Traverse all vertices ; Insert all topmost vertices obtained ; Print count of connected components ; Function to print answer ; Setting parent to itself ; Traverse all edges ; Print answer ; Driver Code ; Given N ; Given edges ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int [ ] parent = new int [ 1000000 ] ; static int root ( int a ) { if ( a == parent [ a ] ) { return a ; } return parent [ a ] = root ( parent [ a ] ) ; } static void connect ( int a , int b ) { a = root ( a ) ; b = root ( b ) ; if ( a != b ) { parent [ b ] = a ; } } static void connectedComponents ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( parent [ i ] ) ; } System . out . println ( s . size ( ) ) ; } static void printAnswer ( int N , int [ ] [ ] edges ) { for ( int i = 0 ; i <= N ; i ++ ) { parent [ i ] = i ; } for ( int i = 0 ; i < edges . length ; i ++ ) { connect ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; } connectedComponents ( N ) ; } public static void main ( String [ ] args ) { int N = 8 ; int [ ] [ ] edges = { { 1 , 0 } , { 0 , 2 } , { 5 , 3 } , { 3 , 4 } , { 6 , 7 } } ; printAnswer ( N , edges ) ; } }


"Number of containers that can be filled in the given time | Java program for the above problem ; Matrix of containers ; Function to find the number of containers that will be filled in X seconds ; Container on top level ; If container gets filled ; Dividing the liquid equally in two halves ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double cont [ ] [ ] = new double [ 1000 ] [ 1000 ] ; static void num_of_containers ( int n , double x ) { int count = 0 ; cont [ 1 ] [ 1 ] = x ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( cont [ i ] [ j ] >= ( double ) 1 ) { count ++ ; cont [ i + 1 ] [ j ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; cont [ i + 1 ] [ j + 1 ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; } } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 3 ; double x = 5 ; num_of_containers ( n , x ) ; } }


"Number of continuous reductions of A from B or B from A to make them ( 1 , 1 ) | Java implementation to find the minimum number of operations required to reach ( 1 , 1 ) ; Function to find the minimum number of steps required ; Condition to check if it is not possible to reach ; Condition to check if the pair is reached to 1 , 1 ; Condition to change the A as the maximum element ; Driver Code"
How can the above be solved in Java?

class GFG { static int minimumSteps ( int a , int b , int c ) { if ( a < 1 b < 1 ) return - 1 ; if ( a == 1 && b == 1 ) return c ; if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } return minimumSteps ( a - b , b , c + 1 ) ; } public static void main ( String [ ] args ) { int a = 75 ; int b = 17 ; System . out . println ( minimumSteps ( a , b , 0 ) ) ; } }


"Number of cycles formed by joining vertices of n sided polygon at the center | Java program for the above approach ; Function to calculate number of cycles ; BigInteger is used here if N = 10 ^ 9 then multiply will result into value greater than 10 ^ 18 ; BigInteger multiply function ; Return the final result ; Driver Code ; Given N ; Function Call"
How can the above be solved in Java?

import java . util . * ; import java . math . * ; class GFG { static BigInteger findCycles ( int N ) { BigInteger res , finalResult ; long val = 2 * N - 1 ; String st = String . valueOf ( val ) ; BigInteger str = new BigInteger ( st ) ; String n1 = String . valueOf ( ( N - 1 ) ) ; String n2 = String . valueOf ( ( N - 2 ) ) ; BigInteger a = new BigInteger ( n1 ) ; BigInteger b = new BigInteger ( n2 ) ; res = a . multiply ( b ) ; finalResult = res . add ( str ) ; return finalResult ; } public static void main ( String args [ ] ) throws Exception { int N = 5 ; System . out . println ( findCycles ( N ) ) ; } }


"Number of cycles in a Polygon with lines from Centroid to Vertices | Java program to find number of cycles in a Polygon with lines from Centroid to Vertices ; Function to find the Number of Cycles ; Driver code"
How can the above be solved in Java?

class GFG { static int nCycle ( int N ) { return ( N ) * ( N - 1 ) + 1 ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nCycle ( N ) ) ; } }


"Number of days after which tank will become empty | Java code to find number of days after which tank will become empty ; Method returns minimum number of days after which tank will become empty ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class GFG { static int minDaysToEmpty ( int C , int l ) { if ( l >= C ) return C ; double eq_root = ( Math . sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 ; return ( int ) ( Math . ceil ( eq_root ) + l ) ; } public static void main ( String [ ] args ) { System . out . println ( minDaysToEmpty ( 5 , 2 ) ) ; System . out . println ( minDaysToEmpty ( 6514683 , 4965 ) ) ; } }


"Number of days after which tank will become empty | Java code to find number of days after which tank will become empty ; Utility method to get sum of first n numbers ; Method returns minimum number of days after which tank will become empty ; if water filling is more than capacity then after C days only tank will become empty ; initialize binary search variable ; loop until low is less than high ; if cumulate sum is greater than ( C - l ) then search on left side ; if ( C - l ) is more then search on right side ; final answer will be obtained by adding l to binary search result ; Driver code to test above methods"
How can the above be solved in Java?

public class Tank_Empty { static int getCumulateSum ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static int minDaysToEmpty ( int C , int l ) { if ( C <= l ) return C ; int lo = 0 ; int hi = ( int ) 1e4 ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( getCumulateSum ( mid ) >= ( C - l ) ) hi = mid ; else lo = mid + 1 ; } return ( l + lo ) ; } public static void main ( String args [ ] ) { int C = 5 ; int l = 2 ; System . out . println ( minDaysToEmpty ( C , l ) ) ; } }


"Number of decimal numbers of length k , that are strict monotone | Java program to count numbers of k digits that are strictly monotone . ; DP [ i ] [ j ] is going to store monotone numbers of length i + 1 considering j + 1 digits ( 1 , 2 , 3 , . .9 ) ; Unit length numbers ; Building dp [ ] in bottom up ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int DP_s = 9 ; static int getNumStrictMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( getNumStrictMonotone ( n ) ) ; } }


"Number of different cyclic paths of length N in a tetrahedron | Java program count total number of paths to reach B from B ; Function to count the number of steps in a tetrahedron ; initially coming to B is B -> B ; cannot reach A , D or C ; iterate for all steps ; recurrence relation ; memoize previous values ; returns steps ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countPaths ( int n ) { int zB = 1 ; int zADC = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int nzB = zADC * 3 ; int nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countPaths ( n ) ) ; } }


"Number of different positions where a person can stand | Java implementation of above approach ; Function to find the position ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int findPosition ( int n , int f , int b ) { return n - Math . max ( f + 1 , n - b ) + 1 ; } public static void main ( String args [ ] ) { int n = 5 , f = 2 , b = 3 ; System . out . print ( findPosition ( n , f , b ) ) ; } }


"Number of digits before the decimal point in the division of two numbers | Java implementation of the approach ; Function to return the number of digits before the decimal in a / b ; Absolute value of a / b ; If result is 0 ; Count number of digits in the result ; Return the required count of digits ; Driver code"
How can the above be solved in Java?

class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . abs ( a / b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void main ( String args [ ] ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }