"Check if given number contains only “ 01 ” and “ 10 ” as substring in its binary representation | Java Program to implement the above approach ; Function to generate all numbers having "01" and "10" as a substring ; Insert 2 and 5 ; Iterate till x is 10 ^ 15 ; Multiply x by 2 ; Update x as x * 2 + 1 ; Function to check if binary representation of N contains only "01" and "10" as substring ; Function Call to generate all such numbers ; Check if a number N exists in Ans [ ] or not ; If the number exists ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int N = 200000 ; static long Ans [ ] = new long [ 200000 ] ; static int index = 0 ; static void populateNumber ( ) { Ans [ index ++ ] = ( 2 ) ; Ans [ index ++ ] = ( 5 ) ; long x = 5 ; long inf = 1000000000001L ; while ( x < inf ) { x *= 2 ; Ans [ index ++ ] = ( x ) ; x = x * 2 + 1 ; Ans [ index ++ ] = ( x ) ; } } static void checkString ( int N ) { populateNumber ( ) ; for ( int i = 0 ; i < index ; i ++ ) { if ( Ans [ i ] == N ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { N = 5 ; checkString ( N ) ; } }


"Check if given number is a power of d where d is a power of 2 | Java program to find if a number is power of d where d is power of 2. ; Function to count the number of ways to paint N * 3 grid based on given conditions ; Check if there is only one bit set in n ; count 0 bits before set bit ; If count is a multiple of log2 ( d ) then return true else false ; If there are more than 1 bit set then n is not a power of 4 ; Driver Code"
How can the above be solved in Java?

class GFG { static int Log2n ( int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } static boolean isPowerOfd ( int n , int d ) { int count = 0 ; if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % ( Log2n ( d ) ) == 0 ) ; } return false ; } public static void main ( String [ ] args ) { int n = 64 , d = 8 ; if ( isPowerOfd ( n , d ) ) System . out . println ( n + " ▁ is ▁ a ▁ power ▁ of ▁ " + d ) ; else System . out . println ( n + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ " + d ) ; } }


"Check if given number is perfect square | Java program for the above approach ; If ceil and floor are equal the number is a perfect square ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void checkperfectsquare ( int n ) { if ( Math . ceil ( ( double ) Math . sqrt ( n ) ) == Math . floor ( ( double ) Math . sqrt ( n ) ) ) { System . out . print ( " perfect ▁ square " ) ; } else { System . out . print ( " not ▁ a ▁ perfect ▁ square " ) ; } } public static void main ( String [ ] args ) { int n = 49 ; checkperfectsquare ( n ) ; } }


"Check if given number is perfect square | Java program to find if x is a perfect square . ; Find floating point value of square root of x . ; if product of square root is equal , then return T / F ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPerfectSquare ( int x ) { if ( x >= 0 ) { int sr = ( int ) Math . sqrt ( x ) ; return ( ( sr * sr ) == x ) ; } return false ; } public static void main ( String [ ] args ) { int x = 2502 ; if ( isPerfectSquare ( x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if given permutation of 1 to N can be counted in clockwise or anticlockwise direction | Java program to check clockwise or counterclockwise order in an array ; Comparing the first and last value of array ; If the Count is greater than 1 then it can 't be  represented in required order ; Driver code"
How can the above be solved in Java?

class GFG { static boolean check_order ( int [ ] arr ) { int cnt = 0 ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { if ( Math . abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . abs ( arr [ 0 ] - arr [ arr . length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 1 } ; if ( check_order ( arr ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }


"Check if given point lies in range of any of the given towers | Java program to implement the above approach ; Function to check if the point ( X , Y ) exists in the towers network - range or not ; Traverse the array arr [ ] ; Stores distance of the point ( X , Y ) from i - th tower ; If dist lies within the range of the i - th tower ; If the point ( X , Y ) does not lie in the range of any of the towers ; Driver Code ; If point ( X , Y ) lies in the range of any of the towers ; Otherwise"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean checkPointRange ( int arr [ ] [ ] , int X , int Y , int N ) { for ( int i = 0 ; i < N ; i ++ ) { double dist = Math . sqrt ( ( arr [ i ] [ 0 ] - X ) * ( arr [ i ] [ 0 ] - X ) + ( arr [ i ] [ 1 ] - Y ) * ( arr [ i ] [ 1 ] - Y ) ) ; if ( dist <= arr [ i ] [ 2 ] ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 , 3 } , { 10 , 10 , 3 } , { 15 , 15 , 15 } } ; int X = 5 , Y = 5 ; int N = arr . length ; if ( checkPointRange ( arr , X , Y , N ) ) { System . out . print ( " True " ) ; } else { System . out . print ( " False " ) ; } } }


"Check if given polygon is a convex polygon or not | Java program to implement the above approach ; Utility function to find cross product of two vectors ; Stores coefficient of X direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 1 ] A [ 0 ] ; Stores coefficient of X direction of vector A [ 2 ] A [ 0 ] ; Stores coefficient of Y direction of vector A [ 2 ] A [ 0 ] ; Return cross product ; Function to check if the polygon is convex polygon or not ; Stores count of edges in polygon ; Stores direction of cross product of previous traversed edges ; Stores direction of cross product of current traversed edges ; Traverse the array ; Stores three adjacent edges of the polygon ; Update curr ; If curr is not equal to 0 ; If direction of cross product of all adjacent edges are not same ; Update curr ; Driver code"
How can the above be solved in Java?

class GFG { static int CrossProduct ( int A [ ] [ ] ) { int X1 = ( A [ 1 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y1 = ( A [ 1 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; int X2 = ( A [ 2 ] [ 0 ] - A [ 0 ] [ 0 ] ) ; int Y2 = ( A [ 2 ] [ 1 ] - A [ 0 ] [ 1 ] ) ; return ( X1 * Y2 - Y1 * X2 ) ; } static boolean isConvex ( int points [ ] [ ] ) { int N = points . length ; int prev = 0 ; int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp [ ] [ ] = { points [ i ] , points [ ( i + 1 ) % N ] , points [ ( i + 2 ) % N ] } ; curr = CrossProduct ( temp ) ; if ( curr != 0 ) { if ( curr * prev < 0 ) { return false ; } else { prev = curr ; } } } return true ; } public static void main ( String [ ] args ) { int points [ ] [ ] = { { 0 , 0 } , { 0 , 1 } , { 1 , 1 } , { 1 , 0 } } ; if ( isConvex ( points ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if given sorted sub | Java program to find if given array exists as a subsequece in BST ; A binary Tree node ; structure of int class ; A utility function to create a new BST node with key as given num ; A utility function to insert a given key to BST ; function to check if given sorted sub - sequence exist in BST index - . iterator for given sorted sub - sequence seq [ ] - . given sorted sub - sequence ; We traverse left subtree first in Inorder ; If current node matches with se [ index ] then move forward in sub - sequence ; We traverse left subtree in the end in Inorder ; A wrapper over seqExistUtil . It returns true if seq [ 0. . n - 1 ] exists in tree . ; Initialize index in seq [ ] ; Do an inorder traversal and find if all elements of seq [ ] were present ; index would become n if all elements of seq [ ] were present ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; } static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void seqExistUtil ( Node ptr , int seq [ ] , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static boolean seqExist ( Node root , int seq [ ] , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int seq [ ] = { 4 , 6 , 8 , 14 } ; int n = seq . length ; if ( seqExist ( root , seq , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if given string can be formed by two other strings or their permutations | Java implementation of the approach ; Function to sort the given string using counting sort ; Array to store the count of each character ; Insert characters in the string in increasing order ; Function that returns true if str can be generated from any permutation of the two strings selected from the given vector ; Sort the given string ; Select two strings at a time from given vector ; Get the concatenated string ; Sort the resultant string ; If the resultant string is equal to the given string str ; No valid pair found ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . valueOf ( s ) ; } static boolean isPossible ( Vector < String > v , String str ) { str = countingsort ( str . toCharArray ( ) ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { String temp = v . get ( i ) + v . get ( j ) ; temp = countingsort ( temp . toCharArray ( ) ) ; if ( temp . equals ( str ) ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; Vector < String > v = new Vector < String > ( Arrays . asList ( arr ) ) ; if ( isPossible ( v , str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if given string can be split into four distinct strings | Java program to check if we can break a string into four distinct strings . ; Return true if both strings are equal ; Return if the given string can be split or not . ; Brute Force ; Making 4 string from the given string ; Checking if they are distinct or not . ; Driver code"
How can the above be solved in Java?

class GFG { public static boolean strcheck ( String s1 , String s2 ) { if ( s1 . compareTo ( s2 ) != 0 ) return false ; return true ; } public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { String s1 = " " , s2 = " " , s3 = " " , s4 = " " ; try { s1 = s . substring ( 0 , i ) ; s2 = s . substring ( i , j - i ) ; s3 = s . substring ( j , k - j ) ; s4 = s . substring ( k , s . length ( ) - k ) ; } catch ( StringIndexOutOfBoundsException e ) { } if ( strcheck ( s1 , s2 ) && strcheck ( s1 , s3 ) && strcheck ( s1 , s4 ) && strcheck ( s2 , s3 ) && strcheck ( s2 , s4 ) && strcheck ( s3 , s4 ) ) return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " aaabb " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if given string contains all the digits | Java implementation of the approach ; Function that returns true if ch is a digit ; Function that returns true if str contains all the digits from 0 to 9 ; To mark the present digits ; For every character of the String ; If the current character is a digit ; Mark the current digit as present ; For every digit from 0 to 9 ; If the current digit is not present in str ; Driver code"
How can the above be solved in Java?

class GFG { static int MAX = 10 ; static boolean isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static boolean allDigits ( String str , int len ) { boolean [ ] present = new boolean [ MAX ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( isDigit ( str . charAt ( i ) ) ) { int digit = str . charAt ( i ) - '0' ; present [ digit ] = true ; } } for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " Geeks12345for69708" ; int len = str . length ( ) ; if ( allDigits ( str , len ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if given string satisfies the following conditions | Java program for the above approach ; Function to check if given string satisfies the given conditions ; Dimensions ; Left diagonal ; Right diagonal ; Conditions not satisfied ; Print Yes ; Driver Code ; Given String ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static void isValid ( String s ) { int n = ( int ) Math . sqrt ( s . length ( ) ) ; char check = s . charAt ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s . charAt ( ( n * x ) + y ) != check || s . charAt ( ( n * y ) + x ) != check ) { System . out . print ( " No " ) ; return ; } x -- ; y ++ ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { String str = " abacdaeaafaghaia " ; isValid ( str ) ; } }


"Check if given strings can be made same by swapping two characters of same or different strings | Java program to implement the above approach ; Function to check if all strings can be made equal by swapping any pair of characters from the same or different strings ; Stores length of string ; Store frequency of each distinct character of the strings ; Traverse the array ; Iterate over the characters ; Update frequency of arr [ i ] . charAt ( j ) ; Traverse the array cntFreq [ ] ; If cntFreq [ i ] is divisible by N or not ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static boolean isEqualStrings ( String [ ] arr , int N ) { int M = arr [ 0 ] . length ( ) ; int [ ] cntFreq = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ i ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { cntFreq [ arr [ i ] . charAt ( j ) - ' a ' ] += 1 ; } } for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % N != 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String [ ] arr = { " aab " , " bbc " , " cca " } ; int N = arr . length ; if ( isEqualStrings ( arr , N ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if given two straight lines are identical or not | Java program to check if given two straight lines are identical or not ; Function to check if they are identical ; Driver Code"
How can the above be solved in Java?

class GFG { static void idstrt ( double a1 , double b1 , double c1 , double a2 , double b2 , double c2 ) { if ( ( a1 / a2 == b1 / b2 ) && ( a1 / a2 == c1 / c2 ) && ( b1 / b2 == c1 / c2 ) ) System . out . println ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ identical " ) ; else System . out . println ( " The ▁ given ▁ straight " + " ▁ lines ▁ are ▁ not ▁ identical " ) ; } public static void main ( String [ ] args ) { double a1 = - 2 , b1 = 4 , c1 = 3 , a2 = - 6 , b2 = 12 , c2 = 9 ; idstrt ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }


"Check if it is possible to construct an Array of size N having sum as S and XOR value as X | Java program for the above approach ; Function to find if any sequence is possible or not . ; Since , S is greater than equal to X , and either both are odd or even There always exists a sequence ; Only one case possible is S == X or NOT ; ; Considering the above conditions true , check if XOR of S ^ ( S - X ) is X or not ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void findIfPossible ( int N , int S , int X ) { if ( ( S >= X ) && ( S % 2 == X % 2 ) ) { if ( N >= 3 ) { System . out . println ( " Yes " ) ; } if ( N == 1 ) { if ( S == X ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } if ( N == 2 ) { int C = ( S - X ) / 2 ; int A = C ; int B = C ; A = A + X ; if ( ( ( A ^ B ) == X ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 3 , S = 10 , X = 4 ; findIfPossible ( N , S , X ) ; } }


"Check if it is possible to create a palindrome string from given N | Java implementation of the above approach ; Function to check if a string is palindrome or not ; String that stores characters of s in reverse order ; Length of the string s ; String used to form substring using N ; Variable to store sum of digits of N ; Forming the substring by traversing N ; Appending the substr to str till it 's length becomes equal to sum ; Trimming the string str so that it 's length becomes equal to sum ; Driver code ; Calling function isPalindrome to check if str is Palindrome or not"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; public class GFG { static boolean isPalindrome ( String s ) { String s1 = " " ; int N = s . length ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s . charAt ( i ) ; if ( s . equals ( s1 ) ) return true ; return false ; } static boolean createString ( int N ) { String str = " " ; String s = " " + N ; String letters = " abcdefghij " ; int sum = 0 ; String substr = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; substr += letters . charAt ( digit ) ; sum += digit ; } while ( str . length ( ) <= sum ) { str += substr ; } str = str . substring ( 0 , sum ) ; return isPalindrome ( str ) ; } public static void main ( String args [ ] ) { int N = 61 ; boolean flag = createString ( N ) ; if ( flag ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if it is possible to create a polygon with a given angle |  ; Function to check whether it is possible to make a regular polygon with a given angle . ; N denotes the number of sides of polygons possible ; Driver code ; function to print the required answer"
How can the above be solved in Java?

class GFG { static void makePolygon ( double a ) { double n = 360 / ( 180 - a ) ; if ( n == ( int ) n ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { double a = 90 ; makePolygon ( a ) ; } }


"Check if it is possible to create a polygon with given n sides | Java implementation of the approach ; Function that returns true if it is possible to form a polygon with the given sides ; Sum stores the sum of all the sides and maxS stores the length of the largest side ; If the length of the largest side is less than the sum of the other remaining sides ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPossible ( int a [ ] , int n ) { int sum = 0 , maxS = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; maxS = Math . max ( a [ i ] , maxS ) ; } if ( ( sum - maxS ) > maxS ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if it is possible to draw a straight line with the given direction cosines | Java implementation of the approach ; Function that returns true if a straight line is possible ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPossible ( float x , float y , float z ) { float a = x * x + y * y + z * z ; if ( Math . ceil ( a ) == 1 && Math . floor ( a ) == 1 ) return true ; return false ; } public static void main ( String args [ ] ) { float l = 0.70710678f , m = 0.5f , n = 0.5f ; if ( isPossible ( l , m , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to form string B from A under the given constraints | Java implementation of the approach ; Function that returns true if it is possible to form B from A satisfying the given conditions ; List to store the frequency of characters in A ; Vector to store the count of characters used from a particular group of characters ; Store the frequency of the characters ; If a character in B is not present in A ; If count of characters used from a particular group of characters exceeds m ; Update low to the starting index of the next group ; If count of characters used from a particular group of characters has not exceeded m ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean isPossible ( String A , String B , int b , int m ) { List < List < Integer > > S = new ArrayList < List < Integer > > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) S . add ( new ArrayList < Integer > ( ) ) ; int [ ] box = new int [ A . length ( ) ] ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { S . get ( A . charAt ( i ) - ' a ' ) . add ( i ) ; } int low = 0 ; for ( int i = 0 ; i < B . length ( ) ; i ++ ) { List < Integer > indexes = S . get ( B . charAt ( i ) - ' a ' ) ; int it = lower_bound ( indexes , low ) ; if ( it == indexes . size ( ) ) return false ; int count = indexes . get ( it ) / b ; box [ count ] = box [ count ] + 1 ; if ( box [ count ] >= m ) { count ++ ; low = ( count ) * b ; } else low = indexes . get ( it ) + 1 ; } return true ; } static int lower_bound ( List < Integer > indexes , int k ) { int low = 0 , high = indexes . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( indexes . get ( mid ) < k ) low = mid + 1 ; else high = mid ; } return ( indexes . get ( low ) < k ) ? low + 1 : low ; } public static void main ( String [ ] args ) { String A = " abcbbcdefxyz " ; String B = " acdxyz " ; int b = 5 ; int m = 2 ; if ( isPossible ( A , B , b , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to get back to 12 '0 clock only by adding or subtracting given seconds | Java program to check if we come back to zero or not in a clock ; Function to check all combinations ; Generate all power sets ; Check for every combination ; Store sum for all combinations ; Check if jth bit in the counter is set If set then print jth element from set ; sum += a [ j ] ; if set then consider as ' + ' ; sum -= a [ j ] ; else consider as ' - ' ; If we can get back to 0 ; Driver code"
How can the above be solved in Java?

import java . lang . Math ; class GfG { static boolean checkCombinations ( int a [ ] , int n ) { int pow_set_size = ( int ) Math . pow ( 2 , n ) ; int counter , j ; for ( counter = 0 ; counter < pow_set_size ; counter ++ ) { int sum = 0 ; for ( j = 0 ; j < n ; j ++ ) { if ( ( counter & ( 1 << j ) ) != 0 ) else } if ( sum % ( 24 * 60 ) == 0 ) return true ; } return false ; } public static void main ( String [ ] args ) { int a [ ] = { 60 , 60 , 120 } ; int n = a . length ; if ( checkCombinations ( a , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if it is possible to get given sum by taking one element from each row | Java implementation to find whether is it possible to make sum equal to K ; Function that prints whether is it possible to make sum equal to K ; Base case ; Condition if we can make sum equal to current column by using above rows ; Iterate through current column and check whether we can make sum less than or equal to k ; Printing whether is it possible or not ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void PossibleSum ( int n , int m , int [ ] [ ] v , int k ) { int [ ] [ ] dp = new int [ n + 1 ] [ k + 1 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { if ( dp [ i ] [ j ] == 1 ) { for ( int d = 0 ; d < m ; d ++ ) { if ( ( j + v [ i ] [ d ] ) <= k ) { dp [ i + 1 ] [ j + v [ i ] [ d ] ] = 1 ; } } } } } if ( dp [ n ] [ k ] == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 2 , M = 10 , K = 5 ; int [ ] [ ] arr = new int [ ] [ ] { { 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 } , { 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 } } ; PossibleSum ( N , M , arr , K ) ; } }


"Check if it is possible to make array increasing or decreasing by rotating the array | Java implementation of the approach ; Function that returns true if the array can be made increasing or decreasing after rotating it in any direction ; If size of the array is less than 3 ; Check if the array is already decreasing ; If the array is already decreasing ; Check if the array is already increasing ; If the array is already increasing ; Find the indices of the minimum && the maximum value ; Check if we can make array increasing ; If the array is increasing upto max index && minimum element is right to maximum ; Check if array increasing again or not ; Check if we can make array decreasing ; If the array is decreasing upto min index && minimum element is left to maximum ; Check if array decreasing again or not ; If it is not possible to make the array increasing or decreasing ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPossible ( int a [ ] , int n ) { if ( n <= 2 ) return true ; int flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] > a [ i + 1 ] && a [ i + 1 ] > a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; flag = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( ! ( a [ i ] < a [ i + 1 ] && a [ i + 1 ] < a [ i + 2 ] ) ) { flag = 1 ; break ; } } if ( flag == 0 ) return true ; int val1 = Integer . MAX_VALUE , mini = - 1 , val2 = Integer . MIN_VALUE , maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < val1 ) { mini = i ; val1 = a [ i ] ; } if ( a [ i ] > val2 ) { maxi = i ; val2 = a [ i ] ; } } flag = 1 ; for ( int i = 0 ; i < maxi ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi + 1 == mini ) { flag = 1 ; for ( int i = mini ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } flag = 1 ; for ( int i = 0 ; i < mini ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 && maxi - 1 == mini ) { flag = 1 ; for ( int i = maxi ; i < n - 1 ; i ++ ) { if ( a [ i ] < a [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) return true ; } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 4 , 5 , 6 , 2 , 3 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to make two matrices strictly increasing by swapping corresponding values only | Java implementation of the above approach ; Function to check whether the matrices can be made strictly increasing with the given operation ; Swap only when a [ i ] [ j ] > b [ i ] [ j ] ; Check if rows are strictly increasing ; Check if columns are strictly increasing ; Driver code"
How can the above be solved in Java?

using System ; using System . Collections ; class GfG { class GFG { public static String Check ( int a [ ] [ ] , int b [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] > b [ i ] [ j ] ) { int temp = a [ i ] [ j ] ; a [ i ] [ j ] = b [ i ] [ j ] ; b [ i ] [ j ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m - 1 ; j ++ ) if ( a [ i ] [ j ] >= a [ i ] [ j + 1 ] b [ i ] [ j ] >= b [ i ] [ j + 1 ] ) return " No " ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( a [ i ] [ j ] >= a [ i + 1 ] [ j ] b [ i ] [ j ] >= b [ i + 1 ] [ j ] ) return " No " ; return " Yes " ; } public static void main ( String [ ] args ) { int n = 2 , m = 2 ; int a [ ] [ ] = { { 2 , 10 } , { 11 , 5 } } ; int b [ ] [ ] = { { 9 , 4 } , { 3 , 12 } } ; System . out . print ( Check ( a , b , n , m ) ) ; } }


"Check if it is possible to move from ( 0 , 0 ) to ( X , Y ) in exactly K steps | Java implementation of the approach ; Function that returns true if it is possible to move from ( 0 , 0 ) to ( x , y ) in exactly k moves ; Minimum moves required ; If possible ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isPossible ( int x , int y , int k ) { int minMoves = Math . abs ( x ) + Math . abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to move from ( 0 , 0 ) to ( x , y ) in N steps | Java program to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Function to check whether it is possible or not to move from ( 0 , 0 ) to ( x , y ) in exactly n steps ; Driver code"
How can the above be solved in Java?

import java . io . * ; public class GFG { static boolean Arrive ( int a , int b , int n ) { if ( n >= Math . abs ( a ) + Math . abs ( b ) && ( n - ( Math . abs ( a ) + Math . abs ( b ) ) ) % 2 == 0 ) return true ; return false ; } int main ( ) { return 0 ; } public static void main ( String [ ] args ) { int a = 5 , b = 5 , n = 11 ; if ( Arrive ( a , b , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to move from ( a , 0 ) to ( b , 0 ) with given jumps | Java program to move form ( a , 0 ) to ( b , 0 ) with given jumps ; Function to check if it is possible ; Driver code ; function call"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean Move ( int a , int x , int b ) { if ( ( ( ( b - a ) % x == 0 ) || ( ( b - a - 1 ) % x == 0 ) && a + 1 != b ) && b >= a ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 3 , x = 2 , b = 7 ; if ( Move ( a , x , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to obtain a Balanced Parenthesis by shifting brackets to either end at most K times | Java program for the above approach ; Function to check if a valid parenthesis can be obtained by moving characters to either end at most K number of times ; Base Case 1 ; Count of ' ( ' and ' ) ' ; Base Case 2 ; Store the count of moves required to make a valid parenthesis ; Traverse the string ; Increment cnt if opening bracket has occurred ; Otherwise , decrement cnt by 1 ; Decrement cnt by 1 ; If cnt is negative ; Update the cnt ; Increment the ans ; If ans is at most K , then print Yes . Otherwise print No ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumMoves ( String s , int n , int k ) { if ( n % 2 == 1 ) { System . out . println ( " No " ) ; return ; } int countOpen = 0 , countClose = 0 ; for ( char ch : s . toCharArray ( ) ) if ( ch == ' ( ' ) countOpen ++ ; else if ( ch == ' ) ' ) countClose ++ ; if ( countOpen != countClose ) { System . out . println ( " No " ) ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s . charAt ( i ) == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . length ( ) , K ) ; } }


"Check if it is possible to perform the given Grid Division | Java implementation of the approach ; Function that returns true if it is possible to divide the grid satisfying the given conditions ; To store the sum of all the cells of the given parts ; If the sum is equal to the total number of cells in the given grid ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPossible ( int arr [ ] , int p , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 , m = 4 ; int arr [ ] = { 6 , 3 , 2 , 1 } ; int p = arr . length ; if ( isPossible ( arr , p , n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Java program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Stores the number of steps performed to reach ( X , Y ) ; Value of X in base 3 ; Value of Y in base 3 ; Check if any has value 2 ; If both have value 1 ; If both have value 0 ; Otherwise , return true ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean canReach ( int X , int Y ) { int steps = 0 ; while ( X != 0 Y != 0 ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if it is possible to reach ( X , Y ) from origin such that in each ith move increment x or y coordinate with 3 ^ i | Java program for the above approach ; Function to find whether ( 0 , 0 ) can be reached from ( X , Y ) by decrementing 3 ^ i at each ith step ; Termination Condition ; Otherwise , recursively call by decrementing 3 ^ i at each step ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) Math . pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) Math . pow ( 3 , steps ) , steps + 1 ) ) ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { System . out . print ( " YES " + "
"); } else System . out . print ( " NO " + "
"); } }


"Check if it is possible to reach ( x , y ) from origin in exactly Z steps using only plus movements | Java program for the above approach ; Function to check if it is possible to reach ( x , y ) from origin in exactly z steps ; Condition if we can 't reach in Z steps ; Driver Code ; Destination point coordinate ; Number of steps allowed ; Function Call"
How can the above be solved in Java?

 class GFG { static void possibleToReach ( int x , int y , int z ) { if ( z < Math . abs ( x ) + Math . abs ( y ) || ( z - Math . abs ( x ) - Math . abs ( y ) ) % 2 == 1 ) { System . out . print ( " Not ▁ Possible " + "
"); } else System . out . print ( " Possible " + "
"); } public static void main ( String [ ] args ) { int x = 5 , y = 5 ; int z = 11 ; possibleToReach ( x , y , z ) ; } }


"Check if it is possible to reach M from 0 by given paths | Java program for the above approach ; Function to check if it is possible to reach M from 0 ; Stores the farther point that can reach from 1 point ; Stores the farthest point it can go for each index i ; Initialize rightMost [ i ] with 0 ; Traverse the array ; Update the rightMost position reached from a1 ; Find the farthest point it can reach from i ; If point < can be reached ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void canReach0toM ( int [ ] [ ] a , int n , int m ) { int [ ] rightMost = new int [ m + 1 ] ; int [ ] dp = new int [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i ] [ 0 ] ; int b1 = a [ i ] [ 1 ] ; rightMost [ a1 ] = Math . max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = Math . min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = arr . length ; canReach0toM ( arr , N , M ) ; } }


"Check if it is possible to reach a number by making jumps of two given length | Java implementation of the approach ; Function to perform BFS traversal to find minimum number of step needed to reach x from K ; Calculate GCD of d1 and d2 ; If position is not reachable return - 1 ; Queue for BFS ; Hash Table for marking visited positions ; we need 0 steps to reach K ; Mark starting position as visited ; stp is the number of steps to reach position s ; if position not visited add to queue and mark visited ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int minStepsNeeded ( int k , int d1 , int d2 , int x ) { int gcd = __gcd ( d1 , d2 ) ; if ( ( k - x ) % gcd != 0 ) return - 1 ; Queue < pair > q = new LinkedList < > ( ) ; HashSet < Integer > visited = new HashSet < > ( ) ; q . add ( new pair ( k , 0 ) ) ; visited . add ( k ) ; while ( ! q . isEmpty ( ) ) { int s = q . peek ( ) . first ; int stp = q . peek ( ) . second ; if ( s == x ) return stp ; q . remove ( ) ; if ( ! visited . contains ( s + d1 ) ) { q . add ( new pair ( s + d1 , stp + 1 ) ) ; visited . add ( s + d1 ) ; } if ( visited . contains ( s + d2 ) ) { q . add ( new pair ( s + d2 , stp + 1 ) ) ; visited . add ( s + d2 ) ; } if ( ! visited . contains ( s - d1 ) ) { q . add ( new pair ( s - d1 , stp + 1 ) ) ; visited . add ( s - d1 ) ; } if ( ! visited . contains ( s - d2 ) ) { q . add ( new pair ( s - d2 , stp + 1 ) ) ; visited . add ( s - d2 ) ; } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int k = 10 , d1 = 4 , d2 = 6 , x = 8 ; System . out . println ( minStepsNeeded ( k , d1 , d2 , x ) ) ; } }


"Check if it is possible to reach any point on the circumference of a given circle from origin | Java program for the above approach ; Function to check if it is possible to reach any point on circumference of the given circle from ( 0 , 0 ) ; Stores the count of ' L ' , ' R ' ; Stores the count of ' U ' , ' D ' ; Traverse the string S ; Update the count of L ; Update the count of R ; Update the count of U ; Update the count of D ; Condition 1 for reaching the circumference ; Store the the value of ( i * i ) in the Map ; Check if ( r_square - i * i ) already present in HashMap ; If it is possible to reach the point ( mp [ r_square - i * i ] , i ) ; If it is possible to reach the point ( i , mp [ r_square - i * i ] ) ; If it is impossible to reach ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String isPossible ( String S , int R , int N ) { int cntl = 0 , cntr = 0 ; int cntu = 0 , cntd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' L ' ) cntl ++ ; else if ( S . charAt ( i ) == ' R ' ) cntr ++ ; else if ( S . charAt ( i ) == ' U ' ) cntu ++ ; else cntd ++ ; } if ( Math . max ( Math . max ( cntl , cntr ) , Math . max ( cntu , cntd ) ) >= R ) return " Yes " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int r_square = R * R ; for ( int i = 1 ; i * i <= r_square ; i ++ ) { mp . put ( i * i , i ) ; if ( mp . containsKey ( r_square - i * i ) ) { if ( Math . max ( cntl , cntr ) >= mp . get ( r_square - i * i ) && Math . max ( cntu , cntd ) >= i ) return " Yes " ; if ( Math . max ( cntl , cntr ) >= i && Math . max ( cntu , cntd ) >= mp . get ( r_square - i * i ) ) return " Yes " ; } } return " No " ; } public static void main ( String [ ] args ) { String S = " RDLLDDLDU " ; int R = 5 ; int N = S . length ( ) ; System . out . println ( isPossible ( S , R , N ) ) ; } }


"Check if it is possible to reach destination in even number of steps in an Infinite Matrix | Java program for the above approach ; Function to check destination can be reached from source in even number of steps ; Coordinates differences ; Minimum number of steps required ; Minsteps is even ; Minsteps is odd ; Driver code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static void IsEvenPath ( int Source [ ] , int Destination [ ] ) { int x_dif = Math . abs ( Source [ 0 ] - Destination [ 0 ] ) ; int y_dif = Math . abs ( Source [ 1 ] - Destination [ 1 ] ) ; int minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int Source [ ] = { 2 , 1 } ; int Destination [ ] = { 1 , 4 } ; IsEvenPath ( Source , Destination ) ; } }


"Check if it is possible to reach the point ( X , Y ) using distances given in an array | C # program for the above approach ; Function to check if the point ( X , Y ) is reachable from ( 0 , 0 ) or not ; Find the Euclidian Distance ; Calculate the maximum distance ; Case 1. ; Case 2. ; Otherwise , check for the polygon condition for each side ; Otherwise , print Yes ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int isPossibleToReach ( int [ ] A , int N , int X , int Y ) { double distance = Math . sqrt ( ( X * X + Y * Y ) ) ; double mx = 0 ; for ( int i = 0 ; i < N ; i ++ ) { mx += ( A [ i ] ) ; } if ( mx < distance ) { System . out . print ( " NO " ) ; return 0 ; } if ( ( mx - distance ) < 0.000001 ) { System . out . print ( " YES " ) ; return 0 ; } for ( int i = 0 ; i < N ; i ++ ) { if ( distance + mx < 2 * A [ i ] ) { System . out . print ( " No " ) ; return 0 ; } } System . out . print ( " Yes " ) ; return 0 ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , 5 } ; int X = 5 , Y = 4 ; int N = A . length ; isPossibleToReach ( A , N , X , Y ) ; } }


"Check if it is possible to reach vector B by rotating vector A and adding vector C to it | Java program to Check if it is possible to reach vector B by Rotating vector A and adding vector C to it any number of times . ; function to check if vector B is possible from vector A ; if d = 0 , then you need to add nothing to vector A ; for all four quadrants ; Driver code ; initialize all three vector coordinates"
How can the above be solved in Java?

public class GFG { static boolean check ( long a , long b , long p , long q ) { long d = p * p + q * q ; if ( d == 0 ) return a == 0 && b == 0 ; else return ( a * p + b * q ) % d == 0 && ( b * p - a * q ) % d == 0 ; } static boolean check ( int a , int b , int x , int y , int p , int q ) { if ( check ( a - x , b - y , p , q ) || check ( a + x , b + y , p , q ) || check ( a - y , b + x , p , q ) || check ( a + y , b - x , p , q ) ) return true ; else return false ; } public static void main ( String args [ ] ) { int a = - 4 , b = - 2 ; int x = 0 , y = 0 ; int p = - 2 , q = - 1 ; if ( check ( a , b , x , y , p , q ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to rearrange rectangles in a non | Java implementation of above approach ; Function to check if it possible to form rectangles with heights as non - ascending ; set maximum ; replace the maximum with previous maximum ; replace the minimum with previous minimum ; print NO if the above two conditions fail at least once ; Driver code ; initialize the number of rectangles ; initialize n rectangles with length and breadth"
How can the above be solved in Java?

import java . io . * ; class GFG { static int rotateRec ( int n , int L [ ] , int B [ ] ) { int m = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . max ( L [ i ] , B [ i ] ) <= m ) m = Math . max ( L [ i ] , B [ i ] ) ; else if ( Math . min ( L [ i ] , B [ i ] ) <= m ) m = Math . min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } public static void main ( String [ ] args ) { int n = 3 ; int L [ ] = { 5 , 5 , 6 } ; int B [ ] = { 6 , 7 , 8 } ; if ( rotateRec ( n , L , B ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if it is possible to return to the starting position after moving in the given directions | Java implementation of above approach ; Main method ; int n = 0 ; Count of North int s = 0 ; Count of South int e = 0 ; Count of East int w = 0 ; Count of West"
How can the above be solved in Java?

public class GFG { public static void main ( String args [ ] ) { String st = " NNNWEWESSS " ; int len = st . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( st . charAt ( i ) == ' N ' ) n += 1 ; if ( st . charAt ( i ) == ' S ' ) s += 1 ; if ( st . charAt ( i ) == ' W ' ) w += 1 ; if ( st . charAt ( i ) == ' E ' ) e += 1 ; } if ( n == s && w == e ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if it is possible to serve customer queue with different notes | Java implementation of the approach ; Function that returns true is selling of the tickets is possible ; Nothing to return to the customer ; Check if 25 can be returned to customer . ; Try returning one 50 and one 25 ; Try returning three 25 ; If the loop did not break , all the tickets were sold ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean isSellingPossible ( int n , int a [ ] ) { int i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } public static void main ( String [ ] args ) { int a [ ] = { 25 , 25 , 50 , 100 } ; int n = a . length ; if ( isSellingPossible ( n , a ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if it is possible to sort an array with conditional swapping of adjacent allowed | Returns true if it is possible to sort else false / ; We need to do something only if previousl element is greater ; If difference is more than one , then not possible ; Driver function"
How can the above be solved in Java?

class Main { static boolean checkForSorting ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } else return false ; } } return true ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 3 , 2 } ; int n = arr . length ; if ( checkForSorting ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if it is possible to sort an array with conditional swapping of elements at distance K | Java implementation to check if it is possible to sort an array with conditional swapping of elements at distance K ; Function for finding if it possible to obtain sorted array or not ; Iterate over all elements until K ; Store elements as multiples of K ; Sort the elements ; Put elements in their required position ; Check if the array becomes sorted or not ; Driver code"
How can the above be solved in Java?

import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { public static boolean fun ( int [ ] arr , int n , int k ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . add ( arr [ j ] ) ; } Collections . sort ( v ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v . get ( x ) ; x ++ ; } v . clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = arr . length ; if ( fun ( arr , n , K ) ) { System . out . println ( " yes " ) ; } else { System . out . println ( " no " ) ; } } }


"Check if it is possible to survive on Island | Java program to find the minimum days on which you need to buy food from the shop so that you can survive the next S days ; function to find the minimum days ; If we can not buy at least a week supply of food during the first week OR We can not buy a day supply of food on the first day then we can 't survive. ; If we can survive then we can buy ceil ( A / N ) times where A is total units of food required . ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void survival ( int S , int N , int M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) M > N ) System . out . println ( " No " ) ; else { int days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; System . out . println ( " Yes ▁ " + days ) ; } } public static void main ( String [ ] args ) { int S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ; } }


"Check if it is possible to transform one string to another | Java program to check if a string can be converted to another string by performing operations ; function to check if a string can be converted to another string by performing following operations ; calculates length ; mark 1 st position as true ; traverse for all DPi , j ; if possible for to convert i characters of s1 to j characters of s2 ; if upper_case ( s1 [ i ] ) == s2 [ j ] is same ; if not upper then deletion is possible ; driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean check ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { for ( int j = 0 ; j <= s2 . length ( ) ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( j < s2 . length ( ) && ( Character . toUpperCase ( s1 . charAt ( i ) ) == s2 . charAt ( j ) ) ) dp [ i + 1 ] [ j + 1 ] = true ; if ( ! Character . isUpperCase ( s1 . charAt ( i ) ) ) dp [ i + 1 ] [ j ] = true ; } } } return ( dp [ n ] [ m ] ) ; } public static void main ( String args [ ] ) { String s1 = " daBcd " ; String s2 = " ABC " ; if ( check ( s1 , s2 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if leaf traversal of two Binary Trees is same ? | Java program to check if two Leaf Traversal of Two Binary Trees is same or not ; Binary Tree node ; checks if a given node is leaf or not . ; Returns true of leaf traversal of two trees is same , else false ; Create empty stacks . These stacks are going to be used for iterative traversals . ; Loop until either of two stacks is not empty ; If one of the stacks is empty means other stack has extra leaves so return false ; Push right and left children of temp1 . Note that right child is inserted before left ; same for tree2 ; If one is null and other is not , then return false ; If both are not null and data is not same return false ; If control reaches this point , all leaves are matched ; Driver code ; Let us create trees in above example 1"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; import java . io . * ; class Node { int data ; Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } public boolean isLeaf ( ) { return ( left == null && right == null ) ; } } class LeafOrderTraversal { public static boolean isSame ( Node root1 , Node root2 ) { Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { if ( s1 . empty ( ) || s2 . empty ( ) ) return false ; Node temp1 = s1 . pop ( ) ; while ( temp1 != null && ! temp1 . isLeaf ( ) ) { if ( temp1 . right != null ) s1 . push ( temp1 . right ) ; if ( temp1 . left != null ) s1 . push ( temp1 . left ) ; temp1 = s1 . pop ( ) ; } Node temp2 = s2 . pop ( ) ; while ( temp2 != null && ! temp2 . isLeaf ( ) ) { if ( temp2 . right != null ) s2 . push ( temp2 . right ) ; if ( temp2 . left != null ) s2 . push ( temp2 . left ) ; temp2 = s2 . pop ( ) ; } if ( temp1 == null && temp2 != null ) return false ; if ( temp1 != null && temp2 == null ) return false ; if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) return false ; } } return true ; } public static void main ( String [ ] args ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 2 ) ; root1 . right = new Node ( 3 ) ; root1 . left . left = new Node ( 4 ) ; root1 . right . left = new Node ( 6 ) ; root1 . right . right = new Node ( 7 ) ; Node root2 = new Node ( 0 ) ; root2 . left = new Node ( 1 ) ; root2 . right = new Node ( 5 ) ; root2 . left . right = new Node ( 4 ) ; root2 . right . left = new Node ( 6 ) ; root2 . right . right = new Node ( 7 ) ; if ( isSame ( root1 , root2 ) ) System . out . println ( " Same " ) ; else System . out . println ( " Not ▁ Same " ) ; } }


"Check if left and right shift of any string results into given string | Java program to check if left and right shift of any string results into the given string ; Function to check string exist or not as per above approach ; Check if any character at position i and i + 2 are not equal then string doesnot exist ; Driver Code"
How can the above be solved in Java?

class GFG { public static void check_string_exist ( String S ) { int size = S . length ( ) ; boolean check = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( ( i + 2 ) % size ) ) { check = false ; break ; } } if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " papa " ; check_string_exist ( S ) ; } }


"Check if matrix A can be converted to B by changing parity of corner elements of any submatrix | Java implementation of the above approach ; Boolean function that returns true or false ; Traverse for all elements ; If both are not equal ; Change the parity of all corner elements ; Check if A is equal to B ; Not equal ; Driver Code ; First binary matrix ; Second binary matrix"
How can the above be solved in Java?

class GFG { static final int N = 3 , M = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int b [ ] [ ] = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if matrix can be converted to another matrix by transposing square sub | Java implementation of the approach ; Function that returns true if matrix1 can be converted to matrix2 with the given operation ; Traverse all the diagonals starting at first column ; Traverse in diagonal ; Store the diagonal elements ; Move up ; Sort the elements ; Check if they are same ; Traverse all the diagonals starting at last row ; Traverse in the diagonal ; Store diagonal elements ; Sort all elements ; Check for same ; If every element matches ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static final int n = 3 ; static final int m = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int j = 0 ; j < v1 . size ( ) ; j ++ ) { if ( v1 . get ( j ) != v2 . get ( j ) ) { return false ; } } } for ( int j = 1 ; j < m ; j ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) { if ( v1 . get ( i ) != v2 . get ( i ) ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int b [ ] [ ] = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if mirror image of a number is same if displayed in seven segment display | Java Program to check if mirror image of a number is same if displayed in seven segment display ; Return " Yes " , if the mirror image of number is same as the given number Else return " No " ; Checking if the number contain only 0 , 1 , 8. ; Checking if the number is palindrome or not . ; If corresponding index is not equal . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static String checkEqual ( String S ) { for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) != '1' && S . charAt ( i ) != '0' && S . charAt ( i ) != '8' ) { return " No " ; } } int start = 0 , end = S . length ( ) - 1 ; while ( start < end ) { if ( S . charAt ( start ) != S . charAt ( end ) ) { return " No " ; } start ++ ; end -- ; } return " Yes " ; } public static void main ( String [ ] args ) { String S = "101" ; System . out . println ( checkEqual ( S ) ) ; } }


"Check if n is divisible by power of 2 without using arithmetic operators | JAVA Code for Check if n is divisible by power of 2 without using arithmetic operators ; function to check whether n is divisible by pow ( 2 , m ) ; if expression results to 0 , then n is divisible by pow ( 2 , m ) ; n is not divisible ; Driver program to test above function"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isDivBy2PowerM ( int n , int m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if number can be made prime by deleting a single digit | Java implementation to check if a number becomes prime by deleting any digit ; Function to check if N is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to delete character at index i from given String str ; Deletes character at position 4 ; Function to check if a number becomes prime by deleting any digit ; Converting the number to String ; length of String ; number should not be of single digit ; Loop to find all numbers after deleting a single digit ; Deleting ith character from the String ; converting String to int ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static String deleteIth ( String str , int i ) { str = str . substring ( 0 , i ) + str . substring ( i + 1 ) ; return str ; } static boolean isPrimePossible ( int N ) { String s = String . valueOf ( N ) ; int l = s . length ( ) ; if ( l < 2 ) return false ; for ( int i = 0 ; i < l ; i ++ ) { String str = deleteIth ( s , i ) ; int num = Integer . valueOf ( str ) ; if ( isPrime ( num ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int N = 610 ; if ( isPrimePossible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if number is palindrome or not in Octal | Java program to check if octal representation of a number is prime ; Function to Check no is in octal or not ; Function To check no is palindrome or not ; If number is already in octal , we traverse digits using repeated division with 10. Else we traverse digits using repeated division with 8 ; To store individual digits ; Traversing all digits ; checking if octal no is palindrome ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int MAX_DIGITS = 20 ; static int isOctal ( int n ) { while ( n > 0 ) { if ( ( n % 10 ) >= 8 ) return 0 ; else n = n / 10 ; } return 1 ; } static int isPalindrome ( int n ) { int divide = ( isOctal ( n ) == 0 ) ? 8 : 10 ; int octal [ ] = new int [ MAX_DIGITS ] ; int i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = n / divide ; } for ( int j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int n = 97 ; if ( isPalindrome ( n ) > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if number is palindrome or not in base B | Java program to implement the above approach ; Function to check if N in base B is palindrome or not ; Stores the reverse of N ; Stores the value of N ; Extract all the digits of N ; Generate its reverse ; Driver code"
How can the above be solved in Java?

class GFG { static boolean checkPalindromeB ( int N , int B ) { int rev = 0 ; int N1 = N ; while ( N1 > 0 ) { rev = rev * B + N1 % B ; N1 = N1 / B ; } return N == rev ; } public static void main ( String [ ] args ) { int N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if one of the numbers is one 's complement of the other | Java implementation to check if one of the two numbers is one 's complement of the other ; function to check if all the bits are set or not in the binary representation of ' n ' ; all bits are not set ; if true , then all bits are set ; else all bits are not set ; function to check if one of the two numbers is one 's complement  of the other ; Driver function"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; public class GfG { public static boolean areAllBitsSet ( long n ) { if ( n == 0 ) return false ; if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } public static boolean isOnesComplementOfOther ( long a , long b ) { return areAllBitsSet ( a ^ b ) ; } public static void main ( String argc [ ] ) { long a = 10 , b = 5 ; if ( isOnesComplementOfOther ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if one string can be converted to other using given operation | Java implementation of above approach ; Function that prints whether is it possible to make a String equal to T by performing given operations ; Base case , if we put the last character at front of A ; Base case , if we put the last character at back of A ; Condition if current sequence is matchable ; Condition for front move to ( i - 1 ) th character ; Condition for back move to ( i - 1 ) th character ; Condition if it is possible to make String A equal to String T ; Print final answer ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void twoStringsEquality ( String s , String t ) { int n = s . length ( ) ; int [ ] [ ] dp = new int [ n ] [ n + 1 ] ; if ( s . charAt ( n - 1 ) == t . charAt ( 0 ) ) dp [ n - 1 ] [ 1 ] = 1 ; if ( s . charAt ( n - 1 ) == t . charAt ( n - 1 ) ) dp [ n - 1 ] [ 0 ] = 1 ; for ( int i = n - 1 ; i > 0 ; i -- ) { for ( int j = 0 ; j <= n - i ; j ++ ) { if ( dp [ i ] [ j ] > 0 ) { if ( s . charAt ( i - 1 ) == t . charAt ( j ) ) dp [ i - 1 ] [ j + 1 ] = 1 ; if ( s . charAt ( i - 1 ) == t . charAt ( i + j - 1 ) ) dp [ i - 1 ] [ j ] = 1 ; } } } boolean ans = false ; for ( int i = 0 ; i <= n ; i ++ ) { if ( dp [ 0 ] [ i ] == 1 ) { ans = true ; break ; } } if ( ans == true ) System . out . print ( " Yes " + "
"); else System . out . print ( " No " + "
"); } public static void main ( String [ ] args ) { String S = " abab " ; String T = " baab " ; twoStringsEquality ( S , T ) ; } }


"Check if original Array is retained after performing XOR with M exactly K times |  ; Function to check if original Array can be retained by performing XOR with M exactly K times ; Check if O is present or not ; If K is odd and 0 is not present then the answer will always be No . ; Else it will be Yes ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String check ( int [ ] Arr , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } public static void main ( String args [ ] ) { int [ ] Arr = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = Arr . length ; System . out . println ( check ( Arr , n , M , K ) ) ; } }


"Check if permutation of a given string can be made palindromic by removing at most K characters | Java program to implement the above approach ; Function to check if the string satisfies the given conditions or not ; Stores length of given string ; Stores frequency of each character of str ; Update frequency of current character ; Stores count of distinct character whose frequency is odd ; Traverse the cntFreq [ ] array . ; If frequency of character i is odd ; Update cntOddFreq ; If count of distinct character having odd frequency is <= K + 1 ; Driver Code ; If str satisfy the given conditions"
How can the above be solved in Java?

import java . util . * ; class GFG { public static boolean checkPalinK ( String str , int K ) { int N = str . length ( ) ; int cntFreq [ ] = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ str . charAt ( i ) ] ++ ; } int cntOddFreq = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % 2 == 1 ) { cntOddFreq ++ ; } } if ( cntOddFreq <= ( K + 1 ) ) { return true ; } return false ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int K = 2 ; if ( checkPalinK ( str , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if permutation of first N natural numbers exists having Bitwise AND of adjacent elements non | Java Program to implement the above approach ; Function to check if a permutation of first N natural numbers exist with Bitwise AND of adjacent elements not equal to 0 ; If n is a power of 2 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class solution { static void check ( int n ) { if ( ( n & n - 1 ) != 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int n = 5 ; check ( n ) ; } }


"Check if point ( X , Y ) can be reached from origin ( 0 , 0 ) with jump of 1 and N perpendicularly simultaneously | Java code for the above approach ; Function to check if ( X , Y ) is reachable from ( 0 , 0 ) using the jumps of given type ; Case where source & destination are the same ; Check for even N ( X , Y ) is reachable or not ; If N is odd and parity of X and Y is different return , no valid sequence of jumps exist ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static String checkReachability ( int N , int X , int Y ) { if ( X == 0 && Y == 0 ) { return " YES " ; } if ( N % 2 == 0 ) { return " YES " ; } else { if ( X % 2 != Y % 2 ) { return " NO " ; } else { return " YES " ; } } } public static void main ( String [ ] args ) { int N = 2 ; int X = 5 , Y = 4 ; System . out . println ( checkReachability ( N , X , Y ) ) ; } }


"Check if possible to cross the matrix with given power | Java program to find if it is possible to cross the matrix with given power ; Initializing array dp with false value . ; For each value of dp [ i ] [ j ] [ k ] ; For first cell and for each value of k ; For first cell of each row ; For first cell of each column ; For rest of the cell ; Down movement . ; Right movement . ; Diagonal movement . ; Finding maximum k . ; Driver code"
How can the above be solved in Java?

class GFG { static final int N = 105 ; static final int R = 3 ; static final int C = 4 ; static int maximumValue ( int n , int m , int p , int grid [ ] [ ] ) { boolean dp [ ] [ ] [ ] = new boolean [ N ] [ N ] [ N ] ; int i , j , k ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { for ( k = 0 ; k < N ; k ++ ) dp [ i ] [ j ] [ k ] = false ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { for ( k = grid [ i ] [ j ] ; k <= p ; k ++ ) { if ( i == 0 && j == 0 ) { if ( k == grid [ i ] [ j ] ) dp [ i ] [ j ] [ k ] = true ; } else if ( i == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } else if ( j == 0 ) { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; } else { dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j ] [ k - grid [ i ] [ j ] ] ) ; dp [ i ] [ j ] [ k ] = ( dp [ i ] [ j ] [ k ] dp [ i - 1 ] [ j - 1 ] [ k - grid [ i ] [ j ] ] ) ; } } } } k = p ; int ans = 0 ; for ( ans = k ; ans >= 0 ; ans -- ) if ( dp [ n - 1 ] [ m - 1 ] [ ans ] ) break ; return ans ; } public static void main ( String [ ] args ) { int n = 3 , m = 4 , p = 9 ; int grid [ ] [ ] = { { 2 , 3 , 4 , 1 } , { 6 , 5 , 5 , 3 } , { 5 , 2 , 3 , 4 } } ; System . out . println ( maximumValue ( n , m , p , grid ) ) ; } }


"Check if possible to make Array sum equal to Array product by replacing exactly one element | Java program for the above approach ; Function to check if it is possible to form an array whose sum and the product is the same or not ; Find the sum of the array initialize sum ; Iterate through all elements and add them to sum ; Find the product of the array ; Check a complete integer y for every x ; If got such y ; If no such y exist ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int canPossibleReplacement ( int N , int [ ] arr ) { int S = 0 ; int i ; for ( i = 0 ; i < arr . length ; i ++ ) S += arr [ i ] ; int P = 1 ; for ( i = 0 ; i < arr . length ; i ++ ) { P *= i ; } for ( int x : arr ) { int y = ( S - x ) / ( P / x - 1 ) ; if ( ( S - x + y ) == ( P * y ) / x ) return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 1 , 3 , 4 } ; if ( canPossibleReplacement ( N , arr ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if possible to move from given coordinate to desired coordinate | Java program to check if it is possible to reach ( a , b ) from ( x , y ) . ; Returns GCD of i and j ; Returns true if it is possible to go to ( a , b ) from ( x , y ) ; Find absolute values of all as sign doesn 't matter. ; If gcd is equal then it is possible to reach . Else not possible . ; Driver code ; Converting coordinate into positive integer"
How can the above be solved in Java?

class GFG { static int gcd ( int i , int j ) { if ( i == j ) return i ; if ( i > j ) return gcd ( i - j , j ) ; return gcd ( i , j - i ) ; } static boolean ispossible ( int x , int y , int a , int b ) { x = Math . abs ( x ) ; y = Math . abs ( y ) ; a = Math . abs ( a ) ; b = Math . abs ( b ) ; return ( gcd ( x , y ) == gcd ( a , b ) ) ; } public static void main ( String [ ] args ) { int x = 35 , y = 15 ; int a = 20 , b = 25 ; if ( ispossible ( x , y , a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if product of array containing prime numbers is a perfect square | Java implementation of the approach ; Function that returns true if the product of all the array elements is a perfect square ; Update the frequencies of all the array elements ; If frequency of some element in the array is odd ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isPerfectSquare ( int [ ] arr , int n ) { HashMap < Integer , Integer > umap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( umap . containsKey ( arr [ i ] ) ) umap . put ( arr [ i ] , umap . get ( arr [ i ] ) + 1 ) ; else umap . put ( arr [ i ] , 1 ) ; } Iterator < Map . Entry < Integer , Integer > > iterator = umap . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry < Integer , Integer > entry = iterator . next ( ) ; if ( entry . getValue ( ) % 2 == 1 ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 7 , 7 } ; int n = arr . length ; if ( isPerfectSquare ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if product of digits of a number at even and odd places is equal | Java implementation of the approach ; To store the respective product ; Converting integer to String ; Traversing the String ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void getResult ( int n ) { int proOdd = 1 ; int proEven = 1 ; String num = String . valueOf ( n ) ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) if ( i % 2 == 0 ) proOdd = proOdd * ( num . charAt ( i ) - '0' ) ; else proEven = proEven * ( num . charAt ( i ) - '0' ) ; if ( proOdd == proEven ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int n = 4324 ; getResult ( n ) ; } }


"Check if product of first N natural numbers is divisible by their sum | Java implementation of the approach ; Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that return true if the product of the first n natural numbers is divisible by the sum of first n natural numbers ; Driver code"
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 ; } static boolean isDivisible ( int n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { int n = 6 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if quantities of 3 distinct colors can be converted to a single color by given merge | Java program for the above approach ; Function to check whether it is possible to do the operation or not ; Calculate modulo 3 of all the colors ; Check for any equal pair ; Otherwise ; Driver Code ; Given colors ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean isPossible ( int r , int b , int g ) { r = r % 3 ; b = b % 3 ; g = g % 3 ; if ( r == b b == g g == r ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int R = 1 , B = 3 , G = 6 ; if ( isPossible ( R , B , G ) ) { System . out . print ( " Yes " + "
"); } else { System . out . print ( " No " + "
"); } } }


"Check if rearranging Array elements can form a Palindrome or not | Java implementation of the above approach ; Function to check whether elements of an array can form a palindrome ; create an empty string to append elements of an array ; append each element to the string str to form a string so that we can solve it in easy way ; Create a freq array and initialize all values as 0 ; For each character in formed string , increment freq in the corresponding freq array ; Count odd occurring characters ; Return true if odd count is 0 or 1 , ; Drive code"
How can the above be solved in Java?

import java . io . * ; import java . util . Arrays ; class GFG { static int MAX = 256 ; static boolean can_form_palindrome ( int [ ] arr , int n ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) { str += arr [ i ] ; } int freq [ ] = new int [ MAX ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) ] ++ ; } int count = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( ( freq [ i ] & 1 ) != 0 ) { count ++ ; } if ( count > 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 } ; int n = arr . length ; if ( can_form_palindrome ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if removal of a subsequence of non | Java program for the above approach ; Function to check if it is possible to sort the array or not ; Stores the index if there are two consecutive 1 's in the array ; Traverse the given array ; Check adjacent same elements having values 1 s ; If there are no two consecutive 1 s , then always remove all the 1 s from array & make it sorted ; If two consecutive 0 ' s ▁ are ▁ ▁ present ▁ after ▁ two ▁ consecutive ▁ ▁ 1' s then array can 't be sorted ; Otherwise , print Yes ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void isPossibleToSort ( int arr [ ] , int N ) { int idx = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { System . out . println ( " YES " ) ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = arr . length ; isPossibleToSort ( arr , N ) ; } }


"Check if removing an edge can divide a Binary Tree in two halves | Java program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns size of tree rooted with given root . It also set " res " as true if there is an edge whose removal divides tree in two halves . n is size of tree ; Base case ; Compute sizes of left and right children ; If required property is true for current node set " res " as true ; Return size ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Initialize result and recursively check all nodes ; Driver code"
How can the above be solved in Java?

class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class Res { boolean res = false ; } class BinaryTree { Node root ; int count ( Node node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } int checkRec ( Node root , int n , Res res ) { if ( root == null ) return 0 ; int c = checkRec ( root . left , n , res ) + 1 + checkRec ( root . right , n , res ) ; if ( c == n - c ) res . res = true ; return c ; } boolean check ( Node root ) { int n = count ( root ) ; Res res = new Res ( ) ; checkRec ( root , n , res ) ; return res . res ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if removing an edge can divide a Binary Tree in two halves | Java program to check if there exist an edge whose removal creates two trees of same size ; To calculate size of tree with given root ; This function returns true if there is an edge whose removal can divide the tree in two halves n is size of tree ; Base cases ; Check for root ; Check for rest of the nodes ; This function mainly uses checkRec ( ) ; Count total nodes in given tree ; Now recursively check all nodes ; Driver code"
How can the above be solved in Java?

class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; int count ( Node node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } boolean checkRec ( Node node , int n ) { if ( node == null ) return false ; if ( count ( node ) == n - count ( node ) ) return true ; return checkRec ( node . left , n ) || checkRec ( node . right , n ) ; } boolean check ( Node node ) { int n = count ( node ) ; return checkRec ( node , n ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if reversing a sub array make the array sorted | Java program to check whether reversing a sub array make the array sorted or not ; Return true , if reversing the subarray will sort t he array , else return false . ; Find first increasing part ; Find reversed part ; Find last increasing part ; To handle cases like { 1 , 2 , 3 , 4 , 20 , 9 , 16 , 17 } ; Driven Program"
How can the above be solved in Java?

class GFG { static boolean checkReverse ( int arr [ ] , int n ) { if ( n == 1 ) { return true ; } int i ; for ( i = 1 ; arr [ i - 1 ] < arr [ i ] && i < n ; i ++ ) ; if ( i == n ) { return true ; } int j = i ; while ( j < n && arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) { return false ; } j ++ ; } if ( j == n ) { return true ; } int k = j ; if ( arr [ k ] < arr [ i - 1 ] ) { return false ; } while ( k > 1 && k < n ) { if ( arr [ k ] < arr [ k - 1 ] ) { return false ; } k ++ ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 10 , 9 , 8 } ; int n = arr . length ; if ( checkReverse ( arr , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if reversing a sub array make the array sorted | Java program to check whether reversing a sub array make the array sorted or not ; Return true , if reversing the subarray will sort the array , else return false . ; Copying the array . ; Sort the copied array . ; Finding the first mismatch . ; Finding the last mismatch . ; If whole array is sorted ; Checking subarray is decreasing or not . ; Driven Program"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { static boolean checkReverse ( int arr [ ] , int n ) { int temp [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { temp [ i ] = arr [ i ] ; } Arrays . sort ( temp ) ; int front ; for ( front = 0 ; front < n ; front ++ ) { if ( temp [ front ] != arr [ front ] ) { break ; } } int back ; for ( back = n - 1 ; back >= 0 ; back -- ) { if ( temp [ back ] != arr [ back ] ) { break ; } } if ( front >= back ) { return true ; } do { front ++ ; if ( arr [ front - 1 ] < arr [ front ] ) { return false ; } } while ( front != back ) ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 4 , 3 } ; int n = arr . length ; if ( checkReverse ( arr , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not | Java program to check if roots of a Quadratic Equation are numerically equal but opposite in sign or not ; Function to find the required answer ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void checkSolution ( int a , int b , int c ) { if ( b == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ; } }


"Check if sequence of removed middle elements from an array is sorted or not | Java program for the above approach ; Function to check if sequence of removed middle elements from an array is sorted or not ; Points to the ends of the array ; Iterate l + 1 < r ; If the element at index L and R is greater than ( L + 1 ) - th and ( R - 1 ) - th elements ; If true , then decrement R by 1 and increment L by 1 ; Otherwise , return false ; If an increasing sequence is formed , then return true ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isSortedArray ( int [ ] arr , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 3 , 1 , 2 , 5 } ; int N = arr . length ; if ( isSortedArray ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if stack elements are pairwise consecutive | Java program to check if successive pair of numbers in the stack are consecutive or not ; Function to check if elements are pairwise consecutive in stack ; Transfer elements of s to aux . ; Traverse aux and see if elements are pairwise consecutive or not . We also need to make sure that original content is retained . ; Fetch current top two elements of aux and check if they are consecutive . ; Push the elements to original stack . ; Driver program"
How can the above be solved in Java?

import java . util . * ; class GfG { static boolean pairWiseConsecutive ( Stack < Integer > s ) { Stack < Integer > aux = new Stack < Integer > ( ) ; while ( ! s . isEmpty ( ) ) { aux . push ( s . peek ( ) ) ; s . pop ( ) ; } boolean result = true ; while ( aux . size ( ) > 1 ) { int x = aux . peek ( ) ; aux . pop ( ) ; int y = aux . peek ( ) ; aux . pop ( ) ; if ( Math . abs ( x - y ) != 1 ) result = false ; s . push ( x ) ; s . push ( y ) ; } if ( aux . size ( ) == 1 ) s . push ( aux . peek ( ) ) ; return result ; } public static void main ( String [ ] args ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 4 ) ; s . push ( 5 ) ; s . push ( - 2 ) ; s . push ( - 3 ) ; s . push ( 11 ) ; s . push ( 10 ) ; s . push ( 5 ) ; s . push ( 6 ) ; s . push ( 20 ) ; if ( pairWiseConsecutive ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; System . out . println ( " Stack ▁ content ▁ ( from ▁ top ) ▁ after ▁ function ▁ call " ) ; while ( s . isEmpty ( ) == false ) { System . out . print ( s . peek ( ) + " ▁ " ) ; s . pop ( ) ; } } }


"Check if string S1 can be formed using repeated insertions of another string S2 | Java program for the above approach ; Function to check a valid insertion ; Store the size of string ; Maintain a stack for characters ; Iterate through the string ; push the current character on top of the stack ; If the current character is the last character of string S2 then pop characters until S2 is not formed ; index of last character of the string S2 ; pop characters till 0 - th index ; Check if stack in non - empty ; Driver code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static boolean validInsertionstring ( String S1 , String S2 ) { int N = S1 . length ( ) ; int M = S2 . length ( ) ; Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . push ( S1 . charAt ( i ) ) ; if ( S1 . charAt ( i ) == S2 . charAt ( M - 1 ) ) { int idx = M - 1 ; while ( idx >= 0 ) { if ( st . size ( ) == 0 ) { return false ; } char c = st . peek ( ) ; st . pop ( ) ; if ( c != S2 . charAt ( idx ) ) { return false ; } idx -- ; } } } if ( st . size ( ) > 0 ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { String S1 = " aabb " ; String S2 = " ab " ; if ( validInsertionstring ( S1 , S2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if string can be made lexicographically smaller by reversing any substring | Java implementation of the approach ; Function that returns true if s can be made lexicographically smaller by reversing a sub - string in s ; Traverse in the string ; Check if s [ i + 1 ] < s [ i ] ; Not possible ; Driver code"
How can the above be solved in Java?

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


"Check if string can be rearranged so that every Odd length Substring is Palindrome | Java implementation of the approach ; Function to check is it possible to rearrange the string such that every odd length substring is palindrome ; Length of the string ; To count number of distinct character in string ; To count frequency of each character ; Inserting into set ; Incrementing the frequency ; All characters in the string are same ; There are more than 2 different character in string ; Currently there is 2 different character in string ; Get the frequencies of the characters that present in string ; Difference between their count is less than or equal to 1 ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean IsPossible ( String s ) { int n = s . length ( ) ; HashSet < Character > count = new HashSet < > ( ) ; HashMap < Character , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { count . add ( s . charAt ( i ) ) ; map . put ( s . charAt ( i ) , map . get ( s . charAt ( i ) ) == null ? 1 : map . get ( s . charAt ( i ) ) + 1 ) ; } if ( count . size ( ) == 1 ) return true ; if ( count . size ( ) > 2 ) return false ; String newString = count . toArray ( ) . toString ( ) ; int j = 0 ; char it = newString . charAt ( j ) ; int x = 0 , y = 0 ; x = map . get ( it ) == null ? 0 : map . get ( it ) ; j ++ ; it = newString . charAt ( j ) ; y = map . get ( it ) == null ? 0 : map . get ( it ) ; if ( Math . abs ( x - y ) <= 1 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " aaaddad " ; if ( IsPossible ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if string follows order of characters defined by a pattern or not | Set 1 | Java program check if characters in the input string follows the same order as determined by characters present in the given pattern ; Function to check if characters in the input string follows the same order as determined by characters present in the given pattern ; len stores length of the given pattern ; if length of pattern is more than length of input string , return false ; ; x , y are two adjacent characters in pattern ; find index of last occurrence of character x in the input string ; find index of first occurrence of character y in the input string ; return false if x or y are not present in the input string OR last occurrence of x is after the first occurrence of y in the input string ; return true if string matches the pattern ; Driver code"
How can the above be solved in Java?

class GFG { static boolean checkPattern ( String str , String pattern ) { int len = pattern . length ( ) ; if ( str . length ( ) < len ) { return false ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { char x = pattern . charAt ( i ) ; char y = pattern . charAt ( i + 1 ) ; int last = str . lastIndexOf ( x ) ; int first = str . indexOf ( y ) ; if ( last == - 1 first == - 1 last > first ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " engineers ▁ rock " ; String pattern = " gsr " ; System . out . println ( checkPattern ( str , pattern ) ) ; } }


"Check if string follows order of characters defined by a pattern or not | Set 2 | Java program to check if characters of a string follow pattern defined by given pattern . ; Insert all characters of pattern in a hash set , ; Build modified string ( string with characters only from pattern are taken ) ; Remove more than one consecutive occurrences of pattern characters from modified string . ; After above modifications , the length of modified string must be same as pattern length ; And pattern characters must also be same as modified string characters ; Driver program"
How can the above be solved in Java?

import java . util . * ; public class OrderOfCharactersForPattern { public static boolean followsPattern ( String str , String pattern ) { Set < Character > patternSet = neHashSet < > ( ) ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) patternSet . add ( pattern . charAt ( i ) ) ; StringBuilder modifiedString = new StringBuilder ( str ) ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( ! patternSet . contains ( modifiedString . charAt ( i ) ) ) modifiedString . deleteCharAt ( i ) ; for ( int i = modifiedString . length ( ) - 1 ; i > 0 ; i -- ) if ( modifiedString . charAt ( i ) == modifiedString . charAt ( i - 1 ) ) modifiedString . deleteCharAt ( i ) ; if ( pattern . length ( ) != modifiedString . length ( ) ) return false ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) if ( pattern . charAt ( i ) != modifiedString . charAt ( i ) ) return false ; return true ; } int main ( ) { String str = " engineers ▁ rock " ; String pattern = " er " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " egr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " gsr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " eger " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; return 0 ; } }


"Check if string follows order of characters defined by a pattern or not | Set 3 | Java program to find if a string follows order defined by a given pattern . ; Returns true if characters of str follow order defined by a given ptr . ; Initialize all orders as - 1 ; Assign an order to pattern characters according to their appearance in pattern ; give the pattern characters order ; increment the order ; Now one by check if string characters follow above order ; If order of this character is less than order of previous , return false . ; Update last_order for next iteration ; return that str followed pat ; Driver code"
How can the above be solved in Java?

class GFG { static int CHAR_SIZE = 256 ; static boolean checkPattern ( String str , String pat ) { int [ ] label = new int [ CHAR_SIZE ] ; for ( int i = 0 ; i < CHAR_SIZE ; i ++ ) label [ i ] = - 1 ; int order = 1 ; for ( int i = 0 ; i < pat . length ( ) ; i ++ ) { label [ pat . charAt ( i ) ] = order ; order ++ ; } int last_order = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( label [ str . charAt ( i ) ] != - 1 ) { if ( label [ str . charAt ( i ) ] < last_order ) return false ; last_order = label [ str . charAt ( i ) ] ; } } return true ; } public static void main ( String [ ] args ) { String str = " engineers ▁ rock " ; String pattern = " gsr " ; System . out . println ( checkPattern ( str , pattern ) ) ; } }