"Equation of a normal to a Circle from a given point | Java program for the above approach ; Function to calculate the slope ; Store the coordinates the center of the circle ; If slope becomes infinity ; Stores the slope ; If slope is zero ; Return the result ; Function to find the equation of the normal to a circle from a given point ; Stores the slope of the normal ; If slope becomes infinity ; If slope is zero ; Otherwise , print the equation of the normal ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static double normal_slope ( double a , double b , double x1 , double y1 ) { double g = a / 2 ; double f = b / 2 ; if ( g - x1 == 0 ) return ( - 1 ) ; double slope = ( f - y1 ) / ( g - x1 ) ; if ( slope == 0 ) return ( - 2 ) ; return slope ; } static void normal_equation ( double a , double b , double x1 , double y1 ) { double slope = normal_slope ( a , b , x1 , y1 ) ; if ( slope == - 1 ) { System . out . print ( " x ▁ = ▁ " + x1 ) ; } if ( slope == - 2 ) { System . out . print ( " y ▁ = ▁ " + y1 ) ; } if ( slope != - 1 && slope != - 2 ) { x1 *= - slope ; x1 += y1 ; if ( x1 > 0 ) System . out . print ( " y ▁ = ▁ " + slope + " x ▁ + ▁ " + x1 ) ; else System . out . print ( " y ▁ = ▁ " + slope + " x ▁ " + x1 ) ; } } public static void main ( String [ ] args ) { int a = 4 , b = 6 ; int x1 = 12 , y1 = 14 ; normal_equation ( a , b , x1 , y1 ) ; } }


"Equation of a straight line passing through a point and making a given angle with a given line | Java program for the above approach ; Function to find slope of given line ; Special case when slope of line is infinity or is perpendicular to x - axis ; Function to find equations of lines passing through the given point and making an angle with given line ; Store slope of given line ; Convert degrees to radians ; Special case when slope of given line is infinity : In this case slope of one line will be equal to alfa and the other line will be equal to ( 180 - alfa ) ; In this case slope of required lines can 't be  infinity ; g and f are the variables of required equations ; Print first line equation ; Print second line equation ; Special case when slope of required line becomes infinity ; General case ; g and f are the variables of required equations ; Print first line equation ; Print second line equation ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static double line_slope ( double a , double b ) { if ( a != 0 ) return - b / a ; else return ( - 2 ) ; } static void line_equation ( double a , double b , double c , double x1 , double y1 , double alfa ) { double given_slope = line_slope ( a , b ) ; double x = alfa * 3.14159 / 180 ; if ( given_slope == - 2 ) { double slope_1 = Math . tan ( x ) ; double slope_2 = Math . tan ( 3.14159 - x ) ; int g = ( int ) x1 , f = ( int ) x1 ; g *= ( - slope_1 ) ; g += y1 ; if ( g > 0 ) System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_1 * 100.0 ) / 100.0 ) + " x ▁ + " + ( Math . round ( g * 100.0 ) / 100.0 ) ) ; if ( g <= 0 ) System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_1 * 100.0 ) / 100.0 ) + " x ▁ " + ( Math . round ( g * 100.0 ) / 100.0 ) ) ; f *= ( - slope_2 ) ; f += y1 ; if ( f > 0 ) { System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_2 * 100.0 ) / 100.0 ) + " x ▁ + " + ( Math . round ( f * 100.0 ) / 100.0 ) ) ; } if ( f <= 0 ) System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_1 * 100.0 ) / 100.0 ) + " x ▁ " + ( Math . round ( g * 100.0 ) / 100.0 ) ) ; return ; } if ( 1 - Math . tan ( x ) * given_slope == 0 ) { System . out . println ( " x ▁ = ▁ " + ( Math . round ( x1 * 100.0 ) / 100.0 ) ) ; } if ( 1 + Math . tan ( x ) * given_slope == 0 ) { System . out . println ( " x ▁ = ▁ " + ( Math . round ( x1 * 100.0 ) / 100.0 ) ) ; } double slope_1 = ( given_slope + Math . tan ( x ) ) / ( 1 - Math . tan ( x ) * given_slope ) ; double slope_2 = ( given_slope - Math . tan ( x ) ) / ( 1 + Math . tan ( x ) * given_slope ) ; int g = ( int ) x1 , f = ( int ) x1 ; g *= ( - slope_1 ) ; g += y1 ; if ( g > 0 && 1 - Math . tan ( x ) * given_slope != 0 ) System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_1 * 100.0 ) / 100.0 ) + " x ▁ + " + ( Math . round ( g * 100.0 ) / 100.0 ) ) ; if ( g <= 0 && 1 - Math . tan ( x ) * given_slope != 0 ) System . out . println ( " y ▁ = ▁ " + ( Math . round ( slope_1 * 100.0 ) / 100.0 ) + " x ▁ " + ( Math ....


"Equation of a straight line with perpendicular distance D from origin and an angle A between the perpendicular from origin and x | Java program for the approach ; Function to find equation of a line whose distance from origin and angle made by the perpendicular from origin with x - axis is given ; Convert angle from degree to radian ; Handle the special case ; Calculate the sin and cos of angle ; Print the equation of the line ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

class GFG { static void findLine ( int distance , float degree ) { float x = ( float ) ( degree * 3.14159 / 180 ) ; if ( degree > 90 ) { System . out . print ( " Not ▁ Possible " ) ; return ; } float result_1 = ( float ) Math . sin ( x ) ; float result_2 = ( float ) Math . cos ( x ) ; System . out . print ( String . format ( " % .2f " , result_2 ) + " x ▁ + " + String . format ( " % .2f " , result_1 ) + " y ▁ = ▁ " + distance ) ; } public static void main ( String [ ] args ) { int D = 10 ; float A = 30 ; findLine ( D , A ) ; } }


"Equation of circle from center and radius | Java program to find the equation of circle . ; Function to find the equation of circle ; Printing result ; Driver code"
How can the above be solved in Java?

import java . util . * ; class solution { static void circle_equation ( double x1 , double y1 , double r ) { double a = - 2 * x1 ; double b = - 2 * y1 ; double c = ( r * r ) - ( x1 * x1 ) - ( y1 * y1 ) ; System . out . print ( " x ^ 2 ▁ + ▁ ( " + a + " ▁ x ) ▁ + ▁ " ) ; System . out . print ( " y ^ 2 ▁ + ▁ ( " + b + " ▁ y ) ▁ = ▁ " ) ; System . out . println ( c + " . " ) ; } public static void main ( String arr [ ] ) { double x1 = 2 , y1 = - 3 , r = 8 ; circle_equation ( x1 , y1 , r ) ; } }


"Equation of ellipse from its focus , directrix , and eccentricity | Java program to find equation of an ellipse using focus and directrix . ; Function to find equation of ellipse . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class solution { static void equation_ellipse ( float x1 , float y1 , float a , float b , float c , float e ) { float t = a * a + b * b ; float a1 = t - e * ( a * a ) ; float b1 = t - e * ( b * b ) ; float c1 = ( - 2 * t * x1 ) - ( 2 * e * c * a ) ; float d1 = ( - 2 * t * y1 ) - ( 2 * e * c * b ) ; float e1 = - 2 * e * a * b ; float f1 = ( - e * c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) ; System . out . println ( " Equation ▁ of ▁ ellipse ▁ is ▁ " ) ; System . out . print ( a1 + " ▁ x ^ 2 ▁ + ▁ " + b1 + " ▁ y ^ 2 ▁ + ▁ " + c1 + " ▁ x ▁ + ▁ " + d1 + " ▁ y ▁ + ▁ " + e1 + " ▁ xy ▁ + ▁ " + f1 + " ▁ = ▁ 0" ) ; } public static void main ( String arr [ ] ) { float x1 = 1 , y1 = 1 , a = 1 , b = - 1 , c = 3 , e = ( float ) 0.5 * ( float ) 0.5 ; equation_ellipse ( x1 , y1 , a , b , c , e ) ; } }


"Equation of parabola from its focus and directrix | Java program to find equation of a parbola using focus and directrix . ; Function to find equation of parabola . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class solution { static void equation_parabola ( float x1 , float y1 , float a , float b , float c ) { float t = a * a + b * b ; float a1 = t - ( a * a ) ; float b1 = t - ( b * b ) ; float c1 = ( - 2 * t * x1 ) - ( 2 * c * a ) ; float d1 = ( - 2 * t * y1 ) - ( 2 * c * b ) ; float e1 = - 2 * a * b ; float f1 = ( - c * c ) + ( t * x1 * x1 ) + ( t * y1 * y1 ) ; System . out . println ( " equation ▁ of ▁ parabola ▁ is ▁ " + a1 + " ▁ x ^ 2 ▁ + ▁ " + b1 + " ▁ y ^ 2 ▁ + ▁ " + c1 + " ▁ x ▁ + ▁ " + d1 + " ▁ y ▁ + ▁ " + e1 + " ▁ xy ▁ + ▁ " + f1 + " ▁ = ▁ 0 . " ) ; } public static void main ( String arr [ ] ) { float x1 = 0 ; float y1 = 0 ; float a = 3 ; float b = - 4 ; float c = 2 ; equation_parabola ( x1 , y1 , a , b , c ) ; } }


"Equation of straight line passing through a given point which bisects it into two equal line segments | Java implementation of the approach ; Function to print the equation of the required line ; Driver code"
How can the above be solved in Java?

class GFG { static void line ( double x0 , double y0 ) { double c = ( int ) ( 2 * y0 * x0 ) ; System . out . println ( y0 + " x " + " ▁ + ▁ " + x0 + " y ▁ = ▁ " + c ) ; } public static void main ( String [ ] args ) { double x0 = 4 , y0 = 3 ; line ( x0 , y0 ) ; } }


"Equidigital Numbers | Java program to find Equidigital Numbers till n ; Array to store all prime less than and equal to MAX . ; Utility function for sieve of sundaram ; In general Sieve of Sundaram , produces primes smaller than ( 2 * x + 2 ) for a number given number x . Since we want primes smaller than MAX , we reduce MAX to half This array is used to separate numbers of the form i + j + 2 ij from others where 1 <= i <= j ; Main logic of Sundaram . Mark all numbers which do not generate prime number by doing 2 * i + 1 ; Since 2 is a prime number ; Print other primes . Remaining primes are of the form 2 * i + 1 such that marked [ i ] is false . ; Returns true if n is a Equidigital number , else false . ; Count digits in original number ; Count all digits in prime factors of n pDigit is going to hold this value . ; Count powers of p in n ; If primes [ i ] is a prime factor , ; Count the power of prime factors ; Add its digits to pDigit . ; Add digits of power of prime factors to pDigit . ; If n != 1 then one prime factor still to be summed up ; ; If digits in prime factors and digits in original number are same , then return true . Else return false . ; Driver method ; Finding all prime numbers before limit . These numbers are used to find prime factors ."
How can the above be solved in Java?

import java . util . Vector ; import static java . lang . Math . sqrt ; class GFG { static final int MAX = 10000 ; static Vector < Integer > primes = new Vector < Integer > ( MAX + 1 ) ; static void sieveSundaram ( ) { boolean marked [ ] = new boolean [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static boolean isEquidigital ( int n ) { if ( n == 1 ) return true ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p = 0 ; for ( int i = 0 ; primes . elementAt ( i ) <= n / 2 ; i ++ ) { while ( n % primes . elementAt ( i ) == 0 ) { p = primes . elementAt ( i ) ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit == sumDigits ) ; } public static void main ( String [ ] args ) { sieveSundaram ( ) ; System . out . println ( " Printing ▁ first ▁ few ▁ Equidigital ▁ Numbers " + " ▁ using ▁ isEquidigital ( ) " ) ; for ( int i = 1 ; i < 20 ; i ++ ) if ( isEquidigital ( i ) ) System . out . print ( i + " ▁ " ) ; } }


"Equilibrium index of an array | Java program to find equilibrium index of an array ; Taking the prefixsum from front end array ; Taking the prefixsum from back end of array ; Checking for equilibrium index by compairing front and back sums ; If no equilibrium index found , then return - 1 ; Driver code"
How can the above be solved in Java?

class GFG { static int equilibrium ( int a [ ] , int n ) { if ( n == 1 ) return ( 0 ) ; int [ ] front = new int [ n ] ; int [ ] back = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 ) { front [ i ] = front [ i - 1 ] + a [ i ] ; } else { front [ i ] = a [ i ] ; } } for ( int i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { back [ i ] = back [ i + 1 ] + a [ i ] ; } else { back [ i ] = a [ i ] ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( front [ i ] == back [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . length ; System . out . println ( " First ▁ Point ▁ of ▁ equilibrium ▁ " + " is ▁ at ▁ index ▁ " + equilibrium ( arr , arr_size ) ) ; } }


"Equilibrium index of an array | Java program to find equilibrium index of an array ; function to find the equilibrium index ; Check for indexes one by one until an equilibrium index is found ; get left sum ; get right sum ; if leftsum and rightsum are same , then we are done ; return - 1 if no equilibrium index is found ; Driver code"
How can the above be solved in Java?

class EquilibriumIndex { int equilibrium ( int arr [ ] , int n ) { int i , j ; int leftsum , rightsum ; for ( i = 0 ; i < n ; ++ i ) { leftsum = 0 ; rightsum = 0 ; for ( j = 0 ; j < i ; j ++ ) leftsum += arr [ j ] ; for ( j = i + 1 ; j < n ; j ++ ) rightsum += arr [ j ] ; if ( leftsum == rightsum ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { EquilibriumIndex equi = new EquilibriumIndex ( ) ; int arr [ ] = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . length ; System . out . println ( equi . equilibrium ( arr , arr_size ) ) ; } }


"Equilibrium index of an array | Java program to find equilibrium index of an array ; function to find the equilibrium index ; initialize sum of whole array ; initialize leftsum ; Find sum of the whole array ; sum is now right sum for index i ; If no equilibrium index found , then return 0 ; Driver code"
How can the above be solved in Java?

class EquilibriumIndex { int equilibrium ( int arr [ ] , int n ) { int sum = 0 ; int leftsum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { sum -= arr [ i ] ; if ( leftsum == sum ) return i ; leftsum += arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { EquilibriumIndex equi = new EquilibriumIndex ( ) ; int arr [ ] = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . length ; System . out . println ( " First ▁ equilibrium ▁ index ▁ is ▁ " + equi . equilibrium ( arr , arr_size ) ) ; } }


"Euclidâ €“ Mullin Sequence | Java implementation of the approach ; Function to return the smallest prime factor of n ; Initialize i = 2 ; While i <= sqrt ( n ) ; If n is divisible by i ; Increment i ; Function to print the first n terms of the required sequence ; To store the product of the previous terms ; Traverse the prime numbers ; Current term will be smallest prime factor of ( 1 + product of all previous terms ) ; Print the current term ; Update the product ; Driver code ; Find the first 14 terms of the sequence"
How can the above be solved in Java?

import java . math . BigInteger ; class GFG { static BigInteger smallestPrimeFactor ( BigInteger n ) { BigInteger i = BigInteger . valueOf ( 2 ) ; while ( ( i . multiply ( i ) ) . compareTo ( n ) <= 0 ) { if ( n . mod ( i ) . compareTo ( BigInteger . ZERO ) == 0 ) return i ; i = i . add ( BigInteger . ONE ) ; } return n ; } static void solve ( BigInteger n ) { BigInteger product = BigInteger . ONE ; BigInteger i = BigInteger . ZERO ; while ( i . compareTo ( n ) < 0 ) { BigInteger num = smallestPrimeFactor ( product . add ( BigInteger . ONE ) ) ; System . out . print ( num + " ▁ " ) ; product = product . multiply ( num ) ; i = i . add ( BigInteger . ONE ) ; } } public static void main ( String [ ] args ) { BigInteger b = BigInteger . valueOf ( 14 ) ; solve ( b ) ; } }


"Euler 's Factorization method | Java program to implement Eulers Factorization algorithm ;  ; Recursive function to return gcd of a and b ; Function to return N as the sum of two squares in two possible ways ; Iterate a loop from 1 to Math . sqrt ( n ) ; If i * i is square check if there exists another integer such that h is a perfect square and i * i + h = n ; If h is perfect square ; Store in the sorted way ; If there is already a pair check if pairs are equal or not ; Insert the first pair ; If two pairs are found ; Function to find the factors ; Get pairs where a ^ 2 + b ^ 2 = n ; Number cannot be represented as sum of squares in two ways ; Assign a , b , c , d ; Swap if a < c because if a - c < 0 , GCD cant be computed . ; Compute the values of k , h , l , m using the formula mentioned in the approach ; Print the values of a , b , c , d and k , l , m , h ; Printing the factors ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static void sumOfSquares ( int n , Vector < pair > vp ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { int h = n - i * i , h1 = ( int ) Math . sqrt ( h ) ; if ( h1 * h1 == h ) { int a = Math . max ( h1 , i ) , b = Math . min ( h1 , i ) ; if ( vp . size ( ) == 1 && a != vp . get ( 0 ) . first ) vp . add ( new pair ( a , b ) ) ; if ( vp . size ( ) == 0 ) vp . add ( new pair ( a , b ) ) ; if ( vp . size ( ) == 2 ) return ; } } } static void findFactors ( int n ) { Vector < pair > vp = new Vector < > ( ) ; sumOfSquares ( n , vp ) ; if ( vp . size ( ) != 2 ) System . out . print ( " Factors ▁ Not ▁ Possible " ) ; int a , b , c , d ; a = vp . get ( 0 ) . first ; b = vp . get ( 0 ) . second ; c = vp . get ( 1 ) . first ; d = vp . get ( 1 ) . second ; if ( a < c ) { int t = a ; a = c ; c = t ; t = b ; b = d ; d = t ; } int k , h , l , m ; k = __gcd ( a - c , d - b ) ; h = __gcd ( a + c , d + b ) ; l = ( a - c ) / k ; m = ( d - b ) / k ; System . out . print ( " a ▁ = ▁ " + a + " 	 	 ( A ) ▁ a ▁ - ▁ c ▁ = ▁ " + ( a - c ) + " 	 	 k ▁ = ▁ gcd [ A , ▁ C ] ▁ = ▁ " + k + "
"); System . out . print ( " b ▁ = ▁ " + b + " 	 	 ( B ) ▁ a ▁ + ▁ c ▁ = ▁ " + ( a + c ) + " 	 	 h ▁ = ▁ gcd [ B , ▁ D ] ▁ = ▁ " + h + "
"); System . out . print ( " c ▁ = ▁ " + c + " 	 	 ( C ) ▁ d ▁ - ▁ b ▁ = ▁ " + ( d - b ) + " 	 	 l ▁ = ▁ A / k ▁ = ▁ " + l + "
"); System . out . print ( " d ▁ = ▁ " + d + " 	 	 ( D ) ▁ d ▁ + ▁ b ▁ = ▁ " + ( d + b ) + " 	 	 m ▁ = ▁ c / k ▁ = ▁ " + m + "
"); if ( k % 2 == 0 && h % 2 == 0 ) { k = k / 2 ; h = h / 2 ; System . out . print ( " Factors ▁ are : ▁ " + ( ( k ) * ( k ) + ( h ) * ( h ) ) + " ▁ " + ( l * l + m * m ) + "
"); } else { l = l / 2 ; m = m / 2 ; System . out . print ( " Factors ▁ are : ▁ " + ( ( l ) * ( l ) + ( m ) * ( m ) ) + " ▁ " + ( k * k + h * h...


"Euler 's Four Square Identity | Java code to verify euler 's four square identity ; function to check euler four square identity ; loops checking the sum of squares ; sum of 2 squares ; sum of 3 squares ; sum of 4 squares ; product of 2 numbers represented as sum of four squares i , j , k , l ; product of 2 numbers a and b represented as sum of four squares i , j , k , l ; Driver code ; given numbers can be represented as sum of 4 squares By euler 's  four square identity  product also can be  represented as sum  of 4 squares"
How can the above be solved in Java?

 import java . io . * ; class GFG { static void check_euler_four_square_identity ( int a , int b , int ab ) { int s = 0 ; for ( int i = 0 ; i * i <= ab ; i ++ ) { s = i * i ; for ( int j = i ; j * j <= ab ; j ++ ) { s = j * j + i * i ; for ( int k = j ; k * k <= ab ; k ++ ) { s = k * k + j * j + i * i ; for ( int l = k ; l * l <= ab ; l ++ ) { s = l * l + k * k + j * j + i * i ; if ( s == ab ) { System . out . print ( " i ▁ = ▁ " + i + "
"); System . out . print ( " j ▁ = ▁ " + j + "
"); System . out . print ( " k ▁ = ▁ " + k + "
"); System . out . print ( " l ▁ = ▁ " + l + "
"); System . out . print ( " Product ▁ of ▁ " + a + " ▁ and ▁ " + b ) ; System . out . print ( " ▁ can ▁ be ▁ written " + " ▁ as ▁ sum ▁ of ▁ squares ▁ of ▁ i , ▁ " + "j, k, l
"); System . out . print ( ab + " ▁ = ▁ " ) ; System . out . print ( i + " * " + i + " ▁ + ▁ " ) ; System . out . print ( j + " * " + j + " ▁ + ▁ " ) ; System . out . print ( k + " * " + k + " ▁ + ▁ " ) ; System . out . print ( l + " * " + l + "
"); System . out . println ( ) ; } } } } } } public static void main ( String [ ] args ) { int ab = a * b ; check_euler_four_square_identity ( a , b , ab ) ; } }


"Euler 's Totient Function | A simple java program to calculate Euler 's Totient Function ; Function to return GCD of a and b ; A simple method to evaluate Euler Totient Function ; Driver code"
How can the above be solved in Java?

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


"Euler 's Totient Function | Java program to calculate Euler ' s ▁ Totient ▁ Function ▁ using ▁ Euler ' s product formula ; Initialize result as n ; Consider all prime factors of n and for every prime factor p , multiply result with ( 1 - 1 / p ) ; Check if p is a prime factor . ; If yes , then update n and result ; If n has a prime factor greater than sqrt ( n ) ( There can be at - most one such prime factor ) ; Driver program to test above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int phi ( int n ) { float result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result *= ( 1.0 - ( 1.0 / ( float ) p ) ) ; } } if ( n > 1 ) result *= ( 1.0 - ( 1.0 / ( float ) n ) ) ; return ( int ) result ; } public static void main ( String args [ ] ) { int n ; for ( n = 1 ; n <= 10 ; n ++ ) System . out . println ( " phi ( " + n + " ) ▁ = ▁ " + phi ( n ) ) ; } }


"Euler 's Totient function for all numbers smaller than or equal to n | Java program for the above approach ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static long Euler_totient_function ( long n ) { long result = 1 ; for ( long i = 2 ; i * i <= n ; i ++ ) { long c = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { c ++ ; n /= i ; } } if ( c > 0 ) { long power = ( long ) Math . pow ( i , c - 1 ) ; long sm = ( long ) Math . pow ( i , c - 1 ) * ( i - 1 ) ; result *= sm ; } } if ( n > 1 ) { result *= ( n - 1 ) ; } return result ; } public static void main ( String [ ] args ) { for ( long i = 1 ; i < 13 ; i ++ ) { System . out . print ( " Euler _ totient _ function ( " + i + " ) : ▁ " ) ; System . out . println ( Euler_totient_function ( i ) ) ; } } }


"Euler 's Totient function for all numbers smaller than or equal to n | Java program to compute Totient function for all numbers smaller than or equal to n . ; Computes and prints totient of all numbers smaller than or equal to n . ; Create and initialize an array to store phi or totient values ; Compute other Phi values ; If phi [ p ] is not computed already , then number p is prime ; Phi of a prime number p is always equal to p - 1. ; Update phi values of all multiples of p ; Add contribution of p to its multiple i by multiplying with ( 1 - 1 / p ) ; Print precomputed phi values ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void computeTotient ( int n ) { long phi [ ] = new long [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) for ( int p = 2 ; p <= n ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= n ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } for ( int i = 1 ; i <= n ; i ++ ) System . out . println ( " Totient ▁ of ▁ " + i + " ▁ is ▁ " + phi [ i ] ) ; } public static void main ( String [ ] args ) { int n = 12 ; computeTotient ( n ) ; } }


"Euler Method for solving differential equation | Java program to find approximation of an ordinary differential equation using euler method ; Consider a differential equation dy / dx = ( x + y + xy ) ; Function for Euler formula ; Iterating till the point at which we need approximation ; Printing approximation ; Driver program ; Initial Values ; Value of x at which we need approximation"
How can the above be solved in Java?

import java . io . * ; class Euler { float func ( float x , float y ) { return ( x + y + x * y ) ; } void euler ( float x0 , float y , float h , float x ) { float temp = - 0 ; while ( x0 < x ) { temp = y ; y = y + h * func ( x0 , y ) ; x0 = x0 + h ; } System . out . println ( " Approximate ▁ solution ▁ at ▁ x ▁ = ▁ " + x + " ▁ is ▁ " + y ) ; } public static void main ( String args [ ] ) throws IOException { Euler obj = new Euler ( ) ; float x0 = 0 ; float y0 = 1 ; float h = 0.025f ; float x = 0.1f ; obj . euler ( x0 , y0 , h , x ) ; } }


"Eulerian Number | Java program to find Eulerian number A ( n , m ) ; Return eulerian number A ( n , m ) ; driver code"
How can the above be solved in Java?

import java . util . * ; class Eulerian { public static int eulerian ( int n , int m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; System . out . print ( eulerian ( n , m ) ) ; } }


"Eulerian Number | Java program to find Eulerian number A ( n , m ) ; Return euleriannumber A ( n , m ) ; For each row from 1 to n ; For each column from 0 to m ; If i is greater than j ; If j is 0 , then make that state as 1. ; basic recurrence relation . ; driver code"
How can the above be solved in Java?

import java . util . * ; class Eulerian { public static int eulerian ( int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i > j ) { if ( j == 0 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = ( ( i - j ) * dp [ i - 1 ] [ j - 1 ] ) + ( ( j + 1 ) * dp [ i - 1 ] [ j ] ) ; } } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; System . out . print ( eulerian ( n , m ) ) ; } }


"Eulerian Path in undirected graph | Efficient Java program to find out Eulerian path ; Function to find out the path It takes the adjacency matrix representation of the graph as input ; Find out number of edges each vertex has ; Find out how many vertex has odd number edges ; If number of vertex with odd number of edges is greater than two return " No ▁ Solution " . ; If there is a path find the path Initialize empty stack and path take the starting current as discussed ; Loop will run until there is element in the stack or current edge has some neighbour . ; If current node has not any neighbour add it to path and pop stack set new current to the popped element ; If the current vertex has at least one neighbour add the current vertex to stack , remove the edge between them and set the current to its neighbour . ; print the path ; Driver Code ; Test case 1 ; Test case 2 ; Test case 3"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findpath ( int [ ] [ ] graph , int n ) { Vector < Integer > numofadj = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) numofadj . add ( accumulate ( graph [ i ] , 0 ) ) ; int startPoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj . elementAt ( i ) % 2 == 1 ) { numofodd ++ ; startPoint = i ; } } if ( numofodd > 2 ) { System . out . println ( " No ▁ Solution " ) ; return ; } Stack < Integer > stack = new Stack < > ( ) ; Vector < Integer > path = new Vector < > ( ) ; int cur = startPoint ; while ( ! stack . isEmpty ( ) || accumulate ( graph [ cur ] , 0 ) != 0 ) { if ( accumulate ( graph [ cur ] , 0 ) == 0 ) { path . add ( cur ) ; cur = stack . pop ( ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( graph [ cur ] [ i ] == 1 ) { stack . add ( cur ) ; graph [ cur ] [ i ] = 0 ; graph [ i ] [ cur ] = 0 ; cur = i ; break ; } } } } for ( int ele : path ) System . out . print ( ele + " ▁ - > ▁ " ) ; System . out . println ( cur ) ; } static int accumulate ( int [ ] arr , int sum ) { for ( int i : arr ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int [ ] [ ] graph1 = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } } ; int n = graph1 . length ; findpath ( graph1 , n ) ; int [ ] [ ] graph2 = { { 0 , 1 , 0 , 1 , 1 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 } } ; n = graph2 . length ; findpath ( graph2 , n ) ; int [ ] [ ] graph3 = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 1 , 0 } } ; n = graph3 . length ; findpath ( graph3 , n ) ; } }


"Evaluate a boolean expression represented as string | Java program to evaluate value of an expression . ; Evaluates boolean expression and returns the result ; Traverse all operands by jumping a character after every iteration . ; If operator next to current operand is AND . ; If operator next to current operand is OR . ; If operator next to current operand is XOR ( Assuming a valid input ) ; Driver code"
How can the above be solved in Java?

public class Evaluate_BoolExp { static int evaluateBoolExpr ( StringBuffer s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s . charAt ( i + 1 ) == ' A ' ) { if ( s . charAt ( i + 2 ) == '0' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } else if ( ( i + 1 ) < n && s . charAt ( i + 1 ) == ' B ' ) { if ( s . charAt ( i + 2 ) == '1' || s . charAt ( i ) == '1' ) s . setCharAt ( i + 2 , '1' ) ; else s . setCharAt ( i + 2 , '0' ) ; } else { if ( s . charAt ( i + 2 ) == s . charAt ( i ) ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } } } return s . charAt ( n - 1 ) - '0' ; } public static void main ( String [ ] args ) { String s = "1C1B1B0A0" ; StringBuffer sb = new StringBuffer ( s ) ; System . out . println ( evaluateBoolExpr ( sb ) ) ; } }


"Evaluate an array expression with numbers , + and | Java program to find sum of given array of string type in integer form ; Function to find the sum of given array ; if string is empty ; parseInt function to convert string into integer ; parseInt function to convert string into integer ; Find operator ; If operator is equal to ' + ' , add value in sum variable else subtract ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int calculateSum ( String arr [ ] , int n ) { if ( n == 0 ) return 0 ; String s = arr [ 0 ] ; int value = Integer . parseInt ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; value = Integer . parseInt ( s ) ; char operation = arr [ i - 1 ] . charAt ( 0 ) ; if ( operation == ' + ' ) sum += value ; else sum -= value ; } return sum ; } public static void main ( String [ ] args ) { String arr [ ] = { "3" , " + " , "4" , " - " , "7" , " + " , "13" } ; int n = arr . length ; System . out . println ( calculateSum ( arr , n ) ) ; } }


"Evaluate the expression ( N1 * ( N | Java program to implement the above approach ; Function to find the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; factorial [ i ] : Stores factorial of i ; Base Case for factorial ; Precompute the factorial ; dp [ N ] : Stores the value of the expression ( N ^ 1 * ( N 1 ) ^ 2 * ... * 1 ^ N ) % ( 109 + 7 ) . ; Update dp [ i ] ; Return the answer . ; Driver code ; Function call"
How can the above be solved in Java?

class GFG { static int mod = 1000000007 ; static int ValOfTheExpression ( int n ) { int [ ] factorial = new int [ n + 1 ] ; factorial [ 0 ] = factorial [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { factorial [ i ] = ( ( factorial [ i - 1 ] % mod ) * ( i % mod ) ) % mod ; } int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( ( dp [ i - 1 ] % mod ) * ( factorial [ i ] % mod ) ) % mod ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( ValOfTheExpression ( n ) ) ; } }


"Evaluation of Risk in Investments | Java code for above approach ; Vector stores the observation in pairs of format ( xi , fi ) , where xi = value of observation ; Function to calculate the summation of fi * xi ; Function to calculate summation fi ; Function to calculate the mean of the set of observations v ; Function to calculate the std deviation of set of observations v ; Get sum of frequencies ; Get the mean of the set of observations ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static class pair { float first , second ; public pair ( float first , float second ) { this . first = first ; this . second = second ; } } static Vector < pair > Vector ; static float sigma_fx ( pair [ ] a ) { float sum = 0 ; for ( pair i : a ) { sum += i . first * i . second ; } return sum ; } static float sigma_f ( pair [ ] a ) { float sum = 0.0f ; for ( pair i : a ) { sum += i . second ; } return sum ; } static float calculate_mean ( pair [ ] a ) { return sigma_fx ( a ) / sigma_f ( a ) ; } static float calculate_std ( pair [ ] a ) { float f = sigma_f ( a ) ; float mean = sigma_fx ( a ) / f ; float sum = 0 ; for ( pair i : a ) { sum += ( i . first - mean ) * ( i . first - mean ) * i . second ; } return ( float ) Math . sqrt ( sum / f ) ; } public static void main ( String [ ] args ) { pair [ ] A = { new pair ( 0f , 0.1f ) , new pair ( 100f , 0.1f ) , new pair ( 200f , 0.2f ) , new pair ( 333f , 0.3f ) , new pair ( 400f , 0.3f ) } ; pair [ ] B = { new pair ( 100f , 0.1f ) , new pair ( 200f , 0.5f ) , new pair ( 700f , 0.4f ) } ; float avg_A = calculate_mean ( A ) ; float avg_B = calculate_mean ( B ) ; float std_A = calculate_std ( A ) ; float std_B = calculate_std ( B ) ; System . out . print ( " For ▁ Investment ▁ A " + "
"); System . out . print ( " Average : ▁ " + avg_A + "
"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_A + "
"); System . out . print ( " Normalised ▁ Std : ▁ " + std_A / avg_A + "
"); System . out . print ( " For ▁ Investment ▁ B " + "
"); System . out . print ( " Average : ▁ " + avg_B + "
"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_B + "
"); System . out . print ( " Normalised ▁ Std : ▁ " + std_B / avg_B + "
"); if ( ( std_B / avg_B ) < ( std_A / avg_A ) ) System . out . print ( "Investment B is less risky
"); else System . out . print ( "Investment A is less risky
"); } }


"Even Fibonacci Numbers Sum | Find the sum of all the even - valued terms in the Fibonacci sequence which do not exceed given limit . ; Returns sum of even Fibonacci numbers which are less than or equal to given limit . ; Initialize first two even prime numbers and their sum ; calculating sum of even Fibonacci value ; get next even value of Fibonacci sequence ; If we go beyond limit , we break loop ; Move to next even number and update sum ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int evenFibSum ( int limit ) { if ( limit < 2 ) return 0 ; long ef1 = 0 , ef2 = 2 ; long sum = ef1 + ef2 ; while ( ef2 <= limit ) { long ef3 = 4 * ef2 + ef1 ; if ( ef3 > limit ) break ; ef1 = ef2 ; ef2 = ef3 ; sum += ef2 ; } return ( int ) sum ; } public static void main ( String [ ] args ) { int limit = 400 ; System . out . println ( evenFibSum ( limit ) ) ; } }


"Even Perfect Number | Java program for the above approach ; Function to check for perfect number ; Find a number close to 2 ^ q - 1 ; Calculate q - 1 ; Condition of perfect number ; Check whether q is prime or not ; Check whether 2 ^ q - 1 is a prime number or not ; Function to check for prime number ; Check whether it is equal to 2 or 3 ; Check if it can be divided by 2 and 3 then it is not prime number ; Check whether the given number be divide by other prime numbers ; Driver code"
How can the above be solved in Java?

class GFG { private static void check ( long num ) { long root = ( long ) Math . sqrt ( num ) ; long pow = ( long ) ( Math . log ( root ) / Math . log ( 2 ) ) ; if ( num == ( long ) ( Math . pow ( 2 , pow ) * ( Math . pow ( 2 , pow + 1 ) - 1 ) ) ) { if ( isPrime ( pow + 1 ) ) { if ( isPrime ( ( long ) Math . pow ( 2 , pow + 1 ) - 1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } public static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } public static void main ( String args [ ] ) { long num = 6 ; check ( num ) ; } }


"Even | Java program to find max ( X , Y ) / min ( X , Y ) after P turns ; Driver code ; 1 st test case ; 2 nd test case"
How can the above be solved in Java?

import java . util . * ; class Even_odd { public static int findValue ( int X , int Y , int P ) { if ( P % 2 == 0 ) return ( Math . max ( X , Y ) / Math . min ( X , Y ) ) ; else return ( Math . max ( 2 * X , Y ) / Math . min ( 2 * X , Y ) ) ; } public static void main ( String [ ] args ) { int X = 1 , Y = 2 , P = 1 ; System . out . println ( findValue ( X , Y , P ) ) ; X = 3 ; Y = 7 ; P = 2 ; System . out . print ( findValue ( X , Y , P ) ) ; } }


"Evil Number | Java program to check if a number is Evil number or Odious Number ; returns number of 1 s from the binary number ; counting 1 s ; Check if number is evil or not ; converting n to binary form ; calculating remainder ; storing the remainders in binary form as a number ; Calling the count_one function to count and return number of 1 s in bin ; Driver Code"
How can the above be solved in Java?

class GFG { static int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } static int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( Math . pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int i , check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) System . out . println ( num + " ▁ is ▁ Evil ▁ Number " ) ; else System . out . println ( num + " ▁ is ▁ Odious ▁ Number " ) ; } }


"Examples of Big | Java program to illustrate time complexity for single for - loop ; Driver Code ; This loop runs for N time ; This loop runs for M time"
How can the above be solved in Java?

class GFG { public static void main ( String [ ] args ) { int a = 0 , b = 0 ; int N = 4 , M = 4 ; for ( int i = 0 ; i < N ; i ++ ) { a = a + 10 ; } for ( int i = 0 ; i < M ; i ++ ) { b = b + 40 ; } System . out . print ( a + " ▁ " + b ) ; } }


"Expand the string according to the given conditions | Java implementation of the approach ; Function to expand and print the given string ; Subtract '0' to convert char to int ; Characters within brackets ; Expanding ; Reset the variables ; Driver code"
How can the above be solved in Java?

public class GFG { static void expandString ( String strin ) { String temp = " " ; int j ; for ( int i = 0 ; i < strin . length ( ) ; i ++ ) { if ( strin . charAt ( i ) >= 0 ) { int num = strin . charAt ( i ) - '0' ; if ( strin . charAt ( i + 1 ) == ' ( ' ) { for ( j = i + 1 ; strin . charAt ( j ) != ' ) ' ; j ++ ) { if ( ( strin . charAt ( j ) >= ' a ' && strin . charAt ( j ) <= ' z ' ) || ( strin . charAt ( j ) >= ' A ' && strin . charAt ( j ) <= ' Z ' ) ) { temp += strin . charAt ( j ) ; } } for ( int k = 1 ; k <= num ; k ++ ) { System . out . print ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ( ) ) { i = j ; } } } } } public static void main ( String args [ ] ) { String strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; } }


"Expectation or expected value of an array | Java code to calculate expected value of an array ; Function to calculate expectation ; Variable prb is for probability of each element which is same for each element ; calculating expectation overall ; returning expectation as sum ; Driver program ; Function for calculating expectation ; Display expectation of given array"
How can the above be solved in Java?

import java . io . * ; class GFG { static float calc_Expectation ( float a [ ] , float n ) { float prb = ( 1 / n ) ; float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } public static void main ( String args [ ] ) { float expect , n = 6f ; float a [ ] = { 1f , 2f , 3f , 4f , 5f , 6f } ; expect = calc_Expectation ( a , n ) ; System . out . println ( " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " + expect ) ; } }


"Expected number of moves to reach the end of a board | Dynamic programming | Java implementation of the approach ; To store the states of dp ; To determine whether a state has been solved before ; Function to return the count ; Base cases ; If a state has been solved before it won 't be evaluated again ; Recurrence relation ; Driver code"
How can the above be solved in Java?

class GFG { static int maxSize = 50 ; static double dp [ ] = new double [ maxSize ] ; static int v [ ] = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( expectedSteps ( n - 1 ) ) ; } }


"Expected number of moves to reach the end of a board | Matrix Exponentiation | Java implementation of the approach ; Function to multiply two 7 * 7 matrix ; Function to perform matrix exponentiation ; 7 * 7 identity matrix ; Loop to find the power ; Function to return the required count ; Base cases ; Multiplier matrix ; Finding the required multiplier i . e mul ^ ( X - 6 ) ; Final answer ; Driver code"
How can the above be solved in Java?


"Exponential Search | Java program to find an element x in a sorted array using Exponential search . ; Returns position of first occurrence of x in array ; If x is present at firt location itself ; Find range for binary search by repeated doubling ; Call binary search for the found range . ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static int exponentialSearch ( int arr [ ] , int n , int x ) { if ( arr [ 0 ] == x ) return 0 ; int i = 1 ; while ( i < n && arr [ i ] <= x ) i = i * 2 ; return Arrays . binarySearch ( arr , i / 2 , Math . min ( i , n - 1 ) , x ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int result = exponentialSearch ( arr , arr . length , x ) ; System . out . println ( ( result < 0 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }


"Exponential Squaring ( Fast Modulo Multiplication ) | Java program to compute exponential value using ( 2 ^ k ) - ary method . ; static long N = 1000000007L ; prime modulo value ; for cases where exponent is not an even value ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class exp_sq { static long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; } } public static void main ( String [ ] args ) { long base = 5 ; long exp = 100000 ; long modulo = exponentiation ( base , exp ) ; System . out . println ( modulo ) ; }


"Express a number as sum of consecutive numbers | Java program to print a consecutive sequence to express N if possible . ; Print consecutive numbers from last to first ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printConsecutive ( int last , int first ) { System . out . print ( first ++ ) ; for ( int x = first ; x <= last ; x ++ ) System . out . print ( " ▁ + ▁ " + x ) ; } static void findConsecutive ( int N ) { for ( int last = 1 ; last < N ; last ++ ) { for ( int first = 0 ; first < last ; first ++ ) { if ( 2 * N == ( last - first ) * ( last + first + 1 ) ) { System . out . print ( N + " ▁ = ▁ " ) ; printConsecutive ( last , first + 1 ) ; return ; } } } System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 12 ; findConsecutive ( n ) ; } }


"Express an odd number as sum of prime numbers | Java program to express N as sum of at - most three prime numbers . ; Function to check if a number is prime or not . ; Prints at most three prime numbers whose sum is n . ; if ( isPrime ( n ) ) CASE - I ; else if ( isPrime ( n - 2 ) ) CASE - II ; else CASE - III ; driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static boolean isPrime ( int x ) { if ( x == 0 x == 1 ) return false ; for ( int i = 2 ; i * i <= x ; ++ i ) if ( x % i == 0 ) return false ; return true ; } public static void findPrimes ( int n ) { System . out . print ( n ) ; System . out . print ( 2 + " ▁ " + ( n - 2 ) ) ; { System . out . print ( 3 + " ▁ " ) ; n = n - 3 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i ) && isPrime ( n - i ) ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; break ; } } } } public static void main ( String [ ] args ) { int n = 27 ; findPrimes ( n ) ; } }


"Expressing a fraction as a natural number under modulo ' m ' | Java implementation of the approach ; Function to return the GCD of given numbers ; Recursive function to return ( x ^ n ) % m ; Function to return the fraction modulo mod ; ( b ^ m - 2 ) % m ; Final answer ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static long m = 1000000007 ; static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long modexp ( long x , long n ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 ) % m ) ; } } static long getFractionModulo ( long a , long b ) { long c = gcd ( a , b ) ; a = a / c ; b = b / c ; long d = modexp ( b , m - 2 ) ; long ans = ( ( a % m ) * ( d % m ) ) % m ; return ans ; } public static void main ( String [ ] args ) { long a = 2 , b = 6 ; System . out . println ( getFractionModulo ( a , b ) ) ; } }


"Expressing a number as sum of consecutive | Set 2 ( Using odd factors ) | Java program to count number of ways to express N as sum of consecutive numbers . ; returns the number of odd factors ; If i is an odd factor and n is a perfect square ; If n is not perfect square ; Driver Code ; N as sum of consecutive numbers"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countOddFactors ( long n ) { int odd_factors = 0 ; for ( int i = 1 ; 1 * i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( 1 * i * i == n ) { if ( ( i & 1 ) == 1 ) odd_factors ++ ; } else { if ( ( i & 1 ) == 1 ) odd_factors ++ ; int factor = ( int ) n / i ; if ( ( factor & 1 ) == 1 ) odd_factors ++ ; } } } return odd_factors - 1 ; } public static void main ( String args [ ] ) { long N = 15 ; System . out . println ( countOddFactors ( N ) ) ; N = 10 ; System . out . println ( countOddFactors ( N ) ) ; } }


"Expressing factorial n as sum of consecutive numbers | Java program to count number of ways we can express a factorial as sum of consecutive numbers ; sieve of Eratosthenes to compute the prime numbers ; Store all prime numbers ; function to calculate the largest power of a prime in a number ; Modular multiplication to avoid the overflow of multiplication Please see below for details https : www . geeksforgeeks . org / how - to - avoid - overflow - in - modular - multiplication / ; Returns count of ways to express n ! as sum of consecutives . ; We skip 2 ( First prime ) as we need to consider only odd primes ; compute the largest power of prime ; if the power of current prime number is zero in N ! , power of primes greater than current prime number will also be zero , so break out from the loop ; multiply the result at every step ; subtract 1 to exclude the case of 1 being an odd divisor ; Driver function"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MAX = 50002 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; public static void sieve ( ) { boolean isPrime [ ] = new boolean [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] == true ) primes . add ( p ) ; } public static int power ( int x , int y ) { int count = 0 ; int z = y ; while ( x >= z ) { count += ( x / z ) ; z *= y ; } return count ; } public static int modMult ( int a , int b , int mod ) { int res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static int countWays ( int n , int m ) { int ans = 1 ; for ( int i = 1 ; i < primes . size ( ) ; i ++ ) { int powers = power ( n , primes . get ( i ) ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } public static void main ( String [ ] args ) { sieve ( ) ; int n = 4 , m = 7 ; System . out . println ( countWays ( n , m ) ) ; } }


"Expression Evaluation | A Java program to evaluate a given expression where tokens are separated by space . ; Returns true if ' op2' has higher or same precedence as ' op1' , otherwise returns false . ; A utility method to apply an operator ' op ' on operands ' a ' and ' b ' . Return the result . ; Function that returns value of expression after evaluation . ; Stack for numbers : ' values ' ; Stack for Operators : ' ops ' ; Current token is a whitespace , skip it ; Current token is an opening brace , push it to ' ops ' ; Current token is a number , push it to stack for numbers ; There may be more than one digits in number ; right now the i points to the character next to the digit , since the for loop also increases the i , we would skip one token position ; we need to decrease the value of i by 1 to correct the offset . ; Closing brace encountered , solve entire brace ; Current token is an operator . ; While top of ' ops ' has same or greater precedence to current token , which is an operator . Apply operator on top of ' ops ' to top two elements in values stack ; Push current token to ' ops ' . ; Entire expression has been parsed at this point , apply remaining ops to remaining values ; Top of ' values ' contains result , return it ; Driver method to test above methods"
How can the above be solved in Java?

import java . util . Stack ; public class EvaluateString { public static boolean hasPrecedence ( char op1 , char op2 ) { if ( op2 == ' ( ' op2 == ' ) ' ) return false ; if ( ( op1 == ' * ' op1 == ' / ' ) && ( op2 == ' + ' op2 == ' - ' ) ) return false ; else return true ; } public static int applyOp ( char op , int b , int a ) { switch ( op ) { case ' + ' : return a + b ; case ' - ' : return a - b ; case ' * ' : return a * b ; case ' / ' : if ( b == 0 ) throw new UnsupportedOperationException ( " Cannot ▁ divide ▁ by ▁ zero " ) ; return a / b ; } return 0 ; } public static int evaluate ( String expression ) { char [ ] tokens = expression . toCharArray ( ) ; Stack < Integer > values = new Stack < Integer > ( ) ; Stack < Character > ops = new Stack < Character > ( ) ; for ( int i = 0 ; i < tokens . length ; i ++ ) { if ( tokens [ i ] == ' ▁ ' ) continue ; else if ( tokens [ i ] == ' ( ' ) ops . push ( tokens [ i ] ) ; if ( tokens [ i ] >= '0' && tokens [ i ] <= '9' ) { StringBuffer sbuf = new StringBuffer ( ) ; while ( i < tokens . length && tokens [ i ] >= '0' && tokens [ i ] <= '9' ) sbuf . append ( tokens [ i ++ ] ) ; values . push ( Integer . parseInt ( sbuf . toString ( ) ) ) ; i -- ; } else if ( tokens [ i ] == ' ) ' ) { while ( ops . peek ( ) != ' ( ' ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; ops . pop ( ) ; } else if ( tokens [ i ] == ' + ' tokens [ i ] == ' - ' tokens [ i ] == ' * ' tokens [ i ] == ' / ' ) { while ( ! ops . empty ( ) && hasPrecedence ( tokens [ i ] , ops . peek ( ) ) ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; ops . push ( tokens [ i ] ) ; } } while ( ! ops . empty ( ) ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; return values . pop ( ) ; } public static void main ( String [ ] args ) { System . out . println ( EvaluateString . evaluate ( "10 ▁ + ▁ 2 ▁ * ▁ 6" ) ) ; System . out . println ( EvaluateString . evaluate ( "100 ▁ * ▁ 2 ▁ + ▁ 12" ) ) ; System . out ....


"Expression Tree | Java program to construct an expression tree ; Java program for expression tree ; A utility function to check if ' c ' is an operator ; Utility function to do inorder traversal ; Returns root of constructed tree for given postfix expression ; Traverse through every character of input expression ; If operand , simply push into stack ; operator ; Pop two top nodes Store top Remove top ; make them children ; System . out . println ( t1 + " " + t2 ) ; Add this subexpression to stack ; only element will be root of expression tree ; Driver program to test above"
How can the above be solved in Java?

import java . util . Stack ; class Node { char value ; Node left , right ; Node ( char item ) { value = item ; left = right = null ; } } class ExpressionTree { boolean isOperator ( char c ) { if ( c == ' + ' c == ' - ' c == ' * ' c == ' / ' c == ' ^ ' ) { return true ; } return false ; } void inorder ( Node t ) { if ( t != null ) { inorder ( t . left ) ; System . out . print ( t . value + " ▁ " ) ; inorder ( t . right ) ; } } Node constructTree ( char postfix [ ] ) { Stack < Node > st = new Stack < Node > ( ) ; Node t , t1 , t2 ; for ( int i = 0 ; i < postfix . length ; i ++ ) { if ( ! isOperator ( postfix [ i ] ) ) { t = new Node ( postfix [ i ] ) ; st . push ( t ) ; } else { t = new Node ( postfix [ i ] ) ; t1 = st . pop ( ) ; t2 = st . pop ( ) ; t . right = t1 ; t . left = t2 ; st . push ( t ) ; } } t = st . peek ( ) ; st . pop ( ) ; return t ; } public static void main ( String args [ ] ) { ExpressionTree et = new ExpressionTree ( ) ; String postfix = " ab + ef * g * - " ; char [ ] charArray = postfix . toCharArray ( ) ; Node root = et . constructTree ( charArray ) ; System . out . println ( " infix ▁ expression ▁ is " ) ; et . inorder ( root ) ; } }


"Expression contains redundant bracket or not | Java Program to check whether valid expression is redundant or not ; Function to check redundant brackets in a balanced expression ; create a stack of characters ; Iterate through the given expression ; if current character is close parenthesis ' ) ' ; If immediate pop have open parenthesis ' ( ' duplicate brackets found ; Check for operators in expression ; Fetch top element of stack ; If operators not found ; push open parenthesis ' ( ' , ; operators and operands to stack ; Function to check redundant brackets ; Driver code"
How can the above be solved in Java?

import java . util . Stack ; public class GFG { static boolean checkRedundancy ( String s ) { Stack < Character > st = new Stack < > ( ) ; char [ ] str = s . toCharArray ( ) ; for ( char ch : str ) { if ( ch == ' ) ' ) { char top = st . peek ( ) ; st . pop ( ) ; boolean flag = true ; while ( top != ' ( ' ) { if ( top == ' + ' top == ' - ' top == ' * ' top == ' / ' ) { flag = false ; } top = st . peek ( ) ; st . pop ( ) ; } if ( flag == true ) { return true ; } } else { st . push ( ch ) ; } } return false ; } static void findRedundant ( String str ) { boolean ans = checkRedundancy ( str ) ; if ( ans == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String str = " ( ( a + b ) ) " ; findRedundant ( str ) ; str = " ( a + ( b ) / c ) " ; findRedundant ( str ) ; str = " ( a + b * ( c - d ) ) " ; findRedundant ( str ) ; } }


"Extended Knapsack Problem | Java code for the extended Knapsack Approach ; To store the dp values ; for each element given ; For each possible weight value ; For each case where the total elements are less than the constraint ; To ensure that we dont go out of the array ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } public static void main ( String [ ] args ) { int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; System . out . println ( maxProfit ( profit , weight , n , max_weight , max_element ) ) ; } }


"Extended Midy 's theorem | Java program to demonstrate extended Midy 's theorem ; Returns repeating sequence of a fraction . If repeating sequence doesn 't exits, then returns -1 ; Create a map to store already seen remainders remainder is used as key and its position in result is stored as value . Note that we need position for cases like 1 / 6. In this case , the recurring sequence doesn 't start from first  remainder.  ; Find first remainder ; Keep finding remainder until either remainder becomes 0 or repeats ; Store this remainder ; Multiply remainder with 10 ; Append rem / denr to result ; Update remainder ; Checks whether a number is prime or not ; If all conditions are met , it proves Extended Midy 's theorem ; Dividing repeated part into m parts ; Computing sum of parts . ; Checking for Extended Midy ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String fractionToDecimal ( int numerator , int denominator ) { String res = " " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ! mp . containsKey ( rem ) ) { mp . put ( rem , res . length ( ) ) ; rem = rem * 10 ; int res_part = rem / denominator ; res += res_part + " " ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . substring ( mp . get ( rem ) ) ; } static boolean isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void ExtendedMidys ( String str , int n , int m ) { if ( ! isPrime ( n ) ) { System . out . print ( " Denominator ▁ is ▁ not ▁ prime , ▁ " + " thus ▁ Extended ▁ Midy ' s ▁ theorem ▁ " + " is ▁ not ▁ applicable " ) ; return ; } int l = str . length ( ) ; int part1 = 0 , part2 = 0 ; if ( l % 2 == 0 && l % m == 0 ) { int [ ] part = new int [ m ] ; int sum = 0 , res = 0 ; for ( int i = 0 ; i < l ; i ++ ) { int var = i / m ; part [ var ] = part [ var ] * 10 + ( str . charAt ( i ) - '0' ) ; } for ( int i = 0 ; i < m ; i ++ ) { sum = sum + part [ i ] ; System . out . print ( part [ i ] + " ▁ " ) ; } System . out . println ( ) ; res = ( int ) Math . pow ( 10 , m ) - 1 ; if ( sum % res == 0 ) System . out . print ( " Extended ▁ Midy ' s ▁ " + " theorem ▁ holds ! " ) ; else System . out . print ( " Extended ▁ Midy ' s ▁ " + " theorem ▁ doesn ' t ▁ hold ! " ) ; } else if ( l % 2 != 0 ) { System . out . print ( " The ▁ repeating ▁ decimal ▁ is ▁ of ▁ " + " odd ▁ length ▁ thus ▁ Extended ▁ Midy ' s ▁ " + " theorem ▁ is ▁ not ▁ applicable " ) ; } else if ( l % m != 0 ) { System . out . print ( " The ▁ repeating ▁ decimal ▁ can ▁ " + " not ▁ be ▁ divided ▁ into ▁ m ▁ digits " ) ; } } public static void main ( String [ ] args ) { int numr = 1 , denr = 17 , m = 4 ; String res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) System . out . print ( " The ▁ fraction ▁ does ▁ not ▁ " + " have ▁ repeating ▁ decimal...


"Exterior angle of a cyclic quadrilateral when the opposite interior angle is given | Java program to find the exterior angle of a cyclic quadrilateral when the opposite interior angle is given ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void angleextcycquad ( int z ) { System . out . print ( " The ▁ exterior ▁ angle ▁ of ▁ the " + " ▁ cyclic ▁ quadrilateral ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; angleextcycquad ( z ) ; } }


"Extract Leaves of a Binary Tree in a Doubly Linked List | A binay tree node ; Binary Tree class ; The main function that links the list list to be traversed ; Utility function for printing tree in In - Order . ; Prints the DLL in both forward and reverse directions . ; Driver program to test above functions"
How can the above be solved in Java?

class Node { int data ; Node left , right ; Node ( int item ) { data = item ; right = left = null ; } } public class BinaryTree { Node root ; Node head ; Node prev ; public Node extractLeafList ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) { if ( head == null ) { head = root ; prev = root ; } else { prev . right = root ; root . left = prev ; prev = root ; } return null ; } root . left = extractLeafList ( root . left ) ; root . right = extractLeafList ( root . right ) ; return root ; } void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inorder ( node . right ) ; } public void printDLL ( Node head ) { Node last = null ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; last = head ; head = head . right ; } } 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 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . root . left . left . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 9 ) ; tree . root . right . right . right = new Node ( 10 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; tree . extractLeafList ( tree . root ) ; System . out . println ( " " ) ; System . out . println ( " Extracted ▁ double ▁ link ▁ list ▁ is ▁ : ▁ " ) ; tree . printDLL ( tree . head ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ modified ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; } }


"Extract maximum numeric value from a given string | Set 1 ( General approach ) | Java program for above implementation ; Utility method to find maximum string ; If both having equal lengths ; Reach first unmatched character / value ; Return string with maximum value ; If different lengths return string with maximum length ; Method to extract the maximum value ; Start traversing the string ; Ignore leading zeroes ; Store numeric value into a string ; Update maximum string ; To handle the case if there is only 0 numeric value ; Return maximum string ; Driver method"
How can the above be solved in Java?

class GFG { static String maximumNum ( String curr_num , String res ) { int len1 = curr_num . length ( ) ; int len2 = res . length ( ) ; if ( len1 == len2 ) { int i = 0 ; while ( curr_num . charAt ( i ) == res . charAt ( i ) ) i ++ ; if ( curr_num . charAt ( i ) < res . charAt ( i ) ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } static String extractMaximum ( String str ) { int n = str . length ( ) ; String curr_num = " " ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n && str . charAt ( i ) == '0' ) i ++ ; while ( i < n && Character . isDigit ( str . charAt ( i ) ) ) { curr_num = curr_num + str . charAt ( i ) ; i ++ ; } if ( i == n ) break ; if ( curr_num . length ( ) > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . length ( ) == 0 && res . length ( ) == 0 ) res = res + '0' ; return maximumNum ( curr_num , res ) ; } public static void main ( String [ ] args ) { String str = "100klh564abc365bg " ; System . out . println ( extractMaximum ( str ) ) ; } }


"Extract substrings between any pair of delimiters | Java program to implement the above approach ; Function to print Strings present between any pair of delimeters ; Stores the indices of ; If opening delimeter is encountered ; If closing delimeter is encountered ; Extract the position of opening delimeter ; Length of subString ; Extract the subString ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void printSubsInDelimeters ( String str ) { Stack < Integer > dels = new Stack < Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' [ ' ) { dels . add ( i ) ; } else if ( str . charAt ( i ) == ' ] ' && ! dels . isEmpty ( ) ) { int pos = dels . peek ( ) ; dels . pop ( ) ; int len = i - 1 - pos ; String ans = str . substring ( pos + 1 , pos + 1 + len ) ; System . out . print ( ans + "
"); } } } public static void main ( String [ ] args ) { String str = " [ This ▁ is ▁ first ] ▁ ignored ▁ text ▁ [ This ▁ is ▁ second ] " ; printSubsInDelimeters ( str ) ; } }


"FIFO ( First | Java program to demonstrate working of FIFO using Queue interface in Java ; Driver code ; Adds elements { 0 , 1 , 2 , 3 , 4 } to queue ; Display contents of the queue . ; To remove the head of queue . In this the oldest element '0' will be removed ; To view the head of queue ; Rest all methods of collection interface , Like size and contains can be used with this implementation ."
How can the above be solved in Java?

import java . util . LinkedList ; import java . util . Queue ; public class QueueExample { public static void main ( String [ ] args ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) q . add ( i ) ; System . out . println ( " Elements ▁ of ▁ queue - " + q ) ; int removedele = q . remove ( ) ; System . out . println ( " removed ▁ element - " + removedele ) ; System . out . println ( q ) ; int head = q . peek ( ) ; System . out . println ( " head ▁ of ▁ queue - " + head ) ; int size = q . size ( ) ; System . out . println ( " Size ▁ of ▁ queue - " + size ) ; } }


"Factor Tree of a given Number | Java program to conFactor Tree for a given number ; Tree node ; Utility function to create a new tree Node ; Constructs factor tree for given value and stores root of tree at given reference . ; the number is factorized ; If we found a factor , we conleft and right subtrees and return . Since we traverse factors starting from smaller to greater , left child will always have smaller factor ; Iterative method to find the height of Binary Tree ; Base Case ; Print front of queue and remove it from queue ; Driver program ; int val = 48 ; sample value"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { Node left , right ; int key ; } ; static Node root ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node createFactorTree ( Node node_ref , int v ) { ( node_ref ) = newNode ( v ) ; for ( int i = 2 ; i < v / 2 ; i ++ ) { if ( v % i != 0 ) continue ; node_ref . left = createFactorTree ( ( ( node_ref ) . left ) , i ) ; node_ref . right = createFactorTree ( ( ( node_ref ) . right ) , v / i ) ; return node_ref ; } return node_ref ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; System . out . print ( node . key + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } public static void main ( String [ ] args ) { root = null ; root = createFactorTree ( root , val ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ " + " constructed ▁ factor ▁ tree " ) ; printLevelOrder ( root ) ; } }


"Factorial of a number without using multiplication | Java program for the above approach ; Function to calculate factorial of the number without using multiplication operator ; variable to store the final factorial ; Outer loop ; Inner loop ; Driver code ; Input ; Function calling"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int factorialWithoutMul ( int N ) { int ans = N ; for ( int i = N - 1 ; i > 0 ; i -- ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ans ; ans = sum ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( factorialWithoutMul ( N ) ) ; } }


"Farey Sequence | Efficient Java program to print Farey Sequence of order n ; Optimized function to print Farey sequence of order n ; We know first two terms are 0 / 1 and 1 / n ; double x , y = 0 ; For next terms to be evaluated ; Using recurrence relation to find the next term ; Print next term ; Update x1 , y1 , x2 and y2 for next iteration ; Driver program"
How can the above be solved in Java?

 class GFG { static void farey ( int n ) { double x1 = 0 , y1 = 1 , x2 = 1 , y2 = n ; System . out . printf ( " % .0f / % . 0f ▁ % .0f / % . 0f " , x1 , y1 , x2 , y2 ) ; while ( y != 1.0 ) { x = Math . floor ( ( y1 + n ) / y2 ) * x2 - x1 ; y = Math . floor ( ( y1 + n ) / y2 ) * y2 - y1 ; System . out . printf ( " ▁ % .0f / % . 0f " , x , y ) ; x1 = x2 ; x2 = x ; y1 = y2 ; y2 = y ; } } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( " Farey ▁ Sequence ▁ of ▁ order ▁ " + n + " is
"); farey ( n ) ; } }


"Farthest cell from a given cell in a Matrix | Java program for the above approach ; Function to find the farthest cell distance from the given cell ; From cell ( N , M ) ; From Cell ( 1 , 1 ) ; From cell ( N , 1 ) ; From cell ( 1 , M ) ; Finding out maximum ; Print the answer ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void farthestCellDistance ( int N , int M , int R , int C ) { int d1 = N + M - R - C ; int d2 = R + C - 2 ; int d3 = N - R + C - 1 ; int d4 = M - C + R - 1 ; int maxDistance = Math . max ( d1 , Math . max ( d2 , Math . max ( d3 , d4 ) ) ) ; System . out . println ( maxDistance ) ; } public static void main ( String [ ] args ) { int N = 15 , M = 12 , R = 1 , C = 6 ; farthestCellDistance ( N , M , R , C ) ; } }


"Farthest index that can be reached from the Kth index of given array by given operations | C ++ program for the above approach ; Function to find the farthest index that can be reached ; Declare a priority queue ; Iterate the array ; If current element is greater than the next element ; Otherwise , store their difference ; Push diff into pq ; If size of pq exceeds Y ; Decrease X by the top element of pq ; Remove top of pq ; If X is exhausted ; Current index is the farthest possible ; Print N - 1 as farthest index ; Driver Code ; Function Call ;"
How can the above be solved in Java?

# include < bits / stdc ++ . h > using namespace std ; void farthestHill ( int arr [ ] , int X , int Y , int N , int K ) { int i , diff ; priority_queue < int > pq ; for ( i = K ; i < N - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i + 1 ] ) continue ; diff = arr [ i + 1 ] - arr [ i ] ; pq . push ( diff ) ; if ( pq . size ( ) > Y ) { X -= pq . top ( ) ; pq . pop ( ) ; } if ( X < 0 ) { cout << i ; return ; } } cout << N - 1 ; } int main ( ) { int arr [ ] = { 4 , 2 , 7 , 6 , 9 , 14 , 12 } ; int X = 5 , Y = 1 ; int K = 0 ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; farthestHill ( arr , X , Y , N , K ) ; return 0 ; } ]


"Fascinating Number | Java program to implement fascinating number ; function to check if number is fascinating or not ; frequency count array using 1 indexing ; obtaining the resultant number using string concatenation ; Traversing the string character by character ; gives integer value of a character digit ; To check if any digit has appeared multiple times ; Traversing through freq array to check if any digit was missing ; Driver code ; Input number ; Not a valid number ; Calling the function to check if input number is fascinating or not"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; public class GFG { public static boolean isFascinating ( int num ) { int [ ] freq = new int [ 10 ] ; String val = " " + num + num * 2 + num * 3 ; for ( int i = 0 ; i < val . length ( ) ; i ++ ) { int digit = val . charAt ( i ) - '0' ; if ( freq [ digit ] > 0 && digit != 0 ) return false ; else freq [ digit ] ++ ; } for ( int i = 1 ; i < freq . length ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } public static void main ( String args [ ] ) { int num = 192 ; if ( num < 100 ) System . out . println ( " No " ) ; else { boolean ans = isFascinating ( num ) ; if ( ans ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } }


"Fast Doubling method to find the Nth Fibonacci number | Java program to find the Nth Fibonacci number using Fast Doubling Method ; Function calculate the N - th fibanacci number using fast doubling method ; Base Condition ; Here a = F ( n ) ; Here b = F ( n + 1 ) ; As F ( 2 n ) = F ( n ) [ 2F ( n + 1 ) F ( n ) ] Here c = F ( 2 n ) ; As F ( 2 n + 1 ) = F ( n ) ^ 2 + F ( n + 1 ) ^ 2 Here d = F ( 2 n + 1 ) ; Check if N is odd or even ; Driver code"
How can the above be solved in Java?

class GFG { static void FastDoubling ( int n , int [ ] res ) { int a , b , c , d ; int MOD = 1000000007 ; if ( n == 0 ) { res [ 0 ] = 0 ; res [ 1 ] = 1 ; return ; } FastDoubling ( ( n / 2 ) , res ) ; a = res [ 0 ] ; b = res [ 1 ] ; c = 2 * b - a ; if ( c < 0 ) c += MOD ; c = ( a * c ) % MOD ; d = ( a * a + b * b ) % MOD ; if ( n % 2 == 0 ) { res [ 0 ] = c ; res [ 1 ] = d ; } else { res [ 0 ] = d ; res [ 1 ] = c + d ; } } public static void main ( String [ ] args ) { int N = 6 ; int res [ ] = new int [ 2 ] ; FastDoubling ( N , res ) ; System . out . print ( res [ 0 ] ) ; } }


"Fermat 's Factorization Method | Java implementation of fermat 's factorization ; This function finds the value of a and b and returns a + b and a - b ; since fermat 's factorization applicable  for odd positive integers only ; check if n is a even number ; if n is a perfect root , then both its square roots are its factors ; Driver Code"
How can the above be solved in Java?

class GFG { static void FermatFactors ( int n ) { if ( n <= 0 ) { System . out . print ( " [ " + n + " ] " ) ; return ; } if ( ( n & 1 ) == 0 ) { System . out . print ( " [ " + n / 2.0 + " , " + 2 + " ] " ) ; return ; } int a = ( int ) Math . ceil ( Math . sqrt ( n ) ) ; if ( a * a == n ) { System . out . print ( " [ " + a + " , " + a + " ] " ) ; return ; } int b ; while ( true ) { int b1 = a * a - n ; b = ( int ) ( Math . sqrt ( b1 ) ) ; if ( b * b == b1 ) break ; else a += 1 ; } System . out . print ( " [ " + ( a - b ) + " , " + ( a + b ) + " ] " ) ; return ; } public static void main ( String [ ] args ) { FermatFactors ( 6557 ) ; } }


"Fermat 's Last Theorem | Java program to verify fermat 's last theorem for a given range and n. ; Check if there exists a triplet such that a ^ n + b ^ n = c ^ n ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void testSomeNumbers ( int limit , int n ) { if ( n < 3 ) return ; for ( int a = 1 ; a <= limit ; a ++ ) for ( int b = a ; b <= limit ; b ++ ) { int pow_sum = ( int ) ( Math . pow ( a , n ) + Math . pow ( b , n ) ) ; double c = Math . pow ( pow_sum , 1.0 / n ) ; int c_pow = ( int ) Math . pow ( ( int ) c , n ) ; if ( c_pow == pow_sum ) { System . out . println ( " Count ▁ example ▁ found " ) ; return ; } } System . out . println ( " No ▁ counter ▁ example ▁ within ▁ given " + " ▁ range ▁ and ▁ data " ) ; } public static void main ( String [ ] args ) { testSomeNumbers ( 12 , 5 ) ; } }


"Fibbinary Numbers ( No consecutive 1 s in binary ) | Java implementation to check whether a number is fibbinary or not ; function to check whether a number is fibbinary or not ; if the number does not contain adjacent ones then ( n & ( n >> 1 ) ) operation results to 0 ; not a fibbinary number ; Driver program to test above"
How can the above be solved in Java?

class GFG { static boolean isFibbinaryNum ( int n ) { if ( ( n & ( n >> 1 ) ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 10 ; if ( isFibbinaryNum ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Fibbinary Numbers ( No consecutive 1 s in binary ) | Java program to check if a number is fibinnary number or not ; function to check if binary representation of an integer has consecutive 1 s ; stores the previous last bit initially as 0 ; if current last bit and previous last bit is 1 ; stores the last bit ; right shift the number ; Driver code to check above function"
How can the above be solved in Java?

class GFG { static boolean checkFibinnary ( int n ) { int prev_last = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 && prev_last != 0 ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; if ( checkFibinnary ( n ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Fibonacci Coding | Java program for Fibonacci Encoding of a positive integer n ; To limit on the largest Fibonacci number to be used ; Array to store fibonacci numbers . fib [ i ] is going to store ( i + 2 ) 'th Fibonacci number ; Stores values in fib and returns index of the largest fibonacci number smaller than n . ; Fib [ 0 ] stores 2 nd Fibonacci No . ; Fib [ 1 ] stores 3 rd Fibonacci No . ; Keep Generating remaining numbers while previously generated number is smaller ; Return index of the largest fibonacci number smaller than or equal to n . Note that the above loop stopped when fib [ i - 1 ] became larger . ; Returns pointer to the char string which corresponds to code for n ; Allocate memory for codeword ; Index of the largest Fibonacci f <= n ; Mark usage of Fibonacci f ( 1 bit ) ; Subtract f from n ; Move to Fibonacci just smaller than f ; Mark all Fibonacci > n as not used ( 0 bit ) , progress backwards ; Additional '1' bit ; Return pointer to codeword ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int N = 30 ; public static int [ ] fib = new int [ N ] ; public static int largestFiboLessOrEqual ( int n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; int i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } return ( i - 2 ) ; } public static String fibonacciEncoding ( int n ) { int index = largestFiboLessOrEqual ( n ) ; char [ ] codeword = new char [ index + 3 ] ; int i = index ; while ( n > 0 ) { codeword [ i ] = '1' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = '0' ; i = i - 1 ; } } codeword [ index + 1 ] = '1' ; codeword [ index + 2 ] = ' \0' ; String string = new String ( codeword ) ; return string ; } public static void main ( String [ ] args ) { int n = 143 ; System . out . println ( " Fibonacci ▁ code ▁ word ▁ for ▁ " + n + " ▁ is ▁ " + fibonacciEncoding ( n ) ) ; } }


"Fibonacci Power |  ; Iterative function to compute modular power ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now ; Helper function that multiplies 2 matrices F and M of size 2 * 2 , and puts the multiplication result back to F [ ] [ ] ; Helper function that calculates F [ ] [ ] raise to the power n and puts the result in F [ ] [ ] Note that this function is designed only for fib ( ) and won 't work as general power function ; Function that returns nth Fibonacci number ; Driver Code"
How can the above be solved in Java?

class GFG { static int mod = 1000000007 ; static long modularexpo ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void multiply ( long F [ ] [ ] , long M [ ] [ ] , long m ) { long x = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long y = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; long z = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long w = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } static void power ( long F [ ] [ ] , long n , long m ) { if ( n == 0 n == 1 ) return ; long M [ ] [ ] = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 , m ) ; multiply ( F , F , m ) ; if ( n % 2 != 0 ) multiply ( F , M , m ) ; } static long fib ( long n , long m ) { long F [ ] [ ] = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n - 1 , m ) ; return F [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { long n = 4 ; long base = fib ( n , mod ) % mod ; long expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; long result = modularexpo ( base , expo , mod ) % mod ; System . out . println ( result ) ; } }


"Fibonacci Word | Java program for nth Fibonacci word ; Returns n - th Fibonacci word ; driver code"
How can the above be solved in Java?

import java . util . * ; class Eulerian { public static String fibWord ( int n ) { String Sn_1 = "0" ; String Sn = "01" ; String tmp ; for ( int i = 2 ; i <= n ; i ++ ) { tmp = Sn ; Sn += Sn_1 ; Sn_1 = tmp ; } return Sn ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . print ( fibWord ( n ) ) ; } }


"Fibonacci modulo p | Java program to find minimal 0 Fibonacci for a prime number p ; Function that returns position of first Fibonacci number whose modulo p is 0 ; add previous two remainders and then take its modulo p . ; Driver program"
How can the above be solved in Java?

import java . io . * ; class FibZero { static int findMinZero ( int p ) { int first = 1 , second = 1 , number = 2 , next = 1 ; while ( next > 0 ) { next = ( first + second ) % p ; first = second ; second = next ; number ++ ; } return number ; } public static void main ( String [ ] args ) { int p = 7 ; System . out . println ( " Minimal ▁ zero ▁ is ▁ " + findMinZero ( p ) ) ; } }


"Fibonacci problem ( Value of Fib ( N ) * Fib ( N ) | Java implementation of the approach ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int getResult ( int n ) { if ( ( n & 1 ) > 0 ) return 1 ; return - 1 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( getResult ( n ) ) ; } }


"Fibonomial coefficient and Fibonomial triangle | Java Program to print Fibonomial Triangle of height n . ; Function to produce Fibonacci Series . ; 0 th and 1 st number of the series are 0 and 1 ; Add the previous 2 numbers in the series and store it ; Function to produce fibonomial coefficient ; Function to print Fibonomial Triangle . ; Finding the fibonacci series . ; to store triangle value . ; initialising the 0 th element of each row and diagonal element equal to 0. ; for each row . ; for each column . ; finding each element using recurrence relation . ; printing the Fibonomial Triangle . ; Driver code"
How can the above be solved in Java?

class GFG { static final int N = 6 ; static void fib ( int f [ ] , int n ) { int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fibcoef ( int fc [ ] [ ] , int f [ ] , int n ) { for ( int i = 0 ; i <= n ; i ++ ) fc [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int k = j ; while ( k > 0 ) { k -- ; fc [ i ] [ j ] *= f [ k ] ; } k = 1 ; while ( ( j + 1 ) != k ) fc [ i ] [ j ] /= f [ k ++ ] ; } } } static void printFibonomialTriangle ( int n ) { int f [ ] = new int [ N + 1 ] ; fib ( f , n ) ; int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) dp [ i ] [ j ] = f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 6 ; printFibonomialTriangle ( n ) ; } }


"Fill array with 1 's using minimum iterations of filling neighbors | Java program to find number of iterations to fill with all 1 s ; Returns count of iterations to fill arr [ ] with 1 s . ; Start traversing the array ; Traverse until a 0 is found ; Count contiguous 0 s ; Condition for Case 3 ; Condition to check if Case 1 satisfies : ; If count_zero is even ; If count_zero is odd ; Reset count_zero ; Case 2 ; Update res ; Driver method"
How can the above be solved in Java?

class Test { static int countIterations ( int arr [ ] , int n ) { boolean oneFound = false ; int res = 0 ; for ( int i = 0 ; i < n ; ) { if ( arr [ i ] == 1 ) oneFound = true ; while ( i < n && arr [ i ] == 1 ) i ++ ; int count_zero = 0 ; while ( i < n && arr [ i ] == 0 ) { count_zero ++ ; i ++ ; } if ( oneFound == false && i == n ) return - 1 ; int curr_count ; if ( i < n && oneFound == true ) { if ( ( count_zero & 1 ) == 0 ) curr_count = count_zero / 2 ; else curr_count = ( count_zero + 1 ) / 2 ; count_zero = 0 ; } else { curr_count = count_zero ; count_zero = 0 ; } res = Math . max ( res , curr_count ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; System . out . println ( countIterations ( arr , arr . length ) ) ; } }


"Fill the missing numbers in the array of N natural numbers such that arr [ i ] not equal to i | Java implementation of above approach ; Function to fill the position with arr [ i ] = 0 ; Inserting all elements in missing [ ] set from 1 to N ; Inserting unfilled positions ; Removing allocated_elements ; Loop for filling the positions with arr [ i ] != i ; Checking for any arr [ i ] = i ; Finding the suitable position in the array to swap with found i for which arr [ i ] = i ; Checking if the position is present in unfilled_position ; Swapping arr [ i ] & arr [ pos ] ( arr [ pos ] = pos ) ; Function to Print the array ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void solve ( int arr [ ] , int n ) { Set < Integer > unfilled_indices = new HashSet < Integer > ( ) ; Set < Integer > missing = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { missing . add ( i ) ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { unfilled_indices . add ( i ) ; } else { missing . remove ( arr [ i ] ) ; } } int [ ] mi = new int [ missing . size ( ) ] ; int l = 0 ; for ( int x : missing ) { mi [ l ++ ] = x ; } int m = missing . size ( ) ; for ( int it : unfilled_indices ) { arr [ it ] = mi [ m - 1 ] ; m -- ; } int pos = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == i ) { pos = i ; } } int x ; if ( pos != 0 ) { for ( int i = 1 ; i < n ; i ++ ) { if ( pos != i ) { if ( unfilled_indices . contains ( i ) ) { x = arr [ i ] ; arr [ i ] = pos ; arr [ pos ] = x ; break ; } } } } printArray ( arr , n ) ; } static void printArray ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 7 , 4 , 0 , 3 , 0 , 5 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }


"Fill two instances of all numbers from 1 to n in a specific way | A backtracking based C ++ Program to fill two instances of all numbers from 1 to n in a specific way ; A recursive utility function to fill two instances of numbers from 1 to n in res [ 0. .2 n - 1 ] . ' curr ' is current value of n . ; If current number becomes 0 , then all numbers are filled ; Try placing two instances of ' curr ' at all possible locations till solution is found ; Two ' curr ' should be placed at ' curr + 1' distance ; Plave two instances of ' curr ' ; Recur to check if the above placement leads to a solution ; If solution is not possible , then backtrack ; This function prints the result for input number ' n ' using fillUtil ( ) ; Create an array of size 2 n and initialize all elements in it as 0 ; If solution is possible , then print it . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean fillUtil ( int res [ ] , int curr , int n ) { if ( curr == 0 ) return true ; int i ; for ( i = 0 ; i < 2 * n - curr - 1 ; i ++ ) { if ( res [ i ] == 0 && res [ i + curr + 1 ] == 0 ) { res [ i ] = res [ i + curr + 1 ] = curr ; if ( fillUtil ( res , curr - 1 , n ) ) return true ; res [ i ] = res [ i + curr + 1 ] = 0 ; } } return false ; } static void fill ( int n ) { int res [ ] = new int [ 2 * n ] ; int i ; for ( i = 0 ; i < 2 * n ; i ++ ) res [ i ] = 0 ; if ( fillUtil ( res , n , n ) ) { for ( i = 0 ; i < 2 * n ; i ++ ) System . out . print ( res [ i ] + " ▁ " ) ; } else System . out . print ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { fill ( 7 ) ; } }


"Final Matrix after incrementing submatrices by K in range given by Q queries | Java program for the above approach ; Query data type ; Function to update the given query ; Update top cell ; Update bottom left cell ; Update bottom right cell ; Update top right cell ; Function that updates the matrix mat [ ] [ ] by adding elements of aux [ ] [ ] ; Compute the prefix sum of all columns ; Compute the prefix sum of all rows ; Get the final matrix by adding mat and aux matrix at each cell ; Function that prints matrix mat [ ] ; Traverse each row ; Traverse each columns ; Function that performs each query in the given matrix and print the updated matrix after each operation performed ; Initialize all elements to 0 ; Update auxiliary matrix by traversing each query ; Update Query ; Compute the final answer ; Print the updated matrix ; Driver Code ; Given Matrix ; Given Queries ; Function Call"
How can the above be solved in Java?

 class GFG { static final int N = 3 ; static final int M = 4 ; static class query { int x1 , x2 , y1 , y2 , K ; public query ( int x1 , int x2 , int y1 , int y2 , int k ) { this . x1 = x1 ; this . x2 = x2 ; this . y1 = y1 ; this . y2 = y2 ; K = k ; } } ; static void updateQuery ( int from_x , int from_y , int to_x , int to_y , int k , int aux [ ] [ ] ) { aux [ from_x ] [ from_y ] += k ; if ( to_x + 1 < N ) aux [ to_x + 1 ] [ from_y ] -= k ; if ( to_x + 1 < N && to_y + 1 < M ) aux [ to_x + 1 ] [ to_y + 1 ] += k ; if ( to_y + 1 < M ) aux [ from_x ] [ to_y + 1 ] -= k ; } static void updateMatrix ( int mat [ ] [ ] , int aux [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 1 ; j < N ; j ++ ) { aux [ j ] [ i ] += aux [ j - 1 ] [ i ] ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { mat [ i ] [ j ] += aux [ i ] [ j ] ; } } } static void printMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . print ( "
"); } } static void matrixQuery ( int mat [ ] [ ] , int Q , query q [ ] ) { int [ ] [ ] aux = new int [ N ] [ M ] ; for ( int i = 0 ; i < Q ; i ++ ) { updateQuery ( q [ i ] . x1 , q [ i ] . x2 , q [ i ] . y1 , q [ i ] . y2 , q [ i ] . K , aux ) ; } updateMatrix ( mat , aux ) ; printMatrix ( mat ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 , 2 } , { 0 , 2 , 4 , 1 } , { 1 , 2 , 1 , 0 } } ; int Q = 1 ; query q [ ] = { new query ( 0 , 0 , 1 , 1 , 2 ) } ; matrixQuery ( mat , Q , q ) ; } }


"Final cell position in the matrix | Java implementation to find the final cell position in the given matrix ; function to find the final cell position in the given matrix ; to count up , down , left and cright movements ; to store the final coordinate position ; traverse the command array ; calculate final values ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void finalPos ( String command , int n , int x , int y ) { int cup , cdown , cleft , cright ; int final_x , final_y ; cup = cdown = cleft = cright = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( command . charAt ( i ) == ' U ' ) cup ++ ; else if ( command . charAt ( i ) == ' D ' ) cdown ++ ; else if ( command . charAt ( i ) == ' L ' ) cleft ++ ; else if ( command . charAt ( i ) == ' R ' ) cright ++ ; } final_x = x + ( cright - cleft ) ; final_y = y + ( cdown - cup ) ; System . out . println ( " Final ▁ Position : ▁ " + " ( " + final_x + " , ▁ " + final_y + " ) " ) ; } public static void main ( String [ ] args ) { String command = " DDLRULL " ; int n = command . length ( ) ; int x = 3 , y = 4 ; finalPos ( command , n , x , y ) ; } }


"Final direction after visiting every cell of Matrix starting from ( 0 , 0 ) | Java program to find the direction when stopped moving ; Function to find the direction when stopped moving ; Driver code ; Given size of NxM grid ; Function Call"
How can the above be solved in Java?

 class GFG { static void findDirection ( int n , int m ) { if ( n > m ) { if ( m % 2 == 0 ) System . out . print ( "Up
"); else System . out . print ( "Down
"); } else { if ( n % 2 == 0 ) System . out . print ( "Left
"); else System . out . print ( "Right
"); } } public static void main ( String [ ] args ) { int n = 3 , m = 3 ; findDirection ( n , m ) ; } }


"Final string after performing given operations | Java implementation of the approach ; Function to return the modified String ; Count number of ' x ' ; } Count number of ' y ' ; min ( x , y ) number of ' x ' and ' y ' will be deleted ; Driver Code"
How can the above be solved in Java?

class GFG { static String printFinalString ( String s ) { int i , n ; n = s . length ( ) ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' x ' ) { x ++ ; else { y ++ ; } } String finalString = " " ; if ( x > y ) { for ( i = 0 ; i < x - y ; i ++ ) { finalString += " x " ; } } else { for ( i = 0 ; i < y - x ; i ++ ) { finalString += " y " ; } } return finalString ; } public static void main ( String args [ ] ) { String s = " xxyyxyy " ; System . out . println ( printFinalString ( s ) ) ; } }


"Find ' N ' number of solutions with the given inequality equations | java implementation of above approach ; Function to calculate all the solutions ; there is no solutions ; print first element as y - n + 1 ; print rest n - 1 elements as 1 ; Driver code ; initialize the number of elements and the value of x an y"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findsolution ( long n , long x , long y ) { if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x y < n ) { System . out . println ( " No ▁ solution " ) ; return ; } System . out . println ( y - n + 1 ) ; while ( n -- > 1 ) System . out . println ( "1" ) ; } public static void main ( String [ ] args ) { long n , x , y ; n = 5 ; x = 15 ; y = 15 ; findsolution ( n , x , y ) ; } }


"Find ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) mod 5 | Set 2 | Java implementation of the approach ; Function to return A mod B ; length of the string ; to store required answer ; Function to return ( 1 ^ n + 2 ^ n + 3 ^ n + 4 ^ n ) % 5 ; Calculate and return ans ; Driver code"
How can the above be solved in Java?

class GFG { static int A_mod_B ( String N , int a ) { int len = N . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) ans = ( ans * 10 + ( int ) N . charAt ( i ) - '0' ) % a ; return ans % a ; } static int findMod ( String N ) { int mod = A_mod_B ( N , 4 ) ; int ans = ( 1 + ( int ) Math . pow ( 2 , mod ) + ( int ) Math . pow ( 3 , mod ) + ( int ) Math . pow ( 4 , mod ) ) ; return ( ans % 5 ) ; } public static void main ( String args [ ] ) { String N = "4" ; System . out . println ( findMod ( N ) ) ; } }


"Find 2 ^ ( 2 ^ A ) % B | Java implementation of the above approach ; Function to return 2 ^ ( 2 ^ A ) % B ; Base case , 2 ^ ( 2 ^ 1 ) % B = 4 % B ; Driver code ; Print 2 ^ ( 2 ^ A ) % B"
How can the above be solved in Java?

class GFG { static long F ( long A , long B ) { if ( A == 1 ) return ( 4 % B ) ; else { long temp = F ( A - 1 , B ) ; return ( temp * temp ) % B ; } } public static void main ( String args [ ] ) { long A = 25 , B = 50 ; System . out . println ( F ( A , B ) ) ; } }


"Find A and B from list of divisors | Java implementation of the approach ; Function to print A and B all of whose divisors are present in the given array ; Sort the array ; A is the largest element from the array ; Iterate from the second largest element ; If current element is not a divisor of A then it must be B ; If current element occurs more than once ; Print A and B ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GfG { static void printNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int A = arr [ n - 1 ] , B = - 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( A % arr [ i ] != 0 ) { B = arr [ i ] ; break ; } if ( i - 1 >= 0 && arr [ i ] == arr [ i - 1 ] ) { B = arr [ i ] ; break ; } } System . out . print ( " A ▁ = ▁ " + A + " , ▁ B ▁ = ▁ " + B ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 1 , 2 , 4 } ; int n = arr . length ; printNumbers ( arr , n ) ; } }