"Find maximum of minimum for every window size in a given array | A naive method to find maximum of minimum of all windows of different sizes ; Consider all windows of different sizes starting from size 1 ; Initialize max of min for current window size k ; Traverse through all windows of current size k ; Find minimum of current window ; Update maxOfMin if required ; Print max of min for current window size ; Driver method"
How can the above be solved in Java?

class Test { static int arr [ ] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { for ( int k = 1 ; k <= n ; k ++ ) { int maxOfMin = Integer . MIN_VALUE ; for ( int i = 0 ; i <= n - k ; i ++ ) { int min = arr [ i ] ; for ( int j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] < min ) min = arr [ i + j ] ; } if ( min > maxOfMin ) maxOfMin = min ; } System . out . print ( maxOfMin + " ▁ " ) ; } } public static void main ( String [ ] args ) { printMaxOfMin ( arr . length ) ; } }


"Find maximum of minimum for every window size in a given array | An efficient Java program to find maximum of all minimums of windows of different size ; Used to find previous and next smaller ; Arrays to store previous and next smaller ; Initialize elements of left [ ] and right [ ] ; Fill elements of left [ ] using logic discussed on www . geeksforgeeks . org / next - greater - element / https : ; Empty the stack as stack is going to be used for right [ ] ; Fill elements of right [ ] using same logic ; Create and initialize answer array ; Fill answer array by comparing minimums of all lengths computed using left [ ] and right [ ] ; length of the interval ; arr [ i ] is a possible answer for this length ' len ' interval , check if arr [ i ] is more than max for ' len ' ; Some entries in ans [ ] may not be filled yet . Fill them by taking values from right side of ans [ ] ; Print the result ; Driver method"
How can the above be solved in Java?

import java . util . Stack ; class Test { static int arr [ ] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { Stack < Integer > s = new Stack < > ( ) ; int left [ ] = new int [ n + 1 ] ; int right [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) left [ i ] = s . peek ( ) ; s . push ( i ) ; } while ( ! s . empty ( ) ) s . pop ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) right [ i ] = s . peek ( ) ; s . push ( i ) ; } int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) ans [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . max ( ans [ len ] , arr [ i ] ) ; } for ( int i = n - 1 ; i >= 1 ; i -- ) ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { printMaxOfMin ( arr . length ) ; } }


"Find maximum operations to reduce N to 1 | Java program to find maximum number moves possible ; To store number of prime factors of each number ; Function to find number of prime factors of each number ; if i is a prime number ; increase value by one from it 's preveious multiple ; make prefix sum this will be helpful for multiple test cases ; Driver Code ; Generate primeFactors array ; required answer"
How can the above be solved in Java?

import java . io . * ; class GFG { static int N = 1000005 ; static int primeFactors [ ] = new int [ N ] ; static void findPrimeFactors ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( primeFactors [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) primeFactors [ j ] = primeFactors [ j / i ] + 1 ; for ( int i = 1 ; i < N ; i ++ ) primeFactors [ i ] += primeFactors [ i - 1 ] ; } public static void main ( String [ ] args ) { findPrimeFactors ( ) ; int a = 6 , b = 3 ; System . out . println ( primeFactors [ a ] - primeFactors [ b ] ) ; } }


"Find maximum path sum in a 2D matrix when exactly two left moves are allowed | Java program to find maximum path sum in a 2D matrix when exactly two left moves are allowed ; Function to return the maximum path sum ; Copy last column i . e . starting and ending columns in another array ; Calculate suffix sum in each row ; Select the path we are going to follow ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int N = 3 ; static int M = 3 ; static int findMaxSum ( int arr [ ] [ ] ) { int sum = 0 ; int [ ] [ ] b = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ; b [ i ] [ j ] = Math . max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; System . out . println ( findMaxSum ( arr ) ) ; } }


"Find maximum points which can be obtained by deleting elements from array | Java program to find maximum cost after deleting all the elements form the array ; function to return maximum cost obtained ; find maximum element of the array . ; initialize count of all elements to zero . ; calculate frequency of all elements of array . ; stores cost of deleted elements . ; selecting minimum range from L and R . ; finds upto which elements are to be deleted when element num is selected . ; get maximum when selecting element num or not . ; Driver program ; size of array ; function call to find maximum cost"
How can the above be solved in Java?

public class GFG { static int maxCost ( int a [ ] , int n , int l , int r ) { int mx = 0 , k ; for ( int i = 0 ; i < n ; ++ i ) mx = Math . max ( mx , a [ i ] ) ; int [ ] count = new int [ mx + 1 ] ; for ( int i = 0 ; i < count . length ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) count [ a [ i ] ] ++ ; int [ ] res = new int [ mx + 1 ] ; res [ 0 ] = 0 ; l = Math . min ( l , r ) ; for ( int num = 1 ; num <= mx ; num ++ ) { k = Math . max ( num - l - 1 , 0 ) ; res [ num ] = Math . max ( res [ num - 1 ] , num * count [ num ] + res [ k ] ) ; } return res [ mx ] ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 1 , 2 , 3 , 2 , 2 , 1 } , l = 1 , r = 1 ; int n = a . length ; System . out . println ( maxCost ( a , n , l , r ) ) ; } }


"Find maximum possible value of advertising | Java program for the above approach ; Function to find maximum possible advertising value ; To store advertising value at i - th minute ; Base Case ; If no advertisement is taken on ith minute ; If advertisement is taken on i - th minute ; Driver code ; array [ ] [ 0 ] start time array [ ] [ 1 ] advertising value"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static int max_value ( int array [ ] [ ] , int M , int K , int N ) { int [ ] time = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { time [ array [ i ] [ 0 ] ] = array [ i ] [ 1 ] ; } int [ ] [ ] dp = new int [ M ] [ 2 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = time [ 0 ] ; for ( int i = 1 ; i < M ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; dp [ i ] [ 1 ] = time [ i ] ; if ( i - K >= 0 ) { dp [ i ] [ 1 ] += Math . max ( dp [ i - K ] [ 0 ] , dp [ i - K ] [ 1 ] ) ; } } return Math . max ( dp [ M - 1 ] [ 0 ] , dp [ M - 1 ] [ 1 ] ) ; } public static void main ( String [ ] args ) { int array [ ] [ ] = { { 0 , 10 } , { 4 , 110 } , { 5 , 30 } } ; int N = 3 ; int K = 4 ; int M = 6 ; System . out . println ( max_value ( array , M , K , N ) ) ; } }


"Find maximum product of digits among numbers less than or equal to N | Java implementation of the approach ; Function that returns the maximum product of digits among numbers less than or equal to N ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int maxProd ( int N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return Math . max ( maxProd ( N / 10 ) * ( N % 10 ) , maxProd ( N / 10 - 1 ) * 9 ) ; } public static void main ( String [ ] args ) { int N = 390 ; System . out . println ( maxProd ( N ) ) ; } }


"Find maximum subset | Java program for tha above approach ; Function to calculate maximum sum possible by taking at most K elements that is divisibly by D ; Variable to store final answer ; Traverse all subsets ; Update ans if necessary conditions are satisfied ; Driver Code ; Input ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maximumSum ( int [ ] A , int N , int K , int D ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) { int sum = 0 ; int c = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i >> j & 1 ) != 0 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . max ( ans , sum ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 , D = 7 ; int [ ] A = { 1 , 11 , 5 , 5 , 18 } ; System . out . print ( maximumSum ( A , N , K , D ) ) ; } }


"Find maximum subset | Java program for the above approach ; Dp vector ; current element ; current element modulo D ; copy previous state ; Transitions ; return answer ; Driver Code ; Input ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maximumSum ( int [ ] A , int N , int K , int D ) { int [ ] [ ] [ ] dp = new int [ N + 1 ] [ K + 1 ] [ D + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < K + 1 ; j ++ ) { for ( int k = 0 ; k < D + 1 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } for ( int i = 1 ; i <= N ; i ++ ) { int element = A [ i - 1 ] ; int mod = A [ i - 1 ] % D ; dp [ i ] = dp [ i - 1 ] ; for ( int j = 1 ; j <= K ; j ++ ) { dp [ i ] [ j ] [ mod ] = Math . max ( dp [ i ] [ j ] [ mod ] , element ) ; for ( int p = 0 ; p < D ; p ++ ) { if ( dp [ i - 1 ] [ j - 1 ] [ p ] != - 1 ) { dp [ i ] [ j ] [ ( p + mod ) % D ] = Math . max ( dp [ i ] [ j ] [ ( p + mod ) % D ] , dp [ i - 1 ] [ j - 1 ] [ p ] + element ) ; } } } } if ( dp [ N ] [ K ] [ 0 ] == - 1 ) return 0 ; return dp [ N ] [ K ] [ 0 ] ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 , D = 7 ; int [ ] A = { 1 , 11 , 5 , 5 , 18 } ; System . out . print ( maximumSum ( A , N , K , D ) ) ; } }


"Find maximum sum array of length less than or equal to m | Java program to find maximum sum of array of size less than or equal to m from given n arrays ; N and M to define sizes of arr , dp , current_arr and maxSum ; INF to define min value ; Function to find maximum sum ; dp array of size N x M ; current_arr of size M ; maxsum of size M ; if we have 0 elements from 0 th array ; compute the cumulative sum array ; calculating the maximum contiguous array for every length j , j is from 1 to lengtn of the array ; every state is depending on its previous state ; computation of dp table similar approach as knapsack problem ; now we have done processing with the last array lets find out what is the maximum sum possible ; Driver Code ; first element of each row is the size of that row"
How can the above be solved in Java?

import java . io . * ; public class GFG { static int N = 105 ; static int M = 1001 ; static int INF = - 1111111111 ; static int maxSum ( int [ ] [ ] arr ) { int [ ] [ ] dp = new int [ N ] [ M ] ; int [ ] current_arr = new int [ M ] ; int [ ] maxsum = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) dp [ i ] [ j ] = - 1 ; } current_arr [ 0 ] = 0 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= 5 ; i ++ ) { int len = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= len ; j ++ ) { current_arr [ j ] = arr [ i - 1 ] [ j ] ; current_arr [ j ] += current_arr [ j - 1 ] ; maxsum [ j ] = INF ; } for ( int j = 1 ; j <= len && j <= 6 ; j ++ ) for ( int k = 1 ; k <= len ; k ++ ) if ( j + k - 1 <= len ) maxsum [ j ] = Math . max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) ; for ( int j = 0 ; j <= 6 ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; for ( int j = 1 ; j <= 6 ; j ++ ) for ( int cur = 1 ; cur <= j && cur <= len ; cur ++ ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - cur ] + maxsum [ cur ] ) ; } int ans = 0 ; for ( int i = 0 ; i <= 6 ; i ++ ) ans = Math . max ( ans , dp [ 5 ] [ i ] ) ; return ans ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 3 , 2 , 3 , 5 } , { 2 , 7 , - 1 } , { 2 , 8 , 10 } , { 4 , 5 , 2 , 6 , 1 } , { 3 , 2 , 3 , - 2 } } ; System . out . println ( " Maximum ▁ sum ▁ can ▁ be ▁ " + " obtained ▁ is ▁ : ▁ " + maxSum ( arr ) ) ; } }


"Find maximum sum from top to bottom row with no adjacent diagonal elements | Java implementation to find maximum sum from top to bottom row with no adjacent diagonal elements ; Function to find the maximum path sum from top to bottom row ; Create an auxiliary array of next row with the element and it 's position ; Sort the auxiliary array ; Find maximum from row above to be added to the current element ; Find the maximum element from the next row that can be added to current row element ; Find the maximum sum ; Driver code ; Function to find maximum path"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSum ( int [ ] [ ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { ArrayList < int [ ] > aux = new ArrayList < > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . add ( new int [ ] { V [ i + 1 ] [ j ] , j } ) ; } Collections . sort ( aux , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux . get ( k ) [ 1 ] - j == 0 || Math . abs ( aux . get ( k ) [ 1 ] - j ) > 1 ) { V [ i ] [ j ] += aux . get ( k ) [ 0 ] ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . length ; int m = V [ 0 ] . length ; System . out . println ( maxSum ( V , n , m ) ) ; } }


"Find maximum sum of subsequence after flipping signs of at most K elements in given Array | Java implementation for the above approach ; Function to calculate the max sum of subsequence ; Variable to store the max sum ; Sort the array ; Iterate over the array ; Flip sign ; Decrement k ; Traverse over the array ; Add only positive elements ; Return the max sum ; Driver Code ; Given array ; Variable to store number of flips are allowed ; Function call to find the maximum sum of subsequence"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maxSubseq ( int arr [ ] , int N , int K ) { int sum = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( K == 0 ) break ; if ( arr [ i ] < 0 ) { arr [ i ] = - arr [ i ] ; K -- ; } } for ( int i = 0 ; i < N ; i ++ ) if ( arr [ i ] > 0 ) sum += arr [ i ] ; return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , - 10 , - 1 , 0 , - 4 , 2 } ; int K = 2 ; int N = arr . length ; System . out . println ( maxSubseq ( arr , N , K ) ) ; } }


"Find maximum sum taking every Kth element in the array | Java implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Find maximum from all sequences ; Sum of the sequence starting from index i ; Update maximum ; Driver code"
How can the above be solved in Java?

class GFG { static int maxSum ( int arr [ ] , int n , int K ) { int maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int sumk = 0 ; for ( int j = i ; j < n ; j += K ) sumk = sumk + arr [ j ] ; maximum = Math . max ( maximum , sumk ) ; } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . length ; int K = 2 ; System . out . println ( maxSum ( arr , n , K ) ) ; } }


"Find maximum sum taking every Kth element in the array | Java implementation of the approach ; Function to return the maximum sum for every possible sequence such that a [ i ] + a [ i + k ] + a [ i + 2 k ] + ... + a [ i + qk ] is maximized ; Initialize the maximum with the smallest value ; Initialize the sum array with zero ; Iterate from the right ; Update the sum starting at the current element ; Update the maximum so far ; Driver code"
How can the above be solved in Java?

class GFG { static int maxSum ( int arr [ ] , int n , int K ) { int maximum = Integer . MIN_VALUE ; int [ ] sum = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = Math . max ( maximum , sum [ i ] ) ; } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . length ; int K = 2 ; System . out . print ( maxSum ( arr , n , K ) ) ; } }


"Find maximum topics to prepare in order to pass the exam | Java implementation of the approach ; Function to return the maximum marks by considering topics which can be completed in the given time duration ; If we are given 0 time then nothing can be done So all values are 0 ; If we are given 0 topics then the time required will be 0 for sure ; Calculating the maximum marks that can be achieved under the given time constraints ; If time taken to read that topic is more than the time left now at position j then do no read that topic ; Two cases arise : 1 ) Considering current topic 2 ) Ignoring current topic We are finding maximum of ( current topic weightage + topics which can be done in leftover time - current topic time ) and ignoring current topic weightage sum ; Moving upwards in table from bottom right to calculate the total time taken to read the topics which can be done in given time and have highest weightage sum ; It means we have not considered reading this topic for max weightage sum ; Adding the topic time ; Evaluating the left over time after considering this current topic ; One topic completed ; It contains the maximum weightage sum formed by considering the topics ; Condition when exam cannot be passed ; Return the marks that can be obtained after passing the exam ; Driver code ; Number of topics , hours left and the passing marks ; n + 1 is taken for simplicity in loops Array will be indexed starting from 1"
How can the above be solved in Java?

import java . io . * ; class GFG { static int MaximumMarks ( int marksarr [ ] , int timearr [ ] , int h , int n , int p ) { int no_of_topics = n + 1 ; int total_time = h + 1 ; int T [ ] [ ] = new int [ no_of_topics ] [ total_time ] ; for ( int i = 0 ; i < no_of_topics ; i ++ ) { T [ i ] [ 0 ] = 0 ; } for ( int j = 0 ; j < total_time ; j ++ ) { T [ 0 ] [ j ] = 0 ; } for ( int i = 1 ; i < no_of_topics ; i ++ ) { for ( int j = 1 ; j < total_time ; j ++ ) { if ( j < timearr [ i ] ) { T [ i ] [ j ] = T [ i - 1 ] [ j ] ; } else { T [ i ] [ j ] = Math . max ( marksarr [ i ] + T [ i - 1 ] [ j - timearr [ i ] ] , T [ i - 1 ] [ j ] ) ; } } } int i = no_of_topics - 1 , j = total_time - 1 ; int sum = 0 ; while ( i > 0 && j > 0 ) { if ( T [ i ] [ j ] == T [ i - 1 ] [ j ] ) { i -- ; } else { sum += timearr [ i ] ; j -= timearr [ i ] ; i -- ; } } int marks = T [ no_of_topics - 1 ] [ total_time - 1 ] ; if ( marks < p ) return - 1 ; return sum ; } public static void main ( String [ ] args ) { int n = 4 , h = 10 , p = 10 ; int marksarr [ ] = { 0 , 6 , 4 , 2 , 8 } ; int timearr [ ] = { 0 , 4 , 6 , 2 , 7 } ; System . out . println ( MaximumMarks ( marksarr , timearr , h , n , p ) ) ; } }


"Find maximum unreachable height using two ladders | Java implementation of the approach ; Function to return the maximum height which can 't be reached ; Driver code"
How can the above be solved in Java?

class GFG { static int maxHeight ( int h1 , int h2 ) { return ( ( h1 * h2 ) - h1 - h2 ) ; } public static void main ( String args [ ] ) { int h1 = 7 , h2 = 5 ; System . out . println ( Math . max ( 0 , maxHeight ( h1 , h2 ) ) ) ; } }


"Find maximum value of Sum ( i * arr [ i ] ) with only rotations on given array allowed | Java program to find max value of i * arr [ i ] ; Returns max possible value of i * arr [ i ] ; Find array sum and i * arr [ i ] with no rotation Stores sum of arr [ i ] ; Stores sum of i * arr [ i ] ; Initialize result as 0 rotation sum ; Try all rotations one by one and find the maximum rotation sum . ; Return result ; Driver method to test the above function"
How can the above be solved in Java?

import java . util . Arrays ; class Test { static int arr [ ] = new int [ ] { 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; static int maxSum ( ) { int arrSum = 0 ; int currVal = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } int maxVal = currVal ; for ( int j = 1 ; j < arr . length ; j ++ ) { currVal = currVal + arrSum - arr . length * arr [ arr . length - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } public static void main ( String [ ] args ) { System . out . println ( " Max ▁ sum ▁ is ▁ " + maxSum ( ) ) ; } }


"Find maximum value of the last element after reducing the array with given operations | Java implementation of the approach ; Function to return the maximized value ; Overall minimum absolute value of some element from the array ; Add all absolute values ; Count positive and negative elements ; Both positive and negative values are present ; Only positive or negative values are present ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int find_maximum_value ( int a [ ] , int n ) { int sum = 0 ; int minimum = Integer . MAX_VALUE ; int pos = 0 , neg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { minimum = Math . min ( minimum , Math . abs ( a [ i ] ) ) ; sum += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) pos += 1 ; else neg += 1 ; } if ( pos > 0 && neg > 0 ) return sum ; return ( sum - 2 * minimum ) ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 4 , 6 , 2 } ; int n = a . length ; System . out . println ( find_maximum_value ( a , n ) ) ; } }


"Find maximum value of x such that n ! % ( k ^ x ) = 0 | Java program to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Function to maximize the value of x such that n ! % ( k ^ x ) = 0 ; Find square root of k and add 1 to it ; Run the loop from 2 to m and k should be greater than 1 ; optimize the value of k ; Minimum store ; Driver Code"
How can the above be solved in Java?

import java . util . * ; public class GfG { private static int findX ( int n , int k ) { int r = n , v , u ; int m = ( int ) Math . sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = Math . min ( r , u / v ) ; } } return r ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 2 ; System . out . println ( findX ( n , k ) ) ; } }


"Find maximum volume of a cuboid from the given perimeter and area | Java implementation of the above approach ; function to return maximum volume ; calculate length ; calculate volume ; return result ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class Geeks { static float maxVol ( float P , float A ) { float l = ( float ) ( P - Math . sqrt ( P * P - 24 * A ) ) / 12 ; float V = ( float ) ( l * ( A / 2.0 - l * ( P / 4.0 - l ) ) ) ; return V ; } public static void main ( String args [ ] ) { float P = 20 , A = 16 ; System . out . println ( maxVol ( P , A ) ) ; } }


"Find maximum xor of k elements in an array | Java implementation of the approach ; Function to return the maximum xor for a subset of size j from the given array ; If the subset is complete then return the xor value of the selected elements ; Return if already calculated for some mask and j at the i 'th index ; Initialize answer to 0 ; If we can still include elements in our subset include the i 'th element ; Exclude the i 'th element  ans store the max of both operations ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MAX = 10000 ; static int MAX_ELEMENT = 50 ; static int [ ] [ ] [ ] dp = new int [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; static int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != - 1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = Math . max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; for ( int i = 0 ; i < MAX_ELEMENT ; i ++ ) { for ( int j = 0 ; j < MAX_ELEMENT ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } System . out . println ( Max_Xor ( arr , 0 , k , 0 , n ) ) ; } }


"Find maximum xor of k elements in an array | Java implementation of the approach ; Function to return the maximum xor for a subset of size k from the given array ; Initialize result ; Traverse all subsets of the array ; __builtin_popcount ( ) returns the number of sets bits in an integer ; Initialize current xor as 0 ; If jth bit is set in i then include jth element in the current xor ; Update maximum xor so far ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int Max_Xor ( int arr [ ] , int n , int k ) { int maxXor = Integer . MIN_VALUE ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { if ( Integer . bitCount ( i ) == k ) { int cur_xor = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) cur_xor = cur_xor ^ arr [ j ] ; } maxXor = Math . max ( maxXor , cur_xor ) ; } } return maxXor ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; System . out . println ( Max_Xor ( arr , n , k ) ) ; } }


"Find middle point segment from given segment lengths | Java implementation of the approach ; Function that returns the segment for the middle point ; the middle point ; stores the segment index ; increment sum by length of the segment ; if the middle is in between two segments ; if sum is greater than middle point ; Driver code"
How can the above be solved in Java?

class GFG { static int findSegment ( int n , int m , int [ ] segment_length ) { double meet_point = ( 1.0 * n ) / 2.0 ; int sum = 0 ; int segment_number = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( ( double ) sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } public static void main ( String [ ] args ) { int n = 13 ; int m = 3 ; int [ ] segment_length = new int [ ] { 3 , 2 , 8 } ; int ans = findSegment ( n , m , segment_length ) ; System . out . println ( ans ) ; } }


"Find minimum GCD of all pairs in an array | Java program to find the minimum GCD of any pair in the array ; Function returns the minimum GCD of any pair ; Finding GCD of all the elements in the array . ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; else return __gcd ( b , a % b ) ; } static int MinimumGCD ( int arr [ ] , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 3 } ; int N = arr . length ; System . out . println ( MinimumGCD ( arr , N ) ) ; } }


"Find minimum and maximum elements in singly Circular Linked List | Java program to find minimum and maximum value from singly circular linked list ; structure for a node ; Function to print minimum and maximum nodes of the circular linked list ; check list is empty ; pointer for traversing ; initialize head to current pointer ; initialize max int value to min initialize min int value to max ; While last node is not reached ; If current node data is lesser for min then replace it ; If current node data is greater for max then replace it ; Function to insert a node at the end of a Circular linked list ; Create a new node ; check node is created or not ; insert data into newly created node ; check list is empty if not have any node then make first node it ; if list have already some node ; move firt node to last node ; put first or head node address in new node link ; put new node address into last node link ( next ) ; Function to print the Circular linked list ; if list is empty simply show message ; traverse first to last node ; Driver Code"
How can the above be solved in Java?

class GFG { static class Node { int data ; Node next ; } ; static void printMinMax ( Node head ) { if ( head == null ) { return ; } Node current ; current = head ; int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; while ( current . next != head ) { if ( current . data < min ) { min = current . data ; } if ( current . data > max ) { max = current . data ; } current = current . next ; } System . out . println ( " Minimum = " ▁ + ▁ min ▁ + ▁ " , Maximum = " + max); } static Node insertNode ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { System . out . printf ( " Memory Error "); return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static void displayList ( Node head ) { Node current = head ; if ( head == null ) { System . out . printf ( " Display List is empty "); return ; } else { do { System . out . printf ( " % d ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } public static void main ( String args [ ] ) { Node Head = null ; Head = insertNode ( Head , 99 ) ; Head = insertNode ( Head , 11 ) ; Head = insertNode ( Head , 22 ) ; Head = insertNode ( Head , 33 ) ; Head = insertNode ( Head , 44 ) ; Head = insertNode ( Head , 55 ) ; Head = insertNode ( Head , 66 ) ; System . out . println ( " Initial ▁ List : ▁ " ) ; displayList ( Head ) ; printMinMax ( Head ) ; } }


"Find minimum area of rectangle with given set of coordinates | package whatever Java Implementation of above approach ; # function to find minimum area of Rectangle ; creating empty columns ; fill columns with coordinates ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static int minAreaRect ( int [ ] [ ] points ) { @ SuppressWarnings ( " unchecked " ) Set < Integer > columns = new HashSet ( ) ; for ( int [ ] point : points ) columns . add ( 40001 * point [ 0 ] + point [ 1 ] ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < points . length ; ++ i ) for ( int j = i + 1 ; j < points . length ; ++ j ) { if ( points [ i ] [ 0 ] != points [ j ] [ 0 ] && points [ i ] [ 1 ] != points [ j ] [ 1 ] ) { if ( columns . contains ( 40001 * points [ i ] [ 0 ] + points [ j ] [ 1 ] ) && columns . contains ( 40001 * points [ j ] [ 0 ] + points [ i ] [ 1 ] ) ) { ans = Math . min ( ans , Math . abs ( points [ j ] [ 0 ] - points [ i ] [ 0 ] ) * Math . abs ( points [ j ] [ 1 ] - points [ i ] [ 1 ] ) ) ; } } } return ans < Integer . MAX_VALUE ? ans : 0 ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 1 , 1 } , { 1 , 3 } , { 3 , 1 } , { 3 , 3 } , { 2 , 2 } } ; System . out . println ( minAreaRect ( A ) ) ; } }


"Find minimum difference between any two elements | Java implementation of simple method to find minimum difference between any pair ; Returns minimum difference between any pair ; Initialize difference as infinite ; Find the min diff by comparing difference of all possible pairs in given array ; Return min diff ; Driver method to test the above function"
How can the above be solved in Java?

class GFG { static int findMinDiff ( int [ ] arr , int n ) { int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . length ) ) ; } }


"Find minimum difference between any two elements | Java program to find minimum difference between any pair in an unsorted array ; Returns minimum difference between any pair ; Sort array in non - decreasing order ; Initialize difference as infinite ; Find the min diff by comparing adjacent pairs in sorted array ; Return min diff ; Driver method to test the above function"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . length ) ) ; } }


"Find minimum length sub | Java implementation of the approach ; Function to return the minimum length of a sub - array which contains { 0 , 1 , 2 , 3 , 4 } as a sub - sequence ; To store the indices where 0 , 1 , 2 , 3 and 4 are present ; To store if there exist a valid prefix of sequence in array ; Base Case ; If current element is 0 ; Update the count of 0 s till now ; Push the index of the new 0 ; To check if previous element of the given sequence is found till now ; If it is the end of sequence ; Iterate for other elements of the sequence ; Binary Search to find closest occurrence less than equal to starting point ; Update the starting point ; Driver Code"
How can the above be solved in Java?

class GFG { static int MAX_INT = 1000000 ; static int solve ( int [ ] array , int N ) { int [ ] [ ] pos = new int [ 5 ] [ 10000 ] ; int [ ] pref = new int [ 5 ] ; if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = 0 ; } int ans = MAX_INT ; for ( int i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] ] [ pos [ array [ i ] ] . length - 1 ] = i ; if ( array [ i ] == 4 ) { int end = i ; int start = i ; for ( int j = 3 ; j >= 0 ; j -- ) { int s = 0 ; int e = pos [ j ] . length - 1 ; int temp = - 1 ; while ( s <= e ) { int m = ( s + e ) / 2 ; if ( pos [ j ] [ m ] <= start ) { temp = pos [ j ] [ m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . min ( ans , end - start + 1 ) ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 } ; int N = array . length ; System . out . println ( solve ( array , N ) ) ; } }


"Find minimum moves to bring all elements in one cell of a matrix | Java implementation to find the minimum number of moves to bring all non - zero element in one cell of the matrix ; Function to find the minimum number of moves to bring all elements in one cell of matrix ; Moves variable to store the sum of number of moves ; Loop to count the number of the moves ; Condition to check that the current cell is a non - zero element ; Driver code ; Coordinates of given cell ; Given Matrix ; Element to be moved ; Function call"
How can the above be solved in Java?

class GFG { static int M = 4 ; static int N = 5 ; public static void no_of_moves ( int [ ] [ ] Matrix , int x , int y ) { int moves = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( Matrix [ i ] [ j ] != 0 ) { moves += Math . abs ( x - i ) ; moves += Math . abs ( y - j ) ; } } } System . out . println ( moves ) ; } public static void main ( String [ ] args ) { int x = 3 ; int y = 2 ; int [ ] [ ] Matrix = { { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 0 , 0 } } ; int num = 1 ; no_of_moves ( Matrix , x , y ) ; } }


"Find minimum moves to reach target on an infinite line | Java program to find minimum moves to reach target if we can move i steps in i - th move . ; Handling negatives by symmetry ; Keep moving while sum is smaller or difference is odd . ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . math . * ; class GFG { static int reachTarget ( int target ) { target = Math . abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void main ( String args [ ] ) { int target = 5 ; System . out . println ( reachTarget ( target ) ) ; } }


"Find minimum number K such that sum of array after multiplication by K exceed S | Java implementation of the approach ; Function to return the minimum value of k that satisfies the given condition ; Store sum of array elements ; Calculate the sum after ; Return minimum possible K ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . lang . Math ; class GFG { static int findMinimumK ( int a [ ] , int n , int S ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return ( int ) Math . ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . length ; int S = 200 ; System . out . print ( findMinimumK ( a , n , S ) ) ; } }


"Find minimum number of Log value needed to calculate Log upto N | Java program to find number of log values needed to calculate all the log values from 1 to N ; In this vector prime [ i ] will store true if prime [ i ] is prime , else store false ; Using sieve of Eratosthenes to find all prime upto N ; Function to find number of log values needed to calculate all the log values from 1 to N ; calculate primes upto N ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MAX = 1000005 ; static Vector < Boolean > prime = new Vector < > ( MAX ) ; static void vecIni ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { prime . add ( i , true ) ; } } static void sieve ( int N ) { prime . add ( 0 , false ) ; prime . add ( 1 , false ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( prime . get ( i ) ) { for ( int j = 2 ; i * j <= N ; j ++ ) { prime . add ( i * j , false ) ; } } } } static int countLogNeeded ( int N ) { int count = 0 ; sieve ( N ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( prime . get ( i ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { vecIni ( ) ; int N = 6 ; System . out . println ( countLogNeeded ( N ) ) ; } }


"Find minimum number of coins that make a given value | A Dynamic Programming based Java program to find minimum of coins to make a given change V ; m is size of coins array ( number of different coins ) ; table [ i ] will be storing the minimum number of coins required for i value . So table [ V ] will have result ; Base case ( If given value V is 0 ) ; Initialize all table values as Infinite ; Compute minimum coins required for all values from 1 to V ; Go through all coins smaller than i ; Driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static int minCoins ( int coins [ ] , int m , int V ) { int table [ ] = new int [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) if ( coins [ j ] <= i ) { int sub_res = table [ i - coins [ j ] ] ; if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < table [ i ] ) table [ i ] = sub_res + 1 ; } } if ( table [ V ] == Integer . MAX_VALUE ) return - 1 ; return table [ V ] ; } public static void main ( String [ ] args ) { int coins [ ] = { 9 , 6 , 5 , 1 } ; int m = coins . length ; int V = 11 ; System . out . println ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }


"Find minimum number of coins that make a given value | A Naive recursive JAVA program to find minimum of coins to make a given change V ; m is size of coins array ( number of different coins ) ; base case ; Initialize result ; Try every coin that has smaller value than V ; Check for INT_MAX to avoid overflow and see if result can minimized ; Driver program to test above function"
How can the above be solved in Java?

class coin { static int minCoins ( int coins [ ] , int m , int V ) { if ( V == 0 ) return 0 ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( coins [ i ] <= V ) { int sub_res = minCoins ( coins , m , V - coins [ i ] ) ; if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < res ) res = sub_res + 1 ; } } return res ; } public static void main ( String args [ ] ) { int coins [ ] = { 9 , 6 , 5 , 1 } ; int m = coins . length ; int V = 11 ; System . out . println ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }


"Find minimum number of currency notes and values that sum to given amount | Java program to accept an amount and count number of notes ; function to count and print currency notes ; count notes using Greedy approach ; Print notes ; driver function"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; public class GfG { public static void countCurrency ( int amount ) { int [ ] notes = new int [ ] { 2000 , 500 , 200 , 100 , 50 , 20 , 10 , 5 , 1 } ; int [ ] noteCounter = new int [ 9 ] ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( amount >= notes [ i ] ) { noteCounter [ i ] = amount / notes [ i ] ; amount = amount - noteCounter [ i ] * notes [ i ] ; } } System . out . println ( " Currency ▁ Count ▁ - > " ) ; for ( int i = 0 ; i < 9 ; i ++ ) { if ( noteCounter [ i ] != 0 ) { System . out . println ( notes [ i ] + " ▁ : ▁ " + noteCounter [ i ] ) ; } } } public static void main ( String argc [ ] ) { int amount = 868 ; countCurrency ( amount ) ; } }


"Find minimum number of merge operations to make an array palindrome | Java program to find number of operations to make an array palindrome ; Returns minimum number of count operations required to make arr [ ] palindrome ; Initialize result ; Start from two corners ; If corner elements are same , problem reduces arr [ i + 1. . j - 1 ] ; If left element is greater , then we merge right two elements ; need to merge from tail . ; Else we merge left two elements ; Driver method to test the above function"
How can the above be solved in Java?

class GFG { static int findMinOps ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 4 , 5 , 9 , 1 } ; System . out . println ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + findMinOps ( arr , arr . length ) ) ; } }


"Find minimum number of steps to reach the end of String | Java implementation of the approach ; Function to return the minimum number of steps to reach the end ; If the end can 't be reached ; Already at the end ; If the length is 2 or 3 then the end can be reached in a single step ; For the other cases , solve the problem using dynamic programming ; It requires no move from the end to reach the end ; From the 2 nd last and the 3 rd last index , only a single move is required ; Update the answer for every index ; If the current index is not reachable ; To store the minimum steps required from the current index ; If it is a valiINT_MAXd move then update the minimum steps required ; Update the minimum steps required starting from the current index ; Cannot reach the end starting from str [ 0 ] ; Return the minimum steps required ; Driver code"
How can the above be solved in Java?

class GFG { final static int INT_MAX = Integer . MAX_VALUE ; static int minSteps ( String str , int n , int k ) { if ( str . charAt ( n - 1 ) == '0' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int dp [ ] = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str . charAt ( i + k ) == '1' ) steps = Math . min ( steps , dp [ i + k ] ) ; if ( str . charAt ( i + 1 ) == '1' ) steps = Math . min ( steps , dp [ i + 1 ] ) ; if ( str . charAt ( i + 2 ) == '1' ) steps = Math . min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return - 1 ; return dp [ 0 ] ; } public static void main ( String [ ] args ) { String str = "101000011" ; int n = str . length ( ) ; int k = 5 ; System . out . println ( minSteps ( str , n , k ) ) ; } }


"Find minimum number to be divided to make a number a perfect square | Java program to find minimum number which divide n to make it a perfect square . ; Return the minimum number to be divided to make n a perfect square . ; Since 2 is only even prime , compute its power separately . ; If count is odd , it must be removed by dividing n by prime number . ; If count is odd , it must be removed by dividing n by prime number . ; Driver code"
How can the above be solved in Java?

class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 == 1 ) ans *= 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 == 1 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } public static void main ( String [ ] args ) { int n = 72 ; System . out . println ( findMinNumber ( n ) ) ; } }


"Find minimum operations needed to make an Array beautiful | Java implementation of above approach ; Function to find minimum operations required to make array beautiful ; counting consecutive zeros . ; check that start and end are same ; check is zero and one are equal ; Driver program"
How can the above be solved in Java?

class GFG { static int minOperations ( int [ ] A , int n ) { if ( ( n & 1 ) > 0 ) return - 1 ; int zeros = 0 , consZeros = 0 , ones = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] == 0 ) zeros ++ ; else ones ++ ; if ( i + 1 < n ) { if ( A [ i ] == 0 && A [ i + 1 ] == 0 ) consZeros ++ ; } } if ( A [ 0 ] == A [ n - 1 ] && A [ 0 ] == 0 ) consZeros ++ ; if ( zeros == ones ) return consZeros ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] A = new int [ ] { 1 , 1 , 0 , 0 } ; int n = A . length ; System . out . println ( minOperations ( A , n ) ) ; } }


"Find minimum positive integer x such that a ( x ^ 2 ) + b ( x ) + c >= k | Java implementation of the approach ; Function to return the minimum positive integer satisfying the given equation ; Binary search to find the value of x ; Return the answer ; Driver code"
How can the above be solved in Java?

class GFG { static int MinimumX ( int a , int b , int c , int k ) { int x = Integer . MAX_VALUE ; if ( k <= c ) return 0 ; int h = k - c ; int l = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } public static void main ( String [ ] args ) { int a = 3 , b = 2 , c = 4 , k = 15 ; System . out . println ( MinimumX ( a , b , c , k ) ) ; } }


"Find minimum possible size of array with given rules for removing elements | Java program to find size of minimum possible array after removing elements according to given rules ; dp [ i ] [ j ] denotes the minimum number of elements left in the subarray arr [ i . . j ] . ; If already evaluated ; If size of array is less than 3 ; Initialize result as the case when first element is separated ( not removed using given rules ) ; Now consider all cases when first element forms a triplet and removed . Check for all possible triplets ( low , i , j ) ; Check if this triplet follows the given rules of removal . And elements between ' low ' and ' i ' , and between ' i ' and ' j ' can be recursively removed . ; Insert value in table and return result ; This function mainly initializes dp table and calls recursive function minSizeRec ; Driver code"
How can the above be solved in Java?

class GFG { static int MAX = 1000 ; static int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; static int minSizeRec ( int arr [ ] , int low , int high , int k ) { if ( dp [ low ] [ high ] != - 1 ) { return dp [ low ] [ high ] ; } if ( ( high - low + 1 ) < 3 ) { return high - low + 1 ; } int res = 1 + minSizeRec ( arr , low + 1 , high , k ) ; for ( int i = low + 1 ; i <= high - 1 ; i ++ ) { for ( int j = i + 1 ; j <= high ; j ++ ) { if ( arr [ i ] == ( arr [ low ] + k ) && arr [ j ] == ( arr [ low ] + 2 * k ) && minSizeRec ( arr , low + 1 , i - 1 , k ) == 0 && minSizeRec ( arr , i + 1 , j - 1 , k ) == 0 ) { res = Math . min ( res , minSizeRec ( arr , j + 1 , high , k ) ) ; } } } return ( dp [ low ] [ high ] = res ) ; } static int minSize ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return minSizeRec ( arr , 0 , n - 1 , k ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 4 } ; int n = arr . length ; int k = 1 ; System . out . println ( minSize ( arr , n , k ) ) ; } }


"Find minimum radius such that atleast k point lie inside the circle | Java program to find minimum radius such that atleast k point lie inside the circle ; Return minimum distance required so that aleast k point lie inside the circle . ; Finding distance between of each point from origin ; Sorting the distance ; Driven Program"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static int minRadius ( int k , int [ ] x , int [ ] y , int n ) { int [ ] dis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Arrays . sort ( dis ) ; return dis [ k - 1 ] ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] x = { 1 , - 1 , 1 } ; int [ ] y = { 1 , - 1 , - 1 } ; int n = x . length ; System . out . println ( minRadius ( k , x , y , n ) ) ; } }


"Find minimum shift for longest common prefix | Java program to find longest common prefix after rotation of second string . ; function for KMP search ; p [ 1 ] = 0 ; ; find out the longest prefix and position ; for new position with longer prefix in str2 update pos and len ; print result ; Driver code"
How can the above be solved in Java?

class GFG { static void KMP ( int m , int n , String str2 , String str1 ) { int pos = 0 , len = 0 ; int [ ] p = new int [ m + 1 ] ; int k = 0 ; char [ ] ch1 = str1 . toCharArray ( ) ; char [ ] ch2 = str2 . toCharArray ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && ch1 [ k ] != ch1 [ i - 1 ] ) k = p [ k ] ; if ( ch1 [ k ] == ch1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( int j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && j < n && ch1 [ j ] != ch2 [ i ] ) j = p [ j ] ; if ( j < n && ch1 [ j ] == ch2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } System . out . println ( " Shift ▁ = ▁ " + pos ) ; System . out . println ( " Prefix ▁ = ▁ " + str1 . substring ( 0 , len ) ) ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " forgeeksgeeks " ; int n = str1 . length ( ) ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ; } }


"Find minimum speed to finish all Jobs | Java program to find minimum speed to finish all jobs ; Function To findmax value in Array ; Function to check if the person can do all jobs in H hours with speed K ; Function to return the minimum speed of person to complete all jobs ; If H < N it is not possible to complete all jobs as person can not move from one element to another during current hour ; Max element of array ; Use binary search to find smallest K ; Driver Code ; Print required maxLenwer"
How can the above be solved in Java?

class GFG { static int findmax ( int [ ] A ) { int r = A [ 0 ] ; for ( int i = 1 ; i < A . length ; i ++ ) r = Math . max ( r , A [ i ] ) ; return r ; } static boolean isPossible ( int [ ] A , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } static int minJobSpeed ( int [ ] A , int n , int H ) { if ( H < n ) return - 1 ; int max = findmax ( A ) ; int lo = 1 , hi = max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 6 , 7 , 11 } ; int H = 8 ; int n = A . length ; System . out . println ( minJobSpeed ( A , n , H ) ) ; } }


"Find minimum sum of factors of number | Java program to find minimum sum of product of number ; To find minimum sum of product of number ; Find factors of number and add to the sum ; Return sum of numbers having minimum product ; Driver program to test above function"
How can the above be solved in Java?

public class Main { static int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } public static void main ( String [ ] args ) { int num = 12 ; System . out . println ( findMinSum ( num ) ) ; } }


"Find minimum sum such that one of every three consecutive elements is taken | A Dynamic Programming based java program to find minimum possible sum of elements of array such that an element out of every three consecutive is picked . ; A utility function to find minimum of 3 elements ; Returns minimum possible sum of elements such that an element out of every three consecutive elements is picked . ; Create a DP table to store results of subproblems . sum [ i ] is going to store minimum possible sum when arr [ i ] is part of the solution . ; When there are less than or equal to 3 elements ; Iterate through all other elements ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int minimum ( int a , int b , int c ) { return Math . min ( Math . min ( a , b ) , c ) ; } static int findMinSum ( int arr [ ] , int n ) { int sum [ ] = new int [ n ] ; sum [ 0 ] = arr [ 0 ] ; sum [ 1 ] = arr [ 1 ] ; sum [ 2 ] = arr [ 2 ] ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = arr [ i ] + minimum ( sum [ i - 3 ] , sum [ i - 2 ] , sum [ i - 1 ] ) ; return minimum ( sum [ n - 1 ] , sum [ n - 2 ] , sum [ n - 3 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 20 , 2 , 10 , 1 } ; int n = arr . length ; System . out . println ( " Min ▁ Sum ▁ is ▁ " + findMinSum ( arr , n ) ) ; } }


"Find minimum time to finish all jobs with given constraints | Java program to find minimum time to finish all jobs with given number of assignees ; Utility function to get maximum element in job [ 0. . n - 1 ] ; Returns true if it is possible to finish jobs [ ] within given time ' time ' ; cnt is count of current assignees required for jobs ; time assigned to current assignee ; If time assigned to current assignee exceeds max , increment count of assignees . ; Else add time of job to current time and move to next job . ; Returns true if count is smaller than k ; Returns minimum time required to finish given array of jobs k -- > number of assignees T -- > Time required by every assignee to finish 1 unit m -- > Number of jobs ; Set start and end for binary search end provides an upper limit on time ; Initialize answer ; Find the job that takes maximum time ; Do binary search for minimum feasible time ; If it is possible to finish jobs in mid time ; Update answer ; Driver program"
How can the above be solved in Java?

class GFG { static int getMax ( int arr [ ] , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } static boolean isPossible ( int time , int K , int job [ ] , int n ) { int cnt = 1 ; int curr_time = 0 ; for ( int i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } static int findMinTime ( int K , int T , int job [ ] , int n ) { int end = 0 , start = 0 ; for ( int i = 0 ; i < n ; ++ i ) end += job [ i ] ; int ans = end ; int job_max = getMax ( job , n ) ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } public static void main ( String arg [ ] ) { int job [ ] = { 10 , 7 , 8 , 12 , 6 , 8 } ; int n = job . length ; int k = 4 , T = 5 ; System . out . println ( findMinTime ( k , T , job , n ) ) ; } }


"Find minimum value of the expression by choosing K elements from given array | JAVA program to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Function to find the minimum possible of the expression by choosing exactly K ( ? N ) integers form given array arr ; Sorting the array for least k element selection ; Select first k elements from sorted array ; Return value of solved expression ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minimumValue ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 21 , 5 , 3 , 8 } , k = 3 ; int n = arr . length ; System . out . print ( minimumValue ( arr , n , k ) ) ; } }


"Find minimum value of y for the given x values in Q queries from all the given set of lines | Java implementation of the above approach ; Sort the line in decreasing order of their slopes ; If slopes arent equal ; If the slopes are equal ; Checks if line L3 or L1 is better than L2 Intersection of Line 1 and Line 2 has x - coordinate ( b1 - b2 ) / ( m2 - m1 ) Similarly for Line 1 and Line 3 has x - coordinate ( b1 - b3 ) / ( m3 - m1 ) Cross multiplication will give the below result ; To store the lines ; Add the line to the set of lines ; To check if after adding the new line whether old lines are losing significance or not ; Add the present line ; Function to return the y coordinate of the specified line for the given coordinate ; Function to Return the minimum value of y for the given x coordinate ; If there is no lines ; Binary search ; Driver code ; Sort the lines ; Add the lines ; For each query in Q"
How can the above be solved in Java?

import java . util . ArrayList ; import java . util . Arrays ; class GFG { static class Line implements Comparable < Line > { int m , c ; public Line ( int m , int c ) { this . m = m ; this . c = c ; } @ Override public int compareTo ( Line l ) { if ( m != l . m ) return l . m - this . m ; else return l . c - this . c ; } boolean check ( Line L1 , Line L2 , Line L3 ) { return ( L3 . c - L1 . c ) * ( L1 . m - L2 . m ) < ( L2 . c - L1 . c ) * ( L1 . m - L3 . m ) ; } } static class Convex_HULL_Trick { ArrayList < Line > l = new ArrayList < > ( ) ; void add ( Line newLine ) { int n = l . size ( ) ; while ( n >= 2 && newLine . check ( l . get ( n - 2 ) , l . get ( n - 1 ) , newLine ) ) { n -- ; } l = new Line [ n ] ; l . add ( newLine ) ; } int value ( int in , int x ) { return l . get ( in ) . m * x + l . get ( in ) . c ; } int minQuery ( int x ) { if ( l . isEmpty ( ) ) return Integer . MAX_VALUE ; int low = 0 , high = ( int ) l . size ( ) - 2 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( value ( mid , x ) > value ( mid + 1 , x ) ) low = mid + 1 ; else high = mid - 1 ; } return value ( low , x ) ; } } ; public static void main ( String [ ] args ) { Line [ ] lines = { new Line ( 1 , 1 ) , new Line ( 0 , 0 ) , new Line ( - 3 , 3 ) } ; int Q [ ] = { - 2 , 2 , 0 } ; int n = 3 , q = 3 ; Convex_HULL_Trick cht = new Convex_HULL_Trick ( ) ; Arrays . sort ( lines ) ; for ( int i = 0 ; i < n ; i ++ ) cht . add ( lines [ i ] ) ; for ( int i = 0 ; i < q ; i ++ ) { int x = Q [ i ] ; System . out . println ( cht . minQuery ( x ) ) ; } } }


"Find minimum value to assign all array elements so that array product becomes greater | Java program to find minimum value that can be assigned to along elements so that product becomes greater than current product . ; sort the array to apply Binary search ; using log property add every logarithmic value of element to val double val = 0 ; where double is long double ; set left and right extremities to find min value ; multiplying n to mid , to find the correct min value ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG1 { static long findMinValue ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { long arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . length ; System . out . println ( findMinValue ( arr , n ) ) ; } }


"Find minimum x such that ( x % k ) * ( x / k ) == n | Java Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate over all possible remainders ; it must divide n ; Driver Code to test above function"
How can the above be solved in Java?

class Solution { static int minimumX ( int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int rem = k - 1 ; rem > 0 ; rem -- ) { if ( n % rem == 0 ) ans = Math . min ( ans , rem + ( n / rem ) * k ) ; } return ans ; } public static void main ( String args [ ] ) { int n = 4 , k = 6 ; System . out . println ( minimumX ( n , k ) ) ; n = 5 ; k = 5 ; System . out . println ( minimumX ( n , k ) ) ; } }


"Find minimum x such that ( x % k ) * ( x / k ) == n | Set | Java Program to find the minimum positive X such that the given equation holds true ; This function gives the required answer ; Iterate for all the factors ; Check if i is a factor ; Consider i to be A and n / i to be B ; Consider i to be B and n / i to be A ; Driver Code to test above function"
How can the above be solved in Java?

import java . util . * ; class solution { static int minimumX ( int n , int k ) { int mini = Integer . MAX_VALUE ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int fir = i ; int sec = n / i ; int num1 = fir * k + sec ; int res = ( num1 / k ) * ( num1 % k ) ; if ( res == n ) mini = Math . min ( num1 , mini ) ; int num2 = sec * k + fir ; res = ( num2 / k ) * ( num2 % k ) ; if ( res == n ) mini = Math . min ( num2 , mini ) ; } } return mini ; } public static void main ( String args [ ] ) { int n = 4 , k = 6 ; System . out . println ( minimumX ( n , k ) ) ; n = 5 ; k = 5 ; System . out . println ( minimumX ( n , k ) ) ; } }


"Find mirror image of a point in 2 | Java code to find mirror image ; function which finds coordinates of mirror image . This function return a pair of double ; Driver code"
How can the above be solved in Java?

class GFG { static class pair { double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static pair mirrorImage ( double a , double b , double c , double x1 , double y1 ) { double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return new pair ( x , y ) ; } public static void main ( String [ ] args ) { double a = - 1.0 ; double b = 1.0 ; double c = 0.0 ; double x1 = 1.0 ; double y1 = 0.0 ; pair image = mirrorImage ( a , b , c , x1 , y1 ) ; System . out . print ( " Image ▁ of ▁ point ▁ ( " + x1 + " , ▁ " + y1 + " ) ▁ " ) ; System . out . print ( " by ▁ mirror ▁ ( " + a + " ) x ▁ + ▁ ( " + b + " ) y ▁ + ▁ ( " + c + " ) ▁ = ▁ 0 , ▁ is ▁ : " ) ; System . out . println ( " ( " + image . first + " , ▁ " + image . second + " ) " ) ; } }


"Find mirror of a given node in Binary tree | Java program to find the mirror Node in Binary tree ; A binary tree Node has data , pointer to left child and a pointer to right child ; create new Node and initialize it ; recursive function to find mirror of Node ; if any of the Node is none then Node itself and decendent have no mirror , so return none , no need to further explore ! ; if left Node is target Node , then return right 's key (that is mirror) and vice   versa  ; first recur external Nodes ; if no mirror found , recur internal Nodes ; interface for mirror search ; Driver ; target Node whose mirror have to be searched"
How can the above be solved in Java?

class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node n = new Node ( ) ; n . key = key ; n . left = null ; n . right = null ; return n ; } static int findMirrorRec ( int target , Node left , Node right ) { if ( left == null right == null ) return 0 ; if ( left . key == target ) return right . key ; if ( right . key == target ) return left . key ; int mirror_val = findMirrorRec ( target , left . left , right . right ) ; if ( mirror_val != 0 ) return mirror_val ; return findMirrorRec ( target , left . right , right . left ) ; } static int findMirror ( Node root , int target ) { if ( root == null ) return 0 ; if ( root . key == target ) return target ; return findMirrorRec ( target , root . left , root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . right = newNode ( 7 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . left = newNode ( 8 ) ; root . right . left . right = newNode ( 9 ) ; int target = root . left . left . key ; int mirror = findMirror ( root , target ) ; if ( mirror != 0 ) System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ Node ▁ " + mirror ) ; else System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ null ▁ " ) ; } }


"Find missing element in a sorted array of consecutive numbers | Java implementation of the approach ; Function to return the missing element ; Check if middle element is consistent ; No inconsistency till middle elements When missing element is just after the middle element ; Move right ; Inconsistency found When missing element is just before the middle element ; Move left ; No missing element found ; Driver code"
How can the above be solved in Java?

class GFG { public static int findMissing ( int arr [ ] , int n ) { int l = 0 , h = n - 1 ; int mid ; while ( h > l ) { mid = l + ( h - l ) / 2 ; if ( arr [ mid ] - mid == arr [ 0 ] ) { if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) return arr [ mid ] + 1 ; else { l = mid + 1 ; } } else { if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) return arr [ mid ] - 1 ; else { h = mid - 1 ; } } } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 } ; int n = arr . length ; System . out . print ( findMissing ( arr , n ) ) ; } }


"Find missing number in another array which is shuffled copy | Java implementation to find the missing number in shuffled array ; Returns the missing number Size of arr2 [ ] is n - 1 ; Missing number ' mnum ' ; 1 st array is of size ' n ' ; 2 nd array is of size ' n ▁ - ▁ 1' ; Required missing number ; Driver Code"
How can the above be solved in Java?

class GFG { static int missingNumber ( int arr1 [ ] , int arr2 [ ] , int n ) { int mnum = 0 ; for ( int i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 4 , 8 , 1 , 3 , 7 } ; int arr2 [ ] = { 7 , 4 , 3 , 1 } ; int n = arr1 . length ; System . out . println ( " Missing ▁ number ▁ = ▁ " + missingNumber ( arr1 , arr2 , n ) ) ; } }


"Find modular node in a linked list | A Java program to find modular node in a linked list ; A Linkedlist node ; Function to find modular node in the linked list ; Corner cases ; Traverse the given list ; Driver code to test above function"
How can the above be solved in Java?

public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; System . out . print ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) System . out . println ( answer . data ) ; else System . out . println ( " null " ) ; } }


"Find multiple of x closest to or a ^ b ( a raised to power b ) | java Program to find closest multiple of x to a ^ b ; function to find closest multiple of x to a ^ b ; calculate a ^ b / x ; Answer is either ( ans * x ) or ( ans + 1 ) * x ; Printing nearest answer ; Driver Program"
How can the above be solved in Java?

import java . io . * ; public class GFG { static void multiple ( int a , int b , int x ) { if ( b < 0 ) { if ( a == 1 && x == 1 ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } int mul = ( int ) Math . pow ( a , b ) ; int ans = mul / x ; int ans1 = x * ans ; int ans2 = x * ( ans + 1 ) ; System . out . println ( ( ( mul - ans1 ) <= ( ans2 - mul ) ) ? ans1 : ans2 ) ; } static public void main ( String [ ] args ) { int a = 349 , b = 1 , x = 4 ; multiple ( a , b , x ) ; } }


"Find n positive integers that satisfy the given equations | Java implementation of the approach ; Function to find n positive integers that satisfy the given conditions ; To store n positive integers ; Place N - 1 one 's ; If can not place ( y - ( n - 1 ) ) as the Nth integer ; Place Nth integer ; To store the sum of squares of N integers ; If it is less than x ; Print the required integers ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void findIntegers ( int n , int x , int y ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans . add ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { System . out . print ( " - 1" ) ; return ; } ans . add ( y - ( n - 1 ) ) ; int store = 0 ; for ( int i = 0 ; i < n ; i ++ ) store += ans . get ( i ) * ans . get ( i ) ; if ( store < x ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ; } }


"Find n | Java Program to calculate nth term of a series ; func for calualtion ; for summation of square of first n - natural nos . ; summation of first n natural nos . ; return result ; Driver Code"
How can the above be solved in Java?

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


"Find n | Java program for nth nodes of inorder traversals ; A binary tree node has data , pointer to left child and a pointer to right child ; Helper function that allocates a new node with the given data and null left and right pointers . ; Given a binary tree , print its nth nodes of inorder ; first recur on left child ; when count = n then print element ; now recur on right child ; Driver program to test above functions"
How can the above be solved in Java?

import java . util . * ; class Solution { static int count = 0 ; static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void NthInorder ( Node node , int n ) { if ( node == null ) return ; if ( count <= n ) { NthInorder ( node . left , n ) ; count ++ ; if ( count == n ) System . out . printf ( " % d ▁ " , node . data ) ; NthInorder ( node . right , n ) ; } } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; int n = 4 ; NthInorder ( root , n ) ; } }


"Find n | Java program to find n - th Fortunate number ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to Find primorial of order n ( product of first n prime numbers ) . ; Function to find next prime number greater than n ; Note that difference ( or m ) should be greater than 1. ; loop continuously until isPrime returns true for a number above n ; Ignoring the prime number that is 1 greater than n ; Returns n - th Fortunate number ; Driver function"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { public static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } public static int primorial ( int n ) { int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } public static int findNextPrime ( int n ) { int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } public static int fortunateNumber ( int n ) { int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( fortunateNumber ( n ) ) ; } }


"Find n | Java program to find n - th node of Postorder Traversal of Binary Tree ; A binary tree node structure ; function to find the N - th node in the postorder traversal of a given binary tree ; left recursion ; right recursion ; prints the n - th node of preorder traversal ; driver code ; prints n - th node found"
How can the above be solved in Java?

public class NthNodePostOrder { static int flag = 0 ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) System . out . print ( root . data ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } }


"Find n | Java program to find n - th number in a series made of digits 4 and 7 ; Return n - th number in series made of 4 and 7 ; create an array of size ( n + 1 ) ; If i is odd ; main function"
How can the above be solved in Java?

class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( printNthElement ( n ) ) ; } }


"Find n | Java program to find n - th term of series 1 , 3 , 6 , 10 , 15 , 21. . . ; Function to find the nth term of series ; Loop to add numbers ; Driver code"
How can the above be solved in Java?

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


"Find n | Java program to find nth term ; utility function ; since first element of the series is 7 , we initialise a variable with 7 ; Using iteration to find nth term ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class GFG { static int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( findTerm ( n ) ) ; } }


"Find n | Java program to find the n - th term in series 1 3 6 10 . . . ; Function to find nth term ; Driver Code"
How can the above be solved in Java?

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


"Find n | Java program to find the nth element of Stern 's Diatomic Series ; function to find nth stern '  diatomic series ; Initializing the DP array ; SET the Base case ; Traversing the array from 2 nd Element to nth Element ; Case 1 : for even n ; Case 2 : for odd n ; Driver program"
How can the above be solved in Java?

class GFG { static int findSDSFunc ( int n ) { int DP [ ] = new int [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( findSDSFunc ( n ) ) ; } }


"Find n | Java program to find the nth term of the series 1 2 2 3 3 3 ... ; function to solve the quadratic equation ; calculating the Nth term ; driver code to check the above function"
How can the above be solved in Java?

import java . io . * ; class Series { static int term ( int n ) { int x = ( ( ( 1 ) + ( int ) Math . sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( term ( n ) ) ; } }


"Find n | Java program to find the value at n - th place in the given sequence ; Definition of findNumber function ; Finding x from equation n = x ( x + 1 ) / 2 + 1 ; Base of current block ; Value of n - th element ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int findNumber ( int n ) { int x = ( int ) Math . floor ( ( - 1 + Math . sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; int base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - base + 1 ; } public static void main ( String [ ] args ) { int n = 55 ; System . out . println ( findNumber ( n ) ) ; } }


"Find n | Java program to find the value at n - th place in the given sequence ; Returns n - th number in sequence 1 , 1 , 2 , 1 , 2 , 3 , 1 , 2 , 4 , ... ; One by one subtract counts elements in different blocks ; Driver Code"
How can the above be solved in Java?

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


"Find n | Java program to print n - th permutation ; Utility for calculating factorial ; Function for nth permutation ; length of given string ; Count frequencies of all characters ; out string for output string ; Iterate till sum equals n ; We update both n and sum in this loop . ; Check for characters present in freq [ ] ; Remove character ; calculate sum after fixing a particular char ; if sum > n fix that char as present char and update sum and required nth after fixing char at that position ; if sum < n , add character back ; if sum == n means this char will provide its greatest permutation as nth permutation ; append string termination character and print result ; Driver program to test above method"
How can the above be solved in Java?

public class PermuteString { final static int MAX_CHAR = 26 ; final static int MAX_FACT = 20 ; static long fact [ ] = new long [ MAX_FACT ] ; static void precomputeFactorirals ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static void nPermute ( String str , int n ) { precomputeFactorirals ( ) ; int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < len ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; String out = " " ; int sum = 10 ; int k = 0 ; while ( sum >= n ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; int xsum = ( int ) fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= fact [ freq [ j ] ] ; sum += xsum ; if ( sum >= n ) { out += ( char ) ( i + ' a ' ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { out += ( char ) ( i + ' a ' ) ; freq [ i ++ ] -- ; } System . out . println ( out ) ; } public static void main ( String [ ] args ) { int n = 2 ; String str = " geeksquiz " ; nPermute ( str , n ) ; } }


"Find n | Program to find n - th element in the series 9 , 33 , 73 , 128. . ; Returns n - th element of the series ; driver program to test the above function"
How can the above be solved in Java?

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


"Find n | java program to find n - th number containing only 4 and 7. ; If n is odd , append 4 and move to parent ; If n is even , append 7 and move to parent ; Reverse res and return . ; Driver code"
How can the above be solved in Java?

public class GFG { static String findNthNo ( int n ) { String res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } StringBuilder sb = new StringBuilder ( res ) ; sb . reverse ( ) ; return new String ( sb ) ; } public static void main ( String args [ ] ) { int n = 13 ; System . out . print ( findNthNo ( n ) ) ; } }


"Find next Smaller of next Greater in an array | Java Program to find Right smaller element of next greater element ; function find Next greater element ; create empty stack ; Traverse all array elements in reverse order order == ' G ' we compute next greater elements of every element order == ' S ' we compute right smaller element of every element ; Keep removing top element from S while the top element is smaller then or equal to arr [ i ] ( if Key is G ) element is greater then or equal to arr [ i ] ( if order is S ) ; store the next greater element of current element ; If all elements in S were smaller than arr [ i ] ; Push this element ; Function to find Right smaller element of next greater element ; stores indexes of next greater elements ; stores indexes of right smaller elements ; Find next greater element Here G indicate next greater element ; Find right smaller element using same function nextGreater ( ) Here S indicate right smaller elements ; If NG [ i ] = = - 1 then there is no smaller element on right side . We can find Right smaller of next greater by arr [ RS [ NG [ i ] ] ] ; Driver code"
How can the above be solved in Java?

import java . util . Stack ; public class Main { public static void nextGreater ( int arr [ ] , int next [ ] , char order ) { Stack < Integer > stack = new Stack < > ( ) ; for ( int i = arr . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && ( ( order == ' G ' ) ? arr [ stack . peek ( ) ] <= arr [ i ] : arr [ stack . peek ( ) ] >= arr [ i ] ) ) stack . pop ( ) ; if ( ! stack . isEmpty ( ) ) next [ i ] = stack . peek ( ) ; else next [ i ] = - 1 ; stack . push ( i ) ; } } public static void nextSmallerOfNextGreater ( int arr [ ] ) { int NG [ ] = new int [ arr . length ] ; int RS [ ] = new int [ arr . length ] ; nextGreater ( arr , NG , ' G ' ) ; nextGreater ( arr , RS , ' S ' ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( NG [ i ] != - 1 && RS [ NG [ i ] ] != - 1 ) System . out . print ( arr [ RS [ NG [ i ] ] ] + " ▁ " ) ; else System . out . print ( " - 1 ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 9 , 2 , 5 , 1 , 7 } ; nextSmallerOfNextGreater ( arr ) ; } }


"Find next palindrome prime | Java program to find next palindromic prime for a given number . ; if ( 8 <= N <= 11 ) return 11 ; generate odd length palindrome number which will cover given constraint . ; if y >= N and it is a prime number then return it . ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class Geeks { static boolean isPrime ( int num ) { if ( num < 2 num % 2 == 0 ) return num == 2 ; for ( int i = 3 ; i * i <= num ; i += 2 ) if ( num % i == 0 ) return false ; return true ; } static int primePalindrome ( int N ) { if ( 8 <= N && N <= 11 ) return 11 ; for ( int x = 1 ; x < 100000 ; ++ x ) { String s = Integer . toString ( x ) ; StringBuffer buffer = new StringBuffer ( s ) ; buffer . reverse ( ) ; int y = Integer . parseInt ( s + buffer . substring ( 1 ) . toString ( ) ) ; if ( y >= N && isPrime ( y ) == true ) return y ; } return - 1 ; } public static void main ( String args [ ] ) { System . out . print ( primePalindrome ( 112 ) ) ; } }


"Find next right node of a given key | Set 2 | Java program to find next right of a given key using preorder traversal ; A Binary Tree Node ; Utility function to create a new tree node ; Function to find next node for given node in same level in a binary tree by using pre - order traversal ; return null if tree is empty ; if desired node is found , set value [ 0 ] to current level ; if value [ 0 ] is already set , then current node is the next right node ; recurse for left subtree by increasing level by 1 ; if node is found in left subtree , return it ; recurse for right subtree by increasing level by 1 ; Function to find next node of given node in the same level in given binary tree ; A utility function to test above functions ; Driver program to test above functions ; Let us create binary tree given in the above example"
How can the above be solved in Java?

import java . util . * ; class GfG { static class Node { Node left , right ; int key ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static Node nextRightNode ( Node root , int k , int level , int [ ] value ) { if ( root == null ) return null ; if ( root . key == k ) { value [ 0 ] = level ; return null ; } else if ( value [ 0 ] != 0 ) { if ( level == value [ 0 ] ) return root ; } Node leftNode = nextRightNode ( root . left , k , level + 1 , value ) ; if ( leftNode != null ) return leftNode ; return nextRightNode ( root . right , k , level + 1 , value ) ; } static Node nextRightNodeUtil ( Node root , int k ) { int [ ] v = new int [ 1 ] ; v [ 0 ] = 0 ; return nextRightNode ( root , k , 1 , v ) ; } static void test ( Node root , int k ) { Node nr = nextRightNodeUtil ( root , k ) ; if ( nr != null ) System . out . println ( " Next ▁ Right ▁ of ▁ " + k + " ▁ is ▁ " + nr . key ) ; else System . out . println ( " No ▁ next ▁ right ▁ node ▁ found ▁ for ▁ " + k ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 6 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 4 ) ; test ( root , 10 ) ; test ( root , 2 ) ; test ( root , 6 ) ; test ( root , 5 ) ; test ( root , 8 ) ; test ( root , 4 ) ; } }


"Find node U containing all nodes from a set V at atmost distance 1 from the path from root to U | Java program for the above approach ; To store the time ; Function to perform DFS to store times , distance and parent of each node ; Update the distance of node u ; Update parent of node u ; Increment time timeT ; Discovery time of node u ; Traverse the adjacency list of current node and recursively call DFS for each vertex ; If current node Adj [ u ] [ i ] is unvisited ; Update the finishing time ; Function to add edges between nodes u and v ; Function to find the node U such that path from root to U contains nodes in V [ ] ; Initialise vis , dis , parent , preTime , and postTime ; Store Adjacency List ; Create adjacency List ; Perform DFS Traversal ; Stores the distance of deepest vertex ' u ' ; Update the deepest node by traversing the qu [ ] ; Find deepest vertex ; Replace each vertex with it 's  corresponding parent except  the root vertex ; Checks if the ancestor with respect to deepest vertex u ; Update ans ; Print the result ; Driver Code ; Total vertices ; Given set of vertices ; Given edges ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int timeT = 0 ; static void dfs ( int u , int p , int dis , int vis [ ] , int distance [ ] , int parent [ ] , int preTime [ ] , int postTime [ ] , ArrayList < ArrayList < Integer > > Adj ) { distance [ u ] = dis ; parent [ u ] = p ; vis [ u ] = 1 ; timeT ++ ; preTime [ u ] = timeT ; for ( int i = 0 ; i < Adj . get ( u ) . size ( ) ; i ++ ) { if ( vis [ Adj . get ( u ) . get ( i ) ] == 0 ) { dfs ( Adj . get ( u ) . get ( i ) , u , dis + 1 , vis , distance , parent , preTime , postTime , Adj ) ; } } timeT ++ ; postTime [ u ] = timeT ; } static void addEdge ( ArrayList < ArrayList < Integer > > Adj , int u , int v ) { Adj . get ( u ) . add ( v ) ; Adj . get ( v ) . add ( u ) ; } static void findNodeU ( int N , int V , int Vertices [ ] , int Edges [ ] [ ] ) { int vis [ ] = new int [ N + 1 ] ; int distance [ ] = new int [ N + 1 ] ; int parent [ ] = new int [ N + 1 ] ; int preTime [ ] = new int [ N + 1 ] ; int postTime [ ] = new int [ N + 1 ] ; ArrayList < ArrayList < Integer > > Adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . add ( new ArrayList < Integer > ( ) ) ; int u = 0 , v ; for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdge ( Adj , Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } dfs ( 1 , 0 , 0 , vis , distance , parent , preTime , postTime , Adj ) ; int maximumDistance = 0 ; maximumDistance = 0 ; for ( int k = 0 ; k < V ; k ++ ) { if ( maximumDistance < distance [ Vertices [ k ] ] ) { maximumDistance = distance [ Vertices [ k ] ] ; u = Vertices [ k ] ; } if ( parent [ Vertices [ k ] ] != 0 ) { Vertices [ k ] = parent [ Vertices [ k ] ] ; } } boolean ans = true ; boolean flag ; for ( int k = 0 ; k < V ; k ++ ) { if ( preTime [ Vertices [ k ] ] <= preTime [ u ] && postTime [ Vertices [ k ] ] >= postTime [ u ] ) flag = true ; else flag = false ; ans = ans & flag ; } if ( ans ) System . out . println ( u ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 10 ; int V = 5 ; int Vertices [ ] = { 4 , 3 , 8...


"Find non | Java program for the above approach ; Function to find the possible output array ; Base case for the recursion ; If ind becomes half of the size then print the array . ; Exit the function . ; Iterate in the range . ; Put the values in the respective indices . ; Call the function to find values for other indices . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int INF64 = ( int ) 1e10 ; static int N = 200 * 1000 + 13 ; static int n ; static int arr [ ] = new int [ N ] ; static int brr [ ] = new int [ N ] ; static void brute ( int ind , int l , int r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < ( int ) n ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; System . exit ( 0 ) ; } for ( int i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } public static void main ( String [ ] args ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; } }


"Find nth Fibonacci number using Golden ratio | Java program to find n - th Fibonacci number ; Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; Driver code"
How can the above be solved in Java?

class GFG { static double PHI = 1.6180339 ; static int f [ ] = { 0 , 1 , 1 , 2 , 3 , 5 } ; static int fib ( int n ) { if ( n < 6 ) return f [ n ] ; int t = 5 ; int fn = 5 ; while ( t < n ) { fn = ( int ) Math . round ( fn * PHI ) ; t ++ ; } return fn ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( n + " th ▁ Fibonacci ▁ Number ▁ = ▁ " + fib ( n ) ) ; } }


"Find nth Hermite number | Java program to find nth Hermite number ; Function to return nth Hermite number ; Base condition ; Driver Code ; Print nth Hermite number"
How can the above be solved in Java?

import java . util . * ; class GFG { static int getHermiteNumber ( int n ) { if ( n == 0 ) return 1 ; else if ( n == 1 ) return 1 ; else return - 2 * ( n - 1 ) * getHermiteNumber ( n - 2 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( getHermiteNumber ( n ) ) ; } }


"Find nth Hermite number | Java program to find nth Hermite number ; Utility function to calculate double factorial of a number ; Function to return nth Hermite number ; If n is even then return 0 ; If n is odd ; Calculate double factorial of ( n - 1 ) and multiply it with 2 ^ ( n / 2 ) ; If n / 2 is odd then nth Hermite number will be negative ; Return nth Hermite number ; Driver Code ; Print nth Hermite number"
How can the above be solved in Java?

import java . util . * ; class GFG { static int doubleFactorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } static int hermiteNumber ( int n ) { if ( n % 2 == 1 ) return 0 ; else { int number = ( int ) ( Math . pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * - 1 ; return number ; } } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( hermiteNumber ( n ) ) ; } }


"Find nth Magic Number | Java program to find nth magic number ; Function to find nth magic number ; Go through every bit of n ; If last bit of n is set ; proceed to next bit or n = n / 2 ; Driver program to test above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int nthMagicNo ( int n ) { int pow = 1 , answer = 0 ; while ( n != 0 ) { pow = pow * 5 ; if ( ( int ) ( n & 1 ) == 1 ) answer += pow ; n >>= 1 ; } return answer ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " nth ▁ magic " + " ▁ number ▁ is ▁ " + nthMagicNo ( n ) ) ; } }


"Find nth term of a given recurrence relation | Java program to find nth term of a given recurrence relation ; function to return required value ; Get the answer ; Return the answer ; Driver code ; Get the value of n ; function call to print result"
How can the above be solved in Java?

import java . util . * ; class solution { static int sum ( int n ) { int ans = ( n * ( n - 1 ) ) / 2 ; return ans ; } public static void main ( String arr [ ] ) { int n = 5 ; System . out . println ( sum ( n ) ) ; } }


"Find nth term of the Dragon Curve Sequence | Java code to find nth term of the Dragon Curve Sequence ; function to generate the nth term ; first term ; generating each term of the sequence ; loop to generate the ith term ; add character from the original string ; add alternate 0 and 1 in between ; if previous added term was '0' then add '1' ; now current term becomes previous term ; if previous added term was '1' , then add '0' ; now current term becomes previous term ; s becomes the ith term of the sequence ; Driver program ; Taking inputs ; generate nth term of dragon curve sequence ; Printing output"
How can the above be solved in Java?

import java . util . * ; class solution { static String Dragon_Curve_Sequence ( int n ) { String s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { String temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { temp += s . charAt ( j ) ; if ( prev == '0' ) { temp += one ; prev = one ; } else { temp += zero ; prev = zero ; } } s = temp ; } return s ; } public static void main ( String args [ ] ) { int n = 4 ; String s = Dragon_Curve_Sequence ( n ) ; System . out . println ( s ) ; } }


"Find nth term of the series 5 2 13 41 | Java program to find nth term of the series 5 2 13 41 ; function to calculate nth term of the series ; to store the nth term of series ; if n is even number ; if n is odd number ; return nth term ; Driver code"
How can the above be solved in Java?

import java . lang . Math ; class GFG { static long nthTermOfTheSeries ( int n ) { long nthTerm ; if ( n % 2 == 0 ) nthTerm = ( long ) Math . pow ( n - 1 , 2 ) + n ; else nthTerm = ( long ) Math . pow ( n + 1 , 2 ) + n ; return nthTerm ; } public static void main ( String [ ] args ) { int n ; n = 8 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 12 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 102 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 999 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 9999 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; } }