"Check if string is palindrome after removing all consecutive duplicates | Java program for the above approach ; Function to check if a String is palindrome or not ; Length of the String ; Check if its a palindrome ; If the palindromic condition is not met ; Return true as str is palindromic ; Function to check if String str is palindromic after removing every consecutive characters from the str ; Length of the String str ; Create an empty compressed String ; The first character will always be included in the final String ; Check all the characters of the String ; If the current character is not same as its previous one , then insert it in the final String ; Check if the compressed String is a palindrome ; Driver Code ; Given String ; Function call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( len - i - 1 ) ) return false ; } return true ; } static boolean isCompressablePalindrome ( String str ) { int len = str . length ( ) ; String compressed = " " ; compressed = String . valueOf ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( i - 1 ) ) compressed += str . charAt ( i ) ; } return isPalindrome ( compressed ) ; } public static void main ( String [ ] args ) { String str = " abbcbbbaaa " ; if ( isCompressablePalindrome ( str ) ) System . out . print ( "Yes
"); else System . out . print ( "No
"); } }


"Check if strings are rotations of each other or not | Set 2 | Java program to check if two strings are rotations of each other . ; create lps [ ] that will hold the longest prefix suffix values for pattern ; length of the previous longest prefix suffix ; lps [ 0 ] is always 0 ; the loop calculates lps [ i ] for i = 1 to n - 1 ; match from that rotating point ; Driver code"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class stringMatching { public static boolean isRotation ( String a , String b ) { int n = a . length ( ) ; int m = b . length ( ) ; if ( n != m ) return false ; int lps [ ] = new int [ n ] ; int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( len ) ) { lps [ i ] = ++ len ; ++ i ; } else { if ( len == 0 ) { lps [ i ] = 0 ; ++ i ; } else { len = lps [ len - 1 ] ; } } } i = 0 ; for ( int k = lps [ n - 1 ] ; k < m ; ++ k ) { if ( b . charAt ( k ) != a . charAt ( i ++ ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " ABACD " ; String s2 = " CDABA " ; System . out . println ( isRotation ( s1 , s2 ) ? "1" : "0" ) ; } }


"Check if substrings from three given strings can be concatenated to form a palindrome | Java program for the above approach ; Function to check if subStrings from three given Strings can be concatenated to form a palindrome ; Mask for S1 and S2 ; Set ( i - ' a ' ) th bit in maskA ; Set ( i - ' a ' ) th bit in maskC ; If the bitwise AND is > 0 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String make_palindrome ( String S1 , String S2 , String S3 ) { int maskA = 0 , maskC = 0 ; for ( char i : S1 . toCharArray ( ) ) maskA |= ( 1 << ( i - ' a ' ) ) ; for ( char i : S3 . toCharArray ( ) ) maskC |= ( 1 << ( i - ' a ' ) ) ; if ( ( maskA & maskC ) > 0 ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { String S1 = " adcb " , S2 = " bcdb " , S3 = " abe " ; System . out . print ( make_palindrome ( S1 , S2 , S3 ) ) ; } }


"Check if suffix and prefix of a string are palindromes | Java implementation of the approach ; Utility reverse ; Swap values of l and r ; Function to check whether the string is a palindrome ; reverse the string to compare with the original string ; check if both are same ; Function to check whether the string has prefix and suffix substrings of length greater than 1 which are palindromes . ; check all prefix substrings ; check if the prefix substring is a palindrome ; If we did not find any palindrome prefix of length greater than 1. ; check all suffix substrings , as the string is reversed now ; check if the suffix substring is a palindrome ; If we did not find a suffix ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } static boolean isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . equals ( p ) ) ; } static boolean CheckStr ( String s ) { int l = s . length ( ) ; int i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( 0 , i ) ) ) { break ; } } if ( i == ( l + 1 ) ) { return false ; } i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( l - i , l ) ) ) { return true ; } } return false ; } public static void main ( String args [ ] ) { String s = " abccbarfgdbd " ; if ( CheckStr ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if sum of Fibonacci elements in an Array is a Fibonacci number or not | Java program to check whether the sum of fibonacci elements of the array is a Fibonacci number or not ; Hash to store the Fibonacci numbers up to Max ; Function to create the hash table to check Fibonacci numbers ; Inserting the first two Fibonacci numbers into the hash ; Add the remaining Fibonacci numbers based on the previous two numbers ; Function to check if the sum of Fibonacci numbers is Fibonacci or not ; Find the sum of all Fibonacci numbers ; Iterating through the array ; If the sum is Fibonacci then return true ; Driver code ; array of elements ; Creating a set containing all fibonacci numbers"
How can the above be solved in Java?

import java . util . * ; class GFG { static final int MAX = 100005 ; static HashSet < Integer > fibonacci = new HashSet < Integer > ( ) ; static void createHash ( ) { int prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= MAX ) { int temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } static boolean checkArray ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( fibonacci . contains ( arr [ i ] ) ) sum += arr [ i ] ; if ( fibonacci . contains ( sum ) ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 2 } ; int n = arr . length ; createHash ( ) ; if ( checkArray ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if sum of arr [ i ] / j for all possible pairs ( i , j ) in an array is 0 or not | Java program for the above approach ; Function to check if sum of all values of ( arr [ i ] / j ) for all 0 < i <= j < ( N - 1 ) is 0 or not ; Stores the required sum ; Traverse the array ; If the sum is equal to 0 ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void check ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 3 , - 2 , - 1 } ; int N = arr . length ; check ( arr , N ) ; } }


"Check if sum of array can be made equal to X by removing either the first or last digits of every array element | Java program for the above approach ; Utility Function to check if the sum of the array elements can be made equal to X by removing either the first or last digits of every array element ; Base Case ; Convert arr [ i ] to string ; Remove last digit ; Remove first digit ; Recursive function call ; Function to check if sum of given array can be made equal to X or not ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean makeSumX ( int arr [ ] , int X , int S , int i ) { if ( i == arr . length ) { return S == X ; } String a = Integer . toString ( arr [ i ] ) ; int l = Integer . parseInt ( a . substring ( 0 , a . length ( ) - 1 ) ) ; int r = Integer . parseInt ( a . substring ( 1 ) ) ; boolean x = makeSumX ( arr , X , S + l , i + 1 ) ; boolean y = makeSumX ( arr , X , S + r , i + 1 ) ; return ( x y ) ; } static void Check ( int arr [ ] , int X ) { if ( makeSumX ( arr , X , 0 , 0 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 545 , 433 , 654 , 23 } ; int X = 134 ; Check ( arr , X ) ; } }


"Check if sum of count of digits of array elements is Prime or not | Java program for the above approach ; Function to check whether a number is prime or not ; Corner cases ; If given number is a multiple of 2 or 3 ; Function to check if sum of count of digits of all array elements is prime or not ; Initialize sum with 0 ; Traverse over the array ; Convert array element to string ; Add the count of digits to sum ; Print the result ; Drive Code ; Function call"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void CheckSumPrime ( int [ ] A , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String s = Integer . toString ( A [ i ] ) ; sum += s . length ( ) ; } if ( isPrime ( sum ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { 1 , 11 , 12 } ; int N = A . length ; CheckSumPrime ( A , N ) ; } }


"Check if sum of divisors of two numbers are same | Java program to find if two numbers are equivalent or not ; Function to calculate sum of all proper divisors num -- > given natural number ; To store sum of divisors ; Find all divisors and add them ; Function to check if both numbers are equivalent or not ; Drivers code"
How can the above be solved in Java?

import java . math . * ; class GFG { static int divSum ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) sum = sum + i + n / i ; return sum ; } static boolean areEquivalent ( int num1 , int num2 ) { return divSum ( num1 ) == divSum ( num2 ) ; } public static void main ( String [ ] args ) { int num1 = 559 ; int num2 = 703 ; if ( areEquivalent ( num1 , num2 ) ) System . out . println ( " Equivalent " ) ; else System . out . println ( " Not ▁ Equivalent " ) ; } }


"Check if sum of exactly K elements of the Array can be odd or not | Java implementation of the above approach ; Function returns true if it is possible to have odd sum ; Counting number of odd and even elements ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPossible ( int arr [ ] , int N , int K ) { int oddCount = 0 , evenCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCount ++ ; } else { oddCount ++ ; } } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 8 } ; int K = 5 ; int N = arr . length ; if ( isPossible ( arr , N , K ) ) { System . out . println ( " Possible " ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } }


"Check if sum of the given array can be reduced to 0 by reducing array elements by K | Java program for the above approach ; Function to check if the sum can be made 0 or not ; Stores sum of array elements ; Traverse the array ; Driver Code ; Given array arr [ ]"
How can the above be solved in Java?

import java . util . * ; class GFG { static int sumzero ( int arr [ ] , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 0 ) System . out . print ( " Yes " ) ; else if ( sum > 0 ) { if ( sum % K == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } else System . out . print ( " No " ) ; return 0 ; } public static void main ( String [ ] args ) { int K , N ; int arr1 [ ] = { 1 , - 6 , 2 , 2 } ; K = 1 ; N = arr1 . length ; sumzero ( arr1 , N , K ) ; } }


"Check if summation of two words is equal to target word | Java program for the above approach ; Function to check weather summation of two words equal to target word ; Store the length of each String ; Reverse the Strings A , B and C ; Stores the remainder ; Iterate in the range [ 0 , Math . max ( L , Math . max ( M , N ) ) ] ; Stores the integer at ith position from the right in the sum of A and B ; If i is less than L ; If i is less than M ; Update rem and curr ; If i is less than N and curr is not equal to C [ i ] - ' a ' , return " No " ; If rem is greater than 0 , return " No " ; Otherwise , return " Yes " ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static String isSumEqual ( String A , String B , String C ) { int L = A . length ( ) ; int M = B . length ( ) ; int N = A . length ( ) ; A = reverse ( A ) ; B = reverse ( B ) ; C = reverse ( C ) ; int rem = 0 ; for ( int i = 0 ; i < Math . max ( L , Math . max ( M , N ) ) ; i ++ ) { int curr = rem ; if ( i < L ) curr += A . charAt ( i ) - ' a ' ; if ( i < M ) curr += B . charAt ( i ) - ' a ' ; rem = curr / 10 ; curr %= 10 ; if ( i < N && curr != C . charAt ( i ) - ' a ' ) { return " No " ; } } if ( rem > 0 ) return " No " ; else return " Yes " ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String A = " acb " , B = " cba " , C = " cdb " ; System . out . print ( isSumEqual ( A , B , C ) ) ; } }


"Check if sums of i | Java program to check if there are two adjacent set bits . ; Function to check the if sum of a row is same as corresponding column ; Driver code ; number of rows ; number of columns"
How can the above be solved in Java?

 public class GFG { static boolean areSumSame ( int a [ ] [ ] , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 ; sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i ] [ j ] ; sum2 += a [ j ] [ i ] ; } if ( sum1 == sum2 ) return true ; } return false ; } public static void main ( String args [ ] ) { int n = 4 ; int m = 4 ; int M [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 9 , 5 , 3 , 1 } , { 0 , 3 , 5 , 6 } , { 0 , 4 , 5 , 6 } } ; if ( areSumSame ( M , n , m ) == true ) System . out . print ( "1
"); else System . out . print ( "0
"); } }


"Check if the Depth of Parentheses is correct in the given String | Function to check if the Depth of Parentheses is correct in the given String ; Appending if the Character is not integer ; Iterating till the entire Digit is read ; Check if character is ' ( ' ; Increment depth by 1 ; Increment open by 1 ; Check if character is ' ) ' ; Decrement depth by 1 ; Increment close by 1 ; Check if open parentheses NOT equals close parentheses ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean Formatted ( String s ) { Vector < Character > k = new Vector < Character > ( ) ; int i = 0 ; while ( i < s . length ( ) ) { if ( s . charAt ( i ) == ' ) ' || s . charAt ( i ) == ' ( ' ) { k . add ( s . charAt ( i ) ) ; i += 1 ; } else { char st = 0 ; while ( s . charAt ( i ) != ' ) ' && s . charAt ( i ) != ' ) ' ) { st = s . charAt ( i ) ; i = i + 1 ; } k . add ( st ) ; } } int depth = 0 , flag = 1 ; int open = 0 , close = 0 ; for ( char i2 : k ) { if ( i2 == ' ( ' ) { depth += 1 ; open += 1 ; } else if ( i2 == ' ) ' ) { depth -= 1 ; close += 1 ; } else { if ( i2 - '0' != depth ) { flag = 0 ; break ; } } } if ( open != close ) flag = 0 ; return ( flag == 1 ) ? true : false ; } public static void main ( String [ ] args ) { String s = " ( (2 ) ( (3 ) ) ) " ; boolean k = Formatted ( s ) ; if ( k == true ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }


"Check if the Left View of the given tree is sorted or not | Java implementation to check if the left view of the given tree is sorted or not ; Binary Tree Node ; Utility function to create a new node ; Function to find left view and check if it is sorted ; Queue to hold values ; Variable to check whether level order is sorted or not ; Iterate until the queue is empty ; Traverse every level in tree ; Variable for initial level ; Checking values are sorted or not ; Push left value if it is not null ; Push right value if it is not null ; Pop out the values from queue ; Check if the value are not sorted then break the loop ; Driver code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static class node { int val ; node right , left ; } ; static node newnode ( int key ) { node temp = new node ( ) ; temp . val = key ; temp . right = null ; temp . left = null ; return temp ; } static void func ( node root ) { Queue < node > q = new LinkedList < > ( ) ; boolean t = true ; q . add ( root ) ; int i = - 1 , j = - 1 ; while ( ! q . isEmpty ( ) ) { int h = q . size ( ) ; while ( h > 0 ) { root = q . peek ( ) ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . add ( root . left ) ; } if ( root . right != null ) { q . add ( root . right ) ; } h = h - 1 ; q . remove ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) System . out . print ( " true " + "
"); else System . out . print ( " false " + "
"); } public static void main ( String [ ] args ) { node root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ; } }


"Check if the Matrix follows the given constraints or not | Java implementation of the above approach ; Function checks if n is prime or not ; Corner case ; Check from 2 to Math . sqrt ( n ) ; Function returns sum of all elements of matrix ; Stores the sum of the matrix ; Function to check if all a [ i ] [ j ] with prime ( i + j ) are prime ; If index is prime ; If element not prime ; Driver code ; Check for both conditions"
How can the above be solved in Java?

 class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int takeSum ( int a [ ] [ ] ) { int sum = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) sum += a [ i ] [ j ] ; return sum ; } static boolean checkIndex ( int n , int m , int a [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( isPrime ( i + j ) ) { if ( ! isPrime ( a [ i ] [ j ] ) ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = takeSum ( a ) ; if ( isPrime ( sum ) && checkIndex ( n , m , a ) ) { System . out . print ( " YES " + "
"); } else { System . out . print ( " NO " + "
"); } } }


"Check if the Matrix follows the given constraints or not | Java implementation of the above approach ; Stores true at prime indices ; Function to generate the prime numbers using Sieve of Eratosthenes ; If p is still true ; Mark all multiples of p ; Function returns sum of all elements of matrix ; Function to check if for all prime ( i + j ) , a [ i ] [ j ] is prime ; If index is prime ; Driver Code ; Check for both conditions"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean [ ] prime ; static void buildSieve ( int sum ) { prime = new boolean [ sum + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < ( sum + 1 ) ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < ( sum + 1 ) ; i += p ) prime [ i ] = false ; } } } static int getSum ( int a [ ] [ ] ) { int s = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) s += a [ i ] [ j ] ; return s ; } static boolean checkIndex ( int n , int m , int a [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { if ( prime [ i + j ] && ! prime [ a [ i ] [ j ] ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = getSum ( a ) ; buildSieve ( sum ) ; if ( prime [ sum ] && checkIndex ( n , m , a ) ) { System . out . print ( " YES " + "
"); } else System . out . print ( " NO " + "
"); } }


"Check if the XOR of an array of integers is Even or Odd | Java program to check if the XOR of an array is Even or Odd ; Function to check if the XOR of an array of integers is Even or Odd ; Count the number of odd elements ; If count of odd elements is odd , then XOR will be odd ; Else even ; Driver Code ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static String check ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) count ++ ; } if ( ( count & 1 ) != 0 ) return " Odd " ; else return " Even " ; } public static void main ( String args [ ] ) { int [ ] arr = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; System . out . println ( check ( arr , n ) ) ; } }


"Check if the array can be sorted only if the elements on given positions can be swapped | Java program to check if the array can be sorted only if the elements on the given positions can be swapped ; Function to check if the array can be sorted only if the elements on the given positions can be swapped ; Creating an array for marking the positions ; Iterating through the array and mark the positions ; Iterating through the given array ; If pos [ i ] is 1 , then incrementing till 1 is continuously present in pos ; Sorting the required segment ; Checking if the vector is sorted or not ; Print yes if it is sorted ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { public static void check_vector ( int [ ] A , int n , int [ ] p ) { int [ ] pos = new int [ A . length ] ; for ( int i = 0 ; i < p . length ; i ++ ) { pos [ p [ i ] - 1 ] = 1 ; } int flag = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos [ i ] == 0 ) continue ; int j = i ; while ( j < n && pos [ j ] != 0 ) ++ j ; Arrays . sort ( A , i , j + 1 ) ; i = j ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 1 } ; int p [ ] = { 1 , 2 } ; check_vector ( A , A . length , p ) ; } }


"Check if the array has an element which is equal to XOR of remaining elements | Java implementation of the approach ; Function that returns true if the array contains an element which is equal to the XOR of the remaining elements ; To store the XOR of all the array elements ; For every element of the array ; Take the XOR after excluding the current element ; If the XOR of the remaining elements is equal to the current element ; If no such element is found ; Driver code"
How can the above be solved in Java?

class GFG { static boolean containsElement ( int [ ] arr , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { int x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 2 , 4 , 15 , 1 } ; int n = arr . length ; if ( containsElement ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the array has an element which is equal to product of remaining elements | Java implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Calculate the product of all the elements ; Return true if any such element is found ; If no element is found ; Drivercode"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean CheckArray ( int arr [ ] , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) prod *= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] == prod / arr [ i ] ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = arr . length ; if ( CheckArray ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if the array has an element which is equal to product of remaining elements | Java implementation of the above approach ; Function to Check if the array has an element which is equal to product of all the remaining elements ; Storing frequency in map ; Calculate the product of all the elements ; If the prod is a perfect square then check if its square root ; exist in the array or not ; Driver code"
How can the above be solved in Java?

import java . util . ArrayList ; class GFG { static boolean CheckArray ( int arr [ ] , int n ) { int prod = 1 ; ArrayList < Integer > freq = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { freq . add ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = ( int ) Math . sqrt ( prod ) ; if ( root * root == prod ) { if ( freq . contains ( root ) & freq . lastIndexOf ( root ) != ( freq . size ( ) ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = arr . length ; if ( CheckArray ( arr , n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if the array has an element which is equal to sum of all the remaining elements | Java program to Check if the array has an element which is equal to sum of all the remaining elements ; Function to check if such element exists or not ; Storing frequency in map ; Stores the sum ; Traverse the array and count the array elements ; Only possible if sum is even ; If half sum is available ; Driver code"
How can the above be solved in Java?

import java . util . * ; class Solution { static boolean isExists ( int a [ ] , int n ) { Map < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) == null ? 0 : freq . get ( a [ i ] ) + 1 ) ; sum += a [ i ] ; } if ( sum % 2 == 0 ) { if ( freq . get ( sum / 2 ) != null ) return true ; } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 1 , 2 , 2 } ; int n = a . length ; if ( isExists ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Java implementation of the above approach ; Function to check ; Converting integer to its equivalent binary number ; If adjacent character are same then increase counter ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void hasEqualBlockFrequency ( int N ) { String S = Integer . toString ( N , 2 ) ; HashSet < Integer > p = new HashSet < Integer > ( ) ; int c = 1 ; for ( int i = 0 ; i < S . length ( ) - 1 ; i ++ ) { if ( S . charAt ( i ) == S . charAt ( i + 1 ) ) c += 1 ; else { p . add ( c ) ; c = 1 ; } p . add ( c ) ; } if ( p . size ( ) == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 5 ; hasEqualBlockFrequency ( N ) ; } }


"Check if the binary representation of a number has equal number of 0 s and 1 s in blocks | Java program to check if a number has same counts of 0 s and 1 s in every block . ; function to convert decimal to binary ; Count same bits in last block ; If n is 0 or it has all 1 s , then it is not considered to have equal number of 0 s and 1 s in blocks . ; Count same bits in all remaining blocks . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isEqualBlock ( int n ) { int first_bit = n % 2 ; int first_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit && n > 0 ) { n = n / 2 ; first_count ++ ; } if ( n == 0 ) return false ; while ( n > 0 ) { first_bit = n % 2 ; int curr_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit ) { n = n / 2 ; curr_count ++ ; } if ( curr_count != first_count ) return false ; } return true ; } public static void main ( String [ ] args ) { int n = 51 ; if ( isEqualBlock ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Java implementation of the approach ; Function that returns true if the sequence can be balanced by changing the position of at most one bracket ; Odd length string can never be balanced ; Add ' ( ' in the beginning and ' ) ' in the end of the string ; If its an opening bracket then append it to the temp string ; If its a closing bracket ; There was an opening bracket to match it with ; No opening bracket to match it with ; Sequence is balanced ; Driver Code"
How can the above be solved in Java?

import java . util . Vector ; class GFG { static boolean canBeBalanced ( String s , int n ) { if ( n % 2 == 1 ) return false ; String k = " ( " ; k += s + " ) " ; Vector < String > d = new Vector < > ( ) ; for ( int i = 0 ; i < k . length ( ) ; i ++ ) { if ( k . charAt ( i ) == ' ( ' ) d . add ( " ( " ) ; else { if ( d . size ( ) != 0 ) d . remove ( d . size ( ) - 1 ) ; else return false ; } } if ( d . isEmpty ( ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ) ( ( ) " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the bracket sequence can be balanced with at most one change in the position of a bracket | Set 2 | Java program to toggle K - th bit of a number N ; Function that returns true if the string can be balanced ; Count to check the difference between the frequencies of ' ( ' and ' ) ' and count_1 is to find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; Traverse the given string ; Increase the count ; Decrease the count ; Find the minimum value of freq ( ' ( ' ) - freq ( ' ) ' ) ; If the minimum difference is greater than or equal to - 1 and the overall difference is zero ; Driver code"
How can the above be solved in Java?

class GFG { static boolean canBeBalanced ( String s , int n ) { int count = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) count ++ ; else count -- ; count_1 = Math . min ( count_1 , count ) ; } if ( count_1 >= - 1 && count == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ( ) ) ( ) ( " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the characters in a string form a Palindrome in O ( 1 ) extra space | Java program to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; Utility function to get the position of first character in the string ; Get the position of first character in the string ; Utility function to get the position of last character in the string ; Get the position of last character in the string ; Function to check if the characters in the given string forms a Palindrome in O ( 1 ) extra space ; break , when all letters are checked ; if mismatch found , break the loop ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int firstPos ( String str , int start , int end ) { int firstChar = - 1 ; for ( int i = start ; i <= end ; i ++ ) { if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { firstChar = i ; break ; } } return firstChar ; } static int lastPos ( String str , int start , int end ) { int lastChar = - 1 ; for ( int i = start ; i >= end ; i -- ) { if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { lastChar = i ; break ; } } return lastChar ; } static boolean isPalindrome ( String str ) { int firstChar = 0 , lastChar = str . length ( ) - 1 ; boolean ch = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { firstChar = firstPos ( str , firstChar , lastChar ) ; lastChar = lastPos ( str , lastChar , firstChar ) ; if ( lastChar < 0 firstChar < 0 ) break ; if ( str . charAt ( firstChar ) == str . charAt ( lastChar ) ) { firstChar ++ ; lastChar -- ; continue ; } ch = false ; break ; } return ch ; } public static void main ( String [ ] args ) { String str = " m ▁ a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am " ; if ( isPalindrome ( str ) ) System . out . print ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if the characters of a given string are in alphabetical order | Java implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; if element at index ' i ' is less than the element at index ' i - 1' then the string is not sorted ; Driver code ; check whether the string is in alphabetical order or not"
How can the above be solved in Java?

public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { return false ; } } return true ; } static public void main ( String [ ] args ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if the characters of a given string are in alphabetical order | Java implementation of above approach ; Function that checks whether the string is in alphabetical order or not ; length of the string ; create a character array of the length of the string ; assign the string to character array ; sort the character array ; check if the character array is equal to the string or not ; Driver Code ; check whether the string is in alphabetical order or not"
How can the above be solved in Java?

import java . util . Arrays ; public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; char c [ ] = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = s . charAt ( i ) ; } Arrays . sort ( c ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c [ i ] != s . charAt ( i ) ) return false ; return true ; } public static void main ( String args [ ] ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the concatenation of first N natural numbers is divisible by 3 | Java program for the above approach ; Function that returns True if concatenation of first N natural numbers is divisible by 3 ; Check using the formula ; Driver Code ; Given Number ; Function Call"
How can the above be solved in Java?

class GFG { static boolean isDivisible ( int N ) { return ( N - 1 ) % 3 != 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( isDivisible ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the count of inversions of two given types on an Array are equal or not | Java program to implement the above approach ; Function to check if the count of inversion of two types are same or not ; If maximum value is found to be greater than a [ j ] , then that pair of indices ( i , j ) will add extra value to inversion of Type 1 ; Update max ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean solve ( int a [ ] , int n ) { int mx = Integer . MIN_VALUE ; for ( int j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = Math . max ( mx , a [ j - 1 ] ) ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 2 } ; int n = a . length ; boolean possible = solve ( a , n ) ; if ( possible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the door is open or closed | java implementation of doors open or closed ; Function to check whether ' n ' has even number of factors or not ; if ' n ' is a perfect square it has odd number of factors ; else ' n ' has even number of factors ; Function to find and print status of each door ; If even number of factors final status is closed ; else odd number of factors final status is open ; Driver program"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean hasEvenNumberOfFactors ( int n ) { double root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) System . out . print ( " closed " + " ▁ " ) ; else System . out . print ( " open " + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printStatusOfDoors ( n ) ; } }


"Check if the given Prufer sequence is valid or not | Java implementation of the approach ; Function that returns true if given Prufer sequence is valid ; Iterate in the Prufer sequence ; If out of range ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isValidSeq ( int [ ] a , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; if ( isValidSeq ( a , n ) ) System . out . println ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }


"Check if the given array can be reduced to zeros with the given operation performed given number of times | Java implementation of the approach ; Function that returns true if the array can be reduced to 0 s with the given operation performed given number of times ; Set to store unique elements ; Add every element of the array to the set ; Count of all the unique elements in the array ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean check ( int arr [ ] , int N , int K ) { HashSet < Integer > unique = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) unique . add ( arr [ i ] ) ; if ( unique . size ( ) == K ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 } ; int N = arr . length ; int K = 3 ; if ( check ( arr , N , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the given array can represent Level Order Traversal of Binary Search Tree | Java implementation to check if the given array can represent Level Order Traversal of Binary Search Tree ; to store details of a node like node ' s ▁ data , ▁ ' min ' ▁ and ▁ ' max ' ▁ to ▁ obtain ▁ the ▁ range ▁ of ▁ values ▁ where ▁ node ' s left and right child 's should lie ; function to check if the given array can represent Level Order Traversal of Binary Search Tree ; if tree is empty ; queue to store NodeDetails ; index variable to access array elements ; node details for the root of the BST ; until there are no more elements in arr [ ] or queue is not empty ; extracting NodeDetails of a node from the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be left child of ' temp . data ' or not ; Create NodeDetails for newNode and add it to the queue ; check whether there are more elements in the arr [ ] and arr [ i ] can be right child of ' temp . data ' or not ; Create NodeDetails for newNode and add it to the queue ; given array represents level order traversal of BST ; given array do not represent level order traversal of BST ; Driver code"
How can the above be solved in Java?

import java . util . * ; class Solution { static class NodeDetails { int data ; int min , max ; } ; static boolean levelOrderIsOfBST ( int arr [ ] , int n ) { if ( n == 0 ) return true ; Queue < NodeDetails > q = new LinkedList < NodeDetails > ( ) ; int i = 0 ; NodeDetails newNode = new NodeDetails ( ) ; newNode . data = arr [ i ++ ] ; newNode . min = Integer . MIN_VALUE ; newNode . max = Integer . MAX_VALUE ; q . add ( newNode ) ; while ( i != n && q . size ( ) > 0 ) { NodeDetails temp = q . peek ( ) ; q . remove ( ) ; newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] < ( int ) temp . data && arr [ i ] > ( int ) temp . min ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . min ; newNode . max = temp . data ; q . add ( newNode ) ; } newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] > ( int ) temp . data && arr [ i ] < ( int ) temp . max ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . data ; newNode . max = temp . max ; q . add ( newNode ) ; } } if ( i == n ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . length ; if ( levelOrderIsOfBST ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if the given array contains all the divisors of some integer | Java implementation of the approach ; returns th maximum element of the array ; Function that returns true if arr [ ] contains all the divisors of some integer ; Maximum element from the array ; Vector to store divisors of the maximum element i . e . X ; Store all the divisors of X ; If the lengths of a [ ] and b are different return false ; Sort a [ ] and b ; If divisors are not equal return false ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int max_element ( int a [ ] ) { int m = a [ 0 ] ; for ( int i = 0 ; i < a . length ; i ++ ) m = Math . max ( a [ i ] , m ) ; return m ; } static boolean checkDivisors ( int a [ ] , int n ) { int X = max_element ( a ) ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 1 ; i * i <= X ; i ++ ) { if ( X % i == 0 ) { b . add ( i ) ; if ( X / i != i ) b . add ( X / i ) ; } } if ( b . size ( ) != n ) return false ; Arrays . sort ( a ) ; Collections . sort ( b ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( b . get ( i ) != a [ i ] ) return false ; } return true ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 1 , 2 , 12 , 48 , 6 , 4 , 24 , 16 , 3 } ; int N = arr . length ; if ( checkDivisors ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the given array is same as its inverse permutation | Java program to implement the above approach ; Function to check if the inverse permutation of the given array is same as the original array ; Stores the inverse permutation ; Generate the inverse permutation ; Check if the inverse permutation is same as the given array ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void inverseEqual ( int arr [ ] , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int present_index = arr [ i ] - 1 ; brr [ present_index ] = i + 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != brr [ i ] ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] arr = { 1 , 4 , 3 , 2 } ; inverseEqual ( arr , n ) ; } }


"Check if the given matrix is increasing row and column wise | Java implementation of the approach ; Function that returns true if the matrix is strictly increasing ; Check if the matrix is strictly increasing ; Out of bound condition ; Out of bound condition ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int N = 2 ; static int M = 2 ; static boolean isMatrixInc ( int a [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i - 1 ] [ j ] ) return false ; } if ( j - 1 >= 0 ) { if ( a [ i ] [ j ] <= a [ i ] [ j - 1 ] ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 2 , 10 } , { 11 , 20 } } ; if ( isMatrixInc ( a ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if the given number is divisible by 71 or not | Java program to check whether a number is divisible by 71 or not ; Function to check if the number is divisible by 71 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting seven times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 71 or not ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) <= 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n - ( d * 7 ) ) ; } return ( n % 71 == 0 ) ; } public static void main ( String args [ ] ) { int N = 5041 ; if ( isDivisible ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the given string is K | Java implementation of the approach ; Function that return true if sub - string of length k starting at index i is also a prefix of the string ; k length sub - string cannot start at index i ; Character mismatch between the prefix and the sub - string starting at index i ; Function that returns true if str is K - periodic ; Check whether all the sub - strings str [ 0 , k - 1 ] , str [ k , 2 k - 1 ] ... are equal to the k length prefix of the string ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPrefix ( String str , int len , int i , int k ) { if ( i + k > len ) return false ; for ( int j = 0 ; j < k ; j ++ ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; } return true ; } static boolean isKPeriodic ( String str , int len , int k ) { for ( int i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " geeksgeeks " ; int len = str . length ( ) ; int k = 5 ; if ( isKPeriodic ( str , len , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if the given string is linear or not | Java program for the above approach ; Function to check if the given string is linear or not ; Iterate over string ; If character is not same as the first character then return false ; Increment the tmp ; Driver code ; Given String str ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean is_linear ( String s ) { int tmp = 0 ; char first = s . charAt ( 0 ) ; for ( int pos = 0 ; pos < s . length ( ) ; pos += tmp ) { if ( s . charAt ( pos ) != first ) { return false ; } tmp ++ ; } return true ; } public static void main ( String [ ] args ) { String str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if the given string is vowel prime | Java implementation of the approach ; Function that returns true if c is a vowel ; Function that returns true if all the vowels in the given string are only at prime indices ; Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . ; 0 and 1 are not prime ; If prime [ p ] is not changed , then it is a prime ; Update all multiples of p greater than or equal to the square of it numbers which are multiple of p and are less than p ^ 2 are already been marked . ; For every character of the given string ; If current character is vowel and the index is not prime ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean isVowelPrime ( String str , int n ) { boolean [ ] prime = new boolean [ n ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < n ; i += p ) prime [ i ] = false ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) && ! prime [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; if ( isVowelPrime ( str , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the given string of words can be formed from words present in the dictionary | Java program to check if a sentence can be formed from a given set of words . ; Function to check if the word is in the dictionary or not ; map to store all words in dictionary with their count ; adding all words in map ; search in map for all words in the sentence ; all words of sentence are present ; Driver Code ; Calling function to check if words are present in the dictionary or not"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean match_words ( String dictionary [ ] , String sentence [ ] , int n , int m ) { Map < String , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( dictionary [ i ] ) ) { mp . put ( dictionary [ i ] , mp . get ( dictionary [ i ] ) + 1 ) ; } else { mp . put ( dictionary [ i ] , 1 ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( mp . containsKey ( sentence [ i ] ) ) mp . put ( sentence [ i ] , mp . get ( sentence [ i ] ) - 1 ) ; else return false ; } return true ; } public static void main ( String [ ] args ) { String dictionary [ ] = { " find " , " a " , " geeks " , " all " , " for " , " on " , " geeks " , " answers " , " inter " } ; int n = dictionary . length ; String sentence [ ] = { " find " , " all " , " answers " , " on " , " geeks " , " for " , " geeks " } ; int m = sentence . length ; if ( match_words ( dictionary , sentence , n , m ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if the given two matrices are mirror images of one another | Java implementation of the above approach ; Function to check whether the two matrices are mirror of each other ; Initialising row and column of second matrix ; Iterating over the matrices ; Check row of first matrix with reversed row of second matrix ; If the element is not equal ; Increment column ; Reset column to 0 for new row ; Increment row ; Driver code ; Given 2 matrices ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void mirrorMatrix ( int mat1 [ ] [ ] , int mat2 [ ] [ ] , int N ) { int row = 0 ; int col = 0 ; boolean isMirrorImage = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat2 [ row ] [ col ] != mat1 [ i ] [ j ] ) { isMirrorImage = false ; } col ++ ; } col = 0 ; row ++ ; } if ( isMirrorImage ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int mat1 [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 0 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int mat2 [ ] [ ] = { { 4 , 3 , 2 , 1 } , { 8 , 7 , 6 , 0 } , { 12 , 11 , 10 , 9 } , { 16 , 15 , 14 , 13 } } ; mirrorMatrix ( mat1 , mat2 , N ) ; } }


"Check if the given two numbers are friendly pair or not | Java code to check if the given two number are friendly pair or not . ; Returns sum of all factors of n . ; Traversing through all prime factors . ; THE BELOW STATEMENT MAKES IT BETTER THAN ABOVE METHOD AS WE REDUCE VALUE OF n . ; This condition is to handle the case when n is a prime number greater than 2. ; Function to return gcd of a and b ; Function to check if the given two number are friendly pair or not . ; Finding the sum of factors of n and m ; finding gcd of n and sum of its factors . ; finding gcd of m and sum of its factors . ; checking is numerator and denominator of abundancy index of both number are equal or not . ; driver code"
How can the above be solved in Java?

 class GFG { 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 int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static boolean checkFriendly ( int n , int m ) { int sumFactors_n = sumofFactors ( n ) ; int sumFactors_m = sumofFactors ( m ) ; int gcd_n = gcd ( n , sumFactors_n ) ; int gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) System . out . print ( "Yes
"); else System . out . print ( "No
"); } }


"Check if the given vectors are at equilibrium or not | Java program to check the equilibrium of three vectors ; Function to check the equilibrium of three vectors ; summing the x coordinates ; summing the y coordinates ; summing the z coordinates ; Checking the condition for equilibrium ; Driver code ; Checking for equilibrium"
How can the above be solved in Java?

public class GFG { static boolean checkEquilibrium ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int x3 , int y3 , int z3 ) { int resx = x1 + x2 + x3 ; int resy = y1 + y2 + y3 ; int resz = z1 + z2 + z3 ; if ( resx == 0 & resy == 0 & resz == 0 ) return true ; else return false ; } public static void main ( String args [ ] ) { int x1 = - 2 , y1 = - 7 , z1 = - 9 , x2 = 5 , y2 = - 14 , z2 = 14 , x3 = - 3 , y3 = 21 , z3 = - 5 ; if ( checkEquilibrium ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) ) System . out . println ( " The ▁ vectors ▁ are ▁ at ▁ equilibrium . " ) ; else System . out . println ( " The ▁ vectors ▁ are ▁ not ▁ at ▁ equilibrium . " ) ; } }


"Check if the large number formed is divisible by 41 or not | Java program to check a large number divisible by 41 or not ; Check if a number is divisible by 41 or not ; array to store all the digits ; base values ; calculate remaining digits ; calculate answer ; check for divisibility ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean DivisibleBy41 ( int first , int second , int c , int n ) { int digit [ ] = new int [ n ] ; digit [ 0 ] = first ; digit [ 1 ] = second ; for ( int i = 2 ; i < n ; i ++ ) digit [ i ] = ( digit [ i - 1 ] * c + digit [ i - 2 ] ) % 10 ; int ans = digit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) ans = ( ans * 10 + digit [ i ] ) % 41 ; if ( ans % 41 == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int first = 1 , second = 2 , c = 1 , n = 3 ; if ( DivisibleBy41 ( first , second , c , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if the n | Java Program to check if the nth is odd or even in a sequence where each term is sum of previous two term ; Return if the nth term is even or odd . ; If a is even ; If b is even ; If b is odd ; If a is odd ; If b is odd ; If b is eve ; Driven Program"
How can the above be solved in Java?

class GFG { static boolean findNature ( int a , int b , int n ) { if ( n == 0 ) return ( a & 1 ) == 1 ? true : false ; if ( n == 1 ) return ( b & 1 ) == 1 ? true : false ; if ( ( a & 1 ) == 0 ) { if ( ( b & 1 ) == 0 ) return false ; else return ( n % 3 != 0 ) ; } else { if ( ( b & 1 ) == 0 ) return ( ( n - 1 ) % 3 != 0 ) ; else return ( ( n + 1 ) % 3 != 0 ) ; } } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; int n = 3 ; if ( findNature ( a , b , n ) ) System . out . println ( " Odd " ) ; else System . out . println ( " Even " ) ; } }


"Check if the n | Return if the nth term is even or odd . ; Return true if odd ; Driver Code"
How can the above be solved in Java?

class GFG { public static int findNature ( int a , int b , int n ) { int [ ] seq = new int [ 100 ] ; seq [ 0 ] = a ; seq [ 1 ] = b ; for ( int i = 2 ; i <= n ; i ++ ) seq [ i ] = seq [ i - 1 ] + seq [ i - 2 ] ; if ( ( seq [ n ] & 1 ) != 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; int n = 3 ; if ( findNature ( a , b , n ) == 1 ) System . out . println ( " Odd ▁ " ) ; else System . out . println ( " Even ▁ " ) ; } }


"Check if the number formed by the last digits of N numbers is divisible by 10 or not | Java implementation of the approach ; Function that returns true if the number formed by the last digits of all the elements is divisible by 10 ; Last digit of the last element ; Number formed will be divisible by 10 ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isDivisible ( int arr [ ] , int n ) { int lastDigit = arr [ n - 1 ] % 10 ; if ( lastDigit == 0 ) return true ; return false ; } static public void main ( String [ ] arg ) { int arr [ ] = { 12 , 65 , 46 , 37 , 99 } ; int n = arr . length ; if ( isDivisible ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the number is balanced | Java program to check if a number is Balanced or not ; Function to check whether N is Balanced Number or not ; Calculating the Leftsum and rightSum simultaneously ; Typecasting each character to integer and adding the digit to respective sums ; Driver Code ; Function call"
How can the above be solved in Java?

import java . io . * ; class GFG { private static void BalancedNumber ( String s ) { int Leftsum = 0 ; int Rightsum = 0 ; for ( int i = 0 ; i < s . length ( ) / 2 ; i ++ ) { Leftsum += ( int ) ( s . charAt ( i ) - '0' ) ; Rightsum += ( int ) ( s . charAt ( s . length ( ) - 1 - i ) - '0' ) ; } if ( Leftsum == Rightsum ) System . out . println ( " Balanced " ) ; else System . out . println ( " Not ▁ Balanced " ) ; } public static void main ( String [ ] args ) { String s = "12321" ; BalancedNumber ( s ) ; } }


"Check if the number is divisible 43 or not | Java program to check whether a number is divisible by 43 or not ; Function to check if the number is divisible by 43 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; adding thirteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 43 or not ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) > 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n + ( d * 13 ) ) ; } return ( n % 43 == 0 ) ; } public static void main ( String [ ] args ) { int N = 2795 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if the number is even or odd whose digits and base ( radix ) is given | Java implementation of the approach ; Function that returns true if the number represented by arr [ ] is even in base r ; If the base is even , then the last digit is checked ; If base is odd , then the number of odd digits are checked ; To store the count of odd digits ; Number is odd ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isEven ( int arr [ ] , int n , int r ) { if ( r % 2 == 0 ) { if ( arr [ n - 1 ] % 2 == 0 ) return true ; } else { int oddCount = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount % 2 == 0 ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 } ; int n = arr . length ; int r = 2 ; if ( isEven ( arr , n , r ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }


"Check if the number is valid when flipped upside down | Java implementation of the approach ; Function that returns true if str is Topsy Turvy ; For every character of the string ; If the current digit cannot form a valid digit when turned upside - down ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean topsyTurvy ( char [ ] str ) { for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == '2' str [ i ] == '4' str [ i ] == '5' str [ i ] == '6' str [ i ] == '7' str [ i ] == '9' ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = "1234" ; if ( topsyTurvy ( str . toCharArray ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the product of every contiguous subsequence is different or not in a number | Java implementation of the approach ; Function that returns true if the product of every digit of a contiguous subsequence is distinct ; To store the given number as a string ; Append all the digits starting from the end ; Reverse the string to get the original number ; Store size of the string ; Set to store product of each contiguous subsequence ; Find product of every contiguous subsequence ; If current product already exists in the set ; Utility reverse funciton ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean productsDistinct ( int N ) { String s = " " ; while ( N > 0 ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } s = reverse ( s ) ; int sz = s . length ( ) ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s . charAt ( j ) - '0' ) ; if ( se . contains ( product ) ) return false ; else se . add ( product ) ; } } return true ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int N = 2345 ; if ( productsDistinct ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the robot is within the bounds of the grid after given moves | Java implementation of the approach ; Function that returns true if the robot is safe ; If current move is " L " then increase the counter of coll ; If value of coll is equal to column then break ; If current move is " R " then increase the counter of colr ; If value of colr is equal to column then break ; If current move is " U " then increase the counter of rowu ; If value of rowu is equal to row then break ; If current move is " D " then increase the counter of rowd ; If value of rowd is equal to row then break ; If robot is within the bounds of the grid ; Unsafe ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isSafe ( int N , int M , char [ ] str ) { int coll = 0 , colr = 0 , rowu = 0 , rowd = 0 ; for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' L ' ) { coll ++ ; if ( colr > 0 ) { colr -- ; } if ( coll == M ) { break ; } } else if ( str [ i ] == ' R ' ) { colr ++ ; if ( coll > 0 ) { coll -- ; } if ( colr == M ) { break ; } } else if ( str [ i ] == ' U ' ) { rowu ++ ; if ( rowd > 0 ) { rowd -- ; } if ( rowu == N ) { break ; } } else if ( str [ i ] == ' D ' ) { rowd ++ ; if ( rowu > 0 ) { rowu -- ; } if ( rowd == N ) { break ; } } } if ( Math . abs ( rowd ) < N && Math . abs ( rowu ) < N && Math . abs ( coll ) < M && Math . abs ( colr ) < M ) { return true ; } return false ; } public static void main ( String [ ] args ) { int N = 1 , M = 1 ; String str = " R " ; if ( isSafe ( N , M , str . toCharArray ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the square of a number is divisible by K or not | Java implementation to check if the square of X is divisible by K ; Function to return if square of X is divisible by K ; Finding gcd of x and k ; Dividing k by their gcd ; Check for divisibility of X by reduced K ; Driver Code"
How can the above be solved in Java?

 class GFG { static void checkDivisible ( int x , int k ) { int g = __gcd ( x , k ) ; k /= g ; if ( x % k == 0 ) { System . out . print ( "YES
"); } else { System . out . print ( "NO
"); } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int x = 6 , k = 9 ; checkDivisible ( x , k ) ; } }


"Check if the string has a reversible equal substring at the ends | Java program for the above approach ; Function to print longest substring that appears at beginning of string and also at end in reverse order ; Stores the resultant string ; If the characters are same ; Otherwise , break ; If the string can 't be formed ; Otherwise print resultant string ; Driver Code"
How can the above be solved in Java?

public class GFG { static void commonSubstring ( String s ) { int n = s . length ( ) ; int i = 0 ; int j = n - 1 ; String ans = " " ; while ( j >= 0 ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { ans += s . charAt ( i ) ; i ++ ; j -- ; } else { break ; } } if ( ans . length ( ) == 0 ) System . out . println ( " False " ) ; else { System . out . println ( " True ▁ " ) ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " abca " ; commonSubstring ( S ) ; } }


"Check if the string satisfies the given condition | Java implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if c is a vowel ; Function that returns true if the count of vowels in word is prime ; If count of vowels is prime ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean prime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean isVowel ( char c ) { c = Character . toLowerCase ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static boolean isValidString ( String word ) { int cnt = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { if ( isVowel ( word . charAt ( i ) ) ) { cnt ++ ; } } if ( prime ( cnt ) ) { return true ; } else { return false ; } } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( isValidString ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if the sum of K least and most frequent array elements are equal or not | Java program for the above approach ; Function to compare the sum of K most and least occurrences ; Stores frequency of array element ; Stores the frequencies as indexes and putelements with the frequency in a vector ; Find the frequency ; Insert in the vector ; Stores the count of elements ; Traverse the frequency array ; Find the kleastfreqsum ; If the count is K , break ; Reinitialize the count to zero ; Traverse the frequency ; Find the kmostfreqsum ; If the count is K , break ; Comparing the sum ; Otherwise , return No ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String checkSum ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } Vector < Integer > [ ] freq = new Vector [ n + 1 ] ; for ( int i = 0 ; i < freq . length ; i ++ ) freq [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int f = m . get ( arr [ i ] ) ; if ( f != - 1 ) { freq [ f ] . add ( arr [ i ] ) ; m . put ( arr [ i ] , - 1 ) ; } } int count = 0 ; int kleastfreqsum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int x : freq [ i ] ) { kleastfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } count = 0 ; int kmostfreqsum = 0 ; for ( int i = n ; i >= 0 ; i -- ) { for ( int x : freq [ i ] ) { kmostfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } if ( kleastfreqsum == kmostfreqsum ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 2 , 3 , 3 , 4 } ; int N = arr . length ; int K = 2 ; System . out . print ( checkSum ( arr , N , K ) ) ; } }


"Check if the sum of a subarray within a given range is a perfect square or not | Java implementation of the above approach ; Function to calculate the square root of the sum of a subarray in a given range ; Calculate the sum of array elements within a given range ; Finding the square root ; If a perfect square is found ; Reduce the search space if the value is greater than sum ; Reduce the search space if the value if smaller than sum ; Driver Code ; Given Array ; Given range ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int checkForPerfectSquare ( int [ ] arr , int i , int j ) { int mid , sum = 0 ; for ( int m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } int low = 0 , high = sum / 2 ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 19 , 33 , 48 , 90 , 100 } ; int L = 1 , R = 3 ; System . out . print ( checkForPerfectSquare ( arr , L , R ) ) ; } }


"Check if the sum of digits of N is palindrome | Java implementation of the approach ; Function to return the sum of digits of n ; Function that returns true if n is palindrome ; Find the appropriate divisor to extract the leading digit ; If first and last digit not same return false ; Removing the leading and trailing digit from number ; Reducing divisor by a factor of 2 as 2 digits are dropped ; Function that returns true if the digit sum of n is palindrome ; Sum of the digits of n ; If the digit sum is palindrome ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } return sum ; } static boolean isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static boolean isDigitSumPalindrome ( int n ) { int sum = digitSum ( n ) ; if ( isPalindrome ( sum ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 56 ; if ( isDigitSumPalindrome ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the sum of digits of number is divisible by all of its digits | Java implementation of the approach ; Function that returns true if all the digits of n divide the sum of the digits of n ; Store a copy of the original number ; Find the sum of the digits of n ; Restore the original value ; Check if all the digits divide the calculated sum ; If current digit doesn 't  divide the sum ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isDivisible ( long n ) { long temp = n ; int sum = 0 ; while ( n != 0 ) { int digit = ( int ) n % 10 ; sum += digit ; n /= 10 ; } n = temp ; while ( n != 0 ) { int digit = ( int ) n % 10 ; if ( sum % digit != 0 ) return false ; n /= 10 ; } return true ; } public static void main ( String [ ] args ) { long n = 123 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if the sum of distinct digits of two integers are equal | Java program to check if the sum of distinct digits of two integers are equal ; Function to return the sum of distinct digits of a number ; Take last digit ; If digit has not been used before ; Set digit as used ; Remove last digit ; Function to check whether the sum of distinct digits of two numbers are equal ; Driver code"
How can the above be solved in Java?

public class HelloWorld { static int distinctDigitSum ( int n ) { boolean used [ ] = new boolean [ 10 ] ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = n / 10 ; } return sum ; } static String checkSum ( int m , int n ) { int sumM = distinctDigitSum ( m ) ; int sumN = distinctDigitSum ( n ) ; if ( sumM == sumN ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { int m = 2452 , n = 9222 ; System . out . println ( checkSum ( m , n ) ) ; } }


"Check if the sum of perfect squares in an array is divisible by x | Java implementation of the approach ; Function that returns true if the the sum of all the perfect squares of the given array is divisible by x ; If arr [ i ] is a perfect square ; Driver Code"
How can the above be solved in Java?

public class GFG { static boolean check ( int arr [ ] , int x , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double y = Math . sqrt ( arr [ i ] ) ; if ( Math . floor ( y ) == Math . ceil ( y ) ) { sum += arr [ i ] ; } } if ( sum % x == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 9 , 10 } ; int n = arr . length ; int x = 13 ; if ( check ( arr , x , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if the tower of sight issue occurs or not | Java program to check if tower of sight issue occurs or not ; Function to check if point p lies in between the line joining p1 and p2 ; If parallel to X - axis ; Point p lies between p1 and p2 ; If parallel to Y - axis ; Point p lies between p1 and p2 ; If point p satisfies the equation of line joining p1 and p2 ; Function to check if tower of sight issue occurred ; B lies between AC ; D lies between AC ; A lies between BD ; C lies between BD ; Driver code ; Point A ; Point B ; Point C ; Point D"
How can the above be solved in Java?

class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int checkIntersection ( pair p1 , pair p2 , pair p ) { int val ; if ( p1 . second == p2 . second && p1 . second == p . second ) { if ( p . first <= Math . max ( p1 . first , p2 . first ) && ( p . first >= Math . min ( p1 . first , p2 . first ) ) ) return 1 ; } if ( p1 . first == p2 . first && p1 . first == p . first ) { if ( p . second <= Math . max ( p1 . second , p2 . second ) && ( p . second >= Math . min ( p1 . second , p2 . second ) ) ) return 1 ; } else { val = ( p . second - p1 . second ) * ( p2 . first - p1 . first ) - ( p . first - p1 . first ) * ( p2 . second - p1 . second ) ; if ( val == 0 ) if ( ( p . first <= Math . max ( p1 . first , p2 . first ) && ( p . first >= Math . min ( p1 . first , p2 . first ) ) ) && ( p . second <= Math . max ( p1 . second , p2 . second ) && ( p . second >= Math . min ( p1 . second , p2 . second ) ) ) ) return 1 ; } return 0 ; } static void towerOfSight ( pair a , pair b , pair c , pair d ) { int flag = 0 ; if ( checkIntersection ( a , c , b ) == 1 ) flag = 1 ; else if ( checkIntersection ( a , c , d ) == 1 ) flag = 1 ; else if ( checkIntersection ( b , d , a ) == 1 ) flag = 1 ; else if ( checkIntersection ( b , d , c ) == 1 ) flag = 1 ; System . out . print ( flag == 1 ? "Yes " : " No "); } public static void main ( String [ ] args ) { pair a = new pair ( 0 , 0 ) ; pair b = new pair ( 0 , - 2 ) ; pair c = new pair ( 2 , 0 ) ; pair d = new pair ( 0 , 2 ) ; towerOfSight ( a , b , c , d ) ; } }


"Check if there exist two elements in an array whose sum is equal to the sum of rest of the array | Java program to find whether two elements exist whose sum is equal to sum of rest of the elements . ; Function to check whether two elements exist whose sum is equal to sum of rest of the elements . ; Find sum of whole array ; If sum of array is not even than we can not divide it into two part ; For each element arr [ i ] , see if there is another element with vaalue sum - arr [ i ] ; If element exist than return the pair ; Driver program ."
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean checkPair ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = sum / 2 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . contains ( val ) && val == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { System . out . printf ( "Pair elements are %d and %d
", arr[i], val); return true ; } s . add ( arr [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = arr . length ; if ( checkPair ( arr , n ) == false ) { System . out . printf ( " No ▁ pair ▁ found " ) ; } } }


"Check if there exists a number with X factors out of which exactly K are prime | Java program to check if there exists a number with X factors out of which exactly K are prime ; Function to check if such number exists ; To store the sum of powers of prime factors of X which determines the maximum count of numbers whose product can form X ; Determining the prime factors of X ; To check if the number is prime ; If X is 1 , then we cannot form a number with 1 factor and K prime factor ( as K is atleast 1 ) ; If X itself is prime then it can be represented as a power of only 1 prime factor which is X itself so we return true ; If sum of the powers of prime factors of X is greater than or equal to K , which means X can be represented as a product of K numbers , we return true ; In any other case , we return false as we cannot form a number with X factors and K prime factors ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean check ( int X , int K ) { int prime , temp , sqr , i ; prime = 0 ; temp = X ; sqr = ( int ) Math . sqrt ( X ) ; for ( i = 2 ; i <= sqr ; i ++ ) { while ( temp % i == 0 ) { temp = temp / i ; prime ++ ; } } if ( temp > 2 ) prime ++ ; if ( X == 1 ) return false ; if ( prime == 1 && K == 1 ) return true ; else if ( prime >= K ) return true ; else return false ; } public static void main ( String [ ] args ) { int X , K ; X = 4 ; K = 2 ; if ( check ( X , K ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if there exists a permutation of given string which doesn 't contain any monotonous substring | Java implementation such that there are no monotonous string in given string ; Function to check a string doesn 't contains a monotonous substring ; Loop to iterate over the string and check that it doesn 't contains  the monotonous substring ; Function to check that there exist a arrangement of string such that it doesn 't contains monotonous substring ; Loop to group the characters of the string into two buckets ; Sorting the two buckets ; Condition to check if the concatenation point doesn 't  contains the monotonous string ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean check ( String s ) { boolean ok = true ; for ( int i = 0 ; i + 1 < s . length ( ) ; ++ i ) ok &= ( Math . abs ( s . charAt ( i ) - s . charAt ( i + 1 ) ) != 1 ) ; return ok ; } static String monotonousString ( String s ) { String odd = " " , even = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) % 2 == 0 ) odd += s . charAt ( i ) ; else even += s . charAt ( i ) ; } char oddArray [ ] = odd . toCharArray ( ) ; Arrays . sort ( oddArray ) ; odd = new String ( oddArray ) ; char evenArray [ ] = even . toCharArray ( ) ; Arrays . sort ( evenArray ) ; even = new String ( evenArray ) ; if ( check ( odd + even ) ) return " Yes " ; else if ( check ( even + odd ) ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { String str = " abcd " ; String ans ; ans = monotonousString ( str ) ; System . out . println ( ans ) ; } }


"Check if there exists a prime number which gives Y after being repeatedly subtracted from X | Java implementation of the approach ; Function that returns true if any prime number satisfies the given conditions ; No such prime exists ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPossible ( int x , int y ) { if ( ( x - y ) == 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int x = 100 , y = 98 ; if ( isPossible ( x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if there exists any sub | Java program to check if there exists at least 1 sub - sequence in a string which is not palindrome ; Function to check if there exists at least 1 sub - sequence in a string which is not palindrome ; use set to count number of distinct characters ; insert each character in set ; If there is more than 1 unique characters , return true ; Else , return false ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isAnyNotPalindrome ( String s ) { Set < Character > unique = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) unique . add ( s . charAt ( i ) ) ; if ( unique . size ( ) > 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { String s = " aaaaab " ; if ( isAnyNotPalindrome ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if there is a cycle with odd weight sum in an undirected graph | Java program to check if there is a cycle of total odd weight ; This function returns true if the current subpart of the forest is two colorable , else false . ; Assign first color to source ; Create a queue ( FIFO ) of vertex numbers and enqueue source vertex for BFS traversal ; Run while there are vertices in queue ( Similar to BFS ) ; Find all non - colored adjacent vertices ; An edge from u to v exists and destination v is not colored ; Assign alternate color to this adjacent v of u ; An edge from u to v exists and destination v is colored with same color as u ; This function returns true if graph G [ V ] [ V ] is two colorable , else false ; Create a color array to store colors assigned to all veritces . Vertex number is used as index in this array . The value ' - 1' of colorArr [ i ] is used to indicate that no color is assigned to vertex ' i ' . The value 1 is used to indicate first color is assigned and value 0 indicates second color is assigned . ; As we are dealing with graph , the input might come as a forest , thus start coloring from a node and if true is returned we 'll know that  we successfully colored the subpart of our  forest and we start coloring again from a new  uncolored node. This way we cover the entire forest. ; Returns false if an odd cycle is present else true int info [ ] [ ] is the information about our graph int n is the number of nodes int m is the number of informations given to us ; Declaring adjacency list of a graph Here at max , we can encounter all the edges with even weight thus there will be 1 pseudo node for each edge @ SuppressWarnings ( " unchecked " ) ; For odd weight edges , we directly add it in our graph ; For even weight edges , we break it ; Entering a pseudo node between u -- - v ; Keeping a record of number of pseudo nodes inserted ; Making a new pseudo node for next time ; We pass number graph G [ ] [ ] and total number of node = actual number of nodes + number of pseudo nodes added . ;..."
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean twoColorUtil ( Vector < Integer > [ ] G , int src , int N , int [ ] colorArr ) { colorArr [ src ] = 1 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( src ) ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . poll ( ) ; for ( int v = 0 ; v < G [ u ] . size ( ) ; ++ v ) { if ( colorArr [ G [ u ] . elementAt ( v ) ] == - 1 ) { colorArr [ G [ u ] . elementAt ( v ) ] = 1 - colorArr [ u ] ; q . add ( G [ u ] . elementAt ( v ) ) ; } else if ( colorArr [ G [ u ] . elementAt ( v ) ] == colorArr [ u ] ) return false ; } } return true ; } static boolean twoColor ( Vector < Integer > [ ] G , int N ) { int [ ] colorArr = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; ++ i ) colorArr [ i ] = - 1 ; for ( int i = 1 ; i <= N ; i ++ ) if ( colorArr [ i ] == - 1 ) if ( twoColorUtil ( G , i , N , colorArr ) == false ) return false ; return true ; } static boolean isOddSum ( int [ ] [ ] info , int n , int m ) { Vector < Integer > [ ] G = new Vector [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) G [ i ] = new Vector < > ( ) ; int pseudo = n + 1 ; int pseudo_count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( info [ i ] [ 2 ] % 2 == 1 ) { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( v ) ; G [ v ] . add ( u ) ; } else { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( pseudo ) ; G [ pseudo ] . add ( u ) ; G [ v ] . add ( pseudo ) ; G [ pseudo ] . add ( v ) ; pseudo_count ++ ; pseudo ++ ; } } return twoColor ( G , n + pseudo_count ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 3 ; int [ ] [ ] info = { { 1 , 2 , 12 } , { 2 , 3 , 1 } , { 4 , 3 , 1 } , { 4 , 1 , 20 } } ; if ( isOddSum ( info , n , m ) == true ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } }


"Check if there is a root to leaf path with given sequence | Java program to see if there is a root to leaf path with given sequence . ; A binary tree node has data , pointer to left child and a pointer to right child ; Util function ; If root is NULL or reached end of the array ; If current node is leaf ; If current node is equal to arr [ index ] this means that till this level path has been matched and remaining path can be either in left subtree or right subtree . ; Function to check given sequence of root to leaf path exist in tree or not . index : current element in sequence of root to leaf path ; Driver code ; arr [ ] : sequence of root to leaf path"
How can the above be solved in Java?

import java . io . * ; class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } class GFG { static boolean existPathUtil ( Node root , int arr [ ] , int n , int index ) { if ( root == null index == n ) return false ; if ( root . left == null && root . right == null ) { if ( ( root . data == arr [ index ] ) && ( index == n - 1 ) ) return true ; return false ; } return ( ( index < n ) && ( root . data == arr [ index ] ) && ( existPathUtil ( root . left , arr , n , index + 1 ) || existPathUtil ( root . right , arr , n , index + 1 ) ) ) ; } static boolean existPath ( Node root , int arr [ ] , int n , int index ) { if ( root == null ) return ( n == 0 ) ; return existPathUtil ( root , arr , n , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 , 6 , 7 } ; int n = arr . length ; Node root = new Node ( 5 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 8 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 4 ) ; root . left . left . left = new Node ( 1 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; if ( existPath ( root , arr , n , 0 ) ) System . out . println ( " Path ▁ Exists " ) ; else System . out . println ( " Path ▁ does ▁ not ▁ Exist " ) ; } }


"Check if there is any pair in a given range with GCD is divisible by k | Java program to count the numbers divisible by k in a given range ; Returns count of numbers in [ l r ] that are divisible by k . ; Add 1 explicitly as l is divisible by k ; l is not divisible by k ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean Check_is_possible ( int l , int r , int k ) { int div_count = ( r / k ) - ( l / k ) ; if ( l % k == 0 ) { div_count ++ ; } return ( div_count > 1 ) ; } public static void main ( String [ ] args ) { int l = 30 , r = 70 , k = 10 ; if ( Check_is_possible ( l , r , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if there is any pair in a given range with GCD is divisible by k | function to count such possible numbers ; if i is divisible by k ; if count of such numbers is greater than one ; Driver code"
How can the above be solved in Java?

class GFG { public boolean Check_is_possible ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % k == 0 ) { count ++ ; } } return ( count > 1 ) ; } public static void main ( String [ ] args ) { GFG g = new GFG ( ) ; int l = 4 , r = 12 ; int k = 5 ; if ( g . Check_is_possible ( l , r , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if three straight lines are concurrent or not | Java Program to check if three straight line are concurrent or no ; Return true if three line are concurrent , else false . ; Driven Program"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean checkConcurrent ( int a1 , int b1 , int c1 , int a2 , int b2 , int c2 , int a3 , int b3 , int c3 ) { return ( a3 * ( b1 * c2 - b2 * c1 ) + b3 * ( c1 * a2 - c2 * a1 ) + c3 * ( a1 * b2 - a2 * b1 ) == 0 ) ; } public static void main ( String [ ] args ) { int a1 = 2 , b1 = - 3 , c1 = 5 ; int a2 = 3 , b2 = 4 , c2 = - 7 ; int a3 = 9 , b3 = - 5 , c3 = 8 ; if ( checkConcurrent ( a1 , b1 , c1 , a2 , b2 , c2 , a3 , b3 , c3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if two BSTs contain same set of elements | JAVA program to check if two BSTs contains same set of elements ; BST Node ; Utility function to create new Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two hash sets and store elements both BSTs in them . ; Return true if both hash sets contain same elements . ; Driver program to check above functions ; First BST ; Second BST ; check if two BSTs have same set of elements"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void insertToHash ( Node root , HashSet < Integer > s ) { if ( root == null ) return ; insertToHash ( root . left , s ) ; s . add ( root . data ) ; insertToHash ( root . right , s ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; HashSet < Integer > s1 = new HashSet < Integer > ( ) ; HashSet < Integer > s2 = new HashSet < Integer > ( ) ; insertToHash ( root1 , s1 ) ; insertToHash ( root2 , s2 ) ; return ( s1 . equals ( s2 ) ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }


"Check if two BSTs contain same set of elements | Java program to check if two BSTs contain same set of elements ; BST Node ; Utility function to create new Node ; function to insert elements of the tree to map m ; function to check if the two BSTs contain same set of elements ; Base cases ; Create two vectors and store inorder traversals of both BSTs in them . ; Return true if both vectors are identical ; Driver Code ; First BST ; Second BST ; check if two BSTs have same set of elements"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void storeInorder ( Node root , Vector < Integer > v ) { if ( root == null ) return ; storeInorder ( root . left , v ) ; v . add ( root . data ) ; storeInorder ( root . right , v ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; storeInorder ( root1 , v1 ) ; storeInorder ( root2 , v2 ) ; return ( v1 == v2 ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }


"Check if two Integer are anagrams of each other | Java implementation of the approach ; Function to update the frequency array such that freq [ i ] stores the frequency of digit i in n ; While there are digits left to process ; Update the frequency of the current digit ; Remove the last digit ; Function that returns true if a and b are anagarams of each other ; To store the frequencies of the digits in a and b ; Update the frequency of the digits in a ; Update the frequency of the digits in b ; Match the frequencies of the common digits ; If frequency differs for any digit then the numbers are not anagrams of each other ; Driver code"
How can the above be solved in Java?

class GFG { static final int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 204 , b = 240 ; if ( areAnagrams ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if two Linked Lists are permutations of each other | Java program to check if linked lists are permutations of each other ; A linked list node ; Function to check if two linked lists * are permutations of each other * first : reference to head of first linked list * second : reference to head of second linked list ; Variables to keep track of sum and multiplication ; Traversing through linked list and calculating sum and multiply ; Traversing through linked list and calculating sum and multiply ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; First constructed linked list is : 12 . 35 . 1 . 10 . 34 . 1 ; Second constructed linked list is : 35 . 1 . 12 . 1 . 10 . 34"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static boolean isPermutation ( Node first , Node second ) { int sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; Node temp1 = first ; while ( temp1 != null ) { sum1 += temp1 . data ; mul1 *= temp1 . data ; temp1 = temp1 . next ; } Node temp2 = second ; while ( temp2 != null ) { sum2 += temp2 . data ; mul2 *= temp2 . data ; temp2 = temp2 . next ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } public static void main ( String [ ] args ) { Node first = null ; first = push ( first , 1 ) ; first = push ( first , 34 ) ; first = push ( first , 10 ) ; first = push ( first , 1 ) ; first = push ( first , 35 ) ; first = push ( first , 12 ) ; Node second = null ; second = push ( second , 35 ) ; second = push ( second , 1 ) ; second = push ( second , 12 ) ; second = push ( second , 1 ) ; second = push ( second , 10 ) ; second = push ( second , 34 ) ; if ( isPermutation ( first , second ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if two arrays are equal or not | Java program to find given two array are equal or not ; Returns true if arr1 [ 0. . n - 1 ] and arr2 [ 0. . m - 1 ] contain same elements . ; If lengths of array are not equal means array are not equal ; Sort both arrays ; Linearly compare elements ; If all elements were same . ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static boolean areEqual ( int arr1 [ ] , int arr2 [ ] ) { int n = arr1 . length ; int m = arr2 . length ; if ( n != m ) return false ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 5 , 2 , 5 , 2 } ; int arr2 [ ] = { 2 , 3 , 5 , 5 , 2 } ; if ( areEqual ( arr1 , arr2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }