"Check if a number has bits in alternate pattern | Set | Java implementation to check if a number has bits in alternate pattern ; function to check if all the bits are set or not in the binary representation of ' n ' ; if true , then all bits are set ; else all bits are not set ; function to check if a number has bits in alternate pattern ; to check if all bits are set in ' num ' ; Driver Code"
How can the above be solved in Java?

class AlternateSetBits { static boolean allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static boolean bitsAreInAltOrder ( int n ) { int num = n ^ ( n >>> 1 ) ; return allBitsAreSet ( num ) ; } public static void main ( String args [ ] ) { int n = 10 ; if ( bitsAreInAltOrder ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number has digits in the given Order | Java program to check if the digits are in the given order ; Check if the digits follow the correct order ; to store the previous digit ; pointer to tell what type of sequence are we dealing with ; check if we have same digit as the previous digit ; checking the peak point of the number ; check if we have same digit as the previous digit ; check if the digit is greater than the previous one If true , then break from the loop as we are in descending order part ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isCorrectOrder ( int n ) { boolean flag = true ; int prev = - 1 ; int type = - 1 ; while ( n != 0 ) { if ( type == - 1 ) { if ( prev == - 1 ) { prev = n % 10 ; n = n / 10 ; continue ; } if ( prev == n % 10 ) { flag = false ; break ; } if ( prev > n % 10 ) { type = 1 ; prev = n % 10 ; n = n / 10 ; continue ; } prev = n % 10 ; n = n / 10 ; } else { if ( prev == n % 10 ) { flag = false ; break ; } if ( prev < n % 10 ) { flag = false ; break ; } prev = n % 10 ; n = n / 10 ; } } return flag ; } public static void main ( String [ ] args ) { int n = 123454321 ; if ( isCorrectOrder ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a number has same number of set and unset bits | Java program to check if a number has same number of set and unset bits ; Function to check if a number has same setbits and unset bits ; iterate for all bits of a number ; if set ; if unset ; right shift number by 1 ; is number of set bits are equal to unset bits ; Driver Code ; function to check"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean checkSame ( int n ) { int set = 0 ; int unset = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 1 ) set ++ ; else unset ++ ; n = n >> 1 ; } if ( set == unset ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 12 ; if ( checkSame ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a number has two adjacent set bits | Java program to check if there are two adjacent set bits . ; Driver code"
How can the above be solved in Java?

class GFG { static boolean adjacentSet ( int n ) { int x = ( n & ( n >> 1 ) ) ; if ( x > 0 ) return true ; else return false ; } public static void main ( String args [ ] ) { int n = 3 ; if ( adjacentSet ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Bleak | A simple Java program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String args [ ] ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Bleak | An efficient Java program to check Bleak Number ; Function to get no of set bits in binary representation of passed binary no . ; A function to return ceiling of log x in base 2. For example , it returns 3 for 8 and 4 for 9. ; Returns true if n is Bleak ; Check for all numbers ' x ' smaller than n . If x + countSetBits ( x ) becomes n , then n can 't be Bleak ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static int ceilLog2 ( int x ) { int count = 0 ; x -- ; while ( x > 0 ) { x = x >> 1 ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = n - ceilLog2 ( n ) ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String [ ] args ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Bleak | Java program to demonstrate Integer . bitCount ( )"
How can the above be solved in Java?

 import java . util . * ; class GFG { public static void main ( String [ ] args ) { System . out . print ( Integer . bitCount ( 4 ) + "
"); System . out . print ( Integer . bitCount ( 15 ) ) ; } }


"Check if a number is Euler Pseudoprime | Java implementation of the approach ; Function that returns true if n is composite ; Check if there is any divisor of n . we only need check divisor till sqrt ( n ) because if there is divisor which is greater than sqrt ( n ) then there must be a divisor which is less than sqrt ( n ) ; Iterative Function to calculate ( x ^ y ) % p in O ( log y ) ; Initialize result ; Update x if it is greater than or equal to p ; If y is odd , multiply x with result ; y must be even now y = y >> 1 ; y = y / 2 ; Function that returns true if N is Euler Pseudoprime to the base A ; Invalid base ; N is not a composite odd number ; If A and N are not coprime ; All the conditions for Euler Pseudoprime are satisfied ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isComposite ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return true ; } return false ; } static int Power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static boolean isEulerPseudoprime ( int N , int A ) { if ( A <= 0 ) return false ; if ( N % 2 == 0 || ! isComposite ( N ) ) return false ; if ( __gcd ( A , N ) != 1 ) return false ; int mod = Power ( A , ( N - 1 ) / 2 , N ) ; if ( mod != 1 && mod != N - 1 ) return false ; return true ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 121 , A = 3 ; if ( isEulerPseudoprime ( N , A ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Fermat Pseudoprime | Java program to check if N is Fermat pseudoprime to the base A or not ; Function to check if the given number is composite ; Check if there is any divisor of n less than sqrt ( n ) ; Effectively calculate ( x ^ y ) modulo mod ; Initialize result ; If power is odd , then update the answer ; Square the number and reduce the power to its half ; Return the result ; Function to check for Fermat Pseudoprime ; If it is composite and satisfy Fermat criterion ; Else return 0 ; Driver Code ; Function call"
How can the above be solved in Java?

class GFG { static boolean checkcomposite ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return true ; } } return false ; } static int power ( int x , int y , int mod ) { int res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) { res = ( res * x ) % mod ; } y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int Check ( int n , int a ) { if ( a > 1 && checkcomposite ( n ) && power ( a , n - 1 , n ) == 1 ) { return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 645 ; int a = 2 ; System . out . println ( Check ( N , a ) ) ; } }


"Check if a number is Flavius Number | Java implementation of the above approach ; Return the number is Flavious Number or not ; index i starts from 2 because at 1 st iteration every 2 nd element was remove and keep going for k - th iteration ; removing the elements which are already removed at kth iteration ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean Survives ( int n ) { for ( int i = 2 ; ; i ++ ) { if ( i > n ) return true ; if ( n % i == 0 ) return false ; n -= n / i ; } } public static void main ( String [ ] args ) { int n = 17 ; if ( Survives ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Full Fibonacci or not | Java program to check if a given number is a full fibonacci number or not ; A utility function that returns true if x is perfect square ; Returns true if N is a fibonacci number and false otherwise ; N is fibonacci if one of 5 * N ^ 2 + 4 or 5 * N ^ 2 - 4 or both is a perferct square ; Function to check digits ; Check if all digits are fibonacci or not ; Extract digit ; Check if the current digit is not fibonacci ; Function to check and return if N is a full fibonacci number or not ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static boolean checkDigits ( int n ) { while ( n != 0 ) { int dig = n % 10 ; if ( dig == 4 && dig == 6 && dig == 7 && dig == 9 ) return false ; n /= 10 ; } return true ; } static boolean isFullfibonacci ( int n ) { return ( checkDigits ( n ) && isFibonacci ( n ) ) ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isFullfibonacci ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is Full Prime | Java program for checking of full prime ; function to check digits ; check all digits are prime or not ; check if digits are prime or not ; To check if n is prime or not ; check for all factors ; To check if n is Full Prime ; The order is important here for efficiency ; driver code"
How can the above be solved in Java?

import java . util . * ; class Prime { public static boolean checkDigits ( int n ) { while ( n > 0 ) { int dig = n % 10 ; if ( dig != 2 && dig != 3 && dig != 5 && dig != 7 ) return false ; n /= 10 ; } return true ; } public static boolean prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } public static boolean isFullPrime ( int n ) { return ( checkDigits ( n ) && prime ( n ) ) ; } public static void main ( String [ ] args ) { int n = 53 ; if ( isFullPrime ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a number is Quartan Prime or not | JAVA program to check if a number is Quartan Prime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program ; Check if number is prime and of the form 16 * n + 1"
How can the above be solved in Java?

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 ; } public static void main ( String [ ] args ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if a number is Triperfect Number | Java code to check if a given number is Triperfect or not ; Returns true if n is Triperfect ; To store sum of divisors . Adding 1 and n since they are divisors of n . ; Find all divisors and add them ; If sum of divisors is equal to 3 * n , then n is a Triperfect number ; Driver program"
How can the above be solved in Java?

public class GFG { static boolean isTriPerfect ( int n ) { int sum = 1 + n ; int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else sum = sum + i + n / i ; } i += 1 ; } if ( sum == 3 * n & n != 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 120 ; if ( isTriPerfect ( n ) ) System . out . println ( n + " ▁ is ▁ a ▁ Triperfect ▁ number " ) ; } }


"Check if a number is a Mystery Number | Java implementation of above approach ; Finds reverse of given num x . ; if found print the pair , return"
How can the above be solved in Java?

class GFG { static int reverseNum ( int x ) { String s = Integer . toString ( x ) ; String str = " " ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { str = str + s . charAt ( i ) ; } int rev = Integer . parseInt ( str ) ; return rev ; } static boolean isMysteryNumber ( int n ) { for ( int i = 1 ; i <= n / 2 ; i ++ ) { int j = reverseNum ( i ) ; if ( i + j == n ) { System . out . println ( i + " ▁ " + j ) ; return true ; } } System . out . println ( " Not ▁ a ▁ Mystery ▁ Number " ) ; return false ; } public static void main ( String [ ] args ) { int n = 121 ; isMysteryNumber ( n ) ; } }


"Check if a number is a Pythagorean Prime or not | JAVA program to check if a number is Pythagorean prime or not ; Function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Driver Program ; Check if number is prime and of the form 4 n + 1"
How can the above be solved in Java?

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 ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPrime ( n ) && ( n % 4 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if a number is a Trojan Number | Java program to check if a number is Trojan Number or not ; Function to check if a number can be expressed as x ^ y ; Try all numbers from 2 to sqrt ( n ) as base ; Keep increasing y while power ' p ' is smaller than n . ; Function to check if a number is Strong ; count the number for each prime factor ; minimum number of prime divisors should be 2 ; Function to check if a number is Trojan Number ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPerfectPower ( int n ) { if ( n == 1 ) { return true ; } for ( int x = 2 ; x <= Math . sqrt ( n ) ; x ++ ) { int y = 2 ; int p = ( int ) Math . pow ( x , y ) ; while ( p <= n && p > 0 ) { if ( p == n ) { return true ; } y ++ ; p = ( int ) Math . pow ( x , y ) ; } } return false ; } static boolean isStrongNumber ( int n ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; while ( n % 2 == 0 ) { n = n / 2 ; if ( count . containsKey ( 2 ) ) { count . put ( 2 , count . get ( 2 ) + 1 ) ; } else { count . put ( 2 , 1 ) ; } } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { n = n / i ; if ( count . containsKey ( i ) ) { count . put ( i , count . get ( i ) + 1 ) ; } else { count . put ( i , 1 ) ; } } } if ( n > 2 ) { if ( count . containsKey ( n ) ) { count . put ( n , count . get ( n ) + 1 ) ; } else { count . put ( n , 1 ) ; } } int flag = 0 ; for ( Map . Entry < Integer , Integer > b : count . entrySet ( ) ) { if ( b . getValue ( ) == 1 ) { flag = 1 ; break ; } } if ( flag == 1 ) { return false ; } else { return true ; } } static boolean isTrojan ( int n ) { if ( ! isPerfectPower ( n ) && isStrongNumber ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 108 ; if ( isTrojan ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if a number is a perfect square having all its digits as a perfect square | Java program for the above approach ; Function to check if digits of N is a perfect square or not ; Iterate over the digits ; Extract the digit ; Check if digit is a perfect square or not ; Divide N by 10 ; Return true ; Function to check if N is a perfect square or not ; If floor and ceil of n is not same ; Function to check if N satisfies the required conditions or not ; If both the conditions are satisfied ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean check_digits ( long N ) { while ( N > 0 ) { int n = ( int ) ( N % 10 ) ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return false ; } N = N / 10 ; } return true ; } static boolean is_perfect ( long N ) { double n = Math . sqrt ( N ) ; if ( Math . floor ( n ) != Math . ceil ( n ) ) { return false ; } return true ; } static void isFullSquare ( long N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { long N = 144 ; isFullSquare ( N ) ; } }


"Check if a number is a power of another number | Java program to check given number y is power of x ; logarithm function to calculate value ; Note : this is double ; compare to the result1 or result2 both are equal ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } public static void main ( String args [ ] ) { if ( isPower ( 27 , 729 ) ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }


"Check if a number is an Achilles number or not | Program to check if the Given number is an Achilles Number ; function to check if the number is powerful number ; First divide the number repeatedly by 2 ; If only 2 ^ 1 divides n ( not higher powers ) , then return false ; if n is not a power of 2 then this loop will execute repeat above process ; Find highest power of " factor " that divides n ; If only factor ^ 1 divides n ( not higher powers ) , then return false ; n must be 1 now if it is not a prime number . Since prime numbers are not powerful , we return false if n is not 1. ; Utility function to check if number is a perfect power or not ; Function to check Achilles Number ; Driver Program"
How can the above be solved in Java?

class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isAchillesNumber ( int n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 72 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; n = 36 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a number is divisible by 31 or not | Java program to check whether a number is divisible by 31 or not ; Function to check if the number is divisible by 31 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting three times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 31 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 * 3 ) ) ; } return ( n % 31 == 0 ) ; } public static void main ( String [ ] args ) { int N = 1922 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a number is divisible by 47 or not | Java program to check whether a number is divisible by 47 or not ; Function to check if the number is divisible by 47 or not ; While there are at least two digits ; Extracting the last ; Truncating the number ; Subtracting fourteen times the last digit to the remaining number ; Finally return if the two - digit number is divisible by 47 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 * 14 ) ) ; } return ( n % 47 == 0 ) ; } public static void main ( String [ ] args ) { int N = 59173 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a number is divisible by all prime divisors of another number | Java program to find if all prime factors of y divide x . ; Returns true if all prime factors of y divide x . ; Driver program to test above functions"
How can the above be solved in Java?

class Divisible { public static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static boolean isDivisible ( int x , int y ) { if ( y == 1 ) return true ; int z = gcd ( x , y ) ; if ( z == 1 ) return false ; return isDivisible ( x , y / z ) ; } public static void main ( String [ ] args ) { int x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is formed by Concatenation of 1 , 14 or 144 only | Java program to check if a number is formed by Concatenation of 1 , 14 or 144 only ; Function to check if a number is formed by Concatenation of 1 , 14 or 144 only ; check for each possible digit if given number consist other then 1 , 14 , 144 print NO else print YES ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static String checkNumber ( int N ) { int temp = N ; while ( temp > 0 ) { if ( temp % 1000 == 144 ) temp /= 1000 ; else if ( temp % 100 == 14 ) temp /= 100 ; else if ( temp % 10 == 1 ) temp /= 10 ; else { return " NO " ; } } return " YES " ; } public static void main ( String [ ] args ) { int N = 1414 ; System . out . println ( checkNumber ( N ) ) ; } }


"Check if a number is in given base or not | Java program to check if given number is in given base or not . ; Allowed bases are till 16 ( Hexadecimal ) ; If base is below or equal to 10 , then all digits should be from 0 to 9. ; If base is below or equal to 16 , then all digits should be from 0 to 9 or from ' A ' ; Driver Class"
How can the above be solved in Java?

class Geeks { static boolean isInGivenBase ( String str , int base ) { if ( base > 16 ) return false ; else if ( base <= 10 ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) ) return false ; } else { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) || ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) < ( ' A ' + base - 10 ) ) ) ) return false ; } return true ; } public static void main ( String args [ ] ) { String str = " AF87" ; if ( isInGivenBase ( str , 16 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is jumbled or not | Java code to check if a number is jumbled or not ; Function to check if a number is jumbled or not ; Single digit number ; Checking every digit through a loop ; All digits were checked ; Digit at index i ; Digit at index i - 1 ; If difference is greater than 1 ; Number checked ; Driver code ; - 1234 to be checked ; 287 to be checked"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean checkJumbled ( int num ) { if ( num / 10 == 0 ) return true ; while ( num != 0 ) { if ( num / 10 == 0 ) return true ; int digit1 = num % 10 ; int digit2 = ( num / 10 ) % 10 ; if ( Math . abs ( digit2 - digit1 ) > 1 ) return false ; num = num / 10 ; } return true ; } public static void main ( String [ ] args ) { int num = - 1234 ; if ( checkJumbled ( num ) ) System . out . println ( " True ▁ " ) ; else System . out . println ( " False ▁ " ) ; num = - 1247 ; if ( checkJumbled ( num ) ) System . out . println ( " True ▁ " ) ; else System . out . println ( " False ▁ " ) ; } }


"Check if a number is magic ( Recursive sum of digits is 1 ) | Java program to check Whether the number is Magic or not . ; Accepting sample input ; Condition to check Magic number"
How can the above be solved in Java?

class GFG { public static void main ( String [ ] args ) { int x = 1234 ; if ( x % 9 == 1 ) System . out . printf ( " Magic ▁ Number " ) ; else System . out . printf ( " Not ▁ a ▁ Magic ▁ Number " ) ; } }


"Check if a number is magic ( Recursive sum of digits is 1 ) | Java program to check if a number is Magic number . ; Note that the loop continues if n is 0 and sum is non - zero . It stops when n becomes 0 and sum becomes single digit . ; Return true if sum becomes 1. ; Driver code"
How can the above be solved in Java?

class GFG { public static boolean isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void main ( String args [ ] ) { int n = 1234 ; if ( isMagic ( n ) ) System . out . println ( " Magic ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ magic ▁ Number " ) ; } }


"Check if a number is multiple of 5 without using / and % operators | Assuming that integer takes 4 bytes , there can be maximum 10 digits in a integer ; Check the last character of string ; Driver Code"
How can the above be solved in Java?

class GFG { static int MAX = 11 ; static boolean isMultipleof5 ( int n ) { char str [ ] = new char [ MAX ] ; int len = str . length ; if ( str [ len - 1 ] == '5' str [ len - 1 ] == '0' ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) System . out . println ( n + " ▁ is ▁ multiple ▁ " + " of ▁ 5" ) ; else System . out . println ( n + " ▁ is ▁ not ▁ a ▁ " + " multiple ▁ of ▁ 5" ) ; } }


"Check if a number is multiple of 5 without using / and % operators | Java code to check if a number is multiple of 5 without using ' / ' and ' % ' operators ; assumes that n is a positive integer ; Driver Code"
How can the above be solved in Java?

 class GFG { static boolean isMultipleof5 ( int n ) { while ( n > 0 ) n = n - 5 ; if ( n == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) System . out . printf ( "%d is multiple of 5
", n); else System . out . printf ( "%d is not a multiple of 5
", n); } }


"Check if a number is perfect square without finding square root | Java program for above approach ; Program to find if x is a perfect square . ; Check if mid is perfect square ; Mid is small -> go right to increase mid ; Mid is large -> to left to decrease mid ; Driver code ; Function Call"
How can the above be solved in Java?

class GFG { static boolean isPerfectSquare ( int num ) { long left = 1 , right = num ; while ( left <= right ) { long mid = ( left + right ) / 2 ; if ( mid * mid == num ) { return true ; } if ( mid * mid < num ) { left = mid + 1 ; } else { right = mid - 1 ; } } return false ; } public static void main ( String [ ] args ) { int x = 2500 ; if ( isPerfectSquare ( x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a number is positive , negative or zero using bit operators | Java program to find if a number is positive , negative or zero using bit wise operators . ; function to return 1 if it is zero returns 0 if it is negative returns 2 if it is positive ; string array to store all kinds of number ; function call to check the sign of number ; Driver code"
How can the above be solved in Java?

class GFG { static int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } static void check ( int n ) { String s [ ] = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; System . out . println ( n + " ▁ is ▁ " + s [ val ] ) ; } public static void main ( String [ ] args ) { check ( 30 ) ; check ( - 20 ) ; check ( 0 ) ; } }


"Check if a number is power of 8 or not | Java program to check if a number is power of 8 ; function to check if power of 8 ; calculate log8 ( n ) ; check if i is an integer or not ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean checkPowerof8 ( int n ) { double i = Math . log ( n ) / Math . log ( 8 ) ; return ( i - Math . floor ( i ) < 0.000001 ) ; } public static void main ( String args [ ] ) { int n = 65 ; if ( checkPowerof8 ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number is power of k using base changing method | Java program to check if a number can be raised to k ; loop to change base n to base = k ; Find current digit in base k ; If digit is neither 0 nor 1 ; Make sure that only one 1 is present . ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPowerOfK ( int n , int k ) { boolean oneSeen = false ; while ( n > 0 ) { int digit = n % k ; if ( digit > 1 ) return false ; if ( digit == 1 ) { if ( oneSeen ) return false ; oneSeen = true ; } n /= k ; } return true ; } public static void main ( String [ ] args ) { int n = 64 , k = 4 ; if ( isPowerOfK ( n , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a number is sandwiched between primes | java Program to check whether a number is sandwiched between two primes or not ; returns true if number n is prime ; 0 and 1 both are non - primes ; finding square root of n ; checking if n has any factor upto square root of n if yes its not prime ; Driver 's Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean isPrime ( int n ) { if ( n == 0 n == 1 ) return false ; int root = ( int ) Math . sqrt ( n ) ; for ( int i = 2 ; i <= root ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static boolean isSandwitched ( int n ) { return ( isPrime ( n - 1 ) && isPrime ( n + 1 ) ) ; } public static void main ( String [ ] args ) { int n = 642 ; System . out . print ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; n = 9 ; System . out . print ( n + " ▁ : ▁ " ) ; if ( isSandwitched ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a number starts with another number or not | Java program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into Strings ; Check if s2 is a prefix of s1 or not using starts_with ( ) function ; If result is true , print " Yes " ; Driver Code ; Given numbers ; Function call"
How can the above be solved in Java?

class GFG { static void checkprefix ( int A , int B ) { String s1 = String . valueOf ( A ) ; String s2 = String . valueOf ( B ) ; boolean result ; result = s1 . startsWith ( s2 ) ; if ( result ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int A = 12345 , B = 12 ; checkprefix ( A , B ) ; } }


"Check if a number starts with another number or not | Java program for the above approach ; Function to check if B is a prefix of A or not ; Convert numbers into Strings ; Find the lengths of Strings s1 and s2 ; Base Case ; Traverse the Strings s1 & s2 ; If at any index characters are unequals then return false ; Return true ; Driver Code ; Given numbers ; Function call ; If B is a prefix of A , then print " Yes ""
How can the above be solved in Java?

class GFG { static boolean checkprefix ( int A , int B ) { String s1 = String . valueOf ( A ) ; String s2 = String . valueOf ( B ) ; int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 < n2 ) { return false ; } for ( int i = 0 ; i < n2 ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( 1 ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int A = 12345 , B = 12 ; boolean result = checkprefix ( A , B ) ; if ( ! result ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if a number with even number of digits is palindrome or not | Java program to find number is palindrome or not without using any extra space ; Function to check if the number is palindrome ; if divisible by 11 then true ; if not divisible by 11 ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPalindrome ( int n ) { if ( n % 11 == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { System . out . println ( isPalindrome ( 123321 ) ? " Palindrome " : " Not ▁ Palindrome " ) ; } }


"Check if a pair of integers A and B can coincide by shifting them by distances arr [ ( A % N + N ) % N ] and arr [ ( B % N + N ) % N ] | Java program to implement the above approach ; Function to check if two integers coincide at a point ; Store the final position of integers A and B ; Iterate over the range [ 0 , n ] ; Store the final position of the integer i ; If temp is present in the Map ; Print Yes and return ; Mark its final position as visited ; If every integer stored in the Map is unique ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void checkSamePosition ( int [ ] arr , int n ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( ( i + arr [ i ] ) % n + n ) % n ; if ( mp . get ( temp ) == null ) { System . out . println ( " Yes " ) ; return ; } mp . get ( temp + 1 ) ; } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 3 } ; int N = arr . length ; checkSamePosition ( arr , N ) ; } }


"Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Java program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Check for the invalid condition ; Otherwise , ; Driver code"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int first = ( X ^ Y ) ; int second = ( X + Y ) ; if ( first == second ) { System . out . println ( " No " ) ; } else { System . out . println ( " Yes " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }


"Check if a pair of integers from two ranges exists such that their Bitwise XOR exceeds both the ranges | Java program for the above approach ; Function to check if there exists any pair ( P , Q ) whose Bitwise XOR is greater than the Bitwise XOR of X and Y ; Stores the Bitwise XOR of X & Y ; Traverse all possible pairs ; If a pair exists ; If a pair is found ; Driver code"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int playerA = ( X ^ Y ) ; boolean flag = false ; for ( int i = 1 ; i <= X ; i ++ ) { for ( int j = 1 ; j <= Y ; j ++ ) { int val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }


"Check if a pair with given absolute difference exists in a Matrix | Java code to check for pair with given difference exists in the matrix or not ; Function to check if a pair with given difference exists in the matrix ; Store elements in a hash ; Loop to iterate over the elements of the matrix ; Driver Code ; Input matrix ; Given difference"
How can the above be solved in Java?

import java . util . * ; class GFG { static final int N = 4 ; static final int M = 4 ; static boolean isPairWithDiff ( int mat [ ] [ ] , int k ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] > k ) { int m = mat [ i ] [ j ] - k ; if ( s . contains ( m ) ) { return true ; } } else { int m = k - mat [ i ] [ j ] ; if ( s . contains ( m ) ) { return true ; } } s . add ( mat [ i ] [ j ] ) ; } } return false ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 100 } } ; int k = 85 ; System . out . println ( isPairWithDiff ( mat , k ) == true ? " YES " : " NO " ) ; } }


"Check if a pair with given product exists in Linked list | Java code to find the pair with given product ; Link list node ; Given a reference ( pointer to pointer ) to the head of a list and an int , push a new node on the front of the list . ; Function to check if pair with given product exists in the list Takes head pointer of the linked list and product ; Check if pair exits ; Driver program to test above function ; Start with the empty list ; Use push ( ) to construct linked list ; function to print the result"
How can the above be solved in Java?

import java . util . * ; class Solution { static final int MAX = 100000 ; static class Node { int data ; Node next ; } 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 ; } static boolean check_pair_product ( Node head , int prod ) { Vector < Integer > s = new Vector < Integer > ( ) ; Node p = head ; while ( p != null ) { int curr = p . data ; if ( ( prod % curr == 0 ) && ( s . contains ( prod / curr ) ) ) { System . out . print ( curr + " ▁ " + ( prod / curr ) ) ; return true ; } s . add ( p . data ) ; p = p . next ; } return false ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; head = push ( head , 12 ) ; head = push ( head , 1 ) ; head = push ( head , 18 ) ; head = push ( head , 47 ) ; head = push ( head , 16 ) ; head = push ( head , 12 ) ; head = push ( head , 14 ) ; boolean res = check_pair_product ( head , 24 ) ; if ( res == false ) System . out . println ( " NO ▁ PAIR ▁ EXIST " ) ; } }


"Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Java program for the above approach ; Function to check if a string is palindrome or not ; Start and end of the string ; Iterate the string until i > j ; If there is a mismatch ; Increment first pointer and decrement the other ; Given string is a palindrome ; Function two check if the strings can be combined to form a palindrome ; Initialize array of characters ; Stores character of string in the character array ; Find left and right parts of strings a and b ; Substring a [ j ... i - 1 ] ; Substring b [ j ... i - 1 ] ; Substring a [ i ... n ] ; Substring b [ i ... n ] ; Check is left part of a + right part of b or vice versa is a palindrome ; Print the result ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean isPalindrome ( String str ) { int i = 0 , j = str . length ( ) - 1 ; while ( i < j ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static void formPalindrome ( String a , String b , int n ) { char aa [ ] = new char [ n + 2 ] ; char bb [ ] = new char [ n + 2 ] ; Arrays . fill ( aa , ' ▁ ' ) ; Arrays . fill ( bb , ' ▁ ' ) ; for ( int i = 1 ; i <= n ; i ++ ) { aa [ i ] = a . charAt ( i - 1 ) ; bb [ i ] = b . charAt ( i - 1 ) ; } boolean ok = false ; for ( int i = 0 ; i <= n + 1 ; i ++ ) { StringBuilder la = new StringBuilder ( ) ; StringBuilder ra = new StringBuilder ( ) ; StringBuilder lb = new StringBuilder ( ) ; StringBuilder rb = new StringBuilder ( ) ; for ( int j = 1 ; j <= i - 1 ; j ++ ) { la . append ( ( aa [ j ] == ' ▁ ' ) ? " " : aa [ j ] ) ; lb . append ( ( bb [ j ] == ' ▁ ' ) ? " " : bb [ j ] ) ; } for ( int j = i ; j <= n + 1 ; j ++ ) { ra . append ( ( aa [ j ] == ' ▁ ' ) ? " " : aa [ j ] ) ; rb . append ( ( bb [ j ] == ' ▁ ' ) ? " " : bb [ j ] ) ; } if ( isPalindrome ( la . toString ( ) + rb . toString ( ) ) || isPalindrome ( lb . toString ( ) + ra . toString ( ) ) ) { ok = true ; break ; } } if ( ok ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String A = " bdea " ; String B = " abbb " ; int N = 4 ; formPalindrome ( A , B , N ) ; } }


"Check if a palindromic string can be obtained by concatenating substrings split from same indices of two given strings | Java program to implement the above approach ; Iterate through the length if we could find a [ i ] = = b [ j ] we could increment I and decrement j ; Else we could just break the loop as its not a palindrome type sequence ; We could concatenate the a ' s ▁ left ▁ part ▁ + b ' s right part in a variable a and a ' s ▁ right ▁ part + b ' s left in the variable b ; We would check for the palindrome condition if yes we print True else False ; Driver Code"
How can the above be solved in Java?

import java . util . * ; import java . io . * ; class GFG { public static String rev ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; char [ ] s = str . toCharArray ( ) ; while ( st < ed ) { char temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return ( s . toString ( ) ) ; } public static boolean check ( String a , String b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a . charAt ( i ) != b . charAt ( j ) ) break ; i += 1 ; j -= 1 ; String xa = a . substring ( i , j + 1 ) ; String xb = b . substring ( i , j + 1 ) ; StringBuffer XA = new StringBuffer ( xa ) ; XA . reverse ( ) ; StringBuffer XB = new StringBuffer ( xb ) ; XB . reverse ( ) ; if ( xa == XA . toString ( ) || xb == XB . toString ( ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String a = " xbdef " ; String b = " cabex " ; if ( check ( a , b , a . length ( ) ) || check ( b , a , a . length ( ) ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }


"Check if a path exists for a cell valued 1 to reach the bottom right corner of a Matrix before any cell valued 2 | Java program for the above approach ; Function to check if cell with value 1 doesn 't reaches the bottom right cell or not ; Number of rows and columns ; Initialise the deque ; Traverse the matrix ; Push 1 to front of queue ; Push 2 to back of queue ; Store all the possible direction of the current cell ; Run multi - source BFS ; Get the front element ; Pop the front element ; If 1 reached corner first ; Insert new point in queue ; If 1 can 't reach the bottom  right then return false ; Driver Code ; Given matrix ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; class GFG { static boolean reachesBottom ( int [ ] [ ] a ) { int n = a . length ; int m = a [ 0 ] . length ; Deque < int [ ] > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == 1 ) { q . addFirst ( new int [ ] { i , j , 1 } ) ; } else if ( a [ i ] [ j ] == 2 ) { q . addLast ( new int [ ] { i , j , 2 } ) ; } a [ i ] [ j ] = 0 ; } } int dx [ ] = { - 1 , 0 , 1 , 0 } ; int dy [ ] = { 0 , 1 , 0 , - 1 } ; while ( ! q . isEmpty ( ) ) { int [ ] front = q . peekFirst ( ) ; q . removeFirst ( ) ; int i = front [ 0 ] , j = front [ 1 ] ; int t = front [ 2 ] ; if ( a [ i ] [ j ] == 1 ) continue ; a [ i ] [ j ] = 1 ; if ( t == 1 && ( i == n - 1 && j == m - 1 ) ) { return true ; } for ( int d = 0 ; d < 4 ; d ++ ) { int ni = i + dx [ d ] ; int nj = j + dy [ d ] ; if ( ni >= 0 && ni < n && nj >= 0 && nj < m ) { q . addLast ( new int [ ] { ni , nj , t } ) ; } } } return false ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , 2 , 0 } , { 0 , 1 , 0 } , { 0 , 2 , 0 } } ; if ( reachesBottom ( matrix ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }


"Check if a path exists from a given cell to any boundary element of the Matrix with sum of elements not exceeding K | Java program for the above approach ; Function to check if it is valid to move to the given index or not ; Otherwise return false ; Function to check if there exists a path from the cell ( X , Y ) of the matrix to any boundary cell with sum of elements at most K or not ; Base Case ; If K >= board [ X ] [ Y ] ; Stores the current element ; Mark the current cell as visited ; Visit all the adjacent cells ; Mark the cell unvisited ; Return false ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isValid ( int [ ] [ ] board , int i , int j , int K ) { if ( board [ i ] [ j ] <= K ) { return true ; } return false ; } static boolean findPath ( int [ ] [ ] board , int X , int Y , int M , int N , int K ) { if ( X < 0 X == M Y < 0 Y == N ) { return true ; } if ( isValid ( board , X , Y , K ) ) { int board_XY = board [ X ] [ Y ] ; board [ X ] [ Y ] = Integer . MAX_VALUE ; if ( findPath ( board , X + 1 , Y , M , N , K - board_XY ) || findPath ( board , X - 1 , Y , M , N , K - board_XY ) || findPath ( board , X , Y + 1 , M , N , K - board_XY ) || findPath ( board , X , Y - 1 , M , N , K - board_XY ) ) { return true ; } board [ X ] [ Y ] = board_XY ; } return false ; } public static void main ( String [ ] args ) { int [ ] [ ] grid = { { 25 , 5 , 25 , 25 , 25 , 25 } , { 25 , 1 , 1 , 5 , 12 , 25 } , { 25 , 1 , 12 , 0 , 15 , 25 } , { 22 , 1 , 11 , 2 , 19 , 15 } , { 25 , 2 , 2 , 1 , 12 , 15 } , { 25 , 9 , 10 , 1 , 11 , 25 } , { 25 , 25 , 25 , 25 , 25 , 25 } } ; int K = 17 ; int M = grid . length ; int N = grid [ 0 ] . length ; int X = 2 , Y = 3 ; if ( findPath ( grid , X , Y , M , N , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a permutation of S2 can be obtained by adding or removing characters from S1 | Java program for the above approach ; Function to check if the given number is prime or not ; If the number is less than 2 ; If the number is 2 ; If N is a multiple of 2 ; Otherwise , check for the odds values ; Function to check if S1 can be a permutation of S2 by adding or removing characters from S1 ; Initialize a frequency array ; Decrement the frequency for occurrence in s1 ; Increment the frequency for occurence in s2 ; If frequency of current char is same in s1 and s2 ; Check the frequency for the current char is not prime ; Print the result ; Driver Code"
How can the above be solved in Java?

public class GFG { private static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; else if ( n == 2 ) return true ; else if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } private static void checkPermutation ( String s1 , String s2 ) { int freq [ ] = new int [ 26 ] ; for ( char ch : s1 . toCharArray ( ) ) { freq [ ch - ' a ' ] -- ; } for ( char ch : s2 . toCharArray ( ) ) { freq [ ch - ' a ' ] ++ ; } boolean isAllChangesPrime = true ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) { continue ; } else if ( ! isPrime ( Math . abs ( freq [ i ] ) ) ) { isAllChangesPrime = false ; break ; } } if ( isAllChangesPrime ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S1 = " gekforgk " ; String S2 = " geeksforgeeks " ; checkPermutation ( S1 , S2 ) ; } }


"Check if a point having maximum X and Y coordinates exists or not | Java program for the above approach ; Function to find the point having max X and Y coordinates ; Initialize maxX and maxY ; Length of the given array ; Get maximum X & Y coordinates ; Check if the required point i . e . , ( maxX , maxY ) is present ; If point with maximum X and Y coordinates is present ; If no such point exists ; Driver Code ; Given array of points ; Print answer"
How can the above be solved in Java?

import java . io . * ; class GFG { static void findMaxPoint ( int arr [ ] [ ] ) { int maxX = Integer . MIN_VALUE ; int maxY = Integer . MIN_VALUE ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { maxX = Math . max ( maxX , arr [ i ] [ 0 ] ) ; maxY = Math . max ( maxY , arr [ i ] [ 1 ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( maxX == arr [ i ] [ 0 ] && maxY == arr [ i ] [ 1 ] ) { System . out . println ( " ( " + maxX + " , ▁ " + maxY + " ) " ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; findMaxPoint ( arr ) ; } }


"Check if a point having maximum X and Y coordinates exists or not | Java program for the above approach ; Initialize INF as inifnity ; Function to return the point having maximum X and Y coordinates ; Base Case ; Stores if valid point exists ; If point arr [ i ] is valid ; Check for the same point ; Check for a valid point ; If current point is the required point ; Otherwise ; Function to find the required point ; Stores the point with maximum X and Y - coordinates ; If no required point exists ; Driver Code ; Given array of points ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { static int INF = Integer . MAX_VALUE ; static int [ ] findMaxPoint ( int arr [ ] [ ] , int i , int n ) { if ( i == n ) return new int [ ] { INF , INF } ; boolean flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == i ) continue ; if ( arr [ j ] [ 0 ] >= arr [ i ] [ 0 ] arr [ j ] [ 1 ] >= arr [ i ] [ 1 ] ) { flag = false ; break ; } } if ( flag ) return arr [ i ] ; return findMaxPoint ( arr , i + 1 , n ) ; } static void findMaxPoints ( int arr [ ] [ ] , int n ) { int ans [ ] = findMaxPoint ( arr , 0 , n ) ; if ( ans [ 0 ] == INF ) { System . out . println ( - 1 ) ; } else { System . out . println ( " ( " + ans [ 0 ] + " ▁ " + ans [ 1 ] + " ) " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; int N = arr . length ; findMaxPoints ( arr , N ) ; } }


"Check if a point is inside , outside or on a Hyperbola | Java program for the above approach ; Function to check if the point ( x , y ) lies inside , on or outside the given hyperbola ; Stores the value of the equation ; Generate output based on value of p ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( int ) ( Math . pow ( ( x - h ) , 2 ) / Math . pow ( a , 2 ) ) - ( int ) ( Math . pow ( ( y - k ) , 2 ) / Math . pow ( b , 2 ) ) ; if ( p > 1 ) { System . out . println ( " Outside " ) ; } else if ( p == 1 ) { System . out . println ( " On ▁ the ▁ Hyperbola " ) ; } else { System . out . println ( " Inside " ) ; } } public static void main ( String [ ] args ) { int h = 0 , k = 0 , x = 2 ; int y = 1 , a = 4 , b = 5 ; checkpoint ( h , k , x , y , a , b ) ; } }


"Check if a point is inside , outside or on the ellipse | Java Program to check if the point lies within the ellipse or not ; Function to check the point ; checking the equation of ellipse with the given point ; Driver code"
How can the above be solved in Java?

import java . util . * ; class solution { static int checkpoint ( int h , int k , int x , int y , int a , int b ) { int p = ( ( int ) Math . pow ( ( x - h ) , 2 ) / ( int ) Math . pow ( a , 2 ) ) + ( ( int ) Math . pow ( ( y - k ) , 2 ) / ( int ) Math . pow ( b , 2 ) ) ; return p ; } public static void main ( String arr [ ] ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 , b = 5 ; if ( checkpoint ( h , k , x , y , a , b ) > 1 ) System . out . println ( " Outside " ) ; else if ( checkpoint ( h , k , x , y , a , b ) == 1 ) System . out . println ( " On ▁ the ▁ ellipse " ) ; else System . out . println ( " Inside " ) ; } }


"Check if a point is inside , outside or on the parabola | Java Program to check if the point lies within the parabola or not ; Function to check the point ; checking the equation of parabola with the given point ; driver code"
How can the above be solved in Java?

class solution { static int checkpoint ( int h , int k , int x , int y , int a ) { int p = ( int ) Math . pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) ; return p ; } public static void main ( String arr [ ] ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 ; if ( checkpoint ( h , k , x , y , a ) > 0 ) System . out . println ( " Outside " ) ; else if ( checkpoint ( h , k , x , y , a ) == 0 ) System . out . println ( " On ▁ the ▁ parabola " ) ; else System . out . println ( " Inside " ) ; } }


"Check if a prime number can be expressed as sum of two Prime Numbers | Java program to check if a prime number can be expressed as sum of two Prime Numbers ; Function to check whether a number is prime or not ; Function to check if a prime number can be expressed as sum of two Prime Numbers ; if the number is prime , and number - 2 is also prime ; Driver code"
How can the above be solved in Java?

public 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 boolean isPossible ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPossible ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a right | Java implementation of the approach ; Storing all the possible changes to make the triangle right - angled ; Function to check if the triangle is right - angled or not ; Function to check if the triangle can be transformed to right - angled ; Boolean variable to return true or false ; If it is already right - angled ; Applying the changes on the co - ordinates ; Applying on the first co - ordinate ; Applying on the second co - ordinate ; Applying on the third co - ordinate ; If can 't be transformed ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int dx [ ] = { - 1 , 0 , 1 , 0 } ; static int dy [ ] = { 0 , 1 , 0 , - 1 } ; static boolean ifRight ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) ; int b = ( ( x1 - x3 ) * ( x1 - x3 ) ) + ( ( y1 - y3 ) * ( y1 - y3 ) ) ; int c = ( ( x2 - x3 ) * ( x2 - x3 ) ) + ( ( y2 - y3 ) * ( y2 - y3 ) ) ; if ( ( a == ( b + c ) && a != 0 && b != 0 && c != 0 ) || ( b == ( a + c ) && a != 0 && b != 0 && c != 0 ) || ( c == ( a + b ) && a != 0 && b != 0 && c != 0 ) ) { return true ; } return false ; } static void isValidCombination ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int x , y ; boolean possible = false ; if ( ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) ) { System . out . print ( " ALREADY ▁ RIGHT ▁ ANGLED " ) ; return ; } else { for ( int i = 0 ; i < 4 ; i ++ ) { x = dx [ i ] + x1 ; y = dy [ i ] + y1 ; if ( ifRight ( x , y , x2 , y2 , x3 , y3 ) ) { System . out . print ( " POSSIBLE " ) ; return ; } x = dx [ i ] + x2 ; y = dy [ i ] + y2 ; if ( ifRight ( x1 , y1 , x , y , x3 , y3 ) ) { System . out . print ( " POSSIBLE " ) ; return ; } x = dx [ i ] + x3 ; y = dy [ i ] + y3 ; if ( ifRight ( x1 , y1 , x2 , y2 , x , y ) ) { System . out . print ( " POSSIBLE " ) ; return ; } } } if ( ! possible ) System . out . println ( " NOT ▁ POSSIBLE " ) ; } public static void main ( String [ ] args ) { int x1 = - 49 , y1 = 0 ; int x2 = 0 , y2 = 50 ; int x3 = 0 , y3 = - 50 ; isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) ; } }


"Check if a right | Java program for the above approach ; Function to check if right - angled triangle can be formed by the given coordinates ; Calculate the sides ; Check Pythagoras Formula ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void checkRightAngled ( int X1 , int Y1 , int X2 , int Y2 , int X3 , int Y3 ) { int A = ( int ) Math . pow ( ( X2 - X1 ) , 2 ) + ( int ) Math . pow ( ( Y2 - Y1 ) , 2 ) ; int B = ( int ) Math . pow ( ( X3 - X2 ) , 2 ) + ( int ) Math . pow ( ( Y3 - Y2 ) , 2 ) ; int C = ( int ) Math . pow ( ( X3 - X1 ) , 2 ) + ( int ) Math . pow ( ( Y3 - Y1 ) , 2 ) ; if ( ( A > 0 && B > 0 && C > 0 ) && ( A == ( B + C ) || B == ( A + C ) || C == ( A + B ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String s [ ] ) { int X1 = 0 , Y1 = 2 ; int X2 = 0 , Y2 = 14 ; int X3 = 9 , Y3 = 2 ; checkRightAngled ( X1 , Y1 , X2 , Y2 , X3 , Y3 ) ; } }


"Check if a right | Java program to implement the above approach ; Function to check if N is a perfect square number or not ; If N is a non positive integer ; Stores square root of N ; Check for perfect square ; If N is not a perfect square number ; Function to check if given two sides of a triangle forms a right - angled triangle ; If the value of ( A * A + B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( A * A - B * B ) is a perfect square number ; Update checkTriangle ; If the value of ( B * B - A * A ) is a perfect square number ; Update checkTriangle ; Driver Code ; If the given two sides of a triangle forms a right - angled triangle ; Otherwise"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static int checkPerfectSquare ( int N ) { if ( N <= 0 ) { return 0 ; } double sq = Math . sqrt ( N ) ; if ( Math . floor ( sq ) == Math . ceil ( sq ) ) { return 1 ; } return 0 ; } static boolean checktwoSidesareRighTriangle ( int A , int B ) { boolean checkTriangle = false ; if ( checkPerfectSquare ( A * A + B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( A * A - B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( B * B - A * A ) != 0 ) { checkTriangle = true ; } return checkTriangle ; } public static void main ( String [ ] args ) { int A = 3 , B = 4 ; if ( checktwoSidesareRighTriangle ( A , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if a string can be converted to another given string by removal of a substring | Java program to implement the above approach ; Function to check if S can be converted to T by removing at most one subString from S ; Check if S can be converted to T by removing at most one subString from S ; Stores length of String T ; Stores length of String S ; Iterate over the range [ 0 , M - 1 ] ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores Length of the subString { S [ 0 ] , ... , S [ i ] } ; Stores prefix subString ; Stores suffix subString ; Checking if prefix + suffix == T ; Driver Code ; Given String S and T ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static String make_String_S_to_T ( String S , String T ) { boolean possible = false ; int M = T . length ( ) ; int N = S . length ( ) ; for ( int i = 0 ; i <= M ; i ++ ) { int prefix_length = i ; int suffix_length = M - i ; String prefix = S . substring ( 0 , prefix_length ) ; String suffix = S . substring ( N - suffix_length , N ) ; if ( ( prefix + suffix ) . equals ( T ) ) { possible = true ; break ; } } if ( possible ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String S = " ababcdcd " ; String T = " abcd " ; System . out . print ( make_String_S_to_T ( S , T ) ) ; } }


"Check if a string can be converted to another string by replacing vowels and consonants | Java program to check if a string can be converted to other string by replacing vowels and consonants ; Function to check if the character is vowel or not ; Function that checks if a string can be converted to another string ; Find length of string ; If length is not same ; Iterate for every character ; If both vowel ; Both are consonants ; Driver Code"
How can the above be solved in Java?

class GfG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean checkPossibility ( String s1 , String s2 ) { int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( l1 != l2 ) return false ; for ( int i = 0 ; i < l1 ; i ++ ) { if ( isVowel ( s1 . charAt ( i ) ) && isVowel ( s2 . charAt ( i ) ) ) continue ; else if ( ! ( isVowel ( s1 . charAt ( i ) ) ) && ! ( isVowel ( s2 . charAt ( i ) ) ) ) continue ; else return false ; } return true ; } public static void main ( String [ ] args ) { String S1 = " abcgle " ; String S2 = " ezggli " ; if ( checkPossibility ( S1 , S2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a string can be emptied by removing all subsequences of the form "10" | Java program for the above approach ; Function to find if string is reducible to NULL ; Length of string ; Stack to store all 1 s ; Iterate over the characters of the string ; If current character is 1 ; Push it into the stack ; Pop from the stack ; If the stack is empty ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean isReducible ( String str ) { int N = str . length ( ) ; ArrayList < Character > s = new ArrayList < Character > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '1' ) s . add ( str . charAt ( i ) ) ; else if ( s . size ( ) > 0 ) s . remove ( s . size ( ) - 1 ) ; else return false ; } if ( s . size ( ) == 0 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { String str = "11011000" ; if ( isReducible ( str ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if a string can be formed from another string by at most X circular clockwise shifts | Java implementation to check that a given string can be converted to another string by circular clockwise shift of each character by atmost X times ; Function to check that all characters of s1 can be converted to s2 by circular clockwise shift atmost X times ; Check for all characters of the strings whether the difference between their ascii values is less than X or not ; If both the characters are same ; Calculate the difference between the ASCII values of the characters ; If difference exceeds X ; Driver Code ; Function call"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void isConversionPossible ( String s1 , String s2 , int x ) { int diff = 0 , n ; n = s1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 . charAt ( i ) == s2 . charAt ( i ) ) continue ; diff = ( ( int ) ( s2 . charAt ( i ) - s1 . charAt ( i ) ) + 26 ) % 26 ; if ( diff > x ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { String s1 = " you " ; String s2 = " ara " ; int x = 6 ; isConversionPossible ( s1 , s2 , x ) ; } }


"Check if a string can be made empty by repeatedly removing given subsequence | Java program for the above approach ; Function to check if a string can be made empty by removing all subsequences of the form " GFG " or not ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static void findIfPossible ( int N , String str ) { int countG = 0 , countF = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) countG ++ ; else countF ++ ; } if ( 2 * countF != countG ) { System . out . println ( " No " ) ; } else { int id = 0 ; boolean flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' G ' ) { countG -- ; id ++ ; } else { countF -- ; id -- ; } if ( id < 0 ) { flag = false ; break ; } if ( countG < countF ) { flag = false ; break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { int n = 6 ; String str = " GFGFGG " ; findIfPossible ( n , str ) ; } }


"Check if a string can be obtained by rotating another string 2 places | Java program to check if a string is two time rotation of another string . ; Method to check if string2 is obtained by string 1 ; Initialize string as anti - clockwise rotation ; Initialize string as clock wise rotation ; check if any of them is equal to string1 ; Driver method"
How can the above be solved in Java?

class Test { static boolean isRotated ( String str1 , String str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; if ( str1 . length ( ) < 2 ) { return str1 . equals ( str2 ) ; } String clock_rot = " " ; String anticlock_rot = " " ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len ) + str2 . substring ( 0 , len - 2 ) ; clock_rot = clock_rot + str2 . substring ( 2 ) + str2 . substring ( 0 , 2 ) ; return ( str1 . equals ( clock_rot ) || str1 . equals ( anticlock_rot ) ) ; } public static void main ( String [ ] args ) { String str1 = " geeks " ; String str2 = " eksge " ; System . out . println ( isRotated ( str1 , str2 ) ? " Yes " : " No " ) ; } }


"Check if a string can be rearranged to form special palindrome | Java implementation of the above approach ; Driver code ; creating a array which stores the frequency of each character ; Checking if a character is uppercase or not ; Increasing by 1 if uppercase ; Decreasing by 1 if lower case ; Storing the sum of positive numbers in the frequency array ; Storing the sum of negative numbers in the frequency array ; If all character balances out then its Yes ; If there is only 1 character which does not balances then also it is Yes"
How can the above be solved in Java?

public class Improve { public static void main ( String args [ ] ) { String s = " ABCdcba " ; int u [ ] = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Character . isUpperCase ( s . charAt ( i ) ) ) { u [ s . charAt ( i ) - 65 ] += 1 ; } else { u [ s . charAt ( i ) - 97 ] -= 1 ; } } boolean f1 = true ; int po = 0 ; int ne = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( u [ i ] > 0 ) po += u [ i ] ; if ( u [ i ] < 0 ) ne += u [ i ] ; } if ( po == 0 && ne == 0 ) System . out . println ( " YES " ) ; else if ( po == 1 && ne == 0 ) System . out . println ( " YES " ) ; else if ( po == 0 && ne == - 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a string can be split into 3 substrings such that one of them is a substring of the other two | Java program for the above approach ; Function to check if string S contains any character with frequency >= 3 or not ; Stores frequency of characters ; Iterate over the string ; Update the frequency of current character ; Iterate over the hash array ; If any character has frequency >= 3 ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String freqCheck ( String S , int N ) { int hash [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ S . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hash [ i ] > 2 ) { return " Yes " ; } } return " No " ; } public static void main ( String [ ] args ) { String S = " geekseekforgeeks " ; int N = S . length ( ) ; System . out . println ( freqCheck ( S , N ) ) ; } }


"Check if a string can be split into substrings starting with N followed by N characters | Java program to implement the above approach ; Function to check if the given can be split into desired substrings ; Length of the string ; Traverse the string ; Extract the digit ; Check if the extracted number does not exceed the remaining length ; Check for the remaining string ; If generating desired substrings is not possible ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { public static boolean helper ( String s , int pos ) { int len = s . length ( ) ; if ( pos >= len ) return true ; if ( ! Character . isDigit ( s . charAt ( pos ) ) ) return false ; int num = 0 ; for ( int i = pos ; i < len ; i ++ ) { num = num * 10 + s . charAt ( pos ) - '0' ; if ( i + 1 + num > len ) return false ; if ( helper ( s , i + 1 + num ) ) return true ; } return false ; } public static void main ( String [ ] args ) { String s = "123abc4db1c " ; if ( helper ( s , 0 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if a string can be split into two substrings such that one substring is a substring of the other | Java program to implement the above approach ; Function to check if a String can be divided into two subStrings such that one subString is subString of the other ; Store the last character of S ; Traverse the characters at indices [ 0 , N - 2 ] ; Check if the current character is equal to the last character ; If true , set f = 1 ; Break out of the loop ; Driver Code ; Given String , S ; Store the size of S ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void splitString ( String S , int N ) { char c = S . charAt ( N - 1 ) ; int f = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S . charAt ( i ) == c ) { f = 1 ; break ; } } if ( f > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String S = " abcdab " ; int N = S . length ( ) ; splitString ( S , N ) ; } }


"Check if a string can be split into two substrings with equal number of vowels | Java program for the above approach ; Function to check if any character is a vowel or not ; Lowercase vowels ; Uppercase vowels ; Otherwise ; Function to check if string S can be split into two substrings with equal number of vowels ; Stores the count of vowels in the string S ; Traverse over the string ; If S [ i ] is vowel ; Stores the count of vowels upto the current index ; Traverse over the string ; If S [ i ] is vowel ; If vowelsTillNow and totalVowels are equal ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { public static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; if ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) return true ; return false ; } public static String containsEqualStrings ( String S ) { int totalVowels = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( isVowel ( S . charAt ( i ) ) ) totalVowels ++ ; } int vowelsTillNow = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( isVowel ( S . charAt ( i ) ) ) { vowelsTillNow ++ ; totalVowels -- ; if ( vowelsTillNow == totalVowels ) { return " Yes " ; } } } return " No " ; } public static void main ( String [ ] args ) { String S = " geeks " ; System . out . println ( containsEqualStrings ( S ) ) ; } }


"Check if a string can become empty by recursively deleting a given sub | Java program to check if a string can be converted to an empty string by deleting given sub - string from any position , any number of times . ; Returns true if str can be made empty by recursively removing sub_str . ; idx : to store starting index of sub - string found in the original string ; Erasing the found sub - string from the original string ; Driver code"
How can the above be solved in Java?

class GFG { static boolean canBecomeEmpty ( String str , String sub_str ) { while ( str . length ( ) > 0 ) { int idx = str . indexOf ( sub_str ) ; if ( idx == - 1 ) { break ; } str = str . replaceFirst ( sub_str , " " ) ; } return ( str . length ( ) == 0 ) ; } public static void main ( String [ ] args ) { String str = " GEEGEEKSKS " , sub_str = " GEEKS " ; if ( canBecomeEmpty ( str , sub_str ) ) { System . out . print ( " Yes "); } else { System . out . print ( " No "); } } }


"Check if a string consisting only of a , b , c can be made empty by removing substring " abc " recursively | Java program for the above approach ; Function to check if the given String S can be made empty ; Stores the characters of the String S ; Traverse the given String ; If the character is c ; If stack size is greater than 2 ; Pop from the stack ; Top two characters in the stack should be ' b ' and ' a ' respectively ; Otherwise , print No ; If character is ' a ' or ' b ' push to stack ; If stack is empty , then print Yes . Otherwise print No ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String canMadeEmpty ( String s , int n ) { Stack < Character > St = new Stack < Character > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' c ' ) { if ( St . size ( ) >= 2 ) { char b = St . peek ( ) ; St . pop ( ) ; char a = St . peek ( ) ; St . pop ( ) ; if ( a != ' a ' b != ' b ' ) return " No " ; } else return " No " ; } else St . add ( s . charAt ( i ) ) ; } if ( St . size ( ) == 0 ) { return " Yes " ; } else { return " No " ; } } public static void main ( String [ ] args ) { String S = " aabcbc " ; int N = S . length ( ) ; System . out . print ( canMadeEmpty ( S , N ) ) ; } }


"Check if a string contains a palindromic sub | Java program to check if there is a substring palindrome of even length . ; function to check if two consecutive same characters are present ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean check ( String s ) { for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " xzyyz " ; if ( check ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a string contains an anagram of another string as its substring | Java program for the above approach ; Function to check if string s2 contains anagram of the string s1 as its substring ; Stores frequencies of characters in substrings of s2 ; Stores frequencies of characters in s1 ; If length of s2 exceeds length of s1 ; Store frequencies of characters in first substring of length s1len in string s2 ; Perform Sliding Window technique ; If hashmaps are found to be identical for any substring ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { public static boolean checkAnagram ( String s1 , String s2 ) { int s2hash [ ] = new int [ 26 ] ; int s1hash [ ] = new int [ 26 ] ; int s1len = s1 . length ( ) ; int s2len = s2 . length ( ) ; if ( s1len > s2len ) return false ; int left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { if ( Arrays . equals ( s1hash , s2hash ) ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( left ) - ' a ' ] -= 1 ; left ++ ; } return false ; } public static void main ( String [ ] args ) { String s1 = " ab " ; String s2 = " bbpobac " ; if ( checkAnagram ( s1 , s2 ) ) System . out . println ( " YES " ) ; else System . out . println ( " No " ) ; } }


"Check if a string contains two non overlapping sub | Java implementation of the approach ; Function that returns true if s contains two non overlapping sub Strings " geek " and " keeg " ; If " geek " and " keeg " are both present in s without over - lapping and " keeg " starts after " geek " ends ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isValid ( String s ) { if ( ( s . indexOf ( " geek " ) != - 1 ) && ( s . indexOf ( " keeg " , s . indexOf ( " geek " ) + 4 ) != - 1 ) ) return true ; return false ; } public static void main ( String args [ ] ) { String s = " geekeekeeg " ; if ( isValid ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a string follows a ^ nb ^ n pattern or not | Java code to check a ^ nb ^ n pattern ; Returns " Yes " str is of the form a ^ nb ^ n . ; check first half is ' a ' and other half is full of ' b ' ; Driver code ; Function call"
How can the above be solved in Java?

import java . io . * ; class GFG { static String isAnBn ( String str ) { int n = str . length ( ) ; if ( ( n & 1 ) != 0 ) return " No " ; int i ; for ( i = 0 ; i < n / 2 ; i ++ ) if ( str . charAt ( i ) != ' a ' || str . charAt ( n - i - 1 ) != ' b ' ) return " No " ; return " Yes " ; } public static void main ( String [ ] args ) { String str = " ab " ; System . out . println ( isAnBn ( str ) ) ; } }


"Check if a string is Isogram or not | Java code to check string is isogram or not ; function to check isogram ; loop to store count of chars and check if it is greater than 1 ; if count > 1 , return false ; Driver code ; checking str as isogram ; checking str2 as isogram"
How can the above be solved in Java?

class GFG { static boolean check_isogram ( String str ) { int length = str . length ( ) ; int mapHash [ ] = new int [ 26 ] ; for ( int i = 0 ; i < length ; i ++ ) { mapHash [ str . charAt ( i ) - ' a ' ] ++ ; if ( mapHash [ str [ i ] - ' a ' ] > 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " geeks " ; String str2 = " computer " ; if ( check_isogram ( str ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; if ( check_isogram ( str2 ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }


"Check if a string is Pangrammatic Lipogram | Java program to check if a string is Pangrammatic Lipogram ; collection of letters ; function to check for a Pangrammatic Lipogram ; convert string to lowercase ; variable to keep count of all the letters not found in the string ; traverses the string for every letter of the alphabet ; if character not found in string then increment count ; Driver program to test above function"
How can the above be solved in Java?

import java . util . * ; class GFG { static String alphabets = " abcdefghijklmnopqrstuvwxyz " ; static void panLipogramChecker ( char [ ] s ) { for ( int i = 0 ; i < s . length ; i ++ ) { s [ i ] = Character . toLowerCase ( s [ i ] ) ; } int counter = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int pos = find ( s , alphabets . charAt ( i ) ) ; if ( pos < 0 pos > s . length ) counter += 1 ; } if ( counter == 0 ) System . out . println ( " Pangram " ) ; else if ( counter >= 2 ) System . out . println ( " Not ▁ a ▁ pangram ▁ but ▁ might ▁ a ▁ lipogram " ) ; else System . out . println ( " Pangrammatic ▁ Lipogram " ) ; } static int find ( char [ ] arr , char c ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( c == arr [ i ] ) return 1 ; } return - 1 ; } public static void main ( String [ ] args ) { char [ ] str = " The ▁ quick ▁ brown ▁ fox ▁ jumped ▁ over ▁ the ▁ lazy ▁ dog " . toCharArray ( ) ; panLipogramChecker ( str ) ; str = " The ▁ quick ▁ brown ▁ fox ▁ jumps ▁ over ▁ the ▁ lazy ▁ dog " . toCharArray ( ) ; panLipogramChecker ( str ) ; str = " The ▁ quick ▁ brown ▁ fox ▁ jum ▁ over ▁ the ▁ lazy ▁ dog " . toCharArray ( ) ; panLipogramChecker ( str ) ; } }


"Check if a string is a scrambled form of another string | Java Program to check if a given string is a scrambled form of another string ; map declaration for storing key value pair means for storing subproblem result ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Check for the condition of anagram ; make key of type string for search in map ; checking if both string are before calculated or not if calculated means find in map then return it 's  value ; declaring flag variable to store result ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; add key & flag value to map ( store for future use ) so next time no required to calculate it again ; If none of the above conditions are satisfied ; Driver Code"
How can the above be solved in Java?

import java . util . * ; public class Main { static HashMap < String , Boolean > mp = new HashMap < String , Boolean > ( ) ; static boolean isScramble ( String S1 , String S2 ) { if ( S1 . length ( ) != S2 . length ( ) ) { return false ; } int n = S1 . length ( ) ; if ( n != 0 ) { return true ; } if ( S1 == S2 ) { return true ; } String copy_S1 = S1 , copy_S2 = S2 ; char [ ] t1 = copy_S1 . toCharArray ( ) ; char [ ] t2 = copy_S2 . toCharArray ( ) ; Arrays . sort ( t1 ) ; Arrays . sort ( t2 ) ; copy_S1 = new String ( t1 ) ; copy_S2 = new String ( t2 ) ; if ( ! copy_S1 . equals ( copy_S2 ) ) { return false ; } String key = ( S1 + " ▁ " + S2 ) ; if ( mp . containsKey ( key ) ) { return mp . get ( key ) ; } boolean flag = false ; for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( i , n ) ) ) { flag = true ; return true ; } if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( n - i , n ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( 0 , n - i ) ) ) { flag = true ; return true ; } } mp . put ( key , flag ) ; return false ; } public static void main ( String [ ] args ) { String S1 = " coder " ; String S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if a string is a scrambled form of another string | Java program to check if a given string is a scrambled form of another string ; Strings of non - equal length cant ' be scramble strings ; Empty strings are scramble strings ; Equal strings are scramble strings ; Converting string to character array ; Checking condition for Anagram ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ 0. . . i ] and if S2 [ i + 1. . . n ] is a scrambled string of S1 [ i + 1. . . n ] ; Check if S2 [ 0. . . i ] is a scrambled string of S1 [ n - i ... n ] and S2 [ i + 1. . . n ] is a scramble string of S1 [ 0. . . n - i - 1 ] ; If none of the above conditions are satisfied ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isScramble ( String S1 , String S2 ) { if ( S1 . length ( ) != S2 . length ( ) ) { return false ; } int n = S1 . length ( ) ; if ( n == 0 ) { return true ; } if ( S1 . equals ( S2 ) ) { return true ; } char [ ] tempArray1 = S1 . toCharArray ( ) ; char [ ] tempArray2 = S2 . toCharArray ( ) ; Arrays . sort ( tempArray1 ) ; Arrays . sort ( tempArray2 ) ; String copy_S1 = new String ( tempArray1 ) ; String copy_S2 = new String ( tempArray2 ) ; if ( ! copy_S1 . equals ( copy_S2 ) ) { return false ; } for ( int i = 1 ; i < n ; i ++ ) { if ( isScramble ( S1 . substring ( 0 , i ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( i , n ) , S2 . substring ( i , n ) ) ) { return true ; } if ( isScramble ( S1 . substring ( n - i , n ) , S2 . substring ( 0 , i ) ) && isScramble ( S1 . substring ( 0 , n - i ) , S2 . substring ( i , n ) ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String S1 = " coder " ; String S2 = " ocred " ; if ( isScramble ( S1 , S2 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if a string is a subsequence of another string ( using Stacks ) | Java approach for the above approach ; Function to check if target is a subsequence of string S ; Declare a stack ; Push the characters of target into the stack ; Traverse the string S in reverse ; If the stack is empty ; if S [ i ] is same as the top of the stack ; Pop the top of stack ; Stack s is empty ; Driver Code"
How can the above be solved in Java?

import java . util . Stack ; public class GFG { static void checkforSubsequence ( String S , String target ) { Stack < Character > s = new Stack < > ( ) ; for ( int i = 0 ; i < target . length ( ) ; i ++ ) { s . push ( target . charAt ( i ) ) ; } for ( int i = ( int ) S . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s . empty ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( S . charAt ( i ) == s . peek ( ) ) { s . pop ( ) ; } } if ( s . empty ( ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " KOTTAYAM " ; String target = " KOTA " ; checkforSubsequence ( S , target ) ; } }


"Check if a string is concatenation of another given string | Java program to implement the above approach ; Function to check if a String is concatenation of another String ; Stores the length of str2 ; Stores the length of str1 ; If M is not multiple of N ; Traverse both the Strings ; If str1 is not concatenation of str2 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean checkConcat ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N % M != 0 ) { return false ; } for ( int i = 0 ; i < N ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i % M ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str1 = " abcabcabc " ; String str2 = " abc " ; if ( checkConcat ( str1 , str2 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if a string is made up of K alternating characters | Java implementation of the approach ; Function to check if a String is made up of k alternating characters ; Check if all the characters at indices 0 to K - 1 are different ; If that bit is already set in checker , return false ; Otherwise update and continue by setting that bit in the checker ; Driver code"
How can the above be solved in Java?

 class GFG { static boolean isKAlternating ( String s , int k ) { if ( s . length ( ) < k ) return false ; int checker = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int bitAtIndex = s . charAt ( i ) - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( int i = k ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i - k ) != s . charAt ( i ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " acdeac " ; int K = 4 ; if ( isKAlternating ( str , K ) ) System . out . print ( " Yes " + "
"); else System . out . print ( " No " + "
"); } }


"Check if a string is substring of another | Java program to check if a string is substring of other . ; Returns true if s1 is substring of s2 ; A loop to slide pat [ ] one by one ; For current index i , check for pattern match ; Driver code"
How can the above be solved in Java?

class GFG { static int isSubstring ( String s1 , String s2 ) { int M = s1 . length ( ) ; int N = s2 . length ( ) ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) if ( s2 . charAt ( i + j ) != s1 . charAt ( j ) ) break ; if ( j == M ) return i ; } return - 1 ; } public static void main ( String args [ ] ) { String s1 = " for " ; String s2 = " geeksforgeeks " ; int res = isSubstring ( s1 , s2 ) ; if ( res == - 1 ) System . out . println ( " Not ▁ present " ) ; else System . out . println ( " Present ▁ at ▁ index ▁ " + res ) ; } }


"Check if a string is substring of another | int counter = 0 ; pointing s2 ; Special case where character preceding the i 'th character is duplicate ; System . out . println ( s2 . indexOf ( " for " ) ) ;"
How can the above be solved in Java?

import java . io . * ; class GFG { public static int Substr ( String s2 , String s1 ) { int i = 0 ; for ( ; i < s1 . length ( ) ; i ++ ) { if ( counter == s2 . length ( ) ) break ; if ( s2 . charAt ( counter ) == s1 . charAt ( i ) ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ( ) ? - 1 : i - counter ; } public static void main ( String [ ] args ) { String s1 = " geeksfffffoorrfoorforgeeks " ; System . out . println ( Substr ( " for " , s1 ) ) ; } }


"Check if a string is suffix of another | Java program to find if a string is suffix of another ; Driver code ; Test case - sensitive implementation of endsWith function"
How can the above be solved in Java?

class GFG { static boolean isSuffix ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 . charAt ( n1 - i - 1 ) != s2 . charAt ( n2 - i - 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { String s1 = " geeks " , s2 = " geeksforgeeks " ; boolean result = isSuffix ( s1 , s2 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a string is suffix of another | Java program to find if a string is suffix of another ; Test case - sensitive implementation of endsWith function"
How can the above be solved in Java?

class GFG { public static void main ( String [ ] args ) { String s1 = " geeks " , s2 = " geeksforgeeks " ; boolean result = s2 . endsWith ( s1 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a string is the typed name of the given name | Java program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver Code"
How can the above be solved in Java?

public class Improve { static boolean isVowel ( char c ) { String vowel = " aeiou " ; for ( int i = 0 ; i < vowel . length ( ) ; ++ i ) if ( vowel . charAt ( i ) == c ) return true ; return false ; } static boolean printRLE ( String str , String typed ) { int n = str . length ( ) , m = typed . length ( ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != typed . charAt ( j ) ) return false ; if ( isVowel ( str . charAt ( i ) ) == false ) { j ++ ; continue ; } int count1 = 1 ; while ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) { count1 ++ ; i ++ ; } int count2 = 1 ; while ( j < m - 1 && typed . charAt ( j ) == str . charAt ( i ) ) { count2 ++ ; j ++ ; } if ( count1 > count2 ) return false ; } return true ; } public static void main ( String args [ ] ) { String name = " alex " , typed = " aaalaeex " ; if ( printRLE ( name , typed ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a string is the typed name of the given name | Java program to implement run length encoding ; Check if the character is vowel or not ; Returns true if ' typed ' is a typed name given str ; Traverse through all characters of str . ; If current characters do not match ; If not vowel , simply move ahead in both ; Count occurrences of current vowel in str ; Count occurrences of current vowel in typed . ; Driver code"
How can the above be solved in Java?


"Check if a string represents a hexadecimal number or not | Java implementation of the above approach ; Function to check if the string represents a hexadecimal number ; Size of string ; Iterate over string ; Check if the character is invalid ; Print true if all characters are valid ; Driver Code ; Given string ; Function Call"
How can the above be solved in Java?

import java . util . * ; import java . io . * ; class GFG { public static void checkHex ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( ( ch < '0' ch > '9' ) && ( ch < ' A ' ch > ' F ' ) ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { String s = " BF57C " ; checkHex ( s ) ; } }


"Check if a subarray exists with sum greater than the given Array | Java program to check if a subarray exists with sum greater than the given Array ; Function to check whether there exists a subarray whose sum is greater than or equal to sum of given array elements ; Initialize sum with 0 ; Checking possible prefix subarrays . If sum of them is less than or equal to zero , then return 1 ; again reset sum to zero ; Checking possible suffix subarrays . If sum of them is less than or equal to zero , then return 1 ; Otherwise return 0 ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean subarrayPossible ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( sum <= 0 ) return true ; } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 5 , - 12 , 7 , - 10 , 20 , 30 , - 10 , 50 , 60 } ; int size = arr . length ; if ( subarrayPossible ( arr , size ) ) System . out . print ( " Yes " + "
"); else System . out . print ( " No " + "
"); } }


"Check if a subarray of size K exists whose elements form a number divisible by 3 | Java implementation of the above approach ; Function to find the K size subarray ; Check if the first K elements forms a number which is divisible by 3 ; Using Sliding window technique ; Calculate sum of next K size subarray ; Check if sum is divisible by 3 ; Update the indices of the subarray ; If no such subarray is found ; Print the subarray ; Driver code ; Given array and K ; Function call"
How can the above be solved in Java?

import java . util . * ; import java . awt . Point ; class GFG { public static void findSubArray ( Vector < Integer > arr , int k ) { Point ans = new Point ( 0 , 0 ) ; int i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr . get ( i ) ; } int found = 0 ; if ( sum % 3 == 0 ) { ans = new Point ( 0 , i - 1 ) ; found = 1 ; } for ( int j = i ; j < arr . size ( ) ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr . get ( j ) - arr . get ( j - k ) ; if ( sum % 3 == 0 ) { ans = new Point ( j - k + 1 , j ) ; found = 1 ; } } if ( found == 0 ) ans = new Point ( - 1 , 0 ) ; if ( ans . x == - 1 ) { System . out . print ( - 1 ) ; } else { for ( i = ans . x ; i <= ans . y ; i ++ ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 84 ) ; arr . add ( 23 ) ; arr . add ( 45 ) ; arr . add ( 12 ) ; arr . add ( 56 ) ; arr . add ( 82 ) ; int K = 3 ; findSubArray ( arr , K ) ; } }


"Check if a subsequence of length K with odd sum exists | Java program to check if a subsequence of length K with odd sum exists in the given array ; Function to check if any required subsequence exists or not ; Store count of odd and even elements in the array ; Calculate the count of odd and even elements ; If no odd elements exists or no even elements exists when K is even ; Subsequence is not possible ; Possible otherwise ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }


"Check if a substring can be Palindromic by replacing K characters for Q queries | Java program for the above approach ; Function to find whether String can be made palindromic or not for each queries ; To store the count of ith character of subString str [ 0. . . i ] ; Current character ; Update dp [ ] [ ] on the basis recurrence relation ; For each queries ; To store the count of distinct character ; Find occurrence of i + ' a ' ; Half the distinct Count ; If half the distinct count is less than equals to K then palindromic String can be made ; Driver Code ; Given a String str ; Given Queries ; Function call"
How can the above be solved in Java?

 class GFG { static void canMakePaliQueries ( String str , int [ ] [ ] Q ) { int n = str . length ( ) ; int [ ] [ ] dp = new int [ 26 ] [ n ] ; for ( int i = 0 ; i < 26 ; i ++ ) { char currentChar = ( char ) ( i + ' a ' ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) { dp [ i ] [ j ] = ( str . charAt ( j ) == currentChar ) ? 1 : 0 ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + ( ( str . charAt ( j ) == currentChar ) ? 1 : 0 ) ; } } } for ( int [ ] query : Q ) { int left = query [ 0 ] ; int right = query [ 1 ] ; int k = query [ 2 ] ; int unMatchedCount = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int occurrence = dp [ i ] [ right ] - dp [ i ] [ left ] + ( str . charAt ( left ) == ( i + ' a ' ) ? 1 : 0 ) ; if ( occurrence % 2 == 1 ) unMatchedCount ++ ; } int ans = unMatchedCount / 2 ; if ( ans <= k ) { System . out . print ( "YES
"); } else { System . out . print ( "NO
"); } } } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; int [ ] [ ] Q = { { 1 , 5 , 3 } , { 5 , 7 , 0 } , { 8 , 11 , 3 } , { 3 , 10 , 5 } , { 0 , 9 , 5 } } ; canMakePaliQueries ( str , Q ) ; } }