"Array with GCD of any of its subset belongs to the given array | Java implementation to generate the required array ; Function to return gcd of a and b ; Function to find gcd of array of numbers ; Function to generate the array with required constraints . ; computing GCD of the given set ; Solution exists if GCD of array is equal to the minimum element of the array ; Printing the built array ; Driver Code ; Taking in the input and initializing the set STL set in cpp has a property that it maintains the elements in sorted order , thus we do not need to sort them externally ; Calling the computing function ."
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } public static int findGCD ( ArrayList < Integer > arr , int n ) { int result = arr . get ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) result = gcd ( arr . get ( i ) , result ) ; return result ; } public static void compute ( ArrayList < Integer > arr , int n ) { ArrayList < Integer > answer = new ArrayList < Integer > ( ) ; int GCD_of_array = findGCD ( arr , n ) ; if ( GCD_of_array == arr . get ( 0 ) ) { answer . add ( arr . get ( 0 ) ) ; for ( int i = 1 ; i < n ; i ++ ) { answer . add ( arr . get ( 0 ) ) ; answer . add ( arr . get ( i ) ) ; } for ( int i = 0 ; i < answer . size ( ) ; i ++ ) System . out . print ( answer . get ( i ) + " ▁ " ) ; } else System . out . print ( " No ▁ array ▁ " + " can ▁ be ▁ build " ) ; } public static void main ( String args [ ] ) { int n = 3 ; Integer input [ ] = { 2 , 5 , 6 , 7 , 11 } ; HashSet < Integer > GCD = new HashSet < Integer > ( Arrays . asList ( input ) ) ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; for ( int v : GCD ) arr . add ( v ) ; compute ( arr , n ) ; } }


"Aspiring Number | Java implementation to check whether a number is aspiring or not ; Function to calculate sum of all proper divisors ; Note that this loop runs till square root of n ; If divisors are equal , take only one of them ; else Otherwise take both ; calculate sum of all proper divisors only ; Function to get last number of Aliquot Sequence . ; Calculate next term from previous term ; Returns true if n is perfect ; To store sum of divisors ; Find all divisors and add them ; If sum of divisors is equal to n , then n is a perfect number ; Returns true if n is aspiring else returns false ; checking condition for aspiring ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static int getAliquot ( int n ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; s . add ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . contains ( n ) & n != s . last ( ) ) { return n ; } s . add ( n ) ; } return 0 ; } static boolean isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { sum = sum + i + n / i ; } } if ( sum == n && n != 1 ) { return true ; } return false ; } static boolean isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 25 ; if ( isAspiring ( n ) ) { System . out . println ( " Aspiring " ) ; } else { System . out . println ( " Not ▁ Aspiring " ) ; } } }


"Assembly Line Scheduling | DP | A java program to find minimum possible time by the car chassis to complete ; Utility function to find minimum of two numbers ; time taken to leave first station in line 1 ; time taken to leave first station in line 2 ; Fill tables T1 [ ] and T2 [ ] using the above given recursive relations ; Consider exit times and retutn minimum ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int NUM_LINE = 2 ; static int NUM_STATION = 4 ; static int min ( int a , int b ) { return a < b ? a : b ; } static int carAssembly ( int a [ ] [ ] , int t [ ] [ ] , int e [ ] , int x [ ] ) { int T1 [ ] = new int [ NUM_STATION ] ; int T2 [ ] = new int [ NUM_STATION ] ; int i ; T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ; T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ; for ( i = 1 ; i < NUM_STATION ; ++ i ) { T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i ] , T2 [ i - 1 ] + t [ 1 ] [ i ] + a [ 0 ] [ i ] ) ; T2 [ i ] = min ( T2 [ i - 1 ] + a [ 1 ] [ i ] , T1 [ i - 1 ] + t [ 0 ] [ i ] + a [ 1 ] [ i ] ) ; } return min ( T1 [ NUM_STATION - 1 ] + x [ 0 ] , T2 [ NUM_STATION - 1 ] + x [ 1 ] ) ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 5 , 3 , 2 } , { 2 , 10 , 1 , 4 } } ; int t [ ] [ ] = { { 0 , 7 , 4 , 5 } , { 0 , 9 , 2 , 8 } } ; int e [ ] = { 10 , 12 } , x [ ] = { 18 , 7 } ; System . out . println ( carAssembly ( a , t , e , x ) ) ; } }


"Assign other value to a variable from two possible values | Java program to change value of x according to its current value . ; Function to alternate the values ; Main function"
How can the above be solved in Java?

import java . util . * ; class solution { static void alternate ( int a , int b , int x ) { x = a + b - x ; System . out . println ( " After ▁ change " + " " + " x is : "+x); } public static void main ( String args [ ] ) { int a = - 10 ; int b = 15 ; int x = a ; System . out . println ( " x ▁ is ▁ : ▁ " + x ) ; alternate ( a , b , x ) ; } }


class GFG { static int alternate ( int a , int b , int x ) { return x = a ^ b ^ x ; } public static void main ( String [ ] args ) { int a = - 10 ; int b = 15 ; int x = a ; System . out . print ( " x ▁ is ▁ : ▁ " + x ) ; x = alternate ( a , b , x ) ; System . out . print ( " After exchange "); System . out . print ( " x is : " + x); } }


"Astonishing Numbers | Java implementation for the above approach ; Function to concatenate two integers into one ; Convert both the integers to String ; Concatenate both Strings ; Convert the concatenated String to integer ; return the formed integer ; Function to check if N is a Astonishing number ; Loop to find sum of all integers from i till the sum becomes >= n ; variable to store sum of all integers from i to j and check if sum and concatenation equals n or not ; finding concatenation of i and j ; condition for Astonishing number ; Driver Code ; Given Number ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int concat ( int a , int b ) { String s1 = Integer . toString ( a ) ; String s2 = Integer . toString ( b ) ; String s = s1 + s2 ; int c = Integer . parseInt ( s ) ; return c ; } static boolean isAstonishing ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += j ; if ( sum == n ) { int concatenation = concat ( i , j ) ; if ( concatenation == n ) { return true ; } } } } return false ; } public static void main ( String [ ] args ) { int n = 429 ; if ( isAstonishing ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Auxiliary Space with Recursive Functions |"
How can the above be solved in Java?

static int sum ( int sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }


"Average of ASCII values of characters of a given string | Java code to find average of ASCII characters ; Function to find average of ASCII value of chars ; loop to sum the ascii value of chars ; Returning average of chars ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int averageValue ( String s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum_char += ( int ) s . charAt ( i ) ; } return sum_char / s . length ( ) ; } public static void main ( String [ ] args ) { String s = " GeeksforGeeks " ; System . out . println ( averageValue ( s ) ) ; } }


"Average of Cubes of first N natural numbers | Java program for the above approach ; Function to find average of cubes ; Storing sum of cubes of numbers in sum ; Calculate sum of cubes ; Return average ; Driver Code ; Given Number ; Function Call"
How can the above be solved in Java?

import java . util . * ; import java . io . * ; class GFG { static double findAverageOfCube ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( findAverageOfCube ( n ) ) ; } }


"Average of Squares of Natural Numbers | Java program to calculate 1 ^ 2 + 2 ^ 2 + 3 ^ 2 + ... average of square number ; Function to calculate average of square number ; Driver code"
How can the above be solved in Java?

import java . io . * ; public class GFG { static float AvgofSquareN ( int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum / n ; } static public void main ( String [ ] args ) { int n = 2 ; System . out . println ( AvgofSquareN ( n ) ) ; } }


"Average of Squares of Natural Numbers | Java program to get the Average of square of first n natural numbers ; Function to get the average ; Driver Code"
How can the above be solved in Java?

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


"Average of first n even natural numbers | Java Program to find the average of sum of first n even numbers ; Return the average of sum of first n even numbers ; driver code"
How can the above be solved in Java?

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


"Average of first n even natural numbers | java implementation to find Average of sum of first n natural even number ; function to find average of sum of first n even numbers ; sum of first n even numbers ; calculating Average ; driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int avg_of_even_num ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 2 * i ; return ( sum / n ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . print ( avg_of_even_num ( n ) ) ; } }


"Average of remaining elements after removing K largest and K smallest elements from array | Java implementation of the above approach ; Function to find average ; base case if 2 * k >= n means all element get removed ; first sort all elements ; sum of req number ; find average ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static double average ( int arr [ ] , int n , int k ) { double total = 0 ; if ( 2 * k >= n ) return 0 ; Arrays . sort ( arr ) ; int start = k , end = n - k - 1 ; for ( int i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 4 , 5 , 6 } ; int n = arr . length ; int k = 2 ; System . out . println ( average ( arr , n , k ) ) ; } }


"Average value of set bit count in given Binary string after performing all possible choices of K operations | Java program for the above approach ; Function to calculate the average number of Set bits after after given operations ; Stores the average number of set bits after current operation ; Stores the average number of set bits after current operation ; Iterate through the array arr [ ] and update the values of p and q ; Store the value of p and q of the previous state before their updation ; Update average number of set bits after performing the ith operation ; Update average number of off bits after performing the ith operation ; Return Answer ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static double averageSetBits ( int N , int K , int arr [ ] ) { double p = N ; double q = 0 ; for ( int i = 0 ; i < K ; i ++ ) { double _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , 2 , 3 } ; int K = arr . length ; System . out . println ( String . format ( " % .10f " , averageSetBits ( N , K , arr ) ) ) ; } }


"Bakhshali Approximation for computing square roots | Java program gives result approximated to 5 decimal places . ; This will be the nearest perfect square to s ; This is the sqrt of pSq ; Find the nearest perfect square to s ; calculate d ; calculate P ; calculate A ; calculate sqrt ( S ) . ; Driver program"
How can the above be solved in Java?

class GFG { static float sqroot ( float s ) { int pSq = 0 ; int N = 0 ; for ( int i = ( int ) ( s ) ; i > 0 ; i -- ) { for ( int j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } float d = s - pSq ; float P = d / ( 2.0f * N ) ; float A = N + P ; float sqrt_of_s = A - ( ( P * P ) / ( 2.0f * A ) ) ; return sqrt_of_s ; } public static void main ( String [ ] args ) { float num = 9.2345f ; float sqroot_of_num = sqroot ( num ) ; System . out . print ( " Square ▁ root ▁ of ▁ " + num + " ▁ = ▁ " + Math . round ( sqroot_of_num * 100000.0 ) / 100000.0 ) ; } }


"Balance a string after removing extra brackets | Java implementation of above approach ; Print balanced and remove extra brackets from string ; Maintain a count for opening brackets Traversing string ; check if opening bracket ; print str . charAt ( i ) and increment count by 1 ; check if closing bracket and count != 0 ; decrement count by 1 ; if str . charAt ( i ) not a closing brackets print it ; balanced brackets if opening brackets are more then closing brackets ; print remaining closing brackets ; Driver Method"
How can the above be solved in Java?

class GFG { public static void balancedString ( String str ) { int count = 0 , i ; int n = str . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { System . out . print ( str . charAt ( i ) ) ; count ++ ; } else if ( str . charAt ( i ) == ' ) ' && count != 0 ) { System . out . print ( str . charAt ( i ) ) ; count -- ; } else if ( str . charAt ( i ) != ' ) ' ) System . out . print ( str . charAt ( i ) ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String args [ ] ) { String str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; } }


"Balance pans using given weights that are powers of a number | Java code to check whether scale can be balanced or not ; method returns true if balancing of scale is possible ; baseForm vector will store T 's  representation on base a  in reverse order ; convert T to representation on base a ; make first digit of representation as 0 ; loop over base representation of T ; if any digit is not 0 , 1 , ( a - 1 ) or a then balancing is not possible ; if digit is a or ( a - 1 ) then increase left index ' s ▁ count / ▁ ( case , ▁ when ▁ this ▁ weight ▁ is ▁ transferred ▁ to ▁ T ' s side ) ; if representation is processed then balancing is possible ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isBalancePossible ( int T , int a ) { Vector < Integer > baseForm = new Vector < > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . add ( T % a ) ; T /= a ; s ++ ; } baseForm . add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm . get ( i ) != 0 && baseForm . get ( i ) != 1 && baseForm . get ( i ) != ( a - 1 ) && baseForm . get ( i ) != a ) { return false ; } if ( baseForm . get ( i ) == a || baseForm . get ( i ) == ( a - 1 ) ) { baseForm . add ( i + 1 , baseForm . get ( i + 1 ) + 1 ) ; } } return true ; } public static void main ( String [ ] args ) { int T = 11 ; int a = 4 ; boolean balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { System . out . println ( " Balance ▁ is ▁ possible " ) ; } else { System . out . println ( " Balance ▁ is ▁ not ▁ possible " ) ; } } }


"Balanced Prime | Java Program to find Nth Balanced Prime ; Return the Nth balanced prime . ; Sieve of Eratosthenes ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; storing all primes ; Finding the Nth balanced Prime ; Driven Program"
How can the above be solved in Java?

import java . util . * ; public class GFG { static int MAX = 501 ; public static int balancedprime ( int n ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int k = 0 ; k < MAX + 1 ; k ++ ) prime [ k ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } Vector < Integer > v = new Vector < Integer > ( ) ; for ( int p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . add ( p ) ; int count = 0 ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( ( int ) v . get ( i ) == ( ( int ) v . get ( i + 1 ) + ( int ) v . get ( i - 1 ) ) / 2 ) count ++ ; if ( count == n ) return ( int ) v . get ( i ) ; } return 1 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( balancedprime ( n ) ) ; } }


"Balanced Ternary Number System | Java program to convert positive decimals into balanced ternary system ; Driver code"
How can the above be solved in Java?

class GFG { static String balancedTernary ( int n ) { String output = " " ; while ( n > 0 ) { int rem = n % 3 ; n = n / 3 ; if ( rem == 2 ) { rem = - 1 ; n ++ ; } output = ( rem == 0 ? '0' : ( rem == 1 ) ? '1' : ' Z ' ) + output ; } return output ; } public static void main ( String [ ] args ) { int n = 238 ; System . out . print ( " Equivalent ▁ Balanced ▁ Ternary ▁ of ▁ " + n + " ▁ is : ▁ " + balancedTernary ( n ) ) ; } }


"Balanced expressions such that given positions have opening brackets | Java code to find number of ways of arranging bracket with proper expressions ; function to calculate the number of proper bracket sequence ; hash array to mark the positions of opening brackets ; dp 2d array ; mark positions in hash array ; first position marked as 1 ; iterate and formulate the recurrences ; if position has a opening bracket ; return answer ; Driver code ; positions where opening braces will be placed"
How can the above be solved in Java?

public class GFG { static final int N = 1000 ; static long arrangeBraces ( int n , int pos [ ] , int k ) { boolean h [ ] = new boolean [ N ] ; int dp [ ] [ ] = new int [ N ] [ N ] ; for ( int i = 0 ; i < k ; i ++ ) { h [ pos [ i ] ] = true ; } dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= 2 * n ; j ++ ) { if ( h [ i ] ) { if ( j != 0 ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = 0 ; } } else if ( j != 0 ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; } } } return dp [ 2 * n ] [ 0 ] ; } public static void main ( String [ ] args ) { int n = 3 ; int pos [ ] = { 2 } ; int k = pos . length ; System . out . print ( arrangeBraces ( n , pos , k ) ) ; } }


"Balanced expressions such that given positions have opening brackets | Set 2 | Java implementation of above approach using memoization ; Function to find Number of proper bracket expressions ; If open - closed brackets < 0 ; If index reaches the end of expression ; If brackets are balanced ; If already stored in dp ; If the current index has assigned open bracket ; Move forward increasing the length of open brackets ; Move forward by inserting open as well as closed brackets on that index ; return the answer ; Driver code ; DP array to precompute the answer ; Open brackets at position 1 ; Calling the find function to calculate the answer"
How can the above be solved in Java?

public class GFG { static final int N = 1000 ; static int find ( int index , int openbrk , int n , int dp [ ] [ ] , int adj [ ] ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ openbrk ] != - 1 ) { return dp [ index ] [ openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index ] [ openbrk ] ; } public static void main ( String [ ] args ) { int dp [ ] [ ] = new int [ N ] [ N ] ; int n = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int adj [ ] = { 1 , 0 , 0 , 0 } ; System . out . print ( find ( 0 , 0 , 2 * n , dp , adj ) ) ; } }


"Beatty sequence | Java implementation of the above approach ; Function to print the first N terms of the Beatty sequence ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void BeattySequence ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int ans = ( int ) Math . floor ( i * Math . sqrt ( 2 ) ) ; System . out . print ( ans + " , ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; BeattySequence ( n ) ; } }


"Bell Numbers ( Number of ways to Partition a Set ) | Java program to find n 'th Bell number ; Function to find n 'th Bell Number ; Explicitly fill for j = 0 ; Fill for remaining values of j ; Driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int bellNumber ( int n ) { int [ ] [ ] bell = new int [ n + 1 ] [ n + 1 ] ; bell [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { bell [ i ] [ 0 ] = bell [ i - 1 ] [ i - 1 ] ; for ( int j = 1 ; j <= i ; j ++ ) bell [ i ] [ j ] = bell [ i - 1 ] [ j - 1 ] + bell [ i ] [ j - 1 ] ; } return bell [ n ] [ 0 ] ; } public static void main ( String [ ] args ) { for ( int n = 0 ; n <= 5 ; n ++ ) System . out . println ( " Bell ▁ Number ▁ " + n + " ▁ is ▁ " + bellNumber ( n ) ) ; } }


"Bellman Ford Algorithm ( Simple Implementation ) | A Java program for Bellman - Ford 's single source shortest path algorithm. ; The main function that finds shortest distances from src to all other vertices using Bellman - Ford algorithm . The function also detects negative weight cycle The row graph [ i ] represents i - th edge with three values u , v and w . ; Initialize distance of all vertices as infinite . ; initialize distance of source as 0 ; Relax all edges | V | - 1 times . A simple shortest path from src to any other vertex can have at - most | V | - 1 edges ; check for negative - weight cycles . The above step guarantees shortest distances if graph doesn 't contain  negative weight cycle. If we get a  shorter path, then there is a cycle. ; Driver code ; Every edge has three values ( u , v , w ) where the edge is from vertex u to v . And weight of the edge is w ."
How can the above be solved in Java?

class GFG { static void BellmanFord ( int graph [ ] [ ] , int V , int E , int src ) { int [ ] dis = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) dis [ i ] = Integer . MAX_VALUE ; dis [ src ] = 0 ; for ( int i = 0 ; i < V - 1 ; i ++ ) { for ( int j = 0 ; j < E ; j ++ ) { if ( dis [ graph [ j ] [ 0 ] ] != Integer . MAX_VALUE && dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] < dis [ graph [ j ] [ 1 ] ] ) dis [ graph [ j ] [ 1 ] ] = dis [ graph [ j ] [ 0 ] ] + graph [ j ] [ 2 ] ; } } for ( int i = 0 ; i < E ; i ++ ) { int x = graph [ i ] [ 0 ] ; int y = graph [ i ] [ 1 ] ; int weight = graph [ i ] [ 2 ] ; if ( dis [ x ] != Integer . MAX_VALUE && dis [ x ] + weight < dis [ y ] ) System . out . println ( " Graph ▁ contains ▁ negative " + " ▁ weight ▁ cycle " ) ; } System . out . println ( " Vertex ▁ Distance ▁ from ▁ Source " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . println ( i + " 	 	 " + dis [ i ] ) ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , - 1 } , { 0 , 2 , 4 } , { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 4 , 2 } , { 3 , 2 , 5 } , { 3 , 1 , 1 } , { 4 , 3 , - 3 } } ; BellmanFord ( graph , V , E , 0 ) ; } }


"Best meeting point in 2D binary array | Java program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int grid [ ] [ ] ) { if ( ROW == 0 COL == 0 ) return 0 ; Vector < Integer > vertical = new Vector < Integer > ( ) ; Vector < Integer > horizontal = new Vector < Integer > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . add ( i ) ; horizontal . add ( j ) ; } } } Collections . sort ( vertical ) ; Collections . sort ( horizontal ) ; int size = vertical . size ( ) / 2 ; int x = vertical . get ( size ) ; int y = horizontal . get ( size ) ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += Math . abs ( x - i ) + Math . abs ( y - j ) ; return distance ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; System . out . println ( minTotalDistance ( grid ) ) ; } }


"Biggest Reuleaux Triangle inscirbed within a square inscribed in a semicircle | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a semicircle ; Function to find the biggest reuleaux triangle ; radius cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . lang . Math ; class GFG { static float Area ( float r ) { if ( r < 0 ) return - 1 ; float x = ( 2 * r ) / ( float ) ( Math . sqrt ( 5 ) ) ; float A = 0.70477f * ( float ) ( Math . pow ( x , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( Area ( r ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a Square inscribed in an equilateral triangle | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an equilateral triangle ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
How can the above be solved in Java?

class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float x = 0.464f * a ; float A = 0.70477f * ( float ) Math . pow ( x , 2 ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( String . format ( " % .5f " , Area ( a ) ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a hexagon ; Function to find the biggest reuleaux triangle ; side cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float h = ( float ) 1.268 * a ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( Area ( a ) ) ; } }


"Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within an ellipse ; Function to find the biggest reuleaux triangle ; length of the axes cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float Area ( float a , float b ) { if ( a < 0 && b < 0 ) return - 1 ; float h = ( float ) Math . sqrt ( ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) / ( Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) ) ) ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 4 ; System . out . println ( Area ( a , b ) ) ; } }


"Biggest Reuleaux Triangle within A Square | Java Program to find the area of the biggest Reuleaux triangle that can be inscribed within a square ; Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . lang . Math ; class cfg { static double ReuleauxArea ( double a ) { if ( a < 0 ) return - 1 ; double A = 0.70477 * Math . pow ( a , 2 ) ; return A ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( ReuleauxArea ( a ) ) ; }


"Biggest Reuleaux Triangle within a Square which is inscribed within a Circle | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the Area of the Reuleaux triangle ; radius cannot be negative ; Area of the Reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double ReuleauxArea ( double r ) { if ( r < 0 ) return - 1 ; double A = 0.70477 * 2 * Math . pow ( r , 2 ) ; return A ; } public static void main ( String args [ ] ) { double r = 6 ; System . out . println ( ReuleauxArea ( r ) ) ; } }


"Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle | Java Program to find the biggest Reuleaux triangle inscribed within in a square which in turn is inscribed within a circle ; Function to find the biggest reuleaux triangle ; the height or base or hypotenuse cannot be negative ; height of the reuleaux triangle ; area of the reuleaux triangle ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . text . DecimalFormat ; class GFG { static double Area ( double l , double b , double h ) { if ( l < 0 b < 0 h < 0 ) return - 1 ; double x = ( l * b ) / ( l + b ) ; double A = 0.70477 * Math . pow ( x , 2 ) ; return A ; } public static void main ( String args [ ] ) { double l = 5 , b = 12 , h = 13 ; DecimalFormat df = new DecimalFormat ( " # , # # # , # #0.00000" ) ; System . out . println ( df . format ( Area ( l , b , h ) ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | Java program to find number of bins required using Best fit algorithm . ; Returns number of bins required using best fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that can accommodate weight [ i ] ; Initialize minimum space left and index of best bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver code"
How can the above be solved in Java?

class GFG { static int bestFit ( int weight [ ] , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ bi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Best ▁ Fit ▁ : ▁ " + bestFit ( weight , n , c ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | Java program to find number of bins required using First Fit algorithm . ; Returns number of bins required using first fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the first bin that can accommodate weight [ i ] ; If no bin could accommodate weight [ i ] ; Driver program"
How can the above be solved in Java?

class GFG { static int firstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } public static void main ( String [ ] args ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " + firstFit ( weight , n , c ) ) ; } }


"Bin Packing Problem ( Minimize number of used Bins ) | Java program to find number of bins required using Worst fit algorithm . ; Returns number of bins required using worst fit online algorithm ; Initialize result ( Count of bins ) ; Create an array to store remaining space in bins there can be at most n bins ; Place items one by one ; Find the best bin that ca \ n accommodate weight [ i ] ; Initialize maximum space left and index of worst bin ; If no bin could accommodate weight [ i ] , create a new bin ; else Assign the item to best bin ; Driver program"
How can the above be solved in Java?

class GFG { static int worstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int bin_rem [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Worst ▁ Fit ▁ : ▁ " + worstFit ( weight , n , c ) ) ; } }


"Binary Indexed Tree : Range Updates and Point Queries | Java code to demonstrate Range Update and * Point Queries on a Binary Index Tree . * This method only works when all array * values are initially 0. ; Max tree size ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BITree ; Update index to that of parent in update View ; Constructs Binary Indexed Tree for given array of size n . ; Initialize BITree [ ] as 0 ; Store the actual values in BITree [ ] using update ( ) ; SERVES THE PURPOSE OF getElement ( ) Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; Initialize result ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add current element of BITree to sum ; Move index to parent node in getSum View ; Return the sum ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Increase value at ' l ' by ' val ' ; Decrease value at ' r + 1' by ' val ' ; Driver Code ; Add 2 to all the element from [ 2 , 4 ] ; Find the element at Index 4 ; Add 2 to all the element from [ 0 , 3 ] ; Find the element at Index 3"
How can the above be solved in Java?

class GFG { final static int MAX = 1000 ; static int BITree [ ] = new int [ MAX ] ; public static void updateBIT ( int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void constructBITree ( int arr [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( n , i , arr [ i ] ) ; } public static int getSum ( int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void update ( int l , int r , int n , int val ) { updateBIT ( n , l , val ) ; updateBIT ( n , r + 1 , - val ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . length ; constructBITree ( arr , n ) ; int l = 2 , r = 4 , val = 2 ; update ( l , r , n , val ) ; int index = 4 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( l , r , n , val ) ; index = 3 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; } }


"Binary Indexed Tree : Range Updates and Point Queries | Java program to demonstrate Range Update and Point Queries Without using BIT ; Updates such that getElement ( ) gets an increased value when queried from l to r . ; Get the element indexed at i ; To get ith element sum of all the elements from 0 to i need to be computed ; Driver program to test above function ; Find the element at Index 4 ; Find the element at Index 3"
How can the above be solved in Java?

class GfG { static void update ( int arr [ ] , int l , int r , int val ) { arr [ l ] += val ; if ( r + 1 < arr . length ) arr [ r + 1 ] -= val ; } static int getElement ( int arr [ ] , int i ) { int res = 0 ; for ( int j = 0 ; j <= i ; j ++ ) res += arr [ j ] ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . length ; int l = 2 , r = 4 , val = 2 ; update ( arr , l , r , val ) ; int index = 4 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( arr , l , r , val ) ; index = 3 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; } }


"Binary Matrix after flipping submatrices in given range for Q queries | Java program to implement the above approach ; Function to flip a submatrices ; Boundaries of the submatrix ; Iterate over the submatrix ; Check for 1 or 0 and flip accordingly ; Function to perform the queries ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static void manipulation ( int [ ] [ ] matrix , int [ ] q ) { int x1 = q [ 0 ] , y1 = q [ 1 ] , x2 = q [ 2 ] , y2 = q [ 3 ] ; for ( int i = x1 - 1 ; i < x2 ; i ++ ) { for ( int j = y1 - 1 ; j < y2 ; j ++ ) { if ( matrix [ i ] [ j ] == 1 ) matrix [ i ] [ j ] = 0 ; else matrix [ i ] [ j ] = 1 ; } } } static void queries_fxn ( int [ ] [ ] matrix , int [ ] [ ] queries ) { for ( int [ ] q : queries ) manipulation ( matrix , q ) ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int [ ] [ ] queries = { { 1 , 1 , 2 , 3 } , { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 3 } } ; queries_fxn ( matrix , queries ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < matrix . length ; i ++ ) { System . out . print ( " [ " ) ; for ( int j = 0 ; j < matrix [ i ] . length ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; if ( i == matrix . length - 1 ) System . out . print ( " ] " ) ; else System . out . print ( " ] , ▁ " ) ; } System . out . print ( " ] " ) ; } }


"Binary Search Tree insert with Parent Pointer | Java program to demonstrate insert operation in binary search tree with parent pointer ; Node structure ; A utility function to create a new BST Node ; A utility function to do inorder traversal of BST ; A utility function to insert a new Node with given key in BST ; If the tree is empty , return a new Node ; Otherwise , recur down the tree ; Set parent of root of left subtree ; Set parent of root of right subtree ; return the ( unchanged ) Node pointer ; Driver Program to test above functions ; Let us create following BST 50 / \ 30 70 / \ / \ 20 40 60 80 ; print iNoder traversal of the BST"
How can the above be solved in Java?

class GfG { static class Node { int key ; Node left , right , parent ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; temp . parent = null ; return temp ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( " Node ▁ : ▁ " + root . key + " ▁ , ▁ " ) ; if ( root . parent == null ) System . out . println ( " Parent ▁ : ▁ NULL " ) ; else System . out . println ( " Parent ▁ : ▁ " + root . parent . key ) ; inorder ( root . right ) ; } } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) { Node lchild = insert ( node . left , key ) ; node . left = lchild ; lchild . parent = node ; } else if ( key > node . key ) { Node rchild = insert ( node . right , key ) ; node . right = rchild ; rchild . parent = node ; } return node ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; inorder ( root ) ; } }


"Binary Search Tree | Set 1 ( Search and Insertion ) | A utility function to search a given key in BST ; Base Cases : root is null or key is present at root ; Key is greater than root 's key ; Key is smaller than root 's key"
How can the above be solved in Java?

public Node search ( Node root , int key ) { if ( root == null root . key == key ) return root ; if ( root . key < key ) return search ( root . right , key ) ; return search ( root . left , key ) ; }


"Binary Search a String | Java program to implement Binary Search for strings ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; Driver method to test above"
How can the above be solved in Java?

class GFG { static int binarySearch ( String [ ] arr , String x ) { int l = 0 , r = arr . length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; int res = x . compareTo ( arr [ m ] ) ; if ( res == 0 ) return m ; if ( res > 0 ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { String [ ] arr = { " contribute " , " geeks " , " ide " , " practice " } ; String x = " ide " ; int result = binarySearch ( arr , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }


"Binary Search | Java implementation of iterative Binary Search ; Returns index of x if it is present in arr [ ] , else return - 1 ; Check if x is present at mid ; If x greater , ignore left half ; If x is smaller , ignore right half ; if we reach here , then element was not present ; Driver method to test above"
How can the above be solved in Java?

class BinarySearch { int binarySearch ( int arr [ ] , int x ) { int l = 0 , r = arr . length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }


"Binary Search | Java implementation of recursive Binary Search ; Returns index of x if it is present in arr [ l . . r ] , else return - 1 ; If the element is present at the middle itself ; If element is smaller than mid , then it can only be present in left subarray ; Else the element can only be present in right subarray ; We reach here when element is not present in array ; Driver method to test above"
How can the above be solved in Java?

class BinarySearch { int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + result ) ; } }


"Binary String of given length that without a palindrome of size 3 | JAVA program find a binary String of given length that doesn 't contain a palindrome of size 3. ; Printing the character according to i ; Driver code"
How can the above be solved in Java?

class GFG { static void generateString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( ( i & 2 ) > 1 ? ' b ' : ' a ' ) ; System . out . println ( s ) ; } public static void main ( String [ ] args ) { int n = 5 ; generateString ( n ) ; n = 8 ; generateString ( n ) ; n = 10 ; generateString ( n ) ; } }


"Binary Tree ( Array implementation ) | JAVA implementation of tree using array numbering starting from 0 to n - 1. ; create root ; create left son of root ; create right son of root ; Print tree ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; import java . lang . * ; import java . io . * ; class Array_imp { static int root = 0 ; static String [ ] str = new String [ 10 ] ; public void Root ( String key ) { str [ 0 ] = key ; } public void set_Left ( String key , int root ) { int t = ( root * 2 ) + 1 ; if ( str [ root ] == null ) { System . out . printf ( "Can't set child at %d, no parent found
", t); } else { str [ t ] = key ; } } public void set_Right ( String key , int root ) { int t = ( root * 2 ) + 2 ; if ( str [ root ] == null ) { System . out . printf ( "Can't set child at %d, no parent found
", t); } else { str [ t ] = key ; } } public void print_Tree ( ) { for ( int i = 0 ; i < 10 ; i ++ ) { if ( str [ i ] != null ) System . out . print ( str [ i ] ) ; else System . out . print ( " - " ) ; } } } class Tree { public static void main ( String [ ] args ) { Array_imp obj = new Array_imp ( ) ; obj . Root ( " A " ) ; obj . set_Right ( " C " , 0 ) ; obj . set_Left ( " D " , 1 ) ; obj . set_Right ( " E " , 1 ) ; obj . set_Left ( " F " , 2 ) ; obj . print_Tree ( ) ; } }


"Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value ; Binary Tree ; Driver code"
How can the above be solved in Java?

class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( int key ) { root = new Node ( key ) ; } BinaryTree ( ) { root = null ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; } }


"Binary Tree | Set 1 ( Introduction ) | Class containing left and right child of current node and key value"
How can the above be solved in Java?

class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } }


"Binary array after M range toggle operations | Java program to find modified array after m range toggle operations . ; function for toggle ; function for final processing of array ; function for printing result ; Driver Code ; function call for toggle ; process array ; print result"
How can the above be solved in Java?

class GFG { static void command ( boolean arr [ ] , int a , int b ) { arr [ a ] ^= true ; arr [ b + 1 ] ^= true ; } static void process ( boolean arr [ ] , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { arr [ k ] ^= arr [ k - 1 ] ; } } static void result ( boolean arr [ ] , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { if ( arr [ k ] == true ) System . out . print ( "1" + " ▁ " ) ; else System . out . print ( "0" + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 , m = 3 ; boolean arr [ ] = new boolean [ n + 2 ] ; command ( arr , 1 , 5 ) ; command ( arr , 2 , 5 ) ; command ( arr , 3 , 5 ) ; process ( arr , n ) ; result ( arr , n ) ; } }


"Binary representation of a given number |  ; bin function ; Driver code"
How can the above be solved in Java?

public class GFG { static void bin ( long n ) { long i ; System . out . print ( "0" ) ; for ( i = 1 << 30 ; i > 0 ; i = i / 2 ) { if ( ( n & i ) != 0 ) { System . out . print ( "1" ) ; } else { System . out . print ( "0" ) ; } } } public static void main ( String [ ] args ) { bin ( 7 ) ; System . out . println ( ) ; bin ( 4 ) ; } }


"Binary representation of a given number | Java implementation of the approach ; Function to convert decimal to binary number ; Driver code"
How can the above be solved in Java?

 class GFG { static void bin ( Integer n ) { if ( n > 1 ) bin ( n >> 1 ) ; System . out . printf ( " % d " , n & 1 ) ; } public static void main ( String [ ] args ) { bin ( 131 ) ; System . out . printf ( "
"); bin ( 3 ) ; } }


"Binary representation of next greater number with same number of 1 ' s ▁ and ▁ 0' s | Java program to find next permutation in a binary string . ; Function to find the next greater number with same number of 1 ' s ▁ and ▁ 0' s ; locate first ' i ' from end such that bnum [ i ] == '0' and bnum [ i + 1 ] == '1' swap these value and break ; ; if no swapping performed ; Since we want the smallest next value , shift all 1 ' s ▁ at ▁ the ▁ end ▁ in ▁ the ▁ binary ▁ ▁ substring ▁ starting ▁ from ▁ index ▁ ' i + 2 ' ; special case while swapping if '0' occurs then break ; required next greater number ; Driver program to test above"
How can the above be solved in Java?

class GFG { static String nextGreaterWithSameDigits ( char [ ] bnum ) { int l = bnum . length ; int i ; for ( i = l - 2 ; i >= 1 ; i -- ) { if ( bnum [ i ] == '0' && bnum [ i + 1 ] == '1' ) { char ch = bnum [ i ] ; bnum [ i ] = bnum [ i + 1 ] ; bnum [ i + 1 ] = ch ; break ; } } if ( i == 0 ) System . out . println ( " no ▁ greater ▁ number " ) ; int j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum [ j ] == '1' && bnum [ k ] == '0' ) { char ch = bnum [ j ] ; bnum [ j ] = bnum [ k ] ; bnum [ k ] = ch ; j ++ ; k -- ; } else if ( bnum [ i ] == '0' ) break ; else j ++ ; } return String . valueOf ( bnum ) ; } public static void main ( String [ ] args ) { char [ ] bnum = "10010" . toCharArray ( ) ; System . out . println ( " Binary ▁ representation ▁ of ▁ next ▁ greater ▁ number ▁ = ▁ " + nextGreaterWithSameDigits ( bnum ) ) ; } }


"Binary string with given frequencies of sums of consecutive pairs of characters | Java program to generate a binary String with given frequencies of sums of consecutive pair of characters ; A Function that generates and returns the binary String ; P : Frequency of consecutive characters with sum 0 Q : Frequency of consecutive characters with sum 1 R : Frequency of consecutive characters with sum 2 ; If no consecutive character adds up to 1 ; Not possible if both P and Q are non - zero ; If P is not equal to 0 ; Append 0 P + 1 times ; Append 1 R + 1 times ; Append "01" to satisfy Q ; Append "0" P times ; Append "1" R times ; Driver Code"
How can the above be solved in Java?

class GFG { static String build_binary_str ( int p , int q , int r ) { String ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " - 1" ; } else { if ( p > 0 ) { ans = Strings ( p + 1 , '0' ) ; } else { ans = Strings ( r + 1 , '1' ) ; } } } else { for ( int i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += '0' ; } else { ans += '1' ; } } ans = ans . substring ( 0 , 1 ) + Strings ( p , '0' ) + ans . substring ( 1 ) ; ans = Strings ( r , '1' ) + ans ; } return ans ; } static String Strings ( int p , char c ) { String ans = " " ; for ( int i = 0 ; i < p ; i ++ ) ans += c ; return ans ; } public static void main ( String [ ] args ) { int p = 1 , q = 2 , r = 2 ; System . out . print ( build_binary_str ( p , q , r ) ) ; } }


"Binomial Random Variables | Java program to compute Binomial Probability ; function to calculate nCr i . e . , number of ways to choose r out of n objects ; Since nCr is same as nC ( n - r ) To decrease number of iterations ; function to calculate binomial r . v . probability ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . pow ( p , k ) * ( float ) Math . pow ( 1 - p , n - k ) ; } public static void main ( String [ ] args ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; System . out . print ( " Probability ▁ of ▁ " + k ) ; System . out . print ( " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " + n ) ; System . out . println ( " ▁ times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is ▁ " + p ) ; System . out . println ( " ▁ is ▁ = ▁ " + probability ) ; } }


"Birthday Paradox | Java program to approximate number of people in Birthday Paradox problem ; Returns approximate number of people for a given probability ; Driver code"
How can the above be solved in Java?

class GFG { static double find ( double p ) { return Math . ceil ( Math . sqrt ( 2 * 365 * Math . log ( 1 / ( 1 - p ) ) ) ) ; } public static void main ( String [ ] args ) { System . out . println ( find ( 0.70 ) ) ; } }


"Bitonic point in the given linked list | Java implementation of the approach ; Node for linked list ; Function to insert a node at the head of the linked list ; Function to return the bitonic of the given linked list ; If list is empty ; If list contains only a single node ; Invalid bitonic sequence ; If current node is the bitonic point ; Get to the next node in the list ; Nodes must be in descending starting from here ; Out of order node ; Get to the next node in the list ; Driver code"
How can the above be solved in Java?

class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int bitonic_point ( Node node ) { if ( node == null ) return - 1 ; if ( node . next == null ) return - 1 ; if ( node . data > node . next . data ) return - 1 ; while ( node . next != null ) { if ( node . data > node . next . data ) break ; node = node . next ; } int bitonicPoint = node . data ; while ( node . next != null ) { if ( node . data < node . next . data ) return - 1 ; node = node . next ; } return bitonicPoint ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 100 ) ; head = push ( head , 201 ) ; head = push ( head , 399 ) ; head = push ( head , 490 ) ; head = push ( head , 377 ) ; head = push ( head , 291 ) ; head = push ( head , 100 ) ; System . out . println ( bitonic_point ( head ) ) ; } }


"Bitonic string | Java program for the above approach ; Function to check if the given String is bitonic ; Check for increasing sequence ; If end of String has been reached ; Check for decreasing sequence ; If the end of String hasn 't  been reached ; Return true if bitonic ; Driver Code ; Given String ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int checkBitonic ( char [ ] s ) { int i , j ; for ( i = 1 ; i < s . length ; i ++ ) { if ( s [ i ] > s [ i - 1 ] ) continue ; if ( s [ i ] <= s [ i - 1 ] ) break ; } if ( i == s . length - 1 ) return 1 ; for ( j = i + 1 ; j < s . length ; j ++ ) { if ( s [ j ] < s [ j - 1 ] ) continue ; if ( s [ j ] >= s [ j - 1 ] ) break ; } i = j ; if ( i != s . length ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { String s = " abcdfgcba " ; System . out . print ( ( checkBitonic ( s . toCharArray ( ) ) == 1 ) ? " YES " : " NO " ) ; } }


"Bitwise AND of N binary strings | Java implementation of the above approach ; Function to find the bitwise AND of all the binary strings ; To store the largest and the smallest string ' s ▁ size , ▁ We ▁ need ▁ this ▁ to ▁ add ▁ ▁ ' 0 's in the resultant string ; Reverse each string Since we need to perform AND operation on bits from Right to Left ; Update the respective length values ; Traverse bits from 0 to smallest string 's size ; If at this bit position , there is a 0 in any of the given strings then AND operation on current bit position will be 0 ; Add resultant bit to result ; Add 0 's to the string. ; Reverse the string Since we started from LEFT to RIGHT ; Return the resultant string ; Driver code"
How can the above be solved in Java?

class GfG { static String strBitwiseAND ( String [ ] arr , int n ) { String res = " " ; int smallest_size = Integer . MAX_VALUE ; int largest_size = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { StringBuilder temp = new StringBuilder ( ) ; temp . append ( arr [ i ] ) ; arr [ i ] = temp . reverse ( ) . toString ( ) ; smallest_size = Math . min ( smallest_size , arr [ i ] . length ( ) ) ; largest_size = Math . max ( largest_size , arr [ i ] . length ( ) ) ; } for ( int i = 0 ; i < smallest_size ; i ++ ) { boolean all_ones = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] . charAt ( i ) == '0' ) { all_ones = false ; break ; } } res += ( all_ones ? '1' : '0' ) ; } for ( int i = 0 ; i < largest_size - smallest_size ; i ++ ) res += '0' ; StringBuilder temp = new StringBuilder ( ) ; temp . append ( res ) ; res = temp . reverse ( ) . toString ( ) ; return res ; } public static void main ( String [ ] args ) { String arr [ ] = { "101" , "110110" , "111" } ; int n = arr . length ; System . out . println ( strBitwiseAND ( arr , n ) ) ; } }


"Bitwise AND of all the elements of array | Java program to find bitwise AND of all the elements in the array ; Function to calculate bitwise AND ; Initialise ans variable is arr [ 0 ] ; Traverse the array compute AND ; Return ans ; Driver Code ; Function Call to find AND"
How can the above be solved in Java?

import java . util . * ; class GFG { static int find_and ( int arr [ ] ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { ans = ( ans & arr [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 5 , 9 , 11 } ; System . out . println ( find_and ( arr ) ) ; } }


"Bitwise AND of all unordered pairs from a given array | Java program to implement the above approach ; Function to calculate bitwise AND of all pairs from the given array ; Stores bitwise AND of all possible pairs ; Generate all possible pairs ; Calculate bitwise AND of each pair ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int TotalAndPair ( int arr [ ] , int N ) { int totalAND = ( 1 << 30 ) - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalAND &= arr [ i ] & arr [ j ] ; } } return totalAND ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalAndPair ( arr , N ) ) ; } }


"Bitwise AND of sub | Java implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; Driver code"
How can the above be solved in Java?

import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; import java . io . * ; class GFG { static int closetAND ( int arr [ ] , int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 10 } ; int n = arr . length ; int k = 2 ; System . out . println ( closetAND ( arr , n , k ) ) ; } }


"Bitwise AND of sub | Java implementation of the approach ; Function to return the minimum possible value of | K - X | where X is the bitwise AND of the elements of some sub - array ; Check all possible sub - arrays ; Find the overall minimum ; No need to perform more AND operations as | k - X | will increase ; Driver code"
How can the above be solved in Java?

class GFG { static int closetAND ( int arr [ ] , int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; if ( X <= k ) break ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 10 } ; int n = arr . length ; int k = 2 ; System . out . println ( closetAND ( arr , n , k ) ) ; } }


"Bitwise OR ( or | ) of a range | Java Program to find the bitwise OR of all the integers in range L - R ; Returns the Most Significant Bit Position ( MSB ) ; Returns the Bitwise OR of all integers between L and R ; Find the MSB position in L ; Find the MSB position in R ; Add this value until msb_p1 and msb_p2 are same ; ; Calculate msb_p1 and msb_p2 ; Find the max of msb_p1 and msb_p2 ; Set all the bits from msb_p1 upto 0 th bit in the result ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int MSBPosition ( long N ) { int msb_p = - 1 ; while ( N > 0 ) { N = N >> 1 ; msb_p ++ ; } return msb_p ; } static long findBitwiseOR ( long L , long R ) { long res = 0 ; int msb_p1 = MSBPosition ( L ) ; int msb_p2 = MSBPosition ( R ) ; while ( msb_p1 == msb_p2 ) { long res_val = ( 1 << msb_p1 ) ; res += res_val ; L -= res_val ; R -= res_val ; msb_p1 = MSBPosition ( L ) ; msb_p2 = MSBPosition ( R ) ; } msb_p1 = Math . max ( msb_p1 , msb_p2 ) ; for ( int i = msb_p1 ; i >= 0 ; i -- ) { long res_val = ( 1 << i ) ; res += res_val ; } return res ; } public static void main ( String [ ] args ) { int L = 12 , R = 18 ; System . out . println ( findBitwiseOR ( L , R ) ) ; } }


"Bitwise OR ( | ) of all even number from 1 to N | Java implementation of the above approach ; Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code"
How can the above be solved in Java?

class GFG { static int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; return ( int ) Math . pow ( 2 , bitCount ) - 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }


"Bitwise OR ( | ) of all even number from 1 to N | Java implementation of the above approach ; Function to return the bitwise OR of all the even numbers upto N ; Initialize result as 2 ; Driver code"
How can the above be solved in Java?

class GFG { static int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } static public void main ( String args [ ] ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }


"Bitwise OR of Bitwise AND of all subsets of an Array for Q queries | java program for the above approach ; Function to find the OR of AND of all subsets of the array for each query ; An array to store the bits ; Itearte for all the bits ; Iterate over all the numbers and store the bits in bits [ ] array ; Itearte over all the queries ; Replace the bits of the value at arr [ queries [ p ] [ 0 ] ] with the bits of queries [ p ] [ 1 ] ; Substitute the value in the array ; Find OR of the bits [ ] array ; Print the answer ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void Or_of_Ands_for_each_query ( int arr [ ] , int n , int queries [ ] [ ] , int q ) { int bits [ ] = new int [ 32 ] ; Arrays . fill ( bits , 0 ) ; for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( 1 << i ) & arr [ j ] ) != 0 ) { bits [ i ] ++ ; } } } for ( int p = 0 ; p < q ; p ++ ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( 1 << i ) & arr [ queries [ p ] [ 0 ] ] ) != 0 ) { bits [ i ] -- ; } if ( ( queries [ p ] [ 1 ] & ( 1 << i ) ) != 0 ) { bits [ i ] ++ ; } } arr [ queries [ p ] [ 0 ] ] = queries [ p ] [ 1 ] ; int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( bits [ i ] != 0 ) { ans |= ( 1 << i ) ; } } System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int n = 3 , q = 2 ; int arr [ ] = { 3 , 5 , 7 } ; int queries [ ] [ ] = { { 1 , 2 } , { 2 , 1 } } ; Or_of_Ands_for_each_query ( arr , n , queries , q ) ; } }


"Bitwise OR of N binary strings | Java implementation of the approach ; Function to return the bitwise OR of all the binary strings ; Get max size and reverse each string Since we have to perform OR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform OR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Utility reverse ; Driver code"
How can the above be solved in Java?

class GFG { static String strBitwiseOR ( String [ ] arr , int n ) { String res = " " ; int max_size = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max_size = Math . max ( max_size , ( int ) arr [ i ] . length ( ) ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_size - arr [ i ] . length ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_size ; i ++ ) { int curr_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] . charAt ( i ) - '0' ) ; res += ( char ) ( curr_bit + '0' ) ; } res = reverse ( res ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { public static void main ( String [ ] args ) { String arr [ ] = { "10" , "11" , "1000001" } ; int n = arr . length ; System . out . println ( strBitwiseOR ( arr , n ) ) ; } }


"Bitwise OR of all unordered pairs from a given array | Java program to implement the above approach ; Function to find the bitwise OR of all possible pairs of the array ; Stores bitwise OR of all possible pairs of arr [ ] ; Traverse the array arr [ ] ; Update totalOR ; Return bitwise OR of all possible pairs of arr [ ] ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int TotalBitwiseORPair ( int arr [ ] , int N ) { int totalOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalBitwiseORPair ( arr , N ) ) ; } }


"Bitwise Operations on Digits of a Number | Java implementation of the approach ; Function to find the digits ; Function to Find OR of all digits of a number ; Find OR of all digits ; return OR of digits ; Function to Find AND of all digits of a number ; Find AND of all digits ; return AND of digits ; Function to Find XOR of all digits of a number ; Find XOR of all digits ; return XOR of digits ; Driver code ; Find and store all digits ; Find XOR of digits ; Find OR of digits ; Find AND of digits ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int [ ] digit = new int [ 100000 ] ; static int findDigits ( int n ) { int count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } static int OR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } static int AND_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } static int XOR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } static void bitwise_operation ( int N ) { int countOfDigit = findDigits ( N ) ; System . out . print ( " XOR ▁ = ▁ " + XOR_of_Digits ( N , countOfDigit ) + "
"); System . out . print ( " OR ▁ = ▁ " + OR_of_Digits ( N , countOfDigit ) + "
"); System . out . print ( " AND ▁ = ▁ " + AND_of_Digits ( N , countOfDigit ) + "
"); } public static void main ( String [ ] args ) { int N = 123456 ; bitwise_operation ( N ) ; } }


"Bitwise XOR of Bitwise AND of all pairs from two given arrays | Java program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores XOR of array arr1 [ ] ; Stores XOR of array arr2 [ ] ; Traverse the array arr1 [ ] ; Traverse the array arr2 [ ] ; Return the result ; Driver Code ; Input"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int XORS1 = 0 ; int XORS2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { XORS1 ^= arr1 [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { XORS2 ^= arr2 [ i ] ; } return ( XORS1 & XORS2 ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . println ( findXORS ( arr1 , arr2 , N , M ) ) ; } }


"Bitwise XOR of Bitwise AND of all pairs from two given arrays | Java program for the above approach ; Function to find the Bitwise XOR of Bitwise AND of all pairs from the arrays arr1 [ ] and arr2 [ ] ; Stores the result ; Iterate over the range [ 0 , N - 1 ] ; Iterate over the range [ 0 , M - 1 ] ; Stores Bitwise AND of the pair { arr1 [ i ] , arr2 [ j ] } ; Update res ; Return the res ; Driver Code ; Input"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . print ( findXORS ( arr1 , arr2 , N , M ) ) ; } }


"Bitwise XOR of a Binary array | Java implementation of the approach ; Function to return the reverse string ; Function to return the bitwise XOR of all the binary strings ; Get max size and reverse each string Since we have to perform XOR operation on bits from right to left Reversing the string will make it easier to perform operation from left to right ; Add 0 s to the end of strings if needed ; Perform XOR operation on each bit ; Reverse the resultant string to get the final string ; Return the final string ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static String reverse ( String str ) { String rev = " " ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) rev = rev + str . charAt ( i ) ; return rev ; } static String strBitwiseXOR ( String [ ] arr , int n ) { String result = " " ; int max_len = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max_len = Math . max ( max_len , ( int ) arr [ i ] . length ( ) ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_len - arr [ i ] . length ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_len ; i ++ ) { int pres_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) pres_bit = pres_bit ^ ( arr [ j ] . charAt ( i ) - '0' ) ; result += ( char ) ( pres_bit + '0' ) ; } result = reverse ( result ) ; return result ; } public static void main ( String [ ] args ) { String [ ] arr = { "1000" , "10001" , "0011" } ; int n = arr . length ; System . out . print ( strBitwiseXOR ( arr , n ) ) ; } }


"Bitwise XOR of all odd numbers from a given range | Java program for the above approach ; Function to calculate Bitwise XOR of odd numbers in the range [ 1 , N ] ; N & 3 is equivalent to n % 4 ; If n is multiple of 4 ; If n % 4 gives remainder 1 ; If n % 4 gives remainder 2 ; If n % 4 gives remainder 3 ; Function to find the XOR of odd numbers less than or equal to N ; If number is even ; Print the answer ; If number is odd ; Print the answer ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }


"Bitwise XOR of all unordered pairs from a given array | Java program to implement the above approach ; Function to get bitwise XOR of all possible pairs of the given array ; Stores bitwise XOR of all possible pairs ; Generate all possible pairs and calculate bitwise XOR of all possible pairs ; Calculate bitwise XOR of each pair ; Driver code"
How can the above be solved in Java?

class GFG { public static int TotalXorPair ( int arr [ ] , int N ) { int totalXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalXOR ^= arr [ i ] ^ arr [ j ] ; } } return totalXOR ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . print ( TotalXorPair ( arr , N ) ) ; } }


"Bitwise and ( or & ) of a range | An efficient Java program to find bit - wise & of all numbers from x to y . ; Find position of MSB in n . For example if n = 17 , then position of MSB is 4. If n = 7 , value of MSB is 3 ; Function to find Bit - wise & of all numbers from x to y . ; long res = 0 ; Initialize result ; Find positions of MSB in x and y ; If positions are not same , return ; Add 2 ^ msb_p1 to result ; subtract 2 ^ msb_p1 from x and y . ; Driver code"
How can the above be solved in Java?

class GFG { static int msbPos ( long n ) { int msb_p = - 1 ; while ( n > 0 ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } static long andOperator ( long x , long y ) { while ( x > 0 && y > 0 ) { int msb_p1 = msbPos ( x ) ; int msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; long msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } public static void main ( String [ ] args ) { long x = 10 , y = 15 ; System . out . print ( andOperator ( x , y ) ) ; } }


"Bitwise operations on Subarrays of size K | Java program for maximum values of each bitwise OR operation on element of subarray of size K ; Function to convert bit array to decimal number ; Function to find maximum values of each bitwise OR operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation for removed element ; Perform operation for added_element ; Taking maximum value ; Return the result ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int build_num ( int bit [ ] ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] > 0 ) ans += ( 1 << i ) ; return ans ; } static int maximumOR ( int arr [ ] , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int max_or = build_num ( bit ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } max_or = Math . max ( build_num ( bit ) , max_or ) ; } return max_or ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . length ; System . out . print ( maximumOR ( arr , n , k ) ) ; } }


"Bitwise operations on Subarrays of size K | Java program for minimum values of each bitwise AND operation on elements of subarray of size K ; Function to convert bit array to decimal number ; Function to find minimum values of each bitwise AND operation on element of subarray of size K ; Maintain an integer array bit [ ] of size 32 all initialized to 0 ; Create a sliding window of size k ; Function call ; Perform operation to removed element ; Perform operation to add element ; Taking minimum value ; Return the result ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function call"
How can the above be solved in Java?

class GFG { static int build_num ( int bit [ ] , int k ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == k ) ans += ( 1 << i ) ; return ans ; } static int minimumAND ( int arr [ ] , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } } int min_and = build_num ( bit , k ) ; for ( int i = k ; i < n ; i ++ ) { for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i - k ] & ( 1 << j ) ) > 0 ) bit [ j ] -- ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( arr [ i ] & ( 1 << j ) ) > 0 ) bit [ j ] ++ ; } min_and = Math . min ( build_num ( bit , k ) , min_and ) ; } return min_and ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 3 , 6 , 11 , 13 } ; int k = 3 ; int n = arr . length ; System . out . print ( minimumAND ( arr , n , k ) ) ; } }


"Bitwise operations on Subarrays of size K | Java program to find the subarray with minimum XOR ; Function to find the minimum XOR of the subarray of size K ; K must be smaller than or equal to n ; Initialize the beginning index of result ; Compute XOR sum of first subarray of size K ; Initialize minimum XOR sum as current xor ; Traverse from ( k + 1 ) ' th ▁ ▁ element ▁ to ▁ n ' th element ; XOR with current item and first item of previous subarray ; Update result if needed ; Print the minimum XOR ; Driver Code ; Given array arr [ ] ; Given subarray size K ; Function call"
How can the above be solved in Java?

class GFG { static void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } System . out . println ( min_xor ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . length ; findMinXORSubarray ( arr , n , k ) ; } }


"Bitwise recursive addition of two integers | Java program to do recursive addition of two integers ; If bitwise & is 0 , then there is not going to be any carry . Hence result of XOR is addition . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int add ( int x , int y ) { int keep = ( x & y ) << 1 ; int res = x ^ y ; if ( keep == 0 ) return res ; return add ( keep , res ) ; } public static void main ( String [ ] args ) { System . out . println ( add ( 15 , 38 ) ) ; } }


"Block swap algorithm for array rotation |  ; Wrapper over the recursive function leftRotateRec ( ) It left rotates arr [ ] by d . ; Return If number of elements to be rotated is zero or equal to array size ; If number of elements to be rotated is exactly half of array size ; If A is shorter ; If B is shorter ; function to print an array ; This function swaps d elements starting at index fi with d elements starting at index si ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static void leftRotate ( int arr [ ] , int d , int n ) { leftRotateRec ( arr , 0 , d , n ) ; } public static void leftRotateRec ( int arr [ ] , int i , int d , int n ) { if ( d == 0 d == n ) return ; if ( n - d == d ) { swap ( arr , i , n - d + i , d ) ; return ; } if ( d < n - d ) { swap ( arr , i , n - d + i , d ) ; leftRotateRec ( arr , i , d , n - d ) ; } else { swap ( arr , i , d , n - d ) ; leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; } } public static void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void swap ( int arr [ ] , int fi , int si , int d ) { int i , temp ; for ( i = 0 ; i < d ; i ++ ) { temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; } }


"Block swap algorithm for array rotation | Java code for above implementation ; A is shorter ; B is shorter ; Finally , block swap A and B"
How can the above be solved in Java?

static void leftRotate ( int arr [ ] , int d , int n ) { int i , j ; if ( d == 0 d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }


"Blum Integer | Java implementation to check If a number is a Blum integer ; Function to cheek if number is Blum Integer ; to store prime numbers from 2 to n ; If prime [ i ] is not changed , then it is a prime ; Update all multiples of p ; to check if the given odd integer is Blum Integer or not ; checking the factors are of 4 t + 3 form or not ; driver code ; give odd integer greater than 20"
How can the above be solved in Java?

import java . util . * ; class GFG { public static boolean isBlumInteger ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { if ( ( n % i == 0 ) && ( ( i - 3 ) % 4 ) == 0 ) { int q = n / i ; return ( q != i && prime [ q ] && ( q - 3 ) % 4 == 0 ) ; } } } return false ; } public static void main ( String [ ] args ) { int n = 249 ; if ( isBlumInteger ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Boggle ( Find all possible words in a board of characters ) | Set 1 | Java program for Boggle game ; Let the given dictionary be following ; A given function to check if a given string is present in dictionary . The implementation is naive for simplicity . As per the question dictionary is given to us . ; Linearly search all words ; A recursive function to print all words present on boggle ; Mark current cell as visited and append current character to str ; If str is present in dictionary , then print it ; Traverse 8 adjacent cells of boggle [ i ] [ j ] ; Erase current character from string and mark visited of current cell as false ; Prints all words present in dictionary . ; Mark all characters as not visited ; Initialize current string ; Consider every character and look for all words starting with this character ; Driver program to test above function"
How can the above be solved in Java?

class GFG { static final String dictionary [ ] = { " GEEKS " , " FOR " , " QUIZ " , " GUQ " , " EE " } ; static final int n = dictionary . length ; static final int M = 3 , N = 3 ; static boolean isWord ( String str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . equals ( dictionary [ i ] ) ) return true ; return false ; } static void findWordsUtil ( char boggle [ ] [ ] , boolean visited [ ] [ ] , int i , int j , String str ) { visited [ i ] [ j ] = true ; str = str + boggle [ i ] [ j ] ; if ( isWord ( str ) ) System . out . println ( str ) ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row ] [ col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str = " " + str . charAt ( str . length ( ) - 1 ) ; visited [ i ] [ j ] = false ; } static void findWords ( char boggle [ ] [ ] ) { boolean visited [ ] [ ] = new boolean [ M ] [ N ] ; String str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } public static void main ( String args [ ] ) { char boggle [ ] [ ] = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; System . out . println ( " Following ▁ words ▁ of ▁ dictionary ▁ are ▁ present " ) ; findWords ( boggle ) ; } }


"Boolean Parenthesization Problem | DP |  ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static int countWays ( int N , String S ) { int dp [ ] [ ] [ ] = new int [ N + 1 ] [ N + 1 ] [ 2 ] ; for ( int row [ ] [ ] : dp ) for ( int col [ ] : row ) Arrays . fill ( col , - 1 ) ; return parenthesis_count ( S , 0 , N - 1 , 1 , dp ) ; } public static int parenthesis_count ( String str , int i , int j , int isTrue , int [ ] [ ] [ ] dp ) { if ( i > j ) return 0 ; if ( i == j ) { if ( isTrue == 1 ) { return ( str . charAt ( i ) == ' T ' ) ? 1 : 0 ; } else { return ( str . charAt ( i ) == ' F ' ) ? 1 : 0 ; } } if ( dp [ i ] [ j ] [ isTrue ] != - 1 ) return dp [ i ] [ j ] [ isTrue ] ; int temp_ans = 0 ; int leftTrue , rightTrue , leftFalse , rightFalse ; for ( int k = i + 1 ; k <= j - 1 ; k = k + 2 ) { if ( dp [ i ] [ k - 1 ] [ 1 ] != - 1 ) leftTrue = dp [ i ] [ k - 1 ] [ 1 ] ; else { leftTrue = parenthesis_count ( str , i , k - 1 , 1 , dp ) ; } if ( dp [ i ] [ k - 1 ] [ 0 ] != - 1 ) leftFalse = dp [ i ] [ k - 1 ] [ 0 ] ; else { leftFalse = parenthesis_count ( str , i , k - 1 , 0 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 1 ] != - 1 ) rightTrue = dp [ k + 1 ] [ j ] [ 1 ] ; else { rightTrue = parenthesis_count ( str , k + 1 , j , 1 , dp ) ; } if ( dp [ k + 1 ] [ j ] [ 0 ] != - 1 ) rightFalse = dp [ k + 1 ] [ j ] [ 0 ] ; else { rightFalse = parenthesis_count ( str , k + 1 , j , 0 , dp ) ; } if ( str . charAt ( k ) == ' & ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue + leftFalse * rightFalse ; } } else if ( str . charAt ( k ) == ' ▁ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightTrue + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftFalse * rightFalse ; } } else if ( str . charAt ( k ) == ' ^ ' ) { if ( isTrue == 1 ) { temp_ans = temp_ans + leftTrue * rightFalse + leftFalse * rightTrue ; } else { temp_ans = temp_ans + leftTrue * rightTrue + leftFalse * rightFalse ; } } dp [ i ] [ j ] [ isTrue ] =...


"Boolean Parenthesization Problem | DP |  ; CountWays function ; Base Condition ; Count number of True in left Partition ; Count number of False in left Partition ; Count number of True in right Partition ; Count number of False in right Partition ; Evaluate AND operation ; Evaluate OR operation ; Evaluate XOR operation ; Driver code ; We obtain the string T | T & F ^ T ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
How can the above be solved in Java?


"Boolean Parenthesization Problem | DP |  ; Returns count of all possible parenthesizations that lead to result true for a boolean expression with symbols like true and false and operators like & , | and ^ filled between symbols ; Fill diaginal entries first All diagonal entries in T [ i ] [ i ] are 1 if symbol [ i ] is T ( true ) . Similarly , all F [ i ] [ i ] entries are 1 if symbol [ i ] is F ( False ) ; Now fill T [ i ] [ i + 1 ] , T [ i ] [ i + 2 ] , T [ i ] [ i + 3 ] ... in order And F [ i ] [ i + 1 ] , F [ i ] [ i + 2 ] , F [ i ] [ i + 3 ] ... in order ; Find place of parenthesization using current value of gap ; Store Total [ i ] [ k ] and Total [ k + 1 ] [ j ] ; Follow the recursive formulas according to the current operator ; Driver code ; There are 4 ways ( ( T T ) & ( F ^ T ) ) , ( T | ( T & ( F ^ T ) ) ) , ( ( ( T T ) & F ) ^ T ) and ( T | ( ( T & F ) ^ T ) )"
How can the above be solved in Java?

class GFG { static int countParenth ( char symb [ ] , char oper [ ] , int n ) { int F [ ] [ ] = new int [ n ] [ n ] ; int T [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i ] [ i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i ] [ i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int gap = 1 ; gap < n ; ++ gap ) { for ( int i = 0 , j = gap ; j < n ; ++ i , ++ j ) { T [ i ] [ j ] = F [ i ] [ j ] = 0 ; for ( int g = 0 ; g < gap ; g ++ ) { int k = i + g ; int tik = T [ i ] [ k ] + F [ i ] [ k ] ; int tkj = T [ k + 1 ] [ j ] + F [ k + 1 ] [ j ] ; if ( oper [ k ] == ' & ' ) { T [ i ] [ j ] += T [ i ] [ k ] * T [ k + 1 ] [ j ] ; F [ i ] [ j ] += ( tik * tkj - T [ i ] [ k ] * T [ k + 1 ] [ j ] ) ; } if ( oper [ k ] == ' ▁ ' ) { F [ i ] [ j ] += F [ i ] [ k ] * F [ k + 1 ] [ j ] ; T [ i ] [ j ] += ( tik * tkj - F [ i ] [ k ] * F [ k + 1 ] [ j ] ) ; } if ( oper [ k ] == ' ^ ' ) { T [ i ] [ j ] += F [ i ] [ k ] * T [ k + 1 ] [ j ] + T [ i ] [ k ] * F [ k + 1 ] [ j ] ; F [ i ] [ j ] += T [ i ] [ k ] * T [ k + 1 ] [ j ] + F [ i ] [ k ] * F [ k + 1 ] [ j ] ; } } } } return T [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { char symbols [ ] = " TTFT " . toCharArray ( ) ; char operators [ ] = " | & ^ " . toCharArray ( ) ; int n = symbols . length ; System . out . println ( countParenth ( symbols , operators , n ) ) ; } }


"Booth ’ s Multiplication Algorithm | Java code to implement booth 's algorithm ; function to perform adding in the accumulator ; updating accumulator with A = A + BR ; function to find the number 's complement ; function ro perform right shift ; function to display operations ; accumulator content ; multiplier content ; Function to implement booth 's algo ; SECOND CONDITION ; subtract BR from accumulator ; THIRD CONDITION ; add BR to accumulator ; FIRST CONDITION ; decrement counter ; Driver code ; Number of multiplicand bit ; multiplicand ; copy multiplier to temp array mt [ ] ; No . of multiplier bit ; sequence counter ; multiplier"
How can the above be solved in Java?

 class GFG { static void add ( int ac [ ] , int x [ ] , int qrn ) { int i , c = 0 ; for ( i = 0 ; i < qrn ; i ++ ) { ac [ i ] = ac [ i ] + x [ i ] + c ; if ( ac [ i ] > 1 ) { ac [ i ] = ac [ i ] % 2 ; c = 1 ; } else { c = 0 ; } } } static void complement ( int a [ ] , int n ) { int i ; int [ ] x = new int [ 8 ] ; x [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + 1 ) % 2 ; } add ( a , x , n ) ; } static void rightShift ( int ac [ ] , int qr [ ] , int qn , int qrn ) { int temp , i ; temp = ac [ 0 ] ; qn = qr [ 0 ] ; System . out . print ( " 	 	 rightShift 	 " ) ; for ( i = 0 ; i < qrn - 1 ; i ++ ) { ac [ i ] = ac [ i + 1 ] ; qr [ i ] = qr [ i + 1 ] ; } qr [ qrn - 1 ] = temp ; } static void display ( int ac [ ] , int qr [ ] , int qrn ) { int i ; for ( i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } System . out . print ( " 	 " ) ; for ( i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( qr [ i ] ) ; } } static void boothAlgorithm ( int br [ ] , int qr [ ] , int mt [ ] , int qrn , int sc ) { int qn = 0 ; int [ ] ac = new int [ 10 ] ; int temp = 0 ; System . out . print ( "qn	q[n+1]		BR		AC	QR		sc
"); System . out . print ( " 	 	 	 initial 	 	 " ) ; display ( ac , qr , qrn ) ; System . out . print ( " 	 	 " + sc + "
"); while ( sc != 0 ) { System . out . print ( qr [ 0 ] + " 	 " + qn ) ; if ( ( qn + qr [ 0 ] ) == 1 ) { if ( temp == 0 ) { add ( ac , mt , qrn ) ; System . out . print ( " 	 	 A ▁ = ▁ A ▁ - ▁ BR 	 " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } temp = 1 ; } else if ( temp == 1 ) { add ( ac , br , qrn ) ; System . out . print ( " 	 	 A ▁ = ▁ A ▁ + ▁ BR 	 " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } temp = 0 ; } System . out . print ( "
"); rightShift ( ac , qr , qn , qrn ) ; } else if ( qn - qr [ 0 ] == 0 ) { rightShift ( ac , qr , qn , qrn ) ; } display ( ac , qr , qrn ) ; System . out . print ( " 	 " ) ; sc -- ; System . out . print ( " 	 " + sc + "
"); } } static void reverse (...


"Bottom View of a Binary Tree | Java Program to print Bottom View of Binary Tree ; Tree node class ; data of the node ; horizontal distance of the node ; left and right references ; Constructor of tree node ; Tree class ; root node of tree ; Default constructor ; Parameterized tree constructor ; Method that prints the bottom view . ; Initialize a variable ' hd ' with 0 for the root element . ; TreeMap which stores key value pair sorted on key value ; Queue to store tree nodes in level order traversal ; Assign initialized horizontal distance value to root node and add it to the queue . ; In STL , push ( ) is used enqueue an item ; Loop until the queue is empty ( standard level order loop ) ; Extract the horizontal distance value from the dequeued tree node . ; Put the dequeued tree node to TreeMap having key as horizontal distance . Every time we find a node having same horizontal distance we need to replace the data in the map . ; If the dequeued node has a left child add it to the queue with a horizontal distance hd - 1. ; If the dequeued node has a right child add it to the queue with a horizontal distance hd + 1. ; Extract the entries of map into a set to traverse an iterator over that . ; Make an iterator ; Traverse the map elements using the iterator . ; Main driver class"
How can the above be solved in Java?

import java . util . * ; import java . util . Map . Entry ; class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } class Tree { Node root ; public Tree ( ) { } public Tree ( Node node ) { root = node ; } public void bottomView ( ) { if ( root == null ) return ; int hd = 0 ; Map < Integer , Integer > map = new TreeMap < > ( ) ; Queue < Node > queue = new LinkedList < Node > ( ) ; root . hd = hd ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . remove ( ) ; hd = temp . hd ; map . put ( hd , temp . data ) ; if ( temp . left != null ) { temp . left . hd = hd - 1 ; queue . add ( temp . left ) ; } if ( temp . right != null ) { temp . right . hd = hd + 1 ; queue . add ( temp . right ) ; } } Set < Entry < Integer , Integer > > set = map . entrySet ( ) ; Iterator < Entry < Integer , Integer > > iterator = set . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry < Integer , Integer > me = iterator . next ( ) ; System . out . print ( me . getValue ( ) + " ▁ " ) ; } } } public class BottomView { public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; Tree tree = new Tree ( root ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; tree . bottomView ( ) ; } }


"Bottom View of a Binary Tree | Java program to print Bottom View of Binary Tree ; Tree node class ; Data of the node ; Horizontal distance of the node ; Left and right references ; Constructor of tree node ; printBottomViewUtil function ; Base case ; If node for a particular horizontal distance is not present , add to the map . ; Compare height for already present node at similar horizontal distance ; Recur for left subtree ; Recur for right subtree ; printBottomView function ; Map to store Horizontal Distance , Height and Data . ; Prints the values stored by printBottomViewUtil ( ) ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } static void printBottomViewUtil ( Node root , int curr , int hd , TreeMap < Integer , int [ ] > m ) { if ( root == null ) return ; if ( ! m . containsKey ( hd ) ) { m . put ( hd , new int [ ] { root . data , curr } ) ; } else { int [ ] p = m . get ( hd ) ; if ( p [ 1 ] <= curr ) { p [ 1 ] = curr ; p [ 0 ] = root . data ; } m . put ( hd , p ) ; } printBottomViewUtil ( root . left , curr + 1 , hd - 1 , m ) ; printBottomViewUtil ( root . right , curr + 1 , hd + 1 , m ) ; } static void printBottomView ( Node root ) { TreeMap < Integer , int [ ] > m = new TreeMap < > ( ) ; printBottomViewUtil ( root , 0 , 0 , m ) ; for ( int val [ ] : m . values ( ) ) { System . out . print ( val [ 0 ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; printBottomView ( root ) ; } }