"Find Array obtained after adding terms of AP for Q queries | Java program for the above approach ; Function to find array after performing the given query to the array elements ; Traverse the given query ; Traverse the given array ; Update the value of A [ i ] ; Update the value of curr ; Print the array elements ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { public static void addAP ( int A [ ] , int Q , int [ ] [ ] operations ) { for ( int j = 0 ; j < 2 ; ++ j ) { int L = operations [ j ] [ 0 ] , R = operations [ j ] [ 1 ] , a = operations [ j ] [ 2 ] , d = operations [ j ] [ 3 ] ; int curr = a ; for ( int i = L - 1 ; i < R ; i ++ ) { A [ i ] += curr ; curr += d ; } } for ( int i = 0 ; i < 4 ; ++ i ) System . out . print ( A [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 5 , 4 , 2 , 8 } ; int Q = 2 ; int query [ ] [ ] = { { 1 , 2 , 1 , 3 } , { 1 , 4 , 4 , 1 } } ; addAP ( A , Q , query ) ; } }


"Find Binary permutations of given size not present in the Array | Java program for the above approach ; Function to find a Binary String of same length other than the Strings present in the array ; Map all the Strings present in the array ; Find all the subString that can be made ; If num already exists then increase counter ; If not found print ; If all the subStrings are present then print - 1 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findMissingBinaryString ( String [ ] nums , int N ) { HashSet < String > s = new HashSet < String > ( ) ; int counter = 0 ; for ( String str : nums ) { s . add ( str ) ; } int total = ( int ) Math . pow ( 2 , N ) ; String ans = " " ; for ( int i = 0 ; i < total ; i ++ ) { String num = " " ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( ( i & ( 1 << j ) ) > 0 ) { num += '1' ; } else { num += '0' ; } } if ( s . contains ( num ) ) { counter ++ ; continue ; } else { System . out . print ( num + " , ▁ " ) ; } } if ( counter == total ) { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int N = 3 ; String [ ] arr = { "101" , "111" , "001" , "011" , "100" , "110" } ; findMissingBinaryString ( arr , N ) ; } }


"Find Binary string by converting all 01 or 10 to 11 after M iterations | Java program for the above approach . ; Function to find the modified binary string after M iterations ; Set the value of M to the minimum of N or M . ; Declaration of current string state ; Loop over M iterations ; Set the current state as null before each iteration ; Check if this zero has exactly one 1 as neighbour ; Flip the zero ; If there is no change , then no need for further iterations . ; Set the current state as the new previous state ; Driver Code ; Given String ; Number of Iterations ; Function Call"
How can the above be solved in Java?

import java . io . * ; import static java . lang . Math . min ; import java . lang . * ; class GFG { public static void findString ( String str , int M ) { int N = str . length ( ) ; M = Math . min ( M , N ) ; String s1 = " " ; while ( M != 0 ) { s1 = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '0' ) { if ( ( str . charAt ( i ) == '1' && str . charAt ( i ) != '1' ) || ( str . charAt ( i ) == '1' && str . charAt ( i ) == '1' ) ) s1 += '1' ; else s1 += '0' ; } else s1 += '1' ; } if ( str == s1 ) break ; str = s1 ; M -- ; } System . out . print ( s1 ) ; } public static void main ( String [ ] args ) { String str = "0110100" ; int M = 3 ; findString ( str , M ) ; } }


"Find Count of Single Valued Subtrees | Class containing left and right child of current node and key value ; This function increments count by number of single valued subtrees under root . It returns true if subtree under root is Singly , else false . ; Return false to indicate NULL ; Recursively count in left and right subtrees also ; If any of the subtrees is not singly , then this cannot be singly . ; If left subtree is singly and non - empty , but data doesn 't match ; Same for right subtree ; If none of the above conditions is true , then tree rooted under root is single valued , increment count and return true . ; This function mainly calls countSingleRec ( ) after initializing count as 0 ; Recursive function to count ; Driver program to test above functions ; Let us construct the below tree 5 / \ 4 5 / \ \ 4 4 5"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class Count { int count = 0 ; } class BinaryTree { Node root ; Count ct = new Count ( ) ; boolean countSingleRec ( Node node , Count c ) { if ( node == null ) return true ; boolean left = countSingleRec ( node . left , c ) ; boolean right = countSingleRec ( node . right , c ) ; if ( left == false right == false ) return false ; if ( node . left != null && node . data != node . left . data ) return false ; if ( node . right != null && node . data != node . right . data ) return false ; c . count ++ ; return true ; } int countSingle ( ) { return countSingle ( root ) ; } int countSingle ( Node node ) { countSingleRec ( node , ct ) ; return ct . count ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 4 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . right = new Node ( 5 ) ; System . out . println ( " The ▁ count ▁ of ▁ single ▁ valued ▁ sub ▁ trees ▁ is ▁ : ▁ " + tree . countSingle ( ) ) ; } }


"Find Cube root of a number using Log function | Java program to Find Cube root of a number using Logarithm ; Function to find the cube root ; Calculate the cube root ; Return the final answer ; Driver code"
How can the above be solved in Java?

class GFG { static double cubeRoot ( double n ) { double ans = Math . pow ( 3 , ( ( 1.0 / 3 ) * ( Math . log ( n ) / Math . log ( 3 ) ) ) ) ; return ans ; } public static void main ( String [ ] args ) { double N = 8 ; System . out . printf ( " % .2f " , cubeRoot ( N ) ) ; } }


"Find Duplicates of array using bit array | Java program to print all Duplicates in array ; A class to represent array of bits using array of integers ; Constructor ; Divide by 32. To store n bits , we need n / 32 + 1 integers ( Assuming int is stored using 32 bits ) ; Get value of a bit at given position ; Divide by 32 to find position of integer . ; Now find bit number in arr [ index ] ; Find value of given bit number in arr [ index ] ; Sets a bit at given position ; Find index of bit position ; Set bit number in arr [ index ] ; Main function to print all Duplicates ; create a bit with 32000 bits ; Traverse array elements ; Index in bit array ; If num is already present in bit array ; Else insert num ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class BitArray { int [ ] arr ; public BitArray ( int n ) { arr = new int [ ( n >> 5 ) + 1 ] ; } boolean get ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; return ( arr [ index ] & ( 1 << bitNo ) ) != 0 ; } void set ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; arr [ index ] |= ( 1 << bitNo ) ; } static void checkDuplicates ( int [ ] arr ) { BitArray ba = new BitArray ( 320000 ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int num = arr [ i ] - 1 ; if ( ba . get ( num ) ) System . out . print ( num + " ▁ " ) ; else ba . set ( num ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int [ ] arr = { 1 , 5 , 1 , 10 , 12 , 10 } ; checkDuplicates ( arr ) ; } }


"Find Excel column name from a given column number |  ; Step 1 : Converting to number assuming 0 in number system ; Step 2 : Getting rid of 0 , as 0 is not part of number system ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printString ( int n ) { int [ ] arr = new int [ 10000 ] ; int i = 0 ; while ( n > 0 ) { arr [ i ] = n % 26 ; n = n / 26 ; i ++ ; } for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( arr [ j ] <= 0 ) { arr [ j ] += 26 ; arr [ j + 1 ] = arr [ j + 1 ] - 1 ; } } for ( int j = i ; j >= 0 ; j -- ) { if ( arr [ j ] > 0 ) System . out . print ( ( char ) ( ' A ' + arr [ j ] - 1 ) ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }


"Find Excel column name from a given column number | Java program to find Excel column name from a given column number ; Function to print Excel column name for a given column number ; To store result ( Excel column name ) ; Find remainder ; If remainder is 0 , then a ' Z ' must be there in output ; If remainder is non - zero ; Reverse the string and print result ; Driver program to test above function"
How can the above be solved in Java?

public class ExcelColumnTitle { private static void printString ( int columnNumber ) { StringBuilder columnName = new StringBuilder ( ) ; while ( columnNumber > 0 ) { int rem = columnNumber % 26 ; if ( rem == 0 ) { columnName . append ( " Z " ) ; columnNumber = ( columnNumber / 26 ) - 1 ; } else { columnName . append ( ( char ) ( ( rem - 1 ) + ' A ' ) ) ; columnNumber = columnNumber / 26 ; } } System . out . println ( columnName . reverse ( ) ) ; } public static void main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }


"Find Excel column number from column title | Java program to return title to result of excel sheet . ; Returns resul when we pass title . ; This process is similar to binary - to - decimal conversion ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static int titleToNumber ( String s ) { int result = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { result *= 26 ; result += s . charAt ( i ) - ' A ' + 1 ; } return result ; } public static void main ( String [ ] args ) { System . out . print ( titleToNumber ( " CDA " ) ) ; } }


"Find First element in AP which is multiple of given prime | Java Program to Find First element in AP which is multiple of given prime ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is more than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; function to find nearest element in common ; base conditions ; Driver code ; module both A and D ; function call"
How can the above be solved in Java?

class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int NearestElement ( int A , int D , int P ) { if ( A == 0 ) return 0 ; else if ( D == 0 ) return - 1 ; else { int X = power ( D , P - 2 , P ) ; return ( X * ( P - A ) ) % P ; } } public static void main ( String args [ ] ) { int A = 4 , D = 9 , P = 11 ; A %= P ; D %= P ; System . out . println ( NearestElement ( A , D , P ) ) ; } }


"Find GCD between the sum of two given integers raised to the power of N and their difference | Java program for the above approach ; Function to find the value of ( a ^ n ) % d ; Stores the value of ( a ^ n ) % d ; Calculate the value of ( a ^ n ) % d ; If n is odd ; Update res ; Update a ; Update n ; Function to find the GCD of ( p ^ n + q ^ n ) and p - q mod d ; If p and q are equal ; Stores GCD of ( p ^ n + q ^ n ) and ( p - q ) mod d ; Stores the value of ( p - q ) ; Stores square root of num ; Find the divisors of num . ; If i divides num ; Stores power of ( p ^ n ) mod i ; Stores power of ( q ^ n ) mod i ; Stores power of ( p ^ n + q ^ n ) mod i ; If ( p ^ n + q ^ n ) is divisible by i ; Calculate the largest divisor . ; If i divides num , ( num / i ) also divides num . Hence , calculate temp . ; If ( p ^ n + q ^ n ) is divisible by ( num / i ) ; Calculate the largest divisor . ; Driver code ; Given p , q and n ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int a , int n , int d ) { int res = 1 ; while ( n != 0 ) { if ( ( n % 2 ) != 0 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } static int gcd ( int p , int q , int n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } int candidate = 1 ; int num = p - q ; int sq = ( int ) Math . sqrt ( num ) ; for ( int i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { int X = power ( p , n , i ) ; int Y = power ( q , n , i ) ; int temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = Math . max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = Math . max ( candidate , num / i ) ; } } } return candidate % mod ; } public static void main ( String [ ] args ) { int p , q , n ; p = 10 ; q = 6 ; n = 5 ; System . out . println ( gcd ( p , q , n ) ) ; } }


"Find GCD of each subtree of a given node in an N | Java program to find GCD of each subtree for a given node by Q queries ; Maximum Number of nodes ; Tree represented as adjacency list ; For storing value associates with node ; For storing GCD of every subarray ; Number of nodes ; Function to find GCD of two numbers using Euclidean algo ; If b == 0 then simply return a ; DFS function to traverse the tree ; Initializing answer with GCD of this node . ; Iterate over each child of current node ; Skipping the parent ; Call DFS for each child ; Taking GCD of the answer of the child to find node 's GCD ; Calling DFS from the root ( 1 ) for precomputing answers ; Function to find and print GCD for Q queries ; Doing preprocessing ; iterate over each given query ; Driver code ; Tree : 1 ( 2 ) / \ 2 ( 3 ) 3 ( 4 ) / \ 4 ( 8 ) 5 ( 16 ) ; Making a undirected tree ; Values associated with nodes ; Function call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int N = ( int ) ( 1e5 + 5 ) ; static Vector < Integer > [ ] v = new Vector [ N ] ; static int [ ] val = new int [ N ] ; static int [ ] answer = new int [ N ] ; static int n ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void DFS ( int node , int parent ) { answer [ node ] = val [ node ] ; for ( int child : v [ node ] ) { if ( child == parent ) continue ; DFS ( child , node ) ; answer [ node ] = gcd ( answer [ node ] , answer [ child ] ) ; } } static void preprocess ( ) { DFS ( 1 , - 1 ) ; } static void findGCD ( int queries [ ] , int q ) { preprocess ( ) ; for ( int i = 0 ; i < q ; i ++ ) { int GCD = answer [ queries [ i ] ] ; System . out . print ( " For ▁ subtree ▁ of ▁ " + queries [ i ] + " , ▁ GCD ▁ = ▁ " + GCD + "
"); } } public static void main ( String [ ] args ) { n = 5 ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; v [ 1 ] . add ( 2 ) ; v [ 2 ] . add ( 1 ) ; v [ 1 ] . add ( 3 ) ; v [ 3 ] . add ( 1 ) ; v [ 3 ] . add ( 4 ) ; v [ 4 ] . add ( 3 ) ; v [ 3 ] . add ( 5 ) ; v [ 5 ] . add ( 3 ) ; val [ 1 ] = 2 ; val [ 2 ] = 3 ; val [ 3 ] = 4 ; val [ 4 ] = 8 ; val [ 5 ] = 16 ; int queries [ ] = { 2 , 3 , 1 } ; int q = queries . length ; findGCD ( queries , q ) ; } }


"Find GCD of factorial of elements of given array | Java implementation of the above approach ; Implementation of factorial function ; Function to find GCD of factorial of elements from array ; find the minimum element of array ; return the factorial of minimum element ; Driver Code"
How can the above be solved in Java?

class GFG { static int factorial ( int n ) { return ( n == 1 n == 0 ) ? 1 : factorial ( n - 1 ) * n ; } static int gcdOfFactorial ( int [ ] arr , int n ) { int minm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) minm = minm > arr [ i ] ? arr [ i ] : minm ; return factorial ( minm ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 12 , 122 , 34 , 15 } ; int n = arr . length ; System . out . println ( gcdOfFactorial ( arr , n ) ) ; } }


"Find H | Java implementation of the above approach ; Function to find the H - index ; Set the range for binary search ; Check if current citations is possible ; Check to the right of mid ; Update h - index ; Since current value is not possible , check to the left of mid ; Print the h - index ; Driver Code ; citations"
How can the above be solved in Java?

import java . io . * ; class GFG { static int hIndex ( int [ ] citations , int n ) { int hindex = 0 ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( citations [ mid ] >= ( mid + 1 ) ) { low = mid + 1 ; hindex = mid + 1 ; } else { high = mid - 1 ; } } System . out . println ( hindex ) ; return hindex ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] citations = { 5 , 3 , 3 , 2 , 2 } ; hIndex ( citations , n ) ; } }


"Find HCF of two numbers without using recursion or Euclidean algorithm | Java implementation of the approach ; Function to return the HCF of x and y ; Minimum of the two numbers ; If both the numbers are divisible by the minimum of these two then the HCF is equal to the minimum ; Highest number between 2 and minimum / 2 which can divide both the numbers is the required HCF ; If both the numbers are divisible by i ; 1 divides every number ; Driver code"
How can the above be solved in Java?

class GFG { static int getHCF ( int x , int y ) { int minimum = Math . min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( int i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } public static void main ( String [ ] args ) { int x = 16 , y = 32 ; System . out . println ( getHCF ( x , y ) ) ; } }


"Find Harmonic mean using Arithmetic mean and Geometric mean | Java implementation of compution of arithmetic mean , geometric mean and harmonic mean ; Function to calculate arithmetic mean , geometric mean and harmonic mean ; Driver function"
How can the above be solved in Java?

import java . io . * ; class GeeksforGeeks { static double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = Math . sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } public static void main ( String args [ ] ) { int a = 5 , b = 15 ; double HM = compute ( a , b ) ; String str = " " ; str = str + HM ; System . out . print ( " Harmonic ▁ Mean ▁ between ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + str . substring ( 0 , 5 ) ) ; } }


"Find Height of Binary Tree represented by Parent array | Java program to find height using parent array ; This function fills depth of i 'th element in parent[]. The depth is  filled in depth[i]. ; If depth [ i ] is already filled ; If node at index i is root ; If depth of parent is not evaluated before , then evaluate depth of parent first ; Depth of this node is depth of parent plus 1 ; This function returns height of binary tree represented by parent array ; Create an array to store depth of all nodes / and initialize depth of every node as 0 ( an invalid value ) . Depth of root is 1 ; fill depth of all nodes ; The height of binary tree is maximum of all depths . Find the maximum value in depth [ ] and assign it to ht . ; Driver program to test above functions ; int parent [ ] = { 1 , 5 , 5 , 2 , 2 , - 1 , 3 } ;"
How can the above be solved in Java?

class BinaryTree { void fillDepth ( int parent [ ] , int i , int depth [ ] ) { if ( depth [ i ] != 0 ) { return ; } if ( parent [ i ] == - 1 ) { depth [ i ] = 1 ; return ; } if ( depth [ parent [ i ] ] == 0 ) { fillDepth ( parent , parent [ i ] , depth ) ; } depth [ i ] = depth [ parent [ i ] ] + 1 ; } int findHeight ( int parent [ ] , int n ) { int depth [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { depth [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { fillDepth ( parent , i , depth ) ; } int ht = depth [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( ht < depth [ i ] ) { ht = depth [ i ] ; } } return ht ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int parent [ ] = new int [ ] { - 1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = parent . length ; System . out . println ( " Height ▁ is ▁ " + tree . findHeight ( parent , n ) ) ; } }


"Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array | Java program to find Index of 0 to be replaced with 1 to get longest continuous sequence of 1 s in a binary array ; Returns index of 0 to be replaced with 1 to get longest continuous sequence of 1 s . If there is no 0 in array , then it returns - 1. ; for maximum number of 1 around a zero ; for storing result ; index of previous zero ; index of previous to previous zero ; Traverse the input array ; If current element is 0 , then calculate the difference between curr and prev_prev_zero ; Update result if count of 1 s around prev_zero is more ; Update for next iteration ; Check for the last encountered zero ; Driver program to test above function"
How can the above be solved in Java?

import java . io . * ; class Binary { static int maxOnesIndex ( int arr [ ] , int n ) { int max_count = 0 ; int max_index = 0 ; int prev_zero = - 1 ; int prev_prev_zero = - 1 ; for ( int curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " + maxOnesIndex ( arr , n ) ) ; } }


"Find Index of given fibonacci number in constant time | A simple Java program to find index of given Fibonacci number . ; if Fibonacci number is less than 2 , its index will be same as number ; iterate until generated fibonacci number is less than given fibonacci number ; res keeps track of number of generated fibonacci number ; Driver program to test above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } public static void main ( String [ ] args ) { int result = findIndex ( 21 ) ; System . out . println ( result ) ; } }


"Find Intersection of all Intervals | Java implementation of the approach ; Function to print the intersection ; First interval ; Check rest of the intervals and find the intersection ; If no intersection exists ; Else update the intersection ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findIntersection ( int intervals [ ] [ ] , int N ) { int l = intervals [ 0 ] [ 0 ] ; int r = intervals [ 0 ] [ 1 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( intervals [ i ] [ 0 ] > r intervals [ i ] [ 1 ] < l ) { System . out . println ( - 1 ) ; return ; } else { l = Math . max ( l , intervals [ i ] [ 0 ] ) ; r = Math . min ( r , intervals [ i ] [ 1 ] ) ; } } System . out . println ( " [ " + l + " , ▁ " + r + " ] " ) ; } public static void main ( String [ ] args ) { int intervals [ ] [ ] = { { 1 , 6 } , { 2 , 8 } , { 3 , 10 } , { 5 , 8 } } ; int N = intervals . length ; findIntersection ( intervals , N ) ; } }


"Find K distinct positive odd integers with sum N | Java implementation to find K odd positive integers such that their sum is equal to given number ; Function to find K odd positive integers such that their sum is N ; Condition to check if there are enough values to check ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void findDistinctOddSum ( int n , int k ) { if ( ( k * k ) <= n && ( n + k ) % 2 == 0 ) { int val = 1 ; int sum = 0 ; for ( int i = 1 ; i < k ; i ++ ) { System . out . print ( val + " ▁ " ) ; sum += val ; val += 2 ; } System . out . print ( n - sum + "
"); } else System . out . print ( "NO
"); } public static void main ( String [ ] args ) { int n = 100 ; int k = 4 ; findDistinctOddSum ( n , k ) ; } }


"Find K elements whose absolute difference with median of array is maximum | Java implementation to find first K elements whose difference with the median of array is maximum ; Function for calculating median ; check for even case ; Function to find the K maximum absolute difference with the median of the array ; Sort the array . ; Store median ; Find and store difference ; If diff [ i ] is greater print it Else print diff [ j ] ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double findMedian ( int a [ ] , int n ) { if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } static void kStrongest ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; double median = findMedian ( arr , n ) ; int [ ] diff = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { diff [ i ] = ( int ) Math . abs ( median - arr [ i ] ) ; } int i = 0 , j = n - 1 ; while ( k > 0 ) { if ( diff [ i ] > diff [ j ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; i ++ ; } else { System . out . print ( arr [ j ] + " ▁ " ) ; j -- ; } k -- ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int k = 3 ; int n = arr . length ; kStrongest ( arr , n , k ) ; } }


"Find K for every Array element such that at least K prefixes are â ‰¥ K | Java program for the above approach ; Function to find the K - value for every index in the array ; Multiset to store the array in the form of red - black tree ; Iterating over the array ; Inserting the current value in the multiset ; Condition to check if the smallest value in the set is less than it 's size ; Erase the smallest value ; h - index value will be the size of the multiset ; Driver code ; Array ; Size of the array ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void print_h_index ( int arr [ ] , int N ) { List < Integer > ms = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { ms . add ( arr [ i ] ) ; int t = Collections . min ( ms ) ; if ( t < ms . size ( ) ) { ms . remove ( ms . indexOf ( t ) ) ; } System . out . print ( ms . size ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 10 , 7 , 5 , 0 , 10 , 2 , 0 } ; int N = arr . length ; print_h_index ( arr , N ) ; } }


"Find K positive integers not exceeding N and having sum S | Java implementation for the above approach ; Function to represent S as the sum of K positive integers less than or equal to N ; If S can cannot be represented as sum of K integers ; If sum of first i natural numbers exceeds S ; Insert i into nums [ ] ; Insert first K - 1 positive numbers into answer [ ] ; Insert the K - th number ; Traverse the array answer [ ] ; If current element exceeds N ; Add the extra value to the previous element ; Reduce current element to N ; Printing the K numbers ; Driver code"
How can the above be solved in Java?

import java . util . Vector ; class GFG { static void solve ( int S , int K , int N ) { if ( K > N ) { System . out . println ( " - 1" ) ; return ; } int max_sum = 0 , min_sum = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { min_sum += i ; max_sum += N - i + 1 ; } if ( S < min_sum S > max_sum ) { System . out . println ( " - 1" ) ; return ; } int s1 = 0 ; Vector < Integer > nums = new Vector < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( s1 > S ) break ; s1 += i ; nums . add ( i ) ; } Vector < Integer > answer = new Vector < > ( ) ; int s2 = 0 ; for ( int i = 0 ; i < K - 1 ; i ++ ) { answer . add ( nums . get ( i ) ) ; s2 += nums . get ( i ) ; } answer . add ( S - s2 ) ; int Max = N ; for ( int i = answer . size ( ) - 1 ; i >= 0 ; i -- ) { if ( answer . get ( i ) > Max ) { int extra = answer . get ( i ) - Max ; if ( i - 1 >= 0 ) answer . set ( i - 1 , answer . get ( i - 1 ) + extra ) ; answer . set ( i , Max ) ; Max -- ; } else break ; } for ( int x : answer ) System . out . print ( x + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int S = 15 , K = 4 , N = 8 ; solve ( S , K , N ) ; } }


"Find K smallest leaf nodes from a given Binary Tree | Java program of the above approach ; Structure of binary tree node ; Function to create new node ; Utility function which calculates smallest three nodes of all leaf nodes ; Check if current root is a leaf node ; Traverse the left and right subtree ; Function to find the K smallest nodes of the Binary Tree ; Sorting the Leaf nodes array ; Loop to print the K smallest Leaf nodes of the array ; Driver Code ; Construct binary tree ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Vector < Integer > storeLeaf ( Node root , Vector < Integer > arr ) { if ( root == null ) return arr ; if ( root . left == null && root . right == null ) { arr . add ( root . data ) ; return arr ; } arr = storeLeaf ( root . left , arr ) ; arr = storeLeaf ( root . right , arr ) ; return arr ; } static void KSmallest ( Node root , int k ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr = storeLeaf ( root , arr ) ; Collections . sort ( arr ) ; for ( int i = 0 ; i < k ; i ++ ) { if ( i < arr . size ( ) ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } else { break ; } } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . left = newNode ( 21 ) ; root . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 8 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 19 ) ; KSmallest ( root , 3 ) ; } }


"Find K such that | A | Java implementation of the approach ; Function to find k such that | a - k | = | b - k | ; If ( a + b ) is even ; Driver code"
How can the above be solved in Java?

class GFG { static int find_k ( int a , int b ) { if ( ( a + b ) % 2 == 0 ) return ( ( a + b ) / 2 ) ; return - 1 ; } public static void main ( String [ ] args ) { int a = 2 , b = 16 ; System . out . println ( find_k ( a , b ) ) ; } }


"Find Kth element in an array containing odd elements first and then even elements | Java implementation of the approach ; Function to return the kth element in the modified array ; Finding the index from where the even numbers will be stored ; Return the kth element ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int getNumber ( int n , int k ) { int pos ; if ( ( n % 2 ) == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } public static void main ( String [ ] args ) { int n = 8 , k = 5 ; System . out . println ( getNumber ( n , k ) ) ; } }


"Find Kth element in an array containing odd elements first and then even elements | Java implementation of the approach ; Function to return the kth element in the modified array ; First odd number ; Insert the odd number ; Next odd number ; First even number ; Insert the even number ; Next even number ; Return the kth element ; Driver code"
How can the above be solved in Java?

class GFG { static int getNumber ( int n , int k ) { int [ ] arr = new int [ n ] ; int i = 0 ; int odd = 1 ; while ( odd <= n ) { arr [ i ++ ] = odd ; odd += 2 ; } int even = 2 ; while ( even <= n ) { arr [ i ++ ] = even ; even += 2 ; } return arr [ k - 1 ] ; } public static void main ( String [ ] args ) { int n = 8 , k = 5 ; System . out . println ( getNumber ( n , k ) ) ; } }


"Find Kth largest string from the permutations of the string with two characters | Java implementation of above approach ; Function to print the kth largest String ; loop to iterate through series ; total takes the position of second y ; i takes the position of first y ; calculating first y position ; calculating second y position from first y ; print all x before first y ; print first y ; print all x between first y and second y ; print second y ; print x which occur after second y ; Driver code"
How can the above be solved in Java?

class GFG { static void kthString ( int n , int k ) { int total = 0 ; int i = 1 ; while ( total < k ) { total = total + n - i ; i ++ ; } int first_y_position = i - 1 ; int second_y_position = k - ( total - n + first_y_position ) ; for ( int j = 1 ; j < first_y_position ; j ++ ) System . out . print ( " x " ) ; System . out . print ( " y " ) ; int j = first_y_position + 1 ; while ( second_y_position > 1 ) { System . out . print ( " x " ) ; second_y_position -- ; j ++ ; } System . out . print ( " y " ) ; while ( j < n ) { System . out . print ( " x " ) ; j ++ ; } } public static void main ( String [ ] args ) { int n = 5 ; int k = 7 ; kthString ( n , k ) ; } }


"Find Kth lexicographical ordered numeric string of length N with distinct products of each substring | Java program for the above approach ; Function to find the required String ; If the current length is equal to n exit from here only ; Iterate for all the characters ; Check if the product is present before ; If the current String is good then recurse for the next character ; Decrease all the products back to their original state ; Erase the last character ; Function to calculate kth ordered valid String ; Check for the base cases ; There are atmost 10 valid Strings for n = 1 ; Vector to keep a check on number of occurences of products ; Recursively construct the Strings ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String ans , s ; static int K ; static void getString ( int curlen , int N , int [ ] prod ) { if ( curlen == N ) { K -- ; if ( K == 0 ) ans = s ; return ; } char ch ; int ok , t , i ; for ( ch = '2' ; ch <= '9' ; ch ++ ) { s += ch ; ok = 1 ; t = 1 ; for ( i = curlen ; i >= 0 && s . length ( ) > i ; i -- ) { t *= s . charAt ( i ) - 48 ; if ( prod [ t ] != 0 ) ok = 0 ; prod [ t ] ++ ; } if ( ok != 0 ) getString ( curlen + 1 , N , prod ) ; t = 1 ; for ( i = curlen ; i >= 0 && s . length ( ) > i ; i -- ) { t *= s . charAt ( i ) - 48 ; prod [ t ] -- ; } if ( s . length ( ) > 0 ) s = s . substring ( 0 , s . length ( ) - 1 ) ; } } static String kthValidString ( int N ) { if ( N > 10 ) { return " - 1" ; } if ( N == 1 ) { if ( K > 10 ) { return " - 1" ; } String s = " " ; K -- ; s += ( K + '0' ) ; return s ; } ans = " - 1" ; s = " " ; int [ ] prod = new int [ 10005 ] ; getString ( 0 , N , prod ) ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; K = 4 ; System . out . print ( kthValidString ( N ) ) ; } }


"Find Kth number from sorted array formed by multiplying any two numbers in the array | Java implementation to find the Kth number in the list formed from product of any two numbers in the array and sorting them ; Function to find number of pairs ; Negative and Negative ; Add Possible Pairs ; Positive and Positive ; Add Possible pairs ; Negative and Positive ; Add Possible pairs ; Function to find the kth element in the list ; Separate Positive and Negative elements ; Sort the Elements ; Binary search ; Return the required answer ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean check ( int x , Vector pos , Vector neg , int k ) { int pairs = 0 ; int p = neg . size ( ) - 1 ; int nn = neg . size ( ) - 1 ; int pp = pos . size ( ) - 1 ; for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { while ( ( p >= 0 ) && ( ( int ) neg . get ( i ) * ( int ) neg . get ( p ) <= x ) ) p -- ; pairs += Math . min ( nn - p , nn - i ) ; } p = 0 ; for ( int i = pos . size ( ) - 1 ; i >= 0 ; i -- ) { while ( ( p < pos . size ( ) ) && ( ( int ) pos . get ( i ) * ( int ) pos . get ( p ) <= x ) ) p ++ ; pairs += Math . min ( p , i ) ; } p = pos . size ( ) - 1 ; for ( int i = neg . size ( ) - 1 ; i >= 0 ; i -- ) { while ( ( p >= 0 ) && ( ( int ) neg . get ( i ) * ( int ) pos . get ( p ) <= x ) ) p -- ; pairs += pp - p ; } return ( pairs >= k ) ; } static int kth_element ( int a [ ] , int n , int k ) { Vector pos = new Vector ( ) ; Vector neg = new Vector ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) pos . add ( a [ i ] ) ; else neg . add ( a [ i ] ) ; } sort ( pos . begin ( ) , pos . end ( ) ) ; sort ( neg . begin ( ) , neg . end ( ) ) ; Collections . sort ( pos ) ; Collections . sort ( neg ) ; int l = ( int ) - 1e8 , ans = 0 , r = ( int ) 1e8 ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( check ( mid , pos , neg , k ) ) { ans = mid ; r = mid - 1 ; } else l = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { - 4 , - 2 , 3 , 3 } , k = 3 ; int n = a . length ; System . out . println ( kth_element ( a , n , k ) ) ; } }


"Find Kth smallest value for b such that a + b = a | b | Java program to find k 'th smallest value for b such that a + b = a | b ; Function to find the kth smallest value for b ; res wiint store final answer ; skip when j ' th ▁ position ▁ ▁ has ▁ 1 ▁ in ▁ binary ▁ representation ▁ ▁ as ▁ in ▁ res , ▁ j ' th position wiint be 0. ; j 'th bit is set ; i 'th bit is set ; proceed to next bit ; Driver Code"
How can the above be solved in Java?

 class GFG { static int kthSmallest ( int a , int k ) { int res = 0 ; int j = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { while ( j < 32 && ( a & ( 1 << j ) ) > 0 ) j ++ ; if ( ( k & ( 1 << i ) ) > 0 ) res |= ( 1 << j ) ; j ++ ; } return res ; } public static void main ( String [ ] args ) { int a = 10 , k = 3 ; System . out . print ( kthSmallest ( a , k ) + "
"); } }


"Find LCA in Binary Tree using RMQ | Java program to find LCA of u and v by reducing problem to RMQ ; A binary tree node ; v is the highest value of node in our tree ; for euler tour sequence ; level of nodes in tour sequence ; to store 1 st occurrence of nodes ; variable to fill euler and level arrays ; log base 2 of x ; A recursive function to get the minimum value in a given range of array indexes . The following are parameters for this function . st -- > Pointer to segment tree index -- > Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se -- > Starting and ending indexes of the segment represented by current node , i . e . , st [ index ] qs & qe -- > Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the min of the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; Return minimum of elements in range from index qs ( query start ) to qe ( query end ) . It mainly uses RMQUtil ( ) ; Check for erroneous input values ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the minimum of two values in this node ; Function to construct segment tree from given array . This function allocates memory for segment tree and calls constructSTUtil ( ) to fill the allocated memory ; Allocate memory for segment tree Height of segment tree ; Maximum size of segment tree 2 * pow ( 2 , x ) - 1 ; Fill the allocated memory st ; Return the constructed segment tree ; Recursive version of the Euler tour of T ; if the passed node exists ; insert in euler array ; insert l in level array ; increment index ; if unvisited , mark first occurrence ; tour left subtree if exists , and remark euler and level arrays for..."
How can the above be solved in Java?

import java . util . * ; class Node { Node left , right ; int data ; Node ( int item ) { data = item ; left = right = null ; } } class St_class { int st ; int stt [ ] = new int [ 10000 ] ; } class BinaryTree { Node root ; int v = 9 ; int euler [ ] = new int [ 2 * v - 1 ] ; int level [ ] = new int [ 2 * v - 1 ] ; int f_occur [ ] = new int [ 2 * v - 1 ] ; int fill ; St_class sc = new St_class ( ) ; int Log2 ( int x ) { int ans = 0 ; int y = x >>= 1 ; while ( y -- != 0 ) ans ++ ; return ans ; } int swap ( int a , int b ) { return a ; } int RMQUtil ( int index , int ss , int se , int qs , int qe , St_class st ) { if ( qs <= ss && qe >= se ) return st . stt [ index ] ; else if ( se < qs ss > qe ) return - 1 ; int mid = ( ss + se ) / 2 ; int q1 = RMQUtil ( 2 * index + 1 , ss , mid , qs , qe , st ) ; int q2 = RMQUtil ( 2 * index + 2 , mid + 1 , se , qs , qe , st ) ; if ( q1 == - 1 ) return q2 ; else if ( q2 == - 1 ) return q1 ; return ( level [ q1 ] < level [ q2 ] ) ? q1 : q2 ; } int RMQ ( St_class st , int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . println ( " Invalid ▁ input " ) ; return - 1 ; } return RMQUtil ( 0 , 0 , n - 1 , qs , qe , st ) ; } void constructSTUtil ( int si , int ss , int se , int arr [ ] , St_class st ) { if ( ss == se ) st . stt [ si ] = ss ; else { int mid = ( ss + se ) / 2 ; constructSTUtil ( si * 2 + 1 , ss , mid , arr , st ) ; constructSTUtil ( si * 2 + 2 , mid + 1 , se , arr , st ) ; if ( arr [ st . stt [ 2 * si + 1 ] ] < arr [ st . stt [ 2 * si + 2 ] ] ) st . stt [ si ] = st . stt [ 2 * si + 1 ] ; else st . stt [ si ] = st . stt [ 2 * si + 2 ] ; } } int constructST ( int arr [ ] , int n ) { int x = Log2 ( n ) + 1 ; int max_size = 2 * ( 1 << x ) - 1 ; sc . stt = new int [ max_size ] ; constructSTUtil ( 0 , 0 , n - 1 , arr , sc ) ; return sc . st ; } void eulerTour ( Node node , int l ) { if ( node != null ) { euler [ fill ] = node . data ; level [ fill ] = l ; fill ++ ; if ( f_occur [ node . data ] == - 1 ) f_occur [ node . data ] = fill - 1 ; if ( node ....


"Find LCM of rational numbers | JAVA program to find LCM of given array ; get lcm of two numbers ; Finds LCM of numerators ; calculate the lcm of all numerators ; return all numerator lcm ; Get GCD of all the denominators ; calculate the gcd of all the denominators ; return all denominator gcd ; find lcm of all the rational number ; return the LCM of all numerator / GCD of all denominator ; Driver code ; give rational number 2 / 7 , 3 / 14 , 5 / 3 make pair as a numerator and denominator"
How can the above be solved in Java?

import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int LCM ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int lcmOfNumerator ( Vector < pair > vect ) { int lcm = vect . get ( 0 ) . first ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) lcm = LCM ( vect . get ( i ) . first , lcm ) ; return lcm ; } static int gcdOfDemoninators ( Vector < pair > vect ) { int gcd = vect . get ( 0 ) . second ; for ( int i = 1 ; i < vect . size ( ) ; i ++ ) gcd = __gcd ( vect . get ( i ) . second , gcd ) ; return gcd ; } static void lcmOfRationals ( Vector < pair > vect ) { System . out . print ( lcmOfNumerator ( vect ) + " / " + gcdOfDemoninators ( vect ) ) ; } public static void main ( String [ ] args ) { Vector < pair > vect = new Vector < pair > ( ) ; vect . add ( new pair ( 2 , 7 ) ) ; vect . add ( new pair ( 3 , 14 ) ) ; vect . add ( new pair ( 5 , 3 ) ) ; lcmOfRationals ( vect ) ; } }


"Find Largest Special Prime which is less than or equal to a given number | Java program to find the Largest Special Prime which is less than or equal to a given number ; Function to check whether the number is a special prime or not ; While number is not equal to zero ; If the number is not prime return false . ; Else remove the last digit by dividing the number by 10. ; If the number has become zero then the number is special prime , hence return true ; Function to find the Largest Special Prime which is less than or equal to a given number ; Initially all numbers are considered Primes . ; There is always an answer possible ; Checking if the number is a special prime or not ; If yes print the number and break the loop . ; Else decrement the number . ; Driver code"
How can the above be solved in Java?

class GFG { static boolean checkSpecialPrime ( boolean [ ] sieve , int num ) { while ( num != 0 ) { if ( ! sieve [ num ] ) { return false ; } num /= 10 ; } return true ; } static void findSpecialPrime ( int N ) { boolean [ ] sieve = new boolean [ N + 10 ] ; sieve [ 0 ] = sieve [ 1 ] = false ; for ( int i = 0 ; i < N + 10 ; i ++ ) sieve [ i ] = true ; for ( int i = 2 ; i <= N ; i ++ ) { if ( sieve [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { sieve [ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( sieve , N ) ) { System . out . println ( N ) ; break ; } else N -- ; } } public static void main ( String [ ] args ) { findSpecialPrime ( 379 ) ; findSpecialPrime ( 100 ) ; } }


"Find Last Digit of a ^ b for Large Numbers | Java code to find last digit of a ^ b ; Function to find b % a ; Initialize result ; calculating mod of b with a to make b like 0 <= b < a ; return mod ; return modulo ; Function to find last digit of a ^ b ; if a and b both are 0 ; if exponent is 0 ; if base is 0 ; if exponent is divisible by 4 that means last digit will be pow ( a , 4 ) % 10. if exponent is not divisible by 4 that means last digit will be pow ( a , b % 4 ) % 10 ; Find last digit in ' a ' and compute its exponent ; Return last digit of result ; Driver program to run test case"
How can the above be solved in Java?

import java . io . * ; import java . math . * ; class GFG { static int Modulo ( int a , char b [ ] ) { int mod = 0 ; for ( int i = 0 ; i < b . length ; i ++ ) mod = ( mod * 10 + b [ i ] - '0' ) % a ; } static int LastDigit ( char a [ ] , char b [ ] ) { int len_a = a . length , len_b = b . length ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == '0' && a [ 0 ] == '0' ) return 1 ; if ( len_b == 1 && b [ 0 ] == '0' ) return 1 ; if ( len_a == 1 && a [ 0 ] == '0' ) return 0 ; int exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; int res = ( int ) ( Math . pow ( a [ len_a - 1 ] - '0' , exp ) ) ; return res % 10 ; } public static void main ( String args [ ] ) throws IOException { char a [ ] = "117" . toCharArray ( ) , b [ ] = { '3' } ; System . out . println ( LastDigit ( a , b ) ) ; } }


"Find Leftmost and Rightmost node of BST from its given preorder traversal | Java program to find leftmost and rightmost node from given preorder sequence ; Function to return the leftmost and rightmost nodes of the BST whose preorder traversal is given ; Variables for finding minimum and maximum values of the array ; Update the minimum ; Update the maximum ; Print the values ; Driver Code"
How can the above be solved in Java?

class GFG { static void LeftRightNode ( int preorder [ ] , int n ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } System . out . println ( " Leftmost ▁ node ▁ is ▁ " + min ) ; System . out . println ( " Rightmost ▁ node ▁ is ▁ " + max ) ; } public static void main ( String [ ] args ) { int preorder [ ] = { 3 , 2 , 1 , 5 , 4 } ; int n = 5 ; LeftRightNode ( preorder , n ) ; } }


"Find Length of a Linked List ( Iterative and Recursive ) | Linked list Node ; Linked List class ; head of list ; Inserts a new Node at front of the list . ; 1 & 2 : Allocate the Node & Put in the data ; 3. Make next of new Node as head ; 4. Move the head to point to new Node ; Returns count of nodes in linked list ; Base case ; Count is this node plus rest of the list ; Wrapper over getCountRec ( ) ; Driver program to test above functions . Ideally this function should be in a separate user class . It is kept here to keep code compact ; Start with the empty list"
How can the above be solved in Java?

class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } class LinkedList { Node head ; public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public int getCountRec ( Node node ) { if ( node == null ) return 0 ; return 1 + getCountRec ( node . next ) ; } public int getCount ( ) { return getCountRec ( head ) ; } public static void main ( String [ ] args ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 1 ) ; llist . push ( 3 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; System . out . println ( " Count ▁ of ▁ nodes ▁ is ▁ " + llist . getCount ( ) ) ; } }


"Find M | Java program to Find m - th number whose sum of digits of a number until sum becomes single digit is N ; Function to find the M - th number whosesum till one digit is N ; Driver Code"
How can the above be solved in Java?

class GFG { static int findNumber ( int n , int m ) { int num = ( m - 1 ) * 9 + n ; return num ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( findNumber ( n , m ) ) ; } }


"Find Maximum Length Of A Square Submatrix Having Sum Of Elements At | Java implementation of the above approach ; Function to return maximum length of square submatrix having sum of elements at - most K ; Matrix to store prefix sum ; Current maximum length ; Variable for storing maximum length of square ; Calculating prefix sum ; Checking whether there exits square with length cur_max + 1 or not ; Returning the maximum length ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static int maxLengthSquare ( int row , int column , int [ ] [ ] arr , int k ) { int sum [ ] [ ] = new int [ row + 1 ] [ column + 1 ] ; int cur_max = 1 ; int max = 0 ; for ( int i = 1 ; i <= row ; i ++ ) { for ( int j = 1 ; j <= column ; j ++ ) { sum [ i ] [ j ] = sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] + arr [ i - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; if ( i >= cur_max && j >= cur_max && sum [ i ] [ j ] - sum [ i - cur_max ] [ j ] - sum [ i ] [ j - cur_max ] + sum [ i - cur_max ] [ j - cur_max ] <= k ) { max = cur_max ++ ; } } } return max ; } public static void main ( String args [ ] ) { int row = 4 , column = 4 ; int matrix [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } } ; int k = 6 ; int ans = maxLengthSquare ( row , column , matrix , k ) ; System . out . println ( ans ) ; } }


"Find Maximum and Minimum of two numbers using Absolute function | Java program to find maximum and minimum using Absolute function ; Function to return maximum among the two numbers ; Function to return minimum among the two numbers ; Driver code ; Displaying the maximum value ; Displaying the minimum value"
How can the above be solved in Java?

class GFG { static int maximum ( int x , int y ) { return ( ( x + y + Math . abs ( x - y ) ) / 2 ) ; } static int minimum ( int x , int y ) { return ( ( x + y - Math . abs ( x - y ) ) / 2 ) ; } public static void main ( String [ ] args ) { int x = 99 , y = 18 ; System . out . println ( " Maximum : ▁ " + maximum ( x , y ) ) ; System . out . println ( " Minimum : ▁ " + minimum ( x , y ) ) ; } }


"Find Maximum dot product of two arrays with insertion of 0 's | Java program to find maximum dot product of two array ; Function to compute Maximum Dot Product and return it ; Create 2D Matrix that stores dot product dp [ i + 1 ] [ j + 1 ] stores product considering B [ 0. . i ] and A [ 0. . . j ] . Note that since all m > n , we fill values in upper diagonal of dp [ ] [ ] ; Traverse through all elements of B [ ] ; Consider all values of A [ ] with indexes greater than or equal to i and compute dp [ i ] [ j ] ; Two cases arise 1 ) Include A [ j ] 2 ) Exclude A [ j ] ( insert 0 in B [ ] ) ; return Maximum Dot Product ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) { int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] ) ; return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 1 , 7 , 8 } ; int B [ ] = { 3 , 6 , 7 } ; int m = A . length ; int n = B . length ; System . out . print ( MaxDotProduct ( A , B , m , n ) ) ; } }


"Find Maximum number possible by doing at | Java program to find maximum integer possible by doing at - most K swap operations on its digits . ; Function to find maximum integer possible by doing at - most K swap operations on its digits ; Return if no swaps left ; Consider every digit ; Compare it with all digits after it ; if digit at position i is less than digit at position j , swap it and check for maximum number so far and recurse for remaining swaps ; swap str [ i ] with str [ j ] ; If current num is more than maximum so far ; recurse of the other k - 1 swaps ; Backtrack ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static String max ; static void findMaximumNum ( char [ ] str , int k ) { if ( k == 0 ) return ; int n = str . length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( str [ i ] < str [ j ] ) { char t = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = t ; if ( String . valueOf ( str ) . compareTo ( max ) > 0 ) max = String . valueOf ( str ) ; findMaximumNum ( str , k - 1 ) ; char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; } } } } public static void main ( String [ ] args ) { String str = "129814999" ; int k = 4 ; max = str ; findMaximumNum ( str . toCharArray ( ) , k ) ; System . out . print ( max + "
"); } }


"Find Minimum Depth of a Binary Tree | Class containing left and right child of current Node and key value ; Root of the Binary Tree ; Function to calculate the minimum depth of the tree ; Driver program to test above functions"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class MinimumTreeHeight { Node root ; int minimumDepth ( ) { return minimumDepth ( root , 0 ) ; } int minimumDepth ( Node root , int level ) { if ( root == null ) return level ; level ++ ; return Math . min ( minimumDepth ( root . left , level ) , minimumDepth ( root . right , level ) ) ; } public static void main ( String args [ ] ) { MinimumTreeHeight tree = new MinimumTreeHeight ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ minimum ▁ depth ▁ of ▁ " + " binary ▁ tree ▁ is ▁ : ▁ " + tree . minimumDepth ( ) ) ; } }


"Find Minimum Depth of a Binary Tree | Class containing left and right child of current node and key value ; Root of the Binary Tree ; Function to calculate the minimum depth of the tree ; Corner case . Should never be hit unless the code is called on root = NULL ; Base case : Leaf Node . This accounts for height = 1. ; If left subtree is NULL , recur for right subtree ; If right subtree is NULL , recur for left subtree ; Driver program to test above functions ; Let us construct the Tree shown in the above figure"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; int minimumDepth ( ) { return minimumDepth ( root ) ; } int minimumDepth ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; if ( root . left == null ) return minimumDepth ( root . right ) + 1 ; if ( root . right == null ) return minimumDepth ( root . left ) + 1 ; return Math . min ( minimumDepth ( root . left ) , minimumDepth ( root . right ) ) + 1 ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ minimum ▁ depth ▁ of ▁ " + " binary ▁ tree ▁ is ▁ : ▁ " + tree . minimumDepth ( ) ) ; } }


"Find Minimum Depth of a Binary Tree | Java program to find minimum depth of a given Binary Tree ; A binary Tree node ; A queue item ( Stores pointer tonode and an integer ) ; Iterative method to find minimum depth of Binary Tree ; Corner Case ; Create an empty queue for level order traversal ; Enqueue Root and initialize depth as 1 ; Do level order traversal ; Remove the front queue item ; Get details of the remove item ; If this is the first leaf node seen so far Then return its depth as answer ; If left subtree is not null , add it to queue ; If right subtree is not null , add it to queue ; Utility function to create a new tree Node ; Driver Code ; Let us create binary tree shown in above diagram"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static class qItem { Node node ; int depth ; public qItem ( Node node , int depth ) { this . node = node ; this . depth = depth ; } } static int minDepth ( Node root ) { if ( root == null ) return 0 ; Queue < qItem > q = new LinkedList < > ( ) ; qItem qi = new qItem ( root , 1 ) ; q . add ( qi ) ; while ( q . isEmpty ( ) == false ) { qi = q . peek ( ) ; q . remove ( ) ; Node node = qi . node ; int depth = qi . depth ; if ( node . left == null && node . right == null ) return depth ; if ( node . left != null ) { qi . node = node . left ; qi . depth = depth + 1 ; q . add ( qi ) ; } if ( node . right != null ) { qi . node = node . right ; qi . depth = depth + 1 ; q . add ( qi ) ; } } return 0 ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; System . out . println ( minDepth ( root ) ) ; } }


"Find Multiples of 2 or 3 or 5 less than or equal to N | Java program to count number of multiples of 2 or 3 or 5 less than or equal to N ; Bit count function ; Function to count number of multiples of 2 or 3 or 5 less than or equal to N ; As we have to check divisibility by three numbers , So we can implement bit masking ; we check whether jth bit is set or not , if jth bit is set , simply multiply to prod ; check for set bit ; check multiple of product ; Driver code"
How can the above be solved in Java?

class GFG { static int count_setbits ( int N ) { int cnt = 0 ; while ( N > 0 ) { cnt += ( N & 1 ) ; N = N >> 1 ; } return cnt ; } static int countMultiples ( int n ) { int multiple [ ] = { 2 , 3 , 5 } ; int count = 0 , mask = ( int ) Math . pow ( 2 , 3 ) ; for ( int i = 1 ; i < mask ; i ++ ) { int prod = 1 ; for ( int j = 0 ; j < 3 ; j ++ ) { if ( ( i & 1 << j ) > 0 ) prod = prod * multiple [ j ] ; } if ( count_setbits ( i ) % 2 == 1 ) count = count + n / prod ; else count = count - n / prod ; } return count ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( countMultiples ( n ) ) ; } }


"Find N Arithmetic Means between A and B | java program to illustrate n arithmetic mean between A and B ; insert function for calculating the means ; Finding the value of d Common difference ; for finding N the Arithmetic mean between A and B ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printAMeans ( int A , int B , int N ) { float d = ( float ) ( B - A ) / ( N + 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( ( A + i * d ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int A = 20 , B = 32 , N = 5 ; printAMeans ( A , B , N ) ; } }


"Find N Geometric Means between A and B | java program to illustrate n geometric mean between A and B ; insert function for calculating the means ; Finding the value of R Common ration ; for finding N the Geometric mean between A and B ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printGMeans ( int A , int B , int N ) { float R = ( float ) Math . pow ( ( float ) ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( A * Math . pow ( R , i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; } }


"Find N digits number which is divisible by D | Java program to Find N digits number which is divisible by D ; Function to return N digits number which is divisible by D ; to store answer ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static String findNumber ( int n , int d ) { String ans = " " ; if ( d != 10 ) { ans += Integer . toString ( d ) ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } else { if ( n == 1 ) ans += " Impossible " ; else { ans += '1' ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } } return ans ; } public static void main ( String [ ] args ) { int n = 12 , d = 3 ; System . out . println ( findNumber ( n , d ) ) ; } }


"Find N distinct integers with zero sum | Java implementation to Print N distinct numbers such that their sum is 0 ; Function to print distinct n numbers such that their sum is 0 ; Print 2 symmetric numbers ; Print a extra 0 if N is odd ; Driver code"
How can the above be solved in Java?

class GFG { static void findNumbers ( int N ) { for ( int i = 1 ; i <= N / 2 ; i ++ ) { System . out . print ( i + " , ▁ " + - i + " , ▁ " ) ; } if ( N % 2 == 1 ) System . out . print ( 0 ) ; } public static void main ( String [ ] args ) { int N = 10 ; findNumbers ( N ) ; } }


"Find N distinct numbers whose Bitwise XOR is equal to K | Java program for the above approach ; Function to find N integers having Bitwise XOR equal to K ; Base Cases ; Assign values to P and Q ; Stores Bitwise XOR of the first ( N - 3 ) elements ; Print the first N - 3 elements ; Calculate Bitwise XOR of first ( N - 3 ) elements ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findArray ( int N , int K ) { if ( N == 1 ) { System . out . print ( K + " ▁ " ) ; return ; } if ( N == 2 ) { System . out . print ( 0 + " ▁ " + K ) ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { System . out . print ( i + " ▁ " ) ; VAL ^= i ; } if ( VAL == K ) { System . out . print ( P + " ▁ " + Q + " ▁ " + ( P ^ Q ) ) ; } else { System . out . print ( 0 + " ▁ " + P + " ▁ " + ( P ^ K ^ VAL ) ) ; } } public static void main ( String [ ] args ) { int N = 4 , X = 6 ; findArray ( N , X ) ; } }


"Find N distinct numbers whose bitwise Or is equal to K | Java implementation of the approach ; Function to pre - calculate all the powers of 2 upto MAX ; Function to return the count of set bits in x ; To store the count of set bits ; Function to add num to the answer by setting all bit positions as 0 which are also 0 in K ; Bit i is 0 in K ; Function to find and print N distinct numbers whose bitwise OR is K ; Choosing K itself as one number ; Find the count of set bits in K ; Impossible to get N distinct integers ; Add i to the answer after setting all the bits as 0 which are 0 in K ; If N distinct numbers are generated ; Print the generated numbers ; Driver code ; Pre - calculate all the powers of 2"
How can the above be solved in Java?

import java . util . * ; class GFG { static final int MAX = 32 ; static long [ ] pow2 = new long [ MAX ] ; static boolean [ ] visited = new boolean [ MAX ] ; static Vector < Long > ans = new Vector < > ( ) ; static void power_2 ( ) { long ans = 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } static void add ( long num ) { int point = 0 ; long value = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num % 2 == 1 ) { value += ( 1 << i ) ; } num /= 2 ; } } ans . add ( value ) ; } static void solve ( long n , long k ) { ans . add ( k ) ; int countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { System . out . print ( - 1 ) ; return ; } int count = 0 ; for ( long i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { long n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ; } }


"Find N fractions that sum upto a given fraction N / D | Java implementation to split the fraction into N parts ; Function to split the fraction into the N parts ; Loop to find the N - 1 fraction ; Loop to print the Fractions ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . Scanner ; class Solution { public static void splitFraction ( int n , int d ) { long ar [ ] = new long [ n ] ; long first = d + n - 1 ; ar [ 0 ] = first ; for ( int i = 1 ; i < n ; i ++ ) { ar [ i ] = first * ( -- first ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ar [ i ] % n == 0 ) { System . out . print ( "1 / " + ar [ i ] / n + " , ▁ " ) ; } else { System . out . print ( n + " / " + ar [ i ] + " , ▁ " ) ; } } } public static void main ( String [ ] args ) throws Exception { int N = 4 ; int D = 2 ; splitFraction ( N , D ) ; } }


"Find N from the value of N ! | Java program to find a number such that the factorial of that number is given ; Map to precompute and store the factorials of the numbers ; Function to precompute factorial ; Calculating the factorial for each i and storing in a map ; Driver code ; Precomputing the factorials"
How can the above be solved in Java?

 import java . util . * ; class GFG { static Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; static void precompute ( ) { int fact = 1 ; for ( int i = 1 ; i <= 18 ; i ++ ) { fact = fact * i ; m . put ( fact , i ) ; } } public static void main ( String [ ] args ) { precompute ( ) ; int K = 120 ; System . out . print ( m . get ( K ) + "
"); K = 6 ; System . out . print ( m . get ( K ) + "
"); } }


"Find N in the given matrix that follows a pattern | Java implementation of the approach ; Function to return the row and the column of the given integer ; Binary search for the row number ; Condition to get the maximum x that satisfies the criteria ; Binary search for the column number ; Condition to get the maximum y that satisfies the criteria ; Get the row and the column number ; Return the pair ; Driver code"
How can the above be solved in Java?

class GFG { static int [ ] solve ( int n ) { int low = 1 , high = ( int ) 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = ( int ) 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; int ans [ ] = { r , c } ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] p = solve ( n ) ; System . out . println ( p [ 0 ] + " ▁ " + p [ 1 ] ) ; } }


"Find N integers with given difference between product and sum | Java code to generate numbers with difference between product and sum is D ; Function to implement calculation ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findNumbers ( int n , int d ) { for ( int i = 0 ; i < n - 2 ; i ++ ) System . out . print ( "1" + " ▁ " ) ; System . out . print ( "2" + " ▁ " ) ; System . out . println ( n + d ) ; } public static void main ( String args [ ] ) { int N = 3 , D = 5 ; findNumbers ( N , D ) ; } }


"Find N numbers such that a number and its reverse are divisible by sum of its digits | Java program to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits ; Function to calculate the sum of digits ; Loop to iterate through every digit of the number ; Returning the sum of digits ; Function to calculate the reverse of a number ; Loop to calculate the reverse of the number ; Return the reverse of the number ; Function to print the first N numbers such that every number and the reverse of the number is divisible by its sum of digits ; Loop to continuously check and generate number until there are n outputs ; Variable to hold the sum of the digit of the number ; Computing the reverse of the number ; Checking if the condition satisfies . Increment the count and print the number if it satisfies . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int digit_sum ( int n ) { int sum = 0 , m ; while ( n > 0 ) { m = n % 10 ; sum = sum + m ; n = n / 10 ; } return ( sum ) ; } static int reverse ( int n ) { int r = 0 ; while ( n != 0 ) { r = r * 10 ; r = r + n % 10 ; n = n / 10 ; } return ( r ) ; } static void operation ( int n ) { int i = 1 , a , count = 0 , r ; while ( count < n ) { a = digit_sum ( i ) ; r = reverse ( i ) ; if ( i % a == 0 && r % a == 0 ) { System . out . print ( i + " ▁ " ) ; count ++ ; i ++ ; } else i ++ ; } } public static void main ( String args [ ] ) { int n = 10 ; operation ( n ) ; } }


"Find N random points within a Circle | Java program for the above approach ; Return a random double between 0 & 1 ; Function to find the N random points on the given circle ; Result vector ; Get Angle in radians ; Get length from center ; Add point to results . ; Return the N points ; Function to display the content of the vector A ; Iterate over A ; Print the N random points stored ; Driver Code ; Given dimensions ; Function call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static final double PI = 3.141592653589 ; static class pair { double first , second ; public pair ( double first , double second ) { super ( ) ; this . first = first ; this . second = second ; } } static double uniform ( ) { return Math . random ( ) ; } static Vector < pair > randPoint ( int r , int x , int y , int n ) { Vector < pair > res = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { double theta = 2 * PI * uniform ( ) ; double len = Math . sqrt ( uniform ( ) ) * r ; res . add ( new pair ( x + len * Math . cos ( theta ) , y + len * Math . sin ( theta ) ) ) ; } return res ; } static void printVector ( Vector < pair > A ) { for ( pair P : A ) { System . out . printf ( "(%.2f, %.2f)
", P.first, P.second); } } public static void main ( String [ ] args ) { int R = 12 ; int X = 3 ; int Y = 3 ; int N = 5 ; printVector ( randPoint ( R , X , Y , N ) ) ; } }


"Find N values of X1 , X2 , ... Xn such that X1 < X2 < ... < XN and sin ( X1 ) < sin ( X2 ) < ... < sin ( XN ) | Java program for the above approach ; Function to print all such Xi s . t . all Xi and sin ( Xi ) are strictly increasing ; Till N becomes zero ; Find the value of sin ( ) using inbuilt function ; Increment by 710 ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void printSinX ( int N ) { int Xi = 0 ; int num = 1 ; while ( N -- > 0 ) { System . out . print ( " X " + num + " ▁ = ▁ " + Xi ) ; System . out . print ( " ▁ sin ( X " + num + " ) ▁ = ▁ " ) ; System . out . printf ( " % .6f " , Math . sin ( Xi ) ) ; System . out . println ( ) ; num += 1 ; Xi += 710 ; } } public static void main ( String [ ] args ) { int N = 5 ; printSinX ( N ) ; } }


"Find Next Sparse Number | Java program to find next sparse number ; Find binary representation of x and store it in bin . get ( ] . bin . get ( 0 ] contains least significant bit ( LSB ) , next bit is in bin . get ( 1 ] , and so on . ; There my be extra bit in result , so add one extra bit ; The position till which all bits are finalized ; Start from second bit ( next to LSB ) ; If current bit and its previous bit are 1 , but next bit is not 1. ; Make the next bit 1 ; Make all bits before current bit as 0 to make sure that we get the smallest next number ; Store position of the bit set so that this bit and bits before it are not changed next time . ; Find decimal equivalent of modified bin . get ( ] ; Driver program"
How can the above be solved in Java?

import java . util . * ; class GFG { static int nextSparse ( int x ) { ArrayList < Integer > bin = new ArrayList < Integer > ( ) ; while ( x != 0 ) { bin . add ( x & 1 ) ; x >>= 1 ; } bin . add ( 0 ) ; int last_final = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( bin . get ( i ) == 1 && bin . get ( i - 1 ) == 1 && bin . get ( i + 1 ) != 1 ) { bin . set ( i + 1 , 1 ) ; for ( int j = i ; j >= last_final ; j -- ) bin . set ( j , 0 ) ; last_final = i + 1 ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += bin . get ( i ) * ( 1 << i ) ; return ans ; } public static void main ( String [ ] args ) { int x = 38 ; System . out . println ( " Next ▁ Sparse ▁ Number ▁ is ▁ " + nextSparse ( x ) ) ; } }


"Find Nth even length palindromic number formed using digits X and Y | Java program to find nth even palindromic number of only even length composing of 4 ' s ▁ and ▁ 5' s . ; Utility function to compute n 'th palindrome number  ; Calculate the length from above formula as discussed above ; Calculate rank for length L ; Mask to check if i 't bit   is set or not  ; If bit is set append '5' else append '4' ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . ceil ( Math . log ( n + 2 ) / Math . log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } StringBuilder sb = new StringBuilder ( right ) ; sb . reverse ( ) ; right = sb . toString ( ) ; String res = left + right ; return res ; } public static void main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; System . out . println ( ans ) ; } }


"Find Nth number in a sequence which is not a multiple of a given number | Java program for the above approach ; Function to find Nth number not a multiple of A in range [ L , R ] ; Calculate the Nth no ; Check for the edge case ; Driver Code ; Input parameters ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static void countNo ( int A , int N , int L , int R ) { int ans = L - 1 + N + ( int ) Math . floor ( ( N - 1 ) / ( A - 1 ) ) ; if ( ans % A == 0 ) { ans = ans + 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int A = 5 , N = 10 , L = 4 , R = 20 ; countNo ( A , N , L , R ) ; } }


"Find Nth number of the series 1 , 6 , 15 , 28 , 45 , ... . . | Java program to find N - th term of the series : ; function to return nth term of the series ; Driver Code ; Taking n as 4 ; Printing the nth term"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static long NthTerm ( long n ) { long x = ( 2 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } public static void main ( String args [ ] ) { long N = 4 ; System . out . println ( NthTerm ( N ) ) ; } }


"Find Nth positive number whose digital root is X | Java program to find the N - th number whose digital root is X ; Function to find the digital root of a number ; Function to find the Nth number with digital root as X ; Counter variable to keep the count of valid numbers ; Find digital root ; Check if is required answer or not ; Print the answer if you have found it and breakout of the loop ; Driver Code"
How can the above be solved in Java?

class GFG { static int findDigitalRoot ( int num ) { int sum = Integer . MAX_VALUE , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } static void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { System . out . print ( i ) ; break ; } } } public static void main ( String args [ ] ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; } }


"Find Nth positive number whose digital root is X | Java program to find the N - th number with digital root as X ; Function to find the N - th number with digital root as X ; Driver Code"
How can the above be solved in Java?

class GfG { static int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } public static void main ( String [ ] args ) { int X = 7 , N = 43 ; System . out . println ( findAnswer ( X , N ) ) ; } }


"Find Nth root of a number using Bisection method | Java program for the above approach ; Function that returns the value of the function at a given value of x ; calculating the value of the differential of the function ; The function that returns the root of given number ; Defining range on which answer can be found ; finding mid value ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double f ( double x , int p , double num ) { return Math . pow ( x , p ) - num ; } static double f_prime ( double x , int p ) { return p * Math . pow ( x , p - 1 ) ; } static double root ( double num , int p ) { double left = - num ; double right = num ; double x ; while ( true ) { x = ( left + right ) / 2.0 ; double value = f ( x , p , num ) ; double prime = f_prime ( x , p ) ; if ( value * prime <= 0 ) left = x ; else right = x ; if ( value < 0.000001 && value >= 0 ) { return x ; } } } public static void main ( String args [ ] ) { double P = 1234321 ; int N = 2 ; double ans = root ( P , N ) ; System . out . print ( ans ) ; } }


"Find Nth smallest number having exactly 4 divisors | Java program for the above approach ; Function to find the nth number which has exactly 4 divisors ; The divs [ ] array to store number of divisors of every element ; The vis [ ] array to check if given number is considered or not ; The cnt stores number of elements having exactly 4 divisors ; Iterate while cnt less than n ; If i is a prime ; Iterate in the range [ 2 * i , 1000000 ] with increment of i ; If the number j is already considered ; Dividing currNum by i until currNum % i is equal to 0 ; Case a single prime in its factorization ; Case of two distinct primes which divides j exactly once each ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int nthNumber ( int n ) { int divs [ ] = new int [ 1000000 ] ; boolean vis [ ] = new boolean [ 1000000 ] ; int cnt = 0 ; for ( int i = 2 ; cnt < n ; i ++ ) { if ( divs [ i ] == 0 ) { for ( int j = 2 * i ; j < 1000000 ; j += i ) { if ( vis [ j ] ) { continue ; } vis [ j ] = true ; int currNum = j ; int count = 0 ; while ( currNum % i == 0 ) { divs [ j ] ++ ; currNum = currNum / i ; count ++ ; } if ( currNum == 1 && count == 3 && divs [ j ] == 3 ) { cnt ++ ; } else if ( currNum != 1 && divs [ currNum ] == 0 && count == 1 && divs [ j ] == 1 ) { cnt ++ ; } if ( cnt == n ) { return j ; } } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 24 ; System . out . println ( nthNumber ( N ) ) ; } }


"Find Nth smallest number that is divisible by 100 exactly K times | Java implementation of above approach ; Function to find the Nth smallest number ; If N is divisible by 100 then we multiply N + 1 otherwise , it will be divisible by 100 more than K times ; convert integer to string ; if N is not divisible by 100 ; convert integer to string ; add 2 * K 0 's at the end to be divisible  by 100 exactly K times ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String find_number ( int N , int K ) { String r ; if ( N % 100 == 0 ) { N += 1 ; r = String . valueOf ( N ) ; } else { r = String . valueOf ( N ) ; } for ( int i = 1 ; i <= K ; i ++ ) r += "00" ; return r ; } public static void main ( String [ ] args ) { int N = 1000 , K = 2 ; String ans = find_number ( N , K ) ; System . out . println ( ans ) ; } }


"Find Nth term ( A matrix exponentiation example ) | Java program to find n - th term of a recursive function using matrix exponentiation . ; This power function returns first row of { Transformation Matrix } ^ n - 1 * Initial Vector ; This is an identity matrix . ; this is Transformation matrix . ; Matrix exponentiation to calculate power of { tMat } ^ n - 1 store res in " res " matrix . ; res store { Transformation matrix } ^ n - 1 hence wiint be first row of res * Initial Vector . ; Driver code"
How can the above be solved in Java?


"Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Java program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 ; Function to find factorial of N ; base condition ; use recursion ; calculate Nth term of series ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int factorial ( int N ) { if ( N == 0 N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }


"Find Nth term of series 1 , 4 , 15 , 72 , 420. . . | Java program to find N - th term of the series : 1 , 4 , 15 , 72 , 420 ; Function to find factorial of N ; return factorial of N ; calculate Nth term of series ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int factorial ( int N ) { int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fact = fact * i ; return fact ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }


"Find Nth term of the series 1 , 5 , 32 , 288 ... | Java code to generate ' Nth ' terms of this sequence ; Function to generate a fixed \ number ; Finding nth term ; Driver Method"
How can the above be solved in Java?

import java . lang . Math ; class GFG { public static int nthTerm ( int N ) { int nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += Math . pow ( i , i ) ; } return nth ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }


"Find Nth term of the series 1 , 6 , 18 , 40 , 75 , ... . | Java code to generate ' Nth ' term of this sequence ; Function to generate a fixed number ; ( N ^ 2 * ( N + 1 ) ) / 2 ; Driver Method"
How can the above be solved in Java?

class GFG { public static int nthTerm ( int N ) { int nth = 0 ; nth = ( N * N * ( N + 1 ) ) / 2 ; return nth ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( nthTerm ( N ) ) ; } }


"Find Nth term of the series 1 , 8 , 54 , 384. . . | Java program to find N - th term of the series : 1 , 8 , 54 , 384. . . ; Main class for main method ; Calculate factorial of N ; By using above formula"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int fact ( int N ) { int i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } public static int nthTerm ( int N ) { return ( N * N ) * fact ( N ) ; } public static void main ( String [ ] args ) { System . out . println ( nthTerm ( N ) ) ; } }


"Find Nth term of the series 2 , 3 , 10 , 15 , 26. ... | Java program to find Nth term of the series 2 , 3 , 10 , 15 , 26. ... ; Function to find Nth term ; Nth term ; Driver code"
How can the above be solved in Java?

 class GFG { static int nthTerm ( int N ) { int nth = 0 ; if ( N % 2 == 1 ) nth = ( N * N ) + 1 ; else nth = ( N * N ) - 1 ; return nth ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( nthTerm ( N ) + "
"); } }


"Find Nth term of the series 4 , 2 , 2 , 3 , 6 , ... | Java program to find Nth term of the series 4 , 2 , 2 , 3 , 6 , ... ; Function to find Nth term ; Nth term ; Driver code"
How can the above be solved in Java?

 class GFG { static int nthTerm ( int N ) { int nth = 0 , first_term = 4 ; int pi = 1 , po = 1 ; int n = N ; while ( n > 1 ) { pi *= n - 1 ; n -- ; po *= 2 ; } nth = ( first_term * pi ) / po ; return nth ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( nthTerm ( N ) + "
"); } }


"Find Nth term of the series where each term differs by 6 and 2 alternately | Java program for the above approach ; Function to find Nth term ; Iterate from 1 till Nth term ; Check if i is even and then add 6 ; Else add 2 ; Print ans ; Driver Code"
How can the above be solved in Java?

 class GFG { static void findNthTerm ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { ans = ans + 6 ; } else { ans = ans + 2 ; } } System . out . print ( ans + "
"); } public static void main ( String [ ] args ) { int N = 3 ; findNthTerm ( N ) ; } }


"Find Number of Even cells in a Zero Matrix after Q queries | Java program find Number of Even cells in a Zero Matrix after Q queries ; Function to find the number of even cell in a 2D matrix ; Maintain two arrays , one for rows operation and one for column operation ; Increment operation on row [ i ] ; Increment operation on col [ i ] ; Count odd and even values in both arrays and multiply them ; Count of rows having even numbers ; Count of rows having odd numbers ; Count of columns having even numbers ; Count of columns having odd numbers ; Driver code"
How can the above be solved in Java?

class GFG { static int findNumberOfEvenCells ( int n , int q [ ] [ ] , int size ) { int row [ ] = new int [ n ] ; int col [ ] = new int [ n ] ; for ( int i = 0 ; i < size ; i ++ ) { int x = q [ i ] [ 0 ] ; int y = q [ i ] [ 1 ] ; row [ x - 1 ] ++ ; col [ y - 1 ] ++ ; } int r1 = 0 , r2 = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( row [ i ] % 2 == 0 ) { r1 ++ ; } if ( row [ i ] % 2 == 1 ) { r2 ++ ; } if ( col [ i ] % 2 == 0 ) { c1 ++ ; } if ( col [ i ] % 2 == 1 ) { c2 ++ ; } } int count = r1 * c1 + r2 * c2 ; return count ; } public static void main ( String [ ] args ) { int n = 2 ; int q [ ] [ ] = { { 1 , 1 } , { 1 , 2 } , { 2 , 1 } } ; int size = q . length ; System . out . println ( findNumberOfEvenCells ( n , q , size ) ) ; } }


"Find Partition Line such that sum of values on left and right is equal | Java implementation of the approach ; Function that returns true if the required line exists ; To handle negative values from x [ ] ; Update arr [ ] such that arr [ i ] contains the sum of all v [ j ] such that x [ j ] = i for all valid values of j ; Update arr [ i ] such that arr [ i ] contains the sum of the subarray arr [ 0. . . i ] from the original array ; If all the points add to 0 then the line can be drawn anywhere ; If the line is drawn touching the leftmost possible points ; If the line is drawn just before the current point ; If the line is drawn touching the current point ; If the line is drawn just after the current point ; If the line is drawn touching the rightmost possible points ; Driver code"
How can the above be solved in Java?

class GFG { static int MAX = 1000 ; static boolean lineExists ( int x [ ] , int y [ ] , int v [ ] , int n ) { int size = ( 2 * MAX ) + 1 ; long [ ] arr = new long [ size ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ x [ i ] + MAX ] += v [ i ] ; } for ( int i = 1 ; i < size ; i ++ ) arr [ i ] += arr [ i - 1 ] ; if ( arr [ size - 1 ] == 0 ) return true ; if ( arr [ size - 1 ] - arr [ 0 ] == 0 ) return true ; for ( int i = 1 ; i < size - 1 ; i ++ ) { if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i - 1 ] ) return true ; if ( arr [ i - 1 ] == arr [ size - 1 ] - arr [ i ] ) return true ; if ( arr [ i ] == arr [ size - 1 ] - arr [ i ] ) return true ; } if ( arr [ size - 2 ] == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x [ ] = { - 3 , 5 , 8 } ; int y [ ] = { 8 , 7 , 9 } ; int v [ ] = { 8 , 2 , 10 } ; int n = x . length ; if ( lineExists ( x , y , v , n ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }


"Find Prime Adam integers in the given range [ L , R ] | Java program to find all prime adam numbers in the given range ; reversing a number by taking remainder at a time ; Function to check if a number is a prime or not ; Iterating till the number ; Checking for factors ; Returning 1 if the there are no factors of the number other than 1 or itself ; Function to check whether a number is an adam number or not ; Reversing given number ; Squaring given number ; Squaring reversed number ; Reversing the square of the reversed number ; Checking if the square of the number and the square of its reverse are equal or not ; Function to find all the prime adam numbers in the given range ; If the first number is greater than the second number , print invalid ; Iterating through all the numbers in the given range ; Checking for prime number ; Checking for Adam number ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int reverse ( int a ) { int rev = 0 ; while ( a != 0 ) { int r = a % 10 ; rev = rev * 10 + r ; a = a / 10 ; } return ( rev ) ; } public static int prime ( int a ) { int k = 0 ; for ( int i = 2 ; i < a ; i ++ ) { if ( a % i == 0 ) { k = 1 ; break ; } } if ( k == 1 ) { return ( 0 ) ; } else { return ( 1 ) ; } } public static int adam ( int a ) { int r1 = reverse ( a ) ; int s1 = a * a ; int s2 = r1 * r1 ; int r2 = reverse ( s2 ) ; if ( s1 == r2 ) { return ( 1 ) ; } else { return ( 0 ) ; } } public static void find ( int m , int n ) { if ( m > n ) { System . out . println ( " ▁ INVALID ▁ INPUT ▁ " ) ; } else { int c = 0 ; for ( int i = m ; i <= n ; i ++ ) { int l = prime ( i ) ; int k = adam ( i ) ; if ( ( l == 1 ) && ( k == 1 ) ) { System . out . print ( i + " 	 " ) ; } } } } public static void main ( String [ ] args ) { int L = 5 , R = 100 ; find ( L , R ) ; } }


"Find Quotient and Remainder of two integer without using division operators | JAVA implementation to Find Quotient and Remainder of two integer without using / and % operator using Binary search ; Function to the quotient and remainder ; Check if start is greater than the end ; Calculate mid ; Check if n is greater than divisor then increment the mid by 1 ; Check if n is less than 0 then decrement the mid by 1 ; Check if n equals to divisor ; Return the final answer ; Recursive calls ; Driver code"
How can the above be solved in Java?

 class GFG { static int [ ] find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return new int [ ] { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return new int [ ] { mid , n } ; } return find ( dividend , divisor , start , end ) ; } static int [ ] divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } public static void main ( String [ ] args ) { int dividend = 10 , divisor = 3 ; int [ ] ans = divide ( dividend , divisor ) ; System . out . print ( ans [ 0 ] + " , ▁ " ) ; System . out . print ( ans [ 1 ] + "
"); } }


"Find Second largest element in an array | JAVA Code for Find Second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; If current element is smaller than first then update both first and second ; If arr [ i ] is in between first and second then update second ; Driver program to test above function"
How can the above be solved in Java?

 class GFG { public static void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { System . out . print ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } first = second = Integer . MIN_VALUE ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) second = arr [ i ] ; } if ( second == Integer . MIN_VALUE ) System . out . print ( " There ▁ is ▁ no ▁ second ▁ largest " + " element
"); else System . out . print ( " The ▁ second ▁ largest ▁ element " + " ▁ is ▁ " + second ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . length ; print2largest ( arr , n ) ; } }


"Find Second largest element in an array | Java program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; Find the largest element ; Find the second largest element ; Driver code"
How can the above be solved in Java?

 class GFG { static void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int largest = second = Integer . MIN_VALUE ; for ( i = 0 ; i < arr_size ; i ++ ) { largest = Math . max ( largest , arr [ i ] ) ; } for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] != largest ) second = Math . max ( second , arr [ i ] ) ; } if ( second == Integer . MIN_VALUE ) System . out . printf ( " There ▁ is ▁ no ▁ second ▁ " + "largest element
"); else System . out . printf ( " The ▁ second ▁ largest ▁ " + "element is %d
", second); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . length ; print2largest ( arr , n ) ; } }


"Find Second largest element in an array | Java program to find second largest element in an array ; Function to print the second largest elements ; There should be atleast two elements ; Sort the array ; Start from second last element as the largest element is at last ; If the element is not equal to largest element ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } Arrays . sort ( arr ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { System . out . printf ( " The ▁ second ▁ largest ▁ " + "element is %d
", arr[i]); return ; } } System . out . printf ( " There ▁ is ▁ no ▁ second ▁ " + "largest element
"); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . length ; print2largest ( arr , n ) ; } }


"Find Second largest element in an array | Set 2 | Java program to implement the above approach ; Function to find the largest element in the array arr [ ] ; Base Condition ; Initialize an empty list ; Divide the array into two equal length subarrays and recursively find the largest among the two ; Store length of compared1 [ ] in the first index ; Store the maximum element ; Return compared1 which contains the maximum element ; Store length of compared2 [ ] in the first index ; Store the maximum element ; Return compared2 [ ] which contains the maximum element ; Function to print the second largest element in the array arr [ ] ; Find the largest element in arr [ ] ; Find the second largest element in arr [ ] ; Print the second largest element ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int [ ] findLargest ( int beg , int end , int [ ] arr , int n ) { if ( beg == end ) { int [ ] compared = new int [ n ] ; compared [ 0 ] = 1 ; compared [ 1 ] = arr [ beg ] ; return compared ; } int [ ] compared1 = findLargest ( beg , ( beg + end ) / 2 , arr , n ) ; int [ ] compared2 = findLargest ( ( beg + end ) / 2 + 1 , end , arr , n ) ; if ( compared1 [ 1 ] > compared2 [ 1 ] ) { int k = compared1 [ 0 ] + 1 ; compared1 [ 0 ] = k ; compared1 [ k ] = compared2 [ 1 ] ; return compared1 ; } else { int k = compared2 [ 0 ] + 1 ; compared2 [ 0 ] = k ; compared2 [ k ] = compared1 [ 1 ] ; return compared2 ; } } static void findSecondLargest ( int end , int [ ] arr ) { int [ ] compared1 = findLargest ( 0 , end - 1 , arr , end ) ; int [ ] compared2 = findLargest ( 2 , compared1 [ 0 ] + 2 , compared1 , compared1 [ 0 ] ) ; System . out . print ( compared2 [ 1 ] ) ; } public static void main ( String [ ] args ) { int N = 10 ; int [ ] arr = { 20 , 1990 , 12 , 1110 , 1 , 59 , 12 , 15 , 120 , 1110 } ; findSecondLargest ( N , arr ) ; } }


"Find Selling Price from given Profit Percentage and Cost | Java implementation of above approach ; Function to calculate the Selling Price ; Decimal Equivalent of Profit Percentage ; Find the Selling Price ; return the calculated Selling Price ; Driver code ; Get the CP and Profit % ; Printing the returned value"
How can the above be solved in Java?

import java . util . * ; class solution { static float SellingPrice ( float CP , float PP ) { float P_decimal = 1 + ( PP / 100 ) ; float res = P_decimal * CP ; return res ; } public static void main ( String args [ ] ) { float C = 720 , P = 13 ; System . out . println ( SellingPrice ( C , P ) ) ; } }