"Change a Binary Tree so that every node stores sum of all nodes in left subtree | Java program to store sum of nodes in left subtree in every node ; A tree node ; Function to modify a Binary Tree so that every node stores sum of values in its left child including its own value ; Base cases ; Update left and right subtrees ; Add leftsum to current node ; Return sum of values under root ; Utility function to do inorder traversal ; Utility function to create a new node ; Driver program ; Let us construct below tree 1 / \ 2 3 / \ \ 4 5 6"
How can the above be solved in Java?

class GfG { static class node { int data ; node left , right ; } static int updatetree ( node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; int leftsum = updatetree ( root . left ) ; int rightsum = updatetree ( root . right ) ; root . data += leftsum ; return root . data + rightsum ; } static void inorder ( node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inorder ( node . right ) ; } static node newNode ( int data ) { node node = new node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } public static void main ( String [ ] args ) { node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; updatetree ( root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ modified ▁ tree ▁ is " ) ; inorder ( root ) ; } }


"Change all even bits in a number to 0 | Java program to change even bits to 0. ; Returns modified number with all even bits 0. ; To store sum of bits at even positions . ; To store bits to shift ; One by one put all even bits to end ; If current last bit is set , add it to ans ; Next shift position ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int changeEvenBits ( int n ) { int to_subtract = 0 ; int m = 0 ; for ( int x = n ; x > 0 ; x >>= 2 ) { if ( ( x & 1 ) > 0 ) to_subtract += ( 1 << m ) ; m += 2 ; } return n - to_subtract ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( changeEvenBits ( n ) ) ; } }


"Change one element in the given array to make it an Arithmetic Progression | Java program to change one element of an array such that the resulting array is in arithmetic progression . ; Finds the initial term and common difference and prints the resulting array . ; Check if the first three elements are in arithmetic progression ; Check if the first element is not in arithmetic progression ; The first and fourth element are in arithmetic progression ; Print the arithmetic progression ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class AP { static void makeAP ( int arr [ ] , int n ) { int initial_term , common_difference ; if ( n == 3 ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else if ( ( arr [ 1 ] - arr [ 0 ] ) == arr [ 2 ] - arr [ 1 ] ) { initial_term = arr [ 0 ] ; common_difference = arr [ 1 ] - arr [ 0 ] ; } else if ( ( arr [ 2 ] - arr [ 1 ] ) == ( arr [ 3 ] - arr [ 2 ] ) ) { common_difference = arr [ 2 ] - arr [ 1 ] ; initial_term = arr [ 1 ] - common_difference ; } else { common_difference = ( arr [ 3 ] - arr [ 0 ] ) / 3 ; initial_term = arr [ 0 ] ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( initial_term + ( i * common_difference ) + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 } ; int n = arr . length ; makeAP ( arr , n ) ; } }


"Change string to a new character set | Java program to change the sentence with virtual dictionary ; Converts str to given character set ; hashing for new character set ; conversion of new character set ; Driver code"
How can the above be solved in Java?

class GFG { static String conversion ( char charSet [ ] , String str ) { int n = str . length ( ) ; char hashChar [ ] = new char [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int ch = Math . abs ( charSet [ i ] - ' a ' ) ; hashChar [ ch ] = ( char ) ( ' a ' + i ) ; } String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += hashChar [ str . charAt ( i ) - ' a ' ] ; } return s ; } public static void main ( String [ ] args ) { char charSet [ ] = " qwertyuiopasdfghjklzxcvbnm " . toCharArray ( ) ; String str = " egrt " ; str = conversion ( charSet , str ) ; System . out . println ( str ) ; } }


"Change string to a new character set | Java program to change the sentence with virtual dictionary ; function for converting the string ; find the index of each element of the string in the modified set of alphabets replace the element with the one having the same index in the actual set of alphabets ; Driver Code"
How can the above be solved in Java?

class GFG { static char [ ] alphabets = " abcdefghijklmnopqrstuvwxyz " . toCharArray ( ) ; static String conversion ( String charSet , char [ ] str1 ) { String s2 = " " ; for ( char i : str1 ) s2 += alphabets [ charSet . indexOf ( i ) ] ; return s2 ; } public static void main ( String [ ] args ) { String charSet = " qwertyuiopasdfghjklzxcvbnm " ; String str1 = " egrt " ; System . out . print ( conversion ( charSet , str1 . toCharArray ( ) ) ) ; } }


"Change the array into a permutation of numbers from 1 to n | Java program to make a permutation of numbers from 1 to n using minimum changes . ; Store counts of all elements . ; Find next missing element to put in place of current element . ; Replace with next missing and insert the missing element in hash . ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void makePermutation ( int [ ] a , int n ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . containsKey ( a [ i ] ) ) { count . put ( a [ i ] , count . get ( a [ i ] ) + 1 ) ; } else { count . put ( a [ i ] , 1 ) ; } } int next_missing = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . containsKey ( a [ i ] ) && count . get ( a [ i ] ) != 1 a [ i ] > n a [ i ] < 1 ) { count . put ( a [ i ] , count . get ( a [ i ] ) - 1 ) ; while ( count . containsKey ( next_missing ) ) next_missing ++ ; a [ i ] = next_missing ; count . put ( next_missing , 1 ) ; } } } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 3 , 3 } ; int n = A . length ; makePermutation ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }


"Change the given string according to the given conditions | Java program for the above approach ; Function to proofread the spells ; Loop to iterate over the characters of the string ; Push the current character c in the stack ; Check for Rule 1 ; Check for Rule 2 ; To store the resultant string ; Loop to iterate over the characters of stack ; Return the resultant string ; Driver Code ; Given string str ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { public static String proofreadSpell ( String str ) { Vector < Character > result = new Vector < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { result . add ( str . charAt ( i ) ) ; int n = result . size ( ) ; if ( n >= 3 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 1 ) == result . get ( n - 3 ) ) { result . remove ( result . size ( ) - 1 ) ; } } n = result . size ( ) ; if ( n >= 4 ) { if ( result . get ( n - 1 ) == result . get ( n - 2 ) && result . get ( n - 3 ) == result . get ( n - 4 ) ) { result . remove ( result . size ( ) - 1 ) ; } } } String resultStr = " " ; for ( Character c : result ) { resultStr += c ; } return resultStr ; } public static void main ( String [ ] args ) { String str = " hello " ; System . out . println ( proofreadSpell ( str ) ) ; } }


"Channel Assignment Problem |  ; A Depth First Search based recursive function that returns true if a matching for vertex u is possible ; Try every receiver one by one ; If sender u has packets to send to receiver v and receiver v is not already mapped to any other sender just check if the number of packets is greater than '0' because only one packet can be sent in a time frame anyways ; Mark v as visited ; If receiver ' v ' is not assigned to any sender OR previously assigned sender for receiver v ( which is matchR [ v ] ) has an alternate receiver available . Since v is marked as visited in the above line , matchR [ v ] in the following recursive call will not get receiver ' v ' again ; Returns maximum number of packets that can be sent parallely in 1 time slot from sender to receiver ; An array to keep track of the receivers assigned to the senders . The value of matchR [ i ] is the sender ID assigned to receiver i . The value - 1 indicates nobody is assigned . ; Initially all receivers are not mapped to any senders ; Count of receivers assigned to senders ; Mark all receivers as not seen for next sender ; Find if the sender ' u ' can be assigned to the receiver ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int M = 3 ; static int N = 3 ; static boolean bpm ( int table [ ] [ ] , int u , boolean seen [ ] , int matchR [ ] ) { for ( int v = 0 ; v < N ; v ++ ) { if ( table [ u ] [ v ] > 0 && ! seen [ v ] ) { seen [ v ] = true ; if ( matchR [ v ] < 0 || bpm ( table , matchR [ v ] , seen , matchR ) ) { matchR [ v ] = u ; return true ; } } } return false ; } static int maxBPM ( int table [ ] [ ] ) { int [ ] matchR = new int [ N ] ; Arrays . fill ( matchR , - 1 ) ; int result = 0 ; for ( int u = 0 ; u < M ; u ++ ) { boolean [ ] seen = new boolean [ N ] ; Arrays . fill ( seen , false ) ; if ( bpm ( table , u , seen , matchR ) ) result ++ ; } System . out . println ( " The ▁ number ▁ of ▁ maximum ▁ packets " + " ▁ sent ▁ in ▁ the ▁ time ▁ slot ▁ is ▁ " + result ) ; for ( int x = 0 ; x < N ; x ++ ) if ( matchR [ x ] + 1 != 0 ) System . out . println ( " T " + ( matchR [ x ] + 1 ) + " - > ▁ R " + ( x + 1 ) ) ; return result ; } public static void main ( String [ ] args ) { int table [ ] [ ] = { { 0 , 2 , 0 } , { 3 , 0 , 1 } , { 2 , 4 , 0 } } ; maxBPM ( table ) ; } }


"Character replacement after removing duplicates from a string | Java program for character replacement after String minimization ; Function to minimize String ; duplicate characters are removed ; checks if character has previously occurred or not if not then add it to the minimized String ' mstr ' ; return mstr ; minimized String ; Utility function to print the minimized , replaced String ; Creating final String by replacing character ; index calculation ; final String ; Driver program"
How can the above be solved in Java?

class GFG { static String minimize ( String str ) { String mstr = " ▁ " ; int l , i , flagchar [ ] = new int [ 26 ] ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { ch = str . charAt ( i ) ; if ( flagchar [ ch - 97 ] == 0 ) { mstr = mstr + ch ; flagchar [ ch - 97 ] = 1 ; } } } static void replaceMinimizeUtil ( String str ) { String minimizedStr , finalStr = " " ; int i , index , l ; char ch ; l = str . length ( ) ; for ( i = 0 ; i < minimizedStr . length ( ) ; i ++ ) { ch = minimizedStr . charAt ( i ) ; index = ( ch * ch ) % l ; finalStr = finalStr + str . charAt ( index ) ; } System . out . println ( " Final ▁ String : ▁ " + finalStr ) ; } public static void main ( String [ ] args ) { String str = " geeks " ; replaceMinimizeUtil ( str ) ; } }


"Character whose frequency is equal to the sum of frequencies of other characters of the given string | Java implementation of the above approach . ; Function that returns true if some character exists in the given string whose frequency is equal to the sum frequencies of other characters of the string ; If string is of odd length ; To store the frequency of each character of the string ; Update the frequencies of the characters ; No such character exists ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isFrequencyEqual ( String str , int len ) { if ( len % 2 == 1 ) { return false ; } int i , freq [ ] = new int [ 26 ] ; for ( i = 0 ; i < len ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == len / 2 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; if ( isFrequencyEqual ( str , len ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check If every group of a ' s ▁ is ▁ followed ▁ by ▁ a ▁ group ▁ of ▁ b ' s of same length | Java implementation of the above approach ; Function to match whether there are always n consecutive b ' s ▁ followed ▁ by ▁ n ▁ consecutive ▁ a ' s throughout the string ; Traverse through the string ; Count a 's in current segment ; Count b 's in current segment ; If both counts are not same . ; Driver code"
How can the above be solved in Java?

public class GFG { static boolean matchPattern ( String s ) { int count = 0 ; int n = s . length ( ) ; int i = 0 ; while ( i < n ) { while ( i < n && s . charAt ( i ) == ' a ' ) { count ++ ; i ++ ; } while ( i < n && s . charAt ( i ) == ' b ' ) { count -- ; i ++ ; } if ( count != 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { String s = " bb " ; if ( matchPattern ( s ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check a given sentence for a given set of simple grammer rules | Java program to validate a given sentence for a set of rules ; Method to check a given sentence for given rules ; Calculate the length of the string . ; Check that the first character lies in [ A - Z ] . Otherwise return false . ; If the last character is not a full stop ( . ) no need to check further . ; Maintain 2 states . Previous and current state based on which vertex state you are . Initialise both with 0 = start state . ; Keep the index to the next character in the string . ; Loop to go over the string . ; Set states according to the input characters in the string and the rule defined in the description . If current character is [ A - Z ] . Set current state as 0. ; If current character is a space . Set current state as 1. ; If current character is [ a - z ] . Set current state as 2. ; If current state is a dot ( . ) . Set current state as 3. ; Validates all current state with previous state for the rules in the description of the problem . ; If we have reached last state and previous state is not 1 , then check next character . If next character is ' \0' , then return true , else false ; Set previous state as current state before going over to the next character . ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean checkSentence ( char [ ] str ) { int len = str . length ; if ( str [ 0 ] < ' A ' str [ 0 ] > ' Z ' ) return false ; if ( str [ len - 1 ] != ' . ' ) return false ; int prev_state = 0 , curr_state = 0 ; int index = 1 ; while ( index <= str . length ) { if ( str [ index ] >= ' A ' && str [ index ] <= ' Z ' ) curr_state = 0 ; else if ( str [ index ] == ' ▁ ' ) curr_state = 1 ; else if ( str [ index ] >= ' a ' && str [ index ] <= ' z ' ) curr_state = 2 ; else if ( str [ index ] == ' . ' ) curr_state = 3 ; if ( prev_state == curr_state && curr_state != 2 ) return false ; if ( prev_state == 2 && curr_state == 0 ) return false ; if ( curr_state == 3 && prev_state != 1 ) return ( index + 1 == str . length ) ; index ++ ; prev_state = curr_state ; } return false ; } public static void main ( String [ ] args ) { String [ ] str = { " I ▁ love ▁ cinema . " , " The ▁ vertex ▁ is ▁ S . " , " I ▁ am ▁ single . " , " My ▁ name ▁ is ▁ KG . " , " I ▁ lovE ▁ cinema . " , " GeeksQuiz . ▁ is ▁ a ▁ quiz ▁ site . " , " I ▁ love ▁ Geeksquiz ▁ and ▁ Geeksforgeeks . " , " ▁ You ▁ are ▁ my ▁ friend . " , " I ▁ love ▁ cinema " } ; int str_size = str . length ; int i = 0 ; for ( i = 0 ; i < str_size ; i ++ ) { if ( checkSentence ( str [ i ] . toCharArray ( ) ) ) System . out . println ( " \" " + str [ i ] + " \" " + " ▁ is ▁ correct " ) ; else System . out . println ( " \" " + str [ i ] + " \" " + " ▁ is ▁ incorrect " ) ; } } }


"Check a large number is divisible by 16 or not | Java program to find if a number is divisible by 16 or not ; Function to find that number divisible by 16 or not ; Empty string ; If there is double digit ; If there is triple digit ; If number formed by last four digits is divisible by 16. ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str . charAt ( n - 3 ) - '0' ) * 100 + ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; int third_last = str . charAt ( n - 3 ) - '0' ; int fourth_last = str . charAt ( n - 4 ) - '0' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } public static void main ( String args [ ] ) { String str = "769528" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No ▁ " ) ; } }


"Check a number is odd or even without modulus operator | A simple Java program to check for even or odd ; Returns true if n is even , else odd ; Driver Code"
How can the above be solved in Java?

class GFG { static boolean isEven ( int n ) { boolean isEven = true ; for ( int i = 1 ; i <= n ; i ++ ) isEven = ! isEven ; return isEven ; } public static void main ( String args [ ] ) { int n = 101 ; if ( isEven ( n ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }


"Check a number is odd or even without modulus operator | A simple Java program to check for even or odd ; Returns true if n is even , else odd ; Return true if n / 2 does not result in a float value . ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isEven ( int n ) { return ( ( n / 2 ) * 2 == n ) ; } public static void main ( String [ ] args ) { int n = 101 ; if ( isEven ( n ) != false ) System . out . print ( " Even " ) ; else System . out . print ( " Odd " ) ; } }


"Check for Amicable Pair | JAVA program to check if two numbers are Amicable or not . ; Function to calculate sum of all proper divisors of a given number ; Sum of divisors ; find all divisors which divides ' num ' ; if ' i ' is divisor of ' n ' ; if both divisors are same then add it once else add both ; Add 1 and n to result as above loop considers proper divisors greater than 1. ; Returns true if x and y are Amicable else false . ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + 1 ) ; } static boolean areAmicable ( int x , int y ) { if ( divSum ( x ) != y ) return false ; return ( divSum ( y ) == x ) ; } public static void main ( String [ ] args ) { int x = 220 , y = 284 ; if ( areAmicable ( x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check for Children Sum Property in a Binary Tree | A binary tree node has data , pointer to left child and a pointer to right child ; returns 1 if children sum property holds for the given node and both of its children ; left_data is left child data and right_data is for right child data ; If node is NULL or it 's a leaf node then  return true  ; If left child is not present then 0 is used as data of left child ; If right child is not present then 0 is used as data of right child ; if the node and both of its children satisfy the property return 1 else 0 ; driver program to test the above functions"
How can the above be solved in Java?

class Node { int data ; Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int isSumProperty ( Node node ) { int left_data = 0 , right_data = 0 ; if ( node == null || ( node . left == null && node . right == null ) ) return 1 ; else { if ( node . left != null ) left_data = node . left . data ; if ( node . right != null ) right_data = node . right . data ; if ( ( node . data == left_data + right_data ) && ( isSumProperty ( node . left ) != 0 ) && isSumProperty ( node . right ) != 0 ) return 1 ; else return 0 ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 2 ) ; if ( tree . isSumProperty ( tree . root ) != 0 ) System . out . println ( " The ▁ given ▁ tree ▁ satisfies ▁ children " + " ▁ sum ▁ property " ) ; else System . out . println ( " The ▁ given ▁ tree ▁ does ▁ not ▁ satisfy ▁ children " + " ▁ sum ▁ property " ) ; } }


"Check for balanced parentheses in an expression | O ( 1 ) space | Java code to check balanced parentheses with O ( 1 ) space . ; Function1 to match closing bracket ; Function1 to match opening bracket ; Function to check balanced parentheses ; helper variables ; Handling case of opening parentheses ; } Handling case of closing parentheses ; If corresponding matching opening parentheses doesn 't  lie in given interval return 0 ; else continue ; If corresponding closing parentheses doesn 't lie in given interval  return 0 ; if found , now check for each opening and closing parentheses in this interval ; Driver Code"
How can the above be solved in Java?

class GFG { static int matchClosing ( char X [ ] , int start , int end , char open , char close ) { int c = 1 ; int i = start + 1 ; while ( i <= end ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i ++ ; } return i ; } static int matchingOpening ( char X [ ] , int start , int end , char open , char close ) { int c = - 1 ; int i = end - 1 ; while ( i >= start ) { if ( X [ i ] == open ) { c ++ ; } else if ( X [ i ] == close ) { c -- ; } if ( c == 0 ) { return i ; } i -- ; } return - 1 ; } static boolean isBalanced ( char X [ ] , int n ) { int i , j = 0 , k , x , start , end ; for ( i = 0 ; i < n ; i ++ ) { if ( X [ i ] == ' ( ' ) { j = matchClosing ( X , i , n - 1 , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' { ' ) { j = matchClosing ( X , i , n - 1 , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' [ ' ) { j = matchClosing ( X , i , n - 1 , ' [ ' , ' ] ' ) ; } else { if ( X [ i ] == ' ) ' ) { j = matchingOpening ( X , 0 , i , ' ( ' , ' ) ' ) ; } else if ( X [ i ] == ' } ' ) { j = matchingOpening ( X , 0 , i , ' { ' , ' } ' ) ; } else if ( X [ i ] == ' ] ' ) { j = matchingOpening ( X , 0 , i , ' [ ' , ' ] ' ) ; } if ( j < 0 j >= i ) { return false ; } continue ; } if ( j >= n j < 0 ) { return false ; } start = i ; end = j ; for ( k = start + 1 ; k < end ; k ++ ) { if ( X [ k ] == ' ( ' ) { x = matchClosing ( X , k , end , ' ( ' , ' ) ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ) ' ) { x = matchingOpening ( X , start , k , ' ( ' , ' ) ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' { ' ) { x = matchClosing ( X , k , end , ' { ' , ' } ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' } ' ) { x = matchingOpening ( X , start , k , ' { ' , ' } ' ) ; if ( ! ( start < x && x < k ) ) { return false ; } } if ( X [ k ] == ' [ ' ) { x = matchClosing ( X , k , end , ' [ ' , ' ] ' ) ; if ( ! ( k < x && x < end ) ) { return false ; } } else if ( X [ k ] == ' ] ' ) { x =...


"Check for each subarray whether it consists of all natural numbers up to its length or not | Java program to implement the above approach ; Function to check if a subarray of size i exists that contain all the numbers in the range [ 1 , i ] ; Store the position of each element of arr [ ] ; Traverse the array ; Insert the position of arr [ i ] ; Store position of each element from the range [ 1 , N ] ; Iterate over the range [ 1 , N ] ; Insert the index of i into st ; Find the smallest element of st ; Find the largest element of st ; If distance between the largest and smallest element of arr [ ] till i - th index is equal to i ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static void checksubarrayExist1_N ( int arr [ ] , int N ) { Map < Integer , Integer > pos = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pos . put ( arr [ i ] , i ) ; } Set < Integer > st = new HashSet < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { st . add ( pos . get ( i ) ) ; int Min = Collections . min ( st ) ; int Max = Collections . max ( st ) ; if ( Max - Min + 1 == i ) { System . out . print ( " True ▁ " ) ; } else { System . out . print ( " False ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 2 } ; int N = arr . length ; checksubarrayExist1_N ( arr , N ) ; } }


"Check for integer overflow on multiplication | Java program to check for integer overflow on multiplication ; Function to check whether there is overflow in a * b or not . It returns true if there is overflow . ; Check if either of them is zero ; driver function"
How can the above be solved in Java?

import java . util . * ; import java . lang . * ; public class GfG { static Boolean isOverflow ( long a , long b ) { if ( a == 0 b == 0 ) return false ; long result = a * b ; if ( a == result / b ) return false ; else return true ; } public static void main ( String argc [ ] ) { long a = Long . parseLong ( "10000000000" ) ; long b = Long . parseLong ( " - 10000000000" ) ; if ( isOverflow ( a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check for possible path in 2D matrix | Java program to find if there is path from top left to right bottom ; To find the path from top left to bottom right ; Directions ; Queue ; Insert the top right corner . ; Until queue is empty ; Mark as visited ; Destination is reached . ; Check all four directions ; Using the direction array ; Not blocked and valid ; Driver Code ; Given array ; Path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class pair { int Item1 , Item2 ; pair ( int f , int s ) { Item1 = f ; Item2 = s ; } } class GFG { static int row = 5 ; static int col = 5 ; static boolean isPath ( int [ ] [ ] arr ) { int [ ] [ ] dir = { { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } , { - 1 , 0 } } ; Queue < pair > q = new LinkedList < > ( ) ; q . add ( new pair ( 0 , 0 ) ) ; while ( q . size ( ) > 0 ) { pair p = ( q . peek ( ) ) ; q . remove ( ) ; arr [ p . Item1 ] [ p . Item2 ] = - 1 ; if ( p == new pair ( row - 1 , col - 1 ) ) return true ; for ( int i = 0 ; i < 4 ; i ++ ) { int a = p . Item1 + dir [ i ] [ 0 ] ; int b = p . Item2 + dir [ i ] [ 1 ] ; if ( a >= 0 && b >= 0 && a < row && b < col && arr [ a ] [ b ] != - 1 ) { q . add ( new pair ( a , b ) ) ; } } } return false ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , 0 , - 1 , - 1 } , { 0 , 0 , 0 , - 1 , 0 } , { - 1 , 0 , - 1 , 0 , - 1 } , { 0 , 0 , - 1 , 0 , 0 } } ; if ( isPath ( arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check for possible path in 2D matrix | Java program to find if there is path from top left to right bottom ; to find the path from top left to bottom right ; set arr [ 0 ] [ 0 ] = 1 ; Mark reachable ( from top left ) nodes in first row and first column . ; Mark reachable nodes in remaining matrix . ; return yes if right bottom index is 1 ; Driver code ; Given array ; path from arr [ 0 ] [ 0 ] to arr [ row ] [ col ]"
How can the above be solved in Java?


"Check for star graph | Java program to find whether given graph is star or not ; define the size of incidence matrix ; function to find star graph ; initialize number of vertex with deg 1 and n - 1 ; check for S1 ; check for S2 ; check for Sn ( n > 2 ) ; Driver code"
How can the above be solved in Java?

import java . io . * ; class GFG { static int size = 4 ; static boolean checkStar ( int mat [ ] [ ] ) { int vertexD1 = 0 , vertexDn_1 = 0 ; if ( size == 1 ) return ( mat [ 0 ] [ 0 ] == 0 ) ; if ( size == 2 ) return ( mat [ 0 ] [ 0 ] == 0 && mat [ 0 ] [ 1 ] == 1 && mat [ 1 ] [ 0 ] == 1 && mat [ 1 ] [ 1 ] == 0 ) ; for ( int i = 0 ; i < size ; i ++ ) { int degreeI = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( mat [ i ] [ j ] == 1 ) degreeI ++ ; if ( degreeI == 1 ) vertexD1 ++ ; else if ( degreeI == size - 1 ) vertexDn_1 ++ ; } return ( vertexD1 == ( size - 1 ) && vertexDn_1 == 1 ) ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } , { 1 , 0 , 0 , 0 } } ; if ( checkStar ( mat ) ) System . out . print ( " Star ▁ Graph " ) ; else System . out . print ( " Not ▁ a ▁ Star ▁ Graph " ) ; } }


"Check given array of size n can represent BST of n levels or not | Java program to Check given array can represent BST or not ; Driver code ; This element can be inserted to the right of the previous element , only if it is greater than the previous element and in the range . ; max remains same , update min ; This element can be inserted to the left of the previous element , only if it is lesser than the previous element and in the range . ; min remains same , update max ; if the loop completed successfully without encountering else condition"
How can the above be solved in Java?

class Solution { public static void main ( String args [ ] ) { int arr [ ] = { 5123 , 3300 , 783 , 1111 , 890 } ; int n = arr . length ; int max = Integer . MAX_VALUE ; int min = Integer . MIN_VALUE ; boolean flag = true ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { min = arr [ i - 1 ] ; } else if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] > min && arr [ i ] < max ) { max = arr [ i - 1 ] ; } else { flag = false ; break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check given array of size n can represent BST of n levels or not | Java program to Check given array can represent BST or not ; structure for Binary Node ; To create a Tree with n levels . We always insert new node to left if it is less than previous value . ; Please refer below post for details of this function . www . geeksforgeeks . org / a - program - to - check - if - a - binary - tree - is - bst - or - not / https : ; Allow only distinct values ; Returns tree if given array of size n can represent a BST of n levels . ; Driver code"
How can the above be solved in Java?

class GFG { static class Node { int key ; Node right , left ; } ; static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . key = num ; temp . left = null ; temp . right = null ; return temp ; } static Node createNLevelTree ( int arr [ ] , int n ) { Node root = newNode ( arr [ 0 ] ) ; Node temp = root ; for ( int i = 1 ; i < n ; i ++ ) { if ( temp . key > arr [ i ] ) { temp . left = newNode ( arr [ i ] ) ; temp = temp . left ; } else { temp . right = newNode ( arr [ i ] ) ; temp = temp . right ; } } return root ; } static boolean isBST ( Node root , int min , int max ) { if ( root == null ) { return true ; } if ( root . key < min root . key > max ) { return false ; } return ( isBST ( root . left , min , ( root . key ) - 1 ) && isBST ( root . right , ( root . key ) + 1 , max ) ) ; } static boolean canRepresentNLevelBST ( int arr [ ] , int n ) { Node root = createNLevelTree ( arr , n ) ; return isBST ( root , Integer . MIN_VALUE , Integer . MAX_VALUE ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 512 , 330 , 78 , 11 , 8 } ; int n = arr . length ; if ( canRepresentNLevelBST ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check given matrix is magic square or not | JAVA program to check whether a given matrix is magic matrix or not ; Returns true if mat [ ] [ ] is magic square , else returns false . ; calculate the sum of the prime diagonal ; the secondary diagonal ; For sums of Rows ; check if every row sum is equal to prime diagonal sum ; For sums of Columns ; check if every column sum is equal to prime diagonal sum ; driver program to test above function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int N = 3 ; static boolean isMagicSquare ( int mat [ ] [ ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) rowSum += mat [ i ] [ j ] ; if ( rowSum != sum ) return false ; } for ( int i = 0 ; i < N ; i ++ ) { int colSum = 0 ; for ( int j = 0 ; j < N ; j ++ ) colSum += mat [ j ] [ i ] ; if ( sum != colSum ) return false ; } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 7 , 6 } , { 9 , 5 , 1 } , { 4 , 3 , 8 } } ; if ( isMagicSquare ( mat ) ) System . out . println ( " Magic ▁ Square " ) ; else System . out . println ( " Not ▁ a ▁ magic " + " ▁ Square " ) ; } }


"Check given string is oddly palindrome or not | Java program for the above approach ; Function to check if the String str is palindromic or not ; Iterate the String str from left and right pointers ; Keep comparing characters while they are same ; If they are not same then return false ; Return true if the String is palindromic ; Function to make String using odd indices of String str ; Functions checks if characters at odd index of the String forms palindrome or not ; Make odd indexed String ; Check for Palindrome ; Driver Code ; Given String ; Function Call"
How can the above be solved in Java?

class GFG { public static boolean isPalindrome ( String str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) { if ( str . charAt ( l ) != str . charAt ( h ) ) { return false ; } l ++ ; h -- ; } return true ; } public static String makeOddString ( String str ) { String odd = " " ; for ( int i = 1 ; i < str . length ( ) ; i += 2 ) { odd += str . charAt ( i ) ; } return odd ; } public static void checkOddlyPalindrome ( String str ) { String odd = makeOddString ( str ) ; if ( isPalindrome ( odd ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String str = " ddwfefwde " ; checkOddlyPalindrome ( str ) ; } }


"Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Function to check if String formed by odd indices is palindromic or not ; Check if length of OddString odd , to consider edge case ; Push odd index character of first half of str in stack ; Middle element of odd length palindromic String is not compared ; If stack is empty then return true ; Driver Code ; Given String"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int oddStringSize = n / 2 ; boolean lengthOdd = ( ( oddStringSize % 2 == 1 ) ? true : false ) ; Stack < Character > s = new Stack < Character > ( ) ; int i = 1 ; int c = 0 ; while ( i < n && c < oddStringSize / 2 ) { s . add ( str . charAt ( i ) ) ; i += 2 ; c ++ ; } if ( lengthOdd ) i = i + 2 ; while ( i < n && s . size ( ) > 0 ) { if ( s . peek ( ) == str . charAt ( i ) ) s . pop ( ) ; else break ; i = i + 2 ; } if ( s . size ( ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , N ) ) System . out . print ( "Yes
"); else System . out . print ( "No
"); } }


"Check given string is oddly palindrome or not | Set 2 | Java program for the above approach ; Functions checks if characters at odd index of the String forms palindrome or not ; Initialise two pointers ; Iterate till left <= right ; If there is a mismatch occurs then return false ; Increment and decrement the left and right pointer by 2 ; Driver Code ; Given String ; Function Call"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean isOddStringPalindrome ( String str , int n ) { int left , right ; if ( n % 2 == 0 ) { left = 1 ; right = n - 1 ; } else { left = 1 ; right = n - 2 ; } while ( left < n && right >= 0 && left < right ) { if ( str . charAt ( left ) != str . charAt ( right ) ) return false ; left += 2 ; right -= 2 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; String s = " aeafacafae " ; if ( isOddStringPalindrome ( s , n ) ) System . out . print ( "Yes
"); else System . out . print ( "No
"); } }


"Check if 2 * K + 1 non | Java program for the above approach ; Function to check if the string S can be obtained by ( K + 1 ) non - empty substrings whose concatenation and concatenation of the reverse of these K strings ; Stores the size of the string ; If n is less than 2 * k + 1 ; Stores the first K characters ; Stores the last K characters ; Reverse the string ; If both the strings are equal ; Driver Code"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static void checkString ( String s , int k ) { int n = s . length ( ) ; if ( 2 * k + 1 > n ) { System . out . println ( " No " ) ; return ; } String a = s . substring ( 0 , k ) ; String b = s . substring ( n - k , n ) ; StringBuffer str = new StringBuffer ( b ) ; str . reverse ( ) ; b = str . toString ( ) ; if ( a . equals ( b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " qwqwq " ; int K = 1 ; checkString ( S , K ) ; } }


"Check if A can be converted to B by reducing with a Prime number | Java implementation to find if it is possible to make a equal to b ; Function to find if it is possible to make A equal to B ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { static boolean isPossible ( int A , int B ) { return ( A - B > 1 ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 4 ; if ( isPossible ( A , B ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if Array forms an increasing | Java program for the above approach ; Function to check if the given array forms an increasing decreasing sequence or vice versa ; Base Case ; First subarray is stricly increasing ; Check for strictly increasing condition & find the break point ; Check for strictly decreasing condition & find the break point ; If i is equal to length of array ; First subarray is strictly Decreasing ; Check for strictly increasing condition & find the break point ; Check for strictly increasing condition & find the break point ; If i is equal to length of array - 1 ; Condition if ar [ 0 ] == ar [ 1 ] ; Driver Code ; Given array arr [ ] ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean canMake ( int n , int ar [ ] ) { if ( n == 1 ) return true ; else { if ( ar [ 0 ] < ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] < ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] > ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else if ( ar [ 0 ] > ar [ 1 ] ) { int i = 1 ; while ( i < n && ar [ i - 1 ] > ar [ i ] ) { i ++ ; } while ( i + 1 < n && ar [ i ] < ar [ i + 1 ] ) { i ++ ; } if ( i >= n - 1 ) return true ; else return false ; } else { for ( int i = 2 ; i < n ; i ++ ) { if ( ar [ i - 1 ] <= ar [ i ] ) return false ; } return true ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; if ( ! canMake ( n , arr ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if Bitwise AND of concatenation of diagonals exceeds that of middle row / column elements of a Binary Matrix | Java program for the above approach ; Function to convert obtained binary representation to decimal value ; Stores the resultant number ; Traverse string arr ; Return the number formed ; Function to count the number of set bits in the number num ; Stores the count of set bits ; Iterate until num > 0 ; Function to check if the given matrix satisfies the given condition or not ; To get P , S , MR , and MC ; Stores decimal equivalents of binary representations ; Gett the number of set bits ; Print the answer ; Driver code"
How can the above be solved in Java?

import java . util . ArrayList ; import java . util . Collections ; class GFG { static int convert ( ArrayList < Integer > p ) { int ans = 0 ; for ( int i : p ) { ans = ( ans << 1 ) | i ; } return ans ; } static int count ( int num ) { int ans = 0 ; while ( num > 0 ) { ans += num & 1 ; num >>= 1 ; } return ans ; } static void checkGoodMatrix ( int mat [ ] [ ] ) { ArrayList < Integer > P = new ArrayList < Integer > ( ) ; ArrayList < Integer > S = new ArrayList < Integer > ( ) ; ArrayList < Integer > MR = new ArrayList < Integer > ( ) ; ArrayList < Integer > MC = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == j ) P . add ( mat [ i ] [ j ] ) ; if ( i + j == mat . length - 1 ) S . add ( mat [ i ] [ j ] ) ; if ( i == Math . floor ( ( mat . length - 1 ) / 2 ) ) MR . add ( mat [ i ] [ j ] ) ; if ( j == Math . floor ( ( mat . length - 1 ) / 2 ) ) MC . add ( mat [ i ] [ j ] ) ; } } Collections . reverse ( S ) ; int P0 = convert ( P ) ; int S0 = convert ( S ) ; int MR0 = convert ( MR ) ; int MC0 = convert ( MC ) ; int setBitsPS = count ( ( P0 & S0 ) ) ; int setBitsMM = count ( ( MR0 & MC0 ) ) ; if ( setBitsPS > setBitsMM ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 0 , 1 } , { 0 , 1 , 1 } } ; checkGoodMatrix ( mat ) ; } }


"Check if Euler Totient Function is same for a given number and twice of that number | Java program of the above approach ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Return if N is odd ; Driver code ; Function call"
How can the above be solved in Java?

class GFG { static int sameEulerTotient ( int N ) { return ( N & 1 ) ; } public static void main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if Euler Totient Function is same for a given number and twice of that number | Java program of the above approach ; Function to find the Euler 's Totient Function ; Initialize result as N ; Consider all prime factors of n and subtract their multiples from result ; Return the count ; Function to check if phi ( n ) is equals phi ( 2 * n ) ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int phi ( int n ) { int result = 1 ; for ( int p = 2 ; p < n ; p ++ ) { if ( __gcd ( p , n ) == 1 ) { result ++ ; } } return result ; } static boolean sameEulerTotient ( int n ) { return phi ( n ) == phi ( 2 * n ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 13 ; if ( sameEulerTotient ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if GCD of all Composite Numbers in an array divisible by K is a Fibonacci Number or not | Java Program for the above approach ; Function to check if a number is composite or not ; Corner cases ; Check if the number is divisible by 2 or 3 or not ; Check if n is a multiple of any other prime number ; Function to check if a number is a Perfect Square or not ; Function to check if a number is a Fibonacci number or not ; If 5 * n ^ 2 + 4 or 5 * n ^ 2 - 4 or both are perfect square ; Function to check if GCD of composite numbers from the array a [ ] which are divisible by k is a Fibonacci number or not ; Traverse the array ; If array element is composite and divisible by k ; Calculate GCD of all elements in compositeset ; If GCD is Fibonacci ; Recursive function to return gcd of a and b ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void ifgcdFibonacci ( int a [ ] , int n , int k ) { Vector < Integer > compositeset = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isComposite ( a [ i ] ) && a [ i ] % k == 0 ) { compositeset . add ( a [ i ] ) ; } } int gcd = compositeset . get ( 0 ) ; for ( int i = 1 ; i < compositeset . size ( ) ; i ++ ) { gcd = __gcd ( gcd , compositeset . get ( i ) ) ; if ( gcd == 1 ) { break ; } } if ( isFibonacci ( gcd ) ) { System . out . print ( " Yes " ) ; return ; } System . out . print ( " No " ) ; return ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 34 , 2 , 4 , 8 , 5 , 7 , 11 } ; int n = arr . length ; int k = 2 ; ifgcdFibonacci ( arr , n , k ) ; } }


"Check if K '0' s can be flipped such that Binary String contains no pair of adjacent '1' s | package whatever import java . io . * ; class GFG { Function to check if k '0' s can be flipped such that the string does not contain any pair of adjacent '1' s ; Store the count of flips ; Variable to iterate the string ; Iterate over characters of the string ; If the current character is '1' , increment i by 2 ; Otherwise , 3 cases arises ; If the current index is the starting index ; If next character is '0' ; Increment i by 1 ; If the current index is the last index ; If previous character is '0' ; For remaining characters ; If both the adjacent characters are '0' ; If cnt is at least K , print " Yes " ; Otherwise , print " No " ; Driver code"
How can the above be solved in Java?

public static void canPlace ( String s , int n , int k ) { int cnt = 0 ; int i = 0 ; while ( i < n ) { if ( s . charAt ( i ) == '1' ) { i += 2 ; } else { if ( i == 0 ) { if ( s . charAt ( i + 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else if ( i == n - 1 ) { if ( s . charAt ( i - 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } else { if ( s . charAt ( i + 1 ) == '0' && s . charAt ( i - 1 ) == '0' ) { cnt ++ ; i += 2 ; } else i ++ ; } } } if ( cnt >= k ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = "10001" ; int K = 1 ; int N = 5 ; canPlace ( S , N , K ) ; } }


"Check if K can be obtained by performing arithmetic operations on any permutation of an Array | Java program for above approach ; Function that finds the string of possible combination of operations ; If only one number left then check for result ; If resultant value is K ; Else return 0 ; Choose all combination of numbers and operators and operate them ; Choose the first two and operate it with ' + ' ; Place it to 0 th position ; Place ( n - 1 ) th element on 1 st position ; Evaluate the expression with current combination ; Now , we have N - 1 elements ; Try ' - ' operation ; Evaluate the expression with current combination ; Try reverse ' - ' ; Evaluate the expression with current combination ; Try ' * ' operation ; Evaluate the expression with current combination ; Try ' / ' ; Evaluate the expression with current combination ; Try reverse ' / ' ; Evaluate the expression with current combination ; Backtracking Step ; Return 0 if there doesnt exist any combination that gives K ; Function that finds the possible combination of operation to get the resultant value K ; Store the resultant operation ; Function call ; Driver Code ; Given array arr [ ] ; Resultant value K ; Function call"
How can the above be solved in Java?

class GFG { static boolean find ( double [ ] v , int n , double dest , String s ) { if ( n == 1 ) { if ( Math . abs ( v [ 0 ] - dest ) <= 0.0000001 ) { System . out . println ( s + String . valueOf ( ( int ) dest ) + " ▁ " ) ; return true ; } return false ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { double a = v [ i ] , b = v [ j ] ; String p = s ; v [ i ] = a + b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) a ) + ' + ' + String . valueOf ( ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a - b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) a ) + ' - ' + String . valueOf ( ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = b - a ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) b ) + ' - ' + String . valueOf ( ( int ) a ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; v [ i ] = a * b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) a ) + ' * ' + String . valueOf ( ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; s = p ; if ( b != 0 ) { v [ i ] = a / b ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) a ) + ' / ' + String . valueOf ( ( int ) b ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; if ( a != 0 ) { v [ i ] = b / a ; v [ j ] = v [ n - 1 ] ; s = ( s + String . valueOf ( ( int ) b ) + ' / ' + String . valueOf ( ( int ) a ) + " ▁ = > ▁ " ) ; if ( find ( v , n - 1 , dest , s ) ) return true ; } s = p ; v [ i ] = a ; v [ j ] = b ; } } return false ; } static void checkPossibleOperation ( double [ ] arr , double K ) { String s = " " ; if ( ! find ( arr , arr . length , K , s ) ) { System . out . println ( " - 1" ) ; } } public static void main ( String [ ] args ) { double [ ] arr = { 2 , 0 , 0 , 2 } ; double K = 4 ; checkPossibleOperation ( arr , K ) ; } }


"Check if K distinct array elements form an odd sum | Java program for the above approach ; Function to return if odd sum is possible or not ; Stores distinct odd elements ; Stores distinct even elements ; Iterating through given array ; If element is even ; If element is odd ; If atleast K elements in the array are odd ; Check for all odd frequencies of odd elements whether sufficient even numbers are present or not ; Count of even numbers required ; If required even numbers are present in the array ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean oddSum ( int [ ] A , int N , int K ) { HashSet < Integer > Odd = new HashSet < Integer > ( ) ; HashSet < Integer > Even = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { Even . add ( A [ i ] ) ; } else { Odd . add ( A [ i ] ) ; } } if ( Odd . size ( ) >= K ) return true ; boolean flag = false ; for ( int i = 1 ; i < K ; i += 2 ) { int needed = K - i ; if ( needed <= Even . size ( ) ) { return true ; } } return flag ; } public static void main ( String [ ] args ) { int K = 5 ; int [ ] A = { 12 , 1 , 7 , 7 , 26 , 18 } ; int N = 3 ; if ( oddSum ( A , N , K ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }


"Check if LCM of array elements is divisible by a prime number or not | Java program to find LCM of array of number is divisible by a prime number k or not ; Function to check any number of array is divisible by k or not ; If any array element is divisible by k , then LCM of whole array should also be divisible . ; Driver Code"
How can the above be solved in Java?

import java . lang . * ; import java . util . * ; class GFG { static boolean func ( int a [ ] , int k ) { for ( int i = 0 ; i < a . length ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int [ ] a = { 14 , 27 , 38 , 76 , 84 } ; int k = 19 ; boolean res = func ( a , k ) ; System . out . println ( res ) ; } }


"Check if Matrix sum is prime or not | Java implementation to check if the sum of matrix is prime or not ; Function to check whether a number is prime or not ; Corner case ; Check from 2 to n - 1 ; Function for to find the sum of the given matrix ; Driver Code"
How can the above be solved in Java?

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


"Check if N and M can be made equal by increasing N by A and decreasing M by B | Java program to check if two numbers can be made equal by increasing the first by a and decreasing the second by b ; Function to whether the numbers can be made equal or not ; Check whether the numbers can reach an equal point or not ; M and N cannot be made equal by increasing M and decreasing N when M is already greater than N ; Driver code"
How can the above be solved in Java?

class GFG { static boolean checkEqualNo ( int m , int n , int a , int b ) { if ( m <= n ) { if ( ( n - m ) % ( a + b ) == 0 ) { return true ; } else { return false ; } } else { return false ; } } public static void main ( String [ ] args ) { int M = 2 , N = 8 ; int A = 3 , B = 3 ; if ( checkEqualNo ( M , N , A , B ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if N can be converted to the form K power K by the given operation | Java implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if N can be converted to K power K ; Check if n is of the form k ^ k ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Return the result ; Function to check the above method ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7"
How can the above be solved in Java?

import java . util . * ; class GFG { static HashSet < Integer > kPowKform = new HashSet < Integer > ( ) ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . contains ( n ) ) return 1 ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) == 1 ) { answer = 1 ; break ; } } x /= 10 ; } return answer ; } static void canBeConverted ( int n ) { if ( func ( n ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 90 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ; } }


"Check if N can be converted to the form K power K by the given operation | Java implementation to Check whether a given number N can be converted to the form K power K by the given operation ; Function to check if a number is converatable ; Check if n is of the form k ^ k ; Check if the subproblem has been solved before ; Iterate through each digit of n ; Check if it is possible to obtain number of given form ; Reduce the number each time ; Store and return the answer to this subproblem ; Function to check the above method ; Initialise the dp table ; Check if conversion if possible ; Driver code ; Pre store K power K form of numbers Loop till 8 , because 8 ^ 8 > 10 ^ 7"
How can the above be solved in Java?

import java . util . * ; class GFG { static HashSet < Integer > kPowKform = new HashSet < > ( ) ; static int [ ] dp = new int [ 100005 ] ; static int func ( int n ) { if ( n <= 0 ) return 0 ; if ( kPowKform . contains ( n ) ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int answer = 0 ; int x = n ; while ( x > 0 ) { int d = x % 10 ; if ( d != 0 ) { if ( func ( n - d * d ) != 0 ) { answer = 1 ; break ; } } x /= 10 ; } return dp [ n ] = answer ; } static void canBeConverted ( int n ) { for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = - 1 ; if ( func ( n ) == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 13 ; for ( int i = 1 ; i <= 8 ; i ++ ) { int val = 1 ; for ( int j = 1 ; j <= i ; j ++ ) val *= i ; kPowKform . add ( val ) ; } canBeConverted ( N ) ; } }


"Check if N can be expressed as product of 3 distinct numbers | Java program to find the three numbers ; function to find 3 distinct number with given product ; Declare a vector to store divisors ; store all divisors of number in array ; store all the occurence of divisors ; check if n is not equals to - 1 then n is also a prime factor ; Initialize the variables with 1 ; check for first number a ; check for second number b ; check for third number c ; check for all unwanted condition ; Driver function"
How can the above be solved in Java?

 import java . util . * ; class GFG { static void getnumbers ( int n ) { Vector < Integer > divisor = new Vector < Integer > ( ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { while ( n % i == 0 ) { divisor . add ( i ) ; n /= i ; } } if ( n != 1 ) { divisor . add ( n ) ; } int a , b , c , size ; a = b = c = 1 ; size = divisor . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { if ( a == 1 ) { a = a * divisor . get ( i ) ; } else if ( b == 1 b == a ) { b = b * divisor . get ( i ) ; } else { c = c * divisor . get ( i ) ; } } if ( a == 1 b == 1 c == 1 a == b b == c a == c ) { System . out . print ( " - 1" + "
"); } else { System . out . print ( a + " ▁ " + b + " ▁ " + c + "
"); } } public static void main ( String [ ] args ) { int n = 64 ; getnumbers ( n ) ; } }


"Check if N can be represented as sum of distinct powers of 3 | Java program for the above approach ; Function to check whether the given N can be represented as the sum of the distinct powers of 3 ; Iterate until N is non - zero ; Termination Condition ; Right shift ternary bits by 1 for the next digit ; If N can be expressed as the sum of perfect powers of 3 ; Driver Code"
How can the above be solved in Java?

class GFG { public static void DistinctPowersOf3 ( int N ) { while ( N > 0 ) { if ( N % 3 == 2 ) { System . out . println ( " No " ) ; return ; } N /= 3 ; } System . out . println ( " Yes " ) ; } public static void main ( String args [ ] ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }


"Check if N can be represented as sum of distinct powers of 3 | Java program for the above approach ; Function to try all permutations of distinct powers ; Base Case ; If the distinct powers sum is obtained ; Otherwise ; If current element not selected in power [ ] ; If current element selected in power [ ] ; Return 1 if any permutation found ; Function to check the N can be represented as the sum of the distinct powers of 3 ; Stores the all distincts powers of three to [ 0 , 15 ] ; Function Call ; print ; Driven Code"
How can the above be solved in Java?

class GFG { public static boolean PermuteAndFind ( int [ ] power , int idx , int SumSoFar , int target ) { if ( idx == power . length ) { if ( SumSoFar == target ) return true ; return false ; } boolean select = PermuteAndFind ( power , idx + 1 , SumSoFar , target ) ; boolean notselect = PermuteAndFind ( power , idx + 1 , SumSoFar + power [ idx ] , target ) ; return ( select notselect ) ; } public static void DistinctPowersOf3 ( int N ) { int [ ] power = new int [ 16 ] ; power [ 0 ] = 1 ; for ( int i = 1 ; i < 16 ; i ++ ) power [ i ] = 3 * power [ i - 1 ] ; boolean found = PermuteAndFind ( power , 0 , 0 , N ) ; if ( found == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 91 ; DistinctPowersOf3 ( N ) ; } }


"Check if N can be represented as sum of integers chosen from set { A , B } | Java program to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; Function to find if number N can be represented as sum of a ' s ▁ and ▁ b ' s ; base condition ; if x is already visited ; set x as possible ; recursive call ; Driver program"
How can the above be solved in Java?

import java . util . * ; class solution { static void checkIfPossibleRec ( int x , int a , int b , boolean isPossible [ ] , int n ) { if ( x > n ) return ; if ( isPossible [ x ] ) return ; isPossible [ x ] = true ; checkIfPossibleRec ( x + a , a , b , isPossible , n ) ; checkIfPossibleRec ( x + b , a , b , isPossible , n ) ; } static boolean checkPossible ( int n , int a , int b ) { boolean isPossible [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) isPossible [ i ] = false ; checkIfPossibleRec ( 0 , a , b , isPossible , n ) ; return isPossible [ n ] ; } public static void main ( String args [ ] ) { int a = 3 , b = 7 , n = 8 ; if ( checkPossible ( a , b , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if N can be represented as sum of positive integers containing digit D at least once | Java approach for the above approach ; Function to check if N contains digit D in it ; Iterate until N is positive ; Find the last digit ; If the last digit is the same as digit D ; Return false ; Function to check if the value of N can be represented as sum of integers having digit d in it ; Iterate until N is positive ; Check if N contains digit D or not ; Subtracting D from N ; Return false ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean findDigit ( int N , int D ) { while ( N > 0 ) { int a = N % 10 ; if ( a == D ) { return true ; } N /= 10 ; } return false ; } static boolean check ( int N , int D ) { while ( N > 0 ) { if ( findDigit ( N , D ) == true ) { return true ; } N -= D ; } return false ; } public static void main ( String [ ] args ) { int N = 24 ; int D = 7 ; if ( check ( N , D ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }


"Check if N can be represented as sum of squares of two consecutive integers | Java implementation to check that a number is sum of squares of 2 consecutive numbers or not ; Function to check that the a number is sum of squares of 2 consecutive numbers or not ; Condition to check if the a number is sum of squares of 2 consecutive numbers or not ; Driver code ; Function call"
How can the above be solved in Java?

import java . lang . Math ; class GFG { public static boolean isSumSquare ( int N ) { double n = ( 2 + Math . sqrt ( 8 * N - 4 ) ) / 2 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int i = 13 ; if ( isSumSquare ( i ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }


"Check if N contains all digits as K in base B | Java implementation of above approach ; Function to print the number of digits ; Calculate log using base change property and then take its floor and then add 1 ; Return the output ; Function that returns true if n contains all one 's in base b ; Calculate the sum ; Driver code ; Given number N ; Given base B ; Given digit K ; Function call"
How can the above be solved in Java?

import java . util . * ; class GFG { static int findNumberOfDigits ( int n , int base ) { int dig = ( ( int ) Math . floor ( Math . log ( n ) / Math . log ( base ) ) + 1 ) ; return dig ; } static boolean isAllKs ( int n , int b , int k ) { int len = findNumberOfDigits ( n , b ) ; int sum = k * ( 1 - ( int ) Math . pow ( b , len ) ) / ( 1 - b ) ; return sum == n ; } public static void main ( String [ ] args ) { int N = 13 ; int B = 3 ; int K = 1 ; if ( isAllKs ( N , B , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if N is a Balanced Prime number or not | Java program to check if a given number is Balanced prime ; Utility function to check if a number is prime or not ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function that returns true if n is a Balanced prime ; If n is not a prime number or n is the first prime then return false ; Initialize previous_prime to n - 1 and next_prime to n + 1 ; Find next prime number ; Find previous prime number ; Arithmetic mean ; If n is a weak prime ; 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 += 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isBalancedPrime ( int n ) { if ( ! isPrime ( n ) n == 2 ) return false ; int previous_prime = n - 1 ; int next_prime = n + 1 ; while ( ! isPrime ( next_prime ) ) next_prime ++ ; while ( ! isPrime ( previous_prime ) ) previous_prime -- ; int mean = ( previous_prime + next_prime ) / 2 ; if ( n == mean ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 53 ; if ( isBalancedPrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if N is divisible by a number which is composed of the digits from the set { A , B } | Java program to find if number N is divisible by a number that contains only a and b as it 's digits ; Function to check whether n is divisible by a number whose digits are either a or b ; base condition ; recursive call ; Check for all numbers beginning with ' a ' or ' b ' ; Driver program"
How can the above be solved in Java?

import java . util . * ; class solution { static boolean isDivisibleRec ( int x , int a , int b , int n ) { if ( x > n ) return false ; if ( n % x == 0 ) return true ; return ( isDivisibleRec ( x * 10 + a , a , b , n ) || isDivisibleRec ( x * 10 + b , a , b , n ) ) ; } static boolean isDivisible ( int a , int b , int n ) { return isDivisibleRec ( a , a , b , n ) || isDivisibleRec ( b , a , b , n ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 5 , n = 53 ; if ( isDivisible ( a , b , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if N leaves only distinct remainders on division by all values up to K | Java program to check if all remainders are distinct or not ; Function to check and return if all remainders are distinct ; Stores the remainder ; Calculate the remainder ; If remainder already occurred ; Insert into the set ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean is_distinct ( long n , long k ) { HashSet < Long > s = new HashSet < Long > ( ) ; for ( int i = 1 ; i <= k ; i ++ ) { long tmp = n % i ; if ( s . contains ( tmp ) ) { return false ; } s . add ( tmp ) ; } return true ; } public static void main ( String [ ] args ) { long N = 5 , K = 3 ; if ( is_distinct ( N , K ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }


"Check if N numbers with Even Sum can be selected from a given Array | Java efficient program to check if N numbers with Odd sum can be selected from the given array ; Function to check if an odd sum can be made using N integers from the array ; Initialize odd and even counts ; Iterate over the array to count the no . of even and odd integers ; If element is odd ; If element is even ; Check if even_freq is more than N ; If odd_freq is odd ; Consider even count of odd ; Calculate even required ; If even count is less than required count ; Calculate even required ; If even count is less than required count ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static boolean checkEvenSum ( int arr [ ] , int N , int size ) { int even_freq = 0 , odd_freq = 0 ; for ( int i = 0 ; i < size ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd_freq ++ ; else even_freq ++ ; } if ( even_freq >= N ) return true ; else { if ( odd_freq % 2 == 1 ) { int taken = odd_freq - 1 ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } else { int taken = odd_freq ; int req = N - taken ; if ( even_freq < req ) { return false ; } else return true ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 2 , 3 , 4 , 18 , 7 , 7 , 6 } ; int size = arr . length ; int N = 5 ; if ( checkEvenSum ( arr , N , size ) ) System . out . print ( " Yes " + "
"); else System . out . print ( " No " + "
"); } }


"Check if Pascal 's Triangle is possible with a complete layer by using numbers upto N | Java program for the above approach ; Function to check if Pascaltriangle can be made by N integers ; Find X ; If x is integer ; Driver Code ; Given number N ; Function call"
How can the above be solved in Java?

class GFG { static void checkPascaltriangle ( int N ) { double x = ( Math . sqrt ( 8 * N + 1 ) - 1 ) / 2 ; if ( Math . ceil ( x ) - x == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 10 ; checkPascaltriangle ( N ) ; } }


"Check if String formed by first and last X characters of a String is a Palindrome | Java program for the above approach ; Function to check whether the first x characters of both string str and reversed string str are same or not ; Length of the string str ; Traverse over the string while first and last x characters are not equal ; If the current and n - k - 1 from last character are not equal ; Finally , print true ; Driver Code ; Given Input ; Function Call"
How can the above be solved in Java?

import java . io . * ; class GFG { public static void isEqualSubstring ( String str , int x ) { int n = str . length ( ) ; int i = 0 ; while ( i < n && i < x ) { if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) { System . out . println ( " false " ) ; return ; } i ++ ; } System . out . println ( " true " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; int x = 3 ; isEqualSubstring ( str , x ) ; } }


"Check if Sum and XOR of all elements of array is equal | Java Implementation to Check if Sum and XOR of all Elements of Array is equal ; Function to Check if Sum and XOR of all elements of array is equal ; Sum and XOR of all elements ; Checking Sum and XOR to be equal ; Driver Function ; Check Sum and XOR is equal"
How can the above be solved in Java?

class GFG { static void equal_xor_sum ( int arr [ ] , int n ) { int Sum = 0 ; int Xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { Sum = Sum + arr [ i ] ; Xor = Xor ^ arr [ i ] ; } if ( Sum == Xor ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 7 , 10 } ; int n = arr . length ; equal_xor_sum ( arr , n ) ; } }


"Check if X and Y can be made zero by using given operation any number of times | Java implementation of the approach ; Function to check if X and Y can be made equal to zero by using given operation any number of times ; Check for the two conditions ; Driver code"
How can the above be solved in Java?

class GFG { static void ifPossible ( int X , int Y ) { if ( X > Y ) swap ( X , Y ) ; if ( ( X + Y ) % 5 == 0 && 3 * X >= 2 * Y ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int X = 33 , Y = 27 ; ifPossible ( X , Y ) ; } }


"Check if X and Y elements can be selected from two arrays respectively such that the maximum in X is less than the minimum in Y | Java program for the above approach ; Function to check if it is possible to choose X and Y elements from a [ ] and b [ ] such that maximum element among X element is less than minimum element among Y elements ; Check if there are atleast X elements in arr1 [ ] and atleast Y elements in arr2 [ ] ; Sort arrays in ascending order ; Check if ( X - 1 ) - th element in arr1 [ ] is less than from M - Yth element in arr2 [ ] ; Return false ; Driver Code ; Function Call"
How can the above be solved in Java?

import java . io . * ; import java . util . * ; class GFG { static String check ( int [ ] a , int [ ] b , int Na , int Nb , int k , int m ) { if ( Na < k Nb < m ) return " No " ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; if ( a [ k - 1 ] < b [ Nb - m ] ) { return " Yes " ; } return " No " ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 3 , 4 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; int X = 2 , Y = 1 ; System . out . println ( check ( arr1 , arr2 , N , M , X , Y ) ) ; } }


"Check if X can be converted to Y by converting to 3 * ( X / 2 ) or X | Java program for the above approach ; Function to check if X can be made equal to Y by converting X to ( 3 * X / 2 ) or ( X - 1 ) ; Conditions for possible conversion ; Otherwise , conversion is not possible ; Driver Code"
How can the above be solved in Java?

class GFG { static void check ( int X , int Y ) { if ( X > 3 ) { System . out . print ( " Yes " ) ; } else if ( X == 1 && Y == 1 ) { System . out . print ( " Yes " ) ; } else if ( X == 2 && Y <= 3 ) { System . out . print ( " Yes " ) ; } else if ( X == 3 && Y <= 3 ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int X = 6 , Y = 8 ; check ( X , Y ) ; } }


"Check if X can be reduced to 0 in exactly T moves by substracting D or 1 from it | Java program for the above approach ; Function to check the above problem condition ; Check for base cases ; Check if D - 1 is a divisor of X - T ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static int possibleReachingSequence ( int X , int D , int T ) { if ( X < T ) { System . out . println ( " NO " ) ; return 0 ; } if ( T * D < X ) { System . out . println ( " NO " ) ; return 0 ; } if ( ( X - T ) % ( D - 1 ) == 0 ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } return 0 ; } public static void main ( String args [ ] ) { int X = 10 , D = 3 , T = 6 ; possibleReachingSequence ( X , D , T ) ; } }


"Check if X can give change to every person in the Queue | Java program to check whether X can give change to every person in the Queue ; Function to check if every person will get the change from X ; To count the 5 $ and 10 & notes ; Serve the customer in order ; Increase the number of 5 $ note by one ; decrease the number of note 5 $ and increase 10 $ note by one ; decrease 5 $ and 10 $ note by one ; decrease 5 $ note by three ; Driver Code ; queue of customers with available notes . ; Calling function"
How can the above be solved in Java?

import java . io . * ; class GFG { static int isChangeable ( int notes [ ] , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } else { if ( fiveCount > 0 && tenCount > 0 ) { fiveCount -- ; tenCount -- ; } else if ( fiveCount >= 3 ) { fiveCount -= 3 ; } else return 0 ; } } return 1 ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 5 , 5 , 10 , 20 } ; int n = a . length ; if ( isChangeable ( a , n ) > 0 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }


"Check if a Binary String can be converted to another by reversing substrings consisting of even number of 1 s | Java program for the above approach ; Function to check if string A can be transformed to string B by reversing substrings of A having even number of 1 s ; Store the size of string A ; Store the size of string B ; Store the count of 1 s in A and B ; Stores cntA for string A and cntB for string B ; Traverse the string A ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1A or even1A depending whether count1A is odd or even ; Traverse the string B ; If current character is 1 ; Increment 1 s count ; Otherwise , update odd1B or even1B depending whether count1B is odd or even ; If the condition is satisfied ; If true , print Yes ; Otherwise , print No ; Driver Code ; Function Call"
How can the above be solved in Java?

class GFG { public static void canTransformStrings ( String A , String B ) { int n1 = A . length ( ) ; int n2 = B . length ( ) ; int count1A = 0 , count1B = 0 ; int odd1A = 0 , odd1B = 0 ; int even1A = 0 , even1B = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( A . charAt ( i ) == '1' ) count1A ++ ; else { if ( ( count1A & 1 ) == 1 ) odd1A ++ ; else even1A ++ ; } } for ( int i = 0 ; i < n2 ; i ++ ) { if ( B . charAt ( i ) == '1' ) count1B ++ ; else { if ( ( count1B & 1 ) == 1 ) odd1B ++ ; else even1B ++ ; } } if ( count1A == count1B && odd1A == odd1B && even1A == even1B ) { System . out . print ( " Yes " ) ; } else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String A = "10011" , B = "11100" ; canTransformStrings ( A , B ) ; } }


"Check if a Binary String can be sorted in decreasing order by removing non | Java program for the above approach ; Function to sort the given string in decreasing order by removing the non adjacent characters ; Keeps the track whether the string can be sorted or not ; Traverse the given string S ; Check if S [ i ] and S [ i + 1 ] are both '1' ; Traverse the string S from the indices i to 0 ; If S [ j ] and S [ j + 1 ] is equal to 0 ; Mark flag false ; If flag is 0 , then it is not possible to sort the string ; Otherwise ; Driver Code"
How can the above be solved in Java?

import java . util . * ; class GFG { static String canSortString ( String S , int N ) { int flag = 1 ; int i , j ; for ( i = N - 2 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '1' && S . charAt ( i + 1 ) == '1' ) { break ; } } for ( j = i ; j >= 0 ; j -- ) { if ( S . charAt ( j ) == '0' && S . charAt ( j + 1 ) == '0' ) { flag = 0 ; break ; } } if ( flag == 0 ) { return " No " ; } else { return " Yes " ; } } public static void main ( String [ ] args ) { String S = "10101011011" ; int N = S . length ( ) ; System . out . println ( canSortString ( S , N ) ) ; } }


"Check if a Binary String can be split into disjoint subsequences which are equal to "010" | Java program for the above approach ; Function to check if the given string can be partitioned into a number of subsequences all of which are equal to "010" ; Store the size of the string ; Store the count of 0 s and 1 s ; Traverse the given string in the forward direction ; If the character is '0' , increment count_0 by 1 ; If the character is '1' increment count_1 by 1 ; If at any point , count_1 > count_0 , return false ; If count_0 is not equal to twice count_1 , return false ; Reset the value of count_0 and count_1 ; Traverse the string in the reverse direction ; If the character is '0' increment count_0 ; If the character is '1' increment count_1 ; If count_1 > count_0 , return false ; Driver Code ; Given string ; Function Call"
How can the above be solved in Java?

public class MyClass { static boolean isPossible ( String s ) { int n = s . length ( ) ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } if ( count_0 != ( 2 * count_1 ) ) return false ; count_0 = 0 ; count_1 = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s . charAt ( i ) == '0' ) ++ count_0 ; else ++ count_1 ; if ( count_1 > count_0 ) return false ; } return true ; } public static void main ( String args [ ] ) { String s = "010100" ; if ( isPossible ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }


"Check if a Binary Tree ( not BST ) has duplicate values | Java Program to check duplicates in Binary Tree ; Function that used HashSet to find presence of duplicate nodes ; If tree is empty , there are no duplicates . ; If current node 's data is already present.  ; Insert current node ; Recursively check in left and right subtrees . ; To check if tree has duplicates ; A binary tree Node has data , pointer to left child and a pointer to right child"
How can the above be solved in Java?

import java . util . HashSet ; public class CheckDuplicateValues { public static boolean checkDupUtil ( Node root , HashSet < Integer > s ) { if ( root == null ) return false ; if ( s . contains ( root . data ) ) return true ; s . add ( root . data ) ; return checkDupUtil ( root . left , s ) || checkDupUtil ( root . right , s ) ; } public static boolean checkDup ( Node root ) { HashSet < Integer > s = new HashSet < > ( ) ; return checkDupUtil ( root , s ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; if ( checkDup ( root ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } } class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ;


"Check if a Binary Tree consists of a pair of leaf nodes with sum K | Java program to implement the above approach ; Stores if a pair exists or not ; Struct binary tree node ; Creates a new node ; Function to check if a pair of leaf nodes exists with sum K ; Checks if root is null ; Checks if the current node is a leaf node ; Checks for a valid pair of leaf nodes ; Insert value of current node into the set ; Traverse left and right subtree ; Driver Code ; Construct binary tree ; Stores the leaf nodes"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean pairFound = false ; static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void pairSum ( Node root , int target , HashSet < Integer > S ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( S . contains ( target - root . data ) ) { System . out . print ( target - root . data + " ▁ " + root . data ) ; pairFound = true ; return ; } else S . add ( root . data ) ; } pairSum ( root . left , target , S ) ; pairSum ( root . right , target , S ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; HashSet < Integer > S = new HashSet < Integer > ( ) ; int K = 13 ; pairSum ( root , K , S ) ; if ( pairFound == false ) System . out . print ( " - 1" ) ; } }


"Check if a Binary Tree contains duplicate subtrees of size 2 or more | Java program to find if there is a duplicate sub - tree of size 2 or more . ; A binary tree Node has data , pointer to left child and a pointer to right child ; This function returns empty string if tree contains a duplicate subtree of size 2 or more . ; If current node is NULL , return marker ; If left subtree has a duplicate subtree . ; Do same for right subtree ; Serialize current subtree ; If current subtree already exists in hash table . [ Note that size of a serialized tree with single node is 3 as it has two marker nodes . ; Function to find if the Binary Tree contains duplicate subtrees of size 2 or more ; Driver program to test above functions"
How can the above be solved in Java?

import java . util . HashSet ; public class Main { static char MARKER = ' $ ' ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static String dupSubUtil ( Node root , HashSet < String > subtrees ) { String s = " " ; if ( root == null ) return s + MARKER ; String lStr = dupSubUtil ( root . left , subtrees ) ; if ( lStr . equals ( s ) ) return s ; String rStr = dupSubUtil ( root . right , subtrees ) ; if ( rStr . equals ( s ) ) return s ; s = s + root . data + lStr + rStr ; if ( s . length ( ) > 3 && subtrees . contains ( s ) ) return " " ; subtrees . add ( s ) ; return s ; } public static String dupSub ( Node root ) { HashSet < String > subtrees = new HashSet < > ( ) ; return dupSubUtil ( root , subtrees ) ; } public static void main ( String args [ ] ) { Node root = new Node ( ' A ' ) ; root . left = new Node ( ' B ' ) ; root . right = new Node ( ' C ' ) ; root . left . left = new Node ( ' D ' ) ; root . left . right = new Node ( ' E ' ) ; root . right . right = new Node ( ' B ' ) ; root . right . right . right = new Node ( ' E ' ) ; root . right . right . left = new Node ( ' D ' ) ; String str = dupSub ( root ) ; if ( str . equals ( " " ) ) System . out . print ( " ▁ Yes ▁ " ) ; else System . out . print ( " ▁ No ▁ " ) ; } }


"Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | Java Program for the above approach ; Structure of Tree node ; Function to create new Tree node ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Driver Code ; Construct a Binary Tree ; Function Call"
How can the above be solved in Java?

import java . util . * ; class GFG { static class Node { int val ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static boolean checkEvenOddLevel ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; while ( ! q . isEmpty ( ) ) { ArrayList < Integer > list = new ArrayList < > ( ) ; int size = q . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q . poll ( ) ; list . add ( node . val ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } if ( level % 2 == 0 ) { for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { if ( list . get ( i + 1 ) > list . get ( i ) ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { if ( list . get ( i + 1 ) < list . get ( i ) ) continue ; return false ; } } level ++ ; } return true ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 2 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 7 ) ; root . right . right = newNode ( 11 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if a Float value is equivalent to an Integer value | Java program to implement the above approach ; Function to check if N is equivalent to an integer ; Convert float value of N to integer ; If N is not equivalent to any integer ; Driver code"
How can the above be solved in Java?

import java . util . * ; class GFG { static boolean isInteger ( double N ) { int X = ( int ) N ; double temp2 = N - X ; if ( temp2 > 0 ) { return false ; } return true ; } public static void main ( String [ ] args ) { double N = 1.5 ; if ( isInteger ( N ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }


"Check if a HexaDecimal number is Even or Odd | Java code to check if a HexaDecimal number is Even or Odd ; Check if the number is odd or even ; check if the last digit is either '0' , '2' , '4' , '6' , '8' , ' A ' ( = 10 ) , ' C ' ( = 12 ) or ' E ' ( = 14 ) ; Driver code"
How can the above be solved in Java?

class GFG { static String even_or_odd ( String N ) { int len = N . length ( ) ; if ( N . charAt ( len - 1 ) == '0' || N . charAt ( len - 1 ) == '2' || N . charAt ( len - 1 ) == '4' || N . charAt ( len - 1 ) == '6' || N . charAt ( len - 1 ) == '8' || N . charAt ( len - 1 ) == ' A ' || N . charAt ( len - 1 ) == ' C ' || N . charAt ( len - 1 ) == ' E ' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void main ( String [ ] args ) { String N = " AB3454D " ; System . out . print ( even_or_odd ( N ) ) ; } }


"Check if a Linked List is Pairwise Sorted | Java program to check if linked list is pairwise sorted ; A linked list node ; Function to check if linked list is pairwise sorted ; Traverse further only if there are at - least two nodes left ; Function to add a node at the beginning of Linked List ; allocate node ; put in the data ; link the old list off the new node ; move the head to point to the new node ; Driver Code ; The constructed linked list is : 10.15 . 9.9 .1 .5"
How can the above be solved in Java?

class GFG { static class Node { int data ; Node next ; } ; static Node start ; static boolean isPairWiseSorted ( Node head ) { boolean flag = true ; Node temp = head ; while ( temp != null && temp . next != null ) { if ( temp . data > temp . next . data ) { flag = false ; break ; } temp = temp . next . next ; } return flag ; } static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; start = head_ref ; } public static void main ( String [ ] args ) { start = null ; push ( start , 5 ) ; push ( start , 1 ) ; push ( start , 9 ) ; push ( start , 9 ) ; push ( start , 15 ) ; push ( start , 10 ) ; if ( isPairWiseSorted ( start ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }


"Check if a M | Java program to check if M - th fibonacci divides N - th fibonacci ; exceptional case for F ( 2 ) ; if none of the above cases , hence not divisible ; Driver Code"
How can the above be solved in Java?

import java . io . * ; class GFG { static void check ( int n , int m ) { if ( n == 2 m == 2 n % m == 0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int m = 3 , n = 9 ; check ( n , m ) ; } }


"Check if a Matrix is Bitonic or not | Java program to check if a matrix is Bitonic or not ; Function to check if an array is Bitonic or not ; Check for increasing sequence ; Check for decreasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code"
How can the above be solved in Java?

class GFG { final static int N = 3 , M = 3 ; static boolean checkBitonic ( int arr [ ] , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int arr [ ] [ ] ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkBitonic ( arr [ i ] , M ) ) { System . out . println ( " NO " ) ; return ; } } for ( int i = 0 ; i < N ; i ++ ) { int temp [ ] = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j ] [ i ] ; } if ( ! checkBitonic ( temp , N ) ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 2 , 3 } , { 3 , 4 , 5 } , { 2 , 6 , 4 } } ; check ( m ) ; } }


"Check if a Matrix is Reverse Bitonic or Not | Java Program to check if a matrix is Reverse Bitonic or not ; Function to check if an array is Reverse Bitonic or not ; Check for decreasing sequence ; Check for increasing sequence ; Function to check whether given matrix is bitonic or not ; Check row - wise ; Check column wise ; Generate an array consisting of elements of the current column ; Driver Code"
How can the above be solved in Java?

 import java . util . * ; class GFG { static int N = 3 , M = 3 ; static boolean checkReverseBitonic ( int arr [ ] , int n ) { int i , j , f = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { f = 1 ; break ; } } if ( i == n ) return true ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) continue ; if ( arr [ i ] == arr [ i - 1 ] ) return false ; else { if ( f == 1 ) return false ; } } return true ; } static void check ( int arr [ ] [ ] ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! checkReverseBitonic ( arr [ i ] , M ) ) { System . out . print ( " No " + "
"); return ; } } for ( int i = 0 ; i < N ; i ++ ) { int temp [ ] = new int [ N ] ; for ( int j = 0 ; j < N ; j ++ ) { temp [ j ] = arr [ j ] [ i ] ; } if ( ! checkReverseBitonic ( temp , N ) ) { System . out . print ( " No " + "
"); return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 2 , 3 , 4 } , { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; check ( m ) ; } }


"Check if a N base number is Even or Odd | Java code to check if a Octal number is Even or Odd ; To return value of a char . ; Function to convert a number from N base to decimal ; power of base ; Decimal equivalent is str [ len - 1 ] * 1 + str [ len - 1 ] * base + str [ len - 1 ] * ( base ^ 2 ) + ... ; A digit in input number must be less than number 's base ; Returns true if n is even , else odd ; Driver code"
How can the above be solved in Java?

import java . io . * ; class Main { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { System . out . println ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } public static boolean isEven ( String num , int N ) { int deci = toDeci ( num , N ) ; return ( deci % 2 == 0 ) ; } public static void main ( String [ ] args ) { String num = "11A " ; int N = 16 ; if ( isEven ( num , N ) ) { System . out . println ( " Even " ) ; } else { System . out . println ( " Odd " ) ; } } }


"Check if a Number is Odd or Even using Bitwise Operators | Java program to check for even or odd using Bitwise XOR operator ; Returns true if n is even , else odd ; n ^ 1 is n + 1 , then even , else odd ; Driver code"
How can the above be solved in Java?

class GFG { static boolean isEven ( int n ) { if ( ( n ^ 1 ) == n + 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . print ( isEven ( n ) == true ? " Even " : " Odd " ) ; } }


"Check if a Palindromic String can be formed by concatenating Substrings of two given Strings | Java program to implement the above approach ; Function to check if a palindromic string can be formed from the substring of given strings ; Boolean array to mark presence of characters ; Check if any of the character of str2 is already marked ; If a common character is found ; If no common character is found ; Driver code"
How can the above be solved in Java?

import java . util . Arrays ; class GFG { public static boolean check ( String str1 , String str2 ) { boolean [ ] mark = new boolean [ 26 ] ; Arrays . fill ( mark , false ) ; int n = str1 . length ( ) , m = str2 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ str1 . charAt ( i ) - ' a ' ] = true ; } for ( int i = 0 ; i < m ; i ++ ) { if ( mark [ str2 . charAt ( i ) - ' a ' ] ) return true ; } return false ; } public static void main ( String [ ] args ) { String str1 = " abca " , str2 = " efad " ; if ( check ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }