"Alternate bits of two numbers to create a new number | java Program to generate a number using alternate bits of two numbers . ; set even bit of number n ; res for store 101010. . number ; generate number form of 101010. . ... till temp size ; if bit is even then generate number and or with res ; return set even bit number ; set odd bit of number m ; res for store 101010. . number ; generate number form of 101010. . . . till temp size ; if bit is even then generate number and or with res ; return set odd bit number ; set even bit of number n ; set odd bit of number m ; take OR with these number ; Driver code ; n = 1 0 1 0 ^ ^ m = 1 0 1 1 ^ ^ result = 1 0 1 1"
How can the above be solved in Java?

import java . io . * ; class GFG { static int setevenbits ( int n ) { int temp = n ; int count = 0 ; int res = 0 ; for ( temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n & res ) ; } static int setoddbits ( int m ) { int count = 0 ; int res = 0 ; for ( int temp = m ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( m & res ) ; } static int getAlternateBits ( int n , int m ) { int tempn = setevenbits ( n ) ; int tempm = setoddbits ( m ) ; return ( tempn tempm ) ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 11 ; System . out . println ( getAlternateBits ( n , m ) ) ; } }


"Alternate vowel and consonant string | Java implementation of alternate vowel and consonant string ; ' ch ' is vowel or not ; create alternate vowel and consonant string str1 [ 0. . . l1 - 1 ] and str2 [ start ... l2 - 1 ] ; first adding character of vowel / consonant then adding character of consonant / vowel ; function to find the required alternate vowel and consonant string ; count vowels and update vowel string ; count consonants and update consonant string ; no such string can be formed ; remove first character of vowel string then create alternate string with cstr [ 0. . . nc - 1 ] and vstr [ 1. . . nv - 1 ] ; remove first character of consonant string then create alternate string with vstr [ 0. . . nv - 1 ] and cstr [ 1. . . nc - 1 ] ; if both vowel and consonant strings are of equal length start creating string with consonant ; start creating string with vowel ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; return false ; } static String createAltStr ( String str1 , String str2 , int start , int l ) { String finalStr = " " ; for ( int i = 0 , j = start ; j < l ; i ++ , j ++ ) finalStr = ( finalStr + str1 . charAt ( i ) ) + str2 . charAt ( j ) ; return finalStr ; } static String findAltStr ( String str ) { int nv = 0 , nc = 0 ; String vstr = " " , cstr = " " ; int l = str . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { char ch = str . charAt ( i ) ; if ( isVowel ( ch ) ) { nv ++ ; vstr = vstr + ch ; } else { nc ++ ; cstr = cstr + ch ; } } if ( Math . abs ( nv - nc ) >= 2 ) return " no ▁ such ▁ string " ; if ( nv > nc ) return ( vstr . charAt ( 0 ) + createAltStr ( cstr , vstr , 1 , nv ) ) ; if ( nc > nv ) return ( cstr . charAt ( 0 ) + createAltStr ( vstr , cstr , 1 , nc ) ) ; if ( cstr . charAt ( 0 ) < vstr . charAt ( 0 ) ) return createAltStr ( cstr , vstr , 0 , nv ) ; return createAltStr ( vstr , cstr , 0 , nc ) ; } public static void main ( String args [ ] ) { String str = " geeks " ; System . out . println ( findAltStr ( str ) ) ; } }


"Alternating Numbers | Java program for the above approach ; Function to check if a string is of the form even odd even odd ... ; Function to check if a string is of the form odd even odd even ... ; Function to check if n is an alternating number ; Driver Code ; Given number N ; Function call"
How can the above be solved in Java?

class GFG { static boolean isEvenOddForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 0 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 1 ) return false ; } return true ; } static boolean isOddEvenForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 1 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 0 ) return false ; } return true ; } static boolean isAlternating ( int n ) { String str = Integer . toString ( n ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } public static void main ( String [ ] args ) { int N = 129 ; if ( isAlternating ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Alternating split of a given Singly Linked List | Set 1 |  ; points to the last node in ' a ' ; points to the last node in ' b ' ; add at ' a ' tail ; advance the ' a ' tail"
How can the above be solved in Java?

static void AlternatingSplit ( Node source , Node aRef , Node bRef ) { Node aDummy = new Node ( ) ; Node aTail = aDummy ; Node bDummy = new Node ( ) ; Node bTail = bDummy ; Node current = source ; aDummy . next = null ; bDummy . next = null ; while ( current != null ) { MoveNode ( ( aTail . next ) , current ) ; aTail = aTail . next ; if ( current != null ) { MoveNode ( ( bTail . next ) , current ) ; bTail = bTail . next ; } } aRef = aDummy . next ; bRef = bDummy . next ; }


"Alternatively Merge two Strings in Java | Java code to alternatively merge two strings ; Function for alternatively merging two strings ; To store the final string ; For every index in the strings ; First choose the ith character of the first string if it exists ; Then choose the ith character of the second string if it exists ; Driver code"
How can the above be solved in Java?

public class mergeString { public static String merge ( String s1 , String s2 ) { StringBuilder result = new StringBuilder ( ) ; for ( int i = 0 ; i < s1 . length ( ) || i < s2 . length ( ) ; i ++ ) { if ( i < s1 . length ( ) ) result . append ( s1 . charAt ( i ) ) ; if ( i < s2 . length ( ) ) result . append ( s2 . charAt ( i ) ) ; } return result . toString ( ) ; } public static void main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " forgeeks " ; System . out . println ( merge ( s1 , s2 ) ) ; } }


"Altitude of largest Triangle that can be inscribed in a Rectangle | C ++ program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Stores the maximum altitude value ; Return res ; Driver Code ; Given Input ; Function call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int res = Math . min ( B / 2 , L ) ; return res ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 4 ; System . out . print ( largestAltitude ( L , B ) ) ; } }


"Altitude of largest Triangle that can be inscribed in a Rectangle | Java program for the above approach ; Function to find the greatest altitude of the largest triangle triangle that can be inscribed in the rectangle ; If L is greater than B ; Variables to perform binary search ; Stores the maximum altitude possible ; Iterate until low is less than high ; Stores the mid value ; If mide is less than or equal to the B / 2 ; Update res ; Update low ; Update high ; Print the result ; Driver Code ; Given Input ; Function call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int low = 0 , high = L ; int res = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( mid <= ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 4 ; System . out . print ( largestAltitude ( L , B ) ) ; } }


"Amortized analysis for increment in counter |"
How can the above be solved in Java?

import java . util . * ; class GFG { public static void main ( String args [ ] ) { String st = "10010111" ; char [ ] str = st . toCharArray ( ) ; int lengthh = st . length ( ) ; int i = lengthh - 1 ; while ( str [ i ] == '1' ) { str [ i ] = '0' ; i -- ; } if ( i >= 0 ) str [ i ] = '1' ; System . out . print ( str ) ; } }


"An application on Bertrand 's ballot theorem | Java implementation of the approach ; Function to calculate factorial of a number mod 1000000007 ; Factorial of i = factorial of ( i - 1 ) * i ; ; Taking mod along with calculation . ; Function for modular exponentiation ; If p is odd ; If p is even ; Function to return the count of required permutations ; Calculating multiplicative modular inverse for x ! and multiplying with ans ; Calculating multiplicative modular inverse for y ! and multiplying with ans ; Driver code ; Pre - compute factorials"
How can the above be solved in Java?

class GFG { static long mod = 1000000007 ; static long [ ] arr = new long [ 1000001 ] ; static void cal_factorial ( ) { arr [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( ( arr [ i - 1 ] * i ) % mod ) ; } } static long mod_exponent ( long num , long p ) { if ( p == 0 ) return 1 ; if ( ( p & 1 ) != 0 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ) % mod ; } else return ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ; } static long getCount ( long x , long y ) { long ans = arr [ ( int ) x + ( int ) y - 1 ] ; ans *= mod_exponent ( arr [ ( int ) x ] , mod - 2 ) ; ans %= mod ; ans *= mod_exponent ( arr [ ( int ) y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } public static void main ( String [ ] args ) { cal_factorial ( ) ; long x = 3 , y = 1 ; System . out . println ( getCount ( x , y ) ) ; } }


"An in | Java implementation of above approach ; A utility function to reverse String str [ low . . high ] ; Cycle leader algorithm to move all even positioned elements at the end . ; odd index ; even index ; Keep the back - up of element at new position ; The main function to transform a String . This function mainly uses cycleLeader ( ) to transform ; Step 1 : Find the largest prefix subarray of the form 3 ^ k + 1 ; Step 2 : Apply cycle leader algorithm for the largest subarrau ; Step 4.1 : Reverse the second half of first subarray ; Step 4.2 : Reverse the first half of second sub - String . ; Step 4.3 Reverse the second half of first sub - String and first half of second sub - String together ; Increase the length of first subarray ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static char [ ] str ; static void reverse ( int low , int high ) { while ( low < high ) { char t = str [ low ] ; str [ low ] = str [ high ] ; str [ high ] = t ; ++ low ; -- high ; } } static void cycleLeader ( int shift , int len ) { int j ; char item ; for ( int i = 1 ; i < len ; i *= 3 ) { j = i ; item = str [ j + shift ] ; do { if ( j % 2 == 1 ) j = len / 2 + j / 2 ; else j /= 2 ; char t = str [ j + shift ] ; str [ j + shift ] = item ; item = t ; } while ( j != i ) ; } } static void moveNumberToSecondHalf ( ) { int k , lenFirst ; int lenRemaining = str . length ; int shift = 0 ; while ( lenRemaining > 0 ) { k = 0 ; while ( Math . pow ( 3 , k ) + 1 <= lenRemaining ) k ++ ; lenFirst = ( int ) Math . pow ( 3 , k - 1 ) + 1 ; lenRemaining -= lenFirst ; cycleLeader ( shift , lenFirst ) ; reverse ( shift / 2 , shift - 1 ) ; reverse ( shift , shift + lenFirst / 2 - 1 ) ; reverse ( shift / 2 , shift + lenFirst / 2 - 1 ) ; shift += lenFirst ; } } public static void main ( String [ ] args ) { String st = " a1b2c3d4e5f6g7" ; str = st . toCharArray ( ) ; moveNumberToSecondHalf ( ) ; System . out . print ( str ) ; } }


"An interesting method to print reverse of a linked list | Java program to print reverse of list ; Represents node of a linkedlist ; Function to reverse the linked list ; For each node , print proper number of spaces before printing it ; use of carriage return to move back and print . ; Function to push a node ; Function to print linked list and find its length ; i for finding length of list ; Driver code ; Start with the empty list ; list nodes are as 6 5 4 3 2 1 ; printlist print the list and return the size of list ; print reverse list with help of carriage return function"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( int val ) { data = val ; next = null ; } } public class GFG { static void printReverse ( Node head , int n ) { int j = 0 ; Node current = head ; while ( current != null ) { for ( int i = 0 ; i < 2 * ( n - j ) ; i ++ ) System . out . print ( " ▁ " ) ; System . out . print ( " \r " + current . data ) ; current = current . next ; j ++ ; } } static Node push ( Node head , int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; return head ; } static int printList ( Node head ) { int i = 0 ; Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; i ++ ; } return i ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 3 ) ; head = push ( head , 4 ) ; head = push ( head , 5 ) ; head = push ( head , 6 ) ; System . out . println ( " Given ▁ linked ▁ list : ▁ " ) ; int n = printList ( head ) ; System . out . println ( " Reversed ▁ Linked ▁ list : ▁ " ) ; printReverse ( head , n ) ; System . out . println ( ) ; } }


"An interesting solution to get all prime numbers smaller than n | Java program prints prime numbers smaller than n ; Compute factorials and apply Wilson 's  theorem. ; Driver code"
How can the above be solved in Java?

class GFG { static void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) System . out . println ( k ) ; } } public static void main ( String [ ] args ) { int n = 15 ; primesInRange ( n ) ; } }


"Anagram Substring Search ( Or Search for all permutations ) | Java program to search all anagrams of a pattern in a text ; This function returns true if contents of arr1 [ ] and arr2 [ ] are same , otherwise false . ; This function search for all permutations of pat [ ] in txt [ ] ; countP [ ] : Store count of all characters of pattern countTW [ ] : Store count of current window of text ; Traverse through remaining characters of pattern ; Compare counts of current window of text with counts of pattern [ ] ; Add current character to current window ; Remove the first character of previous window ; Check for the last window in text ; Driver program to test above function"
How can the above be solved in Java?

public class GFG { static final int MAX = 256 ; static boolean compare ( char arr1 [ ] , char arr2 [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static void search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; char [ ] countP = new char [ MAX ] ; char [ ] countTW = new char [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( i - M ) ) ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( N - M ) ) ; } public static void main ( String args [ ] ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; search ( pat , txt ) ; } }


"Analysis of Algorithms | Big | Java program for the above approach ; Function to find whether a key exists in an array or not using linear search ; Traverse the given array , a [ ] ; Check if a [ i ] is equal to key ; Driver code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static boolean linearSearch ( int a [ ] , int n , int key ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == key ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int n = arr . length ; if ( linearSearch ( arr , n , x ) ) System . out . println ( " Element ▁ is ▁ present ▁ in ▁ array " ) ; else System . out . println ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; } }


"Analysis of Algorithms | Big | Java program for the above approach ; Function to print all possible pairs ; Driver code ; Given array ; Store the size of the array ; Function Call"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static void print ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j ) System . out . println ( a [ i ] + " ▁ " + a [ j ] ) ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; print ( a , n ) ; } }


"Analysis of Algorithms | Set 2 ( Worst , Average and Best Cases ) | Java implementation of the approach ; Linearly search x in arr [ ] . If x is present then return the index , otherwise return - 1 ; Driver program to test above functions"
How can the above be solved in Java?

public class GFG { static int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 10 , 30 , 15 } ; int x = 30 ; int n = arr . length ; System . out . printf ( " % d ▁ is ▁ present ▁ at ▁ index ▁ % d " , x , search ( arr , n , x ) ) ; } }


"Analysis of Algorithms | Set 5 ( Practice Problems ) |"
How can the above be solved in Java?

static void function ( int n ) { int count = 0 ; for ( int i = n / 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j = 2 * j ) for ( int k = 1 ; k <= n ; k = k * 2 ) count ++ ; }


"Angle between a Pair of Lines in 3D | Java program for the above approach ; Function to find the angle between the two lines ; Find direction ratio of line AB ; Find direction ratio of line BC ; Find the dotProduct of lines AB & BC ; Find magnitude of line AB and BC ; Find the cosine of the angle formed by line AB and BC ; Find angle in radian ; Print the angle ; Driver code ; Given coordinates Points A ; Points B ; Points C ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void calculateAngle ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int x3 , int y3 , int z3 ) { int ABx = x1 - x2 ; int ABy = y1 - y2 ; int ABz = z1 - z2 ; int BCx = x3 - x2 ; int BCy = y3 - y2 ; int BCz = z3 - z2 ; double dotProduct = ABx * BCx + ABy * BCy + ABz * BCz ; double magnitudeAB = ABx * ABx + ABy * ABy + ABz * ABz ; double magnitudeBC = BCx * BCx + BCy * BCy + BCz * BCz ; double angle = dotProduct ; angle /= Math . sqrt ( magnitudeAB * magnitudeBC ) ; angle = ( angle * 180 ) / 3.14 ; System . out . printf ( " % .4f " , Math . abs ( angle ) ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 3 , z1 = 3 ; int x2 = 3 , y2 = 4 , z2 = 5 ; int x3 = 5 , y3 = 6 , z3 = 9 ; calculateAngle ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) ; } }


"Angle between a Pair of Lines | Java program for the above approach ; Function to find the angle between two lines ; Store the tan value of the angle ; Calculate tan inverse of the angle ; Convert the angle from radian to degree ; Print the result ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double PI = 3.14159265 ; static void findAngle ( double M1 , double M2 ) { double angle = Math . abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) ; double ret = Math . atan ( angle ) ; double val = ( ret * 180 ) / PI ; System . out . println ( val ) ; } public static void main ( String [ ] args ) { double M1 = 1.75 , M2 = 0.27 ; findAngle ( M1 , M2 ) ; } }


"Angle between a chord and a tangent when angle in the alternate segment is given | Java program to find the angle between a chord and a tangent when angle in the alternate segment is given ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void anglechordtang ( int z ) { System . out . print ( " The ▁ angle ▁ between ▁ tangent " + " ▁ and ▁ the ▁ chord ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; anglechordtang ( z ) ; } }


"Angle between two Planes in 3D | Java program to find the Angle between two Planes in 3 D . ; Function to find Angle ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . lang . Math ; class GFG { static void distance ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { float d = ( a1 * a2 + b1 * b2 + c1 * c2 ) ; float e1 = ( float ) Math . sqrt ( a1 * a1 + b1 * b1 + c1 * c1 ) ; float e2 = ( float ) Math . sqrt ( a2 * a2 + b2 * b2 + c2 * c2 ) ; d = d / ( e1 * e2 ) ; float pi = ( float ) 3.14159 ; float A = ( 180 / pi ) * ( float ) ( Math . acos ( d ) ) ; System . out . println ( " Angle ▁ is ▁ " + A + " ▁ degree " ) ; } public static void main ( String [ ] args ) { float a1 = 1 ; float b1 = 1 ; float c1 = 2 ; float d1 = 1 ; float a2 = 2 ; float b2 = - 1 ; float c2 = 1 ; float d2 = - 4 ; distance ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }


"Angle of intersection of two circles having their centers D distance apart | Java program for the above approach ; Function to find the cosine of the angle of the intersection of two circles with radius R1 and R2 ; Return the cosine of the angle ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static float angle ( float R1 , float R2 , float D ) { float ans = ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ; return ans ; } public static void main ( String [ ] args ) { float R1 = 3 , R2 = 4 ; float D = 5 ; System . out . println ( angle ( R1 , R2 , D ) ) ; } }


"Angle subtended by an arc at the centre of a circle | Java implementation of the approach ; Function to find Angle subtended by an arc at the centre of a circle ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int angle ( int n ) { return 2 * n ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( angle ( n ) ) ; } }


"Angle subtended by the chord when the angle subtended by another chord of same length is given | Java program to find the angle subtended at the center by the chord when the angle subtended at center by another chord of equal length is given ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void angleequichord ( int z ) { System . out . println ( " The ▁ angle ▁ subtended ▁ at ▁ the ▁ center ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; angleequichord ( z ) ; } }


"Apocalyptic Number | Java program for the above approach ; Function to check if a number N is Apocalyptic ; Driver Code ; Given Number N ; Function Call"
How can the above be solved in Java?

class GFG { static boolean isApocalyptic ( int n ) { if ( String . valueOf ( ( Math . pow ( 2 , n ) ) ) . contains ( "666" ) ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 157 ; if ( isApocalyptic ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Append X digits to the end of N to make it divisible by M | Java program for the above approach ; Function to check if the value of N by appending X digits on right side of N is divisible by M or not ; Base Case ; If N is divisible by M ; Update res ; Iterate over the range [ 0 , 9 ] ; If N is divisible by M by appending X digits ; Driver code ; Stores the number by appending X digits on the right side of N"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void main ( String [ ] args ) throws java . lang . Exception { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; System . out . println ( res ) ; } }


"Append a digit in the end to make the number equal to the length of the remaining string | Java implementation of the approach ; Function to return the required digit ; To store the position of the first numeric digit in the string ; To store the length of the string without the numeric digits in the end ; pw stores the current power of 10 and num is to store the number which is appended in the end ; If current character is a numeric digit ; Get the current digit ; Build the number ; If number exceeds the length ; Next power of 10 ; Append 0 in the end ; Required number that must be added ; If number is not a single digit ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int find_digit ( String s , int n ) { int first_digit = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) < '0' || s . charAt ( i ) > '9' ) { first_digit = i ; break ; } } first_digit ++ ; int s_len = first_digit ; int num = 0 , pw = 1 ; int i = n - 1 ; while ( i >= 0 ) { if ( s . charAt ( i ) >= '0' && s . charAt ( i ) <= '9' ) { int digit = s . charAt ( i ) - '0' ; num = num + ( pw * digit ) ; if ( num >= s_len ) return - 1 ; pw = pw * 10 ; } i -- ; } num = num * 10 ; int req = s_len - num ; if ( req > 9 req < 0 ) return - 1 ; return req ; } public static void main ( String [ ] args ) { String s = " abcd0" ; int n = s . length ( ) ; System . out . print ( find_digit ( s , n ) ) ; } }


"Append digits to the end of duplicate strings to make all strings in an array unique | Java program for the above approach ; Function to replace duplicate strings by alphanumeric strings to make all strings in the array unique ; Store the frequency of strings ; Iterate over the array ; For the first occurrence , update the frequency count ; Otherwise ; Append frequency count to end of the string ; Print the modified array ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void replaceDuplicates ( String [ ] names ) { HashMap < String , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < names . length ; i ++ ) { if ( ! hash . containsKey ( names [ i ] ) ) hash . put ( names [ i ] , 1 ) ; else { int count = hash . get ( names [ i ] ) ; hash . put ( names [ i ] , hash . get ( names [ i ] ) + 1 ) ; names [ i ] += Integer . toString ( count ) ; } } for ( int i = 0 ; i < names . length ; i ++ ) { System . out . print ( names [ i ] + ' ▁ ' ) ; } } public static void main ( String [ ] args ) { String [ ] str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; } }


"Append two elements to make the array satisfy the given condition | Java implementation of the approach ; Function to find the required numbers ; Find the sum and xor ; Print the required elements ; Driver code"
How can the above be solved in Java?

class GFG { static void findNums ( int arr [ ] , int n ) { int S = 0 , X = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += arr [ i ] ; X ^= arr [ i ] ; } System . out . println ( X + " ▁ " + ( X + S ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 } ; int n = arr . length ; findNums ( arr , n ) ; } }


"Area of Circumcircle of a Right Angled Triangle | Java code to find the area of circumscribed circle of right angled triangle ; Function to find the area of circumscribed circle ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_cicumscribed ( double c ) { return ( c * c * ( PI / 4 ) ) ; } public static void main ( String [ ] args ) { double c = 8.0 ; System . out . println ( area_cicumscribed ( c ) ) ; } }


"Area of Circumcircle of an Equilateral Triangle using Median | Java implementation to find the equation of circle which inscribes equilateral triangle of median M ; Function to find the equation of circle whose center is ( x1 , y1 ) and the radius of circle is r ; Function to find the equation of circle which inscribes equilateral triangle of median M ; Function call to find the circle equation ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static double circleArea ( double r ) { double pi = 3.14159265358979323846 ; return ( pi * r * r ) ; } static double findCircleAreaByMedian ( int m ) { double r = 2 * m / 3 ; return circleArea ( r ) ; } public static void main ( String args [ ] ) { int m = 3 ; System . out . printf ( " % .4f " , findCircleAreaByMedian ( m ) ) ; } }


"Area of Incircle of a Right Angled Triangle | Java code to find the area of inscribed circle of right angled triangle ; Function to find the area of inscribed circle ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_inscribed ( double P , double B , double H ) { return ( ( P + B - H ) * ( P + B - H ) * ( PI / 4 ) ) ; } public static void main ( String [ ] args ) { double P = 3 , B = 4 , H = 5 ; System . out . println ( area_inscribed ( P , B , H ) ) ; } }


"Area of Largest rectangle that can be inscribed in an Ellipse | Java Program to find the biggest rectangle which can be inscribed within the ellipse ; Function to find the area of the rectangle ; a and b cannot be negative ; area of the rectangle ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float rectanglearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; return 2 * a * b ; } public static void main ( String args [ ] ) { float a = 10 , b = 8 ; System . out . println ( rectanglearea ( a , b ) ) ; } }


"Area of Reuleaux Triangle | Java Program to find the area of Reuleaux triangle ; Function to find the Area of the Reuleaux triangle ; Side cannot be negative ; Area of the Reuleaux triangle ; Driver code"
How can the above be solved in Java?

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


"Area of Triangle using Side | Java program to calculate the area of a triangle when the length of two adjacent sides and the angle between them is provided ; Function to return the area of triangle using Side - Angle - Side formula ; Driver Code ; Function Call ; Print the final answer"
How can the above be solved in Java?

class GFG { static float Area_of_Triangle ( int a , int b , int k ) { float area = ( float ) ( ( 1 / 2.0 ) * a * b * Math . sin ( k ) ) ; return area ; } public static void main ( String [ ] args ) { int a = 9 ; int b = 12 ; int k = 2 ; float ans = Area_of_Triangle ( a , b , k ) ; System . out . printf ( " % .1f " , ans ) ; } }


"Area of a Triangle from the given lengths of medians | Java program to calculate area of a triangle from the given lengths of medians ; Function to return the area of triangle using medians ; Driver Code ; Function Call ; Print the final answer"
How can the above be solved in Java?

class GFG { static double Area_of_Triangle ( int a , int b , int c ) { int s = ( a + b + c ) / 2 ; int x = s * ( s - a ) ; x = x * ( s - b ) ; x = x * ( s - c ) ; double area = ( 4 / ( double ) 3 ) * Math . sqrt ( x ) ; return area ; } public static void main ( String [ ] args ) { int a = 9 ; int b = 12 ; int c = 15 ; double ans = Area_of_Triangle ( a , b , c ) ; System . out . println ( ans ) ; } }


"Area of a largest square fit in a right angle triangle | Java Program to find the area of the biggest square which can fit inside the right angled triangle ; Function to find the area of the biggest square ; the height or base or hypotenuse cannot be negative ; side of the square ; squaring to get the area ; Driver code"
How can the above be solved in Java?

public class GFG { static float squareArea ( float l , float b , float h ) { if ( l < 0 b < 0 h < 0 ) return - 1 ; float a = ( l * b ) / ( l + b ) ; return a * a ; } public static void main ( String [ ] args ) { float l = 5 , b = 12 , h = 13 ; System . out . println ( squareArea ( l , b , h ) ) ; } }


"Area of a leaf inside a square | Java code to find the area of leaf inside a square ; Function to find the area of leaf ; Driver code"
How can the above be solved in Java?

import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_leaf ( double a ) { return ( a * a * ( PI / 2 - 1 ) ) ; } public static void main ( String [ ] args ) { double a = 7 ; System . out . println ( area_leaf ( a ) ) ; } }


"Area of a polygon with given n ordered vertices | Java program to evaluate area of a polygon using shoelace formula ; ( X [ i ] , Y [ i ] ) are coordinates of i 'th point. ; Initialize area ; Calculate value of shoelace formula ; j is previous vertex to i ; Return absolute value ; Driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { public static double polygonArea ( double X [ ] , double Y [ ] , int n ) { double area = 0.0 ; int j = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ; j = i ; } return Math . abs ( area / 2.0 ) ; } public static void main ( String [ ] args ) { double X [ ] = { 0 , 2 , 4 } ; double Y [ ] = { 1 , 3 , 7 } ; int n = 3 ; System . out . println ( polygonArea ( X , Y , n ) ) ; } }


"Area of a square from diagonal length | Java Program to find the area of square when its diagonal is given . ; Returns area of square from given diagonal ; Driver code"
How can the above be solved in Java?

class GFG { static double findArea ( double d ) { return ( d * d ) / 2 ; } public static void main ( String [ ] args ) { double d = 10 ; System . out . println ( findArea ( d ) ) ; } }


"Area of a triangle inscribed in a rectangle which is inscribed in an ellipse | Java Program to find the area of the triangle inscribed within the rectangle which in turn is inscribed in an ellipse ; Function to find the area of the triangle ; length of a and b cannot be negative ; area of the triangle ; Driver code"
How can the above be solved in Java?

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


"Area of a triangle with two vertices at midpoints of opposite sides of a square and the other vertex lying on vertex of a square | Java program for the above approach ; Function to find the area of the triangle that inscribed in square ; Stores the length of the first side of triangle ; Stores the length of the second side of triangle ; Stores the length of the third side of triangle ; Stores the area of the triangle ; Return the resultant area ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static double areaOftriangle ( int side ) { double a = Math . sqrt ( Math . pow ( side / 2 , 2 ) + Math . pow ( side / 2 , 2 ) ) ; double b = Math . sqrt ( Math . pow ( side , 2 ) + Math . pow ( side / 2 , 2 ) ) ; double c = Math . sqrt ( Math . pow ( side , 2 ) + Math . pow ( side / 2 , 2 ) ) ; double s = ( a + b + c ) / 2 ; double area = Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; return area ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( areaOftriangle ( N ) ) ; } }


"Area of circle inscribed within rhombus | Java Program to find the area of the circle which can be inscribed within the rhombus ; Function to find the area of the inscribed circle ; the diagonals cannot be negative ; area of the circle ; Driver code"
How can the above be solved in Java?

public class GFG { public static float circlearea ( double a , double b ) { if ( a < 0 b < 0 ) return - 1 ; float A = ( float ) ( ( 3.14 * Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) / ( 4 * ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 8 , b = 10 ; System . out . println ( circlearea ( a , b ) ) ; } }


"Area of circle which is inscribed in equilateral triangle | Java program to find the area of circle which is inscribed in equilateral triangle ; Function return the area of circle inscribed in equilateral triangle ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static double PI = 3.14 ; static double circle_inscribed ( int a ) { return PI * ( a * a ) / 12 ; } public static void main ( String [ ] args ) { int a = 4 ; System . out . println ( circle_inscribed ( a ) ) ; } }


"Area of hexagon with given diagonal length | Java program to find the area of Hexagon with given diagonal ; Function to calculate area ; Formula to find area ; Main"
How can the above be solved in Java?

import java . lang . Math ; public class GfG { public static float hexagonArea ( float d ) { return ( float ) ( ( 3 * Math . sqrt ( 3 ) * d * d ) / 8 ) ; } public static void main ( String [ ] args ) { float d = 10 ; System . out . println ( " Area ▁ of ▁ hexagon : ▁ " + hexagonArea ( d ) ) ; } }


"Area of largest isosceles triangle that can be inscribed in an Ellipse whose vertex coincides with one extremity of the major axis | Java program for the above approach ; Function to calculate area of the isosceles triangle ; If a and b are negative ; Stores the area of the triangle ; Print the area ; Driver Code ; Given value of a & b ; Function call to find the area of the isosceles triangle"
How can the above be solved in Java?

import java . util . * ; class GFG { static void triangleArea ( float a , float b ) { if ( a < 0 b < 0 ) { System . out . println ( - 1 ) ; return ; } float area = ( 3 * ( float ) Math . sqrt ( 3 ) * a * b ) / ( 4 ) ; System . out . println ( area ) ; } public static void main ( String [ ] args ) { float a = 1 , b = 2 ; triangleArea ( a , b ) ; } }


"Area of largest semicircle that can be drawn inside a square | Java program to find Area of semicircle in a square ; Function to find area of semicircle ; Driver code ; side of a square ; Call Function to find the area of semicircle"
How can the above be solved in Java?

class GFG { static float find_Area ( float a ) { float R = a * ( float ) ( 2.0 - Math . sqrt ( 2 ) ) ; float area = ( float ) ( ( 3.14 * R * R ) / 2.0 ) ; return area ; } public static void main ( String [ ] args ) { float a = 4 ; System . out . println ( " ▁ Area ▁ of ▁ semicircle ▁ = ▁ " + find_Area ( a ) ) ; } }


"Area of largest triangle that can be inscribed within a rectangle | Java Program to find the biggest triangle which can be inscribed within the rectangle ; Function to find the area of the triangle ; a and b cannot be negative ; area of the triangle ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static float trianglearea ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float area = ( l * b ) / 2 ; return area ; } public static void main ( String args [ ] ) { float l = 5 , b = 4 ; System . out . println ( trianglearea ( l , b ) ) ; } }


"Area of plot remaining at the end | Java implementation of the approach ; Function to return the area of the remaining plot ; Continue while plot has positive area and there are persons left ; If length > breadth then subtract breadth from length ; Else subtract length from breadth ; Driver code"
How can the above be solved in Java?

class GFG { static int remainingArea ( int N , int M , int K ) { while ( K -- > 0 && N > 0 && M > 0 ) { if ( N > M ) N = N - M ; else M = M - N ; } if ( N > 0 && M > 0 ) return N * M ; else return 0 ; } public static void main ( String [ ] args ) { int N = 5 , M = 3 , K = 2 ; System . out . println ( remainingArea ( N , M , K ) ) ; } }


"Area of square Circumscribed by Circle | Java program to find Area of square Circumscribed by Circle ; Function to find area of square ; Driver code ; Radius of a circle ; Call Function to find an area of square"
How can the above be solved in Java?

class GFG { static int find_Area ( int r ) { return ( 2 * r * r ) ; } public static void main ( String [ ] args ) { int r = 3 ; System . out . print ( " ▁ Area ▁ of ▁ square ▁ = ▁ " + find_Area ( r ) ) ; } }


"Area of the Largest Triangle inscribed in a Hexagon | Java Program to find the biggest triangle which can be inscribed within the hexagon ; Function to find the area of the triangle ; side cannot be negative ; area of the triangle ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static double trianglearea ( double a ) { if ( a < 0 ) return - 1 ; double area = ( 3 * Math . sqrt ( 3 ) * Math . pow ( a , 2 ) ) / 4 ; return area ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( trianglearea ( a ) ) ; } }


"Area of the Largest square that can be inscribed in an ellipse | Java Program to find the biggest square which can be inscribed within the ellipse ; Function to find the area of the square ; a and b cannot be negative ; area of the square ; Driver code"
How can the above be solved in Java?

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


"Area of the biggest ellipse inscribed within a rectangle | Java Program to find the biggest rectangle which can be inscribed within the ellipse ; Function to find the area of the rectangle ; The sides cannot be negative ; Area of the ellipse ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float ellipse ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; float x = ( float ) ( 3.14 * l * b ) / 4 ; return x ; } public static void main ( String args [ ] ) { float a = 5 , b = 3 ; System . out . println ( ellipse ( a , b ) ) ; } }


"Area of the circle that has a square and a circle inscribed in it | Java implementation of the approach ; Function to return the required area ; Calculate the area ; Driver code"
How can the above be solved in Java?

class GFG { static float getArea ( int a ) { float area = ( float ) ( Math . PI * a * a ) / 4 ; return area ; } public static void main ( String args [ ] ) { int a = 3 ; System . out . println ( getArea ( a ) ) ; } }


"Area of the circumcircle of any triangles with sides given | Java Program to find the area the circumcircle of the given triangle ; Function to find the area of the circumcircle ; the sides cannot be negative ; semi - perimeter of the circle ; area of triangle ; area of the circle ; Driver code ; Get the sides of the triangle ; Find and print the area of the circumcircle"
How can the above be solved in Java?

import java . * ; class gfg { public double circlearea ( double a , double b , double c ) { if ( a < 0 b < 0 c < 0 ) return - 1 ; double p = ( a + b + c ) / 2 ; double At = Math . sqrt ( p * ( p - a ) * ( p - b ) * ( p - c ) ) ; double A = 3.14 * Math . pow ( ( ( a * b * c ) / ( 4 * At ) ) , 2 ) ; return A ; } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; double a = 4 , b = 5 , c = 3 ; System . out . println ( g . circlearea ( a , b , c ) ) ; } }


"Area of the largest rectangle formed by lines parallel to X and Y axis from given set of points | Java program for the above approach ; Function to return the area of the largest rectangle formed by lines parallel to X and Y axis from given set of points ; Initialize two arrays ; Store x and y coordinates ; Sort arrays ; Initialize max differences ; Find max adjacent differences ; Return answer ; Driver Code ; Given points ; Total points ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int maxRectangle ( int [ ] [ ] sequence , int size ) { int [ ] X_Cord = new int [ size ] ; int [ ] Y_Cord = new int [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { X_Cord [ i ] = sequence [ i ] [ 0 ] ; Y_Cord [ i ] = sequence [ i ] [ 1 ] ; } Arrays . sort ( X_Cord ) ; Arrays . sort ( Y_Cord ) ; int X_Max = 0 , Y_Max = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { X_Max = Math . max ( X_Max , X_Cord [ i + 1 ] - X_Cord [ i ] ) ; Y_Max = Math . max ( Y_Max , Y_Cord [ i + 1 ] - Y_Cord [ i ] ) ; } return X_Max * Y_Max ; } public static void main ( String [ ] args ) { int [ ] [ ] point = { { - 2 , 0 } , { 2 , 0 } , { 4 , 0 } , { 4 , 2 } } ; int n = point . length ; System . out . print ( maxRectangle ( point , n ) ) ; } }


"Area of the largest rectangle possible from given coordinates | Java program for the above approach ; Function to find the maximum possible area of a rectangle ; Initialize variables ; Sort array arr1 [ ] ; Sort array arr2 [ ] ; Traverse arr1 [ ] and arr2 [ ] ; If arr1 [ i ] is same as arr2 [ j ] ; If no starting point is found yet ; Update maximum end ; If arr [ i ] > arr2 [ j ] ; If no rectangle is found ; Return the area ; Driver Code ; Given point ; Given length ; Given points ; Given length ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int largestArea ( int arr1 [ ] , int n , int arr2 [ ] , int m ) { int end = 0 , start = 0 , i = 0 , j = 0 ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr1 ) ; while ( i < n && j < m ) { if ( arr1 [ i ] == arr2 [ j ] ) { if ( start == 0 ) start = arr1 [ i ] ; else end = arr1 [ i ] ; i ++ ; j ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) j ++ ; else i ++ ; } if ( end == 0 start == 0 ) return 0 ; else return ( end - start ) ; } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 } ; int N = arr1 . length ; int arr2 [ ] = { 1 , 3 , 4 } ; int M = arr2 . length ; System . out . println ( largestArea ( arr1 , N , arr2 , M ) ) ; } }


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

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


"Arithmetic Number | Java program to check if a number is Arithmetic number or not ; Sieve Of Eratosthenes ; 1 is not a prime number ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p ; Storing primes in an array ; Update value in primesquare [ p * p ] , if p is prime . ; Function to count divisors ; If number is 1 , then it will have only 1 as a factor . So , total factors will be 1. ; Calling SieveOfEratosthenes to store prime factors of n and to store square of prime factors of n ; ans will contain total number of distinct divisors ; Loop for counting factors of n ; a [ i ] is not less than cube root n ; Calculating power of a [ i ] in n . cnt is power of prime a [ i ] in n . ; if a [ i ] is a factor of n ; cnt = cnt + 1 ; incrementing power ; Calculating number of divisors If n = a ^ p * b ^ q then total divisors of n are ( p + 1 ) * ( q + 1 ) ; First case ; Second case ; Third casse ; return ans ; Total divisors ; Returns sum of all factors of n . ; Traversing through all prime factors . ; This condition is to handle the case when n is a prime number greater than 2. ; Check if number is Arithmetic Number or not . ; Driver Program"
How can the above be solved in Java?

class GFG { static void SieveOfEratosthenes ( int n , boolean prime [ ] , boolean primesquare [ ] , int a [ ] ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } static int countDivisors ( int n ) { if ( n == 1 ) return 1 ; boolean prime [ ] = new boolean [ n + 1 ] , primesquare [ ] = new boolean [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static boolean checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } public static void main ( String [ ] args ) { int n = 6 ; if ( checkArithmetic ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Arithmetic Progression containing X and Y with least possible first term | Java program for the approach ; Function that finds the minimum positive first term including X with given common difference and the number of terms ; Stores the first term ; Initialize the low and high ; Perform binary search ; Find the mid ; Check if first term is greater than 0 ; Store the possible first term ; Search between mid + 1 to high ; Search between low to mid - 1 ; Return the minimum first term ; Function that finds the Arithmetic Progression with minimum possible first term containing X and Y ; Considering X to be smaller than Y always ; Stores the max common difference ; Stores the minimum first term and the corresponding common difference of the resultant AP ; Iterate over all the common difference ; Check if X and Y is included for current common difference ; Store the possible common difference ; Number of terms from X to Y with diff1 common difference ; Number of terms from X to Y with diff2 common difference ; Find the corresponding first terms with diff1 and diff2 ; Store the minimum first term and the corresponding common difference ; Print the resultant AP ; Driver Code ; Given length of AP and the two terms ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minFirstTerm ( int X , int diff , int N ) { int first_term = Integer . MAX_VALUE ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } static void printAP ( int N , int X , int Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } int maxDiff = Y - X ; int first_term = Integer . MAX_VALUE , diff = 0 ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( first_term + " ▁ " ) ; first_term += diff ; } } public static void main ( String [ ] args ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; } }


"Arithmetic Progression | Java program to check if a given array can form arithmetic progression ; Returns true if a permutation of arr [ 0. . n - 1 ] can form arithmetic progression ; Sort array ; After sorting , difference between consecutive elements must be same . ; driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static boolean checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; Arrays . sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 15 , 5 , 0 , 10 } ; int n = arr . length ; if ( checkIsAP ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Armstrong Numbers between two integers | JAVA program to find Armstrong numbers in a range ; Prints Armstrong Numbers in given range ; number of digits calculation ; compute sum of nth power of its digits ; checks if number i is equal to the sum of nth power of its digits ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . math . * ; class GFG { static void findArmstrong ( int low , int high ) { for ( int i = low + 1 ; i < high ; ++ i ) { int x = i ; int n = 0 ; while ( x != 0 ) { x /= 10 ; ++ n ; } int pow_sum = 0 ; x = i ; while ( x != 0 ) { int digit = x % 10 ; pow_sum += Math . pow ( digit , n ) ; x /= 10 ; } if ( pow_sum == i ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String args [ ] ) { int num1 = 100 ; int num2 = 400 ; findArmstrong ( num1 , num2 ) ; System . out . println ( ) ; } }


"Aronson 's Sequence | Java program to generate the first n terms of Aronson 's sequence ; Returns the given number in words ; Get number of digits in given number ; Base cases ; The following arrays contain one digit ( both cardinal and ordinal forms ) , two digit ( < 20 , ordinal forms ) numbers , and multiples ( ordinal forms ) and powers of 10. ; If single digit number ; here len can be 3 or 4 ; last two digits ; Handle all powers of 10 ; Handle two digit numbers < 20 ; Function to print the first n terms of Aronson 's sequence ; check if character is alphabet or not ; convert number to words in ordinal format and append ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String convert_to_words ( char [ ] num ) { int len = num . length ; if ( len == 0 len > 4 ) { return " " ; } String single_digits_temp [ ] = { " " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String single_digits [ ] = { " " , " first " , " second " , " third " , " fourth " , " fifth " , " sixth " , " seventh " , " eighth " , " ninth " } ; String two_digits [ ] = { " " , " tenth " , " eleventh " , " twelfth " , " thirteenth " , " fourteenth " , " fifteenth " , " sixteenth " , " seventeenth " , " eighteenth " , " nineteenth " } ; String tens_multiple [ ] = { " " , " tenth " , " twentieth " , " thirtieth " , " fortieth " , " fiftieth " , " sixtieth " , " seventieth " , " eightieth " , " ninetieth " } ; String tens_power [ ] = { " hundred " , " thousand " } ; String word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] - '0' ] ; return word ; } int i = 0 , ctr = 0 ; String s = " ▁ " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != '0' ) { word += single_digits_temp [ num [ i ] - '0' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; num = Arrays . copyOfRange ( num , 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word = word . substring ( 0 , word . length ( ) - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; else if ( num [ i ] == '1' ) word += s + two_digits [ num [ i + 1 ] - '0' + 1 ] ; else { if ( num [ i ] != '0' ) word += s + tens_multiple [ num [ i ] - '0' ] . substring ( 0 , tens_multiple [ num [ i ] - '0' ] . length ( ) - 4 ) + " y ▁ " ; else word += s ; word += single_digits [ num [ i + 1 ] - '0' ] ; } i += 2 ; } if ( i == len ) { if ( word . charAt ( 0 ) == ' ▁ ' ) word = word . substring ( 1 , word . length ( ) ) ; } } return word ; } static void Aronsons_sequence ( int n ) { String str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str ....


"Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements | Java implementation of the approach ; Function to print the arrangement that satisifes the given condition ; Sort the array initially ; Array that stores the arrangement ; Once the array is sorted Re - fill the array again in the mentioned way in the approach ; Iterate in the array and check if the arrangement made satisfies the given condition or not ; For the first element the adjacents will be a [ 1 ] and a [ n - 1 ] ; For the last element the adjacents will be a [ 0 ] and a [ n - 2 ] ; If we reach this position then the arrangement is possible ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static void printArrangement ( int a [ ] , int n ) { Arrays . sort ( a ) ; int b [ ] = new int [ n ] ; int low = 0 , high = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) b [ low ++ ] = a [ i ] ; else b [ high -- ] = a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( b [ n - 1 ] + b [ 1 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } else if ( i == ( n - 1 ) ) { if ( b [ n - 2 ] + b [ 0 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } else { if ( b [ i - 1 ] + b [ i + 1 ] <= b [ i ] ) { System . out . print ( - 1 ) ; return ; } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( b [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 4 , 3 , 2 } ; int n = a . length ; printArrangement ( a , n ) ; } }


"Arrange a binary string to get maximum value within a range of indices | Java implementation of the approach ; Storing the count of 1 's in the String ; Query of l and r ; Applying range update technique . ; Taking prefix sum to get the range update values ; Final array which will store the arranged String ; if after maximizing the ranges any 1 is left then we maximize the String lexicographically . ; Driver Code"
How can the above be solved in Java?

class GFG { static void arrange ( String s ) { int cc = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) cc ++ ; } int [ ] a = new int [ s . length ( ) + 1 ] ; int qq [ ] [ ] = { { 2 , 3 } , { 5 , 5 } } ; int n = qq . length ; for ( int i = 0 ; i < n ; i ++ ) { int l = qq [ i ] [ 0 ] , r = qq [ i ] [ 1 ] ; l -- ; r -- ; a [ l ] ++ ; a [ r + 1 ] -- ; } int len_a = a . length ; for ( int i = 1 ; i < len_a ; i ++ ) { a [ i ] += a [ i - 1 ] ; } int [ ] zz = new int [ s . length ( ) ] ; for ( int i = 0 ; i < len_a - 1 ; i ++ ) { if ( a [ i ] > 0 ) { if ( cc > 0 ) { zz [ i ] = 1 ; cc -- ; } else break ; } if ( cc == 0 ) break ; } if ( cc > 0 ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( zz [ i ] == 0 ) { zz [ i ] = 1 ; cc -- ; } if ( cc == 0 ) break ; } } for ( int i = 0 ; i < s . length ( ) ; i ++ ) System . out . print ( zz [ i ] ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String str = "11100" ; arrange ( str ) ; } }


"Arrange numbers to form a valid sequence | Java implementation of the above approach ; Function to organize the given numbers to form a valid sequence . ; Sorting the array ; Two pointer technique to organize the numbers ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int [ ] orgazineInOrder ( int [ ] vec , int [ ] op , int n ) { int [ ] result = new int [ n ] ; Arrays . sort ( vec ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } public static void main ( String [ ] args ) { int [ ] vec = { 8 , 2 , 7 , 1 , 5 , 9 } ; int [ ] op = { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ; int [ ] result = orgazineInOrder ( vec , op , vec . length ) ; for ( int i = 0 ; i < result . length ; i ++ ) { System . out . print ( result [ i ] + " ▁ " ) ; } } }


"Arrange the array such that upon performing given operations an increasing order is obtained | Java program to find the desired output after performing given operations ; Function to arrange array in such a way that after performing given operation We get increasing sorted array ; Size of given array ; Sort the given array ; Start erasing last element and place it at ith index ; While we reach at starting ; Store last element ; Shift all elements by 1 position in right ; insert last element at ith position ; Print desired Array ; Driver code ; Given Array"
How can the above be solved in Java?

import java . util . Arrays ; class Main { public static void Desired_Array ( int v [ ] ) { int n = v . length ; Arrays . sort ( v ) ; int i = n - 1 ; while ( i > 0 ) { int p = v [ n - 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { v [ j ] = v [ j - 1 ] ; } v [ i ] = p ; i -- ; } for ( int x = 0 ; x < v . length ; x ++ ) { System . out . print ( v [ x ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int v [ ] = { 1 , 2 , 3 , 4 , 5 } ; Desired_Array ( v ) ; int v1 [ ] = { 1 , 12 , 2 , 10 , 4 , 16 , 6 } ; Desired_Array ( v1 ) ; } }


"Arrange the numbers in the Array as per given inequalities | Java program for the above approach ; Function to place the integers in between the inequality signs ; Sort the integers array and set the index of smallest and largest element ; Iterate over the inequalities ; Append the necessary integers per symbol ; Add the final integer ; Return the answer ; Driver Code ; Given List of Integers ; Given list of inequalities ; Function Call ; Print the output"
How can the above be solved in Java?

import java . util . Arrays ; public class PlacingNumbers { static String formAnInequality ( int [ ] integers , char [ ] inequalities ) { Arrays . sort ( integers ) ; int lowerIndex = 0 ; int higherIndex = integers . length - 1 ; StringBuilder sb = new StringBuilder ( ) ; for ( char ch : inequalities ) { if ( ch == ' < ' ) { sb . append ( " ▁ " + integers [ lowerIndex ++ ] + " ▁ " + ch ) ; } else { sb . append ( " ▁ " + integers [ higherIndex -- ] + " ▁ " + ch ) ; } } sb . append ( " ▁ " + integers [ lowerIndex ] ) ; return sb . toString ( ) ; } public static void main ( String [ ] args ) { int [ ] integers = { 2 , 5 , 1 , 0 } ; char [ ] inequalities = { ' < ' , ' > ' , ' < ' } ; String output = formAnInequality ( integers , inequalities ) ; System . out . println ( output ) ; } }


"Arrangement of the characters of a word such that all vowels are at odd places | Java program to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Function to return the factorial of a number ; calculating nPr ; Function to find the number of ways in which the characters of the word can be arranged such that the vowels occupy only the odd positions ; Get total even positions ; Get total odd positions ; Store frequency of each character of the string ; Count total number of vowels ; Count total number of consonants ; Calculate the total number of ways ; Driver code"
How can the above be solved in Java?

class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } static int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countPermutations ( String str ) { int even = ( int ) Math . floor ( ( double ) ( str . length ( ) / 2 ) ) ; int odd = str . length ( ) - even ; int ways = 0 ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ ( int ) ( str . charAt ( i ) - ' a ' ) ] ++ ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . length ( ) - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } public static void main ( String [ ] args ) { String str = " geeks " ; System . out . println ( countPermutations ( str ) ) ; } }


"Arrangement of words without changing the relative position of vowel and consonants | Java program for Arrangement of words without changing the relative position of vowel and consonants ; this function return n ! ; this will return total number of ways ; freq maintains frequency of each character in word ; check character is vowel or not ; the characters that are not vowel must be consonant ; number of ways to arrange vowel ; multiply both as these are independent ; Driver function ; string contains only capital letters ; this will contain ans"
How can the above be solved in Java?

class GFG { static long factorial ( long n ) { long res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } static long count ( String word ) { int freq [ ] = new int [ 27 ] ; for ( int i = 0 ; i < 27 ; i ++ ) freq [ i ] = 0 ; long vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { freq [ word . charAt ( i ) - ' A ' ] ++ ; if ( word . charAt ( i ) == ' A ' || word . charAt ( i ) == ' E ' || word . charAt ( i ) == ' I ' || word . charAt ( i ) == ' O ' || word . charAt ( i ) == ' U ' ) { vowel ++ ; } else consonant ++ ; } long vowelArrange ; vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; long consonantArrange ; consonantArrange = factorial ( consonant ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } long total = vowelArrange * consonantArrange ; return total ; } public static void main ( String [ ] args ) { String word = " COMPUTER " ; long ans = count ( word ) ; System . out . println ( ans ) ; } }


"Array Queries for multiply , replacements and product | Java program to solve three types of queries . ; Vector of 1000 elements , all set to 0 ; Vector of 1000 elements , all set to 0 ; Function to check number of trailing zeros in multiple of 2 ; Function to check number of trailing zeros in multiple of 5 ; Function to solve the queries received ; If the query is of type 1. ; Counting the number of zeros in the given value of x ; The value x has been multiplied to their respective indices ; The value obtained above has been added to their respective vectors ; If the query is of type 2. ; Counting the number of zero in the given value of x ; The value y has been replaced to their respective indices ; The value obtained above has been added to their respective vectors ; If the query is of type 2 ; As the number of trailing zeros for each case has been found for each array element then we simply add those to the respective index to a variable ; Compare the number of zeros obtained in the multiples of five and two consider the minimum of them and add them ; Driver code ; Input the Size of array and number of queries ; Running the while loop for m number of queries"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static Scanner sc = new Scanner ( System . in ) ; static int twos [ ] = new int [ 1000 ] ; static int fives [ ] = new int [ 1000 ] ; static int sum = 0 ; static int returnTwos ( int val ) { int count = 0 ; while ( val % 2 == 0 && val != 0 ) { val = val / 2 ; count ++ ; } return count ; } static int returnFives ( int val ) { int count = 0 ; while ( val % 5 == 0 && val != 0 ) { val = val / 5 ; count ++ ; } return count ; } static void solve_queries ( int arr [ ] , int n ) { int type = sc . nextInt ( ) ; if ( type == 1 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; int temp = returnTwos ( x ) ; int temp1 = returnFives ( x ) ; for ( int i = ql - 1 ; i < qr ; i ++ ) { arr [ i ] = arr [ i ] * x ; twos [ i ] += temp ; fives [ i ] += temp1 ; } } if ( type == 2 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int y = sc . nextInt ( ) ; int temp = returnTwos ( y ) ; int temp1 = returnFives ( y ) ; for ( int i = ql - 1 ; i < qr ; i ++ ) { arr [ i ] = ( i - ql + 2 ) * y ; twos [ i ] = returnTwos ( i - ql + 2 ) + temp ; fives [ i ] = returnFives ( i - ql + 2 ) + temp1 ; } } if ( type == 3 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int sumtwos = 0 ; int sumfives = 0 ; for ( int i = ql - 1 ; i < qr ; i ++ ) { sumtwos += twos [ i ] ; sumfives += fives [ i ] ; } sum += Math . min ( sumtwos , sumfives ) ; } } public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = sc . nextInt ( ) ; twos [ i ] = returnTwos ( arr [ i ] ) ; fives [ i ] = returnFives ( arr [ i ] ) ; } while ( m -- != 0 ) { solve_queries ( arr , n ) ; } System . out . println ( sum ) ; } }


"Array containing power of 2 whose XOR and Sum of elements equals X | Java implementation implementation of the above approach ; Function to return the required array ; Store the power of 2 ; while n is greater than 0 ; if there is 1 in binary representation ; Divide n by 2 Multiply p2 by 2 ; Driver code ; Get the answer ; Printing the array"
How can the above be solved in Java?

import java . util . * ; class GFG { static Vector < Long > getArray ( int n ) { Vector < Long > ans = new Vector < Long > ( ) ; long p2 = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) ans . add ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; Vector < Long > ans = getArray ( n ) ; for ( Long i : ans ) System . out . print ( i + " ▁ " ) ; } }


"Array element moved by k using single moves | Java program to find winner of game ; if the number of steps is more then n - 1 , ; initially the best is 0 and no of wins is 0. ; traverse through all the numbers ; if the value of array is more then that of previous best ; best is replaced by a [ i ] ; if not the first index ; no of wins is 1 now ; if it wins ; if any position has more then k wins then return ; Maximum element will be winner because we move smaller element at end and repeat the process . ; driver program to test the above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int winner ( int a [ ] , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i == 1 ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 1 , 3 , 4 , 5 } ; int n = a . length ; int k = 2 ; System . out . println ( winner ( a , n , k ) ) ; } }


"Array element with minimum sum of absolute differences | Java implementation of the approach ; Function to return the minimized sum ; Sort the array ; Median of the array ; Calculate the minimized sum ; Return the required sum ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int minSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int x = arr [ ( int ) n / 2 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += Math . abs ( arr [ i ] - x ) ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 9 , 3 , 6 } ; int n = arr . length ; System . out . println ( minSum ( arr , n ) ) ; } }


"Array element with minimum sum of absolute differences | Set 2 | Java program for the above approach ; Function to find the element with minimum sum of differences between any elements in the array ; Stores the required X and sum of absolute differences ; Calculate sum of array elements ; The sum of absolute differences can 't be greater than sum ; Update res that gives the minimum sum ; If the current difference is less than the previous difference ; Update min_diff and res ; Print the resultant value ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void minimumDiff ( int [ ] arr , int N ) { int res = arr [ 0 ] , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int min_diff = sum ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = Math . abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; minimumDiff ( arr , N ) ; } }


"Array formed using sum of absolute differences of that element with all other elements | Java program for the above approach ; Function to return the new array ; Length of the arraylist ; Initialize the Arraylist ; Sum of absolute differences of element with all elements ; Initialize int sum to 0 ; Add the value of sum to ans ; Return the final ans ; Driver Code ; Given array arr [ ] ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { private static List < Integer > calculate ( int [ ] arr ) { int n = arr . length ; List < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < arr . length ; j ++ ) { sum += Math . abs ( arr [ i ] - arr [ j ] ) ; } ans . add ( sum ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; System . out . println ( calculate ( arr ) ) ; } }


"Array obtained by repeatedly reversing array after every insertion from given array | Java program of the above approach ; Function to generate the array by inserting array elements one by one followed by reversing the array ; Doubly ended Queue ; Iterate over the array ; Push array elements alternately to the front and back ; If size of list is odd ; Reverse the list ; Print the elements of the array ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void generateArray ( int arr [ ] , int n ) { Deque < Integer > ans = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . addFirst ( arr [ i ] ) ; else ans . add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { Collections . reverse ( Arrays . asList ( ans ) ) ; } for ( int x : ans ) { System . out . print ( x + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }


"Array range queries for elements with frequency same as value | Java Program to answer Q queries to find number of times an element x appears x times in a Query subarray ; Returns the count of number x with frequency x in the subarray from start to end ; map for frequency of elements ; store frequency of each element in arr [ start ; end ] ; Count elements with same frequency as value ; Driver code ; 2D array of queries with 2 columns ; calculating number of queries"
How can the above be solved in Java?

import java . util . HashMap ; import java . util . Map ; class GFG { static int solveQuery ( int start , int end , int arr [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = start ; i <= end ; i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; int count = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) if ( entry . getKey ( ) == entry . getValue ( ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = A . length ; int [ ] [ ] queries = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i ] [ 0 ] ; int end = queries [ i ] [ 1 ] ; System . out . println ( " Answer ▁ for ▁ Query ▁ " + ( i + 1 ) + " ▁ = ▁ " + solveQuery ( start , end , A ) ) ; } } }


"Array range queries for searching an element | Java program to determine if the element exists for different range queries ; Structure to represent a query range ; Find the root of the group containing the element at index x ; merge the two groups containing elements at indices x and y into one group ; make n subsets with every element as its root ; consecutive elements equal in value are merged into one single group ; Driver code ; check if the current element in consideration is equal to x or not if it is equal , then x exists in the range ; Print if x exists or not"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Query { int L , R , X ; public Query ( int L , int R , int X ) { this . L = L ; this . R = R ; this . X = X ; } } ; static int maxn = 100 ; static int [ ] root = new int [ maxn ] ; static int find ( int x ) { if ( x == root [ x ] ) return x ; else return root [ x ] = find ( root [ x ] ) ; } static void uni ( int x , int y ) { int p = find ( x ) , q = find ( y ) ; if ( p != q ) { root [ p ] = root [ q ] ; } } static void initialize ( int a [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < n ; i ++ ) root [ i ] = i ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] == a [ i - 1 ] ) uni ( i , i - 1 ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 1 , 5 , 4 , 5 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 2 , 2 ) , new Query ( 1 , 4 , 1 ) , new Query ( 2 , 4 , 5 ) } ; int m = q . length ; initialize ( a , n , q , m ) ; for ( int i = 0 ; i < m ; i ++ ) { int flag = 0 ; int l = q [ i ] . L , r = q [ i ] . R , x = q [ i ] . X ; int p = r ; while ( p >= l ) { if ( a [ p ] == x ) { flag = 1 ; break ; } p = find ( p ) - 1 ; } if ( flag != 0 ) System . out . println ( x + " ▁ exists ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; else System . out . println ( x + " ▁ does ▁ not ▁ exist ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; } } }


"Array range queries over range queries | Java program to perform range queries over range queries . ; For prefix sum array ; This function is used to apply square root decomposition in the record array ; traversing first block in range ; traversing completely overlapped blocks in range ; traversing last block in range ; Function to print the resultant array ; Driver code ; If query is of type 2 then function call to record_func ; If query is of type 1 then simply add1 to the record array ; Merging the value of the block in the record array ; If query is of type 1 then the array elements are over - written by the record array ; The prefix sum of the array ; Printing the resultant array"
How can the above be solved in Java?

public class GFG { static final int max = 10000 ; static void update ( int arr [ ] , int l ) { arr [ l ] += arr [ l - 1 ] ; } static void record_func ( int block_size , int block [ ] , int record [ ] , int l , int r , int value ) { while ( l < r && l % block_size != 0 && l != 0 ) { record [ l ] += value ; l ++ ; } while ( l + block_size <= r + 1 ) { block [ l / block_size ] += value ; l += block_size ; } while ( l <= r ) { record [ l ] += value ; l ++ ; } } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int arr [ ] = new int [ n ] , record [ ] = new int [ m ] ; int block_size = ( int ) Math . sqrt ( m ) ; int block [ ] = new int [ max ] ; int command [ ] [ ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( command [ i ] [ 0 ] == 2 ) { int x = i / ( block_size ) ; record_func ( block_size , block , record , command [ i ] [ 1 ] - 1 , command [ i ] [ 2 ] - 1 , ( block [ x ] + record [ i ] + 1 ) ) ; } else { record [ i ] ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { int check = ( i / block_size ) ; record [ i ] += block [ check ] ; } for ( int i = 0 ; i < m ; i ++ ) { if ( command [ i ] [ 0 ] == 1 ) { arr [ command [ i ] [ 1 ] - 1 ] += record [ i ] ; if ( ( command [ i ] [ 2 ] - 1 ) < n - 1 ) { arr [ ( command [ i ] [ 2 ] ) ] -= record [ i ] ; } } } for ( int i = 1 ; i < n ; i ++ ) { update ( arr , i ) ; } print ( arr , n ) ; } }


"Array range queries over range queries | Java program to perform range queries over range queries . ; Function to create the record array ; Driver Code ; Build query matrix ; If query is of type 2 then function call to record_sum ; If query is of type 1 then simply add 1 to the record array ; for type 1 queries adding the contains of record array to the main array record array ; printing the array"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static void record_sum ( int record [ ] , int l , int r , int n , int adder ) { for ( int i = l ; i <= r ; i ++ ) { record [ i ] += adder ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int arr [ ] = new int [ n ] ; Arrays . fill ( arr , 0 ) ; int query [ ] [ ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; int record [ ] = new int [ m ] ; Arrays . fill ( record , 0 ) ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) { record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; } else { record_sum ( record , i , i , m , 1 ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) { record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }


"Array range queries over range queries | Java program to perform range queries over range queries . ; Function to execute type 1 query ; incrementing the array by 1 for type 1 queries ; Function to execute type 2 query ; If the query is of type 1 function call to type 1 query ; If the query is of type 2 recursive call to type 2 query ; Driver Code ; Input size of array amd number of queries ; Build query matrix ; Perform queries ; printing the result"
How can the above be solved in Java?

import java . util . * ; class GFG { static void type1 ( int [ ] arr , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) arr [ i ] ++ ; } static void type2 ( int [ ] arr , int [ ] [ ] query , int start , int limit ) { for ( int i = start ; i <= limit ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int [ ] arr = new int [ n + 1 ] ; int [ ] temp = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ ] [ ] query = new int [ 6 ] [ 4 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { query [ i ] [ 0 ] = temp [ j ++ ] ; query [ i ] [ 1 ] = temp [ j ++ ] ; query [ i ] [ 2 ] = temp [ j ++ ] ; } for ( int i = 1 ; i <= m ; i ++ ) if ( query [ i ] [ 0 ] == 1 ) type1 ( arr , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else if ( query [ i ] [ 0 ] == 2 ) type2 ( arr , query , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } }


"Array range queries over range queries | Java program to perform range queries over range queries . ; Updates a node in Binary Index Tree ( BITree ) at given index in BITree . The given value ' val ' is added to BITree [ i ] and all of its ancestors in tree . ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse all ancestors and add ' val ' ; Add ' val ' to current node of BI Tree ; Update index to that of parent in update View ; Constructs and returns a Binary Indexed Tree for given array of size n . ; Create and initialize BITree [ ] as 0 ; Returns sum of arr [ 0. . index ] . This function assumes that the array is preprocessed and partial sums of array elements are stored in BITree [ ] ; index in BITree [ ] is 1 more than the index in arr [ ] ; Traverse ancestors of BITree [ index ] ; Add element of BITree to sum ; Move index to parent node in getSum View ; Function to update the BITree ; Driver code ; BITree for query of type 2 ; BITree for query of type 1 ; Input the queries in a 2D matrix ; If query is of type 2 then function call to update with BITree ; If query is of type 1 then function call to update with BITree2"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] = ( val + BITree [ index ] ) ; index = ( index + ( index & ( - index ) ) ) ; } return ; } static int [ ] constructBITree ( int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; return BITree ; } static int getSum ( int BITree [ ] , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum = ( sum + BITree [ index ] ) ; index -= index & ( - index ) ; } return sum ; } static void update ( int BITree [ ] , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; return ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int temp [ ] = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ ] [ ] q = new int [ 6 ] [ 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = temp [ j ++ ] ; q [ i ] [ 1 ] = temp [ j ++ ] ; q [ i ] [ 2 ] = temp [ j ++ ] ; } int [ ] BITree = constructBITree ( m ) ; int [ ] BITree2 = constructBITree ( n ) ; Scanner sc = new Scanner ( System . in ) ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = sc . nextInt ( ) ; q [ i ] [ 1 ] = sc . nextInt ( ) ; q [ i ] [ 2 ] = sc . nextInt ( ) ; } for ( int i = m ; i >= 1 ; i -- ) if ( q [ i ] [ 0 ] == 2 ) update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , 1 ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 2 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , val ) ; } } for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 1 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree2 , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , n , ( val + 1 ) ) ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( getSum ( BITree2 , i - 1 ) + " ▁ " ) ; } }


"Array range queries to find the maximum Fibonacci number with updates | Java code for range maximum query and updates ; A utility function to get the middle index of given range . ; Function to create hash table to check Fibonacci numbers ; * A recursive function to get the sum of * values in given range of the array . * The following are parameters for this function . * * st -> Pointer to segment tree * node -> Index of current node * in the segment tree . * ss & se -> Starting and ending indexes * of the segment represented * by current node , i . e . , st [ node ] * l & r -> Starting and ending indexes * of range query ; If segment of this node is completely part of given range , then return the max of segment ; If segment of this node does not belong to given range ; If segment of this node is partially the part of given range ; * A recursive function to update the nodes which * have the given index in their range . The following * are parameters st , ss and se are same as defined * above index -> index of the element to be updated . ; update value in array and in segment tree ; Return max of elements in range from index l ( query start ) to r ( query end ) . ; Check for erroneous input values ; A recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is one element in array , store it in current node of segment tree and return ; If there are more than one elements , then recur for left and right subtrees and store the max of values in this node ; * Function to construct segment tree * from given array . This function allocates * memory for segment tree . ; Height of segment tree ; Maximum size of segment tree ; Allocate memory ; Fill the allocated memory st ; Return the constructed segment tree ; Driver code ; find the largest node value in the array ; creating a set containing all fibonacci numbers upto the maximum data value in the array ; Build segment tree from given array ; Print max of values in array from index 1 to 3 ;..."
How can the above be solved in Java?

 import java . util . Arrays ; import java . util . HashSet ; import java . util . Set ; class GFG { static Set < Integer > fibonacci = new HashSet < > ( ) ; static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static void createHash ( int maxElement ) { int prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } static int MaxUtil ( int [ ] st , int ss , int se , int l , int r , int node ) { if ( l <= ss && r >= se ) return st [ node ] ; if ( se < l ss > r ) return - 1 ; int mid = getMid ( ss , se ) ; return Math . max ( MaxUtil ( st , ss , mid , l , r , 2 * node + 1 ) , MaxUtil ( st , mid + 1 , se , l , r , 2 * node + 2 ) ) ; } static void updateValue ( int arr [ ] , int [ ] st , int ss , int se , int index , int value , int node ) { if ( index < ss index > se ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } if ( ss == se ) { arr [ index ] = value ; if ( fibonacci . contains ( value ) ) st [ node ] = value ; else st [ node ] = - 1 ; } else { int mid = getMid ( ss , se ) ; if ( index >= ss && index <= mid ) updateValue ( arr , st , ss , mid , index , value , 2 * node + 1 ) ; else updateValue ( arr , st , mid + 1 , se , index , value , 2 * node + 2 ) ; st [ node ] = Math . max ( st [ 2 * node + 1 ] , st [ 2 * node + 2 ] ) ; } return ; } static int getMax ( int [ ] st , int n , int l , int r ) { if ( l < 0 r > n - 1 l > r ) { System . out . printf ( "Invalid Input
"); return - 1 ; } return MaxUtil ( st , 0 , n - 1 , l , r , 0 ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( fibonacci . contains ( arr [ ss ] ) ) st [ si ] = arr [ ss ] ; else st [ si ] = - 1 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = Math . max ( constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) , constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ) ; return st [ si ] ; } static int [ ]...


"Array range queries to find the number of perfect square elements with updates | Java program to find number of perfect square numbers in a subarray and performing updates ; Function to check if a number is a perfect square or not ; Find floating point value of square root of x . ; If square root is an integer ; A utility function to get the middle index from corner indexes . ; where st - . Pointer to segment tree index - . Index of current node in the segment tree . Initially 0 is passed as root is always at index 0 ss & se - . Starting and ending indexes of the segment represented by current node i . e . st [ index ] qs & qe - . Starting and ending indexes of query range ; If segment of this node is a part of given range , then return the number of perfect square numbers in the segment ; If segment of this node is outside the given range ; If a part of this segment overlaps with the given range ; where st , si , ss & se are same as getSumUtil ( ) i - . index of the element to be updated . This index is in input array . diff - . Value to be added to all nodes which have i in range ; Base Case : If the input index lies outside the range of this segment ; If the input index is in range of this node , then update the value of the node and its children ; Function to update a value in the input array and segment tree . It uses updateValueUtil ( ) to update the value in segment tree ; Check for erroneous input index ; Update the value in array ; Case 1 : Old and new values both are perfect square numbers ; Case 2 : Old and new values both not perfect square numbers ; Case 3 : Old value was perfect square , new value is not a perfect square ; Case 4 : Old value was non - perfect square , new_val is perfect square ; Update values of nodes in segment tree ; Return no . of perfect square numbers in range from index qs ( query start ) to qe ( query end ) . It mainly uses queryUtil ( ) ; Recursive function that constructs Segment Tree for array [ ss . . se ] . si is index of current node in segment tree st ; If there is..."
How can the above be solved in Java?

 import java . util . * ; class GFG { static final int MAX = 1000 ; static boolean isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ? true : false ; } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int arr [ ] , int [ ] st , int n , int i , int new_val ) { if ( i < 0 i > n - 1 ) { System . out . printf ( " Invalid ▁ Input " ) ; return ; } int diff = 0 , oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPerfectSquare ( oldValue ) && isPerfectSquare ( new_val ) ) return ; if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) return ; if ( isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = - 1 ; } if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void query ( int [ ] st , int n , int qs , int qe ) { int perfectSquareInRange = queryUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; System . out . print ( perfectSquareInRange + "
"); } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( isPerfectSquare ( arr [ ss ] ) ) st [ si ] = 1 ; else st [ si ] = 0 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) +...


"Array sum after replacing all occurrences of X by Y for Q queries | Java implementation to find the sum of the array for the given Q queries ; Function that print the sum of the array for Q queries ; Stores the frequencies of array elements Create an empty hash map ; Calculate the sum of the initial array and store the frequency of each element in map ; Iterate for all the queries ; Store query values ; Decrement the sum accordingly ; Increment the sum accordingly ; Set count of Y [ i ] ; Reset count of X [ i ] ; Print the sum ; Driver code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { public static void sumOfTheArrayForQuery ( int [ ] A , int N , int [ ] X , int [ ] Y , int Q ) { int sum = 0 ; HashMap < Integer , Integer > count = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . containsKey ( A [ i ] ) ) { count . replace ( A [ i ] , count . get ( A [ i ] ) + 1 ) ; } else { count . put ( A [ i ] , 1 ) ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = X [ i ] , y = Y [ i ] ; if ( count . containsKey ( X [ i ] ) ) { sum -= count . get ( X [ i ] ) * X [ i ] ; sum += count . get ( X [ i ] ) * Y [ i ] ; } if ( count . containsKey ( Y [ i ] ) && count . containsKey ( X [ i ] ) ) { count . replace ( Y [ i ] , count . get ( Y [ i ] ) + count . get ( X [ i ] ) ) ; } if ( count . containsKey ( X [ i ] ) ) { count . replace ( X [ i ] , 0 ) ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 2 } ; int X [ ] = { 2 , 3 , 5 } ; int Y [ ] = { 3 , 1 , 2 } ; int N = arr . length ; int Q = X . length ; sumOfTheArrayForQuery ( arr , N , X , Y , Q ) ; } }


"Array value by repeatedly replacing max 2 elements with their absolute difference | Java program to find the array value by repeatedly replacing max 2 elements with their absolute difference ; Function that return last value of array ; Build a binary max_heap ; For max 2 elements ; Iterate until queue is not empty ; If only 1 element is left ; Return the last remaining value ; Check that difference is non zero ; Finally return 0 ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int lastElement ( int [ ] arr ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < arr . length ; i ++ ) pq . add ( arr [ i ] ) ; int m1 , m2 ; while ( ! pq . isEmpty ( ) ) { if ( pq . size ( ) == 1 ) { return pq . poll ( ) ; } m1 = pq . poll ( ) ; m2 = pq . poll ( ) ; if ( m1 != m2 ) pq . add ( m1 - m2 ) ; } return 0 ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 2 , 7 , 4 , 1 , 8 , 1 , 1 } ; System . out . println ( lastElement ( arr ) ) ; } }