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

class GFG { public static int countDigits ( int a , int b ) { double digits = Math . log10 ( Math . abs ( a ) ) - Math . log10 ( Math . abs ( b ) ) + 1 ; return ( int ) Math . floor ( digits ) ; } public static void main ( String [ ] args ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }


"Number of digits in 2 raised to power n | Java program to find number of digits in 2 ^ n ; Function to find number of digits in 2 ^ n ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class Gfg { static int countDigits ( int n ) { return ( int ) ( n * Math . log10 ( 2 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( countDigits ( n ) ) ; } }


"Number of digits in N factorial to the power N | Java program to find count of digits in N factorial raised to N ; we take sum of logarithms as explained in the approach ; multiply the result with n ; Driver Code"
How can the above be solved in Java?

 import java . io . * ; class GFG { static int countDigits ( int n ) { double ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += Math . log10 ( i ) ; ans = ans * n ; return 1 + ( int ) Math . floor ( ans ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countDigits ( n ) + "
"); } }


"Number of digits in the nth number made of given four digits | Java program to count number of digits in n - th number made of given four digits . ; Efficient function to calculate number of digits in the nth number constructed by using 6 , 1 , 4 and 9 as digits in the ascending order . ; Number of digits increase after every i - th number where i increases in powers of 4. ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int number_of_digits ( int n ) { int i ; int res ; int sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( number_of_digits ( n ) ) ; } }


"Number of digits in the product of two numbers | JAVA Code for Number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; if either of the number is 0 , then product will be 0 ; required count of digits ; Driver program to test above function"
How can the above be solved in Java?

class GFG { public static int countDigits ( int a , int b ) { if ( a == 0 b == 0 ) return 1 ; return ( int ) Math . floor ( Math . log10 ( Math . abs ( a ) ) + Math . log10 ( Math . abs ( b ) ) ) + 1 ; } public static void main ( String [ ] args ) { int a = 33 ; int b = - 24 ; System . out . print ( countDigits ( a , b ) ) ; } }


"Number of digits in the product of two numbers | Java implementation to count number of digits in the product of two numbers ; function to count number of digits in the product of two numbers ; absolute value of the product of two numbers ; if product is 0 ; count number of digits in the product ' p ' ; required count of digits ; Driver program to test above"
How can the above be solved in Java?

import java . io . * ; import java . math . * ; 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 = 33 ; int b = - 24 ; System . out . println ( " Number ▁ of ▁ digits ▁ = ▁ " + countDigits ( a , b ) ) ; } }


"Number of digits to be removed to make a number divisible by 3 | Java program to find the minimum number of digits to be removed to make a large number divisible by 3. ; function to count the no of removal of digits to make a very large number divisible by 3 ; add up all the digits of num ; if num is already is divisible by 3 then no digits are to be removed ; if there is single digit , then it is not possible to remove one digit . ; traverse through the number and find out if any number on removal makes the sum divisible by 3 ; if there are two numbers then it is not possible to remove two digits . ; Otherwise we can always make a number multiple of 2 by removing 2 digits . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void main ( String [ ] args ) { String num = "1234" ; System . out . println ( divisible ( num ) ) ; } }


"Number of distinct integers obtained by lcm ( X , N ) / X | Java program to find distinct integers ontained by lcm ( x , num ) / x ; Function to count the number of distinct integers ontained by lcm ( x , num ) / x ; iterate to count the number of factors ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int numberOfDistinct ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { ans ++ ; if ( ( n / i ) != i ) ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( numberOfDistinct ( n ) ) ; } }


"Number of distinct permutation a String can have | Java program to find number of distinct permutations of a string . ; Utility function to find factorial of n . ; Returns count of distinct permutations of str . ; finding frequency of all the lower case alphabet and storing them in array of integer ; finding factorial of number of appearances and multiplying them since they are repeating alphabets ; finding factorial of size of string and dividing it by factorial found after multiplying ; Driver code"
How can the above be solved in Java?

public class GFG { static final int MAX_CHAR = 26 ; static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int countDistinctPermutations ( String str ) { int length = str . length ( ) ; int [ ] freq = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < length ; i ++ ) if ( str . charAt ( i ) >= ' a ' ) freq [ str . charAt ( i ) - ' a ' ] ++ ; int fact = 1 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) fact = fact * factorial ( freq [ i ] ) ; return factorial ( length ) / fact ; } public static void main ( String args [ ] ) { String str = " fvvfhvgv " ; System . out . println ( countDistinctPermutations ( str ) ) ; } }


"Number of distinct subsets of a set | Java program to count number of distinct subsets in an array of distinct numbers ; Returns 2 ^ n ; Driver program to test above function"
How can the above be solved in Java?

class GFG { static int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int n = A . length ; System . out . println ( subsetCount ( A , n ) ) ; } }


"Number of distinct ways to represent a number as sum of K unique primes | Java program to count the number of distinct ways to represent a number as K different primes ; Prime vector ; Sieve array of prime ; DP array ; Initialise all numbers as prime ; Sieve of Eratosthenes . ; Push all the primes into prime vector ; Function to get the number of distinct ways to get sum as K different primes ; If index went out of prime array size or the sum became larger than n return 0 ; If sum becomes equal to n and j becomes exactly equal to k . Return 1 , else if j is still not equal to k , return 0 ; If sum != n and still j as exceeded , return 0 ; If that state is already calculated , return directly the ans ; Include the current prime ; Exclude the current prime ; Return by memoizing the ans ; Driver code ; Precompute primes by sieve"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; static boolean [ ] isprime = new boolean [ 1000 ] ; static int [ ] [ ] [ ] dp = new int [ 200 ] [ 20 ] [ 1000 ] ; static void sieve ( ) { for ( int i = 0 ; i < 1000 ; i ++ ) isprime [ i ] = true ; for ( int i = 2 ; i * i < 1000 ; i ++ ) { if ( isprime [ i ] ) { for ( int j = i * i ; j < 1000 ; j += i ) { isprime [ j ] = false ; } } } for ( int i = 2 ; i < 1000 ; i ++ ) { if ( isprime [ i ] ) { prime . add ( i ) ; } } } static int CountWays ( int i , int j , int sum , int n , int k ) { if ( i >= prime . size ( ) - 1 sum > n ) { return 0 ; } if ( sum == n ) { if ( j == k ) { return 1 ; } return 0 ; } if ( j == k ) return 0 ; if ( dp [ i ] [ j ] [ sum ] != 0 ) return dp [ i ] [ j ] [ sum ] ; int inc = 0 , exc = 0 ; inc = CountWays ( i + 1 , j + 1 , sum + prime . get ( i ) , n , k ) ; exc = CountWays ( i + 1 , j , sum , n , k ) ; return dp [ i ] [ j ] [ sum ] = inc + exc ; } public static void main ( String [ ] args ) { sieve ( ) ; int N = 100 , K = 5 ; System . out . println ( CountWays ( 0 , 0 , 0 , N , K ) ) ; } }


"Number of distinct words of size N with at most K contiguous vowels | Java program for the above approach ; Power function to calculate long powers with mod ; Function for finding number of ways to create string with length N and atmost K contiguous vowels ; Array dp to store number of ways ; dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] . . dp [ i - 1 ] [ k ] ) * 21 ; Now setting sum to be dp [ i ] [ 0 ] ; If j > i , no ways are possible to create a string with length i and vowel j ; If j = i all the character should be vowel ; dp [ i ] [ j ] relation with dp [ i - 1 ] [ j - 1 ] ; Adding dp [ i ] [ j ] in the sum ; Driver Code ; Input ; Function Call"
How can the above be solved in Java?

class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int kvowelwords ( int N , int K ) { int i , j ; int MOD = 1000000007 ; int [ ] [ ] dp = new int [ N + 1 ] [ K + 1 ] ; int sum = 1 ; for ( i = 1 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = sum * 21 ; dp [ i ] [ 0 ] %= MOD ; sum = dp [ i ] [ 0 ] ; for ( j = 1 ; j <= K ; j ++ ) { if ( j > i ) dp [ i ] [ j ] = 0 ; else if ( j == i ) { dp [ i ] [ j ] = power ( 5 , i , MOD ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] * 5 ; } dp [ i ] [ j ] %= MOD ; sum += dp [ i ] [ j ] ; sum %= MOD ; } } return sum ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 3 ; System . out . println ( kvowelwords ( N , K ) ) ; } }


"Number of divisors of a given number N which are divisible by K | Java program to count number of divisors of N which are divisible by K ; Function to count number of divisors of N which are divisible by K ; Variable to store count of divisors ; Traverse from 1 to n ; increase the count if both the conditions are satisfied ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countDivisors ( int n , int k ) { int count = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { if ( n % i == 0 && i % k == 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 12 , k = 3 ; System . out . println ( countDivisors ( n , k ) ) ; } }


"Number of edges in a perfect binary tree with N levels | Java implementation of the approach ; Function to return the count of edges in an n - level perfect binary tree ; Driver code"
How can the above be solved in Java?

class GFG { static int cntEdges ( int n ) { int edges = ( int ) Math . pow ( 2 , n ) - 2 ; return edges ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( cntEdges ( n ) ) ; } }


"Number of elements from the array which are reachable after performing given operations on D | Java implementation of the approach ; Function to return the GCD of a and b ; Function to return the count of reachable integers from the given array ; GCD of A and B ; To store the count of reachable integers ; If current element can be reached ; Return the count ; Driver code"
How can the above be solved in Java?

class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findReachable ( int [ ] arr , int D , int A , int B , int n ) { int gcd_AB = GCD ( A , B ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; int D = 4 , A = 4 , B = 6 ; System . out . println ( findReachable ( arr , D , A , B , n ) ) ; } }


"Number of elements less than or equal to a given number in a given subarray | Set 2 ( Including Updates ) | Number of elements less than or equal to a given number in a given subarray and allowing update operations . ; updating the bit array of a valid block ; answering the query ; traversing the first block in range ; Traversing completely overlapped blocks in range for such blocks bit array of that block is queried ; Traversing the last block ; Preprocessing the array ; updating the bit array at the original and new value of array ; Driver method ; size of block size will be equal to square root of n ; initialising bit array of each block as elements of array cannot exceed 10 ^ 4 so size of bit array is accordingly"
How can the above be solved in Java?

class Test { static final int MAX = 10001 ; static void update ( int idx , int blk , int val , int bit [ ] [ ] ) { for ( ; idx < MAX ; idx += ( idx & - idx ) ) bit [ blk ] [ idx ] += val ; } static int query ( int l , int r , int k , int arr [ ] , int blk_sz , int bit [ ] [ ] ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } while ( l + blk_sz <= r ) { int idx = k ; for ( ; idx > 0 ; idx -= idx & - idx ) sum += bit [ l / blk_sz ] [ idx ] ; l += blk_sz ; } while ( l <= r ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } return sum ; } static void preprocess ( int arr [ ] , int blk_sz , int n , int bit [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) update ( arr [ i ] , i / blk_sz , 1 , bit ) ; } static void preprocessUpdate ( int i , int v , int blk_sz , int arr [ ] , int bit [ ] [ ] ) { update ( arr [ i ] , i / blk_sz , - 1 , bit ) ; update ( v , i / blk_sz , 1 , bit ) ; arr [ i ] = v ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 2 , 3 , 4 } ; int blk_sz = ( int ) Math . sqrt ( arr . length ) ; int bit [ ] [ ] = new int [ blk_sz + 1 ] [ MAX ] ; preprocess ( arr , blk_sz , arr . length , bit ) ; System . out . println ( query ( 1 , 3 , 1 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 3 , 10 , blk_sz , arr , bit ) ; System . out . println ( query ( 3 , 3 , 4 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 2 , 1 , blk_sz , arr , bit ) ; preprocessUpdate ( 0 , 2 , blk_sz , arr , bit ) ; System . out . println ( query ( 0 , 4 , 5 , arr , blk_sz , bit ) ) ; } }


"Number of elements that can be seen from right side | Java program to find number of elements that can be seen from right side . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class Solution { static int numberOfElements ( int height [ ] , int n ) { int max_so_far = 0 ; int coun = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] > max_so_far ) { max_so_far = height [ i ] ; coun ++ ; } } return coun ; } public static void main ( String args [ ] ) { int n = 6 ; int height [ ] = { 4 , 8 , 2 , 0 , 0 , 5 } ; System . out . println ( numberOfElements ( height , n ) ) ; } }


"Number of elements with even factors in the given range | Java implementation of the above approach ; Function to count the perfect squares ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( m , 0.5 ) - ( int ) Math . pow ( n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . println ( " Count ▁ is ▁ " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ; } }


"Number of elements with odd factors in given range | Java program to count number of odd squares in given range [ n , m ] ; Function to count odd squares ; Driver code for above functions"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( ( double ) m , 0.5 ) - ( int ) Math . pow ( ( double ) n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . print ( " Count ▁ is ▁ " + countOddSquares ( n , m ) ) ; } }


"Number of even substrings in a string of digits | Java program to count number of substring which are even integer in a string of digits . ; Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver Program"
How can the above be solved in Java?

public class GFG { static int evenNumSubstring ( String str ) { int len = str . length ( ) ; int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int temp = str . charAt ( i ) - '0' ; if ( temp % 2 == 0 ) count += ( i + 1 ) ; } return count ; } public static void main ( String args [ ] ) { String str = "1234" ; System . out . println ( evenNumSubstring ( str ) ) ; } }


"Number of even substrings in a string of digits | Java program to count number of substring which are even integer in a string of digits . ; Return the even number substrings . ; If current digit is even , add count of substrings ending with it . The count is ( i + 1 ) ; Driver code"
How can the above be solved in Java?


"Number of factors of very large number N modulo M where M is any prime number | Java implementation to find the Number of factors of very large number N modulo M ; Function for modular multiplication ; Function to find the number of factors of large Number N ; Count the number of times 2 divides the number N ; Condition to check if 2 divides it ; Check for all the possible numbers that can divide it ; Loop to check the number of times prime number i divides it ; Condition to check if prime number i divides it ; Condition to check if N at the end is a prime number . ; Driver Code"
How can the above be solved in Java?

 class GFG { static long mod = 1000000007L ; static long mult ( long a , long b ) { return ( ( a % mod ) * ( b % mod ) ) % mod ; } static long calculate_factors ( long n ) { long ans , cnt ; cnt = 0 ; ans = 1 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } if ( cnt % 2 == 1 ) { ans = mult ( ans , ( cnt + 1 ) ) ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { cnt = 0 ; while ( n % i == 0 ) { cnt ++ ; n = n / i ; } if ( cnt % 2 == 1 ) { ans = mult ( ans , ( cnt + 1 ) ) ; } } if ( n > 2 ) { ans = mult ( ans , ( 2 ) ) ; } return ans % mod ; } public static void main ( String [ ] args ) { long n = 193748576239475639L ; mod = 17 ; System . out . print ( calculate_factors ( n ) + "
"); } }


"Number of flips to make binary string alternate | Set 1 | Java program to find minimum number of flip to make binary string alternate ; Utility method to flip a character ; Utility method to get minimum flips when alternate string starts with expected char ; if current character is not expected , increase flip count ; flip expected character each time ; method return minimum flip to make binary string alternate ; return minimum of following two 1 ) flips when alternate string starts with 0 2 ) flips when alternate string starts with 1 ; Driver code to test above method"
How can the above be solved in Java?

class GFG { public static char flip ( char ch ) { return ( ch == '0' ) ? '1' : '0' ; } public static int getFlipWithStartingCharcter ( String str , char expected ) { int flipCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) != expected ) flipCount ++ ; expected = flip ( expected ) ; } return flipCount ; } public static int minFlipToMakeStringAlternate ( String str ) { return Math . min ( getFlipWithStartingCharcter ( str , '0' ) , getFlipWithStartingCharcter ( str , '1' ) ) ; } public static void main ( String args [ ] ) { String str = "0001010111" ; System . out . println ( minFlipToMakeStringAlternate ( str ) ) ; } }


"Number of full binary trees such that each node is product of its children | Java program to find number of full binary tree such that each node is product of its children . ; Return the number of all possible full binary tree with given product property . ; Finding the minimum and maximum values in given array . ; Marking the presence of each array element and initialising the number of possible full binary tree for each integer equal to 1 because single node will also contribute as a full binary tree . ; From minimum value to maximum value of array finding the number of all possible Full Binary Trees . ; Find if value present in the array ; For each multiple of i , less than equal to maximum value of array ; If multiple is not present in the array then continue . ; Finding the number of possible Full binary trees for multiple j by multiplying number of possible Full binary tree from the number i and number of possible Full binary tree from i / j . ; Condition for possiblity when left chid became right child and vice versa . ; driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static int numoffbt ( int arr [ ] , int n ) { int maxvalue = - 2147483647 ; int minvalue = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { maxvalue = Math . max ( maxvalue , arr [ i ] ) ; minvalue = Math . min ( minvalue , arr [ i ] ) ; } int mark [ ] = new int [ maxvalue + 2 ] ; int value [ ] = new int [ maxvalue + 2 ] ; Arrays . fill ( mark , 0 ) ; Arrays . fill ( value , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ arr [ i ] ] = 1 ; value [ arr [ i ] ] = 1 ; } int ans = 0 ; for ( int i = minvalue ; i <= maxvalue ; i ++ ) { if ( mark [ i ] != 0 ) { for ( int j = i + i ; j <= maxvalue && j / i <= i ; j += i ) { if ( mark [ j ] == 0 ) continue ; value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; if ( i != j / i ) value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; } } ans += value [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 6 } ; int n = arr . length ; System . out . print ( numoffbt ( arr , n ) ) ; } }


"Number of horizontal or vertical line segments to connect 3 points | Java program to find number of horizontal ( or vertical line segments needed to connect three points . ; Function to check if the third point forms a rectangle with other two points at corners ; Returns true if point k can be used as a joining point to connect using two line segments ; Check for the valid polyline with two segments ; Check whether the X - coordinates or Y - cocordinates are same . ; Iterate over all pairs to check for two line segments ; Otherwise answer is three . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isBetween ( int a , int b , int c ) { return ( Math . min ( a , b ) <= c && c <= Math . max ( a , b ) ) ; } static boolean canJoin ( int x [ ] , int y [ ] , int i , int j , int k ) { return ( x [ k ] == x [ i ] x [ k ] == x [ j ] ) && isBetween ( y [ i ] , y [ j ] , y [ k ] ) || ( y [ k ] == y [ i ] y [ k ] == y [ j ] ) && isBetween ( x [ i ] , x [ j ] , x [ k ] ) ; } static int countLineSegments ( int x [ ] , int y [ ] ) { if ( ( x [ 0 ] == x [ 1 ] && x [ 1 ] == x [ 2 ] ) || ( y [ 0 ] == y [ 1 ] && y [ 1 ] == y [ 2 ] ) ) return 1 ; else if ( canJoin ( x , y , 0 , 1 , 2 ) || canJoin ( x , y , 0 , 2 , 1 ) || canJoin ( x , y , 1 , 2 , 0 ) ) return 2 ; else return 3 ; } public static void main ( String [ ] args ) { int x [ ] = new int [ 3 ] , y [ ] = new int [ 3 ] ; x [ 0 ] = - 1 ; y [ 0 ] = - 1 ; x [ 1 ] = - 1 ; y [ 1 ] = 3 ; x [ 2 ] = 4 ; y [ 2 ] = 3 ; System . out . println ( countLineSegments ( x , y ) ) ; } }


"Number of hours after which the second person moves ahead of the first person if they travel at a given speed | Java implementation of the above approach ; Function to return the number of hours for the second person to move ahead ; Time taken to equalize ; Time taken to move ahead ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int findHours ( int a , int b , int k ) { if ( a >= b ) return - 1 ; int time = k / ( b - a ) ; time = time + 1 ; return time ; } public static void main ( String [ ] args ) { int a = 4 , b = 5 , k = 1 ; System . out . println ( findHours ( a , b , k ) ) ; } }


"Number of index pairs such that s [ i ] and s [ j ] are anagrams | Java program to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; Function to find number of pairs of integers i , j such that s [ i ] is an anagram of s [ j ] . ; To store count of strings ; Traverse all strings and store in the map ; Sort the string ; Store in the map ; To store the number of pairs ; Traverse through the map ; Count the pairs for each string ; Return the required answer ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int anagram_pairs ( String [ ] s , int n ) { Map < String , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char [ ] chArr = s [ i ] . toCharArray ( ) ; Arrays . sort ( chArr ) ; s [ i ] = new String ( chArr ) ; if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < String , Integer > i : mp . entrySet ( ) ) { int k = i . getValue ( ) ; ans += ( k * ( k - 1 ) ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { String [ ] s = { " aaab " , " aaba " , " baaa " , " cde " , " dec " } ; int n = s . length ; System . out . println ( anagram_pairs ( s , n ) ) ; } }


"Number of indexes with equal elements in given range | Java program to count the number of indexes in range L R such that Ai = Ai + 1 ; Array to store count of index from 0 to i that obey condition ; precomputing prefixans [ ] array ; traverse to compute the prefixans [ ] array ; function that answers every query in O ( 1 ) ; Driver Code ; pre - computation ; 1 - st query ; 2 nd query"
How can the above be solved in Java?

class GFG { public static int N = 1000 ; static int prefixans [ ] = new int [ 1000 ] ; public static void countIndex ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } public static int answer_query ( int l , int r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = 9 ; countIndex ( a , n ) ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( L , R ) ) ; } }


"Number of indexes with equal elements in given range | Java program to count the number of indexes in range L R such that Ai = Ai + 1 ; function that answers every query in O ( r - l ) ; traverse from l to r and count the required indexes ; Driver Code ; 1 - st query ; 2 nd query"
How can the above be solved in Java?

class GFG { static int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = a . length ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( a , n , L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( a , n , L , R ) ) ; } }


"Number of indices pair such that element pair sum from first Array is greater than second Array | Java program to find the number of indices pair such that pair sum from first Array is greater than second Array ; Function to get the number of pairs of indices { i , j } in the given two arrays A and B such that A [ i ] + A [ j ] > B [ i ] + B [ j ] ; Intitializing the difference array D ; Computing the difference between the elements at every index and storing it in the array D ; Sort the array D ; Variable to store the total number of pairs that satisfy the given condition ; Loop to iterate through the difference array D and find the total number of pairs of indices that follow the given condition ; If the value at the index i is positive , then it remains positive for any pairs with j such that j > i . ; If the value at that index is negative then we need to find the index of the value just greater than - D [ i ] ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static long getPairs ( Vector < Integer > A , Vector < Integer > B , int n ) { int [ ] D = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { D [ i ] = A . get ( i ) - B . get ( i ) ; } Arrays . sort ( D ) ; long total = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( D [ i ] > 0 ) { total += n - i - 1 ; } else { int k = upper_bound ( D , 0 , D . length , - D [ i ] ) ; total += n - k ; } } return total ; } 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 n = 5 ; Vector < Integer > A = new Vector < Integer > ( ) ; Vector < Integer > B = new Vector < Integer > ( ) ; A . add ( 4 ) ; A . add ( 8 ) ; A . add ( 2 ) ; A . add ( 6 ) ; A . add ( 2 ) ; B . add ( 4 ) ; B . add ( 5 ) ; B . add ( 4 ) ; B . add ( 1 ) ; B . add ( 3 ) ; System . out . print ( getPairs ( A , B , n ) ) ; } }


"Number of integral solutions for equation x = b * ( sumofdigits ( x ) ^ a ) + c | Java program to find the numbers of values that satisfy the equation ; This function returns the sum of the digits of a number ; This function creates the array of valid numbers ; this computes s ( x ) ^ a ; this gives the result of equation ; checking if the sum same as i ; counter to keep track of numbers ; resultant array ; prints the number ; Driver Code ; calculate which value of x are possible"
How can the above be solved in Java?

import java . util . Vector ; class GFG { static int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } static void value ( int a , int b , int c ) { int co = 0 , p = 0 ; int no , r = 0 , x = 0 , q = 0 , w = 0 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < 82 ; i ++ ) { no = ( int ) Math . pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . add ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ; } }


"Number of intersections between two ranges | Java implementation of above approach ; Function to return total number of intersections ; Maximum possible number of intersections ; Store all starting points of type1 ranges ; Store all endpoints of type1 ranges ; Starting point of type2 ranges ; Ending point of type2 ranges ; Subtract those ranges which are starting after R ; Subtract those ranges which are ending before L ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int upperBound ( ArrayList < Integer > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a . get ( middle ) > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } static int FindIntersection ( ArrayList < ArrayList < Integer > > type1 , int n , ArrayList < ArrayList < Integer > > type2 , int m ) { int ans = n * m ; ArrayList < Integer > start = new ArrayList < Integer > ( ) ; ArrayList < Integer > end = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { start . add ( type1 . get ( i ) . get ( 0 ) ) ; end . add ( type1 . get ( i ) . get ( 1 ) ) ; } Collections . sort ( start ) ; Collections . sort ( end ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 . get ( i ) . get ( 0 ) ; int R = type2 . get ( i ) . get ( 1 ) ; ans -= start . size ( ) - upperBound ( start , 0 , start . size ( ) , R ) ; ans -= upperBound ( end , 0 , end . size ( ) , L - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > type1 = new ArrayList < ArrayList < Integer > > ( ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 5 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 7 ) ) ) ; ArrayList < ArrayList < Integer > > type2 = new ArrayList < ArrayList < Integer > > ( ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 7 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 5 , 7 ) ) ) ; int n = type1 . size ( ) ; int m = type2 . size ( ) ; System . out . println ( FindIntersection ( type1 , n , type2 , m ) ) ; } }


"Number of largest circles that can be inscribed in a rectangle | Java program for the above approach ; Function to count the number of largest circles in a rectangle ; If length exceeds breadth ; Swap to reduce length to smaller than breadth ; Return total count of circles inscribed ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int totalCircles ( int L , int B ) { if ( L > B ) { int temp = L ; L = B ; B = temp ; } return B / L ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 8 ; System . out . print ( totalCircles ( L , B ) ) ; } }


"Number of leading zeros in binary representation of a given number | Java program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Keep shifting x by one until leftmost bit does not become 1. ; Driver Code"
How can the above be solved in Java?

class GFG { static byte sizeofInt = 8 ; static int countZeros ( int x ) { int total_bits = sizeofInt * 8 ; int res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } public static void main ( String [ ] args ) { int x = 101 ; System . out . println ( countZeros ( x ) ) ; } }


"Number of leading zeros in binary representation of a given number | Java program of number of leading zeros in binary representation of a given number ; Function to count the no . of leading zeros ; Main function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countZeros ( int x ) { int y ; int n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } public static void main ( String [ ] args ) { int x = 101 ; System . out . println ( countZeros ( x ) ) ; } }


"Number of lines from given N points not parallel to X or Y axis | Java program to find the number of lines which are formed from given N points and not parallel to X or Y axis ; Function to find the number of lines which are formed from given N points and not parallel to X or Y axis ; This will store the number of points has same x or y coordinates using the map as the value of coordinate can be very large ; Counting frequency of each x and y coordinates ; Total number of pairs can be formed ; We can not choose pairs from these as they have same x coordinatethus they will result line segment parallel to y axis ; we can not choose pairs from these as they have same y coordinate thus they will result line segment parallel to x - axis ; Return the required answer ; Driver Code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int NotParallel ( int p [ ] [ ] , int n ) { HashMap < Integer , Integer > x_axis = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > y_axis = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( x_axis . containsKey ( p [ i ] [ 0 ] ) ) x_axis . put ( p [ i ] [ 0 ] , x_axis . get ( p [ i ] [ 0 ] ) + 1 ) ; else x_axis . put ( p [ i ] [ 0 ] , 1 ) ; if ( y_axis . containsKey ( p [ i ] [ 1 ] ) ) y_axis . put ( p [ i ] [ 1 ] , y_axis . get ( p [ i ] [ 1 ] ) + 1 ) ; else y_axis . put ( p [ i ] [ 1 ] , 1 ) ; } int total = ( n * ( n - 1 ) ) / 2 ; for ( Map . Entry < Integer , Integer > i : x_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } for ( Map . Entry < Integer , Integer > i : y_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } public static void main ( String [ ] args ) { int p [ ] [ ] = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = p . length ; System . out . print ( NotParallel ( p , n ) ) ; } }


"Number of local extrema in an array | Java to find number of extrema ; function to find local extremum ; start loop from position 1 till n - 1 ; check if a [ i ] is greater than both its neighbours then add 1 to x ; check if a [ i ] is less than both its neighbours , then add 1 to x ; driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int extrema ( int a [ ] , int n ) { int count = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } public static void main ( String args [ ] ) throws IOException { int a [ ] = { 1 , 0 , 2 , 1 } ; int n = a . length ; System . out . println ( extrema ( a , n ) ) ; } }


"Number of mismatching bits in the binary representation of two integers | Java implementation of the approach ; compute number of different bits ; since , the numbers are less than 2 ^ 31 run the loop from '0' to '31' only ; right shift both the numbers by ' i ' and check if the bit at the 0 th position is different ; Driver code ; find number of different bits"
How can the above be solved in Java?

import java . io . * ; class GFG { static void solve ( int A , int B ) { int count = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } System . out . println ( " Number ▁ of ▁ different ▁ bits ▁ : ▁ " + count ) ; } public static void main ( String [ ] args ) { int A = 12 , B = 15 ; solve ( A , B ) ; } }


"Number of moves required to guess a permutation . | Java implementation of the approach ; Function that returns the required moves ; Final move ; Driver Program to test above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countMoves ( int n ) { int ct = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ct += i * ( n - i ) ; ct += n ; return ct ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countMoves ( n ) ) ; } }


"Number of n digit numbers that do not contain 9 | Java program to find number of n digit numbers that do not contain 9 as it 's digit ; function to find number of n digit numbers possible ; Driver Code"
How can the above be solved in Java?

import java . io . * ; public class GFG { static int totalNumber ( int n ) { return 8 * ( int ) Math . pow ( 9 , n - 1 ) ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( totalNumber ( n ) ) ; } }


"Number of n digit stepping numbers | Java program to calculate the number of n digit stepping numbers . ; function that calculates the answer ; dp [ i ] [ j ] stores count of i digit stepping numbers ending with digit j . ; if n is 1 then answer will be 10. ; Initialize values for count of digits equal to 1. ; Compute values for count of digits more than 1. ; If ending digit is 0 ; If ending digit is 9 ; For other digits . ; stores the final answer ; driver program to test the above function"
How can the above be solved in Java?

class GFG { static long answer ( int n ) { int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; else if ( j == 9 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ n ] [ j ] ; return sum ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( answer ( n ) ) ; } }


"Number of n | Java program To calculate Number of n - digits non - decreasing integers ; Returns factorial of n ; returns nCr ; Driver code"
How can the above be solved in Java?

class GFG { static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static long nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( " Number ▁ of ▁ Non - Decreasing ▁ digits : ▁ " + nCr ( n + 9 , 9 ) ) ; } }


"Number of n | Java program for counting n digit numbers with non decreasing digits ; Function that returns count of non - decreasing numbers with n digits ; a [ i ] [ j ] = count of all possible number with i digits having leading digit as j ; Initialization of all 0 - digit number ; Initialization of all i - digit non - decreasing number leading with 9 ; for all digits we should calculate number of ways depending upon leading digits ; driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int nonDecNums ( int n ) { int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ; return a [ n ] [ 0 ] ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( " Non - decreasing ▁ digits ▁ = ▁ " + nonDecNums ( n ) ) ; } }


"Number of non | A naive Java solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Initialize result ; Consider all triplets and increment result whenever sum of a triplet is n . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countIntegralSolutions ( n ) ) ; } }


"Number of non | Java implementation of the approach ; Function to return the count of increasing subarrays of length k ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Updating the required count ; Updating i ; Returning res ; Driver code"
How can the above be solved in Java?

class GFG { static int cntSubArrays ( int [ ] arr , int n , int k ) { int res = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; res += Math . max ( j - i - k + 1 , 0 ) ; i = j ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 } ; int n = arr . length ; int k = 2 ; System . out . println ( cntSubArrays ( arr , n , k ) ) ; } }


"Number of non | Java implementation of the approach ; Function to return the required count ; To store the final result ; Two pointer loop ; Initialising j ; Looping till the subarray increases ; Update ret ; Update i ; Return ret ; Driver code"
How can the above be solved in Java?

class GFG { static int findCnt ( int [ ] arr , int n , int k ) { int ret = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; int x = Math . max ( 0 , j - i - k + 1 ) ; ret += ( x * ( x + 1 ) ) / 2 ; i = j ; } return ret ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int n = arr . length ; int k = 2 ; System . out . println ( findCnt ( arr , n , k ) ) ; } }


"Number of non | Java program to find the numbers of non negative integral solutions ; return number of non negative integral solutions ; initialize total = 0 ; Base Case if n = 1 and val >= 0 then it should return 1 ; iterate the loop till equal the val ; total solution of equations and again call the recursive function Solutions ( variable , value ) ; return the total no possible solution ; Driver code"
How can the above be solved in Java?

class GFG { static int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } public static void main ( String [ ] args ) { int n = 5 ; int val = 20 ; System . out . print ( countSolutions ( n , val ) ) ; } }


"Number of non | Java solution to count solutions of a + b + c = n ; Returns count of solutions of a + b + c = n ; Driver code"
How can the above be solved in Java?

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


"Number of occurrences of 2 as a digit in numbers from 0 to n | Java code for above implementation ; Driver code"
How can the above be solved in Java?

class GFG { static int numberOf2sinRange ( int n ) { String s = " " ; for ( int i = 0 ; i < n + 1 ; i ++ ) s += String . valueOf ( i ) ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '2' ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( numberOf2sinRange ( n ) ) ; } }


"Number of occurrences of 2 as a digit in numbers from 0 to n | Java program to count 2 s from 0 to n ; Counts the number of '2' digits in a single number ; Counts the number of '2' digits between 0 and n ; Initialize result ; Count 2 's in every number  from 2 to n ; Driver code"
How can the above be solved in Java?

class GFG { static int number0f2s ( int n ) { int count = 0 ; while ( n > 0 ) { if ( n % 10 == 2 ) count ++ ; n = n / 10 ; } return count ; } static int numberOf2sinRange ( int n ) { int count = 0 ; for ( int i = 2 ; i <= n ; i ++ ) count += number0f2s ( i ) ; return count ; } public static void main ( String [ ] args ) { System . out . print ( numberOf2sinRange ( 22 ) ) ; System . out . println ( ) ; System . out . print ( numberOf2sinRange ( 100 ) ) ; } }


"Number of occurrences of 2 as a digit in numbers from 0 to n | Java program to count 2 s from 0 to n ; Counts the number of 2 s in a number at d - th digit ; if the digit in spot digit is ; Counts the number of '2' digits between 0 and n ; Convert integer to String to find its length ; Traverse every digit and count for every digit ; Driver Code"
How can the above be solved in Java?

class GFG { static int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) Math . pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } static int numberOf2sinRange ( int number ) { String convert ; convert = String . valueOf ( number ) ; String s = convert ; int len = s . length ( ) ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } public static void main ( String [ ] args ) { System . out . println ( numberOf2sinRange ( 22 ) ) ; System . out . println ( numberOf2sinRange ( 100 ) ) ; } }


"Number of occurrences of a given angle formed using 3 vertices of a n | Java implementation of the approach ; Function that calculates occurrences of given angle that can be created using any 3 sides ; Maximum angle in a regular n - gon is equal to the interior angle If the given angle is greater than the interior angle then the given angle cannot be created ; The given angle times n should be divisible by 180 else it cannot be created ; Initialise answer ; Calculate the frequency of given angle for each vertex ; Multiply answer by frequency . ; Multiply answer by the number of vertices . ; Driver code"
How can the above be solved in Java?

class GFG { static int solve ( int ang , int n ) { if ( ( ang * n ) > ( 180 * ( n - 2 ) ) ) { return 0 ; } else if ( ( ang * n ) % 180 != 0 ) { return 0 ; } int ans = 1 ; int freq = ( ang * n ) / 180 ; ans = ans * ( n - 1 - freq ) ; ans = ans * n ; return ans ; } public static void main ( String [ ] args ) { int ang = 90 , n = 4 ; System . out . println ( solve ( ang , n ) ) ; } }


"Number of odd and even results for every value of x in range [ min , max ] after performing N steps | Java program to print Number of odd / even results for every value of x in range [ min , end ] after performing N steps ; Function that prints the number of odd and even results ; If constant at layer i is even , beven is true , otherwise false . If the coefficient of x at layer i is even , aeven is true , otherwise false . ; If any of the coefficients at any layer is found to be even , then the product of all the coefficients will always be even . ; Checking whether the constant added after all layers is even or odd . ; Assuming input x is even . ; Assuming input x is odd . ; Displaying the counts . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void count_even_odd ( int min , int max , int steps [ ] [ ] ) { int a , b , even , odd ; boolean beven = true , aeven = false ; int n = 2 ; for ( int i = 0 ; i < n ; i ++ ) { a = steps [ i ] [ 0 ] ; b = steps [ i ] [ 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } else { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; else odd += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; System . out . print ( " even ▁ = ▁ " + even + " , ▁ odd ▁ = ▁ " + odd ) ; } public static void main ( String [ ] args ) { int min = 1 , max = 4 ; int steps [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; count_even_odd ( min , max , steps ) ; } }


"Number of ones in the smallest repunit | Java program to print the number of 1 s in smallest repunit multiple of the number . ; Function to find number of 1 s in smallest repunit multiple of the number ; to store number of 1 s in smallest repunit multiple of the number . ; initialize rem with 1 ; run loop until rem becomes zero ; rem * 10 + 1 here represents the repunit modulo n ; when remainder becomes 0 return count ; Driver Code ; Calling function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countOnes ( int n ) { int count = 1 ; int rem = 1 ; while ( rem != 0 ) { rem = ( rem * 10 + 1 ) % n ; count ++ ; } return count ; } public static void main ( String [ ] args ) { int n = 13 ; System . out . println ( countOnes ( n ) ) ; } }


"Number of open doors | TCS Coding Question | package whatever do not write package name here ; Function that counts the number of doors opens after the Nth turn ; Find the number of open doors ; Return the resultant count of open doors ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countOpenDoors ( int N ) { int doorsOpen = ( int ) Math . sqrt ( N ) ; return doorsOpen ; } public static void main ( String [ ] args ) { int N = 100 ; System . out . println ( countOpenDoors ( N ) ) ; } }


"Number of ordered pairs such that ( Ai & Aj ) = 0 | Java program to calculate the number of ordered pairs such that their bitwise and is zero ; Efficient function to count pairs ; Stores the frequency of each number ; Count the frequency of every element ; Iterate for al possible values that a [ i ] can be ; If the last bit is ON ; is the last bit is OFF ; Iterate till n ; If mask 's ith bit is set ; If mask 's ith bit is not set ; Iterate for all the array element and count the number of pairs ; return answer ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int N = 15 ; public static int countPairs ( int a [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; int dp [ ] [ ] = new int [ 1 << N ] [ N + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash . containsKey ( a [ i ] ) ) { hash . replace ( a [ i ] , hash . get ( a [ i ] ) + 1 ) ; } else { hash . put ( a [ i ] , 1 ) ; } } for ( int mask = 0 ; mask < ( 1 << N ) ; ++ mask ) { if ( ( mask & 1 ) != 0 ) { if ( hash . containsKey ( mask ) ) { dp [ mask ] [ 0 ] = hash . get ( mask ) ; } if ( hash . containsKey ( mask ^ 1 ) ) { dp [ mask ] [ 0 ] += hash . get ( mask ^ 1 ) ; } } else { if ( hash . containsKey ( mask ) ) { dp [ mask ] [ 0 ] = hash . get ( mask ) ; } } for ( int i = 1 ; i <= N ; ++ i ) { if ( ( mask & ( 1 << i ) ) != 0 ) { dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] + dp [ mask ^ ( 1 << i ) ] [ i - 1 ] ; } else { dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] ; } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] ] [ N ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 6 } ; int n = a . length ; System . out . print ( countPairs ( a , n ) ) ; } }


"Number of ordered pairs such that ( Ai & Aj ) = 0 | Java program to calculate the number of ordered pairs such that their bitwise and is zero ; Naive function to count the number of ordered pairs such that their bitwise and is 0 ; check for all possible pairs ; add 2 as ( i , j ) and ( j , i ) are considered different ; Driver Code"
How can the above be solved in Java?

class GFG { static int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( a [ i ] & a [ j ] ) == 0 ) count += 2 ; } return count ; } public static void main ( String arg [ ] ) { int a [ ] = { 3 , 4 , 2 } ; int n = a . length ; System . out . print ( countPairs ( a , n ) ) ; } }


"Number of ordered points pair satisfying line equation | Java code to find number of ordered points satisfying line equation ; Checks if ( i , j ) is valid , a point ( i , j ) is valid if point ( arr [ i ] , arr [ j ] ) satisfies the equation y = mx + c And i is not equal to j ; check if i equals to j ; Equation LHS = y , and RHS = mx + c ; Returns the number of ordered pairs ( i , j ) for which point ( arr [ i ] , arr [ j ] ) satisfies the equation of the line y = mx + c ; for every possible ( i , j ) check if ( a [ i ] , a [ j ] ) satisfies the equation y = mx + c ; ( firstIndex , secondIndex ) is same as ( i , j ) ; check if ( firstIndex , secondIndex ) is a valid point ; Driver Code ; equation of line is y = mx + c"
How can the above be solved in Java?

import java . io . * ; public class GFG { static boolean isValid ( int [ ] arr , int i , int j , int m , int c ) { if ( i == j ) return false ; int lhs = arr [ j ] ; int rhs = m * arr [ i ] + c ; return ( lhs == rhs ) ; } static int findOrderedPoints ( int [ ] arr , int n , int m , int c ) { int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int firstIndex = i , secondIndex = j ; if ( isValid ( arr , firstIndex , secondIndex , m , c ) ) counter ++ ; } } return counter ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , 2 } ; int n = arr . length ; int m = 1 , c = 1 ; System . out . print ( findOrderedPoints ( arr , n , m , c ) ) ; } }


"Number of pair of positions in matrix which are not accessible | Java program to count number of pair of positions in matrix which are not accessible ; Counts number of vertices connected in a component containing x . Stores the count in k . ; Incrementing the number of node in a connected component . ; Return the number of count of non - accessible cells . ; Initialize count of connected vertices found by DFS starting from i . ; Update result ; Inserting the edge between edge . ; Mapping the cell coordinate into node number . ; Inserting the edge . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int k ; static void dfs ( Vector < Integer > graph [ ] , boolean visited [ ] , int x ) { for ( int i = 0 ; i < graph [ x ] . size ( ) ; i ++ ) { if ( ! visited [ graph [ x ] . get ( i ) ] ) { ( k ) ++ ; visited [ graph [ x ] . get ( i ) ] = true ; dfs ( graph , visited , graph [ x ] . get ( i ) ) ; } } } static int countNonAccessible ( Vector < Integer > graph [ ] , int N ) { boolean [ ] visited = new boolean [ N * N + N ] ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i ) ; ans += k * ( N * N - k ) ; } } return ans ; } static void insertpath ( Vector < Integer > graph [ ] , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . add ( b ) ; graph [ b ] . add ( a ) ; } public static void main ( String args [ ] ) { int N = 2 ; Vector [ ] graph = new Vector [ N * N + 1 ] ; for ( int i = 1 ; i <= N * N ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; System . out . println ( countNonAccessible ( graph , N ) ) ; } }


"Number of pairs from the first N natural numbers whose sum is divisible by K | Java implementation of the approach ; Function to find the number of pairs from the set of natural numbers up to N whose sum is divisible by K ; Declaring a Hash to store count ; Storing the count of integers with a specific remainder in Hash array ; Check if K is even ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Count of pairs when both the remainders are K / 2 ; Count of pairs when both integers are divisible by K ; Count of pairs when one remainder is R and other remainder is K - R ; Driver code ; Print the count of pairs"
How can the above be solved in Java?

class GfG { static int findPairCount ( int N , int K ) { int count = 0 ; int rem [ ] = new int [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } public static void main ( String [ ] args ) { int N = 10 , K = 4 ; System . out . println ( findPairCount ( N , K ) ) ; } }


"Number of pairs in an array having sum equal to product | Java implementation of the approach ; Function to return the count of the required pairs ; Find the count of 0 s and 2 s in the array ; Find the count of required pairs ; Return the count ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . length ; System . out . print ( sumEqualProduct ( a , n ) ) ; } }


"Number of pairs such that path between pairs has the two vertices A and B | Java program to find the number of pairs such that the path between every pair contains two given vertices ; Function to perform DFS on the given graph by fixing the a vertex ; To mark a particular vertex as visited ; Variable to store the count of the vertices which can be reached from a ; Performing the DFS by iterating over the visited array ; If the vertex is not visited and removing the vertex b ; Function to return the number of pairs such that path between any two pairs consists of the given two vertices A and B ; Initializing the visited array and assigning it with 0 's ; Initially , the count of vertices is 0 ; Performing DFS by removing the vertex B ; Count the vertices which cannot be reached after removing the vertex B ; Again reinitializing the visited array ; Setting the count of vertices to 0 to perform the DFS again ; Performing the DFS by removing the vertex A ; Count the vertices which cannot be reached after removing the vertex A ; Multiplying both the vertices set ; Driver code ; Loop to store the graph"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int N = 1000001 ; static int c , n , m , a , b ; static void dfs ( int a , int b , Vector < Integer > v [ ] , int vis [ ] ) { vis [ a ] = 1 ; c ++ ; for ( int i : v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } static void Calculate ( Vector < Integer > v [ ] ) { int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( a , b , v , vis ) ; int ans1 = n - c - 1 ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( b , a , v , vis ) ; int ans2 = n - c - 1 ; System . out . print ( ans1 * ans2 + "
"); } public static void main ( String [ ] args ) { n = 7 ; m = 7 ; a = 3 ; b = 5 ; int edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 7 , 5 } } ; Vector < Integer > [ ] v = new Vector [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { v [ i ] = new Vector < Integer > ( ) ; } for ( int i = 0 ; i < m ; i ++ ) { v [ edges [ i ] [ 0 ] ] . add ( edges [ i ] [ 1 ] ) ; v [ edges [ i ] [ 1 ] ] . add ( edges [ i ] [ 0 ] ) ; } Calculate ( v ) ; } }


"Number of pairs such that their HCF and LCM is equal | Java program to count number of pairs such that their hcf and lcm are equal ; Function to count number of pairs such that their hcf and lcm are equal ; Store frequencies of array elements ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] ; Count of pairs ( arr [ i ] , arr [ j ] ) where arr [ i ] = arr [ j ] , ; Driver function"
How can the above be solved in Java?

import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > frequency = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( frequency . containsKey ( arr [ i ] ) ) { frequency . put ( arr [ i ] , frequency . get ( arr [ i ] ) + 1 ) ; } else { frequency . put ( arr [ i ] , 1 ) ; } } int count = 0 ; for ( Map . Entry < Integer , Integer > x : frequency . entrySet ( ) ) { int f = x . getValue ( ) ; count += f * ( f - 1 ) / 2 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }


"Number of pairs such that their HCF and LCM is equal | Naive Java program to count number of pairs such that their hcf and lcm are equal ; Function to return HCF of two numbers ; Function to return LCM of two numbers ; Returns the number of valid pairs ; Traversing the array . For each array element , checking if it follow the condition ; Driver function"
How can the above be solved in Java?

import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int countPairs ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }


"Number of pairs whose product is a power of 2 | Java program for the above approach ; Function to count pairs having product equal to a power of 2 ; Stores count of array elements which are power of 2 ; If array element contains only one set bit ; Increase count of powers of 2 ; Count required number of pairs ; Print the required number of pairs ; Driver Code ; Given array ; Size of the array ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Integer . bitCount ( arr [ i ] ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; System . out . print ( desiredPairs + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 7 , 2 } ; int N = arr . length ; countPairs ( arr , N ) ; } }


"Number of pairs whose sum is a power of 2 | Java implementation of above approach ; Function to return the count of valid pairs ; Storing occurrences of each element ; Start taking largest element each time ; If element has already been paired ; Find the number which is greater than a [ i ] and power of two ; If there is a number which adds up with a [ i ] to form a power of two ; Edge case when a [ i ] and crr - a [ i ] is same and we have only one occurrence of a [ i ] then it cannot be paired ; Remove already paired elements ; Return the count ; Driver code"
How can the above be solved in Java?

import java . util . TreeMap ; class Count { static int countPairs ( int [ ] a , int n ) { TreeMap < Integer , Integer > map = new TreeMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { map . put ( a [ i ] , 1 ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . get ( a [ i ] ) < 1 ) continue ; int cur = 1 ; while ( cur <= a [ i ] ) cur <<= 1 ; if ( map . containsKey ( cur - a [ i ] ) ) { if ( cur - a [ i ] == a [ i ] && map . get ( a [ i ] ) == 1 ) continue ; count ++ ; map . put ( cur - a [ i ] , map . get ( cur - a [ i ] ) - 1 ) ; map . put ( a [ i ] , map . get ( a [ i ] ) - 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 11 , 14 , 5 , 13 } ; int n = a . length ; System . out . println ( countPairs ( a , n ) ) ; } }


"Number of pairs whose sum is a power of 2 | Set 2 | Java program to implement the above approach ; Function to count all pairs whose sum is power of two ; Stores the frequency of each element of the array ; Update the frequency of array elements ; Stores the count of pairs ; Current power of 2 ; Traverse the array ; Increment count of pairs ; Return count of pairs ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int countPair ( int [ ] arr , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) m . put ( arr [ i ] , m . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < 31 ; i ++ ) { int key = ( int ) Math . pow ( 2 , i ) ; for ( int j = 0 ; j < arr . length ; j ++ ) { int k = key - arr [ j ] ; ans += m . getOrDefault ( k , 0 ) ; if ( k == arr [ j ] ) ans ++ ; } } return ans / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , - 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( countPair ( arr , n ) ) ; } }


"Number of pairs with Bitwise OR as Odd number | Java program to count pairs with odd OR ; Function to count pairs with odd OR ; Count total even numbers in array ; Even pair count ; Total pairs ; Return Odd pair count ; Driver main"
How can the above be solved in Java?

public class GFG { static int countOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 != 1 ) ) { count ++ ; } } int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( countOddPair ( A , N ) ) ; } }


"Number of pairs with Bitwise OR as Odd number | Java program to count pairs with odd OR ; Function to count pairs with odd OR ; find OR operation check odd or odd ; return count of odd pair ; Driver Code"
How can the above be solved in Java?

class GFG { static int findOddPair ( int A [ ] , int N ) { int oddPair = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( findOddPair ( A , N ) ) ; } }


"Number of pairs with Pandigital Concatenation | Java program to count PanDigital pairs ; Stores digits present in String v [ i ] atleast once . We use a set as we only need digits which exist only once ( irrespective of reputation ) ; Calculate the mask by considering all digits existing atleast once ; Increment the frequency of this mask ; Returns number of pairs of Strings resulting in Pandigital Concatenations ; All possible Strings lie between 1 and 1023 so we iterate over every possible mask ; If the concatenation results in mask of Pandigital Concatenation , calculate all pairs formed with Masks i and j ; Since every pair is considers twice , we get rid of half of these ; Find frequencies of all masks in given vector of Strings ; Return all possiblg concatenations . ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( Vector < String > v , HashMap < Integer , Integer > freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; HashSet < Integer > digits = new HashSet < > ( ) ; for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) digits . add ( v . get ( i ) . charAt ( j ) - '0' ) ; for ( int it : digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . containsKey ( mask ) ) { freq . put ( mask , freq . get ( mask ) + 1 ) ; } else { freq . put ( mask , 1 ) ; } } } static int pandigitalConcatenations ( HashMap < Integer , Integer > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . containsKey ( j ) && freq . containsKey ( i ) ) { if ( i == j ) ans += ( freq . get ( i ) * ( freq . get ( i ) - 1 ) ) ; else ans += ( freq . get ( i ) * freq . get ( j ) ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( Vector < String > v ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( v ) + "
"); } }


"Number of pairs with Pandigital Concatenation | Java program to find all Pandigital concatenations of two strings . ; Checks if a given string is Pandigital ; digit i is not present thus not pandigital ; Returns number of pairs of strings resulting in Pandigital Concatenations ; iterate over all pair of strings ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > v = new ArrayList < String > ( ) ; static int isPanDigital ( String s ) { int digits [ ] = new int [ 10 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) digits [ s . charAt ( i ) - ( int ) '0' ] = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( digits [ i ] == 0 ) return 0 ; return 1 ; } static int countPandigitalPairs ( ) { int pairs = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) if ( isPanDigital ( v . get ( i ) + v . get ( j ) ) == 1 ) pairs ++ ; return pairs ; } public static void main ( String args [ ] ) { v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( ) ) ; } }


"Number of pairs with maximum sum | Java program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; Find maximum and second maximum elements . Also find their counts . ; If maximum element appears more than once . ; If maximum element appears only once . ; driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int sum ( int a [ ] , int n ) { int maxVal = a [ 0 ] , maxCount = 1 ; int secondMax = Integer . MIN_VALUE , secondMaxCount = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * ( maxCount - 1 ) / 2 ; return secondMaxCount ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }


"Number of pairs with maximum sum | Java program to count pairs with maximum sum . ; function to find the number of maximum pair sums ; traverse through all the pairs ; traverse through all pairs and keep a count of the number of maximum pairs ; driver program to test the above function"
How can the above be solved in Java?

class GFG { static int sum ( int a [ ] , int n ) { int maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) maxSum = Math . max ( maxSum , a [ i ] + a [ j ] ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }


"Number of palindromic permutations | Set 1 | Java program to find number of palindromic permutations of a given string ; Returns factorial of n ; Returns count of palindromic permutations of str . ; Count frequencies of all characters ; Since half of the characters decide count of palindromic permutations , we take ( n / 2 ) ! ; To make sure that there is at most one odd occurring char ; Traverse through all counts ; To make sure that the string can permute to form a palindrome ; If there are more than one odd occurring chars ; Divide all permutations with repeated characters ; Driver code"
How can the above be solved in Java?

class GFG { static final int MAX = 256 ; static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int countPalinPermutations ( String str ) { int n = str . length ( ) ; int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str . charAt ( i ) ] ++ ; long res = fact ( n / 2 ) ; boolean oddFreq = false ; for ( int i = 0 ; i < MAX ; i ++ ) { int half = freq [ i ] / 2 ; if ( freq [ i ] % 2 != 0 ) { if ( oddFreq == true ) return 0 ; oddFreq = true ; } res = res / fact ( half ) ; } return ( int ) res ; } public static void main ( String [ ] args ) { String str = " gffg " ; System . out . print ( countPalinPermutations ( str ) ) ; } }


"Number of palindromic subsequences of length k where k <= 3 | Java program to count number of subsequences of given length . ; Precompute the prefix and suffix array . ; Precompute the prefix 2D array ; Precompute the Suffix 2D array . ; Find the number of palindromic subsequence of length k ; If k is 1. ; If k is 2 ; Adding all the products of prefix array ; For k greater than 2. Adding all the products of value of prefix and suffix array . ; Driver code"
How can the above be solved in Java?

class GFG { static final int MAX = 100 ; static final int MAX_CHAR = 26 ; static void precompute ( String s , int n , int l [ ] [ ] , int r [ ] [ ] ) { l [ s . charAt ( 0 ) - ' a ' ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) l [ j ] [ i ] += l [ j ] [ i - 1 ] ; l [ s . charAt ( i ) - ' a ' ] [ i ] ++ ; } r [ s . charAt ( n - 1 ) - ' a ' ] [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) r [ j ] [ i ] += r [ j ] [ i + 1 ] ; r [ s . charAt ( i ) - ' a ' ] [ i ] ++ ; } } static int countPalindromes ( int k , int n , int l [ ] [ ] , int r [ ] [ ] ) { int ans = 0 ; if ( k == 1 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += l [ i ] [ n - 1 ] ; return ans ; } if ( k == 2 ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) ans += ( ( l [ i ] [ n - 1 ] * ( l [ i ] [ n - 1 ] - 1 ) ) / 2 ) ; return ans ; } for ( int i = 1 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < MAX_CHAR ; j ++ ) ans += l [ j ] [ i - 1 ] * r [ j ] [ i + 1 ] ; return ans ; } public static void main ( String [ ] args ) { String s = " aabab " ; int k = 2 ; int n = s . length ( ) ; int l [ ] [ ] = new int [ MAX_CHAR ] [ MAX ] ; int r [ ] [ ] = new int [ MAX_CHAR ] [ MAX ] ; precompute ( s , n , l , r ) ; System . out . println ( countPalindromes ( k , n , l , r ) ) ; } }


"Number of perfect cubes between two given numbers | A Simple Method to count cubes between a and b ; Function to count cubes between two numbers ; Traverse through all numbers ; Check if current number ' i ' is perfect cube ; Driver code"
How can the above be solved in Java?

class GFG { static int countCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j * j <= i ; j ++ ) if ( j * j * j == i ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int a = 7 , b = 30 ; System . out . print ( " Count ▁ of ▁ Cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }


"Number of perfect cubes between two given numbers | An Efficient Method to count cubes between a and b ; Function to count cubes between two numbers ; Driver code"
How can the above be solved in Java?

class GFG { static int countCubes ( int a , int b ) { return ( int ) ( Math . floor ( Math . cbrt ( b ) ) - Math . ceil ( Math . cbrt ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 7 , b = 28 ; System . out . print ( " Count ▁ of ▁ cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }


"Number of perfect squares between two given numbers | An efficient solution to count square between a and b ; Driver Code"
How can the above be solved in Java?

class CountSquares { double countSquares ( int a , int b ) { return ( Math . floor ( Math . sqrt ( b ) ) - Math . ceil ( Math . sqrt ( a ) ) + 1 ) ; } } public class PerfectSquares { public static void main ( String [ ] args ) { int a = 9 , b = 25 ; CountSquares obj = new CountSquares ( ) ; System . out . print ( " Count ▁ of ▁ squares ▁ is ▁ " + ( int ) obj . countSquares ( a , b ) ) ; } }


"Number of permutation with K inversions | Java program to find number of permutation with K inversion using Memoization ; Limit on N and K ; 2D array memo for stopping solving same problem again ; method recursively calculates permutation with K inversion ; base cases ; if already solved then return result directly ; calling recursively all subproblem of permutation size N - 1 ; Call recursively only if total inversion to be made are less than size ; store result into memo ; Driver code to test above methods"
How can the above be solved in Java?

import java . io . * ; class GFG { static int M = 100 ; static int memo [ ] [ ] = new int [ M ] [ M ] ; static int numberOfPermWithKInversion ( int N , int K ) { if ( N == 0 ) return 0 ; if ( K == 0 ) return 1 ; if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ; int sum = 0 ; for ( int i = 0 ; i <= K ; i ++ ) { if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ; } memo [ N ] [ K ] = sum ; return sum ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 2 ; System . out . println ( numberOfPermWithKInversion ( N , K ) ) ; } }


"Number of permutations of a string in which all the occurrences of a given character occurs together | Java implementation of above approach ; Function to return factorial of the number passed as argument ; Function to get the total permutations which satisfy the given condition ; Create has to store count of each character ; Store character occurrences ; Count number of times Particular character comes ; If particular character isn 't  present in the string then return 0 ; Remove count of particular character ; Total length of the string ; Assume all occurrences of particular character as a single character . ; Compute factorial of the length ; Divide by the factorials of the no . of occurrences of all the characters . ; return the result ; Driver Code ; Assuming the string and the character are all in uppercase"
How can the above be solved in Java?

import java . util . * ; class solution { static int fact ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } static int getResult ( String str , char ch ) { int has [ ] = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) has [ str . charAt ( i ) - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . length ( ) ; total = total - particular + 1 ; int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } public static void main ( String args [ ] ) { String str = " MISSISSIPPI " ; System . out . println ( getResult ( str , ' S ' ) ) ; } }


"Number of points lying inside a rectangle as well as a triangle | Java program for the above approach ; Function to calculate area of a triangle ; Return the resultant area ; Function to check if a point lies inside a triangle or not ; Calculate area of triangle ABC ; Calculate area of triangle formed by connecting B , C , point ; Calculate area of triangle formed by connecting A , C , point ; Calculate area of triangle formed by connecting A , B , point ; Check if the sum of the areas of above three triangles the same as ABC ; Function to count the number of points lying inside a triangle & rectangle ; Stores the coordinates of the vertices of the triangles ; Stores the number of points lying inside the triangle and rectangle ; Traverse the array of points ; Stores whether the current point lies inside triangle1 or not ; Stores whether the current point lies inside triangle2 or not ; Stores whether the current point lies inside triangle3 or not ; Stores whether the current point lies inside triangle4 or not ; Stores whether the current point lies inside given triangle or not ; If current point lies inside given triangle as well as inside any of the four obtained triangles ; Print the count of points ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int getArea ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) ; } static int isInside ( ArrayList < ArrayList < Integer > > triangle , ArrayList < Integer > point ) { ArrayList < Integer > A = triangle . get ( 0 ) ; ArrayList < Integer > B = triangle . get ( 1 ) ; ArrayList < Integer > C = triangle . get ( 2 ) ; int x = point . get ( 0 ) ; int y = point . get ( 1 ) ; int ABC = getArea ( A . get ( 0 ) , A . get ( 1 ) , B . get ( 0 ) , B . get ( 1 ) , C . get ( 0 ) , C . get ( 1 ) ) ; int BPC = getArea ( x , y , B . get ( 0 ) , B . get ( 1 ) , C . get ( 0 ) , C . get ( 1 ) ) ; int APC = getArea ( A . get ( 0 ) , A . get ( 1 ) , x , y , C . get ( 0 ) , C . get ( 1 ) ) ; int APB = getArea ( A . get ( 0 ) , A . get ( 1 ) , B . get ( 0 ) , B . get ( 1 ) , x , y ) ; return ABC == ( APC + APB + BPC ) ? 1 : 0 ; } static void countPoints ( ArrayList < ArrayList < Integer > > rectangle , ArrayList < ArrayList < Integer > > triangle , ArrayList < ArrayList < Integer > > points ) { int n = rectangle . size ( ) ; ArrayList < ArrayList < Integer > > triangle1 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 1 ; i < n ; i ++ ) triangle1 . add ( rectangle . get ( i ) ) ; ArrayList < ArrayList < Integer > > triangle2 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < 3 ; i ++ ) { triangle2 . add ( rectangle . get ( i ) ) ; } ArrayList < ArrayList < Integer > > triangle3 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < 2 ; i ++ ) { triangle3 . add ( rectangle . get ( i ) ) ; } triangle3 . add ( rectangle . get ( 3 ) ) ; ArrayList < ArrayList < Integer > > triangle4 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = n - 2 ; i < n ; i ++ ) { triangle4 . add ( rectangle . get ( i ) ) ; } triangle4 . add ( rectangle . get ( 0 ) ) ; int ans = 0 ; for ( ArrayList < Integer > point : points ) { int condOne =...


"Number of positions such that adding K to the element is greater than sum of all other elements | Java implementation of the approach ; Function that will find out the valid position ; find sum of all the elements ; adding K to the element and check whether it is greater than sum of all other elements ; Driver code"
How can the above be solved in Java?

class GFG { static int validPosition ( int arr [ ] , int N , int K ) { int count = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 6 , 7 } , K = 4 ; int N = arr . length ; System . out . println ( validPosition ( arr , N , K ) ) ; } }


"Number of positions where a letter can be inserted such that a string becomes palindrome | Java code to find the no . of positions where a letter can be inserted to make it a palindrome ; Function to check if the string is palindrome ; to know the length of string ; if the given string is a palindrome ( Case - I ) ; Sub - case - III ) ; if ( n % 2 == 0 ) if the length is even ; count = 2 * count + 1 ; sub - case - I ; count = 2 * count + 2 ; sub - case - II ; insertion point ; Case - I ; Case - II ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isPalindrome ( String s , int i , int j ) { int p = j ; for ( int k = i ; k <= p ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( p ) ) return false ; p -- ; } return true ; } static int countWays ( String s ) { int n = s . length ( ) ; int count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( int i = n / 2 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) count ++ ; else break ; } { count ++ ; } else } else { for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( n - 1 - i ) ) { int j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( s . charAt ( k ) != s . charAt ( j ) ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( int k = n - i ; k < n ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( i ) ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } public static void main ( String [ ] args ) { String s = " abca " ; System . out . println ( countWays ( s ) ) ; } }


"Number of possible permutations when absolute difference between number of elements to the right and left are given | Java implementation of the above approach ; Function to find the number of permutations possible of the original array to satisfy the given absolute differences ; To store the count of each a [ i ] in a map ; if n is odd ; check the count of each whether it satisfy the given criteria or not ; there is only 1 way for middle element . ; for others there are 2 ways . ; now find total ways ; When n is even . ; there will be no middle element so for each a [ i ] there will be 2 ways ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int totalways ( int [ ] arr , int n ) { HashMap < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( cnt . containsKey ( arr [ i ] ) ) { cnt . put ( arr [ i ] , cnt . get ( arr [ i ] ) + 1 ) ; } else { cnt . put ( arr [ i ] , 1 ) ; } } if ( n % 2 == 1 ) { int start = 0 , endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { if ( i == 0 ) { if ( cnt . get ( i ) != 1 ) { return 0 ; } } else { if ( cnt . get ( i ) != 2 ) { return 0 ; } } } int ways = 1 ; start = 2 ; endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } else if ( n % 2 == 0 ) { int start = 1 , endd = n - 1 ; for ( int i = 1 ; i <= endd ; i = i + 2 ) { if ( cnt . get ( i ) != 2 ) return 0 ; } int ways = 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 2 , 4 , 4 , 0 , 2 } ; System . out . println ( totalways ( arr , N ) ) ; } }


"Number of primes in a subarray ( with updates ) | Java program to find number of prime numbers in a subarray and performing updates ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; A utility function to get the middle index from corner indexes . ; A recursive function to get the number of primes in a given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree index -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ index ] qs & qe -- > Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the number of primes in the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; A recursive function to update the nodes which have the given index in their range . The following are parameters st , si , ss and se are same as getSumUtil ( ) i -- > index of the element to be updated . This index is in input array . diff -- > Value to be added to all nodes which have i in range ; Base Case : If the input index lies outside the range of this segment ; If the input index is in range of this node , then update the value of the node and its children ; The function to update a value in input array and segment tree . It uses updateValueUtil ( ) to update the value in segment tree ; Check for erroneous input index ; Update the value in array ; Case 1 : Old and new values both are primes ; Case 2 : Old and new values both non primes ; Case 3 : Old value was prime , new value is non prime ; Case 4 : Old value was non prime , new_val is prime ; Update the values of nodes in segment tree ; Return number of primes in range from index qs ( query start ) to qe ( query end ) . It mainly uses queryPrimesUtil ( ) ; A recursive function that constructs Segment Tree for array [ ss . . se ] ...."
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000 ; static void sieveOfEratosthenes ( boolean isPrime [ ] ) { isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) isPrime [ i ] = false ; } } } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryPrimesUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryPrimesUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryPrimesUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int arr [ ] , int [ ] st , int n , int i , int new_val , boolean isPrime [ ] ) { if ( i < 0 i > n - 1 ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } int diff = 0 ; int oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPrime [ oldValue ] && isPrime [ new_val ] ) return ; if ( ( ! isPrime [ oldValue ] ) && ( ! isPrime [ new_val ] ) ) return ; if ( isPrime [ oldValue ] && ! isPrime [ new_val ] ) { diff = - 1 ; } if ( ! isPrime [ oldValue ] && isPrime [ new_val ] ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void queryPrimes ( int [ ] st , int n , int qs , int qe ) { int primesInRange = queryPrimesUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; System . out . println ( " Number ▁ of ▁ Primes ▁ in ▁ subarray ▁ from ▁ " + qs + " ▁ to ▁ " + qe + " ▁ = ▁ " + primesInRange ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si , boolean isPrime [ ] ) { if ( ss == se ) { if (...


"Number of quadrilateral formed with N distinct points on circumference of Circle | Java implementation to find the number of quadrilaterals formed with N distinct points ; Function to find the number of combinations in the N ; Function to find the factorial of the given number N ; Loop to find the factorial of the given number ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static int nCr ( int n , int r ) { return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i < n + 1 ; i ++ ) res = res * i ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( nCr ( n , 4 ) ) ; } }


"Number of quadrilaterals possible from the given points | Java implementation of above approach ; static class Point points ; determines the orientation of points ; check whether the distinct line segments intersect ; check if points overlap ( similar ) ; it is same , we are returning false because quadrilateral is not possible in this case ; it is not same , So there is a possibility of a quadrilateral ; check for collinearity ; it is collinear , we are returning false because quadrilateral is not possible in this case ; it is not collinear , So there is a possibility of a quadrilateral ; check if any of the points are same ; similar points exist ; check for collinearity ; points are collinear ; Checking for cases where no of quadrilaterals = 1 or 3 ; Driver code ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 5 , 9 ) ; A = ( 0 , 9 ) , B = ( - 1 , 0 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 ) ; A = ( 0 , 9 ) , B = ( 0 , 10 ) , C = ( 0 , 11 ) , D = ( 0 , 12 ) ; A = ( 0 , 9 ) , B = ( 0 , 9 ) , C = ( 5 , - 1 ) , D = ( 0 , 3 )"
How can the above be solved in Java?

class GFG { { int x ; int y ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static boolean doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; return false ; } static boolean similar ( Point p1 , Point p2 ) { if ( p1 . x == p2 . x && p1 . y == p2 . y ) return false ; return true ; } static boolean collinear ( Point p1 , Point p2 , Point p3 ) { int x1 = p1 . x , y1 = p1 . y ; int x2 = p2 . x , y2 = p2 . y ; int x3 = p3 . x , y3 = p3 . y ; if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) return false ; else return true ; } static int no_of_quads ( Point p1 , Point p2 , Point p3 , Point p4 ) { boolean same = true ; same = same & similar ( p1 , p2 ) ; same = same & similar ( p1 , p3 ) ; same = same & similar ( p1 , p4 ) ; same = same & similar ( p2 , p3 ) ; same = same & similar ( p2 , p4 ) ; same = same & similar ( p3 , p4 ) ; if ( same == false ) return 0 ; boolean coll = true ; coll = coll & collinear ( p1 , p2 , p3 ) ; coll = coll & collinear ( p1 , p2 , p4 ) ; coll = coll & collinear ( p1 , p3 , p4 ) ; coll = coll & collinear ( p2 , p3 , p4 ) ; if ( coll == false ) return 0 ; int check = 0 ; if ( doIntersect ( p1 , p2 , p3 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p3 , p2 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p2 , p4 , p3 ) ) check = 1 ; if ( check == 0 ) return 3 ; return 1 ; } public static void main ( String args [ ] ) { Point p1 , p2 , p3 , p4 ; p1 = new Point ( ) ; p2 = new Point ( ) ; p3 = new Point ( ) ; p4 = new Point ( ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 5 ; p4 . y = 9 ; System . out . println ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; p1 . x = 0 ; p1 . y =...


"Number of rectangles in N * M grid | JAVA Code to count number of rectangles in N * M grid ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } public static void main ( String [ ] args ) { int n = 5 , m = 4 ; System . out . println ( rectCount ( n , m ) ) ; } }