"Find all Autobiographical Numbers with given number of digits | Java implementation to find Autobiographical numbers with length N ; Converting the integer number to string ; Extracting each character from each index one by one and converting into an integer ; initialize count as 0 ; Check if it is equal to the index i if true then increment the count ; It is an Autobiographical number ; Return false if the count and the index number are not equal ; Function to print autobiographical number with given number of digits ; both the boundaries are taken double , so as to satisfy Math . pow ( ) function 's signature ; Left boundary of interval ; Right boundary of interval ; Flag = 0 implies that the number is not an autobiographical no . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . Math ; public class autobio { public static boolean isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = Integer . toString ( num ) ; for ( i = 0 ; i < autoStr . length ( ) ; i ++ ) { index = Integer . parseInt ( autoStr . charAt ( i ) + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . length ( ) ; j ++ ) { number = Integer . parseInt ( autoStr . charAt ( j ) + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . pow ( 10.0 , n - 1 ) ; high = Math . pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; System . out . print ( i + " , ▁ " ) ; } if ( flag == 0 ) System . out . println ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }


"Find all Factors of Large Perfect Square Natural Number in O ( sqrt ( sqrt ( N ) ) | Java program to find the factors of large perfect square number in O ( Math . sqrt ( Math . sqrt ( N ) ) ) time ; Function that find all the prime factors of N ; Store the Math . sqrt ( N ) in temp ; Initialise factor array with 1 as a factor in it ; Check divisibility by 2 ; Store the factors twice ; Check for other prime factors other than 2 ; If j is a prime factor ; Store the prime factor twice ; If j is prime number left other than 2 ; Store j twice ; Initialise Matrix M to to store all the factors ; tpc for rows tpr for column ; Initialise M [ 0 ] [ 0 ] = 1 as it also factor of N ; Traversing factor array ; If current and previous factors are not same then move to next row and insert the current factor ; If current and previous factors are same then , Insert the factor with previous factor inserted in matrix M ; The arr1 [ ] and arr2 [ ] used to store all the factors of N ; Initialise arrays as 1 ; Traversing the matrix M ; Traversing till column element doesn 't become 0 ; Store the product of every element of current row with every element in arr1 [ ] ; Copying every element of arr2 [ ] in arr1 [ ] ; length of arr2 [ ] and arr1 [ ] are equal after copying ; Print all the factors ; Drivers Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MAX = 100000 ; static void findFactors ( int N ) { int temp = ( int ) Math . sqrt ( N ) ; int [ ] factor = new int [ MAX ] ; Arrays . fill ( factor , 1 ) ; int i , j , k ; int len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp /= 2 ; } for ( j = 3 ; j < Math . sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp /= j ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } int [ ] [ ] M = new int [ len1 ] [ MAX ] ; int tpc = 0 , tpr = 0 ; M [ 0 ] [ 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr ] [ 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr ] [ tpc ] = M [ tpr ] [ tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i ] [ j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i ] [ j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { System . out . print ( arr2 [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 900 ; findFactors ( N ) ; } }


"Find all angles of a given triangle | Java Code to find all three angles of a triangle given coordinate of all three vertices ; returns square of distance b / w two points ; Square of lengths be a2 , b2 , c2 ; length of sides be a , b , c ; From Cosine law ; Converting to degree ; printing all the angles ; Driver method"
How can the above be solved in Java?

import java . awt . Point ; import static java . lang . Math . PI ; import static java . lang . Math . sqrt ; import static java . lang . Math . acos ; class Test { static int lengthSquare ( Point p1 , Point p2 ) { int xDiff = p1 . x - p2 . x ; int yDiff = p1 . y - p2 . y ; return xDiff * xDiff + yDiff * yDiff ; } static void printAngle ( Point A , Point B , Point C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = ( float ) sqrt ( a2 ) ; float b = ( float ) sqrt ( b2 ) ; float c = ( float ) sqrt ( c2 ) ; float alpha = ( float ) acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = ( float ) acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = ( float ) acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = ( float ) ( alpha * 180 / PI ) ; betta = ( float ) ( betta * 180 / PI ) ; gamma = ( float ) ( gamma * 180 / PI ) ; System . out . println ( " alpha ▁ : ▁ " + alpha ) ; System . out . println ( " betta ▁ : ▁ " + betta ) ; System . out . println ( " gamma ▁ : ▁ " + gamma ) ; } public static void main ( String [ ] args ) { Point A = new Point ( 0 , 0 ) ; Point B = new Point ( 0 , 1 ) ; Point C = new Point ( 1 , 0 ) ; printAngle ( A , B , C ) ; } }


"Find all angles of a triangle in 3D | Java program for finding all angles of a triangle ; function for finding the angle ; Driver code"
How can the above be solved in Java?

class GFG { static double angle_triangle ( int x1 , int x2 , int x3 , int y1 , int y2 , int y3 , int z1 , int z2 , int z3 ) { int num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) ; double den = Math . sqrt ( Math . pow ( ( x2 - x1 ) , 2 ) + Math . pow ( ( y2 - y1 ) , 2 ) + Math . pow ( ( z2 - z1 ) , 2 ) ) * Math . sqrt ( Math . pow ( ( x3 - x1 ) , 2 ) + Math . pow ( ( y3 - y1 ) , 2 ) + Math . pow ( ( z3 - z1 ) , 2 ) ) ; double angle = Math . acos ( num / den ) * ( 180.0 / 3.141592653589793238463 ) ; return angle ; } public static void main ( String [ ] args ) { int x1 = - 1 ; int y1 = 3 ; int z1 = 2 ; int x2 = 2 ; int y2 = 3 ; int z2 = 5 ; int x3 = 3 ; int y3 = 5 ; int z3 = - 2 ; double angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) ; double angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) ; double angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) ; System . out . println ( " Angles ▁ are ▁ : " ) ; System . out . println ( " angle ▁ A ▁ = ▁ " + angle_A + " ▁ degree " ) ; System . out . println ( " angle ▁ B ▁ = ▁ " + angle_B + " ▁ degree " ) ; System . out . println ( " angle ▁ C ▁ = ▁ " + angle_C + " ▁ degree " ) ; } }


"Find all array elements occurring more than âŒŠN / 3 âŒ ‹ times | Java program to find if any element appears more than n / 3. ; take the integers as the maximum value of integer hoping the integer would not be present in the array ; if this element is previously seen , increment count1 . ; if this element is previously seen , increment count2 . ; if current element is different from both the previously seen variables , decrement both the counts . ; Again traverse the array and find the actual counts . ; Driver code"
How can the above be solved in Java?

class GFG { static void findMajority ( int arr [ ] , int n ) { int count1 = 0 , count2 = 0 ; int flag = 0 ; int first = Integer . MIN_VALUE ; ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( first == arr [ i ] ) count1 ++ ; else if ( second == arr [ i ] ) count2 ++ ; else if ( count1 == 0 ) { count1 ++ ; first = arr [ i ] ; } else if ( count2 == 0 ) { count2 ++ ; second = arr [ i ] ; } else { count1 -- ; count2 -- ; } } count1 = 0 ; count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == first ) count1 ++ ; else if ( arr [ i ] == second ) count2 ++ ; } if ( count1 > n / 3 ) { System . out . print ( first + " ▁ " ) ; flag = 1 ; } if ( count2 > n / 3 ) { System . out . print ( second + " ▁ " ) ; flag = 1 ; } if ( flag == 0 ) System . out . println ( " No ▁ Majority ▁ Element " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 2 , 3 , 1 , 3 , 2 , 1 , 1 } ; int n = arr . length ; findMajority ( arr , n ) ; } }


"Find all combinations that add upto given number | Java program to find out all combinations of positive numbers that add upto given number ; arr - array to store the combination index - next location in array num - given number reducedNum - reduced number ; Base condition ; If combination is found , print it ; Find the previous number stored in arr [ ] . It helps in maintaining increasing order ; note loop starts from previous number i . e . at array location index - 1 ; next element of array is k ; call recursively with reduced number ; Function to find out all combinations of positive numbers that add upto given number . It uses findCombinationsUtil ( ) ; array to store the combinations It can contain max n elements ; find all combinations ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findCombinationsUtil ( int arr [ ] , int index , int num , int reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( int i = 0 ; i < index ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } static void findCombinations ( int n ) { int arr [ ] = new int [ n ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } public static void main ( String [ ] args ) { int n = 5 ; findCombinations ( n ) ; } }


"Find all distinct palindromic sub | Java program to find all distinct palindrome sub - strings of a given string ; Function to print all distinct palindrome sub - strings of s ; table for storing results ( 2 rows for odd - and even - length palindromes ; Find all sub - string palindromes from the given input string insert ' guards ' to iterate easily over s ; int rp = 0 ; length of ' palindrome ▁ radius ' ; Attempt to expand palindrome centered at i ; Assigning the found palindromic length to odd / even length array ; remove ' guards ' ; Put all obtained palindromes in a hash map to find only distinct palindromess ; printing all distinct palindromes from hash map ; Driver program"
How can the above be solved in Java?

import java . util . Map ; import java . util . TreeMap ; public class GFG { static void palindromeSubStrs ( String s ) { TreeMap < String , Integer > m = new TreeMap < > ( ) ; int n = s . length ( ) ; int [ ] [ ] R = new int [ 2 ] [ n + 1 ] ; s = " @ " + s + " # " ; for ( int j = 0 ; j <= 1 ; j ++ ) { R [ j ] [ 0 ] = 0 ; int i = 1 ; while ( i <= n ) { while ( s . charAt ( i - rp - 1 ) == s . charAt ( i + j + rp ) ) R [ j ] [ i ] = rp ; int k = 1 ; while ( ( R [ j ] [ i - k ] != rp - k ) && ( k < rp ) ) { R [ j ] [ i + k ] = Math . min ( R [ j ] [ i - k ] , rp - k ) ; k ++ ; } rp = Math . max ( rp - k , 0 ) ; i += k ; } } s = s . substring ( 1 , s . length ( ) - 1 ) ; m . put ( s . substring ( 0 , 1 ) , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= 1 ; j ++ ) for ( int rp = R [ j ] [ i ] ; rp > 0 ; rp -- ) m . put ( s . substring ( i - rp - 1 , i - rp - 1 + 2 * rp + j ) , 1 ) ; m . put ( s . substring ( i , i + 1 ) , 1 ) ; } System . out . println ( " Below ▁ are ▁ " + ( m . size ( ) ) + " ▁ palindrome ▁ sub - strings " ) ; for ( Map . Entry < String , Integer > ii : m . entrySet ( ) ) System . out . println ( ii . getKey ( ) ) ; } public static void main ( String args [ ] ) { palindromeSubStrs ( " abaaa " ) ; } }


"Find all distinct subset ( or subsequence ) sums of an array | Java program to print distinct subset sums of a given array . ; Uses Dynamic Programming to find distinct subset sums ; dp [ i ] [ j ] would be true if arr [ 0. . i - 1 ] has a subset with sum equal to j . ; There is always a subset with 0 sum ; Fill dp [ ] [ ] in bottom up manner ; Sums that were achievable without current array element ; Print last row elements ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void printDistSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] = true ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] == true ) { dp [ i ] [ j ] = true ; dp [ i ] [ j + arr [ i - 1 ] ] = true ; } } } for ( int j = 0 ; j <= sum ; j ++ ) if ( dp [ n ] [ j ] == true ) System . out . print ( j + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }


"Find all distinct subset ( or subsequence ) sums of an array | Java program to print distinct subset sums of a given array . ; sum denotes the current sum of the subset currindex denotes the index we have reached in the given array ; This function mainly calls recursive function distSumRec ( ) to generate distinct sum subsets . And finally prints the generated subsets . ; Print the result ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void distSumRec ( int arr [ ] , int n , int sum , int currindex , HashSet < Integer > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; for ( int i : s ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }


"Find all distinct subset ( or subsequence ) sums of an array | Set | Java program to find total sum of all distinct subset sums in O ( sum ) space . ; Function to print all th distinct sum ; Declare a boolean array of size equal to total sum of the array ; Fill the first row beforehand ; dp [ j ] will be true only if sum j can be formed by any possible addition of numbers in given array upto index i , otherwise false ; Iterate from maxSum to 1 and avoid lookup on any other row ; Do not change the dp array for j less than arr [ i ] ; If dp [ j ] is true then print ; Function to find the total sum and print the distinct sum ; find the sum of array elements ; Function to print all the distinct sum ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class Main { public static void subsetSum ( int arr [ ] , int n , int maxSum ) { boolean dp [ ] = new boolean [ maxSum + 1 ] ; Arrays . fill ( dp , false ) ; dp [ arr [ 0 ] ] = true ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = maxSum ; j >= 1 ; j -- ) { if ( arr [ i ] <= j ) { if ( arr [ i ] == j || dp [ j ] || dp [ ( j - arr [ i ] ) ] ) dp [ j ] = true ; else dp [ j ] = false ; } } } System . out . print ( 0 + " ▁ " ) ; for ( int j = 0 ; j <= maxSum ; j ++ ) { if ( dp [ j ] == true ) System . out . print ( j + " ▁ " ) ; } System . out . print ( "21" ) ; } public static void printDistinct ( int a [ ] , int n ) { int maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxSum += a [ i ] ; } subsetSum ( a , n , maxSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistinct ( arr , n ) ; } }


"Find all divisors of first N natural numbers | Java implementation to find all the divisors of the first N natural numbers ; Function to find the factors of the numbers from 1 to N ; Loop to find the factors of the first N natural numbers of the integer ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void factors ( int n ) { int i , j ; System . out . print ( "1 -->1
"); for ( i = 2 ; i <= n ; i ++ ) { System . out . print ( i + " ▁ - - > " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { System . out . print ( j + " , ▁ " ) ; if ( i / j != j ) System . out . print ( i / j + " , ▁ " ) ; } } System . out . print ( "
"); } } public static void main ( String args [ ] ) { int n = 5 ; factors ( n ) ; } }


"Find all divisors of first N natural numbers | Java implementation to find the factors of first N natural numbers ; Initialize global divisor vector array of sequence container ; Calculate all divisors of number ; Function to find the factors of first n natural numbers ; Driver Code ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int MAX = ( int ) 1e5 ; static Vector < Integer > [ ] divisor = new Vector [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i + " - - > " ) ; for ( int divi : divisor [ i ] ) { System . out . print ( divi + " , ▁ " ) ; } System . out . print ( "
"); } } public static void main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . length ; i ++ ) divisor [ i ] = new Vector < Integer > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }


"Find all even length binary sequences with same sum of first and second half bits | Iterative | Java code implementation for above approach ; Function to convert the number into binary and store the number into an array ; Function to check if the sum of the digits till the mid of the array and the sum of the digits from mid till n is the same , if they are same then print that binary ; Calculating the sum from 0 till mid and store in sum1 ; Calculating the sum from mid till n and store in sum2 ; If sum1 is same as sum2 print the binary ; Function to print sequence ; Creating the array ; Looping over powers of 2 ; Converting the number into binary first ; Checking if the sum of the first half of the array is same as the sum of the next half ; Driver Code"
How can the above be solved in Java?

class GFG { static void convertToBinary ( int num , int a [ ] , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } static void checkforsum ( int a [ ] , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] ) ; System . out . println ( ) ; } } static void print_seq ( int m ) { int n = ( 2 * m ) ; int a [ ] = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) Math . pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } public static void main ( String [ ] args ) { int m = 2 ; print_seq ( m ) ; } }


"Find all factorial numbers less than or equal to n | Java program to find all factorial numbers smaller than or equal to n . ; Compute next factorial using previous ; Driver code"
How can the above be solved in Java?

class GFG { static void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { System . out . print ( fact + " ▁ " ) ; fact = fact * x ; x ++ ; } } public static void main ( String [ ] args ) { int n = 100 ; printFactorialNums ( n ) ; } }


"Find all good indices in the given Array | Java program to find all good indices in the given array ; Function to find all good indices in the given array ; hash to store frequency of each element ; Storing frequency of each element and calculating sum simultaneously ; check if array is good after removing i - th index element ; print good indices ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class Solution { static void niceIndices ( int A [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . put ( A [ i ] , ( m . get ( A [ i ] ) == null ) ? 0 : m . get ( A [ i ] ) + 1 ) ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . containsKey ( k ) ) { if ( ( A [ i ] == k && m . get ( k ) > 1 ) || ( A [ i ] != k ) ) System . out . print ( ( i + 1 ) + " ▁ " ) ; } } } } public static void main ( String args [ ] ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = A . length ; niceIndices ( A , n ) ; } }


"Find all matrix elements which are minimum in their row and maximum in their column | Java program for the above approach ; Functionto find all the matrix elements which are minimum in its row and maximum in its column ; Initialize unordered set ; Traverse the matrix ; Update the minimum element of current row ; Insert the minimum element of the row ; Update the maximum element of current column ; Checking if it is already present in the unordered_set or not ; Driver code ; Function call ; If no such matrix element is found"
How can the above be solved in Java?

import java . util . * ; class GFG { public static Vector < Integer > minmaxNumbers ( int [ ] [ ] matrix , Vector < Integer > res ) { Set < Integer > set = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < matrix . length ; i ++ ) { int minr = Integer . MAX_VALUE ; for ( int j = 0 ; j < matrix [ i ] . length ; j ++ ) { minr = Math . min ( minr , matrix [ i ] [ j ] ) ; } set . add ( minr ) ; } for ( int j = 0 ; j < matrix [ 0 ] . length ; j ++ ) { int maxc = Integer . MIN_VALUE ; for ( int i = 0 ; i < matrix . length ; i ++ ) { maxc = Math . max ( maxc , matrix [ i ] [ j ] ) ; } if ( set . contains ( maxc ) ) { res . add ( maxc ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 10 , 4 } , { 9 , 3 , 8 } , { 15 , 16 , 17 } } ; Vector < Integer > ans = new Vector < Integer > ( ) ; ans = minmaxNumbers ( mat , ans ) ; if ( ans . size ( ) == 0 ) System . out . println ( " - 1" ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . println ( ans . get ( i ) ) ; } }


"Find all missing numbers from a given sorted array | Java program for the above approach ; Function to find the missing elements ; Initialize an array with zero of size equals to the maximum element in the array ; Make b [ i ] = 1 if i is present in the array ; If the element is present make b [ arr [ i ] ] = 1 ; Print the indices where b [ i ] = 0 ; Driver Code ; Given array arr [ ] ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printMissingElements ( int arr [ ] , int N ) { int [ ] b = new int [ arr [ N - 1 ] + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int i = arr [ 0 ] ; i <= arr [ N - 1 ] ; i ++ ) { if ( b [ i ] == 0 ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . length ; printMissingElements ( arr , N ) ; } }


"Find all missing numbers from a given sorted array | Java program for the above approach ; Function to find the missing elements ; Initialize diff ; Check if diff and arr [ i ] - i both are equal or not ; Loop for consecutive missing elements ; Driver Code ; Given array arr [ ] ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printMissingElements ( int arr [ ] , int N ) { int diff = arr [ 0 ] - 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] - i != diff ) { while ( diff < arr [ i ] - i ) { System . out . print ( ( i + diff ) + " ▁ " ) ; diff ++ ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 10 , 11 , 13 } ; int N = arr . length ; printMissingElements ( arr , N ) ; } }


"Find all numbers in range [ 1 , N ] that are not present in given Array | Java program for the above approach ; Function to find the missing numbers ; traverse the array arr [ ] ; Update ; Traverse the array arr [ ] ; If Num is not present ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static void getMissingNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ Math . abs ( arr [ i ] ) - 1 ] = - ( Math . abs ( arr [ Math . abs ( arr [ i ] ) - 1 ] ) ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) System . out . print ( i + 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 5 , 5 , 4 , 4 , 2 } ; getMissingNumbers ( arr , N ) ; } }


"Find all occurrences of a given word in a matrix | Java Program to find all occurrences of the word in a matrix ; check whether given cell ( row , col ) is a valid cell or not . ; return true if row number and column number is in range ; These arrays are used to get row and column numbers of 8 neighboursof a given cell ; A utility function to do DFS for a 2D boolean matrix . It only considers the 8 neighbours as adjacent vertices ; return if current character doesn 't match with  the next character in the word ; append current character position to path ; current character matches with the last character in the word ; Recur for all connected neighbours ; The main function to find all occurrences of the word in a matrix ; traverse through the all cells of given matrix ; occurrence of first character in matrix ; check and print if path exists ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static final int ROW = 3 ; static final int COL = 5 ; static boolean isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } static int rowNum [ ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int colNum [ ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static void DFS ( char mat [ ] [ ] , int row , int col , int prevRow , int prevCol , char [ ] word , String path , int index , int n ) { if ( index > n mat [ row ] [ col ] != word [ index ] ) return ; path += ( word [ index ] ) + " ( " + String . valueOf ( row ) + " , ▁ " + String . valueOf ( col ) + " ) ▁ " ; if ( index == n ) { System . out . print ( path + "
"); return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } static void findWords ( char mat [ ] [ ] , char [ ] word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i ] [ j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } public static void main ( String [ ] args ) { char mat [ ] [ ] = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char [ ] word = " DES " . toCharArray ( ) ; findWords ( mat , word , word . length - 1 ) ; } }


"Find all pairs ( a , b ) in an array such that a % b = k | Java implementation to find such pairs ; method to find pair such that ( a % b = k ) ; Consider each and every pair ; Print if their modulo equals to k ; Driver method"
How can the above be solved in Java?

class Test { static boolean printPairs ( int arr [ ] , int n , int k ) { boolean isPairFound = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j && arr [ i ] % arr [ j ] == k ) { System . out . print ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " ▁ " ) ; isPairFound = true ; } } } return isPairFound ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 } ; int k = 3 ; if ( printPairs ( arr , arr . length , k ) == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } }


"Find all pairs ( a , b ) in an array such that a % b = k | Java program to find all pairs such that a % b = k . ; Utility method to find the divisors of n and store in vector v [ ] ; Vector is used to store the divisors ; If n is a square number , push only one occurrence ; method to find pairs such that ( a % b = k ) ; Store all the elements in the map to use map as hash for finding elements in O ( 1 ) time . ; Print all the pairs with ( a , b ) as ( k , numbers greater than k ) as k % ( num ( > k ) ) = k i . e . 2 % 4 = 2 ; Now check for the current element as ' a ' how many b exists such that a % b = k ; find all the divisors of ( arr [ i ] - k ) ; Check for each divisor i . e . arr [ i ] % b = k or not , if yes then print that pair . ; Clear vector ; Driver method"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . Vector ; class Test { static Vector < Integer > findDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) v . add ( i ) ; else { v . add ( i ) ; v . add ( n / i ) ; } } } return v ; } static boolean printPairs ( int arr [ ] , int n , int k ) { HashMap < Integer , Boolean > occ = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( arr [ i ] , true ) ; boolean isPairFound = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( occ . get ( k ) && k < arr [ i ] ) { System . out . print ( " ( " + k + " , ▁ " + arr [ i ] + " ) ▁ " ) ; isPairFound = true ; } if ( arr [ i ] >= k ) { Vector < Integer > v = findDivisors ( arr [ i ] - k ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( arr [ i ] % v . get ( j ) == k && arr [ i ] != v . get ( j ) && occ . get ( v . get ( j ) ) ) { System . out . print ( " ( " + arr [ i ] + " , ▁ " + v . get ( j ) + " ) ▁ " ) ; isPairFound = true ; } } v . clear ( ) ; } } return isPairFound ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 1 , 2 , 5 , 4 } ; int k = 2 ; if ( printPairs ( arr , arr . length , k ) == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } }


"Find all pairs raised to power K differs by exactly N | Java program for the above approach ; Function to print pairs whose difference raised to the power K is X ; Stores the count of valid pairs ; Iterate over the range [ - 1000 , 1000 ] ; Iterate over the range [ - 1000 , 1000 ] ; If the current pair satisfies the given condition ; Increment the count by 1 ; If no such pair exists ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void ValidPairs ( int X , int K ) { int count = 0 ; for ( int A = - 1000 ; A <= 1000 ; A ++ ) { for ( int B = - 1000 ; B <= 1000 ; B ++ ) { if ( Math . pow ( A , K ) - Math . pow ( B , K ) == X ) { count ++ ; System . out . println ( A + " ▁ " + B ) ; } } } if ( count == 0 ) { System . out . println ( " - 1" ) ; } } public static void main ( String args [ ] ) { int X = 33 ; int K = 5 ; ValidPairs ( X , K ) ; } }


"Find all palindrome numbers of given digits | Java implementation of the approach ; Function to return the reverse of num ; Function that returns true if num is palindrome ; If the number is equal to the reverse of it then it is a palindrome ; Function to print all the d - digit palindrome numbers ; Smallest and the largest d - digit numbers ; Starting from the smallest d - digit number till the largest ; If the current number is palindrome ; Driver code"
How can the above be solved in Java?

class GFG { static int reverse ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num = num / 10 ; } return rev ; } static boolean isPalindrome ( int num ) { if ( num == reverse ( num ) ) return true ; return false ; } static void printPalindromes ( int d ) { if ( d <= 0 ) return ; int smallest = ( int ) Math . pow ( 10 , d - 1 ) ; int largest = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = smallest ; i <= largest ; i ++ ) { if ( isPalindrome ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int d = 2 ; printPalindromes ( d ) ; } }


"Find all possible binary trees with given Inorder Traversal | Java program to find binary tree with given inorder traversal ; Class containing left and right child of current node and key value ; Class to print Level Order Traversal ; A utility function to do preorder traversal of BST ; Function for constructing all possible trees with given inorder traversal stored in an array from arr [ start ] to arr [ end ] . This function returns a vector of trees . ; List to store all possible trees ; if start > end then subtree will be empty so returning NULL in the list ; Iterating through all values from start to end for constructing left and right subtree recursively ; Constructing left subtree ; Constructing right subtree ; Now looping through all left and right subtrees and connecting them to ith root below ; Making arr [ i ] as root ; Connecting left subtree ; Connecting right subtree ; Adding this tree to list ; Driver Program to test above functions"
How can the above be solved in Java?

import java . util . Vector ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; void preOrder ( Node node ) { if ( node != null ) { System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } } Vector < Node > getTrees ( int arr [ ] , int start , int end ) { Vector < Node > trees = new Vector < Node > ( ) ; if ( start > end ) { trees . add ( null ) ; return trees ; } for ( int i = start ; i <= end ; i ++ ) { Vector < Node > ltrees = getTrees ( arr , start , i - 1 ) ; Vector < Node > rtrees = getTrees ( arr , i + 1 , end ) ; for ( int j = 0 ; j < ltrees . size ( ) ; j ++ ) { for ( int k = 0 ; k < rtrees . size ( ) ; k ++ ) { Node node = new Node ( arr [ i ] ) ; node . left = ltrees . get ( j ) ; node . right = rtrees . get ( k ) ; trees . add ( node ) ; } } } return trees ; } public static void main ( String args [ ] ) { int in [ ] = { 4 , 5 , 7 } ; int n = in . length ; BinaryTree tree = new BinaryTree ( ) ; Vector < Node > trees = tree . getTrees ( in , 0 , n - 1 ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ different ▁ " + " ▁ binary ▁ trees ▁ are : " ) ; for ( int i = 0 ; i < trees . size ( ) ; i ++ ) { tree . preOrder ( trees . get ( i ) ) ; System . out . println ( " " ) ; } } }


"Find all possible coordinates of parallelogram | Java program to all possible points of a parallelogram ; Driver code ; coordinates of A ; coordinates of B ; coordinates of C"
How can the above be solved in Java?

public class ParallelogramPoints { public static void main ( String [ ] s ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; System . out . println ( ax + bx - cx + " , ▁ " + ( ay + by - cy ) ) ; System . out . println ( ax + cx - bx + " , ▁ " + ( ay + cy - by ) ) ; System . out . println ( cx + bx - ax + " , ▁ " + ( cy + by - ax ) ) ; } }


"Find all possible pairs with given Bitwise OR and Bitwise XOR values | Java code for the above approach ; Function to find pairs with XOR equal to A and OR equal to B ; Iterate from 1 to B ; Check if ( i OR y ) is B ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findPairs ( int A , int B ) { for ( int i = 1 ; i <= B ; i ++ ) { int y = A ^ i ; if ( y > 0 && ( i y ) == B ) { System . out . println ( i + " ▁ " + y ) ; } } } public static void main ( String [ ] args ) { int A = 8 , B = 10 ; findPairs ( A , B ) ; } }


"Find all possible subarrays having product less than or equal to K | Java Program to implement the above approach ; Function to return all possible subarrays having product less than or equal to K ; Store the required subarrays ; Stores the product of current subarray ; Stores the starting index of the current subarray ; Check for empty array ; Iterate over the array ; Calculate product ; If product exceeds K ; Reduce product ; Increase starting index of current subarray ; Stores the subarray elements ; Store the subarray elements ; Add the subarrays to the list ; Return the final list of subarrays ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static List < List < Integer > > maxSubArray ( int [ ] arr , int K ) { List < List < Integer > > solution = new ArrayList < > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . length <= 1 K < 0 ) { return new ArrayList < > ( ) ; } for ( int i = 0 ; i < arr . length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < Integer > list = new ArrayList < > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . add ( 0 , arr [ j ] ) ; solution . add ( new ArrayList < > ( list ) ) ; } } return solution ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; System . out . println ( maxSubArray ( arr , K ) ) ; } }


"Find all possible values of K such that the sum of first N numbers starting from K is G | Java program for the above approach ; Function to find the count the value of K such that sum of the first N numbers from K is G ; Stores the total count of K ; Iterate till square root of g ; If the number is factor of g ; If the second factor is not equal to first factor ; Check if two factors are odd or not ; If second factor is the same as the first factor then check if the first factor is odd or not ; Print the resultant count ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findValuesOfK ( int g ) { int count = 0 ; for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { if ( i != g / i ) { if ( i % 2 == 1 ) { count ++ ; } if ( ( g / i ) % 2 == 1 ) { count ++ ; } } else if ( i % 2 == 1 ) { count ++ ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int G = 125 ; findValuesOfK ( G ) ; } }


"Find all possible ways to Split the given string into Primes | Java program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static boolean [ ] primes = new boolean [ 1000001 ] ; static int maxn = 1000000 ; static void sieve ( ) { Arrays . fill ( primes , true ) ; primes [ 0 ] = false ; primes [ 1 ] = false ; for ( int i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( int j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } static String toBinary ( int n ) { String r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? "0" : "1" ) + r ; n /= 2 ; } return ( r == " " ) ? "0" : r ; } static void PrimeSplit ( String str ) { String temp ; int cnt = 0 ; ArrayList < String > ans = new ArrayList < > ( ) ; int bt = 1 << ( str . length ( ) - 1 ) ; int n = str . length ( ) ; for ( int i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + "0" ; int j = 0 , x = n - temp . length ( ) , y ; while ( j < x ) { temp = "0" + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; String sp = " " , tp = " " ; boolean flag = false ; while ( j < n ) { sp += str . charAt ( j ) ; if ( temp . charAt ( j ) == '1' ) { tp += sp + ' , ' ; y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) { flag = true ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) flag = true ; } if ( ! flag ) ans . add ( tp ) ; } if ( ans . size ( ) == 0 ) { System . out . println ( - 1 ) ; } for ( String i : ans ) { System . out . println ( i ) ; } } public static void main ( String [ ] args ) { String str = "11373" ; sieve ( ) ; PrimeSplit ( str ) ; } }


"Find all sides of a right angled triangle from given hypotenuse and area | Set 1 | Java program to get right angle triangle , given hypotenuse and area of triangle ; limit for float comparison ; Utility method to get area of right angle triangle , given base and hypotenuse ; Prints base and height of triangle using hypotenuse and area information ; maximum area will be obtained when base and height are equal ( = sqrt ( h * h / 2 ) ) ; if given area itself is larger than maxArea then no solution is possible ; binary search for base ; get height by pythagorean rule ; Driver code to test above methods"
How can the above be solved in Java?

public class GFG { final static double eps = ( double ) 1e-6 ; static double getArea ( double base , double hypotenuse ) { double height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } static void printRightAngleTriangle ( int hypotenuse , int area ) { int hsquare = hypotenuse * hypotenuse ; double sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; double maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { System . out . print ( " Not ▁ possible " ) ; return ; } double low = 0.0 ; double high = sideForMaxArea ; double base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } double height = Math . sqrt ( hsquare - base * base ) ; System . out . println ( Math . round ( base ) + " ▁ " + Math . round ( height ) ) ; } static public void main ( String [ ] args ) { int hypotenuse = 5 ; int area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ; } }


"Find all the pairs with given sum in a BST | Set 2 | Java implementation of the approach ; A binary tree node ; Function to add a node to the BST ; If the tree is empty , return a new node ; Otherwise , recur down the tree ; Function to find the target pairs ; LeftList which stores the left side values ; RightList which stores the right side values ; curr_left pointer is used for left side execution and curr_right pointer is used for right side execution ; Storing the left side values into LeftList till leaf node not found ; Storing the right side values into RightList till leaf node not found ; Last node of LeftList ; Last node of RightList ; To prevent repetition like 2 , 6 and 6 , 2 ; Delete the last value of LeftList and make the execution to the right side ; Delete the last value of RightList and make the execution to the left side ; ( left value + right value ) = target then print the left value and right value Delete the last value of left and right list and make the left execution to right side and right side execution to left side ; Driver code"
How can the above be solved in Java?

import java . util . * ; public class GFG { public static class Node { int data ; Node left , right , root ; Node ( int data ) { this . data = data ; } } public static Node AddNode ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } public static void TargetPair ( Node node , int tar ) { ArrayList < Node > LeftList = new ArrayList < > ( ) ; ArrayList < Node > RightList = new ArrayList < > ( ) ; Node curr_left = node ; Node curr_right = node ; while ( curr_left != null || curr_right != null || LeftList . size ( ) > 0 && RightList . size ( ) > 0 ) { while ( curr_left != null ) { LeftList . add ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . add ( curr_right ) ; curr_right = curr_right . right ; } Node LeftNode = LeftList . get ( LeftList . size ( ) - 1 ) ; Node RightNode = RightList . get ( RightList . size ( ) - 1 ) ; int leftVal = LeftNode . data ; int rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . remove ( LeftList . size ( ) - 1 ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . remove ( RightList . size ( ) - 1 ) ; curr_right = RightNode . left ; } else { System . out . println ( LeftNode . data + " ▁ " + RightNode . data ) ; RightList . remove ( RightList . size ( ) - 1 ) ; LeftList . remove ( LeftList . size ( ) - 1 ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } public static void main ( String [ ] b ) { Node root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; } }


"Find all the patterns of "1(0 + ) 1" in a given string | SET 1 ( General Approach ) | Java Code to count 1 ( 0 + ) 1 patterns in a string ; Function to count patterns ; Variable to store the last character ; We found 0 and last character was '1' , state change ; After the stream of 0 ' s , ▁ we ▁ ▁ got ▁ a ▁ ' 1 ',counter incremented ; Last character stored ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int patternCount ( String str ) { char last = str . charAt ( 0 ) ; int i = 1 , counter = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) == '0' && last == '1' ) { while ( str . charAt ( i ) == '0' ) i ++ ; if ( str . charAt ( i ) == '1' ) counter ++ ; } last = str . charAt ( i ) ; i ++ ; } return counter ; } public static void main ( String [ ] args ) { String str = "1001ab010abc01001" ; System . out . println ( patternCount ( str ) ) ; } }


"Find all the possible remainders when N is divided by all positive integers from 1 to N + 1 | Java implementation of the approach ; Function to find all the distinct remainders when n is divided by all the elements from the range [ 1 , n + 1 ] ; Set will be used to store the remainders in order to eliminate duplicates ; Find the remainders ; Print the contents of the set ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findRemainders ( long n ) { HashSet < Long > vc = new HashSet < Long > ( ) ; for ( long i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( n / i ) ; for ( long i = ( long ) ( n / Math . ceil ( Math . sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . add ( i ) ; for ( long it : vc ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }


"Find all the queens attacking the king in a chessboard | Java program to implement the above approach ; Method to find the queen closest to king in an attacking position ; Method to find all the queens attacking the king in the chessboard ; If king is horizontally on the right of current queen ; If no attacker is present in that direction ; Or if the current queen is closest in that direction ; Set current queen as the attacker ; If king is horizontally on the left of current queen ; If no attacker is present in that direction ; Or if the current queen is closest in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the left by a queen diagonal above ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the left by a queen diagonally below ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the right by a queen diagonally above ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If the king is attacked by a queen from the right by a queen diagonally below ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If a king is vertically below the current queen ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; If a king is vertically above the current queen ; If no attacker is present in that direction ; Or the current queen is the closest attacker in that direction ; Set current queen as the attacker ; Return the coordinates ; Print all the coordinates of the queens attacking the king ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . util . stream . Collectors ; class GFG { private static int dis ( int [ ] ans , int [ ] attacker ) { return Math . abs ( ans [ 0 ] - attacker [ 0 ] ) + Math . abs ( ans [ 1 ] - attacker [ 1 ] ) ; } private static List < List < Integer > > findQueens ( int [ ] [ ] queens , int [ ] king ) { List < List < Integer > > sol = new ArrayList < List < Integer > > ( ) ; int [ ] [ ] attackers = new int [ 8 ] [ 2 ] ; for ( int i = 0 ; i < 8 ; i ++ ) { Arrays . fill ( attackers [ i ] , - 1 ) ; } for ( int i = 0 ; i < queens . length ; i ++ ) { if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] > queens [ i ] [ 1 ] ) { if ( ( attackers [ 3 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 3 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 3 ] = queens [ i ] ; } if ( king [ 0 ] == queens [ i ] [ 0 ] && king [ 1 ] < queens [ i ] [ 1 ] ) { if ( ( attackers [ 4 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 4 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 4 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 0 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 0 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 0 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == king [ 1 ] - queens [ i ] [ 1 ] && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 7 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 7 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 7 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] > queens [ i ] [ 0 ] ) { if ( ( attackers [ 1 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 1 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 1 ] = queens [ i ] ; } if ( king [ 1 ] - queens [ i ] [ 1 ] == 0 && king [ 0 ] < queens [ i ] [ 0 ] ) { if ( ( attackers [ 6 ] [ 0 ] == - 1 ) || ( dis ( attackers [ 6 ] , king ) > dis ( queens [ i ] , king ) ) ) attackers [ 6 ] = queens [ i ] ; } if ( king [ 0 ] - queens [ i ] [ 0 ] == - ( king...


"Find amount of water wasted after filling the tank | Java program to find the volume of water wasted ; Function to calculate amount of wasted water ; filled amount of water in one minute ; total time taken to fill the tank because of leakage ; wasted amount of water ; Driver code"
How can the above be solved in Java?

class GFG { static double wastedWater ( double V , double M , double N ) { double wasted_amt , amt_per_min , time_to_fill ; amt_per_min = M - N ; time_to_fill = V / amt_per_min ; wasted_amt = N * time_to_fill ; return wasted_amt ; } public static void main ( String [ ] args ) { double V , M , N ; V = 700 ; M = 10 ; N = 3 ; System . out . println ( wastedWater ( V , M , N ) ) ; V = 1000 ; M = 100 ; N = 50 ; System . out . println ( wastedWater ( V , M , N ) ) ; } }


"Find amount to be added to achieve target ratio in a given mixture | Java program to find amount of water to be added to achieve given target ratio . ; Driver code"
How can the above be solved in Java?

public class GFG { static float findAmount ( float X , float W , float Y ) { return ( X * ( Y - W ) ) / ( 100 - Y ) ; } public static void main ( String args [ ] ) { float X = 100 , W = 50 , Y = 60 ; System . out . println ( " Water ▁ to ▁ be ▁ added ▁ = ▁ " + findAmount ( X , W , Y ) ) ; } }


"Find an N x N grid whose xor of every row and column is equal | Java implementation of the approach ; Function to find the n x n matrix that satisfies the given condition ; Initialize x to 0 ; Divide the n x n matrix into n / 4 matrices for each of the n / 4 rows where each matrix is of size 4 x 4 ; Print the generated matrix ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findGrid ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; int x = 0 ; for ( int i = 0 ; i < n / 4 ; i ++ ) { for ( int j = 0 ; j < n / 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { for ( int l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k ] [ j * 4 + l ] = x ; x ++ ; } } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . println ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; findGrid ( n ) ; } }


"Find an N | Java implementation of the above approach ; Function to print the required permutation ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 5 ; findPermutation ( N ) ; } }


"Find an N | Java program for the above approach ; Function to find an N - length binary string having maximum sum of elements from all given ranges ; Iterate over the range [ 1 , N ] ; If i is odd , then print 0 ; Otherwise , print 1 ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static void printBinaryString ( int arr [ ] [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( 0 ) ; } else { System . out . print ( 1 ) ; } } } public static void main ( String [ ] args ) { int N = 5 , M = 3 ; int arr [ ] [ ] = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; } }


"Find an anagram of given String having different characters at corresponding indices | Java program for the above approach ; Function to find anagram of string such that characters at the same indices are different ; Copying our original string for comparison ; Declaring the two pointers ; Checking the given condition ; When string length is odd ; The mid element ; If the characters are the same , then perform the swap operation as illustrated ; Check if the corresponding indices has the same character or not ; If string follows required condition ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findAnagram ( String s ) { String check = s ; int i = 0 , j = s . length ( ) - 1 ; while ( i < s . length ( ) && j >= 0 ) { if ( s . charAt ( i ) != s . charAt ( j ) && check . charAt ( i ) != s . charAt ( j ) && check . charAt ( j ) != s . charAt ( i ) ) { char temp = s . charAt ( i ) ; s = s . substring ( 0 , i ) + s . charAt ( j ) + s . substring ( i + 1 ) ; s = s . substring ( 0 , j ) + temp + s . substring ( j + 1 ) ; i ++ ; j = s . length ( ) - 1 ; } else { j -- ; } } if ( s . length ( ) % 2 != 0 ) { int mid = s . length ( ) / 2 ; if ( check . charAt ( mid ) == s . charAt ( mid ) ) { for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( check . charAt ( i ) != s . charAt ( mid ) && s . charAt ( i ) != s . charAt ( mid ) ) { char temp = s . charAt ( i ) ; s = s . substring ( 0 , i ) + s . charAt ( mid ) + s . substring ( i + 1 ) ; s = s . substring ( 0 , mid ) + temp + s . substring ( mid + 1 ) ; break ; } } } } boolean ok = true ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( check . charAt ( i ) == s . charAt ( i ) ) { ok = false ; break ; } } if ( ok ) System . out . println ( s ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { String S = " geek " ; findAnagram ( S ) ; } }


"Find an array element such that all elements are divisible by it | Java Program to find the smallest number that divides all numbers in an array ; function to find the smallest element ; function to find smallest num ; Find the smallest element ; Check if all array elements are divisible by smallest . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int min_element ( int a [ ] ) { int min = Integer . MAX_VALUE , i ; for ( i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } static int findSmallest ( int a [ ] , int n ) { int smallest = min_element ( a ) ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }


"Find an array element such that all elements are divisible by it | Java program to find an array element that divides all numbers in the array using naive approach ; function to find smallest num ; traverse for all elements ; stores the minimum if it divides all ; driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int findSmallest ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }


"Find an array of size N having exactly K subarrays with sum S | Java program to find array with K subarrays with sum S ; Function to find array with K subarrays with sum S ; Driver Code ; Function call"
How can the above be solved in Java?

class GFG { static void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) System . out . print ( s + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) System . out . print ( s + 1 + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; } }


"Find an array of size N that satisfies the given conditions | Java implementation of the approach ; Utility function to print the contents of an array ; Function to generate and print the required array ; Initially all the positions are empty ; To store the count of positions i such that arr [ i ] = s ; To store the final array elements ; Set arr [ i ] = s and the gap between them is exactly 2 so in for loop we use i += 2 ; Mark the i 'th position as visited  as we put arr[i] = s ; Increment the count ; Finding the next odd number after s ; If the i 'th position is not visited  it means we did not put any value  at position i so we put 1 now ; Print the final array ; Driver code"
How can the above be solved in Java?

class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int n , int k , int s ) { int vis [ ] = new int [ n ] ; int cnt = 0 ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n && cnt < k ; i += 2 ) { arr [ i ] = s ; vis [ i ] = 1 ; cnt ++ ; } int val = s ; if ( s % 2 == 0 ) val ++ ; else val = val + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { arr [ i ] = val ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int n = 8 , k = 3 , s = 12 ; findArray ( n , k , s ) ; } }


"Find an equal point in a string of brackets | Java program to find an index k which decides the number of opening brackets is equal to the number of closing brackets ; Method to find an equal index ; Store the number of opening brackets at each index ; Store the number of closing brackets at each index ; check if there is no opening or closing brackets ; check if there is any index at which both brackets are equal ; Driver Method"
How can the above be solved in Java?

public class GFG { static int findIndex ( String str ) { int len = str . length ( ) ; int open [ ] = new int [ len + 1 ] ; int close [ ] = new int [ len + 1 ] ; int index = - 1 ; open [ 0 ] = 0 ; close [ len ] = 0 ; if ( str . charAt ( 0 ) == ' ( ' ) open [ 1 ] = 1 ; if ( str . charAt ( len - 1 ) == ' ) ' ) close [ len - 1 ] = 1 ; for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) open [ i + 1 ] = open [ i ] + 1 ; else open [ i + 1 ] = open [ i ] ; } for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == ' ) ' ) close [ i ] = close [ i + 1 ] + 1 ; else close [ i ] = close [ i + 1 ] ; } if ( open [ len ] == 0 ) return len ; if ( close [ 0 ] == 0 ) return 0 ; for ( int i = 0 ; i <= len ; i ++ ) if ( open [ i ] == close [ i ] ) index = i ; return index ; } public static void main ( String [ ] args ) { String str = " ( ( ) ) ) ( ( ) ( ) ( ) ) ) ) " ; System . out . println ( findIndex ( str ) ) ; } }


"Find an index such that difference between product of elements before and after it is minimum |  ; Function to find index ; Array to store log values of elements ; Prefix Array to Maintain Sum of log values till index i ; Answer Index ; Find minimum absolute value ; Driver code"
How can the above be solved in Java?

public class Main { public static void solve ( int Array [ ] , int N ) { double Arraynew [ ] = new double [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Arraynew [ i ] = Math . log ( Array [ i ] ) ; } double prefixsum [ ] = new double [ N ] ; prefixsum [ 0 ] = Arraynew [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { prefixsum [ i ] = prefixsum [ i - 1 ] + Arraynew [ i ] ; } int answer = 0 ; double minabs = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ 0 ] ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { double ans1 = Math . abs ( prefixsum [ N - 1 ] - 2 * prefixsum [ i ] ) ; if ( ans1 < minabs ) { minabs = ans1 ; answer = i ; } } System . out . println ( " Index ▁ is : ▁ " + answer ) ; } public static void main ( String [ ] args ) { int Array [ ] = { 1 , 4 , 12 , 2 , 6 } ; int N = 5 ; solve ( Array , N ) ; } }


"Find an index such that difference between product of elements before and after it is minimum | Java implementation of the approach ; Function to return the index i such that the absolute difference between product of elements up to that index and the product of rest of the elements of the array is minimum ; To store the required index ; Prefix product array ; Compute the product array ; Iterate the product array to find the index ; Driver code"
How can the above be solved in Java?

class GFG { static int findIndex ( int a [ ] , int n ) { int res = 0 ; long min_diff = Long . MAX_VALUE ; long prod [ ] = new long [ n ] ; prod [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prod [ i ] = prod [ i - 1 ] * a [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long curr_diff = Math . abs ( ( prod [ n - 1 ] / prod [ i ] ) - prod [ i ] ) ; if ( curr_diff < min_diff ) { min_diff = curr_diff ; res = i ; } } return res ; } public static void main ( String arg [ ] ) { int arr [ ] = { 3 , 2 , 5 , 7 , 2 , 9 } ; int N = arr . length ; System . out . println ( findIndex ( arr , N ) ) ; } }


"Find an integer in the given range that satisfies the given conditions | Java implementation of the approach ; Function that returns true if x contains all distinct digits ; Last digit of x ; If current digit has appeared before ; Mark the current digit to present ; Remove the last digit ; Function to return the required value of k ; To store the maximum value for the given expression ; If i contains all distinct digits ; If the value of the expression is also maximum then update k and the expression ; Driver code"
How can the above be solved in Java?

class GFG { static int MAX = 10 ; static boolean distinctDigits ( int x ) { boolean [ ] present = new boolean [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = Integer . MIN_VALUE ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void main ( String [ ] args ) { int l = 50 , r = 60 ; System . out . print ( findK ( l , r ) ) ; } }


"Find an integer that is common in the maximum number of given arithmetic progressions | Java implementation of the approach ; Function to return element common in maximum number of APs ; Initialize the count variable ; Increment count for every element of an AP ; Find the index with maximum count ; Return the maximum common element ; Driver code"
How can the above be solved in Java?

class GFG { final static int MAXN = 1000000 ; static int max_element ( int [ ] A , int n ) { int max = A [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] > max ) max = A [ i ] ; return max ; } static int maxCommonElement ( int A [ ] , int D [ ] , int N ) { int cnt [ ] = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } int ans = 0 ; int com = 0 ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } public static void main ( String args [ ] ) { int A [ ] = { 13 , 1 , 2 , 5 } , D [ ] = { 5 , 10 , 1 , 12 } ; int N = A . length ; System . out . println ( maxCommonElement ( A , D , N ) ) ; } }


"Find an integral solution of the non | Java program to implement the above approach ; Function to find the value of power ( X , N ) ; Stores the value of ( X ^ N ) ; Calculate the value of power ( x , N ) ; If N is odd ; Update res ; Update x ; Update N ; Function to find the value of X and Y that satisfy the condition ; Base Case ; Stores maximum possible of X . ; Update xMax ; Stores maximum possible of Y . ; Update yMax ; Iterate over all possible values of X ; Iterate over all possible values of Y ; Stores value of 2 ^ i ; Stores value of 5 ^ j ; If the pair ( i , j ) satisfy the equation ; If no solution exists ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( ( N & 1 ) != 0 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void findValX_Y ( int N ) { if ( N <= 1 ) { System . out . println ( - 1 ) ; return ; } int xMax ; xMax = ( int ) Math . log ( N ) ; int yMax ; yMax = ( int ) ( Math . log ( N ) / Math . log ( 5.0 ) ) ; for ( int i = 1 ; i <= xMax ; i ++ ) { for ( int j = 1 ; j <= yMax ; j ++ ) { int a = power ( 2 , i ) ; int b = power ( 5 , j ) ; if ( a + b == N ) { System . out . print ( i + " ▁ " + j ) ; return ; } } } System . out . println ( " - 1" ) ; } public static void main ( String args [ ] ) { int N = 129 ; findValX_Y ( N ) ; } }


"Find any K distinct odd integers such that their sum is equal to N | Java implementation to find k odd integers such that their sum is N ; Function to find K odd integers such that their sum is N ; Condition to check if there exist such K integers ; Loop to find first K - 1 distinct odd integers ; Final Kth odd number ; Driver code"
How can the above be solved in Java?

class GFG { static void oddIntegers ( int n , int k ) { if ( n % 2 != k % 2 ) { System . out . println ( " - 1" ) ; return ; } int sum = 0 ; int i = 1 ; int j = 1 ; while ( j < k ) { sum = sum + i ; System . out . print ( i + " ▁ " ) ; i = i + 2 ; j ++ ; } int finalOdd = n - sum ; System . out . println ( finalOdd ) ; } public static void main ( String [ ] args ) { int n = 10 ; int k = 2 ; oddIntegers ( n , k ) ; } }


"Find any one of the multiple repeating elements in read only array | Java program to find one of the repeating elements in a read only array ; Function to find one of the repeating elements ; Size of blocks except the last block is sq ; Number of blocks to incorporate 1 to n values blocks are numbered from 0 to range - 1 ( both included ) ; Count array maintains the count for all blocks ; Traversing the read only array and updating count ; arr [ i ] belongs to block number ( arr [ i ] - 1 ) / sq i is considered to start from 0 ; The selected_block is set to last block by default . Rest of the blocks are checked ; after finding block with size > sq method of hashing is used to find the element repeating in this block ; checks if the element belongs to the selected_block ; repeating element found ; return - 1 if no repeating element exists ; Driver code ; read only array , not to be modified ; array of size 6 ( n + 1 ) having elements between 1 and 5"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . put ( arr [ i ] , 1 ) ; if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; } } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; System . out . println ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }


"Find any pair with given GCD and LCM | Java program to print any pair with a given gcd G and lcm L ; Function to print the pairs ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void printPair ( int g , int l ) { System . out . print ( g + " ▁ " + l ) ; } public static void main ( String [ ] args ) { int g = 3 , l = 12 ; printPair ( g , l ) ; } }


"Find any permutation of Binary String of given size not present in Array | Java Program for the above approach ; Function to find a binary string of N bits that does not occur in the givrn array arr [ ] ; Stores the resultant string ; Loop to iterate over all the given strings in a diagonal order ; Append the complement of element at current index into ans ; Return Answer ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static String findString ( String arr [ ] , int N ) { String ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] . charAt ( i ) == '0' ? '1' : '0' ; } return ans ; } public static void main ( String [ ] args ) { String arr [ ] = { "111" , "011" , "001" } ; int N = arr . length ; System . out . println ( findString ( arr , N ) ) ; } }


"Find any possible two coordinates of Rectangle whose two coordinates are given | Java program for above approach ; Function to find the remaining two rectangle coordinates ; Pairs to store the position of given two coordinates of the rectangle . ; Pairs to store the remaining two coordinates of the rectangle . ; Traverse through matrix and find pairs p1 and p2 ; First Case ; Second Case ; Third Case ; Print the matrix ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . awt . * ; import java . util . * ; class GFG { static class pair < T , V > { T first ; V second ; } static void Create_Rectangle ( ArrayList < String > arr , int n ) { pair < Integer , Integer > p1 = new pair < > ( ) ; p1 . first = - 1 ; p1 . second = - 1 ; pair < Integer , Integer > p2 = new pair < > ( ) ; p2 . first = - 1 ; p2 . second = - 1 ; pair < Integer , Integer > p3 = new pair < > ( ) ; pair < Integer , Integer > p4 = new pair < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr . get ( i ) . charAt ( j ) == '1' ) if ( p1 . first == - 1 ) { p1 . first = i ; p1 . second = j ; } else { p2 . first = i ; p2 . second = j ; } } } p3 = p1 ; p4 = p2 ; if ( p1 . first . intValue ( ) == ( p2 . first ) . intValue ( ) ) { p3 . first = ( p1 . first + 1 ) % n ; p4 . first = ( p2 . first + 1 ) % n ; } else if ( p1 . second . intValue ( ) == ( p2 . second ) . intValue ( ) ) { p3 . second = ( p1 . second + 1 ) % n ; p4 . second = ( p2 . second + 1 ) % n ; } else { int temp = p3 . first ; p3 . first = p4 . first ; p4 . first = temp ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == p3 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p3 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else if ( i == p4 . first ) { for ( int j = 0 ; j < n ; j ++ ) { if ( j == p4 . second ) System . out . print ( '1' ) ; else System . out . print ( arr . get ( i ) . charAt ( j ) ) ; } } else System . out . print ( arr . get ( i ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; ArrayList < String > arr = new ArrayList < > ( ) ; arr . add ( "0010" ) ; arr . add ( "0000" ) ; arr . add ( "1000" ) ; arr . add ( "0000" ) ; Create_Rectangle ( arr , n ) ; } }


"Find any two pairs ( a , b ) and ( c , d ) such that a d | Java implementation to sort the array of points by its distance from the given point ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; If no such pair is found ; 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 void findPair ( pair arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int a = arr [ i ] . first , b = arr [ i ] . second ; for ( int j = i + 1 ; j < N ; j ++ ) { int c = arr [ j ] . first , d = arr [ j ] . second ; if ( a < c && b > d ) { System . out . println ( " ( " + a + " ▁ " + b + " ) , ▁ ( " + c + " ▁ " + d + " ) " ) ; return ; } } } System . out . println ( " NO ▁ SUCH ▁ PAIR ▁ EXIST " ) ; } public static void main ( String [ ] args ) { pair arr [ ] = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findPair ( arr , 5 ) ; } }


"Find any two pairs ( a , b ) and ( c , d ) such that a d | Java program for the above approach ; Function to find two pairs ( a , b ) and ( c , d ) such that a < c and b > d ; Sort the array in increasing order of first element of pairs ; Traverse the array ; If no such pair found ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static class pair implements Comparable < pair > { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int compareTo ( pair p ) { return this . first - p . first ; } } static void findpair ( pair [ ] arr , int N ) { Arrays . sort ( arr ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { System . out . print ( " ( " + arr [ i - 1 ] . first + " ▁ " + b + " ) , ▁ ( " + arr [ i ] . first + " ▁ " + d + " ) " ) ; return ; } } System . out . print ( "NO SUCH PAIR EXIST
"); } public static void main ( String [ ] args ) { pair arr [ ] = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findpair ( arr , 5 ) ; } }


"Find area of parallelogram if vectors of two adjacent sides are given | Java code to calculate area of parallelogram if vectors of 2 adjacent sides are given ; Function to calculate area of parallelogram ; Driver code"
How can the above be solved in Java?

public class GFG { static float area ( float x1 , float y1 , float z1 , float x2 , float y2 , float z2 ) { float area = ( float ) Math . sqrt ( Math . pow ( ( y1 * z2 - y2 * z1 ) , 2 ) + Math . pow ( ( x1 * z2 - x2 * z1 ) , 2 ) + Math . pow ( ( x1 * y2 - x2 * y1 ) , 2 ) ) ; return area ; } public static void main ( String args [ ] ) { float x1 = 3 ; float y1 = 1 ; float z1 = - 2 ; float x2 = 1 ; float y2 = - 3 ; float z2 = 4 ; float a = area ( x1 , y1 , z1 , x2 , y2 , z2 ) ; System . out . println ( " Area ▁ = ▁ " + a ) ; } }


"Find area of the larger circle when radius of the smaller circle and difference in the area is given | Java implementation of the approach ; Function to return the area of the bigger circle ; Find the radius of the bigger circle ; Calculate the area of the bigger circle ; Driver code"
How can the above be solved in Java?

class GFG { static double PI = 3.14 ; static double find_area ( int r , int d ) { double R = d / PI ; R += Math . pow ( r , 2 ) ; R = Math . sqrt ( R ) ; double area = PI * Math . pow ( R , 2 ) ; return area ; } public static void main ( String [ ] args ) { int r = 4 , d = 5 ; System . out . println ( find_area ( r , d ) ) ; } }


"Find area of triangle if two vectors of two adjacent sides are given | Java program to calculate area of triangle if vectors of 2 adjacent sides are given ; function to calculate area of triangle ; Driver program"
How can the above be solved in Java?

import java . util . * ; class solution { static float area ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { double a = Math . pow ( ( y1 * z2 - y2 * z1 ) , 2 ) + Math . pow ( ( x1 * z2 - x2 * z1 ) , 2 ) + Math . pow ( ( x1 * y2 - x2 * y1 ) , 2 ) ; float area = ( float ) Math . sqrt ( a ) ; area = area / 2 ; return area ; } public static void main ( String arr [ ] ) { int x1 = - 2 ; int y1 = 0 ; int z1 = - 5 ; int x2 = 1 ; int y2 = - 2 ; int z2 = - 1 ; float a = area ( x1 , y1 , z1 , x2 , y2 , z2 ) ; System . out . println ( " Area = ▁ " + a ) ; } }


"Find array elements with frequencies in range [ l , r ] | Java program to find the elements whose frequency lies in the range [ l , r ] ; Hash map which will store the frequency of the elements of the array . ; Increment the frequency of the element by 1. ; Print the element whose frequency lies in the range [ l , r ] ; Driver code"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . Map ; public class GFG { static void findElements ( int arr [ ] , int n , int l , int r ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int a = 0 ; if ( mp . get ( arr [ i ] ) == null ) { a = 1 ; } else { a = mp . get ( arr [ i ] ) + 1 ; } mp . put ( arr [ i ] , a ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( l <= mp . get ( arr [ i ] ) && ( mp . get ( arr [ i ] ) <= r ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = arr . length ; int l = 2 , r = 3 ; findElements ( arr , n , l , r ) ; } }


"Find array elements with rightmost set bit at the position of the rightmost set bit in K | Java program for the above approach ; Function to find the mask for finding rightmost set bit in K ; Function to find all array elements with rightmost set bit same as that in K ; Stores mask of K ; Store position of rightmost set bit ; Traverse the array ; Check if rightmost set bit of current array element is same as position of rightmost set bit in K ; Driver Code ; Input ; Function call to find the elements having same rightmost set bit as of K"
How can the above be solved in Java?

import java . io . * ; class GFG { static int findMask ( int K ) { int mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } public static void sameRightSetBitPos ( int [ ] arr , int N , int K ) { int mask = findMask ( K ) ; final int pos = ( K & mask ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 6 , 7 , 9 , 12 , 15 } ; int N = arr . length ; int K = 7 ; sameRightSetBitPos ( arr , N , K ) ; } }


"Find array sum after incrementing by K adjacent elements of every positive element M times | Java program for the above approach ; Function to find the nearest non - zero element in the left direction ; Stores the index of the first element greater than 0 from the right side ; Traverse the array in the range [ 1 , N ] ; Check arr [ i ] is greater than 0 ; Update the value of L ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of L ; Update the value of steps [ i ] ; Function to find the nearest non - zero element in the right direction ; Stores the index of the first element greater than 0 from the left side ; Traverse the array from the left side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Traverse the array from the right side ; Check arr [ i ] is greater than 0 ; Update the value of R ; Update the value of steps [ i ] ; Function to find the sum of the array after the given operation M times ; Stores the distance of the nearest non zero element . ; Stores sum of the initial array arr [ ] ; Traverse the array from the left side ; Update the value of sum ; Print the total sum of the array ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void nearestLeft ( int arr [ ] , int N , int [ ] steps ) { int L = - N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } static void nearestRight ( int arr [ ] , int N , int [ ] steps ) { int R = 2 * N ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = Math . min ( steps [ i ] , R - i ) ; } } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; return sum ; } static int findSum ( int arr [ ] , int N , int M , int K ) { int [ ] steps = new int [ N ] ; int sum = accumulate ( arr , 0 , N ) ; if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( int i = 0 ; i < N ; i ++ ) sum += 2 * K * Math . max ( 0 , M - steps [ i ] ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 1 , 0 , 0 } ; int N = arr . length ; int M = 2 ; int K = 1 ; System . out . print ( findSum ( arr , N , M , K ) ) ; } }


"Find array using different XORs of elements in groups of size 4 | Java implementation of the approach ; Utility function to print the contents of the array ; Function to find the required array ; Print the array ; Driver code"
How can the above be solved in Java?

class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void main ( String args [ ] ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . length ; findArray ( q , n ) ; } }


"Find array whose elements are XOR of adjacent elements in given array | Java implementation of the above approach ; Function to reconstruct the array arr [ ] with xor of adjacent elements ; Iterate through each element ; Store the xor of current and next element in arr [ i ] ; Function to print the array ; Driver Code ; Inputs ; Length of the array given ; Function call to reconstruct the arr [ ] ; Function call to print arr [ ]"
How can the above be solved in Java?

import java . io . * ; class GFG { static int [ ] game_with_number ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 11 , 1 , 2 , 3 } ; int n = arr . length ; int [ ] new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; } }


"Find array with k number of merge sort calls | Java program to find an array that can be sorted with k merge sort calls . ; We make two recursive calls , so reduce k by 2. ; Create an array with values in [ 1 , n ] ; calling unsort function ; Driver code"
How can the above be solved in Java?

class GFG { static void unsort ( int l , int r , int a [ ] , int k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { System . out . print ( " NO ▁ SOLUTION " ) ; return ; } int a [ ] = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }


"Find bitonic point in given bitonic sequence | Java program to find bitonic point in a bitonic array . ; Function to find bitonic point using binary search ; base condition to check if arr [ mid ] is bitonic point or not ; We assume that sequence is bitonic . We go to right subarray if middle point is part of increasing subsequence . Else we go to left subarray . ; Driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int left , int right ) { if ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr [ mid - 1 ] < arr [ mid ] && arr [ mid ] > arr [ mid + 1 ] ) return mid ; if ( arr [ mid ] < arr [ mid + 1 ] ) return binarySearch ( arr , mid + 1 , right ) ; else return binarySearch ( arr , left , mid - 1 ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 8 , 11 , 9 , 5 , 2 , 1 } ; int n = arr . length ; int index = binarySearch ( arr , 1 , n - 2 ) ; if ( index != - 1 ) System . out . println ( arr [ index ] ) ; } }


"Find bitwise AND ( & ) of all possible sub | Java program to find of all the sub - arrays ; function to return AND of sub - arrays ; Driver code ; size of the array ; print and of all subarrays"
How can the above be solved in Java?

public class GFG { static int AND ( int a [ ] , int n ) { int ans = a [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) ans &= a [ i ] ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; System . out . println ( AND ( a , n ) ) ; } }


"Find bitwise OR of all possible sub | Java program to find OR of all the sub - arrays ; function to return OR of sub - arrays ; Driver Code ; print OR of all subarrays"
How can the above be solved in Java?

class GFG { static int OR ( int a [ ] , int n ) { int ans = a [ 0 ] ; int i ; for ( i = 1 ; i < n ; i ++ ) { ans |= a [ i ] ; } return ans ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 4 , 6 } ; int n = a . length ; System . out . println ( OR ( a , n ) ) ; } }


"Find ceil of a / b without using ceil ( ) function | Java program to find ceil ( a / b ) without using ceil ( ) function ; Driver Code ; taking input 1 ; example of perfect division taking input 2"
How can the above be solved in Java?

import java . io . * ; class GFG { public static void main ( String args [ ] ) { int a = 4 ; int b = 3 , val = 0 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; System . out . println ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; if ( ( a % b ) != 0 ) val = ( a / b ) + ( a % b ) ; else val = ( a / b ) ; System . out . println ( " The ▁ ceiling ▁ " + " value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }


class GFG { public static void main ( String args [ ] ) { int a = 4 ; int b = 3 ; int val = ( a + b - 1 ) / b ; System . out . println ( " The ▁ ceiling ▁ value ▁ of ▁ 4/3 ▁ is ▁ " + val ) ; a = 6 ; b = 3 ; val = ( a + b - 1 ) / b ; System . out . println ( " The ▁ ceiling ▁ value ▁ of ▁ 6/3 ▁ is ▁ " + val ) ; } }


"Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Java program to solve the above approach ; initializing first and second variable as to store how many string ' s ' and string ' t ' will be appended ; storing tmp length ; if length of string tmp is greater than k , then we have reached our destination string now we can return character at index k ; appending s to tmp , f times ; appending t to tmp , s times ; extracting output character ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static char KthCharacter ( String s , String t , long k ) { long f = 1 ; long ss = 2 ; String tmp = " " ; int len = tmp . length ( ) ; while ( len < k ) { long tf = f ; long ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ( ) ; } char output = tmp . charAt ( ( int ) k - 1 ) ; return output ; } public static void main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; System . out . println ( ans ) ; } }


"Find character at Kth index by appending S1 ( M ) times and S2 ( M + 1 ) times | Java program to solve the above approach ; storing length ; variables for temporary strings of s and t ; final output variable ; if k is greater than even after adding string ' s ' ' first ' times ( let ' s ▁ call ▁ it ▁ x ) ▁ we ' ll subtract x from k ; incrementing first by 2 , as said in problem statement ; if k is greater than even after adding string ' t ' ' second ' times ( let ' s ▁ call ▁ it ▁ y ) ▁ we ' ll subtract y from k ; incrementing second by two as said in problem statement ; if k is smaller than y , then the character will be at position k % m . ; returning character at k index ; if k is smaller than x , then the character is at position k % n ; returning character at k index ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static char KthCharacter ( String s , String t , long k ) { long n = s . length ( ) ; long m = t . length ( ) ; long first = 1 ; long second = 2 ; char output = ' ? ' ; while ( k > 0 ) { if ( k > first * n ) { long x = first * n ; k = k - x ; first = first + 2 ; if ( k > second * m ) { long y = second * m ; k = k - y ; second = second + 2 ; } else { long ind = k % m ; ind -- ; if ( ind < 0 ) ind = m - 1 ; output = t . charAt ( ( int ) ind ) ; break ; } } else { long ind = k % n ; ind -- ; if ( ind < 0 ) ind = n - 1 ; output = s . charAt ( ( int ) ind ) ; break ; } } return output ; } public static void main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; System . out . println ( ans ) ; } }


"Find closest integer with the same weight | Java implementation of the approach ; Function to return the number closest to x which has equal number of set bits as x ; Loop for each bit in x and compare with the next bit ; Driver code"
How can the above be solved in Java?

class GFG { static int NumUnsignBits = 64 ; static long findNum ( long x ) { for ( int i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } return Long . MIN_VALUE ; } public static void main ( String [ ] args ) { int n = 92 ; System . out . println ( findNum ( n ) ) ; } }


"Find closest number in array | Java program to find element closet to given target . ; Returns element closest to target in arr [ ] ; Corner cases ; Doing binary search ; If target is less than array element , then search in left ; If target is greater than previous to mid , return closest of two ; Repeat for left half ; If target is greater than mid ; update i ; Only single element left after search ; Method to compare which one is the more close We find the closest by taking the difference between the target and both values . It assumes that val2 is greater than val1 and target lies between these two . ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class FindClosestNumber { public static int findClosest ( int arr [ ] , int target ) { int n = arr . length ; if ( target <= arr [ 0 ] ) return arr [ 0 ] ; if ( target >= arr [ n - 1 ] ) return arr [ n - 1 ] ; int i = 0 , j = n , mid = 0 ; while ( i < j ) { mid = ( i + j ) / 2 ; if ( arr [ mid ] == target ) return arr [ mid ] ; if ( target < arr [ mid ] ) { if ( mid > 0 && target > arr [ mid - 1 ] ) return getClosest ( arr [ mid - 1 ] , arr [ mid ] , target ) ; j = mid ; } else { if ( mid < n - 1 && target < arr [ mid + 1 ] ) return getClosest ( arr [ mid ] , arr [ mid + 1 ] , target ) ; i = mid + 1 ; } } return arr [ mid ] ; } public static int getClosest ( int val1 , int val2 , int target ) { if ( target - val1 >= val2 - target ) return val2 ; else return val1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 , 6 , 8 , 9 } ; int target = 11 ; System . out . println ( findClosest ( arr , target ) ) ; } }


"Find combined mean and variance of two series | Java program to find combined mean and variance of two series . ; Function to find mean of series . ; Function to find the standard deviation of series . ; Function to find combined variance of two different series . ; mean1 and mean2 are the mean of two arrays . ; sd1 and sd2 are the standard deviation of two array . ; combinedMean is variable to store the combined mean of both array . ; d1_square and d2_square are the combined mean deviation . ; combinedVar is variable to store combined variance of both array . ; Driver function . ; Function call to combined mean ."
How can the above be solved in Java?

import java . io . * ; class GFG { static float mean ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; float mean = ( float ) sum / n ; return mean ; } static float sd ( int arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; float sdd = sum / n ; return sdd ; } static float combinedVariance ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { float mean1 = mean ( arr1 , n ) ; float mean2 = mean ( arr2 , m ) ; System . out . print ( " Mean1 : ▁ " + mean1 + " ▁ " ) ; System . out . println ( " Mean2 : ▁ " + mean2 ) ; float sd1 = sd ( arr1 , n ) ; float sd2 = sd ( arr2 , m ) ; System . out . print ( " StandardDeviation1 : ▁ " + sd1 + " ▁ " ) ; System . out . println ( " StandardDeviation2 : ▁ " + sd2 + " ▁ " ) ; float combinedMean = ( float ) ( n * mean1 + m * mean2 ) / ( n + m ) ; System . out . println ( " Combined ▁ Mean : ▁ " + combinedMean + " ▁ " ) ; float d1_square = ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ; float d2_square = ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ; System . out . print ( " d1 ▁ square : ▁ " + d1_square + " ▁ " ) ; System . out . println ( " d2 _ square : ▁ " + d2_square ) ; float combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; return combinedVar ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 23 , 45 , 34 , 78 , 12 , 76 , 34 } ; int arr2 [ ] = { 65 , 67 , 34 , 23 , 45 } ; int n = arr1 . length ; int m = arr2 . length ; System . out . println ( " Combined ▁ Variance : ▁ " + combinedVariance ( arr1 , arr2 , n , m ) ) ; } }


"Find common elements in three sorted arrays | Java program to find common elements in three arrays ; This function prints common elements in ar1 ; Initialize starting indexes for ar1 [ ] , ar2 [ ] and ar3 [ ] ; Iterate through three arrays while all arrays have elements ; If x = y and y = z , print any of them and move ahead in all arrays ; x < y ; y < z ; We reach here when x > y and z < y , i . e . , z is smallest ; Driver code to test above"
How can the above be solved in Java?

class FindCommon { void findCommon ( int ar1 [ ] , int ar2 [ ] , int ar3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < ar1 . length && j < ar2 . length && k < ar3 . length ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { System . out . print ( ar1 [ i ] + " ▁ " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } public static void main ( String args [ ] ) { FindCommon ob = new FindCommon ( ) ; int ar1 [ ] = { 1 , 5 , 10 , 20 , 40 , 80 } ; int ar2 [ ] = { 6 , 7 , 20 , 80 , 100 } ; int ar3 [ ] = { 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 } ; System . out . print ( " Common ▁ elements ▁ are ▁ " ) ; ob . findCommon ( ar1 , ar2 , ar3 ) ; } }


"Find consecutive 1 s of length >= n in binary representation of a number | Java implementation of above approach ; Function to count the number of leading zeros ; Function to find the string of n consecutive 1 's ; Initialize position to return . ; Skip leading 0 's ; Set position after leading 0 's ; Count first group of 1 's. ; If length of consecutive 1 's  is greater than or equal to n ; Not enough 1 's  skip over to next group ; Update the position ; if no string is found ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countLeadingZeros ( int x ) { int y ; int n ; n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } static int FindStringof1s ( int x , int n ) { int k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return - 1 ; } public static void main ( String [ ] args ) { int x = 35 ; int n = 2 ; System . out . println ( FindStringof1s ( x , n ) ) ; } }