import java . util . * ; class GFG { static boolean isOperand ( char x ) { return ( x >= ' a ' && x <= ' z ' ) || ( x >= ' A ' && x <= ' Z ' ) ; } static String getInfix ( String exp ) { Stack < String > s = new Stack < String > ( ) ; for ( int i = 0 ; i < exp . length ( ) ; i ++ ) { if ( isOperand ( exp . charAt ( i ) ) ) { s . push ( exp . charAt ( i ) + " " ) ; } else { String op1 = s . peek ( ) ; s . pop ( ) ; String op2 = s . peek ( ) ; s . pop ( ) ; s . push ( " ( " + op2 + exp . charAt ( i ) + op1 + " ) " ) ; } } return s . peek ( ) ; } public static void main ( String args [ ] ) { String exp = " ab * c + " ; System . out . println ( getInfix ( exp ) ) ; } }
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 ) ; } }
import java . util . Stack ; import java . util . Arrays ; public class GFG { static void calculateSpan ( int price [ ] , int n , int S [ ] ) { Stack < Integer > st = new Stack < > ( ) ; st . push ( 0 ) ; S [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! st . empty ( ) && price [ st . peek ( ) ] <= price [ i ] ) st . pop ( ) ; S [ i ] = ( st . empty ( ) ) ? ( i + 1 ) : ( i - st . peek ( ) ) ; st . push ( i ) ; } } static void printArray ( int arr [ ] ) { System . out . print ( Arrays . toString ( arr ) ) ; } public static void main ( String [ ] args ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . length ; int S [ ] = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S ) ; } }
class GFG { static void calculateSpan ( int A [ ] , int n , int ans [ ] ) { ans [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int counter = 1 ; while ( ( i - counter ) >= 0 && A [ i ] >= A [ i - counter ] ) { counter += ans [ i - counter ] ; } ans [ i ] = counter ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int price [ ] = { 10 , 4 , 5 , 90 , 120 , 80 } ; int n = price . length ; int S [ ] = new int [ n ] ; calculateSpan ( price , n , S ) ; printArray ( S , n ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void mirror ( ) { root = mirror ( root ) ; } Node mirror ( Node node ) { if ( node == null ) return node ; Node left = mirror ( node . left ) ; Node right = mirror ( node . right ) ; node . left = right ; node . right = left ; return node ; } void inOrder ( ) { inOrder ( root ) ; } void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ input ▁ tree ▁ is ▁ : " ) ; tree . inOrder ( ) ; System . out . println ( " " ) ; tree . mirror ( ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " ) ; tree . inOrder ( ) ; } }
import java . util . * ; class GFG { static void fillNext ( int next [ ] , int a [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( s . size ( ) > 0 ) { int cur = s . peek ( ) ; if ( a [ cur ] < a [ i ] ) { next [ cur ] = i ; s . pop ( ) ; } else break ; } s . push ( i ) ; } while ( s . size ( ) > 0 ) { int cur = s . peek ( ) ; next [ cur ] = - 1 ; s . pop ( ) ; } } static void count ( int a [ ] , int dp [ ] , int n ) { int next [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) next [ i ] = 0 ; fillNext ( next , a , n ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( next [ i ] == - 1 ) dp [ i ] = 0 ; else dp [ i ] = 1 + dp [ next [ i ] ] ; } } static int answerQuery ( int dp [ ] , int index ) { return dp [ index ] ; } public static void main ( String args [ ] ) { int a [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; int n = a . length ; int dp [ ] = new int [ n ] ; count ( a , dp , n ) ; System . out . println ( answerQuery ( dp , 3 ) ) ; System . out . println ( answerQuery ( dp , 6 ) ) ; System . out . println ( answerQuery ( dp , 1 ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > no_NGN ( int arr [ ] , int n ) { Vector < Integer > nxt = new Vector < > ( ) ; Stack < Integer > s = new Stack < > ( ) ; nxt . add ( 0 ) ; s . add ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( ! s . isEmpty ( ) && arr [ i ] >= arr [ s . peek ( ) ] ) s . pop ( ) ; if ( s . isEmpty ( ) ) nxt . add ( 0 ) ; else nxt . add ( nxt . get ( n - s . peek ( ) - 1 ) + 1 ) ; s . add ( i ) ; } Collections . reverse ( nxt ) ; return nxt ; } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 3 , 4 , 2 , 7 , 5 , 8 , 10 , 6 } ; Vector < Integer > nxt = no_NGN ( arr , n ) ; System . out . print ( nxt . get ( 3 ) + "NEW_LINE"); System . out . print ( nxt . get ( 6 ) + "NEW_LINE"); System . out . print ( nxt . get ( 1 ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000 ; static int [ ] nextGreaterInLeft ( int [ ] a , int n ) { int [ ] left_index = new int [ MAX ] ; Stack < Integer > s = new Stack < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( s . size ( ) != 0 && a [ i ] > a [ s . peek ( ) - 1 ] ) { int r = s . peek ( ) ; s . pop ( ) ; left_index [ r - 1 ] = i + 1 ; } s . push ( i + 1 ) ; } return left_index ; } static int [ ] nextGreaterInRight ( int [ ] a , int n ) { int [ ] right_index = new int [ MAX ] ; Stack < Integer > s = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { while ( s . size ( ) != 0 && a [ i ] > a [ s . peek ( ) - 1 ] ) { int r = s . peek ( ) ; s . pop ( ) ; right_index [ r - 1 ] = i + 1 ; } s . push ( i + 1 ) ; } return right_index ; } static int LRProduct ( int [ ] arr , int n ) { int [ ] left = nextGreaterInLeft ( arr , n ) ; int [ ] right = nextGreaterInRight ( arr , n ) ; int ans = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) { ans = Math . max ( ans , left [ i ] * right [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 5 , 4 , 3 , 4 , 5 } ; int n = arr . length ; System . out . print ( LRProduct ( arr , n ) ) ; } }
import java . util . * ; class GFG { static final int N = 8 ; static int MATRIX [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 } } ; static int knows ( int a , int b ) { return MATRIX [ a ] [ b ] ; } static int findCelebrity ( int n ) { int [ ] indegree = new int [ n ] ; int [ ] outdegree = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int x = knows ( i , j ) ; outdegree [ i ] += x ; indegree [ j ] += x ; } } for ( int i = 0 ; i < n ; i ++ ) if ( indegree [ i ] == n - 1 && outdegree [ i ] == 0 ) return i ; return - 1 ; } public static void main ( String [ ] args ) { int n = 4 ; int id = findCelebrity ( n ) ; if ( id == - 1 ) System . out . print ( " No ▁ celebrity " ) ; else System . out . print ( " Celebrity ▁ ID ▁ " + id ) ; } }
import java . util . Stack ; public class EvaluateString { public static boolean hasPrecedence ( char op1 , char op2 ) { if ( op2 == ' ( ' op2 == ' ) ' ) return false ; if ( ( op1 == ' * ' op1 == ' / ' ) && ( op2 == ' + ' op2 == ' - ' ) ) return false ; else return true ; } public static int applyOp ( char op , int b , int a ) { switch ( op ) { case ' + ' : return a + b ; case ' - ' : return a - b ; case ' * ' : return a * b ; case ' / ' : if ( b == 0 ) throw new UnsupportedOperationException ( " Cannot ▁ divide ▁ by ▁ zero " ) ; return a / b ; } return 0 ; } public static int evaluate ( String expression ) { char [ ] tokens = expression . toCharArray ( ) ; Stack < Integer > values = new Stack < Integer > ( ) ; Stack < Character > ops = new Stack < Character > ( ) ; for ( int i = 0 ; i < tokens . length ; i ++ ) { if ( tokens [ i ] == ' ▁ ' ) continue ; else if ( tokens [ i ] == ' ( ' ) ops . push ( tokens [ i ] ) ; if ( tokens [ i ] >= '0' && tokens [ i ] <= '9' ) { StringBuffer sbuf = new StringBuffer ( ) ; while ( i < tokens . length && tokens [ i ] >= '0' && tokens [ i ] <= '9' ) sbuf . append ( tokens [ i ++ ] ) ; values . push ( Integer . parseInt ( sbuf . toString ( ) ) ) ; i -- ; } else if ( tokens [ i ] == ' ) ' ) { while ( ops . peek ( ) != ' ( ' ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; ops . pop ( ) ; } else if ( tokens [ i ] == ' + ' tokens [ i ] == ' - ' tokens [ i ] == ' * ' tokens [ i ] == ' / ' ) { while ( ! ops . empty ( ) && hasPrecedence ( tokens [ i ] , ops . peek ( ) ) ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; ops . push ( tokens [ i ] ) ; } } while ( ! ops . empty ( ) ) values . push ( applyOp ( ops . pop ( ) , values . pop ( ) , values . pop ( ) ) ) ; return values . pop ( ) ; } public static void main ( String [ ] args ) { System . out . println ( EvaluateString . evaluate ( "10 ▁ + ▁ 2 ▁ * ▁ 6" ) ) ; System . out . println ( EvaluateString . evaluate ( "100 ▁ * ▁ 2 ▁ + ▁ 12" ) ) ; System . out . println ( EvaluateString . evaluate ( "100 ▁ * ▁ ( ▁ 2 ▁ + ▁ 12 ▁ ) " ) ) ; System . out . println ( EvaluateString . evaluate ( "100 ▁ * ▁ ( ▁ 2 ▁ + ▁ 12 ▁ ) ▁ / ▁ 14" ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void mirror ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node curr = q . peek ( ) ; q . remove ( ) ; Node temp = curr . left ; curr . left = curr . right ; curr . right = temp ; ; if ( curr . left != null ) q . add ( curr . left ) ; if ( curr . right != null ) q . add ( curr . right ) ; } } static void inOrder ( Node node ) { if ( node == null ) return ; inOrder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inOrder ( node . right ) ; } 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 ) ; System . out . print ( " Inorder traversal of the " ▁ + " coned tree is "); inOrder ( root ) ; mirror ( root ) ; System . out . print ( " Inorder traversal of the " + ▁ " mirror tree is "); inOrder ( root ) ; } }
class TOH { class Stack { int capacity ; int top ; int array [ ] ; } Stack createStack ( int capacity ) { Stack stack = new Stack ( ) ; stack . capacity = capacity ; stack . top = - 1 ; stack . array = new int [ capacity ] ; return stack ; } boolean isFull ( Stack stack ) { return ( stack . top == stack . capacity - 1 ) ; } boolean isEmpty ( Stack stack ) { return ( stack . top == - 1 ) ; } void push ( Stack stack , int item ) { if ( isFull ( stack ) ) return ; stack . array [ ++ stack . top ] = item ; } int pop ( Stack stack ) { if ( isEmpty ( stack ) ) return Integer . MIN_VALUE ; return stack . array [ stack . top -- ] ; } void moveDisk ( char fromPeg , char toPeg , int disk ) { System . out . println ( " Move ▁ the ▁ disk ▁ " + disk + " ▁ from ▁ " + fromPeg + " ▁ to ▁ " + toPeg ) ; } void moveDisksBetweenTwoPoles ( Stack src , Stack dest , char s , char d ) { int pole1TopDisk = pop ( src ) ; int pole2TopDisk = pop ( dest ) ; if ( pole1TopDisk == Integer . MIN_VALUE ) { push ( src , pole2TopDisk ) ; moveDisk ( d , s , pole2TopDisk ) ; } else if ( pole2TopDisk == Integer . MIN_VALUE ) { push ( dest , pole1TopDisk ) ; moveDisk ( s , d , pole1TopDisk ) ; } else if ( pole1TopDisk > pole2TopDisk ) { push ( src , pole1TopDisk ) ; push ( src , pole2TopDisk ) ; moveDisk ( d , s , pole2TopDisk ) ; } else { push ( dest , pole2TopDisk ) ; push ( dest , pole1TopDisk ) ; moveDisk ( s , d , pole1TopDisk ) ; } } void tohIterative ( int num_of_disks , Stack src , Stack aux , Stack dest ) { int i , total_num_of_moves ; char s = ' S ' , d = ' D ' , a = ' A ' ; if ( num_of_disks % 2 == 0 ) { char temp = d ; d = a ; a = temp ; } total_num_of_moves = ( int ) ( Math . pow ( 2 , num_of_disks ) - 1 ) ; for ( i = num_of_disks ; i >= 1 ; i -- ) push ( src , i ) ; for ( i = 1 ; i <= total_num_of_moves ; i ++ ) { if ( i % 3 == 1 ) moveDisksBetweenTwoPoles ( src , dest , s , d ) ; else if ( i % 3 == 2 ) moveDisksBetweenTwoPoles ( src , aux , s , a ) ; else if ( i % 3 == 0 ) moveDisksBetweenTwoPoles ( aux , dest , a , d ) ; } } public static void main ( String [ ] args ) { int num_of_disks = 3 ; TOH ob = new TOH ( ) ; Stack src , dest , aux ; src = ob . createStack ( num_of_disks ) ; dest = ob . createStack ( num_of_disks ) ; aux = ob . createStack ( num_of_disks ) ; ob . tohIterative ( num_of_disks , src , aux , dest ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static void deleteMid ( Stack < Character > st , int n , int curr ) { if ( st . empty ( ) curr == n ) return ; char x = st . pop ( ) ; deleteMid ( st , n , curr + 1 ) ; if ( curr != n / 2 ) st . push ( x ) ; } public static void main ( String args [ ] ) { Stack < Character > st = new Stack < Character > ( ) ; st . push ( '1' ) ; st . push ( '2' ) ; st . push ( '3' ) ; st . push ( '4' ) ; st . push ( '5' ) ; st . push ( '6' ) ; st . push ( '7' ) ; deleteMid ( st , st . size ( ) , 0 ) ; while ( ! st . empty ( ) ) { char p = st . pop ( ) ; System . out . print ( p + " ▁ " ) ; } } }
import java . io . * ; import java . util . * ; class GFG { static Stack < Integer > sortStack ( Stack < Integer > input ) { Stack < Integer > tmpStack = new Stack < Integer > ( ) ; while ( ! input . empty ( ) ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( ! tmpStack . empty ( ) && tmpStack . peek ( ) < tmp ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } tmpStack . push ( tmp ) ; } return tmpStack ; } static void sortArrayUsingStacks ( int [ ] arr , int n ) { Stack < Integer > input = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) input . push ( arr [ i ] ) ; Stack < Integer > tmpStack = sortStack ( input ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = tmpStack . peek ( ) ; tmpStack . pop ( ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 5 , 15 , 45 } ; int n = arr . length ; sortArrayUsingStacks ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; Node head ; void push ( Node node ) { node . right = head ; node . left = null ; if ( head != null ) head . left = node ; head = node ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } void spiralLevelOrder ( Node root ) { if ( root == null ) return ; Deque < Node > q = new LinkedList < Node > ( ) ; q . addFirst ( root ) ; Stack < Node > stk = new Stack < Node > ( ) ; int level = 0 ; while ( ! q . isEmpty ( ) ) { int nodeCount = q . size ( ) ; if ( ( level & 1 ) % 2 != 0 ) { while ( nodeCount > 0 ) { Node node = q . peekFirst ( ) ; q . pollFirst ( ) ; stk . push ( node ) ; if ( node . left != null ) q . addLast ( node . left ) ; if ( node . right != null ) q . addLast ( node . right ) ; nodeCount -- ; } } else { while ( nodeCount > 0 ) { Node node = q . peekLast ( ) ; q . pollLast ( ) ; stk . push ( node ) ; if ( node . right != null ) q . addFirst ( node . right ) ; if ( node . left != null ) q . addFirst ( node . left ) ; nodeCount -- ; } } level ++ ; } while ( ! stk . empty ( ) ) { push ( stk . peek ( ) ) ; stk . pop ( ) ; } System . out . println ( " Created ▁ DLL ▁ is ▁ : ▁ " ) ; printList ( head ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . left . right = new Node ( 13 ) ; tree . root . right . right . left = new Node ( 14 ) ; tree . spiralLevelOrder ( tree . root ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void reverseWords ( String str ) { Stack < Character > st = new Stack < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str . charAt ( i ) != ' ▁ ' ) st . push ( str . charAt ( i ) ) ; else { while ( st . empty ( ) == false ) { System . out . print ( st . pop ( ) ) ; } System . out . print ( " ▁ " ) ; } } while ( st . empty ( ) == false ) { System . out . print ( st . pop ( ) ) ; } } public static void main ( String [ ] args ) { String str = " Geeks ▁ for ▁ Geeks " ; reverseWords ( str ) ; } }
import java . util . * ; class GFG { static int MAXN = 100005 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeNext ( int arr [ ] , int n , int nextBig [ ] ) { Stack < pair > s = new Stack < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { nextBig [ i ] = i ; while ( ! s . empty ( ) && s . peek ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) nextBig [ i ] = s . peek ( ) . second ; s . push ( new pair ( arr [ i ] , i ) ) ; } } static void makePrev ( int arr [ ] , int n , int prevBig [ ] ) { Stack < pair > s = new Stack < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { prevBig [ i ] = - 1 ; while ( ! s . empty ( ) && s . peek ( ) . first < arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) prevBig [ i ] = s . peek ( ) . second ; s . push ( new pair ( arr [ i ] , i ) ) ; } } static int wrapper ( int arr [ ] , int n ) { int [ ] nextBig = new int [ MAXN ] ; int [ ] prevBig = new int [ MAXN ] ; int [ ] maxi = new int [ MAXN ] ; int ans = 0 ; makePrev ( arr , n , prevBig ) ; makeNext ( arr , n , nextBig ) ; for ( int i = 0 ; i < n ; i ++ ) if ( nextBig [ i ] != i ) maxi [ nextBig [ i ] - i ] = Math . max ( maxi [ nextBig [ i ] - i ] , i - prevBig [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) ans += maxi [ i ] ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; int n = arr . length ; System . out . println ( wrapper ( arr , n ) ) ; } }
import java . util . Stack ; class GFG { static boolean check ( int A [ ] , int N ) { Stack < Integer > S = new Stack < Integer > ( ) ; int B_end = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! S . empty ( ) ) { int top = S . peek ( ) ; while ( top == B_end + 1 ) { B_end = B_end + 1 ; S . pop ( ) ; if ( S . empty ( ) ) { break ; } top = S . peek ( ) ; } if ( S . empty ( ) ) { S . push ( A [ i ] ) ; } else { top = S . peek ( ) ; if ( A [ i ] < top ) { S . push ( A [ i ] ) ; } else { return false ; } } } else { S . push ( A [ i ] ) ; } } return true ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 1 , 2 , 3 } ; int N = A . length ; if ( check ( A , N ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . util . Stack ; public class RectArea { static int getMaxArea ( int hist [ ] , int n ) { Stack < Integer > s = new Stack < > ( ) ; int max_area = 0 ; int tp ; int area_with_top ; int i = 0 ; while ( i < n ) { if ( s . empty ( ) || hist [ s . peek ( ) ] <= hist [ i ] ) s . push ( i ++ ) ; else { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } } while ( s . empty ( ) == false ) { tp = s . peek ( ) ; s . pop ( ) ; area_with_top = hist [ tp ] * ( s . empty ( ) ? i : i - s . peek ( ) - 1 ) ; if ( max_area < area_with_top ) max_area = area_with_top ; } return max_area ; } public static void main ( String [ ] args ) { int hist [ ] = { 6 , 2 , 5 , 4 , 5 , 1 , 6 } ; System . out . println ( " Maximum ▁ area ▁ is ▁ " + getMaxArea ( hist , hist . length ) ) ; } }
import java . util . * ; import java . io . * ; import java . math . * ; class GFG { static void towerOfHanoi ( int n , char from_rod , char to_rod , char aux_rod ) { if ( n == 1 ) { System . out . println ( " Move ▁ disk ▁ 1 ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; return ; } towerOfHanoi ( n - 1 , from_rod , aux_rod , to_rod ) ; System . out . println ( " Move ▁ disk ▁ " + n + " ▁ from ▁ rod ▁ " + from_rod + " ▁ to ▁ rod ▁ " + to_rod ) ; towerOfHanoi ( n - 1 , aux_rod , to_rod , from_rod ) ; } public static void main ( String args [ ] ) { int n = 4 ; towerOfHanoi ( n , ' A ' , ' C ' , ' B ' ) ; } }
import java . util . Stack ; class Test { static int findMaxLen ( String str ) { int n = str . length ( ) ; Stack < Integer > stk = new Stack < > ( ) ; stk . push ( - 1 ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) stk . push ( i ) ; else { if ( ! stk . empty ( ) ) stk . pop ( ) ; if ( ! stk . empty ( ) ) result = Math . max ( result , i - stk . peek ( ) ) ; else stk . push ( i ) ; } } return result ; } public static void main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; System . out . println ( findMaxLen ( str ) ) ; str = " ( ) ( ( ) ) ) ) ) " ; System . out . println ( findMaxLen ( str ) ) ; } }
import java . util . * ; class GFG { static int N = 12 , ans ; static Vector < Vector < Integer > > tree = new Vector < Vector < Integer > > ( ) ; static int dfs ( int visit [ ] , int node ) { int num = 0 , temp = 0 ; visit [ node ] = 1 ; for ( int i = 0 ; i < tree . get ( node ) . size ( ) ; i ++ ) { if ( visit [ tree . get ( node ) . get ( i ) ] == 0 ) { temp = dfs ( visit , tree . get ( node ) . get ( i ) ) ; if ( temp % 2 != 0 ) num += temp ; else ans ++ ; } } return num + 1 ; } static int minEdge ( int n ) { int visit [ ] = new int [ n + 2 ] ; ans = 0 ; dfs ( visit , 1 ) ; return ans ; } public static void main ( String args [ ] ) { int n = 10 ; for ( int i = 0 ; i < n + 2 ; i ++ ) tree . add ( new Vector < Integer > ( ) ) ; tree . get ( 1 ) . add ( 3 ) ; tree . get ( 3 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 6 ) ; tree . get ( 6 ) . add ( 1 ) ; tree . get ( 1 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 1 ) ; tree . get ( 3 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 3 ) ; tree . get ( 6 ) . add ( 8 ) ; tree . get ( 8 ) . add ( 6 ) ; tree . get ( 2 ) . add ( 7 ) ; tree . get ( 7 ) . add ( 2 ) ; tree . get ( 2 ) . add ( 5 ) ; tree . get ( 5 ) . add ( 2 ) ; tree . get ( 4 ) . add ( 9 ) ; tree . get ( 9 ) . add ( 4 ) ; tree . get ( 4 ) . add ( 10 ) ; tree . get ( 10 ) . add ( 4 ) ; System . out . println ( minEdge ( n ) ) ; } }
import java . util . * ; class GFG { static int findMaxLen ( String s ) { if ( s . length ( ) <= 1 ) return 0 ; int curMax = 0 ; int [ ] longest = new int [ s . length ( ) ] ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ) ' && i - longest [ i - 1 ] - 1 >= 0 && s . charAt ( i - longest [ i - 1 ] - 1 ) == ' ( ' ) { longest [ i ] = longest [ i - 1 ] + 2 + ( ( i - longest [ i - 1 ] - 2 >= 0 ) ? longest [ i - longest [ i - 1 ] - 2 ] : 0 ) ; curMax = Math . max ( longest [ i ] , curMax ) ; } } return curMax ; } public static void main ( String [ ] args ) { String str = " ( ( ( ) ( ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); str = " ( ) ( ( ) ) ) ) ) " ; System . out . print ( findMaxLen ( str ) + "NEW_LINE"); } }
import java . util . Scanner ; import java . util . Arrays ; class GFG { public static int solve ( String s , int n ) { int left = 0 , right = 0 ; int maxlength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . max ( maxlength , 2 * right ) ; else if ( right > left ) left = right = 0 ; } left = right = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == ' ( ' ) left ++ ; else right ++ ; if ( left == right ) maxlength = Math . max ( maxlength , 2 * left ) ; else if ( left > right ) left = right = 0 ; } return maxlength ; } public static void main ( String args [ ] ) { System . out . print ( solve ( " ( ( ( ) ( ) ( ) ( ) ( ( ( ( ) ) " , 16 ) ) ; } }
import java . util . Stack ; public class GFG { static int countMinReversals ( String expr ) { int len = expr . length ( ) ; if ( len % 2 != 0 ) return - 1 ; Stack < Character > s = new Stack < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { char c = expr . charAt ( i ) ; if ( c == ' } ' && ! s . empty ( ) ) { if ( s . peek ( ) == ' { ' ) s . pop ( ) ; else s . push ( c ) ; } else s . push ( c ) ; } int red_len = s . size ( ) ; int n = 0 ; while ( ! s . empty ( ) && s . peek ( ) == ' { ' ) { s . pop ( ) ; n ++ ; } return ( red_len / 2 + n % 2 ) ; } public static void main ( String [ ] args ) { String expr = " } } { { " ; System . out . println ( countMinReversals ( expr ) ) ; } }
import java . util . * ; public class GFG { static int countMinReversals ( String expr ) { int len = expr . length ( ) ; int ans ; if ( len % 2 != 0 ) { return - 1 ; } int left_brace = 0 , right_brace = 0 ; for ( int i = 0 ; i < len ; i ++ ) { char ch = expr . charAt ( i ) ; if ( ch == ' { ' ) { left_brace ++ ; } else { if ( left_brace == 0 ) { right_brace ++ ; } else { left_brace -- ; } } } ans = ( int ) ( Math . ceil ( ( 0.0 + left_brace ) / 2 ) + Math . ceil ( ( 0.0 + right_brace ) / 2 ) ) ; return ans ; } public static void main ( String [ ] args ) { String expr = " } } { { " ; System . out . println ( countMinReversals ( expr ) ) ; } }
import java . util . Stack ; public class GFG { static boolean checkRedundancy ( String s ) { Stack < Character > st = new Stack < > ( ) ; char [ ] str = s . toCharArray ( ) ; for ( char ch : str ) { if ( ch == ' ) ' ) { char top = st . peek ( ) ; st . pop ( ) ; boolean flag = true ; while ( top != ' ( ' ) { if ( top == ' + ' top == ' - ' top == ' * ' top == ' / ' ) { flag = false ; } top = st . peek ( ) ; st . pop ( ) ; } if ( flag == true ) { return true ; } } else { st . push ( ch ) ; } } return false ; } static void findRedundant ( String str ) { boolean ans = checkRedundancy ( str ) ; if ( ans == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String str = " ( ( a + b ) ) " ; findRedundant ( str ) ; str = " ( a + ( b ) / c ) " ; findRedundant ( str ) ; str = " ( a + b * ( c - d ) ) " ; findRedundant ( str ) ; } }
import java . io . * ; import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; static boolean adjSign ( String s , int i ) { if ( i == 0 ) return true ; if ( s . charAt ( i - 1 ) == ' - ' ) return false ; return true ; } ; static void eval ( String s , int [ ] v , boolean add ) { Stack < Boolean > stk = new Stack < > ( ) ; stk . push ( true ) ; int i = 0 ; while ( i < s . length ( ) ) { if ( s . charAt ( i ) == ' + ' || s . charAt ( i ) == ' - ' ) { i ++ ; continue ; } if ( s . charAt ( i ) == ' ( ' ) { if ( adjSign ( s , i ) ) stk . push ( stk . peek ( ) ) ; else stk . push ( ! stk . peek ( ) ) ; } else if ( s . charAt ( i ) == ' ) ' ) stk . pop ( ) ; else { if ( stk . peek ( ) ) v [ s . charAt ( i ) - ' a ' ] += ( adjSign ( s , i ) ? add ? 1 : - 1 : add ? - 1 : 1 ) ; else v [ s . charAt ( i ) - ' a ' ] += ( adjSign ( s , i ) ? add ? - 1 : 1 : add ? 1 : - 1 ) ; } i ++ ; } } ; static boolean areSame ( String expr1 , String expr2 ) { int [ ] v = new int [ MAX_CHAR ] ; eval ( expr1 , v , true ) ; eval ( expr2 , v , false ) ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( v [ i ] != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String expr1 = " - ( a + b + c ) " , expr2 = " - a - b - c " ; if ( areSame ( expr1 , expr2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Stack ; class GFG { static void test ( String expression , int index ) { int i ; if ( expression . charAt ( index ) != ' [ ' ) { System . out . print ( expression + " , ▁ " + index + ": -1NEW_LINE"); return ; } Stack < Integer > st = new Stack < > ( ) ; for ( i = index ; i < expression . length ( ) ; i ++ ) { if ( expression . charAt ( i ) == ' [ ' ) { st . push ( ( int ) expression . charAt ( i ) ) ; } else if ( expression . charAt ( i ) == ' ] ' ) { st . pop ( ) ; if ( st . empty ( ) ) { System . out . print ( expression + " , ▁ " + index + " : ▁ " + i + "NEW_LINE"); return ; } } } System . out . print ( expression + " , ▁ " + index + ": -1NEW_LINE"); } public static void main ( String [ ] args ) { test ( " [ ABC [ 23 ] ] [89 ] " , 0 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 4 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 9 ) ; test ( " [ ABC [ 23 ] ] [89 ] " , 1 ) ; } }
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . data = key ; node . left = null ; node . right = null ; return node ; } static void convertTree ( Node root ) { if ( root == null ) return ; convertTree ( root . left ) ; convertTree ( root . right ) ; if ( root . left != null && root . right != null ) root . data = ( root . left . data ) & ( root . right . data ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 0 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 0 ) ; root . left . left = newNode ( 0 ) ; root . left . right = newNode ( 1 ) ; root . right . left = newNode ( 1 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ " ) ; printInorder ( root ) ; convertTree ( root ) ; System . out . println ( ) ; System . out . print ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ " ) ; printInorder ( root ) ; } }
class GFG { static void PrintMinNumberForPattern ( String arr ) { int curr_max = 0 ; int last_entry = 0 ; int j ; for ( int i = 0 ; i < arr . length ( ) ; i ++ ) { int noOfNextD = 0 ; switch ( arr . charAt ( i ) ) { case ' I ' : j = i + 1 ; while ( j < arr . length ( ) && arr . charAt ( j ) == ' D ' ) { noOfNextD ++ ; j ++ ; } if ( i == 0 ) { curr_max = noOfNextD + 2 ; System . out . print ( " ▁ " + ++ last_entry ) ; System . out . print ( " ▁ " + curr_max ) ; last_entry = curr_max ; } else { curr_max = curr_max + noOfNextD + 1 ; last_entry = curr_max ; System . out . print ( " ▁ " + last_entry ) ; } for ( int k = 0 ; k < noOfNextD ; k ++ ) { System . out . print ( " ▁ " + -- last_entry ) ; i ++ ; } break ; case ' D ' : if ( i == 0 ) { j = i + 1 ; while ( j < arr . length ( ) && arr . charAt ( j ) == ' D ' ) { noOfNextD ++ ; j ++ ; } curr_max = noOfNextD + 2 ; System . out . print ( " ▁ " + curr_max + " ▁ " + ( curr_max - 1 ) ) ; last_entry = curr_max - 1 ; } else { System . out . print ( " ▁ " + ( last_entry - 1 ) ) ; last_entry -- ; } break ; } } System . out . println ( ) ; } public static void main ( String [ ] args ) { PrintMinNumberForPattern ( " IDID " ) ; PrintMinNumberForPattern ( " I " ) ; PrintMinNumberForPattern ( " DD " ) ; PrintMinNumberForPattern ( " II " ) ; PrintMinNumberForPattern ( " DIDI " ) ; PrintMinNumberForPattern ( " IIDDD " ) ; PrintMinNumberForPattern ( " DDIDDIID " ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static void printLeast ( String arr ) { int min_avail = 1 , pos_of_I = 0 ; ArrayList < Integer > al = new ArrayList < > ( ) ; if ( arr . charAt ( 0 ) == ' I ' ) { al . add ( 1 ) ; al . add ( 2 ) ; min_avail = 3 ; pos_of_I = 1 ; } else { al . add ( 2 ) ; al . add ( 1 ) ; min_avail = 3 ; pos_of_I = 0 ; } for ( int i = 1 ; i < arr . length ( ) ; i ++ ) { if ( arr . charAt ( i ) == ' I ' ) { al . add ( min_avail ) ; min_avail ++ ; pos_of_I = i + 1 ; } else { al . add ( al . get ( i ) ) ; for ( int j = pos_of_I ; j <= i ; j ++ ) al . set ( j , al . get ( j ) + 1 ) ; min_avail ++ ; } } for ( int i = 0 ; i < al . size ( ) ; i ++ ) System . out . print ( al . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { printLeast ( " IDID " ) ; printLeast ( " I " ) ; printLeast ( " DD " ) ; printLeast ( " II " ) ; printLeast ( " DIDI " ) ; printLeast ( " IIDDD " ) ; printLeast ( " DDIDDIID " ) ; } }
import java . io . IOException ; public class Test { static String getMinNumberForPattern ( String seq ) { int n = seq . length ( ) ; if ( n >= 9 ) return " - 1" ; char result [ ] = new char [ n + 1 ] ; int count = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == n || seq . charAt ( i ) == ' I ' ) { for ( int j = i - 1 ; j >= - 1 ; j -- ) { result [ j + 1 ] = ( char ) ( ( int ) '0' + count ++ ) ; if ( j >= 0 && seq . charAt ( j ) == ' I ' ) break ; } } } return new String ( result ) ; } public static void main ( String [ ] args ) throws IOException { String inputs [ ] = { " IDID " , " I " , " DD " , " II " , " DIDI " , " IIDDD " , " DDIDDIID " } ; for ( String input : inputs ) { System . out . println ( getMinNumberForPattern ( input ) ) ; } } }
import java . util . Stack ; public class GFG { static boolean findDuplicateparenthesis ( String s ) { Stack < Character > Stack = new Stack < > ( ) ; char [ ] str = s . toCharArray ( ) ; for ( char ch : str ) { if ( ch == ' ) ' ) { char top = Stack . peek ( ) ; Stack . pop ( ) ; int elementsInside = 0 ; while ( top != ' ( ' ) { elementsInside ++ ; top = Stack . peek ( ) ; Stack . pop ( ) ; } if ( elementsInside < 1 ) { return true ; } } else { Stack . push ( ch ) ; } } return false ; } public static void main ( String [ ] args ) { String str = " ( ( ( a + ( b ) ) + ( c + d ) ) ) " ; if ( findDuplicateparenthesis ( str ) ) { System . out . println ( " Duplicate ▁ Found ▁ " ) ; } else { System . out . println ( " No ▁ Duplicates ▁ Found ▁ " ) ; } } }
import java . util . Stack ; public class Main { public static void nextGreater ( int arr [ ] , int next [ ] , char order ) { Stack < Integer > stack = new Stack < > ( ) ; for ( int i = arr . length - 1 ; i >= 0 ; i -- ) { while ( ! stack . isEmpty ( ) && ( ( order == ' G ' ) ? arr [ stack . peek ( ) ] <= arr [ i ] : arr [ stack . peek ( ) ] >= arr [ i ] ) ) stack . pop ( ) ; if ( ! stack . isEmpty ( ) ) next [ i ] = stack . peek ( ) ; else next [ i ] = - 1 ; stack . push ( i ) ; } } public static void nextSmallerOfNextGreater ( int arr [ ] ) { int NG [ ] = new int [ arr . length ] ; int RS [ ] = new int [ arr . length ] ; nextGreater ( arr , NG , ' G ' ) ; nextGreater ( arr , RS , ' S ' ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( NG [ i ] != - 1 && RS [ NG [ i ] ] != - 1 ) System . out . print ( arr [ RS [ NG [ i ] ] ] + " ▁ " ) ; else System . out . print ( " - 1 ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 9 , 2 , 5 , 1 , 7 } ; nextSmallerOfNextGreater ( arr ) ; } }
import java . util . Stack ; class GFG { static int countNumber ( int n ) { int result = 0 ; Stack < Integer > s = new Stack < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . peek ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { int x = tp * 10 + j ; if ( x <= n ) { s . push ( x ) ; result ++ ; } } } } return result ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( countNumber ( n ) ) ; } }
import java . util . Vector ; class Test { static int removeConsecutiveSame ( Vector < String > v ) { int n = v . size ( ) ; for ( int i = 0 ; i < n - 1 ; ) { if ( v . get ( i ) . equals ( v . get ( i + 1 ) ) ) { v . remove ( i ) ; v . remove ( i ) ; if ( i > 0 ) i -- ; n = n - 2 ; } else i ++ ; } return v . size ( ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . addElement ( " tom " ) ; v . addElement ( " jerry " ) ; v . addElement ( " jerry " ) ; v . addElement ( " tom " ) ; System . out . println ( removeConsecutiveSame ( v ) ) ; } }
import java . util . Stack ; import java . util . Vector ; class Test { static int removeConsecutiveSame ( Vector < String > v ) { Stack < String > st = new Stack < > ( ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( st . empty ( ) ) st . push ( v . get ( i ) ) ; else { String str = st . peek ( ) ; if ( str . equals ( v . get ( i ) ) ) st . pop ( ) ; else st . push ( v . get ( i ) ) ; } } return st . size ( ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . addElement ( " ab " ) ; v . addElement ( " aa " ) ; v . addElement ( " aa " ) ; v . addElement ( " bcd " ) ; v . addElement ( " ab " ) ; System . out . println ( removeConsecutiveSame ( v ) ) ; } }
import java . util . Stack ; class Test { static String decode ( String str ) { Stack < Integer > integerstack = new Stack < > ( ) ; Stack < Character > stringstack = new Stack < > ( ) ; String temp = " " , result = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( Character . isDigit ( str . charAt ( i ) ) ) { while ( Character . isDigit ( str . charAt ( i ) ) ) { count = count * 10 + str . charAt ( i ) - '0' ; i ++ ; } i -- ; integerstack . push ( count ) ; } else if ( str . charAt ( i ) == ' ] ' ) { temp = " " ; count = 0 ; if ( ! integerstack . isEmpty ( ) ) { count = integerstack . peek ( ) ; integerstack . pop ( ) ; } while ( ! stringstack . isEmpty ( ) && stringstack . peek ( ) != ' [ ' ) { temp = stringstack . peek ( ) + temp ; stringstack . pop ( ) ; } if ( ! stringstack . empty ( ) && stringstack . peek ( ) == ' [ ' ) stringstack . pop ( ) ; for ( int j = 0 ; j < count ; j ++ ) result = result + temp ; for ( int j = 0 ; j < result . length ( ) ; j ++ ) stringstack . push ( result . charAt ( j ) ) ; result = " " ; } else if ( str . charAt ( i ) == ' [ ' ) { if ( Character . isDigit ( str . charAt ( i - 1 ) ) ) stringstack . push ( str . charAt ( i ) ) ; else { stringstack . push ( str . charAt ( i ) ) ; integerstack . push ( 1 ) ; } } else stringstack . push ( str . charAt ( i ) ) ; } while ( ! stringstack . isEmpty ( ) ) { result = stringstack . peek ( ) + result ; stringstack . pop ( ) ; } return result ; } public static void main ( String args [ ] ) { String str = "3 [ b2 [ ca ] ] " ; System . out . println ( decode ( str ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void printAncestors ( Node root , int key ) { if ( root == null ) return ; Stack < Node > st = new Stack < Node > ( ) ; while ( 1 == 1 ) { while ( root != null && root . data != key ) { st . push ( root ) ; root = root . left ; } if ( root != null && root . data == key ) break ; if ( st . peek ( ) . right == null ) { root = st . peek ( ) ; st . pop ( ) ; while ( ! st . isEmpty ( ) && st . peek ( ) . right == root ) { root = st . peek ( ) ; st . pop ( ) ; } } root = st . isEmpty ( ) ? null : st . peek ( ) . right ; } while ( ! st . isEmpty ( ) ) { System . out . print ( st . peek ( ) . data + " ▁ " ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 9 ) ; root . left . left . left = newNode ( 4 ) ; root . left . right . right = newNode ( 6 ) ; root . right . right . left = newNode ( 10 ) ; int key = 6 ; printAncestors ( root , key ) ; } }
import java . util . * ; class GfG { static class StackWithMax { static Stack < Integer > mainStack = new Stack < Integer > ( ) ; static Stack < Integer > trackStack = new Stack < Integer > ( ) ; static void push ( int x ) { mainStack . push ( x ) ; if ( mainStack . size ( ) == 1 ) { trackStack . push ( x ) ; return ; } if ( x > trackStack . peek ( ) ) trackStack . push ( x ) ; else trackStack . push ( trackStack . peek ( ) ) ; } static int getMax ( ) { return trackStack . peek ( ) ; } static void pop ( ) { mainStack . pop ( ) ; trackStack . pop ( ) ; } } ; public static void main ( String [ ] args ) { StackWithMax s = new StackWithMax ( ) ; s . push ( 20 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 10 ) ; System . out . println ( s . getMax ( ) ) ; s . push ( 50 ) ; System . out . println ( s . getMax ( ) ) ; } }
import java . util . Stack ; public class GFG { static Stack < Integer > st = new Stack < > ( ) ; static void push_digits ( int number ) { while ( number != 0 ) { st . push ( number % 10 ) ; number = number / 10 ; } } static int reverse_number ( int number ) { push_digits ( number ) ; int reverse = 0 ; int i = 1 ; while ( ! st . isEmpty ( ) ) { reverse = reverse + ( st . peek ( ) * i ) ; st . pop ( ) ; i = i * 10 ; } return reverse ; } public static void main ( String [ ] args ) { int number = 39997 ; System . out . println ( reverse_number ( number ) ) ; } }
import java . util . Queue ; import java . util . LinkedList ; public class FlipTree { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static Node flipBinaryTree ( Node root ) { if ( root == null ) return root ; if ( root . left == null && root . right == null ) return root ; Node flippedRoot = flipBinaryTree ( root . left ) ; root . left . left = root . right ; root . left . right = root ; root . left = root . right = null ; return flippedRoot ; } public static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( true ) { int nodeCount = q . size ( ) ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . remove ( ) ; System . out . print ( node . data + " ▁ " ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; nodeCount -- ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 1 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ given ▁ tree " ) ; printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ flipped ▁ tree " ) ; printLevelOrder ( root ) ; } }
import java . util . * ; class GfG { static boolean pairWiseConsecutive ( Stack < Integer > s ) { Stack < Integer > aux = new Stack < Integer > ( ) ; while ( ! s . isEmpty ( ) ) { aux . push ( s . peek ( ) ) ; s . pop ( ) ; } boolean result = true ; while ( aux . size ( ) > 1 ) { int x = aux . peek ( ) ; aux . pop ( ) ; int y = aux . peek ( ) ; aux . pop ( ) ; if ( Math . abs ( x - y ) != 1 ) result = false ; s . push ( x ) ; s . push ( y ) ; } if ( aux . size ( ) == 1 ) s . push ( aux . peek ( ) ) ; return result ; } public static void main ( String [ ] args ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 4 ) ; s . push ( 5 ) ; s . push ( - 2 ) ; s . push ( - 3 ) ; s . push ( 11 ) ; s . push ( 10 ) ; s . push ( 5 ) ; s . push ( 6 ) ; s . push ( 20 ) ; if ( pairWiseConsecutive ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; System . out . println ( " Stack ▁ content ▁ ( from ▁ top ) ▁ after ▁ function ▁ call " ) ; while ( s . isEmpty ( ) == false ) { System . out . print ( s . peek ( ) + " ▁ " ) ; s . pop ( ) ; } } }
import java . util . * ; class GfG { static String simplify ( String str ) { int len = str . length ( ) ; char res [ ] = new char [ len ] ; int index = 0 , i = 0 ; Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; while ( i < len ) { if ( str . charAt ( i ) == ' + ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' - ' ; if ( s . peek ( ) == 0 ) res [ index ++ ] = ' + ' ; } else if ( str . charAt ( i ) == ' - ' ) { if ( s . peek ( ) == 1 ) res [ index ++ ] = ' + ' ; else if ( s . peek ( ) == 0 ) res [ index ++ ] = ' - ' ; } else if ( str . charAt ( i ) == ' ( ' && i > 0 ) { if ( str . charAt ( i - 1 ) == ' - ' ) { int x = ( s . peek ( ) == 1 ) ? 0 : 1 ; s . push ( x ) ; } else if ( str . charAt ( i - 1 ) == ' + ' ) s . push ( s . peek ( ) ) ; } else if ( str . charAt ( i ) == ' ) ' ) s . pop ( ) ; else res [ index ++ ] = str . charAt ( i ) ; i ++ ; } return new String ( res ) ; } public static void main ( String [ ] args ) { String s1 = " a - ( b + c ) " ; String s2 = " a - ( b - c - ( d + e ) ) - f " ; System . out . println ( simplify ( s1 ) ) ; System . out . println ( simplify ( s2 ) ) ; } }
class GFG { static final int BOUND = 4 ; static int top = - 1 ; static int length = 0 ; static int [ ] create_new ( int [ ] a ) { int [ ] new_a = new int [ length + BOUND ] ; for ( int i = 0 ; i < length ; i ++ ) new_a [ i ] = a [ i ] ; length += BOUND ; return new_a ; } static int [ ] push ( int [ ] a , int element ) { if ( top == length - 1 ) a = create_new ( a ) ; a [ ++ top ] = element ; return a ; } static void pop ( int [ ] a ) { top -- ; } static void display ( int [ ] a ) { if ( top == - 1 ) System . out . println ( " Stack ▁ is ▁ Empty " ) ; else { System . out . print ( " Stack : ▁ " ) ; for ( int i = 0 ; i <= top ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int [ ] a = create_new ( new int [ length + BOUND ] ) ; a = push ( a , 1 ) ; a = push ( a , 2 ) ; a = push ( a , 3 ) ; a = push ( a , 4 ) ; display ( a ) ; a = push ( a , 5 ) ; a = push ( a , 6 ) ; display ( a ) ; a = push ( a , 7 ) ; a = push ( a , 8 ) ; display ( a ) ; a = push ( a , 9 ) ; display ( a ) ; } }
import java . util . * ; class GFG { static void constructBlanceArray ( int BOP [ ] , int BCP [ ] , String str , int n ) { Stack < Integer > stk = new Stack < > ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) stk . add ( i ) ; else { if ( ! stk . isEmpty ( ) ) { BCP [ i ] = 1 ; BOP [ stk . peek ( ) ] = 1 ; stk . pop ( ) ; } else BCP [ i ] = 0 ; } } for ( int i = 1 ; i < n ; i ++ ) { BCP [ i ] += BCP [ i - 1 ] ; BOP [ i ] += BOP [ i - 1 ] ; } } static int query ( int BOP [ ] , int BCP [ ] , int s , int e ) { if ( BOP [ s - 1 ] == BOP [ s ] ) { return ( BCP [ e ] - BOP [ s ] ) * 2 ; } else { return ( BCP [ e ] - BOP [ s ] + 1 ) * 2 ; } } public static void main ( String [ ] args ) { String str = " ( ) ) ( ( ) ) ( ( ) ) ( " ; int n = str . length ( ) ; int BCP [ ] = new int [ n + 1 ] ; int BOP [ ] = new int [ n + 1 ] ; constructBlanceArray ( BOP , BCP , str , n ) ; int startIndex = 5 , endIndex = 11 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); startIndex = 4 ; endIndex = 5 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); startIndex = 1 ; endIndex = 5 ; System . out . print ( " Maximum ▁ Length ▁ Correct ▁ " + " Bracket ▁ Subsequence ▁ between ▁ " + startIndex + " ▁ and ▁ " + endIndex + " ▁ = ▁ " + query ( BOP , BCP , startIndex , endIndex ) + "NEW_LINE"); } }
public class HeapSort { void heapify ( int arr [ ] , int n , int i ) { int largest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] > arr [ largest ] ) largest = l ; if ( r < n && arr [ r ] > arr [ largest ] ) largest = r ; if ( largest != i ) { int swap = arr [ i ] ; arr [ i ] = arr [ largest ] ; arr [ largest ] = swap ; heapify ( arr , n , largest ) ; } } public void sort ( int arr [ ] ) { int n = arr . length ; for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . length ; HeapSort ob = new HeapSort ( ) ; ob . sort ( arr ) ; System . out . println ( " Sorted ▁ array ▁ is " ) ; printArray ( arr ) ; } }
public class HeapSort { static void buildMaxHeap ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i - 1 ) / 2 ] ) { int j = i ; while ( arr [ j ] > arr [ ( j - 1 ) / 2 ] ) { swap ( arr , j , ( j - 1 ) / 2 ) ; j = ( j - 1 ) / 2 ; } } } } static void heapSort ( int arr [ ] , int n ) { buildMaxHeap ( arr , n ) ; for ( int i = n - 1 ; i > 0 ; i -- ) { swap ( arr , 0 , i ) ; int j = 0 , index ; do { index = ( 2 * j + 1 ) ; if ( index < ( i - 1 ) && arr [ index ] < arr [ index + 1 ] ) index ++ ; if ( index < i && arr [ j ] < arr [ index ] ) swap ( arr , j , index ) ; j = index ; } while ( index < i ) ; } } public static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 20 , 15 , 17 , 9 , 21 } ; int n = arr . length ; System . out . print ( " Given ▁ array : ▁ " ) ; printArray ( arr ) ; heapSort ( arr , n ) ; System . out . print ( " Sorted ▁ array : ▁ " ) ; printArray ( arr ) ; } }
import java . util . * ; class GFG { 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 Node flipBinaryTree ( Node root ) { Node curr = root ; Node next = null ; Node temp = null ; Node prev = null ; while ( curr != null ) { next = curr . left ; curr . left = temp ; temp = curr . right ; curr . right = prev ; prev = curr ; curr = next ; } return prev ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( true ) { int nodeCount = q . size ( ) ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . peek ( ) ; System . out . print ( node . data + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; nodeCount -- ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; System . out . print ( " Level ▁ order ▁ traversal ▁ " + "of given treeNEW_LINE"); printLevelOrder ( root ) ; root = flipBinaryTree ( root ) ; System . out . print ( " Level order traversal " ▁ + ▁ " of the flipped tree "); printLevelOrder ( root ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean IsFoldable ( Node node ) { if ( node == null ) return true ; return IsFoldableUtil ( node . left , node . right ) ; } boolean IsFoldableUtil ( Node n1 , Node n2 ) { if ( n1 == null && n2 == null ) return true ; if ( n1 == null n2 == null ) return false ; return IsFoldableUtil ( n1 . left , n2 . right ) && IsFoldableUtil ( n1 . right , n2 . left ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; if ( tree . IsFoldable ( tree . root ) ) System . out . println ( " tree ▁ is ▁ foldable " ) ; else System . out . println ( " Tree ▁ is ▁ not ▁ foldable " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int k ) { Node node = new Node ( ) ; node . data = k ; node . right = node . left = null ; return node ; } static boolean isHeap ( Node root ) { Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; boolean nullish = false ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null ) { if ( nullish temp . left . data >= temp . data ) { return false ; } q . add ( temp . left ) ; } else { nullish = true ; } if ( temp . right != null ) { if ( nullish temp . right . data >= temp . data ) { return false ; } q . add ( temp . right ) ; } else { nullish = true ; } } return true ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . right = newNode ( 6 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 4 ) ; root . left . left . left = newNode ( 3 ) ; root . left . left . right = newNode ( 2 ) ; root . left . right . left = newNode ( 1 ) ; if ( isHeap ( root ) ) System . out . print ( "Given binary tree is a HeapNEW_LINE"); else System . out . print ( "Given binary tree is not a HeapNEW_LINE"); } }
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 " ) ; } }
class GFG { static boolean isHeap ( int arr [ ] , int i , int n ) { if ( i >= ( n - 2 ) / 2 ) { return true ; } if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) { return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . length - 1 ; if ( isHeap ( arr , 0 , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static boolean isHeap ( int arr [ ] , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) { return false ; } if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . length ; if ( isHeap ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class ConnectRopes { static int minCost ( int arr [ ] , int n ) { PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . add ( arr [ i ] ) ; } int res = 0 ; while ( pq . size ( ) > 1 ) { int first = pq . poll ( ) ; int second = pq . poll ( ) ; res += first + second ; pq . add ( first + second ) ; } return res ; } public static void main ( String args [ ] ) { int len [ ] = { 4 , 3 , 2 , 6 } ; int size = len . length ; System . out . println ( " Total ▁ cost ▁ for ▁ connecting " + " ▁ ropes ▁ is ▁ " + minCost ( len , size ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void mergeKArrays ( int [ ] [ ] arr , int a , int [ ] output ) { int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { for ( int j = 0 ; j < 4 ; j ++ ) output [ c ++ ] = arr [ i ] [ j ] ; } Arrays . sort ( output ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = 4 ; int n = 3 ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , n , output ) ; System . out . println ( " Merged ▁ array ▁ is ▁ " ) ; printArray ( output , n * k ) ; } }
import java . util . * ; class GFG { static final int n = 4 ; static void mergeArrays ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 , int arr3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void mergeKArrays ( int arr [ ] [ ] , int i , int j , int output [ ] ) { if ( i == j ) { for ( int p = 0 ; p < n ; p ++ ) output [ p ] = arr [ i ] [ p ] ; return ; } if ( j - i == 1 ) { mergeArrays ( arr [ i ] , arr [ j ] , n , n , output ) ; return ; } int [ ] out1 = new int [ n * ( ( ( i + j ) / 2 ) - i + 1 ) ] ; int [ ] out2 = new int [ n * ( j - ( ( i + j ) / 2 ) ) ] ; mergeKArrays ( arr , i , ( i + j ) / 2 , out1 ) ; mergeKArrays ( arr , ( i + j ) / 2 + 1 , j , out2 ) ; mergeArrays ( out1 , out2 , n * ( ( ( i + j ) / 2 ) - i + 1 ) , n * ( j - ( ( i + j ) / 2 ) ) , output ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 2 , 6 , 12 , 34 } , { 1 , 9 , 20 , 1000 } , { 23 , 34 , 90 , 2000 } } ; int k = arr . length ; int [ ] output = new int [ n * k ] ; mergeKArrays ( arr , 0 , 2 , output ) ; System . out . print ( " Merged ▁ array ▁ is ▁ " + "NEW_LINE"); printArray ( output , n * k ) ; } }
import java . util . * ; class GFG { static void generate_derangement ( int N ) { int [ ] S = new int [ N + 1 ] ; PriorityQueue < Integer > PQ = new PriorityQueue < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) { S [ i ] = i ; PQ . add ( S [ i ] ) ; } int [ ] D = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { int d = PQ . peek ( ) ; PQ . remove ( ) ; if ( d != S [ i ] i == N ) { D [ i ] = d ; } else { D [ i ] = PQ . peek ( ) ; PQ . remove ( ) ; PQ . add ( d ) ; } } if ( D [ N ] == S [ N ] ) { int t = D [ N - 1 ] ; D [ N - 1 ] = D [ N ] ; D [ N ] = t ; } for ( int i = 1 ; i <= N ; i ++ ) System . out . printf ( " % d ▁ " , D [ i ] ) ; System . out . printf ( "NEW_LINE"); } public static void main ( String [ ] args ) { generate_derangement ( 10 ) ; } }
class GFG { static void generate_derangement ( int N ) { int S [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int D [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N ) { D [ N ] = S [ N - 1 ] ; D [ N - 1 ] = S [ N ] ; } else { D [ i ] = i + 1 ; D [ i + 1 ] = i ; } } for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( D [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { generate_derangement ( 10 ) ; } }
import java . io . * ; import java . util . Collections ; import java . util . PriorityQueue ; class GFG { public static void printLargest ( int a [ ] , int n ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( Collections . reverseOrder ( ) ) ; int res [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { pq . add ( a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int p = pq . peek ( ) ; pq . remove ( ) ; if ( p != a [ i ] i == n - 1 ) { res [ i ] = p ; } else { res [ i ] = pq . peek ( ) ; pq . remove ( ) ; pq . add ( p ) ; } } if ( res [ n - 1 ] == a [ n - 1 ] ) { res [ n - 1 ] = res [ n - 2 ] ; res [ n - 2 ] = a [ n - 1 ] ; } System . out . println ( " Largest ▁ Derangement " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; int seq [ ] = { 92 , 3 , 52 , 13 , 2 , 31 , 1 } ; printLargest ( seq , n ) ; } }
import java . io . * ; class GFG { static void heapify ( int arr [ ] , int n , int i ) { int smallest = i ; int l = 2 * i + 1 ; int r = 2 * i + 2 ; if ( l < n && arr [ l ] < arr [ smallest ] ) smallest = l ; if ( r < n && arr [ r ] < arr [ smallest ] ) smallest = r ; if ( smallest != i ) { int temp = arr [ i ] ; arr [ i ] = arr [ smallest ] ; arr [ smallest ] = temp ; heapify ( arr , n , smallest ) ; } } static void heapSort ( int arr [ ] , int n ) { for ( int i = n / 2 - 1 ; i >= 0 ; i -- ) heapify ( arr , n , i ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapify ( arr , i , 0 ) ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 3 , 2 , 9 } ; int n = arr . length ; heapSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class KeyComparator implements Comparator < Key > { public int compare ( Key k1 , Key k2 ) { if ( k1 . freq < k2 . freq ) return 1 ; else if ( k1 . freq > k2 . freq ) return - 1 ; return 0 ; } } class Key { int freq ; char ch ; Key ( int val , char c ) { freq = val ; ch = c ; } } class GFG { static int MAX_CHAR = 26 ; static void rearrangeString ( String str ) { int n = str . length ( ) ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ; PriorityQueue < Key > pq = new PriorityQueue < > ( new KeyComparator ( ) ) ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { int val = c - ' a ' ; if ( count [ val ] > 0 ) pq . add ( new Key ( count [ val ] , c ) ) ; } str = " " ; Key prev = new Key ( - 1 , ' # ' ) ; while ( pq . size ( ) != 0 ) { Key k = pq . peek ( ) ; pq . poll ( ) ; str = str + k . ch ; if ( prev . freq > 0 ) pq . add ( prev ) ; ( k . freq ) -- ; prev = k ; } if ( n != str . length ( ) ) System . out . println ( " ▁ Not ▁ valid ▁ String ▁ " ) ; else System . out . println ( str ) ; } public static void main ( String args [ ] ) { String str = " bbbaa " ; rearrangeString ( str ) ; } }
class GFG { static int n = 7 ; static void minheapify ( int [ ] a , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { int t = a [ small ] ; a [ small ] = a [ index ] ; a [ index ] = t ; minheapify ( a , small ) ; } } public static void main ( String [ ] args ) { int i = 0 ; int k1 = 3 ; int k2 = 6 ; int [ ] a = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; int ans = 0 ; for ( i = ( n / 2 ) - 1 ; i >= 0 ; i -- ) { minheapify ( a , i ) ; } k1 -- ; k2 -- ; for ( i = 0 ; i <= k1 ; i ++ ) { a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } for ( i = k1 + 1 ; i < k2 ; i ++ ) { ans += a [ 0 ] ; a [ 0 ] = a [ n - 1 ] ; n -- ; minheapify ( a , 0 ) ; } System . out . println ( ans ) ; } }
import java . util . Arrays ; public class AQRQ { static int minSum ( int a [ ] , int n ) { Arrays . sort ( a ) ; int num1 = 0 ; int num2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) num1 = num1 * 10 + a [ i ] ; else num2 = num2 * 10 + a [ i ] ; } return num2 + num1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 0 , 7 , 4 } ; int n = arr . length ; System . out . println ( " The ▁ required ▁ sum ▁ is ▁ " + minSum ( arr , n ) ) ; } }
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; int sum ( Node t ) { if ( t == null ) return 0 ; return t . key + sum ( t . left ) + sum ( t . right ) ; } int uncoveredSumLeft ( Node t ) { if ( t . left == null && t . right == null ) return t . key ; if ( t . left != null ) return t . key + uncoveredSumLeft ( t . left ) ; else return t . key + uncoveredSumLeft ( t . right ) ; } int uncoveredSumRight ( Node t ) { if ( t . left == null && t . right == null ) return t . key ; if ( t . right != null ) return t . key + uncoveredSumRight ( t . right ) ; else return t . key + uncoveredSumRight ( t . left ) ; } int uncoverSum ( Node t ) { int lb = 0 , rb = 0 ; if ( t . left != null ) lb = uncoveredSumLeft ( t . left ) ; if ( t . right != null ) rb = uncoveredSumRight ( t . right ) ; return t . key + lb + rb ; } boolean isSumSame ( Node root ) { int sumUC = uncoverSum ( root ) ; int sumT = sum ( root ) ; return ( sumUC == ( sumT - sumUC ) ) ; } void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; inorder ( root . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 3 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 4 ) ; tree . root . left . right . right = new Node ( 7 ) ; tree . root . right = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; tree . root . right . right . left = new Node ( 13 ) ; if ( tree . isSumSame ( tree . root ) ) System . out . println ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ same " ) ; else System . out . println ( " Sum ▁ of ▁ covered ▁ and ▁ uncovered ▁ is ▁ not ▁ same " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node prev , next ; static Node getnode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . prev = newNode . next = null ; return newNode ; } } ; static class Deque { Node front ; Node rear ; int Size ; Deque ( ) { front = rear = null ; Size = 0 ; } boolean isEmpty ( ) { return ( front == null ) ; } int size ( ) { return Size ; } void insertFront ( int data ) { Node newNode = Node . getnode ( data ) ; if ( newNode == null ) System . out . print ( "OverFlowNEW_LINE"); else { if ( front == null ) rear = front = newNode ; else { newNode . next = front ; front . prev = newNode ; front = newNode ; } Size ++ ; } } void insertRear ( int data ) { Node newNode = Node . getnode ( data ) ; if ( newNode == null ) System . out . print ( "OverFlowNEW_LINE"); else { if ( rear == null ) front = rear = newNode ; else { newNode . prev = rear ; rear . next = newNode ; rear = newNode ; } Size ++ ; } } void deleteFront ( ) { if ( isEmpty ( ) ) System . out . print ( "UnderFlowNEW_LINE"); else { Node temp = front ; front = front . next ; if ( front == null ) rear = null ; else front . prev = null ; Size -- ; } } void deleteRear ( ) { if ( isEmpty ( ) ) System . out . print ( "UnderFlowNEW_LINE"); else { Node temp = rear ; rear = rear . prev ; if ( rear == null ) front = null ; else rear . next = null ; Size -- ; } } int getFront ( ) { if ( isEmpty ( ) ) return - 1 ; return front . data ; } int getRear ( ) { if ( isEmpty ( ) ) return - 1 ; return rear . data ; } void erase ( ) { rear = null ; while ( front != null ) { Node temp = front ; front = front . next ; } Size = 0 ; } } public static void main ( String [ ] args ) { Deque dq = new Deque ( ) ; System . out . print ( "Insert element '5' at rear endNEW_LINE"); dq . insertRear ( 5 ) ; System . out . print ( "Insert element '10' at rear endNEW_LINE"); dq . insertRear ( 10 ) ; System . out . print ( " Rear ▁ end ▁ element : ▁ " + dq . getRear ( ) + "NEW_LINE"); dq . deleteRear ( ) ; System . out . print ( " After ▁ deleting ▁ rear ▁ element ▁ new ▁ rear " + " ▁ is : ▁ " + dq . getRear ( ) + "NEW_LINE"); System . out . print ( "Inserting element '15' at front end NEW_LINE"); dq . insertFront ( 15 ) ; System . out . print ( " Front ▁ end ▁ element : ▁ " + dq . getFront ( ) + "NEW_LINE"); System . out . print ( " Number ▁ of ▁ elements ▁ in ▁ Deque : ▁ " + dq . size ( ) + "NEW_LINE"); dq . deleteFront ( ) ; System . out . print ( " After ▁ deleting ▁ front ▁ element ▁ new ▁ " + " front ▁ is : ▁ " + dq . getFront ( ) + "NEW_LINE"); } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Leaf { int leaflevel = 0 ; } class BinaryTree { Node root ; Leaf mylevel = new Leaf ( ) ; boolean checkUtil ( Node node , int level , Leaf leafLevel ) { if ( node == null ) return true ; if ( node . left == null && node . right == null ) { if ( leafLevel . leaflevel == 0 ) { leafLevel . leaflevel = level ; return true ; } return ( level == leafLevel . leaflevel ) ; } return checkUtil ( node . left , level + 1 , leafLevel ) && checkUtil ( node . right , level + 1 , leafLevel ) ; } boolean check ( Node node ) { int level = 0 ; return checkUtil ( node , level , mylevel ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 12 ) ; tree . root . left = new Node ( 5 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 9 ) ; tree . root . left . left . left = new Node ( 1 ) ; tree . root . left . right . left = new Node ( 1 ) ; if ( tree . check ( tree . root ) ) System . out . println ( " Leaves ▁ are ▁ at ▁ same ▁ level " ) ; else System . out . println ( " Leaves ▁ are ▁ not ▁ at ▁ same ▁ level " ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Vector < Node > child ; Node ( int data ) { key = data ; child = new Vector < Node > ( ) ; } } ; static int numberOfSiblings ( Node root , int x ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node p = q . peek ( ) ; q . remove ( ) ; for ( int i = 0 ; i < p . child . size ( ) ; i ++ ) { if ( p . child . get ( i ) . key == x ) return p . child . size ( ) - 1 ; q . add ( p . child . get ( i ) ) ; } } return - 1 ; } public static void main ( String args [ ] ) { Node root = new Node ( 50 ) ; ( root . child ) . add ( new Node ( 2 ) ) ; ( root . child ) . add ( new Node ( 30 ) ) ; ( root . child ) . add ( new Node ( 14 ) ) ; ( root . child ) . add ( new Node ( 60 ) ) ; ( root . child . get ( 0 ) . child ) . add ( new Node ( 15 ) ) ; ( root . child . get ( 0 ) . child ) . add ( new Node ( 25 ) ) ; ( root . child . get ( 0 ) . child . get ( 1 ) . child ) . add ( new Node ( 70 ) ) ; ( root . child . get ( 0 ) . child . get ( 1 ) . child ) . add ( new Node ( 100 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 6 ) ) ; ( root . child . get ( 1 ) . child ) . add ( new Node ( 1 ) ) ; ( root . child . get ( 2 ) . child ) . add ( new Node ( 7 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 17 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 99 ) ) ; ( root . child . get ( 2 ) . child . get ( 0 ) . child ) . add ( new Node ( 27 ) ) ; ( root . child . get ( 3 ) . child ) . add ( new Node ( 16 ) ) ; int x = 100 ; System . out . println ( numberOfSiblings ( root , x ) ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { int data = key ; left = right = null ; } } class GFG { static boolean checkLevelLeafNode ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int result = Integer . MAX_VALUE ; int level = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } if ( temp . right != null ) { q . add ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) { if ( result == Integer . MAX_VALUE ) result = level ; else if ( result != level ) return false ; } } size -- ; } } return true ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . right = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; boolean result = checkLevelLeafNode ( root ) ; if ( result == true ) System . out . println ( " All ▁ leaf ▁ nodes ▁ are ▁ at ▁ same ▁ level " ) ; else System . out . println ( " Leaf ▁ nodes ▁ not ▁ at ▁ same ▁ level " ) ; } }
import java . util . LinkedList ; import java . util . Queue ; public class QueueExample { public static void main ( String [ ] args ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) q . add ( i ) ; System . out . println ( " Elements ▁ of ▁ queue - " + q ) ; int removedele = q . remove ( ) ; System . out . println ( " removed ▁ element - " + removedele ) ; System . out . println ( q ) ; int head = q . peek ( ) ; System . out . println ( " head ▁ of ▁ queue - " + head ) ; int size = q . size ( ) ; System . out . println ( " Size ▁ of ▁ queue - " + size ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void stack_push ( Stack < Integer > stack ) { for ( int i = 0 ; i < 5 ; i ++ ) { stack . push ( i ) ; } } static void stack_pop ( Stack < Integer > stack ) { System . out . println ( " Pop ▁ : " ) ; for ( int i = 0 ; i < 5 ; i ++ ) { Integer y = ( Integer ) stack . pop ( ) ; System . out . println ( y ) ; } } static void stack_peek ( Stack < Integer > stack ) { Integer element = ( Integer ) stack . peek ( ) ; System . out . println ( " Element ▁ on ▁ stack ▁ top ▁ : ▁ " + element ) ; } static void stack_search ( Stack < Integer > stack , int element ) { Integer pos = ( Integer ) stack . search ( element ) ; if ( pos == - 1 ) System . out . println ( " Element ▁ not ▁ found " ) ; else System . out . println ( " Element ▁ is ▁ found ▁ at ▁ position ▁ " + pos ) ; } public static void main ( String [ ] args ) { Stack < Integer > stack = new Stack < Integer > ( ) ; stack_push ( stack ) ; stack_pop ( stack ) ; stack_push ( stack ) ; stack_peek ( stack ) ; stack_search ( stack , 2 ) ; stack_search ( stack , 6 ) ; } }
import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; public class Queue_reverse { static Queue < Integer > queue ; static void Print ( ) { while ( ! queue . isEmpty ( ) ) { System . out . print ( queue . peek ( ) + " ▁ " ) ; queue . remove ( ) ; } } static Queue < Integer > reverseQueue ( Queue < Integer > q ) { if ( q . isEmpty ( ) ) return q ; int data = q . peek ( ) ; q . remove ( ) ; q = reverseQueue ( q ) ; q . add ( data ) ; return q ; } public static void main ( String args [ ] ) { queue = new LinkedList < Integer > ( ) ; queue . add ( 56 ) ; queue . add ( 27 ) ; queue . add ( 30 ) ; queue . add ( 45 ) ; queue . add ( 85 ) ; queue . add ( 92 ) ; queue . add ( 58 ) ; queue . add ( 80 ) ; queue . add ( 90 ) ; queue . add ( 100 ) ; queue = reverseQueue ( queue ) ; Print ( ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class GFG { public static int minIndex ( Queue < Integer > list , int sortIndex ) { int min_index = - 1 ; int min_value = Integer . MAX_VALUE ; int s = list . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int current = list . peek ( ) ; list . poll ( ) ; if ( current <= min_value && i <= sortIndex ) { min_index = i ; min_value = current ; } list . add ( current ) ; } return min_index ; } public static void insertMinToRear ( Queue < Integer > list , int min_index ) { int min_value = 0 ; int s = list . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int current = list . peek ( ) ; list . poll ( ) ; if ( i != min_index ) list . add ( current ) ; else min_value = current ; } list . add ( min_value ) ; } public static void sortQueue ( Queue < Integer > list ) { for ( int i = 1 ; i <= list . size ( ) ; i ++ ) { int min_index = minIndex ( list , list . size ( ) - i ) ; insertMinToRear ( list , min_index ) ; } } public static void main ( String [ ] args ) { Queue < Integer > list = new LinkedList < Integer > ( ) ; list . add ( 30 ) ; list . add ( 11 ) ; list . add ( 15 ) ; list . add ( 4 ) ; sortQueue ( list ) ; while ( list . isEmpty ( ) == false ) { System . out . print ( list . peek ( ) + " ▁ " ) ; list . poll ( ) ; } } }
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; int count ( Node node ) { if ( node == null ) return 0 ; return count ( node . left ) + count ( node . right ) + 1 ; } boolean checkRec ( Node node , int n ) { if ( node == null ) return false ; if ( count ( node ) == n - count ( node ) ) return true ; return checkRec ( node . left , n ) || checkRec ( node . right , n ) ; } boolean check ( Node node ) { int n = count ( node ) ; return checkRec ( node , n ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 1 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 4 ) ; if ( tree . check ( tree . root ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . Deque ; import java . util . LinkedList ; public class SlidingWindow { static void printMax ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < Integer > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ! Qi . isEmpty ( ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } for ( ; i < n ; ++ i ) { System . out . print ( arr [ Qi . peek ( ) ] + " ▁ " ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peek ( ) <= i - k ) Qi . removeFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } System . out . print ( arr [ Qi . peek ( ) ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int k = 3 ; printMax ( arr , arr . length , k ) ; } }
import java . util . Deque ; import java . util . LinkedList ; public class Geeks { public static int SumOfKsubArray ( int arr [ ] , int k ) { int sum = 0 ; Deque < Integer > S = new LinkedList < > ( ) , G = new LinkedList < > ( ) ; int i = 0 ; for ( i = 0 ; i < k ; i ++ ) { while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ; while ( ! G . isEmpty ( ) && arr [ G . peekLast ( ) ] <= arr [ i ] ) G . removeLast ( ) ; G . addLast ( i ) ; S . addLast ( i ) ; } for ( ; i < arr . length ; i ++ ) { sum += arr [ S . peekFirst ( ) ] + arr [ G . peekFirst ( ) ] ; while ( ! S . isEmpty ( ) && S . peekFirst ( ) <= i - k ) S . removeFirst ( ) ; while ( ! G . isEmpty ( ) && G . peekFirst ( ) <= i - k ) G . removeFirst ( ) ; while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ; while ( ! G . isEmpty ( ) && arr [ G . peekLast ( ) ] <= arr [ i ] ) G . removeLast ( ) ; G . addLast ( i ) ; S . addLast ( i ) ; } sum += arr [ S . peekFirst ( ) ] + arr [ G . peekFirst ( ) ] ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 5 , - 1 , 7 , - 3 , - 1 , - 2 } ; int k = 3 ; System . out . println ( SumOfKsubArray ( arr , k ) ) ; } }
import java . io . * ; class GFG { static int N = 3 ; static int M = 4 ; static void printDistance ( int mat [ ] [ ] ) { int ans [ ] [ ] = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) ans [ i ] [ j ] = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) for ( int l = 0 ; l < M ; l ++ ) { if ( mat [ k ] [ l ] == 1 ) ans [ i ] [ j ] = Math . min ( ans [ i ] [ j ] , Math . abs ( i - k ) + Math . abs ( j - l ) ) ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) System . out . print ( ans [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 , 1 } , { 0 , 0 , 1 , 1 } , { 0 , 1 , 1 , 0 } } ; printDistance ( mat ) ; } }
import java . util . * ; class solution { static void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { boolean flag ; for ( int i = 0 ; i < ( n - k + 1 ) ; i ++ ) { flag = false ; for ( int j = 0 ; j < k ; j ++ ) { if ( arr [ i + j ] < 0 ) { System . out . print ( ( arr [ i + j ] ) + " ▁ " ) ; flag = true ; break ; } } if ( ! flag ) System . out . print ( "0" + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . length ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; } }
import java . util . * ; class GFG { static void printFirstNegativeInteger ( int arr [ ] , int n , int k ) { LinkedList < Integer > Di = new LinkedList < > ( ) ; int i ; for ( i = 0 ; i < k ; i ++ ) if ( arr [ i ] < 0 ) Di . add ( i ) ; for ( ; i < n ; i ++ ) { if ( ! Di . isEmpty ( ) ) System . out . print ( arr [ Di . peek ( ) ] + " ▁ " ) ; else System . out . print ( "0" + " ▁ " ) ; while ( ( ! Di . isEmpty ( ) ) && Di . peek ( ) < ( i - k + 1 ) ) Di . remove ( ) ; if ( arr [ i ] < 0 ) Di . add ( i ) ; } if ( ! Di . isEmpty ( ) ) System . out . print ( arr [ Di . peek ( ) ] + " ▁ " ) ; else System . out . print ( "0" + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . length ; int k = 3 ; printFirstNegativeInteger ( arr , n , k ) ; } }
import java . util . * ; class GFG { static void printFirstNegativeInteger ( int arr [ ] , int k , int n ) { int firstNegativeIndex = 0 ; int firstNegativeElement ; for ( int i = k - 1 ; i < n ; i ++ ) { while ( ( firstNegativeIndex < i ) && ( firstNegativeIndex <= i - k arr [ firstNegativeIndex ] > 0 ) ) { firstNegativeIndex ++ ; } if ( arr [ firstNegativeIndex ] < 0 ) { firstNegativeElement = arr [ firstNegativeIndex ] ; } else { firstNegativeElement = 0 ; } System . out . print ( firstNegativeElement + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , - 1 , - 7 , 8 , - 15 , 30 , 16 , 28 } ; int n = arr . length ; int k = 3 ; printFirstNegativeInteger ( arr , k , n ) ; } }
import java . util . ArrayList ; import java . util . Collections ; import java . util . LinkedList ; import java . util . Queue ; public class GFG { static class Node { Node left , right ; int data ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static boolean areAnagrams ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new LinkedList < Node > ( ) ; Queue < Node > q2 = new LinkedList < Node > ( ) ; q1 . add ( root1 ) ; q2 . add ( root2 ) ; while ( true ) { int n1 = q1 . size ( ) , n2 = q2 . size ( ) ; if ( n1 != n2 ) return false ; if ( n1 == 0 ) break ; ArrayList < Integer > curr_level1 = new ArrayList < > ( ) ; ArrayList < Integer > curr_level2 = new ArrayList < > ( ) ; while ( n1 > 0 ) { Node node1 = q1 . peek ( ) ; q1 . remove ( ) ; if ( node1 . left != null ) q1 . add ( node1 . left ) ; if ( node1 . right != null ) q1 . add ( node1 . right ) ; n1 -- ; Node node2 = q2 . peek ( ) ; q2 . remove ( ) ; if ( node2 . left != null ) q2 . add ( node2 . left ) ; if ( node2 . right != null ) q2 . add ( node2 . right ) ; curr_level1 . add ( node1 . data ) ; curr_level2 . add ( node2 . data ) ; } Collections . sort ( curr_level1 ) ; Collections . sort ( curr_level2 ) ; if ( ! curr_level1 . equals ( curr_level2 ) ) return false ; } return true ; } public static void main ( String args [ ] ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 3 ) ; root1 . right = new Node ( 2 ) ; root1 . right . left = new Node ( 5 ) ; root1 . right . right = new Node ( 4 ) ; Node root2 = new Node ( 1 ) ; root2 . left = new Node ( 2 ) ; root2 . right = new Node ( 3 ) ; root2 . left . left = new Node ( 4 ) ; root2 . left . right = new Node ( 5 ) ; System . out . println ( areAnagrams ( root1 , root2 ) ? " Yes " : " No " ) ; } }
import java . util . * ; class GfG { static int preIndex = 0 ; static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static int search ( int arr [ ] , int strt , int end , int value ) { for ( int i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return - 1 ; } static Node buildTree ( int in [ ] , int pre [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; Node tNode = newNode ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( in , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } static int checkPostorder ( Node node , int postOrder [ ] , int index ) { if ( node == null ) return index ; index = checkPostorder ( node . left , postOrder , index ) ; index = checkPostorder ( node . right , postOrder , index ) ; if ( node . data == postOrder [ index ] ) index ++ ; else return - 1 ; return index ; } public static void main ( String [ ] args ) { int inOrder [ ] = { 4 , 2 , 5 , 1 , 3 } ; int preOrder [ ] = { 1 , 2 , 4 , 5 , 3 } ; int postOrder [ ] = { 4 , 5 , 2 , 3 , 1 } ; int len = inOrder . length ; Node root = buildTree ( inOrder , preOrder , 0 , len - 1 ) ; int index = checkPostorder ( root , postOrder , 0 ) ; if ( index == len ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
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 " ) ; } }
class GFG { final static int MAX = 1000 ; static boolean [ ] [ ] has = new boolean [ MAX + 1 ] [ 2 ] ; static boolean search ( int X ) { if ( X >= 0 ) { if ( has [ X ] [ 0 ] == true ) { return true ; } else { return false ; } } X = Math . abs ( X ) ; if ( has [ X ] [ 1 ] == true ) { return true ; } return false ; } static void insert ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { has [ a [ i ] ] [ 0 ] = true ; } else { has [ Math . abs ( a [ i ] ) ] [ 1 ] = true ; } } } public static void main ( String args [ ] ) { int a [ ] = { - 1 , 9 , - 5 , - 8 , - 5 , - 2 } ; int n = a . length ; insert ( a , n ) ; int X = - 5 ; if ( search ( X ) == true ) { System . out . println ( " Present " ) ; } else { System . out . println ( " Not ▁ Present " ) ; } } }
import java . io . * ; import java . util . * ; public class detheap { static boolean isMinHeap ( int [ ] level ) { int n = level . length - 1 ; for ( int i = ( n / 2 - 1 ) ; i >= 0 ; i -- ) { if ( level [ i ] > level [ 2 * i + 1 ] ) return false ; if ( 2 * i + 2 < n ) { if ( level [ i ] > level [ 2 * i + 2 ] ) return false ; } } return true ; } public static void main ( String [ ] args ) throws IOException { int [ ] level = new int [ ] { 10 , 15 , 14 , 25 , 30 } ; if ( isMinHeap ( level ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
import java . util . * ; class GFG { static int minDelete ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) freq . put ( arr [ i ] , freq . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; int max_freq = Integer . MIN_VALUE ; for ( Map . Entry < Integer , Integer > entry : freq . entrySet ( ) ) max_freq = Math . max ( max_freq , entry . getValue ( ) ) ; return n - max_freq ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 4 , 4 , 2 , 4 } ; int n = arr . length ; System . out . print ( minDelete ( arr , n ) ) ; } }
import java . util . * ; class GFG { public static int minOperation ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; else hash . put ( arr [ i ] , 1 ) ; int max_count = 0 ; Set < Integer > s = hash . keySet ( ) ; for ( int i : s ) if ( max_count < hash . get ( i ) ) max_count = hash . get ( i ) ; return ( n - max_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . print ( minOperation ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxDistance ( int [ ] arr , int n ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; int max_dist = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! map . containsKey ( arr [ i ] ) ) map . put ( arr [ i ] , i ) ; else max_dist = Math . max ( max_dist , i - map . get ( arr [ i ] ) ) ; } return max_dist ; } public static void main ( String args [ ] ) { int [ ] arr = { 3 , 2 , 1 , 2 , 1 , 4 , 5 , 8 , 6 , 7 , 4 , 2 } ; int n = arr . length ; System . out . println ( maxDistance ( arr , n ) ) ; } }
import java . util . * ; class Main { static boolean checkDuplicatesWithinK ( int arr [ ] , int k ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( set . contains ( arr [ i ] ) ) return true ; set . add ( arr [ i ] ) ; if ( i >= k ) set . remove ( arr [ i - k ] ) ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; if ( checkDuplicatesWithinK ( arr , 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . ArrayList ; public class GFG { static void findDuplicates ( int arr [ ] , int len ) { boolean ifPresent = false ; ArrayList < Integer > al = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { if ( al . contains ( arr [ i ] ) ) { break ; } else { al . add ( arr [ i ] ) ; ifPresent = true ; } } } } if ( ifPresent == true ) { System . out . print ( al + " ▁ " ) ; } else { System . out . print ( " No ▁ duplicates ▁ present ▁ in ▁ arrays " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 40 , 12 , 5 , 6 , 5 , 12 , 11 } ; int n = arr . length ; findDuplicates ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Node { int data ; Node left , right ; public Node ( int x ) { data = x ; left = right = null ; } public boolean isLeaf ( ) { return ( left == null && right == null ) ; } } class LeafOrderTraversal { public static boolean isSame ( Node root1 , Node root2 ) { Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . empty ( ) || ! s2 . empty ( ) ) { if ( s1 . empty ( ) || s2 . empty ( ) ) return false ; Node temp1 = s1 . pop ( ) ; while ( temp1 != null && ! temp1 . isLeaf ( ) ) { if ( temp1 . right != null ) s1 . push ( temp1 . right ) ; if ( temp1 . left != null ) s1 . push ( temp1 . left ) ; temp1 = s1 . pop ( ) ; } Node temp2 = s2 . pop ( ) ; while ( temp2 != null && ! temp2 . isLeaf ( ) ) { if ( temp2 . right != null ) s2 . push ( temp2 . right ) ; if ( temp2 . left != null ) s2 . push ( temp2 . left ) ; temp2 = s2 . pop ( ) ; } if ( temp1 == null && temp2 != null ) return false ; if ( temp1 != null && temp2 == null ) return false ; if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) return false ; } } return true ; } public static void main ( String [ ] args ) { Node root1 = new Node ( 1 ) ; root1 . left = new Node ( 2 ) ; root1 . right = new Node ( 3 ) ; root1 . left . left = new Node ( 4 ) ; root1 . right . left = new Node ( 6 ) ; root1 . right . right = new Node ( 7 ) ; Node root2 = new Node ( 0 ) ; root2 . left = new Node ( 1 ) ; root2 . right = new Node ( 5 ) ; root2 . left . right = new Node ( 4 ) ; root2 . right . left = new Node ( 6 ) ; root2 . right . right = new Node ( 7 ) ; if ( isSame ( root1 , root2 ) ) System . out . println ( " Same " ) ; else System . out . println ( " Not ▁ Same " ) ; } }
import java . util . * ; class GFG { static int mostFrequent ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( mostFrequent ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GfG { static void smallestSubsegment ( int a [ ] , int n ) { HashMap < Integer , Integer > left = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; int mx = 0 ; int mn = - 1 , strindex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count . get ( x ) == null ) { left . put ( x , i ) ; count . put ( x , 1 ) ; } else count . put ( x , count . get ( x ) + 1 ) ; if ( count . get ( x ) > mx ) { mx = count . get ( x ) ; mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } else if ( ( count . get ( x ) == mx ) && ( i - left . get ( x ) + 1 < mn ) ) { mn = i - left . get ( x ) + 1 ; strindex = left . get ( x ) ; } } for ( int i = strindex ; i < strindex + mn ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 2 , 1 } ; int n = A . length ; smallestSubsegment ( A , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . put ( arr [ i ] , 1 ) ; if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; } } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; System . out . println ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class Pair { int first , second ; } class GFG { static void top3Repeated ( int [ ] arr , int n ) { if ( n < 3 ) { System . out . print ( " Invalid ▁ Input " ) ; return ; } TreeMap < Integer , Integer > freq = new TreeMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , 1 + freq . get ( arr [ i ] ) ) ; else freq . put ( arr [ i ] , 1 ) ; Pair x = new Pair ( ) ; Pair y = new Pair ( ) ; Pair z = new Pair ( ) ; x . first = y . first = z . first = Integer . MIN_VALUE ; for ( Map . Entry curr : freq . entrySet ( ) ) { if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > x . first ) { z . first = y . first ; z . second = y . second ; y . first = x . first ; y . second = x . second ; x . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; x . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } else if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > y . first ) { z . first = y . first ; z . second = y . second ; y . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; y . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } else if ( Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) > z . first ) { z . first = Integer . parseInt ( String . valueOf ( curr . getValue ( ) ) ) ; z . second = Integer . parseInt ( String . valueOf ( curr . getKey ( ) ) ) ; } } System . out . print ( " Three ▁ largest ▁ elements ▁ are ▁ " + x . second + " ▁ " + y . second + " ▁ " + z . second ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 3 , 4 , 2 , 3 , 16 , 3 , 15 , 16 , 15 , 15 , 16 , 2 , 3 } ; int n = arr . length ; top3Repeated ( arr , n ) ; } }
class GFG { static void groupElements ( int arr [ ] , int n ) { boolean visited [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { visited [ i ] = false ; } for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; visited [ j ] = true ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 9 , 2 , 3 , 4 , 9 , 6 , 10 , 4 } ; int n = arr . length ; groupElements ( arr , n ) ; } }
public class disjoint1 { boolean aredisjoint ( int set1 [ ] , int set2 [ ] ) { for ( int i = 0 ; i < set1 . length ; i ++ ) { for ( int j = 0 ; j < set2 . length ; j ++ ) { if ( set1 [ i ] == set2 [ j ] ) return false ; } } return true ; } public static void main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; public class disjoint1 { boolean aredisjoint ( int set1 [ ] , int set2 [ ] ) { int i = 0 , j = 0 ; Arrays . sort ( set1 ) ; Arrays . sort ( set2 ) ; while ( i < set1 . length && j < set2 . length ) { if ( set1 [ i ] < set2 [ j ] ) i ++ ; else if ( set1 [ i ] > set2 [ j ] ) j ++ ; else return false ; } return true ; } public static void main ( String [ ] args ) { disjoint1 dis = new disjoint1 ( ) ; int set1 [ ] = { 12 , 34 , 11 , 9 , 3 } ; int set2 [ ] = { 7 , 2 , 1 , 5 } ; boolean result = dis . aredisjoint ( set1 , set2 ) ; if ( result ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . * ; class Main { static boolean areDisjoint ( int set1 [ ] , int set2 [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < set1 . length ; i ++ ) set . add ( set1 [ i ] ) ; for ( int i = 0 ; i < set2 . length ; i ++ ) if ( set . contains ( set2 [ i ] ) ) return false ; return true ; } public static void main ( String [ ] args ) { int set1 [ ] = { 10 , 5 , 3 , 4 , 6 } ; int set2 [ ] = { 8 , 7 , 9 , 3 } ; if ( areDisjoint ( set1 , set2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findSum ( int [ ] A , int [ ] B , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . containsKey ( A [ i ] ) ) hash . put ( A [ i ] , 1 + hash . get ( A [ i ] ) ) ; else hash . put ( A [ i ] , 1 ) ; if ( hash . containsKey ( B [ i ] ) ) hash . put ( B [ i ] , 1 + hash . get ( B [ i ] ) ) ; else hash . put ( B [ i ] , 1 ) ; } int sum = 0 ; for ( Map . Entry entry : hash . entrySet ( ) ) { if ( Integer . parseInt ( ( entry . getValue ( ) ) . toString ( ) ) == 1 ) sum += Integer . parseInt ( ( entry . getKey ( ) ) . toString ( ) ) ; } return sum ; } public static void main ( String args [ ] ) { int [ ] A = { 5 , 4 , 9 , 2 , 3 } ; int [ ] B = { 2 , 8 , 7 , 6 , 3 } ; int n = A . length ; System . out . println ( findSum ( A , B , n ) ) ; } }
class GFG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = a . length ; int m = b . length ; findMissing ( a , b , n , m ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static boolean areEqual ( int arr1 [ ] , int arr2 [ ] ) { int n = arr1 . length ; int m = arr2 . length ; if ( n != m ) return false ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 5 , 2 , 5 , 2 } ; int arr2 [ ] = { 2 , 3 , 5 , 5 , 2 } ; if ( areEqual ( arr1 , arr2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
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 ) ; } } } static int find_maximum ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > b = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = Math . min ( 1 + i , n - i ) ; if ( ! b . containsKey ( x ) ) b . put ( x , d ) ; else { b . put ( x , Math . min ( d , b . get ( x ) ) ) ; } } int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( x != k - x && b . containsKey ( k - x ) ) ans = Math . min ( Math . max ( b . get ( x ) , b . get ( k - x ) ) , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 5 , 8 , 6 , 7 } ; int K = 11 ; int n = a . length ; System . out . println ( find_maximum ( a , n , K ) ) ; } }
class GFG { boolean isProduct ( int arr [ ] , int n , int x ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] == x ) return true ; return false ; } public static void main ( String [ ] args ) { GFG g = new GFG ( ) ; int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . length ; if ( g . isProduct ( arr , n , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; x = 190 ; if ( g . isProduct ( arr , n , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . HashSet ; class GFG { static boolean isProduct ( int arr [ ] , int n , int x ) { HashSet < Integer > hset = new HashSet < > ( ) ; if ( n < 2 ) return false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { if ( x == 0 ) return true ; else continue ; } if ( x % arr [ i ] == 0 ) { if ( hset . contains ( x / arr [ i ] ) ) return true ; hset . add ( arr [ i ] ) ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 9 , 40 } ; int x = 400 ; int n = arr . length ; if ( isProduct ( arr , arr . length , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; x = 190 ; if ( isProduct ( arr , arr . length , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; public class PrintMissing { static void printMissing ( int ar [ ] , int low , int high ) { Arrays . sort ( ar ) ; int index = ceilindex ( ar , low , 0 , ar . length - 1 ) ; int x = low ; while ( index < ar . length && x <= high ) { if ( ar [ index ] != x ) { System . out . print ( x + " ▁ " ) ; } else index ++ ; x ++ ; } while ( x <= high ) { System . out . print ( x + " ▁ " ) ; x ++ ; } } static int ceilindex ( int ar [ ] , int val , int low , int high ) { if ( val < ar [ 0 ] ) return 0 ; if ( val > ar [ ar . length - 1 ] ) return ar . length ; int mid = ( low + high ) / 2 ; if ( ar [ mid ] == val ) return mid ; if ( ar [ mid ] < val ) { if ( mid + 1 < high && ar [ mid + 1 ] >= val ) return mid + 1 ; return ceilindex ( ar , val , mid + 1 , high ) ; } else { if ( mid - 1 >= low && ar [ mid - 1 ] < val ) return mid ; return ceilindex ( ar , val , low , mid - 1 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 4 } ; int low = 1 , high = 10 ; printMissing ( arr , low , high ) ; } }
import java . util . Arrays ; public class Print { static void printMissing ( int arr [ ] , int low , int high ) { boolean [ ] points_of_range = new boolean [ high - low + 1 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( low <= arr [ i ] && arr [ i ] <= high ) points_of_range [ arr [ i ] - low ] = true ; } for ( int x = 0 ; x <= high - low ; x ++ ) { if ( points_of_range [ x ] == false ) System . out . print ( ( low + x ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 4 } ; int low = 1 , high = 10 ; printMissing ( arr , low , high ) ; } }
import java . util . Arrays ; import java . util . HashSet ; public class Print { static void printMissing ( int ar [ ] , int low , int high ) { HashSet < Integer > hs = new HashSet < > ( ) ; for ( int i = 0 ; i < ar . length ; i ++ ) hs . add ( ar [ i ] ) ; for ( int i = low ; i <= high ; i ++ ) { if ( ! hs . contains ( i ) ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 4 } ; int low = 1 , high = 10 ; printMissing ( arr , low , high ) ; } }
import java . util . * ; class GFG { static int find ( int a [ ] , int b [ ] , int k , int n1 , int n2 ) { LinkedHashSet < Integer > s = new LinkedHashSet < > ( ) ; for ( int i = 0 ; i < n2 ; i ++ ) s . add ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( ! s . contains ( a [ i ] ) ) missing ++ ; if ( missing == k ) return a [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 } ; int b [ ] = { 4 , 10 , 6 , 8 , 12 } ; int n1 = a . length ; int n2 = b . length ; int k = 3 ; System . out . println ( find ( a , b , k , n1 , n2 ) ) ; } }
class GfG { static class Node { int key ; Node left , right ; } static int findADepth ( Node node ) { int d = 0 ; while ( node != null ) { d ++ ; node = node . left ; } return d ; } static boolean isPerfectRec ( Node root , int d , int level ) { if ( root == null ) return true ; if ( root . left == null && root . right == null ) return ( d == level + 1 ) ; if ( root . left == null root . right == null ) return false ; return isPerfectRec ( root . left , d , level + 1 ) && isPerfectRec ( root . right , d , level + 1 ) ; } static boolean isPerfect ( Node root ) { int d = findADepth ( root ) ; return isPerfectRec ( root , d , 0 ) ; } static Node newNode ( int k ) { Node node = new Node ( ) ; node . key = k ; node . right = null ; node . left = null ; return node ; } public static void main ( String args [ ] ) { Node root = null ; root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . left = newNode ( 60 ) ; root . right . right = newNode ( 70 ) ; if ( isPerfect ( root ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int findGreatest ( int [ ] arr , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . max ( result , arr [ i ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 30 , 10 , 9 , 3 , 35 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findGreatest ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , m . get ( arr [ i ] ) ) ; } } Arrays . sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m . get ( result ) == null || m . get ( result ) > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m . get ( result ) > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }
import java . util . HashMap ; import java . util . Map ; class GFG { static int subset ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; int res = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) res = Math . max ( res , entry . getValue ( ) ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 9 , 3 , 4 , 3 , 4 } ; int n = arr . length ; System . out . println ( subset ( arr , n ) ) ; } }
import java . util . * ; class GFG { public static int minRemove ( int a [ ] , int b [ ] , int n , int m ) { HashMap < Integer , Integer > countA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > countB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countA . containsKey ( a [ i ] ) ) countA . put ( a [ i ] , countA . get ( a [ i ] ) + 1 ) ; else countA . put ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( countB . containsKey ( b [ i ] ) ) countB . put ( b [ i ] , countB . get ( b [ i ] ) + 1 ) ; else countB . put ( b [ i ] , 1 ) ; } int res = 0 ; Set < Integer > s = countA . keySet ( ) ; for ( int x : s ) if ( countB . containsKey ( x ) ) res += Math . min ( countB . get ( x ) , countA . get ( x ) ) ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 3 , 4 , 5 , 8 } ; int n = a . length ; int m = b . length ; System . out . println ( minRemove ( a , b , n , m ) ) ; } }
import java . util . * ; class GFG { static class item { String name ; int price ; item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static class Com implements Comparator < item > { public int compare ( item a , item b ) { return a . name . compareTo ( b . name ) ; } } static int binary_search ( item list2 [ ] , int low , int high , String str ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( list2 [ mid ] . name . compareTo ( str ) == 0 ) return list2 [ mid ] . price ; else if ( list2 [ mid ] . name . compareTo ( str ) < 0 ) low = mid + 1 ; else high = mid - 1 ; } return - 1 ; } static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { Arrays . sort ( list2 , new Com ( ) ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int r = binary_search ( list2 , 0 , n - 1 , list1 [ i ] . name ) ; if ( ( r != - 1 ) && ( r != list1 [ i ] . price ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { item [ ] list1 = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , list2 , n ) ) ; } }
import java . util . * ; class GFG { static class item { String name ; int price ; public item ( String name , int price ) { this . name = name ; this . price = price ; } } ; static int countItems ( item list1 [ ] , int m , item list2 [ ] , int n ) { HashMap < String , Integer > um = new HashMap < > ( ) ; int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) um . put ( list1 [ i ] . name , list1 [ i ] . price ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( um . containsKey ( list2 [ i ] . name ) ) && ( um . get ( list2 [ i ] . name ) != list2 [ i ] . price ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { item list1 [ ] = { new item ( " apple " , 60 ) , new item ( " bread " , 20 ) , new item ( " wheat " , 50 ) , new item ( " oil " , 30 ) } ; item list2 [ ] = { new item ( " milk " , 20 ) , new item ( " bread " , 15 ) , new item ( " wheat " , 40 ) , new item ( " apple " , 60 ) } ; int m = list1 . length ; int n = list2 . length ; System . out . print ( " Count ▁ = ▁ " + countItems ( list1 , m , clist2 , n ) ) ; } }
import java . util . * ; class GFG { static void find ( Vector < String > list1 , Vector < String > list2 ) { Vector < String > res = new Vector < > ( ) ; int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < list2 . size ( ) && list1 . get ( i ) == list2 . get ( sum - i ) ) res . add ( list1 . get ( i ) ) ; if ( res . size ( ) > 0 ) break ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > list1 = new Vector < > ( ) ; list1 . add ( " GeeksforGeeks " ) ; list1 . add ( " Udemy " ) ; list1 . add ( " Coursera " ) ; list1 . add ( " edX " ) ; Vector < String > list2 = new Vector < > ( ) ; list2 . add ( " Codecademy " ) ; list2 . add ( " Khan ▁ Academy " ) ; list2 . add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }
import java . util . * ; class GFG { static void find ( Vector < String > list1 , Vector < String > list2 ) { Map < String , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < list1 . size ( ) ; i ++ ) map . put ( list1 . get ( i ) , i ) ; Vector < String > res = new Vector < String > ( ) ; int minsum = Integer . MAX_VALUE ; for ( int j = 0 ; j < list2 . size ( ) ; j ++ ) { if ( map . containsKey ( list2 . get ( j ) ) ) { int sum = j + map . get ( list2 . get ( j ) ) ; if ( sum < minsum ) { minsum = sum ; res . clear ( ) ; res . add ( list2 . get ( j ) ) ; } else if ( sum == minsum ) res . add ( list2 . get ( j ) ) ; } } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > list1 = new Vector < String > ( ) ; list1 . add ( " GeeksforGeeks " ) ; list1 . add ( " Udemy " ) ; list1 . add ( " Coursera " ) ; list1 . add ( " edX " ) ; Vector < String > list2 = new Vector < String > ( ) ; list2 . add ( " Codecademy " ) ; list2 . add ( " Khan ▁ Academy " ) ; list2 . add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean isFullTree ( Node node ) { if ( node == null ) return true ; if ( node . left == null && node . right == null ) return true ; if ( ( node . left != null ) && ( node . right != null ) ) return ( isFullTree ( node . left ) && isFullTree ( node . right ) ) ; return false ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 20 ) ; tree . root . right = new Node ( 30 ) ; tree . root . left . right = new Node ( 40 ) ; tree . root . left . left = new Node ( 50 ) ; tree . root . right . left = new Node ( 60 ) ; tree . root . left . left . left = new Node ( 80 ) ; tree . root . right . right = new Node ( 70 ) ; tree . root . left . left . right = new Node ( 90 ) ; tree . root . left . right . left = new Node ( 80 ) ; tree . root . left . right . right = new Node ( 90 ) ; tree . root . right . left . left = new Node ( 80 ) ; tree . root . right . left . right = new Node ( 90 ) ; tree . root . right . right . left = new Node ( 80 ) ; tree . root . right . right . right = new Node ( 90 ) ; if ( tree . isFullTree ( tree . root ) ) System . out . print ( " The ▁ binary ▁ tree ▁ is ▁ full " ) ; else System . out . print ( " The ▁ binary ▁ tree ▁ is ▁ not ▁ full " ) ; } }
public class find { public static void getPairsCount ( int [ ] arr , int sum ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) for ( int j = i + 1 ; j < arr . length ; j ++ ) if ( ( arr [ i ] + arr [ j ] ) == sum ) count ++ ; System . out . printf ( " Count ▁ of ▁ pairs ▁ is ▁ % d " , count ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int sum = 6 ; getPairsCount ( arr , sum ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static boolean isFullBinaryTree ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node . left == null && node . right == null ) continue ; if ( node . left == null node . right == null ) return false ; q . add ( node . left ) ; q . add ( node . right ) ; } return true ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; if ( isFullBinaryTree ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . HashMap ; class Test { static int arr [ ] = new int [ ] { 1 , 5 , 7 , - 1 , 5 } ; static int getPairsCount ( int n , int sum ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , 0 ) ; hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; } int twice_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . get ( sum - arr [ i ] ) != null ) twice_count += hm . get ( sum - arr [ i ] ) ; if ( sum - arr [ i ] == arr [ i ] ) twice_count -- ; } return twice_count / 2 ; } public static void main ( String [ ] args ) { int sum = 6 ; System . out . println ( " Count ▁ of ▁ pairs ▁ is ▁ " + getPairsCount ( arr . length , sum ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int [ ] arr1 , int [ ] arr2 , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
import java . io . * ; class GFG { static boolean isPresent ( int arr [ ] , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; HashSet < Integer > us = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) us . add ( arr1 [ i ] ) ; for ( int j = 0 ; j < n ; j ++ ) if ( us . contains ( x - arr2 [ j ] ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . print ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) { l ++ ; r -- ; count ++ ; } else if ( ( arr1 [ l ] + arr2 [ r ] ) < x ) l ++ ; else r -- ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
class GFG { static boolean isPresent ( int [ ] arr , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countQuadruples ( int [ ] arr1 , int [ ] arr2 , int [ ] arr3 , int [ ] arr4 , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) { int T = arr1 [ i ] + arr2 [ j ] + arr3 [ k ] ; if ( isPresent ( arr4 , 0 , n - 1 , x - T ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 4 , 5 , 6 } ; int [ ] arr2 = { 2 , 3 , 7 , 8 } ; int [ ] arr3 = { 1 , 4 , 6 , 10 } ; int [ ] arr4 = { 2 , 4 , 7 , 8 } ; int n = 4 ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int n , int value ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < n & r >= 0 ) { int sum = arr1 [ l ] + arr2 [ r ] ; if ( sum == value ) { l ++ ; r -- ; count ++ ; } else if ( sum > value ) r -- ; else l ++ ; } return count ; } static int countQuadruples ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] , int arr4 [ ] , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { int p_sum = arr1 [ i ] + arr2 [ j ] ; count += countPairs ( arr3 , arr4 , n , x - p_sum ) ; } return count ; } static public void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 7 , 8 } ; int arr3 [ ] = { 1 , 4 , 6 , 10 } ; int arr4 [ ] = { 2 , 4 , 7 , 8 } ; int n = arr1 . length ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
import java . util . * ; class GFG { static int countQuadruples ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] , int arr4 [ ] , int n , int x ) { int count = 0 ; Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( m . containsKey ( arr1 [ i ] + arr2 [ j ] ) ) m . put ( ( arr1 [ i ] + arr2 [ j ] ) , m . get ( ( arr1 [ i ] + arr2 [ j ] ) ) + 1 ) ; else m . put ( ( arr1 [ i ] + arr2 [ j ] ) , 1 ) ; for ( int k = 0 ; k < n ; k ++ ) for ( int l = 0 ; l < n ; l ++ ) { int p_sum = arr3 [ k ] + arr4 [ l ] ; if ( m . containsKey ( x - p_sum ) ) count += m . get ( x - p_sum ) ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 7 , 8 } ; int arr3 [ ] = { 1 , 4 , 6 , 10 } ; int arr4 [ ] = { 2 , 4 , 7 , 8 } ; int n = arr1 . length ; int x = 30 ; System . out . println ( " Count ▁ = ▁ " + countQuadruples ( arr1 , arr2 , arr3 , arr4 , n , x ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; break ; } } } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int result = 0 ; HashSet < Integer > Hash = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Hash . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . contains ( product ) ) { result ++ ; } } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 4 , 12 , 5 , 3 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void findPairs ( int arr1 [ ] , int arr2 [ ] , int n , int m , int x ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( arr1 [ i ] + arr2 [ j ] == x ) System . out . println ( arr1 [ i ] + " ▁ " + arr2 [ j ] ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 7 , 5 , 4 } ; int arr2 [ ] = { 0 , 7 , 4 , 3 , 2 , 1 } ; int x = 8 ; findPairs ( arr1 , arr2 , arr1 . length , arr2 . length , x ) ; } }
class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { left = null ; right = null ; this . key = key ; } } static class INT { static int d ; INT ( ) { d = 0 ; } } static boolean isBalancedUtil ( Node root , INT maxh , INT minh ) { if ( root == null ) { maxh . d = minh . d = 0 ; return true ; } INT lmxh = new INT ( ) , lmnh = new INT ( ) ; INT rmxh = new INT ( ) , rmnh = new INT ( ) ; if ( isBalancedUtil ( root . left , lmxh , lmnh ) == false ) return false ; if ( isBalancedUtil ( root . right , rmxh , rmnh ) == false ) return false ; maxh . d = Math . max ( lmxh . d , rmxh . d ) + 1 ; minh . d = Math . min ( lmnh . d , rmnh . d ) + 1 ; if ( maxh . d <= 2 * minh . d ) return true ; return false ; } static boolean isBalanced ( Node root ) { INT maxh = new INT ( ) , minh = new INT ( ) ; return isBalancedUtil ( root , maxh , minh ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 5 ) ; root . right = new Node ( 100 ) ; root . right . left = new Node ( 50 ) ; root . right . right = new Node ( 150 ) ; root . right . left . left = new Node ( 40 ) ; System . out . println ( isBalanced ( root ) ? " Balanced " : " Not ▁ Balanced " ) ; } }
class GFG { static void countFreq ( int a [ ] , int n ) { int hm [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; int cumul = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumul += hm [ a [ i ] ] ; if ( hm [ a [ i ] ] != 0 ) { System . out . println ( a [ i ] + " - > " + cumul ) ; } hm [ a [ i ] ] = 0 ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 4 , 2 , 1 } ; int n = a . length ; countFreq ( a , n ) ; } }
import java . io . * ; public class GFG { static void findPair ( int [ ] arr , int n ) { boolean found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { System . out . println ( arr [ i ] + " ▁ " + arr [ j ] ) ; found = true ; } } } } if ( found == false ) System . out . println ( " Not ▁ exist " ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 8 , 13 , 5 } ; int n = arr . length ; findPair ( arr , n ) ; } }
class Test { static boolean printPairs ( int arr [ ] , int n , int k ) { boolean isPairFound = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j && arr [ i ] % arr [ j ] == k ) { System . out . print ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " ▁ " ) ; isPairFound = true ; } } } return isPairFound ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 } ; int k = 3 ; if ( printPairs ( arr , arr . length , k ) == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } }
import java . util . HashMap ; import java . util . Vector ; class Test { static Vector < Integer > findDivisors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) v . add ( i ) ; else { v . add ( i ) ; v . add ( n / i ) ; } } } return v ; } static boolean printPairs ( int arr [ ] , int n , int k ) { HashMap < Integer , Boolean > occ = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( arr [ i ] , true ) ; boolean isPairFound = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( occ . get ( k ) && k < arr [ i ] ) { System . out . print ( " ( " + k + " , ▁ " + arr [ i ] + " ) ▁ " ) ; isPairFound = true ; } if ( arr [ i ] >= k ) { Vector < Integer > v = findDivisors ( arr [ i ] - k ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( arr [ i ] % v . get ( j ) == k && arr [ i ] != v . get ( j ) && occ . get ( v . get ( j ) ) ) { System . out . print ( " ( " + arr [ i ] + " , ▁ " + v . get ( j ) + " ) ▁ " ) ; isPairFound = true ; } } v . clear ( ) ; } } return isPairFound ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 1 , 2 , 5 , 4 } ; int k = 2 ; if ( printPairs ( arr , arr . length , k ) == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } }
import java . util . * ; class GFG { public static void convert ( int arr [ ] , int n ) { int temp [ ] = arr . clone ( ) ; Arrays . sort ( temp ) ; HashMap < Integer , Integer > umap = new HashMap < > ( ) ; int val = 0 ; for ( int i = 0 ; i < n ; i ++ ) umap . put ( temp [ i ] , val ++ ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = umap . get ( arr [ i ] ) ; } public static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int n = arr . length ; System . out . println ( " Given ▁ Array ▁ is ▁ " ) ; printArr ( arr , n ) ; convert ( arr , n ) ; System . out . println ( " Converted Array is "); printArr ( arr , n ) ; } }
public class GFG { static final int ASCII_SIZE = 256 ; static char getMaxOccuringChar ( String str ) { int count [ ] = new int [ ASCII_SIZE ] ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) count [ str . charAt ( i ) ] ++ ; int max = - 1 ; char result = ' ▁ ' ; for ( int i = 0 ; i < len ; i ++ ) { if ( max < count [ str . charAt ( i ) ] ) { max = count [ str . charAt ( i ) ] ; result = str . charAt ( i ) ; } } return result ; } public static void main ( String [ ] args ) { String str = " sample ▁ string " ; System . out . println ( " Max ▁ occurring ▁ character ▁ is ▁ " + getMaxOccuringChar ( str ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . HashMap ; import java . util . Map . Entry ; public class GFG { static final int MAX_CHAR = 26 ; static String getKey ( String str ) { boolean [ ] visited = new boolean [ MAX_CHAR ] ; Arrays . fill ( visited , false ) ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) visited [ str . charAt ( j ) - ' a ' ] = true ; String key = " " ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) if ( visited [ j ] ) key = key + ( char ) ( ' a ' + j ) ; return key ; } static void wordsWithSameCharSet ( String words [ ] , int n ) { HashMap < String , ArrayList < Integer > > Hash = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String key = getKey ( words [ i ] ) ; if ( Hash . containsKey ( key ) ) { ArrayList < Integer > get_al = Hash . get ( key ) ; get_al . add ( i ) ; Hash . put ( key , get_al ) ; } else { ArrayList < Integer > new_al = new ArrayList < > ( ) ; new_al . add ( i ) ; Hash . put ( key , new_al ) ; } } for ( Entry < String , ArrayList < Integer > > it : Hash . entrySet ( ) ) { ArrayList < Integer > get = it . getValue ( ) ; for ( Integer v : get ) System . out . print ( words [ v ] + " , ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { String words [ ] = { " may " , " student " , " students " , " dog " , " studentssess " , " god " , " cat " , " act " , " tab " , " bat " , " flow " , " wolf " , " lambs " , " amy " , " yam " , " balms " , " looped " , " poodle " } ; int n = words . length ; wordsWithSameCharSet ( words , n ) ; } }
import java . util . * ; class GFG { static String secMostRepeated ( Vector < String > seq ) { HashMap < String , Integer > occ = new HashMap < String , Integer > ( seq . size ( ) ) { @ Override public Integer get ( Object key ) { return containsKey ( key ) ? super . get ( key ) : 0 ; } } ; for ( int i = 0 ; i < seq . size ( ) ; i ++ ) occ . put ( seq . get ( i ) , occ . get ( seq . get ( i ) ) + 1 ) ; int first_max = Integer . MIN_VALUE , sec_max = Integer . MIN_VALUE ; Iterator < Map . Entry < String , Integer > > itr = occ . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { Map . Entry < String , Integer > entry = itr . next ( ) ; int v = entry . getValue ( ) ; if ( v > first_max ) { sec_max = first_max ; first_max = v ; } else if ( v > sec_max && v != first_max ) sec_max = v ; } itr = occ . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { Map . Entry < String , Integer > entry = itr . next ( ) ; int v = entry . getValue ( ) ; if ( v == sec_max ) return entry . getKey ( ) ; } return null ; } public static void main ( String [ ] args ) { String arr [ ] = { " ccc " , " aaa " , " ccc " , " ddd " , " aaa " , " aaa " } ; List < String > seq = Arrays . asList ( arr ) ; System . out . println ( secMostRepeated ( new Vector < > ( seq ) ) ) ; } }
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 ; } } ;
import java . util . Stack ; class Node { char value ; Node left , right ; Node ( char item ) { value = item ; left = right = null ; } } class ExpressionTree { boolean isOperator ( char c ) { if ( c == ' + ' c == ' - ' c == ' * ' c == ' / ' c == ' ^ ' ) { return true ; } return false ; } void inorder ( Node t ) { if ( t != null ) { inorder ( t . left ) ; System . out . print ( t . value + " ▁ " ) ; inorder ( t . right ) ; } } Node constructTree ( char postfix [ ] ) { Stack < Node > st = new Stack < Node > ( ) ; Node t , t1 , t2 ; for ( int i = 0 ; i < postfix . length ; i ++ ) { if ( ! isOperator ( postfix [ i ] ) ) { t = new Node ( postfix [ i ] ) ; st . push ( t ) ; } else { t = new Node ( postfix [ i ] ) ; t1 = st . pop ( ) ; t2 = st . pop ( ) ; t . right = t1 ; t . left = t2 ; st . push ( t ) ; } } t = st . peek ( ) ; st . pop ( ) ; return t ; } public static void main ( String args [ ] ) { ExpressionTree et = new ExpressionTree ( ) ; String postfix = " ab + ef * g * - " ; char [ ] charArray = postfix . toCharArray ( ) ; Node root = et . constructTree ( charArray ) ; System . out . println ( " infix ▁ expression ▁ is " ) ; et . inorder ( root ) ; } }
import java . util . * ; class GFG { public static int smallestKFreq ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( m . containsKey ( a [ i ] ) ) m . put ( a [ i ] , m . get ( a [ i ] ) + 1 ) ; else m . put ( a [ i ] , 1 ) ; int res = Integer . MAX_VALUE ; Set < Integer > s = m . keySet ( ) ; for ( int temp : s ) if ( m . get ( temp ) == k ) res = Math . min ( res , temp ) ; return ( res != Integer . MAX_VALUE ) ? res : - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 3 , 1 } ; int k = 2 ; System . out . println ( smallestKFreq ( arr , arr . length , k ) ) ; } }
import java . util . * ; class Main { static void printFirstRepeating ( int arr [ ] ) { int min = - 1 ; HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = arr . length - 1 ; i >= 0 ; i -- ) { if ( set . contains ( arr [ i ] ) ) min = i ; else set . add ( arr [ i ] ) ; } if ( min != - 1 ) System . out . println ( " The ▁ first ▁ repeating ▁ element ▁ is ▁ " + arr [ min ] ) ; else System . out . println ( " There ▁ are ▁ no ▁ repeating ▁ elements " ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; printFirstRepeating ( arr ) ; } }
public class GFG { static void printFirstRepeating ( int [ ] arr , int n ) { int k = 0 ; int max = n ; for ( int i = 0 ; i < n ; i ++ ) if ( max < arr [ i ] ) max = arr [ i ] ; int [ ] a = new int [ max + 1 ] ; int [ ] b = new int [ max + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ arr [ i ] ] != 0 ) { b [ arr [ i ] ] = 1 ; k = 1 ; continue ; } else a [ arr [ i ] ] = i ; } if ( k == 0 ) System . out . println ( " No ▁ repeating ▁ element ▁ found " ) ; else { int min = max + 1 ; for ( int i = 0 ; i < max + 1 ; i ++ ) if ( a [ i ] != 0 && min > a [ i ] && b [ i ] != 0 ) min = a [ i ] ; System . out . print ( arr [ min ] ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; int n = arr . length ; printFirstRepeating ( arr , n ) ; } }
import java . util . Arrays ; public class GFG { static int findSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = arr [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { sum = sum + arr [ i + 1 ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
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 ▁ " ) ; } }
class GFG { static int firstNonRepeating ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ; if ( j == n ) return arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; System . out . print ( firstNonRepeating ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int firstNonRepeating ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; System . out . println ( firstNonRepeating ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void firstNonRepeating ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) if ( x . getValue ( ) == 1 ) System . out . print ( x . getKey ( ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 9 , 6 , 7 , 4 } ; int n = arr . length ; firstNonRepeating ( arr , n ) ; } }
class GFG { static int printKDistinct ( int arr [ ] , int n , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( i != j && arr [ j ] == arr [ i ] ) break ; if ( j == n ) dist_count ++ ; if ( dist_count == k ) return arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 1 , 3 , 4 , 2 } ; int n = ar . length ; int k = 2 ; System . out . print ( printKDistinct ( ar , n , k ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static void printPairs ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; Collections . sort ( v ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( - v . get ( i ) + " ▁ " + v . get ( i ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
import java . util . HashMap ; public class Divisiblepair { static boolean canPairs ( int ar [ ] , int k ) { if ( ar . length % 2 == 1 ) return false ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < ar . length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( ! hm . containsKey ( rem ) ) { hm . put ( rem , 0 ) ; } hm . put ( rem , hm . get ( rem ) + 1 ) ; } for ( int i = 0 ; i < ar . length ; i ++ ) { int rem = ( ( ar [ i ] % k ) + k ) % k ; if ( 2 * rem == k ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else if ( rem == 0 ) { if ( hm . get ( rem ) % 2 == 1 ) return false ; } else { if ( hm . get ( k - rem ) != hm . get ( rem ) ) return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 92 , 75 , 65 , 48 , 45 , 35 } ; int k = 10 ; boolean ans = canPairs ( arr , k ) ; if ( ans ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) { int [ ] mp = new int [ 1000 ] ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr [ s ] % k ] -- ; s ++ ; } mp [ mod ] ++ ; e ++ ; if ( ( e - s ) > ( maxe - maxs ) ) { maxe = e ; maxs = s ; } } System . out . print ( " The ▁ maximum ▁ size ▁ is ▁ " + ( maxe - maxs + 1 ) + " and the subarray is as followsNEW_LINE"); for ( int i = maxs ; i <= maxe ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int k = 3 ; int [ ] arr = { 5 , 10 , 15 , 20 , 25 } ; int n = arr . length ; subarrayDivisibleByK ( arr , n , k ) ; } }
import java . util . * ; class GFG { static boolean findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n1 ; i ++ ) { s . add ( a1 [ i ] ) ; } ArrayList < Integer > al = new ArrayList < > ( s ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( al . contains ( sum - a2 [ i ] - a3 [ j ] ) & al . indexOf ( sum - a2 [ i ] - a3 [ j ] ) != al . get ( al . size ( ) - 1 ) ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { int a1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int a2 [ ] = { 2 , 3 , 6 , 1 , 2 } ; int a3 [ ] = { 3 , 2 , 4 , 5 , 6 } ; int sum = 9 ; int n1 = a1 . length ; int n2 = a2 . length ; int n3 = a3 . length ; if ( findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . io . * ; import java . util . * ; class ArrayElements { class pair { int first , second ; pair ( int f , int s ) { first = f ; second = s ; } } ; boolean findPairs ( int arr [ ] ) { HashMap < Integer , pair > map = new HashMap < Integer , pair > ( ) ; int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . containsKey ( sum ) ) map . put ( sum , new pair ( i , j ) ) ; else { pair p = map . get ( sum ) ; System . out . println ( " ( " + arr [ p . first ] + " , ▁ " + arr [ p . second ] + " ) ▁ and ▁ ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; return true ; } } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 4 , 7 , 1 , 2 , 9 , 8 } ; ArrayElements a = new ArrayElements ( ) ; a . findPairs ( arr ) ; } }
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node a , b ; boolean areMirror ( Node a , Node b ) { if ( a == null && b == null ) return true ; if ( a == null b == null ) return false ; return a . data == b . data && areMirror ( a . left , b . right ) && areMirror ( a . right , b . left ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node a = new Node ( 1 ) ; Node b = new Node ( 1 ) ; a . left = new Node ( 2 ) ; a . right = new Node ( 3 ) ; a . left . left = new Node ( 4 ) ; a . left . right = new Node ( 5 ) ; b . left = new Node ( 3 ) ; b . right = new Node ( 2 ) ; b . right . left = new Node ( 5 ) ; b . right . right = new Node ( 4 ) ; if ( tree . areMirror ( a , b ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . HashMap ; class MaxLenZeroSumSub { static int maxLen ( int arr [ ] ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; Integer prev_i = hM . get ( sum ) ; if ( prev_i != null ) max_len = Math . max ( max_len , i - prev_i ) ; else hM . put ( sum , i ) ; } return max_len ; } public static void main ( String arg [ ] ) { int arr [ ] = { 15 , - 2 , 2 , - 8 , 1 , 7 , 10 , 23 } ; System . out . println ( " Length ▁ of ▁ the ▁ longest ▁ 0 ▁ sum ▁ subarray ▁ is ▁ " + maxLen ( arr ) ) ; } }
import java . util . * ; class lics { static int LongIncrConseqSubseq ( int arr [ ] , int n ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( map . containsKey ( arr [ i ] - 1 ) ) { map . put ( arr [ i ] , map . get ( arr [ i ] - 1 ) + 1 ) ; map . remove ( arr [ i ] - 1 ) ; } else { map . put ( arr [ i ] , 1 ) ; } } return Collections . max ( map . values ( ) ) ; } public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = sc . nextInt ( ) ; System . out . println ( LongIncrConseqSubseq ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int longLenSub ( int [ ] arr , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . containsKey ( arr [ i ] - 1 ) && len < um . get ( arr [ i ] - 1 ) ) len = um . get ( arr [ i ] - 1 ) ; if ( um . containsKey ( arr [ i ] + 1 ) && len < um . get ( arr [ i ] + 1 ) ) len = um . get ( arr [ i ] + 1 ) ; um . put ( arr [ i ] , len + 1 ) ; if ( longLen < um . get ( arr [ i ] ) ) longLen = um . get ( arr [ i ] ) ; } return longLen ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( " Longest ▁ length ▁ subsequence ▁ = ▁ " + longLenSub ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findLongestConseqSubseq ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 , count = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; v . add ( 10 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) v . add ( arr [ i ] ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( i > 0 && v . get ( i ) == v . get ( i - 1 ) + 1 ) count ++ ; else count = 1 ; ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ " + " contiguous ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class ArrayElements { static int findLongestConseqSubseq ( int arr [ ] , int n ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) S . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! S . contains ( arr [ i ] - 1 ) ) { int j = arr [ i ] ; while ( S . contains ( j ) ) j ++ ; if ( ans < j - arr [ i ] ) ans = j - arr [ i ] ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
import java . io . * ; import java . util . PriorityQueue ; public class Longset_Sub { static int findLongestConseqSubseq ( int arr [ ] , int N ) { PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . add ( arr [ i ] ) ; } int prev = pq . poll ( ) ; int c = 1 ; int max = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( pq . peek ( ) - prev > 1 ) { c = 1 ; prev = pq . poll ( ) ; } else if ( pq . peek ( ) - prev == 0 ) { prev = pq . poll ( ) ; } else { c ++ ; prev = pq . poll ( ) ; } if ( max < c ) { max = c ; } } return max ; } public static void main ( String args [ ] ) throws IOException { int arr [ ] = { 1 , 9 , 3 , 10 , 4 , 20 , 2 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ Longest ▁ consecutive ▁ subsequence ▁ is ▁ " + findLongestConseqSubseq ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void findLIS ( int A [ ] , int n ) { Map < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; int LIS_size = 1 ; int LIS_index = 0 ; hash . put ( A [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { hash . put ( A [ i ] , hash . get ( A [ i ] - 1 ) == null ? 1 : hash . get ( A [ i ] - 1 ) + 1 ) ; if ( LIS_size < hash . get ( A [ i ] ) ) { LIS_size = hash . get ( A [ i ] ) ; LIS_index = A [ i ] ; } } System . out . println ( " LIS _ size ▁ = ▁ " + LIS_size ) ; System . out . print ( " LIS ▁ : ▁ " ) ; int start = LIS_index - LIS_size + 1 ; while ( start <= LIS_index ) { System . out . print ( start + " ▁ " ) ; start ++ ; } } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 3 , 7 , 4 , 8 , 5 , 13 , 6 } ; int n = A . length ; findLIS ( A , n ) ; } }
import java . util . * ; class GFG { static int countSubsets ( int arr [ ] , int n ) { HashSet < Integer > us = new HashSet < > ( ) ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . add ( arr [ i ] ) ; even_count = us . size ( ) ; return ( int ) ( Math . pow ( 2 , even_count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = arr . length ; System . out . println ( " Number ▁ of ▁ subsets ▁ = ▁ " + countSubsets ( arr , n ) ) ; } }
import java . util . Arrays ; class Test { static int countWindowDistinct ( int win [ ] , int k ) { int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( win [ i ] == win [ j ] ) break ; if ( j == i ) dist_count ++ ; } return dist_count ; } static void countDistinct ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) System . out . println ( countWindowDistinct ( Arrays . copyOfRange ( arr , i , arr . length ) , k ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 3 } , k = 4 ; countDistinct ( arr , arr . length , k ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static String areMirrors ( Node root1 , Node root2 ) { Stack < Node > st1 = new Stack < Node > ( ) ; Stack < Node > st2 = new Stack < Node > ( ) ; while ( true ) { while ( root1 != null && root2 != null ) { if ( root1 . data != root2 . data ) return " No " ; st1 . push ( root1 ) ; st2 . push ( root2 ) ; root1 = root1 . left ; root2 = root2 . right ; } if ( ! ( root1 == null && root2 == null ) ) return " No " ; if ( ! st1 . isEmpty ( ) && ! st2 . isEmpty ( ) ) { root1 = st1 . peek ( ) ; root2 = st2 . peek ( ) ; st1 . pop ( ) ; st2 . pop ( ) ; root1 = root1 . right ; root2 = root2 . left ; } else break ; } return " Yes " ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 3 ) ; root1 . right = newNode ( 2 ) ; root1 . right . left = newNode ( 5 ) ; root1 . right . right = newNode ( 4 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; System . out . println ( areMirrors ( root1 , root2 ) ) ; } }
import java . util . HashSet ; import java . util . Set ; public class MaxPossibleSuminWindow { static int returnMaxSum ( int A [ ] , int B [ ] , int n ) { Set < Integer > mp = new HashSet < Integer > ( ) ; int result = 0 ; int curr_sum = 0 , curr_begin = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( mp . contains ( A [ i ] ) ) { mp . remove ( A [ curr_begin ] ) ; curr_sum -= B [ curr_begin ] ; curr_begin ++ ; } mp . add ( A [ i ] ) ; curr_sum += B [ i ] ; result = Integer . max ( result , curr_sum ) ; } return result ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 1 , 2 , 3 , 0 , 1 , 4 } ; int B [ ] = { 9 , 8 , 1 , 2 , 3 , 4 , 5 } ; int n = A . length ; System . out . println ( returnMaxSum ( A , B , n ) ) ; } }
import java . util . * ; class MyDS { public MyDS ( ) { arr = new ArrayList < Integer > ( ) ; hash = new HashMap < Integer , Integer > ( ) ; } ArrayList < Integer > arr ; HashMap < Integer , Integer > hash ; void add ( int x ) { if ( hash . get ( x ) != null ) return ; int s = arr . size ( ) ; arr . add ( x ) ; hash . put ( x , s ) ; } void remove ( int x ) { Integer index = hash . get ( x ) ; if ( index == null ) return ; hash . remove ( x ) ; int size = arr . size ( ) ; Integer last = arr . get ( size - 1 ) ; Collections . swap ( arr , index , size - 1 ) ; arr . remove ( size - 1 ) ; hash . put ( last , index ) ; } int getRandom ( ) { Random rand = new Random ( ) ; int index = rand . nextInt ( arr . size ( ) ) ; return arr . get ( index ) ; } Integer search ( int x ) { return hash . get ( x ) ; } } class Main { public static void main ( String [ ] args ) { MyDS ds = new MyDS ( ) ; ds . add ( 10 ) ; ds . add ( 20 ) ; ds . add ( 30 ) ; ds . add ( 40 ) ; System . out . println ( ds . search ( 30 ) ) ; ds . remove ( 20 ) ; ds . add ( 50 ) ; System . out . println ( ds . search ( 50 ) ) ; System . out . println ( ds . getRandom ( ) ) ; } }
import java . util . * ; class GFG { static boolean areElementsContiguous ( int arr [ ] , int n ) { int max = Integer . MIN_VALUE ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max = Math . max ( max , arr [ i ] ) ; min = Math . min ( min , arr [ i ] ) ; } int m = max - min + 1 ; if ( m > n ) return false ; boolean visited [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) visited [ arr [ i ] - min ] = true ; for ( int i = 0 ; i < m ; i ++ ) if ( visited [ i ] == false ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . length ; if ( areElementsContiguous ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static Boolean areElementsContiguous ( int arr [ ] , int n ) { HashSet < Integer > us = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) us . add ( arr [ i ] ) ; int count = 1 ; int curr_ele = arr [ 0 ] - 1 ; while ( us . contains ( curr_ele ) == true ) { count ++ ; curr_ele -- ; } curr_ele = arr [ 0 ] + 1 ; while ( us . contains ( curr_ele ) == true ) { count ++ ; curr_ele ++ ; } return ( count == ( us . size ( ) ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . length ; if ( areElementsContiguous ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { public static void subArraySum ( int [ ] arr , int n , int sum ) { int cur_sum = 0 ; int start = 0 ; int end = - 1 ; HashMap < Integer , Integer > hashMap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { cur_sum = cur_sum + arr [ i ] ; if ( cur_sum - sum == 0 ) { start = 0 ; end = i ; break ; } if ( hashMap . containsKey ( cur_sum - sum ) ) { start = hashMap . get ( cur_sum - sum ) + 1 ; end = i ; break ; } hashMap . put ( cur_sum , i ) ; } if ( end == - 1 ) { System . out . println ( " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " ) ; } else { System . out . println ( " Sum ▁ found ▁ between ▁ indexes ▁ " + start + " ▁ to ▁ " + end ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 2 , - 2 , - 20 , 10 } ; int n = arr . length ; int sum = - 10 ; subArraySum ( arr , n , sum ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root1 , root2 ; boolean identicalTrees ( Node a , Node b ) { if ( a == null && b == null ) return true ; if ( a != null && b != null ) return ( a . data == b . data && identicalTrees ( a . left , b . left ) && identicalTrees ( a . right , b . right ) ) ; return false ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root1 = new Node ( 1 ) ; tree . root1 . left = new Node ( 2 ) ; tree . root1 . right = new Node ( 3 ) ; tree . root1 . left . left = new Node ( 4 ) ; tree . root1 . left . right = new Node ( 5 ) ; tree . root2 = new Node ( 1 ) ; tree . root2 . left = new Node ( 2 ) ; tree . root2 . right = new Node ( 3 ) ; tree . root2 . left . left = new Node ( 4 ) ; tree . root2 . left . right = new Node ( 5 ) ; if ( tree . identicalTrees ( tree . root1 , tree . root2 ) ) System . out . println ( " Both ▁ trees ▁ are ▁ identical " ) ; else System . out . println ( " Trees ▁ are ▁ not ▁ identical " ) ; } }
public class Palindrome { static int minInsertion ( String str ) { int n = str . length ( ) ; int res = 0 ; int [ ] count = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( count [ i ] % 2 == 1 ) res ++ ; } return ( res == 0 ) ? 0 : res - 1 ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( minInsertion ( str ) ) ; } }
import java . util . * ; class GFG { static int maxdiff ( int arr [ ] , int n ) { Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) freq . put ( arr [ i ] , freq . get ( arr [ i ] ) == null ? 1 : freq . get ( arr [ i ] ) + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( freq . get ( arr [ i ] ) > freq . get ( arr [ j ] ) && arr [ i ] > arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ i ] ) - freq . get ( arr [ j ] ) ) ; else if ( freq . get ( arr [ i ] ) < freq . get ( arr [ j ] ) && arr [ i ] < arr [ j ] ) ans = Math . max ( ans , freq . get ( arr [ j ] ) - freq . get ( arr [ i ] ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = arr . length ; System . out . println ( maxdiff ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int maxdiff ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; int [ ] dist = new int [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dist [ j ++ ] = arr [ i ] ; if ( ! freq . containsKey ( arr [ i ] ) ) freq . put ( arr [ i ] , 1 ) ; else freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } Arrays . sort ( dist ) ; int min_freq = n + 1 ; int ans = 0 ; for ( int i = 0 ; i < j ; i ++ ) { int cur_freq = freq . get ( dist [ i ] ) ; ans = Math . max ( ans , cur_freq - min_freq ) ; min_freq = Math . min ( min_freq , cur_freq ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 3 , 2 , 3 , 2 } ; int n = arr . length ; System . out . print ( maxdiff ( arr , n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int findDiff ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = Math . max ( max_count , count ) ; min_count = Math . min ( min_count , count ) ; count = 0 ; } } return ( max_count - min_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = arr . length ; System . out . println ( findDiff ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findDiff ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int max_count = 0 , min_count = n ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { max_count = Math . max ( max_count , x . getValue ( ) ) ; min_count = Math . min ( min_count , x . getValue ( ) ) ; } return ( max_count - min_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 4 , 5 , 4 , 1 , 1 , 7 , 7 , 2 , 5 } ; int n = arr . length ; System . out . println ( findDiff ( arr , n ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static boolean areIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return true ; if ( root1 == null root2 == null ) return false ; Queue < Node > q1 = new LinkedList < Node > ( ) ; Queue < Node > q2 = new LinkedList < Node > ( ) ; q1 . add ( root1 ) ; q2 . add ( root2 ) ; while ( ! q1 . isEmpty ( ) && ! q2 . isEmpty ( ) ) { Node n1 = q1 . peek ( ) ; Node n2 = q2 . peek ( ) ; if ( n1 . data != n2 . data ) return false ; q1 . remove ( ) ; q2 . remove ( ) ; if ( n1 . left != null && n2 . left != null ) { q1 . add ( n1 . left ) ; q2 . add ( n2 . left ) ; } else if ( n1 . left != null n2 . left != null ) return false ; if ( n1 . right != null && n2 . right != null ) { q1 . add ( n1 . right ) ; q2 . add ( n2 . right ) ; } else if ( n1 . right != null n2 . right != null ) return false ; } return true ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; Node root2 = newNode ( 1 ) ; root2 . left = newNode ( 2 ) ; root2 . right = newNode ( 3 ) ; root2 . left . left = newNode ( 4 ) ; root2 . left . right = newNode ( 5 ) ; if ( areIdentical ( root1 , root2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int result = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ; else i ++ ; } if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
import java . util . * ; class GFG { public static int maxDiff ( int arr [ ] , int n ) { HashMap < Integer , Integer > hashPositive = new HashMap < > ( ) ; HashMap < Integer , Integer > hashNegative = new HashMap < > ( ) ; int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 ) { if ( hashPositive . containsKey ( arr [ i ] ) ) { hashPositive . replace ( arr [ i ] , hashPositive . get ( arr [ i ] ) + 1 ) ; } else { hashPositive . put ( arr [ i ] , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] > 0 && hashPositive . containsKey ( arr [ i ] ) ) { if ( hashPositive . get ( arr [ i ] ) == 1 ) { SubsetSum_1 += arr [ i ] ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 ) { if ( hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { hashNegative . replace ( Math . abs ( arr [ i ] ) , hashNegative . get ( Math . abs ( arr [ i ] ) ) + 1 ) ; } else { hashNegative . put ( Math . abs ( arr [ i ] ) , 1 ) ; } } } for ( int i = 0 ; i <= n - 1 ; i ++ ) { if ( arr [ i ] < 0 && hashNegative . containsKey ( Math . abs ( arr [ i ] ) ) ) { if ( hashNegative . get ( Math . abs ( arr [ i ] ) ) == 1 ) { SubsetSum_2 += arr [ i ] ; } } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . print ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }
import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; int j = - 1 ; HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n ) { j ++ ; if ( j < n && hm . size ( ) < k ) hm . put ( arr [ j ] , hm . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( hm . size ( ) == k && ( ( r - l ) >= ( j - i ) ) ) { l = i ; r = j ; break ; } } if ( hm . size ( ) < k ) break ; while ( hm . size ( ) == k ) { if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; i ++ ; if ( hm . size ( ) == k && ( r - l ) >= ( j - i ) ) { l = i ; r = j ; } } if ( hm . getOrDefault ( arr [ i ] , 0 ) == 1 ) hm . remove ( arr [ i ] ) ; else hm . put ( arr [ i ] , hm . getOrDefault ( arr [ i ] , 0 ) - 1 ) ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }
import java . util . * ; public class GfG { public static int sum ( int a [ ] , int n ) { Map < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; int ans = 0 , pre_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( i * a [ i ] ) - pre_sum ; pre_sum += a [ i ] ; if ( cnt . containsKey ( a [ i ] - 1 ) ) ans -= cnt . get ( a [ i ] - 1 ) ; if ( cnt . containsKey ( a [ i ] + 1 ) ) ans += cnt . get ( a [ i ] + 1 ) ; if ( cnt . containsKey ( a [ i ] ) ) { cnt . put ( a [ i ] , cnt . get ( a [ i ] ) + 1 ) ; } else { cnt . put ( a [ i ] , 1 ) ; } } return ans ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 1 , 3 } ; int n = a . length ; System . out . println ( sum ( a , n ) ) ; } }
import java . util . * ; class GFG { static int calculate ( int a [ ] , int n ) { Arrays . sort ( a ) ; int count = 1 ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { count += 1 ; } else { answer = answer + ( count * ( count - 1 ) ) / 2 ; count = 1 ; } } answer = answer + ( count * ( count - 1 ) ) / 2 ; return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 4 } ; int n = a . length ; System . out . println ( calculate ( a , n ) ) ; } }
import java . util . * ; class GFG { static int calculate ( int a [ ] , int n ) { int maximum = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int frequency [ ] = new int [ maximum + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } int answer = 0 ; for ( int i = 0 ; i < ( maximum ) + 1 ; i ++ ) { answer = answer + frequency [ i ] * ( frequency [ i ] - 1 ) ; } return answer / 2 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 4 } ; int n = a . length ; System . out . println ( calculate ( a , n ) ) ; } }
import java . util . * ; class GFG { static int countSubarrWithEqualZeroAndOne ( int arr [ ] , int n ) { Map < Integer , Integer > um = new HashMap < > ( ) ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += ( arr [ i ] == 0 ) ? - 1 : arr [ i ] ; um . put ( curr_sum , um . get ( curr_sum ) == null ? 1 : um . get ( curr_sum ) + 1 ) ; } int count = 0 ; for ( Map . Entry < Integer , Integer > itr : um . entrySet ( ) ) { if ( itr . getValue ( ) > 1 ) count += ( ( itr . getValue ( ) * ( itr . getValue ( ) - 1 ) ) / 2 ) ; } if ( um . containsKey ( 0 ) ) count += um . get ( 0 ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ = ▁ " + countSubarrWithEqualZeroAndOne ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int lenOfLongSubarr ( int arr [ ] , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Length ▁ = ▁ " + lenOfLongSubarr ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printAllAPTriplets ( int [ ] arr , int n ) { ArrayList < Integer > s = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; boolean exists = s . contains ( arr [ i ] - diff ) ; if ( exists ) System . out . println ( arr [ i ] - diff + " ▁ " + arr [ i ] + " ▁ " + arr [ j ] ) ; } s . add ( arr [ i ] ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . length ; printAllAPTriplets ( arr , n ) ; } }
import java . io . * ; class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } class GFG { static boolean existPathUtil ( Node root , int arr [ ] , int n , int index ) { if ( root == null index == n ) return false ; if ( root . left == null && root . right == null ) { if ( ( root . data == arr [ index ] ) && ( index == n - 1 ) ) return true ; return false ; } return ( ( index < n ) && ( root . data == arr [ index ] ) && ( existPathUtil ( root . left , arr , n , index + 1 ) || existPathUtil ( root . right , arr , n , index + 1 ) ) ) ; } static boolean existPath ( Node root , int arr [ ] , int n , int index ) { if ( root == null ) return ( n == 0 ) ; return existPathUtil ( root , arr , n , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 , 6 , 7 } ; int n = arr . length ; Node root = new Node ( 5 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 8 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 4 ) ; root . left . left . left = new Node ( 1 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; if ( existPath ( root , arr , n , 0 ) ) System . out . println ( " Path ▁ Exists " ) ; else System . out . println ( " Path ▁ does ▁ not ▁ Exist " ) ; } }
import java . util . HashMap ; class GFG { static int countTriplets ( int arr [ ] , int n , int m ) { HashMap < Integer , Integer > occ = new HashMap < Integer , Integer > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( arr [ i ] , i ) ; int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ i ] * arr [ j ] <= m ) && ( arr [ i ] * arr [ j ] != 0 ) && ( m % ( arr [ i ] * arr [ j ] ) == 0 ) ) { int check = m / ( arr [ i ] * arr [ j ] ) ; occ . containsKey ( check ) ; if ( check != arr [ i ] && check != arr [ j ] && occ . containsKey ( check ) && occ . get ( check ) > i && occ . get ( check ) > j ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 2 , 3 , 8 } ; int m = 24 ; System . out . println ( countTriplets ( arr , arr . length , m ) ) ; } }
import java . util . * ; class GFG { public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; for ( Map . Entry < Integer , Integer > it : hm . entrySet ( ) ) { int count = it . getValue ( ) ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 1 } ; System . out . println ( countPairs ( arr , arr . length ) ) ; } }
public class GFG { static int p = 101 ; static int MOD = 1000000007 ; static class Query { int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static boolean isPalindrome ( String str , int L , int R ) { while ( R > L ) { if ( str . charAt ( L ++ ) != str . charAt ( R -- ) ) { return ( false ) ; } } return ( true ) ; } static int modPow ( int base , int exponent ) { if ( exponent == 0 ) { return 1 ; } if ( exponent == 1 ) { return base ; } int temp = modPow ( base , exponent / 2 ) ; if ( exponent % 2 == 0 ) { return ( temp % MOD * temp % MOD ) % MOD ; } else { return ( ( ( temp % MOD * temp % MOD ) % MOD ) * base % MOD ) % MOD ; } } static int findMMI ( int n ) { return modPow ( n , MOD - 2 ) ; } static void computePrefixHash ( String str , int n , int prefix [ ] , int power [ ] ) { prefix [ 0 ] = 0 ; prefix [ 1 ] = str . charAt ( 0 ) ; for ( int i = 2 ; i <= n ; i ++ ) { prefix [ i ] = ( prefix [ i - 1 ] % MOD + ( str . charAt ( i - 1 ) % MOD * power [ i - 1 ] % MOD ) % MOD ) % MOD ; } return ; } static void computeSuffixHash ( String str , int n , int suffix [ ] , int power [ ] ) { suffix [ 0 ] = 0 ; suffix [ 1 ] = str . charAt ( n - 1 ) ; for ( int i = n - 2 , j = 2 ; i >= 0 && j <= n ; i -- , j ++ ) { suffix [ j ] = ( suffix [ j - 1 ] % MOD + ( str . charAt ( i ) % MOD * power [ j - 1 ] % MOD ) % MOD ) % MOD ; } return ; } static void queryResults ( String str , Query q [ ] , int m , int n , int prefix [ ] , int suffix [ ] , int power [ ] ) { for ( int i = 0 ; i <= m - 1 ; i ++ ) { int L = q [ i ] . L ; int R = q [ i ] . R ; long hash_LR = ( ( prefix [ R + 1 ] - prefix [ L ] + MOD ) % MOD * findMMI ( power [ L ] ) % MOD ) % MOD ; long reverse_hash_LR = ( ( suffix [ n - L ] - suffix [ n - R - 1 ] + MOD ) % MOD * findMMI ( power [ n - R - 1 ] ) % MOD ) % MOD ; if ( hash_LR == reverse_hash_LR ) { if ( isPalindrome ( str , L , R ) == true ) { System . out . printf ( " The ▁ Substring ▁ [ % d ▁ % d ] ▁ is ▁ a ▁ " + "palindromeNEW_LINE", L, R); } else { System . out . printf ( " The ▁ Substring ▁ [ % d ▁ % d ] ▁ is ▁ not ▁ a ▁ " + "palindromeNEW_LINE", L, R); } } else { System . out . printf ( " The ▁ Substring ▁ [ % d ▁ % d ] ▁ is ▁ not ▁ a ▁ " + "palindromeNEW_LINE", L, R); } } return ; } static void computePowers ( int power [ ] , int n ) { power [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { power [ i ] = ( power [ i - 1 ] % MOD * p % MOD ) % MOD ; } return ; } public static void main ( String [ ] args ) { String str = " abaaabaaaba " ; int n = str . length ( ) ; int [ ] power = new int [ n + 1 ] ; computePowers ( power , n ) ; int [ ] prefix = new int [ n + 1 ] ; int [ ] suffix = new int [ n + 1 ] ; computePrefixHash ( str , n , prefix , power ) ; computeSuffixHash ( str , n , suffix , power ) ; Query q [ ] = { new Query ( 0 , 10 ) , new Query ( 5 , 8 ) , new Query ( 2 , 5 ) , new Query ( 5 , 9 ) } ; int m = q . length ; queryResults ( str , q , m , n , prefix , suffix , power ) ; } }
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int getLevel ( Node root , Node node , int level ) { if ( root == null ) return 0 ; if ( root == node ) return level ; int downlevel = getLevel ( root . left , node , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; return getLevel ( root . right , node , level + 1 ) ; } static void printGivenLevel ( Node root , Node node , int level ) { if ( root == null level < 2 ) return ; if ( level == 2 ) { if ( root . left == node root . right == node ) return ; if ( root . left != null ) System . out . print ( root . left . data + " ▁ " ) ; if ( root . right != null ) System . out . print ( root . right . data + " ▁ " ) ; } else if ( level > 2 ) { printGivenLevel ( root . left , node , level - 1 ) ; printGivenLevel ( root . right , node , level - 1 ) ; } } static void printCousins ( Node root , Node node ) { int level = getLevel ( root , node , 1 ) ; printGivenLevel ( root , node , level ) ; } 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 . left . right . right = newNode ( 15 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; printCousins ( root , root . left . right ) ; } }
class GFG { static final int N = 5 ; static int ptr [ ] = new int [ 501 ] ; static void findSmallestRange ( int arr [ ] [ ] , int n , int k ) { int i , minval , maxval , minrange , minel = 0 , maxel = 0 , flag , minind ; for ( i = 0 ; i <= k ; i ++ ) { ptr [ i ] = 0 ; } minrange = Integer . MAX_VALUE ; while ( true ) { minind = - 1 ; minval = Integer . MAX_VALUE ; maxval = Integer . MIN_VALUE ; flag = 0 ; for ( i = 0 ; i < k ; i ++ ) { if ( ptr [ i ] == n ) { flag = 1 ; break ; } if ( ptr [ i ] < n && arr [ i ] [ ptr [ i ] ] < minval ) { minind = i ; minval = arr [ i ] [ ptr [ i ] ] ; } if ( ptr [ i ] < n && arr [ i ] [ ptr [ i ] ] > maxval ) { maxval = arr [ i ] [ ptr [ i ] ] ; } } if ( flag == 1 ) { break ; } ptr [ minind ] ++ ; if ( ( maxval - minval ) < minrange ) { minel = minval ; maxel = maxval ; minrange = maxel - minel ; } } System . out . printf ( "The smallest range is [%d, %d]NEW_LINE", minel, maxel); } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 4 , 7 , 9 , 12 , 15 } , { 0 , 8 , 10 , 14 , 20 } , { 6 , 12 , 16 , 30 , 50 } } ; int k = arr . length ; findSmallestRange ( arr , N , k ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int countNum ( int [ ] arr , int n ) { int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i + 1 ] - 1 ) count += arr [ i + 1 ] - arr [ i ] - 1 ; return count ; } static public void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 8 , 6 } ; int n = arr . length ; System . out . println ( countNum ( arr , n ) ) ; } }
import java . util . HashSet ; class GFG { static int countNum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; int count = 0 , maxm = Integer . MIN_VALUE , minm = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( arr [ i ] ) ; if ( arr [ i ] < minm ) minm = arr [ i ] ; if ( arr [ i ] > maxm ) maxm = arr [ i ] ; } for ( int i = minm ; i <= maxm ; i ++ ) if ( ! s . contains ( i ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 8 , 6 } ; int n = arr . length ; System . out . println ( countNum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAXN = 11 ; static int ver = 2 ; static int [ ] [ ] hashtable = new int [ ver ] [ MAXN ] ; static int [ ] pos = new int [ ver ] ; static void initTable ( ) { for ( int j = 0 ; j < MAXN ; j ++ ) for ( int i = 0 ; i < ver ; i ++ ) hashtable [ i ] [ j ] = Integer . MIN_VALUE ; } static int hash ( int function , int key ) { switch ( function ) { case 1 : return key % MAXN ; case 2 : return ( key / MAXN ) % MAXN ; } return Integer . MIN_VALUE ; } static void place ( int key , int tableID , int cnt , int n ) { if ( cnt == n ) { System . out . printf ( "%d unpositionedNEW_LINE", key); System . out . printf ( "Cycle present. REHASH.NEW_LINE"); return ; } for ( int i = 0 ; i < ver ; i ++ ) { pos [ i ] = hash ( i + 1 , key ) ; if ( hashtable [ i ] [ pos [ i ] ] == key ) return ; } if ( hashtable [ tableID ] [ pos [ tableID ] ] != Integer . MIN_VALUE ) { int dis = hashtable [ tableID ] [ pos [ tableID ] ] ; hashtable [ tableID ] [ pos [ tableID ] ] = key ; place ( dis , ( tableID + 1 ) % ver , cnt + 1 , n ) ; } else hashtable [ tableID ] [ pos [ tableID ] ] = key ; } static void printTable ( ) { System . out . printf ( "Final hash tables:NEW_LINE"); for ( int i = 0 ; i < ver ; i ++ , System . out . printf ( "NEW_LINE")) for ( int j = 0 ; j < MAXN ; j ++ ) if ( hashtable [ i ] [ j ] == Integer . MIN_VALUE ) System . out . printf ( " - ▁ " ) ; else System . out . printf ( " % d ▁ " , hashtable [ i ] [ j ] ) ; System . out . printf ( "NEW_LINE"); } static void cuckoo ( int keys [ ] , int n ) { initTable ( ) ; for ( int i = 0 , cnt = 0 ; i < n ; i ++ , cnt = 0 ) place ( keys [ i ] , 0 , cnt , n ) ; printTable ( ) ; } public static void main ( String [ ] args ) { int keys_1 [ ] = { 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 } ; int n = keys_1 . length ; cuckoo ( keys_1 , n ) ; int keys_2 [ ] = { 20 , 50 , 53 , 75 , 100 , 67 , 105 , 3 , 36 , 39 , 6 } ; int m = keys_2 . length ; cuckoo ( keys_2 , m ) ; } }
import java . util . HashMap ; class Test { static int countDistictSubarray ( int arr [ ] , int n ) { HashMap < Integer , Integer > vis = new HashMap < Integer , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( ! containsKey ( key ) ) return 0 ; return super . get ( key ) ; } } ; for ( int i = 0 ; i < n ; ++ i ) vis . put ( arr [ i ] , 1 ) ; int k = vis . size ( ) ; vis . clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { vis . put ( arr [ right ] , vis . get ( arr [ right ] ) + 1 ) ; if ( vis . get ( arr [ right ] ) == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; vis . put ( arr [ left ] , vis . get ( arr [ left ] ) - 1 ) ; if ( vis . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 2 , 3 } ; System . out . println ( countDistictSubarray ( arr , arr . length ) ) ; } }
class GFG { static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; } else { difference -- ; } if ( difference < 0 ) { ans += hash_negative [ - difference ] ; hash_negative [ - difference ] ++ ; } else { ans += hash_positive [ difference ] ; hash_positive [ difference ] ++ ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 3 , 4 , 6 , 8 , 1 , 10 , 5 , 7 } ; int n = arr . length ; System . out . println ( " Total ▁ Number ▁ of ▁ Even - Odd " + " ▁ subarrays ▁ are ▁ " + countSubarrays ( arr , n ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printPaths ( Node node ) { int path [ ] = new int [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } void printPathsRecur ( Node node , int path [ ] , int pathLen ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) printArray ( path , pathLen ) ; else { printPathsRecur ( node . left , path , pathLen ) ; printPathsRecur ( node . right , path , pathLen ) ; } } void printArray ( int ints [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) System . out . print ( ints [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . printPaths ( tree . root ) ; } }
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; public class DistinctIds { static int distinctIds ( int arr [ ] , int n , int mi ) { Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int count = 0 ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) == false ) { m . put ( arr [ i ] , 1 ) ; size ++ ; } else m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } for ( Entry < Integer , Integer > mp : m . entrySet ( ) ) { if ( mp . getKey ( ) <= mi ) { mi -= mp . getKey ( ) ; count ++ ; } else return size - count ; } return size - count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 , 3 , 3 } ; int m = 3 ; System . out . println ( distinctIds ( arr , arr . length , m ) ) ; } }
import java . util . * ; class GFG { static int findLongestConseqSubseq ( int arr [ ] , int n ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) S . add ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . contains ( arr [ i ] ) ) { int j = arr [ i ] ; while ( S . contains ( j ) ) j ++ ; ans = Math . max ( ans , j - arr [ i ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 94 , 93 , 1000 , 5 , 92 , 78 } ; int n = arr . length ; System . out . println ( findLongestConseqSubseq ( arr , n ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int findLargestd ( int [ ] S , int n ) { boolean found = false ; Arrays . sort ( S ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; for ( int l = k + 1 ; l < n ; l ++ ) { if ( i == l ) continue ; if ( S [ i ] == S [ j ] + S [ k ] + S [ l ] ) { found = true ; return S [ i ] ; } } } } } if ( found == false ) return Integer . MAX_VALUE ; return - 1 ; } public static void main ( String [ ] args ) { int [ ] S = new int [ ] { 2 , 3 , 5 , 7 , 12 } ; int n = S . length ; int ans = findLargestd ( S , n ) ; if ( ans == Integer . MAX_VALUE ) System . out . println ( " No ▁ Solution " ) ; else System . out . println ( " Largest ▁ d ▁ such ▁ that ▁ " + " a ▁ + ▁ " + " b ▁ + ▁ c ▁ = ▁ d ▁ is ▁ " + ans ) ; } }
import java . util . HashMap ; import java . lang . Math ; class Indexes { int i , j ; Indexes ( int i , int j ) { this . i = i ; this . j = j ; } int getI ( ) { return i ; } int getJ ( ) { return j ; } } class GFG { static int findFourElements ( int [ ] arr , int n ) { HashMap < Integer , Indexes > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { map . put ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ; } } int d = Integer . MIN_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int abs_diff = Math . abs ( arr [ i ] - arr [ j ] ) ; if ( map . containsKey ( abs_diff ) ) { Indexes indexes = map . get ( abs_diff ) ; if ( indexes . getI ( ) != i && indexes . getI ( ) != j && indexes . getJ ( ) != i && indexes . getJ ( ) != j ) { d = Math . max ( d , Math . max ( arr [ i ] , arr [ j ] ) ) ; } } } } return d ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 7 , 12 } ; int n = arr . length ; int res = findFourElements ( arr , n ) ; if ( res == Integer . MIN_VALUE ) System . out . println ( " No ▁ Solution " ) ; else System . out . println ( res ) ; } }
import java . util . * ; class GFG { static int findSubarraySum ( int arr [ ] , int n ) { int i , j ; int cArray [ ] = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) cArray [ i + 1 ] = cArray [ i ] + arr [ i ] ; Vector < Integer > subArrSum = new Vector < Integer > ( ) ; for ( i = 1 ; i <= n ; i ++ ) for ( j = i ; j <= n ; j ++ ) subArrSum . add ( cArray [ j ] - cArray [ i - 1 ] ) ; Collections . sort ( subArrSum ) ; int totalSum = 0 ; for ( i = 0 ; i < subArrSum . size ( ) - 1 ; i ++ ) { if ( subArrSum . get ( i ) == subArrSum . get ( i + 1 ) ) { j = i + 1 ; while ( subArrSum . get ( j ) == subArrSum . get ( i ) && j < subArrSum . size ( ) ) { subArrSum . set ( j , 0 ) ; j ++ ; } subArrSum . set ( i , 0 ) ; } } for ( i = 0 ; i < subArrSum . size ( ) ; i ++ ) totalSum += subArrSum . get ( i ) ; return totalSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . length ; System . out . print ( findSubarraySum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findSubarraySum ( int [ ] arr , int n ) { int res = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; if ( m . containsKey ( sum ) ) { m . put ( sum , m . get ( sum ) + 1 ) ; } else { m . put ( sum , 1 ) ; } } } for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) if ( x . getValue ( ) == 1 ) res += x . getKey ( ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 3 , 1 , 4 } ; int n = arr . length ; System . out . println ( findSubarraySum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void recaman ( int n ) { int arr [ ] = new int [ n ] ; arr [ 0 ] = 0 ; System . out . print ( arr [ 0 ] + " ▁ , " ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }
import java . io . * ; class Node { int data ; Node left , right ; Node ( int val ) { data = val ; left = right = null ; } } class GFG { static int ans , lh , rh , f ; static Node k ; public static Node Root ; static int height ( Node root ) { if ( root == null ) return 0 ; int left_height = height ( root . left ) ; int right_height = height ( root . right ) ; if ( ans < 1 + left_height + right_height ) { ans = 1 + left_height + right_height ; k = root ; lh = left_height ; rh = right_height ; } return 1 + Math . max ( left_height , right_height ) ; } static void printArray ( int [ ] ints , int len ) { int i ; if ( f == 0 ) { for ( i = len - 1 ; i >= 0 ; i -- ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } else if ( f == 1 ) { for ( i = 0 ; i < len ; i ++ ) { System . out . print ( ints [ i ] + " ▁ " ) ; } } } static void printPathsRecur ( Node node , int [ ] path , int pathLen , int max ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) { if ( pathLen == max && ( f == 0 f == 1 ) ) { printArray ( path , pathLen ) ; f = 2 ; } } else { printPathsRecur ( node . left , path , pathLen , max ) ; printPathsRecur ( node . right , path , pathLen , max ) ; } } static void diameter ( Node root ) { if ( root == null ) return ; ans = Integer . MIN_VALUE ; lh = 0 ; rh = 0 ; f = 0 ; int height_of_tree = height ( root ) ; int [ ] lPath = new int [ 100 ] ; int pathlen = 0 ; printPathsRecur ( k . left , lPath , pathlen , lh ) ; System . out . print ( k . data + " ▁ " ) ; int [ ] rPath = new int [ 100 ] ; f = 1 ; printPathsRecur ( k . right , rPath , pathlen , rh ) ; } public static void main ( String [ ] args ) { GFG . Root = new Node ( 1 ) ; GFG . Root . left = new Node ( 2 ) ; GFG . Root . right = new Node ( 3 ) ; GFG . Root . left . left = new Node ( 4 ) ; GFG . Root . left . right = new Node ( 5 ) ; GFG . Root . left . right . left = new Node ( 6 ) ; GFG . Root . left . right . right = new Node ( 7 ) ; GFG . Root . left . left . right = new Node ( 8 ) ; GFG . Root . left . left . right . left = new Node ( 9 ) ; diameter ( Root ) ; } }
import java . util . * ; class GFG { static void recaman ( int n ) { if ( n <= 0 ) return ; System . out . printf ( " % d , ▁ " , 0 ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ; s . add ( curr ) ; System . out . printf ( " % d , ▁ " , curr ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }
import java . util . * ; class GFG { public static void findFibSubset ( Integer [ ] x ) { List < Integer > fib = new ArrayList < Integer > ( ) ; List < Integer > result = new ArrayList < Integer > ( ) ; Integer max = Collections . max ( Arrays . asList ( x ) ) ; Integer a = 0 ; Integer b = 1 ; while ( b < max ) { Integer c = a + b ; a = b ; b = c ; fib . add ( c ) ; } for ( Integer i = 0 ; i < x . length ; i ++ ) { if ( fib . contains ( x [ i ] ) ) { result . add ( x [ i ] ) ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { Integer [ ] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; findFibSubset ( a ) ; } }
import java . util . * ; class GFG { static int sumOfDiv ( int x ) { int sum = 1 ; for ( int i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { sum += i ; if ( x / i != i ) sum += x / i ; } } return sum ; } static boolean isAmicable ( int a , int b ) { return ( sumOfDiv ( a ) == b && sumOfDiv ( b ) == a ) ; } static int countPairs ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( sumOfDiv ( arr [ i ] ) ) ) { int sum = sumOfDiv ( arr [ i ] ) ; if ( isAmicable ( arr [ i ] , sum ) ) count ++ ; } } return count / 2 ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 220 , 284 , 1184 , 1210 , 2 , 5 } ; int n1 = arr1 . length ; System . out . println ( countPairs ( arr1 , n1 ) ) ; int arr2 [ ] = { 2620 , 2924 , 5020 , 5564 , 6232 , 6368 } ; int n2 = arr2 . length ; System . out . println ( countPairs ( arr2 , n2 ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; public class GFG { static int findArea ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int [ ] dimension = { 0 , 0 } ; for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . length ; System . out . println ( findArea ( arr , n ) ) ; } }
import java . util . HashSet ; import java . util . Set ; public class GFG { static int findArea ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < > ( ) ; int first = 0 , second = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! s . contains ( arr [ i ] ) ) { s . add ( arr [ i ] ) ; continue ; } if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } return ( first * second ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 2 , 1 , 4 , 6 , 6 , 2 , 5 } ; int n = arr . length ; System . out . println ( findArea ( arr , n ) ) ; } }
import java . util . Stack ; import java . util . HashMap ; public class PrintPath { class Node { int data ; Node left , right ; Node ( int data ) { left = right = null ; this . data = data ; } } ; public static void printTopToBottomPath ( Node curr , HashMap < Node , Node > parent ) { Stack < Node > stk = new Stack < > ( ) ; while ( curr != null ) { stk . push ( curr ) ; curr = parent . get ( curr ) ; } while ( ! stk . isEmpty ( ) ) { curr = stk . pop ( ) ; System . out . print ( curr . data + " ▁ " ) ; } System . out . println ( ) ; } public static void printRootToLeaf ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < > ( ) ; nodeStack . push ( root ) ; HashMap < Node , Node > parent = new HashMap < > ( ) ; parent . put ( root , null ) ; while ( ! nodeStack . isEmpty ( ) ) { Node current = nodeStack . pop ( ) ; if ( current . left == null && current . right == null ) printTopToBottomPath ( current , parent ) ; if ( current . right != null ) { parent . put ( current . right , current ) ; nodeStack . push ( current . right ) ; } if ( current . left != null ) { parent . put ( current . left , current ) ; nodeStack . push ( current . left ) ; } } } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 2 ) ; printRootToLeaf ( root ) ; } }
import java . util . HashSet ; public class GameOfReplacingArrayElements { public static int playGame ( int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i : arr ) set . add ( i ) ; return ( set . size ( ) % 2 == 0 ) ? 1 : 2 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 2 , 2 , 2 } ; System . out . print ( " Player ▁ " + playGame ( arr ) + " ▁ wins " ) ; } }
import java . util . * ; class GfG { static int longLenStrictBitonicSub ( int arr [ ] , int n ) { HashMap < Integer , Integer > inc = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > dcr = new HashMap < Integer , Integer > ( ) ; int len_inc [ ] = new int [ n ] ; int len_dcr [ ] = new int [ n ] ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( inc . containsKey ( arr [ i ] - 1 ) ) len = inc . get ( arr [ i ] - 1 ) ; len_inc [ i ] = len + 1 ; inc . put ( arr [ i ] , len_inc [ i ] ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { int len = 0 ; if ( dcr . containsKey ( arr [ i ] - 1 ) ) len = dcr . get ( arr [ i ] - 1 ) ; len_dcr [ i ] = len + 1 ; dcr . put ( arr [ i ] , len_dcr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( " Longest ▁ length ▁ strict ▁ " + " bitonic ▁ subsequence ▁ = ▁ " + longLenStrictBitonicSub ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int lastSeenElement ( int a [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { hash . put ( a [ i ] , i ) ; } int res_ind = Integer . MAX_VALUE , res = 0 ; for ( Map . Entry < Integer , Integer > x : hash . entrySet ( ) ) { if ( x . getValue ( ) < res_ind ) { res_ind = x . getValue ( ) ; res = x . getKey ( ) ; } } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 1 , 2 , 2 , 4 , 1 } ; int n = a . length ; System . out . println ( lastSeenElement ( a , n ) ) ; } }
import java . util . * ; class GFG { public static void leftRotate ( int arr [ ] , int d , int n ) { leftRotateRec ( arr , 0 , d , n ) ; } public static void leftRotateRec ( int arr [ ] , int i , int d , int n ) { if ( d == 0 d == n ) return ; if ( n - d == d ) { swap ( arr , i , n - d + i , d ) ; return ; } if ( d < n - d ) { swap ( arr , i , n - d + i , d ) ; leftRotateRec ( arr , i , d , n - d ) ; } else { swap ( arr , i , d , n - d ) ; leftRotateRec ( arr , n - d + i , 2 * d - n , d ) ; } } public static void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void swap ( int arr [ ] , int fi , int si , int d ) { int i , temp ; for ( i = 0 ; i < d ; i ++ ) { temp = arr [ fi + i ] ; arr [ fi + i ] = arr [ si + i ] ; arr [ si + i ] = temp ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; leftRotate ( arr , 2 , 7 ) ; printArray ( arr , 7 ) ; } }
static void leftRotate ( int arr [ ] , int d , int n ) { int i , j ; if ( d == 0 d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }
import java . util . Arrays ; public class Test { static int arr [ ] = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; static void rotate ( ) { int i = 0 , j = arr . length - 1 ; while ( i != j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; } } public static void main ( String [ ] args ) { System . out . println ( " Given ▁ Array ▁ is " ) ; System . out . println ( Arrays . toString ( arr ) ) ; rotate ( ) ; System . out . println ( " Rotated ▁ Array ▁ is " ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
import java . io . * ; class GFG { static int pairsInSortedRotated ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; int cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 15 , 6 , 7 , 9 , 10 } ; int sum = 16 ; int n = arr . length ; System . out . println ( pairsInSortedRotated ( arr , n , sum ) ) ; } }
import java . util . Arrays ; class Test { static int arr [ ] = new int [ ] { 10 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; static int maxSum ( ) { int arrSum = 0 ; int currVal = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { arrSum = arrSum + arr [ i ] ; currVal = currVal + ( i * arr [ i ] ) ; } int maxVal = currVal ; for ( int j = 1 ; j < arr . length ; j ++ ) { currVal = currVal + arrSum - arr . length * arr [ arr . length - j ] ; if ( currVal > maxVal ) maxVal = currVal ; } return maxVal ; } public static void main ( String [ ] args ) { System . out . println ( " Max ▁ sum ▁ is ▁ " + maxSum ( ) ) ; } }
class GfG { static class Node { int data ; Node left , right ; } static void printOddNodes ( Node root , boolean isOdd ) { if ( root == null ) return ; if ( isOdd == true ) System . out . print ( root . data + " ▁ " ) ; printOddNodes ( root . left , ! isOdd ) ; printOddNodes ( root . right , ! isOdd ) ; } 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 ) ; printOddNodes ( root , true ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . max ( res , curr_sum ) ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int cum_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; int curr_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; int res = curr_val ; for ( int i = 1 ; i < n ; i ++ ) { int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . max ( res , next_val ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class LinearSearch { static int countRotations ( int arr [ ] , int n ) { int min = arr [ 0 ] , min_index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > arr [ i ] ) { min = arr [ i ] ; min_index = i ; } } return min_index ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = arr . length ; System . out . println ( countRotations ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class BinarySearch { static int countRotations ( int arr [ ] , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = arr . length ; System . out . println ( countRotations ( arr , 0 , n - 1 ) ) ; } }
class LeftRotate { static void preprocess ( int arr [ ] , int n , int temp [ ] ) { for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } static void leftRotate ( int arr [ ] , int n , int k , int temp [ ] ) { int start = k % n ; for ( int i = start ; i < start + n ; i ++ ) System . out . print ( temp [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int temp [ ] = new int [ 2 * n ] ; preprocess ( arr , n , temp ) ; int k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ; } }
import java . io . * ; class GFG { static void reverseArray ( int arr [ ] , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void rightRotate ( int arr [ ] , int d , int n ) { reverseArray ( arr , 0 , n - 1 ) ; reverseArray ( arr , 0 , d - 1 ) ; reverseArray ( arr , d , n - 1 ) ; } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = arr . length ; int k = 3 ; rightRotate ( arr , k , n ) ; printArray ( arr , n ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static void printOddNodes ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; boolean isOdd = true ; while ( true ) { int nodeCount = q . size ( ) ; if ( nodeCount == 0 ) break ; while ( nodeCount > 0 ) { Node node = q . peek ( ) ; if ( isOdd == true ) System . out . print ( node . data + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; nodeCount -- ; } isOdd = ! isOdd ; } } 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 ) ; printOddNodes ( root ) ; } }
class GFG { static int maxHamming ( int arr [ ] , int n ) { int brr [ ] = new int [ 2 * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ n + i ] = arr [ i ] ; int maxHam = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int currHam = 0 ; for ( int j = i , k = 0 ; j < ( i + n ) ; j ++ , k ++ ) if ( brr [ j ] != arr [ k ] ) currHam ++ ; if ( currHam == n ) return n ; maxHam = Math . max ( maxHam , currHam ) ; } return maxHam ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 } ; int n = arr . length ; System . out . print ( maxHamming ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class arr_rot { static void leftRotate ( int arr [ ] , int n , int k ) { int mod = k % n ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ ( i + mod ) % n ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int k = 2 ; leftRotate ( arr , n , k ) ; k = 3 ; leftRotate ( arr , n , k ) ; k = 4 ; leftRotate ( arr , n , k ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void leftRotate ( Integer arr [ ] , int k , int n ) { Collections . rotate ( Arrays . asList ( arr ) , n - k ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int k = 2 ; leftRotate ( arr , k , n ) ; } }
import java . util . * ; class GFG { static int findElement ( int [ ] arr , int [ ] [ ] ranges , int rotations , int index ) { for ( int i = rotations - 1 ; i >= 0 ; i -- ) { int left = ranges [ i ] [ 0 ] ; int right = ranges [ i ] [ 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int rotations = 2 ; int [ ] [ ] ranges = { { 0 , 2 } , { 0 , 3 } } ; int index = 1 ; System . out . println ( findElement ( arr , ranges , rotations , index ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static void splitArr ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ 0 ] ; for ( int j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . length ; int position = 2 ; splitArr ( arr , 6 , position ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class GFG { public static void fixArray ( int ar [ ] , int n ) { int i , j , temp ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( ar [ j ] == i ) { temp = ar [ j ] ; ar [ j ] = ar [ i ] ; ar [ i ] = temp ; break ; } } } for ( i = 0 ; i < n ; i ++ ) { if ( ar [ i ] != i ) { ar [ i ] = - 1 ; } } System . out . println ( " Array ▁ after ▁ Rearranging " ) ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( ar [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n , ar [ ] = { - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 } ; n = ar . length ; fixArray ( ar , n ) ; } }
class Node { int data ; Node left , right ; Node ( int data ) { left = right = null ; this . data = data ; } } ; public class FullNodes { public static void findFullNode ( Node root ) { if ( root != null ) { findFullNode ( root . left ) ; if ( root . left != null && root . right != null ) System . out . print ( root . data + " ▁ " ) ; findFullNode ( root . right ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; findFullNode ( root ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static void rearrangeArr ( int arr [ ] , int n ) { int evenPos = n / 2 ; int oddPos = n - evenPos ; int [ ] tempArr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) tempArr [ i ] = arr [ i ] ; Arrays . sort ( tempArr ) ; int j = oddPos - 1 ; for ( int i = 0 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j -- ; } j = oddPos ; for ( int i = 1 ; i < n ; i += 2 ) { arr [ i ] = tempArr [ j ] ; j ++ ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int size = 7 ; rearrangeArr ( arr , size ) ; } }
import java . io . * ; class PushZero { static void pushZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = arr . length ; pushZerosToEnd ( arr , n ) ; System . out . println ( " Array ▁ after ▁ pushing ▁ zeros ▁ to ▁ the ▁ back : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . io . * ; class GFG { static void moveZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; int temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] != 0 ) ) { temp = arr [ count ] ; arr [ count ] = arr [ i ] ; arr [ i ] = temp ; count = count + 1 ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = arr . length ; System . out . print ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; moveZerosToEnd ( arr , n ) ; System . out . print ( " Modified array : "); printArray ( arr , n ) ; } }
class GFG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } static int addBT ( Node root ) { if ( root == null ) return 0 ; return ( root . key + addBT ( root . left ) + addBT ( root . right ) ) ; } 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 . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . right . left . right = newNode ( 8 ) ; int sum = addBT ( root ) ; System . out . println ( " Sum ▁ of ▁ all ▁ the ▁ elements ▁ is : ▁ " + sum ) ; } }
import java . lang . * ; class GFG { static int minSwap ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] <= k ) ++ count ; int bad = 0 ; for ( int i = 0 ; i < count ; ++ i ) if ( arr [ i ] > k ) ++ bad ; int ans = bad ; for ( int i = 0 , j = count ; j < n ; ++ i , ++ j ) { if ( arr [ i ] > k ) -- bad ; if ( arr [ j ] > k ) ++ bad ; ans = Math . min ( ans , bad ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 5 , 6 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( minSwap ( arr , n , k ) + "NEW_LINE"); int arr1 [ ] = { 2 , 7 , 9 , 5 , 8 , 7 , 4 } ; n = arr1 . length ; k = 5 ; System . out . print ( minSwap ( arr1 , n , k ) ) ; } }
class GFG { static void pushZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 0 ) arr [ count ++ ] = arr [ i ] ; while ( count < n ) arr [ count ++ ] = 0 ; } static void modifyAndRearrangeArr ( int arr [ ] , int n ) { if ( n == 1 ) return ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( arr [ i ] != 0 ) && ( arr [ i ] == arr [ i + 1 ] ) ) { arr [ i ] = 2 * arr [ i ] ; arr [ i + 1 ] = 0 ; i ++ ; } } pushZerosToEnd ( arr , n ) ; } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 2 , 2 , 0 , 6 , 6 , 0 , 0 , 8 } ; int n = arr . length ; System . out . print ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; modifyAndRearrangeArr ( arr , n ) ; System . out . print ( " Modified ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
class GFG { public static void swap ( int [ ] A , int i , int j ) { int temp = A [ i ] ; A [ i ] = A [ j ] ; A [ j ] = temp ; } static void shiftAllZeroToLeft ( int array [ ] , int n ) { int lastSeenNonZero = 0 ; for ( int index = 0 ; index < n ; index ++ ) { if ( array [ index ] != 0 ) { swap ( array , array [ index ] , array [ lastSeenNonZero ] ) ; lastSeenNonZero ++ ; } } } }
import java . util . Arrays ; class Test { static int arr [ ] = new int [ ] { 50 , 40 , 70 , 60 , 90 } ; static int index [ ] = new int [ ] { 3 , 0 , 4 , 1 , 2 } ; static void reorder ( ) { int temp [ ] = new int [ arr . length ] ; for ( int i = 0 ; i < arr . length ; i ++ ) temp [ index [ i ] ] = arr [ i ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { arr [ i ] = temp [ i ] ; index [ i ] = i ; } } public static void main ( String [ ] args ) { reorder ( ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : " ) ; System . out . println ( Arrays . toString ( index ) ) ; } }
import java . util . Arrays ; class Test { static int arr [ ] = new int [ ] { 50 , 40 , 70 , 60 , 90 } ; static int index [ ] = new int [ ] { 3 , 0 , 4 , 1 , 2 } ; static void reorder ( ) { for ( int i = 0 ; i < arr . length ; i ++ ) { while ( index [ i ] != i ) { int oldTargetI = index [ index [ i ] ] ; char oldTargetE = ( char ) arr [ index [ i ] ] ; arr [ index [ i ] ] = arr [ i ] ; index [ index [ i ] ] = index [ i ] ; index [ i ] = oldTargetI ; arr [ i ] = oldTargetE ; } } } public static void main ( String [ ] args ) { reorder ( ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; System . out . println ( Arrays . toString ( arr ) ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : " ) ; System . out . println ( Arrays . toString ( index ) ) ; } }
class GFG { static int heapSize ; public static void heapify ( int arr [ ] , int index [ ] , int i ) { int largest = i ; int left = 2 * i + 1 ; int right = 2 * i + 2 ; if ( left < heapSize && index [ left ] > index [ largest ] ) { largest = left ; } if ( right < heapSize && index [ right ] > index [ largest ] ) { largest = right ; } if ( largest != i ) { int temp = arr [ largest ] ; arr [ largest ] = arr [ i ] ; arr [ i ] = temp ; temp = index [ largest ] ; index [ largest ] = index [ i ] ; index [ i ] = temp ; heapify ( arr , index , largest ) ; } } public static void heapSort ( int arr [ ] , int index [ ] , int n ) { for ( int i = ( n - 1 ) / 2 ; i >= 0 ; i -- ) { heapify ( arr , index , i ) ; } for ( int i = n - 1 ; i > 0 ; i -- ) { int temp = index [ 0 ] ; index [ 0 ] = index [ i ] ; index [ i ] = temp ; temp = arr [ 0 ] ; arr [ 0 ] = arr [ i ] ; arr [ i ] = temp ; heapSize -- ; heapify ( arr , index , 0 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 40 , 70 , 60 , 90 } ; int index [ ] = { 3 , 0 , 4 , 1 , 2 } ; int n = arr . length ; heapSize = n ; heapSort ( arr , index , n ) ; System . out . println ( " Reordered ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; System . out . println ( " Modified ▁ Index ▁ array ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( index [ i ] + " ▁ " ) ; } }
import java . io . * ; class GFG { static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void RearrangePosNeg ( int arr [ ] , int n ) { int key , j ; for ( int i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; if ( key > 0 ) continue ; j = i - 1 ; while ( j >= 0 && arr [ j ] > 0 ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } public static void main ( String [ ] args ) { int arr [ ] = { - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 } ; int n = arr . length ; RearrangePosNeg ( arr , n ) ; printArray ( arr , n ) ; } }
class GFG { static int sum = 0 ; static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static void sumOfParentOfX ( Node root , int x ) { if ( root == null ) return ; if ( ( root . left != null && root . left . data == x ) || ( root . right != null && root . right . data == x ) ) sum += root . data ; sumOfParentOfX ( root . left , x ) ; sumOfParentOfX ( root . right , x ) ; } static int sumOfParentOfXUtil ( Node root , int x ) { sum = 0 ; sumOfParentOfX ( root , x ) ; return sum ; } public static void main ( String args [ ] ) { Node root = getNode ( 4 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 5 ) ; root . left . left = getNode ( 7 ) ; root . left . right = getNode ( 2 ) ; root . right . left = getNode ( 2 ) ; root . right . right = getNode ( 3 ) ; int x = 2 ; System . out . println ( " Sum ▁ = ▁ " + sumOfParentOfXUtil ( root , x ) ) ; } }
import java . io . * ; class GFG { public static void RearrangePosNeg ( int arr [ ] ) { int i = 0 ; int j = arr . length - 1 ; while ( true ) { while ( arr [ i ] < 0 && i < arr . length ) i ++ ; while ( arr [ j ] > 0 && j >= 0 ) j -- ; if ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } else break ; } } public static void main ( String [ ] args ) { int arr [ ] = { - 12 , 11 , - 13 , - 5 , 6 , - 7 , 5 , - 3 , - 6 } ; RearrangePosNeg ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class RearrangeArray { void rearrangeNaive ( int arr [ ] , int n ) { int temp [ ] = new int [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) temp [ arr [ i ] ] = i ; for ( i = 0 ; i < n ; i ++ ) arr [ i ] = temp [ i ] ; } void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { RearrangeArray arrange = new RearrangeArray ( ) ; int arr [ ] = { 1 , 3 , 0 , 2 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; arrange . rearrangeNaive ( arr , n ) ; System . out . println ( " Modified ▁ array ▁ is ▁ " ) ; arrange . printArray ( arr , n ) ; } }
class GFG { static void rearrange ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ arr [ i ] % n ] += i * n ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] /= n ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 1 , 4 , 5 , 3 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ : ▁ " ) ; printArray ( arr , n ) ; rearrange ( arr , n ) ; System . out . println ( " Modified ▁ array ▁ is ▁ : " ) ; printArray ( arr , n ) ; } }
public class Main { public static void rearrange ( int arr [ ] , int n ) { int max_idx = n - 1 , min_idx = 0 ; int max_elem = arr [ n - 1 ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { arr [ i ] += ( arr [ max_idx ] % max_elem ) * max_elem ; max_idx -- ; } else { arr [ i ] += ( arr [ min_idx ] % max_elem ) * max_elem ; min_idx ++ ; } } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] / max_elem ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( " Original ▁ Array " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; rearrange ( arr , n ) ; System . out . print ( " Modified Array "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . io . * ; class GFG { static void rearrange ( int arr [ ] , int n ) { int j = 0 , temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( i != j ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 } ; int n = arr . length ; rearrange ( arr , n ) ; printArray ( arr , n ) ; } }
class GFG { static void rearrange ( int arr [ ] , int n ) { int temp ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 0 && arr [ i ] > arr [ i + 1 ] ) { temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } if ( i % 2 != 0 && arr [ i ] < arr [ i + 1 ] ) { temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } } } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 4 , 2 , 1 , 8 , 3 } ; int n = arr . length ; System . out . print ( "Before rearranging: NEW_LINE"); printArray ( arr , n ) ; rearrange ( arr , n ) ; System . out . print ( "After rearranging: NEW_LINE"); printArray ( arr , n ) ; } }
import java . io . * ; class GFG { public static void arrayEvenAndOdd ( int arr [ ] , int n ) { int [ ] a ; a = new int [ n ] ; int ind = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { a [ ind ] = arr [ i ] ; ind ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 != 0 ) { a [ ind ] = arr [ i ] ; ind ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 } ; int n = arr . length ; arrayEvenAndOdd ( arr , n ) ; } }
public class GFG { static void arrayEvenAndOdd ( int arr [ ] , int n ) { int i = - 1 , j = 0 ; while ( j != n ) { if ( arr [ j ] % 2 == 0 ) { i ++ ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } for ( int k = 0 ; k < n ; k ++ ) System . out . print ( arr [ k ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 2 , 4 , 7 , 6 , 9 , 10 } ; int n = arr . length ; arrayEvenAndOdd ( arr , n ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Sum { int sum = 0 ; } class BinaryTree { Node root ; void leftLeavesSumRec ( Node node , boolean isleft , Sum summ ) { if ( node == null ) return ; if ( node . left == null && node . right == null && isleft ) summ . sum = summ . sum + node . data ; leftLeavesSumRec ( node . left , true , summ ) ; leftLeavesSumRec ( node . right , false , summ ) ; } int leftLeavesSum ( Node node ) { Sum suum = new Sum ( ) ; leftLeavesSumRec ( node , false , suum ) ; return suum . sum ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 9 ) ; tree . root . right = new Node ( 49 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 5 ) ; tree . root . right . left = new Node ( 23 ) ; tree . root . right . right = new Node ( 52 ) ; tree . root . left . right . right = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 50 ) ; System . out . println ( " The ▁ sum ▁ of ▁ leaves ▁ is ▁ " + tree . leftLeavesSum ( tree . root ) ) ; } }
class Node { int key ; Node left , right ; Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; boolean isMirror ( Node node1 , Node node2 ) { if ( node1 == null && node2 == null ) return true ; if ( node1 != null && node2 != null && node1 . key == node2 . key ) return ( isMirror ( node1 . left , node2 . right ) && isMirror ( node1 . right , node2 . left ) ) ; return false ; } boolean isSymmetric ( ) { return isMirror ( root , root ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 4 ) ; tree . root . right . right = new Node ( 3 ) ; boolean output = tree . isSymmetric ( ) ; if ( output == true ) System . out . println ( " Symmetric " ) ; else System . out . println ( " Not ▁ symmetric " ) ; } }
class GFG { static class HeapNode { int val ; int r ; int c ; HeapNode ( int val , int r , int c ) { this . val = val ; this . c = c ; this . r = r ; } } static void swap ( int i , int min , HeapNode [ ] arr ) { HeapNode temp = arr [ i ] ; arr [ i ] = arr [ min ] ; arr [ min ] = temp ; } static void minHeapify ( HeapNode harr [ ] , int i , int heap_size ) { int l = 2 * i + 1 ; int r = 2 * i + 2 ; int min = i ; if ( l < heap_size && harr [ l ] . val < harr [ min ] . val ) { min = l ; } if ( r < heap_size && harr [ r ] . val = 0 ) { minHeapify ( harr , i , n ) ; i -- ; } } public static int kthSmallest ( int [ ] [ ] mat , int n , int k ) { if ( k > 0 && k < n * n ) { return Integer . MAX_VALUE ; } HeapNode harr [ ] = new HeapNode [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { harr [ i ] = new HeapNode ( mat [ 0 ] [ i ] , 0 , i ) ; } HeapNode hr = new HeapNode ( 0 , 0 , 0 ) ; for ( int i = 1 ; i <= k ; i ++ ) { hr = harr [ 0 ] ; int nextVal = hr . r < n - 1 ? mat [ hr . r + 1 ] [ hr . c ] : Integer . MAX_VALUE ; harr [ 0 ] = new HeapNode ( nextVal , hr . r + 1 , hr . c ) ; minHeapify ( harr , 0 , n ) ; } return hr . val ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 25 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; int res = kthSmallest ( mat , 4 , 7 ) ; System . out . print ( "7th ▁ smallest ▁ element ▁ is ▁ " + res ) ; } }
class Test { static int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ; static int largest ( ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } public static void main ( String [ ] args ) { System . out . println ( " Largest ▁ in ▁ given ▁ array ▁ is ▁ " + largest ( ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; if ( count >= 2 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { int first = Integer . MIN_VALUE ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < second ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; Node ( int key_ ) { this . key = key_ ; this . left = null ; this . right = null ; } } ; static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Stack < Node > stack_ = new Stack < > ( ) ; stack_ . push ( root ) ; int sum = 0 ; while ( stack_ . size ( ) > 0 ) { Node currentNode = stack_ . peek ( ) ; stack_ . pop ( ) ; if ( currentNode . left != null ) { stack_ . add ( currentNode . left ) ; if ( currentNode . left . left == null && currentNode . left . right == null ) { sum = sum + currentNode . left . key ; } } if ( currentNode . right != null ) stack_ . add ( currentNode . right ) ; } return sum ; } public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; System . out . print ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + "NEW_LINE"); } }
import java . util . * ; import java . lang . * ; public class GfG { public static void printSmall ( int arr [ ] , int n , int k ) { for ( int i = k ; i < n ; ++ i ) { int max_var = arr [ k - 1 ] ; int pos = k - 1 ; for ( int j = k - 2 ; j >= 0 ; j -- ) { if ( arr [ j ] > max_var ) { max_var = arr [ j ] ; pos = j ; } } if ( max_var > arr [ i ] ) { int j = pos ; while ( j < k - 1 ) { arr [ j ] = arr [ j + 1 ] ; j ++ ; } arr [ k - 1 ] = arr [ i ] ; } } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String argc [ ] ) { int [ ] arr = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = 10 ; int k = 5 ; printSmall ( arr , n , k ) ; } }
import java . io . * ; class KSmallestPair { static void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { System . out . print ( " k ▁ pairs ▁ don ' t ▁ exist " ) ; return ; } int index2 [ ] = new int [ n1 ] ; while ( k > 0 ) { int min_sum = Integer . MAX_VALUE ; int min_index = 0 ; for ( int i1 = 0 ; i1 < n1 ; i1 ++ ) { if ( index2 [ i1 ] < n2 && arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] < min_sum ) { min_index = i1 ; min_sum = arr1 [ i1 ] + arr2 [ index2 [ i1 ] ] ; } } System . out . print ( " ( " + arr1 [ min_index ] + " , ▁ " + arr2 [ index2 [ min_index ] ] + " ) ▁ " ) ; index2 [ min_index ] ++ ; k -- ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 11 } ; int n1 = arr1 . length ; int arr2 [ ] = { 2 , 4 , 8 } ; int n2 = arr2 . length ; int k = 4 ; kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) ; } }
import java . util . Scanner ; import java . util . Arrays ; class GFG { static int countPairs ( int [ ] a , int n , int mid ) { int res = 0 , value ; for ( int i = 0 ; i < n ; i ++ ) { int ub = upperbound ( a , n , a [ i ] + mid ) ; res += ( ub - ( i - 1 ) ) ; } return res ; } static int upperbound ( int a [ ] , int n , int value ) { int low = 0 ; int high = n ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value >= a [ mid ] ) low = mid + 1 ; else high = mid ; } return low ; } static int kthDiff ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; int low = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i <= n - 2 ; ++ i ) low = Math . min ( low , a [ i + 1 ] - a [ i ] ) ; int high = a [ n - 1 ] - a [ 0 ] ; while ( low < high ) { int mid = ( low + high ) >> 1 ; if ( countPairs ( a , n , mid ) < k ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String args [ ] ) { Scanner s = new Scanner ( System . in ) ; int k = 3 ; int a [ ] = { 1 , 2 , 3 , 4 } ; int n = a . length ; System . out . println ( kthDiff ( a , n , k ) ) ; } }
import java . util . * ; class GFG { static void print2largest ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } Arrays . sort ( arr ) ; for ( i = arr_size - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] != arr [ arr_size - 1 ] ) { System . out . printf ( " The ▁ second ▁ largest ▁ " + "element is %dNEW_LINE", arr[i]); return ; } } System . out . printf ( " There ▁ is ▁ no ▁ second ▁ " + "largest elementNEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 35 , 1 , 10 , 34 , 1 } ; int n = arr . length ; print2largest ( arr , n ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; Node ( int key_ ) { key = key_ ; left = null ; right = null ; } } ; static class pair { Node first ; boolean second ; public pair ( Node first , boolean second ) { this . first = first ; this . second = second ; } } static int sumOfLeftLeaves ( Node root ) { if ( root == null ) return 0 ; Queue < pair > q = new LinkedList < > ( ) ; q . add ( new pair ( root , false ) ) ; int sum = 0 ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) . first ; boolean is_left_child = q . peek ( ) . second ; q . remove ( ) ; if ( is_left_child ) sum = sum + temp . key ; if ( temp . left != null && temp . right != null && is_left_child ) sum = sum - temp . key ; if ( temp . left != null ) { q . add ( new pair ( temp . left , true ) ) ; } if ( temp . right != null ) { q . add ( new pair ( temp . right , false ) ) ; } } return sum ; } public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 9 ) ; root . right = new Node ( 49 ) ; root . right . left = new Node ( 23 ) ; root . right . right = new Node ( 52 ) ; root . right . right . left = new Node ( 50 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 12 ) ; root . left . right . right = new Node ( 12 ) ; System . out . print ( " Sum ▁ of ▁ left ▁ leaves ▁ is ▁ " + sumOfLeftLeaves ( root ) + "NEW_LINE"); } }
import java . io . * ; class GFG { int findFirstMissing ( int [ ] arr , int start , int end , int first ) { if ( start < end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] != mid + first ) return findFirstMissing ( arr , start , mid , first ) ; else return findFirstMissing ( arr , mid + 1 , end , first ) ; } return start + first ; } int findSmallestMissinginSortedArray ( int [ ] arr ) { if ( arr [ 0 ] != 0 ) return 0 ; if ( arr [ arr . length - 1 ] == arr . length - 1 ) return arr . length ; int first = arr [ 0 ] ; return findFirstMissing ( arr , 0 , arr . length - 1 , first ) ; } public static void main ( String [ ] args ) { GFG small = new GFG ( ) ; int arr [ ] = { 0 , 1 , 2 , 3 , 4 , 5 , 7 } ; int n = arr . length ; System . out . println ( " First ▁ Missing ▁ element ▁ is ▁ : ▁ " + small . findSmallestMissinginSortedArray ( arr ) ) ; } }
public class GFG { static class Pair { int min ; int max ; } static Pair getMinMax ( int arr [ ] , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n == 1 ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 0 ] ; return minmax ; } if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . max = arr [ 1 ] ; minmax . min = arr [ 0 ] ; } for ( i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } else if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } return minmax ; } public static void main ( String args [ ] ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; System . out . printf ( " Minimum element is % d ", minmax.min); System . out . printf ( " Maximum element is % d ", minmax.max); } }
import java . util . * ; class GFG { static int sumNodes ( int l ) { int leafNodeCount = ( int ) Math . pow ( 2 , l - 1 ) ; Vector < Vector < Integer > > vec = new Vector < Vector < Integer > > ( ) ; for ( int i = 1 ; i <= l ; i ++ ) vec . add ( new Vector < Integer > ( ) ) ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec . get ( l - 1 ) . add ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec . get ( i + 1 ) . size ( ) - 1 ) { vec . get ( i ) . add ( vec . get ( i + 1 ) . get ( k ) + vec . get ( i + 1 ) . get ( k + 1 ) ) ; k += 2 ; } } int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < vec . get ( i ) . size ( ) ; j ++ ) sum += vec . get ( i ) . get ( j ) ; } return sum ; } public static void main ( String args [ ] ) { int l = 3 ; System . out . println ( sumNodes ( l ) ) ; } }
public class GFG { static class Pair { int min ; int max ; } static Pair getMinMax ( int arr [ ] , int n ) { Pair minmax = new Pair ( ) ; int i ; if ( n % 2 == 0 ) { if ( arr [ 0 ] > arr [ 1 ] ) { minmax . max = arr [ 0 ] ; minmax . min = arr [ 1 ] ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 1 ] ; } i = 2 ; } else { minmax . min = arr [ 0 ] ; minmax . max = arr [ 0 ] ; i = 1 ; } while ( i < n - 1 ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] > minmax . max ) { minmax . max = arr [ i ] ; } if ( arr [ i + 1 ] < minmax . min ) { minmax . min = arr [ i + 1 ] ; } } else { if ( arr [ i + 1 ] > minmax . max ) { minmax . max = arr [ i + 1 ] ; } if ( arr [ i ] < minmax . min ) { minmax . min = arr [ i ] ; } } i += 2 ; } return minmax ; } public static void main ( String args [ ] ) { int arr [ ] = { 1000 , 11 , 445 , 1 , 330 , 3000 } ; int arr_size = 6 ; Pair minmax = getMinMax ( arr , arr_size ) ; System . out . printf ( " Minimum element is % d ", minmax.min); System . out . printf ( " Maximum element is % d ", minmax.max); } }
import java . util . * ; class Query { int L ; int R ; Query ( int L , int R ) { this . L = L ; this . R = R ; } } class GFG { static void printQuerySums ( int a [ ] , int n , ArrayList < Query > q , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q . get ( i ) . L , R = q . get ( i ) . R ; int sum = 0 ; for ( int j = L ; j <= R ; j ++ ) sum += a [ j ] ; System . out . println ( " Sum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + sum ) ; } } public static void main ( String argv [ ] ) { int a [ ] = { 1 , 1 , 2 , 1 , 3 , 4 , 5 , 2 , 8 } ; int n = a . length ; ArrayList < Query > q = new ArrayList < Query > ( ) ; q . add ( new Query ( 0 , 4 ) ) ; q . add ( new Query ( 1 , 3 ) ) ; q . add ( new Query ( 2 , 4 ) ) ; int m = q . size ( ) ; printQuerySums ( a , n , q , m ) ; } }
import java . util . * ; class GFG { static int MAXN = 10000 ; static int SQRSIZE = 100 ; static int [ ] arr = new int [ MAXN ] ; static int [ ] block = new int [ SQRSIZE ] ; static int blk_sz ; static void update ( int idx , int val ) { int blockNumber = idx / blk_sz ; block [ blockNumber ] += val - arr [ idx ] ; arr [ idx ] = val ; } static int query ( int l , int r ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { sum += arr [ l ] ; l ++ ; } while ( l + blk_sz <= r ) { sum += block [ l / blk_sz ] ; l += blk_sz ; } while ( l <= r ) { sum += arr [ l ] ; l ++ ; } return sum ; } static void preprocess ( int input [ ] , int n ) { int blk_idx = - 1 ; blk_sz = ( int ) Math . sqrt ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = input [ i ] ; if ( i % blk_sz == 0 ) { blk_idx ++ ; } block [ blk_idx ] += arr [ i ] ; } } public static void main ( String [ ] args ) { int input [ ] = { 1 , 5 , 2 , 4 , 6 , 1 , 3 , 5 , 7 , 10 } ; int n = input . length ; preprocess ( input , n ) ; System . out . println ( " query ( 3 , ▁ 8 ) ▁ : ▁ " + query ( 3 , 8 ) ) ; System . out . println ( " query ( 1 , ▁ 6 ) ▁ : ▁ " + query ( 1 , 6 ) ) ; update ( 8 , 0 ) ; System . out . println ( " query ( 8 , ▁ 8 ) ▁ : ▁ " + query ( 8 , 8 ) ) ; } }
import java . io . * ; class GFG { static int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static void buildSparseTable ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = arr [ i ] ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( lookup [ i ] [ j - 1 ] < lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } static int query ( int L , int R ) { int j = ( int ) Math . log ( R - L + 1 ) ; if ( lookup [ L ] [ j ] <= lookup [ R - ( 1 << j ) + 1 ] [ j ] ) return lookup [ L ] [ j ] ; else return lookup [ R - ( 1 << j ) + 1 ] [ j ] ; } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; buildSparseTable ( a , n ) ; System . out . println ( query ( 0 , 4 ) ) ; System . out . println ( query ( 4 , 7 ) ) ; System . out . println ( query ( 7 , 8 ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 500 ; static int [ ] [ ] table = new int [ MAX ] [ MAX ] ; static void buildSparseTable ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) table [ i ] [ 0 ] = arr [ i ] ; for ( int j = 1 ; j <= n ; j ++ ) for ( int i = 0 ; i <= n - ( 1 << j ) ; i ++ ) table [ i ] [ j ] = __gcd ( table [ i ] [ j - 1 ] , table [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) ; } static int query ( int L , int R ) { int j = ( int ) Math . log ( R - L + 1 ) ; return __gcd ( table [ L ] [ j ] , table [ R - ( 1 << j ) + 1 ] [ j ] ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; buildSparseTable ( a , n ) ; System . out . print ( query ( 0 , 2 ) + "NEW_LINE"); System . out . print ( query ( 1 , 3 ) + "NEW_LINE"); System . out . print ( query ( 4 , 5 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static class Query { int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ i ] = i ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ j ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = j ; } } static void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; System . out . println ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + arr [ lookup [ L ] [ R ] ] ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . length ; RMQ ( a , n , q , m ) ; } }
import java . util . * ; class GFG { static int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static class Query { int L , R ; public Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } static int query ( int arr [ ] , int L , int R ) { int j = ( int ) Math . log ( R - L + 1 ) ; if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) return arr [ lookup [ L ] [ j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ; } static void RMQ ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; System . out . println ( " Minimum ▁ of ▁ [ " + L + " , ▁ " + R + " ] ▁ is ▁ " + query ( arr , L , R ) ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 7 , 2 , 3 , 0 , 5 , 10 , 3 , 12 , 18 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 4 ) , new Query ( 4 , 7 ) , new Query ( 7 , 8 ) } ; int m = q . length ; RMQ ( a , n , q , m ) ; } }
import java . io . * ; import java . lang . Math ; class GFG { static double sumNodes ( int l ) { double leafNodeCount = Math . pow ( 2 , l - 1 ) ; double sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; double sum = sumLastLevel * l ; return sum ; } public static void main ( String [ ] args ) { int l = 3 ; System . out . println ( sumNodes ( l ) ) ; } }
import java . io . * ; class GFG { static void add ( int arr [ ] , int N , int lo , int hi , int val ) { arr [ lo ] += val ; if ( hi != N - 1 ) arr [ hi + 1 ] -= val ; } static void updateArray ( int arr [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) arr [ i ] += arr [ i - 1 ] ; } static void printArr ( int arr [ ] , int N ) { updateArray ( arr , N ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( " " + arr [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = new int [ N ] ; add ( arr , N , 0 , 2 , 100 ) ; add ( arr , N , 1 , 5 , 100 ) ; add ( arr , N , 2 , 3 , 100 ) ; printArr ( arr , N ) ; } }
import java . io . * ; public class Main { private static int [ ] st ; public static int constructST ( int [ ] arr , int ss , int se , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return st [ si ] ; } int mid = ss + ( se - ss ) / 2 ; st [ si ] = gcd ( constructST ( arr , ss , mid , si * 2 + 1 ) , constructST ( arr , mid + 1 , se , si * 2 + 2 ) ) ; return st [ si ] ; } public static int [ ] constructSegmentTree ( int [ ] arr ) { int height = ( int ) Math . ceil ( Math . log ( arr . length ) / Math . log ( 2 ) ) ; int size = 2 * ( int ) Math . pow ( 2 , height ) - 1 ; st = new int [ size ] ; constructST ( arr , 0 , arr . length - 1 , 0 ) ; return st ; } private static int gcd ( int a , int b ) { if ( a < b ) { int temp = b ; b = a ; a = temp ; } if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static int findGcd ( int ss , int se , int qs , int qe , int si ) { if ( ss > qe se < qs ) return 0 ; if ( qs <= ss && qe >= se ) return st [ si ] ; int mid = ss + ( se - ss ) / 2 ; return gcd ( findGcd ( ss , mid , qs , qe , si * 2 + 1 ) , findGcd ( mid + 1 , se , qs , qe , si * 2 + 2 ) ) ; } public static int findRangeGcd ( int ss , int se , int [ ] arr ) { int n = arr . length ; if ( ss < 0 se > n - 1 ss > se ) throw new IllegalArgumentException ( " Invalid ▁ arguments " ) ; return findGcd ( 0 , n - 1 , ss , se , 0 ) ; } public static void main ( String [ ] args ) throws IOException { int [ ] a = { 2 , 3 , 6 , 9 , 5 } ; constructSegmentTree ( a ) ; int l = 1 ; int r = 3 ; System . out . print ( " GCD ▁ of ▁ the ▁ given ▁ range ▁ is : ▁ " ) ; System . out . print ( findRangeGcd ( l , r , a ) ) ; } }
import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void FillPrefixSuffix ( int prefix [ ] , int arr [ ] , int suffix [ ] , int n ) { prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) ; suffix [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) ; } static int GCDoutsideRange ( int l , int r , int prefix [ ] , int suffix [ ] , int n ) { if ( l == 0 ) return suffix [ r + 1 ] ; if ( r == n - 1 ) return prefix [ l - 1 ] ; return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 9 } ; int n = arr . length ; int prefix [ ] = new int [ n ] ; int suffix [ ] = new int [ n ] ; FillPrefixSuffix ( prefix , arr , suffix , n ) ; int l = 0 , r = 0 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 1 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 2 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; } }
class Test { static final int MAX = 10001 ; static void update ( int idx , int blk , int val , int bit [ ] [ ] ) { for ( ; idx < MAX ; idx += ( idx & - idx ) ) bit [ blk ] [ idx ] += val ; } static int query ( int l , int r , int k , int arr [ ] , int blk_sz , int bit [ ] [ ] ) { int sum = 0 ; while ( l < r && l % blk_sz != 0 && l != 0 ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } while ( l + blk_sz <= r ) { int idx = k ; for ( ; idx > 0 ; idx -= idx & - idx ) sum += bit [ l / blk_sz ] [ idx ] ; l += blk_sz ; } while ( l <= r ) { if ( arr [ l ] <= k ) sum ++ ; l ++ ; } return sum ; } static void preprocess ( int arr [ ] , int blk_sz , int n , int bit [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) update ( arr [ i ] , i / blk_sz , 1 , bit ) ; } static void preprocessUpdate ( int i , int v , int blk_sz , int arr [ ] , int bit [ ] [ ] ) { update ( arr [ i ] , i / blk_sz , - 1 , bit ) ; update ( v , i / blk_sz , 1 , bit ) ; arr [ i ] = v ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 2 , 3 , 4 } ; int blk_sz = ( int ) Math . sqrt ( arr . length ) ; int bit [ ] [ ] = new int [ blk_sz + 1 ] [ MAX ] ; preprocess ( arr , blk_sz , arr . length , bit ) ; System . out . println ( query ( 1 , 3 , 1 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 3 , 10 , blk_sz , arr , bit ) ; System . out . println ( query ( 3 , 3 , 4 , arr , blk_sz , bit ) ) ; preprocessUpdate ( 2 , 1 , blk_sz , arr , bit ) ; preprocessUpdate ( 0 , 2 , blk_sz , arr , bit ) ; System . out . println ( query ( 0 , 4 , 5 , arr , blk_sz , bit ) ) ; } }
import java . io . * ; class GFG { static int countInRange ( int arr [ ] , int n , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 9 , 10 , 3 } ; int n = arr . length ; int i = 1 , j = 4 ; System . out . println ( countInRange ( arr , n , i , j ) ) ; i = 9 ; j = 12 ; System . out . println ( countInRange ( arr , n , i , j ) ) ; } }
import java . util . Arrays ; class GFG { static void precompute ( int arr [ ] , int n , int pre [ ] ) { Arrays . fill ( pre , 0 ) ; pre [ n - 1 ] = arr [ n - 1 ] * ( int ) ( Math . pow ( 2 , 0 ) ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) pre [ i ] = pre [ i + 1 ] + arr [ i ] * ( 1 << ( n - 1 - i ) ) ; } static int decimalOfSubarr ( int arr [ ] , int l , int r , int n , int pre [ ] ) { if ( r != n - 1 ) return ( pre [ l ] - pre [ r + 1 ] ) / ( 1 << ( n - 1 - r ) ) ; return pre [ l ] / ( 1 << ( n - 1 - r ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; int pre [ ] = new int [ n ] ; precompute ( arr , n , pre ) ; System . out . println ( decimalOfSubarr ( arr , 2 , 4 , n , pre ) ) ; System . out . println ( decimalOfSubarr ( arr , 4 , 5 , n , pre ) ) ; } }
import java . io . * ; class GFG { static int answerQuery ( int a [ ] , int n , int l , int r ) { int count = 0 ; l = l - 1 ; for ( int i = l ; i < r ; i ++ ) { int element = a [ i ] ; int divisors = 0 ; for ( int j = l ; j < r ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) divisors ++ ; else break ; } if ( divisors == ( r - l ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 5 } ; int n = a . length ; int l = 1 , r = 4 ; System . out . println ( answerQuery ( a , n , l , r ) ) ; l = 2 ; r = 4 ; System . out . println ( answerQuery ( a , n , l , r ) ) ; } }
import java . lang . Math ; class GFG { private static final int MAX = 2147483647 ; static int [ ] [ ] one = new int [ 100001 ] [ 32 ] ; static void make_prefix ( int A [ ] , int n ) { for ( int j = 0 ; j < 32 ; j ++ ) one [ 0 ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int a = A [ i - 1 ] ; for ( int j = 0 ; j < 32 ; j ++ ) { int x = ( int ) Math . pow ( 2 , j ) ; if ( ( a & x ) != 0 ) one [ i ] [ j ] = 1 + one [ i - 1 ] [ j ] ; else one [ i ] [ j ] = one [ i - 1 ] [ j ] ; } } } static int Solve ( int L , int R ) { int l = L , r = R ; int tot_bits = r - l + 1 ; int X = MAX ; for ( int i = 0 ; i < 31 ; i ++ ) { int x = one [ r ] [ i ] - one [ l - 1 ] [ i ] ; if ( x >= tot_bits - x ) { int ith_bit = ( int ) Math . pow ( 2 , i ) ; X = X ^ ith_bit ; } } return X ; } public static void main ( String [ ] args ) { int n = 5 , q = 3 ; int A [ ] = { 210 , 11 , 48 , 22 , 133 } ; int L [ ] = { 1 , 4 , 2 } , R [ ] = { 3 , 14 , 4 } ; make_prefix ( A , n ) ; for ( int j = 0 ; j < q ; j ++ ) System . out . println ( Solve ( L [ j ] , R [ j ] ) ) ; } }
import java . util . Arrays ; class GFG { static void record_sum ( int record [ ] , int l , int r , int n , int adder ) { for ( int i = l ; i <= r ; i ++ ) { record [ i ] += adder ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int arr [ ] = new int [ n ] ; Arrays . fill ( arr , 0 ) ; int query [ ] [ ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; int record [ ] = new int [ m ] ; Arrays . fill ( record , 0 ) ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( query [ i ] [ 0 ] == 2 ) { record_sum ( record , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , m , record [ i ] + 1 ) ; } else { record_sum ( record , i , i , m , 1 ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) { record_sum ( arr , query [ i ] [ 1 ] - 1 , query [ i ] [ 2 ] - 1 , n , record [ i ] ) ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
public class GFG { static final int max = 10000 ; static void update ( int arr [ ] , int l ) { arr [ l ] += arr [ l - 1 ] ; } static void record_func ( int block_size , int block [ ] , int record [ ] , int l , int r , int value ) { while ( l < r && l % block_size != 0 && l != 0 ) { record [ l ] += value ; l ++ ; } while ( l + block_size <= r + 1 ) { block [ l / block_size ] += value ; l += block_size ; } while ( l <= r ) { record [ l ] += value ; l ++ ; } } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int arr [ ] = new int [ n ] , record [ ] = new int [ m ] ; int block_size = ( int ) Math . sqrt ( m ) ; int block [ ] = new int [ max ] ; int command [ ] [ ] = { { 1 , 1 , 2 } , { 1 , 4 , 5 } , { 2 , 1 , 2 } , { 2 , 1 , 3 } , { 2 , 3 , 4 } } ; for ( int i = m - 1 ; i >= 0 ; i -- ) { if ( command [ i ] [ 0 ] == 2 ) { int x = i / ( block_size ) ; record_func ( block_size , block , record , command [ i ] [ 1 ] - 1 , command [ i ] [ 2 ] - 1 , ( block [ x ] + record [ i ] + 1 ) ) ; } else { record [ i ] ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { int check = ( i / block_size ) ; record [ i ] += block [ check ] ; } for ( int i = 0 ; i < m ; i ++ ) { if ( command [ i ] [ 0 ] == 1 ) { arr [ command [ i ] [ 1 ] - 1 ] += record [ i ] ; if ( ( command [ i ] [ 2 ] - 1 ) < n - 1 ) { arr [ ( command [ i ] [ 2 ] ) ] -= record [ i ] ; } } } for ( int i = 1 ; i < n ; i ++ ) { update ( arr , i ) ; } print ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] = ( val + BITree [ index ] ) ; index = ( index + ( index & ( - index ) ) ) ; } return ; } static int [ ] constructBITree ( int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; return BITree ; } static int getSum ( int BITree [ ] , int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum = ( sum + BITree [ index ] ) ; index -= index & ( - index ) ; } return sum ; } static void update ( int BITree [ ] , int l , int r , int n , int val ) { updateBIT ( BITree , n , l , val ) ; updateBIT ( BITree , n , r + 1 , - val ) ; return ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; int temp [ ] = { 1 , 1 , 2 , 1 , 4 , 5 , 2 , 1 , 2 , 2 , 1 , 3 , 2 , 3 , 4 } ; int [ ] [ ] q = new int [ 6 ] [ 3 ] ; int j = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = temp [ j ++ ] ; q [ i ] [ 1 ] = temp [ j ++ ] ; q [ i ] [ 2 ] = temp [ j ++ ] ; } int [ ] BITree = constructBITree ( m ) ; int [ ] BITree2 = constructBITree ( n ) ; Scanner sc = new Scanner ( System . in ) ; for ( int i = 1 ; i <= m ; i ++ ) { q [ i ] [ 0 ] = sc . nextInt ( ) ; q [ i ] [ 1 ] = sc . nextInt ( ) ; q [ i ] [ 2 ] = sc . nextInt ( ) ; } for ( int i = m ; i >= 1 ; i -- ) if ( q [ i ] [ 0 ] == 2 ) update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , 1 ) ; for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 2 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , m , val ) ; } } for ( int i = m ; i >= 1 ; i -- ) { if ( q [ i ] [ 0 ] == 1 ) { int val = getSum ( BITree , i - 1 ) ; update ( BITree2 , q [ i ] [ 1 ] - 1 , q [ i ] [ 2 ] - 1 , n , ( val + 1 ) ) ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( getSum ( BITree2 , i - 1 ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static class Query { int L , R , X ; public Query ( int L , int R , int X ) { this . L = L ; this . R = R ; this . X = X ; } } ; static int maxn = 100 ; static int [ ] root = new int [ maxn ] ; static int find ( int x ) { if ( x == root [ x ] ) return x ; else return root [ x ] = find ( root [ x ] ) ; } static void uni ( int x , int y ) { int p = find ( x ) , q = find ( y ) ; if ( p != q ) { root [ p ] = root [ q ] ; } } static void initialize ( int a [ ] , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < n ; i ++ ) root [ i ] = i ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] == a [ i - 1 ] ) uni ( i , i - 1 ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 1 , 5 , 4 , 5 } ; int n = a . length ; Query q [ ] = { new Query ( 0 , 2 , 2 ) , new Query ( 1 , 4 , 1 ) , new Query ( 2 , 4 , 5 ) } ; int m = q . length ; initialize ( a , n , q , m ) ; for ( int i = 0 ; i < m ; i ++ ) { int flag = 0 ; int l = q [ i ] . L , r = q [ i ] . R , x = q [ i ] . X ; int p = r ; while ( p >= l ) { if ( a [ p ] == x ) { flag = 1 ; break ; } p = find ( p ) - 1 ; } if ( flag != 0 ) System . out . println ( x + " ▁ exists ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; else System . out . println ( x + " ▁ does ▁ not ▁ exist ▁ between ▁ [ " + l + " , ▁ " + r + " ] ▁ " ) ; } } }
import java . util . HashMap ; import java . util . Map ; class GFG { static int solveQuery ( int start , int end , int arr [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = start ; i <= end ; i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; int count = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) if ( entry . getKey ( ) == entry . getValue ( ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = A . length ; int [ ] [ ] queries = { { 0 , 1 } , { 1 , 1 } , { 0 , 2 } , { 1 , 3 } , { 3 , 5 } , { 0 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { int start = queries [ i ] [ 0 ] ; int end = queries [ i ] [ 1 ] ; System . out . println ( " Answer ▁ for ▁ Query ▁ " + ( i + 1 ) + " ▁ = ▁ " + solveQuery ( start , end , A ) ) ; } } }
class GFG { static int answer_query ( int a [ ] , int n , int l , int r ) { int count = 0 ; for ( int i = l ; i < r ; i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = a . length ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( a , n , L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( a , n , L , R ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static HashMap < Integer , Integer > grid = new HashMap < > ( ) ; static Node newNode ( int data ) { Node Node = new Node ( ) ; Node . data = data ; Node . left = Node . right = null ; return Node ; } static void addConsideringGrid ( Node root , int level , int index ) { if ( root == null ) return ; if ( grid . containsKey ( level - index ) ) grid . put ( level - index , grid . get ( level - index ) + ( root . data ) ) ; else grid . put ( level - index , root . data ) ; addConsideringGrid ( root . left , level + 1 , index - 1 ) ; addConsideringGrid ( root . right , level + 1 , index + 1 ) ; } static Vector < Integer > diagonalSum ( Node root ) { grid . clear ( ) ; addConsideringGrid ( root , 0 , 0 ) ; Vector < Integer > ans = new Vector < > ( ) ; for ( Map . Entry < Integer , Integer > x : grid . entrySet ( ) ) { ans . add ( x . getValue ( ) ) ; } return ans ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 9 ) ; root . left . right = newNode ( 6 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . right = newNode ( 7 ) ; root . right . left . left = newNode ( 12 ) ; root . left . right . left = newNode ( 11 ) ; root . left . left . right = newNode ( 10 ) ; Vector < Integer > v = diagonalSum ( root ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; } }
class GFG { public static int N = 1000 ; static int prefixans [ ] = new int [ 1000 ] ; public static void countIndex ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && a [ i ] == a [ i + 1 ] ) prefixans [ i ] = 1 ; if ( i != 0 ) prefixans [ i ] += prefixans [ i - 1 ] ; } } public static int answer_query ( int l , int r ) { if ( l == 0 ) return prefixans [ r - 1 ] ; else return prefixans [ r - 1 ] - prefixans [ l - 1 ] ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 2 , 2 , 3 , 3 , 4 , 4 , 4 } ; int n = 9 ; countIndex ( a , n ) ; int L , R ; L = 1 ; R = 8 ; System . out . println ( answer_query ( L , R ) ) ; L = 0 ; R = 4 ; System . out . println ( answer_query ( L , R ) ) ; } }
import java . util . LinkedHashSet ; class GFG { static int repeated_digit ( int n ) { LinkedHashSet < Integer > s = new LinkedHashSet < > ( ) ; while ( n != 0 ) { int d = n % 10 ; if ( s . contains ( d ) ) { return 0 ; } s . add ( d ) ; n = n / 10 ; } return 1 ; } static int calculate ( int L , int R ) { int answer = 0 ; for ( int i = L ; i < R + 1 ; ++ i ) { answer = answer + repeated_digit ( i ) ; } return answer ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; System . out . println ( calculate ( L , R ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100 ; static Vector < Integer > Prefix = new Vector < > ( ) ; static int repeated_digit ( int n ) { HashSet < Integer > a = new HashSet < > ( ) ; int d ; while ( n != 0 ) { d = n % 10 ; if ( a . contains ( d ) ) return 0 ; a . add ( d ) ; n /= 10 ; } return 1 ; } static void pre_calculations ( ) { Prefix . add ( 0 ) ; Prefix . add ( repeated_digit ( 1 ) ) ; for ( int i = 2 ; i < MAX + 1 ; i ++ ) Prefix . add ( repeated_digit ( i ) + Prefix . elementAt ( i - 1 ) ) ; } static int calculate ( int L , int R ) { return Prefix . elementAt ( R ) - Prefix . elementAt ( L - 1 ) ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; pre_calculations ( ) ; System . out . println ( calculate ( L , R ) ) ; } }
class GFG { static void initializeDiffArray ( int A [ ] , int D [ ] ) { int n = A . length ; D [ 0 ] = A [ 0 ] ; D [ n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) D [ i ] = A [ i ] - A [ i - 1 ] ; } static void update ( int D [ ] , int l , int r , int x ) { D [ l ] += x ; D [ r + 1 ] -= x ; } static int printArray ( int A [ ] , int D [ ] ) { for ( int i = 0 ; i < A . length ; i ++ ) { if ( i == 0 ) A [ i ] = D [ i ] ; else A [ i ] = D [ i ] + A [ i - 1 ] ; System . out . print ( A [ i ] + " ▁ " ) ; } System . out . println ( ) ; return 0 ; } public static void main ( String [ ] args ) { int A [ ] = { 10 , 5 , 20 , 40 } ; int n = A . length ; int D [ ] = new int [ n + 1 ] ; initializeDiffArray ( A , D ) ; update ( D , 0 , 1 , 10 ) ; printArray ( A , D ) ; update ( D , 1 , 3 , 20 ) ; update ( D , 2 , 2 , 30 ) ; printArray ( A , D ) ; } }
import java . io . * ; import java . util . * ; class Kadane { static int maxSubArraySum ( int a [ ] ) { int size = a . length ; int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void main ( String [ ] args ) { int [ ] a = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySum ( a ) ) ; } }
static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = a [ 0 ] , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; }
class GFG { static void maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + max_so_far ) ; System . out . println ( " Starting ▁ index ▁ " + start ) ; System . out . println ( " Ending ▁ index ▁ " + end ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; maxSubArraySum ( a , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int price [ ] = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int profit = 0 ; for ( int i = 1 ; i < price . length ; i ++ ) { int sub = price [ i ] - price [ i - 1 ] ; if ( sub > 0 ) profit += sub ; } System . out . print ( " Maximum ▁ Profit = " + profit ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newnode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean printPathUtil ( Node node , HashSet < Integer > s , int root_data ) { if ( node == null ) return false ; int rem = root_data - node . data ; if ( s . contains ( rem ) ) return true ; s . add ( node . data ) ; boolean res = printPathUtil ( node . left , s , root_data ) || printPathUtil ( node . right , s , root_data ) ; s . remove ( node . data ) ; return res ; } static boolean isPathSum ( Node root ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; return printPathUtil ( root . left , s , root . data ) || printPathUtil ( root . right , s , root . data ) ; } public static void main ( String [ ] args ) { Node root = newnode ( 8 ) ; root . left = newnode ( 5 ) ; root . right = newnode ( 4 ) ; root . left . left = newnode ( 9 ) ; root . left . right = newnode ( 7 ) ; root . left . right . left = newnode ( 1 ) ; root . left . right . right = newnode ( 12 ) ; root . left . right . right . right = newnode ( 2 ) ; root . right . right = newnode ( 11 ) ; root . right . right . left = newnode ( 3 ) ; System . out . print ( isPathSum ( root ) == true ? " Yes " : " No " ) ; } }
class Test { static int arr [ ] = new int [ ] { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; static void findMinAvgSubarray ( int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } System . out . println ( " Subarray ▁ between ▁ [ " + res_index + " , ▁ " + ( res_index + k - 1 ) + " ] ▁ has ▁ minimum ▁ average " ) ; } public static void main ( String [ ] args ) { int k = 3 ; findMinAvgSubarray ( arr . length , k ) ; } }
class GFG { static int minJumps ( int arr [ ] , int n ) { int [ ] jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = Integer . MAX_VALUE ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = Integer . MAX_VALUE ; for ( int j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) min = jumps [ j ] ; } if ( min != Integer . MAX_VALUE ) jumps [ i ] = min + 1 ; else jumps [ i ] = min ; } } return jumps [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 1 , 0 , 9 } ; int size = arr . length ; System . out . println ( " Minimum ▁ number ▁ of " + " ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , size ) ) ; } }
class SmallestSubArraySum { static int smallestSubWithSum ( int arr [ ] , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) curr_sum += arr [ end ++ ] ; while ( curr_sum > x && start < n ) { if ( end - start < min_len ) min_len = end - start ; curr_sum -= arr [ start ++ ] ; } } return min_len ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 4 , 45 , 6 , 10 , 19 } ; int x = 51 ; int n1 = arr1 . length ; int res1 = smallestSubWithSum ( arr1 , n1 , x ) ; if ( res1 == n1 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res1 ) ; int arr2 [ ] = { 1 , 10 , 5 , 2 , 7 } ; int n2 = arr2 . length ; x = 9 ; int res2 = smallestSubWithSum ( arr2 , n2 , x ) ; if ( res2 == n2 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res2 ) ; int arr3 [ ] = { 1 , 11 , 100 , 1 , 0 , 200 , 3 , 2 , 1 , 250 } ; int n3 = arr3 . length ; x = 280 ; int res3 = smallestSubWithSum ( arr3 , n3 , x ) ; if ( res3 == n3 + 1 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( res3 ) ; } }
public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static int maxLen ; static int maxSum ; static void sumOfLongRootToLeafPath ( Node root , int sum , int len ) { if ( root == null ) { if ( maxLen < len ) { maxLen = len ; maxSum = sum ; } else if ( maxLen == len && maxSum < sum ) maxSum = sum ; return ; } sumOfLongRootToLeafPath ( root . left , sum + root . data , len + 1 ) ; sumOfLongRootToLeafPath ( root . right , sum + root . data , len + 1 ) ; } static int sumOfLongRootToLeafPathUtil ( Node root ) { if ( root == null ) return 0 ; maxSum = Integer . MIN_VALUE ; maxLen = 0 ; sumOfLongRootToLeafPath ( root , 0 , 0 ) ; return maxSum ; } public static void main ( String args [ ] ) { Node root = new Node ( 4 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( 2 ) ; root . right . right = new Node ( 3 ) ; root . left . right . left = new Node ( 6 ) ; System . out . println ( " Sum ▁ = ▁ " + sumOfLongRootToLeafPathUtil ( root ) ) ; } }
import java . io . * ; class GFG { static int findMaxAverage ( int [ ] arr , int n , int k ) { if ( k > n ) return - 1 ; int [ ] csum = new int [ n ] ; csum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) csum [ i ] = csum [ i - 1 ] + arr [ i ] ; int max_sum = csum [ k - 1 ] , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { int curr_sum = csum [ i ] - csum [ i - k ] ; if ( curr_sum > max_sum ) { max_sum = curr_sum ; max_end = i ; } } return max_end - k + 1 ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 12 , - 5 , - 6 , 50 , 3 } ; int k = 4 ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ " + " average ▁ subarray ▁ of ▁ length ▁ " + k + " ▁ begins ▁ at ▁ index ▁ " + findMaxAverage ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static int findMaxAverage ( int arr [ ] , int n , int k ) { if ( k > n ) return - 1 ; int sum = arr [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) sum += arr [ i ] ; int max_sum = sum , max_end = k - 1 ; for ( int i = k ; i < n ; i ++ ) { sum = sum + arr [ i ] - arr [ i - k ] ; if ( sum > max_sum ) { max_sum = sum ; max_end = i ; } } return max_end - k + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 12 , - 5 , - 6 , 50 , 3 } ; int k = 4 ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ average " + " ▁ subarray ▁ of ▁ length ▁ " + k + " ▁ begins ▁ at ▁ index ▁ " + findMaxAverage ( arr , n , k ) ) ; } }
class Test { static int arr [ ] = new int [ ] { 16 , 16 , 16 } ; static int countMinOperations ( int n ) { int result = 0 ; while ( true ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) break ; else if ( arr [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; if ( i == n ) { for ( int j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] / 2 ; result ++ ; } for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] % 2 == 1 ) { arr [ j ] -- ; result ++ ; } } } } public static void main ( String [ ] args ) { System . out . println ( "Minimum number of steps required to NEW_LINE" + " get ▁ the ▁ given ▁ target ▁ array ▁ is ▁ " + countMinOperations ( arr . length ) ) ; } }
class GFG { static int findMinOps ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 , j = n - 1 ; i <= j ; ) { if ( arr [ i ] == arr [ j ] ) { i ++ ; j -- ; } else if ( arr [ i ] > arr [ j ] ) { j -- ; arr [ j ] += arr [ j + 1 ] ; ans ++ ; } else { i ++ ; arr [ i ] += arr [ i - 1 ] ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 4 , 5 , 9 , 1 } ; System . out . println ( " Count ▁ of ▁ minimum ▁ operations ▁ is ▁ " + findMinOps ( arr , arr . length ) ) ; } }
class FindSmallestInteger { int findSmallest ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } public static void main ( String [ ] args ) { FindSmallestInteger small = new FindSmallestInteger ( ) ; int arr1 [ ] = { 1 , 3 , 4 , 5 } ; int n1 = arr1 . length ; System . out . println ( small . findSmallest ( arr1 , n1 ) ) ; int arr2 [ ] = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = arr2 . length ; System . out . println ( small . findSmallest ( arr2 , n2 ) ) ; int arr3 [ ] = { 1 , 1 , 1 , 1 } ; int n3 = arr3 . length ; System . out . println ( small . findSmallest ( arr3 , n3 ) ) ; int arr4 [ ] = { 1 , 1 , 3 , 4 } ; int n4 = arr4 . length ; System . out . println ( small . findSmallest ( arr4 , n4 ) ) ; } }
class GFG { static int findMinDiff ( int [ ] arr , int n ) { int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( ( arr [ i ] - arr [ j ] ) ) < diff ) diff = Math . abs ( ( arr [ i ] - arr [ j ] ) ) ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . length ) ) ; } }
import java . util . Arrays ; class GFG { static int findMinDiff ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 5 , 3 , 19 , 18 , 25 } ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , arr . length ) ) ; } }
import java . lang . * ; class GFG { public static void main ( String [ ] args ) { int a = 2 , b = 10 ; int size = Math . abs ( b - a ) + 1 ; int array [ ] = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) array [ i - a ] = 1 ; System . out . println ( " MULTIPLES ▁ of ▁ 2" + " ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( array [ i - a ] == 1 ) System . out . printf ( i + " ▁ " ) ; } }
import java . util . * ; class GFG { static int max ( int l , int r ) { return ( l > r ? l : r ) ; } static class Node { int data ; Node left , right ; } ; static class INT { int v ; INT ( int a ) { v = a ; } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return node ; } static void print ( Node root ) { if ( root != null ) { print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } static Node pruneUtil ( Node root , int k , INT sum ) { if ( root == null ) return null ; INT lsum = new INT ( sum . v + ( root . data ) ) ; INT rsum = new INT ( lsum . v ) ; root . left = pruneUtil ( root . left , k , lsum ) ; root . right = pruneUtil ( root . right , k , rsum ) ; sum . v = max ( lsum . v , rsum . v ) ; if ( sum . v < k ) { root = null ; } return root ; } static Node prune ( Node root , int k ) { INT sum = new INT ( 0 ) ; return pruneUtil ( root , k , sum ) ; } public static void main ( String args [ ] ) { int k = 45 ; Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 12 ) ; root . right . right . left = newNode ( 10 ) ; root . right . right . left . right = newNode ( 11 ) ; root . left . left . right . left = newNode ( 13 ) ; root . left . left . right . right = newNode ( 14 ) ; root . left . left . right . right . left = newNode ( 15 ) ; System . out . println ( "Tree before truncationNEW_LINE");  print ( root ) ; root = prune ( root , k ) ; System . out . println ( " Tree after truncation ");  print ( root ) ; } }
class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( ) { root = null ; } void printPostorder ( Node node ) { if ( node == null ) return ; printPostorder ( node . left ) ; printPostorder ( node . right ) ; System . out . print ( node . key + " ▁ " ) ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . key + " ▁ " ) ; printInorder ( node . right ) ; } void printPreorder ( Node node ) { if ( node == null ) return ; System . out . print ( node . key + " ▁ " ) ; printPreorder ( node . left ) ; printPreorder ( node . right ) ; } void printPostorder ( ) { printPostorder ( root ) ; } void printInorder ( ) { printInorder ( root ) ; } void printPreorder ( ) { printPreorder ( root ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ " ) ; tree . printPreorder ( ) ; System . out . println ( " Inorder traversal of binary tree is "); tree . printInorder ( ) ; System . out . println ( " Postorder traversal of binary tree is "); tree . printPostorder ( ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean checkbit ( int array [ ] , int index ) { int val = array [ index >> 5 ] & ( 1 << ( index & 31 ) ) ; if ( val == 0 ) return false ; return true ; } static void setbit ( int array [ ] , int index ) { array [ index >> 5 ] |= ( 1 << ( index & 31 ) ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 10 ; int size = Math . abs ( b - a ) ; size = ( int ) Math . ceil ( ( double ) size / 32 ) ; int [ ] array = new int [ size ] ; for ( int i = a ; i <= b ; i ++ ) if ( i % 2 == 0 i % 5 == 0 ) setbit ( array , i - a ) ; System . out . println ( " MULTIPLES ▁ of ▁ 2 ▁ and ▁ 5 : " ) ; for ( int i = a ; i <= b ; i ++ ) if ( checkbit ( array , i - a ) ) System . out . print ( i + " ▁ " ) ; } }
class Test { static int arr1 [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; static int arr2 [ ] = new int [ ] { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; static int longestCommonSum ( int n ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum1 = 0 , sum2 = 0 ; for ( int j = i ; j < n ; j ++ ) { sum1 += arr1 [ j ] ; sum2 += arr2 [ j ] ; if ( sum1 == sum2 ) { int len = j - i + 1 ; if ( len > maxLen ) maxLen = len ; } } } return maxLen ; } public static void main ( String [ ] args ) { System . out . print ( " Length ▁ of ▁ the ▁ longest ▁ common ▁ span ▁ with ▁ same ▁ sum ▁ is ▁ " ) ; System . out . println ( longestCommonSum ( arr1 . length ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int longestCommonSum ( int [ ] arr1 , int [ ] arr2 , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr1 [ i ] - arr2 [ i ] ; HashMap < Integer , Integer > hM = new HashMap < > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) max_len = i + 1 ; if ( hM . containsKey ( sum ) ) max_len = Math . max ( max_len , i - hM . get ( sum ) ) ; else hM . put ( sum , i ) ; } return max_len ; } public static void main ( String args [ ] ) { int [ ] arr1 = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; int [ ] arr2 = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = arr1 . length ; System . out . println ( longestCommonSum ( arr1 , arr2 , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void rearrange ( int [ ] arr , int n , int x ) { TreeMap < Integer , ArrayList < Integer > > m = new TreeMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int diff = Math . abs ( x - arr [ i ] ) ; if ( m . containsKey ( diff ) ) { ArrayList < Integer > al = m . get ( diff ) ; al . add ( arr [ i ] ) ; m . put ( diff , al ) ; } else { ArrayList < Integer > al = new ArrayList < > ( ) ; al . add ( arr [ i ] ) ; m . put ( diff , al ) ; } } int index = 0 ; for ( Map . Entry entry : m . entrySet ( ) ) { ArrayList < Integer > al = m . get ( entry . getKey ( ) ) ; for ( int i = 0 ; i < al . size ( ) ; i ++ ) arr [ index ++ ] = al . get ( i ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . length ; int x = 7 ; rearrange ( arr , n , x ) ; printArray ( arr , n ) ; } }
class SortWave { void swap ( int arr [ ] , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( arr , i - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } } public static void main ( String args [ ] ) { SortWave ob = new SortWave ( ) ; int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . length ; ob . sortInWave ( arr , n ) ; for ( int i : arr ) System . out . print ( i + " ▁ " ) ; } }
class Node { int data ; Node left ; Node right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public void print ( Node root ) { if ( root == null ) return ; print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } class BinaryTree { Node root ; public Node prune ( Node root , int sum ) { if ( root == null ) return null ; root . left = prune ( root . left , sum - root . data ) ; root . right = prune ( root . right , sum - root . data ) ; if ( isLeaf ( root ) ) { if ( sum > root . data ) root = null ; } return root ; } public boolean isLeaf ( Node root ) { if ( root == null ) return false ; if ( root . left == null && root . right == null ) return true ; return false ; } public class GFG { public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 10 ) ; tree . root . right . right . left . right = new Node ( 11 ) ; tree . root . left . left . right . left = new Node ( 13 ) ; tree . root . left . left . right . right = new Node ( 14 ) ; tree . root . left . left . right . right . left = new Node ( 15 ) ; System . out . println ( " Tree ▁ before ▁ truncation " ) ; tree . print ( tree . root ) ; tree . prune ( tree . root , 45 ) ; System . out . println ( " Tree after truncation "); tree . print ( tree . root ) ; } }
class GFG { static int sortedAfterSwap ( int [ ] A , int [ ] B , int n ) { int t = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( B [ i ] != 0 ) { if ( A [ i ] != i + 1 ) t = A [ i ] ; A [ i ] = A [ i + 1 ] ; A [ i + 1 ] = t ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != i + 1 ) return 0 ; } return 1 ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 5 , 3 , 4 , 6 } ; int [ ] B = { 0 , 1 , 1 , 1 , 0 } ; int n = A . length ; if ( sortedAfterSwap ( A , B , n ) == 0 ) System . out . println ( " A ▁ can ▁ be ▁ sorted " ) ; else System . out . println ( " A ▁ can ▁ not ▁ be ▁ sorted " ) ; } }
class segregation { static void segregate0and1 ( int arr [ ] , int n ) { int type0 = 0 ; int type1 = n - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { arr [ type0 ] = arr [ type0 ] + arr [ type1 ] ; arr [ type1 ] = arr [ type0 ] - arr [ type1 ] ; arr [ type0 ] = arr [ type0 ] - arr [ type1 ] ; type1 -- ; } else { type0 ++ ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 } ; segregate0and1 ( arr , arr . length ) ; for ( int a : arr ) System . out . print ( a + " ▁ " ) ; } }
import java . io . * ; class GFG { static void findMinSum ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( ! ( Math . abs ( arr [ i - 1 ] ) < Math . abs ( arr [ i ] ) ) ) { int temp = arr [ i - 1 ] ; arr [ i - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } int min = Integer . MAX_VALUE ; int x = 0 , y = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i - 1 ] + arr [ i ] ) <= min ) { min = Math . abs ( arr [ i - 1 ] + arr [ i ] ) ; x = i - 1 ; y = i ; } } System . out . println ( " The ▁ two ▁ elements ▁ whose ▁ " + " sum ▁ is ▁ minimum ▁ are ▁ " + arr [ x ] + " ▁ and ▁ " + arr [ y ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 60 , - 10 , 70 , - 80 , 85 } ; int n = arr . length ; findMinSum ( arr , n ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static boolean increasing ( int a [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( a [ i ] >= a [ i + 1 ] ) return false ; return true ; } public static boolean decreasing ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] < arr [ i + 1 ] ) return false ; return true ; } public static int shortestUnsorted ( int a [ ] , int n ) { if ( increasing ( a , n ) == true || decreasing ( a , n ) == true ) return 0 ; else return 3 ; } public static void main ( String [ ] args ) { int ar [ ] = new int [ ] { 7 , 9 , 10 , 8 , 11 } ; int n = ar . length ; System . out . println ( shortestUnsorted ( ar , n ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Res { int val ; } class BinaryTree { static Node root ; int maxPathSumUtil ( Node node , Res res ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return node . data ; int ls = maxPathSumUtil ( node . left , res ) ; int rs = maxPathSumUtil ( node . right , res ) ; if ( node . left != null && node . right != null ) { res . val = Math . max ( res . val , ls + rs + node . data ) ; return Math . max ( ls , rs ) + node . data ; } return ( node . left == null ) ? rs + node . data : ls + node . data ; } int maxPathSum ( Node node ) { Res res = new Res ( ) ; res . val = Integer . MIN_VALUE ; maxPathSumUtil ( root , res ) ; return res . val ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( - 15 ) ; tree . root . left = new Node ( 5 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( - 8 ) ; tree . root . left . right = new Node ( 1 ) ; tree . root . left . left . left = new Node ( 2 ) ; tree . root . left . left . right = new Node ( 6 ) ; tree . root . right . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . right = new Node ( 0 ) ; tree . root . right . right . right . left = new Node ( 4 ) ; tree . root . right . right . right . right = new Node ( - 1 ) ; tree . root . right . right . right . right . left = new Node ( 10 ) ; System . out . println ( " Max ▁ pathSum ▁ of ▁ the ▁ given ▁ binary ▁ tree ▁ is ▁ " + tree . maxPathSum ( root ) ) ; } }
import javafx . util . Pair ; import java . util . ArrayList ; import java . util . * ; class GfG { public static int minSwaps ( int [ ] arr ) { int n = arr . length ; ArrayList < Pair < Integer , Integer > > arrpos = new ArrayList < Pair < Integer , Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arrpos . add ( new Pair < Integer , Integer > ( arr [ i ] , i ) ) ; arrpos . sort ( new Comparator < Pair < Integer , Integer > > ( ) { @ Override public int compare ( Pair < Integer , Integer > o1 , Pair < Integer , Integer > o2 ) { if ( o1 . getKey ( ) > o2 . getKey ( ) ) return - 1 ; else if ( o1 . getKey ( ) . equals ( o2 . getKey ( ) ) ) return 0 ; else return 1 ; } } ) ; Boolean [ ] vis = new Boolean [ n ] ; Arrays . fill ( vis , false ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] || arrpos . get ( i ) . getValue ( ) == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; j = arrpos . get ( j ) . getValue ( ) ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } } class MinSwaps { public static void main ( String [ ] args ) { int [ ] a = { 1 , 5 , 4 , 3 , 2 } ; GfG g = new GfG ( ) ; System . out . println ( g . minSwaps ( a ) ) ; } }
import java . util . * ; import java . io . * ; class GfG { public void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public int indexOf ( int [ ] arr , int ele ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == ele ) { return i ; } } return - 1 ; } public int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = Arrays . copyOfRange ( arr , 0 , N ) ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; swap ( arr , i , indexOf ( arr , temp [ i ] ) ) ; } } return ans ; } } class Main { public static void main ( String [ ] args ) throws Exception { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . length ; System . out . println ( new GfG ( ) . minSwaps ( a , n ) ) ; } }
import java . util . * ; import java . io . * ; class GfG { public int minSwaps ( int [ ] arr , int N ) { int ans = 0 ; int [ ] temp = Arrays . copyOfRange ( arr , 0 , N ) ; HashMap < Integer , Integer > h = new HashMap < Integer , Integer > ( ) ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < N ; i ++ ) { h . put ( arr [ i ] , i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != temp [ i ] ) { ans ++ ; int init = arr [ i ] ; swap ( arr , i , h . get ( temp [ i ] ) ) ; h . put ( init , h . get ( temp [ i ] ) ) ; h . put ( temp [ i ] , i ) ; } } return ans ; } public void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } class Main { public static void main ( String [ ] args ) throws Exception { int [ ] a = { 101 , 758 , 315 , 730 , 472 , 619 , 460 , 479 } ; int n = a . length ; System . out . println ( new GfG ( ) . minSwaps ( a , n ) ) ; } }
class FindUnion { static int printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) System . out . print ( arr1 [ i ++ ] + " ▁ " ) ; else if ( arr2 [ j ] < arr1 [ i ] ) System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; else { System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } while ( i < m ) System . out . print ( arr1 [ i ++ ] + " ▁ " ) ; while ( j < n ) System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; return 0 ; } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; printUnion ( arr1 , arr2 , m , n ) ; } }
class FindIntersection { static void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ; else { System . out . print ( arr2 [ j ++ ] + " ▁ " ) ; i ++ ; } } } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 , 5 , 6 } ; int arr2 [ ] = { 2 , 3 , 5 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; printIntersection ( arr1 , arr2 , m , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printUnion ( int [ ] a , int n , int [ ] b , int m ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , i ) ; } for ( int i = 0 ; i < m ; i ++ ) { mp . put ( b [ i ] , i ) ; } System . out . println ( " The ▁ union ▁ set ▁ of ▁ both ▁ arrays ▁ is ▁ : " ) ; for ( Map . Entry mapElement : mp . entrySet ( ) ) { System . out . print ( mapElement . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 5 , 6 , 2 , 3 , 5 } ; int b [ ] = { 2 , 4 , 5 , 6 , 8 , 9 , 4 , 6 , 5 } ; printUnion ( a , 7 , b , 9 ) ; } }
import java . util . Arrays ; class UnionAndIntersection { void printUnion ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int tempp [ ] = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Arrays . sort ( arr1 ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . print ( arr1 [ i ] + " ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) == - 1 ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } } void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { if ( m > n ) { int tempp [ ] = arr1 ; arr1 = arr2 ; arr2 = tempp ; int temp = m ; m = n ; n = temp ; } Arrays . sort ( arr1 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( binarySearch ( arr1 , 0 , m - 1 , arr2 [ i ] ) != - 1 ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } } int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void main ( String [ ] args ) { UnionAndIntersection u_i = new UnionAndIntersection ( ) ; int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; u_i . printUnion ( arr1 , arr2 , m , n ) ; System . out . println ( " " ) ; System . out . println ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ " ) ; u_i . printIntersection ( arr1 , arr2 , m , n ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Maximum { int max_no = Integer . MIN_VALUE ; } class BinaryTree { Node root ; Maximum max = new Maximum ( ) ; Node target_leaf = null ; boolean printPath ( Node node , Node target_leaf ) { if ( node == null ) return false ; if ( node == target_leaf || printPath ( node . left , target_leaf ) || printPath ( node . right , target_leaf ) ) { System . out . print ( node . data + " ▁ " ) ; return true ; } return false ; } void getTargetLeaf ( Node node , Maximum max_sum_ref , int curr_sum ) { if ( node == null ) return ; curr_sum = curr_sum + node . data ; if ( node . left == null && node . right == null ) { if ( curr_sum > max_sum_ref . max_no ) { max_sum_ref . max_no = curr_sum ; target_leaf = node ; } } getTargetLeaf ( node . left , max_sum_ref , curr_sum ) ; getTargetLeaf ( node . right , max_sum_ref , curr_sum ) ; } int maxSumPath ( ) { if ( root == null ) return 0 ; getTargetLeaf ( root , max , 0 ) ; printPath ( root , target_leaf ) ; return max . max_no ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 7 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; System . out . println ( " Following ▁ are ▁ the ▁ nodes ▁ " + " on ▁ maximum ▁ sum ▁ path " ) ; int sum = tree . maxSumPath ( ) ; System . out . println ( " " ) ; System . out . println ( " Sum ▁ of ▁ nodes ▁ is ▁ : ▁ " + sum ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void intersection ( int a [ ] , int b [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { System . out . print ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 3 , 4 , 5 , 5 , 6 } ; int b [ ] = { 3 , 3 , 5 } ; int n = a . length ; int m = b . length ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; intersection ( a , b , n , m ) ; } }
import java . util . HashSet ; class Test { static void printUnion ( int arr1 [ ] , int arr2 [ ] ) { HashSet < Integer > hs = new HashSet < > ( ) ; for ( int i = 0 ; i < arr1 . length ; i ++ ) hs . add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . length ; i ++ ) hs . add ( arr2 [ i ] ) ; System . out . println ( hs ) ; } static void printIntersection ( int arr1 [ ] , int arr2 [ ] ) { HashSet < Integer > hs = new HashSet < > ( ) ; HashSet < Integer > hs1 = new HashSet < > ( ) ; for ( int i = 0 ; i < arr1 . length ; i ++ ) hs . add ( arr1 [ i ] ) ; for ( int i = 0 ; i < arr2 . length ; i ++ ) if ( hs . contains ( arr2 [ i ] ) ) System . out . print ( arr2 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 7 , 1 , 5 , 2 , 3 , 6 } ; int arr2 [ ] = { 3 , 8 , 6 , 20 , 7 } ; System . out . println ( " Union ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printUnion ( arr1 , arr2 ) ; System . out . println ( " Intersection ▁ of ▁ two ▁ arrays ▁ is ▁ : ▁ " ) ; printIntersection ( arr1 , arr2 ) ; } }
import java . io . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortArr ( int arr [ ] , int n ) { int i , cnt0 = 0 , cnt1 = 0 , cnt2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { switch ( arr [ i ] ) { case 0 : cnt0 ++ ; break ; case 1 : cnt1 ++ ; break ; case 2 : cnt2 ++ ; break ; } } i = 0 ; while ( cnt0 > 0 ) { arr [ i ++ ] = 0 ; cnt0 -- ; } while ( cnt1 > 0 ) { arr [ i ++ ] = 1 ; cnt1 -- ; } while ( cnt2 > 0 ) { arr [ i ++ ] = 2 ; cnt2 -- ; } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
import java . util . * ; class GFG { static void CountTriangles ( int [ ] A ) { int n = A . length ; Arrays . sort ( A ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else { l ++ ; } } } System . out . print ( " No ▁ of ▁ possible ▁ solutions : ▁ " + count ) ; } public static void main ( String [ ] args ) { int [ ] A = { 4 , 3 , 5 , 7 , 6 } ; CountTriangles ( A ) ; } }
import java . util . HashMap ; public class FindSumOfNotAdjacentNodes { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; public static int sumOfGrandChildren ( Node node , HashMap < Node , Integer > mp ) { int sum = 0 ; if ( node . left != null ) sum += getMaxSumUtil ( node . left . left , mp ) + getMaxSumUtil ( node . left . right , mp ) ; if ( node . right != null ) sum += getMaxSumUtil ( node . right . left , mp ) + getMaxSumUtil ( node . right . right , mp ) ; return sum ; } public static int getMaxSumUtil ( Node node , HashMap < Node , Integer > mp ) { if ( node == null ) return 0 ; if ( mp . containsKey ( node ) ) return mp . get ( node ) ; int incl = node . data + sumOfGrandChildren ( node , mp ) ; int excl = getMaxSumUtil ( node . left , mp ) + getMaxSumUtil ( node . right , mp ) ; mp . put ( node , Math . max ( incl , excl ) ) ; return mp . get ( node ) ; } public static int getMaxSum ( Node node ) { if ( node == null ) return 0 ; HashMap < Node , Integer > mp = new HashMap < > ( ) ; return getMaxSumUtil ( node , mp ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . left . left = new Node ( 1 ) ; System . out . print ( getMaxSum ( root ) ) ; } }
public static long countPairsBruteForce ( long X [ ] , long Y [ ] , int m , int n ) { long ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( Math . pow ( X [ i ] , Y [ j ] ) > Math . pow ( Y [ j ] , X [ i ] ) ) ans ++ ; return ans ; }
import java . util . Arrays ; class Test { static int count ( int x , int Y [ ] , int n , int NoOfY [ ] ) { if ( x == 0 ) return 0 ; if ( x == 1 ) return NoOfY [ 0 ] ; int idx = Arrays . binarySearch ( Y , x ) ; int ans ; if ( idx < 0 ) { idx = Math . abs ( idx + 1 ) ; ans = Y . length - idx ; } else { while ( idx < n && Y [ idx ] == x ) { idx ++ ; } ans = Y . length - idx ; } ans += ( NoOfY [ 0 ] + NoOfY [ 1 ] ) ; if ( x == 2 ) ans -= ( NoOfY [ 3 ] + NoOfY [ 4 ] ) ; if ( x == 3 ) ans += NoOfY [ 2 ] ; return ans ; } static long countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int NoOfY [ ] = new int [ 5 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( Y [ i ] < 5 ) NoOfY [ Y [ i ] ] ++ ; Arrays . sort ( Y ) ; long total_pairs = 0 ; for ( int i = 0 ; i < m ; i ++ ) total_pairs += count ( X [ i ] , Y , n , NoOfY ) ; return total_pairs ; } public static void main ( String args [ ] ) { int X [ ] = { 2 , 1 , 6 } ; int Y [ ] = { 1 , 5 } ; System . out . println ( " Total ▁ pairs ▁ = ▁ " + countPairs ( X , Y , X . length , Y . length ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 , i ; Arrays . sort ( arr ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
static int MAX = 100000 ; public static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; boolean hashmap [ MAX ] = { false } ; for ( int i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; if ( x - k >= 0 && hashmap [ x - k ] ) count ++ ; if ( x + k < MAX && hashmap [ x + k ] ) count ++ ; hashmap [ x ] = false ; } return count ; }
import java . util . * ; class GFG { static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; Arrays . sort ( arr ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
public class FindSumOfNotAdjacentNodes { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; public static Pair maxSumHelper ( Node root ) { if ( root == null ) { Pair sum = new Pair ( 0 , 0 ) ; return sum ; } Pair sum1 = maxSumHelper ( root . left ) ; Pair sum2 = maxSumHelper ( root . right ) ; Pair sum = new Pair ( 0 , 0 ) ; sum . first = sum1 . second + sum2 . second + root . data ; sum . second = Math . max ( sum1 . first , sum1 . second ) + Math . max ( sum2 . first , sum2 . second ) ; return sum ; } public static int maxSum ( Node root ) { Pair res = maxSumHelper ( root ) ; return Math . max ( res . first , res . second ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 1 ) ; root . left . left = new Node ( 2 ) ; root . left . left . left = new Node ( 1 ) ; root . left . right = new Node ( 3 ) ; root . left . right . left = new Node ( 4 ) ; root . left . right . right = new Node ( 5 ) ; System . out . print ( maxSum ( root ) ) ; } } class Pair { int first , second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } }
import java . io . * ; class PairSum { static void constructArr ( int arr [ ] , int pair [ ] , int n ) { arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ; for ( int i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; } public static void main ( String [ ] args ) { int pair [ ] = { 15 , 13 , 11 , 10 , 12 , 10 , 9 , 8 , 7 , 5 } ; int n = 5 ; int [ ] arr = new int [ n ] ; constructArr ( arr , pair , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . util . Arrays ; class Test { static int arr1 [ ] = new int [ ] { 1 , 5 , 9 , 10 , 15 , 20 } ; static int arr2 [ ] = new int [ ] { 2 , 3 , 8 , 13 } ; static void merge ( int m , int n ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { int j , last = arr1 [ m - 1 ] ; for ( j = m - 2 ; j >= 0 && arr1 [ j ] > arr2 [ i ] ; j -- ) arr1 [ j + 1 ] = arr1 [ j ] ; if ( j != m - 2 last > arr2 [ i ] ) { arr1 [ j + 1 ] = arr2 [ i ] ; arr2 [ i ] = last ; } } } public static void main ( String [ ] args ) { merge ( arr1 . length , arr2 . length ) ; System . out . print ( " After ▁ Merging ▁ nFirst ▁ Array : ▁ " ) ; System . out . println ( Arrays . toString ( arr1 ) ) ; System . out . print ( " Second ▁ Array : ▁ " ) ; System . out . println ( Arrays . toString ( arr2 ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static void merge ( int m , int n ) { int i = 0 , j = 0 , k = n - 1 ; while ( i <= k and j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) i ++ ; else { int temp = arr2 [ j ] ; arr2 [ j ] = arr1 [ k ] ; arr1 [ k ] = temp ; j ++ ; k -- ; } } Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = new int [ ] { 1 , 5 , 9 , 10 , 15 , 20 } ; int arr2 [ ] = new int [ ] { 2 , 3 , 8 , 13 } ; merge ( arr1 . length , arr2 . length ) ; System . out . print ( "After Merging  First Array : "); System . out . println ( Arrays . toString ( arr1 ) ) ; System . out . print ( " Second ▁ Array : ▁ " ) ; System . out . println ( Arrays . toString ( arr2 ) ) ; } }
import java . util . * ; import java . lang . * ; class GfG { public static int minMaxProduct ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; return arr1 [ n1 - 1 ] * arr2 [ 0 ] ; } public static void main ( String argc [ ] ) { int [ ] arr1 = new int [ ] { 10 , 2 , 3 , 6 , 4 , 1 } ; int [ ] arr2 = new int [ ] { 5 , 1 , 4 , 2 , 6 , 9 } ; int n1 = 6 ; int n2 = 6 ; System . out . println ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ; } }
import java . util . * ; import java . lang . * ; class GfG { public static int minMaxProduct ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { int max = arr1 [ 0 ] ; int min = arr2 [ 0 ] ; int i ; for ( i = 1 ; i < n1 && i < n2 ; ++ i ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; if ( arr2 [ i ] < min ) min = arr2 [ i ] ; } while ( i < n1 ) { if ( arr1 [ i ] > max ) max = arr1 [ i ] ; i ++ ; } while ( i < n2 ) { if ( arr2 [ i ] < min ) min = arr2 [ i ] ; i ++ ; } return max * min ; } public static void main ( String argc [ ] ) { int [ ] arr1 = new int [ ] { 10 , 2 , 3 , 6 , 4 , 1 } ; int [ ] arr2 = new int [ ] { 5 , 1 , 4 , 2 , 6 , 9 } ; int n1 = 6 ; int n2 = 6 ; System . out . println ( minMaxProduct ( arr1 , arr2 , n1 , n2 ) ) ; } }
class Main { static int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; arr [ n ] = key ; return ( n + 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = 20 ; int n = 6 ; int i , key = 26 ; System . out . print ( " Before ▁ Insertion : ▁ " ) ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; n = insertSorted ( arr , n , key , capacity ) ; System . out . print ( " After Insertion : "); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class Main { static int findElement ( int arr [ ] , int n , int key ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == key ) return i ; return - 1 ; } static int deleteElement ( int arr [ ] , int n , int key ) { int pos = findElement ( arr , n , key ) ; if ( pos == - 1 ) { System . out . println ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void main ( String args [ ] ) { int i ; int arr [ ] = { 10 , 50 , 30 , 40 , 20 } ; int n = arr . length ; int key = 30 ; System . out . println ( " Array ▁ before ▁ deletion " ) ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; n = deleteElement ( arr , n , key ) ; System . out . println ( " Array after deletion "); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . util . * ; public class Main { static class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } public static int getSumAlternate ( Node root ) { if ( root == null ) return 0 ; int sum = root . data ; if ( root . left != null ) { sum += getSum ( root . left . left ) ; sum += getSum ( root . left . right ) ; } if ( root . right != null ) { sum += getSum ( root . right . left ) ; sum += getSum ( root . right . right ) ; } return sum ; } public static int getSum ( Node root ) { if ( root == null ) return 0 ; return Math . max ( getSumAlternate ( root ) , ( getSumAlternate ( root . left ) + getSumAlternate ( root . right ) ) ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . left . right = new Node ( 5 ) ; root . right . left . right . left = new Node ( 6 ) ; System . out . println ( getSum ( root ) ) ; } }
class Main { static int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = arr . length ; int n = 6 ; int key = 26 ; System . out . print ( " Before Insertion : "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; n = insertSorted ( arr , n , key , capacity ) ; System . out . print ( " After Insertion : "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class Main { static int binarySearch ( int arr [ ] , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } static int deleteElement ( int arr [ ] , int n , int key ) { int pos = binarySearch ( arr , 0 , n - 1 , key ) ; if ( pos == - 1 ) { System . out . println ( " Element ▁ not ▁ found " ) ; return n ; } int i ; for ( i = pos ; i < n - 1 ; i ++ ) arr [ i ] = arr [ i + 1 ] ; return n - 1 ; } public static void main ( String [ ] args ) { int i ; int arr [ ] = { 10 , 20 , 30 , 40 , 50 } ; int n = arr . length ; int key = 30 ; System . out . print ( "Array before deletion:NEW_LINE"); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; n = deleteElement ( arr , n , key ) ; System . out . print ( " Array after deletion : "); for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class FindCommon { void findCommon ( int ar1 [ ] , int ar2 [ ] , int ar3 [ ] ) { int i = 0 , j = 0 , k = 0 ; while ( i < ar1 . length && j < ar2 . length && k < ar3 . length ) { if ( ar1 [ i ] == ar2 [ j ] && ar2 [ j ] == ar3 [ k ] ) { System . out . print ( ar1 [ i ] + " ▁ " ) ; i ++ ; j ++ ; k ++ ; } else if ( ar1 [ i ] < ar2 [ j ] ) i ++ ; else if ( ar2 [ j ] < ar3 [ k ] ) j ++ ; else k ++ ; } } public static void main ( String args [ ] ) { FindCommon ob = new FindCommon ( ) ; int ar1 [ ] = { 1 , 5 , 10 , 20 , 40 , 80 } ; int ar2 [ ] = { 6 , 7 , 20 , 80 , 100 } ; int ar3 [ ] = { 3 , 4 , 15 , 20 , 30 , 70 , 80 , 120 } ; System . out . print ( " Common ▁ elements ▁ are ▁ " ) ; ob . findCommon ( ar1 , ar2 , ar3 ) ; } }
class Test { static int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } static int findPos ( int arr [ ] , int key ) { int l = 0 , h = 1 ; int val = arr [ 0 ] ; while ( val < key ) { l = h ; if ( 2 * h < arr . length - 1 ) h = 2 * h ; else h = arr . length - 1 ; val = arr [ h ] ; } return binarySearch ( arr , l , h , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 3 , 5 , 7 , 9 , 10 , 90 , 100 , 130 , 140 , 160 , 170 } ; int ans = findPos ( arr , 10 ) ; if ( ans == - 1 ) System . out . println ( " Element ▁ not ▁ found " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + ans ) ; } }
class MaxSum { static int findSingle ( int ar [ ] , int ar_size ) { int res = ar [ 0 ] ; for ( int i = 1 ; i < ar_size ; i ++ ) res = res ^ ar [ i ] ; return res ; } public static void main ( String [ ] args ) { int ar [ ] = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = ar . length ; System . out . println ( " Element ▁ occurring ▁ once ▁ is ▁ " + findSingle ( ar , n ) + " ▁ " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int singleNumber ( int [ ] nums , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . put ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( int ) ( 2 * ( sum1 ) - sum2 ) ; } public static void main ( String args [ ] ) { int [ ] a = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; System . out . println ( singleNumber ( a , n ) ) ; int [ ] b = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; System . out . println ( singleNumber ( b , n ) ) ; } }
import java . io . * ; class GFG { static boolean isPresent ( int B [ ] , int m , int x ) { for ( int i = 0 ; i < m ; i ++ ) if ( B [ i ] == x ) return true ; return false ; } static int findMaxSubarraySumUtil ( int A [ ] , int B [ ] , int n , int m ) { int max_so_far = - 2147483648 , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPresent ( B , m , A [ i ] ) ) { curr_max = 0 ; continue ; } curr_max = Math . max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } static void findMaxSubarraySum ( int A [ ] , int B [ ] , int n , int m ) { int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == - 2147483648 ) { System . out . println ( " Maximum ▁ Subarray ▁ Sum " + " ▁ " + " can ' t ▁ be ▁ found " ) ; } else { System . out . println ( " The ▁ Maximum ▁ Subarray ▁ Sum ▁ = ▁ " + maxSubarraySum ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 3 , 4 , 5 , - 4 , 6 } ; int B [ ] = { 1 , 8 , 5 } ; int n = A . length ; int m = B . length ; findMaxSubarraySum ( A , B , n , m ) ; } }
import java . util . * ; class GFG { static int findMaxSubarraySumUtil ( int A [ ] , int B [ ] , int n , int m ) { int max_so_far = Integer . MIN_VALUE , curr_max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arrays . binarySearch ( B , A [ i ] ) >= 0 ) { curr_max = 0 ; continue ; } curr_max = Math . max ( A [ i ] , curr_max + A [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } static void findMaxSubarraySum ( int A [ ] , int B [ ] , int n , int m ) { Arrays . sort ( B ) ; int maxSubarraySum = findMaxSubarraySumUtil ( A , B , n , m ) ; if ( maxSubarraySum == Integer . MIN_VALUE ) { System . out . println ( " Maximum ▁ subarray ▁ sum ▁ cant ▁ be ▁ found " ) ; } else { System . out . println ( " The ▁ Maximum ▁ subarray ▁ sum ▁ = ▁ " + maxSubarraySum ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 3 , 4 , 5 , - 4 , 6 } ; int B [ ] = { 1 , 8 , 5 } ; int n = A . length ; int m = B . length ; findMaxSubarraySum ( A , B , n , m ) ; } }
import java . util . * ; class GFG { static int findMaxSubarraySum ( int A [ ] , int B [ ] ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < B . length ; i ++ ) { m . put ( B [ i ] , 1 ) ; } int max_so_far = Integer . MIN_VALUE ; int currmax = 0 ; for ( int i = 0 ; i < A . length ; i ++ ) { if ( currmax < 0 || ( m . containsKey ( A [ i ] ) && m . get ( A [ i ] ) == 1 ) ) { currmax = 0 ; continue ; } currmax = Math . max ( A [ i ] , A [ i ] + currmax ) ; if ( max_so_far < currmax ) { max_so_far = currmax ; } } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 4 , 5 , - 4 , 6 } ; int b [ ] = { 1 , 8 , 5 } ; System . out . println ( findMaxSubarraySum ( a , b ) ) ; } }
import java . io . * ; class GFG { static int findMaxSum ( int [ ] arr , int n ) { int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int prefix_sum = arr [ i ] ; for ( int j = 0 ; j < i ; j ++ ) prefix_sum += arr [ j ] ; int suffix_sum = arr [ i ] ; for ( int j = n - 1 ; j > i ; j -- ) suffix_sum += arr [ j ] ; if ( prefix_sum == suffix_sum ) res = Math . max ( res , prefix_sum ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 5 , 3 , 1 , 2 , 6 , - 4 , 2 } ; int n = arr . length ; System . out . println ( findMaxSum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void printVector ( Vector < Integer > v , int i ) { for ( int j = i ; j < v . size ( ) ; j ++ ) System . out . print ( v . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } static class Node { int data ; Node left , right ; Node ( int x ) { data = x ; left = right = null ; } } ; static Vector < Integer > path = new Vector < Integer > ( ) ; static void printKPathUtil ( Node root , int k ) { if ( root == null ) return ; path . add ( root . data ) ; printKPathUtil ( root . left , k ) ; printKPathUtil ( root . right , k ) ; int f = 0 ; for ( int j = path . size ( ) - 1 ; j >= 0 ; j -- ) { f += path . get ( j ) ; if ( f == k ) printVector ( path , j ) ; } path . remove ( path . size ( ) - 1 ) ; } static void printKPath ( Node root , int k ) { path = new Vector < Integer > ( ) ; printKPathUtil ( root , k ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 3 ) ; root . left . left = new Node ( 2 ) ; root . left . right = new Node ( 1 ) ; root . left . right . left = new Node ( 1 ) ; root . right = new Node ( - 1 ) ; root . right . left = new Node ( 4 ) ; root . right . left . left = new Node ( 1 ) ; root . right . left . right = new Node ( 2 ) ; root . right . right = new Node ( 5 ) ; root . right . right . right = new Node ( 2 ) ; int k = 5 ; printKPath ( root , k ) ; } }
import java . io . * ; public class GFG { static int findMaxSum ( int [ ] arr , int n ) { int [ ] preSum = new int [ n ] ; int [ ] suffSum = new int [ n ] ; int ans = Integer . MIN_VALUE ; preSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) preSum [ i ] = preSum [ i - 1 ] + arr [ i ] ; suffSum [ n - 1 ] = arr [ n - 1 ] ; if ( preSum [ n - 1 ] == suffSum [ n - 1 ] ) ans = Math . max ( ans , preSum [ n - 1 ] ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffSum [ i ] = suffSum [ i + 1 ] + arr [ i ] ; if ( suffSum [ i ] == preSum [ i ] ) ans = Math . max ( ans , preSum [ i ] ) ; } return ans ; } static public void main ( String [ ] args ) { int [ ] arr = { - 2 , 5 , 3 , 1 , 2 , 6 , - 4 , 2 } ; int n = arr . length ; System . out . println ( findMaxSum ( arr , n ) ) ; } }
import java . lang . Math . * ; import java . util . stream . * ; class GFG { static int findMaxSum ( int arr [ ] , int n ) { int sum = IntStream . of ( arr ) . sum ( ) ; int prefix_sum = 0 , res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum += arr [ i ] ; if ( prefix_sum == sum ) res = Math . max ( res , prefix_sum ) ; sum -= arr [ i ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 5 , 3 , 1 , 2 , 6 , - 4 , 2 } ; int n = arr . length ; System . out . print ( findMaxSum ( arr , n ) ) ; } }
class GFG { static int equilibrium ( int a [ ] , int n ) { if ( n == 1 ) return ( 0 ) ; int [ ] front = new int [ n ] ; int [ ] back = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 ) { front [ i ] = front [ i - 1 ] + a [ i ] ; } else { front [ i ] = a [ i ] ; } } for ( int i = n - 1 ; i > 0 ; i -- ) { if ( i <= n - 2 ) { back [ i ] = back [ i + 1 ] + a [ i ] ; } else { back [ i ] = a [ i ] ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( front [ i ] == back [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { - 7 , 1 , 5 , 2 , - 4 , 3 , 0 } ; int arr_size = arr . length ; System . out . println ( " First ▁ Point ▁ of ▁ equilibrium ▁ " + " is ▁ at ▁ index ▁ " + equilibrium ( arr , arr_size ) ) ; } }
class LeadersInArray { void printLeaders ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { int j ; for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) break ; } if ( j == size ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { LeadersInArray lead = new LeadersInArray ( ) ; int arr [ ] = new int [ ] { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = arr . length ; lead . printLeaders ( arr , n ) ; } }
class LeadersInArray { void printLeaders ( int arr [ ] , int size ) { int max_from_right = arr [ size - 1 ] ; System . out . print ( max_from_right + " ▁ " ) ; for ( int i = size - 2 ; i >= 0 ; i -- ) { if ( max_from_right < arr [ i ] ) { max_from_right = arr [ i ] ; System . out . print ( max_from_right + " ▁ " ) ; } } } public static void main ( String [ ] args ) { LeadersInArray lead = new LeadersInArray ( ) ; int arr [ ] = new int [ ] { 16 , 17 , 4 , 3 , 5 , 2 } ; int n = arr . length ; lead . printLeaders ( arr , n ) ; } }
import java . io . * ; class GFG { static void findMajority ( int arr [ ] , int n ) { int maxCount = 0 ; int index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count > maxCount ) { maxCount = count ; index = i ; } } if ( maxCount > n / 2 ) System . out . println ( arr [ index ] ) ; else System . out . println ( " No ▁ Majority ▁ Element " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 5 , 1 } ; int n = arr . length ; findMajority ( arr , n ) ; } }
import java . io . * ; class Node { int key ; int c = 0 ; Node left , right ; } class GFG { static int ma = 0 ; static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . c = 1 ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) { if ( ma == 0 ) ma = 1 ; return newNode ( key ) ; } if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; else node . c ++ ; ma = Math . max ( ma , node . c ) ; return node ; } static void inorder ( Node root , int s ) { if ( root != null ) { inorder ( root . left , s ) ; if ( root . c > ( s / 2 ) ) System . out . println ( root . key + "NEW_LINE"); inorder ( root . right , s ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 3 , 3 , 2 } ; int size = a . length ; Node root = null ; for ( int i = 0 ; i < size ; i ++ ) { root = insert ( root , a [ i ] ) ; } if ( ma > ( size / 2 ) ) inorder ( root , size ) ; else System . out . println ( "No majority elementNEW_LINE"); } }
import java . util . HashMap ; class MajorityElement { private static void findMajority ( int [ ] arr ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( map . containsKey ( arr [ i ] ) ) { int count = map . get ( arr [ i ] ) + 1 ; if ( count > arr . length / 2 ) { System . out . println ( " Majority ▁ found ▁ : - ▁ " + arr [ i ] ) ; return ; } else map . put ( arr [ i ] , count ) ; } else map . put ( arr [ i ] , 1 ) ; } System . out . println ( " ▁ No ▁ Majority ▁ element " ) ; } public static void main ( String [ ] args ) { int a [ ] = new int [ ] { 2 , 2 , 2 , 2 , 5 , 5 , 2 , 3 , 3 } ; findMajority ( a ) ; } }
import java . io . * ; class Majority { static int _binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = ( low + high ) / 2 ; if ( ( mid == 0 x > arr [ mid - 1 ] ) && ( arr [ mid ] == x ) ) return mid ; else if ( x > arr [ mid ] ) return _binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return _binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } static boolean isMajority ( int arr [ ] , int n , int x ) { int i = _binarySearch ( arr , 0 , n - 1 , x ) ; if ( i == - 1 ) return false ; if ( ( ( i + n / 2 ) <= ( n - 1 ) ) && arr [ i + n / 2 ] == x ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 3 , 10 } ; int n = arr . length ; int x = 3 ; if ( isMajority ( arr , n , x ) == true ) System . out . println ( x + " ▁ appears ▁ more ▁ than ▁ " + n / 2 + " ▁ times ▁ in ▁ arr [ ] " ) ; else System . out . println ( x + " ▁ does ▁ not ▁ appear ▁ more ▁ than ▁ " + n / 2 + " ▁ times ▁ in ▁ arr [ ] " ) ; } }
import java . util . * ; class GFG { static boolean isMajorityElement ( int arr [ ] , int n , int key ) { if ( arr [ n / 2 ] == key ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 3 , 10 } ; int n = arr . length ; int x = 3 ; if ( isMajorityElement ( arr , n , x ) ) System . out . printf ( " % d ▁ appears ▁ more ▁ than ▁ % d ▁ " + " times ▁ in ▁ arr [ ] " , x , n / 2 ) ; else System . out . printf ( " % d ▁ does ▁ not ▁ appear ▁ more ▁ " + " than ▁ % d ▁ times ▁ in ▁ " + " arr [ ] " , x , n / 2 ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static boolean isMajority ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; else mp . put ( a [ i ] , 1 ) ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) if ( x . getValue ( ) >= n / 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 9 , 2 , 2 } ; int n = a . length ; if ( isMajority ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { private static int isPairSum ( int A [ ] , int N , int X ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; if ( A [ i ] + A [ j ] == X ) return true ; if ( A [ i ] + A [ j ] > X ) break ; } } return 0 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 9 , 2 , 8 , 10 , 11 } ; int val = 17 ; System . out . println ( isPairSum ( arr , arr . length , val ) ) ; }
import java . io . * ; class GFG { public static int isPairSum ( int A [ ] , int N , int X ) { int i = 0 ; int j = N - 1 ; while ( i < j ) { if ( A [ i ] + A [ j ] == X ) return 1 ; else if ( A [ i ] + A [ j ] < X ) i ++ ; else j -- ; } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 9 , 2 , 8 , 10 , 11 } ; int val = 17 ; int arrSize = arr . length ; System . out . println ( isPairSum ( arr , arrSize , val ) ) ; } }
import java . util . Stack ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void inorder ( ) { if ( root == null ) return ; Stack < Node > s = new Stack < Node > ( ) ; Node curr = root ; while ( curr != null || s . size ( ) > 0 ) { while ( curr != null ) { s . push ( curr ) ; curr = curr . left ; } curr = s . pop ( ) ; System . out . print ( curr . data + " ▁ " ) ; curr = curr . right ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . inorder ( ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static class INT { int v ; INT ( int a ) { v = a ; } } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static int countSubtreesWithSumX ( Node root , INT count , int x ) { if ( root == null ) return 0 ; int ls = countSubtreesWithSumX ( root . left , count , x ) ; int rs = countSubtreesWithSumX ( root . right , count , x ) ; int sum = ls + rs + root . data ; if ( sum == x ) count . v ++ ; return sum ; } static int countSubtreesWithSumXUtil ( Node root , int x ) { if ( root == null ) return 0 ; INT count = new INT ( 0 ) ; int ls = countSubtreesWithSumX ( root . left , count , x ) ; int rs = countSubtreesWithSumX ( root . right , count , x ) ; if ( ( ls + rs + root . data ) == x ) count . v ++ ; return count . v ; } public static void main ( String args [ ] ) { Node root = getNode ( 5 ) ; root . left = getNode ( - 10 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 9 ) ; root . left . right = getNode ( 8 ) ; root . right . left = getNode ( - 4 ) ; root . right . right = getNode ( 7 ) ; int x = 7 ; System . out . println ( " Count ▁ = ▁ " + countSubtreesWithSumXUtil ( root , x ) ) ; } }
import java . io . * ; class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; } } class GFG { static int count = 0 ; static Node ptr ; int countSubtreesWithSumXUtil ( Node root , int x ) { int l = 0 , r = 0 ; if ( root == null ) return 0 ; l += countSubtreesWithSumXUtil ( root . left , x ) ; r += countSubtreesWithSumXUtil ( root . right , x ) ; if ( l + r + root . data == x ) count ++ ; if ( ptr != root ) return l + root . data + r ; return count ; } public static void main ( String [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( - 10 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 9 ) ; root . left . right = new Node ( 8 ) ; root . right . left = new Node ( - 4 ) ; root . right . right = new Node ( 7 ) ; int x = 7 ; ptr = root ; System . out . println ( " Count ▁ = ▁ " + new GFG ( ) . countSubtreesWithSumXUtil ( root , x ) ) ; } }
class GFG { static int sum = 0 , max_level = Integer . MIN_VALUE ; static class Node { int d ; Node l ; Node r ; } ; static Node createNode ( int d ) { Node node ; node = new Node ( ) ; node . d = d ; node . l = null ; node . r = null ; return node ; } static void sumOfNodesAtMaxDepth ( Node ro , int level ) { if ( ro == null ) return ; if ( level > max_level ) { sum = ro . d ; max_level = level ; } else if ( level == max_level ) { sum = sum + ro . d ; } sumOfNodesAtMaxDepth ( ro . l , level + 1 ) ; sumOfNodesAtMaxDepth ( ro . r , level + 1 ) ; } public static void main ( String [ ] args ) { Node root ; root = createNode ( 1 ) ; root . l = createNode ( 2 ) ; root . r = createNode ( 3 ) ; root . l . l = createNode ( 4 ) ; root . l . r = createNode ( 5 ) ; root . r . l = createNode ( 6 ) ; root . r . r = createNode ( 7 ) ; sumOfNodesAtMaxDepth ( root , 0 ) ; System . out . println ( sum ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } class GfG { public static int sumMaxLevelRec ( Node node , int max ) { if ( node == null ) return 0 ; if ( max == 1 ) return node . data ; return sumMaxLevelRec ( node . left , max - 1 ) + sumMaxLevelRec ( node . right , max - 1 ) ; } public static int maxDepth ( Node node ) { if ( node == null ) return 0 ; return 1 + Math . max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) ; } public static int sumMaxLevel ( Node root ) { int MaxDepth = maxDepth ( root ) ; return sumMaxLevelRec ( root , MaxDepth ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; System . out . println ( sumMaxLevel ( root ) ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static int evenOddLevelDifference ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( level % 2 == 0 ) evenSum += temp . data ; else oddSum += temp . data ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; size -- ; } } return ( oddSum - evenSum ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; root . left . right . left = new Node ( 3 ) ; root . right . right = new Node ( 8 ) ; root . right . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 7 ) ; System . out . println ( " diffence ▁ between ▁ sums ▁ is ▁ " + evenOddLevelDifference ( root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root ; int getLevelDiff ( Node node ) { if ( node == null ) return 0 ; return node . data - getLevelDiff ( node . left ) - getLevelDiff ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 3 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 7 ) ; System . out . println ( tree . getLevelDiff ( tree . root ) + " ▁ is ▁ the ▁ required ▁ difference " ) ; } }
import java . util . ArrayList ; import java . util . Stack ; public class MaxSpiralSum { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; static int maxSum ( ArrayList < Integer > arr ) { int max_ending_here = Integer . MIN_VALUE ; int max_so_far = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { if ( max_ending_here < 0 ) max_ending_here = arr . get ( i ) ; else max_ending_here += arr . get ( i ) ; max_so_far = Math . max ( max_so_far , max_ending_here ) ; } return max_so_far ; } public static int maxSpiralSum ( Node root ) { if ( root == null ) return 0 ; Stack < Node > s1 = new Stack < > ( ) ; Stack < Node > s2 = new Stack < > ( ) ; ArrayList < Integer > arr = new ArrayList < > ( ) ; s1 . push ( root ) ; while ( ! s1 . isEmpty ( ) || ! s2 . isEmpty ( ) ) { while ( ! s1 . isEmpty ( ) ) { Node temp = s1 . pop ( ) ; arr . add ( temp . data ) ; if ( temp . right != null ) s2 . push ( temp . right ) ; if ( temp . left != null ) s2 . push ( temp . left ) ; } while ( ! s2 . isEmpty ( ) ) { Node temp = s2 . pop ( ) ; arr . add ( temp . data ) ; if ( temp . left != null ) s1 . push ( temp . left ) ; if ( temp . right != null ) s1 . push ( temp . right ) ; } } return maxSum ( arr ) ; } public static void main ( String args [ ] ) { Node root = new Node ( - 2 ) ; root . left = new Node ( - 3 ) ; root . right = new Node ( 4 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 1 ) ; root . right . left = new Node ( - 2 ) ; root . right . right = new Node ( - 1 ) ; root . left . left . left = new Node ( - 3 ) ; root . right . right . right = new Node ( 2 ) ; System . out . print ( " Maximum ▁ Spiral ▁ Sum ▁ = ▁ " + maxSpiralSum ( root ) ) ; } }
public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static int sum ; static void leafSum ( Node root ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) sum += root . data ; leafSum ( root . left ) ; leafSum ( root . right ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right = new Node ( 3 ) ; root . right . right = new Node ( 7 ) ; root . right . left = new Node ( 6 ) ; root . right . left . right = new Node ( 8 ) ; sum = 0 ; leafSum ( root ) ; System . out . println ( sum ) ; } }
class tNode { int data ; tNode left , right ; tNode ( int item ) { data = item ; left = right = null ; } } class BinaryTree { tNode root ; void MorrisTraversal ( tNode root ) { tNode current , pre ; if ( root == null ) return ; current = root ; while ( current != null ) { if ( current . left == null ) { System . out . print ( current . data + " ▁ " ) ; current = current . right ; } else { pre = current . left ; while ( pre . right != null && pre . right != current ) pre = pre . right ; if ( pre . right == null ) { pre . right = current ; current = current . left ; } else { pre . right = null ; System . out . print ( current . data + " ▁ " ) ; current = current . right ; } } } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new tNode ( 1 ) ; tree . root . left = new tNode ( 2 ) ; tree . root . right = new tNode ( 3 ) ; tree . root . left . left = new tNode ( 4 ) ; tree . root . left . right = new tNode ( 5 ) ; tree . MorrisTraversal ( tree . root ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static int sumOfLeafNodesAtMinLevel ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; Queue < Node > q = new LinkedList < > ( ) ; int sum = 0 ; boolean f = false ; q . add ( root ) ; while ( f == false ) { int nc = q . size ( ) ; while ( nc -- > 0 ) { Node top = q . peek ( ) ; q . remove ( ) ; if ( top . left == null && top . right == null ) { sum += top . data ; f = true ; } else { if ( top . left != null ) q . add ( top . left ) ; if ( top . right != null ) q . add ( top . right ) ; } } } return sum ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; root . right . left = getNode ( 6 ) ; root . right . right = getNode ( 7 ) ; root . left . right . left = getNode ( 8 ) ; root . right . left . right = getNode ( 9 ) ; System . out . println ( " Sum ▁ = ▁ " + sumOfLeafNodesAtMinLevel ( root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; boolean hasPathSum ( Node node , int sum ) { if ( node == null ) return sum == 0 ; return hasPathSum ( node . left , sum - node . data ) || hasPathSum ( node . right , sum - node . data ) ; } public static void main ( String args [ ] ) { int sum = 21 ; 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 . left = new Node ( 2 ) ; if ( tree . haspathSum ( tree . root , sum ) ) System . out . println ( " There ▁ is ▁ a ▁ root ▁ to ▁ leaf ▁ path ▁ with ▁ sum ▁ " + sum ) ; else System . out . println ( " There ▁ is ▁ no ▁ root ▁ to ▁ leaf ▁ path ▁ with ▁ sum ▁ " + sum ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int treePathsSumUtil ( Node node , int val ) { if ( node == null ) return 0 ; val = ( val * 10 + node . data ) ; if ( node . left == null && node . right == null ) return val ; return treePathsSumUtil ( node . left , val ) + treePathsSumUtil ( node . right , val ) ; } int treePathsSum ( Node node ) { return treePathsSumUtil ( node , 0 ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 6 ) ; tree . root . left = new Node ( 3 ) ; tree . root . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 4 ) ; tree . root . left . left = new Node ( 2 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . right . right = new Node ( 4 ) ; tree . root . left . right . left = new Node ( 7 ) ; System . out . print ( " Sum ▁ of ▁ all ▁ paths ▁ is ▁ " + tree . treePathsSum ( tree . root ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int data , Node left , Node right ) { this . data = data ; this . left = left ; this . right = right ; } static Node newNode ( int data ) { return new Node ( data , null , null ) ; } static void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . printf ( " % d ▁ " , node . data ) ; inorder ( node . right ) ; } static Node MergeTrees ( Node t1 , Node t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; t1 . data += t2 . data ; t1 . left = MergeTrees ( t1 . left , t2 . left ) ; t1 . right = MergeTrees ( t1 . right , t2 . right ) ; return t1 ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; root1 . right . right = newNode ( 6 ) ; Node root2 = newNode ( 4 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 7 ) ; root2 . left . left = newNode ( 3 ) ; root2 . right . left = newNode ( 2 ) ; root2 . right . right = newNode ( 6 ) ; Node root3 = MergeTrees ( root1 , root2 ) ; System . out . printf ( "The Merged Binary Tree is:NEW_LINE"); inorder ( root3 ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class snode { Node l , r ; } ; static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . left = new_node . right = null ; return new_node ; } static void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . printf ( " % d ▁ " , node . data ) ; inorder ( node . right ) ; } static Node MergeTrees ( Node t1 , Node t2 ) { if ( t1 == null ) return t2 ; if ( t2 == null ) return t1 ; Stack < snode > s = new Stack < > ( ) ; snode temp = new snode ( ) ; temp . l = t1 ; temp . r = t2 ; s . add ( temp ) ; snode n ; while ( ! s . isEmpty ( ) ) { n = s . peek ( ) ; s . pop ( ) ; if ( n . l == null n . r == null ) continue ; n . l . data += n . r . data ; if ( n . l . left == null ) n . l . left = n . r . left ; else { snode t = new snode ( ) ; t . l = n . l . left ; t . r = n . r . left ; s . add ( t ) ; } if ( n . l . right == null ) n . l . right = n . r . right ; else { snode t = new snode ( ) ; t . l = n . l . right ; t . r = n . r . right ; s . add ( t ) ; } } return t1 ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 1 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 3 ) ; root1 . left . left = newNode ( 4 ) ; root1 . left . right = newNode ( 5 ) ; root1 . right . right = newNode ( 6 ) ; Node root2 = newNode ( 4 ) ; root2 . left = newNode ( 1 ) ; root2 . right = newNode ( 7 ) ; root2 . left . left = newNode ( 3 ) ; root2 . right . left = newNode ( 2 ) ; root2 . right . right = newNode ( 6 ) ; Node root3 = MergeTrees ( root1 , root2 ) ; System . out . printf ( "The Merged Binary Tree is:NEW_LINE"); inorder ( root3 ) ; } }
import java . util . Arrays ; class GFG { static int search ( int arr [ ] , int x , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; } static void printPostOrder ( int in1 [ ] , int pre [ ] , int n ) { int root = search ( in1 , pre [ 0 ] , n ) ; if ( root != 0 ) printPostOrder ( in1 , Arrays . copyOfRange ( pre , 1 , n ) , root ) ; if ( root != n - 1 ) printPostOrder ( Arrays . copyOfRange ( in1 , root + 1 , n ) , Arrays . copyOfRange ( pre , 1 + root , n ) , n - root - 1 ) ; System . out . print ( pre [ 0 ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int in1 [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = in1 . length ; System . out . println ( " Postorder ▁ traversal ▁ " ) ; printPostOrder ( in1 , pre , n ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; Node findLCA ( int n1 , int n2 ) { return findLCA ( root , n1 , n2 ) ; } Node findLCA ( Node node , int n1 , int n2 ) { if ( node == null ) return null ; if ( node . data == n1 node . data == n2 ) return node ; Node left_lca = findLCA ( node . left , n1 , n2 ) ; Node right_lca = findLCA ( node . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) return node ; return ( left_lca != null ) ? left_lca : right_lca ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; System . out . println ( " LCA ( 4 , ▁ 5 ) ▁ = ▁ " + tree . findLCA ( 4 , 5 ) . data ) ; System . out . println ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ " + tree . findLCA ( 4 , 6 ) . data ) ; System . out . println ( " LCA ( 3 , ▁ 4 ) ▁ = ▁ " + tree . findLCA ( 3 , 4 ) . data ) ; System . out . println ( " LCA ( 2 , ▁ 4 ) ▁ = ▁ " + tree . findLCA ( 2 , 4 ) . data ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; static boolean v1 = false , v2 = false ; Node findLCAUtil ( Node node , int n1 , int n2 ) { if ( node == null ) return null ; Node temp = null ; if ( node . data == n1 ) { v1 = true ; temp = node ; } if ( node . data == n2 ) { v2 = true ; temp = node ; } Node left_lca = findLCAUtil ( node . left , n1 , n2 ) ; Node right_lca = findLCAUtil ( node . right , n1 , n2 ) ; if ( temp != null ) return temp ; if ( left_lca != null && right_lca != null ) return node ; return ( left_lca != null ) ? left_lca : right_lca ; } Node findLCA ( int n1 , int n2 ) { v1 = false ; v2 = false ; Node lca = findLCAUtil ( root , n1 , n2 ) ; if ( v1 && v2 ) return lca ; return null ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; Node lca = tree . findLCA ( 4 , 5 ) ; if ( lca != null ) System . out . println ( " LCA ( 4 , ▁ 5 ) ▁ = ▁ " + lca . data ) ; else System . out . println ( " Keys ▁ are ▁ not ▁ present " ) ; lca = tree . findLCA ( 4 , 10 ) ; if ( lca != null ) System . out . println ( " LCA ( 4 , ▁ 10 ) ▁ = ▁ " + lca . data ) ; else System . out . println ( " Keys ▁ are ▁ not ▁ present " ) ; } }
import java . util . HashMap ; import java . util . Map ; class Node { int key ; Node left , right , parent ; Node ( int key ) { this . key = key ; left = right = parent = null ; } } class BinaryTree { Node root , n1 , n2 , lca ; Node insert ( Node node , int key ) { if ( node == null ) return new Node ( key ) ; if ( key < node . key ) { node . left = insert ( node . left , key ) ; node . left . parent = node ; } else if ( key > node . key ) { node . right = insert ( node . right , key ) ; node . right . parent = node ; } return node ; } int depth ( Node node ) { int d = - 1 ; while ( node != null ) { ++ d ; node = node . parent ; } return d ; } Node LCA ( Node n1 , Node n2 ) { int d1 = depth ( n1 ) , d2 = depth ( n2 ) ; int diff = d1 - d2 ; if ( diff < 0 ) { Node temp = n1 ; n1 = n2 ; n2 = temp ; diff = - diff ; } while ( diff -- != 0 ) n1 = n1 . parent ; while ( n1 != null && n2 != null ) { if ( n1 == n2 ) return n1 ; n1 = n1 . parent ; n2 = n2 . parent ; } return null ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = tree . insert ( tree . root , 20 ) ; tree . root = tree . insert ( tree . root , 8 ) ; tree . root = tree . insert ( tree . root , 22 ) ; tree . root = tree . insert ( tree . root , 4 ) ; tree . root = tree . insert ( tree . root , 12 ) ; tree . root = tree . insert ( tree . root , 10 ) ; tree . root = tree . insert ( tree . root , 14 ) ; tree . n1 = tree . root . left . right . left ; tree . n2 = tree . root . right ; tree . lca = tree . LCA ( tree . n1 , tree . n2 ) ; System . out . println ( " LCA ▁ of ▁ " + tree . n1 . key + " ▁ and ▁ " + tree . n2 . key + " ▁ is ▁ " + tree . lca . key ) ; } }
import java . util . * ; public class GFG { static int maxn = 100005 ; static int left ( int i ) { return ( 2 * i + 1 ) ; } static int right ( int i ) { return 2 * i + 2 ; } static Vector < Integer > [ ] g = new Vector [ maxn ] ; static int [ ] level = new int [ maxn ] ; static Vector < Integer > e = new Vector < > ( ) ; static Vector < Integer > l = new Vector < > ( ) ; static int [ ] h = new int [ maxn ] ; static int [ ] st = new int [ 5 * maxn ] ; static void add_edge ( int u , int v ) { g [ u ] . add ( v ) ; g [ v ] . add ( u ) ; } static void levelling ( int src ) { for ( int i = 0 ; i < ( g [ src ] . size ( ) ) ; i ++ ) { int des = g [ src ] . get ( i ) ; if ( level [ des ] != 0 ) { level [ des ] = level [ src ] + 1 ; leveling ( des ) ; } } } static boolean [ ] visited = new boolean [ maxn ] ; static void dfs ( int src ) { e . add ( src ) ; visited [ src ] = true ; for ( int i = 0 ; i < ( g [ src ] ) . size ( ) ; i ++ ) { int des = g [ src ] . get ( i ) ; if ( ! visited [ des ] ) { dfs ( des ) ; e . add ( src ) ; } } } static void setting_l ( int n ) { for ( int i = 0 ; i < e . size ( ) ; i ++ ) l . add ( level [ e . get ( i ) ] ) ; } static void setting_h ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) h [ i ] = - 1 ; for ( int i = 0 ; i < e . size ( ) ; i ++ ) { if ( h [ e . get ( i ) ] == - 1 ) h [ e . get ( i ) ] = i ; } } static int RMQ ( int ss , int se , int qs , int qe , int i ) { if ( ss > se ) return - 1 ; if ( se < qs qe < ss ) return - 1 ; if ( qs <= ss && se <= qe ) return st [ i ] ; int mid = ( ss + se ) / 2 ; int st = RMQ ( ss , mid , qs , qe , left ( i ) ) ; int en = RMQ ( mid + 1 , se , qs , qe , right ( i ) ) ; if ( st != - 1 && en != - 1 ) { if ( l . get ( st ) < l . get ( en ) ) return st ; return en ; } else if ( st != - 1 ) return st - 2 ; else if ( en != - 1 ) return en - 1 ; return 0 ; } static void SegmentTreeConstruction ( int ss , int se , int i ) { if ( ss > se ) return ; if ( ss == se ) { st [ i ] = ss ; return ; } int mid = ( ss + se ) / 2 ; SegmentTreeConstruction ( ss , mid , left ( i ) ) ; SegmentTreeConstruction ( mid + 1 , se , right ( i ) ) ; if ( l . get ( st [ left ( i ) ] ) < l . get ( st [ right ( i ) ] ) ) st [ i ] = st [ left ( i ) ] ; else st [ i ] = st [ right ( i ) ] ; } static int LCA ( int x , int y ) { if ( h [ x ] > h [ y ] ) { int t = x ; x = y ; y = t ; } return e . get ( RMQ ( 0 , l . size ( ) - 1 , h [ x ] , h [ y ] , 0 ) ) ; } public static void main ( String [ ] args ) { int n = 15 , q = 5 ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 1 , 4 ) ; add_edge ( 3 , 5 ) ; add_edge ( 4 , 6 ) ; add_edge ( 5 , 7 ) ; add_edge ( 5 , 8 ) ; add_edge ( 5 , 9 ) ; add_edge ( 7 , 10 ) ; add_edge ( 7 , 11 ) ; add_edge ( 7 , 12 ) ; add_edge ( 9 , 13 ) ; add_edge ( 9 , 14 ) ; add_edge ( 12 , 15 ) ; level [ 1 ] = 1 ; leveling ( 1 ) ; dfs ( 1 ) ; setting_l ( n ) ; setting_h ( n ) ; SegmentTreeConstruction ( 0 , l . size ( ) - 1 , 0 ) ; System . out . print ( LCA ( 10 , 15 ) + "NEW_LINE"); System . out . print ( LCA ( 11 , 14 ) + "NEW_LINE"); } }
import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { static Node root ; static Node findLCA ( Node root , int n1 , int n2 ) { if ( root == null ) return null ; if ( root . data == n1 root . data == n2 ) return root ; Node left_lca = findLCA ( root . left , n1 , n2 ) ; Node right_lca = findLCA ( root . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) return root ; return ( left_lca != null ) ? left_lca : right_lca ; } static boolean printAncestors ( Node root , int target ) { if ( root == null ) return false ; if ( root . data == target ) { System . out . print ( root . data + " ▁ " ) ; return true ; } if ( printAncestors ( root . left , target ) || printAncestors ( root . right , target ) ) { System . out . print ( root . data + " ▁ " ) ; return true ; } return false ; } static boolean findCommonNodes ( Node root , int first , int second ) { Node LCA = findLCA ( root , first , second ) ; if ( LCA == null ) return false ; printAncestors ( root , LCA . data ) ; return true ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . right . left . left = new Node ( 9 ) ; tree . root . right . left . right = new Node ( 10 ) ; if ( findCommonNodes ( root , 9 , 7 ) == false ) System . out . println ( " No ▁ Common ▁ nodes " ) ; } }
import java . util . * ; class Node { Node left , right ; int data ; Node ( int item ) { data = item ; left = right = null ; } } class St_class { int st ; int stt [ ] = new int [ 10000 ] ; } class BinaryTree { Node root ; int v = 9 ; int euler [ ] = new int [ 2 * v - 1 ] ; int level [ ] = new int [ 2 * v - 1 ] ; int f_occur [ ] = new int [ 2 * v - 1 ] ; int fill ; St_class sc = new St_class ( ) ; int Log2 ( int x ) { int ans = 0 ; int y = x >>= 1 ; while ( y -- != 0 ) ans ++ ; return ans ; } int swap ( int a , int b ) { return a ; } int RMQUtil ( int index , int ss , int se , int qs , int qe , St_class st ) { if ( qs <= ss && qe >= se ) return st . stt [ index ] ; else if ( se < qs ss > qe ) return - 1 ; int mid = ( ss + se ) / 2 ; int q1 = RMQUtil ( 2 * index + 1 , ss , mid , qs , qe , st ) ; int q2 = RMQUtil ( 2 * index + 2 , mid + 1 , se , qs , qe , st ) ; if ( q1 == - 1 ) return q2 ; else if ( q2 == - 1 ) return q1 ; return ( level [ q1 ] < level [ q2 ] ) ? q1 : q2 ; } int RMQ ( St_class st , int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . println ( " Invalid ▁ input " ) ; return - 1 ; } return RMQUtil ( 0 , 0 , n - 1 , qs , qe , st ) ; } void constructSTUtil ( int si , int ss , int se , int arr [ ] , St_class st ) { if ( ss == se ) st . stt [ si ] = ss ; else { int mid = ( ss + se ) / 2 ; constructSTUtil ( si * 2 + 1 , ss , mid , arr , st ) ; constructSTUtil ( si * 2 + 2 , mid + 1 , se , arr , st ) ; if ( arr [ st . stt [ 2 * si + 1 ] ] < arr [ st . stt [ 2 * si + 2 ] ] ) st . stt [ si ] = st . stt [ 2 * si + 1 ] ; else st . stt [ si ] = st . stt [ 2 * si + 2 ] ; } } int constructST ( int arr [ ] , int n ) { int x = Log2 ( n ) + 1 ; int max_size = 2 * ( 1 << x ) - 1 ; sc . stt = new int [ max_size ] ; constructSTUtil ( 0 , 0 , n - 1 , arr , sc ) ; return sc . st ; } void eulerTour ( Node node , int l ) { if ( node != null ) { euler [ fill ] = node . data ; level [ fill ] = l ; fill ++ ; if ( f_occur [ node . data ] == - 1 ) f_occur [ node . data ] = fill - 1 ; if ( node . left != null ) { eulerTour ( node . left , l + 1 ) ; euler [ fill ] = node . data ; level [ fill ] = l ; fill ++ ; } if ( node . right != null ) { eulerTour ( node . right , l + 1 ) ; euler [ fill ] = node . data ; level [ fill ] = l ; fill ++ ; } } } int findLCA ( Node node , int u , int v ) { Arrays . fill ( f_occur , - 1 ) ; fill = 0 ; eulerTour ( root , 0 ) ; sc . st = constructST ( level , 2 * v - 1 ) ; if ( f_occur [ u ] > f_occur [ v ] ) u = swap ( u , u = v ) ; int qs = f_occur [ u ] ; int qe = f_occur [ v ] ; int index = RMQ ( sc , 2 * v - 1 , qs , qe ) ; return euler [ index ] ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . right . left = new Node ( 8 ) ; tree . root . left . right . right = new Node ( 9 ) ; int u = 4 , v = 9 ; System . out . println ( " The ▁ LCA ▁ of ▁ node ▁ " + u + " ▁ and ▁ " + v + " ▁ is ▁ " + tree . findLCA ( tree . root , u , v ) ) ; } }
public class PrintPost { static int preIndex = 0 ; void printPost ( int [ ] in , int [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return ; int inIndex = search ( in , inStrt , inEnd , pre [ preIndex ++ ] ) ; printPost ( in , pre , inStrt , inIndex - 1 ) ; printPost ( in , pre , inIndex + 1 , inEnd ) ; System . out . print ( in [ inIndex ] + " ▁ " ) ; } int search ( int [ ] in , int startIn , int endIn , int data ) { int i = 0 ; for ( i = startIn ; i < endIn ; i ++ ) if ( in [ i ] == data ) return i ; return i ; } public static void main ( String ars [ ] ) { int in [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int len = in . length ; PrintPost tree = new PrintPost ( ) ; tree . printPost ( in , pre , 0 , len - 1 ) ; } }
class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } }
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class Res { int r = Integer . MIN_VALUE ; } public class BinaryTree { Node root ; int maxDiffUtil ( Node t , Res res ) { if ( t == null ) return Integer . MAX_VALUE ; if ( t . left == null && t . right == null ) return t . key ; int val = Math . min ( maxDiffUtil ( t . left , res ) , maxDiffUtil ( t . right , res ) ) ; res . r = Math . max ( res . r , t . key - val ) ; return Math . min ( val , t . key ) ; } int maxDiff ( Node root ) { Res res = new Res ( ) ; maxDiffUtil ( root , res ) ; return res . r ; } void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " " ) ; inorder ( root . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 3 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 4 ) ; tree . root . left . right . right = new Node ( 7 ) ; tree . root . right = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; tree . root . right . right . left = new Node ( 13 ) ; System . out . println ( " Maximum ▁ difference ▁ between ▁ a ▁ node ▁ and " + " ▁ its ▁ ancestor ▁ is ▁ : ▁ " + tree . maxDiff ( tree . root ) ) ; } }
import java . util . ArrayList ; public class PrintCommonPath { static boolean v1 = false , v2 = false ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } static Node findLCAUtil ( Node node , int n1 , int n2 ) { if ( node == null ) return null ; Node temp = null ; if ( node . data == n1 ) { v1 = true ; temp = node ; } if ( node . data == n2 ) { v2 = true ; temp = node ; } Node left_lca = findLCAUtil ( node . left , n1 , n2 ) ; Node right_lca = findLCAUtil ( node . right , n1 , n2 ) ; if ( temp != null ) return temp ; if ( left_lca != null && right_lca != null ) return node ; return ( left_lca != null ) ? left_lca : right_lca ; } static boolean find ( Node root , int k ) { if ( root == null ) return false ; if ( root . data == k || find ( root . left , k ) || find ( root . right , k ) ) return true ; return false ; } static Node findLCA ( Node root , int n1 , int n2 ) { Node lca = findLCAUtil ( root , n1 , n2 ) ; if ( v1 && v2 || v1 && find ( lca , n2 ) || v2 && find ( lca , n1 ) ) return lca ; return null ; } static boolean hasPath ( Node root , ArrayList < Integer > arr , int x ) { if ( root == null ) return false ; arr . add ( root . data ) ; if ( root . data == x ) return true ; if ( hasPath ( root . left , arr , x ) || hasPath ( root . right , arr , x ) ) return true ; arr . remove ( arr . size ( ) - 1 ) ; return false ; } static void printCommonPath ( Node root , int n1 , int n2 ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; Node lca = findLCA ( root , n1 , n2 ) ; if ( lca != null ) { if ( hasPath ( root , arr , lca . data ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) System . out . print ( arr . get ( i ) + " - > " ) ; System . out . print ( arr . get ( arr . size ( ) - 1 ) ) ; } } else System . out . print ( " No ▁ Common ▁ Path " ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . right . left = new Node ( 8 ) ; root . right . left . right = new Node ( 9 ) ; int n1 = 4 , n2 = 8 ; printCommonPath ( root , n1 , n2 ) ; } }
import java . util . Vector ; class GFG { static int cnt ; static void dfs ( Vector < Integer > [ ] g , int u , int parent , int [ ] timeIn , int [ ] timeOut ) { timeIn [ u ] = cnt ++ ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { int v = g [ u ] . get ( i ) ; if ( v != parent ) dfs ( g , v , u , timeIn , timeOut ) ; } timeOut [ u ] = cnt ++ ; } static void preProcess ( int [ ] [ ] edges , int V , int [ ] timeIn , int [ ] timeOut ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] g = new Vector [ V ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < V - 1 ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; g [ u ] . add ( v ) ; g [ v ] . add ( u ) ; } cnt = 0 ; dfs ( g , 0 , - 1 , timeIn , timeOut ) ; } static String isAncestor ( int u , int v , int [ ] timeIn , int [ ] timeOut ) { boolean b = ( timeIn [ u ] <= timeIn [ v ] && timeOut [ v ] <= timeOut [ u ] ) ; return ( b ? " yes " : " no " ) ; } public static void main ( String [ ] args ) { int edges [ ] [ ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 5 } , { 4 , 6 } , { 5 , 7 } } ; int E = edges . length ; int V = E + 1 ; int [ ] timeIn = new int [ V ] ; int [ ] timeOut = new int [ V ] ; preProcess ( edges , V , timeIn , timeOut ) ; int u = 1 ; int v = 6 ; System . out . println ( isAncestor ( u , v , timeIn , timeOut ) ) ; u = 1 ; v = 7 ; System . out . println ( isAncestor ( u , v , timeIn , timeOut ) ) ; } }
import java . util . ArrayList ; public class PrintPath { class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; public static boolean hasPath ( Node root , ArrayList < Integer > arr , int x ) { if ( root == null ) return false ; arr . add ( root . data ) ; if ( root . data == x ) return true ; if ( hasPath ( root . left , arr , x ) || hasPath ( root . right , arr , x ) ) return true ; arr . remove ( arr . size ( ) - 1 ) ; return false ; } public static void printPath ( Node root , int x ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; if ( hasPath ( root , arr , x ) ) { for ( int i = 0 ; i < arr . size ( ) - 1 ; i ++ ) System . out . print ( arr . get ( i ) + " - > " ) ; System . out . print ( arr . get ( arr . size ( ) - 1 ) ) ; } else System . out . print ( " No ▁ Path " ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; int x = 5 ; printPath ( root , x ) ; } }
class Node { int data ; Node left , right , nextRight ; Node ( int item ) { data = item ; left = right = nextRight = null ; } } class BinaryTree { Node root ; boolean printAncestors ( Node node , int target ) { if ( node == null ) return false ; if ( node . data == target ) return true ; if ( printAncestors ( node . left , target ) || printAncestors ( node . right , target ) ) { System . out . print ( node . data + " ▁ " ) ; return true ; } return false ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . printAncestors ( tree . root , 7 ) ; } }
class Solution { static class Node { int data ; Node left , right ; } ; static Node temp = null ; static int k ; static Node kthAncestorDFS ( Node root , int node ) { if ( root == null ) return null ; if ( root . data == node || ( temp = kthAncestorDFS ( root . left , node ) ) != null || ( temp = kthAncestorDFS ( root . right , node ) ) != null ) { if ( k > 0 ) k -- ; else if ( k == 0 ) { System . out . print ( " Kth ▁ ancestor ▁ is : ▁ " + root . data ) ; return null ; } return root ; } return null ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } 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 ) ; k = 2 ; int node = 5 ; Node parent = kthAncestorDFS ( root , node ) ; if ( parent != null ) System . out . println ( " - 1" ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static Vector < Boolean > struc ; static Vector < Integer > data ; static Node root ; static void EncodeSuccinct ( Node root ) { if ( root == null ) { struc . add ( false ) ; return ; } struc . add ( true ) ; data . add ( root . key ) ; EncodeSuccinct ( root . left ) ; EncodeSuccinct ( root . right ) ; } static Node DecodeSuccinct ( ) { if ( struc . size ( ) <= 0 ) return null ; boolean b = struc . get ( 0 ) ; struc . remove ( 0 ) ; if ( b == true ) { int key = data . get ( 0 ) ; data . remove ( 0 ) ; Node root = newNode ( key ) ; root . left = DecodeSuccinct ( ) ; root . right = DecodeSuccinct ( ) ; return root ; } return null ; } static void preorder ( Node root ) { if ( root != null ) { System . out . print ( " key : ▁ " + root . key ) ; if ( root . left != null ) System . out . print ( " ▁ left ▁ child : ▁ " + root . left . key ) ; if ( root . right != null ) System . out . print ( " ▁ right ▁ child : ▁ " + root . right . key ) ; System . out . println ( ) ; preorder ( root . left ) ; preorder ( root . right ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 20 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 40 ) ; root . left . right = newNode ( 50 ) ; root . right . right = newNode ( 70 ) ; System . out . print ( "Given TreeNEW_LINE"); preorder ( root ) ; struc = new Vector < > ( ) ; data = new Vector < > ( ) ; EncodeSuccinct ( root ) ; System . out . print ( " Encoded Tree "); System . out . print ( "Structure ListNEW_LINE"); for ( boolean si : struc ) { if ( si == true ) System . out . print ( 1 + " ▁ " ) ; else System . out . print ( 0 + " ▁ " ) ; } System . out . print ( " Data List "); for ( int di : data ) System . out . print ( di + " ▁ " ) ; Node newroot = DecodeSuccinct ( ) ; System . out . print ( " Preorder traversal " ▁ + ▁ " of decoded tree "); preorder ( newroot ) ; } }
class GfG { static void update ( int arr [ ] , int l , int r , int val ) { arr [ l ] += val ; if ( r + 1 < arr . length ) arr [ r + 1 ] -= val ; } static int getElement ( int arr [ ] , int i ) { int res = 0 ; for ( int j = 0 ; j <= i ; j ++ ) res += arr [ j ] ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . length ; int l = 2 , r = 4 , val = 2 ; update ( arr , l , r , val ) ; int index = 4 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( arr , l , r , val ) ; index = 3 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getElement ( arr , index ) ) ; } }
class GFG { final static int MAX = 1000 ; static int BITree [ ] = new int [ MAX ] ; public static void updateBIT ( int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void constructBITree ( int arr [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( n , i , arr [ i ] ) ; } public static int getSum ( int index ) { int sum = 0 ; index = index + 1 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void update ( int l , int r , int n , int val ) { updateBIT ( n , l , val ) ; updateBIT ( n , r + 1 , - val ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int n = arr . length ; constructBITree ( arr , n ) ; int l = 2 , r = 4 , val = 2 ; update ( l , r , n , val ) ; int index = 4 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; l = 0 ; r = 3 ; val = 4 ; update ( l , r , n , val ) ; index = 3 ; System . out . println ( " Element ▁ at ▁ index ▁ " + index + " ▁ is ▁ " + getSum ( index ) ) ; } }
import java . util . * ; public class PrintPost { static int preIndex = 0 ; void printPost ( int [ ] in , int [ ] pre , int inStrt , int inEnd , HashMap < Integer , Integer > hm ) { if ( inStrt > inEnd ) return ; int inIndex = hm . get ( pre [ preIndex ++ ] ) ; printPost ( in , pre , inStrt , inIndex - 1 , hm ) ; printPost ( in , pre , inIndex + 1 , inEnd , hm ) ; System . out . print ( in [ inIndex ] + " ▁ " ) ; } void printPostMain ( int [ ] in , int [ ] pre ) { int n = pre . length ; HashMap < Integer , Integer > hm = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) hm . put ( in [ i ] , i ) ; printPost ( in , pre , 0 , n - 1 , hm ) ; } public static void main ( String ars [ ] ) { int in [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; PrintPost tree = new PrintPost ( ) ; tree . printPostMain ( in , pre ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root1 , root2 ; boolean isIsomorphic ( Node n1 , Node n2 ) { if ( n1 == null && n2 == null ) return true ; if ( n1 == null n2 == null ) return false ; if ( n1 . data != n2 . data ) return false ; return ( isIsomorphic ( n1 . left , n2 . left ) && isIsomorphic ( n1 . right , n2 . right ) ) || ( isIsomorphic ( n1 . left , n2 . right ) && isIsomorphic ( n1 . right , n2 . left ) ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root1 = new Node ( 1 ) ; tree . root1 . left = new Node ( 2 ) ; tree . root1 . right = new Node ( 3 ) ; tree . root1 . left . left = new Node ( 4 ) ; tree . root1 . left . right = new Node ( 5 ) ; tree . root1 . right . left = new Node ( 6 ) ; tree . root1 . left . right . left = new Node ( 7 ) ; tree . root1 . left . right . right = new Node ( 8 ) ; tree . root2 = new Node ( 1 ) ; tree . root2 . left = new Node ( 3 ) ; tree . root2 . right = new Node ( 2 ) ; tree . root2 . right . left = new Node ( 4 ) ; tree . root2 . right . right = new Node ( 5 ) ; tree . root2 . left . right = new Node ( 6 ) ; tree . root2 . right . right . left = new Node ( 8 ) ; tree . root2 . right . right . right = new Node ( 7 ) ; if ( tree . isIsomorphic ( tree . root1 , tree . root2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int fastPow ( int N , int K ) { if ( K == 0 ) return 1 ; int temp = fastPow ( N , K / 2 ) ; if ( K % 2 == 0 ) return temp * temp ; else return N * temp * temp ; } static int countWays ( int N , int K ) { return K * fastPow ( K - 1 , N - 1 ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 3 ; System . out . println ( countWays ( N , K ) ) ; } }
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return ( newNode ) ; } static Node deleteLeaves ( Node root , int x ) { if ( root == null ) return null ; root . left = deleteLeaves ( root . left , x ) ; root . right = deleteLeaves ( root . right , x ) ; if ( root . data == x && root . left == null && root . right == null ) { return null ; } return root ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 1 ) ; root . right . right = newNode ( 3 ) ; root . right . right . left = newNode ( 3 ) ; root . right . right . right = newNode ( 3 ) ; deleteLeaves ( root , 3 ) ; System . out . print ( " Inorder ▁ traversal ▁ after ▁ deletion ▁ : ▁ " ) ; inorder ( root ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void _deleteTree ( ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . poll ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } void deleteTree ( ) { _deleteTree ( ) ; root = null ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 15 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 20 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . right . left = new Node ( 16 ) ; tree . root . right . right = new Node ( 25 ) ; tree . deleteTree ( ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; public int size ( ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < Node > ( ) ; q . offer ( root ) ; int count = 1 ; while ( ! q . isEmpty ( ) ) { Node tmp = q . poll ( ) ; if ( tmp != null ) { if ( tmp . left != null ) { count ++ ; q . offer ( tmp . left ) ; } if ( tmp . right != null ) { count ++ ; q . offer ( tmp . right ) ; } } } return count ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ size ▁ of ▁ binary ▁ tree " + " ▁ is ▁ : ▁ " + tree . size ( ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int maxDepth ( Node node ) { if ( node == null ) return 0 ; else { int lDepth = maxDepth ( node . left ) ; int rDepth = maxDepth ( node . right ) ; if ( lDepth > rDepth ) return ( lDepth + 1 ) ; else return ( rDepth + 1 ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Height ▁ of ▁ tree ▁ is ▁ : ▁ " + tree . maxDepth ( tree . root ) ) ; } }
import java . util . * ; class Solution { static class INT { int data ; INT ( int d ) { data = d ; } } static void findPostOrderUtil ( int pre [ ] , int n , int minval , int maxval , INT preIndex ) { if ( preIndex . data == n ) return ; if ( pre [ preIndex . data ] < minval pre [ preIndex . data ] > maxval ) { return ; } int val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; System . out . print ( val + " ▁ " ) ; } static void findPostOrder ( int pre [ ] , int n ) { INT preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , Integer . MIN_VALUE , Integer . MAX_VALUE , preIndex ) ; } public static void main ( String args [ ] ) { int pre [ ] = { 40 , 30 , 35 , 80 , 100 } ; int n = pre . length ; findPostOrder ( pre , n ) ; } }
class GfG { static class Node { int data ; Node left ; Node right ; } static int heightOfTreeUtil ( Node root , boolean isEven ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) { if ( isEven == true ) return 1 ; else return 0 ; } int left = heightOfTreeUtil ( root . left , ! isEven ) ; int right = heightOfTreeUtil ( root . right , ! isEven ) ; if ( left == 0 && right == 0 ) return 0 ; return ( 1 + Math . max ( left , right ) ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int heightOfTree ( Node root ) { return heightOfTreeUtil ( root , false ) ; } 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 . left . right . left = newNode ( 6 ) ; System . out . println ( " Height ▁ of ▁ tree ▁ is ▁ " + heightOfTree ( root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; boolean isBalanced ( Node node ) { int lh ; int rh ; if ( node == null ) return true ; lh = height ( node . left ) ; rh = height ( node . right ) ; if ( Math . abs ( lh - rh ) <= 1 && isBalanced ( node . left ) && isBalanced ( node . right ) ) return true ; return false ; } int height ( Node node ) { if ( node == null ) return 0 ; return 1 + Math . max ( height ( node . left ) , height ( node . right ) ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . left . left = new Node ( 8 ) ; if ( tree . isBalanced ( tree . root ) ) System . out . println ( " Tree ▁ is ▁ balanced " ) ; else System . out . println ( " Tree ▁ is ▁ not ▁ balanced " ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static boolean isLeaf ( Node node ) { return ( node . left != null && node . left . right == node && node . right != null && node . right . left == node ) ; } static int maxDepth ( Node node ) { if ( node == null ) return 0 ; if ( isLeaf ( node ) ) return 1 ; return 1 + Math . max ( maxDepth ( node . left ) , maxDepth ( node . right ) ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . left . left . left = new Node ( 6 ) ; Node L1 = root . left . left . left ; Node L2 = root . left . right ; Node L3 = root . right ; L1 . right = L2 ; L2 . right = L3 ; L3 . right = L1 ; L3 . left = L2 ; L2 . left = L1 ; L1 . left = L3 ; System . out . println ( " Height ▁ of ▁ tree ▁ is ▁ " + maxDepth ( root ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static int height ( Node node ) { if ( node == null ) return 0 ; return ( 1 + Math . max ( height ( node . left ) , height ( node . right ) ) ) ; } int diameter ( Node root ) { if ( root == null ) return 0 ; int lheight = height ( root . left ) ; int rheight = height ( root . right ) ; int ldiameter = diameter ( root . left ) ; int rdiameter = diameter ( root . right ) ; return Math . max ( lheight + rheight + 1 , Math . max ( ldiameter , rdiameter ) ) ; } int diameter ( ) { return diameter ( root ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ diameter ▁ of ▁ given ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . diameter ( ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class Height { int h ; } class BinaryTree { Node root ; int diameterOpt ( Node root , Height height ) { Height lh = new Height ( ) , rh = new Height ( ) ; if ( root == null ) { height . h = 0 ; return 0 ; } int ldiameter = diameterOpt ( root . left , lh ) ; int rdiameter = diameterOpt ( root . right , rh ) ; height . h = Math . max ( lh . h , rh . h ) + 1 ; return Math . max ( lh . h + rh . h + 1 , Math . max ( ldiameter , rdiameter ) ) ; } int diameter ( ) { Height height = new Height ( ) ; return diameterOpt ( root , height ) ; } static int height ( Node node ) { if ( node == null ) return 0 ; return ( 1 + Math . max ( height ( node . left ) , height ( node . right ) ) ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ diameter ▁ of ▁ given ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . diameter ( ) ) ; } }
class GfG { static class Node { int data ; Node left , right ; } static class A { int ans = Integer . MIN_VALUE ; } static int height ( Node root , A a ) { if ( root == null ) return 0 ; int left_height = height ( root . left , a ) ; int right_height = height ( root . right , a ) ; a . ans = Math . max ( a . ans , 1 + left_height + right_height ) ; return 1 + Math . max ( left_height , right_height ) ; } static int diameter ( Node root ) { if ( root == null ) return 0 ; A a = new A ( ) ; int height_of_tree = height ( root , a ) ; return a . ans ; } 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 ) ; System . out . println ( " Diameter ▁ is ▁ " + diameter ( root ) ) ; } }
class GfG { static void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { System . out . println ( "1 ▁ 2" ) ; return ; } System . out . println ( " - 1" ) ; return ; } if ( d > 2 * h ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 1 ; i <= h ; i ++ ) System . out . println ( i + " ▁ " + ( i + 1 ) ) ; if ( d > h ) { System . out . println ( "1" + " ▁ " + ( h + 2 ) ) ; for ( int i = h + 2 ; i <= d ; i ++ ) { System . out . println ( i + " ▁ " + ( i + 1 ) ) ; } } for ( int i = d + 1 ; i < n ; i ++ ) { int k = 1 ; if ( d == h ) k = 2 ; System . out . println ( k + " ▁ " + ( i + 1 ) ) ; } } public static void main ( String [ ] args ) { int n = 5 , d = 3 , h = 2 ; constructTree ( n , d , h ) ; } }
import java . util . * ; class GFG { 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 Node getDeepestRightLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node result = null ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; if ( temp . right . left == null && temp . right . right == null ) result = temp . right ; } } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . right = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestRightLeafNode ( root ) ; if ( result != null ) System . out . println ( " Deepest ▁ Right ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else System . out . println ( "No result, right leaf not foundNEW_LINE"); } }
class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return ( temp ) ; } static boolean isleaf ( Node curr_node ) { return ( curr_node . left == null && curr_node . right == null ) ; } static int deepestOddLevelDepthUtil ( Node curr_node , int curr_level ) { if ( curr_node == null ) return 0 ; curr_level += 1 ; if ( curr_level % 2 != 0 && isleaf ( curr_node ) ) return curr_level ; return Math . max ( deepestOddLevelDepthUtil ( curr_node . left , curr_level ) , deepestOddLevelDepthUtil ( curr_node . right , curr_level ) ) ; } static int deepestOddLevelDepth ( Node curr_node ) { return deepestOddLevelDepthUtil ( curr_node , 0 ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 28 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; System . out . println ( deepestOddLevelDepth ( root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int depthOfOddLeafUtil ( Node node , int level ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null && ( level & 1 ) != 0 ) return level ; return Math . max ( depthOfOddLeafUtil ( node . left , level + 1 ) , depthOfOddLeafUtil ( node . right , level + 1 ) ) ; } int depthOfOddLeaf ( Node node ) { int level = 1 , depth = 0 ; return depthOfOddLeafUtil ( node , level ) ; } public static void main ( String args [ ] ) { int k = 45 ; BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . left . right = new Node ( 7 ) ; tree . root . right . right . right = new Node ( 8 ) ; tree . root . right . left . right . left = new Node ( 9 ) ; tree . root . right . right . right . right = new Node ( 10 ) ; tree . root . right . right . right . right . left = new Node ( 11 ) ; System . out . println ( tree . depthOfOddLeaf ( tree . root ) + " ▁ is ▁ the ▁ required ▁ depth " ) ; } }
import java . util . * ; class GFG { 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 int maxOddLevelDepth ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int result = Integer . MAX_VALUE ; int level = 0 ; while ( ! q . isEmpty ( ) ) { int size = q . size ( ) ; level += 1 ; while ( size > 0 ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null && temp . right == null && ( level % 2 != 0 ) ) { result = level ; } if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } size -= 1 ; } } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; root . right . right . right . right . left = newNode ( 11 ) ; int result = maxOddLevelDepth ( root ) ; if ( result == Integer . MAX_VALUE ) System . out . println ( " No ▁ leaf ▁ node ▁ at ▁ odd ▁ level " ) ; else { System . out . print ( result ) ; System . out . println ( " ▁ is ▁ the ▁ required ▁ depth ▁ " ) ; } } }
class GFG { static class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = null ; right = null ; } } static int maxLevel = - 1 ; static int res = - 1 ; static void find ( Node root , int level ) { if ( root != null ) { find ( root . left , ++ level ) ; if ( level > maxLevel ) { res = root . data ; maxLevel = level ; } find ( root . right , level ) ; } } static int deepestNode ( Node root ) { int res = - 1 ; int maxLevel = - 1 ; find ( root , 0 ) ; return res ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; System . out . println ( deepestNode ( root ) ) ; } }
class GFG { static class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = null ; right = null ; } } ; static int height ( Node root ) { if ( root == null ) return 0 ; int leftHt = height ( root . left ) ; int rightHt = height ( root . right ) ; return Math . max ( leftHt , rightHt ) + 1 ; } static void deepestNode ( Node root , int levels ) { if ( root == null ) return ; if ( levels == 1 ) System . out . print ( root . data + " ▁ " ) ; else if ( levels > 1 ) { deepestNode ( root . left , levels - 1 ) ; deepestNode ( root . right , levels - 1 ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . left = new Node ( 5 ) ; root . right . right = new Node ( 6 ) ; root . right . left . right = new Node ( 7 ) ; root . right . right . right = new Node ( 8 ) ; root . right . left . right . left = new Node ( 9 ) ; int levels = height ( root ) ; deepestNode ( root , levels ) ; } }
import java . util . * ; class GFG { 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 Node getDeepestLeftLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node result = null ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) result = temp . left ; } if ( temp . right != null ) q . add ( temp . right ) ; } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestLeftLeafNode ( root ) ; if ( result != null ) System . out . println ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else System . out . println ( " No ▁ result , ▁ " + " left ▁ leaf ▁ not ▁ found " ) ; } }
import java . util . Vector ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; void preOrder ( Node node ) { if ( node != null ) { System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } } Vector < Node > getTrees ( int arr [ ] , int start , int end ) { Vector < Node > trees = new Vector < Node > ( ) ; if ( start > end ) { trees . add ( null ) ; return trees ; } for ( int i = start ; i <= end ; i ++ ) { Vector < Node > ltrees = getTrees ( arr , start , i - 1 ) ; Vector < Node > rtrees = getTrees ( arr , i + 1 , end ) ; for ( int j = 0 ; j < ltrees . size ( ) ; j ++ ) { for ( int k = 0 ; k < rtrees . size ( ) ; k ++ ) { Node node = new Node ( arr [ i ] ) ; node . left = ltrees . get ( j ) ; node . right = rtrees . get ( k ) ; trees . add ( node ) ; } } } return trees ; } public static void main ( String args [ ] ) { int in [ ] = { 4 , 5 , 7 } ; int n = in . length ; BinaryTree tree = new BinaryTree ( ) ; Vector < Node > trees = tree . getTrees ( in , 0 , n - 1 ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ different ▁ " + " ▁ binary ▁ trees ▁ are : " ) ; for ( int i = 0 ; i < trees . size ( ) ; i ++ ) { tree . preOrder ( trees . get ( i ) ) ; System . out . println ( " " ) ; } } }
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class Level { int maxlevel = 0 ; } class BinaryTree { Node root ; Node result ; void deepestLeftLeafUtil ( Node node , int lvl , Level level , boolean isLeft ) { if ( node == null ) return ; if ( isLeft != false && node . left == null && node . right == null && lvl > level . maxlevel ) { result = node ; level . maxlevel = lvl ; } deepestLeftLeafUtil ( node . left , lvl + 1 , level , true ) ; deepestLeftLeafUtil ( node . right , lvl + 1 , level , false ) ; } void deepestLeftLeaf ( Node node ) { Level level = new Level ( ) ; deepestLeftLeafUtil ( node , 0 , level , false ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . left . right = new Node ( 7 ) ; tree . root . right . right . right = new Node ( 8 ) ; tree . root . right . left . right . left = new Node ( 9 ) ; tree . root . right . right . right . right = new Node ( 10 ) ; tree . deepestLeftLeaf ( tree . root ) ; if ( tree . result != null ) System . out . println ( " The ▁ deepest ▁ left ▁ child " + " ▁ is ▁ " + tree . result . data ) ; else System . out . println ( " There ▁ is ▁ no ▁ left ▁ leaf ▁ in " + " ▁ the ▁ given ▁ tree " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static class qItem { Node node ; int depth ; public qItem ( Node node , int depth ) { this . node = node ; this . depth = depth ; } } static int minDepth ( Node root ) { if ( root == null ) return 0 ; Queue < qItem > q = new LinkedList < > ( ) ; qItem qi = new qItem ( root , 1 ) ; q . add ( qi ) ; while ( q . isEmpty ( ) == false ) { qi = q . peek ( ) ; q . remove ( ) ; Node node = qi . node ; int depth = qi . depth ; if ( node . left == null && node . right == null ) return depth ; if ( node . left != null ) { qi . node = node . left ; qi . depth = depth + 1 ; q . add ( qi ) ; } if ( node . right != null ) { qi . node = node . right ; qi . depth = depth + 1 ; q . add ( qi ) ; } } return 0 ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } 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 ) ; System . out . println ( minDepth ( root ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class MinimumTreeHeight { Node root ; int minimumDepth ( ) { return minimumDepth ( root , 0 ) ; } int minimumDepth ( Node root , int level ) { if ( root == null ) return level ; level ++ ; return Math . min ( minimumDepth ( root . left , level ) , minimumDepth ( root . right , level ) ) ; } public static void main ( String args [ ] ) { MinimumTreeHeight tree = new MinimumTreeHeight ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ minimum ▁ depth ▁ of ▁ " + " binary ▁ tree ▁ is ▁ : ▁ " + tree . minimumDepth ( ) ) ; } }
class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static void replaceNode ( Node node , int level ) { if ( node == null ) return ; node . data = level ; replaceNode ( node . left , level + 1 ) ; replaceNode ( node . right , level + 1 ) ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String [ ] args ) { Node root = new Node ( ) ; root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; System . out . println ( " Before ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; replaceNode ( root , 0 ) ; System . out . println ( ) ; System . out . println ( " After ▁ Replacing ▁ Nodes " ) ; printInorder ( root ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int getMaxWidth ( Node node ) { int maxWidth = 0 ; int width ; int h = height ( node ) ; int i ; for ( i = 1 ; i <= h ; i ++ ) { width = getWidth ( node , i ) ; if ( width > maxWidth ) maxWidth = width ; } return maxWidth ; } int getWidth ( Node node , int level ) { if ( node == null ) return 0 ; if ( level == 1 ) return 1 ; else if ( level > 1 ) return getWidth ( node . left , level - 1 ) + getWidth ( node . right , level - 1 ) ; return 0 ; } int height ( Node node ) { if ( node == null ) return 0 ; else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; System . out . println ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }
import java . util . LinkedList ; import java . util . Queue ; public class maxwidthusingqueue { static class node { int data ; node left , right ; public node ( int data ) { this . data = data ; } } static int maxwidth ( node root ) { if ( root == null ) return 0 ; int maxwidth = 0 ; Queue < node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { int count = q . size ( ) ; maxwidth = Math . max ( maxwidth , count ) ; while ( count -- > 0 ) { node temp = q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } } } return maxwidth ; } public static void main ( String [ ] args ) { node root = new node ( 1 ) ; root . left = new node ( 2 ) ; root . right = new node ( 3 ) ; root . left . left = new node ( 4 ) ; root . left . right = new node ( 5 ) ; root . right . right = new node ( 8 ) ; root . right . right . left = new node ( 6 ) ; root . right . right . right = new node ( 7 ) ; System . out . println ( " Maximum ▁ width ▁ = ▁ " + maxwidth ( root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int getMaxWidth ( Node node ) { int width ; int h = height ( node ) ; int count [ ] = new int [ 10 ] ; int level = 0 ; getMaxWidthRecur ( node , count , level ) ; return getMax ( count , h ) ; } void getMaxWidthRecur ( Node node , int count [ ] , int level ) { if ( node != null ) { count [ level ] ++ ; getMaxWidthRecur ( node . left , count , level + 1 ) ; getMaxWidthRecur ( node . right , count , level + 1 ) ; } } int height ( Node node ) { if ( node == null ) return 0 ; else { int lHeight = height ( node . left ) ; int rHeight = height ( node . right ) ; return ( lHeight > rHeight ) ? ( lHeight + 1 ) : ( rHeight + 1 ) ; } } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; System . out . println ( " Maximum ▁ width ▁ is ▁ " + tree . getMaxWidth ( tree . root ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int maximum = 0 , minimum = 0 ; static void lengthUtil ( Node root , int curr ) { if ( root == null ) return ; lengthUtil ( root . left , curr - 1 ) ; if ( minimum > curr ) minimum = curr ; if ( maximum < curr ) maximum = curr ; lengthUtil ( root . right , curr + 1 ) ; } static int getLength ( Node root ) { maximum = 0 ; minimum = 0 ; lengthUtil ( root , 0 ) ; return ( Math . abs ( minimum ) + maximum ) + 1 ; } static Node newNode ( int data ) { Node curr = new Node ( ) ; curr . data = data ; curr . left = curr . right = null ; return curr ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . println ( getLength ( root ) ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void fillSet ( Node root , Set < Integer > set , int hd ) { if ( root == null ) return ; fillSet ( root . left , set , hd - 1 ) ; set . add ( hd ) ; fillSet ( root . right , set , hd + 1 ) ; } int verticalWidth ( Node root ) { Set < Integer > set = new HashSet < Integer > ( ) ; fillSet ( root , set , 0 ) ; return set . size ( ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 6 ) ; tree . root . right . right . right = new Node ( 7 ) ; System . out . println ( tree . verticalWidth ( tree . root ) ) ; } }
import java . util . * ; class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static int isSorted ( Node root ) { int prevMax = Integer . MIN_VALUE ; int minval ; int maxval ; int levelSize ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { levelSize = q . size ( ) ; minval = Integer . MAX_VALUE ; maxval = Integer . MIN_VALUE ; while ( levelSize > 0 ) { root = q . peek ( ) ; q . remove ( ) ; levelSize -- ; minval = Math . min ( minval , root . key ) ; maxval = Math . max ( maxval , root . key ) ; if ( root . left != null ) q . add ( root . left ) ; if ( root . right != null ) q . add ( root . right ) ; } if ( minval <= prevMax ) return 0 ; prevMax = maxval ; } return 1 ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 8 ) ; root . left . right . right = newNode ( 9 ) ; root . left . right . left . left = newNode ( 12 ) ; root . left . right . right . right = newNode ( 10 ) ; if ( isSorted ( root ) == 1 ) System . out . println ( " Sorted " ) ; else System . out . println ( " Not ▁ sorted " ) ; } }
import java . util . * ; import java . util . Map . Entry ; class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } class Tree { Node root ; public Tree ( ) { } public Tree ( Node node ) { root = node ; } public void bottomView ( ) { if ( root == null ) return ; int hd = 0 ; Map < Integer , Integer > map = new TreeMap < > ( ) ; Queue < Node > queue = new LinkedList < Node > ( ) ; root . hd = hd ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . remove ( ) ; hd = temp . hd ; map . put ( hd , temp . data ) ; if ( temp . left != null ) { temp . left . hd = hd - 1 ; queue . add ( temp . left ) ; } if ( temp . right != null ) { temp . right . hd = hd + 1 ; queue . add ( temp . right ) ; } } Set < Entry < Integer , Integer > > set = map . entrySet ( ) ; Iterator < Entry < Integer , Integer > > iterator = set . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry < Integer , Integer > me = iterator . next ( ) ; System . out . print ( me . getValue ( ) + " ▁ " ) ; } } } public class BottomView { public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; Tree tree = new Tree ( root ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; tree . bottomView ( ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Node { int data ; int hd ; Node left , right ; public Node ( int key ) { data = key ; hd = Integer . MAX_VALUE ; left = right = null ; } } static void printBottomViewUtil ( Node root , int curr , int hd , TreeMap < Integer , int [ ] > m ) { if ( root == null ) return ; if ( ! m . containsKey ( hd ) ) { m . put ( hd , new int [ ] { root . data , curr } ) ; } else { int [ ] p = m . get ( hd ) ; if ( p [ 1 ] <= curr ) { p [ 1 ] = curr ; p [ 0 ] = root . data ; } m . put ( hd , p ) ; } printBottomViewUtil ( root . left , curr + 1 , hd - 1 , m ) ; printBottomViewUtil ( root . right , curr + 1 , hd + 1 , m ) ; } static void printBottomView ( Node root ) { TreeMap < Integer , int [ ] > m = new TreeMap < > ( ) ; printBottomViewUtil ( root , 0 , 0 , m ) ; for ( int val [ ] : m . values ( ) ) { System . out . print ( val [ 0 ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = new Node ( 20 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 22 ) ; root . left . left = new Node ( 5 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 4 ) ; root . right . right = new Node ( 25 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 14 ) ; System . out . println ( " Bottom ▁ view ▁ of ▁ the ▁ given ▁ binary ▁ tree : " ) ; printBottomView ( root ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; int getLeafCount ( ) { return getLeafCount ( root ) ; } int getLeafCount ( Node node ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return 1 ; else return getLeafCount ( node . left ) + getLeafCount ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " The ▁ leaf ▁ count ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " + tree . getLeafCount ( ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static int getLeafCount ( Node node ) { if ( node == null ) { return 0 ; } Queue < Node > q = new LinkedList < > ( ) ; int count = 0 ; q . add ( node ) ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } if ( temp . left == null && temp . right == null ) { count ++ ; } } return count ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = 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 ) ; System . out . println ( getLeafCount ( root ) ) ; } } }
import java . util . LinkedList ; import java . util . Queue ; import java . io . * ; import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int countLeaves ( Node node ) { if ( node == null ) return 0 ; if ( node . left == null && node . right == null ) return 1 ; return countLeaves ( node . left ) + countLeaves ( node . right ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = 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 ) ; System . out . println ( countLeaves ( root ) ) ; } }
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int countNonleaf ( Node root ) { if ( root == null || ( root . left == null && root . right == null ) ) return 0 ; return 1 + countNonleaf ( root . left ) + countNonleaf ( root . right ) ; } 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 ) ; System . out . println ( countNonleaf ( root ) ) ; } }
import java . util . Queue ; import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; int gethalfCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new LinkedList < Node > ( ) ; queue . add ( root ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . poll ( ) ; if ( temp . left != null && temp . right == null temp . left == null && temp . right != null ) count ++ ; if ( temp . left != null ) queue . add ( temp . left ) ; if ( temp . right != null ) queue . add ( temp . right ) ; } return count ; } public static void main ( String args [ ] ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; System . out . println ( tree_level . gethalfCount ( ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int gethalfCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( ( root . left == null && root . right != null ) || ( root . left != null && root . right == null ) ) res ++ ; res += ( gethalfCount ( root . left ) + gethalfCount ( root . right ) ) ; return res ; } 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 ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; System . out . println ( gethalfCount ( root ) ) ; } }
import java . util . Queue ; import java . util . LinkedList ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = null ; right = null ; } } class BinaryTree { Node root ; int getfullCount ( ) { if ( root == null ) return 0 ; Queue < Node > queue = new LinkedList < Node > ( ) ; queue . add ( root ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { Node temp = queue . poll ( ) ; if ( temp . left != null && temp . right != null ) count ++ ; if ( temp . left != null ) { queue . add ( temp . left ) ; } if ( temp . right != null ) { queue . add ( temp . right ) ; } } return count ; } public static void main ( String args [ ] ) { BinaryTree tree_level = new BinaryTree ( ) ; tree_level . root = new Node ( 2 ) ; tree_level . root . left = new Node ( 7 ) ; tree_level . root . right = new Node ( 5 ) ; tree_level . root . left . right = new Node ( 6 ) ; tree_level . root . left . right . left = new Node ( 1 ) ; tree_level . root . left . right . right = new Node ( 11 ) ; tree_level . root . right . right = new Node ( 9 ) ; tree_level . root . right . right . left = new Node ( 4 ) ; System . out . println ( tree_level . getfullCount ( ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int getfullCount ( Node root ) { if ( root == null ) return 0 ; int res = 0 ; if ( root . left != null && root . right != null ) res ++ ; res += ( getfullCount ( root . left ) + getfullCount ( root . right ) ) ; return res ; } 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 ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; System . out . println ( getfullCount ( root ) ) ; } }
import java . util . LinkedList ; import java . util . Queue ; public class Connect_node_same_level { static class Node { int data ; Node left , right , nextRight ; Node ( int data ) { this . data = data ; left = null ; right = null ; nextRight = null ; } } ; static void connect ( Node root ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { Node p = q . peek ( ) ; q . remove ( ) ; if ( p != null ) { p . nextRight = q . peek ( ) ; if ( p . left != null ) q . add ( p . left ) ; if ( p . right != null ) q . add ( p . right ) ; } else if ( ! q . isEmpty ( ) ) q . add ( null ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 8 ) ; root . right = new Node ( 2 ) ; root . left . left = new Node ( 3 ) ; root . right . right = new Node ( 90 ) ; connect ( root ) ; System . out . println ( "Following are populated nextRight pointers in NEW_LINE" + " the ▁ tree ▁ ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; System . out . println ( " nextRight ▁ of ▁ " + root . data + " ▁ is ▁ " + ( ( root . nextRight != null ) ? root . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . left . data + " ▁ is ▁ " + ( ( root . left . nextRight != null ) ? root . left . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . right . data + " ▁ is ▁ " + ( ( root . right . nextRight != null ) ? root . right . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . left . left . data + " ▁ is ▁ " + ( ( root . left . left . nextRight != null ) ? root . left . left . nextRight . data : - 1 ) ) ; System . out . println ( " nextRight ▁ of ▁ " + root . right . right . data + " ▁ is ▁ " + ( ( root . right . right . nextRight != null ) ? root . right . right . nextRight . data : - 1 ) ) ; } }
class Node { int data ; Node left , right , nextRight ; Node ( int item ) { data = item ; left = right = nextRight = null ; } } class BinaryTree { Node root ; Node getNextRight ( Node p ) { Node temp = p . nextRight ; while ( temp != null ) { if ( temp . left != null ) return temp . left ; if ( temp . right != null ) return temp . right ; temp = temp . nextRight ; } return null ; } void connect ( Node p ) { Node temp = null ; if ( p == null ) return ; p . nextRight = null ; while ( p != null ) { Node q = p ; while ( q != null ) { if ( q . left != null ) { if ( q . right != null ) q . left . nextRight = q . right ; else q . left . nextRight = getNextRight ( q ) ; } if ( q . right != null ) q . right . nextRight = getNextRight ( q ) ; q = q . nextRight ; } if ( p . left != null ) p = p . left ; else if ( p . right != null ) p = p . right ; else p = getNextRight ( p ) ; } } 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 . right . right = new Node ( 90 ) ; tree . connect ( tree . root ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; int e = tree . root . right . right . nextRight != null ? tree . root . right . right . nextRight . data : - 1 ; System . out . println ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " ▁ the ▁ tree ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . right . right . data + " ▁ is ▁ " + e ) ; } }
class Node { int data ; Node left , right , nextRight ; Node ( int item ) { data = item ; left = right = nextRight = null ; } } class BinaryTree { Node root ; void connect ( Node p ) { p . nextRight = null ; connectRecur ( p ) ; } void connectRecur ( Node p ) { if ( p == null ) return ; if ( p . left != null ) p . left . nextRight = p . right ; if ( p . right != null ) p . right . nextRight = ( p . nextRight != null ) ? p . nextRight . left : null ; connectRecur ( p . left ) ; connectRecur ( p . right ) ; } 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 . connect ( tree . root ) ; System . out . println ( " Following ▁ are ▁ populated ▁ nextRight ▁ pointers ▁ in ▁ " + " the ▁ tree " + " ( -1 ▁ is ▁ printed ▁ if ▁ there ▁ is ▁ no ▁ nextRight ) " ) ; int a = tree . root . nextRight != null ? tree . root . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . data + " ▁ is ▁ " + a ) ; int b = tree . root . left . nextRight != null ? tree . root . left . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . data + " ▁ is ▁ " + b ) ; int c = tree . root . right . nextRight != null ? tree . root . right . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . right . data + " ▁ is ▁ " + c ) ; int d = tree . root . left . left . nextRight != null ? tree . root . left . left . nextRight . data : - 1 ; System . out . println ( " nextRight ▁ of ▁ " + tree . root . left . left . data + " ▁ is ▁ " + d ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static int maxNodeLevel ( Node root ) { if ( root == null ) return - 1 ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; int level = 0 ; int max = Integer . MIN_VALUE ; int level_no = 0 ; while ( true ) { int NodeCount = q . size ( ) ; if ( NodeCount == 0 ) break ; if ( NodeCount > max ) { max = NodeCount ; level_no = level ; } while ( NodeCount > 0 ) { Node Node = q . peek ( ) ; q . remove ( ) ; if ( Node . left != null ) q . add ( Node . left ) ; if ( Node . right != null ) q . add ( Node . right ) ; NodeCount -- ; } level ++ ; } return level_no ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 8 ) ; root . left . right . left = newNode ( 5 ) ; System . out . println ( " Level ▁ having ▁ maximum ▁ number ▁ of ▁ Nodes ▁ : ▁ " + maxNodeLevel ( root ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left = null ; Node right = null ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = null ; temp . right = null ; return temp ; } static void largestValueInEachLevel ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; int nc , max ; q . add ( root ) ; while ( true ) { nc = q . size ( ) ; if ( nc == 0 ) break ; max = Integer . MIN_VALUE ; while ( nc != 0 ) { Node front = q . peek ( ) ; q . remove ( ) ; if ( max < front . data ) max = front . data ; if ( front . left != null ) q . add ( front . left ) ; if ( front . right != null ) q . add ( front . right ) ; nc -- ; } System . out . println ( max + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; largestValueInEachLevel ( root ) ; } }
class Node { int data ; Node left , right , next ; Node ( int item ) { data = item ; left = right = next = null ; } } class BinaryTree { Node root ; static Node next = null ; void populateNext ( Node node ) { if ( node != null ) { populateNext ( node . right ) ; node . next = next ; next = node ; populateNext ( node . left ) ; } } 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 ( 12 ) ; tree . root . left . left = new Node ( 3 ) ; tree . populateNext ( tree . root ) ; Node ptr = tree . root . left . left ; while ( ptr != null ) { int print = ptr . next != null ? ptr . next . data : - 1 ; System . out . println ( " Next ▁ of ▁ " + ptr . data + " ▁ is : ▁ " + print ) ; ptr = ptr . next ; } } }
import java . util . Arrays ; class GFG { static int INT_MAX = ( int ) 10e6 ; static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root , int [ ] res , int level ) { if ( root != null ) { printPerLevelMinimum ( root . left , res , level + 1 ) ; if ( root . data < res [ level ] ) res [ level ] = root . data ; printPerLevelMinimum ( root . right , res , level + 1 ) ; } } static void perLevelMinimumUtility ( Node root ) { int n = heightoftree ( root ) , i ; int [ ] res = new int [ n ] ; Arrays . fill ( res , INT_MAX ) ; printPerLevelMinimum ( root , res , 0 ) ; System . out . print ( "Every level minimum isNEW_LINE"); for ( i = 0 ; i < n ; i ++ ) { System . out . print ( " level ▁ " + i + " ▁ min ▁ is ▁ = ▁ " + res [ i ] + "NEW_LINE"); } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; perLevelMinimumUtility ( root ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; int min = Integer . MAX_VALUE ; int level = 0 ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node == null ) { System . out . print ( " level ▁ " + level + " ▁ min ▁ is ▁ = ▁ " + min + "NEW_LINE"); if ( q . isEmpty ( ) ) break ; q . add ( null ) ; level ++ ; min = Integer . MAX_VALUE ; continue ; } if ( min > node . data ) min = node . data ; if ( node . left != null ) { q . add ( node . left ) ; } if ( node . right != null ) { q . add ( node . right ) ; } } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Every ▁ Level ▁ minimum ▁ is " + "NEW_LINE"); printPerLevelMinimum ( root ) ; } }
class Node { int data ; Node left , right ; public Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int getLevelUtil ( Node node , int data , int level ) { if ( node == null ) return 0 ; if ( node . data == data ) return level ; int downlevel = getLevelUtil ( node . left , data , level + 1 ) ; if ( downlevel != 0 ) return downlevel ; downlevel = getLevelUtil ( node . right , data , level + 1 ) ; return downlevel ; } int getLevel ( Node node , int data ) { return getLevelUtil ( node , data , 1 ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 3 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 4 ) ; for ( int x = 1 ; x <= 5 ; x ++ ) { int level = tree . getLevel ( tree . root , x ) ; if ( level != 0 ) System . out . println ( " Level ▁ of ▁ " + x + " ▁ is ▁ " + tree . getLevel ( tree . root , x ) ) ; else System . out . println ( x + " ▁ is ▁ not ▁ present ▁ in ▁ tree " ) ; } } }
import java . io . * ; import java . util . * ; class GFG { static class node { int data ; node left , right ; node ( int data ) { this . data = data ; this . left = this . right = null ; } } static int getLevel ( node root , int data ) { Queue < node > q = new LinkedList < > ( ) ; int level = 1 ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { node temp = q . poll ( ) ; if ( temp == null ) { if ( q . peek ( ) != null ) { q . add ( null ) ; } level += 1 ; } else { if ( temp . data == data ) { return level ; } if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } } } return 0 ; } public static void main ( String [ ] args ) { node root = new node ( 20 ) ; root . left = new node ( 10 ) ; root . right = new node ( 30 ) ; root . left . left = new node ( 5 ) ; root . left . right = new node ( 15 ) ; root . left . right . left = new node ( 12 ) ; root . right . left = new node ( 25 ) ; root . right . right = new node ( 40 ) ; int level = getLevel ( root , 30 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 30 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 30 ▁ not ▁ found " ) ; level = getLevel ( root , 12 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 12 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 12 ▁ not ▁ found " ) ; level = getLevel ( root , 25 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 25 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 25 ▁ not ▁ found " ) ; level = getLevel ( root , 27 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 27 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 27 ▁ not ▁ found " ) ; } }
class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int key ) { Node n = new Node ( ) ; n . key = key ; n . left = null ; n . right = null ; return n ; } static int findMirrorRec ( int target , Node left , Node right ) { if ( left == null right == null ) return 0 ; if ( left . key == target ) return right . key ; if ( right . key == target ) return left . key ; int mirror_val = findMirrorRec ( target , left . left , right . right ) ; if ( mirror_val != 0 ) return mirror_val ; return findMirrorRec ( target , left . right , right . left ) ; } static int findMirror ( Node root , int target ) { if ( root == null ) return 0 ; if ( root . key == target ) return target ; return findMirrorRec ( target , root . left , root . right ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . left . left = newNode ( 4 ) ; root . left . left . right = newNode ( 7 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . left = newNode ( 8 ) ; root . right . left . right = newNode ( 9 ) ; int target = root . left . left . key ; int mirror = findMirror ( root , target ) ; if ( mirror != 0 ) System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ Node ▁ " + mirror ) ; else System . out . println ( " Mirror ▁ of ▁ Node ▁ " + target + " ▁ is ▁ null ▁ " ) ; } }
class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static class string { String str ; } static int maxSize ; static Node maxNode ; static class pair { int first ; String second ; pair ( int a , String b ) { first = a ; second = b ; } } static pair largestSubtreeUtil ( Node root , String str ) { if ( root == null ) return new pair ( 0 , str ) ; String left = " " , right = " " ; pair ls1 = largestSubtreeUtil ( root . left , left ) ; left = ls1 . second ; int ls = ls1 . first ; pair rs1 = largestSubtreeUtil ( root . right , right ) ; right = rs1 . second ; int rs = rs1 . first ; int size = ls + rs + 1 ; if ( left . equals ( right ) ) { if ( size > maxSize ) { maxSize = size ; maxNode = root ; } } str += " | " + left + " | " ; str += " | " + root . data + " | " ; str += " | " + right + " | " ; return new pair ( size , str ) ; } static int largestSubtree ( Node node ) { maxSize = 0 ; largestSubtreeUtil ( node , " " ) ; return maxSize ; } public static void main ( String args [ ] ) { Node root = newNode ( 50 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 60 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 20 ) ; root . right . left = newNode ( 70 ) ; root . right . left . left = newNode ( 65 ) ; root . right . left . right = newNode ( 80 ) ; root . right . right = newNode ( 70 ) ; root . right . right . left = newNode ( 65 ) ; root . right . right . right = newNode ( 80 ) ; maxNode = null ; maxSize = largestSubtree ( root ) ; System . out . println ( " Largest ▁ Subtree ▁ is ▁ rooted ▁ at ▁ node ▁ " + maxNode . data + " and its size is " + maxSize); } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class Count { int count = 0 ; } class BinaryTree { Node root ; Count ct = new Count ( ) ; boolean countSingleRec ( Node node , Count c ) { if ( node == null ) return true ; boolean left = countSingleRec ( node . left , c ) ; boolean right = countSingleRec ( node . right , c ) ; if ( left == false right == false ) return false ; if ( node . left != null && node . data != node . left . data ) return false ; if ( node . right != null && node . data != node . right . data ) return false ; c . count ++ ; return true ; } int countSingle ( ) { return countSingle ( root ) ; } int countSingle ( Node node ) { countSingleRec ( node , ct ) ; return ct . count ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 5 ) ; tree . root . left = new Node ( 4 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . right = new Node ( 5 ) ; System . out . println ( " The ▁ count ▁ of ▁ single ▁ valued ▁ sub ▁ trees ▁ is ▁ : ▁ " + tree . countSingle ( ) ) ; } }
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class Distance { int minDis = Integer . MAX_VALUE ; } class BinaryTree { Node root ; void findLeafDown ( Node root , int lev , Distance minDist ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( lev < ( minDist . minDis ) ) minDist . minDis = lev ; return ; } findLeafDown ( root . left , lev + 1 , minDist ) ; findLeafDown ( root . right , lev + 1 , minDist ) ; } int findThroughParent ( Node root , Node x , Distance minDist ) { if ( root == null ) return - 1 ; if ( root == x ) return 0 ; int l = findThroughParent ( root . left , x , minDist ) ; if ( l != - 1 ) { findLeafDown ( root . right , l + 2 , minDist ) ; return l + 1 ; } int r = findThroughParent ( root . right , x , minDist ) ; if ( r != - 1 ) { findLeafDown ( root . left , r + 2 , minDist ) ; return r + 1 ; } return - 1 ; } int minimumDistance ( Node root , Node x ) { Distance d = new Distance ( ) ; findLeafDown ( x , 0 , d ) ; findThroughParent ( root , x , d ) ; return d . minDis ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 13 ) ; tree . root . right . left = new Node ( 14 ) ; tree . root . right . right = new Node ( 15 ) ; tree . root . right . left . left = new Node ( 21 ) ; tree . root . right . left . right = new Node ( 22 ) ; tree . root . right . right . left = new Node ( 23 ) ; tree . root . right . right . right = new Node ( 24 ) ; tree . root . right . left . left . left = new Node ( 1 ) ; tree . root . right . left . left . right = new Node ( 2 ) ; tree . root . right . left . right . left = new Node ( 3 ) ; tree . root . right . left . right . right = new Node ( 4 ) ; tree . root . right . right . left . left = new Node ( 5 ) ; tree . root . right . right . left . right = new Node ( 6 ) ; tree . root . right . right . right . left = new Node ( 7 ) ; tree . root . right . right . right . right = new Node ( 8 ) ; Node x = tree . root . right ; System . out . println ( " The ▁ closest ▁ leaf ▁ to ▁ node ▁ with ▁ value ▁ " + x . key + " ▁ is ▁ at ▁ a ▁ distance ▁ of ▁ " + tree . minimumDistance ( tree . root , x ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int getMin ( int x , int y ) { return ( x < y ) ? x : y ; } int closestDown ( Node node ) { if ( node == null ) return Integer . MAX_VALUE ; if ( node . left == null && node . right == null ) return 0 ; return 1 + getMin ( closestDown ( node . left ) , closestDown ( node . right ) ) ; } int findClosestUtil ( Node node , char k , Node ancestors [ ] , int index ) { if ( node == null ) return Integer . MAX_VALUE ; if ( node . data == k ) { int res = closestDown ( node ) ; for ( int i = index - 1 ; i >= 0 ; i -- ) res = getMin ( res , index - i + closestDown ( ancestors [ i ] ) ) ; return res ; } ancestors [ index ] = node ; return getMin ( findClosestUtil ( node . left , k , ancestors , index + 1 ) , findClosestUtil ( node . right , k , ancestors , index + 1 ) ) ; } int findClosest ( Node node , char k ) { Node ancestors [ ] = new Node [ 100 ] ; return findClosestUtil ( node , k , ancestors , 0 ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( ' A ' ) ; tree . root . left = new Node ( ' B ' ) ; tree . root . right = new Node ( ' C ' ) ; tree . root . right . left = new Node ( ' E ' ) ; tree . root . right . right = new Node ( ' F ' ) ; tree . root . right . left . left = new Node ( ' G ' ) ; tree . root . right . left . left . left = new Node ( ' I ' ) ; tree . root . right . left . left . right = new Node ( ' J ' ) ; tree . root . right . right . right = new Node ( ' H ' ) ; tree . root . right . right . right . left = new Node ( ' H ' ) ; char k = ' H ' ; System . out . println ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' C ' ; System . out . println ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' E ' ; System . out . println ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; k = ' B ' ; System . out . println ( " Distance ▁ of ▁ the ▁ closest ▁ key ▁ from ▁ " + k + " ▁ is ▁ " + tree . findClosest ( tree . root , k ) ) ; } }
import java . util . * ; class GFG { static class node { int data ; node left ; node right ; node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static boolean iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Queue < node > q = new LinkedList ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { node node = q . peek ( ) ; if ( node . data == x ) return true ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } return false ; } public static void main ( String ags [ ] ) { node NewRoot = null ; node root = new node ( 2 ) ; root . left = new node ( 7 ) ; root . right = new node ( 5 ) ; root . left . right = new node ( 6 ) ; root . left . right . left = new node ( 1 ) ; root . left . right . right = new node ( 11 ) ; root . right . right = new node ( 9 ) ; root . right . right . left = new node ( 4 ) ; System . out . print ( ( iterativeSearch ( root , 6 ) ? "Found " : ▁ " Not Found "));  System . out . print ( ( iterativeSearch ( root , 12 ) ? "Found " : ▁ " Not Found "));  } }
class Node { int data ; Node left , right , next ; Node ( int item ) { data = item ; left = right = next = null ; } } void populateNext ( Node node ) { populateNextRecur ( node , next ) ; } void populateNextRecur ( Node p , Node next_ref ) { if ( p != null ) { populateNextRecur ( p . right , next_ref ) ; p . next = next_ref ; next_ref = p ; populateNextRecur ( p . left , next_ref ) ; } }
import java . util . * ; class GFG { static class node { int data ; node left , right ; } ; static node newNode ( int data ) { node node = new node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean iterativeSearch ( node root , int x ) { if ( root == null ) return false ; Stack < node > nodeStack = new Stack < node > ( ) ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { node node = nodeStack . peek ( ) ; if ( node . data == x ) return true ; nodeStack . pop ( ) ; if ( node . right != null ) nodeStack . push ( node . right ) ; if ( node . left != null ) nodeStack . push ( node . left ) ; } return false ; } public static void main ( String [ ] args ) { node NewRoot = null ; node root = newNode ( 2 ) ; root . left = newNode ( 7 ) ; root . right = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 1 ) ; root . left . right . right = newNode ( 11 ) ; root . right . right = newNode ( 9 ) ; root . right . right . left = newNode ( 4 ) ; if ( iterativeSearch ( root , 6 ) ) System . out . println ( " Found " ) ; else System . out . println ( " Not ▁ Found " ) ; if ( iterativeSearch ( root , 12 ) ) System . out . println ( " Found " ) ; else System . out . println ( " Not ▁ Found " ) ; } }
class GFG { 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 swapEveryKLevelUtil ( Node root , int level , int k ) { if ( root == null || ( root . left == null && root . right == null ) ) return ; if ( ( level + 1 ) % k == 0 ) { Node temp = root . left ; root . left = root . right ; root . right = temp ; } swapEveryKLevelUtil ( root . left , level + 1 , k ) ; swapEveryKLevelUtil ( root . right , level + 1 , k ) ; } static void swapEveryKLevel ( Node root , int k ) { swapEveryKLevelUtil ( root , 1 , k ) ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . right = newNode ( 8 ) ; root . right . left = newNode ( 7 ) ; int k = 2 ; System . out . println ( " Before ▁ swap ▁ node ▁ : " ) ; inorder ( root ) ; swapEveryKLevel ( root , k ) ; System . out . println ( " After swap Node : " );  inorder ( root ) ; } }
import java . util . HashMap ; import java . util . Map ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newnode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void pathCountUtil ( Node node , HashMap < Integer , Integer > m , int path_len ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { if ( ! m . containsKey ( path_len ) ) m . put ( path_len , 0 ) ; m . put ( path_len , m . get ( path_len ) + 1 ) ; return ; } pathCountUtil ( node . left , m , path_len + 1 ) ; pathCountUtil ( node . right , m , path_len + 1 ) ; } static void pathCounts ( Node root ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; pathCountUtil ( root , m , 1 ) ; for ( Map . Entry < Integer , Integer > entry : m . entrySet ( ) ) { System . out . printf ( "%d paths have length %dNEW_LINE", entry . getValue ( ) , entry . getKey ( ) ) ; } } public static void main ( String [ ] args ) { Node root = newnode ( 8 ) ; root . left = newnode ( 5 ) ; root . right = newnode ( 4 ) ; root . left . left = newnode ( 9 ) ; root . left . right = newnode ( 7 ) ; root . right . right = newnode ( 11 ) ; root . right . right . left = newnode ( 3 ) ; pathCounts ( root ) ; } }
import java . util . * ; class GfG { static class Node { Node left , right ; int val ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . val = val ; temp . left = null ; temp . right = null ; return temp ; } static int maxPathLenUtil ( Node root , int prev_val , int prev_len ) { if ( root == null ) return prev_len ; int cur_val = root . val ; if ( cur_val == prev_val + 1 ) { return Math . max ( maxPathLenUtil ( root . left , cur_val , prev_len + 1 ) , maxPathLenUtil ( root . right , cur_val , prev_len + 1 ) ) ; } int newPathLen = Math . max ( maxPathLenUtil ( root . left , cur_val , 1 ) , maxPathLenUtil ( root . right , cur_val , 1 ) ) ; return Math . max ( prev_len , newPathLen ) ; } static int maxConsecutivePathLength ( Node root ) { if ( root == null ) return 0 ; return maxPathLenUtil ( root , root . val - 1 , 0 ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 11 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 13 ) ; root . left . right = newNode ( 12 ) ; root . right . left = newNode ( 13 ) ; root . right . right = newNode ( 8 ) ; System . out . println ( " Maximum ▁ Consecutive ▁ Increasing ▁ Path ▁ Length ▁ is ▁ " + maxConsecutivePathLength ( root ) ) ; } }
class GFG { static int ans ; static class Node { int val ; Node left , right ; } ; static int length ( Node node ) { if ( node == null ) return 0 ; int left = length ( node . left ) ; int right = length ( node . right ) ; int Leftmax = 0 , Rightmax = 0 ; if ( node . left != null && node . left . val == node . val ) Leftmax += left + 1 ; if ( node . right != null && node . right . val == node . val ) Rightmax += right + 1 ; ans = Math . max ( ans , Leftmax + Rightmax ) ; return Math . max ( Leftmax , Rightmax ) ; } static int longestSameValuePath ( Node root ) { ans = 0 ; length ( root ) ; return ans ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 4 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 9 ) ; root . right . right = newNode ( 5 ) ; System . out . print ( longestSameValuePath ( root ) ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node removeShortPathNodesUtil ( Node node , int level , int k ) { if ( node == null ) return null ; node . left = removeShortPathNodesUtil ( node . left , level + 1 , k ) ; node . right = removeShortPathNodesUtil ( node . right , level + 1 , k ) ; if ( node . left == null && node . right == null && level < k ) return null ; return node ; } Node removeShortPathNodes ( Node node , int k ) { int pathLen = 0 ; return removeShortPathNodesUtil ( node , 1 , k ) ; } void printInorder ( Node node ) { if ( node != null ) { printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int k = 4 ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . right . right . left = new Node ( 8 ) ; System . out . println ( " The ▁ inorder ▁ traversal ▁ of ▁ original ▁ tree ▁ is ▁ : ▁ " ) ; tree . printInorder ( tree . root ) ; Node res = tree . removeShortPathNodes ( tree . root , k ) ; System . out . println ( " " ) ; System . out . println ( " The ▁ inorder ▁ traversal ▁ of ▁ modified ▁ tree ▁ is ▁ : ▁ " ) ; tree . printInorder ( res ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Result { int res = 0 ; } class BinaryTree { Node root ; private void longestConsecutiveUtil ( Node root , int curlength , int expected , Result res ) { if ( root == null ) return ; if ( root . data == expected ) curlength ++ ; else curlength = 1 ; res . res = Math . max ( res . res , curlength ) ; longestConsecutiveUtil ( root . left , curlength , root . data + 1 , res ) ; longestConsecutiveUtil ( root . right , curlength , root . data + 1 , res ) ; } int longestConsecutive ( Node root ) { if ( root == null ) return 0 ; Result res = new Result ( ) ; longestConsecutiveUtil ( root , 0 , root . data , res ) ; return res . res ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 6 ) ; tree . root . right = new Node ( 9 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 10 ) ; tree . root . right . right . right = new Node ( 11 ) ; System . out . println ( tree . longestConsecutive ( tree . root ) ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left ; Node right ; } ; static Node newNode ( int key ) { Node node = new Node ( ) ; node . left = null ; node . right = null ; node . key = key ; return node ; } static int maxBends ; static int len ; static void findMaxBendsUtil ( Node node , char dir , int bends , int soFar ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { if ( bends > maxBends ) { maxBends = bends ; len = soFar ; } } else { if ( dir == ' l ' ) { findMaxBendsUtil ( node . left , dir , bends , soFar + 1 ) ; findMaxBendsUtil ( node . right , ' r ' , bends + 1 , soFar + 1 ) ; } else { findMaxBendsUtil ( node . right , dir , bends , soFar + 1 ) ; findMaxBendsUtil ( node . left , ' l ' , bends + 1 , soFar + 1 ) ; } } } static int findMaxBends ( Node node ) { if ( node == null ) return 0 ; len = 0 ; maxBends = - 1 ; int bends = 0 ; if ( node . left != null ) findMaxBendsUtil ( node . left , ' l ' , bends , 1 ) ; if ( node . right != null ) findMaxBendsUtil ( node . right , ' r ' , bends , 1 ) ; len ++ ; return len ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 2 ) ; root . right . left . right = newNode ( 1 ) ; root . right . left . right . left = newNode ( 9 ) ; System . out . print ( findMaxBends ( root ) - 1 ) ; } }
public class Turns_to_reach_another_node { static int Count ; static class Node { Node left , right ; int key ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node findLCA ( Node root , int n1 , int n2 ) { if ( root == null ) return null ; if ( root . key == n1 root . key == n2 ) return root ; Node left_lca = findLCA ( root . left , n1 , n2 ) ; Node right_lca = findLCA ( root . right , n1 , n2 ) ; if ( left_lca != null && right_lca != null ) return root ; return ( left_lca != null ) ? left_lca : right_lca ; } static boolean CountTurn ( Node root , int key , boolean turn ) { if ( root == null ) return false ; if ( root . key == key ) return true ; if ( turn == true ) { if ( CountTurn ( root . left , key , turn ) ) return true ; if ( CountTurn ( root . right , key , ! turn ) ) { Count += 1 ; return true ; } } else { if ( CountTurn ( root . right , key , turn ) ) return true ; if ( CountTurn ( root . left , key , ! turn ) ) { Count += 1 ; return true ; } } return false ; } static int NumberOfTurn ( Node root , int first , int second ) { Node LCA = findLCA ( root , first , second ) ; if ( LCA == null ) return - 1 ; Count = 0 ; if ( LCA . key != first && LCA . key != second ) { if ( CountTurn ( LCA . right , second , false ) || CountTurn ( LCA . left , second , true ) ) ; if ( CountTurn ( LCA . left , first , true ) || CountTurn ( LCA . right , first , false ) ) ; return Count + 1 ; } if ( LCA . key == first ) { CountTurn ( LCA . right , second , false ) ; CountTurn ( LCA . left , second , true ) ; return Count ; } else { CountTurn ( LCA . right , first , false ) ; CountTurn ( LCA . left , first , true ) ; return Count ; } } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . right . left . left = new Node ( 9 ) ; root . right . left . right = new Node ( 10 ) ; int turn = 0 ; if ( ( turn = NumberOfTurn ( root , 5 , 10 ) ) != 0 ) System . out . println ( turn ) ; else System . out . println ( " Not ▁ Possible " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right , abtr ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = node . abtr = null ; return node ; } static Vector < Node > even_ptrs = new Vector < > ( ) ; static Vector < Node > odd_ptrs = new Vector < > ( ) ; static void preorderTraversal ( Node root ) { if ( root == null ) return ; if ( root . data % 2 == 0 ) ( even_ptrs ) . add ( root ) ; else ( odd_ptrs ) . add ( root ) ; preorderTraversal ( root . left ) ; preorderTraversal ( root . right ) ; } static void createLoops ( Node root ) { preorderTraversal ( root ) ; int i ; for ( i = 1 ; i < even_ptrs . size ( ) ; i ++ ) even_ptrs . get ( i - 1 ) . abtr = even_ptrs . get ( i ) ; even_ptrs . get ( i - 1 ) . abtr = even_ptrs . get ( 0 ) ; for ( i = 1 ; i < odd_ptrs . size ( ) ; i ++ ) odd_ptrs . get ( i - 1 ) . abtr = odd_ptrs . get ( i ) ; odd_ptrs . get ( i - 1 ) . abtr = odd_ptrs . get ( 0 ) ; } static void traverseLoop ( Node start ) { Node curr = start ; do { System . out . print ( curr . data + " ▁ " ) ; curr = curr . abtr ; } while ( curr != start ) ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createLoops ( root ) ; System . out . print ( " Odd ▁ nodes : ▁ " ) ; traverseLoop ( root . right ) ; System . out . print ( " Even nodes : "); traverseLoop ( root . left ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . left = null ; temp . right = null ; return temp ; } static boolean isLeaf ( Node t ) { return ( ( t . left == null ) && ( t . right == null ) ) ; } static void findFirstUnmatch ( Node root1 , Node root2 ) { if ( root1 == null root2 == null ) return ; Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; s1 . push ( root1 ) ; s2 . push ( root2 ) ; while ( ! s1 . isEmpty ( ) || ! s2 . isEmpty ( ) ) { if ( s1 . isEmpty ( ) || s2 . isEmpty ( ) ) return ; Node temp1 = s1 . peek ( ) ; s1 . pop ( ) ; while ( temp1 != null && isLeaf ( temp1 ) != true ) { s1 . push ( temp1 . right ) ; s1 . push ( temp1 . left ) ; temp1 = s1 . peek ( ) ; s1 . pop ( ) ; } Node temp2 = s2 . peek ( ) ; s2 . pop ( ) ; while ( temp2 != null && isLeaf ( temp2 ) != true ) { s2 . push ( temp2 . right ) ; s2 . push ( temp2 . left ) ; temp2 = s2 . peek ( ) ; s2 . pop ( ) ; } if ( temp1 != null && temp2 != null ) { if ( temp1 . data != temp2 . data ) { System . out . println ( temp1 . data + " ▁ " + temp2 . data ) ; return ; } } } } public static void main ( String [ ] args ) { Node root1 = newNode ( 5 ) ; root1 . left = newNode ( 2 ) ; root1 . right = newNode ( 7 ) ; root1 . left . left = newNode ( 10 ) ; root1 . left . right = newNode ( 11 ) ; Node root2 = newNode ( 6 ) ; root2 . left = newNode ( 10 ) ; root2 . right = newNode ( 15 ) ; findFirstUnmatch ( root1 , root2 ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static int maxOfLeftElement ( Node root ) { int res = Integer . MIN_VALUE ; if ( root == null ) return res ; if ( root . left != null ) res = root . left . data ; return Math . max ( maxOfLeftElement ( root . left ) , Math . max ( res , maxOfLeftElement ( root . right ) ) ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . println ( maxOfLeftElement ( root ) ) ; } }
import java . util . * ; class GFG { static final int InF = 99999 ; static class number { int no ; int level ; number ( ) { } number ( int n , int l ) { this . no = n ; this . level = l ; } } ; static void findnthnumber ( int n ) { Queue < number > q = new LinkedList < > ( ) ; number r = new number ( 0 , 1 ) ; q . add ( r ) ; while ( ! q . isEmpty ( ) ) { number temp = q . peek ( ) ; q . remove ( ) ; if ( temp . no >= InF temp . no <= - InF ) break ; if ( temp . no == n ) { System . out . print ( " Found ▁ number ▁ n ▁ at ▁ level ▁ " + ( temp . level - 1 ) ) ; break ; } q . add ( new number ( temp . no + temp . level , temp . level + 1 ) ) ; q . add ( new number ( temp . no - temp . level , temp . level + 1 ) ) ; } } public static void main ( String [ ] args ) { findnthnumber ( 13 ) ; } }
import java . util . * ; class GFG { static Vector < Integer > find ( int n ) { Vector < Integer > ans = new Vector < > ( ) ; int sum = 0 ; int i = 1 ; int sign = ( n >= 0 ? 1 : - 1 ) ; n = Math . abs ( n ) ; for ( ; sum < n ; ) { ans . add ( sign * i ) ; sum += i ; i ++ ; } if ( sum > sign * n ) { if ( i % 2 != 0 ) { sum -= n ; if ( sum % 2 != 0 ) { ans . add ( sign * i ) ; sum += i ; i ++ ; } int a = ans . get ( ( sum / 2 ) - 1 ) ; ans . remove ( ( sum / 2 ) - 1 ) ; ans . add ( ( ( sum / 2 ) - 1 ) , a * ( - 1 ) ) ; } else { sum -= n ; if ( sum % 2 != 0 ) { sum -- ; ans . add ( sign * i ) ; ans . add ( sign * - 1 * ( i + 1 ) ) ; } ans . add ( ( sum / 2 ) - 1 , ans . get ( ( sum / 2 ) - 1 ) * - 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 20 ; if ( n == 0 ) System . out . print ( "Minimum number of Steps: 0 Step sequence : 0 "); else { Vector < Integer > a = find ( n ) ; System . out . print ( " Minimum ▁ number ▁ of ▁ Steps : ▁ " + a . size ( ) + " Step sequence : "); for ( int i : a ) System . out . print ( i + " ▁ " ) ; } } }
import java . util . * ; class GFG { static class Node { Node left , right ; int key ; } ; static Node root ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static Node createFactorTree ( Node node_ref , int v ) { ( node_ref ) = newNode ( v ) ; for ( int i = 2 ; i < v / 2 ; i ++ ) { if ( v % i != 0 ) continue ; node_ref . left = createFactorTree ( ( ( node_ref ) . left ) , i ) ; node_ref . right = createFactorTree ( ( ( node_ref ) . right ) , v / i ) ; return node_ref ; } return node_ref ; } static void printLevelOrder ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; System . out . print ( node . key + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } public static void main ( String [ ] args ) { root = null ; root = createFactorTree ( root , val ) ; System . out . println ( " Level ▁ order ▁ traversal ▁ of ▁ " + " constructed ▁ factor ▁ tree " ) ; printLevelOrder ( root ) ; } }
import java . util . Arrays ; class GFG { static int numoffbt ( int arr [ ] , int n ) { int maxvalue = - 2147483647 ; int minvalue = 2147483647 ; for ( int i = 0 ; i < n ; i ++ ) { maxvalue = Math . max ( maxvalue , arr [ i ] ) ; minvalue = Math . min ( minvalue , arr [ i ] ) ; } int mark [ ] = new int [ maxvalue + 2 ] ; int value [ ] = new int [ maxvalue + 2 ] ; Arrays . fill ( mark , 0 ) ; Arrays . fill ( value , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { mark [ arr [ i ] ] = 1 ; value [ arr [ i ] ] = 1 ; } int ans = 0 ; for ( int i = minvalue ; i <= maxvalue ; i ++ ) { if ( mark [ i ] != 0 ) { for ( int j = i + i ; j <= maxvalue && j / i <= i ; j += i ) { if ( mark [ j ] == 0 ) continue ; value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; if ( i != j / i ) value [ j ] = value [ j ] + ( value [ i ] * value [ j / i ] ) ; } } ans += value [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 6 } ; int n = arr . length ; System . out . print ( numoffbt ( arr , n ) ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static class P { int pcount = 0 ; } static int countRec ( Node root , P p ) { if ( root == null ) return 0 ; int c = countRec ( root . left , p ) ; c += countRec ( root . right , p ) ; if ( root . data % 2 == 0 ) c += 1 ; if ( c % 2 != 0 ) ( p . pcount ) ++ ; return c ; } static int countSubtrees ( Node root ) { P p = new P ( ) ; countRec ( root , p ) ; return p . pcount ; } public static void main ( String [ ] args ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 10 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 5 ) ; root . left . right . left = newNode ( 6 ) ; System . out . println ( " Count ▁ = ▁ " + countSubtrees ( root ) ) ; } }
class Solution { static class Node { int data ; Node left , right ; } static Node temp = new Node ( ) ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node leftMostNode ( Node node ) { while ( node != null && node . left != null ) node = node . left ; return node ; } static Node rightMostNode ( Node node ) { while ( node != null && node . right != null ) node = node . right ; return node ; } static Node findInorderRecursive ( Node root , Node x ) { if ( root == null ) return null ; if ( root == x || ( temp = findInorderRecursive ( root . left , x ) ) != null || ( temp = findInorderRecursive ( root . right , x ) ) != null ) { if ( temp != null ) { if ( root . left == temp ) { System . out . print ( " Inorder ▁ Successor ▁ of ▁ " + x . data ) ; System . out . print ( " ▁ is ▁ " + root . data + "NEW_LINE");  return null ; } } return root ; } return null ; } static void inorderSuccesor ( Node root , Node x ) { if ( x . right != null ) { Node inorderSucc = leftMostNode ( x . right ) ; System . out . print ( " Inorder ▁ Successor ▁ of ▁ " + x . data + " ▁ is ▁ " ) ; System . out . print ( inorderSucc . data + "NEW_LINE");  } if ( x . right == null ) { int f = 0 ; Node rightMost = rightMostNode ( root ) ; if ( rightMost == x ) System . out . print ( "No inorder successor! Right most node.NEW_LINE");  else findInorderRecursive ( root , x ) ; } } 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 ) ; inorderSuccesor ( root , root . right ) ; inorderSuccesor ( root , root . left . left ) ; inorderSuccesor ( root , root . right . right ) ; } }
class Node { int key ; Node left , right ; public Node ( int item ) { key = item ; left = right = null ; } } class BinaryTree { Node root ; BinaryTree ( int key ) { root = new Node ( key ) ; } BinaryTree ( ) { root = null ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . left = null ; temp . right = null ; return temp ; } static int findDistance ( Node root , int x ) { if ( root == null ) return - 1 ; int dist = - 1 ; if ( ( root . data == x ) || ( dist = findDistance ( root . left , x ) ) >= 0 || ( dist = findDistance ( root . right , x ) ) >= 0 ) return dist + 1 ; return dist ; } public static void main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 15 ) ; root . left . left = newNode ( 20 ) ; root . left . right = newNode ( 25 ) ; root . left . right . right = newNode ( 45 ) ; root . right . left = newNode ( 30 ) ; root . right . right = newNode ( 35 ) ; System . out . println ( findDistance ( root , 45 ) ) ; } }
public class Right_Sibling { static class Node { int data ; Node left , right , parent ; public Node ( int data , Node parent ) { this . data = data ; left = null ; right = null ; this . parent = parent ; } } ; static Node findRightSibling ( Node node , int level ) { if ( node == null node . parent == null ) return null ; while ( node . parent . right == node || ( node . parent . right == null && node . parent . left == node ) ) { if ( node . parent == null ) return null ; node = node . parent ; level -- ; } node = node . parent . right ; while ( level < 0 ) { if ( node . left != null ) node = node . left ; else if ( node . right != null ) node = node . right ; else break ; level ++ ; } if ( level == 0 ) return node ; return findRightSibling ( node , level ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 , null ) ; root . left = new Node ( 2 , root ) ; root . right = new Node ( 3 , root ) ; root . left . left = new Node ( 4 , root . left ) ; root . left . right = new Node ( 6 , root . left ) ; root . left . left . left = new Node ( 7 , root . left . left ) ; root . left . left . left . left = new Node ( 10 , root . left . left . left ) ; root . left . right . right = new Node ( 9 , root . left . right ) ; root . right . right = new Node ( 5 , root . right ) ; root . right . right . right = new Node ( 8 , root . right . right ) ; root . right . right . right . right = new Node ( 12 , root . right . right . right ) ; System . out . println ( findRightSibling ( root . left . left . left . left , 0 ) . data ) ; } }
import java . util . * ; class GfG { static class Node { Node left , right ; int key ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } static Node nextRightNode ( Node root , int k , int level , int [ ] value ) { if ( root == null ) return null ; if ( root . key == k ) { value [ 0 ] = level ; return null ; } else if ( value [ 0 ] != 0 ) { if ( level == value [ 0 ] ) return root ; } Node leftNode = nextRightNode ( root . left , k , level + 1 , value ) ; if ( leftNode != null ) return leftNode ; return nextRightNode ( root . right , k , level + 1 , value ) ; } static Node nextRightNodeUtil ( Node root , int k ) { int [ ] v = new int [ 1 ] ; v [ 0 ] = 0 ; return nextRightNode ( root , k , 1 , v ) ; } static void test ( Node root , int k ) { Node nr = nextRightNodeUtil ( root , k ) ; if ( nr != null ) System . out . println ( " Next ▁ Right ▁ of ▁ " + k + " ▁ is ▁ " + nr . key ) ; else System . out . println ( " No ▁ next ▁ right ▁ node ▁ found ▁ for ▁ " + k ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 6 ) ; root . right . right = newNode ( 5 ) ; root . left . left = newNode ( 8 ) ; root . left . right = newNode ( 4 ) ; test ( root , 10 ) ; test ( root , 2 ) ; test ( root , 6 ) ; test ( root , 5 ) ; test ( root , 8 ) ; test ( root , 4 ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static int first , second , third ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void threelargest ( Node root ) { if ( root == null ) return ; if ( root . data > first ) { third = second ; second = first ; first = root . data ; } else if ( root . data > second && root . data != first ) { third = second ; second = root . data ; } else if ( root . data > third && root . data != first && root . data != second ) third = root . data ; threelargest ( root . left ) ; threelargest ( root . right ) ; } 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 . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; first = 0 ; second = 0 ; third = 0 ; threelargest ( root ) ; System . out . print ( " three ▁ largest ▁ elements ▁ are ▁ " + first + " ▁ " + second + " ▁ " + third ) ; } }
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; static int findMax ( Node node ) { if ( node == null ) return Integer . MIN_VALUE ; int res = node . data ; int lres = findMax ( node . left ) ; int rres = findMax ( node . right ) ; if ( lres > res ) res = lres ; if ( rres > res ) res = rres ; return res ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 2 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . left . right . left = new Node ( 1 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . right = new Node ( 9 ) ; tree . root . right . right . left = new Node ( 4 ) ; System . out . println ( " Maximum ▁ element ▁ is ▁ " + tree . findMax ( tree . root ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; right = left = null ; } } public class BinaryTree { Node root ; Node head ; Node prev ; public Node extractLeafList ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) { if ( head == null ) { head = root ; prev = root ; } else { prev . right = root ; root . left = prev ; prev = root ; } return null ; } root . left = extractLeafList ( root . left ) ; root . right = extractLeafList ( root . right ) ; return root ; } void inorder ( Node node ) { if ( node == null ) return ; inorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; inorder ( node . right ) ; } public void printDLL ( Node head ) { Node last = null ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; last = head ; head = head . right ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . root . left . left . left = new Node ( 7 ) ; tree . root . left . left . right = new Node ( 8 ) ; tree . root . right . right . left = new Node ( 9 ) ; tree . root . right . right . right = new Node ( 10 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ given ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; tree . extractLeafList ( tree . root ) ; System . out . println ( " " ) ; System . out . println ( " Extracted ▁ double ▁ link ▁ list ▁ is ▁ : ▁ " ) ; tree . printDLL ( tree . head ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ modified ▁ tree ▁ is ▁ : ▁ " ) ; tree . inorder ( tree . root ) ; } }
class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } class InorderSuccessor { Node root ; static class PreviousNode { Node pNode ; PreviousNode ( ) { pNode = null ; } } private void inOrderSuccessorOfBinaryTree ( Node root , PreviousNode pre , int searchNode ) { if ( root . right != null ) inOrderSuccessorOfBinaryTree ( root . right , pre , searchNode ) ; if ( root . data == searchNode ) System . out . println ( " inorder ▁ successor ▁ of ▁ " + searchNode + " ▁ is : ▁ " + ( pre . pNode != null ? pre . pNode . data : " null " ) ) ; pre . pNode = root ; if ( root . left != null ) inOrderSuccessorOfBinaryTree ( root . left , pre , searchNode ) ; } public static void main ( String [ ] args ) { InorderSuccessor tree = new InorderSuccessor ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . right = new Node ( 6 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 3 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 4 ) ; tree . inOrderSuccessorOfBinaryTree ( tree . root , new PreviousNode ( ) , 6 ) ; } }
import java . util . * ; class Graph { static void addEdge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void printGraph ( ArrayList < ArrayList < Integer > > adj ) { for ( int i = 0 ; i < adj . size ( ) ; i ++ ) { System . out . println ( " Adjacency list of vertex " + i); System . out . print ( " head " ) ; for ( int j = 0 ; j < adj . get ( i ) . size ( ) ; j ++ ) { System . out . print ( " ▁ - > ▁ " + adj . get ( i ) . get ( j ) ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int V = 5 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( V ) ; for ( int i = 0 ; i < V ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; addEdge ( adj , 0 , 1 ) ; addEdge ( adj , 0 , 4 ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 1 , 4 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; printGraph ( adj ) ; } }
import java . util . * ; class Graph { HashMap < Integer , TreeSet < Integer > > graph ; static int v ; public Graph ( ) { graph = new HashMap < > ( ) ; for ( int i = 0 ; i < v ; i ++ ) { graph . put ( i , new TreeSet < > ( ) ) ; } } public void addEdge ( int src , int dest ) { graph . get ( src ) . add ( dest ) ; graph . get ( dest ) . add ( src ) ; } public void printGraph ( ) { for ( int i = 0 ; i < v ; i ++ ) { System . out . println ( " Adjacency ▁ list ▁ of ▁ vertex ▁ " + i ) ; Iterator set = graph . get ( i ) . iterator ( ) ; while ( set . hasNext ( ) ) System . out . print ( set . next ( ) + " ▁ " ) ; System . out . println ( ) ; System . out . println ( ) ; } } public void searchEdge ( int src , int dest ) { Iterator set = graph . get ( src ) . iterator ( ) ; if ( graph . get ( src ) . contains ( dest ) ) System . out . println ( " Edge ▁ from ▁ " + src + " ▁ to ▁ " + dest + " ▁ found " ) ; else System . out . println ( " Edge ▁ from ▁ " + src + " ▁ to ▁ " + dest + " ▁ not ▁ found " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { v = 5 ; Graph graph = new Graph ( ) ; graph . addEdge ( 0 , 1 ) ; graph . addEdge ( 0 , 4 ) ; graph . addEdge ( 1 , 2 ) ; graph . addEdge ( 1 , 3 ) ; graph . addEdge ( 1 , 4 ) ; graph . addEdge ( 2 , 3 ) ; graph . addEdge ( 3 , 4 ) ; graph . printGraph ( ) ; graph . searchEdge ( 2 , 1 ) ; graph . searchEdge ( 0 , 3 ) ; } }
import java . util . * ; class GFG { static class Graph { int V ; Vector < Integer > [ ] adj ; @ SuppressWarnings ( " unchecked " ) Graph ( int V ) { this . V = V ; this . adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < > ( ) ; } void addEdge ( int u , int v ) { this . adj [ u ] . add ( v ) ; this . adj [ v ] . add ( u ) ; } boolean DFSUtil ( int v , boolean [ ] visited , int [ ] vDegree , int k ) { visited [ v ] = true ; for ( int i : adj [ v ] ) { if ( vDegree [ v ] < k ) vDegree [ i ] -- ; if ( ! visited [ i ] ) { DFSUtil ( i , visited , vDegree , k ) ; } } return ( vDegree [ v ] < k ) ; } void printKCores ( int k ) { boolean [ ] visited = new boolean [ V ] ; boolean [ ] processed = new boolean [ V ] ; Arrays . fill ( visited , false ) ; Arrays . fill ( processed , false ) ; int mindeg = Integer . MAX_VALUE ; int startvertex = 0 ; int [ ] vDegree = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { vDegree [ i ] = adj [ i ] . size ( ) ; if ( vDegree [ i ] < mindeg ) { mindeg = vDegree [ i ] ; startvertex = i ; } } DFSUtil ( startvertex , visited , vDegree , k ) ; for ( int i = 0 ; i < V ; i ++ ) if ( ! visited [ i ] ) DFSUtil ( i , visited , vDegree , k ) ; System . out . println ( " K - Cores ▁ : ▁ " ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( vDegree [ v ] >= k ) { System . out . printf ( " [ % d ] ", v); for ( int i : adj [ v ] ) if ( vDegree [ i ] >= k ) System . out . printf ( " ▁ - > ▁ % d " , i ) ; } } } } public static void main ( String [ ] args ) { int k = 3 ; Graph g1 = new Graph ( 9 ) ; g1 . addEdge ( 0 , 1 ) ; g1 . addEdge ( 0 , 2 ) ; g1 . addEdge ( 1 , 2 ) ; g1 . addEdge ( 1 , 5 ) ; g1 . addEdge ( 2 , 3 ) ; g1 . addEdge ( 2 , 4 ) ; g1 . addEdge ( 2 , 5 ) ; g1 . addEdge ( 2 , 6 ) ; g1 . addEdge ( 3 , 4 ) ; g1 . addEdge ( 3 , 6 ) ; g1 . addEdge ( 3 , 7 ) ; g1 . addEdge ( 4 , 6 ) ; g1 . addEdge ( 4 , 7 ) ; g1 . addEdge ( 5 , 6 ) ; g1 . addEdge ( 5 , 8 ) ; g1 . addEdge ( 6 , 7 ) ; g1 . addEdge ( 6 , 8 ) ; g1 . printKCores ( k ) ; System . out . println ( ) ; Graph g2 = new Graph ( 13 ) ; g2 . addEdge ( 0 , 1 ) ; g2 . addEdge ( 0 , 2 ) ; g2 . addEdge ( 0 , 3 ) ; g2 . addEdge ( 1 , 4 ) ; g2 . addEdge ( 1 , 5 ) ; g2 . addEdge ( 1 , 6 ) ; g2 . addEdge ( 2 , 7 ) ; g2 . addEdge ( 2 , 8 ) ; g2 . addEdge ( 2 , 9 ) ; g2 . addEdge ( 3 , 10 ) ; g2 . addEdge ( 3 , 11 ) ; g2 . addEdge ( 3 , 12 ) ; g2 . printKCores ( k ) ; } }
import java . util . * ; class GFG { static void printLevels ( Vector < Vector < Integer > > graph , int V , int x ) { int level [ ] = new int [ V ] ; boolean marked [ ] = new boolean [ V ] ; Queue < Integer > que = new LinkedList < Integer > ( ) ; que . add ( x ) ; level [ x ] = 0 ; marked [ x ] = true ; while ( que . size ( ) > 0 ) { x = que . peek ( ) ; que . remove ( ) ; for ( int i = 0 ; i < graph . get ( x ) . size ( ) ; i ++ ) { int b = graph . get ( x ) . get ( i ) ; if ( ! marked [ b ] ) { que . add ( b ) ; level [ b ] = level [ x ] + 1 ; marked [ b ] = true ; } } } System . out . println ( " Nodes " + " ▁ " + " Level " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . println ( " ▁ " + i + " ▁ - - > ▁ " + level [ i ] ) ; } public static void main ( String args [ ] ) { int V = 8 ; Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 0 ) . add ( 1 ) ; graph . get ( 0 ) . add ( 2 ) ; graph . get ( 1 ) . add ( 3 ) ; graph . get ( 1 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; graph . get ( 2 ) . add ( 5 ) ; graph . get ( 2 ) . add ( 6 ) ; graph . get ( 6 ) . add ( 7 ) ; printLevels ( graph , V , 0 ) ; } }
import java . util . * ; class GFG { static void dfs ( int parent , int ans [ ] , Vector < Integer > connectchars [ ] ) { ans [ parent ] = 1 ; for ( int i = 0 ; i < connectchars [ parent ] . size ( ) ; i ++ ) { if ( ans [ connectchars [ parent ] . get ( i ) ] != 1 ) dfs ( connectchars [ parent ] . get ( i ) , ans , connectchars ) ; } } static void printBinaryPalindrome ( int n , int k ) { int [ ] arr = new int [ n ] ; int [ ] ans = new int [ n ] ; Vector < Integer > [ ] connectchars = new Vector [ k ] ; for ( int i = 0 ; i < k ; i ++ ) connectchars [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i % k ; for ( int i = 0 ; i < n / 2 ; i ++ ) { connectchars [ arr [ i ] ] . add ( arr [ n - i - 1 ] ) ; connectchars [ arr [ n - i - 1 ] ] . add ( arr [ i ] ) ; } dfs ( 0 , ans , connectchars ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans [ arr [ i ] ] ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 4 ; printBinaryPalindrome ( n , k ) ; } }
public class NthNodePostOrder { static int flag = 0 ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ; public static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) System . out . print ( root . data ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } }
import java . util . * ; class GFG { public static void dfs ( LinkedList < Integer > list [ ] , int node , int arrival ) { System . out . println ( node ) ; for ( int i = 0 ; i < list [ node ] . size ( ) ; i ++ ) { if ( list [ node ] . get ( i ) != arrival ) dfs ( list , list [ node ] . get ( i ) , node ) ; } } public static void main ( String [ ] args ) { int nodes = 5 ; LinkedList < Integer > list [ ] = new LinkedList [ nodes + 1 ] ; for ( int i = 0 ; i < list . length ; i ++ ) { list [ i ] = new LinkedList < Integer > ( ) ; } list [ 1 ] . add ( 2 ) ; list [ 2 ] . add ( 1 ) ; list [ 1 ] . add ( 3 ) ; list [ 3 ] . add ( 1 ) ; list [ 2 ] . add ( 4 ) ; list [ 4 ] . add ( 2 ) ; list [ 3 ] . add ( 5 ) ; list [ 5 ] . add ( 3 ) ; dfs ( list , 1 , 0 ) ; } }
import java . util . * ; class GFG { static long [ ] count_color = new long [ 2 ] ; static void dfs ( Vector < Integer > adj [ ] , int node , int parent , boolean color ) { count_color [ color == false ? 0 : 1 ] ++ ; for ( int i = 0 ; i < adj [ node ] . size ( ) ; i ++ ) { if ( adj [ node ] . get ( i ) != parent ) dfs ( adj , adj [ node ] . get ( i ) , node , ! color ) ; } } static int findMaxEdges ( Vector < Integer > adj [ ] , int n ) { dfs ( adj , 1 , 0 , false ) ; return ( int ) ( count_color [ 0 ] * count_color [ 1 ] - ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; Vector < Integer > [ ] adj = new Vector [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 1 ] . add ( 2 ) ; adj [ 1 ] . add ( 3 ) ; adj [ 2 ] . add ( 4 ) ; adj [ 3 ] . add ( 5 ) ; System . out . println ( findMaxEdges ( adj , n ) ) ; } }
import java . io . * ; import java . util . * ; class Graph { private static void printPath ( List < Integer > path ) { int size = path . size ( ) ; for ( Integer v : path ) { System . out . print ( v + " ▁ " ) ; } System . out . println ( ) ; } private static boolean isNotVisited ( int x , List < Integer > path ) { int size = path . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) if ( path . get ( i ) == x ) return false ; return true ; } private static void findpaths ( List < List < Integer > > g , int src , int dst , int v ) { Queue < List < Integer > > queue = new LinkedList < > ( ) ; List < Integer > path = new ArrayList < > ( ) ; path . add ( src ) ; queue . offer ( path ) ; while ( ! queue . isEmpty ( ) ) { path = queue . poll ( ) ; int last = path . get ( path . size ( ) - 1 ) ; if ( last == dst ) { printPath ( path ) ; } List < Integer > lastNode = g . get ( last ) ; for ( int i = 0 ; i < lastNode . size ( ) ; i ++ ) { if ( isNotVisited ( lastNode . get ( i ) , path ) ) { List < Integer > newpath = new ArrayList < > ( path ) ; newpath . add ( lastNode . get ( i ) ) ; queue . offer ( newpath ) ; } } } } public static void main ( String [ ] args ) { List < List < Integer > > g = new ArrayList < > ( ) ; int v = 4 ; for ( int i = 0 ; i < 4 ; i ++ ) { g . add ( new ArrayList < > ( ) ) ; } g . get ( 0 ) . add ( 3 ) ; g . get ( 0 ) . add ( 1 ) ; g . get ( 0 ) . add ( 2 ) ; g . get ( 1 ) . add ( 3 ) ; g . get ( 2 ) . add ( 0 ) ; g . get ( 2 ) . add ( 1 ) ; int src = 2 , dst = 3 ; System . out . println ( " path ▁ from ▁ src ▁ " + src + " ▁ to ▁ dst ▁ " + dst + " ▁ are ▁ " ) ; findpaths ( g , src , dst , v ) ; } }
import java . util . LinkedList ; import java . util . Queue ; import java . util . Vector ; class Test { static int minEdgeBFS ( Vector < Integer > edges [ ] , int u , int v , int n ) { Vector < Boolean > visited = new Vector < Boolean > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { visited . addElement ( false ) ; } Vector < Integer > distance = new Vector < Integer > ( n ) ; for ( int i = 0 ; i < n ; i ++ ) { distance . addElement ( 0 ) ; } Queue < Integer > Q = new LinkedList < > ( ) ; distance . setElementAt ( 0 , u ) ; Q . add ( u ) ; visited . setElementAt ( true , u ) ; while ( ! Q . isEmpty ( ) ) { int x = Q . peek ( ) ; Q . poll ( ) ; for ( int i = 0 ; i < edges [ x ] . size ( ) ; i ++ ) { if ( visited . elementAt ( edges [ x ] . get ( i ) ) ) continue ; distance . setElementAt ( distance . get ( x ) + 1 , edges [ x ] . get ( i ) ) ; Q . add ( edges [ x ] . get ( i ) ) ; visited . setElementAt ( true , edges [ x ] . get ( i ) ) ; } } return distance . get ( v ) ; } static void addEdge ( Vector < Integer > edges [ ] , int u , int v ) { edges [ u ] . add ( v ) ; edges [ v ] . add ( u ) ; } public static void main ( String args [ ] ) { int n = 9 ; Vector < Integer > edges [ ] = new Vector [ 9 ] ; for ( int i = 0 ; i < edges . length ; i ++ ) { edges [ i ] = new Vector < > ( ) ; } addEdge ( edges , 0 , 1 ) ; addEdge ( edges , 0 , 7 ) ; addEdge ( edges , 1 , 7 ) ; addEdge ( edges , 1 , 2 ) ; addEdge ( edges , 2 , 3 ) ; addEdge ( edges , 2 , 5 ) ; addEdge ( edges , 2 , 8 ) ; addEdge ( edges , 3 , 4 ) ; addEdge ( edges , 3 , 5 ) ; addEdge ( edges , 4 , 5 ) ; addEdge ( edges , 5 , 6 ) ; addEdge ( edges , 6 , 7 ) ; addEdge ( edges , 7 , 8 ) ; int u = 0 ; int v = 5 ; System . out . println ( minEdgeBFS ( edges , u , v , n ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; pair ( int a , int b ) { first = a ; second = b ; } } static int dfs ( Vector < Vector < pair > > g , pair disRev [ ] , boolean visit [ ] , int u ) { visit [ u ] = true ; int totalRev = 0 ; for ( int i = 0 ; i < g . get ( u ) . size ( ) ; i ++ ) { int v = g . get ( u ) . get ( i ) . first ; if ( ! visit [ v ] ) { disRev [ v ] . first = disRev [ u ] . first + 1 ; disRev [ v ] . second = disRev [ u ] . second ; if ( g . get ( u ) . get ( i ) . second != 0 ) { disRev [ v ] . second = disRev [ u ] . second + 1 ; totalRev ++ ; } totalRev += dfs ( g , disRev , visit , v ) ; } } return totalRev ; } static void printMinEdgeReverseForRootNode ( int edges [ ] [ ] , int e ) { int V = e + 1 ; Vector < Vector < pair > > g = new Vector < Vector < pair > > ( ) ; for ( int i = 0 ; i < V + 1 ; i ++ ) g . add ( new Vector < pair > ( ) ) ; pair disRev [ ] = new pair [ V ] ; for ( int i = 0 ; i < V ; i ++ ) disRev [ i ] = new pair ( 0 , 0 ) ; boolean visit [ ] = new boolean [ V ] ; int u , v ; for ( int i = 0 ; i < e ; i ++ ) { u = edges [ i ] [ 0 ] ; v = edges [ i ] [ 1 ] ; g . get ( u ) . add ( new pair ( v , 0 ) ) ; g . get ( v ) . add ( new pair ( u , 1 ) ) ; } for ( int i = 0 ; i < V ; i ++ ) { visit [ i ] = false ; disRev [ i ] . first = disRev [ i ] . second = 0 ; } int root = 0 ; int totalRev = dfs ( g , disRev , visit , root ) ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < V ; i ++ ) { int edgesToRev = ( totalRev - disRev [ i ] . second ) + ( disRev [ i ] . first - disRev [ i ] . second ) ; if ( edgesToRev < res ) { res = edgesToRev ; root = i ; } } System . out . println ( root + " ▁ " + res ) ; } public static void main ( String args [ ] ) { int edges [ ] [ ] = { { 0 , 1 } , { 2 , 1 } , { 3 , 2 } , { 3 , 4 } , { 5 , 4 } , { 5 , 6 } , { 7 , 6 } } ; int e = edges . length ; printMinEdgeReverseForRootNode ( edges , e ) ; } }
class GFG { static boolean dfs ( int residue , int curStep , int [ ] wt , int [ ] arr , int N , int steps ) { if ( curStep >= steps ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( curStep - 1 < 0 || ( arr [ i ] > residue && arr [ i ] != wt [ curStep - 1 ] ) ) { wt [ curStep ] = arr [ i ] ; if ( dfs ( arr [ i ] - residue , curStep + 1 , wt , arr , N , steps ) ) return true ; } } return false ; } static void printWeightOnScale ( int [ ] arr , int N , int steps ) { int [ ] wt = new int [ steps ] ; if ( dfs ( 0 , 0 , wt , arr , N , steps ) ) { for ( int i = 0 ; i < steps ; i ++ ) System . out . print ( wt [ i ] + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 } ; int N = arr . length ; int steps = 10 ; printWeightOnScale ( arr , N , steps ) ; } }
import java . util . * ; class GFG { static int k ; static void dfs ( Vector < Integer > graph [ ] , boolean visited [ ] , int x ) { for ( int i = 0 ; i < graph [ x ] . size ( ) ; i ++ ) { if ( ! visited [ graph [ x ] . get ( i ) ] ) { ( k ) ++ ; visited [ graph [ x ] . get ( i ) ] = true ; dfs ( graph , visited , graph [ x ] . get ( i ) ) ; } } } static int countNonAccessible ( Vector < Integer > graph [ ] , int N ) { boolean [ ] visited = new boolean [ N * N + N ] ; int ans = 0 ; for ( int i = 1 ; i <= N * N ; i ++ ) { if ( ! visited [ i ] ) { visited [ i ] = true ; int k = 1 ; dfs ( graph , visited , i ) ; ans += k * ( N * N - k ) ; } } return ans ; } static void insertpath ( Vector < Integer > graph [ ] , int N , int x1 , int y1 , int x2 , int y2 ) { int a = ( x1 - 1 ) * N + y1 ; int b = ( x2 - 1 ) * N + y2 ; graph [ a ] . add ( b ) ; graph [ b ] . add ( a ) ; } public static void main ( String args [ ] ) { int N = 2 ; Vector [ ] graph = new Vector [ N * N + 1 ] ; for ( int i = 1 ; i <= N * N ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; insertpath ( graph , N , 1 , 1 , 1 , 2 ) ; insertpath ( graph , N , 1 , 2 , 2 , 2 ) ; System . out . println ( countNonAccessible ( graph , N ) ) ; } }
import java . util . * ; class GFG { static int curMax ; static int dfs ( Vector < Integer > g [ ] , int u , int v ) { int max1 = 0 , max2 = 0 , total = 0 ; for ( int i = 0 ; i < g [ u ] . size ( ) ; i ++ ) { if ( g [ u ] . get ( i ) == v ) continue ; total = Math . max ( total , dfs ( g , g [ u ] . get ( i ) , u ) ) ; if ( curMax > max1 ) { max2 = max1 ; max1 = curMax ; } else max2 = Math . max ( max2 , curMax ) ; } total = Math . max ( total , max1 + max2 ) ; curMax = max1 + 1 ; return total ; } static int maxProductOfTwoPaths ( Vector < Integer > g [ ] , int N ) { int res = Integer . MIN_VALUE ; int path1 , path2 ; for ( int i = 1 ; i < N + 2 ; i ++ ) { for ( int j = 0 ; j < g [ i ] . size ( ) ; j ++ ) { curMax = 0 ; path1 = dfs ( g , g [ i ] . get ( j ) , i ) ; curMax = 0 ; path2 = dfs ( g , i , g [ i ] . get ( j ) ) ; res = Math . max ( res , path1 * path2 ) ; } } return res ; } static void addEdge ( Vector < Integer > g [ ] , int u , int v ) { g [ u ] . add ( v ) ; g [ v ] . add ( u ) ; } public static void main ( String [ ] args ) { int edges [ ] [ ] = { { 1 , 8 } , { 2 , 6 } , { 3 , 1 } , { 5 , 3 } , { 7 , 8 } , { 8 , 4 } , { 8 , 6 } } ; int N = edges . length ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] g = new Vector [ N + 2 ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) addEdge ( g , edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) ; System . out . print ( maxProductOfTwoPaths ( g , N ) + "NEW_LINE"); } }
import java . util . ArrayList ; class Graph { static int res ; static void dfs ( int u , int parent , int totalSum , ArrayList < Integer > [ ] edge , int subtree [ ] ) { int sum = subtree [ u ] ; for ( int i = 0 ; i < edge [ u ] . size ( ) ; i ++ ) { int v = edge [ u ] . get ( i ) ; if ( v != parent ) { dfs ( v , u , totalSum , edge , subtree ) ; sum += subtree [ v ] ; } } subtree [ u ] = sum ; if ( u != 0 && Math . abs ( totalSum - 2 * sum ) < res ) res = Math . abs ( totalSum - 2 * sum ) ; } static int getMinSubtreeSumDifference ( int vertex [ ] , int [ ] [ ] edges , int N ) { int totalSum = 0 ; int [ ] subtree = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { subtree [ i ] = vertex [ i ] ; totalSum += vertex [ i ] ; } @ SuppressWarnings ( " unchecked " ) ArrayList < Integer > [ ] edge = new ArrayList [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { edge [ i ] = new ArrayList < > ( ) ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { edge [ edges [ i ] [ 0 ] ] . add ( edges [ i ] [ 1 ] ) ; edge [ edges [ i ] [ 1 ] ] . add ( edges [ i ] [ 0 ] ) ; } dfs ( 0 , - 1 , totalSum , edge , subtree ) ; return res ; } public static void main ( String [ ] args ) { res = Integer . MAX_VALUE ; int [ ] vertex = { 4 , 2 , 1 , 6 , 3 , 5 , 2 } ; int [ ] [ ] edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 2 , 4 } , { 2 , 5 } , { 3 , 6 } } ; int N = vertex . length ; System . out . println ( getMinSubtreeSumDifference ( vertex , edges , N ) ) ; } }
class Main { public static boolean isStepNum ( int n ) { int prevDigit = - 1 ; while ( n > 0 ) { int curDigit = n % 10 ; if ( prevDigit != - 1 ) { if ( Math . abs ( curDigit - prevDigit ) != 1 ) return false ; } n /= 10 ; prevDigit = curDigit ; } return true ; } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = n ; i <= m ; i ++ ) if ( isStepNum ( i ) ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
import java . util . * ; class Main { public static void dfs ( int n , int m , int stepNum ) { if ( stepNum <= m && stepNum >= n ) System . out . print ( stepNum + " ▁ " ) ; if ( stepNum == 0 stepNum > m ) return ; int lastDigit = stepNum % 10 ; int stepNumA = stepNum * 10 + ( lastDigit - 1 ) ; int stepNumB = stepNum * 10 + ( lastDigit + 1 ) ; if ( lastDigit == 0 ) dfs ( n , m , stepNumB ) ; else if ( lastDigit == 9 ) dfs ( n , m , stepNumA ) ; else { dfs ( n , m , stepNumA ) ; dfs ( n , m , stepNumB ) ; } } public static void displaySteppingNumbers ( int n , int m ) { for ( int i = 0 ; i <= 9 ; i ++ ) dfs ( n , m , i ) ; } public static void main ( String args [ ] ) { int n = 0 , m = 21 ; displaySteppingNumbers ( n , m ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int ROW , COL , count ; static boolean isSafe ( int [ ] [ ] M , int row , int col , boolean [ ] [ ] visited ) { return ( ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ( M [ row ] [ col ] == 1 && ! visited [ row ] [ col ] ) ) ; } static void DFS ( int [ ] [ ] M , int row , int col , boolean [ ] [ ] visited ) { int [ ] rowNbr = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; int [ ] colNbr = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; visited [ row ] [ col ] = true ; for ( int k = 0 ; k < 8 ; k ++ ) { if ( isSafe ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ) { count ++ ; DFS ( M , row + rowNbr [ k ] , col + colNbr [ k ] , visited ) ; } } } static int largestRegion ( int [ ] [ ] M ) { boolean [ ] [ ] visited = new boolean [ ROW ] [ COL ] ; int result = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( M [ i ] [ j ] == 1 && ! visited [ i ] [ j ] ) { count = 1 ; DFS ( M , i , j , visited ) ; result = Math . max ( result , count ) ; } } } return result ; } public static void main ( String args [ ] ) { int M [ ] [ ] = { { 0 , 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 } } ; ROW = 4 ; COL = 5 ; System . out . println ( largestRegion ( M ) ) ; } }
public class Main { public static final int V = 5 ; static int count = 0 ; static void DFS ( int graph [ ] [ ] , boolean marked [ ] , int n , int vert , int start ) { marked [ vert ] = true ; if ( n == 0 ) { marked [ vert ] = false ; if ( graph [ vert ] [ start ] == 1 ) { count ++ ; return ; } else return ; } for ( int i = 0 ; i < V ; i ++ ) if ( ! marked [ i ] && graph [ vert ] [ i ] == 1 ) DFS ( graph , marked , n - 1 , i , start ) ; marked [ vert ] = false ; } static int countCycles ( int graph [ ] [ ] , int n ) { boolean marked [ ] = new boolean [ V ] ; for ( int i = 0 ; i < V - ( n - 1 ) ; i ++ ) { DFS ( graph , marked , n - 1 , i , i ) ; marked [ i ] = true ; } return count / 2 ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 0 } } ; int n = 4 ; System . out . println ( " Total ▁ cycles ▁ of ▁ length ▁ " + n + " ▁ are ▁ " + countCycles ( graph , n ) ) ; } }
class GFG { static final int V = 4 ; static final int INF = 99999 ; static boolean negCyclefloydWarshall ( int graph [ ] [ ] ) { int dist [ ] [ ] = new int [ V ] [ V ] , i , j , k ; for ( i = 0 ; i < V ; i ++ ) for ( j = 0 ; j < V ; j ++ ) dist [ i ] [ j ] = graph [ i ] [ j ] ; for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } for ( i = 0 ; i < V ; i ++ ) if ( dist [ i ] [ i ] < 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , INF , INF } , { INF , 0 , - 1 , INF } , { INF , INF , 0 , - 1 } , { - 1 , INF , INF , 0 } } ; if ( negCyclefloydWarshall ( graph ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean twoColorUtil ( Vector < Integer > [ ] G , int src , int N , int [ ] colorArr ) { colorArr [ src ] = 1 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( src ) ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . poll ( ) ; for ( int v = 0 ; v < G [ u ] . size ( ) ; ++ v ) { if ( colorArr [ G [ u ] . elementAt ( v ) ] == - 1 ) { colorArr [ G [ u ] . elementAt ( v ) ] = 1 - colorArr [ u ] ; q . add ( G [ u ] . elementAt ( v ) ) ; } else if ( colorArr [ G [ u ] . elementAt ( v ) ] == colorArr [ u ] ) return false ; } } return true ; } static boolean twoColor ( Vector < Integer > [ ] G , int N ) { int [ ] colorArr = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; ++ i ) colorArr [ i ] = - 1 ; for ( int i = 1 ; i <= N ; i ++ ) if ( colorArr [ i ] == - 1 ) if ( twoColorUtil ( G , i , N , colorArr ) == false ) return false ; return true ; } static boolean isOddSum ( int [ ] [ ] info , int n , int m ) { Vector < Integer > [ ] G = new Vector [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) G [ i ] = new Vector < > ( ) ; int pseudo = n + 1 ; int pseudo_count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( info [ i ] [ 2 ] % 2 == 1 ) { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( v ) ; G [ v ] . add ( u ) ; } else { int u = info [ i ] [ 0 ] ; int v = info [ i ] [ 1 ] ; G [ u ] . add ( pseudo ) ; G [ pseudo ] . add ( u ) ; G [ v ] . add ( pseudo ) ; G [ pseudo ] . add ( v ) ; pseudo_count ++ ; pseudo ++ ; } } return twoColor ( G , n + pseudo_count ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 3 ; int [ ] [ ] info = { { 1 , 2 , 12 } , { 2 , 3 , 1 } , { 4 , 3 , 1 } , { 4 , 1 , 20 } } ; if ( isOddSum ( info , n , m ) == true ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } }
import java . util . Vector ; class GFG { static boolean isCycleRec ( int v , Vector < Integer > [ ] adj , Vector < Boolean > visited , Vector < Boolean > recur ) { visited . set ( v , true ) ; recur . set ( v , true ) ; for ( int i = 0 ; i < adj [ v ] . size ( ) ; i ++ ) { if ( visited . elementAt ( adj [ v ] . elementAt ( i ) ) == false ) { if ( isCycleRec ( adj [ v ] . elementAt ( i ) , adj , visited , recur ) ) return true ; } else if ( visited . elementAt ( adj [ v ] . elementAt ( i ) ) == true && recur . elementAt ( adj [ v ] . elementAt ( i ) ) == true ) return true ; } recur . set ( v , false ) ; return false ; } @ SuppressWarnings ( " unchecked " ) static boolean isCycle ( int [ ] arr , int n ) { Vector < Integer > [ ] adj = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) if ( i != ( i + arr [ i ] + n ) % n && adj [ i ] != null ) adj [ i ] . add ( ( i + arr [ i ] + n ) % n ) ; Vector < Boolean > visited = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) visited . add ( true ) ; Vector < Boolean > recur = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) recur . add ( true ) ; for ( int i = 0 ; i < n ; i ++ ) if ( visited . elementAt ( i ) == false ) if ( isCycleRec ( i , adj , visited , recur ) ) return true ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , - 1 , 1 , 2 , 2 } ; int n = arr . length ; if ( isCycle ( arr , n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
static int find ( int parent [ ] , int i ) { if ( parent [ i ] == - 1 ) return i ; return find ( parent , parent [ i ] ) ; } static void Union ( int parent [ ] , int x , int y ) { int xset = find ( parent , x ) ; int yset = find ( parent , y ) ; parent [ xset ] = yset ; }
import java . util . * ; class GFG { static int MAX_VERTEX = 101 ; static int [ ] Arr = new int [ MAX_VERTEX ] ; static int [ ] size = new int [ MAX_VERTEX ] ; static void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { Arr [ i ] = i ; size [ i ] = 1 ; } } static int find ( int i ) { while ( Arr [ i ] != i ) { Arr [ i ] = Arr [ Arr [ i ] ] ; i = Arr [ i ] ; } return i ; } static void _union ( int xr , int yr ) { if ( size [ xr ] < size [ yr ] ) { Arr [ xr ] = Arr [ yr ] ; size [ yr ] += size [ xr ] ; } else { Arr [ yr ] = Arr [ xr ] ; size [ xr ] += size [ yr ] ; } } static int isCycle ( Vector < Integer > adj [ ] , int V ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < adj [ i ] . size ( ) ; j ++ ) { int x = find ( i ) ; int y = find ( adj [ i ] . get ( j ) ) ; if ( x == y ) return 1 ; _union ( x , y ) ; } } return 0 ; } public static void main ( String [ ] args ) { int V = 3 ; initialize ( V ) ; Vector < Integer > [ ] adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; adj [ 0 ] . add ( 1 ) ; adj [ 0 ] . add ( 2 ) ; adj [ 1 ] . add ( 2 ) ; if ( isCycle ( adj , V ) == 1 ) System . out . print ( "Graph contains Cycle.NEW_LINE"); else System . out . print ( "Graph does not contain Cycle.NEW_LINE"); } }
import java . io . * ; import java . util . * ; public class GFG { static int solve ( int [ ] A , int n ) { int i , cnt = 0 , j ; int [ ] parent = new int [ n + 1 ] ; int [ ] vis = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { parent [ i ] = - 1 ; vis [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { j = i ; if ( parent [ j ] == - 1 ) { while ( parent [ j ] == - 1 ) { parent [ j ] = i ; j = ( j + A [ j ] + 1 ) % n ; } if ( parent [ j ] == i ) { while ( vis [ j ] == 0 ) { vis [ j ] = 1 ; cnt ++ ; j = ( j + A [ j ] + 1 ) % n ; } } } } return cnt ; } public static void main ( String args [ ] ) { int [ ] A = { 0 , 0 , 0 , 2 } ; int n = A . length ; System . out . print ( solve ( A , n ) ) ; } }
import java . util . * ; class GFG { static class Node { Node left ; int data ; Node right ; } ; static void modifiedLevelOrder ( Node node ) { if ( node == null ) return ; if ( node . left == null && node . right == null ) { System . out . print ( node . data ) ; return ; } Queue < Node > myQueue = new LinkedList < > ( ) ; Stack < Node > myStack = new Stack < > ( ) ; Node temp = null ; int sz ; int ct = 0 ; boolean rightToLeft = false ; myQueue . add ( node ) ; while ( ! myQueue . isEmpty ( ) ) { ct ++ ; sz = myQueue . size ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { temp = myQueue . peek ( ) ; myQueue . remove ( ) ; if ( rightToLeft == false ) System . out . print ( temp . data + " ▁ " ) ; else myStack . push ( temp ) ; if ( temp . left != null ) myQueue . add ( temp . left ) ; if ( temp . right != null ) myQueue . add ( temp . right ) ; } if ( rightToLeft == true ) { while ( ! myStack . isEmpty ( ) ) { temp = myStack . peek ( ) ; myStack . pop ( ) ; System . out . print ( temp . data + " ▁ " ) ; } } if ( ct == 2 ) { rightToLeft = ! rightToLeft ; ct = 0 ; } System . out . print ( "NEW_LINE"); } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } 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 . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }
import java . util . * ; class GFG { static int minnode ( int n , int keyval [ ] , boolean mstset [ ] ) { int mini = Integer . MAX_VALUE ; int mini_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mstset [ i ] == false && keyval [ i ] < mini ) { mini = keyval [ i ] ; mini_index = i ; } } return mini_index ; } static void findcost ( int n , int city [ ] [ ] ) { int parent [ ] = new int [ n ] ; int keyval [ ] = new int [ n ] ; boolean mstset [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { keyval [ i ] = Integer . MAX_VALUE ; mstset [ i ] = false ; } parent [ 0 ] = - 1 ; keyval [ 0 ] = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int u = minnode ( n , keyval , mstset ) ; mstset [ u ] = true ; for ( int v = 0 ; v < n ; v ++ ) { if ( city [ u ] [ v ] > 0 && mstset [ v ] == false && city [ u ] [ v ] < keyval [ v ] ) { keyval [ v ] = city [ u ] [ v ] ; parent [ v ] = u ; } } } int cost = 0 ; for ( int i = 1 ; i < n ; i ++ ) cost += city [ parent [ i ] ] [ i ] ; System . out . println ( cost ) ; } public static void main ( String args [ ] ) { int n1 = 5 ; int city1 [ ] [ ] = { { 0 , 1 , 2 , 3 , 4 } , { 1 , 0 , 5 , 0 , 7 } , { 2 , 5 , 0 , 6 , 0 } , { 3 , 0 , 6 , 0 , 0 } , { 4 , 7 , 0 , 0 , 0 } } ; findcost ( n1 , city1 ) ; int n2 = 6 ; int city2 [ ] [ ] = { { 0 , 1 , 1 , 100 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 0 , 0 , 0 } , { 100 , 0 , 0 , 0 , 2 , 2 } , { 0 , 0 , 0 , 2 , 0 , 2 } , { 0 , 0 , 0 , 2 , 2 , 0 } } ; findcost ( n2 , city2 ) ; } }
import java . util . * ; class GFG { static int V = 5 ; static int minKey ( int key [ ] , boolean [ ] mstSet ) { int min = Integer . MAX_VALUE , min_index = 0 ; for ( int v = 0 ; v < V ; v ++ ) { if ( mstSet [ v ] == false && key [ v ] < min ) { min = key [ v ] ; min_index = v ; } } return min_index ; } static void printMST ( int parent [ ] , int n , int graph [ ] [ ] ) { System . out . printf ( "Edge WeightNEW_LINE"); int minProduct = 1 ; for ( int i = 1 ; i < V ; i ++ ) { System . out . printf ( "%d - %d %d NEW_LINE", parent[i], i, graph[i][parent[i]]); minProduct *= graph [ i ] [ parent [ i ] ] ; } System . out . printf ( "Minimum Obtainable product is %dNEW_LINE", minProduct); } static void primMST ( int inputGraph [ ] [ ] , double logGraph [ ] [ ] ) { int [ ] parent = new int [ V ] ; int [ ] key = new int [ V ] ; boolean [ ] mstSet = new boolean [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { key [ i ] = Integer . MAX_VALUE ; mstSet [ i ] = false ; } key [ 0 ] = 0 ; parent [ 0 ] = - 1 ; for ( int count = 0 ; count < V - 1 ; count ++ ) { int u = minKey ( key , mstSet ) ; mstSet [ u ] = true ; for ( int v = 0 ; v < V ; v ++ ) { if ( logGraph [ u ] [ v ] > 0 && mstSet [ v ] == false && logGraph [ u ] [ v ] < key [ v ] ) { parent [ v ] = u ; key [ v ] = ( int ) logGraph [ u ] [ v ] ; } } } printMST ( parent , V , inputGraph ) ; } static void minimumProductMST ( int graph [ ] [ ] ) { double [ ] [ ] logGraph = new double [ V ] [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { if ( graph [ i ] [ j ] > 0 ) { logGraph [ i ] [ j ] = Math . log ( graph [ i ] [ j ] ) ; } else { logGraph [ i ] [ j ] = 0 ; } } } primMST ( graph , logGraph ) ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 2 , 0 , 6 , 0 } , { 2 , 0 , 3 , 8 , 5 } , { 0 , 3 , 0 , 0 , 7 } , { 6 , 8 , 0 , 0 , 9 } , { 0 , 5 , 7 , 9 , 0 } , } ; minimumProductMST ( graph ) ; } }
import java . util . * ; class GFG { static final int LEFT = 0 ; static final int RIGHT = 1 ; static int ChangeDirection ( int Dir ) { Dir = 1 - Dir ; return Dir ; } 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 modifiedLevelOrder ( node root ) { if ( root == null ) return ; int dir = LEFT ; node temp ; Queue < node > Q = new LinkedList < > ( ) ; Stack < node > S = new Stack < > ( ) ; S . add ( root ) ; while ( ! Q . isEmpty ( ) || ! S . isEmpty ( ) ) { while ( ! S . isEmpty ( ) ) { temp = S . peek ( ) ; S . pop ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) Q . add ( temp . left ) ; if ( temp . right != null ) Q . add ( temp . right ) ; } else { if ( temp . right != null ) Q . add ( temp . right ) ; if ( temp . left != null ) Q . add ( temp . left ) ; } } System . out . println ( ) ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; System . out . print ( temp . data + " ▁ " ) ; if ( dir == LEFT ) { if ( temp . left != null ) S . add ( temp . left ) ; if ( temp . right != null ) S . add ( temp . right ) ; } else { if ( temp . right != null ) S . add ( temp . right ) ; if ( temp . left != null ) S . add ( temp . left ) ; } } System . out . println ( ) ; dir = ChangeDirection ( dir ) ; } } 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 . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 8 ) ; root . left . left . right = newNode ( 9 ) ; root . left . right . left = newNode ( 3 ) ; root . left . right . right = newNode ( 1 ) ; root . right . left . left = newNode ( 4 ) ; root . right . left . right = newNode ( 2 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 2 ) ; root . left . right . left . left = newNode ( 16 ) ; root . left . right . left . right = newNode ( 17 ) ; root . right . left . right . left = newNode ( 18 ) ; root . right . right . left . right = newNode ( 19 ) ; modifiedLevelOrder ( root ) ; } }
import java . util . LinkedList ; import java . util . Queue ; public class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static Node temp = root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void insert ( Node temp , int key ) { if ( temp == null ) { root = new Node ( key ) ; return ; } Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( temp ) ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left == null ) { temp . left = new Node ( key ) ; break ; } else q . add ( temp . left ) ; if ( temp . right == null ) { temp . right = new Node ( key ) ; break ; } else q . add ( temp . right ) ; } } public static void main ( String args [ ] ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; System . out . print ( " Inorder ▁ traversal ▁ before ▁ insertion : " ) ; inorder ( root ) ; int key = 12 ; insert ( root , key ) ; System . out . print ( " Inorder traversal after insertion : "); inorder ( root ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class TugOfWar { public int min_diff ; void TOWUtil ( int arr [ ] , int n , boolean curr_elements [ ] , int no_of_selected_elements , boolean soln [ ] , int sum , int curr_sum , int curr_position ) { if ( curr_position == n ) return ; if ( ( n / 2 - no_of_selected_elements ) > ( n - curr_position ) ) return ; TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; no_of_selected_elements ++ ; curr_sum = curr_sum + arr [ curr_position ] ; curr_elements [ curr_position ] = true ; if ( no_of_selected_elements == n / 2 ) { if ( Math . abs ( sum / 2 - curr_sum ) < min_diff ) { min_diff = Math . abs ( sum / 2 - curr_sum ) ; for ( int i = 0 ; i < n ; i ++ ) soln [ i ] = curr_elements [ i ] ; } } else { TOWUtil ( arr , n , curr_elements , no_of_selected_elements , soln , sum , curr_sum , curr_position + 1 ) ; } curr_elements [ curr_position ] = false ; } void tugOfWar ( int arr [ ] ) { int n = arr . length ; boolean [ ] curr_elements = new boolean [ n ] ; boolean [ ] soln = new boolean [ n ] ; min_diff = Integer . MAX_VALUE ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; curr_elements [ i ] = soln [ i ] = false ; } TOWUtil ( arr , n , curr_elements , 0 , soln , sum , 0 , 0 ) ; System . out . print ( " The ▁ first ▁ subset ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == true ) System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( " The second subset is : "); for ( int i = 0 ; i < n ; i ++ ) { if ( soln [ i ] == false ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 45 , - 34 , 12 , 0 , 98 , - 99 , 4 , 189 , - 1 , 4 } ; TugOfWar a = new TugOfWar ( ) ; a . tugOfWar ( arr ) ; } }
class KnightTour { static int N = 8 ; static boolean isSafe ( int x , int y , int sol [ ] [ ] ) { return ( x >= 0 && x < N && y >= 0 && y < N && sol [ x ] [ y ] == - 1 ) ; } static void printSolution ( int sol [ ] [ ] ) { for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < N ; y ++ ) System . out . print ( sol [ x ] [ y ] + " ▁ " ) ; System . out . println ( ) ; } } static boolean solveKT ( ) { int sol [ ] [ ] = new int [ 8 ] [ 8 ] ; for ( int x = 0 ; x < N ; x ++ ) for ( int y = 0 ; y < N ; y ++ ) sol [ x ] [ y ] = - 1 ; int xMove [ ] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int yMove [ ] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; sol [ 0 ] [ 0 ] = 0 ; if ( ! solveKTUtil ( 0 , 0 , 1 , sol , xMove , yMove ) ) { System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } else printSolution ( sol ) ; return true ; } static boolean solveKTUtil ( int x , int y , int movei , int sol [ ] [ ] , int xMove [ ] , int yMove [ ] ) { int k , next_x , next_y ; if ( movei == N * N ) return true ; for ( k = 0 ; k < 8 ; k ++ ) { next_x = x + xMove [ k ] ; next_y = y + yMove [ k ] ; if ( isSafe ( next_x , next_y , sol ) ) { sol [ next_x ] [ next_y ] = movei ; if ( solveKTUtil ( next_x , next_y , movei + 1 , sol , xMove , yMove ) ) return true ; else sol [ next_x ] [ next_y ] = - 1 ; } } return false ; } public static void main ( String args [ ] ) { solveKT ( ) ; } }
public class RatMaze { static int N ; void printSolution ( int sol [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( " ▁ " + sol [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } boolean isSafe ( int maze [ ] [ ] , int x , int y ) { return ( x >= 0 && x < N && y >= 0 && y < N && maze [ x ] [ y ] == 1 ) ; } boolean solveMaze ( int maze [ ] [ ] ) { int sol [ ] [ ] = new int [ N ] [ N ] ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { System . out . print ( " Solution ▁ doesn ' t ▁ exist " ) ; return false ; } printSolution ( sol ) ; return true ; } boolean solveMazeUtil ( int maze [ ] [ ] , int x , int y , int sol [ ] [ ] ) { if ( x == N - 1 && y == N - 1 && maze [ x ] [ y ] == 1 ) { sol [ x ] [ y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { if ( sol [ x ] [ y ] == 1 ) return false ; sol [ x ] [ y ] = 1 ; if ( solveMazeUtil ( maze , x + 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y + 1 , sol ) ) return true ; if ( solveMazeUtil ( maze , x - 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y - 1 , sol ) ) return true ; sol [ x ] [ y ] = 0 ; return false ; } return false ; } public static void main ( String args [ ] ) { RatMaze rat = new RatMaze ( ) ; int maze [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 } , { 1 , 1 , 1 , 1 } } ; N = maze . length ; rat . solveMaze ( maze ) ; } }
public class GFG { static int V = 4 ; static void printSolution ( int [ ] color ) { System . out . println ( " Solution ▁ Exists : " + " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . print ( " ▁ " + color [ i ] ) ; System . out . println ( ) ; } static boolean isSafe ( boolean [ ] [ ] graph , int [ ] color ) { for ( int i = 0 ; i < V ; i ++ ) for ( int j = i + 1 ; j < V ; j ++ ) if ( graph [ i ] [ j ] && color [ j ] == color [ i ] ) return false ; return true ; } static boolean graphColoring ( boolean [ ] [ ] graph , int m , int i , int [ ] color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( int j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } public static void main ( String [ ] args ) { boolean [ ] [ ] graph = { { false , true , true , true } , { true , false , true , false } , { true , true , false , true } , { true , false , true , false } , } ; int m = 3 ; int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; } }
import java . io . * ; import java . util . * ; class Node { int color = 1 ; Set < Integer > edges = new HashSet < Integer > ( ) ; } class GFG { static int canPaint ( ArrayList < Node > nodes , int n , int m ) { ArrayList < Integer > visited = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { visited . add ( 0 ) ; } int maxColors = 1 ; for ( int sv = 1 ; sv <= n ; sv ++ ) { if ( visited . get ( sv ) > 0 ) { continue ; } visited . set ( sv , 1 ) ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( sv ) ; while ( q . size ( ) != 0 ) { int top = q . peek ( ) ; q . remove ( ) ; for ( int it : nodes . get ( top ) . edges ) { if ( nodes . get ( top ) . color == nodes . get ( it ) . color ) { nodes . get ( it ) . color += 1 ; } maxColors = Math . max ( maxColors , Math . max ( nodes . get ( top ) . color , nodes . get ( it ) . color ) ) ; if ( maxColors > m ) return 0 ; if ( visited . get ( it ) == 0 ) { visited . set ( it , 1 ) ; q . remove ( it ) ; } } } } return 1 ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] [ ] graph = { { 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; int m = 3 ; ArrayList < Node > nodes = new ArrayList < Node > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { nodes . add ( new Node ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( graph [ i ] [ j ] > 0 ) { nodes . get ( i ) . edges . add ( i ) ; nodes . get ( j ) . edges . add ( j ) ; } } } System . out . println ( canPaint ( nodes , n , m ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class ShortestPath { static final int V = 4 ; static final int INF = Integer . MAX_VALUE ; int shortestPath ( int graph [ ] [ ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 0 ; if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ; if ( k <= 0 ) return INF ; int res = INF ; for ( int i = 0 ; i < V ; i ++ ) { if ( graph [ u ] [ i ] != INF && u != i && v != i ) { int rec_res = shortestPath ( graph , i , v , k - 1 ) ; if ( rec_res != INF ) res = Math . min ( res , graph [ u ] [ i ] + rec_res ) ; } } return res ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = new int [ ] [ ] { { 0 , 10 , 3 , 2 } , { INF , 0 , INF , 7 } , { INF , INF , 0 , 6 } , { INF , INF , INF , 0 } } ; ShortestPath t = new ShortestPath ( ) ; int u = 0 , v = 3 , k = 2 ; System . out . println ( " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is ▁ " + t . shortestPath ( graph , u , v , k ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class ShortestPath { static final int V = 4 ; static final int INF = Integer . MAX_VALUE ; int shortestPath ( int graph [ ] [ ] , int u , int v , int k ) { int sp [ ] [ ] [ ] = new int [ V ] [ V ] [ k + 1 ] ; for ( int e = 0 ; e <= k ; e ++ ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { sp [ i ] [ j ] [ e ] = INF ; if ( e == 0 && i == j ) sp [ i ] [ j ] [ e ] = 0 ; if ( e == 1 && graph [ i ] [ j ] != INF ) sp [ i ] [ j ] [ e ] = graph [ i ] [ j ] ; if ( e > 1 ) { for ( int a = 0 ; a < V ; a ++ ) { if ( graph [ i ] [ a ] != INF && i != a && j != a && sp [ a ] [ j ] [ e - 1 ] != INF ) sp [ i ] [ j ] [ e ] = Math . min ( sp [ i ] [ j ] [ e ] , graph [ i ] [ a ] + sp [ a ] [ j ] [ e - 1 ] ) ; } } } } } return sp [ u ] [ v ] [ k ] ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = new int [ ] [ ] { { 0 , 10 , 3 , 2 } , { INF , 0 , INF , 7 } , { INF , INF , 0 , 6 } , { INF , INF , INF , 0 } } ; ShortestPath t = new ShortestPath ( ) ; int u = 0 , v = 3 , k = 2 ; System . out . println ( " Weight ▁ of ▁ the ▁ shortest ▁ path ▁ is ▁ " + t . shortestPath ( graph , u , v , k ) ) ; } }
class GFG { static int N = 8 ; static int INF = Integer . MAX_VALUE ; public static int shortestDist ( int [ ] [ ] graph ) { int [ ] dist = new int [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i ] [ j ] == INF ) { continue ; } dist [ i ] = Math . min ( dist [ i ] , graph [ i ] [ j ] + dist [ j ] ) ; } } return dist [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] [ ] graph = new int [ ] [ ] { { INF , 1 , 2 , 5 , INF , INF , INF , INF } , { INF , INF , INF , INF , 4 , 11 , INF , INF } , { INF , INF , INF , INF , 9 , 5 , 16 , INF } , { INF , INF , INF , INF , INF , INF , 2 , INF } , { INF , INF , INF , INF , INF , INF , INF , 18 } , { INF , INF , INF , INF , INF , INF , INF , 13 } , { INF , INF , INF , INF , INF , INF , INF , 2 } } ; System . out . println ( shortestDist ( graph ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root ; void reverseLevelOrder ( Node node ) { int h = height ( node ) ; int i ; for ( i = h ; i >= 1 ; i -- ) { printGivenLevel ( node , i ) ; } } void printGivenLevel ( Node node , int level ) { if ( node == null ) return ; if ( level == 1 ) System . out . print ( node . data + " ▁ " ) ; else if ( level > 1 ) { printGivenLevel ( node . left , level - 1 ) ; printGivenLevel ( node . right , level - 1 ) ; } } int height ( Node node ) { if ( node == null ) return 0 ; else { int lheight = height ( node . left ) ; int rheight = height ( node . right ) ; if ( lheight > rheight ) return ( lheight + 1 ) ; else return ( rheight + 1 ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; System . out . println ( " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : ▁ " ) ; tree . reverseLevelOrder ( tree . root ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int V = 4 ; static class Edge { int from , weight ; Edge ( int from , int weight ) { this . from = from ; this . weight = weight ; } } static Vector < Edge > [ ] edges = new Vector [ V ] ; static { for ( int i = 0 ; i < V ; i ++ ) edges [ i ] = new Vector < > ( ) ; } static void addedge ( int u , int v , int w ) { edges [ v ] . add ( new Edge ( u , w ) ) ; } static void shortestpath ( int [ ] [ ] dp ) { for ( int i = 0 ; i <= V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) dp [ i ] [ j ] = - 1 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < edges [ j ] . size ( ) ; k ++ ) { if ( dp [ i - 1 ] [ edges [ j ] . elementAt ( k ) . from ] != - 1 ) { int curr_wt = dp [ i - 1 ] [ edges [ j ] . elementAt ( k ) . from ] + edges [ j ] . elementAt ( k ) . weight ; if ( dp [ i ] [ j ] == - 1 ) dp [ i ] [ j ] = curr_wt ; else dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , curr_wt ) ; } } } } } static double minAvgWeight ( ) { int [ ] [ ] dp = new int [ V + 1 ] [ V ] ; shortestpath ( dp ) ; double [ ] avg = new double [ V ] ; for ( int i = 0 ; i < V ; i ++ ) avg [ i ] = - 1 ; for ( int i = 0 ; i < V ; i ++ ) { if ( dp [ V ] [ i ] != - 1 ) { for ( int j = 0 ; j < V ; j ++ ) if ( dp [ j ] [ i ] != - 1 ) avg [ i ] = Math . max ( avg [ i ] , ( ( double ) dp [ V ] [ i ] - dp [ j ] [ i ] ) / ( V - j ) ) ; } } double result = avg [ 0 ] ; for ( int i = 0 ; i < V ; i ++ ) if ( avg [ i ] != - 1 && avg [ i ] < result ) result = avg [ i ] ; return result ; } public static void main ( String [ ] args ) { addedge ( 0 , 1 , 1 ) ; addedge ( 0 , 2 , 10 ) ; addedge ( 1 , 2 , 3 ) ; addedge ( 2 , 3 , 2 ) ; addedge ( 3 , 1 , 0 ) ; addedge ( 3 , 0 , 8 ) ; System . out . printf ( " % .5f " , minAvgWeight ( ) ) ; } }
import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right ; } } class BinaryTree { Node root ; void reverseLevelOrder ( Node node ) { Stack < Node > S = new Stack ( ) ; Queue < Node > Q = new LinkedList ( ) ; Q . add ( node ) ; while ( Q . isEmpty ( ) == false ) { node = Q . peek ( ) ; Q . remove ( ) ; S . push ( node ) ; if ( node . right != null ) Q . add ( node . right ) ; if ( node . left != null ) Q . add ( node . left ) ; } while ( S . empty ( ) == false ) { node = S . peek ( ) ; System . out . print ( node . data + " ▁ " ) ; S . pop ( ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; System . out . println ( " Level ▁ Order ▁ traversal ▁ of ▁ binary ▁ tree ▁ is ▁ : " ) ; tree . reverseLevelOrder ( tree . root ) ; } }
import java . io . * ; import java . util . * ; public class Main { class DisjointUnionSets { int [ ] rank , parent ; int n ; public DisjointUnionSets ( int n ) { rank = new int [ n ] ; parent = new int [ n ] ; this . n = n ; makeSet ( ) ; } void makeSet ( ) { for ( int i = 0 ; i < n ; i ++ ) parent [ i ] = i ; } int find ( int x ) { if ( parent [ x ] != x ) { return find ( parent [ x ] ) ; } return x ; } void union ( int x , int y ) { int xRoot = find ( x ) ; int yRoot = find ( y ) ; if ( xRoot == yRoot ) return ; if ( rank [ xRoot ] < rank [ yRoot ] ) parent [ xRoot ] = yRoot ; else if ( rank [ yRoot ] < rank [ xRoot ] ) parent [ yRoot ] = xRoot ; else { parent [ yRoot ] = xRoot ; rank [ xRoot ] = rank [ xRoot ] + 1 ; } } } static int countIslands ( int a [ ] [ ] ) { int n = a . length ; int m = a [ 0 ] . length ; DisjointUnionSets dus = new DisjointUnionSets ( n * m ) ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < m ; k ++ ) { if ( a [ j ] [ k ] == 0 ) continue ; if ( j + 1 < n && a [ j + 1 ] [ k ] == 1 ) dus . union ( j * ( m ) + k , ( j + 1 ) * ( m ) + k ) ; if ( j - 1 >= 0 && a [ j - 1 ] [ k ] == 1 ) dus . union ( j * ( m ) + k , ( j - 1 ) * ( m ) + k ) ; if ( k + 1 < m && a [ j ] [ k + 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j ) * ( m ) + k + 1 ) ; if ( k - 1 >= 0 && a [ j ] [ k - 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j ) * ( m ) + k - 1 ) ; if ( j + 1 < n && k + 1 < m && a [ j + 1 ] [ k + 1 ] == 1 ) dus . union ( j * ( m ) + k , ( j + 1 ) * ( m ) + k + 1 ) ; if ( j + 1 < n && k - 1 >= 0 && a [ j + 1 ] [ k - 1 ] == 1 ) dus . union ( j * m + k , ( j + 1 ) * ( m ) + k - 1 ) ; if ( j - 1 >= 0 && k + 1 < m && a [ j - 1 ] [ k + 1 ] == 1 ) dus . union ( j * m + k , ( j - 1 ) * m + k + 1 ) ; if ( j - 1 >= 0 && k - 1 >= 0 && a [ j - 1 ] [ k - 1 ] == 1 ) dus . union ( j * m + k , ( j - 1 ) * m + k - 1 ) ; } } int [ ] c = new int [ n * m ] ; int numberOfIslands = 0 ; for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < m ; k ++ ) { if ( a [ j ] [ k ] == 1 ) { int x = dus . find ( j * m + k ) ; if ( c [ x ] == 0 ) { numberOfIslands ++ ; c [ x ] ++ ; } else c [ x ] ++ ; } } } return numberOfIslands ; } } public static void main ( String [ ] args ) throws IOException { int [ ] [ ] a = new int [ ] [ ] { { 1 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 0 , 1 , 1 } , { 0 , 0 , 0 , 0 , 0 } , { 1 , 0 , 1 , 0 , 1 } } ; System . out . println ( " Number ▁ of ▁ Islands ▁ is : ▁ " + countIslands ( a ) ) ; }
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static class INT { int data ; } ; static Node reverseTreePathUtil ( Node root , int data , Map < Integer , Integer > temp , int level , INT nextpos ) { if ( root == null ) return null ; if ( data == root . data ) { temp . put ( level , root . data ) ; root . data = temp . get ( nextpos . data ) ; nextpos . data ++ ; return root ; } temp . put ( level , root . data ) ; Node left , right = null ; left = reverseTreePathUtil ( root . left , data , temp , level + 1 , nextpos ) ; if ( left == null ) right = reverseTreePathUtil ( root . right , data , temp , level + 1 , nextpos ) ; if ( left != null right != null ) { root . data = temp . get ( nextpos . data ) ; nextpos . data ++ ; return ( left != null ? left : right ) ; } return null ; } static void reverseTreePath ( Node root , int data ) { Map < Integer , Integer > temp = new HashMap < Integer , Integer > ( ) ; INT nextpos = new INT ( ) ; nextpos . data = 0 ; reverseTreePathUtil ( root , data , temp , 0 , nextpos ) ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; int data = 4 ; reverseTreePath ( root , data ) ; inorder ( root ) ; } }
import java . util . * ; class GFG { static void findpath ( int [ ] [ ] graph , int n ) { Vector < Integer > numofadj = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) numofadj . add ( accumulate ( graph [ i ] , 0 ) ) ; int startPoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj . elementAt ( i ) % 2 == 1 ) { numofodd ++ ; startPoint = i ; } } if ( numofodd > 2 ) { System . out . println ( " No ▁ Solution " ) ; return ; } Stack < Integer > stack = new Stack < > ( ) ; Vector < Integer > path = new Vector < > ( ) ; int cur = startPoint ; while ( ! stack . isEmpty ( ) || accumulate ( graph [ cur ] , 0 ) != 0 ) { if ( accumulate ( graph [ cur ] , 0 ) == 0 ) { path . add ( cur ) ; cur = stack . pop ( ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( graph [ cur ] [ i ] == 1 ) { stack . add ( cur ) ; graph [ cur ] [ i ] = 0 ; graph [ i ] [ cur ] = 0 ; cur = i ; break ; } } } } for ( int ele : path ) System . out . print ( ele + " ▁ - > ▁ " ) ; System . out . println ( cur ) ; } static int accumulate ( int [ ] arr , int sum ) { for ( int i : arr ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int [ ] [ ] graph1 = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } } ; int n = graph1 . length ; findpath ( graph1 , n ) ; int [ ] [ ] graph2 = { { 0 , 1 , 0 , 1 , 1 } , { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 1 , 0 , 0 } } ; n = graph2 . length ; findpath ( graph2 , n ) ; int [ ] [ ] graph3 = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 } , { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 1 , 1 , 0 , 1 , 0 } } ; n = graph3 . length ; findpath ( graph3 , n ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int value ) { this . data = value ; } } ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . data + " ▁ " ) ; inorder ( temp . right ) ; } static void reverseTreePathUtil ( Node root , ArrayList < Node > path , int pathLen , int key ) { if ( root == null ) return ; path . set ( pathLen , root ) ; pathLen ++ ; if ( root . data == key ) { int i = 0 , j = pathLen - 1 ; while ( i < j ) { int temp = path . get ( i ) . data ; path . get ( i ) . data = path . get ( j ) . data ; path . get ( j ) . data = temp ; i ++ ; j -- ; } } if ( root . left == null && root . right == null ) return ; reverseTreePathUtil ( root . left , path , pathLen , key ) ; reverseTreePathUtil ( root . right , path , pathLen , key ) ; } static void reverseTreePath ( Node root , int key ) { if ( root == null ) return ; ArrayList < Node > path = new ArrayList < Node > ( ) ; for ( int i = 0 ; i < 50 ; i ++ ) { path . add ( null ) ; } reverseTreePathUtil ( root , path , 0 , key ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 7 ) ; root . left = new Node ( 6 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 2 ) ; root . right . right = new Node ( 1 ) ; int key = 4 ; reverseTreePath ( root , key ) ; inorder ( root ) ; } }
class shortest_path { static int INF = Integer . MAX_VALUE , N = 4 ; static int minCost ( int cost [ ] [ ] ) { int dist [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ; return dist [ N - 1 ] ; } public static void main ( String args [ ] ) { int cost [ ] [ ] = { { 0 , 15 , 80 , 90 } , { INF , 0 , 40 , 50 } , { INF , INF , 0 , 70 } , { INF , INF , INF , 0 } } ; System . out . println ( " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " + N + " ▁ is ▁ " + minCost ( cost ) ) ; } }
public class GFG { static int numOfways ( int n , int k ) { int p = 1 ; if ( k % 2 != 0 ) p = - 1 ; return ( int ) ( Math . pow ( n - 1 , k ) + p * ( n - 1 ) ) / n ; } public static void main ( String args [ ] ) { int n = 4 , k = 2 ; System . out . println ( numOfways ( n , k ) ) ; } }
import java . util . * ; class GFG { static int root ( int arr [ ] , int i ) { while ( arr [ i ] != i ) { arr [ i ] = arr [ arr [ i ] ] ; i = arr [ i ] ; } return i ; } static void weighted_union ( int arr [ ] , int rank [ ] , int a , int b ) { int root_a = root ( arr , a ) ; int root_b = root ( arr , b ) ; if ( rank [ root_a ] < rank [ root_b ] ) { arr [ root_a ] = arr [ root_b ] ; rank [ root_b ] += rank [ root_a ] ; } else { arr [ root_b ] = arr [ root_a ] ; rank [ root_a ] += rank [ root_b ] ; } } static boolean areSame ( int arr [ ] , int a , int b ) { return ( root ( arr , a ) == root ( arr , b ) ) ; } static void query ( int type , int x , int y , int arr [ ] , int rank [ ] ) { if ( type == 1 ) { if ( areSame ( arr , x , y ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } else if ( type == 2 ) { if ( areSame ( arr , x , y ) == false ) weighted_union ( arr , rank , x , y ) ; } } public static void main ( String [ ] args ) { int n = 7 ; int [ ] arr = new int [ n ] ; int [ ] rank = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = i ; rank [ i ] = 1 ; } int q = 11 ; query ( 1 , 0 , 1 , arr , rank ) ; query ( 2 , 0 , 1 , arr , rank ) ; query ( 2 , 1 , 2 , arr , rank ) ; query ( 1 , 0 , 2 , arr , rank ) ; query ( 2 , 0 , 2 , arr , rank ) ; query ( 2 , 2 , 3 , arr , rank ) ; query ( 2 , 3 , 4 , arr , rank ) ; query ( 1 , 0 , 5 , arr , rank ) ; query ( 2 , 4 , 5 , arr , rank ) ; query ( 2 , 5 , 6 , arr , rank ) ; query ( 1 , 2 , 6 , arr , rank ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printSpecificLevelOrder ( Node node ) { if ( node == null ) return ; System . out . print ( node . data ) ; if ( node . left != null ) System . out . print ( " ▁ " + node . left . data + " ▁ " + node . right . data ) ; if ( node . left . left == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( node . left ) ; q . add ( node . right ) ; Node first = null , second = null ; while ( ! q . isEmpty ( ) ) { first = q . peek ( ) ; q . remove ( ) ; second = q . peek ( ) ; q . remove ( ) ; System . out . print ( " ▁ " + first . left . data + " ▁ " + second . right . data ) ; System . out . print ( " ▁ " + first . right . data + " ▁ " + second . left . data ) ; if ( first . left . left != null ) { q . add ( first . left ) ; q . add ( second . right ) ; q . add ( first . right ) ; q . add ( second . left ) ; } } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . root . right . left . left = new Node ( 12 ) ; tree . root . right . left . right = new Node ( 13 ) ; tree . root . right . right . left = new Node ( 14 ) ; tree . root . right . right . right = new Node ( 15 ) ; tree . root . left . left . left . left = new Node ( 16 ) ; tree . root . left . left . left . right = new Node ( 17 ) ; tree . root . left . left . right . left = new Node ( 18 ) ; tree . root . left . left . right . right = new Node ( 19 ) ; tree . root . left . right . left . left = new Node ( 20 ) ; tree . root . left . right . left . right = new Node ( 21 ) ; tree . root . left . right . right . left = new Node ( 22 ) ; tree . root . left . right . right . right = new Node ( 23 ) ; tree . root . right . left . left . left = new Node ( 24 ) ; tree . root . right . left . left . right = new Node ( 25 ) ; tree . root . right . left . right . left = new Node ( 26 ) ; tree . root . right . left . right . right = new Node ( 27 ) ; tree . root . right . right . left . left = new Node ( 28 ) ; tree . root . right . right . left . right = new Node ( 29 ) ; tree . root . right . right . right . left = new Node ( 30 ) ; tree . root . right . right . right . right = new Node ( 31 ) ; System . out . println ( " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ binary " + " tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; import java . util . LinkedList ; class MaxFlow { static final int V = 6 ; boolean bfs ( int rGraph [ ] [ ] , int s , int t , int parent [ ] ) { boolean visited [ ] = new boolean [ V ] ; for ( int i = 0 ; i < V ; ++ i ) visited [ i ] = false ; LinkedList < Integer > queue = new LinkedList < Integer > ( ) ; queue . add ( s ) ; visited [ s ] = true ; parent [ s ] = - 1 ; while ( queue . size ( ) != 0 ) { int u = queue . poll ( ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( visited [ v ] == false && rGraph [ u ] [ v ] > 0 ) { if ( v == t ) { parent [ v ] = u ; return true ; } queue . add ( v ) ; parent [ v ] = u ; visited [ v ] = true ; } } } return false ; } int fordFulkerson ( int graph [ ] [ ] , int s , int t ) { int u , v ; int rGraph [ ] [ ] = new int [ V ] [ V ] ; for ( u = 0 ; u < V ; u ++ ) for ( v = 0 ; v < V ; v ++ ) rGraph [ u ] [ v ] = graph [ u ] [ v ] ; int parent [ ] = new int [ V ] ; int max_flow = 0 ; while ( bfs ( rGraph , s , t , parent ) ) { int path_flow = Integer . MAX_VALUE ; for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; path_flow = Math . min ( path_flow , rGraph [ u ] [ v ] ) ; } for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; rGraph [ u ] [ v ] -= path_flow ; rGraph [ v ] [ u ] += path_flow ; } max_flow += path_flow ; } return max_flow ; } public static void main ( String [ ] args ) throws java . lang . Exception { int graph [ ] [ ] = new int [ ] [ ] { { 0 , 16 , 13 , 0 , 0 , 0 } , { 0 , 0 , 10 , 12 , 0 , 0 } , { 0 , 4 , 0 , 0 , 14 , 0 } , { 0 , 0 , 9 , 0 , 0 , 20 } , { 0 , 0 , 0 , 7 , 0 , 4 } , { 0 , 0 , 0 , 0 , 0 , 0 } } ; MaxFlow m = new MaxFlow ( ) ; System . out . println ( " The ▁ maximum ▁ possible ▁ flow ▁ is ▁ " + m . fordFulkerson ( graph , 0 , 5 ) ) ; } }
import java . util . * ; class GFG { static int V = 8 ; static boolean bfs ( int rGraph [ ] [ ] , int s , int t , int parent [ ] ) { boolean [ ] visited = new boolean [ V ] ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( s ) ; visited [ s ] = true ; parent [ s ] = - 1 ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . remove ( ) ; for ( int v = 0 ; v < V ; v ++ ) { if ( visited [ v ] == false && rGraph [ u ] [ v ] > 0 ) { q . add ( v ) ; parent [ v ] = u ; visited [ v ] = true ; } } } return ( visited [ t ] == true ) ; } static int findDisjointPaths ( int graph [ ] [ ] , int s , int t ) { int u , v ; int [ ] [ ] rGraph = new int [ V ] [ V ] ; for ( u = 0 ; u < V ; u ++ ) for ( v = 0 ; v < V ; v ++ ) rGraph [ u ] [ v ] = graph [ u ] [ v ] ; int [ ] parent = new int [ V ] ; int max_flow = 0 ; while ( bfs ( rGraph , s , t , parent ) ) { int path_flow = Integer . MAX_VALUE ; for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; path_flow = Math . min ( path_flow , rGraph [ u ] [ v ] ) ; } for ( v = t ; v != s ; v = parent [ v ] ) { u = parent [ v ] ; rGraph [ u ] [ v ] -= path_flow ; rGraph [ v ] [ u ] += path_flow ; } max_flow += path_flow ; } return max_flow ; } public static void main ( String [ ] args ) { int graph [ ] [ ] = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int s = 0 ; int t = 7 ; System . out . println ( " There ▁ can ▁ be ▁ maximum ▁ " + findDisjointPaths ( graph , s , t ) + " ▁ edge - disjoint ▁ paths ▁ from ▁ " + s + " ▁ to ▁ " + t ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void printSpecificLevelOrderUtil ( Node root , Stack < Node > s ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root . left ) ; q . add ( root . right ) ; Node first = null , second = null ; while ( ! q . isEmpty ( ) ) { first = q . peek ( ) ; q . poll ( ) ; second = q . peek ( ) ; q . poll ( ) ; s . push ( second . left ) ; s . push ( first . right ) ; s . push ( second . right ) ; s . push ( first . left ) ; if ( first . left . left != null ) { q . add ( first . right ) ; q . add ( second . left ) ; q . add ( first . left ) ; q . add ( second . right ) ; } } } void printSpecificLevelOrder ( Node root ) { Stack < Node > s = new Stack < Node > ( ) ; s . push ( root ) ; if ( root . left != null ) { s . push ( root . right ) ; s . push ( root . left ) ; } if ( root . left . left != null ) printSpecificLevelOrderUtil ( root , s ) ; while ( ! s . empty ( ) ) { System . out . print ( s . peek ( ) . data + " ▁ " ) ; s . pop ( ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; System . out . println ( " Specific ▁ Level ▁ Order ▁ Traversal ▁ " + " of ▁ Binary ▁ Tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; }
import java . util . * ; class GFG { static int maxindex ( int [ ] dist , int n ) { int mi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dist [ i ] > dist [ mi ] ) mi = i ; } return mi ; } static void selectKcities ( int n , int weights [ ] [ ] , int k ) { int [ ] dist = new int [ n ] ; ArrayList < Integer > centers = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { dist [ i ] = Integer . MAX_VALUE ; } int max = 0 ; for ( int i = 0 ; i < k ; i ++ ) { centers . add ( max ) ; for ( int j = 0 ; j < n ; j ++ ) { dist [ j ] = Math . min ( dist [ j ] , weights [ max ] [ j ] ) ; } max = maxindex ( dist , n ) ; } System . out . println ( dist [ max ] ) ; for ( int i = 0 ; i < centers . size ( ) ; i ++ ) { System . out . print ( centers . get ( i ) + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 4 ; int [ ] [ ] weights = new int [ ] [ ] { { 0 , 4 , 8 , 5 } , { 4 , 0 , 10 , 7 } , { 8 , 10 , 0 , 9 } , { 5 , 7 , 9 , 0 } } ; int k = 2 ; selectKcities ( n , weights , k ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int value ) { data = value ; left = null ; right = null ; } } ; static void specific_level_order_traversal ( Node root ) { Queue < Node > q = new LinkedList < > ( ) ; Stack < Vector < Integer > > s = new Stack < Vector < Integer > > ( ) ; q . add ( root ) ; int sz ; while ( q . size ( ) > 0 ) { Vector < Integer > v = new Vector < Integer > ( ) ; sz = q . size ( ) ; for ( int i = 0 ; i < sz ; ++ i ) { Node temp = q . peek ( ) ; q . remove ( ) ; v . add ( temp . data ) ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } s . push ( v ) ; } while ( s . size ( ) > 0 ) { Vector < Integer > v = s . peek ( ) ; s . pop ( ) ; for ( int i = 0 , j = v . size ( ) - 1 ; i < j ; ++ i ) { System . out . print ( v . get ( i ) + " ▁ " + v . get ( j ) + " ▁ " ) ; j -- ; } } System . out . println ( root . data ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; System . out . println ( " Specific ▁ Level ▁ Order ▁ traversal " + " ▁ of ▁ binary ▁ tree ▁ is " ) ; specific_level_order_traversal ( root ) ; } }
import java . io . * ; class Directed { int V = 4 ; void multiply ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { C [ i ] [ j ] = 0 ; for ( int k = 0 ; k < V ; k ++ ) { C [ i ] [ j ] += A [ i ] [ k ] * B [ k ] [ j ] ; } } } } int getTrace ( int graph [ ] [ ] ) { int trace = 0 ; for ( int i = 0 ; i < V ; i ++ ) { trace += graph [ i ] [ i ] ; } return trace ; } int triangleInGraph ( int graph [ ] [ ] ) { int [ ] [ ] aux2 = new int [ V ] [ V ] ; int [ ] [ ] aux3 = new int [ V ] [ V ] ; for ( int i = 0 ; i < V ; ++ i ) { for ( int j = 0 ; j < V ; ++ j ) { aux2 [ i ] [ j ] = aux3 [ i ] [ j ] = 0 ; } } multiply ( graph , graph , aux2 ) ; multiply ( graph , aux2 , aux3 ) ; int trace = getTrace ( aux3 ) ; return trace / 6 ; } public static void main ( String args [ ] ) { Directed obj = new Directed ( ) ; int graph [ ] [ ] = { { 0 , 1 , 1 , 0 } , { 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 0 } } ; System . out . println ( " Total ▁ number ▁ of ▁ Triangle ▁ in ▁ Graph ▁ : ▁ " + obj . triangleInGraph ( graph ) ) ; } }
import java . util . * ; public class GFG { static class Pair { int first , second ; Pair ( int f , int s ) { first = f ; second = s ; } } static boolean isBipartite ( int V , ArrayList < ArrayList < Integer > > adj ) { int col [ ] = new int [ V ] ; Arrays . fill ( col , - 1 ) ; Queue < Pair > q = new LinkedList < Pair > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { if ( col [ i ] == - 1 ) { q . add ( new Pair ( i , 0 ) ) ; col [ i ] = 0 ; while ( ! q . isEmpty ( ) ) { Pair p = q . peek ( ) ; q . poll ( ) ; int v = p . first ; int c = p . second ; for ( int j : adj . get ( v ) ) { if ( col [ j ] == c ) return false ; if ( col [ j ] == - 1 ) { col [ j ] = ( c == 1 ) ? 0 : 1 ; q . add ( new Pair ( j , col [ j ] ) ) ; } } } } } return true ; } public static void main ( String args [ ] ) { int V , E ; V = 4 ; E = 8 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } adj . get ( 0 ) . add ( 1 ) ; adj . get ( 0 ) . add ( 3 ) ; adj . get ( 1 ) . add ( 0 ) ; adj . get ( 1 ) . add ( 2 ) ; adj . get ( 2 ) . add ( 1 ) ; adj . get ( 2 ) . add ( 3 ) ; adj . get ( 3 ) . add ( 0 ) ; adj . get ( 3 ) . add ( 2 ) ; boolean ans = isBipartite ( V , adj ) ; if ( ans ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static final int V = 4 ; static boolean colorGraph ( int G [ ] [ ] , int color [ ] , int pos , int c ) { if ( color [ pos ] != - 1 && color [ pos ] != c ) return false ; color [ pos ] = c ; boolean ans = true ; for ( int i = 0 ; i < V ; i ++ ) { if ( G [ pos ] [ i ] == 1 ) { if ( color [ i ] == - 1 ) ans &= colorGraph ( G , color , i , 1 - c ) ; if ( color [ i ] != - 1 && color [ i ] != 1 - c ) return false ; } if ( ! ans ) return false ; } return true ; } static boolean isBipartite ( int G [ ] [ ] ) { int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = - 1 ; int pos = 0 ; return colorGraph ( G , color , pos , 1 ) ; } public static void main ( String [ ] args ) { int G [ ] [ ] = { { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; if ( isBipartite ( G ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static final String dictionary [ ] = { " GEEKS " , " FOR " , " QUIZ " , " GUQ " , " EE " } ; static final int n = dictionary . length ; static final int M = 3 , N = 3 ; static boolean isWord ( String str ) { for ( int i = 0 ; i < n ; i ++ ) if ( str . equals ( dictionary [ i ] ) ) return true ; return false ; } static void findWordsUtil ( char boggle [ ] [ ] , boolean visited [ ] [ ] , int i , int j , String str ) { visited [ i ] [ j ] = true ; str = str + boggle [ i ] [ j ] ; if ( isWord ( str ) ) System . out . println ( str ) ; for ( int row = i - 1 ; row <= i + 1 && row < M ; row ++ ) for ( int col = j - 1 ; col <= j + 1 && col < N ; col ++ ) if ( row >= 0 && col >= 0 && ! visited [ row ] [ col ] ) findWordsUtil ( boggle , visited , row , col , str ) ; str = " " + str . charAt ( str . length ( ) - 1 ) ; visited [ i ] [ j ] = false ; } static void findWords ( char boggle [ ] [ ] ) { boolean visited [ ] [ ] = new boolean [ M ] [ N ] ; String str = " " ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) findWordsUtil ( boggle , visited , i , j , str ) ; } public static void main ( String args [ ] ) { char boggle [ ] [ ] = { { ' G ' , ' I ' , ' Z ' } , { ' U ' , ' E ' , ' K ' } , { ' Q ' , ' S ' , ' E ' } } ; System . out . println ( " Following ▁ words ▁ of ▁ dictionary ▁ are ▁ present " ) ; findWords ( boggle ) ; } }
class Node { char data ; Node left , right ; Node ( char item ) { data = item ; left = right = null ; } } class Index { int index ; } class BinaryTree { Node root ; Index index_obj = new Index ( ) ; void storeAlternate ( Node node , char arr [ ] , Index index , int l ) { if ( node == null ) { return ; } storeAlternate ( node . left , arr , index , l + 1 ) ; if ( l % 2 != 0 ) { arr [ index . index ] = node . data ; index . index ++ ; } storeAlternate ( node . right , arr , index , l + 1 ) ; } void modifyTree ( Node node , char arr [ ] , Index index , int l ) { if ( node == null ) { return ; } modifyTree ( node . left , arr , index , l + 1 ) ; if ( l % 2 != 0 ) { node . data = arr [ index . index ] ; ( index . index ) ++ ; } modifyTree ( node . right , arr , index , l + 1 ) ; } void reverse ( char arr [ ] , int n ) { int l = 0 , r = n - 1 ; while ( l < r ) { char temp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = temp ; l ++ ; r -- ; } } void reverseAlternate ( ) { reverseAlternate ( root ) ; } void reverseAlternate ( Node node ) { char [ ] arr = new char [ 100 ] ; storeAlternate ( node , arr , index_obj , 0 ) ; reverse ( arr , index_obj . index ) ; index_obj . index = 0 ; modifyTree ( node , arr , index_obj , 0 ) ; } void printInorder ( ) { printInorder ( root ) ; } void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( ' a ' ) ; tree . root . left = new Node ( ' b ' ) ; tree . root . right = new Node ( ' c ' ) ; tree . root . left . left = new Node ( ' d ' ) ; tree . root . left . right = new Node ( ' e ' ) ; tree . root . right . left = new Node ( ' f ' ) ; tree . root . right . right = new Node ( ' g ' ) ; tree . root . left . left . left = new Node ( ' h ' ) ; tree . root . left . left . right = new Node ( ' i ' ) ; tree . root . left . right . left = new Node ( ' j ' ) ; tree . root . left . right . right = new Node ( ' k ' ) ; tree . root . right . left . left = new Node ( ' l ' ) ; tree . root . right . left . right = new Node ( ' m ' ) ; tree . root . right . right . left = new Node ( ' n ' ) ; tree . root . right . right . right = new Node ( ' o ' ) ; System . out . println ( " Inorder ▁ Traversal ▁ of ▁ given ▁ tree " ) ; tree . printInorder ( ) ; tree . reverseAlternate ( ) ; System . out . println ( " " ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ Traversal ▁ of ▁ modified ▁ tree " ) ; tree . printInorder ( ) ; } }
class GfG { static int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( power ( n ) ) ; } }
public class GFG { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } static int findVertices ( int n ) { return fib ( n + 2 ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( findVertices ( n ) ) ; } }
import java . util . * ; class GFG { static void printMat ( int degseq [ ] , int n ) { int [ ] [ ] mat = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i ] [ j ] = 1 ; mat [ j ] [ i ] = 1 ; } } } System . out . print ( " " ▁ + ▁ setw ( 3 ) ▁ + ▁ " "); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( setw ( 3 ) + " ( " + i + " ) " ) ; System . out . print ( "NEW_LINE"); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( setw ( 4 ) + " ( " + i + " ) " ) ; for ( int j = 0 ; j < n ; j ++ ) System . out . print ( setw ( 5 ) + mat [ i ] [ j ] ) ; System . out . print ( "NEW_LINE"); } } static String setw ( int n ) { String space = " " ; while ( n -- > 0 ) space += " ▁ " ; return space ; } public static void main ( String [ ] args ) { int degseq [ ] = { 2 , 2 , 1 , 1 , 1 } ; int n = degseq . length ; printMat ( degseq , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static final int MAX = 100000 ; @ SuppressWarnings ( " unchecked " ) static List < List < Integer > > adj = new ArrayList ( ) ; @ SuppressWarnings ( " unchecked " ) static List < List < Integer > > adjInv = new ArrayList ( ) ; static boolean [ ] visited = new boolean [ MAX ] ; static boolean [ ] visitedInv = new boolean [ MAX ] ; static Stack < Integer > s = new Stack < Integer > ( ) ; static int [ ] scc = new int [ MAX ] ; static int counter = 1 ; static void addEdges ( int a , int b ) { adj . get ( a ) . add ( b ) ; } static void addEdgesInverse ( int a , int b ) { adjInv . get ( b ) . add ( a ) ; } static void dfsFirst ( int u ) { if ( visited [ u ] ) return ; visited [ u ] = true ; for ( int i = 0 ; i < adj . get ( u ) . size ( ) ; i ++ ) dfsFirst ( adj . get ( u ) . get ( i ) ) ; s . push ( u ) ; } static void dfsSecond ( int u ) { if ( visitedInv [ u ] ) return ; visitedInv [ u ] = true ; for ( int i = 0 ; i < adjInv . get ( u ) . size ( ) ; i ++ ) dfsSecond ( adjInv . get ( u ) . get ( i ) ) ; scc [ u ] = counter ; } static void is2Satisfiable ( int n , int m , int a [ ] , int b [ ] ) { for ( int i = 0 ; i < m ; i ++ ) { if ( a [ i ] > 0 && b [ i ] > 0 ) { addEdges ( a [ i ] + n , b [ i ] ) ; addEdgesInverse ( a [ i ] + n , b [ i ] ) ; addEdges ( b [ i ] + n , a [ i ] ) ; addEdgesInverse ( b [ i ] + n , a [ i ] ) ; } else if ( a [ i ] > 0 && b [ i ] < 0 ) { addEdges ( a [ i ] + n , n - b [ i ] ) ; addEdgesInverse ( a [ i ] + n , n - b [ i ] ) ; addEdges ( - b [ i ] , a [ i ] ) ; addEdgesInverse ( - b [ i ] , a [ i ] ) ; } else if ( a [ i ] < 0 && b [ i ] > 0 ) { addEdges ( - a [ i ] , b [ i ] ) ; addEdgesInverse ( - a [ i ] , b [ i ] ) ; addEdges ( b [ i ] + n , n - a [ i ] ) ; addEdgesInverse ( b [ i ] + n , n - a [ i ] ) ; } else { addEdges ( - a [ i ] , n - b [ i ] ) ; addEdgesInverse ( - a [ i ] , n - b [ i ] ) ; addEdges ( - b [ i ] , n - a [ i ] ) ; addEdgesInverse ( - b [ i ] , n - a [ i ] ) ; } } for ( int i = 1 ; i <= 2 * n ; i ++ ) if ( ! visited [ i ] ) dfsFirst ( i ) ; while ( ! s . isEmpty ( ) ) { int top = s . peek ( ) ; s . pop ( ) ; if ( ! visitedInv [ top ] ) { dfsSecond ( top ) ; counter ++ ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( scc [ i ] == scc [ i + n ] ) { System . out . println ( " The ▁ given ▁ expression " + " is ▁ unsatisfiable . " ) ; return ; } } System . out . println ( " The ▁ given ▁ expression ▁ " + " is ▁ satisfiable . " ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 7 ; for ( int i = 0 ; i < MAX ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; adjInv . add ( new ArrayList < Integer > ( ) ) ; } int a [ ] = { 1 , - 2 , - 1 , 3 , - 3 , - 4 , - 3 } ; int b [ ] = { 2 , 3 , - 2 , 4 , 5 , - 5 , 4 } ; is2Satisfiable ( n , m , a , b ) ; } }
class Sol { static class Node { char key ; Node left , right ; } ; static void preorder ( Node root1 , Node root2 , int lvl ) { if ( root1 == null root2 == null ) return ; if ( lvl % 2 == 0 ) { char t = root1 . key ; root1 . key = root2 . key ; root2 . key = t ; } preorder ( root1 . left , root2 . right , lvl + 1 ) ; preorder ( root1 . right , root2 . left , lvl + 1 ) ; } static void reverseAlternate ( Node root ) { preorder ( root . left , root . right , 0 ) ; } static void printInorder ( Node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; printInorder ( root . right ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = ( char ) key ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( ' a ' ) ; root . left = newNode ( ' b ' ) ; root . right = newNode ( ' c ' ) ; root . left . left = newNode ( ' d ' ) ; root . left . right = newNode ( ' e ' ) ; root . right . left = newNode ( ' f ' ) ; root . right . right = newNode ( ' g ' ) ; root . left . left . left = newNode ( ' h ' ) ; root . left . left . right = newNode ( ' i ' ) ; root . left . right . left = newNode ( ' j ' ) ; root . left . right . right = newNode ( ' k ' ) ; root . right . left . left = newNode ( ' l ' ) ; root . right . left . right = newNode ( ' m ' ) ; root . right . right . left = newNode ( ' n ' ) ; root . right . right . right = newNode ( ' o ' ) ; System . out . print ( "Inorder Traversal of given treeNEW_LINE"); printInorder ( root ) ; reverseAlternate ( root ) ; System . out . print ( " Inorder Traversal of modified tree "); printInorder ( root ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class GFG { static class Node { int key ; Node left , right ; Node ( int key ) { this . key = key ; left = null ; right = null ; } } static Node root ; static Node temp = root ; static void inorder ( Node temp ) { if ( temp == null ) return ; inorder ( temp . left ) ; System . out . print ( temp . key + " ▁ " ) ; inorder ( temp . right ) ; } static void deleteDeepest ( Node root , Node delNode ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; Node temp = null ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp == delNode ) { temp = null ; return ; } if ( temp . right != null ) { if ( temp . right == delNode ) { temp . right = null ; return ; } else q . add ( temp . right ) ; } if ( temp . left != null ) { if ( temp . left == delNode ) { temp . left = null ; return ; } else q . add ( temp . left ) ; } } } static void delete ( Node root , int key ) { if ( root == null ) return ; if ( root . left == null && root . right == null ) { if ( root . key == key ) { root = null ; return ; } else return ; } Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; Node temp = null , keyNode = null ; while ( ! q . isEmpty ( ) ) { temp = q . peek ( ) ; q . remove ( ) ; if ( temp . key == key ) keyNode = temp ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } if ( keyNode != null ) { int x = temp . key ; deleteDeepest ( root , temp ) ; keyNode . key = x ; } } public static void main ( String args [ ] ) { root = new Node ( 10 ) ; root . left = new Node ( 11 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 12 ) ; root . right = new Node ( 9 ) ; root . right . left = new Node ( 15 ) ; root . right . right = new Node ( 8 ) ; System . out . print ( " Inorder ▁ traversal ▁ " + " before ▁ deletion : " ) ; inorder ( root ) ; int key = 11 ; delete ( root , key ) ; System . out . print ( " Inorder traversal " ▁ + ▁ " after deletion : "); inorder ( root ) ; } }
import java . util . * ; class GFG { static int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 4 , m = 2 ; int edgeFrom [ ] = { 2 , 4 } ; int edgeTo [ ] = { 3 , 3 } ; System . out . println ( countSink ( n , m , edgeFrom , edgeTo ) ) ; } }
import java . util . * ; class GFG { static int N = 6 ; static int subsetGraph ( int C [ ] [ ] ) { HashSet < Integer > vertices = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { vertices . add ( i ) ; } while ( ! vertices . isEmpty ( ) ) { if ( vertices . size ( ) == 1 ) { return 1 ; } boolean someone_removed = false ; for ( int x : vertices ) { HashSet < Integer > values = new HashSet < > ( ) ; for ( int y : vertices ) { if ( y != x ) { values . add ( C [ x ] [ y ] ) ; } } if ( values . size ( ) == 1 ) { vertices . remove ( x ) ; someone_removed = true ; break ; } } if ( ! someone_removed ) { break ; } } return ( vertices . size ( ) ) ; } public static void main ( String [ ] args ) { int C [ ] [ ] = { { 0 , 9 , 2 , 4 , 7 , 8 } , { 9 , 0 , 9 , 9 , 7 , 9 } , { 2 , 9 , 0 , 3 , 7 , 6 } , { 4 , 9 , 3 , 0 , 7 , 1 } , { 7 , 7 , 7 , 7 , 0 , 7 } , { 8 , 9 , 6 , 1 , 7 , 0 } } ; System . out . println ( subsetGraph ( C ) ) ; } }
import java . io . * ; import java . util . * ; class Graph { int V ; Vector < Integer > [ ] adj ; Graph ( int V ) { this . V = V ; this . adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; } void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } int countEdges ( ) { int sum = 0 ; for ( int i = 0 ; i < V ; i ++ ) sum += adj [ i ] . size ( ) ; return sum / 2 ; } } class GFG { public static void main ( String [ ] args ) throws IOException { int V = 9 ; Graph g = new Graph ( V ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 7 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 1 , 7 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 2 , 8 ) ; g . addEdge ( 2 , 5 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 3 , 5 ) ; g . addEdge ( 4 , 5 ) ; g . addEdge ( 5 , 6 ) ; g . addEdge ( 6 , 7 ) ; g . addEdge ( 6 , 8 ) ; g . addEdge ( 7 , 8 ) ; System . out . println ( g . countEdges ( ) ) ; } }
import java . util . ArrayDeque ; import java . util . Deque ; class GFG { static int V = 5 ; static boolean isBipartiteUtil ( int G [ ] [ ] , int src , int colorArr [ ] ) { colorArr [ src ] = 1 ; Deque < Integer > q = new ArrayDeque < > ( ) ; q . push ( src ) ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . pop ( ) ; for ( int v = 0 ; v < V ; ++ v ) { if ( G [ u ] [ v ] == - 1 && colorArr [ v ] == - 1 ) { colorArr [ v ] = 1 - colorArr [ u ] ; q . push ( v ) ; } else if ( G [ u ] [ v ] == colorArr [ u ] && colorArr [ v ] == colorArr [ u ] ) return false ; } } return true ; } static boolean isBipartite ( int G [ ] [ ] ) { int colorArr [ ] = new int [ V ] ; for ( int i = 0 ; i < V ; ++ i ) colorArr [ i ] = - 1 ; for ( int i = 0 ; i < V ; i ++ ) if ( colorArr [ i ] == - 1 ) if ( isBipartiteUtil ( G , i , colorArr ) == false ) return false ; return true ; } static boolean canBeDividedinTwoCliques ( int G [ ] [ ] ) { int GC [ ] [ ] = new int [ V ] [ V ] ; for ( int i = 0 ; i < V ; i ++ ) for ( int j = 0 ; j < V ; j ++ ) GC [ i ] [ j ] = ( i != j ) ? - GC [ i ] [ j ] : 0 ; return isBipartite ( GC ) ; } public static void main ( String [ ] args ) { int G [ ] [ ] = { { 0 , 1 , 1 , 1 , 0 } , { 1 , 0 , 1 , 0 , 0 } , { 1 , 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 } } ; if ( canBeDividedinTwoCliques ( G ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { deg_sum += degree [ i ] ; } return ( 2 * ( n - 1 ) == deg_sum ) ; } public static void main ( String [ ] args ) { int n = 5 ; int degree [ ] = { 2 , 3 , 1 , 1 , 1 } ; if ( check ( degree , n ) ) { System . out . println ( " Tree " ) ; } else { System . out . println ( " Graph " ) ; } } }
class GFG { static final int maxn = 25 ; static boolean [ ] [ ] gr = new boolean [ maxn ] [ maxn ] ; static boolean isCover ( int V , int k , int E ) { int set = ( 1 << k ) - 1 ; int limit = ( 1 << V ) ; boolean [ ] [ ] vis = new boolean [ maxn ] [ maxn ] ; ; while ( set < limit ) { for ( int i = 0 ; i < maxn ; i ++ ) { for ( int j = 0 ; j < maxn ; j ++ ) { vis [ i ] [ j ] = false ; } } int cnt = 0 ; for ( int j = 1 , v = 1 ; j < limit ; j = j << 1 , v ++ ) { if ( ( set & j ) != 0 ) { for ( int co = 1 ; co <= V ; co ++ ) { if ( gr [ v ] [ co ] && ! vis [ v ] [ co ] ) { vis [ v ] [ co ] = true ; vis [ co ] [ v ] = true ; cnt ++ ; } } } } if ( cnt == E ) return true ; int co = set & - set ; int ro = set + co ; set = ( ( ( ro ^ set ) >> 2 ) / co ) | ro ; } return false ; } static int findMinCover ( int n , int m ) { int left = 1 , right = n ; while ( right > left ) { int mid = ( left + right ) >> 1 ; if ( isCover ( n , mid , m ) == false ) left = mid + 1 ; else right = mid ; } return left ; } static void insertEdge ( int u , int v ) { gr [ u ] [ v ] = true ; gr [ v ] [ u ] = true ; } public static void main ( String [ ] args ) { int V = 6 , E = 6 ; insertEdge ( 1 , 2 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 1 , 4 ) ; insertEdge ( 1 , 5 ) ; insertEdge ( 1 , 6 ) ; System . out . print ( " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " + findMinCover ( V , E ) + "NEW_LINE"); for ( int i = 0 ; i < maxn ; i ++ ) { for ( int j = 0 ; j < maxn ; j ++ ) { gr [ i ] [ j ] = false ; } } V = 6 ; E = 7 ; insertEdge ( 1 , 2 ) ; insertEdge ( 1 , 3 ) ; insertEdge ( 2 , 3 ) ; insertEdge ( 2 , 4 ) ; insertEdge ( 3 , 5 ) ; insertEdge ( 4 , 5 ) ; insertEdge ( 4 , 6 ) ; System . out . print ( " Minimum ▁ size ▁ of ▁ a ▁ vertex ▁ cover ▁ = ▁ " + findMinCover ( V , E ) + "NEW_LINE"); } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void morrisTraversalPreorder ( ) { morrisTraversalPreorder ( root ) ; } void morrisTraversalPreorder ( Node node ) { while ( node != null ) { if ( node . left == null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } else { Node current = node . left ; while ( current . right != null && current . right != node ) { current = current . right ; } if ( current . right == node ) { current . right = null ; node = node . right ; } else { System . out . print ( node . data + " ▁ " ) ; current . right = node ; node = node . left ; } } } } void preorder ( ) { preorder ( root ) ; } void preorder ( Node node ) { if ( node != null ) { System . out . print ( node . data + " ▁ " ) ; preorder ( node . left ) ; preorder ( node . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 11 ) ; tree . morrisTraversalPreorder ( ) ; System . out . println ( " " ) ; tree . preorder ( ) ; } }
import java . util . Vector ; class Test { static void addEdge ( Vector < Integer > adj [ ] , int u , int v ) { adj [ u ] . addElement ( ( v ) ) ; } static int findSum ( Vector < Integer > adj [ ] , int V ) { int sum = 0 ; for ( int u = 0 ; u < V ; u ++ ) sum += adj [ u ] . size ( ) ; return sum ; } public static void main ( String [ ] args ) { int V = 4 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > adj [ ] = new Vector [ V ] ; for ( int i = 0 ; i < adj . length ; i ++ ) { adj [ i ] = new Vector < > ( ) ; } addEdge ( adj , 0 , 2 ) ; addEdge ( adj , 0 , 3 ) ; addEdge ( adj , 1 , 3 ) ; addEdge ( adj , 2 , 3 ) ; System . out . println ( " Sum ▁ of ▁ dependencies ▁ is ▁ " + findSum ( adj , V ) ) ; } }
public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; }
public void insertAfter ( Node prev_node , int new_data ) { if ( prev_node == null ) { System . out . println ( " The ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ null " ) ; return ; } Node new_node = new Node ( new_data ) ; new_node . next = prev_node . next ; prev_node . next = new_node ; }
import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void iterativePreorder ( ) { iterativePreorder ( root ) ; } void iterativePreorder ( Node node ) { if ( node == null ) { return ; } Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . push ( root ) ; while ( nodeStack . empty ( ) == false ) { Node mynode = nodeStack . peek ( ) ; System . out . print ( mynode . data + " ▁ " ) ; nodeStack . pop ( ) ; if ( mynode . right != null ) { nodeStack . push ( mynode . right ) ; } if ( mynode . left != null ) { nodeStack . push ( mynode . left ) ; } } } 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 . left = new Node ( 2 ) ; tree . iterativePreorder ( ) ; } }
import java . util . Stack ; class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void preorderIterative ( ) { preorderIterative ( root ) ; } void preorderIterative ( Node node ) { if ( node == null ) { return ; } Stack < Node > st = new Stack < Node > ( ) ; Node curr = node ; while ( curr != null || ! st . isEmpty ( ) ) { while ( curr != null ) { System . out . print ( curr . data + " ▁ " ) ; if ( curr . right != null ) st . push ( curr . right ) ; curr = curr . left ; } if ( ! st . isEmpty ( ) ) { curr = st . pop ( ) ; } } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 20 ) ; tree . root . right = new Node ( 30 ) ; tree . root . left . left = new Node ( 40 ) ; tree . root . left . left . left = new Node ( 70 ) ; tree . root . left . right = new Node ( 50 ) ; tree . root . right . left = new Node ( 60 ) ; tree . root . left . left . right = new Node ( 80 ) ; tree . preorderIterative ( ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( int val ) { data = val ; next = null ; } } class GFG { static int frequency = 0 ; static Node push ( Node head , int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; return head ; } static int count ( Node head , int key ) { if ( head == null ) return frequency ; if ( head . data == key ) frequency ++ ; return count ( head . next , key ) ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 3 ) ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; System . out . print ( " count ▁ of ▁ 1 ▁ is ▁ " + count ( head , 1 ) ) ; } }
int count ( Node head , int key ) { if ( head == null ) return 0 ; if ( head . data == key ) return 1 + count ( head . next , key ) ; return count ( head . next , key ) ; }
import java . util . * ; class GFG { static class Node { int data ; Node next ; int flag ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . flag = 0 ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static boolean detectLoop ( Node h ) { while ( h != null ) { if ( h . flag == 1 ) return true ; h . flag = 1 ; h = h . next ; } return false ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 4 ) ; head = push ( head , 15 ) ; head = push ( head , 10 ) ; head . next . next . next . next = head ; if ( detectLoop ( head ) ) System . out . print ( " Loop ▁ found " ) ; else System . out . print ( " No ▁ Loop " ) ; } }
class LinkedList { Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } void detectLoop ( ) { Node slow_p = head , fast_p = head ; int flag = 0 ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( " Loop ▁ found " ) ; else System . out . println ( " Loop ▁ not ▁ found " ) ; } public static void main ( String args [ ] ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 20 ) ; llist . push ( 4 ) ; llist . push ( 15 ) ; llist . push ( 10 ) ; llist . head . next . next . next . next = llist . head ; llist . detectLoop ( ) ; } }
class GFG { static class Node { int key ; Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . next = null ; return temp ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . key + " ▁ " ) ; head = head . next ; } System . out . println ( ) ; } static boolean detectLoop ( Node head ) { Node temp = new Node ( ) ; while ( head != null ) { if ( head . next == null ) { return false ; } if ( head . next == temp ) { return true ; } Node nex = head . next ; head . next = temp ; head = nex ; } return false ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = head . next . next ; boolean found = detectLoop ( head ) ; if ( found ) System . out . println ( " Loop ▁ Found " ) ; else System . out . println ( " No ▁ Loop " ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . next = null ; return temp ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . key + " ▁ " ) ; head = head . next ; } System . out . println ( ) ; } static int distance ( Node first , Node last ) { int counter = 0 ; Node curr ; curr = first ; while ( curr != last ) { counter += 1 ; curr = curr . next ; } return counter + 1 ; } static boolean detectLoop ( Node head ) { Node temp = new Node ( ) ; Node first , last ; first = head ; last = head ; int current_length = 0 ; int prev_length = - 1 ; while ( current_length > prev_length && last != null ) { prev_length = current_length ; current_length = distance ( first , last ) ; last = last . next ; } if ( last == null ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = head . next . next ; boolean found = detectLoop ( head ) ; if ( found ) System . out . print ( " Loop ▁ Found " ) ; else System . out . print ( " No ▁ Loop ▁ Found " ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( ) { data = 0 ; next = null ; } } class GFG { static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; head_ref = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } static void removeDuplicates ( Node head ) { HashMap < Integer , Boolean > track = new HashMap < > ( ) ; Node temp = head ; while ( temp != null ) { if ( ! track . containsKey ( temp . data ) ) { System . out . print ( temp . data + " ▁ " ) ; } track . put ( temp . data , true ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 13 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; head = push ( head , 11 ) ; System . out . print ( " Linked ▁ list ▁ before ▁ duplicate ▁ removal ▁ " ) ; printList ( head ) ; System . out . print ( " Linked list after duplicate removal "); removeDuplicates ( head ) ; } }
class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } class LinkedList { Node head ; public void swapNodes ( int x , int y ) { if ( x == y ) return ; Node prevX = null , currX = head ; while ( currX != null && currX . data != x ) { prevX = currX ; currX = currX . next ; } Node prevY = null , currY = head ; while ( currY != null && currY . data != y ) { prevY = currY ; currY = currY . next ; } if ( currX == null currY == null ) return ; if ( prevX != null ) prevX . next = currY ; else head = currY ; if ( prevY != null ) prevY . next = currX ; else head = currX ; Node temp = currX . next ; currX . next = currY . next ; currY . next = temp ; } public void push ( int new_data ) { Node new_Node = new Node ( new_data ) ; new_Node . next = head ; head = new_Node ; } public void printList ( ) { Node tNode = head ; while ( tNode != null ) { System . out . print ( tNode . data + " ▁ " ) ; tNode = tNode . next ; } } public static void main ( String [ ] args ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 7 ) ; llist . push ( 6 ) ; llist . push ( 5 ) ; llist . push ( 4 ) ; llist . push ( 3 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; System . out . print ( " Linked list before calling swapNodes ( ) "); llist . printList ( ) ; llist . swapNodes ( 4 , 3 ) ; System . out . print ( " Linked list after calling swapNodes ( ) "); llist . printList ( ) ; } }
import java . util . * ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = this . right = null ; } } ; class GFG { Node root ; void postorder ( Node head ) { Node temp = root ; HashSet < Node > visited = new HashSet < > ( ) ; while ( ( temp != null && ! visited . contains ( temp ) ) ) { if ( temp . left != null && ! visited . contains ( temp . left ) ) temp = temp . left ; else if ( temp . right != null && ! visited . contains ( temp . right ) ) temp = temp . right ; else { System . out . printf ( " % d ▁ " , temp . data ) ; visited . add ( temp ) ; temp = head ; } } } public static void main ( String [ ] args ) { GFG gfg = new GFG ( ) ; gfg . root = new Node ( 8 ) ; gfg . root . left = new Node ( 3 ) ; gfg . root . right = new Node ( 10 ) ; gfg . root . left . left = new Node ( 1 ) ; gfg . root . left . right = new Node ( 6 ) ; gfg . root . left . right . left = new Node ( 4 ) ; gfg . root . left . right . right = new Node ( 7 ) ; gfg . root . right . right = new Node ( 14 ) ; gfg . root . right . right . left = new Node ( 13 ) ; gfg . postorder ( gfg . root ) ; } }
class GFG { static class Node { int data ; Node left , right ; boolean visited ; } static void postorder ( Node head ) { Node temp = head ; while ( temp != null && temp . visited == false ) { if ( temp . left != null && temp . left . visited == false ) temp = temp . left ; else if ( temp . right != null && temp . right . visited == false ) temp = temp . right ; else { System . out . printf ( " % d ▁ " , temp . data ) ; temp . visited = true ; temp = head ; } } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; node . visited = false ; return ( node ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 4 ) ; root . left . right . right = newNode ( 7 ) ; root . right . right = newNode ( 14 ) ; root . right . right . left = newNode ( 13 ) ; postorder ( root ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } static boolean isCircular ( Node head ) { if ( head == null ) return true ; Node node = head . next ; while ( node != null && node != head ) node = node . next ; return ( node == head ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; return temp ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; System . out . print ( isCircular ( head ) ? "Yes " ▁ : ▁ " No " ); head . next . next . next . next = head ; System . out . print ( isCircular ( head ) ? "Yes " ▁ : ▁ " No " ); } }
static Node addToEmpty ( Node last , int data ) { if ( last != null ) return last ; Node temp = ( Node * ) malloc ( sizeof ( Node ) ) ; temp . data = data ; last = temp ; temp . next = last ; return last ; }
static Node addBegin ( Node last , int data ) { if ( last == null ) return addToEmpty ( last , data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = last . next ; last . next = temp ; return last ; }
import java . util . HashMap ; import java . util . Map . Entry ; import java . util . Vector ; public class DiagonalTraversalBTree { static class Node { int data ; Node left ; Node right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static void diagonalPrintUtil ( Node root , int d , HashMap < Integer , Vector < Integer > > diagonalPrint ) { if ( root == null ) return ; Vector < Integer > k = diagonalPrint . get ( d ) ; if ( k == null ) { k = new Vector < > ( ) ; k . add ( root . data ) ; } else { k . add ( root . data ) ; } diagonalPrint . put ( d , k ) ; diagonalPrintUtil ( root . left , d + 1 , diagonalPrint ) ; diagonalPrintUtil ( root . right , d , diagonalPrint ) ; } static void diagonalPrint ( Node root ) { HashMap < Integer , Vector < Integer > > diagonalPrint = new HashMap < > ( ) ; diagonalPrintUtil ( root , 0 , diagonalPrint ) ; System . out . println ( " Diagonal ▁ Traversal ▁ of ▁ Binnary ▁ Tree " ) ; for ( Entry < Integer , Vector < Integer > > entry : diagonalPrint . entrySet ( ) ) { System . out . println ( entry . getValue ( ) ) ; } } public static void main ( String [ ] args ) { Node root = new Node ( 8 ) ; root . left = new Node ( 3 ) ; root . right = new Node ( 10 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 6 ) ; root . right . right = new Node ( 14 ) ; root . right . right . left = new Node ( 13 ) ; root . left . right . left = new Node ( 4 ) ; root . left . right . right = new Node ( 7 ) ; diagonalPrint ( root ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Array_imp { static int root = 0 ; static String [ ] str = new String [ 10 ] ; public void Root ( String key ) { str [ 0 ] = key ; } public void set_Left ( String key , int root ) { int t = ( root * 2 ) + 1 ; if ( str [ root ] == null ) { System . out . printf ( "Can't set child at %d, no parent foundNEW_LINE", t); } else { str [ t ] = key ; } } public void set_Right ( String key , int root ) { int t = ( root * 2 ) + 2 ; if ( str [ root ] == null ) { System . out . printf ( "Can't set child at %d, no parent foundNEW_LINE", t); } else { str [ t ] = key ; } } public void print_Tree ( ) { for ( int i = 0 ; i < 10 ; i ++ ) { if ( str [ i ] != null ) System . out . print ( str [ i ] ) ; else System . out . print ( " - " ) ; } } } class Tree { public static void main ( String [ ] args ) { Array_imp obj = new Array_imp ( ) ; obj . Root ( " A " ) ; obj . set_Right ( " C " , 0 ) ; obj . set_Left ( " D " , 1 ) ; obj . set_Right ( " E " , 1 ) ; obj . set_Left ( " F " , 2 ) ; obj . print_Tree ( ) ; } }
class GFG { static class Node { int data ; Node next , prev ; } ; static void pairSum ( Node head , int x ) { Node first = head ; Node second = head ; while ( second . next != null ) second = second . next ; boolean found = false ; while ( first != second && second . next != first ) { if ( ( first . data + second . data ) == x ) { found = true ; System . out . println ( " ( " + first . data + " , ▁ " + second . data + " ) " ) ; first = first . next ; second = second . prev ; } else { if ( ( first . data + second . data ) < x ) first = first . next ; else second = second . prev ; } } if ( found == false ) System . out . println ( " No ▁ pair ▁ found " ) ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( head == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return temp ; } public static void main ( String args [ ] ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 7 ; pairSum ( head , x ) ; } }
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void diagonalPrint ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; while ( q . size ( ) > 0 ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp == null ) { if ( q . size ( ) == 0 ) return ; System . out . println ( ) ; q . add ( null ) ; } else { while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; if ( temp . left != null ) q . add ( temp . left ) ; temp = temp . right ; } } } } public static void main ( String args [ ] ) { Node root = newNode ( 8 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 6 ) ; root . right . right = newNode ( 14 ) ; root . right . right . left = newNode ( 13 ) ; root . left . right . left = newNode ( 4 ) ; root . left . right . right = newNode ( 7 ) ; diagonalPrint ( root ) ; } }
class GFG { static class Node { int data ; Node next ; Node prev ; } static Node deleteNode ( Node head_ref , Node del ) { if ( head_ref == null del == null ) return head_ref ; if ( head_ref == del ) head_ref = del . next ; if ( del . next != null ) del . next . prev = del . prev ; if ( del . prev != null ) del . prev . next = del . next ; return head_ref ; } static Node removeDuplicates ( Node head_ref ) { if ( ( head_ref ) == null || ( head_ref ) . next == null ) return head_ref ; ; Node ptr1 , ptr2 ; for ( ptr1 = head_ref ; ptr1 != null ; ptr1 = ptr1 . next ) { ptr2 = ptr1 . next ; while ( ptr2 != null ) { if ( ptr1 . data == ptr2 . data ) { Node next = ptr2 . next ; head_ref = deleteNode ( head_ref , ptr2 ) ; ptr2 = next ; } else ptr2 = ptr2 . next ; } } return head_ref ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) System . out . print ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; System . out . print ( "Original Doubly linked list:NEW_LINE"); printList ( head ) ; head = removeDuplicates ( head ) ; System . out . print ( " Doubly linked list after " ▁ + ▁ " removing duplicates : "); printList ( head ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; Node prev ; } ; static Node deleteNode ( Node head_ref , Node del ) { if ( head_ref == null del == null ) return null ; if ( head_ref == del ) head_ref = del . next ; if ( del . next != null ) del . next . prev = del . prev ; if ( del . prev != null ) del . prev . next = del . next ; return head_ref ; } static Node removeDuplicates ( Node head_ref ) { if ( ( head_ref ) == null ) return null ; HashSet < Integer > us = new HashSet < > ( ) ; Node current = head_ref , next ; while ( current != null ) { if ( us . contains ( current . data ) ) { next = current . next ; head_ref = deleteNode ( head_ref , current ) ; current = next ; } else { us . add ( current . data ) ; current = current . next ; } } return head_ref ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) System . out . print ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 12 ) ; head = push ( head , 10 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 4 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; System . out . println ( " Original ▁ Doubly ▁ linked ▁ list : " ) ; printList ( head ) ; head = removeDuplicates ( head ) ; System . out . println ( " Doubly linked list after " ▁ + ▁ " removing duplicates : "); printList ( head ) ; } }
class GFG { static class Node { int data ; Node next ; Node prev ; } static Node reverse ( Node head_ref ) { Node temp = null ; Node current = head_ref ; while ( current != null ) { temp = current . prev ; current . prev = current . next ; current . next = temp ; current = current . prev ; } if ( temp != null ) head_ref = temp . prev ; return head_ref ; } static Node merge ( Node first , Node second ) { if ( first == null ) return second ; if ( second == null ) return first ; if ( first . data < second . data ) { first . next = merge ( first . next , second ) ; first . next . prev = first ; first . prev = null ; return first ; } else { second . next = merge ( first , second . next ) ; second . next . prev = second ; second . prev = null ; return second ; } } static Node sort ( Node head ) { if ( head == null head . next == null ) return head ; Node current = head . next ; while ( current != null ) { if ( current . data < current . prev . data ) break ; current = current . next ; } if ( current == null ) return head ; current . prev . next = null ; current . prev = null ; current = reverse ( current ) ; return merge ( head , current ) ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) System . out . println ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; head = push ( head , 12 ) ; head = push ( head , 7 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; System . out . println ( " Original ▁ Doubly ▁ linked ▁ list : n " ) ; printList ( head ) ; head = sort ( head ) ; System . out . println ( " Doubly linked list after sorting : n "); printList ( head ) ; } }
import java . io . * ; import java . util . * ; class Node { int info ; Node prev , next ; } class GFG { static Node head , tail ; static void nodeInsetail ( int key ) { Node p = new Node ( ) ; p . info = key ; p . next = null ; if ( head == null ) { head = p ; tail = p ; head . prev = null ; return ; } if ( p . info < head . info ) { p . prev = null ; head . prev = p ; p . next = head ; head = p ; return ; } if ( p . info > tail . info ) { p . prev = tail ; tail . next = p ; tail = p ; return ; } Node temp = head . next ; while ( temp . info < p . info ) temp = temp . next ; ( temp . prev ) . next = p ; p . prev = temp . prev ; temp . prev = p ; p . next = temp ; } static void printList ( Node temp ) { while ( temp != null ) { System . out . print ( temp . info + " ▁ " ) ; temp = temp . next ; } } public static void main ( String args [ ] ) { head = tail = null ; nodeInsetail ( 30 ) ; nodeInsetail ( 50 ) ; nodeInsetail ( 90 ) ; nodeInsetail ( 10 ) ; nodeInsetail ( 40 ) ; nodeInsetail ( 110 ) ; nodeInsetail ( 60 ) ; nodeInsetail ( 95 ) ; nodeInsetail ( 23 ) ; System . out . println ( " Doubly ▁ linked ▁ list ▁ on ▁ printing ▁ from ▁ left ▁ to ▁ right " ) ; printList ( head ) ; } }
static void insertEnd ( int value ) { if ( start == null ) { Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; }
import java . util . * ; class GFG { static Node start ; static class Node { int data ; Node next ; Node prev ; } ; static void insertEnd ( int value ) { if ( start == null ) { Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return ; } Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; } static void insertBegin ( int value ) { Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; new_node . prev = last ; last . next = ( start ) . prev = new_node ; start = new_node ; } static void insertAfter ( int value1 , int value2 ) { Node new_node = new Node ( ) ; new_node . data = value1 ; Node temp = start ; while ( temp . data != value2 ) temp = temp . next ; Node next = temp . next ; temp . next = new_node ; new_node . prev = temp ; new_node . next = next ; next . prev = new_node ; } static void display ( ) { Node temp = start ; System . out . printf ( " Traversal in forward direction "); while ( temp . next != start ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } System . out . printf ( " % d ▁ " , temp . data ) ; System . out . printf ( " Traversal in reverse direction "); Node last = start . prev ; temp = last ; while ( temp . prev != last ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . prev ; } System . out . printf ( " % d ▁ " , temp . data ) ; } public static void main ( String [ ] args ) { Node start = null ; insertEnd ( 5 ) ; insertBegin ( 4 ) ; insertEnd ( 7 ) ; insertEnd ( 8 ) ; insertAfter ( 6 , 5 ) ; System . out . printf ( " Created ▁ circular ▁ doubly ▁ linked ▁ list ▁ is : ▁ " ) ; display ( ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printLeaves ( Node node ) { if ( node == null ) return ; printLeaves ( node . left ) ; if ( node . left == null && node . right == null ) System . out . print ( node . data + " ▁ " ) ; printLeaves ( node . right ) ; } void printBoundaryLeft ( Node node ) { if ( node == null ) return ; if ( node . left != null ) { System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; } else if ( node . right != null ) { System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . right ) ; } } void printBoundaryRight ( Node node ) { if ( node == null ) return ; if ( node . right != null ) { printBoundaryRight ( node . right ) ; System . out . print ( node . data + " ▁ " ) ; } else if ( node . left != null ) { printBoundaryRight ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; } } void printBoundary ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; printBoundaryLeft ( node . left ) ; printLeaves ( node . left ) ; printLeaves ( node . right ) ; printBoundaryRight ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; tree . root . right = new Node ( 22 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printBoundary ( tree . root ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node next ; Node ( int val ) { data = val ; next = null ; } } public class GFG { static void printReverse ( Node head , int n ) { int j = 0 ; Node current = head ; while ( current != null ) { for ( int i = 0 ; i < 2 * ( n - j ) ; i ++ ) System . out . print ( " ▁ " ) ; System . out . print ( " \r " + current . data ) ; current = current . next ; j ++ ; } } static Node push ( Node head , int data ) { Node new_node = new Node ( data ) ; new_node . next = head ; head = new_node ; return head ; } static int printList ( Node head ) { int i = 0 ; Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; i ++ ; } return i ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 3 ) ; head = push ( head , 4 ) ; head = push ( head , 5 ) ; head = push ( head , 6 ) ; System . out . println ( " Given ▁ linked ▁ list : ▁ " ) ; int n = printList ( head ) ; System . out . println ( " Reversed ▁ Linked ▁ list : ▁ " ) ; printReverse ( head , n ) ; System . out . println ( ) ; } }
static class Node { int data ; Node next ; } ;
static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; System . out . print ( head . data + " ▁ " ) ; }
static void fun2 ( Node head ) { if ( head == null ) { return ; } System . out . print ( head . data + " ▁ " ) ; if ( head . next != null ) { fun2 ( head . next . next ) ; } System . out . print ( head . data + " ▁ " ) ; }
class GFG { static class Node { int data ; Node next ; } ; static void fun1 ( Node head ) { if ( head == null ) { return ; } fun1 ( head . next ) ; System . out . print ( head . data + " ▁ " ) ; } static void fun2 ( Node start ) { if ( start == null ) { return ; } System . out . print ( start . data + " ▁ " ) ; if ( start . next != null ) { fun2 ( start . next . next ) ; } System . out . print ( start . data + " ▁ " ) ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; System . out . print ( " Output ▁ of ▁ fun1 ( ) ▁ for ▁ " + "list 1->2->3->4->5 NEW_LINE"); fun1 ( head ) ; System . out . print ( " Output of fun2 ( ) for " ▁ + ▁ " list 1 -> 2 -> 3 -> 4 -> 5 "); fun2 ( head ) ; } }
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class Size { int size = 0 ; } class BinaryTree { Node root ; int heighAndSize ( Node node , Size size ) { if ( node == null ) return 0 ; int l = heighAndSize ( node . left , size ) ; int r = heighAndSize ( node . right , size ) ; size . size ++ ; return ( l > r ) ? l + 1 : r + 1 ; } float density ( Node root ) { Size size = new Size ( ) ; if ( root == null ) return 0 ; int _height = heighAndSize ( root , size ) ; return ( float ) size . size / _height ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; System . out . println ( " Density ▁ of ▁ given ▁ Binary ▁ Tree ▁ is ▁ : ▁ " + tree . density ( tree . root ) ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node head = null ; static int printsqrtn ( Node head ) { Node sqrtn = null ; int i = 1 , j = 1 ; while ( head != null ) { if ( i == j * j ) { if ( sqrtn == null ) sqrtn = head ; else sqrtn = sqrtn . next ; j ++ ; } i ++ ; head = head . next ; } return sqrtn . data ; } static void print ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } System . out . println ( ) ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } public static void main ( String [ ] args ) { push ( 40 ) ; push ( 30 ) ; push ( 20 ) ; push ( 10 ) ; System . out . print ( " Given ▁ linked ▁ list ▁ is : " ) ; print ( head ) ; System . out . print ( " sqrt ( n ) th ▁ node ▁ is ▁ " + printsqrtn ( head ) ) ; } }
public class FractionalNodell { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node fractionalNodes ( Node head , int k ) { if ( k <= 0 head == null ) return null ; Node fractionalNode = null ; int i = 0 ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) { if ( fractionalNode == null ) fractionalNode = head ; else fractionalNode = fractionalNode . next ; } i ++ ; } return fractionalNode ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; System . out . print ( " List ▁ is ▁ " ) ; printList ( head ) ; Node answer = fractionalNodes ( head , k ) ; System . out . println ( " Fractional ▁ node ▁ is ▁ " + answer . data ) ; } }
public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; System . out . print ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) System . out . println ( answer . data ) ; else System . out . println ( " null " ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static Node front , back ; static void frontAndBackSplit ( Node head ) { Node slow , fast ; slow = head ; fast = head . next ; while ( fast != null ) { fast = fast . next ; if ( fast != null ) { slow = slow . next ; fast = fast . next ; } } front = head ; back = slow . next ; slow . next = null ; } static Node reverseList ( Node head_ref ) { Node current , prev , next ; current = head_ref ; prev = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; return head_ref ; } static void modifyTheContentsOf1stHalf ( ) { Node front1 = front , back1 = back ; while ( back1 != null ) { front1 . data = front1 . data - back1 . data ; front1 = front1 . next ; back1 = back1 . next ; } } static Node concatFrontAndBackList ( Node front , Node back ) { Node head = front ; if ( front == null ) return back ; while ( front . next != null ) front = front . next ; front . next = back ; return head ; } static Node modifyTheList ( Node head ) { if ( head == null head . next == null ) return head ; front = null ; back = null ; frontAndBackSplit ( head ) ; back = reverseList ( back ) ; modifyTheContentsOf1stHalf ( ) ; back = reverseList ( back ) ; head = concatFrontAndBackList ( front , back ) ; return head ; } static void printList ( Node head ) { if ( head == null ) return ; while ( head . next != null ) { System . out . print ( head . data + " ▁ - > ▁ " ) ; head = head . next ; } System . out . println ( head . data ) ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; head = modifyTheList ( head ) ; System . out . println ( " Modified ▁ List : " ) ; printList ( head ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node head ) { if ( head == null ) { return ; } while ( head . next != null ) { System . out . print ( head . data + " - > " ) ; head = head . next ; } System . out . print ( head . data + "NEW_LINE"); } static Node find_mid ( Node head ) { Node temp = head , slow = head , fast = head ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } if ( fast != null ) { slow = slow . next ; } return slow ; } static void modifyTheList ( Node head , Node slow ) { Stack < Integer > s = new Stack < Integer > ( ) ; Node temp = head ; while ( slow != null ) { s . add ( slow . data ) ; slow = slow . next ; } while ( ! s . empty ( ) ) { temp . data = temp . data - s . peek ( ) ; temp = temp . next ; s . pop ( ) ; } } public static void main ( String [ ] args ) { Node head = null , mid ; head = push ( head , 10 ) ; head = push ( head , 7 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 2 ) ; mid = find_mid ( head ) ; modifyTheList ( head , mid ) ; System . out . print ( " Modified ▁ List : " + "NEW_LINE"); printList ( head ) ; } }
import java . io . * ; class GFG { static int findDepthRec ( String tree , int n , int index ) { if ( index >= n || tree . charAt ( index ) == ' l ' ) return 0 ; index ++ ; int left = findDepthRec ( tree , n , index ) ; index ++ ; int right = findDepthRec ( tree , n , index ) ; return Math . max ( left , right ) + 1 ; } static int findDepth ( String tree , int n ) { int index = 0 ; return ( findDepthRec ( tree , n , index ) ) ; } static public void main ( String [ ] args ) { String tree = " nlnnlll " ; int n = tree . length ( ) ; System . out . println ( findDepth ( tree , n ) ) ; } }
public Node search ( Node root , int key ) { if ( root == null root . key == key ) return root ; if ( root . key < key ) return search ( root . right , key ) ; return search ( root . left , key ) ; }
class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static Node modifytree ( Node root ) { Node right = root . right ; Node rightMost = root ; if ( root . left != null ) { rightMost = modifytree ( root . left ) ; root . right = root . left ; root . left = null ; } if ( right == null ) return rightMost ; rightMost . right = right ; rightMost = modifytree ( right ) ; return rightMost ; } static void printpre ( Node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }
class GFG { static class Node { int data ; Node left , right ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static Node LevelOrder ( Node root , int data ) { if ( root == null ) { root = getNode ( data ) ; return root ; } if ( data <= root . data ) root . left = LevelOrder ( root . left , data ) ; else root . right = LevelOrder ( root . right , data ) ; return root ; } static Node constructBst ( int arr [ ] , int n ) { if ( n == 0 ) return null ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = LevelOrder ( root , arr [ i ] ) ; return root ; } static void inorderTraversal ( Node root ) { if ( root == null ) return ; inorderTraversal ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorderTraversal ( root . right ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . length ; Node root = constructBst ( arr , n ) ; System . out . print ( " Inorder ▁ Traversal : ▁ " ) ; inorderTraversal ( root ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return ( node ) ; } static void modifytree ( Node root ) { if ( root == null ) return ; Stack < Node > nodeStack = new Stack < Node > ( ) ; nodeStack . push ( root ) ; Node pre = null ; while ( nodeStack . isEmpty ( ) == false ) { Node node = nodeStack . peek ( ) ; nodeStack . pop ( ) ; if ( node . right != null ) nodeStack . push ( node . right ) ; if ( node . left != null ) nodeStack . push ( node . left ) ; if ( pre != null ) { pre . right = node ; } pre = node ; } } static void printpre ( Node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; modifytree ( root ) ; printpre ( root ) ; } }
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 " ) ; } } }
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 " ) ; } } }
import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void storeBSTNodes ( Node root , Vector < Node > nodes ) { if ( root == null ) return ; storeBSTNodes ( root . left , nodes ) ; nodes . add ( root ) ; storeBSTNodes ( root . right , nodes ) ; } Node buildTreeUtil ( Vector < Node > nodes , int start , int end ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; Node node = nodes . get ( mid ) ; node . left = buildTreeUtil ( nodes , start , mid - 1 ) ; node . right = buildTreeUtil ( nodes , mid + 1 , end ) ; return node ; } Node buildTree ( Node root ) { Vector < Node > nodes = new Vector < Node > ( ) ; storeBSTNodes ( root , nodes ) ; int n = nodes . size ( ) ; return buildTreeUtil ( nodes , 0 , n - 1 ) ; } void preOrder ( Node node ) { if ( node == null ) return ; System . out . print ( node . data + " ▁ " ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 8 ) ; tree . root . left . left = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 6 ) ; tree . root . left . left . left . left = new Node ( 5 ) ; tree . root = tree . buildTree ( tree . root ) ; System . out . println ( " Preorder ▁ traversal ▁ of ▁ balanced ▁ BST ▁ is ▁ : " ) ; tree . preOrder ( tree . root ) ; } }
class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { static Node head ; Node insert ( Node node , int data ) { if ( node == null ) { return ( new Node ( data ) ) ; } else { if ( data <= node . data ) { node . left = insert ( node . left , data ) ; } else { node . right = insert ( node . right , data ) ; } return node ; } } int minvalue ( Node node ) { Node current = node ; while ( current . left != null ) { current = current . left ; } return ( current . data ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; Node root = null ; root = tree . insert ( root , 4 ) ; tree . insert ( root , 2 ) ; tree . insert ( root , 1 ) ; tree . insert ( root , 3 ) ; tree . insert ( root , 6 ) ; tree . insert ( root , 5 ) ; System . out . println ( " Minimum ▁ value ▁ of ▁ BST ▁ is ▁ " + tree . minvalue ( root ) ) ; } }
import java . util . * ; class Solution { static class NodeDetails { int data ; int min , max ; } ; static boolean levelOrderIsOfBST ( int arr [ ] , int n ) { if ( n == 0 ) return true ; Queue < NodeDetails > q = new LinkedList < NodeDetails > ( ) ; int i = 0 ; NodeDetails newNode = new NodeDetails ( ) ; newNode . data = arr [ i ++ ] ; newNode . min = Integer . MIN_VALUE ; newNode . max = Integer . MAX_VALUE ; q . add ( newNode ) ; while ( i != n && q . size ( ) > 0 ) { NodeDetails temp = q . peek ( ) ; q . remove ( ) ; newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] < ( int ) temp . data && arr [ i ] > ( int ) temp . min ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . min ; newNode . max = temp . data ; q . add ( newNode ) ; } newNode = new NodeDetails ( ) ; if ( i < n && ( arr [ i ] > ( int ) temp . data && arr [ i ] < ( int ) temp . max ) ) { newNode . data = arr [ i ++ ] ; newNode . min = temp . data ; newNode . max = temp . max ; q . add ( newNode ) ; } } if ( i == n ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 4 , 12 , 3 , 6 , 8 , 1 , 5 , 10 } ; int n = arr . length ; if ( levelOrderIsOfBST ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class Node { char data ; Node left , right ; Node ( char item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static int preIndex = 0 ; Node buildTree ( char in [ ] , char pre [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; Node tNode = new Node ( pre [ preIndex ++ ] ) ; if ( inStrt == inEnd ) return tNode ; int inIndex = search ( in , inStrt , inEnd , tNode . data ) ; tNode . left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } int search ( char arr [ ] , int strt , int end , char value ) { int i ; for ( i = strt ; i <= end ; i ++ ) { if ( arr [ i ] == value ) return i ; } return i ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; char in [ ] = new char [ ] { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char pre [ ] = new char [ ] { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = in . length ; Node root = tree . buildTree ( in , pre , 0 , len - 1 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ constructed ▁ tree ▁ is ▁ : ▁ " ) ; tree . printInorder ( root ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static Node lca ( Node root , int n1 , int n2 ) { while ( root != null ) { if ( root . data > n1 && root . data > n2 ) root = root . left ; else if ( root . data < n1 && root . data < n2 ) root = root . right ; else break ; } return root ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; int n1 = 10 , n2 = 14 ; Node t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; n1 = 14 ; n2 = 8 ; t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; n1 = 10 ; n2 = 22 ; t = tree . lca ( tree . root , n1 , n2 ) ; System . out . println ( " LCA ▁ of ▁ " + n1 + " ▁ and ▁ " + n2 + " ▁ is ▁ " + t . data ) ; } }
class Sol { static class Node { int data ; Node left , right ; } ; static boolean isBST ( Node root , Node l , Node r ) { if ( root == null ) return true ; if ( l != null && root . data <= l . data ) return false ; if ( r != null && root . data >= r . data ) return false ; return isBST ( root . left , l , root ) && isBST ( root . right , root , r ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 4 ) ; if ( isBST ( root , null , null ) ) System . out . print ( " Is ▁ BST " ) ; else System . out . print ( " Not ▁ a ▁ BST " ) ; } }
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; int lCount ; Node ( int x ) { data = x ; left = right = null ; lCount = 0 ; } } class Gfg { public static Node insert ( Node root , int x ) { if ( root == null ) return new Node ( x ) ; if ( x < root . data ) { root . left = insert ( root . left , x ) ; root . lCount ++ ; } else if ( x > root . data ) root . right = insert ( root . right , x ) ; return root ; } public static Node kthSmallest ( Node root , int k ) { if ( root == null ) return null ; int count = root . lCount + 1 ; if ( count == k ) return root ; if ( count > k ) return kthSmallest ( root . left , k ) ; return kthSmallest ( root . right , k - count ) ; } public static void main ( String args [ ] ) { Node root = null ; int keys [ ] = { 20 , 8 , 22 , 4 , 12 , 10 , 14 } ; for ( int x : keys ) root = insert ( root , x ) ; int k = 4 ; Node res = kthSmallest ( root , k ) ; if ( res == null ) System . out . println ( " There ▁ are ▁ less ▁ " + " than ▁ k ▁ nodes ▁ in ▁ the ▁ BST " ) ; else System . out . println ( " K - th ▁ Smallest " + " ▁ Element ▁ is ▁ " + res . data ) ; } }
import java . io . * ; import java . util . * ; class Node { char data ; Node left , right ; Node ( char item ) { data = item ; left = right = null ; } } class Tree { public static Node root ; static HashMap < Character , Integer > mp = new HashMap < > ( ) ; static int preIndex = 0 ; public static Node buildTree ( char [ ] in , char [ ] pre , int inStrt , int inEnd ) { if ( inStrt > inEnd ) { return null ; } char curr = pre [ preIndex ++ ] ; Node tNode ; tNode = new Node ( curr ) ; if ( inStrt == inEnd ) { return tNode ; } int inIndex = mp . get ( curr ) ; tNode . left = buildTree ( in , pre , inStrt , inIndex - 1 ) ; tNode . right = buildTree ( in , pre , inIndex + 1 , inEnd ) ; return tNode ; } public static Node buldTreeWrap ( char [ ] in , char [ ] pre , int len ) { for ( int i = 0 ; i < len ; i ++ ) { mp . put ( in [ i ] , i ) ; } return buildTree ( in , pre , 0 , len - 1 ) ; } static void printInorder ( Node node ) { if ( node == null ) { return ; } printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String [ ] args ) { char [ ] in = { ' D ' , ' B ' , ' E ' , ' A ' , ' F ' , ' C ' } ; char [ ] pre = { ' A ' , ' B ' , ' D ' , ' E ' , ' C ' , ' F ' } ; int len = in . length ; Tree . root = buldTreeWrap ( in , pre , len ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is " ) ; printInorder ( root ) ; } }
import java . util . * ; class GfG { static class Node { int key ; Node left , right ; } static int KSmallestUsingMorris ( Node root , int k ) { int count = 0 ; int ksmall = Integer . MIN_VALUE ; Node curr = root ; while ( curr != null ) { if ( curr . left == null ) { count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } else { Node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; count ++ ; if ( count == k ) ksmall = curr . key ; curr = curr . right ; } } } return ksmall ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; for ( int k = 1 ; k <= 7 ; k ++ ) System . out . print ( KSmallestUsingMorris ( root , k ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; } static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void seqExistUtil ( Node ptr , int seq [ ] , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static boolean seqExist ( Node root , int seq [ ] , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int seq [ ] = { 4 , 6 , 8 , 14 } ; int n = seq . length ; if ( seqExist ( root , seq , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean isInorder ( int [ ] arr , int n ) { if ( n == 0 n == 1 ) { return true ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] > arr [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 19 , 23 , 25 , 30 , 45 } ; int n = arr . length ; if ( isInorder ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " Non " ) ; } } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void insertToHash ( Node root , HashSet < Integer > s ) { if ( root == null ) return ; insertToHash ( root . left , s ) ; s . add ( root . data ) ; insertToHash ( root . right , s ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; HashSet < Integer > s1 = new HashSet < Integer > ( ) ; HashSet < Integer > s2 = new HashSet < Integer > ( ) ; insertToHash ( root1 , s1 ) ; insertToHash ( root2 , s2 ) ; return ( s1 . equals ( s2 ) ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = temp . right = null ; return temp ; } static void storeInorder ( Node root , Vector < Integer > v ) { if ( root == null ) return ; storeInorder ( root . left , v ) ; v . add ( root . data ) ; storeInorder ( root . right , v ) ; } static boolean checkBSTs ( Node root1 , Node root2 ) { if ( root1 != null && root2 != null ) return true ; if ( ( root1 == null && root2 != null ) || ( root1 != null && root2 == null ) ) return false ; Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; storeInorder ( root1 , v1 ) ; storeInorder ( root2 , v2 ) ; return ( v1 == v2 ) ; } public static void main ( String [ ] args ) { Node root1 = newNode ( 15 ) ; root1 . left = newNode ( 10 ) ; root1 . right = newNode ( 20 ) ; root1 . left . left = newNode ( 5 ) ; root1 . left . right = newNode ( 12 ) ; root1 . right . right = newNode ( 25 ) ; Node root2 = newNode ( 15 ) ; root2 . left = newNode ( 12 ) ; root2 . right = newNode ( 20 ) ; root2 . left . left = newNode ( 5 ) ; root2 . left . left . right = newNode ( 10 ) ; root2 . right . right = newNode ( 25 ) ; if ( checkBSTs ( root1 , root2 ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static int findMaxforN ( Node root , int N ) { if ( root == null ) return - 1 ; if ( root . key == N ) return N ; else if ( root . key < N ) { int k = findMaxforN ( root . right , N ) ; if ( k == - 1 ) return root . key ; else return k ; } else if ( root . key > N ) return findMaxforN ( root . left , N ) ; return - 1 ; } public static void main ( String [ ] args ) { int N = 4 ; Node root = null ; root = insert ( root , 25 ) ; insert ( root , 2 ) ; insert ( root , 1 ) ; insert ( root , 3 ) ; insert ( root , 12 ) ; insert ( root , 9 ) ; insert ( root , 21 ) ; insert ( root , 19 ) ; insert ( root , 25 ) ; System . out . println ( findMaxforN ( root , N ) ) ; } }
class GfG { static class node { int data ; node left , right ; } static void RangeTraversal ( node root , int n1 , int n2 ) { if ( root == null ) return ; node curr = root ; while ( curr != null ) { if ( curr . left == null ) { if ( curr . data <= n2 && curr . data >= n1 ) { System . out . print ( curr . data + " ▁ " ) ; } curr = curr . right ; } else { node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; if ( curr . data <= n2 && curr . data >= n1 ) { System . out . print ( curr . data + " ▁ " ) ; } curr = curr . right ; } } } } static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . right = null ; temp . left = null ; return temp ; } public static void main ( String [ ] args ) { node root = newNode ( 4 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 7 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 10 ) ; RangeTraversal ( root , 4 , 12 ) ; } }
class GfG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int data ) { if ( root == null ) return newNode ( data ) ; if ( data < root . data ) root . left = insert ( root . left , data ) ; else if ( data > root . data ) root . right = insert ( root . right , data ) ; return root ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } } static Node leafDelete ( Node root ) { if ( root == null ) { return null ; } if ( root . left == null && root . right == null ) { return null ; } root . left = leafDelete ( root . left ) ; root . right = leafDelete ( root . right ) ; return root ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; insert ( root , 10 ) ; insert ( root , 5 ) ; insert ( root , 15 ) ; insert ( root , 30 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; System . out . println ( " Inorder ▁ before ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; System . out . println ( ) ; leafDelete ( root ) ; System . out . println ( " INorder ▁ after ▁ Deleting ▁ the ▁ leaf ▁ Node . ▁ " ) ; inorder ( root ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node createNode ( int data ) { Node new_Node = new Node ( ) ; new_Node . left = null ; new_Node . right = null ; new_Node . data = data ; return new_Node ; } static Node insert ( Node root , int key ) { if ( root == null ) return createNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else if ( root . data < key ) root . right = insert ( root . right , key ) ; return root ; } static int count = 0 ; static int ksmallestElementSumRec ( Node root , int k ) { if ( root == null ) return 0 ; if ( count > k ) return 0 ; int res = ksmallestElementSumRec ( root . left , k ) ; if ( count >= k ) return res ; res += root . data ; count ++ ; if ( count >= k ) return res ; return res + ksmallestElementSumRec ( root . right , k ) ; } static int ksmallestElementSum ( Node root , int k ) { int res = ksmallestElementSumRec ( root , k ) ; return res ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; int count = ksmallestElementSum ( root , k ) ; System . out . println ( count ) ; } }
import java . util . * ; class GFG { static class Node { int data ; int lCount ; int Sum ; Node left ; Node right ; } ; static Node createNode ( int data ) { Node new_Node = new Node ( ) ; new_Node . left = null ; new_Node . right = null ; new_Node . data = data ; new_Node . lCount = 0 ; new_Node . Sum = 0 ; return new_Node ; } static Node insert ( Node root , int key ) { if ( root == null ) return createNode ( key ) ; if ( root . data > key ) { root . lCount ++ ; root . Sum += key ; root . left = insert ( root . left , key ) ; } else if ( root . data < key ) root . right = insert ( root . right , key ) ; return root ; } static int temp_sum ; static void ksmallestElementSumRec ( Node root , int k ) { if ( root == null ) return ; if ( ( root . lCount + 1 ) == k ) { temp_sum += root . data + root . Sum ; return ; } else if ( k > root . lCount ) { temp_sum += root . data + root . Sum ; k = k - ( root . lCount + 1 ) ; ksmallestElementSumRec ( root . right , k ) ; } else ksmallestElementSumRec ( root . left , k ) ; } static void ksmallestElementSum ( Node root , int k ) { temp_sum = 0 ; ksmallestElementSumRec ( root , k ) ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 4 ) ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 14 ) ; root = insert ( root , 22 ) ; int k = 3 ; ksmallestElementSum ( root , k ) ; System . out . println ( temp_sum ) ; } }
static class Node { int key ; Node left , right ; } ;
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node pre ; static Node suc ; static void findPreSuc ( Node root , int key ) { if ( root == null ) return ; while ( root != null ) { if ( root . key == key ) { if ( root . right != null ) { suc = root . right ; while ( suc . left != null ) suc = suc . left ; } if ( root . left != null ) { pre = root . left ; while ( pre . right != null ) pre = pre . right ; } return ; } else if ( root . key < key ) { pre = root ; root = root . right ; } else { suc = root ; root = root . left ; } } } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else node . right = insert ( node . right , key ) ; return node ; } public static void main ( String [ ] args ) { int key = 65 ; Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; findPreSuc ( root , key ) ; if ( pre != null ) System . out . println ( " Predecessor ▁ is ▁ " + pre . key ) ; else System . out . print ( " - 1" ) ; if ( suc != null ) System . out . print ( " Successor ▁ is ▁ " + suc . key ) ; else System . out . print ( " - 1" ) ; } }
import java . util . * ; class GFG { static class node { int key ; node left ; node right ; } ; static node head ; static node tail ; static void convertBSTtoDLL ( node root ) { if ( root == null ) return ; if ( root . left != null ) convertBSTtoDLL ( root . left ) ; root . left = tail ; if ( tail != null ) ( tail ) . right = root ; else head = root ; tail = root ; if ( root . right != null ) convertBSTtoDLL ( root . right ) ; } static boolean isPresentInDLL ( node head , node tail , int sum ) { while ( head != tail ) { int curr = head . key + tail . key ; if ( curr == sum ) return true ; else if ( curr > sum ) tail = tail . left ; else head = head . right ; } return false ; } static boolean isTripletPresent ( node root ) { if ( root == null ) return false ; head = null ; tail = null ; convertBSTtoDLL ( root ) ; while ( ( head . right != tail ) && ( head . key < 0 ) ) { if ( isPresentInDLL ( head . right , tail , - 1 * head . key ) ) return true ; else head = head . right ; } return false ; } static node newNode ( int num ) { node temp = new node ( ) ; temp . key = num ; temp . left = temp . right = null ; return temp ; } static node insert ( node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . key > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } public static void main ( String [ ] args ) { node root = null ; root = insert ( root , 6 ) ; root = insert ( root , - 13 ) ; root = insert ( root , 14 ) ; root = insert ( root , - 8 ) ; root = insert ( root , 15 ) ; root = insert ( root , 13 ) ; root = insert ( root , 7 ) ; if ( isTripletPresent ( root ) ) System . out . print ( " Present " ) ; else System . out . print ( " Not ▁ Present " ) ; } }
import java . util . * ; class GFG { static final int MAX_SIZE = 100 ; static class node { int val ; node left , right ; } ; static class Stack { int size ; int top ; node [ ] array ; } ; static Stack createStack ( int size ) { Stack stack = new Stack ( ) ; stack . size = size ; stack . top = - 1 ; stack . array = new node [ stack . size ] ; return stack ; } static int isFull ( Stack stack ) { return ( stack . top - 1 == stack . size ) ? 1 : 0 ; } static int isEmpty ( Stack stack ) { return stack . top == - 1 ? 1 : 0 ; } static void push ( Stack stack , node node ) { if ( isFull ( stack ) == 1 ) return ; stack . array [ ++ stack . top ] = node ; } static node pop ( Stack stack ) { if ( isEmpty ( stack ) == 1 ) return null ; return stack . array [ stack . top -- ] ; } static boolean isPairPresent ( node root , int target ) { Stack s1 = createStack ( MAX_SIZE ) ; Stack s2 = createStack ( MAX_SIZE ) ; boolean done1 = false , done2 = false ; int val1 = 0 , val2 = 0 ; node curr1 = root , curr2 = root ; while ( true ) { while ( done1 == false ) { if ( curr1 != null ) { push ( s1 , curr1 ) ; curr1 = curr1 . left ; } else { if ( isEmpty ( s1 ) == 1 ) done1 = true ; else { curr1 = pop ( s1 ) ; val1 = curr1 . val ; curr1 = curr1 . right ; done1 = true ; } } } while ( done2 == false ) { if ( curr2 != null ) { push ( s2 , curr2 ) ; curr2 = curr2 . right ; } else { if ( isEmpty ( s2 ) == 1 ) done2 = true ; else { curr2 = pop ( s2 ) ; val2 = curr2 . val ; curr2 = curr2 . left ; done2 = true ; } } } if ( ( val1 != val2 ) && ( val1 + val2 ) == target ) { System . out . print ( " Pair ▁ Found : ▁ " + val1 + " + ▁ " + val2 + " ▁ = ▁ " + target + "NEW_LINE"); return true ; } else if ( ( val1 + val2 ) < target ) done1 = false ; else if ( ( val1 + val2 ) > target ) done2 = false ; if ( val1 >= val2 ) return false ; } } static node NewNode ( int val ) { node tmp = new node ( ) ; tmp . val = val ; tmp . right = tmp . left = null ; return tmp ; } public static void main ( String [ ] args ) { node root = NewNode ( 15 ) ; root . left = NewNode ( 10 ) ; root . right = NewNode ( 20 ) ; root . left . left = NewNode ( 8 ) ; root . left . right = NewNode ( 12 ) ; root . right . left = NewNode ( 16 ) ; root . right . right = NewNode ( 25 ) ; int target = 33 ; if ( isPairPresent ( root , target ) == false ) System . out . print ( " No such values are found "); } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node NewNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return NewNode ( key ) ; if ( key < root . data ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static boolean findpairUtil ( Node root , int sum , HashSet < Integer > set ) { if ( root == null ) return false ; if ( findpairUtil ( root . left , sum , set ) ) return true ; if ( set . contains ( sum - root . data ) ) { System . out . println ( " Pair ▁ is ▁ found ▁ ( " + ( sum - root . data ) + " , ▁ " + root . data + " ) " ) ; return true ; } else set . add ( root . data ) ; return findpairUtil ( root . right , sum , set ) ; } static void findPair ( Node root , int sum ) { HashSet < Integer > set = new HashSet < Integer > ( ) ; if ( ! findpairUtil ( root , sum , set ) ) System . out . print ( " Pairs ▁ do ▁ not ▁ exit " + "NEW_LINE"); } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 15 ) ; root = insert ( root , 10 ) ; root = insert ( root , 20 ) ; root = insert ( root , 8 ) ; root = insert ( root , 12 ) ; root = insert ( root , 16 ) ; root = insert ( root , 25 ) ; root = insert ( root , 10 ) ; int sum = 33 ; findPair ( root , sum ) ; } }
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void storeInorder ( Node ptr , Vector < Integer > vect ) { if ( ptr == null ) return ; storeInorder ( ptr . left , vect ) ; vect . add ( ptr . data ) ; storeInorder ( ptr . right , vect ) ; } static void pairSumUtil ( Vector < Integer > vect1 , Vector < Integer > vect2 , int sum ) { int left = 0 ; int right = vect2 . size ( ) - 1 ; while ( left < vect1 . size ( ) && right >= 0 ) { if ( vect1 . get ( left ) + vect2 . get ( right ) == sum ) { System . out . print ( " ( " + vect1 . get ( left ) + " , ▁ " + vect2 . get ( right ) + " ) , ▁ " ) ; left ++ ; right -- ; } else if ( vect1 . get ( left ) + vect2 . get ( right ) < sum ) left ++ ; else right -- ; } } static void pairSum ( Node root1 , Node root2 , int sum ) { Vector < Integer > vect1 = new Vector < Integer > ( ) , vect2 = new Vector < Integer > ( ) ; storeInorder ( root1 , vect1 ) ; storeInorder ( root2 , vect2 ) ; pairSumUtil ( vect1 , vect2 , sum ) ; } public static void main ( String args [ ] ) { Node root1 = null ; root1 = insert ( root1 , 8 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 3 ) ; root1 = insert ( root1 , 6 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 14 ) ; root1 = insert ( root1 , 13 ) ; Node root2 = null ; root2 = insert ( root2 , 5 ) ; root2 = insert ( root2 , 18 ) ; root2 = insert ( root2 , 2 ) ; root2 = insert ( root2 , 1 ) ; root2 = insert ( root2 , 3 ) ; root2 = insert ( root2 , 4 ) ; int sum = 10 ; pairSum ( root1 , root2 , sum ) ; } }
class solution { static int min_diff , min_diff_key ; static class Node { int key ; Node left , right ; } ; static Node newnode ( int key ) { Node node = new Node ( ) ; node . key = key ; node . left = node . right = null ; return ( node ) ; } static void maxDiffUtil ( Node ptr , int k ) { if ( ptr == null ) return ; if ( ptr . key == k ) { min_diff_key = k ; return ; } if ( min_diff > Math . abs ( ptr . key - k ) ) { min_diff = Math . abs ( ptr . key - k ) ; min_diff_key = ptr . key ; } if ( k < ptr . key ) maxDiffUtil ( ptr . left , k ) ; else maxDiffUtil ( ptr . right , k ) ; } static int maxDiff ( Node root , int k ) { min_diff = 999999999 ; min_diff_key = - 1 ; maxDiffUtil ( root , k ) ; return min_diff_key ; } public static void main ( String args [ ] ) { Node root = newnode ( 9 ) ; root . left = newnode ( 4 ) ; root . right = newnode ( 17 ) ; root . left . left = newnode ( 3 ) ; root . left . right = newnode ( 6 ) ; root . left . right . left = newnode ( 5 ) ; root . left . right . right = newnode ( 7 ) ; root . right . right = newnode ( 22 ) ; root . right . right . left = newnode ( 20 ) ; int k = 18 ; System . out . println ( maxDiff ( root , k ) ) ; } }
public class Tree { Node root ; static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } public Node insertLevelOrder ( int [ ] arr , Node root , int i ) { if ( i < arr . length ) { Node temp = new Node ( arr [ i ] ) ; root = temp ; root . left = insertLevelOrder ( arr , root . left , 2 * i + 1 ) ; root . right = insertLevelOrder ( arr , root . right , 2 * i + 2 ) ; } return root ; } public void inOrder ( Node root ) { if ( root != null ) { inOrder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inOrder ( root . right ) ; } } public static void main ( String args [ ] ) { Tree t2 = new Tree ( ) ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 6 , 6 , 6 } ; t2 . root = t2 . insertLevelOrder ( arr , t2 . root , 0 ) ; t2 . inOrder ( t2 . root ) ; } }
import java . util . * ; class solution { static class Node { Node left , right ; int info ; boolean lthread ; boolean rthread ; } ; static Node insert ( Node root , int ikey ) { Node ptr = root ; Node par = null ; while ( ptr != null ) { if ( ikey == ( ptr . info ) ) { System . out . printf ( "Duplicate Key !NEW_LINE"); return root ; } par = ptr ; if ( ikey < ptr . info ) { if ( ptr . lthread == false ) ptr = ptr . left ; else break ; } else { if ( ptr . rthread == false ) ptr = ptr . right ; else break ; } } Node tmp = new Node ( ) ; tmp . info = ikey ; tmp . lthread = true ; tmp . rthread = true ; if ( par == null ) { root = tmp ; tmp . left = null ; tmp . right = null ; } else if ( ikey < ( par . info ) ) { tmp . left = par . left ; tmp . right = par ; par . lthread = false ; par . left = tmp ; } else { tmp . left = par ; tmp . right = par . right ; par . rthread = false ; par . right = tmp ; } return root ; } static Node inorderSuccessor ( Node ptr ) { if ( ptr . rthread == true ) return ptr . right ; ptr = ptr . right ; while ( ptr . lthread == false ) ptr = ptr . left ; return ptr ; } static void inorder ( Node root ) { if ( root == null ) System . out . printf ( " Tree ▁ is ▁ empty " ) ; Node ptr = root ; while ( ptr . lthread == false ) ptr = ptr . left ; while ( ptr != null ) { System . out . printf ( " % d ▁ " , ptr . info ) ; ptr = inorderSuccessor ( ptr ) ; } } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 10 ) ; root = insert ( root , 30 ) ; root = insert ( root , 5 ) ; root = insert ( root , 16 ) ; root = insert ( root , 14 ) ; root = insert ( root , 17 ) ; root = insert ( root , 13 ) ; inorder ( root ) ; } }
static class Node { Node left , right ; int info ; boolean lthread ; boolean rthread ; } ;
Node caseA ( Node root , Node par , Node ptr ) { if ( par == null ) root = null ; else if ( ptr == par . left ) { par . lthread = true ; par . left = ptr . left ; } else { par . rthread = true ; par . right = ptr . right ; } return root ; }
public class fullbinarytreepostpre { static int preindex ; static class node { int data ; node left , right ; public node ( int data ) { this . data = data ; } } static node constructTreeUtil ( int pre [ ] , int post [ ] , int l , int h , int size ) { if ( preindex >= size l > h ) return null ; node root = new node ( pre [ preindex ] ) ; preindex ++ ; if ( l == h preindex >= size ) return root ; int i ; for ( i = l ; i <= h ; i ++ ) { if ( post [ i ] == pre [ preindex ] ) break ; } if ( i <= h ) { root . left = constructTreeUtil ( pre , post , l , i , size ) ; root . right = constructTreeUtil ( pre , post , i + 1 , h , size ) ; } return root ; } static node constructTree ( int pre [ ] , int post [ ] , int size ) { preindex = 0 ; return constructTreeUtil ( pre , post , 0 , size - 1 , size ) ; } static void printInorder ( node root ) { if ( root == null ) return ; printInorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; printInorder ( root . right ) ; } public static void main ( String [ ] args ) { int pre [ ] = { 1 , 2 , 4 , 8 , 9 , 5 , 3 , 6 , 7 } ; int post [ ] = { 8 , 9 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int size = pre . length ; node root = constructTree ( pre , post , size ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree : " ) ; printInorder ( root ) ; } }
static Node caseB ( Node root , Node par , Node ptr ) { Node child ; if ( ptr . lthread == false ) child = ptr . left ; else child = ptr . right ; if ( par == null ) root = child ; else if ( ptr == par . left ) par . left = child ; else par . right = child ; Node s = inSucc ( ptr ) ; Node p = inPred ( ptr ) ; if ( ptr . lthread == false ) p . right = s ; else { if ( ptr . rthread == false ) s . left = p ; } return root ; }
import java . util . * ; class solution { static class Node { Node left , right ; int info ; boolean lthread ; boolean rthread ; } ; static Node insert ( Node root , int ikey ) { Node ptr = root ; Node par = null ; while ( ptr != null ) { if ( ikey == ( ptr . info ) ) { System . out . printf ( "Duplicate Key !NEW_LINE"); return root ; } par = ptr ; if ( ikey < ptr . info ) { if ( ptr . lthread == false ) ptr = ptr . left ; else break ; } else { if ( ptr . rthread == false ) ptr = ptr . right ; else break ; } } Node tmp = new Node ( ) ; tmp . info = ikey ; tmp . lthread = true ; tmp . rthread = true ; if ( par == null ) { root = tmp ; tmp . left = null ; tmp . right = null ; } else if ( ikey < ( par . info ) ) { tmp . left = par . left ; tmp . right = par ; par . lthread = false ; par . left = tmp ; } else { tmp . left = par ; tmp . right = par . right ; par . rthread = false ; par . right = tmp ; } return root ; } static Node inSucc ( Node ptr ) { if ( ptr . rthread == true ) return ptr . right ; ptr = ptr . right ; while ( ptr . lthread == false ) ptr = ptr . left ; return ptr ; } static Node inorderSuccessor ( Node ptr ) { if ( ptr . rthread == true ) return ptr . right ; ptr = ptr . right ; while ( ptr . lthread == false ) ptr = ptr . left ; return ptr ; } static void inorder ( Node root ) { if ( root == null ) System . out . printf ( " Tree ▁ is ▁ empty " ) ; Node ptr = root ; while ( ptr . lthread == false ) ptr = ptr . left ; while ( ptr != null ) { System . out . printf ( " % d ▁ " , ptr . info ) ; ptr = inorderSuccessor ( ptr ) ; } } static Node inPred ( Node ptr ) { if ( ptr . lthread == true ) return ptr . left ; ptr = ptr . left ; while ( ptr . rthread == false ) ptr = ptr . right ; return ptr ; } static Node caseA ( Node root , Node par , Node ptr ) { if ( par == null ) root = null ; else if ( ptr == par . left ) { par . lthread = true ; par . left = ptr . left ; } else { par . rthread = true ; par . right = ptr . right ; } return root ; } static Node caseB ( Node root , Node par , Node ptr ) { Node child ; if ( ptr . lthread == false ) child = ptr . left ; else child = ptr . right ; if ( par == null ) root = child ; else if ( ptr == par . left ) par . left = child ; else par . right = child ; Node s = inSucc ( ptr ) ; Node p = inPred ( ptr ) ; if ( ptr . lthread == false ) p . right = s ; else { if ( ptr . rthread == false ) s . left = p ; } return root ; } static Node caseC ( Node root , Node par , Node ptr ) { Node parsucc = ptr ; Node succ = ptr . right ; while ( succ . lthread == false ) { parsucc = succ ; succ = succ . left ; } ptr . info = succ . info ; if ( succ . lthread == true && succ . rthread == true ) root = caseA ( root , parsucc , succ ) ; else root = caseB ( root , parsucc , succ ) ; return root ; } static Node delThreadedBST ( Node root , int dkey ) { Node par = null , ptr = root ; int found = 0 ; while ( ptr != null ) { if ( dkey == ptr . info ) { found = 1 ; break ; } par = ptr ; if ( dkey < ptr . info ) { if ( ptr . lthread == false ) ptr = ptr . left ; else break ; } else { if ( ptr . rthread == false ) ptr = ptr . right ; else break ; } } if ( found == 0 ) System . out . printf ( "dkey not present in treeNEW_LINE"); else if ( ptr . lthread == false && ptr . rthread == false ) root = caseC ( root , par , ptr ) ; else if ( ptr . lthread == false ) root = caseB ( root , par , ptr ) ; else if ( ptr . rthread == false ) root = caseB ( root , par , ptr ) ; else root = caseA ( root , par , ptr ) ; return root ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 20 ) ; root = insert ( root , 10 ) ; root = insert ( root , 30 ) ; root = insert ( root , 5 ) ; root = insert ( root , 16 ) ; root = insert ( root , 14 ) ; root = insert ( root , 17 ) ; root = insert ( root , 13 ) ; root = delThreadedBST ( root , 20 ) ; inorder ( root ) ; } }
import java . util . * ; class solution { static class Node { int key ; Node left , right ; boolean isThreaded ; } ; static Node createThreaded ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) return root ; if ( root . left != null ) { Node l = createThreaded ( root . left ) ; l . right = root ; l . isThreaded = true ; } if ( root . right == null ) return root ; return createThreaded ( root . right ) ; } static Node leftMost ( Node root ) { while ( root != null && root . left != null ) root = root . left ; return root ; } static void inOrder ( Node root ) { if ( root == null ) return ; Node cur = leftMost ( root ) ; while ( cur != null ) { System . out . print ( cur . key + " ▁ " ) ; if ( cur . isThreaded ) cur = cur . right ; else cur = leftMost ( cur . right ) ; } } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = key ; return temp ; } 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 . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createThreaded ( root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ created ▁ " + "threaded tree isNEW_LINE"); inOrder ( root ) ; } }
class Node { int key ; Node left , right , parent ; public Node ( int key ) { this . key = key ; left = right = parent = null ; } } class BinaryTree { Node root ; Node insert ( Node node , int key ) { if ( node == null ) return new Node ( key ) ; if ( key < node . key ) { node . left = insert ( node . left , key ) ; node . left . parent = node ; } else if ( key > node . key ) { node . right = insert ( node . right , key ) ; node . right . parent = node ; } return node ; } void inorder ( Node root ) { boolean leftdone = false ; while ( root != null ) { if ( ! leftdone ) { while ( root . left != null ) { root = root . left ; } } System . out . print ( root . key + " ▁ " ) ; leftdone = true ; if ( root . right != null ) { leftdone = false ; root = root . right ; } else if ( root . parent != null ) { while ( root . parent != null && root == root . parent . right ) root = root . parent ; if ( root . parent == null ) break ; root = root . parent ; } else break ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = tree . insert ( tree . root , 24 ) ; tree . root = tree . insert ( tree . root , 27 ) ; tree . root = tree . insert ( tree . root , 29 ) ; tree . root = tree . insert ( tree . root , 34 ) ; tree . root = tree . insert ( tree . root , 14 ) ; tree . root = tree . insert ( tree . root , 4 ) ; tree . root = tree . insert ( tree . root , 10 ) ; tree . root = tree . insert ( tree . root , 22 ) ; tree . root = tree . insert ( tree . root , 13 ) ; tree . root = tree . insert ( tree . root , 3 ) ; tree . root = tree . insert ( tree . root , 2 ) ; tree . root = tree . insert ( tree . root , 6 ) ; System . out . println ( " Inorder ▁ traversal ▁ is ▁ " ) ; tree . inorder ( tree . root ) ; } }
class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int Ceil ( Node node , int input ) { if ( node == null ) { return - 1 ; } if ( node . data == input ) { return node . data ; } if ( node . data < input ) { return Ceil ( node . right , input ) ; } int ceil = Ceil ( node . left , input ) ; return ( ceil >= input ) ? ceil : node . data ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 4 ) ; tree . root . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 2 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . right . left = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) { System . out . println ( i + " ▁ " + tree . Ceil ( tree . root , i ) ) ; } } }
class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; INT ( int a ) { this . a = a ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . printf ( " % d ▁ " , node . data ) ; printInorder ( node . right ) ; } static Node conBinaryTreeUtil ( int pre [ ] , int preM [ ] , INT preIndex , int l , int h , int size ) { if ( preIndex . a >= size l > h ) return null ; Node root = newNode ( pre [ preIndex . a ] ) ; ++ ( preIndex . a ) ; if ( l == h ) return root ; int i ; for ( i = l ; i <= h ; ++ i ) if ( pre [ preIndex . a ] == preM [ i ] ) break ; if ( i <= h ) { root . left = conBinaryTreeUtil ( pre , preM , preIndex , i , h , size ) ; root . right = conBinaryTreeUtil ( pre , preM , preIndex , l + 1 , i - 1 , size ) ; } return root ; } static void conBinaryTree ( Node root , int pre [ ] , int preMirror [ ] , int size ) { INT preIndex = new INT ( 0 ) ; int preMIndex = 0 ; root = conBinaryTreeUtil ( pre , preMirror , preIndex , 0 , size - 1 , size ) ; printInorder ( root ) ; } public static void main ( String args [ ] ) { int preOrder [ ] = { 1 , 2 , 4 , 5 , 3 , 6 , 7 } ; int preOrderMirror [ ] = { 1 , 3 , 7 , 6 , 2 , 5 , 4 } ; int size = preOrder . length ; Node root = new Node ( ) ; conBinaryTree ( root , preOrder , preOrderMirror , size ) ; } }
import java . io . * ; class Node { int data ; Node left , right ; Node ( int d ) { data = d ; left = right = null ; } } class BinaryTree { Node root ; int floor ; int ceil ; public void floorCeilBSTHelper ( Node root , int key ) { while ( root != null ) { if ( root . data == key ) { ceil = root . data ; floor = root . data ; return ; } if ( key > root . data ) { floor = root . data ; root = root . right ; } else { ceil = root . data ; root = root . left ; } } return ; } public void floorCeilBST ( Node root , int key ) { floor = - 1 ; ceil = - 1 ; floorCeilBSTHelper ( root , key ) ; System . out . println ( key + " ▁ " + floor + " ▁ " + ceil ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 8 ) ; tree . root . left = new Node ( 4 ) ; tree . root . right = new Node ( 12 ) ; tree . root . left . left = new Node ( 2 ) ; tree . root . left . right = new Node ( 6 ) ; tree . root . right . left = new Node ( 10 ) ; tree . root . right . right = new Node ( 14 ) ; for ( int i = 0 ; i < 16 ; i ++ ) { tree . floorCeilBST ( tree . root , i ) ; } } }
class GFG { static class node { int key ; int count ; node left , right ; } ; static node newNode ( int item ) { node temp = new node ( ) ; temp . key = item ; temp . left = temp . right = null ; temp . count = 1 ; return temp ; } static void inorder ( node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " ( " + root . count + " ) ▁ " ) ; inorder ( root . right ) ; } } static node insert ( node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key == node . key ) { ( node . count ) ++ ; return node ; } if ( key < node . key ) node . left = insert ( node . left , key ) ; else node . right = insert ( node . right , key ) ; return node ; } static node minValueNode ( node node ) { node current = node ; while ( current . left != null ) current = current . left ; return current ; } static node deleteNode ( node root , int key ) { if ( root == null ) return root ; if ( key < root . key ) root . left = deleteNode ( root . left , key ) ; else if ( key > root . key ) root . right = deleteNode ( root . right , key ) ; else { if ( root . count > 1 ) { ( root . count ) -- ; return root ; } if ( root . left == null ) { node temp = root . right ; root = null ; return temp ; } else if ( root . right == null ) { node temp = root . left ; root = null ; return temp ; } node temp = minValueNode ( root . right ) ; root . key = temp . key ; root . right = deleteNode ( root . right , temp . key ) ; } return root ; } public static void main ( String [ ] args ) { node root = null ; root = insert ( root , 12 ) ; root = insert ( root , 10 ) ; root = insert ( root , 20 ) ; root = insert ( root , 9 ) ; root = insert ( root , 11 ) ; root = insert ( root , 10 ) ; root = insert ( root , 12 ) ; root = insert ( root , 12 ) ; System . out . print ( " Inorder ▁ traversal ▁ of ▁ " + " the ▁ given ▁ tree ▁ " + "NEW_LINE"); inorder ( root ) ; System . out . print ( " Delete 20 "); root = deleteNode ( root , 20 ) ; System . out . print ( " Inorder ▁ traversal ▁ of ▁ " + "the modified tree NEW_LINE"); inorder ( root ) ; System . out . print ( " Delete 12 "); root = deleteNode ( root , 12 ) ; System . out . print ( " Inorder ▁ traversal ▁ of ▁ " + "the modified tree NEW_LINE"); inorder ( root ) ; System . out . print ( " Delete 9 "); root = deleteNode ( root , 9 ) ; System . out . print ( " Inorder ▁ traversal ▁ of ▁ " + "the modified tree NEW_LINE"); inorder ( root ) ; } }
class GfG { static class node { int key ; node left , right ; } static node root = null ; static node newNode ( int item ) { node temp = new node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; return temp ; } static void inorder ( node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; inorder ( root . right ) ; } } static node insert ( node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else node . right = insert ( node . right , key ) ; return node ; } static node minValueNode ( node Node ) { node current = Node ; while ( current . left != null ) current = current . left ; return current ; } static node deleteNode ( node root , int key ) { if ( root == null ) return root ; if ( key < root . key ) root . left = deleteNode ( root . left , key ) ; else if ( key > root . key ) root . right = deleteNode ( root . right , key ) ; else { if ( root . left == null ) { node temp = root . right ; return temp ; } else if ( root . right == null ) { node temp = root . left ; return temp ; } node temp = minValueNode ( root . right ) ; root . key = temp . key ; root . right = deleteNode ( root . right , temp . key ) ; } return root ; } static node changeKey ( node root , int oldVal , int newVal ) { root = deleteNode ( root , oldVal ) ; root = insert ( root , newVal ) ; return root ; } public static void main ( String [ ] args ) { root = insert ( root , 50 ) ; root = insert ( root , 30 ) ; root = insert ( root , 20 ) ; root = insert ( root , 40 ) ; root = insert ( root , 70 ) ; root = insert ( root , 60 ) ; root = insert ( root , 80 ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ given ▁ tree " ) ; inorder ( root ) ; root = changeKey ( root , 40 , 10 ) ; System . out . println ( " Inorder traversal of the modified tree "); inorder ( root ) ; } }
import java . util . * ; class GfG { static class Node { int key ; Node left , right ; } static Node newNode ( int ele ) { Node temp = new Node ( ) ; temp . key = ele ; temp . left = null ; temp . right = null ; return temp ; } static void printCommon ( Node root1 , Node root2 ) { Stack < Node > s1 = new Stack < Node > ( ) ; Stack < Node > s2 = new Stack < Node > ( ) ; while ( true ) { if ( root1 != null ) { s1 . push ( root1 ) ; root1 = root1 . left ; } else if ( root2 != null ) { s2 . push ( root2 ) ; root2 = root2 . left ; } else if ( ! s1 . isEmpty ( ) && ! s2 . isEmpty ( ) ) { root1 = s1 . peek ( ) ; root2 = s2 . peek ( ) ; if ( root1 . key == root2 . key ) { System . out . print ( root1 . key + " ▁ " ) ; s1 . pop ( ) ; s2 . pop ( ) ; root1 = root1 . right ; root2 = root2 . right ; } else if ( root1 . key < root2 . key ) { s1 . pop ( ) ; root1 = root1 . right ; root2 = null ; } else if ( root1 . key > root2 . key ) { s2 . pop ( ) ; root2 = root2 . right ; root1 = null ; } } else break ; } } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; inorder ( root . right ) ; } } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } public static void main ( String [ ] args ) { Node root1 = null ; root1 = insert ( root1 , 5 ) ; root1 = insert ( root1 , 1 ) ; root1 = insert ( root1 , 10 ) ; root1 = insert ( root1 , 0 ) ; root1 = insert ( root1 , 4 ) ; root1 = insert ( root1 , 7 ) ; root1 = insert ( root1 , 9 ) ; Node root2 = null ; root2 = insert ( root2 , 10 ) ; root2 = insert ( root2 , 7 ) ; root2 = insert ( root2 , 20 ) ; root2 = insert ( root2 , 4 ) ; root2 = insert ( root2 , 9 ) ; System . out . print ( " Tree ▁ 1 ▁ : ▁ " + "NEW_LINE");  inorder ( root1 ) ; System . out . println ( ) ; System . out . print ( " Tree ▁ 2 ▁ : ▁ " + "NEW_LINE");  inorder ( root2 ) ; System . out . println ( ) ; System . out . println ( " Common ▁ Nodes : ▁ " ) ; printCommon ( root1 , root2 ) ; } }
import java . util . * ; class GFG { static int binarySearch ( int inorder [ ] , int l , int r , int d ) { int mid = ( l + r ) >> 1 ; if ( inorder [ mid ] == d ) return mid ; else if ( inorder [ mid ] > d ) return binarySearch ( inorder , l , mid - 1 , d ) ; else return binarySearch ( inorder , mid + 1 , r , d ) ; } static int ind ; static void leafNodesRec ( int preorder [ ] , int inorder [ ] , int l , int r , int n ) { if ( l == r ) { System . out . printf ( " % d ▁ " , inorder [ l ] ) ; ind = ind + 1 ; return ; } if ( l < 0 l > r r >= n ) return ; int loc = binarySearch ( inorder , l , r , preorder [ ind ] ) ; ind = ind + 1 ; leafNodesRec ( preorder , inorder , l , loc - 1 , n ) ; leafNodesRec ( preorder , inorder , loc + 1 , r , n ) ; } static void leafNodes ( int preorder [ ] , int n ) { int inorder [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) inorder [ i ] = preorder [ i ] ; Arrays . sort ( inorder ) ; leafNodesRec ( preorder , inorder , 0 , n - 1 , n ) ; } public static void main ( String args [ ] ) { int preorder [ ] = { 890 , 325 , 290 , 530 , 965 } ; int n = preorder . length ; leafNodes ( preorder , n ) ; } }
import java . util . * ; class GfG { static void leafNode ( int preorder [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; for ( int i = 0 , j = 1 ; j < n ; i ++ , j ++ ) { boolean found = false ; if ( preorder [ i ] > preorder [ j ] ) s . push ( preorder [ i ] ) ; else { while ( ! s . isEmpty ( ) ) { if ( preorder [ j ] > s . peek ( ) ) { s . pop ( ) ; found = true ; } else break ; } } if ( found ) System . out . print ( preorder [ i ] + " ▁ " ) ; } System . out . println ( preorder [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int preorder [ ] = { 890 , 325 , 290 , 530 , 965 } ; int n = preorder . length ; leafNode ( preorder , n ) ; } }
class GfG { static class Node { int key ; Node left , right , parent ; } static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = null ; temp . right = null ; temp . parent = null ; return temp ; } static void inorder ( Node root ) { if ( root != null ) { inorder ( root . left ) ; System . out . print ( " Node ▁ : ▁ " + root . key + " ▁ , ▁ " ) ; if ( root . parent == null ) System . out . println ( " Parent ▁ : ▁ NULL " ) ; else System . out . println ( " Parent ▁ : ▁ " + root . parent . key ) ; inorder ( root . right ) ; } } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) { Node lchild = insert ( node . left , key ) ; node . left = lchild ; lchild . parent = node ; } else if ( key > node . key ) { Node rchild = insert ( node . right , key ) ; node . right = rchild ; rchild . parent = node ; } return node ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; inorder ( root ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class Index { int index = 0 ; } class BinaryTree { Node root ; Index myindex = new Index ( ) ; Node constructTreeUtil ( int pre [ ] , char preLN [ ] , Index index_ptr , int n , Node temp ) { int index = index_ptr . index ; if ( index == n ) return null ; temp = new Node ( pre [ index ] ) ; ( index_ptr . index ) ++ ; if ( preLN [ index ] == ' N ' ) { temp . left = constructTreeUtil ( pre , preLN , index_ptr , n , temp . left ) ; temp . right = constructTreeUtil ( pre , preLN , index_ptr , n , temp . right ) ; } return temp ; } Node constructTree ( int pre [ ] , char preLN [ ] , int n , Node node ) { int index = 0 ; return constructTreeUtil ( pre , preLN , myindex , n , node ) ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int pre [ ] = new int [ ] { 10 , 30 , 20 , 5 , 15 } ; char preLN [ ] = new char [ ] { ' N ' , ' N ' , ' L ' , ' L ' , ' L ' } ; int n = pre . length ; Node mynode = tree . constructTree ( pre , preLN , n , tree . root ) ; System . out . println ( " Following ▁ is ▁ Inorder ▁ Traversal ▁ of ▁ the " + " Constructed ▁ Binary ▁ Tree : ▁ " ) ; tree . printInorder ( mynode ) ; } }
import java . util . * ; class GFG { static void pairs ( int arr [ ] , int n , int k ) { int smallest = Integer . MAX_VALUE ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( Math . abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = Math . abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if ( Math . abs ( arr [ i ] + arr [ j ] - k ) == smallest ) count ++ ; } System . out . println ( " Minimal ▁ Value ▁ = ▁ " + smallest ) ; System . out . println ( " Total ▁ Pairs ▁ = ▁ " + count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 5 , 1 , 9 , 9 } ; int k = 12 ; int n = arr . length ; pairs ( arr , n , k ) ; } }
class GfG { static class Node { int data ; Node left , right ; int leftSize ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; temp . leftSize = 0 ; return temp ; } static Node insert ( Node root , int data ) { if ( root == null ) return newNode ( data ) ; if ( data <= root . data ) { root . left = insert ( root . left , data ) ; root . leftSize ++ ; } else root . right = insert ( root . right , data ) ; return root ; } static int getRank ( Node root , int x ) { if ( root . data == x ) return root . leftSize ; if ( x < root . data ) { if ( root . left == null ) return - 1 ; else return getRank ( root . left , x ) ; } else { if ( root . right == null ) return - 1 ; else { int rightSize = getRank ( root . right , x ) ; if ( rightSize == - 1 ) return - 1 ; return root . leftSize + 1 + rightSize ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 4 , 4 , 5 , 9 , 7 , 13 , 3 } ; int n = arr . length ; int x = 4 ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) root = insert ( root , arr [ i ] ) ; System . out . println ( " Rank ▁ of ▁ " + x + " ▁ in ▁ stream ▁ is ▁ : ▁ " + getRank ( root , x ) ) ; x = 13 ; System . out . println ( " Rank ▁ of ▁ " + x + " ▁ in ▁ stream ▁ is ▁ : ▁ " + getRank ( root , x ) ) ; } }
class GFG { public static void main ( String [ ] args ) { int a [ ] = { 5 , 1 , 14 , 4 , 15 , 9 , 7 , 20 , 11 } ; int key = 20 ; int arraySize = a . length ; int count = 0 ; for ( int i = 0 ; i < arraySize ; i ++ ) { if ( a [ i ] <= key ) { count += 1 ; } } System . out . println ( " Rank ▁ of ▁ " + key + " ▁ in ▁ stream ▁ is : ▁ " + ( count - 1 ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node buildTree ( int inorder [ ] , int start , int end , Node node ) { if ( start > end ) return null ; int i = max ( inorder , start , end ) ; node = new Node ( inorder [ i ] ) ; if ( start == end ) return node ; node . left = buildTree ( inorder , start , i - 1 , node . left ) ; node . right = buildTree ( inorder , i + 1 , end , node . right ) ; return node ; } int max ( int arr [ ] , int strt , int end ) { int i , max = arr [ strt ] , maxind = strt ; for ( i = strt + 1 ; i <= end ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; maxind = i ; } } return maxind ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; int inorder [ ] = new int [ ] { 5 , 10 , 40 , 30 , 28 } ; int len = inorder . length ; Node mynode = tree . buildTree ( inorder , 0 , len - 1 , tree . root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree ▁ is ▁ " ) ; tree . printInorder ( mynode ) ; } }
import java . util . * ; class solution { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean treeContinuous ( Node ptr ) { if ( ptr == null ) return true ; if ( ptr . left == null && ptr . right == null ) return true ; if ( ptr . left == null ) return ( Math . abs ( ptr . data - ptr . right . data ) == 1 ) && treeContinuous ( ptr . right ) ; if ( ptr . right == null ) return ( Math . abs ( ptr . data - ptr . left . data ) == 1 ) && treeContinuous ( ptr . left ) ; return Math . abs ( ptr . data - ptr . left . data ) == 1 && Math . abs ( ptr . data - ptr . right . data ) == 1 && treeContinuous ( ptr . left ) && treeContinuous ( ptr . right ) ; } public static void main ( String args [ ] ) { Node root = newNode ( 3 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 4 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 3 ) ; root . right . right = newNode ( 5 ) ; if ( treeContinuous ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static int MAX_SIZE = 10 ; static void sortByRow ( Integer mat [ ] [ ] , int n , boolean ascending ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ascending ) Arrays . sort ( mat [ i ] ) ; else Arrays . sort ( mat [ i ] , Collections . reverseOrder ( ) ) ; } } static void transpose ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } static void sortMatRowAndColWise ( Integer mat [ ] [ ] , int n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } static void printMat ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; Integer mat [ ] [ ] = { { 3 , 2 , 1 } , { 9 , 8 , 7 } , { 6 , 5 , 4 } } ; System . out . print ( "Original Matrix:NEW_LINE"); printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; System . out . print ( " Matrix After Sorting : "); printMat ( mat , n ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static void middlesum ( int mat [ ] [ ] , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 ] [ i ] ; System . out . println ( " Sum ▁ of ▁ middle ▁ row ▁ = ▁ " + row_sum ) ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ n / 2 ] ; System . out . println ( " Sum ▁ of ▁ middle ▁ column ▁ = ▁ " + col_sum ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 5 , 7 } , { 3 , 7 , 2 } , { 5 , 6 , 9 } } ; middlesum ( mat , 3 ) ; } }
import java . time . Duration ; import java . time . Instant ; import java . util . * ; class GFG { static int MAX = 10000 ; static int [ ] [ ] arr = new int [ MAX ] [ MAX ] ; static void rowMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ i ] [ j ] ++ ; } } } static void colMajor ( ) { int i , j ; for ( i = 0 ; i < MAX ; i ++ ) { for ( j = 0 ; j < MAX ; j ++ ) { arr [ j ] [ i ] ++ ; } } } public static void main ( String [ ] args ) { Instant start = Instant . now ( ) ; rowMajor ( ) ; Instant end = Instant . now ( ) ; System . out . println ( " Row ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; start = Instant . now ( ) ; colMajor ( ) ; end = Instant . now ( ) ; System . out . printf ( " Column ▁ major ▁ access ▁ time ▁ : ▁ " + Duration . between ( start , end ) ) ; } }
class GFG { static final int M = 3 ; static final int N = 3 ; static void rotateMatrix ( int matrix [ ] [ ] , int k ) { int temp [ ] = new int [ M ] ; k = k % M ; for ( int i = 0 ; i < N ; i ++ ) { for ( int t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i ] [ t ] ; for ( int j = M - k ; j < M ; j ++ ) matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] ; for ( int j = k ; j < M ; j ++ ) matrix [ i ] [ j ] = temp [ j - k ] ; } } static void displayMatrix ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 12 , 23 , 34 } , { 45 , 56 , 67 } , { 78 , 89 , 91 } } ; int k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ; } }
import java . io . * ; class GFG { static int N = 3 ; static void multiply ( int mat [ ] [ ] , int res [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { res [ i ] [ j ] = 0 ; for ( int k = 0 ; k < N ; k ++ ) res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; } } } static boolean InvolutoryMatrix ( int mat [ ] [ ] ) { int res [ ] [ ] = new int [ N ] [ N ] ; multiply ( mat , res ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j && res [ i ] [ j ] != 1 ) return false ; if ( i != j && res [ i ] [ j ] != 0 ) return false ; } } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 0 } , { 0 , - 1 , 0 } , { 0 , 0 , - 1 } } ; if ( InvolutoryMatrix ( mat ) ) System . out . println ( " Involutory ▁ Matrix " ) ; else System . out . println ( " Not ▁ Involutory ▁ Matrix " ) ; } }
class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; void createNode ( int parent [ ] , int i , Node created [ ] ) { if ( created [ i ] != null ) return ; created [ i ] = new Node ( i ) ; if ( parent [ i ] == - 1 ) { root = created [ i ] ; return ; } if ( created [ parent [ i ] ] == null ) createNode ( parent , parent [ i ] , created ) ; Node p = created [ parent [ i ] ] ; if ( p . left == null ) p . left = created [ i ] ; else p . right = created [ i ] ; } Node createTree ( int parent [ ] , int n ) { Node [ ] created = new Node [ n ] ; for ( int i = 0 ; i < n ; i ++ ) created [ i ] = null ; for ( int i = 0 ; i < n ; i ++ ) createNode ( parent , i , created ) ; return root ; } void newLine ( ) { System . out . println ( " " ) ; } void inorder ( Node node ) { if ( node != null ) { inorder ( node . left ) ; System . out . print ( node . key + " ▁ " ) ; inorder ( node . right ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int parent [ ] = new int [ ] { - 1 , 0 , 0 , 1 , 1 , 3 , 5 } ; int n = parent . length ; Node node = tree . createTree ( parent , n ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ constructed ▁ tree ▁ " ) ; tree . inorder ( node ) ; tree . newLine ( ) ; } }
import java . io . * ; public class Sort2DMatrix { static int sortRowWise ( int m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) { for ( int k = 0 ; k < m [ i ] . length - j - 1 ; k ++ ) { if ( m [ i ] [ k ] > m [ i ] [ k + 1 ] ) { int t = m [ i ] [ k ] ; m [ i ] [ k ] = m [ i ] [ k + 1 ] ; m [ i ] [ k + 1 ] = t ; } } } } for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) System . out . print ( m [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int m [ ] [ ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }
import java . io . * ; import java . util . Arrays ; public class Sort2DMatrix { static int sortRowWise ( int m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) Arrays . sort ( m [ i ] ) ; for ( int i = 0 ; i < m . length ; i ++ ) { for ( int j = 0 ; j < m [ i ] . length ; j ++ ) System . out . print ( m [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } return 0 ; } public static void main ( String args [ ] ) { int m [ ] [ ] = { { 9 , 8 , 7 , 1 } , { 7 , 3 , 0 , 2 } , { 9 , 5 , 3 , 2 } , { 6 , 3 , 1 , 2 } } ; sortRowWise ( m ) ; } }
import java . io . * ; public class markov { static boolean checkMarkov ( double m [ ] [ ] ) { for ( int i = 0 ; i < m . length ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < m [ i ] . length ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { double m [ ] [ ] = { { 0 , 0 , 1 } , { 0.5 , 0 , 0.5 } , { 1 , 0 , 0 } } ; if ( checkMarkov ( m ) ) System . out . println ( " ▁ yes ▁ " ) ; else System . out . println ( " ▁ no ▁ " ) ; } }
import java . io . * ; class GFG { static int N = 4 ; static boolean isDiagonalMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 4 , 0 , 0 , 0 } , { 0 , 7 , 0 , 0 } , { 0 , 0 , 5 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isDiagonalMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int N = 4 ; static boolean isScalarMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 2 , 0 , 0 , 0 } , { 0 , 2 , 0 , 0 } , { 0 , 0 , 2 , 0 } , { 0 , 0 , 0 , 2 } } ; if ( isScalarMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; class GFG { static final int MAX_SIZE = 10 ; static void sortByRow ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) Arrays . sort ( mat [ i ] ) ; } static void transpose ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } static void sortMatRowAndColWise ( int mat [ ] [ ] , int n ) { sortByRow ( mat , n ) ; transpose ( mat , n ) ; sortByRow ( mat , n ) ; transpose ( mat , n ) ; } static void printMat ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 4 , 1 , 3 } , { 9 , 6 , 8 } , { 5 , 2 , 7 } } ; int n = 3 ; System . out . print ( "Original Matrix:NEW_LINE"); printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; System . out . print ( " Matrix After Sorting : "); printMat ( mat , n ) ; } }
import java . io . * ; class GFG { static void doublyEven ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) for ( j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * i ) + j + 1 ; for ( i = 0 ; i < n / 4 ; i ++ ) for ( j = 0 ; j < n / 4 ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 0 ; i < n / 4 ; i ++ ) for ( j = 3 * ( n / 4 ) ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 3 * n / 4 ; i < n ; i ++ ) for ( j = 0 ; j < n / 4 ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 3 * n / 4 ; i < n ; i ++ ) for ( j = 3 * n / 4 ; j < n ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = n / 4 ; i < 3 * n / 4 ; i ++ ) for ( j = n / 4 ; j < 3 * n / 4 ; j ++ ) arr [ i ] [ j ] = ( n * n + 1 ) - arr [ i ] [ j ] ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 8 ; doublyEven ( n ) ; } }
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 " ) ; } }
import java . util . * ; class GFG { static final int SIZE = 10 ; static int subCount ( int arr [ ] , int n , int k ) { int mod [ ] = new int [ k ] ; Arrays . fill ( mod , 0 ) ; int cumSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cumSum += arr [ i ] ; mod [ ( ( cumSum % k ) + k ) % k ] ++ ; } int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( mod [ i ] > 1 ) result += ( mod [ i ] * ( mod [ i ] - 1 ) ) / 2 ; result += mod [ 0 ] ; return result ; } static int countSubmatrix ( int mat [ ] [ ] , int n , int k ) { int tot_count = 0 ; int left , right , i ; int temp [ ] = new int [ n ] ; for ( left = 0 ; left < n ; left ++ ) { Arrays . fill ( temp , 0 ) ; for ( right = left ; right < n ; right ++ ) { for ( i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] ; tot_count += subCount ( temp , n , k ) ; } } return tot_count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , - 1 , 6 } , { - 2 , 3 , 8 } , { 7 , 4 , - 9 } } ; int n = 3 , k = 4 ; System . out . print ( " Count ▁ = ▁ " + countSubmatrix ( mat , n , k ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static void ZigZag ( int rows , int columns , int numbers [ ] ) { int k = 0 ; int [ ] [ ] arr = new int [ rows ] [ columns ] ; for ( int i = 0 ; i < rows ; i ++ ) { if ( i % 2 == 0 ) { for ( int j = 0 ; j < columns && numbers [ k ] > 0 ; j ++ ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } else { for ( int j = columns - 1 ; j >= 0 && numbers [ k ] > 0 ; j -- ) { arr [ i ] [ j ] = k + 1 ; numbers [ k ] -- ; if ( numbers [ k ] == 0 ) k ++ ; } } } for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < columns ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String argc [ ] ) { int rows = 4 ; int columns = 5 ; int [ ] Numbers = new int [ ] { 3 , 4 , 2 , 2 , 3 , 1 , 5 } ; ZigZag ( rows , columns , Numbers ) ; } }
import java . io . * ; class GFG { static int numberofPosition ( int n , int k , int x , int y , int obstPosx [ ] , int obstPosy [ ] ) { int d11 , d12 , d21 , d22 , r1 , r2 , c1 , c2 ; d11 = Math . min ( x - 1 , y - 1 ) ; d12 = Math . min ( n - x , n - y ) ; d21 = Math . min ( n - x , y - 1 ) ; d22 = Math . min ( x - 1 , n - y ) ; r1 = y - 1 ; r2 = n - y ; c1 = x - 1 ; c2 = n - x ; for ( int i = 0 ; i < k ; i ++ ) { if ( x > obstPosx [ i ] && y > obstPosy [ i ] && x - obstPosx [ i ] == y - obstPosy [ i ] ) d11 = Math . min ( d11 , x - obstPosx [ i ] - 1 ) ; if ( obstPosx [ i ] > x && obstPosy [ i ] > y && obstPosx [ i ] - x == obstPosy [ i ] - y ) d12 = Math . min ( d12 , obstPosx [ i ] - x - 1 ) ; if ( obstPosx [ i ] > x && y > obstPosy [ i ] && obstPosx [ i ] - x == y - obstPosy [ i ] ) d21 = Math . min ( d21 , obstPosx [ i ] - x - 1 ) ; if ( x > obstPosx [ i ] && obstPosy [ i ] > y && x - obstPosx [ i ] == obstPosy [ i ] - y ) d22 = Math . min ( d22 , x - obstPosx [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] < y ) r1 = Math . min ( r1 , y - obstPosy [ i ] - 1 ) ; if ( x == obstPosx [ i ] && obstPosy [ i ] > y ) r2 = Math . min ( r2 , obstPosy [ i ] - y - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] < x ) c1 = Math . min ( c1 , x - obstPosx [ i ] - 1 ) ; if ( y == obstPosy [ i ] && obstPosx [ i ] > x ) c2 = Math . min ( c2 , obstPosx [ i ] - x - 1 ) ; } return d11 + d12 + d21 + d22 + r1 + r2 + c1 + c2 ; } public static void main ( String [ ] args ) { int n = 8 ; int k = 1 ; int Qposx = 4 ; int Qposy = 4 ; int obstPosx [ ] = { 3 } ; int obstPosy [ ] = { 5 } ; System . out . println ( numberofPosition ( n , k , Qposx , Qposy , obstPosx , obstPosy ) ) ; } }
class GFG { static final int n = 5 ; static int FindMaxProduct ( int arr [ ] [ ] , int n ) { int max = 0 , result ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i ] [ j - 1 ] * arr [ i ] [ j - 2 ] * arr [ i ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j ] * arr [ i - 2 ] [ j ] * arr [ i - 3 ] [ j ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j - 1 ] * arr [ i - 2 ] [ j - 2 ] * arr [ i - 3 ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 1 ) <= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j + 1 ] * arr [ i - 2 ] [ j + 2 ] * arr [ i - 3 ] [ j + 3 ] ; if ( max < result ) max = result ; } } } return max ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 1 } , { 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 1 , 0 } , { 9 , 6 , 4 , 2 , 3 } } ; System . out . print ( FindMaxProduct ( arr , n ) ) ; } }
import java . io . * ; class GFG { public static int maxPro ( int [ ] [ ] a , int n , int m , int k ) { int maxi = 1 , mp = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int wp = 1 ; for ( int l = 0 ; l < k ; ++ l ) { wp *= a [ i ] [ l ] ; } mp = wp ; for ( int j = k ; j < m ; ++ j ) { wp = wp * a [ i ] [ j ] / a [ i ] [ j - k ] ; maxi = Math . max ( maxi , Math . max ( mp , wp ) ) ; } } return maxi ; } public static void main ( String [ ] args ) { int n = 6 , m = 5 , k = 4 ; int [ ] [ ] a = new int [ ] [ ] { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 1 } , { 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 1 , 0 } , { 9 , 6 , 4 , 2 , 3 } , { 1 , 1 , 2 , 1 , 1 } } ; int maxpro = maxPro ( a , n , m , k ) ; System . out . println ( maxpro ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node buildUtil ( int in [ ] , int post [ ] , int inStrt , int inEnd ) { if ( inStrt > inEnd ) return null ; int curr = post [ index ] ; Node node = newNode ( curr ) ; ( index ) -- ; if ( inStrt == inEnd ) return node ; int iIndex = mp . get ( curr ) ; node . right = buildUtil ( in , post , iIndex + 1 , inEnd ) ; node . left = buildUtil ( in , post , inStrt , iIndex - 1 ) ; return node ; } static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int index ; static Node buildTree ( int in [ ] , int post [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) mp . put ( in [ i ] , i ) ; index = len - 1 ; return buildUtil ( in , post , 0 , len - 1 ) ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void preOrder ( Node node ) { if ( node == null ) return ; System . out . printf ( " % d ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { int in [ ] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = in . length ; Node root = buildTree ( in , post , n ) ; System . out . print ( "Preorder of the constructed tree : NEW_LINE"); preOrder ( root ) ; } }
import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int transpose [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) transpose [ i ] [ j ] = mat [ j ] [ i ] ; int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( transpose [ i ] [ j ] != mat [ i ] [ j ] ) flip ++ ; return flip / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }
import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }
import java . io . * ; class Lower_triangular { int N = 4 ; boolean isLowerTriangularMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != 0 ) return false ; return true ; } public static void main ( String args [ ] ) { Lower_triangular ob = new Lower_triangular ( ) ; int mat [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 1 , 4 , 0 , 0 } , { 4 , 6 , 2 , 0 } , { 0 , 4 , 7 , 6 } } ; if ( ob . isLowerTriangularMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { private static final int N = 4 ; public static Boolean isUpperTriangularMatrix ( int mat [ ] [ ] ) { for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != 0 ) return false ; return true ; } public static void main ( String argc [ ] ) { int [ ] [ ] mat = { { 1 , 3 , 5 , 3 } , { 0 , 4 , 6 , 2 } , { 0 , 0 , 2 , 5 } , { 0 , 0 , 0 , 6 } } ; if ( isUpperTriangularMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static final int MAX = 100 ; static void freq ( int ar [ ] [ ] , int m , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { if ( ( ar [ i ] [ j ] % 2 ) == 0 ) ++ even ; else ++ odd ; } } System . out . print ( " ▁ Frequency ▁ of ▁ odd ▁ number ▁ = " + odd + " NEW_LINE"); System . out . print ( " ▁ Frequency ▁ of ▁ even ▁ number ▁ = ▁ " + even + " NEW_LINE"); } public static void main ( String [ ] args ) { int m = 3 , n = 3 ; int array [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; freq ( array , m , n ) ; } }
import java . util . Arrays ; public class GFG { static int MAX = 100 ; static boolean HalfDiagonalSums ( int mat [ ] [ ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else if ( i > n / 2 ) { diag1_right += mat [ i ] [ i ] ; diag2_right += mat [ j ] [ i ] ; } } return ( diag1_left == diag2_right && diag2_right == diag2_left && diag1_right == diag2_left && diag2_right == mat [ n / 2 ] [ n / 2 ] ) ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 2 , 9 , 1 , 4 , - 2 } , { 6 , 7 , 2 , 11 , 4 } , { 4 , 2 , 9 , 2 , 4 } , { 1 , 9 , 2 , 4 , 4 } , { 0 , 2 , 4 , 2 , 5 } } ; System . out . print ( HalfDiagonalSums ( a , 5 ) ? " Yes " : " No " ) ; } }
class GFG { int MAX = 100 ; static boolean isIdentity ( int mat [ ] [ ] , int N ) { for ( int row = 0 ; row < N ; row ++ ) { for ( int col = 0 ; col < N ; col ++ ) { if ( row == col && mat [ row ] [ col ] != 1 ) return false ; else if ( row != col && mat [ row ] [ col ] != 0 ) return false ; } } return true ; } public static void main ( String args [ ] ) { int N = 4 ; int mat [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 1 } } ; if ( isIdentity ( mat , N ) ) System . out . println ( " Yes ▁ " ) ; else System . out . println ( " No ▁ " ) ; } }
import java . io . * ; class Example { final static long mod = 100000007 ; static long modPower ( long a , long t , long mod ) { long now = a , ret = 1 ; while ( t > 0 ) { if ( t % 2 == 1 ) ret = now * ( ret % mod ) ; now = now * ( now % mod ) ; t >>= 1 ; } return ret ; } static long countWays ( int n , int m , int k ) { if ( n == 1 m == 1 ) return 1 ; else if ( ( n + m ) % 2 == 1 && k == - 1 ) return 0 ; return ( modPower ( modPower ( ( long ) 2 , n - 1 , mod ) , m - 1 , mod ) % mod ) ; } public static void main ( String args [ ] ) throws IOException { int n = 2 , m = 7 , k = 1 ; System . out . println ( countWays ( n , m , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static class Node { int data ; Node left , right ; Node ( int x ) { data = x ; left = right = null ; } } static Node buildTree ( int in [ ] , int post [ ] , int n ) { Stack < Node > st = new Stack < > ( ) ; HashSet < Node > s = new HashSet < > ( ) ; int postIndex = n - 1 ; Node root = null ; for ( int p = n - 1 , i = n - 1 ; p >= 0 { Node node = null ; do { node = new Node ( post [ p ] ) ; if ( root == null ) { root = node ; } if ( st . size ( ) > 0 ) { if ( s . contains ( st . peek ( ) ) ) { s . remove ( st . peek ( ) ) ; st . peek ( ) . left = node ; st . pop ( ) ; } else { st . peek ( ) . right = node ; } } st . push ( node ) ; } while ( post [ p -- ] != in [ i ] && p >= 0 ) ; node = null ; while ( st . size ( ) > 0 && i >= 0 && st . peek ( ) . data == in [ i ] ) { node = st . peek ( ) ; st . pop ( ) ; i -- ; } if ( node != null ) { s . add ( node ) ; st . push ( node ) ; } } return root ; } static void preOrder ( Node node ) { if ( node == null ) return ; System . out . printf ( " % d ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } public static void main ( String [ ] args ) { int in [ ] = { 4 , 8 , 2 , 5 , 1 , 6 , 3 , 7 } ; int post [ ] = { 8 , 4 , 5 , 2 , 6 , 7 , 3 , 1 } ; int n = in . length ; Node root = buildTree ( in , post , n ) ; System . out . print ( "Preorder of the constructed tree : NEW_LINE"); preOrder ( root ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static void imageSwap ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j <= i ; j ++ ) mat [ i ] [ j ] = mat [ i ] [ j ] + mat [ j ] [ i ] - ( mat [ j ] [ i ] = mat [ i ] [ j ] ) ; } static void printMatrix ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; imageSwap ( mat , n ) ; printMatrix ( mat , n ) ; } }
class GFG { static final int m = 3 ; static final int n = 2 ; static long countSets ( int a [ ] [ ] ) { long res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int u = 0 , v = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] == 1 ) u ++ ; else v ++ ; } res += Math . pow ( 2 , u ) - 1 + Math . pow ( 2 , v ) - 1 ; } for ( int i = 0 ; i < m ; i ++ ) { int u = 0 , v = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] [ i ] == 1 ) u ++ ; else v ++ ; } res += Math . pow ( 2 , u ) - 1 + Math . pow ( 2 , v ) - 1 ; } return res - ( n * m ) ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 1 , 0 } } ; System . out . print ( countSets ( a ) ) ; } }
class GFG { static int search ( int [ ] [ ] mat , int n , int x ) { if ( n == 0 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) if ( mat [ i ] [ j ] == x ) { System . out . print ( " Element ▁ found ▁ at ▁ ( " + i + " , ▁ " + j + ")NEW_LINE"); return 1 ; } } System . out . print ( " ▁ Element ▁ not ▁ found " ) ; return 0 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; search ( mat , 4 , 29 ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static int R = 5 , C = 4 ; public static void diagonalOrder ( int [ ] [ ] arr , int n , int m ) { ArrayList < ArrayList < Integer > > ans = new ArrayList < ArrayList < Integer > > ( n + m - 1 ) ; for ( int i = 0 ; i < n + m - 1 ; i ++ ) { ans . add ( new ArrayList < Integer > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { ( ans . get ( i + j ) ) . add ( arr [ i ] [ j ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = ans . get ( i ) . size ( ) - 1 ; j >= 0 ; j -- ) { System . out . print ( ans . get ( i ) . get ( j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 4 ; int [ ] [ ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } , { 17 , 18 , 19 , 20 } , } ; diagonalOrder ( arr , n , m ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { private final static int SIZE = 100 ; public static int calculateEnergy ( int mat [ ] [ ] , int n ) { int i_des , j_des , q ; int tot_energy = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { q = mat [ i ] [ j ] / n ; i_des = q ; j_des = mat [ i ] [ j ] - ( n * q ) ; tot_energy += Math . abs ( i_des - i ) + Math . abs ( j_des - j ) ; } } return tot_energy ; } public static void main ( String argc [ ] ) { int [ ] [ ] mat = new int [ ] [ ] { { 4 , 7 , 0 , 3 } , { 8 , 5 , 6 , 1 } , { 9 , 11 , 10 , 2 } , { 15 , 13 , 14 , 12 } } ; int n = 4 ; System . out . println ( " Total ▁ energy ▁ required ▁ = ▁ " + calculateEnergy ( mat , n ) + " ▁ units " ) ; } }
class GFG { static final int MAX = 100 ; static boolean isUnique ( int mat [ ] [ ] , int i , int j , int n , int m ) { int sumrow = 0 ; for ( int k = 0 ; k < m ; k ++ ) { sumrow += mat [ i ] [ k ] ; if ( sumrow > 1 ) return false ; } int sumcol = 0 ; for ( int k = 0 ; k < n ; k ++ ) { sumcol += mat [ k ] [ j ] ; if ( sumcol > 1 ) return false ; } return true ; } static int countUnique ( int mat [ ] [ ] , int n , int m ) { int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] != 0 && isUnique ( mat , i , j , n , m ) ) uniquecount ++ ; return uniquecount ; } static public void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; System . out . print ( countUnique ( mat , 3 , 4 ) ) ; } }
class GFG { static int MAX = 100 ; static int countUnique ( int mat [ ] [ ] , int n , int m ) { int [ ] rowsum = new int [ n ] ; int [ ] colsum = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] != 0 ) { rowsum [ i ] ++ ; colsum [ j ] ++ ; } int uniquecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( mat [ i ] [ j ] != 0 && rowsum [ i ] == 1 && colsum [ j ] == 1 ) uniquecount ++ ; return uniquecount ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 0 , 0 } , { 0 , 0 , 1 , 0 } , { 1 , 0 , 0 , 1 } } ; System . out . print ( countUnique ( mat , 3 , 4 ) ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static boolean isSparse ( int array [ ] [ ] , int m , int n ) { int counter = 0 ; for ( int i = 0 ; i < m ; ++ i ) for ( int j = 0 ; j < n ; ++ j ) if ( array [ i ] [ j ] == 0 ) ++ counter ; return ( counter > ( ( m * n ) / 2 ) ) ; } public static void main ( String args [ ] ) { int array [ ] [ ] = { { 1 , 0 , 3 } , { 0 , 0 , 4 } , { 6 , 0 , 0 } } ; int m = 3 , n = 3 ; if ( isSparse ( array , m , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { int MAX = 100 ; static int countCommon ( int mat [ ] [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( mat [ i ] [ i ] == mat [ i ] [ n - i - 1 ] ) res ++ ; return res ; } public static void main ( String args [ ] ) throws IOException { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; System . out . println ( countCommon ( mat , 3 ) ) ; } }
class GFG { static class NodeTemp { int data ; NodeTemp next , child ; public NodeTemp ( int data ) { this . data = data ; next = child = null ; } } static public NodeTemp addSibling ( NodeTemp node , int data ) { if ( node == null ) return null ; while ( node . next != null ) node = node . next ; return ( node . next = new NodeTemp ( data ) ) ; } static public NodeTemp addChild ( NodeTemp node , int data ) { if ( node == null ) return null ; if ( node . child != null ) return ( addSibling ( node . child , data ) ) ; else return ( node . child = new NodeTemp ( data ) ) ; } static public void traverseTree ( NodeTemp root ) { if ( root == null ) return ; while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; if ( root . child != null ) traverseTree ( root . child ) ; root = root . next ; } } public static void main ( String args [ ] ) { NodeTemp root = new NodeTemp ( 10 ) ; NodeTemp n1 = addChild ( root , 2 ) ; NodeTemp n2 = addChild ( root , 3 ) ; NodeTemp n3 = addChild ( root , 4 ) ; NodeTemp n4 = addChild ( n3 , 6 ) ; NodeTemp n5 = addChild ( root , 5 ) ; NodeTemp n6 = addChild ( n5 , 7 ) ; NodeTemp n7 = addChild ( n5 , 8 ) ; NodeTemp n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }
class GFG { static final int N = 4 ; static void findMax ( int arr [ ] [ ] ) { int row = 0 , i , j ; for ( i = 0 , j = N - 1 ; i < N ; i ++ ) { while ( j >= 0 && arr [ i ] [ j ] == 1 ) { row = i ; j -- ; } } System . out . print ( " Row ▁ number ▁ = ▁ " + ( row + 1 ) ) ; System . out . print ( " , ▁ MaxCount ▁ = ▁ " + ( N - 1 - j ) ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 1 } } ; findMax ( arr ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static void transpose ( int mat [ ] [ ] , int tr [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) tr [ i ] [ j ] = mat [ j ] [ i ] ; } static boolean isSymmetric ( int mat [ ] [ ] , int N ) { int tr [ ] [ ] = new int [ N ] [ MAX ] ; transpose ( mat , tr , N ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != tr [ i ] [ j ] ) return false ; return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 3 , 5 } , { 3 , 2 , 4 } , { 5 , 4 , 1 } } ; if ( isSymmetric ( mat , 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static boolean isSymmetric ( int mat [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 3 , 5 } , { 3 , 2 , 4 } , { 5 , 4 , 1 } } ; if ( isSymmetric ( mat , 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class Cell { int x ; int y ; Cell ( int x , int y ) { this . x = x ; this . y = y ; } } public class MoveCellPerCellValue { public boolean isAllCellTraversed ( Cell grid [ ] [ ] ) { boolean [ ] [ ] visited = new boolean [ grid . length ] [ grid [ 0 ] . length ] ; int total = grid . length * grid [ 0 ] . length ; int startx = grid [ 0 ] [ 0 ] . x ; int starty = grid [ 0 ] [ 0 ] . y ; for ( int i = 0 ; i < total - 2 ; i ++ ) { if ( grid [ startx ] [ starty ] == null ) return false ; if ( visited [ startx ] [ starty ] == true ) return false ; visited [ startx ] [ starty ] = true ; int x = grid [ startx ] [ starty ] . x ; int y = grid [ startx ] [ starty ] . y ; startx = x ; starty = y ; } if ( grid [ startx ] [ starty ] == null ) return true ; return false ; } public static void main ( String args [ ] ) { Cell cell [ ] [ ] = new Cell [ 3 ] [ 2 ] ; cell [ 0 ] [ 0 ] = new Cell ( 0 , 1 ) ; cell [ 0 ] [ 1 ] = new Cell ( 2 , 0 ) ; cell [ 1 ] [ 0 ] = null ; cell [ 1 ] [ 1 ] = new Cell ( 1 , 0 ) ; cell [ 2 ] [ 0 ] = new Cell ( 2 , 1 ) ; cell [ 2 ] [ 1 ] = new Cell ( 1 , 1 ) ; MoveCellPerCellValue mcp = new MoveCellPerCellValue ( ) ; System . out . println ( mcp . isAllCellTraversed ( cell ) ) ; } }
public class PalinPath { public static boolean isPalin ( String str ) { int len = str . length ( ) / 2 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( str . length ( ) - i - 1 ) ) return false ; } return true ; } public static void palindromicPath ( String str , char a [ ] [ ] , int i , int j , int m , int n ) { if ( j < m - 1 i < n - 1 ) { if ( i < n - 1 ) palindromicPath ( str + a [ i ] [ j ] , a , i + 1 , j , m , n ) ; if ( j < m - 1 ) palindromicPath ( str + a [ i ] [ j ] , a , i , j + 1 , m , n ) ; } else { str = str + a [ n - 1 ] [ m - 1 ] ; if ( isPalin ( str ) ) System . out . println ( str ) ; } } public static void main ( String args [ ] ) { char arr [ ] [ ] = { { ' a ' , ' a ' , ' a ' , ' b ' } , { ' b ' , ' a ' , ' a ' , ' a ' } , { ' a ' , ' b ' , ' b ' , ' a ' } } ; String str = " " ; palindromicPath ( str , arr , 0 , 0 , 4 , 3 ) ; } }
public class Main { public static final int n = 4 ; public static final int m = 4 ; static int findPossibleMoves ( int mat [ ] [ ] , int p , int q ) { int X [ ] = { 2 , 1 , - 1 , - 2 , - 2 , - 1 , 1 , 2 } ; int Y [ ] = { 1 , 2 , 2 , 1 , - 1 , - 2 , - 2 , - 1 } ; int count = 0 ; for ( int i = 0 ; i < 8 ; i ++ ) { int x = p + X [ i ] ; int y = q + Y [ i ] ; if ( x >= 0 && y >= 0 && x < n && y < m && mat [ x ] [ y ] == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 1 , 1 } } ; int p = 2 , q = 2 ; System . out . println ( findPossibleMoves ( mat , p , q ) ) ; } }
import java . io . * ; public class GFG { static void printDiagonalSums ( int [ ] [ ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) principal += mat [ i ] [ j ] ; if ( ( i + j ) == ( n - 1 ) ) secondary += mat [ i ] [ j ] ; } } System . out . println ( " Principal ▁ Diagonal : " + principal ) ; System . out . println ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void main ( String [ ] args ) { int [ ] [ ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }
import java . io . * ; public class GFG { static void printDiagonalSums ( int [ ] [ ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { principal += mat [ i ] [ i ] ; secondary += mat [ i ] [ n - i - 1 ] ; } System . out . println ( " Principal ▁ Diagonal : " + principal ) ; System . out . println ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void main ( String [ ] args ) { int [ ] [ ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; Node child ; } ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . next = newNode . child = null ; newNode . data = data ; return newNode ; } static Node addSibling ( Node n , int data ) { if ( n == null ) return null ; while ( n . next != null ) n = n . next ; return ( n . next = newNode ( data ) ) ; } static Node addChild ( Node n , int data ) { if ( n == null ) return null ; if ( n . child != null ) return addSibling ( n . child , data ) ; else return ( n . child = newNode ( data ) ) ; } static void traverseTree ( Node root ) { if ( root == null ) return ; System . out . print ( root . data + " ▁ " ) ; if ( root . child == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; Node curr = root . child ; q . add ( curr ) ; while ( ! q . isEmpty ( ) ) { curr = q . peek ( ) ; q . remove ( ) ; while ( curr != null ) { System . out . print ( curr . data + " ▁ " ) ; if ( curr . child != null ) { q . add ( curr . child ) ; } curr = curr . next ; } } } public static void main ( String [ ] args ) { Node root = newNode ( 10 ) ; Node n1 = addChild ( root , 2 ) ; Node n2 = addChild ( root , 3 ) ; Node n3 = addChild ( root , 4 ) ; Node n4 = addChild ( n3 , 6 ) ; Node n5 = addChild ( root , 5 ) ; Node n6 = addChild ( n5 , 7 ) ; Node n7 = addChild ( n5 , 8 ) ; Node n8 = addChild ( n5 , 9 ) ; traverseTree ( root ) ; } }
class GFG { public static void printBoundary ( int a [ ] [ ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; else if ( i == m - 1 ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; else if ( j == 0 ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; else if ( j == n - 1 ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; else System . out . print ( " ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printBoundary ( a , 4 , 4 ) ; } }
class GFG { public static long getBoundarySum ( int a [ ] [ ] , int m , int n ) { long sum = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == 0 ) sum += a [ i ] [ j ] ; else if ( i == m - 1 ) sum += a [ i ] [ j ] ; else if ( j == 0 ) sum += a [ i ] [ j ] ; else if ( j == n - 1 ) sum += a [ i ] [ j ] ; } } return sum ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; long sum = getBoundarySum ( a , 4 , 4 ) ; System . out . println ( " Sum ▁ of ▁ boundary ▁ elements " + " ▁ is ▁ " + sum ) ; } }
import java . io . * ; class GFG { static void printSpiral ( int [ ] [ ] mat , int r , int c ) { int i , a = 0 , b = 2 ; int low_row = ( 0 > a ) ? 0 : a ; int low_column = ( 0 > b ) ? 0 : b - 1 ; int high_row = ( ( a + 1 ) >= r ) ? r - 1 : a + 1 ; int high_column = ( ( b + 1 ) >= c ) ? c - 1 : b + 1 ; while ( ( low_row > 0 - r && low_column > 0 - c ) ) { for ( i = low_column + 1 ; i <= high_column && i < c && low_row >= 0 ; ++ i ) System . out . print ( mat [ low_row ] [ i ] + " ▁ " ) ; low_row -= 1 ; for ( i = low_row + 2 ; i <= high_row && i < r && high_column < c ; ++ i ) System . out . print ( mat [ i ] [ high_column ] + " ▁ " ) ; high_column += 1 ; for ( i = high_column - 2 ; i >= low_column && i >= 0 && high_row < r ; -- i ) System . out . print ( mat [ high_row ] [ i ] + " ▁ " ) ; high_row += 1 ; for ( i = high_row - 2 ; i > low_row && i >= 0 && low_column >= 0 ; -- i ) System . out . print ( mat [ i ] [ low_column ] + " ▁ " ) ; low_column -= 1 ; } System . out . println ( ) ; } static public void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int r = 3 , c = 3 ; printSpiral ( mat , r , c ) ; } }
class GFG { public static int difference ( int arr [ ] [ ] , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) d1 += arr [ i ] [ j ] ; if ( i == n - j - 1 ) d2 += arr [ i ] [ j ] ; } } return Math . abs ( d1 - d2 ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] [ ] = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , - 12 } } ; System . out . print ( difference ( arr , n ) ) ; } }
class GFG { public static int difference ( int arr [ ] [ ] , int n ) { int d1 = 0 , d2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { d1 += arr [ i ] [ i ] ; d2 += arr [ i ] [ n - i - 1 ] ; } return Math . abs ( d1 - d2 ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] [ ] = { { 11 , 2 , 4 } , { 4 , 5 , 6 } , { 10 , 8 , - 12 } } ; System . out . print ( difference ( arr , n ) ) ; } }
class GFG { static int MAX = 100 ; static void spiralFill ( int m , int n , int a [ ] [ ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = val ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 ] [ i ] = val ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i ] [ l ] = val ++ ; } l ++ ; } } } public static void main ( String [ ] args ) { int m = 4 , n = 4 ; int a [ ] [ ] = new int [ MAX ] [ MAX ] ; spiralFill ( m , n , a ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( " " ) ; } } }
class GFG { static void printTreeEdges ( int prufer [ ] , int m ) { int vertices = m + 2 ; int vertex_set [ ] = new int [ vertices ] ; for ( int i = 0 ; i < vertices ; i ++ ) vertex_set [ i ] = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) vertex_set [ prufer [ i ] - 1 ] += 1 ; System . out . print ( " The edge set E ( G ) is : "); int j = 0 ; for ( int i = 0 ; i < vertices - 2 ; i ++ ) { for ( j = 0 ; j < vertices ; j ++ ) { if ( vertex_set [ j ] == 0 ) { vertex_set [ j ] = - 1 ; System . out . print ( " ( " + ( j + 1 ) + " , ▁ " + prufer [ i ] + " ) ▁ " ) ; vertex_set [ prufer [ i ] - 1 ] -- ; break ; } } } j = 0 ; for ( int i = 0 ; i < vertices ; i ++ ) { if ( vertex_set [ i ] == 0 && j == 0 ) { System . out . print ( " ( " + ( i + 1 ) + " , ▁ " ) ; j ++ ; } else if ( vertex_set [ i ] == 0 && j == 1 ) System . out . print ( ( i + 1 ) + ")NEW_LINE"); } } public static void main ( String args [ ] ) { int prufer [ ] = { 4 , 1 , 3 , 4 } ; int n = prufer . length ; printTreeEdges ( prufer , n ) ; } }
class GFG { static final int MAX = 100 ; static void maxMin ( int arr [ ] [ ] , int n ) { int min = + 2147483647 ; int max = - 2147483648 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n / 2 ; j ++ ) { if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) { if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ] ; if ( max < arr [ i ] [ j ] ) max = arr [ i ] [ j ] ; } else { if ( min > arr [ i ] [ j ] ) min = arr [ i ] [ j ] ; if ( max < arr [ i ] [ n - j - 1 ] ) max = arr [ i ] [ n - j - 1 ] ; } } } System . out . print ( " Maximum ▁ = ▁ " + max + " , ▁ Minimum ▁ = ▁ " + min ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 5 , 9 , 11 } , { 25 , 0 , 14 } , { 21 , 6 , 4 } } ; maxMin ( arr , 3 ) ; } }
import java . util . * ; class GFG { public static void antiSpiralTraversal ( int m , int n , int a [ ] [ ] ) { int i , k = 0 , l = 0 ; Stack < Integer > stk = new Stack < Integer > ( ) ; while ( k <= m && l <= n ) { for ( i = l ; i <= n ; ++ i ) stk . push ( a [ k ] [ i ] ) ; k ++ ; for ( i = k ; i <= m ; ++ i ) stk . push ( a [ i ] [ n ] ) ; n -- ; if ( k <= m ) { for ( i = n ; i >= l ; -- i ) stk . push ( a [ m ] [ i ] ) ; m -- ; } if ( l <= n ) { for ( i = m ; i >= k ; -- i ) stk . push ( a [ i ] [ l ] ) ; l ++ ; } } while ( ! stk . empty ( ) ) { System . out . print ( stk . peek ( ) + " ▁ " ) ; stk . pop ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } , { 11 , 12 , 13 , 14 , 15 } , { 16 , 17 , 18 , 19 , 20 } } ; antiSpiralTraversal ( mat . length - 1 , mat [ 0 ] . length - 1 , mat ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static int findNormal ( int mat [ ] [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] * mat [ i ] [ j ] ; return ( int ) Math . sqrt ( sum ) ; } static int findTrace ( int mat [ ] [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += mat [ i ] [ i ] ; return sum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 , 4 } , { 5 , 5 , 5 , 5 , 5 } , } ; System . out . println ( " Trace ▁ of ▁ Matrix ▁ = ▁ " + findTrace ( mat , 5 ) ) ; System . out . println ( " Normal ▁ of ▁ Matrix ▁ = ▁ " + findNormal ( mat , 5 ) ) ; } }
import java . io . * ; class GFG { static int ROW = 3 ; static int COL = 3 ; static void transpose ( int transpose_matrix [ ] [ ] , int matrix [ ] [ ] ) { for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) transpose_matrix [ j ] [ i ] = matrix [ i ] [ j ] ; } static boolean check ( int transpose_matrix [ ] [ ] , int matrix [ ] [ ] ) { for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( matrix [ i ] [ j ] != - transpose_matrix [ i ] [ j ] ) return false ; return true ; } static void printMatrix ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 0 , 5 , - 4 } , { - 5 , 0 , 1 } , { 4 , - 1 , 0 } , } ; int transpose_matrix [ ] [ ] = new int [ ROW ] [ COL ] ; transpose ( transpose_matrix , matrix ) ; System . out . println ( " Transpose ▁ matrix : ▁ " ) ; printMatrix ( transpose_matrix ) ; if ( check ( transpose_matrix , matrix ) ) System . out . println ( " Skew ▁ Symmetric ▁ Matrix " ) ; else System . out . println ( " Not ▁ Skew ▁ Symmetric ▁ Matrix " ) ; } }
import java . io . * ; class GFG { static int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( findSum ( N ) ) ; } }
import java . io . * ; class GFG { static int countOps ( int A [ ] [ ] , int B [ ] [ ] , int m , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 1 ; j < m ; j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) return - 1 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += Math . abs ( A [ i ] [ 0 ] ) ; for ( int j = 0 ; j < m ; j ++ ) result += Math . abs ( A [ 0 ] [ j ] - A [ 0 ] [ 0 ] ) ; return ( result ) ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int B [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; System . out . println ( countOps ( A , B , 3 , 3 ) ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Vector < Node > child ; } ; static Node newNode ( int value ) { Node nNode = new Node ( ) ; nNode . key = value ; nNode . child = new Vector < Node > ( ) ; return nNode ; } static int ind ; static Node BuildKaryTree ( int A [ ] , int n , int k , int h ) { if ( n <= 0 ) return null ; Node nNode = newNode ( A [ ind ] ) ; if ( nNode == null ) { System . out . println ( " Memory ▁ error " ) ; return null ; } for ( int i = 0 ; i < k ; i ++ ) { if ( ind < n - 1 && h > 1 ) { ind ++ ; nNode . child . add ( BuildKaryTree ( A , n , k , h - 1 ) ) ; } else { nNode . child . add ( null ) ; } } return nNode ; } static Node BuildKaryTree_1 ( int [ ] A , int n , int k , int in ) { int height = ( int ) Math . ceil ( Math . log ( ( double ) n * ( k - 1 ) + 1 ) / Math . log ( ( double ) k ) ) ; ind = in ; return BuildKaryTree ( A , n , k , height ) ; } static void postord ( Node root , int k ) { if ( root == null ) return ; for ( int i = 0 ; i < k ; i ++ ) postord ( root . child . get ( i ) , k ) ; System . out . print ( root . key + " ▁ " ) ; } public static void main ( String args [ ] ) { int ind = 0 ; int k = 3 , n = 10 ; int preorder [ ] = { 1 , 2 , 5 , 6 , 7 , 3 , 8 , 9 , 10 , 4 } ; Node root = BuildKaryTree_1 ( preorder , n , k , ind ) ; System . out . println ( " Postorder ▁ traversal ▁ of ▁ " + " constructed ▁ full ▁ k - ary ▁ tree ▁ is : ▁ " ) ; postord ( root , k ) ; System . out . println ( ) ; } }
class GFG { static void printCoils ( int n ) { int m = 8 * n * n ; int coil1 [ ] = new int [ m ] ; coil1 [ 0 ] = 8 * n * n + 2 * n ; int curr = coil1 [ 0 ] ; int nflg = 1 , step = 2 ; int index = 1 ; while ( index < m ) { for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = ( curr - 4 * n * nflg ) ; if ( index >= m ) break ; } if ( index >= m ) break ; for ( int i = 0 ; i < step ; i ++ ) { curr = coil1 [ index ++ ] = curr + nflg ; if ( index >= m ) break ; } nflg = nflg * ( - 1 ) ; step += 2 ; } int coil2 [ ] = new int [ m ] ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) coil2 [ i ] = 16 * n * n + 1 - coil1 [ i ] ; System . out . print ( " Coil ▁ 1 ▁ : ▁ " ) ; for ( int i = 0 ; i < 8 * n * n ; i ++ ) System . out . print ( coil1 [ i ] + " ▁ " ) ; System . out . print ( " Coil 2 : "); for ( int i = 0 ; i < 8 * n * n ; i ++ ) System . out . print ( coil2 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 1 ; printCoils ( n ) ; } }
import java . io . * ; public class GFG { static int findSum ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = Math . abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }
import java . io . * ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += i * ( n - i ) ; return 2 * sum ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }
import java . io . * ; public class GFG { static int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }
import java . io . * ; public class GFG { static void checkHV ( int [ ] [ ] arr , int N , int M ) { boolean horizontal = true ; boolean vertical = true ; for ( int i = 0 , k = N - 1 ; i < N / 2 ; i ++ , k -- ) { for ( int j = 0 ; j < M ; j ++ ) { if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) { horizontal = false ; break ; } } } for ( int i = 0 , k = M - 1 ; i < M / 2 ; i ++ , k -- ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] != arr [ k ] [ j ] ) { horizontal = false ; break ; } } } if ( ! horizontal && ! vertical ) System . out . println ( " NO " ) ; else if ( horizontal && ! vertical ) System . out . println ( " HORIZONTAL " ) ; else if ( vertical && ! horizontal ) System . out . println ( " VERTICAL " ) ; else System . out . println ( " BOTH " ) ; } static public void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 0 , 1 } , { 0 , 0 , 0 } , { 1 , 0 , 1 } } ; checkHV ( mat , 3 , 3 ) ; } }
import java . io . * ; public class GFG { static int maxDet ( int n ) { return ( 2 * n * n * n ) ; } void resMatrix ( int n ) { for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( i == 0 && j == 2 ) System . out . print ( "0 ▁ " ) ; else if ( i == 1 && j == 0 ) System . out . print ( "0 ▁ " ) ; else if ( i == 2 && j == 1 ) System . out . print ( "0 ▁ " ) ; else System . out . print ( n + " ▁ " ) ; } System . out . println ( " " ) ; } } static public void main ( String [ ] args ) { int n = 15 ; GFG geeks = new GFG ( ) ; System . out . println ( " Maximum ▁ Determinant ▁ = ▁ " + maxDet ( n ) ) ; System . out . println ( " Resultant ▁ Matrix ▁ : " ) ; geeks . resMatrix ( n ) ; } }
class GFG { static final int R = 3 ; static final int C = 5 ; static int numofneighbour ( int mat [ ] [ ] , int i , int j ) { int count = 0 ; if ( i > 0 && mat [ i - 1 ] [ j ] == 1 ) count ++ ; if ( j > 0 && mat [ i ] [ j - 1 ] == 1 ) count ++ ; if ( i < R - 1 && mat [ i + 1 ] [ j ] == 1 ) count ++ ; if ( j < C - 1 && mat [ i ] [ j + 1 ] == 1 ) count ++ ; return count ; } static int findperimeter ( int mat [ ] [ ] ) { int perimeter = 0 ; for ( int i = 0 ; i < R ; i ++ ) for ( int j = 0 ; j < C ; j ++ ) if ( mat [ i ] [ j ] == 1 ) perimeter += ( 4 - numofneighbour ( mat , i , j ) ) ; return perimeter ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 0 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } } ; System . out . println ( findperimeter ( mat ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void preOrder ( Node node ) { if ( node == null ) return ; System . out . printf ( " % d ▁ " , node . data ) ; preOrder ( node . left ) ; preOrder ( node . right ) ; } static int findIndex ( String str , int si , int ei ) { if ( si > ei ) return - 1 ; Stack < Character > s = new Stack < > ( ) ; for ( int i = si ; i <= ei ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) s . add ( str . charAt ( i ) ) ; else if ( str . charAt ( i ) == ' ) ' ) { if ( s . peek ( ) == ' ( ' ) { s . pop ( ) ; if ( s . isEmpty ( ) ) return i ; } } } return - 1 ; } static Node treeFromString ( String str , int si , int ei ) { if ( si > ei ) return null ; Node root = newNode ( str . charAt ( si ) - '0' ) ; int index = - 1 ; if ( si + 1 <= ei && str . charAt ( si + 1 ) == ' ( ' ) index = findIndex ( str , si + 1 , ei ) ; if ( index != - 1 ) { root . left = treeFromString ( str , si + 2 , index - 1 ) ; root . right = treeFromString ( str , index + 2 , ei - 1 ) ; } return root ; } public static void main ( String [ ] args ) { String str = "4(2(3 ) ( 1 ) ) (6(5 ) ) " ; Node root = treeFromString ( str , 0 , str . length ( ) - 1 ) ; preOrder ( root ) ; } }
import java . util . * ; class GFG { static class node { int val ; node left ; node right ; node ( ) { this . val = 0 ; this . left = null ; this . right = null ; } node ( int x ) { this . val = x ; this . left = null ; this . right = null ; } node ( int x , node left , node right ) { this . val = x ; this . left = left ; this . right = right ; } } ; static boolean continuous ( node root ) { if ( root == null ) return false ; int flag = 1 ; Queue < node > Q = new LinkedList < > ( ) ; Q . add ( root ) ; node temp ; while ( ! Q . isEmpty ( ) ) { temp = Q . peek ( ) ; Q . remove ( ) ; if ( temp . left != null ) { if ( Math . abs ( temp . left . val - temp . val ) == 1 ) Q . add ( temp . left ) ; else { flag = 0 ; break ; } } if ( temp . right != null ) { if ( Math . abs ( temp . right . val - temp . val ) == 1 ) Q . add ( temp . right ) ; else { flag = 0 ; break ; } } } if ( flag != 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { node root = new node ( 3 ) ; root . left = new node ( 2 ) ; root . right = new node ( 4 ) ; root . left . left = new node ( 1 ) ; root . left . right = new node ( 3 ) ; root . right . right = new node ( 5 ) ; if ( continuous ( root ) ) System . out . print ( "TrueNEW_LINE"); else System . out . print ( "FalseNEW_LINE"); } }
public class MatrixDiag { public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 , mode = 0 , it = 0 , lower = 0 ; for ( int t = 0 ; t < ( 2 * n - 1 ) ; t ++ ) { int t1 = t ; if ( t1 >= n ) { mode ++ ; t1 = n - 1 ; it -- ; lower ++ ; } else { lower = 0 ; it ++ ; } for ( int i = t1 ; i >= lower ; i -- ) { if ( ( t1 + mode ) % 2 == 0 ) { System . out . println ( mat [ i ] [ t1 + lower - i ] ) ; } else { System . out . println ( mat [ t1 + lower - i ] [ i ] ) ; } } } } }
class GFG { static final int MAX = 100 ; static int maxRowDiff ( int mat [ ] [ ] , int m , int n ) { int rowSum [ ] = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] ; rowSum [ i ] = sum ; } int max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; int min_element = rowSum [ 0 ] ; for ( int i = 1 ; i < m ; i ++ ) { if ( rowSum [ i ] - min_element > max_diff ) max_diff = rowSum [ i ] - min_element ; if ( rowSum [ i ] < min_element ) min_element = rowSum [ i ] ; } return max_diff ; } public static void main ( String [ ] args ) { int m = 5 , n = 4 ; int mat [ ] [ ] = { { - 1 , 2 , 3 , 4 } , { 5 , 3 , - 2 , 1 } , { 6 , 7 , 2 , - 3 } , { 2 , 9 , 1 , 4 } , { 2 , 1 , - 2 , 0 } } ; System . out . print ( maxRowDiff ( mat , m , n ) ) ; } }
class GFG { static void preprocessMatrix ( int rows [ ] , int cols [ ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { rows [ i ] = i ; } for ( int i = 0 ; i < n ; i ++ ) { cols [ i ] = i ; } } static void queryMatrix ( int rows [ ] , int cols [ ] , int m , int n , char ch , int x , int y ) { int tmp ; switch ( ch ) { case ' R ' : swap ( rows , x - 1 , y - 1 ) ; break ; case ' C ' : swap ( cols , x - 1 , y - 1 ) ; break ; case ' P ' : System . out . printf ( "value at (%d, %d) = %dNEW_LINE", x, y, rows[x - 1] * n + cols[y - 1] + 1); break ; } return ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int m = 1234 , n = 5678 ; int rows [ ] = new int [ m ] , cols [ ] = new int [ n ] ; preprocessMatrix ( rows , cols , m , n ) ; queryMatrix ( rows , cols , m , n , ' R ' , 1 , 2 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 1 , 1 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 2 , 1 ) ; queryMatrix ( rows , cols , m , n , ' C ' , 1 , 2 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 1 , 1 ) ; queryMatrix ( rows , cols , m , n , ' P ' , 2 , 1 ) ; } }
class GFG { static final int MAX = 1000 ; static int maxXOR ( int mat [ ] [ ] , int N ) { int r_xor , c_xor ; int max_xor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { r_xor = 0 ; c_xor = 0 ; for ( int j = 0 ; j < N ; j ++ ) { r_xor = r_xor ^ mat [ i ] [ j ] ; c_xor = c_xor ^ mat [ j ] [ i ] ; } if ( max_xor < Math . max ( r_xor , c_xor ) ) max_xor = Math . max ( r_xor , c_xor ) ; } return max_xor ; } public static void main ( String [ ] args ) { int N = 3 ; int mat [ ] [ ] = { { 1 , 5 , 4 } , { 3 , 7 , 2 } , { 5 , 9 , 10 } } ; System . out . print ( " maximum ▁ XOR ▁ value ▁ : ▁ " + maxXOR ( mat , N ) ) ; } }
import java . util . * ; class GFG { static int maximumMirrorInMatrix ( String mat [ ] , int N ) { int [ ] horizontal = new int [ N ] ; int [ ] vertical = new int [ N ] ; Arrays . fill ( horizontal , - 1 ) ; Arrays . fill ( vertical , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] . charAt ( j ) == ' B ' ) { continue ; } horizontal [ i ] = j ; break ; } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] . charAt ( j ) == ' B ' ) { continue ; } vertical [ j ] = i ; break ; } } int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i > vertical [ j ] && j > horizontal [ i ] ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int N = 5 ; String mat [ ] = { " BBOBB " , " BBBBO " , " BBBBB " , " BOOBO " , " BBBOB " } ; System . out . println ( maximumMirrorInMatrix ( mat , N ) ) ; } }
import java . io . * ; class GFG { static void direction ( int R , int C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { System . out . println ( " Left " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { System . out . println ( " Up " ) ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { System . out . println ( " Right " ) ; return ; } if ( R == C && R % 2 == 0 && C % 2 == 0 ) { System . out . println ( " Left " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R < C ) { System . out . println ( " Right " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 != 0 && R > C ) { System . out . println ( " Down " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R < C ) { System . out . println ( " Left " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 == 0 && R > C ) { System . out . println ( " Up " ) ; return ; } if ( R != C && R % 2 == 0 && C % 2 != 0 && R > C ) { System . out . println ( " Down " ) ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R < C ) { System . out . println ( " Right " ) ; return ; } } public static void main ( String [ ] args ) { int R = 3 , C = 1 ; direction ( R , C ) ; } }
class GFG { static int MAX = 100 ; static int findK ( int A [ ] [ ] , int i , int j , int n , int m , int k ) { if ( n < 1 m < 1 ) return - 1 ; if ( k <= m ) return A [ i + 0 ] [ j + k - 1 ] ; if ( k <= ( m + n - 1 ) ) return A [ i + ( k - m ) ] [ j + m - 1 ] ; if ( k <= ( m + n - 1 + m - 1 ) ) return A [ i + n - 1 ] [ j + m - 1 - ( k - ( m + n - 1 ) ) ] ; if ( k <= ( m + n - 1 + m - 1 + n - 2 ) ) return A [ i + n - 1 - ( k - ( m + n - 1 + m - 1 ) ) ] [ j + 0 ] ; return findK ( A , i + 1 , j + 1 , n - 2 , m - 2 , k - ( 2 * n + 2 * m - 4 ) ) ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 , 17 , 18 } } ; int k = 17 ; System . out . println ( findK ( a , 0 , 0 , 3 , 6 , k ) ) ; } }
import java . io . * ; class GFG { public static int N = 5 ; public static int M = 4 ; static boolean checkDiagonal ( int mat [ ] [ ] , int i , int j ) { int res = mat [ i ] [ j ] ; while ( ++ i < N && ++ j < M ) { if ( mat [ i ] [ j ] != res ) return false ; } return true ; } static boolean isToepliz ( int mat [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) { if ( ! checkDiagonal ( mat , 0 , i ) ) return false ; } for ( int i = 1 ; i < N ; i ++ ) { if ( ! checkDiagonal ( mat , i , 0 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 6 , 7 , 8 , 9 } , { 4 , 6 , 7 , 8 } , { 1 , 4 , 6 , 7 } , { 0 , 1 , 4 , 6 } , { 2 , 0 , 1 , 4 } } ; if ( isToepliz ( mat ) ) System . out . println ( " Matrix ▁ is ▁ a ▁ Toepliz ▁ " ) ; else System . out . println ( " Matrix ▁ is ▁ not ▁ a ▁ Toepliz ▁ " ) ; } }
import java . util . * ; class GFG { static boolean isToeplitz ( int [ ] [ ] matrix ) { int row = matrix . length ; int col = matrix [ 0 ] . length ; HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { int key = i - j ; if ( map . containsKey ( key ) ) { if ( map . get ( key ) != matrix [ i ] [ j ] ) return false ; } else { map . put ( i - j , matrix [ i ] [ j ] ) ; } } } return true ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 12 , 23 , - 32 } , { - 20 , 12 , 23 } , { 56 , - 20 , 12 } , { 38 , 56 , - 20 } } ; String result = ( isToeplitz ( matrix ) ) ? " Yes " : " No " ; System . out . println ( result ) ; } }
import java . io . * ; class GFG { public static int N = 5 ; static int countZeroes ( int mat [ ] [ ] ) { int row = N - 1 , col = 0 ; int count = 0 ; while ( col < N ) { while ( mat [ row ] [ col ] > 0 ) if ( -- row < 0 ) return count ; count += ( row + 1 ) ; col ++ ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 1 } , { 0 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 } } ; System . out . println ( countZeroes ( mat ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; int i = 0 ; int j = m - 1 ; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1 ; i += 1 ; } else j -= 1 ; } return count ; } public static void main ( String [ ] args ) { int M [ ] [ ] = { { - 3 , - 2 , - 1 , 1 } , { - 2 , 2 , 3 , 4 } , { 4 , 5 , 7 , 8 } } ; System . out . println ( countNegative ( M , 3 , 4 ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int getLastNegativeIndex ( int array [ ] , int start , int end ) { if ( start == end ) { return start ; } int mid = start + ( end - start ) / 2 ; if ( array [ mid ] < 0 ) { if ( mid + 1 < array . length && array [ mid + 1 ] >= 0 ) { return mid ; } return getLastNegativeIndex ( array , mid + 1 , end ) ; } else { return getLastNegativeIndex ( array , start , mid - 1 ) ; } } static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; int nextEnd = m - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( M [ i ] [ 0 ] >= 0 ) { break ; } nextEnd = getLastNegativeIndex ( M [ i ] , 0 , nextEnd ) ; count += nextEnd + 1 ; } return count ; } public static void main ( String [ ] args ) { int M [ ] [ ] = { { - 3 , - 2 , - 1 , 1 } , { - 2 , 2 , 3 , 4 } , { 4 , 5 , 7 , 8 } } ; int r = M . length ; int c = M [ 0 ] . length ; System . out . println ( countNegative ( M , r , c ) ) ; } }
import java . io . * ; class GFG { static StringBuilder findLeft ( StringBuilder str ) { int n = str . length ( ) ; while ( n > 0 ) { n -- ; if ( str . charAt ( n ) == ' d ' ) { str . setCharAt ( n , ' c ' ) ; break ; } if ( str . charAt ( n ) == ' b ' ) { str . setCharAt ( n , ' a ' ) ; break ; } if ( str . charAt ( n ) == ' a ' ) str . setCharAt ( n , ' b ' ) ; else if ( str . charAt ( n ) == ' c ' ) str . setCharAt ( n , ' d ' ) ; } return str ; } public static void main ( String [ ] args ) { StringBuilder str = new StringBuilder ( " aacbddc " ) ; System . out . print ( " Left ▁ of ▁ " + str + " ▁ is ▁ " + findLeft ( str ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printSpiral ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int x ; x = Math . min ( Math . min ( i , j ) , Math . min ( n - 1 - i , n - 1 - j ) ) ; if ( i <= j ) System . out . print ( ( n - 2 * x ) * ( n - 2 * x ) - ( i - x ) - ( j - x ) + " TABSYMBOL " ) ; else System . out . print ( ( n - 2 * x - 2 ) * ( n - 2 * x - 2 ) + ( i - x ) + ( j - x ) + " TABSYMBOL " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 5 ; printSpiral ( n ) ; } }
import java . util . * ; class GFG { static int ROW = 9 ; static int COL = 10 ; static class Point { int x ; int y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static class queueNode { Point pt ; int dist ; public queueNode ( Point pt , int dist ) { this . pt = pt ; this . dist = dist ; } } ; static boolean isValid ( int row , int col ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) ; } static int rowNum [ ] = { - 1 , 0 , 0 , 1 } ; static int colNum [ ] = { 0 , - 1 , 1 , 0 } ; static int BFS ( int mat [ ] [ ] , Point src , Point dest ) { if ( mat [ src . x ] [ src . y ] != 1 mat [ dest . x ] [ dest . y ] != 1 ) return - 1 ; boolean [ ] [ ] visited = new boolean [ ROW ] [ COL ] ; visited [ src . x ] [ src . y ] = true ; Queue < queueNode > q = new LinkedList < > ( ) ; queueNode s = new queueNode ( src , 0 ) ; q . add ( s ) ; while ( ! q . isEmpty ( ) ) { queueNode curr = q . peek ( ) ; Point pt = curr . pt ; if ( pt . x == dest . x && pt . y == dest . y ) return curr . dist ; q . remove ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) { int row = pt . x + rowNum [ i ] ; int col = pt . y + colNum [ i ] ; if ( isValid ( row , col ) && mat [ row ] [ col ] == 1 && ! visited [ row ] [ col ] ) { visited [ row ] [ col ] = true ; queueNode Adjcell = new queueNode ( new Point ( row , col ) , curr . dist + 1 ) ; q . add ( Adjcell ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } , { 1 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 } , { 1 , 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 0 } , { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 1 } } ; Point source = new Point ( 0 , 0 ) ; Point dest = new Point ( 3 , 4 ) ; int dist = BFS ( mat , source , dest ) ; if ( dist != - 1 ) System . out . println ( " Shortest ▁ Path ▁ is ▁ " + dist ) ; else System . out . println ( " Shortest ▁ Path ▁ doesn ' t ▁ exist " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findMaxValue ( int N , int mat [ ] [ ] ) { int maxValue = Integer . MIN_VALUE ; for ( int a = 0 ; a < N - 1 ; a ++ ) for ( int b = 0 ; b < N - 1 ; b ++ ) for ( int d = a + 1 ; d < N ; d ++ ) for ( int e = b + 1 ; e < N ; e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxValue = mat [ d ] [ e ] - mat [ a ] [ b ] ; return maxValue ; } public static void main ( String [ ] args ) { int N = 5 ; int mat [ ] [ ] = { { 1 , 2 , - 1 , - 4 , - 20 } , { - 8 , - 3 , 4 , 2 , 1 } , { 3 , 8 , 6 , 1 , 3 } , { - 4 , - 1 , 1 , 7 , - 6 } , { 0 , - 4 , 10 , - 5 , 1 } } ; System . out . print ( " Maximum ▁ Value ▁ is ▁ " + findMaxValue ( N , mat ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; Node bintree2listUtil ( Node node ) { if ( node == null ) return node ; if ( node . left != null ) { Node left = bintree2listUtil ( node . left ) ; for ( ; left . right != null ; left = left . right ) ; left . right = node ; node . left = left ; } if ( node . right != null ) { Node right = bintree2listUtil ( node . right ) ; for ( ; right . left != null ; right = right . left ) ; right . left = node ; node . right = right ; } return node ; } Node bintree2list ( Node node ) { if ( node == null ) return node ; node = bintree2listUtil ( node ) ; while ( node . left != null ) node = node . left ; return node ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; Node head = tree . bintree2list ( tree . root ) ; tree . printList ( head ) ; } }
class GFG { public static void modifyMatrix ( int mat [ ] [ ] , int R , int C ) { int row [ ] = new int [ R ] ; int col [ ] = new int [ C ] ; int i , j ; for ( i = 0 ; i < R ; i ++ ) { row [ i ] = 0 ; } for ( i = 0 ; i < C ; i ++ ) { col [ i ] = 0 ; } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] = 1 ; col [ j ] = 1 ; } } } for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { if ( row [ i ] == 1 col [ j ] == 1 ) { mat [ i ] [ j ] = 1 ; } } } } public static void printMatrix ( int mat [ ] [ ] , int R , int C ) { int i , j ; for ( i = 0 ; i < R ; i ++ ) { for ( j = 0 ; j < C ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } , } ; System . out . println ( " Matrix ▁ Intially " ) ; printMatrix ( mat , 3 , 4 ) ; modifyMatrix ( mat , 3 , 4 ) ; System . out . println ( " Matrix ▁ after ▁ modification ▁ n " ) ; printMatrix ( mat , 3 , 4 ) ; } }
class GFG { public static void modifyMatrix ( int mat [ ] [ ] ) { boolean row_flag = false ; boolean col_flag = false ; for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) { if ( i == 0 && mat [ i ] [ j ] == 1 ) row_flag = true ; if ( j == 0 && mat [ i ] [ j ] == 1 ) col_flag = true ; if ( mat [ i ] [ j ] == 1 ) { mat [ 0 ] [ j ] = 1 ; mat [ i ] [ 0 ] = 1 ; } } } for ( int i = 1 ; i < mat . length ; i ++ ) { for ( int j = 1 ; j < mat [ 0 ] . length ; j ++ ) { if ( mat [ 0 ] [ j ] == 1 mat [ i ] [ 0 ] == 1 ) { mat [ i ] [ j ] = 1 ; } } } if ( row_flag == true ) { for ( int i = 0 ; i < mat [ 0 ] . length ; i ++ ) { mat [ 0 ] [ i ] = 1 ; } } if ( col_flag == true ) { for ( int i = 0 ; i < mat . length ; i ++ ) { mat [ i ] [ 0 ] = 1 ; } } } public static void printMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) { System . out . print ( mat [ i ] [ j ] ) ; } System . out . println ( " " ) ; } } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 1 , 0 , 0 , 1 } , { 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( " Input ▁ Matrix ▁ : " ) ; printMatrix ( mat ) ; modifyMatrix ( mat ) ; System . out . println ( " Matrix ▁ After ▁ Modification ▁ : " ) ; printMatrix ( mat ) ; } }
class GFG { static int n = 5 ; static int find ( boolean arr [ ] [ ] ) { int i = 0 , j = n - 1 ; int res = - 1 ; while ( i < n && j >= 0 ) { if ( arr [ i ] [ j ] == false ) { while ( j >= 0 && ( arr [ i ] [ j ] == false i == j ) ) { j -- ; } if ( j == - 1 ) { res = i ; break ; } else { i ++ ; } } else { while ( i < n && ( arr [ i ] [ j ] == true i == j ) ) { i ++ ; } if ( i == n ) { res = j ; break ; } else { j -- ; } } } if ( res == - 1 ) { return res ; } for ( int k = 0 ; k < n ; k ++ ) { if ( res != k && arr [ k ] [ res ] != true ) { return - 1 ; } } for ( int l = 0 ; l < n ; l ++ ) { if ( res != l && arr [ res ] [ l ] != false ) { return - 1 ; } } return res ; } public static void main ( String [ ] args ) { boolean mat [ ] [ ] = { { false , false , true , true , false } , { false , false , false , true , false } , { true , true , true , true , false } , { false , false , false , false , false } , { true , true , true , true , true } } ; System . out . println ( find ( mat ) ) ; } }
import java . util . HashSet ; public class GFG { public static void printArray ( int arr [ ] [ ] , int row , int col ) { HashSet < String > set = new HashSet < String > ( ) ; for ( int i = 0 ; i < row ; i ++ ) { String s = " " ; for ( int j = 0 ; j < col ; j ++ ) s += String . valueOf ( arr [ i ] [ j ] ) ; if ( ! set . contains ( s ) ) { set . add ( s ) ; System . out . println ( s ) ; } } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 0 , 1 , 0 , 0 , 1 } , { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 0 , 0 , 1 } , { 1 , 1 , 1 , 0 , 0 } } ; printArray ( arr , 4 , 5 ) ; } }
class GFG { static final int R = 3 ; static final int C = 5 ; static int maxArea ( int mat [ ] [ ] ) { int hist [ ] [ ] = new int [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) { hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } } for ( int i = 0 ; i < R ; i ++ ) { int count [ ] = new int [ R + 1 ] ; for ( int j = 0 ; j < C ; j ++ ) { count [ hist [ i ] [ j ] ] ++ ; } int col_no = 0 ; for ( int j = R ; j >= 0 ; j -- ) { if ( count [ j ] > 0 ) { for ( int k = 0 ; k < count [ j ] ; k ++ ) { hist [ i ] [ col_no ] = j ; col_no ++ ; } } } } int curr_area , max_area = 0 ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { curr_area = ( j + 1 ) * hist [ i ] [ j ] ; if ( curr_area > max_area ) { max_area = curr_area ; } } } return max_area ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 , 1 } , { 1 , 1 , 0 , 1 , 0 } } ; System . out . println ( " Area ▁ of ▁ the ▁ largest ▁ rectangle ▁ is ▁ " + maxArea ( mat ) ) ; } }
class GFG { static final int M = 4 ; static final int N = 5 ; static int preProcess ( int mat [ ] [ ] , int aux [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int i = 1 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; return 0 ; } static int sumQuery ( int aux [ ] [ ] , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; int aux [ ] [ ] = new int [ M ] [ N ] ; preProcess ( mat , aux ) ; int tli = 2 , tlj = 2 , rbi = 3 , rbj = 4 ; System . out . print ( " Query1 : " + sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; tli = 0 ; tlj = 0 ; rbi = 1 ; rbj = 1 ; System . out . print ( " Query2 : " + sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; tli = 1 ; tlj = 2 ; rbi = 3 ; rbj = 3 ; System . out . print ( " Query3 : "  + sumQuery ( aux , tli , tlj , rbi , rbj ) ) ; } }
class GFG { static final int R = 3 ; static final int C = 3 ; static void swap ( int mat [ ] [ ] , int row1 , int row2 , int col ) { for ( int i = 0 ; i < col ; i ++ ) { int temp = mat [ row1 ] [ i ] ; mat [ row1 ] [ i ] = mat [ row2 ] [ i ] ; mat [ row2 ] [ i ] = temp ; } } static int rankOfMatrix ( int mat [ ] [ ] ) { int rank = C ; for ( int row = 0 ; row < rank ; row ++ ) { if ( mat [ row ] [ row ] != 0 ) { for ( int col = 0 ; col < R ; col ++ ) { if ( col != row ) { double mult = ( double ) mat [ col ] [ row ] / mat [ row ] [ row ] ; for ( int i = 0 ; i < rank ; i ++ ) mat [ col ] [ i ] -= mult * mat [ row ] [ i ] ; } } } else { boolean reduce = true ; for ( int i = row + 1 ; i < R ; i ++ ) { if ( mat [ i ] [ row ] != 0 ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( int i = 0 ; i < R ; i ++ ) mat [ i ] [ row ] = mat [ i ] [ rank ] ; } row -- ; } } return rank ; } static void display ( int mat [ ] [ ] , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) System . out . print ( " ▁ " + mat [ i ] [ j ] ) ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 10 } , { - 20 , - 30 , 10 } , { 30 , 50 , 0 } } ; System . out . print ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ " + rankOfMatrix ( mat ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxHist ( int R , int C , int row [ ] ) { Stack < Integer > result = new Stack < Integer > ( ) ; int top_val ; int max_area = 0 ; int area = 0 ; int i = 0 ; while ( i < C ) { if ( result . empty ( ) || row [ result . peek ( ) ] <= row [ i ] ) result . push ( i ++ ) ; else { top_val = row [ result . peek ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . peek ( ) - 1 ) ; max_area = Math . max ( area , max_area ) ; } } while ( ! result . empty ( ) ) { top_val = row [ result . peek ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . peek ( ) - 1 ) ; max_area = Math . max ( area , max_area ) ; } return max_area ; } static int maxRectangle ( int R , int C , int A [ ] [ ] ) { int result = maxHist ( R , C , A [ 0 ] ) ; for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) if ( A [ i ] [ j ] == 1 ) A [ i ] [ j ] += A [ i - 1 ] [ j ] ; result = Math . max ( result , maxHist ( R , C , A [ i ] ) ) ; } return result ; } public static void main ( String [ ] args ) { int R = 4 ; int C = 4 ; int A [ ] [ ] = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 } , } ; System . out . print ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is ▁ " + maxRectangle ( R , C , A ) ) ; } }
class GFG { static int R = 3 ; static int C = 3 ; static class Cell { int r ; int c ; public Cell ( int r , int c ) { this . r = r ; this . c = c ; } } ; static void printSums ( int mat [ ] [ ] , Cell arr [ ] , int n ) { int sum = 0 ; int [ ] row = new int [ R ] ; int [ ] col = new int [ C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { sum += mat [ i ] [ j ] ; col [ j ] += mat [ i ] [ j ] ; row [ i ] += mat [ i ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { int ro = arr [ i ] . r , co = arr [ i ] . c ; System . out . println ( sum - row [ ro ] - col [ co ] + mat [ ro ] [ co ] ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 2 } , { 3 , 4 , 6 } , { 5 , 3 , 2 } } ; Cell arr [ ] = { new Cell ( 0 , 0 ) , new Cell ( 1 , 1 ) , new Cell ( 0 , 1 ) } ; int n = arr . length ; printSums ( mat , arr , n ) ; } }
import java . io . * ; class islands { static int countIslands ( int mat [ ] [ ] , int m , int n ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == ' X ' ) { if ( ( i == 0 mat [ i - 1 ] [ j ] == ' O ' ) && ( j == 0 mat [ i ] [ j - 1 ] == ' O ' ) ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int m = 6 ; int n = 3 ; int mat [ ] [ ] = { { ' O ' , ' O ' , ' O ' } , { ' X ' , ' X ' , ' O ' } , { ' X ' , ' X ' , ' O ' } , { ' O ' , ' O ' , ' X ' } , { ' O ' , ' O ' , ' X ' } , { ' X ' , ' X ' , ' O ' } } ; System . out . println ( " Number ▁ of ▁ rectangular ▁ islands ▁ is : ▁ " + countIslands ( mat , m , n ) ) ; } }
class GFG { static final int M = 4 ; static final int N = 5 ; static int findCommon ( int mat [ ] [ ] ) { int column [ ] = new int [ M ] ; int min_row ; int i ; for ( i = 0 ; i < M ; i ++ ) column [ i ] = N - 1 ; min_row = 0 ; while ( column [ min_row ] >= 0 ) { for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i ] [ column [ i ] ] < mat [ min_row ] [ column [ min_row ] ] ) min_row = i ; } int eq_count = 0 ; for ( i = 0 ; i < M ; i ++ ) { if ( mat [ i ] [ column [ i ] ] > mat [ min_row ] [ column [ min_row ] ] ) { if ( column [ i ] == 0 ) return - 1 ; column [ i ] -= 1 ; } else eq_count ++ ; } if ( eq_count == M ) return mat [ min_row ] [ column [ min_row ] ] ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) System . out . print ( " No ▁ common ▁ element " ) ; else System . out . print ( " Common ▁ element ▁ is ▁ " + result ) ; } }
import java . util . * ; class GFG { static int M = 4 ; static int N = 5 ; static int findCommon ( int mat [ ] [ ] ) { HashMap < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; int i , j ; for ( i = 0 ; i < M ; i ++ ) { if ( cnt . containsKey ( mat [ i ] [ 0 ] ) ) { cnt . put ( mat [ i ] [ 0 ] , cnt . get ( mat [ i ] [ 0 ] ) + 1 ) ; } else { cnt . put ( mat [ i ] [ 0 ] , 1 ) ; } for ( j = 1 ; j < N ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ j - 1 ] ) if ( cnt . containsKey ( mat [ i ] [ j ] ) ) { cnt . put ( mat [ i ] [ j ] , cnt . get ( mat [ i ] [ j ] ) + 1 ) ; } else { cnt . put ( mat [ i ] [ j ] , 1 ) ; } } } for ( Map . Entry < Integer , Integer > ele : cnt . entrySet ( ) ) { if ( ele . getValue ( ) == M ) return ele . getKey ( ) ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 2 , 4 , 5 , 8 , 10 } , { 3 , 5 , 7 , 9 , 11 } , { 1 , 3 , 5 , 7 , 9 } } ; int result = findCommon ( mat ) ; if ( result == - 1 ) System . out . println ( " No ▁ common ▁ element " ) ; else System . out . println ( " Common ▁ element ▁ is ▁ " + result ) ; } }
import java . io . * ; class GFG { static int M = 6 ; static int N = 6 ; static void floodFillUtil ( char mat [ ] [ ] , int x , int y , char prevV , char newV ) { if ( x < 0 x >= M y < 0 y >= N ) return ; if ( mat [ x ] [ y ] != prevV ) return ; mat [ x ] [ y ] = newV ; floodFillUtil ( mat , x + 1 , y , prevV , newV ) ; floodFillUtil ( mat , x - 1 , y , prevV , newV ) ; floodFillUtil ( mat , x , y + 1 , prevV , newV ) ; floodFillUtil ( mat , x , y - 1 , prevV , newV ) ; } static void replaceSurrounded ( char mat [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] == ' O ' ) mat [ i ] [ j ] = ' - ' ; for ( int i = 0 ; i < M ; i ++ ) if ( mat [ i ] [ 0 ] == ' - ' ) floodFillUtil ( mat , i , 0 , ' - ' , ' O ' ) ; for ( int i = 0 ; i < M ; i ++ ) if ( mat [ i ] [ N - 1 ] == ' - ' ) floodFillUtil ( mat , i , N - 1 , ' - ' , ' O ' ) ; for ( int i = 0 ; i < N ; i ++ ) if ( mat [ 0 ] [ i ] == ' - ' ) floodFillUtil ( mat , 0 , i , ' - ' , ' O ' ) ; for ( int i = 0 ; i < N ; i ++ ) if ( mat [ M - 1 ] [ i ] == ' - ' ) floodFillUtil ( mat , M - 1 , i , ' - ' , ' O ' ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] == ' - ' ) mat [ i ] [ j ] = ' X ' ; } public static void main ( String [ ] args ) { char [ ] [ ] mat = { { ' X ' , ' O ' , ' X ' , ' O ' , ' X ' , ' X ' } , { ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' X ' } , { ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' } , { ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' } } ; replaceSurrounded ( mat ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( " " ) ; } } }
public class BinaryTreeToDLL { static class node { int data ; node left , right ; public node ( int data ) { this . data = data ; } } static node prev ; static void inorder ( node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } static void fixPrevptr ( node root ) { if ( root == null ) return ; fixPrevptr ( root . left ) ; root . left = prev ; prev = root ; fixPrevptr ( root . right ) ; } static node fixNextptr ( node root ) { while ( root . right != null ) root = root . right ; while ( root != null && root . left != null ) { node left = root . left ; left . right = root ; root = root . left ; } return root ; } static node BTTtoDLL ( node root ) { prev = null ; fixPrevptr ( root ) ; return fixNextptr ( root ) ; } static void printlist ( node root ) { while ( root != null ) { System . out . print ( root . data + " ▁ " ) ; root = root . right ; } } public static void main ( String [ ] args ) { node root = new node ( 10 ) ; root . left = new node ( 12 ) ; root . right = new node ( 15 ) ; root . left . left = new node ( 25 ) ; root . left . right = new node ( 30 ) ; root . right . left = new node ( 36 ) ; System . out . println ( " Inorder ▁ Tree ▁ Traversal " ) ; inorder ( root ) ; node head = BTTtoDLL ( root ) ; System . out . println ( " DLL Traversal "); printlist ( head ) ; } }
class GFG { static int N = 6 ; static int maximumSubSquare ( int [ ] [ ] arr ) { int [ ] [ ] [ ] dp = new int [ 51 ] [ 51 ] [ 2 ] ; int [ ] [ ] maxside = new int [ 51 ] [ 51 ] ; for ( int [ ] row : maxside ) Arrays . fill ( row , 10 ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { x = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] == ' X ' ) x += 1 ; else x = 0 ; dp [ i ] [ j ] [ 0 ] = x ; } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] [ i ] == ' X ' ) y += 1 ; else y = 0 ; dp [ j ] [ i ] [ 1 ] = y ; } } int maxval = 0 , val = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { val = Math . min ( dp [ i ] [ j ] [ 0 ] , dp [ i ] [ j ] [ 1 ] ) ; if ( dp [ i ] [ j - val + 1 ] [ 1 ] >= val && dp [ i - val + 1 ] [ j ] [ 0 ] >= val ) maxside [ i ] [ j ] = val ; else maxside [ i ] [ j ] = 0 ; maxval = Math . max ( maxval , maxside [ i ] [ j ] ) ; } } return maxval ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { ' X ' , ' O ' , ' X ' , ' X ' , ' X ' , ' X ' } , { ' X ' , ' O ' , ' X ' , ' X ' , ' O ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' O ' , ' X ' } , { ' O ' , ' X ' , ' X ' , ' X ' , ' X ' , ' X ' } , { ' X ' , ' X ' , ' X ' , ' O ' , ' X ' , ' O ' } , { ' O ' , ' O ' , ' X ' , ' O ' , ' O ' , ' O ' } , } ; System . out . println ( maximumSubSquare ( mat ) ) ; } }
class GFG { static int M = 8 ; static int N = 8 ; static void floodFillUtil ( int screen [ ] [ ] , int x , int y , int prevC , int newC ) { if ( x < 0 x >= M y < 0 y >= N ) return ; if ( screen [ x ] [ y ] != prevC ) return ; screen [ x ] [ y ] = newC ; floodFillUtil ( screen , x + 1 , y , prevC , newC ) ; floodFillUtil ( screen , x - 1 , y , prevC , newC ) ; floodFillUtil ( screen , x , y + 1 , prevC , newC ) ; floodFillUtil ( screen , x , y - 1 , prevC , newC ) ; } static void floodFill ( int screen [ ] [ ] , int x , int y , int newC ) { int prevC = screen [ x ] [ y ] ; floodFillUtil ( screen , x , y , prevC , newC ) ; } public static void main ( String [ ] args ) { int screen [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 } , { 1 , 2 , 2 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 2 , 2 , 0 } , { 1 , 1 , 1 , 1 , 1 , 2 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 2 , 2 , 1 } , } ; int x = 4 , y = 4 , newC = 3 ; floodFill ( screen , x , y , newC ) ; System . out . println ( " Updated ▁ screen ▁ after ▁ call ▁ to ▁ floodFill : ▁ " ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( screen [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } }
class Pair implements Comparable < Pair > { int first ; int second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int compareTo ( Pair o ) { return second - o . second ; } } class GFG { public static int validCoord ( int x , int y , int n , int m ) { if ( x < 0 y < 0 ) { return 0 ; } if ( x >= n y >= m ) { return 0 ; } return 1 ; } public static void bfs ( int n , int m , int data [ ] [ ] , int x , int y , int color ) { int vis [ ] [ ] = new int [ 101 ] [ 101 ] ; for ( int i = 0 ; i <= 100 ; i ++ ) { for ( int j = 0 ; j <= 100 ; j ++ ) { vis [ i ] [ j ] = 0 ; } } Queue < Pair > obj = new LinkedList < > ( ) ; Pair pq = new Pair ( x , y ) ; obj . add ( pq ) ; vis [ x ] [ y ] = 1 ; while ( ! obj . isEmpty ( ) ) { Pair coord = obj . peek ( ) ; int x1 = coord . first ; int y1 = coord . second ; int preColor = data [ x1 ] [ y1 ] ; data [ x1 ] [ y1 ] = color ; obj . remove ( ) ; if ( ( validCoord ( x1 + 1 , y1 , n , m ) == 1 ) && vis [ x1 + 1 ] [ y1 ] == 0 && data [ x1 + 1 ] [ y1 ] == preColor ) { Pair p = new Pair ( x1 + 1 , y1 ) ; obj . add ( p ) ; vis [ x1 + 1 ] [ y1 ] = 1 ; } if ( ( validCoord ( x1 - 1 , y1 , n , m ) == 1 ) && vis [ x1 - 1 ] [ y1 ] == 0 && data [ x1 - 1 ] [ y1 ] == preColor ) { Pair p = new Pair ( x1 - 1 , y1 ) ; obj . add ( p ) ; vis [ x1 - 1 ] [ y1 ] = 1 ; } if ( ( validCoord ( x1 , y1 + 1 , n , m ) == 1 ) && vis [ x1 ] [ y1 + 1 ] == 0 && data [ x1 ] [ y1 + 1 ] == preColor ) { Pair p = new Pair ( x1 , y1 + 1 ) ; obj . add ( p ) ; vis [ x1 ] [ y1 + 1 ] = 1 ; } if ( ( validCoord ( x1 , y1 - 1 , n , m ) == 1 ) && vis [ x1 ] [ y1 - 1 ] == 0 && data [ x1 ] [ y1 - 1 ] == preColor ) { Pair p = new Pair ( x1 , y1 - 1 ) ; obj . add ( p ) ; vis [ x1 ] [ y1 - 1 ] = 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( data [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int nn , mm , xx , yy , colorr ; nn = 8 ; mm = 8 ; int dataa [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 0 , 0 } , { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 1 } , { 1 , 2 , 2 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 0 , 1 , 0 } , { 1 , 1 , 1 , 2 , 2 , 2 , 2 , 0 } , { 1 , 1 , 1 , 1 , 1 , 2 , 1 , 1 } , { 1 , 1 , 1 , 1 , 1 , 2 , 2 , 1 } , } ; xx = 4 ; yy = 4 ; colorr = 3 ; bfs ( nn , mm , dataa , xx , yy , colorr ) ; } }
class GFG { static final int INF = Integer . MAX_VALUE ; static final int N = 4 ; static void youngify ( int mat [ ] [ ] , int i , int j ) { int downVal = ( i + 1 < N ) ? mat [ i + 1 ] [ j ] : INF ; int rightVal = ( j + 1 < N ) ? mat [ i ] [ j + 1 ] : INF ; if ( downVal == INF && rightVal == INF ) { return ; } if ( downVal < rightVal ) { mat [ i ] [ j ] = downVal ; mat [ i + 1 ] [ j ] = INF ; youngify ( mat , i + 1 , j ) ; } else { mat [ i ] [ j ] = rightVal ; mat [ i ] [ j + 1 ] = INF ; youngify ( mat , i , j + 1 ) ; } } static int extractMin ( int mat [ ] [ ] ) { int ret = mat [ 0 ] [ 0 ] ; mat [ 0 ] [ 0 ] = INF ; youngify ( mat , 0 , 0 ) ; return ret ; } static void printSorted ( int mat [ ] [ ] ) { System . out . println ( " Elements ▁ of ▁ matrix ▁ in ▁ sorted ▁ order ▁ n " ) ; for ( int i = 0 ; i < N * N ; i ++ ) { System . out . print ( extractMin ( mat ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 10 , 20 , 30 , 40 } , { 15 , 25 , 35 , 45 } , { 27 , 29 , 37 , 48 } , { 32 , 33 , 39 , 50 } } ; printSorted ( mat ) ; } }
class GFG { static final int n = 5 ; static void printSumSimple ( int mat [ ] [ ] , int k ) { if ( k > n ) return ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { for ( int j = 0 ; j < n - k + 1 ; j ++ ) { int sum = 0 ; for ( int p = i ; p < k + i ; p ++ ) for ( int q = j ; q < k + j ; q ++ ) sum += mat [ p ] [ q ] ; System . out . print ( sum + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String arg [ ] ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 , 4 } , { 5 , 5 , 5 , 5 , 5 } } ; int k = 3 ; printSumSimple ( mat , k ) ; } }
class GFG { static final int M = 3 ; static final int N = 4 ; static void transpose ( int A [ ] [ ] , int B [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < M ; j ++ ) B [ i ] [ j ] = A [ j ] [ i ] ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } } ; int B [ ] [ ] = new int [ N ] [ M ] , i , j ; transpose ( A , B ) ; System . out . print ( "Result matrix is NEW_LINE"); for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < M ; j ++ ) System . out . print ( B [ i ] [ j ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } }
class GFG { static final int N = 4 ; static void transpose ( int A [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { int temp = A [ i ] [ j ] ; A [ i ] [ j ] = A [ j ] [ i ] ; A [ j ] [ i ] = temp ; } } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; transpose ( A ) ; System . out . print ( "Modified matrix is NEW_LINE"); for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( A [ i ] [ j ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } }
class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; Node head ; void BinaryTree2DoubleLinkedList ( Node root ) { if ( root == null ) return ; static Node prev = null ; BinaryTree2DoubleLinkedList ( root . left ) ; if ( prev == null ) head = root ; else { root . left = prev ; prev . right = root ; } prev = root ; BinaryTree2DoubleLinkedList ( root . right ) ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . right ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( 12 ) ; tree . root . right = new Node ( 15 ) ; tree . root . left . left = new Node ( 25 ) ; tree . root . left . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 36 ) ; tree . BinaryTree2DoubleLinkedList ( tree . root ) ; tree . printList ( tree . head ) ; } }
class GFG { static final int R = 5 ; static final int C = 4 ; static boolean isValid ( int x , int y1 , int y2 ) { return ( x >= 0 && x < R && y1 >= 0 && y1 < C && y2 >= 0 && y2 < C ) ; } static int getMaxUtil ( int arr [ ] [ ] , int mem [ ] [ ] [ ] , int x , int y1 , int y2 ) { if ( ! isValid ( x , y1 , y2 ) ) return Integer . MIN_VALUE ; if ( x == R - 1 && y1 == 0 && y2 == C - 1 ) return ( y1 == y2 ) ? arr [ x ] [ y1 ] : arr [ x ] [ y1 ] + arr [ x ] [ y2 ] ; if ( x == R - 1 ) return Integer . MIN_VALUE ; if ( mem [ x ] [ y1 ] [ y2 ] != - 1 ) return mem [ x ] [ y1 ] [ y2 ] ; int ans = Integer . MIN_VALUE ; int temp = ( y1 == y2 ) ? arr [ x ] [ y1 ] : arr [ x ] [ y1 ] + arr [ x ] [ y2 ] ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 + 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 - 1 , y2 + 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 - 1 ) ) ; ans = Math . max ( ans , temp + getMaxUtil ( arr , mem , x + 1 , y1 + 1 , y2 + 1 ) ) ; return ( mem [ x ] [ y1 ] [ y2 ] = ans ) ; } static int geMaxCollection ( int arr [ ] [ ] ) { int [ ] [ ] [ ] mem = new int [ R ] [ C ] [ C ] ; for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) { for ( int l = 0 ; l < C ; l ++ ) mem [ i ] [ j ] [ l ] = - 1 ; } } return getMaxUtil ( arr , mem , 0 , 0 , C - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 6 , 8 , 2 } , { 5 , 2 , 4 , 3 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , { 1 , 1 , 20 , 10 } , } ; System . out . print ( " Maximum ▁ collection ▁ is ▁ " + geMaxCollection ( arr ) ) ; } }
class GFG { static final int R = 3 ; static final int C = 3 ; static int pathCountRec ( int mat [ ] [ ] , int m , int n , int k ) { if ( m < 0 n < 0 ) { return 0 ; } if ( m == 0 && n == 0 && ( k == mat [ m ] [ n ] ) ) { return 1 ; } return pathCountRec ( mat , m - 1 , n , k - mat [ m ] [ n ] ) + pathCountRec ( mat , m , n - 1 , k - mat [ m ] [ n ] ) ; } static int pathCount ( int mat [ ] [ ] , int k ) { return pathCountRec ( mat , R - 1 , C - 1 , k ) ; } public static void main ( String [ ] args ) { int k = 12 ; int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 6 , 5 } , { 3 , 2 , 1 } } ; System . out . println ( pathCount ( mat , k ) ) ; } }
class min_steps { static int minInitialPoints ( int points [ ] [ ] , int R , int C ) { int dp [ ] [ ] = new int [ R ] [ C ] ; int m = R , n = C ; dp [ m - 1 ] [ n - 1 ] = points [ m - 1 ] [ n - 1 ] > 0 ? 1 : Math . abs ( points [ m - 1 ] [ n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i ] [ n - 1 ] = Math . max ( dp [ i + 1 ] [ n - 1 ] - points [ i ] [ n - 1 ] , 1 ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) dp [ m - 1 ] [ j ] = Math . max ( dp [ m - 1 ] [ j + 1 ] - points [ m - 1 ] [ j ] , 1 ) ; for ( int i = m - 2 ; i >= 0 ; i -- ) { for ( int j = n - 2 ; j >= 0 ; j -- ) { int min_points_on_exit = Math . min ( dp [ i + 1 ] [ j ] , dp [ i ] [ j + 1 ] ) ; dp [ i ] [ j ] = Math . max ( min_points_on_exit - points [ i ] [ j ] , 1 ) ; } } return dp [ 0 ] [ 0 ] ; } public static void main ( String args [ ] ) { int points [ ] [ ] = { { - 2 , - 3 , 3 } , { - 5 , - 10 , 1 } , { 10 , 30 , - 5 } } ; int R = 3 , C = 3 ; System . out . println ( " Minimum ▁ Initial ▁ Points ▁ Required : ▁ " + minInitialPoints ( points , R , C ) ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void convertTree ( Node node ) { int left_data = 0 , right_data = 0 , diff ; if ( node == null || ( node . left == null && node . right == null ) ) return ; else { convertTree ( node . left ) ; convertTree ( node . right ) ; if ( node . left != null ) left_data = node . left . data ; if ( node . right != null ) right_data = node . right . data ; diff = left_data + right_data - node . data ; if ( diff > 0 ) node . data = node . data + diff ; if ( diff < 0 ) increment ( node , - diff ) ; } } void increment ( Node node , int diff ) { if ( node . left != null ) { node . left . data = node . left . data + diff ; increment ( node . left , diff ) ; } else if ( node . right != null ) { node . right . data = node . right . data + diff ; increment ( node . right , diff ) ; } } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 50 ) ; tree . root . left = new Node ( 7 ) ; tree . root . right = new Node ( 2 ) ; tree . root . left . left = new Node ( 3 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 1 ) ; tree . root . right . right = new Node ( 30 ) ; System . out . println ( " Inorder ▁ traversal ▁ before ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; tree . convertTree ( tree . root ) ; System . out . println ( " " ) ; System . out . println ( " Inorder ▁ traversal ▁ after ▁ conversion ▁ is ▁ : " ) ; tree . printInorder ( tree . root ) ; } }
import java . util . * ; class GFG { static class Queue { static Stack < Integer > s1 = new Stack < Integer > ( ) ; static Stack < Integer > s2 = new Stack < Integer > ( ) ; static void enQueue ( int x ) { while ( ! s1 . isEmpty ( ) ) { s2 . push ( s1 . pop ( ) ) ; s1 . pop ( ) ; } s1 . push ( x ) ; while ( ! s2 . isEmpty ( ) ) { s1 . push ( s2 . pop ( ) ) ; s2 . pop ( ) ; } } static int deQueue ( ) { if ( s1 . isEmpty ( ) ) { System . out . println ( " Q ▁ is ▁ Empty " ) ; System . exit ( 0 ) ; } int x = s1 . peek ( ) ; s1 . pop ( ) ; return x ; } } ; public static void main ( String [ ] args ) { Queue q = new Queue ( ) ; q . enQueue ( 1 ) ; q . enQueue ( 2 ) ; q . enQueue ( 3 ) ; System . out . println ( q . deQueue ( ) ) ; System . out . println ( q . deQueue ( ) ) ; System . out . println ( q . deQueue ( ) ) ; } }
class GFG { static class node { int key ; node left , right ; node ( int key ) { this . key = key ; this . left = null ; this . right = null ; } } static void convert ( node root ) { if ( root == null ) return ; convert ( root . left ) ; convert ( root . right ) ; if ( root . left == null ) root . left = root . right ; else root . left . right = root . right ; root . right = null ; } static void downRightTraversal ( node root ) { if ( root != null ) { System . out . print ( root . key + " ▁ " ) ; downRightTraversal ( root . right ) ; downRightTraversal ( root . left ) ; } } static node newNode ( int key ) { node temp = new node ( 0 ) ; temp . key = key ; temp . left = null ; temp . right = null ; return temp ; } public static void main ( String [ ] args ) { node root = new node ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . right . left = newNode ( 4 ) ; root . right . right = newNode ( 5 ) ; root . right . left . left = newNode ( 6 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; convert ( root ) ; System . out . println ( " Traversal ▁ of ▁ the ▁ tree ▁ " + " converted ▁ to ▁ down - right ▁ form " ) ; downRightTraversal ( root ) ; } }
void push ( int x ) { if ( isEmpty ( ) == true ) { super . push ( x ) ; min . push ( x ) ; } else { super . push ( x ) ; int y = min . pop ( ) ; min . push ( y ) ; if ( x <= y ) min . push ( x ) ; } } public Integer pop ( ) { int x = super . pop ( ) ; int y = min . pop ( ) ; if ( y != x ) min . push ( y ) ; return x ; }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int toSumTree ( Node node ) { if ( node == null ) return 0 ; int old_val = node . data ; node . data = toSumTree ( node . left ) + toSumTree ( node . right ) ; return node . data + old_val ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 10 ) ; tree . root . left = new Node ( - 2 ) ; tree . root . right = new Node ( 6 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( - 4 ) ; tree . root . right . left = new Node ( 7 ) ; tree . root . right . right = new Node ( 5 ) ; tree . toSumTree ( tree . root ) ; System . out . println ( " Inorder ▁ Traversal ▁ of ▁ the ▁ resultant ▁ tree ▁ is : " ) ; tree . printInorder ( tree . root ) ; } }
import java . util . * ; class GFG { static int findPeak ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; if ( arr [ 0 ] >= arr [ 1 ] ) return 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) return n - 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] >= arr [ i - 1 ] && arr [ i ] >= arr [ i + 1 ] ) return i ; } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 20 , 4 , 1 , 0 } ; int n = arr . length ; System . out . print ( " Index ▁ of ▁ a ▁ peak ▁ point ▁ is ▁ " + findPeak ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int maxTripletSum ( int arr [ ] , int n ) { int sum = - 1000000 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 8 , 6 , 4 , 2 } ; int n = arr . length ; System . out . println ( maxTripletSum ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxTripletSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 8 , 6 , 4 , 2 } ; int n = arr . length ; System . out . println ( maxTripletSum ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxTripletSum ( int arr [ ] , int n ) { int maxA = - 100000000 , maxB = - 100000000 ; int maxC = - 100000000 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; } return ( maxA + maxB + maxC ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 8 , 6 , 4 , 2 } ; int n = arr . length ; System . out . println ( maxTripletSum ( arr , n ) ) ; } }
class FindTriplet { boolean find3Numbers ( int A [ ] , int arr_size , int sum ) { int l , r ; quickSort ( A , 0 , arr_size - 1 ) ; for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { l = i + 1 ; r = arr_size - 1 ; while ( l < r ) { if ( A [ i ] + A [ l ] + A [ r ] == sum ) { System . out . print ( " Triplet ▁ is ▁ " + A [ i ] + " , ▁ " + A [ l ] + " , ▁ " + A [ r ] ) ; return true ; } else if ( A [ i ] + A [ l ] + A [ r ] < sum ) l ++ ; else r -- ; } } return false ; } int partition ( int A [ ] , int si , int ei ) { int x = A [ ei ] ; int i = ( si - 1 ) ; int j ; for ( j = si ; j <= ei - 1 ; j ++ ) { if ( A [ j ] <= x ) { i ++ ; int temp = A [ i ] ; A [ i ] = A [ j ] ; A [ j ] = temp ; } } int temp = A [ i + 1 ] ; A [ i + 1 ] = A [ ei ] ; A [ ei ] = temp ; return ( i + 1 ) ; } void quickSort ( int A [ ] , int si , int ei ) { int pi ; if ( si < ei ) { pi = partition ( A , si , ei ) ; quickSort ( A , si , pi - 1 ) ; quickSort ( A , pi + 1 , ei ) ; } } public static void main ( String [ ] args ) { FindTriplet triplet = new FindTriplet ( ) ; int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = A . length ; triplet . find3Numbers ( A , arr_size , sum ) ; } }
import java . util . * ; class GFG { static boolean find3Numbers ( int A [ ] , int arr_size , int sum ) { for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; int curr_sum = sum - A [ i ] ; for ( int j = i + 1 ; j < arr_size ; j ++ ) { if ( s . contains ( curr_sum - A [ j ] ) ) { System . out . printf ( " Triplet ▁ is ▁ % d , ▁ % d , ▁ % d " , A [ i ] , A [ j ] , curr_sum - A [ j ] ) ; return true ; } s . add ( A [ j ] ) ; } } return false ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int sum = 22 ; int arr_size = A . length ; find3Numbers ( A , arr_size , sum ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int SIZE = 10 ; static void sortMat ( int mat [ ] [ ] , int n ) { int temp [ ] = new int [ n * n ] ; int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) temp [ k ++ ] = mat [ i ] [ j ] ; Arrays . sort ( temp ) ; k = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) mat [ i ] [ j ] = temp [ k ++ ] ; } static void printMat ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 5 , 4 , 7 } , { 1 , 3 , 8 } , { 2 , 9 , 6 } } ; int n = 3 ; System . out . println ( " Original ▁ Matrix : " ) ; printMat ( mat , n ) ; sortMat ( mat , n ) ; System . out . println ( " Matrix ▁ After ▁ Sorting : " ) ; printMat ( mat , n ) ; } }
import java . math . BigInteger ; class Test { static int arr [ ] = new int [ ] { 1 , 2 , 3 , 4 } ; static void printSubsequences ( int n ) { int opsize = ( int ) Math . pow ( 2 , n ) ; for ( int counter = 1 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( BigInteger . valueOf ( counter ) . testBit ( j ) ) System . out . print ( arr [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { System . out . println ( " All ▁ Non - empty ▁ Subsequences " ) ; printSubsequences ( arr . length ) ; } }
class ProductArray { void productArray ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . print ( "0" ) ; return ; } int i , temp = 1 ; int prod [ ] = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) prod [ j ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { prod [ i ] = temp ; temp *= arr [ i ] ; } temp = 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { prod [ i ] *= temp ; temp *= arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) System . out . print ( prod [ i ] + " ▁ " ) ; return ; } public static void main ( String [ ] args ) { ProductArray pa = new ProductArray ( ) ; int arr [ ] = { 10 , 3 , 5 , 6 , 2 } ; int n = arr . length ; System . out . println ( " The ▁ product ▁ array ▁ is ▁ : ▁ " ) ; pa . productArray ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class Solution { public static long [ ] productExceptSelf ( int a [ ] , int n ) { long prod = 1 ; long flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) flag ++ ; else prod *= a [ i ] ; } long arr [ ] = new long [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( flag > 1 ) { arr [ i ] = 0 ; } else if ( flag == 0 ) arr [ i ] = ( prod / a [ i ] ) ; else if ( flag == 1 && a [ i ] != 0 ) { arr [ i ] = 0 ; } else arr [ i ] = prod ; } return arr ; } public static void main ( String args [ ] ) throws IOException { int n = 5 ; int [ ] array = { 10 , 3 , 5 , 6 , 2 } ; Solution ob = new Solution ( ) ; long [ ] ans = new long [ n ] ; ans = ob . productExceptSelf ( array , n ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } }
class AreConsecutive { boolean areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { int j ; if ( arr [ i ] < 0 ) j = - arr [ i ] - min ; else j = arr [ i ] - min ; if ( arr [ j ] > 0 ) arr [ j ] = - arr [ j ] ; else return false ; } return true ; } return false ; } int getMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < min ) min = arr [ i ] ; } return min ; } int getMax ( int arr [ ] , int n ) { int max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { AreConsecutive consecutive = new AreConsecutive ( ) ; int arr [ ] = { 5 , 4 , 2 , 3 , 1 , 6 } ; int n = arr . length ; if ( consecutive . areConsecutive ( arr , n ) == true ) System . out . println ( " Array ▁ elements ▁ are ▁ consecutive " ) ; else System . out . println ( " Array ▁ elements ▁ are ▁ not ▁ consecutive " ) ; } }
class GFG { static void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ] ) { i ++ ; j ++ ; } } while ( i < n ) System . out . print ( arr1 [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 6 , 10 , 12 , 15 } ; int arr2 [ ] = { 1 , 3 , 5 , 10 , 16 } ; int n = arr1 . length ; int m = arr2 . length ; relativeComplement ( arr1 , arr2 , n , m ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int minOps ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int max = arr [ arr . length - 1 ] ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ; else res += ( max - arr [ i ] ) / k ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 21 , 33 , 9 , 45 , 63 } ; int n = arr . length ; int k = 6 ; System . out . println ( minOps ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static int solve ( int [ ] A , int [ ] B , int [ ] C ) { int i , j , k ; i = A . length - 1 ; j = B . length - 1 ; k = C . length - 1 ; int min_diff , current_diff , max_term ; min_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; while ( i != - 1 && j != - 1 && k != - 1 ) { current_diff = Math . abs ( Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) - Math . min ( A [ i ] , Math . min ( B [ j ] , C [ k ] ) ) ) ; if ( current_diff < min_diff ) min_diff = current_diff ; max_term = Math . max ( A [ i ] , Math . max ( B [ j ] , C [ k ] ) ) ; if ( A [ i ] == max_term ) i -= 1 ; else if ( B [ j ] == max_term ) j -= 1 ; else k -= 1 ; } return min_diff ; } public static void main ( String [ ] args ) { int [ ] D = { 5 , 8 , 10 , 15 } ; int [ ] E = { 6 , 9 , 15 , 78 , 89 } ; int [ ] F = { 2 , 3 , 6 , 6 , 8 , 8 , 10 } ; System . out . println ( solve ( D , E , F ) ) ; } }
public class GFG { static int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 10 , 30 , 15 } ; int x = 30 ; int n = arr . length ; System . out . printf ( " % d ▁ is ▁ present ▁ at ▁ index ▁ % d " , x , search ( arr , n , x ) ) ; } }
import java . io . * ; class GFG { public static void search ( int arr [ ] , int search_Element ) { int left = 0 ; int length = arr . length ; int right = length - 1 ; int position = - 1 ; for ( left = 0 ; left <= right ; ) { if ( arr [ left ] == search_Element ) { position = left ; System . out . println ( " Element ▁ found ▁ in ▁ Array ▁ at ▁ " + ( position + 1 ) + " ▁ Position ▁ with ▁ " + ( left + 1 ) + " ▁ Attempt " ) ; break ; } if ( arr [ right ] == search_Element ) { position = right ; System . out . println ( " Element ▁ found ▁ in ▁ Array ▁ at ▁ " + ( position + 1 ) + " ▁ Position ▁ with ▁ " + ( length - right ) + " ▁ Attempt " ) ; break ; } left ++ ; right -- ; } if ( position == - 1 ) System . out . println ( " Not ▁ found ▁ in ▁ Array ▁ with ▁ " + left + " ▁ Attempt " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int search_element = 5 ; search ( arr , search_element ) ; } }
class BinarySearch { int binarySearch ( int arr [ ] , int x ) { int l = 0 , r = arr . length - 1 ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ " + " index ▁ " + result ) ; } }
public class JumpSearch { public static int jumpSearch ( int [ ] arr , int x ) { int n = arr . length ; int step = ( int ) Math . floor ( Math . sqrt ( n ) ) ; int prev = 0 ; while ( arr [ Math . min ( step , n ) - 1 ] < x ) { prev = step ; step += ( int ) Math . floor ( Math . sqrt ( n ) ) ; if ( prev >= n ) return - 1 ; } while ( arr [ prev ] < x ) { prev ++ ; if ( prev == Math . min ( step , n ) ) return - 1 ; } if ( arr [ prev ] == x ) return prev ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 , 55 , 89 , 144 , 233 , 377 , 610 } ; int x = 55 ; int index = jumpSearch ( arr , x ) ; System . out . println ( " Number " ▁ + ▁ x ▁ + ▁ " is at index " + index); } }
import java . util . * ; class GFG { public static int interpolationSearch ( int arr [ ] , int lo , int hi , int x ) { int pos ; if ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { pos = lo + ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - arr [ lo ] ) ) ; if ( arr [ pos ] == x ) return pos ; if ( arr [ pos ] < x ) return interpolationSearch ( arr , pos + 1 , hi , x ) ; if ( arr [ pos ] > x ) return interpolationSearch ( arr , lo , pos - 1 , x ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 12 , 13 , 16 , 18 , 19 , 20 , 21 , 22 , 23 , 24 , 33 , 35 , 42 , 47 } ; int n = arr . length ; int x = 18 ; int index = interpolationSearch ( arr , 0 , n - 1 , x ) ; if ( index != - 1 ) System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + index ) ; else System . out . println ( " Element ▁ not ▁ found . " ) ; } }
import java . util . Arrays ; class GFG { static int exponentialSearch ( int arr [ ] , int n , int x ) { if ( arr [ 0 ] == x ) return 0 ; int i = 1 ; while ( i < n && arr [ i ] <= x ) i = i * 2 ; return Arrays . binarySearch ( arr , i / 2 , Math . min ( i , n - 1 ) , x ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int result = exponentialSearch ( arr , arr . length , x ) ; System . out . println ( ( result < 0 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
class SelectionSort { void sort ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int min_idx = i ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] < arr [ min_idx ] ) min_idx = j ; int temp = arr [ min_idx ] ; arr [ min_idx ] = arr [ i ] ; arr [ i ] = temp ; } } void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { SelectionSort ob = new SelectionSort ( ) ; int arr [ ] = { 64 , 25 , 12 , 22 , 11 } ; ob . sort ( arr ) ; System . out . println ( " Sorted ▁ array " ) ; ob . printArray ( arr ) ; } }
import java . io . * ; class GFG { static void bubbleSort ( int arr [ ] , int n ) { int i , j , temp ; boolean swapped ; for ( i = 0 ; i < n - 1 ; i ++ ) { swapped = false ; for ( j = 0 ; j < n - i - 1 ; j ++ ) { if ( arr [ j ] > arr [ j + 1 ] ) { temp = arr [ j ] ; arr [ j ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; swapped = true ; } } if ( swapped == false ) break ; } } static void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 64 , 34 , 25 , 12 , 22 , 11 , 90 } ; int n = arr . length ; bubbleSort ( arr , n ) ; System . out . println ( " Sorted ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
class MergeSort { void merge ( int arr [ ] , int l , int m , int r ) { int n1 = m - l + 1 ; int n2 = r - m ; int L [ ] = new int [ n1 ] ; int R [ ] = new int [ n2 ] ; for ( int i = 0 ; i < n1 ; ++ i ) L [ i ] = arr [ l + i ] ; for ( int j = 0 ; j < n2 ; ++ j ) R [ j ] = arr [ m + 1 + j ] ; int i = 0 , j = 0 ; int k = l ; while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } void sort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; sort ( arr , l , m ) ; sort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; System . out . println ( " Given ▁ Array " ) ; printArray ( arr ) ; MergeSort ob = new MergeSort ( ) ; ob . sort ( arr , 0 , arr . length - 1 ) ; System . out . println ( " Sorted array "); printArray ( arr ) ; } }
import java . io . * ; class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] < pivot ) { i ++ ; swap ( arr , i , j ) ; } } swap ( arr , i + 1 , high ) ; return ( i + 1 ) ; } static void quickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; quickSort ( arr , low , pi - 1 ) ; quickSort ( arr , pi + 1 , high ) ; } } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 8 , 9 , 1 , 5 } ; int n = arr . length ; quickSort ( arr , 0 , n - 1 ) ; System . out . println ( " Sorted ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class Radix { static int getMax ( int arr [ ] , int n ) { int mx = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > mx ) mx = arr [ i ] ; return mx ; } static void countSort ( int arr [ ] , int n , int exp ) { int output [ ] = new int [ n ] ; int i ; int count [ ] = new int [ 10 ] ; Arrays . fill ( count , 0 ) ; for ( i = 0 ; i < n ; i ++ ) count [ ( arr [ i ] / exp ) % 10 ] ++ ; for ( i = 1 ; i < 10 ; i ++ ) count [ i ] += count [ i - 1 ] ; for ( i = n - 1 ; i >= 0 ; i -- ) { output [ count [ ( arr [ i ] / exp ) % 10 ] - 1 ] = arr [ i ] ; count [ ( arr [ i ] / exp ) % 10 ] -- ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = output [ i ] ; } static void radixsort ( int arr [ ] , int n ) { int m = getMax ( arr , n ) ; for ( int exp = 1 ; m / exp > 0 ; exp *= 10 ) countSort ( arr , n , exp ) ; } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 170 , 45 , 75 , 90 , 802 , 24 , 2 , 66 } ; int n = arr . length ; radixsort ( arr , n ) ; print ( arr , n ) ; } }
import java . util . * ; import java . util . Collections ; class GFG { static void bucketSort ( float arr [ ] , int n ) { if ( n <= 0 ) return ; @ SuppressWarnings ( " unchecked " ) Vector < Float > [ ] buckets = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { buckets [ i ] = new Vector < Float > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { float idx = arr [ i ] * n ; buckets [ ( int ) idx ] . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { Collections . sort ( buckets [ i ] ) ; } int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < buckets [ i ] . size ( ) ; j ++ ) { arr [ index ++ ] = buckets [ i ] . get ( j ) ; } } } public static void main ( String args [ ] ) { float arr [ ] = { ( float ) 0.897 , ( float ) 0.565 , ( float ) 0.656 , ( float ) 0.1234 , ( float ) 0.665 , ( float ) 0.3434 } ; int n = arr . length ; bucketSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; for ( float el : arr ) { System . out . print ( el + " ▁ " ) ; } } }
class ShellSort { static void printArray ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } int sort ( int arr [ ] ) { int n = arr . length ; for ( int gap = n / 2 ; gap > 0 ; gap /= 2 ) { for ( int i = gap ; i < n ; i += 1 ) { int temp = arr [ i ] ; int j ; for ( j = i ; j >= gap && arr [ j - gap ] > temp ; j -= gap ) arr [ j ] = arr [ j - gap ] ; arr [ j ] = temp ; } } return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 34 , 54 , 2 , 3 } ; System . out . println ( " Array ▁ before ▁ sorting " ) ; printArray ( arr ) ; ShellSort ob = new ShellSort ( ) ; ob . sort ( arr ) ; System . out . println ( " Array ▁ after ▁ sorting " ) ; printArray ( arr ) ; } }
class CombSort { int getNextGap ( int gap ) { gap = ( gap * 10 ) / 13 ; if ( gap < 1 ) return 1 ; return gap ; } void sort ( int arr [ ] ) { int n = arr . length ; int gap = n ; boolean swapped = true ; while ( gap != 1 swapped == true ) { gap = getNextGap ( gap ) ; swapped = false ; for ( int i = 0 ; i < n - gap ; i ++ ) { if ( arr [ i ] > arr [ i + gap ] ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + gap ] ; arr [ i + gap ] = temp ; swapped = true ; } } } } public static void main ( String args [ ] ) { CombSort ob = new CombSort ( ) ; int arr [ ] = { 8 , 4 , 1 , 56 , 3 , - 44 , 23 , - 6 , 28 , 0 } ; ob . sort ( arr ) ; System . out . println ( " sorted ▁ array " ) ; for ( int i = 0 ; i < arr . length ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . util . * ; class QuickSort { static int partition ( int arr [ ] , int low , int high ) { int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } int temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void qSort ( int arr [ ] , int low , int high ) { if ( low < high ) { int pi = partition ( arr , low , high ) ; qSort ( arr , low , pi - 1 ) ; qSort ( arr , pi + 1 , high ) ; } } public static void main ( String args [ ] ) { int n = 5 ; int arr [ ] = { 4 , 2 , 6 , 9 , 2 } ; qSort ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
import java . util . * ; class QuickSort { static int partition ( int arr [ ] , int low , int high ) { int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } int temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return i + 1 ; } static void quickSortIterative ( int arr [ ] , int l , int h ) { int [ ] stack = new int [ h - l + 1 ] ; int top = - 1 ; stack [ ++ top ] = l ; stack [ ++ top ] = h ; while ( top >= 0 ) { h = stack [ top -- ] ; l = stack [ top -- ] ; int p = partition ( arr , l , h ) ; if ( p - 1 > l ) { stack [ ++ top ] = l ; stack [ ++ top ] = p - 1 ; } if ( p + 1 < h ) { stack [ ++ top ] = p + 1 ; stack [ ++ top ] = h ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 3 , 5 , 2 , 1 , 3 , 2 , 3 } ; int n = 8 ; quickSortIterative ( arr , 0 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
class KClosest { int findCrossOver ( int arr [ ] , int low , int high , int x ) { if ( arr [ high ] <= x ) return high ; if ( arr [ low ] > x ) return low ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] <= x && arr [ mid + 1 ] > x ) return mid ; if ( arr [ mid ] < x ) return findCrossOver ( arr , mid + 1 , high , x ) ; return findCrossOver ( arr , low , mid - 1 , x ) ; } void printKclosest ( int arr [ ] , int x , int k , int n ) { int l = findCrossOver ( arr , 0 , n - 1 , x ) ; int r = l + 1 ; int count = 0 ; if ( arr [ l ] == x ) l -- ; while ( l >= 0 && r < n && count < k ) { if ( x - arr [ l ] < arr [ r ] - x ) System . out . print ( arr [ l -- ] + " ▁ " ) ; else System . out . print ( arr [ r ++ ] + " ▁ " ) ; count ++ ; } while ( count < k && l >= 0 ) { System . out . print ( arr [ l -- ] + " ▁ " ) ; count ++ ; } while ( count < k && r < n ) { System . out . print ( arr [ r ++ ] + " ▁ " ) ; count ++ ; } } public static void main ( String args [ ] ) { KClosest ob = new KClosest ( ) ; int arr [ ] = { 12 , 16 , 22 , 30 , 35 , 39 , 42 , 45 , 48 , 50 , 53 , 55 , 56 } ; int n = arr . length ; int x = 35 , k = 4 ; ob . printKclosest ( arr , x , 4 , n ) ; } }
class Sort1ToN2 { void countSort ( int arr [ ] , int n , int exp ) { int output [ ] = new int [ n ] ; int i , count [ ] = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) count [ ( arr [ i ] / exp ) % n ] ++ ; for ( i = 1 ; i < n ; i ++ ) count [ i ] += count [ i - 1 ] ; for ( i = n - 1 ; i >= 0 ; i -- ) { output [ count [ ( arr [ i ] / exp ) % n ] - 1 ] = arr [ i ] ; count [ ( arr [ i ] / exp ) % n ] -- ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = output [ i ] ; } void sort ( int arr [ ] , int n ) { countSort ( arr , n , 1 ) ; countSort ( arr , n , n ) ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { Sort1ToN2 ob = new Sort1ToN2 ( ) ; int arr [ ] = { 40 , 12 , 45 , 32 , 33 , 1 , 22 } ; int n = arr . length ; System . out . println ( " Given ▁ array " ) ; ob . printArr ( arr , n ) ; ob . sort ( arr , n ) ; System . out . println ( " Sorted ▁ array " ) ; ob . printArr ( arr , n ) ; } }
class GFG { int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 2 , x ) ; return binarySearch ( arr , mid + 2 , r , x ) ; } return - 1 ; } public static void main ( String args [ ] ) { GFG ob = new GFG ( ) ; int arr [ ] = { 3 , 2 , 10 , 4 , 40 } ; int n = arr . length ; int x = 4 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else System . out . println ( " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
import java . io . * ; import java . util . * ; import java . lang . Math ; class CloseSum { static void printClosest ( int arr [ ] , int n , int x ) { int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 , diff = Integer . MAX_VALUE ; while ( r > l ) { if ( Math . abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] > x ) r -- ; else l ++ ; } System . out . println ( " ▁ The ▁ closest ▁ pair ▁ is ▁ " + arr [ res_l ] + " ▁ and ▁ " + arr [ res_r ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 22 , 28 , 29 , 30 , 40 } , x = 54 ; int n = arr . length ; printClosest ( arr , n , x ) ; } }
class CountOnes { int countOnes ( int arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low , ( mid - 1 ) ) ; } return 0 ; } public static void main ( String args [ ] ) { CountOnes ob = new CountOnes ( ) ; int arr [ ] = { 1 , 1 , 1 , 1 , 0 , 0 , 0 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ array ▁ is ▁ " + ob . countOnes ( arr , 0 , n - 1 ) ) ; } }
import java . io . * ; class GFG { public static void minimumSwaps ( int a [ ] , int n ) { int maxx = - 1 , minn = a [ 0 ] , l = 0 , r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxx ) { maxx = a [ i ] ; l = i ; } if ( a [ i ] <= minn ) { minn = a [ i ] ; r = i ; } } if ( r < l ) System . out . println ( l + ( n - r - 2 ) ) ; else System . out . println ( l + ( n - r - 1 ) ) ; } public static void main ( String args [ ] ) throws IOException { int a [ ] = { 5 , 6 , 1 , 3 } ; int n = a . length ; minimumSwaps ( a , n ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void SelectActivities ( int s [ ] , int f [ ] ) { ArrayList < Pair > ans = new ArrayList < > ( ) ; PriorityQueue < Pair > p = new PriorityQueue < > ( ( p1 , p2 ) -> p1 . first - p2 . first ) ; for ( int i = 0 ; i < s . length ; i ++ ) { p . add ( new Pair ( f [ i ] , s [ i ] ) ) ; } Pair it = p . poll ( ) ; int start = it . second ; int end = it . first ; ans . add ( new Pair ( start , end ) ) ; while ( ! p . isEmpty ( ) ) { Pair itr = p . poll ( ) ; if ( itr . second >= end ) { start = itr . second ; end = itr . first ; ans . add ( new Pair ( start , end ) ) ; } } System . out . println ( "Following Activities should be selected. NEW_LINE"); for ( Pair itr : ans ) { System . out . println ( " Activity ▁ started ▁ at : ▁ " + itr . first + " ▁ and ▁ ends ▁ at ▁ " + itr . second ) ; } } public static void main ( String [ ] args ) { int s [ ] = { 1 , 3 , 0 , 5 , 8 , 5 } ; int f [ ] = { 2 , 4 , 6 , 7 , 9 , 9 } ; SelectActivities ( s , f ) ; } }
class LIS { static int max_ref ; static int _lis ( int arr [ ] , int n ) { if ( n == 1 ) return 1 ; int res , max_ending_here = 1 ; for ( int i = 1 ; i < n ; i ++ ) { res = _lis ( arr , i ) ; if ( arr [ i - 1 ] < arr [ n - 1 ] && res + 1 > max_ending_here ) max_ending_here = res + 1 ; } if ( max_ref < max_ending_here ) max_ref = max_ending_here ; return max_ending_here ; } static int lis ( int arr [ ] , int n ) { max_ref = 1 ; _lis ( arr , n ) ; return max_ref ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lis ▁ is ▁ " + lis ( arr , n ) + "NEW_LINE"); } }
class LIS { static int lis ( int arr [ ] , int n ) { int lis [ ] = new int [ n ] ; int i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return max ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lis ▁ is ▁ " + lis ( arr , n ) + "NEW_LINE"); } }
public class LongestCommonSubsequence { int lcs ( char [ ] X , char [ ] Y , int m , int n ) { if ( m == 0 n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } int max ( int a , int b ) { return ( a > b ) ? a : b ; } public static void main ( String [ ] args ) { LongestCommonSubsequence lcs = new LongestCommonSubsequence ( ) ; String s1 = " AGGTAB " ; String s2 = " GXTXAYB " ; char [ ] X = s1 . toCharArray ( ) ; char [ ] Y = s2 . toCharArray ( ) ; int m = X . length ; int n = Y . length ; System . out . println ( " Length ▁ of ▁ LCS ▁ is " + " ▁ " + lcs . lcs ( X , Y , m , n ) ) ; } }
public class LongestCommonSubsequence { int lcs ( char [ ] X , char [ ] Y , int m , int n ) { int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } int max ( int a , int b ) { return ( a > b ) ? a : b ; } public static void main ( String [ ] args ) { LongestCommonSubsequence lcs = new LongestCommonSubsequence ( ) ; String s1 = " AGGTAB " ; String s2 = " GXTXAYB " ; char [ ] X = s1 . toCharArray ( ) ; char [ ] Y = s2 . toCharArray ( ) ; int m = X . length ; int n = Y . length ; System . out . println ( " Length ▁ of ▁ LCS ▁ is " + " ▁ " + lcs . lcs ( X , Y , m , n ) ) ; } }
import java . util . * ; class GFG { static void EditDistDP ( String str1 , String str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; int [ ] [ ] DP = new int [ 2 ] [ len1 + 1 ] ; for ( int i = 0 ; i <= len1 ; i ++ ) DP [ 0 ] [ i ] = i ; for ( int i = 1 ; i <= len2 ; i ++ ) { for ( int j = 0 ; j <= len1 ; j ++ ) { if ( j == 0 ) DP [ i % 2 ] [ j ] = i ; else if ( str1 . charAt ( j - 1 ) == str2 . charAt ( i - 1 ) ) { DP [ i % 2 ] [ j ] = DP [ ( i - 1 ) % 2 ] [ j - 1 ] ; } else { DP [ i % 2 ] [ j ] = 1 + Math . min ( DP [ ( i - 1 ) % 2 ] [ j ] , Math . min ( DP [ i % 2 ] [ j - 1 ] , DP [ ( i - 1 ) % 2 ] [ j - 1 ] ) ) ; } } } System . out . print ( DP [ len2 % 2 ] [ len1 ] + "NEW_LINE"); } public static void main ( String [ ] args ) { String str1 = " food " ; String str2 = " money " ; EditDistDP ( str1 , str2 ) ; } }
import java . util . * ; class GFG { static int minDis ( String s1 , String s2 , int n , int m , int [ ] [ ] dp ) { if ( n == 0 ) return m ; if ( m == 0 ) return n ; if ( dp [ n ] [ m ] != - 1 ) return dp [ n ] [ m ] ; if ( s1 . charAt ( n - 1 ) == s2 . charAt ( m - 1 ) ) { if ( dp [ n - 1 ] [ m - 1 ] == - 1 ) { return dp [ n ] [ m ] = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } else return dp [ n ] [ m ] = dp [ n - 1 ] [ m - 1 ] ; } else { int m1 , m2 , m3 ; if ( dp [ n - 1 ] [ m ] != - 1 ) { m1 = dp [ n - 1 ] [ m ] ; } else { m1 = minDis ( s1 , s2 , n - 1 , m , dp ) ; } if ( dp [ n ] [ m - 1 ] != - 1 ) { m2 = dp [ n ] [ m - 1 ] ; } else { m2 = minDis ( s1 , s2 , n , m - 1 , dp ) ; } if ( dp [ n - 1 ] [ m - 1 ] != - 1 ) { m3 = dp [ n - 1 ] [ m - 1 ] ; } else { m3 = minDis ( s1 , s2 , n - 1 , m - 1 , dp ) ; } return dp [ n ] [ m ] = 1 + Math . min ( m1 , Math . min ( m2 , m3 ) ) ; } } public static void main ( String [ ] args ) { String str1 = " voldemort " ; String str2 = " dumbledore " ; int n = str1 . length ( ) , m = str2 . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) Arrays . fill ( dp [ i ] , - 1 ) ; System . out . print ( minDis ( str1 , str2 , n , m , dp ) ) ; } }
import java . util . * ; class GFG { static int row = 3 ; static int col = 3 ; static int minCost ( int cost [ ] [ ] ) { for ( int i = 1 ; i < row ; i ++ ) { cost [ i ] [ 0 ] += cost [ i - 1 ] [ 0 ] ; } for ( int j = 1 ; j < col ; j ++ ) { cost [ 0 ] [ j ] += cost [ 0 ] [ j - 1 ] ; } for ( int i = 1 ; i < row ; i ++ ) { for ( int j = 1 ; j < col ; j ++ ) { cost [ i ] [ j ] += Math . min ( cost [ i - 1 ] [ j - 1 ] , Math . min ( cost [ i - 1 ] [ j ] , cost [ i ] [ j - 1 ] ) ) ; } } return cost [ row - 1 ] [ col - 1 ] ; } public static void main ( String [ ] args ) { int cost [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 8 , 2 } , { 1 , 5 , 3 } } ; System . out . print ( minCost ( cost ) + "NEW_LINE"); } }
public static int count ( int S [ ] , int m , int n ) { int table [ ] = new int [ n + 1 ] ; table [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = S [ i ] ; j <= n ; j ++ ) table [ j ] += table [ j - S [ i ] ] ; return table [ n ] ; }
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 100 ] [ 100 ] ; static int matrixChainMemoised ( int [ ] p , int i , int j ) { if ( i == j ) { return 0 ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } dp [ i ] [ j ] = Integer . MAX_VALUE ; for ( int k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , matrixChainMemoised ( p , i , k ) + matrixChainMemoised ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ) ; } return dp [ i ] [ j ] ; } static int MatrixChainOrder ( int [ ] p , int n ) { int i = 1 , j = n - 1 ; return matrixChainMemoised ( p , i , j ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; System . out . println ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int binomialCoeff ( int n , int k ) { int C [ ] = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . printf ( " Value ▁ of ▁ C ( % d , ▁ % d ) ▁ is ▁ % d ▁ " , n , k , binomialCoeff ( n , k ) ) ; } }
import java . util . * ; class GFG { static int binomialCoeff ( int n , int r ) { if ( r > n ) return 0 ; long m = 1000000007 ; long inv [ ] = new long [ r + 1 ] ; inv [ 0 ] = 1 ; if ( r + 1 >= 2 ) inv [ 1 ] = 1 ; for ( int i = 2 ; i <= r ; i ++ ) { inv [ i ] = m - ( m / i ) * inv [ ( int ) ( m % i ) ] % m ; } int ans = 1 ; for ( int i = 2 ; i <= r ; i ++ ) { ans = ( int ) ( ( ( ans % m ) * ( inv [ i ] % m ) ) % m ) ; } for ( int i = n ; i >= ( n - r + 1 ) ; i -- ) { ans = ( int ) ( ( ( ans % m ) * ( i % m ) ) % m ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , r = 2 ; System . out . print ( " Value ▁ of ▁ C ( " + n + " , ▁ " + r + " ) ▁ is ▁ " + binomialCoeff ( n , r ) + "NEW_LINE"); } }
public class GFG { static int eggDrop ( int n , int k ) { if ( k == 1 k == 0 ) return k ; if ( n == 1 ) return k ; int min = Integer . MAX_VALUE ; int x , res ; for ( x = 1 ; x <= k ; x ++ ) { res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ; if ( res < min ) min = res ; } return min + 1 ; } public static void main ( String args [ ] ) { int n = 2 , k = 10 ; System . out . print ( " Minimum ▁ number ▁ of ▁ " + " trials ▁ in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }
class EggDrop { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int eggDrop ( int n , int k ) { int eggFloor [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i ] [ 1 ] = 1 ; eggFloor [ i ] [ 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 ] [ j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 2 ; j <= k ; j ++ ) { eggFloor [ i ] [ j ] = Integer . MAX_VALUE ; for ( x = 1 ; x <= j ; x ++ ) { res = 1 + max ( eggFloor [ i - 1 ] [ x - 1 ] , eggFloor [ i ] [ j - x ] ) ; if ( res < eggFloor [ i ] [ j ] ) eggFloor [ i ] [ j ] = res ; } } } return eggFloor [ n ] [ k ] ; } public static void main ( String args [ ] ) { int n = 2 , k = 10 ; System . out . println ( " Minimum ▁ number ▁ of ▁ trials ▁ in ▁ worst " + " ▁ case ▁ with ▁ " + n + " ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }
class LPS { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int lps ( String seq ) { int n = seq . length ( ) ; int i , j , cl ; int L [ ] [ ] = new int [ n ] [ n ] ; for ( i = 0 ; i < n ; i ++ ) L [ i ] [ i ] = 1 ; for ( cl = 2 ; cl <= n ; cl ++ ) { for ( i = 0 ; i < n - cl + 1 ; i ++ ) { j = i + cl - 1 ; if ( seq . charAt ( i ) == seq . charAt ( j ) && cl == 2 ) L [ i ] [ j ] = 2 ; else if ( seq . charAt ( i ) == seq . charAt ( j ) ) L [ i ] [ j ] = L [ i + 1 ] [ j - 1 ] + 2 ; else L [ i ] [ j ] = max ( L [ i ] [ j - 1 ] , L [ i + 1 ] [ j ] ) ; } } return L [ 0 ] [ n - 1 ] ; } public static void main ( String args [ ] ) { String seq = " GEEKSFORGEEKS " ; int n = seq . length ( ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ lps ▁ is ▁ " + lps ( seq ) ) ; } }
class RodCutting { static int cutRod ( int price [ ] , int n ) { if ( n <= 0 ) return 0 ; int max_val = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , price [ i ] + cutRod ( price , n - i - 1 ) ) ; return max_val ; } public static void main ( String args [ ] ) { int arr [ ] = new int [ ] { 1 , 5 , 8 , 9 , 10 , 17 , 17 , 20 } ; int size = arr . length ; System . out . println ( " Maximum ▁ Obtainable ▁ Value ▁ is ▁ " + cutRod ( arr , size ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class LBS { static int lbs ( int arr [ ] , int n ) { int i , j ; int [ ] lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int [ ] lds = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lds [ i ] = 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) for ( j = n - 1 ; j > i ; j -- ) if ( arr [ i ] > arr [ j ] && lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; int max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < n ; i ++ ) if ( lis [ i ] + lds [ i ] - 1 > max ) max = lis [ i ] + lds [ i ] - 1 ; return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ LBS ▁ is ▁ " + lbs ( arr , n ) ) ; } }
public class GFG { static boolean isPalindrome ( String string , int i , int j ) { while ( i < j ) { if ( string . charAt ( i ) != string . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int minPalPartion ( String string , int i , int j ) { if ( i >= j || isPalindrome ( string , i , j ) ) return 0 ; int ans = Integer . MAX_VALUE , count ; for ( int k = i ; k < j ; k ++ ) { count = minPalPartion ( string , i , k ) + minPalPartion ( string , k + 1 , j ) + 1 ; ans = Math . min ( ans , count ) ; } return ans ; } public static void main ( String args [ ] ) { String str = " ababbbabbababa " ; System . out . println ( " Min ▁ cuts ▁ needed ▁ for ▁ " + " Palindrome ▁ Partitioning ▁ is ▁ " + minPalPartion ( str , 0 , str . length ( ) - 1 ) ) ; } }
import java . io . * ; class GFG { public static int minCut ( String a ) { int [ ] cut = new int [ a . length ( ) ] ; boolean [ ] [ ] palindrome = new boolean [ a . length ( ) ] [ a . length ( ) ] ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { int minCut = i ; for ( int j = 0 ; j <= i ; j ++ ) { if ( a . charAt ( i ) == a . charAt ( j ) && ( i - j < 2 palindrome [ j + 1 ] [ i - 1 ] ) ) { palindrome [ j ] [ i ] = true ; minCut = Math . min ( minCut , j == 0 ? 0 : ( cut [ j - 1 ] + 1 ) ) ; } } cut [ i ] = minCut ; } return cut [ a . length ( ) - 1 ] ; } public static void main ( String [ ] args ) { System . out . println ( minCut ( " aab " ) ) ; System . out . println ( minCut ( " aabababaxx " ) ) ; } }
import java . io . * ; class GFG { public static boolean findPartiion ( int arr [ ] , int n ) { int sum = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; boolean [ ] part = new boolean [ sum / 2 + 1 ] ; for ( i = 0 ; i <= sum / 2 ; i ++ ) { part [ i ] = false ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = sum / 2 ; j >= arr [ i ] ; j -- ) { if ( part [ j - arr [ i ] ] == true j == arr [ i ] ) part [ j ] = true ; } } return part [ sum / 2 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 3 , 2 , 3 , 2 } ; int n = 6 ; if ( findPartiion ( arr , n ) == true ) System . out . println ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ " + " subsets ▁ of ▁ equal ▁ sum " ) ; else System . out . println ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ " + " two ▁ subsets ▁ of ▁ equal ▁ sum " ) ; } }
public class WordWrap { final int MAX = Integer . MAX_VALUE ; int printSolution ( int p [ ] , int n ) { int k ; if ( p [ n ] == 1 ) k = 1 ; else k = printSolution ( p , p [ n ] - 1 ) + 1 ; System . out . println ( " Line ▁ number " + " ▁ " + k + " : ▁ " + " From ▁ word ▁ no . " + " ▁ " + p [ n ] + " ▁ " + " to " + " ▁ " + n ) ; return k ; } void solveWordWrap ( int l [ ] , int n , int M ) { int extras [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; int lc [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; int c [ ] = new int [ n + 1 ] ; int p [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { extras [ i ] [ i ] = M - l [ i - 1 ] ; for ( int j = i + 1 ; j <= n ; j ++ ) extras [ i ] [ j ] = extras [ i ] [ j - 1 ] - l [ j - 1 ] - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { if ( extras [ i ] [ j ] < 0 ) lc [ i ] [ j ] = MAX ; else if ( j == n && extras [ i ] [ j ] >= 0 ) lc [ i ] [ j ] = 0 ; else lc [ i ] [ j ] = extras [ i ] [ j ] * extras [ i ] [ j ] ; } } c [ 0 ] = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { c [ j ] = MAX ; for ( int i = 1 ; i <= j ; i ++ ) { if ( c [ i - 1 ] != MAX && lc [ i ] [ j ] != MAX && ( c [ i - 1 ] + lc [ i ] [ j ] < c [ j ] ) ) { c [ j ] = c [ i - 1 ] + lc [ i ] [ j ] ; p [ j ] = i ; } } } printSolution ( p , n ) ; } public static void main ( String args [ ] ) { WordWrap w = new WordWrap ( ) ; int l [ ] = { 3 , 2 , 2 , 5 } ; int n = l . length ; int M = 6 ; w . solveWordWrap ( l , n , M ) ; } }
class GFG { static int maxDivide ( int a , int b ) { while ( a % b == 0 ) a = a / b ; return a ; } static int isUgly ( int no ) { no = maxDivide ( no , 2 ) ; no = maxDivide ( no , 3 ) ; no = maxDivide ( no , 5 ) ; return ( no == 1 ) ? 1 : 0 ; } static int getNthUglyNo ( int n ) { int i = 1 ; int count = 1 ; while ( n > count ) { i ++ ; if ( isUgly ( i ) == 1 ) count ++ ; } return i ; } public static void main ( String args [ ] ) { int no = getNthUglyNo ( 150 ) ; System . out . println ( "150th ▁ ugly ▁ " + " no . ▁ is ▁ " + no ) ; } }
import java . util . * ; class GFG { static void printSubStr ( String str , int low , int high ) { for ( int i = low ; i <= high ; ++ i ) System . out . print ( str . charAt ( i ) ) ; } static int longestPalSubstr ( String str ) { int n = str . length ( ) ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i ; j < str . length ( ) ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str . charAt ( i + k ) != str . charAt ( j - k ) ) flag = 0 ; if ( flag != 0 && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } System . out . print ( " Longest ▁ palindrome ▁ subString ▁ is : ▁ " ) ; printSubStr ( str , start , start + maxLength - 1 ) ; return maxLength ; } public static void main ( String [ ] args ) { String str = " forgeeksskeegfor " ; System . out . print ( " Length is : " + longestPalSubstr ( str ) ) ; } }
public class LongestPalinSubstring { static void printSubStr ( String str , int low , int high ) { System . out . println ( str . substring ( low , high + 1 ) ) ; } static int longestPalSubstr ( String str ) { int n = str . length ( ) ; boolean table [ ] [ ] = new boolean [ n ] [ n ] ; int maxLength = 1 ; for ( int i = 0 ; i < n ; ++ i ) table [ i ] [ i ] = true ; int start = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( str . charAt ( i ) == str . charAt ( i + 1 ) ) { table [ i ] [ i + 1 ] = true ; start = i ; maxLength = 2 ; } } for ( int k = 3 ; k <= n ; ++ k ) { for ( int i = 0 ; i < n - k + 1 ; ++ i ) { int j = i + k - 1 ; if ( table [ i + 1 ] [ j - 1 ] && str . charAt ( i ) == str . charAt ( j ) ) { table [ i ] [ j ] = true ; if ( k > maxLength ) { start = i ; maxLength = k ; } } } } System . out . print ( " Longest ▁ palindrome ▁ substring ▁ is ; ▁ " ) ; printSubStr ( str , start , start + maxLength - 1 ) ; return maxLength ; } public static void main ( String [ ] args ) { String str = " forgeeksskeegfor " ; System . out . println ( " Length ▁ is : ▁ " + longestPalSubstr ( str ) ) ; } }
public class GFG { static int sum ( int freq [ ] , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) s += freq [ k ] ; return s ; } static int optCost ( int freq [ ] , int i , int j ) { if ( j < i ) return 0 ; if ( j == i ) return freq [ i ] ; int fsum = sum ( freq , i , j ) ; int min = Integer . MAX_VALUE ; for ( int r = i ; r <= j ; ++ r ) { int cost = optCost ( freq , i , r - 1 ) + optCost ( freq , r + 1 , j ) ; if ( cost < min ) min = cost ; } return min + fsum ; } static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) { return optCost ( freq , 0 , n - 1 ) ; } public static void main ( String [ ] args ) { int keys [ ] = { 10 , 12 , 20 } ; int freq [ ] = { 34 , 8 , 50 } ; int n = keys . length ; System . out . println ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }
public class Optimal_BST2 { static int sum ( int freq [ ] , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) { if ( k >= freq . length ) continue ; s += freq [ k ] ; } return s ; } static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) { int cost [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cost [ i ] [ i ] = freq [ i ] ; for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i <= n - L + 1 ; i ++ ) { int j = i + L - 1 ; cost [ i ] [ j ] = Integer . MAX_VALUE ; for ( int r = i ; r <= j ; r ++ ) { int c = ( ( r > i ) ? cost [ i ] [ r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 ] [ j ] : 0 ) + sum ( freq , i , j ) ; if ( c < cost [ i ] [ j ] ) cost [ i ] [ j ] = c ; } } } return cost [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int keys [ ] = { 10 , 12 , 20 } ; int freq [ ] = { 34 , 8 , 50 } ; int n = keys . length ; System . out . println ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }
class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static class Node { int data ; Node left , right ; } ; static int LISS ( Node root ) { if ( root == null ) return 0 ; int size_excl = LISS ( root . left ) + LISS ( root . right ) ; int size_incl = 1 ; if ( root . left != null ) size_incl += LISS ( root . left . left ) + LISS ( root . left . right ) ; if ( root . right != null ) size_incl += LISS ( root . right . left ) + LISS ( root . right . right ) ; return max ( size_incl , size_excl ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( 20 ) ; root . left = newNode ( 8 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 12 ) ; root . left . right . left = newNode ( 10 ) ; root . left . right . right = newNode ( 14 ) ; root . right = newNode ( 22 ) ; root . right . right = newNode ( 25 ) ; System . out . println ( " Size ▁ of ▁ the ▁ Largest " + " ▁ Independent ▁ Set ▁ is ▁ " + LISS ( root ) ) ; } }
class GFG { static boolean isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } public static void main ( String args [ ] ) { int set [ ] = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) System . out . println ( " Found ▁ a ▁ subset " + " ▁ with ▁ given ▁ sum " ) ; else System . out . println ( " No ▁ subset ▁ with " + " ▁ given ▁ sum " ) ; } }
class GFG { static boolean isSubsetSum ( int set [ ] , int n , int sum ) { boolean subset [ ] [ ] = new boolean [ sum + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ i ] [ 0 ] = false ; for ( int i = 1 ; i <= sum ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { subset [ i ] [ j ] = subset [ i ] [ j - 1 ] ; if ( i >= set [ j - 1 ] ) subset [ i ] [ j ] = subset [ i ] [ j ] || subset [ i - set [ j - 1 ] ] [ j - 1 ] ; } } for ( int i = 0 ; i <= sum ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) System . out . println ( subset [ i ] [ j ] ) ; } return subset [ sum ] [ n ] ; } public static void main ( String args [ ] ) { int set [ ] = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) System . out . println ( " Found ▁ a ▁ subset " + " ▁ with ▁ given ▁ sum " ) ; else System . out . println ( " No ▁ subset ▁ with " + " ▁ given ▁ sum " ) ; } }
class GFG { static int countParenth ( char symb [ ] , char oper [ ] , int n ) { int F [ ] [ ] = new int [ n ] [ n ] ; int T [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { F [ i ] [ i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ; T [ i ] [ i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ; } for ( int gap = 1 ; gap < n ; ++ gap ) { for ( int i = 0 , j = gap ; j < n ; ++ i , ++ j ) { T [ i ] [ j ] = F [ i ] [ j ] = 0 ; for ( int g = 0 ; g < gap ; g ++ ) { int k = i + g ; int tik = T [ i ] [ k ] + F [ i ] [ k ] ; int tkj = T [ k + 1 ] [ j ] + F [ k + 1 ] [ j ] ; if ( oper [ k ] == ' & ' ) { T [ i ] [ j ] += T [ i ] [ k ] * T [ k + 1 ] [ j ] ; F [ i ] [ j ] += ( tik * tkj - T [ i ] [ k ] * T [ k + 1 ] [ j ] ) ; } if ( oper [ k ] == ' ▁ ' ) { F [ i ] [ j ] += F [ i ] [ k ] * F [ k + 1 ] [ j ] ; T [ i ] [ j ] += ( tik * tkj - F [ i ] [ k ] * F [ k + 1 ] [ j ] ) ; } if ( oper [ k ] == ' ^ ' ) { T [ i ] [ j ] += F [ i ] [ k ] * T [ k + 1 ] [ j ] + T [ i ] [ k ] * F [ k + 1 ] [ j ] ; F [ i ] [ j ] += T [ i ] [ k ] * T [ k + 1 ] [ j ] + F [ i ] [ k ] * F [ k + 1 ] [ j ] ; } } } } return T [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { char symbols [ ] = " TTFT " . toCharArray ( ) ; char operators [ ] = " | & ^ " . toCharArray ( ) ; int n = symbols . length ; System . out . println ( countParenth ( symbols , operators , n ) ) ; } }
class GFG { static int getCount ( char keypad [ ] [ ] , int n ) { if ( keypad == null n <= 0 ) return 0 ; if ( n == 1 ) return 10 ; int [ ] odd = new int [ 10 ] ; int [ ] even = new int [ 10 ] ; int i = 0 , j = 0 , useOdd = 0 , totalCount = 0 ; for ( i = 0 ; i <= 9 ; i ++ ) odd [ i ] = 1 ; for ( j = 2 ; j <= n ; j ++ ) { useOdd = 1 - useOdd ; if ( useOdd == 1 ) { even [ 0 ] = odd [ 0 ] + odd [ 8 ] ; even [ 1 ] = odd [ 1 ] + odd [ 2 ] + odd [ 4 ] ; even [ 2 ] = odd [ 2 ] + odd [ 1 ] + odd [ 3 ] + odd [ 5 ] ; even [ 3 ] = odd [ 3 ] + odd [ 2 ] + odd [ 6 ] ; even [ 4 ] = odd [ 4 ] + odd [ 1 ] + odd [ 5 ] + odd [ 7 ] ; even [ 5 ] = odd [ 5 ] + odd [ 2 ] + odd [ 4 ] + odd [ 8 ] + odd [ 6 ] ; even [ 6 ] = odd [ 6 ] + odd [ 3 ] + odd [ 5 ] + odd [ 9 ] ; even [ 7 ] = odd [ 7 ] + odd [ 4 ] + odd [ 8 ] ; even [ 8 ] = odd [ 8 ] + odd [ 0 ] + odd [ 5 ] + odd [ 7 ] + odd [ 9 ] ; even [ 9 ] = odd [ 9 ] + odd [ 6 ] + odd [ 8 ] ; } else { odd [ 0 ] = even [ 0 ] + even [ 8 ] ; odd [ 1 ] = even [ 1 ] + even [ 2 ] + even [ 4 ] ; odd [ 2 ] = even [ 2 ] + even [ 1 ] + even [ 3 ] + even [ 5 ] ; odd [ 3 ] = even [ 3 ] + even [ 2 ] + even [ 6 ] ; odd [ 4 ] = even [ 4 ] + even [ 1 ] + even [ 5 ] + even [ 7 ] ; odd [ 5 ] = even [ 5 ] + even [ 2 ] + even [ 4 ] + even [ 8 ] + even [ 6 ] ; odd [ 6 ] = even [ 6 ] + even [ 3 ] + even [ 5 ] + even [ 9 ] ; odd [ 7 ] = even [ 7 ] + even [ 4 ] + even [ 8 ] ; odd [ 8 ] = even [ 8 ] + even [ 0 ] + even [ 5 ] + even [ 7 ] + even [ 9 ] ; odd [ 9 ] = even [ 9 ] + even [ 6 ] + even [ 8 ] ; } } totalCount = 0 ; if ( useOdd == 1 ) { for ( i = 0 ; i <= 9 ; i ++ ) totalCount += even [ i ] ; } else { for ( i = 0 ; i <= 9 ; i ++ ) totalCount += odd [ i ] ; } return totalCount ; } public static void main ( String [ ] args ) { char keypad [ ] [ ] = { { '1' , '2' , '3' } , { '4' , '5' , '6' } , { '7' , '8' , '9' } , { ' * ' , '0' , ' # ' } } ; System . out . printf ( "Count for numbers of length %d: %dNEW_LINE", 1, getCount ( keypad , 1 ) ) ; System . out . printf ( "Count for numbers of length %d: %dNEW_LINE", 2, getCount ( keypad , 2 ) ) ; System . out . printf ( "Count for numbers of length %d: %dNEW_LINE", 3, getCount ( keypad , 3 ) ) ; System . out . printf ( "Count for numbers of length %d: %dNEW_LINE", 4, getCount ( keypad , 4 ) ) ; System . out . printf ( "Count for numbers of length %d: %dNEW_LINE", 5, getCount ( keypad , 5 ) ) ; } }
class sum_dig { static int lookup [ ] [ ] = new int [ 101 ] [ 501 ] ; static int countRec ( int n , int sum ) { if ( n == 0 ) return sum == 0 ? 1 : 0 ; if ( lookup [ n ] [ sum ] != - 1 ) return lookup [ n ] [ sum ] ; int ans = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return lookup [ n ] [ sum ] = ans ; } static int finalCount ( int n , int sum ) { for ( int i = 0 ; i <= 100 ; ++ i ) { for ( int j = 0 ; j <= 500 ; ++ j ) { lookup [ i ] [ j ] = - 1 ; } } int ans = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) if ( sum - i >= 0 ) ans += countRec ( n - 1 , sum - i ) ; return ans ; } public static void main ( String args [ ] ) { int n = 3 , sum = 5 ; System . out . println ( finalCount ( n , sum ) ) ; } }
class NDN { static int countNonDecreasing ( int n ) { int dp [ ] [ ] = new int [ 10 ] [ n + 1 ] ; for ( int i = 0 ; i < 10 ; i ++ ) dp [ i ] [ 1 ] = 1 ; for ( int digit = 0 ; digit <= 9 ; digit ++ ) { for ( int len = 2 ; len <= n ; len ++ ) { for ( int x = 0 ; x <= digit ; x ++ ) dp [ digit ] [ len ] += dp [ x ] [ len - 1 ] ; } } int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) count += dp [ i ] [ n ] ; return count ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( countNonDecreasing ( n ) ) ; } }
public class GFG { static long countNonDecreasing ( int n ) { int N = 10 ; long count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { count *= ( N + i - 1 ) ; count /= i ; } return count ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( countNonDecreasing ( n ) ) ; } }
class squares { static int getMinSquares ( int n ) { if ( n <= 3 ) return n ; int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { dp [ i ] = i ; for ( int x = 1 ; x <= Math . ceil ( Math . sqrt ( i ) ) ; x ++ ) { int temp = x * x ; if ( temp > i ) break ; else dp [ i ] = Math . min ( dp [ i ] , 1 + dp [ i - temp ] ) ; } } int res = dp [ n ] ; return res ; } public static void main ( String args [ ] ) { System . out . println ( getMinSquares ( 6 ) ) ; } }
import java . util . * ; import java . awt . Point ; class GFG { public static int numSquares ( int n ) { int visited [ ] = new int [ n + 1 ] ; Queue < Point > q = new LinkedList < > ( ) ; int ans = Integer . MAX_VALUE ; q . add ( new Point ( n , 0 ) ) ; visited [ n ] = 1 ; while ( q . size ( ) != 0 ) { Point p = q . peek ( ) ; q . poll ( ) ; if ( p . x == 0 ) ans = Math . min ( ans , p . y ) ; for ( int i = 1 ; i * i <= p . x ; i ++ ) { int path = ( p . x - ( i * i ) ) ; if ( path >= 0 && ( visited [ path ] == 0 path == 0 ) ) { visited [ path ] = 1 ; q . add ( new Point ( path , p . y + 1 ) ) ; } } } return ans ; } public static void main ( String [ ] args ) { System . out . println ( numSquares ( 12 ) ) ; } }
class coin { static int minCoins ( int coins [ ] , int m , int V ) { if ( V == 0 ) return 0 ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( coins [ i ] <= V ) { int sub_res = minCoins ( coins , m , V - coins [ i ] ) ; if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < res ) res = sub_res + 1 ; } } return res ; } public static void main ( String args [ ] ) { int coins [ ] = { 9 , 6 , 5 , 1 } ; int m = coins . length ; int V = 11 ; System . out . println ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }
import java . io . * ; class GFG { static int minCoins ( int coins [ ] , int m , int V ) { int table [ ] = new int [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) if ( coins [ j ] <= i ) { int sub_res = table [ i - coins [ j ] ] ; if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < table [ i ] ) table [ i ] = sub_res + 1 ; } } if ( table [ V ] == Integer . MAX_VALUE ) return - 1 ; return table [ V ] ; } public static void main ( String [ ] args ) { int coins [ ] = { 9 , 6 , 5 , 1 } ; int m = coins . length ; int V = 11 ; System . out . println ( " Minimum ▁ coins ▁ required ▁ is ▁ " + minCoins ( coins , m , V ) ) ; } }
class GFG { static int superSeq ( String X , String Y , int m , int n ) { if ( m == 0 ) return n ; if ( n == 0 ) return m ; if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + Math . min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; } public static void main ( String args [ ] ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; System . out . println ( " Length ▁ of ▁ the ▁ shortest " + " supersequence ▁ is : ▁ " + superSeq ( X , Y , X . length ( ) , Y . length ( ) ) ) ; } }
class GFG { static int superSeq ( String X , String Y , int m , int n ) { int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return dp [ m ] [ n ] ; } public static void main ( String args [ ] ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; System . out . println ( " Length ▁ of ▁ the ▁ shortest ▁ supersequence ▁ is ▁ " + superSeq ( X , Y , X . length ( ) , Y . length ( ) ) ) ; } }
import java . io . * ; class GFG { static int sumOfDigitsFrom1ToN ( int n ) { int result = 0 ; for ( int x = 1 ; x <= n ; x ++ ) result += sumOfDigits ( x ) ; return result ; } static int sumOfDigits ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += x % 10 ; x = x / 10 ; } return sum ; } public static void main ( String args [ ] ) { int n = 328 ; System . out . println ( " Sum ▁ of ▁ digits ▁ in ▁ numbers " + " ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int sumOfDigitsFrom1ToN ( int n ) { if ( n < 10 ) return ( n * ( n + 1 ) / 2 ) ; int d = ( int ) ( Math . log10 ( n ) ) ; int a [ ] = new int [ d + 1 ] ; a [ 0 ] = 0 ; a [ 1 ] = 45 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( Math . ceil ( Math . pow ( 10 , i - 1 ) ) ) ; int p = ( int ) ( Math . ceil ( Math . pow ( 10 , d ) ) ) ; int msd = n / p ; return ( msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToN ( n % p ) ) ; } public static void main ( String args [ ] ) { int n = 328 ; System . out . println ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }
class Building { static int countWays ( int N ) { if ( N == 1 ) return 4 ; int countB = 1 , countS = 1 , prev_countB , prev_countS ; for ( int i = 2 ; i <= N ; i ++ ) { prev_countB = countB ; prev_countS = countS ; countS = prev_countB + prev_countS ; countB = prev_countS ; } int result = countS + countB ; return ( result * result ) ; } public static void main ( String args [ ] ) { int N = 3 ; System . out . println ( " Count ▁ of ▁ ways ▁ for ▁ " + N + " ▁ sections ▁ is ▁ " + countWays ( N ) ) ; } }
import java . io . * ; class GFG { static int findoptimal ( int N ) { if ( N <= 6 ) return N ; int screen [ ] = new int [ N ] ; int b ; int n ; for ( n = 1 ; n <= 6 ; n ++ ) screen [ n - 1 ] = n ; for ( n = 7 ; n <= N ; n ++ ) { screen [ n - 1 ] = 0 ; for ( b = n - 3 ; b >= 1 ; b -- ) { int curr = ( n - b - 1 ) * screen [ b - 1 ] ; if ( curr > screen [ n - 1 ] ) screen [ n - 1 ] = curr ; } } return screen [ N - 1 ] ; } public static void main ( String [ ] args ) { int N ; for ( N = 1 ; N <= 20 ; N ++ ) System . out . println ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with ▁ keystrokes ▁ is ▁ " + N + findoptimal ( N ) ) ; } }
class GFG { static int findoptimal ( int N ) { if ( N <= 6 ) return N ; int [ ] screen = new int [ N ] ; int b ; int n ; for ( n = 1 ; n <= 6 ; n ++ ) screen [ n - 1 ] = n ; for ( n = 7 ; n <= N ; n ++ ) { screen [ n - 1 ] = Math . max ( 2 * screen [ n - 4 ] , Math . max ( 3 * screen [ n - 5 ] , 4 * screen [ n - 6 ] ) ) ; } return screen [ N - 1 ] ; } public static void main ( String [ ] args ) { int N ; for ( N = 1 ; N <= 20 ; N ++ ) System . out . printf ( " Maximum ▁ Number ▁ of ▁ A ' s ▁ with " + " %d keystrokes is %dNEW_LINE", N , findoptimal ( N ) ) ; } }
class GFG { static int NO_OF_CHARS = 256 ; static int getNextState ( char [ ] pat , int M , int state , int x ) { if ( state < M && x == pat [ state ] ) return state + 1 ; int ns , i ; for ( ns = state ; ns > 0 ; ns -- ) { if ( pat [ ns - 1 ] == x ) { for ( i = 0 ; i < ns - 1 ; i ++ ) if ( pat [ i ] != pat [ state - ns + 1 + i ] ) break ; if ( i == ns - 1 ) return ns ; } } return 0 ; } static void computeTF ( char [ ] pat , int M , int TF [ ] [ ] ) { int state , x ; for ( state = 0 ; state <= M ; ++ state ) for ( x = 0 ; x < NO_OF_CHARS ; ++ x ) TF [ state ] [ x ] = getNextState ( pat , M , state , x ) ; } static void search ( char [ ] pat , char [ ] txt ) { int M = pat . length ; int N = txt . length ; int [ ] [ ] TF = new int [ M + 1 ] [ NO_OF_CHARS ] ; computeTF ( pat , M , TF ) ; int i , state = 0 ; for ( i = 0 ; i < N ; i ++ ) { state = TF [ state ] [ txt [ i ] ] ; if ( state == M ) System . out . println ( " Pattern ▁ found ▁ " + " at ▁ index ▁ " + ( i - M + 1 ) ) ; } } public static void main ( String [ ] args ) { char [ ] pat = " AABAACAADAABAAABAA " . toCharArray ( ) ; char [ ] txt = " AABA " . toCharArray ( ) ; search ( txt , pat ) ; } }
class AWQ { static int NO_OF_CHARS = 256 ; static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static void badCharHeuristic ( char [ ] str , int size , int badchar [ ] ) { for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) badchar [ i ] = - 1 ; for ( i = 0 ; i < size ; i ++ ) badchar [ ( int ) str [ i ] ] = i ; } static void search ( char txt [ ] , char pat [ ] ) { int m = pat . length ; int n = txt . length ; int badchar [ ] = new int [ NO_OF_CHARS ] ; badCharHeuristic ( pat , m , badchar ) ; int s = 0 ; while ( s <= ( n - m ) ) { int j = m - 1 ; while ( j >= 0 && pat [ j ] == txt [ s + j ] ) j -- ; if ( j < 0 ) { System . out . println ( " Patterns ▁ occur ▁ at ▁ shift ▁ = ▁ " + s ) ; s += ( s + m < n ) ? m - badchar [ txt [ s + m ] ] : 1 ; } else s += max ( 1 , j - badchar [ txt [ s + j ] ] ) ; } } public static void main ( String [ ] args ) { char txt [ ] = " ABAAABCD " . toCharArray ( ) ; char pat [ ] = " ABC " . toCharArray ( ) ; search ( txt , pat ) ; } }
public class GFG { static final int MAX = 256 ; static boolean compare ( char arr1 [ ] , char arr2 [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static void search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; char [ ] countP = new char [ MAX ] ; char [ ] countTW = new char [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( i - M ) ) ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( N - M ) ) ; } public static void main ( String args [ ] ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; search ( pat , txt ) ; } }
import java . util . * ; class GFG { static void findLongestPalindromicString ( String text ) { int N = text . length ( ) ; if ( N == 0 ) return ; N = 2 * N + 1 ; int [ ] L = new int [ N + 1 ] ; L [ 0 ] = 0 ; L [ 1 ] = 1 ; int C = 1 ; int R = 2 ; int i = 0 ; int iMirror ; int maxLPSLength = 0 ; int maxLPSCenterPosition = 0 ; int start = - 1 ; int end = - 1 ; int diff = - 1 ; for ( i = 2 ; i < N ; i ++ ) { iMirror = 2 * C - i ; L [ i ] = 0 ; diff = R - i ; if ( diff > 0 ) L [ i ] = Math . min ( L [ iMirror ] , diff ) ; while ( ( ( i + L [ i ] ) + 1 < N && ( i - L [ i ] ) > 0 ) && ( ( ( i + L [ i ] + 1 ) % 2 == 0 ) || ( text . charAt ( ( i + L [ i ] + 1 ) / 2 ) == text . charAt ( ( i - L [ i ] - 1 ) / 2 ) ) ) ) { L [ i ] ++ ; } if ( L [ i ] > maxLPSLength ) { maxLPSLength = L [ i ] ; maxLPSCenterPosition = i ; } if ( i + L [ i ] > R ) { C = i ; R = i + L [ i ] ; } } start = ( maxLPSCenterPosition - maxLPSLength ) / 2 ; end = start + maxLPSLength - 1 ; System . out . printf ( " LPS ▁ of ▁ string ▁ is ▁ % s ▁ : ▁ " , text ) ; for ( i = start ; i <= end ; i ++ ) System . out . print ( text . charAt ( i ) ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { String text = " babcbabcbaccba " ; findLongestPalindromicString ( text ) ; text = " abaaba " ; findLongestPalindromicString ( text ) ; text = " abababa " ; findLongestPalindromicString ( text ) ; text = " abcbabcbabcba " ; findLongestPalindromicString ( text ) ; text = " forgeeksskeegfor " ; findLongestPalindromicString ( text ) ; text = " caba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcaba " ; findLongestPalindromicString ( text ) ; text = " abacdfgdcabba " ; findLongestPalindromicString ( text ) ; text = " abacdedcaba " ; findLongestPalindromicString ( text ) ; } }
import java . io . * ; class Permutation { static void printPatternUtil ( String str , char buf [ ] , int i , int j , int n ) { if ( i == n ) { buf [ j ] = ' \0' ; System . out . println ( buf ) ; return ; } buf [ j ] = str . charAt ( i ) ; printPatternUtil ( str , buf , i + 1 , j + 1 , n ) ; buf [ j ] = ' ▁ ' ; buf [ j + 1 ] = str . charAt ( i ) ; printPatternUtil ( str , buf , i + 1 , j + 2 , n ) ; } static void printPattern ( String str ) { int len = str . length ( ) ; char [ ] buf = new char [ 2 * len ] ; buf [ 0 ] = str . charAt ( 0 ) ; printPatternUtil ( str , buf , 1 , 1 , len ) ; } public static void main ( String [ ] args ) { String str = " ABCD " ; printPattern ( str ) ; } }
public class Suduko { static int N = 9 ; static void print ( int [ ] [ ] grid ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( grid [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static boolean isSafe ( int [ ] [ ] grid , int row , int col , int num ) { for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ row ] [ x ] == num ) return false ; for ( int x = 0 ; x <= 8 ; x ++ ) if ( grid [ x ] [ col ] == num ) return false ; int startRow = row - row % 3 , startCol = col - col % 3 ; for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) if ( grid [ i + startRow ] [ j + startCol ] == num ) return false ; return true ; } static boolean solveSuduko ( int grid [ ] [ ] , int row , int col ) { if ( row == N - 1 && col == N ) return true ; if ( col == N ) { row ++ ; col = 0 ; } if ( grid [ row ] [ col ] != 0 ) return solveSuduko ( grid , row , col + 1 ) ; for ( int num = 1 ; num < 10 ; num ++ ) { if ( isSafe ( grid , row , col , num ) ) { grid [ row ] [ col ] = num ; if ( solveSuduko ( grid , row , col + 1 ) ) return true ; } grid [ row ] [ col ] = 0 ; } return false ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 3 , 0 , 6 , 5 , 0 , 8 , 4 , 0 , 0 } , { 5 , 2 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 8 , 7 , 0 , 0 , 0 , 0 , 3 , 1 } , { 0 , 0 , 3 , 0 , 1 , 0 , 0 , 8 , 0 } , { 9 , 0 , 0 , 8 , 6 , 3 , 0 , 0 , 5 } , { 0 , 5 , 0 , 0 , 9 , 0 , 6 , 0 , 0 } , { 1 , 3 , 0 , 0 , 0 , 0 , 2 , 5 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 4 } , { 0 , 0 , 5 , 2 , 0 , 6 , 3 , 0 , 0 } } ; if ( solveSuduko ( grid , 0 , 0 ) ) print ( grid ) ; else System . out . println ( " No ▁ Solution ▁ exists " ) ; } }
static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; }
import java . io . * ; class GFG { public static int power ( int x , int y ) { if ( y == 0 ) return 1 ; if ( x == 0 ) return 0 ; return x * power ( x , y - 1 ) ; } public static void main ( String [ ] args ) { int x = 2 ; int y = 3 ; System . out . println ( power ( x , y ) ) ; } }
import java . io . * ; class GFG { public static int power ( int x , int y ) { return ( int ) Math . pow ( x , y ) ; } public static void main ( String [ ] args ) { int x = 2 ; int y = 3 ; System . out . println ( power ( x , y ) ) ; } }
import java . util . * ; class GfG { static int getMedian ( int [ ] a , int [ ] b , int startA , int startB , int endA , int endB ) { if ( endA - startA == 1 ) { return ( Math . max ( a [ startA ] , b [ startB ] ) + Math . min ( a [ endA ] , b [ endB ] ) ) / 2 ; } int m1 = median ( a , startA , endA ) ; int m2 = median ( b , startB , endB ) ; if ( m1 == m2 ) { return m1 ; } else if ( m1 < m2 ) { return getMedian ( a , b , ( endA + startA + 1 ) / 2 , startB , endA , ( endB + startB + 1 ) / 2 ) ; } else { return getMedian ( a , b , startA , ( endB + startB + 1 ) / 2 , ( endA + startA + 1 ) / 2 , endB ) ; } } static int median ( int [ ] arr , int start , int end ) { int n = end - start + 1 ; if ( n % 2 == 0 ) { return ( arr [ start + ( n / 2 ) ] + arr [ start + ( n / 2 - 1 ) ] ) / 2 ; } else { return arr [ start + n / 2 ] ; } } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 2 , 3 , 6 } ; int ar2 [ ] = { 4 , 6 , 8 , 10 } ; int n1 = ar1 . length ; int n2 = ar2 . length ; if ( n1 != n2 ) { System . out . println ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ " + " of ▁ unequal ▁ size " ) ; } else if ( n1 == 0 ) { System . out . println ( " Arrays ▁ are ▁ empty . " ) ; } else if ( n1 == 1 ) { System . out . println ( ( ar1 [ 0 ] + ar2 [ 0 ] ) / 2 ) ; } else { System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , 0 , 0 , ar1 . length - 1 , ar2 . length - 1 ) ) ; } } }
import java . io . * ; import java . util . * ; class GFG { public static int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) { int j = 0 ; int i = n - 1 ; while ( ar1 [ i ] > ar2 [ j ] && j < n && i > - 1 ) { int temp = ar1 [ i ] ; ar1 [ i ] = ar2 [ j ] ; ar2 [ j ] = temp ; i -- ; j ++ ; } Arrays . sort ( ar1 ) ; Arrays . sort ( ar2 ) ; return ( ar1 [ n - 1 ] + ar2 [ 0 ] ) / 2 ; } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 12 , 15 , 26 , 38 } ; int ar2 [ ] = { 2 , 13 , 17 , 30 , 45 } ; int n1 = 5 ; int n2 = 5 ; if ( n1 == n2 ) System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , n1 ) ) ; else System . out . println ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ of ▁ unequal ▁ size " ) ; } }
class GFG { static class Point { int x ; int y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static boolean onSegment ( Point p , Point q , Point r ) { if ( q . x <= Math . max ( p . x , r . x ) && q . x >= Math . min ( p . x , r . x ) && q . y <= Math . max ( p . y , r . y ) && q . y >= Math . min ( p . y , r . y ) ) return true ; return false ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static boolean doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; if ( o1 == 0 && onSegment ( p1 , p2 , q1 ) ) return true ; if ( o2 == 0 && onSegment ( p1 , q2 , q1 ) ) return true ; if ( o3 == 0 && onSegment ( p2 , p1 , q2 ) ) return true ; if ( o4 == 0 && onSegment ( p2 , q1 , q2 ) ) return true ; return false ; } public static void main ( String [ ] args ) { Point p1 = new Point ( 1 , 1 ) ; Point q1 = new Point ( 10 , 1 ) ; Point p2 = new Point ( 1 , 2 ) ; Point q2 = new Point ( 10 , 2 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; p1 = new Point ( 10 , 1 ) ; q1 = new Point ( 0 , 10 ) ; p2 = new Point ( 0 , 0 ) ; q2 = new Point ( 10 , 10 ) ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; p1 = new Point ( - 5 , - 5 ) ; q1 = new Point ( 0 , 0 ) ; p2 = new Point ( 1 , 1 ) ; q2 = new Point ( 10 , 10 ) ; ; if ( doIntersect ( p1 , q1 , p2 , q2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static float getAvg ( float prev_avg , float x , int n ) { return ( prev_avg * n + x ) / ( n + 1 ) ; } static void streamAvg ( float arr [ ] , int n ) { float avg = 0 ; for ( int i = 0 ; i < n ; i ++ ) { avg = getAvg ( avg , arr [ i ] , i ) ; System . out . printf ( "Average of %d numbers is %f NEW_LINE", i + 1, avg); } return ; } public static void main ( String [ ] args ) { float arr [ ] = { 10 , 20 , 30 , 40 , 50 , 60 } ; int n = arr . length ; streamAvg ( arr , n ) ; } }
class SieveOfEratosthenes { void sieveOfEratosthenes ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 30 ; System . out . print ( " Following ▁ are ▁ the ▁ prime ▁ numbers ▁ " ) ; System . out . println ( " smaller ▁ than ▁ or ▁ equal ▁ to ▁ " + n ) ; SieveOfEratosthenes g = new SieveOfEratosthenes ( ) ; g . sieveOfEratosthenes ( n ) ; } }
import java . io . * ; class GFG { static int maximumNumberDistinctPrimeRange ( int m , int n ) { long factorCount [ ] = new long [ n + 1 ] ; boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { factorCount [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] == true ) { factorCount [ i ] = 1 ; for ( int j = i * 2 ; j <= n ; j += i ) { factorCount [ j ] ++ ; prime [ j ] = false ; } } } int max = ( int ) factorCount [ m ] ; int num = m ; for ( int i = m ; i <= n ; i ++ ) { if ( factorCount [ i ] > max ) { max = ( int ) factorCount [ i ] ; num = i ; } } return num ; } public static void main ( String [ ] args ) { int m = 4 , n = 6 ; System . out . println ( maximumNumberDistinctPrimeRange ( m , n ) ) ; } }
class GFG { static int MAX_CHAR = 256 ; int count [ ] = new int [ MAX_CHAR ] ; static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static void populateAndIncreaseCount ( int [ ] count , char [ ] str ) { int i ; for ( i = 0 ; i < str . length ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } static void updatecount ( int [ ] count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } static int findRank ( char [ ] str ) { int len = str . length ; int mul = fact ( len ) ; int rank = 1 , i ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } public static void main ( String args [ ] ) { char str [ ] = " string " . toCharArray ( ) ; System . out . println ( findRank ( str ) ) ; } }
import java . io . * ; class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static void printPascal ( int n ) { for ( int line = 0 ; line < n ; line ++ ) { for ( int i = 0 ; i <= line ; i ++ ) System . out . print ( binomialCoeff ( line , i ) + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 7 ; printPascal ( n ) ; } }
class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } static class Vessel { int capacity , current ; public Vessel ( int capacity ) { this . capacity = capacity ; current = 0 ; } void makeOneLitre ( Vessel V2 ) { if ( gcd ( capacity , V2 . capacity ) != 1 ) return ; while ( current != 1 ) { if ( current == 0 ) current = capacity ; System . out . print ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + "NEW_LINE"); current = current - V2 . transfer ( current ) ; } System . out . print ( " Vessel ▁ 1 : ▁ " + current + " ▁ Vessel ▁ 2 : ▁ " + V2 . current + "NEW_LINE"); } int transfer ( int amount ) { if ( current + amount < capacity ) { current += amount ; return amount ; } int transferred = capacity - current ; current = 0 ; return transferred ; } } public static void main ( String [ ] args ) { int a = 3 , b = 7 ; Vessel V1 = new Vessel ( a ) ; Vessel V2 = new Vessel ( b ) ; V1 . makeOneLitre ( V2 ) ; } }
class GFG { static int findCeil ( int arr [ ] , int r , int l , int h ) { int mid ; while ( l < h ) { mid = l + ( ( h - l ) >> 1 ) ; if ( r > arr [ mid ] ) l = mid + 1 ; else h = mid ; } return ( arr [ l ] >= r ) ? l : - 1 ; } static int myRand ( int arr [ ] , int freq [ ] , int n ) { int prefix [ ] = new int [ n ] , i ; prefix [ 0 ] = freq [ 0 ] ; for ( i = 1 ; i < n ; ++ i ) prefix [ i ] = prefix [ i - 1 ] + freq [ i ] ; int r = ( ( int ) ( Math . random ( ) * ( 323567 ) ) % prefix [ n - 1 ] ) + 1 ; int indexc = findCeil ( prefix , r , 0 , n - 1 ) ; return arr [ indexc ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int freq [ ] = { 10 , 5 , 20 , 100 } ; int i , n = arr . length ; for ( i = 0 ; i < 5 ; i ++ ) System . out . println ( myRand ( arr , freq , n ) ) ; } }
class GFG { static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } public static void main ( String [ ] args ) { for ( int i = 1 ; i <= 10 ; i ++ ) System . out . println ( isFibonacci ( i ) ? i + " ▁ is ▁ a ▁ Fibonacci ▁ Number " : i + " ▁ is ▁ a ▁ not ▁ Fibonacci ▁ Number " ) ; } }
import java . io . * ; class GFG { static int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( " Count ▁ of ▁ trailing ▁ 0s ▁ in ▁ " + n + " ! ▁ is ▁ " + findTrailingZeros ( n ) ) ; } }
class CatalnNumber { int catalan ( int n ) { if ( n <= 1 ) { return 1 ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += catalan ( i ) * catalan ( n - i - 1 ) ; } return res ; } public static void main ( String [ ] args ) { CatalnNumber cn = new CatalnNumber ( ) ; for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( cn . catalan ( i ) + " ▁ " ) ; } } }
class GFG { static int catalanDP ( int n ) { int catalan [ ] = new int [ n + 2 ] ; catalan [ 0 ] = 1 ; catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) { catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } } return catalan [ n ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( catalanDP ( i ) + " ▁ " ) ; } } }
class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) { k = n - k ; } for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) { System . out . print ( catalan ( i ) + " ▁ " ) ; } } }
import java . util . * ; class GFG { static void catalan ( int n ) { int cat_ = 1 ; System . out . print ( cat_ + " ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { cat_ *= ( 4 * i - 2 ) ; cat_ /= ( i + 1 ) ; System . out . print ( cat_ + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; catalan ( n ) ; } }
public class ExcelColumnTitle { private static void printString ( int columnNumber ) { StringBuilder columnName = new StringBuilder ( ) ; while ( columnNumber > 0 ) { int rem = columnNumber % 26 ; if ( rem == 0 ) { columnName . append ( " Z " ) ; columnNumber = ( columnNumber / 26 ) - 1 ; } else { columnName . append ( ( char ) ( ( rem - 1 ) + ' A ' ) ) ; columnNumber = columnNumber / 26 ; } } System . out . println ( columnName . reverse ( ) ) ; } public static void main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }
import java . util . * ; class GFG { static void printString ( int n ) { int [ ] arr = new int [ 10000 ] ; int i = 0 ; while ( n > 0 ) { arr [ i ] = n % 26 ; n = n / 26 ; i ++ ; } for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( arr [ j ] <= 0 ) { arr [ j ] += 26 ; arr [ j + 1 ] = arr [ j + 1 ] - 1 ; } } for ( int j = i ; j >= 0 ; j -- ) { if ( arr [ j ] > 0 ) System . out . print ( ( char ) ( ' A ' + arr [ j ] - 1 ) ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { printString ( 26 ) ; printString ( 51 ) ; printString ( 52 ) ; printString ( 80 ) ; printString ( 676 ) ; printString ( 702 ) ; printString ( 705 ) ; } }
class GFG { static int getInvCount ( int [ ] [ ] arr ) { int inv_count = 0 ; for ( int i = 0 ; i < 3 - 1 ; i ++ ) for ( int j = i + 1 ; j < 3 ; j ++ ) if ( arr [ j ] [ i ] > 0 && arr [ j ] [ i ] > arr [ i ] [ j ] ) inv_count ++ ; return inv_count ; } static boolean isSolvable ( int [ ] [ ] puzzle ) { int invCount = getInvCount ( puzzle ) ; return ( invCount % 2 == 0 ) ; } public static void main ( String [ ] args ) { int [ ] [ ] puzzle = { { 1 , 8 , 2 } , { 0 , 4 , 3 } , { 7 , 6 , 5 } } ; if ( isSolvable ( puzzle ) ) System . out . println ( " Solvable " ) ; else System . out . println ( " Not ▁ Solvable " ) ; } }
import java . io . * ; class GFG { static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; } public static void main ( String args [ ] ) { System . out . println ( " Total ▁ Number ▁ of ▁ distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }
import java . io . * ; class GFG { static int countSolutions ( int n ) { int x = 0 , yCount , res = 0 ; for ( yCount = 0 ; yCount * yCount < n ; yCount ++ ) ; while ( yCount != 0 ) { res += yCount ; x ++ ; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ; } return res ; } public static void main ( String args [ ] ) { System . out . println ( " Total ▁ Number ▁ of ▁ distinct ▁ Non - Negative ▁ pairs ▁ is ▁ " + countSolutions ( 6 ) ) ; } }
class GFG { static final double EPSILON = 0.001 ; static double func ( double x ) { return x * x * x - x * x + 2 ; } static double derivFunc ( double x ) { return 3 * x * x - 2 * x ; } static void newtonRaphson ( double x ) { double h = func ( x ) / derivFunc ( x ) ; while ( Math . abs ( h ) >= EPSILON ) { h = func ( x ) / derivFunc ( x ) ; x = x - h ; } System . out . print ( " The ▁ value ▁ of ▁ the " + " ▁ root ▁ is ▁ : ▁ " + Math . round ( x * 100.0 ) / 100.0 ) ; } public static void main ( String [ ] args ) { double x0 = - 20 ; newtonRaphson ( x0 ) ; } }
class GFG { static boolean oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } public static void main ( String [ ] args ) { int x = 100 , y = - 100 ; if ( oppositeSigns ( x , y ) == true ) System . out . println ( " Signs ▁ are ▁ opposite " ) ; else System . out . println ( " Signs ▁ are ▁ not ▁ opposite " ) ; } }
class GFG { static int countSetBits ( int n ) { int bitCount = 0 ; for ( int i = 1 ; i <= n ; i ++ ) bitCount += countSetBitsUtil ( i ) ; return bitCount ; } static int countSetBitsUtil ( int x ) { if ( x <= 0 ) return 0 ; return ( x % 2 == 0 ? 0 : 1 ) + countSetBitsUtil ( x / 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " ) ; System . out . print ( countSetBits ( n ) ) ; } }
import java . io . * ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBits ( int n ) { int m = getLeftmostBit ( n ) ; return countSetBits ( n , m ) ; } static int countSetBits ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( n ) ) ; } }
static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; }
class GFG { static int swapBits ( int num , int p1 , int p2 , int n ) { int shift1 , shift2 , value1 , value2 ; while ( n -- > 0 ) { shift1 = 1 << p1 ; shift2 = 1 << p2 ; value1 = ( ( num & shift1 ) ) ; value2 = ( ( num & shift2 ) ) ; if ( ( value1 == 0 && value2 != 0 ) || ( value2 == 0 && value1 != 0 ) ) { if ( value1 != 0 ) { num = num & ( ~ shift1 ) ; num = num | shift2 ; } else { num = num & ( ~ shift2 ) ; num = num | shift1 ; } } p1 ++ ; p2 ++ ; } return num ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }
class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( ( Integer . SIZE / 8 ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . min ( x , Math . min ( y , z ) ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . println ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }
class GfG { static int smallest ( int x , int y , int z ) { if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ; return ( ( x / z ) != 1 ) ? x : z ; } public static void main ( String [ ] args ) { int x = 78 , y = 88 , z = 68 ; System . out . printf ( " Minimum ▁ of ▁ 3 ▁ numbers " + " ▁ is ▁ % d " , smallest ( x , y , z ) ) ; } }
import java . io . * ; class GFG { public static void changeToZero ( int a [ ] ) { a [ a [ 1 ] ] = a [ 1 - a [ 1 ] ] ; } public static void main ( String args [ ] ) { int [ ] arr ; arr = new int [ 2 ] ; arr [ 0 ] = 1 ; arr [ 1 ] = 0 ; changeToZero ( arr ) ; System . out . println ( " arr [ 0 ] = ▁ " + arr [ 0 ] ) ; System . out . println ( " arr [ 1 ] = ▁ " + arr [ 1 ] ) ; } }
class GFG { static int snoob ( int x ) { int rightOne , nextHigherOneBit , rightOnesPattern , next = 0 ; if ( x > 0 ) { rightOne = x & - x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; rightOnesPattern >>= 2 ; next = nextHigherOneBit | rightOnesPattern ; } return next ; } public static void main ( String [ ] args ) { int x = 156 ; System . out . println ( " Next ▁ higher ▁ number ▁ with ▁ same " + " number ▁ of ▁ set ▁ bits ▁ is ▁ " + snoob ( x ) ) ; } }
class GFG { static int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } public static void main ( String [ ] args ) { int x = 2 ; System . out . println ( multiplyWith3Point5 ( x ) ) ; } }
import java . io . * ; class GFG { static int multiplyWith3Point5 ( int x ) { int r = 0 ; int x1Shift = x << 1 ; int x2Shifts = x << 2 ; r = ( x ^ x1Shift ) ^ x2Shifts ; int c = ( x & x1Shift ) | ( x & x2Shifts ) | ( x1Shift & x2Shifts ) ; while ( c > 0 ) { c <<= 1 ; int t = r ; r ^= c ; c &= t ; } r = r >> 1 ; return r ; } public static void main ( String [ ] args ) { System . out . println ( multiplyWith3Point5 ( 5 ) ) ; } }
import java . io . * ; class GFG { static boolean isPowerOfFour ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ( n & 0xAAAAAAAA ) == 0 ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) System . out . println ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . println ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
import java . util . * ; class GFG { static double logn ( int n , int r ) { return Math . log ( n ) / Math . log ( r ) ; } static boolean isPowerOfFour ( int n ) { if ( n == 0 ) return false ; return Math . floor ( logn ( n , 4 ) ) == Math . ceil ( logn ( n , 4 ) ) ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) ) System . out . print ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . print ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }
class GFG { static int getModulo ( int n , int d ) { return ( n & ( d - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 6 ; int d = 4 ; System . out . println ( n + " ▁ moduo ▁ " + d + " ▁ is ▁ " + getModulo ( n , d ) ) ; } }
class GFG { static int CHAR_BIT = 4 ; static int INT_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } static int max ( int x , int y ) { return x - ( ( x - y ) & ( ( x - y ) >> ( INT_BIT * CHAR_BIT - 1 ) ) ) ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 6 ; System . out . println ( " Minimum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + min ( x , y ) ) ; System . out . println ( " Maximum ▁ of ▁ " + x + " ▁ and ▁ " + y + " ▁ is ▁ " + max ( x , y ) ) ; } }
class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int arr_size ) { int i ; for ( i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }
import java . io . * ; class GFG { public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return 1 + countSetBits ( n & ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( countSetBits ( n ) ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { System . out . println ( Integer . bitCount ( 4 ) ) ; System . out . println ( Integer . bitCount ( 15 ) ) ; } }
public class GFG { static int countSetBits ( int N ) { int count = 0 ; for ( int i = 0 ; i < 4 * 8 ; i ++ ) { if ( ( N & ( 1 << i ) ) != 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 15 ; System . out . println ( countSetBits ( N ) ) ; } }
import java . util . * ; class Count { public static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { count ++ ; n &= ( n - 1 ) ; } return count ; } public static int FlippedCount ( int a , int b ) { return countSetBits ( a ^ b ) ; } public static void main ( String [ ] args ) { int a = 10 ; int b = 20 ; System . out . print ( FlippedCount ( a , b ) ) ; } }
import java . util . * ; class GFG { static boolean powerOf2 ( int n ) { if ( n == 1 ) return true ; else if ( n % 2 != 0 n == 0 ) return false ; return powerOf2 ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 64 ; int m = 12 ; if ( powerOf2 ( n ) == true ) System . out . print ( " True " + "NEW_LINE"); else System . out . print ( " False " + "NEW_LINE"); if ( powerOf2 ( m ) == true ) System . out . print ( " True " + "NEW_LINE"); else System . out . print ( " False " + "NEW_LINE"); } }
class GFG { static int PositionRightmostSetbit ( int n ) { int position = 1 ; int m = 1 ; while ( ( n & m ) == 0 ) { m = m << 1 ; position ++ ; } return position ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( PositionRightmostSetbit ( n ) ) ; } }
public class GFG { static int INT_SIZE = 32 ; static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } public static void main ( String [ ] args ) { int num = 18 ; int pos = Right_most_setbit ( num ) ; System . out . println ( pos ) ; } }
public class GFG { static void bin ( long n ) { long i ; System . out . print ( "0" ) ; for ( i = 1 << 30 ; i > 0 ; i = i / 2 ) { if ( ( n & i ) != 0 ) { System . out . print ( "1" ) ; } else { System . out . print ( "0" ) ; } } } public static void main ( String [ ] args ) { bin ( 7 ) ; System . out . println ( ) ; bin ( 4 ) ; } }
class GFG { static void bin ( Integer n ) { if ( n > 1 ) bin ( n >> 1 ) ; System . out . printf ( " % d " , n & 1 ) ; } public static void main ( String [ ] args ) { bin ( 131 ) ; System . out . printf ( "NEW_LINE"); bin ( 3 ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int N = 8 ; static void printSolution ( int board [ ] [ ] ) { int N = board . length ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . printf ( " % 2d ▁ " , board [ i ] [ j ] ) ; System . out . printf ( "NEW_LINE"); } } static boolean isSafe ( int row , int col , int slashCode [ ] [ ] , int backslashCode [ ] [ ] , boolean rowLookup [ ] , boolean slashCodeLookup [ ] , boolean backslashCodeLookup [ ] ) { if ( slashCodeLookup [ slashCode [ row ] [ col ] ] backslashCodeLookup [ backslashCode [ row ] [ col ] ] rowLookup [ row ] ) return false ; return true ; } static boolean solveNQueensUtil ( int board [ ] [ ] , int col , int slashCode [ ] [ ] , int backslashCode [ ] [ ] , boolean rowLookup [ ] , boolean slashCodeLookup [ ] , boolean backslashCodeLookup [ ] ) { int N = board . length ; if ( col >= N ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( isSafe ( i , col , slashCode , backslashCode , rowLookup , slashCodeLookup , backslashCodeLookup ) ) { board [ i ] [ col ] = 1 ; rowLookup [ i ] = true ; slashCodeLookup [ slashCode [ i ] [ col ] ] = true ; backslashCodeLookup [ backslashCode [ i ] [ col ] ] = true ; if ( solveNQueensUtil ( board , col + 1 , slashCode , backslashCode , rowLookup , slashCodeLookup , backslashCodeLookup ) ) return true ; board [ i ] [ col ] = 0 ; rowLookup [ i ] = false ; slashCodeLookup [ slashCode [ i ] [ col ] ] = false ; backslashCodeLookup [ backslashCode [ i ] [ col ] ] = false ; } } return false ; } static boolean solveNQueens ( ) { int board [ ] [ ] = new int [ N ] [ N ] ; int slashCode [ ] [ ] = new int [ N ] [ N ] ; int backslashCode [ ] [ ] = new int [ N ] [ N ] ; boolean [ ] rowLookup = new boolean [ N ] ; boolean slashCodeLookup [ ] = new boolean [ 2 * N - 1 ] ; boolean backslashCodeLookup [ ] = new boolean [ 2 * N - 1 ] ; for ( int r = 0 ; r < N ; r ++ ) for ( int c = 0 ; c < N ; c ++ ) { slashCode [ r ] = r + c ; backslashCode [ r ] = r - c + 7 ; } if ( solveNQueensUtil ( board , 0 , slashCode , backslashCode , rowLookup , slashCodeLookup , backslashCodeLookup ) == false ) { System . out . printf ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } printSolution ( board ) ; return true ; } public static void main ( String [ ] args ) { solveNQueens ( ) ; } }
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 " ) ; } } }
import java . io . * ; class Binary { static int maxOnesIndex ( int arr [ ] , int n ) { int max_count = 0 ; int max_index = 0 ; int prev_zero = - 1 ; int prev_prev_zero = - 1 ; for ( int curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " + maxOnesIndex ( arr , n ) ) ; } }
class LargestSubArray2 { int min ( int x , int y ) { return ( x < y ) ? x : y ; } int max ( int x , int y ) { return ( x > y ) ? x : y ; } int findLength ( int arr [ ] , int n ) { int max_len = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int mn = arr [ i ] , mx = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { mn = min ( mn , arr [ j ] ) ; mx = max ( mx , arr [ j ] ) ; if ( ( mx - mn ) == j - i ) max_len = max ( max_len , mx - mn + 1 ) ; } } return max_len ; } public static void main ( String [ ] args ) { LargestSubArray2 large = new LargestSubArray2 ( ) ; int arr [ ] = { 1 , 56 , 58 , 57 , 90 , 92 , 94 , 93 , 91 , 45 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ the ▁ longest ▁ contiguous ▁ subarray ▁ is ▁ " + large . findLength ( arr , n ) ) ; } }
class GFG { static void printArr ( int [ ] arr , int k ) { for ( int i = 0 ; i < k ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } static void printSeqUtil ( int n , int k , int len , int [ ] arr ) { if ( len == k ) { printArr ( arr , k ) ; return ; } int i = ( len == 0 ) ? 1 : arr [ len - 1 ] + 1 ; len ++ ; while ( i < = n ) { arr [ len - 1 ] = i ; printSeqUtil ( n , k , len , arr ) ; i ++ ; } len -- ; } static void printSeq ( int n , int k ) { int [ ] arr = new int [ k ] ; int len = 0 ; printSeqUtil ( n , k , len , arr ) ; } static public void main ( String [ ] args ) { int k = 3 , n = 7 ; printSeq ( n , k ) ; } }
import java . io . * ; class SubSequence { static boolean isSubSequence ( String str1 , String str2 , int m , int n ) { if ( m == 0 ) return true ; if ( n == 0 ) return false ; if ( str1 . charAt ( m - 1 ) == str2 . charAt ( n - 1 ) ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ; return isSubSequence ( str1 , str2 , m , n - 1 ) ; } public static void main ( String [ ] args ) { String str1 = " gksrek " ; String str2 = " geeksforgeeks " ; int m = str1 . length ( ) ; int n = str2 . length ( ) ; boolean res = isSubSequence ( str1 , str2 , m , n ) ; if ( res ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void segregate0and1 ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count ++ ; } for ( int i = 0 ; i < count ; i ++ ) arr [ i ] = 0 ; for ( int i = count ; i < n ; i ++ ) arr [ i ] = 1 ; } static void print ( int arr [ ] , int n ) { System . out . print ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; segregate0and1 ( arr , n ) ; print ( arr , n ) ; } }
import java . util . * ; class GFG { static void segregate0and1 ( int arr [ ] ) { int type0 = 0 ; int type1 = arr . length - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { arr [ type1 ] = arr [ type1 ] + arr [ type0 ] ; arr [ type0 ] = arr [ type1 ] - arr [ type0 ] ; arr [ type1 ] = arr [ type1 ] - arr [ type0 ] ; type1 -- ; } else { type0 ++ ; } } } public static void main ( String [ ] args ) { int [ ] array = { 0 , 1 , 0 , 1 , 1 , 1 } ; segregate0and1 ( array ) ; for ( int a : array ) { System . out . print ( a + " ▁ " ) ; } } }
class Main { public static void find3Numbers ( int [ ] nums ) { if ( nums . length < 3 ) { System . out . print ( " No ▁ such ▁ triplet ▁ found " ) ; return ; } int seq = 1 ; int min_num = nums [ 0 ] ; int max_seq = Integer . MIN_VALUE ; int store_min = min_num ; for ( int i = 1 ; i < nums . length ; i ++ ) { if ( nums [ i ] == min_num ) continue ; else if ( nums [ i ] < min_num ) { min_num = nums [ i ] ; continue ; } else if ( nums [ i ] < max_seq ) { max_seq = nums [ i ] ; store_min = min_num ; } else if ( nums [ i ] > max_seq ) { seq ++ ; if ( seq == 3 ) { System . out . println ( " Triplet : ▁ " + store_min + " , ▁ " + max_seq + " , ▁ " + nums [ i ] ) ; return ; } max_seq = nums [ i ] ; } } System . out . print ( " No ▁ such ▁ triplet ▁ found " ) ; } public static void main ( String [ ] args ) { int [ ] nums = { 1 , 2 , - 1 , 7 , 5 } ; find3Numbers ( nums ) ; } }
import java . io . * ; class GFG { static int maxSubarrayProduct ( int arr [ ] ) { int result = arr [ 0 ] ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { int mul = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { result = Math . max ( result , mul ) ; mul *= arr [ j ] ; } result = Math . max ( result , mul ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 2 , - 3 , 0 , 7 , - 8 , - 2 } ; System . out . println ( " Maximum ▁ Sub ▁ array ▁ product ▁ is ▁ " + maxSubarrayProduct ( arr ) ) ; } }
import java . util . Arrays ; class GFG { static int GetCeilIndex ( int arr [ ] , int T [ ] , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( arr [ T [ m ] ] >= key ) r = m ; else l = m ; } return r ; } static int LongestIncreasingSubsequence ( int arr [ ] , int n ) { int tailIndices [ ] = new int [ n ] ; Arrays . fill ( tailIndices , 0 ) ; int prevIndices [ ] = new int [ n ] ; Arrays . fill ( prevIndices , - 1 ) ; int len = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ tailIndices [ 0 ] ] ) tailIndices [ 0 ] = i ; else if ( arr [ i ] > arr [ tailIndices [ len - 1 ] ] ) { prevIndices [ i ] = tailIndices [ len - 1 ] ; tailIndices [ len ++ ] = i ; } else { int pos = GetCeilIndex ( arr , tailIndices , - 1 , len - 1 , arr [ i ] ) ; prevIndices [ i ] = tailIndices [ pos - 1 ] ; tailIndices [ pos ] = i ; } } System . out . println ( " LIS ▁ of ▁ given ▁ input " ) ; for ( int i = tailIndices [ len - 1 ] ; i >= 0 ; i = prevIndices [ i ] ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 } ; int n = arr . length ; System . out . print ( "LIS sizeNEW_LINE" + LongestIncreasingSubsequence(arr, n)); } }
import java . io . * ; import java . util . Arrays ; class MaxSum { static int maxSum ( int arr [ ] , int n ) { int sum = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { sum -= ( 2 * arr [ i ] ) ; sum += ( 2 * arr [ n - i - 1 ] ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 8 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { public static void threeWayPartition ( int [ ] arr , int lowVal , int highVal ) { int n = arr . length ; int start = 0 , end = n - 1 ; for ( int i = 0 ; i <= end ; ) { if ( arr [ i ] < lowVal ) { int temp = arr [ start ] ; arr [ start ] = arr [ i ] ; arr [ i ] = temp ; start ++ ; i ++ ; } else if ( arr [ i ] > highVal ) { int temp = arr [ end ] ; arr [ end ] = arr [ i ] ; arr [ i ] = temp ; end -- ; } else i ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 14 , 5 , 20 , 4 , 2 , 54 , 20 , 87 , 98 , 3 , 1 , 32 } ; threeWayPartition ( arr , 10 , 20 ) ; System . out . println ( " Modified ▁ array ▁ " ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
class GenerateArrays { void generateUtil ( int A [ ] , int B [ ] , int C [ ] , int i , int j , int m , int n , int len , boolean flag ) { if ( flag ) { if ( len != 0 ) printArr ( C , len + 1 ) ; for ( int k = i ; k < m ; k ++ ) { if ( len == 0 ) { C [ len ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len , ! flag ) ; } else if ( A [ k ] > C [ len ] ) { C [ len + 1 ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , ! flag ) ; } } } else { for ( int l = j ; l < n ; l ++ ) { if ( B [ l ] > C [ len ] ) { C [ len + 1 ] = B [ l ] ; generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , ! flag ) ; } } } } void generate ( int A [ ] , int B [ ] , int m , int n ) { int C [ ] = new int [ m + n ] ; generateUtil ( A , B , C , 0 , 0 , m , n , 0 , true ) ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { GenerateArrays generate = new GenerateArrays ( ) ; int A [ ] = { 10 , 15 , 25 } ; int B [ ] = { 5 , 20 , 30 } ; int n = A . length ; int m = B . length ; generate . generate ( A , B , n , m ) ; } }
class GFG { static void updateindex ( int index [ ] , int a , int ai , int b , int bi ) { index [ a ] = ai ; index [ b ] = bi ; } static int minSwapsUtil ( int arr [ ] , int pairs [ ] , int index [ ] , int i , int n ) { if ( i > n ) return 0 ; if ( pairs [ arr [ i ] ] == arr [ i + 1 ] ) return minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; int one = arr [ i + 1 ] ; int indextwo = i + 1 ; int indexone = index [ pairs [ arr [ i ] ] ] ; int two = arr [ index [ pairs [ arr [ i ] ] ] ] ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; int a = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i + 1 ] = arr [ i + 1 ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i + 1 ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; one = arr [ i ] ; indexone = index [ pairs [ arr [ i + 1 ] ] ] ; two = arr [ index [ pairs [ arr [ i + 1 ] ] ] ] ; indextwo = i ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indexone , two , indextwo ) ; int b = minSwapsUtil ( arr , pairs , index , i + 2 , n ) ; arr [ i ] = arr [ i ] ^ arr [ indexone ] ^ ( arr [ indexone ] = arr [ i ] ) ; updateindex ( index , one , indextwo , two , indexone ) ; return 1 + Math . min ( a , b ) ; } static int minSwaps ( int n , int pairs [ ] , int arr [ ] ) { int index [ ] = new int [ 2 * n + 1 ] ; for ( int i = 1 ; i <= 2 * n ; i ++ ) index [ arr [ i ] ] = i ; return minSwapsUtil ( arr , pairs , index , 1 , 2 * n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 3 , 5 , 6 , 4 , 1 , 2 } ; int pairs [ ] = { 0 , 3 , 6 , 1 , 5 , 4 , 2 } ; int m = pairs . length ; int n = m / 2 ; System . out . print ( " Min ▁ swaps ▁ required ▁ is ▁ " + minSwaps ( n , pairs , arr ) ) ; } }
public class GFG { static void replace_elements ( int arr [ ] , int n ) { int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arr [ pos ++ ] = arr [ i ] ; while ( pos > 1 && arr [ pos - 2 ] == arr [ pos - 1 ] ) { pos -- ; arr [ pos - 1 ] ++ ; } } for ( int i = 0 ; i < pos ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 6 , 4 , 3 , 4 , 3 , 3 , 5 } ; int n = arr . length ; replace_elements ( arr , n ) ; } }
import java . io . * ; class GFG { static void arrangeString ( String str , int x , int y ) { int count_0 = 0 ; int count_1 = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) count_0 ++ ; else count_1 ++ ; } while ( count_0 > 0 count_1 > 0 ) { for ( int j = 0 ; j < x && count_0 > 0 ; j ++ ) { if ( count_0 > 0 ) { System . out . print ( "0" ) ; count_0 -- ; } } for ( int j = 0 ; j < y && count_1 > 0 ; j ++ ) { if ( count_1 > 0 ) { System . out . print ( "1" ) ; count_1 -- ; } } } } public static void main ( String [ ] args ) { String str = "01101101101101101000000" ; int x = 1 ; int y = 2 ; arrangeString ( str , x , y ) ; } }
import java . io . * ; import java . util . HashMap ; import java . util . Map ; class GFG { static void distinctAdjacentElement ( int a [ ] , int n ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( m . containsKey ( a [ i ] ) ) { int x = m . get ( a [ i ] ) + 1 ; m . put ( a [ i ] , x ) ; } else { m . put ( a [ i ] , 1 ) ; } } int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( mx < m . get ( a [ i ] ) ) mx = m . get ( a [ i ] ) ; if ( mx > ( n + 1 ) / 2 ) System . out . println ( " NO " ) ; else System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 7 , 7 , 7 , 7 } ; int n = 4 ; distinctAdjacentElement ( a , n ) ; } }
import java . io . * ; class GFG { public static void rearrange ( int [ ] arr ) { if ( arr == null arr . length % 2 == 1 ) return ; int currIdx = ( arr . length - 1 ) / 2 ; while ( currIdx > 0 ) { int count = currIdx , swapIdx = currIdx ; while ( count -- > 0 ) { int temp = arr [ swapIdx + 1 ] ; arr [ swapIdx + 1 ] = arr [ swapIdx ] ; arr [ swapIdx ] = temp ; swapIdx ++ ; } currIdx -- ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 2 , 4 , 6 } ; rearrange ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) System . out . print ( " ▁ " + arr [ i ] ) ; } }
import java . io . * ; class GFG { static int maxDiff ( int arr [ ] , int n ) { int maxDiff = - 1 ; int maxRight = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { int diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; int n = arr . length ; System . out . println ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
class GFG { static int maxDiff ( int arr [ ] , int n ) { int diff = arr [ 1 ] - arr [ 0 ] ; int curr_sum = diff ; int max_sum = curr_sum ; for ( int i = 1 ; i < n - 1 ; i ++ ) { diff = arr [ i + 1 ] - arr [ i ] ; if ( curr_sum > 0 ) curr_sum += diff ; else curr_sum = diff ; if ( curr_sum > max_sum ) max_sum = curr_sum ; } return max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 80 , 2 , 6 , 3 , 100 } ; int n = arr . length ; System . out . print ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxIndexDiff ( ArrayList < Integer > arr , int n ) { Map < Integer , ArrayList < Integer > > hashmap = new HashMap < Integer , ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashmap . containsKey ( arr . get ( i ) ) ) { hashmap . get ( arr . get ( i ) ) . add ( i ) ; } else { hashmap . put ( arr . get ( i ) , new ArrayList < Integer > ( ) ) ; hashmap . get ( arr . get ( i ) ) . add ( i ) ; } } Collections . sort ( arr ) ; int maxDiff = Integer . MIN_VALUE ; int temp = n ; for ( int i = 0 ; i < n ; i ++ ) { if ( temp > hashmap . get ( arr . get ( i ) ) . get ( 0 ) ) { temp = hashmap . get ( arr . get ( i ) ) . get ( 0 ) ; } maxDiff = Math . max ( maxDiff , hashmap . get ( arr . get ( i ) ) . get ( hashmap . get ( arr . get ( i ) ) . size ( ) - 1 ) - temp ) ; } return maxDiff ; } public static void main ( String [ ] args ) { int n = 9 ; ArrayList < Integer > arr = new ArrayList < Integer > ( Arrays . asList ( 34 , 8 , 10 , 3 , 2 , 80 , 30 , 33 , 1 ) ) ; int ans = maxIndexDiff ( arr , n ) ; System . out . println ( " The ▁ maxIndexDiff ▁ is ▁ : ▁ " + ans ) ; } }
class CountSmaller { void constructLowerArray ( int arr [ ] , int countSmaller [ ] , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { CountSmaller small = new CountSmaller ( ) ; int arr [ ] = { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = arr . length ; int low [ ] = new int [ n ] ; small . constructLowerArray ( arr , low , n ) ; small . printArray ( low , n ) ; } }
import java . util . * ; class Main { static int segregate ( int arr [ ] , int size ) { int j = 0 , i ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ i ] <= 0 ) { int temp ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; j ++ ; } } return j ; } static int findMissingPositive ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) { int x = Math . abs ( arr [ i ] ) ; if ( x - 1 < size && arr [ x - 1 ] > 0 ) arr [ x - 1 ] = - arr [ x - 1 ] ; } for ( i = 0 ; i < size ; i ++ ) if ( arr [ i ] > 0 ) return i + 1 ; return size + 1 ; } static int findMissing ( int arr [ ] , int size ) { int shift = segregate ( arr , size ) ; int arr2 [ ] = new int [ size - shift ] ; int j = 0 ; for ( int i = shift ; i < size ; i ++ ) { arr2 [ j ] = arr [ i ] ; j ++ ; } return findMissingPositive ( arr2 , j ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 10 , 2 , - 10 , - 20 } ; int arr_size = arr . length ; int missing = findMissing ( arr , arr_size ) ; System . out . println ( " The ▁ smallest ▁ positive ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }
import java . util . Arrays ; class GFG { static int firstMissingPositive ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( arr [ i ] >= 1 && arr [ i ] <= n && arr [ i ] != arr [ arr [ i ] - 1 ] ) { int temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != i + 1 ) return ( i + 1 ) ; return ( n + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . length ; int ans = firstMissingPositive ( arr , n ) ; System . out . println ( ans ) ; } }
import java . io . * ; class MaxRepeating { static int maxRepeating ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) arr [ ( arr [ i ] % k ) ] += k ; int max = arr [ 0 ] , result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; result = i ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 5 , 3 , 4 , 1 , 7 } ; int n = arr . length ; int k = 8 ; System . out . println ( " Maximum ▁ repeating ▁ element ▁ is : ▁ " + maxRepeating ( arr , n , k ) ) ; } }
class MaximumSumPath { int max ( int x , int y ) { return ( x > y ) ? x : y ; } int maxPathSum ( int ar1 [ ] , int ar2 [ ] , int m , int n ) { int i = 0 , j = 0 ; int result = 0 , sum1 = 0 , sum2 = 0 ; while ( i < m && j < n ) { if ( ar1 [ i ] < ar2 [ j ] ) sum1 += ar1 [ i ++ ] ; else if ( ar1 [ i ] > ar2 [ j ] ) sum2 += ar2 [ j ++ ] ; else { result += max ( sum1 , sum2 ) + ar1 [ i ] ; sum1 = 0 ; sum2 = 0 ; i ++ ; j ++ ; } } while ( i < m ) sum1 += ar1 [ i ++ ] ; while ( j < n ) sum2 += ar2 [ j ++ ] ; result += max ( sum1 , sum2 ) ; return result ; } public static void main ( String [ ] args ) { MaximumSumPath sumpath = new MaximumSumPath ( ) ; int ar1 [ ] = { 2 , 3 , 7 , 10 , 12 , 15 , 30 , 34 } ; int ar2 [ ] = { 1 , 5 , 7 , 8 , 10 , 15 , 16 , 19 } ; int m = ar1 . length ; int n = ar2 . length ; System . out . println ( " Maximum ▁ sum ▁ path ▁ is ▁ : " + sumpath . maxPathSum ( ar1 , ar2 , m , n ) ) ; } }
import java . io . * ; class GFG { static void smallestGreater ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int diff = Integer . MAX_VALUE ; int closest = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] && arr [ j ] - arr [ i ] < diff ) { diff = arr [ j ] - arr [ i ] ; closest = j ; } } if ( closest == - 1 ) System . out . print ( " _ ▁ " ) ; else System . out . print ( arr [ closest ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . length ; smallestGreater ( ar , n ) ; } }
import java . util . * ; class GFG { static void smallestGreater ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; Vector < Integer > newAr = new Vector < > ( ) ; for ( int p : s ) { newAr . add ( p ) ; } for ( int i = 0 ; i < n ; i ++ ) { int temp = lowerBound ( newAr , 0 , newAr . size ( ) , arr [ i ] ) ; if ( temp < n ) System . out . print ( newAr . get ( temp ) + " ▁ " ) ; else System . out . print ( " _ ▁ " ) ; } } static int lowerBound ( Vector < Integer > vec , int low , int high , int element ) { int [ ] array = new int [ vec . size ( ) ] ; int k = 0 ; for ( Integer val : vec ) { array [ k ] = val ; k ++ ; } while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > array [ middle ] ) { low = middle + 1 ; } else { high = middle ; } } return low + 1 ; } public static void main ( String [ ] args ) { int ar [ ] = { 6 , 3 , 9 , 8 , 10 , 2 , 1 , 15 , 7 } ; int n = ar . length ; smallestGreater ( ar , n ) ; } }
public class GFG { static final int d = 256 ; static final int q = 103 ; static void checkPalindromes ( String str ) { int N = str . length ( ) ; System . out . println ( str . charAt ( 0 ) + " ▁ Yes " ) ; if ( N == 1 ) return ; int firstr = str . charAt ( 0 ) % q ; int second = str . charAt ( 1 ) % q ; int h = 1 , i , j ; for ( i = 1 ; i < N ; i ++ ) { if ( firstr == second ) { for ( j = 0 ; j < i / 2 ; j ++ ) { if ( str . charAt ( j ) != str . charAt ( i - j ) ) break ; } System . out . println ( ( j == i / 2 ) ? str . charAt ( i ) + " ▁ Yes " : str . charAt ( i ) + " ▁ No " ) ; } else System . out . println ( str . charAt ( i ) + " ▁ No " ) ; if ( i != N - 1 ) { if ( i % 2 == 0 ) { h = ( h * d ) % q ; firstr = ( firstr + h * str . charAt ( i / 2 ) ) % q ; second = ( second * d + str . charAt ( i + 1 ) ) % q ; } else { second = ( d * ( second + q - str . charAt ( ( i + 1 ) / 2 ) * h ) % q + str . charAt ( i + 1 ) ) % q ; } } } } public static void main ( String args [ ] ) { String txt = " aabaacaabaa " ; checkPalindromes ( txt ) ; } }
class Test { static int arr [ ] = new int [ ] { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; static void findZeroes ( int m ) { int wL = 0 , wR = 0 ; int bestL = 0 , bestWindow = 0 ; int zeroCount = 0 ; while ( wR < arr . length ) { if ( zeroCount <= m ) { if ( arr [ wR ] == 0 ) zeroCount ++ ; wR ++ ; } if ( zeroCount > m ) { if ( arr [ wL ] == 0 ) zeroCount -- ; wL ++ ; } if ( ( wR - wL > bestWindow ) && ( zeroCount <= m ) ) { bestWindow = wR - wL ; bestL = wL ; } } for ( int i = 0 ; i < bestWindow ; i ++ ) { if ( arr [ bestL + i ] == 0 ) System . out . print ( bestL + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int m = 2 ; System . out . println ( " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ) ; findZeroes ( m ) ; } }
class Test { static int arr [ ] = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ j - 1 ] ) cnt ++ ; else break ; } } return cnt ; } public static void main ( String [ ] args ) { System . out . println ( " Count ▁ of ▁ strictly ▁ increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . length ) ) ; } }
class Test { static int arr [ ] = new int [ ] { 1 , 2 , 2 , 4 } ; static int countIncreasing ( int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] > arr [ i ] ) len ++ ; else { cnt += ( ( ( len - 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } public static void main ( String [ ] args ) { System . out . println ( " Count ▁ of ▁ strictly ▁ increasing ▁ subarrays ▁ is ▁ " + countIncreasing ( arr . length ) ) ; } }
import java . util . Arrays ; public class GFG { static long arraySum ( int arr [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum ; } static long maxDiff ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; long arraysum = arraySum ( arr , n ) ; long diff1 = Math . abs ( arraysum - 2 * arraySum ( arr , k ) ) ; int end = arr . length - 1 ; int start = 0 ; while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } long diff2 = Math . abs ( arraysum - 2 * arraySum ( arr , k ) ) ; return ( Math . max ( diff1 , diff2 ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 7 , 4 , 8 , - 1 , 5 , 2 , 1 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n , k ) ) ; } }
import java . util . Arrays ; class GFG { static int minNumber ( int a [ ] , int n , int x ) { Arrays . sort ( a ) ; int k ; for ( k = 0 ; a [ ( n ) / 2 ] != x ; k ++ ) { a [ n ++ ] = x ; Arrays . sort ( a ) ; } return k ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = 3 ; System . out . println ( minNumber ( a , n - 1 , x ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static int minNumber ( int a [ ] , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = a . length ; System . out . println ( minNumber ( a , n , x ) ) ; } }
class SubArray { static void preprocess ( int arr [ ] , int N , int left [ ] , int right [ ] ) { left [ 0 ] = 0 ; int lastIncr = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) lastIncr = i ; left [ i ] = lastIncr ; } right [ N - 1 ] = N - 1 ; int firstDecr = N - 1 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) firstDecr = i ; right [ i ] = firstDecr ; } } static boolean isSubarrayMountainForm ( int arr [ ] , int left [ ] , int right [ ] , int L , int R ) { return ( right [ L ] >= left [ R ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 4 , 4 , 6 , 3 , 2 } ; int N = arr . length ; int left [ ] = new int [ N ] ; int right [ ] = new int [ N ] ; preprocess ( arr , N , left , right ) ; int L = 0 ; int R = 2 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) System . out . println ( " Subarray ▁ is ▁ in ▁ mountain ▁ form " ) ; else System . out . println ( " Subarray ▁ is ▁ not ▁ in ▁ mountain ▁ form " ) ; L = 1 ; R = 3 ; if ( isSubarrayMountainForm ( arr , left , right , L , R ) ) System . out . println ( " Subarray ▁ is ▁ in ▁ mountain ▁ form " ) ; else System . out . println ( " Subarray ▁ is ▁ not ▁ in ▁ mountain ▁ form " ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static void preCompute ( int arr [ ] , int n , int pre [ ] ) { pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } public static int rangeSum ( int i , int j , int pre [ ] ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int pre [ ] = new int [ n ] ; preCompute ( arr , n , pre ) ; System . out . println ( rangeSum ( 1 , 3 , pre ) ) ; System . out . println ( rangeSum ( 2 , 4 , pre ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000 ; static void sieveOfEratosthenes ( boolean isPrime [ ] ) { isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) isPrime [ i ] = false ; } } } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryPrimesUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryPrimesUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryPrimesUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int arr [ ] , int [ ] st , int n , int i , int new_val , boolean isPrime [ ] ) { if ( i < 0 i > n - 1 ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } int diff = 0 ; int oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPrime [ oldValue ] && isPrime [ new_val ] ) return ; if ( ( ! isPrime [ oldValue ] ) && ( ! isPrime [ new_val ] ) ) return ; if ( isPrime [ oldValue ] && ! isPrime [ new_val ] ) { diff = - 1 ; } if ( ! isPrime [ oldValue ] && isPrime [ new_val ] ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void queryPrimes ( int [ ] st , int n , int qs , int qe ) { int primesInRange = queryPrimesUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; System . out . println ( " Number ▁ of ▁ Primes ▁ in ▁ subarray ▁ from ▁ " + qs + " ▁ to ▁ " + qe + " ▁ = ▁ " + primesInRange ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si , boolean isPrime [ ] ) { if ( ss == se ) { if ( isPrime [ arr [ ss ] ] ) st [ si ] = 1 ; else st [ si ] = 0 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 , isPrime ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 , isPrime ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n , boolean isPrime [ ] ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int [ ] st = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 , isPrime ) ; return st ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 7 , 9 } ; int n = arr . length ; boolean [ ] isPrime = new boolean [ MAX + 1 ] ; Arrays . fill ( isPrime , Boolean . TRUE ) ; sieveOfEratosthenes ( isPrime ) ; int [ ] st = constructST ( arr , n , isPrime ) ; int start = 0 ; int end = 4 ; queryPrimes ( st , n , start , end ) ; int i = 3 ; int x = 6 ; updateValue ( arr , st , n , i , x , isPrime ) ; start = 0 ; end = 4 ; queryPrimes ( st , n , start , end ) ; } }
import java . io . * ; class GFG { static void checkEVENodd ( int arr [ ] , int n , int l , int r ) { if ( arr [ r ] == 1 ) System . out . println ( " odd " ) ; else System . out . println ( " even " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 , 1 } ; int n = arr . length ; checkEVENodd ( arr , n , 1 , 3 ) ; } }
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static Scanner sc = new Scanner ( System . in ) ; static int twos [ ] = new int [ 1000 ] ; static int fives [ ] = new int [ 1000 ] ; static int sum = 0 ; static int returnTwos ( int val ) { int count = 0 ; while ( val % 2 == 0 && val != 0 ) { val = val / 2 ; count ++ ; } return count ; } static int returnFives ( int val ) { int count = 0 ; while ( val % 5 == 0 && val != 0 ) { val = val / 5 ; count ++ ; } return count ; } static void solve_queries ( int arr [ ] , int n ) { int type = sc . nextInt ( ) ; if ( type == 1 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int x = sc . nextInt ( ) ; int temp = returnTwos ( x ) ; int temp1 = returnFives ( x ) ; for ( int i = ql - 1 ; i < qr ; i ++ ) { arr [ i ] = arr [ i ] * x ; twos [ i ] += temp ; fives [ i ] += temp1 ; } } if ( type == 2 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int y = sc . nextInt ( ) ; int temp = returnTwos ( y ) ; int temp1 = returnFives ( y ) ; for ( int i = ql - 1 ; i < qr ; i ++ ) { arr [ i ] = ( i - ql + 2 ) * y ; twos [ i ] = returnTwos ( i - ql + 2 ) + temp ; fives [ i ] = returnFives ( i - ql + 2 ) + temp1 ; } } if ( type == 3 ) { int ql = sc . nextInt ( ) ; int qr = sc . nextInt ( ) ; int sumtwos = 0 ; int sumfives = 0 ; for ( int i = ql - 1 ; i < qr ; i ++ ) { sumtwos += twos [ i ] ; sumfives += fives [ i ] ; } sum += Math . min ( sumtwos , sumfives ) ; } } public static void main ( String [ ] args ) { int n = sc . nextInt ( ) ; int m = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = sc . nextInt ( ) ; twos [ i ] = returnTwos ( arr [ i ] ) ; fives [ i ] = returnFives ( arr [ i ] ) ; } while ( m -- != 0 ) { solve_queries ( arr , n ) ; } System . out . println ( sum ) ; } }
public class Main { static int findMean ( int arr [ ] , int l , int r ) { int sum = 0 , count = 0 ; for ( int i = l ; i <= r ; i ++ ) { sum += arr [ i ] ; count ++ ; } int mean = ( int ) Math . floor ( sum / count ) ; return mean ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; System . out . println ( findMean ( arr , 0 , 2 ) ) ; System . out . println ( findMean ( arr , 1 , 3 ) ) ; System . out . println ( findMean ( arr , 0 , 4 ) ) ; } }
public class Main { public static final int MAX = 1000005 ; static int prefixSum [ ] = new int [ MAX ] ; static void calculatePrefixSum ( int arr [ ] , int n ) { prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } static int findMean ( int l , int r ) { if ( l == 0 ) return ( int ) Math . floor ( prefixSum [ r ] / ( r + 1 ) ) ; return ( int ) Math . floor ( ( prefixSum [ r ] - prefixSum [ l - 1 ] ) / ( r - l + 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; calculatePrefixSum ( arr , n ) ; System . out . println ( findMean ( 1 , 2 ) ) ; System . out . println ( findMean ( 1 , 3 ) ) ; System . out . println ( findMean ( 1 , 4 ) ) ; } }
import java . util . Arrays ; class GFG { static void updateQuery ( int arr [ ] , int n , int q , int l , int r , int k ) { if ( q == 0 ) { arr [ l - 1 ] += k ; arr [ r ] += - k ; } else { arr [ l - 1 ] += - k ; arr [ r ] += k ; } return ; } static void generateArray ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; ++ i ) arr [ i ] += arr [ i - 1 ] ; } public static void main ( String arg [ ] ) { int n = 5 ; int arr [ ] = new int [ n + 1 ] ; Arrays . fill ( arr , 0 ) ; int q = 0 , l = 1 , r = 3 , k = 2 ; updateQuery ( arr , n , q , l , r , k ) ; q = 1 ; l = 3 ; r = 5 ; k = 3 ; updateQuery ( arr , n , q , l , r , k ) ; q = 0 ; l = 2 ; r = 5 ; k = 1 ; updateQuery ( arr , n , q , l , r , k ) ; generateArray ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . io . * ; class GFG { static int calculateProduct ( int [ ] A , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans = 1 ; for ( int i = L ; i <= R ; i ++ ) { ans = ans * A [ i ] ; ans = ans % P ; } return ans ; } static public void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 229 ; int L = 2 , R = 5 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; System . out . println ( calculateProduct ( A , L , R , P ) ) ; } }
class GFG { static int MAX = 100 ; int pre_product [ ] = new int [ MAX ] ; int inverse_product [ ] = new int [ MAX ] ; int modInverse ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } void calculate_Pre_Product ( int A [ ] , int N , int P ) { pre_product [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pre_product [ i ] = pre_product [ i - 1 ] * A [ i ] ; pre_product [ i ] = pre_product [ i ] % P ; } } void calculate_inverse_product ( int A [ ] , int N , int P ) { inverse_product [ 0 ] = modInverse ( pre_product [ 0 ] , P ) ; for ( int i = 1 ; i < N ; i ++ ) inverse_product [ i ] = modInverse ( pre_product [ i ] , P ) ; } int calculateProduct ( int A [ ] , int L , int R , int P ) { L = L - 1 ; R = R - 1 ; int ans ; if ( L == 0 ) ans = pre_product [ R ] ; else ans = pre_product [ R ] * inverse_product [ L - 1 ] ; return ans ; } public static void main ( String [ ] s ) { GFG d = new GFG ( ) ; int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int P = 113 ; d . calculate_Pre_Product ( A , A . length , P ) ; d . calculate_inverse_product ( A , A . length , P ) ; int L = 2 , R = 5 ; System . out . println ( d . calculateProduct ( A , L , R , P ) ) ; L = 1 ; R = 3 ; System . out . println ( d . calculateProduct ( A , L , R , P ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 10000 ; static int prefix [ ] = new int [ MAX + 1 ] ; static void buildPrefix ( ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] ) prefix [ p ] ++ ; } } static int query ( int L , int R ) { return prefix [ R ] - prefix [ L - 1 ] ; } public static void main ( String [ ] args ) { buildPrefix ( ) ; int L = 5 , R = 10 ; System . out . println ( query ( L , R ) ) ; L = 1 ; R = 10 ; System . out . println ( query ( L , R ) ) ; } }
class GFG { static void command ( boolean arr [ ] , int a , int b ) { arr [ a ] ^= true ; arr [ b + 1 ] ^= true ; } static void process ( boolean arr [ ] , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { arr [ k ] ^= arr [ k - 1 ] ; } } static void result ( boolean arr [ ] , int n ) { for ( int k = 1 ; k <= n ; k ++ ) { if ( arr [ k ] == true ) System . out . print ( "1" + " ▁ " ) ; else System . out . print ( "0" + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 , m = 3 ; boolean arr [ ] = new boolean [ n + 2 ] ; command ( arr , 1 , 5 ) ; command ( arr , 2 , 5 ) ; command ( arr , 3 , 5 ) ; process ( arr , n ) ; result ( arr , n ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static class Interval { int start ; int end ; public Interval ( int start , int end ) { super ( ) ; this . start = start ; this . end = end ; } } ; static boolean isIntersect ( Interval arr [ ] , int n ) { Arrays . sort ( arr , ( i1 , i2 ) -> { return i1 . start - i2 . start ; } ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i - 1 ] . end > arr [ i ] . start ) return true ; return false ; } public static void main ( String [ ] args ) { Interval arr1 [ ] = { new Interval ( 1 , 3 ) , new Interval ( 7 , 9 ) , new Interval ( 4 , 6 ) , new Interval ( 10 , 13 ) } ; int n1 = arr1 . length ; if ( isIntersect ( arr1 , n1 ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); Interval arr2 [ ] = { new Interval ( 6 , 8 ) , new Interval ( 1 , 3 ) , new Interval ( 2 , 4 ) , new Interval ( 4 , 7 ) } ; int n2 = arr2 . length ; if ( isIntersect ( arr2 , n2 ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
class GFG { static class Interval { int start ; int end ; public Interval ( int start , int end ) { super ( ) ; this . start = start ; this . end = end ; } } ; static boolean isIntersect ( Interval arr [ ] , int n ) { int max_ele = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_ele < arr [ i ] . end ) max_ele = arr [ i ] . end ; } int [ ] aux = new int [ max_ele + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] . start ; int y = arr [ i ] . end ; aux [ x ] ++ ; aux [ y ] -- ; } for ( int i = 1 ; i <= max_ele ; i ++ ) { aux [ i ] += aux [ i - 1 ] ; if ( aux [ i ] > 1 ) return true ; } return false ; } public static void main ( String [ ] args ) { Interval arr1 [ ] = { new Interval ( 1 , 3 ) , new Interval ( 7 , 9 ) , new Interval ( 4 , 6 ) , new Interval ( 10 , 13 ) } ; int n1 = arr1 . length ; if ( isIntersect ( arr1 , n1 ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); Interval arr2 [ ] = { new Interval ( 6 , 8 ) , new Interval ( 1 , 3 ) , new Interval ( 2 , 4 ) , new Interval ( 4 , 7 ) } ; int n2 = arr2 . length ; if ( isIntersect ( arr2 , n2 ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
class GFG { static class query { int start , end ; query ( int start , int end ) { this . start = start ; this . end = end ; } } public static void incrementByD ( int [ ] arr , query [ ] q_arr , int n , int m , int d ) { int [ ] sum = new int [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { sum [ q_arr [ i ] . start ] += d ; if ( ( q_arr [ i ] . end + 1 ) < n ) sum [ q_arr [ i ] . end + 1 ] -= d ; } arr [ 0 ] += sum [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum [ i ] += sum [ i - 1 ] ; arr [ i ] += sum [ i ] ; } } public static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 4 , 8 , 6 , 1 } ; query [ ] q_arr = new query [ 5 ] ; q_arr [ 0 ] = new query ( 0 , 3 ) ; q_arr [ 1 ] = new query ( 4 , 5 ) ; q_arr [ 2 ] = new query ( 1 , 4 ) ; q_arr [ 3 ] = new query ( 0 , 1 ) ; q_arr [ 4 ] = new query ( 2 , 5 ) ; int n = arr . length ; int m = q_arr . length ; int d = 2 ; System . out . println ( " Original ▁ Array : " ) ; printArray ( arr , n ) ; incrementByD ( arr , q_arr , n , m , d ) ; System . out . println ( " Modified Array : "); printArray ( arr , n ) ; } }
import java . io . * ; class GFG { static void prefixXOR ( int arr [ ] , int preXOR [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( arr [ i ] % 2 != 1 ) arr [ i ] /= 2 ; preXOR [ i ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) preXOR [ i ] = preXOR [ i - 1 ] ^ preXOR [ i ] ; } static int query ( int preXOR [ ] , int l , int r ) { if ( l == 0 ) return preXOR [ r ] ; else return preXOR [ r ] ^ preXOR [ l - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; int preXOR [ ] = new int [ n ] ; prefixXOR ( arr , preXOR , n ) ; System . out . println ( query ( preXOR , 0 , 2 ) ) ; System . out . println ( query ( preXOR , 1 , 2 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000001 ; static class Query { int l , r , idx ; } static void update ( int idx , int val , int bit [ ] , int n ) { for ( ; idx <= n ; idx += idx & - idx ) bit [ idx ] += val ; } static int query ( int idx , int bit [ ] , int n ) { int sum = 0 ; for ( ; idx > 0 ; idx -= idx & - idx ) sum += bit [ idx ] ; return sum ; } static void answeringQueries ( int [ ] arr , int n , Query [ ] queries , int q ) { int [ ] bit = new int [ n + 1 ] ; Arrays . fill ( bit , 0 ) ; int [ ] last_visit = new int [ MAX ] ; Arrays . fill ( last_visit , - 1 ) ; int [ ] ans = new int [ q ] ; int query_counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last_visit [ arr [ i ] ] != - 1 ) update ( last_visit [ arr [ i ] ] + 1 , - 1 , bit , n ) ; last_visit [ arr [ i ] ] = i ; update ( i + 1 , 1 , bit , n ) ; while ( query_counter < q && queries [ query_counter ] . r == i ) { ans [ queries [ query_counter ] . idx ] = query ( queries [ query_counter ] . r + 1 , bit , n ) - query ( queries [ query_counter ] . l , bit , n ) ; query_counter ++ ; } } for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ans [ i ] ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 2 , 1 , 3 } ; int n = a . length ; Query [ ] queries = new Query [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) queries [ i ] = new Query ( ) ; queries [ 0 ] . l = 0 ; queries [ 0 ] . r = 4 ; queries [ 0 ] . idx = 0 ; queries [ 1 ] . l = 1 ; queries [ 1 ] . r = 3 ; queries [ 1 ] . idx = 1 ; queries [ 2 ] . l = 2 ; queries [ 2 ] . r = 4 ; queries [ 2 ] . idx = 2 ; int q = queries . length ; Arrays . sort ( queries , new Comparator < Query > ( ) { public int compare ( Query x , Query y ) { if ( x . r < y . r ) return - 1 ; else if ( x . r == y . r ) return 0 ; else return 1 ; } } ) ; answeringQueries ( a , n , queries , q ) ; } }
class GFG { static final int MAX = 100000 ; static int tree [ ] = new int [ MAX ] ; static boolean lazy [ ] = new boolean [ MAX ] ; static void toggle ( int node , int st , int en , int us , int ue ) { if ( lazy [ node ] ) { lazy [ node ] = false ; tree [ node ] = en - st + 1 - tree [ node ] ; if ( st < en ) { lazy [ node << 1 ] = ! lazy [ node << 1 ] ; lazy [ 1 + ( node << 1 ) ] = ! lazy [ 1 + ( node << 1 ) ] ; } } if ( st > en us > en ue < st ) { return ; } if ( us <= st && en <= ue ) { tree [ node ] = en - st + 1 - tree [ node ] ; if ( st < en ) { lazy [ node << 1 ] = ! lazy [ node << 1 ] ; lazy [ 1 + ( node << 1 ) ] = ! lazy [ 1 + ( node << 1 ) ] ; } return ; } int mid = ( st + en ) / 2 ; toggle ( ( node << 1 ) , st , mid , us , ue ) ; toggle ( ( node << 1 ) + 1 , mid + 1 , en , us , ue ) ; if ( st < en ) { tree [ node ] = tree [ node << 1 ] + tree [ ( node << 1 ) + 1 ] ; } } static int countQuery ( int node , int st , int en , int qs , int qe ) { if ( st > en qs > en qe < st ) { return 0 ; } if ( lazy [ node ] ) { lazy [ node ] = false ; tree [ node ] = en - st + 1 - tree [ node ] ; if ( st < en ) { lazy [ node << 1 ] = ! lazy [ node << 1 ] ; lazy [ ( node << 1 ) + 1 ] = ! lazy [ ( node << 1 ) + 1 ] ; } } if ( qs <= st && en <= qe ) { return tree [ node ] ; } int mid = ( st + en ) / 2 ; return countQuery ( ( node << 1 ) , st , mid , qs , qe ) + countQuery ( ( node << 1 ) + 1 , mid + 1 , en , qs , qe ) ; } public static void main ( String args [ ] ) { int n = 5 ; toggle ( 1 , 0 , n - 1 , 1 , 2 ) ; toggle ( 1 , 0 , n - 1 , 2 , 4 ) ; System . out . println ( countQuery ( 1 , 0 , n - 1 , 2 , 3 ) ) ; toggle ( 1 , 0 , n - 1 , 2 , 4 ) ; System . out . println ( countQuery ( 1 , 0 , n - 1 , 1 , 4 ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int a [ ] , int n , int x , int k ) { Arrays . sort ( a ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int d = ( a [ i ] - 1 ) / x ; int it1 = Arrays . binarySearch ( a , Math . max ( ( d + k ) * x , a [ i ] ) ) ; int it2 = Arrays . binarySearch ( a , Math . max ( ( d + k + 1 ) * x , a [ i ] ) ) ; ans += it1 - it2 ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 3 , 5 , 7 } ; int n = a . length ; int x = 2 , k = 1 ; System . out . println ( countPairs ( a , n , x , k ) ) ; } }
import java . util . * ; import java . lang . * ; import java . util . Arrays ; public class GeeksforGeeks { public static int countDe ( int arr [ ] , int n ) { int v [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = arr [ i ] ; Arrays . sort ( arr ) ; int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count1 ++ ; Collections . reverse ( Arrays . asList ( arr ) ) ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count2 ++ ; return ( Math . min ( count1 , count2 ) ) ; } public static void main ( String argc [ ] ) { int arr [ ] = { 5 , 9 , 21 , 17 , 13 } ; int n = 5 ; System . out . println ( " Minimum ▁ Dearrangement ▁ = ▁ " + countDe ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxOfSegmentMins ( int [ ] a , int n , int k ) { if ( k == 1 ) { Arrays . sort ( a ) ; return a [ 0 ] ; } if ( k == 2 ) return Math . max ( a [ 0 ] , a [ n - 1 ] ) ; return a [ n - 1 ] ; } static public void main ( String [ ] args ) { int [ ] a = { - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 } ; int n = a . length ; int k = 2 ; System . out . println ( maxOfSegmentMins ( a , n , k ) ) ; } }
import java . util . * ; class GFG { static int printMinimumProduct ( int arr [ ] , int n ) { int first_min = Math . min ( arr [ 0 ] , arr [ 1 ] ) ; int second_min = Math . max ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] < first_min ) { second_min = first_min ; first_min = arr [ i ] ; } else if ( arr [ i ] < second_min ) second_min = arr [ i ] ; } return first_min * second_min ; } public static void main ( String [ ] args ) { int a [ ] = { 11 , 8 , 5 , 7 , 5 , 100 } ; int n = a . length ; System . out . print ( printMinimumProduct ( a , n ) ) ; } }
import java . util . Arrays ; class GFG { static long noOfTriples ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == arr [ 2 ] ) count ++ ; if ( arr [ 0 ] == arr [ 2 ] ) return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 ; else if ( arr [ 1 ] == arr [ 2 ] ) return ( count - 1 ) * ( count ) / 2 ; return count ; } public static void main ( String arg [ ] ) { long arr [ ] = { 1 , 3 , 3 , 4 } ; int n = arr . length ; System . out . print ( noOfTriples ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static boolean checkReverse ( int arr [ ] , int n ) { int temp [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { temp [ i ] = arr [ i ] ; } Arrays . sort ( temp ) ; int front ; for ( front = 0 ; front < n ; front ++ ) { if ( temp [ front ] != arr [ front ] ) { break ; } } int back ; for ( back = n - 1 ; back >= 0 ; back -- ) { if ( temp [ back ] != arr [ back ] ) { break ; } } if ( front >= back ) { return true ; } do { front ++ ; if ( arr [ front - 1 ] < arr [ front ] ) { return false ; } } while ( front != back ) ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 4 , 3 } ; int n = arr . length ; if ( checkReverse ( arr , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static boolean checkReverse ( int arr [ ] , int n ) { if ( n == 1 ) { return true ; } int i ; for ( i = 1 ; arr [ i - 1 ] < arr [ i ] && i < n ; i ++ ) ; if ( i == n ) { return true ; } int j = i ; while ( j < n && arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) { return false ; } j ++ ; } if ( j == n ) { return true ; } int k = j ; if ( arr [ k ] < arr [ i - 1 ] ) { return false ; } while ( k > 1 && k < n ) { if ( arr [ k ] < arr [ k - 1 ] ) { return false ; } k ++ ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 10 , 9 , 8 } ; int n = arr . length ; if ( checkReverse ( arr , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . Arrays ; import java . io . * ; class GFG { static int MinOperation ( int a [ ] , int b [ ] , int n ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] > b [ i ] ) result = result + Math . abs ( a [ i ] - b [ i ] ) ; else if ( a [ i ] < b [ i ] ) result = result + Math . abs ( a [ i ] - b [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 1 , 1 } ; int b [ ] = { 1 , 2 , 2 } ; int n = a . length ; System . out . println ( MinOperation ( a , b , n ) ) ; } }
import java . util . Arrays ; import java . io . * ; public class GFG { public static void sortExceptUandL ( int a [ ] , int l , int u , int n ) { int b [ ] = new int [ n - ( u - l + 1 ) ] ; for ( int i = 0 ; i < l ; i ++ ) b [ i ] = a [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) b [ l + ( i - ( u + 1 ) ) ] = a [ i ] ; Arrays . sort ( b ) ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = b [ i ] ; for ( int i = u + 1 ; i < n ; i ++ ) a [ i ] = b [ l + ( i - ( u + 1 ) ) ] ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 4 , 3 , 12 , 14 , 9 } ; int n = a . length ; int l = 2 , u = 4 ; sortExceptUandL ( a , l , u , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
import java . util . Arrays ; class GFG { static int sortExceptK ( int arr [ ] , int k , int n ) { int temp = arr [ k ] ; arr [ k ] = arr [ n - 1 ] ; arr [ n - 1 ] = temp ; Arrays . sort ( arr , 0 , n - 1 ) ; int last = arr [ n - 1 ] ; for ( int i = n - 1 ; i > k ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ k ] = last ; return 0 ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 4 , 11 , 7 , 6 , 20 } ; int k = 2 ; int n = a . length ; sortExceptK ( a , k , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " - > " ) ; head = head . next ; } } static void sortlist ( int arr [ ] , int N , Node head ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; Node temp = head ; while ( temp != null ) { if ( hash . containsKey ( temp . data ) ) hash . put ( temp . data , hash . get ( temp . data ) + 1 ) ; else hash . put ( temp . data , 1 ) ; temp = temp . next ; } temp = head ; for ( int i = 0 ; i < N ; i ++ ) { int frequency = hash . get ( arr [ i ] ) ; while ( frequency -- > 0 ) { temp . data = arr [ i ] ; temp = temp . next ; } } } public static void main ( String [ ] args ) { Node head = null ; int arr [ ] = { 5 , 1 , 3 , 2 , 8 } ; int N = arr . length ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 5 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; sortlist ( arr , N , head ) ; System . out . print ( " Sorted ▁ List : " + "NEW_LINE"); printList ( head ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static void printRepeating ( Integer [ ] arr , int size ) { SortedSet < Integer > s = new TreeSet < > ( ) ; Collections . addAll ( s , arr ) ; System . out . print ( s ) ; } public static void main ( String args [ ] ) { Integer [ ] arr = { 1 , 3 , 2 , 2 , 1 } ; int n = arr . length ; printRepeating ( arr , n ) ; } }
import java . io . * ; class GFG { static int maxPartitions ( int arr [ ] , int n ) { int ans = 0 , max_so_far = 0 ; for ( int i = 0 ; i < n ; ++ i ) { max_so_far = Math . max ( max_so_far , arr [ i ] ) ; if ( max_so_far == i ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( maxPartitions ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void cuttringRopes ( int Ropes [ ] , int n ) { Arrays . sort ( Ropes ) ; int singleOperation = 0 ; int cuttingLenght = Ropes [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( Ropes [ i ] - cuttingLenght > 0 ) { System . out . print ( n - i + " ▁ " ) ; cuttingLenght = Ropes [ i ] ; singleOperation ++ ; } } if ( singleOperation == 0 ) System . out . print ( "0" ) ; } public static void main ( String [ ] arg ) { int [ ] Ropes = { 5 , 1 , 1 , 2 , 3 , 5 } ; int n = Ropes . length ; cuttringRopes ( Ropes , n ) ; } }
public class GfG { public static void rankify ( int A [ ] , int n ) { float R [ ] = new float [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int r = 1 , s = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != i && A [ j ] < A [ i ] ) r += 1 ; if ( j != i && A [ j ] == A [ i ] ) s += 1 ; } R [ i ] = r + ( float ) ( s - 1 ) / ( float ) 2 ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( R [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 5 , 2 , 1 , 25 , 2 } ; int n = A . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; rankify ( A , n ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int min_noOf_operation ( int arr [ ] , int n , int k ) { int noOfSubtraction ; int res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { noOfSubtraction = 0 ; if ( arr [ i ] > arr [ i - 1 ] ) { noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) noOfSubtraction ++ ; arr [ i ] = arr [ i ] - k * noOfSubtraction ; } res = res + noOfSubtraction ; } return res ; } public static void main ( String argc [ ] ) { int arr = { 1 , 1 , 2 , 3 } ; int N = 4 ; int k = 5 ; System . out . println ( min_noOf_operation ( arr , N , k ) ) ; } }
import java . util . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( arr [ i ] * i ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 6 , 1 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int a [ ] , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( a [ j ] - a [ i ] ) < k ) res ++ ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . length ; System . out . println ( countPairs ( a , n , k ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int countPairs ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && a [ j ] - a [ i ] < k ) { res ++ ; j ++ ; } } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 10 , 4 , 2 } ; int k = 3 ; int n = a . length ; System . out . println ( countPairs ( a , n , k ) ) ; } }
import java . util . * ; class solution { static int findMaxPairs ( int a [ ] , int b [ ] , int n , int k ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; boolean [ ] flag = new boolean [ n ] ; Arrays . fill ( flag , false ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( Math . abs ( a [ i ] - b [ j ] ) <= k && flag [ j ] == false ) { result ++ ; flag [ j ] = true ; break ; } } } return result ; } public static void main ( String args [ ] ) { int [ ] a = { 10 , 15 , 20 } ; int [ ] b = { 17 , 12 , 24 } ; int n = a . length ; int k = 3 ; System . out . println ( findMaxPairs ( a , b , n , k ) ) ; } }
import java . util . * ; class GFG { static int findMaxPairs ( int a [ ] , int b [ ] , int n , int k ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; int result = 0 ; for ( int i = 0 , j = 0 ; i < n && j < n ; ) { if ( Math . abs ( a [ i ] - b [ j ] ) <= k ) { result ++ ; i ++ ; j ++ ; } else if ( a [ i ] > b [ j ] ) j ++ ; else i ++ ; } return result ; } public static void main ( String args [ ] ) { int a [ ] = { 10 , 15 , 20 } ; int b [ ] = { 17 , 12 , 24 } ; int n = a . length ; int k = 3 ; System . out . println ( findMaxPairs ( a , b , n , k ) ) ; } }
import java . * ; import java . util . Arrays ; public class GFG { static int sumOfMinAbsDifferences ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = 0 ; sum += Math . abs ( arr [ 0 ] - arr [ 1 ] ) ; sum += Math . abs ( arr [ n - 1 ] - arr [ n - 2 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) sum += Math . min ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) , Math . abs ( arr [ i ] - arr [ i + 1 ] ) ) ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 10 , 1 , 4 , 8 , 7 } ; int n = arr . length ; System . out . println ( " Sum ▁ = ▁ " + sumOfMinAbsDifferences ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; int a = 0 , b = 0 ; int result = Integer . MAX_VALUE ; while ( a < m && b < n ) { if ( Math . abs ( A [ a ] - B [ b ] ) < result ) result = Math . abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else b ++ ; } return result ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 11 , 5 } ; int B [ ] = { 4 , 12 , 19 , 23 , 127 , 235 } ; int m = A . length ; int n = B . length ; System . out . println ( findSmallestDifference ( A , B , m , n ) ) ; } }
class GFG { static boolean arraySortedOrNot ( int arr [ ] , int n ) { if ( n == 0 n == 1 ) return true ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 23 , 23 , 45 , 78 , 88 } ; int n = arr . length ; if ( arraySortedOrNot ( arr , n ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
import java . util . * ; class Gfg { static void findLarger ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 6 , 1 , 0 , 9 } ; int n = arr . length ; findLarger ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int singleNumber ( int [ ] nums , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; long sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( nums [ i ] ) ) { sum1 += nums [ i ] ; m . put ( nums [ i ] , 1 ) ; } sum2 += nums [ i ] ; } return ( int ) ( 2 * ( sum1 ) - sum2 ) ; } public static void main ( String args [ ] ) { int [ ] a = { 2 , 3 , 5 , 4 , 5 , 3 , 4 } ; int n = 7 ; System . out . println ( singleNumber ( a , n ) ) ; int [ ] b = { 15 , 18 , 16 , 18 , 16 , 15 , 89 } ; System . out . println ( singleNumber ( b , n ) ) ; } }
class Test { static int arr [ ] = new int [ ] { 5 , 1 , 3 , 4 , 7 } ; static int countTriplets ( int n , int sum ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int sum = 12 ; System . out . println ( countTriplets ( arr . length , sum ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countTriplets ( int [ ] a , int n ) { ArrayList < Integer > s = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( a [ i ] ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xr = a [ i ] ^ a [ j ] ; if ( s . contains ( xr ) && xr != a [ i ] && xr != a [ j ] ) count ++ ; } } return count / 3 ; } public static void main ( String srgs [ ] ) { int [ ] a = { 1 , 3 , 5 , 10 , 14 , 15 } ; int n = a . length ; System . out . print ( countTriplets ( a , n ) ) ; } }
class Main { static int countOccurrences ( int arr [ ] , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 } ; int n = arr . length ; int x = 2 ; System . out . println ( countOccurrences ( arr , n , x ) ) ; } }
class GFG { static int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r < l ) return - 1 ; int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } static int countOccurrences ( int arr [ ] , int n , int x ) { int ind = binarySearch ( arr , 0 , n - 1 , x ) ; if ( ind == - 1 ) return 0 ; int count = 1 ; int left = ind - 1 ; while ( left >= 0 && arr [ left ] == x ) { count ++ ; left -- ; } int right = ind + 1 ; while ( right < n && arr [ right ] == x ) { count ++ ; right ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 } ; int n = arr . length ; int x = 2 ; System . out . print ( countOccurrences ( arr , n , x ) ) ; } }
import java . io . * ; import java . util . * ; import java . lang . Math ; class CloseSum { static void printClosest ( int arr [ ] , int n , int x ) { int res_l = 0 , res_r = 0 ; int l = 0 , r = n - 1 , diff = Integer . MAX_VALUE ; while ( r > l ) { if ( Math . abs ( arr [ l ] + arr [ r ] - x ) < diff ) { res_l = l ; res_r = r ; diff = Math . abs ( arr [ l ] + arr [ r ] - x ) ; } if ( arr [ l ] + arr [ r ] > x ) r -- ; else l ++ ; } System . out . println ( " ▁ The ▁ closest ▁ pair ▁ is ▁ " + arr [ res_l ] + " ▁ and ▁ " + arr [ res_r ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 22 , 28 , 29 , 30 , 40 } , x = 54 ; int n = arr . length ; printClosest ( arr , n , x ) ; } }
class CountOnes { int countOnes ( int arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low , ( mid - 1 ) ) ; } return 0 ; } public static void main ( String args [ ] ) { CountOnes ob = new CountOnes ( ) ; int arr [ ] = { 1 , 1 , 1 , 1 , 0 , 0 , 0 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ array ▁ is ▁ " + ob . countOnes ( arr , 0 , n - 1 ) ) ; } }
import java . io . * ; class GFG { static int countOnes ( int arr [ ] , int n ) { int ans ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] < 1 ) high = mid - 1 ; else if ( arr [ mid ] > 1 ) low = mid + 1 ; else { if ( mid == n - 1 arr [ mid + 1 ] != 1 ) return mid + 1 ; else low = mid + 1 ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 , 0 , 0 , 0 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ 1 ' s ▁ in ▁ given ▁ array ▁ is ▁ " + countOnes ( arr , n ) ) ; } }
import java . io . * ; class MissingNumber { int findMissingUtil ( int arr1 [ ] , int arr2 [ ] , int N ) { if ( N == 1 ) return arr1 [ 0 ] ; if ( arr1 [ 0 ] != arr2 [ 0 ] ) return arr1 [ 0 ] ; int lo = 0 , hi = N - 1 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( arr1 [ mid ] == arr2 [ mid ] ) lo = mid ; else hi = mid ; if ( lo == hi - 1 ) break ; } return arr1 [ hi ] ; } void findMissing ( int arr1 [ ] , int arr2 [ ] , int M , int N ) { if ( N == M - 1 ) System . out . println ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr1 , arr2 , M ) + "NEW_LINE"); else if ( M == N - 1 ) System . out . println ( " Missing ▁ Element ▁ is ▁ " + findMissingUtil ( arr2 , arr1 , N ) + "NEW_LINE"); else System . out . println ( " Invalid ▁ Input " ) ; } public static void main ( String args [ ] ) { MissingNumber obj = new MissingNumber ( ) ; int arr1 [ ] = { 1 , 4 , 5 , 7 , 9 } ; int arr2 [ ] = { 4 , 5 , 7 , 9 } ; int M = arr1 . length ; int N = arr2 . length ; obj . findMissing ( arr1 , arr2 , M , N ) ; } }
import java . io . * ; class Missing { void findMissing ( int arr1 [ ] , int arr2 [ ] , int M , int N ) { if ( M != N - 1 && N != M - 1 ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } int res = 0 ; for ( int i = 0 ; i < M ; i ++ ) res = res ^ arr1 [ i ] ; for ( int i = 0 ; i < N ; i ++ ) res = res ^ arr2 [ i ] ; System . out . println ( " Missing ▁ element ▁ is ▁ " + res ) ; } public static void main ( String args [ ] ) { Missing obj = new Missing ( ) ; int arr1 [ ] = { 4 , 1 , 5 , 9 , 7 } ; int arr2 [ ] = { 7 , 5 , 9 , 4 } ; int M = arr1 . length ; int N = arr2 . length ; obj . findMissing ( arr1 , arr2 , M , N ) ; } }
import java . io . * ; class GFG { static int x , y ; static void getTwoElements ( int arr [ ] , int n ) { int xor1 ; int set_bit_no ; int i ; x = 0 ; y = 0 ; xor1 = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) xor1 = xor1 ^ arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor1 = xor1 ^ i ; set_bit_no = xor1 & ~ ( xor1 - 1 ) ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit_no ) != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { if ( ( i & set_bit_no ) != 0 ) x = x ^ i ; else y = y ^ i ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 5 , 1 , 6 , 2 } ; int n = arr . length ; getTwoElements ( arr , n ) ; System . out . println ( " ▁ The ▁ missing ▁ element ▁ is ▁ " + x + " and ▁ the ▁ " + " repeating ▁ number ▁ is ▁ " + y ) ; } }
import java . util . * ; public class Test1 { public static void main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 6 , 2 , 1 , 1 } ; Map < Integer , Boolean > numberMap = new HashMap < > ( ) ; int max = arr . length ; for ( Integer i : arr ) { if ( numberMap . get ( i ) == null ) { numberMap . put ( i , true ) ; } else { System . out . println ( " Repeating ▁ = ▁ " + i ) ; } } for ( int i = 1 ; i <= max ; i ++ ) { if ( numberMap . get ( i ) == null ) { System . out . println ( " Missing ▁ = ▁ " + i ) ; } } } }
import java . util . HashMap ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findFourElements ( int arr [ ] , int n , int X ) { HashMap < Integer , pair > mp = new HashMap < Integer , pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) mp . put ( arr [ i ] + arr [ j ] , new pair ( i , j ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( mp . containsKey ( X - sum ) ) { pair p = mp . get ( X - sum ) ; if ( p . first != i && p . first != j && p . second != i && p . second != j ) { System . out . print ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ p . first ] + " , ▁ " + arr [ p . second ] ) ; return ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 30 , 40 , 1 , 2 } ; int n = arr . length ; int X = 91 ; findFourElements ( arr , n , X ) ; } }
import java . io . * ; class GFG { static int search ( int arr [ ] , int n , int x ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] == x ) return i ; i = i + Math . abs ( arr [ i ] - x ) ; } System . out . println ( " number ▁ is ▁ not " + " ▁ present ! " ) ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 7 , 6 , 7 , 6 , 5 , 4 , 3 , 2 , 3 , 4 , 3 } ; int n = arr . length ; int x = 3 ; System . out . println ( " Element ▁ " + x + " ▁ is ▁ present ▁ at ▁ index ▁ " + search ( arr , n , 3 ) ) ; } }
class GFG { static void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; int third = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; System . out . printf ( " The ▁ third ▁ Largest ▁ " + "element is %dNEW_LINE", third); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . length ; thirdLargest ( arr , n ) ; } }
class GFG { static void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] , second = Integer . MIN_VALUE , third = Integer . MIN_VALUE ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { third = second ; second = arr [ i ] ; } else if ( arr [ i ] > third ) { third = arr [ i ] ; } } System . out . printf ( "The third Largest element is %dNEW_LINE", third); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . length ; thirdLargest ( arr , n ) ; } }
import java . util . * ; class GFG { static boolean checkPair ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = sum / 2 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . contains ( val ) && val == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { System . out . printf ( "Pair elements are %d and %dNEW_LINE", arr[i], val); return true ; } s . add ( arr [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = arr . length ; if ( checkPair ( arr , n ) == false ) { System . out . printf ( " No ▁ pair ▁ found " ) ; } } }
import java . io . * ; class GFG { static String search ( int arr [ ] , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 1 , 5 , 8 } ; int n = arr . length ; int x = 1 ; System . out . println ( search ( arr , n , x ) ) ; } }
import java . awt . Point ; import java . util . Arrays ; import java . util . Vector ; class Test { static Point sequence ( Vector < Integer > a ) { if ( a . size ( ) == 0 ) return new Point ( 0 , 0 ) ; int s = 0 ; int e = a . size ( ) - 1 ; while ( s < e ) { int m = ( s + e ) / 2 ; if ( a . get ( m ) >= m + a . get ( 0 ) ) s = m + 1 ; else e = m ; } return new Point ( a . get ( s ) , a . size ( ) - ( a . get ( a . size ( ) - 1 ) - a . get ( 0 ) ) ) ; } public static void main ( String args [ ] ) { Integer array [ ] = new Integer [ ] { 1 , 2 , 3 , 4 , 4 , 4 , 5 , 6 } ; Point p = sequence ( new Vector < > ( Arrays . asList ( array ) ) ) ; System . out . println ( " Repeated ▁ element ▁ is ▁ " + p . x + " , ▁ it ▁ appears ▁ " + p . y + " ▁ times " ) ; } }
class GFG { static void minAdjDifference ( int arr [ ] , int n ) { if ( n < 2 ) return ; int res = Math . abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) res = Math . min ( res , Math . abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = Math . min ( res , Math . abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; System . out . print ( " Min ▁ Difference ▁ = ▁ " + res ) ; } public static void main ( String arg [ ] ) { int a [ ] = { 10 , 12 , 13 , 15 , 10 } ; int n = a . length ; minAdjDifference ( a , n ) ; } }
import java . util . * ; public class GFG { static void Print3Smallest ( int array [ ] , int n ) { int firstmin = Integer . MAX_VALUE ; int secmin = Integer . MAX_VALUE ; int thirdmin = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } System . out . println ( " First ▁ min ▁ = ▁ " + firstmin ) ; System . out . println ( " Second ▁ min ▁ = ▁ " + secmin ) ; System . out . println ( " Third ▁ min ▁ = ▁ " + thirdmin ) ; } public static void main ( String [ ] args ) { int array [ ] = { 4 , 9 , 1 , 32 , 12 } ; int n = array . length ; Print3Smallest ( array , n ) ; } }
class GFG { static int getMin ( int arr [ ] , int i , int n ) { return ( n == 1 ) ? arr [ i ] : Math . min ( arr [ i ] , getMin ( arr , i + 1 , n - 1 ) ) ; } static int getMax ( int arr [ ] , int i , int n ) { return ( n == 1 ) ? arr [ i ] : Math . max ( arr [ i ] , getMax ( arr , i + 1 , n - 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . length ; System . out . print ( " Minimum ▁ element ▁ of ▁ array : ▁ " + getMin ( arr , 0 , n ) + "NEW_LINE"); System . out . println ( " Maximum ▁ element ▁ of ▁ array : ▁ " + getMax ( arr , 0 , n ) ) ; } }
import java . util . Arrays ; import java . util . Arrays ; class GFG { static int getMin ( int arr [ ] , int n ) { return Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; } static int getMax ( int arr [ ] , int n ) { return Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . length ; System . out . println ( " Minimum ▁ element ▁ of ▁ array : ▁ " + getMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ element ▁ of ▁ array : ▁ " + getMax ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void closestResult ( Integer [ ] a , int [ ] b , int n ) { TreeSet < Integer > vect = new TreeSet < > ( Arrays . asList ( a ) ) ; Vector < Integer > c = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Integer up = vect . higher ( b [ i ] ) ; if ( up == null ) c . add ( - 1 ) ; else c . add ( up ) ; } System . out . print ( " Result ▁ = ▁ " ) ; for ( int i : c ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { Integer [ ] a = { 2 , 5 , 6 , 1 , 8 , 9 } ; int [ ] b = { 2 , 1 , 0 , 5 , 4 , 9 } ; int n = a . length ; closestResult ( a , b , n ) ; } }
import java . util . * ; class GFG { public static void findCounts ( int arr [ ] , int n ) { int hash [ ] = new int [ n ] ; Arrays . fill ( hash , 0 ) ; int i = 0 ; while ( i < n ) { hash [ arr [ i ] - 1 ] ++ ; i ++ ; } System . out . println ( " Below are counts " ▁ + ▁ " of all elements "); for ( i = 0 ; i < n ; i ++ ) { System . out . println ( ( i + 1 ) + " ▁ - > ▁ " + hash [ i ] ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 2 , 5 } ; findCounts ( arr , arr . length ) ; int arr1 [ ] = { 1 } ; findCounts ( arr1 , arr1 . length ) ; int arr3 [ ] = { 4 , 4 , 4 , 4 } ; findCounts ( arr3 , arr3 . length ) ; int arr2 [ ] = { 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 } ; findCounts ( arr2 , arr2 . length ) ; int arr4 [ ] = { 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 } ; findCounts ( arr4 , arr4 . length ) ; int arr5 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 } ; findCounts ( arr5 , arr5 . length ) ; int arr6 [ ] = { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ; findCounts ( arr6 , arr6 . length ) ; } }
class CountFrequencies { void findCounts ( int arr [ ] , int n ) { int i = 0 ; while ( i < n ) { if ( arr [ i ] <= 0 ) { i ++ ; continue ; } int elementIndex = arr [ i ] - 1 ; if ( arr [ elementIndex ] > 0 ) { arr [ i ] = arr [ elementIndex ] ; arr [ elementIndex ] = - 1 ; } else { arr [ elementIndex ] -- ; arr [ i ] = 0 ; i ++ ; } } System . out . println ( " Below ▁ are ▁ counts ▁ of ▁ all ▁ elements " ) ; for ( int j = 0 ; j < n ; j ++ ) System . out . println ( j + 1 + " - > " + Math . abs ( arr [ j ] ) ) ; } public static void main ( String [ ] args ) { CountFrequencies count = new CountFrequencies ( ) ; int arr [ ] = { 2 , 3 , 3 , 2 , 5 } ; count . findCounts ( arr , arr . length ) ; int arr1 [ ] = { 1 } ; count . findCounts ( arr1 , arr1 . length ) ; int arr3 [ ] = { 4 , 4 , 4 , 4 } ; count . findCounts ( arr3 , arr3 . length ) ; int arr2 [ ] = { 1 , 3 , 5 , 7 , 9 , 1 , 3 , 5 , 7 , 9 , 1 } ; count . findCounts ( arr2 , arr2 . length ) ; int arr4 [ ] = { 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 , 3 } ; count . findCounts ( arr4 , arr4 . length ) ; int arr5 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 } ; count . findCounts ( arr5 , arr5 . length ) ; int arr6 [ ] = { 11 , 10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 } ; count . findCounts ( arr6 , arr6 . length ) ; } }
class CountFrequency { void printfrequency ( int arr [ ] , int n ) { for ( int j = 0 ; j < n ; j ++ ) arr [ j ] = arr [ j ] - 1 ; for ( int i = 0 ; i < n ; i ++ ) arr [ arr [ i ] % n ] = arr [ arr [ i ] % n ] + n ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( i + 1 + " - > " + arr [ i ] / n ) ; } public static void main ( String [ ] args ) { CountFrequency count = new CountFrequency ( ) ; int arr [ ] = { 2 , 3 , 3 , 2 , 5 } ; int n = arr . length ; count . printfrequency ( arr , n ) ; } }
import java . io . * ; class Deletion { static int deleteElement ( int arr [ ] , int n , int x ) { if ( arr [ n - 1 ] == x ) return ( n - 1 ) ; int prev = arr [ n - 1 ] , i ; for ( i = n - 2 ; i >= 0 && arr [ i ] != x ; i -- ) { int curr = arr [ i ] ; arr [ i ] = prev ; prev = curr ; } if ( i < 0 ) return 0 ; arr [ i ] = prev ; return ( n - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 15 , 6 , 8 , 9 , 10 } ; int n = arr . length ; int x = 6 ; n = deleteElement ( arr , n , x ) ; System . out . println ( " Modified ▁ array ▁ is " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class Inversion { int getInvCount ( int arr [ ] , int n ) { int invcount = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ] ) great ++ ; invcount += great * small ; } return invcount ; } public static void main ( String args [ ] ) { Inversion inversion = new Inversion ( ) ; int arr [ ] = new int [ ] { 8 , 4 , 2 , 1 } ; int n = arr . length ; System . out . print ( " Inversion ▁ count ▁ : ▁ " + inversion . getInvCount ( arr , n ) ) ; } }
class GFG { public static int maxWater ( int [ ] arr , int n ) { int res = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int left = arr [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { left = Math . max ( left , arr [ j ] ) ; } int right = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { right = Math . max ( right , arr [ j ] ) ; } res += Math . min ( left , right ) - arr [ i ] ; } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . length ; System . out . print ( maxWater ( arr , n ) ) ; } }
class Test { static int arr [ ] = new int [ ] { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; static int findWater ( int n ) { int left [ ] = new int [ n ] ; int right [ ] = new int [ n ] ; int water = 0 ; left [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) left [ i ] = Math . max ( left [ i - 1 ] , arr [ i ] ) ; right [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) right [ i ] = Math . max ( right [ i + 1 ] , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) water += Math . min ( left [ i ] , right [ i ] ) - arr [ i ] ; return water ; } public static void main ( String [ ] args ) { System . out . println ( " Maximum ▁ water ▁ that ▁ can ▁ be ▁ accumulated ▁ is ▁ " + findWater ( arr . length ) ) ; } }
import java . util . * ; class GFG { static int findWater ( int arr [ ] , int n ) { int result = 0 ; int left_max = 0 , right_max = 0 ; int lo = 0 , hi = n - 1 ; while ( lo <= hi ) { if ( arr [ lo ] < arr [ hi ] ) { if ( arr [ lo ] > left_max ) left_max = arr [ lo ] ; else result += left_max - arr [ lo ] ; lo ++ ; } else { if ( arr [ hi ] > right_max ) right_max = arr [ hi ] ; else result += right_max - arr [ hi ] ; hi -- ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . length ; System . out . println ( " Maximum ▁ water ▁ that ▁ " + " can ▁ be ▁ accumulated ▁ is ▁ " + findWater ( arr , n ) ) ; } }
class GFG { public static int maxWater ( int arr [ ] , int n ) { int size = n - 1 ; int prev = arr [ 0 ] ; int prev_index = 0 ; int water = 0 ; int temp = 0 ; for ( int i = 1 ; i <= size ; i ++ ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; prev_index = i ; temp = 0 ; } else { water += prev - arr [ i ] ; temp += prev - arr [ i ] ; } } if ( prev_index < size ) { water -= temp ; prev = arr [ size ] ; for ( int i = size ; i >= prev_index ; i -- ) { if ( arr [ i ] >= prev ) { prev = arr [ i ] ; } else { water += prev - arr [ i ] ; } } } return water ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . length ; System . out . print ( maxWater ( arr , n ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static int maxWater ( int [ ] height ) { Stack < Integer > stack = new Stack < > ( ) ; int n = height . length ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ( ! stack . isEmpty ( ) ) && ( height [ stack . peek ( ) ] < height [ i ] ) ) { int pop_height = height [ stack . peek ( ) ] ; stack . pop ( ) ; if ( stack . isEmpty ( ) ) break ; int distance = i - stack . peek ( ) - 1 ; int min_height = Math . min ( height [ stack . peek ( ) ] , height [ i ] ) - pop_height ; ans += distance * min_height ; } stack . push ( i ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; System . out . print ( maxWater ( arr ) ) ; } }
import java . util . * ; class GFG { static int maxWater ( int [ ] arr , int n ) { int left = 0 ; int right = n - 1 ; int l_max = 0 ; int r_max = 0 ; int result = 0 ; while ( left <= right ) { if ( r_max <= l_max ) { result += Math . max ( 0 , r_max - arr [ right ] ) ; r_max = Math . max ( r_max , arr [ right ] ) ; right -= 1 ; } else { result += Math . max ( 0 , l_max - arr [ left ] ) ; l_max = Math . max ( l_max , arr [ left ] ) ; left += 1 ; } } return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 0 , 2 , 1 , 0 , 1 , 3 , 2 , 1 , 2 , 1 } ; int n = arr . length ; System . out . print ( maxWater ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int missingK ( int [ ] a , int k , int n ) { int difference = 0 , ans = 0 , count = k ; boolean flag = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { difference = 0 ; if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) { difference += ( a [ i + 1 ] - a [ i ] ) - 1 ; if ( difference >= count ) { ans = a [ i ] + count ; flag = true ; break ; } else count -= difference ; } } if ( flag ) return ans ; else return - 1 ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 5 , 11 , 19 } ; int k = 11 ; int n = a . length ; int missing = missingK ( a , k , n ) ; System . out . print ( missing ) ; } }
public class GFG { static int missingK ( int [ ] arr , int k ) { int n = arr . length ; int l = 0 , u = n - 1 , mid ; while ( l <= u ) { mid = ( l + u ) / 2 ; int numbers_less_than_mid = arr [ mid ] - ( mid + 1 ) ; if ( numbers_less_than_mid == k ) { if ( mid > 0 && ( arr [ mid - 1 ] - ( mid ) ) == k ) { u = mid - 1 ; continue ; } return arr [ mid ] - 1 ; } if ( numbers_less_than_mid < k ) { l = mid + 1 ; } else if ( k < numbers_less_than_mid ) { u = mid - 1 ; } } if ( u < 0 ) return k ; int less = arr [ u ] - ( u + 1 ) ; k -= less ; return arr [ u ] + k ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 7 , 11 } ; int k = 5 ; System . out . println ( " Missing ▁ kth ▁ number ▁ = ▁ " + missingK ( arr , k ) ) ; } }
import java . io . * ; class GFG { static int findMedianSortedArrays ( int [ ] a , int n , int [ ] b , int m ) { int min_index = 0 , max_index = n , i , j ; while ( min_index <= max_index ) { i = ( min_index + max_index ) / 2 ; j = ( ( n + m + 1 ) / 2 ) - i ; if ( i < n && j > 0 && b [ j - 1 ] > a [ i ] ) min_index = i + 1 ; else if ( i > 0 && j < m && b [ j ] < a [ i - 1 ] ) max_index = i - 1 ; else { if ( i == 0 ) return b [ j - 1 ] ; if ( j == 0 ) return a [ i - 1 ] ; else return maximum ( a [ i - 1 ] , b [ j - 1 ] ) ; } } System . out . print ( " ERROR ! ! ! ▁ " + "returningNEW_LINE");  return 0 ; } static int maximum ( int a , int b ) { return a > b ? a : b ; } public static void main ( String [ ] args ) { int [ ] a = { 900 } ; int [ ] b = { 10 , 13 , 14 } ; int n = a . length ; int m = b . length ; if ( n < m ) System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( a , n , b , m ) ) ; else System . out . print ( " The ▁ median ▁ is : ▁ " + findMedianSortedArrays ( b , m , a , n ) ) ; } }
import java . io . * ; class GFG { static void printUncommon ( int arr1 [ ] , int arr2 [ ] , int n1 , int n2 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } else if ( arr2 [ j ] < arr1 [ i ] ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; k ++ ; j ++ ; } else { i ++ ; j ++ ; } } while ( i < n1 ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; i ++ ; k ++ ; } while ( j < n2 ) { System . out . print ( arr2 [ j ] + " ▁ " ) ; j ++ ; k ++ ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 10 , 20 , 30 } ; int arr2 [ ] = { 20 , 25 , 30 , 40 , 50 } ; int n1 = arr1 . length ; int n2 = arr2 . length ; printUncommon ( arr1 , arr2 , n1 , n2 ) ; } }
import java . util . HashMap ; import java . util . Map ; import java . util . Map . Entry ; class GFG { static int leastFrequent ( int arr [ ] , int n ) { Map < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( count . containsKey ( key ) ) { int freq = count . get ( key ) ; freq ++ ; count . put ( key , freq ) ; } else count . put ( key , 1 ) ; } int min_count = n + 1 , res = - 1 ; for ( Entry < Integer , Integer > val : count . entrySet ( ) ) { if ( min_count >= val . getValue ( ) ) { res = val . getKey ( ) ; min_count = val . getValue ( ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 1 , 2 , 2 , 3 , 1 } ; int n = arr . length ; System . out . println ( leastFrequent ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int M = 4 ; static int arr [ ] [ ] = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; static void sort ( int a [ ] [ ] , int row , int n ) { for ( int i = 0 ; i < M - 1 ; i ++ ) { if ( a [ row ] [ i ] > a [ row ] [ i + 1 ] ) { int temp = a [ row ] [ i ] ; a [ row ] [ i ] = a [ row ] [ i + 1 ] ; a [ row ] [ i + 1 ] = temp ; } } } static int maximumSum ( int a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a , i , n ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev ) { prev = a [ i ] [ j ] ; sum += prev ; break ; } } if ( j == - 1 ) return 0 ; } return sum ; } public static void main ( String args [ ] ) { int n = arr . length ; System . out . print ( maximumSum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int M = 4 ; static int maximumSum ( int a [ ] [ ] , int n ) { int prev = Math . max ( a [ n - 1 ] [ 0 ] , a [ n - 1 ] [ M - 1 ] + 1 ) ; int sum = prev ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int max_smaller = Integer . MIN_VALUE ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev && a [ i ] [ j ] > max_smaller ) max_smaller = a [ i ] [ j ] ; } if ( max_smaller == Integer . MIN_VALUE ) return 0 ; prev = max_smaller ; sum += max_smaller ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; int n = arr . length ; System . out . print ( maximumSum ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countPairs ( int A [ ] , int n , int k ) { int ans = 0 ; Arrays . sort ( A ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int x = 0 ; while ( ( A [ i ] * Math . pow ( k , x ) ) <= A [ j ] ) { if ( ( A [ i ] * Math . pow ( k , x ) ) == A [ j ] ) { ans ++ ; break ; } x ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 8 , 9 , 12 , 18 , 4 , 24 , 2 , 6 } ; int n = A . length ; int k = 3 ; System . out . println ( countPairs ( A , n , k ) ) ; } }
class GFG { static int findValue ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == k ) k *= 2 ; return k ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 8 , 1 } , k = 2 ; int n = arr . length ; System . out . print ( findValue ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static void dupLastIndex ( int arr [ ] , int n ) { if ( arr == null n <= 0 ) return ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] == arr [ i - 1 ] ) { System . out . println ( " Last ▁ index : " + i ) ; System . out . println ( " Last ▁ duplicate ▁ item : ▁ " + arr [ i ] ) ; return ; } } System . out . print ( " no ▁ duplicate ▁ found " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 5 , 6 , 6 , 7 , 9 } ; int n = arr . length ; dupLastIndex ( arr , n ) ; } }
import java . io . * ; class GFG { static int findSmallest ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }
import java . io . * ; class GFG { static int min_element ( int a [ ] ) { int min = Integer . MAX_VALUE , i ; for ( i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return min ; } static int findSmallest ( int a [ ] , int n ) { int smallest = min_element ( a ) ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] % smallest >= 1 ) return - 1 ; return smallest ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; public class GfG { public static void printMax ( int arr [ ] , int k , int n ) { Integer [ ] brr = new Integer [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr , Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < n ; ++ i ) if ( Arrays . binarySearch ( brr , arr [ i ] , Collections . reverseOrder ( ) ) >= 0 && Arrays . binarySearch ( brr , arr [ i ] , Collections . reverseOrder ( ) ) < k ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 50 , 8 , 45 , 12 , 25 , 40 , 84 } ; int n = arr . length ; int k = 3 ; printMax ( arr , k , n ) ; } }
import java . util . * ; import java . lang . * ; class GfG { public static int findIndex ( int [ ] arr ) { int maxIndex = 0 ; for ( int i = 0 ; i < arr . length ; ++ i ) if ( arr [ i ] > arr [ maxIndex ] ) maxIndex = i ; for ( int i = 0 ; i < arr . length ; ++ i ) if ( maxIndex != i && arr [ maxIndex ] < 2 * arr [ i ] ) return - 1 ; return maxIndex ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 3 , 6 , 1 , 0 } ; System . out . println ( findIndex ( arr ) ) ; } }
import java . io . * ; class GFG { static int find_consecutive_steps ( int arr [ ] , int len ) { int count = 0 ; int maximum = 0 ; for ( int index = 1 ; index < len ; index ++ ) { if ( arr [ index ] > arr [ index - 1 ] ) count ++ ; else { maximum = Math . max ( maximum , count ) ; count = 0 ; } } return Math . max ( maximum , count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int len = arr . length ; System . out . println ( find_consecutive_steps ( arr , len ) ) ; } }
import java . util . Arrays ; import java . io . * ; class GFG { static int CalculateMax ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int min_sum = arr [ 0 ] + arr [ 1 ] ; int max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . abs ( max_sum - min_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 1 , 11 } ; int n = arr . length ; System . out . println ( CalculateMax ( arr , n ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; import java . util . Vector ; class GFG { static long calculate ( long a [ ] , int n ) { Arrays . sort ( a ) ; int i , j ; Vector < Long > s = new Vector < > ( ) ; for ( i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) s . add ( ( a [ i ] + a [ j ] ) ) ; long mini = Collections . min ( s ) ; long maxi = Collections . max ( s ) ; return Math . abs ( maxi - mini ) ; } public static void main ( String [ ] args ) { long a [ ] = { 2 , 6 , 4 , 3 } ; int n = a . length ; System . out . println ( calculate ( a , n ) ) ; } }
import java . util . * ; class GFG { static void printMinDiffPairs ( int arr [ ] , int n ) { if ( n <= 1 ) return ; Arrays . sort ( arr ) ; int minDiff = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) minDiff = Math . min ( minDiff , arr [ i ] - arr [ i - 1 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( arr [ i ] - arr [ i - 1 ] ) == minDiff ) { System . out . print ( " ( " + arr [ i - 1 ] + " , ▁ " + arr [ i ] + " ) , " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 2 , 4 , 1 } ; int n = arr . length ; printMinDiffPairs ( arr , n ) ; } }
public class MaximumAbsoluteDifference { private static int calculateDiff ( int i , int j , int [ ] array ) { return Math . abs ( array [ i ] - array [ j ] ) + Math . abs ( i - j ) ; } private static int maxDistance ( int [ ] array ) { int result = 0 ; for ( int i = 0 ; i < array . length ; i ++ ) { for ( int j = i ; j < array . length ; j ++ ) { result = Math . max ( result , calculateDiff ( i , j , array ) ) ; } } return result ; } public static void main ( String [ ] args ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; System . out . println ( maxDistance ( array ) ) ; } }
import java . io . * ; class GFG { static int extrema ( int a [ ] , int n ) { int count = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( a [ i ] > a [ i - 1 ] && a [ i ] > a [ i + 1 ] ) count += 1 ; if ( a [ i ] < a [ i - 1 ] && a [ i ] < a [ i + 1 ] ) count += 1 ; } return count ; } public static void main ( String args [ ] ) throws IOException { int a [ ] = { 1 , 0 , 2 , 1 } ; int n = a . length ; System . out . println ( extrema ( a , n ) ) ; } }
class GFG { static int sum ( int a [ ] , int n ) { int maxSum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) maxSum = Math . max ( maxSum , a [ i ] + a [ j ] ) ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( a [ i ] + a [ j ] == maxSum ) c ++ ; return c ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }
import java . io . * ; class GFG { static int sum ( int a [ ] , int n ) { int maxVal = a [ 0 ] , maxCount = 1 ; int secondMax = Integer . MIN_VALUE , secondMaxCount = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == maxVal ) maxCount ++ ; else if ( a [ i ] > maxVal ) { secondMax = maxVal ; secondMaxCount = maxCount ; maxVal = a [ i ] ; maxCount = 1 ; } else if ( a [ i ] == secondMax ) { secondMax = a [ i ] ; secondMaxCount ++ ; } else if ( a [ i ] > secondMax ) { secondMax = a [ i ] ; secondMaxCount = 1 ; } } if ( maxCount > 1 ) return maxCount * ( maxCount - 1 ) / 2 ; return secondMaxCount ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 } ; int n = array . length ; System . out . println ( sum ( array , n ) ) ; } }
import java . util . * ; class GFG { static void printSmall ( int arr [ ] , int asize , int n ) { int [ ] copy_arr = Arrays . copyOf ( arr , asize ) ; Arrays . sort ( copy_arr ) ; for ( int i = 0 ; i < asize ; ++ i ) { if ( Arrays . binarySearch ( copy_arr , 0 , n , arr [ i ] ) > - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int asize = arr . length ; int n = 5 ; printSmall ( arr , asize , n ) ; } }
import java . util . Arrays ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { System . out . print ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { System . out . print ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }
import java . util . ArrayList ; class GFG { public static int nobleInteger ( int arr [ ] ) { int size = arr . length ; for ( int i = 0 ; i < size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( arr [ i ] < arr [ j ] ) count ++ ; if ( count == arr [ i ] ) return arr [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) System . out . println ( " The ▁ noble ▁ " + " integer ▁ is ▁ " + res ) ; else System . out . println ( " No ▁ Noble ▁ " + " Integer ▁ Found " ) ; } }
import java . util . Arrays ; public class Main { public static int nobleInteger ( int arr [ ] ) { Arrays . sort ( arr ) ; int n = arr . length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) System . out . println ( " The ▁ noble ▁ integer ▁ is ▁ " + res ) ; else System . out . println ( " No ▁ Noble ▁ Integer ▁ Found " ) ; } }
import java . util . Arrays ; class MinSum { static long findMinSum ( long a [ ] , long b [ ] , long n ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + Math . abs ( a [ i ] - b [ i ] ) ; return sum ; } public static void main ( String [ ] args ) { long a [ ] = { 4 , 1 , 8 , 7 } ; long b [ ] = { 2 , 3 , 6 , 5 } ; int n = a . length ; System . out . println ( findMinSum ( a , b , n ) ) ; } }
import java . util . Arrays ; class GFG { static boolean checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; Arrays . sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 15 , 5 , 0 , 10 } ; int n = arr . length ; if ( checkIsAP ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int countPairs ( int a [ ] , int n ) { int mn = Integer . MAX_VALUE ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { mn = Math . min ( mn , a [ i ] ) ; mx = Math . max ( mx , a [ i ] ) ; } int c1 = 0 ; int c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == mn ) c1 ++ ; if ( a [ i ] == mx ) c2 ++ ; } if ( mn == mx ) return n * ( n - 1 ) / 2 ; else return c1 * c2 ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 2 , 1 , 1 , 3 } ; int n = a . length ; System . out . print ( countPairs ( a , n ) ) ; } }
import java . util . Arrays ; public class Test4 { static int findElement ( int a [ ] , int n , int b ) { Arrays . sort ( a ) ; int max = a [ n - 1 ] ; while ( b < max ) { if ( Arrays . binarySearch ( a , b ) > - 1 ) b *= 2 ; else return b ; } return b ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; int b = 1 ; System . out . println ( findElement ( a , n , b ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int Mod = 1000000007 ; static long findSum ( int arr [ ] , int n ) { long sum = 0 ; Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] < 0 ) { if ( i != n - 1 && arr [ i + 1 ] <= 0 ) { sum = ( sum + ( arr [ i ] * arr [ i + 1 ] ) % Mod ) % Mod ; i += 2 ; } else break ; } int j = n - 1 ; while ( j >= 0 && arr [ j ] > 0 ) { if ( j != 0 && arr [ j - 1 ] > 0 ) { sum = ( sum + ( arr [ j ] * arr [ j - 1 ] ) % Mod ) % Mod ; j -= 2 ; } else break ; } if ( j > i ) sum = ( sum + ( arr [ i ] * arr [ j ] ) % Mod ) % Mod ; else if ( i == j ) sum = ( sum + arr [ i ] ) % Mod ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 9 , 4 , 5 , - 4 , 7 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
public void insertAfter ( Node prev_node , int new_data ) { if ( prev_node == null ) { System . out . println ( " The ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ null " ) ; return ; } Node new_node = new Node ( new_data ) ; new_node . next = prev_node . next ; prev_node . next = new_node ; }
class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } class LinkedList { Node head ; public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public boolean search ( Node head , int x ) { if ( head == null ) return false ; if ( head . data == x ) return true ; return search ( head . next , x ) ; } public static void main ( String args [ ] ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 10 ) ; llist . push ( 30 ) ; llist . push ( 11 ) ; llist . push ( 21 ) ; llist . push ( 14 ) ; if ( llist . search ( llist . head , 21 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class Main { static class Node { int data ; Node next ; } ; static Node head_ref = null ; static void reverse ( ) { Node prev = null ; Node current = head_ref ; while ( current != null ) { Node next = current . next ; current . next = prev ; prev = current ; current = next ; } head_ref = prev ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; } static void printList ( ) { Node temp = head_ref ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { push ( 20 ) ; push ( 4 ) ; push ( 15 ) ; push ( 85 ) ; System . out . print ( "Given linked listNEW_LINE"); printList ( ) ; reverse ( ) ; System . out . print ( " Reversed Linked list "); printList ( ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } System . out . println ( ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node insertBeg ( Node head , int val ) { Node temp = newNode ( val ) ; temp . next = head ; head = temp ; return head ; } static void rearrangeOddEven ( Node head ) { Stack < Node > odd = new Stack < Node > ( ) ; Stack < Node > even = new Stack < Node > ( ) ; int i = 1 ; while ( head != null ) { if ( head . data % 2 != 0 && i % 2 == 0 ) { odd . push ( head ) ; } else if ( head . data % 2 == 0 && i % 2 != 0 ) { even . push ( head ) ; } head = head . next ; i ++ ; } while ( odd . size ( ) > 0 && even . size ( ) > 0 ) { int k = odd . peek ( ) . data ; odd . peek ( ) . data = even . peek ( ) . data ; even . peek ( ) . data = k ; odd . pop ( ) ; even . pop ( ) ; } } public static void main ( String args [ ] ) { Node head = newNode ( 8 ) ; head = insertBeg ( head , 7 ) ; head = insertBeg ( head , 6 ) ; head = insertBeg ( head , 5 ) ; head = insertBeg ( head , 3 ) ; head = insertBeg ( head , 2 ) ; head = insertBeg ( head , 1 ) ; System . out . println ( " Linked ▁ List : " ) ; printList ( head ) ; rearrangeOddEven ( head ) ; System . out . println ( " Linked ▁ List ▁ after ▁ " + " Rearranging : " ) ; printList ( head ) ; } }
static Node deleteAlt ( Node head ) { if ( head == null ) return ; Node node = head . next ; if ( node == null ) return ; head . next = node . next ; head . next = deleteAlt ( head . next ) ; }
static void AlternatingSplit ( Node source , Node aRef , Node bRef ) { Node aDummy = new Node ( ) ; Node aTail = aDummy ; Node bDummy = new Node ( ) ; Node bTail = bDummy ; Node current = source ; aDummy . next = null ; bDummy . next = null ; while ( current != null ) { MoveNode ( ( aTail . next ) , current ) ; aTail = aTail . next ; if ( current != null ) { MoveNode ( ( bTail . next ) , current ) ; bTail = bTail . next ; } } aRef = aDummy . next ; bRef = bDummy . next ; }
class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } class LinkedList { Node head ; boolean areIdentical ( LinkedList listb ) { Node a = this . head , b = listb . head ; while ( a != null && b != null ) { if ( a . data != b . data ) return false ; a = a . next ; b = b . next ; } return ( a == null && b == null ) ; } void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } public static void main ( String args [ ] ) { LinkedList llist1 = new LinkedList ( ) ; LinkedList llist2 = new LinkedList ( ) ; llist1 . push ( 1 ) ; llist1 . push ( 2 ) ; llist1 . push ( 3 ) ; llist2 . push ( 1 ) ; llist2 . push ( 2 ) ; llist2 . push ( 3 ) ; if ( llist1 . areIdentical ( llist2 ) == true ) System . out . println ( " Identical ▁ " ) ; else System . out . println ( " Not ▁ identical ▁ " ) ; } }
boolean areIdenticalRecur ( Node a , Node b ) { if ( a == null && b == null ) return true ; if ( a != null && b != null ) return ( a . data == b . data ) && areIdenticalRecur ( a . next , b . next ) ; return false ; } boolean areIdentical ( LinkedList listb ) { return areIdenticalRecur ( this . head , listb . head ) ; }
class LinkedList { Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } void sortList ( ) { int count [ ] = { 0 , 0 , 0 } ; Node ptr = head ; while ( ptr != null ) { count [ ptr . data ] ++ ; ptr = ptr . next ; } int i = 0 ; ptr = head ; while ( ptr != null ) { if ( count [ i ] == 0 ) i ++ ; else { ptr . data = i ; -- count [ i ] ; ptr = ptr . next ; } } } public void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } void printList ( ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { LinkedList llist = new LinkedList ( ) ; llist . push ( 0 ) ; llist . push ( 1 ) ; llist . push ( 0 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; llist . push ( 1 ) ; llist . push ( 2 ) ; System . out . println ( " Linked ▁ List ▁ before ▁ sorting " ) ; llist . printList ( ) ; llist . sortList ( ) ; System . out . println ( " Linked ▁ List ▁ after ▁ sorting " ) ; llist . printList ( ) ; } }
class Geeks { static class Node { int data ; Node next ; } static Node rearrange ( Node head ) { if ( head == null ) return null ; Node prev = head , curr = head . next ; while ( curr != null ) { if ( prev . data > curr . data ) { int t = prev . data ; prev . data = curr . data ; curr . data = t ; } if ( curr . next != null && curr . next . data > curr . data ) { int t = curr . next . data ; curr . next . data = curr . data ; curr . data = t ; } prev = curr . next ; if ( curr . next == null ) break ; curr = curr . next . next ; } return head ; } static Node push ( Node head , int k ) { Node tem = new Node ( ) ; tem . data = k ; tem . next = head ; head = tem ; return head ; } static void display ( Node head ) { Node curr = head ; while ( curr != null ) { System . out . printf ( " % d ▁ " , curr . data ) ; curr = curr . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 7 ) ; head = push ( head , 3 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 9 ) ; head = rearrange ( head ) ; display ( head ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node rearrangeEvenOdd ( Node head ) { if ( head == null ) return null ; Node odd = head ; Node even = head . next ; Node evenFirst = even ; while ( 1 == 1 ) { if ( odd == null || even == null || ( even . next ) == null ) { odd . next = evenFirst ; break ; } odd . next = even . next ; odd = even . next ; if ( odd . next == null ) { even . next = null ; odd . next = evenFirst ; break ; } even . next = odd . next ; even = odd . next ; } return head ; } static void printlist ( Node node ) { while ( node != null ) { System . out . print ( node . data + " - > " ) ; node = node . next ; } System . out . println ( " NULL " ) ; } public static void main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; System . out . println ( " Given ▁ Linked ▁ List " ) ; printlist ( head ) ; head = rearrangeEvenOdd ( head ) ; System . out . println ( " Modified ▁ Linked ▁ List " ) ; printlist ( head ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static int addWithCarry ( Node head ) { if ( head == null ) return 1 ; int res = head . data + addWithCarry ( head . next ) ; head . data = ( res ) % 10 ; return ( res ) / 10 ; } static Node addOne ( Node head ) { int carry = addWithCarry ( head ) ; if ( carry > 0 ) { Node newNode = newNode ( carry ) ; newNode . next = head ; return newNode ; } return head ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data ) ; node = node . next ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 9 ) ; head . next . next = newNode ( 9 ) ; head . next . next . next = newNode ( 9 ) ; System . out . print ( " List ▁ is ▁ " ) ; printList ( head ) ; head = addOne ( head ) ; System . out . println ( ) ; System . out . print ( " Resultant ▁ list ▁ is ▁ " ) ; printList ( head ) ; } }
class GfG { static class Node { int data ; Node next , arbit ; } static Node reverse ( Node head ) { Node prev = null , current = head , next = null ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node populateArbit ( Node head ) { head = reverse ( head ) ; Node max = head ; Node temp = head . next ; while ( temp != null ) { temp . arbit = max ; if ( max . data < temp . data ) max = temp ; temp = temp . next ; } return reverse ( head ) ; } static void printNextArbitPointers ( Node node ) { System . out . println ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) ; while ( node != null ) { System . out . print ( node . data + " TABSYMBOL TABSYMBOL " ) ; if ( node . next != null ) System . out . print ( node . next . data + " TABSYMBOL TABSYMBOL " ) ; else System . out . print ( " NULL " + " TABSYMBOL TABSYMBOL " ) ; if ( node . arbit != null ) System . out . print ( node . arbit . data ) ; else System . out . print ( " NULL " ) ; System . out . println ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; head = populateArbit ( head ) ; System . out . println ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }
class GfG { static class Node { int data ; Node next , arbit ; } static Node maxNode ; static void populateArbit ( Node head ) { if ( head == null ) return ; if ( head . next == null ) { maxNode = head ; return ; } populateArbit ( head . next ) ; head . arbit = maxNode ; if ( head . data > maxNode . data ) maxNode = head ; return ; } static void printNextArbitPointers ( Node node ) { System . out . println ( " Node TABSYMBOL Next ▁ Pointer TABSYMBOL Arbit ▁ Pointer " ) ; while ( node != null ) { System . out . print ( node . data + " TABSYMBOL TABSYMBOL TABSYMBOL " ) ; if ( node . next != null ) System . out . print ( node . next . data + " TABSYMBOL TABSYMBOL TABSYMBOL TABSYMBOL " ) ; else System . out . print ( " NULL " + " TABSYMBOL TABSYMBOL TABSYMBOL " ) ; if ( node . arbit != null ) System . out . print ( node . arbit . data ) ; else System . out . print ( " NULL " ) ; System . out . println ( ) ; node = node . next ; } } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } public static void main ( String [ ] args ) { Node head = newNode ( 5 ) ; head . next = newNode ( 10 ) ; head . next . next = newNode ( 2 ) ; head . next . next . next = newNode ( 3 ) ; populateArbit ( head ) ; System . out . println ( " Resultant ▁ Linked ▁ List ▁ is : ▁ " ) ; printNextArbitPointers ( head ) ; } }
import java . util . Scanner ; class Node { String data ; Node next ; Node ( String d ) { data = d ; next = null ; } } class LinkedList_Palindrome { Node head ; boolean isPalidromeUtil ( String str ) { int length = str . length ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( length - i - 1 ) ) return false ; return true ; } boolean isPalindrome ( ) { Node node = head ; String str = " " ; while ( node != null ) { str = str . concat ( node . data ) ; node = node . next ; } return isPalidromeUtil ( str ) ; } public static void main ( String [ ] args ) { LinkedList_Palindrome list = new LinkedList_Palindrome ( ) ; list . head = new Node ( " a " ) ; list . head . next = new Node ( " bc " ) ; list . head . next . next = new Node ( " d " ) ; list . head . next . next . next = new Node ( " dcb " ) ; list . head . next . next . next . next = new Node ( " a " ) ; System . out . println ( list . isPalindrome ( ) ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static void deleteLast ( Node head , int x ) { Node temp = head , ptr = null ; while ( temp != null ) { if ( temp . data == x ) ptr = temp ; temp = temp . next ; } if ( ptr != null && ptr . next == null ) { temp = head ; while ( temp . next != ptr ) temp = temp . next ; temp . next = null ; } if ( ptr != null && ptr . next != null ) { ptr . data = ptr . next . data ; temp = ptr . next ; ptr . next = ptr . next . next ; System . gc ( ) ; } } static Node newNode ( int x ) { Node node = new Node ( ) ; node . data = x ; node . next = null ; return node ; } static void display ( Node head ) { Node temp = head ; if ( head == null ) { System . out . print ( "nullNEW_LINE"); return ; } while ( temp != null ) { System . out . printf ( " % d ▁ - - > ▁ " , temp . data ) ; temp = temp . next ; } System . out . print ( "nullNEW_LINE"); } public static void main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = newNode ( 4 ) ; head . next . next . next . next . next . next = newNode ( 4 ) ; System . out . print ( " Created ▁ Linked ▁ list : ▁ " ) ; display ( head ) ; deleteLast ( head , 4 ) ; System . out . print ( " List ▁ after ▁ deletion ▁ of ▁ 4 : ▁ " ) ; display ( head ) ; } }
Node flattenList2 ( Node head ) { Node headcop = head ; Stack < Node > save = new Stack < > ( ) ; save . push ( head ) ; Node prev = null ; while ( ! save . isEmpty ( ) ) { Node temp = save . peek ( ) ; save . pop ( ) ; if ( temp . next ) save . push ( temp . next ) ; if ( temp . down ) save . push ( temp . down ) ; if ( prev != null ) prev . next = temp ; prev = temp ; } return headcop ; }
import java . util . * ; import java . lang . * ; import java . io . * ; class LinkedList { static Node head ; boolean borrow ; static class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } int getLength ( Node node ) { int size = 0 ; while ( node != null ) { node = node . next ; size ++ ; } return size ; } Node paddZeros ( Node sNode , int diff ) { if ( sNode == null ) return null ; Node zHead = new Node ( 0 ) ; diff -- ; Node temp = zHead ; while ( ( diff -- ) != 0 ) { temp . next = new Node ( 0 ) ; temp = temp . next ; } temp . next = sNode ; return zHead ; } Node subtractLinkedListHelper ( Node l1 , Node l2 ) { if ( l1 == null && l2 == null && borrow == false ) return null ; Node previous = subtractLinkedListHelper ( ( l1 != null ) ? l1 . next : null , ( l2 != null ) ? l2 . next : null ) ; int d1 = l1 . data ; int d2 = l2 . data ; int sub = 0 ; if ( borrow ) { d1 -- ; borrow = false ; } if ( d1 < d2 ) { borrow = true ; d1 = d1 + 10 ; } sub = d1 - d2 ; Node current = new Node ( sub ) ; current . next = previous ; return current ; } Node subtractLinkedList ( Node l1 , Node l2 ) { if ( l1 == null && l2 == null ) return null ; int len1 = getLength ( l1 ) ; int len2 = getLength ( l2 ) ; Node lNode = null , sNode = null ; Node temp1 = l1 ; Node temp2 = l2 ; if ( len1 != len2 ) { lNode = len1 > len2 ? l1 : l2 ; sNode = len1 > len2 ? l2 : l1 ; sNode = paddZeros ( sNode , Math . abs ( len1 - len2 ) ) ; } else { while ( l1 != null && l2 != null ) { if ( l1 . data != l2 . data ) { lNode = l1 . data > l2 . data ? temp1 : temp2 ; sNode = l1 . data > l2 . data ? temp2 : temp1 ; break ; } l1 = l1 . next ; l2 = l2 . next ; } } borrow = false ; return subtractLinkedListHelper ( lNode , sNode ) ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 0 ) ; head . next . next = new Node ( 0 ) ; Node head2 = new Node ( 1 ) ; LinkedList ob = new LinkedList ( ) ; Node result = ob . subtractLinkedList ( head , head2 ) ; printList ( result ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node partition ( Node head , int x ) { Node smallerHead = null , smallerLast = null ; Node greaterLast = null , greaterHead = null ; Node equalHead = null , equalLast = null ; while ( head != null ) { if ( head . data == x ) { if ( equalHead == null ) equalHead = equalLast = head ; else { equalLast . next = head ; equalLast = equalLast . next ; } } else if ( head . data < x ) { if ( smallerHead == null ) smallerLast = smallerHead = head ; else { smallerLast . next = head ; smallerLast = head ; } } else { if ( greaterHead == null ) greaterLast = greaterHead = head ; else { greaterLast . next = head ; greaterLast = head ; } } head = head . next ; } if ( greaterLast != null ) greaterLast . next = null ; if ( smallerHead == null ) { if ( equalHead == null ) return greaterHead ; equalLast . next = greaterHead ; return equalHead ; } if ( equalHead == null ) { smallerLast . next = greaterHead ; return smallerHead ; } smallerLast . next = equalHead ; equalLast . next = greaterHead ; return smallerHead ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { Node head = newNode ( 10 ) ; head . next = newNode ( 4 ) ; head . next . next = newNode ( 5 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 2 ) ; head . next . next . next . next . next = newNode ( 50 ) ; int x = 3 ; head = partition ( head , x ) ; printList ( head ) ; } }
import java . util . * ; class GfG { static class Node { int data ; Node next ; } static Node getLoopstart ( Node loop_node , Node head ) { Node ptr1 = loop_node ; Node ptr2 = loop_node ; int k = 1 , i ; while ( ptr1 . next != ptr2 ) { ptr1 = ptr1 . next ; k ++ ; } ptr1 = head ; ptr2 = head ; for ( i = 0 ; i < k ; i ++ ) ptr2 = ptr2 . next ; while ( ptr2 != ptr1 ) { ptr1 = ptr1 . next ; ptr2 = ptr2 . next ; } return ptr1 ; } static Node detectAndgetLoopstarting ( Node head ) { Node slow_p = head , fast_p = head , loop_start = null ; while ( slow_p != null && fast_p != null && fast_p . next != null ) { slow_p = slow_p . next ; fast_p = fast_p . next . next ; if ( slow_p == fast_p ) { loop_start = getLoopstart ( slow_p , head ) ; break ; } } return loop_start ; } static boolean isPalindromeUtil ( Node head , Node loop_start ) { Node ptr = head ; Stack < Integer > s = new Stack < Integer > ( ) ; int count = 0 ; while ( ptr != loop_start count != 1 ) { s . push ( ptr . data ) ; if ( ptr == loop_start ) count = 1 ; ptr = ptr . next ; } ptr = head ; count = 0 ; while ( ptr != loop_start count != 1 ) { if ( ptr . data == s . peek ( ) ) s . pop ( ) ; else return false ; if ( ptr == loop_start ) count = 1 ; ptr = ptr . next ; } return true ; } static boolean isPalindrome ( Node head ) { Node loop_start = detectAndgetLoopstarting ( head ) ; return isPalindromeUtil ( head , loop_start ) ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } public static void main ( String [ ] args ) { Node head = newNode ( 50 ) ; head . next = newNode ( 20 ) ; head . next . next = newNode ( 15 ) ; head . next . next . next = newNode ( 20 ) ; head . next . next . next . next = newNode ( 50 ) ; head . next . next . next . next . next = head . next . next ; if ( isPalindrome ( head ) == true ) System . out . println ( " Palindrome " ) ; else System . out . println ( " Not ▁ Palindrome " ) ; } }
class GFG { static class Node { int data ; Node next ; } static Node newNode ( int x ) { Node temp = new Node ( ) ; temp . data = x ; temp . next = null ; return temp ; } static void printList ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } System . out . printf ( "NEW_LINE"); } static void moveToEnd ( Node head , int key ) { Node pKey = head ; Node pCrawl = head ; while ( pCrawl != null ) { if ( pCrawl != pKey && pCrawl . data != key ) { pKey . data = pCrawl . data ; pCrawl . data = key ; pKey = pKey . next ; } if ( pKey . data != key ) pKey = pKey . next ; pCrawl = pCrawl . next ; } } public static void main ( String args [ ] ) { Node head = newNode ( 10 ) ; head . next = newNode ( 20 ) ; head . next . next = newNode ( 10 ) ; head . next . next . next = newNode ( 30 ) ; head . next . next . next . next = newNode ( 40 ) ; head . next . next . next . next . next = newNode ( 10 ) ; head . next . next . next . next . next . next = newNode ( 60 ) ; System . out . printf ( "Before moveToEnd(), the Linked list isNEW_LINE"); printList ( head ) ; int key = 10 ; moveToEnd ( head , key ) ; System . out . printf ( " After moveToEnd ( ) , the Linked list is "); printList ( head ) ; } }
import java . io . * ; class GfG { static class Node { int data ; Node next ; } static int LinkedListLength ( Node head ) { while ( head != null && head . next != null ) { head = head . next . next ; } if ( head == null ) return 0 ; return 1 ; } static void push ( Node head , int info ) { Node node = new Node ( ) ; node . data = info ; node . next = ( head ) ; ( head ) = node ; } public static void main ( String [ ] args ) { Node head = null ; push ( head , 4 ) ; push ( head , 5 ) ; push ( head , 7 ) ; push ( head , 2 ) ; push ( head , 9 ) ; push ( head , 6 ) ; push ( head , 1 ) ; push ( head , 2 ) ; push ( head , 0 ) ; push ( head , 5 ) ; push ( head , 5 ) ; int check = LinkedListLength ( head ) ; if ( check == 0 ) { System . out . println ( " Odd " ) ; } else { System . out . println ( " Even " ) ; } } }
class GFG { static class Node { int data ; Node next ; } ; static Node head ; 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 ; head = head_ref ; } static int sumOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; int sum = 0 , len = 0 ; Node temp = head ; while ( temp != null ) { len ++ ; temp = temp . next ; } int c = len - n ; temp = head ; while ( temp != null && c -- > 0 ) { temp = temp . next ; } while ( temp != null ) { sum += temp . data ; temp = temp . next ; } return sum ; } public static void main ( String [ ] args ) { push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; int n = 2 ; System . out . println ( " Sum ▁ of ▁ last ▁ " + n + " ▁ nodes ▁ = ▁ " + sumOfLastN_NodesUtil ( head , n ) ) ; } }
Node SortedMerge ( Node a , Node b ) { Node result = null ; Node lastPtrRef = result ; while ( 1 ) { if ( a == null ) { lastPtrRef = b ; break ; } else if ( b == null ) { lastPtrRef = a ; break ; } if ( a . data <= b . data ) { MoveNode ( lastPtrRef , a ) ; } else { MoveNode ( lastPtrRef , b ) ; } lastPtrRef = ( ( lastPtrRef ) . next ) ; } return ( result ) ; }
import java . io . * ; class Node { int data ; Node next ; Node ( int key ) { data = key ; next = null ; } } class GFG { static Node head ; static Node temp ; static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } System . out . println ( ) ; } static Node mergeKLists ( Node arr [ ] , int last ) { for ( int i = 1 ; i <= last ; i ++ ) { while ( true ) { Node head_0 = arr [ 0 ] ; Node head_i = arr [ i ] ; if ( head_i == null ) break ; if ( head_0 . data >= head_i . data ) { arr [ i ] = head_i . next ; head_i . next = head_0 ; arr [ 0 ] = head_i ; } else { while ( head_0 . next != null ) { if ( head_0 . next . data >= head_i . data ) { arr [ i ] = head_i . next ; head_i . next = head_0 . next ; head_0 . next = head_i ; break ; } head_0 = head_0 . next ; if ( head_0 . next == null ) { arr [ i ] = head_i . next ; head_i . next = null ; head_0 . next = head_i ; head_0 . next . next = null ; break ; } } } } } return arr [ 0 ] ; } public static void main ( String [ ] args ) { int k = 3 ; int n = 4 ; Node [ ] arr = new Node [ k ] ; arr [ 0 ] = new Node ( 1 ) ; arr [ 0 ] . next = new Node ( 3 ) ; arr [ 0 ] . next . next = new Node ( 5 ) ; arr [ 0 ] . next . next . next = new Node ( 7 ) ; arr [ 1 ] = new Node ( 2 ) ; arr [ 1 ] . next = new Node ( 4 ) ; arr [ 1 ] . next . next = new Node ( 6 ) ; arr [ 1 ] . next . next . next = new Node ( 8 ) ; arr [ 2 ] = new Node ( 0 ) ; arr [ 2 ] . next = new Node ( 9 ) ; arr [ 2 ] . next . next = new Node ( 10 ) ; arr [ 2 ] . next . next . next = new Node ( 11 ) ; head = mergeKLists ( arr , k - 1 ) ; printList ( head ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static void printList ( Node node ) { while ( node != null ) { System . out . printf ( " % d ▁ " , node . data ) ; node = node . next ; } } static Node merge ( Node h1 , Node h2 ) { if ( h1 == null ) return h2 ; if ( h2 == null ) return h1 ; if ( h1 . data < h2 . data ) { h1 . next = merge ( h1 . next , h2 ) ; return h1 ; } else { h2 . next = merge ( h1 , h2 . next ) ; return h2 ; } } public static void main ( String args [ ] ) { Node head1 = newNode ( 1 ) ; head1 . next = newNode ( 3 ) ; head1 . next . next = newNode ( 5 ) ; Node head2 = newNode ( 0 ) ; head2 . next = newNode ( 2 ) ; head2 . next . next = newNode ( 4 ) ; Node mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } static Node mergeUtil ( Node h1 , Node h2 ) { if ( h1 . next == null ) { h1 . next = h2 ; return h1 ; } Node curr1 = h1 , next1 = h1 . next ; Node curr2 = h2 , next2 = h2 . next ; while ( next1 != null && curr2 != null ) { if ( ( curr2 . data ) >= ( curr1 . data ) && ( curr2 . data ) <= ( next1 . data ) ) { next2 = curr2 . next ; curr1 . next = curr2 ; curr2 . next = next1 ; curr1 = curr2 ; curr2 = next2 ; } else { if ( next1 . next != null ) { next1 = next1 . next ; curr1 = curr1 . next ; } else { next1 . next = curr2 ; return h1 ; } } } return h1 ; } static Node merge ( Node h1 , Node h2 ) { if ( h1 == null ) return h2 ; if ( h2 == null ) return h1 ; if ( h1 . data < h2 . data ) return mergeUtil ( h1 , h2 ) ; else return mergeUtil ( h2 , h1 ) ; } public static void main ( String [ ] args ) { Node head1 = newNode ( 1 ) ; head1 . next = newNode ( 3 ) ; head1 . next . next = newNode ( 5 ) ; Node head2 = newNode ( 0 ) ; head2 . next = newNode ( 2 ) ; head2 . next . next = newNode ( 4 ) ; Node mergedhead = merge ( head1 , head2 ) ; printList ( mergedhead ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class LinkedList { static Node head ; static class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } static void insertAtMid ( int x ) { if ( head == null ) head = new Node ( x ) ; else { Node newNode = new Node ( x ) ; Node ptr = head ; int len = 0 ; while ( ptr != null ) { len ++ ; ptr = ptr . next ; } int count = ( ( len % 2 ) == 0 ) ? ( len / 2 ) : ( len + 1 ) / 2 ; ptr = head ; while ( count -- > 1 ) ptr = ptr . next ; newNode . next = ptr . next ; ptr . next = newNode ; } } static void display ( ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { head = null ; head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 4 ) ; head . next . next . next = new Node ( 5 ) ; System . out . println ( " Linked ▁ list ▁ before ▁ " + " insertion : ▁ " ) ; display ( ) ; int x = 3 ; insertAtMid ( x ) ; System . out . println ( " Linked list after " + ▁ " insertion : "); display ( ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class LinkedList { static Node head ; static class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } static void insertAtMid ( int x ) { if ( head == null ) head = new Node ( x ) ; else { Node newNode = new Node ( x ) ; Node slow = head ; Node fast = head . next ; while ( fast != null && fast . next != null ) { slow = slow . next ; fast = fast . next . next ; } newNode . next = slow . next ; slow . next = newNode ; } } static void display ( ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } public static void main ( String [ ] args ) { head = null ; head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 4 ) ; head . next . next . next = new Node ( 5 ) ; System . out . println ( " Linked ▁ list ▁ before " + " ▁ insertion : ▁ " ) ; display ( ) ; int x = 3 ; insertAtMid ( x ) ; System . out . println ( " Linked list after " + ▁ " insertion : "); display ( ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . next = null ; return newNode ; } static void insertAfterNthNode ( Node head , int n , int x ) { if ( head == null ) return ; Node newNode = getNode ( x ) ; Node ptr = head ; int len = 0 , i ; while ( ptr != null ) { len ++ ; ptr = ptr . next ; } ptr = head ; for ( i = 1 ; i <= ( len - n ) ; i ++ ) ptr = ptr . next ; newNode . next = ptr . next ; ptr . next = newNode ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String [ ] args ) { Node head = getNode ( 1 ) ; head . next = getNode ( 3 ) ; head . next . next = getNode ( 4 ) ; head . next . next . next = getNode ( 5 ) ; int n = 4 , x = 2 ; System . out . print ( " Original ▁ Linked ▁ List : ▁ " ) ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; System . out . println ( ) ; System . out . print ( " Linked ▁ List ▁ After ▁ Insertion : ▁ " ) ; printList ( head ) ; } }
class GfG { static class Node { int data ; Node next ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . next = null ; return newNode ; } static void insertAfterNthNode ( Node head , int n , int x ) { if ( head == null ) return ; Node newNode = getNode ( x ) ; Node slow_ptr = head ; Node fast_ptr = head ; for ( int i = 1 ; i <= n - 1 ; i ++ ) fast_ptr = fast_ptr . next ; while ( fast_ptr . next != null ) { slow_ptr = slow_ptr . next ; fast_ptr = fast_ptr . next ; } newNode . next = slow_ptr . next ; slow_ptr . next = newNode ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String [ ] args ) { Node head = getNode ( 1 ) ; head . next = getNode ( 3 ) ; head . next . next = getNode ( 4 ) ; head . next . next . next = getNode ( 5 ) ; int n = 4 , x = 2 ; System . out . println ( " Original ▁ Linked ▁ List : ▁ " ) ; printList ( head ) ; insertAfterNthNode ( head , n , x ) ; System . out . println ( ) ; System . out . println ( " Linked ▁ List ▁ After ▁ Insertion : ▁ " ) ; printList ( head ) ; } }
public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; next = null ; } } static Node head ; static void setMiddleHead ( ) { if ( head == null ) return ; Node one_node = head ; Node two_node = head ; Node prev = null ; while ( two_node != null && two_node . next != null ) { prev = one_node ; two_node = two_node . next . next ; one_node = one_node . next ; } prev . next = prev . next . next ; one_node . next = head ; head = one_node ; } static void push ( int new_data ) { Node new_node = new Node ( new_data ) ; new_node . next = head ; head = new_node ; } static void printList ( Node ptr ) { while ( ptr != null ) { System . out . print ( ptr . data + " ▁ " ) ; ptr = ptr . next ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { head = null ; int i ; for ( i = 5 ; i > 0 ; i -- ) push ( i ) ; System . out . print ( " ▁ list ▁ before : ▁ " ) ; printList ( head ) ; setMiddleHead ( ) ; System . out . print ( " ▁ list ▁ After : ▁ " ) ; printList ( head ) ; } }
class GFG { static class Node { int data ; Node prev , next ; } ; static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . prev = temp . next = null ; return temp ; } static void printList ( Node head ) { while ( head . next != null ) { System . out . print ( head . data + " ▁ < - > ▁ " ) ; head = head . next ; } System . out . println ( head . data ) ; } static Node insert ( Node head , int val ) { Node temp = newNode ( val ) ; temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; return head ; } static Node reverseList ( Node head ) { Node left = head , right = head ; while ( right . next != null ) right = right . next ; while ( left != right && left . prev != right ) { int t = left . data ; left . data = right . data ; right . data = t ; left = left . next ; right = right . prev ; } return head ; } public static void main ( String args [ ] ) { Node head = newNode ( 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 3 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; printList ( head ) ; System . out . println ( " List ▁ After ▁ Reversing " ) ; head = reverseList ( head ) ; printList ( head ) ; } }
class Solution { static class Node { int data ; Node prev , next ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . prev = newNode . next = null ; return newNode ; } static Node sortedInsert ( Node head_ref , Node newNode ) { Node current ; if ( head_ref == null ) head_ref = newNode ; else if ( ( head_ref ) . data >= newNode . data ) { newNode . next = head_ref ; newNode . next . prev = newNode ; head_ref = newNode ; } else { current = head_ref ; while ( current . next != null && current . next . data < newNode . data ) current = current . next ; newNode . next = current . next ; if ( current . next != null ) newNode . next . prev = newNode ; current . next = newNode ; newNode . prev = current ; } return head_ref ; } static Node insertionSort ( Node head_ref ) { Node sorted = null ; Node current = head_ref ; while ( current != null ) { Node next = current . next ; current . prev = current . next = null ; sorted = sortedInsert ( sorted , current ) ; current = next ; } head_ref = sorted ; return head_ref ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; new_node . prev = null ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 9 ) ; head = push ( head , 3 ) ; head = push ( head , 5 ) ; head = push ( head , 10 ) ; head = push ( head , 12 ) ; head = push ( head , 8 ) ; System . out . println ( "Doubly Linked List Before SortingNEW_LINE"); printList ( head ) ; head = insertionSort ( head ) ; System . out . println ( " Doubly Linked List After Sorting "); printList ( head ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printLevels ( Node node , int low , int high ) { Queue < Node > Q = new LinkedList < > ( ) ; Node marker = new Node ( 4 ) ; int level = 1 ; Q . add ( node ) ; Q . add ( marker ) ; while ( Q . isEmpty ( ) == false ) { Node n = Q . peek ( ) ; Q . remove ( ) ; if ( n == marker ) { System . out . println ( " " ) ; level ++ ; if ( Q . isEmpty ( ) == true level > high ) break ; Q . add ( marker ) ; continue ; } if ( level >= low ) System . out . print ( n . data + " ▁ " ) ; if ( n . left != null ) Q . add ( n . left ) ; if ( n . right != null ) Q . add ( n . right ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 20 ) ; tree . root . left = new Node ( 8 ) ; tree . root . right = new Node ( 22 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . left . right . left = new Node ( 10 ) ; tree . root . left . right . right = new Node ( 14 ) ; System . out . print ( " Level ▁ Order ▁ traversal ▁ between ▁ given ▁ two ▁ levels ▁ is ▁ " ) ; tree . printLevels ( tree . root , 2 , 3 ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printKDistant ( Node node , int k ) { if ( node == null k < 0 ) return ; if ( k == 0 ) { System . out . print ( node . data + " ▁ " ) ; return ; } printKDistant ( node . left , k - 1 ) ; printKDistant ( node . right , k - 1 ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 8 ) ; tree . printKDistant ( tree . root , 2 ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node newnode = new Node ( ) ; newnode . data = data ; newnode . left = newnode . right = null ; return newnode ; } static boolean printKDistant ( Node root , int klevel ) { Queue < Node > q = new LinkedList < > ( ) ; int level = 1 ; boolean flag = false ; q . add ( root ) ; q . add ( null ) ; while ( q . size ( ) > 0 ) { Node temp = q . peek ( ) ; if ( level == klevel && temp != null ) { flag = true ; System . out . print ( temp . data + " ▁ " ) ; } q . remove ( ) ; if ( temp == null ) { if ( q . peek ( ) != null ) q . add ( null ) ; level += 1 ; if ( level > klevel ) break ; } else { if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } } System . out . println ( ) ; return flag ; } public static void main ( String srga [ ] ) { Node root = newNode ( 20 ) ; root . left = newNode ( 10 ) ; root . right = newNode ( 30 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 15 ) ; root . left . right . left = newNode ( 12 ) ; root . right . left = newNode ( 25 ) ; root . right . right = newNode ( 40 ) ; System . out . print ( " data ▁ at ▁ level ▁ 1 ▁ : ▁ " ) ; boolean ret = printKDistant ( root , 1 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 2 ▁ : ▁ " ) ; ret = printKDistant ( root , 2 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 3 ▁ : ▁ " ) ; ret = printKDistant ( root , 3 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total ▁ " + "number of levelsNEW_LINE"); System . out . print ( " data ▁ at ▁ level ▁ 6 ▁ : ▁ " ) ; ret = printKDistant ( root , 6 ) ; if ( ret == false ) System . out . print ( " Number ▁ exceeds ▁ total " + "number of levelsNEW_LINE"); } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printSingles ( Node node ) { if ( node == null ) return ; if ( node . left != null && node . right != null ) { printSingles ( node . left ) ; printSingles ( node . right ) ; } else if ( node . right != null ) { System . out . print ( node . right . data + " ▁ " ) ; printSingles ( node . right ) ; } else if ( node . left != null ) { System . out . print ( node . left . data + " ▁ " ) ; printSingles ( node . left ) ; } } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . right = new Node ( 4 ) ; tree . root . right . left = new Node ( 5 ) ; tree . root . right . left . right = new Node ( 6 ) ; tree . printSingles ( tree . root ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void kDistantFromLeafUtil ( Node node , int path [ ] , boolean visited [ ] , int pathLen , int k ) { if ( node == null ) return ; path [ pathLen ] = node . data ; visited [ pathLen ] = false ; pathLen ++ ; if ( node . left == null && node . right == null && pathLen - k - 1 >= 0 && visited [ pathLen - k - 1 ] == false ) { System . out . print ( path [ pathLen - k - 1 ] + " ▁ " ) ; visited [ pathLen - k - 1 ] = true ; return ; } kDistantFromLeafUtil ( node . left , path , visited , pathLen , k ) ; kDistantFromLeafUtil ( node . right , path , visited , pathLen , k ) ; } void printKDistantfromLeaf ( Node node , int k ) { int path [ ] = new int [ 1000 ] ; boolean visited [ ] = new boolean [ 1000 ] ; kDistantFromLeafUtil ( node , path , visited , 0 , k ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; System . out . println ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }
class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; int printKDistantfromLeaf ( Node node , int k ) { if ( node == null ) return - 1 ; int lk = printKDistantfromLeaf ( node . left , k ) ; int rk = printKDistantfromLeaf ( node . right , k ) ; boolean isLeaf = lk == - 1 && lk == rk ; if ( lk == 0 || rk == 0 || ( isLeaf && k == 0 ) ) System . out . print ( " ▁ " + node . data ) ; if ( isLeaf && k > 0 ) return k - 1 ; if ( lk > 0 && lk < k ) return lk - 1 ; if ( rk > 0 && rk < k ) return rk - 1 ; return - 2 ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . right . left . right = new Node ( 8 ) ; System . out . println ( " ▁ Nodes ▁ at ▁ distance ▁ 2 ▁ are ▁ : " ) ; tree . printKDistantfromLeaf ( tree . root , 2 ) ; } }
import java . util . * ; class Node { int key ; Node left , right ; public Node ( int key ) { this . key = key ; left = right = null ; } } class BinaryTree { Node root ; void printCorner ( Node root ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { int n = q . size ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Node temp = q . peek ( ) ; q . poll ( ) ; if ( i == 0 i == n - 1 ) System . out . print ( temp . key + " ▁ " ) ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 15 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 20 ) ; tree . root . left . left = new Node ( 8 ) ; tree . root . left . right = new Node ( 12 ) ; tree . root . right . left = new Node ( 16 ) ; tree . root . right . right = new Node ( 25 ) ; tree . printCorner ( tree . root ) ; } }
class GFG { static final int COUNT = 10 ; static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static void print2DUtil ( Node root , int space ) { if ( root == null ) return ; space += COUNT ; print2DUtil ( root . right , space ) ; System . out . print ( "NEW_LINE"); for ( int i = COUNT ; i < space ; i ++ ) System . out . print ( " ▁ " ) ; System . out . print ( root . data + "NEW_LINE"); print2DUtil ( root . left , space ) ; } static void print2D ( Node root ) { print2DUtil ( root , 0 ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . left = new Node ( 12 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; root . right . right . right = new Node ( 15 ) ; print2D ( root ) ; } }
class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; static int max_level = 0 ; void leftViewUtil ( Node node , int level ) { if ( node == null ) return ; if ( max_level < level ) { System . out . print ( " ▁ " + node . data ) ; max_level = level ; } leftViewUtil ( node . left , level + 1 ) ; leftViewUtil ( node . right , level + 1 ) ; } void leftView ( ) { leftViewUtil ( root , 1 ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 12 ) ; tree . root . left = new Node ( 10 ) ; tree . root . right = new Node ( 30 ) ; tree . root . right . left = new Node ( 25 ) ; tree . root . right . right = new Node ( 40 ) ; tree . leftView ( ) ; } }
import java . util . * ; public class PrintRightView { private static class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } private static void printLeftView ( Node root ) { if ( root == null ) return ; Queue < Node > queue = new LinkedList < > ( ) ; queue . add ( root ) ; while ( ! queue . isEmpty ( ) ) { int n = queue . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { Node temp = queue . poll ( ) ; if ( i == 1 ) System . out . print ( temp . data + " ▁ " ) ; if ( temp . left != null ) queue . add ( temp . left ) ; if ( temp . right != null ) queue . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 7 ) ; root . left . right = new Node ( 8 ) ; root . right . right = new Node ( 15 ) ; root . right . left = new Node ( 12 ) ; root . right . right . left = new Node ( 14 ) ; printLeftView ( root ) ; } }
import java . io . * ; class GFG { static int rotate ( int arr [ ] , int N , int X ) { long nextPower = 1 ; while ( nextPower <= N ) nextPower *= 2 ; if ( X == 1 ) return ( int ) nextPower - N ; long prevPower = nextPower / 2 ; return 2 * ( N - ( int ) prevPower ) + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int X = 1 ; int N = arr . length ; System . out . println ( rotate ( arr , N , X ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void print ( int mat [ ] [ ] ) { for ( int i = 0 ; i < mat . length ; i ++ ) { for ( int j = 0 ; j < mat [ 0 ] . length ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void performSwap ( int mat [ ] [ ] , int i , int j ) { int N = mat . length ; int ei = N - 1 - i ; int ej = N - 1 - j ; int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ ej ] [ i ] ; mat [ ej ] [ i ] = mat [ ei ] [ ej ] ; mat [ ei ] [ ej ] = mat [ j ] [ ei ] ; mat [ j ] [ ei ] = temp ; } static void rotate ( int mat [ ] [ ] , int N , int K ) { K = K % 4 ; while ( K -- > 0 ) { for ( int i = 0 ; i < N / 2 ; i ++ ) { for ( int j = i ; j < N - i - 1 ; j ++ ) { if ( i != j && ( i + j ) != N - 1 ) { performSwap ( mat , i , j ) ; } } } } print ( mat ) ; } public static void main ( String [ ] args ) { int K = 5 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 6 , 7 , 8 , 9 } , { 11 , 12 , 13 , 14 } , { 16 , 17 , 18 , 19 } , } ; int N = mat . length ; rotate ( mat , N , K ) ; } }
import java . util . * ; class GFG { static void findMaximumZeros ( String str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str . charAt ( i ) == '0' ) c0 ++ ; } if ( c0 == n ) { System . out . print ( n ) ; return ; } String s = str + str ; int mx = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int cs = 0 ; int ce = 0 ; for ( int j = i ; j < i + n ; ++ j ) { if ( s . charAt ( j ) == '0' ) cs ++ ; else break ; } for ( int j = i + n - 1 ; j >= i ; -- j ) { if ( s . charAt ( j ) == '0' ) ce ++ ; else break ; } int val = cs + ce ; mx = Math . max ( val , mx ) ; } System . out . print ( mx ) ; } public static void main ( String [ ] args ) { String s = "1001" ; int n = s . length ( ) ; findMaximumZeros ( s , n ) ; } }
import java . util . * ; class GFG { static void findMaximumZeros ( String str , int n ) { int c0 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str . charAt ( i ) == '0' ) c0 ++ ; } if ( c0 == n ) { System . out . print ( n ) ; return ; } int mx = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) cnt ++ ; else { mx = Math . max ( mx , cnt ) ; cnt = 0 ; } } mx = Math . max ( mx , cnt ) ; int start = 0 , end = n - 1 ; cnt = 0 ; while ( str . charAt ( start ) != '1' && start < n ) { cnt ++ ; start ++ ; } while ( str . charAt ( end ) != '1' && end >= 0 ) { cnt ++ ; end -- ; } mx = Math . max ( mx , cnt ) ; System . out . println ( mx ) ; } public static void main ( String [ ] args ) { String s = "1001" ; int n = s . length ( ) ; findMaximumZeros ( s , n ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void reverse ( int arr [ ] , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void rotateMatrix ( int mat [ ] [ ] ) { int i = 0 ; for ( int rows [ ] : mat ) { reverse ( rows , 0 , rows . length - 1 ) ; reverse ( rows , 0 , i - 1 ) ; reverse ( rows , i , rows . length - 1 ) ; i ++ ; } for ( int rows [ ] : mat ) { for ( int cols : rows ) { System . out . print ( cols + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; rotateMatrix ( mat ) ; } }
import java . io . * ; class GFG { static void stringShift ( String s , int [ ] [ ] shift ) { int val = 0 ; for ( int i = 0 ; i < shift . length ; ++ i ) if ( shift [ i ] [ 0 ] == 0 ) val -= shift [ i ] [ 1 ] ; else val += shift [ i ] [ 1 ] ; int len = s . length ( ) ; val = val % len ; String result = " " ; if ( val > 0 ) result = s . substring ( len - val , ( len - val ) + val ) + s . substring ( 0 , len - val ) ; else result = s . substring ( - val , len + val ) + s . substring ( 0 , - val ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { String s = " abc " ; int [ ] [ ] shift = new int [ ] [ ] { { 0 , 1 } , { 1 , 2 } } ; stringShift ( s , shift ) ; } }
import java . util . * ; class GFG { static void rotateArray ( int [ ] arr , int N ) { int [ ] v = arr ; Arrays . sort ( v ) ; for ( int i = 1 ; i <= N ; ++ i ) { int x = arr [ N - 1 ] ; i = N - 1 ; while ( i > 0 ) { arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; i -= 1 ; } if ( arr == v ) { System . out . print ( " YES " ) ; return ; } } System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 5 , 1 , 2 } ; int N = arr . length ; rotateArray ( arr , N ) ; } }
import java . util . * ; class GFG { static void findLargestRotation ( int num ) { int ans = num ; int len = ( int ) Math . floor ( ( ( int ) Math . log10 ( num ) ) + 1 ) ; int x = ( int ) Math . pow ( 10 , len - 1 ) ; for ( int i = 1 ; i < len ; i ++ ) { int lastDigit = num % 10 ; num = num / 10 ; num += ( lastDigit * x ) ; if ( num > ans ) { ans = num ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 657 ; findLargestRotation ( N ) ; } }
import java . io . * ; class GFG { static int numberOfDigit ( int N ) { int digit = 0 ; while ( N > 0 ) { digit ++ ; N /= 10 ; } return digit ; } static void rotateNumberByK ( int N , int K ) { int X = numberOfDigit ( N ) ; K = ( ( K % X ) + X ) % X ; int left_no = N / ( int ) ( Math . pow ( 10 , X - K ) ) ; N = N % ( int ) ( Math . pow ( 10 , X - K ) ) ; int left_digit = numberOfDigit ( left_no ) ; N = ( N * ( int ) ( Math . pow ( 10 , left_digit ) ) ) + left_no ; System . out . println ( N ) ; } public static void main ( String args [ ] ) { int N = 12345 , K = 7 ; rotateNumberByK ( N , K ) ; } }
import java . util . * ; class GFG { static void minMovesToSort ( int arr [ ] , int N ) { int count = 0 ; int index = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { count ++ ; index = i ; } } if ( count == 0 ) { System . out . print ( "0" ) ; } else if ( count == N - 1 ) { System . out . print ( N - 1 ) ; } else if ( count == 1 && arr [ 0 ] <= arr [ N - 1 ] ) { System . out . print ( index + 1 ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 5 , 4 , 2 } ; int N = arr . length ; minMovesToSort ( arr , N ) ; } }
class GFG { static void diagonalSumPerfectSquare ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { System . out . print ( arr [ ( j + i ) % 7 ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] srgs ) { int N = 7 ; int [ ] arr = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } diagonalSumPerfectSquare ( arr , N ) ; } }
class GFG { static int MaxSum ( int [ ] arr , int n , int k ) { int i , max_sum = 0 , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr [ i ] ; } max_sum = sum ; while ( i < n ) { sum = sum - arr [ i - k ] + arr [ i ] ; if ( max_sum < sum ) { max_sum = sum ; } i ++ ; } return max_sum ; } static int gcd ( int n1 , int n2 ) { if ( n2 == 0 ) { return n1 ; } else { return gcd ( n2 , n1 % n2 ) ; } } static int [ ] RotateArr ( int [ ] arr , int n , int d ) { int i = 0 , j = 0 ; d = d % n ; int no_of_sets = gcd ( d , n ) ; for ( i = 0 ; i < no_of_sets ; i ++ ) { int temp = arr [ i ] ; j = i ; while ( true ) { int k = j + d ; if ( k >= n ) k = k - n ; if ( k == i ) break ; arr [ j ] = arr [ k ] ; j = k ; } arr [ j ] = temp ; } return arr ; } static void performQuery ( int [ ] arr , int Q [ ] [ ] , int q ) { int N = arr . length ; for ( int i = 0 ; i < q ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { arr = RotateArr ( arr , N , Q [ i ] [ 1 ] ) ; for ( int t : arr ) { System . out . print ( t + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } else { System . out . print ( MaxSum ( arr , N , Q [ i ] [ 1 ] ) + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int q = 5 ; int Q [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 1 , 3 } , { 1 , 1 } , { 2 , 4 } } ; performQuery ( arr , Q , q ) ; } }
class GFG { public static int getMinimumRemoval ( String str ) { int n = str . length ( ) ; int ans = n ; if ( n % 2 == 0 ) { int [ ] freqEven = new int [ 128 ] ; int [ ] freqOdd = new int [ 128 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { freqEven [ str . charAt ( i ) ] ++ ; } else { freqOdd [ str . charAt ( i ) ] ++ ; } } int evenMax = 0 , oddMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { evenMax = Math . max ( evenMax , freqEven [ chr ] ) ; oddMax = Math . max ( oddMax , freqOdd [ chr ] ) ; } ans = ans - evenMax - oddMax ; } else { int [ ] freq = new int [ 128 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str . charAt ( i ) ] ++ ; } int strMax = 0 ; for ( char chr = ' a ' ; chr <= ' z ' ; chr ++ ) { strMax = Math . max ( strMax , freq [ chr ] ) ; } ans = ans - strMax ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksgeeks " ; System . out . print ( getMinimumRemoval ( str ) ) ; } }
import java . util . * ; class GFG { static int findAltSubSeq ( String s ) { int n = s . length ( ) , ans = Integer . MIN_VALUE ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { int cur = 0 , f = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( f == 0 && s . charAt ( k ) - '0' == i ) { f = 1 ; cur ++ ; } else if ( f == 1 && s . charAt ( k ) - '0' == j ) { f = 0 ; cur ++ ; } } if ( i != j && cur % 2 == 1 ) cur -- ; ans = Math . max ( cur , ans ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = "100210601" ; System . out . print ( findAltSubSeq ( s ) ) ; } }
import java . util . * ; class GFG { static void matrix ( int n , int m , int [ ] [ ] li ) { int ctr = 0 ; while ( ctr < 2 * n - 1 ) { for ( int i = 0 ; i < Math . abs ( n - ctr - 1 ) ; i ++ ) { System . out . print ( " ▁ " ) ; } Vector < Integer > lst = new Vector < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j == ctr ) { lst . add ( li [ i ] [ j ] ) ; } } } for ( int i = lst . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . print ( lst . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; ctr += 1 ; } } public static void main ( String [ ] args ) { int n = 8 ; int m = n ; int [ ] [ ] li = { { 4 , 5 , 6 , 9 , 8 , 7 , 1 , 4 } , { 1 , 5 , 9 , 7 , 5 , 3 , 1 , 6 } , { 7 , 5 , 3 , 1 , 5 , 9 , 8 , 0 } , { 6 , 5 , 4 , 7 , 8 , 9 , 3 , 7 } , { 3 , 5 , 6 , 4 , 8 , 9 , 2 , 1 } , { 3 , 1 , 6 , 4 , 7 , 9 , 5 , 0 } , { 8 , 0 , 7 , 2 , 3 , 1 , 0 , 8 } , { 7 , 5 , 3 , 1 , 5 , 9 , 8 , 5 } } ; matrix ( n , m , li ) ; } }
class GFG { static class Node { int key ; Node left ; Node right ; int height ; int size ; public Node ( int key ) { this . key = key ; this . left = this . right = null ; this . size = this . height = 1 ; } } ; static class RefInteger { Integer value ; public RefInteger ( Integer value ) { this . value = value ; } } static int height ( Node N ) { if ( N == null ) return 0 ; return N . height ; } static int size ( Node N ) { if ( N == null ) return 0 ; return N . size ; } static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static Node rightRotate ( Node y ) { Node x = y . left ; Node T2 = x . right ; x . right = y ; y . left = T2 ; y . height = max ( height ( y . left ) , height ( y . right ) ) + 1 ; x . height = max ( height ( x . left ) , height ( x . right ) ) + 1 ; y . size = size ( y . left ) + size ( y . right ) + 1 ; x . size = size ( x . left ) + size ( x . right ) + 1 ; return x ; } static Node leftRotate ( Node x ) { Node y = x . right ; Node T2 = y . left ; y . left = x ; x . right = T2 ; x . height = max ( height ( x . left ) , height ( x . right ) ) + 1 ; y . height = max ( height ( y . left ) , height ( y . right ) ) + 1 ; x . size = size ( x . left ) + size ( x . right ) + 1 ; y . size = size ( y . left ) + size ( y . right ) + 1 ; return y ; } static int getBalance ( Node N ) { if ( N == null ) return 0 ; return height ( N . left ) - height ( N . right ) ; } static Node insert ( Node Node , int key , RefInteger count ) { if ( Node == null ) return ( new Node ( key ) ) ; if ( key < Node . key ) Node . left = insert ( Node . left , key , count ) ; else { Node . right = insert ( Node . right , key , count ) ; count . value = count . value + size ( Node . left ) + 1 ; } Node . height = max ( height ( Node . left ) , height ( Node . right ) ) + 1 ; Node . size = size ( Node . left ) + size ( Node . right ) + 1 ; int balance = getBalance ( Node ) ; if ( balance > 1 && key < Node . left . key ) return rightRotate ( Node ) ; if ( balance < - 1 && key > Node . right . key ) return leftRotate ( Node ) ; if ( balance > 1 && key > Node . left . key ) { Node . left = leftRotate ( Node . left ) ; return rightRotate ( Node ) ; } if ( balance < - 1 && key < Node . right . key ) { Node . right = rightRotate ( Node . right ) ; return leftRotate ( Node ) ; } return Node ; } static void constructLowerArray ( int arr [ ] , RefInteger [ ] countSmaller , int n ) { int i , j ; Node root = null ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = new RefInteger ( 0 ) ; for ( i = n - 1 ; i >= 0 ; i -- ) { root = insert ( root , arr [ i ] , countSmaller [ i ] ) ; } } static int countElements ( int A [ ] , int n , int K ) { int count = 0 ; RefInteger [ ] countSmaller = new RefInteger [ n ] ; constructLowerArray ( A , countSmaller , n ) ; int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i <= ( n - K - 1 ) ; i ++ ) { if ( A [ i ] > maxi && countSmaller [ i ] . value >= K ) { count ++ ; maxi = A [ i ] ; } } return count ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 1 , 7 , 3 , 4 , 0 } ; int n = A . length ; int K = 3 ; System . out . println ( countElements ( A , n , K ) ) ; } }
class GFG { static int getFirstElement ( int a [ ] , int N , int K , int M ) { K %= N ; int index ; if ( K >= M ) index = ( N - K ) + ( M - 1 ) ; else index = ( M - K - 1 ) ; int result = a [ index ] ; return result ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int K = 3 , M = 2 ; System . out . println ( getFirstElement ( a , N , K , M ) ) ; } }
import java . util . * ; class GFG { public static int getFirstElement ( int [ ] a , int N , int K , int M ) { K %= N ; int index = ( K + M - 1 ) % N ; int result = a [ index ] ; return result ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 4 , 5 , 23 } ; int N = a . length ; int K = 2 , M = 1 ; System . out . println ( getFirstElement ( a , N , K , M ) ) ; } }
import java . util . * ; class GFG { static int maximumMatchingPairs ( int perm1 [ ] , int perm2 [ ] , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; HashMap < Integer , Integer > mp1 = new HashMap < > ( ) ; HashMap < Integer , Integer > mp2 = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp1 . put ( perm1 [ i ] , i ) ; } for ( int j = 0 ; j < n ; j ++ ) { mp2 . put ( perm2 [ j ] , j ) ; } for ( int i = 0 ; i < n ; i ++ ) { int idx2 = mp2 . get ( perm1 [ i ] ) ; int idx1 = i ; if ( idx1 == idx2 ) { left [ i ] = 0 ; right [ i ] = 0 ; } else if ( idx1 < idx2 ) { left [ i ] = ( n - ( idx2 - idx1 ) ) ; right [ i ] = ( idx2 - idx1 ) ; } else { left [ i ] = ( idx1 - idx2 ) ; right [ i ] = ( n - ( idx1 - idx2 ) ) ; } } HashMap < Integer , Integer > freq1 = new HashMap < > ( ) ; HashMap < Integer , Integer > freq2 = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq1 . containsKey ( left [ i ] ) ) freq1 . put ( left [ i ] , freq1 . get ( left [ i ] ) + 1 ) ; else freq1 . put ( left [ i ] , 1 ) ; if ( freq2 . containsKey ( right [ i ] ) ) freq2 . put ( right [ i ] , freq2 . get ( right [ i ] ) + 1 ) ; else freq2 . put ( right [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Math . max ( freq1 . get ( left [ i ] ) , freq2 . get ( right [ i ] ) ) ) ; } return ans ; } public static void main ( String [ ] args ) { int P1 [ ] = { 5 , 4 , 3 , 2 , 1 } ; int P2 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = P1 . length ; System . out . print ( maximumMatchingPairs ( P1 , P2 , n ) ) ; } }
class GFG { static int arr [ ] = new int [ 10000 ] ; public static void reverse ( int arr [ ] , int s , int e ) { while ( s < e ) { int tem = arr [ s ] ; arr [ s ] = arr [ e ] ; arr [ e ] = tem ; s = s + 1 ; e = e - 1 ; } } public static void fun ( int arr [ ] , int k ) { int n = 4 - 1 ; int v = n - k ; if ( v >= 0 ) { reverse ( arr , 0 , v ) ; reverse ( arr , v + 1 , n ) ; reverse ( arr , 0 , n ) ; } } public static void main ( String args [ ] ) { arr [ 0 ] = 1 ; arr [ 1 ] = 2 ; arr [ 2 ] = 3 ; arr [ 3 ] = 4 ; for ( int i = 0 ; i < 4 ; i ++ ) { fun ( arr , i ) ; System . out . print ( " [ " ) ; for ( int j = 0 ; j < 4 ; j ++ ) { System . out . print ( arr [ j ] + " , ▁ " ) ; } System . out . print ( " ] " ) ; } } }
public class GFG { public static int countRotation ( int [ ] arr , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 4 , 5 , 1 , 2 , 3 } ; System . out . println ( countRotation ( arr1 , arr1 . length ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . HashSet ; import java . util . LinkedList ; import java . util . Queue ; class GFG { static int minCircularRotations ( String target , ArrayList < String > blocked , int N ) { String start = " " ; for ( int i = 0 ; i < N ; i ++ ) { start += '0' ; } HashSet < String > avoid = new HashSet < > ( ) ; for ( int i = 0 ; i < blocked . size ( ) ; i ++ ) avoid . add ( blocked . get ( i ) ) ; if ( avoid . contains ( start ) ) return - 1 ; if ( avoid . contains ( target ) ) return - 1 ; Queue < String > qu = new LinkedList < > ( ) ; qu . add ( start ) ; int count = 0 ; while ( ! qu . isEmpty ( ) ) { count ++ ; int size = qu . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { StringBuilder st = new StringBuilder ( qu . poll ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { char ch = st . charAt ( i ) ; st . setCharAt ( i , ( char ) ( st . charAt ( i ) + 1 ) ) ; if ( st . charAt ( i ) > '9' ) st . setCharAt ( i , '0' ) ; if ( st . toString ( ) . equals ( target ) ) return count ; if ( ! avoid . contains ( st . toString ( ) ) ) qu . add ( st . toString ( ) ) ; avoid . add ( st . toString ( ) ) ; st . setCharAt ( i , ( char ) ( ch - 1 ) ) ; if ( st . charAt ( i ) < '0' ) st . setCharAt ( i , '9' ) ; if ( st . toString ( ) . equals ( target ) ) return count ; if ( ! avoid . contains ( st . toString ( ) ) ) qu . add ( st . toString ( ) ) ; avoid . add ( st . toString ( ) ) ; st . setCharAt ( i , ch ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 4 ; String target = "7531" ; ArrayList < String > blocked = new ArrayList < > ( Arrays . asList ( "1543" , "7434" , "7300" , "7321" , "2427" ) ) ; System . out . println ( minCircularRotations ( target , blocked , N ) ) ; } }
class GFG { static void rotatedSumQuery ( int arr [ ] , int n , int [ ] [ ] query , int Q ) { int [ ] prefix = new int [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix [ i ] = arr [ i ] ; prefix [ i + n ] = arr [ i ] ; } for ( int i = 1 ; i < 2 * n ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; int start = 0 ; for ( int q = 0 ; q < Q ; q ++ ) { if ( query [ q ] [ 0 ] == 1 ) { int k = query [ q ] [ 1 ] ; start = ( start + k ) % n ; } else if ( query [ q ] [ 0 ] == 2 ) { int L , R ; L = query [ q ] [ 1 ] ; R = query [ q ] [ 2 ] ; if ( start + L == 0 ) System . out . print ( prefix [ start + R ] + "NEW_LINE"); else System . out . print ( prefix [ start + R ] - prefix [ start + L - 1 ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int Q = 5 ; int [ ] [ ] query = { { 2 , 1 , 3 } , { 1 , 3 } , { 2 , 0 , 3 } , { 1 , 4 } , { 2 , 3 , 5 } } ; int n = arr . length ; rotatedSumQuery ( arr , n , query , Q ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void isConversionPossible ( String s1 , String s2 , int x ) { int diff = 0 , n ; n = s1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 . charAt ( i ) == s2 . charAt ( i ) ) continue ; diff = ( ( int ) ( s2 . charAt ( i ) - s1 . charAt ( i ) ) + 26 ) % 26 ; if ( diff > x ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { String s1 = " you " ; String s2 = " ara " ; int x = 6 ; isConversionPossible ( s1 , s2 , x ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Array_Rotation { static void RightRotate ( int a [ ] , int n , int k ) { k = k % n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < k ) { System . out . print ( a [ n + i - k ] + " ▁ " ) ; } else { System . out . print ( a [ i - k ] + " ▁ " ) ; } } System . out . println ( ) ; } public static void main ( String args [ ] ) { int Array [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = Array . length ; int K = 2 ; RightRotate ( Array , N , K ) ; } }
class GFG { static class Node { int data ; Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ - > ▁ " ) ; node = node . next ; } System . out . print ( " null " ) ; } static Node rightRotate ( Node head , int k ) { if ( head == null ) return head ; Node tmp = head ; int len = 1 ; while ( tmp . next != null ) { tmp = tmp . next ; len ++ ; } if ( k > len ) k = k % len ; k = len - k ; if ( k == 0 k == len ) return head ; Node current = head ; int cnt = 1 ; while ( cnt < k && current != null ) { current = current . next ; cnt ++ ; } if ( current == null ) return head ; Node kthnode = current ; tmp . next = head ; head = kthnode . next ; kthnode . next = null ; return head ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; int k = 2 ; Node updated_head = rightRotate ( head , k ) ; printList ( updated_head ) ; } }
class GFG { static int cntRotations ( String s , int n ) { String str = s + s ; int pre [ ] = new int [ 2 * n ] ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( i != 0 ) pre [ i ] += pre [ i - 1 ] ; if ( str . charAt ( i ) == ' a ' || str . charAt ( i ) == ' e ' || str . charAt ( i ) == ' i ' || str . charAt ( i ) == ' o ' || str . charAt ( i ) == ' u ' ) { pre [ i ] ++ ; } } int ans = 0 ; for ( int i = n - 1 ; i < 2 * n - 1 ; i ++ ) { int r = i , l = i - n ; int x1 = pre [ r ] ; if ( l >= 0 ) x1 -= pre [ l ] ; r = i - n / 2 ; int left = pre [ r ] ; if ( l >= 0 ) left -= pre [ l ] ; int right = x1 - left ; if ( left > right ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { String s = " abecidft " ; int n = s . length ( ) ; System . out . println ( cntRotations ( s , n ) ) ; } }
class GFG { public static int cntRotations ( char s [ ] , int n ) { int lh = 0 , rh = 0 , i , ans = 0 ; for ( i = 0 ; i < n / 2 ; ++ i ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { lh ++ ; } for ( i = n / 2 ; i < n ; ++ i ) if ( s [ i ] == ' a ' s [ i ] == ' e ' s [ i ] == ' i ' s [ i ] == ' o ' s [ i ] == ' u ' ) { rh ++ ; } if ( lh > rh ) ans ++ ; for ( i = 1 ; i < n ; ++ i ) { if ( s [ i - 1 ] == ' a ' s [ i - 1 ] == ' e ' s [ i - 1 ] == ' i ' s [ i - 1 ] == ' o ' s [ i - 1 ] == ' u ' ) { rh ++ ; lh -- ; } if ( s [ ( i - 1 + n / 2 ) % n ] == ' a ' || s [ ( i - 1 + n / 2 ) % n ] == ' e ' || s [ ( i - 1 + n / 2 ) % n ] == ' i ' || s [ ( i - 1 + n / 2 ) % n ] == ' o ' || s [ ( i - 1 + n / 2 ) % n ] == ' u ' ) { rh -- ; lh ++ ; } if ( lh > rh ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { char s [ ] = { ' a ' , ' b ' , ' e ' , ' c ' , ' i ' , ' d ' , ' f ' , ' t ' } ; int n = s . length ; System . out . println ( cntRotations ( s , n ) ) ; } }
import java . util . * ; class GFG { static int size = 2 ; static void performQueries ( String str , int n , int queries [ ] [ ] , int q ) { int ptr = 0 ; for ( int i = 0 ; i < q ; i ++ ) { if ( queries [ i ] [ 0 ] == 1 ) { ptr = ( ptr + queries [ i ] [ 1 ] ) % n ; } else { int k = queries [ i ] [ 1 ] ; int index = ( ptr + k - 1 ) % n ; System . out . println ( str . charAt ( index ) ) ; } } } public static void main ( String [ ] args ) { String str = " abcdefgh " ; int n = str . length ( ) ; int queries [ ] [ ] = { { 1 , 2 } , { 2 , 2 } , { 1 , 4 } , { 2 , 7 } } ; int q = queries . length ; performQueries ( str , n , queries , q ) ; } }
class Solution { static void countOddRotations ( int n ) { int odd_count = 0 , even_count = 0 ; do { int digit = n % 10 ; if ( digit % 2 == 1 ) odd_count ++ ; else even_count ++ ; n = n / 10 ; } while ( n != 0 ) ; System . out . println ( " Odd ▁ = ▁ " + odd_count ) ; System . out . println ( " Even ▁ = ▁ " + even_count ) ; } public static void main ( String [ ] args ) { int n = 1234 ; countOddRotations ( n ) ; } }
import java . util . * ; class Solution { static class ListNode { int data ; ListNode next ; } static void rotateSubList ( ListNode A , int m , int n , int k ) { int size = n - m + 1 ; if ( k > size ) { k = k % size ; } if ( k == 0 k == size ) { ListNode head = A ; while ( head != null ) { System . out . print ( head . data ) ; head = head . next ; } return ; } ListNode link = null ; if ( m == 1 ) { link = A ; } ListNode c = A ; int count = 0 ; ListNode end = null ; ListNode pre = null ; while ( c != null ) { count ++ ; if ( count == m - 1 ) { pre = c ; link = c . next ; } if ( count == n - k ) { if ( m == 1 ) { end = c ; A = c . next ; } else { end = c ; pre . next = c . next ; } } if ( count == n ) { ListNode d = c . next ; c . next = link ; end . next = d ; ListNode head = A ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } return ; } c = c . next ; } } static ListNode push ( ListNode head , int val ) { ListNode new_node = new ListNode ( ) ; new_node . data = val ; new_node . next = ( head ) ; ( head ) = new_node ; return head ; } public static void main ( String args [ ] ) { ListNode head = null ; head = push ( head , 70 ) ; head = push ( head , 60 ) ; head = push ( head , 50 ) ; head = push ( head , 40 ) ; head = push ( head , 30 ) ; head = push ( head , 20 ) ; head = push ( head , 10 ) ; ListNode tmp = head ; System . out . print ( " Given ▁ List : ▁ " ) ; while ( tmp != null ) { System . out . print ( tmp . data + " ▁ " ) ; tmp = tmp . next ; } System . out . println ( ) ; int m = 3 , n = 6 , k = 2 ; System . out . print ( " After ▁ rotation ▁ of ▁ sublist : ▁ " ) ; rotateSubList ( head , m , n , k ) ; } }
public class GFG { static boolean rightRotationDivisor ( int N ) { int lastDigit = N % 10 ; int rightRotation = ( int ) ( lastDigit * Math . pow ( 10 , ( int ) ( Math . log10 ( N ) ) ) + Math . floor ( N / 10 ) ) ; return ( rightRotation % N == 0 ) ; } static void generateNumbers ( int m ) { for ( int i = ( int ) Math . pow ( 10 , ( m - 1 ) ) ; i < Math . pow ( 10 , m ) ; i ++ ) if ( rightRotationDivisor ( i ) ) System . out . println ( i ) ; } public static void main ( String args [ ] ) { int m = 3 ; generateNumbers ( m ) ; } }
import java . io . * ; class GFG { static void checkIfSortRotated ( int arr [ ] , int n ) { int minEle = Integer . MAX_VALUE ; int maxEle = Integer . MIN_VALUE ; int minIndex = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < minEle ) { minEle = arr [ i ] ; minIndex = i ; } } boolean flag1 = true ; for ( int i = 1 ; i < minIndex ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag1 = false ; break ; } } boolean flag2 = true ; for ( int i = minIndex + 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { flag2 = false ; break ; } } if ( minIndex == 0 ) { System . out . print ( " NO " ) ; return ; } if ( flag1 && flag2 && ( arr [ n - 1 ] < arr [ 0 ] ) ) System . out . println ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 , 1 , 2 } ; int n = arr . length ; checkIfSortRotated ( arr , n ) ; } }
import java . io . * ; class GFG { static boolean checkIfSortRotated ( int arr [ ] , int n ) { int x = 0 , y = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) x ++ ; else y ++ ; } if ( x == 1 y == 1 ) { if ( arr [ n - 1 ] < arr [ 0 ] ) x ++ ; else y ++ ; if ( x == 1 y == 1 ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 2 , 3 , 4 } ; int n = arr . length ; boolean x = checkIfSortRotated ( arr , n ) ; if ( x == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class GFG { static void rotate ( int [ ] [ ] arr ) { int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < i ; ++ j ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n / 2 ; ++ j ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ i ] [ n - j - 1 ] ; arr [ i ] [ n - j - 1 ] = temp ; } } } static void printMatrix ( int arr [ ] [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; printMatrix ( arr ) ; } }
import java . io . * ; class GFG { static void rotate ( int [ ] [ ] arr ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ n - 1 - j ] [ n - 1 - i ] ; arr [ n - 1 - j ] [ n - 1 - i ] = temp ; } } for ( int i = 0 ; i < n / 2 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ n - 1 - i ] [ j ] ; arr [ n - 1 - i ] [ j ] = temp ; } } } static void printMatrix ( int arr [ ] [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate ( arr ) ; printMatrix ( arr ) ; } }
import java . util . * ; class GFG { static void occurredOnce ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; if ( arr [ 0 ] != arr [ 1 ] ) System . out . println ( arr [ 0 ] + " ▁ " ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( arr [ i ] != arr [ i + 1 ] && arr [ i ] != arr [ i - 1 ] ) System . out . print ( arr [ i ] + " ▁ " ) ; if ( arr [ n - 2 ] != arr [ n - 1 ] ) System . out . print ( arr [ n - 1 ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . length ; occurredOnce ( arr , n ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void occurredOnce ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 1 + mp . get ( arr [ i ] ) ) ; else mp . put ( arr [ i ] , 1 ) ; } for ( Map . Entry entry : mp . entrySet ( ) ) { if ( Integer . parseInt ( String . valueOf ( entry . getValue ( ) ) ) == 1 ) System . out . print ( entry . getKey ( ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . length ; occurredOnce ( arr , n ) ; } }
class GFG { static void occurredOnce ( int arr [ ] , int n ) { int i = 1 , len = n ; if ( arr [ 0 ] == arr [ len - 1 ] ) { i = 2 ; len -- ; } for ( ; i < n ; i ++ ) if ( arr [ i ] == arr [ i - 1 ] ) i ++ ; else System . out . print ( arr [ i - 1 ] + " ▁ " ) ; if ( arr [ n - 1 ] != arr [ 0 ] && arr [ n - 1 ] != arr [ n - 2 ] ) System . out . print ( arr [ n - 1 ] ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 } ; int n = arr . length ; occurredOnce ( arr , n ) ; } }
class Geeks { static void rvereseArray ( int arr [ ] , int start , int end ) { while ( start < end ) { int temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; start ++ ; end -- ; } } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void splitArr ( int arr [ ] , int k , int n ) { rvereseArray ( arr , 0 , n - 1 ) ; rvereseArray ( arr , 0 , n - k - 1 ) ; rvereseArray ( arr , n - k , n - 1 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . length ; int k = 2 ; splitArr ( arr , k , n ) ; printArray ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class stringMatching { public static boolean isRotation ( String a , String b ) { int n = a . length ( ) ; int m = b . length ( ) ; if ( n != m ) return false ; int lps [ ] = new int [ n ] ; int len = 0 ; int i = 1 ; lps [ 0 ] = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( len ) ) { lps [ i ] = ++ len ; ++ i ; } else { if ( len == 0 ) { lps [ i ] = 0 ; ++ i ; } else { len = lps [ len - 1 ] ; } } } i = 0 ; for ( int k = lps [ n - 1 ] ; k < m ; ++ k ) { if ( b . charAt ( k ) != a . charAt ( i ++ ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " ABACD " ; String s2 = " CDABA " ; System . out . println ( isRotation ( s1 , s2 ) ? "1" : "0" ) ; } }
import java . io . * ; class GFG { static int countRotationsDivBy8 ( String n ) { int len = n . length ( ) ; int count = 0 ; if ( len == 1 ) { int oneDigit = n . charAt ( 0 ) - '0' ; if ( oneDigit % 8 == 0 ) return 1 ; return 0 ; } if ( len == 2 ) { int first = ( n . charAt ( 0 ) - '0' ) * 10 + ( n . charAt ( 1 ) - '0' ) ; int second = ( n . charAt ( 1 ) - '0' ) * 10 + ( n . charAt ( 0 ) - '0' ) ; if ( first % 8 == 0 ) count ++ ; if ( second % 8 == 0 ) count ++ ; return count ; } int threeDigit ; for ( int i = 0 ; i < ( len - 2 ) ; i ++ ) { threeDigit = ( n . charAt ( i ) - '0' ) * 100 + ( n . charAt ( i + 1 ) - '0' ) * 10 + ( n . charAt ( i + 2 ) - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; } threeDigit = ( n . charAt ( len - 1 ) - '0' ) * 100 + ( n . charAt ( 0 ) - '0' ) * 10 + ( n . charAt ( 1 ) - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; threeDigit = ( n . charAt ( len - 2 ) - '0' ) * 100 + ( n . charAt ( len - 1 ) - '0' ) * 10 + ( n . charAt ( 0 ) - '0' ) ; if ( threeDigit % 8 == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { String n = "43262488612" ; System . out . println ( " Rotations : ▁ " + countRotationsDivBy8 ( n ) ) ; } }
import java . util . * ; class GFG { static int minimunMoves ( String arr [ ] , int n ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int curr_count = 0 ; String tmp = " " ; for ( int j = 0 ; j < n ; j ++ ) { tmp = arr [ j ] + arr [ j ] ; int index = tmp . indexOf ( arr [ i ] ) ; if ( index == arr [ i ] . length ( ) ) return - 1 ; curr_count += index ; } ans = Math . min ( curr_count , ans ) ; } return ans ; } public static void main ( String args [ ] ) { String arr [ ] = { " xzzwo " , " zwoxz " , " zzwox " , " xzzwo " } ; int n = arr . length ; System . out . println ( minimunMoves ( arr , n ) ) ; } }
class GFG { static void restoreSortedArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { reverse ( arr , 0 , i ) ; reverse ( arr , i + 1 , n ) ; reverse ( arr , 0 , n ) ; } } } static void reverse ( int [ ] arr , int i , int j ) { int temp ; while ( i < j ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 , 1 , 2 } ; int n = arr . length ; restoreSortedArray ( arr , n - 1 ) ; printArray ( arr , n ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static int countRotation ( Node head ) { int count = 0 ; int min = head . data ; while ( head != null ) { if ( min > head . data ) break ; count ++ ; head = head . next ; } return count ; } static Node push ( Node head , int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . next = ( head ) ; ( head ) = newNode ; return head ; } static void printList ( Node node ) { while ( node != null ) { System . out . printf ( " % d ▁ " , node . data ) ; node = node . next ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 11 ) ; head = push ( head , 8 ) ; head = push ( head , 5 ) ; head = push ( head , 18 ) ; head = push ( head , 15 ) ; printList ( head ) ; System . out . println ( ) ; System . out . print ( " Linked ▁ list ▁ rotated ▁ elements : ▁ " ) ; System . out . print ( countRotation ( head ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node tail ; static Node rotateHelper ( Node blockHead , Node blockTail , int d , int k ) { if ( d == 0 ) return blockHead ; if ( d > 0 ) { Node temp = blockHead ; for ( int i = 1 ; temp . next . next != null && i < k - 1 ; i ++ ) temp = temp . next ; blockTail . next = blockHead ; tail = temp ; return rotateHelper ( blockTail , temp , d - 1 , k ) ; } if ( d < 0 ) { blockTail . next = blockHead ; tail = blockHead ; return rotateHelper ( blockHead . next , blockHead , d + 1 , k ) ; } return blockHead ; } static Node rotateByBlocks ( Node head , int k , int d ) { if ( head == null head . next == null ) return head ; if ( d == 0 ) return head ; Node temp = head ; tail = null ; int i ; for ( i = 1 ; temp . next != null && i < k ; i ++ ) temp = temp . next ; Node nextBlock = temp . next ; if ( i < k ) head = rotateHelper ( head , temp , d % k , i ) ; else head = rotateHelper ( head , temp , d % k , k ) ; tail . next = rotateByBlocks ( nextBlock , k , d % k ) ; return head ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } public static void main ( String [ ] args ) { Node head = null ; for ( int i = 9 ; i > 0 ; i -= 1 ) head = push ( head , i ) ; System . out . print ( "Given linked list NEW_LINE"); printList ( head ) ; int k = 3 , d = 2 ; head = rotateByBlocks ( head , k , d ) ; System . out . print ( " Rotated by blocks Linked list "); printList ( head ) ; } }
import java . util . * ; class GFG { static int findRotations ( String str ) { String tmp = str + str ; int n = str . length ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { String substring = tmp . substring ( i , i + str . length ( ) ) ; if ( str . equals ( substring ) ) return i ; } return n ; } public static void main ( String [ ] args ) { String str = " aaaa " ; System . out . println ( findRotations ( str ) ) ; } }
class GFG { static boolean isRotation ( long x , long y ) { long x64 = x | ( x << 32 ) ; while ( x64 >= y ) { if ( x64 == y ) { return true ; } x64 >>= 1 ; } return false ; } public static void main ( String [ ] args ) { long x = 122 ; long y = 2147483678L ; if ( isRotation ( x , y ) == false ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; import java . io . * ; class GFG { static String leftrotate ( String str , int d ) { String ans = str . substring ( d ) + str . substring ( 0 , d ) ; return ans ; } static String rightrotate ( String str , int d ) { return leftrotate ( str , str . length ( ) - d ) ; } public static void main ( String args [ ] ) { String str1 = " GeeksforGeeks " ; System . out . println ( leftrotate ( str1 , 2 ) ) ; String str2 = " GeeksforGeeks " ; System . out . println ( rightrotate ( str2 , 2 ) ) ; } }
import java . io . * ; class GFG { static int countRotations ( String n ) { int len = n . length ( ) ; if ( len == 1 ) { int oneDigit = n . charAt ( 0 ) - '0' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } int twoDigit , count = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n . charAt ( i ) - '0' ) * 10 + ( n . charAt ( i + 1 ) - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; } twoDigit = ( n . charAt ( len - 1 ) - '0' ) * 10 + ( n . charAt ( 0 ) - '0' ) ; if ( twoDigit % 4 == 0 ) count ++ ; return count ; } public static void main ( String args [ ] ) { String n = "4834" ; System . out . println ( " Rotations : ▁ " + countRotations ( n ) ) ; } }
class Test { static boolean isRotated ( String str1 , String str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; if ( str1 . length ( ) < 2 ) { return str1 . equals ( str2 ) ; } String clock_rot = " " ; String anticlock_rot = " " ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len ) + str2 . substring ( 0 , len - 2 ) ; clock_rot = clock_rot + str2 . substring ( 2 ) + str2 . substring ( 0 , 2 ) ; return ( str1 . equals ( clock_rot ) || str1 . equals ( anticlock_rot ) ) ; } public static void main ( String [ ] args ) { String str1 = " geeks " ; String str2 = " eksge " ; System . out . println ( isRotated ( str1 , str2 ) ? " Yes " : " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static Node head ; static class Node { int data ; Node next ; public Node ( int data ) { this . data = data ; this . next = null ; } } static void print ( Node head ) { Node temp = head ; while ( temp != null ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } } static void NextGreaterElement ( Node head ) { Node H = head ; Node res = null ; Node tempList = null ; do { Node curr = head ; int Val = - 1 ; do { if ( head . data < curr . data ) { Val = curr . data ; break ; } curr = curr . next ; } while ( curr != head ) ; if ( res == null ) { res = new Node ( Val ) ; tempList = res ; } else { tempList . next = new Node ( Val ) ; tempList = tempList . next ; } head = head . next ; } while ( head != H ) ; print ( res ) ; } public static void main ( String [ ] args ) { head = new Node ( 1 ) ; head . next = new Node ( 5 ) ; head . next . next = new Node ( 12 ) ; head . next . next . next = new Node ( 10 ) ; head . next . next . next . next = new Node ( 0 ) ; head . next . next . next . next . next = head ; NextGreaterElement ( head ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static void printList ( Node n ) { while ( n != null ) { System . out . print ( n . data + " ▁ " ) ; n = n . next ; } } public static void main ( String [ ] args ) { Node head = null ; Node second = null ; Node third = null ; head = new Node ( ) ; second = new Node ( ) ; third = new Node ( ) ; head . data = 1 ; head . next = second ; second . data = 2 ; second . next = third ; third . data = 3 ; third . next = null ; printList ( head ) ; } }
static class Node { int data ; Node next ; Node prev ; } ;
static class Node { int data ; Node next ; } ;
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static void deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; del = null ; return ; } static int largestElement ( Node head_ref ) { Node current ; current = head_ref ; int maxEle = Integer . MIN_VALUE ; do { if ( current . data > maxEle ) { maxEle = current . data ; } current = current . next ; } while ( current != head_ref ) ; return maxEle ; } static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static void deleteFibonacciNodes ( Node head ) { int maxEle = largestElement ( head ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , maxEle ) ; Node ptr = head ; Node next ; do { if ( hash . contains ( ptr . data ) ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 20 ) ; head = push ( head , 13 ) ; head = push ( head , 6 ) ; head = push ( head , 34 ) ; head = push ( head , 11 ) ; head = push ( head , 9 ) ; deleteFibonacciNodes ( head ) ; printList ( head ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; return head_ref ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node deleteoddNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ptr . data % 2 == 1 ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; return head ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 2 ) ; head = push ( head , 12 ) ; head = push ( head , 11 ) ; head = push ( head , 57 ) ; head = push ( head , 61 ) ; head = push ( head , 56 ) ; System . out . println ( " List after deletion : "); head = deleteoddNodes ( head ) ; printList ( head ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class Node { Node next ; int data ; } ; static Node create ( ) { Node new_node = new Node ( ) ; new_node . next = null ; return new_node ; } static Node find_head ( Node random ) { if ( random == null ) return null ; Node head , var = random ; while ( ! ( var . data > var . next . data var . next == random ) ) { var = var . next ; } return var . next ; } static Node sortedInsert ( Node head_ref , Node new_node ) { Node current = head_ref ; if ( current == null ) { new_node . next = new_node ; head_ref = new_node ; } else if ( current . data >= new_node . data ) { while ( current . next != head_ref ) current = current . next ; current . next = new_node ; new_node . next = head_ref ; head_ref = new_node ; } else { while ( current . next != head_ref && current . next . data < new_node . data ) { current = current . next ; } new_node . next = current . next ; current . next = new_node ; } return head_ref ; } static void printList ( Node start ) { Node temp ; if ( start != null ) { temp = start ; do { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } while ( temp != start ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 56 , 2 , 11 , 1 , 90 } ; int list_size , i ; Node start = null ; Node temp ; for ( i = 0 ; i < 6 ; i ++ ) { if ( start != null ) for ( int j = 0 ; j < ( Math . random ( ) * 10 ) ; j ++ ) start = start . next ; temp = create ( ) ; temp . data = arr [ i ] ; start = sortedInsert ( find_head ( start ) , temp ) ; } printList ( find_head ( start ) ) ; } }
public class CircularLinkedList { Node last ; static class Node { int data ; Node next ; } ; public Node addToEmpty ( int data ) { if ( this . last != null ) return this . last ; Node temp = new Node ( ) ; temp . data = data ; this . last = temp ; this . last . next = this . last ; return last ; } public Node addBegin ( int data ) { if ( last == null ) return addToEmpty ( data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = this . last . next ; this . last . next = temp ; return this . last ; } public void traverse ( ) { Node p ; if ( this . last == null ) { System . out . println ( " List ▁ is ▁ empty . " ) ; return ; } p = this . last . next ; do { System . out . print ( p . data + " ▁ " ) ; p = p . next ; } while ( p != this . last . next ) ; System . out . println ( " " ) ; } public int length ( ) { int x = 0 ; if ( this . last == null ) return x ; Node itr = this . last . next ; while ( itr . next != this . last . next ) { x ++ ; itr = itr . next ; } return ( x + 1 ) ; } public Node split ( int k ) { Node pass = new Node ( ) ; if ( this . last == null ) return this . last ; Node newLast , itr = this . last ; for ( int i = 0 ; i < k ; i ++ ) { itr = itr . next ; } newLast = itr ; pass . next = itr . next ; newLast . next = this . last . next ; this . last . next = pass . next ; return newLast ; } public static void main ( String [ ] args ) { CircularLinkedList clist = new CircularLinkedList ( ) ; clist . last = null ; clist . addToEmpty ( 12 ) ; clist . addBegin ( 10 ) ; clist . addBegin ( 8 ) ; clist . addBegin ( 6 ) ; clist . addBegin ( 4 ) ; clist . addBegin ( 2 ) ; System . out . println ( " Original ▁ list : " ) ; clist . traverse ( ) ; int k = 4 ; CircularLinkedList clist2 = new CircularLinkedList ( ) ; clist2 . last = clist . split ( k ) ; System . out . println ( " The ▁ new ▁ lists ▁ are : " ) ; clist2 . traverse ( ) ; clist . traverse ( ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int x ) { data = x ; left = right = null ; } } ; static void inorder ( Node root , Vector < Integer > v ) { if ( root == null ) return ; inorder ( root . left , v ) ; v . add ( root . data ) ; inorder ( root . right , v ) ; } static Node bTreeToCList ( Node root ) { if ( root == null ) return null ; Vector < Integer > v = new Vector < > ( ) ; inorder ( root , v ) ; Node head_ref = new Node ( v . get ( 0 ) ) ; Node curr = head_ref ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { Node temp = curr ; curr . right = new Node ( v . get ( i ) ) ; curr = curr . right ; curr . left = temp ; } curr . right = head_ref ; head_ref . left = curr ; return head_ref ; } static void displayCList ( Node head ) { System . out . println ( " Circular ▁ Doubly ▁ Linked ▁ List ▁ is ▁ : " ) ; Node itr = head ; do { System . out . print ( itr . data + " ▁ " ) ; itr = itr . right ; } while ( head != itr ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 10 ) ; root . left = new Node ( 12 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 25 ) ; root . left . right = new Node ( 30 ) ; root . right . left = new Node ( 36 ) ; Node head = bTreeToCList ( root ) ; displayCList ( head ) ; } }
static void DeleteAllOddNode ( Node head ) { int len = Length ( head ) ; int count = 0 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " Delete Last List is empty "); return ; } if ( len == 1 ) { DeleteFirst ( head ) ; return ; } while ( len > 0 ) { if ( count == 0 ) { DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { deleteNode ( head , previous ) ; } previous = previous . next ; next = previous . next ; len -- ; count ++ ; } return ; }
static Node DeleteAllEvenNode ( Node head ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return null ; } if ( len < 2 ) { return null ; } previous = head ; next = previous . next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous . next = next . next ; previous = next . next ; next = previous . next ; } len -- ; count ++ ; } return head ; }
class GFG { static class Node { int data ; Node next ; } ; static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { System . out . printf ( " Display List is empty "); return ; } else { do { System . out . printf ( " % d ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { System . out . printf ( " Memory Error "); return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) { head_ref = del . next ; } while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return head ; } if ( previous . next == previous ) { head = null ; return head ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteAllOddNode ( Node head ) { int len = Length ( head ) ; int count = 0 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " Delete Last List is empty "); return null ; } if ( len == 1 ) { head = DeleteFirst ( head ) ; return head ; } while ( len > 0 ) { if ( count == 0 ) { head = DeleteFirst ( head ) ; } if ( count % 2 == 0 && count != 0 ) { head = deleteNode ( head , previous ) ; } previous = previous . next ; next = previous . next ; len -- ; count ++ ; } return head ; } static Node DeleteAllEvenNode ( Node head ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return null ; } if ( len < 2 ) { return null ; } previous = head ; next = previous . next ; while ( len > 0 ) { if ( count % 2 == 0 ) { previous . next = next . next ; previous = next . next ; next = previous . next ; } len -- ; count ++ ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = Insert ( head , 99 ) ; head = Insert ( head , 11 ) ; head = Insert ( head , 22 ) ; head = Insert ( head , 33 ) ; head = Insert ( head , 44 ) ; head = Insert ( head , 55 ) ; head = Insert ( head , 66 ) ; System . out . printf ( " Initial ▁ List : ▁ " ) ; Display ( head ) ; System . out . printf ( " After deleting Odd position nodes : "); head = DeleteAllOddNode ( head ) ; Display ( head ) ; System . out . printf ( " Initial List : "); Display ( head ) ; System . out . printf ( " After deleting even position nodes : "); head = DeleteAllEvenNode ( head ) ; Display ( head ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class Node { int data ; Node next ; } ; static Node Insert ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { System . out . printf ( " Memory Error "); return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static void Display ( Node head ) { Node current = head ; if ( head == null ) { System . out . printf ( " Display List is empty "); return ; } else { do { System . out . printf ( " % d ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } static int Length ( Node head ) { Node current = head ; int count = 0 ; if ( head == null ) { return 0 ; } else { do { current = current . next ; count ++ ; } while ( current != head ) ; } return count ; } static Node DeleteFirst ( Node head ) { Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " List is empty "); return null ; } if ( previous . next == previous ) { head = null ; return null ; } while ( previous . next != head ) { previous = previous . next ; next = previous . next ; } previous . next = next . next ; head = previous . next ; return head ; } static Node DeleteLast ( Node head ) { Node current = head , temp = head , previous = null ; if ( head == null ) { System . out . printf ( " List is empty "); return null ; } if ( current . next == current ) { head = null ; return null ; } while ( current . next != head ) { previous = current ; current = current . next ; } previous . next = current . next ; head = previous . next ; return head ; } static Node DeleteAtPosition ( Node head , int index ) { int len = Length ( head ) ; int count = 1 ; Node previous = head , next = head ; if ( head == null ) { System . out . printf ( " Delete Last List is empty "); return null ; } if ( index >= len index < 0 ) { System . out . printf ( " Index is not Found "); return null ; } if ( index == 0 ) { head = DeleteFirst ( head ) ; return head ; } while ( len > 0 ) { if ( index == count ) { previous . next = next . next ; return head ; } previous = previous . next ; next = previous . next ; len -- ; count ++ ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = Insert ( head , 99 ) ; head = Insert ( head , 11 ) ; head = Insert ( head , 22 ) ; head = Insert ( head , 33 ) ; head = Insert ( head , 44 ) ; head = Insert ( head , 55 ) ; head = Insert ( head , 66 ) ; System . out . printf ( " Initial ▁ List : ▁ " ) ; Display ( head ) ; System . out . printf ( " After Deleting node at index 4 : "); head = DeleteAtPosition ( head , 4 ) ; Display ( head ) ; System . out . printf ( " Initial List : "); Display ( head ) ; System . out . printf ( " After Deleting first node : "); head = DeleteFirst ( head ) ; Display ( head ) ; System . out . printf ( " Initial List : "); Display ( head ) ; System . out . printf ( " After Deleting last node : "); head = DeleteLast ( head ) ; Display ( head ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static void printMinMax ( Node head ) { if ( head == null ) { return ; } Node current ; current = head ; int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; while ( current . next != head ) { if ( current . data < min ) { min = current . data ; } if ( current . data > max ) { max = current . data ; } current = current . next ; } System . out . println ( " Minimum = " ▁ + ▁ min ▁ + ▁ " , Maximum = " + max); } static Node insertNode ( Node head , int data ) { Node current = head ; Node newNode = new Node ( ) ; if ( newNode == null ) { System . out . printf ( " Memory Error "); return null ; } newNode . data = data ; if ( head == null ) { newNode . next = newNode ; head = newNode ; return head ; } else { while ( current . next != head ) { current = current . next ; } newNode . next = head ; current . next = newNode ; } return head ; } static void displayList ( Node head ) { Node current = head ; if ( head == null ) { System . out . printf ( " Display List is empty "); return ; } else { do { System . out . printf ( " % d ▁ " , current . data ) ; current = current . next ; } while ( current != head ) ; } } public static void main ( String args [ ] ) { Node Head = null ; Head = insertNode ( Head , 99 ) ; Head = insertNode ( Head , 11 ) ; Head = insertNode ( Head , 22 ) ; Head = insertNode ( Head , 33 ) ; Head = insertNode ( Head , 44 ) ; Head = insertNode ( Head , 55 ) ; Head = insertNode ( Head , 66 ) ; System . out . println ( " Initial ▁ List : ▁ " ) ; displayList ( Head ) ; printMinMax ( Head ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; return head_ref ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static Node deleteNode ( Node head_ref , Node del ) { Node temp = head_ref ; if ( head_ref == del ) head_ref = del . next ; while ( temp . next != del ) { temp = temp . next ; } temp . next = del . next ; return head_ref ; } static Node deleteEvenNodes ( Node head ) { Node ptr = head ; Node next ; do { if ( ptr . data % 2 == 0 ) deleteNode ( head , ptr ) ; next = ptr . next ; ptr = next ; } while ( ptr != head ) ; return head ; } static void printList ( Node head ) { Node temp = head ; if ( head != null ) { do { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . next ; } while ( temp != head ) ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 61 ) ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; head = push ( head , 57 ) ; System . out . println ( " List after deletion : "); head = deleteEvenNodes ( head ) ; printList ( head ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node ptr1 = new Node ( ) ; Node temp = head_ref ; ptr1 . data = data ; ptr1 . next = head_ref ; if ( head_ref != null ) { while ( temp . next != head_ref ) temp = temp . next ; temp . next = ptr1 ; } else ptr1 . next = ptr1 ; head_ref = ptr1 ; return head_ref ; } static int sumOfList ( Node head ) { Node temp = head ; int sum = 0 ; if ( head != null ) { do { temp = temp . next ; sum += temp . data ; } while ( temp != head ) ; } return sum ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 56 ) ; head = push ( head , 2 ) ; head = push ( head , 11 ) ; System . out . println ( " Sum ▁ of ▁ Circular ▁ linked " + " ▁ list ▁ is ▁ = ▁ " + sumOfList ( head ) ) ; } }
class GFG { static class Node { int data ; Node next ; Node ( int x ) { data = x ; next = null ; } } ; static void printList ( Node head ) { if ( head == null ) return ; Node temp = head ; do { System . out . print ( temp . data + " - > " ) ; temp = temp . next ; } while ( temp != head ) ; System . out . println ( head . data ) ; } static Node deleteK ( Node head_ref , int k ) { Node head = head_ref ; if ( head == null ) return null ; Node curr = head , prev = null ; while ( true ) { if ( curr . next == head && curr == head ) break ; printList ( head ) ; for ( int i = 0 ; i < k ; i ++ ) { prev = curr ; curr = curr . next ; } if ( curr == head ) { prev = head ; while ( prev . next != head ) prev = prev . next ; head = curr . next ; prev . next = head ; head_ref = head ; } else if ( curr . next == head ) { prev . next = head ; } else { prev . next = curr . next ; } } return head ; } static Node insertNode ( Node head_ref , int x ) { Node head = head_ref ; Node temp = new Node ( x ) ; if ( head == null ) { temp . next = temp ; head_ref = temp ; return head_ref ; } else { Node temp1 = head ; while ( temp1 . next != head ) temp1 = temp1 . next ; temp1 . next = temp ; temp . next = head ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = insertNode ( head , 1 ) ; head = insertNode ( head , 2 ) ; head = insertNode ( head , 3 ) ; head = insertNode ( head , 4 ) ; head = insertNode ( head , 5 ) ; head = insertNode ( head , 6 ) ; head = insertNode ( head , 7 ) ; head = insertNode ( head , 8 ) ; head = insertNode ( head , 9 ) ; int k = 4 ; head = deleteK ( head , k ) ; } }
class GFG { static class node { int data ; node next ; node prev ; } ; static node getNode ( ) { return new node ( ) ; } static int displayList ( node temp ) { node t = temp ; if ( temp == null ) return 0 ; else { System . out . println ( " The ▁ list ▁ is : ▁ " ) ; while ( temp . next != t ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . println ( temp . data ) ; return 1 ; } } static int countList ( node start ) { node temp = start ; int count = 0 ; while ( temp . next != start ) { temp = temp . next ; count ++ ; } count ++ ; return count ; } static node insertAtLocation ( node start , int data , int loc ) { node temp , newNode ; int i , count ; newNode = getNode ( ) ; temp = start ; count = countList ( start ) ; if ( temp == null count < loc ) return start ; else { newNode . data = data ; for ( i = 1 ; i < loc - 1 ; i ++ ) { temp = temp . next ; } newNode . next = temp . next ; ( temp . next ) . prev = newNode ; temp . next = newNode ; newNode . prev = temp ; return start ; } } static node createList ( int arr [ ] , int n , node start ) { node newNode , temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; start = insertAtLocation ( start , 8 , 3 ) ; displayList ( start ) ; } }
class GFG { static class Node { int data ; Node next , prev ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; return newNode ; } static Node insertEnd ( Node head , Node new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } Node last = ( head ) . prev ; new_node . next = head ; ( head ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } static Node reverse ( Node head ) { if ( head == null ) return null ; Node new_head = null ; Node last = head . prev ; Node curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } static void display ( Node head ) { if ( head == null ) return ; Node temp = head ; System . out . print ( " Forward ▁ direction : ▁ " ) ; while ( temp . next != head ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . print ( temp . data + " ▁ " ) ; Node last = head . prev ; temp = last ; System . out . print ( " Backward direction : "); while ( temp . prev != last ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . prev ; } System . out . print ( temp . data + " ▁ " ) ; } public static void main ( String args [ ] ) { Node head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; System . out . print ( "Current list:NEW_LINE"); display ( head ) ; head = reverse ( head ) ; System . out . print ( " Reversed list : "); display ( head ) ; } }
class GFG { static class node { int data ; node next ; node prev ; } ; static node getNode ( ) { return new node ( ) ; } static int displayList ( node temp ) { node t = temp ; if ( temp == null ) return 0 ; else { System . out . print ( " The ▁ list ▁ is : ▁ " ) ; while ( temp . next != t ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . print ( temp . data ) ; return 1 ; } } static node createList ( int arr [ ] , int n , node start ) { node newNode , temp ; int i ; for ( i = 0 ; i < n ; i ++ ) { newNode = getNode ( ) ; newNode . data = arr [ i ] ; if ( i == 0 ) { start = newNode ; newNode . prev = start ; newNode . next = start ; } else { temp = ( start ) . prev ; temp . next = newNode ; newNode . next = start ; newNode . prev = temp ; temp = start ; temp . prev = newNode ; } } return start ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; node start = null ; start = createList ( arr , n , start ) ; displayList ( start ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . next = null ; return node ; } static int alivesol ( int Num ) { if ( Num == 1 ) return 1 ; Node last = newNode ( 1 ) ; last . next = last ; for ( int i = 2 ; i <= Num ; i ++ ) { Node temp = newNode ( i ) ; temp . next = last . next ; last . next = temp ; last = temp ; } Node curr = last . next ; Node temp = new Node ( ) ; while ( curr . next != curr ) { temp = curr ; curr = curr . next ; temp . next = curr . next ; temp = temp . next ; curr = temp ; } int res = temp . data ; return res ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . println ( alivesol ( N ) ) ; } }
class Node { Node left = null ; Node right = null ; int data ; Node ( int data ) { this . data = data ; } } class GFG { public static int findDepth ( Node root ) { if ( root == null ) return 0 ; int left = findDepth ( root . left ) ; int right = findDepth ( root . right ) ; return 1 + Math . max ( left , right ) ; } public static Node DFS ( Node root , int curr , int depth ) { if ( root == null ) return null ; if ( curr == depth ) return root ; Node left = DFS ( root . left , curr + 1 , depth ) ; Node right = DFS ( root . right , curr + 1 , depth ) ; if ( left != null && right != null ) return root ; return ( left != null ) ? left : right ; } public static Node lcaOfDeepestLeaves ( Node root ) { if ( root == null ) return null ; int depth = findDepth ( root ) - 1 ; return DFS ( root , 0 , depth ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . right . left . left = new Node ( 8 ) ; root . right . left . right = new Node ( 9 ) ; System . out . println ( lcaOfDeepestLeaves ( root ) . data ) ; } }
class GFG { static int filter ( int x , int y , int z ) { if ( x != - 1 && y != - 1 ) { return z ; } return x == - 1 ? y : x ; } static int samePathUtil ( int mtrx [ ] [ ] , int vrtx , int v1 , int v2 , int i ) { int ans = - 1 ; if ( i == v1 i == v2 ) return i ; for ( int j = 0 ; j < vrtx ; j ++ ) { if ( mtrx [ i ] [ j ] == 1 ) { ans = filter ( ans , samePathUtil ( mtrx , vrtx , v1 , v2 , j ) , i ) ; } } return ans ; } static boolean isVertexAtSamePath ( int mtrx [ ] [ ] , int vrtx , int v1 , int v2 , int i ) { int lca = samePathUtil ( mtrx , vrtx , v1 - 1 , v2 - 1 , i ) ; if ( lca == v1 - 1 lca == v2 - 1 ) return true ; return false ; } public static void main ( String [ ] args ) { int vrtx = 7 ; int mtrx [ ] [ ] = { { 0 , 1 , 1 , 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 1 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 } } ; int v1 = 1 , v2 = 5 ; if ( isVertexAtSamePath ( mtrx , vrtx , v1 , v2 , 0 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static final int MAX = 1000 ; static final int log = 10 ; static int [ ] level = new int [ MAX ] ; static int [ ] [ ] lca = new int [ MAX ] [ log ] ; static int [ ] [ ] dist = new int [ MAX ] [ log ] ; @ SuppressWarnings ( " unchecked " ) static List < List < int [ ] > > graph = new ArrayList ( ) ; static void addEdge ( int u , int v , int cost ) { graph . get ( u ) . add ( new int [ ] { v , cost } ) ; graph . get ( v ) . add ( new int [ ] { u , cost } ) ; } static void dfs ( int node , int parent , int h , int cost ) { lca [ node ] [ 0 ] = parent ; level [ node ] = h ; if ( parent != - 1 ) { dist [ node ] [ 0 ] = cost ; } for ( int i = 1 ; i < log ; i ++ ) { if ( lca [ node ] [ i - 1 ] != - 1 ) { lca [ node ] [ i ] = lca [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; dist [ node ] [ i ] = dist [ node ] [ i - 1 ] + dist [ lca [ node ] [ i - 1 ] ] [ i - 1 ] ; } } for ( int [ ] i : graph . get ( node ) ) { if ( i [ 0 ] == parent ) continue ; dfs ( i [ 0 ] , node , h + 1 , i [ 1 ] ) ; } } static void findDistance ( int u , int v ) { int ans = 0 ; if ( level [ u ] > level [ v ] ) { int temp = u ; u = v ; v = temp ; } for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != - 1 && level [ lca [ v ] [ i ] ] >= level [ u ] ) { ans += dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; } } if ( v == u ) { System . out . println ( ans ) ; } else { for ( int i = log - 1 ; i >= 0 ; i -- ) { if ( lca [ v ] [ i ] != lca [ u ] [ i ] ) { ans += dist [ u ] [ i ] + dist [ v ] [ i ] ; v = lca [ v ] [ i ] ; u = lca [ u ] [ i ] ; } } ans += dist [ u ] [ 0 ] + dist [ v ] [ 0 ] ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < MAX ; i ++ ) { graph . add ( new ArrayList < int [ ] > ( ) ) ; } addEdge ( 1 , 2 , 2 ) ; addEdge ( 1 , 3 , 3 ) ; addEdge ( 2 , 4 , 5 ) ; addEdge ( 2 , 5 , 7 ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < log ; j ++ ) { lca [ i ] [ j ] = - 1 ; dist [ i ] [ j ] = 0 ; } } dfs ( 1 , - 1 , 0 , 0 ) ; findDistance ( 1 , 3 ) ; findDistance ( 2 , 3 ) ; findDistance ( 3 , 5 ) ; } }
import java . util . * ; class GFG { static int T = 1 ; static void dfs ( int node , int parent , Vector < Integer > g [ ] , int level [ ] , int t_in [ ] , int t_out [ ] ) { if ( parent == - 1 ) { level [ node ] = 1 ; } else { level [ node ] = level [ parent ] + 1 ; } t_in [ node ] = T ; for ( int i : g [ node ] ) { if ( i != parent ) { T ++ ; dfs ( i , node , g , level , t_in , t_out ) ; } } T ++ ; t_out [ node ] = T ; } static int findLCA ( int n , Vector < Integer > g [ ] , Vector < Integer > v ) { int [ ] level = new int [ n + 1 ] ; int [ ] t_in = new int [ n + 1 ] ; int [ ] t_out = new int [ n + 1 ] ; dfs ( 1 , - 1 , g , level , t_in , t_out ) ; int mint = Integer . MAX_VALUE , maxt = Integer . MIN_VALUE ; int minv = - 1 , maxv = - 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( t_in [ v . get ( i ) ] < mint ) { mint = t_in [ v . get ( i ) ] ; minv = v . get ( i ) ; } if ( t_out [ v . get ( i ) ] > maxt ) { maxt = t_out [ v . get ( i ) ] ; maxv = v . get ( i ) ; } } if ( minv == maxv ) { return minv ; } int lev = Math . min ( level [ minv ] , level [ maxv ] ) ; int node = 0 , l = Integer . MIN_VALUE ; for ( int i = 1 ; i <= n ; i ++ ) { if ( level [ i ] > lev ) continue ; if ( t_in [ i ] <= mint && t_out [ i ] >= maxt && level [ i ] > l ) { node = i ; l = level [ i ] ; } } return node ; } public static void main ( String [ ] args ) { int n = 10 ; Vector < Integer > [ ] g = new Vector [ n + 1 ] ; for ( int i = 0 ; i < g . length ; i ++ ) g [ i ] = new Vector < Integer > ( ) ; g [ 1 ] . add ( 2 ) ; g [ 2 ] . add ( 1 ) ; g [ 1 ] . add ( 3 ) ; g [ 3 ] . add ( 1 ) ; g [ 1 ] . add ( 4 ) ; g [ 4 ] . add ( 1 ) ; g [ 2 ] . add ( 5 ) ; g [ 5 ] . add ( 2 ) ; g [ 2 ] . add ( 6 ) ; g [ 6 ] . add ( 2 ) ; g [ 3 ] . add ( 7 ) ; g [ 7 ] . add ( 3 ) ; g [ 4 ] . add ( 10 ) ; g [ 10 ] . add ( 4 ) ; g [ 8 ] . add ( 7 ) ; g [ 7 ] . add ( 8 ) ; g [ 9 ] . add ( 7 ) ; g [ 7 ] . add ( 9 ) ; Vector < Integer > v = new Vector < > ( ) ; v . add ( 7 ) ; v . add ( 3 ) ; v . add ( 8 ) ; System . out . print ( findLCA ( n , g , v ) + "NEW_LINE"); } }
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static int MAX_SIZE = 100005 , MAX_CHAR = 26 ; static int [ ] [ ] nodeCharactersCount = new int [ MAX_SIZE ] [ MAX_CHAR ] ; static Vector < Integer > [ ] tree = new Vector [ MAX_SIZE ] ; static boolean canFormPalindrome ( int [ ] charArray ) { int oddCount = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( charArray [ i ] % 2 == 1 ) oddCount ++ ; } if ( oddCount >= 2 ) return false ; else return true ; } static int LCA ( int currentNode , int x , int y ) { if ( currentNode == x ) return x ; if ( currentNode == y ) return y ; int xLca , yLca ; xLca = yLca = - 1 ; int gotLca = - 1 ; for ( int l = 0 ; l < tree [ currentNode ] . size ( ) ; l ++ ) { int nextNode = tree [ currentNode ] . elementAt ( l ) ; int out_ = LCA ( nextNode , x , y ) ; if ( out_ == x ) xLca = out_ ; if ( out_ == y ) yLca = out_ ; if ( xLca != - 1 && yLca != - 1 ) return currentNode ; if ( out_ != - 1 ) gotLca = out_ ; } return gotLca ; } static void buildTree ( int i ) { for ( int l = 0 ; l < tree [ i ] . size ( ) ; l ++ ) { int nextNode = tree [ i ] . elementAt ( l ) ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) { nodeCharactersCount [ nextNode ] [ j ] += nodeCharactersCount [ i ] [ j ] ; } buildTree ( nextNode ) ; } } static boolean canFormPalindromicPath ( int x , int y ) { int lcaNode ; if ( x == y ) lcaNode = x ; else lcaNode = LCA ( 1 , x , y ) ; int [ ] charactersCountFromXtoY = new int [ MAX_CHAR ] ; Arrays . fill ( charactersCountFromXtoY , 0 ) ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { charactersCountFromXtoY [ i ] = nodeCharactersCount [ x ] [ i ] + nodeCharactersCount [ y ] [ i ] - 2 * nodeCharactersCount [ lcaNode ] [ i ] ; } if ( canFormPalindrome ( charactersCountFromXtoY ) ) return true ; return false ; } static void updateNodeCharactersCount ( String str , int v ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) nodeCharactersCount [ v ] [ str . charAt ( i ) - ' a ' ] ++ ; } static void performQueries ( int [ ] [ ] queries , int q ) { int i = 0 ; while ( i < q ) { int x = queries [ i ] [ 0 ] ; int y = queries [ i ] [ 1 ] ; if ( canFormPalindromicPath ( x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; i ++ ; } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAX_SIZE ; i ++ ) { for ( int j = 0 ; j < MAX_CHAR ; j ++ ) { nodeCharactersCount [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < MAX_SIZE ; i ++ ) { tree [ i ] = new Vector < > ( ) ; } tree [ 1 ] . add ( 2 ) ; updateNodeCharactersCount ( " bbc " , 2 ) ; tree [ 1 ] . add ( 3 ) ; updateNodeCharactersCount ( " ac " , 3 ) ; buildTree ( 1 ) ; int [ ] [ ] queries = { { 1 , 2 } , { 2 , 3 } , { 3 , 1 } , { 3 , 3 } } ; int q = queries . length ; performQueries ( queries , q ) ; } }
import java . util . * ; class solution { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static boolean getPath ( Node root , Vector < Integer > arr , int x ) { if ( root == null ) return false ; arr . add ( root . data ) ; if ( root . data == x ) return true ; if ( getPath ( root . left , arr , x ) || getPath ( root . right , arr , x ) ) return true ; arr . remove ( arr . size ( ) - 1 ) ; return false ; } static int sumOddNodes ( Node root , int n1 , int n2 ) { Vector < Integer > path1 = new Vector < Integer > ( ) ; Vector < Integer > path2 = new Vector < Integer > ( ) ; getPath ( root , path1 , n1 ) ; getPath ( root , path2 , n2 ) ; int intersection = - 1 ; int i = 0 , j = 0 ; while ( i != path1 . size ( ) || j != path2 . size ( ) ) { if ( i == j && path1 . get ( i ) == path2 . get ( j ) ) { i ++ ; j ++ ; } else { intersection = j - 1 ; break ; } } int sum = 0 ; for ( i = path1 . size ( ) - 1 ; i > intersection ; i -- ) if ( path1 . get ( i ) % 2 != 0 ) sum += path1 . get ( i ) ; for ( i = intersection ; i < path2 . size ( ) ; i ++ ) if ( path2 . get ( i ) % 2 != 0 ) sum += path2 . get ( i ) ; return sum ; } 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 ) ; int node1 = 5 ; int node2 = 6 ; System . out . print ( sumOddNodes ( root , node1 , node2 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 100001 ; static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; this . left = this . right = null ; } } static class Pair < T , V > { T first ; V second ; Pair ( ) { } Pair ( T first , V second ) { this . first = first ; this . second = second ; } } static int [ ] level = new int [ MAX ] ; static void findLevels ( Node root ) { if ( root == null ) return ; Queue < Pair < Node , Integer > > q = new LinkedList < > ( ) ; q . add ( new Pair < Node , Integer > ( root , 0 ) ) ; Pair < Node , Integer > p = new Pair < Node , Integer > ( ) ; while ( ! q . isEmpty ( ) ) { p = q . poll ( ) ; level [ p . first . data ] = p . second ; if ( p . first . left != null ) q . add ( new Pair < Node , Integer > ( p . first . left , p . second + 1 ) ) ; if ( p . first . right != null ) q . add ( new Pair < Node , Integer > ( p . first . right , p . second + 1 ) ) ; } } static int [ ] Euler = new int [ MAX ] ; static int idx = 0 ; static void eulerTree ( Node root ) { Euler [ ++ idx ] = root . data ; if ( root . left != null ) { eulerTree ( root . left ) ; Euler [ ++ idx ] = root . data ; } if ( root . right != null ) { eulerTree ( root . right ) ; Euler [ ++ idx ] = root . data ; } } static int [ ] vis = new int [ MAX ] ; static int [ ] L = new int [ MAX ] ; static int [ ] H = new int [ MAX ] ; static void preprocessEuler ( int size ) { for ( int i = 1 ; i <= size ; i ++ ) { L [ i ] = level [ Euler [ i ] ] ; if ( vis [ Euler [ i ] ] == 0 ) { H [ Euler [ i ] ] = i ; vis [ Euler [ i ] ] = 1 ; } } } @ SuppressWarnings ( " unchecked " ) static Pair < Integer , Integer > [ ] seg = ( Pair < Integer , Integer > [ ] ) new Pair [ 4 * MAX ] ; static Pair < Integer , Integer > min ( Pair < Integer , Integer > a , Pair < Integer , Integer > b ) { if ( a . first <= b . first ) return a ; return b ; } static Pair < Integer , Integer > buildSegTree ( int low , int high , int pos ) { if ( low == high ) { seg [ pos ] . first = L [ low ] ; seg [ pos ] . second = low ; return seg [ pos ] ; } int mid = low + ( high - low ) / 2 ; buildSegTree ( low , mid , 2 * pos ) ; buildSegTree ( mid + 1 , high , 2 * pos + 1 ) ; seg [ pos ] = min ( seg [ 2 * pos ] , seg [ 2 * pos + 1 ] ) ; return seg [ pos ] ; } static Pair < Integer , Integer > LCA ( int qlow , int qhigh , int low , int high , int pos ) { if ( qlow <= low && qhigh >= high ) return seg [ pos ] ; if ( qlow > high qhigh < low ) return new Pair < Integer , Integer > ( Integer . MAX_VALUE , 0 ) ; int mid = low + ( high - low ) / 2 ; return min ( LCA ( qlow , qhigh , low , mid , 2 * pos ) , LCA ( qlow , qhigh , mid + 1 , high , 2 * pos + 1 ) ) ; } static int findDistance ( int n1 , int n2 , int size ) { int prevn1 = n1 , prevn2 = n2 ; n1 = H [ n1 ] ; n2 = H [ n2 ] ; if ( n2 < n1 ) { int temp = n1 ; n1 = n2 ; n2 = temp ; } int lca = LCA ( n1 , n2 , 1 , size , 1 ) . second ; lca = Euler [ lca ] ; return level [ prevn1 ] + level [ prevn2 ] - 2 * level [ lca ] ; } static void preProcessing ( Node root , int N ) { for ( int i = 0 ; i < 4 * MAX ; i ++ ) { seg [ i ] = new Pair < > ( ) ; } eulerTree ( root ) ; findLevels ( root ) ; preprocessEuler ( 2 * N - 1 ) ; buildSegTree ( 1 , 2 * N - 1 , 1 ) ; } public static void main ( String [ ] args ) { int N = 8 ; Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . right . left . right = new Node ( 8 ) ; preProcessing ( root , N ) ; System . out . println ( " Dist ( 4 , ▁ 5 ) ▁ = ▁ " + findDistance ( 4 , 5 , 2 * N - 1 ) ) ; System . out . println ( " Dist ( 4 , ▁ 6 ) ▁ = ▁ " + findDistance ( 4 , 6 , 2 * N - 1 ) ) ; System . out . println ( " Dist ( 3 , ▁ 4 ) ▁ = ▁ " + findDistance ( 3 , 4 , 2 * N - 1 ) ) ; System . out . println ( " Dist ( 2 , ▁ 4 ) ▁ = ▁ " + findDistance ( 2 , 4 , 2 * N - 1 ) ) ; System . out . println ( " Dist ( 8 , ▁ 5 ) ▁ = ▁ " + findDistance ( 8 , 5 , 2 * N - 1 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAXN = 1001 ; static int [ ] depth = new int [ MAXN ] ; static int [ ] parent = new int [ MAXN ] ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] adj = new Vector [ MAXN ] ; static { for ( int i = 0 ; i < MAXN ; i ++ ) adj [ i ] = new Vector < > ( ) ; } static void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } static void dfs ( int cur , int prev ) { parent [ cur ] = prev ; depth [ cur ] = depth [ prev ] + 1 ; for ( int i = 0 ; i < adj [ cur ] . size ( ) ; i ++ ) if ( adj [ cur ] . elementAt ( i ) != prev ) dfs ( adj [ cur ] . elementAt ( i ) , cur ) ; } static void preprocess ( ) { depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } static int LCANaive ( int u , int v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { int temp = u ; u = v ; v = temp ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } public static void main ( String [ ] args ) { addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; preprocess ( ) ; System . out . println ( " LCA ( 11,8 ) ▁ : ▁ " + LCANaive ( 11 , 8 ) ) ; System . out . println ( " LCA ( 3,13 ) ▁ : ▁ " + LCANaive ( 3 , 13 ) ) ; } }
import java . util . * ; class GFG { static final int MAXN = 1001 ; static int block_sz ; static int [ ] depth = new int [ MAXN ] ; static int [ ] parent = new int [ MAXN ] ; static int [ ] jump_parent = new int [ MAXN ] ; static Vector < Integer > [ ] adj = new Vector [ MAXN ] ; static void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } static int LCANaive ( int u , int v ) { if ( u == v ) return u ; if ( depth [ u ] > depth [ v ] ) { int t = u ; u = v ; v = t ; } v = parent [ v ] ; return LCANaive ( u , v ) ; } static void dfs ( int cur , int prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] = prev ; if ( depth [ cur ] % block_sz == 0 ) jump_parent [ cur ] = parent [ cur ] ; else jump_parent [ cur ] = jump_parent [ prev ] ; for ( int i = 0 ; i < adj [ cur ] . size ( ) ; ++ i ) if ( adj [ cur ] . get ( i ) != prev ) dfs ( adj [ cur ] . get ( i ) , cur ) ; } static int LCASQRT ( int u , int v ) { while ( jump_parent [ u ] != jump_parent [ v ] ) { if ( depth [ u ] > depth [ v ] ) { int t = u ; u = v ; v = t ; } v = jump_parent [ v ] ; } return LCANaive ( u , v ) ; } static void preprocess ( int height ) { block_sz = ( int ) Math . sqrt ( height ) ; depth [ 0 ] = - 1 ; dfs ( 1 , 0 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 1 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 4 , 8 ) ; addEdge ( 4 , 9 ) ; addEdge ( 9 , 10 ) ; addEdge ( 9 , 11 ) ; addEdge ( 7 , 12 ) ; addEdge ( 7 , 13 ) ; int height = 4 ; preprocess ( height ) ; System . out . print ( " LCA ( 11,8 ) ▁ : ▁ " + LCASQRT ( 11 , 8 ) + "NEW_LINE"); System . out . print ( " LCA ( 3,13 ) ▁ : ▁ " + LCASQRT ( 3 , 13 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static final int MAXN = 100000 ; static final int level = 18 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] tree = new Vector [ MAXN ] ; static int [ ] depth = new int [ MAXN ] ; static int [ ] [ ] parent = new int [ MAXN ] [ level ] ; static void dfs ( int cur , int prev ) { depth [ cur ] = depth [ prev ] + 1 ; parent [ cur ] [ 0 ] = prev ; for ( int i = 0 ; i < tree [ cur ] . size ( ) ; i ++ ) { if ( tree [ cur ] . get ( i ) != prev ) dfs ( tree [ cur ] . get ( i ) , cur ) ; } } static void precomputeSparseMatrix ( int n ) { for ( int i = 1 ; i < level ; i ++ ) { for ( int node = 1 ; node <= n ; node ++ ) { if ( parent [ node ] [ i - 1 ] != - 1 ) parent [ node ] [ i ] = parent [ parent [ node ] [ i - 1 ] ] [ i - 1 ] ; } } } static int lca ( int u , int v ) { if ( depth [ v ] < depth [ u ] ) { u = u + v ; v = u - v ; u = u - v ; } int diff = depth [ v ] - depth [ u ] ; for ( int i = 0 ; i < level ; i ++ ) if ( ( ( diff >> i ) & 1 ) == 1 ) v = parent [ v ] [ i ] ; if ( u == v ) return u ; for ( int i = level - 1 ; i >= 0 ; i -- ) if ( parent [ u ] [ i ] != parent [ v ] [ i ] ) { u = parent [ u ] [ i ] ; v = parent [ v ] [ i ] ; } return parent [ u ] [ 0 ] ; } static void addEdge ( int u , int v ) { tree [ u ] . add ( v ) ; tree [ v ] . add ( u ) ; } static void memset ( int value ) { for ( int i = 0 ; i < MAXN ; i ++ ) { for ( int j = 0 ; j < level ; j ++ ) { parent [ i ] [ j ] = - 1 ; } } } public static void main ( String [ ] args ) { memset ( - 1 ) ; for ( int i = 0 ; i < MAXN ; i ++ ) tree [ i ] = new Vector < Integer > ( ) ; int n = 8 ; addEdge ( 1 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 2 , 4 ) ; addEdge ( 2 , 5 ) ; addEdge ( 2 , 6 ) ; addEdge ( 3 , 7 ) ; addEdge ( 3 , 8 ) ; depth [ 0 ] = 0 ; dfs ( 1 , 0 ) ; precomputeSparseMatrix ( n ) ; System . out . print ( " LCA ( 4 , ▁ 7 ) ▁ = ▁ " + lca ( 4 , 7 ) + "NEW_LINE"); System . out . print ( " LCA ( 4 , ▁ 6 ) ▁ = ▁ " + lca ( 4 , 6 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static class Query { int L , R ; Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static int LCE ( String str , int n , int L , int R ) { int length = 0 ; while ( R + length < n && str . charAt ( L + length ) == str . charAt ( R + length ) ) length ++ ; return ( length ) ; } static void LCEQueries ( String str , int n , Query q [ ] , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L ; int R = q [ i ] . R ; System . out . printf ( "LCE (%d, %d) = %dNEW_LINE", L, R, LCE ( str , n , L , R ) ) ; } return ; } public static void main ( String [ ] args ) { String str = " abbababba " ; int n = str . length ( ) ; Query q [ ] = new Query [ 3 ] ; q [ 0 ] = new Query ( 1 , 2 ) ; q [ 1 ] = new Query ( 1 , 6 ) ; q [ 2 ] = new Query ( 0 , 5 ) ; int m = q . length ; LCEQueries ( str , n , q , m ) ; } }
import java . util . Arrays ; class GFG { static final int V = 5 ; static final int WHITE = 1 ; static final int BLACK = 2 ; static class Node { int data ; Node left , right ; } ; static class subset { int parent ; int rank ; int ancestor ; int child ; int sibling ; int color ; } ; static class Query { int L , R ; Query ( int L , int R ) { this . L = L ; this . R = R ; } } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static void makeSet ( subset subsets [ ] , int i ) { if ( i < 1 i > V ) return ; subsets [ i ] . color = WHITE ; subsets [ i ] . parent = i ; subsets [ i ] . rank = 0 ; return ; } static int findSet ( subset subsets [ ] , int i ) { if ( subsets [ i ] . parent != i ) subsets [ i ] . parent = findSet ( subsets , subsets [ i ] . parent ) ; return subsets [ i ] . parent ; } static void unionSet ( subset subsets [ ] , int x , int y ) { int xroot = findSet ( subsets , x ) ; int yroot = findSet ( subsets , y ) ; if ( subsets [ xroot ] . rank < subsets [ yroot ] . rank ) subsets [ xroot ] . parent = yroot ; else if ( subsets [ xroot ] . rank > subsets [ yroot ] . rank ) subsets [ yroot ] . parent = xroot ; else { subsets [ yroot ] . parent = xroot ; ( subsets [ xroot ] . rank ) ++ ; } } static void lcaWalk ( int u , Query q [ ] , int m , subset subsets [ ] ) { makeSet ( subsets , u ) ; subsets [ findSet ( subsets , u ) ] . ancestor = u ; int child = subsets [ u ] . child ; while ( child != 0 ) { lcaWalk ( child , q , m , subsets ) ; unionSet ( subsets , u , child ) ; subsets [ findSet ( subsets , u ) ] . ancestor = u ; child = subsets [ child ] . sibling ; } subsets [ u ] . color = BLACK ; for ( int i = 0 ; i < m ; i ++ ) { if ( q [ i ] . L == u ) { if ( subsets [ q [ i ] . R ] . color == BLACK ) { System . out . printf ( "LCA(%d %d)->%dNEW_LINE", q [ i ] . L , q [ i ] . R , subsets [ findSet ( subsets , q [ i ] . R ) ] . ancestor ) ; } } else if ( q [ i ] . R == u ) { if ( subsets [ q [ i ] . L ] . color == BLACK ) { System . out . printf ( "LCA(%d %d)->%dNEW_LINE", q [ i ] . L , q [ i ] . R , subsets [ findSet ( subsets , q [ i ] . L ) ] . ancestor ) ; } } } return ; } static void preprocess ( Node node , subset subsets [ ] ) { if ( node == null ) return ; preprocess ( node . left , subsets ) ; if ( node . left != null && node . right != null ) { subsets [ node . data ] . child = node . left . data ; subsets [ node . left . data ] . sibling = node . right . data ; } else if ( ( node . left != null && node . right == null ) || ( node . left == null && node . right != null ) ) { if ( node . left != null && node . right == null ) subsets [ node . data ] . child = node . left . data ; else subsets [ node . data ] . child = node . right . data ; } preprocess ( node . right , subsets ) ; } static void initialise ( subset subsets [ ] ) { for ( int i = 1 ; i < subsets . length ; i ++ ) { subsets [ i ] = new subset ( ) ; subsets [ i ] . color = WHITE ; } return ; } static void printLCAs ( Node root , Query q [ ] , int m ) { subset [ ] subsets = new subset [ V + 1 ] ; initialise ( subsets ) ; preprocess ( root , subsets ) ; lcaWalk ( root . data , q , m , subsets ) ; } 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 ) ; Query q [ ] = new Query [ 3 ] ; q [ 0 ] = new Query ( 5 , 4 ) ; q [ 1 ] = new Query ( 1 , 3 ) ; q [ 2 ] = new Query ( 2 , 3 ) ; int m = q . length ; printLCAs ( root , q , m ) ; } }
import java . util . * ; class GFG { static final int N = 1024 ; static int edge_counted ; static int curr_chain ; static int [ ] [ ] tree = new int [ N ] [ N ] ; static class treeNode { int par ; int depth ; int size ; int pos_segbase ; int chain ; } ; static treeNode node [ ] = new treeNode [ N ] ; static class Edge { int weight ; int deeper_end ; Edge ( int weight , int deeper_end ) { this . weight = weight ; this . deeper_end = deeper_end ; } } ; static Edge edge [ ] = new Edge [ N ] ; static class segmentTree { int [ ] base_array = new int [ N ] ; int [ ] tree = new int [ 6 * N ] ; } ; static segmentTree s = new segmentTree ( ) ; static void addEdge ( int e , int u , int v , int w ) { tree [ u - 1 ] [ v - 1 ] = e - 1 ; tree [ v - 1 ] [ u - 1 ] = e - 1 ; edge [ e - 1 ] . weight = w ; } static void dfs ( int curr , int prev , int dep , int n ) { node [ curr ] . par = prev ; node [ curr ] . depth = dep ; node [ curr ] . size = 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != curr && j != node [ curr ] . par && tree [ curr ] [ j ] != - 1 ) { edge [ tree [ curr ] [ j ] ] . deeper_end = j ; dfs ( j , curr , dep + 1 , n ) ; node [ curr ] . size += node [ j ] . size ; } } } static void hld ( int curr_node , int id , int n , int chain_heads [ ] ) { if ( chain_heads [ curr_chain ] == - 1 ) chain_heads [ curr_chain ] = curr_node ; node [ curr_node ] . chain = curr_chain ; node [ curr_node ] . pos_segbase = edge_counted ; s . base_array [ ( edge_counted ) ++ ] = edge [ id ] . weight ; int spcl_chld = - 1 , spcl_edg_id = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( j != curr_node && j != node [ curr_node ] . par && tree [ curr_node ] [ j ] != - 1 ) if ( spcl_chld == - 1 node [ spcl_chld ] . size < node [ j ] . size ) { spcl_chld = j ; spcl_edg_id = tree [ curr_node ] [ j ] ; } if ( spcl_chld != - 1 ) hld ( spcl_chld , spcl_edg_id , n , chain_heads ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != curr_node && j != node [ curr_node ] . par && j != spcl_chld && tree [ curr_node ] [ j ] != - 1 ) { ( curr_chain ) ++ ; hld ( j , tree [ curr_node ] [ j ] , n , chain_heads ) ; } } } static int construct_ST ( int ss , int se , int si ) { if ( ss == se - 1 ) { s . tree [ si ] = s . base_array [ ss ] ; return s . base_array [ ss ] ; } int mid = ( ss + se ) / 2 ; s . tree [ si ] = Math . max ( construct_ST ( ss , mid , si * 2 ) , construct_ST ( mid , se , si * 2 + 1 ) ) ; return s . tree [ si ] ; } static int update_ST ( int ss , int se , int si , int x , int val ) { if ( ss > x se <= x ) ; else if ( ss == x && ss == se - 1 ) s . tree [ si ] = val ; else { int mid = ( ss + se ) / 2 ; s . tree [ si ] = Math . max ( update_ST ( ss , mid , si * 2 , x , val ) , update_ST ( mid , se , si * 2 + 1 , x , val ) ) ; } return s . tree [ si ] ; } static void change ( int e , int val , int n ) { update_ST ( 0 , n , 1 , node [ edge [ e ] . deeper_end ] . pos_segbase , val ) ; } static int LCA ( int u , int v , int n ) { int [ ] LCA_aux = new int [ n + 5 ] ; if ( node [ u ] . depth < node [ v ] . depth ) { int t = u ; u = v ; v = t ; } Arrays . fill ( LCA_aux , - 1 ) ; while ( u != - 1 ) { LCA_aux [ u ] = 1 ; u = node [ u ] . par ; } while ( v > 0 ) { if ( LCA_aux [ v ] == 1 ) break ; v = node [ v ] . par ; } return v ; } static int RMQUtil ( int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se - 1 ) return s . tree [ index ] ; if ( se - 1 < qs ss > qe ) return - 1 ; int mid = ( ss + se ) / 2 ; return Math . max ( RMQUtil ( ss , mid , qs , qe , 2 * index ) , RMQUtil ( mid , se , qs , qe , 2 * index + 1 ) ) ; } static int RMQ ( int qs , int qe , int n ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . printf ( " Invalid ▁ Input " ) ; return - 1 ; } return RMQUtil ( 0 , n , qs , qe , 1 ) ; } static int crawl_tree ( int u , int v , int n , int chain_heads [ ] ) { int chain_u , chain_v = node [ v ] . chain , ans = 0 ; while ( true ) { chain_u = node [ u ] . chain ; if ( chain_u == chain_v ) { if ( u == v ) ; else ans = Math . max ( RMQ ( node [ v ] . pos_segbase + 1 , node [ u ] . pos_segbase , n ) , ans ) ; break ; } else { ans = Math . max ( ans , RMQ ( node [ chain_heads [ chain_u ] ] . pos_segbase , node [ u ] . pos_segbase , n ) ) ; u = node [ chain_heads [ chain_u ] ] . par ; } } return ans ; } static void maxEdge ( int u , int v , int n , int chain_heads [ ] ) { int lca = LCA ( u , v , n ) ; int ans = Math . max ( crawl_tree ( u , lca , n , chain_heads ) , crawl_tree ( v , lca , n , chain_heads ) ) ; System . out . printf ( "%dNEW_LINE", ans); } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { tree [ i ] [ j ] = - 1 ; } } int n = 11 ; for ( int i = 0 ; i < edge . length ; i ++ ) { edge [ i ] = new Edge ( 0 , 0 ) ; } for ( int i = 0 ; i < node . length ; i ++ ) { node [ i ] = new treeNode ( ) ; } addEdge ( 1 , 1 , 2 , 13 ) ; addEdge ( 2 , 1 , 3 , 9 ) ; addEdge ( 3 , 1 , 4 , 23 ) ; addEdge ( 4 , 2 , 5 , 4 ) ; addEdge ( 5 , 2 , 6 , 25 ) ; addEdge ( 6 , 3 , 7 , 29 ) ; addEdge ( 7 , 6 , 8 , 5 ) ; addEdge ( 8 , 7 , 9 , 30 ) ; addEdge ( 9 , 8 , 10 , 1 ) ; addEdge ( 10 , 8 , 11 , 6 ) ; int root = 0 , parent_of_root = - 1 , depth_of_root = 0 ; dfs ( root , parent_of_root , depth_of_root , n ) ; int [ ] chain_heads = new int [ N ] ; Arrays . fill ( chain_heads , - 1 ) ; edge_counted = 0 ; curr_chain = 0 ; hld ( root , n - 1 , n , chain_heads ) ; construct_ST ( 0 , edge_counted , 1 ) ; int u = 11 , v = 9 ; System . out . print ( " Max ▁ edge ▁ between ▁ " + u + " ▁ and ▁ " + v + " ▁ is ▁ " ) ; maxEdge ( u - 1 , v - 1 , n , chain_heads ) ; change ( 8 - 1 , 28 , n ) ; System . out . print ( " After ▁ Change : ▁ max ▁ edge ▁ between ▁ " + u + " ▁ and ▁ " + v + " ▁ is ▁ " ) ; maxEdge ( u - 1 , v - 1 , n , chain_heads ) ; v = 4 ; System . out . print ( " Max ▁ edge ▁ between ▁ " + u + " ▁ and ▁ " + v + " ▁ is ▁ " ) ; maxEdge ( u - 1 , v - 1 , n , chain_heads ) ; change ( 5 - 1 , 22 , n ) ; System . out . print ( " After ▁ Change : ▁ max ▁ edge ▁ between ▁ " + u + " ▁ and ▁ " + v + " ▁ is ▁ " ) ; maxEdge ( u - 1 , v - 1 , n , chain_heads ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static boolean findPair ( int a [ ] , int n , int z ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( i != j && a [ i ] + a [ j ] == z ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , - 2 , 1 , 0 , 5 } ; int z = 0 ; int n = a . length ; if ( findPair ( a , n , z ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void print ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i != j ) System . out . println ( a [ i ] + " ▁ " + a [ j ] ) ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; print ( a , n ) ; } }
import java . io . * ; class GFG { public static int maximumSum ( int arr [ ] , int N ) { int Sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , N ) ) ; } }
class GFG { public static void main ( String [ ] args ) { int a = 0 , b = 0 ; int N = 4 , M = 4 ; for ( int i = 0 ; i < N ; i ++ ) { a = a + 10 ; } for ( int i = 0 ; i < M ; i ++ ) { b = b + 40 ; } System . out . print ( a + " ▁ " + b ) ; } }
class GFG { static void stoogesort ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) { int temp = arr [ l ] ; arr [ l ] = arr [ h ] ; arr [ h ] = temp ; } if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 3 , 1 } ; int N = arr . length ; stoogesort ( arr , 0 , N - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
class GFG { static int Fibonacci ( int N ) { if ( N < 2 ) return N ; return Fibonacci ( N - 1 ) + Fibonacci ( N - 2 ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( Fibonacci ( N ) ) ; } }
class GFG { static void findWinner ( int n , int m ) { int X = 0 ; int Y = 0 ; if ( n >= m ) { X = n - 1 ; Y = m ; } else { X = m - 1 ; Y = n ; } if ( X > Y ) System . out . print ( " X " ) ; else if ( Y > X ) System . out . print ( " Y " ) ; else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; findWinner ( n , m ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static void countSubarrays ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( ( j - i ) % 2 == 0 ) sum += arr [ j ] ; else sum -= arr [ j ] ; if ( sum == 0 ) count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 4 , 2 } ; int n = arr . length ; countSubarrays ( arr , n ) ; } }
import java . util . Scanner ; public class P2Placerooks { static void findCountRooks ( int row [ ] , int col [ ] , int n , int k ) { int res = n - k ; System . out . println ( res + " ▁ " ) ; int ri = 0 , ci = 0 ; while ( res -- > 0 ) { while ( ri < k && row [ ri ] == 1 ) { ri ++ ; } while ( ci < k && col [ ci ] == 1 ) { ci ++ ; } System . out . println ( ( ri + 1 ) + " ▁ " + ( ci + 1 ) + " ▁ " ) ; ri ++ ; ci ++ ; } } public static void main ( String [ ] args ) { int n = 4 ; int k = 2 ; int row [ ] = { 1 , 2 } ; int col [ ] = { 4 , 2 } ; findCountRooks ( row , col , n , k ) ; } }
class GFG { static boolean check_order ( int [ ] arr ) { int cnt = 0 ; for ( int i = 0 ; i < arr . length - 1 ; i ++ ) { if ( Math . abs ( arr [ i + 1 ] - arr [ i ] ) > 1 ) cnt ++ ; } if ( Math . abs ( arr [ 0 ] - arr [ arr . length - 1 ] ) > 1 ) cnt ++ ; if ( cnt > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 , 5 , 1 } ; if ( check_order ( arr ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . util . * ; class GFG { static int getM ( int n ) { if ( n == 1 ) return - 1 ; else return n - 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( getM ( n ) ) ; } }
class GFG { static int maxProfit ( int prices [ ] , int n ) { if ( n <= 1 ) return 0 ; int rest [ ] = new int [ n ] ; int hold [ ] = new int [ 9 ] ; int sold [ ] = new int [ 9 ] ; rest [ 0 ] = 0 ; hold [ 0 ] = - prices [ 0 ] ; sold [ 0 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { rest [ i ] = Math . max ( rest [ i - 1 ] , sold [ i - 1 ] ) ; hold [ i ] = Math . max ( hold [ i - 1 ] , rest [ i - 1 ] - prices [ i ] ) ; sold [ i ] = hold [ i - 1 ] + prices [ i ] ; } return Math . max ( rest [ n - 1 ] , sold [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int price [ ] = { 2 , 4 , 5 , 0 , 2 } ; int n = price . length ; System . out . print ( maxProfit ( price , n ) + "NEW_LINE"); } }
class GFG { static int findSubarrays ( int arr [ ] , int N , int K ) { return N - K + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 2 , 17 , 18 } ; int K = 4 ; int N = arr . length ; System . out . print ( findSubarrays ( arr , N , K ) ) ; } }
class GFG { static int [ ] factorial = { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; public static boolean isStrong ( int N ) { String num = Integer . toString ( N ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { sum += factorial [ Integer . parseInt ( num . charAt ( i ) + " " ) ] ; } return sum == N ; } public static void printStrongNumbers ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( isStrong ( i ) ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) throws java . lang . Exception { int N = 1000 ; printStrongNumbers ( N ) ; } }
import java . util . * ; class GFG { static int countPairs ( int A , int B ) { int countY = 0 , countX = ( B - A ) + 1 , next_val = 9 ; while ( next_val <= B ) { if ( next_val >= A ) { countY += 1 ; } next_val = next_val * 10 + 9 ; } return ( countX * countY ) ; } public static void main ( String args [ ] ) { int A = 1 ; int B = 16 ; System . out . print ( countPairs ( A , B ) ) ; } }
import java . util . * ; class GFG { static int getXOR ( int BITree [ ] , int index ) { int ans = 0 ; index += 1 ; while ( index > 0 ) { ans ^= BITree [ index ] ; index -= index & ( - index ) ; } return ans ; } static void updateBIT ( int BITree [ ] , int n , int index , int val ) { index = index + 1 ; while ( index <= n ) { BITree [ index ] ^= val ; index += index & ( - index ) ; } } static int [ ] constructBITree ( int arr [ ] , int n ) { int [ ] BITree = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BITree [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) updateBIT ( BITree , n , i , arr [ i ] ) ; return BITree ; } static int rangeXor ( int BITree [ ] , int l , int r ) { return getXOR ( BITree , r ) ^ getXOR ( BITree , l - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 1 , 3 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = A . length ; int [ ] [ ] q = { { 1 , 0 , 9 } , { 2 , 3 , 6 } , { 2 , 5 , 5 } , { 2 , 8 , 1 } , { 1 , 0 , 9 } } ; int [ ] BITree = constructBITree ( A , n ) ; for ( int i = 0 ; i < q . length ; i ++ ) { int id = q [ i ] [ 0 ] ; if ( id == 1 ) { int L = q [ i ] [ 1 ] ; int R = q [ i ] [ 2 ] ; System . out . print ( " XOR ▁ of ▁ elements ▁ " + " in ▁ given ▁ range ▁ is ▁ " + rangeXor ( BITree , L , R ) + "NEW_LINE"); } else { int idx = q [ i ] [ 1 ] ; int val = q [ i ] [ 2 ] ; A [ idx ] ^= val ; updateBIT ( BITree , n , idx , val ) ; } } } }
import java . util . * ; class GFG { static final int MAX = 10000 ; static int [ ] prefix = new int [ MAX + 1 ] ; static boolean isPowerOfTwo ( int x ) { if ( x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ) return true ; return false ; } static void computePrefix ( int n , int a [ ] ) { if ( isPowerOfTwo ( a [ 0 ] ) ) prefix [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] ; if ( isPowerOfTwo ( a [ i ] ) ) prefix [ i ] ++ ; } } static int query ( int L , int R ) { if ( L == 0 ) return prefix [ R ] ; return prefix [ R ] - prefix [ L - 1 ] ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 8 , 5 , 2 , 5 , 10 } ; int N = A . length ; int Q = 2 ; computePrefix ( N , A ) ; System . out . println ( query ( 0 , 4 ) ) ; System . out . println ( query ( 3 , 5 ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int [ ] arr = new int [ 1000001 ] ; static int [ ] dp = new int [ 1000001 ] ; static void count ( ) { int maxN = 1000000 , i , j ; for ( i = 0 ; i < maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( j = 2 * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } int cnt = 0 ; for ( i = 0 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) cnt ++ ; if ( arr [ cnt ] == 1 ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } for ( i = 1 ; i <= maxN ; i ++ ) dp [ i ] += dp [ i - 1 ] ; } public static void main ( String [ ] args ) { int L = 4 , R = 12 ; count ( ) ; System . out . println ( dp [ R ] - dp [ L - 1 ] ) ; } }
import java . util . * ; class GFG { static void findUngroupedElement ( String s , int k ) { int n = s . length ( ) ; int [ ] b = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) b [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char p = s . charAt ( i ) ; b [ p - ' a ' ] += 1 ; } int sum = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( b [ i ] > k ) sum += b [ i ] - k ; } System . out . println ( sum ) ; } public static void main ( String srgs [ ] ) { String s = " stayinghomesaveslife " ; int k = 1 ; findUngroupedElement ( s , k ) ; } }
class GFG { static void findSolution ( int N ) { if ( N % 4 != 0 ) System . out . print ( - 1 + "NEW_LINE"); else { int temp = 0 , sum_odd = 0 ; int sum_even = 0 ; int result [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i += 2 ) { temp += 2 ; result [ i + 1 ] = temp ; sum_even += result [ i + 1 ] ; result [ i ] = temp - 1 ; sum_odd += result [ i ] ; } int diff = sum_even - sum_odd ; result [ N - 2 ] += diff ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 8 ; findSolution ( N ) ; } }
class GFG { public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( Math . pow ( 2 , N + 1 ) - 2 ) ; } }
class GFG { static double possiblePositions ( double n ) { double term1 = Math . pow ( n , 4 ) ; double term2 = Math . pow ( n , 3 ) ; double term3 = Math . pow ( n , 2 ) ; double term4 = n / 3 ; double ans = ( Math . ceil ( term1 / 2 ) ) - ( Math . ceil ( 5 * term2 ) / 3 ) + ( Math . ceil ( 3 * term3 ) / 2 ) - term4 ; return ( long ) ans ; } public static void main ( String [ ] args ) { double n ; n = 3 ; double ans = possiblePositions ( n ) ; System . out . print ( ans + "NEW_LINE"); } }
class GFG { static void mutually_coprime ( int n ) { if ( n <= 3 ) { for ( int j = 1 ; j < n + 1 ; j ++ ) System . out . print ( j + " ▁ " ) ; System . out . println ( ) ; } else { System . out . println ( "1 ▁ 2 ▁ 3" ) ; for ( int j = 4 ; j < n ; j += 2 ) { System . out . println ( j + " ▁ " + ( j + 1 ) ) ; if ( n % 2 == 0 ) System . out . println ( n ) ; } } } public static void main ( String [ ] args ) { int n = 9 ; mutually_coprime ( n ) ; } }
import java . util . * ; class GFG { static int findMaxSum ( int [ ] arr , int n ) { Vector < Integer > a = new Vector < Integer > ( ) ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { a . add ( arr [ i ] ) ; } else if ( arr [ i ] < 0 ) { b . add ( arr [ i ] ) ; } } Collections . sort ( a ) ; Collections . sort ( b ) ; int p = a . size ( ) - 1 ; int q = b . size ( ) - 1 ; int s = 0 ; while ( p >= 0 && q >= 0 ) { if ( a . get ( p ) + b . get ( q ) > 0 ) { s = s + a . get ( p ) + b . get ( q ) ; } else { break ; } p = p - 1 ; q = q - 1 ; } return s ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , - 2 , 3 , 4 , - 5 , 8 } ; int n1 = arr1 . length ; System . out . print ( findMaxSum ( arr1 , n1 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static void PrimeDegreeNodes ( int prufer [ ] , int n ) { int nodes = n + 2 ; boolean [ ] prime = new boolean [ nodes + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , nodes + 1 ) ; int [ ] degree = new int [ n + 2 + 1 ] ; for ( int i = 1 ; i <= nodes ; i ++ ) degree [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) degree [ prufer [ i ] ] ++ ; for ( int i = 1 ; i <= nodes ; i ++ ) { if ( prime [ degree [ i ] ] ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; PrimeDegreeNodes ( a , n ) ; } }
class GFG { static char ShortestPalindrome ( String s ) { int n = s . length ( ) ; char ans = s . charAt ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( char ) Math . min ( ans , s . charAt ( i ) ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . print ( ShortestPalindrome ( s ) ) ; } }
class GFG { static void findSubsequence ( int arr [ ] , int n ) { int sign [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = - 1 ; } int k = 0 ; int result [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = arr [ i ] ; int j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = Math . max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 } ; int n = arr . length ; findSubsequence ( arr , n ) ; } }
import java . util . * ; class GFG { static int countOfNum ( int n , int a , int b ) { int cnt_of_a , cnt_of_b , cnt_of_ab , sum ; cnt_of_a = n / a ; cnt_of_b = n / b ; sum = cnt_of_b + cnt_of_a ; cnt_of_ab = n / ( a * b ) ; sum = sum - cnt_of_ab ; return sum ; } static int sumOfNum ( int n , int a , int b ) { int i ; int sum = 0 ; Set < Integer > ans = new HashSet < Integer > ( ) ; for ( i = a ; i <= n ; i = i + a ) { ans . add ( i ) ; } for ( i = b ; i <= n ; i = i + b ) { ans . add ( i ) ; } for ( Integer it : ans ) { sum = sum + it ; } return sum ; } public static void main ( String [ ] args ) { int N = 88 ; int A = 11 ; int B = 8 ; int count = countOfNum ( N , A , B ) ; int sumofnum = sumOfNum ( N , A , B ) ; System . out . print ( sumofnum % count ) ; } }
import java . util . * ; class GFG { static boolean isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . equals ( p ) ) ; } static String PrefixSuffixPalindrome ( String str ) { int n = str . length ( ) , len = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) { len = i ; break ; } } String prefix = " " , suffix = " " ; String midPal = " " ; prefix = str . substring ( 0 , len ) ; suffix = str . substring ( n - len ) ; str = str . substring ( len , ( n - 2 * len ) + len ) ; for ( int i = 1 ; i <= str . length ( ) ; i ++ ) { String y = str . substring ( 0 , i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length ( ) < y . length ( ) ) { midPal = y ; } } } for ( int i = 1 ; i <= str . length ( ) ; i ++ ) { String y = str . substring ( str . length ( ) - i ) ; if ( isPalindrome ( y ) ) { if ( midPal . length ( ) < y . length ( ) ) { midPal = y ; } } } String answer = prefix + midPal + suffix ; return answer ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " abcdfdcecba " ; System . out . print ( PrefixSuffixPalindrome ( str ) ) ; } }
import java . util . * ; class GFG { static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = Integer . bitCount ( i ) ; if ( cnt == n - k ) { Vector < Integer > temp = new Vector < Integer > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) temp . add ( a [ j ] ) ; } int maxDiff = Integer . MIN_VALUE ; for ( int j = 0 ; j < temp . size ( ) - 1 ; j ++ ) { maxDiff = Math . max ( maxDiff , temp . get ( j + 1 ) - temp . get ( j ) ) ; } minDiff = Math . min ( minDiff , maxDiff ) ; } } return minDiff ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 2 ; int a [ ] = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
class GFG { static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = Integer . MIN_VALUE ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = Math . max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . min ( minDiff , maxDiff ) ; } return minDiff ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int findKMin ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . pollLast ( ) ; Qi . addLast ( i ) ; } int minDiff = Integer . MAX_VALUE ; for ( ; i < n ; ++ i ) { minDiff = Math . min ( minDiff , arr [ Qi . peekFirst ( ) ] ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peekFirst ( ) <= i - k ) Qi . pollFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . pollLast ( ) ; Qi . addLast ( i ) ; } minDiff = Math . min ( minDiff , arr [ Qi . peekFirst ( ) ] ) ; return minDiff ; } static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int [ ] diff = new int [ n - 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff [ i ] = a [ i + 1 ] - a [ i ] ; } int answer = findKMin ( diff , n - 1 , n - k - 1 ) ; return answer ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int a [ ] = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }
class GFG { static int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } static int findMinimumElements ( int [ ] a , int n ) { int [ ] b = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = arr1 . length ; System . out . print ( findMinimumElements ( arr1 , n1 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void findTriplets ( int x ) { Vector < Integer > fact = new Vector < Integer > ( ) ; HashSet < Integer > factors = new HashSet < Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . add ( i ) ; if ( x / i != i ) fact . add ( x / i ) ; factors . add ( i ) ; factors . add ( x / i ) ; } } boolean found = false ; int k = fact . size ( ) ; for ( int i = 0 ; i < k ; i ++ ) { int a = fact . get ( i ) ; for ( int j = 0 ; j < k ; j ++ ) { int b = fact . get ( j ) ; if ( ( a != b ) && ( x % ( a * b ) == 0 ) && ( x / ( a * b ) != a ) && ( x / ( a * b ) != b ) && ( x / ( a * b ) != 1 ) ) { System . out . print ( a + " ▁ " + b + " ▁ " + ( x / ( a * b ) ) ) ; found = true ; break ; } } if ( found ) break ; } if ( ! found ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int x = 105 ; findTriplets ( x ) ; } }
class GFG { static int countPair ( int a , int b ) { String s = String . valueOf ( b ) ; int i ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != '9' ) break ; } int result ; if ( i == s . length ( ) ) result = a * s . length ( ) ; else result = a * ( s . length ( ) - 1 ) ; return result ; } public static void main ( String [ ] args ) { int a = 5 , b = 101 ; System . out . print ( countPair ( a , b ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > makeSeq ( String s , int a [ ] ) { Vector < Integer > seq = new Vector < Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == ' A ' ) seq . add ( a [ i ] ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == ' B ' ) seq . add ( a [ i ] ) ; return seq ; } static boolean checkSeq ( Vector < Integer > v ) { boolean check = true ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) if ( v . get ( i ) < v . get ( i - 1 ) ) check = false ; return check ; } static String digitPartition ( int X ) { String num = String . valueOf ( X ) ; int l = num . length ( ) ; int [ ] a = new int [ l ] ; for ( int i = 0 ; i < l ; i ++ ) a [ i ] = ( num . charAt ( i ) - '0' ) ; for ( int D = 0 ; D < 10 ; D ++ ) { String res = " " ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] < D ) res += ' A ' ; else if ( a [ i ] > D ) res += ' B ' ; else res += ' C ' ; } boolean flag = false ; for ( int i = 0 ; i < l ; i ++ ) { if ( res . charAt ( i ) == ' B ' ) flag = true ; if ( res . charAt ( i ) == ' C ' ) res = res . substring ( 0 , i ) + ( flag ? ' A ' : ' B ' ) + res . substring ( i + 1 ) ; } Vector < Integer > seq = makeSeq ( res , a ) ; if ( checkSeq ( seq ) ) return res ; } return " - 1" ; } public static void main ( String [ ] args ) { int X = 777147777 ; System . out . print ( digitPartition ( X ) ) ; } }
class GFG { static float getArea ( int a ) { float area = ( float ) ( Math . PI * a * a ) / 4 ; return area ; } public static void main ( String args [ ] ) { int a = 3 ; System . out . println ( getArea ( a ) ) ; } }
public class GFG { static boolean flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 0 , 1 , 1 , 0 , 1 } ; int n = a . length ; if ( flipsPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static void PerformQueries ( int [ ] a , int [ ] [ ] vec ) { Vector < Integer > ans = new Vector < > ( ) ; int n = ( int ) a . length - 1 ; int q = ( int ) vec . length ; for ( int i = 0 ; i < q ; ++ i ) { long t = vec [ i ] [ 0 ] ; int m = vec [ i ] [ 1 ] ; if ( m > n ) { ans . add ( - 1 ) ; continue ; } int turn = ( int ) ( t / n ) ; int rem = ( int ) ( t % n ) ; if ( rem == 0 && turn % 2 == 1 ) { ans . add ( - 1 ) ; continue ; } if ( rem == 0 && turn % 2 == 0 ) { ans . add ( a [ m ] ) ; continue ; } if ( turn % 2 == 0 ) { int cursize = n - rem ; if ( cursize < m ) { ans . add ( - 1 ) ; continue ; } ans . add ( a [ m + rem ] ) ; } else { int cursize = rem ; if ( cursize < m ) { ans . add ( - 1 ) ; continue ; } ans . add ( a [ m ] ) ; } } for ( int i : ans ) System . out . print ( i + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] a = { - 1 , 1 , 2 , 3 , 4 , 5 } ; int [ ] [ ] vec = { { 1 , 4 } , { 6 , 1 } , { 3 , 5 } } ; PerformQueries ( a , vec ) ; } }
import java . util . * ; class GFG { static int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . length ; System . out . print ( sumEqualProduct ( a , n ) ) ; } }
class GFG { static void initialize ( int mul [ ] , int div [ ] , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } static void update ( int l , int r , int x , int mul [ ] , int div [ ] ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } static void printArray ( int ar [ ] , int mul [ ] , int div [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; System . out . print ( ar [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 10 , 5 , 20 , 40 } ; int n = ar . length ; int [ ] mul = new int [ n + 1 ] ; int [ ] div = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; } }
class GFG { static final long MAX = 1000000000 ; static void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; System . out . println ( " ( " + X1 + " , ▁ " + Y1 + " ) " ) ; System . out . println ( " ( " + X2 + " , ▁ " + Y2 + " ) " ) ; System . out . println ( " ( " + X3 + " , ▁ " + Y3 + " ) " ) ; } public static void main ( String [ ] args ) { long S = 4 ; findTriangle ( S ) ; } }
class GFG { final static int DIGITS = 10 ; final static int MAX = 10000 ; final static int MOD = 1000000007 ; static int dp [ ] [ ] = new int [ MAX ] [ DIGITS ] ; static int cnt [ ] [ ] = new int [ MAX ] [ DIGITS ] ; static void precompute ( ) { for ( int i = 0 ; i < DIGITS ; i ++ ) { dp [ 1 ] [ i ] = i ; cnt [ 1 ] [ i ] = 1 ; } for ( int i = 2 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < DIGITS ; j ++ ) { for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( j != k ) { dp [ i ] [ j ] = dp [ i ] [ j ] + ( dp [ i - 1 ] [ k ] + ( cnt [ i - 1 ] [ k ] * j ) % MOD ) % MOD ; dp [ i ] [ j ] %= MOD ; cnt [ i ] [ j ] += cnt [ i - 1 ] [ k ] ; cnt [ i ] [ j ] %= MOD ; } } } } } public static void main ( String [ ] args ) { int x = 6 , y = 4 ; precompute ( ) ; System . out . println ( dp [ x ] [ y ] ) ; } }
class GFG { static int N = 10000 ; static int MOD = 1000000007 ; static int [ ] F = new int [ N ] ; static void precompute ( ) { F [ 1 ] = 2 ; F [ 2 ] = 3 ; F [ 3 ] = 4 ; for ( int i = 4 ; i < N ; i ++ ) F [ i ] = ( F [ i - 1 ] + F [ i - 2 ] ) % MOD ; } public static void main ( String [ ] args ) { int n = 8 ; precompute ( ) ; System . out . println ( F [ n ] ) ; } }
import java . util . * ; class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static double minimizedSum ( int a [ ] , int n , int K ) { int sum = maxSubArraySum ( a , n ) ; double totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) totalSum += a [ i ] ; if ( sum < 0 ) return totalSum ; totalSum = totalSum - sum + ( double ) sum / ( double ) K ; return totalSum ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , - 2 , 3 } ; int n = a . length ; int K = 2 ; System . out . println ( minimizedSum ( a , n , K ) ) ; } }
import java . util . * ; class GFG { static double get ( double L , double R ) { double x = 1.0 / L ; double y = 1.0 / ( R + 1.0 ) ; return ( x - y ) ; } public static void main ( String [ ] args ) { int L = 6 , R = 12 ; double ans = get ( L , R ) ; System . out . printf ( " % .2f " , ans ) ; } }
class GFG { static int findMin ( int N ) { int digit = N % 10 ; switch ( digit ) { case 0 : if ( N >= 90 ) return 10 ; break ; case 1 : if ( N >= 81 ) return 9 ; break ; case 2 : if ( N >= 72 ) return 8 ; break ; case 3 : if ( N >= 63 ) return 7 ; break ; case 4 : if ( N >= 54 ) return 6 ; break ; case 5 : if ( N >= 45 ) return 5 ; break ; case 6 : if ( N >= 36 ) return 4 ; break ; case 7 : if ( N >= 27 ) return 3 ; break ; case 8 : if ( N >= 18 ) return 2 ; break ; case 9 : if ( N >= 9 ) return 1 ; break ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 27 ; System . out . println ( findMin ( N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 100000 ; static ArrayList < Integer > v = new ArrayList < Integer > ( ) ; public static int upper_bound ( ArrayList < Integer > ar , int k ) { int s = 0 ; int e = ar . size ( ) ; while ( s != e ) { int mid = s + e >> 1 ; if ( ar . get ( mid ) <= k ) { s = mid + 1 ; } else { e = mid ; } } if ( s == ar . size ( ) ) { return - 1 ; } return s ; } static int consecutiveOnes ( int x ) { int p = 0 ; while ( x > 0 ) { if ( x % 2 == 1 && p == 1 ) { return 1 ; } p = x % 2 ; x /= 2 ; } return 0 ; } static void preCompute ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) { if ( consecutiveOnes ( i ) == 0 ) { v . add ( i ) ; } } } static int nextValid ( int n ) { int it = upper_bound ( v , n ) ; int val = v . get ( it ) ; return val ; } static void performQueries ( int queries [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( nextValid ( queries [ i ] ) ) ; } } public static void main ( String [ ] args ) { int queries [ ] = { 4 , 6 } ; int q = queries . length ; preCompute ( ) ; performQueries ( queries , q ) ; } }
class GFG { static int changeToOnes ( String str ) { int i , l , ctr = 0 ; l = str . length ( ) ; for ( i = l - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '1' ) ctr ++ ; else break ; } return l - ctr ; } static String removeZeroesFromFront ( String str ) { String s ; int i = 0 ; while ( i < str . length ( ) && str . charAt ( i ) == '0' ) i ++ ; if ( i == str . length ( ) ) s = "0" ; else s = str . substring ( i , str . length ( ) - i ) ; return s ; } public static void main ( String [ ] args ) { String str = "10010111" ; str = removeZeroesFromFront ( str ) ; System . out . println ( changeToOnes ( str ) ) ; } }
class GFG { static int maxPartition ( String s ) { int n = s . length ( ) , P = 0 ; String current = " " , previous = " " ; for ( int i = 0 ; i < n ; i ++ ) { current += s . charAt ( i ) ; if ( ! current . equals ( previous ) ) { previous = current ; current = " " ; P ++ ; } } return P ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int ans = maxPartition ( s ) ; System . out . println ( ans ) ; } }
class GFG { public static void main ( String [ ] args ) { int N = 4 , i , j , min ; System . out . println ( " Value ▁ of ▁ N : ▁ " + N ) ; for ( i = 1 ; i <= N ; i ++ ) { for ( j = 1 ; j <= N ; j ++ ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } for ( j = N - 1 ; j >= 1 ; j -- ) { min = i < j ? i : j ; System . out . print ( N - min + 1 ) ; } System . out . println ( ) ; } } }
import java . util . * ; class GFG { static int findMissingPositive ( int arr [ ] , int n ) { int m = 1 ; HashSet < Integer > x = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m < arr [ i ] ) { x . add ( arr [ i ] ) ; } else if ( m == arr [ i ] ) { m = m + 1 ; while ( x . contains ( m ) ) { x . remove ( m ) ; m = m + 1 ; } } } return m ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 7 , 6 , 8 , 1 , - 10 , 15 } ; int n = arr . length ; System . out . println ( findMissingPositive ( arr , n ) ) ; } }
import java . io . * ; class GFG { static boolean prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } static int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } public static void main ( String [ ] args ) { int n = 27 ; System . out . println ( Min_Sum ( n ) ) ; } }
import java . util . * ; class GFG { static int MinDeletion ( int a [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( a [ i ] ) ) { mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . put ( a [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int x = i . getKey ( ) ; int frequency = i . getValue ( ) ; if ( x <= frequency ) { ans += ( frequency - x ) ; } else ans += frequency ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 } ; int n = a . length ; System . out . println ( MinDeletion ( a , n ) ) ; } }
import java . util . * ; class GFG { static String find_number ( int N , int K ) { String r ; if ( N % 100 == 0 ) { N += 1 ; r = String . valueOf ( N ) ; } else { r = String . valueOf ( N ) ; } for ( int i = 1 ; i <= K ; i ++ ) r += "00" ; return r ; } public static void main ( String [ ] args ) { int N = 1000 , K = 2 ; String ans = find_number ( N , K ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int maxCountAB ( String s [ ] , int n ) { int A = 0 , B = 0 , BA = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String S = s [ i ] ; int L = S . length ( ) ; for ( int j = 0 ; j < L - 1 ; j ++ ) { if ( S . charAt ( j ) == ' A ' && S . charAt ( j + 1 ) == ' B ' ) { ans ++ ; } } if ( S . charAt ( 0 ) == ' B ' && S . charAt ( L - 1 ) == ' A ' ) BA ++ ; else if ( S . charAt ( 0 ) == ' B ' ) B ++ ; else if ( S . charAt ( L - 1 ) == ' A ' ) A ++ ; } if ( BA == 0 ) ans += Math . min ( B , A ) ; else if ( A + B == 0 ) ans += BA - 1 ; else ans += BA + Math . min ( B , A ) ; return ans ; } public static void main ( String [ ] args ) { String s [ ] = { " ABCA " , " BOOK " , " BAND " } ; int n = s . length ; System . out . println ( maxCountAB ( s , n ) ) ; } }
class GFG { static int MinOperations ( int n , int x , int [ ] arr ) { int total = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] > x ) { int difference = arr [ i ] - x ; total = total + difference ; arr [ i ] = x ; } } for ( int i = 1 ; i < n ; ++ i ) { int LeftNeigbouringSum = arr [ i ] + arr [ i - 1 ] ; if ( LeftNeigbouringSum > x ) { int current_diff = LeftNeigbouringSum - x ; arr [ i ] = Math . max ( 0 , arr [ i ] - current_diff ) ; total = total + current_diff ; } } return total ; } public static void main ( String args [ ] ) { int X = 1 ; int arr [ ] = { 1 , 6 , 1 , 2 , 0 , 4 } ; int N = arr . length ; System . out . println ( MinOperations ( N , X , arr ) ) ; } }
import java . util . * ; class Sol { static int countPairs ( String s ) { Set < Character > st = new HashSet < Character > ( ) ; st . add ( ' a ' ) ; st . add ( ' e ' ) ; st . add ( ' i ' ) ; st . add ( ' o ' ) ; st . add ( ' u ' ) ; int count = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( st . contains ( s . charAt ( i ) ) && ! st . contains ( s . charAt ( i + 1 ) ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; System . out . println ( countPairs ( s ) ) ; } }
import java . io . * ; class GFG { static void solve ( int d1 , int d2 , int d3 ) { int maxx = Math . max ( d1 , Math . max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum sum % 2 == 1 ) { System . out . print ( " - 1" ) ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; System . out . print ( " ( " + x1 + " , ▁ " + y1 + " ) , ▁ ( " + x2 + " , ▁ " + y2 + " ) ▁ and ▁ ( " + x3 + " , ▁ " + y3 + " ) " ) ; } public static void main ( String [ ] args ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; } }
import static java . lang . Integer . max ; import java . util . * ; class GFG { static int maxSize ( Vector < Integer > v , int n ) { v . add ( 0 ) ; v . add ( 100001 ) ; n += 2 ; Collections . sort ( v ) ; int mx = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int L = v . get ( i - 1 ) + 1 ; int R = v . get ( i + 1 ) - 1 ; int cnt = R - L + 1 ; mx = max ( mx , cnt ) ; } return mx ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 200 , 10 , 5 } ; Vector v = new Vector ( Arrays . asList ( arr ) ) ; int n = v . size ( ) ; System . out . println ( maxSize ( v , n ) ) ; } }
class GFG { static int maxLength ( int a [ ] , int n ) { int sum = 0 , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } { return n ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { len = Math . max ( len , Math . max ( n - i - 1 , i ) ) ; } } return len ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 2 } ; int n = a . length ; System . out . println ( maxLength ( a , n ) ) ; } }
class GFG { static void solve ( long n ) { long initial_array [ ] [ ] = new long [ ( int ) n - 1 ] [ ( int ) n - 1 ] , final_array [ ] [ ] = new long [ ( int ) n ] [ ( int ) n ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) initial_array [ 0 ] [ ( int ) i ] = i + 1 ; for ( long i = 1 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) initial_array [ ( int ) i ] [ ( int ) j ] = initial_array [ ( int ) i - 1 ] [ ( int ) ( ( int ) j + 1 ) % ( ( int ) n - 1 ) ] ; for ( long i = 0 ; i < n - 1 ; ++ i ) for ( long j = 0 ; j < n - 1 ; ++ j ) final_array [ ( int ) i ] [ ( int ) j ] = initial_array [ ( int ) i ] [ ( int ) j ] ; for ( long i = 0 ; i < n ; ++ i ) final_array [ ( int ) i ] [ ( int ) n - 1 ] = final_array [ ( int ) n - 1 ] [ ( int ) i ] = 0 ; for ( long i = 0 ; i < n ; ++ i ) { long t0 = final_array [ ( int ) i ] [ ( int ) i ] ; long t1 = final_array [ ( int ) i ] [ ( int ) n - 1 ] ; long s = final_array [ ( int ) i ] [ ( int ) i ] ; final_array [ ( int ) i ] [ ( int ) i ] = final_array [ ( int ) i ] [ ( int ) n - 1 ] ; final_array [ ( int ) i ] [ ( int ) n - 1 ] = s ; final_array [ ( int ) n - 1 ] [ ( int ) i ] = t0 ; } for ( long i = 0 ; i < n ; ++ i ) { for ( long j = 0 ; j < n ; ++ j ) System . out . print ( final_array [ ( int ) i ] [ ( int ) j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { long n = 5 ; solve ( n ) ; } }
import java . util . * ; class GFG { static void findPerm ( int n , Vector < Integer > differences ) { Vector < Integer > ans = new Vector < Integer > ( ) ; ans . clear ( ) ; ans . add ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences . get ( i ) ; x = x + diff ; ans . add ( x ) ; } Vector < Integer > anss = new Vector < Integer > ( ) ; for ( Integer obj : ans ) anss . add ( obj ) ; Collections . sort ( ans ) ; int flag = - 1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans . get ( i ) - ans . get ( i - 1 ) ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { System . out . print ( - 1 ) ; return ; } else { Map < Integer , Integer > mpp = new HashMap < > ( ) ; mpp . clear ( ) ; int j = 1 ; Vector < Integer > value_at_index = new Vector < Integer > ( ) ; for ( Integer x1 : ans ) { mpp . put ( x1 , j ) ; ++ j ; } for ( Integer x2 : anss ) { value_at_index . add ( mpp . get ( x2 ) ) ; } for ( Integer x3 : value_at_index ) { System . out . print ( x3 + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { Vector < Integer > differences = new Vector < Integer > ( ) ; differences . add ( 2 ) ; differences . add ( - 3 ) ; differences . add ( 2 ) ; int n = differences . size ( ) + 1 ; findPerm ( n , differences ) ; } }
import java . util . Arrays ; class GfG { static boolean isPalindrome ( String str , int i , int j ) { while ( i < j ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int countPrimePalindrome ( String str , int len ) { boolean [ ] prime = new boolean [ len + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= len ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i <= len ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int j = 2 ; j <= len ; j ++ ) { if ( prime [ j ] ) { for ( int i = 0 ; i + j - 1 < len ; i ++ ) { if ( isPalindrome ( str , i , i + j - 1 ) ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int len = s . length ( ) ; System . out . println ( countPrimePalindrome ( s , len ) ) ; } }
class GFG { static int minOperations ( int N ) { double x = Math . log ( N ) / Math . log ( 2 ) ; int ans = ( int ) ( Math . ceil ( x ) ) ; return ans ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( minOperations ( N ) ) ; } }
class GFG { static int find_greatest_divisor ( int l , int r ) { if ( l == r ) { return l ; } return 1 ; } public static void main ( String [ ] args ) { int l = 2 , r = 12 ; System . out . println ( find_greatest_divisor ( l , r ) ) ; } }
public class GFG { static void findNumbers ( int arr [ ] , int n ) { int sumN = ( n * ( n + 1 ) ) / 2 ; int sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sum = 0 , sumSq = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; sumSq += Math . pow ( arr [ i ] , 2 ) ; } int B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; int A = sum - sumN + B ; System . out . println ( " A ▁ = ▁ " + A + " B = " + B); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 } ; int n = arr . length ; findNumbers ( arr , n ) ; } }
import java . io . * ; class GFG { static double getProbability ( double p , double q ) { p /= 100 ; q /= 100 ; double probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } public static void main ( String [ ] args ) { double p , q ; p = 80 ; q = 40 ; System . out . println ( getProbability ( p , q ) ) ; } }
class GFG { static boolean check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { return true ; } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; if ( check ( arr , n ) ) { System . out . println ( " Odd ▁ Bit - wise ▁ OR " ) ; } else { System . out . println ( " Even ▁ Bit - wise ▁ OR " ) ; } } }
import java . util . * ; class GFG { public static void main ( String args [ ] ) { String st = "10010111" ; char [ ] str = st . toCharArray ( ) ; int lengthh = st . length ( ) ; int i = lengthh - 1 ; while ( str [ i ] == '1' ) { str [ i ] = '0' ; i -- ; } if ( i >= 0 ) str [ i ] = '1' ; System . out . print ( str ) ; } }
class GFG { public static void main ( String [ ] args ) { int n = 800000 ; double t = ( double ) System . nanoTime ( ) ; long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += i ; t = ( double ) System . nanoTime ( ) - t ; System . out . println ( " sum ▁ is : ▁ " + Double . toString ( sum ) ) ; System . out . println ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + Double . toString ( t / Math . pow ( 10.0 , 9.0 ) ) ) ; t = ( double ) System . nanoTime ( ) ; sum = 0 ; for ( int i = 1 ; i <= n ; i += 8 ) { sum += i ; sum += ( i + 1 ) ; sum += ( i + 2 ) ; sum += ( i + 3 ) ; sum += ( i + 4 ) ; sum += ( i + 5 ) ; sum += ( i + 6 ) ; sum += ( i + 7 ) ; } t = ( double ) System . nanoTime ( ) - t ; System . out . println ( " sum ▁ is : ▁ " + Double . toString ( sum ) ) ; System . out . println ( " time ▁ taken ▁ by ▁ normal ▁ loops : " + Double . toString ( t / Math . pow ( 10.0 , 9.0 ) ) ) ; } }
import java . io . * ; class GFG { static int _log ( double x , double base ) { return ( int ) ( Math . log ( x ) / Math . log ( base ) ) ; } static double recursiveLogStar ( double n , double b ) { if ( n > 1.0 ) return 1.0 + recursiveLogStar ( _log ( n , b ) , b ) ; else return 0 ; } public static void main ( String [ ] args ) { int n = 100 , base = 5 ; System . out . println ( " Log * ( " + n + " ) ▁ = ▁ " + recursiveLogStar ( n , base ) ) ; } }
public static int iterativeLogStar ( double n , double b ) { int count = 0 ; while ( n >= 1 ) { n = _log ( n , b ) ; count ++ ; } return count ; }
static void function ( int n ) { int count = 0 ; for ( int i = n / 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= n ; j = 2 * j ) for ( int k = 1 ; k <= n ; k = k * 2 ) count ++ ; }
static int sum ( int sum ) { if ( n <= 0 ) return 0 ; return n + sum ( n - 1 ) ; }
import java . io . * ; public class GFG { static int findMaximum ( int arr [ ] , int size ) { int i = 0 , j = size - 1 , mid ; while ( i <= j ) { mid = i + ( j - i ) / 2 ; if ( arr [ mid ] < 0 ) i = mid + 1 ; else if ( arr [ mid ] > 0 ) j = mid - 1 ; } return Math . max ( i , size - i ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 9 , - 7 , - 4 , 1 , 5 , 8 , 9 } ; int N = arr . length ; System . out . println ( findMaximum ( arr , N ) ) ; } }
import java . io . * ; class GFG { static double maxKth ( int arr [ ] , int N , int C , int K ) { if ( N + C < K ) { return - 1 ; } int iter = 300 ; double l = 0 , r = 1000000000.0 ; while ( iter -- > 0 ) { double mid = ( l + r ) * 0.5 ; double a = 0 ; double b = 0 ; for ( int i = 0 ; i < N ; i ++ ) { a += ( int ) ( ( double ) arr [ i ] / mid ) ; if ( ( double ) arr [ i ] >= mid ) { b ++ ; } } if ( a >= K && b + C >= K ) { l = mid ; } else { r = mid ; } } return l ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 } ; int K = 1 , C = 1 ; int N = arr . length ; System . out . println ( maxKth ( arr , N , C , K ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static boolean isPossibleToSelect ( int [ ] arr , int N , int dif , int K ) { int count = 1 ; int prev = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= ( prev + dif ) ) { count ++ ; if ( count == K ) return true ; prev = arr [ i ] ; } } return false ; } static int binarySearch ( int [ ] arr , int left , int right , int K , int N ) { int ans = 1 ; while ( left <= right ) { int dif = left + ( right - left ) / 2 ; if ( isPossibleToSelect ( arr , N , dif , K ) ) { ans = Math . max ( ans , dif ) ; left = dif + 1 ; } else right = dif - 1 ; } return ans ; } public static void main ( String [ ] args ) { int N , K ; N = 7 ; K = 4 ; int [ ] arr = { 1 , 4 , 9 , 0 , 2 , 13 , 3 } ; Arrays . sort ( arr ) ; System . out . println ( binarySearch ( arr , 0 , arr [ N - 1 ] , K , N ) ) ; } }
import java . util . * ; import java . util . Arrays ; class GFG { static int cal ( int arr [ ] , int mid ) { int chocolate = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] >= mid ) chocolate += arr [ i ] - mid ; } return chocolate ; } static int maximumCut ( int arr [ ] , int K ) { int low = 0 ; int high = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int chocolate = cal ( arr , mid ) ; if ( chocolate == K ) return mid ; else if ( chocolate < K ) high = mid - 1 ; else { low = mid + 1 ; if ( mid > high ) high = mid ; } } return high ; } public static void main ( String [ ] args ) { int K = 7 ; int arr [ ] = { 15 , 20 , 8 , 17 } ; System . out . println ( maximumCut ( arr , K ) ) ; } }
import java . util . * ; class GFG { static boolean check ( int [ ] bananas , int mid_val , int H ) { int time = 0 ; for ( int i = 0 ; i < bananas . length ; i ++ ) { if ( bananas [ i ] % mid_val != 0 ) { time += ( ( bananas [ i ] / mid_val ) + 1 ) ; } else { time += ( bananas [ i ] / mid_val ) ; } } if ( time <= H ) { return true ; } else { return false ; } } static int minEatingSpeed ( int [ ] piles , int H ) { int start = 1 ; int end = Arrays . stream ( piles ) . max ( ) . getAsInt ( ) ; while ( start < end ) { int mid = start + ( end - start ) / 2 ; if ( ( check ( piles , mid , H ) ) == true ) { end = mid ; } else { start = mid + 1 ; } } return end ; } public static void main ( String [ ] args ) { int [ ] piles = { 30 , 11 , 23 , 4 , 20 } ; int H = 6 ; System . out . print ( minEatingSpeed ( piles , H ) ) ; } }
import java . io . * ; class GFG { static long check ( long H [ ] , long A [ ] , long mid , long N , long M , long L ) { long sum = 0 ; for ( long i = 0 ; i < N ; i ++ ) { long speed = mid * A [ ( int ) i ] + H [ ( int ) i ] ; if ( speed >= L ) { sum += speed ; } } return sum ; } static long buzzTime ( long N , long M , long L , long H [ ] , long A [ ] ) { long low = 0 , high = 100000000 ; long ans = 0 ; while ( high >= low ) { long mid = low + ( high - low ) / 2 ; if ( check ( H , A , mid , N , M , L ) >= M ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { long M = 400 , L = 120 ; long H [ ] = { 20 , 50 , 20 } ; long A [ ] = { 20 , 70 , 90 } ; long N = A . length ; System . out . println ( buzzTime ( N , M , L , H , A ) ) ; } }
class GFG { public static boolean is_possible ( int [ ] teams , int T , int k ) { int sum = 0 ; for ( int i = 0 ; i < teams . length ; i ++ ) { sum += Math . min ( T , teams [ i ] ) ; } return ( sum >= ( T * k ) ) ; } public static int countOfTeams ( int [ ] teams_list , int N , int K ) { int lb = 0 ; double ub = 1e9 ; while ( lb <= ub ) { int mid = lb + ( int ) ( ub - lb ) / 2 ; if ( is_possible ( teams_list , mid , K ) ) { if ( ! is_possible ( teams_list , mid + 1 , K ) ) { return mid ; } else { lb = mid + 1 ; } } else { ub = mid - 1 ; } } return 0 ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 4 } ; int K = 2 ; int N = arr . length ; System . out . println ( countOfTeams ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static void minimumOperations ( int arr [ ] , int N ) { int mx = 0 , pos = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= mx ) { mx = arr [ i ] ; pos = i ; } } System . out . println ( ( mx - 1 ) * N + pos + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 2 } ; int N = arr . length ; minimumOperations ( arr , N ) ; } }
import java . util . * ; public class GFG { static void Rearrange ( int arr [ ] , int K , int N ) { int ans [ ] = new int [ N + 1 ] ; int f = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = - 1 ; } for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == K ) { K = i ; break ; } } Vector < Integer > smaller = new Vector < Integer > ( ) ; Vector < Integer > greater = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < arr [ K ] ) smaller . add ( arr [ i ] ) ; else if ( arr [ i ] > arr [ K ] ) greater . add ( arr [ i ] ) ; } int low = 0 , high = N - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid == K ) { ans [ mid ] = arr [ K ] ; f = 1 ; break ; } else if ( mid < K ) { if ( smaller . size ( ) == 0 ) { break ; } ans [ mid ] = smaller . lastElement ( ) ; smaller . remove ( smaller . size ( ) - 1 ) ; low = mid + 1 ; } else { if ( greater . size ( ) == 0 ) { break ; } ans [ mid ] = greater . lastElement ( ) ; greater . remove ( greater . size ( ) - 1 ) ; high = mid - 1 ; } } if ( f == - 1 ) { System . out . println ( - 1 ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ans [ i ] == - 1 ) { if ( smaller . size ( ) > 0 ) { ans [ i ] = smaller . lastElement ( ) ; smaller . remove ( smaller . size ( ) - 1 ) ; } else if ( greater . size ( ) > 0 ) { ans [ i ] = greater . lastElement ( ) ; greater . remove ( greater . size ( ) - 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 7 , 2 , 5 , 3 , 8 } ; int K = 7 ; int N = arr . length ; Rearrange ( arr , K , N ) ; } }
import java . io . * ; class GFG { static int largestAltitude ( int L , int B ) { if ( L > B ) { int t = L ; L = B ; B = t ; } int low = 0 , high = L ; int res = 0 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( mid <= ( B / 2 ) ) { res = mid ; low = mid + 1 ; } else high = mid - 1 ; } return res ; } public static void main ( String [ ] args ) { int L = 3 ; int B = 4 ; System . out . print ( largestAltitude ( L , B ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void printFreq ( int arr [ ] , int N ) { int freq = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) { freq ++ ; } else { System . out . println ( " Frequency ▁ of ▁ " + arr [ i - 1 ] + " ▁ is : ▁ " + freq ) ; freq = 1 ; } } System . out . println ( " Frequency ▁ of ▁ " + arr [ N - 1 ] + " ▁ is : ▁ " + freq ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int N = arr . length ; printFreq ( arr , N ) ; } }
public class GFG { static boolean isMaximumMedian ( int arr [ ] [ ] , int N , int K , int mid ) { int [ ] [ ] Pre = new int [ N + 5 ] [ N + 5 ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { Pre [ i + 1 ] [ j + 1 ] = Pre [ i + 1 ] [ j ] + Pre [ i ] [ j + 1 ] - Pre [ i ] [ j ] ; if ( arr [ i ] [ j ] <= mid ) Pre [ i + 1 ] [ j + 1 ] ++ ; } } int required = ( K * K + 1 ) / 2 ; boolean flag = false ; for ( int i = K ; i <= N ; ++ i ) { for ( int j = K ; j <= N ; ++ j ) { int X = Pre [ i ] [ j ] - Pre [ i - K ] [ j ] - Pre [ i ] [ j - K ] + Pre [ i - K ] [ j - K ] ; if ( X < required ) flag = true ; } } return flag ; } static int maximumMedian ( int arr [ ] [ ] , int N , int K ) { int low = 0 , high = ( int ) 1e9 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( isMaximumMedian ( arr , N , K , mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 1 , 5 , 12 } , { 6 , 7 , 11 } , { 8 , 9 , 10 } } ; int N = arr . length ; int K = 2 ; System . out . print ( maximumMedian ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static void findMinDays ( int arr [ ] , int R [ ] , int N , int X , int K ) { int low = 0 , high = X ; int minDays = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int temp = arr [ i ] + R [ i ] * mid ; if ( temp >= K ) { sum += temp ; } } if ( sum >= X ) { minDays = mid ; high = mid - 1 ; } else { low = mid + 1 ; } } System . out . println ( minDays ) ; } public static void main ( String [ ] args ) { int X = 100 , K = 45 ; int arr [ ] = { 2 , 5 , 2 , 6 } ; int R [ ] = { 10 , 13 , 15 , 12 } ; int N = arr . length ; findMinDays ( arr , R , N , X , K ) ; } }
import java . util . * ; class GFG { static int getPairsCount ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( a [ i ] * a [ j ] ) == Math . abs ( i - j ) ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 4 } ; int N = arr . length ; System . out . print ( getPairsCount ( arr , N ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static String lexicographicallySmallestString ( String S , int N ) { int LastZe = - 1 ; String ans = " " ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '0' ) { LastZe = i ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( i <= LastZe && S . charAt ( i ) == '0' ) ans += S . charAt ( i ) ; else if ( i > LastZe ) ans += S . charAt ( i ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "11001101" ; int N = S . length ( ) ; System . out . println ( lexicographicallySmallestString ( S , N ) ) ; } }
public class MyClass { public static int minSwaps ( int N , int M , int A [ ] [ ] , int B [ ] [ ] ) { int count01 = 0 ; int count10 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( A [ i ] [ j ] != B [ i ] [ j ] ) { if ( A [ i ] [ j ] == 1 ) count10 ++ ; else count01 ++ ; } } } if ( count01 == count10 ) return count01 ; else return - 1 ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 1 , 0 } , { 0 , 0 , 1 } , { 0 , 1 , 0 } } ; int [ ] [ ] B = { { 0 , 0 , 1 } , { 0 , 1 , 0 } , { 1 , 1 , 0 } } ; int N = A . length ; int M = B [ 0 ] . length ; System . out . println ( minSwaps ( N , M , A , B ) ) ; } }
public class GFG { static int Count ( int N , int K ) { if ( K == 0 ) return 0 ; int sum = 0 ; int res = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += i ; if ( sum <= K ) res ++ ; else break ; } return res ; } public static void main ( String args [ ] ) { int N = 6 , K = 14 ; System . out . println ( Count ( N , K ) ) ; } }
import java . util . * ; class GFG { static String removeLastOccurrence ( String S , String W , int N , int M ) { char [ ] ch = S . toCharArray ( ) ; if ( M > N ) return S ; for ( int i = N - M ; i >= 0 ; i -- ) { int flag = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( ch [ j + i ] != W . charAt ( j ) ) { flag = 1 ; break ; } } if ( flag == 0 ) { for ( int j = i ; j < N - M ; j ++ ) ch [ j ] = ch [ j + M ] ; break ; } } char [ ] chh = new char [ N - M ] ; for ( int i = 0 ; i < N - M ; i ++ ) { chh [ i ] = ch [ i ] ; } return String . valueOf ( chh ) ; } public static void main ( String [ ] args ) { String S = " This ▁ is ▁ GeeksForGeeks " ; String W = " Geeks " ; int N = S . length ( ) ; int M = W . length ( ) ; System . out . print ( removeLastOccurrence ( S , W , N , M ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maximumIndices ( int arr [ ] , int N ) { int [ ] temp = new int [ N ] ; int maxIndices = ( N - 1 ) / 2 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < maxIndices ; i ++ ) { temp [ 2 * i + 1 ] = arr [ i ] ; } int j = 0 ; for ( int i = maxIndices ; i < N ; ) { if ( temp [ j ] == 0 ) { temp [ j ] = arr [ i ] ; i ++ ; } j ++ ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( temp [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = 4 ; maximumIndices ( arr , N ) ; } }
import java . util . * ; class GFG { static void minimumK ( ArrayList < Integer > arr , int M , int N ) { int good = ( int ) ( ( N * 1.0 ) / ( ( M + 1 ) * 1.0 ) ) + 1 ; for ( int i = 1 ; i <= N ; i ++ ) { int K = i ; int candies = N ; int taken = 0 ; while ( candies > 0 ) { taken += Math . min ( K , candies ) ; candies -= Math . min ( K , candies ) ; for ( int j = 0 ; j < M ; j ++ ) { int consume = ( arr . get ( j ) * candies ) / 100 ; candies -= consume ; } } if ( taken >= good ) { System . out . print ( i ) ; return ; } } } public static void main ( String [ ] args ) { int N = 13 , M = 1 ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 50 ) ; minimumK ( arr , M , N ) ; } }
import java . util . * ; class GFG { static boolean check ( int K , int n , int m , ArrayList < Integer > arr , int good_share ) { int candies = n , taken = 0 ; while ( candies > 0 ) { taken += Math . min ( K , candies ) ; candies -= Math . min ( K , candies ) ; for ( int j = 0 ; j < m ; j ++ ) { int consume = ( arr . get ( j ) * candies ) / 100 ; candies -= consume ; } } return ( taken >= good_share ) ; } static void minimumK ( ArrayList < Integer > arr , int N , int M ) { int good_share = ( int ) Math . ceil ( ( N * 1.0 ) / ( ( M + 1 ) * 1.0 ) ) ; int lo = 1 , hi = N ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( mid , N , M , arr , good_share ) ) { hi = mid ; } else { lo = mid + 1 ; } } System . out . print ( hi ) ; } public static void main ( String [ ] args ) { int N = 13 , M = 1 ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 50 ) ; minimumK ( arr , N , M ) ; } }
import java . util . * ; class GFG { static int ans = 0 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > edges [ ] = new Vector [ 100000 ] ; static void Add_edge ( int u , int v ) { edges [ u ] . add ( v ) ; edges [ v ] . add ( u ) ; } static void minTimeToColor ( int node , int parent , int arrival_time ) { int current_time = 0 ; for ( int x = 0 ; x < edges [ node ] . size ( ) ; x ++ ) { if ( edges [ node ] . get ( x ) != parent ) { ++ current_time ; if ( current_time == arrival_time ) ++ current_time ; ans = Math . max ( ans , current_time ) ; minTimeToColor ( edges [ node ] . get ( x ) , node , current_time ) ; } } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < edges . length ; i ++ ) edges [ i ] = new Vector < Integer > ( ) ; int A [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; for ( int i = 0 ; i < 3 ; i ++ ) { Add_edge ( A [ i ] [ 0 ] , A [ i ] [ 1 ] ) ; } minTimeToColor ( 1 , - 1 , 0 ) ; System . out . print ( ans + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; public class GFG { public static int largestNum ( int [ ] arr ) { int res = 0 ; Arrays . sort ( arr ) ; int l = 0 , r = arr . length - 1 ; while ( l < r ) { int sum = arr [ l ] + arr [ r ] ; if ( sum == 0 ) { res = Math . max ( res , Math . max ( arr [ l ] , arr [ r ] ) ) ; return res ; } else if ( sum < 0 ) { l ++ ; } else { r -- ; } } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , - 2 , 5 , - 3 } ; System . out . println ( largestNum ( arr ) ) ; } }
import java . util . * ; class GFG { static void findLarger ( int arr [ ] , int n ) { int mid = ( n + 1 ) / 2 ; int mx = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int count [ ] = new int [ mx + 1 ] ; for ( int i = 0 ; i < mx + 1 ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } for ( int i = mx ; i >= 0 ; i -- ) { while ( count [ i ] > 0 ) { count [ i ] -- ; mid -- ; System . out . print ( i + " ▁ " ) ; if ( mid == 0 ) break ; } if ( mid == 0 ) break ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 4 , 2 , 8 , 9 } ; int N = arr . length ; findLarger ( arr , N ) ; } }
import java . util . * ; class GFG { static double probability ( int [ ] arr1 , int [ ] arr2 ) { int N = arr1 . length ; int M = arr2 . length ; double res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int y = 0 ; for ( int j = 0 ; j < M ; j ++ ) { if ( arr2 [ j ] > arr1 [ i ] ) y ++ ; } res += y ; } res = ( double ) res / ( double ) ( N * M ) ; return res ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 5 , 2 , 6 , 1 } ; int [ ] arr2 = { 1 , 6 , 10 , 1 } ; System . out . println ( probability ( arr1 , arr2 ) ) ; } }
import java . util . * ; class GFG { static double probability ( int [ ] arr1 , int [ ] arr2 ) { int N = arr1 . length ; int M = arr2 . length ; double res = 0 ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < N ; i ++ ) { int y = countGreater ( arr2 , arr1 [ i ] ) ; res += y ; } res = ( double ) res / ( double ) ( N * M ) ; return res ; } static int countGreater ( int [ ] arr , int k ) { int n = arr . length ; int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 5 , 2 , 6 , 1 } ; int [ ] arr2 = { 1 , 6 , 10 , 1 } ; System . out . println ( probability ( arr1 , arr2 ) ) ; } }
import java . util . * ; public class MyClass { static void minCost ( char arr [ ] [ ] , int A , int B ) { int n = arr . length ; int m = arr [ 0 ] . length ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == ' * ' ) continue ; if ( j == m - 1 ) ans += A ; else { if ( arr [ i ] [ j + 1 ] == ' . ' ) { ans += Math . min ( 2 * A , B ) ; j ++ ; } else ans += A ; } } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { char [ ] [ ] arr = { { ' . ' , ' . ' , ' * ' } , { ' . ' , ' * ' , ' * ' } } ; int A = 2 , B = 10 ; minCost ( arr , A , B ) ; } }
import java . util . * ; class GFG { static void calcTotalTime ( String path ) { int time = 0 ; int x = 0 , y = 0 ; Set < String > s = new HashSet < > ( ) ; for ( int i = 0 ; i < path . length ( ) ; i ++ ) { int p = x ; int q = y ; if ( path . charAt ( i ) == ' N ' ) y ++ ; else if ( path . charAt ( i ) == ' S ' ) y -- ; else if ( path . charAt ( i ) == ' E ' ) x ++ ; else if ( path . charAt ( i ) == ' W ' ) x -- ; String o = ( p + x ) + " ▁ " + ( q + y ) ; if ( ! s . contains ( o ) ) { time += 2 ; s . add ( o ) ; } else time += 1 ; } System . out . println ( time ) ; } public static void main ( String [ ] args ) { String path = " NSE " ; calcTotalTime ( path ) ; } }
import java . util . Vector ; class GFG { static void countInversions ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { v . add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int itr = v . indexOf ( arr [ i ] ) ; ans += itr ; v . remove ( itr ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 } ; int n = arr . length ; countInversions ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int binarysearch ( int array [ ] , int element ) { int left = 1 ; int right = array . length - 1 ; while ( left <= right ) { int mid = ( int ) ( left + right / 2 ) ; if ( array [ mid ] == element ) { return mid ; } if ( mid - 1 > 0 && array [ mid ] > element && array [ mid - 1 ] < element ) { return mid ; } else if ( array [ mid ] < element ) { left = mid + 1 ; } else { right = mid - 1 ; } } return 1 ; } static void countDistinct ( int arr [ ] , int L , int R ) { int count = 0 ; int pref [ ] = new int [ arr . length + 1 ] ; for ( int i = 1 ; i <= arr . length ; ++ i ) { count += arr [ i - 1 ] ; pref [ i ] = count ; } int left = binarysearch ( pref , L ) ; int right = binarysearch ( pref , R ) ; System . out . println ( ( right - left ) + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 7 , 1 , 8 } ; int L = 3 ; int R = 7 ; countDistinct ( arr , L , R ) ; } }
public class GFG { public static void countSubstrings ( String s ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = i ; j < n ; j ++ ) { if ( s . charAt ( j ) == ' a ' ) cnt ++ ; else if ( s . charAt ( j ) == ' c ' ) cnt -- ; if ( cnt > 0 ) { ans ++ ; } } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { String S = " abccaab " ; countSubstrings ( S ) ; } }
import java . io . * ; class GFG { static boolean isValid ( int [ ] weight , int n , int D , int mx ) { int st = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += weight [ i ] ; if ( sum > mx ) { st ++ ; sum = weight [ i ] ; } if ( st > D ) return false ; } return true ; } static void shipWithinDays ( int [ ] weight , int D , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += weight [ i ] ; int s = weight [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = Math . max ( s , weight [ i ] ) ; } int e = sum ; int res = - 1 ; while ( s <= e ) { int mid = s + ( e - s ) / 2 ; if ( isValid ( weight , n , D , mid ) ) { res = mid ; e = mid - 1 ; } else s = mid + 1 ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int [ ] weight = { 9 , 8 , 10 } ; int D = 3 ; int N = weight . length ; shipWithinDays ( weight , D , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void createGraph ( HashMap < Integer , ArrayList < Integer > > adj , int paths [ ] [ ] , int A [ ] , int N , int E ) { for ( int i = 0 ; i < E ; i ++ ) { if ( A [ paths [ i ] [ 0 ] ] != 1 && A [ paths [ i ] [ 1 ] ] != 1 ) { ArrayList < Integer > list = adj . getOrDefault ( paths [ i ] [ 0 ] , new ArrayList < > ( ) ) ; list . add ( paths [ i ] [ 1 ] ) ; adj . put ( paths [ i ] [ 0 ] , list ) ; } } } static int minPath ( int paths [ ] [ ] , int A [ ] , int N , int E ) { if ( A [ 0 ] == 1 A [ N - 1 ] == 1 ) return - 1 ; HashMap < Integer , ArrayList < Integer > > adj = new HashMap < > ( ) ; createGraph ( adj , paths , A , N , E ) ; boolean visited [ ] = new boolean [ N ] ; int dist [ ] = new int [ N ] ; dist [ 0 ] = 0 ; ArrayDeque < Integer > q = new ArrayDeque < > ( ) ; q . addLast ( 0 ) ; visited [ 0 ] = true ; while ( ! q . isEmpty ( ) ) { int temp = q . removeFirst ( ) ; for ( int x : adj . getOrDefault ( temp , new ArrayList < > ( ) ) ) { if ( ! visited [ x ] ) { q . addLast ( x ) ; visited [ x ] = true ; dist [ x ] = dist [ temp ] + 1 ; } } } if ( ! visited [ N - 1 ] ) return - 1 ; else return dist [ N - 1 ] ; } public static void main ( String [ ] args ) { int N = 5 , E = 5 ; int A [ ] = { 0 , 1 , 0 , 0 , 0 } ; int paths [ ] [ ] = { { 0 , 1 } , { 0 , 2 } , { 1 , 4 } , { 2 , 3 } , { 3 , 4 } } ; System . out . print ( minPath ( paths , A , N , E ) ) ; } }
import java . lang . * ; class GFG { public static int findSum ( int arr [ ] , int N , int pt ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += Math . abs ( arr [ i ] - pt ) ; } return sum ; } public static void findPoint ( int arr [ ] , int N , int K ) { int left ; if ( N % 2 != 0 ) { left = arr [ N / 2 ] ; } else { left = arr [ N / 2 - 1 ] + 1 ; } int right = arr [ N - 1 ] ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { System . out . println ( mid ) ; return ; } else if ( K < temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } left = arr [ 0 ] ; right = arr [ N / 2 ] - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; int temp = findSum ( arr , N , mid ) ; if ( temp == K ) { System . out . println ( mid ) ; return ; } else if ( K > temp ) { right = mid - 1 ; } else { left = mid + 1 ; } } System . out . println ( " - 1" ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 6 , 7 , 11 } ; int K = 18 ; int N = arr . length ; findPoint ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void findArray ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int ans = - 1 , old_c = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { int curr_c = 0 ; for ( int k = j ; k < n ; k ++ ) { if ( arr [ k ] == arr [ j ] ) { curr_c ++ ; } } ; if ( curr_c == old_c ) { if ( arr [ j ] < ans ) ans = arr [ j ] ; } if ( curr_c > old_c ) { ans = arr [ j ] ; old_c = curr_c ; } } } v . add ( ans ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 2 , 25 , 10 , 5 , 10 , 3 , 10 , 5 } ; int size = arr . length ; findArray ( arr , size ) ; } }
import java . util . * ; class GFG { static void prodEquilibrium ( int arr [ ] , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } int left = 1 ; int right = product ; for ( int i = 0 ; i < N ; i ++ ) { left = left * arr [ i ] ; right = right / arr [ i ] ; if ( left == right ) { System . out . print ( i + 1 + "NEW_LINE"); return ; } } System . out . print ( - 1 + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 1 } ; int N = arr . length ; prodEquilibrium ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void convert ( int arr [ ] , int n ) { int brr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; for ( int i = 0 ; i < n ; i ++ ) { int l = 0 , r = n - 1 , mid ; while ( l <= r ) { mid = ( l + r ) / 2 ; if ( brr [ mid ] == arr [ i ] ) { System . out . print ( mid + " ▁ " ) ; break ; } else if ( brr [ mid ] < arr [ i ] ) { l = mid + 1 ; } else { r = mid - 1 ; } } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 15 , 12 , 11 , 50 } ; int N = arr . length ; convert ( arr , N ) ; } }
import java . util . HashMap ; public class GFG { static int findIndex ( int arr [ ] , int N ) { int maxValue = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { maxValue = Math . max ( maxValue , arr [ i ] ) ; } HashMap < Integer , Integer > St = new HashMap < > ( ) ; for ( int i = 1 ; i <= maxValue ; i ++ ) { St . put ( i , St . getOrDefault ( i , 0 ) + 1 ) ; } if ( St . containsKey ( 1 ) ) { St . remove ( 1 ) ; } for ( int i = 2 ; i <= Math . sqrt ( maxValue ) ; i ++ ) { int j = 2 ; while ( ( i * j ) <= maxValue ) { if ( St . containsKey ( i * j ) ) { St . remove ( i * j ) ; } j ++ ; } } int LeftCount = 0 ; int Prefix [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Prefix [ i ] = LeftCount ; if ( St . containsKey ( arr [ i ] ) ) { LeftCount ++ ; } } int RightCount = 0 ; int Suffix [ ] = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { Suffix [ i ] = RightCount ; if ( St . containsKey ( arr [ i ] ) ) { RightCount ++ ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( Prefix [ i ] == Suffix [ i ] ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 7 , 5 , 10 , 1 , 8 } ; int N = arr . length ; System . out . println ( findIndex ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int getCountPairs ( int arr [ ] , int N , int S ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { for ( int j = i + 1 ; j < arr . length ; j ++ ) { if ( ( arr [ i ] + arr [ j ] ) == S ) count ++ ; } } return count ; } public static int countPairs ( int arr [ ] , int N ) { int count = 0 ; HashSet < Integer > S = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) S . add ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
import java . util . Vector ; public class GFG { static int MAX = ( int ) 1e5 ; public static class pair { long first ; long second ; pair ( long first , long second ) { this . first = first ; this . second = second ; } } static long power ( long x , long y ) { long res = 1 ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } static void SieveOfEratosthenes ( Vector < pair > v ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } int num = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) { if ( prime [ i ] ) { v . add ( new pair ( i , num ) ) ; num ++ ; } } } static long countIntegers ( long n ) { if ( n < 16 ) { return 0 ; } Vector < pair > v = new Vector < > ( ) ; SieveOfEratosthenes ( v ) ; int low = 0 ; int high = v . size ( ) - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; long curr = power ( v . get ( mid ) . first , 4 ) ; long prev = power ( v . get ( mid - 1 ) . first , 4 ) ; if ( curr == n ) { return v . get ( mid ) . second ; } else if ( curr > n && prev <= n ) { return v . get ( mid - 1 ) . second ; } else if ( curr > n ) { high = mid - 1 ; } else { low = mid + 1 ; } } return 0 ; } public static void main ( String [ ] args ) { long N = 100 ; System . out . println ( countIntegers ( N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) == Math . floor ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } static void printArray ( ArrayList < String > res ) { Collections . sort ( res ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } } static void sortedStrings ( String S [ ] , int N ) { HashMap < Character , Integer > freq = new HashMap < > ( ) ; ArrayList < String > res = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { String st = " " ; for ( int j = 0 ; j < S [ i ] . length ( ) ; j ++ ) { freq . put ( S [ i ] . charAt ( j ) , freq . getOrDefault ( S [ i ] . charAt ( j ) , 0 ) + 1 ) ; } for ( char ch : freq . keySet ( ) ) { if ( isPowerOfTwo ( freq . get ( ch ) ) ) { for ( int j = 0 ; j < freq . get ( ch ) ; j ++ ) { st += ch ; } } } freq . clear ( ) ; if ( st . length ( ) == 0 ) continue ; char myCharArr [ ] = st . toCharArray ( ) ; Arrays . sort ( myCharArr ) ; String ns = " " ; for ( int j = myCharArr . length - 1 ; j >= 0 ; -- j ) ns += myCharArr [ j ] ; res . add ( ns ) ; } printArray ( res ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " aaacbb " , " geeks " , " aaa " } ; int N = arr . length ; sortedStrings ( arr , N ) ; } }
import java . util . * ; class GFG { static void replaceCharacters ( String s , char [ ] [ ] p ) { int n = s . length ( ) , k = p . length ; char [ ] arr = new char [ 26 ] ; char [ ] brr = new char [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - ' a ' ] = s . charAt ( i ) ; brr [ s . charAt ( i ) - ' a ' ] = s . charAt ( i ) ; } for ( int j = 0 ; j < k ; j ++ ) { char a = p [ j ] [ 0 ] , b = p [ j ] [ 1 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( arr [ i ] == a ) { brr [ i ] = b ; } } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( brr [ s . charAt ( i ) - ' a ' ] ) ; } } public static void main ( String [ ] args ) { String S = " aabbgg " ; char [ ] [ ] P = { { ' a ' , ' b ' } , { ' b ' , ' g ' } , { ' g ' , ' a ' } } ; replaceCharacters ( S , P ) ; } }
class GFG { static int MAX = 100001 ; static void SieveOfEratosthenes ( boolean prime [ ] ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static void smallestSemiPrime ( int n ) { boolean [ ] prime = new boolean [ MAX ] ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } SieveOfEratosthenes ( prime ) ; int num1 = n + 1 ; while ( prime [ num1 ] != true ) { num1 ++ ; } int num2 = num1 + n ; while ( prime [ num2 ] != true ) { num2 ++ ; } System . out . print ( num1 * num2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; smallestSemiPrime ( N ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void productOfTwoPerfectCubes ( int N ) { Map < Integer , Integer > cubes = new HashMap < > ( ) ; for ( int i = 1 ; i * i * i <= N ; i ++ ) cubes . put ( i * i * i , i ) ; for ( Map . Entry < Integer , Integer > itr : cubes . entrySet ( ) ) { int firstNumber = itr . getKey ( ) ; if ( N % itr . getKey ( ) == 0 ) { int secondNumber = N / itr . getKey ( ) ; if ( cubes . containsKey ( secondNumber ) ) { System . out . println ( " Yes " ) ; return ; } } } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 216 ; productOfTwoPerfectCubes ( N ) ; } }
import java . lang . * ; class GFG { public static void productOfTwoPerfectCubes ( double N ) { double cube_root ; cube_root = Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { System . out . println ( " Yes " ) ; return ; } else { System . out . println ( " No " ) ; return ; } } public static void main ( String args [ ] ) { double N = 216 ; productOfTwoPerfectCubes ( N ) ; } }
import java . util . HashSet ; public class GFG { static void printNumberOfPairs ( int N ) { HashSet < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { int x = i ; while ( x <= N ) { x *= i ; if ( x <= N ) { st . add ( x ) ; } } } System . out . println ( st . size ( ) ) ; } public static void main ( String args [ ] ) { int N = 10000 ; printNumberOfPairs ( N ) ; } }
import java . io . * ; class GFG { public static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void maximumProduct ( String [ ] words ) { int [ ] bits = new int [ words . length ] ; for ( int i = 0 ; i < words . length ; i ++ ) { for ( int j = 0 ; j < words [ i ] . length ( ) ; j ++ ) { bits [ i ] = bits [ i ] | 1 << ( words [ i ] . charAt ( j ) - ' a ' ) ; } } int result = 0 ; for ( int i = 0 ; i < bits . length ; i ++ ) { for ( int j = i + 1 ; j < bits . length ; j ++ ) { if ( ( bits [ i ] & bits [ j ] ) == 0 ) { int L = countSetBits ( bits [ i ] ) ; int R = countSetBits ( bits [ j ] ) ; result = Math . max ( L * R , result ) ; } } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " abcw " , " baz " , " foo " , " bar " , " xtfn " , " abcdef " } ; maximumProduct ( arr ) ; } }
import java . util . * ; class GFG { static boolean palindrome ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; while ( st < ed ) { if ( str . charAt ( st ) == str . charAt ( ed ) ) { st ++ ; ed -- ; } else return false ; } return true ; } static void printReverse ( String sentence ) { ArrayList < String > newlist = new ArrayList < String > ( ) ; ArrayList < String > lis = new ArrayList < String > ( ) ; String temp = " " ; for ( char x : sentence . toCharArray ( ) ) { if ( x == ' ▁ ' ) { lis . add ( temp ) ; temp = " " ; } else temp += x ; } lis . add ( temp ) ; for ( String x : lis ) { if ( palindrome ( x ) ) newlist . add ( x ) ; } Collections . reverse ( newlist ) ; int j = 0 ; for ( int i = 0 ; i < lis . size ( ) ; i ++ ) { if ( palindrome ( lis . get ( i ) ) ) { lis . set ( i , newlist . get ( j ) ) ; j = j + 1 ; } } for ( String x : lis ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { String sentence = " mom ▁ and ▁ dad ▁ went ▁ to ▁ eye ▁ hospital " ; printReverse ( sentence ) ; } }
class GFG { static int minCount ( int A [ ] , int B [ ] , int N ) { int i = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] == B [ j ] ) { i ++ ; } } return N - i ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = { 1 , 5 , 2 , 3 , 4 } ; int N = A . length ; System . out . println ( minCount ( A , B , N ) ) ; } }
import java . util . * ; class GFG { static boolean possible ( int A [ ] , int N , int mid , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += ( A [ i ] - 1 ) / mid ; } return count <= K ; } static int minimumMaximum ( int A [ ] , int N , int K ) { int lo = 1 ; Arrays . sort ( A ) ; int hi = A [ N - 1 ] ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( possible ( A , N , mid , K ) ) { hi = mid ; } else { lo = mid + 1 ; } } return hi ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 4 ; int N = arr . length ; System . out . println ( minimumMaximum ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void maximumNumber ( int arr [ ] , int N , int K ) { int minimum = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int ans = minimum + K ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . abs ( arr [ i ] - ans ) > K ) { ans = - 1 ; break ; } } System . out . print ( ans ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 5 } ; int K = 2 ; int N = arr . length ; maximumNumber ( arr , N , K ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static void sumOfPrimeSquare ( int n ) { int i = 0 ; ArrayList < Integer > squares = new ArrayList < Integer > ( ) ; while ( i * i < n ) { squares . add ( i * i ) ; i ++ ; } boolean flag = false ; for ( i = 0 ; i < squares . size ( ) ; i ++ ) { int difference = n - squares . get ( i ) ; if ( isPrime ( difference ) ) { flag = true ; break ; } } if ( flag ) { System . out . print ( " Yes " ) ; } else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int N = 27 ; sumOfPrimeSquare ( N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static boolean checkExists ( int arr [ ] , int N , int M , int K ) { int count = 0 ; for ( int i = 0 ; i < N - M ; i ++ ) { if ( arr [ i ] == arr [ i + M ] ) count ++ ; else count = 0 ; if ( count == M * ( K - 1 ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 2 , 1 , 1 , 1 , 3 } ; int M = 2 , K = 2 ; int N = arr . length ; if ( checkExists ( arr , N , M , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int CommonDigits ( int N , int M ) { int count = 0 ; int freq1 [ ] = new int [ 10 ] ; int freq2 [ ] = new int [ 10 ] ; while ( N > 0 ) { freq1 [ N % 10 ] ++ ; N = N / 10 ; } while ( M > 0 ) { freq2 [ M % 10 ] ++ ; M = M / 10 ; } for ( int i = 0 ; i < 10 ; i ++ ) { if ( freq1 [ i ] > 0 & freq2 [ i ] > 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 748294 ; int M = 34298156 ; System . out . print ( CommonDigits ( N , M ) ) ; } }
import java . util . * ; class GFG { static int CountPairs ( int [ ] arr , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( i * j ) == ( arr [ i ] * arr [ j ] ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 3 , 2 , 6 } ; int N = arr . length ; System . out . println ( CountPairs ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void maxRemainder ( int A [ ] , int N , int K ) { HashSet < Integer > even = new HashSet < > ( ) ; TreeSet < Integer > odd = new TreeSet < > ( ) ; for ( int num : A ) { if ( num % 2 == 0 ) even . add ( num % K ) ; else odd . add ( num % K ) ; } int max_rem = 0 ; for ( int x : even ) { int y = K - 1 - x ; if ( odd . floor ( y ) != null ) max_rem = Math . max ( max_rem , x + odd . floor ( y ) ) ; } System . out . print ( max_rem ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 11 , 6 , 7 } ; int N = arr . length ; int K = 7 ; maxRemainder ( arr , N , K ) ; } }
import java . util . ArrayList ; class GFG { static int distance ; private static void dfs ( int src , ArrayList < ArrayList < Integer > > Adj , ArrayList < Boolean > visited , int level ) { visited . set ( src , true ) ; distance = Math . max ( distance , level ) ; for ( int child : Adj . get ( src ) ) { if ( ( child != src ) && ( visited . get ( child ) == false ) ) { dfs ( child , Adj , visited , level + 1 ) ; } } visited . set ( src , false ) ; } private static int maximumLength ( int [ ] [ ] mat , int v ) { distance = 0 ; int N = mat [ 0 ] . length ; ArrayList < Boolean > visited = new ArrayList < Boolean > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { visited . add ( false ) ; } ArrayList < ArrayList < Integer > > Adj = new ArrayList < ArrayList < Integer > > ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { Adj . add ( new ArrayList < Integer > ( ) ) ; } int i , j ; for ( i = 0 ; i < mat [ 0 ] . length ; i ++ ) { for ( j = 0 ; j < mat . length ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { Adj . get ( i ) . add ( j ) ; } } } dfs ( v , Adj , visited , 0 ) ; return distance ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 1 , 0 , 0 } , { 1 , 0 , 1 , 1 } , { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } } ; int v = 2 ; System . out . print ( maximumLength ( mat , v ) ) ; } }
class GFG { static int findLength ( String s , char i , char j ) { char required = i ; int length = 0 ; for ( int k = 0 ; k < s . length ( ) ; k ++ ) { char curr = s . charAt ( k ) ; if ( curr == required ) { length += 1 ; if ( required == i ) required = j ; else required = i ; } } return length ; } static int minimumDeletions ( String S ) { int len = 0 ; int n = S . length ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = i + 1 ; j < 26 ; j ++ ) { int newLen = findLength ( S , ( char ) ( i + 97 ) , ( char ) ( j + 97 ) ) ; len = Math . max ( len , newLen ) ; } } return n - len ; } public static void main ( String [ ] args ) { String S = " adebbeeaebd " ; System . out . print ( minimumDeletions ( S ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class Main { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int A [ ] , int B [ ] , int N ) { int prod = 1 ; for ( int i = 0 ; i < N ; i ++ ) prod *= A [ i ] ; int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int pp = prod / A [ i ] ; result += B [ i ] * inv ( pp , A [ i ] ) * pp ; } return result % prod ; } static int product ( int a [ ] ) { int ans = 1 ; for ( int i = 0 ; i < a . length ; i ++ ) { ans *= a [ i ] ; } return ans ; } public static void findSmallestInteger ( int A [ ] , int B [ ] , int P ) { int Y = findMinX ( A , B , A . length ) ; int M = product ( A ) ; int K = ( int ) Math . ceil ( ( ( double ) P - ( double ) Y ) / ( double ) M ) ; int X = Y + K * M ; System . out . println ( X ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 3 , 1 } ; int P = 72 ; findSmallestInteger ( A , B , P ) ; } }
class GFG { public static boolean intersection ( int X1 , int Y1 , double R1 , int X2 , int Y2 , double R2 , int X3 , int Y3 , double R3 ) { double d12 = Math . sqrt ( ( X1 - X2 ) * ( X1 - X2 ) + ( Y1 - Y2 ) * ( Y1 - Y2 ) ) ; double d13 = Math . sqrt ( ( X1 - X3 ) * ( X1 - X3 ) + ( Y1 - Y3 ) * ( Y1 - Y3 ) ) ; double d23 = Math . sqrt ( ( X2 - X3 ) * ( X2 - X3 ) + ( Y2 - Y3 ) * ( Y2 - Y3 ) ) ; if ( ( R1 + R2 < d12 ) || ( R1 + R3 < d13 ) || ( R2 + R3 < d23 ) ) { return false ; } else { if ( Math . abs ( R1 - R2 ) >= d12 ) { if ( R1 < R2 ) { return R1 + R3 >= d13 ; } else { return R2 + R3 >= d23 ; } } else if ( Math . abs ( R1 - R3 ) >= d13 ) { if ( R1 < R3 ) { return R1 + R2 >= d12 ; } else { return R2 + R3 >= d23 ; } } else if ( Math . abs ( R2 - R3 ) >= d23 ) { if ( R2 < R3 ) { return R1 + R2 >= d12 ; } else { return R1 + R3 >= d13 ; } } else { double x121 , y121 , x122 , y122 , x131 , y131 , x132 , y132 , x231 , y231 , x232 , y232 , a , b ; a = ( R1 * R1 - R2 * R2 ) / ( 2 * d12 * d12 ) ; b = Math . sqrt ( 2 * ( R1 * R1 + R2 * R2 ) / ( d12 * d12 ) - ( R1 * R1 - R2 * R2 ) * ( R1 * R1 - R2 * R2 ) / ( Math . pow ( d12 , 4 ) ) - 1 ) / 2 ; x121 = ( X1 + X2 ) / 2.0 + a * ( X2 - X1 ) + b * ( Y2 - Y1 ) ; y121 = ( Y1 + Y2 ) / 2.0 + a * ( Y2 - Y1 ) + b * ( X1 - X2 ) ; if ( R3 >= Math . sqrt ( ( x121 - X3 ) * ( x121 - X3 ) + ( y121 - Y3 ) * ( y121 - Y3 ) ) ) { return true ; } x122 = ( X1 + X2 ) / 2.0 + a * ( X2 - X1 ) - b * ( Y2 - Y1 ) ; y122 = ( Y1 + Y2 ) / 2.0 + a * ( Y2 - Y1 ) - b * ( X1 - X2 ) ; if ( R3 >= Math . sqrt ( ( x122 - X3 ) * ( x122 - X3 ) + ( y122 - Y3 ) * ( y122 - Y3 ) ) ) { return true ; } a = ( R1 * R1 - R3 * R3 ) / ( 2 * d13 * d13 ) ; b = Math . sqrt ( 2 * ( R1 * R1 + R3 * R3 ) / ( d13 * d13 ) - ( R1 * R1 - R3 * R3 ) * ( R1 * R1 - R3 * R3 ) / ( Math . pow ( d13 , 4 ) ) - 1 ) / 2 ; x131 = ( X1 + X3 ) / 2.0 + a * ( X3 - X1 ) + b * ( Y3 - Y1 ) ; y131 = ( Y1 + Y3 ) / 2.0 + a * ( Y3 - Y1 ) + b * ( X1 - X3 ) ; if ( R2 >= Math . sqrt ( ( x131 - X2 ) * ( x131 - X2 ) + ( y131 - Y2 ) * ( y131 - Y2 ) ) ) { return true ; } x132 = ( X1 + X3 ) / 2.0 + a * ( X3 - X1 ) - b * ( Y3 - Y1 ) ; y132 = ( Y1 + Y3 ) / 2.0 + a * ( Y3 - Y1 ) - b * ( X1 - X3 ) ; if ( R2 >= Math . sqrt ( ( x132 - X2 ) * ( x132 - X2 ) + ( y132 - Y2 ) * ( y132 - Y2 ) ) ) { return true ; } a = ( R2 * R2 - R3 * R3 ) / ( 2 * d23 * d23 ) ; b = Math . sqrt ( 2 * ( R2 * R2 + R3 * R3 ) / ( d23 * d23 ) - ( R2 * R2 - R3 * R3 ) * ( R2 * R2 - R3 * R3 ) / ( Math . pow ( d23 , 4 ) ) - 1 ) / 2 ; x231 = ( X2 + X3 ) / 2.0 + a * ( X3 - X2 ) + b * ( Y3 - Y2 ) ; y231 = ( Y2 + Y3 ) / 2.0 + a * ( Y3 - Y2 ) + b * ( X2 - X3 ) ; if ( R1 >= Math . sqrt ( ( x231 - X1 ) * ( x231 - X1 ) + ( y231 - Y1 ) * ( y231 - Y1 ) ) ) { return true ; } x232 = ( X2 + X3 ) / 2.0 + a * ( X3 - X2 ) - b * ( Y3 - Y2 ) ; y232 = ( Y2 + Y3 ) / 2.0 + a * ( Y3 - Y2 ) - b * ( X2 - X3 ) ; return R1 >= Math . sqrt ( ( x232 - X1 ) * ( x232 - X1 ) + ( y232 - Y1 ) * ( y232 - Y1 ) ) ; } } } public static boolean isGood ( double t , int N , int [ ] X , int [ ] Y , int [ ] V ) { if ( N >= 3 ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { if ( ! intersection ( X [ i ] , Y [ i ] , t * V [ i ] , X [ j ] , Y [ j ] , t * V [ j ] , X [ k ] , Y [ k ] , t * V [ k ] ) ) return false ; } } } return true ; } else { return Math . sqrt ( ( X [ 0 ] - X [ 1 ] ) * ( X [ 0 ] - X [ 1 ] ) + ( Y [ 0 ] - Y [ 1 ] ) * ( Y [ 0 ] - Y [ 1 ] ) ) <= t * ( V [ 0 ] + V [ 1 ] ) ; } } public static void binarySearch ( int N , int [ ] X , int [ ] Y , int [ ] V ) { double tl = 0.0 , tu = 100000.0 , t ; for ( int i = 0 ; i < 1000 ; i ++ ) { t = ( tl + tu ) / 2.0 ; if ( isGood ( t , N , X , Y , V ) ) { tu = t ; } else { tl = t ; } } System . out . println ( tu ) ; } public static void main ( String [ ] args ) { int N = 4 ; int [ ] X = { 1 , - 3 , - 1 , 2 } ; int [ ] Y = { 2 , 4 , - 2 , - 2 } ; int [ ] V = { 3 , 2 , 4 , 5 } ; binarySearch ( N , X , Y , V ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minJumps ( int arr [ ] , int jump [ ] , int N ) { int jumps = 0 ; int temp [ ] = new int [ 1000 ] ; int vect [ ] [ ] = new int [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) { vect [ i ] [ 0 ] = arr [ i ] ; vect [ i ] [ 1 ] = i + 1 ; } for ( int i = 0 ; i < N ; i ++ ) { temp [ arr [ i ] ] = jump [ i ] ; } Arrays . sort ( vect , ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] - b [ 0 ] ; return a [ 1 ] - b [ 1 ] ; } ) ; for ( int i = 1 ; i < N ; i ++ ) { while ( vect [ i ] [ 1 ] <= vect [ i - 1 ] [ 1 ] ) { vect [ i ] [ 0 ] = vect [ i ] [ 0 ] ; vect [ i ] [ 1 ] = vect [ i ] [ 1 ] + temp [ vect [ i ] [ 0 ] ] ; jumps ++ ; } } System . out . println ( jumps ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int jump [ ] = { 1 , 1 , 1 } ; int N = arr . length ; minJumps ( arr , jump , N ) ; } }
class GFG { static void numberOfGates ( String s ) { int N = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' . ' || s . charAt ( i ) == ' + ' || s . charAt ( i ) == '1' ) { ans ++ ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " ( 1 - A ) . B + C " ; numberOfGates ( S ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumSwaps ( int arr [ ] , int N ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != i % 2 ) { if ( i % 2 == 0 ) { even ++ ; } else { odd ++ ; } } } if ( even != odd ) { System . out . println ( - 1 ) ; } else { System . out . println ( even ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 7 , 6 } ; int N = arr . length ; minimumSwaps ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int smallestSubstring ( String a ) { ArrayList < String > a1 = new ArrayList < > ( ) ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { for ( int j = i + 1 ; j <= a . length ( ) ; j ++ ) { if ( i != j ) a1 . add ( a . substring ( i , j ) ) ; } } TreeMap < String , Integer > a2 = new TreeMap < > ( ) ; for ( String s : a1 ) a2 . put ( s , a2 . getOrDefault ( s , 0 ) + 1 ) ; ArrayList < String > freshlist = new ArrayList < > ( ) ; for ( String s : a2 . keySet ( ) ) { if ( a2 . get ( s ) == 1 ) freshlist . add ( s ) ; } TreeMap < String , Integer > dictionary = new TreeMap < > ( ) ; for ( String s : freshlist ) { dictionary . put ( s , s . length ( ) ) ; } ArrayList < Integer > newlist = new ArrayList < > ( ) ; for ( String s : dictionary . keySet ( ) ) newlist . add ( dictionary . get ( s ) ) ; int ans = Integer . MAX_VALUE ; for ( int i : newlist ) ans = Math . min ( ans , i ) ; return ans == Integer . MAX_VALUE ? 0 : ans ; } public static void main ( String [ ] args ) { String S = " ababaabba " ; System . out . println ( smallestSubstring ( S ) ) ; } }
import java . util . * ; class GFG { static void CalculateValues ( int N ) { int A = 0 , B = 0 , C = 0 ; for ( C = 0 ; C < N / 7 ; C ++ ) { for ( B = 0 ; B < N / 5 ; B ++ ) { A = N - 7 * C - 5 * B ; if ( A >= 0 && A % 3 == 0 ) { System . out . print ( " A ▁ = ▁ " + A / 3 + " , ▁ B ▁ = ▁ " + B + " , ▁ C ▁ = ▁ " + C ) ; return ; } } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 19 ; CalculateValues ( 19 ) ; } }
import java . util . * ; class GFG { static int mininsert ( int arr [ ] , int K , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = Math . min ( arr [ i ] , arr [ i + 1 ] ) ; int b = Math . max ( arr [ i ] , arr [ i + 1 ] ) ; while ( K * a < b ) { a *= K ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 25 , 21 } ; int K = 2 ; int N = arr . length ; System . out . print ( mininsert ( arr , K , N ) ) ; } }
import java . util . * ; class GFG { static void print_triplet ( int arr [ ] , int n ) { for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( arr [ i - 1 ] < arr [ i ] && arr [ i ] > arr [ i + 1 ] ) { System . out . print ( i - 1 + " ▁ " + i + " ▁ " + ( i + 1 ) ) ; return ; } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 5 , 2 , 1 , 6 } ; int N = arr . length ; print_triplet ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void findConcatenatedNumbers ( int [ ] a , int [ ] b ) { boolean ans = true ; int n1 = a . length ; int n2 = b . length ; int cnt [ ] = new int [ 100000 ] ; for ( int i = 0 ; i < n1 ; i ++ ) { cnt [ a [ i ] ] = 1 ; } for ( int i = 0 ; i < n2 ; i ++ ) { int left = b [ i ] ; int right = 0 ; int mul = 1 ; while ( left > 9 ) { right += ( left % 10 ) * mul ; left /= 10 ; mul *= 10 ; if ( cnt [ left ] == 1 && cnt [ right ] == 1 ) { ans = false ; System . out . print ( b [ i ] + " ▁ " ) ; } } } if ( ans ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 34 , 4 , 5 } ; int [ ] b = { 26 , 24 , 345 , 4 , 22 } ; findConcatenatedNumbers ( a , b ) ; } }
import java . util . * ; class GFG { static int ans = 0 ; static ArrayList < Integer > [ ] adj = new ArrayList [ ( int ) ( 1e5 ) + 1 ] ; static { for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new ArrayList < > ( ) ; } static void dfsUtil ( int u , int par , int depth ) { for ( int it : adj [ u ] ) { if ( it != par ) { dfsUtil ( it , u , depth + 1 ) ; } } ans += depth ; } static void dfs ( int u , int par , int depth ) { dfsUtil ( u , par , depth ) ; System . out . print ( ans + "NEW_LINE"); } static void countPairs ( int [ ] [ ] edges ) { for ( int i = 0 ; i < edges . length ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } dfs ( 1 , 1 , 1 ) ; } public static void main ( String [ ] args ) { int [ ] [ ] edges = { { 1 , 2 } , { 1 , 3 } , { 2 , 4 } } ; countPairs ( edges ) ; } }
import java . util . ArrayList ; import java . util . LinkedList ; import java . util . Queue ; class GFG { static class TreeNode { int val ; TreeNode left , right ; public TreeNode ( int key ) { val = key ; left = right = null ; } } static int convertBinaryToDecimal ( ArrayList < Integer > arr ) { int ans = 0 ; for ( int i : arr ) ans = ( ans << 1 ) | i ; return ans ; } static void decimalEquilvalentAtEachLevel ( TreeNode root ) { int ans = 0 ; Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; while ( true ) { int length = que . size ( ) ; if ( length == 0 ) break ; ArrayList < Integer > eachLvl = new ArrayList < > ( ) ; while ( length > 0 ) { TreeNode temp = que . poll ( ) ; eachLvl . add ( temp . val ) ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; length -= 1 ; } ans += convertBinaryToDecimal ( eachLvl ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 0 ) ; root . left = new TreeNode ( 1 ) ; root . right = new TreeNode ( 0 ) ; root . left . left = new TreeNode ( 0 ) ; root . left . right = new TreeNode ( 1 ) ; root . right . left = new TreeNode ( 1 ) ; root . right . right = new TreeNode ( 1 ) ; decimalEquilvalentAtEachLevel ( root ) ; } }
class GFG { static int minIncr ( int [ ] arr , int n ) { int forEven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 1 ) { if ( ( arr [ i ] % 2 ) == 0 ) forEven += 1 ; } else { if ( arr [ i ] % 2 == 1 ) forEven += 1 ; } } return Math . min ( forEven , n - forEven ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 8 , 9 , 5 } ; int n = arr . length ; System . out . println ( minIncr ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void hasArrayTwoPairs ( int nums [ ] , int n , int target ) { Arrays . sort ( nums ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = target - nums [ i ] ; int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = low + ( ( high - low ) / 2 ) ; if ( nums [ mid ] > x ) { high = mid - 1 ; } else if ( nums [ mid ] < x ) { low = mid + 1 ; } else { if ( mid == i ) { if ( ( mid - 1 >= 0 ) && nums [ mid - 1 ] == x ) { System . out . print ( nums [ i ] + " , ▁ " ) ; System . out . print ( nums [ mid - 1 ] ) ; return ; } if ( ( mid + 1 < n ) && nums [ mid + 1 ] == x ) { System . out . print ( nums [ i ] + " , ▁ " ) ; System . out . print ( nums [ mid + 1 ] ) ; return ; } break ; } else { System . out . print ( nums [ i ] + " , ▁ " ) ; System . out . print ( nums [ mid ] ) ; return ; } } } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int X = - 2 ; int N = A . length ; hasArrayTwoPairs ( A , N , X ) ; } }
import java . util . * ; class GFG { static void minOperations ( int [ ] arr , int N , int [ ] Q , int M ) { Map < Integer , Integer > m1 = new HashMap < Integer , Integer > ( ) ; Map < Integer , Integer > m2 = new HashMap < Integer , Integer > ( ) ; int front = 0 , rear = 0 ; for ( int i = 0 ; i < N ; i ++ ) { front += arr [ i ] ; m1 . put ( arr [ i ] , front ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { rear += arr [ i ] ; m2 . put ( arr [ i ] , rear ) ; } for ( int i = 0 ; i < M ; i ++ ) { System . out . print ( Math . min ( m1 . get ( Q [ i ] ) , m2 . get ( Q [ i ] ) ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = arr . length ; int [ ] Q = { 7 , 6 } ; int M = Q . length ; minOperations ( arr , N , Q , M ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static boolean checkAnagram ( String s1 , String s2 ) { int s2hash [ ] = new int [ 26 ] ; int s1hash [ ] = new int [ 26 ] ; int s1len = s1 . length ( ) ; int s2len = s2 . length ( ) ; if ( s1len > s2len ) return false ; int left = 0 , right = 0 ; while ( right < s1len ) { s1hash [ s1 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; right ++ ; } right -= 1 ; while ( right < s2len ) { if ( Arrays . equals ( s1hash , s2hash ) ) return true ; right ++ ; if ( right != s2len ) s2hash [ s2 . charAt ( right ) - ' a ' ] += 1 ; s2hash [ s2 . charAt ( left ) - ' a ' ] -= 1 ; left ++ ; } return false ; } public static void main ( String [ ] args ) { String s1 = " ab " ; String s2 = " bbpobac " ; if ( checkAnagram ( s1 , s2 ) ) System . out . println ( " YES " ) ; else System . out . println ( " No " ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMaxIntervals ( ArrayList < Pair > v , int n ) { int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int c = n ; for ( int j = 0 ; j < n ; j ++ ) { if ( v . get ( i ) . second < v . get ( j ) . first || v . get ( i ) . first > v . get ( j ) . second ) { c -- ; } } maxi = Math . max ( c , maxi ) ; } System . out . print ( maxi ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > arr = new ArrayList < > ( ) ; arr . add ( new Pair ( 1 , 2 ) ) ; arr . add ( new Pair ( 3 , 4 ) ) ; arr . add ( new Pair ( 2 , 5 ) ) ; int N = arr . size ( ) ; findMaxIntervals ( arr , N ) ; } }
class GFG { static void smallestNumber ( int arr [ ] , int len ) { int maxi = 0 ; for ( int i = 0 ; i < len ; i ++ ) { maxi = Math . max ( maxi , arr [ i ] ) ; } int ans = - 1 ; for ( int i = 2 ; i < maxi + 2 ; i ++ ) { boolean flag = true ; for ( int j = 0 ; j < len ; j ++ ) { if ( arr [ j ] % i == 0 ) { flag = false ; break ; } } if ( flag ) { ans = i ; break ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 6 , 9 , 2 } ; int N = arr . length ; smallestNumber ( arr , N ) ; } }
public class GFG { static void findDuplicate ( String str , int N ) { int first = 0 ; int second = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( first & ( 1 << ( str . charAt ( i ) - ' a ' ) ) ) != 0 ) { second = second | ( 1 << ( str . charAt ( i ) - ' a ' ) ) ; } else { first = first | ( 1 << ( str . charAt ( i ) - ' a ' ) ) ; } } for ( int i = 0 ; i < 26 ; i ++ ) { if ( ( ( first & ( 1 << i ) ) & ( second & ( 1 << i ) ) ) != 0 ) { System . out . print ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } static public void main ( String args [ ] ) { String str = " geeksforgeeks " ; int N = str . length ( ) ; findDuplicate ( str , N ) ; } }
import java . util . * ; import java . lang . * ; public class GFG { static void calculateQuery ( int arr [ ] , int N , int query [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; int sum_start = 0 , sum_end = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum_start += arr [ j ] ; if ( arr [ j ] == X ) break ; } for ( int j = N - 1 ; j >= 0 ; j -- ) { sum_end += arr [ j ] ; if ( arr [ j ] == X ) break ; } System . out . print ( Math . min ( sum_end , sum_start ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int queries [ ] = { 6 , 5 } ; int N = arr . length ; int M = queries . length ; calculateQuery ( arr , N , queries , M ) ; } }
import java . util . * ; class GFG { static class pair < E , P > { E first ; P second ; public pair ( E first , P second ) { this . first = first ; this . second = second ; } } @ SuppressWarnings ( { " unchecked " , " rawtypes " } ) static void calculateQuery ( int arr [ ] , int N , int query [ ] , int M ) { int prefix = 0 , suffix = 0 ; HashMap < Integer , pair > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { prefix += arr [ i ] ; mp . put ( arr [ i ] , new pair ( prefix , 0 ) ) ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { suffix += arr [ i ] ; mp . put ( arr [ i ] , new pair ( mp . get ( arr [ i ] ) . first , suffix ) ) ; } for ( int i = 0 ; i < M ; i ++ ) { int X = query [ i ] ; System . out . print ( Math . min ( ( int ) mp . get ( X ) . first , ( int ) mp . get ( X ) . second ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 30 } ; int queries [ ] = { 6 , 5 } ; int N = arr . length ; int M = queries . length ; calculateQuery ( arr , N , queries , M ) ; } }
import java . util . * ; class GFG { static void oddOneOut ( int arr [ ] , int N ) { int odd = 0 , even = 0 ; int lastOdd = 0 , lastEven = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { even ++ ; lastEven = i ; } else { odd ++ ; lastOdd = i ; } } if ( odd == 1 ) { System . out . println ( lastOdd ) ; } else { System . out . println ( lastEven ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 7 , 8 , 10 } ; int N = arr . length ; oddOneOut ( arr , N ) ; } }
import java . io . * ; class GFG { static boolean isGreaterEqual ( int N , int X ) { return ( N * ( N + 1 ) / 2 ) >= X ; } static int minimumPossible ( int X ) { for ( int i = 1 ; i <= X ; i ++ ) { if ( isGreaterEqual ( i , X ) ) return i ; } return 0 ; } public static void main ( String [ ] args ) { int X = 14 ; System . out . print ( minimumPossible ( X ) ) ; } }
import java . io . * ; class GFG { public static int Max = 1000005 ; static boolean CheckDivByAllDigits ( int number ) { int n = number ; while ( n > 0 ) { if ( n % 10 != 0 ) if ( number % ( n % 10 ) != 0 ) { return false ; } n /= 10 ; } return true ; } static void cntNumInRang ( int arr [ ] [ ] , int N ) { int prefCntDiv [ ] = new int [ Max + 1 ] ; for ( int i = 1 ; i <= Max ; i ++ ) { int ans = 0 ; if ( CheckDivByAllDigits ( i ) ) ans = 1 ; prefCntDiv [ i ] = prefCntDiv [ i - 1 ] + ans ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( ( prefCntDiv [ arr [ i ] [ 1 ] ] - prefCntDiv [ arr [ i ] [ 0 ] - 1 ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 5 } , { 12 , 14 } } ; int N = arr . length ; cntNumInRang ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class sol { public int arrayNesting ( int [ ] arr ) { int res = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == i ) { res = Math . max ( res , 1 ) ; } else { int count = 0 ; int curr_index = i ; while ( arr [ curr_index ] != curr_index ) { int next_index = arr [ curr_index ] ; arr [ curr_index ] = curr_index ; curr_index = next_index ; count ++ ; } res = Math . max ( res , count ) ; } } return res ; } } class GFG { public static void main ( String [ ] args ) { sol st = new sol ( ) ; int [ ] arr = { 5 , 4 , 0 , 3 , 1 , 6 , 2 } ; int res = st . arrayNesting ( arr ) ; System . out . println ( res ) ; } }
import java . util . * ; class GFG { static int N = 200000 ; static int prime [ ] = new int [ 20000 ] ; static int index = 0 ; static void Sieve ( ) { index = 0 ; int p [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) { p [ i ] = 1 ; } p [ 0 ] = p [ 1 ] = 0 ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( p [ i ] == 1 ) { for ( int j = i * i ; j <= N ; j += i ) { p [ j ] = 0 ; } } } for ( int i = 1 ; i < N ; i ++ ) { if ( p [ i ] == 1 ) { prime [ index ++ ] = ( int ) ( Math . pow ( i , 4 ) ) ; } } } static void countNumbers ( int L , int R ) { int Count = 0 ; for ( int i = 0 ; i < index ; i ++ ) { int p = prime [ i ] ; if ( p >= L && p <= R ) { Count ++ ; } } System . out . println ( Count ) ; } public static void main ( String [ ] args ) { int L = 16 , R = 85000 ; Sieve ( ) ; countNumbers ( L , R ) ; } }
import java . util . * ; class GFG { static int getCount ( int rows , int columns , int [ ] [ ] A ) { int res = 0 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = i + 1 ; j < rows ; j ++ ) { if ( A [ i ] [ 0 ] * A [ j ] [ 1 ] == A [ i ] [ 1 ] * A [ j ] [ 0 ] ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 4 , 8 } , { 10 , 20 } , { 15 , 30 } , { 3 , 6 } } ; int columns = 2 ; int rows = 4 ; System . out . print ( getCount ( rows , columns , A ) ) ; } }
import java . util . * ; class GFG { static void Min_Score_Index ( int N , int [ ] A ) { int [ ] Score = new int [ N ] ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( A [ i ] + i < N ) Score [ i ] = A [ i ] * Score [ A [ i ] + i ] ; else Score [ i ] = A [ i ] ; } int min_value = Integer . MAX_VALUE ; for ( int i : Score ) min_value = Math . min ( i , min_value ) ; int ind = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Score [ i ] == min_value ) ind = i ; } System . out . print ( ind ) ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; Min_Score_Index ( N , A ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void minLength ( String S , String T , int N , int M ) { String temp = " " ; int subtract = 0 ; for ( int i = 0 ; i < N ; ++ i ) { temp += S . charAt ( i ) ; if ( temp . length ( ) >= M ) { if ( T . equals ( temp . substring ( temp . length ( ) - M , temp . length ( ) ) ) ) { subtract += M ; int cnt = 0 ; while ( cnt != M ) { temp = temp . substring ( 0 , temp . length ( ) - 1 ) ; ++ cnt ; } } } } System . out . println ( ( N - subtract ) ) ; } public static void main ( String [ ] args ) { String S = " aabcbcbd " , T = " abc " ; int N = S . length ( ) ; int M = T . length ( ) ; minLength ( S , T , N , M ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maximumCount ( int [ ] arr1 , int [ ] arr2 , int s1 , int s2 ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < s1 ; i ++ ) { sum1 += arr1 [ i ] ; } for ( int j = 0 ; j < s2 ; j ++ ) { sum2 += arr2 [ j ] ; } int len = 0 ; if ( s1 >= s2 ) { len = s2 ; } else { len = s1 ; } Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; int j = 0 , k = s2 - 1 , count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( sum1 <= sum2 ) { if ( arr2 [ k ] >= arr1 [ i ] ) { int dif1 = arr1 [ j ] , dif2 = arr2 [ k ] ; sum1 -= dif1 ; sum1 += dif2 ; sum2 -= dif2 ; sum2 += dif1 ; j ++ ; k -- ; count ++ ; } else { break ; } } else { break ; } } return count ; } public static void main ( String [ ] args ) { int [ ] arr1 = new int [ ] { 1 , 3 , 2 , 4 } ; int [ ] arr2 = new int [ ] { 6 , 7 , 8 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . println ( maximumCount ( arr1 , arr2 , N , M ) ) ; } }
import java . io . * ; class GFG { static boolean isValidDigit ( int digit , int K ) { while ( K != 0 ) { if ( K % 10 == digit ) { return true ; } K = K / 10 ; } return false ; } static int noOfValidNumbers ( int K , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { int no = arr [ i ] ; boolean flag = true ; while ( no != 0 ) { int digit = no % 10 ; if ( ! isValidDigit ( digit , K ) ) { flag = false ; break ; } no = no / 10 ; } if ( flag == true ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int K = 12 ; int arr [ ] = { 1 , 12 , 1222 , 13 , 2 } ; System . out . println ( noOfValidNumbers ( K , arr ) ) ; } }
import java . util . * ; import java . lang . * ; class pair { int element , index ; pair ( int element , int index ) { this . element = element ; this . index = index ; } } class GFG { static void minimumIndex ( int [ ] arr , int [ ] Q ) { int N = arr . length ; int M = Q . length ; ArrayList < pair > storeArrIdx = new ArrayList < > ( ) ; int [ ] minIdx = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { storeArrIdx . add ( new pair ( arr [ i ] , i ) ) ; } Arrays . sort ( arr ) ; Collections . sort ( storeArrIdx , ( a , b ) -> a . element - b . element ) ; minIdx [ N - 1 ] = storeArrIdx . get ( N - 1 ) . index ; for ( int i = N - 2 ; i >= 0 ; i -- ) { minIdx [ i ] = Math . min ( minIdx [ i + 1 ] , storeArrIdx . get ( i ) . index ) ; } for ( int i = 0 ; i < M ; i ++ ) { int pos = lower_bound ( arr , Q [ i ] ) ; if ( pos == N ) { System . out . print ( " - 1" + " ▁ " ) ; continue ; } System . out . print ( minIdx [ pos ] + " ▁ " ) ; } } static int lower_bound ( int [ ] arr , int element ) { for ( int i = 0 ; i < arr . length ; i ++ ) if ( element <= arr [ i ] ) return i ; return arr . length ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 9 } ; int [ ] Q = { 7 , 10 , 0 } ; minimumIndex ( arr , Q ) ; } }
import java . util . * ; class GFG { static boolean isSubarrayExistUtil ( int arr [ ] , int K , int N ) { int totalXOR = 0 ; int SubarrayXOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) totalXOR ^= arr [ i ] ; for ( int i = 0 ; i < K ; i ++ ) SubarrayXOR ^= arr [ i ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; for ( int i = K ; i < N ; i ++ ) { SubarrayXOR ^= arr [ i ] ; SubarrayXOR ^= arr [ i - 1 ] ; if ( SubarrayXOR == ( totalXOR ^ SubarrayXOR ) ) return true ; } return false ; } static void isSubarrayExist ( int arr [ ] , int K , int N ) { if ( isSubarrayExistUtil ( arr , K , N ) ) System . out . print ( "YESNEW_LINE"); else System . out . print ( "NONEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 5 , 7 , 7 , 3 , 4 } ; int N = arr . length ; int K = 5 ; isSubarrayExist ( arr , K , N ) ; } }
class GFG { static void countPairs ( int L , int R ) { int count_even ; if ( L % 2 == 0 ) { count_even = ( R / 2 ) - ( L / 2 ) + 1 ; } else { count_even = ( R / 2 ) - ( L / 2 ) ; } int count_odd ; if ( L % 2 == 0 ) { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) ; } else { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) + 1 ; } count_even *= count_even ; count_odd *= count_odd ; System . out . println ( count_even + count_odd ) ; } public static void main ( String [ ] args ) { int L = 1 , R = 3 ; countPairs ( L , R ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getDefiniteFinds ( int [ ] arr ) { int n = arr . length ; int [ ] smallestRight = new int [ n + 1 ] ; smallestRight [ n ] = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smallestRight [ i ] = Math . min ( smallestRight [ i + 1 ] , arr [ i ] ) ; } int mn = Integer . MIN_VALUE ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mn < arr [ i ] && arr [ i ] < smallestRight [ i + 1 ] ) { ans ++ ; } mn = Math . max ( arr [ i ] , mn ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 5 , 4 , 9 } ; System . out . println ( getDefiniteFinds ( arr ) ) ; } }
import java . util . * ; class GFG { static void Count_numbers ( int L , int R ) { int count = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) count += 1 ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int L = 6 , R = 30 ; Count_numbers ( L , R ) ; } }
import java . util . * ; class GFG { static boolean checkReverse ( int leftSum , int rightSum ) { int rev = 0 ; int temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp /= 10 ; } if ( rev == leftSum ) { return true ; } return false ; } static int findIndex ( int [ ] arr , int N ) { int rightSum = 0 ; int leftSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 7 , 3 , 6 , 4 , 9 , 2 } ; int N = arr . length ; System . out . print ( findIndex ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void minimumTime ( int [ ] arr , int n ) { int sum = 0 ; int T = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } System . out . println ( Math . max ( 2 * T , sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 8 , 3 } ; int N = arr . length ; minimumTime ( arr , N ) ; } }
import java . util . * ; class GFG { static void findClosest ( int N , int target ) { int closest = - 1 ; int diff = Integer . MAX_VALUE ; for ( int i = 1 ; i <= ( int ) Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( N / i == i ) { if ( Math . abs ( target - i ) < diff ) { diff = Math . abs ( target - i ) ; closest = i ; } } else { if ( Math . abs ( target - i ) < diff ) { diff = Math . abs ( target - i ) ; closest = i ; } if ( Math . abs ( target - N / i ) < diff ) { diff = Math . abs ( target - N / i ) ; closest = N / i ; } } } } System . out . println ( closest ) ; } public static void main ( String [ ] args ) { int N = 16 , X = 5 ; findClosest ( N , X ) ; } }
import java . util . * ; class GFG { static int LCS ( int [ ] firstArr , int [ ] secondArr ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < firstArr . length ; i ++ ) { mp . put ( firstArr [ i ] , i + 1 ) ; } Vector < Integer > tempArr = new Vector < > ( ) ; for ( int i = 0 ; i < secondArr . length ; i ++ ) { if ( mp . containsKey ( secondArr [ i ] ) ) { tempArr . add ( mp . get ( secondArr [ i ] ) ) ; } } Vector < Integer > tail = new Vector < > ( ) ; tail . add ( tempArr . get ( 0 ) ) ; for ( int i = 1 ; i < tempArr . size ( ) ; i ++ ) { if ( tempArr . get ( i ) > tail . lastElement ( ) ) tail . add ( tempArr . get ( i ) ) ; else if ( tempArr . get ( i ) < tail . get ( 0 ) ) tail . add ( 0 , tempArr . get ( i ) ) ; } return ( int ) tail . size ( ) ; } public static void main ( String [ ] args ) { int [ ] firstArr = { 3 , 5 , 1 , 8 } ; int [ ] secondArr = { 3 , 3 , 5 , 3 , 8 } ; System . out . print ( LCS ( firstArr , secondArr ) ) ; } }
import java . util . * ; class GFG { static void countTriplets ( int [ ] arr , int N ) { Vector < Integer > zero_i = new Vector < Integer > ( ) ; Vector < Integer > one_i = new Vector < Integer > ( ) ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . add ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . add ( i + 1 ) ; else mp . put ( i + 1 , 1 ) ; } int total = zero_i . size ( ) * one_i . size ( ) * mp . size ( ) ; for ( int i = 0 ; i < zero_i . size ( ) ; i ++ ) { for ( int j = 0 ; j < one_i . size ( ) ; j ++ ) { int p = zero_i . get ( i ) ; int q = one_i . get ( j ) ; int r = 2 * p - q ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 ) total -- ; r = 2 * q - p ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 ) total -- ; r = ( p + q ) / 2 ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 && Math . abs ( r - p ) == Math . abs ( r - q ) ) total -- ; } } System . out . print ( total ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 1 } ; int N = arr . length ; countTriplets ( arr , N ) ; } }
import java . util . * ; class GFG { static void PrimeFactors ( int N ) { Stack < Integer > st = new Stack < > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( ! st . isEmpty ( ) ) { System . out . println ( st . peek ( ) ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { int N = 8 ; PrimeFactors ( N ) ; ; } }
import java . io . * ; class GFG { static int power ( int A , int N ) { int count = 0 ; if ( A == 1 ) return 0 ; while ( N > 0 ) { count ++ ; N /= A ; } return count ; } static void Pairs ( int N , int A , int B ) { int powerA , powerB ; powerA = power ( A , N ) ; powerB = power ( B , N ) ; int intialB = B , intialA = A ; A = 1 ; for ( int i = 0 ; i <= powerA ; i ++ ) { B = 1 ; for ( int j = 0 ; j <= powerB ; j ++ ) { if ( B == N - A ) { System . out . println ( i + " ▁ " + j ) ; return ; } B *= intialB ; } A *= intialA ; } System . out . println ( " - 1" ) ; return ; } public static void main ( String args [ ] ) { int N = 106 , A = 3 , B = 5 ; Pairs ( N , A , B ) ; } }
import java . io . * ; class GFG { static void minSum ( int N ) { long low = 0 , high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( mid * mid >= N ) { high = mid ; } else { low = mid ; } } long first = high ; low = 0 ; high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( first * mid > N ) { high = mid ; } else { low = mid ; } } long second = high ; System . out . println ( first + second ) ; } public static void main ( String [ ] args ) { int N = 10 ; minSum ( N ) ; } }
import java . lang . * ; class GFG { static void minSum ( int N ) { int ans = ( int ) Math . ceil ( 2 * Math . sqrt ( N + 1 ) ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 10 ; minSum ( N ) ; } }
import java . util . * ; class GFG { static final int INF = 1000000 ; static int [ ] pref = new int [ 500005 ] ; static int [ ] suf = new int [ 500005 ] ; static int [ ] MAX = new int [ 500005 ] ; static void build ( int a [ ] , int tl , int tr , int v ) { if ( tl == tr ) { MAX [ v ] = a [ tl ] ; pref [ v ] = a [ tl ] ; suf [ v ] = a [ tl ] ; } else { int tm = ( tl + tr ) / 2 ; build ( a , tl , tm , v * 2 ) ; build ( a , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . max ( MAX [ v * 2 ] , Math . max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) ) ; } } static void update ( int a [ ] , int pos , int tl , int tr , int v ) { if ( tl > pos tr < pos ) { return ; } if ( tl == tr && tl == pos ) { MAX [ v ] = a [ pos ] ; pref [ v ] = a [ pos ] ; suf [ v ] = a [ pos ] ; } else { int tm = ( tl + tr ) / 2 ; update ( a , pos , tl , tm , v * 2 ) ; update ( a , pos , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . max ( MAX [ v * 2 ] , Math . max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) * suf [ v * 2 ] ) ; } } static void solveQueries ( int arr [ ] , int n , int [ ] [ ] Q , int k ) { int cntQuery = Q . length ; build ( arr , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { System . out . print ( MAX [ 1 ] + " ▁ " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; update ( arr , Q [ i ] [ 1 ] - 1 , 0 , n - 1 , 1 ) ; } } } public static void main ( String [ ] args ) { int N = 10 ; int arr [ ] = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int [ ] [ ] Q = { { 1 } , { 2 , 3 } , { 1 } } ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }
class GFG { static int countFreq ( int N , int K ) { int count = 0 ; while ( N > 0 ) { if ( N % 10 == K ) { count ++ ; } N = N / 10 ; } return count ; } static int findElementUtil ( int arr [ ] , int N , int K ) { int c ; int max ; int ele = 0 ; max = 0 ; for ( int i = 0 ; i < N ; i ++ ) { c = countFreq ( arr [ i ] , K ) ; if ( c > max ) { max = c ; ele = arr [ i ] ; } } if ( max == 0 ) return - 1 ; else return ele ; } static void findElement ( int arr [ ] , int N , int K ) { int ele = findElementUtil ( arr , N , K ) ; if ( ele == - 1 ) System . out . print ( " - 1" ) ; else System . out . print ( ele ) ; } public static void main ( String [ ] args ) { int K = 3 ; int arr [ ] = { 3 , 77 , 343 , 456 } ; int N = arr . length ; findElement ( arr , K , N ) ; } }
import java . util . * ; class GFG { static int findMinDeletions ( int [ ] [ ] v , int n ) { int minDel = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int L = v [ i ] [ 0 ] ; int R = v [ i ] [ 1 ] ; int Count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] [ 0 ] >= L && v [ j ] [ 1 ] <= R ) { Count += 1 ; } } minDel = Math . min ( minDel , n - Count ) ; } return minDel ; } public static void main ( String [ ] args ) { int [ ] [ ] v = { { 1 , 3 } , { 4 , 12 } , { 5 , 8 } , { 13 , 20 } } ; int N = v . length ; System . out . print ( findMinDeletions ( v , N ) ) ; } }
import java . util . * ; class GFG { static int maxEvenIntegers ( int arr [ ] , int N , int M ) { int curr = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( arr [ i ] % 2 == 0 ) curr ++ ; } int ans = curr ; for ( int i = M ; i < N ; i ++ ) { if ( arr [ i - M ] % 2 == 0 ) { curr -- ; } if ( arr [ i ] % 2 == 0 ) curr ++ ; ans = Math . max ( ans , curr ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 , 6 } ; int M = 3 ; int N = arr . length ; System . out . print ( maxEvenIntegers ( arr , N , M ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static void countNumbers ( int n , int d ) { int total = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } System . out . println ( total ) ; } public static void main ( String [ ] args ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; } }
import java . util . * ; class GFG { static int build ( int tree [ ] , int [ ] A , int start , int end , int node ) { if ( start == end ) { tree [ node ] = A [ start ] ; return tree [ node ] ; } int mid = ( start + end ) / 2 ; int X = build ( tree , A , start , mid , 2 * node + 1 ) ; int Y = build ( tree , A , mid + 1 , end , 2 * node + 2 ) ; return ( tree [ node ] = Math . min ( X , Y ) ) ; } static int query ( int tree [ ] , int start , int end , int l , int r , int node ) { if ( start > r end < l ) return Integer . MAX_VALUE ; if ( start >= l && end <= r ) return tree [ node ] ; int mid = ( start + end ) / 2 ; int X = query ( tree , start , mid , l , r , 2 * node + 1 ) ; int Y = query ( tree , mid + 1 , end , l , r , 2 * node + 2 ) ; return Math . min ( X , Y ) ; } static int longestSubArray ( int [ ] A , int N , int K ) { int res = 1 ; int preSum [ ] = new int [ N + 1 ] ; preSum [ 0 ] = A [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) preSum [ i + 1 ] = preSum [ i ] + A [ i ] ; int tree [ ] = new int [ 4 * N + 5 ] ; build ( tree , A , 0 , N - 1 , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { int start = i ; int end = N - 1 ; int mid ; int max_index = i ; while ( start <= end ) { mid = ( start + end ) / 2 ; int min_element = query ( tree , 0 , N - 1 , i , mid , 0 ) ; int expected_sum = ( mid - i + 1 ) * min_element ; int actual_sum = preSum [ mid + 1 ] - preSum [ i ] ; if ( actual_sum - expected_sum <= K ) { start = mid + 1 ; max_index = Math . max ( max_index , mid ) ; } else { end = mid - 1 ; } } res = Math . max ( res , max_index - i + 1 ) ; } return res ; } static public void main ( String args [ ] ) { int arr [ ] = { 1 , 7 , 3 , 4 , 5 , 6 } ; int k = 6 ; int n = 6 ; System . out . print ( longestSubArray ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] *= - 1 ; int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum += arr [ j ] ; } if ( sum == 0 ) { pos = i ; break ; } else { arr [ i ] *= - 1 ; } } return pos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . length ; System . out . println ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = - 1 ; int ArrSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ArrSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( 2 * arr [ i ] == ArrSum ) { pos = i ; break ; } } return pos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . length ; System . out . println ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }
import java . util . * ; class GFG { static String make_String_S_to_T ( String S , String T ) { boolean possible = false ; int M = T . length ( ) ; int N = S . length ( ) ; for ( int i = 0 ; i <= M ; i ++ ) { int prefix_length = i ; int suffix_length = M - i ; String prefix = S . substring ( 0 , prefix_length ) ; String suffix = S . substring ( N - suffix_length , N ) ; if ( ( prefix + suffix ) . equals ( T ) ) { possible = true ; break ; } } if ( possible ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String S = " ababcdcd " ; String T = " abcd " ; System . out . print ( make_String_S_to_T ( S , T ) ) ; } }
import java . io . * ; class GFG { public static void findSum ( int a [ ] ) { int ans = 0 ; for ( int low = 0 ; low < a . length ; low ++ ) { for ( int high = low ; high < a . length ; high ++ ) { int count = 0 ; int maxNumber = 0 ; for ( int i = low ; i <= high ; i ++ ) { if ( a [ i ] == maxNumber ) count ++ ; else if ( a [ i ] > maxNumber ) { maxNumber = a [ i ] ; count = 1 ; } } ans += maxNumber * ( ( count % 2 == 0 ) ? 2 : 1 ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 4 , 4 , 2 } ; findSum ( arr ) ; } }
import java . util . * ; class GFG { static void countPairs ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp1 = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( mp1 . containsKey ( sum ) ) { mp1 . put ( sum , mp1 . get ( sum ) + 1 ) ; } else { mp1 . put ( sum , 1 ) ; } } sum = 0 ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum += arr [ i ] ; if ( mp1 . containsKey ( sum ) ) { ans += mp1 . get ( sum ) ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 1 } ; int n = arr . length ; countPairs ( arr , n ) ; } }
class GFG { static boolean isMagic ( int num ) { return ( num % 9 == 1 ) ; } 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 void find ( int L [ ] , int R [ ] , int q ) { int dp [ ] = new int [ 1000005 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 0 ; for ( int i = 1 ; i < 1000005 ; i ++ ) { if ( isComposite ( i ) && isMagic ( i ) == true ) { dp [ i ] = dp [ i - 1 ] + 1 ; } else dp [ i ] = dp [ i - 1 ] ; } for ( int i = 0 ; i < q ; i ++ ) System . out . println ( dp [ R [ i ] ] - dp [ L [ i ] - 1 ] ) ; } public static void main ( String [ ] args ) { int L [ ] = { 10 , 3 } ; int R [ ] = { 100 , 2279 } ; int Q = 2 ; find ( L , R , Q ) ; } }
import java . util . * ; class GFG { static int findSmallestInteger ( int arr [ ] , int N , int K ) { int left = 1 ; int right = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += ( arr [ i ] + mid - 1 ) / mid ; } if ( sum > K ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 9 } ; int N = arr . length ; int K = 6 ; System . out . println ( findSmallestInteger ( arr , N , K ) ) ; } }
class GFG { static int minimum_deci_binary_number ( String s ) { int m = Integer . MIN_VALUE ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int temp = s . charAt ( i ) - '0' ; if ( temp > m ) { m = temp ; } } return m ; } public static void main ( String [ ] args ) { String S = "31" ; System . out . println ( minimum_deci_binary_number ( S ) ) ; } }
import java . util . * ; class GFG { static void minOperations ( int arr [ ] , int n ) { int flips = 0 ; for ( int i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } for ( int i = 1 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 1 ) { continue ; } while ( i < n && arr [ i ] % 2 == 0 ) { i += 2 ; } flips ++ ; } System . out . println ( flips ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 3 , 5 , 3 , 2 } ; int N = arr . length ; minOperations ( arr , N ) ; } }
import java . util . * ; class GFG { static void lexicographicallyMax ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; int beg = i ; int end = i ; if ( s . charAt ( i ) == '1' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( s . charAt ( j ) == '1' ) count ++ ; if ( count % 2 == 0 && count != 0 ) { end = j ; break ; } } s = reverse ( s , beg , end + 1 ) ; } System . out . println ( s ) ; } static String reverse ( String s , int beg , int end ) { StringBuilder x = new StringBuilder ( " " ) ; for ( int i = 0 ; i < beg ; i ++ ) x . append ( s . charAt ( i ) ) ; for ( int i = end - 1 ; i >= beg ; i -- ) x . append ( s . charAt ( i ) ) ; for ( int i = end ; i < s . length ( ) ; i ++ ) x . append ( s . charAt ( i ) ) ; return x . toString ( ) ; } public static void main ( String args [ ] ) { String S = "0101" ; lexicographicallyMax ( S ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minimumReverse ( String s , int n ) { int k = 0 , l = 0 ; int sum1 = 0 , sum0 = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) sum1 ++ ; else sum0 ++ ; if ( s . charAt ( i ) == s . charAt ( i - 1 ) && s . charAt ( i ) == '0' ) k ++ ; else if ( s . charAt ( i ) == s . charAt ( i - 1 ) && s . charAt ( i ) == '1' ) l ++ ; } if ( s . charAt ( 0 ) == '1' ) sum1 ++ ; sum0 ++ ; if ( Math . abs ( sum1 - sum0 ) > 1 ) return - 1 ; return Math . max ( k , l ) ; } public static void main ( String [ ] args ) { String S = "10001" ; int N = S . length ( ) ; System . out . print ( minimumReverse ( S , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maxPairs ( int [ ] nums , int k ) { Arrays . sort ( nums ) ; int result = 0 ; int start = 0 , end = nums . length - 1 ; while ( start < end ) { if ( nums [ start ] + nums [ end ] > k ) end -- ; else if ( nums [ start ] + nums [ end ] < k ) start ++ ; else { start ++ ; end -- ; result ++ ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; maxPairs ( arr , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maxPairs ( int [ ] nums , int k ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int result = 0 ; for ( int i : nums ) { if ( map . containsKey ( i ) && map . get ( i ) > 0 ) { map . put ( i , map . get ( i ) - 1 ) ; result ++ ; } else { map . put ( k - i , map . getOrDefault ( k - i , 0 ) + 1 ) ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int K = 5 ; maxPairs ( arr , K ) ; } }
import java . util . * ; class GFG { static void removeIndicesToMakeSumEqual ( int [ ] arr ) { int N = arr . length ; int [ ] odd = new int [ N ] ; int [ ] even = new int [ N ] ; even [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { odd [ i ] = odd [ i - 1 ] ; even [ i ] = even [ i - 1 ] ; if ( i % 2 == 0 ) { even [ i ] += arr [ i ] ; } else { odd [ i ] += arr [ i ] ; } } boolean find = false ; int p = odd [ N - 1 ] ; int q = even [ N - 1 ] - arr [ 0 ] ; if ( p == q ) { System . out . print ( "0 ▁ " ) ; find = true ; } for ( int i = 1 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { p = even [ N - 1 ] - even [ i - 1 ] - arr [ i ] + odd [ i - 1 ] ; q = odd [ N - 1 ] - odd [ i - 1 ] + even [ i - 1 ] ; } else { q = odd [ N - 1 ] - odd [ i - 1 ] - arr [ i ] + even [ i - 1 ] ; p = even [ N - 1 ] - even [ i - 1 ] + odd [ i - 1 ] ; } if ( p == q ) { find = true ; System . out . print ( i + " ▁ " ) ; } } if ( ! find ) { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 6 , 2 } ; removeIndicesToMakeSumEqual ( arr ) ; } }
import java . util . * ; class GFG { static int cntDisPairs ( int arr [ ] , int N , int K ) { int cntPairs = 0 ; Arrays . sort ( arr ) ; int i = 0 ; int j = N - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] == K ) { while ( i < j && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } while ( i < j && arr [ j ] == arr [ j - 1 ] ) { j -- ; } cntPairs += 1 ; i ++ ; j -- ; } else if ( arr [ i ] + arr [ j ] < K ) { i ++ ; } else { j -- ; } } return cntPairs ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 5 , 7 , 7 , 8 } ; int N = arr . length ; int K = 13 ; System . out . print ( cntDisPairs ( arr , N , K ) ) ; }
import java . util . * ; class GFG { static int cntDisPairs ( int arr [ ] , int N , int K ) { int cntPairs = 0 ; HashMap < Integer , Integer > cntFre = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntFre . containsKey ( arr [ i ] ) ) cntFre . put ( arr [ i ] , cntFre . get ( arr [ i ] ) + 1 ) ; else cntFre . put ( arr [ i ] , 1 ) ; } for ( Map . Entry < Integer , Integer > it : cntFre . entrySet ( ) ) { int i = it . getKey ( ) ; if ( 2 * i == K ) { if ( cntFre . get ( i ) > 1 ) cntPairs += 2 ; } else { if ( cntFre . containsKey ( K - i ) ) { cntPairs += 1 ; } } } cntPairs = cntPairs / 2 ; return cntPairs ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 5 , 7 , 7 , 8 } ; int N = arr . length ; int K = 13 ; System . out . print ( cntDisPairs ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static int cntPairsInTwoArray ( int arr [ ] , int brr [ ] , int N , int M ) { int cntOddArr = 0 ; int cntOddBrr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { cntOddArr += 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { if ( brr [ i ] % 2 == 1 ) { cntOddBrr += 1 ; } } return ( N * M ) - ( cntOddArr * cntOddBrr ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int N = arr . length ; int brr [ ] = { 1 , 2 } ; int M = brr . length ; System . out . print ( cntPairsInTwoArray ( arr , brr , N , M ) ) ; } }
import java . util . Arrays ; class GFG { static void max_freq ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int Max = arr [ N - 1 ] ; int Min = arr [ 0 ] ; int freq [ ] = new int [ Max - Min + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] - Min ] ++ ; } int maxSum = 0 ; for ( int i = 0 ; i < ( Max - Min - 1 ) ; i ++ ) { int val = freq [ i ] + freq [ i + 1 ] + freq [ i + 2 ] ; maxSum = Math . max ( maxSum , val ) ; } System . out . println ( maxSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 1 , 5 , 9 , 2 } ; int N = arr . length ; max_freq ( arr , N ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int binary_search ( int [ ] [ ] vec , int n , int a ) { int low , high , mid ; low = 0 ; high = n - 1 ; while ( low < high ) { mid = low + ( high - low + 1 ) / 2 ; if ( vec [ mid ] [ 0 ] > a ) { high = mid - 1 ; } else if ( vec [ mid ] [ 1 ] <= a ) { low = mid ; } } return low ; } static void modify_vec ( int [ ] [ ] v , int n ) { for ( int i = 1 ; i < n ; i ++ ) { v [ i ] [ 1 ] = Math . min ( v [ i ] [ 1 ] , v [ i - 1 ] [ 1 ] ) ; } } static int evaluate_query ( int [ ] [ ] v , int n , int m1 , int m2 ) { int temp = binary_search ( v , n , m1 ) ; if ( ( v [ temp ] [ 0 ] <= m1 ) && ( v [ temp ] [ 1 ] <= m2 ) ) { return 1 ; } return 0 ; } static void checkPairs ( int [ ] [ ] v , int [ ] [ ] queries ) { int n = v . length ; Arrays . sort ( v , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; modify_vec ( v , n ) ; int k = queries . length ; for ( int i = 0 ; i < k ; i ++ ) { int m1 = queries [ i ] [ 0 ] ; int m2 = queries [ i ] [ 1 ] ; int result = evaluate_query ( v , n , m1 , m2 ) ; if ( result > 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 3 , 5 } , { 2 , 7 } , { 2 , 3 } , { 4 , 9 } } ; int [ ] [ ] queries = { { 3 , 4 } , { 3 , 2 } , { 4 , 1 } , { 3 , 7 } } ; checkPairs ( arr , queries ) ; } }
class GFG { static void min_element_removal ( int arr [ ] , int N ) { int left [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) left [ i ] = 1 ; int right [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) right [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { left [ i ] = Math . max ( left [ i ] , left [ j ] + 1 ) ; } } } for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] > arr [ j ] ) { right [ i ] = Math . max ( right [ i ] , right [ j ] + 1 ) ; } } } int maxLen = 0 ; for ( int i = 1 ; i < N - 1 ; i ++ ) { maxLen = Math . max ( maxLen , left [ i ] + right [ i ] - 1 ) ; } System . out . println ( N - maxLen ) ; } static void makeBitonic ( int arr [ ] , int N ) { if ( N == 1 ) { System . out . println ( "0" ) ; return ; } if ( N == 2 ) { if ( arr [ 0 ] != arr [ 1 ] ) System . out . println ( "0" ) ; else System . out . println ( "1" ) ; return ; } min_element_removal ( arr , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 } ; int N = arr . length ; makeBitonic ( arr , N ) ; } }
import java . util . * ; class GFG { static void Sort ( Vector < Integer > ans , int num ) { int pos = - 1 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans . get ( i ) >= num ) { pos = i ; break ; } } if ( pos == - 1 ) ans . add ( num ) ; else ans . add ( pos , num ) ; } static void sortStream ( int arr [ ] , int N ) { Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Sort ( ans , arr [ i ] ) ; for ( int j = 0 ; j < ans . size ( ) ; j ++ ) { System . out . print ( ans . get ( j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 1 , 7 , 6 , 2 } ; int N = arr . length ; sortStream ( arr , N ) ; } }
import java . util . * ; class GFG { static void countMaxCommonChar ( char [ ] S ) { LinkedHashSet < Character > Q = new LinkedHashSet < > ( ) ; int res = 1 ; HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < S . length ; i ++ ) { if ( freq . containsKey ( S [ i ] ) ) { freq . put ( S [ i ] , freq . get ( S [ i ] ) + 1 ) ; } else { freq . put ( S [ i ] , 1 ) ; } } for ( int i = 0 ; i < S . length ; i ++ ) { if ( freq . containsKey ( S [ i ] ) ) { freq . put ( S [ i ] , freq . get ( S [ i ] ) - 1 ) ; } if ( ! freq . containsKey ( S [ i ] ) ) { Q . remove ( S [ i ] ) ; } else { Q . add ( S [ i ] ) ; } int curr = Q . size ( ) - 1 ; res = Math . max ( res , curr ) ; } System . out . print ( res + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " aabbca " ; countMaxCommonChar ( str . toCharArray ( ) ) ; } }
import java . util . Arrays ; class GFG { static int longestSubarray ( String s ) { s += '0' ; int i ; int res = 0 ; int prev_one = 0 ; int curr_one = 0 ; int numberOfZeros = 0 ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { curr_one += 1 ; } else { numberOfZeros += 1 ; prev_one += curr_one ; res = Math . max ( res , prev_one ) ; prev_one = curr_one ; curr_one = 0 ; } } if ( numberOfZeros == 1 ) { res -= 1 ; } return res ; } public static void main ( String [ ] args ) { String S = "1101" ; System . out . println ( longestSubarray ( S ) ) ; } }
import java . util . * ; class GFG { static int longestSubarray ( String s ) { int i = 0 , j = 0 , k = 1 ; for ( j = 0 ; j < s . length ( ) ; ++ j ) { if ( s . charAt ( j ) == '0' ) k -- ; if ( k < 0 && s . charAt ( i ++ ) == '0' ) k ++ ; } return j - i - 1 ; } public static void main ( String [ ] args ) { String S = "011101101" ; System . out . print ( longestSubarray ( S ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void LarUnEl ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { map . put ( arr [ i ] , map . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int LNRElem = Integer . MIN_VALUE ; int ind = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( map . get ( arr [ i ] ) == 1 && arr [ i ] > LNRElem ) { ind = i ; LNRElem = arr [ i ] ; } } if ( ind == - 1 ) { System . out . println ( ind ) ; return ; } System . out . println ( arr [ ind ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 8 , 8 , 4 } ; int N = arr . length ; LarUnEl ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void countShifts ( String str ) { int firstOne = - 1 ; int lastOne = - 1 ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == '1' ) { if ( firstOne == - 1 ) firstOne = i ; lastOne = i ; } } if ( ( firstOne == - 1 ) || ( firstOne == lastOne ) ) { System . out . print ( 0 ) ; return ; } for ( int i = firstOne ; i <= lastOne ; i ++ ) { if ( str . charAt ( i ) == '0' ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String str = "00110111011" ; countShifts ( str ) ; } }
class GFG { static boolean checkSubsequenceUtil ( int arr [ ] , int L , int R , int N ) { for ( int i = 0 ; i < L ; i ++ ) if ( arr [ i ] == arr [ L ] ) return true ; for ( int i = R + 1 ; i < N ; i ++ ) if ( arr [ i ] == arr [ R ] ) return true ; return false ; } static void checkSubsequence ( int arr [ ] , int L , int R , int N ) { if ( checkSubsequenceUtil ( arr , L , R , N ) ) { System . out . print ( "YESNEW_LINE"); } else { System . out . print ( "NONEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 12 , 1 , 7 , 5 , 10 , 11 , 42 } ; int N = arr . length ; int L = 3 , R = 6 ; checkSubsequence ( arr , L , R , N ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; Node prev ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = head_ref ; if ( head_ref != null ) { head_ref . prev = new_node ; } head_ref = new_node ; return head_ref ; } static int search ( Node head_ref , int x ) { Node temp = head_ref ; int pos = 0 ; while ( temp . data != x && temp . next != null ) { pos ++ ; temp = temp . next ; } if ( temp . data != x ) return - 1 ; return ( pos + 1 ) ; } public static void main ( String [ ] args ) { Node head = null ; int X = 8 ; head = push ( head , 14 ) ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 15 ) ; head = push ( head , 18 ) ; System . out . print ( search ( head , X ) ) ; } }
import java . util . * ; class GFG { static void largestSubarray ( int [ ] arr , int N , int K ) { int count = 0 ; int len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < K ) { count += 1 ; } else { len = Math . max ( len , count ) ; count = 0 ; } } if ( count != 0 ) { len = Math . max ( len , count ) ; } System . out . println ( len ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 8 , 3 , 5 , 2 , 2 , 1 , 13 } ; int N = arr . length ; int K = 6 ; largestSubarray ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void countSubarrays ( int A [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) { ans ++ ; for ( int j = i - 1 , k = i + 2 ; j >= 0 && k < N && A [ j ] == A [ i ] && A [ k ] == A [ i + 1 ] ; j -- , k ++ ) { ans ++ ; } } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 0 , 0 , 1 , 0 } ; int N = A . length ; countSubarrays ( A , N ) ; } }
import java . util . * ; class GFG { static void longestSubsequence ( int N , int Q , int arr [ ] , int Queries [ ] [ ] ) { for ( int i = 0 ; i < Q ; i ++ ) { int x = Queries [ i ] [ 0 ] ; int y = Queries [ i ] [ 1 ] ; arr [ x - 1 ] = y ; int count = 1 ; for ( int j = 1 ; j < N ; j ++ ) { if ( arr [ j ] != arr [ j - 1 ] ) { count += 1 ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 5 , 2 } ; int N = arr . length ; int Q = 2 ; int Queries [ ] [ ] = { { 1 , 3 } , { 4 , 2 } } ; longestSubsequence ( N , Q , arr , Queries ) ; } }
import java . util . * ; class GFG { static void longestSubsequence ( int N , int Q , int arr [ ] , int Queries [ ] [ ] ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count += 1 ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = Queries [ i ] [ 0 ] ; int y = Queries [ i ] [ 1 ] ; if ( x > 1 ) { if ( arr [ x - 1 ] != arr [ x - 2 ] ) { count -= 1 ; } if ( arr [ x - 2 ] != y ) { count += 1 ; } } if ( x < N ) { if ( arr [ x ] != arr [ x - 1 ] ) { count -= 1 ; } if ( y != arr [ x ] ) { count += 1 ; } } System . out . print ( count + " ▁ " ) ; arr [ x - 1 ] = y ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 5 , 2 } ; int N = arr . length ; int Q = 2 ; int Queries [ ] [ ] = { { 1 , 3 } , { 4 , 2 } } ; longestSubsequence ( N , Q , arr , Queries ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void findPair ( int A [ ] , int N ) { int right_prod [ ] = new int [ N ] ; int flag = 0 ; right_prod [ N - 1 ] = A [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) right_prod [ i ] = right_prod [ i + 1 ] * A [ i ] ; int total_prod = right_prod [ 0 ] ; int product ; for ( int i = 1 ; i < N - 1 ; i ++ ) { product = 1 ; for ( int j = i ; j < N - 1 ; j ++ ) { product *= A [ j ] ; if ( gcd ( product , right_prod [ j + 1 ] ) == 1 || gcd ( product , total_prod / right_prod [ i ] ) == 1 ) { flag = 1 ; System . out . println ( " ( " + ( i - 1 ) + " , ▁ " + ( j + 1 ) + " ) " ) ; break ; } } if ( flag == 1 ) break ; } if ( flag == 0 ) System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 3 , 7 } ; int N = arr . length ; findPair ( arr , N ) ; } }
import java . util . * ; class GFG { static void countString ( String S ) { int count = 0 ; for ( char it : S . toCharArray ( ) ) { if ( it == '0' && count > 0 ) { count -- ; } else { count ++ ; } } System . out . print ( ( 1 << count ) - 1 ) ; } public static void main ( String [ ] args ) { String S = "1001" ; countString ( S ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void replaceDuplicates ( String [ ] names ) { HashMap < String , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < names . length ; i ++ ) { if ( ! hash . containsKey ( names [ i ] ) ) hash . put ( names [ i ] , 1 ) ; else { int count = hash . get ( names [ i ] ) ; hash . put ( names [ i ] , hash . get ( names [ i ] ) + 1 ) ; names [ i ] += Integer . toString ( count ) ; } } for ( int i = 0 ; i < names . length ; i ++ ) { System . out . print ( names [ i ] + ' ▁ ' ) ; } } public static void main ( String [ ] args ) { String [ ] str = { " aa " , " bb " , " cc " , " bb " , " aa " , " aa " , " aa " } ; replaceDuplicates ( str ) ; } }
class GFG { public static void convert_to_allzeroes ( String str , int a , int b ) { int len = str . length ( ) ; int left_1 , i = 0 ; while ( i < len && str . charAt ( i ) == '0' ) i ++ ; left_1 = i ; int right_1 ; i = len - 1 ; while ( i >= 0 && str . charAt ( i ) == '0' ) i -- ; right_1 = i ; if ( left_1 == len && right_1 == - 1 ) { System . out . print ( 0 ) ; return ; } int cost = a , zeroes ; for ( i = left_1 ; i <= right_1 ; i ++ ) { zeroes = 0 ; while ( i < len && str . charAt ( i ) == '0' ) { zeroes ++ ; i ++ ; } if ( zeroes != 0 ) cost += Math . min ( zeroes * b , a ) ; } System . out . print ( cost ) ; } public static void main ( String [ ] args ) { String str = "01101110" ; int A = 5 , B = 1 ; convert_to_allzeroes ( str , A , B ) ; } }
import java . util . * ; class solution { static int MinDistK ( int arr [ ] , int N , int K ) { int res = Integer . MAX_VALUE ; int dist = 0 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i + K - 1 ] >= 0 ) { dist = Math . max ( arr [ i ] , arr [ i + K - 1 ] ) ; } else { dist = Math . abs ( arr [ i ] ) + Math . abs ( arr [ i + K - 1 ] ) + Math . min ( Math . abs ( arr [ i ] ) , Math . abs ( arr [ i + K - 1 ] ) ) ; } res = Math . min ( res , dist ) ; } return res ; } public static void main ( String args [ ] ) { int K = 3 ; int arr [ ] = { - 30 , - 10 , 10 , 20 , 50 } ; int N = arr . length ; System . out . println ( MinDistK ( arr , N , K ) ) ; } }
import java . util . * ; class solution { static int min1 ( int a [ ] , int n ) { int minIndex = 0 , minValue = a [ 0 ] , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < minValue ) { minValue = a [ i ] ; minIndex = i ; } } return minIndex ; } static int maximizeMin ( int A [ ] , int N , int S , int M ) { int minIndex , left , right , i , j ; for ( i = 0 ; i < M ; i ++ ) { minIndex = min1 ( A , N ) ; A [ minIndex ] ++ ; left = minIndex - 1 ; right = minIndex + 1 ; for ( j = 0 ; j < S - 1 ; j ++ ) { if ( left == - 1 ) A [ right ++ ] ++ ; else if ( right == N ) A [ left -- ] ++ ; else { if ( A [ left ] < A [ right ] ) A [ left -- ] ++ ; else A [ right ++ ] ++ ; } } } minIndex = min1 ( A , N ) ; return A [ minIndex ] ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int S = 2 , M = 3 ; System . out . print ( maximizeMin ( arr , N , S , M ) ) ; } }
import java . util . * ; class GFG { static void sum ( int arr [ ] , int n ) { HashMap < Integer , Vector < Integer > > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Vector < Integer > v = new Vector < > ( ) ; v . add ( i ) ; if ( mp . containsKey ( arr [ i ] ) ) v . addAll ( mp . get ( arr [ i ] ) ) ; mp . put ( arr [ i ] , v ) ; } int [ ] ans = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int it : mp . get ( arr [ i ] ) ) { sum += Math . abs ( it - i ) ; } ans [ i ] = sum ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 1 , 1 , 2 } ; int n = arr . length ; sum ( arr , n ) ; } }
import java . util . * ; class GFG { static int maxN = 2002 ; static int [ ] [ ] lcount = new int [ maxN ] [ maxN ] ; static int [ ] [ ] rcount = new int [ maxN ] [ maxN ] ; static void fill_counts ( int a [ ] , int n ) { int i , j ; int maxA = a [ 0 ] ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] > maxA ) { maxA = a [ i ] ; } } for ( i = 0 ; i < n ; i ++ ) { lcount [ a [ i ] ] [ i ] = 1 ; rcount [ a [ i ] ] [ i ] = 1 ; } for ( i = 0 ; i <= maxA ; i ++ ) { for ( j = 1 ; j < n ; j ++ ) { lcount [ i ] [ j ] = lcount [ i ] [ j - 1 ] + lcount [ i ] [ j ] ; } for ( j = n - 2 ; j >= 0 ; j -- ) { rcount [ i ] [ j ] = rcount [ i ] [ j + 1 ] + rcount [ i ] [ j ] ; } } } static int countSubsequence ( int a [ ] , int n ) { int i , j ; fill_counts ( a , n ) ; int answer = 0 ; for ( i = 1 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n - 1 ; j ++ ) { answer += lcount [ a [ j ] ] [ i - 1 ] * rcount [ a [ i ] ] [ j + 1 ] ; } } return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 2 , 1 , 3 , 2 } ; System . out . print ( countSubsequence ( a , a . length ) ) ; } }
import java . util . * ; class GFG { static boolean checkPointRange ( int arr [ ] [ ] , int X , int Y , int N ) { for ( int i = 0 ; i < N ; i ++ ) { double dist = Math . sqrt ( ( arr [ i ] [ 0 ] - X ) * ( arr [ i ] [ 0 ] - X ) + ( arr [ i ] [ 1 ] - Y ) * ( arr [ i ] [ 1 ] - Y ) ) ; if ( dist <= arr [ i ] [ 2 ] ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 1 , 3 } , { 10 , 10 , 3 } , { 15 , 15 , 15 } } ; int X = 5 , Y = 5 ; int N = arr . length ; if ( checkPointRange ( arr , X , Y , N ) ) { System . out . print ( " True " ) ; } else { System . out . print ( " False " ) ; } } }
import java . util . * ; class GFG { static void findWinner ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == 0 ) { if ( odd % 2 == 0 ) { System . out . println ( " Player ▁ 2" ) ; } else if ( odd % 2 == 1 ) { System . out . println ( " Player ▁ 1" ) ; } } else if ( even == 1 && odd % 2 == 1 ) { System . out . println ( " Player ▁ 1" ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 1 , 9 , 7 } ; int N = arr . length ; findWinner ( arr , N ) ; } }
import java . util . * ; class GFG { static void rearrangeArrMaxcntMinima ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int left = 0 ; int right = N / 2 ; while ( left < N / 2 right < N ) { if ( right < N ) { System . out . print ( arr [ right ] + " ▁ " ) ; right ++ ; } if ( left < N / 2 ) { System . out . print ( arr [ left ] + " ▁ " ) ; left ++ ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; rearrangeArrMaxcntMinima ( arr , N ) ; } }
import java . util . * ; class GFG { static int MOD = ( int ) 1e9 + 7 ; static int minJumps ( String seats ) { Vector < Integer > position = new Vector < > ( ) ; int count = 0 ; int len = seats . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( seats . charAt ( i ) == ' x ' ) { position . add ( i - count ) ; count ++ ; } } if ( count == len count == 0 ) return 0 ; int med_index = ( count - 1 ) / 2 ; int med_val = position . get ( med_index ) ; int ans = 0 ; for ( int i = 0 ; i < position . size ( ) ; i ++ ) { ans = ( ans % MOD + Math . abs ( position . get ( i ) - med_val ) % MOD ) % MOD ; } return ans % MOD ; } public static void main ( String [ ] args ) { String S = " . . . . x . . xx . . . x . . " ; System . out . print ( minJumps ( S ) ) ; } }
import java . util . * ; class GFG { static int singlePrimeFactor ( int N ) { HashSet < Integer > disPrimeFact = new HashSet < > ( ) ; for ( int i = 2 ; i * i <= N ; ++ i ) { while ( N % i == 0 ) { disPrimeFact . add ( i ) ; N /= i ; } } if ( N != 1 ) { disPrimeFact . add ( N ) ; } if ( disPrimeFact . size ( ) == 1 ) { for ( int i : disPrimeFact ) return i ; } return - 1 ; } static int cntsingleFactorPair ( int arr [ ] , int N ) { int countOf1 = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { countOf1 ++ ; continue ; } int factorValue = singlePrimeFactor ( arr [ i ] ) ; if ( factorValue == - 1 ) { continue ; } else { if ( mp . containsKey ( factorValue ) ) mp . put ( factorValue , mp . get ( factorValue ) + 1 ) ; else mp . put ( factorValue , 1 ) ; } } int res = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int X = it . getValue ( ) ; res += countOf1 * X + ( X * ( X - 1 ) ) / 2 ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . print ( cntsingleFactorPair ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int MincntBothPalin ( char [ ] str1 , char [ ] str2 , int N ) { int i = 0 ; int j = N - 1 ; int cntOp = 0 ; while ( i < j ) { if ( str1 [ i ] == str1 [ j ] && str2 [ i ] != str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] == str2 [ j ] ) { cntOp += 1 ; } else if ( str1 [ i ] != str1 [ j ] && str2 [ i ] != str2 [ j ] ) { if ( str1 [ i ] == str2 [ j ] && str2 [ i ] == str1 [ j ] ) { cntOp += 1 ; } else { cntOp += 2 ; } } i += 1 ; j -= 1 ; } return cntOp ; } public static void main ( String [ ] args ) { String str1 = " dbba " ; String str2 = " abcd " ; int N = str1 . length ( ) ; System . out . print ( MincntBothPalin ( str1 . toCharArray ( ) , str2 . toCharArray ( ) , N ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int v1 [ ] , int v2 [ ] , int n , int m , int k ) { int count = 0 ; if ( n <= m ) { Arrays . sort ( v1 ) ; for ( int j = 0 ; j < m ; j ++ ) { int index = lowerBound ( v1 , 0 , n , v2 [ j ] - k ) ; count += index ; } } else { Arrays . sort ( v2 ) ; for ( int i = 0 ; i < n ; i ++ ) { int index = upperBound ( v2 , 0 , m , v1 [ i ] + k ) ; count += m - index ; } } return count ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int upperBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 9 , 1 , 8 } ; int brr [ ] = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = arr . length ; int M = brr . length ; System . out . print ( countPairs ( arr , brr , N , M , K ) ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Vector < Node > child = new Vector < > ( ) ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; return temp ; } static void Siblings ( Node root , int value ) { int flag = 0 ; if ( root == null ) return ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; for ( int i = 0 ; i < temp . child . size ( ) ; i ++ ) { if ( temp . child . get ( i ) . key == value ) { flag = 1 ; for ( int j = 0 ; j < temp . child . size ( ) ; j ++ ) { if ( value != temp . child . get ( j ) . key ) System . out . print ( temp . child . get ( j ) . key + " ▁ " ) ; } break ; } q . add ( temp . child . get ( i ) ) ; } } if ( flag == 0 ) System . out . print ( " No ▁ siblings ! ! " ) ; } static Node constructTree ( ) { Node root = newNode ( 10 ) ; ( root . child ) . add ( newNode ( 20 ) ) ; ( root . child ) . add ( newNode ( 30 ) ) ; ( root . child ) . add ( newNode ( 40 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 50 ) ) ; ( root . child . get ( 0 ) . child ) . add ( newNode ( 60 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 70 ) ) ; ( root . child . get ( 1 ) . child ) . add ( newNode ( 80 ) ) ; ( root . child . get ( 2 ) . child ) . add ( newNode ( 90 ) ) ; ( root . child . get ( 2 ) . child ) . add ( newNode ( 100 ) ) ; ( root . child . get ( 2 ) . child ) . add ( newNode ( 110 ) ) ; return root ; } public static void main ( String [ ] args ) { Node root = constructTree ( ) ; int X = 30 ; Siblings ( root , X ) ; } }
import java . io . * ; class GFG { private static void maxSumSubarr ( int A [ ] , int N , int K , int X ) { int sum_K = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum_K += A [ i ] ; } int Max_Sum = 0 ; if ( sum_K < X ) { Max_Sum = sum_K ; } for ( int i = K ; i < N ; i ++ ) { sum_K -= ( A [ i - K ] - A [ i ] ) ; if ( sum_K < X ) { Max_Sum = Math . max ( Max_Sum , sum_K ) ; } } System . out . println ( Max_Sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 5 , 8 , 7 , 2 , 10 , 1 , 20 , - 4 , 6 , 9 } ; int K = 5 ; int X = 30 ; int N = arr . length ; maxSumSubarr ( arr , N , K , X ) ; } }
import java . io . * ; class GFG { static String removeOuterParentheses ( String S ) { String res = " " ; int count = 0 ; for ( int c = 0 ; c < S . length ( ) ; c ++ ) { if ( S . charAt ( c ) == ' ( ' && count ++ > 0 ) res += S . charAt ( c ) ; if ( S . charAt ( c ) == ' ) ' && count -- > 1 ) res += S . charAt ( c ) ; } return res ; } public static void main ( String [ ] args ) { String S = " ( ( ) ( ) ) ( ( ) ) ( ) " ; System . out . print ( removeOuterParentheses ( S ) ) ; } }
import java . util . * ; class GFG { static void removeSmallestSubarray ( int arr [ ] , int n , int k ) { int [ ] mod_arr = new int [ n ] ; int total_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mod_arr [ i ] = ( arr [ i ] + k ) % k ; total_sum += arr [ i ] ; } int target_remainder = total_sum % k ; if ( target_remainder == 0 ) { System . out . print ( "0" ) ; return ; } HashMap < Integer , Integer > map1 = new HashMap < > ( ) ; map1 . put ( 0 , - 1 ) ; int curr_remainder = 0 ; int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { curr_remainder = ( curr_remainder + arr [ i ] + k ) % k ; map1 . put ( curr_remainder , i ) ; int mod = ( curr_remainder - target_remainder + k ) % k ; if ( map1 . containsKey ( mod ) ) res = Math . min ( res , i - map1 . get ( mod ) ) ; } if ( res == Integer . MAX_VALUE res == n ) { res = - 1 ; } System . out . print ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 2 } ; int N = arr . length ; int K = 6 ; removeSmallestSubarray ( arr , N , K ) ; } }
import java . util . * ; class GFG { static boolean isValid ( int wood [ ] , int N , int len , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { count += wood [ i ] / len ; } return count >= K ; } static int findMaxLen ( int wood [ ] , int N , int K ) { int left = 1 ; int right = Arrays . stream ( wood ) . max ( ) . getAsInt ( ) ; while ( left <= right ) { int mid = left + ( right - left ) / 2 ; if ( isValid ( wood , N , mid , K ) ) { left = mid + 1 ; } else { right = mid - 1 ; } } return right ; } public static void main ( String [ ] args ) { int wood [ ] = { 5 , 9 , 7 } ; int N = wood . length ; int K = 4 ; System . out . print ( findMaxLen ( wood , N , K ) ) ; } }
import java . util . * ; class GFG { static int countSum ( int a [ ] , int n , int sum ) { int i , j , k , l ; int count = 0 ; for ( i = 0 ; i < n - 3 ; i ++ ) { for ( j = i + 1 ; j < n - 2 ; j ++ ) { for ( k = j + 1 ; k < n - 1 ; k ++ ) { for ( l = k + 1 ; l < n ; l ++ ) { if ( a [ i ] + a [ j ] + a [ k ] + a [ l ] == sum ) count ++ ; } } } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . length ; System . out . print ( countSum ( arr , N , S ) ) ; } }
import java . util . * ; class GFG { static int minSwaps ( String S , int n ) { int swaps = 0 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] arr = new Vector [ 26 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) arr [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int pos = S . charAt ( i ) - ' a ' ; arr [ pos ] . add ( i ) ; } for ( char ch = ' a ' ; ch <= ' z ' ; ++ ch ) { int pos = ch - ' a ' ; for ( int i = 1 ; i < arr [ pos ] . size ( ) ; ++ i ) { swaps += Math . abs ( arr [ pos ] . get ( i ) - arr [ pos ] . get ( i - 1 ) - 1 ) ; } } return swaps ; } public static void main ( String [ ] args ) { String S = " abbccabbcc " ; int N = S . length ( ) ; System . out . print ( minSwaps ( S , N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void printConnections ( Vector < pair > store , Vector < Integer > ind , int ind1 ) { for ( pair pr : store ) { System . out . print ( pr . first + " ▁ " + pr . second + "NEW_LINE"); } if ( ind . size ( ) != 0 ) { for ( int x : ind ) { System . out . print ( ind1 + " ▁ " + ( x + 1 ) + "NEW_LINE"); } } } static void constructGraph ( char arr [ ] , int N ) { Vector < Integer > ind = new Vector < > ( ) ; Vector < pair > store = new Vector < > ( ) ; char x = arr [ 0 ] ; int count = 0 ; int ind1 = - 1 ; for ( int i = 1 ; i <= N - 1 ; ++ i ) { if ( arr [ i ] != x ) { ind1 = i + 1 ; count ++ ; store . add ( new pair ( 1 , i + 1 ) ) ; } else { ind . add ( i ) ; } } if ( count == 0 ) { System . out . print ( " Not ▁ Possible " ) ; } else { System . out . print ( " Possible " + "NEW_LINE"); printConnections ( store , ind , ind1 ) ; } } public static void main ( String [ ] args ) { int N = 5 ; char arr [ ] = { ' a ' , ' b ' , ' a ' , ' b ' , ' c ' } ; constructGraph ( arr , N ) ; } }
import java . util . * ; class GFG { static int isValid ( int arr [ ] , int n , int m , int d ) { for ( int i = 0 ; i < m ; i ++ ) { if ( Math . abs ( arr [ n - m + i ] - arr [ i ] ) < d ) { return 0 ; } } return 1 ; } static void countPairs ( int arr [ ] , int N , int K ) { int ans = 0 ; int left = 0 , right = N / 2 + 1 ; Arrays . sort ( arr ) ; while ( left < right ) { int mid = ( left + right ) / 2 ; if ( isValid ( arr , N , mid , K ) == 1 ) { ans = mid ; left = mid + 1 ; } else right = mid - 1 ; } System . out . print ( ans ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 3 , 5 } ; int K = 2 ; int N = arr . length ; countPairs ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void max_distance ( int a [ ] , int temp [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { temp [ i ] = - 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == null ) temp [ a [ i ] ] = i + 1 ; else temp [ a [ i ] ] = Math . max ( temp [ a [ i ] ] , i - mp . getOrDefault ( a [ i ] , 0 ) ) ; mp . put ( a [ i ] , i ) ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] != - 1 ) temp [ i ] = Math . max ( temp [ i ] , n - mp . getOrDefault ( i , 0 ) ) ; } } static void min_comm_ele ( int a [ ] , int ans [ ] , int temp [ ] , int n ) { max_distance ( a , temp , n ) ; for ( int i = 1 ; i <= n ; i ++ ) { ans [ i ] = - 1 ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( temp [ i ] >= 0 && ans [ temp [ i ] ] == - 1 ) ans [ temp [ i ] ] = i ; } for ( int i = 1 ; i <= n ; i ++ ) { if ( i > 1 && ans [ i - 1 ] != - 1 ) { if ( ans [ i ] == - 1 ) ans [ i ] = ans [ i - 1 ] ; else ans [ i ] = Math . min ( ans [ i ] , ans [ i - 1 ] ) ; } System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 6 ; int a [ ] = { 1 , 3 , 4 , 5 , 6 , 7 } ; int [ ] temp = new int [ 100 ] ; Arrays . fill ( temp , 0 ) ; int [ ] ans = new int [ 100 ] ; Arrays . fill ( ans , 0 ) ; min_comm_ele ( a , ans , temp , N ) ; } }
import java . util . * ; class GFG { static void FindTrip ( int arr [ ] , int N ) { for ( int i = 1 ; i < N - 1 ; i ++ ) { int p = arr [ i - 1 ] ; int q = arr [ i ] ; int r = arr [ i + 1 ] ; if ( p < q && q > r ) { System . out . println ( ( i - 1 ) + " ▁ " + ( i ) + " ▁ " + ( i + 1 ) ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 4 , 3 } ; int N = arr . length ; FindTrip ( arr , N ) ; } }
import java . util . * ; class GFG { static int cntSubarrays ( int arr [ ] , int N ) { int cntSub = 0 ; int cntUnique = 0 ; Map < Integer , Integer > cntFreq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j ++ ) { cntFreq . put ( arr [ j ] , cntFreq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; if ( cntFreq . get ( arr [ j ] ) == 1 ) { cntUnique ++ ; } else if ( cntFreq . get ( arr [ j ] ) == 2 ) { cntUnique -- ; } if ( cntUnique == 0 ) { cntSub ++ ; } } cntFreq . clear ( ) ; cntUnique = 0 ; } return cntSub ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 2 , 2 } ; int N = arr . length ; System . out . println ( cntSubarrays ( arr , N ) ) ; } }
import java . util . * ; class GFG { public static boolean checkPalinK ( String str , int K ) { int N = str . length ( ) ; int cntFreq [ ] = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { cntFreq [ str . charAt ( i ) ] ++ ; } int cntOddFreq = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( cntFreq [ i ] % 2 == 1 ) { cntOddFreq ++ ; } } if ( cntOddFreq <= ( K + 1 ) ) { return true ; } return false ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int K = 2 ; if ( checkPalinK ( str , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . io . * ; import java . util . Arrays ; class GFG { static int Count_Number ( int x ) { int ans = 0 ; if ( x % 99 != 0 ) { ans = - 1 ; } else { int diff = x / 99 ; for ( int i = 1 ; i < 10 ; i ++ ) { for ( int j = 1 ; j < 10 ; j ++ ) { if ( ( i - j ) == diff ) { ans += 10 ; } } } } return ans ; } public static void main ( String [ ] args ) { int x = 792 ; System . out . println ( Count_Number ( x ) ) ; } }
import java . io . * ; class GFG { static int INF = Integer . MAX_VALUE ; static int [ ] findMaxPoint ( int arr [ ] [ ] , int i , int n ) { if ( i == n ) return new int [ ] { INF , INF } ; boolean flag = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( j == i ) continue ; if ( arr [ j ] [ 0 ] >= arr [ i ] [ 0 ] arr [ j ] [ 1 ] >= arr [ i ] [ 1 ] ) { flag = false ; break ; } } if ( flag ) return arr [ i ] ; return findMaxPoint ( arr , i + 1 , n ) ; } static void findMaxPoints ( int arr [ ] [ ] , int n ) { int ans [ ] = findMaxPoint ( arr , 0 , n ) ; if ( ans [ 0 ] == INF ) { System . out . println ( - 1 ) ; } else { System . out . println ( " ( " + ans [ 0 ] + " ▁ " + ans [ 1 ] + " ) " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; int N = arr . length ; findMaxPoints ( arr , N ) ; } }
import java . io . * ; class GFG { static void findMaxPoint ( int arr [ ] [ ] ) { int maxX = Integer . MIN_VALUE ; int maxY = Integer . MIN_VALUE ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { maxX = Math . max ( maxX , arr [ i ] [ 0 ] ) ; maxY = Math . max ( maxY , arr [ i ] [ 1 ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( maxX == arr [ i ] [ 0 ] && maxY == arr [ i ] [ 1 ] ) { System . out . println ( " ( " + maxX + " , ▁ " + maxY + " ) " ) ; return ; } } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 } , { 2 , 1 } , { 3 , 4 } , { 4 , 3 } , { 5 , 5 } } ; findMaxPoint ( arr ) ; } }
import java . io . * ; class GFG { static int find_index ( int [ ] arr , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == K ) return i ; else if ( arr [ i ] > K ) return i ; return n ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 5 , 6 } ; int n = arr . length ; int K = 2 ; System . out . println ( find_index ( arr , n , K ) ) ; } }
import java . util . * ; class GFG { static void minSubarray ( int [ ] arr , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int mini = Integer . MAX_VALUE ; for ( int j = i ; j < N ; j ++ ) { mini = Math . min ( mini , arr [ j ] ) ; if ( mp . containsKey ( mini ) ) { mp . put ( mini , mp . get ( mini ) + 1 ) ; } else { mp . put ( mini , 1 ) ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( mp . get ( arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int N = arr . length ; minSubarray ( arr , N ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void minSubarray ( int [ ] arr , int N ) { int [ ] result = new int [ N ] ; Stack < pair > l = new Stack < > ( ) ; Stack < pair > r = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int count = 1 ; while ( ! l . isEmpty ( ) && l . peek ( ) . first > arr [ i ] ) { count += l . peek ( ) . second ; l . pop ( ) ; } l . add ( new pair ( arr [ i ] , count ) ) ; result [ i ] = count ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { int count = 1 ; while ( ! r . isEmpty ( ) && r . peek ( ) . first >= arr [ i ] ) { count += r . peek ( ) . second ; r . pop ( ) ; } r . add ( new pair ( arr [ i ] , count ) ) ; result [ i ] *= count ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( result [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int N = arr . length ; minSubarray ( arr , N ) ; } }
import java . io . * ; class GFG { static void printAlter ( int [ ] arr , int N ) { for ( int currIndex = 0 ; currIndex < N ; currIndex ++ ) { if ( currIndex % 2 == 0 ) { System . out . print ( arr [ currIndex ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; printAlter ( arr , N ) ; } }
import java . io . * ; class GFG { static int findmin ( String s ) { int n = s . length ( ) ; int i , j , maximum = 0 ; int [ ] incr = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { incr [ i + 1 ] = incr [ i ] ; if ( s . charAt ( i ) == '0' ) { incr [ i + 1 ] ++ ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { maximum = Math . max ( maximum , incr [ i ] + j - i + 1 - ( incr [ j + 1 ] - incr [ i ] ) + incr [ n ] - incr [ j + 1 ] ) ; } } return n - maximum ; } public static void main ( String [ ] args ) { String S = "0110100110" ; int minimum = findmin ( S ) ; System . out . println ( minimum ) ; } }
import java . util . * ; class GFG { static int N = 100005 ; static void SieveOfEratosthenes ( boolean [ ] prime , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int digitSum ( int number ) { int sum = 0 ; while ( number > 0 ) { sum += ( number % 10 ) ; number /= 10 ; } return sum ; } static void longestCompositeDigitSumSubsequence ( int [ ] arr , int n ) { int count = 0 ; boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; for ( int i = 0 ; i < n ; i ++ ) { int res = digitSum ( arr [ i ] ) ; if ( res == 1 ) { continue ; } if ( prime [ res ] == false ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 13 , 55 , 7 , 3 , 5 , 1 , 10 , 21 , 233 , 144 , 89 } ; int n = arr . length ; longestCompositeDigitSumSubsequence ( arr , n ) ; } }
import java . util . * ; class GFG { public static boolean isPower ( int x ) { if ( x != 0 && ( x & ( x - 1 ) ) == 0 ) return true ; return false ; } public static void maximumlength ( int arr [ ] , int N ) { int max_length = 0 ; int max_len_subarray = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( isPower ( arr [ i ] ) ) { max_length ++ ; max_len_subarray = Math . max ( max_length , max_len_subarray ) ; } else { max_length = 0 ; } } System . out . println ( max_len_subarray ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 5 , 4 , 6 , 8 , 8 , 2 } ; int N = arr . length ; maximumlength ( arr , N ) ; } }
import java . util . * ; class GFG { static int findLengthOfShortestSubarray ( int arr [ ] , int N ) { int minlength = Integer . MAX_VALUE ; int left = 0 ; int right = N - 1 ; while ( left < right && arr [ left + 1 ] >= arr [ left ] ) { left ++ ; } if ( left == N - 1 ) return 0 ; while ( right > left && arr [ right - 1 ] <= arr [ right ] ) { right -- ; } minlength = Math . min ( N - left - 1 , right ) ; int j = right ; for ( int i = 0 ; i < left + 1 ; i ++ ) { if ( arr [ i ] <= arr [ j ] ) { minlength = Math . min ( minlength , j - i - 1 ) ; } else if ( j < N - 1 ) { j ++ ; } else { break ; } } return minlength ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 3 , 10 , 11 , 15 , 20 , 13 , 3 , 18 , 12 } ; int N = arr . length ; System . out . print ( ( findLengthOfShortestSubarray ( arr , N ) ) ) ; } }
import java . util . * ; class GFG { static void minSub ( int N ) { int count = 0 ; while ( N >= 0 ) { int num = N ; int rev = 0 ; while ( num != 0 ) { int digit = num % 10 ; rev = ( rev * 10 ) + digit ; num = num / 10 ; } if ( N == rev ) { break ; } count ++ ; N -- ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 3456 ; minSub ( N ) ; } }
import java . util . * ; class GFG { static int numberOfGP ( int L [ ] , int N ) { if ( N <= 2 ) return 0 ; int count = 0 ; int res = 0 ; for ( int i = 2 ; i < N ; ++ i ) { if ( L [ i - 1 ] * L [ i - 1 ] == L [ i ] * L [ i - 2 ] ) { ++ count ; } else { count = 0 ; } res += count ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 16 , 24 } ; int N = arr . length ; System . out . print ( numberOfGP ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void conVowUpp ( char [ ] str ) { int N = str . length ; for ( int i = 0 ; i < N ; i ++ ) { if ( str [ i ] == ' a ' str [ i ] == ' e ' str [ i ] == ' i ' str [ i ] == ' o ' str [ i ] == ' u ' ) { char c = Character . toUpperCase ( str [ i ] ) ; str [ i ] = c ; } } for ( char c : str ) System . out . print ( c ) ; } public static void main ( String [ ] args ) { String str = " eutopia " ; conVowUpp ( str . toCharArray ( ) ) ; } }
import java . io . * ; class GFG { static int cntSubarr ( int arr [ ] , int N ) { int res = 0 ; int prefixXor = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { prefixXor = arr [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { prefixXor ^= arr [ j ] ; if ( prefixXor == 0 && ( j - i + 1 ) % 2 == 0 ) { res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . length ; System . out . println ( cntSubarr ( arr , N ) ) ; } }
import java . util . * ; class GFG { static final int M = 1000000 ; static int cntSubXor ( int arr [ ] , int N ) { int prefixXor = 0 ; int [ ] Even = new int [ M ] ; int [ ] Odd = new int [ M ] ; int cntSub = 0 ; Odd [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { prefixXor ^= arr [ i ] ; if ( i % 2 == 1 ) { cntSub += Odd [ prefixXor ] ; Odd [ prefixXor ] ++ ; } else { cntSub += Even [ prefixXor ] ; Even [ prefixXor ] ++ ; } } return cntSub ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 3 , 6 , 7 , 8 } ; int N = arr . length ; System . out . print ( cntSubXor ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int S ; static void findMaxSum ( int [ ] arr , int n , int [ ] min , int k ) { ArrayList < int [ ] > A = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { A . add ( new int [ ] { arr [ i ] , min [ i ] } ) ; } Collections . sort ( A , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; int K = 0 ; for ( int i = 0 ; i < n ; i ++ ) { S += Math . max ( A . get ( i ) [ 0 ] - K , A . get ( i ) [ 1 ] ) ; K += k ; } } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 2 , 1 } ; int [ ] min = { 3 , 2 , 1 , 3 } ; int N = arr . length ; int K = 3 ; S = 0 ; findMaxSum ( arr , N , min , K ) ; System . out . println ( S ) ; } }
import java . util . ArrayList ; import java . util . HashMap ; class GFG { static int minLength ( String a , String b ) { HashMap < Character , Integer > Char = new HashMap < > ( ) ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { Char . put ( b . charAt ( i ) , Char . getOrDefault ( b . charAt ( i ) , 0 ) + 1 ) ; } HashMap < Character , ArrayList < Integer > > CharacterIndex = new HashMap < > ( ) ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) { char x = a . charAt ( i ) ; if ( Char . containsKey ( x ) ) { if ( CharacterIndex . get ( x ) == null ) { CharacterIndex . put ( x , new ArrayList < Integer > ( ) ) ; } CharacterIndex . get ( x ) . add ( i ) ; } } int len = Integer . MAX_VALUE ; int flag ; while ( true ) { flag = 1 ; int firstVar = 0 , lastVar = 0 ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { if ( i == 0 ) { if ( CharacterIndex . containsKey ( i ) ) { flag = 0 ; break ; } else { int x = CharacterIndex . get ( b . charAt ( i ) ) . get ( 0 ) ; CharacterIndex . get ( b . charAt ( i ) ) . remove ( CharacterIndex . get ( b . charAt ( i ) ) . get ( 0 ) ) ; firstVar = x ; lastVar = x ; } } else { int elementFound = 0 ; for ( var e : CharacterIndex . get ( b . charAt ( i ) ) ) { if ( e > lastVar ) { elementFound = 1 ; lastVar = e ; break ; } } if ( elementFound == 0 ) { flag = 0 ; break ; } } } if ( flag == 0 ) { break ; } len = Math . min ( len , Math . abs ( lastVar - firstVar ) + 1 ) ; } return len ; } public static void main ( String [ ] args ) { String a = " abcdefababaef " ; String b = " abf " ; int len = minLength ( a , b ) ; if ( len != Integer . MAX_VALUE ) { System . out . println ( len ) ; } else { System . out . println ( " Impossible " ) ; } } }
class GFG { static int MinLen ( String str , int K ) { int N = str . length ( ) ; int i = 0 ; while ( i < N - 1 ) { if ( str . charAt ( i ) != str . charAt ( i + 1 ) ) { break ; } i ++ ; } if ( i == N - 1 ) { return N ; } return Math . max ( 1 , N - K ) ; } public static void main ( String [ ] args ) { String str = " aabc " ; int K = 1 ; System . out . print ( MinLen ( str , K ) ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int N , P ; static boolean helper ( int mid ) { int cnt = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int temp = i . getValue ( ) ; while ( temp >= mid ) { temp -= mid ; cnt ++ ; } } return cnt >= N ; } static int findMaximumDays ( int arr [ ] ) { for ( int i = 0 ; i < P ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int start = 0 , end = P , ans = 0 ; while ( start <= end ) { int mid = start + ( ( end - start ) / 2 ) ; if ( mid != 0 && helper ( mid ) ) { ans = mid ; start = mid + 1 ; } else if ( mid == 0 ) { start = mid + 1 ; } else { end = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { N = 3 ; P = 10 ; int arr [ ] = { 1 , 2 , 2 , 1 , 1 , 3 , 3 , 3 , 2 , 4 } ; System . out . print ( findMaximumDays ( arr ) ) ; } }
import java . util . * ; class GFG { static double temp ( int n , int x ) { return Math . pow ( x * 1.0 / n , n ) ; } static boolean check ( int n , int y , int x ) { double v = temp ( n , x ) ; return ( v >= y ) ; } static void find ( int x , int y ) { int high = ( int ) Math . floor ( x / Math . exp ( 1.0 ) ) ; int low = 1 ; if ( x == y ) System . out . print ( 1 + "NEW_LINE"); else if ( ! check ( high , y , x ) ) System . out . print ( - 1 + "NEW_LINE"); else { while ( high - low > 1 ) { int mid = ( high + low ) / 2 ; if ( check ( mid , y , x ) ) high = mid ; else low = mid ; } System . out . print ( high + "NEW_LINE"); } } public static void main ( String [ ] args ) { int x = 9 , y = 8 ; find ( x , y ) ; } }
import java . util . * ; class GFG { static int sum ; static class Node { int data ; Node left , right ; } ; static Node newnode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static Node insert ( String s , int i , int N , Node root , Node temp ) { if ( i == N ) return temp ; if ( s . charAt ( i ) == ' L ' ) root . left = insert ( s , i + 1 , N , root . left , temp ) ; else root . right = insert ( s , i + 1 , N , root . right , temp ) ; return root ; } static int SBTUtil ( Node root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return root . data ; int left = SBTUtil ( root . left ) ; int right = SBTUtil ( root . right ) ; if ( root . left != null && root . right != null ) { if ( ( left % 2 == 0 && right % 2 != 0 ) || ( left % 2 != 0 && right % 2 == 0 ) ) { sum += root . data ; } } return left + right + root . data ; } static Node build_tree ( int R , int N , String str [ ] , int values [ ] ) { Node root = newnode ( R ) ; int i ; for ( i = 0 ; i < N - 1 ; i ++ ) { String s = str [ i ] ; int x = values [ i ] ; Node temp = newnode ( x ) ; root = insert ( s , 0 , s . length ( ) , root , temp ) ; } return root ; } static void speciallyBalancedNodes ( int R , int N , String str [ ] , int values [ ] ) { Node root = build_tree ( R , N , str , values ) ; sum = 0 ; SBTUtil ( root ) ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 7 ; int R = 12 ; String str [ ] = { " L " , " R " , " RL " , " RR " , " RLL " , " RLR " } ; int values [ ] = { 17 , 16 , 4 , 9 , 2 , 3 } ; speciallyBalancedNodes ( R , N , str , values ) ; } }
import java . util . * ; class GFG { static Map < Integer , Vector < Integer > > M ; static class node { int data ; node left ; node right ; public node ( ) { } public node ( int data , node left , node right ) { super ( ) ; this . data = data ; this . left = left ; this . right = right ; } } ; static node newnode ( int d ) { node temp = new node ( ) ; temp . data = d ; temp . left = null ; temp . right = null ; return temp ; } static void dfs ( node root , int l ) { if ( root == null ) return ; if ( ! M . containsKey ( l ) ) { Vector < Integer > temp = new Vector < Integer > ( ) ; temp . add ( root . data ) ; M . put ( l , temp ) ; } else M . get ( l ) . add ( root . data ) ; dfs ( root . left , l + 1 ) ; dfs ( root . right , l + 1 ) ; } static void printMidNodes ( node root ) { M = new HashMap < Integer , Vector < Integer > > ( ) ; dfs ( root , 0 ) ; for ( Map . Entry < Integer , Vector < Integer > > it : M . entrySet ( ) ) { int size = it . getValue ( ) . size ( ) ; if ( size % 2 == 1 ) { System . out . print ( it . getValue ( ) . get ( ( size - 1 ) / 2 ) + "NEW_LINE"); } else { System . out . print ( it . getValue ( ) . get ( ( size - 1 ) / 2 ) + " ▁ " + it . getValue ( ) . get ( ( ( size - 1 ) / 2 ) + 1 ) + "NEW_LINE"); } } } 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 . left . right . left = newnode ( 11 ) ; root . left . right . right = newnode ( 6 ) ; root . left . right . right . left = newnode ( 7 ) ; root . left . right . right . right = newnode ( 9 ) ; root . right . left = newnode ( 10 ) ; root . right . right = newnode ( 8 ) ; printMidNodes ( root ) ; } }
import java . util . * ; class GFG { static class node { node left ; node right ; } ; static void insert ( int x , node head ) { node curr = head ; for ( int i = 30 ; i >= 0 ; i -- ) { int val = ( x >> i ) & 1 ; if ( val == 0 ) { if ( curr . left == null ) curr . left = new node ( ) ; curr = curr . left ; } else { if ( curr . right == null ) curr . right = new node ( ) ; curr = curr . right ; } } } static int findMaximumXOR ( int arr [ ] , int n ) { node head = new node ( ) ; for ( int i = 0 ; i < n ; i ++ ) { insert ( arr [ i ] , head ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_xor = 0 ; int M = ( int ) Math . pow ( 2 , 30 ) ; node curr = head ; for ( int j = 30 ; j >= 0 ; j -- ) { int val = ( arr [ i ] >> j ) & 1 ; if ( val == 0 ) { if ( curr . right != null ) { curr_xor += M ; curr = curr . right ; } else { curr = curr . left ; } } else { if ( curr . left != null ) { curr_xor += M ; curr = curr . left ; } else { curr = curr . right ; } } M /= 2 ; } ans = Math . max ( ans , curr_xor ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . print ( findMaximumXOR ( arr , N ) ) ; } }
import java . io . * ; class GFG { static void PrintIndexes ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int currSubSum = 0 ; for ( int j = i ; j < N ; j ++ ) { currSubSum += arr [ j ] ; int sq = ( int ) Math . sqrt ( currSubSum ) ; if ( sq * sq == currSubSum ) { System . out . print ( " ( " + i + " , " + j + " ) " + " ▁ " ) ; } } } } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 65 , 79 , 81 } ; PrintIndexes ( arr , arr . length ) ; } }
import java . util . * ; class GFG { static void position ( int arr [ ] [ ] , int N ) { int pos = - 1 ; int right = Integer . MIN_VALUE ; int left = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 1 ] > right ) { right = arr [ i ] [ 1 ] ; } if ( arr [ i ] [ 0 ] < left ) { left = arr [ i ] [ 0 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] [ 0 ] == left && arr [ i ] [ 1 ] == right ) { pos = i + 1 ; } } System . out . print ( pos + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 3 } , { 1 , 3 } , { 2 , 2 } , { 2 , 3 } , { 1 , 2 } } ; int N = arr . length ; position ( arr , N ) ; } }
import java . util . * ; class GFG { static void maxAtLevel ( int N , int M , int [ ] Value , int Edges [ ] [ ] ) { Vector < Integer > [ ] adj = new Vector [ N ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; adj [ u ] . add ( v ) ; } Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; while ( ! q . isEmpty ( ) ) { int count = q . size ( ) ; int maxVal = 0 ; while ( count -- > 0 ) { int temp = q . peek ( ) ; q . remove ( ) ; maxVal = Math . max ( maxVal , Value [ temp ] ) ; for ( int i = 0 ; i < adj [ temp ] . size ( ) ; i ++ ) { q . add ( adj [ temp ] . get ( i ) ) ; } } System . out . print ( maxVal + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 10 ; int Edges [ ] [ ] = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 3 , 6 } , { 6 , 7 } , { 6 , 8 } , { 6 , 9 } } ; int [ ] Value = { 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 6 , 12 , 7 } ; maxAtLevel ( N , N - 1 , Value , Edges ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void ctTriplets ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int [ ] temp = new int [ N ] ; int index = 0 ; for ( int i = 1 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } for ( int i = 0 ; i < N ; i += 2 ) { temp [ i ] = arr [ index ++ ] ; } int ct = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i > 0 && i + 1 < N ) { if ( temp [ i ] < temp [ i + 1 ] && temp [ i ] < temp [ i - 1 ] ) { ct ++ ; } } } System . out . println ( " Count ▁ of ▁ triplets : " + ct ) ; System . out . print ( " Array : " ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( temp [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; ctTriplets ( arr , N ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void countSubarrays ( int a [ ] , int n , int k ) { int ans = 0 ; ArrayList < Integer > pref = new ArrayList < > ( ) ; pref . add ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref . add ( ( a [ i ] + pref . get ( i ) ) % k ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { if ( ( pref . get ( j ) - pref . get ( i - 1 ) + k ) % k == j - i + 1 ) { ans ++ ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 2 , 3 , 5 , 3 , 1 , 5 } ; int N = arr . length ; int K = 4 ; countSubarrays ( arr , N , K ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void countSubarrays ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > cnt = new HashMap < > ( ) ; long ans = 0 ; ArrayList < Integer > pref = new ArrayList < > ( ) ; pref . add ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) pref . add ( ( a [ i ] + pref . get ( i ) ) % k ) ; cnt . put ( 0 , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { int remIdx = i - k ; if ( remIdx >= 0 ) { if ( cnt . containsKey ( ( pref . get ( remIdx ) - remIdx % k + k ) % k ) ) cnt . put ( ( pref . get ( remIdx ) - remIdx % k + k ) % k , cnt . get ( ( pref . get ( remIdx ) - remIdx % k + k ) % k ) - 1 ) ; else cnt . put ( ( pref . get ( remIdx ) - remIdx % k + k ) % k , - 1 ) ; } if ( cnt . containsKey ( ( pref . get ( i ) - i % k + k ) % k ) ) ans += cnt . get ( ( pref . get ( i ) - i % k + k ) % k ) ; if ( cnt . containsKey ( ( pref . get ( i ) - i % k + k ) % k ) ) cnt . put ( ( pref . get ( i ) - i % k + k ) % k , cnt . get ( ( pref . get ( i ) - i % k + k ) % k ) + 1 ) ; else cnt . put ( ( pref . get ( i ) - i % k + k ) % k , 1 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 2 , 3 , 5 , 3 , 1 , 5 } ; int N = arr . length ; int K = 4 ; countSubarrays ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { public static boolean check ( int [ ] a , int n ) { int [ ] b = new int [ n ] ; int minElement = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] = a [ i ] ; minElement = Math . min ( minElement , a [ i ] ) ; } Arrays . sort ( b ) ; int k = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] && a [ i ] % minElement != 0 ) { k = 0 ; break ; } } return k == 1 ? true : false ; } public static void main ( String [ ] args ) { int [ ] a = { 4 , 3 , 6 , 6 , 2 , 9 } ; int n = a . length ; if ( check ( a , n ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static int ctMinEdits ( String str1 , String str2 ) { int N1 = str1 . length ( ) ; int N2 = str2 . length ( ) ; int freq1 [ ] = new int [ 256 ] ; Arrays . fill ( freq1 , 0 ) ; for ( int i = 0 ; i < N1 ; i ++ ) { freq1 [ str1 . charAt ( i ) ] ++ ; } int freq2 [ ] = new int [ 256 ] ; Arrays . fill ( freq2 , 0 ) ; for ( int i = 0 ; i < N2 ; i ++ ) { freq2 [ str2 . charAt ( i ) ] ++ ; } for ( int i = 0 ; i < 256 ; i ++ ) { if ( freq1 [ i ] > freq2 [ i ] ) { freq1 [ i ] = freq1 [ i ] - freq2 [ i ] ; freq2 [ i ] = 0 ; } else { freq2 [ i ] = freq2 [ i ] - freq1 [ i ] ; freq1 [ i ] = 0 ; } } int sum1 = 0 ; int sum2 = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { sum1 += freq1 [ i ] ; sum2 += freq2 [ i ] ; } return Math . max ( sum1 , sum2 ) ; } public static void main ( final String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " geeksforcoder " ; System . out . println ( ctMinEdits ( str1 , str2 ) ) ; } }
import java . util . * ; class GFG { static int minOperation ( String s ) { int n = s . length ( ) ; int temp1 = - 1 , temp2 = - 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr = s . charAt ( i ) ; if ( curr == '0' ) { if ( temp1 == - 1 && temp2 == - 1 ) { temp1 = i ; } else if ( temp1 != - 1 && temp2 == - 1 && i - temp1 == 1 ) { temp2 = i ; } else if ( temp1 != - 1 ) { temp1 = - 1 ; ans ++ ; } else if ( temp1 == - 1 && temp2 != - 1 && i - temp2 != 1 ) { temp2 = - 1 ; ans ++ ; } } } if ( temp1 != - 1 && temp2 != - 1 ) { ans += 2 ; } else if ( temp1 != - 1 temp2 != - 1 ) { ans += 1 ; } return ans ; } public static void main ( String [ ] args ) { String s = "110010" ; System . out . println ( minOperation ( s ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int findMinLength ( int [ ] arr ) { int index = ( int ) arr . length - 1 ; while ( index > 0 && arr [ index ] >= arr [ index - 1 ] ) { index -- ; } return index ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 5 , 0 , - 1 , - 1 , 0 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinLength ( arr ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void CountPairs ( int a [ ] , int b [ ] , int n ) { int C [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { C [ i ] = a [ i ] + b [ i ] ; } HashMap < Integer , Integer > freqCount = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! freqCount . containsKey ( C [ i ] ) ) freqCount . put ( C [ i ] , 1 ) ; else freqCount . put ( C [ i ] , freqCount . get ( C [ i ] ) + 1 ) ; } int NoOfPairs = 0 ; for ( Map . Entry < Integer , Integer > x : freqCount . entrySet ( ) ) { int y = x . getValue ( ) ; NoOfPairs = NoOfPairs + y * ( y - 1 ) / 2 ; } System . out . println ( NoOfPairs ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 20 , 3 , 10 , 5 } ; int brr [ ] = { 9 , 6 , 1 , 7 , 11 , 6 } ; int N = arr . length ; CountPairs ( arr , brr , N ) ; } }
import java . io . * ; class GFG { static void convertBfromA ( int a , int b ) { int moves = 0 ; int x = Math . abs ( a - b ) ; for ( int i = 10 ; i > 0 ; i -- ) { moves += x / i ; x = x % i ; } System . out . print ( moves + " ▁ " ) ; } public static void main ( String [ ] args ) { int A = 188 , B = 4 ; convertBfromA ( A , B ) ; } }
import java . io . * ; class GFG { static int smallestNth ( int A , int B , int N ) { int res = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = N ; j > i ; j -- ) { if ( ( B - A ) % ( j - i ) == 0 ) { int D = ( B - A ) / ( j - i ) ; int FirstTerm = A - ( i - 1 ) * D ; int NthTerm = FirstTerm + ( N - 1 ) * D ; if ( FirstTerm > 0 ) res = Math . min ( res , NthTerm ) ; } } } return res ; } public static void main ( String [ ] args ) { int N = 3 ; int A = 1 ; int B = 6 ; System . out . print ( smallestNth ( A , B , N ) ) ; } }
import java . util . * ; class GFG { static void sieve ( boolean prime [ ] ) { for ( int i = 0 ; i < 1000000 ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j < 1000000 ; j += i ) { prime [ j ] = false ; } } } } static void subPrimeSum ( int N , int K , int arr [ ] , boolean prime [ ] ) { int currSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { currSum += arr [ i ] ; } if ( prime [ currSum ] ) { for ( int i = 0 ; i < K ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } int st = 1 , en = K ; while ( en < N ) { currSum += arr [ en ] - arr [ st - 1 ] ; if ( prime [ currSum ] ) { for ( int i = st ; i <= en ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } en ++ ; st ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 7 , 5 , 4 , 3 , 11 , 99 , 87 , 23 , 45 } ; int K = 4 ; int N = arr . length ; boolean [ ] prime = new boolean [ 1000000 ] ; sieve ( prime ) ; subPrimeSum ( N , K , arr , prime ) ; } }
import java . io . * ; class GFG { public static int maximumSum ( int [ ] a , int [ ] b , int n , int index , int lastpicked ) { if ( index == n ) return 0 ; int option1 = 0 , option2 = 0 ; if ( lastpicked == - 1 a [ lastpicked ] != a [ index ] ) option1 = b [ index ] + maximumSum ( a , b , n , index + 1 , index ) ; option2 = maximumSum ( a , b , n , index + 1 , lastpicked ) ; return Math . max ( option1 , option2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 1 } ; int brr [ ] = { - 1 , 2 , 10 , 20 , - 10 , - 9 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , brr , N , 0 , - 1 ) ) ; } }
import java . io . * ; class GFG { static void sumOfKElements ( int arr [ ] , int n , int k ) { boolean rev = false ; if ( k < 0 ) { rev = true ; k *= - 1 ; int l = 0 , r = n - 1 ; while ( l < r ) { int tmp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = tmp ; l ++ ; r -- ; } } int dp [ ] = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] += dp [ i - 1 ] + arr [ i ] ; } int ans [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + k < n ) ans [ i ] = dp [ i + k ] - dp [ i ] ; else { int x = k - ( n - 1 - i ) ; int y = x / n ; int rem = x % n ; ans [ i ] = dp [ n - 1 ] - dp [ i ] + y * dp [ n - 1 ] + ( rem - 1 >= 0 ? dp [ rem - 1 ] : 0 ) ; } } if ( rev ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , - 5 , 11 } ; int N = arr . length ; int K = 3 ; sumOfKElements ( arr , N , K ) ; } }
import java . util . * ; class GFG { static int binaryInsert ( ArrayList < Integer > boundary , int i ) { int l = 0 ; int r = boundary . size ( ) - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( boundary . get ( m ) < i ) l = m + 1 ; else r = m - 1 ; } boundary . add ( l , i ) ; return l ; } static int [ ] countingSubarray ( int [ ] arr , int n ) { Map < Integer , Integer > index = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) index . put ( arr [ i ] , i ) ; ArrayList < Integer > boundary = new ArrayList < > ( ) ; boundary . add ( - 1 ) ; boundary . add ( n ) ; Arrays . sort ( arr ) ; int [ ] ans = new int [ n ] ; for ( int num : arr ) { int i = binaryInsert ( boundary , index . get ( num ) ) ; int l = boundary . get ( i ) - boundary . get ( i - 1 ) - 1 ; int r = boundary . get ( i + 1 ) - boundary . get ( i ) - 1 ; int cnt = l + r + l * r + 1 ; ans [ index . get ( num ) ] += cnt ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 3 , 2 , 4 , 1 , 5 } ; int [ ] a = countingSubarray ( arr , N ) ; System . out . print ( " [ " ) ; int n = a . length - 1 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " , ▁ " ) ; System . out . print ( a [ n ] + " ] " ) ; } }
import java . util . * ; class GFG { static final int limit = 1000000000 ; static int F ( int N ) { String str = String . valueOf ( N ) ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int val = ( int ) ( str . charAt ( i ) - '0' ) ; sum += val * val * val ; } return sum ; } static int findDestination ( int N ) { HashSet < Integer > s = new HashSet < > ( ) ; int prev = N , next = 0 ; s . add ( N ) ; while ( N <= limit ) { next = F ( N ) ; if ( s . contains ( next ) ) { return next ; } prev = next ; s . add ( prev ) ; N = next ; } return next ; } static void digitCubeLimit ( int N ) { if ( N < 0 ) System . out . print ( "N cannot be negativeNEW_LINE"); else { int ans = findDestination ( N ) ; if ( ans > limit ) System . out . print ( "Limit exceededNEW_LINE"); else if ( ans == F ( ans ) ) { System . out . print ( N ) ; System . out . print ( " ▁ reaches ▁ to ▁ a " + " ▁ fixed ▁ point : ▁ " ) ; System . out . print ( ans ) ; } else { System . out . print ( N ) ; System . out . print ( " ▁ reaches ▁ to ▁ a " + " ▁ limit ▁ cycle : ▁ " ) ; System . out . print ( ans ) ; } } } public static void main ( String [ ] args ) { int N = 3 ; digitCubeLimit ( N ) ; } }
import java . util . * ; class GFG { static int countTuples ( int arr [ ] , int N ) { int ans = 0 , val = 0 ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int j = 0 ; j < N - 2 ; j ++ ) { val = 0 ; for ( int l = j + 1 ; l < N ; l ++ ) { if ( arr [ j ] == arr [ l ] ) { ans += val ; } if ( freq . containsKey ( arr [ l ] ) ) val += freq . get ( arr [ l ] ) ; } if ( freq . containsKey ( arr [ j ] ) ) { freq . put ( arr [ j ] , freq . get ( arr [ j ] ) + 1 ) ; } else { freq . put ( arr [ j ] , 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 , 2 } ; int N = arr . length ; System . out . print ( countTuples ( arr , N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minOperation ( int [ ] arr ) { int minOp = Integer . MIN_VALUE ; int minNeg = 0 , maxPos = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] < 0 ) { if ( arr [ i ] < minNeg ) minNeg = arr [ i ] ; } else { if ( arr [ i ] > maxPos ) maxPos = arr [ i ] ; } } return Math . abs ( minNeg ) + maxPos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 1 } ; System . out . println ( minOperation ( arr ) ) ; } }
import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) { return a ; } return GCD ( b , a % b ) ; } static void GcdPair ( int [ ] [ ] arr , int k ) { int lo = 0 , hi = arr . length - 1 , mid ; int [ ] ans = { - 1 , 0 } ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( GCD ( arr [ mid ] [ 0 ] , arr [ mid ] [ 1 ] ) > k ) { ans = arr [ mid ] ; hi = mid - 1 ; } else lo = mid + 1 ; } if ( ans [ 0 ] == - 1 ) System . out . print ( " - 1" ) ; else System . out . print ( " ( ▁ " + ans [ 0 ] + " , ▁ " + ans [ 1 ] + " ▁ ) " ) ; return ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 3 , 6 } , { 15 , 30 } , { 25 , 75 } , { 30 , 120 } } ; int K = 16 ; GcdPair ( arr , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void addEdge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; adj . get ( v ) . add ( u ) ; } static void DFSUtil ( int u , ArrayList < ArrayList < Integer > > adj , boolean [ ] visited , int componentMin , int componentMax ) { visited [ u ] = true ; componentMax = Math . max ( componentMax , u ) ; componentMin = Math . min ( componentMin , u ) ; for ( int i = 0 ; i < adj . get ( u ) . size ( ) ; i ++ ) if ( visited [ adj . get ( u ) . get ( i ) ] == false ) DFSUtil ( adj . get ( u ) . get ( i ) , adj , visited , componentMin , componentMax ) ; } static boolean isValid ( ArrayList < pair > v ) { int MAX = - 1 ; boolean ans = false ; for ( pair i : v ) { if ( i . first <= MAX ) { ans = true ; } MAX = Math . max ( MAX , i . second ) ; } return ( ans == false ? true : false ) ; } static void DFS ( ArrayList < ArrayList < Integer > > adj , int V ) { ArrayList < pair > v = new ArrayList < > ( ) ; boolean [ ] visited = new boolean [ V + 1 ] ; for ( int u = 1 ; u <= V ; u ++ ) { if ( visited [ u ] == false ) { int componentMax = u ; int componentMin = u ; DFSUtil ( u , adj , visited , componentMin , componentMax ) ; v . add ( new pair ( componentMin , componentMax ) ) ; } } boolean check = isValid ( v ) ; if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; return ; } public static void main ( String [ ] args ) { int N = 4 , K = 3 ; ArrayList < ArrayList < Integer > > adj = new ArrayList < > ( ) ; for ( int i = 0 ; i <= N + 1 ; i ++ ) adj . add ( new ArrayList < > ( ) ) ; addEdge ( adj , 1 , 2 ) ; addEdge ( adj , 2 , 3 ) ; addEdge ( adj , 3 , 4 ) ; DFS ( adj , N ) ; } }
import java . io . * ; class GFG { static int longDelSub ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; int prev_pos = 0 ; int pos [ ] = new int [ M ] ; for ( int i = 0 ; i < M ; i ++ ) { int index = prev_pos ; while ( index < N && str1 . charAt ( index ) != str2 . charAt ( i ) ) { index ++ ; } pos [ i ] = index ; prev_pos = index + 1 ; } int res = N - prev_pos ; prev_pos = N - 1 ; for ( int i = M - 1 ; i >= 0 ; i -- ) { int index = prev_pos ; while ( index >= 0 && str1 . charAt ( index ) != str2 . charAt ( i ) ) { index -- ; } if ( i != 0 ) { res = Math . max ( res , index - pos [ i - 1 ] - 1 ) ; } prev_pos = index - 1 ; } res = Math . max ( res , prev_pos + 1 ) ; return res ; } public static void main ( String [ ] args ) { String str1 = " GeeksforGeeks " ; String str2 = " forks " ; System . out . print ( longDelSub ( str1 , str2 ) ) ; } }
import java . io . * ; class GFG { static void solve ( String S ) { int count_0 = 0 , count_1 = 0 ; int prev = - 1 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '0' ) count_0 ++ ; else if ( S . charAt ( i ) == '1' ) count_1 ++ ; } for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == ' * ' ) continue ; else if ( S . charAt ( i ) == '0' && prev == - 1 ) { count_0 = count_0 + i ; prev = i ; } else if ( S . charAt ( i ) == '1' && prev == - 1 ) { prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) / 2 ; count_1 = count_1 + ( i - prev - 1 ) / 2 ; prev = i ; } else if ( S . charAt ( prev ) == '1' && S . charAt ( i ) == '1' ) { count_1 = count_1 + ( i - prev - 1 ) ; prev = i ; } else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '1' ) prev = i ; else if ( S . charAt ( prev ) == '0' && S . charAt ( i ) == '0' ) { count_0 = count_0 + ( i - prev - 1 ) ; prev = i ; } } if ( count_0 > count_1 ) System . out . print ( "0" ) ; else if ( count_1 > count_0 ) System . out . print ( "1" ) ; else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { String str = " * *0 * * 1 * * *0" ; solve ( str ) ; } }
import java . util . * ; class GFG { static int minFirstTerm ( int X , int diff , int N ) { int first_term = Integer . MAX_VALUE ; int low = 0 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( X - mid * diff > 0 ) { first_term = X - mid * diff ; low = mid + 1 ; } else high = mid - 1 ; } return first_term ; } static void printAP ( int N , int X , int Y ) { if ( X > Y ) { X = X + Y ; Y = X - Y ; X = X - Y ; } int maxDiff = Y - X ; int first_term = Integer . MAX_VALUE , diff = 0 ; for ( int i = 1 ; i * i <= maxDiff ; i ++ ) { if ( maxDiff % i == 0 ) { int diff1 = i ; int diff2 = maxDiff / diff1 ; int terms1 = diff2 + 1 ; int terms2 = diff1 + 1 ; int first_term1 = minFirstTerm ( X , diff1 , N - terms1 ) ; int first_term2 = minFirstTerm ( X , diff2 , N - terms2 ) ; if ( first_term1 < first_term ) { first_term = first_term1 ; diff = diff1 ; } if ( first_term2 < first_term ) { first_term = first_term2 ; diff = diff2 ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( first_term + " ▁ " ) ; first_term += diff ; } } public static void main ( String [ ] args ) { int N = 5 , X = 10 , Y = 15 ; printAP ( N , X , Y ) ; } }
import java . util . * ; class GFG { static boolean isCubeSum ( int n ) { for ( int i = 1 ; i * i * i <= n ; i ++ ) { if ( i * i * i + ( i + 1 ) * ( i + 1 ) * ( i + 1 ) == n ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 35 ; if ( isCubeSum ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isSumCube ( int N ) { int a = ( int ) Math . cbrt ( N ) ; int b = a - 1 ; return ( ( a * a * a + b * b * b ) == N ) ; } public static void main ( String [ ] args ) { int i = 35 ; if ( isSumCube ( i ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static int maxweight ( int s , int e , Map < Integer , Integer > pre ) { if ( s == e ) return 0 ; int ans = 0 ; for ( int i = s ; i < e ; i ++ ) { int left = pre . get ( i ) - pre . get ( s - 1 ) ; int right = pre . get ( e ) - pre . get ( i ) ; if ( left < right ) ans = Math . max ( ans , left + maxweight ( s , i , pre ) ) ; if ( left == right ) { ans = Math . max ( ans , Math . max ( left + maxweight ( s , i , pre ) , right + maxweight ( i + 1 , e , pre ) ) ) ; } if ( left > right ) ans = Math . max ( ans , right + maxweight ( i + 1 , e , pre ) ) ; } return ans ; } static void maxSum ( List < Integer > arr ) { Map < Integer , Integer > pre = new HashMap < > ( ) ; pre . put ( - 1 , 0 ) ; pre . put ( 0 , arr . get ( 0 ) ) ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { pre . put ( i , pre . getOrDefault ( i - 1 , 0 ) + arr . get ( i ) ) ; } System . out . println ( maxweight ( 0 , arr . size ( ) - 1 , pre ) ) ; } public static void main ( String [ ] args ) { List < Integer > arr = Arrays . asList ( 6 , 2 , 3 , 4 , 5 , 5 ) ; maxSum ( arr ) ; } }
import java . util . * ; class GFG { static String largestTimeFromDigits ( int [ ] A ) { HashMap < Integer , Integer > mp1 = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mp2 = new HashMap < Integer , Integer > ( ) ; for ( int x : A ) { if ( mp1 . containsKey ( x ) ) mp1 . put ( x , mp1 . get ( x ) + 1 ) ; else mp1 . put ( x , 1 ) ; } mp2 = ( HashMap < Integer , Integer > ) mp1 . clone ( ) ; int hr = 23 , m = 59 , s = 59 ; while ( hr >= 0 ) { int h0 = hr / 10 , h1 = hr % 10 ; int m0 = m / 10 , m1 = m % 10 ; int s0 = s / 10 , s1 = s % 10 ; int p = 0 ; int [ ] arr = { h0 , h1 , m0 , m1 , s0 , s1 } ; for ( int it : arr ) { if ( mp1 . containsKey ( it ) && mp1 . get ( it ) > 0 ) { mp1 . put ( it , mp1 . get ( it ) - 1 ) ; } else { p = 1 ; } } if ( p == 0 ) { String st = " " ; st = String . valueOf ( h0 ) + String . valueOf ( h1 ) ; st += ' : ' + String . valueOf ( m0 ) + String . valueOf ( m1 ) ; st += ' : ' + String . valueOf ( s0 ) + String . valueOf ( s1 ) ; return st ; } mp1 = ( HashMap < Integer , Integer > ) mp2 . clone ( ) ; if ( s == 0 ) { s = 59 ; m -- ; } else if ( m < 0 ) { m = 59 ; hr -- ; } if ( s > 0 ) { s -- ; } } return " - 1" ; } public static void main ( String [ ] args ) { int [ ] v = { 0 , 2 , 1 , 9 , 3 , 2 } ; System . out . print ( largestTimeFromDigits ( v ) ) ; } }
class GFG { static void countNumEnds ( int A , int L , int R ) { int temp , count = 0 , digits ; int cycle ; digits = ( int ) ( Math . log10 ( A ) + 1 ) ; temp = ( int ) Math . pow ( 10 , digits ) ; cycle = temp ; while ( temp <= R ) { if ( temp >= L ) count ++ ; temp += cycle ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A = 2 , L = 2 , R = 20 ; countNumEnds ( A , L , R ) ; } }
class GFG { public static int minOperations ( String s ) { int len = s . length ( ) ; int result = 0 ; for ( int i = 0 ; i < len / 2 ; i ++ ) { int D1 = Math . max ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) - Math . min ( s . charAt ( i ) , s . charAt ( len - 1 - i ) ) ; int D2 = 26 - D1 ; result += Math . min ( D1 , D2 ) ; } return result ; } public static void main ( String [ ] args ) { String s = " abccdb " ; System . out . println ( minOperations ( s ) ) ; } }
import java . util . * ; class GFG { static boolean findFactor ( int value , HashSet < Integer > factors ) { factors . add ( value ) ; for ( int i = 2 ; i * i <= value ; i ++ ) { if ( value % i == 0 ) { if ( value / i == i ) { if ( factors . contains ( i ) ) { return true ; } else { factors . add ( i ) ; } } else { if ( factors . contains ( i ) || factors . contains ( value / i ) ) { return true ; } else { factors . add ( i ) ; factors . add ( value / i ) ; } } } } return false ; } static boolean allCoprime ( int A [ ] , int n ) { boolean all_coprime = true ; HashSet < Integer > factors = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == 1 ) continue ; if ( findFactor ( A [ i ] , factors ) ) { all_coprime = false ; break ; } } return all_coprime ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 11 , 7 , 19 } ; int arr_size = A . length ; if ( allCoprime ( A , arr_size ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . Arrays ; class GFG { static double countSubSequence ( int [ ] arr , int n ) { int maximum = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int minimum = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; if ( maximum == minimum ) return Math . pow ( 2 , n ) - 1 ; int i = count ( arr , maximum ) ; int j = count ( arr , minimum ) ; double res = ( Math . pow ( 2 , i ) - 1 ) * ( Math . pow ( 2 , j ) - 1 ) * Math . pow ( 2 , n - i - j ) ; return res ; } static int count ( int [ ] arr , int value ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] == value ) sum ++ ; return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( countSubSequence ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int transformSubsequence ( int n , int m , int [ ] A , int [ ] B ) { if ( B . length == 0 ) return n ; int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( A [ i - 1 ] == B [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return n - dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int N = 5 ; int M = 6 ; int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 2 , 5 , 6 , 4 , 9 , 12 } ; System . out . print ( transformSubsequence ( N , M , A , B ) ) ; } }
import java . util . * ; class GFG { static int checkForPerfectSquare ( int [ ] arr , int i , int j ) { int mid , sum = 0 ; for ( int m = i ; m <= j ; m ++ ) { sum += arr [ m ] ; } int low = 0 , high = sum / 2 ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( mid * mid == sum ) { return mid ; } else if ( mid * mid > sum ) { high = mid - 1 ; } else { low = mid + 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 19 , 33 , 48 , 90 , 100 } ; int L = 1 , R = 3 ; System . out . print ( checkForPerfectSquare ( arr , L , R ) ) ; } }
import java . util . * ; class GFG { static int calcSubarray ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { boolean flag = true ; int comm_diff = A [ i + 1 ] - A [ i ] ; for ( int k = i ; k < j ; k ++ ) { if ( A [ k + 1 ] - A [ k ] == comm_diff ) { continue ; } else { flag = false ; break ; } } if ( flag ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] A = { 8 , 7 , 4 , 1 , 0 } ; int N = A . length ; System . out . print ( calcSubarray ( A , N ) ) ; } }
import java . io . * ; import java . lang . Math ; import java . util . * ; public class GFG { public static int maxSumSubarray ( int [ ] arr ) { int i = 0 , j = 1 ; HashSet < Integer > set = new HashSet < Integer > ( ) ; set . add ( arr [ 0 ] ) ; int sum = arr [ 0 ] ; int maxsum = sum ; while ( i < arr . length - 1 && j < arr . length ) { if ( ! set . contains ( arr [ j ] ) ) { sum = sum + arr [ j ] ; maxsum = Math . max ( sum , maxsum ) ; set . add ( arr [ j ++ ] ) ; } else { sum -= arr [ i ] ; set . remove ( arr [ i ++ ] ) ; } } return maxsum ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 1 , 5 } ; int ans = maxSumSubarray ( arr ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; for ( int n = L ; n <= R ; n ++ ) { int no_of_bits = ( int ) ( Math . log ( n ) + 1 ) ; int no_of_set_bits = Integer . bitCount ( n ) ; if ( no_of_bits - no_of_set_bits == 1 ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 9 ; System . out . print ( count_numbers ( L , R ) ) ; } }
import java . util . * ; class GFG { static int count_numbers ( int L , int R ) { int ans = 0 ; int LogR = ( int ) ( Math . log ( R ) + 1 ) ; for ( int zero_bit = 0 ; zero_bit < LogR ; zero_bit ++ ) { int cur = 0 ; for ( int j = 0 ; j < zero_bit ; j ++ ) { cur |= ( 1L << j ) ; } for ( int j = zero_bit + 1 ; j < LogR ; j ++ ) { cur |= ( 1L << j ) ; if ( cur >= L && cur <= R ) { ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 9 ; System . out . print ( count_numbers ( L , R ) ) ; } }
import java . util . * ; class GFG { static int countElements ( int [ ] a , int n ) { int [ ] cnt = new int [ n + 1 ] ; int ans = 0 ; for ( int k : a ) { ++ cnt [ k ] ; } for ( int l = 0 ; l < n ; ++ l ) { int sum = 0 ; for ( int r = l ; r < n ; ++ r ) { sum += a [ r ] ; if ( l == r ) continue ; if ( sum <= n ) { ans += cnt [ sum ] ; cnt [ sum ] = 0 ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 1 , 1 } ; System . out . println ( countElements ( a , a . length ) ) ; } }
class GFG { static int maxGCD ( int n ) { return ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( maxGCD ( n ) ) ; } }
import java . util . * ; class GFG { static String removeOddIndexCharacters ( String s ) { String new_string = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 1 ) continue ; new_string += s . charAt ( i ) ; } return new_string ; } public static void main ( String [ ] args ) { String str = " abcdef " ; str = removeOddIndexCharacters ( str ) ; System . out . print ( str ) ; } }
import java . io . * ; class GFG { static int longestGP ( int A [ ] , int N ) { if ( N < 2 ) return N ; int length = 1 , common_ratio = 1 ; int maxlength = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i + 1 ] % A [ i ] == 0 ) { if ( A [ i + 1 ] / A [ i ] == common_ratio ) { length = length + 1 ; maxlength = Math . max ( maxlength , length ) ; } else { common_ratio = A [ i + 1 ] / A [ i ] ; length = 2 ; } } else { maxlength = Math . max ( maxlength , length ) ; length = 1 ; } } maxlength = Math . max ( maxlength , length ) ; return maxlength ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 7 , 14 , 28 , 56 , 89 } ; int N = arr . length ; System . out . println ( longestGP ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int uncrossedLines ( int [ ] a , int [ ] b , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) dp [ i ] [ j ] = 0 ; else if ( a [ i - 1 ] == b [ j - 1 ] ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 9 , 2 } ; int B [ ] = { 3 , 2 , 9 } ; int N = A . length ; int M = B . length ; System . out . print ( uncrossedLines ( A , B , N , M ) ) ; } }
class GFG { static int minFlips ( int mat [ ] [ ] , String s ) { int N = mat . length ; int M = mat [ 0 ] . length ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != s . charAt ( i + j ) - '0' ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 1 } , { 0 , 1 , 1 } , { 0 , 0 , 0 } } ; String s = "10001" ; System . out . print ( minFlips ( mat , s ) ) ; } }
import java . util . * ; class GFG { static void lcmpair ( int l , int r ) { int x , y ; x = l ; y = 2 * l ; if ( y > r ) { System . out . print ( "-1NEW_LINE"); } else { System . out . print ( " X ▁ = ▁ " + x + " ▁ Y ▁ = ▁ " + y + "NEW_LINE"); } } public static void main ( String [ ] args ) { int l = 13 , r = 69 ; lcmpair ( l , r ) ; } }
import java . io . * ; class GFG { static int revNum ( int N ) { int x = 0 ; while ( N != 0 ) { x = x * 10 + N % 10 ; N = N / 10 ; } return x ; } static int ctNonPalin ( int arr [ ] , int N ) { int Res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int x = revNum ( arr [ i ] ) ; if ( x == arr [ i ] ) { continue ; } else { if ( arr [ i ] % 10 == x % 10 ) Res += 1 ; } } return Res ; } public static void main ( String [ ] args ) { int arr [ ] = { 121 , 134 , 2342 , 4514 } ; int N = arr . length ; System . out . println ( ctNonPalin ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int minlt ( int [ ] arr , int target , int n ) { int [ ] [ ] dp = new int [ arr . length + 1 ] [ target + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; for ( int i = 0 ; i < arr . length + 1 ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j < target + 1 ; j ++ ) dp [ 0 ] [ j ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= arr . length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { if ( arr [ i - 1 ] > j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } else { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , ( dp [ i ] [ j - arr [ i - 1 ] ] ) != Integer . MAX_VALUE ? ( dp [ i ] [ j - arr [ i - 1 ] ] + 1 ) : Integer . MAX_VALUE ) ; } } } if ( dp [ arr . length ] [ target ] == Integer . MAX_VALUE ) { return - 1 ; } else { return dp [ arr . length ] [ target ] ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 4 , 1 } ; int target = 11 ; int n = arr . length ; System . out . print ( minlt ( arr , target , n ) ) ; } }
import java . util . * ; class GFG { static boolean isSame ( String str , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( mp . containsKey ( str . charAt ( i ) - ' a ' ) ) { mp . put ( str . charAt ( i ) - ' a ' , mp . get ( str . charAt ( i ) - ' a ' ) + 1 ) ; } else { mp . put ( str . charAt ( i ) - ' a ' , 1 ) ; } } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( ( it . getValue ( ) ) >= n ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String str = " ccabcba " ; int n = 4 ; if ( isSame ( str , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static int minServerLoads ( int n , int [ ] servers ) { int totalLoad = 0 ; for ( int i = 0 ; i < servers . length ; i ++ ) totalLoad += servers [ i ] ; int requiredLoad = totalLoad / 2 ; int dp [ ] [ ] = new int [ n + 1 ] [ requiredLoad + 1 ] ; for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < requiredLoad + 1 ; j ++ ) { if ( servers [ i - 1 ] > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , servers [ i - 1 ] + dp [ i - 1 ] [ j - servers [ i - 1 ] ] ) ; } } return totalLoad - 2 * dp [ n ] [ requiredLoad ] ; } public static void main ( String [ ] args ) { int N = 5 ; int servers [ ] = { 1 , 2 , 3 , 4 , 5 } ; System . out . print ( minServerLoads ( N , servers ) ) ; } }
import java . util . * ; class GFG { static int maxLenSub ( int arr [ ] , int N ) { int Pos = 0 ; int Neg = 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) { Pos = Neg = 0 ; } else if ( arr [ i ] > 0 ) { Pos += 1 ; if ( Neg != 0 ) { Neg += 1 ; } res = Math . max ( res , Pos ) ; } else { Pos = Pos + Neg ; Neg = Pos - Neg ; Neg = Pos - Neg ; Neg += 1 ; if ( Pos != 0 ) { Pos += 1 ; } res = Math . max ( res , Pos ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 2 , - 3 , 0 , 1 } ; int N = arr . length ; System . out . print ( maxLenSub ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int maxLength ( String [ ] arr ) { int len = Integer . MIN_VALUE ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { int l = arr [ i ] . length ( ) ; if ( len < l ) { len = l ; } } return len ; } static void maxStrings ( String [ ] arr , int len ) { int N = arr . length ; Vector < String > ans = new Vector < String > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( len == arr [ i ] . length ( ) ) { ans . add ( arr [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } static void printStrings ( String [ ] arr ) { int max = maxLength ( arr ) ; maxStrings ( arr , max ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " aba " , " aa " , " ad " , " vcd " , " aba " } ; printStrings ( arr ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static List < List < Integer > > maxSubArray ( int [ ] arr , int K ) { List < List < Integer > > solution = new ArrayList < > ( ) ; int multi = 1 ; int start = 0 ; if ( arr . length <= 1 K < 0 ) { return new ArrayList < > ( ) ; } for ( int i = 0 ; i < arr . length ; i ++ ) { multi = multi * arr [ i ] ; while ( multi > K ) { multi = multi / arr [ start ] ; start ++ ; } List < Integer > list = new ArrayList < > ( ) ; for ( int j = i ; j >= start ; j -- ) { list . add ( 0 , arr [ j ] ) ; solution . add ( new ArrayList < > ( list ) ) ; } } return solution ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 1 , 4 } ; int K = 7 ; System . out . println ( maxSubArray ( arr , K ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static double solve ( int [ ] [ ] D , int N , int X , int Y ) { ArrayList < Double > T = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int x = D [ i ] [ 0 ] ; int y = D [ i ] [ 1 ] ; double speed = D [ i ] [ 2 ] ; double time = ( ( x * x - X * X ) + ( y * y - Y * Y ) ) / ( speed * speed ) ; T . add ( time ) ; } Collections . sort ( T ) ; int i = 0 ; int total = 0 ; while ( i < ( T . size ( ) - 1 ) ) { int count = 1 ; while ( ( i < ( T . size ( ) - 1 ) ) && ( Double . compare ( T . get ( i ) , T . get ( i + 1 ) ) == 0 ) ) { count += 1 ; i += 1 ; } total += ( count * ( count - 1 ) ) / 2 ; i += 1 ; } return total ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = 5 ; int [ ] [ ] D = { { 5 , 12 , 1 } , { 16 , 63 , 5 } , { - 10 , 24 , 2 } , { 7 , 24 , 2 } , { - 24 , 7 , 2 } } ; int X = 0 , Y = 0 ; System . out . println ( solve ( D , N , X , Y ) ) ; } }
import java . util . * ; class GFG { static int getMaxLength ( int arr [ ] , int N ) { int res = 2 ; int dist = 2 ; int curradj = ( arr [ 1 ] - arr [ 0 ] ) ; int prevadj = ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < N ; i ++ ) { curradj = arr [ i ] - arr [ i - 1 ] ; if ( curradj == prevadj ) { dist ++ ; } else { prevadj = curradj ; res = Math . max ( res , dist ) ; dist = 2 ; } } res = Math . max ( res , dist ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 7 , 4 , 6 , 8 , 10 , 11 } ; int N = arr . length ; System . out . print ( getMaxLength ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int minSteps ( String str , int N ) { int smaller , cost = 0 ; int f [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = str . charAt ( i ) - ' a ' ; smaller = 0 ; for ( int j = 0 ; j <= curr_ele ; j ++ ) { if ( f [ j ] != 0 ) smaller += f [ j ] ; } if ( smaller == 0 ) cost += ( i + 1 ) ; else cost += ( i - smaller + 1 ) ; f [ str . charAt ( i ) - ' a ' ] ++ ; } return cost ; } public static void main ( String [ ] args ) { String str = " abcab " ; int N = str . length ( ) ; System . out . println ( minSteps ( str , N ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static boolean possible ( int T , int arr [ ] , int n , int d ) { int partition = 1 ; int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total = total + arr [ i ] ; if ( total > T ) { partition = partition + 1 ; total = arr [ i ] ; if ( partition > d ) { return false ; } } } return true ; } public static void calcT ( int n , int d , int arr [ ] ) { int mx = - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , arr [ i ] ) ; sum = sum + arr [ i ] ; } int lb = mx ; int ub = sum ; while ( lb < ub ) { int T_mid = lb + ( ub - lb ) / 2 ; if ( possible ( T_mid , arr , n , d ) == true ) { ub = T_mid ; } else { lb = T_mid + 1 ; } } System . out . println ( lb ) ; } public static void main ( String args [ ] ) { int d = 2 ; int arr [ ] = { 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; calcT ( n , d , arr ) ; } }
import java . util . * ; class GFG { static int minReqSubstring ( String s , int n ) { char ongoing = ' N ' ; int count = 0 , l = s . length ( ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( ongoing == ' N ' ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' N ' ; } else { ongoing = ' I ' ; } } else if ( ongoing == ' I ' ) { if ( s . charAt ( i ) > s . charAt ( i - 1 ) ) { ongoing = ' I ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' I ' ; } else { ongoing = ' N ' ; count += 1 ; } } else { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ongoing = ' D ' ; } else { ongoing = ' N ' ; count += 1 ; } } } return count + 1 ; } public static void main ( String [ ] args ) { String S = " aeccdhba " ; int n = S . length ( ) ; System . out . print ( minReqSubstring ( S , n ) ) ; } }
import java . util . * ; class GFG { static int minimum_required_operations ( int arr [ ] , int n , int k ) { int answer = 0 ; long mod = 1000000007 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + arr [ i + 1 ] > k ) { if ( arr [ i ] > k ) { answer += ( arr [ i ] - k ) ; arr [ i ] = k ; } answer += ( arr [ i ] + arr [ i + 1 ] ) - k ; arr [ i + 1 ] = ( k - arr [ i ] ) ; answer %= mod ; } } return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 50 , 4 , 14 , 42 , 89 } ; int k = 10 ; int n = a . length ; System . out . print ( minimum_required_operations ( a , n , k ) ) ; } }
import java . util . * ; class GFG { static String findSmallestString ( String str1 , String str2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; Arrays . fill ( freq1 , 0 ) ; Arrays . fill ( freq2 , 0 ) ; int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { freq1 [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < n2 ; ++ i ) { freq2 [ str2 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; ++ i ) { freq1 [ i ] -= freq2 [ i ] ; } String res = " " ; int minIndex = str2 . charAt ( 0 ) - ' a ' ; for ( int i = 0 ; i < 26 ; ++ i ) { for ( int j = 0 ; j < freq1 [ i ] ; ++ j ) { res += ( char ) ( i + ' a ' ) ; } if ( i == minIndex ) { res += str2 ; } } return res ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeksfor " ; String str2 = " for " ; System . out . print ( findSmallestString ( str1 , str2 ) ) ; } }
import java . util . * ; class GFG { static void determineWinner ( String str ) { int [ ] A = new int [ 10 ] ; int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { A [ ( int ) str . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i <= 9 ; i ++ ) { if ( i % 2 != 0 ) { sum1 = sum1 + A [ i ] ; } else { sum2 = sum2 + A [ i ] ; } } if ( sum1 == sum2 ) { System . out . print ( " - 1" ) ; } else if ( sum1 > sum2 ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } public static void main ( String [ ] args ) { String str = "78787" ; determineWinner ( str ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void manipulateStrings ( String P , String Q ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < P . length ( ) ; i ++ ) { freq [ P . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < Q . length ( ) ; i ++ ) { freq [ Q . charAt ( i ) - ' a ' ] -- ; } StringBuilder sb = new StringBuilder ( ) ; int pos = Q . charAt ( 0 ) - ' a ' ; for ( int i = 0 ; i <= pos ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb . append ( c ) ; freq [ i ] -- ; } } sb . append ( Q ) ; for ( int i = pos + 1 ; i < 26 ; i ++ ) { while ( freq [ i ] > 0 ) { char c = ( char ) ( ' a ' + i ) ; sb . append ( c ) ; freq [ i ] -- ; } } System . out . println ( sb ) ; } public static void main ( String [ ] args ) { String P = " geeksforgeeksfor " ; String Q = " for " ; manipulateStrings ( P , Q ) ; } }
import java . util . * ; class GFG { static int numberMoves ( String s ) { int row [ ] = { 2 , 3 , 3 , 2 , 1 , 2 , 2 , 2 , 1 , 2 , 2 , 2 , 3 , 3 , 1 , 1 , 1 , 1 , 2 , 1 , 1 , 3 , 1 , 3 , 1 , 3 } ; int n = s . length ( ) ; int move = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( row [ s . charAt ( i ) - ' a ' ] != row [ s . charAt ( i - 1 ) - ' a ' ] ) { move ++ ; } } return move ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( numberMoves ( str ) ) ; } }
import java . util . * ; class GFG { static class Node { int L ; int R ; int K ; } ; static void kMultipleSum ( int arr [ ] , Node Query [ ] , int N , int Q ) { int prefixSum [ ] [ ] = new int [ N + 1 ] [ N ] ; for ( int i = 1 ; i <= N ; i ++ ) { prefixSum [ i ] [ 0 ] = arr [ 0 ] ; for ( int j = 0 ; j < N ; j ++ ) { if ( j % i == 0 ) { if ( j != 0 ) prefixSum [ i ] [ j ] = arr [ j ] + prefixSum [ i ] [ j - 1 ] ; } else { prefixSum [ i ] [ j ] = prefixSum [ i ] [ j - 1 ] ; } } } for ( int i = 0 ; i < Q ; i ++ ) { int last = prefixSum [ Query [ i ] . K ] [ Query [ i ] . R ] ; int first ; if ( Query [ i ] . L == 0 ) { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L ] ; } else { first = prefixSum [ Query [ i ] . K ] [ Query [ i ] . L - 1 ] ; } System . out . print ( last - first + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int Q = 2 ; Node Query [ ] = new Node [ Q ] ; for ( int i = 0 ; i < Q ; i ++ ) Query [ i ] = new Node ( ) ; Query [ 0 ] . L = 2 ; Query [ 0 ] . R = 5 ; Query [ 0 ] . K = 2 ; Query [ 1 ] . L = 3 ; Query [ 1 ] . R = 5 ; Query [ 1 ] . K = 5 ; kMultipleSum ( arr , Query , N , Q ) ; } }
import java . util . * ; import java . io . * ; public class Main { static int mod = 1000000007 ; public static long countS ( String str [ ] , int n , int m ) { Map < Integer , Set < Character > > counts = new HashMap < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { counts . put ( i , new HashSet < > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = str [ i ] ; for ( int j = 0 ; j < m ; j ++ ) { counts . get ( j ) . add ( s . charAt ( j ) ) ; } } long result = 1 ; for ( int index : counts . keySet ( ) ) result = ( result * counts . get ( index ) . size ( ) ) % mod ; return result ; } public static void main ( String [ ] args ) { String str [ ] = { "112" , "211" } ; int N = 2 , M = 3 ; System . out . println ( countS ( str , N , M ) ) ; } }
import java . util . * ; class GFG { static int countSubStrings ( String s , int n ) { int count = 0 , sum = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { sum += ( s . charAt ( i ) - '0' ) ; count += mp . containsKey ( sum - ( i + 1 ) ) == true ? mp . get ( sum - ( i + 1 ) ) : 0 ; if ( ! mp . containsKey ( sum - ( i + 1 ) ) ) mp . put ( sum - ( i + 1 ) , 1 ) ; else mp . put ( sum - ( i + 1 ) , mp . get ( sum - ( i + 1 ) ) + 1 ) ; } return count ; } public static void main ( String [ ] args ) { String str = "112112" ; int n = str . length ( ) ; System . out . print ( countSubStrings ( str , n ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static int findCount ( int arr [ ] , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int s = 1 , e = 1 ; int curr_subarray_sum = 0 , count = 0 ; while ( s < n - 1 && e < n - 1 ) { while ( e < n - 1 && curr_subarray_sum < prefix_sum [ s - 1 ] ) { curr_subarray_sum += arr [ e ++ ] ; } if ( curr_subarray_sum <= suffix_sum [ e ] ) { count ++ ; } curr_subarray_sum -= arr [ s ++ ] ; } return count ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 1 , 7 } ; int n = arr . length ; System . out . println ( findCount ( arr , n ) ) ; } }
class GFG { static int minOperation ( String s , int n ) { int cnt1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) cnt1 ++ ; } int cnt0 = 0 ; for ( int i = 0 ; i < cnt1 ; i ++ ) { if ( s . charAt ( i ) == '0' ) cnt0 ++ ; } return cnt0 ; } public static void main ( String [ ] args ) { int n = 8 ; String s = "01001011" ; int ans = minOperation ( s , n ) ; System . out . print ( ans + "NEW_LINE"); } }
import java . util . * ; class GFG { static int minOperation ( String s , int n ) { int ans = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( s . charAt ( i ) == '0' && s . charAt ( j ) == '1' ) { ans ++ ; i ++ ; j -- ; continue ; } if ( s . charAt ( i ) == '1' ) { i ++ ; } if ( s . charAt ( j ) == '0' ) { j -- ; } } return ans ; } public static void main ( String [ ] args ) { int n = 8 ; String s = "10100101" ; System . out . println ( minOperation ( s , n ) ) ; } }
import java . util . * ; class GFG { static int power ( int x , int y , int mod ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } public static void count ( int N ) { if ( N == 1 ) { System . out . println ( 10 ) ; return ; } System . out . println ( power ( 9 , N , 1000000007 ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; count ( N ) ; } }
import java . util . * ; class GFG { static int [ ] kmp ( String s ) { int n = s . length ( ) ; int [ ] lps = new int [ n ] ; lps [ 0 ] = 0 ; int i = 1 , len = 0 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } static String findString ( String s , int k ) { int n = s . length ( ) ; int [ ] lps = kmp ( s ) ; String ans = " " ; String suff = s . substring ( 0 , n - lps [ n - 1 ] ) ; for ( int i = 0 ; i < k - 1 ; ++ i ) { ans += suff ; } ans += s ; return ans ; } public static void main ( String [ ] args ) { int k = 3 ; String s = " geeksforgeeks " ; System . out . println ( findString ( s , k ) ) ; } }
class GFG { static boolean check ( int arr [ ] , int n ) { int i , g ; g = 0 ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 0 && g == 1 ) return false ; if ( arr [ i ] - arr [ i - 1 ] < 0 ) g = 1 ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 4 , 3 , 1 } ; int n = arr . length ; if ( check ( arr , n ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static boolean possible ( long mid , int [ ] a ) { long n = a . length ; long total = ( n * ( n - 1 ) ) / 2 ; long need = ( total + 1 ) / 2 ; long count = 0 ; long start = 0 , end = 1 ; while ( end < n ) { if ( a [ ( int ) end ] - a [ ( int ) start ] <= mid ) { end ++ ; } else { count += ( end - start - 1 ) ; start ++ ; } } if ( end == n && start < end && a [ ( int ) end - 1 ] - a [ ( int ) start ] <= mid ) { long t = end - start - 1 ; count += ( t * ( t + 1 ) / 2 ) ; } if ( count >= need ) return true ; else return false ; } static long findMedian ( int [ ] a ) { long n = a . length ; long low = 0 , high = a [ ( int ) n - 1 ] - a [ 0 ] ; while ( low <= high ) { long mid = ( low + high ) / 2 ; if ( possible ( mid , a ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 7 , 5 , 2 } ; Arrays . sort ( a ) ; System . out . println ( findMedian ( a ) ) ; } }
import java . util . * ; class GFG { public static void findMinSum ( int A [ ] , int K ) { int n = A . length ; int suffix_min [ ] = new int [ n ] ; suffix_min [ n - 1 ] = A [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , A [ i ] ) ; int min_sum = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + K < n ) min_sum = Math . min ( min_sum , A [ i ] + suffix_min [ i + K ] ) ; } System . out . println ( min_sum ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int K = 2 ; findMinSum ( A , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int maxEnvelopes ( int [ ] [ ] envelopes ) { int N = envelopes . length ; if ( N == 0 ) return N ; Arrays . sort ( envelopes , ( a , b ) -> ( a [ 0 ] != b [ 0 ] ) ? a [ 0 ] - b [ 0 ] : a [ 1 ] - b [ 1 ] ) ; int [ ] dp = new int [ N ] ; int max_envelope = 1 ; dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; ++ j ) { if ( envelopes [ i ] [ 0 ] > envelopes [ j ] [ 0 ] && envelopes [ i ] [ 1 ] > envelopes [ j ] [ 1 ] && dp [ i ] < dp [ j ] + 1 ) dp [ i ] = dp [ j ] + 1 ; } max_envelope = Math . max ( max_envelope , dp [ i ] ) ; } return max_envelope ; } public static void main ( String [ ] args ) { int [ ] [ ] envelopes = { { 4 , 3 } , { 5 , 3 } , { 5 , 6 } , { 1 , 2 } } ; System . out . println ( maxEnvelopes ( envelopes ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static String checkPossible ( int A [ ] , int B [ ] , int n ) { Set values = new HashSet ( ) ; for ( int i = 0 ; i < n ; i ++ ) { values . add ( B [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( values . contains ( A [ i ] + A [ j ] ) ) { values . remove ( A [ i ] + A [ j ] ) ; if ( values . size ( ) == 0 ) break ; } } } if ( values . size ( ) == 0 ) return " Yes " ; else return " No " ; } public static void main ( String args [ ] ) { int N = 5 ; int A [ ] = { 3 , 5 , 1 , 4 , 2 } ; int B [ ] = { 3 , 4 , 5 , 6 , 7 } ; System . out . print ( checkPossible ( A , B , N ) ) ; } }
class GFG { static int N = ( int ) 2e5 ; static int V = 500 ; static void solve ( int n , int d , int input [ ] ) { int [ ] a = new int [ N ] ; int [ ] cnt = new int [ V + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) a [ i ] = input [ i ] ; int answer = 0 ; for ( int i = 0 ; i < d ; ++ i ) cnt [ a [ i ] ] ++ ; for ( int i = d ; i <= n - 1 ; ++ i ) { int acc = 0 ; int low_median = - 1 , high_median = - 1 ; for ( int v = 0 ; v <= V ; ++ v ) { acc += cnt [ v ] ; if ( low_median == - 1 && acc >= ( int ) ( Math . floor ( ( d + 1 ) / 2.0 ) ) ) low_median = v ; if ( high_median == - 1 && acc >= ( int ) ( Math . ceil ( ( d + 1 ) / 2.0 ) ) ) high_median = v ; } int double_median = low_median + high_median ; if ( a [ i ] >= double_median ) answer ++ ; cnt [ a [ i - d ] ] -- ; cnt [ a [ i ] ] ++ ; } System . out . print ( answer + "NEW_LINE"); } public static void main ( String [ ] args ) { int input [ ] = { 1 , 2 , 2 , 4 , 5 } ; int n = input . length ; int k = 3 ; solve ( n , k , input ) ; } }
import java . util . * ; class GFG { static int findMinSoln ( int n , int k ) { int minSoln = Integer . MAX_VALUE ; for ( int i = 1 ; i < k ; i ++ ) { if ( n % i == 0 ) minSoln = Math . min ( minSoln , ( n / i ) * k + i ) ; } return minSoln ; } public static void main ( String [ ] args ) { int n = 4 , k = 6 ; System . out . println ( findMinSoln ( n , k ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int BFS ( int [ ] [ ] mat , int row , int col ) { int area = 0 ; Queue < pair > Q = new LinkedList < > ( ) ; Q . add ( new pair ( row , col ) ) ; while ( ! Q . isEmpty ( ) ) { pair it = Q . peek ( ) ; Q . poll ( ) ; int r = it . first , c = it . second ; if ( r < 0 c < 0 r > 4 c > 4 ) continue ; if ( mat [ r ] == 0 ) continue ; if ( mat [ r ] == 1 ) { mat [ r ] = 0 ; area ++ ; } Q . add ( new pair ( r + 1 , c ) ) ; Q . add ( new pair ( r - 1 , c ) ) ; Q . add ( new pair ( r , c + 1 ) ) ; Q . add ( new pair ( r , c - 1 ) ) ; } return area ; } static void sizeOfConnections ( int [ ] [ ] mat ) { ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int row = 0 ; row < 5 ; ++ row ) { for ( int col = 0 ; col < 5 ; ++ col ) { if ( mat [ row ] [ col ] == 1 ) { int area = BFS ( mat , row , col ) ; result . add ( area ) ; } } } for ( int val : result ) System . out . print ( val + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 , 1 } , { 1 , 0 , 0 , 1 , 1 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 1 , 1 , 1 } } ; sizeOfConnections ( mat ) ; } }
import java . util . * ; class GFG { static void maxProduct ( int arr [ ] , int N ) { if ( N < 3 ) { return ; } Arrays . sort ( arr ) ; int smallest1 = arr [ 0 ] ; int smallest3 = arr [ 2 ] ; int largest1 = arr [ N - 1 ] ; int largest3 = arr [ N - 3 ] ; if ( smallest1 * smallest3 >= largest1 * largest3 ) { System . out . print ( smallest1 + " ▁ " + smallest3 ) ; } else { System . out . print ( largest1 + " ▁ " + largest3 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 67 , 45 , 160 , 78 } ; int N = arr . length ; maxProduct ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxLen ( int A [ ] , int N ) { List < List < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 1 ) { int s = i , len ; while ( i < N && A [ i ] == 1 ) { i ++ ; } len = i - s ; v . add ( Arrays . asList ( len , s , i - 1 ) ) ; } } if ( v . size ( ) == 0 ) { return - 1 ; } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) ) ; if ( v . get ( i + 1 ) . get ( 1 ) - v . get ( i ) . get ( 2 ) - 1 == 2 ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) + v . get ( i + 1 ) . get ( 0 ) ) ; } if ( v . get ( i + 1 ) . get ( 1 ) - v . get ( i ) . get ( 2 ) - 1 == 1 ) { ans = Math . max ( ans , v . get ( i ) . get ( 0 ) + v . get ( i + 1 ) . get ( 0 ) - 1 ) ; } } ans = Math . max ( v . get ( v . size ( ) - 1 ) . get ( 0 ) , ans ) ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 1 , 0 , 0 , 1 } ; int N = arr . length ; System . out . println ( maxLen ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int t , n , m , a ; static Stack < Integer > s ; static ArrayList < Integer > tsort ; static ArrayList < ArrayList < Integer > > adj ; static int [ ] visited = new int [ ( int ) 1e5 + 1 ] ; static void dfs ( int u ) { visited [ u ] = 1 ; for ( Integer it : adj . get ( u ) ) { if ( visited [ it ] == 0 ) dfs ( it ) ; } s . push ( u ) ; } static boolean check_cycle ( ) { Map < Integer , Integer > pos = new HashMap < > ( ) ; int ind = 0 ; while ( ! s . isEmpty ( ) ) { pos . put ( s . peek ( ) , ind ) ; tsort . add ( s . peek ( ) ) ; ind += 1 ; s . pop ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( Integer it : adj . get ( i ) ) { if ( pos . get ( i ) > pos . get ( it ) ) { return true ; } } } return false ; } static void addEdge ( int u , int v ) { adj . get ( u ) . add ( v ) ; } public static void main ( String [ ] args ) { n = 4 ; m = 5 ; s = new Stack < > ( ) ; adj = new ArrayList < > ( ) ; tsort = new ArrayList < > ( ) ; for ( int i = 0 ; i < 4 ; i ++ ) adj . add ( new ArrayList < > ( ) ) ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 2 ) ; addEdge ( 2 , 0 ) ; addEdge ( 2 , 3 ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == 0 ) { dfs ( i ) ; } } if ( check_cycle ( ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void printNewArray ( List < Integer > a , int n ) { List < pair > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { v . add ( new pair ( a . get ( i ) , i ) ) ; } Collections . sort ( v , ( pair s1 , pair s2 ) -> { return s1 . first - s2 . first ; } ) ; int ans [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { ans [ v . get ( ( i + 1 ) % n ) . second ] = v . get ( i ) . first ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { List < Integer > a = Arrays . asList ( 4 , 1 , 2 , 5 , 3 ) ; int n = a . size ( ) ; printNewArray ( a , n ) ; } }
import java . util . * ; class GFG { static int min_remove ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( arr [ j ] - arr [ i ] <= k ) { ans = Math . min ( ans , n - j + i - 1 ) ; } } } return ans ; } public static void main ( String [ ] args ) { int k = 3 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . print ( min_remove ( arr , n , k ) ) ; } }
class GFG { static void solve ( int arr [ ] , int n ) { int countzeroes = 0 ; int countones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countzeroes ++ ; else countones ++ ; } if ( Math . abs ( countzeroes - countones ) == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 1 , 1 } ; int n = arr . length ; solve ( arr , n ) ; } }
import java . util . * ; class GFG { static char bs ( char ar [ ] , int n , int ele ) { int start = 0 ; int end = n - 1 ; char ch = ' @ ' ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( ar [ mid ] == ele ) end = mid - 1 ; else if ( ar [ mid ] < ele ) { ch = ar [ mid ] ; start = mid + 1 ; } else end = mid - 1 ; } return ch ; } public static void main ( String [ ] args ) { char ar [ ] = { ' e ' , ' g ' , ' t ' , ' y ' } ; int n = ar . length ; char K = ' u ' ; char ch = bs ( ar , n , K ) ; if ( ch == ' @ ' ) System . out . print ( " - 1" ) ; else System . out . print ( ch ) ; } }
import java . util . * ; class GFG { static int findMaxLen ( int [ ] a , int k ) { int n = a . length ; Arrays . sort ( a ) ; boolean [ ] vis = new boolean [ n ] ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , i ) ; } int c = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( vis [ i ] == false ) { int check = a [ i ] * k ; if ( mp . containsKey ( check ) ) { c ++ ; vis [ mp . get ( check ) ] = true ; } } } return n - c ; } public static void main ( String [ ] args ) { int K = 3 ; int [ ] arr = { 1 , 4 , 3 , 2 } ; System . out . print ( findMaxLen ( arr , K ) ) ; } }
import java . util . * ; public class Main { public static int getPrefixLength ( String srcStr , String targetStr ) { if ( targetStr . length ( ) == 0 ) return 0 ; srcStr = srcStr . toLowerCase ( ) ; targetStr = targetStr . toLowerCase ( ) ; HashMap < Character , Integer > dictCount = new HashMap < > ( ) ; int nUnique = 0 ; for ( char ch : targetStr . toCharArray ( ) ) { if ( dictCount . containsKey ( ch ) != true ) { nUnique += 1 ; dictCount . put ( ch , 0 ) ; } dictCount . replace ( ch , dictCount . get ( ch ) + 1 ) ; } for ( int i = 0 ; i < srcStr . length ( ) ; i ++ ) { char ch = srcStr . charAt ( i ) ; if ( dictCount . containsKey ( ch ) != true ) continue ; dictCount . replace ( ch , dictCount . get ( ch ) - 1 ) ; if ( dictCount . get ( ch ) == 0 ) nUnique -= 1 ; if ( nUnique == 0 ) return ( i + 1 ) ; } return - 1 ; } public static void main ( String [ ] args ) { String S = " MarvoloGaunt " ; String T = " Tom " ; System . out . println ( getPrefixLength ( S , T ) ) ; } }
import java . util . * ; class GFG { static boolean check ( char [ ] str , int mid , char a ) { int n = str . length ; int previous = - 1 , i ; for ( i = 0 ; i < n ; ++ i ) { if ( str [ i ] == a ) { if ( i - previous > mid ) { return false ; } previous = i ; } } if ( i - previous > mid ) return false ; else return true ; } static boolean possible ( char [ ] str , int mid ) { for ( int i = 0 ; i < 26 ; ++ i ) { if ( check ( str , mid , ( char ) ( i + ' a ' ) ) ) return true ; } return false ; } static int findMinLength ( char [ ] str ) { int low = 1 , high = str . length ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( possible ( str , mid ) ) high = mid - 1 ; else low = mid + 1 ; } return high + 1 ; } static boolean ifAllDistinct ( String str ) { HashSet < Character > s = new HashSet < Character > ( ) ; for ( char c : str . toCharArray ( ) ) { s . add ( c ) ; } return s . size ( ) == str . length ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( ifAllDistinct ( str ) ) System . out . print ( - 1 + "NEW_LINE"); else System . out . print ( findMinLength ( str . toCharArray ( ) ) ) ; } }
class GFG { static int solve ( int A [ ] , int N ) { int i = 0 ; int k = 0 ; for ( int j = 1 ; j < N ; j ++ ) { int tempK = Math . min ( A [ i ] , A [ j ] ) / ( j - i ) ; if ( tempK > k ) { k = tempK ; } if ( A [ j ] >= A [ i ] / ( j - i ) ) i = j ; } return k ; } public static void main ( String [ ] args ) { int A [ ] = { 10 , 5 , 12 , 15 , 8 } ; int N = A . length ; System . out . println ( solve ( A , N ) ) ; } }
import java . util . * ; class GFG { static void split ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { count = 2 ; break ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; split ( arr , n ) ; } }
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 " ) ; } }
import java . util . * ; class GFG { static void FindPairs ( int n , int k ) { System . out . print ( 1 + " ▁ " + n + "NEW_LINE"); k -- ; for ( long i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { System . out . print ( 1 + " ▁ " + i + "NEW_LINE"); k -- ; if ( k == 0 ) break ; if ( i != n / i ) { System . out . print ( 1 + " ▁ " + n / i + "NEW_LINE"); k -- ; } if ( k == 0 ) break ; } } } public static void main ( String [ ] args ) { int N = 100 ; int K = 5 ; FindPairs ( N , K ) ; } }
import java . util . * ; class GFG { static void dfs ( int u , Vector < Integer > adj [ ] , int [ ] vis , int [ ] inDeg , int [ ] outDeg ) { vis [ u ] = 1 ; for ( int v : adj [ u ] ) { outDeg [ u ] = 1 ; inDeg [ v ] = 1 ; if ( vis [ v ] == 0 ) { dfs ( v , adj , vis , inDeg , outDeg ) ; } } } static int findMinimumEdges ( int source [ ] , int N , int M , int dest [ ] ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] adj = new Vector [ N + 1 ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { adj ] . add ( dest [ i ] ) ; } int inDeg [ ] = new int [ N + 1 ] ; int outDeg [ ] = new int [ N + 1 ] ; int vis [ ] = new int [ N + 1 ] ; dfs ( 1 , adj , vis , inDeg , outDeg ) ; int minEdges = 0 ; int totalIndegree = 0 ; int totalOutdegree = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( inDeg [ i ] == 1 ) totalIndegree ++ ; if ( outDeg [ i ] == 1 ) totalOutdegree ++ ; } minEdges = Math . max ( N - totalIndegree , N - totalOutdegree ) ; return minEdges ; } public static void main ( String [ ] args ) { int N = 5 , M = 5 ; int source [ ] = { 1 , 3 , 1 , 3 , 4 } ; int destination [ ] = { 2 , 2 , 3 , 4 , 5 } ; System . out . print ( findMinimumEdges ( source , N , M , destination ) ) ; } }
class GFG { static int PermuteTheArray ( int A [ ] , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ A [ i ] - 1 ] = i ; } int mini = n , maxi = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mini = Math . min ( mini , arr [ i ] ) ; maxi = Math . max ( maxi , arr [ i ] ) ; if ( maxi - mini == i ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 5 , 1 , 3 , 2 , 6 } ; System . out . print ( PermuteTheArray ( A , 6 ) ) ; } }
import java . util . * ; class GFG { static boolean isSubsetSum ( int arr [ ] , int n , int sum ) { boolean [ ] [ ] subset = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= arr [ i - 1 ] ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - arr [ i - 1 ] ] ; } } return subset [ n ] [ sum ] ; } static int findSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int val = ( int ) Math . sqrt ( sum ) ; for ( int i = val ; i >= 0 ; i -- ) { if ( isSubsetSum ( arr , n , i * i ) ) { return i * i ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findsubArray ( int arr [ ] , int N , int K ) { int res = N + 1 ; for ( int i = 0 ; i < N ; i ++ ) { int curr_prod = 1 ; for ( int j = i ; j < N ; j ++ ) { curr_prod = curr_prod * arr [ j ] ; if ( curr_prod % N == K && res > ( j - i + 1 ) ) { res = Math . min ( res , j - i + 1 ) ; break ; } } } return ( res == N + 1 ) ? 0 : res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 } ; int N = arr . length ; int K = 1 ; int answer = findsubArray ( arr , N , K ) ; if ( answer != 0 ) System . out . println ( answer ) ; else System . out . println ( " - 1" ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int getMaxGcd ( int N ) { int maxGcd = Integer . MIN_VALUE , A , B ; for ( int i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { A = i ; B = N / i ; maxGcd = Math . max ( maxGcd , gcd ( A , B ) ) ; } } return maxGcd ; } public static void main ( String s [ ] ) { int N = 18 ; System . out . println ( getMaxGcd ( N ) ) ; } }
import java . util . * ; class GFG { static boolean can_place ( int A [ ] , int n , int B , int mid ) { int count = 1 ; int last_position = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] - last_position >= mid ) { last_position = A [ i ] ; count ++ ; if ( count == B ) { return true ; } } } return false ; } static int find_min_difference ( int A [ ] , int n , int B ) { Arrays . sort ( A ) ; int s = 0 ; int e = A [ n - 1 ] - A [ 0 ] ; int ans = 0 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( can_place ( A , n , B , mid ) ) { ans = mid ; s = mid + 1 ; } else { e = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 5 } ; int n = A . length ; int B = 3 ; int min_difference = find_min_difference ( A , n , B ) ; System . out . print ( min_difference ) ; } }
import java . util . * ; class GFG { static void UniversalSubset ( List < String > A , List < String > B ) { int n1 = A . size ( ) ; int n2 = B . size ( ) ; List < String > res = new ArrayList < > ( ) ; int [ ] [ ] A_fre = new int [ n1 ] [ 26 ] ; for ( int i = 0 ; i < n1 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) A_fre [ i ] [ j ] = 0 ; } for ( int i = 0 ; i < n1 ; i ++ ) { for ( int j = 0 ; j < A . get ( i ) . length ( ) ; j ++ ) { A_fre [ i ] [ A . get ( i ) . charAt ( j ) - ' a ' ] ++ ; } } int [ ] B_fre = new int [ 26 ] ; for ( int i = 0 ; i < n2 ; i ++ ) { int [ ] arr = new int [ 26 ] ; for ( int j = 0 ; j < B . get ( i ) . length ( ) ; j ++ ) { arr [ B . get ( i ) . charAt ( j ) - ' a ' ] ++ ; B_fre [ B . get ( i ) . charAt ( j ) - ' a ' ] = Math . max ( B_fre [ B . get ( i ) . charAt ( j ) - ' a ' ] , arr [ B . get ( i ) . charAt ( j ) - ' a ' ] ) ; } } for ( int i = 0 ; i < n1 ; i ++ ) { int flag = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( A_fre [ i ] [ j ] < B_fre [ j ] ) { flag = 1 ; break ; } } if ( flag == 0 ) res . add ( A . get ( i ) ) ; } if ( res . size ( ) != 0 ) { for ( int i = 0 ; i < res . size ( ) ; i ++ ) { for ( int j = 0 ; j < res . get ( i ) . length ( ) ; j ++ ) System . out . print ( res . get ( i ) . charAt ( j ) ) ; } System . out . print ( " ▁ " ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { List < String > A = Arrays . asList ( " geeksforgeeks " , " topcoder " , " leetcode " ) ; List < String > B = Arrays . asList ( " geek " , " ee " ) ; UniversalSubset ( A , B ) ; } }
import java . util . * ; class GFG { static int countTriplets ( int A [ ] , int N ) { int ans = 0 ; HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int j = N - 2 ; j >= 1 ; j -- ) { if ( map . containsKey ( A [ j + 1 ] ) ) map . put ( A [ j + 1 ] , map . get ( A [ j + 1 ] ) + 1 ) ; else map . put ( A [ j + 1 ] , 1 ) ; for ( int i = 0 ; i < j ; i ++ ) { int target = A [ i ] * A [ j ] ; if ( map . containsKey ( target ) ) ans += map . get ( target ) ; } } return ans ; } public static void main ( String [ ] args ) { int N = 5 ; int A [ ] = { 2 , 3 , 4 , 6 , 12 } ; System . out . print ( countTriplets ( A , N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void MissingPoint ( Vector < pair > V , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < V . size ( ) ; i ++ ) { if ( mp . containsKey ( V . get ( i ) . first ) ) mp . put ( V . get ( i ) . first , V . get ( i ) . first + 1 ) ; else mp . put ( V . get ( i ) . first , 1 ) ; } int x = 0 , y = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) { x = it . getKey ( ) ; break ; } } mp . clear ( ) ; for ( int i = 0 ; i < V . size ( ) ; i ++ ) { if ( mp . containsKey ( V . get ( i ) . second ) ) mp . put ( V . get ( i ) . second , V . get ( i ) . second + 1 ) ; else mp . put ( V . get ( i ) . second , 1 ) ; } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 1 ) { y = it . getKey ( ) ; break ; } } System . out . print ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int N = 2 ; Vector < pair > V = new Vector < pair > ( ) ; V . add ( new pair ( 1 , 1 ) ) ; V . add ( new pair ( 1 , 2 ) ) ; V . add ( new pair ( 4 , 6 ) ) ; V . add ( new pair ( 2 , 1 ) ) ; V . add ( new pair ( 9 , 6 ) ) ; V . add ( new pair ( 9 , 3 ) ) ; V . add ( new pair ( 4 , 3 ) ) ; MissingPoint ( V , N ) ; } }
import java . util . * ; class GFG { static int sign ( int x ) { if ( x > 0 ) return 1 ; else return - 1 ; } static int findMaxSum ( int arr [ ] , int size ) { int max_sum = 0 , pres , i , j ; for ( i = 0 ; i < size ; i ++ ) { pres = arr [ i ] ; j = i ; while ( j < size && sign ( arr [ i ] ) == sign ( arr [ j ] ) ) { pres = Math . max ( pres , arr [ j ] ) ; j ++ ; } max_sum = max_sum + pres ; i = j - 1 ; } return max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 8 , 3 , 8 , - 4 , - 15 , 5 , - 2 , - 3 , 1 } ; int size = arr . length ; System . out . println ( findMaxSum ( arr , size ) ) ; } }
import java . util . * ; class GFG { static boolean groupInKConsecutive ( int [ ] arr , int K ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int h : arr ) { if ( count . containsKey ( h ) ) count . put ( h , count . get ( h ) + 1 ) ; else count . put ( h , 1 ) ; } for ( Map . Entry < Integer , Integer > c : count . entrySet ( ) ) { int cur = c . getKey ( ) ; int n = c . getValue ( ) ; if ( n > 0 ) { for ( int i = 1 ; i < K ; ++ i ) { if ( ! count . containsKey ( cur + i ) ) { return false ; } count . put ( cur + i , count . get ( cur + i ) - n ) ; if ( count . get ( cur + i ) < 0 ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 6 , 2 , 3 , 4 , 7 , 8 } ; int k = 3 ; if ( groupInKConsecutive ( arr , k ) ) { System . out . print ( " True " ) ; } else { System . out . print ( " False " ) ; } } }
import java . util . * ; class GFG { static int countSubAllOnes ( String s ) { int l = 0 , r = 0 , ans = 0 ; while ( l <= r ) { if ( r == s . length ( ) ) { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; break ; } if ( s . charAt ( r ) == '1' ) r ++ ; else { ans += ( ( r - l ) * ( r - l + 1 ) ) / 2 ; l = r + 1 ; r ++ ; } } return ans ; } static String flip ( char [ ] s ) { for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == '1' ) s [ i ] = '0' ; else s [ i ] = '1' ; } return String . valueOf ( s ) ; } static int countSubAllZerosOnes ( String s ) { int only_1s = countSubAllOnes ( s ) ; s = flip ( s . toCharArray ( ) ) ; int only_0s = countSubAllOnes ( s ) ; return only_0s + only_1s ; } public static void main ( String [ ] args ) { String s = "011" ; System . out . print ( countSubAllZerosOnes ( s ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static boolean sumSquare ( int p ) { return ( p - 1 ) % 4 == 0 ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countOfPrimes ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime ( i ) ) { if ( sumSquare ( i ) ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int L = 5 , R = 41 ; System . out . println ( countOfPrimes ( L , R ) ) ; } }
import java . util . * ; class GFG { public static void findPair ( int a [ ] , int n ) { int min_dist = Integer . MAX_VALUE ; int index_a = - 1 , index_b = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( j - i < min_dist ) { if ( a [ i ] % a [ j ] == 0 a [ j ] % a [ i ] == 0 ) { min_dist = j - i ; index_a = i ; index_b = j ; } } } } if ( index_a == - 1 ) { System . out . println ( " - 1" ) ; } else { System . out . print ( " ( " + a [ index_a ] + " , ▁ " + a [ index_b ] + " ) " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; findPair ( a , n ) ; } }
import java . util . * ; class GFG { static void subString ( char str [ ] , int n ) { int c = 0 ; for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; char temp = str [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( k < n && temp < str [ k ] ) { temp = str [ k ] ; k ++ ; f = 2 ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k < n && temp > str [ k ] && f != 2 ) { k ++ ; f = 0 ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } System . out . print ( c + "NEW_LINE"); } public static void main ( String [ ] args ) { char str [ ] = " bade " . toCharArray ( ) ; subString ( str , str . length ) ; } }
import java . util . Scanner ; public class GreeksForGreeksQuestions { static int findNumberDivByPowerofTwo ( int [ ] ar , int k , int n ) { int found = - 1 , m = k ; while ( m > 0 ) { int l = 0 ; int r = n - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( ar [ mid ] == m ) { found = m ; break ; } else if ( ar [ mid ] > m ) { r = mid - 1 ; } else if ( ar [ mid ] < m ) { l = mid + 1 ; } } if ( found != - 1 ) { break ; } m = m / 2 ; } return found ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 8 , 10 } ; int k = 4 , n = 5 ; System . out . println ( findNumberDivByPowerofTwo ( arr , k , n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void setElement ( int [ ] arr , int n , int index , int x ) { arr [ index ] = x ; } static int getCount ( int [ ] arr , int n , int l , int r ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l && arr [ i ] <= r ) { count ++ ; } } return count ; } static void SolveQuery ( int arr [ ] , int n , ArrayList < List < Integer > > Q ) { int x ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { if ( Q . get ( i ) . get ( 0 ) == 1 ) { x = getCount ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; System . out . print ( x + " ▁ " ) ; } else { setElement ( arr , n , Q . get ( i ) . get ( 1 ) , Q . get ( i ) . get ( 2 ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 6 } ; int n = arr . length ; ArrayList < List < Integer > > Q = new ArrayList < > ( ) ; Q . add ( Arrays . asList ( 1 , 3 , 5 ) ) ; Q . add ( Arrays . asList ( 1 , 2 , 4 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; Q . add ( Arrays . asList ( 2 , 1 , 7 ) ) ; Q . add ( Arrays . asList ( 1 , 1 , 2 ) ) ; SolveQuery ( arr , n , Q ) ; } }
import java . util . * ; class GFG { public static void getMinimumDistance ( int a [ ] , int n ) { Set < Integer > min_set = new HashSet < > ( ) ; Set < Integer > max_set = new HashSet < > ( ) ; int max = 0 , min = Integer . MAX_VALUE ; HashMap < Integer , Integer > frequency = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { frequency . put ( a [ i ] , frequency . getOrDefault ( a [ i ] , 0 ) + 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { int count = frequency . get ( a [ i ] ) ; if ( count == max ) { max_set . add ( a [ i ] ) ; } else if ( count > max ) { max_set . clear ( ) ; max = count ; max_set . add ( a [ i ] ) ; } if ( count == min ) { min_set . add ( a [ i ] ) ; } else if ( count < min ) { min_set . clear ( ) ; min = count ; min_set . add ( a [ i ] ) ; } } int min_dist = Integer . MAX_VALUE ; int last_min_found = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( min_set . contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . min ( min_dist , i - last_min_found ) ; } } last_min_found = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( min_set . contains ( a [ i ] ) ) last_min_found = i ; if ( max_set . contains ( a [ i ] ) && last_min_found != - 1 ) { min_dist = Math . min ( min_dist , last_min_found - i ) ; } } System . out . println ( min_dist ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 2 , 3 , 3 } ; int N = arr . length ; getMinimumDistance ( arr , N ) ; } }
import java . util . * ; class GFG { static void printNum ( int L , int R ) { for ( int i = L ; i <= R ; i ++ ) { int temp = i ; int c = 10 ; int flag = 0 ; while ( temp > 0 ) { if ( temp % 10 >= c ) { flag = 1 ; break ; } c = temp % 10 ; temp /= 10 ; } if ( flag == 0 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int L = 10 , R = 15 ; printNum ( L , R ) ; } }
import java . util . * ; class GFG { static void printSubsequenceString ( String str , int k ) { int n = str . length ( ) ; int i ; Vector < Integer > factors = new Vector < Integer > ( ) ; for ( i = 2 ; i <= Math . sqrt ( k ) ; i ++ ) { while ( k % i == 0 ) { factors . add ( i ) ; k /= i ; } } if ( k > 1 ) factors . add ( k ) ; int [ ] count = new int [ n ] ; Arrays . fill ( count , 1 ) ; int index = 0 ; while ( factors . size ( ) > 0 ) { count [ index ++ ] *= factors . get ( factors . size ( ) - 1 ) ; factors . remove ( factors . get ( factors . size ( ) - 1 ) ) ; if ( index == n ) index = 0 ; } String s = " " ; for ( i = 0 ; i < n ; i ++ ) { while ( count [ i ] -- > 0 ) { s += str . charAt ( i ) ; } } System . out . print ( s ) ; } public static void main ( String [ ] args ) { String str = " code " ; int k = 20 ; printSubsequenceString ( str , k ) ; } }
import java . util . * ; class GFG { static void changeArr ( int [ ] input ) { int newArray [ ] = Arrays . copyOfRange ( input , 0 , input . length ) ; Arrays . sort ( newArray ) ; int i ; Map < Integer , Integer > ranks = new HashMap < > ( ) ; int rank = 1 ; for ( int index = 0 ; index < newArray . length ; index ++ ) { int element = newArray [ index ] ; if ( ranks . get ( element ) == null ) { ranks . put ( element , rank ) ; rank ++ ; } } for ( int index = 0 ; index < input . length ; index ++ ) { int element = input [ index ] ; input [ index ] = ranks . get ( input [ index ] ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 100 , 2 , 70 , 2 } ; changeArr ( arr ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
import java . util . * ; class GFG { public static void findPair ( int [ ] arr , int n ) { int i , j ; if ( n < 2 ) { System . out . println ( " - 1" ) ; } HashSet < Integer > hashMap = new HashSet < Integer > ( ) ; for ( Integer k : arr ) { hashMap . add ( k ) ; } for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( ! hashMap . contains ( sum ) ) { System . out . println ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; } } } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 2 , 6 } ; int n = arr . length ; findPair ( arr , n ) ; } }
import java . util . * ; class GFG { static int findLengthOfMaxSubset ( int arr [ ] , int n , int m ) { Set < Integer > s = new HashSet < > ( ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = arr [ i ] ; if ( tmp != m ) { s . add ( tmp ) ; answer ++ ; } } int min = 1 ; while ( s . contains ( min ) ) { min ++ ; } if ( min != m ) { answer = - 1 ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 } ; int N = arr . length ; int M = 3 ; System . out . print ( findLengthOfMaxSubset ( arr , N , M ) ) ; } }
import java . util . * ; class GFG { static final int m = 5 ; static int countSubsquare ( int arr [ ] [ ] , int n , int X ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i + 1 ] [ j + 1 ] = arr [ i ] [ j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { int lo = 1 ; int hi = Math . min ( n - i , m - j ) + 1 ; boolean found = false ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int ni = i + mid - 1 ; int nj = j + mid - 1 ; int sum = dp [ ni ] [ nj ] - dp [ ni ] [ j - 1 ] - dp [ i - 1 ] [ nj ] + dp [ i - 1 ] [ j - 1 ] ; if ( sum >= X ) { if ( sum == X ) { found = true ; } hi = mid - 1 ; } else { lo = mid + 1 ; } } if ( found == true ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int N = 4 , X = 10 ; int arr [ ] [ ] = { { 2 , 4 , 3 , 2 , 10 } , { 3 , 1 , 1 , 1 , 5 } , { 1 , 1 , 2 , 1 , 4 } , { 2 , 1 , 1 , 1 , 3 } } ; System . out . print ( countSubsquare ( arr , N , X ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int countSub ( String str ) { int n = ( int ) str . length ( ) ; int ans = 0 ; int [ ] cnt = new int [ 26 ] ; int i = 0 , j = 0 ; while ( i < n ) { if ( j < n && ( cnt [ str . charAt ( j ) - ' a ' ] == 0 ) ) { cnt [ str . charAt ( j ) - ' a ' ] ++ ; ans += ( j - i + 1 ) ; j ++ ; } else { cnt [ str . charAt ( i ) - ' a ' ] -- ; i ++ ; } } return ans ; } public static void main ( String [ ] args ) { String str = " gffg " ; System . out . print ( countSub ( str ) ) ; } }
import java . util . * ; class GFG { static int solve ( int n , int k ) { int ans = 0 ; for ( int j = 1 ; j * j <= n ; j ++ ) { if ( n % j == 0 ) { if ( j <= k ) { ans = Math . max ( ans , j ) ; } if ( n / j <= k ) { ans = Math . max ( ans , n / j ) ; } } } return ans ; } public static void main ( String [ ] args ) { int N = 8 , K = 7 ; System . out . print ( ( N / solve ( N , K ) ) ) ; } }
import java . util . * ; class GFG { static void max_sum_subarray ( List < Integer > arr , int L , int R ) { int n = arr . size ( ) ; int [ ] pre = new int [ n ] ; pre [ 0 ] = arr . get ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { pre [ i ] = pre [ i - 1 ] + arr . get ( i ) ; } List < Integer > s1 = new ArrayList < > ( ) ; s1 . add ( 0 ) ; int ans = Integer . MIN_VALUE ; ans = Math . max ( ans , pre [ L - 1 ] ) ; int flag = 0 ; for ( int i = L ; i < n ; i ++ ) { if ( i - R >= 0 ) { if ( flag == 0 ) { int it = s1 . indexOf ( 0 ) ; s1 . remove ( it ) ; flag = 1 ; } } if ( i - L >= 0 ) s1 . add ( pre [ i - L ] ) ; ans = Math . max ( ans , pre [ i ] - s1 . get ( 0 ) ) ; if ( i - R >= 0 ) { int it = s1 . indexOf ( pre [ i - R ] ) ; s1 . remove ( it ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int L , R ; L = 1 ; R = 3 ; List < Integer > arr = Arrays . asList ( 1 , 2 , 2 , 1 ) ; max_sum_subarray ( arr , L , R ) ; } }
import java . util . * ; class GFG { static int rowWise ( char [ ] [ ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < m ; j ++ ) { if ( v [ i ] [ j ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } static int colWise ( char [ ] [ ] v , int n , int m , int k ) { int ans = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int countcons = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] [ i ] == '1' ) { countcons = 0 ; } else { countcons ++ ; } if ( countcons >= k ) { ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int n = 3 , m = 3 , k = 1 ; char v [ ] [ ] = { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; if ( k == 1 ) { System . out . print ( rowWise ( v , n , m , k ) ) ; } else { System . out . print ( colWise ( v , n , m , k ) + rowWise ( v , n , m , k ) ) ; } } }
import java . io . * ; class GFG { static boolean solve ( int a [ ] , int n ) { int mx = Integer . MIN_VALUE ; for ( int j = 1 ; j < n ; j ++ ) { if ( mx > a [ j ] ) return false ; mx = Math . max ( mx , a [ j - 1 ] ) ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 2 } ; int n = a . length ; boolean possible = solve ( a , n ) ; if ( possible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int [ ] spf = new int [ 100009 ] ; static void sieve ( ) { for ( int i = 0 ; i < 100005 ; i ++ ) spf [ i ] = - 1 ; for ( int i = 2 ; i * i <= 100005 ; i ++ ) { for ( int j = i ; j <= 100005 ; j += i ) { if ( spf [ j ] == - 1 ) { spf [ j ] = i ; } } } } static HashSet < Integer > findPrimeFactors ( HashSet < Integer > s , int n ) { while ( n > 1 ) { s . add ( spf [ n ] ) ; n /= spf [ n ] ; } return s ; } static int MinimumSteps ( int n , int m ) { Queue < pair > q = new LinkedList < > ( ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; q . add ( new pair ( n , 0 ) ) ; while ( ! q . isEmpty ( ) ) { int newNum = q . peek ( ) . first ; int distance = q . peek ( ) . second ; q . remove ( ) ; HashSet < Integer > k = findPrimeFactors ( s , newNum ) ; for ( int i : k ) { if ( newNum == m ) { return distance ; } else if ( newNum > m ) { break ; } else { q . add ( new pair ( newNum + i , distance + 1 ) ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 7 , M = 16 ; sieve ( ) ; System . out . print ( MinimumSteps ( N , M ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > [ ] v = new Vector [ 100000 ] ; static void div ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v [ n ] . add ( i ) ; } } } static int prodof2elements ( int arr [ ] , int n ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; } Arrays . sort ( arr2 ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v [ arr [ i ] ] . size ( ) == 0 ) div ( arr [ i ] ) ; for ( int j : v [ arr [ i ] ] ) { if ( Arrays . binarySearch ( arr2 , j ) >= 0 && Arrays . binarySearch ( arr2 , ( int ) arr [ i ] / j ) >= 0 ) { ans ++ ; break ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 8 , 4 , 32 , 18 } ; int N = arr . length ; for ( int i = 0 ; i < v . length ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; System . out . print ( prodof2elements ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int max_subarray_size ( int N , int [ ] arr ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { HashMap < Integer , Integer > map1 = new HashMap < > ( ) ; HashMap < Integer , Integer > map2 = new HashMap < > ( ) ; for ( int j = i ; j < N ; j ++ ) { int ele_count ; if ( ! map1 . containsKey ( arr [ j ] ) ) { ele_count = 0 ; } else { ele_count = map1 . get ( arr [ j ] ) ; } if ( map1 . containsKey ( arr [ j ] ) ) { map1 . put ( arr [ j ] , map1 . get ( arr [ j ] ) + 1 ) ; } else { map1 . put ( arr [ j ] , 1 ) ; } if ( map2 . containsKey ( ele_count ) ) { if ( map2 . get ( ele_count ) == 1 ) { map2 . remove ( ele_count ) ; } else { map2 . put ( ele_count , map2 . get ( ele_count ) - 1 ) ; } } if ( map2 . containsKey ( ele_count + 1 ) ) { map2 . put ( ele_count + 1 , map2 . get ( ele_count + 1 ) + 1 ) ; } else { map2 . put ( ele_count + 1 , 1 ) ; } if ( map2 . size ( ) == 1 ) ans = Math . max ( ans , j - i + 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 5 , 6 , 5 , 6 } ; int N = arr . length ; System . out . println ( max_subarray_size ( N , arr ) ) ; } }
import java . util . * ; class GFG { static boolean vowel ( int vo ) { if ( vo == 0 vo == 4 vo == 8 vo == 14 vo == 20 ) return true ; else return false ; } static boolean check ( String s , int k ) { int [ ] cnt = new int [ 26 ] ; for ( int i = 0 ; i < k - 1 ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = k - 1 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; int flag1 = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( vowel ( j ) == false && cnt [ j ] > 0 ) { flag1 = 1 ; break ; } } if ( flag1 == 0 ) return true ; cnt [ s . charAt ( i - k + 1 ) - ' a ' ] -- ; } return false ; } static int longestSubString ( String s ) { int l = 1 , r = s . length ( ) ; int maxi = 0 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( check ( s , mid ) ) { l = mid + 1 ; maxi = Math . max ( maxi , mid ) ; } else r = mid - 1 ; } return maxi ; } public static void main ( String [ ] args ) { String s = " sedrewaefhoiu " ; System . out . print ( longestSubString ( s ) ) ; } }
class GFG { static int countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int [ ] suffix = new int [ 1005 ] ; long total_pairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) suffix [ Y [ i ] ] ++ ; for ( int i = ( int ) 1e3 ; i >= 3 ; i -- ) suffix [ i ] += suffix [ i + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( X [ i ] == 0 ) continue ; else if ( X [ i ] == 1 ) { total_pairs += suffix [ 0 ] ; continue ; } else if ( X [ i ] == 2 ) total_pairs += suffix [ 5 ] ; else if ( X [ i ] == 3 ) total_pairs += suffix [ 2 ] + suffix [ 4 ] ; else total_pairs += suffix [ X [ i ] + 1 ] ; total_pairs += suffix [ 0 ] + suffix [ 1 ] ; } return ( int ) total_pairs ; } public static void main ( String [ ] args ) { int X [ ] = { 10 , 19 , 18 } ; int Y [ ] = { 11 , 15 , 9 } ; int m = X . length ; int n = Y . length ; System . out . print ( countPairs ( X , Y , m , n ) ) ; } }
import java . util . * ; class GFG { static int solve ( int A [ ] [ ] , int N , int M ) { int [ ] [ ] B = new int [ N ] [ M ] ; int c = 1 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) B [ i ] [ j ] = c ++ ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i != 0 ) { if ( A [ i - 1 ] [ j ] == A [ i ] [ j ] ) B [ i - 1 ] [ j ] = B [ i ] [ j ] ; } if ( i != N - 1 ) { if ( A [ i + 1 ] [ j ] == A [ i ] [ j ] ) B [ i + 1 ] [ j ] = B [ i ] [ j ] ; } if ( j != 0 ) { if ( A [ i ] [ j - 1 ] == A [ i ] [ j ] ) B [ i ] [ j - 1 ] = B [ i ] [ j ] ; } if ( j != M - 1 ) { if ( A [ i ] [ j + 1 ] == A [ i ] [ j ] ) B [ i ] [ j + 1 ] = B [ i ] [ j ] ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) s . add ( B [ i ] [ j ] ) ; } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; int A [ ] [ ] = { { 2 , 1 , 3 } , { 1 , 1 , 2 } } ; System . out . print ( solve ( A , N , M ) ) ; } }
class GFG { static int splitArray ( int start , int end , int [ ] arr , int [ ] prefix_sum ) { if ( start >= end ) return 0 ; for ( int k = start ; k < end ; ++ k ) { if ( ( prefix_sum [ k ] - prefix_sum [ start - 1 ] ) == ( prefix_sum [ end ] - prefix_sum [ k ] ) ) { return 1 + splitArray ( start , k , arr , prefix_sum ) + splitArray ( k + 1 , end , arr , prefix_sum ) ; } } return 0 ; } static void solve ( int arr [ ] , int n ) { int [ ] prefix_sum = new int [ n + 1 ] ; prefix_sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i - 1 ] ; } System . out . print ( splitArray ( 1 , n , arr , prefix_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 3 , 3 , 0 , 3 , 3 } ; int N = arr . length ; solve ( arr , N ) ; } }
class GFG { static int minDays ( int [ ] arr , int n , int k ) { int l = arr . length , left = 1 , right = ( int ) 1e9 ; if ( n * k > l ) return - 1 ; while ( left < right ) { int mid = ( left + right ) / 2 , cnt = 0 , product = 0 ; for ( int j = 0 ; j < l ; ++ j ) { if ( arr [ j ] > mid ) { cnt = 0 ; } else if ( ++ cnt >= k ) { product ++ ; cnt = 0 ; } } if ( product < n ) { left = mid + 1 ; } else { right = mid ; } } return left ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 3 , 10 , 2 } ; int n = 3 , k = 1 ; System . out . print ( minDays ( arr , n , k ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void minimumSum ( int a [ ] , int b [ ] , int c , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { dp [ i ] [ j ] = ( int ) 1e6 ; } } dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 0 ] + a [ i ] ) ; dp [ i ] [ 0 ] = Math . min ( dp [ i ] [ 0 ] , dp [ i - 1 ] [ 1 ] + a [ i ] + c ) ; dp [ i ] [ 1 ] = Math . min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 0 ] + b [ i ] + c ) ; dp [ i ] [ 1 ] = Math . min ( dp [ i ] [ 1 ] , dp [ i - 1 ] [ 1 ] + b [ i ] ) ; } System . out . print ( Math . min ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr1 [ ] = { 7 , 6 , 18 , 6 , 16 , 18 , 1 , 17 , 17 } ; int arr2 [ ] = { 6 , 9 , 3 , 10 , 9 , 1 , 10 , 1 , 5 } ; int C = 2 ; int N = arr1 . length ; minimumSum ( arr1 , arr2 , C , N ) ; } }
class GFG { public static int minimumMoves ( String s , int K ) { StringBuilder S = new StringBuilder ( s ) ; int N = S . length ( ) ; int ops = 0 ; int last_idx = - 1 ; for ( int i = 0 ; i < K ; i ++ ) { if ( S . charAt ( i ) == '1' ) last_idx = i ; } if ( last_idx == - 1 ) { ++ ops ; S . setCharAt ( K - 1 , '1' ) ; last_idx = K - 1 ; } for ( int i = 1 ; i < N - K + 1 ; i ++ ) { if ( last_idx < i ) last_idx = - 1 ; if ( S . charAt ( i + K - 1 ) == '1' ) last_idx = i + K - 1 ; if ( last_idx == - 1 ) { ++ ops ; S . setCharAt ( i + K - 1 , '1' ) ; last_idx = i + K - 1 ; } } return ops ; } public static void main ( String [ ] args ) { String S = "001010000" ; int K = 3 ; System . out . println ( minimumMoves ( S , K ) ) ; } }
import java . util . * ; class GFG { static char arr [ ] = { ' a ' , ' b ' , ' c ' } ; static void findString ( int n , int k , char [ ] s1 , char [ ] s2 ) { char [ ] s3 = s2 ; int d = 0 ; for ( int i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) d ++ ; } if ( ( d + 1 ) / 2 > k ) { System . out . print ( " - 1" + "NEW_LINE"); return ; } else { if ( k <= d ) { int X = d - k ; int T = 2 * k - d ; for ( int i = 0 ; i < s3 . length ; i ++ ) { if ( s1 [ i ] != s2 [ i ] ) { if ( T > 0 ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s2 [ i ] ) { s3 [ i ] = arr [ j ] ; T -- ; break ; } } } else if ( X > 0 ) { s3 [ i ] = s1 [ i ] ; X -- ; } } } System . out . print ( new String ( s3 ) + "NEW_LINE"); } else { for ( int i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] != s3 [ i ] ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } for ( int i = 0 ; i < s1 . length ; i ++ ) { if ( s1 [ i ] == s3 [ i ] && k > 0 ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( arr [ j ] != s1 [ i ] && arr [ j ] != s3 [ i ] ) { s3 [ i ] = arr [ j ] ; k -- ; break ; } } } } System . out . print ( new String ( s3 ) + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int N = 4 , k = 2 ; String S1 = " zzyy " ; String S2 = " zxxy " ; findString ( N , k , S1 . toCharArray ( ) , S2 . toCharArray ( ) ) ; } }
import java . util . * ; class GFG { static void findSolutions ( int N ) { int count = 0 ; for ( int i = 1 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { if ( ( i + N / i ) % 2 == 0 ) { count ++ ; } } } System . out . print ( 4 * count ) ; } public static void main ( String [ ] args ) { int N = 80 ; findSolutions ( N ) ; } }
import java . util . * ; class GFG { static long check ( int A , int B , int C , int D , long x ) { long ans ; ans = ( A * x * x * x + B * x * x + C * x + D ) ; return ans ; } static void findSolution ( int A , int B , int C , int D , int E ) { long start = 0 , end = 100000 ; long mid , ans ; while ( start <= end ) { mid = start + ( end - start ) / 2 ; ans = check ( A , B , C , D , mid ) ; if ( ans == E ) { System . out . println ( mid ) ; return ; } if ( ans < E ) start = mid + 1 ; else end = mid - 1 ; } System . out . println ( " NA " ) ; } public static void main ( String args [ ] ) { int A = 1 , B = 0 , C = 0 ; int D = 0 , E = 27 ; findSolution ( A , B , C , D , E ) ; } }
class GFG { static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } static int nthRootSearch ( int low , int high , int N , int K ) { if ( low <= high ) { int mid = ( low + high ) / 2 ; if ( ( power ( mid , K ) <= N ) && ( power ( mid + 1 , K ) > N ) ) { return mid ; } else if ( power ( mid , K ) < N ) { return nthRootSearch ( mid + 1 , high , N , K ) ; } else { return nthRootSearch ( low , mid - 1 , N , K ) ; } } return low ; } public static void main ( String s [ ] ) { int N = 16 , K = 4 ; System . out . println ( nthRootSearch ( 0 , N , N , K ) ) ; } }
class GFG { static int countTriplets ( String color ) { int result = 0 , Blue_Count = 0 ; int Red_Count = 0 ; int len = color . length ( ) ; int i ; for ( i = 0 ; i < len ; i ++ ) { if ( color . charAt ( i ) == ' B ' ) Blue_Count ++ ; } for ( i = 0 ; i < len ; i ++ ) { if ( color . charAt ( i ) == ' B ' ) Blue_Count -- ; if ( color . charAt ( i ) == ' R ' ) Red_Count ++ ; if ( color . charAt ( i ) == ' G ' ) result += Red_Count * Blue_Count ; } return result ; } public static void main ( String [ ] args ) { String color = " RRGGBBRGGBB " ; System . out . println ( countTriplets ( color ) ) ; } }
import java . util . * ; class GFG { static int minMaxDiff ( int arr [ ] , int n , int k ) { int max_adj_dif = Integer . MIN_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) max_adj_dif = Math . max ( max_adj_dif , Math . abs ( arr [ i ] - arr [ i + 1 ] ) ) ; if ( max_adj_dif == 0 ) return 0 ; int best = 1 ; int worst = max_adj_dif ; int mid , required ; while ( best < worst ) { mid = ( best + worst ) / 2 ; required = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { required += ( Math . abs ( arr [ i ] - arr [ i + 1 ] ) - 1 ) / mid ; } if ( required > k ) best = mid + 1 ; else worst = mid ; } return worst ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 12 , 25 , 50 } ; int n = arr . length ; int k = 7 ; System . out . println ( minMaxDiff ( arr , n , k ) ) ; } }
class GFG { static void minpoint ( int arr [ ] , int n , int k ) { int min_point = 0 ; for ( int i = 1 ; i < k - 1 ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) min_point += 1 ; } int final_point = min_point ; for ( int i = k ; i < n ; i ++ ) { if ( arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) + 1 ] && arr [ i - ( k - 1 ) ] < arr [ i - ( k - 1 ) - 1 ] ) min_point -= 1 ; if ( arr [ i - 1 ] < arr [ i ] && arr [ i - 1 ] < arr [ i - 2 ] ) min_point += 1 ; if ( min_point > final_point ) final_point = min_point ; } System . out . println ( final_point ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 2 , 3 , 4 , 1 , 2 } ; int n = arr . length ; int k = 4 ; minpoint ( arr , n , k ) ; } }
import java . util . * ; class GFG { static void checkMin ( int arr [ ] , int len ) { int smallest = Integer . MAX_VALUE ; int secondSmallest = Integer . MAX_VALUE ; for ( int i = 0 ; i < len ; i ++ ) { if ( arr [ i ] < smallest ) { secondSmallest = smallest ; smallest = arr [ i ] ; } else if ( arr [ i ] < secondSmallest ) { secondSmallest = arr [ i ] ; } } if ( 2 * smallest <= secondSmallest ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int len = arr . length ; checkMin ( arr , len ) ; } }
class GFG { static void CountStrings ( String [ ] str ) { int size = str . length ; int [ ] count = new int [ 26 ] ; boolean [ ] visited = new boolean [ 26 ] ; for ( int i = 0 ; i < size ; ++ i ) { for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { if ( visited [ str [ i ] . charAt ( j ) - ' a ' ] == false ) count [ str [ i ] . charAt ( j ) - ' a ' ] ++ ; visited [ str [ i ] . charAt ( j ) - ' a ' ] = true ; } for ( int j = 0 ; j < str [ i ] . length ( ) ; ++ j ) { visited [ str [ i ] . charAt ( j ) - ' a ' ] = false ; } } for ( int i = 0 ; i < 26 ; ++ i ) { System . out . print ( count [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { String [ ] str = { " i " , " will " , " practice " , " everyday " } ; CountStrings ( str ) ; } }
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static void distanceSort ( int arr [ ] , int K , int n ) { Vector vd [ ] = new Vector [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { vd [ i ] = new Vector ( ) ; } int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == K ) { pos = i ; break ; } } int i = pos - 1 , j = pos + 1 ; vd [ 0 ] . add ( arr [ pos ] ) ; while ( i >= 0 ) { vd [ pos - i ] . add ( arr [ i ] ) ; -- i ; } while ( j < n ) { vd [ j - pos ] . add ( arr [ j ] ) ; ++ j ; } for ( i = 0 ; i <= Math . max ( pos , n - pos - 1 ) ; ++ i ) { Collections . sort ( vd [ i ] ) ; for ( j = 0 ; j < vd [ i ] . size ( ) ; j ++ ) { int element = ( int ) vd [ i ] . get ( j ) ; System . out . print ( element + " ▁ " ) ; } } } public static void main ( String s [ ] ) { int arr [ ] = { 14 , 1101 , 10 , 35 , 0 } ; int K = 35 ; int n = arr . length ; distanceSort ( arr , K , n ) ; } }
class GFG { static int [ ] find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return new int [ ] { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return new int [ ] { mid , n } ; } return find ( dividend , divisor , start , end ) ; } static int [ ] divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } public static void main ( String [ ] args ) { int dividend = 10 , divisor = 3 ; int [ ] ans = divide ( dividend , divisor ) ; System . out . print ( ans [ 0 ] + " , ▁ " ) ; System . out . print ( ans [ 1 ] + "NEW_LINE"); } }
import java . util . * ; class GFG { static int countTriplets ( int a [ ] , int n , int x ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { Vector < Integer > temp = new Vector < > ( ) ; temp . add ( a [ i ] ) ; temp . add ( a [ j ] ) ; temp . add ( a [ k ] ) ; Collections . sort ( temp ) ; if ( temp . get ( 0 ) < temp . get ( 1 ) && temp . get ( 1 ) < temp . get ( 2 ) && temp . get ( 0 ) * temp . get ( 1 ) * temp . get ( 2 ) <= x ) answer ++ ; } } } return answer ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 5 , 7 } ; int N = A . length ; int X = 42 ; System . out . println ( countTriplets ( A , N , X ) ) ; } }
import java . util . * ; class GFG { static int log ( int a , int b ) { return ( int ) ( Math . log10 ( a ) / Math . log10 ( b ) ) ; } static int get ( int a , int b , int n ) { int lo = 0 , hi = ( int ) 1e6 ; int ans = 0 ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; int dig = ( int ) Math . ceil ( ( mid * log ( mid , b ) + log ( a , b ) ) ) ; if ( dig > n ) { hi = mid - 1 ; } else { ans = mid ; lo = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int a = 2 , b = 2 , n = 6 ; System . out . print ( get ( a , b , n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void findMaxMatrixSize ( int [ ] [ ] arr , int K ) { int i , j ; int n = arr . length ; int m = arr [ 0 ] . length ; int [ ] [ ] sum = new int [ n + 1 ] [ m + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= m ; j ++ ) { if ( i == 0 j == 0 ) { sum [ i ] [ j ] = 0 ; continue ; } sum [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + sum [ i - 1 ] [ j ] + sum [ i ] [ j - 1 ] - sum [ i - 1 ] [ j - 1 ] ; } } int ans = 0 ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= m ; j ++ ) { if ( i + ans - 1 > n j + ans - 1 > m ) break ; int mid , lo = ans ; int hi = Math . min ( n - i + 1 , m - j + 1 ) ; while ( lo < hi ) { mid = ( hi + lo + 1 ) / 2 ; if ( sum [ i + mid - 1 ] [ j + mid - 1 ] + sum [ i - 1 ] [ j - 1 ] - sum [ i + mid - 1 ] [ j - 1 ] - sum [ i - 1 ] [ j + mid - 1 ] <= K ) { lo = mid ; } else { hi = mid - 1 ; } } ans = Math . max ( ans , lo ) ; } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } , { 1 , 1 , 3 , 2 , 4 , 3 , 2 } } ; int K = 4 ; findMaxMatrixSize ( arr , K ) ; } }
import java . util . * ; class GFG { static int longestPermutation ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } int len = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ! freq . containsKey ( i ) ) { break ; } len ++ ; } return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 6 , 5 } ; int n = arr . length ; System . out . print ( longestPermutation ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000005 ; static Set < Integer > fib = new HashSet < Integer > ( ) ; static void fibonacci ( ) { int prev = 0 , curr = 1 , len = 2 ; fib . add ( prev ) ; fib . add ( curr ) ; while ( len <= MAX ) { int temp = curr + prev ; fib . add ( temp ) ; prev = curr ; curr = temp ; len ++ ; } } static void findFibonacci ( int N ) { for ( int i = N / 2 ; i > 1 ; i -- ) { if ( fib . contains ( i ) && fib . contains ( N - i ) ) { System . out . println ( i + " ▁ " + ( N - i ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String args [ ] ) { fibonacci ( ) ; int sum = 199 ; findFibonacci ( sum ) ; } }
import java . util . * ; class GFG { static final int MAX = 100005 ; static HashSet < Integer > fibonacci = new HashSet < Integer > ( ) ; static void createHash ( ) { int prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= MAX ) { int temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } static boolean checkArray ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( fibonacci . contains ( arr [ i ] ) ) sum += arr [ i ] ; if ( fibonacci . contains ( sum ) ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 2 } ; int n = arr . length ; createHash ( ) ; if ( checkArray ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int N = 100001 ; static int [ ] [ ] f = new int [ 26 ] [ N ] ; static void precompute ( String s , int n ) { for ( int i = 0 ; i < n ; i ++ ) { f [ s . charAt ( i ) - ' a ' ] [ i ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { f [ i ] [ j ] += f [ i ] [ j - 1 ] ; } } } static int palindromicSubsequencesUtil ( int L , int R ) { int c = 0 , ok = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int cnt = f [ i ] [ R ] ; if ( L > 0 ) cnt -= f [ i ] [ L - 1 ] ; if ( cnt > 1 ) { ok = 1 ; c = i ; break ; } } if ( ok == 0 ) { return - 1 ; } return c ; } static void palindromicSubsequences ( int Q [ ] [ ] , int l ) { for ( int i = 0 ; i < l ; i ++ ) { int x = palindromicSubsequencesUtil ( Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; if ( x == - 1 ) { System . out . print ( - 1 + "NEW_LINE"); } else { char c = ( char ) ( ' a ' + x ) ; System . out . print ( ( char ) c + " " + ( char ) c + "NEW_LINE"); } } } public static void main ( String [ ] args ) { String str = " dbdeke " ; int Q [ ] [ ] = { { 0 , 5 } , { 1 , 5 } , { 1 , 3 } } ; int n = str . length ( ) ; int l = Q . length ; precompute ( str , n ) ; palindromicSubsequences ( Q , l ) ; } }
import java . util . * ; class GFG { static final int N = 100000 ; static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static int longestFibSubarray ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , max_val ) ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; int fibcount = 0 , res = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = fibcount ; if ( hash . contains ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } fibcount = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right [ i ] = fibcount ; if ( hash . contains ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , left [ i ] + right [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 8 , 5 , 7 , 3 , 5 , 7 } ; int n = arr . length ; System . out . print ( longestFibSubarray ( arr , n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static boolean isPrime ( int N ) { if ( N < 2 ) return false ; if ( N < 4 ) return true ; if ( ( N & 1 ) == 0 ) return false ; if ( N % 3 == 0 ) return false ; int curr = 5 , s = ( int ) Math . sqrt ( N ) ; while ( curr <= s ) { if ( N % curr == 0 ) return false ; curr += 2 ; if ( N % curr == 0 ) return false ; curr += 4 ; } return true ; } static boolean check ( int s , int p , int prefix_sum [ ] , int n ) { boolean satisfies = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + s - 1 >= n ) break ; if ( prefix_sum [ i + s - 1 ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) < p ) satisfies = false ; } return satisfies ; } static int minimumWindowSize ( int x , int y , int p ) { int [ ] prefix_sum = new int [ y - x + 1 ] ; for ( int i = x ; i <= y ; i ++ ) { if ( isPrime ( i ) ) prefix_sum [ i - x ] = 1 ; } for ( int i = 1 ; i < y - x + 1 ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int low = 1 , high = y - x + 1 ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , p , prefix_sum , y - x + 1 ) ) { high = mid ; } else low = mid ; } if ( check ( low , p , prefix_sum , y - x + 1 ) ) return low ; return high ; } public static void main ( String [ ] args ) { int x = 12 ; int y = 42 ; int p = 3 ; System . out . print ( minimumWindowSize ( x , y , p ) ) ; } }
class GFG { static final int N = 4 ; static final int M = 5 ; static void preProcess ( int [ ] [ ] mat , int [ ] [ ] aux ) { for ( int i = 0 ; i < M ; i ++ ) aux [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) aux [ i ] [ j ] = mat [ i ] [ j ] + aux [ i - 1 ] [ j ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 1 ; j < M ; j ++ ) aux [ i ] [ j ] += aux [ i ] [ j - 1 ] ; } static int sumQuery ( int [ ] [ ] aux , int tli , int tlj , int rbi , int rbj ) { int res = aux [ rbi ] [ rbj ] ; if ( tli > 0 ) res = res - aux [ tli - 1 ] [ rbj ] ; if ( tlj > 0 ) res = res - aux [ rbi ] [ tlj - 1 ] ; if ( tli > 0 && tlj > 0 ) res = res + aux [ tli - 1 ] [ tlj - 1 ] ; return res ; } static int maximumSquareSize ( int [ ] [ ] mat , int K ) { int [ ] [ ] aux = new int [ N ] [ M ] ; preProcess ( mat , aux ) ; for ( int i = Math . min ( N , M ) ; i >= 1 ; i -- ) { boolean satisfies = true ; for ( int x = 0 ; x < N ; x ++ ) { for ( int y = 0 ; y < M ; y ++ ) { if ( x + i - 1 <= N - 1 && y + i - 1 <= M - 1 ) { if ( sumQuery ( aux , x , y , x + i - 1 , y + i - 1 ) > K ) satisfies = false ; } } } if ( satisfies == true ) return ( i ) ; } return 0 ; } public static void main ( String [ ] args ) { int K = 30 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 4 , 6 } , { 5 , 3 , 8 , 1 , 2 } , { 4 , 6 , 7 , 5 , 5 } , { 2 , 4 , 8 , 9 , 4 } } ; System . out . print ( maximumSquareSize ( mat , K ) ) ; } }
import java . util . * ; class GFG { static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr < maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static void findFibonacciPair ( int n ) { HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . contains ( i ) && hash . contains ( n - i ) ) { System . out . print ( i + " , ▁ " + ( n - i ) + "NEW_LINE"); return ; } } System . out . print ( "-1NEW_LINE"); } public static void main ( String [ ] args ) { int N = 90 ; findFibonacciPair ( N ) ; } }
import java . util . * ; class GFG { static void add_factors ( int n , HashMap < Integer , Integer > mp ) { for ( int i = 1 ; i <= ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( mp . containsKey ( i ) ) mp . put ( i , mp . get ( i ) + 1 ) ; else mp . put ( i , 1 ) ; } else { if ( mp . containsKey ( i ) ) mp . put ( i , mp . get ( i ) + 1 ) ; else mp . put ( i , 1 ) ; if ( mp . containsKey ( n / i ) ) mp . put ( n / i , mp . get ( n / i ) + 1 ) ; else mp . put ( n / i , 1 ) ; } } } } static void count_divisors ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( mp . get ( a [ i ] ) == null ? 0 + " ▁ " : mp . get ( a [ i ] ) + " ▁ " ) ; add_factors ( a [ i ] , mp ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; count_divisors ( arr , n ) ; } }
class GFG { static void removeDuplicates ( int arr [ ] , int n ) { int st = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } System . out . print ( " { " ) ; for ( int i = 0 ; i < st ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i != st - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 } ; int n = arr . length ; removeDuplicates ( arr , n ) ; } }
class GFG { static int nfa = 1 ; static int flag = 0 ; static void state1 ( char c ) { if ( c == ' a ' ) nfa = 2 ; else if ( c == ' b ' c == ' c ' ) nfa = 1 ; else flag = 1 ; } static void state2 ( char c ) { if ( c == ' a ' ) nfa = 3 ; else if ( c == ' b ' c == ' c ' ) nfa = 2 ; else flag = 1 ; } static void state3 ( char c ) { if ( c == ' a ' ) nfa = 1 ; else if ( c == ' b ' c == ' c ' ) nfa = 3 ; else flag = 1 ; } static void state4 ( char c ) { if ( c == ' b ' ) nfa = 5 ; else if ( c == ' a ' c == ' c ' ) nfa = 4 ; else flag = 1 ; } static void state5 ( char c ) { if ( c == ' b ' ) nfa = 6 ; else if ( c == ' a ' c == ' c ' ) nfa = 5 ; else flag = 1 ; } static void state6 ( char c ) { if ( c == ' b ' ) nfa = 4 ; else if ( c == ' a ' c == ' c ' ) nfa = 6 ; else flag = 1 ; } static void state7 ( char c ) { if ( c == ' c ' ) nfa = 8 ; else if ( c == ' b ' c == ' a ' ) nfa = 7 ; else flag = 1 ; } static void state8 ( char c ) { if ( c == ' c ' ) nfa = 9 ; else if ( c == ' b ' c == ' a ' ) nfa = 8 ; else flag = 1 ; } static void state9 ( char c ) { if ( c == ' c ' ) nfa = 7 ; else if ( c == ' b ' c == ' a ' ) nfa = 9 ; else flag = 1 ; } static boolean checkA ( String s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 1 ) state1 ( s . charAt ( i ) ) ; else if ( nfa == 2 ) state2 ( s . charAt ( i ) ) ; else if ( nfa == 3 ) state3 ( s . charAt ( i ) ) ; } if ( nfa == 1 ) { return true ; } else { nfa = 4 ; } return false ; } static boolean checkB ( String s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 4 ) state4 ( s . charAt ( i ) ) ; else if ( nfa == 5 ) state5 ( s . charAt ( i ) ) ; else if ( nfa == 6 ) state6 ( s . charAt ( i ) ) ; } if ( nfa == 4 ) { return true ; } else { nfa = 7 ; } return false ; } static boolean checkC ( String s , int x ) { for ( int i = 0 ; i < x ; i ++ ) { if ( nfa == 7 ) state7 ( s . charAt ( i ) ) ; else if ( nfa == 8 ) state8 ( s . charAt ( i ) ) ; else if ( nfa == 9 ) state9 ( s . charAt ( i ) ) ; } if ( nfa == 7 ) { return true ; } return false ; } public static void main ( String [ ] args ) { String s = " bbbca " ; int x = 5 ; if ( checkA ( s , x ) || checkB ( s , x ) || checkC ( s , x ) ) { System . out . println ( " ACCEPTED " ) ; } else { if ( flag == 0 ) { System . out . println ( " NOT ▁ ACCEPTED " ) ; } else { System . out . println ( " INPUT ▁ OUT ▁ OF ▁ DICTIONARY . " ) ; } } } }
class GFG { static int getPositionCount ( int a [ ] , int n ) { int count = 1 ; int min = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= min ) { min = a [ i ] ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 6 , 1 , 3 , 1 } ; int n = a . length ; System . out . print ( getPositionCount ( a , n ) ) ; } }
class GFG { static int CountSubSet ( int arr [ ] , int n , int X ) { int N = ( int ) Math . pow ( 2 , n ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) if ( arr [ j ] == X ) count += 1 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = arr . length ; System . out . print ( CountSubSet ( arr , n , X ) ) ; } }
class GFG { static int calculatePower ( int b , int e ) { int ans = 1 ; while ( e > 0 ) { if ( e % 2 == 1 ) ans = ans * b ; e = e / 2 ; b = b * b ; } return ans ; } static int CountSubSet ( int arr [ ] , int n , int X ) { int count = 0 , checkX = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == X ) { checkX = 1 ; break ; } } if ( checkX == 1 ) count = calculatePower ( 2 , n - 1 ) ; else count = 0 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = arr . length ; System . out . println ( CountSubSet ( arr , n , X ) ) ; } }
class GFG { static int maxSum ( int arr [ ] , int n , int k ) { if ( n < k ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < k ; i ++ ) res += arr [ i ] ; int curr_sum = res ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; res = Math . max ( res , curr_sum ) ; } return res ; } static int solve ( int arr [ ] , int n , int k ) { int max_len = 0 , l = 0 , r = n , m ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( maxSum ( arr , n , m ) > k ) r = m - 1 ; else { l = m + 1 ; max_len = m ; } } return max_len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 10 ; System . out . println ( solve ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static boolean isValidLen ( String s , int len , int k ) { int n = s . length ( ) ; Map < Character , Integer > mp = new HashMap < Character , Integer > ( ) ; int right = 0 ; while ( right < len ) { if ( mp . containsKey ( s . charAt ( right ) ) ) { mp . put ( s . charAt ( right ) , mp . get ( s . charAt ( right ) ) + 1 ) ; } else { mp . put ( s . charAt ( right ) , 1 ) ; } right ++ ; } if ( mp . size ( ) <= k ) return true ; while ( right < n ) { if ( mp . containsKey ( s . charAt ( right ) ) ) { mp . put ( s . charAt ( right ) , mp . get ( s . charAt ( right ) ) + 1 ) ; } else { mp . put ( s . charAt ( right ) , 1 ) ; } if ( mp . containsKey ( s . charAt ( right - len ) ) ) { mp . put ( s . charAt ( right - len ) , mp . get ( s . charAt ( right - len ) ) - 1 ) ; } if ( mp . get ( s . charAt ( right - len ) ) == 0 ) mp . remove ( s . charAt ( right - len ) ) ; if ( mp . size ( ) <= k ) return true ; right ++ ; } return mp . size ( ) <= k ; } static int maxLenSubStr ( String s , int k ) { Set < Character > uni = new HashSet < Character > ( ) ; for ( Character x : s . toCharArray ( ) ) uni . add ( x ) ; if ( uni . size ( ) < k ) return - 1 ; int n = s . length ( ) ; int lo = - 1 , hi = n + 1 ; while ( hi - lo > 1 ) { int mid = lo + hi >> 1 ; if ( isValidLen ( s , mid , k ) ) lo = mid ; else hi = mid ; } return lo ; } public static void main ( String [ ] args ) { String s = " aabacbebebe " ; int k = 3 ; System . out . print ( maxLenSubStr ( s , k ) ) ; } }
import java . util . Arrays ; class GFG { static int [ ] height = new int [ ] { 1 , 2 , 1 , 2 } ; public static int woodCollected ( int n , int m ) { int sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] - m <= 0 ) break ; sum += ( height [ i ] - m ) ; } return sum ; } public static int collectKWood ( int n , int k ) { Arrays . sort ( height ) ; int low = 0 , high = height [ n - 1 ] ; while ( low <= high ) { int mid = low + ( ( high - low ) / 2 ) ; int collected = woodCollected ( n , mid ) ; if ( collected == k ) return mid ; if ( collected > k ) low = mid + 1 ; else high = mid - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int k = 2 ; int n = height . length ; System . out . print ( collectKWood ( n , k ) ) ; } }
class GFG { static int MAX = 10 ; static boolean isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static boolean allDigits ( String str , int len ) { boolean [ ] present = new boolean [ MAX ] ; for ( int i = 0 ; i < len ; i ++ ) { if ( isDigit ( str . charAt ( i ) ) ) { int digit = str . charAt ( i ) - '0' ; present [ digit ] = true ; } } for ( int i = 0 ; i < MAX ; i ++ ) { if ( ! present [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " Geeks12345for69708" ; int len = str . length ( ) ; if ( allDigits ( str , len ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static boolean isPlusPossible ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } boolean foundModOne = false ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { int element = x . getKey ( ) ; int frequency = x . getValue ( ) ; if ( frequency % 4 == 0 ) continue ; if ( frequency % 4 == 1 ) { if ( foundModOne ) return false ; foundModOne = true ; } else return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 , 2 , 2 , 2 , 3 , 2 } ; int n = arr . length ; if ( isPlusPossible ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static Vector < Integer > merge ( Vector < Integer > v1 , Vector < Integer > v2 ) { int i = 0 , j = 0 ; Vector < Integer > v = new Vector < > ( ) ; while ( i < v1 . size ( ) && j < v2 . size ( ) ) { if ( v1 . elementAt ( i ) <= v2 . elementAt ( j ) ) { v . add ( v1 . elementAt ( i ) ) ; i ++ ; } else { v . add ( v2 . elementAt ( j ) ) ; j ++ ; } } for ( int k = i ; k < v1 . size ( ) ; k ++ ) v . add ( v1 . elementAt ( k ) ) ; for ( int k = j ; k < v2 . size ( ) ; k ++ ) v . add ( v2 . elementAt ( k ) ) ; return v ; } static void buildTree ( Vector < Integer > [ ] tree , int [ ] arr , int index , int s , int e ) { if ( s == e ) { tree [ index ] . add ( arr [ s ] ) ; return ; } int mid = ( s + e ) / 2 ; buildTree ( tree , arr , 2 * index , s , mid ) ; buildTree ( tree , arr , 2 * index + 1 , mid + 1 , e ) ; tree [ index ] = merge ( tree [ 2 * index ] , tree [ 2 * index + 1 ] ) ; } static int query ( Vector < Integer > [ ] tree , int index , int s , int e , int l , int r , int k ) { if ( r < s l > e ) return 0 ; if ( s >= l && e <= r ) { return ( tree [ index ] . size ( ) - lowerBound ( tree [ index ] , tree [ index ] . size ( ) , k ) ) ; } int mid = ( s + e ) / 2 ; return ( query ( tree , 2 * index , s , mid , l , r , k ) + query ( tree , 2 * index + 1 , mid + 1 , e , l , r , k ) ) ; } static void performQueries ( int L [ ] , int R [ ] , int K [ ] , int n , int q , Vector < Integer > tree [ ] ) { for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( query ( tree , 1 , 0 , n - 1 , L [ i ] - 1 , R [ i ] - 1 , K [ i ] ) ) ; } } static int lowerBound ( Vector < Integer > array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value <= array . elementAt ( mid ) ) { high = mid ; } else { low = mid + 1 ; } } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = arr . length ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] tree = new Vector [ 4 * n + 1 ] ; for ( int i = 0 ; i < ( 4 * n + 1 ) ; i ++ ) { tree [ i ] = new Vector < > ( ) ; } buildTree ( tree , arr , 1 , 0 , n - 1 ) ; int L [ ] = { 1 , 2 } ; int R [ ] = { 4 , 6 } ; int K [ ] = { 6 , 8 } ; int q = L . length ; performQueries ( L , R , K , n , q , tree ) ; } }
public class GFG { int [ ] arr , tree ; int combine ( int a , int b ) { if ( a != 0 && b != 0 ) { return a ; } if ( a >= b ) { return a ; } return b ; } void buildTree ( int ind , int low , int high , int x ) { if ( low == high ) { if ( arr [ low ] > x ) { tree [ ind ] = arr [ low ] ; } else { tree [ ind ] = 0 ; } return ; } int mid = ( high - low ) / 2 + low ; buildTree ( 2 * ind + 1 , low , mid , x ) ; buildTree ( 2 * ind + 2 , mid + 1 , high , x ) ; tree [ ind ] = combine ( tree [ 2 * ind + 1 ] , tree [ 2 * ind + 2 ] ) ; } int query ( int ind , int low , int high , int l , int r ) { int mid = ( high - low ) / 2 + low ; if ( low > r high < l ) { return 0 ; } if ( l <= low && r >= high ) { return tree [ ind ] ; } return combine ( query ( 2 * ind + 1 , low , mid , l , r ) , query ( 2 * ind + 2 , mid + 1 , high , l , r ) ) ; } public static void main ( String args [ ] ) { GFG ob = new GFG ( ) ; ob . arr = new int [ ] { 7 , 3 , 9 , 13 , 5 , 4 } ; int n = ob . arr . length ; int k = 6 ; ob . tree = new int [ 4 * n ] ; int l = 1 , r = 4 ; ob . buildTree ( 0 , 0 , n - 1 , k ) ; System . out . println ( ob . query ( 0 , 0 , n - 1 , l - 1 , r - 1 ) ) ; } }
class GFG { static int findGCDSum ( int n , int a [ ] ) { int GCDSum = 0 ; int tempGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( int k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int totalSum = findGCDSum ( n , a ) ; System . out . print ( totalSum + "NEW_LINE"); } }
class GFG { static boolean isSquarePossible ( int arr [ ] , int n , int l ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= l ) cnt ++ ; if ( cnt >= l ) return true ; } return false ; } static int maxArea ( int arr [ ] , int n ) { int l = 0 , r = n ; int len = 0 ; while ( l <= r ) { int m = l + ( ( r - l ) / 2 ) ; if ( isSquarePossible ( arr , n , m ) ) { len = m ; l = m + 1 ; } else r = m - 1 ; } return ( len * len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 5 , 5 } ; int n = arr . length ; System . out . println ( maxArea ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100001 ; static int ROW = 10 ; static int COl = 3 ; static Vector < Integer > [ ] indices = new Vector [ MAX ] ; static int test [ ] [ ] = { { 2 , 3 , 6 } , { 2 , 4 , 4 } , { 2 , 6 , 3 } , { 3 , 2 , 6 } , { 3 , 3 , 3 } , { 3 , 6 , 2 } , { 4 , 2 , 4 } , { 4 , 4 , 2 } , { 6 , 2 , 3 } , { 6 , 3 , 2 } } ; static int find_triplet ( int array [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { indices [ i ] = new Vector < > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { indices [ array [ i ] ] . add ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { int y = array [ i ] ; for ( int j = 0 ; j < ROW ; j ++ ) { int s = test [ j ] [ 1 ] * y ; if ( s % test [ j ] [ 0 ] != 0 ) continue ; if ( s % test [ j ] [ 2 ] != 0 ) continue ; int x = s / test [ j ] [ 0 ] ; int z = s / test [ j ] [ 2 ] ; if ( x > MAX z > MAX ) continue ; int l = 0 ; int r = indices [ x ] . size ( ) - 1 ; int first = - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( indices [ x ] . get ( m ) < i ) { first = m ; l = m + 1 ; } else { r = m - 1 ; } } l = 0 ; r = indices [ z ] . size ( ) - 1 ; int third = - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( indices [ z ] . get ( m ) > i ) { third = m ; r = m - 1 ; } else { l = m + 1 ; } } if ( first != - 1 && third != - 1 ) { answer += ( first + 1 ) * ( indices [ z ] . size ( ) - third ) ; } } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 2 , 4 , 5 , 6 , 7 } ; int n = array . length ; System . out . println ( find_triplet ( array , n ) ) ; } }
import java . util . * ; class GFG { static void insertNames ( String arr [ ] , int n ) { HashSet < String > set = new HashSet < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! set . contains ( arr [ i ] ) ) { System . out . print ( "NoNEW_LINE"); set . add ( arr [ i ] ) ; } else { System . out . print ( "YesNEW_LINE"); } } } public static void main ( String [ ] args ) { String arr [ ] = { " geeks " , " for " , " geeks " } ; int n = arr . length ; insertNames ( arr , n ) ; } }
class GFG { static boolean containsElement ( int [ ] arr , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; ++ i ) xorArr ^= arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { int x = xorArr ^ arr [ i ] ; if ( arr [ i ] == x ) return true ; } return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 2 , 4 , 15 , 1 } ; int n = arr . length ; if ( containsElement ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static double Square ( double n , double i , double j ) { double mid = ( i + j ) / 2 ; double mul = mid * mid ; if ( ( mul == n ) || ( Math . abs ( mul - n ) < 0.00001 ) ) return mid ; else if ( mul < n ) return Square ( n , mid , j ) ; else return Square ( n , i , mid ) ; } static void findSqrt ( double n ) { double i = 1 ; boolean found = false ; while ( ! found ) { if ( i * i == n ) { System . out . println ( i ) ; found = true ; } else if ( i * i > n ) { double res = Square ( n , i - 1 , i ) ; System . out . printf ( " % .5f " , res ) ; found = true ; } i ++ ; } } public static void main ( String [ ] args ) { double n = 3 ; findSqrt ( n ) ; } }
import java . util . * ; class GFG { static int length_substring ( String S , char K ) { int curr_cnt = 0 , prev_cnt = 0 , max_len ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == K ) { curr_cnt += 1 ; } else { prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; curr_cnt = 0 ; } } prev_cnt = Math . max ( prev_cnt , curr_cnt ) ; max_len = prev_cnt ; return max_len ; } public static void main ( String [ ] args ) { String S = " abcd1111aabc " ; char K = '1' ; System . out . print ( length_substring ( S , K ) ) ; } }
class GFG { public static int Xor_Sum ( int [ ] arr , int n ) { int sum = 0 , index = - 1 ; int left_xor = 0 , right_xor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left_xor = left_xor ^ arr [ i ] ; right_xor = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { right_xor = right_xor ^ arr [ j ] ; } if ( left_xor + right_xor > sum ) { sum = left_xor + right_xor ; index = i ; } } return index + 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . length ; System . out . println ( Xor_Sum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void ComputePrefixXor ( int arr [ ] , int PrefixXor [ ] , int n ) { PrefixXor [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) PrefixXor [ i ] = PrefixXor [ i - 1 ] ^ arr [ i ] ; } static int Xor_Sum ( int arr [ ] , int n ) { int [ ] PrefixXor = new int [ n ] ; ComputePrefixXor ( arr , PrefixXor , n ) ; int sum = 0 , index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) > sum ) { sum = PrefixXor [ i ] + ( PrefixXor [ n - 1 ] ^ PrefixXor [ i ] ) ; index = i ; } } return index + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 3 , 8 , 13 , 34 , 2 , 21 , 10 } ; int n = arr . length ; System . out . println ( Xor_Sum ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children ; boolean isEndOfWord ; TrieNode ( ) { this . children = new TrieNode [ ALPHABET_SIZE ] ; } } static TrieNode getNode ( ) { TrieNode pNode = new TrieNode ( ) ; pNode . isEndOfWord = false ; Arrays . fill ( pNode . children , null ) ; return pNode ; } static void insert ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = getNode ( ) ; pCrawl . isEndOfWord = false ; pCrawl = pCrawl . children [ index ] ; } int i ; for ( i = 0 ; i < 26 ; i ++ ) { if ( pCrawl . children [ i ] != null ) { break ; } } if ( i == 26 ) { pCrawl . isEndOfWord = true ; } } static void display ( TrieNode root , char str [ ] , int level ) { if ( root . isEndOfWord ) { str [ level ] = ' \0' ; System . out . println ( str ) ; } int i ; for ( i = 0 ; i < ALPHABET_SIZE ; i ++ ) { if ( root . children [ i ] != null ) { str [ level ] = ( char ) ( i + ' a ' ) ; display ( root . children [ i ] , str , level + 1 ) ; } } } public static void main ( String [ ] args ) { String keys [ ] = { " apple " , " app " , " there " , " the " , " like " } ; int n = keys . length ; TrieNode root = getNode ( ) ; for ( int i = 0 ; i < n ; i ++ ) insert ( root , keys [ i ] ) ; char [ ] str = new char [ 100 ] ; display ( root , str , 0 ) ; } }
import java . util . * ; class GFG { static int findColor ( HashMap < Integer , Integer > mapWithColor , int query ) { int maximum = 0 ; while ( query >= 1 ) { if ( mapWithColor . containsKey ( query ) ) { maximum = Math . max ( maximum , mapWithColor . get ( query ) ) ; } if ( query % 2 == 1 ) query = ( query - 1 ) / 2 ; else query = query / 2 ; } return maximum ; } static HashMap < Integer , Integer > buildMapWithColor ( int arr [ ] , int n ) { HashMap < Integer , Integer > mapWithColor = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mapWithColor . put ( arr [ i ] , i + 1 ) ; } return mapWithColor ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 3 } ; int n = arr . length ; int k = 7 ; HashMap < Integer , Integer > mapWithColor = buildMapWithColor ( arr , n ) ; System . out . print ( findColor ( mapWithColor , k ) ) ; } }
class GFG { static void LeftRightNode ( int preorder [ ] , int n ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( min > preorder [ i ] ) min = preorder [ i ] ; if ( max < preorder [ i ] ) max = preorder [ i ] ; } System . out . println ( " Leftmost ▁ node ▁ is ▁ " + min ) ; System . out . println ( " Rightmost ▁ node ▁ is ▁ " + max ) ; } public static void main ( String [ ] args ) { int preorder [ ] = { 3 , 2 , 1 , 5 , 4 } ; int n = 5 ; LeftRightNode ( preorder , n ) ; } }
class GFG { static int distinct ( int [ ] arr , int n ) { int count = 0 ; if ( n == 1 ) return 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] != arr [ i + 1 ] ) count += 1 ; } else { if ( arr [ i ] != arr [ i + 1 ] arr [ i ] != arr [ i - 1 ] ) count += 1 ; } } if ( arr [ n - 1 ] != arr [ n - 2 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; int n = arr . length ; System . out . println ( distinct ( arr , n ) ) ; } }
class GFG { static int m = 6 , n = 4 ; static int compareRow ( int a1 [ ] , int a2 [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a1 [ i ] < a2 [ i ] ) return 1 ; else if ( a1 [ i ] > a2 [ i ] ) return - 1 ; } return 0 ; } static int binaryCheck ( int ar [ ] [ ] , int arr [ ] ) { int l = 0 , r = m - 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int temp = compareRow ( ar [ mid ] , arr ) ; if ( temp == 0 ) return mid + 1 ; else if ( temp == 1 ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ ] = { 10 , 9 , 22 , 23 } ; System . out . println ( binaryCheck ( mat , row ) ) ; } }
import java . util . * ; class GFG { static int maxLen = 30 ; static int findCnt ( int arr [ ] , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k arr [ 0 ] < - 1 * k ) ans ++ ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ; return ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 4 , - 5 , 6 } ; int n = arr . length ; int k = 0 ; System . out . println ( findCnt ( arr , n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int upperBound ( ArrayList < Integer > a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a . get ( middle ) > element ) { high = middle ; } else { low = middle + 1 ; } } return low ; } static int FindIntersection ( ArrayList < ArrayList < Integer > > type1 , int n , ArrayList < ArrayList < Integer > > type2 , int m ) { int ans = n * m ; ArrayList < Integer > start = new ArrayList < Integer > ( ) ; ArrayList < Integer > end = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { start . add ( type1 . get ( i ) . get ( 0 ) ) ; end . add ( type1 . get ( i ) . get ( 1 ) ) ; } Collections . sort ( start ) ; Collections . sort ( end ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = type2 . get ( i ) . get ( 0 ) ; int R = type2 . get ( i ) . get ( 1 ) ; ans -= start . size ( ) - upperBound ( start , 0 , start . size ( ) , R ) ; ans -= upperBound ( end , 0 , end . size ( ) , L - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > type1 = new ArrayList < ArrayList < Integer > > ( ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 2 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 5 ) ) ) ; type1 . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 7 ) ) ) ; ArrayList < ArrayList < Integer > > type2 = new ArrayList < ArrayList < Integer > > ( ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 7 ) ) ) ; type2 . add ( new ArrayList < Integer > ( Arrays . asList ( 5 , 7 ) ) ) ; int n = type1 . size ( ) ; int m = type2 . size ( ) ; System . out . println ( FindIntersection ( type1 , n , type2 , m ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int divTermCount ( int a , int b , int c , int num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / lcm ( a , b ) ) - ( num / lcm ( b , c ) ) - ( num / lcm ( a , c ) ) + ( num / lcm ( a , lcm ( b , c ) ) ) ) ; } static int findNthTerm ( int a , int b , int c , int n ) { int low = 1 , high = Integer . MAX_VALUE , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int a = 2 , b = 3 , c = 5 , n = 10 ; System . out . println ( findNthTerm ( a , b , c , n ) ) ; } }
class GFG { static boolean check ( int mid , int array [ ] , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } static int solve ( int array [ ] , int n , int K ) { int start = 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( array [ i ] > start ) } int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int n = array . length ; int K = 3 ; System . out . println ( solve ( array , n , K ) ) ; } }
import java . util . * ; class GFG { static void createSorted ( int a [ ] , int n ) { Vector < Integer > b = new Vector < > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( b . isEmpty ( ) ) b . add ( a [ j ] ) ; else { int start = 0 , end = b . size ( ) - 1 ; int pos = 0 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( b . get ( mid ) == a [ j ] ) { b . add ( ( Math . max ( 0 , mid + 1 ) ) , a [ j ] ) ; break ; } else if ( b . get ( mid ) > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . add ( Math . max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( b . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 5 , 4 , 9 , 8 } ; int n = a . length ; createSorted ( a , n ) ; } }
class GFG { static final int MAX = 26 ; static boolean atLeastK ( int freq [ ] , int k ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] < k ) return false ; } return true ; } static void setZero ( int freq [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) freq [ i ] = 0 ; } static int findlength ( String str , int n , int k ) { int maxLen = 0 ; int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { setZero ( freq ) ; for ( int j = i ; j < n ; j ++ ) { freq [ str . charAt ( j ) - ' a ' ] ++ ; if ( atLeastK ( freq , k ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } public static void main ( String args [ ] ) { String str = " xyxyyz " ; int n = str . length ( ) ; int k = 2 ; System . out . println ( findlength ( str , n , k ) ) ; } }
import java . util . HashSet ; class GFG { static class Node { int data ; Node next ; } ; static Node head ; static void insert ( Node ref_head , int item ) { Node temp = new Node ( ) ; temp . data = item ; temp . next = ref_head ; head = temp ; } static int countNode ( Node head ) { if ( head == null ) return 0 ; ; HashSet < Integer > s = new HashSet < > ( ) ; s . add ( head . data ) ; int count = 0 ; for ( Node curr = head . next ; curr != null ; curr = curr . next ) { if ( s . contains ( curr . data ) ) count ++ ; s . add ( curr . data ) ; } return count ; } public static void main ( String [ ] args ) { insert ( head , 5 ) ; insert ( head , 7 ) ; insert ( head , 5 ) ; insert ( head , 1 ) ; insert ( head , 7 ) ; System . out . println ( countNode ( head ) ) ; } }
import java . util . * ; class GFG { static int minSteps ( int arr [ ] , int n ) { boolean [ ] v = new boolean [ n ] ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; int depth = 0 ; while ( q . size ( ) > 0 ) { int x = q . size ( ) ; while ( x -- > 0 ) { int i = q . peek ( ) ; q . poll ( ) ; if ( v [ i ] ) continue ; if ( i == n - 1 ) return depth ; v [ i ] = true ; if ( i + arr [ i ] < n ) q . add ( i + arr [ i ] ) ; if ( i - arr [ i ] >= 0 ) q . add ( i - arr [ i ] ) ; } depth ++ ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( minSteps ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static boolean isPossible ( int box [ ] , int truck [ ] , int n , int m , int min_time ) { int temp = 0 ; int count = 0 ; while ( count < m ) { for ( int j = 0 ; j < min_time && temp < n && truck [ count ] >= box [ temp ] ; j += 2 ) temp ++ ; count ++ ; } if ( temp == n ) return true ; return false ; } static int minTime ( int box [ ] , int truck [ ] , int n , int m ) { Arrays . sort ( box ) ; Arrays . sort ( truck ) ; int l = 0 ; int h = 2 * n ; int min_time = 0 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( isPossible ( box , truck , n , m , mid ) ) { min_time = mid ; h = mid - 1 ; } else l = mid + 1 ; } return min_time ; } public static void main ( String [ ] args ) { int box [ ] = { 10 , 2 , 16 , 19 } ; int truck [ ] = { 29 , 25 } ; int n = box . length ; int m = truck . length ; System . out . printf ( " % d " , minTime ( box , truck , n , m ) ) ; } }
class GFG { static void solve ( int interval [ ] [ ] , int N , int Q ) { int Mark [ ] = new int [ Q ] ; for ( int i = 0 ; i < N ; i ++ ) { int l = interval [ i ] [ 0 ] - 1 ; int r = interval [ i ] [ 1 ] - 1 ; for ( int j = l ; j <= r ; j ++ ) Mark [ j ] ++ ; } int count = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( Mark [ i ] != 0 ) count ++ ; } int count1 [ ] = new int [ Q ] ; if ( Mark [ 0 ] == 1 ) count1 [ 0 ] = 1 ; for ( int i = 1 ; i < Q ; i ++ ) { if ( Mark [ i ] == 1 ) count1 [ i ] = count1 [ i - 1 ] + 1 ; else count1 [ i ] = count1 [ i - 1 ] ; } int maxindex = 0 ; int maxcoverage = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int l = interval [ i ] [ 0 ] - 1 ; int r = interval [ i ] [ 1 ] - 1 ; int elem1 ; if ( l != 0 ) elem1 = count1 [ r ] - count1 [ l - 1 ] ; else elem1 = count1 [ r ] ; if ( count - elem1 >= maxcoverage ) { maxcoverage = count - elem1 ; maxindex = i ; } } System . out . println ( " Maximum ▁ Coverage ▁ is ▁ " + maxcoverage + " ▁ after ▁ removing ▁ interval ▁ at ▁ index ▁ " + maxindex ) ; } public static void main ( String [ ] args ) { int interval [ ] [ ] = { { 1 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 3 , 5 } } ; int N = interval . length ; int Q = 7 ; solve ( interval , N , Q ) ; } }
class GFG { static int getKthElement ( int n , int k , int L [ ] , int R [ ] ) { int l = 1 ; int h = n ; int total [ ] = new int [ n + 1 ] ; total [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total [ i + 1 ] = total [ i ] + ( R [ i ] - L [ i ] ) + 1 ; } int index = - 1 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( total [ m ] > k ) { index = m ; h = m - 1 ; } else if ( total [ m ] < k ) l = m + 1 ; else { index = m ; break ; } } l = L [ index - 1 ] ; h = R [ index - 1 ] ; int x = k - total [ index - 1 ] ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( m - L [ index - 1 ] ) + 1 == x ) { return m ; } else if ( ( m - L [ index - 1 ] ) + 1 > x ) h = m - 1 ; else l = m + 1 ; } return k ; } public static void main ( String [ ] args ) { int L [ ] = { 1 , 8 , 21 } ; int R [ ] = { 4 , 10 , 23 } ; int n = L . length ; int k = 6 ; System . out . println ( getKthElement ( n , k , L , R ) ) ; } }
class GFG { static int MinimumX ( int a , int b , int c , int k ) { int x = Integer . MAX_VALUE ; if ( k <= c ) return 0 ; int h = k - c ; int l = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } public static void main ( String [ ] args ) { int a = 3 , b = 2 , c = 4 , k = 15 ; System . out . println ( MinimumX ( a , b , c , k ) ) ; } }
import java . util . * ; class GFG { static void IfExists ( int arr [ ] , int n ) { int sum [ ] = new int [ n ] ; Arrays . sort ( arr ) ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; int max = arr [ n - 1 ] ; boolean flag = false ; for ( int i = 1 ; i <= max ; i ++ ) { int findex = 0 ; int lindex = 0 ; int l = 0 ; int r = n - 1 ; while ( l <= r ) { int m = ( l + r ) / 2 ; if ( arr [ m ] < i ) { findex = m ; l = m + 1 ; } else r = m - 1 ; } l = 1 ; r = n ; flag = false ; while ( l <= r ) { int m = ( r + l ) / 2 ; if ( arr [ m ] > i ) { lindex = m ; r = m - 1 ; } else l = m + 1 ; } if ( sum [ findex ] == sum [ n - 1 ] - sum [ lindex - 1 ] ) { flag = true ; break ; } } if ( flag ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 2 , 5 } ; int n = arr . length ; IfExists ( arr , n ) ; } }
class GFG { public static int findMissing ( int arr [ ] , int n ) { int l = 0 , h = n - 1 ; int mid ; while ( h > l ) { mid = l + ( h - l ) / 2 ; if ( arr [ mid ] - mid == arr [ 0 ] ) { if ( arr [ mid + 1 ] - arr [ mid ] > 1 ) return arr [ mid ] + 1 ; else { l = mid + 1 ; } } else { if ( arr [ mid ] - arr [ mid - 1 ] > 1 ) return arr [ mid ] - 1 ; else { h = mid - 1 ; } } } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { - 9 , - 8 , - 7 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 } ; int n = arr . length ; System . out . print ( findMissing ( arr , n ) ) ; } }
class GFG { static int maxSum ( int arr [ ] , int n , int K ) { int maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int sumk = 0 ; for ( int j = i ; j < n ; j += K ) sumk = sumk + arr [ j ] ; maximum = Math . max ( maximum , sumk ) ; } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . length ; int K = 2 ; System . out . println ( maxSum ( arr , n , K ) ) ; } }
class GFG { static int countGreater ( int arr [ ] , int n , int k ) { int l = 0 ; int r = n - 1 ; int leftGreater = n ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] > k ) { leftGreater = m ; r = m - 1 ; } else l = m + 1 ; } return ( n - leftGreater ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 4 , 7 , 7 , 7 , 11 , 13 , 13 } ; int n = arr . length ; int k = 7 ; System . out . println ( countGreater ( arr , n , k ) ) ; } }
class GFG { static int operations ( int op [ ] , int n , int k ) { int i , count = 0 ; int nVal = 0 ; int min = Integer . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { nVal += op [ i ] ; min = Math . min ( min , nVal ) ; if ( ( k + nVal ) <= 0 ) return ( i + 1 ) ; } if ( nVal >= 0 ) return - 1 ; int times = ( k - Math . abs ( min ) ) / Math . abs ( nVal ) ; k = ( k - ( times * Math . abs ( nVal ) ) ) ; count = ( times * n ) ; while ( k > 0 ) { for ( i = 0 ; i < n ; i ++ ) { k = k + op [ i ] ; count ++ ; if ( k <= 0 ) break ; } } return count ; } public static void main ( String [ ] args ) { int op [ ] = { - 60 , 65 , - 1 , 14 , - 25 } ; int n = op . length ; int k = 100000 ; System . out . print ( operations ( op , n , k ) ) ; } }
class GFG { static int MAX_SIZE = 1000 ; static int lookup_table [ ] = new int [ MAX_SIZE ] ; static void create_table ( int n ) { int pow = 1 ; int co = 0 ; do { pow <<= 1 ; lookup_table [ co ] = ( n + ( pow >> 1 ) ) / pow ; } while ( lookup_table [ co ++ ] != 0 ) ; } static int binary ( int arr [ ] , int v ) { int index = lookup_table [ 0 ] - 1 ; int co = 0 ; while ( lookup_table [ co ] != 0 ) { if ( v == arr [ index ] ) return index ; else if ( v < arr [ index ] ) { index -= lookup_table [ ++ co ] ; return index ; } else { index += lookup_table [ ++ co ] ; return index ; } } return index ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; create_table ( n ) ; System . out . println ( " Position ▁ of ▁ 3 ▁ in ▁ array ▁ = ▁ " + binary ( arr , 3 ) ) ; } }
class GFG { static long squareSum ( long N ) { long sum = ( long ) ( N * ( N + 1 ) * ( 2 * N + 1 ) ) / 6 ; return sum ; } static long findMaxN ( long X ) { long low = 1 , high = 100000 ; int N = 0 ; while ( low <= high ) { long mid = ( high + low ) / 2 ; if ( squareSum ( mid ) <= X ) { N = ( int ) mid ; low = mid + 1 ; } else high = mid - 1 ; } return N ; } public static void main ( String [ ] args ) { long X = 25 ; System . out . println ( findMaxN ( X ) ) ; } }
class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair a [ ] , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( K >= a [ i ] . first && K <= a [ i ] . second ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { pair a [ ] = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) System . out . println ( index ) ; else System . out . println ( - 1 ) ; } }
class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findNumber ( pair a [ ] , int n , int K ) { int low = 0 , high = n - 1 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( K >= a [ mid ] . first && K <= a [ mid ] . second ) return mid ; else if ( K < a [ mid ] . first ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { pair a [ ] = { new pair ( 1 , 3 ) , new pair ( 4 , 7 ) , new pair ( 8 , 11 ) } ; int n = a . length ; int k = 6 ; int index = findNumber ( a , n , k ) ; if ( index != - 1 ) System . out . println ( index ) ; else System . out . println ( - 1 ) ; } }
import java . util . * ; class GFG { static void prefixSum ( int [ ] p_arr , int [ ] arr , int n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } static void qPrefixSum ( int [ ] p_arr , int n , HashSet < Integer > q ) { for ( int i = 0 ; i < n ; i ++ ) q . add ( p_arr [ i ] ) ; } static void canDivide ( int [ ] p_arr , int n , HashSet < Integer > q , int l , int r ) { int sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { System . out . println ( " No " ) ; return ; } int beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . contains ( beg + sum / 2 ) && ( beg + sum / 2 ) != ( int ) q . toArray ( ) [ q . size ( ) - 1 ] ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 } ; int n = arr . length ; int p_arr [ ] = new int [ n ] ; prefixSum ( p_arr , arr , n ) ; HashSet < Integer > q = new HashSet < > ( ) ; qPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ; } }
class GFG { final static int n = 4 ; static int findRing ( int arr [ ] [ ] , int x ) { if ( arr [ 0 ] [ 0 ] > x ) return - 1 ; int l = 0 , r = ( n + 1 ) / 2 - 1 ; if ( n % 2 == 1 && arr [ r ] [ r ] < x ) return - 1 ; if ( n % 2 == 0 && arr [ r + 1 ] [ r ] < x ) return - 1 ; while ( l < r ) { int mid = ( l + r ) / 2 ; if ( arr [ mid ] [ mid ] <= x ) if ( mid == ( n + 1 ) / 2 - 1 arr [ mid + 1 ] [ mid + 1 ] > x ) return mid ; else l = mid + 1 ; else r = mid - 1 ; } return r ; } static int binarySearchRowInc ( int arr [ ] [ ] , int row , int l , int r , int x ) { while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( arr [ row ] [ mid ] == x ) return mid ; if ( arr [ row ] [ mid ] < x ) l = mid + 1 ; else r = mid - 1 ; } return - 1 ; } static int binarySearchColumnInc ( int arr [ ] [ ] , int col , int t , int b , int x ) { while ( t <= b ) { int mid = ( t + b ) / 2 ; if ( arr [ mid ] [ col ] == x ) return mid ; if ( arr [ mid ] [ col ] < x ) t = mid + 1 ; else b = mid - 1 ; } return - 1 ; } static int binarySearchRowDec ( int arr [ ] [ ] , int row , int l , int r , int x ) { while ( l <= r ) { int mid = ( l + r ) / 2 ; if ( arr [ row ] [ mid ] == x ) return mid ; if ( arr [ row ] [ mid ] < x ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } static int binarySearchColumnDec ( int arr [ ] [ ] , int col , int t , int b , int x ) { while ( t <= b ) { int mid = ( t + b ) / 2 ; if ( arr [ mid ] [ col ] == x ) return mid ; if ( arr [ mid ] [ col ] < x ) b = mid - 1 ; else t = mid + 1 ; } return - 1 ; } static void spiralBinary ( int arr [ ] [ ] , int x ) { int f1 = findRing ( arr , x ) ; int r , c ; if ( f1 == - 1 ) { System . out . print ( " - 1" ) ; return ; } if ( n % 2 == 1 && f1 == ( n + 1 ) / 2 - 1 ) { System . out . println ( f1 + " ▁ " + f1 ) ; return ; } if ( x < arr [ f1 ] [ n - f1 - 1 ] ) { c = binarySearchRowInc ( arr , f1 , f1 , n - f1 - 2 , x ) ; r = f1 ; } else if ( x < arr [ n - f1 - 1 ] [ n - f1 - 1 ] ) { c = n - f1 - 1 ; r = binarySearchColumnInc ( arr , n - f1 - 1 , f1 , n - f1 - 2 , x ) ; } else if ( x < arr [ n - f1 - 1 ] [ f1 ] ) { c = binarySearchRowDec ( arr , n - f1 - 1 , f1 + 1 , n - f1 - 1 , x ) ; r = n - f1 - 1 ; } else { r = binarySearchColumnDec ( arr , f1 , f1 + 1 , n - f1 - 1 , x ) ; c = f1 ; } if ( c == - 1 r == - 1 ) System . out . print ( " - 1" ) ; else System . out . print ( r + " ▁ " + c ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 12 , 13 , 14 , 5 } , { 11 , 16 , 15 , 6 } , { 10 , 9 , 8 , 7 } } ; spiralBinary ( arr , 7 ) ; } }
import java . util . * ; class solution { static int countSubArrays ( int arr [ ] , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = 0 ; for ( int k = i ; k <= j ; k ++ ) { bitwise_or = bitwise_or | arr [ k ] ; } if ( bitwise_or >= K ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; int k = 6 ; System . out . println ( countSubArrays ( arr , n , k ) ) ; } }
public class Main { static int N = 100002 ; static int tree [ ] = new int [ 4 * N ] ; static void build ( int arr [ ] , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int arr [ ] , int n , int K ) { build ( arr , 1 , 0 , n - 1 ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int bitwise_or = query ( 1 , 0 , n - 1 , i , j ) ; if ( bitwise_or >= K ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; int k = 6 ; System . out . print ( countSubArrays ( arr , n , k ) ) ; } }
class GFG { static int N = 100002 ; static int tree [ ] = new int [ 4 * N ] ; static void build ( int [ ] arr , int node , int start , int end ) { if ( start == end ) { tree [ node ] = arr [ start ] ; return ; } int mid = ( start + end ) >> 1 ; build ( arr , 2 * node , start , mid ) ; build ( arr , 2 * node + 1 , mid + 1 , end ) ; tree [ node ] = tree [ 2 * node ] | tree [ 2 * node + 1 ] ; } static int query ( int node , int start , int end , int l , int r ) { if ( start > end start > r end < l ) { return 0 ; } if ( start >= l && end <= r ) { return tree [ node ] ; } int mid = ( start + end ) >> 1 ; int q1 = query ( 2 * node , start , mid , l , r ) ; int q2 = query ( 2 * node + 1 , mid + 1 , end , l , r ) ; return q1 | q2 ; } static int countSubArrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int low = i , high = n - 1 , index = Integer . MAX_VALUE ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( query ( 1 , 0 , n - 1 , i , mid ) >= K ) { index = Math . min ( index , mid ) ; high = mid - 1 ; } else { low = mid + 1 ; } } if ( index != Integer . MAX_VALUE ) { count += n - index ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; build ( arr , 1 , 0 , n - 1 ) ; int k = 6 ; System . out . println ( countSubArrays ( arr , n , k ) ) ; } }
import java . util . * ; class solution { static int countPattern ( int n , String pat ) { int pattern_int = 0 ; int power_two = 1 ; int all_ones = 0 ; for ( int i = pat . length ( ) - 1 ; i >= 0 ; i -- ) { int current_bit = pat . charAt ( i ) - '0' ; pattern_int += ( power_two * current_bit ) ; all_ones = all_ones + power_two ; power_two = power_two * 2 ; } int count = 0 ; while ( n != 0 && n >= pattern_int ) { if ( ( n & all_ones ) == pattern_int ) { count ++ ; } n = n >> 1 ; } return count ; } public static void main ( String args [ ] ) { int n = 500 ; String pat = "10" ; System . out . println ( countPattern ( n , pat ) ) ; } }
import java . util . * ; class GFG { static int max_min ( int a [ ] , int n ) { Arrays . sort ( a ) ; return Math . min ( a [ n - 2 ] - a [ 0 ] , a [ n - 1 ] - a [ 1 ] ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 3 , 7 } ; int n = a . length ; System . out . println ( max_min ( a , n ) ) ; } }
public class GFG { static final int MAX = 1000001 ; static int primeUpto [ ] = new int [ MAX ] ; static void SieveOfEratosthenes ( ) { int isPrime [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = 1 ; isPrime [ 0 ] = isPrime [ 1 ] = 0 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( isPrime [ i ] == 1 ) { for ( int j = i * 2 ; j < MAX ; j += i ) isPrime [ j ] = 0 ; } } for ( int i = 1 ; i < MAX ; i ++ ) { primeUpto [ i ] = primeUpto [ i - 1 ] ; if ( isPrime [ i ] == 1 ) primeUpto [ i ] ++ ; } } static int countOfNumbers ( int N , int K ) { SieveOfEratosthenes ( ) ; int low = 1 , high = N , ans = 0 ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( mid - primeUpto [ mid ] >= K ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } ans = ans != 0 ? N - ans + 1 : 0 ; return ans ; } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; System . out . println ( countOfNumbers ( N , K ) ) ; } }
import java . io . * ; class GFG { static int minIndex ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( 2 * arr [ i ] == sum ) return ( i + 1 ) ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , - 5 , 3 , 4 } ; int n = arr . length ; System . out . print ( minIndex ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static void printPrevGreater ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . println ( " - 1" ) ; return ; } int v [ ] = Arrays . copyOf ( arr , arr . length ) ; Arrays . sort ( v ) ; for ( int i = 0 ; i < n ; i ++ ) { int it = Arrays . binarySearch ( v , arr [ i ] ) ; it ++ ; if ( ( it - 1 ) != 0 && v [ it - 2 ] == arr [ i ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; } else if ( it != v . length ) System . out . print ( v [ it ] + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 11 , 10 , 20 , 12 } ; int n = arr . length ; printPrevGreater ( arr , n ) ; } }
public class GFG { static void diagonalsMinMax ( int [ ] [ ] mat ) { int n = mat . length ; if ( n == 0 ) return ; int principalMin = mat [ 0 ] [ 0 ] , principalMax = mat [ 0 ] [ 0 ] ; int secondaryMin = mat [ n - 1 ] [ 0 ] , secondaryMax = mat [ n - 1 ] [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( mat [ i ] [ i ] < principalMin ) { principalMin = mat [ i ] [ i ] ; } if ( mat [ i ] [ i ] > principalMax ) { principalMax = mat [ i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] < secondaryMin ) { secondaryMin = mat [ n - 1 - i ] [ i ] ; } if ( mat [ n - 1 - i ] [ i ] > secondaryMax ) { secondaryMax = mat [ n - 1 - i ] [ i ] ; } } System . out . println ( " Principal ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + principalMin ) ; System . out . println ( " Principal ▁ Diagonal ▁ Greatest ▁ Element ▁ : ▁ " + principalMax ) ; System . out . println ( " Secondary ▁ Diagonal ▁ Smallest ▁ Element : ▁ " + secondaryMin ) ; System . out . println ( " Secondary ▁ Diagonal ▁ Greatest ▁ Element : ▁ " + secondaryMax ) ; } static public void main ( String [ ] args ) { int [ ] [ ] matrix = { { 1 , 2 , 3 , 4 , - 10 } , { 5 , 6 , 7 , 8 , 6 } , { 1 , 2 , 11 , 3 , 4 } , { 5 , 6 , 70 , 5 , 8 } , { 4 , 9 , 7 , 1 , - 5 } } ; diagonalsMinMax ( matrix ) ; } }
class GFG { static boolean checkSorted ( int n , int arr [ ] ) { int first = 0 , second = 0 ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ i - 1 ] ) { count ++ ; if ( first == 0 ) first = i ; else second = i ; } } if ( count > 2 ) return false ; if ( count == 0 ) return true ; if ( count == 2 ) swap ( arr , first - 1 , second ) ; else if ( count == 1 ) swap ( arr , first - 1 , first ) ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] < arr [ i - 1 ] ) return false ; return true ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 2 } ; int n = arr . length ; if ( checkSorted ( n , arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int N = 4 ; static int M = 4 ; static void printIntersection ( int A [ ] [ ] , int B [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( A [ i ] [ j ] == B [ i ] [ j ] ) System . out . print ( A [ i ] [ j ] + " ▁ " ) ; else System . out . print ( " * ▁ " ) ; } System . out . println ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 2 , 4 , 6 , 8 } , { 1 , 3 , 5 , 7 } , { 8 , 6 , 4 , 2 } , { 7 , 5 , 3 , 1 } } ; int B [ ] [ ] = { { 2 , 3 , 6 , 8 } , { 1 , 3 , 5 , 2 } , { 8 , 1 , 4 , 2 } , { 3 , 5 , 4 , 1 } } ; printIntersection ( A , B ) ; } }
class GFG { static int countWays ( int [ ] arr , int n ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , arr [ i ] ) ; int [ ] freq = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( int i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( int i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( int i = 1 ; i <= max_val ; i ++ ) { for ( int j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countWays ( arr , n ) ) ; } }
import java . util . * ; class GfG { static int countPairs ( int A1 [ ] , int A2 [ ] , int n1 , int n2 , int K ) { int res = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { if ( m . containsKey ( A1 [ i ] ) ) m . put ( A1 [ i ] , m . get ( A1 [ i ] ) + 1 ) ; else m . put ( A1 [ i ] , 1 ) ; } for ( int i = 0 ; i < n2 ; ++ i ) { int temp = K - A2 [ i ] ; if ( m . containsKey ( temp ) && m . get ( temp ) != 0 ) { res ++ ; m . put ( temp , m . get ( A1 [ i ] ) - 1 ) ; } } return res ; } public static void main ( String [ ] args ) { int A1 [ ] = { 1 , 1 , 3 , 4 , 5 , 6 , 6 } ; int A2 [ ] = { 1 , 4 , 4 , 5 , 7 } , K = 10 ; int n1 = A1 . length ; int n2 = A2 . length ; System . out . println ( countPairs ( A1 , A2 , n1 , n2 , K ) ) ; } }
import java . util . HashMap ; import java . util . Map ; class GFG { public static int countOrderedPairs ( int [ ] A , int n ) { int orderedPairs = 0 ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . get ( A [ i ] ) == null ) m . put ( A [ i ] , 1 ) ; else { int a = m . get ( A [ i ] ) ; m . put ( A [ i ] , ++ a ) ; } } for ( int entry : m . keySet ( ) ) { int X = entry ; int Y = m . get ( entry ) ; for ( int j = 1 ; j <= Y ; j ++ ) { if ( m . get ( j ) >= X ) orderedPairs ++ ; } } return orderedPairs ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 1 , 2 , 2 , 3 } ; int n = A . length ; System . out . print ( countOrderedPairs ( A , n ) ) ; } }
class GFG { static public int LongestSubarray ( int arr [ ] , int n , int k ) { int count = 1 ; int max_length = 1 ; int prev_mod = arr [ 0 ] % k ; for ( int i = 1 ; i < n ; i ++ ) { int curr_mod = arr [ i ] % k ; if ( curr_mod == prev_mod ) { count ++ ; } else { max_length = Math . max ( max_length , count ) ; count = 1 ; prev_mod = curr_mod ; } } return Math . max ( max_length , count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 9 , 7 , 18 , 29 , 11 } ; int n = arr . length ; int k = 11 ; System . out . print ( LongestSubarray ( arr , n , k ) ) ; } }
public class GFG { static final int M = 3 ; static final int N = 4 ; static boolean binarySearch1D ( int arr [ ] , int K ) { int low = 0 ; int high = N - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] == K ) { return true ; } if ( arr [ mid ] < K ) { low = mid + 1 ; } else { high = mid - 1 ; } } return false ; } static boolean searchMatrix ( int matrix [ ] [ ] , int K ) { int low = 0 ; int high = M - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( K >= matrix [ mid ] [ 0 ] && K <= matrix [ mid ] [ N - 1 ] ) { return binarySearch1D ( matrix [ mid ] , K ) ; } if ( K < matrix [ mid ] [ 0 ] ) { high = mid - 1 ; } else { low = mid + 1 ; } } return false ; } public static void main ( String args [ ] ) { int matrix [ ] [ ] = { { 1 , 3 , 5 , 7 } , { 10 , 11 , 16 , 20 } , { 23 , 30 , 34 , 50 } } ; int K = 3 ; if ( searchMatrix ( matrix , K ) ) { System . out . println ( " Found " ) ; } else { System . out . println ( " Not ▁ found " ) ; } } }
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int divTermCount ( int a , int b , int lcm , int num ) { return num / a + num / b - num / lcm ; } static int findNthTerm ( int a , int b , int n ) { int low = 1 , high = Integer . MAX_VALUE , mid ; int lcm = ( a * b ) / gcd ( a , b ) ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , lcm , mid ) < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , n = 10 ; System . out . println ( findNthTerm ( a , b , n ) ) ; } }
import java . util . * ; class Solution { static final int MAX = 100000 ; static class Node { int data ; Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static boolean check_pair_product ( Node head , int prod ) { Vector < Integer > s = new Vector < Integer > ( ) ; Node p = head ; while ( p != null ) { int curr = p . data ; if ( ( prod % curr == 0 ) && ( s . contains ( prod / curr ) ) ) { System . out . print ( curr + " ▁ " + ( prod / curr ) ) ; return true ; } s . add ( p . data ) ; p = p . next ; } return false ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; head = push ( head , 12 ) ; head = push ( head , 1 ) ; head = push ( head , 18 ) ; head = push ( head , 47 ) ; head = push ( head , 16 ) ; head = push ( head , 12 ) ; head = push ( head , 14 ) ; boolean res = check_pair_product ( head , 24 ) ; if ( res == false ) System . out . println ( " NO ▁ PAIR ▁ EXIST " ) ; } }
import java . util . Arrays ; public class GFG { static void solve ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; if ( k == 1 && arr [ n - 2 ] != arr [ n - 1 ] ) { System . out . println ( arr [ n - 1 ] ) ; return ; } int count = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) count ++ ; else count = 1 ; if ( count == k && ( i == 0 || ( arr [ i - 1 ] != arr [ i ] ) ) ) { System . out . println ( arr [ i ] ) ; return ; } } System . out . println ( " No ▁ such ▁ element " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 2 ; int n = arr . length ; solve ( arr , n , k ) ; } }
import java . util . HashMap ; import java . util . Map ; class GfG { static void solve ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! m . containsKey ( arr [ i ] ) ) m . put ( arr [ i ] , 0 ) ; m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . get ( arr [ i ] ) == k && max == Integer . MIN_VALUE ) { max = arr [ i ] ; } else if ( m . get ( arr [ i ] ) == k && max < arr [ i ] ) { max = arr [ i ] ; } } if ( max == Integer . MIN_VALUE ) System . out . println ( " No ▁ such ▁ element " ) ; else System . out . println ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 4 ; int n = arr . length ; solve ( arr , n , k ) ; } }
import java . util . HashMap ; import java . util . Stack ; class GFG { public static void countEle ( Stack < Integer > s , int [ ] a , int N ) { HashMap < Integer , Boolean > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { int num = a [ i ] ; if ( mp . containsKey ( num ) ) System . out . print ( "0 ▁ " ) ; else { int cnt = 0 ; while ( s . peek ( ) != num ) { mp . put ( s . peek ( ) , true ) ; s . pop ( ) ; cnt ++ ; } s . pop ( ) ; cnt ++ ; System . out . print ( cnt + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 5 ; Stack < Integer > s = new Stack < > ( ) ; s . add ( 1 ) ; s . add ( 2 ) ; s . add ( 3 ) ; s . add ( 4 ) ; s . add ( 6 ) ; int [ ] a = { 6 , 3 , 4 , 1 , 2 } ; countEle ( s , a , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int N = 6 ; @ SuppressWarnings ( " unchecked " ) static HashSet < Integer > [ ] Tree = new HashSet [ 6 * N ] ; static { for ( int i = 0 ; i < 6 * N ; i ++ ) Tree [ i ] = new HashSet < > ( ) ; } static void buildTree ( int [ ] arr , int idx , int s , int e ) { if ( s == e ) { Tree [ idx ] . add ( arr [ s ] ) ; return ; } int mid = ( s + e ) / 2 ; buildTree ( arr , 2 * idx , s , mid ) ; buildTree ( arr , 2 * idx + 1 , mid + 1 , e ) ; for ( int it : Tree [ 2 * idx ] ) Tree [ idx ] . add ( it ) ; for ( int it : Tree [ 2 * idx + 1 ] ) Tree [ idx ] . add ( it ) ; } static boolean query ( int idx , int s , int e , int qs , int qe , int x ) { if ( qs <= s && e <= qe ) { if ( Collections . frequency ( Tree [ idx ] , x ) != 0 ) return true ; else return false ; } if ( qe < s e < qs ) return false ; int mid = ( s + e ) / 2 ; boolean LeftAns = query ( 2 * idx , s , mid , qs , qe , x ) ; boolean RightAns = query ( 2 * idx + 1 , mid + 1 , e , qs , qe , x ) ; return ( LeftAns RightAns ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 3 , 9 , 8 , 7 } ; int n = arr . length ; buildTree ( arr , 1 , 0 , n - 1 ) ; int l , r , x ; l = 0 ; r = 3 ; x = 2 ; if ( query ( 1 , 0 , n - 1 , l , r , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; l = 2 ; r = 5 ; x = 3 ; if ( query ( 1 , 0 , n - 1 , l , r , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int countChar ( String str ) { int n = str . length ( ) ; if ( n <= 2 ) return n ; int count = 2 ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( str . charAt ( i - 1 ) == str . charAt ( i + 1 ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { String str = " egeeksk " ; System . out . println ( countChar ( str ) ) ; } }
class GfG { private static int next ( int [ ] arr , int target ) { int start = 0 , end = arr . length - 1 ; if ( end == 0 ) return - 1 ; if ( target > arr [ end ] ) return end ; int ans = - 1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] >= target ) { end = mid - 1 ; } else { ans = mid ; start = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 5 , 8 , 12 } ; System . out . println ( next ( arr , 5 ) ) ; } }
class GfG { private static int next ( int [ ] arr , int target ) { int start = 0 , end = arr . length - 1 ; int ans = - 1 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( arr [ mid ] <= target ) { start = mid + 1 ; } else { ans = mid ; end = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 5 , 8 , 12 } ; System . out . println ( next ( arr , 8 ) ) ; } }
class Test { private int josephus ( int n , int k ) { int sum = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { sum = ( sum + k ) % i ; } return sum + 1 ; } public static void main ( String [ ] args ) { int n = 14 ; int k = 2 ; Test obj = new Test ( ) ; System . out . println ( obj . josephus ( n , k ) ) ; } }
import java . util . * ; class solution { static boolean search ( int [ ] arr , int n , int x ) { int res = - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res = i ; if ( res == - 1 ) return false ; int temp = arr [ res ] ; for ( int i = res ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = temp ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 12 , 25 , 36 , 85 , 98 , 75 , 89 , 15 , 63 , 66 , 64 , 74 , 27 , 83 , 97 } ; int [ ] q = { 63 , 63 , 86 , 63 , 78 } ; int n = arr . length ; int m = q . length ; for ( int i = 0 ; i < m ; i ++ ) { if ( search ( arr , n , q [ i ] ) == true ) System . out . print ( " Yes ▁ " ) ; else System . out . print ( " No ▁ " ) ; } } }
class GFG { static float kPresentProbability ( int a [ ] , int n , int k ) { float count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == k ) count ++ ; return count / n ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 7 , 2 , 0 , 8 , 7 , 5 } ; int K = 2 ; int N = A . length ; double n = kPresentProbability ( A , N , K ) ; double p = ( double ) Math . round ( n * 100 ) / 100 ; System . out . println ( p ) ; } }
import java . io . * ; class GFG { static int slopeOfNum ( String num , int n ) { int slope = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( num . charAt ( i ) > num . charAt ( i - 1 ) && num . charAt ( i ) > num . charAt ( i + 1 ) ) slope ++ ; else if ( num . charAt ( i ) < num . charAt ( i - 1 ) && num . charAt ( i ) < num . charAt ( i + 1 ) ) slope ++ ; } return slope ; } public static void main ( String [ ] args ) { String num = "1213321" ; int n = num . length ( ) ; System . out . println ( " Slope ▁ = ▁ " + slopeOfNum ( num , n ) ) ; } }
import java . util . * ; class GFG { static boolean pairExists ( int [ ] arr1 , int m , int [ ] arr2 , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( s . contains ( arr2 [ j ] - arr1 [ i ] ) ) return true ; } s . add ( arr1 [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 5 , 10 , 8 } ; int [ ] arr2 = { 2 , 20 , 13 } ; if ( pairExists ( arr1 , 4 , arr2 , 3 ) ) System . out . println ( 1 ) ; else System . out . println ( 0 ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void prevGreater ( int arr [ ] , int n ) { System . out . print ( " - 1 , ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { int j ; for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] < arr [ j ] ) { System . out . print ( arr [ j ] + " , ▁ " ) ; break ; } } if ( j == - 1 ) System . out . print ( " - 1 , ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 4 , 2 , 20 , 40 , 12 , 30 } ; int n = arr . length ; prevGreater ( arr , n ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void prevGreater ( int arr [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( arr [ 0 ] ) ; System . out . print ( " - 1 , ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( s . empty ( ) == false && s . peek ( ) < arr [ i ] ) s . pop ( ) ; if ( s . empty ( ) == true ) System . out . print ( " - 1 , ▁ " ) ; else System . out . print ( s . peek ( ) + " , ▁ " ) ; s . push ( arr [ i ] ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 4 , 2 , 20 , 40 , 12 , 30 } ; int n = arr . length ; prevGreater ( arr , n ) ; } }
import java . io . * ; class GFG { static void printDuplicates ( int arr [ ] , int n ) { int i ; int fl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ arr [ i ] % n ] >= n ) { if ( arr [ arr [ i ] % n ] < 2 * n ) { System . out . print ( arr [ i ] % n + " ▁ " ) ; fl = 1 ; } } arr [ arr [ i ] % n ] += n ; } if ( ! ( fl > 0 ) ) System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 1 , 3 , 6 , 6 } ; int arr_size = arr . length ; printDuplicates ( arr , arr_size ) ; } }
import java . io . * ; class GFG { static int findMissingNo ( int [ ] arr , int n ) { int val ; int nextval ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= 0 arr [ i ] > n ) continue ; val = arr [ i ] ; while ( arr [ val - 1 ] != val ) { nextval = arr [ val - 1 ] ; arr [ val - 1 ] = val ; val = nextval ; if ( val <= 0 val > n ) break ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 } ; int arr_size = arr . length ; int missing = findMissingNo ( arr , arr_size ) ; System . out . println ( " The ▁ smallest ▁ positive " + " ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }
import java . io . * ; class GFG { static void findTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] == sum ) { System . out . println ( arr [ i ] + " ▁ " + arr [ j ] + " ▁ " + arr [ k ] ) ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int n = arr . length ; findTriplets ( arr , n , - 2 ) ; } }
import java . util . * ; class GFG { static void findTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . contains ( x ) ) System . out . printf ( "%d %d %dNEW_LINE", x, arr[i], arr[j]); else s . add ( arr [ j ] ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . length ; findTriplets ( arr , n , sum ) ; } }
import java . io . * ; class GFG { static int maxProduct ( int arr [ ] , int n ) { if ( n < 4 ) return - 1 ; int max_product = Integer . MIN_VALUE ; for ( int i = 0 ; i < n - 3 ; i ++ ) for ( int j = i + 1 ; j < n - 2 ; j ++ ) for ( int k = j + 1 ; k < n - 1 ; k ++ ) for ( int l = k + 1 ; l < n ; l ++ ) max_product = Math . max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] * arr [ l ] ) ; return max_product ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 3 , 5 , 6 , 20 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . println ( " No ▁ Quadruple ▁ Exists " ) ; else System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int maxProduct ( int arr [ ] , int n ) { if ( n < 4 ) return - 1 ; Arrays . sort ( arr ) ; int x = arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] * arr [ n - 4 ] ; int y = arr [ 0 ] * arr [ 1 ] * arr [ 2 ] * arr [ 3 ] ; int z = arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] * arr [ n - 2 ] ; return Math . max ( x , Math . max ( y , z ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 3 , 5 , 6 , - 20 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . println ( " No ▁ Quadruple ▁ Exists " ) ; else System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { if ( n < 2 ) return - 1 ; int ans = arr [ 0 ] + arr [ 1 ] ; for ( int i = 1 ; i + 1 < n ; i ++ ) ans = Math . min ( ans , ( arr [ i ] + arr [ i + 1 ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 12 , 2 , 2 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int costToBalance ( String s ) { if ( s . length ( ) == 0 ) System . out . println ( 0 ) ; int ans = 0 ; int o = 0 , c = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) o ++ ; if ( s . charAt ( i ) == ' ) ' ) c ++ ; } if ( o != c ) return - 1 ; int [ ] a = new int [ s . length ( ) ] ; if ( s . charAt ( 0 ) == ' ( ' ) a [ 0 ] = 1 ; else a [ 0 ] = - 1 ; if ( a [ 0 ] < 0 ) ans += Math . abs ( a [ 0 ] ) ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) a [ i ] = a [ i - 1 ] + 1 ; else a [ i ] = a [ i - 1 ] - 1 ; if ( a [ i ] < 0 ) ans += Math . abs ( a [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { String s ; s = " ) ) ) ( ( ( " ; System . out . println ( costToBalance ( s ) ) ; s = " ) ) ( ( " ; System . out . println ( costToBalance ( s ) ) ; } }
import java . io . * ; class GFG { static int f ( int n ) { return n * ( 2 * n * n - 1 ) ; } static boolean binarySearch ( int low , int high , int x ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( f ( mid ) < x ) low = mid + 1 ; else if ( f ( mid ) > x ) high = mid - 1 ; else return true ; } return false ; } static boolean isStellaOctangula ( int x ) { if ( x == 0 ) return true ; int i = 1 ; while ( f ( i ) < x ) i = i * 2 ; if ( f ( i ) == x ) return true ; return binarySearch ( i / 2 , i , x ) ; } public static void main ( String [ ] args ) { int n = 51 ; if ( isStellaOctangula ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static String sortString ( String str , int n ) { int i ; char [ ] arr = new char [ 26 ] ; String new_str = " " ; for ( i = 0 ; i < n ; i ++ ) ++ arr [ str . charAt ( i ) - ' a ' ] ; for ( i = 0 ; i < 26 ; i ++ ) while ( arr [ i ] -- > 0 ) { new_str += String . valueOf ( ( char ) ( i + ' a ' ) ) ; } return new_str ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; System . out . print ( sortString ( str , n ) ) ; } }
import java . util . * ; class GFG { static class Node { char data ; Node next ; } ; static Node head ; static char maxChar ( Node head ) { Node p = head ; int [ ] hash = new int [ 256 ] ; while ( p != null ) { hash [ p . data ] ++ ; p = p . next ; } p = head ; int max = - 1 ; char res = 0 ; while ( p != null ) { if ( max < hash [ p . data ] ) { res = p . data ; max = hash [ p . data ] ; } p = p . next ; } return res ; } static void push ( Node head_ref , char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } public static void main ( String [ ] args ) { head = null ; char str [ ] = " skeegforskeeg " . toCharArray ( ) ; for ( int i = 0 ; i < str . length ; i ++ ) { push ( head , str [ i ] ) ; } System . out . println ( maxChar ( head ) ) ; } }
class GFG { static final int N = 3 ; static int getGreatestSum ( int a [ ] [ ] ) { int prev_max = 0 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max < a [ N - 1 ] [ j ] ) prev_max = a [ N - 1 ] [ j ] ; int sum = prev_max ; for ( int i = N - 2 ; i >= 0 ; i -- ) { int curr_max = - 2147483648 ; for ( int j = 0 ; j < N ; j ++ ) if ( prev_max > a [ i ] [ j ] && a [ i ] [ j ] > curr_max ) curr_max = a [ i ] [ j ] ; if ( curr_max == - 2147483648 ) return - 1 ; prev_max = curr_max ; sum += prev_max ; } return sum ; } public static void main ( String arg [ ] ) { int a [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; System . out . println ( getGreatestSum ( a ) ) ; int b [ ] [ ] = { { 4 , 5 , 6 } , { 4 , 5 , 6 } , { 4 , 5 , 6 } } ; System . out . println ( getGreatestSum ( b ) ) ; } }
class GFG { static void findElement ( int n , int k ) { int ans = n ; int left = 1 ; int right = ( int ) ( Math . pow ( 2 , n ) - 1 ) ; while ( true ) { int mid = ( left + right ) / 2 ; if ( k == mid ) { System . out . println ( ans ) ; break ; } ans -- ; if ( k < mid ) { right = mid - 1 ; } else { left = mid + 1 ; } } } public static void main ( String [ ] args ) { int n = 4 , k = 8 ; findElement ( n , k ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int firstCommon ( Node head1 , Node head2 ) { for ( ; head1 != null ; head1 = head1 . next ) for ( Node p = head2 ; p != null ; p = p . next ) if ( p . data == head1 . data ) return head1 . data ; return 0 ; } public static void main ( String [ ] args ) { Node head1 = null ; head1 = push ( head1 , 20 ) ; head1 = push ( head1 , 5 ) ; head1 = push ( head1 , 15 ) ; head1 = push ( head1 , 10 ) ; Node head2 = null ; head2 = push ( head2 , 10 ) ; head2 = push ( head2 , 2 ) ; head2 = push ( head2 , 15 ) ; head2 = push ( head2 , 8 ) ; System . out . println ( firstCommon ( head1 , head2 ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int checkBit ( int pattern , int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int arr [ ] , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } if ( res == 0 ) System . out . println ( " Not ▁ Possible " ) ; else { System . out . print ( " Pair ▁ = ▁ " ) ; count = 0 ; for ( int i = 0 ; i < n && count < 2 ; i ++ ) { if ( ( arr [ i ] & res ) == res ) { count ++ ; System . out . print ( arr [ i ] + " ▁ " ) ; } } System . out . println ( ) ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = arr . length ; System . out . println ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }
import java . util . Scanner ; import java . io . * ; class GFG { static int countMaxSumPairs ( int a [ ] , int n ) { int first = Integer . MIN_VALUE , second = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > first ) { second = first ; first = a [ i ] ; } else if ( a [ i ] > second && a [ i ] != first ) second = a [ i ] ; } int cnt1 = 0 , cnt2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == first ) cnt1 ++ ; if ( a [ i ] == second ) cnt2 ++ ; } if ( cnt1 == 1 ) return cnt2 ; if ( cnt1 > 1 ) return cnt1 * ( cnt1 - 1 ) / 2 ; return 0 ; } static float findMaxSumProbability ( int a [ ] , int n ) { int total = n * ( n - 1 ) / 2 ; int max_sum_pairs = countMaxSumPairs ( a , n ) ; return ( float ) max_sum_pairs / ( float ) total ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 3 } ; int n = a . length ; ; System . out . println ( findMaxSumProbability ( a , n ) ) ; } }
import java . util . * ; class Middle { public static int middleOfThree ( int a , int b , int c ) { int x = a - b ; int y = b - c ; int z = a - c ; if ( x * y > 0 ) return b ; else if ( x * z > 0 ) return c ; else return a ; } public static void main ( String [ ] args ) { int a = 20 , b = 30 , c = 40 ; System . out . println ( middleOfThree ( a , b , c ) ) ; } }
int sum [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) dp [ 1 ] [ i ] = sum [ i ] ; and using it to calculate the result as : best = Math . min ( best , Math . max ( dp [ i - 1 ] [ p ] , sum [ j ] - sum [ p ] ) ) ;
for ( int i = k - 1 ; i <= n ; i ++ ) best = Math . min ( best , Math . max ( partition ( arr , i , k - 1 ) , sum ( arr , i , n - 1 ) ) ) ;
class GFG { static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n ; if ( sum == s ) return n ; } return - 1 ; } public static void main ( String [ ] args ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) System . out . println ( " - 1" ) ; else System . out . println ( n ) ; } }
import java . util . * ; class GFG { public static int isvalid ( int s ) { double k = ( - 1.0 + Math . sqrt ( 1 + 8 * s ) ) / 2 ; if ( Math . ceil ( k ) == Math . floor ( k ) ) return ( int ) k ; else return - 1 ; } public static void main ( String [ ] args ) { int s = 15 ; System . out . print ( isvalid ( s ) ) ; } }
class GFG { static int calcSafe ( int pos ) { int j = pos % 10 ; int i = pos / 10 ; int dis_11 = Math . min ( Math . abs ( 1 - i ) , Math . abs ( 1 - j ) ) ; int dis_18 = Math . min ( Math . abs ( 1 - i ) , Math . abs ( 8 - j ) ) ; int dis_81 = Math . min ( Math . abs ( 8 - i ) , Math . abs ( 1 - j ) ) ; int dis_88 = Math . min ( Math . abs ( 8 - i ) , Math . abs ( 8 - j ) ) ; int sum = dis_11 + dis_18 + dis_81 + dis_88 + 1 ; return ( 64 - sum ) ; } public static void main ( String [ ] args ) { int pos = 34 ; System . out . print ( " Safe ▁ Positions ▁ = ▁ " + calcSafe ( pos ) ) ; } }
import java . util . * ; class GFG { static int count_crossline ; static void merge ( int arr [ ] , int l , int m , int r ) { int i , j , k ; int n1 = m - l + 1 ; int n2 = r - m ; int [ ] L = new int [ n1 ] ; int [ ] R = new int [ n2 ] ; for ( i = 0 ; i < n1 ; i ++ ) { L [ i ] = arr [ l + i ] ; } for ( j = 0 ; j < n2 ; j ++ ) { R [ j ] = arr [ m + 1 + j ] ; } while ( i < n1 && j < n2 ) { if ( L [ i ] <= R [ j ] ) { arr [ k ] = L [ i ] ; i ++ ; } else { arr [ k ] = R [ j ] ; count_crossline += ( n1 - i ) ; j ++ ; } k ++ ; } while ( i < n1 ) { arr [ k ] = L [ i ] ; i ++ ; k ++ ; } while ( j < n2 ) { arr [ k ] = R [ j ] ; j ++ ; k ++ ; } } static void mergeSort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static int countCrossLine ( int arr [ ] , int n ) { mergeSort ( arr , 0 , n - 1 ) ; return count_crossline ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( countCrossLine ( arr , n ) ) ; } }
class Missing4 { public static void missing4 ( int [ ] arr ) { int [ ] helper = new int [ 4 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = Math . abs ( arr [ i ] ) ; if ( temp <= arr . length ) arr [ temp - 1 ] *= ( - 1 ) ; else if ( temp > arr . length ) { if ( temp % arr . length != 0 ) helper [ temp % arr . length - 1 ] = - 1 ; else helper [ ( temp % arr . length ) + arr . length - 1 ] = - 1 ; } } for ( int i = 0 ; i < arr . length ; i ++ ) if ( arr [ i ] > 0 ) System . out . print ( i + 1 + " ▁ " ) ; for ( int i = 0 ; i < helper . length ; i ++ ) if ( helper [ i ] >= 0 ) System . out . print ( arr . length + i + 1 + " ▁ " ) ; return ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 3 , 12 , 5 , 10 , 8 , 4 , 9 } ; missing4 ( arr ) ; } }
import java . io . * ; class GFG { static int getCount ( int arr [ ] , int n , int num1 , int num2 ) { int i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; int j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 } ; int n = arr . length ; int num1 = 5 , num2 = 4 ; System . out . println ( getCount ( arr , n , num1 , num2 ) ) ; } }
import java . util . * ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int grid [ ] [ ] ) { if ( ROW == 0 COL == 0 ) return 0 ; Vector < Integer > vertical = new Vector < Integer > ( ) ; Vector < Integer > horizontal = new Vector < Integer > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . add ( i ) ; horizontal . add ( j ) ; } } } Collections . sort ( vertical ) ; Collections . sort ( horizontal ) ; int size = vertical . size ( ) / 2 ; int x = vertical . get ( size ) ; int y = horizontal . get ( size ) ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += Math . abs ( x - i ) + Math . abs ( y - j ) ; return distance ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; System . out . println ( minTotalDistance ( grid ) ) ; } }
class Test { static int sumOfDigit ( int K ) { int sod = 0 ; while ( K != 0 ) { sod += K % 10 ; K /= 10 ; } return sod ; } static int totalNumbersWithSpecificDifference ( int N , int diff ) { int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } public static void main ( String args [ ] ) { int N = 13 ; int diff = 2 ; System . out . println ( totalNumbersWithSpecificDifference ( N , diff ) ) ; } }
class Test { static int countBuildings ( int arr [ ] , int n ) { int count = 1 ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > curr_max arr [ i ] == curr_max ) { count ++ ; curr_max = arr [ i ] ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 4 , 8 , 2 , 9 } ; System . out . println ( countBuildings ( arr , arr . length ) ) ; } }
class GFG { static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . length ; System . out . println ( findExtra ( arr1 , arr2 , n ) ) ; } }
class GFG { static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . length ; System . out . println ( findExtra ( arr1 , arr2 , n ) ) ; } }
class GFG { public static int computeCost ( int arr [ ] , int N , int X ) { int cost = 0 ; for ( int i = 0 ; i < N ; i ++ ) cost += Math . abs ( arr [ i ] - X ) ; return cost ; } public static int minCostToMakeElementEqual ( int arr [ ] , int N ) { int low , high ; low = high = arr [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( low > arr [ i ] ) low = arr [ i ] ; if ( high < arr [ i ] ) high = arr [ i ] ; } while ( ( high - low ) > 2 ) { int mid1 = low + ( high - low ) / 3 ; int mid2 = high - ( high - low ) / 3 ; int cost1 = computeCost ( arr , N , mid1 ) ; int cost2 = computeCost ( arr , N , mid2 ) ; if ( cost1 < cost2 ) high = mid2 ; else low = mid1 ; } return computeCost ( arr , N , ( low + high ) / 2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 100 , 101 } ; int N = arr . length ; System . out . println ( minCostToMakeElementEqual ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void minJumps ( int [ ] w , int [ ] l , int n ) { if ( n == 1 ) { System . out . print ( 0 ) ; return ; } int ans = 0 ; HashMap < Integer , Integer > pos = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > jump = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Boolean > filled = new HashMap < Integer , Boolean > ( ) ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos . containsKey ( w [ i ] ) ) pos . put ( w [ i ] , i ) ; else pos . put ( w [ i ] , i ) ; if ( filled . containsKey ( w [ i ] ) ) filled . put ( i , true ) ; else filled . put ( i , true ) ; if ( jump . containsKey ( w [ i ] ) ) jump . put ( w [ i ] , l [ i ] ) ; else jump . put ( w [ i ] , l [ i ] ) ; a [ i ] = w [ i ] ; } Arrays . sort ( a ) ; for ( int curr = 1 ; curr < n ; curr ++ ) { int currElementPos = pos . get ( a [ curr ] ) ; int prevElementPos = pos . get ( a [ curr - 1 ] ) ; if ( currElementPos > prevElementPos ) continue ; while ( currElementPos <= prevElementPos || filled . containsKey ( currElementPos ) && filled . containsKey ( currElementPos ) ) { currElementPos += jump . get ( a [ curr ] ) ; ans ++ ; } if ( pos . containsKey ( a [ curr ] ) ) pos . put ( a [ curr ] , currElementPos ) ; else pos . put ( a [ curr ] , currElementPos ) ; if ( filled . containsKey ( currElementPos ) ) filled . put ( currElementPos , true ) ; else filled . put ( currElementPos , true ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int [ ] W = { 2 , 1 , 4 , 3 } ; int [ ] L = { 4 , 1 , 2 , 4 } ; int N = W . length ; minJumps ( W , L , N ) ; } }
import java . io . * ; class GFG { static void check ( int arr [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] >= ( i - 1 ) ) { int p = arr [ i - 1 ] - ( i - 1 ) ; arr [ i ] += p ; arr [ i - 1 ] -= p ; } } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] <= arr [ i - 1 ] ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 7 , 6 } ; int N = arr . length ; check ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int keyFunc ( int n ) { int rev = 0 ; while ( n > 0 ) { rev = rev << 1 ; if ( ( n & 1 ) == 1 ) rev = rev ^ 1 ; n = n >> 1 ; } return rev ; } static int [ ] [ ] getNew ( int arr [ ] ) { int ans [ ] [ ] = new int [ arr . length ] [ 2 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) ans [ i ] = new int [ ] { keyFunc ( arr [ i ] ) , arr [ i ] } ; return ans ; } static int [ ] getArr ( int [ ] [ ] arr ) { int ans [ ] = new int [ arr . length ] ; int idx = 0 ; for ( int i [ ] : arr ) ans [ idx ++ ] = i [ 1 ] ; return ans ; } static void sortArray ( int arr [ ] ) { int [ ] [ ] newArr = getNew ( arr ) ; Arrays . sort ( newArr , ( a , b ) -> { if ( Integer . compare ( a [ 0 ] , b [ 0 ] ) == 0 ) return Integer . compare ( a [ 1 ] , b [ 1 ] ) ; return Integer . compare ( a [ 0 ] , b [ 0 ] ) ; } ) ; arr = getArr ( newArr ) ; int n = arr . length ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( arr [ i ] + " , ▁ " ) ; System . out . print ( arr [ n - 1 ] + " ] " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 43 , 52 , 61 , 41 } ; sortArray ( arr ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void minimumSize ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; reverse ( arr ) ; if ( count ( arr , 0 ) == N ) System . out . println ( 0 ) ; else if ( K >= N ) System . out . println ( 1 ) ; else System . out . println ( arr [ K ] ) ; } static void reverse ( int [ ] a ) { int i , k , t ; int n = a . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } static int count ( int [ ] a , int n ) { int freq = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] == n ) freq ++ ; } return freq ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 2 } ; int K = 2 ; int N = arr . length ; minimumSize ( arr , N , K ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void isPossibleToSort ( int arr [ ] , int N ) { int idx = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == 1 && arr [ i - 1 ] == 1 ) { idx = i ; break ; } } if ( idx == - 1 ) { System . out . println ( " YES " ) ; return ; } for ( int i = idx + 1 ; i < N ; i ++ ) { if ( arr [ i ] == 0 && arr [ i - 1 ] == 0 ) { System . out . println ( " NO " ) ; return ; } } System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int N = arr . length ; isPossibleToSort ( arr , N ) ; } }
import java . util . * ; class GFG { static int maxConsecutiveRope ( int ropes [ ] , int N ) { int curSize = 0 ; Arrays . sort ( ropes ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ropes [ i ] <= curSize + 1 ) { curSize = curSize + ropes [ i ] ; } else break ; } return curSize ; } public static void main ( String [ ] args ) { int N = 5 ; int ropes [ ] = { 1 , 2 , 7 , 1 , 1 } ; System . out . println ( maxConsecutiveRope ( ropes , N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int maxRanges ( Integer arr [ ] [ ] , int N ) { Arrays . sort ( arr , ( a , b ) -> { if ( a [ 0 ] . equals ( b [ 0 ] ) ) return Integer . compare ( a [ 1 ] , b [ 1 ] ) ; return Integer . compare ( a [ 0 ] , b [ 0 ] ) ; } ) ; int count = 1 ; Integer prev [ ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { Integer last [ ] = arr [ i - 1 ] ; Integer current [ ] = arr [ i ] ; if ( last [ 0 ] == current [ 0 ] && last [ 1 ] == current [ 1 ] && current [ 1 ] == current [ 0 ] ) continue ; int start = Math . max ( prev [ 0 ] , current [ 0 ] - 1 ) ; int end = Math . max ( prev [ 1 ] , current [ 1 ] ) ; if ( end - start > 0 ) { prev [ 0 ] = 1 + start ; prev [ 1 ] = end ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { Integer range [ ] [ ] = { { 1 , 4 } , { 4 , 4 } , { 2 , 2 } , { 3 , 4 } , { 1 , 1 } } ; int N = range . length ; System . out . print ( maxRanges ( range , N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int i , start , end ; static boolean adjust ( String S ) { int N = S . length ( ) ; start = i - 1 ; while ( i < N && S . charAt ( i ) < S . charAt ( i - 1 ) ) { i ++ ; } end = i - 1 ; if ( start <= 0 && i >= N ) return true ; if ( start >= 1 && i <= N ) { return ( S . charAt ( end ) >= S . charAt ( start - 1 ) && S . charAt ( start ) <= S . charAt ( i ) ) ; } if ( start >= 1 ) { return S . charAt ( end ) >= S . charAt ( start - 1 ) ; } if ( i < N ) { return S . charAt ( start ) <= S . charAt ( i ) ; } return false ; } static void isPossible ( String S , int N ) { start = - 1 ; end = - 1 ; boolean flag = true ; for ( i = 1 ; i < N ; i ++ ) { if ( S . charAt ( i ) < S . charAt ( i - 1 ) ) { if ( flag ) { if ( adjust ( S ) == false ) { System . out . println ( - 1 ) ; return ; } flag = false ; } else { System . out . println ( - 1 ) ; return ; } } } if ( start == - 1 ) { start = end = 1 ; } System . out . println ( start + " ▁ " + end ) ; } public static void main ( String [ ] args ) { String S = " abcyxuz " ; int N = S . length ( ) ; isPossible ( S , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int maximizedCapital ( int K , int W , int profits [ ] , int capital [ ] ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( Collections . reverseOrder ( ) ) ; ArrayList < int [ ] > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < capital . length ; i ++ ) { v . add ( new int [ ] { capital [ i ] , i } ) ; } Collections . sort ( v , ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return a [ 0 ] - b [ 0 ] ; return a [ 1 ] - b [ 1 ] ; } ) ; int j = 0 ; while ( K != 0 ) { while ( j < capital . length && v . get ( j ) [ 0 ] <= W ) { pq . add ( profits [ v . get ( j ) [ 1 ] ] ) ; j ++ ; } if ( ! pq . isEmpty ( ) ) { W = W + pq . peek ( ) ; pq . poll ( ) ; } K -- ; } return W ; } public static void main ( String [ ] args ) { int K = 2 ; int W = 0 ; int P [ ] = { 1 , 2 , 3 } ; int C [ ] = { 0 , 1 , 1 } ; System . out . println ( maximizedCapital ( K , W , P , C ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int maximumSum ( int arr [ ] , int S [ ] , int N , int K ) { int ans = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int temp = arr [ i ] ; arr [ i ] = arr [ N - 1 - i ] ; arr [ N - 1 - i ] = temp ; } for ( int i = 0 ; i < K ; i ++ ) ans += arr [ i ] ; Arrays . sort ( S ) ; for ( int i = 0 ; i < K ; i ++ ) { if ( S [ i ] == 1 ) ans += arr [ i ] ; S [ i ] -- ; } int counter = K - 1 ; for ( int i = 0 ; i < K ; i ++ ) { counter = counter + S [ i ] ; if ( S [ i ] != 0 ) ans += arr [ counter ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 13 , 7 , 17 } ; int S [ ] = { 1 , 3 } ; int N = arr . length ; int K = S . length ; System . out . println ( maximumSum ( arr , S , N , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static String decimalToBinary ( int n ) { String binstr = " " ; while ( n > 0 ) { binstr += ( char ) ( n % 2 + 48 ) ; n = n / 2 ; } return binstr ; } static int reversedBinaryDecimal ( int N ) { String decimal_to_binar = decimalToBinary ( N ) ; int binary_to_decimal = binaryToDecimal ( decimal_to_binar ) ; return binary_to_decimal ; } static void printSortedArray ( int arr [ ] , int size ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void modifyArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { arr [ i ] = reversedBinaryDecimal ( arr [ i ] ) ; } printSortedArray ( arr , size ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 98 , 43 , 66 , 83 } ; int n = arr . length ; modifyArray ( arr , n ) ; } }
import java . util . * ; class GFG { static boolean isSortedArray ( int [ ] arr , int n ) { int l = 0 , r = ( n - 1 ) ; while ( ( l + 1 ) < r ) { if ( arr [ l ] >= Math . max ( arr [ l + 1 ] , arr [ r - 1 ] ) && arr [ r ] >= Math . max ( arr [ r - 1 ] , arr [ l + 1 ] ) ) { l ++ ; r -- ; } else { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 3 , 1 , 2 , 5 } ; int N = arr . length ; if ( isSortedArray ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int solve ( int N , int X [ ] , int Y [ ] ) { ArrayList < int [ ] > p = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { p . add ( new int [ ] { X [ i ] , Y [ i ] } ) ; } Collections . sort ( p , ( p1 , p2 ) -> { if ( p1 [ 0 ] != p2 [ 0 ] ) return p1 [ 0 ] - p2 [ 0 ] ; return p1 [ 1 ] - p2 [ 1 ] ; } ) ; TreeSet < Integer > s = new TreeSet < > ( ) ; s . add ( p . get ( 0 ) [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { Integer it = s . floor ( p . get ( i ) [ 1 ] ) ; if ( it == null ) { s . add ( p . get ( i ) [ 1 ] ) ; } else { s . remove ( it ) ; s . add ( p . get ( i ) [ 1 ] ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; int X [ ] = { 1 , 2 , 0 } ; int Y [ ] = { 2 , 0 , 1 } ; int maxintersection = solve ( N , X , Y ) ; System . out . println ( maxintersection ) ; } }
import java . util . * ; class GFG { static void canReduceArray ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int b [ ] = new int [ N ] ; int j = N ; for ( int i = 0 ; i < N ; i ++ ) { b [ j - 1 ] = arr [ i ] ; j = j - 1 ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] - arr [ i + 1 ] > K ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 3 } ; int N = arr . length ; int K = 1 ; canReduceArray ( arr , N , K ) ; } }
import java . io . * ; class GFG { static void mergeStrings ( String s1 , String s2 ) { int len1 = s1 . length ( ) ; int len2 = s2 . length ( ) ; int pntr1 = 0 ; int pntr2 = 0 ; String ans = " " ; while ( pntr1 < len1 && pntr2 < len2 ) { if ( s1 . charAt ( pntr1 ) < s2 . charAt ( pntr2 ) ) { ans += s1 . charAt ( pntr1 ) ; pntr1 ++ ; } else { ans += s2 . charAt ( pntr2 ) ; pntr2 ++ ; } } if ( pntr1 < len1 ) { ans += s1 . substring ( pntr1 , len1 ) ; } if ( pntr2 < len2 ) { ans += s2 . substring ( pntr2 , len2 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S1 = " abdcdtx " ; String S2 = " achilp " ; mergeStrings ( S1 , S2 ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void sortStr ( String str ) { char S [ ] = str . toCharArray ( ) ; int N = S . length ; ArrayList < Character > temp = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) temp . add ( S [ i ] ) ; } if ( temp . size ( ) != 0 ) Collections . sort ( temp ) ; int ptr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S [ i ] != ' a ' && S [ i ] != ' e ' && S [ i ] != ' i ' && S [ i ] != ' o ' && S [ i ] != ' u ' ) S [ i ] = temp . get ( ptr ++ ) ; } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; sortStr ( S ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { public static int upper_bound ( int arr [ ] , int key ) { int l = - 1 , r = arr . length ; while ( l + 1 < r ) { int m = ( l + r ) >>> 1 ; if ( arr [ m ] <= key ) l = m ; else r = m ; } return l + 1 ; } static void countPairs ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int high = upper_bound ( arr , k * arr [ i ] ) ; ans += high - i - 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 9 , 5 } ; int n = arr . length ; int k = 2 ; countPairs ( arr , n , k ) ; } }
import java . util . * ; class GFG { static void findXOR ( int [ ] [ ] mat , int K ) { int N = mat . length ; int M = mat [ 0 ] . length ; ArrayList < Integer > digXOR = new ArrayList < Integer > ( ) ; for ( int l = 1 ; l <= ( N + M - 1 ) ; l ++ ) { int s_col = Math . max ( 0 , l - N ) ; int count = Math . min ( l , Math . min ( ( M - s_col ) , N ) ) ; int currXOR = 0 ; for ( int j = 0 ; j < count ; j ++ ) { currXOR = ( currXOR ^ mat [ Math . min ( N , l ) - j - 1 ] [ s_col + j ] ) ; } digXOR . add ( currXOR ) ; } Collections . sort ( digXOR ) ; System . out . print ( digXOR . get ( N + M - 1 - K ) ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int K = 3 ; findXOR ( mat , K ) ; } }
import java . util . * ; class pair { int first , second , third ; pair ( int first , int second , int third ) { this . first = first ; this . second = second ; this . third = third ; } } class GFG { static int findSum ( ArrayList < pair > v , int n ) { Map < Integer , Boolean > um = new HashMap < > ( ) ; int res = 0 ; int cnt = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( cnt == n ) break ; int x = v . get ( i ) . second ; int y = v . get ( i ) . third ; int xorResult = v . get ( i ) . first ; if ( um . get ( x ) == null && um . get ( y ) == null ) { res += xorResult ; um . put ( x , true ) ; um . put ( y , true ) ; cnt += 2 ; } } return res ; } static void findMaxMinSum ( int a [ ] , int n ) { ArrayList < pair > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int xorResult = a [ i ] ^ a [ j ] ; v . add ( new pair ( xorResult , a [ i ] , a [ j ] ) ) ; } } Collections . sort ( v , ( aa , b ) -> aa . first - b . first ) ; int maxi = 0 , mini = 0 ; mini = findSum ( v , n ) ; Collections . reverse ( v ) ; maxi = findSum ( v , n ) ; System . out . print ( mini + " ▁ " + maxi ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; findMaxMinSum ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void maximumSumPermutation ( int [ ] arr ) { int N = arr . length ; Arrays . sort ( arr ) ; int temp = arr [ 0 ] ; arr [ 0 ] = arr [ N - 1 ] ; arr [ N - 1 ] = temp ; for ( int i : arr ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; maximumSumPermutation ( arr ) ; } }
import java . util . * ; public class GFG { static void findMaxNumbers ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int opr = ( int ) Math . ceil ( ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ) ; k -= opr ; if ( k < 0 ) { break ; } cnt ++ ; } System . out . println ( cnt ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 8 , 4 , 7 } ; int N = arr . length ; int K = 5 ; findMaxNumbers ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void findLastElement ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int i = 0 ; for ( i = 1 ; i < N ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] != 0 && arr [ i ] - arr [ i - 1 ] != 2 ) { System . out . println ( " - 1" ) ; return ; } } System . out . println ( arr [ N - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 0 , 8 } ; int N = arr . length ; findLastElement ( arr , N ) ; } }
import java . util . * ; class GFG { static void maxDivisions ( Integer arr [ ] , int N , int X ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int maxSub = 0 ; int size = 0 ; for ( int i = 0 ; i < N ; i ++ ) { size ++ ; if ( arr [ i ] * size >= X ) { maxSub ++ ; size = 0 ; } } System . out . print ( maxSub + "NEW_LINE"); } public static void main ( String [ ] args ) { Integer arr [ ] = { 1 , 3 , 3 , 7 } ; int N = arr . length ; int X = 3 ; maxDivisions ( arr , N , X ) ; } }
import java . util . * ; public class GFG { static void meanOfRemainingElements ( int [ ] arr , int N , int K ) { Arrays . sort ( arr ) ; int kthPercent = ( N * K ) / 100 ; float sum = 0f ; for ( int i = 0 ; i < N ; i ++ ) if ( i >= kthPercent && i < ( N - kthPercent ) ) sum += arr [ i ] ; float mean = ( sum / ( N - 2 * kthPercent ) ) ; System . out . format ( " % .5f " , mean ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 6 , 2 , 7 , 5 , 1 , 2 , 0 , 3 , 10 , 2 , 5 , 0 , 5 , 5 , 0 , 8 , 7 , 6 , 8 , 0 } ; int arr_size = arr . length ; int K = 5 ; meanOfRemainingElements ( arr , arr_size , K ) ; } }
import java . util . * ; class GFG { static void maxIntersection ( int segments [ ] [ ] , int N ) { int [ ] rvalues = new int [ N ] ; int [ ] lvalues = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) { lvalues [ i ] = segments [ i ] [ 0 ] ; rvalues [ i ] = segments [ i ] [ 1 ] ; } Arrays . sort ( lvalues ) ; Arrays . sort ( rvalues ) ; int [ ] answer = { - 1 , - 1 } ; int numIntersections = 0 ; for ( int i = 0 ; i < N ; ++ i ) { int lesser = lower_bound ( rvalues , 0 , segments . length , segments [ i ] [ 0 ] ) ; int greater = Math . max ( 0 , N - ( upper_bound ( lvalues , 0 , segments . length , segments [ i ] [ 1 ] ) ) ) ; if ( ( N - lesser - greater ) >= numIntersections ) { answer = new int [ ] { segments [ i ] [ 0 ] , segments [ i ] [ 1 ] } ; numIntersections = ( N - lesser - greater ) ; } } System . out . print ( answer [ 0 ] + " ▁ " + answer [ 1 ] ) ; } static int lower_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int segments [ ] [ ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 6 } } ; int N = segments . length ; maxIntersection ( segments , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maxPossibleSum ( int [ ] arr , int N ) { Arrays . sort ( arr ) ; int sum = 0 ; int j = N - 3 ; while ( j >= 0 ) { sum += arr [ j ] ; j -= 3 ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 4 , 5 , 2 , 3 , 1 , 5 , 9 } ; int N = arr . length ; maxPossibleSum ( arr , N ) ; } }
import java . util . * ; class GFG { static int temp [ ] = new int [ 100000 ] ; static int merge ( int A [ ] , int left , int mid , int right ) { int swaps = 0 ; int i = left , j = mid , k = left ; while ( i < mid && j <= right ) { if ( A [ i ] <= A [ j ] ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } else { temp [ k ] = A [ j ] ; k ++ ; j ++ ; swaps += mid - i ; } } while ( i < mid ) { temp [ k ] = A [ i ] ; k ++ ; i ++ ; } while ( j <= right ) { temp [ k ] = A [ j ] ; k ++ ; j ++ ; } while ( left <= right ) { A [ left ] = temp [ left ] ; left ++ ; } return swaps ; } static int mergeInsertionSwap ( int A [ ] , int left , int right ) { int swaps = 0 ; if ( left < right ) { int mid = left + ( right - left ) / 2 ; swaps += mergeInsertionSwap ( A , left , mid ) ; swaps += mergeInsertionSwap ( A , mid + 1 , right ) ; swaps += merge ( A , left , mid + 1 , right ) ; } return swaps ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 3 , 1 , 2 } ; int N = A . length ; System . out . println ( mergeInsertionSwap ( A , 0 , N - 1 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxCntRemovedfromArray ( int [ ] arr , int N , int [ ] brr , int M ) { Arrays . sort ( arr ) ; int i = 0 ; int sumArr = 0 ; for ( i = 0 ; i < N ; i ++ ) { sumArr += arr [ i ] ; } int sumBrr = 0 ; for ( i = 0 ; i < M ; i ++ ) { sumBrr += brr [ i ] ; } int cntRemElem = 0 ; while ( i < N && sumArr >= sumBrr ) { sumArr -= arr [ i ] ; i += 1 ; if ( sumArr >= sumBrr ) { cntRemElem += 1 ; } } return cntRemElem ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 } ; int [ ] brr = new int [ ] { 7 } ; int N = arr . length ; int M = brr . length ; System . out . println ( maxCntRemovedfromArray ( arr , N , brr , M ) ) ; } }
import java . util . * ; class GFG { static class pair implements Comparable < pair > { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } public int compareTo ( pair p ) { return this . first - p . first ; } } static void findpair ( pair [ ] arr , int N ) { Arrays . sort ( arr ) ; for ( int i = 1 ; i < N ; i ++ ) { int b = arr [ i - 1 ] . second ; int d = arr [ i ] . second ; if ( b > d ) { System . out . print ( " ( " + arr [ i - 1 ] . first + " ▁ " + b + " ) , ▁ ( " + arr [ i ] . first + " ▁ " + d + " ) " ) ; return ; } } System . out . print ( "NO SUCH PAIR EXISTNEW_LINE"); } public static void main ( String [ ] args ) { pair arr [ ] = { new pair ( 3 , 7 ) , new pair ( 21 , 23 ) , new pair ( 4 , 13 ) , new pair ( 1 , 2 ) , new pair ( 7 , - 1 ) } ; findpair ( arr , 5 ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printOrder ( int order [ ] , int N , int X ) { ArrayList < int [ ] > vect = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( order [ i ] % X == 0 ) { vect . add ( new int [ ] { order [ i ] / X , i + 1 } ) ; } else { vect . add ( new int [ ] { order [ i ] / X + 1 , i + 1 } ) ; } } Collections . sort ( vect , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( vect . get ( i ) [ 1 ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { int N = 3 , X = 3 ; int order [ ] = { 2 , 7 , 4 } ; printOrder ( order , N , X ) ; } }
import java . util . * ; class GFG { static void rearrange ( int A [ ] , int B [ ] , int N , int X ) { boolean flag = true ; Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] + B [ N - 1 - i ] > X ) { flag = false ; break ; } } if ( flag == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 1 , 2 } ; int X = 4 ; int N = A . length ; rearrange ( A , B , N , X ) ; } }
import java . util . * ; class GFG { static void insertionSort ( int arr [ ] , int n ) { int i , key , j ; for ( i = 1 ; i < n ; i ++ ) { key = arr [ i ] ; j = i - 1 ; while ( j >= 0 && arr [ j ] > key ) { arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } arr [ j + 1 ] = key ; } } static void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; int N = arr . length ; insertionSort ( arr , N ) ; printArray ( arr , N ) ; } }
import java . util . PriorityQueue ; class GFG { static int findSmallestNumLeft ( int arr [ ] , int N ) { PriorityQueue < Integer > PQ = new PriorityQueue < Integer > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < N ; i ++ ) { PQ . add ( arr [ i ] ) ; } while ( PQ . size ( ) > 1 ) { int top1 = PQ . peek ( ) ; PQ . remove ( ) ; int top2 = PQ . peek ( ) ; PQ . remove ( ) ; PQ . add ( ( top1 + top2 + 1 ) / 2 ) ; } return PQ . peek ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 30 , 16 , 40 } ; int N = arr . length ; System . out . print ( findSmallestNumLeft ( arr , N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } 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 int digitProduct ( int number ) { int product = 1 ; while ( number > 0 ) { product *= ( number % 10 ) ; number /= 10 ; } return product ; } static boolean compositedigitProduct ( int num ) { int res = digitProduct ( num ) ; if ( res == 1 ) { return false ; } if ( isComposite ( res ) ) { return true ; } return false ; } static int largestCompositeDigitProduct ( int a [ ] , int n , int k ) { Vector < pair > pq = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i % k ) == 0 ) { continue ; } if ( compositedigitProduct ( a [ i ] ) ) { int b = digitProduct ( a [ i ] ) ; pq . add ( new pair ( b , a [ i ] ) ) ; } } Collections . sort ( pq , ( x , y ) -> x . first - y . first ) ; return pq . get ( pq . size ( ) - 1 ) . second ; } public static void main ( String [ ] args ) { int arr [ ] = { 233 , 144 , 89 , 71 , 13 , 21 , 11 , 34 , 55 , 23 } ; int n = arr . length ; int k = 3 ; int ans = largestCompositeDigitProduct ( arr , n , k ) ; System . out . print ( ans + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { public static void findMinimum ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 1 ; i < 2 * ( N / K ) ; i += 2 ) { ans += arr [ i ] ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 11 , 20 , 5 , 7 , 8 , 14 , 2 , 17 , 16 , 10 } ; int N = arr . length ; int K = 5 ; findMinimum ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void findSubarrays ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int i = 0 ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 3 , 0 , 1 , 2 , 0 } ; int N = arr . length ; findSubarrays ( arr , N ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; import java . util . HashMap ; import java . util . Map ; class GFG { static class Pair < K , V > { K first ; V second ; public Pair ( K first , V second ) { this . first = first ; this . second = second ; } public static < K , V > Pair < K , V > of ( K first , V second ) { return new Pair < > ( first , second ) ; } } static void sweetTypeOnGivenDay ( int a [ ] , int b [ ] , int n , int k , ArrayList < Pair < Integer , Integer > > q ) { ArrayList < Pair < Integer , Pair < Integer , Integer > > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . add ( Pair . of ( b [ i ] , Pair . of ( a [ i ] , i + 1 ) ) ) ; Collections . sort ( v , new Comparator < Pair < Integer , Pair < Integer , Integer > > > ( ) { @ Override public int compare ( Pair < Integer , Pair < Integer , Integer > > a , Pair < Integer , Pair < Integer , Integer > > b ) { if ( a . first == b . first ) { if ( a . second . first == b . second . first ) { return b . second . second - a . second . second ; } return b . second . first - a . second . first ; } return b . first - a . first ; } } ) ; Map < Integer , Pair < Integer , Integer > > mp = new HashMap < > ( ) ; int lowerbound = 0 , upperbound = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int maxi_days = v . get ( i ) . second . first ; int mini_days = v . get ( i ) . second . first / k ; if ( v . get ( i ) . second . first % k != 0 ) mini_days ++ ; upperbound += maxi_days ; mp . put ( v . get ( i ) . second . second , Pair . of ( lowerbound , upperbound ) ) ; lowerbound += mini_days ; } for ( int i = 0 ; i < q . size ( ) ; i ++ ) { int x = q . get ( i ) . first , y = q . get ( i ) . second ; int e = mp . get ( x ) . first ; int f = mp . get ( x ) . second ; if ( y >= e && y <= f ) System . out . println ( " Yes ▁ " ) ; else System . out . println ( " No ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 6 , 3 , 7 , 5 , 2 } ; int B [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 3 ; ArrayList < Pair < Integer , Integer > > Queries = new ArrayList < > ( Arrays . asList ( Pair . of ( 4 , 4 ) , Pair . of ( 3 , 16 ) , Pair . of ( 2 , 7 ) ) ) ; int n = A . length ; sweetTypeOnGivenDay ( A , B , n , K , Queries ) ; } }
class GFG { static void getPairs ( int arr [ ] , int N , int K ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] > K * arr [ i + 1 ] ) count ++ ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 2 , 1 } ; int N = arr . length ; int K = 2 ; getPairs ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void NDivKWithFreq ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < N ; ) { int cnt = 1 ; while ( ( i + 1 ) < N && arr [ i ] == arr [ i + 1 ] ) { cnt ++ ; i ++ ; } if ( cnt > ( N / K ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } i ++ ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 6 , 6 , 6 , 6 , 7 , 10 } ; int N = arr . length ; int K = 4 ; NDivKWithFreq ( arr , N , K ) ; } }
class GFG { static void printCircularArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void sortCircularArray ( int arr [ ] , int n , int k , int m ) { for ( int i = 0 ; i < m ; i ++ ) { for ( int j = k ; j < k + m - 1 ; j ++ ) { if ( arr [ j % n ] > arr [ ( j + 1 ) % n ] ) { int t = arr [ j % n ] ; arr [ j % n ] = arr [ ( j + 1 ) % n ] ; arr [ ( j + 1 ) % n ] = t ; } } } printCircularArray ( arr , n ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 1 , 6 , 5 , 3 } ; int K = 2 , M = 3 ; int N = arr . length ; sortCircularArray ( arr , N , K , M ) ; } }
import java . util . * ; class GFG { static int minimumCostReqToCompthePrcess ( int [ ] [ ] arr ) { Arrays . sort ( arr , ( a , b ) -> b [ 1 ] - a [ 1 ] ) ; int n = arr . length ; int minCost = 0 ; int minCostInit = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] [ 1 ] > minCostInit ) { minCost += ( arr [ i ] [ 1 ] - minCostInit ) ; minCostInit = arr [ i ] [ 1 ] ; } minCostInit -= arr [ i ] [ 0 ] ; } return minCost ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 2 } , { 2 , 4 } , { 4 , 8 } } ; System . out . println ( minimumCostReqToCompthePrcess ( arr ) ) ; } }
import java . util . * ; class GFG { static void rearrangeArray ( int a [ ] , int n ) { Arrays . sort ( a ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] == i + 1 ) { int temp = a [ i ] ; a [ i ] = a [ i + 1 ] ; a [ i + 1 ] = temp ; } } if ( a [ n - 1 ] == n ) { int temp = a [ n - 1 ] ; a [ n - 1 ] = a [ n - 2 ] ; a [ n - 2 ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 2 , 4 } ; int N = arr . length ; rearrangeArray ( arr , N ) ; } }
import java . util . * ; import java . util . Collections ; class GFG { static int maximumKthLargestsumPart ( int [ ] arr , int N , int M , int K ) { int maxSum = 0 ; if ( N % M != 0 ) return - 1 ; int sz = ( N / M ) ; if ( K > sz ) return - 1 ; Arrays . sort ( arr ) ; int i , k , t ; for ( i = 0 ; i < N / 2 ; i ++ ) { t = arr [ i ] ; arr [ i ] = arr [ N - i - 1 ] ; arr [ N - i - 1 ] = t ; } for ( i = 1 ; i <= M ; i ++ ) { maxSum += arr [ i * K - 1 ] ; } return maxSum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int M = 2 ; int K = 1 ; int N = arr . length ; System . out . println ( maximumKthLargestsumPart ( arr , N , M , K ) ) ; } }
import java . util . * ; class GFG { static void minimumRange ( int arr [ ] , int N , int K ) { if ( K >= N ) { System . out . print ( 0 ) ; return ; } Arrays . sort ( arr ) ; int left = 0 , right = N - 1 , i ; for ( i = 0 ; i < K ; i ++ ) { if ( arr [ right - 1 ] - arr [ left ] < arr [ right ] - arr [ left + 1 ] ) right -- ; else left ++ ; } System . out . print ( arr [ right ] - arr [ left ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 12 , 14 , 21 , 54 , 61 } ; int N = arr . length ; int K = 4 ; minimumRange ( arr , N , K ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void splitArray ( int arr [ ] , int n , int K ) { Arrays . sort ( arr ) ; int i = n - 1 ; int result = 0 ; while ( K -- != 0 ) { result += arr [ i - 1 ] ; i -= 2 ; } System . out . print ( result ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 1 , 5 , 1 , 3 } ; int N = arr . length ; int K = 2 ; splitArray ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void merge ( int [ ] indices , int [ ] a , int l , int mid , int r ) { int [ ] temp_ind = new int [ r - l + 1 ] ; int j = mid + 1 ; int i = 0 , temp_l = l , k ; while ( l <= mid && j <= r ) { if ( a [ indices [ l ] ] < a [ indices [ j ] ] ) temp_ind [ i ++ ] = indices [ l ++ ] ; else temp_ind [ i ++ ] = indices [ j ++ ] ; } while ( l <= mid ) temp_ind [ i ++ ] = indices [ l ++ ] ; while ( j <= r ) temp_ind [ i ++ ] = indices [ j ++ ] ; for ( k = 0 ; k < i ; k ++ ) indices [ temp_l ++ ] = temp_ind [ k ] ; } static void divide ( int [ ] indices , int [ ] a , int l , int r ) { if ( l >= r ) return ; int mid = l / 2 + r / 2 ; divide ( indices , a , l , mid ) ; divide ( indices , a , mid + 1 , r ) ; merge ( indices , a , l , mid , r ) ; } static void noOfSubsequences ( int arr [ ] , int N ) { int [ ] indices = new int [ N ] ; int i ; for ( i = 0 ; i < N ; i ++ ) indices [ i ] = i ; divide ( indices , arr , 0 , N - 1 ) ; int [ ] B = new int [ N ] ; int subseq = 1 ; for ( i = 0 ; i < N ; i ++ ) { B [ indices [ i ] ] = subseq ; subseq *= 2 ; } for ( i = 0 ; i < N ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 } ; int N = arr . length ; noOfSubsequences ( arr , N ) ; } }
import java . util . * ; class GFG { static void merge ( int order [ ] , int freq [ ] , int i , int mid , int j ) { int tempOrder [ ] = new int [ j - i + 1 ] ; int temp = mid + 1 , index = - 1 ; while ( i <= mid && temp <= j ) { if ( freq [ order [ i ] ] <= freq [ order [ temp ] ] ) { tempOrder [ ++ index ] = order [ i ++ ] ; } else { tempOrder [ ++ index ] = order [ temp ++ ] ; } } while ( i <= mid ) { tempOrder [ ++ index ] = order [ i ++ ] ; } while ( temp <= j ) { tempOrder [ ++ index ] = order [ temp ++ ] ; } int ind = index ; for ( index = ind ; index >= 0 ; index -- ) { order [ j -- ] = tempOrder [ index ] ; } } static void divide ( int order [ ] , int freq [ ] , int i , int j ) { if ( i >= j ) return ; int mid = i / 2 + j / 2 ; divide ( order , freq , i , mid ) ; divide ( order , freq , mid + 1 , j ) ; merge ( order , freq , i , mid , j ) ; } static void orderProcesses ( int A [ ] , int N , int q ) { int i = 0 ; int freq [ ] = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { freq [ i ] = ( A [ i ] / q ) ; if ( A [ i ] % q > 0 ) freq [ i ] += 1 ; } int order [ ] = new int [ 4 ] ; for ( i = 0 ; i < N ; i ++ ) { order [ i ] = i ; } divide ( order , freq , 0 , N - 1 ) ; for ( i = 0 ; i < N ; i ++ ) { System . out . print ( order [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 4 } ; int Q = 3 ; int N = arr . length ; orderProcesses ( arr , N , Q ) ; } }
import java . util . * ; class GFG { static String convert_to_words ( int n ) { char num [ ] = String . valueOf ( n ) . toCharArray ( ) ; int len = num . length ; String ans = " " ; if ( len == 0 ) { ans += " Empty ▁ String " ; return ans ; } String [ ] single_digits = new String [ ] { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String [ ] two_digits = new String [ ] { " " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " } ; String [ ] tens_multiple = new String [ ] { " " , " " , " twenty " , " thirty " , " forty " , " fifty " , " sixty " , " seventy " , " eighty " , " ninety " } ; String [ ] tens_power = new String [ ] { " hundred " , " thousand " } ; if ( len == 1 ) { ans += single_digits [ num [ 0 ] - '0' ] ; return ans ; } int x = 0 ; while ( x < num . length ) { if ( len >= 3 ) { if ( num [ x ] - '0' != 0 ) { ans += single_digits [ num [ x ] - '0' ] ; ans += " ▁ " ; ans += tens_power [ len - 3 ] ; ans += " ▁ " ; } -- len ; } else { if ( num [ x ] - '0' == 1 ) { int sum = num [ x ] - '0' + num [ x ] - '0' ; ans += two_digits [ sum ] ; return ans ; } else if ( num [ x ] - '0' == 2 && num [ x + 1 ] - '0' == 0 ) { ans += " twenty " ; return ans ; } else { int i = ( num [ x ] - '0' ) ; if ( i > 0 ) { ans += tens_multiple [ i ] ; ans += " ▁ " ; } else ans += " " ; ++ x ; if ( num [ x ] - '0' != 0 ) ans += single_digits [ num [ x ] - '0' ] ; } } ++ x ; } return " " ; } static void lexNumbers ( int n ) { Vector < String > s = new Vector < String > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { s . add ( convert_to_words ( i ) ) ; } Collections . sort ( s ) ; Vector < String > ans = new Vector < String > ( ) ; for ( int i = 0 ; i < n ; i ++ ) ans . add ( s . get ( i ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( ans . get ( i ) + " , ▁ " ) ; System . out . print ( ans . get ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; lexNumbers ( n ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void minimumCost ( int [ ] binary , int n , int a , int b ) { List < Integer > groupOfZeros = new ArrayList < Integer > ( ) ; int len = 0 , i = 0 ; boolean increment_need = true ; while ( i < n ) { increment_need = true ; while ( i < n && binary [ i ] == 0 ) { len ++ ; i ++ ; increment_need = false ; } if ( increment_need == true ) { i ++ ; } if ( len != 0 ) { groupOfZeros . add ( len ) ; } len = 0 ; } Collections . sort ( groupOfZeros ) ; i = 0 ; boolean found_ones = false ; int NumOfOnes = 0 ; while ( i < n ) { found_ones = false ; while ( i < n && binary [ i ] == 1 ) { i ++ ; found_ones = true ; } if ( found_ones == false ) i ++ ; else NumOfOnes ++ ; } int ans = Integer . MAX_VALUE ; for ( int i1 = 0 ; i1 < n ; i1 ++ ) { int curr = 0 , totalOnes = NumOfOnes ; if ( i1 == 0 ) { curr = totalOnes * a ; } else { int mark = i1 , num_of_changes = 0 ; for ( int x : groupOfZeros ) { if ( mark >= x ) { totalOnes -- ; mark -= x ; num_of_changes += x ; } else { break ; } } curr = ( num_of_changes * b ) + ( totalOnes * a ) ; } ans = Math . min ( ans , curr ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 1 } ; int N = 6 ; int X = 10 , Y = 4 ; minimumCost ( arr , N , X , Y ) ; } }
import java . util . * ; class GFG { static int isSortedparitions ( int arr [ ] , int i , int j ) { for ( int k = i + 1 ; k <= j ; k ++ ) { if ( arr [ k ] < arr [ k - 1 ] ) { return 0 ; } } return 1 ; } static int partitionsArr ( int arr [ ] , int i , int j ) { if ( i >= j ) return 1 ; int flag = ( int ) isSortedparitions ( arr , i , j ) ; if ( flag != 0 ) { return ( j - i + 1 ) ; } int mid = ( i + j ) / 2 ; int X = partitionsArr ( arr , i , mid ) ; int Y = partitionsArr ( arr , mid + 1 , j ) ; return Math . max ( X , Y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 12 , 1 , 2 , 13 , 14 , 3 , 4 } ; int N = arr . length ; System . out . print ( partitionsArr ( arr , 0 , N - 1 ) ) ; } }
import java . util . * ; class GFG { static int minimum_possible_sum ( int arr [ ] , int n , int k ) { if ( n == 0 ) { return 0 ; } if ( n == 1 ) { return arr [ 0 ] ; } for ( int i = 0 ; i < k ; i ++ ) { int smallest_element = arr [ 0 ] ; int smallest_pos = 0 ; int largest_element = arr [ 0 ] ; int largest_pos = 0 ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] >= largest_element ) { largest_element = arr [ j ] ; largest_pos = j ; } if ( arr [ j ] < smallest_element ) { smallest_element = arr [ j ] ; smallest_pos = j ; } } int a = smallest_element * 2 ; int b = largest_element / 2 ; if ( a + b < smallest_element + largest_element ) { arr [ smallest_pos ] = a ; arr [ largest_pos ] = b ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += arr [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 1 , 100 , 100 , 1 } ; int K = 2 ; int n = arr . length ; System . out . print ( minimum_possible_sum ( arr , n , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; Arrays . sort ( A ) ; int [ ] bits = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] == 0 ) { continue ; } for ( int j = 0 ; j < 32 ; j ++ ) { if ( ( ( 1 << j ) & A [ i ] ) == 0 ) { count += bits [ j ] ; } } ++ bits [ ( int ) ( ( int ) ( Math . log ( A [ i ] ) / Math . log ( 2 ) ) ) ] ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void mostvisitedsector ( int N , ArrayList < Integer > A ) { int maxVisited = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < A . size ( ) - 1 ; i ++ ) { int start = A . get ( i ) % N ; int end = A . get ( i + 1 ) % N ; while ( start != end ) { if ( start == 0 ) { if ( mp . containsKey ( N ) ) mp . put ( N , mp . get ( N ) + 1 ) ; else mp . put ( N , 1 ) ; if ( mp . get ( N ) > maxVisited ) { maxVisited = mp . get ( N ) ; } } else { if ( mp . containsKey ( start ) ) mp . put ( start , mp . get ( start ) + 1 ) ; else mp . put ( start , 1 ) ; if ( mp . get ( start ) > maxVisited ) { maxVisited = mp . get ( start ) ; } } start = ( start + 1 ) % N ; } } int last = A . get ( A . size ( ) - 1 ) ; if ( mp . containsKey ( last ) ) mp . put ( last , mp . get ( last ) + 1 ) ; else mp . put ( last , 1 ) ; if ( mp . get ( last ) > maxVisited ) { maxVisited = mp . get ( last ) ; } for ( Map . Entry x : mp . entrySet ( ) ) { if ( ( int ) x . getValue ( ) == maxVisited ) { System . out . print ( x . getKey ( ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 4 ; ArrayList < Integer > arr = new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , 1 , 2 ) ) ; mostvisitedsector ( N , arr ) ; } }
import java . util . * ; class GFG { static int largestArea ( int arr1 [ ] , int n , int arr2 [ ] , int m ) { int end = 0 , start = 0 , i = 0 , j = 0 ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr1 ) ; while ( i < n && j < m ) { if ( arr1 [ i ] == arr2 [ j ] ) { if ( start == 0 ) start = arr1 [ i ] ; else end = arr1 [ i ] ; i ++ ; j ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) j ++ ; else i ++ ; } if ( end == 0 start == 0 ) return 0 ; else return ( end - start ) ; } public static void main ( String args [ ] ) { int arr1 [ ] = { 1 , 2 , 4 } ; int N = arr1 . length ; int arr2 [ ] = { 1 , 3 , 4 } ; int M = arr2 . length ; System . out . println ( largestArea ( arr1 , N , arr2 , M ) ) ; } }
import java . util . * ; class GFG { static int count_pairs ( int arr [ ] , int brr [ ] , int N , int M , int K ) { int i = 0 ; int j = 0 ; int cntPairs = 0 ; Arrays . sort ( arr ) ; Arrays . sort ( brr ) ; while ( i < N && j < M ) { if ( brr [ j ] - arr [ i ] > K ) { cntPairs += ( M - j ) ; i ++ ; } else { j ++ ; } } return cntPairs ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 9 , 1 , 8 } ; int brr [ ] = { 10 , 12 , 7 , 4 , 2 , 3 } ; int K = 3 ; int N = arr . length ; int M = brr . length ; System . out . println ( count_pairs ( arr , brr , N , M , K ) ) ; } }
import java . util . * ; class GFG { static void partition ( int arr [ ] , int N , int brr [ ] , int M , int Pivot ) { int l = N - 1 ; int r = 0 ; while ( l >= 0 && r < M ) { if ( arr [ l ] < Pivot ) l -- ; else if ( brr [ r ] > Pivot ) r ++ ; else { int t = arr [ l ] ; arr [ l ] = brr [ r ] ; brr [ r ] = t ; l -- ; r ++ ; } } } static void Merge ( int arr [ ] , int N , int brr [ ] , int M ) { int l = 0 ; int r = 0 ; int index = - 1 ; int Pivot = 0 ; while ( index < N && l < N && r < M ) { if ( arr [ l ] < brr [ r ] ) { Pivot = arr [ l ++ ] ; } else { Pivot = brr [ r ++ ] ; } index ++ ; } while ( index < N && l < N ) { Pivot = arr [ l ++ ] ; index ++ ; } while ( index < N && r < M ) { Pivot = brr [ r ++ ] ; index ++ ; } partition ( arr , N , brr , M , Pivot ) ; Arrays . sort ( arr ) ; Arrays . sort ( brr ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; for ( int i = 0 ; i < M ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 9 } ; int brr [ ] = { 2 , 4 , 7 , 10 } ; int N = arr . length ; int M = brr . length ; Merge ( arr , N , brr , M ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int rankLessThanK ( int [ ] arr , int k , int n ) { int rank = 1 ; int position = 1 ; Arrays . sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i == n - 1 arr [ i ] != arr [ i + 1 ] ) { rank = position ; if ( rank > k ) return position - 1 ; } position ++ ; } return n ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 4 , 5 } ; int N = arr . length ; int K = 4 ; System . out . println ( rankLessThanK ( arr , K , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void eraseSubdirectory ( ArrayList < String > dir ) { ArrayList < String > res = new ArrayList < String > ( ) ; Collections . sort ( dir ) ; res . add ( dir . get ( 0 ) ) ; System . out . print ( " { " + dir . get ( 0 ) + " , ▁ " ) ; for ( int i = 1 ; i < dir . size ( ) ; i ++ ) { String curr = dir . get ( i ) ; String prev = res . get ( res . size ( ) - 1 ) ; int l = prev . length ( ) ; if ( curr . length ( ) > l && curr . charAt ( l ) == ' / ' && prev . equals ( curr . substring ( 0 , l ) ) ) continue ; res . add ( curr ) ; System . out . print ( curr + " , ▁ " ) ; } System . out . print ( "}NEW_LINE"); } public static void main ( String [ ] args ) { ArrayList < String > dir = new ArrayList < String > ( Arrays . asList ( " / a " , " / a / j " , " / c / d / e " , " / c / d " , " / b " ) ) ; eraseSubdirectory ( dir ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int numberOfPairs ( int [ ] arr1 , int n , int [ ] arr2 , int m ) { PriorityQueue < Integer > pQueue = new PriorityQueue < Integer > ( new Comparator < Integer > ( ) { public int compare ( Integer lhs , Integer rhs ) { if ( lhs < rhs ) { return + 1 ; } if ( lhs . equals ( rhs ) ) { return 0 ; } return - 1 ; } } ) ; int i , j ; Arrays . sort ( arr1 ) ; for ( j = 0 ; j < m ; j ++ ) { pQueue . add ( arr2 [ j ] ) ; } int ans = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( pQueue . peek ( ) >= 2 * arr1 [ i ] ) { ans ++ ; pQueue . poll ( ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 3 , 1 , 2 } ; int [ ] arr2 = { 3 , 4 , 2 , 1 } ; int N = 3 ; int M = 4 ; System . out . println ( numberOfPairs ( arr1 , N , arr2 , M ) ) ; } }
import java . util . * ; class GFG { static void minCost ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . print ( 0 + "NEW_LINE"); return ; } Arrays . sort ( arr ) ; float raised = 1 / ( float ) ( n - 1 ) ; float temp = ( float ) Math . pow ( arr [ n - 1 ] , raised ) ; int r = ( int ) ( temp ) + 1 ; int i , j , min_cost = Integer . MAX_VALUE ; int common_ratio = 1 ; for ( j = 1 ; j <= r ; j ++ ) { int curr_cost = 0 , prod = 1 ; for ( i = 0 ; i < n ; i ++ ) { curr_cost += Math . abs ( arr [ i ] - prod ) ; prod *= j ; if ( curr_cost >= min_cost ) break ; } if ( i == n ) { min_cost = Math . min ( min_cost , curr_cost ) ; common_ratio = j ; } } System . out . print ( min_cost + " ▁ " ) ; System . out . print ( common_ratio + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 1 , 11 , 4 , 27 , 15 , 33 } ; minCost ( arr , N ) ; } }
import java . util . * ; class GFG { static final int maxN = 10 ; static void calculateFib ( int fib [ ] , int n ) { fib [ 0 ] = fib [ 1 ] = 1 ; for ( int x = 2 ; x < n ; x ++ ) { fib [ x ] = fib [ x - 1 ] + fib [ x - 2 ] ; } } static int find_mth_bit ( int n , int m , int fib [ ] ) { if ( n <= 1 ) { return n ; } int len_left = fib [ n - 2 ] ; int len_right = fib [ n - 1 ] ; if ( m <= len_left ) { return find_mth_bit ( n - 2 , len_left + 1 - m , fib ) ; } else { return find_mth_bit ( n - 1 , len_right + 1 - ( m - len_left ) , fib ) ; } } static void find_mth_bitUtil ( int n , int m ) { int [ ] fib = new int [ maxN ] ; calculateFib ( fib , maxN ) ; int ans = find_mth_bit ( n , m , fib ) ; System . out . print ( ans + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 3 ; find_mth_bitUtil ( n , m ) ; } }
import java . util . * ; class GFG { static int minAdjSwaps ( int [ ] [ ] mat ) { int N = mat . length ; int [ ] cntZero = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 && mat [ i ] [ j ] == 0 ; j -- ) { cntZero [ i ] ++ ; } } int cntSwaps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( cntZero [ i ] < ( N - i - 1 ) ) { int First = i ; while ( First < N && cntZero [ First ] < ( N - i - 1 ) ) { First ++ ; } if ( First == N ) { return - 1 ; } while ( First > i ) { cntZero = swap ( cntZero , First , First - 1 ) ; First -- ; cntSwaps ++ ; } } } return cntSwaps ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 0 , 2 } , { 3 , 1 , 0 } , { 4 , 0 , 0 } } ; System . out . print ( minAdjSwaps ( mat ) ) ; } }
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static int minOperations ( int arr1 [ ] , int arr2 [ ] , int i , int j ) { if ( arr1 . equals ( arr2 ) ) return 0 ; if ( i >= arr1 . length j >= arr2 . length ) return 0 ; if ( arr1 [ i ] < arr2 [ j ] ) return 1 + minOperations ( arr1 , arr2 , i + 1 , j + 1 ) ; return Math . max ( minOperations ( arr1 , arr2 , i , j + 1 ) , minOperations ( arr1 , arr2 , i + 1 , j ) ) ; } static void minOperationsUtil ( int [ ] arr ) { int brr [ ] = new int [ arr . length ] ; for ( int i = 0 ; i < arr . length ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; if ( arr . equals ( brr ) ) System . out . print ( "0" ) ; else System . out . println ( minOperations ( arr , brr , 0 , 0 ) ) ; } public static void main ( final String [ ] args ) { int arr [ ] = { 4 , 7 , 2 , 3 , 9 } ; minOperationsUtil ( arr ) ; } }
import java . util . * ; class GFG { static void canTransform ( String s , String t ) { int n = s . length ( ) ; Vector < Integer > occur [ ] = new Vector [ 26 ] ; for ( int i = 0 ; i < occur . length ; i ++ ) occur [ i ] = new Vector < Integer > ( ) ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( s . charAt ( x ) - ' a ' ) ; occur [ ch ] . add ( x ) ; } int [ ] idx = new int [ 26 ] ; boolean poss = true ; for ( int x = 0 ; x < n ; x ++ ) { char ch = ( char ) ( t . charAt ( x ) - ' a ' ) ; if ( idx [ ch ] >= occur [ ch ] . size ( ) ) { poss = false ; break ; } for ( int small = 0 ; small < ch ; small ++ ) { if ( idx [ small ] < occur [ small ] . size ( ) && occur [ small ] . get ( idx [ small ] ) < occur [ ch ] . get ( idx [ ch ] ) ) { poss = false ; break ; } } idx [ ch ] ++ ; } if ( poss ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { String s , t ; s = " hdecb " ; t = " cdheb " ; canTransform ( s , t ) ; } }
import java . util . * ; class GFG { static int make_cuts ( int arr [ ] , int n , int K ) { int ans = 0 ; Vector < Integer > poss = new Vector < Integer > ( ) ; int ce = 0 ; int co = 0 ; for ( int x = 0 ; x < n - 1 ; x ++ ) { if ( arr [ x ] % 2 == 0 ) ce ++ ; else co ++ ; if ( ce == co && co > 0 && ce > 0 ) { poss . add ( Math . abs ( arr [ x ] - arr [ x + 1 ] ) ) ; } } Collections . sort ( poss ) ; for ( int x : poss ) { if ( K >= x ) { ans ++ ; K -= x ; } else break ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 4 ; int arr [ ] = { 1 , 2 , 5 , 10 , 15 , 20 } ; System . out . print ( make_cuts ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void Arrange ( int arr [ ] , int n ) { int cost = 0 ; HashMap < Integer , Integer > index = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { index . put ( arr [ i ] , i ) ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] > arr [ i + 1 ] + arr [ n - 1 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else if ( i == n - 1 ) { if ( arr [ i ] > arr [ i - 1 ] + arr [ 0 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } else { if ( arr [ i ] > arr [ i - 1 ] + arr [ i + 1 ] ) { System . out . print ( " - 1" ) ; return ; } else { cost += Math . abs ( index . get ( arr [ i ] ) - i ) ; } } } System . out . print ( cost ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 1 , 3 } ; int N = arr . length ; Arrange ( arr , N ) ; } }
import java . io . * ; class GFG { static int XorSum ( int A [ ] , int B [ ] , int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { ans = ans ^ ( A [ i ] + B [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 6 , 0 , 0 , 3 , 3 } ; int B [ ] = { 0 , 5 , 6 , 5 , 0 , 3 } ; int N = A . length ; System . out . println ( XorSum ( A , B , N ) ) ; } }
import java . util . * ; class GFG { static int maxDistinctChar ( char [ ] s , int n , int k ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( ( int ) s [ i ] ) ) { freq . put ( ( int ) s [ i ] , freq . get ( ( int ) s [ i ] ) + 1 ) ; } else { freq . put ( ( int ) s [ i ] , 1 ) ; } } Vector < Integer > v = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : freq . entrySet ( ) ) { v . add ( it . getValue ( ) ) ; } Collections . sort ( v ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = Math . min ( v . get ( i ) - 1 , k ) ; v . set ( i , v . get ( i ) - mn ) ; k -= mn ; } if ( k > 0 ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mn = Math . min ( v . get ( i ) , k ) ; v . set ( i , v . get ( i ) - mn ) ; k -= mn ; } } int res = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( v . get ( i ) == 1 ) res ++ ; return res ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int N = S . length ( ) ; int K = 1 ; System . out . print ( maxDistinctChar ( S . toCharArray ( ) , N , K ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > primes = new Vector < > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] prime = new boolean [ 10005 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= 1000 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 1000 ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= 1000 ; p ++ ) if ( prime [ p ] ) primes . add ( p ) ; } static void generate ( int n , int k ) { Vector < Integer > prod = new Vector < > ( ) ; SieveOfEratosthenes ( ) ; int l = primes . size ( ) ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = i + 1 ; j < l ; j ++ ) { if ( primes . get ( i ) * primes . get ( j ) > 0 ) prod . add ( primes . get ( i ) * primes . get ( j ) ) ; } } Collections . sort ( prod ) ; int sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) sum += prod . get ( i ) ; if ( sum > n ) System . out . print ( " - 1" ) ; else { for ( int i = 0 ; i < k - 1 ; i ++ ) { System . out . print ( prod . get ( i ) + " , ▁ " ) ; } System . out . print ( n - sum + "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 52 , k = 5 ; generate ( n , k ) ; } }
import java . util . * ; class GFG { static int minCost ( int arr [ ] , int n ) { int [ ] [ ] sorted = new int [ n ] [ 2 ] ; int total_cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sorted [ i ] [ 0 ] = arr [ i ] ; sorted [ i ] [ 1 ] = i ; } Arrays . sort ( sorted , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; int overall_minimum = sorted [ 0 ] [ 0 ] ; boolean [ ] vis = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] && sorted [ i ] [ 1 ] == i ) continue ; ArrayList < Integer > v = new ArrayList < > ( ) ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; v . add ( sorted [ j ] [ 0 ] ) ; j = sorted [ j ] [ 1 ] ; } if ( v . size ( ) > 0 ) { int local_minimum = v . get ( 0 ) , result1 = 0 , result2 = 0 ; for ( int k = 1 ; k < v . size ( ) ; k ++ ) result1 += ( local_minimum * v . get ( k ) ) ; for ( int k = 0 ; k < v . size ( ) ; k ++ ) result2 += ( overall_minimum * v . get ( k ) ) ; result2 += ( overall_minimum * local_minimum ) ; total_cost += Math . min ( result1 , result2 ) ; } } return total_cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 9 , 7 , 6 } ; int n = arr . length ; System . out . print ( minCost ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int maximizeFinalElement ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; if ( arr [ 0 ] != 1 ) arr [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] > 1 ) { arr [ i ] = arr [ i - 1 ] + 1 ; } } return arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 3 , 1 , 3 , 4 } ; int max = maximizeFinalElement ( arr , n ) ; System . out . print ( max ) ; } }
import java . util . * ; class GFG { static int findLeastDist ( int A [ ] , int N ) { Arrays . sort ( A ) ; if ( N % 2 == 0 ) { return A [ ( N - 1 ) / 2 ] ; } else { return A [ N / 2 ] ; } } public static void main ( String [ ] args ) { int A [ ] = { 4 , 1 , 5 , 10 , 2 } ; int N = A . length ; System . out . print ( " ( " + findLeastDist ( A , N ) + " , ▁ " + 0 + " ) " ) ; } }
class GFG { static int maxSubMatSum ( int [ ] [ ] mat ) { int n = mat . length ; int m = mat [ 0 ] . length ; int i , j ; int [ ] [ ] dp = new int [ n ] [ m ] ; dp [ n - 1 ] [ m - 1 ] = mat [ n - 1 ] [ m - 1 ] ; int res = dp [ n - 1 ] [ m - 1 ] ; for ( i = m - 2 ; i >= 0 ; i -- ) { dp [ n - 1 ] [ i ] = mat [ n - 1 ] [ i ] + dp [ n - 1 ] [ i + 1 ] ; res = Math . max ( res , dp [ n - 1 ] [ i ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { dp [ i ] [ m - 1 ] = mat [ i ] [ m - 1 ] + dp [ i + 1 ] [ m - 1 ] ; res = Math . max ( res , dp [ i ] [ m - 1 ] ) ; } for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = m - 2 ; j >= 0 ; j -- ) { dp [ i ] [ j ] = mat [ i ] [ j ] + dp [ i ] [ j + 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j + 1 ] ; res = Math . max ( res , dp [ i ] [ j ] ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { - 6 , - 4 , - 1 } , { - 3 , 2 , 4 } , { 2 , 5 , 8 } } ; System . out . print ( maxSubMatSum ( mat ) ) ; } }
import java . lang . * ; import java . util . * ; class Node { Node left , right ; int data ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } class AlphaScore { Node root ; AlphaScore ( ) { root = null ; } static long sum = 0 , total_sum = 0 ; static long mod = 1000000007 ; public static long getAlphaScore ( Node node ) { if ( node . left != null ) getAlphaScore ( node . left ) ; sum = ( sum + node . data ) % mod ; total_sum = ( total_sum + sum ) % mod ; if ( node . right != null ) getAlphaScore ( node . right ) ; return total_sum ; } public static Node constructBST ( int [ ] arr , int start , int end , Node root ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; if ( root == null ) root = new Node ( arr [ mid ] ) ; root . left = constructBST ( arr , start , mid - 1 , root . left ) ; root . right = constructBST ( arr , mid + 1 , end , root . right ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 11 , 12 } ; int length = arr . length ; Arrays . sort ( arr ) ; Node root = null ; root = constructBST ( arr , 0 , length - 1 , root ) ; System . out . println ( getAlphaScore ( root ) ) ; } }
import java . util . * ; class GFG { public static boolean canMadeEqual ( int [ ] A , int [ ] B , int n ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] != B [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 1 , 3 , 2 } ; int n = A . length ; if ( canMadeEqual ( A , B , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int maxMinDifference ( int arr [ ] , int n , int k ) { if ( k >= n - 1 ) return 0 ; Arrays . sort ( arr ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; for ( int i = k , j = n - 1 ; i >= 0 ; -- i , -- j ) { ans = Math . min ( arr [ j ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 6 , 11 , 15 } ; int N = arr . length ; int K = 3 ; System . out . print ( maxMinDifference ( arr , N , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int inversionCount ( String s ) { int [ ] freq = new int [ 26 ] ; int inv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int temp = 0 ; for ( int j = 0 ; j < ( int ) ( s . charAt ( i ) - ' a ' ) ; j ++ ) temp += freq [ j ] ; inv += ( i - temp ) ; freq [ s . charAt ( i ) - ' a ' ] ++ ; } return inv ; } static boolean haveRepeated ( String S1 , String S2 ) { int [ ] freq = new int [ 26 ] ; for ( char i : S1 . toCharArray ( ) ) { if ( freq [ i - ' a ' ] > 0 ) return true ; freq [ i - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) freq [ i ] = 0 ; for ( char i : S2 . toCharArray ( ) ) { if ( freq [ i - ' a ' ] > 0 ) return true ; freq [ i - ' a ' ] ++ ; } return false ; } static void checkToMakeEqual ( String S1 , String S2 ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < S1 . length ( ) ; i ++ ) { freq [ S1 . charAt ( i ) - ' a ' ] ++ ; } boolean flag = false ; for ( int i = 0 ; i < S2 . length ( ) ; i ++ ) { if ( freq [ S2 . charAt ( i ) - ' a ' ] == 0 ) { flag = true ; break ; } freq [ S2 . charAt ( i ) - ' a ' ] -- ; } if ( flag == true ) { System . out . println ( " No " ) ; return ; } int invCount1 = inversionCount ( S1 ) ; int invCount2 = inversionCount ( S2 ) ; if ( invCount1 == invCount2 || ( invCount1 & 1 ) == ( invCount2 & 1 ) || haveRepeated ( S1 , S2 ) ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S1 = " abbca " , S2 = " acabb " ; checkToMakeEqual ( S1 , S2 ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static class pair { int first , second ; pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void max_sum ( int ar [ ] , int n , int q , int x [ ] , int y [ ] ) { int ans = 0 , i ; PriorityQueue < pair > pq = new PriorityQueue < > ( ( a , b ) -> Integer . compare ( a . second , b . second ) ) ; for ( i = 0 ; i < n ; i ++ ) pq . add ( new pair ( ar [ i ] , 1 ) ) ; for ( i = 0 ; i < q ; i ++ ) pq . add ( new pair ( y [ i ] , x [ i ] ) ) ; while ( n > 0 ) { pair pr = pq . peek ( ) ; pq . poll ( ) ; ans += pr . first * Math . min ( n , pr . second ) ; n -= pr . second ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 200 , 100 , 200 , 300 } ; int n = ar . length ; int q = 2 ; int x [ ] = { 2 , 3 } ; int y [ ] = { 100 , 90 } ; max_sum ( ar , n , q , x , y ) ; } }
import java . io . * ; class GFG { static void sortArr ( int a [ ] , int n ) { int i , k ; k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; k = ( int ) Math . pow ( 2 , k ) ; while ( k > 0 ) { for ( i = 0 ; i + k < n ; i ++ ) if ( a [ i ] > a [ i + k ] ) { int tmp = a [ i ] ; a [ i ] = a [ i + k ] ; a [ i + k ] = tmp ; } k = k / 2 ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 20 , 30 , 40 , 36 , 33 , 25 , 15 , 10 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
import java . util . Arrays ; class GFG { static void maximumSum ( int arr [ ] , int n , int k ) { int elt = n / k ; int sum = 0 ; Arrays . sort ( arr ) ; int count = 0 ; int i = n - 1 ; while ( count < k ) { sum += arr [ i ] ; i -- ; count ++ ; } count = 0 ; i = 0 ; while ( count < k ) { sum += arr [ i ] ; i += elt - 1 ; count ++ ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 1 , 13 , 7 , 17 , 6 , 5 } ; int K = 2 ; int size = Arr . length ; maximumSum ( Arr , size , K ) ; } }
class GFG { static int MAX = ( int ) ( 1e5 + 5 ) ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_missing ( pair [ ] interval ) { int [ ] vis = new int [ MAX ] ; for ( int i = 0 ; i < interval . length ; ++ i ) { int start = interval [ i ] . first ; int end = interval [ i ] . second ; vis [ start ] ++ ; vis [ end + 1 ] -- ; } for ( int i = 1 ; i < MAX ; i ++ ) { vis [ i ] += vis [ i - 1 ] ; if ( vis [ i ] == 0 ) { System . out . print ( i + "NEW_LINE"); return ; } } } public static void main ( String [ ] args ) { pair [ ] interval = { new pair ( 0 , 14 ) , new pair ( 86 , 108 ) , new pair ( 22 , 30 ) , new pair ( 5 , 17 ) } ; find_missing ( interval ) ; } }
import java . util . * ; import java . io . * ; class GFG { static class Pair implements Comparable < Pair > { int start , end ; Pair ( int s , int e ) { start = s ; end = e ; } public int compareTo ( Pair p ) { return this . start - p . start ; } } static void findMissing ( ArrayList < Pair > interval ) { Collections . sort ( interval ) ; int mx = 0 ; for ( int i = 0 ; i < interval . size ( ) ; ++ i ) { if ( interval . get ( i ) . start > mx ) { System . out . println ( mx ) ; return ; } else mx = Math . max ( mx , interval . get ( i ) . end + 1 ) ; } System . out . println ( mx ) ; } public static void main ( String [ ] args ) { ArrayList < Pair > interval = new ArrayList < > ( ) ; interval . add ( new Pair ( 0 , 14 ) ) ; interval . add ( new Pair ( 86 , 108 ) ) ; interval . add ( new Pair ( 22 , 30 ) ) ; interval . add ( new Pair ( 5 , 17 ) ) ; findMissing ( interval ) ; } }
class GFG { static int build_num ( int [ ] bit ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) if ( bit [ i ] == 1 ) ans += ( 1 << i ) ; ans += 32 ; return ans ; } static int maximumOR ( int [ ] arr , int n , int k ) { int bit [ ] = new int [ 32 ] ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( bit [ i ] == 0 && k > 0 ) { int temp = build_num ( bit ) ; int temp1 = temp ; int val = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( temp1 < ( temp arr [ j ] ) ) { temp1 = temp | arr [ j ] ; val = arr [ j ] ; } } if ( val != - 1 ) { k -- ; for ( int j = 0 ; j < 32 ; j ++ ) { bit [ j ] ++ ; } } } } return build_num ( bit ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 9 , 7 , 19 } ; int k = 3 ; int n = arr . length ; System . out . println ( maximumOR ( arr , n , k ) ) ; } }
class GFG { static void smallest_String ( char [ ] s , int m ) { int n = s . length ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int distance = s [ i ] - ' a ' ; if ( distance == 0 ) a [ i ] = 0 ; else a [ i ] = 26 - distance ; } for ( int i = 0 ; i < n ; i ++ ) { if ( m >= a [ i ] ) { s [ i ] = ' a ' ; m = m - a [ i ] ; } } m = m % 26 ; s [ n - 1 ] = ( char ) ( s [ n - 1 ] + m ) ; System . out . print ( String . valueOf ( s ) ) ; } public static void main ( String [ ] args ) { String str = " aazzx " ; int m = 6 ; smallest_String ( str . toCharArray ( ) , m ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int find_maximum_jobs ( int N , ArrayList < ArrayList < Integer > > ranges ) { ArrayList < Integer > queue = new ArrayList < Integer > ( ) ; Collections . sort ( ranges , new Comparator < ArrayList < Integer > > ( ) { @ Override public int compare ( ArrayList < Integer > o1 , ArrayList < Integer > o2 ) { return o1 . get ( 0 ) . compareTo ( o2 . get ( 0 ) ) ; } } ) ; int min_day = ranges . get ( 0 ) . get ( 0 ) ; int max_day = 0 ; for ( int i = 0 ; i < N ; i ++ ) max_day = Math . max ( max_day , ranges . get ( i ) . get ( 1 ) ) ; int index = 0 , count_jobs = 0 ; for ( int i = min_day ; i <= max_day ; i ++ ) { while ( index < ranges . size ( ) && ranges . get ( index ) . get ( 0 ) <= i ) { queue . add ( ranges . get ( index ) . get ( 1 ) ) ; index ++ ; } Collections . sort ( queue ) ; while ( queue . size ( ) > 0 && queue . get ( 0 ) < i ) queue . remove ( 0 ) ; if ( queue . size ( ) == 0 ) continue ; count_jobs ++ ; queue . remove ( 0 ) ; } return count_jobs ; } public static void main ( String [ ] args ) { int N = 5 ; ArrayList < ArrayList < Integer > > ranges = new ArrayList < ArrayList < Integer > > ( ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; ranges . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 3 ) ) ) ; System . out . println ( find_maximum_jobs ( N , ranges ) ) ; } }
import java . util . Arrays ; class GFG { public static void minBox ( int arr [ ] , int n ) { int box = n ; Arrays . sort ( arr ) ; int curr_box = 0 , next_box = 1 ; while ( curr_box < n && next_box < n ) { if ( arr [ curr_box ] < arr [ next_box ] ) { box -- ; curr_box ++ ; next_box ++ ; } else if ( arr [ curr_box ] == arr [ next_box ] ) next_box ++ ; } System . out . println ( box ) ; } public static void main ( String args [ ] ) { int [ ] size = { 1 , 2 , 3 } ; int n = size . length ; minBox ( size , n ) ; } }
import java . util . * ; class GFG { static int countReductions ( ArrayList < int [ ] > v , int K ) { int sum = 0 ; for ( int [ ] i : v ) { sum += i [ 0 ] ; } if ( sum <= K ) { return 0 ; } Collections . sort ( v , ( a , b ) -> Math . abs ( b [ 0 ] - b [ 1 ] ) - Math . abs ( a [ 0 ] - a [ 1 ] ) ) ; int i = 0 ; while ( sum > K && i < v . size ( ) ) { sum -= ( v . get ( i ) [ 0 ] - v . get ( i ) [ 1 ] ) ; i ++ ; } if ( sum <= K ) return i ; return - 1 ; } public static void main ( String [ ] args ) { int N = 4 , K = 25 ; ArrayList < int [ ] > v = new ArrayList < > ( ) ; v . add ( new int [ ] { 10 , 5 } ) ; v . add ( new int [ ] { 20 , 9 } ) ; v . add ( new int [ ] { 12 , 10 } ) ; v . add ( new int [ ] { 4 , 2 } ) ; System . out . println ( countReductions ( v , K ) ) ; } }
class GFG { static node head1 = null ; static node head2 = null ; static class node { int data ; node next ; } ; static void setData ( node head ) { node tmp ; tmp = head ; while ( tmp != null ) { System . out . print ( tmp . data + " ▁ - > ▁ " ) ; tmp = tmp . next ; } } static node getData ( node head , int num ) { node temp = new node ( ) ; node tail = head ; temp . data = num ; temp . next = null ; if ( head == null ) { head = temp ; tail = temp ; } else { while ( tail != null ) { if ( tail . next == null ) { tail . next = temp ; tail = tail . next ; } tail = tail . next ; } } return head ; } static node mergelists ( ) { node tail = head1 ; while ( tail != null ) { if ( tail . next == null && head2 != null ) { tail . next = head2 ; break ; } tail = tail . next ; } return head1 ; } static void sortlist ( ) { node curr = head1 ; node temp = head1 ; while ( curr . next != null ) { temp = curr . next ; while ( temp != null ) { if ( temp . data < curr . data ) { int t = temp . data ; temp . data = curr . data ; curr . data = t ; } temp = temp . next ; } curr = curr . next ; } } public static void main ( String [ ] args ) { head1 = getData ( head1 , 4 ) ; head1 = getData ( head1 , 7 ) ; head1 = getData ( head1 , 5 ) ; head2 = getData ( head2 , 2 ) ; head2 = getData ( head2 , 1 ) ; head2 = getData ( head2 , 8 ) ; head2 = getData ( head2 , 1 ) ; head1 = mergelists ( ) ; sortlist ( ) ; setData ( head1 ) ; } }
import java . util . * ; class GFG { static double findMedian ( int a [ ] , int n ) { if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } static void kStrongest ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; double median = findMedian ( arr , n ) ; int [ ] diff = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { diff [ i ] = ( int ) Math . abs ( median - arr [ i ] ) ; } int i = 0 , j = n - 1 ; while ( k > 0 ) { if ( diff [ i ] > diff [ j ] ) { System . out . print ( arr [ i ] + " ▁ " ) ; i ++ ; } else { System . out . print ( arr [ j ] + " ▁ " ) ; j -- ; } k -- ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int k = 3 ; int n = arr . length ; kStrongest ( arr , n , k ) ; } }
import java . util . * ; class GFG { static boolean sorting_possible ( int a [ ] , int b [ ] , int n ) { boolean sorted = true ; int type1 = 0 , type0 = 0 , i ; for ( i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { sorted = false ; break ; } } for ( i = 0 ; i < n ; i ++ ) { if ( b [ i ] == 0 ) type0 ++ ; else type1 ++ ; } if ( sorted ) return true ; else if ( type1 == n type0 == n ) return false ; else return true ; } public static void main ( String [ ] args ) { int a [ ] = { 15 , 1 , 2 , 17 , 6 } ; int b [ ] = { 1 , 1 , 0 , 1 , 1 } ; int n = a . length ; boolean res = sorting_possible ( a , b , n ) ; if ( res ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . ArrayList ; import java . util . HashSet ; import java . util . List ; import java . util . Set ; class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static final int infi = 1000000000 ; static class Node { int vertexNumber ; List < Pair > children ; Node ( int vertexNumber ) { this . vertexNumber = vertexNumber ; children = new ArrayList < > ( ) ; } void add_child ( int vNumber , int length ) { Pair p = new Pair ( vNumber , length ) ; children . add ( p ) ; } } static int [ ] dijkstraDist ( List < Node > g , int s , int [ ] path ) { int [ ] dist = new int [ g . size ( ) ] ; boolean [ ] visited = new boolean [ g . size ( ) ] ; for ( int i = 0 ; i < g . size ( ) ; i ++ ) { visited [ i ] = false ; path [ i ] = - 1 ; dist [ i ] = infi ; } dist [ s ] = 0 ; path [ s ] = - 1 ; int current = s ; Set < Integer > sett = new HashSet < > ( ) ; while ( true ) { visited [ current ] = true ; for ( int i = 0 ; i < g . get ( current ) . children . size ( ) ; i ++ ) { int v = g . get ( current ) . children . get ( i ) . first ; if ( visited [ v ] ) continue ; sett . add ( v ) ; int alt = dist [ current ] + g . get ( current ) . children . get ( i ) . second ; if ( alt < dist [ v ] ) { dist [ v ] = alt ; path [ v ] = current ; } } sett . remove ( current ) ; if ( sett . isEmpty ( ) ) break ; int minDist = infi ; int index = 0 ; for ( int a : sett ) { if ( dist [ a ] < minDist ) { minDist = dist [ a ] ; index = a ; } } current = index ; } return dist ; } void printPath ( int [ ] path , int i , int s ) { if ( i != s ) { if ( path [ i ] == - 1 ) { System . out . println ( " Path ▁ not ▁ found ! ! " ) ; return ; } printPath ( path , path [ i ] , s ) ; System . out . print ( path [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { List < Node > v = new ArrayList < > ( ) ; int n = 4 , s = 0 , e = 5 ; for ( int i = 0 ; i < n ; i ++ ) { Node a = new Node ( i ) ; v . add ( a ) ; } v . get ( 0 ) . add_child ( 1 , 1 ) ; v . get ( 0 ) . add_child ( 2 , 4 ) ; v . get ( 1 ) . add_child ( 2 , 2 ) ; v . get ( 1 ) . add_child ( 3 , 6 ) ; v . get ( 2 ) . add_child ( 3 , 3 ) ; int [ ] path = new int [ v . size ( ) ] ; int [ ] dist = dijkstraDist ( v , s , path ) ; for ( int i = 0 ; i < dist . length ; i ++ ) { if ( dist [ i ] == infi ) { System . out . printf ( " % d ▁ and ▁ % d ▁ are ▁ not ▁ " + "connectedNEW_LINE", i, s); continue ; } System . out . printf ( " Distance ▁ of ▁ % dth ▁ vertex ▁ " + "from source vertex %d is: %dNEW_LINE", i, s, dist[i]); } } }
import java . util . * ; class GFG { public static void maximizeval10 ( int [ ] a , int n , int k ) { int increments = 0 ; int ans = 0 ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ans += ( a [ i ] / 10 ) ; if ( a [ i ] == 1000 ) continue ; else { v . add ( 10 - a [ i ] % 10 ) ; increments += ( 100 - ( ( a [ i ] ) / 10 ) - 1 ) ; } } Collections . sort ( v ) ; int sum = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v . get ( i ) ; if ( sum <= k ) { ans ++ ; } else break ; } if ( sum < k ) { int remaining = k - sum ; ans += Math . min ( increments , remaining / 10 ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 4 ; int X = 6 ; int A [ ] = { 4 , 8 , 8 , 8 } ; maximizeval10 ( A , N , X ) ; } }
import java . util . * ; class GFG { static int findMinDif ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; return ( arr [ N - 1 ] - arr [ N - 2 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 9 , 5 , 10 } ; int N = arr . length ; System . out . println ( findMinDif ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int partition ( int arr [ ] , int l , int h ) { int pivot = arr [ l ] ; int i = l + 1 ; int j = h ; while ( i <= j ) { while ( i <= h && arr [ i ] < pivot ) { i ++ ; } while ( j > l && arr [ j ] > pivot ) { j -- ; } if ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i ++ ; j -- ; } else i ++ ; } arr [ l ] = arr [ j ] ; arr [ j ] = pivot ; return j ; } static void sortArray ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; int pivot = partition ( arr , l , h ) ; sortArray ( arr , l , pivot - 1 ) ; sortArray ( arr , pivot + 1 , h ) ; } static int findMaxIntervals ( int start [ ] , int end [ ] , int n , int R ) { int ans = 0 ; int prev = 0 ; int currActive = 0 ; int i = 0 ; int j = 0 ; if ( start [ 0 ] > 0 ) ans ++ ; while ( i < n && j < n ) { if ( start [ i ] < end [ j ] ) { i ++ ; currActive ++ ; } else if ( start [ i ] > end [ j ] ) { j ++ ; currActive -- ; } else { i ++ ; j ++ ; } if ( currActive == 0 ) { ans ++ ; } } if ( end [ n - 1 ] < R ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int R , N ; R = 10 ; N = 3 ; int start [ ] = new int [ ] { 2 , 5 , 8 } ; int end [ ] = new int [ ] { 3 , 9 , 10 } ; sortArray ( start , 0 , N - 1 ) ; sortArray ( end , 0 , N - 1 ) ; System . out . print ( findMaxIntervals ( start , end , N , R ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int number ( int [ ] a , int n , int p , int k ) { Arrays . sort ( a ) ; int [ ] pre = new int [ n ] ; int val , i , j , ans = 0 ; pre [ 0 ] = a [ 0 ] ; if ( pre [ 0 ] <= p ) ans = 1 ; for ( i = 1 ; i < k - 1 ; i ++ ) { pre [ i ] = pre [ i - 1 ] + a [ i ] ; if ( pre [ i ] <= p ) ans = i + 1 ; } pre [ k - 1 ] = a [ k - 1 ] ; for ( i = k - 1 ; i < n ; i ++ ) { if ( i >= k ) { pre [ i ] += pre [ i - k ] + a [ i ] ; } if ( pre [ i ] <= p ) ans = i + 1 ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 2 , 4 , 3 , 5 , 7 } ; int p = 11 ; int k = 2 ; System . out . println ( number ( arr , n , p , k ) ) ; } }
import java . util . * ; class GFG { static void sieveOfEratosthenes ( boolean prime [ ] , int n ) { prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } } static void sortSpecialPrimes ( int arr [ ] , int n ) { int maxVal = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ maxVal + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; sieveOfEratosthenes ( prime , maxVal ) ; Vector < Integer > list = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] && prime [ arr [ i ] - 2 ] ) { list . add ( arr [ i ] ) ; arr [ i ] = - 1 ; } } Collections . sort ( list ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) System . out . print ( list . get ( j ++ ) + " ▁ " ) ; else System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 31 , 5 , 2 , 1 , 7 } ; int n = arr . length ; sortSpecialPrimes ( arr , n ) ; } }
import java . util . Arrays ; import java . util . Collections ; public class GFG { static int maxProfit ( Integer [ ] arr ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int ans = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] - ( 1 * i ) ) > 0 ) ans += ( arr [ i ] - ( 1 * i ) ) ; if ( ( arr [ i ] - ( 1 * i ) ) == 0 ) break ; } return ans ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 6 , 6 , 6 } ; System . out . println ( maxProfit ( arr ) ) ; } }
import java . util . Arrays ; class GFG { public static void check_vector ( int [ ] A , int n , int [ ] p ) { int [ ] pos = new int [ A . length ] ; for ( int i = 0 ; i < p . length ; i ++ ) { pos [ p [ i ] - 1 ] = 1 ; } int flag = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( pos [ i ] == 0 ) continue ; int j = i ; while ( j < n && pos [ j ] != 0 ) ++ j ; Arrays . sort ( A , i , j + 1 ) ; i = j ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( A [ i ] > A [ i + 1 ] ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 2 , 1 } ; int p [ ] = { 1 , 2 } ; check_vector ( A , A . length , p ) ; } }
import java . util . * ; class GFG { static void minimumCost ( int arr [ ] , int N , int M ) { Arrays . sort ( arr ) ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = arr [ i ] + pref [ i - 1 ] ; } for ( int i = M ; i < N ; i ++ ) { pref [ i ] += pref [ i - M ] ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( pref [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 19 , 3 , 4 , 4 , 2 , 6 , 7 , 8 } ; int M = 2 ; int N = arr . length ; minimumCost ( arr , N , M ) ; } }
import java . util . * ; class GFG { static int uniqueSum ( int A [ ] , int R [ ] [ ] , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int ans = 0 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . contains ( A [ i ] ) ) ans += A [ i ] ; s . add ( A [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 0 , 0 , 0 , 0 , 0 } ; int R [ ] [ ] = { { 1 , 3 } , { 4 , 6 } , { 3 , 4 } , { 3 , 3 } } ; int N = A . length ; int M = R . length ; System . out . print ( uniqueSum ( A , R , N , M ) ) ; } }
import java . util . * ; class GFG { static void kthpair ( int n , int k , int arr [ ] ) { int i , t = 0 ; Arrays . sort ( arr ) ; -- k ; for ( i = 0 ; i < n ; i += t ) { for ( t = 1 ; arr [ i ] == arr [ i + t ] ; ++ t ) ; if ( t * n > k ) break ; k = k - t * n ; } System . out . print ( arr [ i ] + " ▁ " + arr [ k / t ] ) ; } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; int arr [ ] = { 3 , 1 , 5 } ; kthpair ( n , k , arr ) ; } }
import java . util . * ; class GFG { static int minOperations ( String s ) { Integer freq [ ] = new Integer [ 26 ] ; Arrays . fill ( freq , 0 ) ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq [ s . charAt ( i ) - ' A ' ] ++ ; } Arrays . sort ( freq , Collections . reverseOrder ( ) ) ; int answer = n ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( n % i == 0 ) { int x = n / i ; int y = 0 ; for ( int j = 0 ; j < i ; j ++ ) { y += Math . min ( freq [ j ] , x ) ; } answer = Math . min ( answer , n - y ) ; } } return answer ; } public static void main ( String [ ] args ) { String s = " BBC " ; System . out . print ( minOperations ( s ) ) ; } }
import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { public static boolean fun ( int [ ] arr , int n , int k ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = i ; j < n ; j += k ) { v . add ( arr [ j ] ) ; } Collections . sort ( v ) ; int x = 0 ; for ( int j = i ; j < n ; j += k ) { arr [ j ] = v . get ( x ) ; x ++ ; } v . clear ( ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { return false ; } } return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 2 , 3 , 7 , 6 } ; int K = 2 ; int n = arr . length ; if ( fun ( arr , n , K ) ) { System . out . println ( " yes " ) ; } else { System . out . println ( " no " ) ; } } }
import java . util . * ; class GFG { static int get_permutation ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result += Math . abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; System . out . print ( get_permutation ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1000 ] [ 1000 ] ; static int minimumIncDec ( int arr [ ] , int N , int maxE , int minE ) { if ( N == 0 ) { return 0 ; } if ( dp [ N ] [ maxE ] != 0 ) return dp [ N ] [ maxE ] ; int ans = Integer . MAX_VALUE ; for ( int k = minE ; k <= maxE ; k ++ ) { int x = minimumIncDec ( arr , N - 1 , k , minE ) ; ans = Math . min ( ans , x + Math . abs ( arr [ N - 1 ] - k ) ) ; } dp [ N ] [ maxE ] = ans ; return dp [ N ] [ maxE ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . length ; int minE = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int maxE = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; System . out . print ( minimumIncDec ( arr , N , maxE , minE ) ) ; } }
class GFG { static void sortDiagonal ( int a [ ] [ ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void swap ( int [ ] [ ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i ] [ i2 ] ; a [ i ] [ i2 ] = a [ pos ] [ pos2 ] ; a [ pos ] [ pos2 ] = temp ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }
import java . util . * ; class GFG { static int make_permutation ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += Math . abs ( i + 1 - arr [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 8 , 1 , 1 } ; int n = arr . length ; System . out . print ( make_permutation ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSum ( int [ ] [ ] V , int n , int m ) { int ans = 0 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { ArrayList < int [ ] > aux = new ArrayList < > ( ) ; for ( int j = 0 ; j < m ; ++ j ) { aux . add ( new int [ ] { V [ i + 1 ] [ j ] , j } ) ; } Collections . sort ( aux , ( a , b ) -> b [ 0 ] - a [ 0 ] ) ; for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < m ; ++ k ) { if ( aux . get ( k ) [ 1 ] - j == 0 || Math . abs ( aux . get ( k ) [ 1 ] - j ) > 1 ) { V [ i ] [ j ] += aux . get ( k ) [ 0 ] ; break ; } } } } for ( int i = 0 ; i < m ; ++ i ) { ans = Math . max ( ans , V [ 0 ] [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] V = { { 1 , 2 , 3 , 4 } , { 8 , 7 , 6 , 5 } , { 10 , 11 , 12 , 13 } } ; int n = V . length ; int m = V [ 0 ] . length ; System . out . println ( maxSum ( V , n , m ) ) ; } }
import java . util . * ; class GFG { static int minimumProduct ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int product = 1 ; for ( int i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 1 , 7 , 8 } ; int n = arr . length ; System . out . print ( minimumProduct ( arr , n ) + "NEW_LINE"); } }
class GFG { static void sortMat ( int [ ] [ ] data , int row , int col ) { int size = row * col ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size - 1 ; j ++ ) { if ( data [ j / col ] [ j % col ] > data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ) { int temp = data [ j / col ] [ j % col ] ; data [ j / col ] [ j % col ] = data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] ; data [ ( j + 1 ) / col ] [ ( j + 1 ) % col ] = temp ; } } } } static void printMat ( int [ ] [ ] mat , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 5 , 4 , 7 } , { 1 , 3 , 8 } , { 2 , 9 , 6 } } ; int row = mat . length ; int col = mat [ 0 ] . length ; sortMat ( mat , row , col ) ; printMat ( mat , row , col ) ; } }
import java . util . * ; class GFG { static int minimiseMedian ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; return Math . abs ( arr [ n / 2 ] - arr [ ( n / 2 ) - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 25 , 35 , 50 } ; int n = arr . length ; System . out . println ( minimiseMedian ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class pair { int first ; char second ; pair ( int first , char second ) { this . first = first ; this . second = second ; } } static void overlap ( int [ ] [ ] v ) { int ans = 0 ; int count = 0 ; ArrayList < pair > data = new ArrayList < > ( ) ; for ( int i = 0 ; i < v . length ; i ++ ) { data . add ( new pair ( v [ i ] [ 0 ] , ' x ' ) ) ; data . add ( new pair ( v [ i ] [ 1 ] , ' y ' ) ) ; } Collections . sort ( data , ( a , b ) -> a . first - b . first ) ; for ( int i = 0 ; i < data . size ( ) ; i ++ ) { if ( data . get ( i ) . second == ' x ' ) count ++ ; if ( data . get ( i ) . second == ' y ' ) count -- ; ans = Math . max ( ans , count ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] [ ] v = { { 1 , 2 } , { 2 , 4 } , { 3 , 6 } } ; overlap ( v ) ; } }
import java . util . * ; class GFG { static boolean canBeEqual ( int a , int b , int c , int k ) { int [ ] arr = new int [ 3 ] ; arr [ 0 ] = a ; arr [ 1 ] = b ; arr [ 2 ] = c ; Arrays . sort ( arr ) ; int diff = 2 * arr [ 2 ] - arr [ 1 ] - arr [ 0 ] ; k = k - diff ; if ( k < 0 k % 3 != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int a1 = 6 , b1 = 3 , c1 = 2 , k1 = 7 ; if ( canBeEqual ( a1 , b1 , c1 , k1 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . length ; System . out . println ( findSum ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static boolean checkRectangles ( int [ ] arr , int n ) { boolean ans = true ; Arrays . sort ( arr ) ; int area = arr [ 0 ] * arr [ 4 * n - 1 ] ; for ( int i = 0 ; i < 2 * n ; i = i + 2 ) { if ( arr [ i ] != arr [ i + 1 ] arr [ 4 * n - i - 1 ] != arr [ 4 * n - i - 2 ] arr [ i ] * arr [ 4 * n - i - 1 ] != area ) { ans = false ; break ; } } if ( ans ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 2 , 1 , 2 , 4 , 4 , 8 } ; int n = 2 ; if ( checkRectangles ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static int findMin ( int arr [ ] , int n ) { int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; ans = Math . max ( ans , j - i ) ; i = j - 1 ; } return n - ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int countSubSeq ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; List < Integer > arr = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; ) { int count = 1 , x = a [ i ] ; i ++ ; while ( i < n && a [ i ] == x ) { count ++ ; i ++ ; } arr . add ( count ) ; } int m = arr . size ( ) ; n = Math . min ( m , k ) ; int count = 1 ; int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = m ; j >= 0 ; j -- ) { if ( j > m - i ) dp [ i ] [ j ] = 0 ; else { dp [ i ] [ j ] = dp [ i ] [ j + 1 ] + arr . get ( j ) * dp [ i - 1 ] [ j + 1 ] ; } } count = count + dp [ i ] [ 0 ] ; } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 3 , 5 } ; int n = a . length ; int k = 3 ; System . out . println ( countSubSeq ( a , n , k ) ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class GFG { public static int countUnvisited ( int n , int m ) { int X = ( m * n ) - m - n ; Queue < Integer > queue = new LinkedList < > ( ) ; queue . add ( X ) ; int count = 0 ; while ( ! queue . isEmpty ( ) ) { int curr = queue . poll ( ) ; count ++ ; if ( curr - m > 0 ) queue . add ( curr - m ) ; if ( curr - n > 0 ) queue . add ( curr - n ) ; } return count ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( countUnvisited ( n , m ) ) ; } }
import java . util . * ; class GFG { static String partOdd ( String s ) { String st = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 != 0 ) st += s . charAt ( i ) ; } return st ; } static String partEven ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 0 ) s += str . charAt ( i ) ; } return s ; } static boolean canBeMadeEqual ( String s1 , String s2 ) { char [ ] even1 = partEven ( s1 ) . toCharArray ( ) ; char [ ] even2 = partEven ( s2 ) . toCharArray ( ) ; char [ ] odd1 = partOdd ( s1 ) . toCharArray ( ) ; char [ ] odd2 = partOdd ( s2 ) . toCharArray ( ) ; Arrays . sort ( even1 ) ; Arrays . sort ( even2 ) ; Arrays . sort ( odd1 ) ; Arrays . sort ( odd2 ) ; String even_s1 = new String ( even1 ) ; String even_s2 = new String ( even2 ) ; String odd_s1 = new String ( odd1 ) ; String odd_s2 = new String ( odd2 ) ; if ( even_s1 . equals ( even_s2 ) && odd_s1 . equals ( odd_s2 ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = " cdab " ; String s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void sortStack ( Stack < Integer > input , int k ) { Stack < Integer > tmpStack = new Stack < Integer > ( ) ; while ( ! input . isEmpty ( ) ) { int tmp = input . peek ( ) ; input . pop ( ) ; while ( ! tmpStack . isEmpty ( ) ) { int tmpStackMod = tmpStack . peek ( ) % k ; int tmpMod = tmp % k ; if ( ( tmpStackMod > tmpMod ) || ( tmpStackMod == tmpMod && tmpStack . peek ( ) > tmp ) ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } else break ; } tmpStack . push ( tmp ) ; } while ( ! tmpStack . isEmpty ( ) ) { input . push ( tmpStack . peek ( ) ) ; tmpStack . pop ( ) ; } while ( ! input . empty ( ) ) { System . out . print ( input . peek ( ) + " ▁ " ) ; input . pop ( ) ; } } public static void main ( String args [ ] ) { Stack < Integer > input = new Stack < Integer > ( ) ; input . push ( 10 ) ; input . push ( 3 ) ; input . push ( 2 ) ; input . push ( 6 ) ; input . push ( 12 ) ; int k = 4 ; sortStack ( input , k ) ; } }
import java . util . * ; class GFG { static int maxLen ( int [ ] arr , int n ) { int c_sum = 0 ; Arrays . sort ( arr ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { c_sum += arr [ i ] ; if ( c_sum < 0 ) return i ; } return n ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , - 6 } ; int n = arr . length ; System . out . println ( maxLen ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int cntElements ( int arr [ ] , int n ) { int copy_arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) copy_arr [ i ] = arr [ i ] ; int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != copy_arr [ i ] ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 6 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( cntElements ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortWithRemainder ( int [ ] arr , int n , int k ) { ArrayList < ArrayList < Integer > > v = new ArrayList < ArrayList < Integer > > ( k ) ; for ( int i = 0 ; i < k ; i ++ ) v . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int t = arr [ i ] % k ; v . get ( t ) . add ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) { Collections . sort ( v . get ( i ) ) ; } int j = 0 ; for ( int i = 0 ; i < k ; i ++ ) { for ( int x : v . get ( i ) ) { arr [ j ] = x ; j ++ ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 7 , 2 , 6 , 12 , 3 , 33 , 46 } ; int n = arr . length ; int k = 4 ; sortWithRemainder ( arr , n , k ) ; } }
import java . util . * ; class Gfg { public static boolean customSort ( String s1 , String s2 ) { int count_a1 = 0 , count_b1 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == ' a ' ) { count_a1 ++ ; } else { count_b1 ++ ; } } int count_a2 = 0 , count_b2 = 0 ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { if ( s2 . charAt ( i ) == ' a ' ) { count_a2 ++ ; } else { count_b2 ++ ; } } if ( count_a1 * count_b2 > count_b1 * count_a2 ) { return true ; } else { return false ; } } public static String concatenateStrings ( String S [ ] , int N ) { String str = " " ; for ( int i = 0 ; i < N ; i ++ ) { str += S [ i ] ; } return str ; } public static int getMaxScore ( String S [ ] , int N ) { Arrays . sort ( S ) ; String combined_string = concatenateStrings ( S , N ) ; int final_score = 0 , count_a = 0 ; for ( int i = 0 ; i < combined_string . length ( ) ; i ++ ) { if ( combined_string . charAt ( i ) == ' a ' ) { count_a ++ ; } else { final_score += count_a ; } } return final_score ; } public static void main ( String [ ] args ) { String S [ ] = { " aa " , " bb " , " aab " , " bab " } ; int N = S . length ; System . out . println ( getMaxScore ( S , N ) - 10 ) ; } }
class GFG { static int x = - 1 ; static String [ ] heap = new String [ 1000 ] ; static void heapForm ( String k ) { x ++ ; heap [ x ] = k ; int child = x ; String tmp ; int index = x / 2 ; while ( index >= 0 ) { if ( heap [ index ] . compareTo ( heap [ child ] ) > 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ child ] ; heap [ child ] = tmp ; child = index ; index = index / 2 ; } else { break ; } } } static void heapSort ( ) { int left1 , right1 ; while ( x >= 0 ) { String k ; k = heap [ 0 ] ; System . out . print ( k + " ▁ " ) ; heap [ 0 ] = heap [ x ] ; x = x - 1 ; String tmp ; int index = 0 ; int length = x ; left1 = 1 ; right1 = left1 + 1 ; while ( left1 <= length ) { if ( heap [ index ] . compareTo ( heap [ left1 ] ) <= 0 && heap [ index ] . compareTo ( heap [ right1 ] ) <= 0 ) { break ; } else { if ( heap [ left1 ] . compareTo ( heap [ right1 ] ) < 0 ) { tmp = heap [ index ] ; heap [ index ] = heap [ left1 ] ; heap [ left1 ] = tmp ; index = left1 ; } else { tmp = heap [ index ] ; heap [ index ] = heap [ right1 ] ; heap [ right1 ] = tmp ; index = right1 ; } } left1 = 2 * left1 ; right1 = left1 + 1 ; } } } static void sort ( String k [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { heapForm ( k [ i ] ) ; } heapSort ( ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " banana " , " orange " , " apple " , " pineapple " , " berries " , " lichi " } ; int n = arr . length ; sort ( arr , n ) ; } }
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void UpdateArr ( int arr [ ] , int n ) { int i = 0 , j = n - 1 ; while ( i < j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; i += 2 ; j -= 2 ; } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; UpdateArr ( arr , n ) ; } }
import java . util . Arrays ; import java . io . * ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int a [ ] , int n ) { int ways = 1 ; Arrays . sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . length ; pre ( ) ; System . out . println ( CountPermutation ( a , n ) ) ; } }
import java . util . * ; class GFG { static int Next_greater ( int a [ ] , int n , int x ) { Arrays . sort ( a ) ; int low = 0 , high = n - 1 , ans = x + 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( a [ mid ] <= ans ) { if ( a [ mid ] == ans ) { ans ++ ; high = n - 1 ; } low = mid + 1 ; } else high = mid - 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 5 , 10 , 4 , 7 } , x = 4 ; int n = a . length ; System . out . println ( Next_greater ( a , n , x ) ) ; } }
class GFG { static int MAX = 100000 ; public static void xorOnSortedArray ( int [ ] arr , int n , int k , int x ) { int [ ] arr1 = new int [ MAX + 1 ] ; int [ ] arr2 = new int [ MAX + 1 ] ; int [ ] xor_val = new int [ MAX + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) arr1 [ arr [ i ] ] ++ ; for ( int i = 0 ; i <= MAX ; i ++ ) xor_val [ i ] = i ^ x ; while ( k -- > 0 ) { int count = 0 ; for ( int i = 0 ; i <= MAX ; i ++ ) { int store = arr1 [ i ] ; if ( arr1 [ i ] > 0 ) { if ( count % 2 == 0 ) { int div = ( int ) Math . ceil ( arr1 [ i ] / 2 ) ; arr1 [ i ] = arr1 [ i ] - div ; arr2 [ xor_val [ i ] ] += div ; } else if ( count % 2 != 0 ) { int div = arr1 [ i ] / 2 ; arr1 [ i ] = arr1 [ i ] - div ; arr2 [ xor_val [ i ] ] += div ; } } count = count + store ; } for ( int i = 0 ; i <= MAX ; i ++ ) { arr1 [ i ] = arr1 [ i ] + arr2 [ i ] ; arr2 [ i ] = 0 ; } } int min = Integer . MAX_VALUE ; int max = Integer . MIN_VALUE ; for ( int i = 0 ; i <= MAX ; i ++ ) { if ( arr1 [ i ] > 0 ) { if ( min > i ) min = i ; if ( max < i ) max = i ; } } System . out . println ( min + " ▁ " + max ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 605 , 986 } ; int n = arr . length ; int k = 548 , x = 569 ; xorOnSortedArray ( arr , n , k , x ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPairs ( int arr [ ] , int n , int k , int d ) { if ( n < 2 * k ) { System . out . print ( - 1 ) ; return ; } Vector < pair > pairs = new Vector < pair > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < k ; i ++ ) { if ( arr [ n - k + i ] - arr [ i ] >= d ) { pair p = new pair ( arr [ i ] , arr [ n - k + i ] ) ; pairs . add ( p ) ; } } if ( pairs . size ( ) < k ) { System . out . print ( - 1 ) ; return ; } for ( pair v : pairs ) { System . out . println ( " ( " + v . first + " , ▁ " + v . second + " ) " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 10 , 23 , 14 , 7 , 2 , 20 , 9 } ; int n = arr . length ; int k = 4 , d = 3 ; findPairs ( arr , n , k , d ) ; } }
import java . util . Arrays ; import java . io . * ; class GFG { static int pairs_count ( int arr [ ] , int n , int sum ) { int ans = 0 ; Arrays . sort ( arr ) ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( arr [ i ] + arr [ j ] < sum ) i ++ ; else if ( arr [ i ] + arr [ j ] > sum ) j -- ; else { int x = arr [ i ] , xx = i ; while ( ( i < j ) && ( arr [ i ] == x ) ) i ++ ; int y = arr [ j ] , yy = j ; while ( ( j >= i ) && ( arr [ j ] == y ) ) j -- ; if ( x == y ) { int temp = i - xx + yy - j - 1 ; ans += ( temp * ( temp + 1 ) ) / 2 ; } else ans += ( i - xx ) * ( yy - j ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 , 5 , - 1 } ; int n = arr . length ; int sum = 6 ; System . out . println ( pairs_count ( arr , n , sum ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > findSubarray ( int a [ ] , int k , int n ) { ArrayList < ArrayList < Integer > > vec = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { ArrayList < Integer > temp = new ArrayList < Integer > ( ) ; for ( int j = i ; j < i + k ; j ++ ) { temp . add ( a [ j ] ) ; } vec . add ( temp ) ; } Collections . sort ( vec , new Comparator < ArrayList < Integer > > ( ) { @ Override public int compare ( ArrayList < Integer > o1 , ArrayList < Integer > o2 ) { return o1 . get ( 0 ) . compareTo ( o2 . get ( 0 ) ) ; } } ) ; return vec . get ( vec . size ( ) - 1 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 3 , 2 , 5 } ; int k = 4 ; int n = a . length ; ArrayList < Integer > ans = findSubarray ( a , k , n ) ; for ( int it : ans ) { System . out . print ( it + " ▁ " ) ; } } }
import java . util . * ; class GFG { static class Pair { int first ; int second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } ; static int maxChainLen ( Pair p [ ] , int n ) { int l = 1 ; Arrays . sort ( p , new Comparator < Pair > ( ) { public int compare ( Pair a , Pair b ) { return a . second - b . second ; } } ) ; int s = p [ 0 ] . second ; for ( int i = 1 ; i < n ; i ++ ) { if ( p [ i ] . first > s ) { l ++ ; s = p [ i ] . second ; } } return l ; } public static void main ( String args [ ] ) { Pair p [ ] = new Pair [ 5 ] ; p [ 0 ] = new Pair ( 5 , 24 ) ; p [ 1 ] = new Pair ( 39 , 60 ) ; p [ 2 ] = new Pair ( 15 , 28 ) ; p [ 3 ] = new Pair ( 27 , 40 ) ; p [ 4 ] = new Pair ( 50 , 90 ) ; int n = p . length ; System . out . println ( maxChainLen ( p , n ) ) ; } }
import java . util . * ; class GFG { static void sortArray ( int a [ ] , int n ) { Vector < Integer > ans = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . add ( a [ i ] ) ; } Collections . sort ( ans ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans . get ( j ) ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 6 , - 3 , 8 , 4 , 1 } ; int n = arr . length ; sortArray ( arr , n ) ; } }
import static java . lang . Math . abs ; import java . util . * ; class GFG { static int solution ( Vector < Integer > arr , int x ) { Collections . sort ( arr ) ; long closestSum = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . size ( ) - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . size ( ) - 1 ; while ( ptr1 < ptr2 ) { int sum = arr . get ( i ) + arr . get ( ptr1 ) + arr . get ( ptr2 ) ; if ( abs ( x - sum ) < abs ( x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return ( int ) closestSum ; } public static void main ( String [ ] args ) { Vector arr = new Vector ( Arrays . asList ( - 1 , 2 , 1 , - 4 ) ) ; int x = 1 ; System . out . println ( solution ( arr , x ) ) ; } }
import java . io . * ; class GFG { static boolean IsSorted ( int [ ] A , int [ ] B , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x , y ; x = Math . max ( A [ i ] , B [ i ] ) ; y = Math . min ( A [ i ] , B [ i ] ) ; A [ i ] = x ; B [ i ] = y ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] <= A [ i - 1 ] B [ i ] <= B [ i - 1 ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 4 , 3 , 5 , 7 } ; int [ ] B = { 2 , 2 , 5 , 8 , 9 } ; int n = A . length ; if ( IsSorted ( A , B , n ) == true ) { System . out . println ( " True " ) ; } else { System . out . println ( " False " ) ; } } }
import java . util . * ; class GFG { static void K_multiple ( int a [ ] , int n , int k ) { Arrays . sort ( a ) ; HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] % k == 0 && ! s . contains ( a [ i ] / k ) ) a [ i ] % k != 0 ) s . add ( a [ i ] ) ; } for ( Integer i : s ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 3 , 4 , 5 , 6 , 10 } ; int k = 2 ; int n = a . length ; K_multiple ( a , n , k ) ; } }
import java . util . Arrays ; class GFG { static int maxWater ( int height [ ] , int n ) { int max = 0 ; int i = 0 , j = n - 1 ; while ( i < j ) { if ( height [ i ] < height [ j ] ) { max = Math . max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; } else if ( height [ j ] < height [ i ] ) { max = Math . max ( max , ( j - i - 1 ) * height [ j ] ) ; j -- ; } else { max = Math . max ( max , ( j - i - 1 ) * height [ i ] ) ; i ++ ; j -- ; } } return max ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 3 , 4 , 6 , 5 } ; int n = height . length ; System . out . print ( maxWater ( height , n ) ) ; } }
import java . util . * ; class GFG { static int maxSelectionCount ( int a [ ] , int n ) { int res = 0 ; Arrays . sort ( a ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = arr . length ; System . out . println ( maxSelectionCount ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void findCombinationsUtil ( int arr [ ] , int index , int n , int red_num ) { HashSet < Integer > s = new HashSet < > ( ) ; int sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num == 0 ) { for ( int i = 0 ; i < index ; i ++ ) { s . add ( arr [ i ] ) ; } for ( Integer itr : s ) { sum = sum + itr ; } if ( sum == n ) { for ( Integer i : s ) { System . out . print ( i + " ▁ " ) ; } System . out . println ( ) ; return ; } } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } static void findCombinations ( int n ) { int [ ] a = new int [ n ] ; findCombinationsUtil ( a , 0 , n , n ) ; } public static void main ( String arr [ ] ) { int n = 7 ; findCombinations ( n ) ; } }
class GFG { static boolean check ( int arr [ ] , int n ) { int modify = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] / 2 ; modify ++ ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] < arr [ i ] && arr [ i + 1 ] < arr [ i ] ) || ( arr [ i - 1 ] > arr [ i ] && arr [ i + 1 ] > arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i + 1 ] ) return false ; modify ++ ; } } if ( arr [ n - 1 ] < arr [ n - 2 ] ) modify ++ ; if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 6 , 9 , 12 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static long mod = 1000000007 ; static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static long powmod ( long a , long n ) { if ( n == 0 ) return 1 ; long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) return ( pt * a ) % mod ; else return pt ; } static long CountSubset ( int [ ] arr , int n ) { long ans = powmod ( 2 , n - 1 ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && arr [ j ] == arr [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + nCr ( l + r , l ) ) % mod ; j ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 } ; int n = arr . length ; System . out . println ( CountSubset ( arr , n ) ) ; } }
class GFG { static int MAX = 100000 ; static int findSumofEle ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { int hash [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr2 [ i ] ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } int maximumFreq = 0 ; for ( int i = 0 ; i < m ; i ++ ) { maximumFreq = Math . max ( maximumFreq , hash [ arr1 [ i ] ] ) ; } int sumOfElements = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sumOfElements += ( maximumFreq == hash [ arr1 [ i ] ] ) ? arr1 [ i ] : 0 ; } return sumOfElements ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 5 , 6 , 8 } ; int arr2 [ ] = { 4 , 10 } ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( findSumofEle ( arr1 , m , arr2 , n ) ) ; } }
import java . util . * ; class GFG { static void PrintBothArrays ( int a [ ] , int n ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) ; HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mpp . put ( a [ i ] , ( mpp . get ( a [ i ] ) == null ? 0 : mpp . get ( a [ i ] ) ) + 1 ) ; if ( mpp . get ( a [ i ] ) == 1 ) v1 . add ( a [ i ] ) ; else if ( mpp . get ( a [ i ] ) == 2 ) v2 . add ( a [ i ] ) ; else { System . out . println ( " Not ▁ possible " ) ; return ; } } Collections . sort ( v1 ) ; System . out . println ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) System . out . print ( v1 . get ( i ) + " ▁ " ) ; Collections . sort ( v2 ) ; Collections . reverse ( v2 ) ; System . out . println ( " Strictly decreasing array is : "); for ( int i = 0 ; i < v2 . size ( ) ; i ++ ) System . out . print ( v2 . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 7 , 2 , 7 , 3 , 3 , 1 , 4 } ; int n = a . length ; PrintBothArrays ( a , n ) ; } }
void selectionSort ( node head ) { node temp = head ; while ( temp ) { node min = temp ; node r = temp . next ; while ( r ) { if ( min . data > r . data ) min = r ; r = r . next ; } int x = temp . data ; temp . data = min . data ; min . data = x ; temp = temp . next ; } }
public class GFG { public static boolean check ( String str ) { int min = Integer . MAX_VALUE ; int max = Integer . MIN_VALUE ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int ascii = ( int ) str . charAt ( i ) ; if ( ascii < 96 ascii > 122 ) return false ; sum += ascii ; if ( min > ascii ) min = ascii ; if ( max < ascii ) max = ascii ; } min -= 1 ; int eSum = ( ( max * ( max + 1 ) ) / 2 ) - ( ( min * ( min + 1 ) ) / 2 ) ; return sum == eSum ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . ArrayList ; import java . util . Collections ; class GFG { static boolean isUgly ( int n ) { while ( n % 2 == 0 ) n = n / 2 ; while ( n % 3 == 0 ) n = n / 3 ; while ( n % 5 == 0 ) n = n / 5 ; if ( n == 1 ) return true ; return false ; } static void sortUglyNumbers ( int arr [ ] , int n ) { ArrayList < Integer > list = new ArrayList < > ( ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( isUgly ( arr [ i ] ) ) { list . add ( arr [ i ] ) ; arr [ i ] = - 1 ; } } Collections . sort ( list ) ; int j = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) System . out . print ( list . get ( j ++ ) + " ▁ " ) ; else System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 , 12 , 10 } ; int n = arr . length ; sortUglyNumbers ( arr , n ) ; } }
import java . util . Arrays ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void sortArr ( int arr [ ] , int n , int k ) { int prev = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { if ( prev != - 1 ) Arrays . sort ( arr , prev + 1 , i ) ; prev = i ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 13 , 3 , 7 , 8 , 21 , 13 , 12 } ; int n = arr . length ; int k = 2 ; sortArr ( arr , n , k ) ; } }
import java . util . * ; class GFG { static int findKth ( int arr [ ] , int n , int k ) { HashSet < Integer > missing = new HashSet < > ( ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { missing . add ( arr [ i ] ) ; } int maxm = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int minm = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; for ( int i = minm + 1 ; i < maxm ; i ++ ) { if ( ! missing . contains ( i ) ) { count ++ ; } if ( count == k ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 9 , 4 } ; int n = arr . length ; int k = 5 ; System . out . println ( findKth ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static void minOperation ( String S , int N , int K ) { if ( N % K != 0 ) { System . out . println ( " Not ▁ Possible " ) ; } else { int [ ] count = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { count [ ( S . charAt ( i ) - 97 ) ] ++ ; } int E = N / K ; Vector < Integer > greaterE = new Vector < > ( ) ; Vector < Integer > lessE = new Vector < > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( count [ i ] < E ) lessE . add ( E - count [ i ] ) ; else greaterE . add ( count [ i ] - E ) ; } Collections . sort ( greaterE ) ; Collections . sort ( lessE ) ; int mi = Integer . MAX_VALUE ; for ( int i = 0 ; i <= K ; i ++ ) { int set1 = i ; int set2 = K - i ; if ( greaterE . size ( ) >= set1 && lessE . size ( ) >= set2 ) { int step1 = 0 ; int step2 = 0 ; for ( int j = 0 ; j < set1 ; j ++ ) step1 += greaterE . get ( j ) ; for ( int j = 0 ; j < set2 ; j ++ ) step2 += lessE . get ( j ) ; mi = Math . min ( mi , Math . max ( step1 , step2 ) ) ; } } System . out . println ( mi ) ; } } public static void main ( String [ ] args ) { String S = " accb " ; int N = S . length ( ) ; int K = 2 ; minOperation ( S , N , K ) ; } }
import java . util . * ; class GFG { static long maxProd ( int arr [ ] , int n , int k ) { long product = 1 ; HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . containsKey ( arr [ i ] ) == false ) { product = product * arr [ i ] ; s . put ( arr [ i ] , 1 ) ; } else s . put ( arr [ i ] , s . get ( arr [ i ] ) + 1 ) ; } for ( int j = n - 1 ; j >= 0 && k > 0 ; j -- ) { if ( ( k > ( s . get ( arr [ j ] ) - 1 ) ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , s . get ( arr [ j ] ) - 1 ) ; k = k - s . get ( arr [ j ] ) + 1 ; s . put ( arr [ j ] , 0 ) ; } if ( k <= ( s . get ( arr [ j ] ) - 1 ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , k ) ; break ; } } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 } ; int n = arr . length ; int k = 2 ; System . out . println ( maxProd ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static int findAnswer ( int n , int [ ] arr ) { Arrays . sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 53 , 28 , 143 , 5 } ; int n = arr . length ; System . out . println ( findAnswer ( n , arr ) ) ; } }
import java . util . * ; class GFG { static int n = 4 ; static void merge ( int l , int r , int [ ] output ) { int l_in = l * n , r_in = ( ( l + r ) / 2 + 1 ) * n ; int l_c = ( ( l + r ) / 2 - l + 1 ) * n ; int r_c = ( r - ( l + r ) / 2 ) * n ; int l_arr [ ] = new int [ l_c ] , r_arr [ ] = new int [ r_c ] ; for ( int i = 0 ; i < l_c ; i ++ ) l_arr [ i ] = output [ l_in + i ] ; for ( int i = 0 ; i < r_c ; i ++ ) r_arr [ i ] = output [ r_in + i ] ; int l_curr = 0 , r_curr = 0 ; int in = l_in ; while ( l_curr + r_curr < l_c + r_c ) { if ( r_curr == r_c || ( l_curr != l_c && l_arr [ l_curr ] < r_arr [ r_curr ] ) ) { output [ in ] = l_arr [ l_curr ] ; l_curr ++ ; in ++ ; } else { output [ in ] = r_arr [ r_curr ] ; r_curr ++ ; in ++ ; } } } static void divide ( int l , int r , int [ ] output , int arr [ ] [ ] ) { if ( l == r ) { for ( int i = 0 ; i < n ; i ++ ) output [ l * n + i ] = arr [ l ] [ i ] ; return ; } divide ( l , ( l + r ) / 2 , output , arr ) ; divide ( ( l + r ) / 2 + 1 , r , output , arr ) ; merge ( l , r , output ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int k = arr . length ; int [ ] output = new int [ n * k ] ; divide ( 0 , k - 1 , output , arr ) ; for ( int i = 0 ; i < n * k ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; } }
class GfG { static void findNonPalinString ( char s [ ] ) { int freq [ ] = new int [ 26 ] ; int flag = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] != s [ 0 ] ) flag = 1 ; freq [ s [ i ] - ' a ' ] ++ ; } if ( flag == 0 ) System . out . println ( " - 1" ) ; else { for ( int i = 0 ; i < 26 ; i ++ ) for ( int j = 0 ; j < freq [ i ] ; j ++ ) System . out . print ( ( char ) ( ' a ' + i ) ) ; } } public static void main ( String [ ] args ) { String s = " abba " ; findNonPalinString ( s . toCharArray ( ) ) ; } }
import java . util . * ; class GFG { static int minOperations ( int n , int m , int k , int matrix [ ] [ ] ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 ] [ 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i ] [ j ] ; if ( matrix [ i ] [ j ] % k != mod ) { return - 1 ; } } } Arrays . sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( ( n * m ) / 2 ) - 1 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr [ i ] - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . length ; int m = matrix [ 0 ] . length ; int k = 2 ; System . out . println ( minOperations ( n , m , k , matrix ) ) ; } }
class GFG { static int countDistinct ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static int countDistinct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }
import java . util . Arrays ; class GfG { static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void printMaxMean ( int arr [ ] , int n ) { int newArr [ ] = new int [ n ] ; Arrays . sort ( arr , 0 , 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = arr . length ; printMaxMean ( arr , n / 2 ) ; } }
import java . io . * ; import java . util . * ; class GFG { static double average ( int arr [ ] , int n , int k ) { double total = 0 ; if ( 2 * k >= n ) return 0 ; Arrays . sort ( arr ) ; int start = k , end = n - k - 1 ; for ( int i = start ; i <= end ; i ++ ) total += arr [ i ] ; return ( total / ( n - 2 * k ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 4 , 5 , 6 } ; int n = arr . length ; int k = 2 ; System . out . println ( average ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static int minMovesToSort ( int arr [ ] , int n ) { int moves = 0 ; int i , mn = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > mn ) moves += arr [ i ] - mn ; } return moves ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 2 , 8 , 4 } ; int n = arr . length ; System . out . println ( minMovesToSort ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int min_sum ( int n , int k , int a [ ] ) { Arrays . sort ( a ) ; if ( a [ 0 ] < 0 ) return - 1 ; if ( k == 0 ) { if ( a [ 0 ] == a [ n - 1 ] ) return ( n * a [ 0 ] ) ; else return - 1 ; } else { int f = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int p = a [ i ] - a [ 0 ] ; if ( p % k == 0 ) continue ; else { f = 1 ; break ; } } if ( f > 0 ) return - 1 ; else { if ( k == 1 ) return n ; else return ( n * ( a [ 0 ] % k ) ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int K = 1 ; int N = arr . length ; System . out . println ( min_sum ( N , K , arr ) ) ; } }
public class GFG { static void merge ( int arr [ ] , int start , int mid , int end ) { int start2 = mid + 1 ; if ( arr [ mid ] <= arr [ start2 ] ) { return ; } while ( start <= mid && start2 <= end ) { if ( arr [ start ] <= arr [ start2 ] ) { start ++ ; } else { int value = arr [ start2 ] ; int index = start2 ; while ( index != start ) { arr [ index ] = arr [ index - 1 ] ; index -- ; } arr [ start ] = value ; start ++ ; mid ++ ; start2 ++ ; } } } static void mergeSort ( int arr [ ] , int l , int r ) { if ( l < r ) { int m = l + ( r - l ) / 2 ; mergeSort ( arr , l , m ) ; mergeSort ( arr , m + 1 , r ) ; merge ( arr , l , m , r ) ; } } static void printArray ( int A [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 , 7 } ; int arr_size = arr . length ; mergeSort ( arr , 0 , arr_size - 1 ) ; printArray ( arr , arr_size ) ; } }
import java . util . * ; class GfG { static int minCost ( int A [ ] , int n ) { int cost = 0 ; Arrays . sort ( A ) ; int K = A [ n / 2 ] ; for ( int i = 0 ; i < n ; ++ i ) cost += Math . abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { int tempCost = 0 ; K = A [ ( n / 2 ) - 1 ] ; for ( int i = 0 ; i < n ; ++ i ) tempCost += Math . abs ( A [ i ] - K ) ; cost = Math . min ( cost , tempCost ) ; } return cost ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 6 , 7 , 10 } ; int n = A . length ; System . out . println ( minCost ( A , n ) ) ; } }
import java . util . * ; class Solution { static void printArray ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int l = 0 , r = n - 1 , flag = 0 , i ; int k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 1 ; l = i ; } { for ( i = r ; i > r - k && i >= l ; i -- ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 0 ; r = i ; } k ++ ; } } public static void main ( String args [ ] ) { int n = 6 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; printArray ( arr , n ) ; } }
import java . util . * ; class GFG { static boolean prime [ ] = new boolean [ 100005 ] ; static void SieveOfEratosthenes ( int n ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n ; i += p ) { prime [ i ] = false ; } } } } static void sortPrimes ( int arr [ ] , int n ) { SieveOfEratosthenes ( 100005 ) ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { v . add ( arr [ i ] ) ; } } Comparator comparator = Collections . reverseOrder ( ) ; Collections . sort ( v , comparator ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { arr [ i ] = v . get ( j ++ ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 6 , 100 , 17 } ; int n = arr . length ; sortPrimes ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
import java . util . * ; class GFG { static class Node { int data ; Node next , prev ; } ; static int countPairs ( Node first , Node second , int value ) { int count = 0 ; while ( first != null && second != null && first != second && second . next != first ) { if ( ( first . data * second . data ) == value ) { count ++ ; first = first . next ; second = second . prev ; } else if ( ( first . data * second . data ) > value ) second = second . prev ; else first = first . next ; } return count ; } static int countTriplets ( Node head , int x ) { if ( head == null ) return 0 ; Node current , first , last ; int count = 0 ; last = head ; while ( last . next != null ) last = last . next ; for ( current = head ; current != null ; current = current . next ) { first = current . next ; count += countPairs ( first , last , x / current . data ) ; } return count ; } static Node insert ( Node head , int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = temp . prev = null ; if ( ( head ) == null ) ( head ) = temp ; else { temp . next = head ; ( head ) . prev = temp ; ( head ) = temp ; } return head ; } public static void main ( String args [ ] ) { Node head = null ; head = insert ( head , 9 ) ; head = insert ( head , 8 ) ; head = insert ( head , 6 ) ; head = insert ( head , 5 ) ; head = insert ( head , 4 ) ; head = insert ( head , 2 ) ; head = insert ( head , 1 ) ; int x = 8 ; System . out . println ( " Count ▁ = ▁ " + countTriplets ( head , x ) ) ; } }
public class GFG { static boolean isAlphabaticOrder ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) < s . charAt ( i - 1 ) ) { return false ; } } return true ; } static public void main ( String [ ] args ) { String s = " aabbbcc " ; if ( isAlphabaticOrder ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . io . * ; class GFG { static int rotateRec ( int n , int L [ ] , int B [ ] ) { int m = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . max ( L [ i ] , B [ i ] ) <= m ) m = Math . max ( L [ i ] , B [ i ] ) ; else if ( Math . min ( L [ i ] , B [ i ] ) <= m ) m = Math . min ( L [ i ] , B [ i ] ) ; else { return 0 ; } } return 1 ; } public static void main ( String [ ] args ) { int n = 3 ; int L [ ] = { 5 , 5 , 6 } ; int B [ ] = { 6 , 7 , 8 } ; if ( rotateRec ( n , L , B ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . Arrays ; class GFG { static void minDistance ( int n , int k , int point [ ] [ ] ) { for ( int i = 0 ; i < k ; i ++ ) Arrays . sort ( point [ i ] ) ; for ( int i = 0 ; i < k ; i ++ ) System . out . print ( point [ i ] [ ( int ) Math . ceil ( ( double ) ( n / 2 ) - 1 ) ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int k = 4 ; int point [ ] [ ] = { { 1 , 5 , 2 , 4 } , { 6 , 2 , 0 , 6 } , { 9 , 5 , 1 , 3 } , { 6 , 7 , 5 , 9 } } ; minDistance ( n , k , point ) ; } }
import java . util . * ; class GFG { static void printOrder ( int [ ] arr , int n , int k ) { int len1 = k , len2 = n - k ; int [ ] arr1 = new int [ k ] ; int [ ] arr2 = new int [ n - k ] ; for ( int i = 0 ; i < k ; i ++ ) arr1 [ i ] = arr [ i ] ; for ( int i = k ; i < n ; i ++ ) arr2 [ i - k ] = arr [ i ] ; Arrays . sort ( arr1 , 0 , k ) ; Arrays . sort ( arr2 , k , n - k ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < k ) arr [ i ] = arr1 [ i ] ; else { arr [ i ] = arr2 [ len2 - 1 ] ; len2 -- ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , - 1 } ; int k = 4 ; int n = arr . length ; printOrder ( arr , n , k ) ; } }
class GFG { static void findMaxNum ( int arr [ ] , int n ) { int [ ] hash = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 9 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < hash [ i ] ; j ++ ) System . out . print ( i ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 0 } ; int n = arr . length ; findMaxNum ( arr , n ) ; } }
import java . util . * ; public class GFG { static void sortK ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; } static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] arr = { 2 , 6 , 3 , 12 , 56 , 8 } ; int n = arr . length ; sortK ( arr , n , k ) ; System . out . println ( " Following ▁ is ▁ sorted ▁ array " ) ; printArray ( arr , n ) ; } }
import java . util . * ; class Geeks { static int distribution ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) count ++ ; return Math . min ( count , n / 2 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . length ; System . out . println ( distribution ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n ) { Set < Integer > pairs = new HashSet < Integer > ( ) ; boolean pair_exists = false ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] > 0 ) pairs . add ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) if ( pairs . contains ( - arr [ i ] ) ) { System . out . println ( arr [ i ] + " , ▁ " + - arr [ i ] ) ; pair_exists = true ; } } if ( pair_exists == false ) System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
import java . io . * ; class GFG { static int printTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ i ] + arr [ j ] + arr [ k ] < sum ) System . out . println ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ k ] ) ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 4 , 7 } ; int n = arr . length ; int sum = 12 ; printTriplets ( arr , n , sum ) ; } }
import java . util . * ; class GFG { public static int countTriplets ( int [ ] arr , int n , int a , int b ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= a && arr [ i ] + arr [ j ] + arr [ k ] <= b ) { ans ++ ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 } ; int n = arr . length ; int a = 8 , b = 16 ; System . out . println ( " " + countTriplets ( arr , n , a , b ) ) ; } }
import java . util . * ; class GFG { static boolean areSame ( int [ ] a , int [ ] b ) { if ( a . length != b . length ) return false ; HashMap < Integer , Integer > m1 , m2 ; m1 = new HashMap < Integer , Integer > ( ) ; m2 = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( m1 . containsKey ( a [ i ] ) ) { m1 . put ( a [ i ] , m1 . get ( a [ i ] ) + 1 ) ; } else { m1 . put ( a [ i ] , 1 ) ; } if ( m2 . containsKey ( b [ i ] ) ) { m2 . put ( b [ i ] , m2 . get ( b [ i ] ) + 1 ) ; } else { m2 . put ( b [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > x : m1 . entrySet ( ) ) { if ( ! m2 . containsKey ( x . getKey ( ) ) || m2 . get ( x . getKey ( ) ) != x . getValue ( ) ) return false ; } return true ; } public static void main ( String args [ ] ) { int [ ] a = { 7 , 7 , 5 } ; int [ ] b = { 7 , 7 , 5 } ; if ( areSame ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void findOptimalPairs ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; for ( int i = 0 , j = N - 1 ; i <= j ; i ++ , j -- ) System . out . print ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 6 , 5 , 1 } ; int N = arr . length ; findOptimalPairs ( arr , N ) ; } }
import java . io . * ; import java . util . Arrays ; import java . util . * ; class GFG { static int MaxTotalRectangleArea ( Integer [ ] a , int n ) { Arrays . sort ( a , Collections . reverseOrder ( ) ) ; int sum = 0 ; boolean flag = false ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ! flag ) { flag = true ; len = a [ i + 1 ] ; i ++ ; } else if ( ( a [ i ] == a [ i + 1 ] a [ i ] - a [ i + 1 ] == 1 ) && ( flag ) ) { sum = sum + a [ i + 1 ] * len ; flag = false ; i ++ ; } } return sum ; } public static void main ( String args [ ] ) { Integer [ ] a = { 10 , 10 , 10 , 10 , 11 , 10 , 11 , 10 , 9 , 9 , 8 , 8 } ; int n = a . length ; System . out . print ( MaxTotalRectangleArea ( a , n ) ) ; } }
class GfG { static class Node { int data ; Node next ; } static boolean isSortedDesc ( Node head ) { if ( head == null head . next == null ) return true ; return ( head . data > head . next . data && isSortedDesc ( head . next ) ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . next = null ; temp . data = data ; return temp ; } public static void main ( String [ ] args ) { Node head = newNode ( 7 ) ; head . next = newNode ( 5 ) ; head . next . next = newNode ( 4 ) ; head . next . next . next = newNode ( 3 ) ; if ( isSortedDesc ( head ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void evenOddInsertionSort ( int arr [ ] , int n ) { for ( int i = 2 ; i < n ; i ++ ) { int j = i - 2 ; int temp = arr [ i ] ; if ( ( ( i + 1 ) & 1 ) == 1 ) { while ( j >= 0 && temp >= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } else { while ( j >= 0 && temp <= arr [ j ] ) { arr [ j + 2 ] = arr [ j ] ; j -= 2 ; } arr [ j + 2 ] = temp ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . printf ( " % d ▁ " , arr [ i ] ) ; } System . out . printf ( "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 11 , 13 , 5 , 6 } ; int n = arr . length ; evenOddInsertionSort ( arr , n ) ; printArray ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void print ( ArrayList < Integer > a , int n ) { for ( int i = 0 ; i <= n ; i ++ ) System . out . print ( a . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } static void sort ( ArrayList < Integer > a , int n ) { for ( int i = n ; i >= 0 ; i -- ) for ( int j = n ; j > n - i ; j -- ) if ( a . get ( j ) > a . get ( j - 1 ) ) { int tempswap = a . get ( j ) ; a . remove ( j ) ; a . add ( j , a . get ( j - 1 ) ) ; a . remove ( j - 1 ) ; a . add ( j - 1 , tempswap ) ; } print ( a , n ) ; } public static void main ( String [ ] args ) { int n = 6 ; ArrayList < Integer > a = new ArrayList < Integer > ( ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 3 ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 5 ) ; a . add ( 3 ) ; sort ( a , n ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static void sort012 ( int arr [ ] , int n ) { int count0 = 0 , count1 = 0 ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; if ( arr [ i ] == 1 ) count1 ++ ; if ( arr [ i ] == 2 ) count2 ++ ; } for ( int i = 0 ; i < count0 ; i ++ ) arr [ i ] = 0 ; for ( int i = count0 ; i < ( count0 + count1 ) ; i ++ ) arr [ i ] = 1 ; for ( int i = ( count0 + count1 ) ; i < n ; i ++ ) arr [ i ] = 2 ; printArray ( arr , n ) ; } public static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 1 , 1 , 0 , 1 , 2 , 1 , 2 , 0 , 0 , 0 , 1 } ; int n = 12 ; sort012 ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { private final static int MAX = 100 ; public static String alternateSort ( String s1 ) { int n = s1 . length ( ) ; char [ ] s = s1 . toCharArray ( ) ; int [ ] lCount = new int [ MAX ] ; int [ ] uCount = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( Character . isUpperCase ( s [ i ] ) ) uCount [ s [ i ] - ' A ' ] ++ ; else lCount [ s [ i ] - ' a ' ] ++ ; } int i = 0 , j = 0 , k = 0 ; while ( k < n ) { while ( i < MAX && uCount [ i ] == 0 ) i ++ ; if ( i < MAX ) { s [ k ++ ] = ( char ) ( ' A ' + i ) ; uCount [ i ] -- ; } while ( j < MAX && lCount [ j ] == 0 ) j ++ ; if ( j < MAX ) { s [ k ++ ] = ( char ) ( ' a ' + j ) ; lCount [ j ] -- ; } } return ( new String ( s ) ) ; } public static void main ( String argc [ ] ) { String str = " bAwutndekWEdkd " ; System . out . println ( alternateSort ( str ) ) ; } }
import java . util . * ; class GFG { static void optimum_sequence_jobs ( int [ ] V , double P ) { int j = 1 , N = V . length - 1 ; double result = 0 ; PriorityQueue < Integer > Queue = new PriorityQueue < > ( ) ; for ( int i = 1 ; i <= N ; i ++ ) Queue . add ( V [ i ] ) ; while ( ! Queue . isEmpty ( ) ) { System . out . print ( Queue . peek ( ) + " ▁ " ) ; V [ j ++ ] = Queue . peek ( ) ; Queue . remove ( ) ; } for ( int i = N ; i >= 1 ; i -- ) result += Math . pow ( ( 1 - P ) , N - i ) * V [ i ] ; System . out . printf ( " % .2f ", result ); } public static void main ( String [ ] args ) { int [ ] V = { - 1 , 3 , 5 , 4 , 1 , 2 , 7 , 6 , 8 , 9 , 10 } ; double P = 0.10 ; optimum_sequence_jobs ( V , P ) ; } }
import java . io . * ; class GFG { static int distancesum ( int x [ ] , int y [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ; return sum ; } public static void main ( String [ ] args ) { int x [ ] = { - 1 , 1 , 3 , 2 } ; int y [ ] = { 5 , 6 , 5 , 3 } ; int n = x . length ; System . out . println ( distancesum ( x , y , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int distancesum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += ( arr [ i ] * i - sum ) ; sum += arr [ i ] ; } return res ; } static int totaldistancesum ( int x [ ] , int y [ ] , int n ) { return distancesum ( x , n ) + distancesum ( y , n ) ; } public static void main ( String [ ] args ) { int x [ ] = { - 1 , 1 , 3 , 2 } ; int y [ ] = { 5 , 6 , 5 , 3 } ; int n = x . length ; System . out . println ( totaldistancesum ( x , y , n ) ) ; } }
class GFG { static void sortRec ( int arr [ ] , int index , int k , int n ) { if ( k == 0 ) { for ( int i = 1 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( 999 ) ; return ; } else if ( k > 0 && index == 0 ) { index = n - 2 ; for ( int i = 1 ; i <= index ; i ++ ) { arr [ i - 1 ] = arr [ i ] ; } arr [ index ] = 999 ; } if ( index - 1 >= 0 && index + 1 < n && arr [ index - 1 ] > arr [ index + 1 ] ) { swap ( arr , index , index + 1 ) ; swap ( arr , index - 1 , index + 1 ) ; } else swap ( arr , index , index - 1 ) ; sortRec ( arr , index - 1 , k - 1 , n ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort ( int arr [ ] , int n ) { int index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 999 ) { index = i ; break ; } } if ( index == - 1 ) return ; sortRec ( arr , index , n , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 999 , 1 } ; int n = arr . length ; sort ( arr , n ) ; } }
class GFG { static void unsort ( int l , int r , int a [ ] , int k ) { if ( k < 1 l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { System . out . print ( " NO ▁ SOLUTION " ) ; return ; } int a [ ] = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }
class GFG { static int MAX_CHAR = 26 ; static void sortString ( String str ) { int charCount [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { charCount [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < charCount [ i ] ; j ++ ) { System . out . print ( ( char ) ( ' a ' + i ) ) ; } } } public static void main ( String [ ] args ) { String s = " alkasingh " ; sortString ( s ) ; } }
import java . util . Arrays ; class GFG { static void printMedian ( int arr [ ] , int n , int K ) { Arrays . sort ( arr ) ; System . out . print ( arr [ ( n + K ) / 2 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 2 , 8 } ; int k = 3 ; int n = arr . length ; printMedian ( arr , n , k ) ; } }
class GFG { static void swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } static void dualPivotQuickSort ( int [ ] arr , int low , int high ) { if ( low < high ) { int [ ] piv ; piv = partition ( arr , low , high ) ; dualPivotQuickSort ( arr , low , piv [ 0 ] - 1 ) ; dualPivotQuickSort ( arr , piv [ 0 ] + 1 , piv [ 1 ] - 1 ) ; dualPivotQuickSort ( arr , piv [ 1 ] + 1 , high ) ; } } static int [ ] partition ( int [ ] arr , int low , int high ) { if ( arr [ low ] > arr [ high ] ) swap ( arr , low , high ) ; int j = low + 1 ; int g = high - 1 , k = low + 1 , p = arr [ low ] , q = arr [ high ] ; while ( k <= g ) { if ( arr [ k ] < p ) { swap ( arr , k , j ) ; j ++ ; } else if ( arr [ k ] >= q ) { while ( arr [ g ] > q && k < g ) g -- ; swap ( arr , k , g ) ; g -- ; if ( arr [ k ] < p ) { swap ( arr , k , j ) ; j ++ ; } } k ++ ; } j -- ; g ++ ; swap ( arr , low , j ) ; swap ( arr , high , g ) ; return new int [ ] { j , g } ; } public static void main ( String [ ] args ) { int [ ] arr = { 24 , 8 , 42 , 75 , 29 , 77 , 38 , 57 } ; dualPivotQuickSort ( arr , 0 , 7 ) ; System . out . print ( " Sorted ▁ array : ▁ " ) ; for ( int i = 0 ; i < 8 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } }
class GFG { static void minMaxSelectionSort ( int [ ] arr , int n ) { for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { int min = arr [ i ] , max = arr [ i ] ; int min_i = i , max_i = i ; for ( int k = i ; k <= j ; k ++ ) { if ( arr [ k ] > max ) { max = arr [ k ] ; max_i = k ; } else if ( arr [ k ] < min ) { min = arr [ k ] ; min_i = k ; } } swap ( arr , i , min_i ) ; if ( arr [ min_i ] == max ) swap ( arr , j , min_i ) ; else swap ( arr , j , max_i ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 23 , 78 , 45 , 8 , 32 , 56 , 1 } ; int n = arr . length ; minMaxSelectionSort ( arr , n ) ; System . out . printf ( "Sorted array:NEW_LINE"); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } }
import java . io . * ; class GFG { static void mergeArray ( int a [ ] , int b [ ] , int n , int m ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { mx = Math . max ( mx , b [ i ] ) ; } mx ++ ; int i = 0 , j = 0 , k = 0 ; while ( i < n && j < m && k < ( n + m ) ) { int e1 = a [ i ] % mx ; int e2 = b [ j ] % mx ; if ( e1 <= e2 ) { if ( k < n ) a [ k ] += ( e1 * mx ) ; else b [ k - n ] += ( e1 * mx ) ; i ++ ; k ++ ; } else { if ( k < n ) a [ k ] += ( e2 * mx ) ; else b [ k - n ] += ( e2 * mx ) ; j ++ ; k ++ ; } } while ( i < n ) { int el = a [ i ] % mx ; if ( k < n ) a [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; i ++ ; k ++ ; } while ( j < m ) { int el = b [ j ] % mx ; if ( k < n ) b [ k ] += ( el * mx ) ; else b [ k - n ] += ( el * mx ) ; j ++ ; k ++ ; } for ( int in = 0 ; in < n ; in ++ ) a [ in ] = a [ in ] / mx ; for ( int in = 0 ; in < m ; in ++ ) b [ in ] = b [ in ] / mx ; return ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 5 , 6 , 8 , 12 } ; int b [ ] = { 1 , 4 , 9 , 13 } ; int n = a . length ; int m = b . length ; mergeArray ( a , b , n , m ) ; System . out . print ( " First ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; System . out . print ( " Second ▁ array ▁ : ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . print ( b [ i ] + " ▁ " ) ; System . out . println ( ) ; } }
class GFG { static void arrange ( int arr [ ] , int n , int x ) { for ( int i = 1 ; i < n ; i ++ ) { int diff = Math . abs ( arr [ i ] - x ) ; int j = i - 1 ; if ( Math . abs ( arr [ j ] - x ) > diff ) { int temp = arr [ i ] ; while ( j >= 0 && Math . abs ( arr [ j ] - x ) > diff ) { arr [ j + 1 ] = arr [ j ] ; j -- ; } arr [ j + 1 ] = temp ; } } } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . length ; int x = 7 ; arrange ( arr , n , x ) ; print ( arr , n ) ; } }
import java . util . Arrays ; class GfG { static boolean check ( String [ ] v , int len ) { int n = v . length ; char [ ] tempArray ; for ( int i = 0 ; i < n ; i ++ ) { tempArray = v [ i ] . toCharArray ( ) ; Arrays . sort ( tempArray ) ; v [ i ] = new String ( tempArray ) ; } for ( int i = 0 ; i < len - 1 ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( v [ i ] . charAt ( j ) > v [ i + 1 ] . charAt ( j ) ) return false ; return true ; } public static void main ( String [ ] args ) { String [ ] v = { " ebcda " , " ihgfj " , " klmno " , " pqrst " , " yvwxu " } ; if ( check ( v , len ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static void printOrder ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } for ( int j = n - 1 ; j >= n / 2 ; j -- ) { System . out . print ( arr [ j ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 } ; int n = arr . length ; printOrder ( arr , n ) ; } }
class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Pair findSubArray ( int arr [ ] , int n ) { int start = 0 , end = 0 , min_sum = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = arr [ i ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = i ; } for ( int j = i + 1 ; j < n ; j ++ ) { curr_sum = curr_sum + arr [ j ] ; if ( min_sum > Math . abs ( curr_sum ) ) { min_sum = Math . abs ( curr_sum ) ; start = i ; end = j ; } } } Pair p = new Pair ( start , end ) ; return p ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 5 , 4 , - 6 , - 3 } ; int n = arr . length ; Pair point = findSubArray ( arr , n ) ; System . out . println ( " Subarray ▁ starting ▁ from ▁ " + point . first + " ▁ to ▁ " + point . second ) ; } }
import java . io . * ; import java . util . * ; class GFG { public String [ ] uniquePrefix ( String [ ] a ) { int size = a . length ; String [ ] res = new String [ size ] ; Arrays . sort ( a ) ; int j = 0 ; while ( j < Math . min ( a [ 0 ] . length ( ) - 1 , a [ 1 ] . length ( ) - 1 ) ) { if ( a [ 0 ] . charAt ( j ) == a [ 1 ] . charAt ( j ) ) j ++ ; else break ; } int ind = 0 ; res [ ind ++ ] = a [ 0 ] . substring ( 0 , j + 1 ) ; String temp_prefix = a [ 1 ] . substring ( 0 , j + 1 ) ; for ( int i = 1 ; i < size - 1 ; i ++ ) { j = 0 ; while ( j < Math . min ( a [ i ] . length ( ) - 1 , a [ i + 1 ] . length ( ) - 1 ) ) { if ( a [ i ] . charAt ( j ) == a [ i + 1 ] . charAt ( j ) ) j ++ ; else break ; } String new_prefix = a [ i ] . substring ( 0 , j + 1 ) ; if ( temp_prefix . length ( ) > new_prefix . length ( ) ) res [ ind ++ ] = temp_prefix ; else res [ ind ++ ] = new_prefix ; temp_prefix = a [ i + 1 ] . substring ( 0 , j + 1 ) ; } j = 0 ; String sec_last = a [ size - 2 ] ; String last = a [ size - 1 ] ; while ( j < Math . min ( sec_last . length ( ) - 1 , last . length ( ) - 1 ) ) { if ( sec_last . charAt ( j ) == last . charAt ( j ) ) j ++ ; else break ; } res [ ind ] = last . substring ( 0 , j + 1 ) ; return res ; } public static void main ( String [ ] args ) { GFG gfg = new GFG ( ) ; String [ ] input = { " zebra " , " dog " , " duck " , " dove " } ; String [ ] output = gfg . uniquePrefix ( input ) ; System . out . println ( " The ▁ shortest ▁ unique ▁ prefixes " + " ▁ in ▁ sorted ▁ order ▁ are ▁ : " ) ; for ( int i = 0 ; i < output . length ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static int findMinimum ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += arr [ i ] ; n = n - k ; } return res ; } static int findMaximum ( int arr [ ] , int n , int k ) { int res = 0 , index = 0 ; for ( int i = n - 1 ; i >= index ; i -- ) { res += arr [ i ] ; index += k ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 4 } ; int n = arr . length ; int k = 2 ; Arrays . sort ( arr ) ; System . out . println ( findMinimum ( arr , n , k ) + " ▁ " + findMaximum ( arr , n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void mergeArrays ( int a [ ] , int b [ ] , int n , int m ) { Map < Integer , Boolean > mp = new HashMap < Integer , Boolean > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( a [ i ] , true ) ; } for ( int i = 0 ; i < m ; i ++ ) { mp . put ( b [ i ] , true ) ; } for ( Map . Entry < Integer , Boolean > me : mp . entrySet ( ) ) { System . out . print ( me . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 5 , 7 } , b [ ] = { 2 , 4 , 6 , 8 } ; int size = a . length ; int size1 = b . length ; mergeArrays ( a , b , size , size1 ) ; } }
class GFG { static void printInSortedOrder ( String arr [ ] , int n ) { int index [ ] = new int [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) { index [ i ] = i ; } for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . compareTo ( arr [ index [ j ] ] ) > 0 ) { min = j ; } } if ( min != i ) { int temp = index [ min ] ; index [ min ] = index [ i ] ; index [ i ] = temp ; } } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ index [ i ] ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String arr [ ] = { " geeks " , " quiz " , " geeks " , " for " } ; int n = 4 ; printInSortedOrder ( arr , n ) ; } }
import java . util . * ; class GFG { static int MAX_SIZE = 10 ; static void sortArrayUsingCounts ( int arr [ ] , int n ) { int [ ] count = new int [ MAX_SIZE ] ; for ( int i = 0 ; i < n ; i ++ ) { count [ arr [ i ] ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX_SIZE ; i ++ ) { while ( count [ i ] > 0 ) { arr [ index ++ ] = i ; count [ i ] -- ; } } } static void removeAndPrintResult ( int arr [ ] , int n , int ind1 , int ind2 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i != ind1 && i != ind2 ) { System . out . print ( arr [ i ] ) ; } } } static boolean largest3Multiple ( int arr [ ] , int n ) { int sum = accumulate ( arr , 0 , n ) ; if ( sum % 3 == 0 ) { return true ; } sortArrayUsingCounts ( arr , n ) ; int remainder = sum % 3 ; if ( remainder == 1 ) { int [ ] rem_2 = new int [ 2 ] ; rem_2 [ 0 ] = - 1 ; rem_2 [ 1 ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 1 ) { removeAndPrintResult ( arr , n , i , - 1 ) ; return true ; } if ( arr [ i ] % 3 == 2 ) { if ( rem_2 [ 0 ] == - 1 ) { rem_2 [ 0 ] = i ; } else if ( rem_2 [ 1 ] == - 1 ) { rem_2 [ 1 ] = i ; } } } if ( rem_2 [ 0 ] != - 1 && rem_2 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_2 [ 0 ] , rem_2 [ 1 ] ) ; return true ; } } else if ( remainder == 2 ) { int [ ] rem_1 = new int [ 2 ] ; rem_1 [ 0 ] = - 1 ; rem_1 [ 1 ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 3 == 2 ) { removeAndPrintResult ( arr , n , i , - 1 ) ; return true ; } if ( arr [ i ] % 3 == 1 ) { if ( rem_1 [ 0 ] == - 1 ) { rem_1 [ 0 ] = i ; } else if ( rem_1 [ 1 ] == - 1 ) { rem_1 [ 1 ] = i ; } } } if ( rem_1 [ 0 ] != - 1 && rem_1 [ 1 ] != - 1 ) { removeAndPrintResult ( arr , n , rem_1 [ 0 ] , rem_1 [ 1 ] ) ; return true ; } } System . out . print ( " Not ▁ possible " ) ; return false ; } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 4 , 1 , 1 , 1 , 3 } ; int n = arr . length ; largest3Multiple ( arr , n ) ; } }
class GFG { static int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) { temp [ k ++ ] = arr [ i ++ ] ; } else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) { temp [ k ++ ] = arr [ i ++ ] ; } while ( j <= right ) { temp [ k ++ ] = arr [ j ++ ] ; } for ( i = left ; i <= right ; i ++ ) { arr [ i ] = temp [ i ] ; } return inv_count ; } static int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int mergeSort ( int arr [ ] , int array_size ) { int [ ] temp = new int [ array_size ] ; return _mergeSort ( arr , temp , 0 , array_size - 1 ) ; } static int minSwapToReachArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] - 1 ) - i > 2 ) { return - 1 ; } } int numOfInversion = mergeSort ( arr , N ) ; return numOfInversion ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 3 , 4 } ; int N = arr . length ; int res = minSwapToReachArr ( arr , N ) ; System . out . println ( res == - 1 ? "Not PossibleNEW_LINE" : res); } }
import java . util . Arrays ; public class GFG { static void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] *= - 1 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . length ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }
import java . util . * ; class GFG { static void bucketSort ( Vector < Double > arr , int n ) { @ SuppressWarnings ( " unchecked " ) Vector < Double > b [ ] = new Vector [ n ] ; for ( int i = 0 ; i < b . length ; i ++ ) b [ i ] = new Vector < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { b [ bi ] . add ( arr . get ( i ) ) ; } for ( int i = 0 ; i < n ; i ++ ) Collections . sort ( b [ i ] ) ; int index = 0 ; arr . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < b [ i ] . size ( ) ; j ++ ) arr . add ( b [ i ] . get ( j ) ) ; } static void sortMixed ( double arr [ ] , int n ) { Vector < Double > Neg = new Vector < > ( ) ; Vector < Double > Pos = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) Neg . add ( - 1 * arr [ i ] ) ; else Pos . add ( arr [ i ] ) ; } bucketSort ( Neg , ( int ) Neg . size ( ) ) ; bucketSort ( Pos , ( int ) Pos . size ( ) ) ; for ( int i = 0 ; i < Neg . size ( ) ; i ++ ) arr [ i ] = - 1 * Neg . get ( Neg . size ( ) - 1 - i ) ; for ( int j = Neg . size ( ) ; j < n ; j ++ ) arr [ j ] = Pos . get ( j - Neg . size ( ) ) ; } public static void main ( String [ ] args ) { double arr [ ] = { - 0.897 , 0.565 , 0.656 , - 0.1234 , 0 , 0.3434 } ; int n = arr . length ; sortMixed ( arr , n ) ; System . out . print ( "Sorted array is NEW_LINE"); for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
class GFG { static final int MAX = 1000000 ; static int ksmallest ( int arr [ ] , int n , int k ) { int b [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { b [ arr [ i ] ] = 1 ; } for ( int j = 1 ; j < MAX ; j ++ ) { if ( b [ j ] != 1 ) { k -- ; } if ( k != 1 ) { return j ; } } return Integer . MAX_VALUE ; } public static void main ( String [ ] args ) { int k = 1 ; int arr [ ] = { 1 } ; int n = arr . length ; System . out . println ( ksmallest ( arr , n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void mergeTwoHalf ( int [ ] A , int n ) { Arrays . sort ( A ) ; } static public void main ( String [ ] args ) { int [ ] A = { 2 , 3 , 8 , - 1 , 7 , 10 } ; int n = A . length ; mergeTwoHalf ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static int minDiff ( int arr [ ] , int n , int k ) { int result = Integer . MAX_VALUE ; Arrays . sort ( arr ) ; for ( int i = 0 ; i <= n - k ; i ++ ) result = Math . min ( result , arr [ i + k - 1 ] - arr [ i ] ) ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 100 , 300 , 200 , 1000 , 20 , 30 } ; int n = arr . length ; int k = 3 ; System . out . println ( minDiff ( arr , n , k ) ) ; } }
class GFG { public static boolean isEven ( int n ) { return ( n % 2 == 0 ) ; } public static void main ( String [ ] args ) { int n = 101 ; if ( isEven ( n ) == true ) System . out . print ( " Even " ) ; else System . out . print ( " Odd " ) ; } }
import java . io . * ; class GFG { static int countBits ( int a ) { int count = 0 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) count += 1 ; a = a >> 1 ; } return count ; } static void insertionSort ( int arr [ ] , int aux [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { int key1 = aux [ i ] ; int key2 = arr [ i ] ; int j = i - 1 ; while ( j >= 0 && aux [ j ] < key1 ) { aux [ j + 1 ] = aux [ j ] ; arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } aux [ j + 1 ] = key1 ; arr [ j + 1 ] = key2 ; } } static void sortBySetBitCount ( int arr [ ] , int n ) { int aux [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = countBits ( arr [ i ] ) ; insertionSort ( arr , aux , n ) ; } static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; sortBySetBitCount ( arr , n ) ; printArr ( arr , n ) ; } }
import java . io . * ; class GFG { static void findSurpasser ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; System . out . print ( count + " ▁ " ) ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 5 , 3 , 0 , 8 , 1 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; System . out . println ( " Surpasser ▁ Count ▁ of " + " ▁ array ▁ is ▁ " ) ; findSurpasser ( arr , n ) ; } }
class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int max_product = Integer . MIN_VALUE ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) max_product = Math . max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ; return max_product ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 3 , 5 , 6 , 20 } ; int n = arr . length ; ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . println ( " No ▁ Triplet ▁ Exists " ) ; else System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
import java . util . * ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int [ ] leftMin = new int [ n ] ; int [ ] rightMin = new int [ n ] ; int [ ] leftMax = new int [ n ] ; int [ ] rightMax = new int [ n ] ; Arrays . fill ( leftMin , - 1 ) ; Arrays . fill ( leftMax , - 1 ) ; Arrays . fill ( rightMax , - 1 ) ; Arrays . fill ( rightMin , - 1 ) ; int max_product = Integer . MIN_VALUE ; int max_sum = arr [ 0 ] ; int min_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { leftMax [ i ] = max_sum ; if ( arr [ i ] > max_sum ) max_sum = arr [ i ] ; leftMin [ i ] = min_sum ; if ( arr [ i ] < min_sum ) min_sum = arr [ i ] ; } max_sum = arr [ n - 1 ] ; min_sum = arr [ n - 1 ] ; for ( int j = n - 2 ; j >= 0 ; j -- ) { rightMax [ j ] = max_sum ; if ( arr [ j ] > max_sum ) max_sum = arr [ j ] ; rightMin [ j ] = min_sum ; if ( arr [ j ] < min_sum ) min_sum = arr [ j ] ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { int max1 = Math . max ( arr [ i ] * leftMax [ i ] * rightMax [ i ] , arr [ i ] * leftMin [ i ] * rightMin [ i ] ) ; int max2 = Math . max ( arr [ i ] * leftMax [ i ] * rightMin [ i ] , arr [ i ] * leftMin [ i ] * rightMax [ i ] ) ; max_product = Math . max ( max_product , Math . max ( max1 , max2 ) ) ; } return max_product ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 3 , - 6 , - 7 , 0 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . println ( " No ▁ Triplet ▁ Exists " ) ; else System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
import java . util . Arrays ; class GFG { static int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) { return - 1 ; } Arrays . sort ( arr ) ; return Math . max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 3 , 5 , 6 , - 20 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) { System . out . println ( " No ▁ Triplet ▁ Exists " ) ; } else { System . out . println ( " Maximum ▁ product ▁ is ▁ " + max ) ; } } }
import java . util . * ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int maxA = Integer . MIN_VALUE , maxB = Integer . MIN_VALUE , maxC = Integer . MIN_VALUE ; int minA = Integer . MAX_VALUE , minB = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; if ( arr [ i ] < minA ) { minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) minB = arr [ i ] ; } return Math . max ( minA * minB * maxA , maxA * maxB * maxC ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , - 4 , 3 , - 6 , 7 , 0 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . print ( " No ▁ Triplet ▁ Exists " ) ; else System . out . print ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }
import java . util . * ; class GFG { static int getMax ( int arr [ ] [ ] , int n , int q ) { int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , arr [ i ] [ q ] ) ; } return maxi ; } static void sortDatesUtil ( int arr [ ] [ ] , int n , int q ) { int maxi = getMax ( arr , n , q ) ; int p = 1 ; while ( maxi > 0 ) { int [ ] cnt = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ ( arr [ i ] [ q ] / p ) % 10 ] ++ ; } for ( int i = 1 ; i < 10 ; i ++ ) { cnt [ i ] += cnt [ i - 1 ] ; } int [ ] [ ] ans = new int [ n ] [ 3 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int lastDigit = ( arr [ i ] [ q ] / p ) % 10 ; for ( int j = 0 ; j < 3 ; j ++ ) { ans [ cnt [ lastDigit ] - 1 ] [ j ] = arr [ i ] [ j ] ; } cnt [ lastDigit ] -- ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { arr [ i ] [ j ] = ans [ i ] [ j ] ; } } p *= 10 ; maxi /= 10 ; } } static void sortDates ( int dates [ ] [ ] , int n ) { sortDatesUtil ( dates , n , 0 ) ; sortDatesUtil ( dates , n , 1 ) ; sortDatesUtil ( dates , n , 2 ) ; } static void printArr ( int arr [ ] [ ] , int n ) { for ( int i = 0 ; i < 6 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int dates [ ] [ ] = { { 20 , 1 , 2014 } , { 25 , 3 , 2010 } , { 3 , 12 , 2000 } , { 18 , 11 , 2000 } , { 19 , 4 , 2015 } , { 9 , 7 , 2005 } } ; int n = dates . length ; sortDates ( dates , n ) ; System . out . print ( " Sorted Dates "); printArr ( dates , n ) ; } }
public class GFG { static int minMoves ( char a [ ] [ ] , char b [ ] [ ] , int K ) { int n = a . length ; int m = a [ 0 ] . length ; int cntOperations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { cntOperations ++ ; if ( i + K - 1 >= n j + K - 1 >= m ) { return - 1 ; } for ( int p = 0 ; p <= K - 1 ; p ++ ) { for ( int q = 0 ; q <= K - 1 ; q ++ ) { if ( a [ i + p ] [ j + q ] == '0' ) { a [ i + p ] [ j + q ] = '1' ; } else { a [ i + p ] [ j + q ] = '0' ; } } } } } } return cntOperations ; } public static void main ( String [ ] args ) { char A [ ] [ ] = { { '1' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; char B [ ] [ ] = { { '0' , '0' , '0' } , { '0' , '0' , '0' } , { '0' , '0' , '0' } } ; int K = 3 ; System . out . println ( minMoves ( A , B , K ) ) ; } }
import java . util . Arrays ; class GFG { public static int maxConsecutiveCnt ( int [ ] arr ) { int X = 0 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] <= ( X + 1 ) ) { X = X + arr [ i ] ; } else { break ; } } return X + 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 1 , 4 } ; System . out . println ( maxConsecutiveCnt ( arr ) ) ; } }
import java . util . HashMap ; class GFG { public static int maxLength ( int [ ] A , int [ ] B ) { int n = A . length ; int maxSize = 0 ; HashMap < Integer , Integer > pre = new HashMap < Integer , Integer > ( ) ; int diff = 0 ; pre . put ( 0 , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { diff += ( A [ i ] - B [ i ] ) ; if ( ! pre . containsKey ( diff ) ) { pre . put ( diff , i + 1 ) ; } else { maxSize = Math . max ( maxSize , i - pre . get ( diff ) ) ; } } return maxSize ; } public static void main ( String args [ ] ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] B = { 4 , 3 , 2 , 1 } ; System . out . println ( maxLength ( A , B ) ) ; } }
class GFG { public static int [ ] [ ] prefix_sum_row = new int [ 50 ] [ 51 ] ; public static int [ ] [ ] prefix_sum_col = new int [ 51 ] [ 50 ] ; public static boolean is_valid ( int r , int c , int size , int [ ] [ ] grid ) { int r_end = r + size , c_end = c + size ; int sum = 0 ; for ( int i = r , j = c ; i < r_end ; i ++ , j ++ ) { sum += grid [ i ] [ j ] ; } for ( int i = r ; i < r_end ; i ++ ) { if ( prefix_sum_row [ i ] [ c_end ] - prefix_sum_row [ i ] != sum ) { return false ; } } for ( int i = c ; i < c_end ; i ++ ) { if ( prefix_sum_col [ r_end ] [ i ] - prefix_sum_col [ r ] [ i ] != sum ) { return false ; } } int ad_sum = 0 ; for ( int i = r , j = c_end - 1 ; i < r_end ; i ++ , j -- ) { ad_sum += grid [ i ] [ j ] ; } return ad_sum == sum ; } public static int largestSquareValidMatrix ( int [ ] [ ] grid ) { int m = grid . length , n = grid [ 0 ] . length ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { prefix_sum_row [ i ] [ j ] = prefix_sum_row [ i ] [ j - 1 ] + grid [ i ] [ j - 1 ] ; } } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_sum_col [ i ] [ j ] = prefix_sum_col [ i - 1 ] [ j ] + grid [ i - 1 ] [ j ] ; } } for ( int size = Math . min ( m , n ) ; size > 1 ; size -- ) { for ( int i = 0 ; i <= m - size ; i ++ ) { for ( int j = 0 ; j <= n - size ; j ++ ) { if ( is_valid ( i , j , size , grid ) ) { return size ; } } } } return 1 ; } public static void main ( String args [ ] ) { int [ ] [ ] grid = { { 7 , 1 , 4 , 5 , 6 } , { 2 , 5 , 1 , 6 , 4 } , { 1 , 5 , 4 , 3 , 2 } , { 1 , 2 , 7 , 3 , 4 } } ; System . out . println ( largestSquareValidMatrix ( grid ) ) ; } }
import java . io . * ; class GFG { static int minimumPossibleProduct ( int K ) { int res = 1 ; int range = ( 1 << K ) - 1 ; for ( int i = 0 ; i < K ; i ++ ) { res *= ( range - 1 ) ; } res *= range ; return res ; } public static void main ( String [ ] args ) { int K = 3 ; System . out . println ( minimumPossibleProduct ( K ) ) ; } }
import java . util . * ; class GFG { static int maximizeSubset ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int maxSize = 0 ; int i = 0 ; int j = i + 2 ; while ( i < N - 2 ) { while ( j < N && arr [ i ] + arr [ i + 1 ] > arr [ j ] ) { j ++ ; } maxSize = Math . max ( maxSize , j - i ) ; i ++ ; j = Math . max ( j , i + 2 ) ; } return maxSize ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 4 , 1 , 6 , 9 , 5 , 3 } ; int N = arr . length ; System . out . print ( maximizeSubset ( arr , N ) + "NEW_LINE"); } }
class GFG { public static int maxShiftArrayValue ( int arr [ ] , int cap [ ] , int N ) { int sumVals = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sumVals += arr [ i ] ; } int maxCapacity = 0 ; for ( int i = 0 ; i < N ; i ++ ) { maxCapacity = Math . max ( cap [ i ] , maxCapacity ) ; } return Math . min ( maxCapacity , sumVals ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 } ; int cap [ ] = { 5 , 6 } ; int N = arr . length ; System . out . println ( maxShiftArrayValue ( arr , cap , N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int Find ( int [ ] parent , int a ) { return parent [ a ] = ( parent [ a ] == a ? a : Find ( parent , parent [ a ] ) ) ; } static void Union ( int [ ] parent , int [ ] rank , int [ ] minVal , int a , int b ) { a = Find ( parent , a ) ; b = Find ( parent , b ) ; if ( rank [ a ] == rank [ b ] ) rank [ a ] ++ ; if ( rank [ a ] > rank [ b ] ) { parent [ b ] = a ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ a ] = Math . min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ a ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ a ] = minVal [ b ] ; } else { minVal [ a ] = Math . max ( minVal [ a ] , minVal [ b ] ) ; } } else { parent [ a ] = b ; if ( minVal [ a ] >= 0 && minVal [ b ] >= 0 ) { minVal [ b ] = Math . min ( minVal [ a ] , minVal [ b ] ) ; } else if ( minVal [ a ] >= 0 && minVal [ b ] < 0 ) { minVal [ b ] = minVal [ a ] ; } else if ( minVal [ a ] < 0 && minVal [ b ] >= 0 ) { minVal [ b ] = minVal [ b ] ; } else { minVal [ b ] = Math . max ( minVal [ a ] , minVal [ b ] ) ; } } } static void findMinCost ( pair [ ] G , int [ ] cost , int N , int M ) { int [ ] parent = new int [ N + 1 ] ; int [ ] rank = new int [ N + 1 ] ; int [ ] minVal = new int [ N + 1 ] ; for ( int i = 1 ; i < N + 1 ; i ++ ) { parent [ i ] = i ; minVal [ i ] = cost [ i - 1 ] ; } for ( pair i : G ) { Union ( parent , rank , minVal , i . first , i . second ) ; } HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < N + 1 ; i ++ ) { s . add ( Find ( parent , i ) ) ; } pair min = new pair ( 0 , Integer . MAX_VALUE ) ; boolean flag = false ; for ( int i : s ) { if ( minVal [ i ] < 0 ) { flag = true ; } if ( min . second > minVal [ i ] ) { min = new pair ( i , minVal [ i ] ) ; } } int minCost = 0 ; if ( ! flag || ( flag && s . size ( ) == 1 ) ) { for ( int i : s ) { if ( i != min . first ) { minCost += ( minVal [ i ] + min . second ) ; } } System . out . print ( minCost + "NEW_LINE"); } else { System . out . print ( - 1 + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 6 ; pair [ ] G = { new pair ( 3 , 1 ) , new pair ( 2 , 3 ) , new pair ( 2 , 1 ) , new pair ( 4 , 5 ) , new pair ( 5 , 6 ) , new pair ( 6 , 4 ) } ; int [ ] cost = { 2 , 1 , 5 , 3 , 2 , 9 } ; int M = G . length ; findMinCost ( G , cost , N , M ) ; } }
import java . io . * ; class GFG { static int countPoints ( int D ) { int count = 0 ; for ( int x = 1 ; x * x < D * D ; x ++ ) { int y = ( int ) Math . sqrt ( ( D * D - x * x ) ) ; if ( x * x + y * y == D * D ) { count += 4 ; } } count += 4 ; return count ; } public static void main ( String [ ] args ) { int D = 5 ; System . out . println ( countPoints ( D ) ) ; } }
import java . io . * ; public class GFG { static void findTotalDistance ( String S , int N ) { int [ ] prefixDistance = new int [ N ] ; int [ ] suffixDistance = new int [ N ] ; int cnt = 0 ; boolean haveOne = false ; for ( int i = 0 ; i < N ; ++ i ) { if ( S . charAt ( i ) == '1' ) { haveOne = true ; cnt = 0 ; prefixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; prefixDistance [ i ] = cnt ; } else prefixDistance [ i ] = Integer . MAX_VALUE ; } haveOne = false ; for ( int i = N - 1 ; i >= 0 ; -- i ) { if ( S . charAt ( i ) == '1' ) { haveOne = true ; cnt = 0 ; suffixDistance [ i ] = 0 ; } else if ( haveOne ) { cnt ++ ; suffixDistance [ i ] = cnt ; } else suffixDistance [ i ] = Integer . MAX_VALUE ; } int sum = 0 ; for ( int i = 0 ; i < N ; ++ i ) { if ( S . charAt ( i ) == '0' ) { sum += Math . min ( prefixDistance [ i ] , suffixDistance [ i ] ) ; } } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { String S = "100100" ; int N = S . length ( ) ; findTotalDistance ( S , N ) ; } }
class GFG { public static int minimizeCost ( int A , int B , int C ) { int ans = A + B + C ; for ( int i = 1 ; i <= 2 * C ; i ++ ) { int j = 0 ; while ( i * j <= 2 * C ) { ans = Math . min ( ans , Math . abs ( A - i ) + Math . abs ( B - j ) + Math . abs ( i * j - C ) ) ; j ++ ; } } return ans ; } public static void main ( String args [ ] ) { int A = 19 , B = 28 , C = 522 ; System . out . print ( minimizeCost ( A , B , C ) ) ; } }
import java . io . * ; class GFG { static double averageSetBits ( int N , int K , int arr [ ] ) { double p = N ; double q = 0 ; for ( int i = 0 ; i < K ; i ++ ) { double _p = p , _q = q ; p = _p - _p * arr [ i ] / N + _q * arr [ i ] / N ; q = _q - _q * arr [ i ] / N + _p * arr [ i ] / N ; } return p ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , 2 , 3 } ; int K = arr . length ; System . out . println ( String . format ( " % .10f " , averageSetBits ( N , K , arr ) ) ) ; } }
import java . util . * ; class GFG { static void generateString ( int k1 , int k2 , char [ ] s ) { int C1s = 0 , C0s = 0 ; int flag = 0 ; Vector < Integer > pos = new Vector < Integer > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == '0' ) { C0s ++ ; if ( ( i + 1 ) % k1 != 0 && ( i + 1 ) % k2 != 0 ) { pos . add ( i ) ; } } else { C1s ++ ; } if ( C0s >= C1s ) { if ( pos . size ( ) == 0 ) { System . out . print ( - 1 ) ; flag = 1 ; break ; } else { int k = pos . get ( pos . size ( ) - 1 ) ; s [ k ] = '1' ; C0s -- ; C1s ++ ; pos . remove ( pos . size ( ) - 1 ) ; } } } if ( flag == 0 ) { System . out . print ( s ) ; } } public static void main ( String [ ] args ) { int K1 = 2 , K2 = 4 ; String S = "11000100" ; generateString ( K1 , K2 , S . toCharArray ( ) ) ; } }
import java . util . * ; import java . lang . * ; class Codechef { static void findRange ( int arr [ ] , int N , int K ) { int L = Integer . MIN_VALUE ; int R = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int l = ( int ) Math . ceil ( 1.0 * arr [ i ] * K / ( i + 1 ) ) ; int r = ( int ) Math . ceil ( ( 1.0 + arr [ i ] ) * K / ( i + 1 ) ) - 1 ; L = Math . max ( L , l ) ; R = Math . min ( R , r ) ; } System . out . println ( L + " ▁ " + R ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 9 , 11 } ; int K = 10 ; int N = arr . length ; findRange ( arr , N , K ) ; } }
import java . io . * ; class GFG { static void maximizeProduct ( int N ) { int MSB = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; int X = 1 << MSB ; int Y = N - ( 1 << MSB ) ; for ( int i = 0 ; i < MSB ; i ++ ) { if ( ( N & ( 1 << i ) ) == 0 ) { X += 1 << i ; Y += 1 << i ; } } System . out . println ( X + " ▁ " + Y ) ; } public static void main ( String [ ] args ) { int N = 45 ; maximizeProduct ( N ) ; } }
import java . util . * ; class GFG { static void constructPermutation ( String S , int N ) { int [ ] ans = new int [ N ] ; ans [ 0 ] = 1 ; for ( int i = 1 ; i < N ; ++ i ) { if ( S . charAt ( i - 1 ) == '0' ) { ans [ i ] = i + 1 ; } else { ans [ i ] = ans [ i - 1 ] ; } for ( int j = 0 ; j < i ; ++ j ) { if ( ans [ j ] >= ans [ i ] ) { ans [ j ] ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ans [ i ] ) ; if ( i != N - 1 ) { System . out . print ( " ▁ " ) ; } } } public static void main ( String [ ] args ) { String S = "100101" ; constructPermutation ( S , S . length ( ) + 1 ) ; } }
import java . io . * ; class GFG { static long KthSolution ( long X , long K ) { long ans = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( X & ( 1 << i ) ) == 0 ) { if ( ( K & 1 ) > 0 ) { ans |= ( 1 << i ) ; } K >>= 1 ; if ( K == 0 ) { break ; } } } return ans ; } public static void main ( String [ ] args ) { long X = 10 ; long K = 5 ; System . out . println ( KthSolution ( X , K ) ) ; } }
import java . io . * ; class GFG { static int minimumMoves ( int A [ ] , int B [ ] , int N ) { int ans = 0 ; int sum_A = 0 , sum_B = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_A += A [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { sum_B += B [ i ] ; } if ( sum_A != sum_B ) { return - 1 ; } int i = 0 ; while ( i < N ) { if ( A [ i ] > B [ i ] ) { int temp = A [ i ] - B [ i ] ; int j = i - 1 ; while ( j >= 0 && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j -- ; } if ( temp > 0 ) { j = i + 1 ; while ( j < N && temp > 0 ) { if ( B [ j ] > A [ j ] ) { int cnt = Math . min ( temp , ( B [ j ] - A [ j ] ) ) ; A [ j ] += cnt ; temp -= cnt ; ans += ( cnt * Math . abs ( j - i ) ) ; } j ++ ; } } } i ++ ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 5 , 7 } ; int B [ ] = { 13 , 0 , 0 } ; int N = A . length ; System . out . println ( minimumMoves ( A , B , N ) ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static int arrayDivisionByTwo ( int arr [ ] , int n ) { int cnt = 0 ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; cnt ++ ; } ans = Math . min ( ans , cnt ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 12 , 40 } ; int n = arr . length ; System . out . print ( arrayDivisionByTwo ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void printMinInteger ( int P , int Q , int N ) { if ( Q > P ) { int temp ; temp = P ; P = Q ; Q = temp ; } if ( Q == 0 && ( P == 0 N % P != 0 ) ) { System . out . println ( " Not ▁ Possible " ) ; return ; } int count_P = 0 , count_Q = 0 ; while ( N > 0 ) { if ( N % P == 0 ) { count_P += N / P ; N = 0 ; } else { N = N - Q ; count_Q ++ ; } } if ( N == 0 ) { for ( int i = 0 ; i < count_Q ; i ++ ) System . out . print ( Q ) ; for ( int i = 0 ; i < count_P ; i ++ ) System . out . print ( P ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } public static void main ( String [ ] args ) { int N = 32 ; int P = 7 ; int Q = 4 ; printMinInteger ( P , Q , N ) ; } }
class GFG { static int minPushes ( int N , int K , int [ ] arr ) { int [ ] dp = new int [ 100000 ] ; for ( int i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; dp [ N ] = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { for ( int xx = 0 ; xx < 100000 ; xx ++ ) { int x = xx ; if ( dp [ x ] == - 1 ) continue ; int next = ( x + arr [ i ] ) % 100000 ; while ( dp [ next ] == - 1 dp [ next ] > dp [ x ] + 1 ) { dp [ next ] = dp [ x ] + 1 ; x = next ; next = ( next + arr [ i ] ) % 100000 ; } } } return dp [ K ] ; } public static void main ( String [ ] args ) { int N = 99880 , K = 89 ; int [ ] arr = { 100 , 3 } ; System . out . print ( minPushes ( N , K , arr ) ) ; } }
import java . io . * ; class GFG { static int minApples ( int M , int K , int N , int S , int W , int E ) { if ( M <= S * K ) return M ; else if ( M <= S * K + E + W ) return S * K + ( M - S * K ) * K ; else return - 1 ; } public static void main ( String [ ] args ) { int M = 10 ; int K = 15 ; int N = 0 ; int S = 1 ; int W = 0 ; int E = 0 ; int ans = minApples ( M , K , N , S , W , E ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int minimumOperations ( int A [ ] , int N ) { int cur_prefix_1 = 0 ; int parity = - 1 ; int minOperationsCase1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_1 += A [ i ] ; if ( cur_prefix_1 == 0 parity * cur_prefix_1 < 0 ) { minOperationsCase1 += Math . abs ( parity - cur_prefix_1 ) ; cur_prefix_1 = parity ; } parity *= - 1 ; } int cur_prefix_2 = 0 ; parity = 1 ; int minOperationsCase2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { cur_prefix_2 += A [ i ] ; if ( cur_prefix_2 == 0 parity * cur_prefix_2 < 0 ) { minOperationsCase2 += Math . abs ( parity - cur_prefix_2 ) ; cur_prefix_2 = parity ; } parity *= - 1 ; } return Math . min ( minOperationsCase1 , minOperationsCase2 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , - 3 , 1 , 0 } ; int N = A . length ; System . out . print ( minimumOperations ( A , N ) ) ; } }
import java . io . * ; class GFG { static int minProductUtil ( int R1 , int B1 , int R2 , int B2 , int M ) { int x = Math . min ( R1 - B1 , M ) ; M -= x ; R1 -= x ; if ( M > 0 ) { int y = Math . min ( R2 - B2 , M ) ; M -= y ; R2 -= y ; } return R1 * R2 ; } static int minProduct ( int R1 , int B1 , int R2 , int B2 , int M ) { int res1 = minProductUtil ( R1 , B1 , R2 , B2 , M ) ; int res2 = minProductUtil ( R2 , B2 , R1 , B1 , M ) ; return Math . min ( res1 , res2 ) ; } public static void main ( String [ ] args ) { int R1 = 21 , B1 = 10 , R2 = 13 , B2 = 11 , M = 3 ; System . out . print ( ( minProduct ( R1 , B1 , R2 , B2 , M ) ) ) ; } }
import java . util . * ; class GFG { static int maxAmount ( int n , int k , int arr [ ] ) { int A [ ] = new int [ 1000001 ] ; for ( int i = 0 ; i < n ; i ++ ) { A [ arr [ i ] ] ++ ; } int j = 0 ; for ( int i = 0 ; i < 1000001 ; i ++ ) { while ( A [ i ] != 0 ) { arr [ j ++ ] = i ; A [ i ] -- ; } } int ans = 0 ; int mod = ( int ) ( 1e9 + 7 ) ; int i = n - 1 ; j = n - 2 ; while ( k > 0 && j >= 0 ) { if ( arr [ i ] > arr [ j ] ) { ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } else { while ( j >= 0 && arr [ j ] == arr [ i ] ) j -- ; if ( j < 0 ) break ; ans = ans + Math . min ( k , ( i - j ) ) * arr [ i ] ; k = k - ( i - j ) ; arr [ i ] -- ; } } while ( k > 0 && arr [ i ] != 0 ) { ans = ans + Math . min ( n , k ) * arr [ i ] ; k -= n ; arr [ i ] -- ; } ans = ans % mod ; int x = ans ; return x ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 3 ; int arr [ ] = { 4 , 3 , 6 , 2 , 4 } ; int ans = maxAmount ( n , k , arr ) ; System . out . print ( ans ) ; } }
import java . util . * ; class GFG { static void maxAverage ( int A [ ] , int N , int X , int Y ) { Arrays . sort ( A ) ; int sum = 0 ; double res = 0 ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; count ++ ; if ( count == X ) { if ( N - i - 1 < X ) { i ++ ; int cnt = 0 ; while ( i < N ) { cnt ++ ; sum += A [ i ] ; i ++ ; } X = X + cnt ; res += ( double ) sum / ( double ) ( X ) ; break ; } res += ( double ) sum / ( double ) ( X ) ; sum = 0 ; count = 0 ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , 10 , 6 , 5 } ; int N = A . length ; int X = 2 , Y = 3 ; maxAverage ( A , N , X , Y ) ; } }
import java . util . * ; class GFG { static char [ ] S = " aaabc " . toCharArray ( ) ; static boolean isAdjChar ( ) { for ( int i = 0 ; i < S . length - 1 ; i ++ ) { if ( S [ i ] == S [ i + 1 ] ) return true ; } return false ; } static void rearrangeStringUtil ( int N ) { int i = 0 , j = 1 , k = 2 ; while ( k < N ) { if ( S [ i ] != S [ j ] ) { i ++ ; j ++ ; if ( j == k ) { k ++ ; } } else { if ( S [ j ] == S [ k ] ) { k ++ ; } else { swap ( k , j ) ; i ++ ; j ++ ; if ( j == k ) { k ++ ; } } } } } static void swap ( int i , int j ) { char temp = S [ i ] ; S [ i ] = S [ j ] ; S [ j ] = temp ; } static String rearrangeString ( int N ) { if ( isAdjChar ( ) == false ) { return String . valueOf ( S ) ; } if ( S . length == 2 ) return " - 1" ; rearrangeStringUtil ( N ) ; reverse ( ) ; rearrangeStringUtil ( N ) ; if ( isAdjChar ( ) == false ) { return String . valueOf ( S ) ; } return " - 1" ; } static void reverse ( ) { int l , r = S . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = S [ l ] ; S [ l ] = S [ r ] ; S [ r ] = temp ; } } public static void main ( String [ ] args ) { int N = S . length ; System . out . print ( rearrangeString ( N ) ) ; } }
public class Main { public static void maxTime ( String S ) { char [ ] s = S . toCharArray ( ) ; if ( s [ 0 ] == ' ? ' ) { if ( s [ 1 ] <= '3' s [ 1 ] == ' ? ' ) s [ 0 ] = '2' ; else s [ 0 ] = '1' ; } if ( s [ 1 ] == ' ? ' ) { if ( s [ 0 ] != '2' ) { s [ 1 ] = 9 ; } else s [ 1 ] = 3 ; } if ( s [ 3 ] == ' ? ' ) s [ 3 ] = '5' ; if ( s [ 4 ] == ' ? ' ) s [ 4 ] = '9' ; System . out . println ( new String ( s ) ) ; } public static void main ( String [ ] args ) { String S = " ? 4:5 ? " ; maxTime ( S ) ; } }
import java . io . * ; class GFG { static void maxGcd ( int a , int b ) { System . out . println ( Math . abs ( a - b ) ) ; } public static void main ( String [ ] args ) { int a = 2231 ; int b = 343 ; maxGcd ( a , b ) ; } }
import java . io . * ; class GFG { static void printBinaryString ( int arr [ ] [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( 0 ) ; } else { System . out . print ( 1 ) ; } } } public static void main ( String [ ] args ) { int N = 5 , M = 3 ; int arr [ ] [ ] = { { 1 , 3 } , { 2 , 4 } , { 2 , 5 } } ; printBinaryString ( arr , N ) ; } }
public class GFG { static int MinCost ( int days [ ] , int cost [ ] , int N ) { int size = days [ N - 1 ] + 1 ; int [ ] dp = new int [ size ] ; dp [ size - 1 ] = Math . min ( cost [ 0 ] , Math . min ( cost [ 1 ] , cost [ 2 ] ) ) ; int ptr = N - 2 ; for ( int i = size - 2 ; i > 0 ; i -- ) { if ( ptr >= 0 && days [ ptr ] == i ) { int val1 = dp [ i + 1 ] + cost [ 0 ] ; int val2 = cost [ 1 ] + ( ( i + 7 >= size ) ? 0 : dp [ i + 7 ] ) ; int val3 = cost [ 2 ] + ( ( i + 30 >= size ) ? 0 : dp [ i + 30 ] ) ; dp [ i ] = Math . min ( val1 , Math . min ( val2 , val3 ) ) ; ptr -- ; } else { dp [ i ] = dp [ i + 1 ] ; } } return dp [ 1 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 6 , 7 , 8 , 10 , 17 } ; int cost [ ] = { 3 , 8 , 20 } ; int N = arr . length ; System . out . println ( MinCost ( arr , cost , N ) ) ; } }
import java . io . * ; class GFG { public static int minElements ( int arr [ ] , int N , int K ) { int count = 0 ; long requiredNum = 1 ; int i = 0 ; while ( requiredNum <= K ) { if ( i < N && requiredNum >= arr [ i ] ) { requiredNum += arr [ i ] ; i ++ ; } else { count ++ ; requiredNum += requiredNum ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 } ; int K = 6 ; int N = arr . length ; System . out . println ( minElements ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static int maximumCount ( int arr [ ] , int N ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd > 0 && even > 0 ) return N - 2 ; else return N - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 13 , 15 , 3 , 16 , 9 , 13 , 18 } ; int N = arr . length ; System . out . println ( maximumCount ( arr , N ) ) ; } }
import java . io . * ; class GFG { public static int minPossibleValue ( int N , int K , int X ) { for ( int i = 1 ; i <= N ; i ++ ) { if ( X <= K ) { X = X * 2 ; } else { X = X + K ; } } return X ; } public static void main ( String [ ] args ) { int N = 7 , K = 4 , X = 1 ; System . out . println ( minPossibleValue ( N , K , X ) ) ; } }
import java . io . * ; class GFG { public static void findMissingValue ( int arr [ ] , int N ) { int minE = Integer . MAX_VALUE , maxE = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { minE = Math . min ( minE , arr [ i + 1 ] ) ; maxE = Math . max ( maxE , arr [ i + 1 ] ) ; } if ( arr [ i ] != - 1 && arr [ i + 1 ] == - 1 ) { minE = Math . min ( minE , arr [ i ] ) ; maxE = Math . max ( maxE , arr [ i ] ) ; } } if ( minE == Integer . MAX_VALUE && maxE == Integer . MIN_VALUE ) { System . out . println ( "0" ) ; } else { System . out . println ( ( minE + maxE ) / 2 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , - 1 , - 1 , 5 } ; int N = arr . length ; findMissingValue ( arr , N ) ; } }
import java . io . * ; class GFG { static void findLastElement ( int N , int [ ] A ) { int l = A . length ; int j = 0 ; while ( N > l * ( int ) ( Math . pow ( 2 , j ) ) ) { N = N - l * ( int ) Math . pow ( 2 , j ) ; j += 1 ; } int k = 1 ; int r = ( int ) Math . pow ( 2 , j ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( N > r * i ) k += 1 ; } for ( int i = 0 ; i < l ; i ++ ) { if ( i + 1 == k ) { System . out . print ( A [ i ] ) ; return ; } } } public static void main ( String [ ] args ) { int K = 7 ; int [ ] A = { 1 , 2 , 3 } ; findLastElement ( K , A ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findMaxD ( int arr [ ] , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = Math . abs ( arr [ i ] - K ) ; } int D = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { D = gcd ( D , arr [ i ] ) ; } return D ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 11 } ; int N = arr . length ; int K = 3 ; System . out . print ( findMaxD ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static int maxOperations ( String S , int N ) { int X = 0 ; int Y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '0' ) break ; Y ++ ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( S . charAt ( i ) == '1' ) break ; X ++ ; } if ( N == X + Y ) return 0 ; return N - ( X + Y ) - 1 ; } public static void main ( String [ ] args ) { String S = "001111" ; int N = S . length ( ) ; System . out . println ( maxOperations ( S , N ) ) ; } }
import java . util . * ; class GFG { static int maximumSum ( String S , int N , int K ) { int sum = 0 ; Integer [ ] freq = new Integer [ 256 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { freq [ ( int ) S . charAt ( i ) ] += 1 ; } Arrays . sort ( freq , Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < 256 ; i ++ ) { if ( K > freq [ i ] ) { sum += freq [ i ] * freq [ i ] ; K -= freq [ i ] ; } else { sum += freq [ i ] * K ; break ; } } return sum ; } public static void main ( String args [ ] ) { String S = " GEEKSFORGEEKS " ; int K = 10 ; int N = S . length ( ) ; System . out . print ( maximumSum ( S , N , K ) ) ; } }
import java . io . * ; class GFG { static int dp [ ] [ ] [ ] = new int [ 100 ] [ 10 ] [ 10 ] ; static int countOfNumbers ( int digit , int prev1 , int prev2 , int n ) { if ( digit == n + 1 ) { return 1 ; } int val = dp [ digit ] [ prev1 ] [ prev2 ] ; if ( val != - 1 ) { return val ; } val = 0 ; if ( digit == 1 ) { for ( int i = ( n == 1 ? 0 : 1 ) ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else if ( digit == 2 ) { for ( int i = 0 ; i <= 9 ; ++ i ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } else { int diff = Math . abs ( prev2 - prev1 ) ; for ( int i = 0 ; i <= 9 ; ++ i ) { if ( Math . abs ( prev1 - i ) <= diff ) { val += countOfNumbers ( digit + 1 , i , prev1 , n ) ; } } } return val ; } static void countNumbersUtil ( int N ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { for ( int k = 0 ; k < 10 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } System . out . println ( countOfNumbers ( 1 , 0 , 0 , N ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; countNumbersUtil ( N ) ; } }
import java . io . * ; class GFG { static void maxSum ( int arr [ ] , int n ) { int sum = 0 ; int max_neg = Integer . MAX_VALUE ; int tempsum = 0 ; int small = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; small = Math . min ( small , arr [ i ] ) ; if ( arr [ i ] > 0 ) { tempsum = 0 ; } else { tempsum += arr [ i ] ; } max_neg = Math . min ( max_neg , tempsum ) ; } if ( max_neg == 0 ) { max_neg = small ; } System . out . println ( sum - max_neg ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = arr . length ; maxSum ( arr , n ) ; } }
import java . util . * ; class GFG { static void convertXintoY ( int X , int Y ) { while ( Y > X ) { if ( Y % 2 == 0 ) Y /= 2 ; else if ( Y % 10 == 1 ) Y /= 10 ; else break ; } if ( X == Y ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int X = 100 , Y = 40021 ; convertXintoY ( X , Y ) ; } }
public class GFG { static void minimumOperation ( String a , String b ) { int n = a . length ( ) ; int i = 0 ; int minoperation = 0 ; while ( i < n ) { if ( a . charAt ( i ) == b . charAt ( i ) ) { i = i + 1 ; continue ; } else if ( a . charAt ( i ) == b . charAt ( i + 1 ) && a . charAt ( i + 1 ) == b . charAt ( i ) && i < n - 1 ) { minoperation ++ ; i = i + 2 ; } else if ( a . charAt ( i ) != b . charAt ( i ) ) { minoperation ++ ; i = i + 1 ; } else { ++ i ; } } System . out . println ( minoperation ) ; } public static void main ( String [ ] args ) { String a = "10010010" , b = "00001000" ; minimumOperation ( a , b ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void findMinOperations ( int arr [ ] , int N , int K ) { int operations = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int j = i ; j < N ; j += K ) freq . put ( arr [ j ] , freq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; int max1 = 0 , num = - 1 ; for ( Map . Entry < Integer , Integer > x : freq . entrySet ( ) ) { if ( x . getValue ( ) > max1 ) { max1 = x . getValue ( ) ; num = x . getKey ( ) ; } } for ( Map . Entry < Integer , Integer > x : freq . entrySet ( ) ) { if ( x . getKey ( ) != num ) operations += x . getValue ( ) ; } } System . out . print ( operations ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 3 , 5 , 6 } ; int K = 2 ; int N = arr . length ; findMinOperations ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int countPermutations ( int N ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( 105 ) ; for ( int i = 0 ; i < 105 ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; int indeg = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= N ; j ++ ) { if ( i == j ) continue ; int sum = i + j ; if ( Math . ceil ( Math . sqrt ( sum ) ) == Math . floor ( Math . sqrt ( sum ) ) ) { adj . get ( i ) . add ( j ) ; } } if ( adj . get ( i ) . size ( ) == 1 ) indeg ++ ; } if ( indeg == 0 ) return N ; else if ( indeg <= 2 ) return 2 ; else return 0 ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . println ( countPermutations ( N ) ) ; } }
class GFG { public static void makeList ( int N , int K ) { int [ ] list = new int [ N ] ; int left = 1 ; int right = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( K % 2 == 0 ) { list [ i ] = left ; left = left + 1 ; } else { list [ i ] = right ; right = right - 1 ; } if ( K > 1 ) K -- ; } for ( int i = 0 ; i < list . length ; i ++ ) { System . out . print ( list [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; int K = 3 ; makeList ( N , K ) ; } }
import java . util . * ; class GFG { static void maxDiamonds ( int A [ ] , int N , int K ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ( a , b ) -> b - a ) ; for ( int i = 0 ; i < N ; i ++ ) { pq . add ( A [ i ] ) ; } int ans = 0 ; while ( ! pq . isEmpty ( ) && K -- > 0 ) { int top = pq . peek ( ) ; pq . remove ( ) ; ans += top ; top = top / 2 ; pq . add ( top ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 1 , 7 , 4 , 2 } ; int K = 3 ; int N = A . length ; maxDiamonds ( A , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static long largestNumber ( int N ) { if ( N > 45 ) return - 1 ; int num = 0 , digit = 9 ; while ( N > 0 && digit > 0 ) { if ( digit <= N ) { num *= 10 ; num += digit ; N -= digit ; } digit -= 1 ; } return num * 10 ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . print ( largestNumber ( N ) ) ; } }
import java . util . * ; class GFG { static int minimumSum ( int A [ ] , int N , String S ) { int mul = 0 ; for ( int i = 0 ; i < ( int ) S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == ' * ' ) mul += 1 ; } int ans = 1000000 ; for ( int i = 0 ; i < ( 1 << ( N - 1 ) ) ; i ++ ) { int cnt = 0 ; Vector < Character > v = new Vector < Character > ( ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( ( ( 1 << j ) & ( i ) ) > 0 ) { cnt += 1 ; v . add ( ' * ' ) ; } else { v . add ( ' + ' ) ; } } if ( cnt == mul ) { LinkedList < Integer > d = new LinkedList < Integer > ( ) ; d . add ( A [ 0 ] ) ; for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( v . get ( j ) == ' * ' ) { int x = d . getLast ( ) ; d . removeLast ( ) ; x = x * A [ j + 1 ] ; d . add ( x ) ; } else { d . add ( A [ j + 1 ] ) ; } } int sum = 0 ; while ( d . size ( ) > 0 ) { int x = d . peek ( ) ; sum += x ; d . removeFirst ( ) ; } ans = Math . min ( ans , sum ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 2 , 2 , 2 } ; String S = " * * + " ; int N = A . length ; System . out . print ( minimumSum ( A , N , S ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void sumOfMedians ( int arr [ ] , int N , int K ) { int selectMedian = ( K + 1 ) / 2 ; int totalArrays = N / K ; int minSum = 0 ; int i = selectMedian - 1 ; while ( i < N && totalArrays != 0 ) { minSum = minSum + arr [ i ] ; i = i + selectMedian ; totalArrays -- ; } System . out . println ( minSum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; int K = 2 ; sumOfMedians ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void generateString ( int K ) { String s = " " ; for ( int i = 97 ; i < 97 + K ; i ++ ) { s = s + ( char ) ( i ) ; for ( int j = i + 1 ; j < 97 + K ; j ++ ) { s += ( char ) ( i ) ; s += ( char ) ( j ) ; } } s += ( char ) ( 97 ) ; System . out . println ( s ) ; } public static void main ( String [ ] args ) { int K = 4 ; generateString ( K ) ; } }
class GFG { static int minimumValue ( int N , int K ) { return ( int ) Math . ceil ( ( double ) K / ( double ) N ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 50 ; System . out . print ( minimumValue ( N , K ) ) ; } }
import java . util . * ; class GFG { static void minSizeArr ( int A [ ] , int N , int K ) { int leftTaken = N , rightTaken = N ; int leftSum = 0 , rightSum = 0 ; for ( int left = - 1 ; left < N ; left ++ ) { if ( left != - 1 ) leftSum += A [ left ] ; rightSum = 0 ; for ( int right = N - 1 ; right > left ; right -- ) { rightSum += A [ right ] ; if ( leftSum + rightSum == K ) { if ( leftTaken + rightTaken > ( left + 1 ) + ( N - right ) ) { leftTaken = left + 1 ; rightTaken = N - right ; } break ; } if ( leftSum + rightSum > K ) break ; } } if ( leftTaken + rightTaken <= N ) { for ( int i = 0 ; i < leftTaken ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; for ( int i = 0 ; i < rightTaken ; i ++ ) System . out . print ( A [ N - i - 1 ] + " ▁ " ) ; } else System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 7 ; int A [ ] = { 3 , 2 , 1 , 1 , 1 , 1 , 3 } ; int K = 10 ; minSizeArr ( A , N , K ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void minModulo ( int L , int R , int N ) { if ( R - L < N ) { int ans = Integer . MAX_VALUE ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( i != j ) ans = Math . min ( ans , ( i * j ) % N ) ; System . out . println ( ans ) ; } else { System . out . println ( 0 ) ; } } public static void main ( String [ ] args ) { int L = 6 , R = 10 , N = 2019 ; minModulo ( L , R , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { count ++ ; if ( N / i != i ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( countNumbers ( N ) ) ; } }
import java . util . * ; class GFG { static int maximumSize ( Integer [ ] a , int k ) { int low = 1 ; int high = Collections . max ( Arrays . asList ( a ) ) ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int count = 0 ; for ( int c = 0 ; c < a . length ; c ++ ) { count += a / mid ; } if ( count >= k ) { ans = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 2 , 3 , 4 , 9 } ; int K = 6 ; System . out . println ( maximumSize ( arr , K ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void findMaxValueOfEquation ( int arr [ ] [ ] , int K ) { PriorityQueue < int [ ] > pq = new PriorityQueue < > ( ( a , b ) -> { if ( a [ 0 ] != b [ 0 ] ) return b [ 0 ] - a [ 0 ] ; return b [ 1 ] - a [ 1 ] ; } ) ; int res = Integer . MIN_VALUE ; for ( int point [ ] : arr ) { while ( ! pq . isEmpty ( ) && point [ 0 ] - pq . peek ( ) [ 1 ] > K ) { pq . poll ( ) ; } if ( ! pq . isEmpty ( ) ) { res = Math . max ( res , pq . peek ( ) [ 0 ] + point [ 0 ] + point [ 1 ] ) ; } pq . add ( new int [ ] { point [ 1 ] - point [ 0 ] , point [ 0 ] } ) ; } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 3 } , { 2 , 0 } , { 5 , 10 } , { 6 , - 10 } } ; int K = 1 ; findMaxValueOfEquation ( arr , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minOperations ( int arr [ ] , int n ) { int oddcount = 0 , evencount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) oddcount ++ ; else evencount ++ ; } return Math . min ( oddcount , evencount ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 1 , 8 } ; int N = arr . length ; System . out . println ( minOperations ( arr , N ) ) ; } }
class GFG { static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countDistinctNumbers ( long A , long B , long C ) { long g = gcd ( A , B ) ; long count = C / g ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { long A = 2 , B = 3 , C = 5 ; countDistinctNumbers ( A , B , C ) ; } }
class GFG { static void max_min ( int N , int C , int K ) { int maximum , minimum ; if ( N == 1 ) { maximum = minimum = C ; } else if ( K >= C ) { maximum = C ; minimum = 0 ; } else { maximum = K ; minimum = 0 ; int remain_candy = C - K ; maximum += remain_candy / N ; minimum = remain_candy / N ; if ( remain_candy % N == N - 1 ) { minimum ++ ; } } System . out . println ( " Maximum ▁ = ▁ " + maximum ) ; System . out . println ( " Minimum ▁ = ▁ " + minimum ) ; } public static void main ( String [ ] args ) { int N = 4 ; int C = 12 ; int K = 3 ; max_min ( N , C , K ) ; } }
import java . io . * ; class GFG { public static void findEquation ( int S , int M ) { System . out . println ( "1 ▁ " + ( ( - 1 ) * S ) + " ▁ " + M ) ; } public static void main ( String [ ] args ) { int S = 5 , M = 6 ; findEquation ( S , M ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void shortestSubarray ( int A [ ] , int N ) { int i ; int left_index ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] + 1 != A [ i + 1 ] ) break ; } left_index = i ; int right_index ; for ( i = N - 1 ; i >= 1 ; i -- ) { if ( A [ i ] != A [ i - 1 ] + 1 ) break ; } right_index = i ; int updated_right ; int minLength = Math . min ( N - left_index - 1 , right_index ) ; if ( A [ right_index ] <= A [ left_index ] + 1 ) { updated_right = right_index + A [ left_index ] - A [ right_index ] + 1 ; if ( updated_right < N ) minLength = Math . min ( minLength , updated_right - left_index - 1 ) ; } System . out . println ( minLength ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 , 4 , 3 , 5 } ; int N = arr . length ; shortestSubarray ( arr , N ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int minLength ( String s ) { int i = 0 , j = s . length ( ) - 1 ; for ( ; i < j && s . charAt ( i ) == s . charAt ( j ) ; ) { char d = s . charAt ( i ) ; while ( i <= j && s . charAt ( i ) == d ) i ++ ; while ( i <= j && s . charAt ( j ) == d ) j -- ; } return j - i + 1 ; } public static void main ( String [ ] args ) { String S = " aacbcca " ; System . out . println ( minLength ( S ) ) ; } }
class GFG { static int mod = 1000000007 ; static long power ( long x , int y ) { long res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static long CountBST ( int H ) { return power ( 2 , H ) ; } public static void main ( String [ ] args ) { int H = 2 ; System . out . print ( CountBST ( H ) ) ; } }
class GFG { static void rotateString ( int n , int m , String s ) { int v [ ] = new int [ n ] ; int cnt = 0 ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { v [ j ] = i ; j += 1 ; } } for ( int i = 1 ; i < j ; i ++ ) { if ( ( v [ i ] - v [ i - 1 ] - 1 ) > m ) { cnt ++ ; } } if ( j >= 2 && ( n - ( v [ j - 1 ] - v [ 0 ] ) - 1 ) > m ) { cnt ++ ; } if ( cnt <= 1 ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { String S = "101001" ; int M = 1 ; int N = S . length ( ) ; rotateString ( N , M , S ) ; } }
import java . util . * ; class GFG { static int findNumber ( int N ) { int [ ] ans = { 1 , 2 , 4 , 8 , 7 , 5 } ; return ans [ N % 6 ] ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( findNumber ( N ) ) ; } }
import java . util . * ; class GFG { static void canBeEmptied ( int A , int B ) { if ( Math . max ( A , B ) > 2 * Math . min ( A , B ) ) { System . out . println ( " No " ) ; return ; } if ( ( A + B ) % 3 == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int A = 1 , B = 2 ; canBeEmptied ( A , B ) ; } }
class GFG { static void maximumIndex ( int N , int B ) { int i = 0 , j = 1 ; int cnt = 0 ; int sum = N * ( N + 1 ) / 2 ; boolean flag = false ; while ( cnt < N ) { i += j ; j ++ ; cnt ++ ; if ( i == B ) { flag = true ; break ; } } if ( ! flag == true ) { System . out . print ( sum ) ; } else { System . out . print ( sum - 1 ) ; } } public static void main ( String [ ] args ) { int N = 4 , B = 6 ; maximumIndex ( N , B ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findLongestArray ( int K ) { ArrayList < int [ ] > primefactors = new ArrayList < > ( ) ; int K_temp = K ; for ( int i = 2 ; i * i <= K ; i ++ ) { int count = 0 ; while ( K_temp % i == 0 ) { K_temp /= i ; count ++ ; } if ( count > 0 ) primefactors . add ( new int [ ] { count , i } ) ; } if ( K_temp != 1 ) primefactors . add ( new int [ ] { 1 , K_temp } ) ; Collections . sort ( primefactors , ( x , y ) -> { if ( x [ 0 ] != y [ 0 ] ) return y [ 0 ] - x [ 0 ] ; return y [ 1 ] - x [ 1 ] ; } ) ; int n = primefactors . get ( 0 ) [ 0 ] ; int val = primefactors . get ( 0 ) [ 1 ] ; int answer [ ] = new int [ n ] ; Arrays . fill ( answer , val ) ; answer [ n - 1 ] *= K ; for ( int i = 0 ; i < n ; i ++ ) { answer [ n - 1 ] /= val ; } System . out . print ( " { " ) ; for ( int i = 0 ; i < answer . length ; i ++ ) { if ( i == answer . length - 1 ) System . out . print ( answer [ i ] + " } " ) ; else System . out . print ( answer [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int K = 4 ; findLongestArray ( K ) ; } }
import java . io . * ; class GFG { static void findScoreSum ( int n ) { int total = 0 ; int prev_monday = 0 , curr_day = 0 ; for ( int day = 1 ; day <= n ; day ++ ) { if ( day % 7 == 1 ) { prev_monday ++ ; curr_day = prev_monday ; } total += curr_day ++ ; } System . out . println ( total ) ; } public static void main ( String [ ] args ) { int N = 8 ; findScoreSum ( N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findScoreSum ( int n ) { int F = n / 7 ; int D = n % 7 ; int fullWeekScore = ( 49 + 7 * F ) * F / 2 ; int lastNonFullWeekScore = ( 2 * F + D + 1 ) * D / 2 ; System . out . println ( fullWeekScore + lastNonFullWeekScore ) ; } public static void main ( String [ ] args ) { int N = 8 ; findScoreSum ( N ) ; } }
import java . util . * ; class GFG { static int minSteps ( ArrayList < Integer > a , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = a . get ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] + a . get ( i ) ; int mx = - 1 ; for ( int subgroupsum : prefix_sum ) { int sum = 0 ; int i = 0 ; int grp_count = 0 ; while ( i < n ) { sum += a . get ( i ) ; if ( sum == subgroupsum ) { grp_count += 1 ; sum = 0 ; } else if ( sum > subgroupsum ) { grp_count = - 1 ; break ; } i += 1 ; } if ( grp_count > mx ) mx = grp_count ; } return n - mx ; } public static void main ( String [ ] args ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; A . add ( 1 ) ; A . add ( 2 ) ; A . add ( 3 ) ; A . add ( 2 ) ; A . add ( 1 ) ; A . add ( 3 ) ; int N = A . size ( ) ; System . out . print ( minSteps ( A , N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static long maximumNum ( long X , long Y , long N ) { long num = 0 ; if ( N - N % X + Y <= N ) { num = N - N % X + Y ; } else { num = N - N % X - ( X - Y ) ; } return num ; } public static void main ( String [ ] args ) { long X = 10 ; long Y = 5 ; long N = 15 ; System . out . println ( maximumNum ( X , Y , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getArea ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2 ) ; } static int isInside ( ArrayList < ArrayList < Integer > > triangle , ArrayList < Integer > point ) { ArrayList < Integer > A = triangle . get ( 0 ) ; ArrayList < Integer > B = triangle . get ( 1 ) ; ArrayList < Integer > C = triangle . get ( 2 ) ; int x = point . get ( 0 ) ; int y = point . get ( 1 ) ; int ABC = getArea ( A . get ( 0 ) , A . get ( 1 ) , B . get ( 0 ) , B . get ( 1 ) , C . get ( 0 ) , C . get ( 1 ) ) ; int BPC = getArea ( x , y , B . get ( 0 ) , B . get ( 1 ) , C . get ( 0 ) , C . get ( 1 ) ) ; int APC = getArea ( A . get ( 0 ) , A . get ( 1 ) , x , y , C . get ( 0 ) , C . get ( 1 ) ) ; int APB = getArea ( A . get ( 0 ) , A . get ( 1 ) , B . get ( 0 ) , B . get ( 1 ) , x , y ) ; return ABC == ( APC + APB + BPC ) ? 1 : 0 ; } static void countPoints ( ArrayList < ArrayList < Integer > > rectangle , ArrayList < ArrayList < Integer > > triangle , ArrayList < ArrayList < Integer > > points ) { int n = rectangle . size ( ) ; ArrayList < ArrayList < Integer > > triangle1 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 1 ; i < n ; i ++ ) triangle1 . add ( rectangle . get ( i ) ) ; ArrayList < ArrayList < Integer > > triangle2 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < 3 ; i ++ ) { triangle2 . add ( rectangle . get ( i ) ) ; } ArrayList < ArrayList < Integer > > triangle3 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < 2 ; i ++ ) { triangle3 . add ( rectangle . get ( i ) ) ; } triangle3 . add ( rectangle . get ( 3 ) ) ; ArrayList < ArrayList < Integer > > triangle4 = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = n - 2 ; i < n ; i ++ ) { triangle4 . add ( rectangle . get ( i ) ) ; } triangle4 . add ( rectangle . get ( 0 ) ) ; int ans = 0 ; for ( ArrayList < Integer > point : points ) { int condOne = isInside ( triangle1 , point ) ; int condTwo = isInside ( triangle2 , point ) ; int condThree = isInside ( triangle3 , point ) ; int condFour = isInside ( triangle4 , point ) ; int condFive = isInside ( triangle , point ) ; if ( ( condOne != 0 condTwo != 0 condThree != 0 condFour != 0 ) && condFive != 0 ) ans += 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > rectangle = new ArrayList < ArrayList < Integer > > ( ) ; ArrayList < ArrayList < Integer > > points = new ArrayList < ArrayList < Integer > > ( ) ; ArrayList < ArrayList < Integer > > triangle = new ArrayList < ArrayList < Integer > > ( ) ; rectangle . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 5 ) ) ) ; rectangle . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 2 ) ) ) ; rectangle . add ( new ArrayList < Integer > ( Arrays . asList ( 2 , 1 ) ) ) ; rectangle . add ( new ArrayList < Integer > ( Arrays . asList ( 5 , 5 ) ) ) ; points . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 1 ) ) ) ; points . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 1 ) ) ) ; points . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 6 ) ) ) ; points . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 6 ) ) ) ; triangle . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 4 ) ) ) ; triangle . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , 4 ) ) ) ; triangle . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , - 2 ) ) ) ; countPoints ( points , triangle , rectangle ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int GCD ( int a , int b ) { if ( b < 1 ) return a ; return GCD ( b , a % b ) ; } static int GCDArr ( ArrayList < Integer > a ) { int ans = a . get ( 0 ) ; for ( int i : a ) ans = GCD ( ans , i ) ; return ans ; } static boolean findSubseqUtil ( ArrayList < Integer > a , ArrayList < Integer > b , ArrayList < Integer > ans , int k , int i ) { if ( ans . size ( ) == a . size ( ) ) { if ( GCDArr ( ans ) == k ) { System . out . print ( " [ " ) ; int m = ans . size ( ) ; for ( int j = 0 ; j < m - 1 ; j ++ ) System . out . print ( ans . get ( j ) + " , ▁ " ) ; System . out . print ( ans . get ( m - 1 ) + " ] " ) ; return true ; } else return false ; } ans . add ( a . get ( i ) ) ; boolean temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . remove ( ans . size ( ) - 1 ) ; ans . add ( b . get ( i ) ) ; temp = findSubseqUtil ( a , b , ans , k , i + 1 ) ; if ( temp ) return true ; ans . remove ( ans . size ( ) - 1 ) ; return false ; } static void findSubseq ( ArrayList < Integer > A , ArrayList < Integer > B , int K , int i ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; findSubseqUtil ( A , B , ans , K , i ) ; if ( ans . size ( ) < 1 ) System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > A = new ArrayList < > ( ) ; A . add ( 5 ) ; A . add ( 3 ) ; A . add ( 6 ) ; A . add ( 2 ) ; A . add ( 9 ) ; ArrayList < Integer > B = new ArrayList < Integer > ( ) ; B . add ( 21 ) ; B . add ( 7 ) ; B . add ( 14 ) ; B . add ( 12 ) ; B . add ( 28 ) ; int K = 3 ; findSubseq ( A , B , K , 0 ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void minimumOperations ( int A [ ] , int n ) { int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( A [ i ] != ( i + 1 ) ) { if ( ( ( i - 1 ) >= 0 ) && A [ i - 1 ] == ( i + 1 ) ) { cnt ++ ; int t = A [ i ] ; A [ i ] = A [ i - 1 ] ; A [ i - 1 ] = t ; } else if ( ( ( i - 2 ) >= 0 ) && A [ i - 2 ] == ( i + 1 ) ) { cnt += 2 ; A [ i - 2 ] = A [ i - 1 ] ; A [ i - 1 ] = A [ i ] ; A [ i ] = i + 1 ; } else { System . out . println ( - 1 ) ; return ; } } } System . out . println ( cnt ) ; } public static void main ( String [ ] args ) { int A [ ] = { 7 , 3 , 2 , 1 , 4 } ; int n = A . length ; minimumOperations ( A , n ) ; } }
class GFG { static void countOccurrences ( int n , int x ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( x % i == 0 ) { if ( x / i <= n ) count ++ ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 7 , X = 12 ; countOccurrences ( N , X ) ; } }
import java . io . * ; class GFG { static int MinimumCost ( int A [ ] , int B [ ] , int N ) { int totalCost = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int mod_A = B [ i ] % A [ i ] ; int totalCost_A = Math . min ( mod_A , A [ i ] - mod_A ) ; int mod_B = A [ i ] % B [ i ] ; int totalCost_B = Math . min ( mod_B , B [ i ] - mod_B ) ; totalCost += Math . min ( totalCost_A , totalCost_B ) ; } return totalCost ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 6 , 3 } ; int B [ ] = { 4 , 8 , 13 } ; int N = A . length ; System . out . print ( MinimumCost ( A , B , N ) ) ; } }
import java . io . * ; class GFG { public static void maxOccuringCharacter ( String s ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { count1 ++ ; } else if ( s . charAt ( i ) == '0' ) { count0 ++ ; } } int prev = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { prev = i ; break ; } } for ( int i = prev + 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != ' X ' ) { if ( s . charAt ( i ) == '1' ) { count1 += i - prev - 1 ; prev = i ; } else { boolean flag = true ; for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( j ) == '1' ) { flag = false ; prev = j ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . length ( ) ; } } } } prev = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { prev = i ; break ; } } for ( int i = prev + 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != ' X ' ) { if ( s . charAt ( i ) == '0' ) { count0 += i - prev - 1 ; prev = i ; } else { boolean flag = true ; for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( j ) == '0' ) { prev = j ; flag = false ; break ; } } if ( ! flag ) { i = prev ; } else { i = s . length ( ) ; } } } } if ( s . charAt ( 0 ) == ' X ' ) { int count = 0 ; int i = 0 ; while ( s . charAt ( i ) == ' X ' ) { count ++ ; i ++ ; } if ( s . charAt ( i ) == '1' ) { count1 += count ; } } if ( s . charAt ( s . length ( ) - 1 ) == ' X ' ) { int count = 0 ; int i = s . length ( ) - 1 ; while ( s . charAt ( i ) == ' X ' ) { count ++ ; i -- ; } if ( s . charAt ( i ) == '0' ) { count0 += count ; } } if ( count0 == count1 ) { System . out . println ( " X " ) ; } else if ( count0 > count1 ) { System . out . println ( 0 ) ; } else System . out . println ( 1 ) ; } public static void main ( String [ ] args ) { String S = " XX10XX10XXX1XX " ; maxOccuringCharacter ( S ) ; } }
import java . util . * ; class GFG { static void minimumDeletions ( String s ) { int n = s . length ( ) ; int ans = n ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int t = s . charAt ( i ) - '0' ; if ( t % 2 == 0 ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { int num = ( s . charAt ( j ) - '0' ) * 10 + t ; if ( num % 4 == 0 ) { int k1 = i - j - 1 ; int k2 = n - i - 1 ; ans = Math . min ( ans , k1 + k2 ) ; } } } } if ( ans == n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = s . charAt ( i ) - '0' ; if ( num % 4 == 0 ) { ans = n - 1 ; } } } System . out . println ( ans ) ; } static public void main ( String [ ] args ) { String str = "12367" ; minimumDeletions ( str ) ; } }
import java . io . * ; class GFG { static void generateSequence ( int arr [ ] , int n ) { String ans ; ans = Integer . toString ( arr [ 0 ] ) ; if ( n == 1 ) System . out . println ( ans ) ; else if ( n == 2 ) { System . out . println ( ans + " / " + Integer . toString ( arr [ 1 ] ) ) ; } else { ans += " / ( " + Integer . toString ( arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { ans += " / " + Integer . toString ( arr [ i ] ) ; } ans += " ) " ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1000 , 100 , 10 , 2 } ; int N = arr . length ; generateSequence ( arr , N ) ; } }
import java . util . * ; class GFG { static void LCMPairs ( int arr [ ] , int N ) { int total_pairs = ( N * ( N - 1 ) ) / 2 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } odd = ( odd * ( odd - 1 ) ) / 2 ; System . out . println ( " Even ▁ = ▁ " + ( total_pairs - odd ) + " , ▁ Odd ▁ = ▁ " + odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 5 , 4 } ; int N = arr . length ; LCMPairs ( arr , N ) ; } }
import java . util . * ; class GFG { static int printNumber ( int N , int K , int M ) { int sum = K * ( M * ( M + 1 ) / 2 ) ; return sum - N ; } public static void main ( String [ ] args ) { int N = 17 ; int K = 3 ; int M = 4 ; System . out . print ( printNumber ( N , K , M ) ) ; } }
import java . io . * ; class GFG { static void countSwaps ( String S , String T ) { int [ ] value = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) value [ i ] = i + 1 ; int N = S . length ( ) ; int sum1 = 0 ; int sum2 = 0 ; boolean flag = false ; for ( int i = 0 ; i < N ; i ++ ) { sum1 += value [ S . charAt ( i ) - ' a ' ] ; sum2 += value [ T . charAt ( i ) - ' a ' ] ; if ( ( value [ S . charAt ( i ) - ' a ' ] % 2 == 0 && value [ T . charAt ( i ) - ' a ' ] % 2 == 1 ) || ( value [ S . charAt ( i ) - ' a ' ] % 2 == 1 && value [ T . charAt ( i ) - ' a ' ] % 2 == 0 ) ) flag = false ; } if ( sum1 % 2 == 1 && sum2 % 2 == 1 ) System . out . println ( "0NEW_LINE"); else if ( sum1 % 2 == 0 && sum2 % 2 == 0 ) { if ( flag ) System . out . println ( "1" ) ; else System . out . println ( " - 1" ) ; } else { System . out . println ( " - 1" ) ; } } public static void main ( String [ ] args ) { String S = " acd " ; String T = " dbf " ; countSwaps ( S , T ) ; } }
import java . io . * ; class GFG { static boolean isPrime ( int N ) { if ( N <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static void countPrimeTuples ( int N ) { int count = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime ( i ) && isPrime ( i - 2 ) ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 6 ; countPrimeTuples ( N ) ; } }
import java . util . * ; class GFG { static void countPairs ( int A [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( A [ i ] A [ j ] ) > ( A [ i ] & A [ j ] ) ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 7 } ; int N = A . length ; countPairs ( A , N ) ; } }
public class GFG { static void checkArrays ( int arr1 [ ] , int arr2 [ ] , int N ) { int count = 0 ; boolean flag = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { if ( arr1 [ i ] == 0 ) count ++ ; else { count -- ; if ( count < 0 ) { flag = false ; break ; } } } } if ( ( flag && ( count == 0 ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 0 , 0 , 1 , 1 } ; int arr2 [ ] = { 1 , 1 , 0 , 0 } ; int N = arr1 . length ; checkArrays ( arr1 , arr2 , N ) ; } }
import java . io . * ; class GFG { static void printLargestDivisible ( int arr [ ] , int N ) { int i , count0 = 0 , count7 = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count0 ++ ; else count7 ++ ; } if ( count7 % 50 == 0 ) { while ( count7 != 0 ) { System . out . print ( 7 ) ; count7 -= 1 ; } while ( count0 != 0 ) { System . out . print ( 0 ) ; count0 -= 1 ; } } else if ( count7 < 5 ) { if ( count0 == 0 ) System . out . print ( " No " ) ; else System . out . print ( "0" ) ; } else { count7 = count7 - count7 % 5 ; while ( count7 != 0 ) { System . out . print ( 7 ) ; count7 -= 1 ; } while ( count0 != 0 ) { System . out . print ( 0 ) ; count0 -= 1 ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 7 , 0 , 7 , 7 , 7 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 7 , 7 , 7 } ; int N = arr . length ; printLargestDivisible ( arr , N ) ; } }
import java . util . * ; class GFG { static int maxSheets ( int A , int B ) { int area = A * B ; int count = 1 ; while ( area % 2 == 0 ) { area /= 2 ; count *= 2 ; } return count ; } public static void main ( String args [ ] ) { int A = 5 , B = 10 ; System . out . println ( maxSheets ( A , B ) ) ; } }
import java . io . * ; class GFG { public static int findMaxNum ( int num ) { String binaryNumber = Integer . toBinaryString ( num ) ; String maxBinaryNumber = " " ; int count0 = 0 , count1 = 0 ; int N = binaryNumber . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( binaryNumber . charAt ( i ) == '1' ) { count1 ++ ; } else { count0 ++ ; } } for ( int i = 0 ; i < count1 ; i ++ ) { maxBinaryNumber += '1' ; } for ( int i = 0 ; i < count0 ; i ++ ) { maxBinaryNumber += '0' ; } return Integer . parseInt ( maxBinaryNumber , 2 ) ; } public static void main ( String [ ] args ) { int N = 11 ; System . out . println ( findMaxNum ( N ) ) ; } }
import java . io . * ; class GFG { static void findMinMoves ( int a , int b ) { int ans = 0 ; if ( a == b || Math . abs ( a - b ) == 1 ) { ans = a + b ; } else { int k = Math . min ( a , b ) ; int j = Math . max ( a , b ) ; ans = 2 * k + 2 * ( j - k ) - 1 ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int a = 3 , b = 5 ; findMinMoves ( a , b ) ; } }
import java . util . * ; class GFG { static void minOperations ( String N ) { int even = 0 ; int odd = 0 ; for ( int it : N . toCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { even ++ ; } else { odd ++ ; } } if ( odd == 0 odd == 1 ) { System . out . print ( " Not ▁ Possible " + "NEW_LINE"); } else { int ans = 0 ; for ( int it : N . toCharArray ( ) ) { if ( ( it - '0' ) % 2 == 0 ) { ans ++ ; } else { ans = 0 ; } } if ( odd % 2 != 0 ) { ans ++ ; } System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { String N = "12345" ; minOperations ( N ) ; } }
import java . util . * ; class GFG { static void generatepermutation ( int N ) { Vector < Integer > answer = new Vector < Integer > ( ) ; answer . add ( N ) ; for ( int i = 1 ; i < N ; i ++ ) { answer . add ( i ) ; } for ( int i : answer ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 4 ; generatepermutation ( N ) ; } }
import java . io . * ; class GFG { static long cntEvenSumPairs ( long X , long Y ) { long cntXEvenNums = X / 2 ; long cntXOddNums = ( X + 1 ) / 2 ; long cntYEvenNums = Y / 2 ; long cntYOddNums = ( Y + 1 ) / 2 ; long cntPairs = ( cntXEvenNums * cntYEvenNums ) + ( cntXOddNums * cntYOddNums ) ; return cntPairs ; } public static void main ( String [ ] args ) { long X = 2 ; long Y = 3 ; System . out . println ( cntEvenSumPairs ( X , Y ) ) ; } }
import java . io . * ; class GFG { static void maxValueAtIndexK ( int N , int K , int M ) { int S1 = 0 , S2 = 0 ; S1 = K * ( K + 1 ) / 2 ; S2 = ( N - K - 1 ) * ( N - K ) / 2 ; int X = ( M + S1 + S2 ) / N ; System . out . println ( X ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 1 , M = 7 ; maxValueAtIndexK ( N , K , M ) ; } }
import java . io . * ; class GFG { static void farthestCellDistance ( int N , int M , int R , int C ) { int d1 = N + M - R - C ; int d2 = R + C - 2 ; int d3 = N - R + C - 1 ; int d4 = M - C + R - 1 ; int maxDistance = Math . max ( d1 , Math . max ( d2 , Math . max ( d3 , d4 ) ) ) ; System . out . println ( maxDistance ) ; } public static void main ( String [ ] args ) { int N = 15 , M = 12 , R = 1 , C = 6 ; farthestCellDistance ( N , M , R , C ) ; } }
import java . io . * ; class GFG { static void maxSum ( int [ ] arr , int N ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 7 , 1 , 8 } ; int N = arr . length ; maxSum ( arr , N ) ; } }
import java . io . * ; class GFG { static int findInGrid ( int i , int j ) { if ( i == j ) return ( i * i - ( i - 1 ) ) ; else if ( i > j ) { if ( i % 2 == 0 ) return i * i - ( j - 1 ) ; else return ( i - 1 ) * ( i - 1 ) + 1 + ( j - 1 ) ; } else { if ( j % 2 == 0 ) return ( j - 1 ) * ( j - 1 ) + 1 + ( i - 1 ) ; else return j * j - ( i - 1 ) ; } } public static void main ( String [ ] args ) { int i = 3 , j = 4 ; System . out . println ( findInGrid ( i , j ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } void splitArray ( int arr [ ] , int N ) { TreeMap < Integer , Integer > mp = new TreeMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int G = 0 ; for ( Map . Entry < Integer , Integer > m : mp . entrySet ( ) ) { Integer i = m . getValue ( ) ; G = gcd ( G , i . intValue ( ) ) ; } if ( G > 1 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 4 , 3 , 2 , 1 } ; int n = arr . length ; new GFG ( ) . splitArray ( arr , n ) ; } }
import java . util . * ; class GFG { static void querySum ( int arr [ ] , int N , int Q [ ] [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int x = Q [ i ] [ 0 ] ; int y = Q [ i ] [ 1 ] ; int sum = 0 ; while ( x < N ) { sum += arr [ x ] ; x += y ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 7 , 5 , 4 } ; int Q [ ] [ ] = { { 2 , 1 } , { 3 , 2 } } ; int N = arr . length ; int M = Q . length ; querySum ( arr , N , Q , M ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static String greatestReducedNumber ( String num , String s ) { Boolean [ ] vis_s = new Boolean [ 10 ] ; Arrays . fill ( vis_s , Boolean . FALSE ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { vis_s [ ( int ) ( s . charAt ( i ) ) - 48 ] = true ; } int n = num . length ( ) ; int in = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis_s [ ( int ) num . charAt ( i ) - '0' ] ) { in = i ; break ; } } if ( in == - 1 ) { return num ; } for ( char dig = num . charAt ( in ) ; dig >= '0' ; dig -- ) { if ( vis_s [ ( int ) dig - '0' ] == false ) { num = num . substring ( 0 , in ) + dig + num . substring ( in + 1 , n ) ; break ; } } char LargestDig = '0' ; for ( char dig = '9' ; dig >= '0' ; dig -- ) { if ( vis_s [ dig - '0' ] == false ) { LargestDig = dig ; break ; } } for ( int i = in + 1 ; i < n ; i ++ ) { num = num . substring ( 0 , i ) + LargestDig ; } int Count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( num . charAt ( i ) == '0' ) Count ++ ; else break ; } num = num . substring ( Count , n ) ; if ( ( int ) num . length ( ) == 0 ) return "0" ; return num ; } public static void main ( String [ ] args ) { String N = "12345" ; String S = "23" ; System . out . print ( greatestReducedNumber ( N , S ) ) ; } }
class GFG { private static void minXOR ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( Arr [ i ] == 2 ) { System . out . print ( ( Arr [ i ] ^ 3 ) + " ▁ " ) ; } else { System . out . print ( ( Arr [ i ] ^ 2 ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int Arr [ ] = { 5 , 4 , 7 , 6 } ; int N = Arr . length ; minXOR ( Arr , N ) ; } }
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static int [ ] findArray ( int n ) { int Arr [ ] = new int [ n + 1 ] ; Arr [ 0 ] = 0 ; Arr [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) { Arr [ i ] = Arr [ i / 2 ] ; } else { Arr [ i ] = Arr [ ( i - 1 ) / 2 ] + Arr [ ( i - 1 ) / 2 + 1 ] ; } } return Arr ; } static int maxElement ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 ) return 1 ; int [ ] Arr = findArray ( n ) ; int ans = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , Arr [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int N = 7 ; System . out . println ( maxElement ( N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void constGraphWithCon ( int N , int K ) { int Max = ( ( N - 1 ) * ( N - 2 ) ) / 2 ; if ( K > Max ) { System . out . print ( - 1 + "NEW_LINE"); return ; } Vector < pair > ans = new Vector < > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { ans . add ( new pair ( i , j ) ) ; } } for ( int i = 0 ; i < ( N - 1 ) + Max - K ; i ++ ) { System . out . print ( ans . get ( i ) . first + " ▁ " + ans . get ( i ) . second + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 5 , K = 3 ; constGraphWithCon ( N , K ) ; } }
import java . util . * ; class GFG { static int min_steps_required ( int n , int m , int r , int c ) { int i , j ; int corner_steps_req = Integer . MAX_VALUE ; i = 1 ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = 1 ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = 1 ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; i = n ; j = m ; corner_steps_req = Math . min ( corner_steps_req , Math . abs ( r - i ) + Math . abs ( j - c ) ) ; int minimum_steps = Math . min ( 2 * ( n - 1 ) + m - 1 , 2 * ( m - 1 ) + n - 1 ) ; return minimum_steps + corner_steps_req ; } public static void main ( String [ ] args ) { int n = 3 ; int m = 2 ; int r = 1 ; int c = 1 ; System . out . print ( min_steps_required ( n , m , r , c ) ) ; } }
import java . util . * ; class GFG { static int min_elements ( int arr [ ] , int N , int K ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } Vector < Integer > Freq = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int i = it . getKey ( ) ; Freq . add ( mp . get ( i ) ) ; } Collections . sort ( Freq , Collections . reverseOrder ( ) ) ; int len = Freq . size ( ) ; if ( len <= K ) { return 0 ; } int cntMin = 0 ; for ( int i = K ; i < len ; i ++ ) { cntMin += Freq . get ( i ) ; } return cntMin ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 2 , 4 , 1 , 1 , 2 , 3 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( min_elements ( arr , N , K ) ) ; } }
class GFG { static int cntOnesArrWithGivenOp ( int arr [ ] , int N ) { int cntOnes = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i - 1 ; j < N ; j += i ) { arr [ j ] = arr [ j ] == 0 ? 1 : 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { cntOnes += 1 ; } } return cntOnes ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int cntOnesArrWithGivenOp ( int arr [ ] , int N ) { int cntOnes = 0 ; cntOnes = ( int ) Math . sqrt ( N ) ; return cntOnes ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 } ; int N = arr . length ; System . out . println ( cntOnesArrWithGivenOp ( arr , N ) ) ; } }
import java . io . * ; import java . lang . Math ; class GFG { static int cntKnightsAttackPawn ( int knights [ ] [ ] , int pawn [ ] , int M ) { int cntKnights = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int X = Math . abs ( knights [ i ] [ 0 ] - pawn [ 0 ] ) ; int Y = Math . abs ( knights [ i ] [ 1 ] - pawn [ 1 ] ) ; if ( ( X == 1 && Y == 2 ) || ( X == 2 && Y == 1 ) ) { cntKnights ++ ; } } return cntKnights ; } public static void main ( String [ ] args ) { int [ ] [ ] knights = { { 0 , 4 } , { 4 , 5 } , { 1 , 4 } , { 3 , 1 } } ; int [ ] pawn = new int [ ] { 2 , 3 } ; int M = knights . length ; System . out . println ( cntKnightsAttackPawn ( knights , pawn , M ) ) ; } }
import java . util . * ; class GFG { static void findWinnerGameRemoveGCD ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( mp . containsKey ( j ) ) { mp . put ( j , mp . get ( j ) + 1 ) ; } else { mp . put ( j , 1 ) ; } while ( arr [ i ] % j == 0 ) { arr [ i ] = arr [ i ] / j ; } } } if ( arr [ i ] > 1 ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } int maxCnt = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { maxCnt = Math . max ( maxCnt , i . getValue ( ) ) ; } if ( n % 2 == 0 ) { if ( maxCnt >= n - 1 ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } else { if ( maxCnt == n ) { System . out . print ( " Player ▁ 1" ) ; } else { System . out . print ( " Player ▁ 2" ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 } ; int N = arr . length ; findWinnerGameRemoveGCD ( arr , N ) ; } }
import java . io . * ; class GFG { static void minimumSwaps ( int arr1 [ ] , int arr2 [ ] , int n ) { int sumArr1 = 0 , sumArr2 = 0 ; for ( int i = 0 ; i < n ; ++ i ) { sumArr1 += arr1 [ i ] ; sumArr2 += arr2 [ i ] ; } if ( sumArr1 % 2 == 0 && sumArr2 % 2 == 0 ) { System . out . print ( 0 ) ; return ; } if ( sumArr1 % 2 != 0 && sumArr2 % 2 != 0 ) { int flag = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( arr1 [ i ] + arr2 [ i ] ) % 2 == 1 ) { flag = 1 ; break ; } } System . out . print ( flag ) ; return ; } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 11 , 14 , 20 , 2 } ; int arr2 [ ] = { 5 , 9 , 6 , 3 } ; int N = arr1 . length ; minimumSwaps ( arr1 , arr2 , N ) ; } }
import java . util . * ; class GFG { static void corpFlightBookings ( int [ ] [ ] Bookings , int N ) { int res [ ] = new int [ N ] ; for ( int i = 0 ; i < Bookings . length ; i ++ ) { int l = Bookings [ i ] [ 0 ] ; int r = Bookings [ i ] [ 1 ] ; int K = Bookings [ i ] [ 2 ] ; res [ l - 1 ] = res [ l - 1 ] + K ; if ( r <= res . length - 1 ) res [ r ] = ( - K ) + res [ r ] ; } for ( int i = 1 ; i < res . length ; i ++ ) res [ i ] = res [ i ] + res [ i - 1 ] ; for ( int i = 0 ; i < res . length ; i ++ ) { System . out . print ( res [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int bookings [ ] [ ] = { { 1 , 3 , 100 } , { 2 , 6 , 100 } , { 3 , 4 , 100 } } ; int N = 6 ; corpFlightBookings ( bookings , N ) ; } }
import java . util . * ; class GFG { static int findXOR ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static void findOddXOR ( int n ) { if ( n % 2 == 0 ) System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( n / 2 ) ) ) ) ; else System . out . print ( ( ( findXOR ( n ) ) ^ ( 2 * findXOR ( ( n - 1 ) / 2 ) ) ) ) ; } public static void main ( String [ ] args ) { int N = 11 ; findOddXOR ( N ) ; } }
import java . util . * ; class GFG { static void findSmallestNumber ( int n ) { for ( int i = n ; i <= ( n + 2520 ) ; ++ i ) { int possible = 1 ; int temp = i ; while ( temp != 0 ) { if ( temp % 10 != 0 ) { int digit = temp % 10 ; if ( i % digit != 0 ) { possible = 0 ; break ; } } temp /= 10 ; } if ( possible == 1 ) { System . out . println ( i ) ; return ; } } } public static void main ( String [ ] args ) { int N = 31 ; findSmallestNumber ( N ) ; } }
import java . util . Arrays ; class GFG { static int minValue ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int minimum = arr [ 0 ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] - minimum ) ; } return sum ; } static public void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 } ; int N = arr . length ; System . out . println ( minValue ( arr , N ) ) ; } }
import java . io . * ; class GFG { public static int countEvenPairs ( int N , int M ) { int count = 0 ; int nEven = ( int ) Math . floor ( ( double ) N / 2 ) ; int nOdd = ( int ) Math . ceil ( ( double ) N / 2 ) ; int mEven = ( int ) Math . floor ( ( double ) M / 2 ) ; int mOdd = ( int ) Math . ceil ( ( double ) M / 2 ) ; count = nEven * mEven + nOdd * mOdd ; return count ; } public static void main ( String [ ] args ) { int N = 4 ; int M = 6 ; System . out . print ( countEvenPairs ( N , M ) ) ; } }
import java . util . * ; class GFG { static void String_palindrome ( int N , int K ) { for ( int i = 0 ; i < K ; i ++ ) System . out . print ( " a " ) ; String s = " bcd " ; for ( int i = 0 ; i < N - K ; i ++ ) System . out . print ( s . charAt ( i % 3 ) ) ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 ; String_palindrome ( N , K ) ; } }
import java . util . * ; class GFG { static char lastRemovedCharacter ( char [ ] str ) { int n = str . length ; if ( n == 1 ) return str [ 0 ] ; if ( str [ n - 2 ] == '0' ) { return ( char ) ( '1' - str [ n - 1 ] + '0' ) ; } else return str [ n - 1 ] ; } public static void main ( String [ ] args ) { String str = "10010" ; System . out . print ( lastRemovedCharacter ( str . toCharArray ( ) ) ) ; } }
import java . util . * ; class GFG { static void findArray ( int N , int K ) { if ( N == 1 ) { System . out . print ( K + " ▁ " ) ; return ; } if ( N == 2 ) { System . out . print ( 0 + " ▁ " + K ) ; return ; } int P = N - 2 ; int Q = N - 1 ; int VAL = 0 ; for ( int i = 1 ; i <= ( N - 3 ) ; i ++ ) { System . out . print ( i + " ▁ " ) ; VAL ^= i ; } if ( VAL == K ) { System . out . print ( P + " ▁ " + Q + " ▁ " + ( P ^ Q ) ) ; } else { System . out . print ( 0 + " ▁ " + P + " ▁ " + ( P ^ K ^ VAL ) ) ; } } public static void main ( String [ ] args ) { int N = 4 , X = 6 ; findArray ( N , X ) ; } }
class GFG { static int findNumber ( int N ) { int count = 0 ; while ( ( N & 1 ) == 1 ) { N = N >> 1 ; count ++ ; } return N << count ; } public static void main ( String [ ] args ) { int N = 39 ; System . out . println ( findNumber ( N ) ) ; } }
import java . util . * ; class GFG { public static int min_elements ( int arr [ ] , int N ) { Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int cntMinRem = 0 ; for ( int key : mp . keySet ( ) ) { int i = key ; int val = mp . get ( i ) ; if ( val < i ) { cntMinRem += val ; } else if ( val > i ) { cntMinRem += ( val - i ) ; } } return cntMinRem ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 4 , 2 } ; System . out . println ( min_elements ( arr , arr . length ) ) ; } }
import java . util . * ; class GFG { static boolean CheckAllarrayEqual ( int [ ] arr , int N ) { if ( N == 1 ) { return true ; } int totalSum = arr [ 0 ] ; int secMax = Integer . MIN_VALUE ; int Max = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] >= Max ) { secMax = Max ; Max = arr [ i ] ; } else if ( arr [ i ] > secMax ) { secMax = arr [ i ] ; } totalSum += arr [ i ] ; } if ( ( secMax * ( N - 1 ) ) > totalSum ) { return false ; } if ( totalSum % ( N - 1 ) != 0 ) { return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 2 , 2 , 2 } ; int N = arr . length ; if ( CheckAllarrayEqual ( arr , N ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }
import java . util . * ; class GFG { static Vector < String > subString ( String s , int n ) { Vector < String > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int len = 1 ; len <= n - i ; len ++ ) { String find = s . substring ( i , i + len ) ; v . add ( find ) ; } } return v ; } static int IsPresent ( String str , String target ) { if ( str . contains ( target ) ) { return 1 ; } return - 1 ; } static void countSubStrings ( String S , String T ) { Vector < String > v = subString ( S , S . length ( ) ) ; int ans = 0 ; for ( String it : v ) { if ( IsPresent ( it , T ) != - 1 ) { ans ++ ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " dabc " ; String T = " ab " ; countSubStrings ( S , T ) ; } }
import java . util . * ; class GFG { static int minimumcntOperationReq ( int arr [ ] , int N ) { int cntOp = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( minimumcntOperationReq ( arr , N ) ) ; } }
class GFG { static void isSequenceValid ( int [ ] B , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( ( i + 1 ) % B [ i ] != 0 ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int [ ] arr = { 1 , 2 , 3 , 2 } ; isSequenceValid ( arr , N ) ; } }
class GFG { static void ConstArrayAdjacentCoprime ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 6 ; ConstArrayAdjacentCoprime ( N ) ; } }
class GFG { static int cntIndexesToMakeBalance ( int arr [ ] , int n ) { if ( n == 1 ) { return 1 ; } if ( n == 2 ) return 0 ; int sumEven = 0 ; int sumOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumEven += arr [ i ] ; } else { sumOdd += arr [ i ] ; } } int currOdd = 0 ; int currEven = arr [ 0 ] ; int res = 0 ; int newEvenSum = 0 ; int newOddSum = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( i % 2 != 0 ) { currOdd += arr [ i ] ; newEvenSum = currEven + sumOdd - currOdd ; newOddSum = currOdd + sumEven - currEven - arr [ i ] ; } else { currEven += arr [ i ] ; newOddSum = currOdd + sumEven - currEven ; newEvenSum = currEven + sumOdd - currOdd - arr [ i ] ; } if ( newEvenSum == newOddSum ) { res ++ ; } } if ( sumOdd == sumEven - arr [ 0 ] ) { res ++ ; } if ( n % 2 == 1 ) { if ( sumOdd == sumEven - arr [ n - 1 ] ) { res ++ ; } } else { if ( sumEven == sumOdd - arr [ n - 1 ] ) { res ++ ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( cntIndexesToMakeBalance ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void findNums ( int X , int Y ) { int A , B ; if ( X < Y ) { A = - 1 ; B = - 1 ; } else if ( ( ( Math . abs ( X - Y ) ) & 1 ) != 0 ) { A = - 1 ; B = - 1 ; } else if ( X == Y ) { A = 0 ; B = Y ; } else { A = ( X - Y ) / 2 ; if ( ( A & Y ) == 0 ) { B = ( A + Y ) ; } else { A = - 1 ; B = - 1 ; } } System . out . print ( A + " ▁ " + B ) ; } public static void main ( String [ ] args ) { int X = 17 , Y = 13 ; findNums ( X , Y ) ; } }
import java . util . * ; class GFG { static int maxEqrows ( Vector < Vector < Integer > > mat , int N , int M ) { HashMap < Vector < Integer > , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( mat . get ( i ) ) ) { mp . put ( mat . get ( i ) , mp . get ( mat . get ( i ) ) + 1 ) ; } else { mp . put ( mat . get ( i ) , 1 ) ; } } int cntMaxRows = 0 ; for ( int i = 0 ; i < N ; i ++ ) { Vector < Integer > onesCompRow = new Vector < Integer > ( ) ; for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . add ( 0 ) ; } for ( int j = 0 ; j < M ; j ++ ) { onesCompRow . set ( j , mat . get ( i ) . get ( j ) ^ 1 ) ; } if ( ! mp . containsKey ( mat . get ( i ) ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( onesCompRow ) ) ; } else if ( ! mp . containsKey ( onesCompRow ) ) { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) ) ; } else { cntMaxRows = Math . max ( cntMaxRows , mp . get ( mat . get ( i ) ) + mp . get ( onesCompRow ) ) ; } } return cntMaxRows ; } public static void main ( String [ ] args ) { Vector < Vector < Integer > > mat = new Vector < Vector < Integer > > ( ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . add ( new Vector < Integer > ( ) ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 0 ) ; mat . get ( 0 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 0 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 1 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 1 ) ; mat . get ( 2 ) . add ( 0 ) ; int N = mat . size ( ) ; int M = mat . get ( 0 ) . size ( ) ; System . out . println ( maxEqrows ( mat , N , M ) ) ; } }
import java . util . * ; class GFG { public static void minimumCost ( String a , String b ) { int fre1 [ ] = new int [ 256 ] ; int fre2 [ ] = new int [ 256 ] ; for ( char c : a . toCharArray ( ) ) fre1 [ ( int ) ( c ) ] ++ ; for ( char c : b . toCharArray ( ) ) fre2 [ ( int ) ( c ) ] ++ ; int mincost = 0 ; for ( int i = 0 ; i < 256 ; i ++ ) { mincost += Math . abs ( fre1 [ i ] - fre2 [ i ] ) ; } System . out . println ( mincost ) ; } public static void main ( String [ ] args ) { String A = "1AB + - " , B = " cc " ; minimumCost ( A , B ) ; } }
import java . util . * ; class GFG { static int findMaxSumByAlternatingSign ( int arr [ ] , int N ) { int cntNeg = 0 ; int MaxAltSum = 0 ; int SmValue = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { cntNeg += 1 ; } sum += Math . abs ( arr [ i ] ) ; SmValue = Math . min ( SmValue , Math . abs ( arr [ i ] ) ) ; } MaxAltSum = sum ; if ( cntNeg % 2 == 1 ) { MaxAltSum -= 2 * SmValue ; } return MaxAltSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , - 2 , - 4 , 5 } ; int N = arr . length ; System . out . print ( findMaxSumByAlternatingSign ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void constructNewArraySumZero ( int arr [ ] , int N ) { int newArr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { newArr [ i ] = arr [ i + 1 ] ; } else { newArr [ i ] = - arr [ i - 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( newArr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , - 5 , - 6 , 8 } ; int N = arr . length ; constructNewArraySumZero ( arr , N ) ; } }
import java . util . * ; class GFG { public static void bitonicSequence ( int num , int lower , int upper ) { if ( num > ( upper - lower ) * 2 + 1 ) { System . out . println ( - 1 ) ; return ; } Deque < Integer > ans = new ArrayDeque < > ( ) ; for ( int i = 0 ; i < Math . min ( upper - lower + 1 , num - 1 ) ; i ++ ) ans . add ( upper - i ) ; for ( int i = 0 ; i < num - ans . size ( ) ; i ++ ) ans . addFirst ( upper - i - 1 ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , L = 3 , R = 10 ; bitonicSequence ( N , L , R ) ; } }
class GFG { static int countSubStrings ( String s , String t ) { int answ = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { for ( int j = 0 ; j < t . length ( ) ; j ++ ) { if ( t . charAt ( j ) != s . charAt ( i ) ) { answ += 1 ; int k = 1 ; int z = - 1 ; int q = 1 ; while ( j + z >= 0 && 0 <= i + z && s . charAt ( i + z ) == t . charAt ( j + z ) ) { z -= 1 ; answ += 1 ; q += 1 ; } while ( s . length ( ) > i + k && j + k < t . length ( ) && s . charAt ( i + k ) == t . charAt ( j + k ) ) { k += 1 ; answ += q ; z = - 1 ; } } } } return answ ; } public static void main ( String [ ] args ) { String S = " aba " ; String T = " baba " ; System . out . println ( countSubStrings ( S , T ) ) ; } }
import java . io . * ; class GFG { public static int minimumCntOfFlipsRequired ( String str ) { int n = str . length ( ) ; int zeros = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { zeros ++ ; } } if ( zeros == 0 zeros == n ) { return 0 ; } int minFlips = Integer . MAX_VALUE ; int currOnes = 0 ; int flips ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '1' ) { currOnes ++ ; } flips = currOnes + ( zeros - ( i + 1 - currOnes ) ) ; minFlips = Math . min ( minFlips , flips ) ; } return minFlips ; } public static void main ( String [ ] args ) { String s1 = "100101" ; System . out . println ( minimumCntOfFlipsRequired ( s1 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void checkCount ( int A [ ] , int Q [ ] [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) { int L = Q [ i ] [ 0 ] ; int R = Q [ i ] [ 1 ] ; L -- ; R -- ; if ( ( A [ L ] < A [ L + 1 ] ) != ( A [ R - 1 ] < A [ R ] ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 13 , 12 , 14 } ; int Q [ ] [ ] = { { 1 , 4 } , { 2 , 4 } } ; int q = Q . length ; checkCount ( arr , Q , q ) ; } }
import java . util . * ; class solution { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void canReach ( int N , int A , int B , int K ) { int gcd = GCD ( N , K ) ; if ( Math . abs ( A - B ) % gcd == 0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int N = 5 , A = 2 , B = 1 , K = 2 ; canReach ( N , A , B , K ) ; } }
import java . util . * ; class GFG { static int separate ( int arr [ ] , int n , int parity ) { int count = 1 , res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) != 0 && ( ( arr [ i - 1 ] + parity ) & 1 ) != 0 ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . println ( arr [ i ] + " ▁ " ) ; } static void requiredOps ( int arr [ ] , int N ) { int res1 = separate ( arr , N , 0 ) ; int res2 = separate ( arr , N , 1 ) ; System . out . print ( Math . max ( res1 , res2 ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 } ; int N = arr . length ; requiredOps ( arr , N ) ; } }
import java . util . * ; class GFG { public static boolean [ ] SieveOfEratosthenes ( int N ) { boolean [ ] isPrime = new boolean [ N + 1 ] ; Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( isPrime [ p ] ) { for ( int i = p * p ; i <= N ; i += p ) { isPrime [ i ] = false ; } } } return isPrime ; } public static int cntPairsdiffOfPrimeisPrime ( int N ) { int cntPairs = 0 ; boolean [ ] isPrime = SieveOfEratosthenes ( N ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( isPrime [ i ] && isPrime [ i - 2 ] ) { cntPairs += 2 ; } } return cntPairs ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( cntPairsdiffOfPrimeisPrime ( N ) ) ; } }
import java . util . * ; class GFG { static void longestSubsequence ( int arr [ ] , int N ) { int count = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] != arr [ i - 1 ] ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 8 , 1 , 2 , 2 , 5 , 5 , 1 } ; int N = arr . length ; longestSubsequence ( arr , N ) ; } }
import java . util . * ; class GFG { static int substringCount ( String s ) { int [ ] freq = new int [ 26 ] ; char max_char = ' # ' ; int maxfreq = Integer . MIN_VALUE ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; if ( maxfreq < freq [ s . charAt ( i ) - ' a ' ] ) maxfreq = freq [ s . charAt ( i ) - ' a ' ] ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( maxfreq == freq [ i ] ) { max_char = ( char ) ( i + ' a ' ) ; break ; } } int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char ch = s . charAt ( i ) ; if ( max_char == ch ) { ans += ( s . length ( ) - i ) ; } } return ans ; } public static void main ( String [ ] args ) { String S = " abcab " ; System . out . println ( substringCount ( S ) ) ; } }
import java . util . * ; class GFG { static void minSum ( int [ ] arr , int n ) { int [ ] ans = new int [ n ] ; Arrays . fill ( ans , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i - 1 ] + 1 ) ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] + 1 ) ; } } int s = 0 ; for ( int x : ans ) { s = s + x ; } System . out . print ( s + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int N = arr . length ; minSum ( arr , N ) ; } }
import java . util . * ; class GFG { static void minSteps ( String S ) { String new_str = " " ; int N = S . length ( ) ; int i = 0 ; while ( i < N ) { new_str += S . charAt ( i ) ; int j = i ; while ( i < N && S . charAt ( i ) == S . charAt ( j ) ) ++ i ; } System . out . print ( ( int ) Math . ceil ( ( new_str . length ( ) + 1 ) / 2.0 ) ) ; } public static void main ( String [ ] args ) { String S = "0010100" ; minSteps ( S ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countPairs ( int [ ] arr , int N ) { int cntPairs = 0 ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { mp . put ( arr [ i ] , mp . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } for ( Map . Entry < Integer , Integer > freq : mp . entrySet ( ) ) { cntPairs += ( freq . getValue ( ) * ( freq . getValue ( ) - 1 ) ) / 2 ; } return cntPairs ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 1 , 2 , 2 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int minimumMoves ( int n ) { int cnt2 = 0 , cnt5 = 0 ; while ( n % 2 == 0 ) { n /= 2 ; cnt2 ++ ; } while ( n % 5 == 0 ) { n /= 5 ; cnt5 ++ ; } if ( n == 1 && cnt2 <= cnt5 ) { return 2 * cnt5 - cnt2 ; } else return - 1 ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . print ( minimumMoves ( N ) ) ; } }
import java . util . * ; class solution { static void check ( int n ) { if ( ( n & n - 1 ) != 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int n = 5 ; check ( n ) ; } }
import java . io . * ; class GFG { static long binCoff ( long n , long r ) { long val = 1 ; int i ; if ( r > ( n - r ) ) r = ( n - r ) ; for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static void findWays ( int M ) { int n = M / 2 ; long a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; System . out . print ( b ) ; } public static void main ( String [ ] args ) { int M = 4 , X = 5 ; findWays ( M ) ; } }
import java . io . * ; class GFG { static int Moves_Calculator ( int x , int y , int row , int col ) { int total_moves = 0 ; if ( ( row - x ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( row - x ) , ( col - y ) ) ; if ( ( y - 1 ) > 0 && ( x - 1 ) > 0 ) total_moves += Math . min ( ( y - 1 ) , ( x - 1 ) ) ; if ( ( x - 1 ) > 0 && ( col - y ) > 0 ) total_moves += Math . min ( ( x - 1 ) , ( col - y ) ) ; if ( ( row - x ) > 0 && ( y - 1 ) > 0 ) total_moves += Math . min ( ( row - x ) , ( y - 1 ) ) ; total_moves += ( row - 1 ) + ( col - 1 ) ; int king_moves = 0 ; if ( x + 1 <= row ) king_moves += 1 ; if ( x - 1 > 0 ) king_moves += 1 ; if ( y + 1 <= col ) king_moves += 1 ; if ( y - 1 > 0 ) king_moves += 1 ; if ( x + 1 <= row && y + 1 <= col ) king_moves += 1 ; if ( x + 1 <= row && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y - 1 > 0 ) king_moves += 1 ; if ( x - 1 > 0 && y + 1 <= col ) king_moves += 1 ; return total_moves - king_moves ; } public static void main ( String [ ] args ) { int n = 8 , m = 8 ; int x = 1 , y = 1 ; System . out . println ( Moves_Calculator ( x , y , m , n ) ) ; } }
import java . io . * ; class GFG { static int equvInverse ( int [ ] arr , int N , int P ) { int cntElem = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] * arr [ i ] ) % P == 1 ) { cntElem ++ ; } } return cntElem ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 4 , 5 } ; int N = arr . length ; int P = 7 ; System . out . println ( equvInverse ( arr , N , P ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int power ( int K , int M ) { int res = 1 ; while ( M > 0 ) { if ( ( M & 1 ) == 1 ) { res = ( res * K ) ; } M = M >> 1 ; K = ( K * K ) ; } return res ; } static int cntWays ( int arr [ ] , int N , int K ) { int cntways = 0 ; int M = 0 ; Set < Integer > st = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } M = st . size ( ) ; cntways = power ( K , M ) ; return cntways ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 } ; int N = arr . length ; int K = 2 ; System . out . println ( cntWays ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static int mod = ( int ) 1e9 + 7 ; static void minProduct ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= ( n - 4 ) / 2 ; i ++ ) { ans = ( int ) ( 1L * ans * ( n - 2 ) ) % mod ; } ans = ( int ) ( 1L * ans * ( n - 2 ) * ( n - 1 ) ) % mod ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 8 ; minProduct ( N ) ; } }
import java . io . * ; class GFG { static int minAbsDiff ( int N ) { int sumSet1 = 0 ; int sumSet2 = 0 ; for ( int i = N ; i > 0 ; i -- ) { if ( sumSet1 <= sumSet2 ) { sumSet1 += i ; } else { sumSet2 += i ; } } return Math . abs ( sumSet1 - sumSet2 ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minAbsDiff ( N ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int oneCount = 0 ; int desiredPair = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { oneCount ++ ; } if ( arr [ i ] == 0 ) { desiredPair += oneCount ; } } return desiredPair ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 1 , 0 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void smlstNonNeg ( int arr [ ] , int N ) { int smNonNeg = 0 ; Boolean [ ] hash = new Boolean [ N + 1 ] ; Arrays . fill ( hash , false ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= 0 && arr [ i ] < N ) { hash [ arr [ i ] ] = true ; } while ( hash [ smNonNeg ] ) { smNonNeg ++ ; } System . out . print ( smNonNeg + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 3 , 5 } ; int N = arr . length ; smlstNonNeg ( arr , N ) ; } }
import java . util . * ; class GFG { static void solve ( int arr [ ] , int N , int X ) { Vector < Integer > A = new Vector < Integer > ( ) , B = new Vector < Integer > ( ) ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( 2 * arr [ i ] ) < X ) { A . add ( arr [ i ] ) ; } else if ( ( 2 * arr [ i ] ) > X ) { B . add ( arr [ i ] ) ; } else { if ( c % 2 == 0 ) { A . add ( arr [ i ] ) ; } else { B . add ( arr [ i ] ) ; } c ++ ; } } System . out . print ( " The ▁ First ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { System . out . print ( A . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " The ▁ Second ▁ Array ▁ is ▁ - ▁ " ) ; for ( int i = 0 ; i < B . size ( ) ; i ++ ) { System . out . print ( B . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 3 , 6 , 2 , 4 , 3 } ; int X = 7 ; int N = arr . length ; solve ( arr , N , X ) ; } }
import java . util . * ; class GFG { static int cntMinSub ( int arr [ ] , int N , int K ) { int res = 0 ; int start = 0 ; int end = N - 1 ; Arrays . sort ( arr ) ; while ( end - start > 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { start ++ ; } else { res ++ ; end -- ; } } if ( end - start == 1 ) { if ( arr [ start ] + arr [ end ] <= K ) { res ++ ; start ++ ; end -- ; } else { res ++ ; end -- ; } } if ( start == end ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 8 , 10 , 20 , 25 } ; int N = arr . length ; int K = 26 ; System . out . print ( cntMinSub ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static int condition ( int a , int b ) { int d = Math . abs ( a - b ) , count = 0 ; for ( int i = 1 ; i <= Math . sqrt ( d ) ; i ++ ) { if ( d % i == 0 ) { if ( d / i == i ) count += 1 ; else count += 2 ; } } return count ; } public static void main ( String [ ] args ) { int x = 2 , y = 6 ; System . out . println ( condition ( x , y ) ) ; } }
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int x , int y ) { if ( y == 0 ) return 1 ; int p = ( int ) ( power ( x , y / 2 ) % mod ) ; p = ( p * p ) % mod ; if ( y % 2 == 1 ) { p = ( x * p ) % mod ; } return p ; } static int TotalNumber ( int N ) { int ans = ( int ) ( ( power ( 10 , N ) - 2 * power ( 9 , N ) + power ( 8 , N ) + 2 * mod ) % mod ) ; return ans ; } public static void main ( String [ ] args ) { int N = 10 , X = 3 , Y = 4 ; System . out . print ( TotalNumber ( N ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static boolean checkDigits ( int n ) { do { int r = n % 10 ; if ( r == 3 r == 4 r == 6 r == 7 r == 9 ) return false ; n /= 10 ; } while ( n != 0 ) ; return true ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isAllPrime ( int n ) { return isPrime ( n ) && checkDigits ( n ) ; } public static void main ( String [ ] args ) { int N = 101 ; if ( isAllPrime ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void generateArray ( int arr [ ] , int n ) { Deque < Integer > ans = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) != 0 ) ans . addFirst ( arr [ i ] ) ; else ans . add ( arr [ i ] ) ; } if ( ( n & 1 ) != 0 ) { Collections . reverse ( Arrays . asList ( ans ) ) ; } for ( int x : ans ) { System . out . print ( x + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; generateArray ( arr , n ) ; } }
import java . util . * ; class GFG { static void findPlayer ( String str [ ] , int n ) { int move_first = 0 ; int move_sec = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] . charAt ( 0 ) == str [ i ] . charAt ( str [ i ] . length ( ) - 1 ) ) { if ( str [ i ] . charAt ( 0 ) == 48 ) move_first ++ ; else move_sec ++ ; } } if ( move_first <= move_sec ) { System . out . print ( " Player ▁ 2 ▁ wins " ) ; } else { System . out . print ( " Player ▁ 1 ▁ wins " ) ; } } public static void main ( String [ ] args ) { String str [ ] = { "010" , "101" } ; int N = str [ 0 ] . length ( ) ; findPlayer ( str , N ) ; } }
import java . util . * ; class GFG { static String closestgtNum ( String str ) { String res = " " ; int n = str . length ( ) ; if ( str . charAt ( 0 ) < '9' ) { res += ( char ) ( str . charAt ( 0 ) + 1 ) ; } else { res += ( char ) ( '1' ) ; res += ( char ) ( '0' ) ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { res += ( char ) ( '0' ) ; } return res ; } public static void main ( String [ ] args ) { String str = "120" ; System . out . print ( closestgtNum ( str ) ) ; } }
import java . util . * ; class GFG { static int find_next ( int n , int k ) { int ans = 0 ; if ( ( n & ( 1L << k ) ) == 0 ) { int cur = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( ( n & ( 1L << i ) ) > 0 ) cur += 1L << i ; } ans = ( int ) ( n - cur + ( 1L << k ) ) ; } else { int first_unset_bit = - 1 , cur = 0 ; for ( int i = 0 ; i < 64 ; i ++ ) { if ( ( n & ( 1L << i ) ) == 0 ) { first_unset_bit = i ; break ; } else cur += ( 1L << i ) ; } ans = ( int ) ( n - cur + ( 1L << first_unset_bit ) ) ; if ( ( ans & ( 1L << k ) ) == 0 ) ans += ( 1L << k ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 , K = 2 ; System . out . print ( find_next ( N , K ) ) ; } }
import java . io . * ; class GFG { static int power ( int x , int y ) { int temp ; if ( y == 0 ) return 1 ; temp = power ( x , y / 2 ) ; if ( y % 2 == 0 ) return temp * temp ; else return x * temp * temp ; } public static void maximumPossibleSum ( int N , int X ) { System . out . println ( X * ( power ( 2 , N ) - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 3 , X = 5 ; maximumPossibleSum ( N , X ) ; } }
import java . util . * ; class GFG { static int countOfPairs ( int x , int y ) { int counter = 1 ; while ( x > 0 y > 0 ) { int bit1 = x % 2 ; int bit2 = y % 2 ; x >>= 1 ; y >>= 1 ; if ( bit1 == 1 && bit2 == 0 ) { counter *= 2 ; continue ; } if ( ( bit1 & bit2 ) > 0 ) { counter = 0 ; break ; } } return counter ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 ; System . out . print ( countOfPairs ( X , Y ) ) ; } }
public class GCD { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findGCD ( int arr [ ] , int N ) { int result = 0 ; for ( int element : arr ) { result = gcd ( result , element ) ; if ( result == 1 ) { return 1 ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( findGCD ( arr , N ) ) ; } }
import java . util . * ; class GFG { static boolean check_digits ( long N ) { while ( N > 0 ) { int n = ( int ) ( N % 10 ) ; if ( ( n != 0 ) && ( n != 1 ) && ( n != 4 ) && ( n != 9 ) ) { return false ; } N = N / 10 ; } return true ; } static boolean is_perfect ( long N ) { double n = Math . sqrt ( N ) ; if ( Math . floor ( n ) != Math . ceil ( n ) ) { return false ; } return true ; } static void isFullSquare ( long N ) { if ( is_perfect ( N ) && check_digits ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { long N = 144 ; isFullSquare ( N ) ; } }
import java . util . * ; class GFG { static void countSubString ( char [ ] s , int n ) { HashMap < Character , Integer > mp = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) - 1 ) ; for ( Map . Entry < Character , Integer > value : mp . entrySet ( ) ) { if ( value . getKey ( ) == s [ i ] ) { continue ; } else { cnt += value . getValue ( ) ; } } ans += cnt ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " abcab " ; int N = 5 ; countSubString ( S . toCharArray ( ) , N ) ; } }
class GFG { static int getPosition ( int N , int M ) { if ( M > ( N / 2 ) ) { return ( M - ( N / 2 ) ) ; } return ( M + ( N / 2 ) ) ; } public static void main ( String [ ] args ) { int N = 8 , M = 5 ; System . out . print ( getPosition ( N , M ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void number_of_strings ( int N , int M ) { int ans = 0 ; while ( N > 0 && M > 0 ) { if ( N > M ) { if ( N >= 2 ) { N -= 2 ; -- M ; ++ ans ; } else { break ; } } else { if ( M >= 2 ) { M -= 2 ; -- N ; ++ ans ; } else { break ; } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 4 , M = 19 ; number_of_strings ( N , M ) ; } }
class GFG { static boolean NegEqu ( int N , int X , int Y ) { while ( X > 0 && ( N > N / 2 + 10 ) ) { N = N / 2 + 10 ; X -= 1 ; } while ( Y > 0 ) { N = N - 10 ; Y -= 1 ; } if ( N <= 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int N = 100 ; int X = 3 ; int Y = 4 ; if ( NegEqu ( N , X , Y ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int findMaxSoln ( int n , int x , int y ) { int ans = Integer . MIN_VALUE ; for ( int k = 0 ; k <= n ; k ++ ) { if ( k % x == y ) { ans = Math . max ( ans , k ) ; } } return ( ( ans >= 0 && ans <= n ) ? ans : - 1 ) ; } public static void main ( String [ ] args ) { int n = 15 , x = 10 , y = 5 ; System . out . print ( findMaxSoln ( n , x , y ) ) ; } }
import java . util . * ; class GFG { static int RightAngled ( int a [ ] [ ] , int n ) { HashMap < Integer , Integer > xpoints = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > ypoints = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . containsKey ( a [ i ] [ 0 ] ) ) { xpoints . put ( a [ i ] [ 0 ] , xpoints . get ( a [ i ] [ 0 ] ) + 1 ) ; } else { xpoints . put ( a [ i ] [ 0 ] , 1 ) ; } if ( ypoints . containsKey ( a [ i ] [ 1 ] ) ) { ypoints . put ( a [ i ] [ 1 ] , ypoints . get ( a [ i ] [ 1 ] ) + 1 ) ; } else { ypoints . put ( a [ i ] [ 1 ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( xpoints . get ( a [ i ] [ 0 ] ) >= 1 && ypoints . get ( a [ i ] [ 1 ] ) >= 1 ) { count += ( xpoints . get ( a [ i ] [ 0 ] ) - 1 ) * ( ypoints . get ( a [ i ] [ 1 ] ) - 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] [ ] = { { 1 , 2 } , { 2 , 1 } , { 2 , 2 } , { 2 , 3 } , { 3 , 2 } } ; System . out . print ( RightAngled ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void printArray ( Vector < Integer > brr ) { for ( int it : brr ) { System . out . print ( it + " ▁ " ) ; } } static void RepeatingSubarray ( int arr [ ] , int N ) { if ( N < 2 ) { System . out . print ( " - 1" ) ; } Vector < Integer > brr = new Vector < Integer > ( ) ; brr . add ( arr [ 0 ] ) ; brr . add ( arr [ 1 ] ) ; for ( int i = 2 ; i < N / 2 + 1 ; i ++ ) { if ( N % i == 0 ) { boolean a = false ; int n = brr . size ( ) ; int j = i ; while ( j < N ) { int K = j % i ; if ( arr [ j ] == brr . get ( K ) ) { j ++ ; } else { a = true ; break ; } } if ( ! a && j == N ) { printArray ( brr ) ; return ; } } brr . add ( arr [ i ] ) ; } System . out . print ( " - 1" ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 , 2 , 2 , 1 , 2 , 2 } ; int N = arr . length ; RepeatingSubarray ( arr , N ) ; } }
import java . util . * ; class GFG { static int smallestCommon ( int a , int b , int c , int d ) { if ( a == c ) return a ; if ( a > c ) { swap ( a , c ) ; swap ( b , d ) ; } int first_term_diff = ( c - a ) ; int possible_y ; for ( possible_y = 0 ; possible_y < b ; possible_y ++ ) { if ( ( first_term_diff % b + possible_y * d ) % b == 0 ) { break ; } } if ( possible_y != b ) { return c + possible_y * d ; } return - 1 ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int A = 2 , B = 20 , C = 19 , D = 9 ; System . out . print ( smallestCommon ( A , B , C , D ) ) ; } }
import java . util . * ; class GFG { static int MinimumFlips ( String s , int n ) { int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( s . charAt ( i ) == '1' ? 1 : 0 ) ; } int [ ] oddone = new int [ n + 1 ] ; int [ ] evenone = new int [ n + 1 ] ; oddone [ 0 ] = 0 ; evenone [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; } else { oddone [ i + 1 ] = oddone [ i ] + ( a [ i ] == 0 ? 1 : 0 ) ; evenone [ i + 1 ] = evenone [ i ] + ( a [ i ] == 1 ? 1 : 0 ) ; } } int minimum = Math . min ( oddone [ n ] , evenone [ n ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( n % 2 != 0 ) { minimum = Math . min ( minimum , oddone [ n ] - oddone [ i + 1 ] + evenone [ i + 1 ] ) ; minimum = Math . min ( minimum , evenone [ n ] - evenone [ i + 1 ] + oddone [ i + 1 ] ) ; } } return minimum ; } public static void main ( String [ ] args ) { String S = "000001100" ; int n = S . length ( ) ; System . out . print ( MinimumFlips ( S , n ) ) ; } }
import java . util . * ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; return 1 ; } static void shortestpath ( int m , int n ) { Vector < Integer > mfactor = new Vector < > ( ) ; Vector < Integer > nfactor = new Vector < > ( ) ; HashMap < Integer , Integer > fre = new HashMap < > ( ) ; mfactor . add ( m ) ; fre . put ( m , 1 ) ; while ( m != 1 ) { if ( isprm ( m ) != 0 ) { mfactor . add ( 1 ) ; fre . put ( 1 , 1 ) ; m = 1 ; } else { for ( int i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { mfactor . add ( m / i ) ; fre . put ( m / i , 1 ) ; m = ( m / i ) ; break ; } } } } nfactor . add ( n ) ; while ( fre . containsKey ( n ) && fre . get ( n ) != 1 ) { if ( isprm ( n ) != 0 ) { nfactor . add ( 1 ) ; n = 1 ; } else { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { nfactor . add ( n / i ) ; n = ( n / i ) ; break ; } } } } for ( int i = 0 ; i < mfactor . size ( ) ; i ++ ) { if ( mfactor . get ( i ) == n ) break ; System . out . print ( mfactor . get ( i ) + " ▁ < - - > ▁ " ) ; } for ( int i = nfactor . size ( ) - 1 ; i >= 0 ; i -- ) { if ( i == 0 ) System . out . print ( nfactor . get ( i ) ) ; else System . out . print ( nfactor . get ( i ) + " ▁ < - - > ▁ " ) ; } } public static void main ( String [ ] args ) { int m = 18 , n = 19 ; shortestpath ( m , n ) ; } }
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int NumberOfTrees ( int arr [ ] , int N ) { int maxElement = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int level [ ] = new int [ maxElement + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { level [ arr [ i ] ] ++ ; } if ( arr [ 0 ] != 0 level [ 0 ] != 1 ) { return 0 ; } int ans = 1 ; for ( int i = 0 ; i < maxElement ; i ++ ) { for ( int j = 0 ; j < level [ i + 1 ] ; j ++ ) { ans = ( ans * level [ i ] ) % mod ; } } return ans ; } public static void main ( String [ ] args ) { int N = 7 ; int arr [ ] = { 0 , 3 , 2 , 1 , 2 , 2 , 1 } ; System . out . print ( NumberOfTrees ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static String gameMax ( String S ) { int N = S . length ( ) ; List < Integer > list = new ArrayList < > ( ) ; int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) { one ++ ; } else { if ( one != 0 ) { list . add ( one ) ; } one = 0 ; } } if ( one != 0 ) { list . add ( one ) ; } Collections . sort ( list , Collections . reverseOrder ( ) ) ; int score_1 = 0 , score_2 = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { if ( list . get ( i ) % 2 == 1 ) { score_1 += list . get ( i ) ; } else { score_2 += list . get ( i ) ; } } if ( score_1 == score_2 ) return " - 1" ; return ( score_1 > score_2 ) ? " Player ▁ 1" : " Player ▁ 2" ; } public static void main ( String [ ] args ) { String S = "11111101" ; System . out . println ( gameMax ( S ) ) ; } }
import java . util . * ; class GFG { static void minCost ( String str , int a , int b ) { int openUnbalanced = 0 ; int closedUnbalanced = 0 ; int openCount = 0 ; int closedCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { openUnbalanced ++ ; openCount ++ ; } else { if ( openUnbalanced == 0 ) closedUnbalanced ++ ; else openUnbalanced -- ; closedCount ++ ; } } int result = a * ( Math . abs ( openCount - closedCount ) ) ; if ( closedCount > openCount ) closedUnbalanced -= ( closedCount - openCount ) ; if ( openCount > closedCount ) openUnbalanced -= ( openCount - closedCount ) ; result += Math . min ( a * ( openUnbalanced + closedUnbalanced ) , b * closedUnbalanced ) ; System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " ) ) ( ) ( ( ) ( ) ( " ; int A = 1 , B = 3 ; minCost ( str , A , B ) ; } }
import java . util . * ; class GFG { static int Mod = ( int ) ( 1e9 + 7 ) ; static int power ( int X , int Y , int Mod ) { if ( Y == 0 ) return 1 ; int p = power ( X , Y / 2 , Mod ) % Mod ; p = ( p * p ) % Mod ; if ( ( Y & 1 ) != 0 ) { p = ( X * p ) % Mod ; } return p ; } static int Inversefactorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return power ( fact , Mod - 2 , Mod ) ; } static int factorial ( int N ) { if ( N <= 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( fact * i ) % Mod ; } return fact ; } static int nck ( int N , int K ) { int factN = factorial ( N ) ; int inv = Inversefactorial ( K ) ; int invFact = Inversefactorial ( N - K ) ; return ( ( ( factN * inv ) % Mod ) * invFact ) % Mod ; } static int TotalWaYs ( int N , int M ) { if ( ( ( N + M ) % 3 ) != 0 ) return 0 ; int X = N - ( N + M ) / 3 ; int Y = M - ( N + M ) / 3 ; if ( X < 0 Y < 0 ) return 0 ; return nck ( X + Y , Y ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . print ( TotalWaYs ( N , M ) ) ; } }
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 " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
import java . util . * ; class Main { static void mindigits ( int n ) { if ( n % 9 == 0 ) { System . out . println ( n / 9 ) ; } else { System . out . println ( ( n / 9 ) + 1 ) ; } } public static void main ( String [ ] args ) { int n1 = 24 ; int n2 = 18 ; mindigits ( n1 ) ; mindigits ( n2 ) ; } }
import java . util . * ; class GFG { static boolean checkEquall ( int arr [ ] , int N ) { int sumEven = 0 , sumOdd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) sumOdd += arr [ i ] ; else sumEven += arr [ i ] ; } if ( sumEven == sumOdd ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 3 , 5 , 7 } ; int N = arr . length ; if ( checkEquall ( arr , N ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } }
import java . util . * ; class GFG { static boolean isSumOfPower ( int n ) { boolean [ ] isSum = new boolean [ n + 1 ] ; Vector < Integer > perfectPowers = new Vector < Integer > ( ) ; perfectPowers . add ( 1 ) ; for ( int i = 0 ; i < ( n + 1 ) ; i ++ ) { isSum [ i ] = false ; } for ( int i = 2 ; i < ( n + 1 ) ; i ++ ) { if ( isSum [ i ] == true ) { perfectPowers . add ( i ) ; continue ; } for ( int j = i * i ; j > 0 && j < ( n + 1 ) ; j *= i ) { isSum [ j ] = true ; } } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { isSum [ perfectPowers . get ( i ) ] = false ; } for ( int i = 0 ; i < perfectPowers . size ( ) ; i ++ ) { for ( int j = i ; j < perfectPowers . size ( ) ; j ++ ) { int sum = perfectPowers . get ( i ) + perfectPowers . get ( j ) ; if ( sum < ( n + 1 ) ) isSum [ sum ] = true ; } } return isSum [ n ] ; } public static void main ( String [ ] args ) { int n = 9 ; if ( isSumOfPower ( n ) ) { System . out . print ( "trueNEW_LINE"); } else { System . out . print ( "falseNEW_LINE"); } } }
import java . util . * ; class GFG { public static void countEvenSum ( int low , int high , int k ) { int even_count = high / 2 - ( low - 1 ) / 2 ; int odd_count = ( high + 1 ) / 2 - low / 2 ; long even_sum = 1 ; long odd_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { long prev_even = even_sum ; long prev_odd = odd_sum ; even_sum = ( prev_even * even_count ) + ( prev_odd * odd_count ) ; odd_sum = ( prev_even * odd_count ) + ( prev_odd * even_count ) ; } System . out . println ( even_sum ) ; } public static void main ( String [ ] args ) { int low = 4 ; int high = 5 ; int K = 3 ; countEvenSum ( low , high , K ) ; } }
import java . util . * ; class GFG { static void update ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] += ( i + 1 ) ; } } static boolean check ( int arr [ ] , int N ) { int f = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; } } if ( count >= 2 ) { f = 1 ; break ; } } if ( f == 1 ) return true ; else return false ; } static void incrementCount ( int arr [ ] , int N ) { int min = 0 ; while ( check ( arr , N ) != true ) { update ( arr , N ) ; min ++ ; } System . out . println ( min ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 12 , 8 , 4 } ; incrementCount ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void checkPossible ( int a [ ] ) { int even_no_count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 == 0 ) even_no_count ++ ; } if ( a . length / 2 > even_no_count ) { System . out . println ( " No " ) ; return ; } System . out . println ( " Yes " ) ; int j = 0 ; for ( int i = 1 ; i < a . length ; i += 2 ) { if ( a [ i ] % 2 == 0 ) continue ; else { while ( j < a . length && a [ j ] % 2 != 0 ) j += 2 ; a [ i ] += a [ j ] ; a [ j ] = a [ i ] - a [ j ] ; a [ i ] -= a [ j ] ; } } for ( int i = 0 ; i < a . length ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; checkPossible ( arr ) ; } }
import java . util . * ; class GFG { static int maxORminusXOR ( int N ) { int MSB = ( int ) Math . ceil ( Math . log ( N ) ) ; int M = 0 ; for ( int i = 0 ; i <= MSB ; i ++ ) M += ( 1 << i ) ; return M ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( maxORminusXOR ( N ) ) ; } }
import java . util . * ; class GFG { static int No_of_Triangle ( int N , int K ) { if ( N < K ) return - 1 ; else { int Tri_up = 0 ; Tri_up = ( ( N - K + 1 ) * ( N - K + 2 ) ) / 2 ; int Tri_down = 0 ; Tri_down = ( ( N - 2 * K + 1 ) * ( N - 2 * K + 2 ) ) / 2 ; return Tri_up + Tri_down ; } } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; System . out . print ( No_of_Triangle ( N , K ) ) ; } }
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 " ) ; } } }
class GFG { static int check_length ( int n ) { int ans = 0 ; while ( n != 0 ) { n = n >> 1 ; ans ++ ; } return ans ; } static boolean check_ith_bit ( int n , int i ) { return ( n & ( 1 << ( i - 1 ) ) ) != 0 ? true : false ; } static int no_of_flips ( int n ) { int len = check_length ( n ) ; int ans = 0 ; int right = 1 ; int left = len ; while ( right < left ) { if ( check_ith_bit ( n , right ) != check_ith_bit ( n , left ) ) ans ++ ; left -- ; right ++ ; } return ans ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( no_of_flips ( n ) ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void findRank ( int [ ] A , int N ) { int [ ] rank = new int [ N ] ; Stack < Integer > s = new Stack < Integer > ( ) ; s . add ( A [ N - 1 ] ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( A [ i ] < s . peek ( ) ) { s . add ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } else { while ( ! s . isEmpty ( ) && A [ i ] >= s . peek ( ) ) { s . pop ( ) ; } s . add ( A [ i ] ) ; rank [ i ] = s . size ( ) - 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( rank [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 5 , 4 } ; int N = A . length ; findRank ( A , N ) ; } }
import java . util . ArrayDeque ; import java . util . Deque ; import java . util . Scanner ; import java . io . IOException ; public class GFG { public static String largestString ( int n , int k , String sc ) { char [ ] s = sc . toCharArray ( ) ; Deque < Character > deq = new ArrayDeque < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { while ( deq . size ( ) > 0 && deq . getLast ( ) < s [ i ] && k > 0 ) { deq . pollLast ( ) ; k -- ; } deq . add ( s [ i ] ) ; } String st = " " ; for ( char c : deq ) st = st + Character . toString ( c ) ; return st ; } public static void main ( String [ ] args ) throws IOException { int n = 4 ; int k = 2 ; String sc = " ritz " ; String result = largestString ( n , k , sc ) ; System . out . println ( result ) ; } }
import java . util . * ; class GFG { static int countSetBit ( int n ) { int i = 0 ; int ans = 0 ; while ( ( 1 << i ) <= n ) { boolean k = true ; int change = 1 << i ; for ( int j = 0 ; j <= n ; j ++ ) { ans += k == true ? 0 : 1 ; if ( change == 1 ) { k = ! k ; change = 1 << i ; } else { change -- ; } } i ++ ; } return ans ; } static int countSetBits ( int L , int R ) { return Math . abs ( countSetBit ( R ) - countSetBit ( L - 1 ) ) ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) + "NEW_LINE"); } }
class GFG { static int countSetBits ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { int n = i ; while ( n > 0 ) { count += ( n & 1 ) ; n = n >> 1 ; } } return count ; } public static void main ( String [ ] args ) { int L = 3 , R = 5 ; System . out . print ( " Total ▁ set ▁ Bit ▁ count ▁ is ▁ " + countSetBits ( L , R ) ) ; } }
class GFG { static void countElement ( int A [ ] , int N , int B [ ] , int M , int K ) { int cnt = 0 ; for ( int i = 0 ; i < M ; i ++ ) { int currentElement = B [ i ] ; for ( int j = 0 ; j < N ; j ++ ) { int diff = Math . abs ( currentElement - A [ j ] ) ; if ( diff <= K ) { cnt ++ ; break ; } } } System . out . print ( cnt ) ; } public static void main ( String [ ] args ) { int A [ ] = { 100 , 65 , 35 , 85 , 55 } ; int B [ ] = { 30 , 60 , 75 , 95 } ; int K = 5 ; int N = A . length ; int M = B . length ; countElement ( A , N , B , M , K ) ; } }
class GFG { static void createString ( int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ' a ' ) ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ' b ' ) ; } } public static void main ( String [ ] args ) { int N = 4 ; createString ( N ) ; } }
class GFG { static String largestString ( String num , int k ) { String ans = " " ; for ( char i : num . toCharArray ( ) ) { while ( ans . length ( ) > 0 && ans . charAt ( ans . length ( ) - 1 ) < i && k > 0 ) { ans = ans . substring ( 0 , ans . length ( ) - 1 ) ; k -- ; } ans += i ; } while ( ans . length ( ) > 0 && k -- > 0 ) { ans = ans . substring ( 0 , ans . length ( ) - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " zyxedcba " ; int k = 1 ; System . out . print ( largestString ( str , k ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int maxSubset ( int [ ] A , boolean flag ) { int n = A . length ; int sum = 0 ; if ( flag ) { for ( int i = 1 ; i < n ; i ++ ) A [ i ] = - A [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > 0 ) { sum += A [ i ] ; } } return sum ; } static int findBest ( int [ ] A ) { int x = maxSubset ( A , false ) ; int y = maxSubset ( A , true ) ; y = - y ; y += A [ 0 ] ; y = - y ; return Math . max ( x , y ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 10 , 4 , - 6 , 3 } ; System . out . print ( findBest ( A ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static Vector < String > solve ( int n , int arr [ ] ) { String s = " aaa " ; Vector < String > ans = new Vector < String > ( ) ; ans . add ( s ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . length ( ) - 1 >= arr [ i ] ) { char ch = s . charAt ( arr [ i ] ) ; if ( ch == ' b ' ) ch = ' a ' ; else ch = ' b ' ; char [ ] myNameChars = s . toCharArray ( ) ; myNameChars [ arr [ i ] ] = ch ; s = String . valueOf ( myNameChars ) ; } ans . add ( s ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 0 , 3 } ; int n = arr . length ; Vector < String > ans = solve ( n , arr ) ; for ( int i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . println ( ans . get ( i ) ) ; } } }
import java . util . * ; class GFG { static int countSwaps ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } max_frequency = Math . max ( max_frequency , mp . get ( arr [ i ] ) ) ; } return n - max_frequency ; } public static void main ( String [ ] args ) { int A [ ] = { 6 , 5 , 4 , 3 , 2 , 1 } ; System . out . print ( countSwaps ( A , 6 ) ) ; } }
import java . util . * ; class GFG { static int min_operation ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . abs ( a [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . print ( min_operation ( arr , n ) ) ; } }
class GFG { static void FindMatrix ( int n , int m , int x , int y ) { int a , b , i , j ; if ( n * m == 1 ) { if ( x > y ) { System . out . print ( y + "NEW_LINE"); } else { System . out . print ( x + "NEW_LINE"); } return ; } a = Math . min ( x , y ) ; b = Math . min ( 2 * x , y ) - a ; boolean flag = true ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( flag ) System . out . print ( a + " ▁ " ) ; else System . out . print ( b + " ▁ " ) ; flag = ! flag ; } if ( ( ( n % 2 != 0 && m % 2 == 0 ) || ( n % 2 == 0 && m % 2 == 0 ) ) ) flag = ! flag ; System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N , M , X , Y ; N = 3 ; M = 3 ; X = 5 ; Y = 3 ; FindMatrix ( N , M , X , Y ) ; } }
import java . util . * ; public class TSPGreedy { static void findMinRoute ( int [ ] [ ] tsp ) { int sum = 0 ; int counter = 0 ; int j = 0 , i = 0 ; int min = Integer . MAX_VALUE ; List < Integer > visitedRouteList = new ArrayList < > ( ) ; visitedRouteList . add ( 0 ) ; int [ ] route = new int [ tsp . length ] ; while ( i < tsp . length && j < tsp [ i ] . length ) { if ( counter >= tsp [ i ] . length - 1 ) { break ; } if ( j != i && ! ( visitedRouteList . contains ( j ) ) ) { if ( tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } j ++ ; if ( j == tsp [ i ] . length ) { sum += min ; min = Integer . MAX_VALUE ; visitedRouteList . add ( route [ counter ] - 1 ) ; j = 0 ; i = route [ counter ] - 1 ; counter ++ ; } } i = route [ counter - 1 ] - 1 ; for ( j = 0 ; j < tsp . length ; j ++ ) { if ( ( i != j ) && tsp [ i ] [ j ] < min ) { min = tsp [ i ] [ j ] ; route [ counter ] = j + 1 ; } } sum += min ; System . out . print ( " Minimum ▁ Cost ▁ is ▁ : ▁ " ) ; System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int [ ] [ ] tsp = { { - 1 , 10 , 15 , 20 } , { 10 , - 1 , 35 , 25 } , { 15 , 35 , - 1 , 30 } , { 20 , 25 , 30 , - 1 } } ; findMinRoute ( tsp ) ; } }
import java . util . * ; class GFG { static void findCount ( int [ ] Arr1 , int [ ] Arr2 ) { int MAX = Math . max ( Arrays . stream ( Arr1 ) . max ( ) . getAsInt ( ) , Arrays . stream ( Arr2 ) . max ( ) . getAsInt ( ) ) ; int [ ] Count = new int [ 2 * MAX + 1 ] ; int n = Arr1 . length , m = Arr2 . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int element = Arr1 [ i ] + Arr2 [ j ] ; Count [ element ] ++ ; } } for ( int i = 1 ; i <= 2 * MAX ; i ++ ) { if ( Count [ i ] > 0 ) { System . out . print ( i + " - > " + Count [ i ] + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 } ; int [ ] arr2 = { 1 , 2 , 1 } ; findCount ( arr1 , arr2 ) ; } }
class GFG { public static void count ( int n , int k ) { long count = ( long ) ( Math . pow ( 10 , k ) - Math . pow ( 10 , k - 1 ) ) ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 2 , k = 1 ; count ( n , k ) ; } }
class GFG { static void findAandB ( double N ) { if ( N == 1 ) { System . out . print ( " No " ) ; return ; } double a = N * N / ( N - 1 ) ; double b = a / N ; System . out . print ( " a ▁ = ▁ " + a + "NEW_LINE"); System . out . print ( " b ▁ = ▁ " + b + "NEW_LINE"); } public static void main ( String [ ] args ) { double N = 6 ; findAandB ( N ) ; } }
import java . util . * ; class GFG { static int maxSelections ( int A [ ] , int n , int k ) { Arrays . sort ( A ) ; int sum = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + A [ i ] ; if ( sum > k ) { break ; } count ++ ; } return count ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 , 2 , 9 , 4 } ; int k = 15 ; int n = A . length ; System . out . print ( maxSelections ( A , n , k ) ) ; } }
class GFG { static void computePair ( double K ) { if ( K == 1 ) { System . out . print ( " No " ) ; return ; } else { System . out . print ( K * K / ( K - 1 ) + " ▁ " ) ; System . out . print ( K / ( K - 1 ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { double K = 6 ; computePair ( K ) ; } }
class GFG { static int func ( int N , int P ) { int sumUptoN = ( N * ( N + 1 ) / 2 ) ; int sumOfMultiplesOfP ; if ( N < P ) { return sumUptoN ; } else if ( ( N / P ) == 1 ) { return sumUptoN - P + 1 ; } sumOfMultiplesOfP = ( ( N / P ) * ( 2 * P + ( N / P - 1 ) * P ) ) / 2 ; return ( sumUptoN + func ( N / P , P ) - sumOfMultiplesOfP ) ; } public static void main ( String [ ] args ) { int N = 10 , P = 5 ; System . out . println ( func ( N , P ) ) ; } }
class GFG { static void number ( int num ) { int a [ ] = new int [ 20 ] ; int r , i = 0 , j ; while ( num > 0 ) { r = num % 10 ; if ( 9 - r > r ) a [ i ] = r ; else a [ i ] = 9 - r ; i ++ ; num = num / 10 ; } if ( a [ i - 1 ] == 0 ) { System . out . print ( "9" ) ; i -- ; } for ( j = i - 1 ; j >= 0 ; j -- ) System . out . print ( a [ j ] ) ; } public static void main ( String [ ] args ) { int num = 4545 ; number ( num ) ; } }
class GFG { public static void findShifts ( int [ ] A , int N ) { int [ ] shift = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == A [ i ] - 1 ) shift [ i ] = 0 ; else shift [ i ] = ( A [ i ] - 1 - i + N ) % N ; } for ( int i = 0 ; i < N ; i ++ ) System . out . print ( shift [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 2 , 5 } ; int N = arr . length ; findShifts ( arr , N ) ; } }
class GFG { static boolean isDivisible ( int N ) { return ( N - 1 ) % 3 != 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( isDivisible ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void maxLengthSubArray ( int A [ ] , int N ) { int forward [ ] = new int [ N ] ; int backward [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( i == 0 A [ i ] != A [ i - 1 ] ) { forward [ i ] = 1 ; } else forward [ i ] = forward [ i - 1 ] + 1 ; } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 A [ i ] != A [ i + 1 ] ) { backward [ i ] = 1 ; } else backward [ i ] = backward [ i + 1 ] + 1 ; } int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( A [ i ] != A [ i + 1 ] ) ans = Math . max ( ans , Math . min ( forward [ i ] , backward [ i + 1 ] ) * 2 ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 4 , 6 , 6 , 6 , 9 } ; int N = arr . length ; maxLengthSubArray ( arr , N ) ; } }
class GFG { static int countDigitSum ( int N , int K ) { int l = ( int ) Math . pow ( 10 , N - 1 ) , r = ( int ) Math . pow ( 10 , N ) - 1 ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; int digits [ ] = new int [ N ] ; for ( int j = N - 1 ; j >= 0 ; j -- ) { digits [ j ] = num % 10 ; num /= 10 ; } int sum = 0 , flag = 0 ; for ( int j = 0 ; j < K ; j ++ ) sum += digits [ j ] ; for ( int j = 1 ; j < N - K + 1 ; j ++ ) { int curr_sum = 0 ; for ( int m = j ; m < j + K ; m ++ ) { curr_sum += digits [ m ] ; } if ( sum != curr_sum ) { flag = 1 ; break ; } } if ( flag == 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 2 , K = 1 ; System . out . print ( countDigitSum ( N , K ) ) ; } }
class GFG { static boolean possible ( int N , int a , int b , int n ) { int sum_of_angle = 180 * ( N - 2 ) ; int Total_angle = ( N * ( ( 2 * a ) + ( N - 1 ) * b ) ) / 2 ; if ( sum_of_angle != Total_angle ) return false ; else return true ; } static int nth_angle ( int N , int a , int b , int n ) { int nth = 0 ; nth = a + ( n - 1 ) * b ; return nth ; } public static void main ( String [ ] args ) { int N = 3 , a = 30 , b = 30 , n = 3 ; if ( possible ( N , a , b , n ) ) System . out . print ( nth_angle ( N , a , b , n ) ) ; else System . out . print ( " Not ▁ Possible " ) ; } }
import java . util . * ; class GFG { static boolean is_linear ( String s ) { int tmp = 0 ; char first = s . charAt ( 0 ) ; for ( int pos = 0 ; pos < s . length ( ) ; pos += tmp ) { if ( s . charAt ( pos ) != first ) { return false ; } tmp ++ ; } return true ; } public static void main ( String [ ] args ) { String str = " aapaxyayziabcde " ; if ( is_linear ( str ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static void DFS ( int U , int [ ] vis , ArrayList < ArrayList < Integer > > adj ) { vis [ U ] = 1 ; for ( Integer V : adj . get ( U ) ) { if ( vis [ V ] == 0 ) DFS ( V , vis , adj ) ; } } static void minCost ( int N , int M , int vCost , int eCost , int sorc [ ] , ArrayList < Integer > colored , int destination [ ] ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) adj . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { adj . get ( sorc [ i ] ) . add ( destination [ i ] ) ; adj . get ( destination [ i ] ) . add ( sorc [ i ] ) ; } int [ ] vis = new int [ N + 1 ] ; for ( int i = 0 ; i < colored . size ( ) ; i ++ ) { DFS ( colored . get ( i ) , vis , adj ) ; } int X = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( vis [ i ] == 0 ) { X ++ ; DFS ( i , vis , adj ) ; } } int mincost = X * Math . min ( vCost , eCost ) ; System . out . println ( mincost ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 1 ; int sorc [ ] = { 1 } ; int destination [ ] = { 2 } ; int vCost = 3 , eCost = 2 ; ArrayList < Integer > colored = new ArrayList < > ( ) ; colored . add ( 1 ) ; minCost ( N , M , vCost , eCost , sorc , colored , destination ) ; } }
import java . util . * ; class GFG { static int min_cost ( int n , int x , int p , int q ) { if ( x == 1 ) { System . out . println ( ( n - 1 ) * p ) ; return 0 ; } int ans = ( n - 1 ) * p ; int pre = 0 ; while ( n > 1 ) { int tmp = n / x ; if ( tmp < 0 ) break ; pre += ( n - tmp * x ) * p ; n /= x ; pre += q ; ans = Math . min ( ans , pre + ( n - 1 ) * p ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 2 , p = 2 , q = 3 ; System . out . println ( min_cost ( n , x , p , q ) ) ; } }
import java . util . * ; class GFG { static int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= ( N ) ; b ++ ) for ( int c = 0 ; c <= C && b * 5 + c * 10 <= ( N ) ; c ++ ) for ( int d = 0 ; d <= D && b * 5 + c * 10 + d * 20 <= ( N ) ; d ++ ) if ( ( b * 5 ) + ( c * 10 ) + ( d * 20 ) >= ( N - A ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; System . out . print ( calculateWays ( arr1 , arr2 , N ) ) ; } }
import java . util . * ; class GFG { static int [ ] ways = new int [ 1010 ] ; static int calculateWays ( int arr1 [ ] , int arr2 [ ] , int N ) { int A = arr2 [ 0 ] , B = arr2 [ 1 ] ; int C = arr2 [ 2 ] , D = arr2 [ 3 ] ; int ans = 0 ; for ( int b = 0 ; b <= B && b * 5 <= N ; b ++ ) { for ( int a = 0 ; a <= A && a * 1 + b * 5 <= N ; a ++ ) { ways [ a + b * 5 ] ++ ; } } for ( int c = 0 ; c <= C && c * 10 <= ( N ) ; c ++ ) { for ( int d = 0 ; d <= D && c * 10 + d * 20 <= ( N ) ; d ++ ) { ans += ways [ N - c * 10 - d * 20 ] ; } } return ans ; } public static void main ( String [ ] args ) { int N = 123 ; int arr1 [ ] = { 1 , 5 , 10 , 20 } ; int arr2 [ ] = { 6 , 4 , 3 , 5 } ; System . out . print ( calculateWays ( arr1 , arr2 , N ) ) ; } }
class GFG { static void minNum ( int n ) { if ( n < 3 ) System . out . println ( - 1 ) ; else System . out . println ( 210 * ( ( int ) ( Math . pow ( 10 , n - 1 ) / 210 ) + 1 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; minNum ( n ) ; } }
class GFG { public static void countMissingNum ( int [ ] a , int N ) { int count = a [ N - 1 ] - a [ 0 ] + 1 - N ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 , 40 } ; int N = arr . length ; countMissingNum ( arr , N ) ; } }
public class Main { public static void constructmatrix ( int N ) { boolean check = true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { System . out . print ( "1 ▁ " ) ; } else if ( check ) { System . out . print ( "2 ▁ " ) ; check = false ; } else { System . out . print ( " - 2 ▁ " ) ; check = true ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 5 ; constructmatrix ( 5 ) ; } }
class GFG { static int maxXORSubsequence ( int arr [ ] , int n ) { int i , odd = 0 , even = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } int maxlen ; if ( odd == n ) { if ( odd % 2 == 0 ) maxlen = n - 1 ; else maxlen = n ; } else if ( even == n ) { maxlen = 0 ; } else { if ( odd % 2 == 0 ) maxlen = even + odd - 1 ; else maxlen = even + odd ; } return maxlen ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; System . out . print ( maxXORSubsequence ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int good_pair ( String str [ ] , int N ) { int countStr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { String res = str [ i ] + str [ j ] ; int vowel [ ] = new int [ 5 ] ; for ( int k = 0 ; k < res . length ( ) ; k ++ ) { if ( res . charAt ( k ) == ' a ' ) vowel [ 0 ] = 1 ; else if ( res . charAt ( k ) == ' e ' ) vowel [ 1 ] = 1 ; else if ( res . charAt ( k ) == ' i ' ) vowel [ 2 ] = 1 ; else if ( res . charAt ( k ) == ' o ' ) vowel [ 3 ] = 1 ; else if ( res . charAt ( k ) == ' u ' ) vowel [ 4 ] = 1 ; } int temp = 0 ; for ( int ind = 0 ; ind < 5 ; ind ++ ) { if ( vowel [ ind ] == 1 ) temp ++ ; } if ( temp == 5 ) countStr ++ ; } } return countStr ; } public static void main ( String [ ] args ) { String arr [ ] = { " aaweiolkju " , " oxdfgujkmi " } ; int N = arr . length ; System . out . print ( good_pair ( arr , N ) ) ; } }
class GFG { static void sieveOfEratosthenes ( int N , int s [ ] ) { boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int generatePrimeFactors ( int N ) { int [ ] s = new int [ N + 1 ] ; int sum = 0 ; sieveOfEratosthenes ( N , s ) ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } sum = sum + cnt ; curr = s [ N ] ; cnt = 1 ; } return sum ; } static void findSum ( int N ) { int sum = 0 ; for ( int i = 2 ; i <= N ; i ++ ) { sum += generatePrimeFactors ( i ) ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; findSum ( N ) ; } }
import java . util . * ; class GFG { static int LCM ( int a , int b ) { int lcm = a > b ? a : b ; while ( true ) { if ( lcm % a == 0 && lcm % b == 0 ) break ; else lcm ++ ; } return lcm ; } static void findMinLCM ( int arr [ ] , int n ) { int minLCM = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = LCM ( arr [ i ] , arr [ i + 1 ] ) ; if ( val < minLCM ) { minLCM = val ; } } System . out . print ( minLCM + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 12 , 16 , 20 , 24 } ; int n = arr . length ; findMinLCM ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int find_max ( int n , int k ) { int [ ] X = new int [ 32 ] ; int cnt = 0 ; for ( int i = 31 ; i >= 0 && cnt != k ; i -- ) { if ( ( n & ( 1 << i ) ) != 0 ) { X [ i ] = 1 ; cnt ++ ; } } String s = " " ; for ( int i = 31 ; i >= 0 ; i -- ) s += X [ i ] == 0 ? '0' : '1' ; return Integer . parseInt ( s , 2 ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 2 ; System . out . println ( find_max ( n , k ) ) ; } }
class GFG { public static void convert ( String s ) { int num = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + ( ( int ) s . charAt ( i ) - 48 ) ; System . out . print ( num ) ; } public static void main ( String [ ] args ) { String s = "123" ; convert ( s ) ; } }
import java . util . * ; class GFG { static final int hell = 1000000007 ; static final int N = 100005 ; static int c_0 , c_1 , c_2 ; static void count_k ( int [ ] arr , Stack < Integer > ones , Stack < Integer > twos , Stack < Integer > zeros ) { for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] % 3 == 0 ) { c_0 ++ ; zeros . add ( arr [ i ] ) ; } else if ( arr [ i ] % 3 == 1 ) { c_1 ++ ; ones . add ( arr [ i ] ) ; } else { c_2 ++ ; twos . add ( arr [ i ] ) ; } } return ; } static void printArrangement ( int [ ] arr , Stack < Integer > ones , Stack < Integer > twos , Stack < Integer > zeros ) { if ( ( c_0 == 0 && c_1 != 0 && c_2 != 0 ) c_0 > c_1 + c_2 + 1 ) { System . out . print ( " - 1" ) ; return ; } if ( c_0 == 0 ) { for ( int i = 0 ; i < arr . length ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } return ; } int i , j ; int [ ] ans = new int [ N ] ; Arrays . fill ( ans , - 1 ) ; for ( i = 1 , j = 0 ; j < c_1 ; i += 2 , j ++ ) { ans [ i ] = ones . peek ( ) ; ones . pop ( ) ; } ans [ i - 1 ] = zeros . peek ( ) ; zeros . pop ( ) ; c_0 -- ; for ( j = 0 ; j < c_2 ; j ++ , i += 2 ) { ans [ i ] = twos . peek ( ) ; twos . pop ( ) ; } for ( int k = 0 ; c_0 > 0 ; k += 2 ) { if ( ans [ k ] == - 1 ) { ans [ k ] = zeros . peek ( ) ; c_0 -- ; } } for ( int i1 = 0 ; i1 < N ; i1 ++ ) { if ( ans [ i1 ] != - 1 ) System . out . print ( ans [ i1 ] + " ▁ " ) ; } return ; } static void solve ( int n , int [ ] arr ) { Stack < Integer > ones = new Stack < Integer > ( ) ; Stack < Integer > zeros = new Stack < Integer > ( ) ; Stack < Integer > twos = new Stack < Integer > ( ) ; c_0 = 0 ; c_1 = 0 ; c_2 = 0 ; count_k ( arr , ones , twos , zeros ) ; printArrangement ( arr , ones , twos , zeros ) ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; solve ( n , arr ) ; } }
import java . util . * ; class GFG { public static void countUniqueDigits ( int N ) { int res = 0 ; int cnt [ ] = { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ; while ( N > 0 ) { int rem = N % 10 ; cnt [ rem ] ++ ; N = N / 10 ; } for ( int i = 0 ; i < cnt . length ; i ++ ) { if ( cnt [ i ] == 1 ) { res ++ ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int N = 2234262 ; countUniqueDigits ( N ) ; } }
import java . util . * ; class GFG { static boolean checkUniqueFrequency ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } HashSet < Integer > uniqueFreq = new HashSet < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : freq . entrySet ( ) ) { if ( uniqueFreq . contains ( i . getValue ( ) ) ) return false ; else uniqueFreq . add ( i . getValue ( ) ) ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 5 , 5 } ; int n = arr . length ; boolean res = checkUniqueFrequency ( arr , n ) ; if ( res ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
import java . util . * ; import java . lang . Math ; class GFG { static double power ( int N , int P ) { return Math . pow ( N , P ) ; } public static int log2 ( int N ) { int result = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) ; return result ; } static double specialSum ( int n ) { double sum = n * ( n + 1 ) / 2 ; int a = log2 ( n ) ; sum = sum + power ( 2 , a + 1 ) - 1 ; return sum ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( specialSum ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void Permute ( Vector < Integer > arr , int n ) { int odd = 0 , four = 0 ; int non_four = 0 ; Vector < Integer > ODD = new Vector < Integer > ( ) ; Vector < Integer > FOUR = new Vector < Integer > ( n ) ; Vector < Integer > NON_FOUR = new Vector < Integer > ( n ) ; for ( int x : arr ) { if ( x % 2 != 0 ) { odd ++ ; ODD . add ( x ) ; } else if ( x % 4 == 0 ) { four ++ ; FOUR . add ( x ) ; } else { non_four ++ ; NON_FOUR . add ( x ) ; } } if ( non_four == 0 && four >= odd - 1 ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { System . out . print ( ODD . get ( i ) + " ▁ " ) ; if ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; } while ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } else if ( non_four > 0 && four >= odd ) { int x = ODD . size ( ) ; int y = FOUR . size ( ) ; int i ; for ( i = 0 ; i < x ; i ++ ) { System . out . print ( ODD . get ( i ) + " ▁ " ) ; if ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; } while ( i < y ) System . out . print ( FOUR . get ( i ) + " ▁ " ) ; for ( int j = 0 ; j < ( int ) NON_FOUR . size ( ) ; j ++ ) System . out . print ( NON_FOUR . get ( j ) + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 2 ) ; arr . add ( 7 ) ; arr . add ( 1 ) ; arr . add ( 8 ) ; arr . add ( 2 ) ; arr . add ( 8 ) ; Permute ( arr , arr . size ( ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void findArray ( int sum , int xorr ) { if ( xorr > sum sum % 2 != xorr % 2 ) { System . out . println ( " No ▁ Array ▁ Possible " ) ; return ; } if ( sum == xorr ) { if ( sum == 0 ) System . out . println ( " Array ▁ is ▁ empty ▁ " + " with ▁ size ▁ 0" ) ; else System . out . println ( " Array ▁ size ▁ is ▁ " + 1 ) ; System . out . println ( " Array ▁ is ▁ " + sum ) ; return ; } int mid = ( sum - xorr ) / 2 ; if ( xorr == 1 && mid == 1 ) { System . out . println ( " Array ▁ size ▁ is ▁ " + 3 ) ; System . out . println ( " Array ▁ is ▁ " + xorr + " ▁ " + mid + " ▁ " + mid ) ; } else { System . out . println ( " Array ▁ size ▁ is ▁ " + 2 ) ; System . out . println ( " Array ▁ is ▁ " + ( xorr + mid ) + " ▁ " + mid ) ; } } public static void main ( String [ ] args ) { int sum = 4 , xorr = 2 ; findArray ( sum , xorr ) ; } }
import java . util . * ; class GFG { static String Kperiodicinteger ( char [ ] X , int N , int K ) { String temp = String . valueOf ( X ) ; for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } if ( String . valueOf ( X ) . compareTo ( temp ) >= 0 ) { return String . valueOf ( X ) ; } int POS = 0 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { if ( X [ i ] != '9' ) { X [ i ] ++ ; POS = i ; break ; } } for ( int i = POS + 1 ; i < K ; i ++ ) { X [ i ] = '0' ; } for ( int i = 0 ; i < K ; i ++ ) { int j = i ; while ( j < N ) { X [ j ] = X [ i ] ; j += K ; } } return String . valueOf ( X ) ; } public static void main ( String [ ] args ) { int N = 4 , K = 2 ; String X = "1215" ; System . out . print ( Kperiodicinteger ( X . toCharArray ( ) , N , K ) ) ; } }
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static String helper ( int d , int s ) { StringBuilder ans = new StringBuilder ( ) ; for ( int i = 0 ; i < d ; i ++ ) { ans . append ( "0" ) ; } for ( int i = d - 1 ; i >= 0 ; i -- ) { if ( s >= 9 ) { ans . setCharAt ( i , '9' ) ; s -= 9 ; } else { char c = ( char ) ( s + ( int ) '0' ) ; ans . setCharAt ( i , c ) ; s = 0 ; } } return ans . toString ( ) ; } static String findMin ( int x , int Y ) { String y = Integer . toString ( Y ) ; int n = y . length ( ) ; ArrayList p = new ArrayList ( ) ; for ( int i = 0 ; i < n ; i ++ ) { p . add ( 0 ) ; } for ( int i = 0 ; i < n ; i ++ ) { p . add ( i , ( int ) ( ( int ) y . charAt ( i ) - ( int ) '0' ) ) ; if ( i > 0 ) { p . add ( i , ( int ) p . get ( i ) + ( int ) p . get ( i - 1 ) ) ; } } for ( int i = n - 1 , k = 0 ; ; i -- , k ++ ) { int d = 0 ; if ( i >= 0 ) { d = ( int ) y . charAt ( i ) - ( int ) '0' ; } for ( int j = d + 1 ; j <= 9 ; j ++ ) { int r = j ; if ( i > 0 ) { r += ( int ) p . get ( i - 1 ) ; } if ( x - r >= 0 && x - r <= 9 * k ) { String suf = helper ( k , x - r ) ; String pre = " " ; if ( i > 0 ) pre = y . substring ( 0 , i ) ; char cur = ( char ) ( j + ( int ) '0' ) ; pre += cur ; return pre + suf ; } } } } public static void main ( String [ ] arg ) { int x = 18 ; int y = 99 ; System . out . print ( findMin ( x , y ) ) ; } }
import java . util . * ; class GFG { public static void largestNumber ( int n , int X , int Y ) { int maxm = Math . max ( X , Y ) ; Y = X + Y - maxm ; X = maxm ; int Xs = 0 ; int Ys = 0 ; while ( n > 0 ) { if ( n % Y == 0 ) { Xs += n ; n = 0 ; } else { n -= X ; Ys += X ; } } if ( n == 0 ) { while ( Xs -- > 0 ) System . out . print ( X ) ; while ( Ys -- > 0 ) System . out . print ( Y ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 19 , X = 7 , Y = 5 ; largestNumber ( n , X , Y ) ; } }
class GFG { static int check ( int unit_digit , int X ) { int times , digit ; for ( times = 1 ; times <= 10 ; times ++ ) { digit = ( X * times ) % 10 ; if ( digit == unit_digit ) return times ; } return - 1 ; } static int getNum ( int N , int X ) { int unit_digit ; unit_digit = N % 10 ; int times = check ( unit_digit , X ) ; if ( times == - 1 ) return times ; else { if ( N >= ( times * X ) ) return times ; else return - 1 ; } } public static void main ( String [ ] args ) { int N = 58 , X = 7 ; System . out . println ( getNum ( N , X ) ) ; } }
import java . util . * ; class GFG { static final int N = 5 ; static int after_rev ( Vector < Integer > v ) { int mini = 0 , count = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { count += v . get ( i ) ; if ( count > 0 ) count = 0 ; if ( mini > count ) mini = count ; } return mini ; } static void print ( int arr [ ] ) { int sum = 0 ; for ( int i = 0 ; i < N ; i += 2 ) sum += arr [ i ] ; Vector < Integer > v1 , v2 ; v1 = new Vector < Integer > ( ) ; v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i + 1 < N ; i += 2 ) v1 . add ( arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 1 ; i + 1 < N ; i += 2 ) v2 . add ( arr [ i ] - arr [ i + 1 ] ) ; int change = Math . min ( after_rev ( v1 ) , after_rev ( v2 ) ) ; if ( change < 0 ) sum += change ; System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 4 , 3 , 0 } ; print ( arr ) ; } }
import java . util . * ; class GFG { static int moduloEquality ( int A [ ] , int B [ ] , int n , int m ) { HashMap < Integer , Integer > mapA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > mapB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mapA . containsKey ( A [ i ] ) ) { mapA . put ( A [ i ] , mapA . get ( A [ i ] ) + 1 ) ; } else { mapA . put ( A [ i ] , 1 ) ; } if ( mapB . containsKey ( B [ i ] ) ) { mapB . put ( B [ i ] , mapB . get ( B [ i ] ) + 1 ) ; } else { mapB . put ( B [ i ] , 1 ) ; } } HashSet < Integer > possibleValues = new HashSet < Integer > ( ) ; int FirstElement = B [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = A [ i ] ; possibleValues . add ( cur > FirstElement ? m - cur + FirstElement : FirstElement - cur ) ; } int ans = Integer . MAX_VALUE ; for ( int it : possibleValues ) { boolean possible = true ; for ( Map . Entry < Integer , Integer > it2 : mapA . entrySet ( ) ) { if ( it2 . getValue ( ) != mapB . get ( ( it2 . getKey ( ) + it ) % m ) ) { possible = false ; break ; } } if ( possible ) { ans = Math . min ( ans , it ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int m = 3 ; int A [ ] = { 0 , 0 , 2 , 1 } ; int B [ ] = { 2 , 0 , 1 , 1 } ; System . out . print ( moduloEquality ( A , B , n , m ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int countCyclicShifts ( String S , int n ) { int [ ] aux = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( S . charAt ( i ) == ' ( ' ) aux [ i ] = 1 ; else aux [ i ] = - 1 ; } int mn = aux [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { aux [ i ] += aux [ i - 1 ] ; mn = Math . min ( mn , aux [ i ] ) ; } if ( aux [ n - 1 ] != 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( aux [ i ] == mn ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String S = " ) ( ) ( " ; int N = S . length ( ) ; System . out . print ( countCyclicShifts ( S , N ) ) ; } }
class GFG { static int maxProfit ( int [ ] prices , int n ) { int profit = 0 , currentDay = n - 1 ; while ( currentDay > 0 ) { int day = currentDay - 1 ; while ( day >= 0 && ( prices [ currentDay ] > prices [ day ] ) ) { profit += ( prices [ currentDay ] - prices [ day ] ) ; day -- ; } currentDay = day ; } return profit ; } public static void main ( String [ ] args ) { int prices [ ] = { 2 , 3 , 5 } ; int N = prices . length ; System . out . print ( maxProfit ( prices , N ) ) ; } }
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 ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
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 ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
class GFG { static void min_remove ( String str ) { int N = str . length ( ) ; int [ ] prefix_a = new int [ N + 1 ] ; int [ ] prefix_b = new int [ N + 1 ] ; int [ ] prefix_c = new int [ N + 1 ] ; prefix_a [ 0 ] = 0 ; prefix_b [ 0 ] = 0 ; prefix_c [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix_a [ i ] = prefix_a [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' a ' ) ? 1 : 0 ) ; prefix_b [ i ] = prefix_b [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' b ' ) ? 1 : 0 ) ; prefix_c [ i ] = prefix_c [ i - 1 ] + ( int ) ( ( str . charAt ( i - 1 ) == ' c ' ) ? 1 : 0 ) ; } int maxi = Integer . MIN_VALUE ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { maxi = Math . max ( maxi , ( prefix_a [ i ] + ( prefix_b [ j ] - prefix_b [ i ] ) + ( prefix_c [ N ] - prefix_c [ j ] ) ) ) ; } } System . out . println ( ( N - maxi ) ) ; } public static void main ( String [ ] args ) { String str = " aaaabaaxccac " ; min_remove ( str ) ; } }
import java . util . * ; class GFG { static int maximum_partition ( String str ) { int i = 0 , j = 0 , k = 0 ; int c = 0 , r = 0 ; HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( ! m . containsKey ( str . charAt ( i ) ) ) { m . put ( str . charAt ( i ) , i ) ; } } i = 0 ; k = m . get ( str . charAt ( i ) ) ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i <= k ) { c = c + 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } else { r = r + 1 ; c = 1 ; k = Math . max ( k , m . get ( str . charAt ( i ) ) ) ; } } if ( c != 0 ) { r = r + 1 ; } return r ; } public static void main ( String [ ] args ) { String str = " ababcbacadefegdehijhklij " ; System . out . print ( maximum_partition ( str ) ) ; } }
import java . util . * ; class GFG { static int findAns ( int [ ] [ ] mat , int x , int y , int n , int m ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) { ans += Math . abs ( x - i ) + Math . abs ( y - j ) ; } } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 1 } } ; int x = 0 , y = 2 ; System . out . print ( findAns ( mat , x , y , mat . length , mat [ 0 ] . length ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void countReversebitonic ( int arr [ ] , int n ) { int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int temp = arr [ i ] , f = 0 ; if ( j == i ) { c ++ ; continue ; } int k = i + 1 ; while ( temp > arr [ k ] && k <= j ) { temp = arr [ k ] ; k ++ ; } if ( k > j ) { c ++ ; f = 2 ; } while ( k <= j && temp < arr [ k ] && f != 2 ) { temp = arr [ k ] ; k ++ ; } if ( k > j && f != 2 ) { c ++ ; f = 0 ; } } } System . out . print ( c + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 4 } ; int N = arr . length ; countReversebitonic ( arr , N ) ; } }
import java . util . * ; class GFG { static int longestSequence ( int n , int arr [ ] ) { int l = 0 , r = n - 1 ; int prev = Integer . MIN_VALUE ; int ans = 0 ; while ( l <= r ) { if ( arr [ l ] > prev && arr [ r ] > prev ) { if ( arr [ l ] < arr [ r ] ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } } else if ( arr [ l ] > prev ) { ans += 1 ; prev = arr [ l ] ; l += 1 ; } else if ( arr [ r ] > prev ) { ans += 1 ; prev = arr [ r ] ; r -= 1 ; } else { break ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 1 , 4 , 2 } ; int n = arr . length ; System . out . print ( longestSequence ( n , arr ) ) ; } }
import java . util . * ; class GFG { static int solve ( int n , int m , int x , int y , int d ) { int top = Math . min ( d , x - 1 ) ; int down = Math . min ( d , n - x ) ; int left = Math . min ( d , y - 1 ) ; int right = Math . min ( d , m - y ) ; int quad1 = top * left ; int quad2 = left * down ; int quad3 = down * right ; int quad4 = right * top ; int totalsq = quad1 + quad2 + quad3 + quad4 ; int singleBlocks = top + down + left + right + 1 ; return totalsq + singleBlocks ; } public static void main ( String [ ] args ) { int n = 10 , m = 10 ; int x = 7 , y = 8 ; int d = 4 ; d -- ; System . out . println ( solve ( n , m , x , y , d ) ) ; } }
import java . util . * ; class GFG { static void getSubsequence ( int [ ] arr , int n ) { Vector < Integer > req = new Vector < Integer > ( ) ; req . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] && arr [ i ] > arr [ i - 1 ] ) req . add ( arr [ i ] ) ; else if ( arr [ i ] < arr [ i + 1 ] && arr [ i ] < arr [ i - 1 ] ) req . add ( arr [ i ] ) ; } req . add ( arr [ n - 1 ] ) ; System . out . print ( req . size ( ) + "NEW_LINE"); for ( int x : req ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 6 , 7 , 4 } ; int n = arr . length ; getSubsequence ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static final int M = 3 ; static final int N = 3 ; static int minchanges ( int [ ] [ ] mat ) { int count = 0 ; int left = 0 , right = N + M - 2 ; while ( left < right ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; int totalsize = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + j == left ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } else if ( i + j == right ) { mp . put ( mat [ i ] [ j ] , mp . getOrDefault ( mat [ i ] [ j ] , 0 ) + 1 ) ; totalsize ++ ; } } } int changes = 0 ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) changes = Math . max ( changes , itr . getValue ( ) ) ; count += totalsize - changes ; left ++ ; right -- ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 4 , 1 } , { 2 , 5 , 3 } , { 1 , 3 , 1 } } ; System . out . println ( minchanges ( mat ) ) ; } }
import java . util . * ; class GFG { static int CountLongestSubarrays ( int arr [ ] , int n , int k ) { int i , s = 0 ; for ( i = 0 ; i < n ; ++ i ) { s += arr [ i ] ; } if ( ( s % k ) != 0 ) { return 1 ; } else { int ini = 0 ; while ( ini < n && arr [ ini ] % k == 0 ) { ++ ini ; } int fin = n - 1 ; while ( fin >= 0 && arr [ fin ] % k == 0 ) { -- fin ; } int len , sum = 0 , count = 0 ; if ( ini == n ) { return - 1 ; } else { len = Math . max ( n - 1 - ini , fin ) ; } for ( i = 0 ; i < len ; i ++ ) { sum += arr [ i ] ; } if ( sum % k != 0 ) { count ++ ; } for ( i = len ; i < n ; i ++ ) { sum = sum + arr [ i ] ; sum = sum - arr [ i - len ] ; if ( sum % k != 0 ) { count ++ ; } } return count ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 2 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( CountLongestSubarrays ( arr , n , k ) ) ; } }
class GFG { static int MAXN = 1000001 ; static int freq [ ] = new int [ MAXN ] ; static int mul [ ] = new int [ MAXN ] ; static int cnt [ ] = new int [ MAXN ] ; static int nC3 ( int n ) { if ( n < 3 ) return 0 ; return ( n * ( n - 1 ) * ( n - 2 ) ) / 6 ; } static void count_triplet ( int [ ] arr , int N , int K ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( i = 1 ; i <= 1000000 ; i ++ ) { for ( j = i ; j <= 1000000 ; j += i ) { mul [ i ] += freq [ j ] ; } cnt [ i ] = nC3 ( mul [ i ] ) ; } for ( i = 1000000 ; i >= 1 ; i -- ) { for ( j = 2 * i ; j <= 1000000 ; j += i ) { cnt [ i ] -= cnt [ j ] ; } } System . out . print ( " Number ▁ of ▁ triplets ▁ " + " with ▁ GCD ▁ " + K ) ; System . out . print ( " ▁ are ▁ " + cnt [ K ] ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 7 , 12 , 6 , 15 , 9 } ; int N = 6 , K = 3 ; count_triplet ( arr , N , K ) ; } }
class GFG { static boolean isSubseqPossible ( int [ ] arr , int N , int K ) { int i ; int odd = 0 , even = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; else even ++ ; } if ( odd == 0 || ( even == 0 && K % 2 == 0 ) ) return false ; return true ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 3 , 5 , 7 , 4 } ; int N = arr . length ; int K = 3 ; System . out . print ( isSubseqPossible ( arr , N , K ) ? " Yes " : " No " ) ; } }
import java . util . * ; class GFG { static final int INF = 10000009 ; static void findMinHike ( Vector < Integer > arr , int n ) { arr . add ( 0 , INF ) ; arr . add ( INF ) ; int [ ] hike = new int [ n + 2 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) <= arr . get ( i + 1 ) ) { hike [ i ] = hike [ i - 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) >= arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = hike [ i + 1 ] + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( arr . get ( i - 1 ) < arr . get ( i ) && arr . get ( i ) > arr . get ( i + 1 ) ) { hike [ i ] = Math . max ( hike [ i - 1 ] , hike [ i + 1 ] ) + 1 ; } } for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( hike [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < > ( ) ; arr . add ( 5 ) ; arr . add ( 3 ) ; arr . add ( 4 ) ; arr . add ( 2 ) ; arr . add ( 1 ) ; arr . add ( 6 ) ; findMinHike ( arr , arr . size ( ) ) ; } }
import java . util . * ; class GFG { static void minimumCost ( int arr [ ] , int cost [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ 3 ] ; dp [ 0 ] [ 0 ] = 0 ; dp [ 0 ] [ 1 ] = cost [ 0 ] ; dp [ 0 ] [ 2 ] = cost [ 0 ] * 2 ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { int minimum = ( int ) 1e6 ; if ( j + arr [ i ] != arr [ i - 1 ] ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 0 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 1 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 1 ] ) ; if ( j + arr [ i ] != arr [ i - 1 ] + 2 ) minimum = Math . min ( minimum , dp [ i - 1 ] [ 2 ] ) ; dp [ i ] [ j ] = j * cost [ i ] + minimum ; } } int ans = ( int ) 1e6 ; for ( int i = 0 ; i < 3 ; i ++ ) ans = Math . min ( ans , dp [ N - 1 ] [ i ] ) ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 4 } ; int cost [ ] = { 3 , 2 , 5 , 4 , 2 , 1 } ; int N = arr . length ; minimumCost ( arr , cost , N ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int maxProfit ( int profit [ ] , int weight [ ] , int n , int max_W , int max_E ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= max_W ; j ++ ) { for ( int k = 1 ; k <= max_E ; k ++ ) { if ( j >= weight [ i - 1 ] ) { dp [ i ] [ j ] [ k ] = Math . max ( dp [ i - 1 ] [ j ] [ k ] , dp [ i - 1 ] [ j - weight [ i - 1 ] ] [ k - 1 ] + profit [ i - 1 ] ) ; } else { dp [ i ] [ j ] [ k ] = dp [ i - 1 ] [ j ] [ k ] ; } } } } return dp [ n ] [ max_W ] [ max_E ] ; } public static void main ( String [ ] args ) { int n = 5 ; int profit [ ] = { 2 , 7 , 1 , 5 , 3 } ; int weight [ ] = { 2 , 5 , 2 , 3 , 4 } ; int max_weight = 8 ; int max_element = 2 ; System . out . println ( maxProfit ( profit , weight , n , max_weight , max_element ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . PriorityQueue ; class GFG { static class Pair implements Comparable < Pair > { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } @ Override public int compareTo ( GFG . Pair o ) { if ( this . first == o . first ) { return this . second - o . second ; } return this . first - o . first ; } } static final int MAXX = 10000 , INF = ( int ) 1e9 ; @ SuppressWarnings ( " unchecked " ) static ArrayList < Pair > [ ] adj = new ArrayList [ MAXX * 10 + 3 ] ; static int [ ] dist = new int [ MAXX * 10 + 3 ] ; static int even ( int x ) { return x * 10 + 2 ; } static int odd ( int x ) { return x * 10 + 1 ; } static void addEdge ( int a , int b , int cost ) { adj [ even ( a ) ] . add ( new Pair ( odd ( b ) , cost ) ) ; adj [ odd ( a ) ] . add ( new Pair ( even ( b ) , cost ) ) ; adj [ odd ( b ) ] . add ( new Pair ( even ( a ) , cost ) ) ; adj [ even ( b ) ] . add ( new Pair ( odd ( a ) , cost ) ) ; } static int dijkstra ( int source , int destination ) { PriorityQueue < Pair > pq = new PriorityQueue < > ( ) ; pq . add ( new Pair ( 0 , even ( source ) ) ) ; dist [ even ( source ) ] = 0 ; while ( ! pq . isEmpty ( ) ) { int u = pq . poll ( ) . second ; for ( Pair p : adj [ u ] ) { int v = p . first ; int c = p . second ; if ( dist [ u ] + c < dist [ v ] ) { dist [ v ] = dist [ u ] + c ; pq . add ( new Pair ( dist [ v ] , v ) ) ; } } } return dist [ even ( destination ) ] ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAXX * 10 + 3 ; i ++ ) { adj [ i ] = new ArrayList < Pair > ( ) ; } Arrays . fill ( dist , INF ) ; int n = 5 , m = 6 ; addEdge ( 1 , 2 , 1 ) ; addEdge ( 2 , 3 , 2 ) ; addEdge ( 2 , 5 , 15 ) ; addEdge ( 3 , 5 , 1 ) ; addEdge ( 3 , 4 , 4 ) ; addEdge ( 5 , 4 , 3 ) ; int source = 1 ; int destination = n ; int ans = dijkstra ( source , destination ) ; if ( ans == INF ) System . out . println ( " - 1" ) ; else System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int make_xor_half ( ArrayList < Integer > arr ) { int sum = 0 , xr = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { int a = arr . get ( i ) ; sum += a ; xr ^= a ; } if ( 2 * xr == sum ) return - 1 ; if ( xr == 0 ) { arr . add ( sum ) ; return 1 ; } arr . add ( xr ) ; arr . add ( sum + xr ) ; return 2 ; } public static void main ( String [ ] args ) { int N = 7 ; ArrayList < Integer > nums = new ArrayList < Integer > ( Arrays . asList ( 3 , 4 , 7 , 1 , 2 , 5 , 6 ) ) ; int count = make_xor_half ( nums ) ; if ( count == - 1 ) System . out . print ( - 1 + "NEW_LINE"); else if ( count == 1 ) System . out . print ( nums . get ( N ) + "NEW_LINE"); else System . out . print ( nums . get ( N ) + " ▁ " + nums . get ( N + 1 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void colorRequired ( int arr [ ] , int n ) { boolean all_same = true ; boolean one_adjacent_same = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { all_same = false ; } if ( arr [ i ] == arr [ i + 1 ] ) { one_adjacent_same = true ; } } if ( all_same == true ) { System . out . print ( 1 + "NEW_LINE"); return ; } if ( n % 2 == 0 one_adjacent_same == true ) { System . out . print ( 2 + "NEW_LINE"); return ; } System . out . print ( 3 + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 1 , 2 } ; int n = arr . length ; colorRequired ( arr , n ) ; } }
import java . util . * ; class GFG { static int maxRemainingSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int i = 0 ; int j = n - 1 ; int result = 0 ; while ( i < j ) { if ( arr [ i ] < arr [ j ] ) { sum -= arr [ i ] ; i ++ ; } else { sum -= arr [ j ] ; j -- ; } result += sum ; } return result ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 1 , 7 , 2 , 1 } ; int N = arr . length ; System . out . println ( maxRemainingSum ( arr , N ) ) ; } }
import java . util . * ; class GFG { static String getLargestString ( String s , int k ) { int [ ] frequency_array = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { frequency_array [ s . charAt ( i ) - ' a ' ] ++ ; } String ans = " " ; for ( int i = 25 ; i >= 0 { if ( frequency_array [ i ] > k ) { int temp = k ; String st = String . valueOf ( ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } frequency_array [ i ] -= k ; int j = i - 1 ; while ( frequency_array [ j ] <= 0 && j >= 0 ) { j -- ; } if ( frequency_array [ j ] > 0 && j >= 0 ) { String str = String . valueOf ( ( char ) ( j + ' a ' ) ) ; ans += str ; frequency_array [ j ] -= 1 ; } else { break ; } } else if ( frequency_array [ i ] > 0 ) { int temp = frequency_array [ i ] ; frequency_array [ i ] -= temp ; String st = String . valueOf ( ( char ) ( i + ' a ' ) ) ; while ( temp > 0 ) { ans += st ; temp -- ; } } else { i -- ; } } return ans ; } public static void main ( String [ ] args ) { String S = " xxxxzza " ; int k = 3 ; System . out . print ( getLargestString ( S , k ) ) ; } }
import java . util . * ; class GFG { static int minOperations ( String s , int len ) { int operationCnt = 0 ; Stack < Character > st = new Stack < Character > ( ) ; int cntClosing = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( s . charAt ( i ) == ' { ' ) { if ( cntClosing > 0 ) { operationCnt ++ ; st . pop ( ) ; } st . add ( s . charAt ( i ) ) ; cntClosing = 0 ; } else if ( st . isEmpty ( ) ) { st . add ( ' { ' ) ; operationCnt ++ ; cntClosing = 1 ; } else { cntClosing = ( cntClosing + 1 ) % 2 ; if ( cntClosing == 0 ) { st . pop ( ) ; } } } operationCnt += st . size ( ) * 2 - cntClosing ; return operationCnt ; } public static void main ( String [ ] args ) { String str = " } } { " ; int len = str . length ( ) ; System . out . print ( minOperations ( str , len ) ) ; } }
import java . util . * ; class GFG { static int longestSubsequence ( String s ) { int n = s . length ( ) ; int answer = 0 ; char prev = ' - ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( prev != s . charAt ( i ) ) { prev = s . charAt ( i ) ; answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { String str = " ababa " ; System . out . print ( longestSubsequence ( str ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumGcdSubarray ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int k = 1 ; for ( int i = 1 ; i < n ; i ++ ) { k = Math . max ( k , gcd ( arr [ i ] , arr [ i - 1 ] ) ) ; } int cnt = 0 ; int maxLength = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { cnt ++ ; } else { maxLength = Math . max ( maxLength , cnt ) ; cnt = 0 ; } } maxLength = Math . max ( maxLength , cnt ) ; return maxLength ; } public static void main ( String args [ ] ) { int arr [ ] = { 18 , 3 , 6 , 9 } ; int n = arr . length ; System . out . println ( maximumGcdSubarray ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int missingnumber ( int n , int arr [ ] ) { int mn = Integer . MAX_VALUE , mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == - 1 && arr [ i - 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i - 1 ] ) ; mx = Math . max ( mx , arr [ i - 1 ] ) ; } if ( i < ( n - 1 ) && arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i + 1 ] ) ; mx = Math . max ( mx , arr [ i + 1 ] ) ; } } int res = ( mx + mn ) / 2 ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; int arr [ ] = { - 1 , 10 , - 1 , 12 , - 1 } ; int res = missingnumber ( n , arr ) ; System . out . print ( res ) ; } }
import java . util . * ; class GFG { static int minOperations ( int a [ ] , int b [ ] , int n ) { int minA = Arrays . stream ( a ) . min ( ) . getAsInt ( ) ; for ( int x = minA ; x >= 0 ; x -- ) { boolean check = true ; int operations = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( x % b [ i ] == a [ i ] % b [ i ] ) { operations += ( a [ i ] - x ) / b [ i ] ; } else { check = false ; break ; } } if ( check ) return operations ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 5 ; int A [ ] = { 5 , 7 , 10 , 5 , 15 } ; int B [ ] = { 2 , 2 , 1 , 3 , 5 } ; System . out . print ( minOperations ( A , B , N ) ) ; } }
class GFG { static void countDist ( int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) { int count = 0 ; int smallest = arr2 [ 0 ] ; int largest = arr2 [ 0 ] ; for ( int i = 0 ; i < m ; i ++ ) { smallest = Math . max ( smallest , arr2 [ i ] ) ; largest = Math . min ( largest , arr1 [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr1 [ i ] - smallest ) > k || Math . abs ( arr1 [ i ] - largest ) > k ) count ++ ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 3 , 1 , 4 } ; int n = arr1 . length ; int arr2 [ ] = { 5 , 1 , 2 } ; int m = arr2 . length ; int k = 2 ; countDist ( arr1 , n , arr2 , m , k ) ; } }
import java . util . * ; class GFG { static void MakeArray ( int a [ ] , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , 1 ) ; } if ( mp . size ( ) > k ) { System . out . print ( "Not possibleNEW_LINE"); return ; } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { ans . add ( i . getKey ( ) ) ; } while ( ans . size ( ) < k ) { ans . add ( 1 ) ; } for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) System . out . print ( ans . get ( j ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 1 } ; int K = 2 ; int size = arr . length ; MakeArray ( arr , size , K ) ; } }
import java . util . * ; class GFG { static int maxNormalSubstring ( String P , Set < Character > Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { if ( Q . contains ( P . charAt ( right ) ) ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { left ++ ; if ( Q . contains ( P . charAt ( left - 1 ) ) ) count -- ; if ( count < K ) break ; } } return ans ; } public static void main ( String [ ] args ) { String P = " giraffe " ; Set < Character > Q = new HashSet < > ( ) ; Q . add ( ' a ' ) ; Q . add ( ' f ' ) ; Q . add ( ' g ' ) ; Q . add ( ' r ' ) ; int K = 2 ; int N = P . length ( ) ; System . out . println ( maxNormalSubstring ( P , Q , K , N ) ) ; } }
import java . util . * ; class GFG { static int count_element ( int N , int K , int [ ] arr ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; ++ i ) if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } int answer = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { if ( mp . containsKey ( i . getKey ( ) + K ) ) answer += i . getValue ( ) ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 2 ; System . out . print ( count_element ( N , K , arr ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int digit_prod ( int x ) { int prod = 1 ; while ( x != 0 ) { prod *= x % 10 ; x = x / 10 ; } return prod ; } static int find_count ( int n ) { Map < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int t = digit_prod ( i ) ; mpp . put ( t , mpp . getOrDefault ( t , 0 ) + 1 ) ; } int ans = 1 ; int maxm = 0 ; for ( Integer x : mpp . values ( ) ) { if ( x > maxm ) { maxm = x ; ans = 1 ; } else if ( x == maxm ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { int N = 13 ; System . out . print ( find_count ( N ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i ; if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int findSubarraySum ( int arr [ ] , int n , int K ) { HashMap < Integer , Integer > prevSum = new HashMap < Integer , Integer > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == K ) { res ++ ; } if ( prevSum . containsKey ( currsum - K ) ) { res += ( prevSum . get ( currsum - K ) ) ; } if ( prevSum . containsKey ( currsum ) ) prevSum . put ( currsum , prevSum . get ( currsum ) + 1 ) ; else prevSum . put ( currsum , 1 ) ; } return res ; } static void countSubarray ( int arr [ ] , int n , int K ) { for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) { arr [ i ] = 1 ; } else { arr [ i ] = 0 ; } } System . out . print ( findSubarraySum ( arr , n , K ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int K = 2 ; int N = arr . length ; countSubarray ( arr , N , K ) ; } }
import java . util . * ; class GFG { static int find_maxm ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { int value = x . getKey ( ) ; int freq = x . getValue ( ) ; if ( value == freq ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = arr . length ; System . out . print ( find_maxm ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int pow ( int x , int y ) { if ( y == 1 ) return x ; if ( y == 0 ) return 1 ; int temp = pow ( x , y / 2 ) ; temp *= temp ; if ( y % 2 == 1 ) temp *= x ; return temp ; } static int noOfPermutations ( int [ ] a , int n ) { if ( n == 1 ) { return 1 ; } Arrays . sort ( a ) ; if ( a [ n - 1 ] == a [ n - 2 ] ) { return 0 ; } int x = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( a [ i ] == a [ i + 1 ] ) { if ( a [ i ] == a [ i + 2 ] ) { return 0 ; } x ++ ; i ++ ; } } return pow ( 2 , n - 2 * x - 1 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 3 , 4 } ; int n = a . length ; int num = noOfPermutations ( a , n ) ; System . out . print ( num ) ; } }
class GFG { static int GetMinSubarrayLength ( int a [ ] , int n ) { int ans = Math . max ( a [ 0 ] , a [ n - 1 ] ) ; int lo = 0 , hi = n - 1 ; while ( lo < n && a [ lo ] % ans == 0 ) lo ++ ; while ( hi > lo && a [ hi ] % ans == 0 ) hi -- ; return ( hi - lo + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 2 , 1 , 4 } ; int N = arr . length ; int Length = GetMinSubarrayLength ( arr , N ) ; System . out . println ( Length ) ; } }
import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxFreq = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { maxFreq = Math . max ( maxFreq , it . getValue ( ) ) ; } System . out . print ( N - maxFreq + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 5 , 9 , 8 , 8 , 7 , 7 , 5 , 7 , 7 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; } static int maxSum ( int a [ ] , int n ) { int S = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) S += a [ i ] ; int X = maxSubArraySum ( a , n ) ; return 2 * X - S ; } public static void main ( String [ ] args ) { int a [ ] = { - 1 , - 2 , - 3 } ; int n = a . length ; int max_sum = maxSum ( a , n ) ; System . out . print ( max_sum ) ; } }
import java . util . * ; class GFG { static int minimumValue ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int answer = 0 ; for ( int i = 0 ; i < k ; i ++ ) answer += arr [ i ] * arr [ i ] ; return answer * ( 2 * k - 2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 21 , 5 , 3 , 8 } , k = 3 ; int n = arr . length ; System . out . print ( minimumValue ( arr , n , k ) ) ; } }
class GFG { static boolean isPrime ( int n ) { int flag = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { flag = 0 ; break ; } } return ( flag == 1 ? true : false ) ; } static boolean isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static int countInterestingPrimes ( int n ) { int answer = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) { for ( int j = 1 ; j * j * j * j <= i ; j ++ ) { if ( isPerfectSquare ( i - j * j * j * j ) ) { answer ++ ; break ; } } } } return answer ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( countInterestingPrimes ( N ) ) ; } }
class GFG { static void noOfMoves ( int arr [ ] , int n ) { int cnt = 0 ; int maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . max ( maxCnt , cnt ) ; cnt = 0 ; } } } System . out . print ( maxCnt + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 } ; int N = arr . length ; noOfMoves ( arr , N ) ; int arr1 [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 } ; N = arr1 . length ; noOfMoves ( arr1 , N ) ; } }
class GFG { static void knowPair ( int a , int b , int n , int s , int m ) { int i = 0 , rem = 0 ; int count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = rem / m ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { System . out . print ( " YES " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int A = 1 ; int B = 2 ; int n = 3 ; int S = 4 ; int m = 2 ; knowPair ( A , B , n , S , m ) ; } }
class GFG { static int findSmallNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } static int findLargeNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } static int sumDivisibles ( int A , int B , int M ) { int first = findSmallNum ( A , M ) ; int last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; int n = ( B / M ) - ( A - 1 ) / M ; return n * ( first + last ) / 2 ; } public static void main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; System . out . print ( sumDivisibles ( A , B , M ) ) ; } }
import java . util . * ; class GFG { static int distinctSubString ( String P , int N ) { HashSet < String > S = new HashSet < String > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { boolean [ ] freq = new boolean [ 26 ] ; String s = " " ; for ( int j = i ; j < N ; ++ j ) { int pos = P . charAt ( j ) - ' a ' ; if ( freq [ pos ] == true ) break ; freq [ pos ] = true ; s += P . charAt ( j ) ; S . add ( s ) ; } } return S . size ( ) ; } public static void main ( String [ ] args ) { String S = " abba " ; int N = S . length ( ) ; System . out . print ( distinctSubString ( S , N ) ) ; } }
class GFG { static void calcSum ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) { int sum = 0 ; for ( int j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; int k = 3 ; calcSum ( arr , n , k ) ; } }
class GFG { static void calcSum ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; System . out . print ( sum + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) { sum = ( sum - arr [ i - k ] ) + arr [ i ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; int k = 3 ; calcSum ( arr , n , k ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int countPairs ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int maxValue ( int arr [ ] , int n , int moves ) { for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { int distance = n - 1 - i ; if ( moves < distance ) break ; int can_take = moves / distance ; int take = Math . min ( arr [ i ] , can_take ) ; arr [ n - 1 ] += take ; moves -= take * distance ; } } return arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 0 , 1 } ; int M = 5 ; int N = arr . length ; System . out . print ( maxValue ( arr , N , M ) ) ; } }
import java . util . * ; class GFG { static int findLCS ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } return mp . size ( ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] = { 3 , 2 , 1 } ; System . out . print ( findLCS ( arr , n ) ) ; } }
class GFG { static final int N = 3 ; static final int M = 3 ; static int maxProductPath ( int arr [ ] [ ] ) { int [ ] [ ] maxPath = new int [ N ] [ M ] ; int [ ] [ ] minPath = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int minVal = Integer . MAX_VALUE ; int maxVal = Integer . MIN_VALUE ; if ( i == 0 && j == 0 ) { maxVal = arr [ i ] [ j ] ; minVal = arr [ i ] [ j ] ; } if ( i > 0 ) { int tempMax = Math . max ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; maxVal = Math . max ( maxVal , tempMax ) ; int tempMin = Math . min ( maxPath [ i - 1 ] [ j ] * arr [ i ] [ j ] , minPath [ i - 1 ] [ j ] * arr [ i ] [ j ] ) ; minVal = Math . min ( minVal , tempMin ) ; } if ( j > 0 ) { int tempMax = Math . max ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; maxVal = Math . max ( maxVal , tempMax ) ; int tempMin = Math . min ( maxPath [ i ] [ j - 1 ] * arr [ i ] [ j ] , minPath [ i ] [ j - 1 ] * arr [ i ] [ j ] ) ; minVal = Math . min ( minVal , tempMin ) ; } maxPath [ i ] [ j ] = maxVal ; minPath [ i ] [ j ] = minVal ; } } return maxPath [ N - 1 ] [ M - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , - 2 , 3 } , { 4 , - 5 , 6 } , { - 7 , - 8 , 9 } } ; System . out . print ( maxProductPath ( arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int z = 1000000007 ; static int sh = 100000 ; static class util { int id ; int at ; int bt ; int ct ; int tat ; int wt ; } static util [ ] ar = new util [ sh + 1 ] ; static { for ( int i = 0 ; i < sh + 1 ; i ++ ) { ar [ i ] = new util ( ) ; } } static class util1 { int p_id ; int bt1 ; } ; static util1 range = new util1 ( ) ; static util1 [ ] tr = new util1 [ 4 * sh + 5 ] ; static { for ( int i = 0 ; i < 4 * sh + 5 ; i ++ ) { tr [ i ] = new util1 ( ) ; } } static int [ ] mp = new int [ sh + 1 ] ; static void update ( int node , int st , int end , int ind , int id1 , int b_t ) { if ( st == end ) { tr [ node ] . p_id = id1 ; tr [ node ] . bt1 = b_t ; return ; } int mid = ( st + end ) / 2 ; if ( ind <= mid ) update ( 2 * node , st , mid , ind , id1 , b_t ) ; else update ( 2 * node + 1 , mid + 1 , end , ind , id1 , b_t ) ; if ( tr [ 2 * node ] . bt1 < tr [ 2 * node + 1 ] . bt1 ) { tr [ node ] . bt1 = tr [ 2 * node ] . bt1 ; tr [ node ] . p_id = tr [ 2 * node ] . p_id ; } else { tr [ node ] . bt1 = tr [ 2 * node + 1 ] . bt1 ; tr [ node ] . p_id = tr [ 2 * node + 1 ] . p_id ; } } static util1 query ( int node , int st , int end , int lt , int rt ) { if ( end < lt st > rt ) return range ; if ( st >= lt && end <= rt ) return tr [ node ] ; int mid = ( st + end ) / 2 ; util1 lm = query ( 2 * node , st , mid , lt , rt ) ; util1 rm = query ( 2 * node + 1 , mid + 1 , end , lt , rt ) ; if ( lm . bt1 < rm . bt1 ) return lm ; return rm ; } static void non_premptive_sjf ( int n ) { int counter = n ; int upper_range = 0 ; int tm = Math . min ( Integer . MAX_VALUE , ar [ upper_range + 1 ] . at ) ; while ( counter != 0 ) { for ( ; upper_range <= n ; ) { upper_range ++ ; if ( ar [ upper_range ] . at > tm upper_range > n ) { upper_range -- ; break ; } update ( 1 , 1 , n , upper_range , ar [ upper_range ] . id , ar [ upper_range ] . bt ) ; } util1 res = query ( 1 , 1 , n , 1 , upper_range ) ; if ( res . bt1 != Integer . MAX_VALUE ) { counter -- ; int index = mp [ res . p_id ] ; tm += ( res . bt1 ) ; ar [ index ] . ct = tm ; ar [ index ] . tat = ar [ index ] . ct - ar [ index ] . at ; ar [ index ] . wt = ar [ index ] . tat - ar [ index ] . bt ; update ( 1 , 1 , n , index , Integer . MAX_VALUE , Integer . MAX_VALUE ) ; } else { tm = ar [ upper_range + 1 ] . at ; } } } static void execute ( int n ) { Arrays . sort ( ar , 1 , n , new Comparator < util > ( ) { public int compare ( util a , util b ) { if ( a . at == b . at ) return a . id - b . id ; return a . at - b . at ; } } ) ; for ( int i = 1 ; i <= n ; i ++ ) mp [ ar [ i ] . id ] = i ; non_premptive_sjf ( n ) ; } static void print ( int n ) { System . out . println ( " ProcessId ▁ Arrival ▁ Time ▁ Burst ▁ Time " + " ▁ Completion ▁ Time ▁ Turn ▁ Around ▁ Time ▁ Waiting ▁ Time " ) ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . printf ( "%d		%d		%d		%d		%d		%dNEW_LINE", ar[i].id, ar[i].at, ar[i].bt, ar[i].ct, ar[i].tat, ar[i].wt); } } public static void main ( String [ ] args ) { int n = 5 ; range . p_id = Integer . MAX_VALUE ; range . bt1 = Integer . MAX_VALUE ; for ( int i = 1 ; i <= 4 * sh + 1 ; i ++ ) { tr [ i ] . p_id = Integer . MAX_VALUE ; tr [ i ] . bt1 = Integer . MAX_VALUE ; } ar [ 1 ] . at = 1 ; ar [ 1 ] . bt = 7 ; ar [ 1 ] . id = 1 ; ar [ 2 ] . at = 2 ; ar [ 2 ] . bt = 5 ; ar [ 2 ] . id = 2 ; ar [ 3 ] . at = 3 ; ar [ 3 ] . bt = 1 ; ar [ 3 ] . id = 3 ; ar [ 4 ] . at = 4 ; ar [ 4 ] . bt = 2 ; ar [ 4 ] . id = 4 ; ar [ 5 ] . at = 5 ; ar [ 5 ] . bt = 8 ; ar [ 5 ] . id = 5 ; execute ( n ) ; print ( n ) ; } }
import java . util . * ; class GFG { static void vertical_Fill ( int records [ ] , int tape [ ] , int m , int n ) { int [ ] [ ] v = new int [ m ] [ n ] ; int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int z = 0 , j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { sum = 0 ; for ( int k = 0 ; k < i ; k ++ ) { sum += v [ j ] [ k ] ; } if ( sum + records [ z ] <= tape [ j ] ) { v [ j ] [ i ] = records [ z ] ; z ++ ; } } if ( v [ 2 ] [ i ] == 0 ) { break ; } } for ( int i = 0 ; i < m ; i ++ ) { Retrieval_Time = 0 ; System . out . print ( " tape ▁ " + ( i + 1 ) + " ▁ : ▁ [ ▁ " ) ; for ( j = 0 ; j < n ; j ++ ) { if ( v [ i ] [ j ] != 0 ) { System . out . print ( v [ i ] [ j ] + " ▁ " ) ; } else { break ; } } System . out . print ( " ] " ) ; for ( int k = 0 ; v [ i ] [ k ] != 0 ; k ++ ) { Retrieval_Time += v [ i ] [ k ] * ( j - k ) ; } Mrt = ( double ) Retrieval_Time / j ; System . out . print ( " TABSYMBOL MRT ▁ : ▁ " + Mrt + "NEW_LINE"); } } public static void main ( String [ ] args ) { int records [ ] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int tape [ ] = { 25 , 80 , 160 } ; int n = records . length ; int m = tape . length ; Arrays . sort ( records ) ; vertical_Fill ( records , tape , m , n ) ; } }
class GFG { static int MAX = 26 ; static int maxLength ( String str , int len ) { int res = 0 ; int lastPos [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { lastPos [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int C = str . charAt ( i ) - ' a ' ; if ( lastPos [ C ] != - 1 ) { res = Math . max ( len - ( i - lastPos [ C ] - 1 ) - 1 , res ) ; } lastPos [ C ] = i ; } return res ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . println ( maxLength ( str , len ) ) ; } }
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int min_element ( int [ ] A , int N ) { int min = A [ 0 ] ; int i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } static int countPartitions ( int [ ] A , int N ) { int count = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { int min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = arr . length ; System . out . println ( countPartitions ( arr , N ) ) ; } }
class GFG { static int minSwaps ( String s1 , String s2 ) { int c0 = 0 , c1 = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) { c0 ++ ; } else if ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) { c1 ++ ; } } int ans = c0 / 2 + c1 / 2 ; if ( c0 % 2 == 0 && c1 % 2 == 0 ) { return ans ; } else if ( ( c0 + c1 ) % 2 == 0 ) { return ans + 2 ; } else { return - 1 ; } } public static void main ( String [ ] args ) { String s1 = "0011" , s2 = "1111" ; int ans = minSwaps ( s1 , s2 ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int minChanges ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxElem = 0 ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { maxElem = Math . max ( maxElem , entry . getValue ( ) ) ; } return n - maxElem ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int n = arr . length ; System . out . println ( minChanges ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int minOperations ( int n , int m ) { int a = 0 , k = 1 ; int p = Math . max ( n , m ) ; while ( n != m ) { float s = ( float ) ( p - n + p - m ) ; float q = ( float ) ( ( - 1 + Math . sqrt ( 8 * s + 1 ) ) / 2 ) ; if ( q - Math . floor ( q ) == 0 ) { a = ( int ) q ; n = m ; } p = p + 1 ; } return a ; } public static void main ( String [ ] args ) { int n = 1 , m = 3 ; System . out . print ( minOperations ( n , m ) ) ; } }
class GFG { static void findSubSeq ( int arr [ ] , int n , int sum ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum < arr [ i ] ) arr [ i ] = - 1 ; else sum -= arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 17 , 25 , 46 , 94 , 201 , 400 } ; int n = arr . length ; int sum = 272 ; findSubSeq ( arr , n , sum ) ; } }
import java . util . * ; class GFG { static boolean hasCrest ( int arr [ ] , int n , int L , int R ) { int [ ] present = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { present [ i ] = 0 ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( int i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 } ; int N = arr . length ; int L = 2 ; int R = 6 ; if ( hasCrest ( arr , N , L , R ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static boolean IsLarger ( int [ ] X , int [ ] Y , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } static int solve ( int X [ ] , int Y [ ] , int n ) { int ans = 0 ; if ( IsLarger ( X , Y , n ) ) ans = 0 ; else { int d = Y [ 0 ] - X [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) X [ i ] += d ; if ( IsLarger ( X , Y , n ) ) ans = d ; else { ans = d + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int X [ ] = { 2 , 3 , 6 , 9 } ; int Y [ ] = { 3 , 4 , 8 , 1 } ; int n = X . length ; System . out . println ( solve ( X , Y , n ) ) ; } }
import java . util . * ; class GFG { static int max_bitwise_or ( int L , int R ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) , v3 = new Vector < Integer > ( ) ; int z = 0 , i , ans = 0 , cnt = 1 ; while ( L > 0 ) { v1 . add ( L % 2 ) ; L = L / 2 ; } while ( R > 0 ) { v2 . add ( R % 2 ) ; R = R / 2 ; } while ( v1 . size ( ) != v2 . size ( ) ) { v1 . add ( 0 ) ; } for ( i = v2 . size ( ) - 1 ; i >= 0 ; i -- ) { if ( v2 . get ( i ) == 1 && v1 . get ( i ) == 0 && z == 0 ) { z = 1 ; continue ; } if ( z == 1 ) { v1 . remove ( i ) ; v1 . add ( i , 1 ) ; } } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { v3 . add ( v2 . get ( i ) | v1 . get ( i ) ) ; } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { if ( v3 . get ( i ) == 1 ) { ans += cnt ; } cnt *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int L = 10 , R = 20 ; System . out . println ( max_bitwise_or ( L , R ) ) ; } }
class GFG { static int valueofX ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return sum / n ; } else { int A = sum / n , B = sum / n + 1 ; int ValueA = 0 , ValueB = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } public static void main ( String args [ ] ) { int n = 7 ; int arr [ ] = { 6 , 9 , 1 , 6 , 1 , 3 , 7 } ; System . out . println ( valueofX ( arr , n ) ) ; } }
class GFG { static int minLength ( int n ) { int ans = n / 26 ; if ( n % 26 != 0 ) ans ++ ; return ans ; } static String minString ( int n ) { int ans = n / 26 ; String res = " " ; while ( ans -- != 0 ) { res = res + " z " ; } if ( n % 26 != 0 ) { res = res + ( char ) ( ( n % 26 ) + 96 ) ; } return res ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( minLength ( n ) ) ; System . out . println ( minString ( n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100001 ; static int minHalls ( int lectures [ ] [ ] , int n ) { int [ ] prefix_sum = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix_sum [ lectures [ i ] [ 0 ] ] ++ ; prefix_sum [ lectures [ i ] [ 1 ] + 1 ] -- ; } int ans = prefix_sum [ 0 ] ; for ( int i = 1 ; i < MAX ; i ++ ) { prefix_sum [ i ] += prefix_sum [ i - 1 ] ; ans = Math . max ( ans , prefix_sum [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int lectures [ ] [ ] = { { 0 , 5 } , { 1 , 2 } , { 1 , 10 } } ; int n = lectures . length ; System . out . println ( minHalls ( lectures , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 543210 ; static Vector < String > ans = new Vector < String > ( ) ; static boolean isValidNum ( String x ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( mp . containsKey ( x . charAt ( i ) - '0' ) ) { return false ; } else if ( x . charAt ( i ) - '0' > 5 ) { return false ; } else { mp . put ( x . charAt ( i ) - '0' , 1 ) ; } } return true ; } static void generate ( ) { Queue < String > q = new LinkedList < String > ( ) ; q . add ( "1" ) ; q . add ( "2" ) ; q . add ( "3" ) ; q . add ( "4" ) ; q . add ( "5" ) ; boolean flag = true ; ans . add ( "0" ) ; while ( ! q . isEmpty ( ) ) { String x = q . peek ( ) ; q . remove ( ) ; if ( isValidNum ( x ) ) { ans . add ( x ) ; } if ( x . length ( ) == 6 ) continue ; for ( int i = 0 ; i <= 5 ; i ++ ) { String z = String . valueOf ( i ) ; String temp = x + z ; q . add ( temp ) ; } } } static boolean comp ( String a , String b ) { if ( a . length ( ) == b . length ( ) ) { int i = a . compareTo ( b ) ; return i < 0 ? true : false ; } else return a . length ( ) < b . length ( ) ; } static int findcount ( String l , String r ) { generate ( ) ; int count = 0 ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { String a = ans . get ( i ) ; if ( comp ( l , a ) && comp ( a , r ) ) { count ++ ; } else if ( a == l a == r ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String l = "1" , r = "1000" ; System . out . println ( findcount ( l , r ) ) ; } }
import java . util . * ; class GFG { static int [ ] Findpermutation ( int n ) { int [ ] a = new int [ n + 1 ] ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } public static void main ( String [ ] args ) { int n = 8 ; int [ ] v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( v [ i ] + " ▁ " ) ; } }
import java . util . Arrays ; public class Main { public static char [ ] lexo_small ( int n , int k ) { char arr [ ] = new char [ n ] ; Arrays . fill ( arr , ' a ' ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { k -= i ; if ( k >= 0 ) { if ( k >= 26 ) { arr [ i ] = ' z ' ; k -= 26 ; } else { arr [ i ] = ( char ) ( k + 97 - 1 ) ; k -= arr [ i ] - ' a ' + 1 ; } } else break ; k += i ; } return arr ; } public static void main ( String [ ] args ) { int n = 5 , k = 42 ; char arr [ ] = lexo_small ( n , k ) ; System . out . print ( new String ( arr ) ) ; } }
import java . util . * ; class GFG { static boolean IsPossible ( String s ) { int n = s . length ( ) ; HashSet < Character > count = new HashSet < > ( ) ; HashMap < Character , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { count . add ( s . charAt ( i ) ) ; map . put ( s . charAt ( i ) , map . get ( s . charAt ( i ) ) == null ? 1 : map . get ( s . charAt ( i ) ) + 1 ) ; } if ( count . size ( ) == 1 ) return true ; if ( count . size ( ) > 2 ) return false ; String newString = count . toArray ( ) . toString ( ) ; int j = 0 ; char it = newString . charAt ( j ) ; int x = 0 , y = 0 ; x = map . get ( it ) == null ? 0 : map . get ( it ) ; j ++ ; it = newString . charAt ( j ) ; y = map . get ( it ) == null ? 0 : map . get ( it ) ; if ( Math . abs ( x - y ) <= 1 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " aaaddad " ; if ( IsPossible ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . * ; class GFG { static int Substring ( String s ) { int ans = 1 , temp = 1 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) { ++ temp ; } else { ans = Math . max ( ans , temp ) ; temp = 1 ; } } ans = Math . max ( ans , temp ) ; return ans ; } public static void main ( String [ ] args ) { String s = " abcdddddeff " ; System . out . println ( Substring ( s ) ) ; } }
class GFG { public static int Balanced_Substring ( String str , int n ) { int ans = 0 ; int [ ] arr = new int [ n / 2 + 1 ] ; int d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) d ++ ; else { if ( d == 1 ) { for ( int j = 2 ; j <= n / 2 + 1 && arr [ j ] != 0 ; j ++ ) arr [ j ] = 0 ; } ++ ans ; ans += arr [ d ] ; arr [ d ] ++ ; d -- ; } } return ans ; } public static void main ( String [ ] args ) { String str = " ( ) ( ) ( ) " ; int n = str . length ( ) ; System . out . println ( Balanced_Substring ( str , n ) ) ; } }
class GFG { static int power ( long x , int y , int mod ) { long res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % mod ; x = ( x * x ) % mod ; y = ( y >> 1 ) ; } return ( int ) ( res % mod ) ; } static int ncr ( int n , int r , int mod ) { long res = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { res = ( res * ( n - i + 1 ) ) % mod ; int inv = power ( i , mod - 2 , mod ) ; res = ( res * inv ) % mod ; } return ( int ) ( res % mod ) ; } static int NoOfChords ( int A ) { int mod = ( int ) ( 1e9 + 7 ) ; long ans = ncr ( 2 * A , A , mod ) ; int inv = power ( A + 1 , mod - 2 , mod ) ; ans = ( ans * inv ) % mod ; return ( int ) ( ans % mod ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( NoOfChords ( N ) ) ; } }
import java . io . * ; class GFG { static int StrictlyPositiveXor ( int [ ] A , int N ) { int allxor = 0 ; boolean checkallzero = true ; for ( int i = 0 ; i < N ; i += 1 ) { allxor ^= A [ i ] ; if ( A [ i ] > 0 ) checkallzero = false ; } if ( allxor != 0 ) return N ; if ( checkallzero ) return - 1 ; int l = N , r = - 1 ; for ( int i = 0 ; i < N ; i += 1 ) { if ( A [ i ] > 0 ) { l = i + 1 ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -= 1 ) { if ( A [ i ] > 0 ) { r = i + 1 ; break ; } } return Math . max ( N - l , r - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 0 , 0 , 1 } ; int N = A . length ; System . out . print ( StrictlyPositiveXor ( A , N ) ) ; } }
class GFG { static int MAX_INT = 1000000 ; static int solve ( int [ ] array , int N ) { int [ ] [ ] pos = new int [ 5 ] [ 10000 ] ; int [ ] pref = new int [ 5 ] ; if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = 0 ; } int ans = MAX_INT ; for ( int i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] ] [ pos [ array [ i ] ] . length - 1 ] = i ; if ( array [ i ] == 4 ) { int end = i ; int start = i ; for ( int j = 3 ; j >= 0 ; j -- ) { int s = 0 ; int e = pos [ j ] . length - 1 ; int temp = - 1 ; while ( s <= e ) { int m = ( s + e ) / 2 ; if ( pos [ j ] [ m ] <= start ) { temp = pos [ j ] [ m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . min ( ans , end - start + 1 ) ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 } ; int N = array . length ; System . out . println ( solve ( array , N ) ) ; } }
import java . util . * ; public class GFG { static void findWeights ( int X ) { int sum = 0 ; int power = 0 ; int number = 3 ; while ( sum < X ) { sum = number - 1 ; sum /= 2 ; power ++ ; number *= 3 ; } int ans = 1 ; for ( int i = 1 ; i <= power ; i ++ ) { System . out . print ( ans + " ▁ " ) ; ans = ans * 3 ; } } public static void main ( String [ ] args ) { int X = 2 ; findWeights ( X ) ; } }
import java . util . * ; class GFG { static int MAX = 100001 ; static void find_distinct ( int a [ ] , int n , int q , int queries [ ] ) { int [ ] check = new int [ MAX ] ; int [ ] idx = new int [ MAX ] ; int cnt = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( check [ a [ i ] ] == 0 ) { idx [ i ] = cnt ; check [ a [ i ] ] = 1 ; cnt ++ ; } else { idx [ i ] = cnt - 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { int m = queries [ i ] ; System . out . print ( idx [ m ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; int queries [ ] = { 0 , 3 , 5 , 7 } ; int q = queries . length ; find_distinct ( a , n , q , queries ) ; } }
import java . util . * ; class GFG { static Vector < Integer > solve ( int X , Vector < Integer > A ) { int min = Integer . MAX_VALUE ; int ind = - 1 ; for ( int i = 0 ; i < A . size ( ) ; i ++ ) { if ( A . get ( i ) < min ) { min = A . get ( i ) ; ind = i ; } } int maxIndChosen = X / min ; Vector < Integer > ans = new Vector < > ( ) ; if ( maxIndChosen == 0 ) { return ans ; } for ( int i = 0 ; i < maxIndChosen ; i ++ ) { ans . add ( ind ) ; } int temp = maxIndChosen ; int sum = maxIndChosen * A . get ( ind ) ; for ( int i = 0 ; i < ind ; i ++ ) { if ( sum - X == 0 temp == 0 ) break ; while ( ( sum - A . get ( ind ) + A . get ( i ) ) <= X && temp != 0 ) { ans . remove ( 0 ) ; ans . add ( i ) ; temp -- ; sum += ( A . get ( i ) - A . get ( ind ) ) ; } } Collections . sort ( ans ) ; return ans ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 5 , 6 , 4 , 8 } ; Vector < Integer > A = new Vector < Integer > ( Arrays . asList ( arr ) ) ; int X = 18 ; Vector < Integer > ans = solve ( X , A ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static boolean matrix_exist ( int row [ ] , int column [ ] , int r , int c ) { int row_sum = 0 ; int column_sum = 0 ; int row_max = - 1 ; int column_max = - 1 ; int row_non_zero = 0 ; int column_non_zero = 0 ; for ( int i = 0 ; i < r ; i ++ ) { row_sum += row [ i ] ; row_max = Math . max ( row_max , row [ i ] ) ; if ( row [ i ] > 0 ) { row_non_zero ++ ; } } for ( int i = 0 ; i < c ; i ++ ) { column_sum += column [ i ] ; column_max = Math . max ( column_max , column [ i ] ) ; if ( column [ i ] > 0 ) { column_non_zero ++ ; } } if ( ( row_sum != column_sum ) || ( row_max > column_non_zero ) || ( column_max > row_non_zero ) ) { return false ; } return true ; } public static void main ( String [ ] args ) { int row [ ] = { 2 , 2 , 2 , 2 , 2 } ; int column [ ] = { 5 , 5 , 0 , 0 } ; int r = row . length ; int c = column . length ; if ( matrix_exist ( row , column , r , c ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int maxDistance ( int arr [ ] , int n ) { if ( arr [ 0 ] != arr [ n - 1 ] ) return ( n - 1 ) ; int i = n - 1 ; while ( i > 0 ) { if ( arr [ i ] != arr [ 0 ] ) break ; i -- ; } int distFirst = ( i == 0 ) ? - 1 : i ; i = 0 ; while ( i < n - 1 ) { if ( arr [ i ] != arr [ n - 1 ] ) break ; i ++ ; } int distLast = ( i == n - 1 ) ? - 1 : ( n - 1 - i ) ; int maxDist = Math . max ( distFirst , distLast ) ; return maxDist ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 4 , 1 , 2 , 1 , 4 } ; int n = arr . length ; System . out . print ( maxDistance ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void solve ( int n , int k ) { int mini = 0 ; int x1 = 1 ; int [ ] a = new int [ k ] ; for ( int i = 1 ; i <= k ; i ++ ) { mini += x1 ; a [ i - 1 ] = x1 ; x1 += 1 ; } if ( n < mini ) { System . out . print ( " - 1" ) ; return ; } int rem = n - mini ; int cnt = rem / k ; rem = rem % k ; for ( int i = 0 ; i < k ; i ++ ) a [ i ] += cnt ; for ( int i = k - 1 ; i > 0 && rem > 0 ; i -- ) { int xx = a [ i - 1 ] * 2 ; int left = xx - a [ i ] ; if ( rem >= left ) { a [ i ] = xx ; rem -= left ; } else { a [ i ] += rem ; rem = 0 ; } } int sum = a [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) { if ( a [ i ] > 2 * a [ i - 1 ] ) { System . out . print ( " - 1" ) ; return ; } sum += a [ i ] ; } if ( sum != n ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 26 , k = 6 ; solve ( n , k ) ; } }
import java . util . Arrays ; class GFG { static int minOperations ( int [ ] arr , int n ) { int maxi , result = 0 ; int [ ] freq = new int [ 1000001 ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; freq [ x ] ++ ; } maxi = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; for ( int i = 1 ; i <= maxi ; i ++ ) { if ( freq [ i ] != 0 ) { for ( int j = i * 2 ; j <= maxi ; j = j + i ) { freq [ j ] = 0 ; } result ++ ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 2 , 4 , 4 , 4 } ; int n = arr . length ; System . out . println ( minOperations ( arr , n ) ) ; } }
class GFG { static int MAX = 25 ; static int getMinSum ( int arr [ ] , int n ) { int bits_count [ ] = new int [ MAX ] , max_bit = 0 , sum = 0 , ans = 0 ; for ( int d = 0 ; d < n ; d ++ ) { int e = arr [ d ] , f = 0 ; while ( e > 0 ) { int rem = e % 2 ; e = e / 2 ; if ( rem == 1 ) { bits_count [ f ] += rem ; } f ++ ; } max_bit = Math . max ( max_bit , f ) ; } for ( int d = 0 ; d < max_bit ; d ++ ) { int temp = ( int ) Math . pow ( 2 , d ) ; if ( bits_count [ d ] > n / 2 ) ans = ans + temp ; } for ( int d = 0 ; d < n ; d ++ ) { arr [ d ] = arr [ d ] ^ ans ; sum = sum + arr [ d ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 11 , 15 } ; int n = arr . length ; System . out . println ( getMinSum ( arr , n ) ) ; } }
import java . util . HashSet ; import java . util . Iterator ; import java . util . Set ; class GFG { static int findMinimumX ( int a [ ] , int n ) { Set < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) st . add ( a [ i ] ) ; if ( st . size ( ) == 1 ) return 0 ; if ( st . size ( ) == 2 ) { Iterator < Integer > it = st . iterator ( ) ; int el1 = it . next ( ) ; int el2 = it . next ( ) ; if ( ( el2 - el1 ) % 2 == 0 ) return ( el2 - el1 ) / 2 ; else return ( el2 - el1 ) ; } if ( st . size ( ) == 3 ) { Iterator < Integer > it = st . iterator ( ) ; int el1 = it . next ( ) ; int el2 = it . next ( ) ; int el3 = it . next ( ) ; if ( ( el2 - el1 ) == ( el3 - el2 ) ) return el2 - el1 ; else return - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 4 , 7 , 4 , 1 } ; int n = a . length ; System . out . println ( findMinimumX ( a , n ) ) ; } }
import java . util . * ; class GFG { static int findElementsCrossed ( int arr [ ] , int a , int b , int n ) { int aa = a ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a == 0 && b == 0 ) break ; else if ( a == 0 ) { if ( arr [ i ] == 1 ) { b -= 1 ; a = Math . min ( aa , a + 1 ) ; } else b -= 1 ; } else if ( b == 0 ) a -- ; else if ( arr [ i ] == 1 && a < aa ) { b -= 1 ; a = Math . min ( aa , a + 1 ) ; } else a -- ; ans ++ ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 0 , 1 , 0 , 1 } ; int n = arr . length ; int a = 1 ; int b = 2 ; System . out . println ( findElementsCrossed ( arr , a , b , n ) ) ; } }
import java . util . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void pushBinaryToBorder ( int arr [ ] , int n ) { int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 1 ) arr [ count1 ++ ] = arr [ i ] ; while ( count1 < n ) arr [ count1 ++ ] = 1 ; int lastNonOne = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == 1 ) continue ; if ( lastNonOne == 0 ) { lastNonOne = i ; } if ( arr [ i ] != 0 ) arr [ lastNonOne -- ] = arr [ i ] ; } while ( lastNonOne >= 0 ) arr [ lastNonOne -- ] = 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 0 , 0 , 0 , 3 , 6 } ; int n = arr . length ; pushBinaryToBorder ( arr , n ) ; printArr ( arr , n ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int solve ( int health [ ] , int n ) { int currentgcd = gcd ( health [ 0 ] , health [ 1 ] ) ; for ( int i = 2 ; i < n ; ++ i ) { currentgcd = gcd ( currentgcd , health [ i ] ) ; } return currentgcd ; } public static void main ( String args [ ] ) { int health [ ] = { 4 , 6 , 8 , 12 } ; int n = health . length ; System . out . println ( solve ( health , n ) ) ; } }
import java . util . * ; class GFG { static void Find_Sequence ( int [ ] arr , int n ) { Vector < Integer > inc_arr = new Vector < > ( ) , dec_arr = new Vector < > ( ) ; int flag = 0 ; long inc = - 1 , dec = ( long ) 1e7 ; for ( int i = 0 ; i < n ; i ++ ) { if ( inc < arr [ i ] && arr [ i ] < dec ) { if ( arr [ i ] < arr [ i + 1 ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } } else if ( inc < arr [ i ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else if ( dec > arr [ i ] ) { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } else { System . out . println ( - 1 ) ; flag = 1 ; break ; } } if ( flag == 0 ) { for ( int i : inc_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; for ( int i : dec_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 } ; int n = arr . length ; Find_Sequence ( arr , n ) ; } }
import java . util . * ; class GFG { static int reverse ( int n ) { int rev = 0 ; while ( n != 0 ) { rev = ( rev * 10 ) + ( n % 10 ) ; n /= 10 ; } return rev ; } static void getSum ( int n ) { n = reverse ( n ) ; int sumOdd = 0 , sumEven = 0 , c = 1 ; while ( n != 0 ) { if ( c % 2 == 0 ) sumEven += n % 10 ; else sumOdd += n % 10 ; n /= 10 ; c ++ ; } System . out . println ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; System . out . println ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void main ( String args [ ] ) { int n = 457892 ; getSum ( n ) ; } }
class GFG { static void getSum ( int n ) { boolean isOdd = ( n % 2 == 1 ) ? true : false ; int sumOdd = 0 , sumEven = 0 ; while ( n != 0 ) { if ( isOdd ) sumOdd += n % 10 ; else sumEven += n % 10 ; isOdd = ! isOdd ; n /= 10 ; } System . out . println ( " Sum ▁ odd ▁ = ▁ " + sumOdd ) ; System . out . println ( " Sum ▁ even ▁ = ▁ " + sumEven ) ; } public static void main ( String [ ] args ) { int n = 457892 ; getSum ( n ) ; } }
class GFG { static int bankNotes ( int A , int B , int S , int N ) { int numerator = S - ( B * N ) ; int denominator = A - B ; if ( numerator % denominator == 0 ) return ( numerator / denominator ) ; return - 1 ; } public static void main ( String [ ] args ) { int A = 1 , B = 2 , S = 7 , N = 5 ; System . out . print ( bankNotes ( A , B , S , N ) ) ; } }
class GfG { static void twoParts ( String str ) { int flag = 0 ; String a = " " ; char [ ] gfg = str . toCharArray ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( gfg [ i ] == '4' ) { gfg [ i ] = '3' ; a += '1' ; flag = 1 ; } else if ( flag != 0 ) a += '0' ; } str = new String ( gfg ) ; System . out . print ( str + " ▁ " + a ) ; } public static void main ( String [ ] args ) { String str = "9441" ; twoParts ( str ) ; } }
import java . lang . Math ; class GfG { static int longestSubstring ( String s ) { int cnt = 1 , maxi = 1 ; int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i - 1 ) ) cnt ++ ; else { maxi = Math . max ( cnt , maxi ) ; cnt = 1 ; } } maxi = Math . max ( cnt , maxi ) ; return maxi ; } public static void main ( String [ ] args ) { String s = " ccccdeededff " ; System . out . println ( longestSubstring ( s ) ) ; } }
class GFG { static int Minimum_Operations ( int [ ] a , int n ) { int [ ] np = new int [ n + 1 ] ; np [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } int pos = 0 ; int ans = n ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = Math . min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } public static void main ( String args [ ] ) { int [ ] a = { - 1 , 0 , 1 , 2 } ; int n = a . length ; System . out . print ( Minimum_Operations ( a , n ) ) ; } }
class GFG { static int findSumofEle ( int [ ] arr1 , int m , int [ ] arr2 , int n , int k ) { float arraySum = 0 ; for ( int i = 0 ; i < n ; i ++ ) arraySum += arr2 [ i ] ; float mean = arraySum / n ; int sumOfElements = 0 ; float difference = 0 ; for ( int i = 0 ; i < m ; i ++ ) { difference = arr1 [ i ] - mean ; if ( ( difference < 0 ) && ( k > ( - 1 ) * difference ) ) { sumOfElements += arr1 [ i ] ; } if ( ( difference >= 0 ) && ( k > difference ) ) { sumOfElements += arr1 [ i ] ; } } return sumOfElements ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 , 4 , 7 , 9 } ; int [ ] arr2 = { 0 , 1 , 2 , 1 , 1 , 4 } ; int k = 2 ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( findSumofEle ( arr1 , m , arr2 , n , k ) ) ; } }
import java . util . * ; class GFG { static void findIntegers ( int n , int x , int y ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans . add ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { System . out . print ( " - 1" ) ; return ; } ans . add ( y - ( n - 1 ) ) ; int store = 0 ; for ( int i = 0 ; i < n ; i ++ ) store += ans . get ( i ) * ans . get ( i ) ; if ( store < x ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ; } }
class CFG { static int Minsteps ( int n , int m ) { int ans = 0 ; while ( m > n ) { if ( m % 2 != 0 ) { m ++ ; ans ++ ; } m /= 2 ; ans ++ ; } return ans + n - m ; } public static void main ( String [ ] args ) { int n = 4 , m = 6 ; System . out . println ( Minsteps ( n , m ) ) ; } }
class GFG { static int getsum ( int x ) { return ( x * ( x + 1 ) ) / 2 ; } static int countJumps ( int n ) { n = Math . abs ( n ) ; int ans = 0 ; while ( getsum ( ans ) < n || ( ( getsum ( ans ) - n ) & 1 ) > 0 ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int n = 9 ; System . out . println ( countJumps ( n ) ) ; } }
class GFG { static int maxCandies ( int arr [ ] , int n ) { int prevBought = arr [ n - 1 ] ; int candies = prevBought ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int x = Math . min ( prevBought - 1 , arr [ i ] ) ; if ( x >= 0 ) { candies += x ; prevBought = x ; } } return candies ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 6 } ; int n = arr . length ; System . out . println ( maxCandies ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static boolean isPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - i - 1 ) ) { return false ; } } return true ; } static boolean ans ( String s ) { String s2 = s ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { s2 = s2 . charAt ( s2 . length ( ) - 1 ) + s2 ; s2 = s2 . substring ( 0 , s2 . length ( ) - 1 ) ; if ( ( s == null ? s2 != null : ! s . equals ( s2 ) ) && isPalindrome ( s2 ) ) { return true ; } } return false ; } static int solve ( String s ) { if ( s . length ( ) <= 3 ) { return - 1 ; } int cnt [ ] = new int [ 25 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } if ( Arrays . stream ( cnt ) . max ( ) . getAsInt ( ) >= ( s . length ( ) - 1 ) ) { return - 1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } public static void main ( String [ ] args ) { String s = " nolon " ; System . out . println ( solve ( s ) ) ; } }
import java . util . Arrays ; class GFG { static int solveEven ( String s ) { if ( s . length ( ) % 2 == 1 ) { return 2 ; } String ls = s . substring ( 0 , s . length ( ) / 2 ) ; String rs = s . substring ( s . length ( ) / 2 , s . length ( ) ) ; if ( ls != rs ) { return 1 ; } return solveEven ( ls ) ; } static int solveOdd ( String s ) { return 2 ; } static int solve ( String s ) { if ( s . length ( ) <= 3 ) { return - 1 ; } int cnt [ ] = new int [ 25 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } if ( Arrays . stream ( cnt ) . max ( ) . getAsInt ( ) >= s . length ( ) - 1 ) { return - 1 ; } if ( s . length ( ) % 2 == 0 ) { return solveEven ( s ) ; } if ( s . length ( ) % 2 == 1 ) { return solveOdd ( s ) ; } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { String s = " nolon " ; System . out . println ( solve ( s ) ) ; } }
class GFG { static int minChanges ( char [ ] str , int n ) { int count = 0 , zeros = 0 , ones = 0 ; if ( str [ 0 ] != '1' ) { count ++ ; ones ++ ; } for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == '0' ) zeros ++ ; else ones ++ ; if ( zeros > ones ) { zeros -- ; ones ++ ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { char [ ] str = "0000" . toCharArray ( ) ; int n = str . length ; System . out . print ( minChanges ( str , n ) ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } static int minGCD ( int arr [ ] , int n ) { int minGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) minGCD = __gcd ( minGCD , arr [ i ] ) ; return minGCD ; } static int minLCM ( int arr [ ] , int n ) { int minLCM = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) minLCM = Math . min ( minLCM , arr [ i ] ) ; return minLCM ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 66 , 14 , 521 } ; int n = arr . length ; System . out . println ( " LCM ▁ = ▁ " + minLCM ( arr , n ) + " ▁ GCD ▁ = ▁ " + minGCD ( arr , n ) ) ; } }
class GFG { static int steps ( int cur , int x , int n ) { if ( x == 0 ) return Integer . MAX_VALUE ; if ( x > 0 ) return Math . abs ( ( n - cur ) / x ) ; else return Math . abs ( ( cur - 1 ) / x ) ; } static int countSteps ( int curx , int cury , int n , int m , int [ ] [ ] moves ) { int count = 0 ; int k = moves . length ; for ( int i = 0 ; i < k ; i ++ ) { int x = moves [ i ] [ 0 ] ; int y = moves [ i ] [ 1 ] ; int stepct = Math . min ( steps ( curx , x , n ) , steps ( cury , y , m ) ) ; count += stepct ; curx += stepct * x ; cury += stepct * y ; } return count ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 , x = 1 , y = 1 ; int [ ] [ ] moves = { { 1 , 1 } , { 1 , 1 } , { 0 , - 2 } } ; System . out . print ( countSteps ( x , y , n , m , moves ) ) ; } }
import java . util . * ; class GFG { static int minimumRemoval ( int n , int a [ ] ) { Map < Integer , Integer > c = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c . containsKey ( a [ i ] ) ) { c . put ( a [ i ] , c . get ( a [ i ] ) + 1 ) ; } else { c . put ( a [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean ok = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int x = ( 1 << j ) - a [ i ] ; if ( ( c . get ( x ) != null && ( c . get ( x ) > 1 ) ) || c . get ( x ) != null && ( c . get ( x ) == 1 && x != a [ i ] ) ) { ok = true ; break ; } } if ( ! ok ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 7 , 1 , 5 , 4 , 9 } ; int n = a . length ; System . out . println ( minimumRemoval ( n , a ) ) ; } }
class GFG { static String getCount ( int N ) { if ( N % 2 == 1 ) return "0" ; String result = "9" ; for ( int i = 1 ; i <= N / 2 - 1 ; i ++ ) result += "0" ; return result ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( getCount ( N ) ) ; } }
class GFG { static boolean canFormTeam ( int n , int m ) { if ( n >= 1 && m >= 2 ) return true ; if ( m >= 1 && n >= 2 ) return true ; return false ; } static int maxTeams ( int n , int m ) { int count = 0 ; while ( canFormTeam ( n , m ) ) { if ( n > m ) { n -= 2 ; m -= 1 ; } else { m -= 2 ; n -= 1 ; } count ++ ; } return count ; } public static void main ( String args [ ] ) { int n = 4 , m = 5 ; System . out . println ( maxTeams ( n , m ) ) ; } }
class GFG { static String formStringMinOperations ( char [ ] s ) { int count [ ] = new int [ 3 ] ; for ( char c : s ) { count [ ( int ) c - 48 ] += 1 ; } int processed [ ] = new int [ 3 ] ; int reqd = ( int ) s . length / 3 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( count [ s [ i ] - '0' ] == reqd ) { continue ; } if ( s [ i ] == '0' && count [ 0 ] > reqd && processed [ 0 ] >= reqd ) { if ( count [ 1 ] < reqd ) { s [ i ] = '1' ; count [ 1 ] ++ ; count [ 0 ] -- ; } else if ( count [ 2 ] < reqd ) { s [ i ] = '2' ; count [ 2 ] ++ ; count [ 0 ] -- ; } } if ( s [ i ] == '1' && count [ 1 ] > reqd ) { if ( count [ 0 ] < reqd ) { s [ i ] = '0' ; count [ 0 ] ++ ; count [ 1 ] -- ; } else if ( count [ 2 ] < reqd && processed [ 1 ] >= reqd ) { s [ i ] = '2' ; count [ 2 ] ++ ; count [ 1 ] -- ; } } if ( s [ i ] == '2' && count [ 2 ] > reqd ) { if ( count [ 0 ] < reqd ) { s [ i ] = '0' ; count [ 0 ] ++ ; count [ 2 ] -- ; } else if ( count [ 1 ] < reqd ) { s [ i ] = '1' ; count [ 1 ] ++ ; count [ 2 ] -- ; } } processed [ s [ i ] - '0' ] ++ ; } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = "011200" ; System . out . println ( formStringMinOperations ( s . toCharArray ( ) ) ) ; } }
class GFG { static void find_rectangle ( int area ) { int l = 0 , b = 0 ; int M = ( int ) Math . sqrt ( area ) , ans ; for ( int i = M ; i >= 1 ; i -- ) { if ( area % i == 0 ) { l = ( area / i ) ; b = i ; break ; } } System . out . println ( " l ▁ = ▁ " + l + " , ▁ b ▁ = ▁ " + b ) ; } public static void main ( String [ ] args ) { int area = 99 ; find_rectangle ( area ) ; } }
import java . io . * ; class GFG { static void find_rectangle ( int area ) { for ( int i = ( int ) Math . ceil ( Math . sqrt ( area ) ) ; i <= area ; i ++ ) { if ( area / i * i == area ) { System . out . println ( i + " ▁ " + ( int ) ( area / i ) ) ; return ; } } } public static void main ( String [ ] args ) { int area = 99 ; find_rectangle ( area ) ; } }
import java . util . * ; class GFG { static int sizeSubSet ( int a [ ] , int k , int n ) { Arrays . sort ( a ) ; HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % k != 0 || s . get ( a [ i ] / k ) == null ) s . put ( a [ i ] , s . get ( a [ i ] ) == null ? 1 : s . get ( a [ i ] ) + 1 ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . length ; int k = 2 ; System . out . println ( sizeSubSet ( a , k , n ) ) ; } }
import java . util . * ; class solution { static int minimumSets ( String s , int y ) { int cnt = 0 ; int num = 0 ; int l = s . length ( ) ; boolean f = false ; for ( int i = 0 ; i < l ; i ++ ) { num = num * 10 + ( s . charAt ( i ) - '0' ) ; if ( num <= y ) f = true ; { if ( f ) cnt += 1 ; num = s . charAt ( i ) - '0' ; f = false ; if ( num <= y ) f = true ; else num = 0 ; } } if ( f == true ) cnt += 1 ; return cnt ; } public static void main ( String args [ ] ) { String s = "1234" ; int y = 30 ; System . out . println ( minimumSets ( s , y ) ) ; } }
import java . util . * ; class solution { void printArr ( int b [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + b [ i ] + " ▁ " ) ; } } void ModifiedArray ( int a [ ] , int n ) { int l = 0 , r = Integer . MAX_VALUE ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 6 } ; int n = a . length ; solution s = new solution ( ) ; s . ModifiedArray ( a , 2 * n ) ; } }
class GFG { static int [ ] numberOfLines ( String S , int [ ] widths ) { if ( S . isEmpty ( ) ) return new int [ ] { 0 , 0 } ; int lines = 1 , width = 0 ; for ( char character : S . toCharArray ( ) ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return new int [ ] { lines , width } ; } public static void main ( String [ ] args ) { String S = " bbbcccdddaa " ; int widths [ ] = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; int [ ] ans = numberOfLines ( S , widths ) ; System . out . print ( ans [ 0 ] + " ▁ " + ans [ 1 ] + "NEW_LINE"); } }
import java . util . HashMap ; import java . util . Map ; import java . util . Arrays ; class GfG { static String canReorder ( int A [ ] , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( A [ i ] ) ) m . put ( A [ i ] , m . get ( A [ i ] ) + 1 ) ; else m . put ( A [ i ] , 1 ) ; } Arrays . sort ( A ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . get ( A [ i ] ) == 0 ) continue ; if ( m . containsKey ( 2 * A [ i ] ) ) { count += 2 ; m . put ( A [ i ] , m . get ( A [ i ] ) - 1 ) ; m . put ( 2 * A [ i ] , m . get ( 2 * A [ i ] ) - 1 ) ; } } if ( count == n ) return " true " ; else return " false " ; } public static void main ( String [ ] args ) { int A [ ] = { 4 , - 2 , 2 , - 4 } ; int n = A . length ; System . out . println ( canReorder ( A , n ) ) ; } }
import java . util . * ; class GFG { static int minimizeDiff ( int [ ] arr , int n , int k ) { int max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int min = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; if ( ( max - min ) <= k ) { return ( max - min ) ; } int avg = ( max + min ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > avg ) { arr [ i ] -= k ; } else { arr [ i ] += k ; } } max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; min = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; return ( max - min ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 16 , 12 , 9 , 20 } ; int n = 5 ; int k = 3 ; System . out . println ( " Max ▁ height ▁ difference ▁ = ▁ " + minimizeDiff ( arr , n , k ) ) ; } }
class GFG { static void maxLitres ( int budget , int plastic , int glass , int refund ) { if ( glass - refund < plastic ) { int ans = Math . max ( ( budget - refund ) / ( glass - refund ) , 0 ) ; budget -= ans * ( glass - refund ) ; ans += budget / plastic ; System . out . println ( ans ) ; } else { System . out . println ( ( budget / plastic ) ) ; } } public static void main ( String [ ] args ) { int budget = 10 , plastic = 11 , glass = 9 , refund = 8 ; maxLitres ( budget , plastic , glass , refund ) ; } }
import java . util . * ; class GFG { static int leastValue ( int P , int A , int N , int a [ ] ) { int ans = - 1 ; float tmp = Float . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { float t = ( float ) ( P - a [ i ] * 0.006 ) ; if ( Math . abs ( t - A ) < tmp ) { tmp = Math . abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } public static void main ( String [ ] args ) { int N = 2 , P = 12 , A = 2005 ; int a [ ] = { 1000 , 2000 } ; System . out . println ( leastValue ( P , A , N , a ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int findPermutation ( int n ) { int len = ( int ) Math . ceil ( Math . log10 ( n ) ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( n % 2 != 0 ) { return n ; } else { n = ( n / 10 ) + ( n % 10 ) * ( int ) Math . pow ( 10 , len - i - 1 ) ; continue ; } } return - 1 ; } public static void main ( String args [ ] ) { int n = 132 ; System . out . println ( findPermutation ( n ) ) ; } }
class GFG { static boolean isSellingPossible ( int n , int a [ ] ) { int i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } public static void main ( String [ ] args ) { int a [ ] = { 25 , 25 , 50 , 100 } ; int n = a . length ; if ( isSellingPossible ( n , a ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
class GFG { static void printCoins ( int arr [ ] , int n ) { int oddSum = 0 ; for ( int i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; int evenSum = 0 ; for ( int i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; int start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( int i = start ; i < n ; i += 2 ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = arr1 . length ; printCoins ( arr1 , n ) ; System . out . println ( ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = arr2 . length ; printCoins ( arr2 , n ) ; System . out . println ( ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . length ; printCoins ( arr3 , n ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void finalPos ( String command , int n , int x , int y ) { int cup , cdown , cleft , cright ; int final_x , final_y ; cup = cdown = cleft = cright = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( command . charAt ( i ) == ' U ' ) cup ++ ; else if ( command . charAt ( i ) == ' D ' ) cdown ++ ; else if ( command . charAt ( i ) == ' L ' ) cleft ++ ; else if ( command . charAt ( i ) == ' R ' ) cright ++ ; } final_x = x + ( cright - cleft ) ; final_y = y + ( cdown - cup ) ; System . out . println ( " Final ▁ Position : ▁ " + " ( " + final_x + " , ▁ " + final_y + " ) " ) ; } public static void main ( String [ ] args ) { String command = " DDLRULL " ; int n = command . length ( ) ; int x = 3 , y = 4 ; finalPos ( command , n , x , y ) ; } }
import java . util . * ; class GFG { static void findLandB ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; Vector < Double > arr_pairs = new Vector < Double > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { arr_pairs . add ( ( double ) arr [ i ] ) ; i ++ ; } } double length = arr_pairs . get ( 0 ) ; double breadth = arr_pairs . get ( 1 ) ; double size = arr_pairs . size ( ) ; for ( int i = 2 ; i < size ; i ++ ) { if ( ( length / breadth + breadth / length ) > ( arr_pairs . get ( i ) / arr_pairs . get ( i - 1 ) + arr_pairs . get ( i - 1 ) / arr_pairs . get ( i ) ) ) { length = arr_pairs . get ( i ) ; breadth = arr_pairs . get ( i - 1 ) ; } } System . out . print ( ( int ) length + " , ▁ " + ( int ) breadth + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 2 , 2 , 5 , 6 , 5 , 6 , 7 , 2 } ; int n = arr . length ; findLandB ( arr , n ) ; } }
import java . io . * ; class GFG { static int smallestSumSubarr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = - arr [ i ] ; int sum_here = arr [ 0 ] , max_sum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum_here = Math . max ( sum_here + arr [ i ] , arr [ i ] ) ; max_sum = Math . max ( max_sum , sum_here ) ; } return ( - 1 ) * max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 4 , 2 , - 3 , - 1 , 7 , - 5 } ; int n = arr . length ; System . out . print ( " Smallest ▁ sum : ▁ " + smallestSumSubarr ( arr , n ) ) ; } }
import java . util . * ; class GFG { static class _pair { int first , second ; } ; static void printKPairs ( int a1 [ ] , int a2 [ ] , int size1 , int size2 , int k ) { if ( k > ( size2 * size1 ) ) { System . out . print ( "k pairs don't existNEW_LINE"); return ; } _pair _one = new _pair ( ) ; _pair _two = new _pair ( ) ; _one . first = _one . second = _two . first = _two . second = 0 ; int cnt = 0 ; while ( cnt < k ) { if ( _one . first == _two . second && _two . first == _one . second ) { if ( a1 [ _one . first ] < a2 [ _one . second ] ) { System . out . print ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( _one . second + 1 ) % size2 ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; _two . second = ( _two . second + 1 ) % size2 ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size2 ; } else { System . out . print ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( _one . first + 1 ) % size1 ; if ( _one . first == 0 ) _one . second = ( _one . second + 1 ) % size2 ; _two . first = ( _two . first + 1 ) % size2 ; if ( _two . first == 0 ) _two . second = ( _two . second + 1 ) % size1 ; } } else if ( a1 [ _one . first ] + a2 [ _one . second ] <= a2 [ _two . first ] + a1 [ _two . second ] ) { if ( a1 [ _one . first ] < a2 [ _one . second ] ) { System . out . print ( " [ " + a1 [ _one . first ] + " , ▁ " + a2 [ _one . second ] + " ] ▁ " ) ; _one . second = ( ( _one . second + 1 ) % size2 ) ; if ( _one . second == 0 ) _one . first = ( _one . first + 1 ) % size1 ; } else { System . out . print ( " [ " + a2 [ _one . second ] + " , ▁ " + a1 [ _one . first ] + " ] ▁ " ) ; _one . first = ( ( _one . first + 1 ) % size1 ) ; if ( _one . first == 0 ) _one . second = ( _one . second + 1 ) % size2 ; } } else if ( a1 [ _one . first ] + a2 [ _one . second ] > a2 [ _two . first ] + a1 [ _two . second ] ) { if ( a2 [ _two . first ] < a1 [ _two . second ] ) { System . out . print ( " [ " + a2 [ _two . first ] + " , ▁ " + a1 [ _two . second ] + " ] ▁ " ) ; _two . first = ( ( _two . first + 1 ) % size2 ) ; if ( _two . first == 0 ) _two . second = ( _two . second + 1 ) % size1 ; } else { System . out . print ( " [ " + a1 [ _two . second ] + " , ▁ " + a2 [ _two . first ] + " ] ▁ " ) ; _two . second = ( ( _two . second + 1 ) % size1 ) ; if ( _two . second == 0 ) _two . first = ( _two . first + 1 ) % size1 ; } } cnt ++ ; } } public static void main ( String [ ] args ) { int a1 [ ] = { 2 , 3 , 4 } ; int a2 [ ] = { 1 , 6 , 5 , 8 } ; int size1 = a1 . length ; int size2 = a2 . length ; int k = 4 ; printKPairs ( a1 , a2 , size1 , size2 , k ) ; } }
import java . util . * ; import java . lang . * ; public class GFG { static void printLargest ( int a [ ] , int n ) { int max = - 1 ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; while ( num > 0 ) { int r = num % 10 ; num = num / 10 ; if ( num == 0 ) { if ( max < r ) { max = r ; ind = i ; } } } } for ( int i = ind ; i < n ; i ++ ) System . out . print ( a [ i ] ) ; for ( int i = 0 ; i < ind ; i ++ ) System . out . print ( a [ i ] ) ; } public static void main ( String args [ ] ) { int a [ ] = { 54 , 546 , 548 , 60 } ; int n = a . length ; printLargest ( a , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void findOrderMRT ( int [ ] L , int n ) { Arrays . sort ( L ) ; System . out . print ( " Optimal ▁ order ▁ in ▁ which ▁ " + " programs ▁ are ▁ to ▁ be ▁ stored ▁ is : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( L [ i ] + " ▁ " ) ; System . out . println ( ) ; double MRT = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j <= i ; j ++ ) sum += L [ j ] ; MRT += sum ; } MRT /= n ; System . out . print ( " Minimum ▁ Retrieval ▁ Time " + " ▁ of ▁ this ▁ order ▁ is ▁ " + MRT ) ; } public static void main ( String [ ] args ) { int [ ] L = { 2 , 5 , 4 } ; int n = L . length ; findOrderMRT ( L , n ) ; } }
class GFG { static int maxProductSubset ( int a [ ] , int n ) { if ( n == 1 ) { return a [ 0 ] ; } int max_neg = Integer . MIN_VALUE ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 ) { count_neg ++ ; max_neg = Math . max ( max_neg , a [ i ] ) ; } prod = prod * a [ i ] ; } if ( count_zero == n ) { return 0 ; } if ( count_neg % 2 == 1 ) { if ( count_neg == 1 && count_zero > 0 && count_zero + count_neg == n ) { return 0 ; } prod = prod / max_neg ; } return prod ; } public static void main ( String [ ] args ) { int a [ ] = { - 1 , - 1 , - 2 , 4 , 3 } ; int n = a . length ; System . out . println ( maxProductSubset ( a , n ) ) ; } }
import java . util . * ; class GFG { static int find ( int [ ] arr , int pos , int m , boolean [ ] isRunning ) { int [ ] d = new int [ m + 1 ] ; for ( int i = m - 1 ; i > pos ; i -- ) { if ( isRunning [ arr [ i ] ] ) d [ arr [ i ] ] = i ; } int maxipos = 0 ; for ( int ele : d ) maxipos = Math . max ( ele , maxipos ) ; return maxipos ; } static int mincost ( int n , int m , int [ ] arr ) { @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] freqarr = new Vector [ m ] ; for ( int i = 0 ; i < freqarr . length ; i ++ ) freqarr [ i ] = new Vector < Integer > ( ) ; int [ ] newvec = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) freqarr [ m - 1 ] . add ( newvec [ i ] ) ; for ( int i = m - 2 ; i > 0 ; i -- ) { Vector < Integer > nv = new Vector < > ( ) ; nv = freqarr [ i + 1 ] ; nv . insertElementAt ( arr [ i + 1 ] + 1 , arr [ i + 1 ] ) ; freqarr [ i ] = nv ; } boolean [ ] isRunning = new boolean [ m + 1 ] ; int cost = 0 ; int truecount = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int ele = arr [ i ] ; if ( isRunning [ ele ] == true ) continue ; else { if ( truecount < n ) { cost += 1 ; truecount += 1 ; isRunning [ ele ] = true ; } else { int mini = 100000 ; int miniind = 0 ; for ( int j = 0 ; j <= m ; j ++ ) { if ( isRunning [ j ] && mini > freqarr [ i ] . get ( j ) ) { mini = freqarr [ i ] . get ( j ) ; miniind = j ; } } if ( mini == 0 ) { isRunning [ miniind ] = false ; isRunning [ ele ] = true ; cost += 1 ; } else { int farpos = find ( arr , i , m , isRunning ) ; isRunning [ arr [ farpos ] ] = false ; isRunning [ ele ] = true ; cost += 1 ; } } } } return cost ; } public static void main ( String [ ] args ) { int n1 = 3 ; int m1 = 6 ; int [ ] arr1 = { 1 , 2 , 1 , 3 , 4 , 1 } ; System . out . print ( mincost ( n1 , m1 , arr1 ) + "NEW_LINE"); int n2 = 2 ; int m2 = 6 ; int [ ] arr2 = { 1 , 2 , 1 , 3 , 2 , 1 } ; System . out . print ( mincost ( n2 , m2 , arr2 ) + "NEW_LINE"); int n3 = 3 ; int m3 = 31 ; int [ ] arr3 = { 7 , 11 , 17 , 10 , 7 , 10 , 2 , 9 , 2 , 18 , 8 , 10 , 20 , 10 , 3 , 20 , 17 , 17 , 17 , 1 , 15 , 10 , 8 , 3 , 3 , 18 , 13 , 2 , 10 , 10 , 11 } ; System . out . print ( mincost ( n3 , m3 - 8 , arr3 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static boolean possibility ( HashMap < Integer , Integer > m , int length , String s ) { int countodd = 0 ; for ( int i = 0 ; i < length ; i ++ ) { if ( m . get ( s . charAt ( i ) - '0' ) % 2 == 1 ) countodd ++ ; if ( countodd > 1 ) return false ; } return true ; } static void largestPalindrome ( String s ) { int l = s . length ( ) ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < l ; i ++ ) if ( m . containsKey ( s . charAt ( i ) - '0' ) ) m . put ( s . charAt ( i ) - '0' , m . get ( s . charAt ( i ) - '0' ) + 1 ) ; else m . put ( s . charAt ( i ) - '0' , 1 ) ; if ( possibility ( m , l , s ) == false ) { System . out . print ( " Palindrome ▁ cannot ▁ be ▁ formed " ) ; return ; } char [ ] largest = new char [ l ] ; int front = 0 ; for ( int i = 9 ; i >= 0 ; i -- ) { if ( m . containsKey ( i ) && m . get ( i ) % 2 == 1 ) { largest [ l / 2 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 1 ) ; while ( m . get ( i ) > 0 ) { largest [ front ] = ( char ) ( i + 48 ) ; largest [ l - front - 1 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 2 ) ; front ++ ; } } else { while ( m . containsKey ( i ) && m . get ( i ) > 0 ) { largest [ front ] = ( char ) ( i + 48 ) ; largest [ l - front - 1 ] = ( char ) ( i + 48 ) ; m . put ( i , m . get ( i ) - 2 ) ; front ++ ; } } } for ( int i = 0 ; i < l ; i ++ ) System . out . print ( largest [ i ] ) ; } public static void main ( String [ ] args ) { String s = "313551" ; largestPalindrome ( s ) ; } }
import java . util . * ; class GFG { public static void calcFiboTerms ( ArrayList < Integer > fiboterms , int k ) { int i = 3 , nextTerm = 0 ; fiboterms . add ( 0 ) ; fiboterms . add ( 1 ) ; fiboterms . add ( 1 ) ; while ( true ) { nextTerm = fiboterms . get ( i - 1 ) + fiboterms . get ( i - 2 ) ; if ( nextTerm > k ) return ; fiboterms . add ( nextTerm ) ; i ++ ; } } public static int fibMinTerms ( int k ) { ArrayList < Integer > fiboterms = new ArrayList < Integer > ( ) ; calcFiboTerms ( fiboterms , k ) ; int count = 0 , j = fiboterms . size ( ) - 1 ; while ( k > 0 ) { count += ( k / fiboterms . get ( j ) ) ; k %= ( fiboterms . get ( j ) ) ; j -- ; } return count ; } public static void main ( String [ ] args ) { int k = 17 ; System . out . println ( fibMinTerms ( k ) ) ; } }
import java . io . * ; class GFG { static void digitsNum ( int N ) { if ( N == 0 ) System . out . println ( "0" ) ; if ( N % 9 != 0 ) System . out . print ( ( N % 9 ) ) ; for ( int i = 1 ; i <= ( N / 9 ) ; ++ i ) System . out . print ( "9" ) ; for ( int i = 1 ; i <= N ; ++ i ) System . out . print ( "0" ) ; System . out . print ( " " ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( " The ▁ number ▁ is ▁ : ▁ " ) ; digitsNum ( N ) ; } }
import java . util . * ; class GFG { static int maxDifference ( int arr [ ] , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } M = Math . max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 4 , 5 , 2 , 10 } ; int N = arr . length ; int k = 2 ; System . out . println ( maxDifference ( arr , N , k ) ) ; } }
import java . math . * ; class GFG { static int minproduct ( int a [ ] , int b [ ] , int n , int k ) { int diff = 0 , res = 0 ; int temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int pro = a [ i ] * b [ i ] ; res = res + pro ; if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro > 0 && a [ i ] > 0 ) temp = ( a [ i ] - 2 * k ) * b [ i ] ; int d = Math . abs ( pro - temp ) ; if ( d > diff ) diff = d ; } return res - diff ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 , 5 , 4 } ; int b [ ] = { 3 , 4 , 2 , 3 , 2 } ; int n = 5 , k = 3 ; System . out . println ( minproduct ( a , b , n , k ) ) ; } }
import java . io . * ; class GFG { static int count ( int n ) { if ( n < 4 ) return - 1 ; int rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return - 1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return - 1 ; return ( n - 15 ) / 4 + 2 ; } return 0 ; } public static void main ( String [ ] args ) { int n = 90 ; System . out . println ( count ( n ) ) ; n = 143 ; System . out . println ( count ( n ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int policeThief ( char arr [ ] , int n , int k ) { int res = 0 ; ArrayList < Integer > thi = new ArrayList < Integer > ( ) ; ArrayList < Integer > pol = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == ' P ' ) pol . add ( i ) ; else if ( arr [ i ] == ' T ' ) thi . add ( i ) ; } int l = 0 , r = 0 ; while ( l < thi . size ( ) && r < pol . size ( ) ) { if ( Math . abs ( thi . get ( l ) - pol . get ( r ) ) <= k ) { res ++ ; l ++ ; r ++ ; } else if ( thi . get ( l ) < pol . get ( r ) ) l ++ ; else r ++ ; } return res ; } public static void main ( String args [ ] ) { int k , n ; char arr1 [ ] = new char [ ] { ' P ' , ' T ' , ' T ' , ' P ' , ' T ' } ; k = 2 ; n = arr1 . length ; System . out . println ( " Maximum ▁ thieves ▁ caught : ▁ " + policeThief ( arr1 , n , k ) ) ; char arr2 [ ] = new char [ ] { ' T ' , ' T ' , ' P ' , ' P ' , ' T ' , ' P ' } ; k = 2 ; n = arr2 . length ; System . out . println ( " Maximum ▁ thieves ▁ caught : ▁ " + policeThief ( arr2 , n , k ) ) ; char arr3 [ ] = new char [ ] { ' P ' , ' T ' , ' P ' , ' T ' , ' T ' , ' P ' } ; k = 3 ; n = arr3 . length ; System . out . println ( " Maximum ▁ thieves ▁ caught : ▁ " + policeThief ( arr3 , n , k ) ) ; } }
class GFG { static int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input > 0 unlock_code > 0 ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( input_digit - code_digit ) ) ; input /= 10 ; unlock_code /= 10 ; } return rotation ; } public static void main ( String [ ] args ) { int input = 28756 ; int unlock_code = 98234 ; System . out . println ( " Minimum ▁ Rotation ▁ = ▁ " + minRotation ( input , unlock_code ) ) ; } }
import java . io . * ; import java . util . Arrays ; public class GFG { static void preprocess ( int [ ] coin , int n ) { Arrays . sort ( coin ) ; for ( int i = 1 ; i <= n - 1 ; i ++ ) coin [ i ] += coin [ i - 1 ] ; } static int minCost ( int [ ] coin , int n , int k ) { int coins_needed = ( int ) Math . ceil ( 1.0 * n / ( k + 1 ) ) ; return coin [ coins_needed - 1 ] ; } static public void main ( String [ ] args ) { int [ ] coin = { 8 , 5 , 3 , 10 , 2 , 1 , 15 , 25 } ; int n = coin . length ; preprocess ( coin , n ) ; int k = 3 ; System . out . println ( minCost ( coin , n , k ) ) ; k = 7 ; System . out . println ( minCost ( coin , n , k ) ) ; } }
public class GFG { static void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( wstIdx == - 1 ) wstIdx = j ; else if ( blockSize [ wstIdx ] < blockSize [ j ] ) wstIdx = j ; } } if ( wstIdx != - 1 ) { allocation [ i ] = wstIdx ; blockSize [ wstIdx ] -= processSize [ i ] ; } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; worstFit ( blockSize , m , processSize , n ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static int minimumCostOfBreaking ( Integer X [ ] , Integer Y [ ] , int m , int n ) { int res = 0 ; Arrays . sort ( X , Collections . reverseOrder ( ) ) ; Arrays . sort ( Y , Collections . reverseOrder ( ) ) ; int hzntl = 1 , vert = 1 ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( X [ i ] > Y [ j ] ) { res += X [ i ] * vert ; hzntl ++ ; i ++ ; } else { res += Y [ j ] * hzntl ; vert ++ ; j ++ ; } } int total = 0 ; while ( i < m ) total += X [ i ++ ] ; res += total * vert ; total = 0 ; while ( j < n ) total += Y [ j ++ ] ; res += total * hzntl ; return res ; } public static void main ( String arg [ ] ) { int m = 6 , n = 4 ; Integer X [ ] = { 2 , 1 , 3 , 1 , 4 } ; Integer Y [ ] = { 4 , 1 , 2 } ; System . out . print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) ; } }
class GFG { static int maximumSum ( int arr [ ] , int n , int k ) { for ( int i = 1 ; i <= k ; i ++ ) { int min = + 2147483647 ; int index = - 1 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] < min ) { min = arr [ j ] ; index = j ; } } if ( min == 0 ) break ; arr [ index ] = - arr [ index ] ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } public static void main ( String arg [ ] ) { int arr [ ] = { - 2 , 0 , 5 , - 1 , 2 } ; int k = 4 ; int n = arr . length ; System . out . print ( maximumSum ( arr , n , k ) ) ; } }
import java . util . Arrays ; public class GFG { static int sol ( int arr [ ] , int k ) { Arrays . sort ( arr ) ; int sum = 0 ; int i = 0 ; while ( k > 0 ) { if ( arr [ i ] >= 0 ) k = 0 ; else { arr [ i ] = ( - 1 ) * arr [ i ] ; k = k - 1 ; } i ++ ; } for ( int j = 0 ; j < arr . length ; j ++ ) { sum += arr [ j ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 0 , 5 , - 1 , 2 } ; System . out . println ( sol ( arr , 4 ) ) ; } }
import java . util . * ; class GFG { static int sumArray ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } return sum ; } static int maximizeSum ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( k != 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; continue ; } break ; } if ( i == n ) i -- ; if ( k == 0 k % 2 == 0 ) { return sumArray ( arr , n ) ; } if ( i != 0 && Math . abs ( arr [ i ] ) >= Math . abs ( arr [ i - 1 ] ) ) { i -- ; } arr [ i ] *= - 1 ; return sumArray ( arr , n ) ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 4 ; int arr [ ] = { - 3 , - 2 , - 1 , 5 , 6 } ; System . out . print ( maximizeSum ( arr , n , k ) ) ; } }
class GFG { static int firstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] ) { bin_rem [ j ] = bin_rem [ j ] - weight [ i ] ; break ; } } if ( j == res ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } } return res ; } public static void main ( String [ ] args ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ First ▁ Fit ▁ : ▁ " + firstFit ( weight , n , c ) ) ; } }
import java . io . * ; class GFG { static int getMinOperations ( int A , int B , int prevA , int prevB , int dp [ ] [ ] ) { if ( A == prevA && B == prevB ) { return Integer . MAX_VALUE ; } if ( A == 0 && B == 0 ) { return 0 ; } if ( dp [ A ] [ B ] != - 1 ) { return dp [ A ] [ B ] ; } int ans1 = getMinOperations ( A / 2 , B , A , B , dp ) ; if ( ans1 != Integer . MAX_VALUE ) { ans1 += 1 ; } int ans2 = getMinOperations ( A , B / 2 , A , B , dp ) ; if ( ans2 != Integer . MAX_VALUE ) { ans2 += 1 ; } int ans3 = getMinOperations ( ( int ) Math . sqrt ( A * B ) , ( int ) Math . sqrt ( A * B ) , A , B , dp ) ; if ( ans3 != Integer . MAX_VALUE ) { ans3 += 2 ; } return dp [ A ] [ B ] = Math . min ( ans1 , Math . min ( ans2 , ans3 ) ) ; } public static void main ( String [ ] args ) { int A = 53 , B = 16 ; int mx = Math . max ( A , B ) ; int dp [ ] [ ] = new int [ mx + 1 ] [ mx + 1 ] ; for ( int i = 0 ; i <= mx ; i ++ ) { for ( int j = 0 ; j <= mx ; j ++ ) dp [ i ] [ j ] = - 1 ; } System . out . println ( getMinOperations ( A , B , - 1 , - 1 , dp ) ) ; } }
import java . io . * ; class GFG { static int maxDiffSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int maxVal = arr [ i ] , minVal = arr [ i ] ; for ( int j = i ; j >= 0 ; j -- ) { minVal = Math . min ( minVal , arr [ j ] ) ; maxVal = Math . max ( maxVal , arr [ j ] ) ; if ( j - 1 >= 0 ) dp [ i ] = Math . max ( dp [ i ] , maxVal - minVal + dp [ j - 1 ] ) ; else dp [ i ] = Math . max ( dp [ i ] , maxVal - minVal ) ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 8 , 1 , 7 , 9 , 2 } ; int N = arr . length ; System . out . print ( maxDiffSum ( arr , N ) ) ; } }
class GFG { public static int MOD = 1000000007 ; public static long modPow ( long x , long y ) { long r = 1 , a = x ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) { r = ( r * a ) % MOD ; } a = ( a * a ) % MOD ; y /= 2 ; } return r ; } public static long modInverse ( long x ) { return modPow ( x , MOD - 2 ) ; } public static long modDivision ( long p , long q ) { return ( p * modInverse ( q ) ) % MOD ; } public static long C ( long n , int k ) { if ( k > n ) { return 0 ; } long p = 1 , q = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { q = ( q * i ) % MOD ; p = ( p * ( n - i + 1 ) ) % MOD ; } return modDivision ( p , q ) ; } public static long countArrays ( int N , int K ) { long res = 1 ; for ( int p = 2 ; p <= K / p ; p ++ ) { int c = 0 ; while ( K % p == 0 ) { K /= p ; c ++ ; } res = ( res * C ( N - 1 + c , c ) ) % MOD ; } if ( N > 1 ) { res = ( res * N ) % MOD ; } return res ; } public static void main ( String args [ ] ) { int N = 3 , K = 5 ; System . out . println ( countArrays ( N , K ) ) ; } }
import java . util . Arrays ; class GFG { public static int minSessions ( int [ ] codeTime , int [ ] [ ] dp , int ones , int n , int mask , int currTime , int WorkingSessionTime ) { if ( currTime > WorkingSessionTime ) return Integer . MAX_VALUE ; if ( mask == ones ) return 1 ; if ( dp [ mask ] [ currTime ] != - 1 ) return dp [ mask ] [ currTime ] ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { int inc = minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , currTime + codeTime [ i ] , WorkingSessionTime ) ; int inc_next = 1 + minSessions ( codeTime , dp , ones , n , mask | ( 1 << i ) , codeTime [ i ] , WorkingSessionTime ) ; ans = Math . min ( ans , Math . min ( inc , inc_next ) ) ; } } return dp [ mask ] [ currTime ] = ans ; } public static int solve ( int [ ] codeTime , int n , int WorkingSessionTime ) { int [ ] [ ] dp = new int [ ( 1 << 14 ) ] [ ] ; for ( int i = 0 ; i < 1 << 14 ; i ++ ) { dp [ i ] = new int [ 15 ] ; Arrays . fill ( dp [ i ] , - 1 ) ; } int ones = ( 1 << n ) - 1 ; int ans = minSessions ( codeTime , dp , ones , n , 0 , 0 , WorkingSessionTime ) ; if ( WorkingSessionTime < 6 ) { if ( ans % 2 == 0 ) ans = ans / 2 ; else ans = ( ans / 2 ) + 1 ; } return ans ; } public static void main ( String args [ ] ) { int [ ] codeTime = { 1 , 2 , 3 , 1 , 1 , 3 } ; int n = codeTime . length ; int WorkingSessionTime = 4 ; System . out . println ( solve ( codeTime , n , WorkingSessionTime ) ) ; } }
import java . io . * ; class GFG { static final int MAXN = 100005 ; static final int dp [ ] [ ] = new int [ MAXN ] [ 10 ] ; static int solve ( int len , int gap ) { if ( dp [ len ] [ gap ] != - 1 ) { return dp [ len ] [ gap ] ; } if ( len == 0 gap == 0 ) { return 1 ; } if ( gap < 0 ) { return 0 ; } int ans = 0 ; for ( int i = 0 ; i <= gap ; i ++ ) { ans += solve ( len - 1 , gap - i ) ; } return dp [ len ] [ gap ] = ans ; } static int countValidStrings ( String S ) { for ( int i = 0 ; i < MAXN ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int N = S . length ( ) ; int L = 1 , R = 9 ; int cnt = 0 ; int ans = 1 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) != ' ? ' ) { R = S . charAt ( i ) - '0' ; ans *= solve ( cnt , R - L ) ; L = R ; R = 9 ; cnt = 0 ; } else { cnt ++ ; } } ans *= solve ( cnt , R - L ) ; return ans ; } public static void main ( String [ ] args ) { String S = "1 ? ? ?2" ; System . out . println ( countValidStrings ( S ) ) ; } }
import java . io . * ; class GFG { static String KthString ( int A , int B , long K , int [ ] [ ] dp ) { if ( A == 0 ) { String ans = " " ; for ( int i = 0 ; i < B ; i ++ ) { ans += '1' ; } return ans ; } if ( B == 0 ) { String ans = " " ; for ( int i = 0 ; i < A ; i ++ ) { ans += '0' ; } return ans ; } if ( K <= dp [ A - 1 ] [ B ] ) { return "0" + KthString ( A - 1 , B , K , dp ) ; } else { return "1" + KthString ( A , B - 1 , K - dp [ A - 1 ] [ B ] , dp ) ; } } static int KthStringUtil ( int A , int B , int K ) { int [ ] [ ] dp = new int [ A + 1 ] [ B + 1 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i <= A ; ++ i ) { for ( int j = 0 ; j <= B ; ++ j ) { if ( i > 0 ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; } if ( j > 0 ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } System . out . println ( KthString ( A , B , K , dp ) ) ; return 0 ; } public static void main ( String [ ] args ) { int A = 3 , B = 3 , K = 7 ; KthStringUtil ( A , B , K ) ; } }
import java . util . * ; class GFG { static int combinationsPossible ( int N , int idx , int prev , int val , int K , int [ ] [ ] [ ] dp ) { if ( val > K ) { return 0 ; } if ( dp [ val ] [ idx ] [ prev ] != - 1 ) { return dp [ val ] [ idx ] [ prev ] ; } if ( idx == N - 1 ) { if ( val == K ) { return 1 ; } return 0 ; } int ans = 0 ; if ( prev == 1 ) { ans += combinationsPossible ( N , idx + 1 , 1 , val + 1 , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } else { ans += combinationsPossible ( N , idx + 1 , 1 , val , K , dp ) ; ans += combinationsPossible ( N , idx + 1 , 0 , val , K , dp ) ; } dp [ val ] [ idx ] [ prev ] = ans ; return dp [ val ] [ idx ] [ prev ] ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 3 ; int [ ] [ ] [ ] dp = new int [ K + 1 ] [ N + 1 ] [ 2 ] ; for ( int i = 0 ; i < K + 1 ; i ++ ) { for ( int j = 0 ; j < N + 1 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } System . out . print ( combinationsPossible ( N , 0 , 0 , 0 , K , dp ) + combinationsPossible ( N , 0 , 1 , 0 , K , dp ) ) ; } }
class GFG { static int getMin ( int x , int y , int z ) { return Math . min ( Math . min ( x , y ) , z ) ; } static int editDistance ( String str1 , String str2 , int m , int n ) { int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( str1 . charAt ( i - 1 ) == str2 . charAt ( j - 1 ) ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else { dp [ i ] [ j ] = 1 + getMin ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } } } return dp [ m ] [ n ] ; } static void minimumSteps ( String S , int N ) { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { String S1 = S . substring ( 0 , i ) ; String S2 = S . substring ( i ) ; int count = editDistance ( S1 , S2 , S1 . length ( ) , S2 . length ( ) ) ; ans = Math . min ( ans , count ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S = " aabb " ; int N = S . length ( ) ; minimumSteps ( S , N ) ; } }
public class GFG { static int countOfWays ( int arr [ ] , int N ) { int pre [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { pre [ i + 1 ] = pre [ i ] + arr [ i ] ; } int dp [ ] [ ] = new int [ N + 2 ] [ N + 2 ] ; dp [ 1 ] [ 0 ] ++ ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N ; j >= 1 ; j -- ) { dp [ j + 1 ] [ pre [ i + 1 ] % ( j + 1 ) ] += dp [ j ] [ pre [ i + 1 ] % j ] ; if ( i == N - 1 ) { ans += dp [ j ] [ pre [ i + 1 ] % j ] ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( countOfWays ( arr , N ) ) ; } }
import java . util . * ; import javax . management . Query ; class GFG { static final int MAX = 500 ; static int [ ] [ ] lookup = new int [ MAX ] [ MAX ] ; static class Query { int L , R ; public Query ( int l , int r ) { super ( ) ; L = l ; R = r ; } } ; static void preprocess ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) lookup [ i ] [ 0 ] = i ; for ( int j = 1 ; ( 1 << j ) <= n ; j ++ ) { for ( int i = 0 ; ( i + ( 1 << j ) - 1 ) < n ; i ++ ) { if ( arr [ lookup [ i ] [ j - 1 ] ] < arr [ lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ] ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] ; else lookup [ i ] [ j ] = lookup [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ; } } } static int query ( int arr [ ] , int L , int R ) { int j = ( int ) Math . log ( R - L + 1 ) ; if ( arr [ lookup [ L ] [ j ] ] <= arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ) return arr [ lookup [ L ] [ j ] ] ; else return arr [ lookup [ R - ( 1 << j ) + 1 ] [ j ] ] ; } static void Min_difference ( int arr [ ] , int n , Query q [ ] , int m ) { preprocess ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; System . out . println ( query ( arr , L , R - 1 ) ) ; } } static void minimumDifference ( int arr [ ] , Query q [ ] , int N , int m ) { int [ ] diff = new int [ N ] ; for ( int i = 0 ; i < N - 1 ; i ++ ) diff [ i ] = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; Min_difference ( diff , N - 1 , q , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 1 , 8 , 3 , 4 } ; int N = arr . length ; Query Q [ ] = { new Query ( 0 , 3 ) , new Query ( 1 , 5 ) , new Query ( 4 , 5 ) } ; int M = Q . length ; minimumDifference ( arr , Q , N , M ) ; } }
import java . io . * ; class GFG { static int minimumOperations ( int N ) { int [ ] dp = new int [ N + 1 ] ; int i ; for ( i = 0 ; i <= N ; i ++ ) { dp [ i ] = ( int ) 1e9 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i <= N ; i ++ ) { if ( dp [ i ] == ( int ) 1e9 ) continue ; if ( i * 5 <= N ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= N ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } if ( dp [ N ] == 1e9 ) return - 1 ; return dp [ N ] ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . println ( minimumOperations ( N ) ) ; } }
import java . util . Arrays ; class GFG { public static long MOD = 1000000007l ; public static long numTilings ( int N ) { if ( N < 3 ) { return N ; } long [ ] [ ] dp = new long [ N + 1 ] [ 3 ] ; for ( long [ ] row : dp ) { Arrays . fill ( row , 0 ) ; } dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = dp [ 1 ] [ 2 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 2 ] [ 0 ] + dp [ i - 2 ] [ 1 ] + dp [ i - 2 ] [ 2 ] ) % MOD ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 2 ] ) % MOD ; dp [ i ] [ 2 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) % MOD ; } return dp [ N ] [ 0 ] ; } public static void main ( String args [ ] ) { int N = 3 ; System . out . println ( numTilings ( N ) ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Integer > dp = new HashMap < Integer , Integer > ( ) ; static int countStrings ( int N ) { if ( N == 1 ) return 0 ; if ( dp . containsKey ( N ) ) return dp . get ( N ) ; int ret = 0 ; for ( int div = 1 ; div <= Math . sqrt ( N ) ; div ++ ) { if ( N % div == 0 ) { ret += ( 1 << div ) - countStrings ( div ) ; int div2 = N / div ; if ( div2 != div && div != 1 ) ret += ( 1 << div2 ) - countStrings ( div2 ) ; } } dp . put ( N , ret ) ; return ret ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( countStrings ( N ) ) ; } }
import java . io . * ; class GFG { static int MaxProfit ( int arr [ ] , int n , int transactionFee ) { int buy = - arr [ 0 ] ; int sell = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int temp = buy ; buy = Math . max ( buy , sell - arr [ i ] ) ; sell = Math . max ( sell , temp + arr [ i ] - transactionFee ) ; } return Math . max ( sell , buy ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 7 , 2 , 8 , 4 } ; int n = arr . length ; int transactionFee = 2 ; System . out . println ( MaxProfit ( arr , n , transactionFee ) ) ; } }
class GFG { static int lcsubtr ( char a [ ] , char b [ ] , int length1 , int length2 ) { int dp [ ] [ ] = new int [ length1 + 1 ] [ length2 + 1 ] ; int max = 0 ; for ( int i = 0 ; i <= length1 ; ++ i ) { for ( int j = 0 ; j <= length2 ; ++ j ) { if ( i == 0 j == 0 ) { dp [ i ] [ j ] = 0 ; } else if ( a [ i - 1 ] == b [ j - 1 ] ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + 1 ; max = Math . max ( dp [ i ] [ j ] , max ) ; } else { dp [ i ] [ j ] = 0 ; } } } return max ; } public static void main ( String [ ] args ) { String m = "0110" ; String n = "1101" ; char m1 [ ] = m . toCharArray ( ) ; char m2 [ ] = n . toCharArray ( ) ; System . out . println ( lcsubtr ( m1 , m2 , m1 . length , m2 . length ) ) ; } }
import java . io . * ; class GFG { static void findMaxJumps ( int arr [ ] , int N ) { int dp [ ] = new int [ N ] ; int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] ; int j = i + arr [ i ] ; if ( j < N ) { dp [ i ] = dp [ i ] + dp [ j ] ; } ans = Math . max ( ans , dp [ i ] ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 7 , 9 } ; int N = arr . length ; findMaxJumps ( arr , N ) ; } }
import java . util . * ; public class MyClass { static int [ ] [ ] [ ] dp = new int [ 100 ] [ 10 ] [ 2 ] ; static int solve ( int i , int n , int prev , int sign ) { if ( i == n ) { return 1 ; } int val = dp [ i ] [ prev ] [ sign ] ; if ( val != - 1 ) return val ; val = 0 ; if ( i == 0 ) { for ( int digit = ( n == 1 ? 0 : 1 ) ; digit <= 9 ; ++ digit ) { val += solve ( i + 1 , n , digit , sign ) ; } } else if ( i == 1 ) { for ( int digit = 0 ; digit <= 9 ; ++ digit ) { if ( digit != prev ) { val += solve ( i + 1 , n , digit , ( ( digit > prev ) ? 1 : 0 ) ) ; } } } else { if ( sign == 1 ) { for ( int digit = prev - 1 ; digit >= 0 ; -- digit ) { val += solve ( i + 1 , n , digit , 0 ) ; } } else { for ( int digit = prev + 1 ; digit <= 9 ; ++ digit ) { val += solve ( i + 1 , n , digit , 1 ) ; } } } return val ; } static void countNdigitNumber ( int N ) { for ( int [ ] [ ] row : dp ) { for ( int [ ] rowColumn : row ) { Arrays . fill ( rowColumn , - 1 ) ; } } System . out . println ( solve ( 0 , N , 0 , 0 ) ) ; } public static void main ( String args [ ] ) { int N = 3 ; countNdigitNumber ( N ) ; } }
import java . util . * ; class GFG { static int Count ( int N , int L , int R ) { int [ ] [ ] dp = new int [ N ] [ R - L + 1 ] ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } for ( int i = 1 ; i < dp [ 0 ] . length ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; } ans = dp [ 0 ] [ R - L ] ; for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < dp [ 0 ] . length ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; } ans += dp [ i ] [ R - L ] ; } return ans ; } public static void main ( String args [ ] ) { int N = 3 ; int L = 6 ; int R = 9 ; System . out . println ( Count ( N , L , R ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int largestCommonSubarray ( int [ ] [ ] arr , int n , int m ) { int dp [ ] [ ] = new int [ n ] [ m + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { dp [ i ] [ arr [ i ] [ j ] ] = j ; } } int ans = 1 ; int len = 1 ; for ( int i = 1 ; i < m ; i ++ ) { boolean check = true ; for ( int j = 1 ; j < n ; j ++ ) { if ( dp [ j ] [ arr [ 0 ] [ i - 1 ] ] + 1 != dp [ j ] [ arr [ 0 ] [ i ] ] ) { check = false ; break ; } } if ( check ) { len ++ ; ans = Math . max ( ans , len ) ; } else { len = 1 ; } } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int m = 5 ; int [ ] [ ] arr = { { 4 , 5 , 1 , 2 , 3 , 6 , 7 } , { 1 , 2 , 4 , 5 , 7 , 6 , 3 } , { 2 , 7 , 3 , 4 , 5 , 1 , 6 } } ; int N = arr . length ; int M = arr [ 0 ] . length ; System . out . println ( largestCommonSubarray ( arr , N , M ) ) ; } }
class GFG { public static int MaximumSum ( int a [ ] , int b [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; dp [ 0 ] [ 0 ] = a [ 0 ] ; dp [ 0 ] [ 1 ] = b [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + a [ i ] ; dp [ i ] [ 1 ] = Math . max ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; if ( i - 2 >= 0 ) { dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , Math . max ( dp [ i - 2 ] [ 0 ] , dp [ i - 2 ] [ 1 ] ) + b [ i ] ) ; } else { dp [ i ] [ 1 ] = Math . max ( dp [ i ] [ 1 ] , b [ i ] ) ; } } return Math . max ( dp [ n - 1 ] [ 0 ] , dp [ n - 1 ] [ 1 ] ) ; } public static void main ( String args [ ] ) { int A [ ] = { 10 , 1 , 10 , 10 } ; int B [ ] = { 5 , 50 , 1 , 5 } ; int N = A . length ; System . out . println ( MaximumSum ( A , B , N ) ) ; } }
public class GFG { static int MAX = 30 ; static void findNumString ( int X , int Y , int dp [ ] [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; } } dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i <= X ; ++ i ) { for ( int j = 0 ; j <= Y ; ++ j ) { if ( i > 0 ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; } if ( j > 0 ) { dp [ i ] [ j ] += dp [ i ] [ j - 1 ] ; } } } } static String kthString ( int X , int Y , int K , int dp [ ] [ ] ) { String x1 = " " ; String y1 = " " ; for ( int i = 0 ; i < Y ; i ++ ) { x1 += ' b ' ; } for ( int i = 0 ; i < X ; i ++ ) { y1 += ' a ' ; } if ( X == 0 ) return x1 ; if ( Y == 0 ) return y1 ; if ( K <= dp [ X - 1 ] [ Y ] ) { return ( " a " + kthString ( X - 1 , Y , K , dp ) ) ; } else { return ( " b " + kthString ( X , Y - 1 , K - dp [ X - 1 ] [ Y ] , dp ) ) ; } } static void kthStringUtil ( int X , int Y , int K ) { int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; findNumString ( X , Y , dp ) ; System . out . println ( kthString ( X , Y , K , dp ) ) ; } public static void main ( String args [ ] ) { int X = 4 ; int Y = 3 ; int K = 4 ; kthStringUtil ( X , Y , K ) ; } }
import java . io . * ; class GFG { static int maximumTip ( int [ ] arr1 , int [ ] arr2 , int n , int x , int y ) { if ( n == 0 ) return 0 ; if ( x != 0 && y != 0 ) return Math . max ( arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) , arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ) ; if ( y == 0 ) return arr1 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x - 1 , y ) ; else return arr2 [ n - 1 ] + maximumTip ( arr1 , arr2 , n - 1 , x , y - 1 ) ; } public static void main ( String [ ] args ) { int N = 5 ; int X = 3 ; int Y = 3 ; int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = { 5 , 4 , 3 , 2 , 1 } ; System . out . println ( maximumTip ( A , B , N , X , Y ) ) ; } }
import java . util . ArrayList ; class GFG { public static int minimumDeletions ( int A [ ] , int N ) { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i < ( 1 << N ) ; i ++ ) { ArrayList < Integer > temp = new ArrayList < Integer > ( ) ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) { temp . add ( A [ j ] ) ; } } int flag = 0 ; for ( int j = 1 ; j < temp . size ( ) ; j ++ ) if ( temp . get ( j ) < temp . get ( j - 1 ) ) flag = 1 ; for ( int j = 1 ; j < temp . size ( ) - 1 ; j ++ ) if ( temp . get ( j ) - temp . get ( j - 1 ) > temp . get ( j + 1 ) - temp . get ( j ) ) flag = 1 ; if ( flag == 0 ) { ans = Math . min ( ans , N - ( int ) temp . size ( ) ) ; } } return ans ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . length ; System . out . println ( minimumDeletions ( A , N ) ) ; } }
import java . io . * ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int A [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ MAX ] ; int [ ] [ ] pref = new int [ N ] [ MAX ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; pref [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; dp [ i ] = Math . max ( dp [ i ] , pref [ j ] + 1 ) ; } } pref [ i ] [ 0 ] = dp [ i ] [ 0 ] ; for ( int j = 1 ; j < MAX ; j ++ ) pref [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , pref [ i ] [ j - 1 ] ) ; } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . max ( maxSetSize , dp [ N - 1 ] [ i ] ) ; return N - maxSetSize ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . length ; System . out . println ( minimumDeletions ( A , N ) ) ; } }
import java . util . * ; class GFG { static int maximumSum ( int [ ] A , int N , int K , int D ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) { int sum = 0 ; int c = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( ( i >> j & 1 ) != 0 ) { sum += A [ j ] ; c ++ ; } } if ( sum % D == 0 && c <= K ) ans = Math . max ( ans , sum ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 5 , K = 3 , D = 7 ; int [ ] A = { 1 , 11 , 5 , 5 , 18 } ; System . out . print ( maximumSum ( A , N , K , D ) ) ; } }
class GFG { public static String longestSubsequence ( String X , String Y ) { int n = X . length ( ) ; int m = Y . length ( ) ; int [ ] [ ] mat = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < m + 1 ; j ++ ) { if ( i == 0 j == 0 ) mat [ i ] [ j ] = 0 ; } } for ( int i = 1 ; i < n + 1 ; i ++ ) { for ( int j = 1 ; j < m + 1 ; j ++ ) { if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { mat [ i ] [ j ] = 1 + mat [ i - 1 ] [ j - 1 ] ; } else { mat [ i ] [ j ] = mat [ i - 1 ] [ j ] ; } } } int len = 0 , col = 0 ; for ( int i = 0 ; i < m + 1 ; i ++ ) { if ( mat [ n ] [ i ] > len ) { len = mat [ n ] [ i ] ; col = i ; } } String res = " " ; int i = n ; int j = col ; while ( len > 0 ) { if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { res = X . charAt ( i - 1 ) + res ; i -- ; j -- ; len -- ; } else { i -- ; } } return res ; } public static void main ( String args [ ] ) { String X = " ABCD " ; String Y = " ACDBDCD " ; System . out . println ( longestSubsequence ( X , Y ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static final int sz = 1001 ; static boolean ok [ ] [ ] = new boolean [ sz ] [ sz ] ; static int dp [ ] [ ] = new int [ sz ] [ sz ] ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minInvalidAreaUtil ( int l , int b ) { if ( dp [ l ] [ b ] == - 1 ) { if ( ok [ l ] [ b ] ) { return dp [ l ] [ b ] = 0 ; } int ans = l * b ; for ( int i = 1 ; i < b ; i ++ ) { ans = Math . min ( ans , minInvalidAreaUtil ( l , i ) + minInvalidAreaUtil ( l , b - i ) ) ; } for ( int i = 1 ; i < l ; i ++ ) { ans = Math . min ( ans , minInvalidAreaUtil ( i , b ) + minInvalidAreaUtil ( l - i , b ) ) ; } dp [ l ] [ b ] = ans ; } return dp [ l ] [ b ] ; } static void minInvalidArea ( int N , int M , Vector < pair > dimensions ) { int K = dimensions . size ( ) ; for ( int i = 0 ; i < K ; i ++ ) { ok [ dimensions . elementAt ( i ) . first ] [ dimensions . elementAt ( i ) . second ] = true ; ok [ dimensions . elementAt ( i ) . second ] [ dimensions . elementAt ( i ) . first ] = true ; } for ( int i = 0 ; i < sz ; i ++ ) { for ( int j = 0 ; j < sz ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int minArea = minInvalidAreaUtil ( N , M ) ; System . out . println ( minArea ) ; } public static void main ( String [ ] args ) { int N = 10 , M = 10 ; Vector < pair > dimensions = new Vector < > ( ) ; dimensions . add ( new pair ( 3 , 5 ) ) ; minInvalidArea ( N , M , dimensions ) ; } }
import java . io . * ; import java . util . * ; import java . util . List ; import com . google . common . collect . * ; class GFG { static void countPairs ( int [ ] arr ) { int N = arr . length ; int ans = 0 ; ListMultimap < Integer , Integer > M = ArrayListMultimap . create ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { M . put ( arr [ i ] , i ) ; } for ( var it : M . keySet ( ) ) { List < Integer > v = M . get ( it ) ; int sum = 0 ; for ( int j : v ) { ans += sum * ( N - j ) ; sum += j + 1 ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 1 } ; countPairs ( arr ) ; } }
import java . util . * ; class GFG { static int start [ ] [ ] = new int [ 3 ] [ 3 ] ; static int ending [ ] [ ] = new int [ 3 ] [ 3 ] ; static void calculateStart ( int n , int m ) { for ( int i = 1 ; i < m ; ++ i ) { start [ 0 ] [ i ] += start [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; ++ i ) { start [ i ] [ 0 ] += start [ i - 1 ] [ 0 ] ; } for ( int i = 1 ; i < n ; ++ i ) { for ( int j = 1 ; j < m ; ++ j ) { start [ i ] [ j ] += Math . max ( start [ i - 1 ] [ j ] , start [ i ] [ j - 1 ] ) ; } } } static void calculateEnd ( int n , int m ) { for ( int i = n - 2 ; i >= 0 ; -- i ) { ending [ i ] [ m - 1 ] += ending [ i + 1 ] [ m - 1 ] ; } for ( int i = m - 2 ; i >= 0 ; -- i ) { ending [ n - 1 ] [ i ] += ending [ n - 1 ] [ i + 1 ] ; } for ( int i = n - 2 ; i >= 0 ; -- i ) { for ( int j = m - 2 ; j >= 0 ; -- j ) { ending [ i ] [ j ] += Math . max ( ending [ i + 1 ] [ j ] , ending [ i ] [ j + 1 ] ) ; } } } static void maximumPathSum ( int mat [ ] [ ] , int n , int m , int q , int coordinates [ ] [ ] ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { start [ i ] [ j ] = mat [ i ] [ j ] ; ending [ i ] [ j ] = mat [ i ] [ j ] ; } } calculateStart ( n , m ) ; calculateEnd ( n , m ) ; int ans = 0 ; for ( int i = 0 ; i < q ; ++ i ) { int X = coordinates [ i ] [ 0 ] - 1 ; int Y = coordinates [ i ] [ 1 ] - 1 ; ans = Math . max ( ans , start [ X ] [ Y ] + ending [ X ] [ Y ] - mat [ X ] [ Y ] ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int N = 3 ; int M = 3 ; int Q = 2 ; int coordinates [ ] [ ] = { { 1 , 2 } , { 2 , 2 } } ; maximumPathSum ( mat , N , M , Q , coordinates ) ; } }
import java . util . * ; class GFG { static int countAlternatingSubstrings ( String S , int N ) { int [ ] [ ] dp = new int [ 2 ] [ N ] ; for ( int i = 0 ; i < 2 ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { if ( S . charAt ( i ) == '1' ) dp [ 1 ] [ i ] = 1 ; else dp [ 0 ] [ i ] = 1 ; } else { if ( S . charAt ( i ) == '0' ) dp [ 0 ] [ i ] = 1 + dp [ 1 ] [ i + 1 ] ; else dp [ 1 ] [ i ] = 1 + dp [ 0 ] [ i + 1 ] ; } } int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { ans += Math . max ( dp [ 0 ] [ i ] , dp [ 1 ] [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { String S = "0010" ; int N = S . length ( ) ; System . out . print ( countAlternatingSubstrings ( S , N ) ) ; } }
import java . util . * ; class GFG { static int solve ( int A [ ] , int N , int K ) { int sum = 0 ; int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( A [ i ] == 0 ) c ++ ; } if ( ( K > sum ) || ( ( ( sum + K ) % 2 ) != 0 ) ) return 0 ; sum = ( sum + K ) / 2 ; int dp [ ] [ ] = new int [ N + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= sum ; i ++ ) dp [ 0 ] [ i ] = 0 ; for ( int i = 0 ; i <= N ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( ( A [ i - 1 ] <= j ) && ( A [ i - 1 ] != 0 ) ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - A [ i - 1 ] ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } return dp [ N ] [ sum ] + ( int ) Math . pow ( 2 , c ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 1 , 2 , 3 } ; int N = A . length ; int K = 3 ; System . out . print ( solve ( A , N , K ) ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] [ ] [ ] dp = new int [ 100 ] [ 2 ] [ 10 ] [ 2 ] ; static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int noncoprimeCount ( int i , int N , char [ ] S , int bound , int prev , int allZeros ) { if ( i == N ) return 1 ; int val = dp [ i ] [ bound ] [ prev ] [ allZeros ] ; if ( val != - 1 ) return val ; int cnt = 0 ; for ( int j = 0 ; j <= ( bound != 0 ? ( S [ i ] - '0' ) : 9 ) ; ++ j ) { if ( ( __gcd ( j , prev ) != 1 ) || ( i == 0 ) allZeros == 1 ) { cnt += noncoprimeCount ( i + 1 , N , S , bound != 0 & ( j == ( S [ i ] - '0' ) ) ? 1 : 0 , j , ( allZeros != 0 & ( j == 0 ) ) ? 1 : 0 ) ; } } return val = cnt ; } static void noncoprimeCountUtil ( int R ) { String S = String . valueOf ( R ) ; int N = S . length ( ) ; for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int k = 0 ; k < 10 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; int ans = noncoprimeCount ( 0 , N , S . toCharArray ( ) , 1 , 0 , 1 ) ; System . out . print ( ans - 1 + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 10000 ; noncoprimeCountUtil ( N ) ; } }
import java . util . * ; class GFG { static int ans = 0 ; static int maximumStonesUtil ( int [ ] arr , int n , int i , int [ ] dp ) { if ( i >= n ) return 0 ; ans = dp [ i ] ; if ( ans != - 1 ) return ans ; ans = Integer . MIN_VALUE ; ans = Math . max ( ans , arr [ i ] - maximumStonesUtil ( arr , n , i + 1 , dp ) ) ; if ( i + 1 < n ) ans = Math . max ( ans , arr [ i ] + arr [ i + 1 ] - maximumStonesUtil ( arr , n , i + 2 , dp ) ) ; if ( i + 2 < n ) ans = Math . max ( ans , arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] - maximumStonesUtil ( arr , n , i + 3 , dp ) ) ; return ans ; } static String maximumStones ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; Arrays . fill ( dp , - 1 ) ; int res = maximumStonesUtil ( arr , n , 0 , dp ) ; if ( res > 0 ) return " Player1" ; else if ( res < 0 ) return " Player2" ; else return " Tie " ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 } ; int n = arr . length ; System . out . print ( maximumStones ( arr , n ) ) ; } }
class GFG { static int numberOfWays ( int N ) { int [ ] [ ] DP = new int [ N + 1 ] [ 5 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { for ( int j = 0 ; j < 5 ; j ++ ) { DP [ i ] [ j ] = 0 ; } } DP [ 1 ] [ 1 ] = 1 ; DP [ 1 ] [ 2 ] = 1 ; DP [ 1 ] [ 3 ] = 1 ; DP [ 1 ] [ 4 ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( i - 1 > 0 && DP [ i - 1 ] [ 1 ] > 0 ) DP [ i ] [ 1 ] = DP [ i - 1 ] [ 4 ] ; if ( i - 2 > 0 && DP [ i - 2 ] [ 2 ] > 0 ) DP [ i ] [ 2 ] = DP [ i - 2 ] [ 4 ] ; if ( i - 3 > 0 && DP [ i - 3 ] [ 3 ] > 0 ) DP [ i ] [ 3 ] = DP [ i - 3 ] [ 4 ] ; DP [ i ] [ 4 ] = ( DP [ i ] [ 1 ] + DP [ i ] [ 2 ] + DP [ i ] [ 3 ] ) ; } return DP [ N ] [ 4 ] ; } static public void main ( String [ ] args ) { int N = 15 ; System . out . println ( numberOfWays ( N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int countSortedArrays ( int start , int m , int size , int n ) { if ( size == m ) return 1 ; if ( start > n ) return 0 ; int notTaken = 0 , taken = 0 ; taken = countSortedArrays ( start , m , size + 1 , n ) ; notTaken = countSortedArrays ( start + 1 , m , size , n ) ; return taken + notTaken ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . println ( countSortedArrays ( 1 , m , 0 , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maximumSum ( int arr [ ] , int n ) { long [ ] preSum = new long [ n ] ; long sum = 0 ; long maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = Math . max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . max ( maxSum , sum ) ; preSum [ i ] = maxSum ; } sum = 0 ; maxSum = 0 ; long [ ] postSum = new long [ n + 1 ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { sum = Math . max ( arr [ i ] , sum + arr [ i ] ) ; maxSum = Math . max ( maxSum , sum ) ; postSum [ i ] = maxSum ; } long ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( ans , preSum [ i ] + postSum [ i + 1 ] ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , - 1 , - 4 , - 5 , 7 , 1 } ; maximumSum ( arr , arr . length ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int MaxSubsetlength ( String arr [ ] , int A , int B ) { int dp [ ] [ ] = new int [ A + 1 ] [ B + 1 ] ; for ( String str : arr ) { int zeros = 0 , ones = 0 ; for ( char ch : str . toCharArray ( ) ) { if ( ch == '0' ) zeros ++ ; else ones ++ ; } for ( int i = A ; i >= zeros ; i -- ) for ( int j = B ; j >= ones ; j -- ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - zeros ] [ j - ones ] + 1 ) ; } return dp [ A ] [ B ] ; } public static void main ( String [ ] args ) { String arr [ ] = { "1" , "0" , "0001" , "10" , "111001" } ; int A = 5 , B = 3 ; System . out . println ( MaxSubsetlength ( arr , A , B ) ) ; } }
import java . io . * ; class GFG { static int numberOfUniqueOutcomes ( int N , int S ) { if ( S < N ) return 0 ; if ( N == 1 N == S ) return 1 ; return ( numberOfUniqueOutcomes ( N - 1 , S - 1 ) + numberOfUniqueOutcomes ( N - 1 , S - 2 ) ) ; } public static void main ( String [ ] args ) { int N = 3 , S = 4 ; System . out . println ( numberOfUniqueOutcomes ( N , S ) ) ; } }
class GFG { final static int size = 20 ; static int [ ] dp = new int [ size ] ; static int ans ; static void insertVector ( int mask ) { for ( int i = 0 ; i < 20 ; i ++ ) { if ( ( mask & 1 << i ) == 0 ) continue ; if ( dp [ i ] == 0 ) { dp [ i ] = mask ; ++ ans ; return ; } mask ^= dp [ i ] ; } } static void maxSizeSet ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { insertVector ( arr [ i ] ) ; } System . out . println ( 1 << ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; maxSizeSet ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void minCost ( int arr [ ] , int n ) { if ( n < 3 ) { System . out . println ( arr [ 0 ] ) ; return ; } int dp [ ] = new int [ n ] ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = dp [ 0 ] + arr [ 1 ] + arr [ 2 ] ; for ( int i = 2 ; i < n - 1 ; i ++ ) dp [ i ] = Math . min ( dp [ i - 2 ] + arr [ i ] , dp [ i - 1 ] + arr [ i ] + arr [ i + 1 ] ) ; dp [ n - 1 ] = Math . min ( dp [ n - 2 ] , dp [ n - 3 ] + arr [ n - 1 ] ) ; System . out . println ( dp [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 4 , 6 , 8 , 5 } ; int N = arr . length ; minCost ( arr , N ) ; } }
import java . util . * ; class GFG { static void maxSum ( int arr [ ] , int N , int K ) { int [ ] dp = new int [ N + 2 ] ; Arrays . fill ( dp , 0 ) ; int maxval = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( ( i + K * arr [ i ] ) >= N ) { dp [ i ] = arr [ i ] ; } else { dp [ i ] = dp [ i + K * arr [ i ] ] + arr [ i ] ; } maxval = Math . max ( maxval , dp [ i ] ) ; } System . out . print ( maxval ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 3 , 1 , 2 } ; int N = arr . length ; int K = 3 ; maxSum ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void maxSubmatrixSum ( int [ ] [ ] matrix ) { int r = matrix . length ; int c = matrix [ 0 ] . length ; int maxSubmatrix = 0 ; for ( int i = 0 ; i < r ; i ++ ) { for ( int j = 0 ; j < c ; j ++ ) { for ( int k = i ; k < r ; k ++ ) { for ( int l = j ; l < c ; l ++ ) { int sumSubmatrix = 0 ; for ( int m = i ; m <= k ; m ++ ) { for ( int n = j ; n <= l ; n ++ ) { sumSubmatrix += matrix [ m ] [ n ] ; } } maxSubmatrix = Math . max ( maxSubmatrix , sumSubmatrix ) ; } } } } System . out . println ( maxSubmatrix ) ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 0 , - 2 , - 7 , 0 } , { 9 , 2 , - 6 , 2 } , { - 4 , 1 , - 4 , 1 } , { - 1 , 8 , 0 , - 2 } } ; maxSubmatrixSum ( matrix ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxN = 20 ; static int maxSum = 50 ; static int minSum = 50 ; static int Base = 50 ; static int [ ] [ ] dp = new int [ maxN ] [ maxSum + minSum ] ; static boolean [ ] [ ] v = new boolean [ maxN ] [ maxSum + minSum ] ; static int findCnt ( int [ ] arr , int i , int required_sum , int n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ required_sum + Base ] ) return dp [ i ] [ required_sum + Base ] ; v [ i ] [ required_sum + Base ] = true ; dp [ i ] [ required_sum + Base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i ] [ required_sum + Base ] ; } static void countSubsets ( int [ ] arr , int K , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int S1 = ( sum + K ) / 2 ; System . out . print ( findCnt ( arr , 0 , S1 , n ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 2 , 3 } ; int N = arr . length ; int K = 1 ; countSubsets ( arr , K , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void maximumSubsetSum ( int arr1 [ ] , int arr2 [ ] , int length ) { int dp [ ] = new int [ length + 1 ] ; if ( length == 1 ) { System . out . print ( Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; return ; } if ( length == 2 ) { System . out . print ( Math . max ( Math . max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ) ; return ; } else { dp [ 0 ] = Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ; dp [ 1 ] = Math . max ( Math . max ( arr1 [ 1 ] , arr2 [ 1 ] ) , Math . max ( arr1 [ 0 ] , arr2 [ 0 ] ) ) ; int index = 2 ; while ( index < length ) { dp [ index ] = Math . max ( Math . max ( arr1 [ index ] , arr2 [ index ] ) , Math . max ( Math . max ( arr1 [ index ] + dp [ index - 2 ] , arr2 [ index ] + dp [ index - 2 ] ) , dp [ index - 1 ] ) ) ; ++ index ; } System . out . print ( dp [ length - 1 ] ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { - 1 , - 2 , 4 , - 4 , 5 } ; int arr2 [ ] = { - 1 , - 2 , - 3 , 4 , 10 } ; int length = arr1 . length ; maximumSubsetSum ( arr1 , arr2 , length ) ; } }
import java . util . * ; import java . util . ArrayList ; import java . util . List ; class GFG { static void minAtEachIndex ( int n , int arr [ ] ) { int dp1 [ ] = new int [ n ] ; int dp2 [ ] = new int [ n ] ; int i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = Math . min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = Math . min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( i = 0 ; i < n ; i ++ ) v . add ( Math . min ( dp1 [ i ] , dp2 [ i ] ) ) ; for ( int x : v ) System . out . print ( x + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 2 , 5 , 3 } ; int N = arr . length ; minAtEachIndex ( N , arr ) ; } }
import java . util . * ; class GFG { static int prodOfDigit ( int N ) { int res = 1 ; while ( N > 0 ) { res = res * ( N % 10 ) ; N /= 10 ; } return res ; } static int cntNumRange ( int L , int R , int K ) { int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( prodOfDigit ( i ) == K ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int L = 20 , R = 10000 , K = 14 ; System . out . print ( cntNumRange ( L , R , K ) ) ; } }
class GFG { static final long M = 1000000007 ; static long power ( long X , long Y ) { long res = 1 ; X = X % M ; if ( X == 0 ) return 0 ; while ( Y > 0 ) { if ( Y % 2 == 1 ) { res = ( res * X ) % M ; } Y = Y >> 1 ; X = ( X * X ) % M ; } return res ; } static long findValue ( int N ) { long [ ] dp = new long [ N + 1 ] ; dp [ 1 ] = 2 ; dp [ 2 ] = 1024 ; for ( int i = 3 ; i <= N ; i ++ ) { int y = ( i & ( - i ) ) ; int x = i - y ; if ( x == 0 ) { dp [ i ] = power ( dp [ i / 2 ] , 10 ) ; } else { dp [ i ] = ( dp [ x ] * dp [ y ] ) % M ; } } return ( dp [ N ] * dp [ N ] ) % M ; } public static void main ( String [ ] args ) { int n = 150 ; System . out . print ( findValue ( n ) ) ; } }
class GFG { static void FindSub ( String str , String res , int i ) { if ( i == str . length ( ) ) { if ( res . length ( ) > 0 ) { System . out . print ( res + " ▁ " ) ; } return ; } char ch = str . charAt ( i ) ; FindSub ( str , res , i + 1 ) ; FindSub ( str , res + ch , i + 1 ) ; FindSub ( str , res + ( int ) ch , i + 1 ) ; ; } public static void main ( String [ ] args ) { String str = " ab " ; String res = " " ; int N = str . length ( ) ; FindSub ( str , res , 0 ) ; } }
class GFG { public static int MinOp ( int N ) { if ( N <= 1 ) return N ; int bit = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; return ( ( 1 << bit ) - 1 ) - MinOp ( N - ( 1 << ( bit - 1 ) ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( MinOp ( N ) ) ; } }
import java . io . * ; class GFG { static int maxProductPath ( int [ ] [ ] grid ) { int n = grid . length ; int m = grid [ 0 ] . length ; int [ ] [ ] maxPath = new int [ n ] [ m ] ; int [ ] [ ] minPath = new int [ n ] [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int mn = Integer . MAX_VALUE ; int mx = Integer . MIN_VALUE ; if ( i == 0 && j == 0 ) { mx = grid [ i ] [ j ] ; mn = grid [ i ] [ j ] ; } if ( i > 0 ) { int tempmx = Math . max ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mx = Math . max ( mx , tempmx ) ; int tempmn = Math . min ( ( maxPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) , ( minPath [ i - 1 ] [ j ] * grid [ i ] [ j ] ) ) ; mn = Math . min ( mn , tempmn ) ; } if ( j > 0 ) { int tempmx = Math . max ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mx = Math . max ( mx , tempmx ) ; int tempmn = Math . min ( ( maxPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) , ( minPath [ i ] [ j - 1 ] * grid [ i ] [ j ] ) ) ; mn = Math . min ( mn , tempmn ) ; } maxPath [ i ] [ j ] = mx ; minPath [ i ] [ j ] = mn ; } } if ( maxPath [ n - 1 ] [ m - 1 ] < 0 ) { return - 1 ; } else { return ( maxPath [ n - 1 ] [ m - 1 ] ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , - 2 , 1 } , { 1 , - 2 , 1 } , { 3 , - 4 , 1 } } ; System . out . println ( maxProductPath ( mat ) ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] bin = new int [ 3000 ] [ 3000 ] ; static void countWays ( int [ ] arr ) { int mod = ( int ) 1e9 + 7 ; bin [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < 3000 ; i ++ ) { bin [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { bin [ i ] [ j ] = ( bin [ i - 1 ] [ j ] + bin [ i - 1 ] [ j - 1 ] ) % mod ; } } int n = 0 ; for ( int x : arr ) n += x ; int [ ] ways = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { ways [ i ] = 1 ; for ( int j = 0 ; j < arr . length ; j ++ ) { ways [ i ] = ( ways [ i ] * bin [ arr [ j ] + i - 1 ] [ i - 1 ] ) % mod ; } for ( int j = 1 ; j < i ; j ++ ) { ways [ i ] = ( ( ways [ i ] - bin [ i ] [ j ] * ways [ j ] ) % mod + mod ) % mod ; } } int ans = 0 ; for ( int x : ways ) ans = ( ans + x ) % mod ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; countWays ( arr ) ; } }
import java . util . * ; class GFG { static int maxScore ( int [ ] arr ) { int N = arr . length ; N ++ ; int [ ] [ ] dp = new int [ N + 1 ] [ 2 * ( N - 1 ) + 1 ] ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int s = 1 ; s <= 2 * ( N - 1 ) ; s ++ ) { for ( int j = 1 ; j <= N - 1 && j <= s ; j ++ ) { dp [ i ] [ s ] = Math . max ( dp [ i ] [ s ] , arr [ j - 1 ] + dp [ i - 1 ] [ s - j ] ) ; } } } return dp [ N ] [ 2 * ( N - 1 ) ] - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 0 } ; System . out . print ( maxScore ( arr ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void minimumCost ( int [ ] cost , int n , int x ) { int [ ] dp = new int [ n + 2 ] ; dp [ 0 ] = cost [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i == 1 ) dp [ i ] = cost [ i ] + dp [ i - 1 ] ; if ( i == 2 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , x + dp [ i - 2 ] ) ; if ( i >= 3 ) dp [ i ] = cost [ i ] + Math . min ( dp [ i - 1 ] , Math . min ( x + dp [ i - 2 ] , 2 * x + dp [ i - 3 ] ) ) ; } System . out . println ( dp [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int X = 4 ; int [ ] cost = { 6 , 3 , 9 , 2 , 1 , 3 } ; int N = cost . length ; minimumCost ( cost , N , X ) ; } }
import java . io . * ; class GFG { public static int maxSum ( int a [ ] , int n , int k ) { if ( n <= 0 ) return 0 ; int option = maxSum ( a , n - 1 , k ) ; if ( k >= a [ n - 1 ] ) option = Math . max ( option , a [ n - 1 ] + maxSum ( a , n - 2 , k - a [ n - 1 ] ) ) ; return option ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 10 , 20 , 30 , 40 } ; int N = arr . length ; int K = 100 ; System . out . println ( maxSum ( arr , N , K ) ) ; } }
class GFG { static boolean checkStr ( String str , int P , int Q ) { int N = str . length ( ) ; char prev = str . charAt ( 0 ) ; int cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == prev ) { cnt ++ ; } else { if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } cnt = 1 ; } prev = str . charAt ( i ) ; } if ( prev == '1' && cnt >= Q ) { return false ; } if ( prev == '0' && cnt >= P ) { return false ; } return true ; } static int cntBinStr ( String str , int N , int P , int Q ) { int len = str . length ( ) ; if ( len == N ) { if ( checkStr ( str , P , Q ) ) return 1 ; return 0 ; } int X = cntBinStr ( str + '0' , N , P , Q ) ; int Y = cntBinStr ( str + '1' , N , P , Q ) ; return X + Y ; } public static void main ( String [ ] args ) { int N = 5 , P = 2 , Q = 3 ; System . out . println ( cntBinStr ( " " , N , P , Q ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void smallestPosition ( int m [ ] [ ] , int k ) { int n = m . length ; int mm = m [ 0 ] . length ; int [ ] [ ] xor = new int [ n ] [ mm ] ; PriorityQueue < Integer > minHeap = new PriorityQueue < > ( ) ; Map < Integer , int [ ] > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < mm ; j ++ ) { int a = i - 1 >= 0 ? xor [ i - 1 ] [ j ] : 0 ; int b = j - 1 >= 0 ? xor [ i ] [ j - 1 ] : 0 ; int c = ( i - 1 >= 0 && j - 1 >= 0 ) ? xor [ i - 1 ] [ j - 1 ] : 0 ; xor [ i ] [ j ] = m [ i ] [ j ] ^ a ^ b ^ c ; minHeap . add ( xor [ i ] [ j ] ) ; if ( minHeap . size ( ) > k ) { minHeap . poll ( ) ; } if ( ! map . containsKey ( xor [ i ] [ j ] ) ) map . put ( xor [ i ] [ j ] , new int [ ] { i , j } ) ; } } int kth_max_e = minHeap . poll ( ) ; System . out . println ( ( map . get ( kth_max_e ) [ 0 ] + 1 ) + " ▁ " + ( map . get ( kth_max_e ) [ 1 ] + 1 ) ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 1 , 2 , 3 } , { 2 , 2 , 1 } , { 2 , 4 , 2 } } ; int k = 1 ; smallestPosition ( m , k ) ; } }
import java . io . * ; class GFG { static int longestPalSubstr ( String str ) { int n = str . length ( ) ; int maxLength = 1 , start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = i ; j < str . length ( ) ; j ++ ) { int flag = 1 ; for ( int k = 0 ; k < ( j - i + 1 ) / 2 ; k ++ ) if ( str . charAt ( i + k ) != str . charAt ( j - k ) ) flag = 0 ; if ( flag != 0 && ( j - i + 1 ) > maxLength ) { start = i ; maxLength = j - i + 1 ; } } } return maxLength ; } public static void main ( String [ ] args ) { String str = " forgeeksskeegfor " ; System . out . print ( longestPalSubstr ( str ) ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 101 ] [ 101 ] [ 101 ] ; static int helper ( int arr [ ] , int left , int right , int count , int m ) { if ( left > right ) return 0 ; if ( dp [ left ] [ right ] [ count ] != - 1 ) { return dp [ left ] [ right ] [ count ] ; } int ans = ( count + 1 ) * m + helper ( arr , left + 1 , right , 0 , m ) ; for ( int i = left + 1 ; i <= right ; ++ i ) { if ( arr [ i ] == arr [ left ] ) { ans = Math . max ( ans , helper ( arr , left + 1 , i - 1 , 0 , m ) + helper ( arr , i , right , count + 1 , m ) ) ; } } dp [ left ] [ right ] [ count ] = ans ; return ans ; } static int maxPoints ( int arr [ ] , int n , int m ) { int len = n ; for ( int i = 0 ; i < 101 ; i ++ ) { for ( int j = 0 ; j < 101 ; j ++ ) { for ( int k = 0 ; k < 101 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } return helper ( arr , 0 , len - 1 , 0 , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 2 , 2 , 3 , 4 , 3 , 1 } ; int M = 3 ; int N = arr . length ; System . out . print ( maxPoints ( arr , N , M ) ) ; } }
import java . util . * ; class GFG { static int findWays ( int N ) { if ( N == 0 ) { return 1 ; } int cnt = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) { if ( N - i >= 0 ) { cnt = cnt + findWays ( N - i ) ; } } return cnt ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . print ( findWays ( N ) ) ; } }
import java . util . * ; public class GFG { static int inf = 1000000008 ; public static int minSteps ( int m , int n ) { if ( n == m ) return 0 ; if ( m > n ) return inf ; int min_cost = inf ; for ( int i = 2 ; i < m ; i += 2 ) { if ( m % i == 0 ) { min_cost = Math . min ( min_cost , m / i + minSteps ( m + i , n ) ) ; } } return min_cost ; } public static void main ( String args [ ] ) { int M = 6 ; int N = 24 ; int minimum_cost = minSteps ( M , N ) ; minimum_cost = minimum_cost == inf ? - 1 : minimum_cost ; System . out . println ( minimum_cost ) ; } }
import java . util . * ; class GFG { static int minCountOfNumbers ( int N ) { int k = N % 10 ; int z = N - ( 9 * ( 9 - k ) ) ; if ( z >= 9 && z % 10 == 9 ) { return 10 - k ; } else return - 1 ; } public static void main ( String [ ] args ) { int N = 156 ; System . out . print ( minCountOfNumbers ( N ) ) ; } }
class GFG { static int INF = ( int ) 1e7 ; static int min_op ( int cur , int M ) { if ( cur > M ) return INF ; if ( cur == M ) return 0 ; int op = INF ; for ( int i = 2 ; i * i <= cur ; i ++ ) { if ( cur % i == 0 ) { if ( i % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + i , M ) ) ; } if ( ( cur / i ) != i && ( cur / i ) % 2 == 0 ) { op = Math . min ( op , 1 + min_op ( cur + ( cur / i ) , M ) ) ; } } } return op ; } static void min_operations ( int N , int M ) { int op = min_op ( N , M ) ; if ( op >= INF ) System . out . print ( " - 1" ) ; else System . out . print ( op + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 6 , M = 24 ; min_operations ( N , M ) ; } }
import java . io . * ; import java . util . * ; class GFG { private static int [ ] findPrimeFactors ( int n ) { int [ ] primeFactors = new int [ 9 ] ; int j = 0 ; if ( n % 2 == 0 ) { primeFactors [ j ++ ] = 2 ; while ( n % 2 == 0 ) n >>= 1 ; } for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) { primeFactors [ j ++ ] = i ; while ( n % i == 0 ) n /= i ; } } if ( n > 2 ) primeFactors [ j ++ ] = n ; return Arrays . copyOfRange ( primeFactors , 0 , j ) ; } private static void findShortestSubsequence ( int [ ] dp , int [ ] a , int index , int [ ] primeFactors ) { int n = a . length ; for ( int j = index ; j < n ; j ++ ) { int bitmask = 0 ; for ( int p = 0 ; p < primeFactors . length ; p ++ ) { if ( a [ j ] % primeFactors [ p ] == 0 ) { bitmask ^= ( 1 << p ) ; } } for ( int i = 0 ; i < dp . length ; i ++ ) { if ( dp [ i ] == n + 1 ) continue ; dp [ bitmask & i ] = Math . min ( dp [ bitmask & i ] , dp [ i ] + 1 ) ; } } } private static void printMinimumLength ( int [ ] a ) { int min = a . length + 1 ; for ( int i = 0 ; i < a . length - 1 ; i ++ ) { int [ ] primeFactors = findPrimeFactors ( a [ i ] ) ; int n = primeFactors . length ; int [ ] dp = new int [ 1 << n ] ; Arrays . fill ( dp , a . length + 1 ) ; int setBits = ( 1 << n ) - 1 ; dp [ setBits ] = 1 ; findShortestSubsequence ( dp , a , i + 1 , primeFactors ) ; min = Math . min ( dp [ 0 ] , min ) ; } if ( min == a . length + 1 ) System . out . println ( - 1 ) ; else System . out . println ( min ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 6 , 12 , 3 } ; printMinimumLength ( arr ) ; } }
import java . util . HashMap ; class GFG { static int count ( int n ) { HashMap < Integer , Integer > dp = new HashMap < Integer , Integer > ( ) ; dp . put ( 0 , 0 ) ; dp . put ( 1 , 1 ) ; if ( ! dp . containsKey ( n ) ) dp . put ( n , 1 + Math . min ( n % 2 + count ( n / 2 ) , n % 3 + count ( n / 3 ) ) ) ; return dp . get ( n ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( String . valueOf ( ( count ( N ) ) ) ) ; } }
import java . util . * ; import java . io . * ; public class GFG { public static int xorSum ( int a [ ] , int n , int mask , int [ ] dp ) { if ( dp [ mask ] != - 1 ) { return dp [ mask ] ; } int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( i != j && ( mask & ( 1 << i ) ) == 0 && ( mask & ( 1 << j ) ) == 0 ) { max_value = Math . max ( max_value , ( a [ i ] ^ a [ j ] ) + xorSum ( a , n , ( mask | ( 1 << i ) | ( 1 << j ) ) , dp ) ) ; } } } return dp [ mask ] = max_value ; } public static void main ( String args [ ] ) { int n = 4 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; int dp [ ] = new int [ ( 1 << n ) + 5 ] ; Arrays . fill ( dp , - 1 ) ; System . out . println ( xorSum ( arr , n , 0 , dp ) ) ; } }
import java . util . * ; class GFG { static void main_fun ( int arr [ ] , int n ) { int [ ] [ ] dp = new int [ 4 ] [ n ] ; if ( arr [ 0 ] == 0 ) dp [ 0 ] [ 0 ] = 1 ; else dp [ 1 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 1 ; else dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) dp [ 1 ] [ i ] = Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ; else dp [ 1 ] [ i ] = dp [ 1 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { dp [ 2 ] [ i ] = Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ; } else dp [ 2 ] [ i ] = dp [ 2 ] [ i - 1 ] ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { dp [ 3 ] [ i ] = Math . max ( dp [ 3 ] [ i - 1 ] + 1 , Math . max ( dp [ 2 ] [ i - 1 ] + 1 , Math . max ( dp [ 1 ] [ i - 1 ] + 1 , dp [ 0 ] [ i - 1 ] + 1 ) ) ) ; } else dp [ 3 ] [ i ] = dp [ 3 ] [ i - 1 ] ; } int ans = Math . max ( dp [ 2 ] [ n - 1 ] , Math . max ( dp [ 1 ] [ n - 1 ] , Math . max ( dp [ 0 ] [ n - 1 ] , dp [ 3 ] [ n - 1 ] ) ) ) ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 0 , 1 , 0 , 1 } ; main_fun ( arr , n ) ; } }
import java . util . * ; class GFG { static ArrayList < Integer > primes ; static int M = 100005 ; static void sieve ( ) { primes = new ArrayList < > ( ) ; boolean mark [ ] = new boolean [ M ] ; for ( int i = 2 ; i < M ; i ++ ) mark [ i ] = true ; for ( int i = 2 ; i * i < M ; i ++ ) { if ( mark [ i ] ) { for ( int j = i * i ; j < M ; j += i ) mark [ j ] = false ; } } for ( int i = 2 ; i < M ; i ++ ) if ( mark [ i ] ) primes . add ( i ) ; } static void countPrimonacci ( int l , int r ) { ArrayList < Integer > dp = new ArrayList < > ( ) ; dp . add ( 1 ) ; dp . add ( 1 ) ; int i = 2 ; HashSet < Integer > s = new HashSet < > ( ) ; while ( true ) { int x = 0 ; for ( int j = 0 ; j < primes . size ( ) ; j ++ ) { int p = primes . get ( j ) ; if ( p >= i ) break ; x += dp . get ( i - p ) ; } if ( x >= l && x <= r ) s . add ( x ) ; if ( x > r ) break ; dp . add ( x ) ; i ++ ; } System . out . println ( s . size ( ) ) ; } public static void main ( String [ ] args ) { sieve ( ) ; int L = 1 , R = 10 ; countPrimonacci ( L , R ) ; } }
import java . util . * ; class GFG { static int N = ( int ) ( 1e5 + 5 ) ; static int MOD = 1000000007 ; static int [ ] dp = new int [ N ] ; static void countStrings ( int K , int [ ] [ ] Q ) { dp [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( i >= K ) dp [ i ] = ( dp [ i ] + dp [ i - K ] ) % MOD ; } for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = ( dp [ i ] + dp [ i - 1 ] ) % MOD ; } for ( int i = 0 ; i < Q . length ; i ++ ) { int ans = dp [ Q [ i ] [ 1 ] ] - dp [ Q [ i ] [ 0 ] - 1 ] ; if ( ans < 0 ) ans = ans + MOD ; System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { int K = 3 ; int [ ] [ ] Q = { { 1 , 4 } , { 3 , 7 } } ; countStrings ( K , Q ) ; } }
import java . util . * ; import java . io . * ; public class Main { public static long minSum ( int n , int num [ ] , int price [ ] ) { long dp [ ] = new long [ n ] ; Arrays . fill ( dp , Integer . MAX_VALUE ) ; long ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( num [ j ] > num [ i ] ) { dp [ j ] = ( long ) Math . min ( ( long ) dp [ j ] , ( long ) price [ i ] + ( long ) price [ j ] ) ; ans = Math . min ( ans , ( long ) dp [ i ] + ( long ) price [ j ] ) ; } } } return ans != Integer . MAX_VALUE ? ans : - 1 ; } public static void main ( String [ ] args ) { int num [ ] = { 2 , 4 , 6 , 7 , 8 } ; int price [ ] = { 10 , 20 , 100 , 20 , 40 } ; int n = price . length ; System . out . println ( minSum ( n , num , price ) ) ; } }
import java . util . * ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long countStrings ( int N ) { if ( N % 2 == 1 ) return 0 ; else { N /= 2 ; long c = binomialCoeff ( 2 * N , N ) ; return c / ( N + 1 ) ; } } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( countStrings ( N ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static int solve ( int n , int k , int mod , int dp [ ] ) { if ( n < 0 ) return 0 ; if ( n == 0 ) return 1 ; if ( dp [ n ] != - 1 ) return dp [ n ] ; int cnt = 0 ; for ( int i = 2 ; i <= k ; i += 2 ) { cnt = ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ; } dp [ n ] = cnt ; return cnt ; } public static void main ( String [ ] args ) { int mod = ( int ) ( 1e9 + 7 ) ; int n = 4 , k = 2 ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; int ans = solve ( n , k , mod , dp ) ; System . out . println ( ans ) ; } }
class GFG { static int [ ] f = new int [ 12 ] ; static void fact ( ) { f [ 0 ] = f [ 1 ] = 1 ; for ( int i = 2 ; i <= 10 ; i ++ ) f [ i ] = i * 1 * f [ i - 1 ] ; } static void solve ( int arr [ ] , int n , int k ) { fact ( ) ; int even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; } if ( k > even ) System . out . print ( 0 + "NEW_LINE"); else { System . out . print ( f [ even ] / ( f [ k ] * f [ even - k ] ) ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 1 ; solve ( arr , n , k ) ; } }
import java . util . * ; class GFG { final static int MOD = 1000000007 ; static int nCrModp ( int n , int r ) { if ( n % 2 == 1 ) { return - 1 ; } int C [ ] = new int [ r + 1 ] ; Arrays . fill ( C , 0 ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD ; } return C [ r ] ; } public static void main ( String s [ ] ) { int N = 6 ; System . out . println ( nCrModp ( N , N / 2 ) ) ; } }
import java . util . * ; class GFG { static void printMinOperations ( int N ) { int dp [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) dp [ i ] = N ; dp [ 1 ] = 0 ; for ( int i = 2 ; i < N + 1 ; i ++ ) { if ( i % 2 == 0 && dp [ i ] > dp [ i / 2 ] + 1 ) dp [ i ] = dp [ i / 2 ] + 1 ; if ( i % 3 == 0 && dp [ i ] > dp [ i / 3 ] + 1 ) dp [ i ] = dp [ i / 3 ] + 1 ; if ( dp [ i ] > dp [ i - 1 ] + 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; } System . out . println ( dp [ N ] ) ; Vector < Integer > seq = new Vector < Integer > ( ) ; while ( N > 1 ) { seq . add ( N ) ; if ( dp [ N - 1 ] == dp [ N ] - 1 ) N = N - 1 ; else if ( N % 2 == 0 && dp [ N / 2 ] == dp [ N ] - 1 ) N = N / 2 ; else N = N / 3 ; } seq . add ( 1 ) ; for ( int i = seq . size ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( seq . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int X = 96234 ; printMinOperations ( X ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void maxLength ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = 1 ; } for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 1 ; j <= Math . sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { int s = i / j ; if ( s == j ) { if ( arr [ i ] > arr [ s ] ) { dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; } } else { if ( s != i && arr [ i ] > arr [ s ] ) dp [ s ] = Math . max ( dp [ i ] + 1 , dp [ s ] ) ; if ( arr [ i ] > arr [ j ] ) { dp [ j ] = Math . max ( dp [ i ] + 1 , dp [ j ] ) ; } } } } } int max = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( dp [ i ] > max ) max = dp [ i ] ; } System . out . println ( max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 } ; int size = arr . length ; maxLength ( arr , size ) ; } }
import java . util . * ; class GFG { static int maxProfit ( int price [ ] , int n ) { int buy1 , profit1 , buy2 , profit2 ; buy1 = buy2 = Integer . MAX_VALUE ; profit1 = profit2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { buy1 = Math . min ( buy1 , price [ i ] ) ; profit1 = Math . max ( profit1 , price [ i ] - buy1 ) ; buy2 = Math . min ( buy2 , price [ i ] - profit1 ) ; profit2 = Math . max ( profit2 , price [ i ] - buy2 ) ; } return profit2 ; } public static void main ( String [ ] args ) { int price [ ] = { 2 , 30 , 15 , 10 , 8 , 25 , 80 } ; int n = price . length ; System . out . print ( " Maximum ▁ Profit ▁ = ▁ " + maxProfit ( price , n ) ) ; } }
import java . util . * ; class GFG { static int findMaximumScore ( int [ ] a , int n ) { @ SuppressWarnings ( " unchecked " ) HashMap < Integer , Integer > freq = new HashMap ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( a [ i ] ) ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . put ( a [ i ] , 1 ) ; } } int [ ] dp = new int [ Arrays . stream ( a ) . max ( ) . getAsInt ( ) + 1 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = freq . get ( 1 ) ; for ( int i = 2 ; i < dp . length ; i ++ ) dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + freq . get ( i ) * i ) ; return dp [ dp . length - 1 ] ; } public static void main ( String [ ] args ) { int n ; n = 3 ; int [ ] a = { 1 , 2 , 3 } ; System . out . print ( findMaximumScore ( a , n ) ) ; } }
import java . util . * ; class GFG { static int prime [ ] = new int [ 1000001 ] ; static void seiveOfEratosthenes ( ) { for ( int i = 2 ; i < 1000001 ; i ++ ) { prime [ i ] = 1 ; } for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 0 ; } } } } static void CountElements ( int arr [ ] , int n , int L , int R ) { seiveOfEratosthenes ( ) ; int countPrime [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { countPrime [ i ] = countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ; } System . out . print ( countPrime [ R ] - countPrime [ L - 1 ] + "NEW_LINE"); return ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 8 } ; int N = arr . length ; int L = 1 , R = 3 ; CountElements ( arr , N , L , R ) ; } }
class GFG { static void find_minimum_operations ( int n , int b [ ] , int k ) { int d [ ] = new int [ n + 1 ] ; int i , operations = 0 , need ; for ( i = 0 ; i < n ; i ++ ) { if ( i > 0 ) { d [ i ] += d [ i - 1 ] ; } if ( b [ i ] > d [ i ] ) { operations += b [ i ] - d [ i ] ; need = b [ i ] - d [ i ] ; d [ i ] += need ; if ( i + k <= n ) { d [ i + k ] -= need ; } } } System . out . println ( operations ) ; } public static void main ( String [ ] args ) { int n = 5 ; int b [ ] = { 1 , 2 , 3 , 4 , 5 } ; int k = 2 ; find_minimum_operations ( n , b , k ) ; } }
class GFG { static int n , k ; static int [ ] [ ] [ ] [ ] dp = new int [ 1024 ] [ 2048 ] [ 2 ] [ 2 ] ; static int Ways ( int col , int comp , int row1 , int row2 ) { if ( comp > k ) return 0 ; if ( col > n ) { if ( comp == k ) return 1 ; else return 0 ; } if ( dp [ col ] [ comp ] [ row1 ] [ row2 ] != - 1 ) return dp [ col ] [ comp ] [ row1 ] [ row2 ] ; else { int ans = 0 ; if ( col == 1 ) { ans = ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) + Ways ( col + 1 , comp + 1 , 1 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } else { if ( ( row1 > 0 && row2 > 0 ) || ( row1 == 0 && row2 == 0 ) ) { ans = ( ( ( ans + Ways ( col + 1 , comp + 1 , 0 , 0 ) ) + Ways ( col + 1 , comp + 1 , 1 , 0 ) ) + Ways ( col + 1 , comp + 1 , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp , 1 , 1 ) ) ; } if ( row1 > 0 && row2 == 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 1 , 0 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 0 , 1 ) ) ; } if ( row1 == 0 && row2 > 0 ) { ans = ( ( ( ans + Ways ( col + 1 , comp , 0 , 0 ) ) + Ways ( col + 1 , comp , 1 , 1 ) ) + Ways ( col + 1 , comp , 0 , 1 ) ) ; ans = ( ans + Ways ( col + 1 , comp + 2 , 1 , 0 ) ) ; } } return dp [ col ] [ comp ] [ row1 ] [ row2 ] = ans ; } } public static void main ( String [ ] args ) { n = 2 ; k = 1 ; for ( int i = 0 ; i < 1024 ; i ++ ) for ( int j = 0 ; j < 2048 ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; System . out . print ( Ways ( 1 , 0 , 0 , 0 ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static boolean [ ] primes = new boolean [ 1000001 ] ; static int maxn = 1000000 ; static void sieve ( ) { Arrays . fill ( primes , true ) ; primes [ 0 ] = false ; primes [ 1 ] = false ; for ( int i = 2 ; i * i <= maxn ; i ++ ) { if ( primes [ i ] ) { for ( int j = i * i ; j <= maxn ; j += i ) primes [ j ] = false ; } } } static String toBinary ( int n ) { String r = " " ; while ( n != 0 ) { r = ( n % 2 == 0 ? "0" : "1" ) + r ; n /= 2 ; } return ( r == " " ) ? "0" : r ; } static void PrimeSplit ( String str ) { String temp ; int cnt = 0 ; ArrayList < String > ans = new ArrayList < > ( ) ; int bt = 1 << ( str . length ( ) - 1 ) ; int n = str . length ( ) ; for ( int i = 0 ; i < bt ; i ++ ) { temp = toBinary ( i ) + "0" ; int j = 0 , x = n - temp . length ( ) , y ; while ( j < x ) { temp = "0" + temp ; j ++ ; } j = 0 ; x = 0 ; y = - 1 ; String sp = " " , tp = " " ; boolean flag = false ; while ( j < n ) { sp += str . charAt ( j ) ; if ( temp . charAt ( j ) == '1' ) { tp += sp + ' , ' ; y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) { flag = true ; break ; } sp = " " ; } j ++ ; } tp += sp ; if ( sp != " " ) { y = Integer . parseInt ( sp ) ; if ( ! primes [ y ] ) flag = true ; } if ( ! flag ) ans . add ( tp ) ; } if ( ans . size ( ) == 0 ) { System . out . println ( - 1 ) ; } for ( String i : ans ) { System . out . println ( i ) ; } } public static void main ( String [ ] args ) { String str = "11373" ; sieve ( ) ; PrimeSplit ( str ) ; } }
class GFG { static long mod = 10000000007L ; static long countWays ( String s , String t , int k ) { int n = s . length ( ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String p = s . substring ( i , n - i ) + s . substring ( 0 , i ) ; if ( p == t ) a ++ ; else b ++ ; } long dp1 [ ] = new long [ k + 1 ] ; long dp2 [ ] = new long [ k + 1 ] ; if ( s == t ) { dp1 [ 0 ] = 1 ; dp2 [ 0 ] = 0 ; } else { dp1 [ 0 ] = 0 ; dp2 [ 0 ] = 1 ; } for ( int i = 1 ; i <= k ; i ++ ) { dp1 [ i ] = ( ( dp1 [ i - 1 ] * ( a - 1 ) ) % mod + ( dp2 [ i - 1 ] * a ) % mod ) % mod ; dp2 [ i ] = ( ( dp1 [ i - 1 ] * ( b ) ) % mod + ( dp2 [ i - 1 ] * ( b - 1 ) ) % mod ) % mod ; } return dp1 [ k ] ; } public static void main ( String [ ] args ) { String S = " ab " , T = " ab " ; int K = 2 ; System . out . print ( countWays ( S , T , K ) ) ; } }
class GFG { static int countSubsequenceUtil ( int ind , int sum , int [ ] A , int N , int [ ] [ ] dp ) { if ( ind == N ) return 1 ; if ( dp [ ind ] [ sum ] != - 1 ) return dp [ ind ] [ sum ] ; if ( A [ ind ] <= sum ) { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) + countSubsequenceUtil ( ind + 1 , sum - A [ ind ] , A , N , dp ) ; } else { dp [ ind ] [ sum ] = countSubsequenceUtil ( ind + 1 , sum , A , N , dp ) ; } return dp [ ind ] [ sum ] ; } static int countSubsequence ( int [ ] A , int N , int X ) { int [ ] [ ] dp = new int [ N ] [ X + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < X + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return countSubsequenceUtil ( 0 , X , A , N , dp ) - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 13 , 40 } , X = 50 ; int N = arr . length ; System . out . print ( countSubsequence ( arr , N , X ) ) ; } }
class GFG { static int waysToDecode2 ( char [ ] s ) { int n = s . length ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; if ( s [ 0 ] == '0' ) return 0 ; dp [ 1 ] = ( ( s [ 0 ] == ' * ' ) ? 9 : 1 ) ; for ( int i = 2 ; i <= n ; i ++ ) { char first = s [ i - 2 ] ; char second = s [ i - 1 ] ; if ( second == ' * ' ) { dp [ i ] += 9 * dp [ i - 1 ] ; } else if ( second > '0' ) dp [ i ] += dp [ i - 1 ] ; if ( first == '1' first == '2' ) { if ( second == ' * ' ) { if ( first == '1' ) dp [ i ] += 9 * dp [ i - 2 ] ; else if ( first == '2' ) dp [ i ] += 6 * dp [ i - 2 ] ; } else if ( ( ( first - '0' ) * 10 + ( second - '0' ) ) <= 26 ) { dp [ i ] += dp [ i - 2 ] ; } } else if ( first == ' * ' ) { if ( second == ' * ' ) { dp [ i ] += 15 * dp [ i - 2 ] ; } else if ( second <= '6' ) { dp [ i ] += 2 * dp [ i - 2 ] ; } else { dp [ i ] += dp [ i - 2 ] ; } } } return dp [ n ] ; } public static void main ( String [ ] args ) { String str = "12*3" ; System . out . print ( waysToDecode2 ( str . toCharArray ( ) ) + "NEW_LINE"); } }
class GFG { static void goodInteger ( int arr [ ] , int n , int h , int l , int r ) { int [ ] [ ] dp = new int [ n + 1 ] [ h ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < h ; j ++ ) dp [ i ] [ j ] = - 1 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < h ; j ++ ) { if ( dp [ i ] [ j ] != - 1 ) { int h1 = ( j + arr [ i ] ) % h ; int h2 = ( j + arr [ i ] - 1 ) % h ; dp [ i + 1 ] [ h1 ] = Math . max ( dp [ i + 1 ] [ h1 ] , dp [ i ] [ j ] + ( ( h1 >= l && h1 <= r ) ? 1 : 0 ) ) ; dp [ i + 1 ] [ h2 ] = Math . max ( dp [ i + 1 ] [ h2 ] , dp [ i ] [ j ] + ( ( h2 >= l && h2 <= r ) ? 1 : 0 ) ) ; } } } int ans = 0 ; for ( int i = 0 ; i < h ; i ++ ) { if ( dp [ n ] [ i ] != - 1 ) ans = Math . max ( ans , dp [ n ] [ i ] ) ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int A [ ] = { 16 , 17 , 14 , 20 , 20 , 11 , 22 } ; int H = 24 ; int L = 21 ; int R = 23 ; int size = A . length ; goodInteger ( A , size , H , L , R ) ; } }
import java . util . * ; class GFG { static class Query { int L , R ; public Query ( int l , int r ) { super ( ) ; L = l ; R = r ; } } ; static void fillPrefixArray ( int arr [ ] , int n , int prefixArray [ ] ) { prefixArray [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { prefixArray [ i ] = prefixArray [ i - 1 ] + arr [ i ] ; } else { prefixArray [ i ] = prefixArray [ i - 1 ] - arr [ i ] ; } } } static int findResultUtil ( int prefixArray [ ] , int L , int R ) { int result ; if ( L == 0 ) { result = prefixArray [ R ] ; } else { result = prefixArray [ R ] - prefixArray [ L - 1 ] ; } if ( L % 2 == 1 ) { result = result * ( - 1 ) ; } return result ; } static void findResult ( int arr [ ] , int n , Query q [ ] , int m ) { int [ ] prefixArray = new int [ n ] ; fillPrefixArray ( arr , n , prefixArray ) ; for ( int i = 0 ; i < m ; i ++ ) { System . out . print ( findResultUtil ( prefixArray , q [ i ] . L , q [ i ] . R ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 13 , 15 , 2 , 45 , 31 , 22 , 3 , 27 } ; int n = arr . length ; Query q [ ] = { new Query ( 2 , 5 ) , new Query ( 6 , 8 ) , new Query ( 1 , 7 ) , new Query ( 4 , 8 ) , new Query ( 0 , 5 ) } ; int m = q . length ; findResult ( arr , n , q , m ) ; } }
import java . util . * ; class GFG { static int validSubArrLength ( int arr [ ] , int N , int K ) { int [ ] dp = new int [ N + 1 ] ; int pos = 0 ; int ans = 0 ; int mx = 0 ; int pre = 0 ; Deque < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { while ( ! q . isEmpty ( ) && arr [ q . getLast ( ) ] < arr [ i ] ) q . removeLast ( ) ; q . add ( i ) ; if ( i == 0 ) { mx = arr [ i ] ; dp [ i ] = arr [ i ] ; } else if ( mx <= arr [ i ] ) { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; mx = arr [ i ] ; } else { dp [ i ] = dp [ i - 1 ] + arr [ i ] ; } if ( pre == 0 ) pos = 0 ; else pos = pre - 1 ; while ( ( i - pre + 1 ) * mx - ( dp [ i ] - dp [ pos ] ) > K && pre < i ) { pos = pre ; pre ++ ; while ( ! q . isEmpty ( ) && q . peek ( ) < pre && pre < i ) { q . removeFirst ( ) ; mx = arr [ q . peek ( ) ] ; } } ans = Math . max ( ans , i - pre + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 8 ; int arr [ ] = { 2 , 7 , 1 , 3 , 4 , 5 } ; System . out . print ( validSubArrLength ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static boolean isKPartitionPossible ( int arr [ ] , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int target = sum / K ; int [ ] dp = new int [ ( 1 << 15 ) ] ; for ( int i = 0 ; i < ( 1 << N ) ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { if ( dp [ mask ] == - 1 ) continue ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ( mask & ( 1 << i ) ) == 0 ) && dp [ mask ] + arr [ i ] <= target ) { dp [ mask ( 1 << i ) ] = ( dp [ mask ] + arr [ i ] ) % target ; } } } if ( dp [ ( 1 << N ) - 1 ] == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) { System . out . print ( " Partitions ▁ into ▁ equal ▁ " ) ; System . out . print ( "sum is possible.NEW_LINE"); } else { System . out . print ( " Partitions ▁ into ▁ equal ▁ " ) ; System . out . print ( "sum is not possible.NEW_LINE"); } } }
import java . util . * ; import java . lang . * ; class GFG { static int printAllSubsetsRec ( int arr [ ] , int n , ArrayList < Integer > v , int sum ) { if ( sum == 0 ) { return ( int ) v . size ( ) ; } if ( sum < 0 ) return Integer . MAX_VALUE ; if ( n == 0 ) return Integer . MAX_VALUE ; int x = printAllSubsetsRec ( arr , n - 1 , v , sum ) ; v . add ( arr [ n - 1 ] ) ; int y = printAllSubsetsRec ( arr , n , v , sum - arr [ n - 1 ] ) ; v . remove ( v . size ( ) - 1 ) ; return Math . min ( x , y ) ; } static int printAllSubsets ( int arr [ ] , int n , int sum ) { ArrayList < Integer > v = new ArrayList < > ( ) ; return printAllSubsetsRec ( arr , n , v , sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 3 , 5 , 6 } ; int sum = 6 ; int n = arr . length ; System . out . println ( printAllSubsets ( arr , n , sum ) ) ; } }
import java . util . * ; class GFG { static int Count ( int S [ ] , int m , int n ) { int [ ] [ ] table = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { table [ 0 ] [ i ] = Integer . MAX_VALUE - 1 ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( S [ i - 1 ] > j ) { table [ i ] [ j ] = table [ i - 1 ] [ j ] ; } else { table [ i ] [ j ] = Math . min ( table [ i - 1 ] [ j ] , table [ i ] [ j - S [ i - 1 ] ] + 1 ) ; } } } return table [ m ] [ n ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 6 , 5 , 1 } ; int m = arr . length ; System . out . print ( Count ( arr , m , 11 ) ) ; } }
class GFG { static final int INF = 10000000 ; static int maximum ( int A , int B , int C , int D ) { return Math . max ( Math . max ( A , B ) , Math . max ( C , D ) ) ; } static int maxProductUtil ( int X , int Y , int [ ] A , int [ ] B , int [ ] [ ] dp ) { if ( X < 0 Y < 0 ) return - INF ; if ( dp [ X ] [ Y ] != - 1 ) return dp [ X ] [ Y ] ; dp [ X ] [ Y ] = maximum ( A [ X ] * B [ Y ] + maxProductUtil ( X - 1 , Y - 1 , A , B , dp ) , A [ X ] * B [ Y ] , maxProductUtil ( X - 1 , Y , A , B , dp ) , maxProductUtil ( X , Y - 1 , A , B , dp ) ) ; return dp [ X ] [ Y ] ; } static int maxProduct ( int A [ ] , int N , int B [ ] , int M ) { int [ ] [ ] dp = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return maxProductUtil ( N - 1 , M - 1 , A , B , dp ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , 6 , - 2 , - 5 } ; int b [ ] = { - 3 , 4 , - 2 , 8 } ; int n = a . length ; int m = b . length ; System . out . print ( maxProduct ( a , n , b , m ) ) ; } }
class GFG { static int minOperation ( int k ) { int dp [ ] = new int [ k + 1 ] ; for ( int i = 1 ; i <= k ; i ++ ) { dp [ i ] = dp [ i - 1 ] + 1 ; if ( i % 2 == 0 ) { dp [ i ] = Math . min ( dp [ i ] , dp [ i / 2 ] + 1 ) ; } } return dp [ k ] ; } public static void main ( String [ ] args ) { int K = 12 ; System . out . print ( minOperation ( K ) ) ; } }
class GFG { static int findSum ( int [ ] arr , int n ) { int totalSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalSum += arr [ i ] ; } int [ ] [ ] dp = new int [ 2 ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 ] [ i ] = 1 ; dp [ 1 ] [ i ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] ) { if ( dp [ 0 ] [ i ] < dp [ 0 ] [ j ] + 1 ) { dp [ 0 ] [ i ] = dp [ 0 ] [ j ] + 1 ; dp [ 1 ] [ i ] = dp [ 1 ] [ j ] + arr [ i ] ; } else if ( dp [ 0 ] [ i ] == dp [ 0 ] [ j ] + 1 ) { dp [ 1 ] [ i ] = Math . min ( dp [ 1 ] [ i ] , dp [ 1 ] [ j ] + arr [ i ] ) ; } } } } int maxm = 0 ; int subtractSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( dp [ 0 ] [ i ] > maxm ) { maxm = dp [ 0 ] [ i ] ; subtractSum = dp [ 1 ] [ i ] ; } else if ( dp [ 0 ] [ i ] == maxm ) { subtractSum = Math . min ( subtractSum , dp [ 1 ] [ i ] ) ; } } return totalSum - subtractSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 1 , 2 , 3 , 8 } ; int n = arr . length ; System . out . print ( findSum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int count ( int n ) { if ( n == 2 ) return 1 ; else if ( n == 4 ) return 2 ; else if ( n == 6 ) return 4 ; int table [ ] = new int [ n + 1 ] ; int i ; for ( i = 0 ; i < n + 1 ; i ++ ) table [ i ] = 0 ; table [ 0 ] = 0 ; table [ 2 ] = 1 ; table [ 4 ] = 2 ; table [ 6 ] = 4 ; for ( i = 8 ; i <= n ; i = i + 2 ) { table [ i ] = table [ i - 2 ] + table [ i - 4 ] + table [ i - 6 ] ; } return table [ n ] ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . print ( count ( n ) ) ; } }
import java . util . * ; class GFG { static final int MOD = 1000000007 ; static boolean isPrime ( String number ) { int num = Integer . valueOf ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) { if ( ( num % i ) == 0 ) return false ; } return num > 1 ? true : false ; } static int countPrimeStrings ( String number , int i ) { if ( i == 0 ) return 1 ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number . charAt ( i - j ) != '0' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += countPrimeStrings ( number , i - j ) ; cnt %= MOD ; } } return cnt ; } public static void main ( String [ ] args ) { String s1 = "3175" ; int l = s1 . length ( ) ; System . out . print ( countPrimeStrings ( s1 , l ) ) ; } }
import java . util . * ; class GFG { static int MOD = 1000000007 ; static boolean [ ] sieve = new boolean [ 1000000 ] ; static void buildSieve ( ) { Arrays . fill ( sieve , true ) ; sieve [ 0 ] = false ; sieve [ 1 ] = false ; for ( int p = 2 ; p * p <= 1000000 ; p ++ ) { if ( sieve [ p ] == true ) { for ( int i = p * p ; i < 1000000 ; i += p ) sieve [ i ] = false ; } } } static boolean isPrime ( String number ) { int num = Integer . valueOf ( number ) ; return sieve [ num ] ; } static int rec ( String number , int i , int [ ] dp ) { if ( dp [ i ] != - 1 ) return dp [ i ] ; int cnt = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 && number . charAt ( i - j ) != '0' && isPrime ( number . substring ( i - j , i ) ) ) { cnt += rec ( number , i - j , dp ) ; cnt %= MOD ; } } return dp [ i ] = cnt ; } static int countPrimeStrings ( String number ) { int n = number . length ( ) ; int [ ] dp = new int [ n + 1 ] ; Arrays . fill ( dp , - 1 ) ; dp [ 0 ] = 1 ; return rec ( number , n , dp ) ; } public static void main ( String [ ] args ) { buildSieve ( ) ; String s1 = "3175" ; System . out . print ( countPrimeStrings ( s1 ) + "NEW_LINE"); } }
class GFG { public static void CountWays ( int a , int b , int c ) { int x = b + c + 1 ; int [ ] arr = new int [ x ] ; for ( int i = a ; i <= b ; i ++ ) { for ( int j = b ; j <= c ; j ++ ) { arr [ i + j ] ++ ; } } for ( int i = 1 ; i < x ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
import java . util . * ; class GFG { static void CountWays ( int a , int b , int c ) { int x = b + c + 2 ; int arr [ ] = new int [ x ] ; for ( int i = 1 ; i <= b ; i ++ ) { arr [ i + b ] ++ ; arr [ i + c + 1 ] -- ; } for ( int i = 1 ; i < x - 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int a = 1 ; int b = 2 ; int c = 2 ; CountWays ( a , b , c ) ; } }
import java . io . * ; class GFG { static void differentStrings ( String s ) { int n = s . length ( ) ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) && ( s . charAt ( i ) == '1' || s . charAt ( i ) == '2' ) ) dp [ i + 1 ] = dp [ i ] + dp [ i - 1 ] ; else dp [ i + 1 ] = dp [ i ] ; } System . out . println ( dp [ n ] ) ; } public static void main ( String [ ] args ) { String S = "0111022110" ; differentStrings ( S ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int ways ( int [ ] [ ] arr , int K ) { int R = arr . length ; int C = arr [ 0 ] . length ; int [ ] [ ] preSum = new int [ R ] [ C ] ; for ( int r = R - 1 ; r >= 0 ; r -- ) { for ( int c = C - 1 ; c >= 0 ; c -- ) { preSum [ r ] = arr [ r ] ; if ( r + 1 < R ) preSum [ r ] += preSum [ r + 1 ] ; if ( c + 1 < C ) preSum [ r ] += preSum [ r ] ; if ( r + 1 < R && c + 1 < C ) preSum [ r ] -= preSum [ r + 1 ] ; } } int [ ] [ ] [ ] dp = new int [ K + 1 ] [ R ] [ C ] ; for ( int k = 1 ; k <= K ; k ++ ) { for ( int r = R - 1 ; r >= 0 ; r -- ) { for ( int c = C - 1 ; c >= 0 ; c -- ) { if ( k == 1 ) { dp [ k ] [ r ] = ( preSum [ r ] > 0 ) ? 1 : 0 ; } else { dp [ k ] [ r ] = 0 ; for ( int r1 = r + 1 ; r1 < R ; r1 ++ ) { if ( preSum [ r ] - preSum [ r1 ] > 0 ) dp [ k ] [ r ] += dp [ k - 1 ] [ r1 ] ; } for ( int c1 = c + 1 ; c1 < C ; c1 ++ ) { if ( preSum [ r ] - preSum [ r ] [ c1 ] > 0 ) dp [ k ] [ r ] += dp [ k - 1 ] [ r ] [ c1 ] ; } } } } } return dp [ K ] [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 0 , 0 } , { 1 , 1 , 1 } , { 0 , 0 , 0 } } ; int k = 3 ; System . out . println ( ways ( arr , k ) ) ; } }
import java . util . * ; class GFG { static int Col = 3 ; static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int sumPathCost ( int grid [ ] [ ] , int m , int n ) { int sum = 0 , count ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { count = nCr ( i + j , i ) * nCr ( m + n - i - j , m - i ) ; sum += count * grid [ i ] [ j ] ; } } return sum ; } public static void main ( String [ ] args ) { int m = 2 ; int n = 2 ; int grid [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; System . out . println ( sumPathCost ( grid , m , n ) ) ; } }
import java . util . * ; class GFG { static int minSumProd ( String S , int K ) { int len = S . length ( ) ; if ( K > len ) return - 1 ; if ( K == len ) return 0 ; int [ ] dp = new int [ len ] ; int count_zero = 0 , count_one = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( S . charAt ( j ) == '0' ) count_zero ++ ; else count_one ++ ; dp [ j ] = count_zero * count_one ; } for ( int i = 1 ; i < K ; i ++ ) { for ( int j = len - 1 ; j >= i ; j -- ) { count_zero = 0 ; count_one = 0 ; dp [ j ] = Integer . MAX_VALUE ; for ( int k = j ; k >= i ; k -- ) { if ( S . charAt ( k ) == '0' ) count_zero ++ ; else count_one ++ ; dp [ j ] = Math . min ( dp [ j ] , count_zero * count_one + dp [ k - 1 ] ) ; } } } return dp [ len - 1 ] ; } public static void main ( String [ ] args ) { String S = "1011000110110100" ; int K = 5 ; System . out . print ( minSumProd ( S , K ) ) ; } }
import java . util . * ; class GFG { static int minPrizes ( int arr [ ] , int n ) { int [ ] dpLeft = new int [ n ] ; dpLeft [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) { dpLeft [ i ] = dpLeft [ i - 1 ] + 1 ; } else { dpLeft [ i ] = 1 ; } } int [ ] dpRight = new int [ n ] ; dpRight [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > arr [ i + 1 ] ) { dpRight [ i ] = dpRight [ i + 1 ] + 1 ; } else { dpRight [ i ] = 1 ; } } int totalPrizes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { totalPrizes += Math . max ( dpLeft [ i ] , dpRight [ i ] ) ; } System . out . print ( totalPrizes + "NEW_LINE"); return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; minPrizes ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static int get_max_sum ( int arr [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { int x = freq . get ( arr [ i ] ) ; freq . replace ( arr [ i ] , x + 1 ) ; } else freq . put ( arr [ i ] , 1 ) ; } int [ ] dp = new int [ 100001 ] ; for ( int i = 0 ; i < 100001 ; i ++ ) dp [ i ] = 0 ; dp [ 0 ] = 0 ; if ( freq . containsKey ( 0 ) ) dp [ 1 ] = freq . get ( 0 ) ; else dp [ 1 ] = 0 ; for ( int i = 2 ; i <= 100000 ; i ++ ) { int temp = ( freq . containsKey ( i ) ) ? freq . get ( i ) : 0 ; dp [ i ] = Math . max ( dp [ i - 1 ] , dp [ i - 2 ] + i * temp ) ; } return dp [ 100000 ] ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 2 , 2 , 3 } ; System . out . println ( get_max_sum ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minFlip ( String a ) { int n = a . length ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ 2 ] ; if ( a . charAt ( 0 ) == '1' ) { dp [ 0 ] [ 0 ] = 1 ; } else dp [ 0 ] [ 0 ] = 0 ; if ( a . charAt ( 0 ) == '0' ) dp [ 0 ] [ 1 ] = 1 ; else dp [ 0 ] [ 1 ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a . charAt ( i ) == '1' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + 1 ; } else dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; if ( a . charAt ( i ) == '0' ) { dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + 1 ; } else dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; } int answer = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { answer = Math . min ( answer , dp [ i ] [ 1 ] + dp [ n - 1 ] [ 0 ] - dp [ i ] [ 0 ] ) ; } return Math . min ( answer , dp [ n - 1 ] [ 0 ] ) ; } public static void main ( String [ ] args ) { String s = "1100111000101" ; System . out . println ( minFlip ( s ) ) ; } }
import java . util . * ; class GFG { static int LIIDS ( int arr [ ] , int N ) { int [ ] dp = new int [ N + 1 ] ; int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 1 ; } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i + i ; j <= N ; j += i ) { if ( j < arr . length && arr [ j ] > arr [ i ] ) { dp [ j ] = Math . max ( dp [ j ] , dp [ i ] + 1 ) ; } } ans = Math . max ( ans , dp [ i ] ) ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 7 , 9 , 10 } ; int N = arr . length ; System . out . println ( LIIDS ( arr , N ) ) ; } }
class GFG { static int mod = 1000000007 ; static int calculate ( int pos , int left , int k , int L , int R ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; int answer = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( i > left ) break ; answer = ( ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod ) ; } return answer ; } static int countWaystoDivide ( int n , int k , int L , int R ) { return calculate ( 0 , n , k , L , R ) ; } public static void main ( String [ ] args ) { int N = 12 ; int K = 3 ; int L = 1 ; int R = 5 ; System . out . print ( countWaystoDivide ( N , K , L , R ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void PossibleSum ( int n , int m , int [ ] [ ] v , int k ) { int [ ] [ ] dp = new int [ n + 1 ] [ k + 1 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= k ; j ++ ) { if ( dp [ i ] [ j ] == 1 ) { for ( int d = 0 ; d < m ; d ++ ) { if ( ( j + v [ i ] [ d ] ) <= k ) { dp [ i + 1 ] [ j + v [ i ] [ d ] ] = 1 ; } } } } } if ( dp [ n ] [ k ] == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 2 , M = 10 , K = 5 ; int [ ] [ ] arr = new int [ ] [ ] { { 4 , 0 , 15 , 3 , 2 , 20 , 10 , 1 , 5 , 4 } , { 4 , 0 , 10 , 3 , 2 , 25 , 4 , 1 , 5 , 4 } } ; PossibleSum ( N , M , arr , K ) ; } }
import java . util . * ; class GFG { static boolean [ ] dp = new boolean [ 1 << 20 ] ; static boolean isprime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static boolean solve ( int arr [ ] , int curr , int mask , int n ) { if ( isprime ( curr ) ) { if ( mask == ( 1 << n ) - 1 ) { return true ; } curr = 0 ; } if ( mask == ( 1 << n ) - 1 ) { if ( ! isprime ( curr ) ) { return false ; } } if ( dp [ mask ] ) return dp [ mask ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( mask & ( 1 << i ) ) == 0 ) { if ( solve ( arr , curr + arr [ i ] , mask 1 << i , n ) ) { return true ; } } } return dp [ mask ] = false ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 7 , 13 } ; int n = arr . length ; if ( solve ( arr , 0 , 0 , n ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }
import java . util . * ; class GFG { static int p = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void nCr ( int n , int p , int f [ ] [ ] , int m ) { for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( j > i ) { f [ i ] [ j ] = 0 ; } else if ( j == 0 j == i ) { f [ i ] [ j ] = 1 ; } else { f [ i ] [ j ] = ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % p ; } } } } static void ProductOfSubsets ( int arr [ ] , int n , int m ) { int [ ] [ ] f = new int [ n + 1 ] [ 100 ] ; nCr ( n , p - 1 , f , m ) ; Arrays . sort ( arr ) ; long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) { x = ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 7 , 9 , 3 } ; int K = 4 ; int N = arr . length ; ProductOfSubsets ( arr , N , K ) ; } }
import java . util . * ; class GFG { static Vector < Integer > dp = new Vector < > ( ) ; static int maxSum ( int i , Vector < Integer > subarr ) { if ( i >= subarr . size ( ) ) return 0 ; if ( dp . get ( i ) != - 1 ) return dp . get ( i ) ; dp . add ( i , Math . max ( maxSum ( i + 1 , subarr ) , subarr . get ( i ) + maxSum ( i + 2 , subarr ) ) ) ; return dp . get ( i ) ; } static void Func ( Vector < Integer > arr ) { Vector < Integer > subarr = new Vector < > ( ) ; subarr . addAll ( arr ) ; subarr . remove ( subarr . size ( ) - 1 ) ; dp . setSize ( subarr . size ( ) ) ; Collections . fill ( dp , - 1 ) ; int max1 = maxSum ( 0 , subarr ) ; subarr = arr ; subarr . remove ( 0 ) ; dp . clear ( ) ; dp . setSize ( subarr . size ( ) ) ; Collections . fill ( dp , - 1 ) ; int max2 = maxSum ( 0 , subarr ) ; System . out . print ( Math . max ( max1 , max2 ) + "NEW_LINE"); } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < > ( ) ; arr . add ( 1 ) ; arr . add ( 2 ) ; arr . add ( 3 ) ; arr . add ( 1 ) ; Func ( arr ) ; }
import java . util . * ; class GFG { static int MAX = 100 ; static int dp [ ] [ ] [ ] = new int [ 1000 ] [ MAX ] [ MAX ] ; static int productDigitSum ( int x , int y ) { int sumx = 0 ; while ( x > 0 ) { sumx += ( x % 10 ) ; x /= 10 ; } int sumy = 0 ; while ( y > 0 ) { sumy += ( y % 10 ) ; y /= 10 ; } return ( sumx * sumy ) ; } static int solve ( int arr [ ] , int i , int len , int prev , int n , int k ) { if ( len == k ) return 0 ; if ( i == n ) return Integer . MIN_VALUE ; if ( dp [ i ] [ len ] [ prev ] != 0 ) return dp [ i ] [ len ] [ prev ] ; int inc = 0 , exc = 0 ; if ( ( len & 1 ) != 0 ) { inc = ( productDigitSum ( arr [ prev ] , arr [ i ] ) + solve ( arr , i + 1 , len + 1 , 0 , n , k ) ) ; } else { inc = solve ( arr , i + 1 , len + 1 , i , n , k ) ; } exc = solve ( arr , i + 1 , len , prev , n , k ) ; return dp [ i ] [ len ] [ prev ] = Math . max ( inc , exc ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 9 , 101 , 24 , 2 , 20 , 14 } ; int n = arr . length ; int k = 6 ; System . out . print ( solve ( arr , 0 , 0 , 0 , n , k ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > [ ] factors = new Vector [ 100005 ] ; static int [ ] dp = new int [ 100005 ] ; static void precompute ( ) { for ( int i = 0 ; i < factors . length ; i ++ ) factors [ i ] = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= 100000 ; i ++ ) { for ( int j = i ; j <= 100000 ; j += i ) { factors [ j ] . add ( i ) ; } } } static int solve ( int arr [ ] , int k , int n ) { if ( k == n - 1 ) { return 0 ; } if ( k >= n ) { return Integer . MAX_VALUE ; } if ( dp [ k ] != 0 ) { return dp [ k ] ; } int ans = Integer . MAX_VALUE ; for ( int j : factors [ arr [ k ] ] ) { int res = solve ( arr , k + j , n ) ; if ( res != Integer . MAX_VALUE ) { ans = Math . min ( ans , res + 1 ) ; } } return dp [ k ] = ans ; } public static void main ( String [ ] args ) { precompute ( ) ; int arr [ ] = { 2 , 8 , 16 , 55 , 99 , 100 } ; int n = arr . length ; System . out . print ( solve ( arr , 0 , n ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ ] [ ] grid ) { Vector < pair > one = new Vector < pair > ( ) ; int M = grid . length ; int N = grid [ 0 ] . length ; int ans = - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) one . add ( new pair ( i , j ) ) ; } } if ( one . isEmpty ( ) || M * N == one . size ( ) ) return - 1 ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) continue ; int dist = Integer . MAX_VALUE ; for ( pair p : one ) { int d = Math . abs ( p . first - i ) + Math . abs ( p . second - j ) ; dist = Math . min ( dist , d ) ; if ( dist <= ans ) break ; } ans = Math . max ( ans , dist ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 0 } } ; System . out . print ( maxDistance ( arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int maxDistance ( int [ ] [ ] grid ) { Queue < pair > q = new LinkedList < pair > ( ) ; int M = grid . length ; int N = grid [ 0 ] . length ; int ans = - 1 ; int dirs [ ] [ ] = { { 0 , 1 } , { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } } ; for ( int i = 0 ; i < M ; ++ i ) { for ( int j = 0 ; j < N ; ++ j ) { if ( grid [ i ] [ j ] == 1 ) q . add ( new pair ( i , j ) ) ; } } if ( q . isEmpty ( ) || M * N == q . size ( ) ) return - 1 ; while ( q . size ( ) > 0 ) { int cnt = q . size ( ) ; while ( cnt -- > 0 ) { pair p = q . peek ( ) ; q . remove ( ) ; for ( int [ ] dir : dirs ) { int x = p . first + dir [ 0 ] ; int y = p . second + dir [ 1 ] ; if ( x < 0 x >= M y < 0 y >= N grid [ x ] [ y ] > 0 ) continue ; q . add ( new pair ( x , y ) ) ; grid [ x ] [ y ] = 1 ; } } ++ ans ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 0 , 1 } , { 0 , 0 , 0 } , { 0 , 0 , 1 } } ; System . out . print ( maxDistance ( arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int [ ] [ ] dp ; static int f ( int n , int k ) { if ( n < k ) return 0 ; if ( n == k ) return 1 ; if ( k == 1 ) return 1 ; if ( dp [ n ] [ k ] != - 1 ) return dp [ n ] [ k ] ; return dp [ n ] [ k ] = ( ( k * f ( n - 1 , k ) ) % mod + ( f ( n - 1 , k - 1 ) ) % mod ) % mod ; } static long operation ( int n ) { dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } long ans = 0 , fac = 1 ; for ( int k = 1 ; k <= n ; k ++ ) { fac *= k ; ans = ( ans + ( fac * f ( n , k ) ) % mod ) % mod ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( operation ( n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int countWays ( int n , int m ) { int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { dp [ 1 ] [ i ] = 1 ; } int sum ; for ( int i = 2 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { sum = 0 ; for ( int k = 0 ; k <= j ; k ++ ) { sum += dp [ i - 1 ] [ k ] ; } dp [ i ] [ j ] = sum ; } } return dp [ m ] [ n ] ; } public static void main ( String [ ] args ) { int N = 2 , K = 3 ; System . out . print ( countWays ( N , K ) ) ; } }
class GFG { static int CountSets ( int x , int pos ) { if ( x <= 0 ) { if ( pos == 0 ) return 1 ; else return 0 ; } if ( pos == 0 ) return 1 ; int answer = CountSets ( x - 1 , pos ) + CountSets ( x - 2 , pos - 1 ) ; return answer ; } static int CountOrderedSets ( int n ) { int [ ] factorial = new int [ 10000 ] ; factorial [ 0 ] = 1 ; for ( int i = 1 ; i < 10000 ; i ++ ) factorial [ i ] = factorial [ i - 1 ] * i ; int answer = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int sets = CountSets ( n , i ) * factorial [ i ] ; answer = answer + sets ; } return answer ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( CountOrderedSets ( N ) ) ; } }
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 ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static final int MAX = 100005 ; static int totBackEdges ; static int [ ] countAdj = new int [ MAX ] ; static int [ ] small = new int [ MAX ] ; static int [ ] isPossible = new int [ MAX ] ; static int [ ] depth = new int [ MAX ] ; @ SuppressWarnings ( " unchecked " ) static ArrayList < Integer > [ ] adj = new ArrayList [ MAX ] ; static int [ ] vis = new int [ MAX ] ; static void change ( Pair p , int x ) { if ( p . second > x ) p . second = x ; if ( p . first > p . second ) { int tmp = p . first ; p . first = p . second ; p . second = tmp ; } } static Pair dfs ( int v , int p , int de ) { Pair answer = new Pair ( 100000000 , 100000000 ) ; depth [ v ] = de ; vis [ v ] = 1 ; isPossible [ v ] = 1 ; for ( int u : adj [ v ] ) { if ( ( u ^ p ) != 0 ) { if ( vis [ u ] == 0 ) { Pair x = dfs ( u , v , de + 1 ) ; small [ v ] += small [ u ] ; change ( answer , x . second ) ; change ( answer , x . first ) ; if ( x . second < de ) isPossible [ v ] = 0 ; } else if ( vis [ u ] == 1 ) { totBackEdges ++ ; countAdj [ v ] ++ ; countAdj [ u ] ++ ; small [ p ] ++ ; small [ u ] -- ; change ( answer , depth [ u ] ) ; } } } vis [ v ] = 2 ; return answer ; } static int minNodetoRemove ( int n , ArrayList < Pair > edges ) { for ( int i = 0 ; i < edges . size ( ) ; i ++ ) { adj [ edges . get ( i ) . first ] . add ( edges . get ( i ) . second ) ; adj [ edges . get ( i ) . second ] . add ( edges . get ( i ) . first ) ; } Arrays . fill ( vis , 0 ) ; totBackEdges = 0 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( vis [ v ] == 0 ) dfs ( v , - 1 , 0 ) ; } if ( totBackEdges == 0 ) return - 1 ; int node = - 1 ; for ( int v = 1 ; v <= n ; v ++ ) { if ( ( countAdj [ v ] + small [ v ] == totBackEdges ) && isPossible [ v ] != 0 ) { node = v ; } if ( node != - 1 ) break ; } return node ; } public static void main ( String [ ] args ) { int N = 5 ; ArrayList < Pair > edges = new ArrayList < > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) { adj [ i ] = new ArrayList < > ( ) ; } edges . add ( new Pair ( 5 , 1 ) ) ; edges . add ( new Pair ( 5 , 2 ) ) ; edges . add ( new Pair ( 1 , 2 ) ) ; edges . add ( new Pair ( 2 , 3 ) ) ; edges . add ( new Pair ( 2 , 4 ) ) ; System . out . println ( minNodetoRemove ( N , edges ) ) ; } }
import java . util . * ; class GFG { static final int N = 3 ; static final int M = 5 ; static int Large = ( int ) 1e6 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] isPrime = new boolean [ Large + 1 ] ; Arrays . fill ( isPrime , true ) ; for ( int p = 2 ; p * p <= Large ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= Large ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= Large ; p ++ ) if ( isPrime [ p ] ) prime . add ( p ) ; } static void Display ( int arr [ ] [ ] , int row ) { for ( int i = 0 ; i < M ; i ++ ) System . out . print ( arr [ row ] [ i ] + " ▁ " ) ; } static void countDivisorsMult ( int arr [ ] [ ] ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int row_no = 0 ; long max_factor = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int no = arr [ i ] [ j ] ; for ( int k = 0 ; k < prime . size ( ) ; k ++ ) { while ( no > 1 && no % prime . get ( k ) == 0 ) { no /= prime . get ( k ) ; if ( mp . containsKey ( prime . get ( k ) ) ) mp . put ( prime . get ( k ) , prime . get ( k ) + 1 ) ; else mp . put ( prime . get ( k ) , 1 ) ; } if ( no == 1 ) break ; } } int res = 1 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { res *= ( it . getValue ( ) + 1L ) ; } if ( max_factor < res ) { row_no = i ; max_factor = res ; } mp . clear ( ) ; } Display ( arr , row_no ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 10 , 23 } , { 4 , 5 , 6 , 7 , 8 } , { 7 , 8 , 9 , 15 , 45 } } ; SieveOfEratosthenes ( ) ; countDivisorsMult ( arr ) ; } }
class GFG { static int isSquare ( int n ) { int k = ( int ) Math . sqrt ( n ) ; return k * k == n ? 1 : 0 ; } static int calculate ( int pos , int prev , int sum , int [ ] v ) { if ( pos == v . length ) return isSquare ( sum ) ; for ( int i = prev ; i <= 9 ; i ++ ) { v [ pos ] = i ; sum += i * i ; if ( calculate ( pos + 1 , i , sum , v ) != 0 ) return 1 ; sum -= i * i ; } return 0 ; } static String minValue ( int n ) { int [ ] v = new int [ n ] ; if ( calculate ( 0 , 1 , 0 , v ) != 0 ) { String answer = " " ; for ( int i = 0 ; i < v . length ; i ++ ) answer += ( char ) ( v [ i ] + '0' ) ; return answer ; } else return " - 1" ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( minValue ( N ) ) ; } }
class GFG { static int MOD = ( int ) 1e9 + 7 ; static int calculate ( int pos , int g , int n , int k ) { if ( pos == n ) { return g ; } int answer = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { answer = ( answer % MOD + calculate ( pos + 1 , __gcd ( g , i ) , n , k ) % MOD ) ; answer %= MOD ; } return answer ; } static int sumofGCD ( int n , int k ) { return calculate ( 0 , 0 , n , k ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; System . out . print ( sumofGCD ( N , K ) ) ; } }
class GFG { static int MOD = ( int ) 1e9 + 7 ; static int fastexpo ( int a , int b ) { int res = 1 ; a %= MOD ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res * a ) % MOD ; a *= a ; a %= MOD ; b >>= 1 ; } return res ; } static int sumofGCD ( int n , int k ) { int [ ] count = new int [ k + 1 ] ; for ( int g = k ; g >= 1 ; g -- ) { int count_multiples = k / g ; int temp ; temp = fastexpo ( count_multiples , n ) ; temp %= MOD ; int extra = 0 ; for ( int j = g * 2 ; j <= k ; j += g ) { extra = ( extra + count [ j ] ) ; extra %= MOD ; } count [ g ] = ( temp - extra + MOD ) ; count [ g ] %= MOD ; } int sum = 0 ; int add ; for ( int i = 1 ; i <= k ; ++ i ) { add = ( count [ i ] % MOD * i % MOD ) ; add %= MOD ; sum += add ; sum %= MOD ; } return sum ; } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; System . out . print ( sumofGCD ( N , K ) ) ; } }
class GFG { static int checkOdd ( String number ) { int n = number . length ( ) ; int num = number . charAt ( n - 1 ) - '0' ; return ( num & 1 ) ; } static int splitIntoOdds ( String number ) { int numLen = number . length ( ) ; int splitDP [ ] = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . substring ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . substring ( i , i + j ) ) == 1 ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . println ( splitIntoOdds ( "123456789123456789123" ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; class Graph { static int m , n , d ; static ArrayList < Integer > v = new ArrayList < > ( ) ; static final int k = ( int ) 1e9 + 7 ; static int [ ] [ ] [ ] dp = new int [ 2001 ] [ 2001 ] [ 2 ] ; static void init ( StringBuilder l ) { for ( int i = 0 ; i < 2001 ; i ++ ) { for ( int j = 0 ; j < 2001 ; j ++ ) { for ( int k = 0 ; k < 2 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } v . clear ( ) ; for ( int i = 0 ; i < l . length ( ) ; i ++ ) { v . add ( l . charAt ( i ) - '0' ) ; } n = l . length ( ) ; } static String number_minus_one ( StringBuilder a ) { String s = a . substring ( 1 ) ; String s1 = " " ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) s1 += '0' ; if ( a . charAt ( 0 ) == '1' && s . compareTo ( s1 ) == 0 ) { int l = s . length ( ) ; a = new StringBuilder ( " " ) ; a . append ( '0' ) ; for ( int i = 0 ; i < l ; i ++ ) a . append ( '9' ) ; } else { for ( int i = a . length ( ) - 1 ; i >= 0 ; i -- ) { if ( a . charAt ( i ) == '0' ) a . setCharAt ( i , '9' ) ; else { a . setCharAt ( i , ( char ) ( ( ( a . charAt ( i ) - '0' ) - 1 ) + '0' ) ) ; break ; } } } return a . toString ( ) ; } static int fun ( int pos , int sum , int f ) { if ( pos == n ) { if ( sum == 0 ) { return 1 ; } return 0 ; } if ( dp [ pos ] [ sum ] [ f ] != - 1 ) return dp [ pos ] [ sum ] [ f ] ; int lmt = 9 ; if ( f == 0 ) lmt = v . get ( pos ) ; int ans = 0 ; for ( int i = 0 ; i <= lmt ; i ++ ) { if ( i == d && pos % 2 == 1 ) ans += 0 ; else if ( i != d && pos % 2 == 0 ) ans += 0 ; else { int new_f = f ; if ( f == 0 && i < lmt ) new_f = 1 ; int new_sum = sum ; new_sum *= 10 ; new_sum += i ; new_sum %= m ; ans += fun ( pos + 1 , new_sum , new_f ) ; ans %= k ; } } return dp [ pos ] [ sum ] [ f ] = ans ; } static void operations ( StringBuilder L , StringBuilder R ) { init ( R ) ; int ans = fun ( 0 , 0 , 0 ) ; L = new StringBuilder ( number_minus_one ( L ) ) ; init ( L ) ; ans -= fun ( 0 , 0 , 0 ) ; if ( ans < 0 ) ans += k ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { m = 2 ; d = 2 ; int Q = 1 ; StringBuilder [ ] [ ] arr = { { new StringBuilder ( "20" ) , new StringBuilder ( "32" ) } } ; for ( int i = 0 ; i < Q ; i ++ ) { operations ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }
import java . util . * ; class GFG { static boolean checkPrime ( String number ) { int num = Integer . valueOf ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { if ( number . length ( ) == 0 ) return 0 ; if ( number . length ( ) <= 6 && checkPrime ( number ) ) return 1 ; else { int numLen = number . length ( ) ; int ans = 1000000 ; for ( int i = 1 ; i <= 6 && i <= numLen ; i ++ ) { if ( checkPrime ( number . substring ( 0 , i ) ) ) { int val = splitIntoPrimes ( number . substring ( i ) ) ; if ( val != - 1 ) { ans = Math . min ( ans , 1 + val ) ; } } } if ( ans == 1000000 ) return - 1 ; return ans ; } } public static void main ( String [ ] args ) { System . out . print ( splitIntoPrimes ( "13499315" ) + "NEW_LINE"); System . out . print ( splitIntoPrimes ( "43" ) + "NEW_LINE"); } }
class Geeks { static int k_nonzero_numbers ( String s , int n , int k ) { int dp [ ] [ ] [ ] = new int [ n + 1 ] [ 2 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) for ( int x = 0 ; x <= k ; x ++ ) dp [ i ] [ j ] [ x ] = 0 ; dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; ++ i ) { int sm = 0 ; while ( sm < 2 ) { for ( int j = 0 ; j < k + 1 ; ++ j ) { int x = 0 ; while ( x <= ( sm != 0 ? 9 : s . charAt ( i ) - '0' ) ) { if ( j + ( x > 0 ? 1 : 0 ) < k + 1 ) { dp [ i + 1 ] [ ( sm != 0 x < ( s . charAt ( i ) - '0' ) ) ? 1 : 0 ] [ j + ( x > 0 ? 1 : 0 ) ] += dp [ i ] [ sm ] [ j ] ; } ++ x ; } } ++ sm ; } } return dp [ n ] [ 0 ] [ k ] + dp [ n ] [ 1 ] [ k ] ; } public static void main ( String [ ] args ) { String s = "25" ; int k = 2 ; int n = s . length ( ) ; System . out . println ( k_nonzero_numbers ( s , n , k ) ) ; } }
class GFG { static int distinctChars ( String s ) { int freq [ ] = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } static int waysToSplit ( String s ) { int n = s . length ( ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { String left = s . substring ( 0 , i ) ; String right = s . substring ( i , n ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { String s = " ababa " ; System . out . print ( waysToSplit ( s ) ) ; } }
class GFG { static int waysToSplit ( String s ) { int n = s . length ( ) ; int answer = 0 ; int prefix [ ] = new int [ n ] ; int suffix [ ] = new int [ n ] ; int seen [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { int prev = ( i - 1 >= 0 ? prefix [ i - 1 ] : 0 ) ; if ( seen [ s . charAt ( i ) - ' a ' ] == 0 ) { prefix [ i ] += ( prev + 1 ) ; } else prefix [ i ] = prev ; seen [ s . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < 26 ; i ++ ) seen [ i ] = 0 ; suffix [ n - 1 ] = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int prev = suffix [ i ] ; if ( seen [ s . charAt ( i ) - ' a ' ] == 0 ) { suffix [ i - 1 ] += ( prev + 1 ) ; } else suffix [ i - 1 ] = prev ; seen [ s . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( prefix [ i ] == suffix [ i ] ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { String s = " ababa " ; System . out . println ( waysToSplit ( s ) ) ; } }
class GFG { static int maxSum ( int p0 , int p1 , int a [ ] , int pos , int n ) { if ( pos == n ) { if ( p0 == p1 ) return p0 ; else return 0 ; } int ans = maxSum ( p0 , p1 , a , pos + 1 , n ) ; ans = Math . max ( ans , maxSum ( p0 + a [ pos ] , p1 , a , pos + 1 , n ) ) ; ans = Math . max ( ans , maxSum ( p0 , p1 + a [ pos ] , a , pos + 1 , n ) ) ; return ans ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] = { 1 , 2 , 3 , 6 } ; System . out . println ( maxSum ( 0 , 0 , a , 0 , n ) ) ; } }
class GFG { final static int INT_MIN = Integer . MIN_VALUE ; static int maxSum ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; int limit = 2 * sum + 1 ; int dp [ ] [ ] = new int [ n + 1 ] [ limit ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < limit ; j ++ ) dp [ i ] [ j ] = INT_MIN ; } dp [ 0 ] [ sum ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < limit ; j ++ ) { if ( ( j - a [ i - 1 ] ) >= 0 && dp [ i - 1 ] [ j - a [ i - 1 ] ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - a [ i - 1 ] ] + a [ i - 1 ] ) ; if ( ( j + a [ i - 1 ] ) < limit && dp [ i - 1 ] [ j + a [ i - 1 ] ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j + a [ i - 1 ] ] ) ; if ( dp [ i - 1 ] [ j ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ sum ] ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] a = { 1 , 2 , 3 , 6 } ; System . out . println ( maxSum ( a , n ) ) ; } }
class GFG { static long Count_Paths ( int x , int y ) { if ( x == 0 && y == 0 ) return 0 ; if ( x == 0 y == 0 ) return 1 ; int [ ] dp = new int [ Math . max ( x , y ) + 1 ] ; int p = Math . max ( x , y ) , q = Math . min ( x , y ) ; for ( int i = 0 ; i <= p ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i <= q ; i ++ ) for ( int j = 1 ; j <= p ; j ++ ) dp [ j ] += dp [ j - 1 ] ; return dp [ p ] ; } public static void main ( String [ ] args ) { int x = 3 , y = 3 ; System . out . print ( " Number ▁ of ▁ Paths ▁ " + Count_Paths ( x , y ) ) ; } }
import java . util . * ; class GFG { static final int MAX_SIZE = 100005 ; static int [ ] fib = new int [ MAX_SIZE + 1 ] ; static void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static void printLevel ( int level ) { int left_index = ( int ) Math . pow ( 2 , level - 1 ) ; int right_index = ( int ) ( Math . pow ( 2 , level ) - 1 ) ; for ( int i = left_index ; i <= right_index ; i ++ ) { System . out . print ( fib [ i - 1 ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; } }
import java . util . * ; class GFG { static int calculate ( int pos , int prev , int left , int k ) { if ( pos == k ) { if ( left == 0 ) return 1 ; else return 0 ; } if ( left == 0 ) return 0 ; int answer = 0 ; for ( int i = prev ; i <= left ; i ++ ) { answer += calculate ( pos + 1 , i , left - i , k ) ; } return answer ; } static int countWaystoDivide ( int n , int k ) { return calculate ( 0 , 1 , n , k ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 4 ; System . out . print ( countWaystoDivide ( N , K ) ) ; } }
import java . util . * ; class GFG { static int maxScore ( int l , int r , int prefix_sum [ ] , int num ) { if ( l > r ) return 0 ; int current_sum = prefix_sum [ r ] - ( l - 1 >= 0 ? prefix_sum [ l - 1 ] : 0 ) ; if ( num % 2 == 0 ) current_sum *= - 1 ; return current_sum + Math . max ( maxScore ( l + 1 , r , prefix_sum , num + 1 ) , maxScore ( l , r - 1 , prefix_sum , num + 1 ) ) ; } static int findMaxScore ( int a [ ] , int n ) { int prefix_sum [ ] = new int [ n ] ; prefix_sum [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = prefix_sum [ i - 1 ] + a [ i ] ; } return maxScore ( 0 , n - 1 , prefix_sum , 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; int A [ ] = { 1 , 2 , 3 , 4 , 2 , 6 } ; System . out . print ( findMaxScore ( A , n ) ) ; } }
class GFG { static int fib [ ] = new int [ 100005 ] ; static void computeFibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 100005 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static int countString ( String str ) { int ans = 1 ; int cnt = 1 ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) { cnt ++ ; } else { ans = ans * fib [ cnt ] ; cnt = 1 ; } } ans = ans * fib [ cnt ] ; return ans ; } public static void main ( String [ ] args ) { String str = " abdllldefkkkk " ; computeFibonacci ( ) ; System . out . println ( countString ( str ) ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Integer > Map = new HashMap < Integer , Integer > ( ) ; static void findPrimeFactors ( int N ) { int i ; Map . clear ( ) ; while ( N % 2 == 0 ) { if ( Map . containsKey ( 2 ) ) Map . put ( 2 , Map . get ( 2 ) + 1 ) ; else Map . put ( 2 , 1 ) ; N /= 2 ; } for ( i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { while ( N % i == 0 ) { if ( Map . containsKey ( i ) ) Map . put ( i , Map . get ( i ) + 1 ) ; else Map . put ( i , 1 ) ; N /= i ; } } if ( N > 2 ) { if ( Map . containsKey ( N ) ) Map . put ( N , Map . get ( N ) + 1 ) ; else Map . put ( N , 1 ) ; } } static int PowInFactN ( int N , int P ) { int ans = 0 ; int temp = P ; while ( temp <= N ) { ans += N / temp ; temp = temp * P ; } return ans ; } static int findPowers ( int N , int P ) { findPrimeFactors ( P ) ; Vector < Integer > Powers = new Vector < Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : Map . entrySet ( ) ) { int primeFac = it . getKey ( ) ; int facPow = it . getValue ( ) ; int p = PowInFactN ( N , primeFac ) ; p /= facPow ; Powers . add ( p ) ; } return Collections . min ( Powers ) ; } public static void main ( String [ ] args ) { int N = 24 , P = 4 ; System . out . print ( findPowers ( N , P ) ) ; } }
class GFG { static int FindMaxLength ( int A [ ] , int B [ ] , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= m ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = m - 1 ; j >= 0 ; j -- ) { if ( A [ i ] == B [ j ] ) dp [ j ] [ i ] = dp [ j + 1 ] [ i + 1 ] + 1 ; } } int maxm = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { maxm = Math . max ( maxm , dp [ i ] [ j ] ) ; } } return maxm ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 8 , 2 , 1 } ; int B [ ] = { 8 , 2 , 1 , 4 , 7 } ; int n = A . length ; int m = B . length ; System . out . print ( FindMaxLength ( A , B , n , m ) ) ; } }
class GFG { static int LAS [ ] [ ] = new int [ 1000 ] [ 2 ] ; static int solve ( int arr [ ] , int n , int i , int pos ) { if ( i == n ) return 0 ; if ( LAS [ i ] [ pos ] == 1 ) return LAS [ i ] [ pos ] ; int inc = 0 , exc = 0 ; if ( arr [ i ] > 0 && pos == 1 ) { pos = 0 ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } else if ( arr [ i ] < 0 && pos == 0 ) { pos = 1 ; inc = 1 + solve ( arr , n , i + 1 , pos ) ; } exc = solve ( arr , n , i + 1 , pos ) ; LAS [ i ] [ pos ] = Math . max ( inc , exc ) ; return LAS [ i ] [ pos ] ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 2 , 3 , 4 , 5 , - 6 , 8 , - 99 } ; int n = arr . length ; System . out . println ( Math . max ( solve ( arr , n , 0 , 0 ) , solve ( arr , n , 0 , 1 ) ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000 ; static void printGolombSequence ( int N ) { int [ ] arr = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) arr [ i ] = 0 ; int cnt = 0 ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; Map < Integer , Integer > M = new HashMap < Integer , Integer > ( ) ; M . put ( 2 , 2 ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( cnt == 0 ) { arr [ i ] = 1 + arr [ i - 1 ] ; cnt = M . get ( arr [ i ] ) ; cnt -- ; } else { arr [ i ] = arr [ i - 1 ] ; cnt -- ; } M . put ( i , arr [ i ] ) ; } for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 11 ; printGolombSequence ( N ) ; } }
class GFG { static int number_of_ways ( int n ) { int [ ] includes_3 = new int [ n + 1 ] ; int [ ] not_includes_3 = new int [ n + 1 ] ; includes_3 [ 3 ] = 1 ; not_includes_3 [ 1 ] = 1 ; not_includes_3 [ 2 ] = 2 ; not_includes_3 [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { includes_3 [ i ] = includes_3 [ i - 1 ] + includes_3 [ i - 2 ] + not_includes_3 [ i - 3 ] ; not_includes_3 [ i ] = not_includes_3 [ i - 1 ] + not_includes_3 [ i - 2 ] ; } return includes_3 [ n ] ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( number_of_ways ( n ) ) ; } }
import java . util . * ; class GFG { static int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; else result = 0 ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 4 , - 1 , 6 } ; int n = arr . length ; System . out . print ( cntSubSeq ( arr , n ) ) ; } }
class GFG { final static int INT_MAX = Integer . MAX_VALUE ; static int minSteps ( String str , int n , int k ) { if ( str . charAt ( n - 1 ) == '0' ) return - 1 ; if ( n == 1 ) return 0 ; if ( n < 4 ) return 1 ; int dp [ ] = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = 1 ; dp [ n - 3 ] = 1 ; for ( int i = n - 4 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' ) continue ; int steps = INT_MAX ; if ( i + k < n && str . charAt ( i + k ) == '1' ) steps = Math . min ( steps , dp [ i + k ] ) ; if ( str . charAt ( i + 1 ) == '1' ) steps = Math . min ( steps , dp [ i + 1 ] ) ; if ( str . charAt ( i + 2 ) == '1' ) steps = Math . min ( steps , dp [ i + 2 ] ) ; dp [ i ] = ( steps == INT_MAX ) ? steps : 1 + steps ; } if ( dp [ 0 ] == INT_MAX ) return - 1 ; return dp [ 0 ] ; } public static void main ( String [ ] args ) { String str = "101000011" ; int n = str . length ( ) ; int k = 5 ; System . out . println ( minSteps ( str , n , k ) ) ; } }
import java . util . * ; class GFG { static final int N = 10000 ; static int [ ] seg = new int [ 3 * N ] ; static int update ( int in , int l , int r , int up_in , int val ) { if ( r < up_in l > up_in ) return seg [ in ] ; if ( l == up_in && r == up_in ) return seg [ in ] = val ; int m = ( l + r ) / 2 ; return seg [ in ] = update ( 2 * in + 1 , l , m , up_in , val ) + update ( 2 * in + 2 , m + 1 , r , up_in , val ) ; } static int query ( int in , int l , int r , int l1 , int r1 ) { if ( l > r ) return 0 ; if ( r < l1 l > r1 ) return 0 ; if ( l1 <= l && r <= r1 ) return seg [ in ] ; int m = ( l + r ) / 2 ; return query ( 2 * in + 1 , l , m , l1 , r1 ) + query ( 2 * in + 2 , m + 1 , r , l1 , r1 ) ; } static int findCnt ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) brr [ i ] = arr [ i ] ; Arrays . sort ( brr ) ; HashMap < Integer , Integer > r = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) r . put ( brr [ i ] , i + 1 ) ; int dp [ ] = new int [ n ] ; int ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int rank = r . get ( arr [ i ] ) ; dp [ i ] = 1 + query ( 0 , 0 , n - 1 , rank , n - 1 ) ; ans += dp [ i ] ; update ( 0 , 0 , n - 1 , rank - 1 , dp [ i ] + query ( 0 , 0 , n - 1 , rank - 1 , rank - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 10 , 9 } ; int n = arr . length ; System . out . print ( findCnt ( arr , n ) ) ; } }
class GFG { static int minCost ( int n , int x , int y , int z ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) { dp [ i ] = Math . min ( dp [ ( i + 1 ) / 2 ] + x + z , dp [ i - 1 ] + y ) ; } else { dp [ i ] = Math . min ( dp [ i / 2 ] + x , dp [ i - 1 ] + y ) ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 5 , x = 2 , y = 1 , z = 3 ; System . out . print ( minCost ( n , x , y , z ) + "NEW_LINE"); } }
class GFG { final static int N = 1000005 ; static int maximum_set ( int a [ ] , int n ) { int dp [ ] = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ a [ i ] ] = 1 ; int ans = 1 ; for ( int i = N - 1 ; i >= 1 ; i -- ) { if ( dp [ i ] != 0 ) { for ( int j = 2 * i ; j < N ; j += i ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ j ] ) ; ans = Math . max ( ans , dp [ i ] ) ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 6 , 8 , 10 , 18 , 21 , 24 } ; int n = arr . length ; System . out . println ( maximum_set ( arr , n ) ) ; } }
class GFG { final static int N = 100 ; final static int INT_MIN = Integer . MIN_VALUE ; static int dp [ ] [ ] = new int [ N ] [ 3 ] ; static int v [ ] [ ] = new int [ N ] [ 3 ] ; static int findLargestString ( String s , int i , int r ) { if ( i == s . length ( ) ) { if ( r == 0 ) return 0 ; else return INT_MIN ; } if ( v [ i ] [ r ] == 1 ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = Math . max ( 1 + findLargestString ( s , i + 1 , ( r * 2 + ( s . charAt ( i ) - '0' ) ) % 3 ) , findLargestString ( s , i + 1 , r ) ) ; return dp [ i ] [ r ] ; } public static void main ( String [ ] args ) { String s = "101" ; System . out . print ( findLargestString ( s , 0 , 0 ) ) ; } }
class GFG { static final int N = 100 ; static int dp [ ] [ ] = new int [ N ] [ 3 ] ; static int v [ ] [ ] = new int [ N ] [ 3 ] ; static int findCnt ( String s , int i , int r ) { if ( i == s . length ( ) ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] == 1 ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s . charAt ( i ) - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } public static void main ( String [ ] args ) { String s = "11" ; System . out . print ( findCnt ( s , 0 , 0 ) - 1 ) ; } }
class GFG { static int minCount ( int arr [ ] , int n , int k ) { int ans = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; sum -= k ; int l = 0 ; int r = 0 ; int tot = 0 ; while ( l < n ) { if ( tot <= sum ) { ans = Math . max ( ans , r - l ) ; if ( r == n ) break ; tot += arr [ r ++ ] ; } else { tot -= arr [ l ++ ] ; } } return ( n - ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 11 , 5 , 5 } ; int n = arr . length ; int k = 11 ; System . out . println ( minCount ( arr , n , k ) ) ; } }
class GFG { static final int N = 401 ; static int [ ] [ ] dp = new int [ N ] [ N ] ; static boolean [ ] [ ] v = new boolean [ N ] [ N ] ; static int minMergeCost ( int i , int j , int [ ] arr ) { if ( i == j ) return 0 ; if ( v [ i ] [ j ] ) return dp [ i ] [ j ] ; v [ i ] [ j ] = true ; int x = dp [ i ] [ j ] ; x = Integer . MAX_VALUE ; int tot = 0 ; for ( int k = i ; k <= j ; k ++ ) tot += arr [ k ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { x = Math . min ( x , tot + minMergeCost ( i , k - 1 , arr ) + minMergeCost ( k , j , arr ) ) ; } return x ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 } ; int n = arr . length ; System . out . print ( minMergeCost ( 0 , n - 1 , arr ) ) ; } }
class GFG { static int maxSize = 50 ; static double dp [ ] = new double [ maxSize ] ; static int v [ ] = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( expectedSteps ( n - 1 ) ) ; } }
class GFG { static int countSubSeq ( String str , int len ) { int ans = 0 ; int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) ans += mul ; mul *= 2 ; } return ans ; } public static void main ( String [ ] args ) { String str = "10010" ; int len = str . length ( ) ; System . out . print ( countSubSeq ( str , len ) ) ; } }
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static int sz = 100005 ; static int n ; static ArrayList [ ] tree = new ArrayList [ sz ] ; static int [ ] subtree_size = new int [ sz ] ; static int [ ] vis = new int [ sz ] ; static void AddEdge ( int a , int b ) { tree [ a ] . add ( b ) ; tree [ b ] . add ( a ) ; } static int dfs ( int node ) { vis [ node ] = 1 ; subtree_size [ node ] = 1 ; for ( int child : ( ArrayList < Integer > ) tree [ node ] ) { if ( vis [ child ] == 0 ) { subtree_size [ node ] += dfs ( child ) ; } } return subtree_size [ node ] ; } static int contribution ( int node , int ans ) { vis [ node ] = 1 ; for ( int child : ( ArrayList < Integer > ) tree [ node ] ) { if ( vis [ child ] == 0 ) { ans += ( subtree_size [ child ] * ( n - subtree_size [ child ] ) ) ; ans = contribution ( child , ans ) ; } } return ans ; } static int getSum ( ) { Arrays . fill ( vis , 0 ) ; dfs ( 0 ) ; int ans = 0 ; Arrays . fill ( vis , 0 ) ; ans = contribution ( 0 , ans ) ; return ans ; } public static void main ( String [ ] args ) { n = 5 ; for ( int i = 0 ; i < sz ; i ++ ) { tree [ i ] = new ArrayList ( ) ; } AddEdge ( 0 , 1 ) ; AddEdge ( 0 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 1 , 4 ) ; System . out . println ( getSum ( ) ) ; } }
import java . util . * ; class GFG { static int N = 3 ; static int numberOfCells ( int mat [ ] [ ] ) { boolean [ ] [ ] row = new boolean [ N ] [ N ] ; boolean [ ] [ ] col = new boolean [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( mat [ i ] [ j ] == 1 ) { row [ i ] [ j ] = ( j + 1 < N ) ? row [ i ] [ j + 1 ] : true ; } else { row [ i ] [ j ] = false ; } } } for ( int j = 0 ; j < N ; j ++ ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( mat [ i ] [ j ] == 1 ) { col [ i ] [ j ] = ( i + 1 < N ) ? col [ i + 1 ] [ j ] : true ; } else { col [ i ] [ j ] = false ; } } } int cnt = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ i ] [ j ] && col [ i ] [ j ] ) { cnt ++ ; } } } for ( int i = 0 ; i < N ; i ++ ) { if ( col [ i ] [ N - 1 ] ) cnt ++ ; } for ( int j = 0 ; j < N - 1 ; j ++ ) { if ( row [ N - 1 ] [ j ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 1 , 1 } , { 0 , 1 , 1 } , { 0 , 1 , 1 } } ; System . out . print ( numberOfCells ( mat ) ) ; } }
import java . util . * ; class GFG { static int maxSumSubarray ( int [ ] arr , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + arr [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static void getUpdatedArray ( int [ ] arr , int [ ] copy , int i ) { for ( int j = 0 ; j <= ( i / 2 ) ; j ++ ) { copy [ j ] = arr [ i - j ] ; copy [ i - j ] = arr [ j ] ; } return ; } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } static int maxSum ( int [ ] arr , int size ) { int resSum = Integer . MIN_VALUE ; resSum = Math . max ( resSum , maxSumSubarray ( arr , size ) ) ; int [ ] copyArr = arr ; for ( int i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } arr = reverse ( arr ) ; copyArr = arr ; for ( int i = 1 ; i < size ; i ++ ) { getUpdatedArray ( arr , copyArr , i ) ; resSum = Math . max ( resSum , maxSumSubarray ( copyArr , size ) ) ; } resSum += 6 ; return resSum ; } public static void main ( String [ ] args ) { int [ ] arr = { - 9 , 21 , 24 , 24 , - 51 , - 6 , 17 , - 42 , - 39 , 33 } ; int size = arr . length ; System . out . print ( maxSum ( arr , size ) ) ; } }
class GFG { static int MaxGCD ( int a [ ] , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = __gcd ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = __gcd ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . max ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . max ( ans , __gcd ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 7 , 8 } ; int n = a . length ; System . out . println ( MaxGCD ( a , n ) ) ; } }
import java . util . * ; class GFG { static int sumOfProduct ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; int cur_sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ 1 ] [ i ] = arr [ i - 1 ] ; cur_sum += arr [ i - 1 ] ; } for ( int i = 2 ; i <= k ; i ++ ) { int temp_sum = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cur_sum -= dp [ i - 1 ] [ j ] ; dp [ i ] [ j ] = arr [ j - 1 ] * cur_sum ; temp_sum += dp [ i ] [ j ] ; } cur_sum = temp_sum ; } return cur_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; System . out . print ( sumOfProduct ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static int subsequence ( int [ ] S , int [ ] T , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; int mod = 1000000007 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int j = 0 ; j <= m ; j ++ ) dp [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= m ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ; dp [ i ] [ j ] += mod ; dp [ i ] [ j ] %= mod ; } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int S [ ] = { 1 , 1 } ; int n = S . length ; int T [ ] = { 1 , 1 } ; int m = T . length ; System . out . println ( subsequence ( S , T , n , m ) ) ; } }
class GFG { static int findMax ( int arr [ ] , int n ) { int res = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) count += 1 ; } res = Math . max ( count , res ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }
class GFG { static int findMax ( int arr [ ] , int n ) { boolean [ ] divisible = new boolean [ n ] ; int res = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }
class GFG { static int maxN = 20 ; static int maxM = 10 ; static int [ ] [ ] dp = new int [ maxN ] [ maxM ] ; static boolean [ ] [ ] v = new boolean [ maxN ] [ maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = true ; return dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr + arr [ i ] ) % m , n , m ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 3 , 3 } ; int n = arr . length ; int m = 6 ; System . out . println ( findCnt ( arr , 0 , 0 , n , m ) - 1 ) ; } }
import java . util . * ; class GFG { static int maxN = 300 ; static int maxM = 300 ; static int dp [ ] [ ] = new int [ maxN ] [ maxM ] ; static boolean v [ ] [ ] = new boolean [ maxN ] [ maxM ] ; static int findLen ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = true ; int l = findLen ( arr , i + 1 , curr , n , m ) ; int r = findLen ( arr , i + 1 , curr & arr [ i ] , n , m ) ; dp [ i ] [ curr ] = l ; if ( r != - 1 ) dp [ i ] [ curr ] = Math . max ( dp [ i ] [ curr ] , r + 1 ) ; return dp [ i ] [ curr ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 7 , 2 , 3 } ; int n = arr . length ; int m = 3 ; int ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ; if ( ans == - 1 ) System . out . print ( 0 ) ; else System . out . print ( ans ) ; } }
import java . util . * ; class GFG { static int maxN = 20 ; static int maxSum = 50 ; static int minSum = 50 ; static int base = 50 ; static int [ ] [ ] dp = new int [ maxN ] [ maxSum + minSum ] ; static boolean [ ] [ ] v = new boolean [ maxN ] [ maxSum + minSum ] ; static int findCnt ( int [ ] arr , int i , int required_sum , int n ) { if ( i == n ) { if ( required_sum == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ required_sum + base ] ) return dp [ i ] [ required_sum + base ] ; v [ i ] [ required_sum + base ] = true ; dp [ i ] [ required_sum + base ] = findCnt ( arr , i + 1 , required_sum , n ) + findCnt ( arr , i + 1 , required_sum - arr [ i ] , n ) ; return dp [ i ] [ required_sum + base ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 3 , 3 } ; int n = arr . length ; int x = 6 ; System . out . println ( findCnt ( arr , 0 , x , n ) ) ; } }
import java . util . * ; class GFG { static int maxN = 20 ; static int maxM = 64 ; static int [ ] [ ] dp = new int [ maxN ] [ maxM ] ; static boolean [ ] [ ] v = new boolean [ maxN ] [ maxM ] ; static int findCnt ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { return ( curr == m ? 1 : 0 ) ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = true ; dp [ i ] [ curr ] = findCnt ( arr , i + 1 , curr , n , m ) + findCnt ( arr , i + 1 , ( curr arr [ i ] ) , n , m ) ; return dp [ i ] [ curr ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 } ; int n = arr . length ; int m = 3 ; System . out . println ( findCnt ( arr , 0 , 0 , n , m ) ) ; } }
class GFG { static int MOD = ( int ) ( 1e9 + 7 ) ; static int modulo_13 ( String s , int n ) { long [ ] [ ] dp = new long [ n + 1 ] [ 13 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { int nxt = s . charAt ( i ) - '0' ; if ( s . charAt ( i ) == ' ? ' ) nxt = j ; for ( int k = 0 ; k < 13 ; k ++ ) { int rem = ( 10 * k + nxt ) % 13 ; dp [ i + 1 ] [ rem ] += dp [ i ] [ k ] ; dp [ i + 1 ] [ rem ] %= MOD ; } if ( s . charAt ( i ) != ' ? ' ) break ; } } return ( int ) dp [ n ] [ 5 ] ; } public static void main ( String [ ] args ) { String s = " ? 44" ; int n = s . length ( ) ; System . out . println ( modulo_13 ( s , n ) ) ; } }
import java . util . * ; class GFG { static int LISusingLCS ( Vector < Integer > seq ) { int n = seq . size ( ) ; int L [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; Vector < Integer > sortedseq = new Vector < Integer > ( seq ) ; Collections . sort ( sortedseq ) ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( seq . get ( i - 1 ) == sortedseq . get ( j - 1 ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ n ] [ n ] ; } public static void main ( String args [ ] ) { Vector < Integer > sequence = new Vector < Integer > ( ) ; sequence . add ( 12 ) ; sequence . add ( 34 ) ; sequence . add ( 1 ) ; sequence . add ( 5 ) ; sequence . add ( 40 ) ; sequence . add ( 80 ) ; System . out . println ( LISusingLCS ( sequence ) ) ; } }
class GFG { static int count_numbers ( int k , int n , boolean flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , false ) + count_numbers ( k , n - 1 , true ) ) ; else return count_numbers ( k , n - 1 , true ) ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 10 ; System . out . println ( count_numbers ( k , n , true ) ) ; } }
import java . util . * ; class GFG { static int count_numbers ( int k , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ 2 ] ; dp [ 1 ] [ 0 ] = 0 ; dp [ 1 ] [ 1 ] = k - 1 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] + dp [ i - 1 ] [ 1 ] ) * ( k - 1 ) ; } return dp [ n ] [ 0 ] + dp [ n ] [ 1 ] ; } public static void main ( String [ ] args ) { int k = 10 ; int n = 3 ; System . out . println ( count_numbers ( k , n ) ) ; } }
import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int max = ( int ) Math . pow ( 2 , n ) ; int result = 0 ; for ( int i = 0 ; i < max ; i ++ ) { int counter = i ; if ( ( counter & ( counter >> 1 ) ) > 0 ) continue ; result ++ ; } return result ; } static public void main ( String [ ] arg ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } int result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void perfix_calculate ( int [ ] [ ] A , int [ ] [ ] row , int [ ] [ ] col ) { int n = ( int ) A . length ; int m = ( int ) A [ 0 ] . length ; for ( int i = 0 ; i < n ; ++ i ) { row [ i ] [ 0 ] = A [ i ] [ 0 ] ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j < m ; ++ j ) { row [ i ] [ j ] = row [ i ] [ j - 1 ] + A [ i ] [ j ] ; } } for ( int i = 0 ; i < m ; ++ i ) { col [ 0 ] [ i ] = A [ 0 ] [ i ] ; } for ( int i = 0 ; i < m ; ++ i ) { for ( int j = 1 ; j < n ; ++ j ) { col [ j ] [ i ] = A [ j ] [ i ] + col [ j - 1 ] [ i ] ; } } } static int perimeter ( int i , int j , int k , int [ ] [ ] row , int [ ] [ ] col , int [ ] [ ] A ) { int row_s , col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i ] [ j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 ] [ j ] ; int upper_row = row [ i ] [ j + k ] - row_s ; int left_col = col [ i + k ] [ j ] - col_s ; if ( j == 0 ) row_s = 0 ; else row_s = row [ i + k ] [ j - 1 ] ; if ( i == 0 ) col_s = 0 ; else col_s = col [ i - 1 ] [ j + k ] ; int lower_row = row [ i + k ] [ j + k ] - row_s ; int right_col = col [ i + k ] [ j + k ] - col_s ; int sum = upper_row + lower_row + left_col + right_col ; sum -= ( A [ i ] [ j ] + A [ i + k ] [ j ] + A [ i ] [ j + k ] + A [ i + k ] [ j + k ] ) ; return sum ; } static int maxPerimeter ( int [ ] [ ] A ) { int n = ( int ) A . length ; int m = ( int ) A [ 0 ] . length ; int [ ] [ ] row = new int [ n ] [ m ] ; int [ ] [ ] col = new int [ n ] [ m ] ; perfix_calculate ( A , row , col ) ; int maxPer = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { for ( int k = 0 ; k < Math . min ( n - i , m - j ) ; ++ k ) { int perimtr = perimeter ( i , j , k , row , col , A ) ; maxPer = Math . max ( maxPer , perimtr ) ; } } } return maxPer ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 1 , 1 , 0 } , { 1 , 1 , 1 } , { 0 , 1 , 1 } } ; System . out . print ( maxPerimeter ( A ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static int minJumps ( int arr [ ] , int N ) { int MAX = 1000000 ; int [ ] fib = new int [ 30 ] ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 30 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; int [ ] DP = new int [ N + 2 ] ; DP [ 0 ] = 0 ; for ( int i = 1 ; i <= N + 1 ; i ++ ) DP [ i ] = MAX ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { for ( int j = 1 ; j < 30 ; j ++ ) { if ( ( i == N + 1 arr [ i - 1 ] == 1 ) && i - fib [ j ] >= 0 ) DP [ i ] = Math . min ( DP [ i ] , 1 + DP [ i - fib [ j ] ] ) ; } } if ( DP [ N + 1 ] != MAX ) return DP [ N + 1 ] ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 0 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 0 , 0 } ; int n = 11 ; int ans = minJumps ( arr , n ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int MAX = 100 ; static int recur ( int ind , int cnt , int last , int a [ ] , int n , int k , int dp [ ] [ ] ) { if ( cnt == k ) return 0 ; if ( ind == n ) return ( int ) - 1e9 ; if ( dp [ ind ] [ cnt ] != - 1 ) return dp [ ind ] [ cnt ] ; int ans = 0 ; for ( int i = ind ; i < n ; i ++ ) { if ( cnt % 2 == 0 ) ans = Math . max ( ans , recur ( i + 1 , cnt + 1 , i , a , n , k , dp ) ) ; else ans = Math . max ( ans , __gcd ( a [ last ] , a [ i ] ) + recur ( i + 1 , cnt + 1 , 0 , a , n , k , dp ) ) ; } return dp [ ind ] [ cnt ] = ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 5 , 3 , 7 , 8 , 10 , 9 , 8 } ; int n = a . length ; int k = 4 ; int [ ] [ ] dp = new int [ n ] [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( recur ( 0 , 0 , 0 , a , n , k , dp ) ) ; } }
import java . util . * ; class GFG { static int find ( String s ) { if ( s . length ( ) == 0 ) return 0 ; int c = 1 ; String d = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { d += s . charAt ( i ) ; String s2 = " " ; if ( i + d . length ( ) < s . length ( ) ) { s2 = s . substring ( i + 1 , d . length ( ) + ( i + 1 ) ) ; } if ( s2 . equals ( d ) ) { c = 1 + find ( s . substring ( i + 1 ) ) ; break ; } } return c ; } public static void main ( String [ ] args ) { String s = " abababab " ; System . out . print ( find ( s ) ) ; } }
class GFG { static int MAX = 10 ; public static int maximizeSum ( int [ ] a , int n , int ind , int k , int [ ] [ ] dp ) { if ( k == 0 ) { if ( ind == n ) return 0 ; else return - 1000000000 ; } else if ( ind == n ) return - 1000000000 ; else if ( dp [ ind ] [ k ] != - 1 ) return dp [ ind ] [ k ] ; else { int ans = 0 ; int mini = a [ ind ] ; for ( int i = ind ; i < n ; i ++ ) { mini = Math . min ( mini , a [ i ] ) ; ans = Math . max ( ans , maximizeSum ( a , n , i + 1 , k - 1 , dp ) + mini ) ; } return dp [ ind ] [ k ] = ans ; } } public static void main ( String [ ] args ) { int [ ] a = { 5 , 7 , 4 , 2 , 8 , 1 , 6 } ; int k = 3 ; int n = a . length ; int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; } System . out . println ( maximizeSum ( a , n , 0 , k , dp ) ) ; } }
import java . util . HashMap ; import java . util . Vector ; class GFG { public static void SieveOfEratosthenes ( int MAX , Vector < Integer > primes ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= MAX ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] ) primes . add ( i ) ; } } public static int findLongest ( int [ ] A , int n ) { HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; Vector < Integer > primes = new Vector < > ( ) ; SieveOfEratosthenes ( A [ n - 1 ] , primes ) ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; mpp . put ( A [ n - 1 ] , n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int num = A [ i ] ; dp [ i ] = 1 ; int maxi = 0 ; for ( int it : primes ) { int xx = num * it ; if ( xx > A [ n - 1 ] ) break ; else if ( mpp . get ( xx ) != null && mpp . get ( xx ) != 0 ) { dp [ i ] = Math . max ( dp [ i ] , 1 + dp [ mpp . get ( xx ) ] ) ; } } mpp . put ( A [ i ] , i ) ; } int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , dp [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 2 , 5 , 6 , 12 , 35 , 60 , 385 } ; int n = a . length ; System . out . println ( findLongest ( a , n ) ) ; } }
class GFG { static int MAX = 10 ; static int getCount ( int n ) { int [ ] [ ] list = new int [ MAX ] [ ] ; list [ 0 ] = new int [ ] { 0 , 8 } ; list [ 1 ] = new int [ ] { 1 , 2 , 4 } ; list [ 2 ] = new int [ ] { 2 , 1 , 3 , 5 } ; list [ 3 ] = new int [ ] { 3 , 6 , 2 } ; list [ 4 ] = new int [ ] { 4 , 1 , 7 , 5 } ; list [ 5 ] = new int [ ] { 5 , 4 , 6 , 2 , 8 } ; list [ 6 ] = new int [ ] { 6 , 3 , 5 , 9 } ; list [ 7 ] = new int [ ] { 7 , 4 , 8 } ; list [ 8 ] = new int [ ] { 8 , 5 , 0 , 7 , 9 } ; list [ 9 ] = new int [ ] { 9 , 6 , 8 } ; int Arr [ ] = new int [ ] { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; for ( int i = 2 ; i <= n ; i ++ ) { int Arr2 [ ] = new int [ MAX ] ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int x = 0 ; x < list [ j ] . length ; x ++ ) { Arr2 [ list [ j ] [ x ] ] += Arr [ j ] ; } } for ( int j = 0 ; j < MAX ; j ++ ) Arr [ j ] = Arr2 [ j ] ; } int sum = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) sum += Arr [ i ] ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( getCount ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] dp ; static int solveUtil ( int start , int bucket , String str , int K ) { int N = str . length ( ) ; if ( start == N ) { if ( bucket == K ) { return 0 ; } return Integer . MAX_VALUE ; } if ( bucket == K ) { return Integer . MAX_VALUE ; } if ( dp [ start ] [ bucket ] != - 1 ) { return dp [ start ] [ bucket ] ; } int zeroes = 0 ; int ones = 0 ; int ans = Integer . MAX_VALUE ; for ( int i = start ; i < N ; ++ i ) { if ( str . charAt ( i ) == '1' ) { ones ++ ; } else { zeroes ++ ; } if ( ones * zeroes > ans ) { break ; } int temp = solveUtil ( i + 1 , bucket + 1 , str , K ) ; if ( temp != Integer . MAX_VALUE ) { ans = Math . min ( ans , temp + ( ones * zeroes ) ) ; } } return dp [ start ] [ bucket ] = ans ; } static int solve ( String str , int K ) { int N = str . length ( ) ; dp = new int [ N ] [ K ] ; for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } int ans = solveUtil ( 0 , 0 , str , K ) ; return ans == Integer . MAX_VALUE ? - 1 : ans ; } public static void main ( String [ ] args ) { String S = "0101" ; int K = 2 ; System . out . println ( solve ( S , K ) ) ; } }
class GFG { static double Probability ( double p [ ] , int n ) { double [ ] [ ] dp = new double [ n + 1 ] [ n + 1 ] ; dp [ 0 ] [ 0 ] = 1.0 ; for ( int i = 1 ; i <= n ; i += 1 ) { for ( int j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ; } } double ans = 0.0 ; for ( int i = ( n + 1 ) / 2 ; i <= n ; i += 1 ) ans += dp [ n ] [ i ] ; return ans ; } public static void main ( String [ ] args ) { double p [ ] = { 0.0 , 0.3 , 0.4 , 0.7 } ; int n = p . length - 1 ; System . out . println ( Probability ( p , n ) ) ; } }
class GFG { static int C_MAX = 30 ; static int max_arr_len = 10 ; static int dp [ ] [ ] = new int [ max_arr_len ] [ C_MAX + 1 ] ; static boolean v [ ] [ ] = new boolean [ max_arr_len ] [ C_MAX + 1 ] ; static int findMax ( int i , int r , int w [ ] , int n ) { if ( r < 0 ) return Integer . MIN_VALUE ; if ( i == n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = true ; dp [ i ] [ r ] = Math . max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i ] [ r ] ; } static void preCompute ( int w [ ] , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } static int ansQuery ( int w ) { return dp [ 0 ] [ w ] ; } public static void main ( String [ ] args ) { int w [ ] = new int [ ] { 3 , 8 , 9 } ; int n = w . length ; preCompute ( w , n ) ; int queries [ ] = new int [ ] { 11 , 10 , 4 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ansQuery ( queries [ i ] ) ) ; } }
import java . util . * ; class GFG { static int maxLen = 10 ; static int [ ] dp = new int [ maxLen ] ; static boolean [ ] v = new boolean [ maxLen ] ; static int [ ] prefix_sum = new int [ maxLen ] ; static void findPrefixSum ( int arr [ ] , int n ) { prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefix_sum [ i ] = arr [ i ] + prefix_sum [ i - 1 ] ; } } static int maxSum ( int arr [ ] , int i , int n , int k ) { if ( i + k > n ) { return 0 ; } if ( v [ i ] ) { return dp [ i ] ; } v [ i ] = true ; int x ; if ( i == 0 ) { x = prefix_sum [ k - 1 ] ; } else { x = prefix_sum [ i + k - 1 ] - prefix_sum [ i - 1 ] ; } dp [ i ] = Math . max ( maxSum ( arr , i + 1 , n , k ) , x + maxSum ( arr , i + k + 1 , n , k ) ) ; return dp [ i ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 7 , 6 } ; int n = arr . length ; int k = 1 ; findPrefixSum ( arr , n ) ; System . out . println ( maxSum ( arr , 0 , n , k ) ) ; } }
import java . io . * ; class GFG { static int N = 3 ; static int M = 3 ; static int findMaxSum ( int arr [ ] [ ] ) { int sum = 0 ; int [ ] [ ] b = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { b [ i ] [ M - 1 ] = arr [ i ] [ M - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = M - 2 ; j >= 0 ; j -- ) { b [ i ] [ j ] = arr [ i ] [ j ] + b [ i ] [ j + 1 ] ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { sum = Math . max ( sum , b [ i ] [ j ] + b [ i - 1 ] [ j ] ) ; b [ i ] [ j ] = Math . max ( b [ i ] [ j ] , b [ i - 1 ] [ j ] + arr [ i ] [ j ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 3 , 7 , 4 } , { 1 , 9 , 6 } , { 1 , 7 , 7 } } ; System . out . println ( findMaxSum ( arr ) ) ; } }
import java . util . * ; class GFG { static int mod = 3803 ; static int d = 26 ; static int hash_b ; static int [ ] hash_a ; static int [ ] mul ; static int mi ( int x ) { int p = mod - 2 ; int s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) { s = ( s * x ) % mod ; } x = ( x * x ) % mod ; p /= 2 ; } return ( s * x ) % mod ; } static void genHash ( char [ ] a , char [ ] b ) { hash_a = new int [ a . length ] ; mul = new int [ a . length ] ; for ( int i = b . length - 1 ; i >= 0 ; i -- ) { hash_b = ( hash_b * d + ( b [ i ] - 97 ) ) % mod ; } mul [ 0 ] = 1 ; hash_a [ 0 ] = ( a [ 0 ] - 97 ) % mod ; for ( int i = 1 ; i < a . length ; i ++ ) { mul [ i ] = ( mul [ i - 1 ] * d ) % mod ; hash_a [ i ] = ( hash_a [ i - 1 ] + mul [ i ] * ( a [ i ] - 97 ) ) % mod ; } } static boolean checkEqual ( int i , int len_a , int len_b ) { int x ; if ( i == 0 ) { x = hash_a [ len_b - 1 ] ; } else { x = ( hash_a [ i + len_b - 1 ] - hash_a [ i - 1 ] + 2 * mod ) % mod ; x = ( x * mi ( mul [ i ] ) ) % mod ; } if ( x == hash_b ) { return true ; } return false ; } public static void main ( String [ ] args ) { String a = " abababababa " ; String b = " aba " ; genHash ( a . toCharArray ( ) , b . toCharArray ( ) ) ; int queries [ ] = { 0 , 1 , 2 , 3 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { if ( checkEqual ( queries [ i ] , a . length ( ) , b . length ( ) ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } } }
class GFG { static int max_size = 20 ; static int max_k = 20 ; static int [ ] [ ] dp = new int [ max_size ] [ max_k ] ; static boolean [ ] [ ] v = new boolean [ max_size ] [ max_k ] ; static int sum = 0 ; static void findSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } static int cntWays ( int arr [ ] , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i ] [ ck ] ) return dp [ i ] [ ck ] ; curr_sum += arr [ i ] ; v [ i ] [ ck ] = true ; dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i ] [ ck ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . length ; int k = 2 ; findSum ( arr , n ) ; System . out . println ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; } }
import java . io . * ; class GFG { static int arrSize = 51 ; static int dp [ ] = new int [ arrSize ] ; static boolean v [ ] = new boolean [ arrSize ] ; static int sumMax ( int i , int arr [ ] , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( sumMax ( 0 , arr , n ) ) ; } }
import java . util . * ; class GFG { static int MaxIncreasingSub ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ n ] [ k + 1 ] , ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < k + 1 ; j ++ ) dp [ i ] [ j ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] [ 1 ] = arr [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { for ( int l = 1 ; l <= k - 1 ; l ++ ) { if ( dp [ j ] [ l ] != - 1 ) { dp [ i ] [ l + 1 ] = Math . max ( dp [ i ] [ l + 1 ] , dp [ j ] [ l ] + arr [ i ] ) ; } } } } } for ( int i = 0 ; i < n ; i ++ ) { if ( ans < dp [ i ] [ k ] ) ans = dp [ i ] [ k ] ; } return ( ans == - 1 ) ? 0 : ans ; } public static void main ( String args [ ] ) { int n = 8 , k = 3 ; int arr [ ] = { 8 , 5 , 9 , 10 , 5 , 6 , 21 , 8 } ; int ans = MaxIncreasingSub ( arr , n , k ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int [ ] dp = new int [ 200 ] ; static int count = 0 ; static void print ( int idx ) { for ( int i = 1 ; i < idx ; i ++ ) { System . out . print ( dp [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } static void solve ( int remSum , int maxVal , int idx , int count ) { if ( remSum == 0 ) { print ( idx ) ; count ++ ; return ; } for ( int i = maxVal ; i >= 1 ; i -- ) { if ( i > remSum ) { continue ; } else if ( i <= remSum ) { dp [ idx ] = i ; solve ( remSum - i , i , idx + 1 , count ) ; } } } public static void main ( String [ ] args ) { int n = 4 , count = 0 ; solve ( n , n , 1 , count ) ; } }
class GFG { final static int bitscount = 32 ; final static int n = 3 ; static int prefix_count [ ] [ ] [ ] = new int [ bitscount ] [ n ] [ n ] ; static void findPrefixCount ( int arr [ ] [ ] ) { for ( int i = 0 ; i < bitscount ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { prefix_count [ i ] [ j ] [ 0 ] = ( ( arr [ j ] [ 0 ] >> i ) & 1 ) ; for ( int k = 1 ; k < n ; k ++ ) { prefix_count [ i ] [ j ] [ k ] = ( ( arr [ j ] [ k ] >> i ) & 1 ) ; prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j ] [ k - 1 ] ; } } } for ( int i = 0 ; i < bitscount ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) prefix_count [ i ] [ j ] [ k ] += prefix_count [ i ] [ j - 1 ] [ k ] ; } static int rangeOr ( int x1 , int y1 , int x2 , int y2 ) { int ans = 0 ; for ( int i = 0 ; i < bitscount ; i ++ ) { int p ; if ( x1 == 0 && y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] ; else if ( x1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] ; else if ( y1 == 0 ) p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] ; else p = prefix_count [ i ] [ x2 ] [ y2 ] - prefix_count [ i ] [ x1 - 1 ] [ y2 ] - prefix_count [ i ] [ x2 ] [ y1 - 1 ] + prefix_count [ i ] [ x1 - 1 ] [ y1 - 1 ] ; if ( p != 0 ) ans = ( ans | ( 1 << i ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; findPrefixCount ( arr ) ; int queries [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( rangeOr ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , queries [ i ] [ 2 ] , queries [ i ] [ 3 ] ) ) ; } }
class GFG { static int maxLen = 10 ; static int dp [ ] = new int [ maxLen ] ; static boolean v [ ] = new boolean [ maxLen ] ; static int maxSum ( int arr [ ] , int i , int n ) { if ( i >= n ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . max ( maxSum ( arr , i + 1 , n ) , arr [ i ] + maxSum ( arr , i + 2 , n ) ) ; return dp [ i ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 9 , 7 , 33 } ; int n = arr . length ; System . out . println ( maxSum ( arr , 0 , n ) ) ; } }
class GFG { static int n = 3 ; static int maxV = 20 ; static int [ ] [ ] [ ] dp = new int [ n ] [ n ] [ maxV ] ; static int [ ] [ ] [ ] v = new int [ n ] [ n ] [ maxV ] ; static int countWays ( int i , int j , int x , int arr [ ] [ ] ) { if ( i == n j == n ) { return 0 ; } x = ( x & arr [ i ] [ j ] ) ; if ( x == 0 ) { return 0 ; } if ( i == n - 1 && j == n - 1 ) { return 1 ; } if ( v [ i ] [ j ] [ x ] == 1 ) { return dp [ i ] [ j ] [ x ] ; } v [ i ] [ j ] [ x ] = 1 ; dp [ i ] [ j ] [ x ] = countWays ( i + 1 , j , x , arr ) + countWays ( i , j + 1 , x , arr ) ; return dp [ i ] [ j ] [ x ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 1 } , { 1 , 1 , 0 } , { 2 , 1 , 1 } } ; System . out . println ( countWays ( 0 , 0 , arr [ 0 ] [ 0 ] , arr ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100 ; static int [ ] [ ] memo = new int [ MAX ] [ MAX ] ; static int oSRec ( int arr [ ] , int i , int j , int sum ) { if ( j == i + 1 ) return Math . max ( arr [ i ] , arr [ j ] ) ; if ( memo [ i ] [ j ] != - 1 ) return memo [ i ] [ j ] ; memo [ i ] [ j ] = Math . max ( ( sum - oSRec ( arr , i + 1 , j , sum - arr [ i ] ) ) , ( sum - oSRec ( arr , i , j - 1 , sum - arr [ j ] ) ) ) ; return memo [ i ] [ j ] ; } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; return sum ; } static int optimalStrategyOfGame ( int [ ] arr , int n ) { int sum = 0 ; sum = accumulate ( arr , 0 , n ) ; for ( int j = 0 ; j < MAX ; j ++ ) { for ( int k = 0 ; k < MAX ; k ++ ) memo [ j ] [ k ] = - 1 ; } return oSRec ( arr , 0 , n - 1 , sum ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = arr1 . length ; System . out . printf ( "%dNEW_LINE", optimalStrategyOfGame ( arr1 , n ) ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = arr2 . length ; System . out . printf ( "%dNEW_LINE", optimalStrategyOfGame ( arr2 , n ) ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . length ; System . out . printf ( "%dNEW_LINE", optimalStrategyOfGame ( arr3 , n ) ) ; } }
class GFG { static int N = 5 ; static int func ( int idx , int cur , int a [ ] , int dp [ ] [ ] , int n , int x ) { if ( idx == n ) { return 0 ; } if ( dp [ idx ] [ cur ] != - 1 ) { return dp [ idx ] [ cur ] ; } int ans = 0 ; if ( cur == 0 ) { ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 0 , a , dp , n , x ) ) ; ans = Math . max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; } else if ( cur == 1 ) { ans = Math . max ( ans , x * a [ idx ] + func ( idx + 1 , 1 , a , dp , n , x ) ) ; ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } { ans = Math . max ( ans , a [ idx ] + func ( idx + 1 , 2 , a , dp , n , x ) ) ; } return dp [ idx ] [ cur ] = ans ; } static int getMaximumSum ( int a [ ] , int n , int x ) { int dp [ ] [ ] = new int [ n ] [ 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int maxi = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . max ( maxi , func ( i , 0 , a , dp , n , x ) ) ; } return maxi ; } public static void main ( String [ ] args ) { int a [ ] = { - 3 , 8 , - 2 , 1 , - 6 } ; int n = a . length ; int x = - 1 ; System . out . println ( getMaximumSum ( a , n , x ) ) ; } }
class GFG { static int N = 100 ; static void pre_process ( boolean dp [ ] [ ] , char [ ] s ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = false ; } } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } } static int countPairs ( String s ) { boolean dp [ ] [ ] = new boolean [ N ] [ N ] ; pre_process ( dp , s . toCharArray ( ) ) ; int n = s . length ( ) ; int left [ ] = new int [ n ] ; int right [ ] = new int [ n ] ; left [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] == true ) { left [ i ] ++ ; } } } right [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { right [ i ] = right [ i + 1 ] ; for ( int j = n - 1 ; j >= i ; j -- ) { if ( dp [ i ] [ j ] == true ) { right [ i ] ++ ; } } } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += left [ i ] * right [ i + 1 ] ; } return ans ; } public static void main ( String [ ] args ) { String s = " abacaba " ; System . out . println ( countPairs ( s ) ) ; } }
class GFG { static int N = 100 ; static void pre_process ( boolean dp [ ] [ ] , char [ ] s ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = false ; } } for ( int j = 1 ; j <= n ; j ++ ) { for ( int i = 0 ; i <= n - j ; i ++ ) { if ( j <= 2 ) { if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = true ; } } else if ( s [ i ] == s [ i + j - 1 ] ) { dp [ i ] [ i + j - 1 ] = dp [ i + 1 ] [ i + j - 2 ] ; } } } } static void answerQuery ( int l , int r , boolean dp [ ] [ ] ) { if ( dp [ l ] [ r ] ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String s = " abaaab " ; boolean [ ] [ ] dp = new boolean [ N ] [ N ] ; pre_process ( dp , s . toCharArray ( ) ) ; int queries [ ] [ ] = { { 0 , 1 } , { 1 , 5 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) { answerQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , dp ) ; } } }
import java . util . * ; class GFG { static boolean check ( String s ) { int n = s . length ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' && s . charAt ( i - 1 ) == '1' && s . charAt ( i - 2 ) == '1' ) return true ; } return false ; } static int countStr ( int i , String s ) { if ( i < 0 ) { if ( check ( s ) ) return 1 ; return 0 ; } char [ ] myNameChars = s . toCharArray ( ) ; myNameChars [ i ] = '0' ; s = String . valueOf ( myNameChars ) ; int ans = countStr ( i - 1 , s ) ; char [ ] myChar = s . toCharArray ( ) ; myChar [ i ] = '1' ; s = String . valueOf ( myChar ) ; ans += countStr ( i - 1 , s ) ; char [ ] myChar1 = s . toCharArray ( ) ; myChar1 [ i ] = '0' ; s = String . valueOf ( myChar1 ) ; return ans ; } public static void main ( String args [ ] ) { int N = 4 ; String s = "0000" ; System . out . println ( countStr ( N - 1 , s ) ) ; } }
class GFG { static int findSum ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 1 ] = 1 ; dp [ 0 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = ( 4 * ( i * i ) ) - 6 * ( i - 1 ) + dp [ i - 2 ] ; } return dp [ n ] ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( findSum ( n ) ) ; } }
class GFG { static int n = 3 ; static int MAX = 30 ; static int dp [ ] [ ] [ ] = new int [ n ] [ n ] [ MAX ] ; static boolean v [ ] [ ] [ ] = new boolean [ n ] [ n ] [ MAX ] ; static int findCount ( int mat [ ] [ ] , int i , int j , int m ) { if ( i == 0 && j == 0 ) { if ( m == mat [ 0 ] [ 0 ] ) return 1 ; else return 0 ; } if ( m < 0 ) return 0 ; if ( i < 0 j < 0 ) return 0 ; if ( v [ i ] [ j ] [ m ] ) return dp [ i ] [ j ] [ m ] ; v [ i ] [ j ] [ m ] = true ; dp [ i ] [ j ] [ m ] = findCount ( mat , i - 1 , j , m - mat [ i ] [ j ] ) + findCount ( mat , i , j - 1 , m - mat [ i ] [ j ] ) ; return dp [ i ] [ j ] [ m ] ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int m = 5 ; System . out . println ( findCount ( mat , n - 1 , n - 1 , m ) ) ; } }
class GFG { static int mod = 1000000007 ; static int RUNMAX = 300 ; static int BALLMAX = 50 ; static int WICKETMAX = 10 ; static int CountWays ( int r , int b , int l , int R , int B , int W , int [ ] [ ] [ ] dp ) { if ( l > W ) return 0 ; if ( r > R ) return 0 ; if ( b == B && r == R ) return 1 ; if ( b == B ) return 0 ; if ( dp [ r ] [ b ] [ l ] != - 1 ) return dp [ r ] [ b ] [ l ] ; int ans = 0 ; ans += CountWays ( r , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 1 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 2 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 3 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 4 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r + 6 , b + 1 , l , R , B , W , dp ) ; ans = ans % mod ; ans += CountWays ( r , b + 1 , l + 1 , R , B , W , dp ) ; ans = ans % mod ; return dp [ r ] [ b ] [ l ] = ans ; } public static void main ( String [ ] args ) { int R = 40 , B = 10 , W = 4 ; int [ ] [ ] [ ] dp = new int [ RUNMAX ] [ BALLMAX ] [ WICKETMAX ] ; for ( int i = 0 ; i < RUNMAX ; i ++ ) for ( int j = 0 ; j < BALLMAX ; j ++ ) for ( int k = 0 ; k < WICKETMAX ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; System . out . println ( CountWays ( 0 , 0 , 0 , R , B , W , dp ) ) ; } }
class GFG { static int N = 3 ; static int FindMaximumSum ( int ind , int kon , int a [ ] , int b [ ] , int c [ ] , int n , int dp [ ] [ ] ) { if ( ind == n ) return 0 ; if ( dp [ ind ] [ kon ] != - 1 ) return dp [ ind ] [ kon ] ; int ans = ( int ) ( - 1e9 + 5 ) ; if ( kon == 0 ) { ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 1 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , c [ ind ] + FindMaximumSum ( ind + 1 , 2 , a , b , c , n , dp ) ) ; } else if ( kon == 2 ) { ans = Math . max ( ans , a [ ind ] + FindMaximumSum ( ind + 1 , 1 , a , b , c , n , dp ) ) ; ans = Math . max ( ans , b [ ind ] + FindMaximumSum ( ind + 1 , 0 , a , b , c , n , dp ) ) ; } return dp [ ind ] [ kon ] = ans ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 8 , 2 , 7 , 4 , 2 , 7 } ; int b [ ] = { 7 , 8 , 5 , 8 , 6 , 3 , 5 } ; int c [ ] = { 8 , 3 , 2 , 6 , 8 , 4 , 1 } ; int n = a . length ; int dp [ ] [ ] = new int [ n ] [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int x = FindMaximumSum ( 0 , 0 , a , b , c , n , dp ) ; int y = FindMaximumSum ( 0 , 1 , a , b , c , n , dp ) ; int z = FindMaximumSum ( 0 , 2 , a , b , c , n , dp ) ; System . out . println ( Math . max ( x , Math . max ( y , z ) ) ) ; } }
class GFG { static int N = 10 ; static int findMinimumDeletion ( int l , int r , int dp [ ] [ ] , String s ) { if ( l > r ) { return 0 ; } if ( l == r ) { return 1 ; } if ( dp [ l ] [ r ] != - 1 ) { return dp [ l ] [ r ] ; } int res = 1 + findMinimumDeletion ( l + 1 , r , dp , s ) ; for ( int i = l + 1 ; i <= r ; ++ i ) { if ( s . charAt ( l ) == s . charAt ( i ) ) { res = Math . min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) ) ; } } return dp [ l ] [ r ] = res ; } public static void main ( String [ ] args ) { String s = " abcddcba " ; int n = s . length ( ) ; int dp [ ] [ ] = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( findMinimumDeletion ( 0 , n - 1 , dp , s ) ) ; } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static int noOfBinaryStrings ( int N , int k ) { int dp [ ] = new int [ 100002 ] ; for ( int i = 1 ; i <= k - 1 ; i ++ ) { dp [ i ] = 1 ; } dp [ k ] = 2 ; for ( int i = k + 1 ; i <= N ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] + dp [ i - k ] ) % mod ; } return dp [ N ] ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 2 ; System . out . println ( noOfBinaryStrings ( N , K ) ) ; } }
class GFG { static int findWaysToPair ( int p ) { int dp [ ] = new int [ p + 1 ] ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; for ( int i = 3 ; i <= p ; i ++ ) { dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ p ] ; } public static void main ( String args [ ] ) { int p = 3 ; System . out . println ( findWaysToPair ( p ) ) ; } }
class GFG { static int C [ ] [ ] = new int [ 1000 ] [ 1000 ] ; static void binomialCoeff ( int n ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } } static int numberOfWays ( int x , int y , int z ) { binomialCoeff ( Math . max ( x , Math . max ( y , z ) ) ) ; int sum = 0 ; for ( int i = 1 ; i <= z ; i ++ ) { sum = ( sum + C [ z ] [ i ] ) ; } int sum1 = 0 ; for ( int i = 1 ; i <= y ; i ++ ) { for ( int j = i + 1 ; j <= x ; j ++ ) { sum1 = ( sum1 + ( C [ y ] [ i ] * C [ x ] [ j ] ) ) ; } } sum1 = ( sum * sum1 ) ; return sum1 ; } public static void main ( String args [ ] ) { int x = 3 ; int y = 2 ; int z = 1 ; System . out . println ( numberOfWays ( x , y , z ) ) ; } }
class GFG { static int findCost ( String str , int arr [ ] , int n ) { long costofC = 0 , costofO = 0 , costofD = 0 , costofE = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' c ' ) costofC += arr [ i ] ; else if ( str . charAt ( i ) == ' o ' ) costofO = Math . min ( costofC , costofO + arr [ i ] ) ; else if ( str . charAt ( i ) == ' d ' ) costofD = Math . min ( costofO , costofD + arr [ i ] ) ; else if ( str . charAt ( i ) == ' e ' ) costofE = Math . min ( costofD , costofE + arr [ i ] ) ; } return ( int ) costofE ; } public static void main ( String [ ] args ) { String str = " geekcodergeeks " ; int arr [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 6 , 4 , 6 , 2 , 3 , 3 , 3 , 2 } ; int n = arr . length ; System . out . print ( findCost ( str , arr , n ) ) ; } }
class GFG { static int findSubsequence ( int arr [ ] , int n ) { int len = 1 ; int tmp ; int i , j , d ; int [ ] [ ] dp = new int [ n ] [ 10 ] ; int [ ] cnt = new int [ 10 ] ; int locMax ; tmp = arr [ 0 ] ; while ( tmp > 0 ) { dp [ 0 ] [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( i = 1 ; i < n ; i ++ ) { tmp = arr [ i ] ; locMax = 1 ; for ( int x = 0 ; x < 10 ; x ++ ) cnt [ x ] = 0 ; while ( tmp > 0 ) { cnt [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = 1 ; for ( j = 0 ; j < i ; j ++ ) { dp [ i ] [ d ] = Math . max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) ; locMax = Math . max ( dp [ i ] [ d ] , locMax ) ; } } } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = locMax ; } } len = Math . max ( len , locMax ) ; } return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 12 , 44 , 29 , 33 , 96 , 89 } ; int n = arr . length ; System . out . println ( findSubsequence ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int M = 18 ; static int a , b , dp [ ] [ ] [ ] [ ] = new int [ M ] [ 90 ] [ 90 ] [ 2 ] ; static int prime [ ] = { 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 } ; static int count ( int pos , int even , int odd , int tight , Vector < Integer > num ) { if ( pos == num . size ( ) ) { if ( ( num . size ( ) & 1 ) != 0 ) { int t = odd ; odd = even ; even = t ; } int d = even - odd ; for ( int i = 0 ; i < 24 ; i ++ ) if ( d == prime [ i ] ) return 1 ; return 0 ; } if ( dp [ pos ] [ even ] [ odd ] [ tight ] != - 1 ) return dp [ pos ] [ even ] [ odd ] [ tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num . get ( pos ) ) ; for ( int d = 0 ; d <= limit ; d ++ ) { int currF = tight , currEven = even ; int currOdd = odd ; if ( d < num . get ( pos ) ) currF = 1 ; if ( ( pos & 1 ) != 0 ) currOdd += d ; else currEven += d ; ans += count ( pos + 1 , currEven , currOdd , currF , num ) ; } return dp [ pos ] [ even ] [ odd ] [ tight ] = ans ; } static int solve ( int x ) { Vector < Integer > num = new Vector < Integer > ( ) ; while ( x != 0 ) { num . add ( x % 10 ) ; x /= 10 ; } Collections . reverse ( num ) ; for ( int i = 0 ; i < dp . length ; i ++ ) for ( int j = 0 ; j < dp [ i ] . length ; j ++ ) for ( int k = 0 ; k < dp [ i ] [ j ] . length ; k ++ ) for ( int k1 = 0 ; k1 < dp [ i ] [ j ] [ k ] . length ; k1 ++ ) dp [ i ] [ j ] [ k ] [ k1 ] = - 1 ; return count ( 0 , 0 , 0 , 0 , num ) ; } public static void main ( String args [ ] ) { int L = 1 , R = 50 ; System . out . println ( solve ( R ) - solve ( L - 1 ) ) ; L = 50 ; R = 100 ; System . out . println ( solve ( R ) - solve ( L - 1 ) ) ; } }
import java . util . * ; class GFG { static final int N = 5005 ; static int n , k ; static Vector < Integer > [ ] gr = new Vector [ N ] ; static int [ ] [ ] d = new int [ N ] [ 505 ] ; static int ans = 0 ; static void Add_edge ( int x , int y ) { gr [ x ] . add ( y ) ; gr [ y ] . add ( x ) ; } static void dfs ( int v , int par ) { d [ v ] [ 0 ] = 1 ; for ( Integer i : gr [ v ] ) { if ( i != par ) { dfs ( i , v ) ; for ( int j = 1 ; j <= k ; j ++ ) ans += d [ i ] [ j - 1 ] * d [ v ] [ k - j ] ; for ( int j = 1 ; j <= k ; j ++ ) d [ v ] [ j ] += d [ i ] [ j - 1 ] ; } } } public static void main ( String [ ] args ) { n = 5 ; k = 2 ; for ( int i = 0 ; i < N ; i ++ ) gr [ i ] = new Vector < Integer > ( ) ; Add_edge ( 1 , 2 ) ; Add_edge ( 2 , 3 ) ; Add_edge ( 3 , 4 ) ; Add_edge ( 2 , 5 ) ; dfs ( 1 , 0 ) ; System . out . print ( ans ) ; } }
import java . util . * ; class GFG { static int findXorSum ( int arr [ ] , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { int c_odd = 0 ; boolean odd = false ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) odd = ( ! odd ) ; if ( odd ) c_odd ++ ; } for ( int j = 0 ; j < n ; j ++ ) { sum += ( mul * c_odd ) ; if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) c_odd = ( n - j - c_odd ) ; } mul *= 2 ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 8 , 13 } ; int n = arr . length ; System . out . println ( findXorSum ( arr , n ) ) ; } }
class GFG { static int charVal ( String s , int i ) { if ( s . charAt ( i ) == '0' ) { return 0 ; } else if ( s . charAt ( i ) == '1' ) { return 1 ; } else { return 2 ; } } static int countMinimalReplacements ( String s , int i , int prev , int dp [ ] [ ] , int n ) { if ( i == n ) { return 0 ; } if ( dp [ i ] [ prev ] != - 1 ) { return dp [ i ] [ prev ] ; } int val = charVal ( s , i ) ; int ans = Integer . MAX_VALUE ; if ( val == prev ) { val = 0 ; for ( int cur = 0 ; cur <= 2 ; cur ++ ) { if ( cur == prev ) { continue ; } val = 1 + countMinimalReplacements ( s , i + 1 , cur , dp , n ) ; ans = Math . min ( ans , val ) ; } { ans = countMinimalReplacements ( s , i + 1 , val , dp , n ) ; } return dp [ i ] [ val ] = ans ; } public static void main ( String [ ] args ) { String s = "201220211" ; int n = s . length ( ) ; int dp [ ] [ ] = new int [ n ] [ 3 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int val = charVal ( s , 0 ) ; System . out . println ( countMinimalReplacements ( s , 1 , val , dp , n ) ) ; } }
class GFG { static int MAX = 50 ; static boolean hasPath ( int maze [ ] [ ] , int sol [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) sol [ i ] [ j ] = 0 ; boolean [ ] [ ] CRF = new boolean [ N ] [ N ] ; CRF [ N - 1 ] [ N - 1 ] = true ; for ( int k = N - 1 ; k >= 0 ; k -- ) { for ( int j = k ; j >= 0 ; j -- ) { if ( ! ( k == N - 1 && j == N - 1 ) ) { for ( int a = 0 ; a <= maze [ k ] [ j ] ; a ++ ) { if ( ( j + a < N && CRF [ k ] [ j + a ] == true ) || ( k + a < N && CRF [ k + a ] [ j ] == true ) ) { CRF [ k ] [ j ] = true ; break ; } } for ( int a = 0 ; a <= maze [ j ] [ k ] ; a ++ ) { if ( ( k + a < N && CRF [ j ] [ k + a ] == true ) || ( j + a < N && CRF [ j + a ] [ k ] == true ) ) { CRF [ j ] [ k ] = true ; break ; } } } } } if ( CRF [ 0 ] [ 0 ] == false ) return false ; int i = 0 , j = 0 ; while ( ! ( i == N - 1 && j == N - 1 ) ) { sol [ i ] [ j ] = 1 ; if ( maze [ i ] [ j ] > 0 ) for ( int a = 1 ; a <= maze [ i ] [ j ] ; a ++ ) { if ( ( j + a < N && CRF [ i ] [ j + a ] == true ) ) { j = j + a ; break ; } else if ( ( i + a < N && CRF [ i + a ] [ j ] == true ) ) { i = i + a ; break ; } } } sol [ N - 1 ] [ N - 1 ] = 1 ; return true ; } static void printMatrix ( int sol [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( sol [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 2 , 2 , 1 , 1 , 0 } , { 0 , 0 , 3 , 0 , 0 } , { 1 , 0 , 0 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 0 , 3 , 0 , 0 } } ; int N = maze . length ; int [ ] [ ] sol = new int [ N ] [ MAX ] ; if ( hasPath ( maze , sol , N ) ) printMatrix ( sol , N ) ; else System . out . println ( " No ▁ path ▁ exists " ) ; } }
class GFG { final static int n = 4 ; final static int m = 4 ; static float calcProbability ( int M [ ] [ ] , int k ) { float dp [ ] [ ] = new float [ m ] [ n ] ; float sum [ ] = new float [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { dp [ 0 ] [ j ] = M [ 0 ] [ j ] ; sum [ 0 ] = sum [ 0 ] + dp [ 0 ] [ j ] ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] / sum [ i - 1 ] + M [ i ] [ j ] ; sum [ i ] += dp [ i ] [ j ] ; } } return dp [ n - 1 ] [ k - 1 ] / sum [ n - 1 ] ; } public static void main ( String [ ] args ) { int M [ ] [ ] = { { 1 , 1 , 0 , 3 } , { 2 , 3 , 2 , 3 } , { 9 , 3 , 0 , 2 } , { 2 , 3 , 2 , 2 } } ; int k = 3 ; System . out . println ( calcProbability ( M , k ) ) ; } }
import java . util . * ; class GFG { static int MaximumNumbers ( String s ) { int n = s . length ( ) ; int [ ] remIndex = { - 1 , - 1 , - 1 } ; remIndex [ 0 ] = 0 ; int [ ] res = new int [ n + 1 ] ; int r = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { r = ( r + s . charAt ( i - 1 ) - '0' ) % 3 ; res [ i ] = res [ i - 1 ] ; if ( remIndex [ r ] != - 1 ) res [ i ] = Math . max ( res [ i ] , res [ remIndex [ r ] ] + 1 ) ; remIndex [ r ] = i + 1 ; } return res [ n ] ; } public static void main ( String [ ] args ) { String s = "12345" ; System . out . println ( MaximumNumbers ( s ) ) ; } }
class GFG { static int INF = 99999 ; static int findMinimumSteps ( int mat [ ] [ ] , int x , int y , int n ) { int i , j , k ; int [ ] [ ] dist = new int [ n ] [ n ] ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( mat [ i ] [ j ] == 0 ) dist [ i ] [ j ] = INF ; else dist [ i ] [ j ] = 1 ; if ( i == j ) dist [ i ] [ j ] = 1 ; } } for ( k = 0 ; k < n ; k ++ ) { for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } if ( dist [ x ] [ y ] < INF ) return dist [ x ] [ y ] ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 } } ; int x = 2 , y = 3 ; int size = mat . length ; System . out . println ( findMinimumSteps ( mat , x , y , size ) ) ; } }
public class GFG { static int max = 4 ; static int c = 2 ; static int countPaths ( int sum , int get , int m , int n , int dp [ ] [ ] ) { if ( sum < 0 ) return 0 ; if ( sum == 0 ) return get ; if ( dp [ sum ] [ get ] != - 1 ) return dp [ sum ] [ get ] ; int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i == m ) res += countPaths ( sum - i , 1 , m , n , dp ) ; res += countPaths ( sum - i , get , m , n , dp ) ; } dp [ sum ] [ get ] = res ; return dp [ sum ] [ get ] ; } public static void main ( String [ ] args ) { int n = 3 , m = 2 , x = 3 ; int dp [ ] [ ] = new int [ max + 1 ] [ 2 ] ; for ( int i = 0 ; i <= max ; i ++ ) for ( int j = 0 ; j < 2 ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . println ( countPaths ( x , 0 , m , n , dp ) ) ; } }
import java . io . * ; class GFG { static int MAX_K = 15 ; static int fac [ ] = new int [ MAX_K ] ; static void factorial ( int k ) { fac [ 0 ] = 1 ; for ( int i = 1 ; i <= k + 1 ; i ++ ) { fac [ i ] = ( i * fac [ i - 1 ] ) ; } } static int bin ( int a , int b ) { int ans = ( ( fac [ a ] ) / ( fac [ a - b ] * fac [ b ] ) ) ; return ans ; } static int sumofn ( int n , int k ) { int p = 0 ; int num1 , temp ; int arr [ ] = new int [ 1000 ] ; for ( int j = 1 ; j <= k ; j ++ ) { if ( j == 1 ) { num1 = ( n * ( n + 1 ) ) / 2 ; arr [ p ++ ] = num1 ; temp = num1 ; } else { temp = ( ( int ) Math . pow ( n + 1 , j + 1 ) - 1 - n ) ; for ( int s = 1 ; s < j ; s ++ ) { temp = temp - ( arr [ j - s - 1 ] * bin ( j + 1 , s + 1 ) ) ; } temp = temp / ( j + 1 ) ; arr [ p ++ ] = temp ; } } temp = arr [ p - 1 ] ; return temp ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; factorial ( k ) ; System . out . println ( sumofn ( n , k ) ) ; } }
class solution { static final int max = 4 ; static int countWays ( int index , int cnt , int dp [ ] [ ] , int n , int m , int k ) { if ( index == n ) { if ( cnt == k ) return 1 ; else return 0 ; } if ( dp [ index ] [ cnt ] != - 1 ) return dp [ index ] [ cnt ] ; int ans = 0 ; ans += countWays ( index + 1 , cnt , dp , n , m , k ) ; ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) ; return dp [ index ] [ cnt ] = ans ; } public static void main ( String args [ ] ) { int n = 3 , m = 3 , k = 2 ; int dp [ ] [ ] = new int [ n + 1 ] [ max ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < max ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . println ( m * countWays ( 1 , 0 , dp , n , m , k ) ) ; } }
import java . util . * ; class solution { static int waysToKAdjacentSetBits ( int n , int k , int currentIndex , int adjacentSetBits , int lastBit ) { if ( currentIndex == n ) { if ( adjacentSetBits == k ) return 1 ; return 0 ; } int noOfWays = 0 ; if ( lastBit == 1 ) { noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits + 1 , 1 ) ; noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } else if ( lastBit == 0 ) { noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 1 ) ; noOfWays += waysToKAdjacentSetBits ( n , k , currentIndex + 1 , adjacentSetBits , 0 ) ; } return noOfWays ; } public static void main ( String args [ ] ) { int n = 5 , k = 2 ; int totalWays = waysToKAdjacentSetBits ( n , k , 1 , 0 , 1 ) + waysToKAdjacentSetBits ( n , k , 1 , 0 , 0 ) ; System . out . println ( " Number ▁ of ▁ ways ▁ = ▁ " + totalWays ) ; } }
class GFG { static int MAX = 300 ; static int checkFunc ( int i , int j , String st ) { if ( st . charAt ( i ) == ' ( ' && st . charAt ( j ) == ' ) ' ) return 1 ; if ( st . charAt ( i ) == ' ( ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' ) ' ) return 1 ; if ( st . charAt ( i ) == ' [ ' && st . charAt ( j ) == ' ] ' ) return 1 ; if ( st . charAt ( i ) == ' [ ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' ] ' ) return 1 ; if ( st . charAt ( i ) == ' { ' && st . charAt ( j ) == ' } ' ) return 1 ; if ( st . charAt ( i ) == ' { ' && st . charAt ( j ) == ' ? ' ) return 1 ; if ( st . charAt ( i ) == ' ? ' && st . charAt ( j ) == ' } ' ) return 1 ; return 0 ; } static int countRec ( int start , int end , int dp [ ] [ ] , String st ) { int sum = 0 ; if ( start > end ) return 1 ; if ( dp [ start ] [ end ] != - 1 ) return dp [ start ] [ end ] ; int i , r = 0 ; for ( i = start + 1 ; i <= end ; i += 2 ) { if ( checkFunc ( start , i , st ) == 1 ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) ; } else if ( st . charAt ( start ) == ' ? ' && st . charAt ( i ) == ' ? ' ) { sum = sum + countRec ( start + 1 , i - 1 , dp , st ) * countRec ( i + 1 , end , dp , st ) * 3 ; } } return dp [ start ] [ end ] = sum ; } static int countWays ( String st ) { int n = st . length ( ) ; if ( n % 2 == 1 ) return 0 ; int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; return countRec ( 0 , n - 1 , dp , st ) ; } public static void main ( String [ ] args ) { String st = " ( ? ( [ ? ) ] ? } ? " ; System . out . println ( countWays ( st ) ) ; } }
import java . util . * ; class GfG { static int dp [ ] [ ] [ ] [ ] = new int [ 64 ] [ 64 ] [ 64 ] [ 2 ] ; static int func ( int third , int seta , int setb , int carry , int number ) { if ( dp [ third ] [ seta ] [ setb ] [ carry ] != - 1 ) return dp [ third ] [ seta ] [ setb ] [ carry ] ; int shift = ( number >> third ) ; if ( shift == 0 && seta == 0 && setb == 0 && carry == 0 ) return 1 ; if ( shift == 0 seta < 0 setb < 0 ) return 0 ; int mask = shift & 1 ; dp [ third ] [ seta ] [ setb ] [ carry ] = 0 ; if ( ( mask == 1 ) && carry == 1 ) { dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) ; } else if ( mask == 1 && carry == 0 ) { dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta - 1 , setb , 0 , number ) + func ( third + 1 , seta , setb - 1 , 0 , number ) ; } else if ( mask == 0 && carry == 1 ) { dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta - 1 , setb , 1 , number ) + func ( third + 1 , seta , setb - 1 , 1 , number ) ; } else if ( mask == 0 && carry == 0 ) { dp [ third ] [ seta ] [ setb ] [ carry ] += func ( third + 1 , seta , setb , 0 , number ) + func ( third + 1 , seta - 1 , setb - 1 , 1 , number ) ; } return dp [ third ] [ seta ] [ setb ] [ carry ] ; } static int possibleSwaps ( int a , int b , int c ) { for ( int q = 0 ; q < 64 ; q ++ ) { for ( int r = 0 ; r < 64 ; r ++ ) { for ( int p = 0 ; p < 64 ; p ++ ) { for ( int d = 0 ; d < 2 ; d ++ ) { dp [ q ] [ r ] [ p ] [ d ] = - 1 ; } } } } int ans = func ( 0 , a , b , 0 , c ) ; return ans ; } public static void main ( String [ ] args ) { int x = 2 , y = 2 , c = 20 ; System . out . println ( possibleSwaps ( x , y , c ) ) ; } }
import java . io . * ; class GFG { static int CountWays ( int n ) { if ( n == 0 ) { return 1 ; } if ( n == 1 ) { return 1 ; } if ( n == 2 ) { return 1 + 1 ; } return CountWays ( n - 1 ) + CountWays ( n - 3 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( CountWays ( n ) ) ; } }
import java . io . * ; class GFG { static int calculateEvenSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ 2 * n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = 0 ; for ( int i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + + calculateEvenSum ( n ) ) ; } }
class GFG { static int MAX = 1000 ; static int f [ ] = new int [ MAX ] ; static int fib ( int n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] == 1 ) { return f [ n ] ; } int k = ( n % 2 == 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n % 2 == 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateEvenSum ( int n ) { return ( fib ( 2 * n + 1 ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateEvenSum ( n ) ) ; } }
class GFG { static final int MAX = 32768 ; static int [ ] arr = new int [ 2 * MAX ] ; static void gouldSequence ( ) { arr [ 0 ] = 1 ; int i = 1 ; int p = 1 ; while ( i <= MAX ) { int j = 0 ; while ( j < i ) { arr [ i + j ] = 2 * arr [ j ] ; j ++ ; } i = ( 1 << p ) ; p ++ ; } } static void printSequence ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { gouldSequence ( ) ; int n = 16 ; printSequence ( n ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } static int countWays ( int m , int n ) { m = m - 1 ; n = n - 1 ; return factorial ( m + n ) / ( factorial ( m ) * factorial ( n ) ) ; } public static void main ( String [ ] args ) { int m = 5 ; int n = 5 ; int result = countWays ( m , n ) ; System . out . println ( result ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MatrixChainOrder ( int p [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ n ] ; for ( int i = 1 ; i < n ; i ++ ) dp [ i ] [ i ] = 0 ; for ( int L = 1 ; L < n - 1 ; L ++ ) for ( int i = 1 ; i < n - L ; i ++ ) dp [ i ] [ i + L ] = Math . min ( dp [ i + 1 ] [ i + L ] + p [ i - 1 ] * p [ i ] * p [ i + L ] , dp [ i ] [ i + L - 1 ] + p [ i - 1 ] * p [ i + L - 1 ] * p [ i + L ] ) ; return dp [ 1 ] [ n - 1 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 20 , 30 , 40 , 30 } ; int size = arr . length ; System . out . print ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , size ) ) ; } }
public class GFG { static int CommomSubsequencesCount ( String s , String t ) { int n1 = s . length ( ) ; int n2 = t . length ( ) ; int dp [ ] [ ] = new int [ n1 + 1 ] [ n2 + 1 ] ; char ch1 , ch2 ; for ( int i = 0 ; i <= n1 ; i ++ ) { for ( int j = 0 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 1 ; i <= n1 ; i ++ ) { for ( int j = 1 ; j <= n2 ; j ++ ) { ch1 = s . charAt ( i - 1 ) ; ch2 = t . charAt ( j - 1 ) ; if ( ch1 == ch2 ) dp [ i ] [ j ] = 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ; } } return dp [ n1 ] [ n2 ] ; } public static void main ( String args [ ] ) { String s = " ajblqcpdz " ; String t = " aefcnbtdi " ; System . out . println ( CommomSubsequencesCount ( s , t ) ) ; } }
import java . util . * ; class GFG { static int createPalindrome ( int input , int isOdd ) { int n = input ; int palin = input ; if ( isOdd % 2 == 1 ) { n /= 10 ; } while ( n > 0 ) { palin = palin * 10 + ( n % 10 ) ; n /= 10 ; } return palin ; } static Vector < Integer > generatePalindromes ( int N ) { Vector < Integer > palindromes = new Vector < > ( ) ; int number ; for ( int j = 0 ; j < 2 ; j ++ ) { int i = 1 ; while ( ( number = createPalindrome ( i ++ , j ) ) <= N ) { palindromes . add ( number ) ; } } return palindromes ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } static int minimumNoOfPalindromes ( int N ) { String a = String . valueOf ( N ) ; String b = String . valueOf ( N ) ; b = reverse ( b ) ; if ( a . equals ( b ) ) { return 1 ; } Vector < Integer > palindromes = generatePalindromes ( N ) ; Collections . sort ( palindromes ) ; int l = 0 , r = palindromes . size ( ) - 1 ; while ( l < r ) { if ( palindromes . get ( l ) + palindromes . get ( r ) == N ) { return 2 ; } else if ( palindromes . get ( l ) + palindromes . get ( r ) < N ) { ++ l ; } else { -- r ; } } return 3 ; } public static void main ( String [ ] args ) { int N = 65 ; System . out . println ( minimumNoOfPalindromes ( N ) ) ; } }
import java . io . * ; class GFG { static int lcs ( int dp [ ] [ ] , String a , String b , int m , int n ) { for ( int i = 0 ; i < 100 ; i ++ ) for ( int j = 0 ; j < 100 ; j ++ ) dp [ i ] [ j ] = - 1 ; if ( m < 0 n < 0 ) { return 0 ; } if ( dp [ m ] [ n ] != - 1 ) return dp [ m ] [ n ] ; int ans = 0 ; if ( a . charAt ( m ) == b . charAt ( n ) ) { ans = ( a . charAt ( m ) - 48 ) + lcs ( dp , a , b , m - 1 , n - 1 ) ; } else ans = Math . max ( lcs ( dp , a , b , m - 1 , n ) , lcs ( dp , a , b , m , n - 1 ) ) ; dp [ m ] [ n ] = ans ; return ans ; } static int costOfString ( String str ) { int cost = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) cost += ( str . charAt ( i ) - 48 ) ; return cost ; } public static void main ( String [ ] args ) { String a , b ; a = "9142" ; b = "1429" ; int dp [ ] [ ] = new int [ 101 ] [ 101 ] ; System . out . print ( ( costOfString ( a ) + costOfString ( b ) - 2 * lcs ( dp , a , b , a . length ( ) - 1 , b . length ( ) - 1 ) ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100 ; static int start , end ; static boolean sumEqualToK ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) { maxLen = i + 1 ; start = 0 ; end = i ; } if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - k ) ) { if ( maxLen < ( i - um . get ( sum - k ) ) ) { maxLen = i - um . get ( sum - k ) ; start = um . get ( sum - k ) + 1 ; end = i ; } } } return ( maxLen != 0 ) ; } static void sumZeroMatrix ( int mat [ ] [ ] , int row , int col , int k ) { int [ ] temp = new int [ row ] ; int area ; boolean sum = false ; int fup = 0 , fdown = 0 , fleft = 0 , fright = 0 ; int maxArea = Integer . MIN_VALUE ; for ( int left = 0 ; left < col ; left ++ ) { temp = memset ( temp , 0 ) ; for ( int right = left ; right < col ; right ++ ) { for ( int i = 0 ; i < row ; i ++ ) temp [ i ] += mat [ i ] [ right ] ; sum = sumEqualToK ( temp , row , k ) ; area = ( end - start + 1 ) * ( right - left + 1 ) ; if ( sum && maxArea < area ) { fup = start ; fdown = end ; fleft = left ; fright = right ; maxArea = area ; } } } if ( fup == 0 && fdown == 0 && fleft == 0 && fright == 0 && mat [ 0 ] [ 0 ] != k ) { System . out . print ( " No ▁ sub - matrix ▁ with ▁ sum ▁ " + k + " ▁ exists " ) ; return ; } System . out . print ( " ( Top , ▁ Left ) : ▁ " + " ( " + fup + " , ▁ " + fleft + " ) " + "NEW_LINE"); System . out . print ( " ( Bottom , ▁ Right ) : ▁ " + " ( " + fdown + " , ▁ " + fright + " ) " + "NEW_LINE"); for ( int j = fup ; j <= fdown ; j ++ ) { for ( int i = fleft ; i <= fright ; i ++ ) System . out . print ( mat [ j ] [ i ] + " ▁ " ) ; System . out . println ( ) ; } } static int [ ] memset ( int [ ] arr , int val ) { for ( int i = 0 ; i < arr . length ; i ++ ) arr [ i ] = val ; return arr ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 7 , - 6 , 5 } , { - 8 , 6 , 7 , - 2 } , { 10 , - 15 , 3 , 2 } , { - 5 , 2 , 0 , 9 } } ; int row = 4 , col = 4 ; int k = 7 ; sumZeroMatrix ( mat , row , col , k ) ; } }
class GFG { static long countPartitions ( int n , int k ) { long [ ] [ ] dp = new long [ 201 ] [ 201 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } if ( dp [ n ] [ k ] >= 0 ) return dp [ n ] [ k ] ; if ( n < k ) return 0 ; if ( n < 2 * k ) return 1 ; long answer = 1 ; for ( int i = k ; i < n ; i ++ ) answer = answer + countPartitions ( n - i , i ) ; dp [ n ] [ k ] = answer ; return answer ; } public static void main ( String [ ] args ) { int n = 10 , k = 3 ; System . out . println ( " Total ▁ Aggregate ▁ sum ▁ of ▁ " + " all ▁ Valid ▁ Partitions : ▁ " + countPartitions ( n , k ) * n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long recursive ( long idx , long sum , long tight , String st , long [ ] [ ] [ ] dp , long num ) { if ( idx == num ) { return sum == 0 ? 1 : 0 ; } if ( dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] != - 1 ) return dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] ; long newTight ; long ans = 0 ; long d ; for ( d = 0 ; d < 10 ; ++ d ) { newTight = 0 ; if ( tight == 1 && st . charAt ( ( int ) idx ) - '0' < d ) continue ; if ( tight == 1 && st . charAt ( ( int ) idx ) - '0' == d ) newTight = 1 ; if ( sum >= d ) ans += recursive ( idx + 1 , sum - d , ( int ) newTight , st , dp , num ) ; } return dp [ ( int ) idx ] [ ( int ) tight ] [ ( int ) sum ] = ans ; } static ArrayList < Long > formArray ( long N ) { long [ ] [ ] [ ] dp = new long [ 20 ] [ 2 ] [ 166 ] ; for ( long [ ] [ ] innerRow : dp ) { for ( long [ ] innerInnerRow : innerRow ) { Arrays . fill ( innerInnerRow , - 1 ) ; } } String st = String . valueOf ( N ) ; long num = st . length ( ) ; ArrayList < Long > arr = new ArrayList < Long > ( ) ; for ( int i = 1 ; i <= 162 ; ++ i ) { arr . add ( recursive ( 0 , i , 1 , st , dp , num ) ) ; } return arr ; } static long findPair ( long a , long b ) { ArrayList < Long > arr_smaller = formArray ( a - 1 ) ; ArrayList < Long > arr_greater = formArray ( b ) ; for ( int i = 0 ; i < arr_greater . size ( ) ; ++ i ) { arr_greater . set ( i , arr_greater . get ( i ) - arr_smaller . get ( i ) ) ; } long ans = 0 ; for ( int i = 1 ; i <= 162 ; ++ i ) { for ( int j = i + 1 ; j <= 162 ; ++ j ) { if ( gcd ( i , j ) == 1 ) { ans = ( ans + arr_greater . get ( i - 1 ) * arr_greater . get ( j - 1 ) ) ; } } } return ans ; } public static void main ( String [ ] args ) { long a = 12 , b = 15 ; System . out . println ( findPair ( a , b ) ) ; } }
import java . util . * ; class GFG { static ArrayList < Integer > factors ( int n ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; v . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . add ( i ) ; if ( n / i != i ) { v . add ( n / i ) ; } } } return v ; } static boolean checkAbundant ( int n ) { ArrayList < Integer > v ; int sum = 0 ; v = factors ( n ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { sum += v . get ( i ) ; } if ( sum > n ) return true ; else return false ; } static boolean checkSemiPerfect ( int n ) { ArrayList < Integer > v ; v = factors ( n ) ; Collections . sort ( v ) ; int r = v . size ( ) ; boolean subset [ ] [ ] = new boolean [ r + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v . get ( i - 1 ) ] ; } } } if ( ( subset [ r ] [ n ] ) == false ) return false ; else return true ; } static boolean checkweird ( int n ) { if ( checkAbundant ( n ) == true && checkSemiPerfect ( n ) == false ) return true ; else return false ; } public static void main ( String args [ ] ) { int n = 70 ; if ( checkweird ( n ) ) System . out . println ( " Weird ▁ Number " ) ; else System . out . println ( " Not ▁ Weird ▁ Number " ) ; } }
import java . util . * ; class GFG { static Vector < Integer > factors ( int n ) { Vector < Integer > v = new Vector < > ( ) ; v . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . add ( i ) ; if ( n / i != i ) { v . add ( n / i ) ; } } } return v ; } static boolean check ( int n ) { Vector < Integer > v = new Vector < > ( ) ; v = factors ( n ) ; Collections . sort ( v ) ; int r = v . size ( ) ; boolean [ ] [ ] subset = new boolean [ r + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= r ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= r ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( j < v . elementAt ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; else { subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - v . elementAt ( i - 1 ) ] ; } } } if ( ( subset [ r ] [ n ] ) == false ) return false ; else return true ; } public static void main ( String [ ] args ) { int n = 40 ; if ( check ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
public class AQW { static int fib [ ] = new int [ 43 ] ; static void fibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( int i = 2 ; i < 43 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } static int rec ( int x , int y , int last ) { if ( y == 0 ) { if ( x == 0 ) return 1 ; return 0 ; } int sum = 0 ; for ( int i = last ; i >= 0 && fib [ i ] * y >= x ; i -- ) { if ( fib [ i ] > x ) continue ; sum += rec ( x - fib [ i ] , y - 1 , i ) ; } return sum ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int n = 13 , k = 3 ; System . out . println ( " Possible ▁ ways ▁ are : ▁ " + rec ( n , k , 42 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minimumCost ( int cost [ ] , int n ) { int dp [ ] = new int [ n ] ; if ( n == 1 ) return cost [ 0 ] ; dp [ 0 ] = cost [ 0 ] ; dp [ 1 ] = cost [ 1 ] ; for ( int i = 2 ; i < n ; i ++ ) { dp [ i ] = Math . min ( dp [ i - 1 ] , dp [ i - 2 ] ) + cost [ i ] ; } return Math . min ( dp [ n - 2 ] , dp [ n - 1 ] ) ; } public static void main ( String args [ ] ) { int a [ ] = { 16 , 19 , 10 , 12 , 18 } ; int n = a . length ; System . out . print ( minimumCost ( a , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minimumCost ( int cost [ ] , int n ) { int dp1 = 0 , dp2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int dp0 = cost [ i ] + Math . min ( dp1 , dp2 ) ; dp2 = dp1 ; dp1 = dp0 ; } return Math . min ( dp1 , dp2 ) ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 5 , 3 , 1 , 7 , 3 , 4 } ; int n = a . length ; System . out . print ( minimumCost ( a , n ) ) ; } }
class GFG { static int size = 1000 ; static int maxCost ( int a [ ] [ ] , int m , int n , int dp [ ] [ ] ) { if ( n < 0 m < 0 ) return ( int ) - 1e9 ; else if ( m == 0 && n == 0 ) return 0 ; else if ( dp [ m ] [ n ] != - 1 ) return dp [ m ] [ n ] ; else { int num = m + n ; if ( ( num & ( num - 1 ) ) == 0 ) return dp [ m ] [ n ] = a [ m ] [ n ] + maxCost ( a , m - 1 , n - 1 , dp ) ; else return dp [ m ] [ n ] = ( a [ m ] [ n ] + Math . max ( maxCost ( a , m - 1 , n , dp ) , maxCost ( a , m , n - 1 , dp ) ) ) ; } } static int answer ( int a [ ] [ ] , int n ) { int dp [ ] [ ] = new int [ size ] [ size ] ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return maxCost ( a , n - 1 , n - 1 , dp ) ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 , 1 } , { 4 , 5 , 6 , 1 } , { 7 , 8 , 9 , 1 } , { 1 , 1 , 1 , 1 } } ; int n = 4 ; System . out . println ( answer ( a , n ) ) ; } }
class GFG { static int editDistanceWith2Ops ( String X , String Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } int lcs = L [ m ] [ n ] ; return ( m - lcs ) + ( n - lcs ) ; } public static void main ( String [ ] args ) { String X = " abc " , Y = " acd " ; System . out . println ( editDistanceWith2Ops ( X , Y ) ) ; } }
class GFG { static int lcs ( String X , String Y , int m , int n ) { if ( m == 0 n == 0 ) { return 0 ; } if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) { return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; } else { return Math . max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; } } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . length ( ) ; int n = Y . length ( ) ; System . out . println ( " Length ▁ of ▁ LCS : ▁ " + lcs ( X , Y , m , n ) ) ; } }
import java . io . * ; class GFG { static int countPaths ( int n ) { int zB = 1 ; int zADC = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int nzB = zADC * 3 ; int nzADC = ( zADC * 2 + zB ) ; zB = nzB ; zADC = nzADC ; } return zB ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countPaths ( n ) ) ; } }
import java . util . * ; class GFG { static int start , finish ; static int lenOfLongSubarr ( int [ ] arr , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 1 ) { start = 0 ; finish = i ; maxLen = i + 1 ; } else if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) start = um . get ( sum - 1 ) + 1 ; finish = i ; maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } static void largestSubmatrix ( int [ ] [ ] mat , int n ) { int finalLeft = 0 , finalRight = 0 , finalTop = 0 , finalBottom = 0 ; int maxArea = 0 , len ; finish = 0 ; start = 0 ; int [ ] temp = new int [ n ] ; for ( int left = 0 ; left < n ; left ++ ) { Arrays . fill ( temp , 0 ) ; for ( int right = left ; right < n ; right ++ ) { for ( int i = 0 ; i < n ; ++ i ) temp [ i ] += mat [ i ] [ right ] == 0 ? - 1 : 1 ; len = lenOfLongSubarr ( temp , n ) ; if ( ( len != 0 ) && ( maxArea < ( finish - start + 1 ) * ( right - left + 1 ) ) ) { finalLeft = left ; finalRight = right ; finalTop = start ; finalBottom = finish ; maxArea = ( finish - start + 1 ) * ( right - left + 1 ) ; } } } System . out . print ( " ( Top , ▁ Left ) : ▁ ( " + finalTop + " , ▁ " + finalLeft + ")NEW_LINE"); System . out . print ( " ( Bottom , ▁ Right ) : ▁ ( " + finalBottom + " , ▁ " + finalRight + ")NEW_LINE"); System . out . print ( " Maximum ▁ area : ▁ " + maxArea ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = new int [ ] [ ] { { 1 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 0 , 0 , 0 } , { 0 , 1 , 0 , 1 } } ; int n = 4 ; largestSubmatrix ( mat , n ) ; } }
public class GFG { static int dp [ ] [ ] = new int [ 8 ] [ 8 ] ; static int getsteps ( int x , int y , int tx , int ty ) { if ( x == tx && y == ty ) { return dp [ 0 ] [ 0 ] ; if ( dp [ Math . abs ( x - tx ) ] [ Math . abs ( y - ty ) ] != 0 ) { return dp [ Math . abs ( x - tx ) ] [ Math . abs ( y - ty ) ] ; } else { int x1 , y1 , x2 , y2 ; if ( x <= tx ) { if ( y <= ty ) { x1 = x + 2 ; y1 = y + 1 ; x2 = x + 1 ; y2 = y + 2 ; } else { x1 = x + 2 ; y1 = y - 1 ; x2 = x + 1 ; y2 = y - 2 ; } } else if ( y <= ty ) { x1 = x - 2 ; y1 = y + 1 ; x2 = x - 1 ; y2 = y + 2 ; } else { x1 = x - 2 ; y1 = y - 1 ; x2 = x - 1 ; y2 = y - 2 ; } dp [ Math . abs ( x - tx ) ] [ Math . abs ( y - ty ) ] = Math . min ( getsteps ( x1 , y1 , tx , ty ) , getsteps ( x2 , y2 , tx , ty ) ) + 1 ; dp [ Math . abs ( y - ty ) ] [ Math . abs ( x - tx ) ] = dp [ Math . abs ( x - tx ) ] [ Math . abs ( y - ty ) ] ; return dp [ Math . abs ( x - tx ) ] [ Math . abs ( y - ty ) ] ; } } static public void main ( String [ ] args ) { int i , n , x , y , tx , ty , ans ; n = 100 ; x = 4 ; y = 5 ; tx = 1 ; ty = 1 ; if ( ( x == 1 && y == 1 && tx == 2 && ty == 2 ) || ( x == 2 && y == 2 && tx == 1 && ty == 1 ) ) { ans = 4 ; } else if ( ( x == 1 && y == n && tx == 2 && ty == n - 1 ) || ( x == 2 && y == n - 1 && tx == 1 && ty == n ) ) { ans = 4 ; } else if ( ( x == n && y == 1 && tx == n - 1 && ty == 2 ) || ( x == n - 1 && y == 2 && tx == n && ty == 1 ) ) { ans = 4 ; } else if ( ( x == n && y == n && tx == n - 1 && ty == n - 1 ) || ( x == n - 1 && y == n - 1 && tx == n && ty == n ) ) { ans = 4 ; } else { dp [ 1 ] [ 0 ] = 3 ; dp [ 0 ] [ 1 ] = 3 ; dp [ 1 ] [ 1 ] = 2 ; dp [ 2 ] [ 0 ] = 2 ; dp [ 0 ] [ 2 ] = 2 ; dp [ 2 ] [ 1 ] = 1 ; dp [ 1 ] [ 2 ] = 1 ; ans = getsteps ( x , y , tx , ty ) ; } System . out . println ( ans ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void getMinimumPenalty ( String x , String y , int pxy , int pgap ) { int dp [ ] [ ] = new int [ n + m + 1 ] [ n + m + 1 ] ; for ( int [ ] x1 : dp ) Arrays . fill ( x1 , 0 ) ; for ( i = 0 ; i <= ( n + m ) ; i ++ ) { dp [ i ] [ 0 ] = i * pgap ; dp [ 0 ] [ i ] = i * pgap ; } for ( i = 1 ; i <= m ; i ++ ) { for ( j = 1 ; j <= n ; j ++ ) { if ( x . charAt ( i - 1 ) == y . charAt ( j - 1 ) ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = Math . min ( Math . min ( dp [ i - 1 ] [ j - 1 ] + pxy , dp [ i - 1 ] [ j ] + pgap ) , dp [ i ] [ j - 1 ] + pgap ) ; } } } i = m ; j = n ; int xpos = l ; int ypos = l ; int xans [ ] = new int [ l + 1 ] ; int yans [ ] = new int [ l + 1 ] ; while ( ! ( i == 0 j == 0 ) ) { if ( x . charAt ( i - 1 ) == y . charAt ( j - 1 ) ) { xans [ xpos -- ] = ( int ) x . charAt ( i - 1 ) ; yans [ ypos -- ] = ( int ) y . charAt ( j - 1 ) ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j - 1 ] + pxy == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) x . charAt ( i - 1 ) ; yans [ ypos -- ] = ( int ) y . charAt ( j - 1 ) ; i -- ; j -- ; } else if ( dp [ i - 1 ] [ j ] + pgap == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) x . charAt ( i - 1 ) ; yans [ ypos -- ] = ( int ) ' _ ' ; i -- ; } else if ( dp [ i ] [ j - 1 ] + pgap == dp [ i ] [ j ] ) { xans [ xpos -- ] = ( int ) ' _ ' ; yans [ ypos -- ] = ( int ) y . charAt ( j - 1 ) ; j -- ; } } while ( xpos > 0 ) { if ( i > 0 ) xans [ xpos -- ] = ( int ) x . charAt ( -- i ) ; else xans [ xpos -- ] = ( int ) ' _ ' ; } while ( ypos > 0 ) { if ( j > 0 ) yans [ ypos -- ] = ( int ) y . charAt ( -- j ) ; else yans [ ypos -- ] = ( int ) ' _ ' ; } int id = 1 ; for ( i = l ; i >= 1 ; i -- ) { if ( ( char ) yans [ i ] == ' _ ' && ( char ) xans [ i ] == ' _ ' ) { id = i + 1 ; break ; } } System . out . print ( " Minimum ▁ Penalty ▁ in ▁ " + " aligning ▁ the ▁ genes ▁ = ▁ " ) ; System . out . print ( dp [ m ] [ n ] + "NEW_LINE"); System . out . println ( " The ▁ aligned ▁ genes ▁ are ▁ : " ) ; for ( i = id ; i <= l ; i ++ ) { System . out . print ( ( char ) xans [ i ] ) ; } System . out . print ( "NEW_LINE"); for ( i = id ; i <= l ; i ++ ) { System . out . print ( ( char ) yans [ i ] ) ; } return ; } public static void main ( String [ ] args ) { String gene1 = " AGGGCT " ; String gene2 = " AGGCA " ; int misMatchPenalty = 3 ; int gapPenalty = 2 ; getMinimumPenalty ( gene1 , gene2 , misMatchPenalty , gapPenalty ) ; } }
class GFG { static int sumSubSequence ( int [ ] arr , int len , int m ) { int sum = 0 ; for ( int x : arr ) { sum += x ; } int [ ] [ ] dp = new int [ len + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= len ; i ++ ) dp [ i ] [ 0 ] ++ ; for ( int i = 1 ; i <= len ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] ++ ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] > 0 ) { dp [ i ] [ j ] ++ ; dp [ i ] [ j + arr [ i - 1 ] ] ++ ; } } } int count = 0 ; for ( int j = 1 ; j <= sum ; j ++ ) if ( dp [ len ] [ j ] > 0 ) if ( j % m == 0 ) count += dp [ len ] [ j ] ; return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int m = 3 ; int len = arr . length ; System . out . print ( sumSubSequence ( arr , len , m ) + "NEW_LINE"); } }
import java . io . * ; class GFG { static int lds ( int arr [ ] , int n ) { int lds [ ] = new int [ n ] ; int i , j , max = 0 ; for ( i = 0 ; i < n ; i ++ ) lds [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) lds [ i ] = lds [ j ] + 1 ; for ( i = 0 ; i < n ; i ++ ) if ( max < lds [ i ] ) max = lds [ i ] ; return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 27 , 14 , 38 , 63 , 55 , 46 , 65 , 85 } ; int n = arr . length ; System . out . print ( " Length ▁ of ▁ LDS ▁ is ▁ " + lds ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static void binomialCoeff ( int C [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } static int sumOfproduct ( int n ) { int sum = 0 ; int C [ ] = new int [ MAX ] ; binomialCoeff ( C , n ) ; for ( int i = 0 ; i <= n ; i ++ ) sum += C [ i ] * C [ i + 1 ] ; return sum ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( sumOfproduct ( n ) ) ; } }
class GFG { static int MAX = 100 ; static void binomialCoeff ( int n , int C [ ] ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , n ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } } static int summation ( int n ) { int C [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) C [ i ] = 0 ; binomialCoeff ( n , C ) ; int sum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) sum += ( i * C [ i ] ) ; return sum ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( summation ( n ) ) ; } }
import java . io . * ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int countArray ( int N , int K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } public static void main ( String [ ] args ) { int N = 2 , K = 3 ; System . out . println ( countArray ( N , K ) ) ; } }
import java . io . * ; class GFG { static int N = 100 ; public static int count ( int n , int c , int a [ ] , int b [ ] ) { int need [ ] = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i + n ] = a [ i ] ; b [ i + n ] = b [ i ] ; } int s = 0 ; int tank = 0 ; for ( int i = 0 ; i < 2 * n ; i ++ ) { tank += a [ i ] ; tank = Math . min ( tank , c ) ; tank -= b [ i ] ; if ( tank < 0 ) { tank = 0 ; s = i + 1 ; } } if ( s >= n ) return 0 ; int ans = 1 ; need [ s + n ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int id = s + n - i ; need [ id ] = Math . max ( 0 , need [ id + 1 ] + b [ id ] - Math . min ( a [ id ] , c ) ) ; if ( need [ id ] == 0 ) ans ++ ; } return ans ; } public static void main ( String args [ ] ) { int n = 3 ; int c = 3 ; int [ ] a = new int [ ] { 3 , 1 , 2 , 0 , 0 , 0 } ; int [ ] b = new int [ ] { 2 , 2 , 2 , 0 , 0 , 0 } ; System . out . print ( count ( n , c , a , b ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static void printEqualSumSets ( int [ ] arr , int n ) { int i , currSum , sum = 0 ; for ( i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; if ( ( sum & 1 ) == 1 ) { System . out . print ( " - 1" ) ; return ; } int k = sum >> 1 ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( currSum = 1 ; currSum <= k ; currSum ++ ) { dp [ i ] [ currSum ] = dp [ i - 1 ] [ currSum ] ; if ( arr [ i - 1 ] <= currSum ) dp [ i ] [ currSum ] = dp [ i ] [ currSum ] | dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ; } } List < Integer > set1 = new ArrayList < Integer > ( ) ; List < Integer > set2 = new ArrayList < Integer > ( ) ; if ( ! dp [ n ] [ k ] ) { System . out . print ( "-1NEW_LINE"); return ; } i = n ; currSum = k ; while ( i > 0 && currSum >= 0 ) { if ( dp [ i - 1 ] [ currSum ] ) { i -- ; set2 . add ( arr [ i ] ) ; } else if ( dp [ i - 1 ] [ currSum - arr [ i - 1 ] ] ) { i -- ; currSum -= arr [ i ] ; set1 . add ( arr [ i ] ) ; } } System . out . print ( " Set ▁ 1 ▁ elements : ▁ " ) ; for ( i = 0 ; i < set1 . size ( ) ; i ++ ) System . out . print ( set1 . get ( i ) + " ▁ " ) ; System . out . print ( " Set 2 elements : "); for ( i = 0 ; i < set2 . size ( ) ; i ++ ) System . out . print ( set2 . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 5 , 5 , 1 , 11 } ; int n = arr . length ; printEqualSumSets ( arr , n ) ; } }
import java . util . * ; class GFG { static double largestSumOfAverages ( int [ ] A , int K ) { int n = A . length ; double [ ] pre_sum = new double [ n + 1 ] ; pre_sum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ; double [ ] dp = new double [ n ] ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i ) ; for ( int k = 0 ; k < K - 1 ; k ++ ) for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) dp [ i ] = Math . max ( dp [ i ] , ( pre_sum [ j ] - pre_sum [ i ] ) / ( j - i ) + dp [ j ] ) ; return dp [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] A = { 9 , 1 , 2 , 3 , 9 } ; System . out . println ( largestSumOfAverages ( A , K ) ) ; } }
import java . io . * ; public class GFG { static int N = 105 ; static int M = 1001 ; static int INF = - 1111111111 ; static int maxSum ( int [ ] [ ] arr ) { int [ ] [ ] dp = new int [ N ] [ M ] ; int [ ] current_arr = new int [ M ] ; int [ ] maxsum = new int [ M ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) dp [ i ] [ j ] = - 1 ; } current_arr [ 0 ] = 0 ; dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= 5 ; i ++ ) { int len = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j <= len ; j ++ ) { current_arr [ j ] = arr [ i - 1 ] [ j ] ; current_arr [ j ] += current_arr [ j - 1 ] ; maxsum [ j ] = INF ; } for ( int j = 1 ; j <= len && j <= 6 ; j ++ ) for ( int k = 1 ; k <= len ; k ++ ) if ( j + k - 1 <= len ) maxsum [ j ] = Math . max ( maxsum [ j ] , current_arr [ j + k - 1 ] - current_arr [ k - 1 ] ) ; for ( int j = 0 ; j <= 6 ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; for ( int j = 1 ; j <= 6 ; j ++ ) for ( int cur = 1 ; cur <= j && cur <= len ; cur ++ ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - cur ] + maxsum [ cur ] ) ; } int ans = 0 ; for ( int i = 0 ; i <= 6 ; i ++ ) ans = Math . max ( ans , dp [ 5 ] [ i ] ) ; return ans ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 3 , 2 , 3 , 5 } , { 2 , 7 , - 1 } , { 2 , 8 , 10 } , { 4 , 5 , 2 , 6 , 1 } , { 3 , 2 , 3 , - 2 } } ; System . out . println ( " Maximum ▁ sum ▁ can ▁ be ▁ " + " obtained ▁ is ▁ : ▁ " + maxSum ( arr ) ) ; } }
import java . io . * ; class GFG { static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ind ; int val ; int [ ] [ ] dp = new int [ n ] [ maxLimit + 1 ] ; for ( ind = 0 ; ind < n ; ind ++ ) { for ( val = 0 ; val <= maxLimit ; val ++ ) { if ( ind == 0 ) { if ( num - arr [ ind ] == val num + arr [ ind ] == val ) { dp [ ind ] [ val ] = 1 ; } else { dp [ ind ] [ val ] = 0 ; } } else { if ( val - arr [ ind ] >= 0 && val + arr [ ind ] <= maxLimit ) { if ( dp [ ind - 1 ] [ val - arr [ ind ] ] == 1 dp [ ind - 1 ] [ val + arr [ ind ] ] == 1 ) dp [ ind ] [ val ] = 1 ; } else if ( val - arr [ ind ] >= 0 ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val - arr [ ind ] ] ; } else if ( val + arr [ ind ] <= maxLimit ) { dp [ ind ] [ val ] = dp [ ind - 1 ] [ val + arr [ ind ] ] ; } else { dp [ ind ] [ val ] = 0 ; } } } } for ( val = maxLimit ; val >= 0 ; val -- ) { if ( dp [ n - 1 ] [ val ] == 1 ) { return val ; } } return - 1 ; } public static void main ( String args [ ] ) { int num = 1 ; int [ ] arr = new int [ ] { 3 , 10 , 6 , 4 , 5 } ; int n = arr . length ; int maxLimit = 15 ; System . out . print ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }
class GFG { public static int gen ( int n ) { if ( n == 0 ) return 0 ; else if ( n == 1 ) return 1 ; else if ( n % 2 == 0 ) return 4 * gen ( n / 2 ) ; else if ( n % 2 == 1 ) return 4 * gen ( n / 2 ) + 1 ; return 0 ; } public static void moserDeBruijn ( int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( gen ( i ) + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( " First ▁ " + n + " ▁ terms ▁ of ▁ " + " Moser - de ▁ Bruijn ▁ Sequence ▁ : ▁ " ) ; moserDeBruijn ( n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int A [ ] [ ] = { { 2 } , { 3 , 9 } , { 1 , 6 , 7 } } ; static int minSumPath ( ) { int [ ] memo = new int [ A . length ] ; int n = A . length - 1 ; for ( int i = 0 ; i < A [ n ] . length ; i ++ ) memo [ i ] = A [ n ] [ i ] ; for ( int i = A . length - 2 ; i >= 0 ; i -- ) for ( int j = 0 ; j < A [ i ] . length ; j ++ ) memo [ j ] = A [ i ] [ j ] + ( int ) Math . min ( memo [ j ] , memo [ j + 1 ] ) ; return memo [ 0 ] ; } public static void main ( String args [ ] ) { System . out . print ( minSumPath ( ) ) ; } }
static boolean isPossible ( int index , int sum ) { if ( index == n ) { if ( ( sum % M ) == 0 ) return true ; return false ; } boolean placeAdd = isPossible ( index + 1 , sum + arr [ index ] ) ; boolean placeMinus = isPossible ( index + 1 , sum - arr [ index ] ) ; if ( placeAdd placeMinus ) return true ; return false ; }
class GFG { static int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( a [ i ] & a [ j ] ) == 0 ) count += 2 ; } return count ; } public static void main ( String arg [ ] ) { int a [ ] = { 3 , 4 , 2 } ; int n = a . length ; System . out . print ( countPairs ( a , n ) ) ; } }
import java . util . * ; class GFG { static int N = 15 ; public static int countPairs ( int a [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; int dp [ ] [ ] = new int [ 1 << N ] [ N + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash . containsKey ( a [ i ] ) ) { hash . replace ( a [ i ] , hash . get ( a [ i ] ) + 1 ) ; } else { hash . put ( a [ i ] , 1 ) ; } } for ( int mask = 0 ; mask < ( 1 << N ) ; ++ mask ) { if ( ( mask & 1 ) != 0 ) { if ( hash . containsKey ( mask ) ) { dp [ mask ] [ 0 ] = hash . get ( mask ) ; } if ( hash . containsKey ( mask ^ 1 ) ) { dp [ mask ] [ 0 ] += hash . get ( mask ^ 1 ) ; } } else { if ( hash . containsKey ( mask ) ) { dp [ mask ] [ 0 ] = hash . get ( mask ) ; } } for ( int i = 1 ; i <= N ; ++ i ) { if ( ( mask & ( 1 << i ) ) != 0 ) { dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] + dp [ mask ^ ( 1 << i ) ] [ i - 1 ] ; } else { dp [ mask ] [ i ] = dp [ mask ] [ i - 1 ] ; } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += dp [ ( ( 1 << N ) - 1 ) ^ a [ i ] ] [ N ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 6 } ; int n = a . length ; System . out . print ( countPairs ( a , n ) ) ; } }
import java . util . * ; class GFG { static void postfix ( int a [ ] , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { a [ i - 1 ] = a [ i - 1 ] + a [ i ] ; } } static void modify ( int a [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { a [ i - 1 ] = i * a [ i ] ; } } static void allCombination ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i ; } System . out . println ( " f ( 1 ) ▁ - - > ▁ " + sum ) ; for ( int i = 1 ; i < n ; i ++ ) { postfix ( a , n - i + 1 ) ; sum = 0 ; for ( int j = 1 ; j <= n - i ; j ++ ) { sum += ( j * a [ j ] ) ; } System . out . println ( " f ( " + ( i + 1 ) + " ) ▁ - - > ▁ " + sum ) ; modify ( a , n ) ; } } public static void main ( String [ ] args ) { int n = 5 ; int [ ] a = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = i + 1 ; } allCombination ( a , n ) ; } }
import java . io . * ; import java . math . * ; class GFG { static long dp [ ] [ ] = new long [ 1000 ] [ 1000 ] ; static long sum ( int a [ ] , int i , int j ) { long ans = 0 ; for ( int m = i ; m <= j ; m ++ ) ans = ( ans + a [ m ] ) % 100 ; return ans ; } static long solve ( int a [ ] , int i , int j ) { if ( i == j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; dp [ i ] [ j ] = 100000000 ; for ( int k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , ( solve ( a , i , k ) + solve ( a , k + 1 , j ) + ( sum ( a , i , k ) * sum ( a , k + 1 , j ) ) ) ) ; } return dp [ i ] [ j ] ; } static void initialize ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 40 , 60 , 20 } ; int n = a . length ; initialize ( n ) ; System . out . println ( solve ( a , 0 , n - 1 ) ) ; } }
import java . io . * ; class GFG { static void reverseFibonacci ( int n ) { int a [ ] = new int [ n ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { a [ i ] = a [ i - 2 ] + a [ i - 1 ] ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; reverseFibonacci ( n ) ; } }
import java . io . * ; class GFG { static boolean check ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { for ( int j = 0 ; j <= s2 . length ( ) ; j ++ ) { if ( dp [ i ] [ j ] ) { if ( j < s2 . length ( ) && ( Character . toUpperCase ( s1 . charAt ( i ) ) == s2 . charAt ( j ) ) ) dp [ i + 1 ] [ j + 1 ] = true ; if ( ! Character . isUpperCase ( s1 . charAt ( i ) ) ) dp [ i + 1 ] [ j ] = true ; } } } return ( dp [ n ] [ m ] ) ; } public static void main ( String args [ ] ) { String s1 = " daBcd " ; String s2 = " ABC " ; if ( check ( s1 , s2 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class GFG { static float find_prob ( int N , float P ) { double dp [ ] = new double [ N + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 0 ; dp [ 2 ] = P ; dp [ 3 ] = 1 - P ; for ( int i = 4 ; i <= N ; ++ i ) dp [ i ] = ( P ) * dp [ i - 2 ] + ( 1 - P ) * dp [ i - 3 ] ; return ( ( float ) ( dp [ N ] ) ) ; } public static void main ( String args [ ] ) { int n = 5 ; float p = 0.2f ; System . out . printf ( " % .2f " , find_prob ( n , p ) ) ; } }
import java . io . * ; class GFG { static int maxSubArraySumRepeated ( int a [ ] , int n , int k ) { int max_so_far = 0 ; int INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < n * k ; i ++ ) { max_ending_here = max_ending_here + a [ i % n ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 20 , - 30 , - 1 } ; int n = a . length ; int k = 3 ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySumRepeated ( a , n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getLevenstein ( StringBuilder input ) { StringBuilder revInput = new StringBuilder ( input ) ; revInput = revInput . reverse ( ) ; int n = input . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { if ( input . charAt ( i - 1 ) == revInput . charAt ( j - 1 ) ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } int res = Integer . MAX_VALUE ; for ( int i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . min ( res , dp [ i ] [ j ] ) ; if ( i < n ) res = Math . min ( res , dp [ i + 1 ] [ j ] ) ; if ( i > 0 ) res = Math . min ( res , dp [ i - 1 ] [ j ] ) ; } return res ; } public static void main ( String [ ] args ) { StringBuilder input = new StringBuilder ( " myfirstgeekarticle " ) ; System . out . println ( getLevenstein ( input ) ) ; } }
class GFG { static int [ ] dp = new int [ MAXN ] ; static int [ ] [ ] nck = new int [ MAXN ] [ MAXN ] ; static int [ ] log2 = new int [ MAXN ] ; public static int choose ( int n , int k ) { if ( k > n ) { return 0 ; } if ( n <= 1 ) { return 1 ; } if ( k == 0 ) { return 1 ; } if ( nck [ n ] [ k ] != - 1 ) { return nck [ n ] [ k ] ; } int answer = choose ( n - 1 , k - 1 ) + choose ( n - 1 , k ) ; nck [ n ] [ k ] = answer ; return answer ; } public static int getLeft ( int n ) { if ( n == 1 ) { return 0 ; } int h = log2 [ n ] ; int last = n - ( ( 1 << h ) - 1 ) ; if ( last >= ( numh / 2 ) ) { } else { return ( 1 << h ) - 1 - ( ( numh / 2 ) - last ) ; } } public static int numberOfHeaps ( int n ) { if ( n <= 1 ) { return 1 ; } if ( dp [ n ] != - 1 ) { return dp [ n ] ; } int left = getLeft ( n ) ; int ans = ( choose ( n - 1 , left ) * numberOfHeaps ( left ) ) * ( numberOfHeaps ( n - 1 - left ) ) ; dp [ n ] = ans ; return ans ; } public static int solve ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] = - 1 ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { nck [ i ] [ j ] = - 1 ; } } int currLog2 = - 1 ; int currPower2 = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( currPower2 == i ) { currLog2 ++ ; currPower2 *= 2 ; } log2 [ i ] = currLog2 ; } return numberOfHeaps ( n ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( solve ( n ) ) ; } }
import java . util . * ; class GFG { static int N = 5 ; static void printHosoya ( int n ) { int dp [ ] [ ] = new int [ N ] [ N ] ; dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ; dp [ 1 ] [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] + dp [ i - 2 ] [ j - 2 ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printHosoya ( n ) ; } }
import java . io . * ; class GFG { static int numberofways ( int n , int m ) { int dp [ ] [ ] = new int [ n + 2 ] [ n + 2 ] ; dp [ 0 ] [ n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - k ] [ k ] ) ; } } return dp [ n ] [ m ] ; } public static void main ( String args [ ] ) { int n = 3 , m = 1 ; System . out . println ( numberofways ( n , m ) ) ; } }
import java . util . * ; class GFG { static int zigzag ( int n , int k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k == 0 ) return 0 ; return zigzag ( n , k - 1 ) + zigzag ( n - 1 , n - k ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; System . out . println ( zigzag ( n , k ) ) ; } }
import java . util . * ; class Eulerian { public static int eulerian ( int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( i > j ) { if ( j == 0 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = ( ( i - j ) * dp [ i - 1 ] [ j - 1 ] ) + ( ( j + 1 ) * dp [ i - 1 ] [ j ] ) ; } } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; System . out . print ( eulerian ( n , m ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int longOddEvenIncSeq ( int arr [ ] , int n ) { int [ ] lioes = new int [ n ] ; int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) lioes [ i ] = lioes [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( maxLen < lioes [ i ] ) maxLen = lioes [ i ] ; return maxLen ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 } ; int n = 10 ; System . out . println ( " Longest ▁ Increasing ▁ Odd " + " ▁ Even ▁ Subsequence : ▁ " + longOddEvenIncSeq ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int dealnnoy ( int n , int m ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] + dp [ i ] [ j - 1 ] ; return dp [ m ] [ n ] ; } public static void main ( String args [ ] ) { int n = 3 , m = 4 ; System . out . println ( dealnnoy ( n , m ) ) ; } }
import java . util . * ; class Longest { public static void longestAlternating ( int arr [ ] , int n ) { int [ ] count = new int [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( count [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 5 , - 1 , - 1 , 2 , - 2 , - 3 } ; int n = 6 ; longestAlternating ( a , n ) ; } }
import java . io . * ; class GFG { static int maxDP ( int n ) { int res [ ] = new int [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = Math . max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } public static void main ( String [ ] args ) { int n = 60 ; System . out . println ( " MaxSum ▁ = ▁ " + maxDP ( n ) ) ; } }
import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int N , int k ) { int MS [ ] = new int [ N ] ; MS [ N - 1 ] = arr [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { if ( i + k + 1 >= N ) MS [ i ] = Math . max ( arr [ i ] , MS [ i + 1 ] ) ; else MS [ i ] = Math . max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ; } return MS [ 0 ] ; } public static void main ( String [ ] args ) { int N = 10 , k = 2 ; int arr [ ] = { 50 , 70 , 40 , 50 , 90 , 70 , 60 , 40 , 70 , 50 } ; System . out . println ( maxSum ( arr , N , k ) ) ; } }
class GFG { static void alternateSubarray ( boolean arr [ ] , int n ) { int len [ ] = new int [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == true ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) System . out . print ( len [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { boolean arr [ ] = { true , false , true , false , false , true } ; int n = arr . length ; alternateSubarray ( arr , n ) ; } }
class GFG { static void alternateSubarray ( boolean arr [ ] , int n ) { int count = 1 ; boolean prev = arr [ 0 ] ; for ( int i = 1 ; i < n ; ++ i ) { if ( ( arr [ i ] ^ prev ) == false ) { while ( count > 0 ) { System . out . print ( count -- + " ▁ " ) ; } } ++ count ; prev = arr [ i ] ; } while ( count != 0 ) { System . out . print ( count -- + " ▁ " ) ; } } public static void main ( String args [ ] ) { boolean arr [ ] = { true , false , true , false , false , true } ; int n = arr . length ; alternateSubarray ( arr , n ) ; } }
import java . io . * ; class GFG { static int countways ( int n ) { int [ ] A = new int [ n + 1 ] ; A [ 0 ] = 1 ; A [ 1 ] = 3 ; A [ 2 ] = 7 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = 2 * A [ i - 1 ] + A [ i - 2 ] ; } return A [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countways ( 5 ) ) ; } }
class GFG { static int uniquePathsWithObstacles ( int [ ] [ ] A ) { int r = A . length ; int c = A [ 0 ] . length ; if ( A [ 0 ] [ 0 ] != 0 ) return 0 ; A [ 0 ] [ 0 ] = 1 ; for ( int j = 1 ; j < c ; j ++ ) { if ( A [ 0 ] [ j ] == 0 ) { A [ 0 ] [ j ] = A [ 0 ] [ j - 1 ] ; } else { A [ 0 ] [ j ] = 0 ; } } for ( int i = 1 ; i < r ; i ++ ) { if ( A [ i ] [ 0 ] == 0 ) { A [ i ] [ 0 ] = A [ i - 1 ] [ 0 ] ; } else { A [ i ] [ 0 ] = 0 ; } } for ( int i = 1 ; i < r ; i ++ ) { for ( int j = 1 ; j < c ; j ++ ) { if ( A [ i ] [ j ] == 0 ) { A [ i ] [ j ] = A [ i - 1 ] [ j ] + A [ i ] [ j - 1 ] ; } else { A [ i ] [ j ] = 0 ; } } } return A [ r - 1 ] ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 0 , 0 , 0 } , { 0 , 1 , 0 } , { 0 , 0 , 0 } } ; System . out . print ( uniquePathsWithObstacles ( A ) ) ; } }
import java . util . Arrays ; public class GFG { static boolean findWinner ( int x , int y , int n ) { boolean [ ] dp = new boolean [ n + 1 ] ; Arrays . fill ( dp , false ) ; dp [ 0 ] = false ; dp [ 1 ] = true ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i - 1 >= 0 && dp [ i - 1 ] == false ) dp [ i ] = true ; else if ( i - x >= 0 && dp [ i - x ] == false ) dp [ i ] = true ; else if ( i - y >= 0 && dp [ i - y ] == false ) dp [ i ] = true ; else dp [ i ] = false ; } return dp [ n ] ; } public static void main ( String args [ ] ) { int x = 3 , y = 4 , n = 5 ; if ( findWinner ( x , y , n ) == true ) System . out . println ( ' A ' ) ; else System . out . println ( ' B ' ) ; } }
class Max_game_winner { static int maxGameByWinner ( int N ) { int [ ] dp = new int [ N ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; int i = 2 ; do { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; } while ( dp [ i ++ ] <= N ) ; return ( i - 2 ) ; } public static void main ( String args [ ] ) { int N = 10 ; System . out . println ( maxGameByWinner ( N ) ) ; } }
public class Main { static int minRemove ( int arr [ ] , int n ) { int LIS [ ] = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [ i ] = Math . max ( LIS [ i ] , LIS [ j ] + 1 ) ; } len = Math . max ( len , LIS [ i ] ) ; } return n - len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 6 , 5 , 4 } ; int n = arr . length ; System . out . println ( minRemove ( arr , n ) ) ; } }
class GFG { static int countTransformation ( String a , String b ) { int n = a . length ( ) , m = b . length ( ) ; if ( m == 0 ) { return 1 ; } int dp [ ] [ ] = new int [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( i == 0 ) { if ( j == 0 ) { dp [ i ] [ j ] = ( a . charAt ( j ) == b . charAt ( i ) ) ? 1 : 0 ; } else if ( a . charAt ( j ) == b . charAt ( i ) ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + 1 ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } } else if ( a . charAt ( j ) == b . charAt ( i ) ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j - 1 ] ; } else { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; } } } return dp [ m - 1 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { String a = " abcccdf " , b = " abccdf " ; System . out . println ( countTransformation ( a , b ) ) ; } }
import java . awt . Point ; import java . util . HashMap ; class Test { static int maxWeightRec ( int wt [ ] , int val [ ] , int K , HashMap < Point , Integer > hm , int last , int diff ) { if ( last == - 1 ) { if ( diff == 0 ) return 0 ; else return Integer . MIN_VALUE ; } Point tmp = new Point ( last , diff ) ; if ( hm . containsKey ( tmp ) ) return hm . get ( tmp ) ; hm . put ( tmp , Math . max ( maxWeightRec ( wt , val , K , hm , last - 1 , diff ) , wt [ last ] + maxWeightRec ( wt , val , K , hm , last - 1 , diff + wt [ last ] - val [ last ] * K ) ) ) ; return hm . get ( tmp ) ; } static int maxWeight ( int wt [ ] , int val [ ] , int K , int N ) { HashMap < Point , Integer > hm = new HashMap < > ( ) ; return maxWeightRec ( wt , val , K , hm , N - 1 , 0 ) ; } public static void main ( String args [ ] ) { int wt [ ] = { 4 , 8 , 9 } ; int val [ ] = { 2 , 4 , 6 } ; int K = 2 ; System . out . println ( maxWeight ( wt , val , K , wt . length ) ) ; } }
import java . util . * ; class GFG { static int minPyramidCost ( int arr [ ] , int N ) { int left [ ] = new int [ N ] ; int right [ ] = new int [ N ] ; left [ 0 ] = Math . min ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < N ; ++ i ) left [ i ] = Math . min ( arr [ i ] , Math . min ( left [ i - 1 ] + 1 , i + 1 ) ) ; right [ N - 1 ] = Math . min ( arr [ N - 1 ] , 1 ) ; for ( int i = N - 2 ; i >= 0 ; -- i ) right [ i ] = Math . min ( arr [ i ] , Math . min ( right [ i + 1 ] + 1 , N - i ) ) ; int tot [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; ++ i ) tot [ i ] = Math . min ( right [ i ] , left [ i ] ) ; int max_ind = 0 ; for ( int i = 0 ; i < N ; ++ i ) if ( tot [ i ] > tot [ max_ind ] ) max_ind = i ; int cost = 0 ; int height = tot [ max_ind ] ; for ( int x = max_ind ; x >= 0 ; -- x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } height = tot [ max_ind ] - 1 ; for ( int x = max_ind + 1 ; x < N ; ++ x ) { cost += arr [ x ] - height ; if ( height > 0 ) -- height ; } return cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 2 , 1 } ; int N = arr . length ; System . out . print ( minPyramidCost ( arr , N ) ) ; } }
import java . util . * ; class GFG { public static int maxSum ( int grid [ ] [ ] , int n ) { int incl = Math . max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = Math . max ( excl , incl ) ; incl = excl + Math . max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ; excl = excl_new ; } return Math . max ( excl , incl ) ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 10 } } ; int n = 5 ; System . out . println ( maxSum ( grid , n ) ) ; } }
class Main { static int minInsertionStepToSortArray ( int arr [ ] , int N ) { int [ ] lis = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] >= arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( max < lis [ i ] ) max = lis [ i ] ; return ( N - max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 1 , 4 , 7 , 6 } ; int N = arr . length ; System . out . println ( minInsertionStepToSortArray ( arr , N ) ) ; } }
public class GFG { static final int NO_OF_CHARS = 256 ; static void printDistinct ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) != ' ▁ ' ) count [ ( int ) str . charAt ( i ) ] ++ ; int n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ ( int ) str . charAt ( i ) ] == 1 ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks " ; printDistinct ( str ) ; } }
import java . util . * ; class GFG { static final int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T , int m , int n ) { if ( m == 0 ) return MAX ; if ( n <= 0 ) return 1 ; int k ; for ( k = 0 ; k < n ; k ++ ) if ( T [ k ] == S [ 0 ] ) break ; if ( k == n ) return 1 ; return Math . min ( shortestSeq ( Arrays . copyOfRange ( S , 1 , S . length ) , T , m - 1 , n ) , 1 + shortestSeq ( Arrays . copyOfRange ( S , 1 , S . length ) , Arrays . copyOfRange ( T , k + 1 , T . length ) , m - 1 , n - k - 1 ) ) ; } public static void main ( String [ ] args ) { char S [ ] = " babab " . toCharArray ( ) ; char T [ ] = " babba " . toCharArray ( ) ; int m = S . length , n = T . length ; int ans = shortestSeq ( S , T , m , n ) ; if ( ans >= MAX ) ans = - 1 ; System . out . print ( " Length ▁ of ▁ shortest ▁ subsequence ▁ is : ▁ " + ans + "NEW_LINE"); } }
class GFG { static final int MAX = 1005 ; static int shortestSeq ( char [ ] S , char [ ] T ) { int m = S . length , n = T . length ; int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { dp [ i ] [ 0 ] = 1 ; } for ( int i = 0 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = MAX ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { char ch = S [ i - 1 ] ; int k ; for ( k = j - 1 ; k >= 0 ; k -- ) { if ( T [ k ] == ch ) { break ; } } if ( k == - 1 ) { dp [ i ] [ j ] = 1 ; } else { dp [ i ] [ j ] = Math . min ( dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ k ] + 1 ) ; } } } int ans = dp [ m ] [ n ] ; if ( ans >= MAX ) { ans = - 1 ; } return ans ; } public static void main ( String [ ] args ) { char S [ ] = " babab " . toCharArray ( ) ; char T [ ] = " babba " . toCharArray ( ) ; int m = S . length , n = T . length ; System . out . println ( " Length ▁ of ▁ shortest " + " subsequence ▁ is ▁ : ▁ " + shortestSeq ( S , T ) ) ; } }
public class GFG { static int minStepToDeleteString ( String str ) { int N = str . length ( ) ; int [ ] [ ] dp = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= N ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int len = 1 ; len <= N ; len ++ ) { for ( int i = 0 , j = len - 1 ; j < N ; i ++ , j ++ ) { if ( len == 1 ) dp [ i ] [ j ] = 1 ; else { dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ] ; if ( str . charAt ( i ) == str . charAt ( i + 1 ) ) dp [ i ] [ j ] = Math . min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] ) ; for ( int K = i + 2 ; K <= j ; K ++ ) if ( str . charAt ( i ) == str . charAt ( K ) ) dp [ i ] [ j ] = Math . min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } public static void main ( String args [ ] ) { String str = "2553432" ; System . out . println ( minStepToDeleteString ( str ) ) ; } }
import java . io . * ; class GFG { static int inf = 1000000000 ; static int ans = inf ; static void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) { if ( par > k ) return ; if ( par == k && i == n - 1 ) { ans = Math . min ( ans , current_ans ) ; return ; } for ( int j = i + 1 ; j < n ; j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j ] - a [ i + 1 ] ) ) ; } public static void main ( String [ ] args ) { int k = 2 ; int a [ ] = { 1 , 5 , 8 , 10 } ; int n = a . length ; solve ( - 1 , 0 , a , n , k , 0 ) ; System . out . println ( ans ) ; } }
import java . io . * ; class GFG { static int getMinSteps ( int n , int memo [ ] ) { if ( n == 1 ) return 0 ; if ( memo [ n ] != - 1 ) return memo [ n ] ; int res = getMinSteps ( n - 1 , memo ) ; if ( n % 2 == 0 ) res = Math . min ( res , getMinSteps ( n / 2 , memo ) ) ; if ( n % 3 == 0 ) res = Math . min ( res , getMinSteps ( n / 3 , memo ) ) ; memo [ n ] = 1 + res ; return memo [ n ] ; } static int getMinSteps ( int n ) { int memo [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) memo [ i ] = - 1 ; return getMinSteps ( n , memo ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( getMinSteps ( n ) ) ; } }
import java . util . * ; class GFG { public static class Temple { public int L ; public int R ; } ; static int offeringNumber ( int n , int [ ] templeHeight ) { Temple [ ] chainSize = new Temple [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { chainSize [ i ] = new Temple ( ) ; chainSize [ i ] . L = - 1 ; chainSize [ i ] . R = - 1 ; } chainSize [ 0 ] . L = 1 ; chainSize [ n - 1 ] . R = 1 ; for ( int i = 1 ; i < n ; ++ i ) { if ( templeHeight [ i - 1 ] < templeHeight [ i ] ) chainSize [ i ] . L = chainSize [ i - 1 ] . L + 1 ; else chainSize [ i ] . L = 1 ; } for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( templeHeight [ i + 1 ] < templeHeight [ i ] ) chainSize [ i ] . R = chainSize [ i + 1 ] . R + 1 ; else chainSize [ i ] . R = 1 ; } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += Math . max ( chainSize [ i ] . L , chainSize [ i ] . R ) ; return sum ; } public static void main ( String [ ] s ) { int [ ] arr1 = { 1 , 2 , 2 } ; System . out . println ( offeringNumber ( 3 , arr1 ) ) ; int [ ] arr2 = { 1 , 4 , 3 , 6 , 2 , 1 } ; System . out . println ( offeringNumber ( 6 , arr2 ) ) ; } }
public class GFG { static int stringReduction ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } if ( count [ 0 ] == n count [ 1 ] == n count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) { return 2 ; } return 1 ; } public static void main ( String [ ] args ) { String str = " abcbbaacb " ; System . out . println ( stringReduction ( str ) ) ; } }
class GFG { static int MAX = 100 ; static int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; static int largestZigZagSumRec ( int mat [ ] [ ] , int i , int j , int n ) { if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( i == n - 1 ) return ( dp [ i ] [ j ] = mat [ i ] [ j ] ) ; int zzs = 0 ; for ( int k = 0 ; k < n ; k ++ ) if ( k != j ) zzs = Math . max ( zzs , largestZigZagSumRec ( mat , i + 1 , k , n ) ) ; return ( dp [ i ] [ j ] = ( zzs + mat [ i ] [ j ] ) ) ; } static int largestZigZag ( int mat [ ] [ ] , int n ) { for ( int i = 0 ; i < MAX ; i ++ ) for ( int k = 0 ; k < MAX ; k ++ ) dp [ i ] [ k ] = - 1 ; int res = 0 ; for ( int j = 0 ; j < n ; j ++ ) res = Math . max ( res , largestZigZagSumRec ( mat , 0 , j , n ) ) ; return res ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 4 , 2 , 1 } , { 3 , 9 , 6 } , { 11 , 3 , 15 } } ; System . out . print ( " Largest ▁ zigzag ▁ sum : ▁ " + largestZigZag ( mat , n ) ) ; } }
public class No_of_subsequence { static int countSubsequences ( String s ) { int aCount = 0 ; int bCount = 0 ; int cCount = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' a ' ) aCount = ( 1 + 2 * aCount ) ; else if ( s . charAt ( i ) == ' b ' ) bCount = ( aCount + 2 * bCount ) ; else if ( s . charAt ( i ) == ' c ' ) cCount = ( bCount + 2 * cCount ) ; } return cCount ; } public static void main ( String args [ ] ) { String s = " abbc " ; System . out . println ( countSubsequences ( s ) ) ; } }
class GFG { static int maxRevenue ( int m , int [ ] x , int [ ] revenue , int n , int t ) { int [ ] maxRev = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) maxRev [ i ] = 0 ; int nxtbb = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ; else { if ( i <= t ) maxRev [ i ] = Math . max ( maxRev [ i - 1 ] , revenue [ nxtbb ] ) ; else maxRev [ i ] = Math . max ( maxRev [ i - t - 1 ] + revenue [ nxtbb ] , maxRev [ i - 1 ] ) ; nxtbb ++ ; } } else maxRev [ i ] = maxRev [ i - 1 ] ; } return maxRev [ m ] ; } public static void main ( String [ ] args ) { int m = 20 ; int [ ] x = new int [ ] { 6 , 7 , 12 , 13 , 14 } ; int [ ] revenue = new int [ ] { 5 , 6 , 5 , 3 , 1 } ; int n = x . length ; int t = 5 ; System . out . println ( maxRevenue ( m , x , revenue , n , t ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > findLIS ( Vector < Integer > arr , int n ) { Vector < Integer > [ ] L = new Vector [ n ] ; for ( int i = 0 ; i < L . length ; i ++ ) L [ i ] = new Vector < Integer > ( ) ; L [ 0 ] . add ( arr . elementAt ( 0 ) ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr . elementAt ( i ) > arr . elementAt ( j ) && ( L [ i ] . size ( ) < L [ j ] . size ( ) ) ) L [ i ] = L [ j ] ; } L [ i ] . add ( arr . elementAt ( i ) ) ; } int maxSize = 1 ; Vector < Integer > lis = new Vector < > ( ) ; for ( Vector < Integer > x : L ) { if ( x . size ( ) > maxSize ) { lis = x ; maxSize = x . size ( ) ; } } return lis ; } static void minimize ( int input [ ] , int n ) { Vector < Integer > arr = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arr . add ( input [ i ] ) ; while ( arr . size ( ) != 0 ) { Vector < Integer > lis = findLIS ( arr , arr . size ( ) ) ; if ( lis . size ( ) < 2 ) break ; for ( int i = 0 ; i < arr . size ( ) && lis . size ( ) > 0 ; i ++ ) { if ( arr . elementAt ( i ) == lis . elementAt ( 0 ) ) { arr . removeAll ( lis ) ; i -- ; lis . remove ( 0 ) ; } } } int i ; for ( i = 1 ; i < arr . size ( ) ; i ++ ) System . out . print ( arr . elementAt ( i ) + " ▁ " ) ; if ( i == 0 ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int input [ ] = { 3 , 2 , 6 , 4 , 5 , 1 } ; int n = input . length ; minimize ( input , n ) ; } }
class GFG { public static double fact [ ] ; public static double probability ( int k , int n ) { double ans = 0 ; for ( int i = k ; i <= n ; ++ i ) ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) ; ans = ans / ( 1 << n ) ; return ans ; } public static void precompute ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 2 ; i < 20 ; ++ i ) fact [ i ] = fact [ i - 1 ] * i ; } public static void main ( String [ ] args ) { fact = new double [ 100 ] ; precompute ( ) ; System . out . println ( probability ( 2 , 3 ) ) ; System . out . println ( probability ( 3 , 6 ) ) ; System . out . println ( probability ( 12 , 18 ) ) ; } }
class GFG { static int countStrings ( int n , int k ) { int dp [ ] [ ] [ ] = new int [ n + 1 ] [ k + 1 ] [ 2 ] ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i && j < k + 1 ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ 1 ] ; dp [ i ] [ j ] [ 1 ] = dp [ i - 1 ] [ j ] [ 0 ] ; if ( j - 1 >= 0 ) { dp [ i ] [ j ] [ 1 ] += dp [ i - 1 ] [ j - 1 ] [ 1 ] ; } } } return dp [ n ] [ k ] [ 0 ] + dp [ n ] [ k ] [ 1 ] ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( countStrings ( n , k ) ) ; } }
class GFG { static int [ ] dp = new int [ 1000 ] ; static int countFriendsPairings ( int n ) { if ( dp [ n ] != - 1 ) return dp [ n ] ; if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ; else return dp [ n ] = n ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 1000 ; i ++ ) dp [ i ] = - 1 ; int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > fact ; static void preComputeFact ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) fact . add ( fact . elementAt ( i - 1 ) * i ) ; } static int countFriendsPairings ( int n ) { int ones = n , twos = 1 , ans = 0 ; while ( ones >= 0 ) { ans += fact . elementAt ( n ) / ( twos * fact . elementAt ( ones ) * fact . elementAt ( ( n - ones ) / 2 ) ) ; ones -= 2 ; twos *= 2 ; } return ans ; } public static void main ( String [ ] args ) { fact = new Vector < > ( ) ; fact . add ( 1 ) ; preComputeFact ( 100 ) ; int n = 4 ; System . out . print ( countFriendsPairings ( n ) + "NEW_LINE"); } }
import java . io . * ; class GFG { static int l = 3 ; static int m = 3 ; static int n = 3 ; static int min ( int x , int y , int z ) { return ( x < y ) ? ( ( x < z ) ? x : z ) : ( ( y < z ) ? y : z ) ; } static int minPathSum ( int arr [ ] [ ] [ ] ) { int i , j , k ; int tSum [ ] [ ] [ ] = new int [ l ] [ m ] [ n ] ; tSum [ 0 ] [ 0 ] [ 0 ] = arr [ 0 ] [ 0 ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) tSum [ i ] [ 0 ] [ 0 ] = tSum [ i - 1 ] [ 0 ] [ 0 ] + arr [ i ] [ 0 ] [ 0 ] ; for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ 0 ] = tSum [ 0 ] [ j - 1 ] [ 0 ] + arr [ 0 ] [ j ] [ 0 ] ; for ( k = 1 ; k < n ; k ++ ) tSum [ 0 ] [ 0 ] [ k ] = tSum [ 0 ] [ 0 ] [ k - 1 ] + arr [ 0 ] [ 0 ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ i ] [ j ] [ 0 ] = min ( tSum [ i - 1 ] [ j ] [ 0 ] , tSum [ i ] [ j - 1 ] [ 0 ] , Integer . MAX_VALUE ) + arr [ i ] [ j ] [ 0 ] ; for ( i = 1 ; i < l ; i ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ 0 ] [ k ] = min ( tSum [ i - 1 ] [ 0 ] [ k ] , tSum [ i ] [ 0 ] [ k - 1 ] , Integer . MAX_VALUE ) + arr [ i ] [ 0 ] [ k ] ; for ( k = 1 ; k < n ; k ++ ) for ( j = 1 ; j < m ; j ++ ) tSum [ 0 ] [ j ] [ k ] = min ( tSum [ 0 ] [ j ] [ k - 1 ] , tSum [ 0 ] [ j - 1 ] [ k ] , Integer . MAX_VALUE ) + arr [ 0 ] [ j ] [ k ] ; for ( i = 1 ; i < l ; i ++ ) for ( j = 1 ; j < m ; j ++ ) for ( k = 1 ; k < n ; k ++ ) tSum [ i ] [ j ] [ k ] = min ( tSum [ i - 1 ] [ j ] [ k ] , tSum [ i ] [ j - 1 ] [ k ] , tSum [ i ] [ j ] [ k - 1 ] ) + arr [ i ] [ j ] [ k ] ; return tSum [ l - 1 ] [ m - 1 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] [ ] = { { { 1 , 2 , 4 } , { 3 , 4 , 5 } , { 5 , 2 , 1 } } , { { 4 , 8 , 3 } , { 5 , 2 , 1 } , { 3 , 4 , 2 } } , { { 2 , 4 , 1 } , { 3 , 1 , 4 } , { 6 , 3 , 8 } } } ; System . out . println ( minPathSum ( arr ) ) ; } }
class GFG { static char name ; static void printParenthesis ( int i , int j , int n , int [ ] [ ] bracket ) { if ( i == j ) { System . out . print ( name ++ ) ; return ; } System . out . print ( " ( " ) ; printParenthesis ( i , bracket [ i ] [ j ] , n , bracket ) ; printParenthesis ( bracket [ i ] [ j ] + 1 , j , n , bracket ) ; System . out . print ( " ) " ) ; } static void matrixChainOrder ( int p [ ] , int n ) { int [ ] [ ] m = new int [ n ] [ n ] ; int [ ] [ ] bracket = new int [ n ] [ n ] ; for ( int i = 1 ; i < n ; i ++ ) m [ i ] [ i ] = 0 ; for ( int L = 2 ; L < n ; L ++ ) { for ( int i = 1 ; i < n - L + 1 ; i ++ ) { int j = i + L - 1 ; m [ i ] [ j ] = Integer . MAX_VALUE ; for ( int k = i ; k <= j - 1 ; k ++ ) { int q = m [ i ] [ k ] + m [ k + 1 ] [ j ] + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( q < m [ i ] [ j ] ) { m [ i ] [ j ] = q ; bracket [ i ] [ j ] = k ; } } } } name = ' A ' ; System . out . print ( " Optimal ▁ Parenthesization ▁ is ▁ : ▁ " ) ; printParenthesis ( 1 , n - 1 , n , bracket ) ; System . out . print ( " Optimal Cost is : " + m[1][n - 1]); } public static void main ( String [ ] args ) { int arr [ ] = { 40 , 20 , 30 , 10 , 30 } ; int n = arr . length ; matrixChainOrder ( arr , n ) ; } }
import java . io . * ; class GFG { static int N = 3 ; static int maxPathSum ( int tri [ ] [ ] , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ; } } return tri [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { int tri [ ] [ ] = { { 1 , 0 , 0 } , { 4 , 8 , 0 } , { 1 , 5 , 3 } } ; System . out . println ( maxPathSum ( tri , 2 , 2 ) ) ; } }
import java . util . * ; class GFG { static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) { int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= m ; j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] ) ; return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 1 , 7 , 8 } ; int B [ ] = { 3 , 6 , 7 } ; int m = A . length ; int n = B . length ; System . out . print ( MaxDotProduct ( A , B , m , n ) ) ; } }
class GFG { static String X = " AGGT12" ; static String Y = "12TXAYB " ; static String Z = "12XBA " ; static int [ ] [ ] [ ] dp = new int [ 100 ] [ 100 ] [ 100 ] ; static int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 j == - 1 k == - 1 ) { return 0 ; } if ( dp [ i ] [ j ] [ k ] != - 1 ) { return dp [ i ] [ j ] [ k ] ; } if ( X . charAt ( i ) == Y . charAt ( j ) && Y . charAt ( j ) == Z . charAt ( k ) ) { return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; } else { return dp [ i ] [ j ] [ k ] = Math . max ( Math . max ( lcsOf3 ( i - 1 , j , k ) , lcsOf3 ( i , j - 1 , k ) ) , lcsOf3 ( i , j , k - 1 ) ) ; } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100 ; i ++ ) { for ( int j = 0 ; j < 100 ; j ++ ) { for ( int k = 0 ; k < 100 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; System . out . print ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( m - 1 , n - 1 , o - 1 ) ) ; } }
import java . util . * ; class GFG { static int MAX = 102 ; static int countMin ( int arr [ ] , int dp [ ] [ ] [ ] , int n , int dec , int inc , int i ) { if ( dp [ dec ] [ inc ] [ i ] != - 1 ) return dp [ dec ] [ inc ] [ i ] ; if ( i == n ) return 0 ; if ( arr [ i ] < arr [ dec ] ) dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , i , inc , i + 1 ) ; if ( arr [ i ] > arr [ inc ] ) { if ( dp [ dec ] [ inc ] [ i ] == - 1 ) dp [ dec ] [ inc ] [ i ] = countMin ( arr , dp , n , dec , i , i + 1 ) ; else dp [ dec ] [ inc ] [ i ] = Math . min ( countMin ( arr , dp , n , dec , i , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) ; } if ( dp [ dec ] [ inc ] [ i ] == - 1 ) dp [ dec ] [ inc ] [ i ] = 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) ; else dp [ dec ] [ inc ] [ i ] = Math . min ( 1 + countMin ( arr , dp , n , dec , inc , i + 1 ) , dp [ dec ] [ inc ] [ i ] ) ; return dp [ dec ] [ inc ] [ i ] ; } static int wrapper ( int arr [ ] , int n ) { arr [ MAX - 2 ] = Integer . MAX_VALUE ; arr [ MAX - 1 ] = Integer . MIN_VALUE ; int [ ] [ ] [ ] dp = new int [ MAX ] [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return countMin ( arr , dp , n , MAX - 2 , MAX - 1 , 0 ) ; } public static void main ( String [ ] args ) { int n = 12 ; int [ ] arr = new int [ MAX ] ; arr [ 0 ] = 7 ; arr [ 1 ] = 8 ; arr [ 2 ] = 1 ; arr [ 3 ] = 2 ; arr [ 4 ] = 4 ; arr [ 5 ] = 6 ; arr [ 6 ] = 3 ; arr [ 7 ] = 5 ; arr [ 8 ] = 2 ; arr [ 9 ] = 1 ; arr [ 10 ] = 8 ; arr [ 11 ] = 7 ; System . out . println ( wrapper ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void distSumRec ( int arr [ ] , int n , int sum , int currindex , HashSet < Integer > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; for ( int i : s ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printDistSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i - 1 ] ] = true ; for ( int j = 1 ; j <= sum ; j ++ ) { if ( dp [ i - 1 ] [ j ] == true ) { dp [ i ] [ j ] = true ; dp [ i ] [ j + arr [ i - 1 ] ] = true ; } } } for ( int j = 0 ; j <= sum ; j ++ ) if ( dp [ n ] [ j ] == true ) System . out . print ( j + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }
import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int maze [ ] [ ] ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( countPaths ( maze ) ) ; } }
import java . io . * ; class GFG { static int minSum ( int [ ] arr , int n ) { int [ ] dp = new int [ n ] ; if ( n == 1 ) return arr [ 0 ] ; if ( n == 2 ) return Math . min ( arr [ 0 ] , arr [ 1 ] ) ; if ( n == 3 ) return Math . min ( arr [ 0 ] , Math . min ( arr [ 1 ] , arr [ 2 ] ) ) ; if ( n == 4 ) return Math . min ( Math . min ( arr [ 0 ] , arr [ 1 ] ) , Math . min ( arr [ 2 ] , arr [ 3 ] ) ) ; dp [ 0 ] = arr [ 0 ] ; dp [ 1 ] = arr [ 1 ] ; dp [ 2 ] = arr [ 2 ] ; dp [ 3 ] = arr [ 3 ] ; for ( int i = 4 ; i < n ; i ++ ) dp [ i ] = arr [ i ] + Math . min ( Math . min ( dp [ i - 1 ] , dp [ i - 2 ] ) , Math . min ( dp [ i - 3 ] , dp [ i - 4 ] ) ) ; return Math . min ( Math . min ( dp [ n - 1 ] , dp [ n - 2 ] ) , Math . min ( dp [ n - 4 ] , dp [ n - 3 ] ) ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 3 , 4 , 5 , 6 , 1 } ; int n = arr . length ; System . out . println ( minSum ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isOperator ( char op ) { return ( op == ' + ' op == ' * ' ) ; } static void printMinAndMaxValueOfExp ( String exp ) { Vector < Integer > num = new Vector < Integer > ( ) ; Vector < Character > opr = new Vector < Character > ( ) ; String tmp = " " ; for ( int i = 0 ; i < exp . length ( ) ; i ++ ) { if ( isOperator ( exp . charAt ( i ) ) ) { opr . add ( exp . charAt ( i ) ) ; num . add ( Integer . parseInt ( tmp ) ) ; tmp = " " ; } else { tmp += exp . charAt ( i ) ; } } num . add ( Integer . parseInt ( tmp ) ) ; int len = num . size ( ) ; int [ ] [ ] minVal = new int [ len ] [ len ] ; int [ ] [ ] maxVal = new int [ len ] [ len ] ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = 0 ; j < len ; j ++ ) { minVal [ i ] [ j ] = Integer . MAX_VALUE ; maxVal [ i ] [ j ] = 0 ; if ( i == j ) minVal [ i ] [ j ] = maxVal [ i ] [ j ] = num . get ( i ) ; } } for ( int L = 2 ; L <= len ; L ++ ) { for ( int i = 0 ; i < len - L + 1 ; i ++ ) { int j = i + L - 1 ; for ( int k = i ; k < j ; k ++ ) { int minTmp = 0 , maxTmp = 0 ; if ( opr . get ( k ) == ' + ' ) { minTmp = minVal [ i ] [ k ] + minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] + maxVal [ k + 1 ] [ j ] ; } else if ( opr . get ( k ) == ' * ' ) { minTmp = minVal [ i ] [ k ] * minVal [ k + 1 ] [ j ] ; maxTmp = maxVal [ i ] [ k ] * maxVal [ k + 1 ] [ j ] ; } if ( minTmp < minVal [ i ] [ j ] ) minVal [ i ] [ j ] = minTmp ; if ( maxTmp > maxVal [ i ] [ j ] ) maxVal [ i ] [ j ] = maxTmp ; } } } System . out . print ( " Minimum ▁ value ▁ : ▁ " + minVal [ 0 ] [ len - 1 ] + " , ▁ Maximum ▁ value ▁ : ▁ " + maxVal [ 0 ] [ len - 1 ] ) ; } public static void main ( String [ ] args ) { String expression = "1 + 2*3 + 4*5" ; printMinAndMaxValueOfExp ( expression ) ; } }
public class GFG { static int CountPS ( char str [ ] , int n ) { int dp [ ] [ ] = new int [ n ] [ n ] ; boolean P [ ] [ ] = new boolean [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { P [ i ] [ i + 1 ] = true ; dp [ i ] [ i + 1 ] = 1 ; } } for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 ] [ j - 1 ] ) P [ i ] [ j ] = true ; if ( P [ i ] [ j ] == true ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] ; } } return dp [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { String str = " abaab " ; System . out . println ( CountPS ( str . toCharArray ( ) , str . length ( ) ) ) ; } }
import java . io . * ; class GFG { static int maxSumWO3Consec ( int arr [ ] , int n ) { int sum [ ] = new int [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = Math . max ( sum [ 1 ] , Math . max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = Math . max ( Math . max ( sum [ i - 1 ] , sum [ i - 2 ] + arr [ i ] ) , arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] ) ; return sum [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 100 , 1000 , 100 , 1000 , 1 } ; int n = arr . length ; System . out . println ( maxSumWO3Consec ( arr , n ) ) ; } }
class GFG { public static int lucas ( int n ) { if ( n == 0 ) return 2 ; if ( n == 1 ) return 1 ; return lucas ( n - 1 ) + lucas ( n - 2 ) ; } public static void main ( String args [ ] ) { int n = 9 ; System . out . println ( lucas ( n ) ) ; } }
import java . io . * ; class GFG { static int breakSum ( int n ) { if ( n == 0 n == 1 ) return n ; return Math . max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( breakSum ( n ) ) ; } }
public class GFG { static int countPS ( String str ) { int N = str . length ( ) ; int [ ] [ ] cps = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i ] [ i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i <= N - L ; i ++ ) { int k = L + i - 1 ; if ( str . charAt ( i ) == str . charAt ( k ) ) { cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] + 1 ; } else { cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] - cps [ i + 1 ] [ k - 1 ] ; } } } return cps [ 0 ] [ N - 1 ] ; } public static void main ( String args [ ] ) { String str = " abcb " ; System . out . println ( " Total ▁ palindromic ▁ " + " subsequence ▁ are ▁ : ▁ " + countPS ( str ) ) ; } }
class GFG { static int n ; static int [ ] [ ] dp = new int [ 1000 ] [ 1000 ] ; static String str = " abcb " ; static int countPS ( int i , int j ) { if ( i > j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( i == j ) return dp [ i ] [ j ] = 1 ; else if ( str . charAt ( i ) == str . charAt ( j ) ) return dp [ i ] [ j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) + 1 ; else return dp [ i ] [ j ] = countPS ( i + 1 , j ) + countPS ( i , j - 1 ) - countPS ( i + 1 , j - 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 1000 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) dp [ i ] [ j ] = - 1 ; n = str . length ( ) ; System . out . println ( " Total ▁ palindromic ▁ subsequence " + " are ▁ : ▁ " + countPS ( 0 , n - 1 ) ) ; } }
import java . io . * ; class GFG { static int countSub ( int arr [ ] , int n ) { int count [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - 1 ; j >= 0 ; j -- ) count [ arr [ i ] ] += count [ j ] ; count [ arr [ i ] ] ++ ; } int result = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) result += count [ i ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 5 , 4 } ; int n = arr . length ; System . out . println ( countSub ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int minimum ( int a , int b , int c ) { return Math . min ( Math . min ( a , b ) , c ) ; } static int findMinSum ( int arr [ ] , int n ) { int sum [ ] = new int [ n ] ; sum [ 0 ] = arr [ 0 ] ; sum [ 1 ] = arr [ 1 ] ; sum [ 2 ] = arr [ 2 ] ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = arr [ i ] + minimum ( sum [ i - 3 ] , sum [ i - 2 ] , sum [ i - 1 ] ) ; return minimum ( sum [ n - 1 ] , sum [ n - 2 ] , sum [ n - 3 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 20 , 2 , 10 , 1 } ; int n = arr . length ; System . out . println ( " Min ▁ Sum ▁ is ▁ " + findMinSum ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class SubsequenceCount { public static int countSub ( String s ) { HashMap < Character , Integer > map = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { map . put ( s . charAt ( i ) , - 1 ) ; } int allCount = 0 ; int levelCount = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char c = s . charAt ( i ) ; if ( i == 0 ) { allCount = 1 ; map . put ( c , 1 ) ; levelCount = 1 ; continue ; } levelCount = allCount + 1 ; if ( map . get ( c ) < 0 ) { allCount = allCount + levelCount ; } else { allCount = allCount + levelCount - map . get ( c ) ; } map . put ( c , levelCount ) ; } return allCount ; } public static void main ( String [ ] args ) { List < String > list = Arrays . asList ( " abab " , " gfg " ) ; for ( String s : list ) { int cnt = countSub ( s ) ; int withEmptyString = cnt + 1 ; System . out . println ( " With ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + withEmptyString ) ; System . out . println ( " Without ▁ empty ▁ string ▁ count ▁ for ▁ " + s + " ▁ is ▁ " + cnt ) ; } } }
import java . util . * ; class GFG { public static int MinimumCost ( int cost [ ] , int n , int W ) { Vector < Integer > val = new Vector < Integer > ( ) ; Vector < Integer > wt = new Vector < Integer > ( ) ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . add ( cost [ i ] ) ; wt . add ( i + 1 ) ; size ++ ; } } n = size ; int min_cost [ ] [ ] = new int [ n + 1 ] [ W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) min_cost [ 0 ] [ i ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= n ; i ++ ) min_cost [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= W ; j ++ ) { if ( wt . get ( i - 1 ) > j ) min_cost [ i ] [ j ] = min_cost [ i - 1 ] [ j ] ; else min_cost [ i ] [ j ] = Math . min ( min_cost [ i - 1 ] [ j ] , min_cost [ i ] [ j - wt . get ( i - 1 ) ] + val . get ( i - 1 ) ) ; } } return ( min_cost [ n ] [ W ] == Integer . MAX_VALUE ) ? - 1 : min_cost [ n ] [ W ] ; } public static void main ( String [ ] args ) { int cost [ ] = { 1 , 2 , 3 , 4 , 5 } , W = 5 ; int n = cost . length ; System . out . println ( MinimumCost ( cost , n , W ) ) ; } }
import java . io . * ; class GFG { static int count ( String a , String b , int m , int n ) { if ( ( m == 0 && n == 0 ) n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a . charAt ( m - 1 ) == b . charAt ( n - 1 ) ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; } public static void main ( String [ ] args ) { String a = " GeeksforGeeks " ; String b = " Gks " ; System . out . println ( count ( a , b , a . length ( ) , b . length ( ) ) ) ; } }
import java . io . * ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 j == 0 ) L [ i ] [ j ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } static int findMinCost ( String X , String Y , int costX , int costY ) { int m = X . length ( ) ; int n = Y . length ( ) ; int len_LCS ; len_LCS = lcs ( X , Y , m , n ) ; return costX * ( m - len_LCS ) + costY * ( n - len_LCS ) ; } public static void main ( String [ ] args ) { String X = " ef " ; String Y = " gh " ; System . out . println ( " Minimum ▁ Cost ▁ to ▁ make ▁ two ▁ strings ▁ " + " ▁ identical ▁ is ▁ = ▁ " + findMinCost ( X , Y , 10 , 20 ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . Collections ; import java . util . Comparator ; class Graph { static class Job { int start , finish , profit ; public Job ( int start , int finish , int profit ) { this . start = start ; this . finish = finish ; this . profit = profit ; } } ; static int findSum ( ArrayList < Job > arr ) { int sum = 0 ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) sum += arr . get ( i ) . profit ; return sum ; } static void findMaxProfit ( ArrayList < Job > arr ) { Collections . sort ( arr , new Comparator < Job > ( ) { @ Override public int compare ( Job x , Job y ) { return x . start - y . start ; } } ) ; ArrayList < ArrayList < Job > > L = new ArrayList < > ( ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { L . add ( new ArrayList < > ( ) ) ; } L . get ( 0 ) . add ( arr . get ( 0 ) ) ; for ( int i = 1 ; i < arr . size ( ) ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr . get ( j ) . finish <= arr . get ( i ) . start ) && ( findSum ( L . get ( j ) ) > findSum ( L . get ( i ) ) ) ) { ArrayList < Job > copied = new ArrayList < > ( L . get ( j ) ) ; L . set ( i , copied ) ; } } L . get ( i ) . add ( arr . get ( i ) ) ; } ArrayList < Job > maxChain = new ArrayList < > ( ) ; for ( int i = 0 ; i < L . size ( ) ; i ++ ) if ( findSum ( L . get ( i ) ) > findSum ( maxChain ) ) maxChain = L . get ( i ) ; for ( int i = 0 ; i < maxChain . size ( ) ; i ++ ) { System . out . printf ( "(%d, %d, %d)NEW_LINE", maxChain . get ( i ) . start , maxChain . get ( i ) . finish , maxChain . get ( i ) . profit ) ; } } public static void main ( String [ ] args ) { Job [ ] a = { new Job ( 3 , 10 , 20 ) , new Job ( 1 , 2 , 50 ) , new Job ( 6 , 19 , 100 ) , new Job ( 2 , 100 , 200 ) } ; ArrayList < Job > arr = new ArrayList < > ( Arrays . asList ( a ) ) ; findMaxProfit ( arr ) ; } }
import java . io . * ; class GFG { static int countIntegralSolutions ( int n ) { int result = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n - i ; j ++ ) for ( int k = 0 ; k <= ( n - i - j ) ; k ++ ) if ( i + j + k == n ) result ++ ; return result ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countIntegralSolutions ( n ) ) ; } }
import java . util . * ; class GFG { static int maxLeftSubArraySum ( int a [ ] , int size , int sum [ ] ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; sum [ 0 ] = max_so_far ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; sum [ i ] = max_so_far ; } return max_so_far ; } static int maxRightSubArraySum ( int a [ ] , int n , int sum [ ] ) { int max_so_far = a [ n ] ; int curr_max = a [ n ] ; sum [ n ] = max_so_far ; for ( int i = n - 1 ; i >= 0 ; i -- ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; sum [ i ] = max_so_far ; } return max_so_far ; } static int findMaxAbsDiff ( int arr [ ] , int n ) { int leftMax [ ] = new int [ n ] ; maxLeftSubArraySum ( arr , n , leftMax ) ; int rightMax [ ] = new int [ n ] ; maxRightSubArraySum ( arr , n - 1 , rightMax ) ; int invertArr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) invertArr [ i ] = - arr [ i ] ; int leftMin [ ] = new int [ n ] ; maxLeftSubArraySum ( invertArr , n , leftMin ) ; for ( int i = 0 ; i < n ; i ++ ) leftMin [ i ] = - leftMin [ i ] ; int rightMin [ ] = new int [ n ] ; maxRightSubArraySum ( invertArr , n - 1 , rightMin ) ; for ( int i = 0 ; i < n ; i ++ ) rightMin [ i ] = - rightMin [ i ] ; int result = - 2147483648 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int absValue = Math . max ( Math . abs ( leftMax [ i ] - rightMin [ i + 1 ] ) , Math . abs ( leftMin [ i ] - rightMax [ i + 1 ] ) ) ; if ( absValue > result ) result = absValue ; } return result ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; System . out . print ( findMaxAbsDiff ( a , n ) ) ; } }
import java . util . * ; class GFG { static int M = 4 ; static int N = 4 ; static class Point { int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static List < Point > findPath ( int grid [ ] [ ] , int mat [ ] [ ] , int i , int j ) { List < Point > path = new LinkedList < > ( ) ; Point pt = new Point ( i , j ) ; path . add ( 0 , pt ) ; while ( grid [ i ] [ j ] != 0 ) { if ( i > 0 && grid [ i ] [ j ] - 1 == grid [ i - 1 ] [ j ] ) { pt = new Point ( i - 1 , j ) ; path . add ( 0 , pt ) ; i -- ; } else if ( j > 0 && grid [ i ] [ j ] - 1 == grid [ i ] [ j - 1 ] ) { pt = new Point ( i , j - 1 ) ; path . add ( 0 , pt ) ; j -- ; } } return path ; } static void findSnakeSequence ( int mat [ ] [ ] ) { int [ ] [ ] lookup = new int [ M ] [ N ] ; int max_len = 0 ; int max_row = 0 ; int max_col = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i != 0 j != 0 ) { if ( i > 0 && Math . abs ( mat [ i - 1 ] [ j ] - mat [ i ] [ j ] ) == 1 ) { lookup [ i ] [ j ] = Math . max ( lookup [ i ] [ j ] , lookup [ i - 1 ] [ j ] + 1 ) ; if ( max_len < lookup [ i ] [ j ] ) { max_len = lookup [ i ] [ j ] ; max_row = i ; max_col = j ; } } if ( j > 0 && Math . abs ( mat [ i ] [ j - 1 ] - mat [ i ] [ j ] ) == 1 ) { lookup [ i ] [ j ] = Math . max ( lookup [ i ] [ j ] , lookup [ i ] [ j - 1 ] + 1 ) ; if ( max_len < lookup [ i ] [ j ] ) { max_len = lookup [ i ] [ j ] ; max_row = i ; max_col = j ; } } } } } System . out . print ( " Maximum ▁ length ▁ of ▁ Snake ▁ " + " sequence ▁ is : ▁ " + max_len + "NEW_LINE"); List < Point > path = findPath ( lookup , mat , max_row , max_col ) ; System . out . print ( " Snake ▁ sequence ▁ is : " ) ; for ( Point it : path ) System . out . print ( " " ▁ + ▁ mat [ it . x ] [ it . y ] ▁ + ▁ " ( " ▁ + ▁ it . x ▁ + ▁ " , " ▁ + ▁ it . y ▁ + ▁ " ) "); } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 9 , 6 , 5 , 2 } , { 8 , 7 , 6 , 5 } , { 7 , 3 , 1 , 6 } , { 1 , 1 , 1 , 7 } } ; findSnakeSequence ( mat ) ; } }
class GFG { static int countWays ( int p , int q , int r , int last ) { if ( p < 0 q < 0 r < 0 ) return 0 ; if ( p == 1 && q == 0 && r == 0 && last == 0 ) return 1 ; if ( p == 0 && q == 1 && r == 0 && last == 1 ) return 1 ; if ( p == 0 && q == 0 && r == 1 && last == 2 ) return 1 ; if ( last == 0 ) return countWays ( p - 1 , q , r , 1 ) + countWays ( p - 1 , q , r , 2 ) ; if ( last == 1 ) return countWays ( p , q - 1 , r , 0 ) + countWays ( p , q - 1 , r , 2 ) ; if ( last == 2 ) return countWays ( p , q , r - 1 , 0 ) + countWays ( p , q , r - 1 , 1 ) ; return 0 ; } static int countUtil ( int p , int q , int r ) { } public static void main ( String [ ] args ) { int p = 1 , q = 1 , r = 1 ; System . out . print ( countUtil ( p , q , r ) ) ; } }
import java . util . * ; class GFG { public static int findMinRec ( int arr [ ] , int i , int sumCalculated , int sumTotal ) { if ( i == 0 ) return Math . abs ( ( sumTotal - sumCalculated ) - sumCalculated ) ; return Math . min ( findMinRec ( arr , i - 1 , sumCalculated + arr [ i - 1 ] , sumTotal ) , findMinRec ( arr , i - 1 , sumCalculated , sumTotal ) ) ; } public static int findMin ( int arr [ ] , int n ) { int sumTotal = 0 ; for ( int i = 0 ; i < n ; i ++ ) sumTotal += arr [ i ] ; return findMinRec ( arr , n , 0 , sumTotal ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 2 , 2 , 1 } ; int n = arr . length ; System . out . print ( " The ▁ minimum ▁ difference " + " ▁ between ▁ two ▁ sets ▁ is ▁ " + findMin ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int minDifference ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int y = sum / 2 + 1 ; boolean dp [ ] = new boolean [ y ] , dd [ ] = new boolean [ y ] ; for ( int i = 0 ; i < y ; i ++ ) { dp [ i ] = dd [ i ] = false ; } dd [ 0 ] = true ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j + arr [ i ] < y ; j ++ ) { if ( dp [ j ] ) dd [ j + arr [ i ] ] = true ; } for ( int j = 0 ; j < y ; j ++ ) { if ( dd [ j ] ) dp [ j ] = true ; } } for ( int i = y - 1 ; i >= 0 ; i -- ) { if ( dp [ i ] ) return ( sum - 2 * i ) ; } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 11 , 5 } ; int n = arr . length ; System . out . print ( " The ▁ Minimum ▁ difference ▁ of ▁ 2 ▁ sets ▁ is ▁ " + minDifference ( arr , n ) + 'NEW_LINE'); } }
import java . io . * ; class GFG { public static int countP ( int n , int k ) { if ( n == 0 k == 0 k > n ) return 0 ; if ( k == 1 k == n ) return 1 ; return ( k * countP ( n - 1 , k ) + countP ( n - 1 , k - 1 ) ) ; } public static void main ( String args [ ] ) { System . out . println ( countP ( 3 , 2 ) ) ; } }
class GFG { static int countP ( int n , int k ) { int [ ] [ ] dp = new int [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 ] [ k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) if ( j == 1 i == j ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = j * dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] ; return dp [ n ] [ k ] ; } public static void main ( String [ ] args ) { System . out . println ( countP ( 5 , 2 ) ) ; } }
import java . io . * ; class GFG { static int printCountRec ( int dist ) { if ( dist < 0 ) return 0 ; if ( dist == 0 ) return 1 ; return printCountRec ( dist - 1 ) + printCountRec ( dist - 2 ) + printCountRec ( dist - 3 ) ; } public static void main ( String [ ] args ) { int dist = 4 ; System . out . println ( printCountRec ( dist ) ) ; } }
import java . util . * ; class GFG { static int printCountDP ( int dist ) { int [ ] ways = new int [ 3 ] ; int n = dist ; ways [ 0 ] = 1 ; ways [ 1 ] = 1 ; ways [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) ways [ i % 3 ] = ways [ ( i - 1 ) % 3 ] + ways [ ( i - 2 ) % 3 ] + ways [ ( i - 3 ) % 3 ] ; return ways [ n % 3 ] ; } public static void main ( String arg [ ] ) { int dist = 4 ; System . out . print ( printCountDP ( dist ) ) ; } }
import java . io . * ; class GFG { static int countNumbersWith4 ( int n ) { int result = 0 ; for ( int x = 1 ; x <= n ; x ++ ) result += has4 ( x ) ? 1 : 0 ; return result ; } static boolean has4 ( int x ) { while ( x != 0 ) { if ( x % 10 == 4 ) return true ; x = x / 10 ; } return false ; } public static void main ( String args [ ] ) { int n = 328 ; System . out . println ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " + " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ a ▁ digit ▁ is ▁ " + countNumbersWith4 ( n ) ) ; } }
class GFG { static int countNumbersWith4 ( int n ) { if ( n < 4 ) return 0 ; int d = ( int ) Math . log10 ( n ) ; int [ ] a = new int [ d + 2 ] ; a [ 0 ] = 0 ; a [ 1 ] = 1 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 9 + ( int ) Math . ceil ( Math . pow ( 10 , i - 1 ) ) ; int p = ( int ) Math . ceil ( Math . pow ( 10 , d ) ) ; int msd = n / p ; if ( msd == 4 ) return ( msd ) * a [ d ] + ( n % p ) + 1 ; if ( msd > 4 ) return ( msd - 1 ) * a [ d ] + p + countNumbersWith4 ( n % p ) ; return ( msd ) * a [ d ] + countNumbersWith4 ( n % p ) ; } public static void main ( String [ ] args ) { int n = 328 ; System . out . println ( " Count ▁ of ▁ numbers ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ that ▁ have ▁ 4 ▁ as ▁ a ▁ digit ▁ is ▁ " + countNumbersWith4 ( n ) ) ; } }
class GFG { static int minRemovalsDP ( int arr [ ] , int n ) { int longest_start = - 1 , longest_end = 0 ; for ( int start = 0 ; start < n ; start ++ ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; for ( int end = start ; end < n ; end ++ ) { int val = arr [ end ] ; if ( val < min ) { min = val ; } if ( val > max ) { max = val ; } if ( 2 * min <= max ) { break ; } if ( end - start > longest_end - longest_start longest_start == - 1 ) { longest_start = start ; longest_end = end ; } } } if ( longest_start == - 1 ) { return n ; } return ( n - ( longest_end - longest_start + 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 100 , 9 , 10 , 11 , 12 , 15 , 200 } ; int n = arr . length ; System . out . println ( minRemovalsDP ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int optimalStrategyOfGame ( int arr [ ] , int n ) { int table [ ] [ ] = new int [ n ] [ n ] ; int gap , i , j , x , y , z ; for ( gap = 0 ; gap < n ; ++ gap ) { for ( i = 0 , j = gap ; j < n ; ++ i , ++ j ) { x = ( ( i + 2 ) <= j ) ? table [ i + 2 ] [ j ] : 0 ; y = ( ( i + 1 ) <= ( j - 1 ) ) ? table [ i + 1 ] [ j - 1 ] : 0 ; z = ( i <= ( j - 2 ) ) ? table [ i ] [ j - 2 ] : 0 ; table [ i ] [ j ] = Math . max ( arr [ i ] + Math . min ( x , y ) , arr [ j ] + Math . min ( y , z ) ) ; } } return table [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = arr1 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr1 , n ) ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = arr2 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr2 , n ) ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr3 , n ) ) ; } }
import java . io . * ; class GFG { public static boolean findPartiion ( int arr [ ] , int n ) { int sum = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; boolean [ ] part = new boolean [ sum / 2 + 1 ] ; for ( i = 0 ; i <= sum / 2 ; i ++ ) { part [ i ] = false ; } for ( i = 0 ; i < n ; i ++ ) { for ( j = sum / 2 ; j >= arr [ i ] ; j -- ) { if ( part [ j - arr [ i ] ] == true j == arr [ i ] ) part [ j ] = true ; } } return part [ sum / 2 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 3 , 2 , 3 , 2 } ; int n = 6 ; if ( findPartiion ( arr , n ) == true ) System . out . println ( " Can ▁ be ▁ divided ▁ into ▁ two ▁ " + " subsets ▁ of ▁ equal ▁ sum " ) ; else System . out . println ( " Can ▁ not ▁ be ▁ divided ▁ into ▁ " + " two ▁ subsets ▁ of ▁ equal ▁ sum " ) ; } }
import java . util . * ; class GFG { static int binomialCoeff ( int n , int r ) { if ( r > n ) return 0 ; long m = 1000000007 ; long inv [ ] = new long [ r + 1 ] ; inv [ 0 ] = 1 ; if ( r + 1 >= 2 ) inv [ 1 ] = 1 ; for ( int i = 2 ; i <= r ; i ++ ) { inv [ i ] = m - ( m / i ) * inv [ ( int ) ( m % i ) ] % m ; } int ans = 1 ; for ( int i = 2 ; i <= r ; i ++ ) { ans = ( int ) ( ( ( ans % m ) * ( inv [ i ] % m ) ) % m ) ; } for ( int i = n ; i >= ( n - r + 1 ) ; i -- ) { ans = ( int ) ( ( ( ans % m ) * ( i % m ) ) % m ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , r = 2 ; System . out . print ( " Value ▁ of ▁ C ( " + n + " , ▁ " + r + " ) ▁ is ▁ " + binomialCoeff ( n , r ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 100 ] [ 100 ] ; static int matrixChainMemoised ( int [ ] p , int i , int j ) { if ( i == j ) { return 0 ; } if ( dp [ i ] [ j ] != - 1 ) { return dp [ i ] [ j ] ; } dp [ i ] [ j ] = Integer . MAX_VALUE ; for ( int k = i ; k < j ; k ++ ) { dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , matrixChainMemoised ( p , i , k ) + matrixChainMemoised ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ) ; } return dp [ i ] [ j ] ; } static int MatrixChainOrder ( int [ ] p , int n ) { int i = 1 , j = n - 1 ; return matrixChainMemoised ( p , i , j ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; System . out . println ( " Minimum ▁ number ▁ of ▁ multiplications ▁ is ▁ " + MatrixChainOrder ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int checkArray ( int arr [ ] , int N , int k ) { if ( N < 3 ) return - 1 ; int i , Sum = 0 , temp = 0 ; for ( i = 0 ; i < N ; i ++ ) Sum += arr [ i ] ; int R = ( k * k + k + 1 ) ; if ( Sum % R != 0 ) return 0 ; int Mid = k * ( Sum / R ) ; for ( i = 1 ; i < N - 1 ; i ++ ) { temp += arr [ i - 1 ] ; if ( arr [ i ] == Mid ) { if ( temp == Mid / k ) return i + 1 ; else return 0 ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 4 , 20 , 6 , 15 , 9 , 10 } ; int N = arr . length ; int K = 2 ; System . out . println ( checkArray ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static int minimumPalindromicStrings ( String S ) { int N = S . length ( ) ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ S . charAt ( i ) - ' a ' ] ++ ; } Vector < Integer > oddFreqCharacters = new Vector < Integer > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 == 1 ) { oddFreqCharacters . add ( i ) ; freq [ i ] -- ; } freq [ i ] /= 2 ; } Vector < String > ans = new Vector < String > ( ) ; if ( oddFreqCharacters . isEmpty ( ) ) { String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . add ( left + right ) ; } else { String middle = " " ; int c = oddFreqCharacters . lastElement ( ) ; oddFreqCharacters . remove ( oddFreqCharacters . size ( ) - 1 ) ; middle += ( char ) ( c + ' a ' ) ; String left = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { left += ( char ) ( i + ' a ' ) ; } } String right = left ; right = reverse ( right ) ; ans . add ( left + middle + right ) ; while ( ! oddFreqCharacters . isEmpty ( ) ) { c = oddFreqCharacters . lastElement ( ) ; oddFreqCharacters . remove ( oddFreqCharacters . size ( ) - 1 ) ; middle = " " ; middle += ( char ) ( c + ' a ' ) ; ans . add ( middle ) ; } } System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( ans . get ( i ) ) ; if ( i != ans . size ( ) - 1 ) { System . out . print ( " , ▁ " ) ; } } System . out . print ( " ] " ) ; return 0 ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; minimumPalindromicStrings ( S ) ; } }
import java . io . * ; class GFG { static void possibleEqualArray ( int A [ ] , int N ) { int [ ] pref = new int [ N ] ; pref [ 0 ] = A [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { pref [ i ] = pref [ i - 1 ] ^ A [ i ] ; } if ( pref [ N - 1 ] == 0 ) { System . out . println ( " YES " ) ; return ; } int cur_xor = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { cur_xor ^= A [ i ] ; for ( int j = 0 ; j < i ; j ++ ) { if ( j != 0 ) { int middle_xor = pref [ j - 1 ] ^ pref [ i - 1 ] ; int left_xor = pref [ j - 1 ] ; int right_xor = cur_xor ; if ( left_xor == middle_xor && middle_xor == right_xor ) { System . out . println ( " YES " ) ; return ; } } } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 2 , 2 } ; int N = A . length ; possibleEqualArray ( A , N ) ; } }
class GFG { static void GenerateArray ( int N , int X ) { int prev_xor = X ; for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( i ^ prev_xor ) ; if ( i != N ) { System . out . print ( " ▁ " ) ; } prev_xor = i ; } } public static void main ( String args [ ] ) { int N = 4 , X = 3 ; System . out . print ( " The ▁ generated ▁ array ▁ is ▁ " ) ; GenerateArray ( N , X ) ; } }
import java . io . * ; class GFG { static int INF64 = ( int ) 1e10 ; static int N = 200 * 1000 + 13 ; static int n ; static int arr [ ] = new int [ N ] ; static int brr [ ] = new int [ N ] ; static void brute ( int ind , int l , int r ) { if ( ind == n / 2 ) { for ( int i = 0 ; i < ( int ) n ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; System . exit ( 0 ) ; } for ( int i = l ; i <= arr [ ind ] / 2 ; ++ i ) if ( arr [ ind ] - i <= r ) { brr [ ind ] = i ; brr [ n - ind - 1 ] = arr [ ind ] - i ; brute ( ind + 1 , i , arr [ ind ] - i ) ; } } public static void main ( String [ ] args ) { n = 2 ; n *= 2 ; arr [ 0 ] = 5 ; arr [ 1 ] = 6 ; brute ( 0 , 0 , INF64 ) ; } }
import java . util . * ; class GFG { static int minCount ( int N , int K ) { int SmallestNumber [ ] = new int [ 10 ] ; int MinimumSteps [ ] = new int [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) { SmallestNumber [ i ] = Integer . MAX_VALUE ; MinimumSteps [ i ] = Integer . MAX_VALUE ; } for ( int i = 1 ; i <= 10 ; i ++ ) { int num = K * i ; SmallestNumber [ num % 10 ] = Math . min ( SmallestNumber [ num % 10 ] , num ) ; MinimumSteps [ num % 10 ] = Math . min ( MinimumSteps [ num % 10 ] , i ) ; } if ( N < SmallestNumber [ N % 10 ] ) { return - 1 ; } else { return MinimumSteps [ N % 10 ] ; } } public static void main ( String [ ] args ) { int N = 42 , K = 7 ; System . out . println ( minCount ( N , K ) ) ; } }
import java . io . * ; class GFG { static int countMinSteps ( int arr [ ] , int N ) { int val = 0 ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int curr = arr [ i ] ; mx = Math . max ( mx , curr ) ; val = Math . max ( val , mx - curr ) ; } long res = 0 ; while ( ( 1 << res ) - 1 < val ) { ++ res ; } return ( int ) res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 6 , 5 } ; int N = arr . length ; System . out . println ( countMinSteps ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int func2 ( int L , int R , int K ) { if ( R - L >= 2 ) return 1 ; return Math . min ( L , L ^ R ) ; } static int func3 ( int L , int R , int K ) { if ( ( R ^ L ) > L && ( R ^ L ) < R ) return 0 ; return func2 ( L , R , K ) ; } static int func4 ( int L , int R , int K ) { if ( R - L >= 4 ) return 0 ; int minval = L ^ ( L + 1 ) ^ ( L + 2 ) ^ ( L + 3 ) ; return Math . min ( minval , func3 ( L , R , K ) ) ; } static int minimumXor ( int L , int R , int K ) { if ( K > 4 ) return 0 ; else if ( K == 4 ) return func4 ( L , R , K ) ; else if ( K == 3 ) return func3 ( L , R , K ) ; else if ( K == 2 ) return func2 ( L , R , K ) ; else return L ; } public static void main ( String [ ] args ) { int L = 1 , R = 3 , K = 3 ; System . out . println ( minimumXor ( L , R , K ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int setBits ( long n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static void findNthTerm ( int N ) { int x = setBits ( N ^ ( N - 1 ) ) ; System . out . println ( x ) ; } public static void main ( String [ ] args ) { int N = 8 ; findNthTerm ( N ) ; } }
class GFG { static void findEquation ( int A , int B , int C ) { System . out . print ( " ( " + C + " ) " + " x ^ 2 ▁ + ( " + B + " ) x ▁ + ▁ ( " + A + " ) ▁ = ▁ 0" ) ; } public static void main ( String args [ ] ) { int A = 1 , B = - 5 , C = 6 ; findEquation ( A , B , C ) ; } }
import java . util . * ; class GFG { static String checkPattern ( int arr [ ] , int m , int k , int n ) { int count = 1 , t = 0 ; for ( int i = 0 ; i < n - m ; i ++ ) { if ( arr [ i ] == arr [ i + m ] ) { t ++ ; if ( t == m ) { t = 0 ; count ++ ; if ( count == k ) { return " Yes " ; } } } else { t = 0 ; count = 1 ; } } return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 3 , 3 , 3 , 3 } ; int M = 1 , K = 3 ; int N = arr . length ; System . out . print ( checkPattern ( arr , M , K , N ) ) ; } }
class GFG { static void findOccurrences ( String S , String T ) { int n1 = S . length ( ) ; int n2 = T . length ( ) ; int ans = 0 ; int last = 0 ; for ( int i = 0 ; i <= n1 - n2 ; i ++ ) { boolean chk = true ; for ( int j = 0 ; j < n2 ; j ++ ) { if ( T . charAt ( j ) != S . charAt ( i + j ) ) { chk = false ; break ; } } if ( chk ) { ans += ( i + 1 - last ) * ( n1 - ( i + n2 - 1 ) ) ; last = i + 1 ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " dabc " , T = " ab " ; findOccurrences ( S , T ) ; } }
import java . util . * ; class GFG { static void LargestArray ( int N , int low , int high ) { int high_index = N - ( high - low + 1 ) ; if ( high_index > ( N - 1 ) / 2 ) { System . out . print ( " Not ▁ Possible " ) ; return ; } if ( high_index <= 0 ) high_index = 1 ; int [ ] A = new int [ N ] ; int temp = high ; for ( int i = high_index ; i >= 0 ; i -- ) { A [ i ] = temp -- ; } high -= 1 ; for ( int i = high_index + 1 ; i < N ; i ++ ) A [ i ] = high -- ; for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( A [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 , low = 2 , high = 6 ; LargestArray ( N , low , high ) ; } }
import java . util . * ; class GFG { static void flipBitsOfAandB ( int A , int B ) { A = A ^ ( A & B ) ; B = B ^ ( A & B ) ; System . out . print ( A + " ▁ " + B ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 20 ; flipBitsOfAandB ( A , B ) ; } }
import java . util . * ; class GFG { static void isValid ( String s ) { int n = ( int ) Math . sqrt ( s . length ( ) ) ; char check = s . charAt ( 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = i , y = i ; while ( x >= 0 && y < n ) { if ( s . charAt ( ( n * x ) + y ) != check || s . charAt ( ( n * y ) + x ) != check ) { System . out . print ( " No " ) ; return ; } x -- ; y ++ ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { String str = " abacdaeaafaghaia " ; isValid ( str ) ; } }
import java . io . * ; class GFG { public static int gcd ( int a , int b ) { if ( a < b ) { int t = a ; a = b ; b = t ; } if ( a % b == 0 ) return b ; return gcd ( b , a % b ) ; } static void printAnswer ( int x , int y ) { int val = gcd ( x , y ) ; if ( ( val & ( val - 1 ) ) == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int x = 4 ; int y = 7 ; printAnswer ( x , y ) ; } }
import java . util . * ; class GFG { static int N = 10 ; static int [ ] pref = new int [ N ] ; static int [ ] power = new int [ N ] ; static int toDigit ( char ch ) { return ( ch - '0' ) ; } static void powerOf10 ( ) { power [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) power [ i ] = power [ i - 1 ] * 10 ; } static void precomputePrefix ( char [ ] str , int n ) { pref [ 0 ] = str [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + toDigit ( str [ i ] ) ; } static int triangularNumber ( int i ) { int res = i * ( i + 1 ) / 2 ; return res ; } static int sumOfSubStrings ( String str ) { int n = str . length ( ) ; powerOf10 ( ) ; precomputePrefix ( str . toCharArray ( ) , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( pref [ n - i - 2 ] * ( i + 1 ) + toDigit ( str . charAt ( n - i - 1 ) ) * triangularNumber ( n - i - 1 ) ) * power [ i ] ; } return ans ; } public static void main ( String [ ] args ) { String str = "1234" ; System . out . print ( sumOfSubStrings ( str ) ) ; } }
class GFG { static int OddLengthSum ( int [ ] arr ) { int sum = 0 ; int l = arr . length ; for ( int i = 0 ; i < l ; i ++ ) { sum += ( ( ( i + 1 ) * ( l - i ) + 1 ) / 2 ) * arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 5 , 3 , 1 , 2 } ; System . out . print ( OddLengthSum ( arr ) ) ; } }
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 " ) ; } }
import java . util . * ; class GFG { static void solve ( int arr [ ] , int N ) { HashMap < Integer , Integer > M = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { M . put ( i , arr [ i ] ) ; } Arrays . sort ( arr ) ; HashMap < Integer , Integer > Count = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Count . put ( arr [ i ] , ( int ) Math . pow ( 2 , N - i - 1 ) ) ; } for ( Map . Entry < Integer , Integer > m : M . entrySet ( ) ) { System . out . print ( Count . get ( m . getValue ( ) ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 1 } ; int N = arr . length ; solve ( arr , N ) ; } }
class GFG { static boolean checkOdd ( char ch ) { return ( ( ch - '0' ) & 1 ) != 0 ? true : false ; } static String Insert_dash ( String num_str ) { StringBuilder result_str = new StringBuilder ( num_str ) ; for ( int x = 0 ; x < num_str . length ( ) - 1 ; x ++ ) { if ( checkOdd ( num_str . charAt ( x ) ) && checkOdd ( num_str . charAt ( x + 1 ) ) ) { result_str . insert ( x + 1 , " - " ) ; num_str = result_str . toString ( ) ; x ++ ; } } return result_str . toString ( ) ; } public static void main ( String [ ] args ) { String str = "1745389" ; System . out . println ( Insert_dash ( str ) ) ; } }
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 " + "NEW_LINE"); 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 " + "NEW_LINE"); return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 2 , 3 , 4 } , { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; check ( m ) ; } }
import java . util . * ; class GFG { static int SpiralElement ( int x , int y ) { int r ; if ( x < y ) { if ( y % 2 == 1 ) { r = y * y ; return ( r - x + 1 ) ; } else { r = ( y - 1 ) * ( y - 1 ) ; return ( r + x ) ; } } else { if ( x % 2 == 0 ) { r = x * x ; return ( r - y + 1 ) ; } else { r = ( x - 1 ) * ( x - 1 ) ; return ( r + y ) ; } } } public static void main ( String [ ] args ) { int x = 2 , y = 3 ; System . out . println ( SpiralElement ( x , y ) ) ; } }
import java . util . * ; class GFG { static void string_modify ( char [ ] s ) { char [ ] alphabets = new char [ 26 ] ; int flag = 0 , hold_i = 0 ; char hold_l = 0 ; int i ; for ( i = 0 ; i < 26 ; i ++ ) { alphabets [ i ] = ( char ) ( i + ' a ' ) ; } HashSet < Integer > difference = new HashSet < Integer > ( ) ; String reconstruct = " " ; for ( i = 1 ; i < s . length ; i ++ ) { difference . add ( s [ i ] - s [ i - 1 ] ) ; } if ( difference . size ( ) == 1 ) { System . out . print ( " No ▁ modifications ▁ required " ) ; return ; } for ( int it : difference ) { int index = s [ 0 ] - ' a ' ; reconstruct = " " ; flag = 0 ; for ( i = 0 ; i < s . length && flag <= 1 ; i ++ ) { reconstruct += alphabets [ index ] ; index += it ; if ( index < 0 ) { index += 26 ; } index %= 26 ; if ( reconstruct . charAt ( i ) != s [ i ] ) { flag ++ ; hold_i = i ; hold_l = s [ i ] ; } } if ( flag == 1 ) { s [ hold_i ] = reconstruct . charAt ( hold_i ) ; break ; } } if ( flag < 1 ) { hold_i = 0 ; hold_l = s [ 0 ] ; int temp = ( s [ 1 ] - ' a ' - ( s [ 2 ] - s [ 1 ] ) ) % 26 ; if ( temp < 0 ) { temp += 26 ; } s [ 0 ] = alphabets [ temp ] ; } System . out . print ( hold_i + " ▁ - > ▁ " + hold_l + "NEW_LINE" + String . valueOf ( s ) + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = " aeimqux " ; string_modify ( s . toCharArray ( ) ) ; } }
import java . util . * ; class GFG { static void evenSubMatrix ( int N ) { int even = 1 ; int odd = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( i + j ) % 2 == 0 ) { System . out . print ( even + " ▁ " ) ; even += 2 ; } else { System . out . print ( odd + " ▁ " ) ; odd += 2 ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 4 ; evenSubMatrix ( N ) ; } }
import java . util . regex . * ; class GFG { public static void removeLeadingZeros ( String str ) { String regex = " ^ 0 + ( ? ! $ ) " ; str = str . replaceAll ( regex , " " ) ; System . out . println ( str ) ; } public static void main ( String args [ ] ) { String str = "0001234" ; removeLeadingZeros ( str ) ; } }
import java . util . * ; class GFG { static int TotalHammingDistance ( int n ) { int i = 1 , sum = 0 ; while ( n / i > 0 ) { sum = sum + n / i ; i = i * 2 ; } return sum ; } public static void main ( String [ ] args ) { int N = 9 ; System . out . println ( TotalHammingDistance ( N ) ) ; } }
import java . util . * ; class GFG { static void maximum_inversion ( int n , int k ) { int answer = 0 ; k = Math . min ( k , n / 2 ) ; int left = 1 ; int right = n ; while ( k != 0 ) { k -- ; answer += 2 * ( right - left ) - 1 ; left ++ ; right -- ; } System . out . println ( answer ) ; } public static void main ( String s [ ] ) { int N = 5 ; int K = 3 ; maximum_inversion ( N , K ) ; N = 4 ; K = 1 ; maximum_inversion ( N , K ) ; } }
import java . util . * ; import java . lang . Math ; import java . io . * ; class GFG { static int getFirstSetBitPos ( int n ) { return ( int ) ( Math . log ( n & - n ) / Math . log ( 2 ) ) ; } static int oddFirst ( int a , int b ) { int steps_a = getFirstSetBitPos ( a ) ; int steps_b = getFirstSetBitPos ( b ) ; if ( steps_a == steps_b ) { return - 1 ; } else if ( steps_a > steps_b ) { return b ; } else { return a ; } } public static void main ( String [ ] args ) { int a = 10 ; int b = 8 ; System . out . print ( oddFirst ( a , b ) ) ; } }
import java . util . * ; class GFG { public static boolean helper ( String s , int pos ) { int len = s . length ( ) ; if ( pos >= len ) return true ; if ( ! Character . isDigit ( s . charAt ( pos ) ) ) return false ; int num = 0 ; for ( int i = pos ; i < len ; i ++ ) { num = num * 10 + s . charAt ( pos ) - '0' ; if ( i + 1 + num > len ) return false ; if ( helper ( s , i + 1 + num ) ) return true ; } return false ; } public static void main ( String [ ] args ) { String s = "123abc4db1c " ; if ( helper ( s , 0 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static final int m = 1000000007 ; static void solve ( long n ) { long s = 0 ; for ( int l = 1 ; l <= n ; ) { int r = ( int ) ( n / Math . floor ( n / l ) ) ; int x = ( ( ( r % m ) * ( ( r + 1 ) % m ) ) / 2 ) % m ; int y = ( ( ( l % m ) * ( ( l - 1 ) % m ) ) / 2 ) % m ; int p = ( int ) ( ( n / l ) % m ) ; s = ( s + ( ( ( x - y ) % m ) * p ) % m + m ) % m ; s %= m ; l = r + 1 ; } System . out . print ( ( s + m ) % m ) ; } public static void main ( String [ ] args ) { long n = 12 ; solve ( n ) ; } }
import java . util . * ; class GFG { static void solve ( int n , int k ) { int x1 , d ; d = k * ( k + 1 ) ; if ( ( 2 * n ) % d != 0 ) { System . out . print ( " - 1" ) ; return ; } x1 = 2 * n / d ; for ( int i = 1 ; i <= k ; i ++ ) { System . out . print ( x1 * i + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 4 ; solve ( n , k ) ; } }
class GFG { static void solve ( int N ) { if ( N % 2 == 0 ) { System . out . print ( N / 2 + " ▁ " + N + "NEW_LINE"); } else { System . out . print ( ( N - 1 ) / 2 + " ▁ " + ( N - 1 ) + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 10 ; solve ( N ) ; } }
import java . util . * ; class GFG { static void findArray ( int [ ] a , int n , int P ) { int mi = Arrays . stream ( a ) . min ( ) . getAsInt ( ) ; int ctr = 0 ; mi = Math . max ( 0 , mi - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -= mi ; ctr += mi ; } int i = P - 1 ; int start = - 1 ; while ( true ) { if ( a [ i ] == 0 ) { start = i ; break ; } a [ i ] -= 1 ; ctr += 1 ; i = ( i - 1 + n ) % n ; } a [ start ] = ctr ; for ( i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int P = 2 ; int arr [ ] = { 3 , 2 , 0 , 2 , 7 } ; findArray ( arr , N , P ) ; } }
import java . lang . * ; class GFG { static int min_time_to_cut ( int N ) { if ( N == 0 ) return 0 ; return ( int ) Math . ceil ( Math . log ( N ) / Math . log ( 2 ) ) ; } public static void main ( String [ ] args ) { int N = 100 ; System . out . print ( min_time_to_cut ( N ) ) ; } }
class GFG { static int k1 = 2 ; static int k2 = 2 ; static int gap = 5 ; public static int printPattern ( int i , int j , int n ) { if ( j >= n ) { k1 = 2 ; k2 = 2 ; k1 -- ; k2 ++ ; if ( i == 2 ) { k1 = 0 ; k2 = n - 1 ; } return 0 ; } if ( i >= 3 ) { return 1 ; } if ( j > k2 ) { k1 += gap ; k2 += gap ; } if ( j >= k1 && j <= k2 i == 2 ) { System . out . print ( " * " ) ; } else { System . out . print ( " ▁ " ) ; } if ( printPattern ( i , j + 1 , n ) == 1 ) { return 1 ; } System . out . println ( ) ; return printPattern ( i + 1 , 0 , n ) ; } public static void main ( String [ ] args ) { int N = 3 ; printPattern ( 0 , 0 , N * 5 ) ; } }
import java . util . * ; class GFG { static void checkstateA ( String n ) { if ( n . charAt ( 0 ) == '0' ) stateB ( n . substring ( 1 ) ) ; else stateD ( n . substring ( 1 ) ) ; } static void stateB ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateC ( n . substring ( 1 ) ) ; else stateD ( n . substring ( 1 ) ) ; } } static void stateC ( String n ) { System . out . println ( " String ▁ accepted " ) ; } static void stateD ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateD ( n . substring ( 1 ) ) ; else stateE ( n . substring ( 1 ) ) ; } } static void stateE ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ not ▁ accepted " ) ; else { if ( n . charAt ( 0 ) == '0' ) stateE ( n . substring ( 1 ) ) ; else stateF ( n . substring ( 1 ) ) ; } } static void stateF ( String n ) { if ( n . length ( ) == 0 ) System . out . println ( " string ▁ accepred " ) ; else { if ( n . charAt ( 0 ) == '1' ) stateD ( n . substring ( 1 ) ) ; else stateE ( n . substring ( 1 ) ) ; } } public static void main ( String args [ ] ) { String n = "0100101" ; checkstateA ( n ) ; } }
import java . util . * ; class GFG { public static int HogbenNumber ( int a ) { int p = ( int ) ( Math . pow ( a , 2 ) - a + 1 ) ; return p ; } public static void main ( String args [ ] ) { int N = 10 ; System . out . print ( HogbenNumber ( N ) ) ; } }
import java . util . * ; class GFG { static int findDistinctSums ( int n ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j ++ ) { s . add ( i + j ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( findDistinctSums ( N ) ) ; } }
class GFG { public static void check_string_exist ( String S ) { int size = S . length ( ) ; boolean check = true ; for ( int i = 0 ; i < size ; i ++ ) { if ( S . charAt ( i ) != S . charAt ( ( i + 2 ) % size ) ) { check = false ; break ; } } if ( check ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " papa " ; check_string_exist ( S ) ; } }
class GFG { static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static int productinRange ( int l , int r ) { if ( r - l > 9 ) return 0 ; else { int p = 1 ; for ( int i = l ; i <= r ; i ++ ) p *= getProduct ( i ) ; return p ; } } public static void main ( String [ ] args ) { int l = 11 , r = 15 ; System . out . print ( productinRange ( l , r ) + "NEW_LINE"); l = 1 ; r = 15 ; System . out . print ( productinRange ( l , r ) ) ; } }
class GFG { static int nthTerm ( int N ) { if ( N == 1 ) { return 2 ; } return ( ( N - 1 ) * 13 ) + nthTerm ( N - 1 ) ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . print ( nthTerm ( N ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void maximumSubsequence ( int arr [ ] , int N ) { HashMap < Integer , Integer > M = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( M . containsKey ( arr [ i ] ) ) { M . put ( arr [ i ] , M . get ( arr [ i ] ) + 1 ) ; } else { M . put ( arr [ i ] , 1 ) ; } } int distinct_size = M . size ( ) ; int maxFreq = 1 ; for ( Map . Entry < Integer , Integer > it : M . entrySet ( ) ) { maxFreq = Math . max ( maxFreq , it . getValue ( ) ) ; } System . out . print ( Math . max ( Math . min ( distinct_size , maxFreq - 1 ) , Math . min ( distinct_size - 1 , maxFreq ) ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 4 , 4 , 4 , 4 , 5 } ; int N = arr . length ; maximumSubsequence ( arr , N ) ; } }
class GFG { static void arrayConstruct ( int N ) { for ( int i = 2 ; i <= N ; i = i + 2 ) System . out . print ( i + " ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i = i + 2 ) System . out . print ( i + " ▁ " ) ; System . out . print ( N - 1 + ( N / 2 ) + "NEW_LINE"); } static void createArray ( int N ) { if ( N % 4 == 0 ) arrayConstruct ( N ) ; else System . out . print ( - 1 + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 8 ; createArray ( N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int distinctSubstring ( String P , String Q , int K , int N ) { Set < String > S = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { int sum = 0 ; String s = " " ; for ( int j = i ; j < N ; ++ j ) { int pos = P . charAt ( j ) - ' a ' ; sum += Q . charAt ( pos ) - '0' ; s += P . charAt ( j ) ; if ( sum <= K ) { S . add ( s ) ; } else { break ; } } } return S . size ( ) ; } public static void main ( String args [ ] ) { String S = " abcde " ; String W = "12345678912345678912345678" ; int K = 5 ; int N = S . length ( ) ; System . out . println ( distinctSubstring ( S , W , K , N ) ) ; } }
import java . util . * ; class GFG { static int distinctSubString ( String P , String Q , int K , int N ) { HashSet < String > S = new HashSet < String > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { int sum = 0 ; String s = " " ; for ( int j = i ; j < N ; ++ j ) { int pos = P . charAt ( j ) - ' a ' ; sum += Q . charAt ( pos ) - '0' ; s += P . charAt ( j ) ; if ( sum <= K ) { S . add ( s ) ; } else { break ; } } } return S . size ( ) ; } public static void main ( String [ ] args ) { String P = " abcde " ; String Q = "12345678912345678912345678" ; int K = 5 ; int N = P . length ( ) ; System . out . print ( distinctSubString ( P , Q , K , N ) ) ; } }
import java . util . * ; class GFG { static void printPattern ( int n ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j < 2 * n ; j ++ ) { if ( j == ( n - i + 1 ) || j == ( n + i - 1 ) ) { System . out . print ( " * ▁ " ) ; } else if ( ( i >= 4 && i <= n - 4 ) && ( j == n - i + 4 j == n + i - 4 ) ) { System . out . print ( " * ▁ " ) ; } else if ( i == n || ( i == n - 4 && j >= n - ( n - 2 * 4 ) && j <= n + n - 2 * 4 ) ) { System . out . print ( " * ▁ " ) ; } else { System . out . print ( " ▁ " + " ▁ " ) ; } } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 9 ; printPattern ( N ) ; } }
class GFG { static boolean isKAlternating ( String s , int k ) { if ( s . length ( ) < k ) return false ; int checker = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int bitAtIndex = s . charAt ( i ) - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( int i = k ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i - k ) != s . charAt ( i ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " acdeac " ; int K = 4 ; if ( isKAlternating ( str , K ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
import java . util . * ; class GFG { static int MAX = 100000 ; static void findFactors ( int N ) { int temp = ( int ) Math . sqrt ( N ) ; int [ ] factor = new int [ MAX ] ; Arrays . fill ( factor , 1 ) ; int i , j , k ; int len1 = 1 ; while ( temp % 2 == 0 ) { factor [ len1 ++ ] = 2 ; factor [ len1 ++ ] = 2 ; temp /= 2 ; } for ( j = 3 ; j < Math . sqrt ( temp ) ; j += 2 ) { while ( temp % j == 0 ) { factor [ len1 ++ ] = j ; factor [ len1 ++ ] = j ; temp /= j ; } } if ( temp > 2 ) { factor [ len1 ++ ] = temp ; factor [ len1 ++ ] = temp ; } int [ ] [ ] M = new int [ len1 ] [ MAX ] ; int tpc = 0 , tpr = 0 ; M [ 0 ] [ 0 ] = 1 ; j = 1 ; while ( j < len1 ) { if ( factor [ j ] != factor [ j - 1 ] ) { tpr ++ ; M [ tpr ] [ 0 ] = factor [ j ] ; j ++ ; tpc = 1 ; } else { M [ tpr ] [ tpc ] = M [ tpr ] [ tpc - 1 ] * factor [ j ] ; j ++ ; tpc ++ ; } } int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 , l2 ; l1 = l2 = 1 ; arr1 [ 0 ] = arr2 [ 0 ] = 1 ; for ( i = 1 ; i < tpr + 1 ; i ++ ) { for ( j = 0 ; M [ i ] [ j ] != 0 ; j ++ ) { for ( k = 0 ; k < l1 ; k ++ ) { arr2 [ l2 ++ ] = arr1 [ k ] * M [ i ] [ j ] ; } } for ( j = l1 ; j < l2 ; j ++ ) { arr1 [ j ] = arr2 [ j ] ; } l1 = l2 ; } for ( i = 0 ; i < l2 ; i ++ ) { System . out . print ( arr2 [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 900 ; findFactors ( N ) ; } }
class GFG { static int matrix [ ] [ ] = new int [ 100 ] [ 100 ] ; static void printRequiredMatrix ( int n ) { if ( n == 1 ) { System . out . println ( "1" ) ; } else if ( n % 2 != 0 ) { System . out . println ( " - 1" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i ] [ i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i ] [ u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) { int temp = a ; a = b ; b = temp ; } matrix [ b ] [ a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; } }
class GFG { static int countSubstring ( String S , int n ) { int ans = 0 ; int i = 0 ; while ( i < n ) { int cnt0 = 0 , cnt1 = 0 ; if ( S . charAt ( i ) == '0' ) { while ( i < n && S . charAt ( i ) == '0' ) { cnt0 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '1' ) { cnt1 ++ ; j ++ ; } } else { while ( i < n && S . charAt ( i ) == '1' ) { cnt1 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '0' ) { cnt0 ++ ; j ++ ; } } ans += Math . min ( cnt0 , cnt1 ) ; } return ans ; } static public void main ( String args [ ] ) { String S = "0001110010" ; int n = S . length ( ) ; System . out . println ( countSubstring ( S , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void decToOctal ( int n ) { int octalNum [ ] = new int [ 100 ] ; int i = 0 ; while ( n != 0 ) { octalNum [ i ] = n % 8 ; n = n / 8 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) System . out . print ( octalNum [ j ] ) ; System . out . println ( "NEW_LINE"); } static void nDigitPerfectSquares ( int n ) { int decimal = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 8 , n ) ) ) - 1 , 2 ) ; decToOctal ( decimal ) ; } public static void main ( String [ ] args ) { int n = 2 ; nDigitPerfectSquares ( n ) ; } }
import java . io . * ; import java . util . * ; class GfG { private static int [ ] zArray ( int arr [ ] ) { int z [ ] ; int n = arr . length ; z = new int [ n ] ; int r = 0 , l = 0 ; for ( int k = 1 ; k < n ; k ++ ) { if ( k > r ) { r = l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } else { int k1 = k - l ; if ( z [ k1 ] < r - k + 1 ) z [ k ] = z [ k1 ] ; else { l = k ; while ( r < n && arr [ r ] == arr [ r - l ] ) r ++ ; z [ k ] = r - l ; r -- ; } } } return z ; } private static int [ ] mergeArray ( int A [ ] , int B [ ] ) { int n = A . length ; int m = B . length ; int z [ ] ; int c [ ] = new int [ n + m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) c [ i ] = B [ i ] ; c [ m ] = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) c [ m + i + 1 ] = A [ i ] ; z = zArray ( c ) ; return z ; } private static void findZArray ( int A [ ] , int B [ ] , int n ) { int flag = 0 ; int z [ ] ; z = mergeArray ( A , B ) ; for ( int i = 0 ; i < z . length ; i ++ ) { if ( z [ i ] == n ) { System . out . print ( ( i - n - 1 ) + " ▁ " ) ; flag = 1 ; } } if ( flag == 0 ) { System . out . println ( " Not ▁ Found " ) ; } } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 3 , 2 , 3 , 2 } ; int B [ ] = { 2 , 3 } ; int n = B . length ; findZArray ( A , B , n ) ; } }
import java . util . * ; class GFG { static boolean Formatted ( String s ) { Vector < Character > k = new Vector < Character > ( ) ; int i = 0 ; while ( i < s . length ( ) ) { if ( s . charAt ( i ) == ' ) ' || s . charAt ( i ) == ' ( ' ) { k . add ( s . charAt ( i ) ) ; i += 1 ; } else { char st = 0 ; while ( s . charAt ( i ) != ' ) ' && s . charAt ( i ) != ' ) ' ) { st = s . charAt ( i ) ; i = i + 1 ; } k . add ( st ) ; } } int depth = 0 , flag = 1 ; int open = 0 , close = 0 ; for ( char i2 : k ) { if ( i2 == ' ( ' ) { depth += 1 ; open += 1 ; } else if ( i2 == ' ) ' ) { depth -= 1 ; close += 1 ; } else { if ( i2 - '0' != depth ) { flag = 0 ; break ; } } } if ( open != close ) flag = 0 ; return ( flag == 1 ) ? true : false ; } public static void main ( String [ ] args ) { String s = " ( (2 ) ( (3 ) ) ) " ; boolean k = Formatted ( s ) ; if ( k == true ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
class GFG { static int findSum ( int [ ] arr , int n , int k ) { if ( k <= n - 2 ) return ( arr [ n - 1 ] - arr [ 0 ] ) ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 1 ; System . out . println ( findSum ( arr , n , k ) ) ; } }
class GFG { static int maxSubStr ( String str , int n ) { int count0 = 0 , count1 = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { count0 ++ ; } else { count1 ++ ; } if ( count0 == count1 ) { cnt ++ ; } } if ( cnt == 0 ) { return - 1 ; } return cnt ; } public static void main ( String [ ] args ) { String str = "0100110101" ; int n = str . length ( ) ; System . out . println ( maxSubStr ( str , n ) ) ; } }
class GFG { static long squareDigitSum ( String s ) { long lengthN = s . length ( ) ; long result = ( lengthN / 9 ) * 81 + ( long ) Math . pow ( ( lengthN % 9 ) , 2 ) ; return result ; } public static void main ( String [ ] args ) { String s = "1111" ; System . out . println ( squareDigitSum ( s ) ) ; } }
class temp { static String getString ( char x ) { String s = String . valueOf ( x ) ; return s ; } static boolean solve ( String s1 , String s2 ) { String a = getString ( s1 . charAt ( 0 ) ) , b = getString ( s2 . charAt ( 0 ) ) ; for ( int i = 1 ; i < s1 . length ( ) ; i ++ ) if ( s1 . charAt ( i ) != s1 . charAt ( i - 1 ) ) { a += getString ( s1 . charAt ( i ) ) ; } for ( int i = 1 ; i < s2 . length ( ) ; i ++ ) if ( s2 . charAt ( i ) != s2 . charAt ( i - 1 ) ) { b += getString ( s2 . charAt ( i ) ) ; } if ( a . equals ( b ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = " Geeks " , s2 = " Geks " ; if ( solve ( s1 , s2 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int x = 26 ; static int mod = 3001 ; static void CntSubstr ( char [ ] s , int l ) { int hash = 0 ; for ( int i = 0 ; i < l ; i ++ ) { hash = ( hash * x + ( s [ i ] - 97 ) ) % mod ; } int pow_l = 1 ; for ( int i = 0 ; i < l - 1 ; i ++ ) { pow_l = ( pow_l * x ) % mod ; } HashSet < Integer > result = new HashSet < Integer > ( ) ; result . add ( hash ) ; for ( int i = l ; i < s . length ; i ++ ) { hash = ( ( hash - pow_l * ( s [ i - l ] - 97 ) + 2 * mod ) * x + ( s [ i ] - 97 ) ) % mod ; result . add ( hash ) ; } System . out . println ( result . size ( ) ) ; } public static void main ( String [ ] args ) { String s = " abcba " ; int l = 2 ; CntSubstr ( s . toCharArray ( ) , l ) ; } }
class GFG { private static boolean isValidString ( String str , int n ) { int index = str . indexOf ( " b " ) ; if ( index == 0 ) return false ; while ( index != - 1 ) { if ( str . charAt ( index - 1 ) != ' a ' ) return false ; if ( index + 1 < n && str . charAt ( index + 1 ) != ' b ' ) return false ; if ( index + 2 < n && str . charAt ( index + 2 ) == ' b ' ) return false ; if ( index == n - 1 ) return false ; index = str . indexOf ( " b " , index + 2 ) ; } return true ; } public static void main ( String [ ] args ) { String str = " abbaaabbabba " ; int n = str . length ( ) ; System . out . println ( isValidString ( str , n ) ) ; } }
class GfG { static boolean endsWith ( String str , String pat ) { int patLen = pat . length ( ) ; int strLen = str . length ( ) ; if ( patLen > strLen ) return false ; patLen -- ; strLen -- ; while ( patLen >= 0 ) { if ( pat . charAt ( patLen ) != str . charAt ( strLen ) ) return false ; patLen -- ; strLen -- ; } return true ; } static int countOfStrings ( String pat , int n , String sArr [ ] ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( endsWith ( sArr [ i ] , pat ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String pat = " ks " ; int n = 4 ; String sArr [ ] = { " geeks " , " geeksforgeeks " , " games " , " unit " } ; System . out . println ( countOfStrings ( pat , n , sArr ) ) ; } }
class GfG { static int getCount ( String a , String b ) { if ( b . length ( ) % a . length ( ) != 0 ) return - 1 ; int count = b . length ( ) / a . length ( ) ; String str = " " ; for ( int i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str . equals ( b ) ) return count ; return - 1 ; } public static void main ( String [ ] args ) { String a = " geeks " ; String b = " geeksgeeks " ; System . out . println ( getCount ( a , b ) ) ; } }
import java . io . * ; class GFG { static long getSum ( long n , int d ) { if ( n < d ) return 0 ; while ( n % 10 != d ) n -- ; long k = n / 10 ; return ( k + 1 ) * d + ( k * 10 + 10 * k * k ) / 2 ; } public static void main ( String [ ] args ) { long n = 30 ; int d = 3 ; System . out . println ( getSum ( n , d ) ) ; } }
import java . util . * ; public class GFG { static HashMap < Integer , Integer > getFrequencyMap ( int arr [ ] ) { HashMap < Integer , Integer > hashMap = new HashMap < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( hashMap . containsKey ( arr [ i ] ) ) { hashMap . put ( arr [ i ] , hashMap . get ( arr [ i ] ) + 1 ) ; } else { hashMap . put ( arr [ i ] , 1 ) ; } } return hashMap ; } static boolean hasDigit ( HashMap < Integer , Integer > hashMap , int digit ) { if ( hashMap . containsKey ( digit ) && hashMap . get ( digit ) > 0 ) { hashMap . put ( digit , hashMap . get ( digit ) - 1 ) ; return true ; } return false ; } static String getMaxTime ( int arr [ ] ) { HashMap < Integer , Integer > hashMap = getFrequencyMap ( arr ) ; int i ; boolean flag ; String time = " " ; flag = false ; for ( i = 2 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } flag = false ; if ( time . charAt ( 0 ) == '2' ) { for ( i = 3 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } else { for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } } if ( ! flag ) { return " - 1" ; } time += " : " ; flag = false ; for ( i = 5 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } flag = false ; for ( i = 9 ; i >= 0 ; i -- ) { if ( hasDigit ( hashMap , i ) ) { flag = true ; time += i ; break ; } } if ( ! flag ) { return " - 1" ; } return time ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 9 } ; System . out . println ( getMaxTime ( arr ) ) ; } }
import java . io . * ; class GFG { static int replace ( String A , String B ) { int n = A . length ( ) , m = B . length ( ) ; int count = 0 , i , j ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < m ; j ++ ) { if ( i + j >= n ) break ; else if ( A . charAt ( i + j ) != B . charAt ( j ) ) break ; } if ( j == m ) { count ++ ; i += m - 1 ; } } return count ; } public static void main ( String args [ ] ) { String str1 = " aaaaaaaa " ; String str2 = " aaa " ; System . out . println ( replace ( str1 , str2 ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static final int n = 6 ; static final int m = 8 ; static final int visited [ ] [ ] = new int [ n ] [ m ] ; static final int result [ ] [ ] = new int [ n ] [ m ] ; static int COUNT ; static boolean is_valid ( int x , int y , int key , int input [ ] [ ] ) { if ( x < n && y < m && x >= 0 && y >= 0 ) { if ( visited [ x ] [ y ] == 0 && input [ x ] [ y ] == key ) return true ; else return false ; } else return false ; } static void BFS ( int x , int y , int i , int j , int input [ ] [ ] ) { if ( x != y ) return ; visited [ i ] [ j ] = 1 ; COUNT ++ ; int x_move [ ] = { 0 , 0 , 1 , - 1 } ; int y_move [ ] = { 1 , - 1 , 0 , 0 } ; for ( int u = 0 ; u < 4 ; u ++ ) if ( ( is_valid ( i + y_move [ u ] , j + x_move [ u ] , x , input ) ) == true ) BFS ( x , y , i + y_move [ u ] , j + x_move [ u ] , input ) ; } static void reset_visited ( ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) visited [ i ] [ j ] = 0 ; } static void reset_result ( int key , int input [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( visited [ i ] [ j ] == 1 && input [ i ] [ j ] == key ) result [ i ] [ j ] = visited [ i ] [ j ] ; else result [ i ] [ j ] = 0 ; } } } static void print_result ( int res ) { System . out . println ( " The ▁ largest ▁ connected ▁ " + " component ▁ of ▁ the ▁ grid ▁ is ▁ : " + res ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( result [ i ] [ j ] != 0 ) System . out . print ( result [ i ] [ j ] + " ▁ " ) ; else System . out . print ( " . ▁ " ) ; } System . out . println ( ) ; } } static void computeLargestConnectedGrid ( int input [ ] [ ] ) { int current_max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { reset_visited ( ) ; COUNT = 0 ; if ( j + 1 < m ) BFS ( input [ i ] [ j ] , input [ i ] [ j + 1 ] , i , j , input ) ; if ( COUNT >= current_max ) { current_max = COUNT ; reset_result ( input [ i ] [ j ] , input ) ; } reset_visited ( ) ; COUNT = 0 ; if ( i + 1 < n ) BFS ( input [ i ] [ j ] , input [ i + 1 ] [ j ] , i , j , input ) ; if ( COUNT >= current_max ) { current_max = COUNT ; reset_result ( input [ i ] [ j ] , input ) ; } } } print_result ( current_max ) ; } public static void main ( String args [ ] ) { int input [ ] [ ] = { { 1 , 4 , 4 , 4 , 4 , 3 , 3 , 1 } , { 2 , 1 , 1 , 4 , 3 , 3 , 1 , 1 } , { 3 , 2 , 1 , 1 , 2 , 3 , 2 , 1 } , { 3 , 3 , 2 , 1 , 2 , 2 , 2 , 2 } , { 3 , 1 , 3 , 1 , 1 , 4 , 4 , 4 } , { 1 , 1 , 3 , 1 , 1 , 4 , 4 , 4 } } ; computeLargestConnectedGrid ( input ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void split ( String str ) { int len = str . length ( ) ; if ( len == 1 ) { System . out . println ( " Not ▁ Possible " ) ; return ; } String s1 = " " , s2 = " " ; long num1 , num2 ; for ( int i = 0 ; i <= len / 2 ; i ++ ) { int flag = 0 ; s1 = str . substring ( 0 , i + 1 ) ; num1 = Long . parseLong ( ( s1 ) ) ; num2 = num1 + 1 ; s2 = Long . toString ( num2 ) ; int k = i + 1 ; while ( flag == 0 ) { int l = s2 . length ( ) ; if ( k + l > len ) { flag = 1 ; break ; } if ( ( str . substring ( k , k + l ) . equals ( s2 ) ) ) { flag = 0 ; num2 ++ ; k = k + l ; if ( k == len ) break ; s2 = Long . toString ( num2 ) ; l = s2 . length ( ) ; if ( k + 1 > len ) { flag = 1 ; break ; } } else flag = 1 ; } if ( flag == 0 ) { System . out . println ( " Possible " + " ▁ " + s1 ) ; break ; } else if ( flag == 1 && i > len / 2 - 1 ) { System . out . println ( " Not ▁ Possible " ) ; break ; } } } public static void main ( String args [ ] ) { Scanner in = new Scanner ( System . in ) ; String str = "99100" ; split ( str ) ; } }
import java . util . * ; class GFG { static int internalSearch ( String needle , int row , int col , String hay [ ] , int row_max , int col_max , int xx ) { int found = 0 ; if ( row >= 0 && row <= row_max && col >= 0 && col <= col_max && xx < needle . length ( ) && needle . charAt ( xx ) == hay [ row ] . charAt ( col ) ) { char match = needle . charAt ( xx ) ; xx += 1 ; hay [ row ] = hay [ row ] . substring ( 0 , col ) + "0" + hay [ row ] . substring ( col + 1 ) ; if ( xx == needle . length ( ) ) { found = 1 ; } else { found += internalSearch ( needle , row , col + 1 , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row , col - 1 , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row + 1 , col , hay , row_max , col_max , xx ) ; found += internalSearch ( needle , row - 1 , col , hay , row_max , col_max , xx ) ; } hay [ row ] = hay [ row ] . substring ( 0 , col ) + match + hay [ row ] . substring ( col + 1 ) ; } return found ; } static int searchString ( String needle , int row , int col , String str [ ] , int row_count , int col_count ) { int found = 0 ; int r , c ; for ( r = 0 ; r < row_count ; ++ r ) { for ( c = 0 ; c < col_count ; ++ c ) { found += internalSearch ( needle , r , c , str , row_count - 1 , col_count - 1 , 0 ) ; } } return found ; } public static void main ( String args [ ] ) { String needle = " MAGIC " ; String input [ ] = { " BBABBM " , " CBMBBA " , " IBABBG " , " GOZBBI " , " ABBBBC " , " MCIGAM " } ; String str [ ] = new String [ input . length ] ; int i ; for ( i = 0 ; i < input . length ; ++ i ) { str [ i ] = input [ i ] ; } System . out . println ( " count : ▁ " + searchString ( needle , 0 , 0 , str , str . length , str [ 0 ] . length ( ) ) ) ; } }
class GFG { static void KMP ( int m , int n , String str2 , String str1 ) { int pos = 0 , len = 0 ; int [ ] p = new int [ m + 1 ] ; int k = 0 ; char [ ] ch1 = str1 . toCharArray ( ) ; char [ ] ch2 = str2 . toCharArray ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && ch1 [ k ] != ch1 [ i - 1 ] ) k = p [ k ] ; if ( ch1 [ k ] == ch1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( int j = 0 , i = 0 ; i < m ; i ++ ) { while ( j > 0 && j < n && ch1 [ j ] != ch2 [ i ] ) j = p [ j ] ; if ( j < n && ch1 [ j ] == ch2 [ i ] ) j ++ ; if ( j > len ) { len = j ; pos = i - j + 1 ; } } System . out . println ( " Shift ▁ = ▁ " + pos ) ; System . out . println ( " Prefix ▁ = ▁ " + str1 . substring ( 0 , len ) ) ; } public static void main ( String [ ] args ) { String str1 = " geeksforgeeks " ; String str2 = " forgeeksgeeks " ; int n = str1 . length ( ) ; str2 = str2 + str2 ; KMP ( 2 * n , n , str2 , str1 ) ; } }
public class GFG { static int countPattern ( String str ) { int len = str . length ( ) ; boolean oneSeen = false ; for ( int i = 0 ; i < len ; i ++ ) { char getChar = str . charAt ( i ) ; if ( getChar == '1' && oneSeen == true ) { if ( str . charAt ( i - 1 ) == '0' ) count ++ ; } if ( getChar == '1' && oneSeen == false ) oneSeen = true ; if ( getChar != '0' && str . charAt ( i ) != '1' ) oneSeen = false ; } return count ; } public static void main ( String [ ] args ) { String str = "100001abc101" ; System . out . println ( countPattern ( str ) ) ; } }
class GFG { static void preprocess_strong_suffix ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i = m , j = m + 1 ; bpos [ i ] = j ; while ( i > 0 ) { while ( j <= m && pat [ i - 1 ] != pat [ j - 1 ] ) { if ( shift [ j ] == 0 ) shift [ j ] = j - i ; j = bpos [ j ] ; } i -- ; j -- ; bpos [ i ] = j ; } } static void preprocess_case2 ( int [ ] shift , int [ ] bpos , char [ ] pat , int m ) { int i , j ; j = bpos [ 0 ] ; for ( i = 0 ; i <= m ; i ++ ) { if ( shift [ i ] == 0 ) shift [ i ] = j ; if ( i == j ) j = bpos [ j ] ; } } static void search ( char [ ] text , char [ ] pat ) { int s = 0 , j ; int m = pat . length ; int n = text . length ; int [ ] bpos = new int [ m + 1 ] ; int [ ] shift = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) shift [ i ] = 0 ; preprocess_strong_suffix ( shift , bpos , pat , m ) ; preprocess_case2 ( shift , bpos , pat , m ) ; while ( s <= n - m ) { j = m - 1 ; while ( j >= 0 && pat [ j ] == text [ s + j ] ) j -- ; if ( j < 0 ) { System . out . printf ( "pattern occurs at shift = %dNEW_LINE", s); s += shift [ 0 ] ; } else s += shift [ j + 1 ] ; } } public static void main ( String [ ] args ) { char [ ] text = " ABAAAABAACD " . toCharArray ( ) ; char [ ] pat = " ABA " . toCharArray ( ) ; search ( text , pat ) ; } }
public class GFG { static int maxPrefix ( String s , String t ) { int count = 0 ; for ( int i = 0 ; i < t . length ( ) ; i ++ ) { if ( count == s . length ( ) ) break ; if ( t . charAt ( i ) == s . charAt ( count ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " digger " ; String T = " biggerdiagram " ; System . out . println ( maxPrefix ( S , T ) ) ; } }
import java . io . * ; class GFG { static void translate ( char str [ ] ) { for ( int i = 1 ; i < str . length ; i ++ ) { if ( str [ i - 1 ] == ' A ' && str [ i ] == ' B ' ) { str [ i - 1 ] = ' C ' ; int j ; for ( j = i ; j < str . length - 1 ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j ] = ' ▁ ' ; } } return ; } public static void main ( String args [ ] ) { String st = " helloABworldABGfG " ; char str [ ] = st . toCharArray ( ) ; translate ( str ) ; System . out . println ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; System . out . println ( str ) ; } }
import java . util . * ; class GFG { static final int ROW = 3 ; static final int COL = 5 ; static boolean isvalid ( int row , int col , int prevRow , int prevCol ) { return ( row >= 0 ) && ( row < ROW ) && ( col >= 0 ) && ( col < COL ) && ! ( row == prevRow && col == prevCol ) ; } static int rowNum [ ] = { - 1 , - 1 , - 1 , 0 , 0 , 1 , 1 , 1 } ; static int colNum [ ] = { - 1 , 0 , 1 , - 1 , 1 , - 1 , 0 , 1 } ; static void DFS ( char mat [ ] [ ] , int row , int col , int prevRow , int prevCol , char [ ] word , String path , int index , int n ) { if ( index > n mat [ row ] [ col ] != word [ index ] ) return ; path += ( word [ index ] ) + " ( " + String . valueOf ( row ) + " , ▁ " + String . valueOf ( col ) + " ) ▁ " ; if ( index == n ) { System . out . print ( path + "NEW_LINE"); return ; } for ( int k = 0 ; k < 8 ; ++ k ) if ( isvalid ( row + rowNum [ k ] , col + colNum [ k ] , prevRow , prevCol ) ) DFS ( mat , row + rowNum [ k ] , col + colNum [ k ] , row , col , word , path , index + 1 , n ) ; } static void findWords ( char mat [ ] [ ] , char [ ] word , int n ) { for ( int i = 0 ; i < ROW ; ++ i ) for ( int j = 0 ; j < COL ; ++ j ) if ( mat [ i ] [ j ] == word [ 0 ] ) DFS ( mat , i , j , - 1 , - 1 , word , " " , 0 , n ) ; } public static void main ( String [ ] args ) { char mat [ ] [ ] = { { ' B ' , ' N ' , ' E ' , ' Y ' , ' S ' } , { ' H ' , ' E ' , ' D ' , ' E ' , ' S ' } , { ' S ' , ' G ' , ' N ' , ' D ' , ' E ' } } ; char [ ] word = " DES " . toCharArray ( ) ; findWords ( mat , word , word . length - 1 ) ; } }
class GFG { public static String generateNumber ( String number ) { int temp = 0 , adding_number = 0 ; String result = " " ; int len = number . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { int digit = ( int ) number . charAt ( i ) - ( int ) '0' ; if ( temp % 2 == 0 ) { adding_number += 1 ; digit += adding_number ; if ( digit >= 10 ) { digit %= 9 ; if ( digit == 0 ) digit = 9 ; } } result = digit + result ; temp += 1 ; } return result ; } public static void main ( String args [ ] ) { String S = "1345" ; System . out . println ( generateNumber ( S ) ) ; } }
class GFG { public static int findK ( String s , char c ) { int n = s . length ( ) ; int diff ; int max = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == c ) { diff = i - prev ; prev = i ; if ( diff > max ) { max = diff ; } } } if ( max == 0 ) return - 1 ; return max ; } public static void main ( String args [ ] ) { String S = " abdegb " ; char ch = ' b ' ; System . out . println ( findK ( S , ch ) ) ; } }
import java . io . * ; class GFG { static int countSubsequence ( String S , int N , int K ) { int C = 0 , C1 = 0 , C0 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) C1 ++ ; else if ( S . charAt ( i ) == '0' ) C0 ++ ; } int B1 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == '1' ) B1 ++ ; else if ( S . charAt ( i ) == '0' ) C = C + ( C1 - B1 ) ; } int ans = C * K ; ans += ( C1 * C0 * ( ( ( K ) * ( K - 1 ) ) / 2 ) ) ; return ans ; } public static void main ( String [ ] args ) { String S = "230013110087" ; int K = 2 ; int N = S . length ( ) ; System . out . println ( countSubsequence ( S , N , K ) ) ; } }
import java . util . * ; class GFG { static int minSwaps ( String s ) { int ones = 0 , zeros = 0 ; int N = s . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones ++ ; else zeros ++ ; } if ( ( N % 2 == 0 && ones != zeros ) || ( N % 2 == 1 && Math . abs ( ones - zeros ) != 1 ) ) { return - 1 ; } int ans_1 = 0 ; int j = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '1' ) { ans_1 += Math . abs ( j - i ) ; j += 2 ; } } int ans_0 = 0 ; int k = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '0' ) { ans_0 += Math . abs ( k - i ) ; k += 2 ; } } if ( N % 2 == 0 ) return Math . min ( ans_1 , ans_0 ) ; else { if ( ones > zeros ) return ans_1 ; else return ans_0 ; } } public static void main ( String [ ] args ) { String S = "110100" ; System . out . print ( minSwaps ( S ) ) ; } }
import java . util . * ; class GFG { static ArrayList < String > TotalPossibleJumps ( int N ) { if ( ( N - 1 ) == 0 ) { ArrayList < String > newvec = new ArrayList < String > ( ) ; newvec . add ( " " ) ; return newvec ; } else { if ( N < 0 ) { ArrayList < String > newvec = new ArrayList < String > ( ) ; return newvec ; } } ArrayList < String > jump1 = TotalPossibleJumps ( N - 1 ) ; ArrayList < String > jump2 = TotalPossibleJumps ( N - 2 ) ; ArrayList < String > totaljumps = new ArrayList < String > ( ) ; for ( String s : jump1 ) { totaljumps . add ( "1" + s ) ; } for ( String s : jump2 ) { totaljumps . add ( "2" + s ) ; } return totaljumps ; } public static void main ( String [ ] args ) { int N = 3 ; ArrayList < String > Ans = TotalPossibleJumps ( N ) ; for ( String it : Ans ) System . out . println ( it ) ; } }
import java . io . * ; class GFG { static String findString ( String arr [ ] , int N ) { String ans = " " ; for ( int i = 0 ; i < N ; i ++ ) { ans += arr [ i ] . charAt ( i ) == '0' ? '1' : '0' ; } return ans ; } public static void main ( String [ ] args ) { String arr [ ] = { "111" , "011" , "001" } ; int N = arr . length ; System . out . println ( findString ( arr , N ) ) ; } }
public class GFG { static void commonSubstring ( String s ) { int n = s . length ( ) ; int i = 0 ; int j = n - 1 ; String ans = " " ; while ( j >= 0 ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { ans += s . charAt ( i ) ; i ++ ; j -- ; } else { break ; } } if ( ans . length ( ) == 0 ) System . out . println ( " False " ) ; else { System . out . println ( " True ▁ " ) ; System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " abca " ; commonSubstring ( S ) ; } }
import java . io . * ; class GFG { public static void findMinimumSubsequence ( String S ) { boolean flag = false ; StringBuilder dummy = new StringBuilder ( ) ; for ( int j = 0 ; j < S . length ( ) ; j ++ ) { if ( S . charAt ( j ) != '2' && S . charAt ( j ) != '3' && S . charAt ( j ) != '5' && S . charAt ( j ) != '7' ) { System . out . println ( 1 ) ; flag = true ; break ; } } if ( ! flag ) { loop : for ( int j = 0 ; j < S . length ( ) - 1 ; j ++ ) { for ( int j1 = j + 1 ; j1 < S . length ( ) ; j1 ++ ) { dummy = new StringBuilder ( Character . toString ( S . charAt ( j ) ) ) ; dummy . append ( S . charAt ( j1 ) ) ; if ( ! dummy . toString ( ) . equals ( "23" ) && ! dummy . toString ( ) . equals ( "37" ) && ! dummy . toString ( ) . equals ( "53" ) && ! dummy . toString ( ) . equals ( "73" ) ) { System . out . println ( 2 ) ; flag = true ; break loop ; } } } } if ( ! flag ) { if ( S . length ( ) >= 3 ) { System . out . println ( 3 ) ; } else { System . out . println ( - 1 ) ; } } } public static void main ( String [ ] args ) { String S = "237" ; findMinimumSubsequence ( S ) ; } }
import java . io . * ; class GFG { static int [ ] [ ] dp = new int [ 505 ] [ 505 ] ; static int [ ] [ ] choose = new int [ 502 ] [ 502 ] ; static int calc ( int l , int r , String s ) { if ( Math . abs ( r - l ) % 2 == 0 ) { return 0 ; } if ( l > r ) { return dp [ l ] [ r ] = 1 ; } if ( dp [ l ] [ r ] != - 1 ) { return dp [ l ] [ r ] ; } if ( ( r - l ) == 1 ) { if ( s . charAt ( l ) == s . charAt ( r ) ) { dp [ l ] [ r ] = 1 ; } else { dp [ l ] [ r ] = 0 ; } return dp [ l ] [ r ] ; } int ans = 0 ; for ( int k = l + 1 ; k <= r ; k += 2 ) { int temp = 1 ; if ( s . charAt ( l ) == s . charAt ( k ) ) { temp = calc ( l + 1 , k - 1 , s ) * calc ( k + 1 , r , s ) * choose [ ( ( r - l + 1 ) / 2 ) ] [ ( ( r - k ) / 2 ) ] ; ans += temp ; } } return dp [ l ] [ r ] = ans ; } static int waysToClearString ( String S ) { for ( int i = 0 ; i < 505 ; i ++ ) { for ( int j = 0 ; j < 505 ; j ++ ) dp [ i ] [ j ] = - 1 ; } int n = S . length ( ) ; choose [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= ( n / 2 ) ; ++ i ) { choose [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; ++ j ) { choose [ i ] [ j ] = ( choose [ i - 1 ] [ j ] + choose [ i - 1 ] [ j - 1 ] ) ; } } return calc ( 0 , n - 1 , S ) ; } public static void main ( String [ ] args ) { String S = " aabccb " ; System . out . println ( waysToClearString ( S ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int BalancedStringBySwapping ( String s ) { int unbalancedPair = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( unbalancedPair > 0 && s . charAt ( i ) == ' ] ' ) { -- unbalancedPair ; } else if ( s . charAt ( i ) == ' [ ' ) { ++ unbalancedPair ; } } return ( unbalancedPair + 1 ) / 2 ; } public static void main ( String [ ] args ) { String s = " ] ] ] [ [ [ " ; System . out . println ( BalancedStringBySwapping ( s ) ) ; } }
class GFG { static long MOD = 1000000007 ; public static long exp_mod ( long a , long b ) { long ret = 1 ; for ( ; b > 0 ; b >>= 1 , a = a * a % MOD ) { if ( ( b & 1 ) > 0 ) ret = ret * a % MOD ; } return ret ; } public static long countOfWays ( String s , int k ) { int N = s . length ( ) ; long ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '5' || s . charAt ( 0 ) == '0' ) { ans = ( ans + exp_mod ( 2 , i ) ) % MOD ; } } long q = exp_mod ( 2 , N ) ; long qk = exp_mod ( q , k ) ; long inv = exp_mod ( q - 1 , MOD - 2 ) ; ans = ans * ( qk - 1 ) % MOD ; ans = ans * inv % MOD ; return ans ; } public static void main ( String args [ ] ) { String S = "1256" ; int K = 1 ; System . out . println ( countOfWays ( S , K ) ) ; } }
import java . util . * ; class GFG { static void printMiddleString ( String S , String T , int N ) { int [ ] a1 = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { a1 [ i + 1 ] = ( int ) S . charAt ( i ) - 97 + ( int ) T . charAt ( i ) - 97 ; } for ( int i = N ; i >= 1 ; i -- ) { a1 [ i - 1 ] += ( int ) a1 [ i ] / 26 ; a1 [ i ] %= 26 ; } for ( int i = 0 ; i <= N ; i ++ ) { if ( ( a1 [ i ] & 1 ) != 0 ) { if ( i + 1 <= N ) { a1 [ i + 1 ] += 26 ; } } a1 [ i ] = ( int ) a1 [ i ] / 2 ; } for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( ( char ) ( a1 [ i ] + 97 ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; String S = " afogk " ; String T = " asdji " ; printMiddleString ( S , T , N ) ; } }
import java . io . * ; class GFG { static char KthCharacter ( String s , String t , long k ) { long f = 1 ; long ss = 2 ; String tmp = " " ; int len = tmp . length ( ) ; while ( len < k ) { long tf = f ; long ts = ss ; while ( tf -- != 0 ) { tmp += s ; } while ( ts -- != 0 ) { tmp += t ; } f += 2 ; ss += 2 ; len = tmp . length ( ) ; } char output = tmp . charAt ( ( int ) k - 1 ) ; return output ; } public static void main ( String [ ] args ) { String S1 = " a " , S2 = " bc " ; int k = 4 ; char ans = KthCharacter ( S1 , S2 , k ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static void makeContiguous ( String S , int N ) { int fst_occur = 0 , lst_occur = 0 ; for ( int x = 0 ; x < N ; x ++ ) { if ( S . charAt ( x ) == '1' ) { fst_occur = x ; break ; } } for ( int x = N - 1 ; x >= 0 ; x -- ) { if ( S . charAt ( x ) == '1' ) { lst_occur = x ; break ; } } int count = 0 ; for ( int x = fst_occur ; x <= lst_occur ; x ++ ) { if ( S . charAt ( x ) == '0' ) { count ++ ; } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S = "010001011" ; int N = S . length ( ) ; makeContiguous ( S , N ) ; } }
import java . util . * ; class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } static int findComb ( int N , int M ) { int x = fact ( N + 1 ) ; int y = fact ( M ) ; return ( x * y ) ; } public static void main ( String [ ] args ) { int N = 2 ; int M = 2 ; System . out . print ( findComb ( N , M ) ) ; } }
import java . io . * ; class GFG { static void countCamelCase ( String S ) { int count = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) >= 65 && S . charAt ( i ) <= 91 ) { count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String S = " ckjkUUYII " ; countCamelCase ( S ) ; } }
import java . util . * ; class GFG { static int findRepitition ( char a ) { if ( a <= ' z ' && a >= ' a ' ) { return a - ' a ' ; } else if ( a <= ' Z ' && a >= ' A ' ) { return a - ' A ' ; } return 0 ; } static void decodeString ( String str ) { String output = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { output += ( str . charAt ( i ) ) ; i += findRepitition ( str . charAt ( i ) ) ; } System . out . print ( " Decrypted ▁ code ▁ is ▁ { " + output + " } " + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " abbbb ▁ acccdddd " ; decodeString ( str ) ; } }
import java . util . * ; class GFG { static void printString ( String S , int N ) { String [ ] plaintext = new String [ 5 ] ; int freq [ ] = new int [ 26 ] ; int freqSorted [ ] = new int [ 26 ] ; int Used [ ] = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) != ' ▁ ' ) { freq [ S . charAt ( i ) - ' A ' ] ++ ; } } for ( int i = 0 ; i < 26 ; i ++ ) { freqSorted [ i ] = freq [ i ] ; } String T = " ETAOINSHRDLCUMWFGYPBVKJXQZ " ; Arrays . sort ( freqSorted ) ; freqSorted = reverse ( freqSorted ) ; for ( int i = 0 ; i < 5 ; i ++ ) { int ch = - 1 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freqSorted [ i ] == freq [ j ] && Used [ j ] == 0 ) { Used [ j ] = 1 ; ch = j ; break ; } } if ( ch == - 1 ) break ; int x = T . charAt ( i ) - ' A ' ; x = x - ch ; String curr = " " ; for ( int k = 0 ; k < N ; k ++ ) { if ( S . charAt ( k ) == ' ▁ ' ) { curr += ( char ) ' ▁ ' ; continue ; } int y = S . charAt ( k ) - ' A ' ; y += x ; if ( y < 0 ) y += 26 ; if ( y > 25 ) y -= 26 ; curr += ( char ) ( ' A ' + y ) ; } plaintext [ i ] = curr ; } for ( int i = 0 ; i < 5 ; i ++ ) { System . out . print ( plaintext [ i ] + "NEW_LINE"); } } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { String S = " B ▁ TJNQMF ▁ NFTTBHF " ; int N = S . length ( ) ; printString ( S , N ) ; } }
import java . util . * ; class GFG { static String canMadeEmpty ( String s , int n ) { Stack < Character > St = new Stack < Character > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' c ' ) { if ( St . size ( ) >= 2 ) { char b = St . peek ( ) ; St . pop ( ) ; char a = St . peek ( ) ; St . pop ( ) ; if ( a != ' a ' b != ' b ' ) return " No " ; } else return " No " ; } else St . add ( s . charAt ( i ) ) ; } if ( St . size ( ) == 0 ) { return " Yes " ; } else { return " No " ; } } public static void main ( String [ ] args ) { String S = " aabcbc " ; int N = S . length ( ) ; System . out . print ( canMadeEmpty ( S , N ) ) ; } }
class GFG { public static String divisibleByk ( String s , int n , int k ) { int [ ] poweroftwo = new int [ n ] ; poweroftwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { poweroftwo [ i ] = ( poweroftwo [ i - 1 ] * ( 2 % k ) ) % k ; } int rem = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( n - i - 1 ) == '1' ) { rem += ( poweroftwo [ i ] ) ; rem %= k ; } } if ( rem == 0 ) { return " Yes " ; } else return " No " ; } public static void main ( String args [ ] ) { String s = "1010001" ; int k = 9 ; int n = s . length ( ) ; System . out . println ( divisibleByk ( s , n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static String check ( String S ) { int N = S . length ( ) ; Deque < Integer > A = new LinkedList < Integer > ( ) ; Deque < Integer > B = new LinkedList < Integer > ( ) ; Deque < Integer > C = new LinkedList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . charAt ( i ) == ' A ' ) { A . addLast ( i ) ; } else if ( S . charAt ( i ) == ' B ' ) { B . addLast ( i ) ; } else { C . addLast ( i ) ; } } if ( N % 3 > 0 || A . size ( ) != B . size ( ) || A . size ( ) != C . size ( ) ) { return " No " ; } for ( Iterator itr = B . iterator ( ) ; itr . hasNext ( ) ; ) { Integer b = ( Integer ) itr . next ( ) ; if ( A . size ( ) > 0 && b > A . getFirst ( ) ) { A . pop ( ) ; } else { return " No " ; } } for ( Iterator itr = B . descendingIterator ( ) ; itr . hasNext ( ) ; ) { Integer b = ( Integer ) itr . next ( ) ; if ( C . size ( ) > 0 && b < C . getLast ( ) ) { C . pollLast ( ) ; } else { return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { String S = " ABABCC " ; System . out . println ( check ( S ) ) ; } }
import java . util . * ; class GFG { static String isSumEqual ( String A , String B , String C ) { int L = A . length ( ) ; int M = B . length ( ) ; int N = A . length ( ) ; A = reverse ( A ) ; B = reverse ( B ) ; C = reverse ( C ) ; int rem = 0 ; for ( int i = 0 ; i < Math . max ( L , Math . max ( M , N ) ) ; i ++ ) { int curr = rem ; if ( i < L ) curr += A . charAt ( i ) - ' a ' ; if ( i < M ) curr += B . charAt ( i ) - ' a ' ; rem = curr / 10 ; curr %= 10 ; if ( i < N && curr != C . charAt ( i ) - ' a ' ) { return " No " ; } } if ( rem > 0 ) return " No " ; else return " Yes " ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String A = " acb " , B = " cba " , C = " cdb " ; System . out . print ( isSumEqual ( A , B , C ) ) ; } }
public class GFG { static int minimumHammingDistance ( String S , int K ) { int n = S . length ( ) ; int [ ] pref = new int [ n ] ; pref [ 0 ] = S . charAt ( 0 ) - '0' ; for ( int i = 1 ; i < n ; i ++ ) pref [ i ] = pref [ i - 1 ] + ( S . charAt ( i ) - '0' ) ; int cnt = pref [ n - 1 ] ; int ans = cnt ; for ( int i = 0 ; i < n - K ; i ++ ) { int value = pref [ i + K - 1 ] - ( i - 1 >= 0 ? pref [ i - 1 ] : 0 ) ; ans = Math . min ( ans , cnt - value + ( K - value ) ) ; } return ans ; } public static void main ( String args [ ] ) { String s = "101" ; int K = 2 ; System . out . println ( minimumHammingDistance ( s , K ) ) ; } }
import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { static int countWays ( String S , int N ) { int ans = 0 ; for ( int mask = 0 ; mask < ( 1 << N ) ; mask ++ ) { String X = " " , Y = " " ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( mask >> i & 1 ) == 1 ) { X += S . charAt ( i ) ; } else { Y += S . charAt ( i ) ; } } Y = new StringBuilder ( Y ) . reverse ( ) . toString ( ) ; if ( X . equals ( Y ) ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String S = " mippiisssisssiipsspiim " ; int N = S . length ( ) ; System . out . println ( countWays ( S , N ) ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static void differentBitwiseOR ( String s1 , String s2 ) { int n = s1 . length ( ) ; int t00 = 0 , t10 = 0 , t01 = 0 , t11 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '0' ) { t00 ++ ; } if ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) { t10 ++ ; } if ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '1' ) { t11 ++ ; } if ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) { t01 ++ ; } } int ans = t00 * t10 + t01 * t10 + t00 * t11 ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String S1 = "01001" ; String S2 = "11011" ; differentBitwiseOR ( S1 , S2 ) ; } }
import java . io . * ; class GFG { static void RemoveCharacters ( String s ) { String ans = " " ; int [ ] c = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) c [ i ] = 0 ; int pos = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( c [ ( int ) s . charAt ( i ) - 97 ] == 0 ) { c [ ( int ) s . charAt ( i ) - 97 ] = 1 ; pos = i ; ans += s . charAt ( i ) ; } } for ( int i = pos + 1 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( 0 ) ) { ans += s . charAt ( i ) ; break ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " abcaadbcd " ; RemoveCharacters ( S ) ; } }
import java . util . * ; class GFG { static void changeCharacters ( String S , int N , int K ) { int flag = 1 ; int count = 0 ; Stack < Character > st = new Stack < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( st . empty ( ) ) { if ( S . charAt ( i ) == '0' ) { flag = 0 ; break ; } count = 0 ; st . push ( S . charAt ( i ) ) ; } else { if ( S . charAt ( i ) == '0' ) { count ++ ; if ( count == K ) { st . pop ( ) ; count = 0 ; } } else { count = 0 ; } } } if ( flag == 1 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String args [ ] ) { String S = "100100" ; int N = S . length ( ) ; int K = 2 ; changeCharacters ( S , N , K ) ; } }
import java . io . * ; class GFG { public static boolean prefixDivisble ( int n ) { int i = 1 ; while ( n > 0 ) { if ( n % i != 0 ) return false ; n = n / 10 ; i ++ ; } return true ; } public static void main ( String [ ] args ) { int n = 52248 ; if ( prefixDivisble ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; public class Main { static void generatePalindrome ( String S ) { HashMap < Character , Integer > Hash = new HashMap < > ( ) ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( Hash . containsKey ( S . charAt ( i ) ) ) Hash . put ( S . charAt ( i ) , Hash . get ( S . charAt ( i ) ) + 1 ) ; else Hash . put ( S . charAt ( i ) , 1 ) ; } TreeSet < String > st = new TreeSet < String > ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( Hash . containsKey ( i ) && Hash . get ( i ) == 2 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { String s = " " ; if ( Hash . containsKey ( j ) && i != j ) { s += i ; s += j ; s += i ; st . add ( s ) ; } } } if ( Hash . containsKey ( i ) && Hash . get ( i ) >= 3 ) { for ( char j = ' a ' ; j <= ' z ' ; j ++ ) { String s = " " ; if ( Hash . containsKey ( j ) ) { s += i ; s += j ; s += i ; st . add ( s ) ; } } } } for ( String ans : st ) { System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { String S = " ddabdac " ; generatePalindrome ( S ) ; } }
class GFG { static void MinimumCost ( String S , int A , int B ) { int count = 0 ; int group = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( S . charAt ( i ) == '0' ) { count += 1 ; } else { if ( count > 0 ) { group += 1 ; } count = 0 ; } } if ( count > 0 ) group += 1 ; if ( group == 0 ) { System . out . println ( 0 ) ; } else { System . out . print ( Math . min ( A , B ) * ( group - 1 ) + B ) ; } } public static void main ( String args [ ] ) { int A = 1 ; int B = 5 ; String S = "01100" ; MinimumCost ( S , A , B ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void countOccurrences ( String S , String X , String Y ) { int count = 0 ; int N = S . length ( ) , A = X . length ( ) ; int B = Y . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( S . substring ( i , Math . min ( N , i + B ) ) . equals ( Y ) ) count ++ ; if ( S . substring ( i , Math . min ( N , i + A ) ) . equals ( X ) ) System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { String S = " abcdefdefabc " ; String X = " abc " ; String Y = " def " ; countOccurrences ( S , X , Y ) ; } }
import java . util . HashMap ; import java . util . Map ; class GFG { static void findMinimumTime ( String S , int N , int K ) { if ( N == 0 ) { System . out . println ( 0 ) ; return ; } int maxfreq = Integer . MIN_VALUE ; char maxchar = ' ▁ ' ; HashMap < Character , Integer > um = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { um . put ( S . charAt ( i ) , um . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; if ( um . get ( S . charAt ( i ) ) > maxfreq ) { maxfreq = um . get ( S . charAt ( i ) ) ; maxchar = S . charAt ( i ) ; } } int emptySlots = ( maxfreq - 1 ) * K ; for ( Map . Entry < Character , Integer > it : um . entrySet ( ) ) { if ( it . getKey ( ) == maxchar ) continue ; emptySlots -= Math . min ( it . getValue ( ) , maxfreq - 1 ) ; } int ans = N + Math . max ( 0 , emptySlots ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " AAABBB " ; int K = 2 ; int N = S . length ( ) ; findMinimumTime ( S , N , K ) ; } }
import java . io . * ; class GFG { static void canMakeEqual ( String S ) { int N = S . length ( ) ; int weightOfString = 0 ; for ( int i = 0 ; i < N ; i ++ ) { weightOfString += S . charAt ( i ) - ' a ' + 1 ; } if ( weightOfString % N == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " beb " ; canMakeEqual ( S ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void addFrequencyToCharacter ( String Str ) { int frequency [ ] = new int [ 26 ] ; int N = Str . length ( ) ; char S [ ] = Str . toCharArray ( ) ; for ( int i = 0 ; i < N ; i ++ ) { frequency [ S [ i ] - ' a ' ] += 1 ; } for ( int i = 0 ; i < N ; i ++ ) { int add = frequency [ S [ i ] - ' a ' ] % 26 ; if ( ( int ) ( S [ i ] ) + add <= ( int ) ( ' z ' ) ) S [ i ] = ( char ) ( ( int ) ( S [ i ] ) + add ) ; else { add = ( ( int ) ( S [ i ] ) + add ) - ( ( int ) ( ' z ' ) ) ; S [ i ] = ( char ) ( ( int ) ( ' a ' ) + add - 1 ) ; } } System . out . println ( new String ( S ) ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; addFrequencyToCharacter ( S ) ; } }
import java . util . * ; class GFG { static void findMinimumTime ( String tasks , int K ) { Map < Character , Integer > map = new HashMap < > ( ) ; int curr_time = 0 ; for ( char c : tasks . toCharArray ( ) ) { if ( map . containsKey ( c ) ) { if ( curr_time - map . get ( c ) <= K ) { curr_time += K - ( curr_time - map . get ( c ) ) + 1 ; } } map . put ( c , curr_time ) ; curr_time ++ ; } System . out . println ( curr_time ) ; } public static void main ( String [ ] args ) { String S = " ABACCA " ; int K = 2 ; findMinimumTime ( S , K ) ; } }
import java . util . Stack ; public class GFG { static void checkforSubsequence ( String S , String target ) { Stack < Character > s = new Stack < > ( ) ; for ( int i = 0 ; i < target . length ( ) ; i ++ ) { s . push ( target . charAt ( i ) ) ; } for ( int i = ( int ) S . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s . empty ( ) ) { System . out . println ( " Yes " ) ; return ; } if ( S . charAt ( i ) == s . peek ( ) ) { s . pop ( ) ; } } if ( s . empty ( ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " KOTTAYAM " ; String target = " KOTA " ; checkforSubsequence ( S , target ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static String digits = "0123456789" , sign = " + - " ; static String dot = " . " , ex = " eE " ; static int dfa [ ] [ ] = new int [ 11 ] [ 5 ] ; static void makeDFA ( ) { dfa [ 0 ] [ 0 ] = 1 ; dfa [ 1 ] [ 0 ] = 1 ; dfa [ 1 ] [ 2 ] = 3 ; dfa [ 1 ] [ 3 ] = 2 ; dfa [ 1 ] [ 4 ] = 6 ; dfa [ 3 ] [ 0 ] = 4 ; dfa [ 4 ] [ 0 ] = 4 ; dfa [ 4 ] [ 3 ] = 5 ; dfa [ 4 ] [ 4 ] = 6 ; dfa [ 6 ] [ 0 ] = 8 ; dfa [ 6 ] [ 1 ] = 7 ; dfa [ 7 ] [ 0 ] = 8 ; dfa [ 8 ] [ 0 ] = 8 ; dfa [ 8 ] [ 3 ] = 9 ; } static void buildDFA ( ) { for ( int i = 0 ; i < 11 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) dfa [ i ] [ j ] = 10 ; makeDFA ( ) ; } static void checkDFA ( String s ) { buildDFA ( ) ; int currentstate = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( digits . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 0 ] ; else if ( sign . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 1 ] ; else if ( dot . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 2 ] ; else if ( ex . indexOf ( s . charAt ( i ) ) != - 1 ) currentstate = dfa [ currentstate ] [ 4 ] ; else currentstate = dfa [ currentstate ] [ 3 ] ; } if ( currentstate == 1 currentstate == 4 currentstate == 8 ) { System . out . println ( " Unsigned ▁ integer " ) ; } else { System . out . println ( " Not ▁ an ▁ unsigned ▁ integer " ) ; } } public static void main ( String [ ] args ) { String S = "1729" ; checkDFA ( S ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printSentence ( String S ) { String nums [ ] = { " " , " " , " ABC " , " DEF " , " GHI " , " JKL " , " MNO " , " PQRS " , " TUV " , " WXYZ " } ; char str [ ] = S . toCharArray ( ) ; int i = 0 ; while ( i < str . length ) { if ( str [ i ] == ' . ' ) { i ++ ; continue ; } int count = 0 ; while ( i + 1 < str . length && str [ i ] == str [ i + 1 ] ) { if ( count == 2 && ( ( str [ i ] >= '2' && str [ i ] <= '6' ) || ( str [ i ] == '8' ) ) ) break ; else if ( count == 3 && ( str [ i ] == '7' str [ i ] == '9' ) ) break ; count ++ ; i ++ ; if ( i == str . length ) break ; } if ( str [ i ] == '7' str [ i ] == '9' ) { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 4 ) ) ; } else { System . out . print ( nums [ str [ i ] - 48 ] . charAt ( count % 3 ) ) ; } i ++ ; } } public static void main ( String [ ] args ) { String str = "234" ; printSentence ( str ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static String getString ( int N ) { String ans = " " ; while ( N >= 26 ) { ans += ' z ' ; N -= 26 ; } ans += ( char ) ( N + ' a ' - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int N = 30 ; System . out . print ( getString ( N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countSamePrefixSuffix ( String s , int n ) { String prefix = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) { prefix += s . charAt ( i ) ; String suffix = s . substring ( n - 1 - i , n ) ; if ( prefix . equals ( suffix ) ) { System . out . print ( prefix . length ( ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String S = " ababababab " ; int N = S . length ( ) ; countSamePrefixSuffix ( S , N ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void countStringPairs ( String a [ ] , int n ) { int ans = 0 ; Map < String , Integer > s = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { s . put ( a [ i ] , s . getOrDefault ( a [ i ] , 0 ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { StringBuilder p = new StringBuilder ( a [ i ] ) ; StringBuilder q = new StringBuilder ( a [ j ] ) ; if ( p . charAt ( 0 ) != q . charAt ( 0 ) ) { char t = p . charAt ( 0 ) ; p . setCharAt ( 0 , q . charAt ( 0 ) ) ; q . setCharAt ( 0 , t ) ; if ( ! s . containsKey ( p . toString ( ) ) && ! s . containsKey ( q . toString ( ) ) ) { ans ++ ; } } } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " good " , " bad " , " food " } ; int N = arr . length ; countStringPairs ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minimumMoves ( String s , int n , int k ) { if ( n % 2 == 1 ) { System . out . println ( " No " ) ; return ; } int countOpen = 0 , countClose = 0 ; for ( char ch : s . toCharArray ( ) ) if ( ch == ' ( ' ) countOpen ++ ; else if ( ch == ' ) ' ) countClose ++ ; if ( countOpen != countClose ) { System . out . println ( " No " ) ; return ; } int ans = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s . charAt ( i ) == ' ( ' ) ++ cnt ; else { -- cnt ; if ( cnt < 0 ) { cnt = 0 ; ++ ans ; } } } if ( ans <= k ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String S = " ) ( " ; int K = 1 ; minimumMoves ( S , S . length ( ) , K ) ; } }
class GFG { static int findRandom ( ) { int num = ( 1 + ( int ) ( Math . random ( ) * 100 ) ) % 2 ; return num ; } static void generateBinaryString ( int N ) { String S = " " ; for ( int i = 0 ; i < N ; i ++ ) { int x = findRandom ( ) ; S = S + String . valueOf ( x ) ; } System . out . println ( S ) ; } public static void main ( String [ ] args ) { int N = 7 ; generateBinaryString ( N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void checkPalindrome ( String s ) { int a [ ] = new int [ 26 ] ; int o = 0 , e = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( a [ i ] == 1 ) o ++ ; else if ( a [ i ] % 2 == 0 && a [ i ] != 0 ) e += ( a [ i ] / 2 ) ; } if ( e >= o ) System . out . println ( " Yes " ) ; else { o = o - e ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( o <= 0 ) break ; if ( o > 0 && a [ i ] % 2 == 1 && a [ i ] > 2 ) { int k = o ; o = o - a [ i ] / 2 ; if ( o > 0 2 * k + 1 == a [ i ] ) { o ++ ; a [ i ] = 1 ; } } } if ( o <= 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String S = " abbbaddzcz " ; checkPalindrome ( S ) ; } }
class GFG { static void maximumTime ( String str ) { char [ ] s = str . toCharArray ( ) ; if ( s [ 0 ] == ' _ ' ) { if ( ( s [ 1 ] == ' _ ' ) || ( s [ 1 ] >= '0' && s [ 1 ] < '4' ) ) { s [ 0 ] = '2' ; } else { s [ 0 ] = '1' ; } } if ( s [ 1 ] == ' _ ' ) { if ( s [ 0 ] == '2' ) { s [ 1 ] = '3' ; } else { s [ 1 ] = '9' ; } } if ( s [ 3 ] == ' _ ' ) { s [ 3 ] = '5' ; } if ( s [ 4 ] == ' _ ' ) { s [ 4 ] = '9' ; } for ( int i = 0 ; i < s . length ; i ++ ) System . out . print ( s [ i ] ) ; } static public void main ( String [ ] args ) { String S = "0 _ : 4 _ " ; maximumTime ( S ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void nearestPowerOfTwoUtil ( HashMap < Character , Integer > freq ) { for ( char key : freq . keySet ( ) ) { System . out . print ( key + " ▁ - > ▁ " ) ; int lg = ( int ) ( Math . log ( freq . get ( key ) / Math . log ( 2 ) ) ) ; int a = ( int ) Math . pow ( 2 , lg ) ; int b = ( int ) Math . pow ( 2 , lg + 1 ) ; if ( ( freq . get ( key ) - a ) < ( b - freq . get ( key ) ) ) { System . out . println ( a ) ; } else { System . out . println ( b ) ; } } } static void nearestPowerOfTwo ( String S ) { int N = S . length ( ) ; HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { freq . put ( S . charAt ( i ) , freq . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; } nearestPowerOfTwoUtil ( freq ) ; } public static void main ( String [ ] args ) { String N = "16333331163" ; nearestPowerOfTwo ( N ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int lcm ( int a , int b ) { return ( a / gcd ( a , b ) ) * b ; } static void findSmallestString ( String s , String t ) { int n = s . length ( ) , m = t . length ( ) ; int l = lcm ( n , m ) ; String s1 = " " , t1 = " " ; for ( int i = 0 ; i < l / n ; i ++ ) { s1 += s ; } for ( int i = 0 ; i < l / m ; i ++ ) { t1 += t ; } if ( s1 . equals ( t1 ) ) { System . out . println ( s1 ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { String S = " baba " , T = " ba " ; findSmallestString ( S , T ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int minRemovals ( String str , int N ) { int ans = 0 ; boolean X = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '0' ) { ans ++ ; while ( i < N && str . charAt ( i ) == '0' ) { i ++ ; } } else { X = true ; } } if ( ! X ) return 1 ; if ( str . charAt ( 0 ) == '0' && str . charAt ( N - 1 ) == '0' ) { ans -- ; } return ans ; } public static void main ( String [ ] args ) { String S = "11010001" ; int N = S . length ( ) ; System . out . println ( minRemovals ( S , N ) ) ; } }
import java . io . * ; class GFG { static int canMakeSame ( String s1 , String s2 ) { int ans = 0 ; if ( s1 . length ( ) != s2 . length ( ) ) { return - 1 ; } int N = s1 . length ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( i ) ) { while ( i < s1 . length ( ) && s1 . charAt ( i ) != s2 . charAt ( i ) ) { i ++ ; } ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String S1 = "100001" ; String S2 = "110111" ; System . out . println ( canMakeSame ( S1 , S2 ) ) ; } }
import java . io . * ; class GFG { static int convert ( String str ) { str = str . substring ( 0 , 2 ) + str . substring ( 3 ) ; int h1 = ( int ) str . charAt ( 1 ) - '0' ; int h2 = ( int ) str . charAt ( 0 ) - '0' ; int hh = ( h2 * 10 + h1 % 10 ) ; int time = 0 ; if ( str . charAt ( 5 ) == ' A ' ) { if ( hh == 12 ) time += Integer . parseInt ( str . substring ( 2 , 4 ) ) ; else { time += Integer . parseInt ( str . substring ( 0 , 2 ) ) ; } } else { if ( hh == 12 ) { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; } else { time += Integer . parseInt ( str . substring ( 0 , 4 ) ) ; time += 1200 ; } } return time ; } static int countOverlap ( String arr [ ] [ ] , int n , String p ) { int ans = 0 ; int M = convert ( p ) ; for ( int i = 0 ; i < n ; i ++ ) { int L = convert ( arr [ i ] [ 0 ] ) ; int R = convert ( arr [ i ] [ 1 ] ) ; if ( ( L <= M && M <= R ) || ( M >= R && M <= L ) ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = new String [ ] [ ] { { "12:00 : AM " , "11:55 : PM " } , { "12:01 : AM " , "11:50 : AM " } , { "12:30 : AM " , "12:00 : PM " } , { "11:57 : AM " , "11:59 : PM " } } ; String P = "12:01 : PM " ; int N = arr . length ; System . out . println ( countOverlap ( arr , N , P ) ) ; } }
import java . util . HashSet ; class GFG { static void countTotalFrequencies ( String S1 , String S2 ) { HashSet < Character > bset = new HashSet < Character > ( ) ; char [ ] S1arr = S1 . toCharArray ( ) ; char [ ] S2arr = S2 . toCharArray ( ) ; for ( char x : S1arr ) bset . add ( x ) ; int count = 0 ; for ( char x : S2arr ) { if ( bset . contains ( x ) ) count += 1 ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { String S1 = " geEksFOR " ; String S2 = " GeEksforgeEKS " ; countTotalFrequencies ( S1 , S2 ) ; } }
import java . util . * ; class GFG { static int nonDecreasing ( char [ ] s ) { int size = s . length ; char c = ' a ' ; for ( int i = 0 ; i < size ; i ++ ) { if ( s [ i ] == ' ? ' ) { continue ; } else if ( ( s [ i ] != ' ? ' ) && ( s [ i ] < c ) ) { return 0 ; } else { c = s [ i ] ; } } return 1 ; } public static void main ( String [ ] args ) { String S = " abb ? xy ? " ; if ( nonDecreasing ( S . toCharArray ( ) ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
import java . io . * ; class GFG { public static void min_length ( String str ) { int n = str . length ( ) ; int total_zeros = 0 ; int total_ones = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { total_zeros ++ ; } else { total_ones ++ ; } } int ans = Math . min ( total_zeros , total_ones ) ; int cur_zeros = 0 , cur_ones = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char x = str . charAt ( i ) ; if ( x == '0' ) { cur_zeros ++ ; } else { cur_ones ++ ; } ans = Math . min ( ans , cur_ones + ( total_zeros - cur_zeros ) ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String str = "10011" ; min_length ( str ) ; } }
import java . util . * ; class GFG { static int maxSumbySplittingString ( String str , int N ) { int cntOne = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == '1' ) { cntOne ++ ; } } int zero = 0 ; int one = 0 ; int res = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( str . charAt ( i ) == '0' ) { zero ++ ; } else { one ++ ; } res = Math . max ( res , zero + cntOne - one ) ; } return res ; } public static void main ( String [ ] args ) { String str = "00111" ; int N = str . length ( ) ; System . out . print ( maxSumbySplittingString ( str , N ) ) ; } }
import java . util . * ; class GFG { static String make_palindrome ( String S1 , String S2 , String S3 ) { int maskA = 0 , maskC = 0 ; for ( char i : S1 . toCharArray ( ) ) maskA |= ( 1 << ( i - ' a ' ) ) ; for ( char i : S3 . toCharArray ( ) ) maskC |= ( 1 << ( i - ' a ' ) ) ; if ( ( maskA & maskC ) > 0 ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { String S1 = " adcb " , S2 = " bcdb " , S3 = " abe " ; System . out . print ( make_palindrome ( S1 , S2 , S3 ) ) ; } }
public class Main { static int SIZE = 26 ; static boolean longHelper ( int [ ] freq1 , int [ ] freq2 ) { for ( int i = 0 ; i < SIZE ; ++ i ) { if ( freq1 [ i ] != freq2 [ i ] ) { return false ; } } return true ; } static void longCommomPrefixAnagram ( String s1 , String s2 , int n1 , int n2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; int ans = 0 ; int mini_len = Math . min ( n1 , n2 ) ; for ( int i = 0 ; i < mini_len ; ++ i ) { freq1 [ s1 . charAt ( i ) - ' a ' ] ++ ; freq2 [ s2 . charAt ( i ) - ' a ' ] ++ ; if ( longHelper ( freq1 , freq2 ) ) { ans = i + 1 ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { String str1 = " abaabcdezzwer " ; String str2 = " caaabbttyh " ; int N = str1 . length ( ) ; int M = str2 . length ( ) ; longCommomPrefixAnagram ( str1 , str2 , N , M ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class Main { static void findRank ( String s ) { int N = s . length ( ) ; StringBuilder sb = new StringBuilder ( " " ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == '0' ) sb . append ( '0' ) ; else sb . append ( '1' ) ; } String bin = sb . toString ( ) ; long X = ( long ) Math . pow ( 2 , N ) ; long Y = Long . parseLong ( bin , 2 ) ; long ans = X + Y - 1 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = "001" ; findRank ( S ) ; } }
import java . util . * ; class GFG { static void DFA ( String str , int N ) { if ( N <= 1 ) { System . out . print ( " No " ) ; return ; } int count = 0 ; if ( str . charAt ( 0 ) == ' C ' ) { count ++ ; for ( int i = 1 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' A ' || str . charAt ( i ) == ' B ' ) count ++ ; else break ; } } else { System . out . print ( " No " ) ; return ; } if ( count == N ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String str = " CAABBAAB " ; int N = str . length ( ) ; DFA ( str , N ) ; } }
import java . util . * ; class GFG { static int GCD ( int lena , int lenb ) { if ( lena == 0 ) return lenb ; if ( lenb == 0 ) return lena ; if ( lena == lenb ) return lena ; if ( lena > lenb ) return GCD ( lena - lenb , lenb ) ; return GCD ( lena , lenb - lena ) ; } static String StringGCD ( String a , String b ) { int gcd = GCD ( a . length ( ) , b . length ( ) ) ; if ( a . substring ( 0 , gcd ) . equals ( b . substring ( 0 , gcd ) ) ) { int x = ( ( int ) b . length ( ) / gcd ) ; int y = ( ( int ) a . length ( ) / gcd ) ; String r = " " , s = " " ; while ( x -- > 0 ) s += a ; while ( y -- > 0 ) r += b ; if ( s . equals ( r ) ) return a . substring ( 0 , gcd ) ; } return " - 1" ; } public static void main ( String [ ] args ) { String a = " geeksgeeks " ; String b = " geeks " ; System . out . print ( StringGCD ( a , b ) ) ; } }
import java . util . * ; class GFG { static long scoreOfParentheses ( String S ) { Stack < String > s = new Stack < > ( ) ; int i = 0 ; long ans = 0 ; while ( i < S . length ( ) ) { if ( S . charAt ( i ) == ' ( ' ) s . add ( " ( " ) ; else { if ( s . peek ( ) == " ( " ) { s . pop ( ) ; s . add ( "1" ) ; } else { long count = 0 ; while ( s . peek ( ) != " ( " ) { count += Integer . parseInt ( s . peek ( ) ) ; s . pop ( ) ; } s . pop ( ) ; s . add ( String . valueOf ( 2 * count ) ) ; } } i ++ ; } while ( ! s . isEmpty ( ) ) { ans += Integer . parseInt ( s . peek ( ) ) ; s . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { String S1 = " ( ( ) ( ( ) ) ) " ; System . out . print ( scoreOfParentheses ( S1 ) + "NEW_LINE"); } }
import java . io . * ; class TrieNode { TrieNode Trie [ ] ; boolean isEnd ; public TrieNode ( ) { Trie = new TrieNode [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) { Trie [ i ] = null ; } isEnd = false ; } } class GFG { static void InsertTrie ( TrieNode root , String s ) { TrieNode temp = root ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( temp . Trie [ s . charAt ( i ) ] == null ) { temp . Trie [ s . charAt ( i ) ] = new TrieNode ( ) ; } temp = temp . Trie [ s . charAt ( i ) ] ; } temp . isEnd = true ; } static void printSuggestions ( TrieNode root , String res ) { if ( root . isEnd == true ) { System . out . print ( res + " ▁ " ) ; } for ( int i = 0 ; i < 256 ; i ++ ) { if ( root . Trie [ i ] != null ) { res += ( char ) i ; printSuggestions ( root . Trie [ i ] , res ) ; res = res . substring ( 0 , res . length ( ) - 2 ) ; } } } static boolean checkPresent ( TrieNode root , String key ) { for ( int i = 0 ; i < key . length ( ) ; i ++ ) { if ( root . Trie [ key . charAt ( i ) ] == null ) { printSuggestions ( root , key . substring ( 0 , i ) ) ; return false ; } root = root . Trie [ key . charAt ( i ) ] ; } if ( root . isEnd == true ) { return true ; } printSuggestions ( root , key ) ; return false ; } public static void main ( String [ ] args ) { String str [ ] = { " gee " , " geeks " , " ape " , " apple " , " geeksforgeeks " } ; String key = " geek " ; TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < str . length ; i ++ ) { InsertTrie ( root , str [ i ] ) ; } if ( checkPresent ( root , key ) ) { System . out . println ( " YES " ) ; } } }
import java . util . * ; class GFG { static int countStrings ( String S , String [ ] list ) { HashSet < Character > valid = new HashSet < Character > ( ) ; for ( char x : S . toCharArray ( ) ) { valid . add ( x ) ; } int cnt = 0 ; for ( int i = 0 ; i < list . length ; i ++ ) { int j = 0 ; for ( j = 0 ; j < list [ i ] . length ( ) ; j ++ ) { if ( valid . contains ( list [ i ] . charAt ( j ) ) ) continue ; else break ; } if ( j == list [ i ] . length ( ) ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { String [ ] arr = { " ab " , " aab " , " abaaaa " , " bbd " } ; String S = " ab " ; System . out . print ( countStrings ( S , arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int minLength ( String str , int N , String K , int M ) { Stack < Character > stackOfChar = new Stack < Character > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { stackOfChar . add ( str . charAt ( i ) ) ; if ( stackOfChar . size ( ) >= M ) { String l = " " ; for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( K . charAt ( j ) != stackOfChar . peek ( ) ) { int f = 0 ; while ( f != l . length ( ) ) { stackOfChar . add ( l . charAt ( f ) ) ; f ++ ; } break ; } else { l = stackOfChar . peek ( ) + l ; stackOfChar . pop ( ) ; } } } } return stackOfChar . size ( ) ; } public static void main ( String [ ] args ) { String S1 = " fffoxoxoxfxo " ; String S2 = " fox " ; int N = S1 . length ( ) ; int M = S2 . length ( ) ; System . out . print ( minLength ( S1 , N , S2 , M ) ) ; } }
import java . util . * ; class GFG { static void palindrome_prod ( String N ) { Vector < Integer > palindrome_list = new Vector < Integer > ( ) ; for ( int i = 101 ; i < 1000 ; i ++ ) { for ( int j = 121 ; j < 1000 ; j += ( i % 11 == 0 ) ? 1 : 11 ) { int n = i * j ; String x = String . valueOf ( n ) ; String y = x ; reverse ( y ) ; if ( x == y ) { if ( n < Integer . valueOf ( N ) ) { palindrome_list . add ( i * j ) ; } } } } System . out . print ( Collections . max ( palindrome_list ) ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String N = "101110" ; palindrome_prod ( N ) ; } }
class GFG { static void minMaxDigits ( String str , int N ) { int arr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) arr [ i ] = ( str . charAt ( i ) - '0' ) % 3 ; int zero = 0 , one = 0 , two = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero ++ ; if ( arr [ i ] == 1 ) one ++ ; if ( arr [ i ] == 2 ) two ++ ; } int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum = ( sum + arr [ i ] ) % 3 ; } if ( sum == 0 ) { System . out . print ( 0 + " ▁ " ) ; } if ( sum == 1 ) { if ( ( one != 0 ) && ( N > 1 ) ) System . out . print ( 1 + " ▁ " ) ; else if ( two > 1 && N > 2 ) System . out . print ( 2 + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } if ( sum == 2 ) { if ( two != 0 && N > 1 ) System . out . print ( 1 + " ▁ " ) ; else if ( one > 1 && N > 2 ) System . out . print ( 2 + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } if ( zero > 0 ) System . out . print ( N - 1 + " ▁ " ) ; else if ( one > 0 && two > 0 ) System . out . print ( N - 2 + " ▁ " ) ; else if ( one > 2 two > 2 ) System . out . print ( N - 3 + " ▁ " ) ; else System . out . print ( - 1 + " ▁ " ) ; } public static void main ( String [ ] args ) { String str = "12345" ; int N = str . length ( ) ; minMaxDigits ( str , N ) ; } }
import java . util . * ; class GFG { static int count_of_accident ( String a , String b ) { int n = a . length ( ) , m = b . length ( ) ; if ( n > m ) return ( m * ( m + 1 ) ) / 2 ; else return ( n * ( n + 1 ) ) / 2 + ( m - n ) * n ; } static double count_of_collision ( String a , String b ) { int n = a . length ( ) , m = b . length ( ) ; double answer = 0 ; int count_of_truck_in_lane_b = 0 ; for ( int i = 0 ; i < m ; i ++ ) if ( b . charAt ( i ) == ' T ' ) count_of_truck_in_lane_b ++ ; for ( int i = 0 ; i < n && i < m ; i ++ ) { if ( a . charAt ( i ) == ' T ' ) answer += count_of_truck_in_lane_b ; if ( b . charAt ( i ) == ' T ' ) count_of_truck_in_lane_b -- ; } return answer ; } static void findProbability ( String a , String b ) { int total_outcome = count_of_accident ( a , b ) ; double favourable_outcome = count_of_collision ( a , b ) ; System . out . printf ( " % 4f " , favourable_outcome / total_outcome ) ; } public static void main ( String [ ] args ) { String S = " TCCBCTTB " , T = " BTCCBBTT " ; findProbability ( S , T ) ; } }
import java . util . * ; class GFG { static int arcIntersection ( String S , int len ) { Stack < Character > stk = new Stack < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { stk . push ( S . charAt ( i ) ) ; if ( stk . size ( ) >= 2 ) { char temp = stk . peek ( ) ; stk . pop ( ) ; if ( stk . peek ( ) == temp ) { stk . pop ( ) ; } else { stk . add ( temp ) ; } } } if ( stk . isEmpty ( ) ) return 1 ; return 0 ; } static void countString ( String arr [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int len = arr [ i ] . length ( ) ; count += arcIntersection ( arr [ i ] , len ) ; } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { String arr [ ] = { "0101" , "0011" , "0110" } ; int N = arr . length ; countString ( arr , N ) ; } }
import java . util . * ; class GFG { static int findMinimumChanges ( int N , int K , char [ ] S ) { int ans = 0 ; for ( int i = 0 ; i < ( K + 1 ) / 2 ; i ++ ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; for ( int j = i ; j < N ; j += K ) { if ( mp . containsKey ( S [ j ] ) ) { mp . put ( S [ j ] , mp . get ( S [ j ] ) + 1 ) ; } else { mp . put ( S [ j ] , 1 ) ; } } for ( int j = N - i - 1 ; j >= 0 ; j -= K ) { if ( K % 2 == 1 && i == K / 2 ) break ; if ( mp . containsKey ( S [ j ] ) ) { mp . put ( S [ j ] , mp . get ( S [ j ] ) + 1 ) ; } else { mp . put ( S [ j ] , 1 ) ; } } int curr_max = Integer . MIN_VALUE ; for ( Map . Entry < Character , Integer > p : mp . entrySet ( ) ) { curr_max = Math . max ( curr_max , p . getValue ( ) ) ; } if ( ( K % 2 == 1 ) && i == K / 2 ) ans += ( N / K - curr_max ) ; else ans += ( N / K * 2 - curr_max ) ; } return ans ; } public static void main ( String [ ] args ) { String S = " aabbcbbcb " ; int N = S . length ( ) ; int K = 3 ; System . out . print ( findMinimumChanges ( N , K , S . toCharArray ( ) ) ) ; } }
import java . util . * ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static void main ( String [ ] args ) { int N = 3 ; int flag , k , m = 0 ; if ( ( N == 1 ) || ( N == 2 ) ) System . out . print ( 62 ) ; else m = 1000000000 + 7 ; if ( N % 2 == 0 ) { k = N / 2 ; flag = 1 ; } else { k = ( N - 1 ) / 2 ; flag = 0 ; } if ( flag != 0 ) { int a = power ( 62 , k , m ) ; System . out . print ( a ) ; } else { int a = power ( 62 , ( k + 1 ) , m ) ; System . out . print ( a ) ; } } }
import java . util . * ; class GFG { static int lenOfLongestReqSubstr ( String s , int N ) { HashMap < Integer , Integer > ind = new HashMap < > ( ) ; int mask = 0 ; ind . put ( 0 , - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int val = s . charAt ( i ) - '0' ; mask ^= ( 1 << val ) ; if ( ind . containsKey ( mask ) ) { ans = Math . max ( ans , i - ind . get ( mask ) ) ; } else ind . put ( mask , i ) ; } return ans ; } public static void main ( String [ ] args ) { String s = "223015150" ; int N = s . length ( ) ; System . out . print ( lenOfLongestReqSubstr ( s , N ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static String ConvertequivalentBase8 ( String S ) { HashMap < String , Character > mp = new HashMap < String , Character > ( ) ; mp . put ( "000" , '0' ) ; mp . put ( "001" , '1' ) ; mp . put ( "010" , '2' ) ; mp . put ( "011" , '3' ) ; mp . put ( "100" , '4' ) ; mp . put ( "101" , '5' ) ; mp . put ( "110" , '6' ) ; mp . put ( "111" , '7' ) ; int N = S . length ( ) ; if ( N % 3 == 2 ) { S = "0" + S ; } else if ( N % 3 == 1 ) { S = "00" + S ; } N = S . length ( ) ; String oct = " " ; for ( int i = 0 ; i < N ; i += 3 ) { String temp = S . substring ( i , i + 3 ) ; oct += mp . get ( temp ) ; } return oct ; } static String binString_div_9 ( String S , int N ) { String oct = " " ; oct = ConvertequivalentBase8 ( S ) ; int oddSum = 0 ; int evenSum = 0 ; int M = oct . length ( ) ; for ( int i = 0 ; i < M ; i += 2 ) oddSum += ( oct . charAt ( i ) - '0' ) ; for ( int i = 1 ; i < M ; i += 2 ) { evenSum += ( oct . charAt ( i ) - '0' ) ; } int Oct_9 = 11 ; if ( Math . abs ( oddSum - evenSum ) % Oct_9 == 0 ) { return " Yes " ; } return " No " ; } public static void main ( String [ ] args ) { String S = "1010001" ; int N = S . length ( ) ; System . out . println ( binString_div_9 ( S , N ) ) ; } }
import java . util . * ; class GFG { static int uniqueMorseRep ( String [ ] arr ) { String [ ] morseCode = { " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " } ; HashSet < String > st = new HashSet < > ( ) ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { String temp = " " ; int M = arr [ i ] . length ( ) ; for ( int j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] . charAt ( j ) - ' a ' ] ; } st . add ( temp ) ; } return st . size ( ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " gig " , " zeg " , " gin " , " msn " } ; System . out . print ( uniqueMorseRep ( arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int findMinCost ( String str , int pos ) { int n = str . length ( ) ; StringBuilder input1 = new StringBuilder ( ) ; input1 . append ( str ) ; if ( pos >= n / 2 ) { input1 = input1 . reverse ( ) ; pos = n - pos - 1 ; } int left , right ; left = right = pos ; for ( int i = pos ; i >= 0 ; -- i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) { left = i ; } } for ( int i = pos ; i < n / 2 ; ++ i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) { right = i ; } } int ans = 0 ; for ( int i = left ; i <= right ; ++ i ) { if ( input1 . charAt ( i ) != input1 . charAt ( n - i - 1 ) ) ans += 1 ; } int dis = Math . min ( ( 2 * ( pos - left ) + ( right - pos ) ) , ( 2 * ( right - pos ) + ( pos - left ) ) ) ; ans = ans + dis ; return ans ; } public static void main ( String args [ ] ) { String S = " bass " ; int P = 3 ; System . out . println ( findMinCost ( S , P ) ) ; } }
import java . io . * ; class GFG { static int minCost ( String s , int n ) { int minValue = 100000000 ; for ( int i = 0 ; i <= 25 ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { cnt += Math . min ( Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) , 26 - Math . abs ( i - ( s . charAt ( j ) - ' a ' ) ) ) ; } minValue = Math . min ( minValue , cnt ) ; } return minValue ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int N = str . length ( ) ; System . out . println ( minCost ( str , N ) ) ; } }
import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } static void max_len ( String s [ ] , int N , int M ) { HashSet < String > set_str = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { set_str . add ( s [ i ] ) ; } Vector < String > left_ans = new Vector < > ( ) ; Vector < String > right_ans = new Vector < > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = s [ i ] ; t = reverse ( t ) ; if ( t == s [ i ] ) { mid = t ; } else if ( set_str . contains ( t ) ) { left_ans . add ( s [ i ] ) ; right_ans . add ( t ) ; set_str . remove ( s [ i ] ) ; set_str . remove ( t ) ; } } for ( String x : left_ans ) { System . out . print ( x ) ; } System . out . print ( mid ) ; Collections . reverse ( right_ans ) ; for ( String x : right_ans ) { System . out . print ( x ) ; } } public static void main ( String [ ] args ) { int N = 4 , M = 3 ; String s [ ] = { " omg " , " bbb " , " ffd " , " gmo " } ; max_len ( s , N , M ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void minimum_length ( int x1 , int y1 , int x2 , int y2 , String str ) { int n = str . length ( ) ; int pos1 , pos2 , pos3 , pos4 ; pos1 = - 1 ; pos2 = - 1 ; pos3 = - 1 ; pos4 = - 1 ; if ( x1 == x2 && y1 == y2 ) { System . out . println ( "0" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { if ( x2 > x1 ) { if ( str . charAt ( i ) == ' E ' ) { x1 = x1 + 1 ; if ( x1 == x2 ) { pos1 = i ; } } } if ( x2 < x1 ) { if ( str . charAt ( i ) == ' W ' ) { x1 = x1 - 1 ; if ( x1 == x2 ) { pos2 = i ; } } } if ( y2 > y1 ) { if ( str . charAt ( i ) == ' N ' ) { y1 = y1 + 1 ; if ( y1 == y2 ) { pos3 = i ; } } } if ( y2 < y1 ) { if ( str . charAt ( i ) == ' S ' ) { y1 = y1 - 1 ; if ( y1 == y2 ) { pos4 = i ; } } } } int z ; z = Math . max ( pos1 , Math . max ( Math . max ( pos2 , pos3 ) , pos4 ) ) ; if ( x1 == x2 && y1 == y2 ) { System . out . println ( z + 1 ) ; } else { System . out . println ( " - 1" ) ; } } } public static void main ( String [ ] args ) throws java . lang . Exception { String str = " SESNW " ; int x1 = 0 , x2 = 1 , y1 = 0 , y2 = 1 ; minimum_length ( x1 , y1 , x2 , y2 , str ) ; } }
import java . io . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { res = res * i ; } return res ; } static int distinctWays ( String str1 , String str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; if ( n == m ) { return 2 * fact ( n ) * fact ( m ) ; } return fact ( n ) * fact ( m ) ; } public static void main ( String [ ] args ) { String str1 = " aegh " ; String str2 = " rsw " ; System . out . print ( distinctWays ( str1 , str2 ) ) ; } }
class GFG { public static String len ( long A , long B ) { String res = " " ; long log = 0 ; do { log = ( long ) ( Math . log ( B ) / Math . log ( 2 ) ) ; if ( log != 0 ) { res += A + " < < " + log ; } else { res += A ; break ; } B = B - ( long ) Math . pow ( 2 , log ) ; if ( B != 0 ) { res += " + " ; } else break ; } while ( log != 0 ) ; return res ; } public static void minimumString ( long A , long B ) { String res1 = len ( A , B ) ; String res2 = len ( B , A ) ; if ( res1 . length ( ) > res2 . length ( ) ) { System . out . println ( res2 ) ; } else { System . out . println ( res1 ) ; } } public static void main ( String args [ ] ) { long A = 6 ; long B = 10 ; minimumString ( A , B ) ; } }
class GFG { static void countSubString ( String s ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int x = 0 ; for ( int j = i ; j < s . length ( ) ; j ++ ) { int temp = 1 << s . charAt ( j ) - ' a ' ; x ^= temp ; if ( ( x & ( x - 1 ) ) == 0 ) res ++ ; } } System . out . print ( res ) ; } public static void main ( String [ ] args ) { String str = " aaa " ; countSubString ( str ) ; } }
import java . util . * ; class GFG { static void countSubString ( String s ) { int answer = 0 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; m . put ( 0 , 1 ) ; int x = 0 ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { x ^= 1 << ( s . charAt ( j ) - ' a ' ) ; answer += m . containsKey ( x ) ? m . get ( x ) : 0 ; for ( int i = 0 ; i < 26 ; ++ i ) { answer += m . containsKey ( x ^ ( 1 << i ) ) ? m . get ( x ^ ( 1 << i ) ) : 0 ; } if ( m . containsKey ( x ) ) m . put ( x , m . get ( x ) + 1 ) ; else m . put ( x , 1 ) ; } System . out . print ( answer ) ; } public static void main ( String [ ] args ) { String str = " abab " ; countSubString ( str ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void printResult ( ArrayList < Integer > result ) { Collections . reverse ( result ) ; int i = 0 ; while ( i < result . size ( ) ) { System . out . print ( result . get ( i ) ) ; i ++ ; } } static void sumOfLargeNumbers ( String v [ ] , int k , int N ) { ArrayList < ArrayList < Integer > > x = new ArrayList < > ( 1000 ) ; for ( int i = 0 ; i < k ; i ++ ) x . add ( new ArrayList < Integer > ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { x . get ( i ) . add ( v [ i ] . charAt ( j ) - '0' ) ; } } int carry = 0 ; ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) { int sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) sum += x . get ( j ) . get ( i ) ; sum += carry ; int temp = sum ; int count = 0 ; while ( temp > 9 ) { temp = temp % 10 ; count ++ ; } long l = ( long ) Math . pow ( 10 , count ) ; if ( l != 1 ) carry = ( int ) ( sum / l ) ; sum = sum % 10 ; result . add ( sum ) ; } while ( carry != 0 ) { int a = carry % 10 ; result . add ( a ) ; carry = carry / 10 ; } printResult ( result ) ; } public static void main ( String [ ] args ) { int K = 10 ; int N = 5 ; String arr [ ] = { "1111111111" , "1111111111" , "1111111111" , "1111111111" , "1111111111" } ; sumOfLargeNumbers ( arr , N , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int min_cost ( String S ) { int cost = 0 ; int F = 0 ; int B = 0 ; int count = 0 ; for ( char c : S . toCharArray ( ) ) if ( c == ' ▁ ' ) count ++ ; int n = S . length ( ) - count ; if ( n == 1 ) return cost ; for ( char in : S . toCharArray ( ) ) { if ( in != ' ▁ ' ) { if ( B != 0 ) { cost += Math . min ( n - F , F ) * B ; B = 0 ; } F += 1 ; } else { B += 1 ; } } return cost ; } public static void main ( String [ ] args ) { String S = " ▁ @ TABSYMBOL $ " ; System . out . println ( min_cost ( S ) ) ; } }
import java . util . * ; class GFG { static String checkString ( String s , int K ) { int n = s . length ( ) ; Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mp . put ( s . charAt ( i ) , i ) ; } int cnt = 0 , f = 0 ; Set < Character > st = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { st . add ( s . charAt ( i ) ) ; if ( st . size ( ) > K ) { f = 1 ; break ; } if ( mp . get ( s . charAt ( i ) ) == i ) st . remove ( s . charAt ( i ) ) ; } return ( f == 1 ? " Yes " : " No " ) ; } public static void main ( String [ ] args ) { String s = " aabbcdca " ; int k = 2 ; System . out . println ( checkString ( s , k ) ) ; } }
class GFG { static final long mod = 1000000007 ; static long binExp ( long base , long power ) { long x = 1 ; while ( power != 0 ) { if ( power % 2 == 1 ) x = ( ( x % mod ) * ( base % mod ) ) % mod ; base = ( ( base % mod ) * ( base % mod ) ) % mod ; power = power / 2 ; } return x ; } static long fact ( long num ) { long result = 1 ; for ( long i = 1 ; i <= num ; ++ i ) { result = ( ( result % mod ) * ( i % mod ) ) % mod ; } return result ; } static long calculate_nCi ( long N , long i ) { long nfact = fact ( N ) ; long ifact = fact ( i ) ; long dfact = fact ( N - i ) ; long inv_ifact = binExp ( ifact , mod - 2 ) ; long inv_dfact = binExp ( dfact , mod - 2 ) ; long denm = ( ( inv_ifact % mod ) * ( inv_dfact % mod ) ) % mod ; long answer = ( ( nfact % mod ) * ( denm % mod ) ) % mod ; return answer ; } static void countSubstring ( long N , long s , long k ) { long allWays = binExp ( 26 , N ) ; long noWays = 0 ; for ( long i = 0 ; i < s ; ++ i ) { long nCi = calculate_nCi ( N , i ) ; long remaining = binExp ( 25 , N - i ) ; long multiply = ( ( nCi % mod ) * ( remaining % mod ) ) % mod ; noWays = ( ( noWays % mod ) + ( multiply % mod ) ) % mod ; } long answer = ( ( allWays % mod ) - ( noWays % mod ) ) % mod ; if ( answer < 0 ) answer += mod ; System . out . println ( answer ) ; } public static void main ( String [ ] args ) { String str = " abc " ; long k = 2 ; long s = str . length ( ) ; long N = s + k ; countSubstring ( N , s , k ) ; } }
import java . util . * ; class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } static int minCost ( String S ) { int cA = 0 ; int cE = 0 ; int cI = 0 ; int cO = 0 ; int cU = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( isVowel ( S . charAt ( i ) ) ) { cA += Math . abs ( S . charAt ( i ) - ' a ' ) ; cE += Math . abs ( S . charAt ( i ) - ' e ' ) ; cI += Math . abs ( S . charAt ( i ) - ' i ' ) ; cO += Math . abs ( S . charAt ( i ) - ' o ' ) ; cU += Math . abs ( S . charAt ( i ) - ' u ' ) ; } } return Math . min ( Math . min ( Math . min ( Math . min ( cA , cE ) , cI ) , cO ) , cU ) ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; System . out . println ( minCost ( S ) ) ; } }
import java . util . * ; class GFG { static void count_minimum ( String s ) { int n = s . length ( ) ; int ans = 0 ; int i = 0 ; while ( i < n ) { int j = i ; while ( j < n && s . charAt ( j ) == s . charAt ( i ) ) { j ++ ; } int diff = j - i ; ans += diff / 2 ; i = j ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String str = " caaab " ; count_minimum ( str ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int min_cost ( String st ) { String vow = " aeiou " ; int cost = 0 ; for ( int i = 0 ; i < st . length ( ) ; i ++ ) { ArrayList < Integer > costs = new ArrayList < > ( ) ; for ( int j = 0 ; j < 5 ; j ++ ) costs . add ( Math . abs ( st . charAt ( i ) - vow . charAt ( j ) ) ) ; int minx = Integer . MAX_VALUE ; for ( int x : costs ) { if ( x < minx ) { minx = x ; } } cost += minx ; } return cost ; } public static void main ( String [ ] args ) { String str = " abcde " ; System . out . println ( min_cost ( str ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { 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 ; } return true ; } static String makeOddString ( String str ) { String odd = " " ; for ( int i = 1 ; i < str . length ( ) ; i += 2 ) { odd += str . charAt ( i ) ; } return odd ; } static String makeevenString ( String str ) { String even = " " ; for ( int i = 0 ; i < str . length ( ) ; i += 2 ) { even += str . charAt ( i ) ; } return even ; } static void checkevenOddPalindrome ( String str ) { String odd = makeOddString ( str ) ; String even = makeevenString ( str ) ; if ( isPalindrome ( odd ) && isPalindrome ( even ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { String str = " abzzab " ; checkevenOddPalindrome ( str ) ; } }
import java . util . * ; class GFG { static long count_Strings ( long n ) { int x = 1 ; for ( int i = 1 ; i < n ; i ++ ) { x = ( 1 << x ) ; } return x - 1 ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . print ( count_Strings ( n ) ) ; } }
class GFG { static void decrypt ( String Str , int Start , int End ) { if ( Start > End ) { return ; } int mid = ( Start + End ) >> 1 ; System . out . print ( Str . charAt ( mid ) ) ; decrypt ( Str , mid + 1 , End ) ; decrypt ( Str , Start , mid - 1 ) ; } public static void main ( String [ ] args ) { int N = 4 ; String Str = " abcd " ; decrypt ( Str , 0 , N - 1 ) ; System . out . print ( "NEW_LINE"); N = 6 ; Str = " gyuitp " ; decrypt ( Str , 0 , N - 1 ) ; } }
class GFG { static boolean isSuffix ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 . charAt ( n1 - i - 1 ) != s2 . charAt ( n2 - i - 1 ) ) return false ; return true ; } static boolean CheckBinaryEquivalent ( int N , String str ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = ( int ) Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } String bin = Integer . toString ( B_Number ) ; return isSuffix ( str , bin ) ; } public static void main ( String [ ] args ) { int N = 23 ; String str = "111" ; if ( CheckBinaryEquivalent ( N , str ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
class GFG { public static void decode_String ( String str , int K ) { String ans = " " ; for ( int i = 0 ; i < str . length ( ) ; i += K ) ans += str . charAt ( i ) ; for ( int i = str . length ( ) - ( K - 1 ) ; i < str . length ( ) ; i ++ ) ans += str . charAt ( i ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int K = 3 ; String str = " abcbcscsesesesd " ; decode_String ( str , K ) ; } }
import java . util . * ; class GFG { static boolean isvowel ( char ch ) { return ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) ; } static boolean isvalid ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! isvowel ( s . charAt ( i ) ) ) return false ; } return true ; } static int CountTotal ( String s ) { int ans = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String temp = " " ; for ( int j = i ; j < n ; j ++ ) { temp += s . charAt ( j ) ; if ( isvalid ( temp ) ) ans += 1 ; } } return ans ; } public static void main ( String [ ] args ) { String s = " aeoibsddaaeiouudb " ; System . out . print ( ( CountTotal ( s ) ) + "NEW_LINE"); } }
import java . io . * ; class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static boolean isValid ( String s ) { int n = s . length ( ) ; if ( n == 1 ) return ( isVowel ( s . charAt ( 0 ) ) ) ; if ( isVowel ( s . charAt ( 0 ) ) == false ) return false ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) || ! isVowel ( s . charAt ( i ) ) ) return false ; } return true ; } static int findMaxLen ( String s ) { int maxLen = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String temp = " " ; for ( int j = i ; j < n ; j ++ ) { temp = temp + s . charAt ( j ) ; if ( isValid ( temp ) ) maxLen = Math . max ( maxLen , ( j - i + 1 ) ) ; } } return maxLen ; } public static void main ( String [ ] args ) { String Str = " aeoibsddaeiouudb " ; System . out . println ( findMaxLen ( Str ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void depthFirst ( int v , List < List < Integer > > graph , boolean [ ] visited , List < Integer > storeChain ) { visited [ v ] = true ; storeChain . add ( v ) ; for ( int i : graph . get ( v ) ) { if ( visited [ i ] == false ) { depthFirst ( i , graph , visited , storeChain ) ; } } } static void createMap ( Map < String , Character > um ) { um . put ( "0000" , '0' ) ; um . put ( "0001" , '1' ) ; um . put ( "0010" , '2' ) ; um . put ( "0011" , '3' ) ; um . put ( "0100" , '4' ) ; um . put ( "0101" , '5' ) ; um . put ( "0110" , '6' ) ; um . put ( "0111" , '7' ) ; um . put ( "1000" , '8' ) ; um . put ( "1001" , '9' ) ; um . put ( "1010" , ' A ' ) ; um . put ( "1011" , ' B ' ) ; um . put ( "1100" , ' C ' ) ; um . put ( "1101" , ' D ' ) ; um . put ( "1110" , ' E ' ) ; um . put ( "1111" , ' F ' ) ; } static String hexaDecimal ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 4 - len_left % 4 ) % 4 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 4 - len_right % 4 ) % 4 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_hex_map = new HashMap < String , Character > ( ) ; createMap ( bin_hex_map ) ; int i = 0 ; String hex = " " ; while ( true ) { hex += bin_hex_map . get ( bin . substring ( i , i + 4 ) ) ; i += 4 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } static void hexValue ( List < List < Integer > > graph , int vertices , List < Integer > values ) { boolean [ ] visited = new boolean [ 1001 ] ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; List < Integer > storeChain = new ArrayList < Integer > ( ) ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . size ( ) ; int [ ] chainValues = new int [ sizeChain + 1 ] ; for ( int j = 0 ; j < sizeChain ; j ++ ) { int temp = values . get ( storeChain . get ( j ) - 1 ) ; chainValues [ j ] = temp ; } System . out . print ( " Chain ▁ = ▁ " ) ; for ( int j = 0 ; j < sizeChain ; j ++ ) { System . out . print ( chainValues [ j ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " TABSYMBOL " ) ; String s = " " ; for ( int j = 0 ; j < sizeChain ; j ++ ) { String s1 = String . valueOf ( chainValues [ j ] ) ; s += s1 ; } System . out . println ( " Hexadecimal ▁ " + " equivalent ▁ = ▁ " + hexaDecimal ( s ) ) ; } } } public static void main ( String [ ] args ) { @ SuppressWarnings ( " unchecked " ) List < List < Integer > > graph = new ArrayList ( ) ; for ( int i = 0 ; i < 1001 ; i ++ ) graph . add ( new ArrayList < Integer > ( ) ) ; int E = 4 , V = 7 ; List < Integer > values = new ArrayList < Integer > ( ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 1 ) ; values . add ( 1 ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 1 ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 1 ) ; graph . get ( 3 ) . add ( 4 ) ; graph . get ( 4 ) . add ( 3 ) ; graph . get ( 4 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 4 ) ; graph . get ( 6 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 6 ) ; graph . get ( 6 ) . add ( 7 ) ; graph . get ( 7 ) . add ( 6 ) ; hexValue ( graph , V , values ) ; } }
import java . io . * ; class GFG { private static void BalancedNumber ( String s ) { int Leftsum = 0 ; int Rightsum = 0 ; for ( int i = 0 ; i < s . length ( ) / 2 ; i ++ ) { Leftsum += ( int ) ( s . charAt ( i ) - '0' ) ; Rightsum += ( int ) ( s . charAt ( s . length ( ) - 1 - i ) - '0' ) ; } if ( Leftsum == Rightsum ) System . out . println ( " Balanced " ) ; else System . out . println ( " Not ▁ Balanced " ) ; } public static void main ( String [ ] args ) { String s = "12321" ; BalancedNumber ( s ) ; } }
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int expo ( int base , int exponent ) { int ans = 1 ; while ( exponent != 0 ) { if ( ( exponent & 1 ) == 1 ) { ans = ans * base ; ans = ans % mod ; } base = base * base ; base %= mod ; exponent >>= 1 ; } return ans % mod ; } static int findCount ( int N ) { int ans = ( expo ( 26 , N ) - expo ( 5 , N ) - expo ( 21 , N ) ) % mod ; ans += mod ; ans %= mod ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( findCount ( N ) ) ; } }
import java . util . * ; class GFG { static int decompose ( Vector < Integer > S ) { Stack < Integer > s = new Stack < Integer > ( ) ; int N = S . size ( ) ; int ans = 0 ; int nix = Integer . MAX_VALUE ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( s . isEmpty ( ) ) { s . add ( S . get ( i ) ) ; nix = S . get ( i ) ; } else { if ( S . get ( i ) < s . peek ( ) ) { s . add ( S . get ( i ) ) ; nix = Math . min ( nix , S . get ( i ) ) ; } else { int val = S . get ( i ) ; while ( ! s . isEmpty ( ) && val >= s . peek ( ) ) { s . pop ( ) ; } nix = Math . min ( nix , S . get ( i ) ) ; s . add ( nix ) ; } } ans += s . size ( ) ; } return ans ; } public static void main ( String args [ ] ) { Vector < Integer > S = new Vector < Integer > ( ) ; S . add ( 9 ) ; S . add ( 6 ) ; S . add ( 9 ) ; S . add ( 35 ) ; System . out . println ( decompose ( S ) ) ; } }
class GFG { static String maxVowelSubString ( String str , int K ) { int N = str . length ( ) ; int [ ] pref = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { if ( str . charAt ( i ) == ' a ' || str . charAt ( i ) == ' e ' || str . charAt ( i ) == ' i ' || str . charAt ( i ) == ' o ' || str . charAt ( i ) == ' u ' ) pref [ i ] = 1 ; else pref [ i ] = 0 ; if ( i != 0 ) pref [ i ] += pref [ i - 1 ] ; } int maxCount = pref [ K - 1 ] ; String res = str . substring ( 0 , K ) ; for ( int i = K ; i < N ; i ++ ) { int currCount = pref [ i ] - pref [ i - K ] ; if ( currCount > maxCount ) { maxCount = currCount ; res = str . substring ( i - K + 1 , i + 1 ) ; } else if ( currCount == maxCount ) { String temp = str . substring ( i - K + 1 , i + 1 ) ; if ( temp . compareTo ( res ) < 0 ) res = temp ; } } return res ; } public static void main ( String [ ] args ) { String str = " ceebbaceeffo " ; int K = 3 ; System . out . print ( maxVowelSubString ( str , K ) ) ; } }
import java . util . * ; class GFG { static int minDel ( String s1 , String s2 ) { int i = s1 . length ( ) ; int j = s2 . length ( ) ; while ( i > 0 && j > 0 ) { if ( s1 . charAt ( i - 1 ) != s2 . charAt ( j - 1 ) ) { break ; } i -- ; j -- ; } return i + j ; } public static void main ( String args [ ] ) { String s1 = " geeksforgeeks " , s2 = " peeks " ; System . out . print ( minDel ( s1 , s2 ) ) ; } }
class GFG { static int minDel ( String s1 , String s2 ) { int i = 0 ; while ( i < Math . min ( s1 . length ( ) , s2 . length ( ) ) ) { if ( s1 . charAt ( i ) != s2 . charAt ( i ) ) { break ; } i ++ ; } int ans = ( ( s1 . length ( ) - i ) + ( s2 . length ( ) - i ) ) ; return ans ; } public static void main ( String [ ] args ) { String s1 = " geeks " ; String s2 = " geeksfor " ; System . out . println ( minDel ( s1 , s2 ) ) ; } }
import java . util . * ; class GFG { static void CountValidTriplet ( String s , int n ) { int count_a = 0 , count_b = 0 , count_c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' ) count_a ++ ; if ( s . charAt ( i ) == ' b ' ) count_b ++ ; if ( s . charAt ( i ) == ' c ' ) count_c ++ ; } int Total_triplet = count_a * count_b * count_c ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( 2 * j - i ) < n && s . charAt ( j ) != s . charAt ( i ) && s . charAt ( j * 2 - i ) != s . charAt ( j ) && s . charAt ( 2 * j - i ) != s . charAt ( i ) ) Total_triplet -- ; } } System . out . println ( Total_triplet ) ; } public static void main ( String [ ] args ) { String s = " abcbcabc " ; int n = s . length ( ) ; CountValidTriplet ( s , n ) ; } }
class GFG { public static boolean isBalanced ( String exp ) { boolean flag = true ; int count = 0 ; for ( int i = 0 ; i < exp . length ( ) ; i ++ ) { if ( exp . charAt ( i ) == ' ( ' ) { count ++ ; } else { count -- ; } if ( count < 0 ) { flag = false ; break ; } } if ( count != 0 ) { flag = false ; } return flag ; } public static void main ( String [ ] args ) { String exp1 = " ( ( ( ) ) ) ( ) ( ) " ; if ( isBalanced ( exp1 ) ) System . out . println ( " Balanced " ) ; else System . out . println ( " Not ▁ Balanced " ) ; String exp2 = " ( ) ) ( ( ( ) ) " ; if ( isBalanced ( exp2 ) ) System . out . println ( " Balanced " ) ; else System . out . println ( " Not ▁ Balanced " ) ; } }
import java . util . * ; class GFG { static void longestPalindrome ( String s ) { int n = s . length ( ) ; int [ ] [ ] pref = new int [ 26 ] [ n ] ; Vector < Integer > [ ] pos = new Vector [ 26 ] ; for ( int i = 0 ; i < pos . length ; i ++ ) pos [ i ] = new Vector < Integer > ( ) ; pref [ s . charAt ( 0 ) - ' a ' ] [ 0 ] ++ ; pos [ s . charAt ( 0 ) - ' a ' ] . add ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) pref [ j ] [ i ] += pref [ j ] [ i - 1 ] ; int index = s . charAt ( i ) - ' a ' ; pref [ index ] [ i ] ++ ; pos [ index ] . add ( i ) ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { int size = pos [ i ] . size ( ) ; ans = Math . max ( ans , size ) ; for ( int j = 0 ; j < size / 2 ; j ++ ) { int l = pos [ i ] . elementAt ( j ) ; int r = pos [ i ] . elementAt ( size - j - 1 ) - 1 ; for ( int k = 0 ; k < 26 ; k ++ ) { int sum = pref [ k ] [ r ] - pref [ k ] [ l ] ; ans = Math . max ( ans , 2 * ( j + 1 ) + sum ) ; } } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { String S = " bbccdcbb " ; longestPalindrome ( S ) ; } }
import java . util . * ; class GFG { static boolean checkWord ( char [ ] [ ] board , String word , int index , int row , int col ) { if ( row < 0 col < 0 row >= board . length col >= board [ 0 ] . length ) return false ; if ( board [ row ] [ col ] != word . charAt ( index ) ) return false ; else if ( index == word . length ( ) - 1 ) return true ; char temp = board [ row ] [ col ] ; board [ row ] [ col ] = ' * ' ; if ( checkWord ( board , word , index + 1 , row + 1 , col ) || checkWord ( board , word , index + 1 , row - 1 , col ) || checkWord ( board , word , index + 1 , row , col + 1 ) || checkWord ( board , word , index + 1 , row , col - 1 ) ) { board [ row ] [ col ] = temp ; return true ; } board [ row ] [ col ] = temp ; return false ; } public static void main ( String [ ] args ) { char [ ] [ ] board = { { ' A ' , ' B ' , ' C ' , ' E ' } , { ' S ' , ' F ' , ' C ' , ' S ' } , { ' A ' , ' D ' , ' E ' , ' E ' } } ; String word = " CFDASABCESEE " ; for ( int i = 0 ; i < board . length ; i ++ ) { for ( int j = 0 ; j < board [ 0 ] . length ; j ++ ) { if ( board [ i ] [ j ] == word . charAt ( 0 ) && checkWord ( board , word , 0 , i , j ) ) { System . out . println ( " True " ) ; return ; } } } System . out . println ( " False " ) ; } }
class GFG { static int count ( String N , int X , String subStr , int index , int n ) { if ( index == n ) return 0 ; String a = subStr + N . charAt ( index ) ; int b = 0 ; if ( Long . valueOf ( a ) % X == 0 ) b = 1 ; int m1 = count ( N , X , a , index + 1 , n ) ; int m2 = b + count ( N , X , " " , index + 1 , n ) ; return Math . max ( m1 , m2 ) ; } public static void main ( String [ ] args ) { String N = "00001242" ; int K = 3 ; int l = N . length ( ) ; System . out . print ( count ( N , K , " " , 0 , l ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void checkSuffix ( int A , int B ) { String s1 = String . valueOf ( A ) ; String s2 = String . valueOf ( B ) ; boolean result ; result = s1 . endsWith ( s2 ) ; if ( result ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int A = 12345 , B = 45 ; checkSuffix ( A , B ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void depthFirst ( int v , List < List < Integer > > graph , boolean [ ] visited , List < Integer > storeChain ) { visited [ v ] = true ; storeChain . add ( v ) ; for ( int i : graph . get ( v ) ) { if ( visited [ i ] == false ) { depthFirst ( i , graph , visited , storeChain ) ; } } } static void createMap ( Map < String , Character > um ) { um . put ( "000" , '0' ) ; um . put ( "001" , '1' ) ; um . put ( "010" , '2' ) ; um . put ( "011" , '3' ) ; um . put ( "100" , '4' ) ; um . put ( "101" , '5' ) ; um . put ( "110" , '6' ) ; um . put ( "111" , '7' ) ; } static String octal ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_oct_map = new HashMap < String , Character > ( ) ; createMap ( bin_oct_map ) ; int i = 0 ; String octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { octal += ' . ' ; i ++ ; } } return octal ; } static void octalValue ( List < List < Integer > > graph , int vertices , List < Integer > values ) { boolean [ ] visited = new boolean [ 1001 ] ; for ( int i = 1 ; i <= vertices ; i ++ ) { if ( visited [ i ] == false ) { int sizeChain ; List < Integer > storeChain = new ArrayList < Integer > ( ) ; depthFirst ( i , graph , visited , storeChain ) ; sizeChain = storeChain . size ( ) ; int [ ] chainValues = new int [ sizeChain + 1 ] ; for ( int j = 0 ; j < sizeChain ; j ++ ) { int temp = values . get ( storeChain . get ( j ) - 1 ) ; chainValues [ j ] = temp ; } System . out . print ( " Chain ▁ = ▁ " ) ; for ( int j = 0 ; j < sizeChain ; j ++ ) { System . out . print ( chainValues [ j ] + " ▁ " ) ; } System . out . print ( " TABSYMBOL " ) ; String s = " " ; for ( int j = 0 ; j < sizeChain ; j ++ ) { String s1 = String . valueOf ( chainValues [ j ] ) ; s += s1 ; } System . out . println ( " Octal ▁ equivalent ▁ = ▁ " + octal ( s ) ) ; } } } public static void main ( String [ ] args ) { @ SuppressWarnings ( " unchecked " ) List < List < Integer > > graph = new ArrayList ( ) ; for ( int i = 0 ; i < 1001 ; i ++ ) graph . add ( new ArrayList < Integer > ( ) ) ; int E = 4 , V = 7 ; List < Integer > values = new ArrayList < Integer > ( ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 0 ) ; values . add ( 0 ) ; values . add ( 0 ) ; values . add ( 1 ) ; values . add ( 1 ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 1 ) ; graph . get ( 3 ) . add ( 4 ) ; graph . get ( 4 ) . add ( 3 ) ; graph . get ( 4 ) . add ( 5 ) ; graph . get ( 5 ) . add ( 4 ) ; graph . get ( 6 ) . add ( 7 ) ; graph . get ( 7 ) . add ( 6 ) ; octalValue ( graph , V , values ) ; } }
class GFG { static String build_binary_str ( int p , int q , int r ) { String ans = " " ; if ( q == 0 ) { if ( p != 0 && r != 0 ) { return " - 1" ; } else { if ( p > 0 ) { ans = Strings ( p + 1 , '0' ) ; } else { ans = Strings ( r + 1 , '1' ) ; } } } else { for ( int i = 1 ; i <= q + 1 ; i ++ ) { if ( i % 2 == 0 ) { ans += '0' ; } else { ans += '1' ; } } ans = ans . substring ( 0 , 1 ) + Strings ( p , '0' ) + ans . substring ( 1 ) ; ans = Strings ( r , '1' ) + ans ; } return ans ; } static String Strings ( int p , char c ) { String ans = " " ; for ( int i = 0 ; i < p ; i ++ ) ans += c ; return ans ; } public static void main ( String [ ] args ) { int p = 1 , q = 2 , r = 2 ; System . out . print ( build_binary_str ( p , q , r ) ) ; } }
import java . util . * ; class GFG { static int get_index ( char ch ) { if ( ch == ' a ' ) return 0 ; else if ( ch == ' e ' ) return 1 ; else if ( ch == ' i ' ) return 2 ; else if ( ch == ' o ' ) return 3 ; else if ( ch == ' u ' ) return 4 ; else return - 1 ; } static int findMinLength ( String s ) { int n = s . length ( ) ; int ans = n + 1 ; int start = 0 ; int count [ ] = new int [ 5 ] ; for ( int x = 0 ; x < n ; x ++ ) { int idx = get_index ( s . charAt ( x ) ) ; if ( idx != - 1 ) { count [ idx ] ++ ; } int idx_start = get_index ( s . charAt ( start ) ) ; while ( idx_start == - 1 count [ idx_start ] > 1 ) { if ( idx_start != - 1 ) { count [ idx_start ] -- ; } start ++ ; if ( start < n ) idx_start = get_index ( s . charAt ( start ) ) ; } if ( count [ 0 ] > 0 && count [ 1 ] > 0 && count [ 2 ] > 0 && count [ 3 ] > 0 && count [ 4 ] > 0 ) { ans = Math . min ( ans , x - start + 1 ) ; } } if ( ans == n + 1 ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String s = " aaeebbeaccaaoiuooooooooiuu " ; System . out . print ( findMinLength ( s ) ) ; } }
class GFG { static int countStrings ( String s ) { int cnt = 0 ; for ( int i = 1 ; i < s . length ( ) - 1 ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { String str = " aacbb " ; System . out . println ( countStrings ( str ) ) ; } }
class GFG { static boolean checkValidString ( String str ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 ) { if ( freq [ i ] != i + 1 ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { String str = " abbcccdddd " ; if ( checkValidString ( str ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static void removeRepeating ( String s1 , String s2 ) { String s3 = s1 + " ▁ " + s2 + " ▁ " ; String words = " " ; int i = 0 ; for ( char x : s3 . toCharArray ( ) ) { if ( x == ' ▁ ' ) { if ( ! s1 . contains ( words ) || ! s2 . contains ( words ) ) System . out . print ( words ) ; words = " ▁ " ; } else { words = words + x ; } } } public static void main ( String [ ] args ) { String s1 = " I ▁ have ▁ go ▁ a ▁ pen " ; String s2 = " I ▁ want ▁ to ▁ go ▁ park " ; removeRepeating ( s1 , s2 ) ; } }
import java . util . * ; class GFG { static String convert ( String input ) { StringBuilder sbInput = new StringBuilder ( input ) ; int len = sbInput . length ( ) ; for ( int i = 0 ; i < len ; ) { if ( sbInput . charAt ( i ) == ' , ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else if ( sbInput . charAt ( i ) == ' ▁ ' ) { sbInput . deleteCharAt ( i ) ; len -- ; i -- ; } else { i ++ ; } } StringBuilder sbInputReverse = sbInput . reverse ( ) ; StringBuilder output = new StringBuilder ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( i == 2 ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else if ( i > 2 && i % 2 == 0 && i + 1 < len ) { output . append ( sbInputReverse . charAt ( i ) ) ; output . append ( " ▁ , " ) ; } else { output . append ( sbInputReverse . charAt ( i ) ) ; } } StringBuilder reverseOutput = output . reverse ( ) ; return reverseOutput . toString ( ) ; } public static void main ( String [ ] args ) { String input1 = "123 , ▁ 456 , ▁ 789" ; String input2 = "90 , ▁ 050 , ▁ 000 , ▁ 000" ; System . out . println ( convert ( input1 ) ) ; System . out . println ( convert ( input2 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void rearrange ( String str , int K ) { int n = str . length ( ) ; int s = 0 , sum = 0 , count = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int a = str . charAt ( i ) - '0' ; sum += a * Math . pow ( 2 , s ) ; s ++ ; if ( sum != 0 && sum % K == 0 ) { count ++ ; sum = 0 ; s = 0 ; } } if ( sum != 0 ) System . out . println ( " - 1" ) ; else System . out . println ( count ) ; } public static void main ( String [ ] args ) { String str = "10111001" ; int K = 5 ; rearrange ( str , K ) ; } }
import java . util . * ; class GFG { static int MOD = 26 ; static int modulo_by_26 ( String num ) { int res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) { res = ( ( res * 10 + ( int ) num . charAt ( i ) - '0' ) % MOD ) ; } return res ; } static String decrypt_message ( String s ) { String decrypted_str = " " ; String num_found_so_far = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) >= '0' && s . charAt ( i ) <= '9' ) { num_found_so_far += s . charAt ( i ) ; } else if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; num_found_so_far = " " ; } } if ( num_found_so_far . length ( ) > 0 ) { decrypted_str += ( char ) ( ' a ' + modulo_by_26 ( num_found_so_far ) ) ; } return decrypted_str ; } public static void main ( String [ ] args ) { String s = "32ytAAcV4ui30hf10hj18" ; System . out . print ( decrypt_message ( s ) ) ; } }
class GFG { static void decodeStr ( String str , int len ) { char [ ] c = new char [ len ] ; int med , pos = 1 , k ; if ( len % 2 == 1 ) med = len / 2 ; else med = len / 2 - 1 ; c [ med ] = str . charAt ( 0 ) ; if ( len % 2 == 0 ) c [ med + 1 ] = str . charAt ( 1 ) ; if ( len % 2 == 1 ) k = 1 ; else k = 2 ; for ( int i = k ; i < len ; i += 2 ) { c [ med - pos ] = str . charAt ( i ) ; if ( len % 2 == 1 ) c [ med + pos ] = str . charAt ( i + 1 ) ; else c [ med + pos + 1 ] = str . charAt ( i + 1 ) ; pos ++ ; } for ( int i = 0 ; i < len ; i ++ ) System . out . print ( c [ i ] ) ; } public static void main ( String [ ] args ) { String str = " ofrsgkeeeekgs " ; int len = str . length ( ) ; decodeStr ( str , len ) ; } }
import java . util . * ; class GFG { static int maxOverlap ( String S , String T ) { String str = T ; int count [ ] = new int [ T . length ( ) ] ; int overlap = 0 ; int max_overlap = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { int index = str . indexOf ( S . charAt ( i ) ) ; if ( index == 0 ) { overlap ++ ; if ( overlap >= 2 ) max_overlap = Math . max ( overlap , max_overlap ) ; count [ index ] ++ ; } else { if ( count [ index - 1 ] <= 0 ) return - 1 ; count [ index ] ++ ; count [ index - 1 ] -- ; } if ( index == 4 ) overlap -- ; } if ( overlap == 0 ) return max_overlap ; else return - 1 ; } public static void main ( String [ ] args ) { String S = " chcirphirp " ; String T = " chirp " ; System . out . print ( maxOverlap ( S , T ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 500001 ; static int [ ] parent = new int [ MAX ] ; static int [ ] Rank = new int [ MAX ] ; static class pair { char first , second ; public pair ( char first , char second ) { this . first = first ; this . second = second ; } } static int find ( int x ) { return parent [ x ] = parent [ x ] == x ? x : find ( parent [ x ] ) ; } static void merge ( int r1 , int r2 ) { if ( r1 != r2 ) { if ( Rank [ r1 ] > Rank [ r2 ] ) { parent [ r2 ] = r1 ; Rank [ r1 ] += Rank [ r2 ] ; } else { parent [ r1 ] = r2 ; Rank [ r2 ] += Rank [ r1 ] ; } } } static void minimumOperations ( String s1 , String s2 ) { for ( int i = 1 ; i <= 26 ; i ++ ) { parent [ i ] = i ; Rank [ i ] = 1 ; } Vector < pair > ans = new Vector < pair > ( ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( s1 . charAt ( i ) != s2 . charAt ( i ) ) { if ( find ( s1 . charAt ( i ) - 96 ) != find ( s2 . charAt ( i ) - 96 ) ) { int x = find ( s1 . charAt ( i ) - 96 ) ; int y = find ( s2 . charAt ( i ) - 96 ) ; merge ( x , y ) ; ans . add ( new pair ( s1 . charAt ( i ) , s2 . charAt ( i ) ) ) ; } } } System . out . print ( ans . size ( ) + "NEW_LINE"); for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) . first + " - > " + ans . get ( i ) . second + "NEW_LINE"); } public static void main ( String [ ] args ) { String s1 , s2 ; s1 = " abb " ; s2 = " dad " ; minimumOperations ( s1 , s2 ) ; } }
import java . util . * ; class GFG { static void String_permutation ( int n , String str ) { Stack < Integer > s = new Stack < Integer > ( ) ; String result = " " ; n = n - 1 ; for ( int i = 1 ; i < str . length ( ) + 1 ; i ++ ) { s . add ( n % i ) ; n = n / i ; } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int a = s . peek ( ) ; result += str . charAt ( a ) ; int j ; for ( j = a ; j < str . length ( ) - 1 ; j ++ ) str = str . substring ( 0 , j ) + str . charAt ( j + 1 ) + str . substring ( j + 1 ) ; str = str . substring ( 0 , j + 1 ) ; s . pop ( ) ; } System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " abcde " ; int n = 11 ; String_permutation ( n , str ) ; } }
class GFG { static String GeberateDefangIP ( String str ) { String defangIP = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char c = str . charAt ( i ) ; if ( c == ' . ' ) { defangIP += " [ . ] " ; } else { defangIP += c ; } } return defangIP ; } public static void main ( String [ ] args ) { String str = "255.100.50.0" ; System . out . println ( GeberateDefangIP ( str ) ) ; } }
import java . util . * ; class GFG { static String generateTheString ( int n ) { String ans = " " ; if ( n % 2 != 0 ) { for ( int i = 0 ; i < Math . min ( n , 24 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 24 ) { for ( int i = 0 ; i < ( n - 24 ) ; i ++ ) ans += ' a ' ; } } else { for ( int i = 0 ; i < Math . min ( n , 25 ) ; i ++ ) { ans += ( char ) ( ' b ' + i ) ; } if ( n > 25 ) { for ( int i = 0 ; i < ( n - 25 ) ; i ++ ) ans += ' a ' ; } } return ans ; } public static void main ( String [ ] args ) { int n = 34 ; System . out . println ( generateTheString ( n ) ) ; } }
import java . util . * ; class GFG { static void moveAtEnd ( String s , int i , int l ) { if ( i >= l ) return ; char curr = s . charAt ( i ) ; if ( curr != ' x ' ) System . out . print ( curr ) ; moveAtEnd ( s , i + 1 , l ) ; if ( curr == ' x ' ) System . out . print ( curr ) ; return ; } public static void main ( String args [ ] ) { String s = " geekxsforgexxeksxx " ; int l = s . length ( ) ; moveAtEnd ( s , 0 , l ) ; } }
import java . util . * ; class GFG { static void finalCoordinates ( int SX , int SY , char [ ] D ) { for ( int i = 0 ; i < D . length ; i ++ ) { if ( D [ i ] == ' N ' ) SY += 1 ; else if ( D [ i ] == ' S ' ) SY -= 1 ; else if ( D [ i ] == ' E ' ) SX += 1 ; else SX -= 1 ; } String ans = ' ( ' + String . valueOf ( SX ) + ' , ' + String . valueOf ( SY ) + ' ) ' ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int SX = 2 , SY = 2 ; String D = " NSSE " ; finalCoordinates ( SX , SY , D . toCharArray ( ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countMinFreq ( String s ) { char [ ] tempArray = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; s = new String ( tempArray ) ; int x = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == s . charAt ( 0 ) ) x ++ ; return x ; } static void countLessThan ( List < String > arr1 , List < String > arr2 ) { List < Integer > freq = new ArrayList < Integer > ( ) ; for ( String s : arr2 ) { int f = countMinFreq ( s ) ; freq . add ( f ) ; } Collections . sort ( freq ) ; for ( String s : arr1 ) { int f = countMinFreq ( s ) ; int it = upper_bound ( freq , f ) ; int cnt = freq . size ( ) - it ; System . out . print ( cnt + " ▁ " ) ; } } static int upper_bound ( List < Integer > freq , int f ) { int low = 0 , high = freq . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( freq . get ( mid ) > f ) high = mid ; else low = mid + 1 ; } return ( freq . get ( low ) < f ) ? low ++ : low ; } public static void main ( String [ ] args ) { List < String > arr1 , arr2 ; arr1 = Arrays . asList ( new String [ ] { " yyy " , " zz " } ) ; arr2 = Arrays . asList ( new String [ ] { " x " , " xx " , " xxx " , " xxxx " } ) ; countLessThan ( arr1 , arr2 ) ; } }
class GFG { static void findCount ( String s , int L , int R ) { int distinct = 0 ; int [ ] frequency = new int [ 26 ] ; for ( int i = L ; i <= R ; i ++ ) { frequency [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( frequency [ i ] > 0 ) distinct ++ ; } System . out . print ( distinct + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = " geeksforgeeksisa " + " computerscienceportal " ; int queries = 3 ; int Q [ ] [ ] = { { 0 , 10 } , { 15 , 18 } , { 12 , 20 } } ; for ( int i = 0 ; i < queries ; i ++ ) findCount ( s , Q [ i ] [ 0 ] , Q [ i ] [ 1 ] ) ; } }
class Main { static void shortestPalindrome ( String s ) { int [ ] abcd = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) abcd [ s . charAt ( i ) - 97 ] = 1 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( abcd [ i ] == 1 ) { System . out . print ( ( char ) ( i + 97 ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; shortestPalindrome ( s ) ; } }
import java . util . * ; class GFG { static String removeDuplicates ( String str ) { int n = str . length ( ) ; HashSet < Character > s = new HashSet < Character > ( ) ; String res = " " ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( ! s . contains ( str . charAt ( i ) ) ) { res = res + str . charAt ( i ) ; s . add ( str . charAt ( i ) ) ; } } res = reverse ( res ) ; return res ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( removeDuplicates ( str ) ) ; } }
import java . util . * ; class GFG { static void contain ( int L , int U , int M ) { int count = 0 ; for ( int j = L ; j < U ; j ++ ) { HashSet < String > num = new HashSet < > ( ) ; String str = Integer . toString ( j ) ; num . add ( str ) ; if ( j % M == 0 && ! num . contains ( Integer . toString ( M ) ) ) { count += 1 ; } } System . out . println ( count - 2 ) ; } public static void main ( String [ ] args ) { int L = 106 ; int U = 200 ; int M = 7 ; contain ( L , U , M ) ; } }
import java . util . * ; class GFG { static int max1 = 0 ; static int MaxLength ( Vector < String > v , int i , int m ) { if ( i >= m ) { return m - 1 ; } for ( int k = 0 ; k < 26 ; k ++ ) { char c = ( char ) ( ' a ' + k ) ; Vector < String > v1 = new Vector < String > ( ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { if ( v . get ( j ) . charAt ( i ) == c ) { v1 . add ( v . get ( j ) ) ; } } if ( v1 . size ( ) >= 2 ) { max1 = Math . max ( max1 , MaxLength ( v1 , i + 1 , m ) ) ; } else { max1 = Math . max ( max1 , i - 1 ) ; } } return max1 ; } public static void main ( String [ ] args ) { String s1 , s2 , s3 , s4 , s5 ; s1 = " abcde " ; s2 = " abcsd " ; s3 = " bcsdf " ; s4 = " abcda " ; s5 = " abced " ; Vector < String > v = new Vector < String > ( ) ; v . add ( s1 ) ; v . add ( s2 ) ; v . add ( s3 ) ; v . add ( s4 ) ; v . add ( s5 ) ; int m = v . get ( 0 ) . length ( ) ; System . out . print ( MaxLength ( v , 0 , m ) + 1 ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static boolean compare ( StringBuilder s , StringBuilder t ) { int ps , pt , i ; ps = - 1 ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == ' # ' && ps != - 1 ) ps -= 1 ; else if ( s . charAt ( i ) != ' # ' ) { s . setCharAt ( ps + 1 , s . charAt ( i ) ) ; ps += 1 ; } } pt = - 1 ; for ( i = 0 ; i < t . length ( ) ; i ++ ) { if ( t . charAt ( i ) == ' # ' && pt != - 1 ) pt -= 1 ; else if ( t . charAt ( i ) != ' # ' ) { t . setCharAt ( pt + 1 , t . charAt ( i ) ) ; pt += 1 ; } } if ( pt != ps ) return false ; else if ( ps == - 1 && pt == - 1 ) return true ; else { for ( i = 0 ; i <= pt ; i ++ ) { if ( s . charAt ( i ) != t . charAt ( i ) ) return false ; } return true ; } } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( " geee # e # ks " ) ; StringBuilder t = new StringBuilder ( " gee # # eeks " ) ; if ( compare ( s , t ) ) System . out . print ( " True " ) ; else System . out . print ( " False " ) ; } }
class GFG { static void solve ( char [ ] a , char [ ] b ) { int l = a . length ; int min = 0 , max = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( a [ i ] == ' + ' b [ i ] == ' + ' a [ i ] != b [ i ] ) max ++ ; if ( a [ i ] != ' + ' && b [ i ] != ' + ' && a [ i ] != b [ i ] ) min ++ ; } System . out . print ( min + max + "NEW_LINE"); } public static void main ( String [ ] args ) { String s1 = " a + c " , s2 = " + + b " ; solve ( s1 . toCharArray ( ) , s2 . toCharArray ( ) ) ; } }
import java . util . * ; class GFG { static int noOfDeletions ( String str , char k ) { int ans = 0 , cnt = 0 , pos = 0 ; while ( pos < str . length ( ) && str . charAt ( pos ) != k ) { pos ++ ; } int i = pos ; while ( i < str . length ( ) ) { while ( i < str . length ( ) && str . charAt ( i ) == k ) { i = i + 1 ; } ans = ans + cnt ; cnt = 0 ; while ( i < str . length ( ) && str . charAt ( i ) != k ) { i = i + 1 ; cnt = cnt + 1 ; } } return ans ; } public static void main ( String [ ] args ) { String str1 = " ababababa " ; char k1 = ' a ' ; System . out . print ( noOfDeletions ( str1 , k1 ) + "NEW_LINE"); String str2 = " kprkkoinkopt " ; char k2 = ' k ' ; System . out . print ( noOfDeletions ( str2 , k2 ) + "NEW_LINE"); } }
class GFG { 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 ) { return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } static void checkCorrespond ( String [ ] str , int x ) { int counter = 0 ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 ; j <= 36 ; j ++ ) { if ( toDeci ( str [ i ] , j ) == x ) { counter ++ ; break ; } } } if ( counter == n ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } public static void main ( String [ ] args ) { int x = 16 ; String [ ] str = { "10000" , "20" , "16" } ; checkCorrespond ( str , x ) ; } }
import java . util . * ; class GFG { static String removeOddFrequencyCharacters ( String s ) { HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { char p = s . charAt ( i ) ; Integer count = m . get ( p ) ; if ( count == null ) { count = 0 ; m . put ( p , 1 ) ; } else m . put ( p , count + 1 ) ; } String new_string = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( m . get ( s . charAt ( i ) ) & 1 ) == 1 ) continue ; new_string += s . charAt ( i ) ; } return new_string ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; str = removeOddFrequencyCharacters ( str ) ; System . out . print ( str ) ; } }
import java . util . * ; class GFG { static int LexicoLesserStrings ( String s ) { int count = 0 ; int len ; len = s . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { count += ( s . charAt ( i ) - ' a ' ) * Math . pow ( 26 , len - i - 1 ) ; } return count ; } static int countString ( String S1 , String S2 ) { int countS1 , countS2 , totalString ; countS1 = LexicoLesserStrings ( S1 ) ; countS2 = LexicoLesserStrings ( S2 ) ; totalString = countS2 - countS1 - 1 ; return ( totalString < 0 ? 0 : totalString ) ; } public static void main ( String args [ ] ) { String S1 , S2 ; S1 = " cda " ; S2 = " cef " ; System . out . println ( countString ( S1 , S2 ) ) ; } }
class GFG { static boolean isVowel ( char x ) { x = Character . toLowerCase ( x ) ; return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static String longestVowelSubsequence ( String str ) { String answer = " " ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) { answer += str . charAt ( i ) ; } } return answer ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( longestVowelSubsequence ( str ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void findMaxFrequency ( String s ) { int arr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) arr [ s . charAt ( i ) - ' a ' ] ++ ; HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) if ( arr [ i ] != 0 ) { if ( hash . containsKey ( arr [ i ] ) ) { hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } else { hash . put ( arr [ i ] , 1 ) ; } } int max_count = 0 , res = - 1 ; for ( Map . Entry < Integer , Integer > i : hash . entrySet ( ) ) { if ( max_count < i . getValue ( ) ) { res = i . getKey ( ) ; max_count = i . getValue ( ) ; } } System . out . println ( " Frequency ▁ " + res + " ▁ is ▁ repeated ▁ " + max_count + " ▁ times " ) ; } public static void main ( String [ ] args ) { String s = " geeksgeeks " ; findMaxFrequency ( s ) ; } }
class GFG { static void findString ( String A , String B ) { int dist = 0 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) ) dist ++ ; } if ( ( dist & 1 ) == 1 ) { System . out . println ( " Not ▁ Possible " ) ; } else { String res = " " ; int K = ( int ) dist / 2 ; for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != B . charAt ( i ) && K > 0 ) { res += B . charAt ( i ) ; K -- ; } else if ( A . charAt ( i ) != B . charAt ( i ) ) { res += A . charAt ( i ) ; } else { res += A . charAt ( i ) ; } } System . out . println ( res ) ; } } public static void main ( String [ ] args ) { String A = "1001010" ; String B = "0101010" ; findString ( A , B ) ; } }
class GFG { static void maximumSuffix ( String s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ ] = new int [ 26 ] ; String suffix = " " ; while ( i > - 1 ) { int index = s . charAt ( i ) - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s . charAt ( i ) ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } suffix = reverse ( suffix ) ; System . out . print ( suffix ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; } }
import java . io . * ; class GFG { static String xoring ( String a , String b , int n ) { String ans = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( a . charAt ( i ) == b . charAt ( i ) ) ans += "0" ; else ans += "1" ; } return ans ; } public static void main ( String [ ] args ) { String a = "1010" ; String b = "1101" ; int n = a . length ( ) ; String c = xoring ( a , b , n ) ; System . out . println ( c ) ; } }
class GFG { static String findLargest ( int N ) { String largest = strings ( N , '7' ) ; return largest ; } static String findSmallest ( int N ) { String smallest = "1" + strings ( ( N - 1 ) , '0' ) ; return smallest ; } private static String strings ( int N , char c ) { String temp = " " ; for ( int i = 0 ; i < N ; i ++ ) { temp += c ; } return temp ; } static void printLargestSmallest ( int N ) { System . out . print ( " Largest : ▁ " + findLargest ( N ) + "NEW_LINE"); System . out . print ( " Smallest : ▁ " + findSmallest ( N ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; printLargestSmallest ( N ) ; } }
import java . util . * ; class GFG { static long countSubstr ( String s , int k ) { int n = s . length ( ) ; int l = n - 1 ; int r = n - 1 ; int [ ] arr = new int [ n ] ; int last_indexof1 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) { arr [ i ] = i ; last_indexof1 = i ; } else { arr [ i ] = last_indexof1 ; } } long no_of_substr = 0 ; for ( r = n - 1 ; r >= 0 ; r -- ) { l = r ; while ( l >= 0 && ( r - l + 1 ) <= 64 && Integer . valueOf ( s . substring ( l , r + 1 ) , 2 ) < k ) { l -- ; } if ( r - l + 1 <= 64 ) no_of_substr += l + 1 ; else { no_of_substr += arr [ l + 1 ] + 1 ; } } return no_of_substr ; } public static void main ( String [ ] args ) { String s = "11100" ; int k = 3 ; System . out . println ( countSubstr ( s , k ) ) ; } }
import java . util . * ; class GFG { static boolean isPerfectCubeString ( String str ) { int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) sum += ( int ) str . charAt ( i ) ; double cr = Math . round ( Math . cbrt ( sum ) ) ; return ( cr * cr * cr == sum ) ; } public static void main ( String [ ] args ) { String str = " ll " ; if ( isPerfectCubeString ( str ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static void CamelCase ( ArrayList < String > words , String pattern ) { Map < String , List < String > > map = new HashMap < String , List < String > > ( ) ; for ( int i = 0 ; i < words . size ( ) ; i ++ ) { String str = " " ; int l = words . get ( i ) . length ( ) ; for ( int j = 0 ; j < l ; j ++ ) { if ( words . get ( i ) . charAt ( j ) >= ' A ' && words . get ( i ) . charAt ( j ) <= ' Z ' ) { str += words . get ( i ) . charAt ( j ) ; map . put ( str , list ( map . get ( str ) , words . get ( i ) ) ) ; } } } boolean wordFound = false ; for ( Map . Entry < String , List < String > > it : map . entrySet ( ) ) { if ( it . getKey ( ) . equals ( pattern ) ) { wordFound = true ; for ( String s : it . getValue ( ) ) System . out . print ( s + "NEW_LINE"); } } if ( ! wordFound ) { System . out . print ( " No ▁ match ▁ found " ) ; } } private static List < String > list ( List < String > list , String str ) { List < String > temp = new ArrayList < String > ( ) ; if ( list != null ) temp . addAll ( list ) ; temp . add ( str ) ; return temp ; } public static void main ( String [ ] args ) { String arr [ ] = { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; ArrayList < String > words = new ArrayList < String > ( Arrays . asList ( arr ) ) ; String pattern = " HT " ; CamelCase ( words , pattern ) ; } }
class GFG { static String encryption ( char [ ] s ) { int l = s . length ; int b = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int a = ( int ) Math . floor ( Math . sqrt ( l ) ) ; String encrypted = " " ; if ( b * a < l ) { if ( Math . min ( b , a ) == b ) { b = b + 1 ; } else { a = a + 1 ; } } char [ ] [ ] arr = new char [ a ] [ b ] ; int k = 0 ; for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { encrypted = encrypted + arr [ i ] [ j ] ; } } return encrypted ; } static String decryption ( char [ ] s ) { int l = s . length ; int b = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int a = ( int ) Math . floor ( Math . sqrt ( l ) ) ; String decrypted = " " ; char [ ] [ ] arr = new char [ a ] [ b ] ; int k = 0 ; for ( int j = 0 ; j < b ; j ++ ) { for ( int i = 0 ; i < a ; i ++ ) { if ( k < l ) { arr [ j ] [ i ] = s [ k ] ; } k ++ ; } } for ( int j = 0 ; j < a ; j ++ ) { for ( int i = 0 ; i < b ; i ++ ) { decrypted = decrypted + arr [ i ] [ j ] ; } } return decrypted ; } public static void main ( String [ ] args ) { String s = " Geeks ▁ For ▁ Geeks " ; String encrypted ; String decrypted ; encrypted = encryption ( s . toCharArray ( ) ) ; System . out . print ( encrypted + "NEW_LINE"); decrypted = decryption ( encrypted . toCharArray ( ) ) ; System . out . print ( decrypted ) ; } }
class GFG { static int checkIfStartsWithCapital ( String str ) { if ( str . charAt ( 0 ) >= ' A ' && str . charAt ( 0 ) <= ' Z ' ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithCapital ( str ) == 1 ) System . out . println ( " Accepted " ) ; else System . out . println ( " Not ▁ Accepted " ) ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; check ( str ) ; str = " geeksforgeeks " ; check ( str ) ; } }
import java . util . * ; class GFG { static void findMostOccurringChar ( Vector < String > str ) { int [ ] hash = new int [ 26 ] ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { for ( int j = 0 ; j < str . get ( i ) . length ( ) ; j ++ ) { hash [ str . get ( i ) . charAt ( j ) - 97 ] ++ ; } } int max = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { max = hash [ i ] > hash [ max ] ? i : max ; } System . out . print ( ( char ) ( max + 97 ) + "NEW_LINE"); } public static void main ( String [ ] args ) { Vector < String > str = new Vector < String > ( ) ; str . add ( " animal " ) ; str . add ( " zebra " ) ; str . add ( " lion " ) ; str . add ( " giraffe " ) ; findMostOccurringChar ( str ) ; } }
class GFG { static int checkIfAllVowels ( String str ) { int [ ] hash = new int [ 5 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' A ' || str . charAt ( i ) == ' a ' ) hash [ 0 ] = 1 ; else if ( str . charAt ( i ) == ' E ' || str . charAt ( i ) == ' e ' ) hash [ 1 ] = 1 ; else if ( str . charAt ( i ) == ' I ' || str . charAt ( i ) == ' i ' ) hash [ 2 ] = 1 ; else if ( str . charAt ( i ) == ' O ' || str . charAt ( i ) == ' o ' ) hash [ 3 ] = 1 ; else if ( str . charAt ( i ) == ' U ' || str . charAt ( i ) == ' u ' ) hash [ 4 ] = 1 ; } for ( int i = 0 ; i < 5 ; i ++ ) { if ( hash [ i ] == 0 ) { return 1 ; } } return 0 ; } static void checkIfAllVowelsArePresent ( String str ) { if ( checkIfAllVowels ( str ) == 1 ) System . out . print ( "Not AcceptedNEW_LINE"); else System . out . print ( "AcceptedNEW_LINE"); } public static void main ( String [ ] args ) { String str = " aeioubc " ; checkIfAllVowelsArePresent ( str ) ; } }
import java . util . * ; class GFG { public static int smallest ( String s ) { int [ ] a = new int [ s . length ( ) ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) a [ i ] = s . charAt ( i ) - '0' ; Vector < Integer > b = new Vector < Integer > ( ) ; for ( int i = 0 ; i < a . length ; i ++ ) { if ( a [ i ] % 2 != 0 ) b . add ( a [ i ] ) ; } Collections . sort ( b ) ; if ( b . size ( ) > 1 ) return ( b . get ( 0 ) ) * 10 + ( b . get ( 1 ) ) ; return - 1 ; } public static void main ( String [ ] args ) { System . out . print ( smallest ( "15470" ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Main { static void converthenumber ( int n ) { String s = Integer . toString ( n ) ; String res = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' || s . charAt ( i ) == '9' ) res += s . charAt ( i ) ; if ( res . length ( ) == 2 ) break ; } if ( res . length ( ) == 2 ) System . out . println ( res ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 18720 ; converthenumber ( n ) ; } }
class GFG { static void longestPalindrome ( String a [ ] , int n ) { String [ ] pair1 = new String [ n ] ; String [ ] pair2 = new String [ n ] ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; s = reverse ( s ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ i ] != " " && a [ j ] != " " ) { if ( s . equals ( a [ j ] ) ) { pair1 [ r ] = a [ i ] ; pair2 [ r ++ ] = a [ j ] ; a [ i ] = " " ; a [ j ] = " " ; break ; } } } } String s1 = " " ; for ( int i = 0 ; i < n ; i ++ ) { String s = a [ i ] ; a [ i ] = reverse ( a [ i ] ) ; if ( a [ i ] != " " ) { if ( a [ i ] . equals ( s ) ) { s1 = a [ i ] ; break ; } } } String ans = " " ; for ( int i = 0 ; i < r ; i ++ ) { ans = ans + pair1 [ i ] ; } if ( s1 != " " ) { ans = ans + s1 ; } for ( int j = r - 1 ; j >= 0 ; j -- ) { ans = ans + pair2 [ j ] ; } System . out . print ( ans + "NEW_LINE"); } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String [ ] a1 = { " aba " , " aba " } ; int n1 = a1 . length ; longestPalindrome ( a1 , n1 ) ; String [ ] a2 = { " abc " , " dba " , " kop " , " abd " , " cba " } ; int n2 = a2 . length ; longestPalindrome ( a2 , n2 ) ; } }
import java . util . * ; class GFG { static class TrieNode { char val ; HashMap < Character , TrieNode > children ; int visited ; TrieNode ( char x ) { val = x ; visited = 0 ; children = new HashMap < > ( ) ; } } static class Trie { TrieNode head ; int ans ; Trie ( ) { head = new TrieNode ( ' \0' ) ; ans = 0 ; } void addWord ( String s ) { TrieNode temp = head ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( temp . children . get ( ch ) == null ) { temp . children . put ( ch , new TrieNode ( ch ) ) ; } temp = temp . children . get ( ch ) ; temp . visited ++ ; } } int dfs ( TrieNode node , int depth ) { int vis = 0 ; Iterator hmIterator = node . children . entrySet ( ) . iterator ( ) ; while ( hmIterator . hasNext ( ) ) { Map . Entry child = ( Map . Entry ) hmIterator . next ( ) ; vis += dfs ( ( TrieNode ) child . getValue ( ) , depth + 1 ) ; } node . visited -= vis ; int String_pair = 0 ; if ( node . visited > 1 ) { String_pair = ( node . visited / 2 ) ; ans += ( depth * String_pair ) ; node . visited -= ( 2 * String_pair ) ; } return ( 2 * String_pair + vis ) ; } int dfshelper ( ) { ans = 0 ; dfs ( head , 0 ) ; return ans ; } } public static void main ( String args [ ] ) { Trie T = new Trie ( ) ; String str [ ] = { " babab " , " ababb " , " abbab " , " aaaaa " , " babaa " , " babbb " } ; int n = 6 ; for ( int i = 0 ; i < n ; i ++ ) { T . addWord ( str [ i ] ) ; } int ans = T . dfshelper ( ) ; System . out . println ( ans ) ; } }
class GFG { static int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; boolean isEndOfWord ; public TrieNode ( ) { super ( ) ; } } ; static TrieNode getNode ( ) { TrieNode pNode = new TrieNode ( ) ; pNode . isEndOfWord = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) pNode . children [ i ] = null ; return pNode ; } static void insert ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = getNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isEndOfWord = true ; } static boolean search ( TrieNode root , String key ) { TrieNode pCrawl = root ; for ( int i = 0 ; i < key . length ( ) ; i ++ ) { int index = key . charAt ( i ) - ' a ' ; if ( pCrawl . children [ index ] == null ) return false ; pCrawl = pCrawl . children [ index ] ; } return ( pCrawl != null && pCrawl . isEndOfWord ) ; } static void wordBreakAll ( TrieNode root , String word , int n , String result ) { for ( int i = 1 ; i <= n ; i ++ ) { String prefix = word . substring ( 0 , i ) ; if ( search ( root , prefix ) ) { if ( i == n ) { result += prefix ; System . out . print ( " TABSYMBOL " + result + "NEW_LINE"); return ; } wordBreakAll ( root , word . substring ( i , n ) , n - i , result + prefix + " ▁ " ) ; } } } public static void main ( String [ ] args ) { new TrieNode ( ) ; TrieNode root = getNode ( ) ; String dictionary [ ] = { " sam " , " sung " , " samsung " } ; int n = dictionary . length ; for ( int i = 0 ; i < n ; i ++ ) { insert ( root , dictionary [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( dictionary [ i ] + ": NEW_LINE"); wordBreakAll ( root , dictionary [ i ] , dictionary [ i ] . length ( ) , " " ) ; } } }
import java . util . * ; class GFG { static class node { char data ; int cnt ; node left , right ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . cnt = 1 ; newnode . left = newnode . right = null ; return newnode ; } static node addinlvlorder ( node root , char data ) { if ( root == null ) { return add ( data ) ; } Queue < node > Q = new LinkedList < node > ( ) ; Q . add ( root ) ; while ( ! Q . isEmpty ( ) ) { node temp = Q . peek ( ) ; Q . remove ( ) ; if ( temp . data == data ) { temp . cnt ++ ; break ; } if ( temp . left == null ) { temp . left = add ( data ) ; break ; } else { if ( temp . left . data == data ) { temp . left . cnt ++ ; break ; } Q . add ( temp . left ) ; } if ( temp . right == null ) { temp . right = add ( data ) ; break ; } else { if ( temp . right . data == data ) { temp . right . cnt ++ ; break ; } Q . add ( temp . right ) ; } } return root ; } static void printlvlorder ( node root ) { Queue < node > Q = new LinkedList < node > ( ) ; Q . add ( root ) ; while ( ! Q . isEmpty ( ) ) { node temp = Q . peek ( ) ; if ( temp . cnt > 1 ) { System . out . print ( ( temp . data + " " + temp . cnt ) ) ; } else { System . out . print ( ( char ) temp . data ) ; } Q . remove ( ) ; if ( temp . left != null ) { Q . add ( temp . left ) ; } if ( temp . right != null ) { Q . add ( temp . right ) ; } } } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; node root = null ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { root = addinlvlorder ( root , s . charAt ( i ) ) ; } printlvlorder ( root ) ; } }
class GFG { static String replaceRemove ( char [ ] s , char X , char Y , char Z ) { int start = 0 , end = s . length - 1 ; while ( start <= end ) { if ( s [ start ] == Z ) { while ( end >= 0 && s [ end ] == Z ) { end -- ; } char temp ; if ( end > start ) { temp = s [ start ] ; s [ start ] = s [ end ] ; s [ end ] = temp ; if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } else { if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } String new_s = new String ( s ) ; while ( new_s . length ( ) > 0 && new_s . charAt ( new_s . length ( ) - 1 ) == Z ) { new_s = new_s . substring ( 0 , new_s . length ( ) - 1 ) ; } return new_s ; } public static void main ( String [ ] args ) { String str = " batman " ; char X = ' a ' , Y = ' d ' , Z = ' b ' ; str = replaceRemove ( str . toCharArray ( ) , X , Y , Z ) ; if ( str . length ( ) == 0 ) { System . out . println ( - 1 ) ; } else { System . out . println ( str ) ; } } }
class GFG { static String canMake ( String s ) { int o = 0 , z = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) - '0' == 1 ) o ++ ; else z ++ ; } if ( o % 2 == 1 && z % 2 == 1 ) return " NO " ; else return " YES " ; } public static void main ( String [ ] args ) { String s = "01011" ; System . out . println ( canMake ( s ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int count = 0 ; public static void countNumberOfStringsUtil ( int [ ] freq ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) { freq [ i ] -- ; count ++ ; countNumberOfStringsUtil ( freq ) ; } } } public static int countNumberOfStrings ( String s ) { int [ ] freq = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { freq [ s . charAt ( i ) - ' A ' ] ++ ; } countNumberOfStringsUtil ( freq ) ; return count ; } public static void main ( String [ ] args ) { String s = " AAABBC " ; System . out . println ( countNumberOfStrings ( s ) ) ; } }
class GFG { static int countSubStr ( String str , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) ans += ( i + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = "10010" ; int len = str . length ( ) ; System . out . println ( countSubStr ( str , len ) ) ; } }
class GFG { static final int INT_MAX = Integer . MAX_VALUE ; static int findLength ( String s , int k ) { int ans = INT_MAX ; int l = 0 ; int r = 0 ; int cnt_zero = 0 , cnt_one = 0 ; while ( l < s . length ( ) && r <= s . length ( ) ) { if ( cnt_zero >= k && cnt_one >= 1 ) { ans = Math . min ( ans , r - l ) ; l ++ ; if ( s . charAt ( l - 1 ) == '0' ) cnt_zero -- ; else cnt_one -- ; } else { if ( r == s . length ( ) ) break ; if ( s . charAt ( r ) == '0' ) cnt_zero ++ ; else cnt_one ++ ; r ++ ; } } if ( ans == INT_MAX ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String s = "100" ; int k = 2 ; System . out . println ( findLength ( s , k ) ) ; } }
class GFG { static String largestSubStr ( String s ) { while ( s . length ( ) != 0 && s . charAt ( s . length ( ) - 1 ) == '1' ) s = s . substring ( 0 , s . length ( ) - 1 ) ; if ( s . length ( ) == 0 ) return " - 1" ; else return s ; } public static void main ( String [ ] args ) { String s = "11001" ; System . out . println ( largestSubStr ( s ) ) ; } }
class GFG { static int MAX = 26 ; static boolean areEqual ( int [ ] a , int [ ] b ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( a [ i ] != b [ i ] ) return false ; return true ; } static boolean xExistsInY ( String x , String y ) { if ( x . length ( ) > y . length ( ) ) return false ; int [ ] cnt_x = new int [ MAX ] ; int [ ] cnt = new int [ MAX ] ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) cnt_x [ x . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) cnt [ y . charAt ( i ) - ' a ' ] ++ ; if ( areEqual ( cnt_x , cnt ) ) return true ; for ( int i = 1 ; i < y . length ( ) - x . length ( ) + 1 ; i ++ ) { cnt [ y . charAt ( i - 1 ) - ' a ' ] -- ; cnt [ y . charAt ( i + x . length ( ) - 1 ) - ' a ' ] ++ ; if ( areEqual ( cnt , cnt_x ) ) return true ; } return false ; } public static void main ( String [ ] args ) { String x = " skege " ; String y = " geeksforgeeks " ; if ( xExistsInY ( x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void reverse ( char A [ ] , int l , int h ) { if ( l < h ) { char ch = A [ l ] ; A [ l ] = A [ h ] ; A [ h ] = ch ; reverse ( A , l + 1 , h - 1 ) ; } } static String reverseParentheses ( String str , int len ) { Stack < Integer > st = new Stack < Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { st . push ( i ) ; } else if ( str . charAt ( i ) == ' ) ' ) { char [ ] A = str . toCharArray ( ) ; reverse ( A , st . peek ( ) + 1 , i ) ; str = String . copyValueOf ( A ) ; st . pop ( ) ; } } String res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) != ' ) ' && str . charAt ( i ) != ' ( ' ) { res += ( str . charAt ( i ) ) ; } } return res ; } public static void main ( String [ ] args ) { String str = " ( skeeg ( for ) skeeg ) " ; int len = str . length ( ) ; System . out . println ( reverseParentheses ( str , len ) ) ; } }
class GFG { static int MAX = 26 ; static void updateFreq ( String str , int freq [ ] ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } } static int maxCount ( String str , String patt ) { int [ ] strFreq = new int [ MAX ] ; updateFreq ( str , strFreq ) ; int [ ] pattFreq = new int [ MAX ] ; updateFreq ( patt , pattFreq ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = Math . min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " geeks " ; System . out . print ( maxCount ( str , patt ) ) ; } }
class GFG { static int TEN = 10 ; static int minNum ( char [ ] str , int len ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < len ; i ++ ) { if ( str [ i ] == '4' str [ i ] == '8' ) { res = Math . min ( res , str [ i ] - '0' ) ; } } for ( int i = 0 ; i < len - 1 ; i ++ ) { for ( int j = i + 1 ; j < len ; j ++ ) { int num = ( str [ i ] - '0' ) * TEN + ( str [ j ] - '0' ) ; if ( num % 4 == 0 ) { res = Math . min ( res , num ) ; } } } return ( ( res == Integer . MAX_VALUE ) ? - 1 : res ) ; } public static void main ( String [ ] args ) { String str = "17" ; int len = str . length ( ) ; System . out . print ( minNum ( str . toCharArray ( ) , len ) ) ; } }
import java . util . * ; class GFG { static boolean canBeEqual ( char [ ] a , char [ ] b , int n ) { Vector < Character > A = new Vector < > ( ) ; Vector < Character > B = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . add ( a [ i ] ) ; B . add ( b [ i ] ) ; } } if ( A . size ( ) == B . size ( ) && B . size ( ) == 0 ) return true ; if ( A . size ( ) == B . size ( ) && B . size ( ) == 2 ) { if ( A . get ( 0 ) == A . get ( 1 ) && B . get ( 0 ) == B . get ( 1 ) ) return true ; } return false ; } public static void main ( String [ ] args ) { char [ ] A = " SEEKSFORGEEKS " . toCharArray ( ) ; char [ ] B = " GEEKSFORGEEKG " . toCharArray ( ) ; if ( canBeEqual ( A , B , A . length ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
class GFG { static void findPhoneNumber ( int n ) { int temp = n ; int sum = 0 ; while ( temp != 0 ) { sum += temp % 10 ; temp = temp / 10 ; } if ( sum < 10 ) System . out . print ( n + "0" + sum ) ; else System . out . print ( n + " " + sum ) ; } public static void main ( String [ ] args ) { int n = 98765432 ; findPhoneNumber ( n ) ; } }
class GFG { static void findMidAlphabet ( String s1 , String s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int mid = ( s1 . charAt ( i ) + s2 . charAt ( i ) ) / 2 ; System . out . print ( ( char ) mid ) ; } } public static void main ( String [ ] args ) { String s1 = " akzbqzgw " ; String s2 = " efhctcsz " ; int n = s1 . length ( ) ; findMidAlphabet ( s1 , s2 , n ) ; } }
class GFG { static int sumDigits ( int n ) { int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; sum += digit ; n /= 10 ; } return sum ; } static boolean isDivBySix ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( str . charAt ( i ) - ' a ' + 1 ) ; } if ( sum % 3 != 0 ) return false ; int lastDigit = ( ( int ) ( str . charAt ( n - 1 ) - ' a ' + 1 ) ) % 10 ; if ( lastDigit % 2 != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " ab " ; int n = str . length ( ) ; if ( isDivBySix ( str , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static char [ ] CHARS = "0123456789ABCDEF " . toCharArray ( ) ; static int DIGITS = 16 ; static boolean isDivisible ( String s , int m ) { Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < DIGITS ; i ++ ) { mp . put ( CHARS [ i ] , i ) ; } int r = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { r = ( r * 16 + mp . get ( s . charAt ( i ) ) ) % m ; } if ( r == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = "10" ; int m = 3 ; if ( isDivisible ( s , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int cntSplits ( String s ) { if ( s . charAt ( s . length ( ) - 1 ) == '1' ) return - 1 ; int ans = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) ans += ( s . charAt ( i ) == '0' ) ? 1 : 0 ; return ans ; } public static void main ( String [ ] args ) { String s = "10010" ; System . out . println ( cntSplits ( s ) ) ; } }
public class GFG { public static boolean isPalindrome ( float num ) { String s = String . valueOf ( num ) ; int low = 0 ; int high = s . length ( ) - 1 ; while ( low < high ) { if ( s . charAt ( low ) != s . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } public static void main ( String args [ ] ) { float n = 123.321f ; if ( isPalindrome ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { final static int MAX = 26 ; static int maxSubStr ( char [ ] str1 , int len1 , char [ ] str2 , int len2 ) { if ( len1 > len2 ) return 0 ; int freq1 [ ] = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) freq1 [ i ] = 0 ; for ( int i = 0 ; i < len1 ; i ++ ) freq1 [ str1 [ i ] - ' a ' ] ++ ; int freq2 [ ] = new int [ MAX ] ; for ( int i = 0 ; i < len2 ; i ++ ) freq2 [ i ] = 0 ; for ( int i = 0 ; i < len2 ; i ++ ) freq2 [ str2 [ i ] - ' a ' ] ++ ; int minPoss = Integer . MAX_VALUE ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; if ( freq1 [ i ] > freq2 [ i ] ) return 0 ; minPoss = Math . min ( minPoss , freq2 [ i ] / freq1 [ i ] ) ; } return minPoss ; } public static void main ( String [ ] args ) { String str1 = " geeks " , str2 = " gskefrgoekees " ; int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; System . out . println ( maxSubStr ( str1 . toCharArray ( ) , len1 , str2 . toCharArray ( ) , len2 ) ) ; } }
import java . util . * ; class GFG { static int cntWays ( String str , int n ) { int x = n + 1 ; int ways = x * x * ( x * x - 1 ) / 12 ; return ways ; } public static void main ( String [ ] args ) { String str = " ab " ; int n = str . length ( ) ; System . out . println ( cntWays ( str , n ) ) ; } }
class GFG { static int Count ( String s , int pos ) { int c = s . charAt ( pos - 1 ) ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s . charAt ( i ) == c ) counter = counter + 1 ; } return counter ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . length ( ) ; int query [ ] = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; System . out . println ( Count ( s , pos ) ) ; } } }
import java . util . * ; class GFG { static void Count ( int [ ] temp ) { int [ ] query = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { int pos = query [ i ] ; System . out . println ( temp [ pos - 1 ] ) ; } } static int [ ] processing ( String s , int len ) { int [ ] temp = new int [ len ] ; HashMap < Character , Integer > d = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! d . containsKey ( s . charAt ( i ) ) ) { d . put ( s . charAt ( i ) , i ) ; } else { temp [ i ] = temp [ d . get ( s . charAt ( i ) ) ] + 1 ; d . put ( s . charAt ( i ) , i ) ; } } return temp ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int n = s . length ( ) ; int [ ] temp = processing ( s , n ) ; Count ( temp ) ; } }
import java . util . * ; class GFG { static int sticks [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { cnt += ( sticks [ str . charAt ( i ) - '0' ] ) ; } return cnt ; } public static void main ( String [ ] args ) { String str = "56" ; int n = str . length ( ) ; System . out . println ( countSticks ( str , n ) ) ; } }
import java . util . * ; class GFG { static void printChar ( char [ ] arr , int len ) { HashMap < Character , Integer > occ = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( occ . containsKey ( arr [ i ] ) ) { occ . put ( arr [ i ] , occ . get ( arr [ i ] ) + 1 ) ; } else { occ . put ( arr [ i ] , 1 ) ; } int size = occ . size ( ) ; while ( size -- > 0 ) { int currentMax = 0 ; char arg_max = 0 ; for ( Map . Entry < Character , Integer > it : occ . entrySet ( ) ) { if ( it . getValue ( ) > currentMax || ( it . getValue ( ) == currentMax && it . getKey ( ) > arg_max ) ) { arg_max = it . getKey ( ) ; currentMax = it . getValue ( ) ; } } System . out . print ( arg_max + " ▁ - ▁ " + currentMax + "NEW_LINE"); occ . remove ( arg_max ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; printChar ( str . toCharArray ( ) , len ) ; } }
import java . util . * ; class GFG { static Set < String > uSet = new HashSet < String > ( ) ; static int minCnt = Integer . MAX_VALUE ; static void findSubStr ( String str , int cnt , int start ) { if ( start == str . length ( ) ) { minCnt = Math . min ( cnt , minCnt ) ; } for ( int len = 1 ; len <= ( str . length ( ) - start ) ; len ++ ) { String subStr = str . substring ( start , start + len ) ; if ( uSet . contains ( subStr ) ) { findSubStr ( str , cnt + 1 , start + len ) ; } } } static void findMinSubStr ( String arr [ ] , int n , String str ) { for ( int i = 0 ; i < n ; i ++ ) uSet . add ( arr [ i ] ) ; findSubStr ( str , 0 , 0 ) ; } public static void main ( String args [ ] ) { String str = "123456" ; String arr [ ] = { "1" , "12345" , "2345" , "56" , "23" , "456" } ; int n = arr . length ; findMinSubStr ( arr , n , str ) ; System . out . print ( minCnt ) ; } }
import java . util . * ; class GFG { static int getMod ( String str , int n , int k ) { int pwrTwo [ ] = new int [ n ] ; pwrTwo [ 0 ] = 1 % k ; for ( int i = 1 ; i < n ; i ++ ) { pwrTwo [ i ] = pwrTwo [ i - 1 ] * ( 2 % k ) ; pwrTwo [ i ] %= k ; } int res = 0 ; int i = 0 , j = n - 1 ; while ( i < n ) { if ( str . charAt ( j ) == '1' ) { res += ( pwrTwo [ i ] ) ; res %= k ; } i ++ ; j -- ; } return res ; } public static void main ( String [ ] args ) { String str = "1101" ; int n = str . length ( ) ; int k = 45 ; System . out . print ( getMod ( str , n , k ) ) ; } }
import java . util . * ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { String ans = " " ; for ( int j = i ; j < ( int ) s . length ( ) ; j ++ ) { ans += s . charAt ( j ) ; if ( ans . length ( ) == n ) { st . add ( ans ) ; break ; } } } for ( String it : st ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }
class GFG { static void findNumbers ( String s ) { int n = s . length ( ) ; int count = 1 ; int result = 0 ; int left = 0 ; int right = 1 ; while ( right < n ) { if ( s . charAt ( left ) == s . charAt ( right ) ) { count ++ ; } else { result += count * ( count + 1 ) / 2 ; left = right ; count = 1 ; } right ++ ; } result += count * ( count + 1 ) / 2 ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { String s = " bbbcbb " ; findNumbers ( s ) ; } }
import java . util . Vector ; class GFG { static boolean canBeBalanced ( String s , int n ) { if ( n % 2 == 1 ) return false ; String k = " ( " ; k += s + " ) " ; Vector < String > d = new Vector < > ( ) ; for ( int i = 0 ; i < k . length ( ) ; i ++ ) { if ( k . charAt ( i ) == ' ( ' ) d . add ( " ( " ) ; else { if ( d . size ( ) != 0 ) d . remove ( d . size ( ) - 1 ) ; else return false ; } } if ( d . isEmpty ( ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ) ( ( ) " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int Find_Index ( String str1 , String str2 ) { int n = str1 . length ( ) ; int m = str2 . length ( ) ; int l = 0 ; int r = 0 ; if ( n != m + 1 ) { return - 1 ; } for ( int i = 0 ; i < m ; i ++ ) { if ( str1 . charAt ( i ) == str2 . charAt ( i ) ) { l += 1 ; } else { break ; } } int i = n - 1 ; int j = m - 1 ; while ( i >= 0 && j >= 0 && str1 . charAt ( i ) == str2 . charAt ( j ) ) { r += 1 ; i -= 1 ; j -= 1 ; } if ( l + r < m ) { return - 1 ; } else { i = Math . max ( n - r , 1 ) ; j = Math . min ( l + 1 , n ) ; return ( j - i + 1 ) ; } } public static void main ( String [ ] args ) { String str1 = " aaa " , str2 = " aa " ; System . out . println ( Find_Index ( str1 , str2 ) ) ; } }
public class GFG { static void expandString ( String strin ) { String temp = " " ; int j ; for ( int i = 0 ; i < strin . length ( ) ; i ++ ) { if ( strin . charAt ( i ) >= 0 ) { int num = strin . charAt ( i ) - '0' ; if ( strin . charAt ( i + 1 ) == ' ( ' ) { for ( j = i + 1 ; strin . charAt ( j ) != ' ) ' ; j ++ ) { if ( ( strin . charAt ( j ) >= ' a ' && strin . charAt ( j ) <= ' z ' ) || ( strin . charAt ( j ) >= ' A ' && strin . charAt ( j ) <= ' Z ' ) ) { temp += strin . charAt ( j ) ; } } for ( int k = 1 ; k <= num ; k ++ ) { System . out . print ( temp ) ; } num = 0 ; temp = " " ; if ( j < strin . length ( ) ) { i = j ; } } } } } public static void main ( String args [ ] ) { String strin = "3 ( ab ) 4 ( cd ) " ; expandString ( strin ) ; } }
import java . util . * ; class GFG { static String findPrefix ( String str1 , String str2 ) { boolean pos = false ; while ( str1 . length ( ) > 0 ) { if ( ! str2 . contains ( str1 ) ) str1 = str1 . substring ( 0 , str1 . length ( ) - 1 ) ; else { pos = str2 . contains ( str1 ) ; break ; } } if ( pos == false ) return " - 1" ; return str1 ; } public static void main ( String [ ] args ) { String str1 = " geeksfor " ; String str2 = " forgeeks " ; System . out . println ( findPrefix ( str1 , str2 ) ) ; } }
class GFG { static int countSubStr ( String s , int n ) { int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 5 && " geeks " . equals ( s . substring ( i , i + 5 ) ) ) { c1 ++ ; } if ( i < n - 3 && " for " . equals ( s . substring ( i , i + 3 ) ) ) { c2 = c2 + c1 ; } } return c2 ; } public static void main ( String [ ] args ) { String s = " geeksforgeeksisforgeeks " ; int n = s . length ( ) ; System . out . println ( countSubStr ( s , n ) ) ; } }
import java . util . PriorityQueue ; class GFG { static String getSmallestStr ( String S , int K ) { String X = " " ; PriorityQueue < Character > pq = new PriorityQueue < > ( ) ; int i , n = S . length ( ) ; K = Math . min ( K , n ) ; for ( i = 0 ; i < K ; i ++ ) pq . add ( S . charAt ( i ) ) ; while ( ! pq . isEmpty ( ) ) { X += pq . peek ( ) ; pq . remove ( ) ; if ( i < S . length ( ) ) pq . add ( S . charAt ( i ) ) ; i ++ ; } return X ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int K = 5 ; System . out . println ( getSmallestStr ( S , K ) ) ; } }
class GFG { static void roundToNearest ( StringBuilder str , int n ) { if ( str . toString ( ) == " " ) return ; if ( str . charAt ( n - 1 ) - '0' <= 5 ) { str . setCharAt ( n - 1 , '0' ) ; System . out . print ( str . substring ( 0 , n ) ) ; } else { int carry = 0 ; str . setCharAt ( n - 1 , '0' ) ; int i = n - 2 ; carry = 1 ; while ( i >= 0 && carry == 1 ) { int currentDigit = str . charAt ( i ) - '0' ; currentDigit += carry ; if ( currentDigit > 9 ) { carry = 1 ; currentDigit = 0 ; } else carry = 0 ; str . setCharAt ( i , ( char ) ( currentDigit + '0' ) ) ; i -- ; } if ( carry == 1 ) System . out . print ( carry ) ; System . out . print ( str . substring ( 0 , n ) ) ; } } public static void main ( String [ ] args ) { StringBuilder str = new StringBuilder ( "99999999999999993" ) ; int n = str . length ( ) ; roundToNearest ( str , n ) ; } }
class GFG { static boolean isDigit ( char ch ) { if ( ch >= '0' && ch <= '9' ) return true ; return false ; } static int nextNonDigit ( String str , int i ) { while ( i < str . length ( ) && isDigit ( str . charAt ( i ) ) ) { i ++ ; } if ( i >= str . length ( ) ) return - 1 ; return i ; } static void appendRepeated ( StringBuilder sb , String str , int times ) { for ( int i = 0 ; i < times ; i ++ ) sb . append ( str ) ; } static String findString ( String str , int n ) { StringBuilder sb = new StringBuilder ( " " ) ; int startStr = nextNonDigit ( str , 0 ) ; while ( startStr != - 1 ) { int endStr = startStr ; while ( ( endStr + 1 ) < n && ! isDigit ( str . charAt ( endStr + 1 ) ) ) { endStr ++ ; } int startNum = endStr + 1 ; if ( startNum == - 1 ) break ; int endNum = startNum ; while ( ( endNum + 1 ) < n && isDigit ( str . charAt ( endNum + 1 ) ) ) { endNum ++ ; } int num = Integer . parseInt ( str . substring ( startNum , endNum + 1 ) ) ; appendRepeated ( sb , str . substring ( startStr , endStr + 1 ) , num ) ; startStr = nextNonDigit ( str , endStr + 1 ) ; } return sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " g1ee1ks1for1g1e2ks1" ; int n = str . length ( ) ; System . out . println ( findString ( str , n ) ) ; } }
import java . util . * ; class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean isVowelPrime ( String str , int n ) { boolean [ ] prime = new boolean [ n ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < n ; i += p ) prime [ i ] = false ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) && ! prime [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; if ( isVowelPrime ( str , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
public class GFG { static int stringToInt ( String str ) { if ( str . length ( ) == 1 ) return ( str . charAt ( 0 ) - '0' ) ; double y = stringToInt ( str . substring ( 1 ) ) ; double x = str . charAt ( 0 ) - '0' ; x = x * Math . pow ( 10 , str . length ( ) - 1 ) + y ; return ( int ) ( x ) ; } public static void main ( String [ ] args ) { String str = "1235" ; System . out . print ( stringToInt ( str ) ) ; } }
import java . util . * ; class GFG { static int MAX = 26 ; static String smallestStr ( char [ ] str , int n ) { int i , j = 0 ; int [ ] chk = new int [ MAX ] ; for ( i = 0 ; i < MAX ; i ++ ) chk [ i ] = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( chk [ str [ i ] - ' a ' ] == - 1 ) chk [ str [ i ] - ' a ' ] = i ; } for ( i = 0 ; i < n ; i ++ ) { boolean flag = false ; for ( j = 0 ; j < str [ i ] - ' a ' ; j ++ ) { if ( chk [ j ] > chk [ str [ i ] - ' a ' ] ) { flag = true ; break ; } } if ( flag ) break ; } if ( i < n ) { char ch1 = str [ i ] ; char ch2 = ( char ) ( j + ' a ' ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ch1 ) str [ i ] = ch2 ; else if ( str [ i ] == ch2 ) str [ i ] = ch1 ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " ccad " ; int n = str . length ( ) ; System . out . println ( smallestStr ( str . toCharArray ( ) , n ) ) ; } }
class GFG { static int longest_substring ( String s , int k ) { int n = s . length ( ) ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( i < n && s . charAt ( i ) == '0' ) { x ++ ; i ++ ; } ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 ans != n ) return ans ; else return ( ans / 2 ) * k ; } public static void main ( String [ ] args ) { String s = "010001000" ; int k = 4 ; System . out . println ( longest_substring ( s , k ) ) ; } }
class GFG { static int Occurrence ( String s , int position ) { int count = 0 ; for ( int i = 0 ; i < position - 1 ; i ++ ) if ( s . charAt ( i ) == s . charAt ( position - 1 ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { String s = " ababababab " ; int p = 9 ; System . out . println ( Occurrence ( s , p ) ) ; } }
class GFG { static int MAX = 26 ; public static boolean canBeMapped ( String s1 , int l1 , String s2 , int l2 ) { if ( l1 != l2 ) return false ; int [ ] freq1 = new int [ MAX ] ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) freq1 [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ s2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; boolean found = false ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " axx " ; String s2 = " cbc " ; int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { public static void generateStr ( String a , String b , String s , int count , int len ) { if ( count == len ) { System . out . println ( s ) ; return ; } generateStr ( a . substring ( 1 ) , b . substring ( 1 ) , s + a . charAt ( 0 ) , count + 1 , len ) ; generateStr ( a . substring ( 1 ) , b . substring ( 1 ) , s + b . charAt ( 0 ) , count + 1 , len ) ; } public static void main ( String [ ] args ) { String a = " abc " , b = " def " ; int n = a . length ( ) ; generateStr ( a , b , " " , 0 , n ) ; } }
import java . util . * ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . length ; Map < Character , Boolean > has = new HashMap < > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } i ++ ; has . clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { System . out . println ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; while ( i < n ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . toCharArray ( ) , k ) ; } }
import java . util . * ; class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static int countSubstringsUtil ( char [ ] s ) { int count = 0 ; Map < Character , Integer > mp = new HashMap < > ( ) ; int n = s . length ; int start = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( s [ i ] ) ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) + 1 ) ; } else { mp . put ( s [ i ] , 1 ) ; } while ( mp . containsKey ( ' a ' ) && mp . containsKey ( ' e ' ) && mp . containsKey ( ' i ' ) && mp . containsKey ( ' o ' ) && mp . containsKey ( ' u ' ) && mp . get ( ' a ' ) > 0 && mp . get ( ' e ' ) > 0 && mp . get ( ' i ' ) > 0 && mp . get ( ' o ' ) > 0 && mp . get ( ' u ' ) > 0 ) { count += n - i ; mp . put ( s [ start ] , mp . get ( s [ start ] ) - 1 ) ; start ++ ; } } return count ; } static int countSubstrings ( String s ) { int count = 0 ; String temp = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) { temp += s . charAt ( i ) ; } else { if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp . toCharArray ( ) ) ; temp = " " ; } } if ( temp . length ( ) > 0 ) count += countSubstringsUtil ( temp . toCharArray ( ) ) ; return count ; } public static void main ( String [ ] args ) { String s = " aeouisddaaeeiouua " ; System . out . println ( countSubstrings ( s ) ) ; } }
class GFG { static String removeDuplicates ( String S ) { String ans = " " ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { if ( ans . isEmpty ( ) || ans . charAt ( ans . length ( ) - 1 ) != S . charAt ( i ) ) ans += S . charAt ( i ) ; else if ( ans . charAt ( ans . length ( ) - 1 ) == S . charAt ( i ) ) ans = ans . substring ( 0 , ans . length ( ) - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " keexxllx " ; System . out . println ( removeDuplicates ( str ) ) ; } }
class GFG { static String swap ( String str , int i , int j ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setCharAt ( i , str . charAt ( j ) ) ; sb . setCharAt ( j , str . charAt ( i ) ) ; return sb . toString ( ) ; } static String reverseWords ( String str ) { int start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' || i == str . length ( ) - 1 ) { int end ; if ( i == str . length ( ) - 1 ) end = i ; else end = i - 1 ; while ( start < end ) { str = swap ( str , start , end ) ; start ++ ; end -- ; } start = i + 1 ; } } return str ; } public static void main ( String args [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; System . out . println ( reverseWords ( str ) ) ; } }
class GFG { static int countSubString ( String s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . length ( ) ; while ( s . charAt ( left ) != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s . charAt ( right ) == c ) { freq ++ ; } right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s . charAt ( left ) != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s . charAt ( right ) != c ) { if ( s . charAt ( right ) == c ) { freq ++ ; } right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } public static void main ( String args [ ] ) { String s = "3123231" ; char c = '3' ; int k = 2 ; System . out . println ( countSubString ( s , c , k ) ) ; } }
import java . io . * ; class GFG { static int solve ( String test , int MOD ) { int size = test . length ( ) ; int total = 0 ; for ( int i = 0 ; i <= 8 ; i ++ ) { int x = i ; int y = i + 1 ; String newtest = " " ; for ( int j = 0 ; j < size ; j ++ ) { if ( test . charAt ( j ) == x + 48 || test . charAt ( j ) == y + 48 ) { newtest += test . charAt ( j ) ; } } if ( newtest . length ( ) > 0 ) { int size1 = newtest . length ( ) ; int [ ] prefix = new int [ size1 ] ; for ( int j = 0 ; j < size1 ; j ++ ) { prefix [ j ] = 0 ; if ( newtest . charAt ( j ) == y + 48 ) { prefix [ j ] ++ ; } } for ( int j = 1 ; j < size1 ; j ++ ) { prefix [ j ] += prefix [ j - 1 ] ; } int count = 0 ; int firstcount = 0 ; int ss = 0 ; int prev = 0 ; for ( int j = 0 ; j < size1 ; j ++ ) { if ( newtest . charAt ( j ) == x + 48 ) { count ++ ; firstcount ++ ; } else { ss += count * count ; int pairsum = ( firstcount * firstcount - ss ) / 2 ; int temp = pairsum ; pairsum -= prev ; prev = temp ; int secondway = prefix [ size1 - 1 ] ; if ( j != 0 ) secondway -= prefix [ j - 1 ] ; int answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) ; answer /= 4 ; answer += ( pairsum * secondway * ( secondway - 1 ) ) / 2 ; total += answer ; count = 0 ; } } } } return total ; } public static void main ( String [ ] args ) { String test = "13134422" ; int MOD = 1000000007 ; System . out . println ( solve ( test , MOD ) ) ; } }
class sol { static boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void main ( String args [ ] ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . Arrays ; class GFG { static int MAX = 26 ; static int countPairs ( char [ ] str , int k ) { int n = str . length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int cnt = 0 ; if ( k == 0 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 1 ) { cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 ) ; } } } else { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 0 && i + k < MAX && freq [ i + k ] > 0 ) { cnt += ( freq [ i ] * freq [ i + k ] ) ; } } ; } return cnt ; } public static void main ( String [ ] args ) { String str = " abcdab " ; int k = 0 ; System . out . println ( countPairs ( str . toCharArray ( ) , k ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n == 0 n == 1 ) { return false ; } for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static int sumAscii ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i + 1 ) ) { sum += ( int ) ( str . charAt ( i ) ) ; } } return sum ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; System . out . println ( sumAscii ( str , n ) ) ; } }
import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] graph = new Vector [ 100 ] ; static String [ ] weight = new String [ 100 ] ; static boolean Pangram ( String x ) { HashMap < Character , Integer > mp = new HashMap < > ( ) ; int n = x . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( x . charAt ( i ) ) ) { mp . put ( x . charAt ( i ) , mp . get ( x . charAt ( i ) ) + 1 ) ; } else { mp . put ( x . charAt ( i ) , 1 ) ; } } if ( mp . size ( ) == 26 ) return true ; else return false ; } static int countTotalPangram ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( Pangram ( weight [ i ] ) ) cnt ++ ; return cnt ; } static void dfs ( int node , int parent ) { for ( int to : graph [ node ] ) { if ( to == parent ) continue ; dfs ( to , node ) ; weight [ node ] += weight [ to ] ; } } public static void main ( String [ ] args ) { int n = 6 ; weight [ 1 ] = " abcde " ; weight [ 2 ] = " fghijkl " ; weight [ 3 ] = " abcdefg " ; weight [ 4 ] = " mnopqr " ; weight [ 5 ] = " stuvwxy " ; weight [ 6 ] = " zabcdef " ; for ( int i = 0 ; i < graph . length ; i ++ ) graph [ i ] = new Vector < Integer > ( ) ; graph [ 1 ] . add ( 2 ) ; graph [ 2 ] . add ( 3 ) ; graph [ 2 ] . add ( 4 ) ; graph [ 1 ] . add ( 5 ) ; graph [ 5 ] . add ( 6 ) ; dfs ( 1 , 1 ) ; System . out . print ( countTotalPangram ( n ) ) ; } }
class GFG { static boolean isValid ( String s ) { if ( ( s . indexOf ( " geek " ) != - 1 ) && ( s . indexOf ( " keeg " , s . indexOf ( " geek " ) + 4 ) != - 1 ) ) return true ; return false ; } public static void main ( String args [ ] ) { String s = " geekeekeeg " ; if ( isValid ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int findCount ( String s , int n , int a , int b ) { int res = 0 ; char [ ] s1 = s . toCharArray ( ) ; for ( int i = 0 ; i < b ; i ++ ) { if ( i == a ) { if ( s1 [ n - i - 1 ] != '1' ) res += 1 ; } else { if ( s1 [ n - i - 1 ] != '0' ) res += 1 ; } } return res ; } static public void main ( String [ ] args ) { String str = "1001011001" ; int N = str . length ( ) ; int A = 3 , B = 6 ; System . out . println ( findCount ( str , N , A , B ) ) ; } }
public class GFG { public static final int NUM = 31 ; static void positions ( String str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ( str . charAt ( i ) & NUM ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " Geeks " ; int n = str . length ( ) ; positions ( str , n ) ; } }
import java . util . * ; class GFG { static void permute ( char [ ] str , int l , int r ) { if ( l == r ) { if ( ! String . valueOf ( str ) . contains ( " AB " ) ) System . out . print ( String . valueOf ( str ) + " ▁ " ) ; return ; } for ( int i = l ; i <= r ; i ++ ) { char tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; permute ( str , l + 1 , r ) ; tmp = str [ l ] ; str [ l ] = str [ i ] ; str [ i ] = tmp ; } } public static void main ( String [ ] args ) { String str = " ABC " ; permute ( str . toCharArray ( ) , 0 , str . length ( ) - 1 ) ; } }
import java . util . Arrays ; import java . lang . Math ; class GFG { public static int lenoflongestnonpalindrome ( String s ) { int max1 = 1 , len = 0 ; char [ ] new_str = s . toCharArray ( ) ; for ( int i = 0 ; i < new_str . length - 1 ; i ++ ) { if ( new_str [ i ] == new_str [ i + 1 ] ) len = 0 ; else if ( i > 0 && ( new_str [ i + 1 ] == new_str [ i - 1 ] ) ) len = 1 ; len ++ ; } if ( max1 == 1 ) return 0 ; else return max1 ; } public static void main ( String [ ] args ) { String s = " synapse " ; System . out . println ( lenoflongestnonpalindrome ( s ) ) ; } }
class GFG { static String makePalindrome ( char [ ] str ) { int i = 0 , j = str . length - 1 ; while ( i <= j ) { if ( str [ i ] == ' * ' && str [ j ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; } else if ( str [ j ] == ' * ' ) str [ j ] = str [ i ] ; else if ( str [ i ] == ' * ' ) str [ i ] = str [ j ] ; else if ( str [ i ] != str [ j ] ) return " - 1" ; i ++ ; j -- ; } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { char [ ] str = " na * an " . toCharArray ( ) ; System . out . println ( makePalindrome ( str ) ) ; } }
class GFG { public static int calcScore ( String str ) { int score = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ) { int chunkSize = 1 ; char currentChar = str . charAt ( i ++ ) ; while ( i < len && str . charAt ( i ) == currentChar ) { chunkSize ++ ; i ++ ; } if ( currentChar == '1' ) score += Math . pow ( chunkSize , 2 ) ; else score -= Math . pow ( chunkSize , 2 ) ; } return score ; } public static void main ( String [ ] args ) { String str = "11011" ; System . out . println ( calcScore ( str ) ) ; } }
class GFG { static int countSubString ( char [ ] s , char c , int k ) { int leftCount = 0 , rightCount = 0 ; int left = 0 , right = 0 ; int freq = 0 ; int result = 0 , len = s . length ; while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } right = left + 1 ; while ( freq != ( k - 1 ) && ( right - 1 ) < len ) { if ( s [ right ] == c ) freq ++ ; right ++ ; } while ( left < len && ( right - 1 ) < len ) { while ( s [ left ] != c && left < len ) { left ++ ; leftCount ++ ; } while ( right < len && s [ right ] != c ) { if ( s [ right ] == c ) freq ++ ; right ++ ; rightCount ++ ; } result = result + ( leftCount + 1 ) * ( rightCount + 1 ) ; freq = k - 1 ; leftCount = 0 ; rightCount = 0 ; left ++ ; right ++ ; } return result ; } public static void main ( String [ ] args ) { String s = " abada " ; char c = ' a ' ; int k = 2 ; System . out . println ( countSubString ( s . toCharArray ( ) , c , k ) ) ; } }
import java . util . * ; class GFG { static boolean isValid ( String str , int n ) { if ( ! ( ( str . charAt ( 0 ) >= ' a ' && str . charAt ( 0 ) <= ' z ' ) || ( str . charAt ( 0 ) >= ' A ' && str . charAt ( 0 ) <= ' Z ' ) || str . charAt ( 0 ) == ' _ ' ) ) return false ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( ! ( ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) || ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) || ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) || str . charAt ( i ) == ' _ ' ) ) return false ; } return true ; } public static void main ( String args [ ] ) { String str = " _ geeks123" ; int n = str . length ( ) ; if ( isValid ( str , n ) ) System . out . println ( " Valid " ) ; else System . out . println ( " Invalid " ) ; } }
import java . util . * ; class GFG { static int findM ( String s , int x ) { int cnt = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) - '0' != x ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String s = "3662123" ; int x = 6 ; System . out . println ( findM ( s , x ) ) ; } }
class GFG { static void FirstAndLast ( String str ) { int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( i == 0 ) System . out . print ( str . charAt ( i ) ) ; if ( i == str . length ( ) - 1 ) System . out . print ( str . charAt ( i ) ) ; if ( str . charAt ( i ) == ' ▁ ' ) { System . out . print ( str . charAt ( i - 1 ) + " ▁ " + str . charAt ( i + 1 ) ) ; } } } public static void main ( String args [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; FirstAndLast ( str ) ; } }
class GFG { static int MAX = 64 ; static int getBitmask ( char [ ] s ) { int temp = 0 ; for ( int j = 0 ; j < s . length ; j ++ ) { switch ( s [ j ] ) { case ' s ' : temp = temp | ( 1 ) ; break ; case ' t ' : temp = temp | ( 2 ) ; break ; case ' r ' : temp = temp | ( 4 ) ; break ; case ' i ' : temp = temp | ( 8 ) ; break ; case ' n ' : temp = temp | ( 16 ) ; break ; case ' g ' : temp = temp | ( 32 ) ; break ; default : break ; } } return temp ; } static int countPairs ( String arr [ ] , int n ) { int [ ] bitMask = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) bitMask [ getBitmask ( arr [ i ] . toCharArray ( ) ) ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i ; j < MAX ; j ++ ) { if ( ( i j ) == ( MAX - 1 ) ) { if ( i == j ) cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) / 2 ) ; else cnt += ( bitMask [ i ] * bitMask [ j ] ) ; } } } return cnt ; } public static void main ( String [ ] args ) { String arr [ ] = { " strrr " , " string " , " gstrin " } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
class GFG { static int countOcc ( String s ) { int cnt = 0 ; for ( int i = 0 ; i < s . length ( ) - 3 ; i ++ ) { int c = 0 , l = 0 , a = 0 , p = 0 ; for ( int j = i ; j < i + 4 ; j ++ ) { switch ( s . charAt ( j ) ) { case ' c ' : c ++ ; break ; case ' l ' : l ++ ; break ; case ' a ' : a ++ ; break ; case ' p ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String s = " clapc " ; System . out . print ( countOcc ( s . toLowerCase ( ) ) ) ; } }
class GfG { static int calculateWays ( String s , int n , int l , int r ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < l i > r ) freq [ s . charAt ( i ) - ' a ' ] ++ ; } int ways = 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( freq [ s . charAt ( i ) - ' a ' ] != 0 ) { ways = ways * freq [ s . charAt ( i ) - ' a ' ] ; freq [ s . charAt ( i ) - ' a ' ] -- ; } else { ways = 0 ; break ; } } return ways ; } public static void main ( String [ ] args ) { String s = " cabcaab " ; int n = s . length ( ) ; int l = 1 , r = 3 ; System . out . println ( calculateWays ( s , n , l , r ) ) ; } }
import java . util . * ; class GfG { static Character findKthChar ( String s , int k ) { int len = s . length ( ) ; int i = 0 ; int total_len = 0 ; while ( i < len ) { if ( Character . isLetter ( s . charAt ( i ) ) ) { total_len ++ ; if ( total_len == k ) return s . charAt ( i ) ; i ++ ; } else { int n = 0 ; while ( i < len && ! Character . isLetter ( s . charAt ( i ) ) ) { n = n * 10 + ( s . charAt ( i ) - '0' ) ; i ++ ; } int next_total_len = total_len * n ; if ( k <= next_total_len ) { int pos = k % total_len ; if ( pos == 0 ) { pos = total_len ; } return findKthChar ( s , pos ) ; } else { total_len = next_total_len ; } } } return ' ▁ ' ; } public static void main ( String [ ] args ) { String s = " ab2c3" ; int k = 5 ; System . out . println ( findKthChar ( s , k ) ) ; } }
import java . math . * ; class GFG { static boolean check_char ( String st , char ch ) { int l = st . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . charAt ( i ) == ch ) return true ; } return false ; } static char find_avg ( String st ) { int i , sm = 0 ; int l = st . length ( ) ; char ch ; for ( i = 0 ; i < l ; i ++ ) { ch = st . charAt ( i ) ; sm = sm + ( int ) ( ch ) ; } int avg = ( int ) ( Math . floor ( sm / l ) ) ; return ( ( char ) ( avg ) ) ; } public static void main ( String [ ] args ) { String st = " ag23sdfa " ; char ch = find_avg ( st ) ; System . out . println ( ch ) ; if ( check_char ( st , ch ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; System . out . print ( ch ) ; num = 0 ; } } } public static void main ( String args [ ] ) { String str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; } }
import java . util . * ; class GFG { static int countDistinctCode ( String str ) { Set < String > codes = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) - 1 ; i ++ ) codes . add ( str . substring ( i , i + 2 ) ) ; return codes . size ( ) ; } public static void main ( String [ ] args ) { String str = " UPUP " ; System . out . println ( countDistinctCode ( str ) ) ; } }
class GFG { static final int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 1 , 2 , 3 , 4 , 1 , 2 , 3 , 1 , 2 , 3 , 4 } ; public static int countKeyPressed ( String str , int len ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) count = count + arr [ str . charAt ( i ) - ' a ' ] ; return count ; } public static void main ( String [ ] args ) { String str = " abcdef " ; int len = str . length ( ) ; System . out . print ( countKeyPressed ( str , len ) ) ; } }
class GFG { static boolean isReverseEqual ( String s1 , String s2 ) { if ( s1 . length ( ) != s2 . length ( ) ) return false ; int len = s1 . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( s1 . charAt ( i ) != s2 . charAt ( len - i - 1 ) ) return false ; return true ; } static String getWord ( String str [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isReverseEqual ( str [ i ] , str [ j ] ) ) return str [ i ] ; return " - 1" ; } public static void main ( String [ ] args ) { String str [ ] = { " geeks " , " for " , " skeeg " } ; int n = str . length ; System . out . print ( getWord ( str , n ) ) ; } }
class GFG { static int minLettersNeeded ( int n ) { if ( n % 26 == 0 ) return ( n / 26 ) ; else return ( ( n / 26 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 52 ; System . out . print ( minLettersNeeded ( n ) ) ; } }
class GFG { static int countMinReversals ( String expr ) { int len = expr . length ( ) ; if ( len % 2 != 0 ) return - 1 ; int ans = 0 ; int i ; int open = 0 ; int close = 0 ; for ( i = 0 ; i < len ; i ++ ) { if ( expr . charAt ( i ) == ' { ' ) open ++ ; else { if ( open == 0 ) close ++ ; else open -- ; } } ans = ( close / 2 ) + ( open / 2 ) ; close %= 2 ; open %= 2 ; if ( close != 0 ) ans += 2 ; return ans ; } public static void main ( String args [ ] ) { String expr = " } } { { " ; System . out . println ( countMinReversals ( expr ) ) ; } }
class GFG { static int minReplacement ( String s , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == '1' ) ans ++ ; if ( i % 2 == 1 && s . charAt ( i ) == '0' ) ans ++ ; } return Math . min ( ans , len - ans ) ; } public static void main ( String args [ ] ) { String s = "1100" ; int len = s . length ( ) ; System . out . print ( minReplacement ( s , len ) ) ; } }
class GFG { static int substrDeletion ( String str , int len ) { int count0 = 0 , count1 = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) count0 ++ ; else count1 ++ ; } return Math . min ( count0 , count1 ) ; } public static void main ( String [ ] args ) { String str = "010" ; int len = str . length ( ) ; System . out . println ( substrDeletion ( str , len ) ) ; } }
class GFG { static String groupCharacters ( char [ ] s , int len ) { String temp = " " ; for ( int i = 0 ; i < len ; i ++ ) if ( s [ i ] != ' ▁ ' ) temp = temp + s [ i ] ; len = temp . length ( ) ; String ans = " " ; int i = 0 ; while ( i < len ) { if ( temp . charAt ( i ) >= ( ' A ' ) && temp . charAt ( i ) <= ( ' Z ' ) ) { while ( i < len && temp . charAt ( i ) >= ( ' A ' ) && temp . charAt ( i ) <= ( ' Z ' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } else if ( temp . charAt ( i ) >= ( '0' ) && temp . charAt ( i ) <= ( '9' ) ) { while ( i < len && temp . charAt ( i ) >= ( '0' ) && temp . charAt ( i ) <= ( '9' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } else { while ( i < len && temp . charAt ( i ) >= ( ' * ' ) && temp . charAt ( i ) <= ( ' / ' ) ) { ans = ans + temp . charAt ( i ) ; i ++ ; } ans = ans + " ▁ " ; } } return ans ; } public static void main ( String [ ] args ) { String s = "34FTG234 + ▁ + - ▁ * " ; int len = s . length ( ) ; System . out . print ( groupCharacters ( s . toCharArray ( ) , len ) ) ; } }
class GFG { static int prefixOccurrences ( String str ) { char c = str . charAt ( 0 ) ; int countc = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == c ) countc ++ ; } return countc ; } public static void main ( String args [ ] ) { String str = " abbcdabbcd " ; System . out . println ( prefixOccurrences ( str ) ) ; } }
class GfG { static String leastLexiString ( String s ) { if ( s . length ( ) == 1 ) return s ; String x = leastLexiString ( s . substring ( 0 , s . length ( ) / 2 ) ) ; String y = leastLexiString ( s . substring ( s . length ( ) / 2 ) ) ; return String . valueOf ( ( x + y ) . compareTo ( y + x ) ) ; } static boolean areEquivalent ( String a , String b ) { return ! ( leastLexiString ( a ) . equals ( leastLexiString ( b ) ) ) ; } public static void main ( String [ ] args ) { String a = " aaba " ; String b = " abaa " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GfG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static boolean checkPossibility ( String s1 , String s2 ) { int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( l1 != l2 ) return false ; for ( int i = 0 ; i < l1 ; i ++ ) { if ( isVowel ( s1 . charAt ( i ) ) && isVowel ( s2 . charAt ( i ) ) ) continue ; else if ( ! ( isVowel ( s1 . charAt ( i ) ) ) && ! ( isVowel ( s2 . charAt ( i ) ) ) ) continue ; else return false ; } return true ; } public static void main ( String [ ] args ) { String S1 = " abcgle " ; String S2 = " ezggli " ; if ( checkPossibility ( S1 , S2 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void generateString ( int A , int B ) { String rt = " " ; while ( 0 < A 0 < B ) { if ( A < B ) { if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } } else if ( B < A ) { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } else { if ( 0 < A -- ) { rt += ( ' a ' ) ; } if ( 0 < B -- ) { rt += ( ' b ' ) ; } } } System . out . println ( rt ) ; } public static void main ( String [ ] args ) { int A = 2 , B = 6 ; generateString ( A , B ) ; } }
import java . util . * ; class GfG { static int countStrings ( int n , int m , String s [ ] ) { HashSet < Integer > ind = new HashSet < Integer > ( ) ; for ( int j = 0 ; j < m ; j ++ ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , ( int ) ( s [ i ] . charAt ( j ) - '0' ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s [ i ] . charAt ( j ) - '0' == mx ) ind . add ( i ) ; } return ind . size ( ) ; } public static void main ( String [ ] args ) { String s [ ] = { "223" , "232" , "112" } ; int m = s [ 0 ] . length ( ) ; int n = s . length ; System . out . println ( countStrings ( n , m , s ) ) ; } }
import java . util . * ; class GFG { static int minOperations ( String s , String t , int n ) { int ct0 = 0 , ct1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == t . charAt ( i ) ) continue ; if ( s . charAt ( i ) == '0' ) ct0 ++ ; else ct1 ++ ; } return Math . max ( ct0 , ct1 ) ; } public static void main ( String args [ ] ) { String s = "010" , t = "101" ; int n = s . length ( ) ; System . out . println ( minOperations ( s , t , n ) ) ; } }
class GFG { static String getSubSeq ( String s , int n ) { String res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s . charAt ( cr ) ; for ( int i = cr + 1 ; i < n ; i ++ ) { mx = ( char ) Math . max ( mx , s . charAt ( i ) ) ; } int lst = cr ; for ( int i = cr ; i < n ; i ++ ) { if ( s . charAt ( i ) == mx ) { res += s . charAt ( i ) ; lst = i ; } } cr = lst + 1 ; } return res ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; System . out . println ( getSubSeq ( s , n ) ) ; } }
class GFG { static int countMinimalReplacements ( String s ) { int n = s . length ( ) ; int mini = Integer . MAX_VALUE ; char [ ] dup = "012" . toCharArray ( ) ; do { int dif = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) != dup [ i % 3 ] ) { dif ++ ; } } mini = Math . min ( mini , dif ) ; } while ( next_permutation ( dup ) ) ; return mini ; } static boolean next_permutation ( char [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( int b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { char t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } public static void main ( String args [ ] ) { String s = "2101200" ; System . out . println ( countMinimalReplacements ( s ) ) ; } }
class GFG { static String decryptString ( String str , int n ) { int i = 0 , jump = 1 ; String decryptedStr = " " ; while ( i < n ) { decryptedStr += str . charAt ( i ) ; i += jump ; jump ++ ; } return decryptedStr ; } public static void main ( String [ ] args ) { String str = " geeeeekkkksssss " ; int n = str . length ( ) ; System . out . println ( decryptString ( str , n ) ) ; } }
class GFG { static int countSubStr ( String str , int len , int k ) { int count = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int n = 0 ; for ( int j = i ; j < len ; j ++ ) { n = n * 10 + ( str . charAt ( j ) - '0' ) ; if ( n % k == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { String str = "33445" ; int len = str . length ( ) ; int k = 11 ; System . out . println ( countSubStr ( str , len , k ) ) ; } }
class GfG { static char Colour_Combination ( String s ) { char temp = s . charAt ( 0 ) ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( temp != s . charAt ( i ) ) { if ( ( temp == ' B ' temp == ' G ' ) && ( s . charAt ( i ) == ' G ' || s . charAt ( i ) == ' B ' ) ) temp = ' Y ' ; else if ( ( temp == ' B ' temp == ' Y ' ) && ( s . charAt ( i ) == ' Y ' || s . charAt ( i ) == ' B ' ) ) temp = ' G ' ; else temp = ' B ' ; } } return temp ; } public static void main ( String [ ] args ) { String s = " GBYGB " ; System . out . println ( Colour_Combination ( s ) ) ; } }
class GfG { static void reverse ( String str , int x ) { int n = ( str . length ( ) - x ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x - 1 ; i >= n ; i -- ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x ; i < str . length ( ) ; i ++ ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int x = 3 ; reverse ( str , x ) ; } }
class GFG { static int getSteps ( String str , int i , int j , int n ) { int k = i + 1 ; int steps = 0 ; char ch = str . charAt ( i ) ; while ( k <= j ) { if ( str . charAt ( k ) != ch ) { steps ++ ; ch = str . charAt ( k ) ; } k ++ ; } return steps ; } static int getMinSteps ( String str , int i , int j , int n ) { if ( j < i ) { int temp = i ; i = j ; j = temp ; } int stepsToRight = getSteps ( str , i , j , n ) ; int stepsToLeft = getSteps ( str , 0 , i , n ) + getSteps ( str , j , n - 1 , n ) ; if ( str . charAt ( 0 ) != str . charAt ( n - 1 ) ) stepsToLeft ++ ; return Math . min ( stepsToLeft , stepsToRight ) ; } public static void main ( String [ ] args ) { String str = " SSNSS " ; int n = str . length ( ) ; int i = 0 , j = 3 ; System . out . println ( getMinSteps ( str , i , j , n ) ) ; } }
import java . util . * ; class GFG { static int MAX_CHAR = 256 ; static int countSubStrings ( String s , String anotherStr , int k ) { boolean illegal [ ] = new boolean [ MAX_CHAR ] ; for ( int i = 0 ; i < anotherStr . length ( ) ; i ++ ) { illegal [ anotherStr . charAt ( i ) ] = true ; } HashSet < String > us = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { String ss = " " ; int count = 0 ; for ( int j = i ; j < s . length ( ) ; ++ j ) { if ( illegal [ s . charAt ( j ) ] ) { ++ count ; } ss = ss + s . charAt ( j ) ; if ( count <= k ) { us . add ( ss ) ; } else { break ; } } } return us . size ( ) ; } public static void main ( String [ ] args ) { String str = " acbacbacaa " ; String anotherStr = " abcdefghijklmnopqrstuvwxyz " ; int k = 2 ; System . out . println ( countSubStrings ( str , anotherStr , k ) ) ; } }
import java . util . HashMap ; class GFG { static int countPairs ( String [ ] bracks , int num ) { HashMap < Integer , Integer > open = new HashMap < > ( ) ; HashMap < Integer , Integer > close = new HashMap < > ( ) ; int cnt = 0 ; for ( int i = 0 ; i < num ; i ++ ) { String s = bracks [ i ] ; int l = s . length ( ) ; int op = 0 , cl = 0 ; for ( int j = 0 ; j < l ; j ++ ) { if ( s . charAt ( j ) == ' ( ' ) op ++ ; else { if ( op != 0 ) op -- ; else cl ++ ; } } if ( op != 0 && cl == 0 ) open . put ( op , open . get ( op ) == null ? 1 : open . get ( op ) + 1 ) ; if ( cl != 0 && op == 0 ) close . put ( cl , close . get ( cl ) == null ? 1 : close . get ( cl ) + 1 ) ; if ( op == 0 && cl == 0 ) cnt ++ ; } cnt /= 2 ; for ( HashMap . Entry < Integer , Integer > it : open . entrySet ( ) ) cnt += Math . min ( it . getValue ( ) , close . get ( it . getKey ( ) ) ) ; return cnt ; } public static void main ( String [ ] args ) { String [ ] bracks = { " ) ( ) ) " , " ) " , " ( ( " , " ( ( " , " ( " , " ) " , " ) " } ; int num = bracks . length ; System . out . println ( countPairs ( bracks , num ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static String decrypt ( String s , int l ) { String ans = " " ; if ( l % 2 == 1 ) { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . length ( ) != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indl ++ ) ; else ans += s . charAt ( indr -- ) ; cnt ++ ; } } else { int cnt = 0 ; int indl = 0 , indr = l - 1 ; while ( ans . length ( ) != l ) { if ( cnt % 2 == 0 ) ans += s . charAt ( indr -- ) ; else ans += s . charAt ( indl ++ ) ; cnt ++ ; } } StringBuffer sbr = new StringBuffer ( ans ) ; sbr . reverse ( ) ; return sbr . toString ( ) ; } public static void main ( String [ ] args ) { String s = " segosegekfrek " ; int l = s . length ( ) ; System . out . println ( decrypt ( s , l ) ) ; } }
class GFG { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len / 2 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( len - 1 - i ) ) return false ; } return true ; } static boolean createStringAndCheckPalindrome ( int N ) { String sub = " " + N , res_str = " " ; int sum = 0 ; while ( N > 0 ) { int digit = N % 10 ; sum += digit ; N = N / 10 ; } while ( res_str . length ( ) < sum ) res_str += sub ; if ( res_str . length ( ) > sum ) res_str = res_str . substring ( 0 , sum ) ; if ( isPalindrome ( res_str ) ) return true ; return false ; } public static void main ( String args [ ] ) { int N = 10101 ; if ( createStringAndCheckPalindrome ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static String removeChars ( String s ) { String modifiedStr = " " ; modifiedStr += s . charAt ( 0 ) ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( Character . isUpperCase ( s . charAt ( i ) ) && Character . isLowerCase ( s . charAt ( i - 1 ) ) || Character . isLowerCase ( s . charAt ( i ) ) && Character . isUpperCase ( s . charAt ( i - 1 ) ) ) modifiedStr += s . charAt ( i ) ; } return modifiedStr ; } public static void main ( String [ ] args ) { String s = " GeeksForGeeks " ; System . out . println ( removeChars ( s ) ) ; } }
class GFG { static String output = " " ; static void pairStar ( String input , int i ) { output = output + input . charAt ( i ) ; if ( i == input . length ( ) - 1 ) return ; if ( input . charAt ( i ) == input . charAt ( i + 1 ) ) output = output + ' * ' ; pairStar ( input , i + 1 ) ; } public static void main ( String [ ] args ) { String input = " geeks " ; pairStar ( input , 0 ) ; System . out . println ( output ) ; } }
class GFG { static int maxOperations ( String str ) { int i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' g ' ) { g ++ ; } else if ( str . charAt ( i ) == ' k ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str . charAt ( i ) == ' s ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } public static void main ( String args [ ] ) { String a = " ggkssk " ; System . out . print ( maxOperations ( a ) ) ; } }
import java . util . * ; public class GFG { static int [ ] getFrequencies ( String str ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } return freq ; } static int countMinParts ( String str ) { int n = str . length ( ) ; int freq [ ] = getFrequencies ( str ) ; List < Integer > oddFreq = new ArrayList < > ( ) ; List < Integer > evenFreq = new ArrayList < > ( ) ; int i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . add ( freq [ i ] ) ; else oddFreq . add ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . size ( ) ; i ++ ) { sumEven += evenFreq . get ( i ) ; } if ( oddFreq . size ( ) == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . size ( ) == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . size ( ) ) { if ( ( sumEven / 2 ) % oddFreq . size ( ) == 0 ) return oddFreq . size ( ) ; if ( oddFreq . get ( i ) == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq . set ( i , oddFreq . get ( i ) - 2 ) ; } return n ; } public static void main ( String [ ] args ) { String s = " noonpeep " ; System . out . println ( countMinParts ( s ) ) ; } }
import java . util . * ; class GFG { static boolean prime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean isVowel ( char c ) { c = Character . toLowerCase ( c ) ; if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) { return true ; } return false ; } static boolean isValidString ( String word ) { int cnt = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { if ( isVowel ( word . charAt ( i ) ) ) { cnt ++ ; } } if ( prime ( cnt ) ) { return true ; } else { return false ; } } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( isValidString ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . * ; class solution { static void printExpansion ( String str ) { String suff = " " ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { suff = suff + str . charAt ( i ) ; System . out . print ( suff ) ; } } public static void main ( String args [ ] ) { String str = " geeks " ; printExpansion ( str ) ; } }
import java . util . * ; class GFG { static char logicalExpressionEvaluation ( String str ) { Stack < Character > arr = new Stack < Character > ( ) ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == ' [ ' ) { Vector < Character > s = new Stack < Character > ( ) ; while ( arr . peek ( ) != ' ] ' ) { s . add ( arr . peek ( ) ) ; arr . pop ( ) ; } arr . pop ( ) ; if ( s . size ( ) == 3 ) { arr . push ( s . get ( 2 ) == '1' ? '0' : '1' ) ; } else if ( s . size ( ) == 5 ) { int a = s . get ( 0 ) - 48 , b = s . get ( 4 ) - 48 , c ; if ( s . get ( 2 ) == ' & ' ) { c = a & b ; } else { c = a | b ; } arr . push ( ( char ) ( c + 48 ) ) ; } } else { arr . push ( str . charAt ( i ) ) ; } } return arr . peek ( ) ; } public static void main ( String [ ] args ) { String str = " [ | , [ & ,1 , [ ! , 0 ] ] , [ ! , [ | , [ ▁ , 1,0 ] , [ ! ,1 ] ] ] ] " ; System . out . println ( logicalExpressionEvaluation ( str ) ) ; } }
public class GFG { static int count ( String s , int k ) { int n = s . length ( ) ; int d = 0 , i ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s . charAt ( i ) ; if ( d % k == 0 ) count += 1 ; for ( i = k ; i < n ; i ++ ) { int prev = s . charAt ( i - k ) ; d -= prev ; d += s . charAt ( i ) ; if ( d % k == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { String s = " bcgabc " ; int k = 3 ; int ans = count ( s , k ) ; System . out . println ( ans ) ; } }
class GFG { static String printFinalString ( String s ) { int i , n ; n = s . length ( ) ; int x = 0 , y = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' x ' ) { x ++ ; else { y ++ ; } } String finalString = " " ; if ( x > y ) { for ( i = 0 ; i < x - y ; i ++ ) { finalString += " x " ; } } else { for ( i = 0 ; i < y - x ; i ++ ) { finalString += " y " ; } } return finalString ; } public static void main ( String args [ ] ) { String s = " xxyyxyy " ; System . out . println ( printFinalString ( s ) ) ; } }
class GfG { static String K_String ( String s , int k ) { int n = s . length ( ) ; int fre [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) fre [ s . charAt ( i ) - ' a ' ] ++ ; String str = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { int x = fre [ i ] / k ; while ( x != 0 ) { str += ( char ) ( i + ' a ' ) ; x -- ; } } else { return " - 1" ; } } return str ; } public static void main ( String [ ] args ) { String s = " aabb " ; int k = 2 ; System . out . println ( K_String ( s , k ) ) ; } }
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < p_size ; i += p ) { prime [ i ] = false ; } } } } static void sumProdOfPrimeFreq ( char [ ] s ) { boolean [ ] prime = new boolean [ s . length + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , s . length + 1 ) ; int i , j ; Map < Character , Integer > mp = new HashMap < > ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) == null ? 1 : mp . get ( s [ i ] ) + 1 ) ; } int sum = 0 , product = 1 ; for ( Map . Entry < Character , Integer > it : mp . entrySet ( ) ) { if ( prime [ it . getValue ( ) ] ) { sum += it . getValue ( ) ; product *= it . getValue ( ) ; } } System . out . print ( " Sum ▁ = ▁ " + sum ) ; System . out . println ( " Product = " + product); } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; sumProdOfPrimeFreq ( s . toCharArray ( ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static HashMap < Integer , Integer > count = new HashMap < > ( ) ; static boolean canBePalindrome ( HashMap < Integer , Integer > count ) { for ( HashMap . Entry < Integer , Integer > entry : count . entrySet ( ) ) if ( ( entry . getValue ( ) & 1 ) == 1 ) return false ; return true ; } static int maxPal ( String str , int start , int end , HashMap < Integer , Integer > count ) { if ( end == str . length ( ) ) { if ( ( end - start ) % 2 == 0 ) if ( canBePalindrome ( count ) ) return end - start ; return 0 ; } else { if ( ( end - start ) % 2 == 0 ) { if ( canBePalindrome ( count ) ) { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; return Math . max ( end - start , maxPal ( str , start , end + 1 , count ) ) ; } else { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; return maxPal ( str , start , end + 1 , count ) ; } } else { count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? 1 : count . get ( ( int ) str . charAt ( end ) ) + 1 ) ; HashMap < Integer , Integer > c = new HashMap < > ( count ) ; int length = maxPal ( str , start , end + 1 , c ) ; count . put ( ( int ) str . charAt ( end ) , count . get ( ( int ) str . charAt ( end ) ) == null ? - 1 : count . get ( ( int ) str . charAt ( end ) ) - 1 ) ; count . put ( ( int ) str . charAt ( start ) , count . get ( ( int ) str . charAt ( start ) ) == null ? - 1 : count . get ( ( int ) str . charAt ( start ) ) - 1 ) ; return Math . max ( length , maxPal ( str , start + 1 , end , count ) ) ; } } } public static void main ( String [ ] args ) { String str = "124565463" ; int start = 0 , end = 0 ; System . out . println ( maxPal ( str , start , end , count ) ) ; } }
import java . util . * ; class Solution { static int CountCharacters ( String str , int l , int r ) { int cnt = 0 ; Map < Character , Integer > m = new HashMap < Character , Integer > ( ) ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ! ( l <= str . charAt ( i ) && str . charAt ( i ) <= r ) ) { cnt ++ ; if ( m . get ( str . charAt ( i ) ) != null ) if ( m . get ( str . charAt ( i ) ) != 1 ) { System . out . print ( str . charAt ( i ) + " ▁ " ) ; m . put ( str . charAt ( i ) , m . get ( str . charAt ( i ) ) == null ? 0 : m . get ( str . charAt ( i ) ) + 1 ) ; } } } return cnt ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int l = 102 , r = 111 ; System . out . println ( "Characters with ASCII values not in the range [l, r]  in the given string are : "); System . out . println ( " and their count is " + CountCharacters(str, l, r)); } }
class GFG { static void CountWords ( String str , int k ) { int sum = 0 ; int NumberOfWords = 0 ; int counter = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str . charAt ( i ) == ' ▁ ' ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } { sum += str . charAt ( i ) ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ " + " of ▁ ASCII ▁ values ▁ less ▁ than ▁ k ▁ = ▁ " + counter ) ; System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ " + " ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = ▁ " + ( NumberOfWords - counter ) ) ; } public static void main ( String [ ] args ) { String str = " Learn ▁ how ▁ to ▁ code " ; int k = 400 ; CountWords ( str , k ) ; } }
class GFG { final static int MAX_CHAR = 26 ; static int countPalindrome ( String str ) { int n = str . length ( ) ; int sum = 0 ; int hashTable [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) { hashTable [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( hashTable [ i ] != 0 ) { sum += ( hashTable [ i ] * ( hashTable [ i ] + 1 ) / 2 ) ; } } return sum ; } public static void main ( String [ ] args ) { String str = " ananananddd " ; System . out . println ( countPalindrome ( str ) ) ; } }
class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { i ++ ; } else { count ++ ; } } return count ; } public static void main ( String args [ ] ) { String str = "44522255" ; System . out . println ( countConsecutive ( str ) ) ; } }
import java . util . HashMap ; class GFG { public static boolean multipleOrFactor ( String s1 , String s2 ) { HashMap < Character , Integer > m1 = new HashMap < > ( ) ; HashMap < Character , Integer > m2 = new HashMap < > ( ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( m1 . containsKey ( s1 . charAt ( i ) ) ) { int x = m1 . get ( s1 . charAt ( i ) ) ; m1 . put ( s1 . charAt ( i ) , ++ x ) ; } else m1 . put ( s1 . charAt ( i ) , 1 ) ; } for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { if ( m2 . containsKey ( s2 . charAt ( i ) ) ) { int x = m2 . get ( s2 . charAt ( i ) ) ; m2 . put ( s2 . charAt ( i ) , ++ x ) ; } else m2 . put ( s2 . charAt ( i ) , 1 ) ; } for ( HashMap . Entry < Character , Integer > entry : m1 . entrySet ( ) ) { if ( ! m2 . containsKey ( entry . getKey ( ) ) ) continue ; if ( m2 . get ( entry . getKey ( ) ) != null && ( m2 . get ( entry . getKey ( ) ) % entry . getValue ( ) == 0 || entry . getValue ( ) % m2 . get ( entry . getKey ( ) ) == 0 ) ) continue ; else return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " geeksforgeeks " , s2 = " geeks " ; if ( multipleOrFactor ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Geeks { static int check_digits ( int n ) { while ( n > 0 ) { return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) > 0 ) return i ; } public static void main ( String args [ ] ) { int N = 2397 ; System . out . println ( smallest_number ( N ) ) ; } }
public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s . charAt ( i ) - '0' ) ; } num = num * 10 + ( s . charAt ( index ) - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallestNumber ( N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int CountCharacters ( String str , int l , int r ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( l <= str . charAt ( i ) && str . charAt ( i ) <= r ) { cnt ++ ; System . out . print ( str . charAt ( i ) + " ▁ " ) ; } } return cnt ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int l = 102 , r = 111 ; System . out . print ( " Characters ▁ with ▁ ASCII ▁ values " + " in the range [l, r] are NEW_LINE"); System . out . print ( " and their count is " + CountCharacters ( str , l , r ) ) ; } }
import java . util . * ; class GFG { static int minSteps ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) - 2 ; i ++ ) { if ( ( ( int ) str . charAt ( i ) ) == '0' ) { if ( str . charAt ( i + 1 ) == '1' ) { if ( str . charAt ( i + 2 ) == '0' ) { count ++ ; i += 2 ; } } } } return count ; } public static void main ( String args [ ] ) { String str = "0101010" ; System . out . println ( minSteps ( str ) ) ; } }
import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int xorOfPrime ( char [ ] s ) { boolean [ ] prime = new boolean [ 100005 ] ; for ( int i = 0 ; i < 100005 ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , 10005 ) ; int i , j ; Map < Character , Integer > m = new HashMap < > ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { if ( m . containsKey ( s [ i ] ) ) { m . put ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . put ( s [ i ] , 1 ) ; } } int result = 0 ; int flag = 0 ; for ( Map . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( prime [ entry . getValue ( ) ] ) { result ^= entry . getValue ( ) ; flag = 1 ; } } if ( flag != 1 ) return - 1 ; return result ; } public static void main ( String [ ] args ) { char [ ] s = " gggggeeekkkks " . toCharArray ( ) ; System . out . println ( xorOfPrime ( s ) ) ; } }
import java . util . * ; class GFG { static int CountCharacters ( String str , int k ) { int cnt = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( ( ( int ) str . charAt ( i ) ) < k ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String str = " GeeksForGeeks " ; int k = 90 ; int count = CountCharacters ( str , k ) ; System . out . println ( " Characters ▁ with ▁ ASCII ▁ values ▁ less ▁ than ▁ K ▁ are ▁ " + count ) ; System . out . println ( " Characters ▁ with ▁ ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ K ▁ are ▁ " + ( str . length ( ) - count ) ) ; } }
import java . util . * ; class GFG { static Set < String > seen = new HashSet < String > ( ) ; static Vector < Integer > edges = new Vector < Integer > ( ) ; static void dfs ( String node , int k , String A ) { for ( int i = 0 ; i < k ; ++ i ) { String str = node + A . charAt ( i ) ; if ( ! seen . contains ( str ) ) { seen . add ( str ) ; dfs ( str . substring ( 1 ) , k , A ) ; edges . add ( i ) ; } } } static String deBruijn ( int n , int k , String A ) { seen . clear ( ) ; edges . clear ( ) ; String startingNode = string ( n - 1 , A . charAt ( 0 ) ) ; dfs ( startingNode , k , A ) ; String S = " " ; int l = ( int ) Math . pow ( k , n ) ; for ( int i = 0 ; i < l ; ++ i ) S += A . charAt ( edges . get ( i ) ) ; S += startingNode ; return S ; } private static String string ( int n , char charAt ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += charAt ; return str ; } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; String A = "01" ; System . out . print ( deBruijn ( n , k , A ) ) ; } }
import java . util . HashMap ; import java . util . Vector ; class GFG { static String isFibonacci ( String s ) { HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) == null ? 1 : m . get ( s . charAt ( i ) ) + 1 ) ; Vector < Integer > v = new Vector < > ( ) ; int n = m . size ( ) ; int a = 1 , b = 1 ; int c ; v . add ( a ) ; v . add ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { v . add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; int i = 0 ; for ( HashMap . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( entry . getValue ( ) != v . elementAt ( i ) ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String s = " abeeedd " ; System . out . println ( isFibonacci ( s ) ) ; } }
class GFG { static String smallest ( String s ) { int l = s . length ( ) ; String ans = " " ; for ( int i = 0 ; i < l - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) { for ( int j = 0 ; j < l ; j ++ ) { if ( i != j ) { ans += s . charAt ( j ) ; } } return ans ; } } ans = s . substring ( 0 , l - 1 ) ; return ans ; } public static void main ( String [ ] args ) { String s = " abcda " ; System . out . println ( smallest ( s ) ) ; } }
class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { f = f * i ; } return f ; } static int npr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countPermutations ( String str ) { int even = ( int ) Math . floor ( ( double ) ( str . length ( ) / 2 ) ) ; int odd = str . length ( ) - even ; int ways = 0 ; int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ ( int ) ( str . charAt ( i ) - ' a ' ) ] ++ ; } int nvowels = freq [ 0 ] + freq [ 4 ] + freq [ 8 ] + freq [ 14 ] + freq [ 20 ] ; int nconsonants = str . length ( ) - nvowels ; ways = npr ( odd , nvowels ) * npr ( nconsonants , nconsonants ) ; return ways ; } public static void main ( String [ ] args ) { String str = " geeks " ; System . out . println ( countPermutations ( str ) ) ; } }
import java . util . * ; class solution { static String replacingConsonants ( String s ) { String str = " aaaeeeeiiiiioooooouuuuuuuu " ; char [ ] st = s . toCharArray ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int index = st [ i ] - ' a ' ; st [ i ] = str . charAt ( index ) ; } String str1 = new String ( st ) ; return str1 ; } public static void main ( String arr [ ] ) { String s = " geeksforgeeks " ; System . out . println ( replacingConsonants ( s ) ) ; } }
class GFG { static boolean isVowel ( char ch ) { if ( ch != ' a ' && ch != ' e ' && ch != ' i ' && ch != ' o ' && ch != ' u ' ) { return false ; } return true ; } static String replaceConsonants ( char [ ] s ) { for ( int i = 0 ; i < s . length ; i ++ ) { if ( ! isVowel ( s [ i ] ) ) { if ( s [ i ] == ' z ' ) { s [ i ] = ' b ' ; } else { s [ i ] = ( char ) ( s [ i ] + 1 ) ; if ( isVowel ( s [ i ] ) ) { s [ i ] = ( char ) ( s [ i ] + 1 ) ; } } } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( replaceConsonants ( s . toCharArray ( ) ) ) ; } }
class GFG { final static int MAX_CHAR = 26 ; static String removeChars ( String str , int k ) { int hash [ ] = new int [ MAX_CHAR ] ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; ++ i ) { hash [ str . charAt ( i ) - ' a ' ] ++ ; } String res = " " ; for ( int i = 0 ; i < n ; ++ i ) { if ( hash [ str . charAt ( i ) - ' a ' ] >= k ) { res += str . charAt ( i ) ; } } return res ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 2 ; System . out . println ( removeChars ( str , k ) ) ; } }
import java . io . * ; class GFG { static int countOnOff ( String n ) { int Led [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 5 } ; int len = n . length ( ) ; int sum = Led [ n . charAt ( 0 ) - '0' ] ; for ( int i = 1 ; i < len ; i ++ ) { sum = sum + Math . abs ( Led [ n . charAt ( i ) - '0' ] - Led [ n . charAt ( i - 1 ) - '0' ] ) ; } return sum ; } public static void main ( String args [ ] ) { String n = "082" ; System . out . println ( countOnOff ( n ) ) ; } }
class GFG { static int max_length_substring ( String st , int n , int k ) { int max_len = 0 ; int len = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( st . charAt ( i ) == '0' ) len ++ ; else len = 0 ; max_len = Math . max ( max_len , len ) ; } if ( max_len == n ) return n * k ; int pref = 0 , suff = 0 ; for ( int i = 0 ; st . charAt ( i ) == '0' ; ++ i , ++ pref ) ; for ( int i = n - 1 ; st . charAt ( i ) == '0' ; -- i , ++ suff ) ; if ( k > 1 ) max_len = Math . max ( max_len , pref + suff ) ; return max_len ; } public static void main ( String [ ] args ) { int n = 6 ; int k = 3 ; String st = "110010" ; int ans = max_length_substring ( st , n , k ) ; System . out . println ( ans ) ; } }
class GFG { boolean isValidSubString ( String r , int K , int p ) { int c = 0 ; for ( int i = 0 ; i < r . length ( ) ; i ++ ) { if ( ( int ) r . charAt ( i ) > p ) { c ++ ; } } if ( c == K ) { return true ; } else { return false ; } } void countSubStrings ( String s , int K , int p ) { int l = s . length ( ) ; int count = 0 ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = K ; ( i + j ) <= l ; j ++ ) { String r = s . substring ( i , ( i + j ) ) ; if ( isValidSubString ( r , K , p ) ) { count ++ ; } } } System . out . println ( count ) ; } public static void main ( String args [ ] ) { GFG g = new GFG ( ) ; String s = " abepztydba " ; int K = 4 ; int p = 110 ; g . countSubStrings ( s , K , p ) ; } }
import java . util . * ; class GFG { static int count ( String S , int X ) { int count = 0 ; int N = S . length ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( S . charAt ( i ) != '0' ) { for ( int len = 1 ; ( i + len ) <= N ; ++ len ) { int num = Integer . parseInt ( S . substring ( i , i + len ) ) ; if ( num > X ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { String S = "2222" ; int X = 97 ; System . out . println ( count ( S , X ) ) ; } }
import java . util . * ; class Solution { static boolean isBinary ( int n ) { while ( n != 0 ) { int temp = n % 10 ; if ( temp != 0 && temp != 1 ) { return false ; } n = n / 10 ; } return true ; } static void formBinaryStr ( int n , int a [ ] ) { boolean flag = true ; String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( isBinary ( a [ i ] ) ) s += " " + a [ i ] ; else { flag = false ; break ; } } if ( flag ) System . out . print ( s + "NEW_LINE"); else System . out . print ( "-1NEW_LINE"); } public static void main ( String args [ ] ) { int a [ ] = { 10 , 1 , 0 , 11 , 10 } ; int N = a . length ; formBinaryStr ( N , a ) ; } }
import java . util . * ; class GFG { static boolean checkPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - i - 1 ) ) return false ; } return true ; } static boolean CheckOdd ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String x = " " ; for ( int j = i ; j < n ; j ++ ) { x += s . charAt ( j ) ; if ( x . length ( ) % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( CheckOdd ( s ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . util . * ; class solution { static int fact ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } static int getResult ( String str , char ch ) { int has [ ] = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) has [ str . charAt ( i ) - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . length ( ) ; total = total - particular + 1 ; int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } public static void main ( String args [ ] ) { String str = " MISSISSIPPI " ; System . out . println ( getResult ( str , ' S ' ) ) ; } }
import java . util . * ; class GFG { static boolean isAnyNotPalindrome ( String s ) { Set < Character > unique = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) unique . add ( s . charAt ( i ) ) ; if ( unique . size ( ) > 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { String s = " aaaaab " ; if ( isAnyNotPalindrome ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static long factorial ( long n ) { long res = 1 ; for ( int i = 1 ; i <= n ; i ++ ) res = res * i ; return res ; } static long count ( String word ) { int freq [ ] = new int [ 27 ] ; for ( int i = 0 ; i < 27 ; i ++ ) freq [ i ] = 0 ; long vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { freq [ word . charAt ( i ) - ' A ' ] ++ ; if ( word . charAt ( i ) == ' A ' || word . charAt ( i ) == ' E ' || word . charAt ( i ) == ' I ' || word . charAt ( i ) == ' O ' || word . charAt ( i ) == ' U ' ) { vowel ++ ; } else consonant ++ ; } long vowelArrange ; vowelArrange = factorial ( vowel ) ; vowelArrange /= factorial ( freq [ 0 ] ) ; vowelArrange /= factorial ( freq [ 4 ] ) ; vowelArrange /= factorial ( freq [ 8 ] ) ; vowelArrange /= factorial ( freq [ 14 ] ) ; vowelArrange /= factorial ( freq [ 20 ] ) ; long consonantArrange ; consonantArrange = factorial ( consonant ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i != 0 && i != 4 && i != 8 && i != 14 && i != 20 ) consonantArrange /= factorial ( freq [ i ] ) ; } long total = vowelArrange * consonantArrange ; return total ; } public static void main ( String [ ] args ) { String word = " COMPUTER " ; long ans = count ( word ) ; System . out . println ( ans ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static boolean isPalindrome ( String s ) { String s1 = " " ; int N = s . length ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s . charAt ( i ) ; if ( s . equals ( s1 ) ) return true ; return false ; } static boolean createString ( int N ) { String str = " " ; String s = " " + N ; String letters = " abcdefghij " ; int sum = 0 ; String substr = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; substr += letters . charAt ( digit ) ; sum += digit ; } while ( str . length ( ) <= sum ) { str += substr ; } str = str . substring ( 0 , sum ) ; return isPalindrome ( str ) ; } public static void main ( String args [ ] ) { int N = 61 ; boolean flag = createString ( N ) ; if ( flag ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static long productAscii ( String str ) { long prod = 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { prod *= str . charAt ( i ) ; } return prod ; } public static void main ( String [ ] args ) { String str = " GfG " ; System . out . println ( productAscii ( str ) ) ; } }
public class GFG { static boolean check ( String s ) { int n = s . length ( ) ; int [ ] Presum = new int [ n ] ; char [ ] s1 = s . toCharArray ( ) ; Presum [ 0 ] = s1 [ 0 ] - '0' ; for ( int i = 1 ; i < n ; i ++ ) { Presum [ i ] = Presum [ i - 1 ] + ( s1 [ i ] - '0' ) ; } for ( int i = 1 ; i <= n - 1 ; i ++ ) { int sum = Presum [ i - 1 ] ; int presum = 0 ; int it = i ; int flag = 0 ; while ( it < n ) { presum += s1 [ it ] - '0' ; if ( presum == sum ) { presum = 0 ; flag = 1 ; } else if ( presum > sum ) { break ; } it ++ ; } if ( presum == 0 && it == n && flag == 1 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String s = "73452" ; if ( check ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { public static void balancedString ( String str ) { int count = 0 , i ; int n = str . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { System . out . print ( str . charAt ( i ) ) ; count ++ ; } else if ( str . charAt ( i ) == ' ) ' && count != 0 ) { System . out . print ( str . charAt ( i ) ) ; count -- ; } else if ( str . charAt ( i ) != ' ) ' ) System . out . print ( str . charAt ( i ) ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String args [ ] ) { String str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countStrings ( String s ) { int sum = 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == ' $ ' ) sum *= 21 ; else if ( s . charAt ( i ) == ' $ ' ) sum *= 5 ; } return sum ; } public static void main ( String args [ ] ) { String str = " s $ $ e $ " ; System . out . println ( countStrings ( str ) ) ; } }
import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int counter = 0 ; char [ ] str = string . toCharArray ( ) ; int i = 0 ; int size = str . length ; int x ; int length = 0 ; while ( i < size ) { x = str [ i ] - 97 ; if ( ( counter & ( 1 << x ) ) == 0 ) { str [ length ] = ( char ) ( ' a ' + x ) ; counter = counter | ( 1 << x ) ; length ++ ; } i ++ ; } return Arrays . copyOfRange ( str , 0 , length ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }
import java . util . Arrays ; class GFG { static char [ ] removeDuplicatesFromString ( String string ) { int [ ] table = new int [ 256 ] ; char [ ] chars = string . toCharArray ( ) ; int endIndex = 0 ; for ( int i = 0 ; i < chars . length ; i ++ ) { if ( table [ chars [ i ] ] == 0 ) { table [ chars [ i ] ] = - 1 ; chars [ endIndex ++ ] = chars [ i ] ; } } return Arrays . copyOfRange ( chars , 0 , endIndex ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDuplicatesFromString ( str ) ) ; } }
import java . io . * ; class GFG { static int firstPos ( String str , int start , int end ) { int firstChar = - 1 ; for ( int i = start ; i <= end ; i ++ ) { if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { firstChar = i ; break ; } } return firstChar ; } static int lastPos ( String str , int start , int end ) { int lastChar = - 1 ; for ( int i = start ; i >= end ; i -- ) { if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { lastChar = i ; break ; } } return lastChar ; } static boolean isPalindrome ( String str ) { int firstChar = 0 , lastChar = str . length ( ) - 1 ; boolean ch = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { firstChar = firstPos ( str , firstChar , lastChar ) ; lastChar = lastPos ( str , lastChar , firstChar ) ; if ( lastChar < 0 firstChar < 0 ) break ; if ( str . charAt ( firstChar ) == str . charAt ( lastChar ) ) { firstChar ++ ; lastChar -- ; continue ; } ch = false ; break ; } return ch ; } public static void main ( String [ ] args ) { String str = " m ▁ a ▁ 343 ▁ la ▁ y ▁ a ▁ l ▁ am " ; if ( isPalindrome ( str ) ) System . out . print ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . * ; class GfG { static boolean arePermutation ( String str1 , String str2 ) { int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; if ( n1 != n2 ) return false ; char ch1 [ ] = str1 . toCharArray ( ) ; char ch2 [ ] = str2 . toCharArray ( ) ; Arrays . sort ( ch1 ) ; Arrays . sort ( ch2 ) ; for ( int i = 0 ; i < n1 ; i ++ ) if ( ch1 [ i ] != ch2 [ i ] ) return false ; return true ; } public static void main ( String [ ] args ) { String str1 = " test " ; String str2 = " ttew " ; if ( arePermutation ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean arePermutation ( char str1 [ ] , char str2 [ ] ) { int count1 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count1 , 0 ) ; int count2 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count2 , 0 ) ; int i ; for ( i = 0 ; i < str1 . length && i < str2 . length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void main ( String args [ ] ) { char str1 [ ] = ( " geeksforgeeks " ) . toCharArray ( ) ; char str2 [ ] = ( " forgeeksgeeks " ) . toCharArray ( ) ; if ( arePermutation ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
static boolean arePermutation ( char str1 [ ] , char str2 [ ] ) { int count [ ] = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; str1 [ i ] && str2 [ i ] ; i ++ ) { count [ str1 [ i ] ] ++ ; count [ str2 [ i ] ] -- ; } if ( str1 [ i ] str2 [ i ] ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; }
import java . util . HashMap ; class GFG { static int findLongestSub ( String bin ) { int n = bin . length ( ) , i ; int sum = 0 ; HashMap < Integer , Integer > prevSum = new HashMap < > ( ) ; int maxlen = 0 ; int currlen ; for ( i = 0 ; i < n ; i ++ ) { if ( bin . charAt ( i ) == '1' ) sum ++ ; else sum -- ; if ( sum > 0 ) { maxlen = i + 1 ; } else if ( sum <= 0 ) { if ( prevSum . containsKey ( sum - 1 ) ) { currlen = i - ( prevSum . get ( sum - 1 ) == null ? 1 : prevSum . get ( sum - 1 ) ) ; maxlen = Math . max ( maxlen , currlen ) ; } } if ( ! prevSum . containsKey ( sum ) ) prevSum . put ( sum , i ) ; } return maxlen ; } public static void main ( String [ ] args ) { String bin = "1010" ; System . out . println ( findLongestSub ( bin ) ) ; } }
import java . io . * ; class GFG { public static int averageValue ( String s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum_char += ( int ) s . charAt ( i ) ; } return sum_char / s . length ( ) ; } public static void main ( String [ ] args ) { String s = " GeeksforGeeks " ; System . out . println ( averageValue ( s ) ) ; } }
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static boolean check ( String s , int l ) { int freq [ ] = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] >= 2 ) return true ; } return false ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; int l = s . length ( ) ; if ( check ( s , l ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void predictWinner ( String score , int n ) { int count [ ] = new int [ 2 ] , i ; for ( i = 0 ; i < score . length ( ) ; i ++ ) { count [ score . charAt ( i ) - '0' ] ++ ; if ( count [ 0 ] == n && count [ 1 ] < n - 1 ) { System . out . print ( " GEEKS ▁ lost " ) ; return ; } if ( count [ 1 ] == n && count [ 0 ] < n - 1 ) { System . out . print ( " GEEKS ▁ won " ) ; return ; } if ( count [ 0 ] == n - 1 && count [ 1 ] == n - 1 ) { count [ 0 ] = 0 ; count [ 1 ] = 0 ; break ; } } for ( i ++ ; i < score . length ( ) ; i ++ ) { count [ score . charAt ( i ) - '0' ] ++ ; if ( Math . abs ( count [ 0 ] - count [ 1 ] ) == 2 ) { if ( count [ 0 ] > count [ 1 ] ) System . out . print ( " GEEKS ▁ lost " ) ; else System . out . print ( " GEEKS ▁ won " ) ; return ; } } } public static void main ( String [ ] args ) { String score = "1001010101111011101111" ; int n = 15 ; predictWinner ( score , n ) ; } }
class GFG { static boolean isPalindrome ( String str , int l , int r ) { while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } static int CyclicShifts ( String str ) { int n = str . length ( ) ; int left = 0 ; int right = n - 1 ; str = str + str ; int cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } public static void main ( String [ ] args ) { String str = " bccbbaab " ; System . out . println ( CyclicShifts ( str ) ) ; } }
class GFG { static String printString ( int n , String str ) { String str2 = " " ; int extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( int i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) { str2 += str . charAt ( i ) ; } } int countOfStr = n / 26 ; for ( int i = 1 ; i <= countOfStr ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { str2 += str . charAt ( j ) ; } } return str2 ; } public static void main ( String [ ] args ) { int n = 30 ; String str = " zyxwvutsrqponmlkjihgfedcba " ; System . out . println ( printString ( n , str ) ) ; } }
import java . util . Arrays ; class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { String result = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { break ; } result += str1 . charAt ( i ) ; } return ( result ) ; } static void commonPrefix ( String arr [ ] , int n ) { Arrays . sort ( arr ) ; System . out . println ( commonPrefixUtil ( arr [ 0 ] , arr [ n - 1 ] ) ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; commonPrefix ( arr , n ) ; } }
class GFG { public static void countDigits ( String st , int n ) { if ( n > 0 ) { int cnt = 1 , i ; String st2 = " " ; for ( i = 1 ; i < st . length ( ) ; i ++ ) { if ( st . charAt ( i ) == st . charAt ( i - 1 ) ) cnt ++ ; else { st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st . charAt ( i - 1 ) ; cnt = 1 ; } } st2 += ( ( char ) 0 + ( char ) cnt ) ; st2 += st . charAt ( i - 1 ) ; countDigits ( st2 , -- n ) ; } else System . out . print ( st ) ; } public static void main ( String [ ] args ) { String num = "123" ; int n = 3 ; countDigits ( num , n ) ; } }
import java . util . * ; import java . io . * ; public class SpecialSubsequences { public static void generateSubsequences ( String input , String output , int index , ArrayList < String > ans ) { if ( index == input . length ( ) ) { if ( output . length ( ) != 0 ) { ans . add ( output ) ; } return ; } generateSubsequences ( input , output , index + 1 , ans ) ; output += String . valueOf ( input . charAt ( index ) ) ; generateSubsequences ( input , output , index + 1 , ans ) ; output = output . substring ( 0 , output . length ( ) - 1 ) ; output += String . valueOf ( Character . toUpperCase ( input . charAt ( index ) ) ) ; generateSubsequences ( input , output , index + 1 , ans ) ; } public static void printSubsequences ( String s ) { String output = " " ; ArrayList < String > ans = new ArrayList < > ( ) ; generateSubsequences ( s , output , 0 , ans ) ; Collections . sort ( ans ) ; for ( String str : ans ) { System . out . print ( str + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String s = " ab " ; printSubsequences ( s ) ; } }
import java . util . * ; import java . lang . * ; public class GFG { static void printNum ( String s ) { int i = 0 , j = 0 , val = 0 ; LinkedHashMap < Integer , Integer > hm = new LinkedHashMap < > ( ) ; val = ( s . charAt ( 0 ) - '0' ) * 100 + ( s . charAt ( 1 ) - '0' ) * 10 + ( s . charAt ( 2 ) - '0' ) ; hm . put ( val , 1 ) ; for ( i = 3 ; i < s . length ( ) ; i ++ ) { val = ( val % 100 ) * 10 + s . charAt ( i ) - '0' ; if ( hm . containsKey ( val ) ) { hm . put ( val , hm . get ( val ) + 1 ) ; } else { hm . put ( val , 1 ) ; } } for ( Map . Entry < Integer , Integer > en : hm . entrySet ( ) ) { int key = en . getKey ( ) ; int value = en . getValue ( ) ; if ( value > 1 ) System . out . println ( key + " ▁ - ▁ " + value + " ▁ times " ) ; } } public static void main ( String args [ ] ) { String input = "123412345123456" ; printNum ( input ) ; } }
class GFG { static int countSubstrig ( String str1 , String str2 ) { int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; if ( n1 == 0 n1 < n2 ) return 0 ; if ( str1 . substring ( 0 , n2 ) . equals ( str2 ) ) return countSubstrig ( str1 . substring ( n2 - 1 ) , str2 ) + 1 ; return countSubstrig ( str1 . substring ( n2 - 1 ) , str2 ) ; } public static void main ( String args [ ] ) { String str1 = " geeksforgeeks " , str2 = " geeks " ; System . out . println ( countSubstrig ( str1 , str2 ) ) ; str1 = " hikakashi " ; str2 = " hi " ; System . out . println ( countSubstrig ( str1 , str2 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean hasAllcodes ( String s , int k ) { Set < String > us = new HashSet < String > ( ) ; for ( int i = 0 ; i + k <= s . length ( ) ; i ++ ) { us . add ( s . substring ( i , i + k ) ) ; } return ( us . size ( ) == ( 1 << k ) ) ; } public static void main ( String [ ] args ) { String s = "00110110" ; int k = 2 ; if ( hasAllcodes ( s , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
class GFG { static String addBinaryUtil ( String a , String b ) { String result = " " ; int s = 0 ; int i = a . length ( ) - 1 , j = b . length ( ) - 1 ; while ( i >= 0 j >= 0 s == 1 ) { s += ( ( i >= 0 ) ? a . charAt ( i ) - '0' : 0 ) ; s += ( ( j >= 0 ) ? b . charAt ( j ) - '0' : 0 ) ; result = s % 2 + result ; s /= 2 ; i -- ; j -- ; } return result ; } static String addBinary ( String arr [ ] , int n ) { String result = " " ; for ( int i = 0 ; i < n ; i ++ ) { result = addBinaryUtil ( result , arr [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { String arr [ ] = { "1" , "10" , "11" } ; int n = arr . length ; System . out . println ( addBinary ( arr , n ) ) ; } }
class GFG { static void reverse ( char str [ ] , int start , int end ) { char temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } } static void reverseletter ( char str [ ] , int start , int end ) { int wstart , wend ; for ( wstart = wend = start ; wend < end ; wend ++ ) { if ( str [ wend ] == ' ▁ ' ) { continue ; } while ( wend <= end && str [ wend ] != ' ▁ ' ) { wend ++ ; } wend -- ; reverse ( str , wstart , wend ) ; } } public static void main ( String [ ] args ) { char str [ ] = " Ashish ▁ Yadav ▁ Abhishek ▁ Rajput ▁ Sunil ▁ Pundir " . toCharArray ( ) ; reverseletter ( str , 0 , str . length - 1 ) ; System . out . printf ( " % s " , String . valueOf ( str ) ) ; } }
import java . io . * ; class GFG { static int prefix ( String k , int n ) { int a = 0 , b = 0 , count = 0 ; int i = 0 ; int len = k . length ( ) ; for ( i = 0 ; i < len ; i ++ ) { if ( k . charAt ( i ) == ' a ' ) a ++ ; if ( k . charAt ( i ) == ' b ' ) b ++ ; if ( a > b ) { count ++ ; } } if ( count == 0 n == 1 ) { System . out . println ( count ) ; return 0 ; } if ( count == len a - b == 0 ) { System . out . println ( count * n ) ; return 0 ; } int n2 = n - 1 , count2 = 0 ; while ( n2 != 0 ) { for ( i = 0 ; i < len ; i ++ ) { if ( k . charAt ( i ) == ' a ' ) a ++ ; if ( k . charAt ( i ) == ' b ' ) b ++ ; if ( a > b ) { count2 ++ ; } } count += count2 ; n2 -- ; if ( count2 == 0 ) break ; if ( count2 == len ) { count += ( n2 * count2 ) ; break ; } count2 = 0 ; } return count ; } public static void main ( String [ ] args ) { String S = " aba " ; int N = 2 ; System . out . println ( prefix ( S , N ) ) ; S = " baa " ; N = 3 ; System . out . println ( prefix ( S , N ) ) ; } }
class GFG { static final int N = 20 ; static int countPair ( char str [ ] [ ] , int n ) { int [ ] cnt = new int [ 1 << 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { int mask = 0 ; for ( int j = 0 ; j < str [ i ] . length ; ++ j ) mask |= ( 1 << ( str [ i ] [ j ] - '0' ) ) ; cnt [ mask ] ++ ; } int ans = 0 ; for ( int m1 = 0 ; m1 <= 1023 ; m1 ++ ) for ( int m2 = 0 ; m2 <= 1023 ; m2 ++ ) if ( ( m1 m2 ) == 1023 ) { ans += ( ( m1 == m2 ) ? ( cnt [ m1 ] * ( cnt [ m1 ] - 1 ) ) : ( cnt [ m1 ] * cnt [ m2 ] ) ) ; } return ans / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; char str [ ] [ ] = { "129300455" . toCharArray ( ) , "5559948277" . toCharArray ( ) , "012334556" . toCharArray ( ) , "56789" . toCharArray ( ) , "123456879" . toCharArray ( ) } ; System . out . print ( countPair ( str , n ) + "NEW_LINE"); } }
class GFG { static final long mod = 1000000007 ; static final long max = 1001 ; static long nCr [ ] [ ] = new long [ 1003 ] [ 1003 ] ; static void preComputeCoeff ( ) { for ( int i = 0 ; i < max ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = ( nCr [ i - 1 ] [ j - 1 ] + nCr [ i - 1 ] [ j ] ) % mod ; } } } static long computeStringCount ( int N ) { int n = N / 2 ; long ans = 0 ; for ( int i = 2 ; i <= n ; i += 2 ) ans = ( ans + ( ( nCr [ n ] [ i ] * nCr [ n ] [ i / 2 ] ) % mod ) ) % mod ; return ans ; } public static void main ( String [ ] args ) { preComputeCoeff ( ) ; int N = 3 ; System . out . println ( computeStringCount ( N ) ) ; } }
public class GFG { static int max = 100 ; static void countSubsequence ( String s , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s . charAt ( i ) ) { case ' G ' : cntG ++ ; result += C ; break ; case ' F ' : cntF ++ ; C += cntG ; break ; default : continue ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { String s = " GFGFG " ; int n = s . length ( ) ; countSubsequence ( s , n ) ; } }
class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( mx . compareTo ( str . substring ( i ) ) <= 0 ) { mx = str . substring ( i ) ; } } return mx ; } public static void main ( String [ ] args ) { String str = " ababaa " ; System . out . println ( LexicographicalMaxString ( str ) ) ; } }
public class GFG { static boolean checkPalin ( String word ) { int n = word . length ( ) ; word = word . toLowerCase ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . charAt ( i ) != word . charAt ( n - 1 ) ) return false ; return true ; } static String longestPalin ( String str ) { str = str + " ▁ " ; String longestword = " " , word = " " ; int length , length1 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { length = word . length ( ) ; if ( checkPalin ( word ) && length > length1 ) { length1 = length ; longestword = word ; } word = " " ; } } return longestword ; } public static void main ( String args [ ] ) { String s = new String ( " My ▁ name ▁ is ▁ ava ▁ " + " and ▁ i ▁ love ▁ Geeksforgeeks " ) ; if ( longestPalin ( s ) == " " ) System . out . println ( " No ▁ Palindrome " + " ▁ Word " ) ; else System . out . println ( longestPalin ( s ) ) ; } }
class GFG { static boolean isCommonBase ( String base , String s1 , String s2 ) { for ( int j = 0 ; j < s1 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s1 . charAt ( j ) ) { return false ; } } for ( int j = 0 ; j < s2 . length ( ) ; ++ j ) { if ( base . charAt ( j % base . length ( ) ) != s2 . charAt ( j ) ) { return false ; } } return true ; } static int countCommonBases ( String s1 , String s2 ) { int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; int count = 0 ; for ( int i = 1 ; i <= Math . min ( n1 , n2 ) ; i ++ ) { String base = s1 . substring ( 0 , i ) ; if ( isCommonBase ( base , s1 , s2 ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s1 = " pqrspqrs " ; String s2 = " pqrspqrspqrspqrs " ; System . out . println ( countCommonBases ( s1 , s2 ) ) ; } }
import java . util . * ; public class GFG { static String findstring ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( s . charAt ( i - 1 ) == '0' && s . charAt ( i + 1 ) == '0' ) ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; i -- ; if ( i > 0 && s . charAt ( i - 1 ) == '0' ) i -- ; n = s . length ( ) ; } } return s ; } public static void main ( String [ ] args ) { String s = "100100" ; System . out . println ( findstring ( s ) ) ; } }
import java . io . * ; class GFG { static boolean isPerfectSquareString ( String str ) { int sum = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) sum += ( int ) str . charAt ( i ) ; long squareRoot = ( long ) Math . sqrt ( sum ) ; return ( ( squareRoot - Math . floor ( squareRoot ) ) == 0 ) ; } public static void main ( String [ ] args ) { String str = " d " ; if ( isPerfectSquareString ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static boolean is_vow ( char c ) { return ( c == ' a ' ) || ( c == ' e ' ) || ( c == ' i ' ) || ( c == ' o ' ) || ( c == ' u ' ) ; } static void removeVowels ( String str ) { System . out . print ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) if ( ( ! is_vow ( str . charAt ( i - 1 ) ) ) || ( ! is_vow ( str . charAt ( i ) ) ) ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks " ; removeVowels ( str ) ; } }
public class GFG { static void printNumbers ( int n , int d ) { String st = " " + d ; char ch = st . charAt ( 0 ) ; for ( int i = 0 ; i <= n ; i ++ ) { st = " " ; st = st + i ; if ( i == d || st . indexOf ( ch ) >= 0 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 100 , d = 5 ; printNumbers ( n , d ) ; } }
import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > v = new ArrayList < String > ( ) ; static int isPanDigital ( String s ) { int digits [ ] = new int [ 10 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) digits [ s . charAt ( i ) - ( int ) '0' ] = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) if ( digits [ i ] == 0 ) return 0 ; return 1 ; } static int countPandigitalPairs ( ) { int pairs = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) if ( isPanDigital ( v . get ( i ) + v . get ( j ) ) == 1 ) pairs ++ ; return pairs ; } public static void main ( String args [ ] ) { v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( ) ) ; } }
import java . util . * ; class GFG { static int pandigitalMask = ( ( 1 << 10 ) - 1 ) ; static void computeMaskFrequencies ( Vector < String > v , HashMap < Integer , Integer > freq ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int mask = 0 ; HashSet < Integer > digits = new HashSet < > ( ) ; for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) digits . add ( v . get ( i ) . charAt ( j ) - '0' ) ; for ( int it : digits ) { int digit = ( it ) ; mask += ( 1 << digit ) ; } if ( freq . containsKey ( mask ) ) { freq . put ( mask , freq . get ( mask ) + 1 ) ; } else { freq . put ( mask , 1 ) ; } } } static int pandigitalConcatenations ( HashMap < Integer , Integer > freq ) { int ans = 0 ; for ( int i = 1 ; i <= 1023 ; i ++ ) { for ( int j = 1 ; j <= 1023 ; j ++ ) { if ( ( i j ) == pandigitalMask && freq . containsKey ( j ) && freq . containsKey ( i ) ) { if ( i == j ) ans += ( freq . get ( i ) * ( freq . get ( i ) - 1 ) ) ; else ans += ( freq . get ( i ) * freq . get ( j ) ) ; } } } return ans / 2 ; } static int countPandigitalPairs ( Vector < String > v ) { HashMap < Integer , Integer > freq = new HashMap < > ( ) ; computeMaskFrequencies ( v , freq ) ; return pandigitalConcatenations ( freq ) ; } public static void main ( String [ ] args ) { Vector < String > v = new Vector < > ( ) ; v . add ( "123567" ) ; v . add ( "098234" ) ; v . add ( "14765" ) ; v . add ( "19804" ) ; System . out . print ( countPandigitalPairs ( v ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int MAX = 256 ; static void printPalindromePos ( String str ) { Vector < Integer > [ ] pos = new Vector [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) pos [ i ] = new Vector < Integer > ( ) ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) pos [ str . charAt ( i ) ] . add ( i + 1 ) ; int oddCount = 0 ; char oddChar = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pos [ i ] . size ( ) % 2 != 0 ) { oddCount ++ ; oddChar = ( char ) i ; } } if ( oddCount > 1 ) System . out . print ( " NO ▁ PALINDROME " ) ; for ( int i = 0 ; i < MAX ; i ++ ) { int mid = pos [ i ] . size ( ) / 2 ; for ( int j = 0 ; j < mid ; j ++ ) System . out . print ( pos [ i ] . get ( j ) + " ▁ " ) ; } if ( oddCount > 0 ) { int last = pos [ oddChar ] . size ( ) - 1 ; System . out . print ( pos [ oddChar ] . get ( last ) + " ▁ " ) ; pos [ oddChar ] . remove ( pos [ oddChar ] . size ( ) - 1 ) ; } for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int count = pos [ i ] . size ( ) ; for ( int j = count / 2 ; j < count ; j ++ ) System . out . print ( pos [ i ] . get ( j ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String s = " geeksgk " ; printPalindromePos ( s ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; public static int minChanges ( String str ) { int n = str . length ( ) ; if ( n > MAX_CHAR ) return - 1 ; int dist_count = 0 ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ str . charAt ( i ) - ' a ' ] == 0 ) dist_count ++ ; count [ str . charAt ( i ) - ' a ' ] ++ ; } return ( n - dist_count ) ; } public static void main ( String [ ] args ) { String str = " aebaecedabbee " ; System . out . println ( minChanges ( str ) ) ; } }
import java . io . * ; class GFG { static int numofstring ( int n , int m ) { if ( n == 1 ) return m ; if ( n == 2 ) return m * ( m - 1 ) ; return m * ( m - 1 ) * ( int ) Math . pow ( m - 2 , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . println ( numofstring ( n , m ) ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public static int CountSpecialPalindrome ( String str ) { int n = str . length ( ) ; int result = 0 ; int [ ] sameChar = new int [ n ] ; for ( int v = 0 ; v < n ; v ++ ) sameChar [ v ] = 0 ; int i = 0 ; while ( i < n ) { int sameCharCount = 1 ; int j = i + 1 ; while ( j < n && str . charAt ( i ) == str . charAt ( j ) ) { sameCharCount ++ ; j ++ ; } result += ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; sameChar [ i ] = sameCharCount ; i = j ; } for ( int j = 1 ; j < n ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) ) sameChar [ j ] = sameChar [ j - 1 ] ; if ( j > 0 && j < ( n - 1 ) && ( str . charAt ( j - 1 ) == str . charAt ( j + 1 ) && str . charAt ( j ) != str . charAt ( j - 1 ) ) ) result += Math . min ( sameChar [ j - 1 ] , sameChar [ j + 1 ] ) ; } return result - n ; } public static void main ( String args [ ] ) { String str = " abccba " ; System . out . print ( CountSpecialPalindrome ( str ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printBracketNumber ( String exp , int n ) { int left_bnum = 1 ; Stack < Integer > right_bnum = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( exp . charAt ( i ) == ' ( ' ) { System . out . print ( left_bnum + " ▁ " ) ; right_bnum . push ( left_bnum ) ; left_bnum ++ ; } else if ( exp . charAt ( i ) == ' ) ' ) { System . out . print ( right_bnum . peek ( ) + " ▁ " ) ; right_bnum . pop ( ) ; } } } public static void main ( String args [ ] ) { String exp = " ( a + ( b * c ) ) + ( d / e ) " ; int n = exp . length ( ) ; printBracketNumber ( exp , n ) ; } }
import java . io . * ; class GFG { static boolean isCornerPresent ( String str , String corner ) { int n = str . length ( ) ; int cl = corner . length ( ) ; if ( n < cl ) return false ; return ( str . substring ( 0 , cl ) . equals ( corner ) && str . substring ( n - cl , n ) . equals ( corner ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String corner = " geeks " ; if ( isCornerPresent ( str , corner ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int findMaxSegment ( String s , int k ) { int seg_len = s . length ( ) - k ; int res = 0 ; for ( int i = 0 ; i < seg_len ; i ++ ) res = res * 10 + ( s . charAt ( i ) - '0' ) ; int seg_len_pow = ( int ) Math . pow ( 10 , seg_len - 1 ) ; int curr_val = res ; for ( int i = 1 ; i <= ( s . length ( ) - seg_len ) ; i ++ ) { curr_val = curr_val - ( s . charAt ( i - 1 ) - '0' ) * seg_len_pow ; curr_val = curr_val * 10 + ( s . charAt ( i + seg_len - 1 ) - '0' ) ; res = Math . max ( res , curr_val ) ; } return res ; } public static void main ( String [ ] args ) { String s = "8754" ; int k = 2 ; System . out . print ( " Maximum ▁ number ▁ = ▁ " + findMaxSegment ( s , k ) ) ; } }
class GFG { static int isConsecutive ( String str ) { int start ; int length = str . length ( ) ; for ( int i = 0 ; i < length / 2 ; i ++ ) { String new_str = str . substring ( 0 , i + 1 ) ; int num = Integer . parseInt ( new_str ) ; start = num ; while ( new_str . length ( ) < length ) { num ++ ; new_str = new_str + String . valueOf ( num ) ; } if ( new_str . equals ( str ) ) return start ; } return - 1 ; } public static void main ( String [ ] args ) { String str = "99100" ; System . out . println ( " String : ▁ " + str ) ; int start = isConsecutive ( str ) ; if ( start != - 1 ) System . out . println ( "Yes NEW_LINE" + start); else System . out . println ( " No " ) ; String str1 = "121315" ; System . out . println ( " String : " + str1); start = isConsecutive ( str1 ) ; if ( start != - 1 ) System . out . println ( "Yes NEW_LINE" + start); else System . out . println ( " No " ) ; } }
class GFG { static final int MAX_CHAR = 26 ; static void findSubsequence ( String str , int k ) { int a [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) a [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( a [ str . charAt ( i ) - ' a ' ] >= k ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { int k = 2 ; findSubsequence ( " geeksforgeeks " , k ) ; } }
import java . io . * ; class GFG { static boolean isConvertible ( String str1 , String str2 , int k ) { if ( ( str1 . length ( ) + str2 . length ( ) ) < k ) return true ; int commonLength = 0 ; for ( int i = 0 ; i < Math . min ( str1 . length ( ) , str2 . length ( ) ) ; i ++ ) { if ( str1 == str2 ) commonLength ++ ; else break ; } if ( ( k - str1 . length ( ) - str2 . length ( ) + 2 * commonLength ) % 2 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String str1 = " geek " ; String str2 = " geek " ; int k = 7 ; if ( isConvertible ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; str1 = " geeks " ; str2 = " geek " ; k = 5 ; if ( isConvertible ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; class GFG { static final int MAX_CHAR = 26 ; static int maxLower ( String str ) { int n = str . length ( ) ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) { i ++ ; break ; } } int maxCount = 0 ; int count [ ] = new int [ MAX_CHAR ] ; for ( ; i < n ; i ++ ) { if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) { int currCount = 0 ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) { if ( count [ j ] > 0 ) { currCount ++ ; } } maxCount = Math . max ( maxCount , currCount ) ; Arrays . fill ( count , 0 ) ; } if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } } return maxCount ; } public static void main ( String [ ] args ) { String str = " zACaAbbaazzC " ; System . out . println ( maxLower ( str ) ) ; } }
import java . util . * ; class GFG { static int maxLower ( char [ ] str ) { int n = str . length ; int i = 0 ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { i ++ ; break ; } } int maxCount = 0 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( ; i < n ; i ++ ) { if ( str [ i ] >= ' A ' && str [ i ] <= ' Z ' ) { maxCount = Math . max ( maxCount , ( int ) s . size ( ) ) ; s . clear ( ) ; } if ( str [ i ] >= ' a ' && str [ i ] <= ' z ' ) s . add ( ( int ) str [ i ] ) ; } return maxCount ; } public static void main ( String args [ ] ) { String str = " zACaAbbaazzC " ; System . out . println ( maxLower ( str . toCharArray ( ) ) ) ; } }
class GFG { static String convert ( String str ) { String w = " " , z = " " ; str = str . toUpperCase ( ) + " ▁ " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) w = w + ch ; else { z = z + ( Character . toLowerCase ( w . charAt ( 0 ) ) ) + w . substring ( 1 ) + " ▁ " ; w = " " ; } } return z ; } public static void main ( String [ ] args ) { String str = " I ▁ got ▁ intern ▁ at ▁ geeksforgeeks " ; System . out . println ( convert ( str ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static char first ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( Character . isUpperCase ( str . charAt ( i ) ) ) return str . charAt ( i ) ; return 0 ; } public static void main ( String args [ ] ) { String str = " geeksforGeeKS " ; char res = first ( str ) ; if ( res == 0 ) System . out . println ( " No ▁ uppercase ▁ letter " ) ; else System . out . println ( res ) ; } }
import java . util . * ; class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ( ans ) ; } static int numberOfPossiblePallindrome ( String str , int n ) { Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) mp . put ( str . charAt ( i ) , mp . get ( str . charAt ( i ) ) == null ? 1 : mp . get ( str . charAt ( i ) ) + 1 ) ; int fi ; for ( Map . Entry < Character , Integer > it : mp . entrySet ( ) ) { if ( it . getValue ( ) % 2 == 0 ) fi = it . getValue ( ) / 2 ; else { fi = ( it . getValue ( ) - 1 ) / 2 ; k ++ ; } num = num + fi ; den = den * fact ( fi ) ; } if ( num != 0 ) num = fact ( num ) ; int ans = num / den ; if ( k != 0 ) { ans = ans * k ; } return ( ans ) ; } public static void main ( String [ ] args ) { String str = " ababab " ; int n = str . length ( ) ; System . out . println ( numberOfPossiblePallindrome ( str , n ) ) ; } }
import java . io . * ; class GFG { static int evenDecimalValue ( String str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int decimalValue = 0 ; int powerOf2 = 1 ; for ( int k = i ; k <= j ; k ++ ) { decimalValue += ( ( str . charAt ( k ) - '0' ) * powerOf2 ) ; powerOf2 *= 2 ; } if ( decimalValue % 2 == 0 ) result ++ ; } } return result ; } public static void main ( String [ ] args ) { String str = "10010" ; int n = 5 ; System . out . println ( evenDecimalValue ( str , n ) ) ; } }
import java . util . * ; public class GFG { private static int recLen ( String str ) { if ( str . equals ( " " ) ) return 0 ; else return recLen ( str . substring ( 1 ) ) + 1 ; } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; System . out . println ( recLen ( str ) ) ; } }
import java . io . * ; class GFG { static boolean isConsonant ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ! ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) && ch >= 65 && ch <= 90 ; } static int totalConsonants ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isConsonant ( str . charAt ( i ) ) ) ++ count ; return count ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( totalConsonants ( str ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static boolean isConsonant ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) == false && ch >= 65 && ch <= 90 ; } static int totalConsonants ( String str , int n ) { if ( n == 1 ) { if ( isConsonant ( str . charAt ( 0 ) ) ) return 1 ; else return 0 ; } if ( isConsonant ( str . charAt ( n - 1 ) ) ) return totalConsonants ( str , n - 1 ) + 1 ; else return totalConsonants ( str , n - 1 ) ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( totalConsonants ( str , str . length ( ) ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static long reverseBits ( long n ) { long rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( ( n & 1 ) == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } public static boolean isPalindrome ( long n ) { long rev = reverseBits ( n ) ; return ( n == rev ) ; } public static void main ( String argc [ ] ) { long n = 9 ; if ( isPalindrome ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Arrays ; class GFG { static final int MAX = 26 ; static void Swap ( String s , String t , int n ) { int dp [ ] [ ] = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int tot = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( s . charAt ( i ) != t . charAt ( i ) ) tot ++ ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . charAt ( i ) - ' a ' ; int b = t . charAt ( i ) - ' a ' ; if ( a == b ) continue ; if ( dp [ a ] [ b ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( dp [ a ] [ b ] + 1 ) ) ; return ; } dp [ b ] [ a ] = i ; } int A [ ] = new int [ MAX ] , B [ ] = new int [ MAX ] ; Arrays . fill ( A , - 1 ) ; Arrays . fill ( B , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { int a = s . charAt ( i ) - ' a ' ; int b = t . charAt ( i ) - ' a ' ; if ( a == b ) continue ; if ( A [ b ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( A [ b ] + 1 ) ) ; return ; } if ( B [ a ] != - 1 ) { System . out . println ( i + 1 + " ▁ " + ( B [ a ] + 1 ) ) ; return ; } A [ a ] = i ; B [ b ] = i ; } System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { String S = " permanent " ; String T = " pergament " ; int n = S . length ( ) ; if ( S == " " T == " " ) System . out . println ( " Required ▁ string ▁ is ▁ empty . " ) ; else Swap ( S , T , n ) ; } }
import java . io . * ; class GFG { static int MAX = 256 ; static int countPairs ( String s ) { int cnt [ ] = new int [ MAX ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) cnt [ s . charAt ( i ) ] ++ ; int ans = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) ans += cnt [ i ] * cnt [ i ] ; return ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( countPairs ( s ) ) ; } }
class GFG { static String lexNext ( String str , int n ) { char [ ] s = str . toCharArray ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s [ i ] != ' z ' ) { s [ i ] ++ ; return String . valueOf ( s ) ; } s [ i ] = ' a ' ; } return null ; } static public void main ( String [ ] args ) { String S = " abcdeg " , T = " abcfgh " ; int n = S . length ( ) ; String res = lexNext ( S , n ) ; if ( res != T ) { System . out . println ( res ) ; } else { System . out . println ( " - 1" ) ; } } }
import java . io . * ; class Geek { static void solve ( int n , int t , char s [ ] ) { for ( int i = 0 ; i < t ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) if ( s [ j ] == ' B ' && s [ j + 1 ] == ' G ' ) { char temp = s [ j ] ; s [ j ] = s [ j + 1 ] ; s [ j + 1 ] = temp ; j ++ ; } System . out . print ( s ) ; } public static void main ( String args [ ] ) { int n = 6 , t = 2 ; String s = " BBGBBG " ; char str [ ] = s . toCharArray ( ) ; solve ( n , t , str ) ; } }
import java . io . * ; class GFG { static int calSumUtil ( int a [ ] , int b [ ] , int n , int m ) { int [ ] sum = new int [ n ] ; int i = n - 1 , j = m - 1 , k = n - 1 ; int carry = 0 , s = 0 ; while ( j >= 0 ) { s = a [ i ] + b [ j ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; k -- ; i -- ; j -- ; } while ( i >= 0 ) { s = a [ i ] + carry ; sum [ k ] = ( s % 10 ) ; carry = s / 10 ; i -- ; k -- ; } int ans = 0 ; if ( carry == 1 ) ans = 10 ; for ( i = 0 ; i <= n - 1 ; i ++ ) { ans += sum [ i ] ; ans *= 10 ; } return ans / 10 ; } static int calSum ( int a [ ] , int b [ ] , int n , int m ) { if ( n >= m ) return calSumUtil ( a , b , n , m ) ; else return calSumUtil ( b , a , m , n ) ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 3 , 9 } ; int b [ ] = { 6 , 1 } ; int n = a . length ; int m = b . length ; System . out . println ( calSum ( a , b , n , m ) ) ; } }
import java . io . * ; class GFG { static int SIZE = 26 ; static int longCommomAnagramSubseq ( String str1 , String str2 , int n1 , int n2 ) { int [ ] freq1 = new int [ SIZE ] ; int [ ] freq2 = new int [ SIZE ] ; for ( int i = 0 ; i < SIZE ; i ++ ) { freq1 [ i ] = 0 ; freq2 [ i ] = 0 ; } int len = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) freq1 [ ( int ) str1 . charAt ( i ) - ( int ) ' a ' ] ++ ; for ( int i = 0 ; i < n2 ; i ++ ) freq2 [ ( int ) str2 . charAt ( i ) - ( int ) ' a ' ] ++ ; for ( int i = 0 ; i < SIZE ; i ++ ) len += Math . min ( freq1 [ i ] , freq2 [ i ] ) ; return len ; } public static void main ( String args [ ] ) { String str1 = " abdacp " ; String str2 = " ckamb " ; int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; System . out . print ( " Length ▁ = ▁ " + longCommomAnagramSubseq ( str1 , str2 , n1 , n2 ) ) ; } }
class GFG { static boolean isPanalphabeticWindow ( String s , int n ) { char ch = ' a ' ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ch ) ch ++ ; if ( ch == ' z ' + 1 ) return true ; } return false ; } public static void main ( String [ ] args ) { String s = " abujm ▁ zvcd ▁ acefc ▁ deghf " + " ▁ gijklem ▁ n ▁ o ▁ p ▁ pafqrstuvwxyzfap " ; int n = s . length ( ) ; if ( isPanalphabeticWindow ( s , n ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void prime_index ( String input ) { int n = input . length ( ) ; for ( int i = 2 ; i <= n ; i ++ ) if ( isPrime ( i ) ) System . out . print ( input . charAt ( i - 1 ) ) ; } public static void main ( String [ ] args ) { String input = " GeeksforGeeks " ; prime_index ( input ) ; } }
class GFG { static boolean isHeterogram ( String s , int n ) { int hash [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) != ' ▁ ' ) { if ( hash [ s . charAt ( i ) - ' a ' ] == 0 ) hash [ s . charAt ( i ) - ' a ' ] = 1 ; else return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " the ▁ big ▁ dwarf ▁ only ▁ jumps " ; int n = s . length ( ) ; if ( isHeterogram ( s , n ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . util . * ; class GFG { static int largestAnagramSet ( String arr [ ] , int n ) { int maxSize = 0 ; HashMap < String , Integer > count = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { char temp [ ] = arr [ i ] . toCharArray ( ) ; Arrays . sort ( temp ) ; arr [ i ] = new String ( temp ) ; if ( count . containsKey ( arr [ i ] ) ) { count . put ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . put ( arr [ i ] , 1 ) ; } maxSize = Math . max ( maxSize , count . get ( arr [ i ] ) ) ; } return maxSize ; } public static void main ( String [ ] args ) { String arr [ ] = { " ant " , " magenta " , " magnate " , " tan " , " gnamate " } ; int n = arr . length ; System . out . println ( largestAnagramSet ( arr , n ) ) ; String arr1 [ ] = { " cars " , " bikes " , " arcs " , " steer " } ; n = arr1 . length ; System . out . println ( largestAnagramSet ( arr1 , n ) ) ; } }
import java . io . * ; public class GFG { static void countCharacterType ( String str ) { int vowels = 0 , consonant = 0 , specialChar = 0 , digit = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ( ch >= ' a ' && ch <= ' z ' ) || ( ch >= ' A ' && ch <= ' Z ' ) ) { ch = Character . toLowerCase ( ch ) ; ; if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) vowels ++ ; else consonant ++ ; } else if ( ch >= '0' && ch <= '9' ) digit ++ ; else specialChar ++ ; } System . out . println ( " Vowels : ▁ " + vowels ) ; System . out . println ( " Consonant : ▁ " + consonant ) ; System . out . println ( " Digit : ▁ " + digit ) ; System . out . println ( " Special ▁ Character : ▁ " + specialChar ) ; } static public void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks121" ; countCharacterType ( str ) ; } }
class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static String encryptString ( char [ ] s , int n , int k ) { int [ ] cv = new int [ n ] ; int [ ] cc = new int [ n ] ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 0 : 1 ) ; } String ans = " " ; int prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += String . valueOf ( prod ) ; for ( int i = k ; i < s . length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += String . valueOf ( prod ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " hello " ; int n = s . length ( ) ; int k = 2 ; System . out . print ( encryptString ( s . toCharArray ( ) , n , k ) + "NEW_LINE"); } }
import java . util . * ; import java . lang . * ; class GeeksforGeeks { static int Octal ( int decimal ) { int octal = 0 ; String temp = " " ; while ( decimal > 0 ) { int remainder = decimal % 8 ; temp = remainder + temp ; decimal /= 8 ; } for ( int i = 0 ; i < temp . length ( ) ; i ++ ) octal = ( octal * 10 ) + ( temp . charAt ( i ) - '0' ) ; return octal ; } static String Hexadecimal ( int decimal ) { String hex = " " ; while ( decimal > 0 ) { int remainder = decimal % 16 ; if ( remainder >= 0 && remainder <= 9 ) hex = remainder + hex ; else hex = ( char ) ( ' A ' + remainder % 10 ) + hex ; decimal /= 16 ; } return hex ; } static String HTML ( int decimal ) { String html = " " ; html = html + decimal ; html = " & # " + html + " ; " ; return html ; } static void ASCIIlookuptable ( char ch ) { int decimal = ch ; System . out . println ( " Octal ▁ value : ▁ " + Octal ( decimal ) ) ; System . out . println ( " Decimal ▁ value : ▁ " + decimal ) ; System . out . println ( " Hexadecimal ▁ value : ▁ " + Hexadecimal ( decimal ) ) ; System . out . println ( " HTML ▁ value : ▁ " + HTML ( decimal ) ) ; } public static void main ( String args [ ] ) { char ch = ' @ ' ; ASCIIlookuptable ( ch ) ; } }
class GFG { static String replace ( String s , char c1 , char c2 ) { int l = s . length ( ) ; char [ ] arr = s . toCharArray ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( arr [ i ] == c1 ) arr [ i ] = c2 ; else if ( arr [ i ] == c2 ) arr [ i ] = c1 ; } return String . valueOf ( arr ) ; } public static void main ( String [ ] args ) { String s = " grrksfoegrrks " ; char c1 = ' e ' , c2 = ' r ' ; System . out . println ( replace ( s , c1 , c2 ) ) ; } }
import java . util . * ; class Eulerian { public static String fibWord ( int n ) { String Sn_1 = "0" ; String Sn = "01" ; String tmp ; for ( int i = 2 ; i <= n ; i ++ ) { tmp = Sn ; Sn += Sn_1 ; Sn_1 = tmp ; } return Sn ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . print ( fibWord ( n ) ) ; } }
import java . util . * ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } static String convertBinToOct ( String bin ) { int l = bin . length ( ) ; for ( int i = 1 ; i <= ( 3 - l % 3 ) % 3 ; i ++ ) bin = '0' + bin ; Map < String , Character > bin_oct_map = new HashMap < > ( ) ; bin_oct_map . put ( "000" , '0' ) ; bin_oct_map . put ( "001" , '1' ) ; bin_oct_map . put ( "010" , '2' ) ; bin_oct_map . put ( "011" , '3' ) ; bin_oct_map . put ( "100" , '4' ) ; bin_oct_map . put ( "101" , '5' ) ; bin_oct_map . put ( "110" , '6' ) ; bin_oct_map . put ( "111" , '7' ) ; int i = 0 ; String octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ( ) ) break ; } return octal ; } static String decToOctal ( int n ) { String bin = decToBin ( n ) ; return convertBinToOct ( bin ) ; } public static void main ( String [ ] args ) { int n = 151 ; System . out . println ( decToOctal ( n ) ) ; } }
class GFG { static String constructPalin ( char [ ] str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; continue ; } else if ( str [ i ] == ' * ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' * ' ) { str [ j ] = str [ i ] ; continue ; } System . out . println ( " Not ▁ Possible " ) ; return " " ; } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " bca * xc * * b " ; int len = str . length ( ) ; System . out . println ( constructPalin ( str . toCharArray ( ) , len ) ) ; } }
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 ) ; } }
import java . util . HashSet ; class GFG { static int numberOfDifferentSubstrings ( String s , char a , char b ) { int ans = 0 ; int ls = s . length ( ) ; int [ ] x = new int [ ls ] ; int [ ] y = new int [ ls ] ; for ( int i = 0 ; i < ls ; i ++ ) { if ( s . charAt ( i ) == a ) x [ i ] = 1 ; if ( s . charAt ( i ) == b ) y [ i ] = 1 ; } HashSet < String > hash = new HashSet < > ( ) ; String curr_substr = " " ; for ( int i = 0 ; i < ls ; i ++ ) { if ( x [ i ] != 0 ) { for ( int j = i ; j < ls ; j ++ ) { if ( y [ j ] == 0 ) curr_substr += s . charAt ( i ) ; if ( y [ j ] != 0 ) { curr_substr += s . charAt ( j ) ; if ( ! hash . contains ( curr_substr ) ) ans ++ ; hash . add ( curr_substr ) ; } } curr_substr = " " ; } } return ans ; } public static void main ( String [ ] args ) { String s = " codecppforfood " ; char begin = ' c ' ; char end = ' d ' ; System . out . println ( numberOfDifferentSubstrings ( s , begin , end ) ) ; } }
class GFG { static final int max = 100 ; static void carveCross ( String str ) { int n = str . length ( ) ; if ( n % 2 == 0 ) { System . out . print ( " Not ▁ possible . ▁ Please ▁ enter ▁ " + "odd length string.NEW_LINE"); } else { char arr [ ] [ ] = new char [ max ] [ max ] ; int m = n / 2 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { arr [ i ] [ j ] = ' X ' ; } } for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] [ m ] = str . charAt ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { arr [ m ] [ i ] = str . charAt ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } } } public static void main ( String [ ] args ) { String str = " PICTURE " ; carveCross ( str ) ; } }
class GFG { static void generateString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) s += ( ( i & 2 ) > 1 ? ' b ' : ' a ' ) ; System . out . println ( s ) ; } public static void main ( String [ ] args ) { int n = 5 ; generateString ( n ) ; n = 8 ; generateString ( n ) ; n = 10 ; generateString ( n ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . HashMap ; import java . util . HashSet ; import java . util . Map ; import java . util . SortedMap ; import java . util . TreeMap ; class Graph { static String subsequence ( String s , int binary , int len ) { String sub = " " ; for ( int j = 0 ; j < len ; j ++ ) if ( ( binary & ( 1 << j ) ) != 0 ) sub += s . charAt ( j ) ; return sub ; } static void possibleSubsequences ( String s ) { SortedMap < Integer , HashSet < String > > sorted_subsequence = new TreeMap < Integer , HashSet < String > > ( ) ; int len = s . length ( ) ; int limit = ( int ) Math . pow ( 2 , len ) ; for ( int i = 1 ; i <= limit - 1 ; i ++ ) { String sub = subsequence ( s , i , len ) ; if ( ! sorted_subsequence . containsKey ( sub . length ( ) ) ) sorted_subsequence . put ( sub . length ( ) , new HashSet < > ( ) ) ; sorted_subsequence . get ( sub . length ( ) ) . add ( sub ) ; } for ( Map . Entry < Integer , HashSet < String > > it : sorted_subsequence . entrySet ( ) ) { System . out . println ( " Subsequences ▁ of ▁ length ▁ = ▁ " + it . getKey ( ) + " ▁ are : " ) ; for ( String ii : it . getValue ( ) ) System . out . print ( ii + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { String s = " aabc " ; possibleSubsequences ( s ) ; } }
import java . io . * ; class GFG { static int remainder ( String str ) { int len = str . length ( ) ; int num , rem = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = rem * 10 + ( str . charAt ( i ) - '0' ) ; rem = num % 11 ; } return rem ; } public static void main ( String args [ ] ) { String str = "3435346456547566345436457867978" ; System . out . println ( remainder ( str ) ) ; } }
class GFG { static void removeSpecialCharacter ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) < ' A ' || s . charAt ( i ) > ' Z ' && s . charAt ( i ) < ' a ' || s . charAt ( i ) > ' z ' ) { s = s . substring ( 0 , i ) + s . substring ( i + 1 ) ; i -- ; } } System . out . print ( s ) ; } public static void main ( String [ ] args ) { String s = " $ Gee * k ; s . . fo , ▁ r ' Ge ^ eks ? " ; removeSpecialCharacter ( s ) ; } }
class GFG { static void removeSpecialCharacter ( String str ) { char [ ] s = str . toCharArray ( ) ; int j = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( ( s [ i ] >= ' A ' && s [ i ] <= ' Z ' ) || ( s [ i ] >= ' a ' && s [ i ] <= ' z ' ) ) { s [ j ] = s [ i ] ; j ++ ; } } System . out . println ( String . valueOf ( s ) . substring ( 0 , j ) ) ; } public static void main ( String [ ] args ) { String s = " $ Gee * k ; s . . fo , ▁ r ' Ge ^ eks ? " ; removeSpecialCharacter ( s ) ; } }
class GFG { static void findPermutations ( char str [ ] , int index , int n ) { if ( index >= n || ( index + 1 ) >= n ) { System . out . println ( str ) ; return ; } findPermutations ( str , index + 1 , n ) ; swap ( str , index ) ; findPermutations ( str , index + 2 , n ) ; swap ( str , index ) ; } static void swap ( char arr [ ] , int index ) { char temp = arr [ index ] ; arr [ index ] = arr [ index + 1 ] ; arr [ index + 1 ] = temp ; } public static void main ( String [ ] args ) { char str [ ] = "12345" . toCharArray ( ) ; int n = str . length ; findPermutations ( str , 0 , n ) ; } }
public class GFG { static String decodeMedianString ( String s ) { int l = s . length ( ) ; String s1 = " " ; boolean isEven = ( l % 2 == 0 ) ? true : false ; for ( int i = 0 ; i < l ; i += 2 ) { if ( isEven ) { s1 = s . charAt ( i ) + s1 ; s1 += s . charAt ( i + 1 ) ; } else { if ( l - i > 1 ) { s1 += s . charAt ( i ) ; s1 = s . charAt ( i + 1 ) + s1 ; } else { s1 += s . charAt ( i ) ; } } } return s1 ; } public static void main ( String args [ ] ) { String s = " eekgs " ; System . out . println ( decodeMedianString ( s ) ) ; } }
class GFG { static int maximumChars ( String str ) { int n = str . length ( ) ; int res = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( str . charAt ( i ) == str . charAt ( j ) ) res = Math . max ( res , Math . abs ( j - i - 1 ) ) ; return res ; } public static void main ( String [ ] args ) { String str = " abba " ; System . out . println ( maximumChars ( str ) ) ; } }
import java . io . * ; class GFG { static boolean isUnique ( int [ ] a , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; sum += n - 1 ; return ( sum == k ) ; } static public void main ( String [ ] args ) { int [ ] a = { 3 , 3 , 3 } ; int n = a . length ; int k = 12 ; if ( isUnique ( a , n , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int MAX_CHAR = 26 ; public static void commonCharacters ( String str [ ] , int n ) { Boolean [ ] prim = new Boolean [ MAX_CHAR ] ; Arrays . fill ( prim , new Boolean ( true ) ) ; for ( int i = 0 ; i < n ; i ++ ) { Boolean [ ] sec = new Boolean [ MAX_CHAR ] ; Arrays . fill ( sec , new Boolean ( false ) ) ; for ( int j = 0 ; j < str [ i ] . length ( ) ; j ++ ) { if ( prim [ str [ i ] . charAt ( j ) - ' a ' ] ) sec [ str [ i ] . charAt ( j ) - ' a ' ] = true ; } System . arraycopy ( sec , 0 , prim , 0 , MAX_CHAR ) ; } for ( int i = 0 ; i < 26 ; i ++ ) if ( prim [ i ] ) { System . out . print ( Character . toChars ( i + 97 ) ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { String str [ ] = { " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " } ; int n = str . length ; commonCharacters ( str , n ) ; } }
import java . io . * ; class GFG { static boolean isPalindrome ( String s , int i , int j ) { int p = j ; for ( int k = i ; k <= p ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( p ) ) return false ; p -- ; } return true ; } static int countWays ( String s ) { int n = s . length ( ) ; int count = 0 ; if ( isPalindrome ( s , 0 , n - 1 ) ) { for ( int i = n / 2 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) count ++ ; else break ; } { count ++ ; } else } else { for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( n - 1 - i ) ) { int j = n - 1 - i ; if ( isPalindrome ( s , i , n - 2 - i ) ) { for ( int k = i - 1 ; k >= 0 ; k -- ) { if ( s . charAt ( k ) != s . charAt ( j ) ) break ; count ++ ; } count ++ ; } if ( isPalindrome ( s , i + 1 , n - 1 - i ) ) { for ( int k = n - i ; k < n ; k ++ ) { if ( s . charAt ( k ) != s . charAt ( i ) ) break ; count ++ ; } count ++ ; } break ; } } } return count ; } public static void main ( String [ ] args ) { String s = " abca " ; System . out . println ( countWays ( s ) ) ; } }
import java . io . * ; public class GFG { static int countOfSubstringWithKOnes ( String s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int [ ] freq = new int [ N + 1 ] ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s . charAt ( i ) - '0' ) ; if ( countOfOne >= K ) { res += freq [ countOfOne - K ] ; } freq [ countOfOne ] ++ ; } return res ; } static public void main ( String [ ] args ) { String s = "10010" ; int K = 1 ; System . out . println ( countOfSubstringWithKOnes ( s , K ) ) ; } }
import java . util . * ; class NextHigherPalindrome { public static void reverse ( char num [ ] , int i , int j ) { while ( i < j ) { char temp = num [ i ] ; num [ i ] = num [ j ] ; num [ j ] = temp ; i ++ ; j -- ; } } public static void nextPalin ( char num [ ] , int n ) { if ( n <= 3 ) { System . out . println ( " Not ▁ Possible " ) ; return ; } char temp ; int mid = n / 2 - 1 ; int i , j ; for ( i = mid - 1 ; i >= 0 ; i -- ) if ( num [ i ] < num [ i + 1 ] ) break ; if ( i < 0 ) { System . out . println ( " Not ▁ Possible " ) ; return ; } int smallest = i + 1 ; for ( j = i + 2 ; j <= mid ; j ++ ) if ( num [ j ] > num [ i ] && num [ j ] <= num [ smallest ] ) smallest = j ; temp = num [ i ] ; num [ i ] = num [ smallest ] ; num [ smallest ] = temp ; temp = num [ n - i - 1 ] ; num [ n - i - 1 ] = num [ n - smallest - 1 ] ; num [ n - smallest - 1 ] = temp ; reverse ( num , i + 1 , mid ) ; if ( n % 2 == 0 ) reverse ( num , mid + 1 , n - i - 2 ) ; else reverse ( num , mid + 2 , n - i - 2 ) ; String result = String . valueOf ( num ) ; System . out . println ( " Next ▁ Palindrome : ▁ " + result ) ; } public static void main ( String args [ ] ) { String str = "4697557964" ; char num [ ] = str . toCharArray ( ) ; int n = str . length ( ) ; nextPalin ( num , n ) ; } }
import java . io . * ; class GFG { static void printRec ( String number , int extraOnes , int remainingPlaces ) { if ( 0 == remainingPlaces ) { System . out . print ( number + " ▁ " ) ; return ; } printRec ( number + "1" , extraOnes + 1 , remainingPlaces - 1 ) ; if ( 0 < extraOnes ) printRec ( number + "0" , extraOnes - 1 , remainingPlaces - 1 ) ; } static void printNums ( int n ) { String str = " " ; printRec ( str , 0 , n ) ; } public static void main ( String [ ] args ) { int n = 4 ; printNums ( n ) ; } }
import java . io . * ; import java . util . * ; class stem { public static String findstem ( String arr [ ] ) { int n = arr . length ; String s = arr [ 0 ] ; int len = s . length ( ) ; String res = " " ; for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i + 1 ; j <= len ; j ++ ) { String stem = s . substring ( i , j ) ; int k = 1 ; for ( k = 1 ; k < n ; k ++ ) if ( ! arr [ k ] . contains ( stem ) ) break ; if ( k == n && res . length ( ) < stem . length ( ) ) res = stem ; } } return res ; } public static void main ( String args [ ] ) { String arr [ ] = { " grace " , " graceful " , " disgraceful " , " gracefully " } ; String stems = findstem ( arr ) ; System . out . println ( stems ) ; } }
class StringfromCharacters { static final int MAX_CHAR = 256 ; static boolean isPossible ( String s1 , String s2 ) { int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) count [ ( int ) s2 . charAt ( i ) ] ++ ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( count [ ( int ) s1 . charAt ( i ) ] == 0 ) return false ; count [ ( int ) s1 . charAt ( i ) ] -- ; } return true ; } public static void main ( String args [ ] ) { String s1 = " GeeksforGeeks " , s2 = " rteksfoGrdsskGeggehes " ; if ( isPossible ( s1 , s2 ) ) System . out . println ( " Possible " ) ; else System . out . println ( " Not ▁ Possible " ) ; } }
class GFG { static String nextHighUsingAtMostOneSwap ( String st ) { char num [ ] = st . toCharArray ( ) ; int l = num . length ; int posRMax = l - 1 ; int index = - 1 ; for ( int i = l - 2 ; i >= 0 ; i -- ) { if ( num [ i ] >= num [ posRMax ] ) posRMax = i ; else { index = i ; break ; } } if ( index == - 1 ) return " Not ▁ Possible " ; int greatSmallDgt = - 1 ; for ( int i = l - 1 ; i > index ; i -- ) { if ( num [ i ] > num [ index ] ) { if ( greatSmallDgt == - 1 ) greatSmallDgt = i ; else if ( num [ i ] <= num [ greatSmallDgt ] ) greatSmallDgt = i ; } } char temp = num [ index ] ; num [ index ] = num [ greatSmallDgt ] ; num [ greatSmallDgt ] = temp ; return ( String . valueOf ( num ) ) ; } public static void main ( String [ ] args ) { String num = "218765" ; System . out . println ( " Original ▁ number : ▁ " + num ) ; System . out . println ( " Next ▁ higher ▁ number : ▁ " + nextHighUsingAtMostOneSwap ( num ) ) ; } }
import java . util . * ; class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static int longestVowel ( String str ) { int count = 0 , res = 0 ; char [ ] s = str . toCharArray ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( isVowel ( s [ i ] ) ) count ++ ; else { res = Math . max ( res , count ) ; count = 0 ; } } return Math . max ( res , count ) ; } public static void main ( String [ ] args ) { String s = " theeare " ; System . out . println ( longestVowel ( s ) ) ; } }
class GFG { static int MAX_CHAR = 26 ; static boolean check ( int freq [ ] , int k ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] != 0 && freq [ i ] != k ) return false ; return true ; } static int substrings ( String s , int k ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int freq [ ] = new int [ MAX_CHAR ] ; for ( int j = i ; j < s . length ( ) ; j ++ ) { int index = s . charAt ( j ) - ' a ' ; freq [ index ] ++ ; if ( freq [ index ] > k ) break ; else if ( freq [ index ] == k && check ( freq , k ) == true ) res ++ ; } } return res ; } public static void main ( String [ ] args ) { String s = " aabbcc " ; int k = 2 ; System . out . println ( substrings ( s , k ) ) ; s = " aabbc " ; k = 2 ; System . out . println ( substrings ( s , k ) ) ; } }
import java . util . * ; class GFG { static boolean have_same_frequency ( int [ ] freq , int k ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] != 0 && freq [ i ] != k ) { return false ; } } return true ; } static int count_substrings ( String s , int k ) { int count = 0 ; int distinct = 0 ; boolean [ ] have = new boolean [ 26 ] ; Arrays . fill ( have , false ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { have [ ( ( int ) ( s . charAt ( i ) - ' a ' ) ) ] = true ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( have [ i ] ) { distinct ++ ; } } for ( int length = 1 ; length <= distinct ; length ++ ) { int window_length = length * k ; int [ ] freq = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; int window_start = 0 ; int window_end = window_start + window_length - 1 ; for ( int i = window_start ; i <= Math . min ( window_end , s . length ( ) - 1 ) ; i ++ ) { freq [ ( ( int ) ( s . charAt ( i ) - ' a ' ) ) ] ++ ; } while ( window_end < s . length ( ) ) { if ( have_same_frequency ( freq , k ) ) { count ++ ; } freq [ ( ( int ) ( s . charAt ( window_start ) - ' a ' ) ) ] -- ; window_start ++ ; window_end ++ ; if ( window_end < s . length ( ) ) { freq [ ( ( int ) ( s . charAt ( window_end ) - ' a ' ) ) ] ++ ; } } } return count ; } public static void main ( String [ ] args ) { String s = " aabbcc " ; int k = 2 ; System . out . println ( count_substrings ( s , k ) ) ; s = " aabbc " ; k = 2 ; System . out . println ( count_substrings ( s , k ) ) ; } }
class GFG { static final int MAX_CHARS = 26 ; static void longestSubseqWithK ( String str , int k ) { int n = str . length ( ) ; int freq [ ] = new int [ MAX_CHARS ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str . charAt ( i ) - ' a ' ] >= k ) { System . out . print ( str . charAt ( i ) ) ; } } } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 2 ; longestSubseqWithK ( str , k ) ; } }
import java . util . * ; class GFG { static void generate ( Set < String > st , String s ) { if ( s . length ( ) == 0 ) { return ; } if ( ! st . contains ( s ) ) { st . add ( s ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { String t = s ; t = t . substring ( 0 , i ) + t . substring ( i + 1 ) ; generate ( st , t ) ; } } return ; } public static void main ( String args [ ] ) { String s = " xyz " ; TreeSet < String > st = new TreeSet < > ( ) ; generate ( st , s ) ; for ( String str : st ) { System . out . println ( str ) ; } } }
import java . util . HashSet ; public class Subsequence { static HashSet < String > st = new HashSet < > ( ) ; static void subsequence ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { for ( int j = str . length ( ) ; j > i ; j -- ) { String sub_str = str . substring ( i , j ) ; if ( ! st . contains ( sub_str ) ) st . add ( sub_str ) ; for ( int k = 1 ; k < sub_str . length ( ) - 1 ; k ++ ) { StringBuffer sb = new StringBuffer ( sub_str ) ; sb . deleteCharAt ( k ) ; if ( ! st . contains ( sb ) ) ; subsequence ( sb . toString ( ) ) ; } } } } public static void main ( String [ ] args ) { String s = " aabc " ; subsequence ( s ) ; System . out . println ( st ) ; } }
class GFG { static void printSubSeqRec ( String str , int n , int index , String curr ) { if ( index == n ) { return ; } if ( curr != null && ! curr . trim ( ) . isEmpty ( ) ) { System . out . println ( curr ) ; } for ( int i = index + 1 ; i < n ; i ++ ) { curr += str . charAt ( i ) ; printSubSeqRec ( str , n , i , curr ) ; curr = curr . substring ( 0 , curr . length ( ) - 1 ) ; } } static void printSubSeq ( String str ) { int index = - 1 ; String curr = " " ; printSubSeqRec ( str , str . length ( ) , index , curr ) ; } public static void main ( String [ ] args ) { String str = " cab " ; printSubSeq ( str ) ; } }
class GFG { static int countSubstrs ( String str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) res ++ ; return res ; } public static void main ( String [ ] args ) { String str = " abcab " ; int n = str . length ( ) ; System . out . print ( countSubstrs ( str , 0 , n - 1 , n ) ) ; } }
public class Similar_strings { static int countManipulations ( String s1 , String s2 ) { int count = 0 ; int char_count [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { char_count [ s2 . charAt ( i ) - ' a ' ] -- ; } for ( int i = 0 ; i < 26 ; ++ i ) { if ( char_count [ i ] != 0 ) { count += Math . abs ( char_count [ i ] ) ; } } return count / 2 ; } public static void main ( String [ ] args ) { String s1 = " ddcf " ; String s2 = " cedk " ; System . out . println ( countManipulations ( s1 , s2 ) ) ; } }
import java . util . * ; class Gfg { public static void prCharWithFreq ( String s ) { Map < Character , Integer > d = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( d . containsKey ( s . charAt ( i ) ) ) { d . put ( s . charAt ( i ) , d . get ( s . charAt ( i ) ) + 1 ) ; } else { d . put ( s . charAt ( i ) , 1 ) ; } } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( d . get ( s . charAt ( i ) ) != 0 ) { System . out . print ( s . charAt ( i ) ) ; System . out . print ( d . get ( s . charAt ( i ) ) + " ▁ " ) ; d . put ( s . charAt ( i ) , 0 ) ; } } } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; prCharWithFreq ( S ) ; } }
import java . util . * ; class GFG { static String findFirstRepeated ( String s ) { String token [ ] = s . split ( " ▁ " ) ; HashMap < String , Integer > setOfWords = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < token . length ; i ++ ) { if ( setOfWords . containsKey ( token [ i ] ) ) else setOfWords . put ( token [ i ] , 1 ) ; } for ( int i = 0 ; i < token . length ; i ++ ) { int count = setOfWords . get ( token [ i ] ) ; if ( count > 1 ) { return token [ i ] ; } } return " NoRepetition " ; } public static void main ( String args [ ] ) { String s = " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ; String firstWord = findFirstRepeated ( s ) ; if ( ! firstWord . equals ( " NoRepetition " ) ) System . out . println ( " First ▁ repeated ▁ word ▁ : : ▁ " + firstWord ) ; else System . out . println ( " No ▁ Repetitionn " ) ; } }
import java . util . * ; public class GFG { static String findFirstRepeated ( String s ) { String token [ ] = s . split ( " ▁ " ) ; HashSet < String > set = new HashSet < String > ( ) ; for ( int i = 0 ; i < token . length ; i ++ ) { if ( set . contains ( token [ i ] ) ) { return token [ i ] ; } set . add ( token [ i ] ) ; } return " NoRepetition " ; } public static void main ( String args [ ] ) { String s = " Ravi ▁ had ▁ been ▁ saying ▁ that ▁ he ▁ had ▁ been ▁ there " ; String firstWord = findFirstRepeated ( s ) ; if ( ! firstWord . equals ( " NoRepetition " ) ) System . out . println ( " First ▁ repeated ▁ word ▁ : : ▁ " + firstWord ) ; else System . out . println ( " No ▁ Repetitionn " ) ; } }
public class PermuteString { static void permute ( String input ) { int n = input . length ( ) ; int max = 1 << n ; input = input . toLowerCase ( ) ; for ( int i = 0 ; i < max ; i ++ ) { char combination [ ] = input . toCharArray ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) combination [ j ] = ( char ) ( combination [ j ] - 32 ) ; } System . out . print ( combination ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { permute ( " ABC " ) ; } }
class GFG { static final int MAX_CHAR = 256 ; static int countBalance ( String s ) { char [ ] str = s . toCharArray ( ) ; int [ ] rightVisited = new int [ MAX_CHAR ] ; int [ ] leftVisited = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) rightVisited [ str [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftVisited [ str [ i ] ] ++ ; rightVisited [ str [ i ] ] -- ; int j ; for ( j = 0 ; j < MAX_CHAR ; j ++ ) { if ( ( leftVisited [ j ] == 0 && rightVisited [ j ] != 0 ) || ( leftVisited [ j ] != 0 && rightVisited [ j ] == 0 ) ) break ; } if ( j == MAX_CHAR ) res ++ ; } return res ; } public static void main ( String [ ] args ) { String str = " abaababa " ; System . out . println ( countBalance ( str ) ) ; } }
public class minFlips { static int findFlips ( String str , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str . charAt ( i ) ) res ++ ; last = str . charAt ( i ) ; } return res / 2 ; } public static void main ( String [ ] args ) { String str = "00011110001110" ; int n = str . length ( ) ; System . out . println ( findFlips ( str , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void allCharactersSame ( String s ) { Set < Character > s1 = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) s1 . add ( s . charAt ( i ) ) ; if ( s1 . size ( ) == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { String str = " nnnn " ; allCharactersSame ( str ) ; } }
public class GFG { static void printString ( String str , char ch , int count ) { int occ = 0 , i ; if ( count == 0 ) { System . out . println ( str ) ; return ; } for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ch ) occ ++ ; if ( occ == count ) break ; } if ( i < str . length ( ) - 1 ) System . out . println ( str . substring ( i + 1 ) ) ; else System . out . println ( " Empty ▁ string " ) ; } public static void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks " ; printString ( str , ' e ' , 2 ) ; } }
public class GFG { static void printMinIndexChar ( String str , String patt ) { int minIndex = Integer . MAX_VALUE ; int m = str . length ( ) ; int n = patt . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( patt . charAt ( i ) == str . charAt ( j ) && j < minIndex ) { minIndex = j ; break ; } } } if ( minIndex != Integer . MAX_VALUE ) System . out . println ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str . charAt ( minIndex ) ) ; else System . out . println ( " No ▁ character ▁ present " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }
import java . util . HashMap ; public class GFG { static void printMinIndexChar ( String str , String patt ) { HashMap < Character , Integer > hm = new HashMap < > ( ) ; int minIndex = Integer . MAX_VALUE ; int m = str . length ( ) ; int n = patt . length ( ) ; for ( int i = 0 ; i < m ; i ++ ) if ( ! hm . containsKey ( str . charAt ( i ) ) ) hm . put ( str . charAt ( i ) , i ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hm . containsKey ( patt . charAt ( i ) ) && hm . get ( patt . charAt ( i ) ) < minIndex ) minIndex = hm . get ( patt . charAt ( i ) ) ; if ( minIndex != Integer . MAX_VALUE ) System . out . println ( " Minimum ▁ Index ▁ Character ▁ = ▁ " + str . charAt ( minIndex ) ) ; else System . out . println ( " No ▁ character ▁ present " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " set " ; printMinIndexChar ( str , patt ) ; } }
class GFG { static int count9s ( char number [ ] ) { int n = number . length ; int d [ ] = new int [ 9 ] ; d [ 0 ] = 1 ; int result = 0 ; int mod_sum = 0 , continuous_zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( number [ i ] - '0' ) == 0 ) { continuous_zero ++ ; } else { continuous_zero = 0 ; } mod_sum += ( number [ i ] - '0' ) ; mod_sum %= 9 ; result += d [ mod_sum ] ; d [ mod_sum ] ++ ; result -= continuous_zero ; } return result ; } public static void main ( String [ ] args ) { System . out . println ( count9s ( "01809" . toCharArray ( ) ) ) ; System . out . println ( count9s ( "1809" . toCharArray ( ) ) ) ; System . out . println ( count9s ( "4189" . toCharArray ( ) ) ) ; } }
class GFG { static int possibleStrings ( int n , int r , int b , int g ) { int fact [ ] = new int [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; int left = n - ( r + g + b ) ; int sum = 0 ; for ( int i = 0 ; i <= left ; i ++ ) { for ( int j = 0 ; j <= left - i ; j ++ ) { int k = left - ( i + j ) ; sum = sum + fact [ n ] / ( fact [ i + r ] * fact [ j + b ] * fact [ k + g ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int n = 4 , r = 2 ; int b = 0 , g = 1 ; System . out . println ( possibleStrings ( n , r , b , g ) ) ; } }
class GFG { static String maximumNum ( String curr_num , String res ) { int len1 = curr_num . length ( ) ; int len2 = res . length ( ) ; if ( len1 == len2 ) { int i = 0 ; while ( curr_num . charAt ( i ) == res . charAt ( i ) ) i ++ ; if ( curr_num . charAt ( i ) < res . charAt ( i ) ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } static String extractMaximum ( String str ) { int n = str . length ( ) ; String curr_num = " " ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n && str . charAt ( i ) == '0' ) i ++ ; while ( i < n && Character . isDigit ( str . charAt ( i ) ) ) { curr_num = curr_num + str . charAt ( i ) ; i ++ ; } if ( i == n ) break ; if ( curr_num . length ( ) > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . length ( ) == 0 && res . length ( ) == 0 ) res = res + '0' ; return maximumNum ( curr_num , res ) ; } public static void main ( String [ ] args ) { String str = "100klh564abc365bg " ; System . out . println ( extractMaximum ( str ) ) ; } }
class Test { static boolean isDivisible999 ( String num ) { int n = num . length ( ) ; if ( n == 0 && num . charAt ( 0 ) == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num . charAt ( i ++ ) - '0' ) * 100 ; group += ( num . charAt ( i ++ ) - '0' ) * 10 ; group += num . charAt ( i ) - '0' ; gSum += group ; } if ( gSum > 1000 ) { num = Integer . toString ( gSum ) ; n = num . length ( ) ; gSum = isDivisible999 ( num ) ? 1 : 0 ; } return ( gSum == 999 ) ; } public static void main ( String args [ ] ) { String num = "1998" ; System . out . println ( isDivisible999 ( num ) ? " Divisible " : " Not ▁ divisible " ) ; } }
class Test { static final int MAX_CHAR = 26 ; static String arrangeString ( String str ) { int char_count [ ] = new int [ MAX_CHAR ] ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isUpperCase ( str . charAt ( i ) ) ) char_count [ str . charAt ( i ) - ' A ' ] ++ ; else sum = sum + ( str . charAt ( i ) - '0' ) ; } String res = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { char ch = ( char ) ( ' A ' + i ) ; while ( char_count [ i ] -- != 0 ) res = res + ch ; } if ( sum > 0 ) res = res + sum ; return res ; } public static void main ( String args [ ] ) { String str = " ACCBA10D2EW30" ; System . out . println ( arrangeString ( str ) ) ; } }
class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' c == ' A ' c == ' e ' c == ' E ' c == ' i ' c == ' I ' c == ' o ' c == ' O ' c == ' u ' c == ' U ' ) ; } static String reverseVowel ( String str ) { int i = 0 ; int j = str . length ( ) - 1 ; char [ ] str1 = str . toCharArray ( ) ; while ( i < j ) { if ( ! isVowel ( str1 [ i ] ) ) { i ++ ; continue ; } if ( ! isVowel ( str1 [ j ] ) ) { j -- ; continue ; } char t = str1 [ i ] ; str1 [ i ] = str1 [ j ] ; str1 [ j ] = t ; i ++ ; j -- ; } String str2 = String . copyValueOf ( str1 ) ; return str2 ; } public static void main ( String [ ] args ) { String str = " hello ▁ world " ; System . out . println ( reverseVowel ( str ) ) ; } }
class GFG { static void subString ( char str [ ] , int n ) { for ( int len = 1 ; len <= n ; len ++ ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; for ( int k = i ; k <= j ; k ++ ) { System . out . print ( str [ k ] ) ; } System . out . println ( ) ; } } } public static void main ( String [ ] args ) { char str [ ] = { ' a ' , ' b ' , ' c ' } ; subString ( str , str . length ) ; } }
public class ReverseStringPreserveSpace { static void reverses ( String str ) { char [ ] inputArray = str . toCharArray ( ) ; char [ ] result = new char [ inputArray . length ] ; for ( int i = 0 ; i < inputArray . length ; i ++ ) { if ( inputArray [ i ] == ' ▁ ' ) { result [ i ] = ' ▁ ' ; } } int j = result . length - 1 ; for ( int i = 0 ; i < inputArray . length ; i ++ ) { if ( inputArray [ i ] != ' ▁ ' ) { if ( result [ j ] == ' ▁ ' ) { j -- ; } result [ j ] = inputArray [ i ] ; j -- ; } } System . out . println ( String . valueOf ( result ) ) ; } public static void main ( String [ ] args ) { reverses ( " internship ▁ at ▁ geeks ▁ for ▁ geeks " ) ; } }
class GFG { static int MAX_CHAR = 26 ; static void findAndPrintUncommonChars ( String str1 , String str2 ) { int present [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { present [ i ] = 0 ; } int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) { present [ str1 . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < l2 ; i ++ ) { if ( present [ str2 . charAt ( i ) - ' a ' ] == 1 || present [ str2 . charAt ( i ) - ' a ' ] == - 1 ) { present [ str2 . charAt ( i ) - ' a ' ] = - 1 ; } else { present [ str2 . charAt ( i ) - ' a ' ] = 2 ; } } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( present [ i ] == 1 present [ i ] == 2 ) { System . out . print ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String str1 = " characters " ; String str2 = " alphabets " ; findAndPrintUncommonChars ( str1 , str2 ) ; } }
public class GFG { public int lengthOfLastWord ( final String a ) { int len = 0 ; String x = a . trim ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( x . charAt ( i ) == ' ▁ ' ) len = 0 ; else len ++ ; } return len ; } public static void main ( String [ ] args ) { String input = " Geeks ▁ For ▁ Geeks ▁ " ; GFG gfg = new GFG ( ) ; System . out . println ( " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " + gfg . lengthOfLastWord ( input ) ) ; } }
public class GFG { static boolean isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } static int countVowels ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) ++ count ; return count ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( countVowels ( str ) ) ; } }
public class Test { static int x = 32 ; static String toggleCase ( char [ ] a ) { for ( int i = 0 ; i < a . length ; i ++ ) { a [ i ] ^= 32 ; } return new String ( a ) ; } public static void main ( String [ ] args ) { String str = " CheRrY " ; System . out . print ( " Toggle ▁ case : ▁ " ) ; str = toggleCase ( str . toCharArray ( ) ) ; System . out . println ( str ) ; System . out . print ( " Original ▁ string : ▁ " ) ; str = toggleCase ( str . toCharArray ( ) ) ; System . out . println ( str ) ; } }
import java . util . * ; class GFG { static boolean uniqueCharacters ( String str ) { HashSet < Character > char_set = new HashSet < > ( ) ; for ( int c = 0 ; c < str . length ( ) ; c ++ ) { char_set . add ( str . charAt ( c ) ) ; } if ( char_set . size ( ) == str . length ( ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { String str = " GeeksforGeeks " ; if ( uniqueCharacters ( str ) ) { System . out . print ( " The ▁ String ▁ " + str + " has all unique charactersNEW_LINE"); } else { System . out . print ( " The ▁ String ▁ " + str + " has duplicate charactersNEW_LINE"); } } }
class GFG { static void concatenate ( char a [ ] , char b [ ] , char c [ ] , int n1 , int n2 ) { int i ; for ( i = 0 ; i < n1 ; i ++ ) { c [ i ] = a [ i ] ; } for ( int j = 0 ; j < n2 ; j ++ ) { c [ i ++ ] = b [ j ] ; } } public static void main ( String [ ] args ) { char a [ ] = " Hi ▁ This ▁ is ▁ geeksforgeeks . ▁ " . toCharArray ( ) ; int n1 = a . length ; char b [ ] = " You ▁ are ▁ welcome ▁ here . " . toCharArray ( ) ; int n2 = b . length ; char c [ ] = new char [ n1 + n2 ] ; concatenate ( a , b , c , n1 , n2 ) ; for ( int i = 0 ; i < n1 + n2 - 1 ; i ++ ) { System . out . print ( c [ i ] ) ; } } }
import java . util . * ; class GFG { static char [ ] S = " GeKf @ rGeek $ " . toCharArray ( ) ; static void toggleChars ( ) { for ( int i = 0 ; i < S . length ; i ++ ) { if ( Character . isAlphabetic ( S [ i ] ) ) { S [ i ] ^= ( 1 << 5 ) ; } } } public static void main ( String [ ] args ) { toggleChars ( ) ; System . out . print ( " String ▁ after ▁ toggle ▁ " + "NEW_LINE"); System . out . print ( String . valueOf ( S ) ) ; } }
class GFG { static String nextGreaterWithSameDigits ( char [ ] bnum ) { int l = bnum . length ; int i ; for ( i = l - 2 ; i >= 1 ; i -- ) { if ( bnum [ i ] == '0' && bnum [ i + 1 ] == '1' ) { char ch = bnum [ i ] ; bnum [ i ] = bnum [ i + 1 ] ; bnum [ i + 1 ] = ch ; break ; } } if ( i == 0 ) System . out . println ( " no ▁ greater ▁ number " ) ; int j = i + 2 , k = l - 1 ; while ( j < k ) { if ( bnum [ j ] == '1' && bnum [ k ] == '0' ) { char ch = bnum [ j ] ; bnum [ j ] = bnum [ k ] ; bnum [ k ] = ch ; j ++ ; k -- ; } else if ( bnum [ i ] == '0' ) break ; else j ++ ; } return String . valueOf ( bnum ) ; } public static void main ( String [ ] args ) { char [ ] bnum = "10010" . toCharArray ( ) ; System . out . println ( " Binary ▁ representation ▁ of ▁ next ▁ greater ▁ number ▁ = ▁ " + nextGreaterWithSameDigits ( bnum ) ) ; } }
class Test { static void printRotatedString ( String str ) { int len = str . length ( ) ; StringBuffer sb ; for ( int i = 0 ; i < len ; i ++ ) { sb = new StringBuffer ( ) ; int j = i ; int k = 0 ; for ( int k2 = j ; k2 < str . length ( ) ; k2 ++ ) { sb . insert ( k , str . charAt ( j ) ) ; k ++ ; j ++ ; } j = 0 ; while ( j < i ) { sb . insert ( k , str . charAt ( j ) ) ; j ++ ; k ++ ; } System . out . println ( sb ) ; } } public static void main ( String [ ] args ) { String str = new String ( " geeks " ) ; printRotatedString ( str ) ; } }
public class GFG { static Boolean isPalindrome ( String str ) { int n = str . length ( ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( n - i - 1 ) ) return false ; return true ; } static int maxLengthNonPalinSubstring ( String str ) { int n = str . length ( ) ; char ch = str . charAt ( 0 ) ; int i = 1 ; for ( i = 1 ; i < n ; i ++ ) if ( str . charAt ( i ) != ch ) break ; if ( i == n ) return 0 ; if ( isPalindrome ( str ) ) return n - 1 ; return n ; } public static void main ( String args [ ] ) { String str = " abba " ; System . out . println ( " Maximum ▁ Length ▁ = ▁ " + maxLengthNonPalinSubstring ( str ) ) ; } }
class GFG { static void moveSpaceInFront ( char str [ ] ) { int i = str . length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) { char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; i -- ; } } public static void main ( String [ ] args ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . toCharArray ( ) ; moveSpaceInFront ( str ) ; System . out . println ( String . valueOf ( str ) ) ; } }
class GFG { static void moveSpaceInFront ( char str [ ] ) { int i = str . length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ▁ ' ; } public static void main ( String [ ] args ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . toCharArray ( ) ; moveSpaceInFront ( str ) ; System . out . println ( String . valueOf ( str ) ) ; } }
class GFG { static boolean isPalindrome ( char [ ] str ) { int len = str . length ; if ( len == 1 ) return true ; int ptr1 = 0 ; int ptr2 = len - 1 ; while ( ptr2 >= ptr1 ) { if ( str [ ptr1 ] != str [ ptr2 ] ) return false ; ptr1 ++ ; ptr2 -- ; } return true ; } static int noOfAppends ( String s ) { if ( isPalindrome ( s . toCharArray ( ) ) ) return 0 ; s = s . substring ( 1 ) ; return 1 + noOfAppends ( s ) ; } public static void main ( String arr [ ] ) { String s = " abede " ; System . out . printf ( "%dNEW_LINE", noOfAppends(s)); } }
import java . util . * ; import java . lang . * ; class GFG { static int titleToNumber ( String s ) { int result = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { result *= 26 ; result += s . charAt ( i ) - ' A ' + 1 ; } return result ; } public static void main ( String [ ] args ) { System . out . print ( titleToNumber ( " CDA " ) ) ; } }
import java . io . * ; class GFG { static void isKthBitSet ( int n , int k ) { if ( ( ( n >> ( k - 1 ) ) & 1 ) > 0 ) System . out . println ( " SET " ) ; else System . out . println ( " NOT ▁ SET " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 1 ; isKthBitSet ( n , k ) ; } }
import java . util . * ; class GFG { static String reversingString ( char [ ] str , int start , int end ) { while ( start < end ) { str [ start ] ^= str [ end ] ; str [ end ] ^= str [ start ] ; str [ start ] ^= str [ end ] ; ++ start ; -- end ; } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String s = " GeeksforGeeks " ; System . out . println ( reversingString ( s . toCharArray ( ) , 0 , 12 ) ) ; } }
import java . io . * ; class GFG { static String isAnBn ( String str ) { int n = str . length ( ) ; if ( ( n & 1 ) != 0 ) return " No " ; int i ; for ( i = 0 ; i < n / 2 ; i ++ ) if ( str . charAt ( i ) != ' a ' || str . charAt ( n - i - 1 ) != ' b ' ) return " No " ; return " Yes " ; } public static void main ( String [ ] args ) { String str = " ab " ; System . out . println ( isAnBn ( str ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100002 ; static int f ( int i , int m , char s [ ] , int memoize [ ] [ ] ) { if ( i == s . length ) { return 0 ; } if ( memoize [ i ] [ m ] != - 1 ) { return memoize [ i ] [ m ] ; } int x = s [ i ] - '0' ; int ans = ( ( x + m ) % 3 == 0 && x % 2 == 0 ) ? 1 + f ( i + 1 , ( m + x ) % 3 , s , memoize ) : f ( i + 1 , ( m + x ) % 3 , s , memoize ) ; memoize [ i ] [ m ] = ans ; return memoize [ i ] [ m ] ; } static int countDivBy6 ( char s [ ] ) { int n = s . length ; int [ ] [ ] memoize = new int [ n + 1 ] [ 3 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { memoize [ i ] [ j ] = - 1 ; } } int ans = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ i ] == '0' ) { ans ++ ; } else { ans += f ( i , 0 , s , memoize ) ; } } return ans ; } public static void main ( String [ ] args ) { char s [ ] = "4806" . toCharArray ( ) ; System . out . println ( countDivBy6 ( s ) ) ; } }
class GFG { static char MAX_CHAR = 26 ; static void countFreq ( String str , int freq [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } } static boolean canMakePalindrome ( int freq [ ] , int len ) { int count_odd = 0 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { count_odd ++ ; } } if ( len % 2 == 0 ) { if ( count_odd > 0 ) { return false ; } else { return true ; } } if ( count_odd != 1 ) { return false ; } return true ; } static String findOddAndRemoveItsFreq ( int freq [ ] ) { String odd_str = " " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] % 2 != 0 ) { freq [ i ] -- ; odd_str = odd_str + ( char ) ( i + ' a ' ) ; return odd_str ; } } return odd_str ; } static String findPalindromicString ( String str ) { int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; countFreq ( str , freq , len ) ; if ( ! canMakePalindrome ( freq , len ) ) { return " No ▁ Palindromic ▁ String " ; } String odd_str = findOddAndRemoveItsFreq ( freq ) ; String front_str = " " , rear_str = " ▁ " ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { String temp = " " ; if ( freq [ i ] != 0 ) { char ch = ( char ) ( i + ' a ' ) ; for ( int j = 1 ; j <= freq [ i ] / 2 ; j ++ ) { temp = temp + ch ; } front_str = front_str + temp ; rear_str = temp + rear_str ; } } return ( front_str + odd_str + rear_str ) ; } public static void main ( String [ ] args ) { String str = " malayalam " ; System . out . println ( findPalindromicString ( str ) ) ; } }
public class GFG { static boolean checkEquality ( String s ) { return ( s . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) ; } static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) if ( checkEquality ( s . substring ( i , i + len ) ) ) result ++ ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }
public class GFG { static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i ; j < n ; j ++ ) if ( s . charAt ( i ) == s . charAt ( j ) ) result ++ ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }
class GFG { static final int MAX_DIGITS = 6 ; static int getValue ( String str , int i , int m ) { if ( i + m > str . length ( ) ) { return - 1 ; } int value = 0 ; for ( int j = 0 ; j < m ; j ++ ) { int c = str . charAt ( i + j ) - '0' ; if ( c < 0 c > 9 ) { return - 1 ; } value = value * 10 + c ; } return value ; } static int findMissingNumber ( String str ) { for ( int m = 1 ; m <= MAX_DIGITS ; ++ m ) { int n = getValue ( str , 0 , m ) ; if ( n == - 1 ) { break ; } int missingNo = - 1 ; boolean fail = false ; for ( int i = m ; i != str . length ( ) ; i += 1 + Math . log10 ( n ) ) { if ( ( missingNo == - 1 ) && ( getValue ( str , i , ( int ) ( 1 + Math . log10 ( n + 2 ) ) ) == n + 2 ) ) { missingNo = n + 1 ; n += 2 ; else if ( getValue ( str , i , ( int ) ( 1 + Math . log10 ( n + 1 ) ) ) == n + 1 ) { n ++ ; } else { fail = true ; break ; } } if ( ! fail ) { return missingNo ; } } } public static void main ( String [ ] args ) { System . out . println ( findMissingNumber ( "99101102" ) ) ; } }
public class GFG { static char maxRepeating ( String str ) { int len = str . length ( ) ; int count = 0 ; char res = str . charAt ( 0 ) ; for ( int i = 0 ; i < len ; i ++ ) { int cur_count = 1 ; for ( int j = i + 1 ; j < len ; j ++ ) { if ( str . charAt ( i ) != str . charAt ( j ) ) break ; cur_count ++ ; } if ( cur_count > count ) { count = cur_count ; res = str . charAt ( i ) ; } } return res ; } public static void main ( String args [ ] ) { String str = " aaaabbaaccde " ; System . out . println ( maxRepeating ( str ) ) ; } }
import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; str1 = new StringBuilder ( str1 ) . reverse ( ) . toString ( ) ; str2 = new StringBuilder ( str2 ) . reverse ( ) . toString ( ) ; int carry = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n1 ; i < n2 ; i ++ ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; str = new StringBuilder ( str ) . reverse ( ) . toString ( ) ; return str ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }
import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n2 - n1 ; int carry = 0 ; for ( int i = n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i + diff ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n2 - n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; return new StringBuilder ( str ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }
import java . util . Arrays ; import java . util . List ; public class Palin_pair1 { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len / 2 ; i ++ ) if ( str . charAt ( i ) != str . charAt ( len - i - 1 ) ) return false ; return true ; } static boolean checkPalindromePair ( List < String > vect ) { for ( int i = 0 ; i < vect . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < vect . size ( ) ; j ++ ) { String check_str = " " ; check_str = check_str + vect . get ( i ) + vect . get ( j ) ; if ( isPalindrome ( check_str ) ) return true ; check_str = vect . get ( j ) + vect . get ( i ) ; if ( isPalindrome ( check_str ) ) return true ; } } return false ; } public static void main ( String args [ ] ) { List < String > vect = Arrays . asList ( " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " ) ; if ( checkPalindromePair ( vect ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void print ( String str1 ) { char str [ ] = str1 . toCharArray ( ) ; System . out . print ( str [ 0 ] ) ; for ( int i = 1 ; i < str . length ; i ++ ) { if ( ( str [ i ] == str [ i - 1 ] + 1 ) || ( str [ i ] == str [ i - 1 ] - 1 ) ) { System . out . print ( str [ i ] ) ; } else { System . out . print ( "NEW_LINE" + str[i]); } } } public static void main ( String [ ] args ) { String str = " ABCXYZACCD " ; print ( str ) ; } }
class GFG { static boolean areChractersUnique ( String str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { int val = ( str . charAt ( i ) - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } public static void main ( String [ ] args ) { String s = " aaabbccdaa " ; if ( areChractersUnique ( s ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
public class GFG { static int countWords ( String str , int len ) { int count = 1 ; if ( len == 1 ) return count ; if ( str . charAt ( 0 ) == str . charAt ( 1 ) ) count *= 1 ; else count *= 2 ; for ( int j = 1 ; j < len - 1 ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) && str . charAt ( j ) == str . charAt ( j + 1 ) ) count *= 1 ; else if ( str . charAt ( j ) == str . charAt ( j - 1 ) || str . charAt ( j ) == str . charAt ( j + 1 ) || str . charAt ( j - 1 ) == str . charAt ( j + 1 ) ) count *= 2 ; else count *= 3 ; } if ( str . charAt ( len - 1 ) == str . charAt ( len - 2 ) ) count *= 1 ; else count *= 2 ; return count ; } public static void main ( String args [ ] ) { String str = " abc " ; int len = str . length ( ) ; System . out . println ( countWords ( str , len ) ) ; } }
class GFG { static void qType1 ( int l , int x , char str [ ] ) { str [ l - 1 ] = ( char ) x ; } static void qType2 ( int l , int r , char str [ ] ) { int freq [ ] = new int [ 27 ] ; for ( int i = l - 1 ; i <= r - 1 ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int count = 0 ; for ( int j = 0 ; j < 26 ; j ++ ) { if ( freq [ j ] % 2 != 0 ) { count ++ ; } } if ( count <= 1 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; qType1 ( 4 , ' g ' , str ) ; qType2 ( 1 , 4 , str ) ; qType2 ( 2 , 3 , str ) ; qType1 ( 10 , ' t ' , str ) ; qType2 ( 10 , 11 , str ) ; } }
import java . util . * ; class GFG { static int max = 1000 ; static int getFrequency ( int tree [ ] [ ] , int idx , int i ) { int sum = 0 ; while ( idx > 0 ) { sum += tree [ idx ] [ i ] ; idx -= ( idx & - idx ) ; } return sum ; } static void update ( int tree [ ] [ ] , int idx , int val , int i ) { while ( idx <= max ) { tree [ idx ] [ i ] += val ; idx += ( idx & - idx ) ; } } static void qType1 ( int tree [ ] [ ] , int l , int x , char str [ ] ) { update ( tree , l , - 1 , str [ l - 1 ] - 97 + 1 ) ; str [ l - 1 ] = ( char ) x ; update ( tree , l , 1 , str [ l - 1 ] - 97 + 1 ) ; } static void qType2 ( int tree [ ] [ ] , int l , int r , char str [ ] ) { int count = 0 ; for ( int i = 1 ; i <= 26 ; i ++ ) { if ( l == 1 ) { if ( getFrequency ( tree , r , i ) % 2 == 1 ) count ++ ; } else { if ( ( getFrequency ( tree , r , i ) - getFrequency ( tree , l - 1 , i ) ) % 2 == 1 ) count ++ ; } } if ( count <= 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } static void buildBIT ( int tree [ ] [ ] , char str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) update ( tree , i + 1 , 1 , str [ i ] - 97 + 1 ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; int tree [ ] [ ] = new int [ max ] [ 27 ] ; buildBIT ( tree , str , n ) ; qType1 ( tree , 4 , ' g ' , str ) ; qType2 ( tree , 1 , 4 , str ) ; qType2 ( tree , 2 , 3 , str ) ; qType1 ( tree , 10 , ' t ' , str ) ; qType2 ( tree , 10 , 11 , str ) ; } }
class GFG { static void findDivision ( String str , int a , int b ) { int len = str . length ( ) ; int [ ] lr = new int [ len + 1 ] ; lr [ 0 ] = ( ( int ) str . charAt ( 0 ) - ( int ) '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( ( int ) str . charAt ( i ) - ( int ) '0' ) ) % a ; int [ ] rl = new int [ len + 1 ] ; rl [ len - 1 ] = ( ( int ) str . charAt ( len - 1 ) - ( int ) '0' ) % b ; int power10 = 10 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( ( int ) str . charAt ( i ) - ( int ) '0' ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { System . out . println ( " YES " ) ; for ( int k = 0 ; k <= i ; k ++ ) System . out . print ( str . charAt ( k ) ) ; System . out . print ( " , ▁ " ) ; for ( int k = i + 1 ; k < len ; k ++ ) System . out . print ( str . charAt ( k ) ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { String str = "123" ; int a = 12 , b = 3 ; findDivision ( str , a , b ) ; } }
import java . io . * ; class GFG { static boolean isequal ( String str ) { int n = str . length ( ) ; int num = 0 , x = 1 , i = n - 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( '0' <= str . charAt ( i ) && str . charAt ( i ) <= '9' ) { num = ( str . charAt ( i ) - '0' ) * x + num ; x = x * 10 ; if ( num >= n ) return false ; } else break ; } return num == i + 1 ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks13" ; if ( isequal ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { public static boolean kAnagrams ( String str1 , String str2 , int k ) { int flag = 0 ; List < Character > list = new ArrayList < > ( ) ; if ( str1 . length ( ) != str2 . length ( ) ) System . exit ( 0 ) ; char arr1 [ ] = str1 . toCharArray ( ) ; char arr2 [ ] = str2 . toCharArray ( ) ; Arrays . sort ( arr1 ) ; Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( arr1 [ i ] != arr2 [ i ] ) { list . add ( arr2 [ i ] ) ; } } if ( list . size ( ) <= k ) flag = 1 ; if ( flag == 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { String str1 = " fodr " ; String str2 = " gork " ; int k = 2 ; kAnagrams ( str1 , str2 , k ) ; if ( kAnagrams ( str1 , str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; public class GFG { static int countToMake0lternate ( String s ) { int result = 0 ; for ( int i = 0 ; i < ( s . length ( ) - 1 ) ; i ++ ) if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) return result ; } static public void main ( String [ ] args ) { System . out . println ( countToMake0lternate ( "000111" ) ) ; System . out . println ( countToMake0lternate ( "11111" ) ) ; System . out . println ( countToMake0lternate ( "01010101" ) ) ; } }
import java . util . HashMap ; import java . util . Iterator ; class GFG { static boolean checkString ( String str , int k ) { int n = str . length ( ) ; if ( n % k != 0 ) return false ; HashMap < String , Integer > mp = new HashMap < > ( ) ; try { for ( int i = 0 ; i < n ; i += k ) mp . put ( str . substring ( i , k ) , mp . get ( str . substring ( i , k ) ) == null ? 1 : mp . get ( str . substring ( i , k ) ) + 1 ) ; } catch ( Exception e ) { } if ( mp . size ( ) == 1 ) return true ; if ( mp . size ( ) != 2 ) return false ; HashMap . Entry < String , Integer > entry = mp . entrySet ( ) . iterator ( ) . next ( ) ; if ( entry . getValue ( ) == ( n / k - 1 ) || entry . getValue ( ) == 1 ) return true ; return false ; } public static void main ( String [ ] args ) { if ( checkString ( " abababcd " , 2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Test { static final int MAX_CHAR = 26 ; static void printGrouped ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { while ( count [ str . charAt ( i ) - ' a ' ] != 0 ) { System . out . print ( str . charAt ( i ) ) ; count [ str . charAt ( i ) - ' a ' ] -- ; } count [ str . charAt ( i ) - ' a ' ] = 0 ; } } public static void main ( String args [ ] ) { String str = new String ( " geeksforgeeks " ) ; printGrouped ( str ) ; } }
class GFG { static int MAX_CHAR = 26 ; static void sortByPattern ( char [ ] str , char [ ] pat ) { int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . length ; i ++ ) { count [ str [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < pat . length ; i ++ ) { for ( int j = 0 ; j < count [ pat [ i ] - ' a ' ] ; j ++ ) { str [ index ++ ] = pat [ i ] ; } } } public static void main ( String args [ ] ) { char [ ] pat = " bca " . toCharArray ( ) ; char [ ] str = " abc " . toCharArray ( ) ; sortByPattern ( str , pat ) ; System . out . println ( String . valueOf ( str ) ) ; } }
class GFG { static boolean isPossiblePalindrome ( char str [ ] ) { int n = str . length ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( str [ i ] != ' . ' && str [ n - i - 1 ] != ' . ' && str [ i ] != str [ n - i - 1 ] ) return false ; } return true ; } static void smallestPalindrome ( char str [ ] ) { if ( ! isPossiblePalindrome ( str ) ) System . out . println ( " Not ▁ Possible " ) ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' . ' ) { if ( str [ n - i - 1 ] != ' . ' ) str [ i ] = str [ n - i - 1 ] ; else str [ i ] = str [ n - i - 1 ] = ' a ' ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str [ i ] + " " ) ; } public static void main ( String [ ] args ) { String str = " ab . . e . c . a " ; char [ ] s = str . toCharArray ( ) ; smallestPalindrome ( s ) ; } }
class GFG { public static char flip ( char ch ) { return ( ch == '0' ) ? '1' : '0' ; } public static int getFlipWithStartingCharcter ( String str , char expected ) { int flipCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) != expected ) flipCount ++ ; expected = flip ( expected ) ; } return flipCount ; } public static int minFlipToMakeStringAlternate ( String str ) { return Math . min ( getFlipWithStartingCharcter ( str , '0' ) , getFlipWithStartingCharcter ( str , '1' ) ) ; } public static void main ( String args [ ] ) { String str = "0001010111" ; System . out . println ( minFlipToMakeStringAlternate ( str ) ) ; } }
import java . util . * ; class GFG { static int remAnagram ( String str1 , String str2 ) { int count1 [ ] = new int [ 26 ] ; int count2 [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; int result = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) result += Math . abs ( count1 [ i ] - count2 [ i ] ) ; return result ; } public static void main ( String [ ] args ) { String str1 = " bcadeh " , str2 = " hea " ; System . out . println ( remAnagram ( str1 , str2 ) ) ; } }
class GFG { static boolean canBecomeEmpty ( String str , String sub_str ) { while ( str . length ( ) > 0 ) { int idx = str . indexOf ( sub_str ) ; if ( idx == - 1 ) { break ; } str = str . replaceFirst ( sub_str , " " ) ; } return ( str . length ( ) == 0 ) ; } public static void main ( String [ ] args ) { String str = " GEEGEEKSKS " , sub_str = " GEEKS " ; if ( canBecomeEmpty ( str , sub_str ) ) { System . out . print ( " Yes "); } else { System . out . print ( " No "); } } }
import java . util . HashMap ; import java . util . Iterator ; import java . util . Map ; public class AllCharsWithSameFrequencyWithOneVarAllowed { public static boolean checkForVariation ( String str ) { if ( str == null || str . isEmpty ( ) ) { return true ; } Map < Character , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { map . put ( str . charAt ( i ) , map . getOrDefault ( str . charAt ( i ) , 0 ) + 1 ) ; } Iterator < Integer > itr = map . values ( ) . iterator ( ) ; boolean first = true , second = true ; int val1 = 0 , val2 = 0 ; int countOfVal1 = 0 , countOfVal2 = 0 ; while ( itr . hasNext ( ) ) { int i = itr . next ( ) ; if ( first ) { val1 = i ; first = false ; countOfVal1 ++ ; continue ; } if ( i == val1 ) { countOfVal1 ++ ; continue ; } if ( second ) { val2 = i ; countOfVal2 ++ ; second = false ; continue ; } if ( i == val2 ) { countOfVal2 ++ ; continue ; } return false ; } if ( countOfVal1 > 1 && countOfVal2 > 1 ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { System . out . println ( checkForVariation ( " abcbc " ) ) ; } }
class GFG { static int countCompletePairs ( String set1 [ ] , String set2 [ ] , int n , int m ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { String concat = set1 [ i ] + set2 [ j ] ; int frequency [ ] = new int [ 26 ] ; for ( int k = 0 ; k < concat . length ( ) ; k ++ ) { frequency [ concat . charAt ( k ) - ' a ' ] ++ ; } int k ; for ( k = 0 ; k < 26 ; k ++ ) { if ( frequency [ k ] < 1 ) { break ; } } if ( k == 26 ) { result ++ ; } } } return result ; } static public void main ( String [ ] args ) { String set1 [ ] = { " abcdefgh " , " geeksforgeeks " , " lmnopqrst " , " abc " } ; String set2 [ ] = { " ijklmnopqrstuvwxyz " , " abcdefghijklmnopqrstuvwxyz " , " defghijklmnopqrstuvwxyz " } ; int n = set1 . length ; int m = set2 . length ; System . out . println ( countCompletePairs ( set1 , set2 , n , m ) ) ; } }
class GFG { static int countCompletePairs ( String set1 [ ] , String set2 [ ] , int n , int m ) { int result = 0 ; int [ ] con_s1 = new int [ n ] ; int [ ] con_s2 = new int [ m ] ; for ( int i = 0 ; i < n ; i ++ ) { con_s1 [ i ] = 0 ; for ( int j = 0 ; j < set1 [ i ] . length ( ) ; j ++ ) { con_s1 [ i ] = con_s1 [ i ] | ( 1 << ( set1 [ i ] . charAt ( j ) - ' a ' ) ) ; } } for ( int i = 0 ; i < m ; i ++ ) { con_s2 [ i ] = 0 ; for ( int j = 0 ; j < set2 [ i ] . length ( ) ; j ++ ) { con_s2 [ i ] = con_s2 [ i ] | ( 1 << ( set2 [ i ] . charAt ( j ) - ' a ' ) ) ; } } long complete = ( 1 << 26 ) - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( con_s1 [ i ] con_s2 [ j ] ) == complete ) { result ++ ; } } } return result ; } public static void main ( String args [ ] ) { String set1 [ ] = { " abcdefgh " , " geeksforgeeks " , " lmnopqrst " , " abc " } ; String set2 [ ] = { " ijklmnopqrstuvwxyz " , " abcdefghijklmnopqrstuvwxyz " , " defghijklmnopqrstuvwxyz " } ; int n = set1 . length ; int m = set2 . length ; System . out . println ( countCompletePairs ( set1 , set2 , n , m ) ) ; } }
class GFG { static int CHAR_SIZE = 256 ; static boolean checkPattern ( String str , String pat ) { int [ ] label = new int [ CHAR_SIZE ] ; for ( int i = 0 ; i < CHAR_SIZE ; i ++ ) label [ i ] = - 1 ; int order = 1 ; for ( int i = 0 ; i < pat . length ( ) ; i ++ ) { label [ pat . charAt ( i ) ] = order ; order ++ ; } int last_order = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( label [ str . charAt ( i ) ] != - 1 ) { if ( label [ str . charAt ( i ) ] < last_order ) return false ; last_order = label [ str . charAt ( i ) ] ; } } return true ; } public static void main ( String [ ] args ) { String str = " engineers ▁ rock " ; String pattern = " gsr " ; System . out . println ( checkPattern ( str , pattern ) ) ; } }
public class GFG { static char encodedChar ( String str , int k ) { String expand = " " ; for ( int i = 0 ; i < str . length ( ) ; ) { while ( i < str . length ( ) && str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { temp += str . charAt ( i ) ; i ++ ; } while ( i < str . length ( ) && str . charAt ( i ) >= '1' && str . charAt ( i ) <= '9' ) { freq = freq * 10 + str . charAt ( i ) - '0' ; i ++ ; } for ( int j = 1 ; j <= freq ; j ++ ) expand += temp ; } if ( freq == 0 ) expand += temp ; return expand . charAt ( k - 1 ) ; } public static void main ( String args [ ] ) { String str = " ab4c12ed3" ; int k = 21 ; System . out . println ( encodedChar ( str , k ) ) ; } }
import java . util . Map ; import java . util . HashMap ; class GFG { private static final Map < Character , Integer > roman = new HashMap < Character , Integer > ( ) { { put ( ' I ' , 1 ) ; put ( ' V ' , 5 ) ; put ( ' X ' , 10 ) ; put ( ' L ' , 50 ) ; put ( ' C ' , 100 ) ; put ( ' D ' , 500 ) ; put ( ' M ' , 1000 ) ; } } ; private static int romanToInt ( String s ) { int sum = 0 ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != n - 1 && roman . get ( s . charAt ( i ) ) < roman . get ( s . charAt ( i + 1 ) ) ) { sum += roman . get ( s . charAt ( i + 1 ) ) - roman . get ( s . charAt ( i ) ) ; i ++ ; } else { sum += roman . get ( s . charAt ( i ) ) ; } } return sum ; } public static void main ( String [ ] args ) { String input = " MCMIV " ; System . out . print ( " Integer ▁ form ▁ of ▁ Roman ▁ Numeral ▁ is ▁ " + romanToInt ( input ) ) ; } }
class GFG { static int findMinLength ( String arr [ ] , int n ) { int min = Integer . MAX_VALUE ; for ( int i = 0 ; i <= ( n - 1 ) ; i ++ ) { if ( arr [ i ] . length ( ) < min ) { min = arr [ i ] . length ( ) ; } } return min ; } static boolean allContainsPrefix ( String arr [ ] , int n , String str , int start , int end ) { for ( int i = 0 ; i <= ( n - 1 ) ; i ++ ) { String arr_i = arr [ i ] ; for ( int j = start ; j <= end ; j ++ ) if ( arr_i . charAt ( j ) != str . charAt ( j ) ) return false ; } return true ; } static String commonPrefix ( String arr [ ] , int n ) { int index = findMinLength ( arr , n ) ; int low = 0 , high = index - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( allContainsPrefix ( arr , n , arr [ 0 ] , low , mid ) ) { prefix = prefix + arr [ 0 ] . substring ( low , mid + 1 ) ; low = mid + 1 ; } { high = mid - 1 ; } } return prefix ; } public static void main ( String args [ ] ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) > 0 ) System . out . println ( " The ▁ longest ▁ common " + " ▁ prefix ▁ is ▁ " + ans ) ; else System . out . println ( " There ▁ is ▁ no ▁ common " + " ▁ prefix " ) ; } }
class GFG { static String to_upper ( char [ ] in ) { for ( int i = 0 ; i < in . length ; i ++ ) if ( ' a ' <= in [ i ] && in [ i ] <= ' z ' ) in [ i ] &= ~ ( 1 << 5 ) ; return String . valueOf ( in ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( to_upper ( str . toCharArray ( ) ) ) ; } }
class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { String result = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { break ; } result += str1 . charAt ( i ) ; } return ( result ) ; } static String commonPrefix ( String arr [ ] , int low , int high ) { if ( low == high ) { return ( arr [ low ] ) ; } if ( high > low ) { int mid = low + ( high - low ) / 2 ; String str1 = commonPrefix ( arr , low , mid ) ; String str2 = commonPrefix ( arr , mid + 1 , high ) ; return ( commonPrefixUtil ( str1 , str2 ) ) ; } return null ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , 0 , n - 1 ) ; if ( ans . length ( ) != 0 ) { System . out . println ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + ans ) ; } else { System . out . println ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; } } }
class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { String result = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { break ; } result += str1 . charAt ( i ) ; } return ( result ) ; } static String commonPrefix ( String arr [ ] , int n ) { String prefix = arr [ 0 ] ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { prefix = commonPrefixUtil ( prefix , arr [ i ] ) ; } return ( prefix ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) > 0 ) { System . out . printf ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ - ▁ % s " , ans ) ; } else { System . out . printf ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; } } }
import java . io . * ; class GFG { 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 ; return true ; } static int minRemovals ( String str ) { if ( str . charAt ( 0 ) == '') return 0 ; if ( isPalindrome ( str ) ) return 1 ; return 2 ; } public static void main ( String [ ] args ) { System . out . println ( minRemovals ( "010010" ) ) ; System . out . println ( minRemovals ( "0100101" ) ) ; } }
class GFG { static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static void printOneAndTwosComplement ( String bin ) { int n = bin . length ( ) ; int i ; String ones = " " , twos = " " ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) { ones += flip ( bin . charAt ( i ) ) ; } twos = ones ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones . charAt ( i ) == '1' ) { twos = twos . substring ( 0 , i ) + '0' + twos . substring ( i + 1 ) ; } else { twos = twos . substring ( 0 , i ) + '1' + twos . substring ( i + 1 ) ; break ; } } if ( i == - 1 ) { twos = '1' + twos ; } System . out . println ( "1 ' s ▁ complement : ▁ " + ones ) ; ; System . out . println ( "2 ' s ▁ complement : ▁ " + twos ) ; } public static void main ( String [ ] args ) { String bin = "1100" ; printOneAndTwosComplement ( bin ) ; } }
class GFG { static void pattern ( String str , int len ) { for ( int i = 0 ; i < len ; i ++ ) { int j = len - 1 - i ; for ( int k = 0 ; k < len ; k ++ ) { if ( k == i k == j ) System . out . print ( str . charAt ( k ) ) ; else System . out . print ( " ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; pattern ( str , len ) ; } }
class GFG { static final int R = 3 ; static final int C = 3 ; static void printUtil ( String [ ] [ ] arr , int m , int n , String [ ] output ) { output [ m ] = arr [ m ] [ n ] ; if ( m == R - 1 ) { for ( int i = 0 ; i < R ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } for ( int i = 0 ; i < C ; i ++ ) if ( arr [ m + 1 ] [ i ] != " " && m < C ) printUtil ( arr , m + 1 , i , output ) ; } static void print ( String [ ] [ ] arr ) { String [ ] output = new String [ R ] ; for ( int i = 0 ; i < C ; i ++ ) if ( arr [ 0 ] [ i ] != " " ) printUtil ( arr , 0 , i , output ) ; } public static void main ( String [ ] args ) { String [ ] [ ] arr = { { " you " , " we " , " " } , { " have " , " are " , " " } , { " sleep " , " eat " , " drink " } } ; print ( arr ) ; } }
import java . io . * ; import java . math . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean canFormPalindrome ( String str ) { int count [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ ( int ) ( str . charAt ( i ) ) ] ++ ; int odd = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( ( count [ i ] & 1 ) == 1 ) odd ++ ; if ( odd > 1 ) return false ; } return true ; } public static void main ( String args [ ] ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { private static String removeDuplicates ( String s , char ch ) { if ( s == null || s . length ( ) <= 1 ) { return s ; } int i = 0 ; while ( i < s . length ( ) ) { if ( i + 1 < s . length ( ) && s . charAt ( i ) == s . charAt ( i + 1 ) ) { int j = i ; while ( j + 1 < s . length ( ) && s . charAt ( j ) == s . charAt ( j + 1 ) ) { j ++ ; } char lastChar = i > 0 ? s . charAt ( i - 1 ) : ch ; String remStr = removeDuplicates ( s . substring ( j + 1 , s . length ( ) ) , lastChar ) ; s = s . substring ( 0 , i ) ; int k = s . length ( ) , l = 0 ; while ( remStr . length ( ) > 0 && s . length ( ) > 0 && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { while ( remStr . length ( ) > 0 && remStr . charAt ( 0 ) != ch && remStr . charAt ( 0 ) == s . charAt ( s . length ( ) - 1 ) ) { remStr = remStr . substring ( 1 , remStr . length ( ) ) ; } s = s . substring ( 0 , s . length ( ) - 1 ) ; } s = s + remStr ; i = j ; } else { i ++ ; } } return s ; } public static void main ( String [ ] args ) { String str1 = " mississipie " ; System . out . println ( removeDuplicates ( str1 , ' ▁ ' ) ) ; String str2 = " ocvvcolop " ; System . out . println ( removeDuplicates ( str2 , ' ▁ ' ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static ArrayList < String > generateGray ( int n ) { if ( n <= 0 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; } } ; return temp ; } if ( n == 1 ) { ArrayList < String > temp = new ArrayList < String > ( ) { { add ( "0" ) ; add ( "1" ) ; } } ; return temp ; } ArrayList < String > recAns = generateGray ( n - 1 ) ; ArrayList < String > mainAns = new ArrayList < String > ( ) ; for ( int i = 0 ; i < recAns . size ( ) ; i ++ ) { String s = recAns . get ( i ) ; mainAns . add ( "0" + s ) ; } for ( int i = recAns . size ( ) - 1 ; i >= 0 ; i -- ) { String s = recAns . get ( i ) ; mainAns . add ( "1" + s ) ; } return mainAns ; } static void generateGrayarr ( int n ) { ArrayList < String > arr = new ArrayList < String > ( ) ; arr = generateGray ( n ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { System . out . println ( arr . get ( i ) ) ; } } public static void main ( String [ ] args ) { generateGrayarr ( 3 ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean areAnagram ( char str1 [ ] , char str2 [ ] ) { int count1 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count1 , 0 ) ; int count2 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count2 , 0 ) ; int i ; for ( i = 0 ; i < str1 . length && i < str2 . length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void main ( String args [ ] ) { char str1 [ ] = ( " geeksforgeeks " ) . toCharArray ( ) ; char str2 [ ] = ( " forgeeksgeeks " ) . toCharArray ( ) ; if ( areAnagram ( str1 , str2 ) ) System . out . println ( " The ▁ two ▁ strings ▁ are " + " anagram ▁ of ▁ each ▁ other " ) ; else System . out . println ( " The ▁ two ▁ strings ▁ are ▁ not " + " ▁ anagram ▁ of ▁ each ▁ other " ) ; } }
public class GFG { static final int no_of_chars = 256 ; static String findSubString ( String str , String pat ) { int len1 = str . length ( ) ; int len2 = pat . length ( ) ; if ( len1 < len2 ) { System . out . println ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } int hash_pat [ ] = new int [ no_of_chars ] ; int hash_str [ ] = new int [ no_of_chars ] ; for ( int i = 0 ; i < len2 ; i ++ ) hash_pat [ pat . charAt ( i ) ] ++ ; int start = 0 , start_index = - 1 , min_len = Integer . MAX_VALUE ; int count = 0 ; for ( int j = 0 ; j < len1 ; j ++ ) { hash_str [ str . charAt ( j ) ] ++ ; if ( hash_str [ str . charAt ( j ) ] <= hash_pat [ str . charAt ( j ) ] ) count ++ ; if ( count == len2 ) { while ( hash_str [ str . charAt ( start ) ] > hash_pat [ str . charAt ( start ) ] || hash_pat [ str . charAt ( start ) ] == 0 ) { if ( hash_str [ str . charAt ( start ) ] > hash_pat [ str . charAt ( start ) ] ) hash_str [ str . charAt ( start ) ] -- ; start ++ ; } int len_window = j - start + 1 ; if ( min_len > len_window ) { min_len = len_window ; start_index = start ; } } } if ( start_index == - 1 ) { System . out . println ( " No ▁ such ▁ window ▁ exists " ) ; return " " ; } return str . substring ( start_index , start_index + min_len ) ; } public static void main ( String [ ] args ) { String str = " this ▁ is ▁ a ▁ test ▁ string " ; String pat = " tist " ; System . out . print ( "Smallest window is :NEW_LINE " + findSubString ( str , pat ) ) ; } }
class GFG { public static String [ ] RevString ( String [ ] s , int l ) { if ( l % 2 == 0 ) { int j = l / 2 ; while ( j <= l - 1 ) { String temp ; temp = s [ l - j - 1 ] ; s [ l - j - 1 ] = s [ j ] ; s [ j ] = temp ; j += 1 ; } } else { int j = ( l / 2 ) + 1 ; while ( j <= l - 1 ) { String temp ; temp = s [ l - j - 1 ] ; s [ l - j - 1 ] = s [ j ] ; s [ j ] = temp ; j += 1 ; } } return s ; } public static void main ( String [ ] args ) { String s = " getting ▁ good ▁ at ▁ coding ▁ " + " needs ▁ a ▁ lot ▁ of ▁ practice " ; String [ ] words = s . split ( " \\ s " ) ; words = RevString ( words , words . length ) ; s = String . join ( " ▁ " , words ) ; System . out . println ( s ) ; } }
import java . util . * ; class GFG { static String removeDuplicate ( char str [ ] , int n ) { int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) { if ( str [ i ] == str [ j ] ) { break ; } } if ( j == i ) { str [ index ++ ] = str [ i ] ; } } return String . valueOf ( Arrays . copyOf ( str , index ) ) ; } public static void main ( String [ ] args ) { char str [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = str . length ; System . out . println ( removeDuplicate ( str , n ) ) ; } }
import java . util . * ; class GFG { static char [ ] removeDuplicates ( char [ ] s , int n ) { Map < Character , Integer > exists = new HashMap < > ( ) ; String st = " " ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! exists . containsKey ( s [ i ] ) ) { st += s [ i ] ; exists . put ( s [ i ] , 1 ) ; } } return st . toCharArray ( ) ; } public static void main ( String [ ] args ) { char s [ ] = " geeksforgeeks " . toCharArray ( ) ; int n = s . length ; System . out . print ( removeDuplicates ( s , n ) ) ; } }
import java . io . * ; import java . util . * ; import java . util . stream . Collectors ; class GFG { static void findSub ( List < List < Integer > > res , int sum , int K , int N , List < Integer > temp ) { if ( K == 0 && sum == 0 ) { List < Integer > newList = temp . stream ( ) . collect ( Collectors . toList ( ) ) ; res . add ( newList ) ; return ; } if ( sum <= 0 K <= 0 ) { return ; } for ( int i = 1 ; i <= N ; i ++ ) { temp . add ( i ) ; findSub ( res , sum - i , K - 1 , N , temp ) ; temp . remove ( temp . size ( ) - 1 ) ; } } static void UtilPrintSubsequncesOfKSumN ( int N , int K ) { @ SuppressWarnings ( " unchecked " ) List < List < Integer > > res = new ArrayList ( ) ; @ SuppressWarnings ( " unchecked " ) List < Integer > temp = new ArrayList ( ) ; findSub ( res , N , K , N , temp ) ; int sz = res . size ( ) ; System . out . print ( " { ▁ " ) ; for ( int i = 0 ; i < sz ; i ++ ) { System . out . print ( " { ▁ " ) ; for ( int j = 0 ; j < K ; j ++ ) { if ( j == K - 1 ) System . out . print ( res . get ( i ) . get ( j ) + " ▁ " ) ; else System . out . print ( res . get ( i ) . get ( j ) + " , ▁ " ) ; } if ( i == sz - 1 ) System . out . print ( " } " ) ; else System . out . print ( " } , ▁ " ) ; } System . out . print ( " ▁ } " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 2 ; UtilPrintSubsequncesOfKSumN ( N , K ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; pair ( int f , int s ) { this . first = f ; this . second = s ; } } static int minSum = Integer . MAX_VALUE ; static void getMinPathSum ( Map < Integer , ArrayList < pair > > graph , boolean [ ] visited , ArrayList < Integer > necessary , int source , int dest , int currSum ) { if ( src == dest ) { boolean flag = true ; for ( int i : necessary ) { if ( ! visited [ i ] ) { flag = false ; break ; } } if ( flag ) minSum = Math . min ( minSum , currSum ) ; return ; } else { visited [ src ] = true ; for ( pair node : graph . get ( src ) ) { if ( ! visited [ node . first ] ) { visited [ node . first ] = true ; getMinPathSum ( graph , visited , necessary , node . first , dest , currSum + node . second ) ; visited [ node . first ] = false ; } } visited [ src ] = false ; } } public static void main ( String [ ] args ) { Map < Integer , ArrayList < pair > > graph = new HashMap < > ( ) ; for ( int i = 0 ; i <= 6 ; i ++ ) graph . put ( i , new ArrayList < pair > ( ) ) ; graph . get ( 0 ) . add ( new pair ( 1 , 2 ) ) ; graph . get ( 0 ) . add ( new pair ( 2 , 3 ) ) ; graph . get ( 0 ) . add ( new pair ( 3 , 2 ) ) ; graph . get ( 1 ) . add ( new pair ( 4 , 4 ) ) ; graph . get ( 1 ) . add ( new pair ( 0 , 1 ) ) ; graph . get ( 2 ) . add ( new pair ( 4 , 5 ) ) ; graph . get ( 2 ) . add ( new pair ( 5 , 6 ) ) ; graph . get ( 3 ) . add ( new pair ( 5 , 7 ) ) ; graph . get ( 3 ) . add ( new pair ( 0 , 1 ) ) ; graph . get ( 4 ) . add ( new pair ( 6 , 4 ) ) ; graph . get ( 5 ) . add ( new pair ( 4 , 2 ) ) ; graph . get ( 6 ) . add ( new pair ( 7 , 11 ) ) ; int n = 7 ; int source = 0 ; int dest = 6 ; boolean [ ] visited = new boolean [ n ] ; ArrayList < Integer > necessary = new ArrayList < > ( Arrays . asList ( 2 , 4 ) ) ; getMinPathSum ( graph , visited , necessary , source , dest , 0 ) ; if ( minSum == Integer . MAX_VALUE ) System . out . println ( - 1 ) ; else System . out . println ( minSum ) ; } }
import java . util . * ; class GFG { static void comb ( int [ ] arr , int len , int r , int ipos , int [ ] op , int opos , int sum ) { if ( opos == r ) { int sum2 = 0 ; for ( int i = 0 ; i < opos ; i ++ ) { sum2 = sum2 + op [ i ] ; } if ( sum == sum2 ) { for ( int i = 0 ; i < opos ; i ++ ) System . out . print ( op [ i ] + " , ▁ " ) ; System . out . println ( ) ; } return ; } if ( ipos < len ) { comb ( arr , len , r , ipos + 1 , op , opos , sum ) ; op [ opos ] = arr [ ipos ] ; comb ( arr , len , r , ipos + 1 , op , opos + 1 , sum ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 8 , 2 , 12 } ; int K = 3 ; int S = 20 ; int N = arr . length ; int op [ ] = new int [ N ] ; comb ( arr , N , K , 0 , op , 0 , S ) ; } }
class GFG { static int k1 = 0 , k2 = 0 ; static boolean flag = true ; static boolean traverseMatrixDiagonally ( int m [ ] [ ] , int i , int j , int row , int col ) { if ( i >= row j >= col ) { if ( flag ) { int a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; k1 ++ ; } else { int a = k1 ; k1 = k2 ; k2 = a ; flag = ! flag ; } System . out . println ( ) ; return false ; } System . out . print ( m [ i ] [ j ] + " ▁ " ) ; if ( traverseMatrixDiagonally ( m , i + 1 , j + 1 , row , col ) ) { return true ; } if ( traverseMatrixDiagonally ( m , k1 , k2 , row , col ) ) { return true ; } return true ; } public static void main ( String [ ] args ) { int mtrx [ ] [ ] = { { 10 , 11 , 12 , 13 , 14 } , { 15 , 16 , 17 , 18 , 19 } , { 20 , 21 , 22 , 23 , 24 } , { 25 , 26 , 27 , 28 , 29 } , { 30 , 31 , 32 , 33 , 34 } } ; traverseMatrixDiagonally ( mtrx , 0 , 0 , 5 , 5 ) ; } }
import java . util . * ; class GFG { static int n = 5 , m = 7 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > [ ] g = new Vector [ m ] ; static int [ ] col = new int [ n ] ; static boolean cyc ; static int [ ] res = new int [ m ] ; static void dfs ( int v ) { col [ v ] = 1 ; for ( pair p : g [ v ] ) { int to = p . first , id = p . second ; if ( col [ to ] == 0 ) { dfs ( to ) ; res [ id ] = 1 ; } else if ( col [ to ] == 2 ) { res [ id ] = 1 ; } else { res [ id ] = 2 ; cyc = true ; } } col [ v ] = 2 ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < m ; i ++ ) g [ i ] = new Vector < pair > ( ) ; g [ 0 ] . add ( new pair ( 1 , 0 ) ) ; g [ 0 ] . add ( new pair ( 2 , 1 ) ) ; g [ 1 ] . add ( new pair ( 2 , 2 ) ) ; g [ 1 ] . add ( new pair ( 3 , 3 ) ) ; g [ 2 ] . add ( new pair ( 3 , 4 ) ) ; g [ 3 ] . add ( new pair ( 4 , 5 ) ) ; g [ 4 ] . add ( new pair ( 2 , 6 ) ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( col [ i ] == 0 ) { dfs ( i ) ; } } System . out . print ( ( cyc ? 2 : 1 ) + "NEW_LINE"); for ( int i = 0 ; i < m ; ++ i ) { System . out . print ( res [ i ] + " ▁ " ) ; } } }
import java . util . * ; class GFG { public static void sumSubsets ( int set [ ] , int n , int target ) { int x [ ] = new int [ set . length ] ; int j = set . length - 1 ; while ( n > 0 ) { x [ j ] = n % 2 ; n = n / 2 ; j -- ; } int sum = 0 ; for ( int i = 0 ; i < set . length ; i ++ ) if ( x [ i ] == 1 ) sum = sum + set [ i ] ; if ( sum == target ) { System . out . print ( " { " ) ; for ( int i = 0 ; i < set . length ; i ++ ) if ( x [ i ] == 1 ) System . out . print ( set [ i ] + " , ▁ " ) ; System . out . print ( " } , ▁ " ) ; } } public static void findSubsets ( int [ ] arr , int K ) { int x = ( int ) Math . pow ( 2 , arr . length ) ; for ( int i = 1 ; i < x ; i ++ ) sumSubsets ( arr , i , K ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 10 , 12 , 13 , 15 , 18 } ; int K = 30 ; findSubsets ( arr , K ) ; } }
class GFG { static int maxN = 10 ; static int [ ] fact = new int [ maxN ] ; static int ncr ( int n , int r ) { return ( fact [ n ] / fact [ r ] ) / fact [ n - r ] ; } static int findSum ( int [ ] arr , int n ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) fact [ i ] = i * fact [ i - 1 ] ; int mul = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) mul += ( int ) Math . pow ( 2 , i ) * ncr ( n - 1 , i ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += mul * arr [ i ] ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int maze [ ] [ ] ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } static int numberOfPaths ( int m , int n ) { int path = 1 ; for ( int i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; } static int solve ( int maze [ ] [ ] ) { int ans = numberOfPaths ( R , C ) - countPaths ( maze ) ; return ans ; } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( solve ( maze ) ) ; } }
import java . util . * ; class GFG { static void printPath ( Vector < Integer > res , int nThNode , int kThNode ) { if ( kThNode > nThNode ) return ; res . add ( kThNode ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; System . out . print ( "NEW_LINE"); printPath ( res , nThNode , kThNode * 2 ) ; printPath ( res , nThNode , kThNode * 2 + 1 ) ; res . remove ( res . size ( ) - 1 ) ; } static void printPathToCoverAllNodeUtil ( int nThNode ) { Vector < Integer > res = new Vector < Integer > ( ) ; printPath ( res , nThNode , 1 ) ; } public static void main ( String args [ ] ) { int nThNode = 7 ; printPathToCoverAllNodeUtil ( nThNode ) ; } }
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > set = new ArrayList < Integer > ( ) ; static ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; static boolean isPrime ( int x ) { int sqroot = ( int ) Math . sqrt ( x ) ; if ( x == 1 ) return false ; for ( int i = 2 ; i <= sqroot ; i ++ ) if ( x % i == 0 ) return false ; return true ; } static void display ( ) { int length = set . size ( ) ; for ( int i = 0 ; i < length ; i ++ ) System . out . print ( set . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } static void primeSum ( int total , int N , int S , int index ) { if ( total == S && set . size ( ) == N ) { display ( ) ; return ; } if ( total > S || index == prime . size ( ) || set . size ( ) >= N ) return ; set . add ( prime . get ( index ) ) ; primeSum ( total + prime . get ( index ) , N , S , index + 1 ) ; set . remove ( set . size ( ) - 1 ) ; primeSum ( total , N , S , index + 1 ) ; } static void allPrime ( int N , int S , int P ) { for ( int i = P + 1 ; i <= S ; i ++ ) { if ( isPrime ( i ) ) prime . add ( i ) ; } if ( prime . size ( ) < N ) return ; primeSum ( 0 , N , S , 0 ) ; } public static void main ( String args [ ] ) { int S = 54 , N = 2 , P = 3 ; allPrime ( N , S , P ) ; } }
import java . util . * ; class GFG { static int num ; static void grayCodeUtil ( Vector < Integer > res , int n ) { if ( n == 0 ) { res . add ( num ) ; return ; } grayCodeUtil ( res , n - 1 ) ; num = num ^ ( 1 << ( n - 1 ) ) ; grayCodeUtil ( res , n - 1 ) ; } static Vector < Integer > grayCodes ( int n ) { Vector < Integer > res = new Vector < Integer > ( ) ; num = 0 ; grayCodeUtil ( res , n ) ; return res ; } public static void main ( String [ ] args ) { int n = 3 ; Vector < Integer > code = grayCodes ( n ) ; for ( int i = 0 ; i < code . size ( ) ; i ++ ) System . out . print ( code . get ( i ) + "NEW_LINE"); } }
class GFG { static boolean isKPartitionPossibleRec ( int arr [ ] , int subsetSum [ ] , boolean taken [ ] , int subset , int K , int N , int curIdx , int limitIdx ) { if ( subsetSum [ curIdx ] == subset ) { if ( curIdx == K - 2 ) return true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx + 1 , N - 1 ) ; } for ( int i = limitIdx ; i >= 0 ; i -- ) { if ( taken [ i ] ) continue ; int tmp = subsetSum [ curIdx ] + arr [ i ] ; if ( tmp <= subset ) { taken [ i ] = true ; subsetSum [ curIdx ] += arr [ i ] ; boolean nxt = isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , curIdx , i - 1 ) ; taken [ i ] = false ; subsetSum [ curIdx ] -= arr [ i ] ; if ( nxt ) return true ; } } return false ; } static boolean isKPartitionPossible ( int arr [ ] , int N , int K ) { if ( K == 1 ) return true ; if ( N < K ) return false ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; if ( sum % K != 0 ) return false ; int subset = sum / K ; int [ ] subsetSum = new int [ K ] ; boolean [ ] taken = new boolean [ N ] ; for ( int i = 0 ; i < K ; i ++ ) subsetSum [ i ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) taken [ i ] = false ; subsetSum [ 0 ] = arr [ N - 1 ] ; taken [ N - 1 ] = true ; return isKPartitionPossibleRec ( arr , subsetSum , taken , subset , K , N , 0 , N - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 4 , 5 , 3 , 3 } ; int N = arr . length ; int K = 3 ; if ( isKPartitionPossible ( arr , N , K ) ) System . out . println ( " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ possible . " ) ; else System . out . println ( " Partitions ▁ into ▁ equal ▁ sum ▁ is ▁ not ▁ possible . " ) ; } }
import java . util . * ; class GFG { static boolean isParenthesis ( char c ) { return ( ( c == ' ( ' ) || ( c == ' ) ' ) ) ; } static boolean isValidString ( String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) cnt ++ ; else if ( str . charAt ( i ) == ' ) ' ) cnt -- ; if ( cnt < 0 ) return false ; } return ( cnt == 0 ) ; } static void removeInvalidParenthesis ( String str ) { if ( str . isEmpty ( ) ) return ; HashSet < String > visit = new HashSet < String > ( ) ; Queue < String > q = new LinkedList < > ( ) ; String temp ; boolean level = false ; q . add ( str ) ; visit . add ( str ) ; while ( ! q . isEmpty ( ) ) { str = q . peek ( ) ; q . remove ( ) ; if ( isValidString ( str ) ) { System . out . println ( str ) ; level = true ; } if ( level ) continue ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! isParenthesis ( str . charAt ( i ) ) ) continue ; temp = str . substring ( 0 , i ) + str . substring ( i + 1 ) ; if ( ! visit . contains ( temp ) ) { q . add ( temp ) ; visit . add ( temp ) ; } } } } public static void main ( String [ ] args ) { String expression = " ( ) ( ) ) ( ) " ; removeInvalidParenthesis ( expression ) ; expression = " ( ) v ) " ; removeInvalidParenthesis ( expression ) ; } }
import java . util . * ; class GFG { static int N = 4 ; static int [ ] ld = new int [ 30 ] ; static int [ ] rd = new int [ 30 ] ; static int [ ] cl = new int [ 30 ] ; static void printSolution ( int board [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . printf ( " ▁ % d ▁ " , board [ i ] [ j ] ) ; System . out . printf ( "NEW_LINE"); } } static boolean solveNQUtil ( int board [ ] [ ] , int col ) { if ( col >= N ) return true ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( ld [ i - col + N - 1 ] != 1 && rd [ i + col ] != 1 ) && cl [ i ] != 1 ) { board [ i ] [ col ] = 1 ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 1 ; if ( solveNQUtil ( board , col + 1 ) ) return true ; ld [ i - col + N - 1 ] = rd [ i + col ] = cl [ i ] = 0 ; } } return false ; } static boolean solveNQ ( ) { int board [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; if ( solveNQUtil ( board , 0 ) == false ) { System . out . printf ( " Solution ▁ does ▁ not ▁ exist " ) ; return false ; } printSolution ( board ) ; return true ; } public static void main ( String [ ] args ) { solveNQ ( ) ; } }
import java . util . * ; class GFG { static void printPairs ( int a [ ] , int n , int x ) { int i ; int [ ] rem = new int [ x ] ; for ( i = 0 ; i < x ; i ++ ) { rem [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] < x ) { rem [ a [ i ] % x ] ++ ; } } for ( i = 1 ; i < x / 2 ; i ++ ) { if ( rem [ i ] > 0 && rem [ x - i ] > 0 ) { System . out . print ( " Yes " + "NEW_LINE"); break ; } } if ( i >= x / 2 ) { if ( x % 2 == 0 ) { if ( rem [ x / 2 ] > 1 ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } else { if ( rem [ x / 2 ] > 0 && rem [ x - x / 2 ] > 0 ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } } } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 45 , 6 , 10 , 8 } ; int n = 16 ; int arr_size = A . length ; printPairs ( A , arr_size , n ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int fact ( int n ) { int ans = 1 ; for ( int i = 2 ; i <= n ; i ++ ) ans *= i ; return ans ; } static int remainder ( int n , int a , int p ) { int len = fact ( n ) ; int ans = 1 ; for ( int i = 1 ; i <= len ; i ++ ) ans = ( ans * a ) % p ; return ans ; } public static void main ( String args [ ] ) { int A = 2 , N = 1 , P = 2 ; System . out . println ( remainder ( N , A , P ) ) ; } }
class GFG { static boolean check ( int n , int w ) { int [ ] a = new int [ 105 ] ; int p = 0 ; while ( n > 0 ) { a [ p ++ ] = n % w ; n /= w ; } boolean flag = true ; for ( int i = 0 ; i <= 100 ; i ++ ) { if ( a [ i ] == 0 a [ i ] == 1 ) continue ; else if ( a [ i ] == w a [ i ] == w - 1 ) a [ i + 1 ] ++ ; else flag = false ; } return flag ; } public static void main ( String [ ] args ) { int B = 3 , N = 7 ; if ( check ( N , B ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static long mod = 1000000007 ; static int exponentMod ( int A , int B ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 ) ; y = ( y * y ) % mod ; } else { y = A % mod ; y = ( y * exponentMod ( A , B - 1 ) % mod ) % mod ; } return ( int ) ( ( y + mod ) % mod ) ; } static void countWays ( int N ) { long select = exponentMod ( 2 , N - 1 ) ; long ways = ( ( N % mod ) * ( select % mod ) ) ; ways %= mod ; System . out . print ( ways ) ; } public static void main ( String [ ] args ) { int N = 5 ; countWays ( N ) ; } }
import java . util . * ; class GFG { static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static boolean isArmstrong ( int x ) { int n = String . valueOf ( x ) . length ( ) ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp /= 10 ; } if ( sum1 == x ) return true ; return false ; } static int MaxUtil ( int [ ] st , int ss , int se , int l , int r , int node ) { if ( l <= ss && r >= se ) return st [ node ] ; if ( se < l ss > r ) return - 1 ; int mid = getMid ( ss , se ) ; return Math . max ( MaxUtil ( st , ss , mid , l , r , 2 * node ) , MaxUtil ( st , mid + 1 , se , l , r , 2 * node + 1 ) ) ; } static void updateValue ( int arr [ ] , int [ ] st , int ss , int se , int index , int value , int node ) { if ( index < ss index > se ) { System . out . print ( " Invalid ▁ Input " + "NEW_LINE"); return ; } if ( ss == se ) { arr [ index ] = value ; if ( isArmstrong ( value ) ) st [ node ] = value ; else st [ node ] = - 1 ; } else { int mid = getMid ( ss , se ) ; if ( index >= ss && index <= mid ) updateValue ( arr , st , ss , mid , index , value , 2 * node ) ; else updateValue ( arr , st , mid + 1 , se , index , value , 2 * node + 1 ) ; st [ node ] = Math . max ( st [ 2 * node + 1 ] , st [ 2 * node + 2 ] ) ; } return ; } static int getMax ( int [ ] st , int n , int l , int r ) { if ( l < 0 r > n - 1 l > r ) { System . out . printf ( " Invalid ▁ Input " ) ; return - 1 ; } return MaxUtil ( st , 0 , n - 1 , l , r , 0 ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( isArmstrong ( arr [ ss ] ) ) st [ si ] = arr [ ss ] ; else st [ si ] = - 1 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = Math . max ( constructSTUtil ( arr , ss , mid , st , si * 2 ) , constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 1 ) ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int [ ] st = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } public static void main ( String [ ] args ) { int arr [ ] = { 192 , 113 , 535 , 7 , 19 , 111 } ; int n = arr . length ; int [ ] st = constructST ( arr , n ) ; System . out . print ( " Maximum ▁ armstrong ▁ " + " number ▁ in ▁ given ▁ range ▁ = ▁ " + getMax ( st , n , 1 , 3 ) + "NEW_LINE"); updateValue ( arr , st , 0 , n - 1 , 1 , 153 , 0 ) ; System . out . print ( " Updated ▁ Maximum ▁ armstrong ▁ " + " number ▁ in ▁ given ▁ range ▁ = ▁ " + getMax ( st , n , 1 , 3 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static final int MAX = 1000 ; static boolean isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ? true : false ; } static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int queryUtil ( int [ ] st , int ss , int se , int qs , int qe , int index ) { if ( qs <= ss && qe >= se ) return st [ index ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return queryUtil ( st , ss , mid , qs , qe , 2 * index + 1 ) + queryUtil ( st , mid + 1 , se , qs , qe , 2 * index + 2 ) ; } static void updateValueUtil ( int [ ] st , int ss , int se , int i , int diff , int si ) { if ( i < ss i > se ) return ; st [ si ] = st [ si ] + diff ; if ( se != ss ) { int mid = getMid ( ss , se ) ; updateValueUtil ( st , ss , mid , i , diff , 2 * si + 1 ) ; updateValueUtil ( st , mid + 1 , se , i , diff , 2 * si + 2 ) ; } } static void updateValue ( int arr [ ] , int [ ] st , int n , int i , int new_val ) { if ( i < 0 i > n - 1 ) { System . out . printf ( " Invalid ▁ Input " ) ; return ; } int diff = 0 , oldValue ; oldValue = arr [ i ] ; arr [ i ] = new_val ; if ( isPerfectSquare ( oldValue ) && isPerfectSquare ( new_val ) ) return ; if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) return ; if ( isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = - 1 ; } if ( ! isPerfectSquare ( oldValue ) && ! isPerfectSquare ( new_val ) ) { diff = 1 ; } updateValueUtil ( st , 0 , n - 1 , i , diff , 0 ) ; } static void query ( int [ ] st , int n , int qs , int qe ) { int perfectSquareInRange = queryUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; System . out . print ( perfectSquareInRange + "NEW_LINE"); } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( isPerfectSquare ( arr [ ss ] ) ) st [ si ] = 1 ; else st [ si ] = 0 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) ) ) ; int max_size = 4 * ( int ) Math . pow ( 2 , x ) + 1 ; int [ ] st = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } public static void main ( String [ ] args ) { int arr [ ] = { 16 , 15 , 8 , 9 , 14 , 25 } ; int n = arr . length ; int [ ] st = constructST ( arr , n ) ; int start = 0 ; int end = 4 ; query ( st , n , start , end ) ; int i = 3 ; int x = 11 ; updateValue ( arr , st , n , i , x ) ; start = 0 ; end = 4 ; query ( st , n , start , end ) ; } }
import java . util . * ; class GFG { static class Node { int value ; int max_set_bits ; } ; static Node [ ] tree = new Node [ 4 * 10000 ] ; static int setBits ( int x ) { int parity = 0 ; while ( x != 0 ) { if ( x % 2 == 1 ) parity ++ ; x = x >> 1 ; } return parity ; } static void buildSegmentTree ( int a [ ] , int index , int beg , int end ) { if ( beg == end ) { tree [ index ] . value = a [ beg ] ; tree [ index ] . max_set_bits = setBits ( a [ beg ] ) ; } else { int mid = ( beg + end ) / 2 ; buildSegmentTree ( a , 2 * index + 1 , beg , mid ) ; buildSegmentTree ( a , 2 * index + 2 , mid + 1 , end ) ; if ( tree [ 2 * index + 1 ] . max_set_bits > tree [ 2 * index + 2 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 1 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 1 ] . value ; } else if ( tree [ 2 * index + 2 ] . max_set_bits > tree [ 2 * index + 1 ] . max_set_bits ) { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = tree [ 2 * index + 2 ] . value ; } else { tree [ index ] . max_set_bits = tree [ 2 * index + 2 ] . max_set_bits ; tree [ index ] . value = Math . max ( tree [ 2 * index + 2 ] . value , tree [ 2 * index + 1 ] . value ) ; } } } static Node query ( int index , int beg , int end , int l , int r ) { Node result = new Node ( ) ; result . value = result . max_set_bits = - 1 ; if ( beg > r end < l ) return result ; if ( beg >= l && end <= r ) return tree [ index ] ; int mid = ( beg + end ) / 2 ; if ( l > mid ) return query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( r <= mid ) return query ( 2 * index + 1 , beg , mid , l , r ) ; Node left = query ( 2 * index + 1 , beg , mid , l , r ) ; Node right = query ( 2 * index + 2 , mid + 1 , end , l , r ) ; if ( left . max_set_bits > right . max_set_bits ) { result . max_set_bits = left . max_set_bits ; result . value = left . value ; } else if ( right . max_set_bits > left . max_set_bits ) { result . max_set_bits = right . max_set_bits ; result . value = right . value ; } else { result . max_set_bits = left . max_set_bits ; result . value = Math . max ( right . value , left . value ) ; } return result ; } public static void main ( String [ ] args ) { int a [ ] = { 18 , 9 , 8 , 15 , 14 , 5 } ; int N = a . length ; for ( int i = 0 ; i < tree . length ; i ++ ) tree [ i ] = new Node ( ) ; buildSegmentTree ( a , 0 , 0 , N - 1 ) ; System . out . print ( query ( 0 , 0 , N - 1 , 1 , 4 ) . value + "NEW_LINE"); System . out . print ( query ( 0 , 0 , N - 1 , 0 , 2 ) . value + "NEW_LINE"); } }
import java . util . Arrays ; import java . util . HashSet ; import java . util . Set ; class GFG { static Set < Integer > fibonacci = new HashSet < > ( ) ; static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static void createHash ( int maxElement ) { int prev = 0 , curr = 1 ; fibonacci . add ( prev ) ; fibonacci . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; fibonacci . add ( temp ) ; prev = curr ; curr = temp ; } } static int MaxUtil ( int [ ] st , int ss , int se , int l , int r , int node ) { if ( l <= ss && r >= se ) return st [ node ] ; if ( se < l ss > r ) return - 1 ; int mid = getMid ( ss , se ) ; return Math . max ( MaxUtil ( st , ss , mid , l , r , 2 * node + 1 ) , MaxUtil ( st , mid + 1 , se , l , r , 2 * node + 2 ) ) ; } static void updateValue ( int arr [ ] , int [ ] st , int ss , int se , int index , int value , int node ) { if ( index < ss index > se ) { System . out . println ( " Invalid ▁ Input " ) ; return ; } if ( ss == se ) { arr [ index ] = value ; if ( fibonacci . contains ( value ) ) st [ node ] = value ; else st [ node ] = - 1 ; } else { int mid = getMid ( ss , se ) ; if ( index >= ss && index <= mid ) updateValue ( arr , st , ss , mid , index , value , 2 * node + 1 ) ; else updateValue ( arr , st , mid + 1 , se , index , value , 2 * node + 2 ) ; st [ node ] = Math . max ( st [ 2 * node + 1 ] , st [ 2 * node + 2 ] ) ; } return ; } static int getMax ( int [ ] st , int n , int l , int r ) { if ( l < 0 r > n - 1 l > r ) { System . out . printf ( "Invalid InputNEW_LINE"); return - 1 ; } return MaxUtil ( st , 0 , n - 1 , l , r , 0 ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int [ ] st , int si ) { if ( ss == se ) { if ( fibonacci . contains ( arr [ ss ] ) ) st [ si ] = arr [ ss ] ; else st [ si ] = - 1 ; return st [ si ] ; } int mid = getMid ( ss , se ) ; st [ si ] = Math . max ( constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) , constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int [ ] st = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 , 11 } ; int n = arr . length ; int maxEle = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; createHash ( maxEle ) ; int [ ] st = constructST ( arr , n ) ; System . out . println ( " Maximum ▁ fibonacci ▁ number ▁ in ▁ given ▁ range ▁ = ▁ " + getMax ( st , n , 1 , 3 ) ) ; updateValue ( arr , st , 0 , n - 1 , 3 , 8 , 0 ) ; System . out . println ( " Updated ▁ Maximum ▁ fibonacci ▁ number ▁ in ▁ given ▁ range ▁ = ▁ " + getMax ( st , n , 1 , 3 ) ) ; } }
class GFG { static int [ ] solve ( int n ) { int low = 1 , high = ( int ) 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = ( int ) 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; int ans [ ] = { r , c } ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] p = solve ( n ) ; System . out . println ( p [ 0 ] + " ▁ " + p [ 1 ] ) ; } }
import java . util . * ; class GFG { static long query ( int start , int end , int left , int right , int node , long seg [ ] ) { if ( end < left start > right ) { return 0 ; } else if ( start >= left && end <= right ) { return seg [ node ] ; } else { int mid = ( start + end ) / 2 ; long leftChild = query ( start , mid , left , right , 2 * node , seg ) ; long rightChild = query ( mid + 1 , end , left , right , 2 * node + 1 , seg ) ; return ( leftChild rightChild ) ; } } static void update ( int left , int right , int index , int Value , int node , int ar [ ] , long seg [ ] ) { if ( left == right ) { ar [ index ] = Value ; seg [ node ] = ( 1L << Value ) ; return ; } int mid = ( left + right ) / 2 ; if ( index > mid ) { update ( mid + 1 , right , index , Value , 2 * node + 1 , ar , seg ) ; } else { update ( left , mid , index , Value , 2 * node , ar , seg ) ; } seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void build ( int left , int right , int node , int ar [ ] , long seg [ ] ) { if ( left == right ) { seg [ node ] = ( 1L << ar [ left ] ) ; return ; } int mid = ( left + right ) / 2 ; build ( left , mid , 2 * node , ar , seg ) ; build ( mid + 1 , right , 2 * node + 1 , ar , seg ) ; seg [ node ] = ( seg [ 2 * node ] seg [ 2 * node + 1 ] ) ; } static void getDistinctCount ( int [ ] [ ] queries , int ar [ ] , long seg [ ] , int n ) { for ( int i = 0 ; i < queries . length ; i ++ ) { int op = queries [ i ] [ 0 ] ; if ( op == 2 ) { int l = queries [ i ] [ 1 ] , r = queries [ i ] [ 2 ] ; long tempMask = query ( 0 , n - 1 , l - 1 , r - 1 , 1 , seg ) ; int countOfBits = 0 ; for ( int s = 63 ; s >= 0 ; s -- ) { if ( ( tempMask & ( 1L << s ) ) > 0 ) { countOfBits ++ ; } } System . out . println ( countOfBits ) ; } else { int index = queries [ i ] [ 1 ] ; int val = queries [ i ] [ 2 ] ; update ( 0 , n - 1 , index - 1 , val , 1 , ar , seg ) ; } } } public static void main ( String [ ] args ) { int n = 7 ; int ar [ ] = { 1 , 2 , 1 , 3 , 1 , 2 , 1 } ; long seg [ ] = new long [ 4 * n ] ; build ( 0 , n - 1 , 1 , ar , seg ) ; int [ ] [ ] queries = { { 2 , 1 , 4 } , { 1 , 4 , 2 } , { 1 , 5 , 2 } , { 2 , 4 , 6 } , { 2 , 1 , 7 } } ; getDistinctCount ( queries , ar , seg , n ) ; } }
import java . io . * ; class GFG { static void findGrid ( int n ) { int [ ] [ ] arr = new int [ n ] [ n ] ; int x = 0 ; for ( int i = 0 ; i < n / 4 ; i ++ ) { for ( int j = 0 ; j < n / 4 ; j ++ ) { for ( int k = 0 ; k < 4 ; k ++ ) { for ( int l = 0 ; l < 4 ; l ++ ) { arr [ i * 4 + k ] [ j * 4 + l ] = x ; x ++ ; } } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . println ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; findGrid ( n ) ; } }
import java . util . * ; class GFG { static int maxLen = 30 ; static class node { int data ; node left ; node right ; node ( int data ) { left = null ; right = null ; this . data = data ; } } ; static int segtree [ ] = new int [ maxLen * 4 ] ; static int buildTree ( int l , int r , int i , int [ ] arr ) { if ( l == r ) { segtree [ i ] = l ; return l ; } int l1 = buildTree ( l , ( l + r ) / 2 , 2 * i + 1 , arr ) ; int r1 = buildTree ( ( l + r ) / 2 + 1 , r , 2 * i + 2 , arr ) ; if ( arr [ l1 ] > arr [ r1 ] ) segtree [ i ] = l1 ; else segtree [ i ] = r1 ; return segtree [ i ] ; } static int rangeMax ( int l , int r , int rl , int rr , int i , int [ ] arr ) { if ( r < rl l > rr ) return - 1 ; if ( l >= rl && r <= rr ) return segtree [ i ] ; int l1 = rangeMax ( l , ( l + r ) / 2 , rl , rr , 2 * i + 1 , arr ) ; int r1 = rangeMax ( ( l + r ) / 2 + 1 , r , rl , rr , 2 * i + 2 , arr ) ; if ( l1 == - 1 ) return r1 ; if ( r1 == - 1 ) return l1 ; if ( arr [ l1 ] > arr [ r1 ] ) return l1 ; else return r1 ; } static void inorder ( node curr ) { if ( curr == null ) return ; inorder ( curr . left ) ; System . out . print ( curr . data + " ▁ " ) ; inorder ( curr . right ) ; } static node createCartesianTree ( int l , int r , int [ ] arr , int n ) { if ( r < l ) return null ; int m = rangeMax ( 0 , n - 1 , l , r , 0 , arr ) ; node curr = new node ( arr [ m ] ) ; curr . left = createCartesianTree ( l , m - 1 , arr , n ) ; curr . right = createCartesianTree ( m + 1 , r , arr , n ) ; return curr ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 11 , 21 , 100 , 5 , 70 , 55 } ; int n = arr . length ; buildTree ( 0 , n - 1 , 0 , arr ) ; inorder ( createCartesianTree ( 0 , n - 1 , arr , n ) ) ; } }
import java . util . * ; class GFG { static int kthSmallest ( int [ ] arr , int k , int n ) { int low = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int high = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int countless = 0 , countequal = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] < mid ) ++ countless ; else if ( arr [ i ] == mid ) ++ countequal ; } if ( countless < k && ( countless + countequal ) >= k ) { return mid ; } else if ( countless >= k ) { high = mid - 1 ; } else if ( countless < k && countless + countequal < k ) { low = mid + 1 ; } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 10 , 4 , 3 , 20 , 15 } ; int n = arr . length ; int k = 3 ; System . out . println ( kthSmallest ( arr , k , n ) ) ; } }
import java . util . * ; class GFG { public static void main ( String [ ] args ) { Scanner sc1 = new Scanner ( System . in ) ; int lower_bound = 2 ; int upper_bound = 10 ; for ( int i = lower_bound ; i <= upper_bound ; i ++ ) { System . out . println ( i ) ; int response = sc1 . nextInt ( ) ; if ( response == 0 ) { System . out . println ( " Number ▁ guessed ▁ is ▁ : " + i ) ; break ; } } } }
class GFG { static int MAX = 1000 ; static int tree [ ] = new int [ MAX ] ; static int lazy [ ] = new int [ MAX ] ; static void updateRangeUtil ( int si , int ss , int se , int us , int ue , int diff ) { if ( lazy [ si ] != 0 ) { tree [ si ] += lazy [ si ] ; if ( ss != se ) { lazy [ si * 2 + 1 ] += lazy [ si ] ; lazy [ si * 2 + 2 ] += lazy [ si ] ; } lazy [ si ] = 0 ; } if ( ss > se ss > ue se < us ) return ; if ( ss >= us && se <= ue ) { tree [ si ] += diff ; if ( ss != se ) { lazy [ si * 2 + 1 ] += diff ; lazy [ si * 2 + 2 ] += diff ; } return ; } int mid = ( ss + se ) / 2 ; updateRangeUtil ( si * 2 + 1 , ss , mid , us , ue , diff ) ; updateRangeUtil ( si * 2 + 2 , mid + 1 , se , us , ue , diff ) ; tree [ si ] = Math . max ( tree [ si * 2 + 1 ] , tree [ si * 2 + 2 ] ) ; } static void updateRange ( int n , int us , int ue , int diff ) { updateRangeUtil ( 0 , 0 , n - 1 , us , ue , diff ) ; } static int getSumUtil ( int ss , int se , int qs , int qe , int si ) { if ( lazy [ si ] != 0 ) { tree [ si ] += lazy [ si ] ; if ( ss != se ) { lazy [ si * 2 + 1 ] += lazy [ si ] ; lazy [ si * 2 + 2 ] += lazy [ si ] ; } lazy [ si ] = 0 ; } if ( ss > se ss > qe se < qs ) return 0 ; if ( ss >= qs && se <= qe ) return tree [ si ] ; int mid = ( ss + se ) / 2 ; return Math . max ( getSumUtil ( ss , mid , qs , qe , 2 * si + 1 ) , getSumUtil ( mid + 1 , se , qs , qe , 2 * si + 2 ) ) ; } static int getSum ( int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . print ( " Invalid ▁ Input " ) ; return - 1 ; } return getSumUtil ( 0 , n - 1 , qs , qe , 0 ) ; } static void constructSTUtil ( int arr [ ] , int ss , int se , int si ) { if ( ss > se ) return ; if ( ss == se ) { tree [ si ] = arr [ ss ] ; return ; } int mid = ( ss + se ) / 2 ; constructSTUtil ( arr , ss , mid , si * 2 + 1 ) ; constructSTUtil ( arr , mid + 1 , se , si * 2 + 2 ) ; tree [ si ] = Math . max ( tree [ si * 2 + 1 ] , tree [ si * 2 + 2 ] ) ; } static void constructST ( int arr [ ] , int n ) { constructSTUtil ( arr , 0 , n - 1 , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; constructST ( arr , n ) ; updateRange ( n , 0 , 3 , 4 ) ; System . out . println ( getSum ( n , 1 , 4 ) ) ; } }
class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int findModuloByM ( int X , int N , int M ) { if ( N < 6 ) { String temp = " " ; for ( int i = 0 ; i < N ; i ++ ) temp = temp + ( char ) ( X + 48 ) ; int res = Integer . parseInt ( temp ) % M ; return res ; } if ( N % 2 == 0 ) { int half = findModuloByM ( X , N / 2 , M ) % M ; int res = ( half * power ( 10 , N / 2 , M ) + half ) % M ; return res ; } else { int half = findModuloByM ( X , N / 2 , M ) % M ; int res = ( half * power ( 10 , N / 2 + 1 , M ) + half * 10 + X ) % M ; return res ; } } public static void main ( String [ ] args ) { int X = 6 , N = 14 , M = 9 ; System . out . println ( findModuloByM ( X , N , M ) ) ; } }
class GFG { static final int MOD = 1000000007 ; static int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = Math . max ( l , a ) ; int R = Math . min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += ( i * i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } public static void main ( String args [ ] ) { int l = 98 , r = 102 ; System . out . print ( rangeSum ( l , r ) ) ; } }
import java . util . Vector ; class GFG { static Vector < Integer > permutation = new Vector < > ( ) ; static void printPermutation ( ) { permutation . stream ( ) . forEach ( ( i ) -> { System . out . print ( i + " ▁ " ) ; } ) ; } static int give_random_number ( int l , int r ) { int x = ( int ) ( Math . random ( ) % ( r - l + 1 ) + l ) ; return x ; } static void generate_random_permutation ( int l , int r ) { if ( l > r ) return ; int n = give_random_number ( l , r ) ; permutation . add ( n ) ; generate_random_permutation ( l , n - 1 ) ; generate_random_permutation ( n + 1 , r ) ; } public static void main ( String [ ] args ) { int l = 5 ; int r = 15 ; generate_random_permutation ( l , r ) ; printPermutation ( ) ; } }
import java . util . * ; class solution { static int INF = 99999 ; static int size = 10 ; static int getSetBitsFromOneToN ( int N ) { int two = 2 , ans = 0 ; int n = N ; while ( n != 0 ) { ans += ( N / two ) * ( two >> 1 ) ; if ( ( N & ( two - 1 ) ) > ( two >> 1 ) - 1 ) ans += ( N & ( two - 1 ) ) - ( two >> 1 ) + 1 ; two <<= 1 ; n >>= 1 ; } return ans ; } static int findMinimum ( int x ) { int low = 0 , high = 100000 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; if ( getSetBitsFromOneToN ( mid ) >= x ) { ans = Math . min ( ans , mid ) ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void main ( String args [ ] ) { int x = 20 ; System . out . println ( findMinimum ( x ) ) ; } }
import java . io . * ; class GFG { static int trailingZeroes ( int n ) { int cnt = 0 ; while ( n > 0 ) { n /= 5 ; cnt += n ; } return cnt ; } static void binarySearch ( int n ) { int low = 0 ; int high = 1000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } int result [ ] = new int [ 1000 ] ; int k = 0 ; while ( trailingZeroes ( low ) == n ) { result [ k ] = low ; k ++ ; low ++ ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 3 ; binarySearch ( n ) ; } }
public class Tank_Empty { static int getCumulateSum ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static int minDaysToEmpty ( int C , int l ) { if ( C <= l ) return C ; int lo = 0 ; int hi = ( int ) 1e4 ; int mid ; while ( lo < hi ) { mid = ( lo + hi ) / 2 ; if ( getCumulateSum ( mid ) >= ( C - l ) ) hi = mid ; else lo = mid + 1 ; } return ( l + lo ) ; } public static void main ( String args [ ] ) { int C = 5 ; int l = 2 ; System . out . println ( minDaysToEmpty ( C , l ) ) ; } }
import java . lang . * ; class GFG { static int minDaysToEmpty ( int C , int l ) { if ( l >= C ) return C ; double eq_root = ( Math . sqrt ( 1 + 8 * ( C - l ) ) - 1 ) / 2 ; return ( int ) ( Math . ceil ( eq_root ) + l ) ; } public static void main ( String [ ] args ) { System . out . println ( minDaysToEmpty ( 5 , 2 ) ) ; System . out . println ( minDaysToEmpty ( 6514683 , 4965 ) ) ; } }
class Main { static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }
class GFG { static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k , int st1 , int st2 ) { if ( st1 == m ) { return arr2 [ st2 + k - 1 ] ; } if ( st2 == n ) { return arr1 [ st1 + k - 1 ] ; } if ( k == 0 || k > ( m - st1 ) + ( n - st2 ) ) { return - 1 ; } if ( k == 1 ) { return ( arr1 [ st1 ] < arr2 [ st2 ] ) ? arr1 [ st1 ] : arr2 [ st2 ] ; } int curr = k / 2 ; if ( curr - 1 >= m - st1 ) { if ( arr1 [ m - 1 ] < arr2 [ st2 + curr - 1 ] ) { return arr2 [ st2 + ( k - ( m - st1 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } if ( curr - 1 >= n - st2 ) { if ( arr2 [ n - 1 ] < arr1 [ st1 + curr - 1 ] ) { return arr1 [ st1 + ( k - ( n - st2 ) - 1 ) ] ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } } else if ( arr1 [ curr + st1 - 1 ] < arr2 [ curr + st2 - 1 ] ) { return kth ( arr1 , arr2 , m , n , k - curr , st1 + curr , st2 ) ; } else { return kth ( arr1 , arr2 , m , n , k - curr , st1 , st2 + curr ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; int st1 = 0 , st2 = 0 ; System . out . println ( kth ( arr1 , arr2 , 5 , 4 , k , st1 , st2 ) ) ; } }
import java . util . Arrays ; class Gfg { static int kth ( int arr1 [ ] , int m , int arr2 [ ] , int n , int k ) { if ( k > ( m + n ) k < 1 ) return - 1 ; if ( m > n ) return kth ( arr2 , n , arr1 , m , k ) ; if ( m == 0 ) return arr2 [ k - 1 ] ; if ( k == 1 ) return Math . min ( arr1 [ 0 ] , arr2 [ 0 ] ) ; int i = Math . min ( m , k / 2 ) ; int j = Math . min ( n , k / 2 ) ; if ( arr1 [ i - 1 ] > arr2 [ j - 1 ] ) { int temp [ ] = Arrays . copyOfRange ( arr2 , j , n ) ; return kth ( arr1 , m , temp , n - j , k - j ) ; } int temp [ ] = Arrays . copyOfRange ( arr1 , i , m ) ; return kth ( temp , m - i , arr2 , n , k - i ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int m = arr1 . length ; int n = arr2 . length ; int k = 5 ; int ans = kth ( arr1 , m , arr2 , n , k ) ; if ( ans == - 1 ) System . out . println ( " Invalid ▁ query " ) ; else System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int kth ( int a [ ] , int b [ ] , int n , int m , int k ) { PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { pq . offer ( a [ i ] ) ; } for ( int i = 0 ; i < m ; i ++ ) { pq . offer ( b [ i ] ) ; } while ( k -- > 1 ) { pq . remove ( ) ; } return pq . peek ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }
import java . io . * ; class GFG { static int MAX = 100 ; static void binarySearch ( int mat [ ] [ ] , int i , int j_low , int j_high , int x ) { while ( j_low <= j_high ) { int j_mid = ( j_low + j_high ) / 2 ; if ( mat [ i ] [ j_mid ] == x ) { System . out . println ( " Found ▁ at ▁ ( " + i + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i ] [ j_mid ] > x ) j_high = j_mid - 1 ; else j_low = j_mid + 1 ; } System . out . println ( " Element ▁ no ▁ found " ) ; } static void sortedMatrixSearch ( int mat [ ] [ ] , int n , int m , int x ) { if ( n == 1 ) { binarySearch ( mat , 0 , 0 , m - 1 , x ) ; return ; } int i_low = 0 ; int i_high = n - 1 ; int j_mid = m / 2 ; while ( ( i_low + 1 ) < i_high ) { int i_mid = ( i_low + i_high ) / 2 ; if ( mat [ i_mid ] [ j_mid ] == x ) { System . out . println ( " Found ▁ at ▁ ( " + i_mid + " , ▁ " + j_mid + " ) " ) ; return ; } else if ( mat [ i_mid ] [ j_mid ] > x ) i_high = i_mid ; else i_low = i_mid ; } if ( mat [ i_low ] [ j_mid ] == x ) System . out . println ( " Found ▁ at ▁ ( " + i_low + " , " + j_mid + " ) " ) ; else if ( mat [ i_low + 1 ] [ j_mid ] == x ) System . out . println ( " Found ▁ at ▁ ( " + ( i_low + 1 ) + " , ▁ " + j_mid + " ) " ) ; else if ( x <= mat [ i_low ] [ j_mid - 1 ] ) binarySearch ( mat , i_low , 0 , j_mid - 1 , x ) ; else if ( x >= mat [ i_low ] [ j_mid + 1 ] && x <= mat [ i_low ] [ m - 1 ] ) binarySearch ( mat , i_low , j_mid + 1 , m - 1 , x ) ; else if ( x <= mat [ i_low + 1 ] [ j_mid - 1 ] ) binarySearch ( mat , i_low + 1 , 0 , j_mid - 1 , x ) ; else binarySearch ( mat , i_low + 1 , j_mid + 1 , m - 1 , x ) ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 , x = 8 ; int mat [ ] [ ] = { { 0 , 6 , 8 , 9 , 11 } , { 20 , 22 , 28 , 29 , 31 } , { 36 , 38 , 50 , 61 , 63 } , { 64 , 66 , 100 , 122 , 128 } } ; sortedMatrixSearch ( mat , n , m , x ) ; } }
import java . util . Arrays ; class GFG { static final int R = 2 ; static final int C = 2 ; static int bsearch ( int low , int high , int n , int arr [ ] ) { int mid = ( low + high ) / 2 ; if ( low <= high ) { if ( arr [ mid ] < n ) return bsearch ( mid + 1 , high , n , arr ) ; return bsearch ( low , mid - 1 , n , arr ) ; } return low ; } static int mindiff ( int arr [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) Arrays . sort ( arr [ i ] ) ; int ans = + 2147483647 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int p = bsearch ( 0 , m - 1 , arr [ i ] [ j ] , arr [ i + 1 ] ) ; ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p ] - arr [ i ] [ j ] ) ) ; if ( p - 1 >= 0 ) ans = Math . min ( ans , Math . abs ( arr [ i + 1 ] [ p - 1 ] - arr [ i ] [ j ] ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int m [ ] [ ] = { { 8 , 5 } , { 6 , 8 } } ; System . out . println ( mindiff ( m , R , C ) ) ; } }
class Test { static int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElement ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 } ; int index = findRepeatingElement ( arr , 0 , arr . length - 1 ) ; if ( index != - 1 ) System . out . println ( arr [ index ] ) ; } }
import java . io . * ; class GFG { static double diff ( double n , double mid ) { if ( n > ( mid * mid * mid ) ) return ( n - ( mid * mid * mid ) ) ; else return ( ( mid * mid * mid ) - n ) ; } static double cubicRoot ( double n ) { double start = 0 , end = n ; double e = 0.0000001 ; while ( true ) { double mid = ( start + end ) / 2 ; double error = diff ( n , mid ) ; if ( error <= e ) return mid ; if ( ( mid * mid * mid ) > n ) end = mid ; else start = mid ; } } public static void main ( String [ ] args ) { double n = 3 ; System . out . println ( " Cube ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + cubicRoot ( n ) ) ; } }
import java . util . * ; class GFG { static void findFrequencyUtil ( int arr [ ] , int low , int high , int [ ] freq ) { if ( arr [ low ] == arr [ high ] ) { freq [ arr [ low ] ] += high - low + 1 ; } else { int mid = ( low + high ) / 2 ; findFrequencyUtil ( arr , low , mid , freq ) ; findFrequencyUtil ( arr , mid + 1 , high , freq ) ; } } static void findFrequency ( int arr [ ] , int n ) { int [ ] freq = new int [ arr [ n - 1 ] + 1 ] ; findFrequencyUtil ( arr , 0 , n - 1 , freq ) ; for ( int i = 0 ; i <= arr [ n - 1 ] ; i ++ ) if ( freq [ i ] != 0 ) System . out . println ( " Element ▁ " + i + " ▁ occurs ▁ " + freq [ i ] + " ▁ times " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 , 3 , 3 , 5 , 5 , 8 , 8 , 8 , 9 , 9 , 10 } ; int n = arr . length ; findFrequency ( arr , n ) ; } }
class GFG { static int floorSqrt ( int x ) { if ( x == 0 x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } public static void main ( String [ ] args ) { int x = 11 ; System . out . print ( floorSqrt ( x ) ) ; } }
import java . util . * ; class GFG { static boolean canReach ( int X , int Y , int steps ) { if ( X == 0 && Y == 0 ) { return true ; } if ( X < 0 Y < 0 ) { return false ; } return ( canReach ( X - ( int ) Math . pow ( 3 , steps ) , Y , steps + 1 ) | canReach ( X , Y - ( int ) Math . pow ( 3 , steps ) , steps + 1 ) ) ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y , 0 ) ) { System . out . print ( " YES " + "NEW_LINE"); } else System . out . print ( " NO " + "NEW_LINE"); } }
import java . io . * ; class GFG { static boolean canReach ( int X , int Y ) { int steps = 0 ; while ( X != 0 Y != 0 ) { int pos1 = X % 3 ; int pos2 = Y % 3 ; if ( pos1 == 2 pos2 == 2 ) { return false ; } if ( pos1 == 1 && pos2 == 1 ) { return false ; } if ( pos1 == 0 && pos2 == 0 ) { return false ; } X /= 3 ; Y /= 3 ; steps ++ ; } return true ; } public static void main ( String [ ] args ) { int X = 10 , Y = 30 ; if ( canReach ( X , Y ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . io . * ; class GFG { static double lengthOfLatusRectum ( double A , double B ) { double major = 2.0 * A ; double minor = 2.0 * B ; double latus_rectum = ( minor * minor ) / major ; return latus_rectum ; } public static void main ( String [ ] args ) { double A = 3.0 , B = 2.0 ; System . out . println ( lengthOfLatusRectum ( A , B ) ) ; } }
class GFG { static double radius ( int a , int b , int c ) { int g = a / 2 ; int f = b / 2 ; if ( g * g + f * f - c < 0 ) return ( - 1 ) ; return ( Math . sqrt ( g * g + f * f - c ) ) ; } static double centerDistanceFromLine ( int a , int b , int i , int j , int k ) { int g = a / 2 ; int f = b / 2 ; double distance = Math . abs ( i * g + j * f + k ) / ( Math . sqrt ( i * i + j * j ) ) ; if ( distance < 0 ) return ( - 1 ) ; return distance ; } static void interceptLength ( int a , int b , int c , int i , int j , int k ) { double rad = radius ( a , b , c ) ; double dist = centerDistanceFromLine ( a , b , i , j , k ) ; if ( rad < 0 dist < 0 ) { System . out . println ( " circle ▁ not ▁ possible " ) ; return ; } if ( dist > rad ) { System . out . println ( " Line ▁ not ▁ cutting ▁ circle " ) ; } else System . out . println ( 2 * Math . sqrt ( rad * rad - dist * dist ) ) ; } public static void main ( String [ ] args ) { int a = 0 , b = 0 , c = - 4 ; int i = 2 , j = - 1 , k = 1 ; interceptLength ( a , b , c , i , j , k ) ; } }
public class GFG { static void check_position ( int a , int b , int c , int d , int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int value_1 = a * x1 + b * y1 + c * z1 + d ; int value_2 = a * x2 + b * y2 + c * z2 + d ; if ( ( value_1 > 0 && value_2 > 0 ) || ( value_1 < 0 && value_2 < 0 ) ) System . out . print ( " On ▁ same ▁ side " ) ; if ( ( value_1 > 0 && value_2 < 0 ) || ( value_1 < 0 && value_2 > 0 ) ) System . out . print ( " On ▁ different ▁ sides " ) ; if ( value_1 == 0 && value_2 == 0 ) System . out . print ( " Both ▁ on ▁ the ▁ plane " ) ; if ( value_1 == 0 && value_2 != 0 ) System . out . print ( " Point ▁ 1 ▁ on ▁ the ▁ plane " ) ; if ( value_1 != 0 && value_2 == 0 ) System . out . print ( " Point ▁ 2 ▁ on ▁ the ▁ plane " ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 2 , c = 3 , d = 4 ; int x1 = - 2 , y1 = - 2 , z1 = 1 ; int x2 = - 4 , y2 = 11 , z2 = - 1 ; check_position ( a , b , c , d , x1 , y1 , z1 , x2 , y2 , z2 ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean checkIntersection ( int arr [ ] , int N ) { Vector < pair > vec = new Vector < > ( ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { int x = arr [ i ] , y = arr [ i + 1 ] ; int minn = Math . min ( x , y ) ; int maxx = Math . max ( x , y ) ; vec . add ( new pair ( minn , maxx ) ) ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { pair x = vec . elementAt ( i ) ; for ( int j = 0 ; j < vec . size ( ) ; j ++ ) { pair y = vec . elementAt ( j ) ; boolean cond1 = ( x . first < y . first && x . second < y . second && y . first < x . second ) ; boolean cond2 = ( y . first < x . first && y . second < x . second && x . first < y . second ) ; if ( cond1 cond2 ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 15 , 5 , 10 } ; int N = arr . length ; if ( checkIntersection ( arr , N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; class GFG { static void AreaFactor ( int n ) { double pi = 3.14159265 ; double areaf = 1 / ( Math . cos ( pi / n ) * Math . cos ( pi / n ) ) ; System . out . format ( " % .6f " , areaf ) ; } public static void main ( String [ ] args ) { int n = 4 ; AreaFactor ( n ) ; } }
class GFG { static class circle { double x ; double y ; double r ; public circle ( int x , int y , int r ) { this . x = x ; this . y = y ; this . r = r ; } } static boolean check ( circle C [ ] ) { double C1C2 = Math . sqrt ( ( C [ 1 ] . x - C [ 0 ] . x ) * ( C [ 1 ] . x - C [ 0 ] . x ) + ( C [ 1 ] . y - C [ 0 ] . y ) * ( C [ 1 ] . y - C [ 0 ] . y ) ) ; boolean flag = false ; if ( C1C2 < ( C [ 0 ] . r + C [ 1 ] . r ) ) { if ( ( C [ 0 ] . x + C [ 1 ] . x ) == 2 * C [ 2 ] . x && ( C [ 0 ] . y + C [ 1 ] . y ) == 2 * C [ 2 ] . y ) { flag = true ; } } return flag ; } static boolean IsFairTriplet ( circle c [ ] ) { boolean f = false ; f |= check ( c ) ; for ( int i = 0 ; i < 2 ; i ++ ) { swap ( c [ 0 ] , c [ 2 ] ) ; f |= check ( c ) ; } return f ; } static void swap ( circle circle1 , circle circle2 ) { circle temp = circle1 ; circle1 = circle2 ; circle2 = temp ; } public static void main ( String [ ] args ) { circle C [ ] = new circle [ 3 ] ; C [ 0 ] = new circle ( 0 , 0 , 8 ) ; C [ 1 ] = new circle ( 0 , 10 , 6 ) ; C [ 2 ] = new circle ( 0 , 5 , 5 ) ; if ( IsFairTriplet ( C ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static final double PI = 3.14159265 ; static double findInteriorAngle ( int n ) { return ( ( n - 2 ) * PI ) / n ; } static double calculateSideLength ( double L , int N , int K ) { double angle = findInteriorAngle ( N ) ; double length = L * Math . pow ( Math . sin ( angle / 2 ) , ( K - 1 ) ) ; return length ; } public static void main ( String [ ] args ) { double L = 21 ; int N = 5 , K = 7 ; System . out . print ( calculateSideLength ( L , N , K ) ) ; } }
import java . util . * ; class GFG { static double PI = 3.14159265 ; static void findAngle ( double M1 , double M2 ) { double angle = Math . abs ( ( M2 - M1 ) / ( 1 + M1 * M2 ) ) ; double ret = Math . atan ( angle ) ; double val = ( ret * 180 ) / PI ; System . out . println ( val ) ; } public static void main ( String [ ] args ) { double M1 = 1.75 , M2 = 0.27 ; findAngle ( M1 , M2 ) ; } }
import java . io . * ; class GFG { static float angle ( float R1 , float R2 , float D ) { float ans = ( R1 * R1 + R2 * R2 - D * D ) / ( 2 * R1 * R2 ) ; return ans ; } public static void main ( String [ ] args ) { float R1 = 3 , R2 = 4 ; float D = 5 ; System . out . println ( angle ( R1 , R2 , D ) ) ; } }
class GFG { static void triangleArea ( int a , int b ) { double ratio = ( double ) b / a ; System . out . println ( ratio ) ; } public static void main ( String args [ ] ) { int a = 1 , b = 2 ; triangleArea ( a , b ) ; } }
import java . io . * ; class GFG { public static String find ( int [ ] a , int [ ] b , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) { continue ; } long dis = Math . abs ( a [ i ] - a [ j ] ) + Math . abs ( b [ i ] - b [ j ] ) ; if ( dis <= K ) { count ++ ; } if ( count == N - 1 ) { return " Yes " ; } } } return " No " ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] A = { 1 , 0 , 2 , 1 , 1 } ; int [ ] B = { 1 , 1 , 1 , 0 , 2 } ; int K = 1 ; System . out . println ( find ( A , B , N , K ) ) ; } }
class GFG { static void minimumTriangleArea ( int a , int b ) { int area = a * b ; System . out . println ( area ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 2 ; minimumTriangleArea ( a , b ) ; } }
class GFG { static void findAngles ( double a , double b , double c , double d ) { double numerator = a * a + d * d - b * b - c * c ; double denominator = 2 * ( a * b + c * d ) ; double x = numerator / denominator ; System . out . println ( " A : ▁ " + Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = a * a + b * b - c * c - d * d ; x = numerator / denominator ; System . out . println ( " B : ▁ " + Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = c * c + b * b - a * a - d * d ; x = numerator / denominator ; System . out . println ( " C : ▁ " + Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; numerator = d * d + c * c - a * a - b * b ; x = numerator / denominator ; System . out . println ( " D : ▁ " + Math . round ( ( ( Math . acos ( x ) * 180 ) / 3.141592 ) * 100.0 ) / 100.0 + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { double A = 10 , B = 15 , C = 20 , D = 25 ; findAngles ( A , B , C , D ) ; } }
import java . util . * ; class GFG { static int countInscribed ( int R1 , int R2 ) { if ( R2 > R1 ) return 0 ; double angle ; double ratio ; int number_of_circles = 0 ; ratio = R2 / ( double ) ( R1 - R2 ) ; if ( R1 < 2 * R2 ) { number_of_circles = 1 ; } else { angle = Math . abs ( Math . asin ( ratio ) * 180 ) / 3.14159265 ; number_of_circles = ( int ) ( 360 / ( 2 * Math . floor ( angle ) ) ) ; } return number_of_circles ; } public static void main ( String args [ ] ) { int R1 = 3 ; int R2 = 1 ; System . out . println ( countInscribed ( R1 , R2 ) ) ; } }
import java . util . * ; class GFG { static class pair { float first , second ; pair ( float first , float second ) { this . first = first ; this . second = second ; } } static float distance ( int m , int n , int p , int q ) { return ( float ) Math . sqrt ( Math . pow ( n - m , 2 ) + Math . pow ( q - p , 2 ) * 1.0 ) ; } static void Excenters ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { float a = distance ( x2 , x3 , y2 , y3 ) ; float b = distance ( x3 , x1 , y3 , y1 ) ; float c = distance ( x1 , x2 , y1 , y2 ) ; pair [ ] excenter = new pair [ 4 ] ; excenter [ 1 ] = new pair ( ( - ( a * x1 ) + ( b * x2 ) + ( c * x3 ) ) / ( - a + b + c ) , ( - ( a * y1 ) + ( b * y2 ) + ( c * y3 ) ) / ( - a + b + c ) ) ; excenter [ 2 ] = new pair ( ( ( a * x1 ) - ( b * x2 ) + ( c * x3 ) ) / ( a - b + c ) , ( ( a * y1 ) - ( b * y2 ) + ( c * y3 ) ) / ( a - b + c ) ) ; excenter [ 3 ] = new pair ( ( ( a * x1 ) + ( b * x2 ) - ( c * x3 ) ) / ( a + b - c ) , ( ( a * y1 ) + ( b * y2 ) - ( c * y3 ) ) / ( a + b - c ) ) ; for ( int i = 1 ; i <= 3 ; i ++ ) { System . out . println ( ( int ) excenter [ i ] . first + " ▁ " + ( int ) excenter [ i ] . second ) ; } } public static void main ( String [ ] args ) { int x1 , x2 , x3 , y1 , y2 , y3 ; x1 = 0 ; x2 = 3 ; x3 = 0 ; y1 = 0 ; y2 = 0 ; y3 = 4 ; Excenters ( x1 , y1 , x2 , y2 , x3 , y3 ) ; } }
import java . util . * ; class GFG { static void findPairs ( int [ ] x , int [ ] y , int K ) { int n = x . length ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { if ( K * Math . abs ( x [ i ] - x [ j ] ) >= Math . abs ( y [ i ] - y [ j ] ) ) { ++ ans ; } } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int [ ] X = { 2 , 1 , 0 } ; int [ ] Y = { 1 , 2 , 0 } ; int K = 1 ; findPairs ( X , Y , K ) ; } }
import java . util . * ; class GFG { static double findDiagonal ( double s ) { return ( double ) Math . sqrt ( 2 ) * s ; } public static void main ( String [ ] args ) { double S = 10 ; System . out . print ( findDiagonal ( S ) ) ; } }
import java . util . * ; class GFG { static void triangleArea ( float a , float b ) { if ( a < 0 b < 0 ) { System . out . println ( - 1 ) ; return ; } float area = ( 3 * ( float ) Math . sqrt ( 3 ) * a * b ) / ( 4 ) ; System . out . println ( area ) ; } public static void main ( String [ ] args ) { float a = 1 , b = 2 ; triangleArea ( a , b ) ; } }
import java . util . * ; class GFG { static int MAX = ( int ) ( 1e6 + 5 ) ; static void query ( double [ ] [ ] arr , int [ ] Q ) { int pre [ ] = new int [ MAX ] ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] [ 0 ] < 0 arr [ i ] [ 1 ] < 0 ) continue ; int sum = ( int ) Math . ceil ( ( arr [ i ] [ 0 ] + arr [ i ] [ 1 ] ) ) ; pre [ sum ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) pre [ i ] += pre [ i - 1 ] ; for ( int i = 0 ; i < Q . length ; i ++ ) { System . out . print ( pre [ Q [ i ] ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { double [ ] [ ] arr = { { 2.1 , 3.0 } , { 3.7 , 1.2 } , { 1.5 , 6.5 } , { 1.2 , 0.0 } } ; int [ ] Q = { 2 , 8 , 5 } ; int N = arr . length ; int M = Q . length ; query ( arr , Q ) ; } }
import java . util . * ; class GFG { static void findHeight ( float p1 , float p2 , float b , float c ) { float a = Math . max ( p1 , p2 ) - Math . min ( p1 , p2 ) ; float s = ( a + b + c ) / 2 ; float area = ( int ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; float height = ( area * 2 ) / a ; System . out . print ( " Height ▁ is : ▁ " + height ) ; } public static void main ( String args [ ] ) { float p1 = 25 , p2 = 10 ; float a = 14 , b = 13 ; findHeight ( p1 , p2 , a , b ) ; } }
import java . util . * ; class GFG { static boolean doIntersect ( int [ ] X , int [ ] Y ) { if ( X [ 0 ] > X [ 3 ] X [ 2 ] > X [ 1 ] ) return false ; if ( Y [ 0 ] > Y [ 3 ] Y [ 2 ] > Y [ 1 ] ) return false ; return true ; } static int getUnionPerimeter ( int [ ] X , int [ ] Y ) { int perimeter = 0 ; if ( ! doIntersect ( X , Y ) ) { perimeter += 2 * ( Math . abs ( X [ 1 ] - X [ 0 ] ) + Math . abs ( Y [ 1 ] - Y [ 0 ] ) ) ; perimeter += 2 * ( Math . abs ( X [ 3 ] - X [ 2 ] ) + Math . abs ( Y [ 3 ] - Y [ 2 ] ) ) ; } else { int w = Arrays . stream ( X ) . max ( ) . getAsInt ( ) - Arrays . stream ( X ) . min ( ) . getAsInt ( ) ; int l = Arrays . stream ( Y ) . max ( ) . getAsInt ( ) - Arrays . stream ( Y ) . min ( ) . getAsInt ( ) ; perimeter = 2 * ( l + w ) ; } return perimeter ; } public static void main ( String [ ] args ) { int [ ] X = { - 1 , 2 , 4 , 6 } ; int [ ] Y = { 2 , 5 , 3 , 7 } ; System . out . print ( getUnionPerimeter ( X , Y ) ) ; } }
import java . util . * ; class GFG { static void parallel ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { if ( ( - ( a1 / b1 ) ) == ( - ( a2 / b2 ) ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { float a1 = - 2 , b1 = 4 , c1 = 5 ; float a2 = - 6 , b2 = 12 , c2 = 6 ; parallel ( a1 , b1 , c1 , a2 , b2 , c2 ) ; } }
import java . util . * ; class GFG { static double Convert ( double degree ) { double pi = 3.14159265359 ; return ( degree * ( pi / 180 ) ) ; } static void areaAndHeightOfTraingle ( double radius , double a ) { if ( a >= 180 a == 0 ) { System . out . println ( " Not ▁ possible " ) ; return ; } double base_angle = ( 180 - a ) / 2 ; double radians = Convert ( base_angle ) ; double height = Math . sin ( radians ) * radius ; System . out . println ( " Height ▁ of ▁ triangle ▁ " + height ) ; double Base = Math . cos ( radians ) * radius ; double area = Base * height ; System . out . println ( " Area ▁ of ▁ triangle ▁ " + area ) ; } public static void main ( String [ ] args ) { double R = 5 , angle = 120 ; areaAndHeightOfTraingle ( R , angle ) ; } }
import java . util . * ; class GFG { static class Tri { int MinX , MaxX , MinY , MaxY ; } ; static int StringtoInt ( String s ) { return Integer . valueOf ( s ) ; } static int min ( int a , int b , int c ) { return Math . min ( a , Math . min ( b , c ) ) ; } static int max ( int a , int b , int c ) { return Math . max ( a , Math . max ( b , c ) ) ; } static void TriangleCuts ( int [ ] [ ] Triangle , String Cuts [ ] , int N , int M , int COL ) { Tri [ ] Minimized = new Tri [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { Minimized [ i ] = new Tri ( ) ; Minimized [ i ] . MaxX = 0 ; Minimized [ i ] . MaxY = 0 ; Minimized [ i ] . MinX = 0 ; Minimized [ i ] . MinY = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { int x1 = Triangle [ i ] [ 0 ] ; int y1 = Triangle [ i ] [ 1 ] ; int x2 = Triangle [ i ] [ 2 ] ; int y2 = Triangle [ i ] [ 3 ] ; int x3 = Triangle [ i ] [ 4 ] ; int y3 = Triangle [ i ] [ 5 ] ; Minimized [ i ] . MinX = min ( x1 , x2 , x3 ) ; Minimized [ i ] . MaxX = max ( x1 , x2 , x3 ) ; Minimized [ i ] . MinY = min ( y1 , y2 , y3 ) ; Minimized [ i ] . MaxY = max ( y1 , y2 , y3 ) ; } for ( int i = 0 ; i < M ; i ++ ) { String Cut = Cuts [ i ] ; int CutCount = 0 ; int CutVal = StringtoInt ( Cut . substring ( 2 , Cut . length ( ) ) ) ; if ( Cut . charAt ( 0 ) == ' X ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinX ) < ( CutVal ) && ( Minimized [ j ] . MaxX ) > ( CutVal ) ) { CutCount ++ ; } } } else if ( Cut . charAt ( 0 ) == ' Y ' ) { for ( int j = 0 ; j < N ; j ++ ) { if ( ( Minimized [ j ] . MinY ) < ( CutVal ) && ( Minimized [ j ] . MaxY ) > ( CutVal ) ) { CutCount ++ ; } } } System . out . print ( CutCount + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] Triangle = { { 0 , 2 , 2 , 9 , 8 , 5 } , { 5 , 0 , 6 , 3 , 7 , 0 } } ; int N = Triangle . length ; int COL = 6 ; String Cuts [ ] = { " X = 2" , " Y = 2" , " Y = 9" } ; int M = Cuts . length ; TriangleCuts ( Triangle , Cuts , N , M , COL ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int cntRect ( int points [ ] [ ] , int N , int rectangle [ ] [ ] ) { HashSet < Integer > cntHor = new HashSet < > ( ) ; HashSet < Integer > cntVer = new HashSet < > ( ) ; cntHor . add ( 0 ) ; cntVer . add ( 0 ) ; cntHor . add ( rectangle [ 3 ] [ 0 ] ) ; cntVer . add ( rectangle [ 3 ] [ 1 ] ) ; for ( int i = 0 ; i < N ; i ++ ) { cntHor . add ( points [ i ] [ 0 ] ) ; cntVer . add ( points [ i ] [ 1 ] ) ; } return ( cntHor . size ( ) - 1 ) * ( cntVer . size ( ) - 1 ) ; } public static void main ( String args [ ] ) { int rectangle [ ] [ ] = { { 0 , 0 } , { 0 , 5 } , { 5 , 0 } , { 5 , 5 } } ; int points [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; int N = points . length ; System . out . println ( cntRect ( points , N , rectangle ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int numberOfSquares ( int [ ] X , int [ ] Y , int N , int M ) { HashMap < Integer , Integer > m1 = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > m2 = new HashMap < Integer , Integer > ( ) ; int i , j , ans = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { int dist = Math . abs ( X [ i ] - X [ j ] ) ; m1 . put ( dist , m1 . getOrDefault ( dist , 0 ) + 1 ) ; } } for ( i = 0 ; i < M ; i ++ ) { for ( j = i + 1 ; j < M ; j ++ ) { int dist = Math . abs ( Y [ i ] - Y [ j ] ) ; m2 . put ( dist , m2 . getOrDefault ( dist , 0 ) + 1 ) ; } } for ( Map . Entry < Integer , Integer > entry : m1 . entrySet ( ) ) { if ( m2 . containsKey ( entry . getKey ( ) ) ) { ans += ( entry . getValue ( ) * m2 . get ( entry . getKey ( ) ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int X [ ] = { 1 , 3 , 7 } ; int Y [ ] = { 2 , 4 , 6 , 1 } ; int N = X . length ; int M = Y . length ; System . out . println ( numberOfSquares ( X , Y , N , M ) ) ; } }
import java . io . * ; class GFG { static double Area_Parallelogram1 ( int a , int b , int theta ) { double area = ( Math . abs ( Math . tan ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( a * a - b * b ) ; return area ; } static double Area_Parallelogram2 ( int a , int b , int gamma ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( gamma ) ) ) ) * Math . abs ( a * b ) ; return area ; } static double Area_Parallelogram3 ( int d1 , int d2 , int theta ) { double area = ( Math . abs ( Math . sin ( Math . toRadians ( theta ) ) ) / 2 ) * Math . abs ( d1 * d2 ) ; return area ; } public static void main ( String [ ] args ) { int d1 = 3 ; int d2 = 5 ; int theta = 90 ; double area = Area_Parallelogram3 ( d1 , d2 , theta ) ; System . out . format ( " % .2f " , area ) ; } }
import java . util . * ; class GFG { static int No_of_squares ( int N , int K ) { int no_of_squares = 0 ; no_of_squares = ( N - K + 1 ) * ( N - K + 1 ) ; return no_of_squares ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 3 ; System . out . print ( No_of_squares ( N , K ) ) ; } }
class GFG { static int No_of_cubes ( int N , int K ) { int No = 0 ; No = ( N - K + 1 ) ; No = ( int ) Math . pow ( No , 3 ) ; return No ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 2 ; System . out . print ( No_of_cubes ( N , K ) ) ; } }
import java . util . * ; class GFG { static int No_of_rectangles ( int L , int B , int l , int b ) { if ( ( l > L ) || ( b > B ) ) { return - 1 ; } else { return ( L - l + 1 ) * ( B - b + 1 ) ; } } public static void main ( String [ ] args ) { int L = 5 , B = 3 ; int l = 4 , b = 1 ; System . out . println ( No_of_rectangles ( L , B , l , b ) ) ; } }
class GFG { static void checkTriangle ( int x , int y , int z ) { if ( x == y && y == z ) System . out . println ( " Equilateral ▁ Triangle " ) ; else if ( x == y y == z z == x ) System . out . println ( " Isosceles ▁ Triangle " ) ; else System . out . println ( " Scalene ▁ Triangle " ) ; } public static void main ( String [ ] args ) { int x = 8 , y = 7 , z = 9 ; checkTriangle ( x , y , z ) ; } }
import java . util . * ; class GFG { static void findVertices ( int x1 , int y1 , int x2 , int y2 ) { if ( x1 == x2 ) { System . out . print ( ( x1 + y2 - y1 ) + " , ▁ " + y1 + "NEW_LINE"); System . out . print ( ( x2 + y2 - y1 ) + " , ▁ " + y2 ) ; } else if ( y1 == y2 ) { System . out . print ( x1 + " , ▁ " + ( y1 + x2 - x1 ) + "NEW_LINE"); System . out . print ( x2 + " , ▁ " + ( y2 + x2 - x1 ) ) ; } else if ( Math . abs ( x2 - x1 ) == Math . abs ( y2 - y1 ) ) { System . out . print ( x1 + " , ▁ " + y2 + "NEW_LINE"); System . out . print ( x2 + " , ▁ " + y1 ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 2 ; int x2 = 3 , y2 = 4 ; findVertices ( x1 , y1 , x2 , y2 ) ; } }
import java . util . * ; class GFG { static int calculateTriangles ( int sides [ ] ) { double count = Math . pow ( sides [ 0 ] + sides [ 1 ] + sides [ 2 ] , 2 ) ; count -= Math . pow ( sides [ 0 ] , 2 ) ; count -= Math . pow ( sides [ 2 ] , 2 ) ; count -= Math . pow ( sides [ 4 ] , 2 ) ; return ( int ) ( count ) ; } public static void main ( String [ ] args ) { int sides [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; System . out . print ( ( calculateTriangles ( sides ) ) + "NEW_LINE"); int sides1 [ ] = { 2 , 2 , 1 , 3 , 1 , 2 } ; System . out . print ( ( calculateTriangles ( sides1 ) ) + "NEW_LINE"); } }
class GFG { static float Length_Diagonal ( int a , int b , int d ) { float diagonal = ( float ) Math . sqrt ( 2 * ( ( a * a ) + ( b * b ) ) - ( d * d ) ) ; return diagonal ; } public static void main ( String [ ] args ) { int A = 10 ; int B = 30 ; int D = 20 ; float ans = Length_Diagonal ( A , B , D ) ; System . out . printf ( " % .1f " , ans ) ; } }
import java . util . * ; class GFG { static Vector < Float > Diagonals ( int a , int b , int c , int d ) { Vector < Float > ans = new Vector < Float > ( ) ; ans . add ( ( float ) Math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * d ) + ( b * c ) ) / ( ( a * b ) + ( c * d ) ) ) ) ; ans . add ( ( float ) Math . sqrt ( ( ( a * c ) + ( b * d ) ) * ( ( a * b ) + ( c * d ) ) / ( ( a * d ) + ( b * c ) ) ) ) ; return ans ; } public static void main ( String [ ] args ) { int A = 10 ; int B = 15 ; int C = 20 ; int D = 25 ; Vector < Float > ans = Diagonals ( A , B , C , D ) ; System . out . printf ( " % .2f ▁ % .2f " , ( ans . get ( 0 ) ) + .01 , ans . get ( 1 ) + .01 ) ; } }
import java . io . * ; class GFG { static int det ( int [ ] [ ] d ) { int Sum = d [ 0 ] [ 0 ] * ( ( d [ 1 ] [ 1 ] * d [ 2 ] [ 2 ] ) - ( d [ 2 ] [ 1 ] * d [ 1 ] [ 2 ] ) ) ; Sum -= d [ 0 ] [ 1 ] * ( ( d [ 1 ] [ 0 ] * d [ 2 ] [ 2 ] ) - ( d [ 1 ] [ 2 ] * d [ 2 ] [ 0 ] ) ) ; Sum += d [ 0 ] [ 2 ] * ( ( d [ 0 ] [ 1 ] * d [ 1 ] [ 2 ] ) - ( d [ 0 ] [ 2 ] * d [ 1 ] [ 1 ] ) ) ; return Sum ; } public static void main ( String [ ] args ) { int x1 = - 3 , y1 = 1 , z1 = 5 ; int a1 = - 3 , b1 = 1 , c1 = 5 ; int x2 = - 1 , y2 = 2 , z2 = 5 ; int a2 = - 1 , b2 = 2 , c2 = 5 ; int [ ] [ ] det_list = { { x2 - x1 , y2 - y1 , z2 - z1 } , { a1 , b1 , c1 } , { a2 , b2 , c2 } } ; if ( det ( det_list ) == 0 ) System . out . print ( " Lines ▁ are ▁ coplanar " ) ; else System . out . print ( " Lines ▁ are ▁ non ▁ coplanar " ) ; } }
import java . util . * ; class GFG { static final double PI = 3.141592653589 ; static class pair { double first , second ; public pair ( double first , double second ) { super ( ) ; this . first = first ; this . second = second ; } } static double uniform ( ) { return Math . random ( ) ; } static Vector < pair > randPoint ( int r , int x , int y , int n ) { Vector < pair > res = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { double theta = 2 * PI * uniform ( ) ; double len = Math . sqrt ( uniform ( ) ) * r ; res . add ( new pair ( x + len * Math . cos ( theta ) , y + len * Math . sin ( theta ) ) ) ; } return res ; } static void printVector ( Vector < pair > A ) { for ( pair P : A ) { System . out . printf ( "(%.2f, %.2f)NEW_LINE", P.first, P.second); } } public static void main ( String [ ] args ) { int R = 12 ; int X = 3 ; int Y = 3 ; int N = 5 ; printVector ( randPoint ( R , X , Y , N ) ) ; } }
import java . util . * ; class GFG { static void checkValidPolygon ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum == 180 * ( N - 2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 60 , 60 , 60 } ; checkValidPolygon ( arr , N ) ; } }
class GFG { public static int minimalSquareSide ( int a , int b ) { if ( a == b ) { return 2 * a ; } if ( a != b ) { if ( a > b ) { int newB = b + b ; int diff = Math . abs ( newB - a ) ; if ( newB < a ) { b = newB + diff ; if ( a == b ) return a ; return 0 ; } else { a = a + diff ; if ( a == newB ) return a ; return 0 ; } } else { int newA = a + a ; int diff = Math . abs ( newA - b ) ; if ( newA < b ) { a = diff + newA ; if ( a == b ) return a ; return 0 ; } else { b = b + diff ; if ( b == newA ) return b ; return 0 ; } } } return 0 ; } public static void main ( String [ ] args ) { int H , W ; H = 3 ; W = 1 ; System . out . println ( minimalSquareSide ( H , W ) ) ; } }
class GFG { static final int MAX = 1001 ; static boolean [ ] [ ] arr = new boolean [ MAX ] [ MAX ] ; static void updateArray ( int x1 , int y1 , int x2 , int y2 ) { for ( int i = x1 ; i < x2 ; i ++ ) { for ( int j = y1 ; j < y2 ; j ++ ) { arr [ i ] [ j ] = true ; } } } static int findAreaCovered ( ) { int area = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { if ( arr [ i ] [ j ] == true ) { area ++ ; } } } return area ; } public static void main ( String [ ] args ) { int N = 3 ; int [ ] [ ] A = { { 1 , 3 , 4 , 5 } , { 3 , 1 , 7 , 4 } , { 5 , 3 , 8 , 6 } } ; for ( int i = 0 ; i < N ; i ++ ) { updateArray ( A [ i ] [ 0 ] , A [ i ] [ 1 ] , A [ i ] [ 2 ] , A [ i ] [ 3 ] ) ; } int area = findAreaCovered ( ) ; System . out . print ( area ) ; } }
import java . util . * ; class GFG { static void findEquation ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int d , int e , int f ) { double a = x2 - x1 ; double b = y2 - y1 ; double c = z2 - z1 ; int A = ( int ) ( b * f - c * e ) ; int B = ( int ) ( a * f - c * d ) ; int C = ( int ) ( a * e - b * d ) ; int D = - ( int ) ( A * d - B * e + C * f ) ; System . out . println ( A + " x ▁ + ▁ " + B + " y ▁ + ▁ " + C + " z ▁ + ▁ " + D + " = ▁ 0 ▁ " ) ; } public static void main ( String [ ] args ) { int x1 = 2 , y1 = 3 , z1 = 5 ; int x2 = 6 , y2 = 7 , z2 = 8 ; int a = 11 , b = 23 , c = 10 ; findEquation ( x1 , y1 , z1 , x2 , y2 , z2 , a , b , c ) ; } }
import java . util . * ; class GFG { public static float median ( int a , int b , int c ) { float n = ( float ) ( Math . sqrt ( 2 * b * b + 2 * c * c - a * a ) / 2 ) ; return n ; } public static void main ( String [ ] args ) { int a , b , c ; a = 4 ; b = 3 ; c = 5 ; float ans = median ( a , b , c ) ; System . out . println ( String . format ( " % .2f " , ans ) ) ; } }
import java . util . * ; class GFG { static void findSide ( double a , double B , double C ) { double A = 180 - C - B ; double radA = ( Math . PI * ( A / 180 ) ) ; double radB = ( Math . PI * ( B / 180 ) ) ; double radC = ( Math . PI * ( C / 180 ) ) ; double b = ( a / Math . sin ( radA ) * Math . sin ( radB ) ) ; double c = ( a / Math . sin ( radA ) * Math . sin ( radC ) ) ; System . out . printf ( " % .15f " , b ) ; System . out . printf ( " ▁ % .15f " , c ) ; } public static void main ( String [ ] args ) { int a = 12 , B = 60 , C = 30 ; findSide ( a , B , C ) ; } }
class GFG { static double pi = Math . acos ( - 1.0 ) ; static double nGon ( int N ) { double proAngleVar ; if ( N % 4 == 0 ) { proAngleVar = pi * ( 180.0 / N ) / 180 ; } else { proAngleVar = pi * ( 180.0 / ( 2 * N ) ) / 180 ; } double negX = 1.0e+99 , posX = - 1.0e+99 , negY = 1.0e+99 , posY = - 1.0e+99 ; for ( int j = 0 ; j < N ; ++ j ) { double px = Math . cos ( 2 * pi * j / N + proAngleVar ) ; double py = Math . sin ( 2 * pi * j / N + proAngleVar ) ; negX = Math . min ( negX , px ) ; posX = Math . max ( posX , px ) ; negY = Math . min ( negY , py ) ; posY = Math . max ( posY , py ) ; } double opt2 = Math . max ( posX - negX , posY - negY ) ; return ( double ) opt2 / Math . sin ( pi / N ) / 2 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . printf ( " % .5f " , nGon ( N ) ) ; } }
class GFG { static int heptacontagonNum ( int n ) { return ( 68 * n * n - 66 * n ) / 2 ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( "3rd ▁ heptacontagon ▁ Number ▁ is ▁ = ▁ " + heptacontagonNum ( N ) ) ; } }
class GFG { static boolean isicositetragonal ( int N ) { float n = ( float ) ( ( 10 + Math . sqrt ( 44 * N + 100 ) ) / 22 ) ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int i = 24 ; if ( isicositetragonal ( i ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static boolean isicosihenagonal ( int N ) { float n = ( float ) ( ( 17 + Math . sqrt ( 152 * N + 289 ) ) / 38 ) ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int i = 21 ; if ( isicosihenagonal ( i ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static boolean isicositrigonal ( int N ) { float n = ( float ) ( 19 + Math . sqrt ( 168 * N + 361 ) ) / 42 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String args [ ] ) { int i = 23 ; if ( isicositrigonal ( i ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; import java . lang . * ; class GFG { static long mod = 1000000007 ; static int MAXN = 1001 ; static long factorial [ ] = new long [ MAXN ] , inverse [ ] = new long [ MAXN ] ; static long power ( long a , long b ) { long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) { res = ( res * a ) % mod ; } a = ( a * a ) % mod ; b >>= 1 ; } return res ; } static long nCk ( int n , int k ) { if ( k < 0 k > n ) { return 0 ; } long ans = factorial [ n ] ; ans = ( ans * inverse [ n - k ] ) % mod ; ans = ( ans * inverse [ k ] ) % mod ; return ans ; } static void numberOfWays ( ArrayList < int [ ] > lines , int K , int N ) { Collections . sort ( lines , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; long total_case = nCk ( N , K ) ; PriorityQueue < Integer > m = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { while ( ! m . isEmpty ( ) && ( m . peek ( ) < lines . get ( i ) [ 0 ] ) ) { m . poll ( ) ; } total_case -= nCk ( m . size ( ) , K - 1 ) ; total_case += mod ; total_case %= mod ; m . add ( lines . get ( i ) [ 1 ] ) ; } System . out . println ( total_case ) ; } static void preCompute ( ) { long fact = 1 ; factorial [ 0 ] = 1 ; inverse [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) { fact = ( fact * i ) % mod ; factorial [ i ] = fact ; inverse [ i ] = power ( factorial [ i ] , mod - 2 ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 2 ; ArrayList < int [ ] > lines = new ArrayList < > ( ) ; preCompute ( ) ; lines . add ( new int [ ] { 1 , 3 } ) ; lines . add ( new int [ ] { 4 , 5 } ) ; lines . add ( new int [ ] { 5 , 7 } ) ; numberOfWays ( lines , K , N ) ; } }
import java . util . * ; class GFG { static int Icositetragonal_num ( int n ) { return ( 22 * n * n - 20 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( Icositetragonal_num ( n ) ) ; n = 10 ; System . out . println ( Icositetragonal_num ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void checkTriangle ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ; if ( a == 0 ) System . out . println ( " No " ) ; else System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 1 , x2 = 2 , y2 = 2 , x3 = 3 , y3 = 3 ; checkTriangle ( x1 , y1 , x2 , y2 , x3 , y3 ) ; } }
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static class pair { public int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int numTrip ( ArrayList points ) { int res = 0 ; for ( int i = 0 ; i < points . size ( ) ; ++ i ) { HashMap < Long , Integer > map = new HashMap < > ( ) ; for ( int j = 0 ; j < points . size ( ) ; ++ j ) { if ( j == i ) continue ; int dy = ( ( pair ) points . get ( i ) ) . second - ( ( pair ) points . get ( j ) ) . second ; int dx = ( ( pair ) points . get ( i ) ) . first - ( ( pair ) points . get ( j ) ) . first ; long key = dy * dy ; key += dx * dx ; if ( map . containsKey ( key ) ) { map . put ( key , map . get ( key ) + 1 ) ; } else { map . put ( key , 1 ) ; } } for ( int p : map . values ( ) ) res += p * ( p - 1 ) ; } return res ; } public static void main ( String [ ] args ) { ArrayList mat = new ArrayList ( ) ; mat . add ( new pair ( 0 , 0 ) ) ; mat . add ( new pair ( 1 , 0 ) ) ; mat . add ( new pair ( 2 , 0 ) ) ; System . out . println ( numTrip ( mat ) ) ; } }
class GFG { static double area_of_circle ( int m , int n ) { int square_of_radius = ( m * n ) / 4 ; double area = ( 3.141 * square_of_radius ) ; return area ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 30 ; System . out . println ( area_of_circle ( m , n ) ) ; } }
import java . util . * ; class GFG { static int NotParallel ( int p [ ] [ ] , int n ) { HashMap < Integer , Integer > x_axis = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > y_axis = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( x_axis . containsKey ( p [ i ] [ 0 ] ) ) x_axis . put ( p [ i ] [ 0 ] , x_axis . get ( p [ i ] [ 0 ] ) + 1 ) ; else x_axis . put ( p [ i ] [ 0 ] , 1 ) ; if ( y_axis . containsKey ( p [ i ] [ 1 ] ) ) y_axis . put ( p [ i ] [ 1 ] , y_axis . get ( p [ i ] [ 1 ] ) + 1 ) ; else y_axis . put ( p [ i ] [ 1 ] , 1 ) ; } int total = ( n * ( n - 1 ) ) / 2 ; for ( Map . Entry < Integer , Integer > i : x_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } for ( Map . Entry < Integer , Integer > i : y_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } public static void main ( String [ ] args ) { int p [ ] [ ] = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = p . length ; System . out . print ( NotParallel ( p , n ) ) ; } }
class GFG { static int check ( int n , int m ) { if ( m % n == 0 ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } return 0 ; } public static void main ( String [ ] args ) { int n = 5 ; int m = 10 ; check ( n , m ) ; } }
import java . util . * ; class GFG { static void findModulo ( String s ) { int l = s . length ( ) ; int i , modulus = 0 ; if ( s . contains ( " + " ) ) { i = s . indexOf ( " + " ) ; } else { i = s . indexOf ( " - " ) ; } String real = s . substring ( 0 , i ) ; String imaginary = s . substring ( i + 1 , l - 1 ) ; int x = Integer . parseInt ( real ) ; int y = Integer . parseInt ( imaginary ) ; System . out . print ( Math . sqrt ( x * x + y * y ) + "NEW_LINE"); } public static void main ( String [ ] args ) { String s = "3 + 4i " ; findModulo ( s ) ; } }
import java . util . * ; class GFG { static void disp ( int row_no , int block ) { System . out . print ( row_no * block ) ; } static int row ( int ht , int h ) { return ht / h ; } static void calculate ( int l , int w , int h , int a , int ht ) { int no_block = ( 4 * a ) / l ; int row_no ; if ( h < w ) row_no = row ( ht , w ) ; else row_no = row ( ht , h ) ; disp ( row_no , no_block ) ; } public static void main ( String [ ] args ) { int l = 50 , w = 20 , h = 35 ; int a = 700 ; int ht = 140 ; calculate ( l , w , h , a , ht ) ; } }
class GFG { static double solve ( int s ) { double area = ( 1.732 * Math . pow ( s , 2 ) ) / 8 ; return area ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . println ( solve ( n ) ) ; } }
class GFG { static float circlearea ( float R ) { if ( R < 0 ) return - 1 ; float a = ( float ) ( ( 3.14 * R * R ) / 4 ) ; return a ; } public static void main ( String [ ] args ) { float R = 2 ; System . out . println ( circlearea ( R ) ) ; } }
class GFG { static float find_Area ( float a ) { float R = a * ( float ) ( 2.0 - Math . sqrt ( 2 ) ) ; float area = ( float ) ( ( 3.14 * R * R ) / 2.0 ) ; return area ; } public static void main ( String [ ] args ) { float a = 4 ; System . out . println ( " ▁ Area ▁ of ▁ semicircle ▁ = ▁ " + find_Area ( a ) ) ; } }
class GFG { static int times ( int [ ] steps , int n ) { int current_level = 0 ; int previous_level = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { previous_level = current_level ; current_level = current_level + steps [ i ] ; if ( ( previous_level < 0 && current_level >= 0 ) || ( previous_level > 0 && current_level <= 0 ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int steps [ ] = { 1 , - 1 , 0 , 0 , 1 , 1 , - 3 , 2 } ; int n = steps . length ; System . out . println ( times ( steps , n ) ) ; } }
import java . util . * ; class solution { static double [ ] XandYandZintercept ( double A , double B , double C , double D ) { double [ ] rslt = new double [ 3 ] ; double x = - D / A ; double y = - D / B ; double z = - D / C ; rslt [ 0 ] = x ; rslt [ 1 ] = y ; rslt [ 2 ] = z ; return rslt ; } static void equation_plane ( int [ ] p , int [ ] q , int [ ] r ) { int x1 = p [ 0 ] ; int y1 = p [ 1 ] ; int z1 = p [ 2 ] ; int x2 = q [ 0 ] ; int y2 = q [ 1 ] ; int z2 = q [ 2 ] ; int x3 = r [ 0 ] ; int y3 = r [ 1 ] ; int z3 = r [ 2 ] ; int a1 = x2 - x1 ; int b1 = y2 - y1 ; int c1 = z2 - z1 ; int a2 = x3 - x1 ; int b2 = y3 - y1 ; int c2 = z3 - z1 ; int A = b1 * c2 - b2 * c1 ; int B = a2 * c1 - a1 * c2 ; int C = a1 * b2 - b1 * a2 ; int D = ( - A * x1 - B * y1 - C * z1 ) ; double [ ] rslt = XandYandZintercept ( A , B , C , D ) ; for ( int i = 0 ; i < 3 ; i ++ ) { System . out . printf ( rslt [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int x1 = - 1 ; int y1 = 2 ; int z1 = 1 ; int x2 = 0 ; int y2 = - 3 ; int z2 = 2 ; int x3 = 1 ; int y3 = 1 ; int z3 = - 4 ; int [ ] p = { x1 , y1 , z1 } ; int [ ] q = { x2 , y2 , z2 } ; int [ ] r = { x3 , y3 , z3 } ; equation_plane ( p , q , r ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > FindPoints ( int n ) { Vector < pair > v = new Vector < pair > ( ) ; v . add ( new pair ( 0 , 0 ) ) ; v . add ( new pair ( 0 , n ) ) ; v . add ( new pair ( n , 0 ) ) ; v . add ( new pair ( n , n ) ) ; if ( n % 2 == 0 ) v . add ( new pair ( n / 2 , n / 2 ) ) ; return v ; } public static void main ( String [ ] args ) { int N = 8 ; Vector < pair > v = FindPoints ( N ) ; for ( pair i : v ) { System . out . print ( " ( " + i . first + " , ▁ " + i . second + " ) ▁ " ) ; } } }
class GFG { static int sumOfInternalAngles ( int n ) { if ( n < 3 ) return 0 ; return ( ( n - 2 ) * 180 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . print ( sumOfInternalAngles ( n ) ) ; } }
class GFG { static int cntWays ( int n ) { if ( n % 2 == 1 ) { return 0 ; } else { return ( n - 2 ) / 4 ; } } public static void main ( String [ ] args ) { int n = 18 ; System . out . println ( cntWays ( n ) ) ; } }
class GFG { static int noOfTriangles ( int n ) { int y = 4 * n ; return ( ( y * ( y - 2 ) * ( y - 1 ) ) - ( 4 * n * ( n - 2 ) * ( n - 1 ) ) ) / 6 ; } public static void main ( String [ ] args ) { int n = 1 ; System . out . println ( noOfTriangles ( n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static boolean orthogonality ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int dsquare = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; if ( dsquare == r1 * r1 + r2 * r2 ) return true ; else return false ; } public static void main ( String [ ] args ) throws java . lang . Exception { int x1 = 4 , y1 = 3 ; int x2 = 0 , y2 = 1 ; int r1 = 2 , r2 = 4 ; boolean f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( f ) System . out . println ( " Given ▁ circles ▁ are ▁ orthogonal . " ) ; else System . out . println ( " Given ▁ circles ▁ are ▁ not ▁ orthogonal . " ) ; } }
import java . util . * ; class GFG { static int dx [ ] = { - 1 , 0 , 1 , 0 } ; static int dy [ ] = { 0 , 1 , 0 , - 1 } ; static boolean ifRight ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int a = ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) ; int b = ( ( x1 - x3 ) * ( x1 - x3 ) ) + ( ( y1 - y3 ) * ( y1 - y3 ) ) ; int c = ( ( x2 - x3 ) * ( x2 - x3 ) ) + ( ( y2 - y3 ) * ( y2 - y3 ) ) ; if ( ( a == ( b + c ) && a != 0 && b != 0 && c != 0 ) || ( b == ( a + c ) && a != 0 && b != 0 && c != 0 ) || ( c == ( a + b ) && a != 0 && b != 0 && c != 0 ) ) { return true ; } return false ; } static void isValidCombination ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { int x , y ; boolean possible = false ; if ( ifRight ( x1 , y1 , x2 , y2 , x3 , y3 ) ) { System . out . print ( " ALREADY ▁ RIGHT ▁ ANGLED " ) ; return ; } else { for ( int i = 0 ; i < 4 ; i ++ ) { x = dx [ i ] + x1 ; y = dy [ i ] + y1 ; if ( ifRight ( x , y , x2 , y2 , x3 , y3 ) ) { System . out . print ( " POSSIBLE " ) ; return ; } x = dx [ i ] + x2 ; y = dy [ i ] + y2 ; if ( ifRight ( x1 , y1 , x , y , x3 , y3 ) ) { System . out . print ( " POSSIBLE " ) ; return ; } x = dx [ i ] + x3 ; y = dy [ i ] + y3 ; if ( ifRight ( x1 , y1 , x2 , y2 , x , y ) ) { System . out . print ( " POSSIBLE " ) ; return ; } } } if ( ! possible ) System . out . println ( " NOT ▁ POSSIBLE " ) ; } public static void main ( String [ ] args ) { int x1 = - 49 , y1 = 0 ; int x2 = 0 , y2 = 50 ; int x3 = 0 , y3 = - 50 ; isValidCombination ( x1 , y1 , x2 , y2 , x3 , y3 ) ; } }
import java . util . * ; class GFG { static double area_of_regular_polygon ( double n , double len ) { double P = ( len * n ) ; double A = len / ( 2 * Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ; double area = ( P * A ) / 2 ; return area ; } static double area_of_triangle_inscribed ( double n , double len ) { double area = area_of_regular_polygon ( n , len ) ; double triangle = area / n ; double ins_tri = ( triangle * 3 ) ; return ins_tri ; } static public void main ( String [ ] arg ) { double n = 6 , len = 10 ; System . out . printf ( " % .3f " , area_of_triangle_inscribed ( n , len ) ) ; } }
class GFG { static void maxRegions ( int n ) { int num ; num = n * ( n + 1 ) / 2 + 1 ; System . out . println ( num ) ; ; } public static void main ( String [ ] args ) { int n = 10 ; maxRegions ( n ) ; } }
class GFG { static float diagonal_length ( float a ) { float L ; L = a * ( float ) Math . sqrt ( 3 ) ; return L ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( diagonal_length ( a ) ) ; } }
class GFG { static int concentric_Haxagon ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( concentric_Haxagon ( n ) ) ; } }
class GFG { static double PI = 3.14 ; static double find_area ( int r , int d ) { double R = d / PI ; R += Math . pow ( r , 2 ) ; R = Math . sqrt ( R ) ; double area = PI * Math . pow ( R , 2 ) ; return area ; } public static void main ( String [ ] args ) { int r = 4 , d = 5 ; System . out . println ( find_area ( r , d ) ) ; } }
class GFG { static double calculate_angle ( double n ) { double total_angle = 360 ; return total_angle / n ; } public static void main ( String [ ] args ) { double N = 5 ; System . out . println ( calculate_angle ( N ) ) ; } }
class GFG { static float areaOfKite ( int d1 , int d2 ) { float area = ( d1 * d2 ) / 2 ; return area ; } public static void main ( String [ ] args ) { int d1 = 4 , d2 = 6 ; System . out . println ( " Area ▁ of ▁ Kite ▁ = ▁ " + areaOfKite ( d1 , d2 ) ) ; } }
import java . io . * ; class GFG { static double PI = ( 3.14159 / 180 ) ; static float areaOfKite ( int a , int b , double angle ) { angle = angle * PI ; double area = a * b * Math . sin ( angle ) ; return ( float ) area ; } public static void main ( String [ ] args ) { int a = 4 , b = 7 , angle = 78 ; System . out . println ( " Area ▁ of ▁ Kite ▁ = ▁ " + areaOfKite ( a , b , angle ) ) ; } }
class GFG { static float angleOncirCumference ( float z ) { return ( z / 2 ) ; } public static void main ( String [ ] args ) { float angle = 65 ; float z = angleOncirCumference ( angle ) ; System . out . println ( " The ▁ angle ▁ is ▁ " + z + " ▁ degrees " ) ; } }
import java . util . * ; class GFG { static void PrintMaxMinValue ( double a , double b , double c ) { double secondPart = c * 1.0 - ( b * b / ( 4.0 * a ) ) ; if ( a > 0 ) { System . out . print ( " Maxvalue ▁ = ▁ " + "InfinityNEW_LINE"); System . out . print ( " Minvalue ▁ = ▁ " + secondPart ) ; } else if ( a < 0 ) { System . out . print ( " Maxvalue ▁ = ▁ " + secondPart + "NEW_LINE"); System . out . print ( " Minvalue ▁ = ▁ " + " - Infinity " ) ; } else { System . out . print ( "Not a quadratic functionNEW_LINE"); } } public static void main ( String [ ] args ) { double a = - 1 , b = 3 , c = - 2 ; PrintMaxMinValue ( a , b , c ) ; } }
import java . io . * ; class GFG { static void newvol ( double x ) { System . out . print ( " percentage ▁ increase ▁ " + " in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " + x + " % " ) ; } public static void main ( String [ ] args ) { double x = 10 ; newvol ( x ) ; } }
import java . io . * ; class GFG { static void radius ( double n , double d ) { System . out . print ( " The ▁ side ▁ of ▁ each ▁ square ▁ is ▁ " + d / ( ( n - 1 ) * Math . sqrt ( 2 ) ) ) ; } public static void main ( String [ ] args ) { double d = 42 , n = 4 ; radius ( n , d ) ; } }
class GFG { static double innerCirclearea ( double radius ) { if ( radius < 0 ) { return - 1 ; } double r = radius / 2 ; double Area = ( 3.14 * Math . pow ( r , 2 ) ) ; return Area ; } public static void main ( String arr [ ] ) { double radius = 4 ; System . out . println ( " Area ▁ of ▁ circle ▁ c2 ▁ = ▁ " + innerCirclearea ( radius ) ) ; } }
public class GFG { static double PI = 3.14159 ; static double areaOfTriangle ( float d ) { float c = ( float ) ( 1.618 * d ) ; float s = ( d + c + c ) / 2 ; double area = Math . sqrt ( s * ( s - c ) * ( s - c ) * ( s - d ) ) ; return 5 * area ; } static double areaOfRegPentagon ( float d ) { double cal = 4 * Math . tan ( PI / 5 ) ; double area = ( 5 * d * d ) / cal ; return area ; } static double areaOfPentagram ( float d ) { return areaOfRegPentagon ( d ) + areaOfTriangle ( d ) ; } public static void main ( String [ ] args ) { float d = 5 ; System . out . println ( areaOfPentagram ( d ) ) ; } }
import java . io . * ; class GFG { static void angleequichord ( int z ) { System . out . println ( " The ▁ angle ▁ subtended ▁ at ▁ the ▁ center ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; angleequichord ( z ) ; } }
import java . io . * ; class GFG { static void lengequichord ( int z ) { System . out . println ( " The ▁ distance ▁ between ▁ the ▁ " + " chord ▁ and ▁ the ▁ center ▁ is ▁ " + z ) ; } public static void main ( String [ ] args ) { int z = 48 ; lengequichord ( z ) ; } }
class GFG { static void lengperpbisect ( double r1 , double r2 ) { double z = 2 * Math . sqrt ( ( r1 * r1 ) - ( ( r1 - r2 ) * ( r1 - r2 ) / 4 ) ) ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ " + " perpendicular ▁ bisector ▁ is ▁ " + z ) ; } public static void main ( String [ ] args ) { double r1 = 5 , r2 = 3 ; lengperpbisect ( r1 , r2 ) ; } }
import java . io . * ; class GFG { static void anglequichord ( int z ) { System . out . println ( " The ▁ angle ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; anglequichord ( z ) ; } }
import java . lang . Math ; import java . io . * ; class GFG { static double distcenter ( int r1 , int r2 , int x ) { double z = ( Math . sqrt ( ( r1 * r1 ) - ( x / 2 * x / 2 ) ) ) + ( Math . sqrt ( ( r2 * r2 ) - ( x / 2 * x / 2 ) ) ) ; System . out . println ( " distance ▁ between ▁ the " + " ▁ centers ▁ is ▁ " + ( int ) z ) ; return 0 ; } public static void main ( String [ ] args ) { int r1 = 24 , r2 = 37 , x = 40 ; distcenter ( r1 , r2 , x ) ; } }
import java . io . * ; class GFG { static void anglechordtang ( int z ) { System . out . print ( " The ▁ angle ▁ between ▁ tangent " + " ▁ and ▁ the ▁ chord ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; anglechordtang ( z ) ; } }
import java . util . * ; class GFG { static class TreeNode { int val ; TreeNode left , right ; TreeNode ( int x ) { val = x ; left = null ; right = null ; } } ; static int findSum ( TreeNode root ) { Queue < TreeNode > que = new LinkedList < > ( ) ; que . add ( root ) ; int ans = 0 ; while ( que . size ( ) > 0 ) { int leaf = 0 ; int nonleaf = 0 ; int length = que . size ( ) ; while ( length > 0 ) { TreeNode temp = que . peek ( ) ; que . remove ( ) ; if ( temp . left == null && temp . right == null ) leaf += temp . val ; else nonleaf += temp . val ; if ( temp . left != null ) que . add ( temp . left ) ; if ( temp . right != null ) que . add ( temp . right ) ; length -= 1 ; } ans += leaf & nonleaf ; } return ans ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 5 ) ; root . left = new TreeNode ( 3 ) ; root . right = new TreeNode ( 9 ) ; root . left . left = new TreeNode ( 6 ) ; root . left . right = new TreeNode ( 4 ) ; root . left . left . right = new TreeNode ( 7 ) ; System . out . print ( findSum ( root ) ) ; } }
import java . util . * ; class GFG { static void MinTime ( int [ ] a , int [ ] b , int n , int m ) { int ans = 2147483647 ; int [ ] prefix_a = new int [ 10 ] ; int [ ] prefix_b = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { prefix_a [ a [ i ] ] ++ ; } for ( int i = 0 ; i < m ; i ++ ) { prefix_b [ b [ i ] ] ++ ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_a [ i ] += prefix_a [ i - 1 ] ; } for ( int i = 1 ; i <= 9 ; i ++ ) { prefix_b [ i ] += prefix_b [ i - 1 ] ; } for ( int i = 0 ; i <= 9 ; i ++ ) { ans = Math . min ( ans , prefix_a [ i ] + m - prefix_b [ i ] ) ; ans = Math . min ( ans , n - prefix_a [ i ] + prefix_b [ i ] ) ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] A = { 0 , 0 , 1 , 3 , 3 } ; int [ ] B = { 2 , 0 , 3 } ; int N = A . length ; int M = B . length ; MinTime ( A , B , N , M ) ; } }
import java . util . * ; public class GFG { static int findReverse ( int N ) { int rev = 0 ; while ( N > 0 ) { rev <<= 1 ; if ( ( N & 1 ) == 1 ) rev ^= 1 ; N >>= 1 ; } return rev ; } static void countElements ( int arr [ ] , int N ) { Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { ans . add ( findReverse ( arr [ i ] ) ) ; } HashMap < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { cnt . put ( arr [ i ] , 1 ) ; } int count = 0 ; for ( Integer i : ans ) { if ( cnt . containsKey ( i ) ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 30 , 3 , 8 , 12 } ; int N = arr . length ; countElements ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean [ ] prime = new boolean [ 1000005 ] ; static void SieveOfEratosthenes ( int N ) { Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= N ; i += p ) prime [ i ] = false ; } } } static void checkElementsOnDiagonal ( int [ ] [ ] M , int N ) { int flag = 1 ; SieveOfEratosthenes ( 1000000 ) ; for ( int i = 0 ; i < N ; i ++ ) { boolean flg = ( boolean ) ( prime [ M [ i ] [ i ] ] && prime [ M [ i ] [ N - 1 - i ] ] ) ; int val = ( flg ) ? 1 : 0 ; flag &= val ; } if ( flag != 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int [ ] [ ] M = { { 1 , 2 , 3 , 13 } , { 5 , 3 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 7 } } ; int N = M . length ; checkElementsOnDiagonal ( M , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static List < Integer > circularPermutation ( int n , int start ) { List < Integer > res = new ArrayList < > ( List . of ( 0 ) ) , ret = new ArrayList < > ( ) ; int index = - 1 ; for ( int k = 0 , add = 1 << k ; k < n ; k ++ , add = 1 << k ) { for ( int i = res . size ( ) - 1 ; i >= 0 ; i -- ) { if ( res . get ( i ) + add == start ) index = res . size ( ) ; res . add ( res . get ( i ) + add ) ; } } if ( start == 0 ) return res ; while ( ret . size ( ) < res . size ( ) ) { ret . add ( res . get ( index ) ) ; index = ( index + 1 ) % res . size ( ) ; } return ret ; } public static void main ( String [ ] args ) { int N = 2 , S = 3 ; System . out . println ( circularPermutation ( N , S ) ) ; } }
import java . util . * ; class GFG { static void canMakeEqual ( int [ ] arr1 , int [ ] arr2 , int N ) { int count = 0 ; int arr1_one = 0 , arr1_zero = 0 ; int arr2_one = 0 , arr2_zero = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr1 [ i ] == 1 ) { arr1_one ++ ; } else if ( arr1 [ i ] == 0 ) { arr1_zero ++ ; } if ( arr2 [ i ] == 1 ) { arr2_one ++ ; } else if ( arr2 [ i ] == 0 ) { arr2_zero ++ ; } } if ( arr1_one != arr2_one arr1_zero != arr2_zero ) { System . out . print ( " No " ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { count = count + ( arr1 [ i ] - arr2 [ i ] ) ; if ( count < 0 ) { System . out . print ( " No " ) ; return ; } } System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 0 , 1 , 1 , 0 } ; int [ ] arr2 = { 0 , 0 , 1 , 1 } ; int N = arr1 . length ; canMakeEqual ( arr1 , arr2 , N ) ; } }
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 ) ; } }
import java . io . * ; class GFG { public static void sumFib ( int N ) { long num = ( long ) Math . round ( Math . pow ( ( Math . sqrt ( 5 ) + 1 ) / 2.0 , N + 2 ) / Math . sqrt ( 5 ) ) ; System . out . println ( num - 1 ) ; } public static void main ( String [ ] args ) { int N = 3 ; sumFib ( N ) ; } }
import java . io . * ; class GFG { public static void sumFib ( int N ) { double num = ( 1 - Math . sqrt ( 5 ) ) / 2 ; long val = Math . round ( Math . abs ( 1 / ( Math . pow ( num , N + 2 ) + Math . pow ( num , N + 1 ) + Math . pow ( num , N ) + Math . pow ( num , N - 1 ) ) ) - 1 ) ; System . out . println ( val ) ; } public static void main ( String [ ] args ) { int N = 3 ; sumFib ( N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void minOperations ( int arr [ ] , int n , int k ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int ans = 0 ; while ( mp . containsKey ( k ) == false || mp . get ( k ) < n ) { for ( int i = 1 ; i <= k - 1 ; i ++ ) { if ( mp . containsKey ( i ) && mp . get ( i ) > 0 ) { mp . put ( i , mp . get ( i ) - 1 ) ; if ( mp . containsKey ( i + 1 ) ) mp . put ( i + 1 , mp . get ( i + 1 ) + 1 ) ; else mp . put ( i + 1 , 1 ) ; if ( mp . containsKey ( i + 1 ) && mp . get ( i + 1 ) == 1 ) { i ++ ; } } } ans ++ ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int K = 5 ; int N = arr . length ; minOperations ( arr , N , K ) ; } }
import java . io . * ; class GFG { static void poorPigs ( int buckets , int minutesToDie , int minutesToTest ) { System . out . print ( ( int ) Math . ceil ( Math . log ( buckets ) / Math . log ( ( minutesToTest / minutesToDie ) + 1 ) ) ) ; } public static void main ( String [ ] args ) { int N = 1000 , M = 15 , P = 60 ; poorPigs ( N , M , P ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void maxProduct ( int [ ] arr , int N ) { int max_product = Integer . MIN_VALUE ; int prefix_sum = 0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N - 1 ; i ++ ) { prefix_sum += arr [ i ] ; int X = prefix_sum ; int Y = sum - prefix_sum ; max_product = Math . max ( max_product , X * Y ) ; } System . out . print ( max_product ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 6 , 7 } ; int N = arr . length ; maxProduct ( arr , N ) ; } }
import java . io . * ; class GFG { static int findMask ( int K ) { int mask = 1 ; while ( ( K & mask ) == 0 ) { mask = mask << 1 ; } return mask ; } public static void sameRightSetBitPos ( int [ ] arr , int N , int K ) { int mask = findMask ( K ) ; final int pos = ( K & mask ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & mask ) == pos ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 4 , 6 , 7 , 9 , 12 , 15 } ; int N = arr . length ; int K = 7 ; sameRightSetBitPos ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 100001 ; static void maxSum ( int arr [ ] , int N , int K ) { if ( N < K ) { System . out . println ( " Invalid " ) ; return ; } int CountDistinct [ ] = new int [ MAX + 1 ] ; boolean prime [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i <= MAX ; i ++ ) { CountDistinct [ i ] = 0 ; prime [ i ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) { CountDistinct [ i ] = 1 ; for ( int j = i * 2 ; j <= MAX ; j += i ) { CountDistinct [ j ] ++ ; prime [ j ] = false ; } } } int Maxarr_sum = 0 , DistPrimeSum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { Maxarr_sum += arr [ i ] ; DistPrimeSum += CountDistinct [ arr [ i ] ] ; } int curr_sum = DistPrimeSum ; int curr_arrSum = Maxarr_sum ; for ( int i = K ; i < N ; i ++ ) { curr_sum += CountDistinct [ arr [ i ] ] - CountDistinct [ arr [ i - K ] ] ; curr_arrSum += arr [ i ] - arr [ i - K ] ; if ( curr_sum > DistPrimeSum ) { DistPrimeSum = curr_sum ; Maxarr_sum = curr_arrSum ; } else if ( curr_sum == DistPrimeSum ) { Maxarr_sum = Math . max ( curr_arrSum , Maxarr_sum ) ; } } System . out . println ( Maxarr_sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 2 , 10 , 3 } ; int K = 3 ; int N = arr . length ; maxSum ( arr , N , K ) ; } }
import java . util . * ; class GFG { static boolean checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . sqrt ( x ) ) ; i ++ ) { if ( ( x == i * ( i + 1 ) ) != false ) { return true ; } } return false ; } static void countPronic ( int A , int B ) { int count = 0 ; for ( int i = A ; i <= B ; i ++ ) { if ( checkPronic ( i ) != false ) { count ++ ; } } System . out . print ( count ) ; } public static void main ( String args [ ] ) { int A = 3 , B = 20 ; countPronic ( A , B ) ; } }
import java . util . * ; class GFG { static void oddDivisors ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int powerOfTwo = 2 ; int count = 0 ; while ( powerOfTwo <= arr [ i ] ) { count ++ ; powerOfTwo = 2 * powerOfTwo ; } System . out . print ( count + " ▁ " ) ; } return ; } public static void main ( String args [ ] ) { int arr [ ] = { 15 , 16 , 20 , 35 } ; int N = arr . length ; oddDivisors ( arr , N ) ; } }
import java . util . * ; class GFG { static void productExceptSelf ( int arr [ ] , int N ) { int product = 1 ; int z = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] != 0 ) product *= arr [ i ] ; if ( arr [ i ] == 0 ) z += 1 ; } int a = Math . abs ( product ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( z == 1 ) { if ( arr [ i ] != 0 ) arr [ i ] = 0 ; else arr [ i ] = product ; continue ; } else if ( z > 1 ) { arr [ i ] = 0 ; continue ; } int b = Math . abs ( arr [ i ] ) ; int curr = ( int ) Math . round ( Math . exp ( Math . log ( a ) - Math . log ( b ) ) ) ; if ( arr [ i ] < 0 && product < 0 ) arr [ i ] = curr ; else if ( arr [ i ] > 0 && product > 0 ) arr [ i ] = curr ; else arr [ i ] = - 1 * curr ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 3 , 5 , 6 , 2 } ; int N = arr . length ; productExceptSelf ( arr , N ) ; } }
import java . util . * ; class GFG { static void highestPowerOfTwo ( int num ) { String s = Integer . toString ( num ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { System . out . print ( "0" ) ; continue ; } int lg = ( int ) ( Math . log ( s . charAt ( i ) - '0' ) / Math . log ( 2 ) ) ; int p = ( int ) Math . pow ( 2 , lg ) ; System . out . print ( ( char ) ( p + 48 ) ) ; } } public static void main ( String args [ ] ) { int num = 4317 ; highestPowerOfTwo ( num ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void maxSumAfterKReverse ( Integer arr [ ] , int K , int N ) { int sum = Integer . MIN_VALUE ; if ( K % 2 != 0 ) Collections . reverse ( Arrays . asList ( arr ) ) ; int currsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = Math . max ( sum , currsum ) ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 5 , 8 , 9 , 11 , 2 } ; int K = 1 ; int N = arr . length ; maxSumAfterKReverse ( arr , K , N ) ; } }
import java . io . * ; class GFG { static int isPossible ( int N ) { return ( ( ( N & ( N - 1 ) ) & N ) ) ; } static void countElements ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( isPossible ( i ) != 0 ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 15 ; countElements ( N ) ; } }
import java . util . * ; class GFG { static void countElements ( int N ) { int Cur_Ele = 1 ; int Count = 0 ; while ( Cur_Ele <= N ) { Count ++ ; Cur_Ele = Cur_Ele * 2 ; } System . out . print ( N - Count ) ; } public static void main ( String [ ] args ) { int N = 15 ; countElements ( N ) ; } }
import java . util . * ; class GFG { static void SubMasks ( int N ) { for ( int S = N ; S > 0 ; S = ( S - 1 ) & N ) { System . out . print ( S + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 25 ; SubMasks ( N ) ; } }
import java . io . * ; class GFG { static int MOD = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } static void countPairs ( int N ) { int high = power ( 2 , N ) ; int count = 0 ; for ( int i = 0 ; i < high ; i ++ ) { for ( int j = 0 ; j < high ; j ++ ) { int X = ( i ^ j ) ; int Y = ( i j ) ; if ( X == Y ) { count ++ ; } } } System . out . println ( count % MOD ) ; } public static void main ( String [ ] args ) { int N = 10 ; countPairs ( N ) ; } }
import java . util . * ; class GFG { static final int MOD = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y >> 1 ; x = ( x * x ) % MOD ; } return res ; } static void countPairs ( int N ) { System . out . print ( power ( 3 , N ) ) ; } public static void main ( String [ ] args ) { int N = 10 ; countPairs ( N ) ; } }
import java . util . * ; class GFG { static void maxAdjacent ( int [ ] arr , int N ) { Vector < Integer > res = new Vector < Integer > ( ) ; int arr_max = Integer . MIN_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { arr_max = Math . max ( arr_max , Math . abs ( arr [ i - 1 ] - arr [ i ] ) ) ; } for ( int i = 1 ; i < N - 1 ; i ++ ) { int curr_max = Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; int ans = Math . max ( curr_max , arr_max ) ; res . add ( ans ) ; } for ( int x : res ) System . out . print ( x + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 7 , 8 } ; int N = arr . length ; maxAdjacent ( arr , N ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minimumIncrement ( int arr [ ] , int N ) { if ( N % 2 != 0 ) { System . out . println ( " - 1" ) ; System . exit ( 0 ) ; } int cntEven = 0 ; int cntOdd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { cntEven += 1 ; } } cntOdd = N - cntEven ; return Math . abs ( cntEven - cntOdd ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 4 , 9 } ; int N = arr . length ; System . out . println ( minimumIncrement ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void divisionalArrays ( int arr [ ] , int N ) { int sum = N ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry x : mp . entrySet ( ) ) { if ( ( int ) x . getValue ( ) > 1 ) { sum += ( int ) x . getValue ( ) - 1 ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 } ; int N = arr . length ; divisionalArrays ( arr , N ) ; } }
import java . util . * ; class GFG { static void totalInversions ( int arr [ ] , int K , int N ) { int inv = 0 ; int X = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) { if ( arr [ i ] > arr [ j ] && i < j ) inv ++ ; if ( arr [ i ] > arr [ j ] ) X ++ ; } } int totalInv = X * K * ( K - 1 ) / 2 + inv * K ; System . out . println ( totalInv ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 } ; int K = 3 ; int N = arr . length ; totalInversions ( arr , K , N ) ; } }
import java . util . * ; class GFG { static int MaximumSum ( int [ ] [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = 0 , y = i , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } for ( int i = 1 ; i < n ; i ++ ) { int x = i , y = 0 , sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ x ++ ] [ y ++ ] ; } if ( sum > ans ) ans = sum ; } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 2 , 3 } , { 3 , 5 , 10 } , { 1 , 3 , 5 } } ; int n = arr . length ; System . out . print ( MaximumSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void numberOfIntegers ( int [ ] arr , int N ) { int cur_max = 0 , count = 0 ; if ( N == 1 ) { count = 1 ; } else { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] > cur_max ) { cur_max = arr [ i ] ; if ( arr [ i ] > arr [ i + 1 ] ) { count ++ ; } } } if ( arr [ N - 1 ] > cur_max ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 0 , 7 , 2 , 0 , 2 , 0 } ; int N = arr . length ; numberOfIntegers ( arr , N ) ; } }
import java . util . * ; class GFG { static int numberOfWays ( int n , int k ) { if ( n == 0 ) return 1 ; if ( k == 0 ) return 1 ; if ( n >= ( int ) Math . pow ( 2 , k ) ) { int curr_val = ( int ) Math . pow ( 2 , k ) ; return numberOfWays ( n - curr_val , k ) + numberOfWays ( n , k - 1 ) ; } else return numberOfWays ( n , k - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; int k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; System . out . println ( numberOfWays ( n , k ) ) ; } }
import java . util . * ; class GFG { static int possibleTriplets ( int arr [ ] , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { freq ++ ; } } return ( int ) ( ( freq * 1L * ( freq - 1 ) * ( freq - 2 ) ) / 6 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , - 9 , - 3 , 6 } ; int N = arr . length ; System . out . print ( possibleTriplets ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void maxPossibleDiff ( Vector < Integer > arr , int N ) { arr . add ( - 1 ) ; Collections . reverse ( arr ) ; arr . add ( - 1 ) ; Collections . reverse ( arr ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr . get ( i ) > arr . get ( i - 1 ) && arr . get ( i ) > arr . get ( i + 1 ) ) { maxDiff += arr . get ( i ) ; } if ( arr . get ( i ) < arr . get ( i - 1 ) && arr . get ( i ) < arr . get ( i + 1 ) ) { maxDiff -= arr . get ( i ) ; } } System . out . print ( maxDiff ) ; } public static void main ( String [ ] args ) { int [ ] array = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; Vector < Integer > v = new Vector < > ( ) ; for ( int i : array ) { v . add ( i ) ; } int N = v . size ( ) ; maxPossibleDiff ( v , N ) ; } }
class GFG { static void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void reverseCircularArray ( int arr [ ] , int N , int K ) { int start = K , end = K - 1 ; int count = N / 2 ; while ( count != 0 ) { int temp = arr [ start % N ] ; arr [ start % N ] = arr [ end % N ] ; arr [ end % N ] = temp ; start ++ ; end -- ; if ( end == - 1 ) { end = N - 1 ; } count -= 1 ; } printArray ( arr , N ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 2 , 4 , 1 } ; int K = 2 ; int N = arr . length ; reverseCircularArray ( arr , N , K ) ; } }
import java . io . * ; class GFG { static void findOriginalMatrix ( int [ ] [ ] B , int N , int M ) { int [ ] [ ] A = new int [ N ] [ M ] ; for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { A [ i ] [ j ] = 1 ; } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { if ( B [ i ] [ j ] == 0 ) { for ( int k = 0 ; k < M ; ++ k ) { A [ i ] [ k ] = 0 ; } for ( int k = 0 ; k < N ; ++ k ) { A [ k ] [ j ] = 0 ; } } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { int c = 0 ; for ( int k = 0 ; k < M ; ++ k ) { if ( c == 1 ) break ; c += A [ i ] [ k ] ; } for ( int k = 0 ; k < N ; ++ k ) { if ( c == 1 ) break ; c += A [ k ] [ j ] ; } if ( c != B [ i ] [ j ] ) { System . out . println ( " Not ▁ Possible " ) ; return ; } } } for ( int i = 0 ; i < N ; ++ i ) { for ( int j = 0 ; j < M ; ++ j ) { System . out . print ( A [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] B = new int [ ] [ ] { { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int N = B . length ; int M = B [ 0 ] . length ; findOriginalMatrix ( B , N , M ) ; } }
import java . util . * ; class GFG { static int gcd ( int x , int y ) { if ( y == 0 ) return x ; return gcd ( y , x % y ) ; } static int lcm ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int minimumCost ( int x , int y ) { int lcm_ = lcm ( x , y ) ; int costx = ( lcm_ - x ) / x ; int costy = ( lcm_ - y ) / y ; return costx + costy ; } public static void main ( String [ ] args ) { int x = 5 , y = 17 ; System . out . print ( minimumCost ( x , y ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int a , int n , int d ) { int res = 1 ; while ( n != 0 ) { if ( ( n % 2 ) != 0 ) { res = ( ( res % d ) * ( a % d ) ) % d ; } a = ( ( a % d ) * ( a % d ) ) % d ; n /= 2 ; } return res ; } static int gcd ( int p , int q , int n ) { if ( p == q ) { return ( power ( p , n , mod ) + power ( q , n , mod ) ) % mod ; } int candidate = 1 ; int num = p - q ; int sq = ( int ) Math . sqrt ( num ) ; for ( int i = 1 ; i <= sq ; ++ i ) { if ( num % i == 0 ) { int X = power ( p , n , i ) ; int Y = power ( q , n , i ) ; int temp = ( X + Y ) % i ; if ( temp == 0 ) { candidate = Math . max ( candidate , i ) ; } temp = ( power ( p , n , num / i ) + power ( q , n , num / i ) ) % ( num / i ) ; if ( temp == 0 ) { candidate = Math . max ( candidate , num / i ) ; } } } return candidate % mod ; } public static void main ( String [ ] args ) { int p , q , n ; p = 10 ; q = 6 ; n = 5 ; System . out . println ( gcd ( p , q , n ) ) ; } }
import java . util . * ; class GFG { static void findPoint ( int n , int h ) { for ( int i = 1 ; i < n ; i ++ ) System . out . printf ( " % .2f ▁ " , Math . sqrt ( i / ( n * 1.0 ) ) * h ) ; } public static void main ( String [ ] args ) { int n = 3 ; int h = 2 ; findPoint ( n , h ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int calculateK ( int x , int y ) { int g = gcd ( x , y ) ; int n = y / g ; int res = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { res -= ( res / i ) ; while ( n % i == 0 ) n /= i ; } } if ( n != 1 ) res -= ( res / n ) ; return res ; } public static void main ( String [ ] args ) { int x = 3 , y = 15 ; System . out . print ( calculateK ( x , y ) + "NEW_LINE"); } }
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 " ) ; } } }
import java . io . * ; class GFG { static void nearestPowerOfTwo ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int lg = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; int a = ( int ) ( Math . pow ( 2 , lg ) ) ; int b = ( int ) ( Math . pow ( 2 , lg + 1 ) ) ; if ( ( arr [ i ] - a ) < ( b - arr [ i ] ) ) System . out . print ( a + " ▁ " ) ; else System . out . print ( b + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 2 , 7 , 12 } ; int N = arr . length ; nearestPowerOfTwo ( arr , N ) ; } }
import java . util . * ; class GFG { static void cntWaysConsArray ( int A [ ] , int N ) { int total = 1 ; int oddArray = 1 ; for ( int i = 0 ; i < N ; i ++ ) { total = total * 3 ; if ( A [ i ] % 2 == 0 ) { oddArray *= 2 ; } } System . out . println ( total - oddArray ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 4 } ; int N = A . length ; cntWaysConsArray ( A , N ) ; } }
import java . util . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int invertBits ( int n ) { int x = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; int m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } static void invertSum ( int A , int B ) { int temp = countSetBits ( A ) + countSetBits ( B ) ; System . out . print ( invertBits ( temp ) ) ; } static public void main ( String args [ ] ) { int A = 5 ; int B = 7 ; invertSum ( A , B ) ; } }
import java . util . Arrays ; class GFG { static void countNumberHavingKthBitSet ( int N , int K ) { int numbers_rightmost_setbit_K = 0 ; for ( int i = 1 ; i <= K ; i ++ ) { int numbers_rightmost_bit_i = ( N + 1 ) / 2 ; N -= numbers_rightmost_bit_i ; if ( i == K ) { numbers_rightmost_setbit_K = numbers_rightmost_bit_i ; } } System . out . println ( numbers_rightmost_setbit_K ) ; } static public void main ( String args [ ] ) { int N = 15 ; int K = 2 ; countNumberHavingKthBitSet ( N , K ) ; } }
import java . util . * ; class GFG { static void XOR_for_every_i ( int A [ ] , int N ) { int frequency_of_bits [ ] = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int M = A [ i ] ; while ( M != 0 ) { if ( ( M & 1 ) != 0 ) { frequency_of_bits [ bit_position ] += 1 ; } bit_position += 1 ; M >>= 1 ; } } for ( int i = 0 ; i < N ; i ++ ) { int M = A [ i ] ; int value_at_that_bit = 1 ; int XOR_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( M & 1 ) != 0 ) { XOR_sum += ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } else { XOR_sum += ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } M >>= 1 ; value_at_that_bit <<= 1 ; } System . out . print ( XOR_sum + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int N = A . length ; XOR_for_every_i ( A , N ) ; } }
import java . util . * ; class GFG { static void minMoves ( int arr [ ] , int N ) { int odd_element_cnt = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 != 0 ) { odd_element_cnt ++ ; } } int moves = ( odd_element_cnt ) / 2 ; if ( odd_element_cnt % 2 != 0 ) moves += 2 ; System . out . print ( moves ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 3 , 7 , 20 } ; int N = arr . length ; minMoves ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minimumMaxDiff ( int [ ] nums ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; for ( int i = 0 ; i < nums . length ; i ++ ) { if ( nums [ i ] % 2 == 0 ) s . add ( nums [ i ] ) ; else s . add ( nums [ i ] * 2 ) ; } int res = s . last ( ) - s . first ( ) ; while ( s . last ( ) % 2 == 0 ) { int x = s . last ( ) ; s . remove ( x ) ; s . add ( x / 2 ) ; res = Math . min ( res , s . last ( ) - s . first ( ) ) ; } return res ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 5 , 9 } ; System . out . print ( minimumMaxDiff ( arr ) ) ; } }
class GFG { static int bitwiseXorRange ( int n ) { if ( n % 4 == 0 ) return n ; if ( n % 4 == 1 ) return 1 ; if ( n % 4 == 2 ) return n + 1 ; return 0 ; } static int evenXorRange ( int l , int r ) { int xor_l ; int xor_r ; xor_r = 2 * bitwiseXorRange ( r / 2 ) ; xor_l = 2 * bitwiseXorRange ( ( l - 1 ) / 2 ) ; return xor_l ^ xor_r ; } public static void main ( String [ ] args ) { int l = 10 ; int r = 20 ; System . out . print ( evenXorRange ( l , r ) ) ; } }
import java . util . * ; class GFG { static int M = 3 ; static int N = 3 ; static void countDigit ( int arr [ ] [ ] ) { for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int X = arr [ i ] [ j ] ; int d = ( int ) ( Math . floor ( Math . log10 ( X ) * 1.0 ) + 1 ) ; System . out . print ( d + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 27 , 173 , 5 } , { 21 , 6 , 624 } , { 5 , 321 , 49 } } ; countDigit ( arr ) ; } }
import java . util . * ; class GFG { static void printPermutation ( int arr [ ] , int n , int k ) { boolean flag = false ; Arrays . sort ( arr ) ; do { int sum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] & arr [ i + 1 ] ; } if ( sum >= k ) { flag = true ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . print ( "NEW_LINE"); } } while ( next_permutation ( arr ) ) ; if ( flag == false ) { System . out . print ( " - 1" ) ; } } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 8 ; int N = arr . length ; printPermutation ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void findMissingNumbers ( int [ ] arr , int len ) { int M = 15 ; BitSet bset = new BitSet ( M ) ; for ( int i = 0 ; i < M ; i ++ ) { bset . set ( i ) ; } for ( int i = 0 ; i < len ; i ++ ) { bset . set ( arr [ i ] - 1 , false ) ; } for ( int i = bset . nextSetBit ( 0 ) ; i >= 0 ; i = bset . nextSetBit ( i + 1 ) ) { if ( i + 1 > len ) break ; System . out . println ( i + 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 4 , 6 , 8 , 9 } ; int n = arr . length ; findMissingNumbers ( arr , n ) ; } }
import java . util . * ; class GFG { static void findSum ( int a , int b , int n ) { int sum = 0 ; int x = a ^ b ; if ( n % 3 == 0 ) { sum = ( n / 3 ) * ( a + b + x ) ; } else if ( n % 3 == 1 ) { sum = ( n / 3 ) * ( a + b + x ) + a ; } else { sum = ( n / 3 ) * ( a + b + x ) + a + b ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , N = 8 ; findSum ( a , b , N ) ; } }
import java . util . * ; class GFG { static int LongestLenXORK ( int arr [ ] , int N , int K ) { int prefixXOR = 0 ; int maxLen = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { prefixXOR ^= arr [ i ] ; if ( mp . containsKey ( prefixXOR ^ K ) ) { maxLen = Math . max ( maxLen , ( i - mp . get ( prefixXOR ^ K ) ) ) ; } if ( ! mp . containsKey ( prefixXOR ) ) { mp . put ( prefixXOR , i ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 7 , 2 } ; int N = arr . length ; int K = 1 ; System . out . print ( LongestLenXORK ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static int N = 3 ; static int M = 3 ; static void intializeDiff ( int D [ ] [ ] , int A [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { D [ i ] [ 0 ] = A [ i ] [ 0 ] ; D [ i ] [ M ] = 0 ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) D [ i ] [ j ] = A [ i ] [ j ] - A [ i ] [ j - 1 ] ; } } static void update ( int D [ ] [ ] , int k , int r1 , int c1 , int r2 , int c2 ) { for ( int i = r1 ; i <= r2 ; i ++ ) { D [ i ] [ c1 ] += k ; D [ i ] [ c2 + 1 ] -= k ; } } static void printArray ( int A [ ] [ ] , int D [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( j == 0 ) A [ i ] [ j ] = D [ i ] [ j ] ; else A [ i ] [ j ] = D [ i ] [ j ] + A [ i ] [ j - 1 ] ; System . out . print ( A [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } static void performQueries ( int A [ ] [ ] , Vector < Vector < Integer > > Queries ) { int D [ ] [ ] = new int [ N ] [ M + 1 ] ; intializeDiff ( D , A ) ; int Q = Queries . size ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { update ( D , Queries . get ( i ) . get ( 0 ) , Queries . get ( i ) . get ( 1 ) , Queries . get ( i ) . get ( 2 ) , Queries . get ( i ) . get ( 3 ) , Queries . get ( i ) . get ( 4 ) ) ; } printArray ( A , D ) ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 1 , 0 } , { 4 , - 2 , 2 } } ; Vector < Vector < Integer > > Queries = new Vector < Vector < Integer > > ( ) ; Vector < Integer > list1 = new Vector < Integer > ( ) ; list1 . add ( 2 ) ; list1 . add ( 0 ) ; list1 . add ( 0 ) ; list1 . add ( 1 ) ; list1 . add ( 1 ) ; Vector < Integer > list2 = new Vector < Integer > ( ) ; list2 . add ( - 1 ) ; list2 . add ( 1 ) ; list2 . add ( 0 ) ; list2 . add ( 2 ) ; list2 . add ( 2 ) ; Queries . add ( list1 ) ; Queries . add ( list2 ) ; performQueries ( A , Queries ) ; } }
class GFG { static int [ ] calculateFactorial ( int N ) { int [ ] fact = new int [ N + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } return fact ; } static int cntWaysSelection ( int arr [ ] , int N , int K , int L , int R ) { int cntWays = 0 ; int cntNum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= L && arr [ i ] <= R ) { cntNum ++ ; } } int [ ] fact = calculateFactorial ( cntNum ) ; for ( int i = K ; i <= cntNum ; i ++ ) { cntWays += fact [ cntNum ] / ( fact [ i ] * fact [ cntNum - i ] ) ; } return cntWays ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 4 , 6 , 13 , 5 , 10 } ; int N = arr . length ; int K = 3 ; int L = 4 ; int R = 10 ; System . out . print ( cntWaysSelection ( arr , N , K , L , R ) ) ; } }
import java . util . * ; class GFG { static int TotalAndPair ( int arr [ ] , int N ) { int totalAND = ( 1 << 30 ) - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { totalAND &= arr [ i ] & arr [ j ] ; } } return totalAND ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalAndPair ( arr , N ) ) ; } }
import java . util . Arrays ; class GFG { static void minimumSubsetDifference ( int N ) { int blockOfSize8 = N / 8 ; String str = " ABBABAAB " ; int subsetDifference = 0 ; String partition = " " ; while ( blockOfSize8 -- > 0 ) { partition += str ; } int A [ ] = new int [ N ] ; int B [ ] = new int [ N ] ; int x = 0 , y = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( partition . charAt ( i ) == ' A ' ) { A [ x ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } else { B [ y ++ ] = ( ( i + 1 ) * ( i + 1 ) ) ; } } System . out . println ( subsetDifference ) ; for ( int i = 0 ; i < x ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; System . out . println ( ) ; for ( int i = 0 ; i < y ; i ++ ) System . out . print ( B [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 8 ; minimumSubsetDifference ( N ) ; } }
class GFG { static int smallestDivisorGr1 ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { return i ; } } return N ; } static int findValOfNWithOperat ( int N , int K ) { for ( int i = 1 ; i <= K ; i ++ ) { N += smallestDivisorGr1 ( N ) ; } return N ; } public static void main ( String [ ] args ) { int N = 6 , K = 4 ; System . out . print ( findValOfNWithOperat ( N , K ) ) ; } }
import java . util . * ; class GFG { static int M = 1000000007 ; static int findTripleSum ( int A , int B , int C ) { int sum = 0 ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { int prod = ( ( ( i % M ) * ( j % M ) ) % M * ( k % M ) ) % M ; sum = ( sum + prod ) % M ; } } } return sum ; } public static void main ( String args [ ] ) { int A = 10 ; int B = 100 ; int C = 1000 ; System . out . println ( findTripleSum ( A , B , C ) ) ; } }
import java . util . * ; class GFG { static final int M = 1000000007 ; static long power ( long x , long N ) { long res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } static long modinv ( long X ) { return power ( X , M - 2 ) ; } static long findTripleSum ( long A , long B , long C ) { long MMI = modinv ( 8 ) ; long res = 0 ; res = ( ( ( ( A % M * ( A + 1 ) % M ) % M * ( B % M * ( B + 1 ) % M ) % M ) % M * ( C % M * ( C + 1 ) % M ) % M ) % M * MMI ) % M ; return res ; } public static void main ( String [ ] args ) { long A = 10 ; long B = 100 ; long C = 1000 ; System . out . print ( findTripleSum ( A , B , C ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getMaximum ( int N , int M , int [ ] [ ] mat ) { int global_max = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int row_min = Integer . MAX_VALUE ; for ( int k = 0 ; k < M ; k ++ ) { int m = Math . max ( mat [ i ] [ k ] , mat [ j ] [ k ] ) ; row_min = Math . min ( row_min , m ) ; } global_max = Math . max ( global_max , row_min ) ; } } return global_max ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 5 , 0 , 3 , 1 , 2 } , { 1 , 8 , 9 , 1 , 3 } , { 1 , 2 , 3 , 4 , 5 } , { 9 , 1 , 0 , 3 , 7 } , { 2 , 3 , 0 , 6 , 3 } , { 6 , 4 , 1 , 7 , 0 } } ; int N = 6 , M = 5 ; System . out . println ( getMaximum ( N , M , mat ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void makeTree ( Vector < Integer > tree [ ] , pair edges [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i ] . first ; int v = edges [ i ] . second ; tree [ u ] . add ( v ) ; } } static pair dfs ( int node , Vector < Integer > tree [ ] ) { int mex = 0 ; int size = 1 ; for ( int u : tree [ node ] ) { pair temp = dfs ( u , tree ) ; mex = Math . max ( mex , temp . first ) ; size += temp . second ; } return new pair ( mex + size , size ) ; } public static void main ( String [ ] args ) { int N = 7 ; pair edges [ ] = { new pair ( 1 , 4 ) , new pair ( 1 , 5 ) , new pair ( 5 , 2 ) , new pair ( 5 , 3 ) , new pair ( 4 , 7 ) , new pair ( 7 , 6 ) } ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] tree = new Vector [ N + 1 ] ; for ( int i = 0 ; i < tree . length ; i ++ ) tree [ i ] = new Vector < Integer > ( ) ; makeTree ( tree , edges , N ) ; System . out . print ( ( dfs ( 1 , tree ) . first ) ) ; } }
import java . util . * ; class GFG { static int divisorCount ( int n ) { int x = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == n / i ) x ++ ; else x += 2 ; } } return x ; } static int minvalue_y ( int x ) { int a = divisorCount ( x ) ; int y = x + 1 ; while ( ( a & 1 ) == ( divisorCount ( y ) & 1 ) ) y ++ ; return y ; } public static void main ( String [ ] args ) { int x = 5 ; System . out . println ( minvalue_y ( x ) ) ; } }
import java . util . * ; class GFG { static int minvalue_y ( int x ) { int n = ( int ) Math . sqrt ( x ) ; if ( n * n == x ) return x + 1 ; return ( int ) Math . pow ( n + 1 , 2 ) ; } public static void main ( String [ ] args ) { int x = 5 ; System . out . print ( minvalue_y ( x ) ) ; } }
import java . util . * ; class GFG { static int alternatingSumOfFirst_N ( int N ) { int alternateSum = 0 ; if ( N % 2 == 0 ) { alternateSum = ( - N ) / 2 ; } else { alternateSum = ( N + 1 ) / 2 ; } return alternateSum ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( alternatingSumOfFirst_N ( N ) ) ; } }
import java . util . * ; class GFG { static int minStartValue ( int [ ] nums ) { int minValue = 0 ; int sum = 0 ; for ( int n : nums ) { sum += n ; minValue = Math . min ( minValue , sum ) ; } int startValue = 1 - minValue ; return startValue ; } public static void main ( String [ ] args ) { int [ ] nums = { - 3 , 2 , - 3 , 4 , 2 } ; System . out . println ( minStartValue ( nums ) ) ; } }
import java . util . * ; class GFG { static int countSubArrayRemove ( int [ ] arr , int N ) { HashMap < Integer , Integer > prefix_element_count = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > suffix_element_count = new HashMap < Integer , Integer > ( ) ; int total_sum_of_elements = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { total_sum_of_elements += arr [ i ] ; if ( ! suffix_element_count . containsKey ( arr [ i ] ) ) suffix_element_count . put ( arr [ i ] , 1 ) ; else suffix_element_count . put ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) + 1 ) ; } int prefix_sum = 0 ; int suffix_sum = 0 ; int count_subarray_equal_sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { prefix_sum += arr [ i ] ; if ( ! prefix_element_count . containsKey ( arr [ i ] ) ) prefix_element_count . put ( arr [ i ] , 1 ) ; else prefix_element_count . put ( arr [ i ] , prefix_element_count . get ( arr [ i ] ) + 1 ) ; suffix_sum = total_sum_of_elements - prefix_sum ; if ( ! suffix_element_count . containsKey ( arr [ i ] ) ) suffix_element_count . put ( arr [ i ] , 0 ) ; else suffix_element_count . put ( arr [ i ] , suffix_element_count . get ( arr [ i ] ) - 1 ) ; int difference = prefix_sum - suffix_sum ; int number_of_subarray_at_i_split = 0 ; if ( prefix_element_count . containsKey ( difference ) ) number_of_subarray_at_i_split = prefix_element_count . get ( difference ) ; if ( suffix_element_count . containsKey ( - difference ) ) number_of_subarray_at_i_split += suffix_element_count . get ( - difference ) ; count_subarray_equal_sum += number_of_subarray_at_i_split ; } return count_subarray_equal_sum ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 1 , 1 , 3 , 1 } ; int N = arr . length ; System . out . println ( countSubArrayRemove ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int checkPerfectSquare ( int N ) { if ( N <= 0 ) { return 0 ; } double sq = Math . sqrt ( N ) ; if ( Math . floor ( sq ) == Math . ceil ( sq ) ) { return 1 ; } return 0 ; } static boolean checktwoSidesareRighTriangle ( int A , int B ) { boolean checkTriangle = false ; if ( checkPerfectSquare ( A * A + B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( A * A - B * B ) != 0 ) { checkTriangle = true ; } if ( checkPerfectSquare ( B * B - A * A ) != 0 ) { checkTriangle = true ; } return checkTriangle ; } public static void main ( String [ ] args ) { int A = 3 , B = 4 ; if ( checktwoSidesareRighTriangle ( A , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static int array_sum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int countOperations ( int arr [ ] , int N ) { int count_zeros = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) count_zeros ++ ; } int sum = array_sum ( arr , N ) ; if ( count_zeros != 0 ) return count_zeros ; if ( sum == 0 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 1 , 0 , 0 } ; int N = arr . length ; System . out . print ( countOperations ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int TotalBitwiseORPair ( int arr [ ] , int N ) { int totalOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalOR |= arr [ i ] ; } return totalOR ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 12 , 15 } ; int N = arr . length ; System . out . print ( TotalBitwiseORPair ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void checkSolveable ( int n , int m ) { if ( n == 1 m == 1 ) System . out . print ( " YES " ) ; else if ( m == 2 && n == 2 ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int n = 1 , m = 3 ; checkSolveable ( n , m ) ; } }
import java . util . * ; class GFG { static String checkRearrangements ( int [ ] [ ] mat , int N , int M ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ 0 ] != mat [ i ] [ j ] ) { return " Yes " ; } } } return " No " ; } static String nonZeroXor ( int [ ] [ ] mat , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { res = res ^ mat [ i ] [ 0 ] ; } if ( res != 0 ) return " Yes " ; else return checkRearrangements ( mat , N , M ) ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 1 , 2 } , { 2 , 2 , 2 } , { 3 , 3 , 3 } } ; int N = mat . length ; int M = mat [ 0 ] . length ; System . out . print ( nonZeroXor ( mat , N , M ) ) ; } }
import java . util . * ; class GFG { static void findDifference ( int [ ] p , int [ ] q , int N ) { int [ ] A = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; boolean IsCorrect ; int a = 1 , b = 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != p [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; a ++ ; } while ( next_permutation ( A ) ) ; for ( int i = 0 ; i < N ; i ++ ) A [ i ] = i + 1 ; do { IsCorrect = true ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != q [ i ] ) { IsCorrect = false ; break ; } } if ( IsCorrect ) break ; b ++ ; } while ( next_permutation ( A ) ) ; System . out . print ( Math . abs ( a - b ) + "NEW_LINE"); } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) if ( p [ a ] < p [ a + 1 ] ) for ( int b = p . length - 1 ; ; -- b ) if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } return false ; } public static void main ( String [ ] args ) { int [ ] p = { 1 , 3 , 2 } ; int [ ] q = { 3 , 1 , 2 } ; int n = p . length ; findDifference ( p , q , n ) ; } }
import java . util . * ; class GFG { static void printFinalArray ( int [ ] a , int n , int [ ] operations , int p , int capacity ) { int i , curr = 0 ; boolean picked = false ; for ( i = 0 ; i < p ; i ++ ) { int s = operations [ i ] ; boolean flag = false ; switch ( s ) { case 1 : if ( curr != 0 ) curr -- ; break ; case 2 : if ( curr != n - 1 ) curr ++ ; break ; case 3 : if ( picked == false && a [ curr ] != 0 ) { picked = true ; a [ curr ] -- ; } break ; case 4 : if ( picked == true && a [ curr ] != capacity ) { picked = false ; a [ curr ] ++ ; } break ; default : flag = true ; } if ( flag == true ) break ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int capacity = 5 ; int a [ ] = { 2 , 5 , 2 } ; int N = a . length ; int operations [ ] = { 3 , 2 , 4 , 1 , 4 , 5 } ; int M = operations . length ; printFinalArray ( a , N , operations , M , capacity ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void CheckSumPrime ( int [ ] A , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { String s = Integer . toString ( A [ i ] ) ; sum += s . length ( ) ; } if ( isPrime ( sum ) == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { 1 , 11 , 12 } ; int N = A . length ; CheckSumPrime ( A , N ) ; } }
import java . util . * ; class GFG { static int power ( int x , int N ) { int res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) ; } x = ( x * x ) ; N = N >> 1 ; } return res ; } static void probablityPrimeprod ( int N ) { int N_E = 3 * N ; int N_S = power ( 6 , N ) ; System . out . print ( N_E + " ▁ / ▁ " + N_S ) ; } public static void main ( String [ ] args ) { int N = 2 ; probablityPrimeprod ( N ) ; } }
import java . util . * ; import java . lang . Math ; class GFG { static boolean checkPalindrome ( int N ) { int rev = 0 ; int temp = N ; while ( N != 0 ) { rev = rev * 10 + N % 10 ; N = N / 10 ; } N = temp ; if ( N == rev ) { return true ; } return false ; } static int closestSmallerPalindrome ( int N ) { do { N -- ; } while ( N >= 0 && ! checkPalindrome ( N ) ) ; return N ; } public static void main ( String [ ] args ) { int N = 4000 ; System . out . println ( closestSmallerPalindrome ( N ) ) ; } }
import java . util . * ; class GFG { static final int M = 1000000007 ; static long power ( long x , long N ) { long res = 1 ; while ( N > 0 ) { if ( N % 2 == 1 ) { res = ( res * x ) % M ; } x = ( x * x ) % M ; N = N >> 1 ; } return res ; } static long moduloInverse ( long X ) { return power ( X , M - 2 ) ; } static long F_1 ( long N , long F_N ) { long P_2 = power ( 2 , N - 1 ) ; long modInv = moduloInverse ( P_2 ) ; long res ; res = ( ( modInv % M ) * ( F_N % M ) ) % M ; return res ; } public static void main ( String [ ] args ) { long N = 3 ; long F_N = 6 ; System . out . print ( F_1 ( N , F_N ) ) ; } }
import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; else return GCD ( b , a % b ) ; } static void check ( int x , int y ) { if ( GCD ( x , y ) == 1 ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int X = 2 , Y = 7 ; check ( X , Y ) ; } }
import java . util . * ; class GFG { static Vector < Integer > arr = new Vector < Integer > ( ) ; static int effect ; static void add ( int x ) { arr . add ( x ) ; } static void update ( int x ) { effect = effect ^ x ; } static void computeResults ( ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { arr . set ( i , arr . get ( i ) ^ effect ) ; System . out . print ( arr . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { arr = new Vector < Integer > ( ) ; arr . add ( 0 ) ; effect = 0 ; add ( 5 ) ; update ( 2 ) ; computeResults ( ) ; } }
import java . util . * ; import java . awt . Point ; class GFG { public static void findSubArray ( Vector < Integer > arr , int k ) { Point ans = new Point ( 0 , 0 ) ; int i , sum = 0 ; for ( i = 0 ; i < k ; i ++ ) { sum += arr . get ( i ) ; } int found = 0 ; if ( sum % 3 == 0 ) { ans = new Point ( 0 , i - 1 ) ; found = 1 ; } for ( int j = i ; j < arr . size ( ) ; j ++ ) { if ( found == 1 ) break ; sum = sum + arr . get ( j ) - arr . get ( j - k ) ; if ( sum % 3 == 0 ) { ans = new Point ( j - k + 1 , j ) ; found = 1 ; } } if ( found == 0 ) ans = new Point ( - 1 , 0 ) ; if ( ans . x == - 1 ) { System . out . print ( - 1 ) ; } else { for ( i = ans . x ; i <= ans . y ; i ++ ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 84 ) ; arr . add ( 23 ) ; arr . add ( 45 ) ; arr . add ( 12 ) ; arr . add ( 56 ) ; arr . add ( 82 ) ; int K = 3 ; findSubArray ( arr , K ) ; } }
import java . util . * ; class GFG { static void calculateFact ( int fact [ ] , int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = fact [ i - 1 ] * i ; } } static int nCr ( int fact [ ] , int N , int R ) { if ( R > N ) return 0 ; int res = fact [ N ] / fact [ R ] ; res /= fact [ N - R ] ; return res ; } static int countWays ( Vector < Integer > arr , int fact [ ] ) { int N = arr . size ( ) ; if ( N <= 2 ) { return 1 ; } Vector < Integer > leftSubTree = new Vector < Integer > ( ) ; Vector < Integer > rightSubTree = new Vector < Integer > ( ) ; int root = arr . get ( 0 ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr . get ( i ) < root ) { leftSubTree . add ( arr . get ( i ) ) ; } else { rightSubTree . add ( arr . get ( i ) ) ; } } int N1 = leftSubTree . size ( ) ; int N2 = rightSubTree . size ( ) ; int countLeft = countWays ( leftSubTree , fact ) ; int countRight = countWays ( rightSubTree , fact ) ; return nCr ( fact , N - 1 , N1 ) * countLeft * countRight ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 4 , 5 , 1 , 2 } ; Vector < Integer > arr = new Vector < Integer > ( ) ; for ( int i : a ) arr . add ( i ) ; int N = a . length ; int [ ] fact = new int [ N ] ; calculateFact ( fact , N ) ; System . out . print ( countWays ( arr , fact ) ) ; } }
import java . util . * ; class GFG { static int sumOfProd ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } return sum * sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 } ; int N = arr . length ; System . out . print ( sumOfProd ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int rev ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static boolean check ( int n ) { return 2 * rev ( n ) == n + 1 ; } public static void main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isPower ( int x , int y ) { double res1 = Math . log ( y ) / Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } static boolean check ( int n ) { int x = ( n + 7 ) / 8 ; if ( ( n + 7 ) % 8 == 0 && isPower ( 10 , x ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 73 ; if ( check ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static double mod = 1e9 + 7 ; static void NthTerm ( int F [ ] , int C [ ] , int K , int n ) { int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( int i = K ; i <= n ; i ++ ) { for ( int j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } System . out . println ( ans [ n ] ) ; } public static void main ( String [ ] args ) { int F [ ] = { 0 , 1 } ; int C [ ] = { 1 , 1 } ; int K = 2 ; int N = 10 ; NthTerm ( F , C , K , N ) ; } }
import java . util . * ; class GFG { static int countFreq ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp1 = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp1 . containsKey ( arr [ i ] ) ) { mp1 . put ( arr [ i ] , mp1 . get ( arr [ i ] ) + 1 ) ; } else { mp1 . put ( arr [ i ] , 1 ) ; } } HashMap < Integer , Integer > mp2 = new HashMap < Integer , Integer > ( ) ; for ( Map . Entry < Integer , Integer > it : mp1 . entrySet ( ) ) { if ( mp2 . containsKey ( it . getValue ( ) ) ) { mp2 . put ( it . getValue ( ) , mp2 . get ( it . getValue ( ) ) + 1 ) ; } else { mp2 . put ( it . getValue ( ) , 1 ) ; } } int M = Integer . MIN_VALUE ; for ( Map . Entry < Integer , Integer > it : mp2 . entrySet ( ) ) { M = Math . max ( M , it . getValue ( ) ) ; } for ( Map . Entry < Integer , Integer > it : mp2 . entrySet ( ) ) { if ( M == it . getValue ( ) ) { return it . getKey ( ) ; } } return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 3 , 10 , 8 , 3 , 6 , 4 } ; int n = arr . length ; System . out . print ( countFreq ( arr , n ) ) ; } }
class GFG { static boolean checkPalindromeB ( int N , int B ) { int rev = 0 ; int N1 = N ; while ( N1 > 0 ) { rev = rev * B + N1 % B ; N1 = N1 / B ; } return N == rev ; } public static void main ( String [ ] args ) { int N = 5 , B = 2 ; if ( checkPalindromeB ( N , B ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findProduct ( int N ) { int result = 1 ; for ( int i = 2 ; i < N ; i ++ ) { if ( gcd ( i , N ) == 1 ) { result *= i ; } } return result ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( findProduct ( N ) ) ; } }
import java . io . * ; class GFG { static int countEqual ( int n ) { return ( n + 1 ) / 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( countEqual ( n ) ) ; } }
import java . util . * ; class GFG { public static int countPairs ( int A [ ] , int B [ ] , int N , int M , int [ ] [ ] List ) { int count = 0 ; int par [ ] = new int [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) par [ i ] = i ; for ( int i = 0 ; i < M ; i ++ ) { int index1 = find ( par , List [ i ] [ 0 ] - 1 ) ; int index2 = find ( par , List [ i ] [ 1 ] - 1 ) ; if ( index1 != index2 ) { union ( par , index1 , index2 ) ; } } HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { map . put ( A [ i ] , i ) ; } for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] != B [ i ] ) { if ( ! map . containsKey ( B [ i ] ) ) { count ++ ; continue ; } int j = map . get ( B [ i ] ) ; if ( find ( par , i ) != find ( par , j ) ) count ++ ; } } return count ; } public static int find ( int par [ ] , int x ) { if ( par [ x ] == x ) return x ; else return par [ x ] = find ( par , par [ x ] ) ; } public static void union ( int par [ ] , int a , int b ) { a = find ( par , a ) ; b = find ( par , b ) ; if ( a == b ) return ; par [ a ] = b ; } public static void main ( String [ ] args ) { int N = 5 ; int M = 4 ; int A [ ] = { 1 , 5 , 9 , 2 , 3 } ; int B [ ] = { 2 , 4 , 5 , 1 , 3 } ; int List [ ] [ ] = { { 1 , 4 } , { 2 , 3 } , { 3 , 5 } , { 2 , 5 } } ; System . out . println ( countPairs ( A , B , N , M , List ) ) ; } }
import java . util . Scanner ; class GFG { static void max_valid_triplet ( int A [ ] , int n ) { int ans = - 1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = arr . length ; max_valid_triplet ( arr , n ) ; } }
import java . util . * ; class GFG { static final int mod = 1000000007 ; static long [ ] ans = new long [ 100002 ] ; static void preCompute ( ) { for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } } static long productOfProperDivi ( int num ) { return ans [ num ] ; } public static void main ( String [ ] args ) { Arrays . fill ( ans , 1 ) ; preCompute ( ) ; int queries = 5 ; int [ ] a = { 4 , 6 , 8 , 16 , 36 } ; for ( int i = 0 ; i < queries ; i ++ ) { System . out . print ( productOfProperDivi ( a [ i ] ) + " , ▁ " ) ; } } }
import java . util . * ; class GFG { static int findlcm ( int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( i * ans ) ) / ( __gcd ( i , ans ) ) ) ; return ans ; } static int highestPower ( int n ) { int lcm = findlcm ( n ) ; int ans = 0 ; for ( int i = 1 ; ; i ++ ) { int x = ( int ) Math . pow ( 2 , i ) ; if ( lcm % x == 0 ) { ans = i ; } if ( x > n ) break ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( highestPower ( n ) ) ; } }
import java . util . * ; class GFG { static void checkFactors ( long N ) { long ev_count = 0 , od_count = 0 ; for ( long i = 1 ; i <= Math . sqrt ( N ) + 1 ; i ++ ) { if ( N % i == 0 ) { if ( i == N / i ) { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } else { if ( i % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; if ( ( N / i ) % 2 == 0 ) ev_count += 1 ; else od_count += 1 ; } } } if ( ev_count % 2 == 0 && od_count % 2 == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } public static void main ( String [ ] args ) { long N = 36 ; checkFactors ( N ) ; } }
class GFG { static int countofPermutations ( int N ) { return ( int ) ( 3 * Math . pow ( 2 , N - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( countofPermutations ( N ) ) ; } }
import java . io . * ; class GFG { static void answer ( int L , int R ) { if ( 2 * L <= R ) System . out . println ( L + " , ▁ " + ( 2 * L ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int L = 3 , R = 8 ; answer ( L , R ) ; } }
class GFG { static int gcd ( int x , int y ) { if ( x % y == 0 ) return y ; else return gcd ( y , x % y ) ; } static int countPairwiseCoprime ( int N , int M ) { int answer = 1 ; int g = gcd ( N , M ) ; int temp = g ; for ( int i = 2 ; i * i <= g ; i ++ ) { if ( temp % i == 0 ) { answer ++ ; while ( temp % i == 0 ) temp /= i ; } } if ( temp != 1 ) answer ++ ; return answer ; } static void countCoprimePair ( int arr [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( countPairwiseCoprime ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 12 , 18 } , { 420 , 660 } } ; int N = arr . length ; countCoprimePair ( arr , N ) ; } }
import java . util . * ; class GFG { static int getProduct ( int a [ ] , int n ) { int p = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { p *= Math . abs ( a [ i ] - a [ j ] ) ; } } return p ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; System . out . println ( getProduct ( arr , N ) ) ; } }
class GFG { static int totalCountDifference ( int n ) { if ( n == 1 ) return 1 ; return n + totalCountDifference ( n / 2 ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( totalCountDifference ( N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left + 1 ; int d = arr [ 1 ] / arr [ 0 ] ; int ans = arr [ left - 1 ] ; if ( d == 1 ) ans = ans * d * k ; else ans = ans * ( ( int ) Math . pow ( d , k ) - 1 / ( d - 1 ) ) ; return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 } ; int queries = 3 ; int [ ] [ ] q = { { 2 , 4 } , { 2 , 6 } , { 5 , 8 } } ; int n = arr . length ; for ( int i = 0 ; i < queries ; i ++ ) System . out . println ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ; } }
import java . util . * ; class GFG { static int nCr ( int n , int r ) { int res = 1 ; if ( r > n - r ) r = n - r ; for ( int i = 0 ; i < r ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int solve ( int n , int m , int k ) { int sum = 0 ; for ( int i = 0 ; i <= k ; i ++ ) sum += nCr ( n , i ) * nCr ( m , k - i ) ; return sum ; } public static void main ( String [ ] args ) { int n = 3 , m = 2 , k = 2 ; System . out . print ( solve ( n , m , k ) ) ; } }
import java . util . * ; class GFG { static final int size = 1000001 ; static void seiveOfEratosthenes ( int [ ] prime ) { prime [ 0 ] = 1 ; prime [ 1 ] = 0 ; for ( int i = 2 ; i * i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = i * i ; j < 1000001 ; j += i ) { prime [ j ] = 1 ; } } } } static float probabiltyEuler ( int [ ] prime , int L , int R , int M ) { int [ ] arr = new int [ size ] ; int [ ] eulerTotient = new int [ size ] ; int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { eulerTotient [ i - L ] = i ; arr [ i - L ] = i ; } for ( int i = 2 ; i < 1000001 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = ( L / i ) * i ; j <= R ; j += i ) { if ( j - L >= 0 ) { eulerTotient [ j - L ] = eulerTotient [ j - L ] / i * ( i - 1 ) ; while ( arr [ j - L ] % i == 0 ) { arr [ j - L ] /= i ; } } } } } for ( int i = L ; i <= R ; i ++ ) { if ( arr [ i - L ] > 1 ) { eulerTotient [ i - L ] = ( eulerTotient [ i - L ] / arr [ i - L ] ) * ( arr [ i - L ] - 1 ) ; } } for ( int i = L ; i <= R ; i ++ ) { if ( ( eulerTotient [ i - L ] % M ) == 0 ) { count ++ ; } } return ( float ) ( 1.0 * count / ( R + 1 - L ) ) ; } public static void main ( String [ ] args ) { int [ ] prime = new int [ size ] ; seiveOfEratosthenes ( prime ) ; int L = 1 , R = 7 , M = 3 ; System . out . print ( probabiltyEuler ( prime , L , R , M ) ) ; } }
class GFG { static long modPower ( long a , long b , long M ) { long res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = res * a % M ; a = a * a % M ; b >>= 1 ; } return res ; } static void findFirstAndLastM ( long N , long K , long M ) { long lastM = modPower ( N , K , ( 1L ) * ( long ) Math . pow ( 10 , M ) ) ; long firstM ; double y = ( double ) K * Math . log10 ( N * 1.0 ) ; y = y - ( long ) y ; double temp = Math . pow ( 10.0 , y ) ; firstM = ( long ) ( temp * ( 1L ) * Math . pow ( 10 , ( M - 1 ) ) ) ; System . out . print ( firstM + " ▁ " + lastM + "NEW_LINE"); } public static void main ( String [ ] args ) { long N = 12 , K = 12 , M = 4 ; findFirstAndLastM ( N , K , M ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int arr [ ] , int n ) { int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; return ans ; } static void minimumSteps ( int arr [ ] , int n ) { int i ; int dat [ ] = new int [ n + 1 ] ; for ( i = 1 ; i <= n ; i ++ ) dat [ arr [ i - 1 ] ] = i ; int b [ ] = new int [ n + 1 ] ; int j = 0 , c ; for ( i = 1 ; i <= n ; i ++ ) { c = 1 ; j = dat [ i ] ; while ( j != i ) { c ++ ; j = dat [ j ] ; } b [ i ] = c ; } System . out . println ( findlcm ( b , n ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 4 , 3 , 2 , 7 , 6 } ; int N = arr . length ; minimumSteps ( arr , N ) ; } }
class GFG { static double maximum ( double a , double b ) { if ( a > b ) return a ; return b ; } static double maxSubarrayRatio ( double arr [ ] , int n ) { double maxRatio = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { double ratio = arr [ i ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { ratio = ratio / arr [ k ] ; } maxRatio = maximum ( maxRatio , ratio ) ; } } return maxRatio ; } public static void main ( String [ ] args ) { double arr [ ] = { 2 , 2 , 4 , - 0.2 , - 1 } ; int n = arr . length ; System . out . println ( maxSubarrayRatio ( arr , n ) ) ; } }
class GFG { private static void check ( long num ) { long root = ( long ) Math . sqrt ( num ) ; long pow = ( long ) ( Math . log ( root ) / Math . log ( 2 ) ) ; if ( num == ( long ) ( Math . pow ( 2 , pow ) * ( Math . pow ( 2 , pow + 1 ) - 1 ) ) ) { if ( isPrime ( pow + 1 ) ) { if ( isPrime ( ( long ) Math . pow ( 2 , pow + 1 ) - 1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } else System . out . println ( " No " ) ; } public static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; else if ( n == 2 n == 3 ) return true ; else { if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( long i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; } return true ; } } public static void main ( String args [ ] ) { long num = 6 ; check ( num ) ; } }
import java . util . * ; class GFG { static boolean isTriplet ( int ar [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( Math . abs ( ar [ i ] - ar [ j ] ) == 1 || Math . abs ( ar [ j ] - ar [ k ] ) == 1 || Math . abs ( ar [ i ] - ar [ k ] ) == 1 ) { int x = ar [ i ] * ar [ i ] , y = ar [ j ] * ar [ j ] , z = ar [ k ] * ar [ k ] ; if ( x == y + z y == x + z z == x + y ) return true ; } } } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 4 , 6 , 5 } ; int ar_size = arr . length ; if ( isTriplet ( arr , ar_size ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static void closestDivisors ( int n ) { for ( int i = ( int ) Math . sqrt ( n + 2 ) ; i > 0 ; i -- ) { if ( ( n + 1 ) % i == 0 ) { System . out . print ( i + " , ▁ " + ( n + 1 ) / i ) ; break ; } if ( ( n + 2 ) % i == 0 ) { System . out . print ( i + " , ▁ " + ( n + 2 ) / i ) ; break ; } } } public static void main ( String [ ] args ) { int N = 123 ; closestDivisors ( N ) ; } }
import java . util . Arrays ; class GFG { public static int count ( int n ) { int [ ] phi = new int [ n + 1 ] ; Arrays . fill ( phi , 0 ) ; int [ ] divs = new int [ n + 1 ] ; Arrays . fill ( divs , 0 ) ; for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] += i ; for ( int j = i * 2 ; j <= n ; j += i ) phi [ j ] -= phi [ i ] ; for ( int j = i ; j <= n ; j += i ) divs [ j ] ++ ; } return ( n - phi [ n ] - divs [ n ] + 1 ) ; } public static void main ( String [ ] args ) { int N = 42 ; System . out . println ( count ( N ) ) ; } }
class GFG { static int sum_of_differences ( int arr [ ] , int N ) { int sum = 0 ; int i , j , flag ; for ( i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] ) { flag = 0 ; for ( j = i + 1 ; j < N - 1 ; j ++ ) { if ( arr [ j ] >= arr [ j + 1 ] ) { sum += ( arr [ j ] - arr [ i ] ) ; i = j ; flag = 1 ; break ; } } if ( flag == 0 && arr [ i ] < arr [ N - 1 ] ) { sum += ( arr [ N - 1 ] - arr [ i ] ) ; break ; } } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 1 , 2 , 5 , 3 , 4 } ; int N = arr . length ; System . out . print ( sum_of_differences ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int MinimumString ( int x ) { int b = 1 ; double left_lim = Math . sqrt ( x ) + 1.0 ; double right_lim = Math . sqrt ( x ) + 2.0 ; int r = 0 ; for ( int i = ( int ) left_lim ; i <= right_lim ; i ++ ) { if ( i > left_lim && i < right_lim ) { r = i ; break ; } } return b + r ; } public static void main ( String [ ] args ) { int X = 2 ; System . out . print ( MinimumString ( X ) ) ; } }
import java . util . * ; class GFG { static boolean isPerfectSquare ( int N ) { int floorSqrt = ( int ) Math . sqrt ( N ) ; return ( N == floorSqrt * floorSqrt ) ; } static boolean legendreFunction ( int N ) { while ( N % 4 == 0 ) N /= 4 ; if ( N % 8 != 7 ) return true ; else return false ; } static int minSquares ( int N ) { if ( isPerfectSquare ( N ) ) return 1 ; for ( int i = 1 ; i * i < N ; i ++ ) { if ( isPerfectSquare ( N - i * i ) ) return 2 ; } if ( legendreFunction ( N ) ) return 3 ; return 4 ; } public static void main ( String [ ] args ) { int N = 123 ; System . out . print ( minSquares ( N ) ) ; } }
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 " ) ; } }
class MultiPart { static int getDivisors ( int min , int n ) { int total = 0 ; for ( int i = min ; i < n ; ++ i ) if ( n % i == 0 && n / i >= i ) { ++ total ; if ( n / i > i ) total += getDivisors ( i , n / i ) ; } return total ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( 1 + getDivisors ( 2 , n ) ) ; } }
class GFG { static class Node { int val ; Node next ; Node ( int _val ) { val = _val ; next = null ; } } ; static void getLastTwoPerson ( int n ) { int total = n ; Node head = new Node ( 1 ) ; Node temp = head ; for ( int i = 2 ; i <= n ; i ++ ) { temp . next = new Node ( i ) ; temp = temp . next ; } temp . next = head ; temp = head ; Node del ; while ( total != 2 ) { del = temp . next . next ; temp . next . next = temp . next . next . next ; temp = temp . next ; del = null ; System . gc ( ) ; total -= 1 ; } System . out . print ( temp . val + " ▁ " + temp . next . val ) ; } public static void main ( String [ ] args ) { int n = 2 ; getLastTwoPerson ( n ) ; } }
import java . io . * ; class GFG { static int findMinDenomin ( int n ) { return ( ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findMinDenomin ( n ) ) ; } }
import java . util . * ; class GFG { static int findMaximumGcd ( int n ) { int max_gcd = 1 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i > max_gcd ) max_gcd = i ; if ( ( n / i != i ) && ( n / i != n ) && ( ( n / i ) > max_gcd ) ) max_gcd = n / i ; } } return max_gcd ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( findMaximumGcd ( N ) ) ; } }
class GFG { static int findContent ( int arr [ ] , int n ) { int content = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { content = __gcd ( content , arr [ i ] ) ; } return content ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] = { 9 , 6 , 12 } ; System . out . print ( findContent ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void linearCongruentialMethod ( int Xo , int m , int a , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( ( randomNums [ i - 1 ] * a ) + c ) % m ; } } public static void main ( String [ ] args ) { int Xo = 5 ; int m = 7 ; int a = 3 ; int c = 3 ; int noOfRandomNums = 10 ; int [ ] randomNums = new int [ noOfRandomNums ] ; linearCongruentialMethod ( Xo , m , a , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }
import java . util . * ; class GFG { static void multiplicativeCongruentialMethod ( int Xo , int m , int a , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] * a ) % m ; } } public static void main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int a = 7 ; int noOfRandomNums = 10 ; int [ ] randomNums = new int [ noOfRandomNums ] ; multiplicativeCongruentialMethod ( Xo , m , a , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int power ( int a , int b , int m ) { a %= m ; int res = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( ( res % m ) * ( a % m ) ) % m ; a = ( ( a % m ) * ( a % m ) ) % m ; b >>= 1 ; } return res % m ; } static int productOfDivisors ( int p [ ] , int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . containsKey ( p [ i ] ) ) prime . put ( p [ i ] , prime . get ( p [ i ] ) + 1 ) ; else prime . put ( p [ i ] , 1 ) ; } int product = 1 , d = 1 ; for ( Map . Entry < Integer , Integer > itr : prime . entrySet ( ) ) { int val = power ( itr . getKey ( ) , ( itr . getValue ( ) ) * ( itr . getValue ( ) + 1 ) / 2 , MOD ) ; product = ( power ( product , itr . getValue ( ) + 1 , MOD ) * power ( val , d , MOD ) ) % MOD ; d = ( d * ( itr . getValue ( ) + 1 ) ) % ( MOD - 1 ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 11 } ; int n = arr . length ; System . out . println ( productOfDivisors ( arr , n ) ) ; } }
class GFG { static void printFractions ( int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = i + 1 ; j <= n ; j ++ ) { if ( __gcd ( i , j ) == 1 ) { String a = String . valueOf ( i ) ; String b = String . valueOf ( j ) ; System . out . print ( a + " / " + b + " , ▁ " ) ; } } } } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 3 ; printFractions ( n ) ; } }
class GFG { static int numberOfObjects ( int N , int M ) { int initial = Math . min ( N , M ) ; int last = ( N + M ) / 3 ; return Math . min ( initial , last ) ; } public static void main ( String [ ] args ) { int N = 8 ; int M = 7 ; System . out . println ( numberOfObjects ( N , M ) ) ; } }
import java . util . * ; class GFG { public static void findLocalMaximaMinima ( int n , int [ ] arr ) { Vector < Integer > mx = new Vector < Integer > ( ) ; Vector < Integer > mn = new Vector < Integer > ( ) ; if ( arr [ 0 ] > arr [ 1 ] ) mx . add ( 0 ) ; else if ( arr [ 0 ] < arr [ 1 ] ) mn . add ( 0 ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] > arr [ i ] ) && ( arr [ i ] < arr [ i + 1 ] ) ) mn . add ( i ) ; else if ( ( arr [ i - 1 ] < arr [ i ] ) && ( arr [ i ] > arr [ i + 1 ] ) ) mx . add ( i ) ; } if ( arr [ n - 1 ] > arr [ n - 2 ] ) mx . add ( n - 1 ) ; else if ( arr [ n - 1 ] < arr [ n - 2 ] ) mn . add ( n - 1 ) ; if ( mx . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " maxima ▁ are ▁ : ▁ " ) ; for ( Integer a : mx ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ " + " of ▁ Local ▁ Maxima ▁ " ) ; if ( mn . size ( ) > 0 ) { System . out . print ( " Points ▁ of ▁ Local ▁ " + " minima ▁ are ▁ : ▁ " ) ; for ( Integer a : mn ) System . out . print ( a + " ▁ " ) ; System . out . println ( ) ; } else System . out . println ( " There ▁ are ▁ no ▁ points ▁ of ▁ " + " Local ▁ Maxima ▁ " ) ; } public static void main ( String [ ] args ) { int N = 9 ; int arr [ ] = { 10 , 20 , 15 , 14 , 13 , 25 , 5 , 4 , 3 } ; findLocalMaximaMinima ( N , arr ) ; } }
import java . util . * ; class GFG { static int power ( int a , int b ) { if ( b == 0 ) return 1 ; if ( b == 1 ) return a ; int tmp = power ( a , b / 2 ) ; int result = tmp * tmp ; if ( b % 2 == 1 ) result *= a ; return result ; } static boolean check ( int n ) { int temp = n ; while ( temp > 0 ) { int last_digit = temp % 10 ; if ( last_digit == 0 last_digit == 1 ) return false ; if ( n % last_digit == 0 ) return false ; temp = temp / 10 ; } return true ; } static void solve ( int n ) { int L = power ( 10 , n - 1 ) ; int R = power ( 10 , n ) - 1 ; int flag = 0 ; for ( int i = L ; i <= R ; i ++ ) { boolean answer = check ( i ) ; if ( answer == true ) { System . out . println ( i ) ; flag ++ ; break ; } } if ( flag == 0 ) System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 4 ; solve ( N ) ; } }
import java . util . * ; class GFG { public static void costOfSubsequence ( int N , int [ ] arr , int [ ] costArray ) { int i , temp ; int cost = 0 ; Set < Integer > removedElements = new HashSet < Integer > ( ) ; int ans = 0 ; for ( i = 1 ; i < ( N - 1 ) ; i ++ ) { temp = arr [ i ] ; if ( ( ( arr [ i - 1 ] < temp ) && ( temp < arr [ i + 1 ] ) ) || ( ( arr [ i - 1 ] > temp ) && ( temp > arr [ i + 1 ] ) ) ) { removedElements . add ( temp ) ; } } for ( i = 0 ; i < ( N ) ; i ++ ) { temp = arr [ i ] ; if ( ! ( removedElements . contains ( temp ) ) ) { ans += arr [ i ] ; } else { cost += costArray [ i ] ; } } System . out . print ( ans + " , ▁ " ) ; System . out . print ( cost ) ; } public static void main ( String [ ] args ) { int N ; N = 4 ; int [ ] arr = { 1 , 3 , 4 , 2 } ; int [ ] costArray = { 0 , 1 , 0 , 0 } ; costOfSubsequence ( N , arr , costArray ) ; } }
import java . util . * ; class GFG { static int maxN = 100000000 ; static Vector < Integer > prime = new Vector < Integer > ( ) ; static void sieve ( int n ) { boolean [ ] check = new boolean [ 1000007 ] ; for ( int i = 0 ; i < 1000007 ; i ++ ) check [ i ] = false ; int i , j ; check [ 0 ] = false ; check [ 1 ] = true ; check [ 2 ] = false ; for ( i = 4 ; i <= n ; i += 2 ) check [ i ] = true ; for ( i = 3 ; i * i <= n ; i += 2 ) if ( ! check [ i ] ) { for ( j = i * i ; j <= n ; j += 2 * i ) check [ j ] = true ; } prime . add ( 2 ) ; for ( i = 3 ; i <= n ; i += 2 ) if ( ! check [ i ] ) prime . add ( i ) ; return ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int count ( int a [ ] , int n , int m ) { int [ ] parity = new int [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) parity [ i ] = 0 ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { int mult = 1 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) != 0 ) mult *= a [ j ] ; parity [ countSetBits ( i ) & 1 ] += ( m / mult ) ; } return parity [ 1 ] - parity [ 0 ] ; } static int countRelPrime ( int n , int m ) { int [ ] a = new int [ 20 ] ; int i = 0 , j = 0 ; int pz = prime . size ( ) ; while ( n != 1 && i < pz ) { if ( ( int ) prime . get ( i ) * ( int ) prime . get ( i ) > n ) break ; if ( n % prime . get ( i ) == 0 ) { a [ j ] = ( int ) prime . get ( i ) ; j ++ ; } while ( n % prime . get ( i ) == 0 ) n /= prime . get ( i ) ; i ++ ; } if ( n != 1 ) { a [ j ] = n ; j ++ ; } return m - count ( a , j , m ) ; } static void countRelPrimeInRange ( int n , int l , int r ) { sieve ( ( int ) Math . sqrt ( maxN ) ) ; int result = countRelPrime ( n , r ) - countRelPrime ( n , l - 1 ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { int N = 7 , L = 3 , R = 9 ; countRelPrimeInRange ( N , L , R ) ; } }
class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void find_Xor ( int arr [ ] , pair query [ ] , int N , int Q ) { for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i - 1 ] ; } int ans = 0 ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] . first == 0 ) ans = arr [ query [ i ] . second ] ; else ans = arr [ query [ i ] . first - 1 ] ^ arr [ query [ i ] . second ] ; System . out . print ( ans + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 5 , 1 , 1 , 5 , 3 } ; int N = 8 ; int Q = 2 ; pair query [ ] = { new pair ( 1 , 4 ) , new pair ( 3 , 7 ) } ; find_Xor ( arr , query , N , Q ) ; } }
class GFG { static int [ ] DiStirngMatch ( String Str ) { int N = Str . length ( ) ; int [ ] arr = new int [ N + 1 ] ; int START = 0 , END = N ; for ( int i = 0 ; i < N ; i ++ ) { if ( Str . charAt ( i ) == ' I ' ) arr [ i ] = START ++ ; if ( Str . charAt ( i ) == ' D ' ) arr [ i ] = END -- ; } arr [ N ] = START ; return arr ; } public static void main ( String [ ] args ) { String Str = " IDID " ; int N = Str . length ( ) ; int [ ] ptr = DiStirngMatch ( Str ) ; for ( int i = 0 ; i <= N ; i ++ ) System . out . print ( ptr [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static boolean IsPrime ( int i ) { if ( i % 2 == 0 && i != 2 ) return false ; else { for ( int j = 3 ; j <= Math . sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) return false ; } return true ; } } public static void main ( String [ ] args ) { int c = 0 ; int N = 72 ; for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( IsPrime ( i ) ) { if ( N % i == 0 ) { c ++ ; if ( IsPrime ( N / i ) && i != ( N / i ) ) c ++ ; } } } System . out . print ( Math . pow ( 2 , c ) - 1 ) ; } }
class GFG { static void find_seq ( int arr [ ] , int m , int n ) { int b [ ] = new int [ n ] ; int x = 0 ; b [ 0 ] = x ; for ( int i = 0 ; i < n - 1 ; i ++ ) { b [ i + 1 ] = x + arr [ i ] + b [ i ] ; } int mn = n ; for ( int i = 0 ; i < n ; i ++ ) { mn = Math . min ( mn , b [ i ] ) ; } x = 1 - mn ; for ( int i = 0 ; i < n ; i ++ ) { b [ i ] += x ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( b [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = new int [ ] { - 2 , 1 } ; int M = arr . length ; find_seq ( arr , M , N ) ; } }
public class GFG { static void isEqualFactors ( int N ) { if ( ( N % 2 == 0 ) && ( N % 4 != 0 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int N = 10 ; isEqualFactors ( N ) ; N = 125 ; isEqualFactors ( N ) ; } }
import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double largest = Math . pow ( b , n ) - 1 ; System . out . println ( largest ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }
class GFG { public static void maximizedArray ( int arr [ ] , int N ) { int num , i = 0 ; for ( int l = N ; l > 0 ; l -- ) { num = arr [ i ] ; int one = 0 ; int zero = 0 ; while ( num != 0 ) { if ( num % 2 == 0 ) { zero ++ ; } else { one ++ ; } num = num >> 1 ; } for ( int j = zero ; j < ( one + zero ) ; j ++ ) { num += ( 1 << j ) ; } System . out . print ( num ) ; i ++ ; if ( N > 0 ) System . out . print ( " , ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 15 , 9 , 10 , 14 } ; int N = arr . length ; maximizedArray ( arr , N ) ; } }
import java . util . * ; class GFG { static final int x = 2000021 ; static int [ ] v = new int [ x ] ; static void sieve ( ) { v [ 1 ] = 1 ; for ( int i = 2 ; i < x ; i ++ ) v [ i ] = i ; for ( int i = 4 ; i < x ; i += 2 ) v [ i ] = 2 ; for ( int i = 3 ; i * i < x ; i ++ ) { if ( v [ i ] == i ) { for ( int j = i * i ; j < x ; j += i ) { if ( v [ j ] == j ) { v [ j ] = i ; } } } } } static int prime_factors ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; while ( n != 1 ) { s . add ( v [ n ] ) ; n = n / v [ n ] ; } return s . size ( ) ; } static void distinctPrimes ( int m , int k ) { Vector < Integer > result = new Vector < Integer > ( ) ; for ( int i = 14 ; i < m + k ; i ++ ) { long count = prime_factors ( i ) ; if ( count == k ) { result . add ( i ) ; } } int p = result . size ( ) ; for ( int index = 0 ; index < p - 1 ; index ++ ) { long element = result . get ( index ) ; int count = 1 , z = index ; while ( z < p - 1 && count <= k && result . get ( z ) + 1 == result . get ( z + 1 ) ) { count ++ ; z ++ ; } if ( count >= k ) System . out . print ( element + " ▁ " ) ; } } public static void main ( String [ ] args ) { sieve ( ) ; int N = 1000 , K = 3 ; distinctPrimes ( N , K ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findGCD ( int arr [ ] , int n ) { int gcd = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( arr [ i ] , gcd ) ; if ( gcd == 1 ) { return 1 ; } } return gcd ; } static int findLCM ( int arr [ ] , int n ) { int lcm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { lcm = ( ( ( arr [ i ] * lcm ) ) / ( __gcd ( arr [ i ] , lcm ) ) ) ; } return lcm ; } static void findRatio ( int arr [ ] , int n ) { int gcd = findGCD ( arr , n ) ; int lcm = findLCM ( arr , n ) ; System . out . print ( ( lcm / gcd ) ) ; System . out . print ( " : 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 6 , 12 , 36 } ; int N = 3 ; findRatio ( arr , N ) ; } }
import java . util . * ; class GFG { static void pen_hex ( long n ) { long pn = 1 ; for ( long i = 1 ; i < n ; i ++ ) { pn = i * ( 3 * i - 1 ) / 2 ; if ( pn > n ) break ; double seqNum = ( 1 + Math . sqrt ( 8 * pn + 1 ) ) / 4 ; if ( seqNum == ( long ) seqNum ) System . out . print ( pn + " , ▁ " ) ; } } public static void main ( String [ ] args ) { long N = 1000000 ; pen_hex ( N ) ; } }
import java . util . * ; class GFG { public static void sumOfTheArrayForQuery ( int [ ] A , int N , int [ ] X , int [ ] Y , int Q ) { int sum = 0 ; HashMap < Integer , Integer > count = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += A [ i ] ; if ( count . containsKey ( A [ i ] ) ) { count . replace ( A [ i ] , count . get ( A [ i ] ) + 1 ) ; } else { count . put ( A [ i ] , 1 ) ; } } for ( int i = 0 ; i < Q ; i ++ ) { int x = X [ i ] , y = Y [ i ] ; if ( count . containsKey ( X [ i ] ) ) { sum -= count . get ( X [ i ] ) * X [ i ] ; sum += count . get ( X [ i ] ) * Y [ i ] ; } if ( count . containsKey ( Y [ i ] ) && count . containsKey ( X [ i ] ) ) { count . replace ( Y [ i ] , count . get ( Y [ i ] ) + count . get ( X [ i ] ) ) ; } if ( count . containsKey ( X [ i ] ) ) { count . replace ( X [ i ] , 0 ) ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 2 } ; int X [ ] = { 2 , 3 , 5 } ; int Y [ ] = { 3 , 1 , 2 } ; int N = arr . length ; int Q = X . length ; sumOfTheArrayForQuery ( arr , N , X , Y , Q ) ; } }
import java . util . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void IncrementFactors ( int count [ ] , int val ) { for ( int i = 1 ; i * i <= val ; i ++ ) { if ( val % i == 0 ) { if ( i == val / i ) { count [ i ] ++ ; } else { count [ i ] ++ ; count [ val / i ] ++ ; } } } } static void generateArr ( int A [ ] , int n ) { int [ ] B = new int [ n ] ; int maxi = Arrays . stream ( A ) . max ( ) . getAsInt ( ) ; int count [ ] = new int [ maxi + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { B [ i ] = count [ A [ i ] ] ; IncrementFactors ( count , A [ i ] ) ; } printArr ( B , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; generateArr ( arr , n ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static ArrayList < Integer > SieveOfEratosthenes ( int n ) { boolean [ ] prime = new boolean [ 2 * n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= 2 * n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= 2 * n ; i += p ) prime [ i ] = false ; } } ArrayList < Integer > primes = new ArrayList < > ( ) ; for ( int p = 2 ; p <= 2 * n ; p ++ ) if ( prime [ p ] ) primes . add ( p ) ; return primes ; } static int minChanges ( int [ ] arr ) { int n = arr . length ; int ans = 0 ; int maxi = arr [ 0 ] ; for ( int i = 1 ; i < arr . length ; i ++ ) maxi = Math . max ( maxi , arr [ i ] ) ; ArrayList < Integer > primes = SieveOfEratosthenes ( maxi ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = - 1 ; for ( int j = 0 ; j < primes . size ( ) ; j ++ ) { if ( arr [ i ] == primes . get ( j ) ) { x = j ; break ; } else if ( arr [ i ] < primes . get ( j ) ) { x = j ; break ; } } int minm = Math . abs ( primes . get ( x ) - arr [ i ] ) ; if ( x > 1 ) { minm = Math . min ( minm , Math . abs ( primes . get ( x - 1 ) - arr [ i ] ) ) ; } ans += minm ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 25 , 13 , 6 , 20 } ; System . out . println ( minChanges ( arr ) ) ; } }
import java . io . * ; class GFG { static int calcSum ( int k , int n ) { int value = ( k * n * ( n + 1 ) ) / 2 ; return value ; } static int findSum ( int [ ] a , int n , int L , int R ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( L % a [ i ] == 0 && L != 0 ) { sum += calcSum ( a [ i ] , R / a [ i ] ) - calcSum ( a [ i ] , ( L - 1 ) / a [ i ] ) ; } else { sum += calcSum ( a [ i ] , R / a [ i ] ) - calcSum ( a [ i ] , L / a [ i ] ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 3 , 8 } ; int N = arr . length ; int L = 7 ; int R = 20 ; System . out . println ( findSum ( arr , N , L , R ) ) ; } }
class GFG { static int maxN = 1000000 ; static int [ ] arr = new int [ 1000001 ] ; static int [ ] prefix = new int [ 1000001 ] ; static void findPrimes ( ) { for ( int i = 1 ; i <= maxN ; i ++ ) arr [ i ] = 1 ; arr [ 0 ] = 0 ; arr [ 1 ] = 0 ; for ( int i = 2 ; i * i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { for ( int j = i * i ; j <= maxN ; j += i ) { arr [ j ] = 0 ; } } } int sum = 0 ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= maxN ; i ++ ) { if ( arr [ i ] == 1 ) { int temp = i ; sum = 0 ; while ( temp > 0 ) { int x = temp % 10 ; sum += x ; temp = temp / 10 ; if ( arr [ sum ] == 1 ) { prefix [ i ] = 1 ; } else { prefix [ i ] = 0 ; } } } } for ( int i = 1 ; i <= maxN ; i ++ ) { prefix [ i ] += prefix [ i - 1 ] ; } } static void countNumbersInRange ( int l , int r ) { findPrimes ( ) ; int result = prefix [ r ] - prefix [ l - 1 ] ; System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { int l , r ; l = 5 ; r = 20 ; countNumbersInRange ( l , r ) ; } }
class GFG { static void print_product ( int a , int b , int c , int d ) { int prod1 = a * c ; int prod2 = b * d ; int prod3 = ( a + b ) * ( c + d ) ; int real = prod1 - prod2 ; int imag = prod3 - ( prod1 + prod2 ) ; System . out . println ( real + " ▁ + ▁ " + imag + " i " ) ; } public static void main ( String [ ] args ) { int a = 2 ; int b = 3 ; int c = 4 ; int d = 5 ; print_product ( a , b , c , d ) ; } }
class GFG { static double get_concentration ( double mass , double volume ) { if ( volume == 0 ) return - 1 ; else return ( mass / volume ) * 1000 ; } public static void main ( String [ ] args ) { double mass , volume ; mass = 100.00 ; volume = 500.00 ; System . out . println ( get_concentration ( mass , volume ) ) ; } }
import java . util . * ; class GFG { static boolean isSubsetSum ( Vector < Integer > set , int n , int sum ) { boolean [ ] [ ] subset = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set . get ( i - 1 ) ] ; } } return subset [ n ] [ sum ] ; } static void storeDivisors ( int n , Vector < Integer > div ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) div . add ( i ) ; else { div . add ( i ) ; div . add ( n / i ) ; } } } } static boolean isPractical ( int N ) { Vector < Integer > div = new Vector < Integer > ( ) ; storeDivisors ( N , div ) ; for ( int i = 1 ; i < N ; i ++ ) { if ( ! isSubsetSum ( div , div . size ( ) , i ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 18 ; if ( isPractical ( N ) == true ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static boolean isDivBySubsetSums ( Vector < Integer > arr , int num ) { int n = arr . size ( ) ; long total = 1 << n ; for ( long i = 0 ; i < total ; i ++ ) { long sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( i & ( 1 << j ) ) > 0 ) sum += arr . get ( j ) ; if ( sum != 0 && num % sum != 0 ) return false ; } return true ; } static boolean isSuperNivenNum ( int n ) { int temp = n ; Vector < Integer > digits = new Vector < Integer > ( ) ; while ( n != 0 ) { int digit = n % 10 ; digits . add ( digit ) ; n = n / 10 ; } return isDivBySubsetSums ( digits , temp ) ; } public static void main ( String [ ] args ) { int n = 500 ; if ( isSuperNivenNum ( n ) ) System . out . print ( " yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int divCount ( int n ) { boolean [ ] hash = new boolean [ n + 1 ] ; Arrays . fill ( hash , true ) ; for ( int p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == true ) for ( int i = p * 2 ; i < n ; i += p ) hash [ i ] = false ; int total = 1 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] ) { int count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } static boolean isHighlyCompositeNumber ( int N ) { int NdivCount = divCount ( N ) ; for ( int i = 1 ; i < N ; i ++ ) { int idivCount = divCount ( i ) ; if ( idivCount >= NdivCount ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 12 ; if ( isHighlyCompositeNumber ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int PentacontahenagonNum ( int N ) { return ( 49 * N * N - 47 * N ) / 2 ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( "3rd ▁ Pentacontahenagon ▁ Number ▁ is ▁ " + PentacontahenagonNum ( N ) ) ; } }
class GFG { static boolean isInsolite ( int n ) { int N = n ; int sum = 0 ; int product = 1 ; while ( n != 0 ) { int r = n % 10 ; sum = sum + r * r ; product = product * r * r ; n = n / 10 ; } return ( N % sum == 0 ) && ( N % product == 0 ) ; } public static void main ( String [ ] args ) { int N = 111 ; if ( isInsolite ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isSaintExuperyNum ( int n ) { for ( int i = 1 ; i <= n / 3 ; i ++ ) { for ( int j = i + 1 ; j <= n / 2 ; j ++ ) { int k = n / i / j ; if ( i * i + j * j == k * k ) { if ( i * j * k == n ) return true ; } } } return false ; } public static void main ( String [ ] args ) { int N = 60 ; if ( isSaintExuperyNum ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static int sigma ( int n ) { if ( n == 1 ) return 1 ; 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 + n + 1 ) ; } static boolean isSuperabundant ( int N ) { for ( double i = 1 ; i < N ; i ++ ) { double x = sigma ( ( int ) ( i ) ) / i ; double y = sigma ( ( int ) ( N ) ) / ( N * 1.0 ) ; if ( x > y ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 4 ; if ( isSuperabundant ( N ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
class GFG { static boolean isRepdigit ( int num , int b ) { int prev = - 1 ; while ( num != 0 ) { int digit = num % b ; num /= b ; if ( prev != - 1 && digit != prev ) return false ; prev = digit ; } return true ; } public static void main ( String args [ ] ) { int num = 2000 , base1 = 7 ; if ( isRepdigit ( num , base1 ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . util . * ; class GFG { static boolean isDNum ( int n ) { if ( n < 4 ) return false ; int numerator = 0 , hcf = 0 ; for ( int k = 2 ; k <= n ; k ++ ) { numerator = ( int ) ( Math . pow ( k , n - 2 ) - k ) ; hcf = __gcd ( n , k ) ; } if ( hcf == 1 && ( numerator % n ) != 0 ) return false ; return true ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 15 ; boolean a = isDNum ( n ) ; if ( a ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static boolean isComposite ( int n ) { 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 String concatenatePrimeFactors ( int n ) { String concatenate = " " ; if ( n % 2 == 0 ) { concatenate += "2" ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { concatenate += i ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) concatenate += n ; return concatenate ; } static boolean isEnlightened ( int N ) { if ( ! isComposite ( N ) ) return false ; String num = String . valueOf ( N ) ; String prefixConc = concatenatePrimeFactors ( N ) ; return num . startsWith ( prefixConc ) ; } public static void main ( String args [ ] ) { int n = 250 ; if ( isEnlightened ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void findNthTerm ( int n ) { System . out . print ( n * ( 3 * n + 1 ) / 2 + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
import java . lang . * ; class GFG { static boolean isIdoneal ( int n ) { for ( int a = 1 ; a <= n ; a ++ ) { for ( int b = a + 1 ; b <= n ; b ++ ) { for ( int c = b + 1 ; c <= n ; c ++ ) { if ( a * b + b * c + c * a == n ) return false ; } } } return true ; } public static void main ( String [ ] args ) { int N = 10 ; if ( isIdoneal ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean checkDivisibility ( int n , int digit ) { return ( digit != 0 && n % digit == 0 ) ; } static boolean isAllDigitsDivide ( int n ) { int temp = n ; while ( temp > 0 ) { int digit = temp % 10 ; if ( ! ( checkDivisibility ( n , digit ) ) ) return false ; temp /= 10 ; } return true ; } static boolean isAllDigitsDistinct ( int n ) { boolean arr [ ] = new boolean [ 10 ] ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } static boolean isLynchBell ( int n ) { return isAllDigitsDivide ( n ) && isAllDigitsDistinct ( n ) ; } public static void main ( String [ ] args ) { int N = 12 ; if ( isLynchBell ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isRepunitNum ( int n , int b ) { int length = 0 ; int countOne = 0 ; while ( n != 0 ) { int r = n % b ; length ++ ; if ( r == 1 ) countOne ++ ; n = n / b ; } return countOne >= 3 && countOne == length ; } public static void main ( String [ ] args ) { int n = 31 ; int base = 2 ; if ( isRepunitNum ( n , base ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static int firstDigit ( int n ) { int digits = ( int ) ( Math . log ( n ) / Math . log ( 10 ) ) ; n = ( int ) ( n / Math . pow ( 10 , digits ) ) ; return n ; } static int lastDigit ( int n ) { return ( n % 10 ) ; } static boolean isGapful ( int n ) { int first_dig = firstDigit ( n ) ; int last_dig = lastDigit ( n ) ; int concatenation = first_dig * 10 + last_dig ; return ( n % concatenation == 0 ) ; } public static void main ( String [ ] args ) { int n = 108 ; if ( isGapful ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int MAX = 10000 ; static Vector < Integer > primes = new Vector < Integer > ( ) ; static void sieveSundaram ( ) { boolean [ ] marked = new boolean [ MAX / 2 + 1 ] ; for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static boolean isEconomical ( int n ) { if ( n == 1 ) return false ; int original_no = n ; int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits ++ ; original_no = original_no / 10 ; } int pDigit = 0 , count_exp = 0 , p = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { p = primes . get ( i ) ; n = n / p ; count_exp ++ ; } while ( p > 0 ) { pDigit ++ ; p = p / 10 ; } while ( count_exp > 1 ) { pDigit ++ ; count_exp = count_exp / 10 ; } } if ( n != 1 ) { while ( n > 0 ) { pDigit ++ ; n = n / 10 ; } } return ( pDigit < sumDigits ) ; } public static void main ( String [ ] args ) { sieveSundaram ( ) ; for ( int i = 1 ; i < 200 ; i ++ ) if ( isEconomical ( i ) ) System . out . print ( i + " ▁ " ) ; } }
class GFG { static boolean isPossible ( int A , int B , int N , int K , int L ) { int needa , needb ; if ( A % K == 0 ) needa = A / K ; else needa = A / K + 1 ; if ( B % L == 0 ) needb = B / L ; else needb = B / L + 1 ; int total = needa + needb ; if ( total > N ) return false ; else return true ; } public static void main ( String [ ] args ) { int A = 3 , B = 3 , N = 3 ; int K = 4 , M = 2 ; if ( isPossible ( A , B , N , K , M ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } }
import java . util . * ; class GFG { static final int MAX = 1000050 ; static int [ ] spf = new int [ MAX + 1 ] ; static void calculate_SPF ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i <= MAX ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i <= MAX ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j <= MAX ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int [ ] tfactor = new int [ MAX + 1 ] ; static int [ ] pre = new int [ MAX + 1 ] ; static void CountTotalfactors ( ) { tfactor [ 1 ] = pre [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) { int mspf = spf [ i ] ; int prim = mspf ; int temp = i ; int cnt = 0 ; while ( temp % mspf == 0 ) { temp /= mspf ; cnt += 1 ; prim = prim * mspf ; } tfactor [ i ] = ( cnt + 1 ) * tfactor [ temp ] ; pre [ i ] = pre [ i - 1 ] + tfactor [ i ] ; } } static int BinarySearch ( int X ) { int start = 1 ; int end = MAX - 1 ; while ( start < end ) { int mid = ( start + end ) / 2 ; if ( pre [ mid ] == X ) return mid ; else if ( pre [ mid ] < X ) start = mid + 1 ; else end = mid ; } return start ; } static void findSumOfCount ( int X ) { calculate_SPF ( ) ; CountTotalfactors ( ) ; System . out . print ( BinarySearch ( X ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int X = 10 ; findSumOfCount ( X ) ; } }
import java . util . * ; class GFG { static boolean [ ] prime ; static void buildSieve ( int sum ) { prime = new boolean [ sum + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < ( sum + 1 ) ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < ( sum + 1 ) ; i += p ) prime [ i ] = false ; } } } static int getSum ( int a [ ] [ ] ) { int s = 0 ; for ( int i = 0 ; i < 4 ; i ++ ) for ( int j = 0 ; j < 5 ; j ++ ) s += a [ i ] [ j ] ; return s ; } static boolean checkIndex ( int n , int m , int a [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { if ( prime [ i + j ] && ! prime [ a [ i ] [ j ] ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 2 , 2 } , { 2 , 2 , 7 , 7 , 7 } , { 7 , 7 , 21 , 7 , 10 } , { 2 , 2 , 3 , 6 , 7 } } ; int sum = getSum ( a ) ; buildSieve ( sum ) ; if ( prime [ sum ] && checkIndex ( n , m , a ) ) { System . out . print ( " YES " + "NEW_LINE"); } else System . out . print ( " NO " + "NEW_LINE"); } }
import java . util . Date ; import java . util . Scanner ; public class GaussEaster { static void gaussEaster ( int Y ) { float A , B , C , P , Q , M , N , D , E ; A = Y % 19 ; B = Y % 4 ; C = Y % 7 ; P = ( float ) Math . floor ( Y / 100 ) ; Q = ( float ) Math . floor ( ( 13 + 8 * P ) / 25 ) ; M = ( 15 - Q + P - P / 4 ) % 30 ; N = ( 4 + P - P / 4 ) % 7 ; D = ( 19 * A + M ) % 30 ; E = ( 2 * B + 4 * C + 6 * D + N ) % 7 ; int days = ( int ) ( 22 + D + E ) ; if ( ( D == 29 ) && ( E == 6 ) ) { System . out . println ( Y + " - 04" + " - 19" ) ; return ; } else if ( ( D == 28 ) && ( E == 6 ) ) { System . out . println ( Y + " - 04" + " - 18" ) ; return ; } else { if ( days > 31 ) { System . out . println ( Y + " - 04 - " + ( days - 31 ) ) ; return ; } else { System . out . println ( Y + " - 03 - " + days ) ; return ; } } } public static void main ( String [ ] args ) { int Y = 2020 ; gaussEaster ( Y ) ; } }
import java . util . * ; public class GFG { static Vector < Vector < Integer > > lis = new Vector < Vector < Integer > > ( ) ; static Vector < Integer > primes = new Vector < Integer > ( ) ; static int [ ] vis = new int [ 100001 ] ; static int [ ] dis = new int [ 100001 ] ; static boolean isPrime ( int n ) { int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i += 1 ; } return true ; } static boolean valid ( int a , int b ) { int c = 0 ; while ( a > 0 ) { if ( ( a % 10 ) != ( b % 10 ) ) c += 1 ; a = a / 10 ; b = b / 10 ; } if ( c == 1 ) return true ; else return false ; } static void makePrimes ( int N ) { int L = ( int ) Math . pow ( 10 , N - 1 ) ; int R = ( int ) Math . pow ( 10 , N ) - 1 ; for ( int i = L ; i < R + 1 ; i ++ ) { if ( isPrime ( i ) ) primes . add ( i ) ; } for ( int i = 0 ; i < primes . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < primes . size ( ) ; j ++ ) { int a = primes . get ( i ) ; int b = primes . get ( j ) ; if ( valid ( a , b ) ) { lis . get ( a ) . add ( b ) ; lis . get ( b ) . add ( a ) ; } } } } static void bfs ( int src ) { Vector < Integer > q = new Vector < Integer > ( ) ; q . add ( src ) ; vis [ src ] = 1 ; dis [ src ] = 0 ; while ( q . size ( ) != 0 ) { int curr = q . get ( 0 ) ; q . remove ( 0 ) ; for ( int x : lis . get ( curr ) ) { if ( vis [ x ] == 0 ) { vis [ x ] = 1 ; q . add ( x ) ; dis [ x ] = dis [ curr ] + 1 ; } } } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100001 ; i ++ ) { lis . add ( new Vector < Integer > ( ) ) ; } int N = 4 ; makePrimes ( N ) ; int A = 1033 ; int B = 8179 ; bfs ( A ) ; if ( dis [ B ] == - 1 ) { System . out . print ( - 1 ) ; } else { System . out . print ( dis [ B ] ) ; } } }
import java . util . * ; import java . io . * ; import java . lang . * ; class GFG { static int placeValue ( int N , int num ) { int total = 1 , value = 0 , rem = 0 ; while ( true ) { rem = N % 10 ; N = N / 10 ; if ( rem == num ) { value = total * rem ; break ; } total = total * 10 ; } return value ; } public static void main ( String [ ] args ) { int D = 5 ; int N = 85932 ; System . out . println ( placeValue ( N , D ) ) ; } }
import java . util . * ; class GFG { static void BeattySequence ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int ans = ( int ) Math . floor ( i * Math . sqrt ( 2 ) ) ; System . out . print ( ans + " , ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; BeattySequence ( n ) ; } }
class GFG { static int calculateSum ( int N ) { return N * ( N - 1 ) / 2 + N * ( N - 1 ) * ( 2 * N - 1 ) / 6 ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( calculateSum ( N ) ) ; } }
class GFG { static int Sum ( int N ) { int SumOfPrimeDivisors [ ] = new int [ N + 1 ] ; for ( int i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } static boolean RuthAaronNumber ( int n ) { if ( Sum ( n ) == Sum ( n + 1 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int N = 714 ; if ( RuthAaronNumber ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static int binomialCoeff ( int n , int k ) { int [ ] [ ] C = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } public static void main ( String [ ] args ) { int n = 3 ; int k = n ; n = 2 * n ; System . out . println ( binomialCoeff ( n , k ) ) ; } }
class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } static boolean MultiplyPerfectNumber ( int n ) { if ( getSum ( n ) % n == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 28 ; if ( MultiplyPerfectNumber ( n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static int phi ( int n ) { int result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) { n /= p ; } result -= result / p ; } } if ( n > 1 ) result -= result / n ; return result ; } static boolean isPerfectTotientNum ( int n ) { int temp = n ; int sum = 0 ; while ( n > 1 ) { sum = sum + phi ( n ) ; n = phi ( n ) ; } if ( sum == temp ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 9 ; if ( isPerfectTotientNum ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isCunningham ( int n ) { return isPower ( n - 1 ) || isPower ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 126 ; if ( isCunningham ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int SubArrayProdct ( int arr [ ] , int n ) { int result = 1 ; for ( int i = 0 ; i < n ; i ++ ) result *= Math . pow ( arr [ i ] , ( i + 1 ) * ( n - i ) ) ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 4 } ; int N = arr . length ; System . out . println ( SubArrayProdct ( arr , N ) ) ; } }
import java . util . * ; class GFG { public static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } public static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } public static int countWays ( int m , int n , int x , int y ) { return nCr ( m + n , m ) - nCr ( x + y , x ) * nCr ( m + n - x - y , m - x ) ; } public static void main ( String [ ] args ) { int m = 5 ; int n = 4 ; int x = 3 ; int y = 2 ; System . out . println ( countWays ( m , n , x , y ) ) ; } }
import java . util . * ; class GFG { static int CountGreater ( int n , int k ) { int a = 0 ; while ( n > 0 ) { if ( ( n % 10 ) > k ) { a ++ ; } n = n / 10 ; } return a ; } static void PrintList ( List < Integer > ans ) { for ( int it : ans ) System . out . print ( it ) ; } static void getNumber ( int n , int k ) { int count = CountGreater ( n , k ) ; if ( count == 0 ) { System . out . print ( n ) ; return ; } List < Integer > ans = new LinkedList < > ( ) ; boolean changed = false ; while ( n > 0 ) { int digit = n % 10 ; if ( changed == true ) { ans . add ( 0 , digit ) ; } else { if ( count == 0 && digit < k ) { ans . add ( 0 , digit + 1 ) ; changed = true ; } else { ans . add ( 0 , 1 ) ; if ( digit > k ) { count -- ; } } } n = n / 10 ; } if ( changed == false ) { ans . add ( 0 , 1 ) ; } PrintList ( ans ) ; return ; } public static void main ( String [ ] args ) { int N = 51234 ; int K = 4 ; getNumber ( N , K ) ; } }
class GFG { static int averageRuns ( int runs , int matches , int notout ) { int out = matches - notout ; if ( out == 0 ) return - 1 ; int avg = ( runs ) / out ; return avg ; } public static void main ( String [ ] args ) { int runs = 10000 ; int matches = 250 ; int notout = 50 ; int avg = averageRuns ( runs , matches , notout ) ; if ( avg == - 1 ) System . out . print ( " NA " ) ; else System . out . print ( avg ) ; } }
class GFG { static void solve ( int N ) { int ans = 0 ; while ( N != 0 ) { ans += N ; N /= 2 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 ; solve ( N ) ; } }
class GFG { static int maximumAND ( int L , int R ) { return R ; } public static void main ( String [ ] args ) { int l = 3 ; int r = 7 ; System . out . print ( maximumAND ( l , r ) ) ; } }
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 " ) ; } }
import java . util . * ; class GFG { static void smallestNumber ( int N ) { int L = ( int ) ( Math . pow ( 10 , N ) - 1 ) ; int S = ( int ) Math . pow ( 10 , N - 1 ) ; for ( int i = S ; i <= L ; i ++ ) { if ( i % N == 0 ) { System . out . print ( i ) ; return ; } } } public static void main ( String [ ] args ) { int N = 2 ; smallestNumber ( N ) ; } }
import java . util . * ; class GFG { static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) arr [ i ] = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] == 1 ) { for ( int j = i ; j <= N ; j += i ) { arr [ j ] *= i ; } } } int maxValue = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { if ( arr [ i ] > maxValue ) { maxValue = i ; } } return maxValue ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . print ( maxPrimefactorNum ( N ) ) ; } }
class GFG { static final int mod = 1000000007 ; public static int linearSum ( int n ) { return ( n * ( n + 1 ) / 2 ) % mod ; } public static int rangeSum ( int b , int a ) { return ( linearSum ( b ) - linearSum ( a ) ) % mod ; } public static int totalSum ( int n ) { int result = 0 ; int i = 1 ; while ( true ) { result += rangeSum ( n / i , n / ( i + 1 ) ) * ( i % mod ) % mod ; result %= mod ; if ( i == n ) break ; i = n / ( n / ( i + 1 ) ) ; } return result ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( totalSum ( N ) ) ; N = 12 ; System . out . println ( totalSum ( N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void spf_array ( int spf [ ] ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < 1000 ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < 1000 ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < 1000 ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < 1000 ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int minimum_operation ( int b [ ] , int d , int spf [ ] ) { Map < Integer , Integer > m = new HashMap < > ( ) ; int i = 0 ; int c = 0 ; for ( i = 0 ; i < d ; i ++ ) { int x = b [ i ] ; while ( x != 1 ) { x = x / spf [ x ] ; if ( m . get ( spf [ x ] ) == null ) { m . put ( spf [ x ] , 1 ) ; } } } m . remove ( 1 ) ; for ( Map . Entry < Integer , Integer > x : m . entrySet ( ) ) { int e = 0 , o = 0 ; int j = x . getKey ( ) ; for ( i = 0 ; i < d ; i ++ ) { if ( b [ i ] % j == 0 ) { int h = 0 ; int g = b [ i ] ; while ( g != 0 ) { if ( g % j != 0 ) { break ; } g = g / j ; h = h + 1 ; } if ( h % 2 == 0 ) { e = e + 1 ; } else { o = o + 1 ; } } else { e = e + 1 ; } } c = c + Math . min ( o , e ) ; } return c ; } public static void main ( String [ ] args ) { int [ ] spf = new int [ 1001 ] ; int b [ ] = { 1 , 4 , 6 } ; int d = b . length ; spf_array ( spf ) ; System . out . print ( minimum_operation ( b , d , spf ) ) ; } }
class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean isRare ( int N ) { int reverseN = reversDigits ( N ) ; if ( reverseN == N ) return false ; return isPerfectSquare ( N + reverseN ) && isPerfectSquare ( N - reverseN ) ; } public static void main ( String [ ] args ) { int n = 65 ; if ( isRare ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } static boolean isZuckerman ( int n ) { return n % getProduct ( n ) == 0 ; } public static void main ( String [ ] args ) { int n = 115 ; if ( isZuckerman ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static boolean isDouble ( int num ) { String s = Integer . toString ( num ) ; int l = s . length ( ) ; if ( s . charAt ( 0 ) == s . charAt ( 1 ) ) return false ; if ( l % 2 == 1 ) { s = s + s . charAt ( 1 ) ; l ++ ; } String s1 = s . substring ( 0 , l / 2 ) ; String s2 = s . substring ( l / 2 ) ; return s1 . equals ( s2 ) ; } static boolean isNontrivialUndulant ( int N ) { return N > 100 && isDouble ( N ) ; } public static void main ( String [ ] args ) { int n = 121 ; if ( isNontrivialUndulant ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static boolean isAdditivePrime ( int n ) { if ( ! isPrime ( n ) ) return false ; return isPrime ( getSum ( n ) ) ; } public static void main ( String [ ] args ) { int n = 23 ; if ( isAdditivePrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean isStraighLineNum ( int N ) { if ( N <= 99 ) return false ; String s = Integer . toString ( N ) ; int d = s . charAt ( 1 ) - s . charAt ( 0 ) ; for ( int i = 2 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) - s . charAt ( i - 1 ) != d ) return false ; return true ; } public static void main ( String [ ] args ) { int n = 135 ; if ( isStraighLineNum ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static void findNthTerm ( int n ) { System . out . println ( Math . pow ( 2 , n ) - 2 * n ) ; } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
class GFG { static boolean isEvenOddForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 0 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 1 ) return false ; } return true ; } static boolean isOddEvenForm ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) % 2 != 1 ) return false ; if ( i % 2 == 1 && s . charAt ( i ) % 2 != 0 ) return false ; } return true ; } static boolean isAlternating ( int n ) { String str = Integer . toString ( n ) ; return ( isEvenOddForm ( str ) || isOddEvenForm ( str ) ) ; } public static void main ( String [ ] args ) { int N = 129 ; if ( isAlternating ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int TEN = 10 ; static void updateFreq ( int n , int freq [ ] ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int freqA [ ] = new int [ TEN ] ; int freqB [ ] = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } static boolean OrmistonPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && areAnagrams ( n1 , n2 ) ) ; } public static void main ( String [ ] args ) { int n1 = 1913 , n2 = 1931 ; if ( OrmistonPrime ( n1 , n2 ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } }
class GFG { static int DecakismyriagonNum ( int N ) { return ( 99998 * N * N - 99996 * N ) / 2 ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( DecakismyriagonNum ( N ) ) ; } }
class GFG { static boolean isLoeschian ( int n ) { for ( int x = 1 ; x <= Math . sqrt ( n ) ; x ++ ) { for ( int y = 1 ; y <= Math . sqrt ( n ) ; y ++ ) { if ( x * x + x * y + y * y == n ) return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isLoeschian ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
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 + n + 1 ) ; } static boolean check ( int num ) { int sigmaN = divSum ( num ) ; for ( int i = 2 ; i <= Math . sqrt ( num ) ; i ++ ) { if ( num % i == 0 ) { if ( i == ( num / i ) ) { if ( sigmaN - 2 * i == 2 * num ) return true ; } else { if ( sigmaN - 2 * i == 2 * num ) return true ; if ( sigmaN - 2 * ( num / i ) == 2 * num ) return true ; } } } if ( sigmaN - 2 * 1 == 2 * num ) return true ; return false ; } static boolean isAdmirableNum ( int N ) { return check ( N ) ; } public static void main ( String [ ] args ) { int n = 12 ; if ( isAdmirableNum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static void printSeries ( int N ) { int ith_term = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { ith_term = ( 13 * i * ( i - 1 ) ) / 2 + 2 ; System . out . print ( ith_term + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 7 ; printSeries ( N ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > Pairs ( int C ) { Vector < pair > ans = new Vector < pair > ( ) ; for ( int i = 1 ; i < C ; i ++ ) { for ( int j = i + 1 ; j < C ; j ++ ) { if ( ( i * i ) + ( j * j ) == ( C * C ) ) { ans . add ( new pair ( i , j ) ) ; } } } return ans ; } public static void main ( String [ ] args ) { int C = 13 ; Vector < pair > ans = Pairs ( C ) ; if ( ans . size ( ) == 0 ) { System . out . print ( " No ▁ valid ▁ pair ▁ " + " exist " + "NEW_LINE"); return ; } for ( pair i : ans ) { System . out . print ( " ( " + i . first + " , ▁ " + i . second + " ) " + "NEW_LINE"); } } }
class GFG { static float strikerate ( float bowls , float runs ) { float z ; z = ( runs / bowls ) * 100 ; return z ; } public static void main ( String [ ] args ) { int A , B ; A = 264 ; B = 173 ; System . out . println ( strikerate ( B , A ) ) ; } }
import java . lang . Math ; class GFG { public static int factorize ( int N ) { int count = 0 ; int cnt = 0 ; while ( ( N % 2 ) == 0 ) { N = N / 2 ; count ++ ; } cnt = cnt + count ; for ( int i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } cnt = cnt + count ; } if ( N > 2 ) cnt = cnt + 1 ; return cnt ; } public static void ifNumberExists ( int X , int Y ) { int C , dsum ; C = X - Y - 1 ; dsum = factorize ( X ) ; if ( dsum >= C ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int X , Y ; X = 6 ; Y = 4 ; ifNumberExists ( X , Y ) ; } }
import java . util . * ; class GFG { static final int max = 10000000 ; static boolean [ ] prime = new boolean [ max ] ; static void SieveOfEratosthenes ( int maxm ) { prime [ 0 ] = prime [ 1 ] = true ; for ( int i = 2 ; i * i <= maxm ; i ++ ) { if ( ! prime [ i ] ) { for ( int j = i * i ; j <= maxm ; j += i ) { prime [ j ] = true ; } } } } static void print_nearest_prime ( int arr [ ] , int N ) { int maxm = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; SieveOfEratosthenes ( maxm ) ; Vector < Integer > primes = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! prime [ arr [ i ] ] ) primes . add ( i ) ; } if ( primes . size ( ) == 0 ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( - 1 + " ▁ " ) ; } return ; } int curr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( curr == primes . size ( ) - 1 || i <= primes . get ( curr ) ) { System . out . print ( arr [ primes . get ( curr ) ] + " ▁ " ) ; continue ; } if ( Math . abs ( primes . get ( curr ) - i ) < Math . abs ( primes . get ( curr + 1 ) - i ) ) { System . out . print ( arr [ primes . get ( curr ) ] + " ▁ " ) ; } else { curr ++ ; System . out . print ( arr [ primes . get ( curr ) ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 8 , 7 , 12 , 15 , 3 , 11 } ; print_nearest_prime ( arr , N ) ; } }
import java . util . * ; class GFG { static void findPosition ( int n ) { int i = 0 ; int pos = 0 ; while ( n > 0 ) { if ( n % 10 == 7 ) { pos = pos + ( int ) Math . pow ( 2 , i + 1 ) ; } else { pos = pos + ( int ) Math . pow ( 2 , i ) ; } i ++ ; n = n / 10 ; } System . out . print ( pos ) ; } public static void main ( String [ ] args ) { int N = 777 ; findPosition ( N ) ; } }
import java . util . * ; import java . io . * ; class GFG { static double findAverageOfCube ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { sum += i * i * i ; } return sum / n ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( findAverageOfCube ( n ) ) ; } }
import java . io . * ; class GFG { static String findNumberL ( int n ) { if ( n == 1 ) return "1" ; String result = " " ; int length = ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '9' ; } result = Integer . toString ( n ) + result + Integer . toString ( n ) ; return result ; } static String findNumberS ( int n ) { if ( n == 1 ) return "1" ; String result = " " ; int length = ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; for ( int i = 1 ; i <= n - ( 2 * length ) ; i ++ ) { result += '0' ; } result = Integer . toString ( n ) + result + Integer . toString ( n ) ; return result ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( " Smallest ▁ Number ▁ = ▁ " + findNumberS ( N ) ) ; System . out . print ( " Largest ▁ Number ▁ = ▁ " + findNumberL ( N ) ) ; } }
class GFG { static int sum_star_num ( int n ) { int summ = 2 * n * ( n + 1 ) * ( n - 1 ) + n ; return summ ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( sum_star_num ( n ) ) ; } }
import java . util . * ; import java . awt . * ; class GFG { static void solve ( int n , ArrayList < Point > vec ) { ArrayList < Integer > a = new ArrayList < > ( ) ; ArrayList < Integer > b = new ArrayList < > ( ) ; for ( Point pr : vec ) { a . add ( pr . x ) ; b . add ( pr . y ) ; } Collections . sort ( a ) ; Collections . sort ( b ) ; int left , right , ans ; if ( ( n & 1 ) != 0 ) { left = a . get ( n / 2 ) ; right = b . get ( n / 2 ) ; ans = right - left + 1 ; } else { left = ( a . get ( n / 2 ) + a . get ( n / 2 - 1 ) ) ; right = ( b . get ( n / 2 ) + b . get ( n / 2 - 1 ) ) ; ans = right - left + 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 3 ; ArrayList < Point > vec = new ArrayList < > ( ) ; vec . add ( new Point ( 100 , 100 ) ) ; vec . add ( new Point ( 10 , 10000 ) ) ; vec . add ( new Point ( 1 , 1000000000 ) ) ; solve ( N , vec ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int find_pairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { int count = i . getValue ( ) ; if ( count > 1 ) { ans += ( ( count * ( count - 1 ) ) / 2 ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 7 , 8 } ; int arr_size = arr . length ; System . out . print ( find_pairs ( arr , arr_size ) ) ; } }
class GFG { static int gonNum360 ( int n ) { return ( 358 * n * n - 356 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( gonNum360 ( n ) ) ; } }
class GFG { static int gonNum257 ( int n ) { return ( 255 * n * n - 253 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( gonNum257 ( n ) ) ; } }
import java . util . * ; class GFG { static int TetracontadigonalNum ( int n ) { return ( 40 * n * n - 38 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( TetracontadigonalNum ( n ) ) ; } }
class GFG { static int findLCMPrime ( int a , int b ) { if ( a == b ) { return a ; } return a * b ; } public static void main ( String [ ] args ) { int a = 3 , b = 5 ; System . out . println ( findLCMPrime ( a , b ) ) ; } }
class GFG { static void smallestNumber ( int N ) { System . out . print ( N * Math . ceil ( Math . pow ( 2 , ( N - 1 ) ) / N ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; smallestNumber ( N ) ; } }
class GFG { static void checkSolution ( int a , int b , int c ) { if ( 2 * b * b == 9 * a * c ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 3 , c = 2 ; checkSolution ( a , b , c ) ; } }
class GFG { static void getParity ( int n , int [ ] A ) { for ( int x : A ) { if ( ( x & 1 ) == 1 ) { System . out . println ( " Even " ) ; return ; } } System . out . println ( " Odd " ) ; } public static void main ( String [ ] args ) { int N = 3 ; int [ ] A = { 2 , 3 , 1 } ; getParity ( N , A ) ; } }
class GFG { static void numberOfDigits ( int N ) { int nod = ( int ) Math . floor ( Math . log10 ( N ) + 1 ) ; int toDecrease = ( int ) ( Math . pow ( 10 , nod ) - 1 ) / 9 ; System . out . print ( ( N + 1 ) * nod - toDecrease ) ; } public static void main ( String [ ] args ) { int N = 13 ; numberOfDigits ( N ) ; } }
class GFG { static boolean isPossible ( int arr [ ] , int N , int K ) { int oddCount = 0 , evenCount = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { evenCount ++ ; } else { oddCount ++ ; } } if ( evenCount == N || ( oddCount == N && K % 2 == 0 ) || ( K == N && oddCount % 2 == 0 ) ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 8 } ; int K = 5 ; int N = arr . length ; if ( isPossible ( arr , N , K ) ) { System . out . println ( " Possible " ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } }
import java . util . * ; class GFG { static void lastTwoDigits ( double N ) { if ( N >= 10 ) { System . out . print ( "00" ) ; return ; } double fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) fac = ( fac * i ) % 100 ; System . out . print ( fac ) ; } public static void main ( String args [ ] ) { int N = 7 ; lastTwoDigits ( N ) ; } }
class GFG { static float productOfGP ( float a , float r , int n ) { return ( float ) Math . pow ( a , n ) * ( float ) Math . pow ( r , n * ( n - 1 ) / 2 ) ; } public static void main ( String s [ ] ) { float a = 1 , r = 2 ; int N = 4 ; System . out . println ( productOfGP ( a , r , N ) ) ; } }
import java . util . * ; class GFG { static float productOfGP ( float a , float r , int n ) { int an = ( int ) ( a * ( int ) ( Math . pow ( r , n - 1 ) ) ) ; return ( int ) Math . sqrt ( ( int ) Math . pow ( a * an , n ) ) ; } public static void main ( String args [ ] ) { float a = 1 , r = 2 ; int N = 4 ; System . out . print ( productOfGP ( a , r , N ) ) ; } }
import java . util . * ; class GFG { static void prob ( int x , int y ) { int num = Math . abs ( x - y + 1 ) * Math . abs ( x - y + 1 ) ; int den = ( x + 1 ) * ( x + 1 ) ; int gcd = __gcd ( num , den ) ; System . out . print ( num / gcd + " / " + den / gcd + "NEW_LINE"); } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int X = 2 , Y = 1 ; prob ( X , Y ) ; } }
class GFG { static boolean is_Chiliagon ( int N ) { float n = ( float ) ( 996 + Math . sqrt ( 7984 * N + 992016 ) ) / 1996 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String s [ ] ) { int N = 1000 ; if ( is_Chiliagon ( N ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static int commonSubarrays ( int [ ] A , int [ ] B , int N ) { int [ ] Map = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) Map [ A [ i ] ] = i ; for ( int i = 0 ; i < N ; i ++ ) { B [ i ] = Map [ B [ i ] ] ; } int count = 0 ; int i = 0 , K ; while ( i < N ) { K = 1 ; i += 1 ; while ( i < N && B [ i ] == B [ i - 1 ] + 1 ) { i += 1 ; K += 1 ; } count = count + ( ( K ) * ( K + 1 ) ) / 2 ; } return count ; } public static void main ( String [ ] args ) { int N = 3 ; int A [ ] = { 1 , 2 , 3 } ; int B [ ] = { 2 , 3 , 1 } ; System . out . print ( commonSubarrays ( A , B , N ) ) ; System . out . print ( "NEW_LINE"); N = 5 ; int C [ ] = { 1 , 2 , 3 , 4 , 5 } ; int D [ ] = { 2 , 3 , 1 , 4 , 5 } ; System . out . print ( commonSubarrays ( C , D , N ) ) ; } }
import java . util . * ; class GFG { static void getArrayOfSizeK ( int n , int k ) { Vector < Integer > ans = new Vector < Integer > ( ) ; int odd = n - ( ( k - 1 ) * 1 ) ; if ( odd > 0 && odd % 2 != 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . add ( 1 ) ; } ans . add ( odd ) ; } int even = n - ( ( k - 1 ) * 2 ) ; if ( even > 0 && even % 2 == 0 && ans . size ( ) == 0 ) { for ( int i = 0 ; i < k - 1 ; i ++ ) { ans . add ( 2 ) ; } ans . add ( even ) ; } if ( ans . size ( ) > 0 ) { for ( int i = 0 ; i < k ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } else { System . out . println ( " NO " ) ; } } public static void main ( String args [ ] ) { int n = 10 , k = 3 ; getArrayOfSizeK ( n , k ) ; } }
import java . util . * ; class GFG { static boolean checkDivisbility ( int num ) { int digit ; int N = num ; while ( num != 0 ) { digit = num % 10 ; num = num / 10 ; if ( digit == 0 N % digit != 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { int N = 128 ; boolean result = checkDivisbility ( N ) ; if ( result ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int count_same_digit ( int L , int R ) { int tmp = 0 , ans = 0 ; int n = ( int ) Math . log10 ( R ) + 1 ; for ( int i = 0 ; i < n ; i ++ ) { tmp = tmp * 10 + 1 ; for ( int j = 1 ; j <= 9 ; j ++ ) { if ( L <= ( tmp * j ) && ( tmp * j ) <= R ) { ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int L = 12 , R = 68 ; System . out . println ( count_same_digit ( L , R ) ) ; } }
import java . util . Vector ; class GFG { static int minLCM ( int arr [ ] , int n ) { int mx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mx = Math . max ( mx , arr [ i ] ) ; } Vector < Integer > [ ] mul = new Vector [ mx + 1 ] ; for ( int i = 0 ; i < mul . length ; i ++ ) mul [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mul [ arr [ i ] ] . size ( ) > 1 ) { continue ; } mul [ arr [ i ] ] . add ( arr [ i ] ) ; } for ( int i = 1 ; i <= mx ; i ++ ) { for ( int j = i + i ; j <= mx ; j += i ) { if ( mul [ i ] . size ( ) > 1 ) { break ; } for ( int k : mul [ j ] ) { if ( mul [ i ] . size ( ) > 1 ) break ; mul [ i ] . add ( k ) ; } } } int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i <= mx ; i ++ ) { if ( mul [ i ] . size ( ) <= 1 ) continue ; int a = mul [ i ] . get ( 0 ) , b = mul [ i ] . get ( 1 ) ; int lcm = ( a * b ) / i ; ans = Math . min ( ans , lcm ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 6 , 5 } ; int n = arr . length ; System . out . print ( minLCM ( arr , n ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static int findArrangement ( int N ) { if ( N < 3 ) return 0 ; return ( ( N ) * ( N - 1 ) * ( N - 2 ) ) / 3 ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( findArrangement ( N ) ) ; } }
import java . util . * ; class GFG { static void calc_ans ( int l , int r ) { Vector < Integer > power2 = new Vector < Integer > ( ) , power3 = new Vector < Integer > ( ) ; int mul2 = 1 ; while ( mul2 <= r ) { power2 . add ( mul2 ) ; mul2 *= 2 ; } int mul3 = 1 ; while ( mul3 <= r ) { power3 . add ( mul3 ) ; mul3 *= 3 ; } Vector < Integer > power23 = new Vector < Integer > ( ) ; for ( int x = 0 ; x < power2 . size ( ) ; x ++ ) { for ( int y = 0 ; y < power3 . size ( ) ; y ++ ) { int mul = power2 . get ( x ) * power3 . get ( y ) ; if ( mul == 1 ) continue ; if ( mul <= r ) power23 . add ( mul ) ; } } int ans = 0 ; for ( int x : power23 ) { if ( x >= l && x <= r ) ans ++ ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int l = 1 , r = 10 ; calc_ans ( l , r ) ; } }
import java . util . * ; class GFG { static final int MOD = 7 ; static int inv ( int a ) { int o = 1 ; for ( int p = MOD - 2 ; p > 0 ; p >>= 1 ) { if ( ( p & 1 ) == 1 ) o = ( o * a ) % MOD ; a = ( a * a ) % MOD ; } return o ; } static int inv6 = inv ( 6 ) ; static int sumOfSquares ( int n ) { n %= MOD ; return ( ( ( n * ( n + 1 ) ) % MOD * ( 2 * n + 1 ) ) % MOD * inv6 ) % MOD ; } static int sums ( int n ) { if ( n < 4 ) return 0 ; int curStart = 2 , ans = 0 ; int sqrtN = ( int ) Math . sqrt ( n ) ; while ( curStart <= n / curStart ) { int V = n / ( curStart * curStart ) ; int end = ( int ) Math . sqrt ( n / V ) ; ans += ( n / ( curStart * curStart ) % MOD * ( sumOfSquares ( end ) + MOD - sumOfSquares ( curStart - 1 ) ) ) % MOD ; if ( ans >= MOD ) ans -= MOD ; curStart = end + 1 ; } return ans ; } public static void main ( String [ ] args ) { int input [ ] = { 5 } ; for ( int x : input ) { System . out . print ( " sum ▁ of ▁ all ▁ perfect ▁ " + " square ▁ divisors ▁ from ▁ " + "1 ▁ to ▁ " + x + " ▁ is : ▁ " ) ; System . out . print ( x + sums ( x ) + "NEW_LINE"); } } }
class GFG { static boolean isSuffix ( String s1 , String s2 ) { int n1 = s1 . length ( ) ; int n2 = s2 . length ( ) ; if ( n1 > n2 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( s1 . charAt ( n1 - i - 1 ) != s2 . charAt ( n2 - i - 1 ) ) return false ; return true ; } static boolean CheckBinaryEquivalent ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; int c = ( int ) Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } String bin = Integer . toString ( B_Number ) ; return isSuffix ( "001" , bin ) ; } public static void main ( String [ ] args ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean CheckBinaryEquivalent ( int N ) { return ( N - 1 ) % 8 == 0 ; } public static void main ( String [ ] args ) { int N = 9 ; if ( CheckBinaryEquivalent ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . math . * ; class GFG { static ArrayList < Integer > get_divisors ( int A ) { ArrayList < Integer > ans = new ArrayList < > ( ) ; ans . add ( 1 ) ; for ( int i = 2 ; i <= Math . sqrt ( A ) ; i ++ ) { if ( A % i == 0 ) { ans . add ( i ) ; if ( ( i * i ) != A ) ans . add ( A / i ) ; } } return ans ; } static boolean Sum_Possible ( ArrayList < Integer > set , int sum ) { int n = set . size ( ) ; boolean subset [ ] [ ] = new boolean [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) { if ( j < set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] ; if ( j >= set . get ( i - 1 ) ) subset [ i ] [ j ] = subset [ i - 1 ] [ j ] || subset [ i - 1 ] [ j - set . get ( i - 1 ) ] ; } } return subset [ n ] [ sum ] ; } static boolean Is_Practical ( int A ) { ArrayList < Integer > divisors ; divisors = get_divisors ( A ) ; for ( int i = 2 ; i < A ; i ++ ) { if ( Sum_Possible ( divisors , i ) == false ) return false ; } return true ; } static void print_practica_No ( int A , int B ) { for ( int i = A ; i <= B ; i ++ ) { if ( Is_Practical ( i ) == true ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int A = 1 , B = 100 ; print_practica_No ( A , B ) ; } }
import java . util . * ; class GFG { static float maxDivision ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; float mxdiv = arr [ 1 ] ; for ( int i = 2 ; i < n ; ++ i ) mxdiv = mxdiv / arr [ i ] ; return arr [ 0 ] / mxdiv ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 100 , 1000 , 10 , 2 } ; int n = arr . length ; System . out . print ( ( int ) maxDivision ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void coPrimeSet ( int n ) { int firstadj , secadj ; if ( n < 4 ) { System . out . print ( " ( ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i + " , ▁ " ) ; System . out . print ( ")NEW_LINE"); } else { if ( n % 2 == 0 ) { for ( int i = 0 ; i < n / 2 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } } else { for ( int i = 0 ; i < n / 2 - 1 ; i ++ ) { firstadj = 2 * i + 1 ; secadj = 2 * i + 2 ; System . out . print ( " ( " + firstadj + " , ▁ " + secadj + ")NEW_LINE"); } System . out . print ( " ( " + ( n - 2 ) + " , ▁ " + ( n - 1 ) + " , ▁ " + n + ")NEW_LINE"); } } } public static void main ( String [ ] args ) { int n = 5 ; coPrimeSet ( n ) ; } }
import java . util . * ; class GFG { public static void main ( String args [ ] ) { int N = 21 ; int A = ( int ) ( Math . sqrt ( 2 * N + 0.25 ) - 0.5 ) ; int B = A ; if ( B != A ) System . out . print ( " - 1" ) ; else System . out . print ( B ) ; } }
import java . util . * ; class GFG { static final int mod = 1000000007 ; static int power ( int x , int y ) { int p = 1000000007 ; int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int productPairs ( int arr [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = ( product % mod * ( int ) power ( arr [ i ] , ( 2 * n ) ) % mod ) % mod ; } return product % mod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . print ( productPairs ( arr , n ) ) ; } }
import java . io . * ; class GFG { static long operation ( int arr [ ] , int N ) { int right = 0 ; int num = 0 ; long ans = 0 ; for ( int left = 0 ; left < N ; left ++ ) { while ( right < N && num + arr [ right ] == ( num ^ arr [ right ] ) ) { num += arr [ right ] ; right ++ ; } ans += right - left ; if ( left == right ) right ++ ; else num -= arr [ left ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . println ( operation ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static boolean [ ] sieveOfEratosthenes ( int N ) { boolean [ ] isPrime = new boolean [ N + 1 ] ; for ( int i = 0 ; i < N + 1 ; i ++ ) { isPrime [ i ] = true ; } isPrime [ 0 ] = false ; isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( isPrime [ i ] == true ) { int j = 2 ; while ( i * j <= N ) { isPrime [ i * j ] = false ; j ++ ; } } } return isPrime ; } public static int numPairsWithPrimeSum ( int [ ] arr , int n ) { int N = 2 * 1000000 ; boolean [ ] isPrime = sieveOfEratosthenes ( N ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = arr [ i ] + arr [ j ] ; if ( isPrime [ sum ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( numPairsWithPrimeSum ( arr , n ) ) ; } }
class GFG { public static int [ ] digit = new int [ 10 ] ; public static void digitsPresent ( int n ) { int lastDigit ; while ( n != 0 ) { lastDigit = n % 10 ; digit [ lastDigit ] = 1 ; n /= 10 ; } } public static int checkLastDigit ( int num ) { int count = 0 ; int lastDigit ; lastDigit = num % 10 ; if ( digit [ lastDigit ] == 1 ) count ++ ; return count ; } public static void findCount ( int N , int K , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < K ; i ++ ) { count = checkLastDigit ( arr [ i ] ) == 1 ? count + 1 : count ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int N = 1731 ; digitsPresent ( N ) ; int K = 5 ; int arr [ ] = { 57 , 6786 , 1111 , 3 , 9812 } ; findCount ( N , K , arr ) ; } }
import java . util . * ; class GFG { static boolean divisorsSame ( int n ) { return ( n - 2 ) % 4 == 0 ; } public static void main ( String [ ] args ) { int N = 6 ; if ( divisorsSame ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int find ( int K , int N ) { N -- ; while ( N -- != 0 ) { int curr_term = K ; int min_d = 9 ; int max_d = 0 ; while ( curr_term > 0 ) { int r = curr_term % 10 ; min_d = Math . min ( min_d , r ) ; max_d = Math . max ( max_d , r ) ; curr_term = curr_term / 10 ; } if ( min_d == 0 ) { break ; } K = K + min_d * max_d ; } return K ; } public static void main ( String [ ] args ) { int K = 487 ; int N = 2 ; System . out . print ( find ( K , N ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static long countSubarrays ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int [ ] prefixSum = new int [ n ] ; int prefixMin = 0 ; prefixSum [ 0 ] = arr [ 0 ] ; prefixMin = Math . min ( prefixMin , prefixSum [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; prefixMin = Math . min ( prefixMin , prefixSum [ i ] ) ; } long countSubs = 0 ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; prefixSum [ i ] - j * j >= prefixMin ; j ++ ) { if ( mp . containsKey ( prefixSum [ i ] - j * j ) ) countSubs += mp . get ( prefixSum [ i ] - j * j ) ; } if ( mp . containsKey ( prefixSum [ i ] ) ) { mp . put ( prefixSum [ i ] , mp . get ( prefixSum [ i ] ) + 1 ) ; } else { mp . put ( prefixSum [ i ] , 1 ) ; } } return countSubs ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , - 5 , 6 , - 7 , 4 } ; int n = arr . length ; long ans = countSubarrays ( arr , n ) ; System . out . print ( ans ) ; } }
class GFG { static boolean checkSolution ( float a1 , float b1 , float c1 , float a2 , float b2 , float c2 ) { return ( ( a1 / a2 ) == ( b1 / b2 ) && ( b1 / b2 ) == ( c1 / c2 ) ) ; } public static void main ( String [ ] args ) { float a1 = 1 , b1 = - 5 , c1 = 6 ; float a2 = 2 , b2 = - 10 , c2 = 12 ; if ( checkSolution ( a1 , b1 , c1 , a2 , b2 , c2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static void checkSolution ( int a , int b , int c ) { if ( b == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 0 , c = 2 ; checkSolution ( a , b , c ) ; } }
class GFG { public static void positiveProduct ( int arr [ ] , int n ) { int even_count = 0 ; int odd_count = 0 ; int total_count = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( total_count % 2 == 0 ) { even_count ++ ; } else { odd_count ++ ; } if ( arr [ i ] < 0 ) { total_count ++ ; } if ( total_count % 2 == 0 ) ans += even_count ; else ans += odd_count ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , - 3 , 3 , - 1 , 1 } ; int size = A . length ; positiveProduct ( A , size ) ; } }
class GFG { static int getCount ( int d , int n ) { int no = n / d ; int result = no ; for ( int p = 2 ; p * p <= no ; ++ p ) { if ( no % p == 0 ) { while ( no % p == 0 ) no /= p ; result -= result / p ; } } if ( no > 1 ) result -= result / no ; return result ; } static int sumOfGCDofPairs ( int n ) { int res = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int d1 = i ; int d2 = n / i ; res += d1 * getCount ( d1 , n ) ; if ( d1 != d2 ) res += d2 * getCount ( d2 , n ) ; } } return res ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . print ( sumOfGCDofPairs ( n ) ) ; } }
import java . util . Arrays ; class GFG { public static boolean checkIsFibonacci ( int arr [ ] , int n ) { if ( n == 1 n == 2 ) return true ; Arrays . sort ( arr ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( ( arr [ i - 1 ] + arr [ i - 2 ] ) != arr [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 3 , 5 , 13 } ; int n = arr . length ; if ( checkIsFibonacci ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int lastDigit2PowerN ( int n ) { if ( n == 0 ) return 1 ; else if ( n % 4 == 1 ) return 2 ; else if ( n % 4 == 2 ) return 4 ; else if ( n % 4 == 3 ) return 8 ; else } public static void main ( String [ ] args ) { for ( int n = 0 ; n < 20 ; n ++ ) System . out . print ( lastDigit2PowerN ( n ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int countSubsequences ( int arr [ ] , int n ) { int totalSubsequence = ( int ) ( Math . pow ( 2 , n ) - 1 ) ; int countPrime = 0 , countOnes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) countOnes ++ ; else if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } int compositeSubsequence ; int onesSequence = ( int ) ( Math . pow ( 2 , countOnes ) - 1 ) ; compositeSubsequence = totalSubsequence - countPrime - onesSequence - onesSequence * countPrime ; return compositeSubsequence ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 2 } ; int n = arr . length ; System . out . print ( countSubsequences ( arr , n ) ) ; } }
class GFG { static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int sumSubsequences ( int arr [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } int kLengthSubSequence ; kLengthSubSequence = nCr ( n , k ) ; int ans = sum * ( ( k * kLengthSubSequence ) / n ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 8 , 9 , 2 } ; int K = 2 ; int n = arr . length ; System . out . print ( sumSubsequences ( arr , n , K ) ) ; } }
import java . util . * ; class GFG { static int findSum ( int n , int k ) { int val = ( k / ( n - 1 ) ) * n ; int rem = k % ( n - 1 ) ; if ( rem == 0 ) { val = val - 1 ; } else { val = val + rem ; } int sum = ( val * ( val + 1 ) ) / 2 ; int x = k / ( n - 1 ) ; int sum_of_multiples = ( x * ( x + 1 ) * n ) / 2 ; sum -= sum_of_multiples ; return sum ; } public static void main ( String [ ] args ) { int n = 7 , k = 13 ; System . out . println ( findSum ( n , k ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > getFactorization ( int x ) { int count = 0 ; Vector < Integer > v = new Vector < > ( ) ; while ( x % 2 == 0 ) { count ++ ; x = x / 2 ; } if ( count != 0 ) v . add ( count ) ; for ( int i = 3 ; i <= Math . sqrt ( x ) ; i += 2 ) { count = 0 ; while ( x % i == 0 ) { count ++ ; x /= i ; } if ( count != 0 ) v . add ( count ) ; } if ( x > 1 ) { v . add ( 1 ) ; } return v ; } static int nonPrimeDivisors ( int N ) { Vector < Integer > v = getFactorization ( N ) ; int ret = 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ret = ret * ( v . get ( i ) + 1 ) ; ret = ret - v . size ( ) ; return ret ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . println ( nonPrimeDivisors ( N ) ) ; } }
import java . util . * ; class GFG { static boolean isDivisible ( char [ ] s ) { while ( s . length > 4 ) { int l = s . length - 1 ; int x = ( s [ l ] - '0' ) * 37 ; s = reverse ( s ) ; s = Arrays . copyOfRange ( s , 1 , s . length ) ; int i = 0 , carry = 0 ; while ( x > 0 ) { int d = ( s [ i ] - '0' ) - ( x % 10 ) - carry ; if ( d < 0 ) { d += 10 ; carry = 1 ; } else carry = 0 ; s [ i ] = ( char ) ( d + '0' ) ; x /= 10 ; i ++ ; } while ( carry > 0 && i < l ) { int d = ( s [ i ] - '0' ) - carry ; if ( d < 0 ) { d += 10 ; carry = 1 ; } else carry = 0 ; s [ i ] = ( char ) ( d + '0' ) ; i ++ ; } s = reverse ( s ) ; } int num = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { num = num * 10 + ( s [ i ] - '0' ) ; } if ( num % 53 == 0 ) return true ; else return false ; } static char [ ] reverse ( char [ ] a ) { int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return a ; } public static void main ( String [ ] args ) { String N = "18432462191076" ; if ( isDivisible ( N . toCharArray ( ) ) ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
import java . util . * ; import java . lang . * ; class GFG { static boolean checksilverRatio ( double a , double b ) { if ( a < b ) { a = a + b ; b = a - b ; a = a - b ; } double ratio1 = ( ( a / b ) * 1000 ) / 1000 ; double ratio2 = ( int ) ( ( ( 2 * a + b ) / a ) * 1000 ) ; ratio2 = ratio2 / 1000 ; if ( ratio1 == ratio2 && ( int ) ( ratio1 - 2.414 ) == 0 ) { System . out . println ( " Yes " ) ; return true ; } else { System . out . println ( " No " ) ; return false ; } } public static void main ( String [ ] args ) { double a = 2.414 ; double b = 1 ; checksilverRatio ( a , b ) ; } }
class GFG { static int MIN ( int n ) { int ans = 11 ; while ( n > 0 ) { ans = Math . min ( ans , n % 10 ) ; n /= 10 ; } return ans ; } static int MAX ( int n ) { int ans = - 1 ; while ( n > 0 ) { ans = Math . max ( ans , n % 10 ) ; n /= 10 ; } return ans ; } static void Find_value ( int n , int k ) { k -- ; int x = 0 ; int y = 0 ; while ( k -- > 0 ) { x = MIN ( n ) ; y = MAX ( n ) ; if ( y - x == 0 ) break ; n *= ( y - x ) ; } System . out . print ( n ) ; } public static void main ( String [ ] args ) { int N = 487 , D = 5 ; Find_value ( N , D ) ; } }
import java . lang . Math ; class GFG { public static boolean isdecagonal ( int N ) { double n = ( 3 + Math . sqrt ( 16 * N + 9 ) ) / 8 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 10 ; if ( isdecagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . lang . Math ; class GFG { public static boolean isOctadecagon ( int N ) { double n = ( 14 + Math . sqrt ( 128 * N + 196 ) ) / 32 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 18 ; if ( isOctadecagon ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int countSubsequences ( int arr [ ] , int n , int k ) { int countPrime = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( arr [ i ] ) ) countPrime ++ ; } if ( countPrime < k ) return 0 ; return nCr ( countPrime , k ) * ( int ) Math . pow ( 2 , ( n - countPrime ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int K = 3 ; int n = arr . length ; System . out . println ( countSubsequences ( arr , n , K ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int digSum ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a = a / 10 ; } return sum ; } static boolean isPrime ( int r ) { boolean s = true ; for ( int i = 2 ; i * i <= r ; i ++ ) { if ( r % i == 0 ) { s = false ; break ; } } return s ; } static void moranNo ( int n ) { int dup = n ; int sum = digSum ( dup ) ; if ( n % sum == 0 ) { int c = n / sum ; if ( isPrime ( c ) ) { System . out . println ( " Yes " ) ; return ; } } System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int n = 21 ; moranNo ( n ) ; } }
import java . lang . Math ; class GFG { public static boolean ishendecagonal ( int N ) { double n = ( 7 + Math . sqrt ( 72 * N + 49 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 11 ; if ( ishendecagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . lang . Math ; class GFG { public static boolean ishexadecagonal ( int N ) { double n = ( 12 + Math . sqrt ( 112 * N + 144 ) ) / 28 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 16 ; if ( ishexadecagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . lang . Math ; class GFG { public static boolean isnonagonal ( int N ) { double n = ( 5 + Math . sqrt ( 56 * N + 25 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 9 ; if ( isnonagonal ( N ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int nCr ( int n , int r ) { if ( r > n ) return 0 ; return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int countSubsequences ( int arr [ ] , int n , int k ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) countOdd ++ ; } int ans = nCr ( n , k ) - nCr ( countOdd , k ) ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 } ; int K = 1 ; int N = arr . length ; System . out . println ( countSubsequences ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void divParts ( int N ) { if ( N % 2 == 0 ) System . out . println ( ( N / 2 ) - 1 ) ; else System . out . println ( ( N / 2 ) ) ; } public static void main ( String [ ] args ) { int N = 8 ; divParts ( N ) ; } }
class GFG { public static void constructArray ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 ; constructArray ( N ) ; } }
import java . lang . Math ; class GFG { public static boolean isCentereddecagonal ( int N ) { double n = ( 5 + Math . sqrt ( 20 * N + 5 ) ) / 10 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int n = 11 ; if ( isCentereddecagonal ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 6 ) == 0 ) { return false ; } } return true ; } static boolean SexyPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 6 ) ; } static int countSexyPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( SexyPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 5 , 11 , 13 } ; int n = arr . length ; System . out . print ( countSexyPairs ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int findFirstNumIndex ( int k , int n ) { if ( n == 1 ) return 0 ; n -- ; int first_num_index ; int n_partial_fact = n ; while ( k >= n_partial_fact && n > 1 ) { n_partial_fact = n_partial_fact * ( n - 1 ) ; n -- ; } first_num_index = k / n_partial_fact ; k = k % n_partial_fact ; return first_num_index ; } static String findKthPermutation ( int n , int k ) { String ans = " " ; HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 1 ; i <= n ; i ++ ) s . add ( i ) ; Vector < Integer > v = new Vector < > ( ) ; v . addAll ( s ) ; int itr = v . elementAt ( 0 ) ; k = k - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int index = findFirstNumIndex ( k , n - i ) ; if ( index < v . size ( ) ) { ans += ( ( v . elementAt ( index ) . toString ( ) ) ) ; v . remove ( index ) ; } else ans += String . valueOf ( itr + 2 ) ; itr = v . elementAt ( 0 ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 3 , k = 4 ; String kth_perm_seq = findKthPermutation ( n , k ) ; System . out . print ( kth_perm_seq + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static double nDigitPerfectSquares ( int n , int b ) { double largest = Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( b , n ) ) ) - 1 , 2 ) ; return largest ; } public static void main ( String [ ] args ) { int N = 1 , B = 8 ; System . out . println ( nDigitPerfectSquares ( N , B ) ) ; } }
class GFG { static double cubeRoot ( double n ) { double ans = Math . pow ( 3 , ( ( 1.0 / 3 ) * ( Math . log ( n ) / Math . log ( 3 ) ) ) ) ; return ans ; } public static void main ( String [ ] args ) { double N = 8 ; System . out . printf ( " % .2f " , cubeRoot ( N ) ) ; } }
class GFG { public static int floorMax ( int A , int B , int N ) { int x = Math . min ( B - 1 , N ) ; return ( A * x ) / B ; } public static void main ( String [ ] args ) { int A = 11 , B = 10 , N = 9 ; System . out . println ( floorMax ( A , B , N ) ) ; } }
class GFG { public static int calculateMoves ( int n ) { int count = 0 ; int layers = n / 2 ; for ( int k = 1 ; k < layers + 1 ; k ++ ) { count += 8 * k * k ; } return count ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( calculateMoves ( N ) ) ; } }
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 " ) ; } } }
import java . lang . Math ; class GFG { public static boolean isCenteredheptagonal ( int N ) { double n = ( 7 + Math . sqrt ( 56 * N - 7 ) ) / 14 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int n = 8 ; if ( isCenteredheptagonal ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . lang . Math ; class GFG { public static boolean isCenteredOctadecagonal ( int N ) { double n = ( 9 + Math . sqrt ( 36 * N + 45 ) ) / 18 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isCenteredOctadecagonal ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static double kthRoot ( double n , int k ) { return Math . pow ( k , ( 1.0 / k ) * ( Math . log ( n ) / Math . log ( k ) ) ) ; } public static void main ( String [ ] args ) { double N = 8.0 ; int K = 3 ; System . out . print ( kthRoot ( N , K ) ) ; } }
class GFG { static boolean isPower ( int N , int K ) { int res1 = ( int ) ( Math . log ( N ) / Math . log ( K ) ) ; double res2 = Math . log ( N ) / Math . log ( K ) ; return ( res1 == res2 ) ; } public static void main ( String [ ] args ) { int N = 8 ; int K = 2 ; if ( isPower ( N , K ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . io . * ; import java . util . * ; class GFG { static int findMaxSize ( int a [ ] , int n ) { ArrayList < Integer > frq = new ArrayList < Integer > ( n + 1 ) ; for ( int i = 0 ; i <= n ; i ++ ) frq . add ( 0 ) ; for ( int i = 0 ; i < n ; ++ i ) frq . set ( a [ i ] , frq . get ( a [ i ] ) + 1 ) ; int maxfrq = Collections . max ( frq ) ; int dist = n + 1 - Collections . frequency ( frq , 0 ) ; int ans1 = Math . min ( maxfrq - 1 , dist ) ; int ans2 = Math . min ( maxfrq , dist - 1 ) ; int ans = Math . max ( ans1 , ans2 ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 4 , 1 , 4 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMaxSize ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int countElement ( int arr [ ] , int n ) { int freq [ ] = new int [ n + 1 ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int tmpsum = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { tmpsum += arr [ j ] ; if ( tmpsum <= n ) { ans += freq [ tmpsum ] ; freq [ tmpsum ] = 0 ; } } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( countElement ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void first_digit ( int x , int y ) { int length = ( int ) ( Math . log ( x ) / Math . log ( y ) + 1 ) ; int first_digit = ( int ) ( x / Math . pow ( y , length - 1 ) ) ; System . out . println ( first_digit ) ; } public static void main ( String args [ ] ) { int X = 55 , Y = 3 ; first_digit ( X , Y ) ; } }
import java . util . * ; class GFG { static double kthRoot ( double n , int k ) { return Math . pow ( k , ( ( 1.0 / k ) * ( Math . log ( n ) / Math . log ( k ) ) ) ) ; } public static void main ( String args [ ] ) { double n = 81 ; int k = 4 ; System . out . printf ( " % .6f " , kthRoot ( n , k ) ) ; } }
class GFG { static void checksum ( int n , int k ) { float first_term = ( float ) ( ( ( 2 * n ) / k + ( 1 - k ) ) / 2.0 ) ; if ( first_term - ( int ) ( first_term ) == 0 ) { for ( int i = ( int ) first_term ; i <= first_term + k - 1 ; i ++ ) { System . out . print ( i + " ▁ " ) ; } } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 33 , k = 6 ; checksum ( n , k ) ; } }
class GFG { static void distributeN ( int A , int B , int C , int n ) { int max1 = Math . max ( A , B ) ; int max2 = Math . max ( B , C ) ; int maximum = Math . max ( max1 , max2 ) ; int sum = A + B + C ; int p = ( 3 * maximum ) - sum ; int diff = n - p ; if ( diff < 0 diff % 3 == 0 ) System . out . print ( " No " ) ; else System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 20 ; int C = 15 , n = 14 ; distributeN ( A , B , C , n ) ; } }
import java . util . * ; class GFG { static void sumOddNumbers ( int N , int K ) { int check = N - ( K - 1 ) ; if ( check > 0 && check % 2 == 1 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { System . out . print ( "1 ▁ " ) ; } System . out . print ( + check ) ; } else System . out . println ( " - 1 ▁ " ) ; } public static void main ( String args [ ] ) { int N = 5 ; int K = 3 ; sumOddNumbers ( N , K ) ; } }
import java . io . * ; class GFG { public static int LongestMountain ( int a [ ] ) { int i = 0 , j = - 1 , k = - 1 , d = 0 ; if ( a . length < 3 ) return 0 ; for ( i = 0 ; i < a . length - 1 ; i ++ ) { if ( a [ i + 1 ] > a [ i ] ) { if ( k != - 1 ) { k = - 1 ; j = - 1 ; } if ( j == - 1 ) j = i ; } else { if ( a [ i + 1 ] < a [ i ] ) { if ( j != - 1 ) k = i + 1 ; if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } } else { k = - 1 ; j = - 1 ; } } } if ( k != - 1 && j != - 1 ) { if ( d < k - j + 1 ) d = k - j + 1 ; } return d ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 8 , 7 , 6 , 5 } ; System . out . println ( LongestMountain ( a ) ) ; } }
class GFG { public static String solve ( int [ ] A ) { int i , ctr = 0 ; for ( i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] % 2 == 1 ) { ctr ++ ; } } if ( ctr % 2 == 1 ) { return " No " ; } else { return " Yes " ; } } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 17 } ; System . out . println ( solve ( arr ) ) ; } }
class GFG { static int countSquares ( int n ) { int r = ( int ) Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) ; int l = ( int ) Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) ; return r - l ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( countSquares ( n ) ) ; } }
import java . util . * ; class GFG { static int summation ( int N ) { int co = ( N + 1 ) / 2 ; int ce = ( N ) / 2 ; int se = 2 * ( ( ce * ( ce + 1 ) ) * ( ce * ( ce + 1 ) ) ) ; int so = ( co * co ) * ( 2 * ( ( co * co ) ) - 1 ) ; return so - se ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( summation ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isStar ( int N ) { double n = ( 6 + Math . sqrt ( 24 * N + 12 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int i = 13 ; if ( isStar ( i ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static void sumEvenNumbers ( int N , int K ) { int check = N - 2 * ( K - 1 ) ; if ( check > 0 && check % 2 == 0 ) { for ( int i = 0 ; i < K - 1 ; i ++ ) { System . out . print ( "2 ▁ " ) ; } System . out . println ( check ) ; } else { System . out . println ( " - 1" ) ; } } public static void main ( String args [ ] ) { int N = 8 ; int K = 2 ; sumEvenNumbers ( N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getCount ( int arr [ ] , int n ) { int count = 0 ; for ( int j = 1 ; j < n - 1 ; j ++ ) { int p = 0 , q = 0 ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) p ++ ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] % arr [ j ] == 0 ) q ++ ; } count += p * q ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int N = arr . length ; System . out . println ( getCount ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static float computeMaxValue ( float arr [ ] , int n ) { float ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { float val = Math . max ( arr [ i ] / arr [ j ] , arr [ j ] / arr [ i ] ) ; ans = Math . max ( ans , val ) ; } } return ans ; } public static void main ( String [ ] args ) { float arr [ ] = { 15 , 10 , 3 , 2 } ; int N = arr . length ; System . out . println ( computeMaxValue ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void checkIfCurzonNumber ( long N ) { double powerTerm , productTerm ; powerTerm = Math . pow ( 2 , N ) + 1 ; productTerm = 2 * N + 1 ; if ( powerTerm % productTerm == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { long N = 5 ; checkIfCurzonNumber ( N ) ; N = 10 ; checkIfCurzonNumber ( N ) ; } }
class GFG { static int kthNonDivisible ( int N , int K ) { return ( int ) ( K + Math . floor ( ( K - 1 ) / ( N - 1 ) ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 6 ; System . out . print ( kthNonDivisible ( N , K ) ) ; } }
class GFG { static int minCount ( int n ) { int [ ] hasharr = { 10 , 3 , 6 , 9 , 2 , 5 , 8 , 1 , 4 , 7 } ; if ( n > 69 ) return hasharr [ n % 10 ] ; else { if ( n >= hasharr [ n % 10 ] * 7 ) return ( hasharr [ n % 10 ] ) ; else return - 1 ; } } public static void main ( String [ ] args ) { int n = 38 ; System . out . println ( minCount ( n ) ) ; } }
class GFG { static void NumbertoCharacter ( int n ) { int rev = 0 , r = 0 ; while ( n > 0 ) { r = n % 10 ; rev = rev * 10 + r ; n = n / 10 ; } while ( rev > 0 ) { r = rev % 10 ; switch ( r ) { case 1 : System . out . print ( " one ▁ " ) ; break ; case 2 : System . out . print ( " two ▁ " ) ; break ; case 3 : System . out . print ( " three ▁ " ) ; break ; case 4 : System . out . print ( " four ▁ " ) ; break ; case 5 : System . out . print ( " five ▁ " ) ; break ; case 6 : System . out . print ( " six ▁ " ) ; break ; case 7 : System . out . print ( " seven ▁ " ) ; break ; case 8 : System . out . print ( " eight ▁ " ) ; break ; case 9 : System . out . print ( " nine ▁ " ) ; break ; case 0 : System . out . print ( " zero ▁ " ) ; break ; default : System . out . print ( " UnValid ▁ " ) ; break ; } rev = rev / 10 ; } } public static void main ( String [ ] args ) { int n = 12345 ; NumbertoCharacter ( n ) ; } }
import java . util . * ; class GFG { static int m = 4 ; static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void nCr ( int n , long p , int f [ ] [ ] ) { for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { if ( j > i ) f [ i ] [ j ] = 0 ; else if ( j == 0 j == i ) f [ i ] [ j ] = 1 ; else f [ i ] [ j ] = ( f [ i - 1 ] [ j ] + f [ i - 1 ] [ j - 1 ] ) % ( int ) p ; } } } static void operations ( int arr [ ] , int n , int f [ ] [ ] ) { long p = 1000000007 ; nCr ( n , p - 1 , f ) ; Arrays . sort ( arr ) ; long ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { long x = 0 ; for ( int j = 1 ; j <= m ; j ++ ) { if ( m % j == 0 ) x = ( x + ( f [ n - i - 1 ] [ m - j ] * f [ i ] [ j - 1 ] ) % ( p - 1 ) ) % ( p - 1 ) ; } ans = ( ( ans * power ( arr [ i ] , x , p ) ) % p ) ; } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 7 , 9 , 3 } ; int n = arr . length ; int [ ] [ ] f = new int [ n + 1 ] [ m + 1 ] ; operations ( arr , n , f ) ; } }
import java . util . * ; class GFG { static boolean prodSquare ( int n ) { HashMap < Float , Float > s = new HashMap < Float , Float > ( ) ; for ( int i = 2 ; i * i <= n ; ++ i ) { s . put ( ( float ) ( i * i ) , ( float ) 1 ) ; if ( s . containsKey ( ( float ) n / ( i * i ) ) ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 25 ; if ( prodSquare ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int GetSum ( int n ) { Vector < Integer > divisors = new Vector < Integer > ( ) ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { divisors . add ( i ) ; if ( i != n / i ) { divisors . add ( n / i ) ; } } } int ans = 1 ; for ( int i = 0 ; i < divisors . size ( ) ; i ++ ) { ans *= ( divisors . get ( i ) + 1 ) ; } ans = ans - 1 ; return ans ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( GetSum ( N ) ) ; } }
import java . util . Scanner ; class contiguous_subarrays { public static int [ ] calculateWays ( int n ) { int x = 0 ; int [ ] v = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( n % 2 == 0 && i == n / 2 ) break ; x = n * ( i + 1 ) - ( i + 1 ) * i ; v [ i ] = x ; v [ n - i - 1 ] = x ; } return v ; } public static void printArray ( int [ ] v ) { for ( int i = 0 ; i < v . length ; i ++ ) System . out . print ( v [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] v ; v = calculateWays ( 4 ) ; printArray ( v ) ; } }
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static int longestPowerfulSubsequence ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 4 , 10 , 13 , 9 , 25 } ; int n = arr . length ; System . out . print ( longestPowerfulSubsequence ( arr , n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int maxOR ( int arr [ ] , int n ) { int max_value = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int number_of_bits = ( int ) ( ( Math . log ( max_value ) ) ) + 2 ; int complement = ( ( 1 << number_of_bits ) - 1 ) ^ max_value ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != max_value ) { c = Math . max ( c , ( complement & arr [ i ] ) ) ; } } return ( max_value + c ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 8 , 16 } ; int n = arr . length ; System . out . print ( maxOR ( arr , n ) ) ; } }
class GFG { static void update ( int res [ ] , int L , int R , int K ) { L -= 1 ; R -= 1 ; res [ L ] ^= K ; res [ R + 1 ] ^= K ; } static void display ( int arr [ ] , int res [ ] , int n ) { int i ; for ( i = 1 ; i < n ; i ++ ) { res [ i ] = res [ i ] ^ res [ i - 1 ] ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( ( arr [ i ] ^ res [ i ] ) + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 } ; int N = arr . length ; int res [ ] = new int [ N ] ; int L = 1 , R = 3 , K = 2 ; update ( res , L , R , K ) ; L = 2 ; R = 4 ; K = 3 ; update ( res , L , R , K ) ; display ( arr , res , N ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static boolean check ( int a [ ] , int n , int k ) { Map < Integer , Integer > m = new HashMap < > ( ) ; int extra = 0 ; for ( int i = k ; i < n ; i ++ ) m . put ( a [ i ] , m . getOrDefault ( a [ i ] , 0 ) + 1 ) ; for ( Integer x : m . values ( ) ) extra += x - 1 ; if ( extra == 0 ) return true ; for ( int i = 1 ; i + k - 1 < n ; i ++ ) { if ( m . get ( a [ i + k - 1 ] ) > 1 ) extra -- ; m . put ( a [ i + k - 1 ] , m . get ( a [ i + k - 1 ] ) - 1 ) ; m . put ( a [ i - 1 ] , m . get ( a [ i - 1 ] ) + 1 ) ; if ( m . get ( a [ i - 1 ] ) > 1 ) extra ++ ; if ( extra == 0 ) return true ; } return false ; } static int minlength ( int a [ ] , int n ) { int lo = 0 , hi = n + 1 ; int ans = 0 ; while ( lo < hi ) { int mid = ( lo + hi ) / 2 ; if ( check ( a , n , mid ) ) { ans = mid ; hi = mid ; } else lo = mid + 1 ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = a . length ; System . out . println ( minlength ( a , n ) ) ; } }
class GFG { static final int MAXN = 10000000 ; static int sumOfDigits ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } static int smallestNum ( int X , int Y ) { int res = - 1 ; for ( int i = X ; i < MAXN ; i ++ ) { int sum_of_digit = sumOfDigits ( i ) ; if ( sum_of_digit % Y == 0 ) { res = i ; break ; } } return res ; } public static void main ( String [ ] args ) { int X = 5923 , Y = 13 ; System . out . print ( smallestNum ( X , Y ) ) ; } }
import java . util . * ; class ComputeElectricityBill { public static int calculateBill ( int units ) { if ( units <= 100 ) { return units * 10 ; } else if ( units <= 200 ) { return ( 100 * 10 ) + ( units - 100 ) * 15 ; } else if ( units <= 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( units - 200 ) * 20 ; } else if ( units > 300 ) { return ( 100 * 10 ) + ( 100 * 15 ) + ( 100 * 20 ) + ( units - 300 ) * 25 ; } return 0 ; } public static void main ( String args [ ] ) { int units = 250 ; System . out . println ( calculateBill ( units ) ) ; } }
class GFG { static void factors ( int n ) { int i , j ; System . out . print ( "1 -->1NEW_LINE"); for ( i = 2 ; i <= n ; i ++ ) { System . out . print ( i + " ▁ - - > " ) ; for ( j = 1 ; j <= i / 2 ; j ++ ) { if ( i % j == 0 ) System . out . print ( j + " , ▁ " ) ; } System . out . print ( i + "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 5 ; factors ( n ) ; } }
import java . util . * ; class GFG { static void factors ( int n ) { int i , j ; System . out . print ( "1 -->1NEW_LINE"); for ( i = 2 ; i <= n ; i ++ ) { System . out . print ( i + " ▁ - - > " ) ; for ( j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { System . out . print ( j + " , ▁ " ) ; if ( i / j != j ) System . out . print ( i / j + " , ▁ " ) ; } } System . out . print ( "NEW_LINE"); } } public static void main ( String args [ ] ) { int n = 5 ; factors ( n ) ; } }
import java . util . * ; class GFG { static int MAX = ( int ) 1e5 ; static Vector < Integer > [ ] divisor = new Vector [ MAX + 1 ] ; static void sieve ( ) { for ( int i = 1 ; i <= MAX ; ++ i ) { for ( int j = i ; j <= MAX ; j += i ) divisor [ j ] . add ( i ) ; } } static void findNFactors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i + " - - > " ) ; for ( int divi : divisor [ i ] ) { System . out . print ( divi + " , ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 5 ; for ( int i = 0 ; i < divisor . length ; i ++ ) divisor [ i ] = new Vector < Integer > ( ) ; sieve ( ) ; findNFactors ( n ) ; } }
class GFG { static int countPrimeFactors ( int n ) { if ( n == 1 ) return 0 ; int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { cnt ++ ; n = n / i ; } } if ( n > 2 ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int x = 8 ; int prime_factor_cnt = countPrimeFactors ( x ) ; System . out . print ( prime_factor_cnt + "NEW_LINE"); } }
import java . util . * ; class GFG { static int countPalindromic ( int n ) { int count ; if ( n % 2 == 1 ) { count = ( int ) Math . pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = ( int ) Math . pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countPalindromic ( n ) ) ; } }
class GFG { static int countSubArrayWithOddProduct ( int A [ ] , int N ) { int count = 0 ; int last = - 1 ; int K = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) { K = ( i - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; last = i ; } } K = ( N - last - 1 ) ; count += ( K * ( K + 1 ) / 2 ) ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 15 , 7 , 3 , 25 , 6 , 2 , 1 , 1 , 7 } ; int n = arr . length ; System . out . println ( countSubArrayWithOddProduct ( arr , n ) ) ; } }
class GFG { static void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } System . out . print ( min_xor + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . length ; findMinXORSubarray ( arr , n , k ) ; } }
import java . util . * ; class GFG { static String getNumber ( int a , int b ) { String X = Integer . toString ( a ) ; String Y = Integer . toString ( b ) ; String XY = X + Y ; String YX = Y + X ; return XY . compareTo ( YX ) > 0 ? XY : YX ; } static void printMaxPair ( int arr [ ] , int n ) { int largest = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int number = Integer . parseInt ( getNumber ( arr [ i ] , arr [ j ] ) ) ; largest = Math . max ( largest , number ) ; } System . out . println ( largest ) ; } public static void main ( String [ ] args ) { int a [ ] = { 23 , 14 , 16 , 25 , 3 , 9 } ; int n = a . length ; printMaxPair ( a , n ) ; } }
import java . util . * ; class GFG { static void findMaxPrimeDivisor ( int n ) { int max_possible_prime = 0 ; while ( n % 2 == 0 ) { max_possible_prime ++ ; n = n / 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { max_possible_prime ++ ; n = n / i ; } } if ( n > 2 ) { max_possible_prime ++ ; } System . out . print ( max_possible_prime + "NEW_LINE"); } public static void main ( String [ ] args ) { int n = 4 ; findMaxPrimeDivisor ( n ) ; } }
import java . util . * ; class GFG { static int previousPerfectSquare ( int N ) { int prevN = ( int ) Math . floor ( Math . sqrt ( N ) ) ; if ( prevN * prevN == N ) prevN -= 1 ; return prevN * prevN ; } static int previousPerfectCube ( int N ) { int prevN = ( int ) Math . floor ( Math . cbrt ( N ) ) ; if ( prevN * prevN * prevN == N ) prevN -= 1 ; return prevN * prevN * prevN ; } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( previousPerfectSquare ( n ) ) ; System . out . println ( previousPerfectCube ( n ) ) ; } }
import java . util . * ; class GFG { static int findCount ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] % 2 == 0 ) && ( arr [ j ] % 2 == 1 ) ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = a . length ; System . out . print ( findCount ( a , n ) ) ; } }
class GFG { static int findCount ( int arr [ ] , int n ) { int count = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { count ++ ; } else { ans = ans + count ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 1 , 2 , 3 } ; int n = a . length ; System . out . print ( findCount ( a , n ) ) ; } }
public class GFG { private static long product ( int [ ] arr ) { double occurrence = Math . pow ( 2 , arr . length - 1 ) ; double product = 1 ; for ( int i = 0 ; i < arr . length ; i ++ ) { product *= Math . pow ( arr [ i ] , occurrence ) ; } return ( long ) product ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 3 , 7 } ; System . out . println ( product ( arr ) ) ; } }
class GFG { static int MOD = 1000000007 ; static int power ( int a , int b ) { if ( b == 0 ) return 1 ; int temp = power ( a , b / 2 ) ; temp = ( temp * temp ) % MOD ; if ( b % 2 != 0 ) { temp = ( temp * a ) % MOD ; } return temp ; } static void palindromicSum ( int N ) { int sum = 0 , res , ways ; if ( N == 1 ) { System . out . print ( "9" + "NEW_LINE"); return ; } if ( N == 2 ) { System . out . print ( "99" + "NEW_LINE"); return ; } ways = N / 2 ; if ( N % 2 == 0 ) ways -- ; res = power ( 9 , ways - 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum = sum * 10 + 45 * res ; sum %= MOD ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 3 ; palindromicSum ( N ) ; } }
class GFG { static int joinNumbers ( int numA , int numB ) { int revB = 0 ; while ( numB > 0 ) { revB = revB * 10 + ( numB % 10 ) ; numB = numB / 10 ; } while ( revB > 0 ) { numA = numA * 10 + ( revB % 10 ) ; revB = revB / 10 ; } return numA ; } static int findMaxSum ( int A [ ] , int B [ ] , int n ) { int maxArr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { int X = joinNumbers ( A [ i ] , B [ i ] ) ; int Y = joinNumbers ( B [ i ] , A [ i ] ) ; int mx = Math . max ( X , Y ) ; maxArr [ i ] = mx ; } int maxAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { maxAns += maxArr [ i ] ; } return maxAns ; } public static void main ( String args [ ] ) { int N = 5 ; int A [ ] = { 11 , 23 , 38 , 43 , 59 } ; int B [ ] = { 36 , 24 , 17 , 40 , 56 } ; System . out . println ( findMaxSum ( A , B , N ) ) ; } }
class GFG { static void createSets ( long n ) { if ( n <= 2 ) { System . out . print ( " - 1" ) ; return ; } else { long x = ( n % 2 == 0 ) ? ( n / 2 ) : ( ( n + 1 ) / 2 ) ; System . out . print ( x + "NEW_LINE"); for ( int i = 1 ; i <= n ; i ++ ) { if ( i == x ) continue ; System . out . print ( i + " ▁ " ) ; } } return ; } public static void main ( String [ ] args ) { long N = 7 ; createSets ( N ) ; } }
class GFG { static int calcCount ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( k % arr [ i ] == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 5 , 6 } ; int n = arr . length ; int k = 6 ; System . out . print ( calcCount ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static void modifiedBinaryPattern ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( j == 1 j == i ) System . out . print ( 1 ) ; else System . out . print ( 0 ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 7 ; modifiedBinaryPattern ( n ) ; } }
import java . util . * ; class GFG { static boolean isPerfect ( int n ) { int sum = 1 ; int i ; for ( i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } static int contiguousPerfectNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( isPerfect ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = arr . length ; System . out . print ( contiguousPerfectNumber ( arr , n ) ) ; } }
import java . util . * ; class solution { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static int contiguousPowerfulNumber ( int [ ] arr , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerful ( arr [ i ] ) ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 7 , 36 , 4 , 6 , 28 , 4 } ; int n = arr . length ; System . out . println ( contiguousPowerfulNumber ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int maxSum ( int arr [ ] , int K , int start , int end , int max_sum ) { if ( K == 0 ) return max_sum ; int max_sum_start = max_sum + arr [ start ] ; int max_sum_end = max_sum + arr [ end ] ; int ans = Math . max ( maxSum ( arr , K - 1 , start + 1 , end , max_sum_start ) , maxSum ( arr , K - 1 , start , end - 1 , max_sum_end ) ) ; return ans ; } static void maximizeSum ( int arr [ ] , int K , int n ) { int max_sum = 0 ; int start = 0 ; int end = n - 1 ; System . out . print ( maxSum ( arr , K , start , end , max_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . length ; maximizeSum ( arr , K , n ) ; } }
import java . util . Scanner ; import java . util . Arrays ; class GFG { public static int maxPointCount ( int arr [ ] , int K , int size ) { int curr_points = 0 ; int max_points = 0 ; for ( int i = 0 ; i < K ; i ++ ) curr_points += arr [ i ] ; max_points = curr_points ; int j = size - 1 ; for ( int i = K - 1 ; i >= 0 ; i -- ) { curr_points = curr_points + arr [ j ] - arr [ i ] ; max_points = Math . max ( curr_points , max_points ) ; j -- ; } return max_points ; } public static void main ( String args [ ] ) { int [ ] arr = { 8 , 4 , 4 , 8 , 12 , 3 , 2 , 9 } ; int K = 3 ; int n = arr . length ; System . out . print ( maxPointCount ( arr , K , n ) ) ; } }
import java . util . * ; class GFG { static int countPalindrome ( int n ) { int count ; if ( n % 2 == 1 ) { count = ( int ) Math . pow ( 9 , ( n - 1 ) / 2 ) ; } else { count = ( int ) Math . pow ( 9 , ( n - 2 ) / 2 ) ; } return count ; } static int sumPalindrome ( int n ) { int count = countPalindrome ( n ) ; int res = 0 ; if ( n == 1 ) return 9 ; if ( n == 2 ) return 99 ; for ( int i = 0 ; i < n ; i ++ ) { res = res * 10 + count * 5 ; } return res ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( sumPalindrome ( n ) ) ; } }
class GFG { static int count ( int N , int B ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += ( B - 1 ) * Math . pow ( B , i - 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int N = 2 , B = 10 ; System . out . print ( count ( N , B ) ) ; } }
public class GFG { static long SmallestPerfectSquare ( long N ) { long X = ( long ) 1e9 ; long ans ; for ( int i = 1 ; i < Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) { long a = i ; long b = N / i ; if ( ( b - a != 0 ) && ( ( b - a ) % 2 == 0 ) ) { X = Math . min ( X , ( b - a ) / 2 ) ; } } } if ( X != 1e9 ) ans = X * X ; else ans = - 1 ; return ans ; } public static void main ( String [ ] args ) { long N = 3 ; System . out . println ( SmallestPerfectSquare ( N ) ) ; } }
import java . util . * ; import java . util . Arrays ; class GFG { static int minCost ( int arr [ ] , int n , int circumference ) { Arrays . sort ( arr ) ; int [ ] arr2 = new int [ 2 * n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr2 [ i ] = arr [ i ] ; arr2 [ i + n ] = arr [ i ] + circumference ; } int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) res = Math . min ( res , arr2 [ i + ( n - 1 ) ] - arr2 [ i ] ) ; return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 19 , 3 , 6 } ; int n = arr . length ; int circumference = 20 ; System . out . println ( minCost ( arr , n , circumference ) ) ; } }
class GFG { static int CountWays ( int n ) { int ans = ( n - 1 ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . print ( CountWays ( N ) ) ; } }
import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; else return __gcd ( b , a % b ) ; } static int MinimumGCD ( int arr [ ] , int n ) { int g = 0 ; for ( int i = 0 ; i < n ; i ++ ) { g = __gcd ( g , arr [ i ] ) ; } return g ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 3 } ; int N = arr . length ; System . out . println ( MinimumGCD ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int stepRequired ( int N ) { int cnt = 0 ; while ( N > 0 ) { N = N & ( N - 1 ) ; cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int N = 23 ; System . out . print ( stepRequired ( N ) ) ; } }
class GFG { static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } static void fibonacciSubarrays ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int sum = 0 ; for ( int j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 8 , 9 } ; int n = arr . length ; fibonacciSubarrays ( arr , n ) ; } }
import java . util . * ; class GFG { static long mod = 1000000007 ; static long factorial [ ] = new long [ 100005 ] ; static void StoreFactorials ( int n ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { factorial [ i ] = ( i * factorial [ i - 1 ] ) % mod ; } } static long Power ( long x , long y ) { long ans = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) { ans = ( ans * x ) % mod ; } x = ( x * x ) % mod ; y /= 2 ; } return ans ; } static long invmod ( long x ) { return Power ( x , mod - 2 ) ; } static long nCr ( int n , int r ) { return ( factorial [ n ] * invmod ( ( factorial [ r ] * factorial [ n - r ] ) % mod ) ) % mod ; } static long CountWays ( int n , int k ) { StoreFactorials ( n ) ; long ans = 0 ; for ( int i = k ; i >= 0 ; i -- ) { if ( i % 2 == k % 2 ) { ans = ( ans + ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } else { ans = ( ans + mod - ( Power ( i , n ) * nCr ( k , i ) ) % mod ) % mod ; } } ans = ( ans * nCr ( n , k ) ) % mod ; return ans ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 5 ; System . out . print ( CountWays ( N , K ) + "NEW_LINE"); } }
class GFG { static void PrintReverseOrder ( int N ) { if ( N <= 0 ) { return ; } else { System . out . print ( N + " ▁ " ) ; PrintReverseOrder ( N - 1 ) ; } } public static void main ( String [ ] args ) { int N = 5 ; PrintReverseOrder ( N ) ; } }
class GFG { static void XOROfElements ( int arr [ ] , int n ) { int FirstHalfXOR = 0 ; int SecondHalfXOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n / 2 ) FirstHalfXOR ^= arr [ i ] ; else SecondHalfXOR ^= arr [ i ] ; } System . out . print ( FirstHalfXOR + " , " + SecondHalfXOR + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 30 , 50 , 10 , 55 , 15 , 42 } ; int N = arr . length ; XOROfElements ( arr , N ) ; } }
class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int modInverse ( int n , int p ) { return power ( n , p - 2 , p ) ; } static int nCrModPFermat ( int n , int r , int p ) { if ( r == 0 ) return 1 ; if ( n < r ) return 0 ; int fac [ ] = new int [ n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ n - r ] , p ) % p ) % p ; } static int SumOfXor ( int a [ ] , int n ) { int mod = 10037 ; int answer = 0 ; for ( int k = 0 ; k < 32 ; k ++ ) { int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & ( 1 << k ) ) != 0 ) x ++ ; else y ++ ; } answer += ( ( 1 << k ) % mod * ( nCrModPFermat ( x , 3 , mod ) + x * nCrModPFermat ( y , 2 , mod ) ) % mod ) % mod ; } return answer ; } public static void main ( String [ ] args ) { int n = 5 ; int A [ ] = { 3 , 5 , 2 , 18 , 7 } ; System . out . println ( SumOfXor ( A , n ) ) ; } }
class GFG { static final int N = 100005 ; static int [ ] phi = new int [ N ] ; static int [ ] pref = new int [ N ] ; static void precompute ( ) { phi [ 1 ] = 0 ; for ( int i = 2 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void prefix ( ) { for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + phi [ i ] ; } static void find_pairs ( int n ) { int total = ( n * ( n - 1 ) ) / 2 ; int ans = total - pref [ n ] ; System . out . print ( " Number ▁ of ▁ pairs ▁ from ▁ 1 ▁ to ▁ " + n + " ▁ are ▁ " + ans + "NEW_LINE"); } public static void main ( String [ ] args ) { precompute ( ) ; prefix ( ) ; int q [ ] = { 5 , 7 } ; int n = q . length ; for ( int i = 0 ; i < n ; i ++ ) { find_pairs ( q [ i ] ) ; } } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void balance ( int x , int y , int p , int q ) { int b1 , b2 , b3 ; if ( p % x == 0 && q % y == 0 ) { b1 = p / x ; b2 = q / y ; b3 = 1 ; } else { p = p * y ; q = q * x ; b3 = x * y ; int temp = gcd ( p , gcd ( q , b3 ) ) ; b1 = p / temp ; b2 = q / temp ; b3 = b3 / temp ; } System . out . print ( b1 + " ▁ " + b2 + " ▁ " + b3 + "NEW_LINE"); } public static void main ( String [ ] args ) { int x = 2 , y = 3 , p = 4 , q = 5 ; balance ( x , y , p , q ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxAlternatingSum ( int [ ] arr , int n ) { int max_sum = 0 ; int i = 0 ; while ( i < n ) { int current_max = arr [ i ] ; int k = i ; while ( k < n && ( ( arr [ i ] > 0 && arr [ k ] > 0 ) || ( arr [ i ] < 0 && arr [ k ] < 0 ) ) ) { current_max = Math . max ( current_max , arr [ k ] ) ; k += 1 ; } max_sum += current_max ; i = k ; } return max_sum ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 3 , 4 , - 1 , - 2 } ; int n = arr . length ; System . out . println ( maxAlternatingSum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int subarrayLength ( int A [ ] , int R [ ] [ ] , int N , int M ) { for ( int i = 0 ; i < M ; ++ i ) { int l = R [ i ] [ 0 ] , r = R [ i ] [ 1 ] + 1 ; l -- ; r -- ; A [ l ] ++ ; if ( r < N ) A [ r ] -- ; } for ( int i = 1 ; i < N ; ++ i ) { A [ i ] += A [ i - 1 ] ; } int count = 0 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { if ( ! s . contains ( A [ i ] ) ) count ++ ; s . add ( A [ i ] ) ; } int [ ] repeat = new int [ count + 1 ] ; int ans = N ; int counter = 0 , left = 0 , right = 0 ; while ( right < N ) { int cur_element = A [ right ] ; repeat [ cur_element ] += 1 ; if ( repeat [ cur_element ] == 1 ) ++ counter ; while ( counter == count ) { ans = Math . min ( ans , right - left + 1 ) ; cur_element = A [ left ] ; repeat [ cur_element ] -= 1 ; ++ left ; if ( repeat [ cur_element ] == 0 ) -- counter ; } ++ right ; } return ans ; } public static void main ( String [ ] args ) { int N = 8 , queries = 6 ; int Q [ ] [ ] = { { 1 , 4 } , { 3 , 4 } , { 4 , 5 } , { 5 , 5 } , { 7 , 8 } , { 8 , 8 } } ; int A [ ] = new int [ N ] ; System . out . print ( subarrayLength ( A , Q , N , queries ) ) ; } }
import java . util . * ; class GFG { static void ModifiedSieveOfEratosthenes ( int N , boolean Expo_Prime [ ] ) { boolean [ ] primes = new boolean [ N ] ; Arrays . fill ( primes , true ) ; for ( int i = 2 ; i < N ; i ++ ) { if ( primes [ i ] ) { int no = i ; while ( no <= N ) { Expo_Prime [ no ] = true ; no *= i ; } for ( int j = i * i ; j < N ; j += i ) primes [ j ] = false ; } } } static void Display ( int arr [ ] , boolean Expo_Prime [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( Expo_Prime [ arr [ i ] ] ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void FindExpoPrime ( int arr [ ] , int n ) { int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) max = arr [ i ] ; } boolean [ ] Expo_Prime = new boolean [ max + 1 ] ; ModifiedSieveOfEratosthenes ( max + 1 , Expo_Prime ) ; Display ( arr , Expo_Prime , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 9 , 16 , 1 , 3 , 12 , 36 , 625 , 1000 } ; int n = arr . length ; FindExpoPrime ( arr , n ) ; } }
class GFG { static int countSubarray ( int n ) { return ( ( n ) * ( n + 1 ) ) / 2 ; } static int countSubsequence ( int n ) { return ( int ) Math . pow ( 2 , n ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( ( countSubarray ( n ) ) + "NEW_LINE"); System . out . print ( ( countSubsequence ( n ) ) + "NEW_LINE"); } }
class GFG { static float y ( float x ) { return ( 1 / ( 1 + x ) ) ; } static float BooleRule ( float a , float b ) { int n = 4 ; int h ; h = ( int ) ( ( b - a ) / n ) ; float sum = 0 ; float bl = ( 7 * y ( a ) + 32 * y ( a + h ) + 12 * y ( a + 2 * h ) + 32 * y ( a + 3 * h ) + 7 * y ( a + 4 * h ) ) * 2 * h / 45 ; sum = sum + bl ; return sum ; } public static void main ( String [ ] args ) { System . out . printf ( ( " f ( x ) ▁ = ▁ % .4f " ) , BooleRule ( 0 , 4 ) ) ; } }
class GFG { static void convertToDecimal ( int N ) { System . out . print ( " Decimal ▁ number ▁ of ▁ " + N + " ▁ is : ▁ " ) ; if ( N != 0 ) { int decimalNumber = 0 , i = 0 , remainder ; while ( N != 0 ) { remainder = N % 10 ; N /= 10 ; decimalNumber += remainder * Math . pow ( 3 , i ) ; ++ i ; } System . out . print ( decimalNumber + "NEW_LINE"); } else System . out . print ( "0" + "NEW_LINE"); } public static void main ( String [ ] args ) { int Ternary = 10202202 ; convertToDecimal ( Ternary ) ; } }
class GFG { static boolean isPerfectSquare ( int N ) { double sr = Math . sqrt ( N ) ; return ( sr - Math . floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } for ( int i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : m / i ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { System . out . print ( " YES " + "NEW_LINE"); } else { System . out . print ( " NO " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }
import java . util . * ; class GFG { static float getPercentile ( int rank , int students ) { float result = ( float ) ( students - rank ) / students * 100 ; return result ; } public static void main ( String [ ] args ) { int your_rank = 805 ; int total_students = 97481 ; System . out . print ( getPercentile ( your_rank , total_students ) ) ; } }
class Sum { void sumOfPrevK ( int N , int K ) { int arr [ ] = new int [ N ] ; int prevsum = 0 ; arr [ 0 ] = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( i < K ) { arr [ i + 1 ] = arr [ i ] + prevsum ; prevsum = arr [ i + 1 ] ; } else { arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; prevsum = arr [ i + 1 ] ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { Sum s = new Sum ( ) ; int N = 8 , K = 3 ; s . sumOfPrevK ( N , K ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void findMaxGCD ( int arr [ ] , int n ) { int maxGCD = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int val = gcd ( arr [ i ] , arr [ i + 1 ] ) ; if ( val > maxGCD ) { maxGCD = val ; } } System . out . print ( maxGCD + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 18 , 9 , 9 , 5 , 15 , 8 , 7 , 6 , 9 } ; int n = arr . length ; findMaxGCD ( arr , n ) ; } }
class GFG { public static float round ( float var , int digit ) { float value = ( int ) ( var * Math . pow ( 10 , digit ) + .5 ) ; return ( float ) value / ( float ) Math . pow ( 10 , digit ) ; } public static int probability ( int N ) { int a = 2 ; int b = 3 ; if ( N == 1 ) { return a ; } else if ( N == 2 ) { return b ; } else { for ( int i = 3 ; i <= N ; i ++ ) { int c = a + b ; a = b ; b = c ; } return b ; } } public static float operations ( int N ) { int x = probability ( N ) ; int y = ( int ) Math . pow ( 2 , N ) ; return round ( ( float ) x / ( float ) y , 2 ) ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( ( operations ( N ) ) ) ; } }
import java . util . * ; class GFG { static float y ( float x ) { float num = 1 ; float denom = ( float ) 1.0 + x * x ; return num / denom ; } static float WeedleRule ( float a , float b ) { float h = ( b - a ) / 6 ; float sum = 0 ; sum = sum + ( ( ( 3 * h ) / 10 ) * ( y ( a ) + y ( a + 2 * h ) + 5 * y ( a + h ) + 6 * y ( a + 3 * h ) + y ( a + 4 * h ) + 5 * y ( a + 5 * h ) + y ( a + 6 * h ) ) ) ; return sum ; } public static void main ( String args [ ] ) { float a = 0 , b = 6 ; float num = WeedleRule ( a , b ) ; System . out . format ( " f ( x ) ▁ = ▁ " + " % .6f " , num ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int findSmallest ( int N ) { if ( N == 0 ) return 2 ; if ( N == 1 ) return 1 ; int prime = N , counter = 0 ; boolean found = false ; while ( ! found ) { if ( isPrime ( prime ) ) found = true ; else { prime ++ ; counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( findSmallest ( N ) ) ; } }
import java . util . * ; class GFG { static boolean checkPermutation ( int arr [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { long lsum = prefix [ i ] ; long rsum = sum - prefix [ i ] ; long l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } static void printPermutations ( int arr [ ] , int n , int l1 , int l2 ) { for ( int i = 0 ; i < l1 ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; for ( int i = l1 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void findPermutations ( int arr [ ] , int n ) { if ( ! checkPermutation ( arr , n ) ) { System . out . print ( " Not ▁ Possible " ) ; return ; } int l1 = 0 , l2 = 0 ; l1 = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; l2 = n - l1 ; HashSet < Integer > s1 = new HashSet < Integer > ( ) , s2 = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) s1 . add ( arr [ i ] ) ; for ( int i = l1 ; i < n ; i ++ ) s2 . add ( arr [ i ] ) ; if ( s1 . size ( ) == l1 && s2 . size ( ) == l2 ) printPermutations ( arr , n , l1 , l2 ) ; else { l1 = l1 + l2 ; l2 = l1 - l2 ; l1 = l1 - l2 ; printPermutations ( arr , n , l1 , l2 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 1 , 10 , 2 } ; int n = arr . length ; findPermutations ( arr , n ) ; } }
import java . util . * ; class GFG { static final int MOD = 1000000007 ; static int rangeSum ( int l , int r ) { int a = 1 , b = 9 , res = 0 ; for ( int i = 1 ; i <= 10 ; i ++ ) { int L = Math . max ( l , a ) ; int R = Math . min ( r , b ) ; if ( L <= R ) { int sum = ( L + R ) * ( R - L + 1 ) / 2 ; res += Math . pow ( i , i ) * ( sum % MOD ) ; res %= MOD ; } a = a * 10 ; b = b * 10 + 9 ; } return res ; } public static void main ( String [ ] args ) { int l = 98 , r = 102 ; System . out . print ( rangeSum ( l , r ) ) ; } }
import java . util . * ; class GFG { static int TrialDivision ( int N ) { int i = 2 ; int k = ( int ) Math . ceil ( Math . sqrt ( N ) ) ; while ( i <= k ) { if ( N % i == 0 ) return 0 ; i += 1 ; } return 1 ; } public static void main ( String [ ] args ) { int N = 49 ; int p = TrialDivision ( N ) ; if ( p != 0 ) System . out . print ( " Prime " ) ; else System . out . print ( " Composite " ) ; } }
class GFG { static int pairs ( int n ) { int [ ] [ ] c = new int [ 10 ] [ 10 ] ; int tmp = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i >= tmp * 10 ) tmp *= 10 ; c [ i / tmp ] [ i % 10 ] ++ ; } int ans = 0 ; for ( int i = 1 ; i < 10 ; i ++ ) for ( int j = 1 ; j < 10 ; j ++ ) ans += c [ i ] [ j ] * c [ j ] [ i ] ; return ans ; } public static void main ( String [ ] args ) { int n = 25 ; System . out . print ( pairs ( n ) ) ; } }
import java . util . * ; class GFG { static int countWays ( int A , int B , int C ) { int minAddA = Math . max ( 0 , ( C + B - A + 2 ) / 2 ) ; int count_ways = Math . max ( C - minAddA + 1 , 0 ) ; return count_ways ; } public static void main ( String args [ ] ) { int A = 3 , B = 5 , C = 5 ; System . out . println ( countWays ( A , B , C ) ) ; } }
import java . util . * ; class GFG { static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static int gcdFibonacciFreq ( int arr [ ] , int n ) { HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ) ; int i ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } int gcd = 0 ; for ( Map . Entry < Integer , Integer > it : m . entrySet ( ) ) { if ( hash . contains ( it . getValue ( ) ) ) { gcd = __gcd ( gcd , it . getKey ( ) ) ; } } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 6 , 5 , 6 , 6 , 5 , 5 } ; int n = arr . length ; System . out . print ( gcdFibonacciFreq ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void printSeries ( int N ) { int ith_term = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { ith_term = i % 2 == 0 ? 2 * i * i + 1 : 2 * i * i - 1 ; System . out . print ( ith_term + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 7 ; printSeries ( N ) ; } }
class GFG { static void findNthTerm ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { ans = ans + 6 ; } else { ans = ans + 2 ; } } System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 3 ; findNthTerm ( N ) ; } }
class GFG { static void printSeriesSum ( int N ) { long sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += Math . pow ( i , i - 1 ) ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 5 ; printSeriesSum ( N ) ; } }
class GFG { static void printSumSeries ( int N ) { float sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { sum += 1.0 / ( 2 * i - 1 ) ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int N = 6 ; printSumSeries ( N ) ; } }
class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; int a = 1 ; int cnt = 0 ; boolean flag = true ; sum += a ; while ( cnt < N ) { int nextElement ; if ( flag == true ) { nextElement = a * 2 ; sum += nextElement ; flag = ! flag ; } else { nextElement = a * 3 / 2 ; sum += nextElement ; flag = ! flag ; } a = nextElement ; cnt ++ ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int N = 8 ; printSeriesSum ( N ) ; } }
class GFG { static void MinimumSwapOperations ( String s ) { boolean zero_exist = false ; boolean multiple_of_2 = false ; int sum = 0 ; int index_of_zero = 0 ; boolean more_zero = false ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int val = s . charAt ( i ) - '0' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false sum % 3 != 0 ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int val = s . charAt ( i ) - '0' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { System . out . println ( " - 1" ) ; return ; } int last_val = s . charAt ( s . length ( ) - 1 ) - '0' ; int second_last_val = s . charAt ( s . length ( ) - 2 ) - '0' ; if ( last_val == 0 && second_last_val % 2 == 0 ) System . out . println ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) System . out . println ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) System . out . println ( 1 ) ; else System . out . println ( 2 ) ; } public static void main ( String [ ] args ) { String N = "20" ; MinimumSwapOperations ( N ) ; } }
class GFG { static final int MAX = 1000 ; static int max_prime [ ] = new int [ MAX ] ; static int min_prime [ ] = new int [ MAX ] ; static void sieve ( int n ) { for ( int i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static int findCost ( int A , int B , int C , int X ) { sieve ( MAX ) ; int N , M ; if ( X == 1 ) { return - 1 ; } int min_cost = C + X ; int cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { int cost_for_comp = B + M ; min_cost = Math . min ( min_cost , cost_for_comp ) ; } return min_cost ; } public static void main ( String [ ] args ) { int A = 7 , B = 11 , C = 2 , X = 20 ; System . out . println ( findCost ( A , B , C , X ) ) ; } }
class GFG { static int highestPower ( int n , int k ) { int i = 0 ; int a = ( int ) Math . pow ( n , i ) ; while ( a <= k ) { i += 1 ; a = ( int ) Math . pow ( n , i ) ; } return i - 1 ; } static int b [ ] = new int [ 50 ] ; static int PowerArray ( int n , int k ) { while ( k > 0 ) { int t = highestPower ( n , k ) ; if ( b [ t ] > 0 ) { System . out . print ( - 1 ) ; return 0 ; } else b [ t ] = 1 ; k -= Math . pow ( n , t ) ; } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] > 0 ) { System . out . print ( i + " , ▁ " ) ; } } return 0 ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; } }
import java . util . * ; class GFG { static double countdigits ( int n , int k ) { if ( n == 0 ) return 0 ; int digit = n % 10 ; if ( digit == k ) return 1 + countdigits ( n / 10 , k ) ; return countdigits ( n / 10 , k ) ; } public static void main ( String [ ] args ) { int n = 1000 ; int k = 0 ; System . out . println ( countdigits ( n , k ) ) ; } }
import java . util . * ; class GFG { static void product_subarrays ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int product = 1 ; for ( int j = i ; j < n ; j ++ ) { product = product * arr [ j ] ; res *= product ; } } System . out . println ( res + "NEW_LINE"); } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 3 , 7 } ; int n = arr . length ; product_subarrays ( arr , n ) ; } }
import java . util . * ; class GFG { static void printModulus ( int X , int Y ) { int d = Math . abs ( X - Y ) ; int i = 1 ; while ( i * i <= d ) { if ( d % i == 0 ) { System . out . print ( i + " ▁ " ) ; if ( d / i != i ) System . out . print ( d / i + " ▁ " ) ; } i ++ ; } } public static void main ( String [ ] args ) { int X = 10 ; int Y = 26 ; printModulus ( X , Y ) ; } }
import java . util . * ; class GFG { static boolean difSquare ( int n ) { if ( n % 4 != 2 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 45 ; if ( difSquare ( n ) ) { System . out . print ( "YesNEW_LINE"); } else { System . out . print ( "NoNEW_LINE"); } } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int res ; static int k ; static Node newNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static int rec ( Node root ) { if ( root == null ) { return 0 ; } xr ^= rec ( root . left ) ; xr ^= rec ( root . right ) ; if ( xr == k ) { res ++ ; } return xr ; } static int findCount ( Node root , int K ) { res = 0 ; k = K ; rec ( root ) ; return res ; } public static void main ( String args [ ] ) { Node root = newNode ( 2 ) ; root . left = newNode ( 1 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 10 ) ; root . left . right = newNode ( 5 ) ; int K = 5 ; System . out . println ( findCount ( root , K ) ) ; } }
class GFG { static void makearrayequal ( int arr [ ] , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x += ( arr [ i ] & 1 ) ; } System . out . println ( Math . min ( x , n - x ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 1 } ; int n = arr . length ; makearrayequal ( arr , n ) ; } }
import java . util . * ; class GFG { static final int N = 10005 ; static void SieveOfEratosthenes ( Vector < Boolean > composite ) { for ( int i = 0 ; i < N ; i ++ ) { composite . add ( i , false ) ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( ! composite . get ( p ) ) { for ( int i = p * 2 ; i < N ; i += p ) { composite . add ( i , true ) ; } } } } static int sumOfElements ( int arr [ ] , int n ) { Vector < Boolean > composite = new Vector < Boolean > ( ) ; for ( int i = 0 ; i < N ; i ++ ) composite . add ( false ) ; SieveOfEratosthenes ( composite ) ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } int sum = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( composite . get ( it . getValue ( ) ) ) { sum += ( it . getKey ( ) ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 } ; int n = arr . length ; System . out . print ( sumOfElements ( arr , n ) ) ; } }
class GFG { static int countDigit ( int n ) { int temp = n , count = 0 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d == 2 d == 3 d == 5 d == 7 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int n = 1234567890 ; System . out . println ( countDigit ( n ) ) ; } }
class GFG { static String permutation ( int arr [ ] , int N ) { int [ ] hash = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 5 , 5 , 3 } ; int n = arr . length ; System . out . print ( permutation ( arr , n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void remove ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ( mp . containsKey ( arr [ i ] ) && mp . get ( arr [ i ] ) % 2 == 1 ) ) continue ; System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 3 , 2 , 2 , 4 , 7 , 7 } ; int n = arr . length ; remove ( arr , n ) ; } }
import java . util . * ; class GFG { static final int N = 100005 ; static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int longestPrimeSubsequence ( int arr [ ] , int n ) { boolean [ ] prime = new boolean [ N + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , N ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 11 , 2 , 9 , 21 } ; int n = arr . length ; System . out . print ( longestPrimeSubsequence ( arr , n ) + "NEW_LINE"); } }
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' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void main ( String [ ] args ) { String N = "735" ; System . out . print ( even_or_odd ( N ) ) ; } }
class GFG { static double dydx ( double x , double y ) { return ( x + y - 2 ) ; } static double rungeKutta ( double x0 , double y0 , double x , double h ) { int n = ( int ) ( ( x - x0 ) / h ) ; double k1 , k2 ; double y = y0 ; for ( int i = 1 ; i <= n ; i ++ ) { k1 = h * dydx ( x0 , y ) ; k2 = h * dydx ( x0 + 0.5 * h , y + 0.5 * k1 ) ; y = y + ( 1.0 / 6.0 ) * ( k1 + 2 * k2 ) ; x0 = x0 + h ; } return y ; } public static void main ( String [ ] args ) { double x0 = 0 , y = 1 , x = 2 , h = 0.2 ; System . out . println ( rungeKutta ( x0 , y , x , h ) ) ; } }
class GFG { static final int size = 10001 ; static long [ ] fib = new long [ 100010 ] ; static int [ ] freq = new int [ 100010 ] ; static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static int isFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 1 ; return 0 ; } static void compute ( int a , int b ) { for ( int i = 1 ; i < 100010 ; i ++ ) { fib [ i ] = isFibonacci ( i ) ; } for ( int x = 1 ; x < 100010 ; x ++ ) { for ( int y = 1 ; y < size ; y ++ ) { if ( fib [ x ] == 1 && fib [ y ] == 1 && a * x + b * y < 100010 ) { freq [ a * x + b * y ] ++ ; } } } } public static void main ( String [ ] args ) { int Q = 2 , A = 5 , B = 10 ; compute ( A , B ) ; int arr [ ] = { 50 , 150 } ; for ( int i = 0 ; i < Q ; i ++ ) { System . out . print ( freq [ arr [ i ] ] + " ▁ " ) ; } } }
import java . util . * ; class GFG { static int MAX = 100 ; static int max_element ( int arr [ ] , int pos ) { int tmp = arr [ 0 ] ; int ind = 0 ; for ( int i = 1 ; i < pos ; i ++ ) { if ( tmp < arr [ i ] ) { tmp = arr [ i ] ; ind = i ; } } return ind ; } static void findMaxSum ( int n1 , int n2 ) { int [ ] arr1 = new int [ MAX ] ; int [ ] arr2 = new int [ MAX ] ; int l1 = 0 , l2 = 0 ; int max1 = Math . max ( n1 , n2 ) ; int min1 = Math . min ( n1 , n2 ) ; for ( int i = max1 ; i > 0 ; i /= 10 ) arr1 [ l1 ++ ] = ( i % 10 ) ; for ( int i = min1 ; i > 0 ; i /= 10 ) arr2 [ l2 ++ ] = ( i % 10 ) ; int f = 0 ; if ( l1 != l2 ) { int index = ( max_element ( arr2 , l2 ) ) ; for ( int i = l1 - 1 ; i > ( l2 - 1 ) ; i -- ) { if ( arr1 [ i ] < arr2 [ index ] ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index ] ; arr2 [ index ] = tmp ; f = 1 ; break ; } } } if ( f != 1 ) { int index1 = 0 , index2 = 0 ; int diff1 = 0 , diff2 = 0 ; for ( int i = l2 - 1 ; i >= 0 ; i -- ) { index1 = ( max_element ( arr1 , i ) ) ; index2 = ( max_element ( arr2 , i ) ) ; diff1 = ( arr2 [ index2 ] - arr1 [ i ] ) ; diff2 = ( arr1 [ index1 ] - arr2 [ i ] ) ; if ( diff1 > 0 diff2 > 0 ) { if ( diff1 > diff2 ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } else if ( diff2 > diff1 ) { int tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( diff1 == diff2 ) { if ( index1 <= index2 ) { int tmp = arr1 [ index1 ] ; arr1 [ index1 ] = arr2 [ i ] ; arr2 [ i ] = tmp ; break ; } else if ( index2 <= index1 ) { int tmp = arr1 [ i ] ; arr1 [ i ] = arr2 [ index2 ] ; arr2 [ index2 ] = tmp ; break ; } } } } } int f_n1 = 0 , f_n2 = 0 ; for ( int i = l1 - 1 ; i >= 0 ; i -- ) { f_n1 = ( f_n1 * 10 ) + arr1 [ i ] ; f_n2 = ( f_n2 * 10 ) + arr2 [ i ] ; } System . out . println ( f_n1 + f_n2 ) ; } public static void main ( String [ ] args ) { int N1 = 9987 ; int N2 = 123 ; findMaxSum ( N1 , N2 ) ; } }
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 ) ) ; } }
import java . util . * ; class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) > 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n - ( d * 3 ) ) ; } return ( n % 31 == 0 ) ; } public static void main ( String [ ] args ) { int N = 1922 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) > 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n + ( d * 13 ) ) ; } return ( n % 43 == 0 ) ; } public static void main ( String [ ] args ) { int N = 2795 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static void print ( int n , int k ) { System . out . print ( k + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { int x = ( int ) ( Math . pow ( k , i ) - Math . pow ( k , i - 1 ) ) ; System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 4 ; print ( N , K ) ; } }
import java . util . * ; class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) <= 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n - ( d * 7 ) ) ; } return ( n % 71 == 0 ) ; } public static void main ( String args [ ] ) { int N = 5041 ; if ( isDivisible ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int [ ] digit = new int [ 100000 ] ; static int findDigits ( int n ) { int count = 0 ; while ( n != 0 ) { digit [ count ] = n % 10 ; n = n / 10 ; ++ count ; } return count ; } static int OR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans | digit [ i ] ; } return ans ; } static int AND_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans & digit [ i ] ; } return ans ; } static int XOR_of_Digits ( int n , int count ) { int ans = 0 ; for ( int i = 0 ; i < count ; i ++ ) { ans = ans ^ digit [ i ] ; } return ans ; } static void bitwise_operation ( int N ) { int countOfDigit = findDigits ( N ) ; System . out . print ( " XOR ▁ = ▁ " + XOR_of_Digits ( N , countOfDigit ) + "NEW_LINE"); System . out . print ( " OR ▁ = ▁ " + OR_of_Digits ( N , countOfDigit ) + "NEW_LINE"); System . out . print ( " AND ▁ = ▁ " + AND_of_Digits ( N , countOfDigit ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 123456 ; bitwise_operation ( N ) ; } }
import java . util . * ; class GFG { static int largest_sum ( int arr [ ] , int n ) { int maximum = - 1 ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( m . get ( arr [ i ] ) > 1 ) { if ( m . containsKey ( 2 * arr [ i ] ) ) { m . put ( 2 * arr [ i ] , m . get ( 2 * arr [ i ] ) + m . get ( arr [ i ] ) / 2 ) ; } else { m . put ( 2 * arr [ i ] , m . get ( arr [ i ] ) / 2 ) ; } if ( 2 * arr [ i ] > maximum ) maximum = 2 * arr [ i ] ; } } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 4 , 7 , 8 } ; int n = arr . length ; System . out . println ( largest_sum ( arr , n ) ) ; } }
import java . util . * ; public class GFG { static int maxLength ( int arr [ ] , int N ) { int product = 1 , len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } if ( product >= 0 ) { return N ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { len = Math . max ( len , Math . max ( N - i - 1 , i ) ) ; } } return len ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 } ; int N = arr . length ; System . out . println ( maxLength ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void findMinimumOperation ( int n , int d , int arrays [ ] [ ] ) { int cnt = 0 ; int first = Integer . MIN_VALUE , end = Integer . MAX_VALUE ; while ( n > 0 ) { int arr [ ] = { arrays [ cnt ] [ 0 ] , arrays [ cnt ] [ 1 ] } ; Arrays . sort ( arr ) ; first = Math . max ( first , arr [ 0 ] ) ; end = Math . min ( end , arr [ 1 ] ) ; cnt ++ ; n -- ; } if ( first > end ) System . out . print ( " - 1" ) ; else { if ( d >= first && d <= end ) { System . out . print ( "0" ) ; } else System . out . print ( Math . min ( Math . abs ( first - d ) , Math . abs ( d - end ) ) ) ; } } public static void main ( String [ ] args ) { int n = 3 , d = 3 ; int arrays [ ] [ ] = { { 0 , 7 } , { 2 , 14 } , { 4 , 6 } } ; findMinimumOperation ( n , d , arrays ) ; } }
class GFG { static long mod = 1000000007L ; static long mult ( long a , long b ) { return ( ( a % mod ) * ( b % mod ) ) % mod ; } static long calculate_factors ( long n ) { long ans , cnt ; cnt = 0 ; ans = 1 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } if ( cnt % 2 == 1 ) { ans = mult ( ans , ( cnt + 1 ) ) ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { cnt = 0 ; while ( n % i == 0 ) { cnt ++ ; n = n / i ; } if ( cnt % 2 == 1 ) { ans = mult ( ans , ( cnt + 1 ) ) ; } } if ( n > 2 ) { ans = mult ( ans , ( 2 ) ) ; } return ans % mod ; } public static void main ( String [ ] args ) { long n = 193748576239475639L ; mod = 17 ; System . out . print ( calculate_factors ( n ) + "NEW_LINE"); } }
class GFG { static void maxValue ( int a ) { String c = Integer . toBinaryString ( a ) ; String b = " " ; for ( int i = 0 ; i < c . length ( ) ; i ++ ) { if ( ( c . charAt ( i ) - '0' ) == 1 ) b += '0' ; else b += '1' ; } System . out . print ( Integer . parseInt ( b , 2 ) ) ; } public static void main ( String [ ] args ) { int a = 4 ; maxValue ( a ) ; } }
import java . util . * ; class GFG { static void getmax ( int arr [ ] , int n , int x ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s = s + arr [ i ] ; } System . out . print ( Math . min ( s , x ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int x = 5 ; int arr_size = arr . length ; getmax ( arr , arr_size , x ) ; } }
import java . util . * ; class GFG { static void canBeReduced ( int x , int y ) { int maxi = Math . max ( x , y ) ; int mini = Math . min ( x , y ) ; if ( ( ( x + y ) % 3 ) == 0 && maxi <= 2 * mini ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " NO " + "NEW_LINE"); } public static void main ( String [ ] args ) { int x = 6 , y = 9 ; canBeReduced ( x , y ) ; } }
class GFG { static double nthRoot ( int A , int N ) { double xPre = Math . random ( ) * 10 % 10 ; double eps = 1e-3 ; double delX = Integer . MAX_VALUE ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static int countPowers ( int a , int b , int k ) { return ( int ) ( Math . floor ( nthRoot ( b , k ) ) - Math . ceil ( nthRoot ( a , k ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 7 , b = 28 , k = 2 ; System . out . print ( " Count ▁ of ▁ Powers ▁ is ▁ " + countPowers ( a , b , k ) ) ; } }
import java . util . * ; class GFG { static final int INFINITY_ = 9999999 ; static int sqrtByLongDivision ( int n ) { int cur_divisor = 0 ; int quotient_units_digit = 0 ; int cur_quotient = 0 ; int cur_dividend = 0 ; int cur_remainder = 0 ; int a [ ] = new int [ 10 ] ; while ( n > 0 ) { a [ i ] = n % 100 ; n = n / 100 ; i ++ ; } i -- ; for ( j = i ; j >= 0 ; j -- ) { cur_remainder = INFINITY_ ; cur_dividend = cur_dividend * 100 + a [ j ] ; for ( udigit = 0 ; udigit <= 9 ; udigit ++ ) { if ( cur_remainder >= cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) && cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) >= 0 ) { cur_remainder = cur_dividend - ( ( cur_divisor * 10 + udigit ) * udigit ) ; quotient_units_digit = udigit ; } } cur_quotient = cur_quotient * 10 + quotient_units_digit ; cur_divisor = cur_quotient * 2 ; cur_dividend = cur_remainder ; } return cur_quotient ; } public static void main ( String [ ] args ) { int x = 1225 ; System . out . print ( sqrtByLongDivision ( x ) + "NEW_LINE"); } }
import java . util . * ; import java . lang . * ; class GFG { static int previousFibonacci ( int n ) { double a = n / ( ( 1 + ( int ) Math . sqrt ( 5 ) ) / 2.0 ) ; return ( int ) Math . round ( a ) ; } static int nextFibonacci ( int n ) { double a = n * ( 1 + ( int ) Math . sqrt ( 5 ) ) / 2.0 ; return ( int ) Math . round ( a ) ; } 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 int countFibonacciPairs ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isFibonacci ( arr [ i ] ) && isFibonacci ( arr [ j ] ) ) { int prevFib = previousFibonacci ( arr [ i ] ) ; int nextFib = nextFibonacci ( arr [ i ] ) ; if ( prevFib == arr [ j ] nextFib == arr [ j ] ) { res ++ ; } } return res ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 5 , 8 , 11 } ; int n = a . length ; System . out . print ( countFibonacciPairs ( a , n ) ) ; } }
class GFG { static int countPairsWithProdK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] == k ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int N = arr . length ; int K = 3 ; System . out . println ( countPairsWithProdK ( arr , N , K ) ) ; } }
class GFG { static int MAX = 100000 ; static int countPairsWithProductK ( int arr [ ] , int n , int k ) { int count = 0 ; int i ; boolean hashmap [ ] = new boolean [ MAX ] ; for ( i = 0 ; i < n ; i ++ ) hashmap [ arr [ i ] ] = true ; for ( i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; double index = 1.0 * k / arr [ i ] ; if ( index >= 0 && ( ( index - ( int ) ( index ) ) == 0 ) && hashmap [ k / x ] ) count ++ ; hashmap [ x ] = false ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int N = arr . length ; int K = 3 ; System . out . print ( countPairsWithProductK ( arr , N , K ) ) ; } }
class GFG { static void oddIntegers ( int n , int k ) { if ( n % 2 != k % 2 ) { System . out . println ( " - 1" ) ; return ; } int sum = 0 ; int i = 1 ; int j = 1 ; while ( j < k ) { sum = sum + i ; System . out . print ( i + " ▁ " ) ; i = i + 2 ; j ++ ; } int finalOdd = n - sum ; System . out . println ( finalOdd ) ; } public static void main ( String [ ] args ) { int n = 10 ; int k = 2 ; oddIntegers ( n , k ) ; } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static int [ ] ans = new int [ 100002 ] ; static int [ ] pref = new int [ 100002 ] ; static void preCompute ( ) { Arrays . fill ( ans , 1 ) ; for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } for ( int i = 1 ; i < 100002 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + ans [ i ] ; pref [ i ] %= mod ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + " ▁ " ) ; } static void printSumProper ( int [ ] [ ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String args [ ] ) { int Q = 2 ; int [ ] [ ] arr = { { 10 , 20 } , { 12 , 16 } } ; printSumProper ( arr , Q ) ; } }
class GFG { static int a [ ] ; static int search ( int a [ ] , int k , int x ) { int j ; for ( j = k - 1 ; j > - 1 ; j -- ) { if ( a [ j ] == x ) return j ; } return - 1 ; } static void genArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( search ( arr , i , arr [ i ] ) == - 1 ) arr [ i + 1 ] = 0 ; else arr [ i + 1 ] = ( i - search ( arr , i , arr [ i ] ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int size = N + 1 ; int a [ ] = new int [ N ] ; genArray ( a , N ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static List < Integer > getPerfectcubes ( int n ) { List < Integer > perfectcubes = new ArrayList < Integer > ( ) ; int current = 1 ; int i = 1 ; while ( current <= n ) { perfectcubes . add ( current ) ; i += 1 ; current = ( int ) ( Math . pow ( i , 3 ) ) ; } return perfectcubes ; } static int maxPairSum ( int [ ] arr ) { int n = arr . length ; int max = 0 ; int secondMax = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } static int countPairsWith ( int n , List < Integer > perfectcubes , List < Integer > nums ) { int count = 0 ; for ( int i = 0 ; i < perfectcubes . size ( ) ; i ++ ) { int temp = perfectcubes . get ( i ) - n ; if ( temp > n && ( nums . contains ( temp ) ) ) count += 1 ; } return count ; } static int countPairs ( int [ ] arr ) { int n = arr . length ; int max = maxPairSum ( arr ) ; List < Integer > perfectcubes = getPerfectcubes ( max ) ; List < Integer > nums = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { nums . add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectcubes , nums ) ; } return count ; } public static void main ( String [ ] agrs ) { int [ ] arr = { 2 , 6 , 18 , 9 , 999 , 1 } ; System . out . print ( countPairs ( arr ) ) ; } }
class GFG { static int countCubes ( int a , int b ) { for ( int i = a ; i <= b ; i ++ ) for ( int j = 1 ; j * j * j <= i ; j ++ ) if ( j * j * j == i ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int a = 7 , b = 30 ; System . out . print ( " Count ▁ of ▁ Cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }
class GFG { static int countCubes ( int a , int b ) { return ( int ) ( Math . floor ( Math . cbrt ( b ) ) - Math . ceil ( Math . cbrt ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 7 , b = 28 ; System . out . print ( " Count ▁ of ▁ cubes ▁ is ▁ " + countCubes ( a , b ) ) ; } }
import java . util . * ; class GFG { static void isPrime ( int N ) { boolean isPrime = true ; int [ ] arr = { 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 } ; if ( N < 2 ) { isPrime = false ; } if ( N % 2 == 0 N % 3 == 0 N % 5 == 0 ) { isPrime = false ; } for ( int i = 0 ; i < Math . sqrt ( N ) ; i += 30 ) { for ( int c : arr ) { if ( c > Math . sqrt ( N ) ) { break ; } else { if ( N % ( c + i ) == 0 ) { isPrime = false ; break ; } } if ( ! isPrime ) break ; } } if ( isPrime ) System . out . println ( " Prime ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ Prime ▁ Number " ) ; } public static void main ( String args [ ] ) { int N = 121 ; isPrime ( N ) ; } }
class GFG { static boolean isArmstrong ( int x , int n ) { int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp /= 10 ; } return sum1 == x ; } static void CalculateXORandOR ( int n ) { int CalculateXOR = 0 ; int CalculateOR = 0 ; int start = ( int ) Math . pow ( 10 , n - 1 ) ; int end = ( int ) ( Math . pow ( 10 , n ) ) - 1 ; for ( int i = start ; i < end + 1 ; i ++ ) { if ( isArmstrong ( i , n ) ) { CalculateXOR = CalculateXOR ^ i ; CalculateOR = CalculateOR | i ; } } System . out . println ( " XOR ▁ = ▁ " + CalculateXOR ) ; System . out . println ( " OR ▁ = ▁ " + CalculateOR ) ; } public static void main ( String [ ] args ) { int n = 4 ; CalculateXORandOR ( n ) ; } }
class GFG { static void perfectCube ( int N ) { int cube ; for ( int i = 0 ; i <= N ; i ++ ) { cube = i * i * i ; if ( cube == N ) { System . out . println ( " Yes " ) ; return ; } else if ( cube > N ) { System . out . println ( " NO " ) ; return ; } } } public static void main ( String [ ] args ) { int N = 216 ; perfectCube ( N ) ; } }
public class GFG { static void perfectCube ( int N ) { int cube_root ; cube_root = ( int ) Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { System . out . println ( " Yes " ) ; return ; } else { System . out . println ( " NO " ) ; return ; } } public static void main ( String [ ] args ) { int N = 125 ; perfectCube ( N ) ; } }
import java . util . * ; class GFG { static void check ( int arr [ ] , int N ) { int even = 0 ; int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { odd ++ ; } else { even ++ ; } } if ( even == N odd == N ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 } ; int N = arr . length ; check ( arr , N ) ; } }
import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double even = 0 , odd = 0 ; if ( b % 2 == 0 ) { even = Math . pow ( b , n ) - 2 ; odd = Math . pow ( b , n ) - 1 ; } else { even = Math . pow ( b , n ) - 1 ; odd = Math . pow ( b , n ) - 2 ; } System . out . println ( " Even ▁ Number ▁ = ▁ " + ( int ) even ) ; System . out . print ( " Odd ▁ Number ▁ = ▁ " + ( int ) odd ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }
class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 3 != 0 ) ans *= Math . pow ( 2 , ( count % 3 ) ) ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 3 != 0 ) ans *= Math . pow ( i , ( count % 3 ) ) ; } if ( n > 2 ) ans *= n ; return ans ; } public static void main ( String [ ] args ) { int n = 128 ; System . out . print ( findMinNumber ( n ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static void isPerfectSquare ( int x ) { int sr = ( int ) Math . sqrt ( x ) ; if ( sr * sr == x ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int n = 7 , k = 2 ; isPerfectSquare ( n + k ) ; } }
class GFG { static void printPairs ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " + " , ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int n = arr . length ; printPairs ( arr , n ) ; } }
class GFG { static int product ( int N ) { int ans = 1 ; int val = ( int ) Math . pow ( 2 , N - 1 ) ; for ( int i = 1 ; i <= N ; i ++ ) { ans *= ( int ) Math . pow ( i , val ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( product ( N ) ) ; } }
class GFG { static int nearest ( int n ) { int prevCube = ( int ) Math . cbrt ( n ) ; int nextCube = prevCube + 1 ; prevCube = prevCube * prevCube * prevCube ; nextCube = nextCube * nextCube * nextCube ; int ans = ( n - prevCube ) < ( nextCube - n ) ? ( prevCube - n ) : ( nextCube - n ) ; return ans ; } public static void main ( String [ ] args ) { int n = 25 ; System . out . println ( nearest ( n ) ) ; n = 27 ; System . out . println ( nearest ( n ) ) ; n = 40 ; System . out . println ( nearest ( n ) ) ; } }
import java . io . * ; class GFG { static void circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( int ) Math . sqrt ( ( ( x1 - x2 ) * ( x1 - x2 ) ) + ( ( y1 - y2 ) * ( y1 - y2 ) ) ) ; if ( distSq + r2 == r1 ) { System . out . println ( " The ▁ smaller ▁ circle ▁ lies ▁ completely " + " ▁ inside ▁ the ▁ bigger ▁ circle ▁ with ▁ " + " touching ▁ each ▁ other ▁ " + " at ▁ a ▁ point ▁ of ▁ circumference . ▁ " ) ; } else if ( distSq + r2 < r1 ) { System . out . println ( " The ▁ smaller ▁ circle ▁ lies ▁ completely " + " ▁ inside ▁ the ▁ bigger ▁ circle ▁ without " + " ▁ touching ▁ each ▁ other ▁ " + " at ▁ a ▁ point ▁ of ▁ circumference . " ) ; } else { System . out . println ( " The ▁ smaller ▁ does ▁ not ▁ lies ▁ inside " + " ▁ the ▁ bigger ▁ circle ▁ completely . " ) ; } } public static void main ( String [ ] args ) { int x1 = 10 , y1 = 8 ; int x2 = 1 , y2 = 2 ; int r1 = 30 , r2 = 10 ; circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; } }
import java . io . * ; class GFG { static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } public static void main ( String [ ] args ) { int x1 = - 10 , y1 = 8 ; int x2 = 14 , y2 = - 24 ; int r1 = 30 , r2 = 10 ; int t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) System . out . println ( " There ▁ are ▁ 3 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else if ( t < 0 ) System . out . println ( " There ▁ are ▁ 4 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; else System . out . println ( " There ▁ are ▁ 2 ▁ common ▁ tangents " + " ▁ between ▁ the ▁ circles . " ) ; } }
import java . io . * ; class GFG { static int GCD ( int a , int b ) { return ( b != 0 ? GCD ( b , a % b ) : a ) ; } static void ratiotang ( int r1 , int r2 ) { System . out . println ( " The ▁ ratio ▁ is ▁ " + r1 / GCD ( r1 , r2 ) + " : " + r2 / GCD ( r1 , r2 ) ) ; } public static void main ( String [ ] args ) { int r1 = 4 , r2 = 8 ; ratiotang ( r1 , r2 ) ; } }
import java . io . * ; class GFG { static int Regions ( int Vertices , int Edges ) { int R = Edges + 2 - Vertices ; return R ; } public static void main ( String [ ] args ) { int V = 5 , E = 7 ; System . out . println ( Regions ( V , E ) ) ; } }
class GFG { static int GCD ( int a , int b ) { return ( b != 0 ? GCD ( b , a % b ) : a ) ; } static void ratiotang ( int r1 , int r2 ) { System . out . println ( " The ▁ ratio ▁ is ▁ " + r1 / GCD ( r1 , r2 ) + " ▁ : ▁ " + r2 / GCD ( r1 , r2 ) ) ; } public static void main ( String args [ ] ) { int r1 = 4 , r2 = 6 ; ratiotang ( r1 , r2 ) ; } }
class GFG { static void lengthOfTangent ( double r1 , double r2 , double d ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ transverse " + " ▁ common ▁ tangent ▁ is ▁ " + Math . sqrt ( Math . pow ( d , 2 ) - Math . pow ( ( r1 + r2 ) , 2 ) ) ) ; } public static void main ( String args [ ] ) { double r1 = 4 , r2 = 6 , d = 12 ; lengthOfTangent ( r1 , r2 , d ) ; } }
class GFG { static void lengtang ( double r1 , double r2 , double d ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ direct " + " ▁ common ▁ tangent ▁ is ▁ " + ( Math . sqrt ( Math . pow ( d , 2 ) - Math . pow ( ( r1 - r2 ) , 2 ) ) ) ) ; } public static void main ( String [ ] args ) { double r1 = 4 , r2 = 6 , d = 3 ; lengtang ( r1 , r2 , d ) ; } }
class GFG { static void shortdis ( double r , double d ) { System . out . println ( " The ▁ shortest ▁ distance ▁ " + " from ▁ the ▁ chord ▁ to ▁ centre ▁ " + ( Math . sqrt ( ( r * r ) - ( ( d * d ) / 4 ) ) ) ) ; } public static void main ( String [ ] args ) { double r = 4 , d = 3 ; shortdis ( r , d ) ; } }
class GFG { static void lengtang ( double r1 , double r2 ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ " + " direct ▁ common ▁ tangent ▁ is ▁ " + ( 2 * Math . sqrt ( r1 * r2 ) ) ) ; } public static void main ( String [ ] args ) { double r1 = 5 , r2 = 9 ; lengtang ( r1 , r2 ) ; } }
class GFG { static void dist ( double x1 , double y1 , double x2 , double y2 , double r ) { System . out . println ( " The ▁ shortest ▁ distance ▁ " + " between ▁ a ▁ point ▁ and ▁ a ▁ circle ▁ is ▁ " + ( Math . sqrt ( ( Math . pow ( ( x2 - x1 ) , 2 ) ) + ( Math . pow ( ( y2 - y1 ) , 2 ) ) ) - r ) ) ; } public static void main ( String [ ] args ) { double x1 = 4 , y1 = 6 , x2 = 35 , y2 = 42 , r = 5 ; dist ( x1 , y1 , x2 , y2 , r ) ; } }
class GFG { static void lengtang ( double r1 , double r2 , double d ) { System . out . println ( " The ▁ length ▁ of ▁ the ▁ direct " + " ▁ common ▁ tangent ▁ is ▁ " + ( Math . sqrt ( Math . pow ( d , 2 ) - Math . pow ( ( r1 - r2 ) , 2 ) ) ) ) ; } public static void main ( String [ ] args ) { double r1 = 4 , r2 = 6 , d = 12 ; lengtang ( r1 , r2 , d ) ; } }
class GFG { static double dist ( double m , double b1 , double b2 ) { double d = Math . abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; return d ; } public static void main ( String [ ] args ) { double m = 2 , b1 = 4 , b2 = 3 ; System . out . println ( dist ( m , b1 , b2 ) ) ; } }
class GFG { static float normal ( float m , float n ) { float N = ( float ) ( ( Math . abs ( m ) * Math . abs ( n ) ) / Math . sqrt ( ( Math . abs ( m ) * Math . abs ( m ) ) + ( Math . abs ( n ) * Math . abs ( n ) ) ) ) ; return N ; } public static void main ( String [ ] args ) { float m = - 5 , n = 3 ; System . out . println ( normal ( m , n ) ) ; } }
class GFG { static float findAreaShaded ( float a ) { float sqArea = a * a ; float semiCircleArea = ( float ) ( 3.14 * ( a * a ) / 8 ) ; float ShadedArea = 4 * semiCircleArea - sqArea ; return ShadedArea ; } public static void main ( String [ ] args ) { float a = 10 ; System . out . println ( findAreaShaded ( a ) ) ; } }
class GfG { static int countSteps ( int x , int y ) { if ( x < y ) { return x + y + 2 * ( ( y - x ) / 2 ) ; } else { return x + y + 2 * ( ( ( x - y ) + 1 ) / 2 ) ; } } public static void main ( String [ ] args ) { int x = 4 , y = 3 ; System . out . println ( countSteps ( x , y ) ) ; } }
import java . util . * ; class GfG { static boolean isSlopeGood ( double slope , int arr [ ] , int n ) { Set < Double > setOfLines = new HashSet < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) setOfLines . add ( arr [ i ] - slope * ( i ) ) ; return setOfLines . size ( ) == 2 ; } static boolean checkForParallel ( int arr [ ] , int n ) { boolean slope1 = isSlopeGood ( arr [ 1 ] - arr [ 0 ] , arr , n ) ; boolean slope2 = isSlopeGood ( arr [ 2 ] - arr [ 1 ] , arr , n ) ; boolean slope3 = isSlopeGood ( ( arr [ 2 ] - arr [ 0 ] ) / 2 , arr , n ) ; return ( slope1 == true slope2 == true slope3 == true ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 8 , 5 } ; int n = arr . length ; if ( checkForParallel ( arr , n ) == true ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }
class GFG { static boolean pointIsOnLine ( int m , int c , int x , int y ) { if ( y == ( ( m * x ) + c ) ) return true ; return false ; } public static void main ( String [ ] args ) { int m = 3 , c = 2 ; int x = 1 , y = 5 ; if ( pointIsOnLine ( m , c , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; class GFG { static float Area ( float a , float b ) { if ( a < 0 && b < 0 ) return - 1 ; float h = ( float ) Math . sqrt ( ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) / ( Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) ) ) ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 4 ; System . out . println ( Area ( a , b ) ) ; } }
class GFG { static int maxRectangles ( int L , int B , int l , int b ) { int horizontal = 0 , vertical = 0 ; if ( l <= L && b <= B ) { int columns = B / b ; int rows = L / l ; horizontal = rows * columns ; } if ( l <= B && b <= L ) { int columns = L / b ; int rows = B / l ; vertical = rows * columns ; } return Math . max ( horizontal , vertical ) ; } public static void main ( String [ ] args ) { int L = 10 , B = 7 , l = 4 , b = 3 ; System . out . print ( maxRectangles ( L , B , l , b ) ) ; } }
import java . io . * ; class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float h = ( float ) 1.268 * a ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( Area ( a ) ) ; } }
class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float x = 0.464f * a ; float A = 0.70477f * ( float ) Math . pow ( x , 2 ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( String . format ( " % .5f " , Area ( a ) ) ) ; } }
class GFG { static double areaSquare ( double side , double fold ) { double area = side * side ; return area * 1.0 / Math . pow ( 2 , fold ) ; } public static void main ( String [ ] args ) { double side = 4 , fold = 2 ; System . out . println ( areaSquare ( side , fold ) ) ; } }
class GFG { static double square ( double a ) { if ( a < 0 ) return - 1 ; double x = 0.464 * a ; return x ; } public static void main ( String [ ] args ) { double a = 5 ; System . out . println ( square ( a ) ) ; } }
import java . util . * ; class GFG { static double ReuleauxArea ( double r ) { if ( r < 0 ) return - 1 ; double A = 0.70477 * 2 * Math . pow ( r , 2 ) ; return A ; } public static void main ( String args [ ] ) { double r = 6 ; System . out . println ( ReuleauxArea ( r ) ) ; } }
import java . lang . Math ; class cfg { static float cyl ( float a ) { if ( a < 0 ) return - 1 ; float r = ( 2 * a * ( float ) ( Math . sqrt ( 2 ) ) / 3 ) ; float h = ( 2 * a ) / 3 ; float V = ( 3.14f * ( float ) ( Math . pow ( r , 2 ) * h ) ) ; return V ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( cyl ( a ) ) ; } }
import java . util . * ; import java . text . DecimalFormat ; class GFG { static double Area ( double l , double b , double h ) { if ( l < 0 b < 0 h < 0 ) return - 1 ; double x = ( l * b ) / ( l + b ) ; double A = 0.70477 * Math . pow ( x , 2 ) ; return A ; } public static void main ( String args [ ] ) { double l = 5 , b = 12 , h = 13 ; DecimalFormat df = new DecimalFormat ( " # , # # # , # #0.00000" ) ; System . out . println ( df . format ( Area ( l , b , h ) ) ) ; } }
class cfg { static float squareSide ( float a ) { if ( a < 0 ) return - 1 ; float x = ( 0.423f * a ) ; return x ; } public static void main ( String [ ] args ) { float a = 8 ; System . out . println ( squareSide ( a ) ) ; } }
import java . util . * ; class GFG { static boolean isPossible ( float x , float y , float z ) { float a = x * x + y * y + z * z ; if ( Math . ceil ( a ) == 1 && Math . floor ( a ) == 1 ) return true ; return false ; } public static void main ( String args [ ] ) { float l = 0.70710678f , m = 0.5f , n = 0.5f ; if ( isPossible ( l , m , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { double polyapothem ( double n , double a ) { if ( a < 0 && n < 0 ) return - 1 ; return ( a / ( 2 * java . lang . Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ) ; } public static void main ( String args [ ] ) { double a = 9 , n = 6 ; GFG g = new GFG ( ) ; System . out . println ( g . polyapothem ( n , a ) ) ; } }
import java . io . * ; class GFG { static float polyarea ( float n , float a ) { if ( a < 0 && n < 0 ) return - 1 ; float A = ( a * a * n ) / ( float ) ( 4 * Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 9 , n = 6 ; System . out . println ( polyarea ( n , a ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { static double decdiagonal ( double a ) { if ( a < 0 ) return - 1 ; double d = 1.902 * a ; return d ; } public static void main ( String [ ] args ) { int a = 9 ; System . out . println ( decdiagonal ( a ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { static double heptdiagonal ( double a ) { if ( a < 0 ) return - 1 ; double d = 1.802 * a ; return d ; } public static void main ( String [ ] args ) { int a = 6 ; System . out . println ( heptdiagonal ( a ) ) ; } }
public class GFG { static double hexDiagonal ( float a ) { if ( a < 0 ) return - 1 ; double d = ( double ) 1.73 * a ; return d ; } public static void main ( String [ ] args ) { float a = 9 ; System . out . println ( hexDiagonal ( a ) ) ; } }
public class GFG { static double ReuleauxArea ( float a ) { if ( a < 0 ) return - 1 ; double A = ( double ) 0.70477 * Math . pow ( a , 2 ) ; return A ; } public static void main ( String args [ ] ) { float a = 6 ; System . out . println ( ReuleauxArea ( a ) ) ; } }
import java . lang . Math ; import java . io . * ; class GFG { static double calculateSide ( double n , double r ) { double theta , theta_in_radians ; theta = 360 / n ; theta_in_radians = theta * 3.14 / 180 ; return 2 * r * Math . sin ( theta_in_radians / 2 ) ; } public static void main ( String [ ] args ) { double n = 3 ; double r = 5 ; System . out . println ( calculateSide ( n , r ) ) ; } }
public class GFG { static float CubeVolume ( float d ) { float Volume ; Volume = ( float ) ( Math . sqrt ( 3 ) * Math . pow ( d , 3 ) ) / 9 ; return Volume ; } public static void main ( String [ ] args ) { float d = 5 ; System . out . println ( " Volume ▁ of ▁ Cube : ▁ " + CubeVolume ( d ) ) ; } }
import java . io . * ; class GFG { static float cyl ( float r , float R , float h ) { if ( h < 0 && r < 0 && R < 0 ) return - 1 ; float r1 = r ; float h1 = h ; float V = ( float ) ( 3.14 * Math . pow ( r1 , 2 ) * h1 ) ; return V ; } public static void main ( String [ ] args ) { float r = 7 , R = 11 , h = 6 ; System . out . print ( cyl ( r , R , h ) ) ; } }
import java . io . * ; class GFG { static double cyl ( double r , double h ) { if ( r < 0 && h < 0 ) return - 1 ; double R = ( 2 * r ) / 3 ; double H = ( 2 * h ) / 3 ; double V = 3.14 * Math . pow ( R , 2 ) * H ; return V ; } public static void main ( String [ ] args ) { double r = 4 , h = 8 ; System . out . println ( cyl ( r , h ) ) ; } }
class Solution { static float cube ( float h , float r ) { if ( h < 0 && r < 0 ) return - 1 ; float a = ( float ) Math . pow ( h , 3 ) ; return a ; } public static void main ( String args [ ] ) { float h = 5 , r = 4 ; System . out . println ( cube ( h , r ) ) ; } }
import java . io . * ; class GFG { static float cyl ( float R ) { if ( R < 0 ) return - 1 ; float V = ( float ) ( ( 2 * 3.14 * Math . pow ( R , 3 ) ) / ( 3 * Math . sqrt ( 3 ) ) ) ; return V ; } public static void main ( String [ ] args ) { float R = 4 ; System . out . print ( cyl ( R ) ) ; } }
import java . io . * ; class GFG { static float rod ( float h , float r ) { if ( h < 0 && r < 0 ) return - 1 ; float l = ( float ) ( Math . sqrt ( Math . pow ( h , 2 ) + 4 * Math . pow ( r , 2 ) ) ) ; return l ; } public static void main ( String [ ] args ) { float h = 4 ; float r = 1.5f ; System . out . print ( rod ( h , r ) ) ; } }
import java . io . * ; class GFG { static float cube ( float h , float r ) { if ( h < 0 && r < 0 ) return - 1 ; float a = ( h * r * ( float ) Math . sqrt ( 2 ) ) / ( h + ( float ) Math . sqrt ( 2 ) * r ) ; return a ; } public static void main ( String [ ] args ) { float h = 5 , r = 6 ; System . out . println ( cube ( h , r ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static float coner ( float R ) { if ( R < 0 ) return - 1 ; float r = ( float ) ( 2 * Math . sqrt ( 2 ) * R ) / 3 ; return r ; } static float coneh ( float R ) { if ( R < 0 ) return - 1 ; float h = ( 4 * R ) / 3 ; return h ; } public static void main ( String args [ ] ) { float R = 10 ; System . out . println ( " r ▁ = ▁ " + coner ( R ) + " , ▁ " + " h ▁ = ▁ " + coneh ( R ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static float coneRadius ( float a ) { if ( a < 0 ) return - 1 ; float r = ( float ) ( a / Math . sqrt ( 2 ) ) ; return r ; } static float coneHeight ( float a ) { if ( a < 0 ) return - 1 ; float h = a ; return h ; } public static void main ( String args [ ] ) { float a = 6 ; System . out . println ( " r ▁ = ▁ " + coneRadius ( a ) + " , ▁ " + " h ▁ = ▁ " + coneHeight ( a ) ) ; } }
import java . util . * ; class Solution { static float largestCube ( float r ) { if ( r < 0 ) return - 1 ; float a = ( 2 * r ) / ( float ) Math . sqrt ( 3 ) ; return a ; } public static void main ( String args [ ] ) { float r = 5 ; System . out . println ( largestCube ( r ) ) ; } }
class GFG { static float sphere ( float a ) { if ( a < 0 ) return - 1 ; float r = a / 2 ; return r ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( sphere ( a ) ) ; } }
import java . util . * ; class Geeks { static float maxVol ( float P , float A ) { float l = ( float ) ( P - Math . sqrt ( P * P - 24 * A ) ) / 12 ; float V = ( float ) ( l * ( A / 2.0 - l * ( P / 4.0 - l ) ) ) ; return V ; } public static void main ( String args [ ] ) { float P = 20 , A = 16 ; System . out . println ( maxVol ( P , A ) ) ; } }
import java . io . * ; class GFG { static int rhombusAreaPeri ( int d1 , int d2 ) { int area , perimeter ; area = ( d1 * d2 ) / 2 ; perimeter = ( int ) ( 2 * Math . sqrt ( Math . pow ( d1 , 2 ) + Math . pow ( d2 , 2 ) ) ) ; System . out . println ( " The ▁ area ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " + d1 + " ▁ and ▁ " + d2 + " ▁ is ▁ " + area + " . " ) ; System . out . println ( " The ▁ perimeter ▁ of ▁ rhombus ▁ with ▁ diagonals ▁ " + d1 + " ▁ and ▁ " + d2 + " ▁ is ▁ " + perimeter + " . " ) ; return 0 ; } public static void main ( String [ ] args ) { int d1 = 2 , d2 = 4 ; rhombusAreaPeri ( d1 , d2 ) ; } }
import java . util . * ; import java . text . * ; class solution { static void foot ( float a , float b , float c , float d , float x1 , float y1 , float z1 ) { float k = ( - a * x1 - b * y1 - c * z1 - d ) / ( float ) ( a * a + b * b + c * c ) ; float x2 = a * k + x1 ; float y2 = b * k + y1 ; float z2 = c * k + z1 ; DecimalFormat form = new DecimalFormat ( "0.0" ) ; System . out . print ( " ▁ x2 ▁ = ▁ " + form . format ( x2 ) ) ; System . out . print ( " ▁ y2 ▁ = ▁ " + form . format ( y2 ) ) ; System . out . print ( " ▁ z2 ▁ = ▁ " + form . format ( z2 ) ) ; } public static void main ( String arr [ ] ) { float a = 1 ; float b = - 2 ; float c = 0 ; float d = 0 ; float x1 = - 1 ; float y1 = 3 ; float z1 = 4 ; foot ( a , b , c , d , x1 , y1 , z1 ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int countRectangles ( int l , int w ) { int squareSide = __gcd ( l , w ) ; return ( l * w ) / ( squareSide * squareSide ) ; } public static void main ( String [ ] args ) { int l = 4 , w = 6 ; System . out . println ( countRectangles ( l , w ) ) ; } }
import java . util . * ; class solution { static void circle_equation ( double x1 , double y1 , double r ) { double a = - 2 * x1 ; double b = - 2 * y1 ; double c = ( r * r ) - ( x1 * x1 ) - ( y1 * y1 ) ; System . out . print ( " x ^ 2 ▁ + ▁ ( " + a + " ▁ x ) ▁ + ▁ " ) ; System . out . print ( " y ^ 2 ▁ + ▁ ( " + b + " ▁ y ) ▁ = ▁ " ) ; System . out . println ( c + " . " ) ; } public static void main ( String arr [ ] ) { double x1 = 2 , y1 = - 3 , r = 8 ; circle_equation ( x1 , y1 , r ) ; } }
public class GFG { static boolean checkEquilibrium ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int x3 , int y3 , int z3 ) { int resx = x1 + x2 + x3 ; int resy = y1 + y2 + y3 ; int resz = z1 + z2 + z3 ; if ( resx == 0 & resy == 0 & resz == 0 ) return true ; else return false ; } public static void main ( String args [ ] ) { int x1 = - 2 , y1 = - 7 , z1 = - 9 , x2 = 5 , y2 = - 14 , z2 = 14 , x3 = - 3 , y3 = 21 , z3 = - 5 ; if ( checkEquilibrium ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 ) ) System . out . println ( " The ▁ vectors ▁ are ▁ at ▁ equilibrium . " ) ; else System . out . println ( " The ▁ vectors ▁ are ▁ not ▁ at ▁ equilibrium . " ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void findTangent ( int A , int x , int y ) { int dif = A - x * 2 ; if ( y == ( 2 * x - x * x ) ) { if ( dif < 0 ) System . out . println ( " y ▁ = ▁ " + dif + " x " + ( x * dif + y ) ) ; else if ( dif > 0 ) System . out . println ( " y ▁ = ▁ " + dif + " x + " + - x * dif + y ) ; else System . out . println ( " Not ▁ possible " ) ; } } public static void main ( String args [ ] ) { int A = 2 , x = 2 , y = 0 ; findTangent ( A , x , y ) ; } }
import java . io . * ; class GFG { static double octaside ( double a ) { if ( a < 0 ) return - 1 ; double s = a / ( Math . sqrt ( 2 ) + 1 ) ; return s ; } public static void main ( String [ ] args ) { double a = 4 ; System . out . println ( octaside ( a ) ) ; } }
class GFG { static class node { long x , y , r ; } ; static long dis ( long x1 , long y1 , long x2 , long y2 ) { return ( long ) Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; } static boolean check ( node c1 , node c2 , int k ) { long min = 0 ; long max = 0 ; long de = dis ( c1 . x , c1 . y , c2 . x , c2 . y ) ; if ( de == 0 ) { if ( c1 . r == c2 . r ) { min = 0 ; max = 0 ; } else if ( c1 . r - c2 . r > 0 ) { min = c1 . r - c2 . r ; max = min + 2 * c2 . r ; } else { min = c2 . r - c1 . r ; max = min + 2 * c1 . r ; } } else if ( de >= c1 . r + c2 . r ) { min = de - c1 . r - c2 . r ; max = de + c1 . r + c2 . r ; } else if ( de + c2 . r < c1 . r ) { max = c2 . r + c1 . r + de ; min = c1 . r - de - c2 . r ; } else if ( de + c1 . r < c2 . r ) { max = c2 . r + c1 . r + de ; min = c2 . r - de - c1 . r ; } else if ( ( de + c2 . r >= c1 . r ) || ( de + c1 . r >= c2 . r ) ) { max = c2 . r + c1 . r + de ; min = 0 ; } long temin = ( long ) ( Math . ceil ( min ) ) ; long re = ( long ) max ; if ( k >= temin && k <= re ) { return true ; } return false ; } public static void main ( String [ ] args ) { node circle1 = new node ( ) ; node circle2 = new node ( ) ; int k = 3 ; circle1 . x = 0 ; circle1 . y = 0 ; circle1 . r = 5 ; circle2 . x = 8 ; circle2 . y = 3 ; circle2 . r = 2 ; if ( check ( circle1 , circle2 , k ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . io . * ; class GFG { static float rhombusarea ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; return ( l * b ) / 2 ; } public static void main ( String [ ] args ) { float l = 16 , b = 6 ; System . out . println ( rhombusarea ( l , b ) ) ; } }
import java . io . * ; class GFG { static boolean Move ( int a , int x , int b ) { if ( ( ( ( b - a ) % x == 0 ) || ( ( b - a - 1 ) % x == 0 ) && a + 1 != b ) && b >= a ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 3 , x = 2 , b = 7 ; if ( Move ( a , x , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static float area ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; float A = a * b ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 2 ; System . out . println ( area ( a , b ) ) ; } }
import java . * ; class gfg { public double circlearea ( double a , double b , double c ) { if ( a < 0 b < 0 c < 0 ) return - 1 ; double p = ( a + b + c ) / 2 ; double At = Math . sqrt ( p * ( p - a ) * ( p - b ) * ( p - c ) ) ; double A = 3.14 * Math . pow ( ( ( a * b * c ) / ( 4 * At ) ) , 2 ) ; return A ; } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; double a = 4 , b = 5 , c = 3 ; System . out . println ( g . circlearea ( a , b , c ) ) ; } }
import java . io . * ; class GFG { static float altitude ( float a , float b ) { return ( float ) ( Math . sqrt ( Math . pow ( a , 2 ) - ( Math . pow ( b , 2 ) / 4 ) ) ) ; } static float area ( float b , float h ) { return ( 1 * b * h ) / 2 ; } public static void main ( String [ ] args ) { float a = 2 , b = 3 ; float h = altitude ( a , b ) ; System . out . print ( " Altitude = ▁ " + h + " , ▁ " ) ; System . out . print ( " Area = ▁ " + area ( b , h ) ) ; } }
import java . io . * ; class GFG { static int surfaceArea ( int b , int s ) { return 2 * b * s + ( int ) Math . pow ( b , 2 ) ; } public static void main ( String [ ] args ) { int b = 3 , s = 4 ; System . out . println ( surfaceArea ( b , s ) ) ; } }
public class GFG { static boolean check ( long a , long b , long p , long q ) { long d = p * p + q * q ; if ( d == 0 ) return a == 0 && b == 0 ; else return ( a * p + b * q ) % d == 0 && ( b * p - a * q ) % d == 0 ; } static boolean check ( int a , int b , int x , int y , int p , int q ) { if ( check ( a - x , b - y , p , q ) || check ( a + x , b + y , p , q ) || check ( a - y , b + x , p , q ) || check ( a + y , b - x , p , q ) ) return true ; else return false ; } public static void main ( String args [ ] ) { int a = - 4 , b = - 2 ; int x = 0 , y = 0 ; int p = - 2 , q = - 1 ; if ( check ( a , b , x , y , p , q ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
public class GFG { static float trianglearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; float area = ( float ) ( 3 * Math . sqrt ( 3 ) * Math . pow ( a , 2 ) ) / ( 4 * b ) ; return area ; } public static void main ( String [ ] args ) { float a = 4 , b = 2 ; System . out . println ( trianglearea ( a , b ) ) ; } }
import java . io . * ; class GFG { static float squarearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; float area = 4 * ( float ) ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) / ( Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) ) ; return area ; } public static void main ( String [ ] args ) { float a = 4 , b = 2 ; System . out . println ( squarearea ( a , b ) ) ; } }
import java . io . * ; class GFG { static float trianglearea ( float r ) { if ( r < 0 ) return - 1 ; return r * r ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( trianglearea ( r ) ) ; } }
import java . io . * ; class GFG { static float squarearea ( float r ) { if ( r < 0 ) return - 1 ; float a = 4 * ( float ) ( Math . pow ( r , 2 ) / 5 ) ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( squarearea ( r ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float rectanglearea ( float a , float b ) { if ( a < 0 b < 0 ) return - 1 ; return 2 * a * b ; } public static void main ( String args [ ] ) { float a = 10 , b = 8 ; System . out . println ( rectanglearea ( a , b ) ) ; } }
class GFG { static int MAX = 50 ; static void solveQuery ( int n , int q , int qi [ ] , int qj [ ] ) { int [ ] arr = new int [ MAX ] ; for ( int i = 0 ; i <= n ; i ++ ) arr [ i ] = 0 ; for ( int k = 0 ; k < q ; k ++ ) { int flag = 0 ; if ( qj [ k ] < qi [ k ] ) { int temp = qi [ k ] ; qi [ k ] = qj [ k ] ; qj [ k ] = temp ; } if ( arr [ qi [ k ] ] != 0 arr [ qj [ k ] ] != 0 ) flag = 1 ; else if ( qi [ k ] == qj [ k ] ) flag = 1 ; else { for ( int i = 1 ; i < qi [ k ] ; i ++ ) { if ( arr [ i ] != 0 && arr [ i ] < qj [ k ] && qi [ k ] < arr [ i ] ) { flag = 1 ; break ; } } if ( flag == 0 ) { for ( int i = qi [ k ] + 1 ; i < qj [ k ] ; i ++ ) { if ( arr [ i ] != 0 && arr [ i ] > qj [ k ] ) { flag = 1 ; break ; } } } } if ( flag == 0 ) { System . out . println ( " YES " ) ; arr [ qi [ k ] ] = qj [ k ] ; arr [ qj [ k ] ] = qi [ k ] ; } else System . out . println ( " NO " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; int q = 7 ; int qi [ ] = { 1 , 2 , 2 , 2 , 9 , 10 , 8 } ; int qj [ ] = { 5 , 7 , 3 , 4 , 9 , 9 , 6 } ; solveQuery ( n , q , qi , qj ) ; } }
class GFG { static void shortestLength ( int n , int x [ ] , int y [ ] ) { int answer = 0 ; int i = 0 ; while ( n != 0 && i < x . length ) { if ( x [ i ] + y [ i ] > answer ) answer = x [ i ] + y [ i ] ; i ++ ; } System . out . println ( " Length ▁ - > ▁ " + answer ) ; System . out . println ( " Path ▁ - > ▁ " + " ( ▁ 1 , ▁ " + answer + " ▁ ) " + " and ▁ ( ▁ " + answer + " , ▁ 1 ▁ ) " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int x [ ] = new int [ ] { 1 , 4 , 2 , 1 } ; int y [ ] = new int [ ] { 4 , 1 , 1 , 2 } ; shortestLength ( n , x , y ) ; } }
class GFG { static int squares ( int l , int b , int a ) { return ( int ) ( Math . ceil ( l / ( double ) a ) * Math . ceil ( b / ( double ) a ) ) ; } public static void main ( String [ ] args ) { int l = 11 , b = 23 , a = 14 ; System . out . println ( squares ( l , b , a ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int squarearea ( int l , int b ) { if ( l < 0 b < 0 ) return - 1 ; int n = ( l * b ) / gcd ( l , b ) ; return n * n ; } public static void main ( String [ ] args ) { int l = 6 , b = 4 ; System . out . println ( squarearea ( l , b ) ) ; } }
class GFG { static double angle_triangle ( int x1 , int x2 , int x3 , int y1 , int y2 , int y3 , int z1 , int z2 , int z3 ) { int num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) ; double den = Math . sqrt ( Math . pow ( ( x2 - x1 ) , 2 ) + Math . pow ( ( y2 - y1 ) , 2 ) + Math . pow ( ( z2 - z1 ) , 2 ) ) * Math . sqrt ( Math . pow ( ( x3 - x1 ) , 2 ) + Math . pow ( ( y3 - y1 ) , 2 ) + Math . pow ( ( z3 - z1 ) , 2 ) ) ; double angle = Math . acos ( num / den ) * ( 180.0 / 3.141592653589793238463 ) ; return angle ; } public static void main ( String [ ] args ) { int x1 = - 1 ; int y1 = 3 ; int z1 = 2 ; int x2 = 2 ; int y2 = 3 ; int z2 = 5 ; int x3 = 3 ; int y3 = 5 ; int z3 = - 2 ; double angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) ; double angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) ; double angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) ; System . out . println ( " Angles ▁ are ▁ : " ) ; System . out . println ( " angle ▁ A ▁ = ▁ " + angle_A + " ▁ degree " ) ; System . out . println ( " angle ▁ B ▁ = ▁ " + angle_B + " ▁ degree " ) ; System . out . println ( " angle ▁ C ▁ = ▁ " + angle_C + " ▁ degree " ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int solve ( int M , int N , int s ) { if ( N % s == 0 ) { N = N / s ; } else { N = ( N / s ) + 1 ; } if ( M % s == 0 ) { M = M / s ; } else { M = ( M / s ) + 1 ; } return M * N ; } public static void main ( String args [ ] ) { int N = 12 , M = 13 , s = 4 ; System . out . println ( solve ( M , N , s ) ) ; } }
class GFG { static int solve ( double M , double N , double s ) { int ans = ( ( int ) ( Math . ceil ( M / s ) ) * ( int ) ( Math . ceil ( N / s ) ) ) ; return ans ; } public static void main ( String [ ] args ) { double N = 12 , M = 13 , s = 4 ; System . out . println ( solve ( M , N , s ) ) ; } }
class GFG { { int x ; int y ; } static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } static boolean doIntersect ( Point p1 , Point q1 , Point p2 , Point q2 ) { int o1 = orientation ( p1 , q1 , p2 ) ; int o2 = orientation ( p1 , q1 , q2 ) ; int o3 = orientation ( p2 , q2 , p1 ) ; int o4 = orientation ( p2 , q2 , q1 ) ; if ( o1 != o2 && o3 != o4 ) return true ; return false ; } static boolean similar ( Point p1 , Point p2 ) { if ( p1 . x == p2 . x && p1 . y == p2 . y ) return false ; return true ; } static boolean collinear ( Point p1 , Point p2 , Point p3 ) { int x1 = p1 . x , y1 = p1 . y ; int x2 = p2 . x , y2 = p2 . y ; int x3 = p3 . x , y3 = p3 . y ; if ( ( y3 - y2 ) * ( x2 - x1 ) == ( y2 - y1 ) * ( x3 - x2 ) ) return false ; else return true ; } static int no_of_quads ( Point p1 , Point p2 , Point p3 , Point p4 ) { boolean same = true ; same = same & similar ( p1 , p2 ) ; same = same & similar ( p1 , p3 ) ; same = same & similar ( p1 , p4 ) ; same = same & similar ( p2 , p3 ) ; same = same & similar ( p2 , p4 ) ; same = same & similar ( p3 , p4 ) ; if ( same == false ) return 0 ; boolean coll = true ; coll = coll & collinear ( p1 , p2 , p3 ) ; coll = coll & collinear ( p1 , p2 , p4 ) ; coll = coll & collinear ( p1 , p3 , p4 ) ; coll = coll & collinear ( p2 , p3 , p4 ) ; if ( coll == false ) return 0 ; int check = 0 ; if ( doIntersect ( p1 , p2 , p3 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p3 , p2 , p4 ) ) check = 1 ; if ( doIntersect ( p1 , p2 , p4 , p3 ) ) check = 1 ; if ( check == 0 ) return 3 ; return 1 ; } public static void main ( String args [ ] ) { Point p1 , p2 , p3 , p4 ; p1 = new Point ( ) ; p2 = new Point ( ) ; p3 = new Point ( ) ; p4 = new Point ( ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 5 ; p4 . y = 9 ; System . out . println ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = - 1 ; p2 . y = 0 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 0 ; p4 . y = 3 ; System . out . println ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = 0 ; p2 . y = 10 ; p3 . x = 0 ; p3 . y = 11 ; p4 . x = 0 ; p4 . y = 12 ; System . out . println ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; p1 . x = 0 ; p1 . y = 9 ; p2 . x = 0 ; p2 . y = 9 ; p3 . x = 5 ; p3 . y = - 1 ; p4 . x = 0 ; p4 . y = 3 ; System . out . println ( no_of_quads ( p1 , p2 , p3 , p4 ) ) ; } }
import java . io . * ; class GFG { static float findVolume ( float l , float b , float h ) { float volume = ( l * b * h ) / 2 ; return volume ; } public static void main ( String [ ] args ) { float l = 18 , b = 12 , h = 9 ; System . out . println ( " Volume ▁ of ▁ triangular ▁ prism : ▁ " + findVolume ( l , b , h ) ) ; } }
import java . io . * ; class GFG { static int countNumberOfTriangles ( int n ) { return n * ( n - 1 ) * ( n - 2 ) / 6 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countNumberOfTriangles ( n ) ) ; } }
import java . io . * ; class GFG { static long centeredDodecagonal ( long n ) { return 6 * n * ( n - 1 ) + 1 ; } public static void main ( String [ ] args ) { long n = 2 ; System . out . println ( centeredDodecagonal ( n ) ) ; n = 9 ; System . out . println ( centeredDodecagonal ( n ) ) ; } }
import java . io . * ; class GFG { static long centeredTridecagonalNum ( long n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; n = 10 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; } }
import java . io . * ; class GFG { static boolean checkConcurrent ( int a1 , int b1 , int c1 , int a2 , int b2 , int c2 , int a3 , int b3 , int c3 ) { return ( a3 * ( b1 * c2 - b2 * c1 ) + b3 * ( c1 * a2 - c2 * a1 ) + c3 * ( a1 * b2 - a2 * b1 ) == 0 ) ; } public static void main ( String [ ] args ) { int a1 = 2 , b1 = - 3 , c1 = 5 ; int a2 = 3 , b2 = 4 , c2 = - 7 ; int a3 = 9 , b3 = - 5 , c3 = 8 ; if ( checkConcurrent ( a1 , b1 , c1 , a2 , b2 , c2 , a3 , b3 , c3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int center_octadecagon_num ( int n ) { return 9 * n * n - 9 * n + 1 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( n + " th ▁ centered ▁ " + " octadecagonal ▁ number ▁ : ▁ " ) ; System . out . println ( center_octadecagon_num ( n ) ) ; n = 13 ; System . out . print ( n + " th ▁ centered ▁ " + " octadecagonal ▁ number ▁ : ▁ " ) ; System . out . println ( center_octadecagon_num ( n ) ) ; } }
import java . io . * ; class GFG { static int centereddecagonalnum ( int n ) { return ( 5 * n * n + 5 * n + 1 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( n + " th ▁ centered ▁ " + " decagonal ▁ number : ▁ " ) ; System . out . println ( centereddecagonalnum ( n ) ) ; n = 9 ; System . out . print ( n + " th ▁ centered ▁ " + " decagonal ▁ number : ▁ " ) ; System . out . println ( centereddecagonalnum ( n ) ) ; } }
import java . io . * ; class GFG { static int factorial ( int a , int b ) { int res = 1 ; for ( int i = 1 ; i <= ( a + b ) ; i ++ ) res = res * i ; for ( int i = 1 ; i <= a ; i ++ ) res = res / i ; for ( int i = 1 ; i <= b ; i ++ ) res = res / i ; return res ; } static void Ksmallest ( int x , int y , int k ) { if ( x == 0 && y == 0 ) return ; else if ( x == 0 ) { y -- ; System . out . print ( " V " ) ; Ksmallest ( x , y , k ) ; } else if ( y == 0 ) { x -- ; System . out . print ( " H " ) ; Ksmallest ( x , y , k ) ; } else { if ( factorial ( x - 1 , y ) > k ) { System . out . print ( " H " ) ; Ksmallest ( x - 1 , y , k ) ; } else { System . out . print ( " V " ) ; Ksmallest ( x , y - 1 , k - factorial ( x - 1 , y ) ) ; } } } public static void main ( String [ ] args ) { int x = 2 , y = 2 , k = 2 ; Ksmallest ( x , y , k ) ; } }
import java . io . * ; class GFG { static int centered_pentagonal_Num ( int n ) { return ( 5 * n * n - 5 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( n + " th ▁ Centered ▁ " + " pentagonal ▁ number : ▁ " ) ; System . out . println ( centered_pentagonal_Num ( n ) ) ; } }
import java . io . * ; class GFG { static int pivotDis ( int x0 , int y0 , int x1 , int y1 ) { return ( int ) Math . sqrt ( ( x1 - x0 ) * ( x1 - x0 ) + ( y1 - y0 ) * ( y1 - y0 ) ) ; } static int minDis ( int D , int r1 , int r2 ) { return Math . max ( ( D - r1 - r2 ) , 0 ) ; } static int maxDis ( int D , int r1 , int r2 ) { return D + r1 + r2 ; } public static void main ( String [ ] args ) { int x0 = 0 , y0 = 0 , x1 = 8 , y1 = 0 , r1 = 4 , r2 = 5 ; int D = pivotDis ( x0 , y0 , x1 , y1 ) ; System . out . print ( " Distance ▁ while ▁ " + " repulsion ▁ = ▁ " + maxDis ( D , r1 , r2 ) ) ; System . out . print ( " Distance while " + " attraction ▁ = ▁ " + minDis ( D , r1 , r2 ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static void printRect ( Integer X [ ] , Integer Y [ ] , int n ) { int Xmax = Collections . max ( Arrays . asList ( X ) ) ; int Xmin = Collections . min ( Arrays . asList ( X ) ) ; int Ymax = Collections . max ( Arrays . asList ( Y ) ) ; int Ymin = Collections . min ( Arrays . asList ( Y ) ) ; System . out . println ( " { " + Xmin + " , ▁ " + Ymin + " } " ) ; System . out . println ( " { " + Xmin + " , ▁ " + Ymax + " } " ) ; System . out . println ( " { " + Xmax + " , ▁ " + Ymax + " } " ) ; System . out . println ( " { " + Xmax + " , ▁ " + Ymin + " } " ) ; } public static void main ( String [ ] args ) { Integer X [ ] = { 4 , 3 , 6 , 1 , - 1 , 12 } ; Integer Y [ ] = { 4 , 1 , 10 , 3 , 7 , - 1 } ; int n = X . length ; printRect ( X , Y , n ) ; } }
import java . io . * ; class GFG { static boolean checkOrigin ( int x1 , int y1 , int x2 , int y2 ) { return ( x1 * ( y2 - y1 ) == y1 * ( x2 - x1 ) ) ; } public static void main ( String [ ] args ) { if ( checkOrigin ( 1 , 28 , 2 , 56 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static boolean isBetween ( int a , int b , int c ) { return ( Math . min ( a , b ) <= c && c <= Math . max ( a , b ) ) ; } static boolean canJoin ( int x [ ] , int y [ ] , int i , int j , int k ) { return ( x [ k ] == x [ i ] x [ k ] == x [ j ] ) && isBetween ( y [ i ] , y [ j ] , y [ k ] ) || ( y [ k ] == y [ i ] y [ k ] == y [ j ] ) && isBetween ( x [ i ] , x [ j ] , x [ k ] ) ; } static int countLineSegments ( int x [ ] , int y [ ] ) { if ( ( x [ 0 ] == x [ 1 ] && x [ 1 ] == x [ 2 ] ) || ( y [ 0 ] == y [ 1 ] && y [ 1 ] == y [ 2 ] ) ) return 1 ; else if ( canJoin ( x , y , 0 , 1 , 2 ) || canJoin ( x , y , 0 , 2 , 1 ) || canJoin ( x , y , 1 , 2 , 0 ) ) return 2 ; else return 3 ; } public static void main ( String [ ] args ) { int x [ ] = new int [ 3 ] , y [ ] = new int [ 3 ] ; x [ 0 ] = - 1 ; y [ 0 ] = - 1 ; x [ 1 ] = - 1 ; y [ 1 ] = 3 ; x [ 2 ] = 4 ; y [ 2 ] = 3 ; System . out . println ( countLineSegments ( x , y ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static Boolean pythagorean_quadruple ( int a , int b , int c , int d ) { int sum = a * a + b * b + c * c ; if ( d * d == sum ) return true ; else return false ; } public static void main ( String [ ] args ) { int a = 1 , b = 2 , c = 2 , d = 3 ; if ( pythagorean_quadruple ( a , b , c , d ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static void midpoint ( int x1 , int x2 , int y1 , int y2 ) { System . out . print ( ( x1 + x2 ) / 2 + " ▁ , ▁ " + ( y1 + y2 ) / 2 ) ; } public static void main ( String [ ] args ) { int x1 = - 1 , y1 = 2 ; int x2 = 3 , y2 = - 6 ; midpoint ( x1 , x2 , y1 , y2 ) ; } }
public class demo { static float pi = 3.14159f ; public static float volume ( float r , float R , float h ) { return ( float ) 1 / 3 * pi * h * ( r * r + R * R + r * R ) ; } public static float curved_surface_area ( float r , float R , float l ) { return pi * l * ( R + r ) ; } public static float total_surface_area ( float r , float R , float l , float h ) { return pi * l * ( R + r ) + pi * ( r * r + R * R ) ; } public static void main ( String args [ ] ) { float small_radius = 3 ; float big_radius = 8 ; float slant_height = 13 ; float height = 12 ; System . out . print ( " Volume ▁ Of ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( volume ( small_radius , big_radius , height ) ) ; System . out . print ( " Curved ▁ Surface ▁ Area ▁ Of " + " ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( curved_surface_area ( small_radius , big_radius , slant_height ) ) ; System . out . print ( " Total ▁ Surface ▁ Area ▁ Of " + " ▁ Frustum ▁ of ▁ Cone ▁ : ▁ " ) ; System . out . println ( total_surface_area ( small_radius , big_radius , slant_height , height ) ) ; } }
import java . io . * ; class GFG { int Circumference ( int a ) { return 4 * a ; } public static void main ( String args [ ] ) { GFG obj = new GFG ( ) ; int a = 5 ; System . out . println ( " Circumference ▁ of ▁ " + " a ▁ square ▁ is ▁ " + obj . Circumference ( a ) ) ; } }
public class Arc { static double arcLength ( double diameter , double angle ) { double pi = 22.0 / 7.0 ; double arc ; if ( angle >= 360 ) { System . out . println ( " Angle ▁ cannot " + " ▁ be ▁ formed " ) ; return 0 ; } else { arc = ( pi * diameter ) * ( angle / 360.0 ) ; return arc ; } } public static void main ( String args [ ] ) { double diameter = 25.0 ; double angle = 45.0 ; double arc_len = arcLength ( diameter , angle ) ; System . out . println ( arc_len ) ; } }
class GFG { static class point { int x , y ; } ; static int RIGHT = 1 , LEFT = - 1 , ZERO = 0 ; static int directionOfPoint ( point A , point B , point P ) { B . x -= A . x ; B . y -= A . y ; P . x -= A . x ; P . y -= A . y ; int cross_product = B . x * P . y - B . y * P . x ; if ( cross_product > 0 ) return RIGHT ; if ( cross_product < 0 ) return LEFT ; return ZERO ; } public static void main ( String [ ] args ) { point A = new point ( ) , B = new point ( ) , P = new point ( ) ; A . x = - 30 ; B . x = 29 ; P . x = 15 ; int direction = directionOfPoint ( A , B , P ) ; if ( direction == 1 ) System . out . println ( " Right ▁ Direction " ) ; else if ( direction == - 1 ) System . out . println ( " Left ▁ Direction " ) ; else System . out . println ( " Point ▁ is ▁ on ▁ the ▁ Line " ) ; } }
import java . util . Arrays ; class GFG { static int minRadius ( int k , int [ ] x , int [ ] y , int n ) { int [ ] dis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Arrays . sort ( dis ) ; return dis [ k - 1 ] ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] x = { 1 , - 1 , 1 } ; int [ ] y = { 1 , - 1 , - 1 } ; int n = x . length ; System . out . println ( minRadius ( k , x , y , n ) ) ; } }
import java . io . * ; class Geometry { static int areaRectangle ( int a , int b ) { int area = a * b ; return area ; } static int perimeterRectangle ( int a , int b ) { int perimeter = 2 * ( a + b ) ; return perimeter ; } public static void main ( String [ ] args ) { int a = 5 ; int b = 6 ; System . out . println ( " Area ▁ = ▁ " + areaRectangle ( a , b ) ) ; System . out . println ( " Perimeter ▁ = ▁ " + perimeterRectangle ( a , b ) ) ; } }
import java . util . * ; class GFG { static int areaSquare ( int side ) { int area = side * side ; return area ; } public static void main ( String [ ] args ) { int side = 5 ; System . out . println ( areaSquare ( 4 ) ) ; } }
import java . util . * ; class GFG { public static long minPerimeter ( int n ) { int l = ( int ) Math . sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long row = n / l ; long perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( minPerimeter ( n ) ) ; } }
import java . util . Arrays ; class GFG { static void possibleOrNot ( long a1 , long a2 , long b1 , long b2 , long c1 , long c2 ) { long dis1 = ( long ) Math . pow ( b1 - a1 , 2 ) + ( long ) Math . pow ( b2 - a2 , 2 ) ; long dis2 = ( long ) Math . pow ( c1 - b1 , 2 ) + ( long ) Math . pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) System . out . print ( " No " ) ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b2 == ( ( a2 + c2 ) / 2.0 ) ) System . out . print ( " No " ) ; else System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { long a1 = 1 , a2 = 0 , b1 = 2 , b2 = 0 , c1 = 3 , c2 = 0 ; possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) ; } }
class GFG { static int countObtuseAngles ( int a , int b , int k ) { int c1 = ( b - a ) - 1 ; int c2 = ( k - b ) + ( a - 1 ) ; if ( c1 == c2 ) return 0 ; return min ( c1 , c2 ) ; } public static void main ( String arg [ ] ) { int k = 6 , a = 1 , b = 3 ; System . out . print ( countObtuseAngles ( a , b , k ) ) ; } }
class GFG { static class Point { float x , y ; Point ( ) { x = y = 0 ; } Point ( float a , float b ) { x = a ; y = b ; } } ; static void printPoints ( Point source , float l , int m ) { Point a = new Point ( ) ; Point b = new Point ( ) ; if ( m == 0 ) { a . x = source . x + l ; a . y = source . y ; b . x = source . x - l ; b . y = source . y ; } else if ( Double . isInfinite ( m ) ) { a . x = source . x ; a . y = source . y + l ; b . x = source . x ; b . y = source . y - l ; } else { float dx = ( float ) ( l / Math . sqrt ( 1 + ( m * m ) ) ) ; float dy = m * dx ; a . x = source . x + dx ; a . y = source . y + dy ; b . x = source . x - dx ; b . y = source . y - dy ; } System . out . println ( a . x + " , ▁ " + a . y ) ; System . out . println ( b . x + " , ▁ " + b . y ) ; } public static void main ( String [ ] args ) { Point p = new Point ( 2 , 1 ) , q = new Point ( 1 , 0 ) ; printPoints ( p , ( float ) Math . sqrt ( 2 ) , 1 ) ; System . out . println ( ) ; printPoints ( q , 5 , 0 ) ; } }
class GFG { static class Point { float x , y ; Point ( ) { x = y = 0 ; } Point ( float a , float b ) { x = a ; y = b ; } } ; static void printCorners ( Point p , Point q , float l ) { Point a = new Point ( ) , b = new Point ( ) , c = new Point ( ) , d = new Point ( ) ; if ( p . x == q . x ) { a . x = ( float ) ( p . x - ( l / 2.0 ) ) ; a . y = p . y ; d . x = ( float ) ( p . x + ( l / 2.0 ) ) ; d . y = p . y ; b . x = ( float ) ( q . x - ( l / 2.0 ) ) ; b . y = q . y ; c . x = ( float ) ( q . x + ( l / 2.0 ) ) ; c . y = q . y ; } else if ( p . y == q . y ) { a . y = ( float ) ( p . y - ( l / 2.0 ) ) ; a . x = p . x ; d . y = ( float ) ( p . y + ( l / 2.0 ) ) ; d . x = p . x ; b . y = ( float ) ( q . y - ( l / 2.0 ) ) ; b . x = q . x ; c . y = ( float ) ( q . y + ( l / 2.0 ) ) ; c . x = q . x ; } else { float m = ( p . x - q . x ) / ( q . y - p . y ) ; float dx = ( float ) ( ( l / Math . sqrt ( 1 + ( m * m ) ) ) * 0.5 ) ; float dy = m * dx ; a . x = p . x - dx ; a . y = p . y - dy ; d . x = p . x + dx ; d . y = p . y + dy ; b . x = q . x - dx ; b . y = q . y - dy ; c . x = q . x + dx ; c . y = q . y + dy ; } System . out . print ( ( int ) a . x + " , ▁ " + ( int ) a . y + " NEW_LINE" + ( int ) b . x + " , ▁ " + ( int ) b . y + "NEW_LINE" + ( int ) c . x + " , ▁ " + ( int ) c . y + " NEW_LINE" + ( int ) d . x + " , ▁ " + ( int ) d . y + "NEW_LINE"); } public static void main ( String [ ] args ) { Point p1 = new Point ( 1 , 0 ) , q1 = new Point ( 1 , 2 ) ; printCorners ( p1 , q1 , 2 ) ; Point p = new Point ( 1 , 1 ) , q = new Point ( - 1 , - 1 ) ; printCorners ( p , q , ( float ) ( 2 * Math . sqrt ( 2 ) ) ) ; } }
class GFG { static class point { int x , y ; public point ( int x , int y ) { super ( ) ; this . x = x ; this . y = y ; } } ; static class line { public line ( int a , int b , int c ) { this . a = a ; this . b = b ; this . c = c ; } int a , b , c ; line ( ) { } } ; static int evalPointOnLine ( point p , line curLine ) { int eval = curLine . a * p . x + curLine . b * p . y + curLine . c ; if ( eval > 0 ) return 1 ; return - 1 ; } static int minJumpToReachDestination ( point start , point dest , line lines [ ] , int N ) { int jumps = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int signStart = evalPointOnLine ( start , lines [ i ] ) ; int signDest = evalPointOnLine ( dest , lines [ i ] ) ; if ( signStart * signDest < 0 ) jumps ++ ; } return jumps ; } public static void main ( String [ ] args ) { point start = new point ( 1 , 1 ) ; point dest = new point ( - 2 , - 1 ) ; line [ ] lines = new line [ 3 ] ; lines [ 0 ] = new line ( 1 , 0 , 0 ) ; lines [ 1 ] = new line ( 0 , 1 , 0 ) ; lines [ 2 ] = new line ( 1 , 1 , - 2 ) ; System . out . print ( minJumpToReachDestination ( start , dest , lines , 3 ) ) ; } }
import java . util . * ; class GFG { static class Interval { int start , end ; Interval ( int start , int end ) { this . start = start ; this . end = end ; } } ; static int minDistanceToCoverIntervals ( Interval intervals [ ] , int N , int x ) { int rightMostStart = Integer . MIN_VALUE ; int leftMostEnd = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { if ( rightMostStart < intervals [ i ] . start ) rightMostStart = intervals [ i ] . start ; if ( leftMostEnd > intervals [ i ] . end ) leftMostEnd = intervals [ i ] . end ; } int res ; if ( rightMostStart > leftMostEnd ) res = - 1 ; else if ( rightMostStart <= x && x <= leftMostEnd ) res = 0 ; else res = ( x < rightMostStart ) ? ( rightMostStart - x ) : ( x - leftMostEnd ) ; return res ; } public static void main ( String [ ] args ) { int x = 3 ; Interval [ ] intervals = { new Interval ( 0 , 7 ) , new Interval ( 2 , 14 ) , new Interval ( 4 , 6 ) } ; int N = intervals . length ; int res = minDistanceToCoverIntervals ( intervals , N , x ) ; if ( res == - 1 ) System . out . print ( " Not ▁ Possible ▁ to ▁ " + "cover all intervalsNEW_LINE"); else System . out . print ( res + "NEW_LINE"); } }
class GFG { static void checkPoint ( int radius , int x , int y , float percent , float startAngle ) { float endAngle = 360 / percent + startAngle ; double polarradius = Math . sqrt ( x * x + y * y ) ; double Angle = Math . atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) System . out . print ( " Point " + " ( " + x + " , " + y + " ) " + " exist in the circle sectorNEW_LINE"); else System . out . print ( " Point " + " ( " + x + " , " + y + " ) " + " exist in the circle sectorNEW_LINE"); } public static void main ( String arg [ ] ) { int radius = 8 , x = 3 , y = 4 ; float percent = 12 , startAngle = 0 ; checkPoint ( radius , x , y , percent , startAngle ) ; } }
import java . util . * ; class GFG { static class point { int x , y ; point ( ) { } public point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static int square ( int x ) { return x * x ; } static int a , b , c ; static void order ( ) { int [ ] copy = new int [ 3 ] ; copy [ 0 ] = a ; copy [ 1 ] = b ; copy [ 2 ] = c ; Arrays . sort ( copy ) ; a = copy [ 0 ] ; b = copy [ 1 ] ; c = copy [ 2 ] ; } static int euclidDistSquare ( point p1 , point p2 ) { return square ( p1 . x - p2 . x ) + square ( p1 . y - p2 . y ) ; } static String getSideClassification ( int a , int b , int c ) { if ( a == b && b == c ) return " Equilateral " ; else if ( a == b b == c ) return " Isosceles " ; else return " Scalene " ; } static String getAngleClassification ( int a , int b , int c ) { if ( a + b > c ) return " acute " ; else if ( a + b == c ) return " right " ; else return " obtuse " ; } static void classifyTriangle ( point p1 , point p2 , point p3 ) { a = euclidDistSquare ( p1 , p2 ) ; b = euclidDistSquare ( p1 , p3 ) ; c = euclidDistSquare ( p2 , p3 ) ; order ( ) ; System . out . println ( " Triangle ▁ is ▁ " + getAngleClassification ( a , b , c ) + " ▁ and ▁ " + getSideClassification ( a , b , c ) ) ; } public static void main ( String [ ] args ) { point p1 , p2 , p3 ; p1 = new point ( 3 , 0 ) ; p2 = new point ( 0 , 4 ) ; p3 = new point ( 4 , 7 ) ; classifyTriangle ( p1 , p2 , p3 ) ; p1 = new point ( 0 , 0 ) ; p2 = new point ( 1 , 1 ) ; p3 = new point ( 1 , 2 ) ; classifyTriangle ( p1 , p2 , p3 ) ; } }
import java . util . HashSet ; class GFG { public static void findDifferenceArray ( int arr [ ] , int N ) { HashSet < Integer > S1 = new HashSet < Integer > ( ) ; HashSet < Integer > S2 = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { S1 . add ( arr [ j ] ) ; } for ( int j = i + 1 ; j < N ; j ++ ) { S2 . add ( arr [ j ] ) ; } System . out . print ( Math . abs ( S1 . size ( ) - S2 . size ( ) ) + " ▁ " ) ; S1 . clear ( ) ; S2 . clear ( ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 7 , 3 , 2 , 3 } ; int N = arr . length ; findDifferenceArray ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maximumSubarraySum ( int arr [ ] , int N ) { HashMap < Integer , Integer > memo = new HashMap < > ( ) ; int res = Integer . MIN_VALUE ; int currsum = 0 , currval = 0 ; for ( int i = 0 ; i < N ; ++ i ) { currval = arr [ i ] ; currsum = currsum + currval ; if ( memo . containsKey ( currval ) ) { if ( currval > 0 ) res = Math . max ( res , currsum - memo . get ( currval ) + currval ) ; else res = Math . max ( res , currsum - memo . get ( currval ) + 2 * currval ) ; } else memo . put ( currval , currsum ) ; if ( currval < 0 ) currsum = currsum - currval ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 3 , 4 , 0 , - 1 , - 2 } ; int N = 6 ; System . out . println ( maximumSubarraySum ( arr , N ) ) ; } }
import java . util . * ; class GFG { static double f ( double x , int p , double num ) { return Math . pow ( x , p ) - num ; } static double f_prime ( double x , int p ) { return p * Math . pow ( x , p - 1 ) ; } static double root ( double num , int p ) { double left = - num ; double right = num ; double x ; while ( true ) { x = ( left + right ) / 2.0 ; double value = f ( x , p , num ) ; double prime = f_prime ( x , p ) ; if ( value * prime <= 0 ) left = x ; else right = x ; if ( value < 0.000001 && value >= 0 ) { return x ; } } } public static void main ( String args [ ] ) { double P = 1234321 ; int N = 2 ; double ans = root ( P , N ) ; System . out . print ( ans ) ; } }
import java . io . * ; class GFG { static int minimumAdditionOperation ( int N ) { int count = 0 ; while ( N > 0 ) { if ( N % 2 == 1 ) { count ++ ; } N = N >> 1 ; } return count ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minimumAdditionOperation ( N ) ) ; } }
class GFG { public static int minimumOperations ( int arr [ ] , int N ) { int mini = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; while ( arr [ i ] % 2 == 0 ) { arr [ i ] = arr [ i ] / 2 ; count ++ ; } if ( mini > count ) { mini = count ; } } return mini ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 6 } ; int N = arr . length ; System . out . println ( minimumOperations ( arr , N ) ) ; } }
public class GFG { static int minimumSizeArr ( int A , int B ) { int currXor = 0 ; int reminder = ( A - 1 ) % 4 ; if ( reminder == 0 ) currXor = A - 1 ; else if ( reminder == 1 ) currXor = 1 ; else if ( reminder == 2 ) currXor = A ; int minSize = A ; if ( currXor == B ) return minSize ; else if ( ( currXor ^ B ) == A ) return minSize + 2 ; else return minSize + 1 ; } public static void main ( String [ ] args ) { int A = 1 ; int B = 999 ; System . out . println ( minimumSizeArr ( A , B ) ) ; } }
import java . io . * ; class GFG { static int countTriplets ( int N ) { int cnt = 0 ; for ( int A = 1 ; A <= N ; ++ A ) { for ( int B = 1 ; B <= N / A ; ++ B ) { cnt += N / ( A * B ) ; } } return cnt ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( countTriplets ( N ) ) ; } }
import java . io . * ; class GFG { static void numberOfPasses ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; ++ i ) { -- arr [ i ] ; } for ( int i = 0 ; i < N ; ++ i ) { int cnt = 0 ; int k = i ; do { ++ cnt ; k = arr [ k ] ; } while ( k != i ) ; System . out . print ( cnt + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 6 , 2 , 1 , 5 , 3 } ; int N = arr . length ; numberOfPasses ( arr , N ) ; } }
import java . util . * ; class GFG { static int minOperations ( int A [ ] , int B [ ] , int N ) { int cnt = 0 ; int i = 0 ; while ( i < N ) { int j = 0 ; while ( j < N && i < N ) { if ( B [ i ] == A [ j ] ) { i ++ ; } j ++ ; } cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 4 , 6 , 1 , 5 , 3 } ; int B [ ] = { 6 , 5 , 4 , 2 , 3 , 1 } ; int N = A . length ; System . out . print ( minOperations ( A , B , N ) ) ; } }
import java . util . * ; class GFG { static int countDistinctSums ( int N , int L , int R ) { int minSum = L * N ; int maxSum = R * N ; return maxSum - minSum + 1 ; } public static void main ( String [ ] args ) { int N = 2 , L = 1 , R = 3 ; System . out . print ( countDistinctSums ( N , L , R ) ) ; } }
import java . util . * ; class GFG { static void setSumtoZero ( double arr [ ] , int N ) { int [ ] A = new int [ N ] ; int sum = 0 ; int m = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { sum += Math . ceil ( arr [ i ] ) ; A [ i ] = ( int ) Math . ceil ( arr [ i ] ) ; } if ( sum > 0 ) { m = Math . min ( sum , N ) ; for ( int i = 0 ; i < N && m > 0 ; i ++ ) { A [ i ] = ( int ) Math . floor ( arr [ i ] ) ; if ( A [ i ] != Math . floor ( arr [ i ] ) ) m -- ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( A [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { double arr [ ] = { - 2 , - 2 , 4.5 } ; int N = arr . length ; setSumtoZero ( arr , N ) ; } }
class GFG { public static int Get ( int [ ] graph , int a ) { return graph [ a ] = ( graph [ a ] == a ? a : Get ( graph , graph [ a ] ) ) ; } public static void Union ( int [ ] graph , int a , int b ) { a = Get ( graph , a ) ; b = Get ( graph , b ) ; graph [ a ] = b ; } public static void Queries ( int [ ] [ ] queries , int N , int M ) { int [ ] graph = new int [ N + 2 ] ; for ( int i = 1 ; i <= N + 1 ; i ++ ) { graph [ i ] = i ; } for ( int [ ] query : queries ) { if ( query [ 0 ] == 1 ) { Union ( graph , query [ 1 ] , query [ 1 ] + 1 ) ; } else { int a = Get ( graph , query [ 1 ] ) ; if ( a == N + 1 ) System . out . print ( - 1 ) ; else System . out . print ( graph [ a ] + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int N = 5 ; int [ ] [ ] queries = { { 2 , 1 } , { 1 , 1 } , { 2 , 1 } , { 2 , 3 } } ; int Q = queries . length ; Queries ( queries , N , Q ) ; } }
import java . io . * ; class GFG { static long convertToBase9 ( long n ) { long ans = 0 ; long a = 1 ; while ( n > 0 ) { ans += ( a * ( n % 9 ) ) ; a *= 10 ; n /= 9 ; } return ans ; } static long getNthnumber ( long base9 , long K ) { long ans = 0 ; long a = 1 ; while ( base9 > 0 ) { int cur = ( int ) ( base9 % 10 ) ; if ( cur >= K ) { ans += a * ( cur + 1 ) ; } else { ans += a * cur ; } base9 /= 10 ; a *= 10 ; } return ans ; } public static void main ( String [ ] args ) { long N = 10 , K = 1 ; long base9 = convertToBase9 ( N ) ; System . out . println ( getNthnumber ( base9 , K ) ) ; } }
import java . io . * ; public class GFG { static boolean binarySearch ( float [ ] arr , int size , float key ) { int low = 0 , high = size - 1 , mid ; while ( low <= high ) { mid = low + ( high - low ) / 2 ; if ( key > arr [ mid ] ) low = mid + 1 ; else if ( key < arr [ mid ] ) high = mid - 1 ; else return true ; } return false ; } static boolean twoPointers ( float [ ] arr , int N , float mean ) { int i = 0 , j = N - 1 ; while ( i < j ) { float temp = ( arr [ i ] + arr [ j ] ) / 2 ; if ( temp > mean ) j -- ; else if ( temp < mean ) i ++ ; else return true ; } return false ; } static boolean checkArray ( float [ ] arr , int N ) { float sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; float mean = sum / N ; if ( ( N & 1 ) != 0 ) return binarySearch ( arr , N , mean ) ; else return twoPointers ( arr , N , mean ) ; } public static void main ( String [ ] args ) { float [ ] arr = { 1.0f , 3.0f , 6.0f , 9.0f , 12.0f , 32.0f } ; int N = arr . length ; if ( checkArray ( arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int UniqueGeometricTerms ( int N , int a1 , int r1 , int a2 , int r2 ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; int p1 = a1 ; for ( int i = 0 ; i < N ; i ++ ) { S . add ( p1 ) ; p1 = ( p1 * r1 ) ; } int p2 = a2 ; for ( int i = 0 ; i < N ; i ++ ) { S . add ( p2 ) ; p2 = ( p2 * r2 ) ; } return S . size ( ) ; } public static void main ( String [ ] args ) { int N = 5 ; int a1 = 3 , r1 = 2 , a2 = 2 , r2 = 3 ; System . out . print ( UniqueGeometricTerms ( N , a1 , r1 , a2 , r2 ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int arr [ ] , int N , int K ) { Vector < pair > vp = new Vector < pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { vp . add ( new pair ( arr [ i ] , i ) ) ; } Collections . sort ( vp , ( a , b ) -> a . first - b . first ) ; int minCnt = 0 ; while ( ! vp . isEmpty ( ) ) { int val , ind ; val = vp . get ( vp . size ( ) - 1 ) . first ; ind = vp . get ( vp . size ( ) - 1 ) . second ; minCnt ++ ; while ( ! vp . isEmpty ( ) && vp . get ( vp . size ( ) - 1 ) . first == val && ind - vp . get ( vp . size ( ) - 1 ) . second + 1 <= K ) vp . remove ( vp . size ( ) - 1 ) ; } return minCnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 18 , 11 , 18 , 11 , 18 } ; int K = 3 ; int N = arr . length ; System . out . print ( minOperations ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void nearestLeft ( int arr [ ] , int N , int [ ] steps ) { int L = - N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { L = - ( N - i ) ; break ; } } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { L = i ; } steps [ i ] = i - L ; } } static void nearestRight ( int arr [ ] , int N , int [ ] steps ) { int R = 2 * N ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] > 0 ) { R = N + i ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] > 0 ) { R = i ; } steps [ i ] = Math . min ( steps [ i ] , R - i ) ; } } static int accumulate ( int [ ] arr , int start , int end ) { int sum = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; return sum ; } static int findSum ( int arr [ ] , int N , int M , int K ) { int [ ] steps = new int [ N ] ; int sum = accumulate ( arr , 0 , N ) ; if ( sum == 0 ) { return 0 ; } nearestLeft ( arr , N , steps ) ; nearestRight ( arr , N , steps ) ; for ( int i = 0 ; i < N ; i ++ ) sum += 2 * K * Math . max ( 0 , M - steps [ i ] ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 1 , 0 , 0 } ; int N = arr . length ; int M = 2 ; int K = 1 ; System . out . print ( findSum ( arr , N , M , K ) ) ; } }
import java . util . * ; class GFG { static int chk ( int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; while ( n != 0 ) { v . add ( n % 2 ) ; n = n / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) == 1 ) { return ( int ) Math . pow ( 2 , i ) ; } } return 0 ; } static void sumOfLSB ( int arr [ ] , int N ) { Vector < Integer > lsb_arr = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { lsb_arr . add ( chk ( arr [ i ] ) ) ; } Collections . sort ( lsb_arr ) ; int ans = 0 ; for ( int i = 0 ; i < N - 1 ; i += 2 ) { ans += ( lsb_arr . get ( i + 1 ) ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; sumOfLSB ( arr , N ) ; } }
import java . util . * ; class GFG { static void elementsCoprimeWithArr ( int A [ ] , int N , int L , int R ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int curr_ele = A [ i ] ; for ( int j = 1 ; j <= Math . sqrt ( curr_ele ) + 1 ; j ++ ) { if ( curr_ele % j == 0 ) { S . add ( j ) ; S . add ( curr_ele / j ) ; } } } HashSet < Integer > store = new HashSet < Integer > ( ) ; for ( int i = L ; i <= R ; i ++ ) store . add ( i ) ; S . remove ( 1 ) ; for ( int it : S ) { int ele = it ; int index = 1 ; while ( index * ele <= R ) { store . remove ( index * ele ) ; index ++ ; } } for ( int i : store ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 } ; int L = 1 , R = 10 ; int N = arr . length ; elementsCoprimeWithArr ( arr , N , L , R ) ; } }
import java . io . * ; import java . util . * ; class GFG { @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] adj = new Vector [ 100000 ] ; static Vector < Integer > setNum = new Vector < Integer > ( 100000 ) ; static void addEdge ( int a1 , int a2 ) { adj [ a1 ] . add ( a2 ) ; adj [ a2 ] . add ( a1 ) ; } static void toBipartite ( int N ) { for ( int i = 0 ; i < 100000 ; i ++ ) setNum . add ( - 1 ) ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; setNum . set ( 0 , 0 ) ; while ( ! q . isEmpty ( ) ) { int v = q . peek ( ) ; q . remove ( ) ; for ( int u : adj [ v ] ) { if ( setNum . get ( u ) == - 1 ) { setNum . set ( u , setNum . get ( v ) ^ 1 ) ; q . add ( u ) ; } } } } static void pathLengthQuery ( int A , int B ) { if ( setNum . get ( A ) == setNum . get ( B ) ) { System . out . println ( " Odd " ) ; } else { System . out . println ( " Even " ) ; } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 100000 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; int N = 7 ; addEdge ( 0 , 1 ) ; addEdge ( 0 , 2 ) ; addEdge ( 1 , 3 ) ; addEdge ( 3 , 4 ) ; addEdge ( 3 , 5 ) ; addEdge ( 2 , 6 ) ; toBipartite ( N ) ; pathLengthQuery ( 4 , 2 ) ; pathLengthQuery ( 0 , 4 ) ; } }
import java . io . * ; class GFG { static int highestPowerof2 ( int n ) { int p = ( int ) Math . floor ( Math . log ( n ) / Math . log ( 2.0 ) ) ; if ( p % 2 == 0 ) p -= 1 ; int result = ( int ) ( Math . pow ( 2 , p ) ) ; return result ; } static int minStep ( int N , int X ) { if ( N % 2 != 0 && X == 0 ) return - 1 ; int size = 0 ; while ( X < N ) { N -= highestPowerof2 ( N ) ; size += 1 ; } if ( N != 0 ) size += 1 ; return size ; } public static void main ( String [ ] args ) { int N = 11 ; int X = 2 ; System . out . println ( minStep ( N , X ) ) ; } }
import java . io . * ; class GFG { public static String checkSameDigits ( int N ) { int digit = N % 10 ; while ( N != 0 ) { int current_digit = N % 10 ; N = N / 10 ; if ( current_digit != digit ) { return " No " ; } } return " Yes " ; } public static void main ( String args [ ] ) throws IOException { int N = 222 ; System . out . println ( checkSameDigits ( N ) ) ; } }
import java . util . * ; class GFG { static int positionAfterKJumps ( int X , int Y , int K ) { int addY = Y * ( K / 2 ) ; int reduceX = - 1 * X * ( K / 2 + K % 2 ) ; return addY + reduceX ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 , K = 3 ; System . out . print ( positionAfterKJumps ( X , Y , K ) ) ; } }
import java . io . * ; class GFG { static int findMaxK ( int N ) { int K = N ; for ( int i = N - 1 ; i >= 0 ; i -- ) { K &= i ; if ( K == 0 ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( findMaxK ( N ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( ( arr [ j ] % arr [ i ] == 0 ) && ( j + 1 ) % ( i + 1 ) == 0 && ( arr [ j ] / arr [ i ] == ( j + 1 ) / ( i + 1 ) ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 2 , 4 , 20 , 25 , - 6 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int findMinMax ( int [ ] a ) { int min_val = 1000000000 ; for ( int i = 1 ; i < a . length ; ++ i ) { min_val = Math . min ( min_val , a [ i ] * a [ i - 1 ] ) ; } return min_val ; } public static void main ( String [ ] args ) { int [ ] arr = { 6 , 4 , 5 , 6 , 2 , 4 , 1 } ; System . out . println ( findMinMax ( arr ) ) ; } }
import java . io . * ; class GFG { static long factorial ( int n ) { long ans = 1 ; while ( n >= 1 ) { ans *= n ; n -- ; } return ans ; } static void Binary_Num ( int n , int k ) { long num_even , num_odd ; if ( n - k - 1 >= 0 && k - 1 >= 0 ) { num_even = factorial ( n - 2 ) / ( factorial ( k - 1 ) * factorial ( n - k - 1 ) ) ; } else { num_even = 0 ; } if ( k - 2 >= 0 ) { num_odd = factorial ( n - 2 ) / ( factorial ( k - 2 ) * factorial ( n - k ) ) ; } else { num_odd = 0 ; } System . out . println ( num_even + " ▁ " + num_odd ) ; } public static void main ( String [ ] args ) { int N = 9 , K = 6 ; Binary_Num ( N , K ) ; } }
import java . util . * ; class GFG { static int intCount ( int X , int K ) { int ans = 0 ; for ( int z = 0 ; z < Math . pow ( 10 , K ) ; z += ( Math . pow ( 10 , K ) - 1 ) / 9 ) { if ( z > X ) break ; ans += ( ( X - z ) / Math . pow ( 10 , K ) + 1 ) ; } return ans ; } static int intCountInRange ( int L , int R , int K ) { return ( intCount ( R , K ) - intCount ( L - 1 , K ) ) ; } public static void main ( String [ ] args ) { int L = 49 ; int R = 101 ; int K = 2 ; System . out . print ( intCountInRange ( L , R , K ) ) ; } }
import java . util . * ; public class GFG { static int sum ; static class TreeNode { int data ; TreeNode left ; TreeNode right ; TreeNode ( int data ) { this . data = data ; this . left = null ; this . right = null ; } } ; static void kDistanceDownSum ( TreeNode root , int k ) { if ( root == null k < 0 ) return ; if ( k == 0 ) { sum += root . data ; return ; } kDistanceDownSum ( root . left , k - 1 ) ; kDistanceDownSum ( root . right , k - 1 ) ; } static int kDistanceSum ( TreeNode root , int target , int k ) { if ( root == null ) return - 1 ; if ( root . data == target ) { kDistanceDownSum ( root . left , k - 1 ) ; return 0 ; } int dl = - 1 ; if ( target < root . data ) { dl = kDistanceSum ( root . left , target , k ) ; } if ( dl != - 1 ) { if ( dl + 1 == k ) sum += root . data ; return - 1 ; } int dr = - 1 ; if ( target > root . data ) { dr = kDistanceSum ( root . right , target , k ) ; } if ( dr != - 1 ) { if ( dr + 1 == k ) sum += root . data ; else kDistanceDownSum ( root . left , k - dr - 2 ) ; return 1 + dr ; } return - 1 ; } static TreeNode insertNode ( int data , TreeNode root ) { if ( root == null ) { TreeNode node = new TreeNode ( data ) ; return node ; } else if ( data > root . data ) { root . right = insertNode ( data , root . right ) ; } else if ( data <= root . data ) { root . left = insertNode ( data , root . left ) ; } return root ; } static void findSum ( TreeNode root , int target , int K ) { sum = 0 ; kDistanceSum ( root , target , K ) ; System . out . print ( sum ) ; } public static void main ( String [ ] args ) { TreeNode root = null ; int N = 11 ; int tree [ ] = { 3 , 1 , 7 , 0 , 2 , 5 , 10 , 4 , 6 , 9 , 8 } ; for ( int i = 0 ; i < N ; i ++ ) { root = insertNode ( tree [ i ] , root ) ; } int target = 7 ; int K = 2 ; findSum ( root , target , K ) ; } }
import java . io . * ; class GFG { static int MAXN = 10000000 ; static boolean can ( int Mid , int X , int Y , int A , int B ) { int p1 = X - Mid * B ; int p2 = Y - Mid * B ; if ( p1 < 0 p2 < 0 ) { return false ; } int k = A - B ; if ( k == 0 ) { return true ; } int val = p1 / k + p2 / k ; if ( val >= Mid ) { return true ; } return false ; } static int maxPossibleMoves ( int X , int Y , int A , int B ) { int ans = 0 ; int L = 1 , R = MAXN ; while ( L <= R ) { int Mid = ( L + R ) / 2 ; if ( can ( Mid , X , Y , A , B ) ) { L = Mid + 1 ; ans = Math . max ( ans , Mid ) ; } else { R = Mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int X = 10 , Y = 12 , A = 2 , B = 5 ; if ( A < B ) { int temp = A ; A = B ; B = temp ; } System . out . println ( maxPossibleMoves ( X , Y , A , B ) ) ; } }
import java . io . * ; class GFG { static void printFactors ( int n ) { for ( int i = 2 ; i <= ( int ) Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { int d1 = i , d2 = n ; while ( d2 % d1 == 0 ) { d2 = d2 / d1 ; } if ( d1 > 1 && d2 > 1 ) { System . out . print ( d1 + " , ▁ " + d2 ) ; return ; } } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int N = 10 ; printFactors ( N ) ; } }
class GFG { static final int MAXN = 100000 ; static int [ ] bit1 = new int [ MAXN ] ; static int [ ] bit2 = new int [ MAXN ] ; static void update ( int idx , int val , int [ ] bit ) { while ( idx < bit . length ) { bit [ idx ] += val ; idx += idx & ( - idx ) ; } } static int query ( int idx , int [ ] bit ) { int res = 0 ; while ( idx > 0 ) { res += bit [ idx ] ; idx -= idx & ( - idx ) ; } return res ; } static void buildFenwickTree ( int a [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { update ( i , a [ i - 1 ] , bit1 ) ; update ( i , a [ i - 1 ] * a [ i - 1 ] , bit2 ) ; } } static void pairwiseProductSum ( int a [ ] , int l , int r ) { int sum , e , q ; e = query ( r , bit1 ) - query ( l - 1 , bit1 ) ; e = e * e ; q = query ( r , bit2 ) - query ( l - 1 , bit2 ) ; sum = ( e - q ) / 2 ; System . out . print ( sum + "NEW_LINE"); } static void updateArray ( int [ ] a , int p , int x ) { update ( p , - a [ p - 1 ] , bit1 ) ; update ( p , x , bit1 ) ; update ( p , - a [ p - 1 ] * a [ p - 1 ] , bit2 ) ; update ( p , x * x , bit2 ) ; a [ p - 1 ] = x ; } static void solveQueries ( int [ ] a , int n , int Q , int query [ ] [ ] ) { buildFenwickTree ( a , n ) ; for ( int i = 0 ; i < Q ; i ++ ) { if ( query [ i ] [ 0 ] == 1 ) pairwiseProductSum ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; else updateArray ( a , query [ i ] [ 1 ] , query [ i ] [ 2 ] ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 5 , 7 , 2 , 3 , 1 } ; int N = A . length ; int Q = 3 ; int query [ ] [ ] = { { 1 , 1 , 3 } , { 2 , 2 , 5 } , { 1 , 2 , 5 } } ; solveQueries ( A , N , Q , query ) ; } }
import java . io . * ; class GFG { static int countTriplets ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } int c1 = odd * ( even * ( even - 1 ) ) / 2 ; int c2 = ( odd * ( odd - 1 ) * ( odd - 2 ) ) / 6 ; return c1 + c2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 4 , 5 , 10 , 1 , 7 } ; int n = arr . length ; int ans = countTriplets ( arr , n ) ; System . out . println ( ans ) ; } }
import java . io . * ; class GFG { public static int countTotalPairs ( int N , int K ) { if ( K == 0 ) { return N * N ; } int ans = 0 ; for ( int i = K + 1 ; i <= N ; i ++ ) { ans += ( N / i ) * ( i - K ) ; if ( ( N % i ) - K + 1 > 0 ) { ans += ( N % i ) - K + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int N = 5 , K = 2 ; System . out . println ( countTotalPairs ( N , K ) ) ; } }
class GFG { public static int minSumDifference ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ar [ i ] ; int rem = sum % n ; return rem * ( n - rem ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 3 , 6 , 8 , 5 , 2 , 1 , 11 , 7 , 10 , 4 } ; int N = arr . length ; System . out . println ( minSumDifference ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int getPairsCount ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = arr [ i ] - ( i % arr [ i ] ) ; j < n ; j += arr [ i ] ) { if ( i < j && Math . abs ( arr [ i ] - arr [ j ] ) >= Math . min ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int N = arr . length ; System . out . println ( getPairsCount ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int minSteps ( int N ) { int count = 1 , res = 0 ; for ( int i = 1 ; i <= N ; i += count ) { res = Math . max ( res , count ) ; count ++ ; } return res ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minSteps ( N ) ) ; } }
import java . io . * ; import java . lang . Math ; class GFG { static int minSteps ( int N ) { int res = ( ( int ) Math . sqrt ( 1 + 8 * N ) - 1 ) / 2 ; return res ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minSteps ( N ) + "NEW_LINE"); } }
import java . io . * ; class GFG { static void oppositeFaceOfDice ( int N ) { int ans = 7 - N ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 2 ; oppositeFaceOfDice ( N ) ; } }
import java . util . * ; class GFG { static int countReachablePoints ( int X , int Y , int L , int R ) { int diff_arr [ ] = new int [ 100000 ] ; int count = 0 ; diff_arr [ X ] = 1 ; diff_arr [ X + 1 ] = - 1 ; for ( int i = X ; i <= Y ; i ++ ) { diff_arr [ i ] += diff_arr [ i - 1 ] ; if ( diff_arr [ i ] >= 1 ) { diff_arr [ i + L ] += 1 ; diff_arr [ i + R + 1 ] -= 1 ; count ++ ; } } return count ; } public static void main ( String [ ] args ) { int X = 3 , Y = 12 , L = 2 , R = 3 ; System . out . print ( countReachablePoints ( X , Y , L , R ) ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node newNode ( int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = null ; return new_node ; } static Node reverse ( Node head ) { Node prev = null ; Node current = head ; Node next ; while ( current != null ) { next = current . next ; current . next = prev ; prev = current ; current = next ; } return prev ; } static Node multiplyHelp ( Node head , int M ) { Node res = head ; Node prev = null ; int carry = 0 , product = 1 ; while ( head != null ) { product = head . data * M ; product += carry ; carry = product / 10 ; head . data = product % 10 ; prev = head ; head = head . next ; } if ( carry > 0 ) prev . next = newNode ( carry ) ; return res ; } static Node multiply ( Node head , int M ) { head = reverse ( head ) ; head = multiplyHelp ( head , M ) ; return reverse ( head ) ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data ) ; node = node . next ; } } public static void main ( String [ ] args ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 7 ) ; head . next . next . next = newNode ( 3 ) ; int M = 3 ; head = multiply ( head , M ) ; printList ( head ) ; } }
import java . util . * ; class GFG { static int [ ] s = new int [ 11 ] ; static void sieveOfEratosthenes ( int N ) { boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 2 ; i < N + 1 ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i < N + 1 ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j < N / i + 1 ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int generatePrimeFactors ( int N ) { int curr = s [ N ] ; HashMap < Integer , Integer > cnt = new HashMap < > ( ) ; cnt . put ( s [ N ] , 1 ) ; while ( N > 1 ) { N /= s [ N ] ; if ( N != 0 && s [ N ] != 0 ) if ( ! cnt . containsKey ( s [ N ] ) ) cnt . put ( s [ N ] , 1 ) ; else cnt . put ( s [ N ] , cnt . get ( s [ N ] ) + 1 ) ; } if ( cnt . containsKey ( 0 ) ) cnt . remove ( 0 ) ; int totfactor = 1 ; for ( Map . Entry < Integer , Integer > i : cnt . entrySet ( ) ) totfactor *= i . getValue ( ) + 1 ; return totfactor ; } static int countTriplets ( int N ) { int CountTriplet = 0 ; for ( int z = 1 ; z < N + 1 ; z ++ ) { int p = generatePrimeFactors ( N - z ) ; if ( p > 1 ) CountTriplet += p ; } return CountTriplet + 1 ; } public static void main ( String [ ] args ) { int N = 10 ; sieveOfEratosthenes ( N ) ; System . out . print ( countTriplets ( N ) ) ; } }
import java . util . * ; class GFG { static String checkSum ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } Vector < Integer > [ ] freq = new Vector [ n + 1 ] ; for ( int i = 0 ; i < freq . length ; i ++ ) freq [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int f = m . get ( arr [ i ] ) ; if ( f != - 1 ) { freq [ f ] . add ( arr [ i ] ) ; m . put ( arr [ i ] , - 1 ) ; } } int count = 0 ; int kleastfreqsum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int x : freq [ i ] ) { kleastfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } count = 0 ; int kmostfreqsum = 0 ; for ( int i = n ; i >= 0 ; i -- ) { for ( int x : freq [ i ] ) { kmostfreqsum += x ; count ++ ; if ( count == k ) break ; } if ( count == k ) break ; } if ( kleastfreqsum == kmostfreqsum ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 2 , 3 , 3 , 4 } ; int N = arr . length ; int K = 2 ; System . out . print ( checkSum ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static Vector < Integer > solve ( int start , int end ) { String num = " " ; Vector < Integer > ans = new Vector < > ( ) ; for ( int i = 1 ; i <= 9 ; i ++ ) { num = Integer . toString ( i ) ; int value = i ; if ( value >= start && value <= end ) { ans . add ( value ) ; } for ( int j = i + 1 ; j <= 9 ; j ++ ) { num += Integer . toString ( j ) ; value = Integer . valueOf ( num ) ; if ( value >= start && value <= end ) { ans . add ( value ) ; } } } Collections . sort ( ans ) ; ; return ans ; } public static void main ( String [ ] args ) { int L = 12 , R = 87 ; Vector < Integer > ans = solve ( L , R ) ; for ( int it : ans ) System . out . print ( it + " ▁ " ) ; } }
import java . io . * ; class GFG { static int minDistance ( int start [ ] , int end [ ] , int n , int d ) { int left = Integer . MIN_VALUE ; int right = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { left = Math . max ( left , start [ i ] ) ; right = Math . min ( right , end [ i ] ) ; } if ( left > right ) return - 1 ; if ( d >= left && d <= right ) return 0 ; if ( d < left ) return ( left - d ) ; if ( d > right ) return ( d - right ) ; return - 1 ; } public static void main ( String [ ] args ) { int start [ ] = { 0 , 2 , 4 } ; int end [ ] = { 7 , 14 , 6 } ; int n = start . length ; int d = 3 ; System . out . println ( minDistance ( start , end , n , d ) ) ; } }
import java . util . * ; class GFG { static int power ( int x , int y ) { if ( y == 0 ) return 1 ; if ( y % 2 == 0 ) return power ( x , y / 2 ) * power ( x , y / 2 ) ; return x * power ( x , y / 2 ) * power ( x , y / 2 ) ; } static int order ( int num ) { int count = 0 ; while ( num > 0 ) { count ++ ; num = num / 10 ; } return count ; } static int isArmstrong ( int N ) { int r = order ( N ) ; int temp = N , sum = 0 ; while ( temp > 0 ) { int d = temp % 10 ; sum += power ( d , r ) ; temp = temp / 10 ; } if ( sum == N ) return 1 ; return 0 ; } static int maxSum ( int [ ] arr , int N , int K ) { if ( N < K ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < K ; i ++ ) { res += arr [ i ] ; } int curr_sum = res ; for ( int i = K ; i < N ; i ++ ) { curr_sum += arr [ i ] - arr [ i - K ] ; res = Math . max ( res , curr_sum ) ; } return res ; } static int maxArmstrong ( int [ ] arr , int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = isArmstrong ( arr [ i ] ) ; } return maxSum ( arr , N , K ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 28 , 2 , 3 , 6 , 153 , 99 , 828 , 24 } ; int K = 6 ; int N = arr . length ; System . out . println ( maxArmstrong ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int countArrays ( int n , int k ) { int mod = 1000000007 ; int ans = power ( n , k , mod ) ; return ans ; } public static void main ( String [ ] args ) { int n = 3 , k = 5 ; int ans = countArrays ( n , k ) ; System . out . println ( ans ) ; } }
class GFG { public static void countPairs ( int arr [ ] , int N ) { int answer = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] + arr [ j ] == i + j ) { answer ++ ; } } } System . out . println ( answer ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
import java . io . * ; class GFG { static void checkDistribution ( int R , int B , int D ) { if ( Math . max ( R , B ) <= Math . min ( R , B ) * ( D + 1 ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int R = 1 , B = 1 , D = 0 ; checkDistribution ( R , B , D ) ; } }
import java . io . * ; class GFG { static int itemType ( int n ) { int count = 0 ; for ( int day = 1 ; ; day ++ ) { for ( int type = day ; type > 0 ; type -- ) { count += type ; if ( count >= n ) return type ; } } } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( itemType ( N ) ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > primeFactor ( int N ) { Vector < pair > v = new Vector < > ( ) ; int count = 0 ; while ( ( N % 2 ) == 0 ) { N >>= 1 ; count ++ ; } if ( count != 0 ) v . add ( new pair ( 2 , count ) ) ; for ( int i = 3 ; i <= Math . sqrt ( N ) ; i += 2 ) { count = 0 ; while ( N % i == 0 ) { count ++ ; N = N / i ; } if ( count != 0 ) { v . add ( new pair ( i , count ) ) ; } } if ( N > 2 ) v . add ( new pair ( N , 1 ) ) ; return v ; } static void printAnswer ( int n ) { Vector < pair > v = primeFactor ( n ) ; int maxi_size = 0 , prime_factor = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( maxi_size < v . get ( i ) . second ) { maxi_size = v . get ( i ) . second ; prime_factor = v . get ( i ) . first ; } } if ( maxi_size < 2 ) { System . out . print ( 1 << ' ▁ ' ) ; } else { int product = 1 ; System . out . print ( maxi_size + "NEW_LINE"); for ( int i = 0 ; i < maxi_size - 1 ; i ++ ) { System . out . print ( prime_factor + " ▁ " ) ; product *= prime_factor ; } System . out . print ( ( n / product ) ) ; } } public static void main ( String [ ] args ) { int N = 360 ; printAnswer ( N ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void IsEvenPath ( int Source [ ] , int Destination [ ] ) { int x_dif = Math . abs ( Source [ 0 ] - Destination [ 0 ] ) ; int y_dif = Math . abs ( Source [ 1 ] - Destination [ 1 ] ) ; int minsteps = x_dif + y_dif ; if ( minsteps % 2 == 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int Source [ ] = { 2 , 1 } ; int Destination [ ] = { 1 , 4 } ; IsEvenPath ( Source , Destination ) ; } }
class GFG { public static String BaseConversion ( int N ) { String s = " " ; while ( N != 0 ) { if ( N % 2 == 0 ) { s = "0" + s ; } else { s = "1" + s ; N -- ; } N /= - 2 ; } if ( s == " " ) { s = "0" ; } return s ; } public static void main ( String args [ ] ) { int N = - 9 ; System . out . println ( BaseConversion ( N ) ) ; } }
class GFG { public static void finalInstances ( int instances , int [ ] arr ) { int i = 0 ; while ( i < arr . length ) { if ( arr [ i ] < 25 && instances > 1 ) { instances = ( instances / 2 ) ; i = i + 10 ; } else if ( arr [ i ] > 60 && instances <= Math . pow ( 10 , 8 ) ) { instances = instances * 2 ; i = i + 10 ; } i = i + 1 ; } System . out . println ( instances ) ; } public static void main ( String args [ ] ) { int instances = 2 ; int [ ] arr = { 25 , 23 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 76 , 80 } ; finalInstances ( instances , arr ) ; } }
import java . io . * ; class GFG { static void check ( int N ) { int twos = 0 , fives = 0 ; while ( N % 2 == 0 ) { N /= 2 ; twos ++ ; } while ( N % 5 == 0 ) { N /= 5 ; fives ++ ; } if ( N == 1 && twos <= fives ) { System . out . println ( 2 * fives - twos ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { int N = 50 ; check ( N ) ; } }
import java . util . ArrayList ; import java . util . HashMap ; class GFG { static String isMakeZero ( int arr [ ] , int N , int K ) { HashMap < Integer , Integer > MP = new HashMap < Integer , Integer > ( ) ; ArrayList < Integer > V = new ArrayList < Integer > ( ) ; int X = 1 ; int i ; while ( X > 0 && X < Integer . MAX_VALUE ) { V . add ( X ) ; X *= K ; } for ( i = 0 ; i < N ; i ++ ) { for ( int j = V . size ( ) - 1 ; j >= 0 ; j -- ) { if ( MP . containsKey ( V . get ( j ) ) == false && V . get ( j ) <= arr [ i ] ) { arr [ i ] -= V . get ( j ) ; MP . put ( V . get ( j ) , 1 ) ; } } if ( arr [ i ] != 0 ) break ; } if ( i < N ) return " No " ; else return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 0 , 3 , 4 , 80 } ; int N = arr . length ; int K = 2 ; System . out . println ( isMakeZero ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static String perfectSquare ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int p = ( int ) Math . sqrt ( arr [ i ] ) ; if ( p * p != arr [ i ] ) { return " No " ; } } return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 100 } ; int N = arr . length ; System . out . println ( perfectSquare ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int countKAverageSubarrays ( int arr [ ] , int n , int k ) { int res = 0 ; for ( int L = 0 ; L < n ; L ++ ) { int sum = 0 ; for ( int R = L ; R < n ; R ++ ) { sum += arr [ R ] ; int len = ( R - L + 1 ) ; if ( sum % len == 0 ) { int avg = sum / len ; if ( avg == k ) res ++ ; } } } return res ; } public static void main ( String [ ] args ) { int K = 6 ; int arr [ ] = { 12 , 5 , 3 , 10 , 4 , 8 , 10 , 12 , - 6 , - 1 } ; int N = arr . length ; System . out . print ( countKAverageSubarrays ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static int minimumElement ( int arr [ ] , int N , int K ) { int minElement = arr [ 0 ] ; for ( int i = 0 ; i < N ; ++ i ) { minElement = Math . min ( minElement , arr [ i ] ) ; } int reqOperations = 0 ; for ( int i = 0 ; i < N ; ++ i ) { reqOperations += arr [ i ] - minElement ; } if ( reqOperations < K ) { K -= reqOperations ; minElement -= ( K + N - 1 ) / N ; } return minElement ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 10 , 10 , 10 } ; int K = 7 ; int N = arr . length ; System . out . println ( minimumElement ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void findPermutation ( int N ) { int [ ] arr = new int [ N ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] = i + 1 ; } for ( int i = 1 ; i < N ; i += 2 ) { temp = arr [ i ] ; arr [ i ] = arr [ i - 1 ] ; arr [ i - 1 ] = temp ; } if ( N % 2 == 1 && N > 1 ) { temp = arr [ N - 1 ] ; arr [ N - 1 ] = arr [ N - 2 ] ; arr [ N - 2 ] = temp ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 7 ; findPermutation ( N ) ; } }
import java . io . * ; class GFG { static void rangeSum ( int arr [ ] , int N , int L , int R ) { int sum = 0 ; for ( int i = L - 1 ; i < R ; i ++ ) { sum += arr [ i % N ] ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 6 , 9 } ; int L = 10 , R = 13 ; int N = arr . length ; rangeSum ( arr , N , L , R ) ; } }
import java . io . * ; class GFG { static void rangeSum ( int arr [ ] , int N , int L , int R ) { int prefix [ ] = new int [ N + 1 ] ; prefix [ 0 ] = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i - 1 ] ; } int leftsum = ( ( L - 1 ) / N ) * prefix [ N ] + prefix [ ( L - 1 ) % N ] ; int rightsum = ( R / N ) * prefix [ N ] + prefix [ R % N ] ; System . out . print ( rightsum - leftsum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 6 , 9 } ; int L = 10 , R = 13 ; int N = arr . length ; rangeSum ( arr , N , L , R ) ; } }
class GFG { public static void addAP ( int A [ ] , int Q , int [ ] [ ] operations ) { for ( int j = 0 ; j < 2 ; ++ j ) { int L = operations [ j ] [ 0 ] , R = operations [ j ] [ 1 ] , a = operations [ j ] [ 2 ] , d = operations [ j ] [ 3 ] ; int curr = a ; for ( int i = L - 1 ; i < R ; i ++ ) { A [ i ] += curr ; curr += d ; } } for ( int i = 0 ; i < 4 ; ++ i ) System . out . print ( A [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 5 , 4 , 2 , 8 } ; int Q = 2 ; int query [ ] [ ] = { { 1 , 2 , 1 , 3 } , { 1 , 4 , 4 , 1 } } ; addAP ( A , Q , query ) ; } }
class GFG { public static void maximizeNumber ( int N , int K ) { String s = Integer . toString ( N ) ; int L = s . length ( ) ; String result = " " ; int i = 0 ; while ( ( i < L ) && ( K <= ( ( int ) s . charAt ( i ) - ( int ) '0' ) ) ) { result += ( s . charAt ( i ) ) ; ++ i ; } result += ( ( char ) ( K + ( int ) '0' ) ) ; while ( i < L ) { result += ( s . charAt ( i ) ) ; ++ i ; } System . out . println ( result ) ; } public static void main ( String args [ ] ) { int N = 6673 , K = 6 ; maximizeNumber ( N , K ) ; } }
public class GFG { static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( y % 2 != 0 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } static void countArrays ( int N , int K ) { System . out . println ( ( int ) ( power ( N , K ) ) ) ; } public static void main ( String args [ ] ) { int N = 5 , K = 6 ; countArrays ( N , K ) ; } }
class GFG { public static int countOfPairs ( int arr [ ] , int N , int X ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( ( arr [ i ] ^ arr [ j ] ) & X ) == 0 ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 2 , 5 , 4 , 6 , 7 } ; int X = 6 ; int N = arr . length ; System . out . println ( countOfPairs ( arr , N , X ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000 ; static int cntMatrices ( int [ ] [ ] arr , int N , int M , int K ) { int cnt = 0 ; int [ ] [ ] pre = new int [ N + 1 ] [ M + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { pre [ i ] [ j ] = arr [ i - 1 ] [ j - 1 ] + pre [ i - 1 ] [ j ] + pre [ i ] [ j - 1 ] - pre [ i - 1 ] [ j - 1 ] ; } } for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= M ; j ++ ) { for ( int l = i , r = j ; l > 0 && r > 0 ; l -- , r -- ) { int sum1 = ( K * ( i - l + 1 ) * ( i - r + 1 ) ) ; int sum2 = pre [ i ] [ j ] - pre [ l - 1 ] [ r ] - pre [ l ] [ r - 1 ] + pre [ l - 1 ] [ r - 1 ] ; if ( sum1 <= sum2 ) cnt ++ ; } } } return cnt ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 2 , 2 , 3 } , { 3 , 4 , 5 } , { 4 , 5 , 5 } } ; int K = 4 ; int N = arr . length ; int M = arr [ 0 ] . length ; System . out . println ( cntMatrices ( arr , N , M , K ) ) ; } }
import java . util . * ; class GFG { static int ConcatenateArr ( int [ ] arr , int N ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { int l = ( int ) Math . floor ( Math . log10 ( arr [ i ] ) + 1 ) ; ans = ans * ( int ) Math . pow ( 10 , l ) ; ans += arr [ i ] ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 23 , 456 } ; int N = arr . length ; System . out . println ( ConcatenateArr ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int countXor ( int N ) { int cnt = ( int ) N / 2 + 1 ; return cnt ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( countXor ( N ) ) ; } }
import java . util . * ; class GFG { static int DistRecursion ( String S , int i , int dist ) { char [ ] ch = S . toCharArray ( ) ; if ( i == ch . length ) return Math . abs ( dist ) ; if ( ch [ i ] == ' L ' ) return DistRecursion ( S , i + 1 , dist - 1 ) ; if ( ch [ i ] == ' R ' ) return DistRecursion ( S , i + 1 , dist + 1 ) ; return Math . max ( DistRecursion ( S , i + 1 , dist - 1 ) , DistRecursion ( S , i + 1 , dist + 1 ) ) ; } static int maxDistance ( String S ) { return DistRecursion ( S , 0 , 0 ) ; } public static void main ( String [ ] args ) { String S = " ? RRR ? " ; System . out . print ( maxDistance ( S ) ) ; } }
class GFG { static int ExpoFactorial ( int N ) { int res = 1 ; int mod = 1000000007 ; for ( int i = 2 ; i < N + 1 ; i ++ ) res = ( int ) Math . pow ( i , res ) % mod ; return res ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( ( ExpoFactorial ( N ) ) ) ; } }
import java . util . * ; class GFG { static int maxSubArraySumRepeated ( int [ ] arr , int N , int K ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int curr = arr [ 0 ] ; int ans = arr [ 0 ] ; if ( K == 1 ) { for ( int i = 1 ; i < N ; i ++ ) { curr = Math . max ( arr [ i ] , curr + arr [ i ] ) ; ans = Math . max ( ans , curr ) ; } return ans ; } ArrayList < Integer > V = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < 2 * N ; i ++ ) { V . add ( arr [ i % N ] ) ; } int maxSuf = V . get ( 0 ) ; int maxPref = V . get ( 2 * N - 1 ) ; curr = V . get ( 0 ) ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr += V . get ( i ) ; maxPref = Math . max ( maxPref , curr ) ; } curr = V . get ( 2 * N - 1 ) ; for ( int i = 2 * N - 2 ; i >= 0 ; i -- ) { curr += V . get ( i ) ; maxSuf = Math . max ( maxSuf , curr ) ; } curr = V . get ( 0 ) ; for ( int i = 1 ; i < 2 * N ; i ++ ) { curr = Math . max ( V . get ( i ) , curr + V . get ( i ) ) ; ans = Math . max ( ans , curr ) ; } if ( sum > 0 ) { int temp = sum * ( K - 2 ) ; ans = Math . max ( ans , Math . max ( temp + maxPref , temp + maxSuf ) ) ; } return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 20 , - 30 , - 1 , 40 } ; int N = arr . length ; int K = 10 ; System . out . print ( maxSubArraySumRepeated ( arr , N , K ) ) ; } }
import java . io . * ; class GFG { static void printRectPattern ( int c1 , int c2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( ( Math . max ( Math . abs ( c1 - i ) , Math . abs ( c2 - j ) ) ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int c1 = 2 ; int c2 = 2 ; int n = 5 ; printRectPattern ( c1 , c2 , n ) ; } }
import java . io . * ; class GFG { static int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( ( i & 1 ) == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int l = 10 , r = 20 ; System . out . println ( countNumbers ( l , r ) ) ; } }
import java . io . * ; class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 2 ) - ( l - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int l = 10 ; int r = 20 ; System . out . println ( countNumbers ( l , r ) ) ; } }
import java . io . * ; class GFG { public static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } public static boolean isFibonacci ( int n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } public static int contiguousFibonacciNumber ( int arr [ ] , int n ) { int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isFibonacci ( arr [ i ] ) ) { current_length ++ ; } else { current_length = 0 ; } max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 8 , 21 , 5 , 3 , 28 , 4 } ; int n = arr . length ; System . out . println ( contiguousFibonacciNumber ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int [ ] [ ] direction = { { - 1 , 0 } , { 0 , 1 } , { 0 , - 1 } , { 1 , 0 } } ; static boolean dfs ( int matrix [ ] [ ] , int i , int j , int N , int M ) { if ( i == N - 1 && j == M - 1 ) { return true ; } matrix [ i ] [ j ] = 1 ; for ( int k = 0 ; k < 4 ; k ++ ) { int newX = i + direction [ k ] [ 0 ] ; int newY = j + direction [ k ] [ 1 ] ; if ( newX >= 0 && newX < N && newY >= 0 && newY < M && matrix [ newX ] [ newY ] == 0 ) { if ( dfs ( matrix , newX , newY , N , M ) ) { return true ; } } } return false ; } static int solve ( int [ ] [ ] matrix ) { int N = matrix . length ; int M = matrix [ 0 ] . length ; if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 0 ; } if ( ! dfs ( matrix , 0 , 0 , N , M ) ) { return 1 ; } return 2 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 1 , 0 , 0 } , { 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( solve ( mat ) ) ; } }
import java . util . HashMap ; class GFG { public static void arrayPartition ( int a [ ] , int n ) { int [ ] ans = new int [ n ] ; int cnt = 0 ; int ind = 0 , flag = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( a [ i ] ) ) { mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . put ( a [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 ) cnt ++ ; if ( mp . get ( a [ i ] ) > 2 && flag == 0 ) { flag = 1 ; ind = i ; } } int p = ( cnt + 1 ) / 2 ; int ans1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans [ i ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) == 1 && ans1 < p ) { ans [ i ] = 1 ; ans1 ++ ; } else if ( mp . get ( a [ i ] ) == 1 ) { ans [ i ] = 2 ; } } if ( cnt % 2 == 1 && flag == 0 ) { System . out . println ( - 1 + "NEW_LINE"); return ; } if ( cnt % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } else { for ( int i = 0 ; i < n ; i ++ ) { if ( ind == i ) System . out . print ( 2 + " ▁ " ) ; else System . out . print ( ans [ i ] + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 4 } ; int N = arr . length ; arrayPartition ( arr , N ) ; } }
import java . io . * ; class GFG { static void evenOdd ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int even_digits = 0 ; int odd_digits = 0 ; int temp = arr [ i ] ; while ( temp > 0 ) { if ( ( temp % 10 ) % 2 != 0 ) odd_digits ++ ; else even_digits ++ ; temp /= 10 ; } if ( even_digits > odd_digits ) { int res = 0 ; while ( arr [ i ] > 0 ) { res += arr [ i ] % 10 ; arr [ i ] /= 10 ; } System . out . print ( res + " ▁ " ) ; } else if ( odd_digits > even_digits ) { int res = 1 ; while ( arr [ i ] > 0 ) { res *= arr [ i ] % 10 ; arr [ i ] /= 10 ; } System . out . print ( res + " ▁ " ) ; } else System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 113 , 141 , 214 , 3186 } ; int N = arr . length ; evenOdd ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; class GFG { public static int sumOfFirstM ( int A [ ] , int N , int M ) { int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { sum = sum + A [ i % N ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int M = 5 ; int N = arr . length ; System . out . println ( sumOfFirstM ( arr , N , M ) ) ; } }
import java . io . * ; class GFG { static void canReach0toM ( int [ ] [ ] a , int n , int m ) { int [ ] rightMost = new int [ m + 1 ] ; int [ ] dp = new int [ m + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { rightMost [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { int a1 = a [ i ] [ 0 ] ; int b1 = a [ i ] [ 1 ] ; rightMost [ a1 ] = Math . max ( rightMost [ a1 ] , b1 ) ; } for ( int i = m ; i >= 0 ; i -- ) { dp [ i ] = i ; for ( int j = Math . min ( m , rightMost [ i ] ) ; j > i ; j -- ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] ) ; } } if ( dp [ 0 ] >= m ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 0 , 2 } , { 2 , 2 } , { 2 , 5 } , { 4 , 5 } } ; int M = 5 ; int N = arr . length ; canReach0toM ( arr , N , M ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void countPairs ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int x = 1 ; x <= arr [ i ] ; x ++ ) { for ( int y = x ; y <= arr [ i ] ; y ++ ) { if ( gcd ( x , y ) > 1 ) count ++ ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
import java . util . * ; class GFG { static int MAX = 1005 ; static void preCalculate ( int [ ] phi , int [ ] ans ) { phi [ 0 ] = 0 ; phi [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) phi [ i ] = i ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( phi [ i ] == i ) { for ( int j = i ; j <= MAX ; j += i ) phi [ j ] -= ( phi [ j ] / i ) ; } } for ( int i = 1 ; i <= MAX ; i ++ ) ans [ i ] = ans [ i - 1 ] + ( i - phi [ i ] ) ; } static void countPairs ( int [ ] arr , int N ) { int [ ] phi = new int [ 100000 ] ; Arrays . fill ( phi , 0 ) ; int [ ] ans = new int [ 100000 ] ; Arrays . fill ( ans , 0 ) ; preCalculate ( phi , ans ) ; for ( int i = 0 ; i < N ; ++ i ) { System . out . print ( ans [ arr [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 20 } ; int N = 3 ; countPairs ( arr , N ) ; } }
import java . io . * ; class GFG { public static void countSubarray ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int mxSubarray = 0 ; int mxOther = 0 ; for ( int k = i ; k <= j ; k ++ ) { mxSubarray = Math . max ( mxSubarray , arr [ k ] ) ; } for ( int k = 0 ; k < i ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { mxOther = Math . max ( mxOther , arr [ k ] ) ; } if ( mxSubarray > ( 2 * mxOther ) ) count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = arr . length ; countSubarray ( arr , N ) ; } }
import java . util . * ; class GFG { static void countSubarray ( int [ ] arr , int n ) { int L = 0 , R = 0 ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) mx = Math . max ( mx , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] * 2 > mx ) { L = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] * 2 > mx ) { R = i ; break ; } } System . out . println ( ( L + 1 ) * ( n - R ) ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 6 , 10 , 9 , 7 , 3 } ; int N = arr . length ; countSubarray ( arr , N ) ; } }
import java . io . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int minOperations ( int A , int B ) { int cnt1 = 0 , cnt2 = 0 ; cnt1 += countSetBits ( A ) ; cnt2 += countSetBits ( B ) ; if ( ( cnt1 + cnt2 ) % 2 != 0 ) return - 1 ; int oneZero = 0 , zeroOne = 0 ; int ans = 0 ; for ( int i = 0 ; i < Math . max ( cnt1 , cnt2 ) ; i ++ ) { int bitpos = 1 << i ; if ( ( ( bitpos & A ) == 0 ) && ( ( bitpos & B ) != 0 ) ) zeroOne ++ ; if ( ( ( bitpos & A ) != 0 ) && ( ( bitpos & B ) == 0 ) ) oneZero ++ ; } ans = ( zeroOne / 2 ) + ( oneZero / 2 ) ; if ( zeroOne % 2 != 0 ) ans += 2 ; return ans ; } public static void main ( String args [ ] ) { int A = 27 , B = 5 ; System . out . println ( minOperations ( A , B ) ) ; } }
import java . util . Arrays ; class GFG { public static Integer maximumSum ( int [ ] A , int N , int K ) { int curr_sum = 0 ; int max_sum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N - K + 1 ; i ++ ) { int [ ] dupl_arr = Arrays . copyOfRange ( A , i , i + K ) ; Arrays . sort ( dupl_arr ) ; Boolean flag = true ; for ( int j = 1 ; j < K ; j ++ ) { if ( dupl_arr [ j ] - dupl_arr [ j - 1 ] != 1 ) { flag = false ; break ; } } if ( flag ) { int temp = 0 ; curr_sum = 0 ; for ( int x = 0 ; x < dupl_arr . length ; x ++ ) { curr_sum += dupl_arr [ x ] ; } max_sum = Math . max ( max_sum , curr_sum ) ; curr_sum = 0 ; } } return max_sum ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 12 , 9 , 8 , 10 , 15 , 1 , 3 , 2 } ; int K = 3 ; int N = arr . length ; System . out . println ( maximumSum ( arr , N , K ) ) ; } }
import java . util . ArrayList ; class GFG { static int MAX = 10000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; public static void sieveSundaram ( ) { ArrayList < Boolean > marked = new ArrayList < Boolean > ( MAX / 2 + 100 ) ; for ( int i = 0 ; i < MAX / 2 + 100 ; i ++ ) { marked . add ( false ) ; } for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked . set ( j , true ) ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked . get ( i ) == false ) primes . add ( 2 * i + 1 ) ; } public static Boolean isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { int p = primes . get ( i ) ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } public static Boolean isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) && Math . abs ( X - Y ) == 1 ; } public static Integer countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) { if ( isSmithBrotherPair ( A [ i ] , A [ j ] ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . length ; System . out . println ( countSmithBrotherPairs ( A , N ) ) ; } }
import java . util . * ; class GFG { static int MAX = 10000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void sieveSundaram ( ) { ArrayList < Boolean > marked = new ArrayList < Boolean > ( MAX / 2 + 100 ) ; for ( int i = 0 ; i < MAX / 2 + 100 ; i ++ ) { marked . add ( false ) ; } for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked . set ( j , true ) ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked . get ( i ) == false ) primes . add ( 2 * i + 1 ) ; } static Boolean isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { int p = primes . get ( i ) ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } static Boolean isSmithBrotherPair ( int X , int Y ) { return isSmith ( X ) && isSmith ( Y ) ; } static Integer countSmithBrotherPairs ( int A [ ] , int N ) { int count = 0 ; Arrays . sort ( A ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( isSmithBrotherPair ( A [ i ] , A [ i + 1 ] ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { sieveSundaram ( ) ; int A [ ] = { 728 , 729 , 28 , 2964 , 2965 } ; int N = A . length ; System . out . print ( countSmithBrotherPairs ( A , N ) ) ; } }
import java . io . * ; class GFG { public static void countPairs ( int arr [ ] , int N ) { int count = 0 , totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( totalSum % ( arr [ i ] + arr [ j ] ) == 0 ) { count += 1 ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int largestK ( int A [ ] , int N ) { int l = 0 , r = N - 1 , flag = 0 ; while ( l < r ) { if ( A [ l ] != A [ r ] ) { flag = 1 ; break ; } l ++ ; r -- ; } if ( flag == 0 ) return - 1 ; int K = Math . abs ( A [ 0 ] - A [ N - 1 ] ) ; for ( int i = 1 ; i < N / 2 ; i ++ ) K = gcd ( K , Math . abs ( A [ i ] - A [ N - i - 1 ] ) ) ; return K ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 2 , 1 } ; int N = A . length ; System . out . println ( largestK ( A , N ) ) ; } }
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 ) ; } }
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 ) ; } }
import java . util . * ; class GFG { static int gcd ( int A , int B ) { if ( B == 0 ) return A ; else return gcd ( B , A % B ) ; } static int getDistinctValues ( int A , int B , int C ) { int g = gcd ( A , B ) ; int num_values = C / g ; return num_values ; } public static void main ( String [ ] args ) { int A = 2 ; int B = 3 ; int C = 10 ; System . out . println ( getDistinctValues ( A , B , C ) ) ; } }
import java . io . * ; class GFG { static int [ ] game_with_number ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { arr [ i ] = arr [ i ] ^ arr [ i + 1 ] ; } return arr ; } static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 11 , 1 , 2 , 3 } ; int n = arr . length ; int [ ] new_arr = game_with_number ( arr , n ) ; print ( new_arr , n ) ; } }
import java . io . * ; class GFG { static int currSubArrayScore ( int [ ] a , int [ ] b , int l , int r ) { int straightScore = 0 ; int reverseScore = 0 ; for ( int i = l ; i <= r ; i ++ ) { straightScore += a [ i ] * b [ i ] ; reverseScore += a [ r - ( i - l ) ] * b [ i ] ; } return Math . max ( straightScore , reverseScore ) ; } static void maxScoreSubArray ( int [ ] a , int [ ] b , int n ) { int res = 0 , start = 0 , end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { int currScore = currSubArrayScore ( a , b , i , j ) ; if ( currScore > res ) { res = currScore ; start = i ; end = j ; } } } System . out . print ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 13 , 4 , 5 } ; int B [ ] = { 10 , 22 , 2 } ; int N = A . length ; maxScoreSubArray ( A , B , N ) ; } }
import java . util . * ; class GFG { static int minimumSizeSubarray ( int arr [ ] , int N ) { int i = 0 , j = N - 1 ; while ( i < N && arr [ i ] == 0 ) { i ++ ; } if ( i == N ) return 1 ; while ( j >= 0 && arr [ j ] == 0 ) { j -- ; } return ( j - i + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 0 , 0 , 12 , 0 , 0 , 0 } ; int N = arr . length ; System . out . print ( minimumSizeSubarray ( arr , N ) ) ; } }
import java . io . * ; class GFG { static void countXorPartition ( int N ) { double a = Math . pow ( 2 , ( int ) ( N - Math . log ( N + 1 ) / Math . log ( 2 ) ) ) ; System . out . print ( a ) ; } public static void main ( String [ ] args ) { int N = 5 ; countXorPartition ( N ) ; } }
class GFG { static int highestPowerof2 ( int n ) { int p = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return ( int ) Math . pow ( 2 , p ) ; } static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; return ( int ) ( Math . ceil ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) == ( int ) ( Math . floor ( ( ( Math . log ( n ) / Math . log ( 2 ) ) ) ) ) ; } static int reduceToOne ( int N ) { if ( N == 1 ) { return 0 ; } if ( isPowerOfTwo ( N ) == true ) { return 1 + reduceToOne ( N / 2 ) ; } else { return 1 + reduceToOne ( N - highestPowerof2 ( N ) ) ; } } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( reduceToOne ( N ) ) ; } }
public class GFG { static int maximumSum ( int arr [ ] , int N ) { int ans = 0 ; for ( int bit = 0 ; bit < 32 ; bit ++ ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & ( 1 << bit ) ) == 1 ) { sum += arr [ i ] ; } } ans = Math . max ( ans , sum ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 1 , 7 , 11 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , N ) ) ; } }
import java . util . HashSet ; public class GFG { static void maxCount ( int arr [ ] , int N , int K ) { HashSet < Integer > st = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { st . add ( arr [ i ] ) ; } if ( N - st . size ( ) <= K ) { System . out . println ( " Yes " ) ; } else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 3 } ; int K = 3 ; int N = arr . length ; maxCount ( arr , N , K ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int max_sum ( int a [ ] , ArrayList < ArrayList < Integer > > v , int q , int n ) { int x = 0 ; int m = Integer . MIN_VALUE ; for ( int i = 0 ; i < q ; i ++ ) { int p , qq , k ; p = v . get ( i ) . get ( 0 ) ; qq = v . get ( i ) . get ( 1 ) ; k = v . get ( i ) . get ( 2 ) ; a [ p ] += k ; if ( qq + 1 <= n ) a [ qq + 1 ] -= k ; } for ( int i = 1 ; i <= n ; i ++ ) { x += a [ i ] ; m = Math . max ( m , x ) ; } return m ; } public static void main ( String [ ] args ) { int n = 10 , q = 3 ; int [ ] a = new int [ n + 5 ] ; ArrayList < ArrayList < Integer > > v = new ArrayList < > ( ) ; for ( int i = 0 ; i < q ; i ++ ) v . add ( new ArrayList < > ( ) ) ; v . get ( 0 ) . add ( 1 ) ; v . get ( 0 ) . add ( 5 ) ; v . get ( 0 ) . add ( 3 ) ; v . get ( 1 ) . add ( 4 ) ; v . get ( 1 ) . add ( 8 ) ; v . get ( 1 ) . add ( 7 ) ; v . get ( 2 ) . add ( 6 ) ; v . get ( 2 ) . add ( 9 ) ; v . get ( 2 ) . add ( 1 ) ; System . out . println ( max_sum ( a , v , q , n ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int playerA = ( X ^ Y ) ; boolean flag = false ; for ( int i = 1 ; i <= X ; i ++ ) { for ( int j = 1 ; j <= Y ; j ++ ) { int val = ( i ^ j ) ; if ( val > playerA ) { flag = true ; break ; } } if ( flag ) { break ; } } if ( flag ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void findWinner ( int X , int Y ) { int first = ( X ^ Y ) ; int second = ( X + Y ) ; if ( first == second ) { System . out . println ( " No " ) ; } else { System . out . println ( " Yes " ) ; } } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; findWinner ( A , B ) ; } }
public class GFG_JAVA { static int KthSmallest ( int A [ ] , int B [ ] , int N , int K ) { int M = 0 ; for ( int i = 0 ; i < N ; i ++ ) { M = Math . max ( A [ i ] , M ) ; } int freq [ ] = new int [ M + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { freq [ A [ i ] ] += B [ i ] ; } int sum = 0 ; for ( int i = 0 ; i <= M ; i ++ ) { sum += freq [ i ] ; if ( sum >= K ) { return i ; } } return - 1 ; } public static void main ( String [ ] args ) int A [ ] = { 3 , 4 , 5 } ; int B [ ] = { 2 , 1 , 3 } ; int N = A . length ; int K = 4 ; System . out . println ( KthSmallest ( A , B , N , K ) ) ; } }
class GFG { static void prefixFactorialArray ( int A [ ] , int N ) { for ( int i = 1 ; i < N ; i ++ ) { A [ i ] += A [ i - 1 ] ; } int fact [ ] = new int [ A [ N - 1 ] + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= A [ N - 1 ] ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = fact [ A [ i ] ] ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( A [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; prefixFactorialArray ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; } static Node newNode ( int d ) { Node n = new Node ( ) ; n . data = d ; n . left = null ; n . right = null ; return n ; } static void findwt ( Node root , int wt [ ] , TreeMap < Integer , Integer > um , int width ) { if ( root == null ) { return ; } if ( um . containsKey ( width ) ) { um . put ( width , um . get ( width ) + wt [ root . data ] ) ; } else { um . put ( width , wt [ root . data ] ) ; } findwt ( root . left , wt , um , width - 1 ) ; findwt ( root . right , wt , um , width + 1 ) ; } static void solveQueries ( int wt [ ] , Node root , int [ ] [ ] queries ) { TreeMap < Integer , Integer > um = new TreeMap < Integer , Integer > ( ) ; findwt ( root , wt , um , 0 ) ; int x = 0 ; for ( Map . Entry < Integer , Integer > it : um . entrySet ( ) ) { x += it . getValue ( ) ; um . put ( it . getKey ( ) , x ) ; } for ( int i = 0 ; i < queries . length ; i ++ ) { int l = queries [ i ] [ 0 ] ; int r = queries [ i ] [ 1 ] ; int ans = 0 ; if ( um . containsKey ( r ) ) { ans = um . get ( r ) ; } if ( um . containsKey ( l - 1 ) ) { ans -= um . get ( l - 1 ) ; } System . out . println ( ans ) ; } } public static void main ( String [ ] args ) { int N = 8 ; Node root = newNode ( 1 ) ; root . left = newNode ( 3 ) ; root . left . left = newNode ( 5 ) ; root . left . right = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 4 ) ; root . right . right . left = newNode ( 7 ) ; root . right . right . right = newNode ( 0 ) ; int wt [ ] = { 8 , 6 , 4 , 5 , 1 , 2 , 9 , 1 } ; int queries [ ] [ ] = { { - 1 , 1 } , { - 2 , - 1 } , { 0 , 3 } } ; solveQueries ( wt , root , queries ) ; } }
import java . io . * ; import java . util . * ; class GFG { static String recreationalSpot ( int arr [ ] , int N ) { if ( N < 3 ) { return " No " ; } int preMin [ ] = new int [ N ] ; preMin [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { preMin [ i ] = Math . min ( preMin [ i - 1 ] , arr [ i ] ) ; } Stack < Integer > stack = new Stack < Integer > ( ) ; for ( int j = N - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > preMin [ j ] ) { while ( stack . empty ( ) == false && stack . peek ( ) <= preMin [ j ] ) { stack . pop ( ) ; } if ( stack . empty ( ) == false && stack . peek ( ) < arr [ j ] ) { return " Yes " ; } stack . push ( arr [ j ] ) ; } } return " No " ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 11 , 5 , 13 , 2 } ; int size = arr . length ; System . out . println ( recreationalSpot ( arr , size ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean ToCheckPowerofX ( int n , int x ) { while ( n > 0 ) { int rem = n % x ; if ( rem >= 2 ) { return false ; } n = n / x ; } return true ; } public static void main ( String [ ] args ) { int N = 10 , X = 3 ; if ( ToCheckPowerofX ( N , X ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int reverseBin ( int N ) { String S = " " ; int i ; for ( i = 0 ; i < 32 ; i ++ ) { if ( ( N & ( 1L << i ) ) != 0 ) S += '1' ; else S += '0' ; } S = ( new StringBuilder ( S ) ) . reverse ( ) . toString ( ) ; int M = 0 ; for ( i = 0 ; i < 32 ; i ++ ) { if ( S . charAt ( i ) == '1' ) M += ( 1L << i ) ; } return M ; } static int maximumOfTwo ( int N ) { int M = reverseBin ( N ) ; return Math . max ( N , M ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( maximumOfTwo ( N ) ) ; } }
public class GFG { static void findbitwiseOR ( int [ ] a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sub_array = a [ i ] ; res = res | curr_sub_array ; for ( int j = i ; j < n ; j ++ ) { curr_sub_array = curr_sub_array & a [ j ] ; res = res | curr_sub_array ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int N = A . length ; findbitwiseOR ( A , N ) ; } }
import java . io . * ; class GFG { static void findbitwiseOR ( int [ ] a , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res | a [ i ] ; System . out . println ( res ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 } ; int N = A . length ; findbitwiseOR ( A , N ) ; } }
import java . util . * ; class GFG { static class pair { double first , second ; public pair ( double first , double second ) { this . first = first ; this . second = second ; } } static void complexRoot ( int A , int B ) { Vector < pair > ans = new Vector < pair > ( ) ; double X1 = Math . abs ( Math . sqrt ( ( A + Math . sqrt ( A * A + B * B ) ) / 2 ) ) ; double Y1 = B / ( 2 * X1 ) ; ans . add ( new pair ( X1 , Y1 ) ) ; double X2 = - 1 * X1 ; double Y2 = B / ( 2 * X2 ) ; if ( X2 != 0 ) { ans . add ( new pair ( X2 , Y2 ) ) ; } double X3 = ( A - Math . sqrt ( A * A + B * B ) ) / 2 ; if ( X3 > 0 ) { X3 = Math . abs ( Math . sqrt ( X3 ) ) ; double Y3 = B / ( 2 * X3 ) ; ans . add ( new pair ( X3 , Y3 ) ) ; double X4 = - 1 * X3 ; double Y4 = B / ( 2 * X4 ) ; if ( X4 != 0 ) { ans . add ( new pair ( X4 , Y4 ) ) ; } } System . out . print ( " The ▁ Square ▁ roots ▁ are : ▁ " + "NEW_LINE"); for ( pair p : ans ) { System . out . printf ( " % .4f " , p . first ) ; if ( p . second > 0 ) System . out . print ( " + " ) ; if ( p . second != 0 ) System . out . printf ( "%.4f*iNEW_LINE", p.second); else System . out . println ( ) ; } } public static void main ( String [ ] args ) { int A = 0 , B = 1 ; complexRoot ( A , B ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findlcm ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { ans = ( ( ( arr [ i ] * ans ) ) / ( gcd ( arr [ i ] , ans ) ) ) ; } return ans ; } static void minimumRod ( int A [ ] , int N ) { System . out . println ( N * findlcm ( A , N ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int N = arr . length ; minimumRod ( arr , N ) ; } }
class GFG { static void check ( int n ) { int sumOfDigit = 0 ; int prodOfDigit = 1 ; while ( n > 0 ) { int rem ; rem = n % 10 ; sumOfDigit += rem ; prodOfDigit *= rem ; n /= 10 ; } if ( sumOfDigit > prodOfDigit ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 1234 ; check ( N ) ; } }
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 " ) ; } } }
class GFG { static int FindSum ( int [ ] arr , int N ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int power = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( 2 ) ) ; int LesserValue = ( int ) Math . pow ( 2 , power ) ; int LargerValue = ( int ) Math . pow ( 2 , power + 1 ) ; if ( ( arr [ i ] - LesserValue ) == ( LargerValue - arr [ i ] ) ) { res += arr [ i ] ; } } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 24 , 17 , 3 , 8 } ; int N = arr . length ; System . out . println ( FindSum ( arr , N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static void countRelations ( int N ) { System . out . print ( ( power ( 2 , N ) - 2 ) * power ( 2 , N * N - N ) ) ; } public static void main ( String [ ] args ) { int N = 2 ; countRelations ( N ) ; } }
class GFG { static void evenOddBitwiseXOR ( int N ) { System . out . print ( " Even : ▁ " + 0 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i + " ▁ " ) ; } System . out . print ( "NEW_LINE"); System . out . print ( " Odd : ▁ " + 1 + " ▁ " ) ; for ( int i = 4 ; i <= N ; i = i + 4 ) { System . out . print ( i - 1 + " ▁ " ) ; } if ( N % 4 == 2 ) System . out . print ( N + 1 ) ; else if ( N % 4 == 3 ) System . out . print ( N ) ; } public static void main ( String [ ] args ) { int N = 6 ; evenOddBitwiseXOR ( N ) ; } }
class GFG { public static int minOperation ( int N ) { if ( N % 2 == 0 ) { return ( N / 2 ) * ( N / 2 ) ; } int k = ( N - 1 ) / 2 ; return k * ( k + 1 ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minOperation ( N ) ) ; } }
import java . util . * ; class GFG { static int findXORS ( int arr1 [ ] , int arr2 [ ] , int N , int M ) { int res = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { int temp = arr1 [ i ] & arr2 [ j ] ; res ^= temp ; } } return res ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 6 , 5 } ; int N = arr1 . length ; int M = arr2 . length ; System . out . print ( findXORS ( arr1 , arr2 , N , M ) ) ; } }
import java . util . * ; class GFG { static void findPermutation ( int [ ] arr ) { int N = arr . length ; int i = N - 2 ; while ( i >= 0 && arr [ i ] <= arr [ i + 1 ] ) i -- ; if ( i == - 1 ) { System . out . print ( " - 1" ) ; return ; } int j = N - 1 ; while ( j > i && arr [ j ] >= arr [ i ] ) j -- ; while ( j > i && arr [ j ] == arr [ j - 1 ] ) { j -- ; } int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; for ( int it : arr ) { System . out . print ( it + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 6 } ; findPermutation ( arr ) ; } }
import java . util . * ; class GFG { static boolean isPowerof2 ( int n ) { return ( ( n & ( n - 1 ) ) > 0 && n > 0 ) ; } static void countNum ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { boolean flag = isPowerof2 ( i ) ; if ( ! flag ) { count ++ ; } } System . out . print ( count + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 100 ; countNum ( N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void countNum ( int N ) { int ans = ( int ) ( Math . log ( N ) / Math . log ( 2 ) ) + 1 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 100 ; countNum ( N ) ; } }
import java . util . * ; class GFG { static class ListNode { int val ; ListNode next ; } ; static ListNode push ( ListNode head_ref , int node_val ) { ListNode new_node = new ListNode ( ) ; new_node . val = node_val ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void splitListInParts ( ListNode head , int K ) { Vector < ListNode > ans = new Vector < ListNode > ( ) ; if ( head == null ) { while ( K -- > 0 ) ans . add ( null ) ; } int N = 0 ; ListNode p = head ; while ( p . next != null ) { p = p . next ; N ++ ; } int len = N / K ; int rem = N % K ; p = head ; while ( K > 0 && p . next != null ) { int x = len ; ListNode curr_head = p ; ListNode last = p ; if ( rem > 0 ) { p = p . next ; rem -- ; } while ( x -- > 0 ) { if ( last == p ) p = p . next ; else { last . next = p ; last = p ; p = p . next ; } } last . next = null ; ans . add ( curr_head ) ; K -- ; } while ( K > 0 ) { ans . add ( null ) ; K -- ; } System . out . print ( " { " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( " { " ) ; while ( ans . get ( i ) != null ) { System . out . print ( ans . get ( i ) . val + " ▁ " ) ; ans . set ( i , ans . get ( i ) . next ) ; } System . out . print ( " } " ) ; if ( i != ans . size ( ) - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { ListNode root = new ListNode ( ) ; root = push ( root , 8 ) ; root = push ( root , 7 ) ; root = push ( root , 6 ) ; root = push ( root , 5 ) ; root = push ( root , 4 ) ; root = push ( root , 3 ) ; root = push ( root , 2 ) ; root = push ( root , 1 ) ; int K = 3 ; splitListInParts ( root , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void findLast ( int mat [ ] [ ] ) { int m = 3 ; int n = 3 ; Set < Integer > rows = new HashSet < Integer > ( ) ; Set < Integer > cols = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( mat [ i ] [ j ] > 0 ) ) { rows . add ( i ) ; cols . add ( j ) ; } } } int avRows = m - rows . size ( ) ; int avCols = n - cols . size ( ) ; int choices = Math . min ( avRows , avCols ) ; if ( ( choices & 1 ) != 0 ) System . out . println ( " P1" ) ; else System . out . println ( " P2" ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 0 } , { 0 , 0 , 0 } , { 0 , 0 , 1 } } ; findLast ( mat ) ; } }
import java . io . * ; class GFG { static int nearestPow ( int x , int base1 ) { int k = ( int ) ( Math . log ( x ) / Math . log ( base1 ) ) ; if ( Math . abs ( Math . pow ( base1 , k ) - x ) < Math . abs ( Math . pow ( base1 , ( k + 1 ) ) - x ) ) return ( int ) Math . pow ( base1 , k ) ; else return ( int ) Math . pow ( base1 , ( k + 1 ) ) ; } static void replaceWithNearestPowerOfK ( int [ ] arr , int K , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int num = arr [ i ] ; String strEle = String . valueOf ( num ) ; for ( int c = 0 ; c < strEle . length ( ) ; c ++ ) { if ( ( strEle . charAt ( c ) - '0' ) == K ) { arr [ i ] = nearestPow ( arr [ i ] , K ) ; break ; } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 432 , 953 , 232 , 333 } ; int n = arr . length ; int K = 3 ; replaceWithNearestPowerOfK ( arr , K , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static String removeTailing ( int A , int B ) { int N = A + B ; Stack < Character > s = new Stack < Character > ( ) ; String strsum = Integer . toString ( N ) ; for ( int i = 0 ; i < strsum . length ( ) ; i ++ ) { s . push ( strsum . charAt ( i ) ) ; } while ( s . peek ( ) == '0' ) { s . pop ( ) ; } String res = " " ; while ( s . empty ( ) == false ) { res = res + ( char ) s . peek ( ) ; s . pop ( ) ; } StringBuilder str = new StringBuilder ( ) ; str . append ( res ) ; str . reverse ( ) ; return str . toString ( ) ; } public static void main ( String [ ] args ) { int A = 130246 , B = 450164 ; System . out . println ( removeTailing ( A , B ) ) ; } }
import java . util . * ; class GFG { static void findSumOfGP ( double a , double r ) { if ( Math . abs ( r ) >= 1 ) { System . out . print ( " Infinite " ) ; return ; } double sum = a / ( 1 - r ) ; System . out . print ( sum ) ; } public static void main ( String [ ] args ) { double A = 1 , R = 0.5 ; findSumOfGP ( A , R ) ; } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( long x , int y ) { int res = 1 ; x = x % mod ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( int ) ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int numberOfRelations ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( numberOfRelations ( N ) ) ; } }
import java . util . * ; class GFG { static void countNumbers ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( __gcd ( i , N ) != 1 ) count ++ ; } System . out . print ( count ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
import java . util . * ; class GFG { static int phi ( int N ) { int result = N ; for ( int p = 2 ; p * p <= N ; ++ p ) { if ( N % p == 0 ) { while ( N % p == 0 ) N /= p ; result -= result / p ; } } if ( N > 1 ) result -= result / N ; return result ; } static void countNumbers ( int N ) { int count = N - phi ( N ) ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 5 ; countNumbers ( N ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int nthFibo ( int n ) { double a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; double b = ( - 1 * ( Math . pow ( 5 , 0.5 ) ) + 1 ) / 2 ; double r = Math . pow ( 5 , 0.5 ) ; double ans = ( Math . pow ( a , n ) - Math . pow ( b , n ) ) / r ; return ( int ) ans ; } static int nearFibo ( int X ) { double a = ( Math . pow ( 5 , 0.5 ) + 1 ) / 2 ; int n = ( int ) ( Math . log ( ( Math . pow ( 5 , 0.5 ) ) * X ) / Math . log ( a ) ) ; int nth = nthFibo ( n ) ; int nplus = nthFibo ( n + 1 ) ; if ( Math . abs ( X - nth ) < Math . abs ( X - nplus ) ) return nth ; else return nplus ; } static int getCost ( int arr [ ] , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int fibo = nearFibo ( arr [ i ] ) ; cost += Math . abs ( arr [ i ] - fibo ) ; } return cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 56 , 34 , 23 , 98 , 7 } ; int n = arr . length ; System . out . print ( getCost ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void sieveOfEratosthenes ( int N , int s [ ] ) { boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 2 ; i <= N ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= N ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= N ; j += 2 ) { if ( ! prime [ i * j ] ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static void findDifference ( int N ) { int [ ] s = new int [ N + 1 ] ; sieveOfEratosthenes ( N , s ) ; int total = 1 , odd = 1 , even = 0 ; int curr = s [ N ] ; int cnt = 1 ; while ( N > 1 ) { N /= s [ N ] ; if ( curr == s [ N ] ) { cnt ++ ; continue ; } if ( curr == 2 ) { total = total * ( cnt + 1 ) ; } else { total = total * ( cnt + 1 ) ; odd = odd * ( cnt + 1 ) ; } curr = s [ N ] ; cnt = 1 ; } even = total - odd ; System . out . print ( Math . abs ( even - odd ) ) ; } public static void main ( String [ ] args ) { int N = 12 ; findDifference ( N ) ; } }
class GFG { static void sumOfInfiniteAGP ( double a , double d , double r ) { double ans = a / ( 1 - r ) + ( d * r ) / ( 1 - r * r ) ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { double a = 0 , d = 1 , r = 0.5 ; sumOfInfiniteAGP ( a , d , r ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countPairs ( int arr [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 arr [ j ] % arr [ i ] == 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 2 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int countbits ( int n ) { int count = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 ) count ++ ; n = n / 2 ; } return count ; } static int BitProduct ( int arr [ ] , int N ) { int product = 1 ; for ( int i = 0 ; i < N ; i ++ ) { int bits = countbits ( arr [ i ] ) ; product *= bits ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 1 , 5 } ; int N = arr . length ; System . out . print ( BitProduct ( arr , N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int getMid ( int s , int e ) { return s + ( e - s ) / 2 ; } static int getSumUtil ( int st [ ] , int ss , int se , int qs , int qe , int si ) { if ( qs <= ss && qe >= se ) return st [ si ] ; if ( se < qs ss > qe ) return 0 ; int mid = getMid ( ss , se ) ; return getSumUtil ( st , ss , mid , qs , qe , 2 * si + 1 ) + getSumUtil ( st , mid + 1 , se , qs , qe , 2 * si + 2 ) ; } static int getSum ( int st [ ] , int n , int qs , int qe ) { if ( qs < 0 qe > n - 1 qs > qe ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } return getSumUtil ( st , 0 , n - 1 , qs , qe , 0 ) ; } static int constructSTUtil ( int arr [ ] , int ss , int se , int st [ ] , int si ) { if ( ss == se ) { st [ si ] = arr [ ss ] ; return arr [ ss ] ; } int mid = getMid ( ss , se ) ; st [ si ] = constructSTUtil ( arr , ss , mid , st , si * 2 + 1 ) + constructSTUtil ( arr , mid + 1 , se , st , si * 2 + 2 ) ; return st [ si ] ; } static int [ ] constructST ( int arr [ ] , int n ) { int x = ( int ) ( Math . ceil ( Math . log ( n ) / Math . log ( 2 ) ) ) ; int max_size = 2 * ( int ) Math . pow ( 2 , x ) - 1 ; int st [ ] = new int [ max_size ] ; constructSTUtil ( arr , 0 , n - 1 , st , 0 ) ; return st ; } static void OddDivisorsSum ( int n , int q , int arr [ ] , int Query [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { int sq = ( int ) Math . sqrt ( arr [ i ] ) ; if ( sq * sq != arr [ i ] ) arr [ i ] = 0 ; } int st [ ] = constructST ( arr , n ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = Query [ i ] [ 0 ] ; int r = Query [ i ] [ 1 ] ; System . out . print ( getSum ( st , n , l , r ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 6 , 9 } ; int N = arr . length ; int Q = 3 ; int Query [ ] [ ] = { { 0 , 2 } , { 1 , 3 } , { 1 , 4 } } ; OddDivisorsSum ( N , Q , arr , Query ) ; } }
import java . io . * ; class GFG { static void OddDivisorsSum ( int n , int q , int [ ] a , int [ ] [ ] Query ) { int [ ] DP = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int x = ( int ) ( Math . sqrt ( a [ i ] ) ) ; if ( x * x == a [ i ] ) DP [ i ] = a [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { DP [ i ] = DP [ i - 1 ] + DP [ i ] ; } for ( int i = 0 ; i < q ; i ++ ) { int l = Query [ i ] [ 0 ] ; int r = Query [ i ] [ 1 ] ; if ( l == 0 ) { System . out . print ( DP [ r ] + " ▁ " ) ; } else { System . out . print ( DP [ r ] - DP [ l - 1 ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 5 , 6 , 9 } ; int N = arr . length ; int Q = 3 ; int [ ] [ ] Query = { { 0 , 2 } , { 1 , 3 } , { 1 , 4 } } ; OddDivisorsSum ( N , Q , arr , Query ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findSubset ( int [ ] arr ) { int N = arr . length ; Map < Integer , Integer > map = new HashMap < > ( ) ; int totSum = 0 ; int s = 0 ; int flag = 0 ; ArrayList < Integer > ans = new ArrayList < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { totSum += arr [ i ] ; map . put ( arr [ i ] , map . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } Arrays . sort ( arr ) ; int i = N - 1 ; while ( i >= 0 ) { int frq = map . get ( arr [ i ] ) ; if ( ( frq + ans . size ( ) ) < ( N - ( frq + ans . size ( ) ) ) ) { for ( int k = 0 ; k < frq ; k ++ ) { ans . add ( arr [ i ] ) ; totSum -= arr [ i ] ; s += arr [ i ] ; i -- ; } } else { i -= frq ; } if ( s > totSum ) { flag = 1 ; break ; } } if ( flag == 1 ) { for ( i = ans . size ( ) - 1 ; i >= 0 ; i -- ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 3 , 2 , 4 , 1 , 2 } ; findSubset ( arr ) ; } }
import java . util . * ; class GFG { static int Rotate ( int n , int f ) { int temp = n ; int maxi = n ; int mini = n ; for ( int idx = 0 ; idx < 7 ; idx ++ ) { if ( temp % 2 == 1 ) { temp >>= 1 ; temp += Math . pow ( 2 , 7 ) ; } else temp >>= 1 ; mini = Math . min ( mini , temp ) ; maxi = Math . max ( maxi , temp ) ; } if ( f == 1 ) return ( maxi ) ; else return ( mini ) ; } static int calcMinDiff ( int arr [ ] , int n ) { int caseOne = 0 ; int sumOfodd = 0 ; int sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 0 ) ; else sumOfeven += Rotate ( arr [ i ] , 1 ) ; } caseOne = Math . abs ( sumOfodd - sumOfeven ) ; int caseTwo = 0 ; sumOfodd = 0 ; sumOfeven = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) sumOfodd += Rotate ( arr [ i ] , 1 ) ; else sumOfeven += Rotate ( arr [ i ] , 0 ) ; } caseTwo = Math . abs ( sumOfodd - sumOfeven ) ; return Math . max ( caseOne , caseTwo ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 123 , 86 , 234 , 189 } ; int n = arr . length ; System . out . print ( ( calcMinDiff ( arr , n ) ) ) ; } }
import java . io . * ; class GFG { static void minimumDiff ( int [ ] arr , int N ) { int res = arr [ 0 ] , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; int min_diff = sum ; for ( int i = 0 ; i < N ; i ++ ) { if ( Math . abs ( sum - ( arr [ i ] * N ) ) < min_diff ) { min_diff = Math . abs ( sum - ( arr [ i ] * N ) ) ; res = arr [ i ] ; } } System . out . println ( res ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; minimumDiff ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int sum ( int arr [ ] , int N , int K ) { int sum = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { v . add ( arr [ i ] ) ; } boolean flag = false ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( ! flag && v . get ( i ) % K == 0 ) v . add ( v . get ( i ) / K ) ; else { flag = true ; } sum = sum + v . get ( i % N ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 8 , 2 } ; int K = 2 ; int N = arr . length ; System . out . println ( sum ( arr , N , K ) ) ; } }
public class GFG { public static int primeDivisors ( int [ ] arr , int N ) { int K = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) { K = Math . max ( K , arr [ i ] ) ; } int [ ] prime = new int [ K + 1 ] ; prime [ 0 ] = 1 ; prime [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < K + 1 ; j += i ) { prime [ j ] = 1 ; } } } int [ ] factor = new int [ K + 1 ] ; factor [ 0 ] = 0 ; factor [ 1 ] = 1 ; for ( int i = 2 ; i < K + 1 ; i ++ ) { factor [ i ] += 1 ; for ( int j = i ; j < K + 1 ; j += i ) { factor [ j ] += 1 ; } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( prime [ factor [ arr [ i ] ] ] == 0 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 13 , 17 , 25 } ; int N = arr . length ; System . out . println ( primeDivisors ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void findMedian ( int Mean , int Mode ) { double Median = ( 2 * Mean + Mode ) / 3.0 ; System . out . print ( ( int ) Median ) ; } public static void main ( String [ ] args ) { int mode = 6 , mean = 3 ; findMedian ( mean , mode ) ; } }
import java . io . * ; import java . lang . * ; class GFG { static final int MOD = 1000000007 ; static void sumOfBinaryNumbers ( int n ) { int ans = 0 ; int one = 1 ; while ( true ) { if ( n <= 1 ) { ans = ( ans + n ) % MOD ; break ; } int x = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; int cur = 0 ; int add = ( int ) ( Math . pow ( 2 , ( x - 1 ) ) ) ; for ( int i = 1 ; i <= x ; i ++ ) { cur = ( cur + add ) % MOD ; add = ( add * 10 % MOD ) ; } ans = ( ans + cur ) % MOD ; int rem = n - ( int ) ( Math . pow ( 2 , x ) ) + 1 ; int p = ( int ) Math . pow ( 10 , x ) ; p = ( p * ( rem % MOD ) ) % MOD ; ans = ( ans + p ) % MOD ; n = rem - 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 3 ; sumOfBinaryNumbers ( N ) ; } }
import java . util . HashMap ; import java . util . Map . Entry ; class GFG { static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; return findGCD ( b , a % b ) ; } static int findLCM ( int a , int b ) { return ( a * b ) / findGCD ( a , b ) ; } static void uniqueElementsLCM ( int arr [ ] , int N ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { freq . put ( arr [ i ] , freq . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int lcm = 1 ; for ( Entry < Integer , Integer > i : freq . entrySet ( ) ) { if ( i . getValue ( ) == 1 ) { lcm = findLCM ( lcm , i . getKey ( ) ) ; } } if ( lcm == 1 ) lcm = - 1 ; System . out . print ( lcm ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 3 , 4 } ; int N = arr . length ; uniqueElementsLCM ( arr , N ) ; } }
import java . io . * ; class GFG { final static int mod = 1000000007 ; public static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } public static int asymmetricRelation ( int N ) { return power ( 3 , ( N * N - N ) / 2 ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( asymmetricRelation ( N ) ) ; } }
class GFG { static void crossProduct ( int vect_A [ ] , int vect_B [ ] , int cross_P [ ] ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } static void checkCollinearity ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 ) { int A [ ] = { x1 , y1 , z1 } ; int B [ ] = { x2 , y2 , z2 } ; int cross_P [ ] = new int [ 3 ] ; crossProduct ( A , B , cross_P ) ; if ( cross_P [ 0 ] == 0 && cross_P [ 1 ] == 0 && cross_P [ 2 ] == 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int x1 = 4 , y1 = 8 , z1 = 12 ; int x2 = 8 , y2 = 16 , z2 = 24 ; checkCollinearity ( x1 , y1 , z1 , x2 , y2 , z2 ) ; } }
class GFG { private static double vectorMagnitude ( int x , int y , int z ) { int sum = x * x + y * y + z * z ; return Math . sqrt ( sum ) ; } public static void main ( String [ ] args ) { int x = 1 ; int y = 2 ; int z = 3 ; System . out . print ( vectorMagnitude ( x , y , z ) ) ; } }
class GFG { static double kineticEnergy ( double M , double V ) { double KineticEnergy ; KineticEnergy = 0.5 * M * V * V ; return KineticEnergy ; } static double potentialEnergy ( double M , double H ) { double PotentialEnergy ; PotentialEnergy = M * 9.8 * H ; return PotentialEnergy ; } public static void main ( String [ ] args ) { double M = 5.5 , H = 23.5 , V = 10.5 ; System . out . println ( " Kinetic ▁ Energy ▁ = ▁ " + kineticEnergy ( M , V ) ) ; System . out . println ( " Potential ▁ Energy ▁ = ▁ " + potentialEnergy ( M , H ) ) ; } }
class GFG { static void nearestFibonacci ( int num ) { if ( num == 0 ) { System . out . print ( 0 ) ; return ; } int first = 0 , second = 1 ; int third = first + second ; while ( third <= num ) { first = second ; second = third ; third = first + second ; } int ans = ( Math . abs ( third - num ) >= Math . abs ( second - num ) ) ? second : third ; System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int N = 17 ; nearestFibonacci ( N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void distribute ( int N , int K , int M , int arr [ ] ) { int distribution [ ] = new int [ N ] ; int ptr = K - 1 ; int rem = M ; while ( rem > 0 ) { if ( rem >= arr [ ptr ] ) { distribution [ ptr ] += arr [ ptr ] ; rem -= arr [ ptr ] ; } else { distribution [ ptr ] += rem ; rem = 0 ; } ptr = ( ptr + 1 ) % N ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( distribution [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 1 , 4 } ; int M = 11 , S = 2 ; int N = arr . length ; distribute ( N , S , M , arr ) ; } }
import java . io . * ; class GFG { static void waysToRemove ( int n , int m ) { int ans = 0 ; if ( m == 1 ) { System . out . println ( n ) ; return ; } for ( int d = 0 ; d >= 0 ; d ++ ) { int len = m + ( m - 1 ) * d ; if ( len > n ) break ; ans += ( n - len ) + 1 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , M = 3 ; waysToRemove ( N , M ) ; } }
import java . util . LinkedList ; import java . util . Queue ; class GFG { static void countStairs ( int n , int x , int a , int b ) { int [ ] vis = new int [ n + 1 ] ; int [ ] moves = { + a , - a , + b , - b } ; Queue < Integer > q = new LinkedList < Integer > ( ) ; q . add ( x ) ; vis [ x ] = 1 ; while ( ! q . isEmpty ( ) ) { int currentStair = q . peek ( ) ; q . remove ( ) ; for ( int j = 0 ; j < 4 ; j ++ ) { int newStair = currentStair + moves [ j ] ; if ( newStair > 0 && newStair <= n && vis [ newStair ] == 0 ) { q . add ( newStair ) ; vis [ newStair ] = 1 ; } } } int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( vis [ i ] == 1 ) cnt ++ ; System . out . print ( cnt ) ; } public static void main ( String args [ ] ) { int N = 10 , S = 2 , A = 5 , B = 7 ; countStairs ( N , S , A , B ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void smallerNumbers ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { count ++ ; } } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 1 , 1 , 2 } ; int N = arr . length ; smallerNumbers ( arr , N ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) { return a ; } return gcd ( b , a % b ) ; } static void minimumOperations ( int X , int Y ) { int GCD = gcd ( X , Y ) ; X = X / GCD ; Y = Y / GCD ; int count = 0 ; while ( X != Y ) { if ( Y > X ) { int t = X ; X = Y ; Y = t ; } if ( X % 2 == 0 ) { X = X / 2 ; } else if ( X % 3 == 0 ) { X = X / 3 ; } else if ( X % 5 == 0 ) { X = X / 5 ; } else { System . out . print ( " - 1" ) ; return ; } count += 1 ; } System . out . println ( count ) ; } static public void main ( String args [ ] ) { int X = 15 , Y = 20 ; minimumOperations ( X , Y ) ; } }
class GFG { static int minOperations ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += a [ i ] ; } ans -= n ; for ( int x = 1 ; ; x ++ ) { int curPow = 1 , curCost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curCost += Math . abs ( a [ i ] - curPow ) ; curPow *= x ; } if ( curPow / x > ans + a [ n - 1 ] ) break ; ans = Math . min ( ans , curCost ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 } ; int N = arr . length ; System . out . print ( minOperations ( arr , N ) ) ; } }
public class GFG { static int floorMod ( int a , int n ) { int q = ( int ) Math . floor ( ( double ) a / n ) ; return a - n * q ; } public static void main ( String [ ] args ) { int a , b ; a = 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; a = - 9 ; b = - 4 ; System . out . println ( a + " ▁ % ▁ " + b + " ▁ = ▁ " + floorMod ( a , b ) ) ; } }
import java . io . * ; class GFG { static void printArray ( int [ ] arr , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void nearestPowerDiff ( int [ ] arr , int N , int a , int b ) { for ( int i = 0 ; i < N ; i ++ ) { int log_a = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( a ) ) ; int A = ( int ) ( Math . pow ( a , log_a ) ) ; int B = ( int ) ( Math . pow ( a , log_a + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_a = A ; else log_a = B ; int log_b = ( int ) ( Math . log ( arr [ i ] ) / Math . log ( b ) ) ; A = ( int ) ( Math . pow ( b , log_b ) ) ; B = ( int ) ( Math . pow ( b , log_b + 1 ) ) ; if ( ( arr [ i ] - A ) < ( B - arr [ i ] ) ) log_b = A ; else log_b = B ; arr [ i ] = Math . abs ( log_a - log_b ) ; } printArray ( arr , N ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 12 , 25 } ; int A = 2 , B = 3 ; int N = arr . length ; nearestPowerDiff ( arr , N , A , B ) ; } }
import java . util . * ; class GFG { static int bitOr ( int arr [ ] , int N ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { length ++ ; } else { if ( length != 0 ) { count += ( ( length ) * ( length + 1 ) ) / 2 ; } length = 0 ; } } count += ( ( length ) * ( length + 1 ) ) / 2 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 2 , 6 } ; int N = arr . length ; System . out . print ( bitOr ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int maximumAND ( int arr [ ] , int n , int m ) { int tot = 1 << n ; int mx = 0 ; for ( int bm = 0 ; bm < tot ; bm ++ ) { int andans = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( ( bm >> i ) & 1 ) != 0 ) { if ( count == 0 ) { andans = arr [ i ] ; } else { andans = andans & arr [ i ] ; } count ++ ; } } if ( count == ( m + 1 ) ) { mx = Math . max ( mx , andans ) ; } } return mx ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; int M = 2 ; System . out . println ( maximumAND ( arr , N , M ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findTotalSum ( String ops [ ] ) { if ( ops . length == 0 ) { System . out . println ( 0 ) ; return ; } Stack < Integer > pts = new Stack < > ( ) ; int ans = 0 ; for ( int i = 0 ; i < ops . length ; i ++ ) { if ( ops [ i ] == " C " ) { ans -= pts . pop ( ) ; } else if ( ops [ i ] == " D " ) { pts . push ( pts . peek ( ) * 2 ) ; ans += pts . peek ( ) ; } else if ( ops [ i ] == " + " ) { int a = pts . pop ( ) ; int b = pts . peek ( ) ; pts . push ( a ) ; ans += ( a + b ) ; pts . push ( a + b ) ; } else { int n = Integer . parseInt ( ops [ i ] ) ; ans += n ; pts . push ( n ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String arr [ ] = { "5" , " - 2" , " C " , " D " , " + " } ; findTotalSum ( arr ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void findXOR ( int mat [ ] [ ] [ ] , int N ) { int XOR = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) { if ( ( i == j && j == k ) ) { XOR ^= mat [ i ] [ j ] [ k ] ; XOR ^= mat [ i ] [ j ] [ N - k - 1 ] ; } } } } System . out . println ( XOR ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] [ ] = { { { 1 , 2 } , { 3 , 4 } } , { { 5 , 6 } , { 7 , 8 } } } ; int N = mat . length ; findXOR ( mat , N ) ; } }
import java . util . * ; class GFG { static int MAX = 300004 ; static ArrayList < ArrayList < Integer > > graph ; static long mod = ( long ) 1e9 + 7 ; static int ans = 0 ; static int countSubtreesUtil ( int cur , int par ) { int res = 1 ; for ( int i = 0 ; i < graph . get ( cur ) . size ( ) ; i ++ ) { int v = graph . get ( cur ) . get ( i ) ; if ( v == par ) continue ; res = ( int ) ( ( res * ( countSubtreesUtil ( v , cur ) + 1 ) ) % mod ) ; } ans = ( int ) ( ( ans + res ) % mod ) ; return res ; } static void countSubtrees ( int N , int [ ] [ ] adj ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int a = adj [ i ] [ 0 ] ; int b = adj [ i ] [ 1 ] ; graph . get ( a ) . add ( b ) ; graph . get ( b ) . add ( a ) ; } countSubtreesUtil ( 1 , 1 ) ; System . out . println ( ans + 1 ) ; } public static void main ( String [ ] args ) { int N = 3 ; int [ ] [ ] adj = { { 0 , 1 } , { 1 , 2 } } ; graph = new ArrayList < > ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) graph . add ( new ArrayList < > ( ) ) ; countSubtrees ( N , adj ) ; } }
import java . util . * ; class GFG { static void constructArray ( int N ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i * i <= N ; i ++ ) { if ( N % i == 0 ) { ans . add ( i ) ; if ( N != ( i * i ) ) { ans . add ( N / i ) ; } } } for ( int it : ans ) { System . out . print ( it + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 12 ; constructArray ( N ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maximumRowValue ( int n , int m ) { return gcd ( n , m ) ; } public static void main ( String args [ ] ) { int N = 4 ; int M = 2 ; System . out . print ( maximumRowValue ( N , M ) ) ; } }
import java . util . * ; class GFG { static int MOD = 998244353 ; static void findConvolution ( int [ ] a , int [ ] b ) { int n = a . length , m = b . length ; int [ ] c = new int [ ( n + m - 1 ) ] ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { c [ i + j ] += ( a [ i ] * b [ j ] ) % MOD ; } } for ( int k = 0 ; k < c . length ; ++ k ) { c [ k ] %= MOD ; System . out . print ( c [ k ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] B = { 5 , 6 , 7 , 8 , 9 } ; findConvolution ( A , B ) ; } }
class GFG { static int count1s ( int start_i , int start_j , int end_i , int end_j , int [ ] [ ] mat ) { int count = 0 ; for ( int x = start_i ; x < end_i ; x ++ ) { for ( int y = start_j ; y < end_j ; y ++ ) { if ( mat [ x ] [ y ] == 1 ) count ++ ; } } return count ; } static int findMinimumCount ( int N , int M , int A , int B , int [ ] [ ] mat ) { int minimum = ( int ) 1e9 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( i + A <= N && j + B <= M ) { int count = count1s ( i , j , i + A , j + B , mat ) ; minimum = Math . min ( count , minimum ) ; } if ( i + B <= N && j + A <= M ) { int count = count1s ( i , j , i + B , j + A , mat ) ; minimum = Math . min ( count , minimum ) ; } } } return minimum ; } public static void main ( String [ ] args ) { int A = 2 , B = 2 ; int N = 3 , M = 4 ; int [ ] [ ] mat = { { 1 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } , { 1 , 0 , 1 , 0 } } ; System . out . println ( findMinimumCount ( N , M , A , B , mat ) ) ; } }
import java . io . * ; class GFG { static boolean checkEvenPower ( int n ) { int x = 0 ; while ( x < n ) { int value = ( int ) Math . pow ( 2 , x ) ; if ( value == n ) { if ( x % 2 == 0 ) return true ; else return false ; } x ++ ; } return false ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( ( checkEvenPower ( N ) ? " Yes " : " No " ) ) ; } }
import java . util . * ; class GFG { static String checkEvenPower ( int n ) { int low = 0 , high = n ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; int value = ( int ) Math . pow ( 2 , mid ) ; if ( value == n ) { if ( mid % 2 == 1 ) return " No " ; else return " Yes " ; } else if ( value < n ) low = mid + 1 ; else high = mid - 1 ; } return " No " ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( checkEvenPower ( N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static long multiplyByMersenne ( long N , long M ) { long x = ( int ) ( Math . log ( M + 1 ) / Math . log ( 2 ) ) ; return ( ( N << x ) - N ) ; } public static void main ( String [ ] args ) { long N = 4 ; long M = 15 ; System . out . print ( multiplyByMersenne ( N , M ) ) ; } }
import java . util . * ; class GFG { static void findSum ( int N , int K ) { int ans = 0 ; int i = 1 ; while ( i <= N ) { ans += N / i ; i = i * K ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 10 , K = 2 ; findSum ( N , K ) ; } }
import java . util . * ; class GFG { static void minimumInsertions ( int arr [ ] , int N , int K ) { boolean possible = true ; int res = 0 ; int last = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] >= K ) { possible = false ; break ; } if ( last + arr [ i ] > K ) res ++ ; last = arr [ i ] ; } if ( possible ) { System . out . print ( res ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int K = 6 ; int N = arr . length ; minimumInsertions ( arr , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int mod = 1000000007 ; static int power ( int x , int y ) { int res = 1 ; x = x % mod ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % mod ; y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int irreflexiveRelation ( int N ) { return power ( 2 , N * N - N ) ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( irreflexiveRelation ( N ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countAPs ( long N ) { long count = 0 ; for ( long i = 1 ; i * i <= 2 * N ; i ++ ) { long res = 2 * N ; if ( res % i == 0 ) { long op = res / i - i + 1 ; if ( op % 2 == 0 ) { count ++ ; } if ( i * i != res && ( i - res / i + 1 ) % 2 == 0 ) { count ++ ; } } } System . out . println ( count - 1 ) ; } public static void main ( String [ ] args ) { long N = 963761198400L ; countAPs ( N ) ; } }
import java . util . * ; class GFG { static int perfectSquare ( int num ) { int sr = ( int ) ( Math . sqrt ( num ) ) ; int a = sr * sr ; int b = ( sr + 1 ) * ( sr + 1 ) ; if ( ( num - a ) < ( b - num ) ) { return a ; } else { return b ; } } static int powerOfTwo ( int num ) { int lg = ( int ) ( Math . log ( num ) / Math . log ( 2 ) ) ; int p = ( int ) ( Math . pow ( 2 , lg ) ) ; return p ; } static void uniqueElement ( int arr [ ] , int N ) { boolean ans = true ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > el : freq . entrySet ( ) ) { if ( el . getValue ( ) == 1 ) { ans = false ; int ps = perfectSquare ( el . getKey ( ) ) ; System . out . print ( powerOfTwo ( ps ) + " ▁ " ) ; } } if ( ans ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 11 , 4 , 3 , 4 } ; int N = arr . length ; uniqueElement ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int [ ] sieve = new int [ 105 ] ; static void buildSieve ( ) { for ( int i = 2 ; i < 100 ; i ++ ) sieve [ i ] = 1 ; for ( int i = 2 ; i < 100 ; i ++ ) { if ( sieve [ i ] == 1 ) { for ( int j = i * i ; j < 100 ; j += i ) sieve [ j ] = 0 ; } } } static boolean isAnyPrime ( int first , int last ) { int num1 = first * 10 + last ; int num2 = last * 10 + first ; if ( sieve [ num1 ] == 1 sieve [ num2 ] == 1 ) return true ; else return false ; } static void performQueries ( int [ ] q ) { for ( int i = 0 ; i < q . length ; i ++ ) { int A = q [ i ] ; int last = A % 10 ; int first ; while ( A >= 10 ) A = A / 10 ; first = A ; if ( isAnyPrime ( first , last ) ) System . out . println ( "TrueNEW_LINE"); else System . out . print ( "FalseNEW_LINE"); } } public static void main ( String [ ] args ) { int [ ] q = { 30 , 66 } ; buildSieve ( ) ; performQueries ( q ) ; } }
import java . io . * ; class GFG { public static String oddDivisor ( int N ) { int X = N ; while ( N % 2 == 0 ) { N /= 2 ; } for ( int i = 3 ; i * i <= X ; i += 2 ) { if ( N % i == 0 ) { return " Yes " ; } } if ( N != X ) { return " Yes " ; } return " No " ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . println ( oddDivisor ( N ) ) ; } }
import java . io . * ; import java . util . * ; public class Pair { private final int key ; private final int value ; public Pair ( int aKey , int aValue ) { key = aKey ; value = aValue ; } public int key ( ) { return key ; } public int value ( ) { return value ; } } class GFG { static int [ ] arr = new int [ 10005 ] ; static int isPalindrome ( int n ) { int temp = n ; int res = 0 ; while ( temp != 0 ) { int rem = temp % 10 ; res = res * 10 + rem ; temp /= 10 ; } if ( res == n ) { return 1 ; } else return 0 ; } static void precompute ( ) { for ( int i = 1 ; i <= 10000 ; i ++ ) { if ( isPalindrome ( i * i * i ) != 0 ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 10000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static void main ( String [ ] args ) { ArrayList < Pair > Q = new ArrayList < Pair > ( ) ; Pair pair = new Pair ( 2 , 7 ) ; Q . add ( pair ) ; Pair pair2 = new Pair ( 10 , 25 ) ; Q . add ( pair2 ) ; precompute ( ) ; for ( int i = 0 ; i < Q . size ( ) ; i ++ ) { System . out . println ( arr [ Q . get ( i ) . value ( ) ] - arr [ Q . get ( i ) . key ( ) - 1 ] ) ; } } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static int arr [ ] = new int [ 100005 ] ; static boolean isDivisible ( int x ) { int sum = 0 ; int temp = x ; while ( x != 0 ) { sum += x % 10 ; x /= 10 ; } if ( temp % sum != 0 ) return false ; else return true ; } static void precompute ( ) { for ( int i = 1 ; i <= 100000 ; i ++ ) { if ( isDivisible ( i ) ) arr [ i ] = 1 ; else arr [ i ] = 0 ; } for ( int i = 1 ; i <= 100000 ; i ++ ) { arr [ i ] = arr [ i ] + arr [ i - 1 ] ; } } public static void main ( String [ ] args ) { int Q [ ] [ ] = { { 5 , 9 } , { 5 , 20 } } ; precompute ( ) ; for ( int q [ ] : Q ) { System . out . println ( arr [ q [ 1 ] ] - arr [ q [ 0 ] - 1 ] ) ; } } }
import java . util . * ; public class GFG { static int primes [ ] = new int [ 1000006 ] ; static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static void preprocess ( ) { for ( int i = 1 ; i <= 1000000 ; i ++ ) primes [ i ] = i ; for ( int i = 2 ; i * i <= 1000000 ; i ++ ) { if ( primes [ i ] == i ) { for ( int j = 2 * i ; j <= 1000000 ; j += i ) { if ( primes [ j ] == j ) primes [ j ] = i ; } } } } static int Steps ( int x , int m ) { int steps = 0 ; boolean flag = false ; while ( x > 1 ) { if ( primes [ x ] > m ) { flag = true ; break ; } x /= primes [ x ] ; steps ++ ; } if ( flag ) return - 1 ; return steps ; } static int minimumSteps ( int x , int y , int m ) { preprocess ( ) ; int g = gcd ( x , y ) ; x = x / g ; y = y / g ; int x_steps = Steps ( x , m ) ; int y_steps = Steps ( y , m ) ; if ( x_steps == - 1 y_steps == - 1 ) return - 1 ; return x_steps + y_steps ; } public static void main ( String args [ ] ) { int X = 160 ; int Y = 180 ; int M = 10 ; System . out . println ( minimumSteps ( X , Y , M ) ) ; } }
import java . util . * ; class GFG { static boolean isNonDeficient ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum >= 2 * n ; } static int LongestNonDeficientSubsequence ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNonDeficient ( arr [ i ] ) ) { res += 1 ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 55 , 240 , 32 , 24 , 27 , 56 , 80 , 100 , 330 , 89 } ; int N = arr . length ; System . out . print ( LongestNonDeficientSubsequence ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= ( int ) Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == 2 ) { sum += num ; i ++ ; } } return sum ; } static void check ( int n , int k ) { int s = findSum ( k - 1 ) ; if ( s >= n ) System . out . print ( " No " ) ; else System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { int n = 100 , k = 6 ; check ( n , k ) ; } }
import java . lang . * ; class GFG { static boolean isPronic ( int n ) { int range = ( int ) Math . sqrt ( n ) ; for ( int i = 0 ; i < range + 1 ; i ++ ) { if ( i * ( i + 1 ) == n ) return true ; } return false ; } static int countSub ( int [ ] arr , int n ) { int ans = 0 ; int ispro = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPronic ( arr [ i ] ) ) ispro += 1 ; else ispro = 0 ; ans += ispro ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 12 , 3 , 4 } ; int n = arr . length ; System . out . print ( countSub ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int LOG ( int a , int b ) { return ( int ) ( Math . log ( a ) / Math . log ( b ) ) ; } static void repbyNP ( int [ ] arr , int n ) { int x = arr [ n - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = LOG ( arr [ i ] , x ) ; int temp = arr [ i ] ; if ( Math . abs ( Math . pow ( x , k ) - arr [ i ] ) < Math . abs ( Math . pow ( x , k + 1 ) - arr [ i ] ) ) arr [ i ] = ( int ) Math . pow ( x , k ) ; else arr [ i ] = ( int ) Math . pow ( x , k + 1 ) ; x = temp ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 3 , 11 } ; int n = arr . length ; repbyNP ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . io . * ; import java . util . Map ; import java . util . HashMap ; class GFG { public static void countEqualElementPairs ( int arr [ ] , int N ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Integer k = map . get ( arr [ i ] ) ; map . put ( arr [ i ] , ( k == null ) ? 1 : k + 1 ) ; } int total = 0 ; for ( Map . Entry < Integer , Integer > e : map . entrySet ( ) ) { total += ( e . getValue ( ) * ( e . getValue ( ) - 1 ) ) / 2 ; } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( total - ( map . get ( arr [ i ] ) - 1 ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 1 , 2 } ; int N = 5 ; countEqualElementPairs ( arr , N ) ; } }
import java . io . * ; class GFG { public static int lastElement ( int arr [ ] , int n ) { int sum = 0 ; int multiplier = n % 2 == 0 ? - 1 : 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * multiplier ; multiplier = multiplier * ( n - 1 - i ) / ( i + 1 ) * ( - 1 ) ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 1 } ; int N = 4 ; System . out . println ( lastElement ( arr , N ) ) ; } }
public class GFG { static int countPairs ( int [ ] arr , int N ) { int odd = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; } int ans = ( N - odd + odd / 2 - 1 ) + odd / 2 ; return ans ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 6 , 1 , 3 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int [ ] arr , int N ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( freq . containsKey ( ( int ) ( Math . log ( arr [ i ] ) ) ) ) freq . put ( ( int ) ( Math . log ( arr [ i ] ) ) , ( int ) ( Math . log ( arr [ i ] ) ) + 1 ) ; else freq . put ( ( int ) ( Math . log ( arr [ i ] ) ) , 1 ) ; } int pairs = 0 ; for ( Map . Entry < Integer , Integer > item : freq . entrySet ( ) ) { pairs += item . getValue ( ) - 1 ; } return pairs ; } public static void main ( String [ ] args ) { int [ ] arr = { 12 , 9 , 15 , 7 } ; int N = arr . length ; System . out . println ( countPairs ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int countBit ( int n ) { return ( int ) ( ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ) ; } static int countSetBit ( int n ) { int ans = 0 ; while ( n > 0 ) { ans += ( n & 1 ) ; n >>= 1 ; } return ans ; } static int maximize ( int n ) { int bits = countBit ( n ) ; int setBits = countSetBit ( n ) ; int ans = 0 ; for ( int i = 0 ; i < bits ; i ++ ) { if ( i < setBits ) ans |= 1 ; if ( i != setBits - 1 ) ans <<= 1 ; } return ans ; } static int minimize ( int n ) { int setBits = countSetBit ( n ) ; int ans = 0 ; for ( int i = 0 ; i < setBits ; i ++ ) { ans |= 1 ; if ( i != setBits - 1 ) ans <<= 1 ; } return ans ; } static int maxDiff ( int [ ] arr ) { int caseOne = 0 ; int SumOfOdd = 0 ; int SumOfeven = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( i % 2 ) != 0 ) SumOfOdd += minimize ( arr [ i ] ) ; else SumOfeven += maximize ( arr [ i ] ) ; } caseOne = Math . abs ( SumOfOdd - SumOfeven ) ; int caseTwo = 0 ; SumOfOdd = 0 ; SumOfeven = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( i % 2 ) != 0 ) SumOfOdd += maximize ( arr [ i ] ) ; else SumOfeven += minimize ( arr [ i ] ) ; } caseTwo = Math . abs ( SumOfOdd - SumOfeven ) ; return Math . max ( caseOne , caseTwo ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 54 , 32 , 11 , 23 } ; System . out . println ( maxDiff ( arr ) ) ; } }
import java . util . * ; class GFG { static void minimumDifference ( int N ) { int sum1 = ( 1 << N ) , sum2 = 0 ; for ( int i = 1 ; i < N / 2 ; i ++ ) sum1 += ( 1 << i ) ; for ( int i = N / 2 ; i < N ; i ++ ) sum2 += ( 1 << i ) ; System . out . println ( sum1 - sum2 ) ; } public static void main ( String args [ ] ) { int N = 4 ; minimumDifference ( N ) ; } }
class GFG { static boolean isPerfectCube ( int x ) { int cr = ( int ) Math . cbrt ( x ) ; return ( cr * cr * cr == x ) ; } static void canBePerfectCube ( int N , int K ) { if ( isPerfectCube ( N + K ) || isPerfectCube ( N - K ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 7 ; int K = 1 ; canBePerfectCube ( N , K ) ; N = 5 ; K = 4 ; canBePerfectCube ( N , K ) ; N = 7 ; K = 2 ; canBePerfectCube ( N , K ) ; } }
class GFG { static int sqrtSearch ( int low , int high , int N ) { if ( low <= high ) { int mid = ( int ) ( low + high ) / 2 ; if ( ( mid * mid <= N ) && ( ( mid + 1 ) * ( mid + 1 ) > N ) ) { return mid ; } else if ( mid * mid < N ) { return sqrtSearch ( mid + 1 , high , N ) ; } else { return sqrtSearch ( low , mid - 1 , N ) ; } } return low ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . println ( sqrtSearch ( 0 , N , N ) ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printAllDivisors ( int arr [ ] , int N ) { int g = arr [ 0 ] ; HashSet < Integer > divisors = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { g = gcd ( arr [ i ] , g ) ; } for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { divisors . add ( i ) ; if ( g / i != i ) divisors . add ( g / i ) ; } } for ( int it : divisors ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 90 , 12 , 18 , 30 , 24 } ; int n = arr . length ; printAllDivisors ( arr , n ) ; } }
class GFG { static void mul_table ( int N , int i ) { if ( i > 10 ) return ; System . out . println ( N + " ▁ * ▁ " + i + " ▁ = ▁ " + N * i ) ; mul_table ( N , i + 1 ) ; } public static void main ( String [ ] args ) { int N = 8 ; mul_table ( N , 1 ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static int getSmallestPrimefactor ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 100 ; int a ; a = getSmallestPrimefactor ( N ) ; if ( isPrime ( N / a ) ) { System . out . print ( " - 1" ) ; } else { System . out . print ( N / a ) ; } } }
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 " ) ; } }
import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int checkPalindrome ( int num ) { String str = String . valueOf ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) { return 0 ; } l ++ ; r -- ; } return num ; } static void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + "NEW_LINE"); } static void printSumPalindromic ( int arr [ ] [ ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int Q = 2 ; int arr [ ] [ ] = { { 10 , 13 } , { 12 , 21 } } ; printSumPalindromic ( arr , Q ) ; } }
class GFG { static int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 1 ) sum += ( i * i ) ; else sum -= ( i * i ) ; } return sum ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( summation ( N ) ) ; } }
class GFG { static int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y + x ) % ( y ^ x ) == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( countPairs ( n ) ) ; } }
import java . util . * ; class GFG { static int xor_all_elements ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static Vector < Integer > findArray ( int xorr [ ] , int n ) { Vector < Integer > arr = new Vector < Integer > ( ) ; int xor_all = xor_all_elements ( n ) ; int xor_adjacent = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { xor_adjacent = xor_adjacent ^ xorr [ i ] ; } int last_element = xor_all ^ xor_adjacent ; arr . add ( last_element ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { last_element = xorr [ i ] ^ last_element ; arr . add ( last_element ) ; } return arr ; } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; int xorr [ ] = { 7 , 5 , 3 , 7 } ; int n = 5 ; arr = findArray ( xorr , n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } } }
class GFG { static int sumOfParts ( int [ ] [ ] arr , int N ) { int sum_part1 = 0 , sum_part2 = 0 , sum_part3 = 0 , sum_part4 = 0 ; int totalsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i + j < N - 1 ) { if ( i < j && i != j && i + j > 0 ) sum_part1 += arr [ i ] [ j ] ; else if ( i != j ) sum_part2 += arr [ i ] [ j ] ; } else { if ( i > j && i + j != N - 1 ) sum_part3 += arr [ i ] [ j ] ; else { if ( i + j != N - 1 && i != j ) sum_part4 += arr [ i ] [ j ] ; } } } } totalsum = sum_part1 + sum_part2 + sum_part3 + sum_part4 ; return totalsum ; } public static void main ( String [ ] args ) { int N = 4 ; int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; System . out . print ( sumOfParts ( arr , N ) ) ; } }
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 ) ; } }
class GFG { static void findNumbers ( int n ) { String ans = string ( n - 1 , ' F ' ) ; String even = ans + ' E ' ; String odd = ans + ' F ' ; System . out . print ( " Even : ▁ " + even + "NEW_LINE"); System . out . print ( " Odd : ▁ " + odd + "NEW_LINE"); } private static String string ( int n , char c ) { String str = " " ; for ( int i = 0 ; i < n ; i ++ ) str += c ; return str ; } public static void main ( String [ ] args ) { int n = 2 ; findNumbers ( n ) ; } }
class GFG { static int log_a_to_base_b ( int a , int b ) { return ( int ) ( Math . log ( a ) / Math . log ( b ) ) ; } public static void main ( String [ ] args ) { int a = 3 ; int b = 2 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; a = 256 ; b = 4 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; } }
class GFG { static int log_a_to_base_b ( int a , int b ) { int rslt = ( a > b - 1 ) ? 1 + log_a_to_base_b ( a / b , b ) : 0 ; return rslt ; } public static void main ( String [ ] args ) { int a = 3 ; int b = 2 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; a = 256 ; b = 4 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; } }
class GFG { static String findLargest ( int N ) { String largest = " " ; for ( int i = 0 ; i < N ; i ++ ) largest += ' F ' ; return largest ; } static String findSmallest ( int N ) { String smallest = "1" ; for ( int i = 0 ; i < N - 1 ; i ++ ) smallest += '0' ; return smallest ; } static void print ( int largest ) { System . out . println ( " Largest : ▁ " + findLargest ( largest ) ) ; System . out . println ( " Smallest : ▁ " + findSmallest ( largest ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; print ( N ) ; } }
import java . io . * ; class GFG { static int prevPowerofK ( int n , int k ) { int p = ( int ) ( Math . log ( n ) / Math . log ( k ) ) ; return ( int ) Math . pow ( k , p ) ; } static int nextPowerOfK ( int n , int k ) { return prevPowerofK ( n , k ) * k ; } static void printResult ( int n , int k ) { System . out . println ( prevPowerofK ( n , k ) + " ▁ " + nextPowerOfK ( n , k ) ) ; } public static void main ( String args [ ] ) { int n = 25 , k = 3 ; printResult ( n , k ) ; } }
class GFG { static void canMake ( int n , int ar [ ] ) { int sum = 0 , maxx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; maxx = Math . max ( maxx , ar [ i ] ) ; } if ( n == 1 sum % 2 == 1 sum - maxx < maxx ) { System . out . print ( "NoNEW_LINE"); } else { System . out . print ( "YesNEW_LINE"); } } public static void main ( String [ ] args ) { int n = 6 ; int arr [ ] = { 1 , 1 , 2 , 3 , 6 , 11 } ; canMake ( n , arr ) ; } }
import java . util . * ; class GFG { static boolean isPalindrome ( int n ) { String str = String . valueOf ( n ) ; int s = 0 , e = str . length ( ) - 1 ; while ( s < e ) { if ( str . charAt ( s ) != str . charAt ( e ) ) { return false ; } s ++ ; e -- ; } return true ; } static void palindromicDivisors ( int n ) { Vector < Integer > PalindromDivisors = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } } else { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } if ( isPalindrome ( n / i ) ) { PalindromDivisors . add ( n / i ) ; } } } } Collections . sort ( PalindromDivisors ) ; for ( int i = 0 ; i < PalindromDivisors . size ( ) ; i ++ ) { System . out . print ( PalindromDivisors . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 66 ; palindromicDivisors ( n ) ; } }
class GFG { static String smallestPoss ( String s , int n ) { String ans = " " ; int arr [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + String . valueOf ( i ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 ; String K = "325343273113434" ; System . out . print ( smallestPoss ( K , N ) ) ; } }
class GFG { static int [ ] pref = new int [ 100010 ] ; static int isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ? x : 0 ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectSquare ( i ) ; } } static void printSum ( int L , int R ) { int sum = pref [ R ] - pref [ L - 1 ] ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 4 ; int arr [ ] [ ] = { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }
class GFG { static void factors ( int n , int i ) { if ( i <= n ) { if ( n % i == 0 ) { System . out . print ( i + " ▁ " ) ; } factors ( n , i + 1 ) ; } } public static void main ( String args [ ] ) { int N = 16 ; factors ( N , 1 ) ; } }
class GFG { public static int solve ( int n ) { int ans = ( n * n / 4 ) ; return ans ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( solve ( n ) ) ; } }
class GFG { public static void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( ( i * j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int N = 5 ; printMultiples ( N ) ; } }
import java . util . * ; class GFG { static void nature_of_roots ( int a , int b , int c ) { if ( a == 0 ) { System . out . print ( " Not ▁ a ▁ Quadratic ▁ Equation " + "NEW_LINE"); return ; } int D = b * b - 4 * a * c ; if ( D > 0 ) { System . out . print ( " Real ▁ Roots " + "NEW_LINE"); } else if ( D == 0 ) { System . out . print ( " Equal ▁ Roots " + "NEW_LINE"); } else { System . out . print ( " Imaginary ▁ Roots " + "NEW_LINE"); } } static void checkForAllTestCase ( ) { System . out . print ( " Testcase " + " TABSYMBOL a TABSYMBOL b TABSYMBOL c TABSYMBOL Actual ▁ Output " + "NEW_LINE"); System . out . println ( ) ; int a , b , c ; a = b = c = 0 ; int testcase = 1 ; while ( testcase <= 13 ) { if ( testcase == 1 ) { a = 0 ; b = 50 ; c = 50 ; } else if ( testcase == 2 ) { a = 1 ; b = 50 ; c = 50 ; } else if ( testcase == 3 ) { a = 50 ; b = 50 ; c = 50 ; } else if ( testcase == 4 ) { a = 99 ; b = 50 ; c = 50 ; } else if ( testcase == 5 ) { a = 100 ; b = 50 ; c = 50 ; } else if ( testcase == 6 ) { a = 50 ; b = 0 ; c = 50 ; } else if ( testcase == 7 ) { a = 50 ; b = 1 ; c = 50 ; } else if ( testcase == 8 ) { a = 50 ; b = 99 ; c = 50 ; } else if ( testcase == 9 ) { a = 50 ; b = 100 ; c = 50 ; } else if ( testcase == 10 ) { a = 50 ; b = 50 ; c = 0 ; } else if ( testcase == 11 ) { a = 50 ; b = 50 ; c = 1 ; } else if ( testcase == 12 ) { a = 50 ; b = 50 ; c = 99 ; } else if ( testcase == 13 ) { a = 50 ; b = 50 ; c = 100 ; } System . out . print ( " TABSYMBOL " + testcase + " TABSYMBOL " + a + " TABSYMBOL " + b + " TABSYMBOL " + c + " TABSYMBOL " ) ; nature_of_roots ( a , b , c ) ; System . out . println ( ) ; testcase ++ ; } } public static void main ( String [ ] args ) { checkForAllTestCase ( ) ; } }
class GFG { static int isPossible ( int N , int S , int C , int H , int L , int T ) { int total_time_required = S * C * H ; int available_time_after_vacation = ( N - L ) * T ; if ( available_time_after_vacation >= total_time_required ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int N = 12 , S = 5 , C = 8 , H = 3 , L = 2 , T = 20 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); N = 1 ; S = 2 ; C = 3 ; H = 4 ; L = 5 ; T = 6 ; if ( isPossible ( N , S , C , H , L , T ) == 1 ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
class GFG { static int maximum ( int x , int y ) { return ( ( x + y + Math . abs ( x - y ) ) / 2 ) ; } static int minimum ( int x , int y ) { return ( ( x + y - Math . abs ( x - y ) ) / 2 ) ; } public static void main ( String [ ] args ) { int x = 99 , y = 18 ; System . out . println ( " Maximum : ▁ " + maximum ( x , y ) ) ; System . out . println ( " Minimum : ▁ " + minimum ( x , y ) ) ; } }
class GFG { static void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) System . out . print ( s + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) System . out . print ( s + 1 + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; } }
class GFG { static int pay ( int n ) { int cuts = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return cuts ; } public static void main ( String [ ] args ) { int n = 5 ; int cuts = pay ( n ) ; System . out . print ( cuts + "NEW_LINE"); n = 15 ; cuts = pay ( n ) ; System . out . print ( cuts ) ; } }
class GFG { static int MAX = 100000 ; static int max_prime [ ] = new int [ MAX + 1 ] ; static int min_prime [ ] = new int [ MAX + 1 ] ; static void sieve ( int n ) { for ( int i = 2 ; i <= n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j <= n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static void findSum ( int arr [ ] , int n ) { sieve ( MAX ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = min_prime [ arr [ i ] ] + max_prime [ arr [ i ] ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 15 , 20 , 25 , 30 } ; int n = arr . length ; findSum ( arr , n ) ; } }
class GFG { final static int maxin = 10001 ; static int divisors [ ] = new int [ maxin + 1 ] ; static void Calc_Max_Div ( int arr [ ] , int n ) { int vis [ ] = new int [ maxin + 1 ] ; for ( int i = 0 ; i < maxin + 1 ; i ++ ) vis [ i ] = 1 ; vis [ 0 ] = vis [ 1 ] = 0 ; for ( int i = 1 ; i <= maxin ; i ++ ) divisors [ i ] = i ; for ( int i = 4 ; i <= maxin ; i += 2 ) { vis [ i ] = 0 ; divisors [ i ] = i / 2 ; } for ( int i = 3 ; i <= maxin ; i += 2 ) { if ( divisors [ i ] != i ) { divisors [ i ] = i / divisors [ i ] ; } if ( vis [ i ] == 1 ) { for ( int j = i * i ; j < maxin ; j += i ) { vis [ j ] = 0 ; if ( divisors [ j ] == j ) divisors [ j ] = i ; } } } for ( int i = 0 ; i < n ; i ++ ) { if ( divisors [ arr [ i ] ] == arr [ i ] ) System . out . print ( " - 1 ▁ " ) ; else System . out . print ( divisors [ arr [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 7 , 8 , 9 , 10 } ; int n = arr . length ; Calc_Max_Div ( arr , n ) ; } }
class GFG { static int sumOfDigit ( int n , int b ) { int unitDigit , sum = 0 ; while ( n > 0 ) { unitDigit = n % b ; sum += unitDigit ; n = n / b ; } return sum ; } public static void main ( String [ ] args ) { int n = 50 ; int b = 2 ; System . out . print ( sumOfDigit ( n , b ) ) ; } }
import java . util . * ; class GFG { static int nthDigit ( int a , int n , int b ) { for ( int i = 1 ; i < n ; i ++ ) a = a / b ; return a % b ; } public static void main ( String [ ] args ) { int a = 100 ; int n = 3 ; int b = 4 ; System . out . print ( nthDigit ( a , n , b ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MAX = 100001 ; static ArrayList < Integer > primes ; static void initialize ( ) { boolean [ ] numbers = new boolean [ MAX + 1 ] ; int n = MAX ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( ! numbers [ i ] ) for ( int j = i * i ; j <= n ; j += i ) numbers [ j ] = true ; for ( int i = 2 ; i <= n ; i ++ ) if ( numbers [ i ] == false ) primes . add ( i ) ; } static void findNums ( int num ) { boolean ans = false ; int first = - 1 , second = - 1 , third = - 1 ; for ( int i = 0 ; i < num ; i ++ ) { first = primes . get ( i ) ; for ( int j = 0 ; j < num ; j ++ ) { second = primes . get ( j ) ; third = num - first - second ; if ( Collections . binarySearch ( primes , third ) >= 0 ) { ans = true ; break ; } } if ( ans ) break ; } if ( ans ) System . out . println ( first + " ▁ " + second + " ▁ " + third ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 101 ; primes = new ArrayList < > ( ) ; initialize ( ) ; findNums ( n ) ; } }
import java . util . * ; class GFG { static void printmaxSubseq ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( i + 1 < n && arr [ i ] < arr [ i + 1 ] ) System . out . print ( arr [ i ] + " ▁ " ) ; else System . out . print ( arr [ i ] + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 , 5 } ; int n = arr . length ; printmaxSubseq ( arr , n ) ; } }
class GFG { static int countSum ( int N , int L , int R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 4 , L = 4 , R = 6 ; System . out . print ( countSum ( N , L , R ) ) ; } }
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int solve ( int arr [ ] , int n , int k ) { int minx = INT_MAX ; int i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . min ( minx , arr [ i ] ) ; } int decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } public static void main ( String [ ] args ) { int n , k ; n = 3 ; k = 3 ; int arr [ ] = { 12 , 9 , 15 } ; System . out . println ( solve ( arr , n , k ) ) ; } }
class GFG { static void distribution ( int n , int k , int l , int r , int S , int Sk ) { int a [ ] = new int [ n ] ; int len = k , temp , rem , s ; int diff = S - Sk ; for ( int i = 0 ; i < len ; i ++ ) { temp = Sk / k ; rem = Sk % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { System . out . print ( " - 1" ) ; return ; } Sk = Sk - a [ i ] ; k = k - 1 ; } if ( Sk > 0 ) { System . out . print ( " - 1" ) ; return ; } if ( len != 0 ) { k = n - len ; for ( int i = len ; i < n ; i ++ ) { temp = diff / k ; rem = diff % k ; if ( temp + rem >= l && temp + rem <= r ) { a [ i ] = temp ; } else if ( temp + rem > r ) { a [ i ] = r ; } else if ( temp + rem < r ) { System . out . print ( " - 1" ) ; return ; } diff = diff - a [ i ] ; k = k - 1 ; } if ( diff != 0 ) { System . out . print ( " - 1" ) ; return ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 , k = 3 , l = 1 , r = 5 , S = 13 , Sk = 9 ; distribution ( n , k , l , r , S , Sk ) ; } }
class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int min_index ( int p [ ] , int n ) { int ans = 0 ; int mini = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] <= mini ) mini = p [ i ] ; if ( mini == p [ i ] ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int P [ ] = { 4 , 2 , 5 , 1 , 3 } ; int n = P . length ; System . out . println ( min_index ( P , n ) ) ; } }
import java . util . * ; class GFG { static int Count_subarray ( int arr [ ] , int n ) { int subarray_sum , remaining_sum , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { subarray_sum = 0 ; remaining_sum = 0 ; for ( int k = i ; k <= j ; k ++ ) { subarray_sum += arr [ k ] ; } for ( int l = 0 ; l < i ; l ++ ) { remaining_sum += arr [ l ] ; } for ( int l = j + 1 ; l < n ; l ++ ) { remaining_sum += arr [ l ] ; } if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 9 , 12 , 6 } ; int n = arr . length ; System . out . print ( Count_subarray ( arr , n ) ) ; } }
class GFG { static int Count_subarray ( int arr [ ] , int n ) { int total_sum = 0 , subarray_sum , remaining_sum , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total_sum += arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { subarray_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { subarray_sum += arr [ j ] ; remaining_sum = total_sum - subarray_sum ; if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 9 , 12 , 6 } ; int n = arr . length ; System . out . print ( Count_subarray ( arr , n ) ) ; } }
class GFG { static final int MAX = 10 ; static int countPalindromes ( int n , int m , int matrix [ ] [ ] ) { int count = n * m ; int length_of_sequence_row ; int length_of_sequence_column ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { length_of_sequence_row = Math . min ( j , m - 1 - j ) ; length_of_sequence_column = Math . min ( i , n - i - 1 ) ; for ( int k = 1 ; k <= length_of_sequence_row ; k ++ ) { if ( matrix [ i ] [ j - k ] == matrix [ i ] [ j + k ] ) { count ++ ; } else { break ; } } for ( int k = 1 ; k <= length_of_sequence_column ; k ++ ) { if ( matrix [ i - k ] [ j ] == matrix [ i + k ] [ j ] ) { count ++ ; } else { break ; } } } } return count ; } public static void main ( String [ ] args ) { int m = 3 , n = 3 ; int matrix [ ] [ ] = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 2 , 1 , 2 } } ; System . out . print ( countPalindromes ( n , m , matrix ) + "NEW_LINE"); } } `
import java . util . * ; class GFG { static int minimumNo ( int n ) { int ans = 0 ; Stack < Integer > stack1 = new Stack < Integer > ( ) ; Stack < Integer > stack2 = new Stack < Integer > ( ) ; while ( n != 0 ) { int r = n % 10 ; if ( r % 2 == 0 ) { stack1 . add ( r ) ; } else { stack2 . add ( r ) ; } n = n / 10 ; } while ( ! stack1 . isEmpty ( ) && ! stack2 . isEmpty ( ) ) { if ( stack1 . peek ( ) < stack2 . peek ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } else { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } } while ( ! stack1 . isEmpty ( ) ) { ans = ans * 10 + stack1 . peek ( ) ; stack1 . pop ( ) ; } while ( ! stack2 . isEmpty ( ) ) { ans = ans * 10 + stack2 . peek ( ) ; stack2 . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int n1 = 64432 ; System . out . print ( minimumNo ( n1 ) + "NEW_LINE"); int n2 = 3137 ; System . out . print ( minimumNo ( n2 ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int power ( int a , int b ) { int result = 1 ; while ( b > 0 ) { if ( b % 2 == 1 ) { result = result * a ; } a = a * a ; b = b / 2 ; } return result ; } static int factorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { fact = fact * i ; } return fact ; } static void findNoOfArrays ( int [ ] a , int n ) { int sum = 0 , s1 , c = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 2 * n ; i ++ ) { if ( mp . get ( a [ i ] ) == null ) mp . put ( a [ i ] , 1 ) ; else mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; sum = sum + a [ i ] ; } boolean isArrayPossible = true ; int ans = factorial ( n - 1 ) ; s1 = sum / ( n + 1 ) ; if ( mp . get ( s1 ) >= 2 ) { mp . replace ( s1 , mp . get ( s1 ) - 2 ) ; } else { isArrayPossible = false ; } if ( isArrayPossible ) { for ( Map . Entry < Integer , Integer > m : mp . entrySet ( ) ) { if ( m . getKey ( ) == s1 - m . getKey ( ) ) { if ( mp . get ( m . getKey ( ) ) % 2 != 0 ) { isArrayPossible = false ; break ; } } if ( m . getKey ( ) != s1 - m . getKey ( ) ) { if ( mp . get ( m . getKey ( ) ) != mp . get ( s1 - m . getKey ( ) ) ) { isArrayPossible = false ; break ; } } if ( m . getValue ( ) > 0 ) { if ( m . getKey ( ) != s1 - m . getKey ( ) ) { c = c + m . getValue ( ) ; ans = ans * power ( 2 , m . getValue ( ) ) ; ans = ans / factorial ( m . getValue ( ) ) ; mp . replace ( m . getKey ( ) , 0 ) ; mp . replace ( s1 - m . getKey ( ) , 0 ) ; } if ( m . getKey ( ) == s1 - m . getKey ( ) ) { c = c + m . getValue ( ) / 2 ; ans = ans / factorial ( m . getValue ( ) / 2 ) ; mp . replace ( m . getKey ( ) , 0 ) ; } } } } if ( c < n - 1 && isArrayPossible == false ) { System . out . println ( "0" ) ; } else { System . out . println ( ans ) ; } } public static void main ( String args [ ] ) { int [ ] arr1 = { 5 , 2 , 3 , 5 } ; int n1 = arr1 . length ; findNoOfArrays ( arr1 , n1 / 2 ) ; int [ ] arr2 = { - 1 , - 1 , - 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 } ; int n2 = arr2 . length ; findNoOfArrays ( arr2 , n2 / 2 ) ; } }
class GFG { final static int MAXN = 1000000 ; static int max_element ( int [ ] A , int n ) { int max = A [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( A [ i ] > max ) max = A [ i ] ; return max ; } static int maxCommonElement ( int A [ ] , int D [ ] , int N ) { int cnt [ ] = new int [ MAXN ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = A [ i ] ; j < MAXN ; j += D [ i ] ) cnt [ j ] ++ ; } int ans = 0 ; int com = 0 ; for ( int i = 0 ; i < MAXN ; i ++ ) { if ( cnt [ i ] > ans ) { ans = cnt [ i ] ; com = i ; } } return com ; } public static void main ( String args [ ] ) { int A [ ] = { 13 , 1 , 2 , 5 } , D [ ] = { 5 , 10 , 1 , 12 } ; int N = A . length ; System . out . println ( maxCommonElement ( A , D , N ) ) ; } }
import java . util . * ; class GFG { static int findTrailingZeros ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; else { int ans = 0 ; n /= 2 ; while ( n != 0 ) { ans += n / 5 ; n /= 5 ; } return ans ; } } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( findTrailingZeros ( n ) ) ; } }
import java . util . * ; class GFG { static double r1 , r2 , r3 ; static double area ( double a , double b , double c ) { double p = ( a + b + c ) / 2 ; return Math . sqrt ( p ) * Math . sqrt ( p - a ) * Math . sqrt ( p - b ) * Math . sqrt ( p - c ) ; } static double binary_search ( ) { double s = area ( r1 + r2 , r2 + r3 , r3 + r1 ) ; double l = 0 , h = s / ( r1 + r2 + r3 ) ; while ( h - l >= 1.e-7 ) { double mid = ( l + h ) / 2 ; double s1 = area ( mid + r1 , mid + r2 , r1 + r2 ) ; double s2 = area ( mid + r1 , mid + r3 , r1 + r3 ) ; double s3 = area ( mid + r2 , mid + r3 , r2 + r3 ) ; if ( s1 + s2 + s3 < s ) { l = mid ; } else { h = mid ; } } return ( l + h ) / 2 ; } public static void main ( String [ ] args ) { r1 = 1.0 ; r2 = 2.0 ; r3 = 3.0 ; System . out . printf ( " % .6f " , binary_search ( ) ) ; } }
class GFG { public static void main ( String [ ] args ) { double r1 , r2 , r3 ; r1 = 1 ; r2 = 2 ; r3 = 3 ; double r4 = ( r1 * r2 * r3 ) / ( r1 * r2 + r2 * r3 + r1 * r3 + 2.0 * Math . sqrt ( r1 * r2 * r3 * ( r1 + r2 + r3 ) ) ) ; System . out . printf ( " % .6f " , r4 ) ; } }
public class GFG { static boolean checkZeroArray ( int [ ] arr , int n ) { int sum = 0 , maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + arr [ i ] ; maximum = Math . max ( maximum , arr [ i ] ) ; } if ( sum % 2 == 0 && maximum <= sum / 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 2 , 2 } ; int n = arr . length ; if ( checkZeroArray ( arr , n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static boolean subArray ( int [ ] arr , int n , int m ) { int i ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , i + 1 ) ; } int sumcur = 0 ; int p = Integer . MAX_VALUE ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( i = 1 ; i <= m ; i ++ ) { sumcur += mp . get ( i ) ; p = Math . min ( p , mp . get ( i ) ) ; int val = p * i - i + ( i * ( i + 1 ) ) / 2 ; if ( i == m ) { if ( val == sumcur ) { return true ; } else return false ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 1 , 3 , 2 , 6 } ; int n = arr . length ; int m = 3 ; if ( subArray ( arr , n , m ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static boolean isPalindrome ( String s ) { int left = 0 , right = s . length ( ) - 1 ; while ( left <= right ) { if ( s . charAt ( left ) != s . charAt ( right ) ) { return false ; } left ++ ; right -- ; } return true ; } static long getSum ( int n ) { int start = ( int ) Math . pow ( 10 , n - 1 ) ; int end = ( int ) ( Math . pow ( 10 , n ) - 1 ) ; long sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { String s = String . valueOf ( i ) ; if ( isPalindrome ( s ) ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 1 ; long ans = getSum ( n ) ; System . out . print ( ans ) ; } }
class GFG { static int findLength ( int [ ] arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) x = Math . max ( x , arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; int j = i ; while ( arr [ j ] == x ) { j ++ ; if ( j >= n ) break ; } ans = Math . max ( ans , j - i ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int n = arr . length ; System . out . println ( findLength ( arr , n ) ) ; } }
class GFG { static long countPairs ( int arr [ ] , int n ) { long count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
class GFG { static long countPairs ( int arr [ ] , int n ) { int countZero = 0 ; int countTwo = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) countZero ++ ; else if ( arr [ i ] == 2 ) countTwo ++ ; } long pair0 = ( countZero * ( countZero - 1 ) ) / 2 ; long pair2 = ( countTwo * ( countTwo - 1 ) ) / 2 ; return pair0 + pair2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 3 , - 1 , 4 } ; int n = arr . length ; System . out . print ( cntSubSeq ( arr , n ) ) ; } }
class GFG { static int findMin ( int arr [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) m = Math . max ( m , arr [ i ] ) ; int cnt [ ] = new int [ m + 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j * j <= arr [ i ] ; j ++ ) { if ( arr [ i ] % j == 0 ) { if ( j * j == arr [ i ] ) cnt [ j ] ++ ; else { cnt [ j ] ++ ; cnt [ arr [ i ] / j ] ++ ; } } } } for ( int i = 1 ; i <= m + 1 ; i ++ ) if ( cnt [ i ] == 0 ) { return i ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 12 , 6 } ; int n = arr . length ; System . out . println ( findMin ( arr , n ) ) ; } }
class GFG { static float findNumber ( int N , int S ) { float i = ( ( ( float ) ( N ) * ( float ) ( N + 1 ) ) / 4 ) - ( ( float ) ( S + 1 ) / 2 ) ; return i ; } static void check ( int N , int S ) { float i = findNumber ( N , S ) ; int integerI = ( int ) i ; if ( i - integerI == 0 ) System . out . println ( " Yes : ▁ " + integerI + " , ▁ " + ( integerI + 1 ) ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 4 , S = 3 ; check ( N , S ) ; N = 5 ; S = 3 ; check ( N , S ) ; } }
class GFG { static int maxXOR ( int arr [ ] , int n ) { int xorArr = 0 ; for ( int i = 0 ; i < n ; i ++ ) xorArr ^= arr [ i ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , ( xorArr ^ arr [ i ] ) ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 3 } ; int n = arr . length ; System . out . println ( maxXOR ( arr , n ) ) ; } }
class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i < sz + 1 ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void removePrimes ( int arr [ ] , int len ) { sieve ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( int j = i ; j < len - 1 ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 } ; int len = arr . length ; removePrimes ( arr , len ) ; } }
import java . util . * ; class GFG { static boolean isPossible ( int x , int y ) { if ( ( x - y ) == 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int x = 100 , y = 98 ; if ( isPossible ( x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int subgrids ( int N , int Z , int row [ ] , int col [ ] , int r , int d ) { Vector < Integer > conrow = new Vector < Integer > ( ) ; Vector < Integer > concol = new Vector < Integer > ( ) ; int K = ( int ) Math . sqrt ( Z ) ; conrow . add ( row [ 0 ] - 0 - 1 ) ; conrow . add ( N + 1 - row [ r - 1 ] - 1 ) ; for ( int i = 1 ; i < r ; i ++ ) { conrow . add ( row [ i ] - row [ i - 1 ] - 1 ) ; } concol . add ( col [ 0 ] - 0 - 1 ) ; concol . add ( N + 1 - col [ d - 1 ] - 1 ) ; for ( int i = 1 ; i < d ; i ++ ) { concol . add ( col [ i ] - col [ i - 1 ] - 1 ) ; } int row_size = conrow . size ( ) ; int col_size = concol . size ( ) ; int answer = 0 ; for ( int i = 0 ; i < row_size ; i ++ ) { for ( int j = 0 ; j < col_size ; j ++ ) { int total = ( concol . get ( j ) / K ) * ( conrow . get ( i ) / K ) ; answer += ( total ) ; } } return answer ; } public static void main ( String [ ] args ) { int N = 8 , Z = 4 ; int row [ ] = { 4 , 6 } ; int col [ ] = { 3 , 8 } ; int r = row . length ; int d = col . length ; System . out . print ( subgrids ( N , Z , row , col , r , d ) ) ; } }
class GFG { static boolean digitDividesK ( int num , int k ) { while ( num != 0 ) { int d = num % 10 ; if ( d != 0 && k % d == 0 ) return true ; num = num / 10 ; } return false ; } static int findCount ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( digitDividesK ( i , k ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int l = 20 , r = 35 ; int k = 45 ; System . out . println ( findCount ( l , r , k ) ) ; } }
class GFG { static void perfectSquares ( float l , float r ) { int number = ( int ) Math . ceil ( Math . sqrt ( l ) ) ; int n2 = number * number ; number = ( number * 2 ) + 1 ; while ( ( n2 >= l && n2 <= r ) ) { System . out . print ( n2 + " ▁ " ) ; n2 = n2 + number ; number += 2 ; } } public static void main ( String [ ] args ) { int l = 2 , r = 24 ; perfectSquares ( l , r ) ; } }
class GFG { static int xorK ( int n , int k ) { int res = n ; for ( int i = 1 ; i < k ; i ++ ) res = ( res ^ n ) ; return n ; } public static void main ( String [ ] args ) { int n = 123 , k = 3 ; System . out . print ( xorK ( n , k ) ) ; } }
import java . util . * ; class GFG { static void findRemainders ( long n ) { HashSet < Long > vc = new HashSet < Long > ( ) ; for ( long i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( n / i ) ; for ( long i = ( long ) ( n / Math . ceil ( Math . sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . add ( i ) ; for ( long it : vc ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }
class GFG { static int MAX = 100005 ; static boolean [ ] prime = new boolean [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int countPrimes ( int n ) { SieveOfEratosthenes ( ) ; int cnt = 0 ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . print ( countPrimes ( n ) ) ; } }
class GFG { static int MAX_VALID_YR = 9999 ; static int MIN_VALID_YR = 1800 ; static boolean isLeap ( int year ) { return ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) ) ; } static boolean isValidDate ( int d , int m , int y ) { if ( y > MAX_VALID_YR y < MIN_VALID_YR ) return false ; if ( m < 1 m > 12 ) return false ; if ( d < 1 d > 31 ) return false ; if ( m == 2 ) { if ( isLeap ( y ) ) return ( d <= 29 ) ; else return ( d <= 28 ) ; } if ( m == 4 m == 6 m == 9 m == 11 ) return ( d <= 30 ) ; return true ; } static void printPalindromeDates ( int y1 , int y2 ) { for ( int year = y1 ; year <= y2 ; year ++ ) { String str = String . valueOf ( year ) ; String rev = str ; rev = reverse ( rev ) ; int day = Integer . parseInt ( rev . substring ( 0 , 2 ) ) ; int month = Integer . parseInt ( rev . substring ( 2 , 2 + 2 ) ) ; if ( isValidDate ( day , month , year ) ) { System . out . print ( rev + str + "NEW_LINE"); } } } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int y1 = 2001 , y2 = 2005 ; printPalindromeDates ( y1 , y2 ) ; } }
class GFG { static int MAX = 10 ; static boolean distinctDigits ( int x ) { boolean [ ] present = new boolean [ MAX ] ; while ( x > 0 ) { int digit = x % 10 ; if ( present [ digit ] ) return false ; present [ digit ] = true ; x /= 10 ; } return true ; } static int findK ( int l , int r ) { int maxExp = Integer . MIN_VALUE ; int k = - 1 ; for ( int i = l ; i <= r ; i ++ ) { if ( distinctDigits ( i ) ) { int exp = ( l - i ) * ( i - r ) ; if ( exp >= maxExp ) { k = i ; maxExp = exp ; } } } return k ; } public static void main ( String [ ] args ) { int l = 50 , r = 60 ; System . out . print ( findK ( l , r ) ) ; } }
class GFG { static void findPerm ( int Q [ ] , int n ) { int minval = 0 , qsum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { qsum += Q [ i ] ; if ( qsum < minval ) minval = qsum ; } int [ ] P = new int [ n ] ; P [ 0 ] = 1 - minval ; boolean permFound = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { P [ i + 1 ] = P [ i ] + Q [ i ] ; if ( P [ i + 1 ] > n P [ i + 1 ] < 1 ) { permFound = false ; break ; } } if ( permFound ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( P [ i ] + " ▁ " ) ; } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int Q [ ] = { - 2 , 1 } ; int n = 1 + Q . length ; findPerm ( Q , n ) ; } }
import java . util . * ; import java . util . HashMap ; import java . util . Map ; class GFG { static String Hex ( int num ) { HashMap < Integer , Character > m = new HashMap < Integer , Character > ( ) ; char digit = '0' ; char c = ' a ' ; for ( int i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m . put ( i , digit ) ; digit ++ ; } else { m . put ( i , c ) ; c ++ ; } } String res = " " ; if ( num == 0 ) { return "0" ; } if ( num > 0 ) { while ( num != 0 ) { res = m . get ( num % 16 ) + res ; num /= 16 ; } } else { int n = num ; while ( n != 0 ) { res = m . get ( n % 16 ) + res ; n /= 16 ; } } return res ; } public static void main ( String [ ] args ) { int x = 134 , y = - 1 , z = - 234 ; System . out . println ( " Hexa ▁ representation ▁ for " ) ; System . out . println ( x + " ▁ is ▁ " + Hex ( x ) ) ; System . out . println ( y + " ▁ is ▁ " + Hex ( y ) ) ; System . out . println ( z + " ▁ is ▁ " + Hex ( z ) ) ; } }
import java . util . * ; class GFG { static final int N = 100005 ; static final int mod = ( int ) ( 1e9 + 7 ) ; static long [ ] factorial = new long [ N ] ; static long [ ] modinverse = new long [ N ] ; static long power ( long a , int m1 ) { if ( m1 == 0 ) return 1 ; else if ( m1 == 1 ) return a ; else if ( m1 == 2 ) return ( 1L * a * a ) % mod ; else if ( m1 % 2 == 1 ) return ( 1L * a * power ( power ( a , m1 / 2 ) , 2 ) ) % mod ; else return power ( power ( a , m1 / 2 ) , 2 ) % mod ; } static void factorialfun ( ) { factorial [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) factorial [ i ] = ( 1L * factorial [ i - 1 ] * i ) % mod ; } static void modinversefun ( ) { modinverse [ N - 1 ] = ( int ) ( power ( factorial [ N - 1 ] , mod - 2 ) % mod ) ; for ( int i = N - 2 ; i >= 0 ; i -- ) modinverse [ i ] = ( 1 * modinverse [ i + 1 ] * ( i + 1 ) ) % mod ; } static long binomial ( int n , int r ) { if ( r > n ) return 0 ; long a = ( 1L * factorial [ n ] * modinverse [ n - r ] ) % mod ; a = ( 1 * a * modinverse [ r ] ) % mod ; return a ; } static void arrange_balls ( int n , int k ) { factorialfun ( ) ; modinversefun ( ) ; for ( int i = 1 ; i <= k ; i ++ ) System . out . print ( ( 1L * binomial ( n - k + 1 , i ) * binomial ( k - 1 , i - 1 ) ) % mod + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 3 ; arrange_balls ( n , k ) ; } }
class GFG { static int min_changes ( int a [ ] , int n ) { int ans_a = 0 , ans_b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . min ( ans_a , ans_b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } ; int n = a . length ; System . out . println ( min_changes ( a , n ) ) ; } }
class GFG { static void ans ( int n ) { if ( n == 1 ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } public static void main ( String [ ] args ) { int n = 2 ; ans ( n ) ; } }
class GFG { static boolean isPossible ( int w , int h , int x , int y ) { if ( x * 2 == w && y * 2 == h ) return true ; return false ; } public static void main ( String [ ] args ) { int w = 1 , h = 2 , x = 1 , y = 2 ; if ( isPossible ( w , h , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static String minNum ( char num [ ] , int k ) { int len = num . length ; if ( len == 0 k == 0 ) { String num_str = new String ( num ) ; return num_str ; } if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } String num_str = new String ( num ) ; return num_str ; } public static void main ( String args [ ] ) { String num = "91945" ; int k = 3 ; System . out . println ( minNum ( num . toCharArray ( ) , k ) ) ; } }
class GFG { static int minsteps ( int n , int m ) { if ( m > n ) { return - 1 ; } else { return ( ( n + 1 ) / 2 + m - 1 ) / m * m ; } } public static void main ( String [ ] args ) { int n = 17 , m = 4 ; int ans = minsteps ( n , m ) ; System . out . println ( ans ) ; } }
class GFG { static boolean isFactorial ( int n ) { for ( int i = 1 ; ; i ++ ) { if ( n % i == 0 ) { n /= i ; } else { break ; } } if ( n == 1 ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 24 ; boolean ans = isFactorial ( n ) ; if ( ans == true ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class GFG { static class Node { int data ; Node next ; } ; static Node removeLeadingZeros ( Node a ) { if ( a != null && a . data == 0 ) return removeLeadingZeros ( a . next ) ; else return a ; } static int getSize ( Node a ) { int sz = 0 ; while ( a != null ) { a = a . next ; sz ++ ; } return sz ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static int compare ( Node a , Node b ) { a = removeLeadingZeros ( a ) ; b = removeLeadingZeros ( b ) ; int lenA = getSize ( a ) ; int lenB = getSize ( b ) ; if ( lenA > lenB ) { return 1 ; } else if ( lenB > lenA ) { return - 1 ; } while ( a != null && b != null ) { if ( a . data > b . data ) return 1 ; else if ( a . data < b . data ) return - 1 ; a = a . next ; b = b . next ; } return 0 ; } public static void main ( String [ ] args ) { Node a = null ; a = push ( a , 7 ) ; a = push ( a , 6 ) ; a = push ( a , 5 ) ; Node b = null ; b = push ( b , 3 ) ; b = push ( b , 3 ) ; b = push ( b , 2 ) ; System . out . println ( compare ( a , b ) ) ; } }
import java . util . * ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int subSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 40 , 25 , 12 , 10 } ; int n = arr . length ; System . out . println ( sumModArr ( arr , n ) ) ; } }
class GFG { static int minHealth ( int health [ ] , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { gcd = __gcd ( gcd , health [ i ] ) ; } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int health [ ] = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n = health . length ; System . out . println ( minHealth ( health , n ) ) ; } }
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void constructArr ( int pair [ ] , int n ) { int size = ( 1 + ( int ) Math . sqrt ( 1 + 8 * n ) ) / 2 ; int [ ] arr = new int [ size ] ; arr [ 0 ] = ( int ) Math . sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } public static void main ( String [ ] args ) { int pair [ ] = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = pair . length ; constructArr ( pair , n ) ; } }
class GFG { static void findPairs ( int arr [ ] , int n ) { int cntEven = 0 , cntOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) cntEven ++ ; else cntOdd ++ ; } int evenPairs = 0 ; evenPairs += ( ( cntEven * ( cntEven - 1 ) ) / 2 ) ; evenPairs += ( ( cntOdd * ( cntOdd - 1 ) ) / 2 ) ; int oddPairs = 0 ; oddPairs += ( cntEven * cntOdd ) ; System . out . println ( " Odd ▁ pairs ▁ = ▁ " + oddPairs ) ; System . out . println ( " Even ▁ pairs ▁ = ▁ " + evenPairs ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; findPairs ( arr , n ) ; } }
class GFG { static int binomialCoeff ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int Fuss_catalan ( int n ) { int c = binomialCoeff ( 3 * n , n ) ; return c / ( 2 * n + 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 10 ; i ++ ) System . out . print ( Fuss_catalan ( i ) + " ▁ " ) ; } }
import java . util . * ; class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static int minDifference ( int L , int R ) { int fst = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { fst = i ; break ; } } int snd = 0 ; for ( int i = fst + 1 ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { snd = i ; break ; } } if ( snd == 0 ) return - 1 ; int diff = snd - fst ; int left = snd + 1 ; int right = R ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { if ( i - snd <= diff ) { fst = snd ; snd = i ; diff = snd - fst ; } } } return diff ; } public static void main ( String [ ] args ) { sieve ( ) ; int L = 21 , R = 50 ; System . out . println ( minDifference ( L , R ) ) ; } }
class GFG { static int cntSubArr ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_gcd = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_gcd = __gcd ( curr_gcd , arr [ j ] ) ; ans += ( curr_gcd == 1 ) ? 1 : 0 ; } } return ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( cntSubArr ( arr , n ) ) ; } }
class GFG { static int countElements ( int p [ ] , int n ) { int ans = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( p [ i - 1 ] > p [ i ] && p [ i ] > p [ i + 1 ] ) ans ++ ; else if ( p [ i - 1 ] < p [ i ] && p [ i ] < p [ i + 1 ] ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int p [ ] = { 2 , 5 , 1 , 3 , 4 } ; int n = p . length ; System . out . println ( countElements ( p , n ) ) ; } }
class GFG { static int countNums ( int a , int b , int c , int d ) { int x = b / c - ( a - 1 ) / c ; int y = b / d - ( a - 1 ) / d ; int k = ( c * d ) / __gcd ( c , d ) ; int z = b / k - ( a - 1 ) / k ; return b - a + 1 - x - y + z ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 50 , c = 4 , d = 6 ; System . out . println ( countNums ( a , b , c , d ) ) ; } }
class GFG { static int cntSubArrays ( int [ ] arr , int n , int k ) { int res = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; res += Math . max ( j - i - k + 1 , 0 ) ; i = j ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 } ; int n = arr . length ; int k = 2 ; System . out . println ( cntSubArrays ( arr , n , k ) ) ; } }
import java . util . Arrays ; class GFG { static int maxLen ( int [ ] arr , int n ) { int min_val = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; int freq = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == min_val ) freq ++ ; } return freq ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( maxLen ( arr , n ) ) ; } }
class GFG { static void print_primes_till_N ( int N ) { int i , j , flag ; System . out . println ( " Prime ▁ numbers ▁ between ▁ 1 ▁ and ▁ " + N + " ▁ are : " ) ; for ( i = 1 ; i <= N ; i ++ ) { if ( i == 1 i == 0 ) continue ; flag = 1 ; for ( j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 100 ; print_primes_till_N ( N ) ; } }
class GFG { static void last_digit ( int X , int Y ) { System . out . print ( X % Y ) ; } public static void main ( String [ ] args ) { int X = 55 , Y = 3 ; last_digit ( X , Y ) ; } }
class GFG { static int countSquares ( int n ) { return ( int ) ( Math . pow ( n , 2 ) - ( 2 * n ) + 2 ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( countSquares ( n ) ) ; } }
class GFG { static int MAX = 32 ; static int findX ( int A , int B ) { int X = 0 ; for ( int bit = 0 ; bit < MAX ; bit ++ ) { int tempBit = 1 << bit ; int bitOfX = A & B & tempBit ; X += bitOfX ; } return X ; } public static void main ( String [ ] args ) { int A = 11 , B = 13 ; System . out . println ( findX ( A , B ) ) ; } }
class GFG { static int cntPairs ( int a [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd ++ ; else if ( ( a [ i ] / 2 ) % 2 == 1 ) even ++ ; } int ans = odd * even + ( odd * ( odd - 1 ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 2 , 7 , 11 , 14 , 15 , 18 } ; int n = a . length ; System . out . println ( cntPairs ( a , n ) ) ; } }
class GFG { static boolean isPossible ( int a [ ] , int n ) { int cur = a [ 0 ] ; cur -- ; for ( int i = 1 ; i < n ; i ++ ) { int nxt = a [ i ] ; if ( nxt > cur ) nxt -- ; else if ( nxt < cur ) return false ; cur = nxt ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 1 , 2 , 3 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
class GFG { static void max_element ( int a [ ] , int n ) { int [ ] pre = new int [ n ] ; pre [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = Math . max ( pre [ i - 1 ] , a [ i ] ) ; int [ ] suf = new int [ n ] ; suf [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suf [ i ] = Math . max ( suf [ i + 1 ] , a [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) System . out . print ( suf [ i + 1 ] + " ▁ " ) ; else if ( i == n - 1 ) System . out . print ( pre [ i - 1 ] + " ▁ " ) ; else System . out . print ( Math . max ( pre [ i - 1 ] , suf [ i + 1 ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 2 , 5 , 6 , 1 , 3 } ; int n = a . length ; max_element ( a , n ) ; } }
class GFG { static boolean isPossible ( int a [ ] , int n ) { int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != i + 1 ) k ++ ; } if ( k <= 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 2 , 3 , 4 , 1 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int odd_digits ( int n ) { if ( n < 10 ) return n ; else if ( n / 10 < 10 ) return 9 ; else if ( n / 100 < 10 ) return 9 + n - 99 ; else if ( n / 1000 < 10 ) return 9 + 900 ; else if ( n / 10000 < 10 ) return 909 + n - 9999 ; else return 90909 ; } public static void main ( String [ ] args ) { int n = 893 ; System . out . println ( odd_digits ( n ) ) ; } }
class GFG { static int nDigitPalindromes ( int n ) { return ( 9 * ( int ) Math . pow ( 10 , ( n - 1 ) / 2 ) ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( nDigitPalindromes ( n ) ) ; } }
class GFG { static int maxLCM ( int n ) { return ( n * ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( maxLCM ( n ) ) ; } }
class GFG { static int cntCells ( int n ) { int cells = ( int ) Math . pow ( n , 2 ) + ( int ) Math . pow ( n - 1 , 2 ) ; return cells ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( cntCells ( n ) ) ; } }
class GFG { static int findSum ( int n ) { return ( int ) Math . pow ( n , 3 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }
class GFG { static void findNumbers ( int n ) { String ans = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans += '7' ; String even = ans + '6' ; String odd = ans + '7' ; System . out . println ( " Even ▁ : ▁ " + even ) ; System . out . println ( " Odd ▁ : ▁ " + odd ) ; } public static void main ( String args [ ] ) { int n = 4 ; findNumbers ( n ) ; } }
class GFG { static int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } public static void main ( String [ ] args ) { int a = 10 , b = 17 , n = 3 ; System . out . println ( nth_term ( a , b , n ) ) ; } }
import java . util . * ; class GFG { static boolean inProportion ( int [ ] arr ) { int n = 4 ; Arrays . sort ( arr ) ; long extremes = ( long ) arr [ 0 ] * ( long ) arr [ 3 ] ; long means = ( long ) arr [ 1 ] * ( long ) arr [ 2 ] ; if ( extremes == means ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 4 , 2 } ; if ( inProportion ( arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static int countNum ( int n ) { if ( n > 10 ) return 0 ; return ( 9 * factorial ( 9 ) / factorial ( 10 - n ) ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countNum ( n ) ) ; } }
class GFG { static int count ( int n ) { return ( int ) ( - 1 + Math . sqrt ( 1 + 8 * n ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( count ( n ) ) ; } }
class GFG { static void findEquation ( int a , int b ) { int sum = ( a + b ) ; int product = ( a * b ) ; System . out . println ( " x ^ 2 ▁ - ▁ ( " + sum + " x ) ▁ + ▁ ( " + product + " ) ▁ = ▁ 0" ) ; } public static void main ( String args [ ] ) { int a = 2 , b = 3 ; findEquation ( a , b ) ; } }
class GFG { static int cal ( int n ) { double res = Math . pow ( Math . ceil ( ( Math . pow ( Math . pow ( 10 , ( n - 1 ) ) , 1 / 4 ) ) ) , 4 ) ; return ( int ) res ; } public static void main ( String [ ] args ) { int n = 1 ; System . out . println ( cal ( n ) ) ; } }
class GFG { static long binomialCoeff ( long n , long k ) { if ( k == 0 k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } static long fact ( long n ) { if ( n >= 1 ) return n * fact ( n - 1 ) ; else return 1 ; } static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) { return true ; } return false ; } static long countWords ( String s , int p , int q ) { long countc = 0 , countv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) countv ++ ; else countc ++ ; } long a = binomialCoeff ( countc , p ) ; long b = binomialCoeff ( countv , q ) ; long c = fact ( p + q ) ; long ans = ( a * b ) * c ; return ans ; } public static void main ( String [ ] args ) { String s = " crackathon " ; int p = 4 , q = 3 ; System . out . println ( countWords ( s , p , q ) ) ; } }
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void generateArr ( int A [ ] , int n ) { int [ ] B = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j ] % A [ i ] == 0 ) cnt ++ ; } B [ i ] = cnt ; } printArr ( B , n ) ; } public static void main ( String args [ ] ) { int A [ ] = { 3 , 5 , 1 } ; int n = A . length ; generateArr ( A , n ) ; } }
class GFG { static void findNums ( int n ) { if ( n <= 11 ) { if ( n == 8 ) System . out . print ( "4 ▁ 4" ) ; if ( n == 10 ) System . out . print ( "4 ▁ 6" ) ; else System . out . print ( " - 1" ) ; return ; } if ( n % 2 == 0 ) System . out . print ( "4 ▁ " + ( n - 4 ) ) ; else System . out . print ( "9 ▁ " + ( n - 9 ) ) ; } public static void main ( String args [ ] ) { int n = 13 ; findNums ( n ) ; } }
class GFG { static int count ( int n ) { return ( int ) Math . pow ( 2 , n - 1 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( count ( n ) ) ; } }
class GFG { static long nextFibonacci ( int n ) { double a = n * ( 1 + Math . sqrt ( 5 ) ) / 2.0 ; return Math . round ( a ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( nextFibonacci ( n ) ) ; } }
class GFG { static int sum ( int h ) { return ( int ) Math . pow ( 2 , h - 1 ) ; } public static void main ( String [ ] args ) { int L = 3 ; System . out . println ( sum ( L ) ) ; } }
class GFG { static int product ( int a [ ] , int n ) { int ans = 1 ; int val = ( int ) Math . pow ( 2 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { ans *= ( int ) Math . pow ( a [ i ] , val ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 2 ; int a [ ] = { 3 , 7 } ; System . out . println ( product ( a , n ) ) ; } }
class GFG { static int oddFib ( int n ) { n = ( 3 * n + 1 ) / 2 ; int a = - 1 , b = 1 , c = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( oddFib ( n ) ) ; } }
class GFG { static void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " - 1" ) ; return ; } int i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } System . out . println ( " n ▁ = ▁ " + first + " ▁ and ▁ r ▁ = ▁ " + second ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; findPair ( arr , n ) ; } }
import java . util . * ; class GFG { static void triangle ( int n ) { int k = 2 * n - 2 ; int ch = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < k ; j ++ ) System . out . print ( " ▁ " ) ; k = k - 1 ; for ( int j = 0 ; j <= i ; j ++ ) { System . out . print ( ch ++ + " ▁ " ) ; } System . out . println ( ) ; } } static int maxHeight ( int n ) { return ( ( ( int ) Math . sqrt ( 1 + 8.0 * n ) ) - 1 ) / 2 ; } public static void main ( String [ ] args ) { int N = 9 ; triangle ( maxHeight ( N ) ) ; } }
class GFG { static int no_of_ways ( int a [ ] , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } public static void main ( String [ ] args ) { int n = 4 ; int a1 [ ] = { 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a1 , n ) ) ; n = 5 ; int a2 [ ] = { 1 , 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a2 , n ) ) ; n = 5 ; int a3 [ ] = { 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a3 , n ) ) ; n = 6 ; int a4 [ ] = { 1 , 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a4 , n ) ) ; } }
class GFG { static String maxInt ( char str [ ] ) { for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] < '5' ) { str [ i ] = ( char ) ( ( '9' - str [ i ] ) + '0' ) ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; String str2 = new String ( str ) ; return str2 ; } public static void main ( String [ ] args ) { String str = "42" ; System . out . println ( maxInt ( str . toCharArray ( ) ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void FindRatio ( int a , int b , int c ) { int up = Math . abs ( b - c ) ; int down = Math . abs ( c - a ) ; int g = gcd ( up , down ) ; up /= g ; down /= g ; System . out . println ( up + " : " + down ) ; } public static void main ( String [ ] args ) { int a = 4 , b = 10 , c = 6 ; FindRatio ( a , b , c ) ; } }
class GFG { static int longest_gap ( int N ) { int distance = 0 , count = 0 , first_1 = - 1 , last_1 = - 1 ; while ( N != 0 ) { count ++ ; int r = N & 1 ; if ( r == 1 ) { first_1 = first_1 == - 1 ? count : first_1 ; last_1 = count ; } N = N / 2 ; } if ( last_1 <= first_1 ) { return - 1 ; } else { distance = ( last_1 - first_1 - 1 ) ; return distance ; } } public static void main ( String [ ] args ) { int N = 131 ; System . out . println ( longest_gap ( N ) ) ; N = 8 ; System . out . println ( longest_gap ( N ) ) ; N = 17 ; System . out . println ( longest_gap ( N ) ) ; N = 33 ; System . out . println ( longest_gap ( N ) ) ; } }
class GFG { static boolean isPossible ( int x , int y , int k ) { int minMoves = Math . abs ( x ) + Math . abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int TOTAL_BITS = 32 ; static boolean isPossible ( int n ) { int cnt = Integer . bitCount ( n ) ; if ( cnt == TOTAL_BITS / 2 ) return true ; return false ; } static public void main ( String [ ] arr ) { int n = 524280 ; if ( isPossible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static final int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 204 , b = 240 ; if ( areAnagrams ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int days [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; static int dayOfYear ( String date ) { int year = Integer . parseInt ( date . substring ( 0 , 4 ) ) ; int month = Integer . parseInt ( date . substring ( 5 , 7 ) ) ; int day = Integer . parseInt ( date . substring ( 8 ) ) ; if ( month > 2 && year % 4 == 0 && ( year % 100 != 0 year % 400 == 0 ) ) { ++ day ; } while ( -- month > 0 ) { day = day + days [ month - 1 ] ; } return day ; } public static void main ( String [ ] args ) { String date = "2019-01-09" ; System . out . println ( dayOfYear ( date ) ) ; } }
class GFG { static int Max_Sum ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( Max_Sum ( n ) ) ; } }
import java . util . * ; class GFG { static boolean isDivisible ( int arr [ ] , int n ) { int lastDigit = arr [ n - 1 ] % 10 ; if ( lastDigit == 0 ) return true ; return false ; } static public void main ( String [ ] arg ) { int arr [ ] = { 12 , 65 , 46 , 37 , 99 } ; int n = arr . length ; if ( isDivisible ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long divTermCount ( long a , long b , long c , long num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; } static public void main ( String [ ] arr ) { long a = 7 , b = 3 , c = 5 , n = 100 ; System . out . println ( divTermCount ( a , b , c , n ) ) ; } }
import java . util . * ; class GFG { static Vector < Long > getArray ( int n ) { Vector < Long > ans = new Vector < Long > ( ) ; long p2 = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) ans . add ( p2 ) ; n >>= 1 ; p2 *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; Vector < Long > ans = getArray ( n ) ; for ( Long i : ans ) System . out . print ( i + " ▁ " ) ; } }
import java . io . * ; class GFG { static int M = 3 ; static int N = 2 ; static void getIndex ( int n , int shuffle [ ] [ ] ) { for ( int i = 0 ; i < 3 ; i ++ ) { if ( shuffle [ i ] [ 0 ] == n ) n = shuffle [ i ] [ 1 ] ; else if ( shuffle [ i ] [ 1 ] == n ) n = shuffle [ i ] [ 0 ] ; } System . out . println ( n ) ; } public static void main ( String [ ] args ) { int n = 3 ; int shuffle [ ] [ ] = { { 3 , 1 } , { 2 , 1 } , { 1 , 2 } } ; getIndex ( n , shuffle ) ; } }
class GFG { static final int mod = ( int ) ( 1e9 + 7 ) ; static long power ( int p ) { long res = 1 ; for ( int i = 1 ; i <= p ; ++ i ) { res *= 2 ; res %= mod ; } return res % mod ; } static long subset_square_sum ( int A [ ] ) { int n = A . length ; long ans = 0 ; for ( int i : A ) { ans += ( 1 * i * i ) % mod ; ans %= mod ; } return ( 1 * ans * power ( n - 1 ) ) % mod ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 } ; System . out . println ( subset_square_sum ( A ) ) ; } }
import java . util . * ; class GFG { static long fact ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static long nCr ( int n , int r ) { return fact ( n ) / ( 1 * fact ( r ) * fact ( n - r ) ) ; } static long number_of_subsequences ( int arr [ ] , int k , int n ) { long s = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > j : mp . entrySet ( ) ) { s = s + 1 * nCr ( j . getValue ( ) , k ) ; } return s ; } static public void main ( String [ ] arg ) { int arr [ ] = { 1 , 1 , 1 , 1 , 2 , 2 , 2 } ; int k = 2 ; int n = arr . length ; System . out . println ( number_of_subsequences ( arr , k , n ) ) ; } }
import java . util . * ; class GFG { static long countTriplets ( int A , int B , int C ) { long ans = 0 ; for ( int i = 1 ; i <= A ; i ++ ) { for ( int j = 1 ; j <= B ; j ++ ) { for ( int k = 1 ; k <= C ; k ++ ) { if ( i * k > j * j ) ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int A = 3 , B = 2 , C = 2 ; System . out . println ( countTriplets ( A , B , C ) ) ; } }
class GFG { static long getSum ( int n ) { long sum = 0 ; int k = ( int ) Math . sqrt ( n ) ; for ( int i = 1 ; i <= k ; i ++ ) { sum += Math . floor ( n / i ) ; } sum *= 2 ; sum -= Math . pow ( k , 2 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( getSum ( n ) ) ; } }
class GFG { static int distinctRemainders ( int n ) { if ( n % 2 == 0 ) return ( n / 2 ) ; return ( 1 + ( n / 2 ) ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( distinctRemainders ( n ) ) ; } }
class GFG { static int N = 100050 ; static int [ ] lpf = new int [ N ] ; static int [ ] mobius = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] == 0 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] == 0 ) lpf [ j ] = i ; } static void Mobius ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } } static int gcd_pairs ( int a [ ] , int n ) { int maxi = 0 ; int [ ] fre = new int [ N ] ; for ( int i = 0 ; i < n ; i ++ ) { fre [ a [ i ] ] ++ ; maxi = Math . max ( a [ i ] , maxi ) ; } least_prime_factor ( ) ; Mobius ( ) ; int ans = 0 ; for ( int i = 1 ; i <= maxi ; i ++ ) { if ( mobius [ i ] == 0 ) continue ; int temp = 0 ; for ( int j = i ; j <= maxi ; j += i ) temp += fre [ j ] ; ans += temp * ( temp - 1 ) / 2 * mobius [ i ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; System . out . print ( gcd_pairs ( a , n ) ) ; } }
import java . util . * ; @ SuppressWarnings ( " unchecked " ) class GFG { static boolean graphExists ( ArrayList a , int n ) { while ( true ) { Collections . sort ( a , Collections . reverseOrder ( ) ) ; if ( ( int ) a . get ( 0 ) == 0 ) return true ; int v = ( int ) a . get ( 0 ) ; a . remove ( a . get ( 0 ) ) ; if ( v > a . size ( ) ) return false ; for ( int i = 0 ; i < v ; i ++ ) { a . set ( i , ( int ) a . get ( i ) - 1 ) ; if ( ( int ) a . get ( i ) < 0 ) return false ; } } } public static void main ( String [ ] args ) { ArrayList a = new ArrayList ( ) ; a . add ( 3 ) ; a . add ( 3 ) ; a . add ( 3 ) ; a . add ( 3 ) ; int n = a . size ( ) ; if ( graphExists ( a , n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " NO " ) ; } } }
class GFG { static int negProdSubArr ( int arr [ ] , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . length ; System . out . println ( negProdSubArr ( arr , n ) ) ; } }
class GFG { static int difference ( int n ) { int S , res ; S = ( n * ( n + 1 ) ) / 2 ; res = S * ( S - 1 ) ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( difference ( n ) ) ; } }
import java . util . * ; class GFG { static int N = 100005 ; static int [ ] lpf = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] % 2 != 1 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] % 2 != 0 ) lpf [ j ] = i ; } static void Mobius ( int n ) { int [ ] mobius = new int [ N ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( mobius [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; Arrays . fill ( lpf , - 1 ) ; least_prime_factor ( ) ; Mobius ( n ) ; } }
import java . util . * ; class GFG { static int DIGITS = 4 , MIN = 1000 , MAX = 9999 ; static int getBest ( int prev , int cur ) { int maximum = Math . max ( MIN , prev ) ; for ( int i = maximum ; i <= MAX ; i ++ ) { int cnt = 0 ; int a = i ; int b = cur ; for ( int k = 0 ; k < DIGITS ; k ++ ) { if ( a % 10 != b % 10 ) cnt += 1 ; a /= 10 ; b /= 10 ; } if ( cnt <= 1 ) return i ; } return - 1 ; } static void getList ( int arr [ ] , int n ) { Vector < Integer > myList = new Vector < Integer > ( ) ; int i , cur ; boolean possible = true ; myList . add ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { cur = arr [ i ] ; myList . add ( getBest ( myList . lastElement ( ) , cur ) ) ; if ( myList . lastElement ( ) == - 1 ) { possible = false ; break ; } } if ( possible ) { for ( i = 1 ; i < myList . size ( ) ; i ++ ) System . out . print ( myList . get ( i ) + " ▁ " ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1095 , 1094 , 1095 } ; int n = arr . length ; getList ( arr , n ) ; } }
class GFG { static int countFactors ( int n , int p ) { int pwr = 0 ; while ( n > 0 && n % p == 0 ) { n /= p ; pwr ++ ; } return pwr ; } static int getCount ( int l , int r , int p ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { cnt += countFactors ( i , p ) ; } return cnt ; } public static void main ( String [ ] args ) { int l = 2 , r = 8 , p = 2 ; System . out . println ( getCount ( l , r , p ) ) ; } }
import java . util . * ; class GFG { static int getCount ( int l , int r , int p ) { int cnt = 0 ; int val = p ; while ( true ) { int a = r / val ; int b = ( l - 1 ) / val ; val *= p ; if ( ( a - b ) > 0 ) { cnt += ( a - b ) ; } else break ; } return cnt ; } public static void main ( String [ ] args ) { int l = 2 , r = 8 , p = 2 ; System . out . println ( getCount ( l , r , p ) ) ; } }
import java . util . * ; class GFG { static boolean topsyTurvy ( char [ ] str ) { for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == '2' str [ i ] == '4' str [ i ] == '5' str [ i ] == '6' str [ i ] == '7' str [ i ] == '9' ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = "1234" ; if ( topsyTurvy ( str . toCharArray ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int countSubSeq ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { count ++ ; } } return ( int ) ( Math . pow ( 2 , count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; int k = 3 ; System . out . println ( countSubSeq ( arr , n , k ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000001 ; static int countNum ( int N , int K ) { int [ ] sumPF = new int [ MAX ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == 0 ) { for ( int j = i ; j < N ; j += i ) { sumPF [ j ] += i ; } } } int count = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == K ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 20 , K = 7 ; System . out . println ( countNum ( N , K ) ) ; } }
import java . util . * ; class GFG { static void printMatrix ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) System . out . print ( k + " ▁ " ) ; else System . out . print ( "0 ▁ " ) ; } System . out . print ( "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 3 , k = 7 ; printMatrix ( n , k ) ; } }
import java . util . * ; class GFG { static int findNthNumber ( int A , int B , int N ) { int sum = 0 ; for ( int i = 2 ; i < N ; i ++ ) { sum = A + B ; A = B ; B = sum ; } return sum ; } public static void main ( String [ ] args ) { int A = 5 , B = 7 , N = 10 ; System . out . println ( findNthNumber ( A , B , N ) ) ; } }
import java . math . * ; class GFG { public static void cal_IST ( int h , double r ) { double IST = ( h * r * 1.0 ) / 360 ; int int_IST = ( int ) IST ; int float_IST = ( int ) Math . ceil ( ( int ) ( ( IST - int_IST ) * 60 ) ) ; System . out . println ( int_IST + " : " + float_IST ) ; } public static void main ( String [ ] args ) { int h = 20 ; double r = 150 ; cal_IST ( h , r ) ; } }
import java . util . * ; class GFG { static boolean isPossible ( int arr [ ] , int p , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < p ; i ++ ) sum += arr [ i ] ; if ( sum == ( n * m ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 , m = 4 ; int arr [ ] = { 6 , 3 , 2 , 1 } ; int p = arr . length ; if ( isPossible ( arr , p , n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int MinStep ( int a [ ] , int n ) { int positive = 0 , negative = 0 , zero = 0 ; int step = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } else if ( a [ i ] < 0 ) { negative ++ ; step = step + ( - 1 - a [ i ] ) ; } else { positive ++ ; step = step + ( a [ i ] - 1 ) ; } } if ( negative % 2 == 0 ) { step = step + zero ; } else { if ( zero > 0 ) { step = step + zero ; } else { step = step + 2 ; } } return step ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , - 2 , - 1 , - 3 , 4 } ; int n = a . length ; System . out . println ( MinStep ( a , n ) ) ; } }
import java . util . * ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static void firstN ( int n ) { int num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { System . out . print ( num + " ▁ " ) ; cnt ++ ; } num += 9 ; } } public static void main ( String [ ] args ) { int n = 10 ; firstN ( n ) ; } }
import java . io . * ; class GFG { static long sumOfLastDig ( long n , long m ) { long sum = 0 , k ; k = n / m ; long [ ] arr = new long [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) { arr [ i ] = m * ( i + 1 ) % 10 ; sum += arr [ i ] ; } long rem = k % 10 ; long ans = ( k / 10 ) * sum ; for ( int i = 0 ; i < rem ; i ++ ) { ans += arr [ i ] ; } return ans ; } public static void main ( String [ ] args ) { long n = 100 , m = 3 ; System . out . println ( sumOfLastDig ( n , m ) ) ; } }
import java . util . * ; class GFG { static void compareVal ( int x , int y ) { double a = y * Math . log ( x ) ; double b = x * Math . log ( y ) ; if ( a > b ) System . out . print ( x + " ^ " + y + " ▁ > ▁ " + y + " ^ " + x ) ; else if ( a < b ) System . out . print ( x + " ^ " + y + " ▁ < ▁ " + y + " ^ " + x ) ; else if ( a == b ) System . out . print ( x + " ^ " + y + " ▁ = ▁ " + y + " ^ " + x ) ; } public static void main ( String [ ] args ) { int x = 4 , y = 5 ; compareVal ( x , y ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int nextPrime ( int N ) { if ( N <= 1 ) return 2 ; int prime = N ; boolean found = false ; while ( ! found ) { prime ++ ; if ( isPrime ( prime ) ) found = true ; } return prime ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nextPrime ( N ) ) ; } }
class GFG { static boolean Survives ( int n ) { for ( int i = 2 ; ; i ++ ) { if ( i > n ) return true ; if ( n % i == 0 ) return false ; n -= n / i ; } } public static void main ( String [ ] args ) { int n = 17 ; if ( Survives ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int nthXorFib ( int n , int a , int b ) { if ( n == 0 ) return a ; if ( n == 1 ) return b ; if ( n == 2 ) return ( a ^ b ) ; return nthXorFib ( n % 3 , a , b ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 2 , n = 10 ; System . out . println ( nthXorFib ( n , a , b ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void flip ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; a = lcm / a ; b = lcm / b ; System . out . print ( ( a - 1 ) + " ▁ " + ( b - 1 ) ) ; } public static void main ( String [ ] args ) { int a = 10 ; int b = 5 ; flip ( a , b ) ; } }
import java . io . * ; class GFG { static void find_Solution ( double x , int n ) { double sum = x , e = 2 , o = 1 , p = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { p += 2 ; sum += ( double ) ( o / e ) * ( double ) ( Math . pow ( x , p ) / p ) ; o = o * ( o + 2 ) ; e = e * ( e + 2 ) ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { double x = - 0.5 ; if ( Math . abs ( x ) >= 1 ) { System . out . println ( " Invalid ▁ Input " ) ; } int n = 8 ; find_Solution ( x , n ) ; } }
class GFG { static int totalPay ( int totalItems , int priceOfOneItem , int N , int M ) { int freeItems = 0 , actual = 0 ; freeItems = totalItems / ( N + M ) ; actual = totalItems - freeItems ; int amount = actual * priceOfOneItem ; return amount ; } public static void main ( String [ ] args ) { int T = 12 , P = 8 ; int N = 2 , M = 1 ; System . out . print ( " Amount ▁ = ▁ " + totalPay ( T , P , N , M ) ) ; } }
class GFG { static int p = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res % p ; } public static void main ( String [ ] args ) { long n = 3 ; long x = ( power ( 3 , n ) % p + 1 ) % p ; x = ( x - power ( 2 , n + 1 ) + p ) % p ; x = ( x * power ( 2 , p - 2 ) ) % p ; System . out . println ( x ) ; } }
import java . util . * ; class GFG { static int rightmostNonZero ( int a [ ] , int n ) { int c5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] > 0 && a [ i ] % 5 == 0 ) { a [ i ] /= 5 ; c5 ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { while ( c5 != 0 && a [ i ] > 0 && ( a [ i ] & 1 ) == 0 ) { a [ i ] >>= 1 ; c5 -- ; } } int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { ans = ( ans * a [ i ] % 10 ) % 10 ; } if ( c5 != 0 ) ans = ( ans * 5 ) % 10 ; if ( ans != 0 ) return ans ; return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 7 , 42 , 11 , 64 } ; int n = a . length ; System . out . println ( rightmostNonZero ( a , n ) ) ; } }
class GFG { static void findRemainder ( int n ) { int l = n % 10 ; while ( n >= 10 ) n /= 10 ; int f = n ; int remainder = f % l ; System . out . println ( remainder ) ; } public static void main ( String [ ] args ) { int n = 5223 ; findRemainder ( n ) ; } }
class GFG { static double increaseInVol ( double l , double b , double h ) { double percentInc = ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ; percentInc -= 1 ; percentInc *= 100 ; return percentInc ; } public static void main ( String [ ] args ) { double l = 50 , b = 20 , h = 10 ; System . out . println ( increaseInVol ( l , b , h ) + " % " ) ; } }
import java . util . * ; class GFG { static int countOccurrances ( int n , int d ) { int count = 0 ; while ( n > 0 ) { count = ( n % 10 == d ) ? count + 1 : count ; n = n / 10 ; } return count ; } public static void main ( String args [ ] ) { int d = 2 ; int n = 214215421 ; System . out . println ( countOccurrances ( n , d ) ) ; } }
class GFG { static void findFactors ( int a , int b ) { int c ; c = a + b - 1 ; System . out . print ( c ) ; } public static void main ( String [ ] args ) { int a , b ; a = 13 ; b = 36 ; findFactors ( a , b ) ; } }
class GFG { static void TwentyoneMatchstick ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i += 1 ) { System . out . print ( 5 - arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 2 , 2 } ; int N = arr . length ; TwentyoneMatchstick ( arr , N ) ; } }
import java . util . * ; class GFG { static void expressDigit ( int arr [ ] , int n ) { int min = 9 , index = 0 , temp = 0 ; temp = arr [ 0 ] ; for ( int i = 1 ; i < 10 ; i ++ ) { if ( arr [ i ] < min ) { min = arr [ i ] ; index = i ; } } if ( temp < min ) { System . out . print ( 1 ) ; for ( int i = 1 ; i <= temp + 1 ; i ++ ) System . out . print ( 0 ) ; } else { for ( int i = 0 ; i < min ; i ++ ) System . out . print ( index ) ; System . out . print ( index ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 1 , 2 , 1 , 1 , 3 , 1 , 1 , 1 } ; int N = 10 ; expressDigit ( arr , N ) ; } }
class GFG { public static int countDigits ( int num ) { int cnt = 0 ; while ( num > 0 ) { cnt ++ ; num /= 10 ; } return cnt ; } public static int sumFromStart ( int num , int n , int rem ) { num /= ( ( int ) Math . pow ( 10 , rem ) ) ; int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static int sumFromEnd ( int num , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static float getAverage ( int n , int k , int l ) { int totalDigits = countDigits ( n ) ; if ( totalDigits < ( k + l ) ) return - 1 ; int sum1 = sumFromEnd ( n , l ) ; int sum2 = sumFromStart ( n , k , totalDigits - k ) ; return ( ( float ) ( sum1 + sum2 ) / ( float ) ( k + l ) ) ; } public static void main ( String args [ ] ) { int n = 123456 , k = 2 , l = 3 ; System . out . print ( getAverage ( n , k , l ) ) ; } }
import java . util . * ; public class GFG { static int Wythoff ( HashMap < Integer , HashMap < Integer , Integer > > mp , int n , int k ) { double tau = ( Math . sqrt ( 5 ) + 1 ) / 2.0 , t_n_k ; if ( mp . containsKey ( n ) && mp . get ( n ) . containsKey ( k ) && mp . get ( n ) . get ( k ) != 0 ) return mp . get ( n ) . get ( k ) ; if ( k == - 1 ) { return n - 1 ; } else if ( k == 0 ) { t_n_k = Math . floor ( n * tau ) ; } else { t_n_k = Wythoff ( mp , n , k - 1 ) + Wythoff ( mp , n , k - 2 ) ; } mp . put ( n , new HashMap < Integer , Integer > ( k , ( int ) t_n_k ) ) ; return ( int ) t_n_k ; } static void Wythoff_Array ( int n ) { int i = 0 , j = 0 , count = 0 ; HashMap < Integer , HashMap < Integer , Integer > > mp = new HashMap < Integer , HashMap < Integer , Integer > > ( ) ; while ( count < n ) { System . out . print ( Wythoff ( mp , i + 1 , j + 1 ) ) ; count ++ ; if ( count != n ) System . out . print ( " , ▁ " ) ; i ++ ; j -- ; if ( j < 0 ) { j = i ; i = 0 ; } } } public static void main ( String [ ] args ) { int n = 15 ; Wythoff_Array ( n ) ; } }
import java . util . * ; public class GFG { static boolean is_Prime ( int n ) { if ( n < 2 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void set_primes ( HashMap < Integer , HashMap < Integer , Integer > > mp , HashMap < Integer , HashMap < Integer , Integer > > hash , int c ) { int count = 0 ; for ( int i = 2 ; count < c ; i ++ ) { if ( is_Prime ( i ) ) { if ( ! mp . containsKey ( 0 ) ) { mp . put ( 0 , new HashMap < Integer , Integer > ( ) ) ; } if ( ! mp . get ( 0 ) . containsKey ( count ) ) { mp . get ( 0 ) . put ( count , i ) ; } else { mp . get ( 0 ) . put ( count , i ) ; } count ++ ; if ( ! hash . containsKey ( 0 ) ) { hash . put ( 0 , new HashMap < Integer , Integer > ( ) ) ; } if ( ! hash . get ( 0 ) . containsKey ( count - 1 ) ) { hash . get ( 0 ) . put ( count - 1 , 1 ) ; } else { hash . get ( 0 ) . put ( count - 1 , 1 ) ; } } } } static int Gilbreath ( HashMap < Integer , HashMap < Integer , Integer > > mp , HashMap < Integer , HashMap < Integer , Integer > > hash , int n , int k ) { if ( hash . containsKey ( n ) && hash . get ( n ) . containsKey ( k ) && hash . get ( n ) . get ( k ) != 0 ) return mp . get ( n ) . get ( k ) ; int ans = Math . abs ( Gilbreath ( mp , hash , n - 1 , k + 1 ) - Gilbreath ( mp , hash , n - 1 , k ) ) ; if ( ! mp . containsKey ( n ) ) { mp . put ( n , new HashMap < Integer , Integer > ( ) ) ; } mp . get ( n ) . put ( k , ans ) ; return ans ; } static void solve ( int n ) { int i = 0 , j = 0 , count = 0 ; HashMap < Integer , HashMap < Integer , Integer > > mp = new HashMap < Integer , HashMap < Integer , Integer > > ( ) ; HashMap < Integer , HashMap < Integer , Integer > > hash = new HashMap < Integer , HashMap < Integer , Integer > > ( ) ; set_primes ( mp , hash , 100 ) ; while ( count < n ) { System . out . print ( Gilbreath ( mp , hash , i , j ) + " , ▁ " ) ; count ++ ; i -- ; j ++ ; if ( i < 0 ) { i = j ; j = 0 ; } } } public static void main ( String [ ] args ) { int n = 15 ; solve ( n ) ; } }
class GFG { static int longestAlternativeSequence ( int a [ ] , int n ) { int maxi1 = 0 ; int f1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( f1 % 2 != 0 ) { if ( a [ i ] % 2 == 1 ) { f1 = 1 ; maxi1 ++ ; } } else { if ( a [ i ] % 2 == 0 ) { maxi1 ++ ; f1 = 0 ; } } } int maxi2 = 0 ; int f2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( f2 % 2 != 0 ) { if ( a [ i ] % 2 == 1 ) { f2 = 1 ; maxi2 ++ ; } } else { if ( a [ i ] % 2 == 0 ) { maxi2 ++ ; f2 = 0 ; } } } return Math . max ( maxi1 , maxi2 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 13 , 16 , 8 , 9 , 32 , 10 } ; int n = a . length ; System . out . println ( longestAlternativeSequence ( a , n ) ) ; } }
class GFG { public static int Cells ( int n , int x ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( x % i == 0 && x / i <= n ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int n = 6 , x = 12 ; System . out . println ( Cells ( n , x ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void ZigZag ( int n ) { long [ ] fact = new long [ n + 1 ] ; long [ ] zig = new long [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) zig [ i ] = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; zig [ 0 ] = 1 ; zig [ 1 ] = 1 ; System . out . print ( " zig ▁ zag ▁ numbers : ▁ " ) ; System . out . print ( zig [ 0 ] + " ▁ " + zig [ 1 ] + " ▁ " ) ; for ( int i = 2 ; i < n ; i ++ ) { long sum = 0 ; for ( int k = 0 ; k <= i - 1 ; k ++ ) { sum += ( fact [ i - 1 ] / ( fact [ i - 1 - k ] * fact [ k ] ) ) * zig [ k ] * zig [ i - 1 - k ] ; } zig [ i ] = sum / 2 ; System . out . print ( sum / 2 + " ▁ " ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 10 ; ZigZag ( n ) ; } }
class GFG { static String nambiarNumber ( String str , int i ) { if ( i >= str . length ( ) ) return " " ; int firstDigit = ( str . charAt ( i ) - '0' ) ; int digitParity = firstDigit % 2 ; int sumDigits = 0 ; while ( i < str . length ( ) ) { sumDigits += ( str . charAt ( i ) - '0' ) ; int sumParity = sumDigits % 2 ; if ( digitParity != sumParity ) { break ; } i ++ ; } return ( " " + sumDigits + nambiarNumber ( str , i + 1 ) ) ; } public static void main ( String [ ] args ) { String str = "9880127431" ; System . out . println ( nambiarNumber ( str , 0 ) ) ; } }
import java . io . * ; class GFG { static float Depreciation ( float v , float r , float t ) { float D = ( float ) ( v * Math . pow ( ( 1 - r / 100 ) , t ) ) ; return D ; } public static void main ( String [ ] args ) { float V1 = 200 , R = 10 , T = 2 ; System . out . print ( Depreciation ( V1 , R , T ) ) ; } }
import java . util . * ; import java . lang . Math ; import java . io . * ; class GFG { static int nextPowerOfFour ( int n ) { int x = ( int ) Math . floor ( Math . sqrt ( Math . sqrt ( n ) ) ) ; if ( Math . pow ( x , 4 ) == n ) return n ; else { x = x + 1 ; return ( int ) Math . pow ( x , 4 ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 122 ; System . out . println ( nextPowerOfFour ( n ) ) ; } }
import java . util . * ; class GFG { static int phi ( int n ) { for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result -= result / p ; } } if ( n > 1 ) result -= result / n ; return result ; } static void Highly_Totient ( int n ) { int count = 0 , p_count = - 1 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 1 ; i < 100000 ; i ++ ) { if ( mp . containsKey ( phi ( i ) ) ) { mp . put ( phi ( i ) , mp . get ( phi ( i ) ) + 1 ) ; } else { mp . put ( phi ( i ) , 1 ) ; } } int i = 1 ; while ( count < n ) { if ( mp . containsKey ( i ) && mp . get ( i ) > p_count ) { System . out . print ( i ) ; if ( count < n - 1 ) System . out . print ( " , ▁ " ) ; p_count = mp . get ( i ) ; count ++ ; } i ++ ; } } public static void main ( String [ ] args ) { int n = 20 ; Highly_Totient ( n ) ; } }
import java . util . * ; class GFG { static int find_count ( Vector < Integer > ele ) { int count = 0 ; for ( int i = 0 ; i < ele . size ( ) ; i ++ ) { Vector < Integer > p = new Vector < Integer > ( ) ; int c = 0 ; for ( int j = ele . size ( ) - 1 ; j >= ( ele . size ( ) - 1 - i ) && j >= 0 ; j -- ) { p . add ( ele . get ( j ) ) ; } int j = ele . size ( ) - 1 , k = 0 ; while ( j >= 0 ) { if ( ele . get ( j ) != p . get ( k ) ) { break ; } j -- ; k ++ ; if ( k == p . size ( ) ) { c ++ ; k = 0 ; } } count = Math . max ( count , c ) ; } return count ; } static void solve ( int n ) { int count = 1 ; Vector < Integer > ele = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( count + " , ▁ " ) ; ele . add ( count ) ; count = find_count ( ele ) ; } } public static void main ( String [ ] args ) { int n = 10 ; solve ( n ) ; } }
class GFG { static int speedOfTrain ( float X , float Y ) { float Speed ; Speed = 1188 * ( ( X - Y ) / Y ) ; return ( int ) Speed ; } public static void main ( String [ ] args ) { float X = 8f , Y = 7.2f ; int result = ( speedOfTrain ( X , Y ) ) ; System . out . println ( result + " ▁ km / hr " ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int Division ( int a [ ] , int n ) { int maxi = Integer . MIN_VALUE , mini = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { maxi = Math . max ( a [ i ] , maxi ) ; mini = Math . min ( a [ i ] , mini ) ; } return maxi / mini ; } public static void main ( String [ ] args ) throws java . lang . Exception { int a [ ] = { 3 , 7 , 9 , 3 , 11 } ; int n = a . length ; System . out . print ( Division ( a , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000000 ; static Vector < Integer > addPrimes ( ) { int n = MAX ; boolean [ ] prime = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) ans . add ( p ) ; return ans ; } static int pi ( int x , Vector < Integer > v ) { int l = 0 , r = v . size ( ) - 1 , m , in = - 1 ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( v . get ( m ) <= x ) { in = m ; l = m + 1 ; } else { r = m - 1 ; } } return in + 1 ; } static int Ramanujan ( int n , Vector < Integer > v ) { int upperbound = ( int ) ( 4 * n * ( Math . log ( 4 * n ) / Math . log ( 2 ) ) ) ; for ( int i = upperbound ; ; i -- ) { if ( pi ( i , v ) - pi ( i / 2 , v ) < n ) return 1 + i ; } } static void Ramanujan_Numbers ( int n ) { int c = 1 ; Vector < Integer > v = addPrimes ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( Ramanujan ( i , v ) ) ; if ( i != n ) System . out . print ( " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; Ramanujan_Numbers ( n ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MAX = 130 ; static boolean validQuadruple ( int arr [ ] , int n ) { if ( n >= MAX ) return true ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) for ( int l = k + 1 ; l < n ; l ++ ) { if ( ( arr [ i ] ^ arr [ j ] ^ arr [ k ] ^ arr [ l ] ) == 0 ) { return true ; } } return false ; } public static void main ( String [ ] args ) throws java . lang . Exception { int arr [ ] = { 1 , 0 , 2 , 3 , 7 } ; int n = arr . length ; if ( validQuadruple ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int NumberOfWays ( int X , int Y , int M , int N ) { return fact ( X + Y ) * nCr ( M , X ) * nCr ( N , Y ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int X = 2 , Y = 2 , M = 3 , N = 3 ; System . out . println ( NumberOfWays ( X , Y , M , N ) ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Integer > store = new HashMap < Integer , Integer > ( ) ; static int Wedderburn ( int n ) { if ( n <= 2 ) return store . get ( n ) ; else if ( n % 2 == 0 ) { int x = n / 2 , ans = 0 ; for ( int i = 1 ; i < x ; i ++ ) { ans += store . get ( i ) * store . get ( n - i ) ; } ans += ( store . get ( x ) * ( store . get ( x ) + 1 ) ) / 2 ; store . put ( n , ans ) ; return ans ; } else { int x = ( n + 1 ) / 2 , ans = 0 ; for ( int i = 1 ; i < x ; i ++ ) { ans += store . get ( i ) * store . get ( n - i ) ; } store . put ( n , ans ) ; return ans ; } } static void Wedderburn_Etherington ( int n ) { store . put ( 0 , 0 ) ; store . put ( 1 , 1 ) ; store . put ( 2 , 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( Wedderburn ( i ) ) ; if ( i != n - 1 ) System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; Wedderburn_Etherington ( n ) ; } }
import java . util . * ; class GFG { static int fact ( int n ) { int i = 1 , fac = 1 ; for ( i = 1 ; i <= n ; i ++ ) fac = fac * i ; return fac ; } static double log_Expansion ( double x , int n ) { double sum = 0 ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + Math . pow ( x , 2 * i ) / fact ( 2 * i ) ; } return sum ; } public static void main ( String [ ] args ) { double x = 1 ; int n = 10 ; System . out . println ( log_Expansion ( x , n ) ) ; } }
class GFG { static int countElements ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; float mean = ( float ) sum / n ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float newMean = ( float ) ( sum - arr [ i ] ) / ( n - 1 ) ; if ( newMean == mean ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countElements ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 10000 ; static int MAX_ELEMENT = 50 ; static int [ ] [ ] [ ] dp = new int [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; static int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != - 1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = Math . max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; for ( int i = 0 ; i < MAX_ELEMENT ; i ++ ) { for ( int j = 0 ; j < MAX_ELEMENT ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } System . out . println ( Max_Xor ( arr , 0 , k , 0 , n ) ) ; } }
import java . util . * ; class GFG { static boolean Prime ( int n ) { for ( int j = 2 ; j <= Math . sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } static int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= Math . max ( Math . sqrt ( n ) , Math . sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; System . out . println ( find_prime ( x , xsqmodp , y , ysqmodp ) ) ; } }
import java . util . * ; class GFG { static int numberOfMinutes ( int S , int S1 ) { int Min = 0 ; Min = ( int ) ( ( ( S - S1 ) / Math . floor ( S ) ) * 60 ) ; return Min ; } public static void main ( String [ ] args ) { int S = 30 , S1 = 10 ; System . out . println ( numberOfMinutes ( S , S1 ) + " ▁ min " ) ; } }
class GFG { static int fac ( int num ) { if ( num == 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } static void Tanx_expansion ( int terms , int x ) { double sum = 0 ; for ( int i = 1 ; i <= terms ; i += 1 ) { double B = 0 ; int Bn = 2 * i ; for ( int k = 0 ; k <= Bn ; k ++ ) { double temp = 0 ; for ( int r = 0 ; r <= k ; r ++ ) temp = temp + Math . pow ( - 1 , r ) * fac ( k ) * Math . pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( ( double ) ( k + 1 ) ) ; } sum = sum + Math . pow ( - 4 , i ) * ( 1 - Math . pow ( 4 , i ) ) * B * Math . pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } System . out . printf ( " % .9f " , sum ) ; } public static void main ( String [ ] args ) { int n = 6 , x = 1 ; Tanx_expansion ( n , x ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int minimum_elements ( int n , int arr [ ] ) { if ( n < 3 ) return 0 ; int g , ans = 0 , diff , cnt ; diff = arr [ 1 ] - arr [ 0 ] ; g = diff ; for ( int i = 2 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; g = gcd ( g , diff ) ; } for ( int i = 1 ; i < n ; i ++ ) { diff = arr [ i ] - arr [ i - 1 ] ; cnt = diff / g ; ans += ( cnt - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 10 , 12 , 16 } ; int n = arr . length ; System . out . println ( minimum_elements ( n , arr ) ) ; } }
class GFG { static int minOperations ( int x , int y , int p , int q ) { if ( y % x != 0 ) return - 1 ; int d = y / x ; int a = 0 ; while ( d % p == 0 ) { d /= p ; a ++ ; } int b = 0 ; while ( d % q == 0 ) { d /= q ; b ++ ; } if ( d != 1 ) return - 1 ; return ( a + b ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 2592 , p = 2 , q = 3 ; System . out . println ( minOperations ( x , y , p , q ) ) ; } }
class GFG { static boolean checkcomposite ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return true ; } } return false ; } static int power ( int x , int y , int mod ) { int res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) { res = ( res * x ) % mod ; } y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int Check ( int n , int a ) { if ( a > 1 && checkcomposite ( n ) && power ( a , n - 1 , n ) == 1 ) { return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 645 ; int a = 2 ; System . out . println ( Check ( N , a ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static void Digits ( int n ) { int largest = 0 ; int smallest = 9 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } System . out . println ( largest + " ▁ " + smallest ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 2346 ; Digits ( n ) ; } }
class GFG { public static int factorial ( int n ) { if ( n <= 1 ) return 1 ; return n * factorial ( n - 1 ) ; } public static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } public static float Probability ( int M , int N , int X ) { return ( float ) ( nCr ( M - N - 1 , X - 2 ) / ( nCr ( M - 1 , X - 1 ) * 1.0 ) ) ; } public static void main ( String [ ] args ) { int M = 9 , X = 3 , N = 4 ; System . out . println ( Probability ( M , N , X ) ) ; } }
class GFG { static int fact ( int n ) { if ( n <= 1 ) return 1 ; return n * fact ( n - 1 ) ; } static int nPr ( int n , int r ) { return fact ( n ) / fact ( n - r ) ; } static int countWords ( int N , int M ) { return ( int ) Math . pow ( N , M ) - nPr ( N , M ) ; } public static void main ( String [ ] args ) { int N = 10 , M = 5 ; System . out . print ( countWords ( N , M ) ) ; } }
import java . io . * ; class GFG { static void MinimumValue ( int x , int y ) { if ( x > y ) { int temp = x ; x = y ; y = temp ; } int a = 1 ; int b = x - 1 ; int c = y - b ; System . out . print ( a + " ▁ " + b + " ▁ " + c ) ; } public static void main ( String [ ] args ) { int x = 123 , y = 13 ; MinimumValue ( x , y ) ; } }
import java . io . * ; class GFG { static void find_length ( int s1 , int s2 ) { if ( s1 <= 0 s2 <= 0 ) { System . out . print ( - 1 ) ; return ; } int max_length = s1 + s2 - 1 ; int min_length = Math . max ( s1 , s2 ) - Math . min ( s1 , s2 ) + 1 ; if ( min_length > max_length ) { System . out . print ( - 1 ) ; return ; } System . out . println ( " Max ▁ = ▁ " + max_length ) ; System . out . print ( " Min ▁ = ▁ " + min_length ) ; } public static void main ( String [ ] args ) { int s1 = 8 , s2 = 5 ; find_length ( s1 , s2 ) ; } }
class geeks { public static int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( firstSetBit ( n ) ) ; } }
import java . util . HashMap ; class geeks { public static int nCr ( int n , int r ) { int res = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { res *= ( n - r + i ) ; res /= i ; } return res ; } public static int NoofSequences ( int N , int M ) { HashMap < Integer , Integer > prime = new HashMap < > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( M ) ; i ++ ) { while ( M % i == 0 ) { if ( prime . get ( i ) == null ) prime . put ( i , 1 ) ; else { int x = prime . get ( i ) ; prime . put ( i , ++ x ) ; } M /= i ; } } if ( M > 1 ) { if ( prime . get ( M ) != null ) { int x = prime . get ( M ) ; prime . put ( M , ++ x ) ; } else prime . put ( M , 1 ) ; } int ans = 1 ; for ( HashMap . Entry < Integer , Integer > entry : prime . entrySet ( ) ) { ans *= ( nCr ( N + entry . getValue ( ) - 1 , N - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , M = 6 ; System . out . println ( NoofSequences ( N , M ) ) ; } }
import java . util . * ; class GFG { static int nCr ( int n ) { if ( n < 4 ) return 0 ; int answer = n * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) ; answer /= 24 ; return answer ; } static int countQuadruples ( int N , int K ) { int M = N / K ; int answer = nCr ( M ) ; for ( int i = 2 ; i < M ; i ++ ) { int j = i ; int temp2 = M / i ; int count = 0 ; int check = 0 ; int temp = j ; while ( j % 2 == 0 ) { count ++ ; j /= 2 ; if ( count >= 2 ) break ; } if ( count >= 2 ) { check = 1 ; } for ( int k = 3 ; k <= Math . sqrt ( temp ) ; k += 2 ) { int cnt = 0 ; while ( j % k == 0 ) { cnt ++ ; j /= k ; if ( cnt >= 2 ) break ; } if ( cnt >= 2 ) { check = 1 ; break ; } else if ( cnt == 1 ) count ++ ; } if ( j > 2 ) { count ++ ; } if ( check == 1 ) continue ; else { if ( count % 2 == 1 ) { answer -= nCr ( temp2 ) ; } else { answer += nCr ( temp2 ) ; } } } return answer ; } public static void main ( String [ ] args ) { int N = 10 , K = 2 ; System . out . println ( countQuadruples ( N , K ) ) ; } }
import java . util . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int NoOfDistributions ( int N , int R ) { return ncr ( N + R - 1 , R - 1 ) ; } public static void main ( String [ ] args ) { int N = 4 , R = 3 ; System . out . println ( NoOfDistributions ( N , R ) ) ; } }
import java . io . * ; class GFG { static boolean checkPerfectcube ( int n ) { int d = ( int ) Math . cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int smallestPerfectCube ( int a [ ] , int n ) { int mini = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkPerfectcube ( a [ i ] ) ) { mini = Math . min ( a [ i ] , mini ) ; } } return mini ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 8 , 25 , 2 , 3 , 10 } ; int n = a . length ; System . out . print ( smallestPerfectCube ( a , n ) ) ; } }
import java . io . * ; class GFG { static void CheckSubarray ( int arr [ ] , int N ) { int presum [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 1 ; i <= N ; i += 1 ) { for ( int j = i ; j <= N ; j += 1 ) { if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) { System . out . print ( ( i - 1 ) + " ▁ " + ( j - 1 ) ) ; return ; } } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 5 , 3 , 7 } ; int N = arr . length ; CheckSubarray ( arr , N ) ; } }
import java . util . * ; class GFG { static void CheckSubarray ( int arr [ ] , int N ) { int [ ] presum = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } Vector < Integer > [ ] moduloclass = new Vector [ N ] ; for ( int i = 0 ; i < N ; i += 1 ) { moduloclass [ i ] = new Vector < > ( ) ; } for ( int i = 1 ; i <= N ; i += 1 ) { moduloclass [ presum [ i ] % N ] . add ( i - 1 ) ; } if ( moduloclass [ 0 ] . size ( ) > 0 ) { System . out . print ( 0 + " ▁ " + moduloclass [ 0 ] . get ( 0 ) ) ; return ; } for ( int i = 1 ; i < N ; i += 1 ) { if ( moduloclass [ i ] . size ( ) >= 2 ) { System . out . print ( moduloclass [ i ] . get ( 0 ) + 1 + " ▁ " + moduloclass [ i ] . get ( 1 ) ) ; return ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 3 , 5 , 2 } ; int N = arr . length ; CheckSubarray ( arr , N ) ; } }
import java . util . * ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } static int findNearestNumber ( int n ) { int cnt = countBits ( n ) ; cnt -= 1 ; if ( cnt % 2 == 1 ) { return 1 << ( cnt + 1 ) ; } else { int tempnum = 0 ; for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum += 1 << i ; } if ( tempnum < n ) { return ( 1 << ( cnt + 2 ) ) ; } else if ( tempnum == n ) { return n ; } for ( int i = 0 ; i <= cnt ; i += 2 ) { tempnum -= ( 1 << i ) ; if ( tempnum < n ) { return tempnum += ( 1 << i ) ; } } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int n = 19 ; System . out . println ( findNearestNumber ( n ) ) ; } }
import java . io . * ; class GFG { static int getX ( int a , int b , int c , int d ) { int X = ( b * c - a * d ) / ( d - c ) ; return X ; } public static void main ( String [ ] args ) { int a = 2 , b = 3 , c = 4 , d = 5 ; System . out . println ( getX ( a , b , c , d ) ) ; } }
import java . io . * ; class GFG { static int countNum ( int n ) { if ( n == 1 ) return 1 ; int count = ( int ) Math . pow ( 2 , n - 2 ) ; return count ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countNum ( n ) ) ; } }
import java . util . * ; class GFG { static String constructString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static int findNumber ( int n ) { String s = constructString ( n - 1 ) ; int num = binaryToDecimal ( s ) ; return num ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( findNumber ( n ) ) ; } }
class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int data ) { Node new_node = new Node ( ) ; new_node . data = data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int bitonic_point ( Node node ) { if ( node == null ) return - 1 ; if ( node . next == null ) return - 1 ; if ( node . data > node . next . data ) return - 1 ; while ( node . next != null ) { if ( node . data > node . next . data ) break ; node = node . next ; } int bitonicPoint = node . data ; while ( node . next != null ) { if ( node . data < node . next . data ) return - 1 ; node = node . next ; } return bitonicPoint ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 100 ) ; head = push ( head , 201 ) ; head = push ( head , 399 ) ; head = push ( head , 490 ) ; head = push ( head , 377 ) ; head = push ( head , 291 ) ; head = push ( head , 100 ) ; System . out . println ( bitonic_point ( head ) ) ; } }
class GFG { static boolean canConvert ( int a , int b ) { while ( b > a ) { if ( b % 10 == 1 ) { b /= 10 ; continue ; } if ( b % 2 == 0 ) { b /= 2 ; continue ; } return false ; } if ( b == a ) return true ; return false ; } public static void main ( String [ ] args ) { int A = 2 , B = 82 ; if ( canConvert ( A , B ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } static void nMosaicNumbers ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( mosaic ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 ; nMosaicNumbers ( n ) ; } }
import java . util . * ; class GFG { static void countNodes ( int N , int X ) { int height = ( int ) Math . floor ( Math . log ( N ) / Math . log ( 2 ) ) ; if ( X > height ) { System . out . println ( "0 0 "); return ; } if ( N == 1 ) { System . out . println ( "1 1 "); return ; } int max_total_nodes = ( 1 << ( height + 1 ) ) - 1 ; int nodes_last_level = ( 1 << height ) - ( max_total_nodes - N ) ; int from_root ; int from_leaf ; if ( X == height ) from_root = nodes_last_level ; else from_root = 1 << X ; int left_leaf_nodes = ( ( 1 << height ) - nodes_last_level ) / 2 ; if ( X == 0 ) { from_leaf = nodes_last_level + left_leaf_nodes ; } else { int i = X ; while ( nodes_last_level > 1 && i > 0 ) { nodes_last_level = ( int ) Math . ceil ( ( float ) nodes_last_level / ( float ) 2 ) ; i -- ; } from_leaf = nodes_last_level ; i = X ; while ( left_leaf_nodes > 1 && i > 0 ) { left_leaf_nodes = ( int ) Math . ceil ( ( float ) left_leaf_nodes / ( float ) 2 ) ; i -- ; } from_leaf += left_leaf_nodes ; } System . out . println ( from_root + "NEW_LINE" + from_leaf); } public static void main ( String [ ] args ) { int N = 38 , X = 3 ; countNodes ( N , X ) ; } }
import java . io . * ; class GFG { static int sum_of_4_squares ( int n ) { int i , odd = 0 , even = 0 ; for ( i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % 2 == 0 ) even += i ; else odd += i ; if ( ( n / i ) != i ) { if ( ( n / i ) % 2 == 0 ) even += ( n / i ) ; else odd += ( n / i ) ; } } } if ( n % 2 == 1 ) return 8 * ( odd + even ) ; else return 24 * ( odd ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( sum_of_4_squares ( n ) ) ; } }
import java . io . * ; class GFG { static int mosaic ( int n ) { int i , ans = 1 ; for ( i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 && n > 0 ) { int count = 0 ; while ( n % i == 0 ) { n /= i ; count ++ ; } ans *= count * i ; } } return ans ; } public static void main ( String [ ] args ) { int n = 36 ; System . out . println ( mosaic ( n ) ) ; } }
import java . io . * ; class GFG { static int Total_Ways ( int n ) { int fac = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) { fac = fac * i ; } return ( fac * 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( Total_Ways ( n ) ) ; } }
import java . io . * ; class GFG { static int MaxNumbers ( int a [ ] , int n ) { int [ ] fre = { 0 , 0 , 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } int ans = fre [ 0 ] ; int k = Math . min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += fre [ 1 ] / 3 + fre [ 2 ] / 3 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 } ; int n = a . length ; System . out . println ( MaxNumbers ( a , n ) ) ; } }
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int pairs ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int sum = countSetBits ( arr [ i ] ) + countSetBits ( arr [ j ] ) ; if ( sum == k ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 4 ; System . out . println ( pairs ( arr , n , k ) ) ; } }
class GFG { static void UpperHessenbergMatrix ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i > j + 1 ) { System . out . print ( 0 + " ▁ " ) ; } else { System . out . print ( ( int ) ( Math . random ( ) * 10 ) + " ▁ " ) ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; UpperHessenbergMatrix ( n ) ; } }
import java . util . * ; class Gfg { public static int composite_factors ( int n ) { int count = 0 ; int i , j ; int [ ] a = new int [ n + 1 ] ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = 0 ; } for ( i = 1 ; i <= n ; ++ i ) { if ( n % i == 0 ) { a [ i ] = i ; } } for ( i = 2 ; i <= n ; i ++ ) { j = 2 ; int p = 1 ; while ( j < a [ i ] ) { if ( a [ i ] % j == 0 ) { p = 0 ; break ; } j ++ ; } if ( p == 0 && a [ i ] != 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( composite_factors ( n ) ) ; } }
class GFG { static int sumOfFactors ( int n ) { int sum = 0 ; for ( int f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } static int getSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 17 , 6 , 10 , 6 , 4 } ; int n = arr . length ; System . out . print ( getSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static boolean sameLength ( int A , int B ) { while ( ( A > 0 ) && ( B > 0 ) ) { A = A / 10 ; B = B / 10 ; } if ( ( A == 0 ) && ( B == 0 ) ) return true ; return false ; } public static void main ( String [ ] args ) { int A = 21 , B = 1 ; if ( sameLength ( A , B ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int N = 2 ; static int M = 2 ; static void Matrix ( char a [ ] [ ] ) { char ch = '1' ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == ' . ' ) a [ i ] [ j ] = ch ; ch = ( ch == '1' ) ? '0' : '1' ; System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; if ( i % 2 == 0 ) ch = '0' ; else ch = '1' ; } } public static void main ( String [ ] args ) { char a [ ] [ ] = { { ' . ' , ' _ ' } , { ' _ ' , ' . ' } } ; Matrix ( a ) ; } }
class GFG { static int FindXorSum ( int arr [ ] , int k , int n ) { if ( n < k ) return 0 ; int [ ] x = new int [ n ] ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) x [ i ] = x [ i - 1 ] ^ arr [ i ] ; else x [ i ] = arr [ i ] ; if ( i >= k - 1 ) { int sum = 0 ; sum = x [ i ] ; if ( i - k > - 1 ) sum ^= x [ i - k ] ; result += sum ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 2 ; System . out . println ( FindXorSum ( arr , k , n ) ) ; } }
class GFG { static int reverse ( int n ) { int rev = 0 ; while ( n > 0 ) { int d = n % 10 ; rev = rev * 10 + d ; n = n / 10 ; } return rev ; } static boolean isPalin ( int n ) { return ( n == reverse ( n ) ) ; } static int countDigits ( int n ) { int c = 0 ; while ( n > 0 ) { n = n / 10 ; c ++ ; } return c ; } static int countPalinDigits ( int [ ] arr , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 10 || isPalin ( arr [ i ] ) ) { s += countDigits ( arr [ i ] ) ; } } return s ; } public static void main ( String [ ] args ) { int [ ] arr = { 121 , 56 , 434 } ; int n = arr . length ; System . out . println ( countPalinDigits ( arr , n ) ) ; } }
class GFG { static int reverse ( int n ) { int d = 0 , s = 0 ; while ( n > 0 ) { d = n % 10 ; s = s * 10 + d ; n = n / 10 ; } return s ; } static boolean isPalin ( int n ) { return n == reverse ( n ) ; } static int sumOfArray ( int [ ] arr , int n ) { int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] > 10 ) && isPalin ( arr [ i ] ) ) { s += arr [ i ] ; } } return s ; } public static void main ( String [ ] args ) { int n = 6 ; int [ ] arr = { 12 , 313 , 11 , 44 , 9 , 1 } ; System . out . println ( sumOfArray ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' ch == ' e ' ch == ' i ' ch == ' o ' ch == ' u ' ) return true ; else return false ; } static long fact ( long n ) { if ( n < 2 ) { return 1 ; } return n * fact ( n - 1 ) ; } static long only_vowels ( HashMap < Character , Integer > freq ) { long denom = 1 ; long cnt_vwl = 0 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { if ( isVowel ( itr . getKey ( ) ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt_vwl += itr . getValue ( ) ; } } return fact ( cnt_vwl ) / denom ; } static long all_vowels_together ( HashMap < Character , Integer > freq ) { long vow = only_vowels ( freq ) ; long denom = 1 ; long cnt_cnst = 0 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { if ( ! isVowel ( itr . getKey ( ) ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt_cnst += itr . getValue ( ) ; } } long ans = fact ( cnt_cnst + 1 ) / denom ; return ( ans * vow ) ; } static long total_permutations ( HashMap < Character , Integer > freq ) { long cnt = 0 ; long denom = 1 ; for ( Map . Entry < Character , Integer > itr : freq . entrySet ( ) ) { denom *= fact ( itr . getValue ( ) ) ; cnt += itr . getValue ( ) ; } return fact ( cnt ) / denom ; } static long no_vowels_together ( String word ) { HashMap < Character , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < word . length ( ) ; i ++ ) { char ch = Character . toLowerCase ( word . charAt ( i ) ) ; if ( freq . containsKey ( ch ) ) { freq . put ( ch , freq . get ( ch ) + 1 ) ; } else { freq . put ( ch , 1 ) ; } } long total = total_permutations ( freq ) ; long vwl_tgthr = all_vowels_together ( freq ) ; long res = total - vwl_tgthr ; return res ; } public static void main ( String [ ] args ) { String word = " allahabad " ; long ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; word = " geeksforgeeks " ; ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; word = " abcd " ; ans = no_vowels_together ( word ) ; System . out . println ( ans ) ; } }
import java . io . * ; class GFG { static boolean isValidSeq ( int [ ] a , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 a [ i ] > nodes ) return false ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; if ( isValidSeq ( a , n ) ) System . out . println ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }
import java . text . * ; class GFG { static double p = 1 , f = 1 ; static double e ( int x , int n ) { double r ; if ( n == 0 ) return 1 ; r = e ( x , n - 1 ) ; p = p * x ; f = f * n ; return ( r + p / f ) ; } public static void main ( String [ ] args ) { int x = 4 , n = 15 ; DecimalFormat df = new DecimalFormat ( "0 . # # # # # # " ) ; System . out . println ( df . format ( e ( x , n ) ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000000 ; static int [ ] Sieve = new int [ MAX + 1 ] ; static void constructSieve ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( int j = 2 * i ; j <= MAX ; j += i ) { int temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = temp / i ; } } } } } static void checkElements ( int A [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { System . out . println ( " YES " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { constructSieve ( ) ; int k = 3 ; int A [ ] = { 12 , 36 , 42 , 72 } ; int n = A . length ; checkElements ( A , n , k ) ; } }
class GFG { static int mod = 1000000007 ; static long modularexpo ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static void multiply ( long F [ ] [ ] , long M [ ] [ ] , long m ) { long x = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long y = ( ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; long z = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) % m ) % m ; long w = ( ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] ) % m + ( F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) % m ) % m ; F [ 0 ] [ 0 ] = x ; F [ 0 ] [ 1 ] = y ; F [ 1 ] [ 0 ] = z ; F [ 1 ] [ 1 ] = w ; } static void power ( long F [ ] [ ] , long n , long m ) { if ( n == 0 n == 1 ) return ; long M [ ] [ ] = { { 1 , 1 } , { 1 , 0 } } ; power ( F , n / 2 , m ) ; multiply ( F , F , m ) ; if ( n % 2 != 0 ) multiply ( F , M , m ) ; } static long fib ( long n , long m ) { long F [ ] [ ] = { { 1 , 1 } , { 1 , 0 } } ; if ( n == 0 ) return 0 ; power ( F , n - 1 , m ) ; return F [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { long n = 4 ; long base = fib ( n , mod ) % mod ; long expo = fib ( n , mod - 1 ) % ( mod - 1 ) ; long result = modularexpo ( base , expo , mod ) % mod ; System . out . println ( result ) ; } }
class GFG { static int cntWays ( int arr [ ] , int n ) { if ( n == 1 ) { return 0 ; } int tot_sum = 0 , sum = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tot_sum += arr [ i ] ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . length ; System . out . println ( cntWays ( arr , n ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 32 ; static long [ ] pow2 = new long [ MAX ] ; static boolean [ ] visited = new boolean [ MAX ] ; static Vector < Long > ans = new Vector < > ( ) ; static void power_2 ( ) { long ans = 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } static void add ( long num ) { int point = 0 ; long value = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num % 2 == 1 ) { value += ( 1 << i ) ; } num /= 2 ; } } ans . add ( value ) ; } static void solve ( long n , long k ) { ans . add ( k ) ; int countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { System . out . print ( - 1 ) ; return ; } int count = 0 ; for ( long i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { long n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ; } }
import java . util . * ; class GFG { static int numberOfMen ( int D , int m , int d ) { int Men = ( m * ( D - d ) ) / d ; return Men ; } public static void main ( String args [ ] ) { int D = 5 , m = 4 , d = 4 ; System . out . println ( numberOfMen ( D , m , d ) ) ; } }
class GFG { static int countX ( int a , int b ) { if ( b > a ) return 0 ; else if ( a == b ) return - 1 ; else { int x = a - b , ans = 0 ; for ( int i = 1 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { int d1 = i , d2 = b - 1 ; if ( i * i != x ) d2 = x / i ; if ( d1 > b ) ans ++ ; if ( d2 > b ) ans ++ ; } } return ans ; } } static public void main ( String args [ ] ) { int a = 21 , b = 5 ; System . out . println ( countX ( a , b ) ) ; } }
import java . io . * ; class GFG { static long sumPowersK ( long n , long k ) { long sum = 0 , num = 1 ; while ( num <= n ) { sum += num ; num *= k ; } return sum ; } static long getSum ( long n , long k ) { long pwrK = sumPowersK ( n , k ) ; long sumAll = ( n * ( n + 1 ) ) / 2 ; return ( sumAll - pwrK ) ; } public static void main ( String [ ] args ) { long n = 10 , k = 3 ; System . out . println ( getSum ( n , k ) ) ; } }
import java . util . * ; class GFG { static int maxPeople ( int p ) { int tmp = 0 , count = 0 ; for ( int i = 1 ; i * i <= p ; i ++ ) { tmp = tmp + ( i * i ) ; if ( tmp <= p ) count ++ ; else break ; } return count ; } public static void main ( String args [ ] ) { int p = 14 ; System . out . println ( maxPeople ( p ) ) ; } }
class GFG { static int kN = 1000000 ; static int maxPeople ( int p ) { long [ ] sums = new long [ kN ] ; sums [ 0 ] = 0 ; for ( int i = 1 ; i < kN ; i ++ ) sums [ i ] = ( long ) ( i * i ) + sums [ i - 1 ] ; int it = lower_bound ( sums , 0 , kN , p ) ; if ( sums [ it ] > p ) { -- it ; } return it ; } private static int lower_bound ( long [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void main ( String [ ] args ) { int p = 14 ; System . out . println ( maxPeople ( p ) ) ; } }
class GFG { static int getAverage ( int x , int y ) { int avg = ( x & y ) + ( ( x ^ y ) >> 1 ) ; return avg ; } public static void main ( String [ ] args ) { int x = 10 , y = 9 ; System . out . print ( getAverage ( x , y ) ) ; } }
class GFG { static int MAXN = 60 ; static int solveQuery ( int L , int X ) { int res ; int [ ] a = new int [ MAXN ] ; int [ ] b = new int [ L ] ; int refer = X , size_a = 0 ; while ( refer > 0 ) { a [ size_a ] = refer % 2 ; refer /= 2 ; size_a ++ ; } for ( int i = 0 ; i < Math . min ( size_a , L ) ; i ++ ) { if ( a [ i ] == 1 ) b [ i ] = 0 ; else b [ i ] = 1 ; } for ( int i = Math . min ( size_a , L ) ; i < L ; i ++ ) b [ i ] = 1 ; b [ L - 1 ] = 1 ; int temp = 0 , p = 1 ; for ( int i = 0 ; i < L ; i ++ ) { temp += b [ i ] * p ; p *= 2 ; } res = temp ^ X ; return res ; } static public void main ( String args [ ] ) { int [ ] [ ] queries = { { 2 , 5 } , { 3 , 15 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( solveQuery ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } }
import java . io . * ; class GFG { static boolean solve ( int n , int k ) { if ( n >= ( k * ( k + 1 ) ) / 2 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 12 , k = 4 ; if ( solve ( n , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; import java . io . * ; class GFG { static int closetAND ( int arr [ ] , int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 10 } ; int n = arr . length ; int k = 2 ; System . out . println ( closetAND ( arr , n , k ) ) ; } }
class GFG { static int Rate ( int N1 , int N2 ) { float rate = ( N2 - N1 ) * 100 / N1 ; return ( int ) rate ; } public static void main ( String [ ] args ) { int N1 = 100 , N2 = 120 ; System . out . println ( Rate ( N1 , N2 ) + " ▁ % " ) ; } }
import java . util . * ; class GFG { static int getPrime ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; Vector < Boolean > prime = new Vector < > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . add ( i , Boolean . TRUE ) ; prime . add ( 1 , Boolean . FALSE ) ; prime . add ( 2 , Boolean . FALSE ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . add ( i , Boolean . FALSE ) ; } } int maximum = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . get ( arr [ i ] ) ) { maximum = Math . max ( maximum , arr [ i ] ) ; } } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = arr . length ; System . out . println ( getPrime ( arr , n ) ) ; } }
class GFG { static long getMinNum ( int N ) { int lcm = 1 ; for ( int i = 2 ; i <= N ; i ++ ) lcm = ( ( i * lcm ) / ( __gcd ( i , lcm ) ) ) ; return ( lcm + 1 ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( getMinNum ( N ) ) ; } }
class GFG { public static double maxEdges ( double N ) { double edges = 0 ; edges = Math . floor ( ( N * N ) / 4 ) ; return edges ; } public static void main ( String [ ] args ) { double N = 5 ; System . out . println ( maxEdges ( N ) ) ; } }
class GFG { static boolean isSafe ( int N , int M , char [ ] str ) { int coll = 0 , colr = 0 , rowu = 0 , rowd = 0 ; for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] == ' L ' ) { coll ++ ; if ( colr > 0 ) { colr -- ; } if ( coll == M ) { break ; } } else if ( str [ i ] == ' R ' ) { colr ++ ; if ( coll > 0 ) { coll -- ; } if ( colr == M ) { break ; } } else if ( str [ i ] == ' U ' ) { rowu ++ ; if ( rowd > 0 ) { rowd -- ; } if ( rowu == N ) { break ; } } else if ( str [ i ] == ' D ' ) { rowd ++ ; if ( rowu > 0 ) { rowu -- ; } if ( rowd == N ) { break ; } } } if ( Math . abs ( rowd ) < N && Math . abs ( rowu ) < N && Math . abs ( coll ) < M && Math . abs ( colr ) < M ) { return true ; } return false ; } public static void main ( String [ ] args ) { int N = 1 , M = 1 ; String str = " R " ; if ( isSafe ( N , M , str . toCharArray ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void Permutation ( int n , int k ) { int [ ] p = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) p [ i ] = i ; for ( int i = 1 ; i < n - k ; i ++ ) p [ i + 1 ] = i ; p [ 1 ] = n - k ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( p [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; Permutation ( n , k ) ; } }
class GFG { static int countOnes ( int arr [ ] , int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i ; j <= N ; j ++ ) { if ( j % i == 0 ) { if ( arr [ j - 1 ] == 0 ) { } else { } } } } int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) { } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ 10 ] ; int ans = countOnes ( arr , N ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static void count_unique ( HashSet < Integer > s , int n ) { if ( s . contains ( n ) ) return ; s . add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void main ( String [ ] args ) { int n = 10 ; HashSet < Integer > s = new HashSet < > ( ) ; count_unique ( s , n ) ; System . out . println ( s . size ( ) ) ; } }
class GFG { static int maxBitElement ( int arr [ ] , int n ) { int num = 0 , max = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = Integer . bitCount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 } ; int n = arr . length ; System . out . print ( maxBitElement ( arr , n ) ) ; } }
class GFG { static int smallestSum ( int n ) { if ( n == 1 ) return 0 ; return ( 2 * ( int ) Math . pow ( 10 , n - 1 ) ) ; } static int largestSum ( int n ) { return ( 2 * ( ( int ) Math . pow ( 10 , n ) - 1 ) ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Largest ▁ = ▁ " + largestSum ( n ) ) ; System . out . print ( " Smallest ▁ = ▁ " + smallestSum ( n ) ) ; } }
class GFG { static int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd1 ++ ; } else { even1 ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) { odd2 ++ ; } else { even2 ++ ; } } int pairs = Math . min ( odd1 , even2 ) + Math . min ( odd2 , even1 ) ; return pairs ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = a . length ; int m = b . length ; System . out . println ( count_pairs ( a , b , n , m ) ) ; } }
class GFG { static int find_leftmost_unsetbit ( int n ) { int ind = - 1 ; int i = 1 ; while ( n > 0 ) { if ( ( n % 2 ) != 1 ) { ind = i ; } i ++ ; n >>= 1 ; } return ind ; } static void perform_steps ( int n ) { int left = find_leftmost_unsetbit ( n ) ; if ( left == - 1 ) { System . out . print ( " No ▁ steps ▁ required " ) ; return ; } int step = 1 ; while ( find_leftmost_unsetbit ( n ) != - 1 ) { if ( step % 2 == 0 ) { n += 1 ; System . out . println ( " Step " + step + " : ▁ Increase ▁ by ▁ 1" ) ; } else { int m = find_leftmost_unsetbit ( n ) ; int num = ( int ) ( Math . pow ( 2 , m ) - 1 ) ; n = n ^ num ; System . out . println ( " Step " + step + " : ▁ Xor ▁ with ▁ " + num ) ; } step += 1 ; } } public static void main ( String [ ] args ) { int n = 39 ; perform_steps ( n ) ; } }
class GFG { static int vertices ( int N , int A , int B ) { int position = 0 ; int minisum = Integer . MAX_VALUE ; int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i == A i == B ) continue ; else { int x = Math . abs ( i - A ) ; int y = Math . abs ( i - B ) ; sum = x + y ; if ( sum < minisum ) { minisum = sum ; position = i ; } } } return position ; } public static void main ( String [ ] args ) { int N = 3 , A = 1 , B = 2 ; System . out . println ( " Vertex ▁ = ▁ " + vertices ( N , A , B ) ) ; } }
import java . util . * ; class GFG { static int ans = 0 ; static Vector < Vector < Integer > > gr = new Vector < Vector < Integer > > ( ) ; static void Add_Edge ( int u , int v ) { gr . get ( u ) . add ( v ) ; gr . get ( v ) . add ( u ) ; } static void dfs ( int child , int par , int color [ ] ) { if ( color [ child ] != color [ par ] ) ans ++ ; for ( int i = 0 ; i < gr . get ( child ) . size ( ) ; i ++ ) { if ( gr . get ( child ) . get ( i ) == par ) continue ; dfs ( gr . get ( child ) . get ( i ) , child , color ) ; } } public static void main ( String args [ ] ) { for ( int i = 0 ; i <= 10 ; i ++ ) gr . add ( new Vector < Integer > ( ) ) ; int color [ ] = { 0 , 1 , 2 , 3 , 2 , 2 , 3 } ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 2 , 4 ) ; Add_Edge ( 2 , 5 ) ; Add_Edge ( 3 , 6 ) ; dfs ( 1 , 0 , color ) ; System . out . println ( ans ) ; } }
class GFG { static long mod = ( long ) ( 1e9 + 7 ) ; static long factorial ( long n , long p ) { for ( int i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long n , long p ) { return power ( n , p - 2 , p ) ; } static long nCrModP ( long n , long r , long p ) { if ( r == 0 ) return 1 ; long fac [ ] = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ ( int ) r ] , p ) % p * modInverse ( fac [ ( int ) n - ( int ) r ] , p ) % p ) % p ; } static long countArrangements ( long n , long k , long p ) { return ( factorial ( n , p ) * nCrModP ( k , n , p ) ) % p ; } public static void main ( String [ ] args ) { long N = 5 , K = 8 ; System . out . println ( countArrangements ( N , K , mod ) ) ; } }
import java . io . * ; class GFG { static int maxProd ( int N ) { if ( N == 0 ) return 1 ; if ( N < 10 ) return N ; return Math . max ( maxProd ( N / 10 ) * ( N % 10 ) , maxProd ( N / 10 - 1 ) * 9 ) ; } public static void main ( String [ ] args ) { int N = 390 ; System . out . println ( maxProd ( N ) ) ; } }
import java . io . * ; class GFG { static int N = 2 ; static int M = 3 ; static boolean check ( int mat [ ] [ ] ) { int xorr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xorr ^= mat [ i ] [ 0 ] ; } if ( xorr != 0 ) return true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) return true ; } } return false ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 7 , 7 , 7 } , { 10 , 10 , 7 } } ; if ( check ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int sumodd ( int n ) { return ( n * n ) ; } static int sumeven ( int n ) { return ( n * ( n + 1 ) ) ; } static int findSum ( int num ) { int sumo = 0 ; int sume = 0 ; int x = 1 ; int cur = 0 ; int ans = 0 ; while ( num > 0 ) { int inc = Math . min ( x , num ) ; num -= inc ; if ( cur == 0 ) { ans = ans + sumodd ( sumo + inc ) - sumodd ( sumo ) ; sumo += inc ; } else { ans = ans + sumeven ( sume + inc ) - sumeven ( sume ) ; sume += inc ; } x *= 2 ; cur ^= 1 ; } return ans ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( findSum ( n ) ) ; } }
class GFG { static int oddTriangularNumber ( int N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( oddTriangularNumber ( N ) ) ; } }
import java . io . * ; class GFG { static double area ( double a , double b , double c ) { double d = Math . abs ( ( c * c ) / ( 2 * a * b ) ) ; return d ; } public static void main ( String [ ] args ) { double a = - 2 , b = 4 , c = 3 ; System . out . println ( area ( a , b , c ) ) ; } }
import java . util . HashMap ; class GFG { static long MAXN = 100001L , mod = 1000000007L ; static long [ ] spf = new long [ ( int ) MAXN ] ; static HashMap < Long , Long > cnt = new HashMap < > ( ) ; public static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } public static void factorize ( long f ) { while ( f > 1 ) { long x = spf [ ( int ) f ] ; while ( f % x == 0 ) { if ( cnt . containsKey ( x ) ) { long z = cnt . get ( x ) ; cnt . put ( x , ++ z ) ; } else cnt . put ( x , ( long ) 1 ) ; f /= x ; } } } public static long factorial ( long n , long p ) { long res = 1 ; for ( long i = 2 ; i <= n ; i ++ ) res = ( res * i ) % p ; return res ; } public static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } public static long modInverse ( long n , long p ) { return power ( n , p - 2 , p ) ; } public static long nCrModP ( long n , long r , long p ) { if ( r == 0 ) return 1 ; long [ ] fac = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ ( int ) r ] , p ) % p * modInverse ( fac [ ( int ) ( n - r ) ] , p ) % p ) % p ; } public static long countArrays ( long [ ] arr , long N , long K , long P ) { long res = 1 ; sieve ( ) ; for ( int i = 0 ; i < N ; i ++ ) { factorize ( arr [ i ] ) ; } for ( HashMap . Entry < Long , Long > entry : cnt . entrySet ( ) ) { long ci = entry . getValue ( ) ; res = ( res * nCrModP ( ci + K - 1 , K - 1 , P ) ) % P ; } return res ; } public static void main ( String [ ] args ) { long [ ] arr = { 1 , 3 , 5 , 2 } ; long K = 3 ; long N = arr . length ; System . out . println ( countArrays ( arr , N , K , mod ) ) ; } }
class GFG { static int MAXN = 100000 ; static int MOD = 1000000007 ; static long fact [ ] = new long [ MAXN ] ; static void factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; } static long power ( long x , long y , long p ) { while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long x , long p ) { return power ( x , p - 2 , p ) ; } static long countDifferentNumbers ( long arr [ ] , long P ) { factorial ( ) ; long res = 0 , X = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) X += arr [ i ] ; res = fact [ ( int ) X ] ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] > 1 ) res = ( res * modInverse ( fact [ ( int ) arr [ i ] ] , P ) ) % P ; } return res ; } public static void main ( String [ ] args ) { long arr [ ] = { 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 } ; System . out . println ( countDifferentNumbers ( arr , MOD ) ) ; } }
class GFG { static void line ( double x0 , double y0 ) { double c = ( int ) ( 2 * y0 * x0 ) ; System . out . println ( y0 + " x " + " ▁ + ▁ " + x0 + " y ▁ = ▁ " + c ) ; } public static void main ( String [ ] args ) { double x0 = 4 , y0 = 3 ; line ( x0 , y0 ) ; } }
class GFG { static int N = 3 ; static int M = 7 ; static void printOriginalMatrix ( int a [ ] , int b [ ] , int [ ] [ ] mat ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] == 1 ) System . out . print ( Math . min ( a [ i ] , b [ j ] ) + " ▁ " ) ; else System . out . print ( "0" + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 2 , 1 , 3 } ; int b [ ] = { 2 , 3 , 0 , 0 , 2 , 0 , 1 } ; int [ ] [ ] mat = { { 1 , 0 , 0 , 0 , 1 , 0 , 0 } , { 0 , 0 , 0 , 0 , 0 , 0 , 1 } , { 1 , 1 , 0 , 0 , 0 , 0 , 0 } } ; printOriginalMatrix ( a , b , mat ) ; } }
class GFG { static float percent ( int n , int x ) { float p = n * x ; p /= 100 ; return p ; } static float getLoss ( int price [ ] , int quantity [ ] , int X [ ] , int n ) { float loss = 0 ; for ( int i = 0 ; i < n ; i ++ ) { float originalPrice = price [ i ] ; float sellingPrice = originalPrice + percent ( ( int ) originalPrice , X [ i ] ) ; float afterDiscount = sellingPrice - percent ( ( int ) sellingPrice , X [ i ] ) ; loss += ( ( originalPrice - afterDiscount ) * quantity [ i ] ) ; } return loss ; } public static void main ( String args [ ] ) { int price [ ] = { 20 , 48 , 200 , 100 } ; int quantity [ ] = { 20 , 48 , 1 , 1 } ; int X [ ] = { 0 , 48 , 200 , 5 } ; int n = X . length ; System . out . print ( getLoss ( price , quantity , X , n ) ) ; } }
class GFG { static int maxAbsDiff ( int arr [ ] , int n ) { int minEle = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { minEle = Math . min ( minEle , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 5 , 3 } ; int n = arr . length ; System . out . print ( maxAbsDiff ( arr , n ) ) ; } }
class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maximizeSum ( int a [ ] , int n ) { int cnt = 0 ; int mini = Integer . MAX_VALUE ; int minSubarray = Integer . MAX_VALUE ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . min ( mini , 0 ) ; else mini = Math . min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = Integer . MAX_VALUE ; } } return sum - mini ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , - 2 , 3 } ; int n = a . length ; System . out . println ( maximizeSum ( a , n ) ) ; } }
class GFG { static boolean isOsiris ( int n ) { int a = n % 10 ; int b = ( n / 10 ) % 10 ; int c = n / 100 ; int digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } public static void main ( String args [ ] ) { int n = 132 ; if ( isOsiris ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . lang . Math ; class GFG { static boolean isDudeney ( int n ) { int cube_rt = ( int ) ( Math . round ( ( Math . pow ( n , 1.0 / 3.0 ) ) ) ) ; if ( cube_rt * cube_rt * cube_rt != n ) return false ; int dig_sum = 0 ; int temp = n ; while ( temp > 0 ) { int rem = temp % 10 ; dig_sum += rem ; temp /= 10 ; } if ( cube_rt != dig_sum ) return false ; return true ; } public static void main ( String [ ] args ) { int n = 17576 ; if ( isDudeney ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int countTriangles ( int a [ ] , int n ) { int cnt = 0 ; int pairs = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { pairs += a [ i ] / 2 ; if ( a [ i ] % 2 == 1 && pairs > 0 ) { cnt += 1 ; pairs -= 1 ; } } cnt += ( 2 * pairs ) / 3 ; return cnt ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 2 , 2 } ; int n = a . length ; System . out . println ( countTriangles ( a , n ) ) ; } }
class GFG { static int getHCF ( int x , int y ) { int minimum = Math . min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( int i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } public static void main ( String [ ] args ) { int x = 16 , y = 32 ; System . out . println ( getHCF ( x , y ) ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isDivisible ( int n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { int n = 6 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int R = 5 ; static int C = 5 ; static int findMaxCock ( int ar [ ] [ ] ) { if ( R < 3 C < 3 ) return - 1 ; int max_sum = Integer . MIN_VALUE ; for ( int i = 0 ; i < R - 2 ; i ++ ) { for ( int j = 0 ; j < C - 2 ; j ++ ) { int sum = ( ar [ i ] [ j ] + ar [ i ] [ j + 2 ] ) + ( ar [ i + 1 ] [ j + 1 ] ) + ( ar [ i + 2 ] [ j ] + ar [ i + 2 ] [ j + 1 ] + ar [ i + 2 ] [ j + 2 ] ) ; max_sum = Math . max ( max_sum , sum ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int ar [ ] [ ] = { { 0 , 3 , 0 , 6 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 2 , 0 , 1 , 3 } } ; System . out . println ( findMaxCock ( ar ) ) ; } }
import java . util . HashMap ; class GFG { public static int segments ( int n , int [ ] p , int m ) { HashMap < Integer , Integer > c = new HashMap < > ( ) ; c . put ( 0 , 1 ) ; boolean has = false ; int sum = 0 ; int ans = 0 ; for ( int r = 0 ; r < n ; r ++ ) { if ( p [ r ] < m ) sum -- ; else if ( p [ r ] > m ) sum ++ ; if ( p [ r ] == m ) has = true ; if ( has ) ans += ( c . get ( sum ) == null ? 0 : c . get ( sum ) ) + ( c . get ( sum - 1 ) == null ? 0 : c . get ( sum - 1 ) ) ; else c . put ( sum , c . get ( sum ) == null ? 1 : c . get ( sum ) + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 4 , 5 , 3 , 1 } ; int n = a . length ; int m = 4 ; System . out . println ( segments ( n , a , m ) ) ; } }
class GFG { static int oddDays ( int N ) { int hund1 = N / 100 ; int hund4 = N / 400 ; int leap = N >> 2 ; int ord = N - leap ; if ( hund1 > 0 ) { ord += hund1 ; leap -= hund1 ; } if ( hund4 > 0 ) { ord -= hund4 ; leap += hund4 ; } int days = ord + leap * 2 ; int odd = days % 7 ; return odd ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . print ( oddDays ( N ) ) ; } }
class GFG { static float ellipsearea ( float r ) { if ( r < 0 ) return - 1 ; float a = ( float ) ( ( 3.14f * r * r ) / 4 ) ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( ellipsearea ( r ) ) ; } }
import java . util . * ; class GFG { static int minOperations ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxFreq = Integer . MIN_VALUE ; maxFreq = Collections . max ( mp . entrySet ( ) , Comparator . comparingInt ( Map . Entry :: getKey ) ) . getValue ( ) ; return ( n - maxFreq ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; System . out . println ( minOperations ( arr , n ) ) ; } }
class GfG { static int CntDivbyX ( int arr [ ] , int n , int x ) { int number = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { number = number * 2 + arr [ i ] ; if ( ( number % x == 0 ) ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int n = arr . length ; int x = 2 ; System . out . println ( CntDivbyX ( arr , n , x ) ) ; } }
class GFG { static int smallestIndex ( int [ ] a , int n ) { int right1 = 0 , right0 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) right1 = i ; else right0 = i ; } return Math . min ( right1 , right0 ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 } ; int n = a . length ; System . out . println ( smallestIndex ( a , n ) ) ; } }
public class GfG { static int countCon ( int ar [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 2 , 3 , 4 , 4 , 5 , 5 , 5 , 5 } ; int n = ar . length ; System . out . println ( countCon ( ar , n ) ) ; } }
class GFG { static void reduceFraction ( int x , int y ) { int d ; d = __gcd ( x , y ) ; x = x / d ; y = y / d ; System . out . println ( " x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int x = 16 ; int y = 10 ; reduceFraction ( x , y ) ; } }
class GFG { static float noOfYears ( int t1 , int n1 , int t2 ) { float years = ( ( t2 - 1 ) * n1 / ( float ) ( t1 - 1 ) ) ; return years ; } public static void main ( String [ ] args ) { int T1 = 3 , N1 = 5 , T2 = 6 ; System . out . println ( noOfYears ( T1 , N1 , T2 ) ) ; } }
import java . util . * ; class GFG { static void findSubsequence ( int [ ] arr , int n , int k ) { HashMap < Integer , Integer > M = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( M . containsKey ( arr [ i ] ) ) M . put ( arr [ i ] , M . get ( arr [ i ] ) + 1 ) ; else M . put ( arr [ i ] , 1 ) ; } int [ ] numCount = new int [ k + 1 ] ; for ( int i = 0 ; i <= k ; ++ i ) numCount [ i ] = 0 ; Iterator < HashMap . Entry < Integer , Integer > > itr = M . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { HashMap . Entry < Integer , Integer > entry = itr . next ( ) ; if ( entry . getKey ( ) <= k ) { for ( int i = 1 ; ; ++ i ) { if ( entry . getKey ( ) * i > k ) break ; numCount [ entry . getKey ( ) * i ] += entry . getValue ( ) ; } } else break ; } int lcm = 0 , length = 0 ; for ( int i = 1 ; i <= k ; ++ i ) { if ( numCount [ i ] > length ) { length = numCount [ i ] ; lcm = i ; } } if ( lcm == 0 ) System . out . println ( - 1 ) ; else { System . out . println ( " LCM ▁ = ▁ " + lcm + " ▁ Length ▁ = ▁ " + length ) ; System . out . print ( " Indexes ▁ = ▁ " ) ; for ( int i = 0 ; i < n ; ++ i ) if ( lcm % arr [ i ] == 0 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int k = 14 ; int arr [ ] = { 2 , 3 , 4 , 5 } ; int n = arr . length ; findSubsequence ( arr , n , k ) ; } }
import java . util . * ; class GFG { static ArrayList < Integer > addToArrayForm ( ArrayList < Integer > A , int K ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; int rem = 0 ; int i = 0 ; for ( i = A . size ( ) - 1 ; i >= 0 ; i -- ) { int my = A . get ( i ) + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . add ( my % 10 ) ; } else { v . add ( my ) ; rem = 0 ; } K = K / 10 ; } while ( K > 0 ) { int my = K % 10 + rem ; v . add ( my % 10 ) ; if ( my / 10 > 0 ) rem = 1 ; else rem = 0 ; K = K / 10 ; } if ( rem > 0 ) v . add ( rem ) ; for ( int j = v . size ( ) - 1 ; j >= 0 ; j -- ) ans . add ( v . get ( j ) ) ; return ans ; } public static void main ( String [ ] args ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; A . add ( 2 ) ; A . add ( 7 ) ; A . add ( 4 ) ; int K = 181 ; ArrayList < Integer > ans = addToArrayForm ( A , K ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) ) ; } }
import java . util . * ; class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int power ( int x , int y , int p ) { while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static int findCount ( int N ) { int count = power ( 2 , N , mod ) ; return count ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . println ( findCount ( N ) ) ; } }
class GFG { static int msb ( int x ) { int ret = 0 ; while ( ( x >> ( ret + 1 ) ) != 0 ) ret ++ ; return ret ; } static int xorRange ( int l , int r ) { int max_bit = msb ( r ) ; int mul = 2 ; int ans = 0 ; for ( int i = 1 ; i <= max_bit ; i ++ ) { if ( ( l / mul ) * mul == ( r / mul ) * mul ) { if ( ( ( l & ( 1 << i ) ) != 0 ) && ( r - l + 1 ) % 2 == 1 ) ans += mul ; mul *= 2 ; continue ; } int odd_c = 0 ; if ( ( ( l & ( 1 << i ) ) != 0 ) && l % 2 == 1 ) odd_c = ( odd_c ^ 1 ) ; if ( ( ( r & ( 1 << i ) ) != 0 ) && r % 2 == 0 ) odd_c = ( odd_c ^ 1 ) ; if ( odd_c != 0 ) ans += mul ; mul *= 2 ; } int zero_bit_cnt = zero_bit_cnt = ( r - l + 1 ) / 2 ; if ( l % 2 == 1 && r % 2 == 1 ) zero_bit_cnt ++ ; if ( zero_bit_cnt % 2 == 1 ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int l = 1 , r = 4 ; System . out . print ( xorRange ( l , r ) ) ; } }
class GFG { static int countNumbers ( int x , int n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } public static void main ( String [ ] args ) { int n = 120 ; System . out . println ( countNumbers ( 1 , n ) ) ; } }
class GfG { static boolean checkfact ( int N , int countprime , int prime ) { int countfact = 0 ; if ( prime == 2 prime == 3 ) countfact ++ ; int divide = prime ; while ( N / divide != 0 ) { countfact += N / divide ; divide = divide * divide ; } if ( countfact >= countprime ) return true ; else return false ; } static boolean check ( int N ) { int sumsquares = ( N + 1 ) * ( 2 * N + 1 ) ; int countprime = 0 ; for ( int i = 2 ; i <= Math . sqrt ( sumsquares ) ; i ++ ) { int flag = 0 ; while ( sumsquares % i == 0 ) { flag = 1 ; countprime ++ ; sumsquares /= i ; } if ( flag == 1 ) { if ( ! checkfact ( N - 1 , countprime , i ) ) return false ; countprime = 0 ; } } if ( sumsquares != 1 ) if ( ! checkfact ( N - 1 , 1 , sumsquares ) ) return false ; return true ; } public static void main ( String [ ] args ) { int N = 5 ; if ( check ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int countSquares ( int r , int c , int m ) { int squares = 0 ; for ( int i = 1 ; i <= 8 ; i ++ ) { for ( int j = 1 ; j <= 8 ; j ++ ) { if ( Math . max ( Math . abs ( i - r ) , Math . abs ( j - c ) ) <= m ) squares ++ ; } } return squares ; } public static void main ( String [ ] args ) { int r = 4 , c = 4 , m = 1 ; System . out . print ( countSquares ( r , c , m ) ) ; } }
class GFG { static int countNonIncreasing ( int arr [ ] , int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] <= arr [ i ] ) len ++ ; else { cnt += ( ( ( len + 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len + 1 ) * len ) / 2 ) ; return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 7 , 1 , 1 } ; int n = arr . length ; System . out . println ( countNonIncreasing ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int recamanGenerator ( int arr [ ] , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } return 0 ; } static int recamanArray ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; int recaman [ ] = new int [ n + 1 ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) s . remove ( arr [ i ] ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = arr . length ; System . out . print ( recamanArray ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 100005 ; static int kadaneAlgorithm ( int [ ] ar , int n ) { int sum = 0 , maxSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; if ( sum < 0 ) sum = 0 ; maxSum = Math . max ( maxSum , sum ) ; } return maxSum ; } static int maxFunction ( int [ ] arr , int n ) { int [ ] b = new int [ MAX ] ; int [ ] c = new int [ MAX ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i % 2 == 1 ) { b [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; c [ i ] = - b [ i ] ; } else { c [ i ] = Math . abs ( arr [ i + 1 ] - arr [ i ] ) ; b [ i ] = - c [ i ] ; } } int ans = kadaneAlgorithm ( b , n - 1 ) ; ans = Math . max ( ans , kadaneAlgorithm ( c , n - 1 ) ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 4 , 7 } ; int n = arr . length ; System . out . println ( maxFunction ( arr , n ) ) ; } }
class GFG { static int printPermutation ( int n ) { if ( n % 2 != 0 ) { System . out . println ( " - 1" ) ; } else for ( int i = 1 ; i <= n / 2 ; i ++ ) { System . out . print ( 2 * i + " ▁ " + ( ( 2 * i ) - 1 ) + " ▁ " ) ; } return n ; } public static void main ( String [ ] args ) { int n = 4 ; printPermutation ( n ) ; } }
class GFG { static int MAXN = 10000000 ; static int minimumSteps ( int n , int m , int a , int b ) { if ( n > m ) return MAXN ; if ( n == m ) return 0 ; return Math . min ( 1 + minimumSteps ( n * a , m , a , b ) , 1 + minimumSteps ( n * b , m , a , b ) ) ; } public static void main ( String [ ] args ) { int n = 120 , m = 51840 ; int a = 2 , b = 3 ; System . out . println ( minimumSteps ( n , m , a , b ) ) ; } }
class GfG { static int minOperations ( int n , int m ) { if ( m % n != 0 ) return - 1 ; int minOperations = 0 ; int q = m / n ; while ( q % 2 == 0 ) { q = q / 2 ; minOperations ++ ; } while ( q % 3 == 0 ) { q = q / 3 ; minOperations ++ ; } if ( q == 1 ) return minOperations ; return - 1 ; } public static void main ( String [ ] args ) { int n = 120 , m = 51840 ; System . out . println ( minOperations ( n , m ) ) ; } }
import java . lang . Math ; class GFG { static float sphereSide ( float h , float r ) { if ( h < 0 && r < 0 ) return - 1 ; float R = ( float ) ( ( h * r * Math . sqrt ( 2 ) ) / ( h + Math . sqrt ( 2 ) * r ) ) / 2 ; return R ; } public static void main ( String [ ] args ) { float h = 5 , r = 6 ; System . out . println ( sphereSide ( h , r ) ) ; } }
class GFG { static int possibleways ( int n ) { if ( n % 2 == 1 ) return 0 ; else if ( n % 4 == 0 ) return n / 4 - 1 ; else return n / 4 ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( possibleways ( n ) ) ; } }
import java . util . * ; class GFG { static int countSubarrays ( int [ ] arr , int n , int K ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product = 1 ; for ( int x = i ; x <= j ; x ++ ) product *= arr [ x ] ; if ( product % K == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 8 } ; int n = arr . length ; int K = 4 ; System . out . println ( countSubarrays ( arr , n , K ) ) ; } }
class GFG { static int MAX = 100002 ; static long tree [ ] = new long [ 4 * MAX ] ; static void build ( int node , int start , int end , int [ ] arr , int k ) { if ( start == end ) { tree [ node ] = ( 1L * arr [ start ] ) % k ; return ; } int mid = ( start + end ) >> 1 ; build ( 2 * node , start , mid , arr , k ) ; build ( 2 * node + 1 , mid + 1 , end , arr , k ) ; tree [ node ] = ( tree [ 2 * node ] * tree [ 2 * node + 1 ] ) % k ; } static long query ( int node , int start , int end , int l , int r , int k ) { if ( start > end start > r end < l ) { return 1 ; } if ( start >= l && end <= r ) { return tree [ node ] % k ; } int mid = ( start + end ) >> 1 ; long q1 = query ( 2 * node , start , mid , l , r , k ) ; long q2 = query ( 2 * node + 1 , mid + 1 , end , l , r , k ) ; return ( q1 * q2 ) % k ; } static long countSubarrays ( int [ ] arr , int n , int k ) { long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { long product_mod_k = query ( 1 , 0 , n - 1 , i , j , k ) ; if ( product_mod_k == 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 8 } ; int n = arr . length ; int k = 4 ; build ( 1 , 0 , n - 1 , arr , k ) ; System . out . println ( countSubarrays ( arr , n , k ) ) ; } }
class GFG { static int Permutations ( int n , int k ) { int ans = 1 ; if ( k >= 2 ) ans += ( n ) * ( n - 1 ) / 2 ; if ( k >= 3 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * 2 / 6 ; if ( k >= 4 ) ans += ( n ) * ( n - 1 ) * ( n - 2 ) * ( n - 3 ) * 9 / 24 ; return ans ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( Permutations ( n , k ) ) ; } }
class GFG { static long power ( long a , long n ) { if ( n == 0 ) return 1 ; long p = power ( a , n / 2 ) ; p = p * p ; if ( n % 2 == 1 ) p = p * a ; return p ; } static int countIntegers ( long l , long r ) { long ans = 0 , i = 1 ; long v = power ( 2 , i ) ; while ( v <= r ) { while ( v <= r ) { if ( v >= l ) ans ++ ; v = v * 3 ; } i ++ ; v = power ( 2 , i ) ; } if ( l == 1 ) ans ++ ; return ( int ) ans ; } public static void main ( String [ ] args ) { long l = 12 , r = 21 ; System . out . println ( countIntegers ( l , r ) ) ; } }
class GfG { static int findPairCount ( int N , int K ) { int count = 0 ; int rem [ ] = new int [ K ] ; rem [ 0 ] = N / K ; for ( int i = 1 ; i < K ; i ++ ) rem [ i ] = ( N - i ) / K + 1 ; if ( K % 2 == 0 ) { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i < K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; count += ( rem [ K / 2 ] * ( rem [ K / 2 ] - 1 ) ) / 2 ; } else { count += ( rem [ 0 ] * ( rem [ 0 ] - 1 ) ) / 2 ; for ( int i = 1 ; i <= K / 2 ; i ++ ) count += rem [ i ] * rem [ K - i ] ; } return count ; } public static void main ( String [ ] args ) { int N = 10 , K = 4 ; System . out . println ( findPairCount ( N , K ) ) ; } }
import java . util . * ; class GFG { static final int N = 1000005 ; static boolean prime [ ] = new boolean [ N ] ; static void sieve ( ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } static int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; boolean flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 25 ; sieve ( ) ; System . out . println ( sumTruncatablePrimes ( n ) ) ; } }
class GFG { static void nDigitPerfectSquares ( int n ) { int smallest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) , 2 ) ; System . out . print ( smallest + " ▁ " ) ; int largest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) - 1 , 2 ) ; System . out . print ( largest ) ; } public static void main ( String args [ ] ) { int n = 4 ; nDigitPerfectSquares ( n ) ; } }
class GFG { static int N = 3 ; static int MaxTraceSub ( int mat [ ] [ ] ) { int max_trace = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { int r = i , s = j , trace = 0 ; while ( r < N && s < N ) { trace += mat [ r ] [ s ] ; r ++ ; s ++ ; max_trace = Math . max ( trace , max_trace ) ; } } } return max_trace ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 2 , 5 } , { 6 , 10 , 4 } , { 2 , 7 , - 10 } } ; System . out . println ( MaxTraceSub ( mat ) ) ; } }
import java . util . * ; class GFG { static final int n = 3 ; static final int m = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = i ; int col = 0 ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int j = 0 ; j < v1 . size ( ) ; j ++ ) { if ( v1 . get ( j ) != v2 . get ( j ) ) { return false ; } } } for ( int j = 1 ; j < m ; j ++ ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; int r = n - 1 ; int col = j ; while ( r >= 0 && col < m ) { v1 . add ( a [ r ] [ col ] ) ; v2 . add ( b [ r ] [ col ] ) ; r -- ; col ++ ; } Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) { if ( v1 . get ( i ) != v2 . get ( i ) ) { return false ; } } } return true ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int b [ ] [ ] = { { 1 , 4 , 7 } , { 2 , 5 , 6 } , { 3 , 8 , 9 } } ; if ( check ( a , b ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
class Solution { public static void lastDigit ( String a , String b ) { int lastDig = ( a . charAt ( a . length ( ) - 1 ) - '0' ) * ( b . charAt ( b . length ( ) - 1 ) - '0' ) ; System . out . println ( lastDig % 10 ) ; } public static void main ( String [ ] args ) { String a = "1234567891233" , b = "1234567891233156" ; lastDigit ( a , b ) ; } }
class GfG { static void printPalindrome ( int n ) { if ( n == 1 ) { System . out . println ( " Smallest ▁ Palindrome : ▁ 0" ) ; System . out . println ( " Largest ▁ Palindrome : ▁ 9" ) ; } else { System . out . println ( " Smallest ▁ Palindrome : ▁ " + ( int ) ( Math . pow ( 10 , n - 1 ) ) + 1 ) ; System . out . println ( " Largest ▁ Palindrome : ▁ " + ( ( int ) ( Math . pow ( 10 , n ) ) - 1 ) ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printPalindrome ( n ) ; } }
class GFG { static int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a != 0 ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b != 0 ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a != 0 ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) { res = res * 10 + sum ; } else { temp1 = 0 ; while ( sum != 0 ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum != 0 ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } public static void main ( String [ ] args ) { int a = 7752 , b = 8834 ; System . out . println ( printSum ( a , b ) ) ; } }
class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . abs ( a / b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void main ( String args [ ] ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }
class GFG { public static int countDigits ( int a , int b ) { double digits = Math . log10 ( Math . abs ( a ) ) - Math . log10 ( Math . abs ( b ) ) + 1 ; return ( int ) Math . floor ( digits ) ; } public static void main ( String [ ] args ) { int a = 100 ; int b = 10 ; System . out . print ( countDigits ( a , b ) ) ; } }
class Solution { static int smallestOdd ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( smallestOdd ( n ) ) ; } }
class GFG { static void findNumbers ( int n ) { int odd = ( int ) Math . pow ( 10 , n ) - 1 ; int even = odd - 1 ; System . out . println ( " Even ▁ = ▁ " + even ) ; System . out . print ( " Odd ▁ = ▁ " + odd ) ; } public static void main ( String args [ ] ) { int n = 4 ; findNumbers ( n ) ; } }
class GFG { static int longestSubArray ( int arr [ ] , int n ) { boolean isZeroPresent = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) { isZeroPresent = true ; break ; } } if ( isZeroPresent ) return n ; return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 0 , 1 , 2 , 0 } ; int n = arr . length ; System . out . print ( longestSubArray ( arr , n ) ) ; } }
class Solution { static int smallestEven ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( smallestEven ( n ) ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } public static void main ( String [ ] args ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; System . out . println ( maxProfit ( n , a , b , x , y ) ) ; } }
class GFG { public static final int MOD = 1000000007 ; static int sumOfSeries ( int n ) { int ans = ( int ) Math . pow ( n % MOD , 2 ) ; return ( ans % MOD ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
public class GFG { static int kthOdd ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 2 ; System . out . print ( kthOdd ( arr , n , k ) ) ; } }
class GfG { static void lastFiveDigits ( int n ) { n = ( n / 10000 ) * 10000 + ( ( n / 100 ) % 10 ) * 1000 + ( n % 10 ) * 100 + ( ( n / 10 ) % 10 ) * 10 + ( n / 1000 ) % 10 ; int ans = 1 ; for ( int i = 0 ; i < 5 ; i ++ ) { ans *= n ; ans %= 100000 ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int n = 12345 ; lastFiveDigits ( n ) ; } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) { return 1 ; } long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n == 1 ) { p = ( p * a ) % mod ; } return p ; } static long computeSum ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { long maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; long mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; long V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 1 } ; int n = arr . length ; System . out . println ( computeSum ( arr , n ) ) ; } }
class GFG { static int count ( int n ) { int cnt = 0 ; int p = 1 ; while ( p <= n ) { cnt ++ ; p *= 2 ; } return cnt ; } public static void main ( String args [ ] ) { int n = 7 ; System . out . print ( count ( n ) ) ; } }
import java . util . * ; class GFG1 { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { Arrays . sort ( A ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . length ; System . out . println ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }
class GFG { static int sum ( int n ) { n = ( n + 1 ) / 2 ; return ( n * n ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum ( n ) ) ; } }
class GFG { static int countNumbers ( int n ) { return n - n / 2 - n / 3 - n / 5 - n / 7 + n / 6 + n / 10 + n / 14 + n / 15 + n / 21 + n / 35 - n / 30 - n / 42 - n / 70 - n / 105 + n / 210 ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . println ( countNumbers ( n ) ) ; } }
class GFG { static int findThirdDigit ( int n ) { if ( n < 3 ) return 0 ; return ( n & 1 ) > 0 ? 1 : 6 ; } public static void main ( String args [ ] ) { int n = 7 ; System . out . println ( findThirdDigit ( n ) ) ; } }
class GFG { static int maxPrimes ( int n ) { return n / 2 ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( maxPrimes ( n ) ) ; } }
class GFG { static int sum ( int n ) { return n * ( n + 1 ) * ( n + 2 ) / 3 ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sum ( n ) ) ; } }
import java . util . ArrayList ; import java . util . Comparator ; import java . util . PriorityQueue ; class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } public Pair ( ) { } } static final int N = 100005 ; static final int MAXI = 99999999 ; static void add_edge ( ArrayList < Pair > gr [ ] , int x , int y , int weight ) { gr [ x ] . add ( new Pair ( y , weight ) ) ; gr [ y ] . add ( new Pair ( x , weight ) ) ; } static int minDistance ( ArrayList < Pair > gr [ ] , int n , int dist [ ] , int vis [ ] , int a [ ] , int k ) { PriorityQueue < Pair > q = new PriorityQueue < > ( new Comparator < Pair > ( ) { public int compare ( Pair p1 , Pair p2 ) { if ( p1 . first == p2 . first ) { return p1 . second - p2 . second ; } return p1 . first - p2 . first ; } } ) ; int ans = MAXI ; for ( int i = 1 ; i <= n ; i ++ ) { if ( a [ i ] == 0 ) continue ; for ( int j = 1 ; j <= n ; j ++ ) { dist [ j ] = MAXI ; vis [ j ] = 0 ; } dist [ i ] = 0 ; while ( ! q . isEmpty ( ) ) q . poll ( ) ; q . add ( new Pair ( 0 , i ) ) ; int good = 0 ; while ( ! q . isEmpty ( ) ) { int v = q . peek ( ) . second ; q . poll ( ) ; if ( vis [ v ] != 0 ) continue ; vis [ v ] = 1 ; good += a [ v ] ; if ( dist [ v ] > ans ) break ; if ( good == 2 && a [ v ] != 0 ) { ans = Math . min ( ans , dist [ v ] ) ; break ; } for ( int j = 0 ; j < gr [ v ] . size ( ) ; j ++ ) { int to = gr [ v ] . get ( j ) . first ; int weight = gr [ v ] . get ( j ) . second ; if ( dist [ v ] + weight < dist [ to ] ) { dist [ to ] = dist [ v ] + weight ; q . add ( new Pair ( dist [ to ] , to ) ) ; } } } } return ans ; } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; @ SuppressWarnings ( " unchecked " ) ArrayList < Pair > [ ] gr = new ArrayList [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { gr [ i ] = new ArrayList < Pair > ( ) ; } add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 1 , 2 , 3 ) ; add_edge ( gr , 2 , 3 , 4 ) ; add_edge ( gr , 3 , 4 , 1 ) ; add_edge ( gr , 4 , 5 , 8 ) ; int k = 3 ; int [ ] a = new int [ N ] , vis = new int [ N ] , dist = new int [ N ] ; a [ 1 ] = a [ 3 ] = a [ 5 ] = 1 ; System . out . println ( minDistance ( gr , n , dist , vis , a , k ) ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int findMinimum ( int n , int m ) { int mini = m ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { int sec = n / i ; if ( __gcd ( m , i ) > 1 ) { return i ; } else if ( __gcd ( sec , m ) > 1 ) { mini = Math . min ( sec , mini ) ; } } } if ( mini == m ) return - 1 ; else return mini ; } public static void main ( String [ ] args ) { int n = 8 , m = 10 ; System . out . println ( findMinimum ( n , m ) ) ; } }
import java . lang . Math ; class GFG { public static int nthTerm ( int N ) { int nth = 0 , i ; for ( i = N ; i > 0 ; i -- ) { nth += Math . pow ( i , i ) ; } return nth ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }
class GFG { static int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } public static void main ( String args [ ] ) { int n = 8 , k = 4 ; System . out . print ( kthSmallest ( n , k ) ) ; } }
class GFG { static boolean isSumOfPowersOfTwo ( int n ) { if ( n % 2 == 1 ) return false ; else return true ; } public static void main ( String args [ ] ) { int n = 10 ; if ( isSumOfPowersOfTwo ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static int findMinRemoval ( int arr [ ] , int n ) { int temp , value = 0 ; int maximum = 0 ; if ( n == 1 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != 0 && i != n - 1 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) + Math . abs ( arr [ i ] - arr [ i - 1 ] ) - Math . abs ( arr [ i - 1 ] - arr [ i + 1 ] ) ; } else if ( i == 0 ) { value = value + Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; temp = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; } else temp = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; maximum = Math . max ( maximum , temp ) ; } return ( value - maximum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 , 2 , 10 } ; int n = arr . length ; System . out . print ( findMinRemoval ( arr , n ) + "NEW_LINE"); } }
class GFG { static double getTime ( int u , int v , int x ) { double speed = u + v ; double time = x / speed ; return time ; } public static void main ( String [ ] args ) { int u = 3 , v = 3 , x = 3 ; System . out . println ( getTime ( u , v , x ) ) ; } }
import java . io . * ; class GFG { static int number_of_teams ( int M ) { int N1 , N2 , sqr ; sqr = ( int ) Math . sqrt ( 1 + ( 8 * M ) ) ; N1 = ( 1 + sqr ) / 2 ; N2 = ( 1 - sqr ) / 2 ; if ( N1 > 0 ) return N1 ; return N2 ; } public static void main ( String [ ] args ) { int M = 45 ; System . out . println ( number_of_teams ( M ) ) ; } }
class GFG { static int LucasSum ( int N ) { int sum = 0 ; int a = 2 , b = 1 , c ; sum += a ; while ( b <= N ) { sum += b ; c = a + b ; a = b ; b = c ; } return sum ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( LucasSum ( N ) ) ; } }
class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } public static void main ( String [ ] args ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }
class GFG { static int countNumbers ( int L , int R , int K ) { if ( K == 9 ) { K = 0 ; } int totalnumbers = R - L + 1 ; int factor9 = totalnumbers / 9 ; int rem = totalnumbers % 9 ; int ans = factor9 ; for ( int i = R ; i > R - rem ; i -- ) { int rem1 = i % 9 ; if ( rem1 == K ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int L = 10 ; int R = 22 ; int K = 3 ; System . out . println ( countNumbers ( L , R , K ) ) ; } }
class GFG { static int EvenSum ( int [ ] A , int index , int value ) { A [ index ] = A [ index ] + value ; int sum = 0 ; for ( int i = 0 ; i < A . length ; i ++ ) if ( A [ i ] % 2 == 0 ) sum = sum + A [ i ] ; return sum ; } static void BalanceArray ( int [ ] A , int [ ] [ ] Q ) { int [ ] ANS = new int [ Q . length ] ; int i , sum ; for ( i = 0 ; i < Q . length ; i ++ ) { int index = Q [ i ] [ 0 ] ; int value = Q [ i ] [ 1 ] ; sum = EvenSum ( A , index , value ) ; ANS [ i ] = sum ; } for ( i = 0 ; i < ANS . length ; i ++ ) System . out . print ( ANS [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] [ ] Q = { { 0 , 1 } , { 1 , - 3 } , { 0 , - 4 } , { 3 , 2 } } ; BalanceArray ( A , Q ) ; } }
class GFG { static void BalanceArray ( int [ ] A , int [ ] [ ] Q ) { int [ ] ANS = new int [ A . length ] ; int i , sum = 0 ; for ( i = 0 ; i < A . length ; i ++ ) if ( A [ i ] % 2 == 0 ) sum = sum + A [ i ] ; for ( i = 0 ; i < Q . length ; i ++ ) { int index = Q [ i ] [ 0 ] ; int value = Q [ i ] [ 1 ] ; if ( A [ index ] % 2 == 0 ) sum = sum - A [ index ] ; A [ index ] = A [ index ] + value ; if ( A [ index ] % 2 == 0 ) sum = sum + A [ index ] ; ANS [ i ] = sum ; } for ( i = 0 ; i < ANS . length ; i ++ ) System . out . print ( ANS [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 } ; int [ ] [ ] Q = { { 0 , 1 } , { 1 , - 3 } , { 0 , - 4 } , { 3 , 2 } } ; BalanceArray ( A , Q ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int findPowerOfP ( int N , int p ) { int count = 0 ; int r = p ; while ( r <= N ) { count += ( N / r ) ; r = r * p ; } return count ; } static Vector < pair > primeFactorsofB ( int B ) { Vector < pair > ans = new Vector < pair > ( ) ; for ( int i = 2 ; B != 1 ; i ++ ) { if ( B % i == 0 ) { int count = 0 ; while ( B % i == 0 ) { B = B / i ; count ++ ; } ans . add ( new pair ( i , count ) ) ; } } return ans ; } static int largestPowerOfB ( int N , int B ) { Vector < pair > vec = new Vector < pair > ( ) ; vec = primeFactorsofB ( B ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) ans = Math . min ( ans , findPowerOfP ( N , vec . get ( i ) . first ) / vec . get ( i ) . second ) ; return ans ; } public static void main ( String [ ] args ) { System . out . println ( largestPowerOfB ( 5 , 2 ) ) ; System . out . println ( largestPowerOfB ( 6 , 9 ) ) ; } }
class GFG { static int countNumbers ( int X , int Y , int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( ( i % X == 0 ) && ( i % Y != 0 ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int X = 2 , Y = 3 , N = 10 ; System . out . println ( countNumbers ( X , Y , N ) ) ; } }
import java . util . * ; class GFG { static int n = 3 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int p_arr [ ] [ ] , boolean arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( arr [ i ] [ j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( boolean arr [ ] [ ] ) { int [ ] [ ] p_arr = new int [ n ] [ n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 && q . peek ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . peek ( ) . second + 1 ) * ( q . peek ( ) . first - p_arr [ i ] [ j ] ) ; c += q . peek ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . add ( new pair ( p_arr [ i ] [ j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void main ( String [ ] args ) { boolean arr [ ] [ ] = { { false , false , false } , { false , true , false } , { false , false , false } } ; System . out . println ( matrixOrValueOne ( arr ) ) ; } }
class GFG { static int submatrixXor ( int [ ] [ ] arr ) { int n = 3 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 6 , 7 , 13 } , { 8 , 3 , 4 } , { 9 , 7 , 6 } } ; System . out . println ( submatrixXor ( arr ) ) ; } }
class GFG { static int findDigitalRoot ( int num ) { int sum = Integer . MAX_VALUE , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } static void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { System . out . print ( i ) ; break ; } } } public static void main ( String args [ ] ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; } }
class GfG { static int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } public static void main ( String [ ] args ) { int X = 7 , N = 43 ; System . out . println ( findAnswer ( X , N ) ) ; } }
class GfG { static long count ( int N , int K , int R ) { long sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % K == R ) sum += i ; } return sum ; } public static void main ( String [ ] args ) { int N = 20 , K = 4 , R = 3 ; System . out . println ( count ( N , K , R ) ) ; } }
import java . util . * ; class GFG { static int LucasSequence ( int [ ] arr , int n ) { int max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int counter = 0 ; HashSet < Integer > s = new HashSet < > ( ) ; int a = 2 , b = 1 ; s . add ( a ) ; s . add ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; s . add ( b ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) { counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 11 , 22 , 4 , 2 , 1 , 8 , 9 } ; int n = arr . length ; System . out . println ( LucasSequence ( arr , n ) ) ; } }
class GfG { static int getCount ( int a , int b , int c ) { int count = 0 ; for ( int i = 1 ; i <= 81 ; i ++ ) { int cr = b * ( int ) Math . pow ( i , a ) + c ; int tmp = cr ; int sm = 0 ; while ( tmp != 0 ) { sm += tmp % 10 ; tmp /= 10 ; } if ( sm == i && cr < 1e9 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a = 3 , b = 2 , c = 8 ; System . out . println ( getCount ( a , b , c ) ) ; } }
class GFG { static boolean isSpiltPossible ( int n , int a [ ] ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 3 ; int a [ ] = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static int maxAND ( int L , int R ) { int maximum = L & R ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i & j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 1 , R = 632 ; System . out . println ( maxAND ( L , R ) ) ; } }
class GfG { static int findCount ( int arr [ ] , int n ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; if ( countOdd % 2 == 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 1 } ; int n = arr . length ; System . out . println ( findCount ( arr , n ) ) ; } }
class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean isSunnyNum ( int n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 ; if ( isSunnyNum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int Cycles ( int N ) { int fact = 1 , result = 0 ; result = N - 1 ; int i = result ; while ( i > 0 ) { fact = fact * i ; i -- ; } return fact / 2 ; } public static void main ( String [ ] args ) { int N = 5 ; int Number = Cycles ( N ) ; System . out . println ( " Hamiltonian ▁ cycles ▁ = ▁ " + Number ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int minOperations ( int n , int a [ ] , int m ) { int k = n / m ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] val = new Vector [ m ] ; for ( int i = 0 ; i < val . length ; i ++ ) val [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { val [ a [ i ] % m ] . add ( i ) ; } long ans = 0 ; Vector < pair > extra = new Vector < > ( ) ; for ( int i = 0 ; i < 2 * m ; ++ i ) { int cur = i % m ; while ( ( val [ cur ] . size ( ) ) > k ) { int elem = val [ cur ] . lastElement ( ) ; val [ cur ] . removeElementAt ( val [ cur ] . size ( ) - 1 ) ; extra . add ( new pair ( elem , i ) ) ; } while ( val [ cur ] . size ( ) < k && ! extra . isEmpty ( ) ) { int elem = extra . get ( extra . size ( ) - 1 ) . first ; int mmod = extra . get ( extra . size ( ) - 1 ) . second ; extra . remove ( extra . size ( ) - 1 ) ; val [ cur ] . add ( elem ) ; ans += i - mmod ; } } return ( int ) ans ; } public static void main ( String [ ] args ) { int m = 3 ; int a [ ] = { 3 , 2 , 0 , 6 , 10 , 12 } ; int n = a . length ; System . out . print ( minOperations ( n , a , m ) ) ; } }
class GFG { static int findAndSum ( int [ ] arr , int n ) { int sum = 0 ; int mul = 1 ; for ( int i = 0 ; i < 30 ; i ++ ) { boolean count_on = false ; int l = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) > 0 ) if ( count_on ) l ++ ; else { count_on = true ; l ++ ; } else if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } } if ( count_on ) { sum += ( ( mul * l * ( l + 1 ) ) / 2 ) ; count_on = false ; l = 0 ; } mul *= 2 ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 1 , 1 , 5 } ; int n = arr . length ; System . out . println ( findAndSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static boolean isPossible ( int Sx , int Sy , int Dx , int Dy , int x , int y ) { if ( Math . abs ( Sx - Dx ) % x == 0 && Math . abs ( Sy - Dy ) % y == 0 && ( Math . abs ( Sx - Dx ) / x ) % 2 == ( Math . abs ( Sy - Dy ) / y ) % 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int Sx = 0 , Sy = 0 , Dx = 0 , Dy = 0 ; int x = 3 , y = 4 ; if ( isPossible ( Sx , Sy , Dx , Dy , x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int getPairs ( int a [ ] ) { int count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < a . length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 3 , 1 } ; System . out . println ( getPairs ( a ) ) ; } }
public 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 isDigitSumOne ( int nm ) { while ( nm > 9 ) { int sum_digit = 0 ; while ( nm > 0 ) { int digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = nm / 10 ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } static void printValidNums ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String arg [ ] ) { int l = 10 , r = 100 ; printValidNums ( l , r ) ; } }
class GfG { static int calcNodes ( int N , int I ) { int result = 0 ; result = I * ( N - 1 ) + 1 ; return result ; } public static void main ( String [ ] args ) { int N = 5 , I = 2 ; System . out . println ( " Leaf ▁ nodes ▁ = ▁ " + calcNodes ( N , I ) ) ; } }
class GFG { static long mod = 1000000007 ; static long [ ] arr = new long [ 1000001 ] ; static void cal_factorial ( ) { arr [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; i ++ ) { arr [ i ] = ( ( arr [ i - 1 ] * i ) % mod ) ; } } static long mod_exponent ( long num , long p ) { if ( p == 0 ) return 1 ; if ( ( p & 1 ) != 0 ) { return ( ( num % mod ) * ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ) % mod ; } else return ( mod_exponent ( ( num * num ) % mod , p / 2 ) ) % mod ; } static long getCount ( long x , long y ) { long ans = arr [ ( int ) x + ( int ) y - 1 ] ; ans *= mod_exponent ( arr [ ( int ) x ] , mod - 2 ) ; ans %= mod ; ans *= mod_exponent ( arr [ ( int ) y ] , mod - 2 ) ; ans %= mod ; ans *= ( x - y ) ; ans %= mod ; return ans ; } public static void main ( String [ ] args ) { cal_factorial ( ) ; long x = 3 , y = 1 ; System . out . println ( getCount ( x , y ) ) ; } }
import java . io . * ; class GFG { static void findValues ( int a , int b ) { if ( ( a - b ) % 2 == 1 ) { System . out . println ( " - 1" ) ; return ; } System . out . println ( ( ( a - b ) / 2 ) + " ▁ " + ( ( a + b ) / 2 ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 8 ; findValues ( a , b ) ; } }
class GFG { static class Data { double x , y ; public Data ( double x , double y ) { super ( ) ; this . x = x ; this . y = y ; } } ; static double inv_interpolate ( Data [ ] d , int n , double y ) { double x = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { double xi = d [ i ] . x ; for ( j = 0 ; j < n ; j ++ ) { if ( j != i ) { xi = xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ; } } x += xi ; } return x ; } public static void main ( String [ ] args ) { Data [ ] d = { new Data ( 1.27 , 2.3 ) , new Data ( 2.25 , 2.95 ) , new Data ( 2.5 , 3.5 ) , new Data ( 3.6 , 5.1 ) } ; int n = 4 ; double y = 4.5 ; System . out . printf ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : ▁ % .5f " , inv_interpolate ( d , n , y ) ) ; } }
class GFG { static double mixtureConcentration ( int n , int [ ] p ) { double res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res += p [ i ] ; res /= n ; return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 20 , 20 } ; int n = arr . length ; System . out . println ( String . format ( " % .4f " , mixtureConcentration ( n , arr ) ) ) ; } }
class GFG { static int MAX = ( int ) ( 1E5 + 5 ) ; static long MOD = ( long ) ( 1E9 + 7 ) ; static boolean check ( long num , long a , long b ) { while ( num > 0 ) { long rem = num % 10 ; num /= 10 ; if ( rem != a && rem != b ) return false ; } return true ; } static long power ( long x , long y ) { long ans = 1 ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) ans = ( ans * x ) % MOD ; y >>= 1 ; x = ( x * x ) % MOD ; } return ans % MOD ; } static long modInverse ( long x ) { return power ( x , MOD - 2 ) ; } static long countNumbers ( long n , long a , long b ) { long [ ] fact = new long [ MAX ] ; long [ ] inv = new long [ MAX ] ; long ans = 0 ; fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX ; i ++ ) { fact [ i ] = ( 1 * fact [ i - 1 ] * i ) ; fact [ i ] %= MOD ; } inv [ MAX - 1 ] = modInverse ( fact [ MAX - 1 ] ) ; for ( int i = MAX - 2 ; i >= 0 ; i -- ) { inv [ i ] = ( inv [ i + 1 ] * ( i + 1 ) ) ; inv [ i ] %= MOD ; } if ( a < b ) { long x = a ; a = b ; b = x ; } for ( long s = n ; s <= 9 * n ; s ++ ) { if ( ! check ( s , a , b ) ) continue ; if ( s < n * b || ( s - n * b ) % ( a - b ) != 0 ) continue ; int numDig = ( int ) ( ( s - n * b ) / ( a - b ) ) ; if ( numDig > n ) continue ; long curr = fact [ ( int ) n ] ; curr = ( curr * inv [ numDig ] ) % MOD ; curr = ( curr * inv [ ( int ) n - numDig ] ) % MOD ; ans = ( ans + curr ) % MOD ; } return ans ; } public static void main ( String [ ] args ) { long n = 3 , a = 1 , b = 3 ; System . out . println ( countNumbers ( n , a , b ) ) ; } }
import java . io . * ; class GFG { static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( m , 0.5 ) - ( int ) Math . pow ( n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . println ( " Count ▁ is ▁ " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ; } }
class GFG { static int Square ( int row , int column , int moves ) { int a = 0 , b = 0 , c = 0 , d = 0 , total = 0 ; a = row - moves ; b = row + moves ; c = column - moves ; d = column + moves ; if ( a < 1 ) a = 1 ; if ( c < 1 ) c = 1 ; if ( b > 8 ) b = 8 ; if ( d > 8 ) d = 8 ; total = ( b - a + 1 ) * ( d - c + 1 ) - 1 ; return total ; } public static void main ( String [ ] args ) { int R = 4 , C = 5 , M = 2 ; System . out . println ( Square ( R , C , M ) ) ; } }
class GFG { static int findNumber ( int n , int m ) { int num = ( m - 1 ) * 9 + n ; return num ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( findNumber ( n , m ) ) ; } }
import java . util . * ; class GFG { static boolean checkTriangularSumRepresentation ( int n ) { HashSet < Integer > tri = new HashSet < > ( ) ; int i = 1 ; while ( true ) { int x = i * ( i + 1 ) / 2 ; if ( x >= n ) { break ; } tri . add ( x ) ; i ++ ; } for ( Integer tm : tri ) { if ( tri . contains ( n - tm ) && ( n - tm ) != ( int ) tri . toArray ( ) [ tri . size ( ) - 1 ] ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 24 ; if ( checkTriangularSumRepresentation ( n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static int digitsCount ( int n ) { int len = 0 ; while ( n > 0 ) { len ++ ; n /= 10 ; } return len ; } static int absoluteFirstLast ( int n , int x ) { int i = 0 , mod = 1 ; while ( i < x ) { mod *= 10 ; i ++ ; } int last = n % mod ; int len = digitsCount ( n ) ; while ( len != x ) { n /= 10 ; len -- ; } int first = n ; return Math . abs ( first - last ) ; } public static void main ( String args [ ] ) { int n = 21546 , x = 2 ; System . out . println ( absoluteFirstLast ( n , x ) ) ; } }
class GFG { static void make_sequence ( int N ) { int [ ] arr = new int [ N + 1 ] ; int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 1 ) arr [ i ] = 1 ; else arr [ i ] = 2 ; sum += arr [ i ] ; } if ( sum % 2 == 1 ) arr [ 2 ] = 3 ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 9 ; make_sequence ( N ) ; } }
class GFG { static int countOdd ( int L , int R ) { int N = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) N ++ ; return N ; } public static void main ( String [ ] args ) { int L = 3 , R = 7 ; int odds = countOdd ( L , R ) ; int evens = ( R - L + 1 ) - odds ; System . out . println ( " Count ▁ of ▁ odd ▁ numbers ▁ is ▁ " + odds ) ; System . out . println ( " Count ▁ of ▁ even ▁ numbers ▁ is ▁ " + evens ) ; } }
class gfg { static int getMinCost ( int n , int m ) { int cost = ( n - 1 ) * m + ( m - 1 ) * n ; return cost ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 ; System . out . println ( getMinCost ( n , m ) ) ; } }
import java . io . * ; class GFG { static void minOperation ( int n , int k , int a [ ] ) { for ( int i = k ; i < n ; i ++ ) { if ( a [ i ] != a [ k - 1 ] ) System . out . println ( - 1 ) ; } for ( int i = k - 2 ; i > - 1 ; i -- ) { if ( a [ i ] != a [ k - 1 ] ) System . out . println ( i + 1 ) ; } } public static void main ( String [ ] args ) { int n = 5 ; int k = 3 ; int a [ ] = { 2 , 1 , 1 , 1 , 1 } ; minOperation ( n , k , a ) ; } }
class GFG { static boolean digitWell ( int n , int m , int k ) { int cnt = 0 ; while ( n > 0 ) { if ( n % 10 == m ) ++ cnt ; n /= 10 ; } return cnt == k ; } static int findInt ( int n , int m , int k ) { int i = n + 1 ; while ( true ) { if ( digitWell ( i , m , k ) ) return i ; i ++ ; } } public static void main ( String [ ] args ) { int n = 111 , m = 2 , k = 2 ; System . out . println ( findInt ( n , m , k ) ) ; } }
class GFG { static int binomialCoeff ( int n , int k ) { if ( k > n ) return 0 ; int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int N = 5 , M = 1 ; int P = ( N * ( N - 1 ) ) / 2 ; System . out . println ( binomialCoeff ( P , M ) ) ; } }
class GFG { static void generateSequence ( int n , int g ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i * g + " ▁ " ) ; ; } public static void main ( String [ ] args ) { int n = 6 , g = 5 ; generateSequence ( n , g ) ; } }
import java . util . * ; class GFG { static int MAX = 1000 ; static int [ ] f = new int [ MAX ] ; static int fib ( int n ) { if ( n == 0 ) return 0 ; if ( n == 1 n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] != 0 ) return f [ n ] ; int k = 0 ; if ( ( n & 1 ) != 0 ) k = ( n + 1 ) / 2 ; else k = n / 2 ; if ( ( n & 1 ) != 0 ) f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) ; else f [ n ] = ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findLCMFibonacci ( int a , int b ) { return ( fib ( a ) * fib ( b ) ) / fib ( gcd ( a , b ) ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 12 ; System . out . println ( findLCMFibonacci ( a , b ) ) ; } }
class GFG { static String isEvenOrOdd ( int L , int R ) { int oddCount = ( R - L ) / 2 ; if ( R % 2 == 1 L % 2 == 1 ) oddCount ++ ; if ( oddCount % 2 == 0 ) return " Even " ; else return " Odd " ; } public static void main ( String [ ] args ) { int L = 5 , R = 15 ; System . out . println ( isEvenOrOdd ( L , R ) ) ; } }
class GFG { static int trailing_zeros ( int N ) { int count_of_two = 0 , count_of_five = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { int val = i ; while ( val % 2 == 0 && val > 0 ) { val /= 2 ; count_of_two += i ; } while ( val % 5 == 0 && val > 0 ) { val /= 5 ; count_of_five += i ; } } int ans = Math . min ( count_of_two , count_of_five ) ; return ans ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . println ( trailing_zeros ( N ) ) ; } }
import java . util . * ; class GFG { static int limit = 1000000000 ; static int MAX_PRIME = 2000000 ; static int MAX = 1000000 ; static int I_MAX = 50000 ; static HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; static int [ ] b = new int [ MAX ] ; static int [ ] p = new int [ MAX ] ; static int j = 0 ; static boolean [ ] prime = new boolean [ MAX_PRIME + 1 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 0 ; i < MAX_PRIME + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { b [ j ++ ] = p ; } } } static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void printSeries ( int n ) { SieveOfEratosthenes ( MAX_PRIME ) ; int i , g , k , l , m , d ; int [ ] ar = new int [ I_MAX + 2 ] ; for ( i = 0 ; i < j ; i ++ ) { if ( ( b [ i ] * b [ i + 1 ] ) > limit ) break ; p [ i ] = b [ i ] ; mp . put ( b [ i ] * b [ i + 1 ] , 1 ) ; } d = 550 ; boolean flag = false ; for ( k = 2 ; ( k < d - 1 ) && ! flag ; k ++ ) { for ( m = 2 ; ( m < d ) && ! flag ; m ++ ) { for ( l = m + k ; l < d ; l += k ) { if ( ( ( b [ l ] * b [ l + k ] ) < limit ) && mp . containsKey ( b [ l ] * b [ l + k ] ) && mp . containsKey ( p [ i - 1 ] * b [ l ] ) && ( l + k ) < d && p [ i - 1 ] != b [ l + k ] && p [ i - 1 ] != b [ l ] && mp . get ( b [ l ] * b [ l + k ] ) != 1 ) { if ( mp . get ( p [ i - 1 ] * b [ l ] ) != 1 ) { p [ i ] = b [ l ] ; mp . put ( p [ i - 1 ] * b [ l ] , 1 ) ; i ++ ; } } if ( i >= I_MAX ) { flag = true ; break ; } } } } for ( i = 0 ; i < n ; i ++ ) ar [ i ] = p [ i ] * p [ i + 1 ] ; for ( i = 0 ; i < n - 1 ; i ++ ) System . out . print ( ar [ i ] + " ▁ " ) ; g = gcd ( ar [ n - 1 ] , ar [ n - 2 ] ) ; System . out . print ( g * 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; printSeries ( n ) ; } }
import java . util . * ; class GFG { static int MAX = 620000 ; static int [ ] prime = new int [ MAX ] ; static void Sieve ( ) { for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) { prime [ j ] = 1 ; } } } } static void printSequence ( int n ) { Sieve ( ) ; Vector < Integer > v = new Vector < Integer > ( ) ; Vector < Integer > u = new Vector < Integer > ( ) ; for ( int i = 13 ; i < MAX ; i ++ ) { if ( prime [ i ] == 0 ) { v . add ( i ) ; } } if ( n == 3 ) { System . out . print ( 6 + " ▁ " + 10 + " ▁ " + 15 ) ; return ; } int k ; for ( k = 0 ; k < n - 2 ; k ++ ) { if ( k % 3 == 0 ) { u . add ( v . get ( k ) * 6 ) ; } else if ( k % 3 == 1 ) { u . add ( v . get ( k ) * 15 ) ; } else { u . add ( v . get ( k ) * 10 ) ; } } k -- ; u . add ( v . get ( k ) * 7 ) ; u . add ( 7 * 11 ) ; u . set ( 0 , u . get ( 0 ) * 11 ) ; for ( int i = 0 ; i < u . size ( ) ; i ++ ) { System . out . print ( u . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printSequence ( n ) ; } }
class GFG { static int countOdd ( int [ ] arr , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd ++ ; } return odd ; } static int countValidPairs ( int [ ] arr , int n ) { int odd = countOdd ( arr , n ) ; return ( odd * ( odd - 1 ) ) / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countValidPairs ( arr , n ) ) ; } }
class GFG { static String isDivisible ( long n ) { long temp = n ; int sum = 0 ; while ( n != 0 ) { int k = ( int ) n % 10 ; sum += k ; n /= 10 ; } if ( temp % sum == 0 ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { long n = 123 ; System . out . println ( isDivisible ( n ) ) ; } }
class GFG { static String getResult ( int n ) { String st = String . valueOf ( n ) ; int sum = 0 ; for ( char i : st . toCharArray ( ) ) { sum = sum + ( int ) i ; } if ( n % sum == 0 ) return " Yes " ; else return " No " ; } public static void main ( String [ ] args ) { int n = 123 ; System . out . println ( getResult ( n ) ) ; } }
import java . io . * ; class GFG { static int digitProduct ( int n ) { int prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = n / 10 ; } return prod ; } static void printMultiplicativePrimes ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printMultiplicativePrimes ( n ) ; } }
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static boolean coPrime ( int n1 , int n2 ) { if ( gcd ( n1 , n2 ) == 1 ) return true ; else return false ; } static int largestCoprime ( int N ) { int half = ( int ) ( N / 2 ) ; while ( coPrime ( N , half ) == false ) half -- ; return half ; } public static void main ( String args [ ] ) { int n = 50 ; System . out . println ( largestCoprime ( n ) ) ; } }
class GfG { static int largestCoprime ( int N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( largestCoprime ( n ) ) ; } }
class GFG { static void printSafePrimes ( int n ) { int prime [ ] = new int [ n + 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = 0 ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] != 0 ) { int temp = ( 2 * i ) + 1 ; if ( temp <= n && prime [ temp ] != 0 ) prime [ temp ] = 2 ; } } for ( int i = 5 ; i <= n ; i ++ ) if ( prime [ i ] == 2 ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 20 ; printSafePrimes ( n ) ; } }
import java . util . Vector ; class GFG { static Vector < Integer > Divisors ( int x ) { int c = 0 ; Vector < Integer > v = new Vector < Integer > ( ) ; while ( x % 2 == 0 ) { c ++ ; x /= 2 ; } v . add ( c ) ; c = 0 ; while ( x % 3 == 0 ) { c ++ ; x /= 3 ; } v . add ( c ) ; c = 0 ; while ( x % 7 == 0 ) { c ++ ; x /= 7 ; } v . add ( c ) ; v . add ( x ) ; return v ; } static int MinOperations ( int a , int b ) { Vector < Integer > va = Divisors ( a ) ; Vector < Integer > vb = Divisors ( b ) ; if ( va . get ( 3 ) != vb . get ( 3 ) ) { return - 1 ; } int minOperations = Math . abs ( va . get ( 0 ) - vb . get ( 0 ) ) + Math . abs ( va . get ( 1 ) - vb . get ( 1 ) ) + Math . abs ( va . get ( 2 ) - vb . get ( 2 ) ) ; return minOperations ; } public static void main ( String [ ] args ) { int a = 14 , b = 28 ; System . out . println ( MinOperations ( a , b ) ) ; } }
class GFG { static int largestOddDigit ( int n ) { int maxOdd = - 1 ; while ( n > 0 ) { int digit = n % 10 ; if ( digit % 2 == 1 && digit > maxOdd ) maxOdd = digit ; n = n / 10 ; } return maxOdd ; } static int getProduct ( int n ) { int maxOdd = largestOddDigit ( n ) ; if ( maxOdd == - 1 ) return - 1 ; return ( n * maxOdd ) ; } public static void main ( String [ ] args ) { int n = 12345 ; System . out . println ( getProduct ( n ) ) ; } }
class GFG { static boolean isPerfectSquare ( int num ) { long left = 1 , right = num ; while ( left <= right ) { long mid = ( left + right ) / 2 ; if ( mid * mid == num ) { return true ; } if ( mid * mid < num ) { left = mid + 1 ; } else { right = mid - 1 ; } } return false ; } public static void main ( String [ ] args ) { int x = 2500 ; if ( isPerfectSquare ( x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isStrongPrime ( 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 = 11 ; if ( isStrongPrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printNum ( int n , int k ) { String str = Integer . toBinaryString ( n ) ; int x = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) == '1' ) x ++ ; if ( k < x k > n ) { System . out . println ( " - 1" ) ; return ; } PriorityQueue < Integer > pq = new PriorityQueue < > ( Comparator . reverseOrder ( ) ) ; int two = 1 ; while ( n > 0 ) { if ( ( n & 1 ) == 1 ) pq . add ( two ) ; two *= 2 ; n = n >> 1 ; } while ( pq . size ( ) < k ) { int el = pq . poll ( ) ; pq . add ( el / 2 ) ; pq . add ( el / 2 ) ; } int ind = 0 ; while ( ind < k ) { System . out . print ( pq . poll ( ) + " ▁ " ) ; ind ++ ; } } public static void main ( String [ ] args ) { int n = 9 , k = 4 ; printNum ( n , k ) ; } }
class GFG { static int n = 1000002 ; static int [ ] phi = new int [ n + 2 ] ; static int [ ] ans = new int [ n + 2 ] ; static void ETF ( ) { for ( int i = 1 ; i <= n ; i ++ ) { phi [ i ] = i ; } for ( int i = 2 ; i <= n ; i ++ ) { if ( phi [ i ] == i ) { phi [ i ] = i - 1 ; for ( int j = 2 * i ; j <= n ; j += i ) { phi [ j ] = ( phi [ j ] * ( i - 1 ) ) / i ; } } } } static int LcmSum ( int m ) { ETF ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = i ; j <= n ; j += i ) { ans [ j ] += ( i * phi [ i ] ) ; } } int answer = ans [ m ] ; answer = ( answer + 1 ) * m ; answer = answer / 2 ; return answer ; } public static void main ( String [ ] args ) { int m = 5 ; System . out . println ( LcmSum ( m ) ) ; } }
import java . io . * ; class GFG { static int setBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int setbits_x = setBits ( a [ i ] ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int setbits_y = setBits ( a [ j ] ) ; int setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; System . out . println ( countPairs ( a , n ) ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findPossibleDerivables ( int arr [ ] , int n , int D , int A , int B ) { int gcdAB = gcd ( A , B ) ; int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( Math . abs ( arr [ i ] - D ) % gcdAB ) == 0 ) { counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 7 , 13 } ; int n = arr . length ; int D = 5 , A = 4 , B = 2 ; System . out . println ( findPossibleDerivables ( arr , n , D , A , B ) ) ; int a [ ] = { 1 , 2 , 3 } ; n = a . length ; D = 6 ; A = 3 ; B = 2 ; System . out . println ( findPossibleDerivables ( a , n , D , A , B ) ) ; } }
class GFG { static int calSum ( int n ) { return ( n * ( 2 * n * n * n + 12 * n * n + 25 * n + 21 ) ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( calSum ( n ) ) ; } }
import java . io . * ; class GFG { static void getElements ( int a , int arr [ ] , int n ) { int elements [ ] = new int [ n + 1 ] ; elements [ 0 ] = a ; for ( int i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( int i = 0 ; i < n + 1 ; i ++ ) System . out . print ( elements [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 2 , 6 , 1 } ; int n = arr . length ; int a = 5 ; getElements ( a , arr , n ) ; } }
public class HelloWorld { static int distinctDigitSum ( int n ) { boolean used [ ] = new boolean [ 10 ] ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; if ( ! used [ digit ] ) { used [ digit ] = true ; sum += digit ; } n = n / 10 ; } return sum ; } static String checkSum ( int m , int n ) { int sumM = distinctDigitSum ( m ) ; int sumN = distinctDigitSum ( n ) ; if ( sumM == sumN ) return " YES " ; return " NO " ; } public static void main ( String [ ] args ) { int m = 2452 , n = 9222 ; System . out . println ( checkSum ( m , n ) ) ; } }
import java . util . * ; class solution { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countPairs ( int a [ ] , int n ) { int count = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m . get ( a [ i ] ^ a [ j ] ) == null ) { m . put ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } public static void main ( String args [ ] ) { int a [ ] = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = a . length ; System . out . println ( countPairs ( a , n ) ) ; } }
import java . util . * ; public class GFG { final static int MAXN = 1000001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int sumFactors ( int arr [ ] , int n ) { sieve ( ) ; Map map = new HashMap ( ) ; for ( int i = 0 ; i < MAXN ; ++ i ) map . put ( i , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) map . put ( arr [ i ] , 1 ) ; int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num != 1 && ( int ) ( map . get ( spf [ num ] ) ) == 1 ) { num /= spf [ num ] ; } if ( num == 1 ) sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 11 , 55 , 25 , 100 } ; int n = arr . length ; System . out . println ( sumFactors ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int doubleFactorial ( int n ) { int fact = 1 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { fact = fact * i ; } return fact ; } static int hermiteNumber ( int n ) { if ( n % 2 == 1 ) return 0 ; else { int number = ( int ) ( Math . pow ( 2 , n / 2 ) ) * doubleFactorial ( n - 1 ) ; if ( ( n / 2 ) % 2 == 1 ) number = number * - 1 ; return number ; } } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( hermiteNumber ( n ) ) ; } }
import java . util . * ; class GFG { static int N = 1000005 ; static long MAX = ( long ) 1e18 ; static Vector < Long > powers = new Vector < > ( ) ; static TreeSet < Long > squares = new TreeSet < > ( ) ; static TreeSet < Long > s = new TreeSet < > ( ) ; static void powersPrecomputation ( ) { for ( long i = 2 ; i < N ; i ++ ) { squares . add ( i * i ) ; if ( squares . contains ( i ) ) continue ; long temp = i ; while ( i * i <= MAX / temp ) { temp *= ( i * i ) ; s . add ( temp ) ; } } for ( long x : s ) powers . add ( x ) ; } static long calculateAnswer ( long L , long R ) { powersPrecomputation ( ) ; long perfectSquares = ( long ) ( Math . floor ( Math . sqrt ( R ) ) - Math . floor ( Math . sqrt ( L - 1 ) ) ) ; long high = Collections . binarySearch ( powers , R ) ; long low = Collections . binarySearch ( powers , L ) ; long perfectPowers = perfectSquares + ( high - low ) ; long ans = ( R - L + 1 ) - perfectPowers ; return ans ; } public static void main ( String [ ] args ) { long L = 13 , R = 20 ; System . out . println ( calculateAnswer ( L , R ) ) ; } }
import java . util . * ; class solution { static long getSum ( int n , int d ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 10 == d ) sum += i ; } return sum ; } public static void main ( String args [ ] ) { int n = 30 , d = 3 ; System . out . println ( getSum ( n , d ) ) ; } }
import java . util . * ; class solution { static void printThreeParts ( int N ) { if ( N % 3 == 0 ) System . out . println ( " x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " + ( N - 2 ) ) ; else System . out . println ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " + ( N - 3 ) ) ; } public static void main ( String args [ ] ) { int N = 10 ; printThreeParts ( N ) ; } }
public class GFG { static int countDigitsToBeRemoved ( int N , int K ) { String s = Integer . toString ( N ) ; int res = 0 ; int f_zero = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( K == 0 ) return res ; if ( s . charAt ( i ) == '0' ) { f_zero = 1 ; K -- ; } else res ++ ; } if ( K == 0 ) return res ; else if ( f_zero == 1 ) return s . length ( ) - 1 ; return - 1 ; } public static void main ( String [ ] args ) { int N = 10904025 ; int K = 2 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; N = 1000 ; K = 5 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; N = 23985 ; K = 2 ; System . out . println ( countDigitsToBeRemoved ( N , K ) ) ; } }
import java . util . * ; class GFG { static boolean isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static int largestPalindrome ( int [ ] A , int n ) { int currentMax = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] > currentMax && isPalindrome ( A [ i ] ) ) currentMax = A [ i ] ; } return currentMax ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 232 , 54545 , 999991 } ; int n = A . length ; System . out . println ( largestPalindrome ( A , n ) ) ; } }
class GFG { static boolean isPrime ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static int getDifference ( int N ) { if ( N == 0 ) return 2 ; else if ( N == 1 ) return 1 ; else if ( isPrime ( N ) ) return 0 ; int aboveN = - 1 , belowN = - 1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPrime ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPrime ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; return Math . min ( diff1 , diff2 ) ; } public static void main ( String args [ ] ) { int N = 25 ; System . out . println ( getDifference ( N ) ) ; } }
public class GFG { static boolean check ( int arr [ ] , int x , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double y = Math . sqrt ( arr [ i ] ) ; if ( Math . floor ( y ) == Math . ceil ( y ) ) { sum += arr [ i ] ; } } if ( sum % x == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 9 , 10 } ; int n = arr . length ; int x = 13 ; if ( check ( arr , x , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class Main { static int MAX = ( int ) 1e5 ; public static int findLargestDivisor ( int n ) { int m = n ; Set < Integer > s = new HashSet < Integer > ( ) ; s . add ( 1 ) ; s . add ( n ) ; for ( int i = 2 ; i < ( int ) Math . sqrt ( n ) + 1 ; i ++ ) { if ( n % i == 0 ) { s . add ( n / i ) ; s . add ( i ) ; while ( m % i == 0 ) m /= i ; } } if ( m > 1 ) s . add ( m ) ; List < Integer > l = new ArrayList < Integer > ( s ) ; Collections . sort ( l ) ; Collections . reverse ( l ) ; Vector < Integer > vec = new Vector < Integer > ( ) ; for ( int i = 2 ; i <= MAX ; i ++ ) vec . add ( i * i ) ; for ( int d : l ) { int divi = 0 ; for ( int j = 0 ; j < vec . size ( ) && vec . get ( j ) <= d ; j ++ ) { if ( d % vec . get ( j ) == 0 ) { divi = 1 ; break ; } } if ( divi == 0 ) return d ; } return 0 ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( findLargestDivisor ( n ) ) ; n = 97 ; System . out . println ( findLargestDivisor ( n ) ) ; } }
import java . util . * ; class Solution { static int NCR ( int n , int r ) { int numerator = 1 ; int denominator = 1 ; while ( r > 0 ) { numerator *= n ; denominator *= r ; n -- ; r -- ; } return ( numerator / denominator ) ; } static int findWays ( int arr [ ] , int n ) { HashMap < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; if ( n < 4 ) return 0 ; for ( int i = 0 ; i < n ; i ++ ) { count . put ( arr [ i ] , ( count . get ( arr [ i ] ) == null ? 0 int ) count . get ( arr [ i ] ) ) ) ; } int remaining_choices = 4 ; int ans = 1 ; Iterator hmIterator = count . entrySet ( ) . iterator ( ) ; while ( hmIterator . hasNext ( ) ) { Map . Entry mapElement = ( Map . Entry ) hmIterator . next ( ) ; int number = ( int ) mapElement . getKey ( ) ; int frequency = ( int ) mapElement . getValue ( ) ; int toSelect = Math . min ( remaining_choices , frequency ) ; ans = ans * NCR ( frequency , toSelect ) ; remaining_choices -= toSelect ; if ( remaining_choices == 0 ) { break ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 3 , 3 , 5 } ; int n = arr . length ; int maxQuadrupleWays = findWays ( arr , n ) ; System . out . print ( maxQuadrupleWays ) ; } }
class OddPosition { public static long getFinalElement ( long n ) { long finalNum ; for ( finalNum = 2 ; finalNum * 2 <= n ; finalNum *= 2 ) ; return finalNum ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . println ( getFinalElement ( N ) ) ; } }
import java . io . * ; class GFG { static void MinimumMaximumPairs ( int n , int m ) { int max_pairs = ( ( n - m + 1 ) * ( n - m ) ) / 2 ; int min_pairs = m * ( ( ( n - m ) / m + 1 ) * ( ( n - m ) / m ) ) / 2 + ( int ) Math . ceil ( ( double ) ( ( n - m ) / ( double ) ( m ) ) ) * ( ( n - m ) % m ) ; System . out . println ( " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + min_pairs ) ; System . out . println ( " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + max_pairs ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 2 ; MinimumMaximumPairs ( n , m ) ; } }
public class GFG { static void findGreater ( int a , int b ) { double x = ( double ) a * ( double ) ( Math . log ( ( double ) ( b ) ) ) ; double y = ( double ) b * ( double ) ( Math . log ( ( double ) ( a ) ) ) ; if ( y > x ) { System . out . println ( " a ^ b ▁ is ▁ greater " ) ; } else if ( y < x ) { System . out . println ( " b ^ a ▁ is ▁ greater " ) ; } else { System . out . println ( " Both ▁ are ▁ equal " ) ; } } public static void main ( String [ ] args ) { int a = 3 , b = 5 , c = 2 , d = 4 ; findGreater ( a , b ) ; findGreater ( c , d ) ; } }
import java . io . * ; class GFG { static long m = 1000000007 ; static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long modexp ( long x , long n ) { if ( n == 0 ) { return 1 ; } else if ( n % 2 == 0 ) { return modexp ( ( x * x ) % m , n / 2 ) ; } else { return ( x * modexp ( ( x * x ) % m , ( n - 1 ) / 2 ) % m ) ; } } static long getFractionModulo ( long a , long b ) { long c = gcd ( a , b ) ; a = a / c ; b = b / c ; long d = modexp ( b , m - 2 ) ; long ans = ( ( a % m ) * ( d % m ) ) % m ; return ans ; } public static void main ( String [ ] args ) { long a = 2 , b = 6 ; System . out . println ( getFractionModulo ( a , b ) ) ; } }
import java . io . * ; class GFG { static int maxPrimeFactors ( int n ) { int num = n ; int maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n /= 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) { maxPrime = n ; } int sum = maxPrime + num ; return sum ; } public static void main ( String [ ] args ) { int n = 19 ; System . out . println ( maxPrimeFactors ( n ) ) ; } }
import java . io . * ; class GFG { static int sumOfDigits ( int n ) { int res = 0 ; while ( n > 0 ) { res += n % 10 ; n /= 10 ; } return res ; } static int findNumber ( int n ) { int i = n - 1 ; while ( i > 0 ) { if ( sumOfDigits ( i ) > sumOfDigits ( n ) ) return i ; i -- ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 824 ; System . out . println ( findNumber ( n ) ) ; } }
import java . io . * ; class GFG { static int findNth ( int N ) { int b = 14 ; int i ; for ( i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) b = b * 2 ; else b = b - 8 ; } return b ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( findNth ( N ) ) ; } }
public class GFG { final static int N = 4 ; static void predictMatrix ( int arr [ ] [ ] , int range1a , int range1b , int range0a , int range0b , int K , int b [ ] [ ] ) { int c = 0 ; while ( K != 0 ) { K -- ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { c = 0 ; if ( i > 0 && arr [ i - 1 ] [ j ] == 1 ) c ++ ; if ( j > 0 && arr [ i ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j > 0 && arr [ i - 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i < N - 1 && arr [ i + 1 ] [ j ] == 1 ) c ++ ; if ( j < N - 1 && arr [ i ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j < N - 1 && arr [ i + 1 ] [ j + 1 ] == 1 ) c ++ ; if ( i < N - 1 && j > 0 && arr [ i + 1 ] [ j - 1 ] == 1 ) c ++ ; if ( i > 0 && j < N - 1 && arr [ i - 1 ] [ j + 1 ] == 1 ) c ++ ; if ( arr [ i ] [ j ] == 1 ) { if ( c >= range1a && c <= range1b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } if ( arr [ i ] [ j ] == 0 ) { if ( c >= range0a && c <= range0b ) b [ i ] [ j ] = 1 ; else b [ i ] [ j ] = 0 ; } } } for ( int k = 0 ; k < N ; k ++ ) for ( int m = 0 ; m < N ; m ++ ) arr [ k ] [ m ] = b [ k ] [ m ] ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 } , { 0 , 0 , 1 , 0 } , { 0 , 1 , 0 , 1 } } ; int range1a = 2 , range1b = 2 ; int range0a = 2 , range0b = 3 ; int K = 3 ; int b [ ] [ ] = new int [ N ] [ N ] ; predictMatrix ( arr , range1a , range1b , range0a , range0b , K , b ) ; for ( int i = 0 ; i < N ; i ++ ) { System . out . println ( ) ; for ( int j = 0 ; j < N ; j ++ ) System . out . print ( b [ i ] [ j ] + " ▁ " ) ; } } }
import java . io . * ; class GFG { static int countMoves ( int n ) { int ct = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ct += i * ( n - i ) ; ct += n ; return ct ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countMoves ( n ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static double countNumbers ( int N ) { return ( Math . pow ( 10 , N ) - 1 ) - ( Math . pow ( 10 , N ) - Math . pow ( 8 , N ) ) / 2 ; } static public void main ( String args [ ] ) { int n = 2 ; System . out . println ( countNumbers ( n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int N = 3 ; static double calcDeterminant ( int arr [ ] ) { double determinant = 0 ; for ( int i = 0 ; i < N ; i ++ ) { determinant += Math . pow ( arr [ i ] , 3 ) ; } determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] ; return determinant ; } static public void main ( String args [ ] ) { int [ ] arr = { 4 , 5 , 3 } ; System . out . println ( calcDeterminant ( arr ) ) ; } }
import java . io . * ; class GFG { static int minimumMoves ( int k , int l , int r ) { int count = r - l + 1 ; if ( count % k == 0 ) return 0 ; return ( k - ( count % k ) ) ; } public static void main ( String [ ] args ) { int k = 3 , l = 10 , r = 10 ; System . out . print ( minimumMoves ( k , l , r ) ) ; } }
import java . io . * ; class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) ; return sum ; } static int sumEven ( int l , int r ) { return sumNatural ( r / 2 ) - sumNatural ( ( l - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . println ( " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + sumEven ( l , r ) ) ; } }
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 " ) ; } }
import java . util . * ; public class GFG { public static ArrayList < Integer > getPerfectSquares ( int n ) { ArrayList < Integer > perfectSquares = new ArrayList < > ( ) ; int current = 1 , i = 1 ; while ( current <= n ) { perfectSquares . add ( current ) ; current = ( int ) Math . pow ( ++ i , 2 ) ; } return perfectSquares ; } public static int maxPairSum ( int arr [ ] ) { int n = arr . length ; int max , secondMax ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } public static int countPairsWith ( int n , ArrayList < Integer > perfectSquares , HashSet < Integer > nums ) { int count = 0 ; for ( int i = 0 ; i < perfectSquares . size ( ) ; i ++ ) { int temp = perfectSquares . get ( i ) - n ; if ( temp > n && nums . contains ( temp ) ) count ++ ; } return count ; } public static int countPairs ( int arr [ ] ) { int i , n = arr . length ; int max = maxPairSum ( arr ) ; ArrayList < Integer > perfectSquares = getPerfectSquares ( max ) ; HashSet < Integer > nums = new HashSet < > ( ) ; for ( i = 0 ; i < n ; i ++ ) nums . add ( arr [ i ] ) ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectSquares , nums ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 9 , 10 , 20 } ; System . out . println ( countPairs ( arr ) ) ; } }
import java . io . * ; class GFG { static int findEle ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == sum - arr [ i ] ) return arr [ i ] ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; System . out . print ( findEle ( arr , n ) ) ; } }
class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) / 2 ; return sum ; } static int suminRange ( int l , int r ) { return sumNatural ( r ) - sumNatural ( l - 1 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . println ( " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + suminRange ( l , r ) ) ; } }
import java . io . * ; class GFG { static boolean divisibleBy3 ( String number ) { int sumOfDigit = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) sumOfDigit += number . charAt ( i ) - '0' ; if ( sumOfDigit % 3 == 0 ) return true ; return false ; } static boolean divisibleBy25 ( String number ) { if ( number . length ( ) < 2 ) return false ; int length = number . length ( ) ; int lastTwo = ( number . charAt ( length - 2 ) - '0' ) * 10 + ( number . charAt ( length - 1 ) - '0' ) ; if ( lastTwo % 25 == 0 ) return true ; return false ; } static boolean divisibleBy75 ( String number ) { if ( divisibleBy3 ( number ) && divisibleBy25 ( number ) ) return true ; return false ; } public static void main ( String [ ] args ) { String number = "754586672150" ; boolean divisible = divisibleBy75 ( number ) ; if ( divisible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int otherNumber ( int A , int Lcm , int Hcf ) { return ( Lcm * Hcf ) / A ; } public static void main ( String args [ ] ) { int A = 8 , Lcm = 8 , Hcf = 1 ; int result = otherNumber ( A , Lcm , Hcf ) ; System . out . println ( " B ▁ = ▁ " + result ) ; } }
import java . io . * ; class GFG { static float successiveChange ( int arr [ ] , int N ) { float var1 , var2 , result = 0 ; var1 = arr [ 0 ] ; var2 = arr [ 1 ] ; result = var1 + var2 + ( ( var1 * var2 ) / 100 ) ; for ( int i = 2 ; i < N ; i ++ ) result = result + arr [ i ] + ( ( result * arr [ i ] ) / 100 ) ; return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 10 } ; int N = arr . length ; float result = successiveChange ( arr , N ) ; System . out . println ( " Percentage ▁ change ▁ is ▁ = ▁ " + result + " ▁ % " ) ; } }
import java . io . * ; class GFG { static int minimumNumbers ( int n , int s ) { if ( ( s % n ) > 0 ) return s / n + 1 ; else return s / n ; } public static void main ( String [ ] args ) { int n = 5 ; int s = 11 ; System . out . println ( minimumNumbers ( n , s ) ) ; } }
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int sumAP ( int n , int d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static int sumMultiples ( int A , int B , int n ) { n -- ; int common = ( A * B ) / __gcd ( A , B ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } public static void main ( String [ ] args ) { int n = 100 , A = 5 , B = 10 ; System . out . println ( " Sum ▁ = ▁ " + sumMultiples ( A , B , n ) ) ; } }
public class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isPossible ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPossible ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int contiguousPerfectSquare ( int [ ] arr , int n ) { int a ; float b ; int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { b = ( float ) Math . sqrt ( arr [ i ] ) ; a = ( int ) b ; if ( a == b ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 75 , 4 , 64 , 121 , 25 } ; int n = arr . length ; System . out . print ( contiguousPerfectSquare ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int countPairs ( int n ) { int count = 0 ; for ( int x = 1 ; x < n ; x ++ ) { for ( int y = x + 1 ; y <= n ; y ++ ) { if ( ( y * x ) % ( y + x ) == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( countPairs ( n ) ) ; } }
import java . util . Scanner ; public class HelloWorld { public static float getSum ( int a , int n ) { float sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { sum += ( i / Math . pow ( a , i ) ) ; } return sum ; } public static void main ( String [ ] args ) { int a = 3 , n = 3 ; System . out . println ( getSum ( a , n ) ) ; } }
import java . io . * ; class GFG { static int getIndex ( int a [ ] , int n ) { if ( n == 1 ) return 0 ; int ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 7 , 9 , 8 , 7 } ; int n = a . length ; System . out . println ( getIndex ( a , n ) ) ; } }
import java . util . * ; class solution { static int getPosition ( int a [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] / m + ( a [ i ] % m ) ) ; } int ans = - 1 , max = - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( max < a [ i ] ) { max = a [ i ] ; ans = i ; } } return ans + 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 5 , 4 } ; int n = a . length ; int m = 2 ; System . out . println ( getPosition ( a , n , m ) ) ; } }
class GFG { static int calcFunction ( int n , int r ) { int finalDenominator = 1 ; int mx = Math . max ( r , n - r ) ; for ( int i = mx + 1 ; i <= n ; i ++ ) { int denominator = ( int ) Math . pow ( i , i ) ; int numerator = ( int ) Math . pow ( i - mx , i - mx ) ; finalDenominator = ( finalDenominator * denominator ) / numerator ; } return finalDenominator ; } public static void main ( String [ ] args ) { int n = 6 , r = 2 ; System . out . println ( "1 / " + calcFunction ( n , r ) ) ; } }
import java . util . * ; class Solution { static boolean isExists ( int a [ ] , int n ) { Map < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) == null ? 0 : freq . get ( a [ i ] ) + 1 ) ; sum += a [ i ] ; } if ( sum % 2 == 0 ) { if ( freq . get ( sum / 2 ) != null ) return true ; } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 1 , 2 , 2 } ; int n = a . length ; if ( isExists ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static Set < Integer > st = new HashSet < Integer > ( ) ; static void generateNumbers ( int n , int num , int a , int b ) { if ( num > 0 && num < n ) st . add ( num ) ; if ( num >= n ) return ; if ( num * 10 + a > num ) generateNumbers ( n , num * 10 + a , a , b ) ; generateNumbers ( n , num * 10 + b , a , b ) ; } static void printNumbers ( int n ) { for ( int i = 0 ; i <= 9 ; i ++ ) for ( int j = i + 1 ; j <= 9 ; j ++ ) generateNumbers ( n , 0 , i , j ) ; System . out . print ( " The ▁ numbers ▁ are : ▁ " ) ; System . out . print ( st ) ; st . clear ( ) ; } public static void main ( String args [ ] ) { int n = 12 ; printNumbers ( n ) ; } }
public class GFG { static int findNum ( int div [ ] , int rem [ ] , int N ) { int num = rem [ N - 1 ] ; for ( int i = N - 2 ; i >= 0 ; i -- ) { num = num * div [ i ] + rem [ i ] ; } return num ; } public static void main ( String [ ] args ) { int div [ ] = { 8 , 3 } ; int rem [ ] = { 2 , 2 } ; int N = div . length ; System . out . println ( findNum ( div , rem , N ) ) ; } }
import java . util . * ; class GFG { static String convert_to_words ( char [ ] num ) { int len = num . length ; if ( len == 0 len > 4 ) { return " " ; } String single_digits_temp [ ] = { " " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; String single_digits [ ] = { " " , " first " , " second " , " third " , " fourth " , " fifth " , " sixth " , " seventh " , " eighth " , " ninth " } ; String two_digits [ ] = { " " , " tenth " , " eleventh " , " twelfth " , " thirteenth " , " fourteenth " , " fifteenth " , " sixteenth " , " seventeenth " , " eighteenth " , " nineteenth " } ; String tens_multiple [ ] = { " " , " tenth " , " twentieth " , " thirtieth " , " fortieth " , " fiftieth " , " sixtieth " , " seventieth " , " eightieth " , " ninetieth " } ; String tens_power [ ] = { " hundred " , " thousand " } ; String word = " " ; if ( len == 1 ) { word += single_digits [ num [ 0 ] - '0' ] ; return word ; } int i = 0 , ctr = 0 ; String s = " ▁ " ; while ( i < len ) { if ( len >= 3 ) { if ( num [ i ] != '0' ) { word += single_digits_temp [ num [ i ] - '0' ] + " ▁ " ; word += tens_power [ len - 3 ] + " ▁ " ; ctr ++ ; } len -- ; num = Arrays . copyOfRange ( num , 1 , num . length ) ; } else { if ( ctr != 0 ) { s = " ▁ and ▁ " ; word = word . substring ( 0 , word . length ( ) - 1 ) ; } if ( num [ i + 1 ] == '0' ) if ( num [ i ] == '0' ) word = word + " th " ; else word += s + tens_multiple [ num [ i ] - '0' ] ; else if ( num [ i ] == '1' ) word += s + two_digits [ num [ i + 1 ] - '0' + 1 ] ; else { if ( num [ i ] != '0' ) word += s + tens_multiple [ num [ i ] - '0' ] . substring ( 0 , tens_multiple [ num [ i ] - '0' ] . length ( ) - 4 ) + " y ▁ " ; else word += s ; word += single_digits [ num [ i + 1 ] - '0' ] ; } i += 2 ; } if ( i == len ) { if ( word . charAt ( 0 ) == ' ▁ ' ) word = word . substring ( 1 , word . length ( ) ) ; } } return word ; } static void Aronsons_sequence ( int n ) { String str = " T ▁ is ▁ the ▁ " ; int ind = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isAlphabetic ( str . charAt ( i ) ) ) { ind += 1 ; } if ( str . charAt ( i ) == ' t ' || str . charAt ( i ) == ' T ' ) { n -= 1 ; str += convert_to_words ( String . valueOf ( ind ) . toCharArray ( ) ) + " , ▁ " ; System . out . print ( ind + " , ▁ " ) ; } if ( n == 0 ) break ; } } public static void main ( String [ ] args ) { int n = 6 ; Aronsons_sequence ( n ) ; } }
import java . util . * ; class Solution { static void niceIndices ( int A [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . put ( A [ i ] , ( m . get ( A [ i ] ) == null ) ? 0 : m . get ( A [ i ] ) + 1 ) ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . containsKey ( k ) ) { if ( ( A [ i ] == k && m . get ( k ) > 1 ) || ( A [ i ] != k ) ) System . out . print ( ( i + 1 ) + " ▁ " ) ; } } } } public static void main ( String args [ ] ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = A . length ; niceIndices ( A , n ) ; } }
import java . util . * ; class solution { static int countPieces ( int N ) { return 2 * N ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . println ( countPieces ( N ) ) ; } }
import java . util . * ; class solution { static long sumAP ( long n , long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static long sumMultiples ( long n ) { n -- ; return sumAP ( n , 3 ) + sumAP ( n , 7 ) - sumAP ( n , 21 ) ; } public static void main ( String args [ ] ) { long n = 24 ; System . out . println ( sumMultiples ( n ) ) ; } }
import java . io . * ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long powGCD ( long a , long n , long b ) { for ( int i = 0 ; i < n ; i ++ ) a = a * a ; return gcd ( a , b ) ; } public static void main ( String [ ] args ) { long a = 10 , b = 5 , n = 2 ; System . out . println ( powGCD ( a , n , b ) ) ; } }
import java . util . * ; class solution { static int lastCoordinate ( int n , int a , int b ) { return ( ( n + 1 ) / 2 ) * a - ( n / 2 ) * b ; } public static void main ( String args [ ] ) { int n = 3 , a = 5 , b = 2 ; System . out . println ( lastCoordinate ( n , a , b ) ) ; } }
public class GFG { static int findNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } public static void main ( String [ ] args ) { int N = 45 , K = 6 ; System . out . println ( " Smallest ▁ number ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " ▁ + ▁ K ▁ + ▁ " is " + findNum(N, K)); } }
import java . lang . * ; import java . util . * ; class GFG { static int findNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } public static void main ( String args [ ] ) { int N = 45 , K = 6 ; System . out . print ( " Largest ▁ number ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + N + " that is divisible by " + K + " ▁ is ▁ " + findNum ( N , K ) ) ; } }
public class GFG { static boolean isDivisiblePalindrome ( int n ) { int hash [ ] = new int [ 10 ] ; int digitSum = 0 ; while ( n != 0 ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n /= 10 ; } if ( digitSum % 3 != 0 ) return false ; int oddCount = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } public static void main ( String [ ] args ) { int n = 34734 ; System . out . print ( isDivisiblePalindrome ( n ) ) ; } }
import java . util . * ; class GFG { static int lcm ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int dfs ( int src , Vector < Integer > adj [ ] , boolean [ ] visited ) { visited [ src ] = true ; int count = 1 ; for ( int i = 0 ; i < adj [ src ] . size ( ) ; i ++ ) if ( ! visited [ adj [ src ] . get ( i ) ] ) count += dfs ( adj [ src ] . get ( i ) , adj , visited ) ; return count ; } static int findMinTime ( int arr [ ] , int P [ ] , int n ) { Vector < Integer > [ ] adj = new Vector [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adj [ arr [ i ] ] . add ( P [ i ] ) ; } boolean [ ] visited = new boolean [ n + 1 ] ; int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! visited [ i ] ) { ans = lcm ( ans , dfs ( i , adj , visited ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int P [ ] = { 3 , 2 , 1 } ; int n = arr . length ; System . out . print ( findMinTime ( arr , P , n ) ) ; } }
class GFG { static boolean productDivisible ( int n , int k ) { int product = 1 , position = 1 ; while ( n > 0 ) { if ( position % 2 == 0 ) { product *= n % 10 ; } n = n / 10 ; position ++ ; } if ( product % k == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 321922 ; int k = 3 ; if ( productDivisible ( n , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int npr ( int n , int r ) { int pnr = factorial ( n ) / factorial ( n - r ) ; return pnr ; } static int countPermutations ( int n , int r , int k ) { return factorial ( k ) * ( r - k + 1 ) * npr ( n - k , r - k ) ; } public static void main ( String [ ] args ) { int n = 8 ; int r = 5 ; int k = 2 ; System . out . println ( countPermutations ( n , r , k ) ) ; } }
class GFG { static int GIF ( double n ) { return ( int ) Math . floor ( n ) ; } public static void main ( String [ ] args ) { double n = 2.3 ; System . out . println ( GIF ( n ) ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact = fact * i ; return fact ; } static int ncr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } public static void main ( String [ ] args ) { int m = 3 , n = 4 , k = 5 ; int totalTriangles = ncr ( m + n + k , 3 ) - ncr ( m , 3 ) - ncr ( n , 3 ) - ncr ( k , 3 ) ; System . out . println ( totalTriangles ) ; } }
class GFG { static String isTriangleExists ( int a , int b , int c ) { if ( a != 0 && b != 0 && c != 0 && ( a + b + c ) == 180 ) if ( ( a + b ) >= c || ( b + c ) >= a || ( a + c ) >= b ) return " YES " ; else return " NO " ; else return " NO " ; } public static void main ( String [ ] args ) { int a = 50 , b = 60 , c = 70 ; System . out . println ( isTriangleExists ( a , b , c ) ) ; } }
import java . util . * ; class GFG { static int fact ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f = f * i ; return f ; } static int waysOfConsonants ( int size1 , int [ ] freq ) { int ans = fact ( size1 ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( i == 0 i == 4 i == 8 i == 14 i == 20 ) continue ; else ans = ans / fact ( freq [ i ] ) ; } return ans ; } static int waysOfVowels ( int size2 , int [ ] freq ) { return fact ( size2 ) / ( fact ( freq [ 0 ] ) * fact ( freq [ 4 ] ) * fact ( freq [ 8 ] ) * fact ( freq [ 14 ] ) * fact ( freq [ 20 ] ) ) ; } static int countWays ( String str ) { int [ ] freq = new int [ 200 ] ; for ( int i = 0 ; i < 200 ; i ++ ) freq [ i ] = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; int vowel = 0 , consonant = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) != ' a ' && str . charAt ( i ) != ' e ' && str . charAt ( i ) != ' i ' && str . charAt ( i ) != ' o ' && str . charAt ( i ) != ' u ' ) consonant ++ ; else vowel ++ ; } return waysOfConsonants ( consonant + 1 , freq ) * waysOfVowels ( vowel , freq ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( countWays ( str ) ) ; } }
import java . util . * ; public class GfG { private static int findX ( int n , int k ) { int r = n , v , u ; int m = ( int ) Math . sqrt ( k ) + 1 ; for ( int i = 2 ; i <= m && k > 1 ; i ++ ) { if ( i == m ) { i = k ; } for ( u = v = 0 ; k % i == 0 ; v ++ ) { k /= i ; } if ( v > 0 ) { int t = n ; while ( t > 0 ) { t /= i ; u += t ; } r = Math . min ( r , u / v ) ; } } return r ; } public static void main ( String args [ ] ) { int n = 5 ; int k = 2 ; System . out . println ( findX ( n , k ) ) ; } }
import java . io . * ; class GFG { static int fact ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) fact *= i ; return fact ; } static int ncr ( int n , int r ) { int ncr = fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; return ncr ; } static int ways ( int m , int w , int n , int k ) { int ans = 0 ; while ( m >= k ) { ans += ncr ( m , k ) * ncr ( w , n - k ) ; k += 1 ; } return ans ; } public static void main ( String [ ] args ) { int m , w , n , k ; m = 7 ; w = 6 ; n = 5 ; k = 3 ; System . out . println ( ways ( m , w , n , k ) ) ; } }
class gfg { static int square ( int n ) { return n * n ; } static int sum ( int n ) { if ( n == 0 ) return 0 ; if ( n % 2 == 1 ) { return square ( ( n + 1 ) / 2 ) + sum ( n / 2 ) ; } else { return square ( n / 2 ) + sum ( n / 2 ) ; } } static int oddDivSum ( int a , int b ) { return sum ( b ) - sum ( a - 1 ) ; } public static void main ( String [ ] args ) { int a = 3 , b = 9 ; System . out . println ( oddDivSum ( a , b ) ) ; } }
import java . io . * ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 32 ; System . out . println ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " + countBits ( n ) ) ; } }
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isAchillesNumber ( int n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 72 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; n = 36 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class GFG { static int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = 0 ; for ( int j = 1 ; j <= i ; ++ j ) { if ( i % j == 0 ) { ++ divCount ; } } if ( ( divCount % 2 ) != 0 ) { ++ res ; } } return res ; } public static void main ( String [ ] args ) { int a = 1 , b = 10 ; System . out . println ( OddDivCount ( a , b ) ) ; } }
class GFG { static int largestSquareFactor ( int num ) { int answer = 1 ; for ( int i = 2 ; i < Math . sqrt ( num ) ; ++ i ) { int cnt = 0 ; int j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( ( cnt & 1 ) != 0 ) { cnt -- ; answer *= Math . pow ( i , cnt ) ; } else { answer *= Math . pow ( i , cnt ) ; } } return answer ; } public static void main ( String args [ ] ) { int N = 420 ; System . out . println ( largestSquareFactor ( N ) ) ; } }
class GFG { static int largestPrimeFactor ( int n ) { int max = - 1 ; while ( n % 2 == 0 ) { max = 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { max = i ; n = n / i ; } } if ( n > 2 ) max = n ; return max ; } static boolean checkUnusual ( int n ) { int factor = largestPrimeFactor ( n ) ; if ( factor > Math . sqrt ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 14 ; if ( checkUnusual ( n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . io . * ; class GFG { static int Nth_Term ( int n ) { return ( int ) ( 3 * Math . pow ( n , 2 ) - n + 2 ) / ( 2 ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( Nth_Term ( N ) ) ; } }
class GFG { static void countPermutations ( int N , int B ) { int x = ( int ) Math . pow ( B , N ) ; int y = ( int ) Math . pow ( B , N - 1 ) ; System . out . println ( x - y ) ; } public static void main ( String [ ] args ) { int N = 6 ; int B = 4 ; countPermutations ( N , B ) ; } }
import java . util . * ; import java . util . Arrays ; import java . util . Collections ; class GFG { public static int calculateDifference ( int [ ] arr , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ max_val + 1 ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int P1 = 1 , P2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return Math . abs ( P2 - P1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . length ; System . out . println ( calculateDifference ( arr , n ) ) ; System . exit ( 0 ) ; } }
class GFG { static int count_odd_pair ( int n , int a [ ] ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } int ans = odd * even * 2 ; return ans ; } static int count_even_pair ( int odd_sum_pairs , int n ) { int total_pairs = ( n * ( n - 1 ) ) ; int ans = total_pairs - odd_sum_pairs ; return ans ; } public static void main ( String [ ] args ) { int n = 6 ; int [ ] a = { 2 , 4 , 5 , 9 , 1 , 8 } ; int odd_sum_pairs = count_odd_pair ( n , a ) ; int even_sum_pairs = count_even_pair ( odd_sum_pairs , n ) ; System . out . println ( " Even ▁ Sum ▁ Pairs ▁ = ▁ " + even_sum_pairs ) ; System . out . println ( " Odd ▁ Sum ▁ Pairs = ▁ " + odd_sum_pairs ) ; } }
class GFG { static int findSteps ( int n , int m , int a [ ] ) { int cur = 1 ; int steps = 0 ; for ( int i = 0 ; i < m ; i ++ ) { if ( a [ i ] >= cur ) steps += ( a [ i ] - cur ) ; else steps += ( n - cur + a [ i ] ) ; cur = a [ i ] ; } return steps ; } public static void main ( String [ ] args ) { int n = 3 , m = 3 ; int a [ ] = { 2 , 1 , 2 } ; System . out . println ( findSteps ( n , m , a ) ) ; } }
public class Improve { static void HexToBin ( char hexdec [ ] ) { int i = 0 ; while ( hexdec [ i ] != ' \u0000' ) { switch ( hexdec [ i ] ) { case '0' : System . out . print ( "0000" ) ; break ; case '1' : System . out . print ( "0001" ) ; break ; case '2' : System . out . print ( "0010" ) ; break ; case '3' : System . out . print ( "0011" ) ; break ; case '4' : System . out . print ( "0100" ) ; break ; case '5' : System . out . print ( "0101" ) ; break ; case '6' : System . out . print ( "0110" ) ; break ; case '7' : System . out . print ( "0111" ) ; break ; case '8' : System . out . print ( "1000" ) ; break ; case '9' : System . out . print ( "1001" ) ; break ; case ' A ' : case ' a ' : System . out . print ( "1010" ) ; break ; case ' B ' : case ' b ' : System . out . print ( "1011" ) ; break ; case ' C ' : case ' c ' : System . out . print ( "1100" ) ; break ; case ' D ' : case ' d ' : System . out . print ( "1101" ) ; break ; case ' E ' : case ' e ' : System . out . print ( "1110" ) ; break ; case ' F ' : case ' f ' : System . out . print ( "1111" ) ; break ; default : System . out . print ( " Invalid hexadecimal digit " + hexdec[i]); } i ++ ; } } public static void main ( String args [ ] ) { String s = "1AC5" ; char hexdec [ ] = new char [ 100 ] ; hexdec = s . toCharArray ( ) ; System . out . print ( " Equivalent Binary value is : "); try { HexToBin ( hexdec ) ; } catch ( ArrayIndexOutOfBoundsException e ) { System . out . print ( " " ) ; } } }
import java . io . * ; class GFG { static boolean isPowerOfTwo ( int x ) { return ( x > 0 && ( ! ( ( x & ( x - 1 ) ) > 0 ) ) ) ; } static void Count_pairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 5 , 8 , 16 , 128 } ; int n = a . length ; Count_pairs ( a , n ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = result * i ; return result ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } static int calculate_result ( int n ) { int result = 2 * nCr ( ( n - 2 ) , ( n / 2 - 1 ) ) ; return result ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; System . out . println ( calculate_result ( 2 * a ) ) ; System . out . print ( calculate_result ( 2 * b ) ) ; } }
public class GFG { static void isHalfReducible ( int arr [ ] , int n , int m ) { int frequencyHash [ ] = new int [ m + 1 ] ; int i ; for ( i = 0 ; i < frequencyHash . length ; i ++ ) frequencyHash [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { frequencyHash [ arr [ i ] % ( m + 1 ) ] ++ ; } for ( i = 0 ; i <= m ; i ++ ) { if ( frequencyHash [ i ] >= n / 2 ) break ; } if ( i <= m ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 16 , 32 , 3 , 12 } ; int n = arr . length ; int m = 7 ; isHalfReducible ( arr , n , m ) ; } }
import java . util . * ; class GFG { static Vector < Integer > arr = new Vector < Integer > ( ) ; static void generateDivisors ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) arr . add ( i ) ; { arr . add ( i ) ; arr . add ( n / i ) ; } } } } static double harmonicMean ( int n ) { generateDivisors ( n ) ; double sum = 0.0 ; int len = arr . size ( ) ; for ( int i = 0 ; i < len ; i ++ ) sum = sum + n / arr . get ( i ) ; sum = sum / n ; return arr . size ( ) / sum ; } static boolean isOreNumber ( int n ) { double mean = harmonicMean ( n ) ; if ( mean - Math . floor ( mean ) == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 28 ; if ( isOreNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int digitProduct ( int [ ] digits , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } static boolean isDistinct ( int N ) { String s = " " + N ; int len = s . length ( ) ; int [ ] digits = new int [ len ] ; ArrayList < Integer > products = new ArrayList < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = Integer . parseInt ( " " + s . charAt ( i ) ) ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . contains ( val ) ) return false ; else products . add ( val ) ; } } return true ; } public static void main ( String args [ ] ) { int N = 324 ; if ( isDistinct ( N ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
public class GFG { static double calculateAlternateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; double sum = Math . pow ( fibo [ 0 ] , 2 ) + Math . pow ( fibo [ 1 ] , 2 ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum -= fibo [ i ] ; else sum += fibo [ i ] ; } return sum ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . println ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateAlternateSum ( n ) ) ; } }
class GFG { static int getValue ( int n ) { int i = 0 , k = 1 ; while ( i < n ) { i = i + k ; k = k * 2 ; } return k / 2 ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( getValue ( n ) ) ; n = 1025 ; System . out . println ( getValue ( n ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 10000 ; static Vector < Integer > arr = new Vector < Integer > ( ) ; static void ulam ( ) { Set < Integer > s = new HashSet < Integer > ( ) ; arr . add ( 1 ) ; s . add ( 1 ) ; arr . add ( 2 ) ; s . add ( 2 ) ; for ( int i = 3 ; i < MAX ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( s . contains ( i - arr . get ( j ) ) && arr . get ( j ) != ( i - arr . get ( j ) ) ) count ++ ; if ( count > 2 ) break ; } if ( count == 2 ) { arr . add ( i ) ; s . add ( i ) ; } } } public static void main ( String [ ] args ) { ulam ( ) ; int n = 9 ; System . out . println ( arr . get ( n - 1 ) ) ; } }
class GFG { static long nthKyneaNumber ( int n ) { n = ( 1 << n ) + 1 ; n = n * n ; n = n - 2 ; return n ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( nthKyneaNumber ( n ) ) ; } }
class GFG { static boolean isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static boolean isProthNumber ( int n ) { int k = 1 ; while ( k < ( n / k ) ) { if ( n % k == 0 ) { if ( isPowerOfTwo ( n / k ) ) return true ; } k = k + 2 ; } return false ; } public static void main ( String [ ] args ) { int n = 25 ; if ( isProthNumber ( n - 1 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
public class AAA { static int get_last_two_digit ( long N ) { if ( N <= 10 ) { long ans = 0 , fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fac = fac * i ; ans += fac ; } return ( int ) ans % 100 ; } return 13 ; } public static void main ( String [ ] args ) { long N = 1 ; for ( N = 1 ; N <= 10 ; N ++ ) System . out . println ( " For ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + get_last_two_digit ( N ) ) ; } }
public class GFG { static boolean isProductEven ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 3 , 5 } ; int n = arr . length ; if ( isProductEven ( arr , n ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }
public class Improve { static int calculateSquareSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += ( fibo [ i ] * fibo [ i ] ) ; } return sum ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSquareSum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int MinimumMoves ( int a [ ] , int n , int x ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] <= a [ i - 1 ] ) { int p = ( a [ i - 1 ] - a [ i ] ) / x + 1 ; ans += p ; a [ i ] += p * x ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 3 , 2 } ; int x = 2 ; int n = arr . length ; System . out . println ( MinimumMoves ( arr , n , x ) ) ; } }
class GFG { static int SumOfDigits ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( str . charAt ( i ) - '0' ) ; return sum ; } static boolean Divisible ( String str , int n ) { if ( SumOfDigits ( str , n ) % 3 == 0 && str . charAt ( n - 1 ) == '0' ) return true ; return false ; } public static void main ( String [ ] args ) { String str = "263730746028908374890" ; int n = str . length ( ) ; if ( Divisible ( str , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
public class GFG { static boolean isPrime ( int k ) { if ( k <= 1 ) return false ; for ( int i = 2 ; i < k ; i ++ ) if ( k % i == 0 ) return false ; return true ; } static int check ( int num , int k ) { int flag = 1 ; for ( int i = 2 ; i < k ; i ++ ) { if ( num % i == 0 ) flag = 0 ; } if ( flag == 1 ) { if ( num % k == 0 ) return 1 ; else return 0 ; } else return 0 ; } static int findCount ( int a , int b , int k ) { int count = 0 ; if ( ! isPrime ( k ) ) return 0 ; else { int ans ; for ( int i = a ; i <= b ; i ++ ) { ans = check ( i , k ) ; if ( ans == 1 ) count ++ ; else continue ; } } return count ; } public static void main ( String args [ ] ) { int a = 2020 , b = 6300 , k = 29 ; System . out . println ( findCount ( a , b , k ) ) ; } }
import java . util . * ; class GFG { static int findLargest ( int n , Vector < Integer > v ) { int flag = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v . get ( i ) == 0 ) flag = 1 ; sum += v . get ( i ) ; } if ( flag != 1 ) System . out . println ( " Not ▁ possible " ) ; else { Collections . sort ( v , Collections . reverseOrder ( ) ) ; if ( v . get ( 0 ) == 0 ) { System . out . println ( "0" ) ; return 0 ; } else { int flags = 0 ; int y = ( int ) ( sum % 3 ) ; if ( y != 0 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v . get ( i ) % 3 == y ) { v . remove ( i ) ; flags = 1 ; break ; } } if ( flags == 0 ) { y = 3 - y ; int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v . get ( i ) % 3 == y ) { v . remove ( i ) ; cnt ++ ; if ( cnt >= 2 ) break ; } } } } if ( v . get ( 0 ) == 0 ) System . out . println ( "0" ) ; else for ( Integer i : v ) { System . out . print ( i ) ; } } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 } ; int n = 11 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . add ( i , arr [ i ] ) ; findLargest ( n , v ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } public static void main ( String [ ] args ) { int n = 43 ; if ( isPrime ( n ) && ( isPowerOfTwo ( n * 3 - 1 ) ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . util . * ; class GFG { static void findkth ( int n , int k ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v1 . add ( i ) ; if ( i != Math . sqrt ( n ) ) v2 . add ( n / i ) ; } } Collections . reverse ( v2 ) ; if ( k > ( v1 . size ( ) + v2 . size ( ) ) ) System . out . print ( " Doesn ' t ▁ Exist " ) ; else { if ( k <= v1 . size ( ) ) System . out . print ( v1 . get ( k - 1 ) ) ; else System . out . print ( v2 . get ( k - v1 . size ( ) - 1 ) ) ; } } public static void main ( String [ ] args ) { int n = 15 , k = 2 ; findkth ( n , k ) ; } }
import java . io . * ; class GFG { static long findF_N ( long n ) { long ans = 0 ; for ( long i = 0 ; i < n ; ++ i ) ans += ( i + 1 ) * ( n - i - 1 ) ; return ans ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( findF_N ( n ) ) ; } }
import java . io . * ; class GFG { static String findNumber ( int n , int d ) { String ans = " " ; if ( d != 10 ) { ans += Integer . toString ( d ) ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } else { if ( n == 1 ) ans += " Impossible " ; else { ans += '1' ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } } return ans ; } public static void main ( String [ ] args ) { int n = 12 , d = 3 ; System . out . println ( findNumber ( n , d ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static void countDigits ( double val , long [ ] arr ) { while ( ( long ) val > 0 ) { long digit = ( long ) val % 10 ; arr [ ( int ) digit ] ++ ; val = ( long ) val / 10 ; } return ; } static void countFrequency ( int x , int n ) { long [ ] freq_count = new long [ 10 ] ; for ( int i = 1 ; i <= n ; i ++ ) { double val = Math . pow ( ( double ) x , ( double ) i ) ; countDigits ( val , freq_count ) ; } for ( int i = 0 ; i <= 9 ; i ++ ) { System . out . print ( freq_count [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int x = 15 , n = 3 ; countFrequency ( x , n ) ; } }
import java . io . * ; class GFG { static int MAX = 1000000 ; static int sieve_Prime [ ] = new int [ MAX + 4 ] ; static int sieve_count [ ] = new int [ MAX + 4 ] ; static void form_sieve ( ) { sieve_Prime [ 1 ] = 1 ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( sieve_Prime [ i ] == 0 ) { for ( int j = i * 2 ; j <= MAX ; j += i ) { if ( sieve_Prime [ j ] == 0 ) { sieve_Prime [ j ] = 1 ; sieve_count [ i ] ++ ; } } } } } public static void main ( String [ ] args ) { form_sieve ( ) ; int n = 2 ; System . out . println ( " Count ▁ = ▁ " + ( sieve_count [ n ] + 1 ) ) ; n = 3 ; System . out . println ( " Count ▁ = ▁ " + ( sieve_count [ n ] + 1 ) ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { int matrix [ ] [ ] = new int [ 5 ] [ 5 ] , row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( "The matrix isNEW_LINE"); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d TABSYMBOL " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( "NEW_LINE"); } System . out . printf ( " Elements on Secondary diagonal : "); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( ( row_index + column_index ) == size - 1 ) System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } }
class GFG { public static void main ( String [ ] args ) { int [ ] [ ] matrix = new int [ 5 ] [ 5 ] ; int row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( "The matrix isNEW_LINE"); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d TABSYMBOL " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( "NEW_LINE"); } System . out . printf ( " Elements above Secondary " ▁ + ▁ " diagonal are : "); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( ( row_index + column_index ) < size - 1 ) System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } }
class GFG { public static void main ( String [ ] args ) { int [ ] [ ] matrix = new int [ 5 ] [ 5 ] ; int row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( "The matrix isNEW_LINE"); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d TABSYMBOL " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( "NEW_LINE"); } System . out . printf ( " Corner Elements are : "); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( ( row_index == 0 row_index == size - 1 ) && ( column_index == 0 column_index == size - 1 ) ) System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } }
class GFG { public static void main ( String [ ] args ) { int matrix [ ] [ ] = new int [ 5 ] [ 5 ] , row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( "The matrix isNEW_LINE"); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d TABSYMBOL " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( "NEW_LINE"); } System . out . printf ( " Boundary Elements are : "); for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( ( row_index == 0 row_index == size - 1 column_index == 0 column_index == size - 1 ) ) { System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } } }
import java . util . * ; class GFG { static Vector < Integer > PrimeFactors ( int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; int x = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( x % i == 0 ) { v . add ( i ) ; while ( x % i == 0 ) x /= i ; } } if ( x > 1 ) v . add ( x ) ; return v ; } static int GoodNumber ( int n ) { Vector < Integer > v = new Vector < Integer > ( PrimeFactors ( n ) ) ; int ans = 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans *= v . get ( i ) ; return ans ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( GoodNumber ( n ) ) ; } }
class GFG { static boolean checkSpecialPrime ( boolean [ ] sieve , int num ) { while ( num != 0 ) { if ( ! sieve [ num ] ) { return false ; } num /= 10 ; } return true ; } static void findSpecialPrime ( int N ) { boolean [ ] sieve = new boolean [ N + 10 ] ; sieve [ 0 ] = sieve [ 1 ] = false ; for ( int i = 0 ; i < N + 10 ; i ++ ) sieve [ i ] = true ; for ( int i = 2 ; i <= N ; i ++ ) { if ( sieve [ i ] ) { for ( int j = i * i ; j <= N ; j += i ) { sieve [ j ] = false ; } } } while ( true ) { if ( checkSpecialPrime ( sieve , N ) ) { System . out . println ( N ) ; break ; } else N -- ; } } public static void main ( String [ ] args ) { findSpecialPrime ( 379 ) ; findSpecialPrime ( 100 ) ; } }
import java . util . * ; class GFG { static final int MAX = 1000000 ; static Vector < Integer > arr = new Vector < > ( ) ; static boolean [ ] sprime = new boolean [ MAX ] ; static void computeSemiPrime ( ) { for ( int i = 0 ; i < MAX ; i ++ ) sprime [ i ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { int cnt = 0 ; int num = i ; for ( int j = 2 ; cnt < 2 && j * j <= num ; ++ j ) { while ( num % j == 0 ) { num /= j ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; if ( cnt == 2 ) { sprime [ i ] = true ; arr . add ( i ) ; } } } static boolean checkSemiPrime ( int n ) { int i = 0 ; while ( arr . get ( i ) <= n / 2 ) { if ( sprime [ n - arr . get ( i ) ] ) { return true ; } i ++ ; } return false ; } public static void main ( String [ ] args ) { computeSemiPrime ( ) ; int n = 30 ; if ( checkSemiPrime ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPrime ( n ) && ( n % 4 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . io . * ; class GFG { static float area ( float a ) { if ( a < 0 ) return - 1 ; float area = ( float ) Math . pow ( ( a * Math . sqrt ( 3 ) ) / ( Math . sqrt ( 2 ) ) , 2 ) ; return area ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( area ( a ) ) ; } }
import java . io . * ; class GFG { static float heightCalculate ( int H , int n , int m ) { float N = ( float ) ( n * 1.0 ) ; float M = ( float ) ( m * 1.0 ) ; float h = H * ( float ) Math . sqrt ( N / ( N + M ) ) ; return h ; } public static void main ( String [ ] args ) { int H = 10 , n = 3 , m = 4 ; System . out . print ( heightCalculate ( H , n , m ) ) ; } }
class GFG { static boolean isprime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) if ( x % i == 0 ) return false ; return true ; } static boolean isNSqMinusnMSqPrime ( int m , int n ) { if ( n - m == 1 && isprime ( m + n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int m = 13 , n = 16 ; if ( isNSqMinusnMSqPrime ( m , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class GFG { static void findsolution ( long n , long x , long y ) { if ( ( y - n + 1 ) * ( y - n + 1 ) + n - 1 < x y < n ) { System . out . println ( " No ▁ solution " ) ; return ; } System . out . println ( y - n + 1 ) ; while ( n -- > 1 ) System . out . println ( "1" ) ; } public static void main ( String [ ] args ) { long n , x , y ; n = 5 ; x = 15 ; y = 15 ; findsolution ( n , x , y ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int findPosition ( int n , int f , int b ) { return n - Math . max ( f + 1 , n - b ) + 1 ; } public static void main ( String args [ ] ) { int n = 5 , f = 2 , b = 3 ; System . out . print ( findPosition ( n , f , b ) ) ; } }
class GFG { static int nthOdd ( int n ) { return ( 2 * n - 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( nthOdd ( n ) ) ; } }
class GFG { static int nthEven ( int n ) { return ( 2 * n ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( nthEven ( n ) ) ; } }
import java . io . * ; class GFG { static int countSolutions ( int a ) { int count = 0 ; for ( int i = 0 ; i <= a ; i ++ ) { if ( a == ( i + ( a ^ i ) ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a = 3 ; System . out . println ( countSolutions ( a ) ) ; } }
import java . io . * ; class GFG { static int countSolutions ( int a ) { int count = Integer . bitCount ( a ) ; count = ( int ) Math . pow ( 2 , count ) ; return count ; } public static void main ( String [ ] args ) { int a = 3 ; System . out . println ( countSolutions ( a ) ) ; } }
import java . io . * ; class GFG { static double nthHarmonic ( int N ) { float harmonic = 1 ; for ( int i = 2 ; i <= N ; i ++ ) { harmonic += ( float ) 1 / i ; } return harmonic ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . print ( nthHarmonic ( N ) ) ; } }
import java . io . * ; class GFG { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 2 ) + n - 3 ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . util . * ; class solution { static int nthTerm ( int n ) { return 5 * ( int ) Math . pow ( n , 2 ) - 5 * n ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . util . * ; class solution { static int nthTerm ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) + n - 2 ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . io . * ; class GFG { static int nthTerm ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) + 2 * n - 5 ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
public class GFG { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 2 ) + 3 * n - 5 ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
public class GFG { static int nthTerm ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) - 4 * n + 2 ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
public class GFG { static int nthTerm ( int n ) { return 4 * ( int ) Math . pow ( n , 2 ) - 3 * n + 2 ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . io . * ; class GFG { static double PI = 3.1415926535 ; static double findAnglesA ( double a , double b , double c ) { double A = Math . acos ( ( b * b + c * c - a * a ) / ( 2 * b * c ) ) ; return A * 180 / PI ; } static double findAnglesB ( double a , double b , double c ) { double B = Math . acos ( ( a * a + c * c - b * b ) / ( 2 * a * c ) ) ; return B * 180 / PI ; } static void printAngles ( int a , int b , int c ) { double x = ( double ) a ; double y = ( double ) b ; double z = ( double ) c ; double A = findAnglesA ( x , y , z ) ; double B = findAnglesB ( x , y , z ) ; System . out . println ( " Angles ▁ are ▁ A ▁ = ▁ " + A + " , ▁ B ▁ = ▁ " + B + " , ▁ C ▁ = ▁ " + 90 ) ; } static void printOtherSides ( int n ) { int b = 0 , c = 0 ; if ( ( n & 1 ) > 0 ) { if ( n == 1 ) System . out . println ( - 1 ) ; else { b = ( n * n - 1 ) / 2 ; c = ( n * n + 1 ) / 2 ; System . out . println ( " Side ▁ b ▁ = ▁ " + b + " , ▁ Side ▁ c ▁ = ▁ " + c ) ; } } else { if ( n == 2 ) System . out . println ( - 1 ) ; else { b = n * n / 4 - 1 ; c = n * n / 4 + 1 ; System . out . println ( " Side ▁ b ▁ = ▁ " + b + " , ▁ Side ▁ c ▁ = ▁ " + c ) ; } } printAngles ( n , b , c ) ; } public static void main ( String [ ] args ) { int a = 12 ; printOtherSides ( a ) ; } }
public class GFG { static int calculateSum ( int n ) { return n * ( n + 1 ) / 2 + ( int ) Math . pow ( ( n * ( n + 1 ) / 2 ) , 2 ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int calculateSum ( int n ) { return n * ( n + 1 ) / 2 + n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }
import java . util . * ; class solution { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 2 ) - n - 1 ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int nthTerm ( int n ) { return 4 * ( int ) Math . pow ( n , 2 ) - 7 * n + 3 ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . print ( nthTerm ( N ) ) ; } }
class GFG { static void checkIfPowerIsolated ( int num ) { int input = num ; int count = 0 ; int [ ] factor = new int [ num + 1 ] ; if ( num % 2 == 0 ) { while ( num % 2 == 0 ) { ++ count ; num /= 2 ; } factor [ 2 ] = count ; } for ( int i = 3 ; i * i <= num ; i += 2 ) { count = 0 ; while ( num % i == 0 ) { ++ count ; num /= i ; } if ( count > 0 ) factor [ i ] = count ; } if ( num > 1 ) factor [ num ] = 1 ; int product = 1 ; for ( int i = 0 ; i < num + 1 ; i ++ ) { if ( factor [ i ] > 0 ) product = product * factor [ i ] * i ; } if ( product == input ) System . out . print ( "Power-isolated IntegerNEW_LINE"); else System . out . print ( "Not a Power-isolated IntegerNEW_LINE"); } public static void main ( String [ ] args ) { checkIfPowerIsolated ( 12 ) ; checkIfPowerIsolated ( 18 ) ; checkIfPowerIsolated ( 35 ) ; } }
import java . util . * ; class solution { static long getNthTerm ( long N ) { return ( ( int ) Math . pow ( N , 2 ) + N + 1 ) ; } public static void main ( String arr [ ] ) { long N = 11 ; System . out . println ( getNthTerm ( N ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static long findSum ( int n ) { long sum2 , sum5 , sum10 ; sum2 = ( ( n / 2 ) * ( 4 + ( n / 2 - 1 ) * 2 ) ) / 2 ; sum5 = ( ( n / 5 ) * ( 10 + ( n / 5 - 1 ) * 5 ) ) / 2 ; sum10 = ( ( n / 10 ) * ( 20 + ( n / 10 - 1 ) * 10 ) ) / 2 ; return sum2 + sum5 - sum10 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( findSum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static Vector < Vector < Integer > > v = new Vector < Vector < Integer > > ( ) ; static void factors ( int N ) { for ( int i = 2 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( i / j == j ) v . get ( i ) . add ( j ) ; else { v . get ( i ) . add ( j ) ; v . get ( i ) . add ( i / j ) ; } } } Collections . sort ( v . get ( i ) ) ; } } static int product ( int n ) { v . clear ( ) ; for ( int i = 0 ; i < n + 100 ; i ++ ) v . add ( new Vector < Integer > ( ) ) ; factors ( n + 100 ) ; if ( n % 4 == 0 ) { int x = n / 4 ; x *= x ; return x * x ; } else { if ( isPrime ( n ) ) return - 1 ; else { int ans = - 1 ; if ( v . get ( n ) . size ( ) > 2 ) { int fac = v . get ( n ) . get ( v . get ( n ) . size ( ) - 3 ) ; for ( int i = v . get ( n ) . size ( ) - 1 ; i >= 0 ; i -- ) { for ( int j = v . get ( n ) . size ( ) - 1 ; j >= 0 ; j -- ) { if ( ( fac * 2 ) + ( v . get ( n ) . get ( j ) + v . get ( n ) . get ( i ) ) == n ) ans = Math . max ( ans , fac * fac * v . get ( n ) . get ( j ) * v . get ( n ) . get ( i ) ) ; } } return ans ; } } } return 0 ; } public static void main ( String args [ ] ) { int n = 24 ; System . out . println ( product ( n ) ) ; } }
import java . io . * ; class GFG { static boolean arePermutations ( int a [ ] , int b [ ] , int n , int m ) { int sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 += a [ i ] ; mul1 *= a [ i ] ; } for ( int i = 0 ; i < m ; i ++ ) { sum2 += b [ i ] ; mul2 *= b [ i ] ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 } ; int b [ ] = { 3 , 1 , 2 } ; int n = a . length ; int m = b . length ; if ( arePermutations ( a , b , n , m ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class Nth { static float CalculateRatio ( float m , float n ) { return ( 2 * m - 1 ) / ( 2 * n - 1 ) ; } } class GFG { public static void main ( String [ ] args ) { float m = 6 , n = 2 ; Nth a = new Nth ( ) ; System . out . println ( a . CalculateRatio ( m , n ) ) ; } }
import java . io . * ; class GFG { static int calculateSum ( int n ) { return ( int ) Math . pow ( n * ( n + 1 ) / 2 , 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( calculateSum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static int powerDigitSum ( int a , int n ) { int res = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { res = res * digSum ( a ) ; res = digSum ( res ) ; } a = digSum ( digSum ( a ) * digSum ( a ) ) ; n /= 2 ; } return res ; } public static void main ( String args [ ] ) { int a = 9 , n = 4 ; System . out . print ( powerDigitSum ( a , n ) ) ; } }
class GFG { static int totEdge ( int n ) { int result = 0 ; result = ( n * ( n - 1 ) ) / 2 ; return result ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( totEdge ( n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 1000005 ; static Vector < Boolean > prime = new Vector < > ( MAX ) ; static void vecIni ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { prime . add ( i , true ) ; } } static void sieve ( int N ) { prime . add ( 0 , false ) ; prime . add ( 1 , false ) ; for ( int i = 2 ; i <= N ; i ++ ) { if ( prime . get ( i ) ) { for ( int j = 2 ; i * j <= N ; j ++ ) { prime . add ( i * j , false ) ; } } } } static int countLogNeeded ( int N ) { int count = 0 ; sieve ( N ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( prime . get ( i ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { vecIni ( ) ; int N = 6 ; System . out . println ( countLogNeeded ( N ) ) ; } }
import java . io . * ; class GFG { static int coin ( int totalRupees , int X , int Y , int Z ) { float one = 0 , fifty = 0 , twentyfive = 0 , result = 0 , total = 0 ; one = X * 1 ; fifty = ( ( Y * 1 ) / 2 ) ; twentyfive = ( ( Z * 1 ) / 4 ) ; total = one + fifty + twentyfive ; result = ( ( totalRupees ) / total ) ; return ( int ) result ; } public static void main ( String [ ] args ) { int totalRupees = 1800 ; int X = 1 , Y = 2 , Z = 4 ; int Rupees = coin ( totalRupees , X , Y , Z ) ; System . out . println ( "1 ▁ rupess ▁ coins ▁ = ▁ " + Rupees * 1 ) ; System . out . println ( "50 ▁ paisa ▁ coins ▁ = ▁ " + Rupees * 2 ) ; System . out . println ( "25 ▁ paisa ▁ coins ▁ = ▁ " + Rupees * 4 ) ; } }
public class GFG { static float sumOfSeries ( int x , int k ) { float y = ( float ) ( ( ( float ) ( x ) / 81 ) * ( 9 * k - 1 + Math . pow ( 10 , ( - 1 ) * k ) ) ) ; return y ; } public static void main ( String args [ ] ) { int x = 9 ; int k = 20 ; System . out . println ( sumOfSeries ( x , k ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void findfactors ( int n ) { HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; Vector < Integer > v = new Vector < Integer > ( ) , v1 = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= ( int ) Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { v . add ( i ) ; if ( i != ( n / i ) && i != 1 ) v . add ( n / i ) ; } } int s = v . size ( ) ; int maxi = - 1 ; int mp1_first [ ] = new int [ n + 5 ] , mp1_second [ ] = new int [ n + 5 ] ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = i ; j < s ; j ++ ) { if ( v . get ( i ) + v . get ( j ) < n ) { v1 . add ( v . get ( i ) + v . get ( j ) ) ; mp1_first [ v . get ( i ) + v . get ( j ) ] = v . get ( i ) ; mp1_second [ v . get ( i ) + v . get ( j ) ] = v . get ( j ) ; mpp . put ( v . get ( i ) + v . get ( j ) , 1 ) ; } } } s = v1 . size ( ) ; for ( int i = 0 ; i < s ; i ++ ) { int el = n - ( v1 . get ( i ) ) ; if ( mpp . get ( el ) != null ) { int a = mp1_first [ v1 . get ( i ) ] ; int b = mp1_second [ v1 . get ( i ) ] ; int c = mp1_first [ n - v1 . get ( i ) ] ; int d = mp1_second [ n - v1 . get ( i ) ] ; maxi = Math . max ( a * b * c * d , maxi ) ; } } if ( maxi == - 1 ) System . out . println ( " Not ▁ Possible " ) ; else { System . out . println ( " The ▁ maximum ▁ product " + " ▁ is ▁ " + maxi ) ; } } public static void main ( String args [ ] ) { int n = 50 ; findfactors ( n ) ; } }
import java . util . ArrayList ; import java . util . List ; public class GFG { static List < Integer > factors = new ArrayList < > ( 10 ) ; static void findFactors ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . add ( factors . size ( ) , i ) ; } else { factors . add ( factors . size ( ) , n / i ) ; factors . add ( factors . size ( ) , i ) ; } } } } static int findProduct ( int n ) { int product = - 1 ; int si = factors . size ( ) ; for ( int i = 0 ; i < si ; i ++ ) { for ( int j = 0 ; j < si ; j ++ ) { for ( int k = 0 ; k < si ; k ++ ) { for ( int l = 0 ; l < si ; l ++ ) { int s = factors . get ( i ) + factors . get ( j ) + factors . get ( k ) + factors . get ( l ) ; if ( s == n ) { int p = factors . get ( i ) * factors . get ( j ) * factors . get ( k ) * factors . get ( l ) ; if ( p > product ) { product = p ; } } } } } } return product ; } public static void main ( String [ ] args ) { int n = 10 ; findFactors ( n ) ; System . out . println ( findProduct ( n ) ) ; } }
import java . io . * ; class GFG { static boolean isDivisible ( long n ) { while ( n / 100 > 0 ) { long d = n % 10 ; n /= 10 ; n -= d * 5 ; } return ( n % 17 == 0 ) ; } public static void main ( String [ ] args ) { long n = 19877658 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
public class GFG { static int nextPowerOf2 ( int n ) { int count = 0 ; if ( n != 0 && ( n & ( n - 1 ) ) == 0 ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; } static int removeElement ( int n ) { if ( n == 1 n == 2 ) return 0 ; int a = nextPowerOf2 ( n ) ; if ( n == a n == a - 1 ) return 1 ; else if ( n == a - 2 ) return 0 ; else if ( n % 2 == 0 ) return 1 ; else return 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( removeElement ( n ) ) ; } }
public class GFG { static int bridge_length ( int trainLength , int Speed , int Time ) { return ( ( Time * Speed ) - trainLength ) ; } public static void main ( String [ ] args ) { int trainLength = 120 ; int Speed = 30 ; int Time = 18 ; System . out . print ( " Length ▁ of ▁ bridge ▁ = ▁ " + bridge_length ( trainLength , Speed , Time ) + " ▁ meters " ) ; } }
import java . io . * ; class GFG { static float Time ( float arr [ ] , int n , float Emptypipe ) { float fill = 0 ; for ( int i = 0 ; i < n ; i ++ ) fill += 1 / arr [ i ] ; fill = fill - ( 1 / ( float ) Emptypipe ) ; return 1 / fill ; } public static void main ( String [ ] args ) { float arr [ ] = { 12 , 14 } ; float Emptypipe = 30 ; int n = arr . length ; System . out . println ( ( int ) ( Time ( arr , n , Emptypipe ) ) + " ▁ Hours " ) ; } }
class GFG { static long MOD = 1000000007 ; static long modInv ( long x ) { long n = MOD - 2 ; long result = 1 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) { result = result * x % MOD ; } x = x * x % MOD ; n = n / 2 ; } return result ; } static long getSum ( long n , long k ) { long ans = 1 ; for ( long i = n + 1 ; i > n - k ; i -- ) { ans = ans * i % MOD ; } ans = ans * modInv ( k + 1 ) % MOD ; return ans ; } public static void main ( String [ ] args ) { long n = 3 , k = 2 ; System . out . println ( getSum ( n , k ) ) ; } }
class GFG { static void printCombination ( int n ) { System . out . print ( 1 + " ▁ " ) ; if ( ( n - 2 ) % 3 == 0 ) System . out . print ( 2 + " ▁ " + ( n - 3 ) ) ; else System . out . print ( 1 + " ▁ " + ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 233 ; printCombination ( n ) ; } }
class GFG { static boolean checkPairs ( int l , int r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean isPalindrome ( int n ) { if ( n % 11 == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { System . out . println ( isPalindrome ( 123321 ) ? " Palindrome " : " Not ▁ Palindrome " ) ; } }
class GFG { public static void main ( String [ ] args ) { int target = 93 ; int [ ] arr = { 1 , 31 , 3 , 1 , 93 , 3 , 31 , 1 , 93 } ; int length = arr . length ; int totalCount = 0 ; for ( int i = 0 ; i < length - 2 ; i ++ ) { if ( target % arr [ i ] == 0 ) { for ( int j = i + 1 ; j < length - 1 ; j ++ ) { if ( target % ( arr [ i ] * arr [ j ] ) == 0 ) { int toFind = target / ( arr [ i ] * arr [ j ] ) ; for ( int k = j + 1 ; k < length ; k ++ ) { if ( arr [ k ] == toFind ) { totalCount ++ ; } } } } } } System . out . println ( " Total ▁ number ▁ of ▁ triplets ▁ found ▁ : ▁ " + totalCount ) ; } }
import java . io . * ; import java . util . * ; public class GFG { public static boolean isFascinating ( int num ) { int [ ] freq = new int [ 10 ] ; String val = " " + num + num * 2 + num * 3 ; for ( int i = 0 ; i < val . length ( ) ; i ++ ) { int digit = val . charAt ( i ) - '0' ; if ( freq [ digit ] > 0 && digit != 0 ) return false ; else freq [ digit ] ++ ; } for ( int i = 1 ; i < freq . length ; i ++ ) { if ( freq [ i ] == 0 ) return false ; } return true ; } public static void main ( String args [ ] ) { int num = 192 ; if ( num < 100 ) System . out . println ( " No " ) ; else { boolean ans = isFascinating ( num ) ; if ( ans ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { public static int binomial_coefficient ( int n , int m ) { int res = 1 ; if ( m > n - m ) m = n - m ; for ( int i = 0 ; i < m ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } public static int calculate_ways ( int m , int n ) { if ( m < n ) { return 0 ; } int ways = binomial_coefficient ( n + m - 1 , n - 1 ) ; return ways ; } public static void main ( String [ ] args ) { int m = 7 , n = 5 ; int result = calculate_ways ( m , n ) ; System . out . println ( Integer . toString ( result ) ) ; System . exit ( 0 ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static final int N = 100005 ; static int [ ] phi ; static int [ ] S ; static void computeTotient ( ) { for ( int i = 1 ; i < N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p < N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i < N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static void CoPrimes ( ) { computeTotient ( ) ; for ( int i = 1 ; i < N ; i ++ ) S [ i ] = S [ i - 1 ] + phi [ i ] ; } public static void main ( String args [ ] ) { phi = new int [ N ] ; S = new int [ N ] ; CoPrimes ( ) ; int q [ ] = { 3 , 4 } ; int n = q . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( "Number of unordered coprime " ▁ + ▁ " pairs of integers from 1 to " ▁ + ▁ q [ i ] ▁ + ▁ " are " + S[q[i]] ); } }
import java . util . * ; class GFG { static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static int numberSequence ( int n ) { if ( n == 1 ) return 1 ; String s = " " ; for ( int i = 1 ; i < n ; i ++ ) s += '1' ; s += '0' ; for ( int i = 1 ; i <= n ; i ++ ) s += '1' ; int num = binaryToDecimal ( s ) ; return num ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( numberSequence ( n ) ) ; } }
class GFG { static int numberSequence ( int n ) { int num = ( int ) ( Math . pow ( 4 , n ) - Math . pow ( 2 , n ) ) - 1 ; return num ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( numberSequence ( n ) ) ; } }
class GFG { static int prime ( int num ) { int i , flag = 0 ; for ( i = 2 ; i <= num / 2 ; i ++ ) { if ( num % i == 0 ) { flag = 1 ; break ; } } if ( flag == 0 ) return 1 ; else return 0 ; } static void print_alternate_prime ( int n ) { int counter = 0 ; for ( int num = 2 ; num < n ; num ++ ) { if ( prime ( num ) == 1 ) { if ( counter % 2 == 0 ) System . out . print ( num + " ▁ " ) ; counter ++ ; } } } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( " Following ▁ are ▁ the ▁ alternate ▁ " + " prime ▁ number ▁ smaller ▁ than ▁ " + " or ▁ equal ▁ to ▁ " + n ) ; print_alternate_prime ( n ) ; } }
class GFG { static void SieveOfEratosthenes ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } boolean flag = true ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { if ( flag ) { System . out . print ( p + " ▁ " ) ; flag = false ; } else { flag = true ; } } } } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( " Following ▁ are ▁ the ▁ alternate " + " ▁ prime ▁ numbers ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + n ) ; SieveOfEratosthenes ( n ) ; } }
import java . io . * ; class GFG { static boolean findGreater ( int x , int y ) { if ( x > y ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { int x = 4 ; int y = 9 ; if ( findGreater ( x , y ) ) System . out . println ( "1" ) ; else System . out . println ( "2" ) ; } }
import java . util . * ; class solution { static int max_profit ( int a [ ] , int b [ ] , int n , int fee ) { int i , j , profit ; int l , r , diff_day = 1 , sum = 0 ; b [ 0 ] = 0 ; b [ 1 ] = diff_day ; for ( i = 1 ; i < n ; i ++ ) { l = 0 ; r = diff_day ; sum = 0 ; for ( j = n - 1 ; j >= i ; j -- ) { profit = ( a [ r ] - a [ l ] ) - fee ; if ( profit > 0 ) { sum = sum + profit ; } l ++ ; r ++ ; } if ( b [ 0 ] < sum ) { b [ 0 ] = sum ; b [ 1 ] = diff_day ; } diff_day ++ ; } return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 6 , 1 , 7 , 2 , 8 , 4 } ; int n = arr . length ; int [ ] b = new int [ 2 ] ; int tranFee = 2 ; max_profit ( arr , b , n , tranFee ) ; System . out . println ( b [ 0 ] + " , ▁ " + b [ 1 ] ) ; } }
import java . util . Vector ; class GFG { static int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } static void value ( int a , int b , int c ) { int co = 0 , p = 0 ; int no , r = 0 , x = 0 , q = 0 , w = 0 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < 82 ; i ++ ) { no = ( int ) Math . pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . add ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ; } }
class GFG { static void print ( int p0 ) { int p1 , i = 0 , x , flag , k ; while ( true ) { flag = 1 ; x = ( int ) ( Math . pow ( 2 , i ) ) ; p1 = x * p0 - ( x - 1 ) ; for ( k = 2 ; k < p1 ; k ++ ) { if ( p1 % k == 0 ) { flag = 0 ; break ; } } if ( flag == 0 ) break ; System . out . print ( p1 + " ▁ " ) ; i ++ ; } } public static void main ( String [ ] args ) { int p0 = 19 ; print ( p0 ) ; } }
class GFG { static long boost_hyperfactorial ( long num ) { long val = 1 ; for ( int i = 1 ; i <= num ; i ++ ) { val = val * ( long ) Math . pow ( i , i ) ; } return val ; } public static void main ( String args [ ] ) { int num = 5 ; System . out . println ( boost_hyperfactorial ( num ) ) ; } }
class solution_1 { static int findOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ( A [ i ] % 2 == 1 ) ) count ++ ; return count * ( count - 1 ) / 2 ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = a . length ; System . out . println ( findOddPair ( a , n ) ) ; } }
class GFG { static boolean isSurd ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int j = i ; while ( j < n ) j = j * i ; if ( j == n ) return false ; } return true ; } public static void main ( String args [ ] ) { int n = 15 ; if ( isSurd ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Geeks { static long LastTwoDigit ( long num ) { long one = num % 10 ; num /= 10 ; long tens = num % 10 ; tens *= 10 ; num = tens + one ; return num ; } public static void main ( String args [ ] ) { int n = 10 ; long num = 1 ; num = ( long ) Math . pow ( 2 , n ) ; System . out . println ( " Last ▁ 2 ▁ digits ▁ of ▁ 2 ^ 10 ▁ = ▁ " + LastTwoDigit ( num ) ) ; } }
class GFG { static int power ( long x , long y , long p ) { while ( y > 0 ) { long r = y & 1 ; if ( r == 1 ) res = ( res * ( int ) x ) % ( int ) p ; x = ( x * x ) % p ; } return res ; } static int numberOfDigits ( int x ) { int i = 0 ; while ( x != 0 ) { x /= 10 ; i ++ ; } return i ; } static void LastTwoDigit ( int n ) { System . out . print ( " Last ▁ " + 2 + " ▁ digits ▁ of ▁ " + 2 + " ^ " ) ; System . out . print ( n + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( int i = 0 ; i < ( 2 - numberOfDigits ( temp ) ) ; i ++ ) System . out . print ( 0 + " ▁ " ) ; if ( temp != 0 ) System . out . println ( temp ) ; } public static void main ( String [ ] args ) { int n = 72 ; LastTwoDigit ( n ) ; } }
class GFG { static long modPower ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long gcd ( long a , long b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static long gcdPow ( long a , long n , long c ) { if ( a % c == 0 ) return c ; long modexpo = modPower ( a , n , c ) ; return gcd ( modexpo , c ) ; } public static void main ( String [ ] args ) { long a = 10248585 , n = 1000000 , c = 12564 ; System . out . println ( gcdPow ( a , n , c ) ) ; } }
import java . io . * ; class GFG { static int countOddSum ( int ar [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int val = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { val = val + ar [ j ] ; if ( val % 2 != 0 ) result ++ ; } } return ( result ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 5 , 4 , 4 , 5 , 1 , 3 } ; int n = ar . length ; System . out . print ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ odd ▁ sum ▁ is ▁ " ) ; System . out . println ( countOddSum ( ar , n ) ) ; } }
public class GEE { static void printPFsInPairs ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 ) System . out . println ( i + " * " + n / i ) ; } public static void main ( String [ ] args ) { int n = 24 ; printPFsInPairs ( n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int SumOfPrimeDivisors ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( n % i == 0 ) { if ( isPrime ( i ) ) sum += i ; } } return sum ; } public static void main ( String args [ ] ) { int n = 60 ; System . out . print ( " Sum ▁ of ▁ prime ▁ divisors ▁ of ▁ 60 ▁ is ▁ " + SumOfPrimeDivisors ( n ) + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static int Sum ( int N ) { int SumOfPrimeDivisors [ ] = new int [ N + 1 ] ; for ( int i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 0 ) { for ( int j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } public static void main ( String args [ ] ) { int N = 60 ; System . out . print ( " Sum ▁ of ▁ prime ▁ " + " divisors ▁ of ▁ 60 ▁ is ▁ " + Sum ( N ) + "NEW_LINE"); } }
import java . io . * ; public class GFG { static int rangesum ( int n , int l , int r ) { int [ ] arr = new int [ n ] ; int c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } int sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 12 ; int l = 1 , r = 11 ; System . out . println ( rangesum ( n , l , r ) ) ; } }
import java . io . * ; public class GFG { static int sumeven ( int n , int l , int r ) { int sum = 0 ; int mid = n / 2 ; if ( r <= mid ) { int first = ( 2 * l - 1 ) ; int last = ( 2 * r - 1 ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l >= mid ) { int first = ( 2 * ( l - n / 2 ) ) ; int last = ( 2 * ( r - n / 2 ) ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { int sumleft = 0 , sumright = 0 ; int first_term1 = ( 2 * l - 1 ) ; int last_term1 = ( 2 * ( n / 2 ) - 1 ) ; int no_of_terms1 = n / 2 - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; int first_term2 = 2 ; int last_term2 = ( 2 * ( r - n / 2 ) ) ; int no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } static int sumodd ( int n , int l , int r ) { int mid = n / 2 + 1 ; int sum = 0 ; if ( r <= mid ) { int first = ( 2 * l - 1 ) ; int last = ( 2 * r - 1 ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else if ( l > mid ) { int first = ( 2 * ( l - mid ) ) ; int last = ( 2 * ( r - mid ) ) ; int no_of_terms = r - l + 1 ; sum = ( ( no_of_terms ) * ( ( first + last ) ) ) / 2 ; } else { int sumleft = 0 , sumright = 0 ; int first_term1 = ( 2 * l - 1 ) ; int last_term1 = ( 2 * mid - 1 ) ; int no_of_terms1 = mid - l + 1 ; sumleft = ( ( no_of_terms1 ) * ( ( first_term1 + last_term1 ) ) ) / 2 ; int first_term2 = 2 ; int last_term2 = ( 2 * ( r - mid ) ) ; int no_of_terms2 = r - mid ; sumright = ( ( no_of_terms2 ) * ( ( first_term2 + last_term2 ) ) ) / 2 ; sum = ( sumleft + sumright ) ; } return sum ; } static int rangesum ( int n , int l , int r ) { int sum = 0 ; if ( n % 2 == 0 ) return sumeven ( n , l , r ) ; else return sumodd ( n , l , r ) ; } public static void main ( String [ ] args ) { int n = 12 ; int l = 1 , r = 11 ; System . out . println ( rangesum ( n , l , r ) ) ; } }
import java . io . * ; class GFG { static void findAngle ( int n ) { int interiorAngle , exteriorAngle ; interiorAngle = ( n - 2 ) * 180 / n ; exteriorAngle = 360 / n ; System . out . println ( " Interior ▁ angle : ▁ " + interiorAngle ) ; System . out . println ( " Exterior ▁ angle : ▁ " + exteriorAngle ) ; } public static void main ( String [ ] args ) { int n = 10 ; findAngle ( n ) ; } }
import java . io . * ; class GFG { static boolean DivisibleBy41 ( int first , int second , int c , int n ) { int digit [ ] = new int [ n ] ; digit [ 0 ] = first ; digit [ 1 ] = second ; for ( int i = 2 ; i < n ; i ++ ) digit [ i ] = ( digit [ i - 1 ] * c + digit [ i - 2 ] ) % 10 ; int ans = digit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) ans = ( ans * 10 + digit [ i ] ) % 41 ; if ( ans % 41 == 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int first = 1 , second = 2 , c = 1 , n = 3 ; if ( DivisibleBy41 ( first , second , c , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; class GFG { static int findTetrahedralNumber ( int n ) { return ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) ; } static void printSeries ( int n ) { int prev = 0 ; int curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTetrahedralNumber ( i ) ; curr = curr + prev ; System . out . print ( curr + " ▁ " ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }
import java . io . * ; class GFG { static void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = ( i * ( i + 1 ) * ( i + 2 ) * ( i + 3 ) / 24 ) ; System . out . print ( num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }
import java . io . * ; class GFG { static long findTriangularNumber ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static void printSeries ( int n ) { long prev = 0 ; long curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; System . out . print ( curr + " ▁ " ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }
import java . io . * ; class GFG { static void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = i * ( i + 1 ) * ( i + 2 ) / 6 ; System . out . print ( num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }
import java . io . * ; class GFG { static void count_even_odd ( int min , int max , int steps [ ] [ ] ) { int a , b , even , odd ; boolean beven = true , aeven = false ; int n = 2 ; for ( int i = 0 ; i < n ; i ++ ) { a = steps [ i ] [ 0 ] ; b = steps [ i ] [ 1 ] ; if ( ! ( aeven || ( a & 1 ) > 0 ) ) aeven = true ; if ( beven ) { if ( ( b & 1 ) > 0 ) beven = false ; } else if ( ! ( ( a & 1 ) > 0 ) ) { if ( ! ( ( b & 1 ) > 0 ) ) beven = true ; } else { if ( ( b & 1 ) > 0 ) beven = true ; } } if ( beven ) { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } else { even = ( int ) max / 2 - ( int ) ( min - 1 ) / 2 ; odd = 0 ; } if ( ! ( beven ^ aeven ) ) even += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; else odd += max - min + 1 - ( int ) max / 2 + ( int ) ( min - 1 ) / 2 ; System . out . print ( " even ▁ = ▁ " + even + " , ▁ odd ▁ = ▁ " + odd ) ; } public static void main ( String [ ] args ) { int min = 1 , max = 4 ; int steps [ ] [ ] = { { 1 , 2 } , { 3 , 4 } } ; count_even_odd ( min , max , steps ) ; } }
import java . io . * ; class GFG { static int getMaxOnes ( int n , int x ) { int zeroes = ( n / x ) ; zeroes = zeroes * zeroes ; int total = n * n ; int ans = total - zeroes ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int x = 2 ; System . out . println ( getMaxOnes ( n , x ) ) ; } }
import java . io . * ; class GFG { static long power ( long x , long y , long p ) { x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } public static void main ( String [ ] args ) { long a = 3 ; String b = "100000000000000000000000000" ; long remainderB = 0 ; long MOD = 1000000007 ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) remainderB = ( remainderB * 10 + b . charAt ( i ) - '0' ) % ( MOD - 1 ) ; System . out . println ( power ( a , remainderB , MOD ) ) ; } }
import java . util . * ; class geeks { public static int minimumOperations ( int [ ] a , int n ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . get ( a [ i ] ) != null ) { int x = mp . get ( a [ i ] ) ; mp . put ( a [ i ] , ++ x ) ; } else mp . put ( a [ i ] , 1 ) ; } int count = 0 ; for ( HashMap . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { if ( entry . getValue ( ) > 1 ) { count += ( entry . getValue ( ) - 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 1 , 2 , 3 , 3 , 4 , 3 } ; int n = a . length ; System . out . println ( minimumOperations ( a , n ) ) ; } }
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 ) ; } }
import java . io . * ; class GFG { static void findSurfaceArea ( float a , float h ) { float Area ; Area = 6 * a * h + 3 * ( float ) ( Math . sqrt ( 3 ) ) * a * a ; System . out . println ( " Surface ▁ Area : ▁ " + Area ) ; } static void findVolume ( float a , float h ) { float Volume ; Volume = 3 * ( float ) ( Math . sqrt ( 3 ) ) * a * a * h / 2 ; System . out . println ( " Volume : ▁ " + Volume ) ; } public static void main ( String [ ] args ) { float a = 5 , h = 10 ; findSurfaceArea ( a , h ) ; findVolume ( a , h ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static double MinimumMail ( int n , int k , int x ) { double m = ( n - 1 ) + Math . ceil ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + Math . ceil ( n * 1.0 / x ) * ( k - n ) ; return m ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 9 ; int X = 2 ; System . out . print ( ( int ) MinimumMail ( N , K , X ) + "NEW_LINE"); } }
class GFG { static void findArea ( float a , float b ) { float Area ; Area = ( float ) 3.142 * a * b ; System . out . println ( " Area : ▁ " + Area ) ; } public static void main ( String [ ] args ) { float a = 5 , b = 4 ; findArea ( a , b ) ; } }
class GFG { static int calculate ( int x , int k , int m ) { int result = x ; k -- ; while ( k -- > 0 ) { result = ( int ) Math . pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } public static void main ( String args [ ] ) { int x = 5 , k = 2 , m = 3 ; System . out . println ( calculate ( x , k , m ) ) ; } }
import java . io . * ; class GFG { static int rev ( int n , int temp ) { if ( n == 0 ) return temp ; temp = ( temp * 10 ) + ( n % 10 ) ; return rev ( n / 10 , temp ) ; } public static void main ( String [ ] args ) { int n = 121 ; int temp = rev ( n , 0 ) ; if ( temp == n ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
class GFG { static void fibonacci ( double n ) { double fib ; for ( double i = 0 ; i < n ; i ++ ) { fib = ( Math . pow ( ( 1 + Math . sqrt ( 5 ) ) , i ) - Math . pow ( ( 1 - Math . sqrt ( 5 ) ) , i ) ) / ( Math . pow ( 2 , i ) * Math . sqrt ( 5 ) ) ; System . out . print ( ( int ) fib + " ▁ " ) ; } } public static void main ( String [ ] args ) { double n = 8 ; fibonacci ( n ) ; } }
class GFG { public static int findNature ( int a , int b , int n ) { int [ ] seq = new int [ 100 ] ; seq [ 0 ] = a ; seq [ 1 ] = b ; for ( int i = 2 ; i <= n ; i ++ ) seq [ i ] = seq [ i - 1 ] + seq [ i - 2 ] ; if ( ( seq [ n ] & 1 ) != 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; int n = 3 ; if ( findNature ( a , b , n ) == 1 ) System . out . println ( " Odd ▁ " ) ; else System . out . println ( " Even ▁ " ) ; } }
import java . io . * ; class GFG { static void printMatrix ( int n ) { float H [ ] [ ] = new float [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { H [ i ] [ j ] = ( float ) 1.0 / ( ( i + 1 ) + ( j + 1 ) - ( float ) 1.0 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( H [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; printMatrix ( n ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxDivisorRange ( int a , int b , int l , int h ) { int g = gcd ( a , b ) ; int res = - 1 ; for ( int i = l ; i * i <= g && i <= h ; i ++ ) if ( g % i == 0 ) res = Math . max ( res , Math . max ( i , g / i ) ) ; return res ; } public static void main ( String [ ] args ) { int a = 3 , b = 27 , l = 1 , h = 5 ; System . out . println ( maxDivisorRange ( a , b , l , h ) ) ; } }
import java . io . * ; class GFG { static int rows = 3 ; static int cols = 3 ; static void meanVector ( int mat [ ] [ ] ) { System . out . print ( " [ ▁ " ) ; for ( int i = 0 ; i < rows ; i ++ ) { double mean = 0.00 ; int sum = 0 ; for ( int j = 0 ; j < cols ; j ++ ) sum += mat [ j ] [ i ] ; mean = sum / rows ; System . out . print ( ( int ) mean + " ▁ " ) ; } System . out . print ( " ] " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; meanVector ( mat ) ; } }
import java . io . * ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { System . out . println ( " - 2 ▁ - 1 ▁ 0 ▁ 1 ▁ 2" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 4 ; i += inc ) { if ( i + i + 1 + i + 2 + i + 3 + i + 4 == n ) { System . out . print ( ( i ) + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) + " ▁ " + ( i + 3 ) + " ▁ " + ( i + 4 ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; checksum ( n ) ; } }
import java . io . * ; class GFG { static void checksum ( int n ) { if ( n % 5 == 0 ) System . out . println ( ( n / 5 - 2 ) + " ▁ " + ( n / 5 - 1 ) + " ▁ " + ( n / 5 ) + " ▁ " + ( n / 5 + 1 ) + " ▁ " + ( n / 5 + 2 ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; checksum ( n ) ; } }
import java . io . * ; class GFG { static int N = 1000001 ; static int visited [ ] = new int [ N ] ; static int goesTo [ ] = new int [ N ] ; static int dfs ( int i ) { if ( visited [ i ] == 1 ) return 0 ; visited [ i ] = 1 ; int x = dfs ( goesTo [ i ] ) ; return ( x + 1 ) ; } static int noOfTranspositions ( int P [ ] , int n ) { for ( int i = 1 ; i <= n ; i ++ ) visited [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) goesTo [ P [ i ] ] = i + 1 ; int transpositions = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( visited [ i ] == 0 ) { int ans = dfs ( i ) ; transpositions += ans - 1 ; } } return transpositions ; } public static void main ( String [ ] args ) { int permutation [ ] = { 5 , 1 , 4 , 3 , 2 } ; int n = permutation . length ; System . out . println ( noOfTranspositions ( permutation , n ) ) ; } }
import java . io . * ; class GFG { static int sumOfSeries ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( sumOfSeries ( n ) ) ; } }
import java . io . * ; class GFG { static int countUnmarked ( int N ) { if ( N % 2 == 0 ) return N / 2 ; else return N / 2 + 1 ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( " Number ▁ of ▁ unmarked ▁ " + " elements : ▁ " + countUnmarked ( N ) ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int calculateSeries ( int n ) { return factorial ( n + 1 ) - 1 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( calculateSeries ( n ) ) ; } }
import java . util . * ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static int getAliquot ( int n ) { TreeSet < Integer > s = new TreeSet < Integer > ( ) ; s . add ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . contains ( n ) & n != s . last ( ) ) { return n ; } s . add ( n ) ; } return 0 ; } static boolean isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { sum = sum + i + n / i ; } } if ( sum == n && n != 1 ) { return true ; } return false ; } static boolean isAspiring ( int n ) { int alq = getAliquot ( n ) ; if ( isPerfect ( alq ) && ! isPerfect ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 25 ; if ( isAspiring ( n ) ) { System . out . println ( " Aspiring " ) ; } else { System . out . println ( " Not ▁ Aspiring " ) ; } } }
import java . io . * ; class GFG { static int minimumLength ( int x , int y , int z ) { return 1 + Math . abs ( x - y ) + Math . abs ( y - z ) ; } public static void main ( String [ ] args ) { int x = 3 , y = 1 , z = 2 ; System . out . println ( minimumLength ( x , y , z ) ) ; } }
import java . io . * ; class GFG { static void endPointOfDiameterofCircle ( int x1 , int y1 , int c1 , int c2 ) { System . out . print ( " x2 ▁ = ▁ " + ( 2 * c1 - x1 ) + " ▁ " ) ; System . out . print ( " y2 ▁ = ▁ " + ( 2 * c2 - y1 ) ) ; } public static void main ( String [ ] args ) { int x1 = - 4 , y1 = - 1 ; int c1 = 3 , c2 = 5 ; endPointOfDiameterofCircle ( x1 , y1 , c1 , c2 ) ; } }
import java . text . * ; import java . math . * ; class GFG { static float proterm ( int i , float value , float x [ ] ) { float pro = 1 ; for ( int j = 0 ; j < i ; j ++ ) { pro = pro * ( value - x [ j ] ) ; } return pro ; } static void dividedDiffTable ( float x [ ] , float y [ ] [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { y [ j ] [ i ] = ( y [ j ] [ i - 1 ] - y [ j + 1 ] [ i - 1 ] ) / ( x [ j ] - x [ i + j ] ) ; } } } static float applyFormula ( float value , float x [ ] , float y [ ] [ ] , int n ) { float sum = y [ 0 ] [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum = sum + ( proterm ( i , value , x ) * y [ 0 ] [ i ] ) ; } return sum ; } static void printDiffTable ( float y [ ] [ ] , int n ) { DecimalFormat df = new DecimalFormat ( " # . # # # # " ) ; df . setRoundingMode ( RoundingMode . HALF_UP ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n - i ; j ++ ) { String str1 = df . format ( y [ i ] [ j ] ) ; System . out . print ( str1 + " TABSYMBOL ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; float value , sum ; float y [ ] [ ] = new float [ 10 ] [ 10 ] ; float x [ ] = { 5 , 6 , 9 , 11 } ; y [ 0 ] [ 0 ] = 12 ; y [ 1 ] [ 0 ] = 13 ; y [ 2 ] [ 0 ] = 14 ; y [ 3 ] [ 0 ] = 16 ; dividedDiffTable ( x , y , n ) ; printDiffTable ( y , n ) ; value = 7 ; DecimalFormat df = new DecimalFormat ( " # . # # " ) ; df . setRoundingMode ( RoundingMode . HALF_UP ) ; System . out . println ( " Value at " + df . format ( value ) + " is " + df . format ( applyFormula ( value , x , y , n ) ) ) ; } }
import java . io . * ; class GFG { static long centered_heptagonal_num ( long n ) { return ( 7 * n * n - 7 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 5 ; System . out . println ( n + " th ▁ Centered ▁ " + " heptagonal ▁ number ▁ : ▁ " + centered_heptagonal_num ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean checkHankelMatrix ( int n , int m [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i + j < n ) { if ( m [ i ] [ j ] != m [ i + j ] [ 0 ] ) return false ; } else { if ( m [ i ] [ j ] != m [ i + j - n + 1 ] [ n - 1 ] ) return false ; } } } return true ; } public static void main ( String args [ ] ) { int n = 4 ; int m [ ] [ ] = { { 1 , 2 , 3 , 5 } , { 2 , 3 , 5 , 8 } , { 3 , 5 , 8 , 0 } , { 5 , 8 , 0 , 9 } } ; if ( checkHankelMatrix ( n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static String find_Square_369 ( String num ) { char a , b , c , d ; if ( num . charAt ( 0 ) == '3' ) { a = '1' ; b = '0' ; c = '8' ; d = '9' ; } else if ( num . charAt ( 0 ) == '6' ) { a = '4' ; b = '3' ; c = '5' ; d = '6' ; } else { a = '9' ; b = '8' ; c = '0' ; d = '1' ; } String result = " " ; int size = num . length ( ) ; for ( int i = 1 ; i < size ; i ++ ) result += a ; result += b ; for ( int i = 1 ; i < size ; i ++ ) result += c ; result += d ; return result ; } public static void main ( String [ ] args ) { String num_3 , num_6 , num_9 ; num_3 = "3333" ; num_6 = "6666" ; num_9 = "9999" ; String result = " " ; result = find_Square_369 ( num_3 ) ; System . out . println ( " Square ▁ of ▁ " + num_3 + " ▁ is ▁ : ▁ " + result ) ; result = find_Square_369 ( num_6 ) ; System . out . println ( " Square ▁ of ▁ " + num_9 + " ▁ is ▁ : ▁ " + result ) ; result = find_Square_369 ( num_9 ) ; System . out . println ( " Square ▁ of ▁ " + num_9 + " ▁ is ▁ : ▁ " + result ) ; } }
class GFG { static boolean isPower ( int n ) { for ( int x = 2 ; x <= ( int ) Math . sqrt ( n ) ; x ++ ) { float f = ( float ) Math . log ( n ) / ( float ) Math . log ( x ) ; if ( ( f - ( int ) f ) == 0.0 ) return true ; } return false ; } public static void main ( String args [ ] ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) System . out . print ( i + " ▁ " ) ; } }
class GFG { static int N = 1000005 ; static void sumOddDigit ( int digitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ i / 10 ] + ( i & 1 ) * ( i % 10 ) ; } } static void sumFactor ( int digitSum [ ] , int factorDigitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } static void wrapper ( int q , int n [ ] ) { int digitSum [ ] = new int [ N ] ; int factorDigitSum [ ] = new int [ N ] ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( int i = 0 ; i < q ; i ++ ) System . out . print ( factorDigitSum [ n [ i ] ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int q = 2 ; int n [ ] = new int [ ] { 10 , 36 } ; wrapper ( q , n ) ; } }
class GFG { static int M = 10 ; static void PrintMatrix ( float a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static int PerformOperation ( float a [ ] [ ] , int n ) { int i , j , k = 0 , c , flag = 0 , m = 0 ; float pro = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] [ i ] == 0 ) { c = 1 ; while ( ( i + c ) < n && a [ i + c ] [ i ] == 0 ) c ++ ; if ( ( i + c ) == n ) { flag = 1 ; break ; } for ( j = i , k = 0 ; k <= n ; k ++ ) { float temp = a [ j ] [ k ] ; a [ j ] [ k ] = a [ j + c ] [ k ] ; a [ j + c ] [ k ] = temp ; } } for ( j = 0 ; j < n ; j ++ ) { if ( i != j ) { float p = a [ j ] [ i ] / a [ i ] [ i ] ; for ( k = 0 ; k <= n ; k ++ ) a [ j ] [ k ] = a [ j ] [ k ] - ( a [ i ] [ k ] ) * p ; } } } return flag ; } static void PrintResult ( float a [ ] [ ] , int n , int flag ) { System . out . print ( " Result ▁ is ▁ : ▁ " ) ; if ( flag == 2 ) System . out . println ( " Infinite ▁ Solutions ▁ Exists " ) ; else if ( flag == 3 ) System . out . println ( " No ▁ Solution ▁ Exists " ) ; else { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] [ n ] / a [ i ] [ i ] + " ▁ " ) ; } } static int CheckConsistency ( float a [ ] [ ] , int n , int flag ) { int i , j ; float sum ; flag = 3 ; for ( i = 0 ; i < n ; i ++ ) { sum = 0 ; for ( j = 0 ; j < n ; j ++ ) sum = sum + a [ i ] [ j ] ; if ( sum == a [ i ] [ j ] ) flag = 2 ; } return flag ; } public static void main ( String [ ] args ) { float a [ ] [ ] = { { 0 , 2 , 1 , 4 } , { 1 , 1 , 2 , 6 } , { 2 , 1 , 1 , 7 } } ; int n = 3 , flag = 0 ; flag = PerformOperation ( a , n ) ; if ( flag == 1 ) flag = CheckConsistency ( a , n , flag ) ; System . out . println ( " Final ▁ Augumented ▁ Matrix ▁ is ▁ : ▁ " ) ; PrintMatrix ( a , n ) ; System . out . println ( " " ) ; PrintResult ( a , n , flag ) ; } }
class GFG { public static void main ( String [ ] args ) { long ans = 1 ; long mod = ( long ) 1000000007 * 120 ; for ( int i = 0 ; i < 5 ; i ++ ) ans = ( ans * ( 55555 - i ) ) % mod ; ans = ans / 120 ; System . out . println ( " Answer ▁ using " + " ▁ shortcut : ▁ " + ans ) ; } }
import java . io . * ; class GFG { static int number_of_digits ( int n ) { int i ; int res ; int sum = 0 ; for ( i = 4 , res = 1 ; ; i *= 4 , res ++ ) { sum += i ; if ( sum >= n ) break ; } return res ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( number_of_digits ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void reverseorder ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = n ; i >= 2 ; i -- ) if ( prime [ i ] == true ) System . out . print ( i + " ▁ " ) ; } public static void main ( String args [ ] ) { int N = 25 ; System . out . println ( " Prime ▁ number ▁ in ▁ reverse ▁ order " ) ; if ( N == 1 ) System . out . println ( " No ▁ prime ▁ no ▁ exist ▁ in ▁ this ▁ range " ) ; else } }
import java . util . * ; class GFG { static void checkVantieghemsTheorem ( int limit ) { long prod = 1 ; for ( long n = 2 ; n < limit ; n ++ ) { if ( ( ( prod - n < 0 ? 0 : prod - n ) % ( ( 1 << n ) - 1 ) ) == 0 ) System . out . print ( n + " is primeNEW_LINE"); prod *= ( ( 1 << n ) - 1 ) ; } } public static void main ( String [ ] args ) { checkVantieghemsTheorem ( 10 ) ; } }
class GFG { static int mod = ( int ) ( 1e9 + 7 ) ; static int N = 1000005 ; static int fact [ ] = new int [ N ] , invfact [ ] = new int [ N ] ; static int check ( int x , int a , int b ) { if ( x == 0 ) { return 0 ; } while ( x > 0 ) { if ( x % 10 != a & x % 10 != b ) { return 0 ; } x /= 10 ; } return 1 ; } static int modInverse ( int a , int m ) { int m0 = m ; int y = 0 , x = 1 ; if ( m == 1 ) { return 0 ; } while ( a > 1 ) { int q = a / m ; int t = m ; m = a % m ; a = t ; t = y ; y = x - q * y ; x = t ; } if ( x < 0 ) { x += m0 ; } return x ; } static void pregenFact ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 1 ; i <= 1000000 ; ++ i ) { fact [ i ] = ( int ) ( ( long ) fact [ i - 1 ] * i % mod ) ; } } static void pregenInverse ( ) { invfact [ 0 ] = invfact [ 1 ] = 1 ; invfact [ 1000000 ] = modInverse ( fact [ 1000000 ] , mod ) ; for ( int i = 999999 ; i > 1 ; -- i ) { invfact [ i ] = ( int ) ( ( ( long ) invfact [ i + 1 ] * ( long ) ( i + 1 ) ) % mod ) ; } } static int comb ( int big , int small ) { return ( int ) ( ( long ) fact [ big ] * invfact [ small ] % mod * invfact [ big - small ] % mod ) ; } static int count ( int a , int b , int n ) { pregenFact ( ) ; pregenInverse ( ) ; if ( a == b ) { return ( check ( a * n , a , b ) ) ; } int ans = 0 ; for ( int i = 0 ; i <= n ; ++ i ) { if ( check ( i * a + ( n - i ) * b , a , b ) == 1 ) { ans = ( ans + comb ( n , i ) ) % mod ; } } return ans ; } public static void main ( String [ ] args ) { int a = 3 , b = 4 , n = 11028 ; System . out . println ( count ( a , b , n ) ) ; } }
class GFG { public static int magicOfSequence ( int N ) { int sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) sum += ( i * i * i + i * 2 ) ; return sum ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( magicOfSequence ( N ) ) ; } }
import java . io . * ; class GFG { static int countOddFactors ( long n ) { int odd_factors = 0 ; for ( int i = 1 ; 1 * i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( 1 * i * i == n ) { if ( ( i & 1 ) == 1 ) odd_factors ++ ; } else { if ( ( i & 1 ) == 1 ) odd_factors ++ ; int factor = ( int ) n / i ; if ( ( factor & 1 ) == 1 ) odd_factors ++ ; } } } return odd_factors - 1 ; } public static void main ( String args [ ] ) { long N = 15 ; System . out . println ( countOddFactors ( N ) ) ; N = 10 ; System . out . println ( countOddFactors ( N ) ) ; } }
import java . io . * ; class GFG { static boolean isPossibleToZero ( int a [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i & 1 ) == 0 ) odd += a [ i ] ; else even += a [ i ] ; } return ( odd == even ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = arr . length ; if ( isPossibleToZero ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static List < Integer > primeFactors ( int n ) { List < Integer > res = new ArrayList < Integer > ( ) ; if ( n % 2 == 0 ) { while ( n % 2 == 0 ) n = n / 2 ; res . add ( 2 ) ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { while ( n % i == 0 ) n = n / i ; res . add ( i ) ; } } if ( n > 2 ) res . add ( n ) ; return res ; } static boolean isHoax ( int n ) { List < Integer > pf = primeFactors ( n ) ; if ( pf . get ( 0 ) == n ) return false ; int all_pf_sum = 0 ; for ( int i = 0 ; i < pf . size ( ) ; i ++ ) { int pf_sum ; for ( pf_sum = 0 ; pf . get ( i ) > 0 ; pf_sum += pf . get ( i ) % 10 , pf . set ( i , pf . get ( i ) / 10 ) ) ; all_pf_sum += pf_sum ; } int sum_n ; for ( sum_n = 0 ; n > 0 ; sum_n += n % 10 , n /= 10 ) ; return sum_n == all_pf_sum ; } public static void main ( String args [ ] ) { int n = 84 ; if ( isHoax ( n ) ) System . out . print ( "A Hoax NumberNEW_LINE"); else System . out . print ( "Not a Hoax NumberNEW_LINE"); } }
import java . io . * ; class GFG { static int fact ( int n ) { if ( n == 0 n == 1 ) return 1 ; int ans = 1 ; for ( int i = 1 ; i <= n ; i ++ ) ans = ans * i ; return ans ; } static int nCr ( int n , int r ) { int Nr = n , Dr = 1 , ans = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { ans = ( ans * Nr ) / ( Dr ) ; Nr -- ; Dr ++ ; } return ans ; } static int solve ( int n ) { int N = 2 * n - 2 ; int R = n - 1 ; return nCr ( N , R ) * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( solve ( n ) ) ; } }
class GFG { static int solve ( int n , int base ) { int result = 0 ; while ( n > 0 ) { int remainder = n % base ; result = result + remainder ; n = n / base ; } return result ; } static void printSumsOfDigits ( int n ) { for ( int base = 2 ; base < n ; ++ base ) System . out . print ( solve ( n , base ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 8 ; printSumsOfDigits ( n ) ; } }
class GFG { static boolean check ( int N , int D ) { int temp = ( N * ( N + 1 ) ) / 2 + D ; return ( temp % 2 == 0 ) ; } static public void main ( String args [ ] ) { int N = 5 ; int M = 7 ; if ( check ( N , M ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
import java . io . * ; import java . lang . * ; public class GFG { static int perfectSquare ( String s ) { int n = s . length ( ) ; int ans = - 1 ; String num = " " ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { String str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) { str += s . charAt ( j ) ; } } if ( str . charAt ( 0 ) != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) temp = temp * 10 + ( int ) ( str . charAt ( j ) - '0' ) ; int k = ( int ) Math . sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . length ( ) ) { ans = ( int ) str . length ( ) ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { System . out . print ( num + " ▁ " ) ; return n - ans ; } } public static void main ( String args [ ] ) { System . out . println ( perfectSquare ( "8314" ) ) ; System . out . println ( perfectSquare ( "753" ) ) ; } }
class GFG { static void printFourSquares ( int a ) { for ( int i = 0 ; i * i <= a ; i ++ ) { for ( int j = i ; j * j <= a ; j ++ ) { for ( int k = j ; k * k <= a ; k ++ ) { for ( int l = k ; l * l <= a ; l ++ ) { if ( i * i + j * j + k * k + l * l == a ) { System . out . print ( a + " ▁ = ▁ " + i + " * " + i + " ▁ + ▁ " + j + " * " + j + " ▁ + ▁ " ) ; System . out . println ( k + " * " + k + " ▁ + ▁ " + l + " * " + l ) ; } } } } } } public static void main ( String [ ] args ) { int a = 74 ; printFourSquares ( a ) ; } }
import java . io . * ; class GFG { static int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 51242183 ; System . out . println ( " The ▁ number ▁ of ▁ distinct ▁ " + " prime ▁ factors ▁ is / are ▁ " + exactPrimeFactorCount ( n ) ) ; System . out . println ( " The ▁ value ▁ of ▁ log ( log ( n ) ) " + " ▁ is ▁ " + Math . log ( Math . log ( n ) ) ) ; } }
class GFG { static int no_of_digit ( int a , int b ) { return ( ( int ) ( b * Math . log10 ( a ) ) + 1 ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 100 ; System . out . print ( " no . ▁ of ▁ digits ▁ = ▁ " + no_of_digit ( a , b ) ) ; } }
import java . io . * ; class GFG { static boolean checkSemiprime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) { while ( num % i == 0 ) { num /= i ; ++ cnt ; } } if ( num > 1 ) ++ cnt ; return cnt == 2 ; } static boolean isEmirpimes ( int n ) { if ( checkSemiprime ( n ) == false ) return false ; int r = 0 ; for ( int t = n ; t != 0 ; t = t / n ) r = r * 10 + t % 10 ; if ( r == n ) return false ; return ( checkSemiprime ( r ) ) ; } public static void main ( String [ ] args ) { int n = 15 ; if ( isEmirpimes ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static void add ( int ac [ ] , int x [ ] , int qrn ) { int i , c = 0 ; for ( i = 0 ; i < qrn ; i ++ ) { ac [ i ] = ac [ i ] + x [ i ] + c ; if ( ac [ i ] > 1 ) { ac [ i ] = ac [ i ] % 2 ; c = 1 ; } else { c = 0 ; } } } static void complement ( int a [ ] , int n ) { int i ; int [ ] x = new int [ 8 ] ; x [ 0 ] = 1 ; for ( i = 0 ; i < n ; i ++ ) { a [ i ] = ( a [ i ] + 1 ) % 2 ; } add ( a , x , n ) ; } static void rightShift ( int ac [ ] , int qr [ ] , int qn , int qrn ) { int temp , i ; temp = ac [ 0 ] ; qn = qr [ 0 ] ; System . out . print ( " TABSYMBOL TABSYMBOL rightShift TABSYMBOL " ) ; for ( i = 0 ; i < qrn - 1 ; i ++ ) { ac [ i ] = ac [ i + 1 ] ; qr [ i ] = qr [ i + 1 ] ; } qr [ qrn - 1 ] = temp ; } static void display ( int ac [ ] , int qr [ ] , int qrn ) { int i ; for ( i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } System . out . print ( " TABSYMBOL " ) ; for ( i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( qr [ i ] ) ; } } static void boothAlgorithm ( int br [ ] , int qr [ ] , int mt [ ] , int qrn , int sc ) { int qn = 0 ; int [ ] ac = new int [ 10 ] ; int temp = 0 ; System . out . print ( "qn	q[n+1]		BR		AC	QR		scNEW_LINE"); System . out . print ( " TABSYMBOL TABSYMBOL TABSYMBOL initial TABSYMBOL TABSYMBOL " ) ; display ( ac , qr , qrn ) ; System . out . print ( " TABSYMBOL TABSYMBOL " + sc + "NEW_LINE"); while ( sc != 0 ) { System . out . print ( qr [ 0 ] + " TABSYMBOL " + qn ) ; if ( ( qn + qr [ 0 ] ) == 1 ) { if ( temp == 0 ) { add ( ac , mt , qrn ) ; System . out . print ( " TABSYMBOL TABSYMBOL A ▁ = ▁ A ▁ - ▁ BR TABSYMBOL " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } temp = 1 ; } else if ( temp == 1 ) { add ( ac , br , qrn ) ; System . out . print ( " TABSYMBOL TABSYMBOL A ▁ = ▁ A ▁ + ▁ BR TABSYMBOL " ) ; for ( int i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( ac [ i ] ) ; } temp = 0 ; } System . out . print ( "NEW_LINE	"); rightShift ( ac , qr , qn , qrn ) ; } else if ( qn - qr [ 0 ] == 0 ) { rightShift ( ac , qr , qn , qrn ) ; } display ( ac , qr , qrn ) ; System . out . print ( " TABSYMBOL " ) ; sc -- ; System . out . print ( " TABSYMBOL " + sc + "NEW_LINE"); } } static void reverse ( int a [ ] ) { int i , k , n = a . length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { int [ ] mt = new int [ 10 ] ; int sc ; int brn , qrn ; brn = 4 ; int br [ ] = { 0 , 1 , 1 , 0 } ; for ( int i = brn - 1 ; i >= 0 ; i -- ) { mt [ i ] = br [ i ] ; } reverse ( br ) ; complement ( mt , brn ) ; qrn = 4 ; sc = qrn ; int qr [ ] = { 1 , 0 , 1 , 0 } ; reverse ( qr ) ; boothAlgorithm ( br , qr , mt , qrn , sc ) ; System . out . print ( " " ▁ + ▁ " Result = "); for ( int i = qrn - 1 ; i >= 0 ; i -- ) { System . out . print ( qr [ i ] ) ; } } }
class GFG { static long factorial ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } static void printNComposite ( int n ) { long fact = factorial ( n + 1 ) ; for ( int i = 2 ; i <= n + 1 ; ++ i ) { System . out . print ( fact + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printNComposite ( n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Long > primes ( long n ) { boolean [ ] prime = new boolean [ ( int ) n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( prime [ i ] == true ) { for ( int j = i * 2 ; j <= n ; j += i ) prime [ j ] = false ; } } ArrayList < Long > arr = new ArrayList < Long > ( ) ; for ( int i = 2 ; i < n ; i ++ ) if ( prime [ i ] ) arr . add ( ( long ) i ) ; return arr ; } static int countDigits ( long n ) { long temp = n ; int c = 0 ; while ( temp != 0 ) { temp = temp / 10 ; c ++ ; } return c ; } static boolean frugal ( long n ) { ArrayList < Long > r = primes ( n ) ; long t = n ; long s = 0 ; for ( int i = 0 ; i < r . size ( ) ; i ++ ) { if ( t % r . get ( i ) == 0 ) { long k = 0 ; while ( t % r . get ( i ) == 0 ) { t = t / r . get ( i ) ; k ++ ; } if ( k == 1 ) s = s + countDigits ( r . get ( i ) ) ; else if ( k != 1 ) s = s + countDigits ( r . get ( i ) ) + countDigits ( k ) ; } } return ( countDigits ( n ) > s && s != 0 ) ; } public static void main ( String [ ] args ) { long n = 343 ; if ( frugal ( n ) ) System . out . print ( "A Frugal numberNEW_LINE"); else System . out . print ( "Not a frugal numberNEW_LINE"); } }
class GFG { static int nthSquareCube ( int n ) { return n * n * n * n * n * n ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( nthSquareCube ( n ) ) ; } }
class GFG { static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; } public static void main ( String [ ] args ) { int s = 9 ; int n = findS ( s ) ; if ( n == - 1 ) System . out . println ( " - 1" ) ; else System . out . println ( n ) ; } }
class GFG { static int findKthGoodNo ( int n ) { int lastDig = n % 10 ; if ( lastDig >= 0 && lastDig <= 4 ) return n << 1 ; else return ( n << 1 ) + 1 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findKthGoodNo ( n ) ) ; } }
import java . io . * ; public class GFG { static int checkDigits ( int n ) { while ( n > 0 ) { if ( ( ( n % 10 ) % 2 ) > 0 ) return 0 ; n /= 10 ; } return 1 ; } static int largestNumber ( int n ) { for ( int i = n ; ; i -- ) if ( checkDigits ( i ) > 0 ) return i ; } static public void main ( String [ ] args ) { int N = 23 ; System . out . println ( largestNumber ( N ) ) ; } }
class GFG { static int largestNumber ( int n ) { String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( ( ( int ) ( s . charAt ( i ) - '0' ) % 2 ) & 1 ) > 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( int ) ( s . charAt ( i ) - '0' ) ; num = num * 10 + ( ( int ) s . charAt ( index ) - ( int ) ( '0' ) - 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 8 ; return num ; } public static void main ( String [ ] args ) { int N = 24578 ; System . out . println ( largestNumber ( N ) ) ; } }
import java . util . * ; class Gfg { static int countDigits ( int n ) { return ( int ) ( n * Math . log10 ( 2 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( countDigits ( n ) ) ; } }
class GFG { static int check_digits ( int n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 != 0 ) return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) != 0 ) return i ; } public static void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallest_number ( N ) ) ; } }
class GFG { static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static boolean checkFriendly ( int n , int m ) { int sumFactors_n = sumofFactors ( n ) ; int sumFactors_m = sumofFactors ( m ) ; int gcd_n = gcd ( n , sumFactors_n ) ; int gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) System . out . print ( "YesNEW_LINE"); else System . out . print ( "NoNEW_LINE"); } }
import java . lang . * ; import java . util . * ; class GFG { public static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } public static int primorial ( int n ) { int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } public static int findNextPrime ( int n ) { int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } public static int fortunateNumber ( int n ) { int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( fortunateNumber ( n ) ) ; } }
class GFG { static double procal ( int n ) { return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ; } public static void main ( String arg [ ] ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; System . out . print ( Math . round ( procal ( n ) * 1000000.0 ) / 1000000.0 ) ; } }
import java . io . * ; class GFG { static void testSomeNumbers ( int limit , int n ) { if ( n < 3 ) return ; for ( int a = 1 ; a <= limit ; a ++ ) for ( int b = a ; b <= limit ; b ++ ) { int pow_sum = ( int ) ( Math . pow ( a , n ) + Math . pow ( b , n ) ) ; double c = Math . pow ( pow_sum , 1.0 / n ) ; int c_pow = ( int ) Math . pow ( ( int ) c , n ) ; if ( c_pow == pow_sum ) { System . out . println ( " Count ▁ example ▁ found " ) ; return ; } } System . out . println ( " No ▁ counter ▁ example ▁ within ▁ given " + " ▁ range ▁ and ▁ data " ) ; } public static void main ( String [ ] args ) { testSomeNumbers ( 12 , 5 ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } static void series ( int A , int X , int n ) { int nFact = factorial ( n ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { int niFact = factorial ( n - i ) ; int iFact = factorial ( i ) ; int aPow = ( int ) Math . pow ( A , n - i ) ; int xPow = ( int ) Math . pow ( X , i ) ; System . out . print ( ( nFact * aPow * xPow ) / ( niFact * iFact ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void productReduce ( int n , int num [ ] , int den [ ] ) { int new_num = 1 , new_den = 1 ; for ( int i = 0 ; i < n ; i ++ ) { new_num *= num [ i ] ; new_den *= den [ i ] ; } int GCD = gcd ( new_num , new_den ) ; new_num /= GCD ; new_den /= GCD ; System . out . println ( new_num + " / " + new_den ) ; } public static void main ( String [ ] args ) { int n = 3 ; int num [ ] = { 1 , 2 , 5 } ; int den [ ] = { 2 , 1 , 6 } ; productReduce ( n , num , den ) ; } }
import java . io . * ; class GFG { static int fnMod ( int n ) { if ( n % 5 == 1 ) return 4 ; else return 0 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( fnMod ( n ) ) ; n = 11 ; System . out . println ( fnMod ( n ) ) ; } }
import java . io . * ; class GFG { static int totalSumDivisibleByNum ( int n , int number ) { int firstnum = ( int ) Math . pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 3 , num = 7 ; System . out . println ( totalSumDivisibleByNum ( n , num ) ) ; } }
import java . util . * ; class GFG { static String fractionToDecimal ( int numerator , int denominator ) { String res = " " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ! mp . containsKey ( rem ) ) { mp . put ( rem , res . length ( ) ) ; rem = rem * 10 ; int res_part = rem / denominator ; res += res_part + " " ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . substring ( mp . get ( rem ) ) ; } static boolean isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void ExtendedMidys ( String str , int n , int m ) { if ( ! isPrime ( n ) ) { System . out . print ( " Denominator ▁ is ▁ not ▁ prime , ▁ " + " thus ▁ Extended ▁ Midy ' s ▁ theorem ▁ " + " is ▁ not ▁ applicable " ) ; return ; } int l = str . length ( ) ; int part1 = 0 , part2 = 0 ; if ( l % 2 == 0 && l % m == 0 ) { int [ ] part = new int [ m ] ; int sum = 0 , res = 0 ; for ( int i = 0 ; i < l ; i ++ ) { int var = i / m ; part [ var ] = part [ var ] * 10 + ( str . charAt ( i ) - '0' ) ; } for ( int i = 0 ; i < m ; i ++ ) { sum = sum + part [ i ] ; System . out . print ( part [ i ] + " ▁ " ) ; } System . out . println ( ) ; res = ( int ) Math . pow ( 10 , m ) - 1 ; if ( sum % res == 0 ) System . out . print ( " Extended ▁ Midy ' s ▁ " + " theorem ▁ holds ! " ) ; else System . out . print ( " Extended ▁ Midy ' s ▁ " + " theorem ▁ doesn ' t ▁ hold ! " ) ; } else if ( l % 2 != 0 ) { System . out . print ( " The ▁ repeating ▁ decimal ▁ is ▁ of ▁ " + " odd ▁ length ▁ thus ▁ Extended ▁ Midy ' s ▁ " + " theorem ▁ is ▁ not ▁ applicable " ) ; } else if ( l % m != 0 ) { System . out . print ( " The ▁ repeating ▁ decimal ▁ can ▁ " + " not ▁ be ▁ divided ▁ into ▁ m ▁ digits " ) ; } } public static void main ( String [ ] args ) { int numr = 1 , denr = 17 , m = 4 ; String res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) System . out . print ( " The ▁ fraction ▁ does ▁ not ▁ " + " have ▁ repeating ▁ decimal " ) ; else { System . out . println ( " Repeating ▁ decimal ▁ = ▁ " + res ) ; ExtendedMidys ( res , denr , m ) ; } } }
import java . io . * ; class GFG { static int numberofterm ( int n , int number ) { int firstnum = ( int ) Math . pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , n ) ; int count = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 3 , num = 7 ; System . out . println ( numberofterm ( n , num ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int seiresSum ( int n , int [ ] a ) { int res = 0 , i ; for ( i = 0 ; i < 2 * n ; i ++ ) { if ( i % 2 == 0 ) res += a [ i ] * a [ i ] ; else res -= a [ i ] * a [ i ] ; } return res ; } public static void main ( String args [ ] ) { int n = 2 ; int a [ ] = { 1 , 2 , 3 , 4 } ; System . out . println ( seiresSum ( n , a ) ) ; } }
import java . io . * ; class GFG { static void georgeCantor ( int n ) { int i = 1 ; int j = 1 ; int k = 1 ; while ( k < n ) { j ++ ; k ++ ; if ( k == n ) break ; while ( j > 1 && k < n ) { i ++ ; j -- ; k ++ ; } if ( k == n ) break ; i ++ ; k ++ ; if ( k == n ) break ; while ( i > 1 && k < n ) { i -- ; j ++ ; k ++ ; } } System . out . println ( " N - th ▁ term ▁ : ▁ " + i + " / " + j ) ; } public static void main ( String [ ] args ) { int n = 15 ; georgeCantor ( n ) ; } }
import java . io . * ; class GFG { static boolean isDivisible ( long n ) { while ( n / 100 > 0 ) { int last_digit = ( int ) n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } public static void main ( String [ ] args ) { long n = 348 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static String solveEquation ( String equation ) { int n = equation . length ( ) , sign = 1 , coeff = 0 ; int total = 0 , i = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( equation . charAt ( j ) == ' + ' || equation . charAt ( j ) == ' - ' ) { if ( j > i ) total += sign * Integer . parseInt ( equation . substring ( i , j ) ) ; i = j ; } else if ( equation . charAt ( j ) == ' x ' ) { if ( ( i == j ) || equation . charAt ( j - 1 ) == ' + ' ) coeff += sign ; else if ( equation . charAt ( j - 1 ) == ' - ' ) coeff -= sign ; else coeff += sign * Integer . parseInt ( equation . substring ( i , j ) ) ; i = j + 1 ; } else if ( equation . charAt ( j ) == ' = ' ) { if ( j > i ) total += sign * Integer . parseInt ( equation . substring ( i , j ) ) ; sign = - 1 ; i = j + 1 ; } } if ( i < n ) total = total + sign * Integer . parseInt ( equation . substring ( i ) ) ; if ( coeff == 0 && total == 0 ) return " Infinite ▁ solutions " ; if ( coeff == 0 && total != 0 ) return " No ▁ solution " ; int ans = - total / coeff ; return ( Integer . toString ( ans ) ) ; } public static void main ( String args [ ] ) { String equation = new String ( " x + 5-3 + x = 6 + x - 2" ) ; System . out . print ( " x ▁ = ▁ " + solveEquation ( equation ) ) ; } }
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { static boolean pronic_check ( int n ) { int x = ( int ) ( Math . sqrt ( n ) ) ; if ( x * ( x + 1 ) == n ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 56 ; if ( pronic_check ( n ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static int power ( int n , int r ) { int count = 0 ; for ( int i = r ; ( n / i ) >= 1 ; i = i * r ) count += n / i ; return count ; } public static void main ( String [ ] args ) { int n = 6 , r = 3 ; System . out . print ( power ( n , r ) ) ; } }
import java . util . * ; class GFG { static void LucasLehmer ( int n ) { long current_val = 4 ; ArrayList < Long > series = new ArrayList < > ( ) ; series . add ( current_val ) ; for ( int i = 0 ; i < n ; i ++ ) { current_val = current_val * current_val - 2 ; series . add ( current_val ) ; } for ( int i = 0 ; i <= n ; i ++ ) { System . out . println ( " Term ▁ " + i + " : ▁ " + series . get ( i ) ) ; } } public static void main ( String [ ] args ) { int n = 5 ; LucasLehmer ( n ) ; } }
import java . io . * ; class GFG { static void checkperfectsquare ( int n ) { if ( Math . ceil ( ( double ) Math . sqrt ( n ) ) == Math . floor ( ( double ) Math . sqrt ( n ) ) ) { System . out . print ( " perfect ▁ square " ) ; } else { System . out . print ( " not ▁ a ▁ perfect ▁ square " ) ; } } public static void main ( String [ ] args ) { int n = 49 ; checkperfectsquare ( n ) ; } }
import java . lang . * ; class GFG { static boolean check ( int n , int p ) { int maximum = - 1 ; while ( ( n % 2 ) == 0 ) { maximum = Math . max ( maximum , 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maximum = Math . max ( maximum , i ) ; n = n / i ; } } if ( n > 2 ) maximum = Math . max ( maximum , n ) ; return ( maximum <= p ) ; } public static void main ( String [ ] args ) { int n = 24 , p = 7 ; if ( check ( n , p ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
import java . io . * ; class GFG { static int sum_series ( int n ) { int nSquare = n * n ; return nSquare * ( nSquare - 1 ) / 4 ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sum_series ( n ) ) ; } }
class tomohiko_sakamoto { public static int day_of_the_week ( int y , int m , int d ) { int t [ ] = { 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 } ; if ( m < 3 ) y -= 1 ; return ( y + y / 4 - y / 100 + y / 400 + t [ m - 1 ] + d ) % 7 ; } public static void main ( String args [ ] ) { int day = 13 , month = 7 , year = 2017 ; System . out . println ( day_of_the_week ( year , month , day ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n , int i ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; } public static void main ( String [ ] args ) { int n = 15 ; if ( isPrime ( n , 2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int sumOfSeries ( int n ) { return ( n * ( 2 * n - 1 ) * ( 2 * n + 1 ) ) / 3 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
class GFG { static float mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float SSD ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; return ( float ) Math . sqrt ( sum / ( n - 1 ) ) ; } static float sampleError ( float arr [ ] , int n ) { return SSD ( arr , n ) / ( float ) Math . sqrt ( n ) ; } public static void main ( String [ ] args ) { float arr [ ] = { 78.53f , 79.62f , 80.25f , 81.05f , 83.21f , 83.46f } ; int n = arr . length ; System . out . println ( sampleError ( arr , n ) ) ; } }
import java . lang . * ; class GFG { static int StepstoReachTarget ( int target ) { target = Math . abs ( target ) ; int n = ( int ) Math . ceil ( ( - 1.0 + ( int ) Math . sqrt ( 1 + 8.0 * target ) ) / 2 ) ; int sum = n * ( n + 1 ) / 2 ; if ( sum == target ) return n ; int d = sum - target ; if ( ( d & 1 ) == 0 ) return n ; else return n + ( ( n & 1 ) != 0 ? 2 : 1 ) ; } public static void main ( String [ ] arg ) { int target = 5 ; System . out . println ( StepstoReachTarget ( target ) ) ; } }
import java . io . * ; class GFG { static int avg_of_odd_num ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( 2 * i + 1 ) ; return sum / n ; } public static void main ( String [ ] args ) { int n = 20 ; avg_of_odd_num ( n ) ; System . out . println ( avg_of_odd_num ( n ) ) ; } }
import java . io . * ; class GFG { static int avg_of_odd_num ( int n ) { return n ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( avg_of_odd_num ( n ) ) ; } }
import java . io . * ; class GFG { static double seriesSum ( int n ) { int i = 1 ; double res = 0.0 ; boolean sign = true ; while ( n > 0 ) { n -- ; if ( sign ) { sign = ! sign ; res = res + ( double ) ++ i / ++ i ; } else { sign = ! sign ; res = res - ( double ) ++ i / ++ i ; } } return res ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( seriesSum ( n ) ) ; } }
import java . io . * ; class GFG { static int modInverse ( int a , int prime ) { a = a % prime ; for ( int x = 1 ; x < prime ; x ++ ) if ( ( a * x ) % prime == 1 ) return x ; return - 1 ; } static void printModIverses ( int n , int prime ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( modInverse ( i , prime ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 , prime = 17 ; printModIverses ( n , prime ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countSymmetric ( int n ) { if ( n == 0 ) return 1 ; return 1 << ( ( n * ( n + 1 ) ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( countSymmetric ( n ) ) ; } }
import java . io . * ; class GFG { static int centeredNonagonal ( int n ) { return ( 3 * n - 2 ) * ( 3 * n - 1 ) / 2 ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( centeredNonagonal ( n ) ) ; } }
import java . io . * ; class GFG { static void fib ( int f [ ] , int N ) { f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( int i = 3 ; i <= N ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fiboTriangle ( int n ) { int N = n * ( n + 1 ) / 2 ; int f [ ] = new int [ N + 1 ] ; fib ( f , N ) ; int fiboNum = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( f [ fiboNum ++ ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 5 ; fiboTriangle ( n ) ; } }
import java . io . * ; class GFG { static float Mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float meanAbsDevtion ( float arr [ ] , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + Math . abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } public static void main ( String [ ] args ) { float arr [ ] = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = arr . length ; System . out . println ( meanAbsDevtion ( arr , n ) ) ; } }
import java . io . * ; class Ratio { static boolean isRatioPossible ( int lowCost , int upCost , int lowQuant , int upQuant , int r ) { for ( int i = lowQuant ; i <= upQuant ; i ++ ) { int ans = i * r ; if ( lowCost <= ans && ans <= upCost ) return true ; } return false ; } public static void main ( String args [ ] ) { int lowCost = 14 , upCost = 30 , lowQuant = 5 , upQuant = 12 , r = 9 ; if ( isRatioPossible ( lowCost , upCost , lowQuant , upQuant , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static long oddNumSum ( int n ) { return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( oddNumSum ( n ) ) ; } }
import java . io . * ; class GFG { static int trailingZero ( int x ) { int i = 5 , count = 0 ; while ( x > i ) { count = count + x / i ; i = i * 5 ; } return count ; } static int countProductTrailing ( int M , int N ) { return trailingZero ( N ) + trailingZero ( M ) ; } public static void main ( String args [ ] ) { int N = 67 , M = 98 ; System . out . println ( countProductTrailing ( N , M ) ) ; } }
class GFG { static boolean isTrimorphic ( int N ) { int cube = N * N * N ; while ( N > 0 ) { if ( N % 10 != cube % 10 ) return false ; N /= 10 ; cube /= 10 ; } return true ; } public static void main ( String [ ] args ) { int N = 24 ; if ( isTrimorphic ( N ) == true ) System . out . println ( " trimorphic " ) ; else System . out . println ( " not ▁ trimorphic " ) ; } }
class GFG { static int INT_MAX = 2147483647 ; static boolean checkTrimorphic ( int num ) { int cube = num * num * num ; while ( num > 0 ) { if ( num % 10 != cube % 10 ) return false ; num /= 10 ; cube /= 10 ; } return true ; } static int nthTrimorphic ( int n ) { int count = 0 ; for ( int i = 0 ; i < INT_MAX ; i ++ ) { if ( checkTrimorphic ( i ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( nthTrimorphic ( n ) ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int reachTarget ( int target ) { target = Math . abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void main ( String args [ ] ) { int target = 5 ; System . out . println ( reachTarget ( target ) ) ; } }
import java . io . * ; class GFG { static int averageOdd ( int n ) { if ( n % 2 == 0 ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } int sum = 0 , count = 0 ; while ( n >= 1 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( averageOdd ( n ) ) ; } }
import java . io . * ; class GFG { static long fifthPowerSum ( int n ) { return ( ( 2 * n * n * n * n * n * n ) + ( 6 * n * n * n * n * n ) + ( 5 * n * n * n * n ) - ( n * n ) ) / 12 ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( fifthPowerSum ( n ) ) ; } }
import java . io . * ; class GFG { static int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } public static void main ( String args [ ] ) throws IOException { System . out . println ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }
import java . util . * ; class GFG { static class pair { float first , second ; public pair ( float first , float second ) { this . first = first ; this . second = second ; } } static Vector < pair > Vector ; static float sigma_fx ( pair [ ] a ) { float sum = 0 ; for ( pair i : a ) { sum += i . first * i . second ; } return sum ; } static float sigma_f ( pair [ ] a ) { float sum = 0.0f ; for ( pair i : a ) { sum += i . second ; } return sum ; } static float calculate_mean ( pair [ ] a ) { return sigma_fx ( a ) / sigma_f ( a ) ; } static float calculate_std ( pair [ ] a ) { float f = sigma_f ( a ) ; float mean = sigma_fx ( a ) / f ; float sum = 0 ; for ( pair i : a ) { sum += ( i . first - mean ) * ( i . first - mean ) * i . second ; } return ( float ) Math . sqrt ( sum / f ) ; } public static void main ( String [ ] args ) { pair [ ] A = { new pair ( 0f , 0.1f ) , new pair ( 100f , 0.1f ) , new pair ( 200f , 0.2f ) , new pair ( 333f , 0.3f ) , new pair ( 400f , 0.3f ) } ; pair [ ] B = { new pair ( 100f , 0.1f ) , new pair ( 200f , 0.5f ) , new pair ( 700f , 0.4f ) } ; float avg_A = calculate_mean ( A ) ; float avg_B = calculate_mean ( B ) ; float std_A = calculate_std ( A ) ; float std_B = calculate_std ( B ) ; System . out . print ( " For ▁ Investment ▁ A " + "NEW_LINE"); System . out . print ( " Average : ▁ " + avg_A + "NEW_LINE"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_A + "NEW_LINE"); System . out . print ( " Normalised ▁ Std : ▁ " + std_A / avg_A + "NEW_LINE"); System . out . print ( " For ▁ Investment ▁ B " + "NEW_LINE"); System . out . print ( " Average : ▁ " + avg_B + "NEW_LINE"); System . out . print ( " Standard ▁ Deviation : ▁ " + std_B + "NEW_LINE"); System . out . print ( " Normalised ▁ Std : ▁ " + std_B / avg_B + "NEW_LINE"); if ( ( std_B / avg_B ) < ( std_A / avg_A ) ) System . out . print ( "Investment B is less riskyNEW_LINE"); else System . out . print ( "Investment A is less riskyNEW_LINE"); } }
import java . io . * ; class GFG { static int findDivisor ( int x , int y ) { if ( x == y ) return y ; return 2 ; } public static void main ( String [ ] args ) { int x = 3 , y = 16 ; System . out . println ( findDivisor ( x , y ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String argc [ ] ) { int n = 18 ; System . out . println ( sumofFactors ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static float focal_length_concave ( float R ) { return R / 2 ; } public static float focal_length_convex ( float R ) { return - ( R / 2 ) ; } public static void main ( String argc [ ] ) { float R = 30 ; System . out . print ( " Focal ▁ length ▁ of " + " spherical ▁ concave " + " mirror ▁ is ▁ : ▁ " + focal_length_concave ( R ) + " unitsNEW_LINE"); System . out . println ( " Focal ▁ length ▁ of " + " spherical ▁ convex " + " mirror ▁ is ▁ : ▁ " + focal_length_convex ( R ) + " ▁ units " ) ; } }
class GFG { static class Rational { int nume , deno ; public Rational ( int nume , int deno ) { this . nume = nume ; this . deno = deno ; } } ; static int lcm ( int a , int b ) { return ( a * b ) / ( __gcd ( a , b ) ) ; } static Rational maxRational ( Rational first , Rational sec ) { int k = lcm ( first . deno , sec . deno ) ; int nume1 = first . nume ; int nume2 = sec . nume ; nume1 *= k / ( first . deno ) ; nume2 *= k / ( sec . deno ) ; return ( nume2 < nume1 ) ? first : sec ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { Rational first = new Rational ( 3 , 2 ) ; Rational sec = new Rational ( 3 , 4 ) ; Rational res = maxRational ( first , sec ) ; System . out . print ( res . nume + " / " + res . deno ) ; } }
class GFG { static final int N = 6 ; static void fib ( int f [ ] , int n ) { int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fibcoef ( int fc [ ] [ ] , int f [ ] , int n ) { for ( int i = 0 ; i <= n ; i ++ ) fc [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { int k = j ; while ( k > 0 ) { k -- ; fc [ i ] [ j ] *= f [ k ] ; } k = 1 ; while ( ( j + 1 ) != k ) fc [ i ] [ j ] /= f [ k ++ ] ; } } } static void printFibonomialTriangle ( int n ) { int f [ ] = new int [ N + 1 ] ; fib ( f , n ) ; int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j < i ; j ++ ) dp [ i ] [ j ] = f [ i - j + 1 ] * dp [ i - 1 ] [ j - 1 ] + f [ j - 1 ] * dp [ i - 1 ] [ j ] ; } for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( dp [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 6 ; printFibonomialTriangle ( n ) ; } }
class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 2 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . printf ( " Sum ▁ = ▁ % d " , sumOfTheSeries ( n ) ) ; } }
public class GfG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + i * ( i + 1 ) * ( i + 2 ) ; return sum ; } public static void main ( String s [ ] ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int TrinomialValue ( int n , int k ) { if ( n == 0 && k == 0 ) return 1 ; if ( k < - n k > n ) return 0 ; return TrinomialValue ( n - 1 , k - 1 ) + TrinomialValue ( n - 1 , k ) + TrinomialValue ( n - 1 , k + 1 ) ; } public static void printTrinomial ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = - i ; j <= 0 ; j ++ ) System . out . print ( TrinomialValue ( i , j ) + " ▁ " ) ; for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( TrinomialValue ( i , j ) + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String argc [ ] ) { int n = 4 ; printTrinomial ( n ) ; } }
import java . io . * ; class GFG { static double Series ( double x , int n ) { double sum = 1 , term = 1 , fct = 1 ; double p = 1 , multi = 1 ; for ( int i = 1 ; i < n ; i ++ ) { fct = fct * multi * ( multi + 1 ) ; p = p * x * x ; term = ( - 1 ) * term ; multi += 2 ; sum = sum + ( term * p ) / fct ; } return sum ; } public static void main ( String args [ ] ) { double x = 9 ; int n = 10 ; System . out . printf ( " % .4f " , Series ( x , n ) ) ; } }
public class GfG { static int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } static int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } static int find_con_zero ( int arr [ ] , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } public static void main ( String s [ ] ) { int arr [ ] = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; System . out . println ( find_con_zero ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void subsetDifference ( int n ) { int s = n * ( n + 1 ) / 2 ; if ( n % 4 == 0 ) { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Difference ▁ = ▁ " + 0 ) ; } else { if ( n % 4 == 1 n % 4 == 2 ) { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + ( ( s / 2 ) + 1 ) ) ; System . out . println ( " Difference ▁ = ▁ " + 1 ) ; } else { System . out . println ( " First ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Second ▁ subset ▁ sum ▁ = ▁ " + s / 2 ) ; System . out . println ( " Difference ▁ = ▁ " + 0 ) ; } } } public static void main ( String [ ] args ) { int n = 6 ; subsetDifference ( n ) ; } }
import java . io . * ; public class GFG { static void timeToMeet ( double s , double v ) { double V = 3 * v / 2 ; double time = s / V ; System . out . println ( ( float ) time ) ; } static public void main ( String [ ] args ) { double s = 25 , v = 56 ; timeToMeet ( s , v ) ; } }
import java . util . * ; class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
import java . util . * ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { System . out . println ( " - 1 ▁ 0 ▁ 1" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { System . out . println ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 6 ; checksum ( n ) ; } }
import java . io . * ; class GFG { static int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } } return sum ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( divisorSum ( n ) ) ; n = 5 ; System . out . println ( divisorSum ( n ) ) ; } }
import java . io . * ; class GFG { static void printbinomial ( int max ) { for ( int m = 0 ; m <= max ; m ++ ) { System . out . print ( m + " ▁ " ) ; int binom = 1 ; for ( int x = 0 ; x <= m ; x ++ ) { if ( m != 0 && x != 0 ) binom = binom * ( m - x + 1 ) / x ; System . out . print ( binom + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int max = 10 ; printbinomial ( max ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int sumOfLargePrimeFactor ( int n ) { int prime [ ] = new int [ n + 1 ] , sum = 0 ; Arrays . fill ( prime , 0 ) ; int max = n / 2 ; for ( int p = 2 ; p <= max ; p ++ ) { if ( prime [ p ] == 0 ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = p ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] != 0 ) sum += prime [ p ] ; else sum += p ; } return sum ; } public static void main ( String args [ ] ) { int n = 12 ; System . out . println ( " Sum ▁ = ▁ " + sumOfLargePrimeFactor ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static long maxPrimeFactors ( long n ) { long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } while ( n % 3 == 0 ) { maxPrime = 3 ; n = n / 3 ; } for ( int i = 5 ; i <= Math . sqrt ( n ) ; i += 6 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } while ( n % ( i + 2 ) == 0 ) { maxPrime = i + 2 ; n = n / ( i + 2 ) ; } } if ( n > 4 ) maxPrime = n ; return maxPrime ; } public static void main ( String [ ] args ) { Long n = 15l ; System . out . println ( maxPrimeFactors ( n ) ) ; n = 25698751364526l ; System . out . println ( maxPrimeFactors ( n ) ) ; } }
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countUnsetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } public static void main ( String [ ] args ) { int n = 80 ; int l = 1 , r = 4 ; System . out . print ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }
import java . util . * ; class GFG { static String fractionToDecimal ( int numerator , int denominator ) { String res = " " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int rem = numerator % denominator ; while ( ( rem != 0 ) && ! mp . containsKey ( rem ) ) { mp . put ( rem , res . length ( ) ) ; rem = rem * 10 ; int res_part = rem / denominator ; res += res_part + " " ; rem = rem % denominator ; } return ( rem == 0 ) ? " - 1" : res . substring ( mp . get ( rem ) ) ; } static boolean isPrime ( int n ) { for ( int i = 2 ; i <= n / 2 ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void Midys ( String str , int n ) { int l = str . length ( ) ; int part1 = 0 , part2 = 0 ; if ( ! isPrime ( n ) ) { System . out . print ( " Denominator ▁ is ▁ not ▁ prime , ▁ " + " thus ▁ Midy ' s ▁ theorem ▁ is ▁ not ▁ " + " applicable " ) ; } else if ( l % 2 == 0 ) { for ( int i = 0 ; i < l / 2 ; i ++ ) { part1 = part1 * 10 + ( str . charAt ( i ) - '0' ) ; part2 = part2 * 10 + ( str . charAt ( l / 2 + i ) - '0' ) ; } System . out . println ( part1 + " ▁ + ▁ " + part2 + " ▁ = ▁ " + ( part1 + part2 ) ) ; System . out . print ( " Midy ' s ▁ theorem ▁ holds ! " ) ; } else { System . out . print ( " The ▁ repeating ▁ decimal ▁ is ▁ " + " of ▁ odd ▁ length ▁ thus ▁ Midy ' s ▁ " + " theorem ▁ is ▁ not ▁ applicable " ) ; } } public static void main ( String [ ] args ) { int numr = 2 , denr = 11 ; String res = fractionToDecimal ( numr , denr ) ; if ( res == " - 1" ) System . out . print ( " The ▁ fraction ▁ does ▁ not ▁ " + " have ▁ repeating ▁ decimal " ) ; else { System . out . println ( " Repeating ▁ decimal ▁ = ▁ " + res ) ; Midys ( res , denr ) ; } } }
import java . io . * ; class GFG { static long evenPowerSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int j = 2 * i ; sum = sum + ( j * j * j * j ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( evenPowerSum ( n ) ) ; } }
import java . io . * ; class GFG { static long evenPowerSum ( int n ) { return ( 8 * n * ( n + 1 ) * ( 2 * n + 1 ) * ( 3 * n * n + 3 * n - 1 ) ) / 15 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( evenPowerSum ( n ) ) ; } }
import java . util . * ; public class GFG { static int MAX = 501 ; public static int balancedprime ( int n ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int k = 0 ; k < MAX + 1 ; k ++ ) prime [ k ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } Vector < Integer > v = new Vector < Integer > ( ) ; for ( int p = 3 ; p <= MAX ; p += 2 ) if ( prime [ p ] ) v . add ( p ) ; int count = 0 ; for ( int i = 1 ; i < v . size ( ) ; i ++ ) { if ( ( int ) v . get ( i ) == ( ( int ) v . get ( i + 1 ) + ( int ) v . get ( i - 1 ) ) / 2 ) count ++ ; if ( count == n ) return ( int ) v . get ( i ) ; } return 1 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( balancedprime ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { private static final int MAX = 1000001 ; private static final int [ ] factor = new int [ MAX ] ; public static void generatePrimeFactors ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } public static int calculateNoOFactors ( int n ) { if ( n == 1 ) return 1 ; int ans = 1 ; int dup = factor [ n ] ; int c = 1 ; int j = n / factor [ n ] ; while ( j != 1 ) { if ( factor [ j ] == dup ) c += 1 ; else { dup = factor [ j ] ; ans = ans * ( c + 1 ) ; c = 1 ; } j = j / factor [ j ] ; } ans = ans * ( c + 1 ) ; return ans ; } public static int smallest ( int n ) { for ( int i = 1 ; ; i ++ ) if ( calculateNoOFactors ( i ) >= n ) return i ; } public static void main ( String args [ ] ) { generatePrimeFactors ( ) ; int n = 4 ; System . out . println ( smallest ( n ) ) ; } }
import java . io . * ; class GFG { static int squaresum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } public static void main ( String args [ ] ) throws IOException { int n = 4 ; System . out . println ( squaresum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int firstDigit ( int n ) { while ( n >= 10 ) n /= 10 ; return n ; } public static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void main ( String argc [ ] ) { int n = 98562 ; System . out . println ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }
import java . math . * ; class GFG { static int firstDigit ( int n ) { int digits = ( int ) ( Math . log10 ( n ) ) ; n = ( int ) ( n / ( int ) ( Math . pow ( 10 , digits ) ) ) ; return n ; } static int lastDigit ( int n ) { return ( n % 10 ) ; } public static void main ( String args [ ] ) { int n = 98562 ; System . out . println ( firstDigit ( n ) + " ▁ " + lastDigit ( n ) ) ; } }
import java . util . * ; class GFG { public static boolean isPrime ( int x ) { if ( x == 0 x == 1 ) return false ; for ( int i = 2 ; i * i <= x ; ++ i ) if ( x % i == 0 ) return false ; return true ; } public static void findPrimes ( int n ) { System . out . print ( n ) ; System . out . print ( 2 + " ▁ " + ( n - 2 ) ) ; { System . out . print ( 3 + " ▁ " ) ; n = n - 3 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i ) && isPrime ( n - i ) ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; break ; } } } } public static void main ( String [ ] args ) { int n = 27 ; findPrimes ( n ) ; } }
class GFG { static long c [ ] = new long [ 100 ] ; static void coef ( int n ) { c [ 0 ] = 1 ; for ( int i = 0 ; i < n ; c [ 0 ] = - c [ 0 ] , i ++ ) { c [ 1 + i ] = 1 ; for ( int j = i ; j > 0 ; j -- ) c [ j ] = c [ j - 1 ] - c [ j ] ; } } static boolean isPrime ( int n ) { coef ( n ) ; c [ 0 ] ++ ; c [ n ] -- ; int i = n ; while ( ( i -- ) > 0 && c [ i ] % n == 0 ) ; return i < 0 ; } public static void main ( String [ ] args ) { int n = 37 ; if ( isPrime ( n ) ) System . out . println ( " Prime " ) ; else System . out . println ( " Not ▁ Prime " ) ; } }
import java . util . * ; class Digits { public static int motzkin ( int n ) { if ( n == 0 n == 1 ) return 1 ; return ( ( 2 * n + 1 ) * motzkin ( n - 1 ) + ( 3 * n - 3 ) * motzkin ( n - 2 ) ) / ( n + 2 ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . print ( motzkin ( n ) ) ; } }
import java . io . * ; class GFG { static double sumOfSeries ( int n ) { return ( 0.666 ) * ( 1 - 1 / Math . pow ( 10 , n ) ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sumOfSeries ( n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { public static int summation ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( i * i ) ; return sum ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( summation ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GFG { private static final int MAX = 0 ; public static void leyland ( int n ) { List < Integer > ans = new ArrayList < Integer > ( ) ; for ( int x = 2 ; x <= n ; x ++ ) { for ( int y = 2 ; y <= x ; y ++ ) { int temp = ( int ) Math . pow ( x , y ) + ( int ) Math . pow ( y , x ) ; ans . add ( temp ) ; } } Collections . sort ( ans ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 6 ; leyland ( n ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }
import java . util . * ; class GFG { public static int nthTerm ( int n ) { return ( n * n ) + ( n * n * n ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( nthTerm ( n ) ) ; } }
import java . io . * ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( seriesSum ( n ) ) ; } }
class GFG { static double cal_speed ( double dist , double time ) { System . out . print ( " Distance ( km ) : " + dist) ; System . out . print ( " Time ( hr ) : " + time) ; return dist / time ; } static double cal_dis ( double speed , double time ) { System . out . print ( " Time ( hr ) : " + time) ; System . out . print ( " Speed ( km / hr ) : " + speed) ; return speed * time ; } static double cal_time ( double dist , double speed ) { System . out . print ( " Distance ( km ) : "+ dist) ; System . out . print ( " Speed ( km / hr ) : " + speed) ; return speed * dist ; } public static void main ( String [ ] args ) { System . out . println ( " The calculated Speed ( km / hr ) is : "+ cal_speed ( 45.9 , 2.0 ) ) ; System . out . println ( " The calculated Distance ( km ) : "+ cal_dis ( 62.9 , 2.5 ) ) ; System . out . println ( " The calculated Time ( hr ) : "+ cal_time ( 48.0 , 4.5 ) ) ; } }
import java . io . * ; class GFG { static float avgOfFirstN ( int n ) { return ( float ) ( 1 + n ) / 2 ; } public static void main ( String args [ ] ) { int n = 20 ; System . out . println ( avgOfFirstN ( n ) ) ; } }
import java . io . * ; class GFG { static int summation ( int n ) { int sum ; sum = ( int ) ( Math . pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( summation ( n ) ) ; } }
import java . io . * ; class Series { static int term ( int n ) { int x = ( ( ( 1 ) + ( int ) Math . sqrt ( 1 + ( 8 * n ) ) ) / 2 ) ; return x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( term ( n ) ) ; } }
import java . util . Scanner ; class Deserium { static int countDigits ( int n ) { int c = 0 ; do { c ++ ; n = n / 10 ; } while ( n != 0 ) ; return c ; } static boolean isDeserium ( int x ) { int temp = x ; int p = countDigits ( x ) ; int sum = 0 ; while ( x != 0 ) { int digit = x % 10 ; sum += Math . pow ( digit , p ) ; p -- ; x = x / 10 ; } return ( sum == temp ) ; } public static void main ( String [ ] args ) { int x = 135 ; if ( isDeserium ( x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static float mean ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; float mean = ( float ) sum / n ; return mean ; } static float sd ( int arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( arr [ i ] - mean ( arr , n ) ) * ( arr [ i ] - mean ( arr , n ) ) ; float sdd = sum / n ; return sdd ; } static float combinedVariance ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { float mean1 = mean ( arr1 , n ) ; float mean2 = mean ( arr2 , m ) ; System . out . print ( " Mean1 : ▁ " + mean1 + " ▁ " ) ; System . out . println ( " Mean2 : ▁ " + mean2 ) ; float sd1 = sd ( arr1 , n ) ; float sd2 = sd ( arr2 , m ) ; System . out . print ( " StandardDeviation1 : ▁ " + sd1 + " ▁ " ) ; System . out . println ( " StandardDeviation2 : ▁ " + sd2 + " ▁ " ) ; float combinedMean = ( float ) ( n * mean1 + m * mean2 ) / ( n + m ) ; System . out . println ( " Combined ▁ Mean : ▁ " + combinedMean + " ▁ " ) ; float d1_square = ( mean1 - combinedMean ) * ( mean1 - combinedMean ) ; float d2_square = ( mean2 - combinedMean ) * ( mean2 - combinedMean ) ; System . out . print ( " d1 ▁ square : ▁ " + d1_square + " ▁ " ) ; System . out . println ( " d2 _ square : ▁ " + d2_square ) ; float combinedVar = ( n * ( sd1 + d1_square ) + m * ( sd2 + d2_square ) ) / ( n + m ) ; return combinedVar ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 23 , 45 , 34 , 78 , 12 , 76 , 34 } ; int arr2 [ ] = { 65 , 67 , 34 , 23 , 45 } ; int n = arr1 . length ; int m = arr2 . length ; System . out . println ( " Combined ▁ Variance : ▁ " + combinedVariance ( arr1 , arr2 , n , m ) ) ; } }
class GFG { static boolean checkDivisibility ( String num ) { int length = num . length ( ) ; if ( length == 1 && num . charAt ( 0 ) == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num . charAt ( i -- ) - '0' ; group += ( num . charAt ( i -- ) - '0' ) * 10 ; group += ( num . charAt ( i ) - '0' ) * 100 ; sum = sum + group * p ; p *= ( - 1 ) ; } sum = Math . abs ( sum ) ; return ( sum % 13 == 0 ) ; } public static void main ( String [ ] args ) { String number = "83959092724" ; if ( checkDivisibility ( number ) ) System . out . println ( number + " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else System . out . println ( number + " ▁ is ▁ not ▁ divisible ▁ by ▁ 13 . " ) ; } }
import java . io . * ; class GFG { static void modularEquation ( int a , int b ) { if ( a < b ) { System . out . println ( " No ▁ solution ▁ possible ▁ " ) ; return ; } if ( a == b ) { System . out . println ( " Infinite ▁ Solution ▁ possible ▁ " ) ; return ; } int count = 0 ; int n = a - b ; int y = ( int ) Math . sqrt ( a - b ) ; for ( int i = 1 ; i <= y ; ++ i ) { if ( n % i == 0 ) { if ( n / i > b ) count ++ ; if ( i > b ) count ++ ; } } if ( y * y == n && y > b ) count -- ; System . out . println ( count ) ; } public static void main ( String [ ] args ) { int a = 21 , b = 5 ; modularEquation ( a , b ) ; } }
class GFG { static int minOp ( int num ) { int rem ; int count = 0 ; while ( num > 0 ) { rem = num % 10 ; if ( ! ( rem == 3 rem == 8 ) ) count ++ ; num /= 10 ; } return count ; } public static void main ( String [ ] args ) { int num = 234198 ; System . out . print ( " Minimum ▁ Operations ▁ = " + minOp ( num ) ) ; } }
import java . io . * ; public class GFG { static int countWays ( int num ) { int [ ] dp = new int [ num + 1 ] ; int MOD = ( int ) 1E9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( countWays ( n ) ) ; } }
import java . util . * ; class GFG { static boolean sumSquare ( int n ) { HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . put ( i * i , 1 ) ; if ( s . containsKey ( n - i * i ) ) { System . out . println ( ( int ) Math . sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 169 ; System . out . print ( sumSquare ( n ) ? "YES " ▁ : ▁ " NO "); } }
import java . util . * ; import java . lang . * ; class GFG { public static boolean judgeSquareSum ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { int count = 0 ; if ( n % i == 0 ) { while ( n % i == 0 ) { count ++ ; n /= i ; } if ( i % 4 == 3 && count % 2 != 0 ) return false ; } } return n % 4 != 3 ; } public static void main ( String argc [ ] ) { int n = 17 ; if ( judgeSquareSum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int countDigitOne ( int n ) { int countr = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) { int divider = i * 10 ; countr += ( n / divider ) * i + Math . min ( Math . max ( n % divider - i + 1 , 0 ) , i ) ; } return countr ; } public static void main ( String [ ] args ) { int n = 13 ; System . out . println ( countDigitOne ( n ) ) ; n = 113 ; System . out . println ( countDigitOne ( n ) ) ; n = 205 ; System . out . println ( countDigitOne ( n ) ) ; } }
class GFG { public static boolean isPrime ( char c ) { return ( c == '2' c == '3' c == '5' c == '7' ) ; } public static void decrease ( StringBuilder s , int i ) { if ( s . charAt ( i ) <= '2' ) { s . deleteCharAt ( i ) ; s . setCharAt ( i , '7' ) ; } else if ( s . charAt ( i ) == '3' ) s . setCharAt ( i , '2' ) ; else if ( s . charAt ( i ) <= '5' ) s . setCharAt ( i , '3' ) ; else if ( s . charAt ( i ) <= '7' ) s . setCharAt ( i , '5' ) ; else s . setCharAt ( i , '7' ) ; return ; } public static String primeDigits ( StringBuilder s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! isPrime ( s . charAt ( i ) ) ) { while ( i >= 0 && s . charAt ( i ) <= '2' ) i -- ; if ( i < 0 ) { i = 0 ; decrease ( s , i ) ; } else decrease ( s , i ) ; for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) s . setCharAt ( j , '7' ) ; break ; } } return s . toString ( ) ; } public static void main ( String [ ] args ) { StringBuilder s = new StringBuilder ( "45" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "1000" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "7721" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "7221" ) ; System . out . println ( primeDigits ( s ) ) ; s = new StringBuilder ( "74545678912345689748593275897894708927680" ) ; System . out . println ( primeDigits ( s ) ) ; } }
import java . util . * ; class GFG { static int factors ( int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . containsKey ( i ) ) { prime . put ( i , prime . get ( i ) + 1 ) ; } else { prime . put ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . containsKey ( n ) ) { prime . put ( n , prime . get ( n ) + 1 ) ; } else { prime . put ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; for ( Map . Entry < Integer , Integer > it : prime . entrySet ( ) ) { ans1 *= 2 * it . getValue ( ) + 1 ; ans2 *= it . getValue ( ) + 1 ; } return ans1 - ans2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( factors ( n ) ) ; n = 8 ; System . out . println ( factors ( n ) ) ; } }
import java . util . * ; class solution { static void greatest ( String s ) { int n = s . length ( ) ; int [ ] a = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s . charAt ( i ) - '0' ; sum += a [ i ] ; } { if ( a [ n - 2 ] % 2 != 0 || ( sum - a [ n - 1 ] ) % 3 != 0 ) { System . out . println ( " - 1" ) ; } else { System . out . println ( n ) ; } } else { int re = sum % 3 ; int del = - 1 ; int flag = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( ( a [ i ] ) % 3 == re ) { if ( a [ i + 1 ] > a [ i ] ) { del = i ; flag = 1 ; break ; } else { del = i ; } } } if ( flag == 0 ) { if ( a [ n - 2 ] % 2 == 0 && re == a [ n - 1 ] % 3 ) del = n - 1 ; } if ( del == - 1 ) System . out . println ( - 1 ) ; else { System . out . println ( del + 1 ) ; } } } public static void main ( String args [ ] ) { String s = "7510222" ; greatest ( s ) ; } }
class GFG { static boolean asPowerSum ( int w , int m ) { while ( m > 0 ) { if ( ( m - 1 ) % w == 0 ) m = ( m - 1 ) / w ; else if ( ( m + 1 ) % w == 0 ) m = ( m + 1 ) / w ; else if ( m % w == 0 ) m = m / w ; else } return ( m == 0 ) ; } public static void main ( String [ ] args ) { int w = 3 , m = 7 ; if ( asPowerSum ( w , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void main ( String [ ] args ) { String num = "1234" ; System . out . println ( divisible ( num ) ) ; } }
import java . io . * ; class GFG { static int n = 3 ; static int dotProduct ( int vect_A [ ] , int vect_B [ ] ) { int product = 0 ; for ( int i = 0 ; i < n ; i ++ ) product = product + vect_A [ i ] * vect_B [ i ] ; return product ; } static void crossProduct ( int vect_A [ ] , int vect_B [ ] , int cross_P [ ] ) { cross_P [ 0 ] = vect_A [ 1 ] * vect_B [ 2 ] - vect_A [ 2 ] * vect_B [ 1 ] ; cross_P [ 1 ] = vect_A [ 2 ] * vect_B [ 0 ] - vect_A [ 0 ] * vect_B [ 2 ] ; cross_P [ 2 ] = vect_A [ 0 ] * vect_B [ 1 ] - vect_A [ 1 ] * vect_B [ 0 ] ; } public static void main ( String [ ] args ) { int vect_A [ ] = { 3 , - 5 , 4 } ; int vect_B [ ] = { 2 , 6 , 5 } ; int cross_P [ ] = new int [ n ] ; System . out . print ( " Dot ▁ product : " ) ; System . out . println ( dotProduct ( vect_A , vect_B ) ) ; System . out . print ( " Cross ▁ product : " ) ; crossProduct ( vect_A , vect_B , cross_P ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( cross_P [ i ] + " ▁ " ) ; } }
import java . io . * ; public class GFG { static int totalNumber ( int n ) { return 8 * ( int ) Math . pow ( 9 , n - 1 ) ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( totalNumber ( n ) ) ; } }
class GFG { static int MOD = 1000000007 ; static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( 1 * res * x ) % p ; x = ( 1 * x * x ) % p ; } return res ; } static int countEvenWays ( int n ) { return power ( 2 , n / 2 - 1 , MOD ) ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( countEvenWays ( n ) ) ; n = 8 ; System . out . println ( countEvenWays ( n ) ) ; } }
class GFG { static final int MAX = 1000001 ; static int factor [ ] = new int [ MAX ] ; static void cal_factor ( ) { factor [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) factor [ i ] = i ; for ( int i = 4 ; i < MAX ; i += 2 ) factor [ i ] = 2 ; for ( int i = 3 ; i * i < MAX ; i ++ ) { if ( factor [ i ] == i ) { for ( int j = i * i ; j < MAX ; j += i ) { if ( factor [ j ] == j ) factor [ j ] = i ; } } } } static int no_of_representations ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int temp = a [ i ] ; int flag = 0 ; while ( factor [ temp ] != 1 ) { flag = - 1 ; count ++ ; temp = temp / factor [ temp ] ; } count += flag ; } return count ; } public static void main ( String [ ] args ) { cal_factor ( ) ; int a [ ] = { 4 , 4 , 4 } ; int n = a . length ; System . out . print ( no_of_representations ( a , n ) ) ; } }
import java . io . * ; class GFG { static int coutSubSeq ( int A [ ] , int N , int M ) { int sum = 0 ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { for ( int k = j + 1 ; k < N ; k ++ ) { sum = A [ i ] + A [ j ] + A [ k ] ; if ( sum % M == 0 ) ans ++ ; } } } return ans ; } public static void main ( String args [ ] ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = A . length ; System . out . println ( coutSubSeq ( A , N , M ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int countSubSeq ( int A [ ] , int N , int M ) { int ans = 0 ; int h [ ] = new int [ M ] ; Arrays . fill ( h , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { A [ i ] = A [ i ] % M ; h [ A [ i ] ] ++ ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = i ; j < M ; j ++ ) { int rem = ( M - ( i + j ) % M ) % M ; if ( rem < j ) continue ; if ( i == j && rem == j ) ans += h [ i ] * ( h [ i ] - 1 ) * ( h [ i ] - 2 ) / 6 ; else if ( i == j ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ rem ] / 2 ; else if ( i == rem ) ans += h [ i ] * ( h [ i ] - 1 ) * h [ j ] / 2 ; else if ( rem == j ) ans += h [ j ] * ( h [ j ] - 1 ) * h [ i ] / 2 ; else ans = ans + h [ i ] * h [ j ] * h [ rem ] ; } } return ans ; } public static void main ( String args [ ] ) { int M = 3 ; int A [ ] = { 1 , 2 , 4 , 3 } ; int N = A . length ; System . out . println ( countSubSeq ( A , N , M ) ) ; } }
import java . lang . * ; class GFG { static int findTerm ( int n ) { if ( n == 1 ) return n ; else { int term = 7 ; for ( int i = 2 ; i <= n ; i ++ ) term = term * 2 + ( i - 1 ) ; return term ; } } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( findTerm ( n ) ) ; } }
import java . io . * ; class GFG { static int findNumber ( int n ) { n -- ; int i = 1 ; while ( n >= 0 ) { n -= i ; ++ i ; } return ( n + i ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( findNumber ( n ) ) ; } }
import java . math . * ; class GFG { static float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) { int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; int squareSum_X = 0 , squareSum_Y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum_X + X [ i ] * X [ i ] ; squareSum_Y = squareSum_Y + Y [ i ] * Y [ i ] ; } float corr = ( float ) ( n * sum_XY - sum_X * sum_Y ) / ( float ) ( Math . sqrt ( ( n * squareSum_X - sum_X * sum_X ) * ( n * squareSum_Y - sum_Y * sum_Y ) ) ) ; return corr ; } public static void main ( String args [ ] ) { int X [ ] = { 15 , 18 , 21 , 24 , 27 } ; int Y [ ] = { 25 , 25 , 27 , 31 , 32 } ; int n = X . length ; System . out . printf ( " % 6f " , correlationCoefficient ( X , Y , n ) ) ; } }
class GFG { static void result ( long n , long k , long t ) { if ( t <= k ) System . out . println ( t ) ; else if ( t <= n ) System . out . println ( k ) ; else { long temp = t - n ; temp = k - temp ; System . out . println ( temp ) ; } } public static void main ( String args [ ] ) { long n , k , t ; n = 10 ; k = 5 ; t = 12 ; result ( n , k , t ) ; } }
class GFG { static float weightedMean ( int X [ ] , int W [ ] , int n ) { int sum = 0 , numWeight = 0 ; for ( int i = 0 ; i < n ; i ++ ) { numWeight = numWeight + X [ i ] * W [ i ] ; sum = sum + W [ i ] ; } return ( float ) ( numWeight ) / sum ; } public static void main ( String args [ ] ) { int X [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int W [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = X . length ; int m = W . length ; if ( n == m ) System . out . println ( weightedMean ( X , W , n ) ) ; else System . out . println ( " - 1" ) ; } }
import java . io . * ; class GFG { static int sumOfDigits ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a /= 10 ; } return sum ; } static int findMax ( int x ) { int b = 1 , ans = x ; while ( x != 0 ) { int cur = ( x - 1 ) * b + ( b - 1 ) ; if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) || ( sumOfDigits ( cur ) == sumOfDigits ( ans ) && cur > ans ) ) ans = cur ; x /= 10 ; b *= 10 ; } return ans ; } public static void main ( String [ ] args ) { int n = 521 ; System . out . println ( findMax ( n ) ) ; } }
import java . io . * ; class GFG { static float harmonicMean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) 1 / arr [ i ] ; return ( float ) n / sum ; } public static void main ( String args [ ] ) { float arr [ ] = { 13.5f , 14.5f , 14.8f , 15.2f , 16.1f } ; int n = arr . length ; System . out . println ( harmonicMean ( arr , n ) ) ; } }
class GFG { static float harmonicMean ( int arr [ ] , int freq [ ] , int n ) { float sum = 0 , frequency_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( float ) freq [ i ] / arr [ i ] ; frequency_sum = frequency_sum + freq [ i ] ; } return ( frequency_sum / sum ) ; } public static void main ( String args [ ] ) { int num [ ] = { 13 , 14 , 15 , 16 , 17 } ; int freq [ ] = { 2 , 5 , 13 , 7 , 3 } ; int n = num . length ; System . out . println ( harmonicMean ( num , freq , n ) ) ; } }
class GFG { static int calculate_sum ( int a , int N ) { int m = N / a ; int sum = m * ( m + 1 ) / 2 ; int ans = a * sum ; return ans ; } public static void main ( String [ ] args ) { int a = 7 , N = 49 ; System . out . println ( " Sum ▁ of ▁ multiples ▁ of ▁ " + a + " ▁ up ▁ to ▁ " + N + " ▁ = ▁ " + calculate_sum ( a , N ) ) ; } }
import java . io . * ; class GFG { static void point ( int a , int b , int c , int d , int n ) { int x , flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { x = b + i * a ; if ( ( x - d ) % c == 0 && x - d >= 0 ) { System . out . println ( x ) ; flag = 1 ; break ; } } if ( flag == 0 ) { System . out . println ( " No ▁ collision ▁ point " ) ; } } public static void main ( String [ ] args ) { int a = 20 ; int b = 2 ; int c = 9 ; int d = 19 ; int n = 20 ; point ( a , b , c , d , n ) ; } }
import java . io . * ; import java . math . * ; class GFG { static void findArmstrong ( int low , int high ) { for ( int i = low + 1 ; i < high ; ++ i ) { int x = i ; int n = 0 ; while ( x != 0 ) { x /= 10 ; ++ n ; } int pow_sum = 0 ; x = i ; while ( x != 0 ) { int digit = x % 10 ; pow_sum += Math . pow ( digit , n ) ; x /= 10 ; } if ( pow_sum == i ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String args [ ] ) { int num1 = 100 ; int num2 = 400 ; findArmstrong ( num1 , num2 ) ; System . out . println ( ) ; } }
class GfG { static long ispowerof2 ( long num ) { if ( ( num & ( num - 1 ) ) == 0 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { long num = 549755813888L ; System . out . println ( ispowerof2 ( num ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int median ( int a [ ] , int l , int r ) { int n = r - l + 1 ; n = ( n + 1 ) / 2 - 1 ; return n + l ; } static int IQR ( int [ ] a , int n ) { Arrays . sort ( a ) ; int mid_index = median ( a , 0 , n ) ; int Q1 = a [ median ( a , 0 , mid_index ) ] ; int Q3 = a [ mid_index + median ( a , mid_index + 1 , n ) ] ; return ( Q3 - Q1 ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 19 , 7 , 6 , 5 , 9 , 12 , 27 , 18 , 2 , 15 } ; int n = a . length ; System . out . println ( IQR ( a , n ) ) ; } }
import java . util . * ; class GFG { static void printPierpont ( int n ) { boolean [ ] arr = new boolean [ n + 1 ] ; int two = 1 , three = 1 ; while ( two + 1 < n ) { arr [ two ] = true ; while ( two * three + 1 < n ) { arr [ three ] = true ; arr [ two * three ] = true ; three *= 3 ; } three = 1 ; two *= 2 ; } ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] ) v . add ( i + 1 ) ; arr = new boolean [ n + 1 ] ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( arr [ p ] == false ) for ( int i = p * 2 ; i < n ; i += p ) arr [ i ] = true ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) if ( ! arr [ v . get ( i ) ] ) System . out . print ( v . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 200 ; printPierpont ( n ) ; } }
class GFG { static boolean isWoodall ( int x ) { if ( x % 2 == 0 ) return false ; if ( x == 1 ) return true ; int p = 0 ; while ( x % 2 == 0 ) { x = x / 2 ; p ++ ; if ( p == x ) return true ; } return false ; } public static void main ( String args [ ] ) { int x = 383 ; if ( isWoodall ( x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static void print_result ( int a [ ] , int n , int k , int m ) { Vector < Vector < Integer > > v = new Vector < Vector < Integer > > ( m ) ; for ( int i = 0 ; i < m ; i ++ ) v . add ( new Vector < Integer > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = a [ i ] % m ; v . get ( rem ) . add ( a [ i ] ) ; if ( v . get ( rem ) . size ( ) == k ) { for ( int j = 0 ; j < k ; j ++ ) System . out . print ( v . get ( rem ) . get ( j ) + " ▁ " ) ; return ; } } System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 8 , 4 } ; int n = a . length ; print_result ( a , n , 2 , 3 ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int smallestX ( int n ) { int [ ] temp = new int [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) temp [ i ] = 0 ; if ( n == 0 ) return - 1 ; int count = 0 , x = 0 ; for ( x = 1 ; count < 10 ; x ++ ) { int y = x * n ; while ( y > 0 ) { if ( temp [ y % 10 ] == 0 ) { count ++ ; temp [ y % 10 ] = 1 ; } y /= 10 ; } } return x - 1 ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . print ( smallestX ( n ) ) ; } }
class GFG { static int digSum ( int n ) { int sum = 0 , rem = 0 ; while ( n > 0 ) { rem = n % 10 ; sum += rem ; n /= 10 ; } return sum ; } static int findX ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i + digSum ( i ) == n ) return i ; return - 1 ; } public static void main ( String [ ] args ) { int n = 43 ; System . out . println ( " x ▁ = ▁ " + findX ( n ) ) ; } }
class GFG { static void complement ( String number1 ) { char [ ] number = number1 . toCharArray ( ) ; for ( int i = 0 ; i < number . length ; i ++ ) if ( number [ i ] != ' . ' ) number [ i ] = ( char ) ( ( int ) ( '9' ) - ( int ) ( number [ i ] ) + ( int ) ( '0' ) ) ; System . out . println ( "9 ' s ▁ complement ▁ is ▁ : ▁ " + String . valueOf ( number ) ) ; } public static void main ( String [ ] args ) { String number = "345.45" ; complement ( number ) ; } }
public class Main { static int countWays ( int n ) { int count = 0 ; for ( int i = 1 ; i * i < n ; i ++ ) if ( n % i == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( countWays ( n ) ) ; } }
class GFG { static boolean isDigitPresent ( int m , boolean hash [ ] ) { while ( m > 0 ) { if ( hash [ m % 10 ] ) return true ; m = m / 10 ; } return false ; } static int countDivisibles ( int n ) { boolean hash [ ] = new boolean [ 10 ] ; int m = n ; while ( m > 0 ) { hash [ m % 10 ] = true ; m = m / 10 ; } int ans = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( countDivisibles ( n ) ) ; } }
class GFG { static int MAX = 100 ; static String s = " " ; static void luDecomposition ( int [ ] [ ] mat , int n ) { int [ ] [ ] lower = new int [ n ] [ n ] ; int [ ] [ ] upper = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = i ; k < n ; k ++ ) { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ i ] [ j ] * upper [ j ] [ k ] ) ; upper [ i ] [ k ] = mat [ i ] [ k ] - sum ; } for ( int k = i ; k < n ; k ++ ) { if ( i == k ) else { int sum = 0 ; for ( int j = 0 ; j < i ; j ++ ) sum += ( lower [ k ] [ j ] * upper [ j ] [ i ] ) ; lower [ k ] [ i ] = ( mat [ k ] [ i ] - sum ) / upper [ i ] [ i ] ; } } } System . out . println ( setw ( 2 ) + " TABSYMBOL Lower ▁ Triangular " + setw ( 10 ) + " Upper ▁ Triangular " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( setw ( 4 ) + lower [ i ] [ j ] + " TABSYMBOL " ) ; System . out . print ( " TABSYMBOL " ) ; for ( int j = 0 ; j < n ; j ++ ) System . out . print ( setw ( 4 ) + upper [ i ] [ j ] + " TABSYMBOL " ) ; System . out . print ( "NEW_LINE"); } } static String setw ( int noOfSpace ) { s = " " ; for ( int i = 0 ; i < noOfSpace ; i ++ ) s += " ▁ " ; return s ; } public static void main ( String arr [ ] ) { int mat [ ] [ ] = { { 2 , - 1 , - 2 } , { - 4 , 6 , 3 } , { - 4 , - 2 , 8 } } ; luDecomposition ( mat , 3 ) ; } }
public class DivisibleParts { static void printTwoDivisibleParts ( String num , int f , int s ) { int N = num . length ( ) ; int [ ] prefixReminder = new int [ N + 1 ] ; int [ ] suffixReminder = new int [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num . charAt ( i - 1 ) - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num . charAt ( i ) - '0' ) * base ) % s ; base = ( base * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num . charAt ( i ) != '0' ) { System . out . println ( num . substring ( 0 , i ) + " ▁ " + num . substring ( i ) ) ; return ; } } System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { String num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int MAX_DIGITS = 20 ; static int isOctal ( int n ) { while ( n > 0 ) { if ( ( n % 10 ) >= 8 ) return 0 ; else n = n / 10 ; } return 1 ; } static int isPalindrome ( int n ) { int divide = ( isOctal ( n ) == 0 ) ? 8 : 10 ; int octal [ ] = new int [ MAX_DIGITS ] ; int i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = n / divide ; } for ( int j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int n = 97 ; if ( isPalindrome ( n ) > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { System . out . print ( fact + " ▁ " ) ; fact = fact * x ; x ++ ; } } public static void main ( String [ ] args ) { int n = 100 ; printFactorialNums ( n ) ; } }
import java . util . * ; class GFG { static int sumDigitSquare ( int n ) { int sq = 0 ; while ( n > 0 ) { int digit = n % 10 ; sq += digit * digit ; n = n / 10 ; } return sq ; } static boolean isHappy ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( n ) ; while ( true ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( ( s . contains ( n ) && n != ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) ) return false ; s . add ( n ) ; } } public static void main ( String [ ] args ) { int n = 23 ; if ( isHappy ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
public class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isThreeDisctFactors ( long n ) { int sq = ( int ) Math . sqrt ( n ) ; if ( 1L * sq * sq != n ) return false ; return isPrime ( sq ) ? true : false ; } public static void main ( String [ ] args ) { long num = 9 ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; num = 15 ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; num = 12397923568441L ; if ( isThreeDisctFactors ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int computeLastDigit ( long A , long B ) { int variable = 1 ; return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long i = A + 1 ; i <= B ; i ++ ) variable = ( int ) ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } } public static void main ( String [ ] args ) { System . out . println ( computeLastDigit ( 2632 , 2634 ) ) ; } }
class GFG { static float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } public static void main ( String args [ ] ) { int n = 20 ; float a = 2.5f , d = 1.5f ; System . out . println ( sumOfAP ( a , d , n ) ) ; } }
class GFG { public static final long M = 1000000007 ; static long multiplyFactors ( int n ) { long prod = 1 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) prod = ( prod * i ) % M ; else { prod = ( prod * i ) % M ; prod = ( prod * n / i ) % M ; } } } return prod ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( multiplyFactors ( n ) ) ; } }
class GFG { public static final long M = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % M ; y = ( y >> 1 ) % M ; x = ( x * x ) % M ; } return res ; } static int countFactors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) count ++ ; else count += 2 ; } } return count ; } static long multiplyFactors ( int n ) { int numFactor = countFactors ( n ) ; long product = power ( n , numFactor / 2 ) ; if ( numFactor % 2 == 1 ) product = ( product * ( int ) Math . sqrt ( n ) ) % M ; return product ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( multiplyFactors ( n ) ) ; } }
import java . util . * ; class GFG { static boolean isDivisibleBy10 ( String bin ) { int n = bin . length ( ) ; if ( bin . charAt ( n - 1 ) == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin . charAt ( i ) == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String bin = "11000111001110" ; if ( isDivisibleBy10 ( bin ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; class GFG { static void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; System . out . print ( first + " ▁ " ) ; if ( n > 1 ) System . out . print ( second + " ▁ " ) ; if ( n > 2 ) System . out . print ( second + " ▁ " ) ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; System . out . print ( curr + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 10 ; printTrib ( n ) ; } }
class GFG { static int counDivisors ( int X ) { int count = 0 ; for ( int i = 1 ; i <= X ; ++ i ) { if ( X % i == 0 ) { count ++ ; } } return count ; } static int countDivisorsMult ( int arr [ ] , int n ) { int mul = 1 ; for ( int i = 0 ; i < n ; ++ i ) mul *= arr [ i ] ; return counDivisors ( mul ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; System . out . println ( countDivisorsMult ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void SieveOfEratosthenes ( int largest , ArrayList < Integer > prime ) { boolean [ ] isPrime = new boolean [ largest + 1 ] ; Arrays . fill ( isPrime , true ) ; for ( int p = 2 ; p * p <= largest ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= largest ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p <= largest ; p ++ ) if ( isPrime [ p ] ) prime . add ( p ) ; } static long countDivisorsMult ( int [ ] arr , int n ) { int largest = 0 ; for ( int a : arr ) { largest = Math . max ( largest , a ) ; } ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; SieveOfEratosthenes ( largest , prime ) ; Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < prime . size ( ) ; j ++ ) { while ( arr [ i ] > 1 && arr [ i ] % prime . get ( j ) == 0 ) { arr [ i ] /= prime . get ( j ) ; if ( mp . containsKey ( prime . get ( j ) ) ) { mp . put ( prime . get ( j ) , mp . get ( prime . get ( j ) ) + 1 ) ; } else { mp . put ( prime . get ( j ) , 1 ) ; } } } if ( arr [ i ] != 1 ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } long res = 1 ; for ( int it : mp . keySet ( ) ) res *= ( mp . get ( it ) + 1L ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; System . out . println ( countDivisorsMult ( arr , n ) ) ; } }
class GFG { static void additiveCongruentialMethod ( int Xo , int m , int c , int [ ] randomNums , int noOfRandomNums ) { randomNums [ 0 ] = Xo ; for ( int i = 1 ; i < noOfRandomNums ; i ++ ) { randomNums [ i ] = ( randomNums [ i - 1 ] + c ) % m ; } } public static void main ( String [ ] args ) { int Xo = 3 ; int m = 15 ; int c = 2 ; int noOfRandomNums = 20 ; int [ ] randomNums = new int [ noOfRandomNums ] ; additiveCongruentialMethod ( Xo , m , c , randomNums , noOfRandomNums ) ; for ( int i = 0 ; i < noOfRandomNums ; i ++ ) { System . out . print ( randomNums [ i ] + " ▁ " ) ; } } }
class GFG { static void findNthTerm ( int n ) { System . out . println ( n * ( 4 * n + 3 ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; findNthTerm ( N ) ; } }
class GFG { static int enneacontahexagonNum ( int n ) { return ( 94 * n * n - 92 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( enneacontahexagonNum ( n ) ) ; } }
class GFG { static int HexacontatetragonNum ( int n ) { return ( 62 * n * n - 60 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( HexacontatetragonNum ( n ) ) ; } }
class GFG { static int icosikaipentagonNum ( int N ) { return ( 23 * N * N - 21 * N ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( "3rd ▁ icosikaipentagon ▁ Number ▁ is ▁ " + icosikaipentagonNum ( n ) ) ; } }
class GFG { static int chiliagonNum ( int n ) { return ( 998 * n * n - 996 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ chiliagon ▁ Number ▁ is ▁ = ▁ " + chiliagonNum ( n ) ) ; } }
import java . util . * ; class GFG { static int pentacontagonNum ( int n ) { return ( 48 * n * n - 46 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ pentacontagon ▁ Number ▁ is ▁ = ▁ " + pentacontagonNum ( n ) ) ; } }
class GFG { static int countDigit ( double n ) { return ( ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ) ; } public static void main ( String [ ] args ) { double N = 80 ; System . out . println ( countDigit ( N ) ) ; } }
class GFG { static double sum ( int x , int n ) { double i , total = 1.0 , multi = x ; System . out . print ( "1 ▁ " ) ; for ( i = 1 ; i < n ; i ++ ) { total = total + multi ; System . out . print ( multi ) ; System . out . print ( " ▁ " ) ; multi = multi * x ; } System . out . println ( ) ; return total ; } public static void main ( String [ ] args ) { int x = 2 ; int n = 5 ; System . out . printf ( " % .2f " , sum ( x , n ) ) ; } }
class Main { public static void main ( String [ ] args ) { int N = 43 ; int ans = findRemainder ( N ) ; System . out . println ( ans ) ; } public static int findRemainder ( int n ) { int x = n & 3 ; return x ; } }
import java . util . * ; import java . lang . Math ; public class autobio { public static boolean isAutoBio ( int num ) { String autoStr ; int index , number , i , j , cnt ; autoStr = Integer . toString ( num ) ; for ( i = 0 ; i < autoStr . length ( ) ; i ++ ) { index = Integer . parseInt ( autoStr . charAt ( i ) + " " ) ; cnt = 0 ; for ( j = 0 ; j < autoStr . length ( ) ; j ++ ) { number = Integer . parseInt ( autoStr . charAt ( j ) + " " ) ; if ( number == i ) cnt ++ ; } if ( cnt != index ) return false ; } return true ; } public static void findAutoBios ( double n ) { double high , low ; int i , flag = 0 ; low = Math . pow ( 10.0 , n - 1 ) ; high = Math . pow ( 10.0 , n ) - 1.0 ; for ( i = ( int ) low ; i <= ( int ) high ; i ++ ) if ( isAutoBio ( i ) ) { flag = 1 ; System . out . print ( i + " , ▁ " ) ; } if ( flag == 0 ) System . out . println ( " There ▁ is ▁ no ▁ Autobiographical ▁ Number " + " with ▁ " + ( int ) n + " ▁ digits " ) ; } public static void main ( String [ ] args ) { double N = 0 ; findAutoBios ( N ) ; N = 4 ; findAutoBios ( N ) ; } }
import java . util . * ; class GFG { static void checkPalindrome ( int num ) { String str = Integer . toString ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) { System . out . print ( " No " ) ; return ; } l ++ ; r -- ; } System . out . print ( " Yes " ) ; return ; } public static void main ( String args [ ] ) { int n = 19 , k = 3 ; checkPalindrome ( n + k ) ; } }
import java . util . * ; class GFG { static ArrayList < Integer > sieve ( int n ) { ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime . add ( 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime . get ( p ) == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime . set ( i , 1 ) ; } p += 1 ; } ArrayList < Integer > allPrimes = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime . get ( i ) == 0 ) allPrimes . add ( i ) ; } return allPrimes ; } static ArrayList < Integer > distPrime ( ArrayList < Integer > arr , ArrayList < Integer > allPrimes ) { ArrayList < Integer > list1 = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( arr . get ( j ) % allPrimes . get ( i ) == 0 ) { list1 . add ( allPrimes . get ( i ) ) ; break ; } } } return list1 ; } public static void main ( String args [ ] ) { ArrayList < Integer > allPrimes = new ArrayList < Integer > ( sieve ( 10000 ) ) ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 15 ) ; arr . add ( 30 ) ; arr . add ( 60 ) ; ArrayList < Integer > ans = new ArrayList < Integer > ( distPrime ( arr , allPrimes ) ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; System . out . print ( " ] " ) ; } }
class GFG { static void find_composite_nos ( int n ) { System . out . println ( 9 * n + " ▁ " + 8 * n ) ; } public static void main ( String [ ] args ) { int n = 4 ; find_composite_nos ( n ) ; } }
class GFG { static int sum ( int m , int n ) { return 8 * m * n - 6 * m - 6 * n + 4 ; } public static void main ( String [ ] args ) { int m = 3 , n = 2 ; System . out . println ( sum ( m , n ) ) ; } }
class GFG { static double volumeRec ( double a , double b , double e , double h ) { return ( ( ( b * h ) / 6 ) * ( 2 * a + e ) ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { double a = 2 , b = 5 , e = 5 , h = 6 ; System . out . print ( " Volume ▁ = ▁ " + volumeRec ( a , b , e , h ) ) ; } }
class GFG { static int count_square ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i = i + 2 ) { int k = n - i + 1 ; count += ( k * k ) ; } return count ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . println ( count_square ( N ) ) ; } }
class GFG { static int countAnomalies ( int arr [ ] , int n , int k ) { int cnt = 0 ; int i , sum = 0 ; for ( i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) if ( Math . abs ( arr [ i ] - ( sum - arr [ i ] ) ) > k ) cnt ++ ; return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 } ; int n = arr . length ; int k = 1 ; System . out . print ( countAnomalies ( arr , n , k ) ) ; } }
class GFG { static int N = 100005 ; static int d [ ] = new int [ N ] , pre [ ] = new int [ N ] ; static void Positive_Divisors ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { if ( j * j == i ) d [ i ] ++ ; else d [ i ] += 2 ; } } } int ans = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( d [ i ] == d [ i - 1 ] ) ans ++ ; pre [ i ] = ans ; } } public static void main ( String [ ] args ) { Positive_Divisors ( ) ; int n = 15 ; System . out . println ( pre [ n ] ) ; } }
class GFG { static int numLen ( int K ) { if ( K % 2 == 0 K % 5 == 0 ) { return - 1 ; } int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) { return len ; } } return - 1 ; } public static void main ( String [ ] args ) { int K = 7 ; System . out . println ( numLen ( K ) ) ; } }
import java . util . Arrays ; class GFG { static int freqPairs ( int arr [ ] , int n ) { int max = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int freq [ ] = new int [ max + 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 2 * arr [ i ] ; j <= max ; j += arr [ i ] ) { if ( freq [ j ] >= 1 ) { count += freq [ j ] ; } } if ( freq [ arr [ i ] ] > 1 ) { count += freq [ arr [ i ] ] - 1 ; freq [ arr [ i ] ] -- ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 2 , 6 } ; int n = arr . length ; System . out . println ( freqPairs ( arr , n ) ) ; } }
class GFG { static void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) System . out . print ( y + " ▁ " + x + " ▁ " ) ; else System . out . print ( x + " ▁ " + y + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; } }
class GFG { static int maxSum ( int N ) { int ans = 0 ; for ( int u = 1 ; u <= N ; u ++ ) { for ( int v = 1 ; v <= N ; v ++ ) { if ( u == v ) continue ; int degreeU = 2 ; if ( u == 1 u == N ) degreeU = 1 ; int degreeV = 2 ; if ( v == 1 v == N ) degreeV = 1 ; ans += ( degreeU * degreeV ) ; } } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( maxSum ( N ) ) ; } }
import java . util . * ; class GFG { static boolean isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static int largestPalindrome ( int [ ] A , int n ) { Arrays . sort ( A ) ; for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( isPalindrome ( A [ i ] ) ) return A [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 232 , 54545 , 999991 } ; int n = A . length ; System . out . println ( largestPalindrome ( A , n ) ) ; } }
import java . io . * ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( findSum ( n ) ) ; } }
import java . util . * ; class GFG { public static double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . length ; System . out . println ( " Mean ▁ = ▁ " + findMean ( a , n ) ) ; } }
import java . util . ArrayList ; class GFG { static boolean CheckArray ( int arr [ ] , int n ) { int prod = 1 ; ArrayList < Integer > freq = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { freq . add ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = ( int ) Math . sqrt ( prod ) ; if ( root * root == prod ) { if ( freq . contains ( root ) & freq . lastIndexOf ( root ) != ( freq . size ( ) ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = arr . length ; if ( CheckArray ( arr , n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
import java . io . * ; import java . util . * ; class GFG { public static int minAreaRect ( int [ ] [ ] points ) { @ SuppressWarnings ( " unchecked " ) Set < Integer > columns = new HashSet ( ) ; for ( int [ ] point : points ) columns . add ( 40001 * point [ 0 ] + point [ 1 ] ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < points . length ; ++ i ) for ( int j = i + 1 ; j < points . length ; ++ j ) { if ( points [ i ] [ 0 ] != points [ j ] [ 0 ] && points [ i ] [ 1 ] != points [ j ] [ 1 ] ) { if ( columns . contains ( 40001 * points [ i ] [ 0 ] + points [ j ] [ 1 ] ) && columns . contains ( 40001 * points [ j ] [ 0 ] + points [ i ] [ 1 ] ) ) { ans = Math . min ( ans , Math . abs ( points [ j ] [ 0 ] - points [ i ] [ 0 ] ) * Math . abs ( points [ j ] [ 1 ] - points [ i ] [ 1 ] ) ) ; } } } return ans < Integer . MAX_VALUE ? ans : 0 ; } public static void main ( String [ ] args ) { int [ ] [ ] A = { { 1 , 1 } , { 1 , 3 } , { 3 , 1 } , { 3 , 3 } , { 2 , 2 } } ; System . out . println ( minAreaRect ( A ) ) ; } }
import java . util . * ; class GFG { static int toK ( int N , int K ) { int w = 1 ; int s = 0 ; while ( N != 0 ) { int r = N % K ; N = N / K ; s = r * w + s ; w *= 10 ; } return s ; } static boolean check ( int N ) { boolean fl = false ; while ( N != 0 ) { int r = N % 10 ; N = N / 10 ; if ( fl == true && r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } static void hasConsecutiveZeroes ( int N , int K ) { int z = toK ( N , K ) ; if ( check ( z ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 15 ; int K = 8 ; hasConsecutiveZeroes ( N , K ) ; } }
public class GFG { static int MAX = 1000000 ; static boolean prime [ ] = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) { prime [ i ] = false ; } } } } static void SumOfKthPrimes ( int arr [ ] , int n , int k ) { int c = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 3 , 5 , 7 , 11 } ; int n = arr . length ; int k = 2 ; SumOfKthPrimes ( arr , n , k ) ; } }
class GFG { static int MAX = 100000 ; static boolean [ ] prime = new boolean [ 100002 ] ; static void SieveOfEratosthenes ( ) { for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } static int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( ! prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } public static void main ( String [ ] args ) { int n = 256 ; System . out . println ( superpower ( n ) ) ; } }
class solution { static class Node { int data ; Node next ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int countPrime ( Node head_ref ) { int count = 0 ; Node ptr = head_ref ; while ( ptr != null ) { if ( isPrime ( ptr . data ) ) { count ++ ; } ptr = ptr . next ; } return count ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 17 ) ; head = push ( head , 10 ) ; head = push ( head , 6 ) ; head = push ( head , 5 ) ; head = push ( head , 15 ) ; System . out . print ( " Count ▁ of ▁ prime ▁ nodes ▁ = ▁ " + countPrime ( head ) ) ; } }
import java . io . * ; class GFG { static int smallestDivisor ( int n ) { if ( n % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= n ; i += 2 ) { if ( n % i == 0 ) return i ; } return n ; } public static void main ( String [ ] args ) { int n = 31 ; System . out . println ( smallestDivisor ( n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int countRabbits ( int Heads , int Legs ) { int count = 0 ; count = ( Legs ) - 2 * ( Heads ) ; count = count / 2 ; return count ; } public static void main ( String args [ ] ) { int Heads = 100 , Legs = 300 ; int Rabbits = countRabbits ( Heads , Legs ) ; System . out . println ( " Rabbits ▁ = ▁ " + Rabbits ) ; System . out . println ( " Pigeons ▁ = ▁ " + ( Heads - Rabbits ) ) ; } }
import java . util . * ; class gfg { public static double calculateSum ( double n ) { return 2 * ( Math . pow ( n , 6 ) + 15 * Math . pow ( n , 4 ) + 15 * Math . pow ( n , 2 ) + 1 ) ; } public static void main ( String [ ] args ) { double n = 1.4142 ; System . out . println ( ( int ) Math . ceil ( calculateSum ( n ) ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static double Nth_Term ( int n ) { return ( 2 * Math . pow ( n , 3 ) - 3 * Math . pow ( n , 2 ) + n + 6 ) / 6 ; } static public void main ( String args [ ] ) { int N = 8 ; System . out . println ( Nth_Term ( N ) ) ; } }
import java . util . * ; class solution { static int Sum_upto_nth_Term ( int n ) { return ( 1 - ( int ) Math . pow ( - 2 , n ) ) ; } public static void main ( String arr [ ] ) { int N = 5 ; System . out . println ( Sum_upto_nth_Term ( N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int xorEqualsOrCount ( int N ) { int count = 0 ; int bit ; while ( N > 0 ) { bit = N % 2 ; if ( bit == 0 ) count ++ ; N = N / 2 ; } return ( int ) Math . pow ( 2 , count ) ; } public static void main ( String args [ ] ) { int N = 7 ; System . out . println ( xorEqualsOrCount ( N ) ) ; } }
public class SumOfSeries { static int fact ( int n ) { if ( n == 1 ) return 1 ; return n * fact ( n - 1 ) ; } static double sum ( int x , int n ) { double total = 1.0 ; for ( int i = 1 ; i <= n ; i ++ ) { total = total + ( Math . pow ( x , i ) / fact ( i + 1 ) ) ; } return total ; } public static void main ( String [ ] args ) { int x = 5 , n = 4 ; System . out . print ( " Sum ▁ is : ▁ " + sum ( x , n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - ( int ) Math . pow ( i , 2 ) ; else result = result + ( int ) Math . pow ( i , 2 ) ; } return result ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum_of_series ( n ) ) ; n = 10 ; System . out . println ( sum_of_series ( n ) ) ; } }
import java . util . * ; class solution { static int findSum ( int N ) { return ( 2 * N * ( N + 1 ) * ( 4 * N + 17 ) + 54 * N ) / 6 ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( findSum ( N ) ) ; } }
public class GFG { private static final int MAX = 16 ; static long nCr [ ] [ ] = new long [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findCosNTheta ( double sinTheta , int n ) { double cosTheta = Math . sqrt ( 1 - sinTheta * sinTheta ) ; double ans = 0 ; long toggle = 1 ; for ( int i = 1 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void main ( String args [ ] ) { binomial ( ) ; double sinTheta = 0.5 ; int n = 10 ; System . out . println ( findCosNTheta ( sinTheta , n ) ) ; } }
class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 3 ; arr [ 2 ] = 5 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 3 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 5 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( printNthElement ( n ) ) ; } }
class GFG { static int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
class GFG { static int MAX = 16 ; static int [ ] [ ] nCr = new int [ MAX ] [ MAX ] ; static void binomial ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( j == 0 j == i ) nCr [ i ] [ j ] = 1 ; else nCr [ i ] [ j ] = nCr [ i - 1 ] [ j ] + nCr [ i - 1 ] [ j - 1 ] ; } } } static double findCosnTheta ( double cosTheta , int n ) { double sinTheta = Math . sqrt ( 1 - cosTheta * cosTheta ) ; double ans = 0 ; int toggle = 1 ; for ( int i = 0 ; i <= n ; i += 2 ) { ans = ans + nCr [ n ] [ i ] * Math . pow ( cosTheta , n - i ) * Math . pow ( sinTheta , i ) * toggle ; toggle = toggle * - 1 ; } return ans ; } public static void main ( String [ ] args ) { binomial ( ) ; double cosTheta = 0.5 ; int n = 10 ; System . out . println ( String . format ( " % .5f " , findCosnTheta ( cosTheta , n ) ) ) ; } }
import java . io . * ; class Nth { public int nthTerm ( int N ) { return ( N * ( ( N / 2 ) + ( ( N % 2 ) * 2 ) + N ) ) ; } } class GFG { public static void main ( String [ ] args ) { int N = 5 ; Nth a = new Nth ( ) ; System . out . println ( " Nth ▁ term ▁ for ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + a . nthTerm ( N ) ) ; } }
import java . util . * ; class solution { static int calculateSum ( int n ) { return ( ( int ) Math . pow ( 10 , n + 1 ) * ( 9 * n - 1 ) + 10 ) / ( int ) Math . pow ( 9 , 3 ) - n * ( n + 1 ) / 18 ; } public static void main ( String ar [ ] ) { int n = 3 ; System . out . println ( " Sum = ▁ " + calculateSum ( n ) ) ; } }
import java . util . * ; class GFG { static int calculateSum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } public static void main ( String ar [ ] ) { int n = 8 ; System . out . println ( calculateSum ( n ) ) ; } }
public class GFG { static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( isPower ( n ) ? " Yes " : " No " ) ; } }
class GFG { static float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0 ; float root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { square += Math . pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = ( float ) Math . sqrt ( mean ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = arr . length ; System . out . println ( rmsValue ( arr , n ) ) ; } }
import java . io . * ; class GFG { static double Mixture ( int X , int Y , int Z ) { double result1 = 0.0 , result = 0.0 ; result1 = ( ( X - Y ) / ( float ) X ) ; result = Math . pow ( result1 , Z ) ; result = result * X ; return result ; } public static void main ( String [ ] args ) { int X = 10 , Y = 2 , Z = 2 ; System . out . println ( ( float ) Mixture ( X , Y , Z ) + " ▁ litres " ) ; } }
class Geeks { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int check ( int n ) { int mod = ( int ) ( 1e9 + 7 ) ; n -- ; int ans = n * n ; if ( ans >= mod ) ans %= mod ; ans += n + 2 ; if ( ans >= mod ) ans %= mod ; ans = ( power ( 2 , n , mod ) % mod * ans % mod ) % mod ; ans = ( ans - 1 + mod ) % mod ; return ans ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( check ( n ) ) ; } }
import java . io . * ; import java . lang . * ; class GfG { public static int findLCM ( int a , int b ) { int lar = Math . max ( a , b ) ; int small = Math . min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } public static void main ( String [ ] argc ) { int a = 5 , b = 7 ; System . out . println ( " LCM ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + findLCM ( a , b ) ) ; } }
class GFG { static void primes ( int n ) { int i = 2 ; int j = 0 ; int [ ] result = new int [ n ] ; int z = 0 ; while ( j < n ) { boolean flag = true ; for ( int item = 2 ; item <= ( int ) ( i * 1 / 2 ) ; item ++ ) if ( i % item == 0 && i != item ) { flag = false ; break ; } if ( flag ) { result [ z ++ ] = i ; j += 1 ; } i += 1 ; } for ( i = 0 ; i < result . length ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) System . out . print ( result [ j ] ) ; System . out . print ( " ▁ " ) ; } } static void smar_wln ( int n ) { primes ( n ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " First ▁ " + n + " ▁ terms ▁ of ▁ the ▁ Sequence ▁ are " ) ; smar_wln ( n ) ; } }
import java . io . * ; class GFG { static int Pentatope_number ( int n ) { return n * ( n + 1 ) * ( n + 2 ) * ( n + 3 ) / 24 ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; n = 12 ; System . out . println ( n + " th ▁ " + " Pentatope ▁ number ▁ : " + Pentatope_number ( n ) ) ; } }
import java . io . * ; class GFG { static int centered_square_num ( int n ) { return n * n + ( ( n - 1 ) * ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( n + " th ▁ Centered " + " ▁ square ▁ number : ▁ " + centered_square_num ( n ) ) ; } }
import java . io . * ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( seriesSum ( n ) ) ; } }
class GFG { static void SieveOfEratosthenes ( int n , boolean prime [ ] , boolean primesquare [ ] , int a [ ] ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= ( n * n ) ; i ++ ) primesquare [ i ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } int j = 0 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { a [ j ] = p ; primesquare [ p * p ] = true ; j ++ ; } } } static int countDivisors ( int n ) { if ( n == 1 ) return 1 ; boolean prime [ ] = new boolean [ n + 1 ] , primesquare [ ] = new boolean [ n * n + 1 ] ; SieveOfEratosthenes ( n , prime , primesquare , a ) ; int ans = 1 ; for ( int i = 0 ; ; i ++ ) { if ( a [ i ] * a [ i ] * a [ i ] > n ) break ; int cnt = 1 ; while ( n % a [ i ] == 0 ) { n = n / a [ i ] ; } ans = ans * cnt ; } if ( prime [ n ] ) ans = ans * 2 ; else if ( primesquare [ n ] ) ans = ans * 3 ; else if ( n != 1 ) ans = ans * 4 ; } static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static boolean checkArithmetic ( int n ) { int count = countDivisors ( n ) ; int sum = sumofFactors ( n ) ; return ( sum % count == 0 ) ; } public static void main ( String [ ] args ) { int n = 6 ; if ( checkArithmetic ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int magicOfSequence ( int N ) { return ( N * ( N + 1 ) / 2 ) + 2 * N ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( magicOfSequence ( N ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static List < Integer > power = new ArrayList < Integer > ( ) ; static void nextPower ( Integer N ) { Integer carry = 0 ; for ( int i = 0 ; i < power . size ( ) ; i ++ ) { Integer prod = ( power . get ( i ) * N ) + carry ; power . set ( i , prod % 10 ) ; carry = prod / 10 ; } while ( carry >= 1 ) { power . add ( carry % 10 ) ; carry = carry / 10 ; } } static void printPowerNumber ( int X , int N ) { power . add ( 1 ) ; List < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N ) ; res . add ( power . get ( power . size ( ) - 1 ) ) ; res . add ( power . get ( 0 ) ) ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) ) ; } public static void main ( String args [ ] ) { Integer N = 19 , X = 4 ; printPowerNumber ( X , N ) ; } }
class GFG { static int firstDigit ( int n ) { int fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return fact ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( firstDigit ( n ) ) ; } }
import java . io . * ; class GFG { static void series ( int A , int X , int n ) { int term = ( int ) Math . pow ( A , n ) ; System . out . print ( term + " ▁ " ) ; for ( int i = 1 ; i <= n ; i ++ ) { term = term * X * ( n - i + 1 ) / ( i * A ) ; System . out . print ( term + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int sumofseries ( int n ) { int res = 0 ; for ( int i = 1 ; i <= n ; i ++ ) res += ( i ) * ( i + 1 ) * ( i + 2 ) ; return res ; } public static void main ( String [ ] args ) { System . out . println ( sumofseries ( 3 ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; public class GFG { static void printGMeans ( int A , int B , int N ) { float R = ( float ) Math . pow ( ( float ) ( B / A ) , 1.0 / ( float ) ( N + 1 ) ) ; for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( A * Math . pow ( R , i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int A = 3 , B = 81 , N = 2 ; printGMeans ( A , B , N ) ; } }
import java . io . * ; class GFG { static int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return Integer . MAX_VALUE ; boolean negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } public static void main ( String [ ] args ) { int num1 = 13 , num2 = 2 ; System . out . println ( division ( num1 , num2 ) ) ; } }
import java . io . * ; class GFG { static void Nonagonal ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( i * ( 7 * i - 5 ) / 2 ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 10 ; Nonagonal ( n ) ; } }
import java . io . * ; class GFG { static int seriesFunc ( int n ) { int sumSquare = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sumNatural = ( n * ( n + 1 ) / 2 ) ; return ( sumSquare + sumNatural + 1 ) ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . println ( seriesFunc ( n ) ) ; n = 13 ; System . out . println ( seriesFunc ( 13 ) ) ; } }
import java . io . * ; class GFG { static boolean checkplusperfect ( int x ) { int temp = x ; int n = 0 ; while ( x != 0 ) { x /= 10 ; n ++ ; } x = temp ; int sum = 0 ; while ( x != 0 ) { sum += Math . pow ( x % 10 , n ) ; x /= 10 ; } return ( sum == temp ) ; } public static void main ( String [ ] args ) { int x = 9474 ; if ( checkplusperfect ( x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean Div_by_8 ( int n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } public static void main ( String [ ] args ) { int n = 16 ; if ( Div_by_8 ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int n = A . length ; System . out . println ( subsetCount ( A , n ) ) ; } }
import java . io . * ; class GFG { static float Calculate_GST ( float org_cost , float N_price ) { return ( ( ( N_price - org_cost ) * 100 ) / org_cost ) ; } public static void main ( String [ ] args ) { float org_cost = 100 ; float N_price = 120 ; System . out . print ( " ▁ GST ▁ = ▁ " + Calculate_GST ( org_cost , N_price ) + " % " ) ; } }
import java . io . * ; class GFG { static int centeredHexagonalNumber ( int n ) { return 3 * n * ( n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . print ( n + " th ▁ centered ▁ " + " hexagonal ▁ number : ▁ " ) ; System . out . println ( centeredHexagonalNumber ( n ) ) ; } }
import java . io . * ; class GFG { static int averageEven ( int n ) { if ( n % 2 != 0 ) { System . out . println ( " Invalid ▁ Input " ) ; return - 1 ; } int sum = 0 , count = 0 ; while ( n >= 2 ) { count ++ ; sum += n ; n = n - 2 ; } return sum / count ; } public static void main ( String args [ ] ) { int n = 16 ; System . out . println ( averageEven ( n ) ) ; } }
class demo { public static int find_distance ( int n ) { return n * ( 3 * n + 7 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . print ( " Distance ▁ = ▁ " ) ; System . out . println ( find_distance ( n ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } public static void main ( String [ ] args ) { int n1 = 11 , n2 = 13 ; if ( twinPrime ( n1 , n2 ) ) System . out . println ( " Twin ▁ Prime " ) ; else System . out . println ( " Not ▁ Twin ▁ Prime " ) ; } }
import java . io . * ; class GFG { static int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( evenbinomialCoeffSum ( n ) ) ; } }
import java . io . * ; class GfG { static int sumOfTheSeries ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Sum ▁ = ▁ " + sumOfTheSeries ( n ) ) ; } }
import java . util . * ; class GFG { static int sumOfSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) sum += j ; return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumOfSeries ( n ) ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static int cpFact ( int x , int y ) { while ( gcd ( x , y ) != 1 ) { x = x / gcd ( x , y ) ; } return x ; } public static void main ( String [ ] args ) { int x = 15 ; int y = 3 ; System . out . println ( cpFact ( x , y ) ) ; x = 14 ; y = 28 ; System . out . println ( cpFact ( x , y ) ) ; x = 7 ; y = 3 ; System . out . println ( cpFact ( x , y ) ) ; } }
import java . io . * ; class GFG { static int squaresum ( int n ) { return ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; } public static void main ( String args [ ] ) throws IOException { int n = 4 ; System . out . println ( squaresum ( n ) ) ; } }
import java . util . * ; import java . lang . * ; public class GfG { public static int counLastDigitK ( int low , int high , int k ) { int count = 0 ; for ( int i = low ; i <= high ; i ++ ) if ( i % 10 == k ) count ++ ; return count ; } public static void main ( String args [ ] ) { int low = 3 , high = 35 , k = 3 ; System . out . println ( counLastDigitK ( low , high , k ) ) ; } }
import java . util . * ; import static java . lang . Math . pow ; class Triangle { public static double numberOfTriangles ( int n ) { double ans = 2 * ( pow ( 3 , n ) ) - 1 ; return ans ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( numberOfTriangles ( n ) ) ; } }
import java . util . * ; class Digits { public static int motzkin ( int n ) { int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = ( ( 2 * i + 1 ) * dp [ i - 1 ] + ( 3 * i - 3 ) * dp [ i - 2 ] ) / ( i + 2 ) ; return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . print ( motzkin ( n ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { return k * k * k ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }
import java . io . * ; class Sums { static void printXYZ ( int n ) { if ( n == 1 ) System . out . println ( - 1 ) ; else { System . out . println ( " x ▁ is ▁ " + n ) ; System . out . println ( " y ▁ is ▁ " + ( n + 1 ) ) ; System . out . println ( " z ▁ is ▁ " + ( n * ( n + 1 ) ) ) ; } } public static void main ( String [ ] args ) { int n = 7 ; printXYZ ( n ) ; } }
import java . io . * ; class Series { static int term ( int n ) { return n * ( n + 1 ) / 2 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( term ( n ) ) ; } }
import java . io . * ; class GFG { static int series ( int n ) { return ( 8 * n * n ) + 1 ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( series ( n ) ) ; } }
class Divisible { public static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static boolean isDivisible ( int x , int y ) { if ( y == 1 ) return true ; int z = gcd ( x , y ) ; if ( z == 1 ) return false ; return isDivisible ( x , y / z ) ; } public static void main ( String [ ] args ) { int x = 18 , y = 12 ; if ( isDivisible ( x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { double n = 5 , a = 2 ; System . out . println ( sumOfSeries ( a , n ) ) ; } static double sumOfSeries ( double a , double n ) { double res = 0 , prev = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { prev *= ( a / i ) ; res = res + prev ; } return ( res ) ; } }
class GFG { static float Cel_To_Fah ( float n ) { return ( ( n * 9.0f / 5.0f ) + 32.0f ) ; } public static void main ( String [ ] args ) { float n = 20.0f ; System . out . println ( Cel_To_Fah ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int countCompositions ( int n ) { return 1 << ( n - 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . print ( countCompositions ( n ) ) ; } }
import java . io . * ; class GFG { static void printTrib ( int n ) { int dp [ ] = new int [ n ] ; dp [ 0 ] = dp [ 1 ] = 0 ; dp [ 2 ] = 1 ; for ( int i = 3 ; i < n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + dp [ i - 3 ] ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( dp [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; printTrib ( n ) ; } }
import java . io . * ; class GFG { static void multiply ( int T [ ] [ ] , int M [ ] [ ] ) { int a , b , c , d , e , f , g , h , i ; a = T [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 0 ] ; b = T [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 1 ] ; c = T [ 0 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 0 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 0 ] [ 2 ] * M [ 2 ] [ 2 ] ; d = T [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 0 ] ; e = T [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 1 ] ; f = T [ 1 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 1 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 1 ] [ 2 ] * M [ 2 ] [ 2 ] ; g = T [ 2 ] [ 0 ] * M [ 0 ] [ 0 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 0 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 0 ] ; h = T [ 2 ] [ 0 ] * M [ 0 ] [ 1 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 1 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 1 ] ; i = T [ 2 ] [ 0 ] * M [ 0 ] [ 2 ] + T [ 2 ] [ 1 ] * M [ 1 ] [ 2 ] + T [ 2 ] [ 2 ] * M [ 2 ] [ 2 ] ; T [ 0 ] [ 0 ] = a ; T [ 0 ] [ 1 ] = b ; T [ 0 ] [ 2 ] = c ; T [ 1 ] [ 0 ] = d ; T [ 1 ] [ 1 ] = e ; T [ 1 ] [ 2 ] = f ; T [ 2 ] [ 0 ] = g ; T [ 2 ] [ 1 ] = h ; T [ 2 ] [ 2 ] = i ; } static void power ( int T [ ] [ ] , int n ) { if ( n == 0 n == 1 ) return ; int M [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; power ( T , n / 2 ) ; multiply ( T , T ) ; if ( n % 2 != 0 ) multiply ( T , M ) ; } static int tribonacci ( int n ) { int T [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 0 n == 1 ) return 0 ; else power ( T , n - 2 ) ; return T [ 0 ] [ 0 ] ; } public static void main ( String args [ ] ) { int n = 10 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( tribonacci ( i ) + " ▁ " ) ; System . out . println ( ) ; } }
import java . io . * ; class GFG { static float geometricMean ( int [ ] arr , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( float ) Math . log ( arr [ i ] ) ; sum = sum / n ; return ( float ) Math . exp ( sum ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = arr . length ; System . out . println ( geometricMean ( arr , n ) ) ; } }
import java . util . Stack ; public class GFG { static long smallestNumber ( int n ) { if ( n >= 0 && n <= 9 ) { return n ; } Stack < Integer > digits = new Stack < > ( ) ; for ( int i = 9 ; i >= 2 && n > 1 ; i -- ) { while ( n % i == 0 ) { digits . push ( i ) ; n = n / i ; } } if ( n != 1 ) { return - 1 ; } long k = 0 ; while ( ! digits . empty ( ) ) { k = k * 10 + digits . peek ( ) ; digits . pop ( ) ; } return k ; } static public void main ( String [ ] args ) { int n = 100 ; System . out . println ( smallestNumber ( n ) ) ; } }
import java . util . * ; class GFG { public static void printTaxicab2 ( int N ) { int i = 1 , count = 0 ; while ( count < N ) { int int_count = 0 ; for ( int j = 1 ; j <= Math . pow ( i , 1.0 / 3 ) ; j ++ ) for ( int k = j + 1 ; k <= Math . pow ( i , 1.0 / 3 ) ; k ++ ) if ( j * j * j + k * k * k == i ) int_count ++ ; if ( int_count == 2 ) { count ++ ; System . out . println ( count + " ▁ " + i ) ; } i ++ ; } } public static void main ( String [ ] args ) { int N = 5 ; printTaxicab2 ( N ) ; } }
class GFG { public static boolean isMagic ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void main ( String args [ ] ) { int n = 1234 ; if ( isMagic ( n ) ) System . out . println ( " Magic ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ magic ▁ Number " ) ; } }
class GFG { public static void main ( String [ ] args ) { int x = 1234 ; if ( x % 9 == 1 ) System . out . printf ( " Magic ▁ Number " ) ; else System . out . printf ( " Not ▁ a ▁ Magic ▁ Number " ) ; } }
import java . util . * ; class GFG { public static void printSequence ( int n ) { int a = 1 ; int ans = 2 ; int N = 1000000007 ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( ans + " ▁ " ) ; ans = ( ( a % N ) * ( ans % N ) ) % N ; a = ans ; ans = ( ans + 1 ) % N ; } } public static void main ( String [ ] args ) { int n = 6 ; printSequence ( n ) ; } }
import java . io . * ; class Composite { static boolean isComposite ( int n ) { if ( n <= 1 ) System . out . println ( " False " ) ; if ( n <= 3 ) System . out . println ( " 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 ; } public static void main ( String args [ ] ) { System . out . println ( isComposite ( 11 ) ? " true " : " false " ) ; System . out . println ( isComposite ( 15 ) ? " true " : " false " ) ; } }
import java . io . * ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int x = 1 ; x <= n ; x ++ ) sum = sum + x ; return sum ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( findSum ( n ) ) ; } }
import java . util . * ; class GFG { static int c ; static int HailstoneNumbers ( int N ) { System . out . print ( N + " ▁ " ) ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } return c ; } public static void main ( String [ ] args ) { int N = 7 ; int x ; x = HailstoneNumbers ( N ) ; System . out . println ( ) ; System . out . println ( " Number ▁ of ▁ Steps : ▁ " + x ) ; } }
class GFG { static int SUM ( int n , int m ) { if ( m == 1 ) return ( n * ( n + 1 ) / 2 ) ; int sum = SUM ( n , m - 1 ) ; return ( sum * ( sum + 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; int m = 3 ; System . out . println ( " SUM ( " + n + " , ▁ " + m + " ) : ▁ " + SUM ( n , m ) ) ; } }
import java . util . * ; class GFG { static int count ( int x , int y ) { Map < Integer , Integer > m = new HashMap < > ( ) ; while ( x % y != 0 ) { x = x % y ; ans ++ ; if ( m . containsKey ( x ) ) return - 1 ; m . put ( x , 1 ) ; x = x * 10 ; } return ans ; } public static void main ( String [ ] args ) { int res = count ( 1 , 2 ) ; if ( ( res == - 1 ) ) System . out . println ( " INF " ) ; else System . out . println ( res ) ; res = count ( 5 , 3 ) ; if ( ( res == - 1 ) ) System . out . println ( " INF " ) ; else System . out . println ( res ) ; res = count ( 3 , 5 ) ; if ( ( res == - 1 ) ) System . out . println ( " INF " ) ; else System . out . println ( res ) ; } }
class GFG { static int xorCalc ( int k ) { if ( k == 1 ) return 2 ; if ( ( ( k + 1 ) & k ) == 0 ) return k / 2 ; return 1 ; } public static void main ( String [ ] args ) { int k = 31 ; System . out . println ( xorCalc ( k ) ) ; } }
public class GFG { static String findNthNo ( int n ) { String res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } StringBuilder sb = new StringBuilder ( res ) ; sb . reverse ( ) ; return new String ( sb ) ; } public static void main ( String args [ ] ) { int n = 13 ; System . out . print ( findNthNo ( n ) ) ; } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int divCount ( int n ) { boolean hash [ ] = new boolean [ n + 1 ] ; Arrays . fill ( hash , true ) ; for ( int p = 2 ; p * p < n ; p ++ ) if ( hash [ p ] == true ) for ( int i = p * 2 ; i < n ; i += p ) hash [ i ] = false ; int total = 1 ; for ( int p = 2 ; p <= n ; p ++ ) { if ( hash [ p ] ) { int count = 0 ; if ( n % p == 0 ) { while ( n % p == 0 ) { n = n / p ; count ++ ; } total = total * ( count + 1 ) ; } } } return total ; } public static void main ( String [ ] args ) { int n = 24 ; System . out . print ( divCount ( n ) ) ; } }
class GFG { static int getMax ( int [ ] Arr ) { int max = Arr [ 0 ] ; for ( int i = 1 ; i < Arr . length ; i ++ ) if ( Arr [ i ] > max ) max = Arr [ i ] ; return max ; } static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) for ( int j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return getMax ( arr ) ; } public static void main ( String [ ] args ) { int N = 40 ; System . out . println ( maxPrimefactorNum ( N ) ) ; } }
class GFG { public static int arraySum ( int arr [ ] , int n ) { int x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 11 , 12 , 13 , 12 , 11 , 10 } ; int n = arr . length ; System . out . print ( arraySum ( arr , n ) ) ; } }
import java . util . * ; class GFG { static boolean isBalancePossible ( int T , int a ) { Vector < Integer > baseForm = new Vector < > ( ) ; int s = 0 ; while ( T > 0 ) { baseForm . add ( T % a ) ; T /= a ; s ++ ; } baseForm . add ( 0 ) ; for ( int i = 0 ; i < s ; i ++ ) { if ( baseForm . get ( i ) != 0 && baseForm . get ( i ) != 1 && baseForm . get ( i ) != ( a - 1 ) && baseForm . get ( i ) != a ) { return false ; } if ( baseForm . get ( i ) == a || baseForm . get ( i ) == ( a - 1 ) ) { baseForm . add ( i + 1 , baseForm . get ( i + 1 ) + 1 ) ; } } return true ; } public static void main ( String [ ] args ) { int T = 11 ; int a = 4 ; boolean balancePossible = isBalancePossible ( T , a ) ; if ( balancePossible ) { System . out . println ( " Balance ▁ is ▁ possible " ) ; } else { System . out . println ( " Balance ▁ is ▁ not ▁ possible " ) ; } } }
class GFG { public static int countDigits ( int a , int b ) { if ( a == 0 b == 0 ) return 1 ; return ( int ) Math . floor ( Math . log10 ( Math . abs ( a ) ) + Math . log10 ( Math . abs ( b ) ) ) + 1 ; } public static void main ( String [ ] args ) { int a = 33 ; int b = - 24 ; System . out . print ( countDigits ( a , b ) ) ; } }
class GFG { static int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } public static void main ( String arg [ ] ) { int n = 5 ; int m = 8 ; int k = 2 ; System . out . print ( lastPosition ( n , m , k ) ) ; } }
class GFG { static void primesInRange ( int n ) { int fact = 1 ; for ( int k = 2 ; k < n ; k ++ ) { fact = fact * ( k - 1 ) ; if ( ( fact + 1 ) % k == 0 ) System . out . println ( k ) ; } } public static void main ( String [ ] args ) { int n = 15 ; primesInRange ( n ) ; } }
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 ) ) ; } }
public class close_to_n_divisible_m { static int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . abs ( n - n1 ) < Math . abs ( n - n2 ) ) return n1 ; return n2 ; } public static void main ( String args [ ] ) { int n = 13 , m = 4 ; System . out . println ( closestNumber ( n , m ) ) ; n = - 15 ; m = 6 ; System . out . println ( closestNumber ( n , m ) ) ; n = 0 ; m = 8 ; System . out . println ( closestNumber ( n , m ) ) ; n = 18 ; m = - 7 ; System . out . println ( closestNumber ( n , m ) ) ; } }
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { static boolean checkPronic ( int x ) { for ( int i = 0 ; i <= ( int ) ( Math . sqrt ( x ) ) ; i ++ ) if ( x == i * ( i + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i <= 200 ; i ++ ) if ( checkPronic ( i ) ) System . out . print ( i + " ▁ " ) ; } }
public class Main { static int findMinSum ( int num ) { int sum = 0 ; for ( int i = 2 ; i * i <= num ; i ++ ) { while ( num % i == 0 ) { sum += i ; num /= i ; } } sum += num ; return sum ; } public static void main ( String [ ] args ) { int num = 12 ; System . out . println ( findMinSum ( num ) ) ; } }
import java . io . * ; class GFG { static void findMin ( int sum ) { int a = 0 , b = 0 ; while ( sum > 0 ) { if ( sum % 7 == 0 ) { b ++ ; sum -= 7 ; } else if ( sum % 4 == 0 ) { a ++ ; sum -= 4 ; } else { a ++ ; sum -= 4 ; } } if ( sum < 0 ) { System . out . print ( " - 1n " ) ; return ; } for ( int i = 0 ; i < a ; i ++ ) System . out . print ( "4" ) ; for ( int i = 0 ; i < b ; i ++ ) System . out . print ( "7" ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) throws IOException { findMin ( 15 ) ; } }
class GFG { static int minNum ( int arr [ ] , int n ) { int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 != 0 ) odd += 1 ; return ( ( odd % 2 ) != 0 ) ? 1 : 2 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( minNum ( arr , n ) ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static int findPrime ( int n ) { int num = n + 1 ; while ( num > 0 ) { if ( isPrime ( num ) ) return num ; num = num + 1 ; } return 0 ; } static int minNumber ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( isPrime ( sum ) ) return 0 ; int num = findPrime ( sum ) ; return num - sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 12 } ; int n = arr . length ; System . out . println ( minNumber ( arr , n ) ) ; } }
public class GFG { static int printMaxNum ( int num ) { int count [ ] = new int [ 10 ] ; String str = Integer . toString ( num ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) count [ str . charAt ( i ) - '0' ] ++ ; int result = 0 , multiplier = 1 ; for ( int i = 0 ; i <= 9 ; i ++ ) { while ( count [ i ] > 0 ) { result = result + ( i * multiplier ) ; count [ i ] -- ; multiplier = multiplier * 10 ; } } return result ; } public static void main ( String [ ] args ) { int num = 38293367 ; System . out . println ( printMaxNum ( num ) ) ; } }
public class GFG { static String largestNumber ( String num ) { int n = num . length ( ) ; int right ; int rightMax [ ] = new int [ n ] ; rightMax [ n - 1 ] = - 1 ; right = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) < num . charAt ( right ) ) rightMax [ i ] = right ; else { rightMax [ i ] = - 1 ; right = i ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( rightMax [ i ] != - 1 ) { num = swap ( num , i , rightMax [ i ] ) ; break ; } } return num ; } static String swap ( String num , int i , int j ) { StringBuilder sb = new StringBuilder ( num ) ; sb . setCharAt ( i , num . charAt ( j ) ) ; sb . setCharAt ( j , num . charAt ( i ) ) ; return sb . toString ( ) ; } public static void main ( String [ ] args ) { String num = "8725634" ; System . out . println ( " Largest ▁ Number ▁ : ▁ " + largestNumber ( num ) ) ; } }
import java . util . * ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . pow ( p , k ) * ( float ) Math . pow ( 1 - p , n - k ) ; } public static void main ( String [ ] args ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; System . out . print ( " Probability ▁ of ▁ " + k ) ; System . out . print ( " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " + n ) ; System . out . println ( " ▁ times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is ▁ " + p ) ; System . out . println ( " ▁ is ▁ = ▁ " + probability ) ; } }
class GFG { public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int divisors [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % j == 0 ) { divisors [ j ] ++ ; if ( j != arr [ i ] / j ) divisors [ arr [ i ] / j ] ++ ; } } } for ( int i = high ; i >= 1 ; i -- ) if ( divisors [ i ] > 1 ) return i ; return 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . length ; System . out . println ( findMaxGCD ( arr , n ) ) ; } }
class GFG { public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int count [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . length ; System . out . println ( findMaxGCD ( arr , n ) ) ; } }
class GFG { static int count_one ( int n ) { int c_one = 0 ; while ( n != 0 ) { int rem = n % 10 ; if ( rem == 1 ) c_one = c_one + 1 ; n = n / 10 ; } return c_one ; } static int checkEvil ( int n ) { int i = 0 , bin = 0 , n_one = 0 ; while ( n != 0 ) { int r = n % 2 ; bin = bin + r * ( int ) ( Math . pow ( 10 , i ) ) ; n = n / 2 ; } n_one = count_one ( bin ) ; if ( n_one % 2 == 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int i , check , num ; num = 32 ; check = checkEvil ( num ) ; if ( check == 1 ) System . out . println ( num + " ▁ is ▁ Evil ▁ Number " ) ; else System . out . println ( num + " ▁ is ▁ Odious ▁ Number " ) ; } }
class GFG { static int CountPairs ( int n ) { int k = n ; int imin = 1 ; int ans = 0 ; while ( imin <= n ) { int imax = n / k ; ans += k * ( imax - imin + 1 ) ; imin = imax + 1 ; k = n / imin ; } return ans ; } public static void main ( String [ ] args ) { System . out . println ( CountPairs ( 1 ) ) ; System . out . println ( CountPairs ( 2 ) ) ; System . out . println ( CountPairs ( 3 ) ) ; } }
class GFG { static long powermod ( long x , long y , long p ) { while ( y > 0 ) { if ( ( y & 1L ) > 0 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long a , long m ) { long m0 = m , t , q ; long x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static long evaluteExpression ( long n ) { long firstsum = 0 , mod = 10 ; for ( long i = 2 , j = 0 ; ( 1L << j ) <= n ; i *= i , ++ j ) firstsum = ( firstsum + i ) % mod ; long secondsum = ( powermod ( 4L , n + 1 , mod ) - 1 ) * modInverse ( 3L , mod ) ; return ( firstsum * secondsum ) % mod ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( evaluteExpression ( n ) ) ; n = 10 ; System . out . println ( evaluteExpression ( n ) ) ; } }
import java . util . * ; class GFG { static final int MAX = 100001 ; static class pair { int F , S ; public pair ( int f , int s ) { F = f ; S = s ; } } static Vector < pair > [ ] factors = new Vector [ MAX ] ; static void sieveOfEratothenese ( ) { boolean [ ] isPrime = new boolean [ MAX ] ; Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i ; j < MAX ; j += i ) { int k , l ; isPrime [ j ] = false ; for ( k = j , l = 0 ; k % i == 0 ; l ++ , k /= i ) ; factors [ j ] . add ( new pair ( i , l ) ) ; } } } } static int sumOfProperDivisors ( int num ) { int mul = 1 ; for ( int i = 0 ; i < factors [ num ] . size ( ) ; i ++ ) mul *= ( ( Math . pow ( factors [ num ] . get ( i ) . F , factors [ num ] . get ( i ) . S + 1 ) - 1 ) / ( factors [ num ] . get ( i ) . F - 1 ) ) ; return mul - num ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAX ; i ++ ) factors [ i ] = new Vector < pair > ( ) ; sieveOfEratothenese ( ) ; int arr [ ] = { 8 , 13 , 24 , 36 , 59 , 75 , 91 } ; for ( int i = 0 ; i < arr . length ; i ++ ) System . out . print ( sumOfProperDivisors ( arr [ i ] ) + " ▁ " ) ; System . out . println ( ) ; } }
public class GFG { static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } public static void main ( String [ ] args ) { System . out . println ( PowerOFPINnfactorial ( 4 , 2 ) ) ; } }
import java . io . * ; class GFG { static int decimalToBinary ( int N ) { int B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; double c = Math . pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . println ( decimalToBinary ( N ) ) ; } }
class GFG { static int binaryToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 2 ; } return dec_value ; } public static void main ( String [ ] args ) { int num = 10101001 ; System . out . println ( binaryToDecimal ( num ) ) ; } }
class GFG { public static int stirlingFactorial ( double n ) { if ( n == 1 ) return 1 ; double z ; z = Math . sqrt ( 2 * 3.14 * n ) * Math . pow ( ( n / e ) , n ) ; return ( int ) ( z ) ; } public static void main ( String [ ] args ) { System . out . println ( stirlingFactorial ( 1 ) ) ; System . out . println ( stirlingFactorial ( 2 ) ) ; System . out . println ( stirlingFactorial ( 3 ) ) ; System . out . println ( stirlingFactorial ( 4 ) ) ; System . out . println ( stirlingFactorial ( 5 ) ) ; System . out . println ( stirlingFactorial ( 6 ) ) ; System . out . println ( stirlingFactorial ( 7 ) ) ; } }
public class CountXor { static int countXorPair ( int arr [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } return odd * even ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; System . out . println ( countXorPair ( arr , arr . length ) ) ; } }
import java . io . * ; public class LychrelNumberTest { private static int MAX_ITERATIONS = 20 ; private static boolean isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } private static boolean isPalindrome ( final long number ) { return number == reverse ( number ) ; } private static long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } public static void main ( String [ ] args ) { long number = 295 ; System . out . println ( number + " ▁ is ▁ lychrel ? ▁ " + isLychrel ( number ) ) ; } }
import java . io . * ; class GFG { static int findRectNum ( int n ) { return n * ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( findRectNum ( n ) ) ; } }
import java . io . * ; import static java . lang . Math . * ; class Muller { static final int MAX_ITERATIONS = 10000 ; static double f ( double x ) { return 1 * pow ( x , 3 ) + 2 * x * x + 10 * x - 20 ; } static void Muller ( double a , double b , double c ) { int i ; double res ; for ( i = 0 ; ; ++ i ) { double f1 = f ( a ) ; double f2 = f ( b ) ; double f3 = f ( c ) ; double d1 = f1 - f3 ; double d2 = f2 - f3 ; double h1 = a - c ; double h2 = b - c ; double a0 = f3 ; double a1 = ( ( ( d2 * pow ( h1 , 2 ) ) - ( d1 * pow ( h2 , 2 ) ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double a2 = ( ( ( d1 * h2 ) - ( d2 * h1 ) ) / ( ( h1 * h2 ) * ( h1 - h2 ) ) ) ; double x = ( ( - 2 * a0 ) / ( a1 + abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; double y = ( ( - 2 * a0 ) / ( a1 - abs ( sqrt ( a1 * a1 - 4 * a0 * a2 ) ) ) ) ; if ( x >= y ) res = x + c ; else res = y + c ; double m = res * 100 ; double n = c * 100 ; m = floor ( m ) ; n = floor ( n ) ; if ( m == n ) break ; a = b ; b = c ; c = res ; if ( i > MAX_ITERATIONS ) { System . out . println ( " Root ▁ cannot ▁ be ▁ found ▁ using " + " ▁ Muller ' s ▁ method " ) ; break ; } } if ( i <= MAX_ITERATIONS ) System . out . println ( " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ " + res ) ; } public static void main ( String args [ ] ) { double a = 0 , b = 1 , c = 2 ; Muller ( a , b , c ) ; } }
import java . util . * ; class GFG { static int MAX = 100001 ; static ArrayList < Integer > p = new ArrayList < Integer > ( ) ; static void sieve ( ) { int [ ] isPrime = new int [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( isPrime [ i ] == 0 ) { p . add ( i ) ; for ( int j = 2 ; i * j <= MAX ; j ++ ) isPrime [ i * j ] = 1 ; } } } static int phi ( int n ) { int res = n ; for ( int i = 0 ; p . get ( i ) * p . get ( i ) <= n ; i ++ ) { if ( n % p . get ( i ) == 0 ) { res -= ( res / p . get ( i ) ) ; while ( n % p . get ( i ) == 0 ) n /= p . get ( i ) ; } } if ( n > 1 ) res -= ( res / n ) ; return res ; } public static void main ( String [ ] args ) { sieve ( ) ; System . out . println ( phi ( 11 ) ) ; System . out . println ( phi ( 21 ) ) ; System . out . println ( phi ( 31 ) ) ; System . out . println ( phi ( 41 ) ) ; System . out . println ( phi ( 51 ) ) ; System . out . println ( phi ( 61 ) ) ; System . out . println ( phi ( 91 ) ) ; System . out . println ( phi ( 101 ) ) ; } }
import static java . lang . Math . pow ; class Test { static void nthprimedigitsnumber ( long n ) { long len = 1 ; long prev_count = 0 ; while ( true ) { long curr_count = ( long ) ( prev_count + pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + pow ( 4 , len - i ) < n ) prev_count += pow ( 4 , len - i ) ; else { if ( j == 1 ) System . out . print ( "2" ) ; else if ( j == 2 ) System . out . print ( "3" ) ; else if ( j == 3 ) System . out . print ( "5" ) ; else if ( j == 4 ) System . out . print ( "7" ) ; break ; } } } System . out . println ( ) ; } public static void main ( String args [ ] ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; } }
class Gfg { static int cassini ( int n ) { return ( n & 1 ) != 0 ? - 1 : 1 ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( cassini ( n ) ) ; } }
class Test { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static void printRange ( int n ) { int a = factorial ( n + 2 ) + 2 ; int b = a + n - 1 ; System . out . println ( " [ " + a + " , ▁ " + b + " ] " ) ; } public static void main ( String args [ ] ) throws Exception { int n = 3 ; printRange ( n ) ; } }
import static java . lang . Math . pow ; class Test { static int sumOfTermsInNthRow ( int n ) { int sum = ( int ) ( n * ( 2 * pow ( n , 2 ) + 1 ) ) ; return sum ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Sum ▁ of ▁ all ▁ the ▁ terms ▁ in ▁ nth ▁ row ▁ = ▁ " + sumOfTermsInNthRow ( n ) ) ; } }
class Test { static int firstDigit ( int x ) { while ( x >= 10 ) x = x / 10 ; return x ; } public static void main ( String args [ ] ) { System . out . println ( firstDigit ( 12345 ) ) ; System . out . println ( firstDigit ( 5432 ) ) ; } }
import java . * ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void main ( String [ ] args ) { int n = 11 , d = 1 ; System . out . println ( getOccurence ( n , d ) ) ; } }
import java . util . * ; class GFG { static void printUnique ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { String s = String . valueOf ( i ) ; HashSet < Integer > uniDigits = new HashSet < Integer > ( ) ; for ( int c : s . toCharArray ( ) ) uniDigits . add ( c ) ; if ( s . length ( ) == uniDigits . size ( ) ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int l = 1 , r = 20 ; printUnique ( l , r ) ; } }
import static java . lang . Math . cos ; class GFG { static void cal_cos ( float n ) { float accuracy = ( float ) 0.0001 , x1 , denominator , cosx , cosval ; n = n * ( float ) ( 3.142 / 180.0 ) ; x1 = 1 ; cosx = x1 ; cosval = ( float ) cos ( n ) ; int i = 1 ; do { denominator = 2 * i * ( 2 * i - 1 ) ; x1 = - x1 * n * n / denominator ; cosx = cosx + x1 ; i = i + 1 ; } while ( accuracy <= cosval - cosx ) ; System . out . println ( cosx ) ; } public static void main ( String [ ] args ) { float n = 30 ; cal_cos ( n ) ; } }
import java . util . * ; class GFG { static ArrayList < Integer > v = new ArrayList < Integer > ( ) ; static void multiply ( int x ) { int carry = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { int res = carry + v . get ( i ) * x ; v . set ( i , res % 10 ) ; carry = res / 10 ; } while ( carry != 0 ) { v . add ( carry % 10 ) ; carry /= 10 ; } } static int findSumOfDigits ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) multiply ( i ) ; int sum = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) sum += v . get ( i ) ; return sum ; } public static void main ( String [ ] args ) { int n = 1000 ; System . out . println ( findSumOfDigits ( n ) ) ; } }
import java . io . * ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } public static void main ( String [ ] args ) { int n = 36 ; int res = makeOdd ( n ) ; System . out . println ( res ) ; } }
class Test { static int countIterations ( int arr [ ] , int n ) { boolean oneFound = false ; int res = 0 ; for ( int i = 0 ; i < n ; ) { if ( arr [ i ] == 1 ) oneFound = true ; while ( i < n && arr [ i ] == 1 ) i ++ ; int count_zero = 0 ; while ( i < n && arr [ i ] == 0 ) { count_zero ++ ; i ++ ; } if ( oneFound == false && i == n ) return - 1 ; int curr_count ; if ( i < n && oneFound == true ) { if ( ( count_zero & 1 ) == 0 ) curr_count = count_zero / 2 ; else curr_count = ( count_zero + 1 ) / 2 ; count_zero = 0 ; } else { curr_count = count_zero ; count_zero = 0 ; } res = Math . max ( res , curr_count ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; System . out . println ( countIterations ( arr , arr . length ) ) ; } }
class GFG { static int MAX = 100 ; public static void main ( String [ ] args ) { long [ ] arr = new long [ MAX ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; System . out . print ( " Fibonacci ▁ numbers ▁ divisible ▁ by ▁ " + "their indexes are :NEW_LINE"); for ( int i = 1 ; i < MAX ; i ++ ) if ( arr [ i ] % i == 0 ) System . out . print ( i + " ▁ " ) ; } }
import java . util . * ; class GFG { static void printConsecutive ( int last , int first ) { System . out . print ( first ++ ) ; for ( int x = first ; x <= last ; x ++ ) System . out . print ( " ▁ + ▁ " + x ) ; } static void findConsecutive ( int N ) { for ( int last = 1 ; last < N ; last ++ ) { for ( int first = 0 ; first < last ; first ++ ) { if ( 2 * N == ( last - first ) * ( last + first + 1 ) ) { System . out . print ( N + " ▁ = ▁ " ) ; printConsecutive ( last , first + 1 ) ; return ; } } } System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 12 ; findConsecutive ( n ) ; } }
class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( printNthElement ( n ) ) ; } }
class MaxSum { static int maxSumLCM ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( maxSumLCM ( n ) ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { double n = 12 ; System . out . println ( Math . sqrt ( n ) ) ; } }
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 ) ; } }
import java . io . * ; class GFG { static String tidyNum ( String str1 , int len ) { char [ ] str = str1 . toCharArray ( ) ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = "11333445538" ; int len = str . length ( ) ; System . out . println ( tidyNum ( str , len ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void createMap ( Map < String , Character > um ) { um . put ( "0000" , '0' ) ; um . put ( "0001" , '1' ) ; um . put ( "0010" , '2' ) ; um . put ( "0011" , '3' ) ; um . put ( "0100" , '4' ) ; um . put ( "0101" , '5' ) ; um . put ( "0110" , '6' ) ; um . put ( "0111" , '7' ) ; um . put ( "1000" , '8' ) ; um . put ( "1001" , '9' ) ; um . put ( "1010" , ' A ' ) ; um . put ( "1011" , ' B ' ) ; um . put ( "1100" , ' C ' ) ; um . put ( "1101" , ' D ' ) ; um . put ( "1110" , ' E ' ) ; um . put ( "1111" , ' F ' ) ; } static String convertBinToHex ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 4 - len_left % 4 ) % 4 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 4 - len_right % 4 ) % 4 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_hex_map = new HashMap < String , Character > ( ) ; createMap ( bin_hex_map ) ; int i = 0 ; String hex = " " ; while ( true ) { hex += bin_hex_map . get ( bin . substring ( i , i + 4 ) ) ; i += 4 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { hex += ' . ' ; i ++ ; } } return hex ; } public static void main ( String [ ] args ) { String bin = "1111001010010100001.010110110011011" ; System . out . print ( " Hexadecimal ▁ number ▁ = ▁ " + convertBinToHex ( bin ) ) ; } }
class Main { static int findCount ( int m , int n ) { int num1 = 0 ; for ( int i = 0 ; i < m ; i ++ ) num1 = ( num1 * 10 ) + 9 ; int num2 = 0 ; for ( int i = 0 ; i < ( m - 1 ) ; i ++ ) num2 = ( num2 * 10 ) + 9 ; return ( ( num1 / n ) - ( num2 / n ) ) ; } public static void main ( String [ ] args ) { int m = 2 , n = 6 ; System . out . println ( findCount ( m , n ) ) ; } }
class GFG { static int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; boolean isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 curr % 10 == 3 curr % 10 == 5 curr % 10 == 7 curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } public static void main ( String [ ] args ) { System . out . println ( findNthEvenDigitNumber ( 2 ) ) ; System . out . println ( findNthEvenDigitNumber ( 10 ) ) ; } }
class IsDivisible { static boolean isDivisibleBy25 ( String str ) { int n = str . length ( ) ; if ( n == 1 ) return false ; return ( ( str . charAt ( n - 1 ) - '0' == 0 && str . charAt ( n - 2 ) - '0' == 0 ) || ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 25 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76955" ; if ( isDivisibleBy25 ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
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 ▁ " ) ; } }
import java . io . * ; class GFG { static int findIndex ( int n ) { if ( n <= 1 ) return n ; int a = 0 , b = 1 , c = 1 ; int res = 1 ; while ( c < n ) { c = a + b ; res ++ ; a = b ; b = c ; } return res ; } public static void main ( String [ ] args ) { int result = findIndex ( 21 ) ; System . out . println ( result ) ; } }
class GFG { static int m2 , d2 ; static boolean isLeap ( int y ) { if ( y % 100 != 0 && y % 4 == 0 y % 400 == 0 ) return true ; return false ; } static int offsetDays ( int d , int m , int y ) { int offset = d ; if ( m - 1 == 11 ) offset += 335 ; if ( m - 1 == 10 ) offset += 304 ; if ( m - 1 == 9 ) offset += 273 ; if ( m - 1 == 8 ) offset += 243 ; if ( m - 1 == 7 ) offset += 212 ; if ( m - 1 == 6 ) offset += 181 ; if ( m - 1 == 5 ) offset += 151 ; if ( m - 1 == 4 ) offset += 120 ; if ( m - 1 == 3 ) offset += 90 ; if ( m - 1 == 2 ) offset += 59 ; if ( m - 1 == 1 ) offset += 31 ; if ( isLeap ( y ) && m > 2 ) offset += 1 ; return offset ; } static void revoffsetDays ( int offset , int y ) { int [ ] month = { 0 , 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; if ( isLeap ( y ) ) month [ 2 ] = 29 ; int i ; for ( i = 1 ; i <= 12 ; i ++ ) { if ( offset <= month [ i ] ) break ; offset = offset - month [ i ] ; } d2 = offset ; m2 = i ; } static void addDays ( int d1 , int m1 , int y1 , int x ) { int offset1 = offsetDays ( d1 , m1 , y1 ) ; int remDays = isLeap ( y1 ) ? ( 366 - offset1 ) : ( 365 - offset1 ) ; int y2 , offset2 = 0 ; if ( x <= remDays ) { y2 = y1 ; offset2 = offset1 + x ; } else { x -= remDays ; y2 = y1 + 1 ; int y2days = isLeap ( y2 ) ? 366 : 365 ; while ( x >= y2days ) { x -= y2days ; y2 ++ ; y2days = isLeap ( y2 ) ? 366 : 365 ; } offset2 = x ; } revoffsetDays ( offset2 , y2 ) ; System . out . println ( " d2 ▁ = ▁ " + d2 + " , ▁ m2 ▁ = ▁ " + m2 + " , ▁ y2 ▁ = ▁ " + y2 ) ; } public static void main ( String [ ] args ) { int d = 14 , m = 3 , y = 2015 ; int x = 366 ; addDays ( d , m , y , x ) ; } }
import java . io . * ; class GFG { static int divisorSum ( int N , int K ) { int sum = 0 ; for ( int i = 2 ; i <= Math . ceil ( Math . sqrt ( N ) ) ; i ++ ) if ( N % i == 0 ) sum += ( i + N / i ) ; return sum ; } static boolean isPrime ( int n ) { if ( n == 1 n == 0 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isHyperPerfect ( int N , int K ) { int sum = divisorSum ( N , K ) ; if ( ( 1 + K * ( sum ) ) == N ) return true ; else return false ; } public static void main ( String [ ] args ) { int N1 = 1570153 , K1 = 12 ; int N2 = 321 , K2 = 3 ; if ( isHyperPerfect ( N1 , K1 ) ) System . out . println ( N1 + " ▁ is ▁ " + K1 + " - HyperPerfect " ) ; else System . out . println ( N1 + " ▁ is ▁ not ▁ " + K1 + " - HyperPerfect " ) ; if ( isHyperPerfect ( N2 , K2 ) ) System . out . println ( N2 + " ▁ is ▁ " + K2 + " - HyperPerfect " ) ; else System . out . println ( N2 + " ▁ is ▁ not ▁ " + K2 + " - HyperPerfect " ) ; } }
class Generate { static void printWellOrdered ( int number , int x , int k ) { if ( k == 0 ) { System . out . print ( number + " ▁ " ) ; return ; } for ( int i = ( x + 1 ) ; i < 10 ; i ++ ) printWellOrdered ( number * 10 + i , i , k - 1 ) ; } static void generateWellOrdered ( int k ) { printWellOrdered ( 0 , 0 , k ) ; } public static void main ( String [ ] args ) { int k = 3 ; generateWellOrdered ( k ) ; } }
class ConvertNum { static int convert ( int m , int n ) { if ( m == n ) return 0 ; if ( m > n ) return m - n ; if ( m <= 0 && n > 0 ) return - 1 ; if ( n % 2 == 1 ) return 1 + convert ( m , n + 1 ) ; else return 1 + convert ( m , n / 2 ) ; } public static void main ( String [ ] args ) { int m = 3 , n = 11 ; System . out . println ( " Minimum ▁ number ▁ of ▁ " + " operations ▁ : ▁ " + convert ( m , n ) ) ; } }
class GFG { static int count2sinRangeAtDigit ( int number , int d ) { int powerOf10 = ( int ) Math . pow ( 10 , d ) ; int nextPowerOf10 = powerOf10 * 10 ; int right = number % powerOf10 ; int roundDown = number - number % nextPowerOf10 ; int roundup = roundDown + nextPowerOf10 ; int digit = ( number / powerOf10 ) % 10 ; if ( digit < 2 ) { return roundDown / 10 ; } if ( digit == 2 ) { return roundDown / 10 + right + 1 ; } return roundup / 10 ; } static int numberOf2sinRange ( int number ) { String convert ; convert = String . valueOf ( number ) ; String s = convert ; int len = s . length ( ) ; int count = 0 ; for ( int digit = 0 ; digit < len ; digit ++ ) { count += count2sinRangeAtDigit ( number , digit ) ; } return count ; } public static void main ( String [ ] args ) { System . out . println ( numberOf2sinRange ( 22 ) ) ; System . out . println ( numberOf2sinRange ( 100 ) ) ; } }
static long factorial ( int n ) { const long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f % M ; }
static long factorial ( int n ) { long M = 1000000007 ; long f = 1 ; for ( int i = 1 ; i <= n ; i ++ ) return f ; }
static int mod ( int a , int m ) { return ( a % m + m ) % m ; }
import java . io . * ; class GFG { static int findStarNum ( int n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( findStarNum ( n ) ) ; } }
class IsDivisible { static boolean isDivisibleBy5 ( String str ) { int n = str . length ( ) ; return ( ( ( str . charAt ( n - 1 ) - '0' ) == 0 ) || ( ( str . charAt ( n - 1 ) - '0' ) == 5 ) ) ; } public static void main ( String [ ] args ) { String str = "76955" ; if ( isDivisibleBy5 ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Test { static boolean isTidy ( int num ) { int prev = 10 ; while ( num != 0 ) { int rem = num % 10 ; num /= 10 ; if ( rem > prev ) return false ; prev = rem ; } return true ; } public static void main ( String [ ] args ) { int num = 1556 ; System . out . println ( isTidy ( num ) ? " Yes " : " No " ) ; } }
import java . io . * ; class GFG { public static int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { boolean isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " " + squareFree ( n ) ) ; } }
import java . util . * ; class GFG { static int MAX_PRIME = 100000 ; static int MAX_RES = 2000000000 ; static void SieveOfEratosthenes ( Vector < Long > a ) { boolean prime [ ] = new boolean [ MAX_PRIME + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= MAX_PRIME ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX_PRIME ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= MAX_PRIME ; p ++ ) if ( prime [ p ] ) a . add ( ( long ) p ) ; } static long countSquares ( long i , long cur , long k , Vector < Long > a ) { long square = a . get ( ( int ) i ) * a . get ( ( int ) ( i ) ) ; long newCur = square * cur ; if ( newCur > k ) return 0 ; long cnt = k / ( newCur ) ; cnt += countSquares ( i + 1 , cur , k , a ) ; cnt -= countSquares ( i + 1 , newCur , k , a ) ; return cnt ; } static long squareFree ( long n ) { Vector < Long > a = new Vector < > ( ) ; SieveOfEratosthenes ( a ) ; long low = 1 ; long high = MAX_RES ; while ( low < high ) { long mid = low + ( high - low ) / 2 ; long c = mid - countSquares ( 0 , 1 , mid , a ) ; if ( c < n ) low = mid + 1 ; else high = mid ; } return low ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( squareFree ( n ) ) ; } }
import java . util . Vector ; class GFG { static long LCM ( int arr [ ] , int n ) { int max_num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max_num < arr [ i ] ) { max_num = arr [ i ] ; } } long res = 1 ; while ( x <= max_num ) { Vector < Integer > indexes = new Vector < > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % x == 0 ) { indexes . add ( indexes . size ( ) , j ) ; } } if ( indexes . size ( ) >= 2 ) { for ( int j = 0 ; j < indexes . size ( ) ; j ++ ) { arr [ indexes . get ( j ) ] = arr [ indexes . get ( j ) ] / x ; } res = res * x ; } else { x ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { res = res * arr [ i ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 10 , 20 , 35 } ; int n = arr . length ; System . out . println ( LCM ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int nondecdigits ( int n ) { int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; boolean flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } public static void main ( String [ ] args ) { int n = 200 ; System . out . println ( nondecdigits ( n ) ) ; } }
import java . util . * ; class GFG { static void nondecdigits ( String s ) { int m = s . length ( ) ; int [ ] a = new int [ m + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) a [ i ] = ( int ) s . charAt ( i ) - ( int ) '0' ; int level = m - 1 ; for ( int i = m - 1 ; i > 0 ; i -- ) { if ( a [ i ] < a [ i - 1 ] ) { a [ i - 1 ] -- ; level = i - 1 ; } } if ( a [ 0 ] != 0 ) { for ( int i = 0 ; i <= level ; i ++ ) System . out . print ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) System . out . print ( "9" ) ; } else { for ( int i = 1 ; i < level ; i ++ ) System . out . print ( a [ i ] ) ; for ( int i = level + 1 ; i < m ; i ++ ) System . out . print ( "9" ) ; } } public static void main ( String [ ] args ) { String n = "200" ; nondecdigits ( n ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int countDivisors ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) cnt ++ ; cnt = cnt + 2 ; } } return cnt ; } public static void main ( String args [ ] ) { System . out . println ( " Total ▁ distinct ▁ " + " divisors ▁ of ▁ 100 ▁ are ▁ : ▁ " + countDivisors ( 100 ) ) ; } }
import java . io . * ; class GFG { static boolean hasEvenNumberOfFactors ( int n ) { double root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) System . out . print ( " closed " + " ▁ " ) ; else System . out . print ( " open " + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printStatusOfDoors ( n ) ; } }
import java . io . * ; public class GFG { static boolean validate ( long n ) { for ( int i = 0 ; i < 10 ; i ++ ) { long temp = n ; int count = 0 ; while ( temp > 0 ) { if ( temp % 10 == i ) count ++ ; if ( count > i ) return false ; temp /= 10 ; } } return true ; } static public void main ( String [ ] args ) { long n = 1552793 ; if ( validate ( n ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
class IsDivisible { static boolean divisibleBy36 ( String num ) { int l = num . length ( ) ; if ( l == 0 ) return false ; if ( l == 1 && num . charAt ( 0 ) != '0' ) return false ; int two_digit_num = ( num . charAt ( l - 2 ) - '0' ) * 10 + ( num . charAt ( l - 1 ) - '0' ) ; if ( two_digit_num % 4 != 0 ) return false ; int sum = 0 ; for ( int i = 0 ; i < l ; i ++ ) sum += ( num . charAt ( i ) - '0' ) ; if ( sum % 9 != 0 ) return false ; return true ; } public static void main ( String [ ] args ) { String num = "92567812197966231384" ; if ( divisibleBy36 ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str . charAt ( 0 ) - '0' ) % 8 == 0 ) ; if ( n == 2 ) return ( ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 8 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; int third_last = str . charAt ( n - 3 ) - '0' ; return ( ( third_last * 100 + second_last * 10 + last ) % 8 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76952" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n / 10 ; } return count ; } static int checkPrime ( int n ) { if ( n <= 1 ) return - 1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 n % 3 == 0 ) return - 1 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return - 1 ; return 0 ; } static void printPrimePoints ( int n ) { int count = countDigits ( n ) ; if ( count == 1 count == 2 ) { System . out . print ( " - 1" ) ; return ; } boolean found = false ; for ( int i = 1 ; i < ( count - 1 ) ; i ++ ) { int left = n / ( ( int ) Math . pow ( 10 , count - i ) ) ; int right = n % ( ( int ) Math . pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { System . out . print ( i + " ▁ " ) ; found = true ; } } if ( found == false ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 2317 ; printPrimePoints ( n ) ; } }
import java . lang . Math ; public class Main { static int politness ( int n ) { int count = 0 ; for ( int i = 2 ; i <= Math . sqrt ( 2 * n ) ; i ++ ) { int a ; if ( ( 2 * n ) % i != 0 ) continue ; a = 2 * n ; a /= i ; a -= ( i - 1 ) ; if ( a % 2 != 0 ) continue ; a /= 2 ; if ( a > 0 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int n = 90 ; System . out . println ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; n = 15 ; System . out . println ( " Politness ▁ of ▁ " + n + " ▁ = ▁ " + politness ( n ) ) ; } }
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 3 == 0 ) ; } public static void main ( String [ ] args ) { String str = "1332" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . Vector ; class Test { static final int MAXN = 100001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static Vector < Integer > getFactorization ( int x ) { Vector < Integer > ret = new Vector < > ( ) ; while ( x != 1 ) { ret . add ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } public static void main ( String args [ ] ) { sieve ( ) ; int x = 12246 ; System . out . print ( " prime ▁ factorization ▁ for ▁ " + x + " ▁ : ▁ " ) ; Vector < Integer > p = getFactorization ( x ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) System . out . print ( p . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } }
import java . util . * ; class GFg { static int MAX = 10000 ; static int [ ] prodDig = new int [ MAX ] ; static int getDigitProduct ( int x ) { if ( x < 10 ) return x ; if ( prodDig [ x ] != 0 ) return prodDig [ x ] ; int prod = ( x % 10 ) * getDigitProduct ( x / 10 ) ; return ( prodDig [ x ] = prod ) ; } static void findSeed ( int n ) { List < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= n / 2 ; i ++ ) if ( i * getDigitProduct ( i ) == n ) res . add ( i ) ; if ( res . size ( ) == 0 ) { System . out . println ( " NO ▁ seed ▁ exists " ) ; return ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 138 ; findSeed ( n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MAX = 100001 ; static int INF = Integer . MAX_VALUE ; static int [ ] primedivisor = new int [ MAX ] ; static int [ ] dist = new int [ MAX ] ; static int [ ] pos = new int [ MAX ] ; static int [ ] divInd = new int [ MAX ] ; static ArrayList < ArrayList < Integer > > divisors = new ArrayList < ArrayList < Integer > > ( ) ; static void sieveOfEratosthenes ( ) { for ( int i = 2 ; i * i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { for ( int j = i * i ; j < MAX ; j += i ) { primedivisor [ j ] = i ; } } } for ( int i = 1 ; i < MAX ; ++ i ) { if ( primedivisor [ i ] == 0 ) { primedivisor [ i ] = i ; } } } static void findDivisors ( int arr [ ] , int n ) { for ( int i = 0 ; i < MAX ; ++ i ) { pos [ i ] = divInd [ i ] = - 1 ; dist [ i ] = INF ; } for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num > 1 ) { int div = primedivisor [ num ] ; divisors . get ( i ) . add ( div ) ; while ( num % div == 0 ) { num /= div ; } } } } static void nearestGCD ( int arr [ ] , int n ) { findDivisors ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int div : divisors . get ( i ) ) { if ( divInd [ div ] == - 1 ) { divInd [ div ] = i ; } else { int ind = divInd [ div ] ; divInd [ div ] = i ; if ( dist [ i ] > Math . abs ( ind - i ) ) { dist [ i ] = Math . abs ( ind - i ) ; pos [ i ] = ind + 1 ; } if ( dist [ ind ] > Math . abs ( ind - i ) ) { dist [ ind ] = Math . abs ( ind - i ) ; pos [ ind ] = i + 1 ; } } } } } public static void main ( String [ ] args ) { for ( int i = 0 ; i < MAX ; i ++ ) { divisors . add ( new ArrayList < Integer > ( ) ) ; } sieveOfEratosthenes ( ) ; int arr [ ] = { 2 , 9 , 4 , 3 , 13 } ; int n = arr . length ; nearestGCD ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( pos [ i ] + " ▁ " ) ; } } }
import java . util . Arrays ; public class GFG { static int maxPrimefactorNum ( int N ) { int arr [ ] = new int [ N + 5 ] ; Arrays . fill ( arr , 0 ) ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) { for ( int j = 2 * i ; j <= N ; j += i ) { arr [ j ] ++ ; } } arr [ i ] = 1 ; } int maxval = 0 , maxint = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > maxval ) { maxval = arr [ i ] ; maxint = i ; } } return maxint ; } public static void main ( String [ ] args ) { int N = 40 ; System . out . println ( maxPrimefactorNum ( N ) ) ; } }
import java . util . * ; class GFG { static int MAX = 10000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void sieveSundaram ( ) { boolean [ ] marked = new boolean [ MAX / 2 + 100 ] ; for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static void findPrimes ( int n ) { if ( n <= 2 n % 2 != 0 ) { System . out . println ( " Invalid ▁ Input ▁ " ) ; return ; } for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { int diff = n - primes . get ( i ) ; if ( primes . contains ( diff ) ) { System . out . println ( primes . get ( i ) + " ▁ + ▁ " + diff + " ▁ = ▁ " + n ) ; return ; } } } public static void main ( String [ ] args ) { sieveSundaram ( ) ; findPrimes ( 4 ) ; findPrimes ( 38 ) ; findPrimes ( 100 ) ; } }
import java . util . Arrays ; class GFG { static int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; int maxele = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; ; for ( int i = 2 ; i <= maxele ; ++ i ) { int count = 0 ; for ( int j = 0 ; j < n ; ++ j ) { if ( arr [ j ] % i == 0 ) ++ count ; } ans = Math . max ( ans , count ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 2 , 5 , 4 } ; int size = arr . length ; System . out . println ( largestGCDSubsequence ( arr , size ) ) ; } }
import java . util . * ; class GFG { static class data { int val ; int steps ; public data ( int val , int steps ) { this . val = val ; this . steps = steps ; } } ; static int minStepToReachOne ( int N ) { Queue < data > q = new LinkedList < > ( ) ; q . add ( new data ( N , 0 ) ) ; HashSet < Integer > st = new HashSet < Integer > ( ) ; while ( ! q . isEmpty ( ) ) { data t = q . peek ( ) ; q . remove ( ) ; if ( t . val == 1 ) return t . steps ; if ( ! st . contains ( t . val - 1 ) ) { q . add ( new data ( t . val - 1 , t . steps + 1 ) ) ; st . add ( t . val - 1 ) ; } for ( int i = 2 ; i * i <= t . val ; i ++ ) { if ( t . val % i == 0 && ! st . contains ( t . val / i ) ) { q . add ( new data ( t . val / i , t . steps + 1 ) ) ; st . add ( t . val / i ) ; } } } return - 1 ; } public static void main ( String [ ] args ) { int N = 17 ; System . out . print ( minStepToReachOne ( N ) + "NEW_LINE"); } }
class GFG { static final int MAX = 1000006 ; static void sieve ( int count [ ] ) { for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( count [ i ] == 0 ) { for ( int j = 2 * i ; j < MAX ; j += i ) count [ j ] ++ ; count [ i ] = 1 ; } } } static int query ( int count [ ] , int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) sum += count [ i ] ; return sum ; } public static void main ( String [ ] args ) { int count [ ] = new int [ MAX ] ; sieve ( count ) ; System . out . println ( query ( count , 6 , 10 ) + " ▁ " + query ( count , 1 , 5 ) ) ; } }
class GFG { public static long SubArraySum ( int arr [ ] , int n ) { long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( " Sum ▁ of ▁ SubArray ▁ " + SubArraySum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void generateNumbers ( int factor [ ] , int n , int k ) { int next [ ] = new int [ k ] ; for ( int i = 0 ; i < n ; ) { int toincrement = 0 ; for ( int j = 0 ; j < k ; j ++ ) if ( next [ j ] < next [ toincrement ] ) toincrement = j ; if ( output != next [ toincrement ] ) { output = next [ toincrement ] ; System . out . print ( next [ toincrement ] + " ▁ " ) ; i ++ ; } next [ toincrement ] += factor [ toincrement ] ; } } public static void main ( String [ ] args ) { int factor [ ] = { 3 , 5 , 7 } ; int n = 10 ; int k = factor . length ; generateNumbers ( factor , n , k ) ; } }
import java . io . * ; class GFG { static void checkReversible ( int n ) { int rev = 0 , rem = 0 ; int flag = n ; while ( flag > 0 ) { rem = flag % 10 ; rev *= 10 ; rev += rem ; flag /= 10 ; } int sum = rev + n ; while ( sum > 0 && ( rem % 2 != 0 ) ) { rem = sum % 10 ; sum /= 10 ; } if ( sum == 0 ) System . out . println ( " Reversible ▁ Number " ) ; else System . out . println ( " Non - Reversible ▁ Number " ) ; } public static void main ( String [ ] args ) { int n = 36 ; checkReversible ( n ) ; } }
import java . io . * ; class GFG { static void countReversible ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { switch ( i % 4 ) { case 0 : case 2 : count += 20 * Math . pow ( 30 , ( i / 2 - 1 ) ) ; break ; case 3 : count += 100 * Math . pow ( 500 , i / 4 ) ; break ; case 1 : break ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int n = 9 ; countReversible ( n ) ; } }
import java . io . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } public static void main ( String args [ ] ) { int A = 4 , N = 7 ; System . out . println ( multiplicativeOrder ( A , N ) ) ; } }
import java . io . * ; class GFG { static int sumOfRange ( int a , int b ) { int i = ( a * ( a + 1 ) ) >> 1 ; int j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } static int sumofproduct ( int n ) { int sum = 0 ; int root = ( int ) Math . sqrt ( n ) ; for ( int i = 1 ; i <= root ; i ++ ) { int up = n / i ; int low = Math . max ( n / ( i + 1 ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * ( n / i ) ) ; } return sum ; } static public void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumofproduct ( n ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int power ( int x , int y , int p ) { while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % p ; } x = ( x * x ) % p ; } return res ; } static void findPrimefactors ( HashSet < Integer > s , int n ) { while ( n % 2 == 0 ) { s . add ( 2 ) ; n = n / 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { s . add ( i ) ; n = n / i ; } } if ( n > 2 ) { s . add ( n ) ; } } static int findPrimitive ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; if ( isPrime ( n ) == false ) { return - 1 ; } int phi = n - 1 ; findPrimefactors ( s , phi ) ; for ( int r = 2 ; r <= phi ; r ++ ) { boolean flag = false ; for ( Integer a : s ) { if ( power ( r , phi / ( a ) , n ) == 1 ) { flag = true ; break ; } } if ( flag == false ) { return r ; } } return - 1 ; } public static void main ( String [ ] args ) { int n = 761 ; System . out . println ( " ▁ Smallest ▁ primitive ▁ root ▁ of ▁ " + n + " ▁ is ▁ " + findPrimitive ( n ) ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; return - 1 ; } public static void main ( String args [ ] ) { int n = 12 , k = 3 ; System . out . println ( kPrimeFactor ( n , k ) ) ; n = 14 ; k = 3 ; System . out . println ( kPrimeFactor ( n , k ) ) ; } }
class GFG { static int MAX = 10001 ; static void sieveOfEratosthenes ( int [ ] s ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= MAX ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= MAX ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int kPrimeFactor ( int n , int k , int [ ] s ) { while ( n > 1 ) { if ( k == 1 ) return s [ n ] ; k -- ; n /= s [ n ] ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] s = new int [ MAX + 1 ] ; sieveOfEratosthenes ( s ) ; int n = 12 , k = 3 ; System . out . println ( kPrimeFactor ( n , k , s ) ) ; n = 14 ; k = 3 ; System . out . println ( kPrimeFactor ( n , k , s ) ) ; } }
class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 3 ; System . out . println ( minPower ( n , x ) ) ; } }
import java . lang . * ; class GFG { static int calSum ( int n ) { int a = 3 , b = 0 , c = 2 ; return 3 ; return 3 ; return 5 ; int sum = 5 ; while ( n > 2 ) { int d = a + b ; sum += d ; a = b ; b = c ; c = d ; n -- ; } return sum ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . print ( calSum ( n ) ) ; } }
import java . lang . * ; class GFG { static int findKHCF ( int x , int y , int k ) { int small = Math . min ( x , y ) ; int count = 1 ; for ( int i = 2 ; i <= small ; i ++ ) { if ( x % i == 0 && y % i == 0 ) count ++ ; if ( count == k ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int x = 4 , y = 24 , k = 3 ; System . out . print ( findKHCF ( x , y , k ) ) ; } }
import java . util . * ; class GFG { static boolean isToOneRec ( int n , HashSet < Integer > s ) { if ( n == 1 ) { return true ; } if ( s . contains ( n ) ) { return false ; } return ( n % 2 == 1 ) ? isToOneRec ( 3 * n + 1 , s ) : isToOneRec ( n / 2 , s ) ; } static boolean isToOne ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; return isToOneRec ( n , s ) ; } public static void main ( String [ ] args ) { int n = 5 ; if ( isToOne ( n ) ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " ) ; } } }
class GFG { static boolean isToOne ( int n ) { return ( n > 0 ) ; } public static void main ( String [ ] args ) { int n = 5 ; if ( isToOne ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void findgcd ( int n , int x , int y ) { int g = gcd ( x , y ) ; for ( int i = 0 ; i < g ; i ++ ) System . out . print ( n ) ; } public static void main ( String [ ] args ) { int n = 123 , x = 5 , y = 2 ; findgcd ( n , x , y ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } static int firstFactorialDivisibleNumber ( int x ) { int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } static int countFactorialXNotY ( int x , int y ) { return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) ; } public static void main ( String [ ] args ) { int x = 15 , y = 25 ; System . out . print ( countFactorialXNotY ( x , y ) ) ; } }
class GFG { static int firstFactorialDivisibleNumber ( int x ) { int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } public static void main ( String [ ] args ) { int x = 16 ; System . out . print ( firstFactorialDivisibleNumber ( x ) ) ; } }
class GFG { static boolean SieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isPrime [ i ] = false ; } } return false ; } static void findPrimePair ( int n ) { boolean isPrime [ ] = new boolean [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime [ i ] && isPrime [ n - i ] ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; return ; } } } public static void main ( String [ ] args ) { int n = 74 ; findPrimePair ( n ) ; } }
class GFG { static int highestPowerof2 ( int n ) { int res = 0 ; for ( int i = n ; i >= 1 ; i -- ) { if ( ( i & ( i - 1 ) ) == 0 ) { res = i ; break ; } } return res ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( highestPowerof2 ( n ) ) ; } }
import java . util . * ; class Digits { public static int getFirstDigit ( int x ) { while ( x >= 10 ) x /= 10 ; return x ; } public static int getCountWithSameStartAndEndFrom1 ( int x ) { if ( x < 10 ) return x ; int tens = x / 10 ; int res = tens + 9 ; int firstDigit = getFirstDigit ( x ) ; int lastDigit = x % 10 ; if ( lastDigit < firstDigit ) res -- ; return res ; } public static int getCountWithSameStartAndEnd ( int start , int end ) { return getCountWithSameStartAndEndFrom1 ( end ) - getCountWithSameStartAndEndFrom1 ( start - 1 ) ; } public static void main ( String [ ] args ) { int start = 5 , end = 40 ; System . out . print ( getCountWithSameStartAndEnd ( start , end ) ) ; } }
import java . io . * ; class GFG { static void sieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static boolean rightTruPrime ( int n ) { boolean isPrime [ ] = new boolean [ n + 1 ] ; sieveOfEratosthenes ( n , isPrime ) ; while ( n != 0 ) { if ( isPrime [ n ] ) n = n / 10 ; else return false ; } return true ; } public static void main ( String args [ ] ) { int n = 59399 ; if ( rightTruPrime ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int findSum ( int N , int K ) { int ans = 0 ; int y = N / K ; int x = N % K ; ans = ( K * ( K - 1 ) / 2 ) * y + ( x * ( x + 1 ) ) / 2 ; return ans ; } static public void main ( String [ ] args ) { int N = 10 , K = 2 ; System . out . println ( findSum ( N , K ) ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int findnum ( String str ) { int n = str . length ( ) ; int count_after_dot = 0 ; boolean dot_seen = false ; int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) != ' . ' ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( dot_seen == true ) count_after_dot ++ ; } else dot_seen = true ; } if ( dot_seen == false ) return 1 ; int dem = ( int ) Math . pow ( 10 , count_after_dot ) ; return ( dem / gcd ( num , dem ) ) ; } public static void main ( String [ ] args ) { String str = "5.125" ; System . out . print ( findnum ( str ) ) ; } }
class GFG { static int maxHandshake ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( maxHandshake ( n ) ) ; } }
import java . util . * ; class solution { static boolean divisible ( String N , int digit ) { int ans = 0 ; for ( int i = 0 ; i < N . length ( ) ; i ++ ) { ans = ( ans * 10 + ( N . charAt ( i ) - '0' ) ) ; ans %= digit ; } return ( ans == 0 ) ; } static int allDigits ( String N ) { Boolean [ ] divide = new Boolean [ 10 ] ; Arrays . fill ( divide , Boolean . FALSE ) ; for ( int digit = 2 ; digit <= 9 ; digit ++ ) { if ( divisible ( N , digit ) ) divide [ digit ] = true ; } int result = 0 ; for ( int i = 0 ; i < N . length ( ) ; i ++ ) { if ( divide [ N . charAt ( i ) - '0' ] == true ) result ++ ; } return result ; } public static void main ( String args [ ] ) { String N = "122324" ; System . out . println ( allDigits ( N ) ) ; } }
import java . util . * ; class GFG { static int getSum ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static void printAliquot ( int n ) { System . out . printf ( " % d ▁ " , n ) ; TreeSet < Integer > s = new TreeSet < > ( ) ; s . add ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . contains ( n ) && n != s . last ( ) ) { System . out . print ( " Repeats with " + n); break ; } System . out . print ( n + " ▁ " ) ; s . add ( n ) ; } } public static void main ( String [ ] args ) { printAliquot ( 12 ) ; } }
import java . util . * ; class GFG { static int MAX = 10000 ; static ArrayList < Object > primes = new ArrayList < Object > ( ) ; static void sieveSundaram ( ) { boolean [ ] marked = new boolean [ MAX / 2 + 1 ] ; Arrays . fill ( marked , false ) ; for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static int LSCPUtil ( int limit , long [ ] sum_prime ) { int max_length = - 1 ; int prime_number = - 1 ; for ( int i = 0 ; ( int ) primes . get ( i ) <= limit ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( sum_prime [ i ] - sum_prime [ j ] > limit ) break ; long consSum = sum_prime [ i ] - sum_prime [ j ] ; Object [ ] prime = primes . toArray ( ) ; if ( Arrays . binarySearch ( prime , ( int ) consSum ) >= 0 ) { if ( max_length < i - j + 1 ) { max_length = i - j + 1 ; prime_number = ( int ) consSum ; } } } } return prime_number ; } static void LSCP ( int [ ] arr , int n ) { sieveSundaram ( ) ; long [ ] sum_prime = new long [ primes . size ( ) + 1 ] ; sum_prime [ 0 ] = 0 ; for ( int i = 1 ; i <= primes . size ( ) ; i ++ ) sum_prime [ i ] = ( int ) primes . get ( i - 1 ) + sum_prime [ i - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( LSCPUtil ( arr [ i ] , sum_prime ) + " ▁ " ) ; } public static void main ( String [ ] arg ) { int [ ] arr = { 10 , 30 , 40 , 50 , 1000 } ; int n = arr . length ; LSCP ( arr , n ) ; } }
class gfg { static int countNums ( int n , int x , int y ) { boolean [ ] arr = new boolean [ n + 1 ] ; if ( x <= n ) arr [ x ] = true ; if ( y <= n ) arr [ y ] = true ; int result = 0 ; for ( int i = Math . min ( x , y ) ; i <= n ; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true ; if ( i + y <= n ) arr [ i + y ] = true ; result ++ ; } } return result ; } public static void main ( String [ ] args ) { int n = 15 , x = 5 , y = 7 ; System . out . println ( countNums ( n , x , y ) ) ; } }
import java . util . Arrays ; class GFG { static int reverse ( int x ) { int rev = 0 ; while ( x > 0 ) { rev = ( rev * 10 ) + x % 10 ; x = x / 10 ; } return rev ; } static void printEmirp ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] ) { int rev = reverse ( p ) ; if ( p != rev && rev <= n && prime [ rev ] ) { System . out . print ( p + " ▁ " + rev + " ▁ " ) ; prime [ rev ] = false ; } } } } public static void main ( String [ ] args ) { int n = 100 ; printEmirp ( n ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum = sum + i ; { sum = sum + i ; sum = sum + ( n / i ) ; } } } sum = sum - n ; return sum ; } static boolean checkAbundant ( int n ) { return ( getSum ( n ) > n ) ; } public static void main ( String args [ ] ) throws IOException { if ( checkAbundant ( 12 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; if ( checkAbundant ( 15 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } public static void main ( String [ ] args ) { if ( isPowerful ( 20 ) ) System . out . print ( "YESNEW_LINE"); else System . out . print ( "NONEW_LINE"); if ( isPowerful ( 27 ) ) System . out . print ( "YESNEW_LINE"); else System . out . print ( "NONEW_LINE"); } }
import java . io . * ; class GFG { static int divisorsSum ( int n ) { for ( int i = 1 ; i <= ( Math . sqrt ( n ) ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum ; } static boolean isDeficient ( int n ) { return ( divisorsSum ( n ) < ( 2 * n ) ) ; } public static void main ( String args [ ] ) { if ( isDeficient ( 12 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; if ( isDeficient ( 15 ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . Vector ; class Test { static int MAX = 10000 ; static Vector < Integer > primes = new Vector < > ( ) ; static void sieveSundaram ( ) { boolean marked [ ] = new boolean [ MAX / 2 + 100 ] ; for ( int i = 1 ; i <= ( Math . sqrt ( MAX ) - 1 ) / 2 ; i ++ ) for ( int j = ( i * ( i + 1 ) ) << 1 ; j <= MAX / 2 ; j = j + 2 * i + 1 ) marked [ j ] = true ; primes . addElement ( 2 ) ; for ( int i = 1 ; i <= MAX / 2 ; i ++ ) if ( marked [ i ] == false ) primes . addElement ( 2 * i + 1 ) ; } static boolean isSmith ( int n ) { int original_no = n ; int pDigitSum = 0 ; for ( int i = 0 ; primes . get ( i ) <= n / 2 ; i ++ ) { while ( n % primes . get ( i ) == 0 ) { int p = primes . get ( i ) ; n = n / p ; while ( p > 0 ) { pDigitSum += ( p % 10 ) ; p = p / 10 ; } } } if ( n != 1 && n != original_no ) { while ( n > 0 ) { pDigitSum = pDigitSum + n % 10 ; n = n / 10 ; } } int sumDigits = 0 ; while ( original_no > 0 ) { sumDigits = sumDigits + original_no % 10 ; original_no = original_no / 10 ; } return ( pDigitSum == sumDigits ) ; } public static void main ( String [ ] args ) { sieveSundaram ( ) ; System . out . println ( " Printing ▁ first ▁ few ▁ Smith ▁ Numbers " + " ▁ using ▁ isSmith ( ) " ) ; for ( int i = 1 ; i < 500 ; i ++ ) if ( isSmith ( i ) ) System . out . print ( i + " ▁ " ) ; } }
class GFG { static boolean iskaprekar ( int n ) { if ( n == 1 ) return true ; int sq_n = n * n ; int count_digits = 0 ; while ( sq_n != 0 ) { count_digits ++ ; sq_n /= 10 ; } for ( int r_digits = 1 ; r_digits < count_digits ; r_digits ++ ) { int eq_parts = ( int ) Math . pow ( 10 , r_digits ) ; if ( eq_parts == n ) continue ; int sum = sq_n / eq_parts + sq_n % eq_parts ; if ( sum == n ) return true ; } return false ; } public static void main ( String [ ] args ) { System . out . println ( " Printing ▁ first ▁ few ▁ Kaprekar ▁ Numbers " + " ▁ using ▁ iskaprekar ( ) " ) ; for ( int i = 1 ; i < 10000 ; i ++ ) if ( iskaprekar ( i ) ) System . out . print ( i + " ▁ " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isKeith ( int x ) { ArrayList < Integer > terms = new ArrayList < Integer > ( ) ; while ( temp > 0 ) { terms . add ( temp % 10 ) ; temp = temp / 10 ; n ++ ; } Collections . reverse ( terms ) ; int next_term = 0 , i = n ; while ( next_term < x ) { next_term = 0 ; for ( int j = 1 ; j <= n ; j ++ ) next_term += terms . get ( i - j ) ; terms . add ( next_term ) ; i ++ ; } return ( next_term == x ) ; } public static void main ( String [ ] args ) { if ( isKeith ( 14 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isKeith ( 12 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isKeith ( 197 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Test { static boolean canBeSumofConsec ( int n ) { return ( ( ( n & ( n - 1 ) ) != 0 ) && n != 0 ) ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( canBeSumofConsec ( n ) ? " true " : " false " ) ; } }
import java . util . * ; class GFG { static int canBeSumofConsec ( int n ) { n = 2 * n ; return ( ( n & ( n - 1 ) ) != 0 ) ? 1 : 0 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( canBeSumofConsec ( n ) + "NEW_LINE"); } }
import java . io . * ; class GFG { static int prime ( int n ) { if ( n % 2 != 0 ) n -= 2 ; else n -- ; int i , j ; for ( i = n ; i >= 2 ; i -= 2 ) { if ( i % 2 == 0 ) continue ; for ( j = 3 ; j <= Math . sqrt ( i ) ; j += 2 ) { if ( i % j == 0 ) break ; } if ( j > Math . sqrt ( i ) ) return i ; } return 2 ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . print ( prime ( n ) ) ; } }
import java . io . * ; class GFG { static int evenFibSum ( int limit ) { if ( limit < 2 ) return 0 ; long ef1 = 0 , ef2 = 2 ; long sum = ef1 + ef2 ; while ( ef2 <= limit ) { long ef3 = 4 * ef2 + ef1 ; if ( ef3 > limit ) break ; ef1 = ef2 ; ef2 = ef3 ; sum += ef2 ; } return ( int ) sum ; } public static void main ( String [ ] args ) { int limit = 400 ; System . out . println ( evenFibSum ( limit ) ) ; } }
class GFG { static long evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( evenFib ( n ) ) ; } }
import java . util . * ; class GFG { static int maxn = 10005 ; static int INF = 999999 ; static int [ ] smallest_prime = new int [ maxn ] ; static int [ ] divisors = new int [ maxn ] ; static int [ ] segmentTree = new int [ 4 * maxn ] ; static void findSmallestPrimeFactors ( ) { for ( int i = 0 ; i < maxn ; i ++ ) smallest_prime [ i ] = INF ; for ( int i = 2 ; i < maxn ; i ++ ) { if ( smallest_prime [ i ] == INF ) { smallest_prime [ i ] = i ; for ( int j = i * i ; j < maxn ; j += i ) if ( smallest_prime [ j ] > i ) smallest_prime [ j ] = i ; } } } static void buildDivisorsArray ( ) { for ( int i = 1 ; i < maxn ; i ++ ) { divisors [ i ] = 1 ; int n = i , p = smallest_prime [ i ] , k = 0 ; while ( n > 1 ) { n = n / p ; k ++ ; if ( smallest_prime [ n ] != p ) { divisors [ i ] = divisors [ i ] * ( k + 1 ) ; k = 0 ; } p = smallest_prime [ n ] ; } } } static void buildSegtmentTree ( int node , int a , int b ) { if ( a == b ) { segmentTree [ node ] = divisors [ a ] ; return ; } buildSegtmentTree ( 2 * node , a , ( a + b ) / 2 ) ; buildSegtmentTree ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b ) ; segmentTree [ node ] = Math . max ( segmentTree [ 2 * node ] , segmentTree [ 2 * node + 1 ] ) ; } static int query ( int node , int a , int b , int l , int r ) { if ( l > b a > r ) return - 1 ; if ( a >= l && b <= r ) return segmentTree [ node ] ; return Math . max ( query ( 2 * node , a , ( a + b ) / 2 , l , r ) , query ( 2 * node + 1 , ( ( a + b ) / 2 ) + 1 , b , l , r ) ) ; } public static void main ( String [ ] args ) { findSmallestPrimeFactors ( ) ; buildDivisorsArray ( ) ; buildSegtmentTree ( 1 , 1 , maxn - 1 ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 1 , ▁ 100 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 1 , 100 ) ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 10 , ▁ 48 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 10 , 48 ) ) ; System . out . println ( " Maximum ▁ divisors ▁ that ▁ a ▁ number ▁ " + " has ▁ in ▁ [ 1 , ▁ 10 ] ▁ are ▁ " + query ( 1 , 1 , maxn - 1 , 1 , 10 ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int MAX = 3000 ; public static int smartNumber ( int n ) { Integer [ ] primes = new Integer [ MAX ] ; Arrays . fill ( primes , new Integer ( 0 ) ) ; Vector < Integer > result = new Vector < > ( ) ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( primes [ i ] == 0 ) { primes [ i ] = 1 ; for ( int j = i * 2 ; j < MAX ; j = j + i ) { primes [ j ] -= 1 ; if ( ( primes [ j ] + 3 ) == 0 ) result . add ( j ) ; } } } Collections . sort ( result ) ; return result . get ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( smartNumber ( n ) ) ; } }
import java . io . * ; class GFG { static int countSteps ( int x , int y ) { if ( x % y == 0 ) return x / y ; return x / y + countSteps ( y , x % y ) ; } public static void main ( String [ ] args ) { int x = 100 , y = 19 ; System . out . println ( countSteps ( x , y ) ) ; } }
import java . util . * ; import java . io . * ; class GFG { static HashMap < Integer , Integer > ma = new HashMap < > ( ) ; static void primeFactorize ( int a ) { for ( int i = 2 ; i * i <= a ; i += 2 ) { int cnt = 0 ; while ( a % i == 0 ) { cnt ++ ; a /= i ; } ma . put ( i , cnt ) ; } if ( a > 1 ) ma . put ( a , 1 ) ; } static int commDiv ( int a , int b ) { primeFactorize ( a ) ; int res = 1 ; for ( Map . Entry < Integer , Integer > m : ma . entrySet ( ) ) { int cnt = 0 ; int key = m . getKey ( ) ; int value = m . getValue ( ) ; while ( b % key == 0 ) { b /= key ; cnt ++ ; } res *= ( Math . min ( cnt , value ) + 1 ) ; } return res ; } public static void main ( String args [ ] ) { int a = 12 , b = 24 ; System . out . println ( commDiv ( a , b ) ) ; } }
static int isHappyNumber ( int n ) { HashSet < Integer > st = new HashSet < > ( ) ; while ( 1 ) { n = numSquareSum ( n ) ; if ( n == 1 ) return true ; if ( st . contains ( n ) ) return false ; st . add ( n ) ; } }
class GFG { static float sqroot ( float s ) { int pSq = 0 ; int N = 0 ; for ( int i = ( int ) ( s ) ; i > 0 ; i -- ) { for ( int j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } float d = s - pSq ; float P = d / ( 2.0f * N ) ; float A = N + P ; float sqrt_of_s = A - ( ( P * P ) / ( 2.0f * A ) ) ; return sqrt_of_s ; } public static void main ( String [ ] args ) { float num = 9.2345f ; float sqroot_of_num = sqroot ( num ) ; System . out . print ( " Square ▁ root ▁ of ▁ " + num + " ▁ = ▁ " + Math . round ( sqroot_of_num * 100000.0 ) / 100000.0 ) ; } }
class GFG { static int power ( int x , int a ) { int res = 1 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) res = res * x ; x = x * x ; a >>= 1 ; } return res ; } static int breakInteger ( int N ) { if ( N == 2 ) return 1 ; if ( N == 3 ) return 2 ; int maxProduct = - 1 ; switch ( N % 3 ) { case 0 : maxProduct = power ( 3 , N / 3 ) ; break ; case 1 : maxProduct = 2 * 2 * power ( 3 , ( N / 3 ) - 1 ) ; break ; case 2 : maxProduct = 2 * power ( 3 , N / 3 ) ; break ; } return maxProduct ; } public static void main ( String [ ] args ) { int maxProduct = breakInteger ( 10 ) ; System . out . println ( maxProduct ) ; } }
import java . util . * ; public class GfG { static int digSum ( int n ) { int sum = 0 ; while ( n > 0 sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return sum ; } public static void main ( String argc [ ] ) { int n = 1234 ; System . out . println ( digSum ( n ) ) ; } }
import java . io . * ; class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } public static void main ( String [ ] args ) { int n = 9999 ; System . out . println ( digSum ( n ) ) ; } }
import java . io . * ; class GFG { static int countMultiples ( int n ) { return n / 3 + n / 7 - n / 21 ; } public static void main ( String args [ ] ) { System . out . println ( " Count ▁ = ▁ " + countMultiples ( 25 ) ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int Modulo ( int a , char b [ ] ) { int mod = 0 ; for ( int i = 0 ; i < b . length ; i ++ ) mod = ( mod * 10 + b [ i ] - '0' ) % a ; } static int LastDigit ( char a [ ] , char b [ ] ) { int len_a = a . length , len_b = b . length ; if ( len_a == 1 && len_b == 1 && b [ 0 ] == '0' && a [ 0 ] == '0' ) return 1 ; if ( len_b == 1 && b [ 0 ] == '0' ) return 1 ; if ( len_a == 1 && a [ 0 ] == '0' ) return 0 ; int exp = ( Modulo ( 4 , b ) == 0 ) ? 4 : Modulo ( 4 , b ) ; int res = ( int ) ( Math . pow ( a [ len_a - 1 ] - '0' , exp ) ) ; return res % 10 ; } public static void main ( String args [ ] ) throws IOException { char a [ ] = "117" . toCharArray ( ) , b [ ] = { '3' } ; System . out . println ( LastDigit ( a , b ) ) ; } }
public class ReverseAdd { long reversDigits ( long num ) { long rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } boolean isPalindrome ( long num ) { return ( reversDigits ( num ) == num ) ; } void ReverseandAdd ( long num ) { long rev_num = 0 ; while ( num <= 4294967295l ) { rev_num = reversDigits ( num ) ; num = num + rev_num ; if ( isPalindrome ( num ) ) { System . out . println ( num ) ; break ; } else if ( num > 4294967295l ) { System . out . println ( " No ▁ palindrome ▁ exist " ) ; } } } public static void main ( String [ ] args ) { ReverseAdd ob = new ReverseAdd ( ) ; ob . ReverseandAdd ( 195l ) ; ob . ReverseandAdd ( 265l ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; { return gcd ( a >> 1 , b ) ; return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ( ~ b & 1 ) == 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; return gcd ( ( b - a ) >> 1 , a ) ; } public static void main ( String args [ ] ) { int a = 34 , b = 17 ; System . out . println ( " Gcd ▁ of ▁ given " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }
class GFG { static int sum [ ] = new int [ 1000005 ] ; static int toInt ( char x ) { return x - '0' ; } static void prepareSum ( String s ) { sum [ 0 ] = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum [ i + 1 ] = sum [ i ] + toInt ( s . charAt ( i ) ) ; } } static void query ( int l , int r ) { if ( ( sum [ r + 1 ] - sum [ l ] ) % 3 == 0 ) { System . out . println ( " Divisible ▁ by ▁ 3" ) ; } else { System . out . println ( " Not ▁ divisible ▁ by ▁ 3" ) ; } } public static void main ( String [ ] args ) { String n = "12468236544" ; prepareSum ( n ) ; query ( 0 , 1 ) ; query ( 1 , 2 ) ; query ( 3 , 6 ) ; query ( 0 , 10 ) ; } }
import java . io . * ; class GFG { static void findNDigitNumsUtil ( int n , int sum , char out [ ] , int index ) { if ( index > n sum < 0 ) return ; if ( index == n ) { if ( sum == 0 ) { out [ index ] = ' \0' ; System . out . print ( out ) ; System . out . print ( " ▁ " ) ; } return ; } for ( int i = 0 ; i <= 9 ; i ++ ) { out [ index ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , out , index + 1 ) ; } } static void findNDigitNums ( int n , int sum ) { char [ ] out = new char [ n + 1 ] ; for ( int i = 1 ; i <= 9 ; i ++ ) { out [ 0 ] = ( char ) ( i + '0' ) ; findNDigitNumsUtil ( n , sum - i , out , 1 ) ; } } public static void main ( String [ ] args ) { int n = 2 , sum = 3 ; findNDigitNums ( n , sum ) ; } }
import java . util . * ; class GFG { static boolean oneDigit ( int num ) { return ( num >= 0 && num < 10 ) ; } static boolean isPalUtil ( int num , int dupNum ) { if ( oneDigit ( num ) ) return ( num == ( dupNum ) % 10 ) ; if ( ! isPalUtil ( num / 10 , dupNum ) ) return false ; dupNum /= 10 ; return ( num % 10 == ( dupNum ) % 10 ) ; } static boolean isPal ( int num ) { if ( num < 0 ) num = - num ; return isPalUtil ( num , dupNum ) ; } static void printPalPrimesLessThanN ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) { prime [ i ] = false ; } } } for ( int p = 2 ; p <= n ; p ++ ) { if ( prime [ p ] && isPal ( p ) ) { System . out . print ( p + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int n = 100 ; System . out . printf ( " Palindromic ▁ primes ▁ smaller ▁ than ▁ or ▁ " + "equal to %d are :NEW_LINE", n); printPalPrimesLessThanN ( n ) ; } }
import java . io . * ; class GFG { static int countPrimeFactors ( int n ) { int count = 0 ; while ( n % 2 == 0 ) { n = n / 2 ; count ++ ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; count ++ ; } } if ( n > 2 ) count ++ ; return ( count ) ; } static void printKAlmostPrimes ( int k , int n ) { for ( int i = 1 , num = 2 ; i <= n ; num ++ ) { if ( countPrimeFactors ( num ) == k ) { System . out . print ( num + " ▁ " ) ; i ++ ; } } return ; } public static void main ( String [ ] args ) { int n = 10 , k = 2 ; System . out . println ( " First ▁ " + n + " ▁ " + k + " - almost ▁ prime ▁ numbers ▁ : ▁ " ) ; printKAlmostPrimes ( k , n ) ; } }
class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void lowest ( int den3 , int num3 ) { int common_factor = gcd ( num3 , den3 ) ; den3 = den3 / common_factor ; num3 = num3 / common_factor ; System . out . println ( num3 + " / " + den3 ) ; } static void addFraction ( int num1 , int den1 , int num2 , int den2 ) { int den3 = gcd ( den1 , den2 ) ; den3 = ( den1 * den2 ) / den3 ; int num3 = ( num1 ) * ( den3 / den1 ) + ( num2 ) * ( den3 / den2 ) ; lowest ( den3 , num3 ) ; } public static void main ( String [ ] args ) { int num1 = 1 , den1 = 500 , num2 = 2 , den2 = 1500 ; System . out . print ( num1 + " / " + den1 + " ▁ + ▁ " + num2 + " / " + den2 + " ▁ is ▁ equal ▁ to ▁ " ) ; addFraction ( num1 , den1 , num2 , den2 ) ; } }
import java . io . * ; class GFG { static int findPieces ( int n ) { return ( n * ( n + 1 ) ) / 2 + 1 ; } public static void main ( String [ ] args ) { System . out . println ( findPieces ( 1 ) ) ; System . out . println ( findPieces ( 2 ) ) ; System . out . println ( findPieces ( 3 ) ) ; System . out . println ( findPieces ( 50 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static double M_E = 2.71828182845904523536 ; public static double M_PI = 3.141592654 ; static long findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ; return ( long ) Math . floor ( x ) + 1 ; } public static void main ( String [ ] args ) { System . out . println ( findDigits ( 1 ) ) ; System . out . println ( findDigits ( 50000000 ) ) ; System . out . println ( findDigits ( 1000000000 ) ) ; System . out . println ( findDigits ( 120 ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += Math . log10 ( i ) ; return ( int ) ( Math . floor ( digits ) ) + 1 ; } public static void main ( String [ ] args ) { System . out . println ( findDigits ( 1 ) ) ; System . out . println ( findDigits ( 5 ) ) ; System . out . println ( findDigits ( 10 ) ) ; System . out . println ( findDigits ( 120 ) ) ; } }
import java . io . * ; class GFG { static void findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; int cube [ ] = new int [ cubeRoot + 1 ] ; for ( int i = 1 ; i <= cubeRoot ; i ++ ) cube [ i ] = i * i * i ; int l = 1 ; int r = cubeRoot ; while ( l < r ) { if ( cube [ l ] + cube [ r ] < n ) l ++ ; else if ( cube [ l ] + cube [ r ] > n ) r -- ; else { System . out . println ( " ( " + l + " , ▁ " + r + " ) " ) ; l ++ ; r -- ; } } } public static void main ( String [ ] args ) { int n = 20683 ; findPairs ( n ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPairs ( int n ) { int cubeRoot = ( int ) Math . pow ( n , 1.0 / 3.0 ) ; HashMap < Integer , pair > s = new HashMap < Integer , pair > ( ) ; for ( int x = 1 ; x < cubeRoot ; x ++ ) { for ( int y = x + 1 ; y <= cubeRoot ; y ++ ) { int sum = x * x * x + y * y * y ; if ( sum != n ) continue ; if ( s . containsKey ( sum ) ) { System . out . print ( " ( " + s . get ( sum ) . first + " , ▁ " + s . get ( sum ) . second + " ) ▁ and ▁ ( " + x + " , ▁ " + y + " ) " + "NEW_LINE"); } else s . put ( sum , new pair ( x , y ) ) ; } } } public static void main ( String [ ] args ) { int n = 13832 ; findPairs ( n ) ; } }
import java . io . * ; class GFG { static int countEvenSum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; System . out . print ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " ) ; System . out . println ( countEvenSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int countEvenSum ( int arr [ ] , int n ) { int temp [ ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; System . out . println ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { while ( b != 0 ) { int t = b ; b = a % b ; a = t ; } return a ; } static int findMinDiff ( int a , int b , int x , int y ) { int g = gcd ( a , b ) ; int diff = Math . abs ( x - y ) % g ; return Math . min ( diff , g - diff ) ; } public static void main ( String [ ] args ) { int a = 20 , b = 52 , x = 5 , y = 7 ; System . out . println ( findMinDiff ( a , b , x , y ) ) ; } }
import java . io . * ; class GFG { static int calculateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = fibo [ 0 ] + fibo [ 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += fibo [ i ] ; } return sum ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Sum ▁ of ▁ Fibonacci " + " ▁ numbers ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } }
import java . io . * ; class GFG { static void findCombinationsUtil ( int arr [ ] , int index , int num , int reducedNum ) { if ( reducedNum < 0 ) return ; if ( reducedNum == 0 ) { for ( int i = 0 ; i < index ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; return ; } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= num ; k ++ ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , num , reducedNum - k ) ; } } static void findCombinations ( int n ) { int arr [ ] = new int [ n ] ; findCombinationsUtil ( arr , 0 , n , n ) ; } public static void main ( String [ ] args ) { int n = 5 ; findCombinations ( n ) ; } }
class GFG { static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } public static void main ( String args [ ] ) { if ( isPower ( 27 , 729 ) ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }
public class GFG { static boolean isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } public static void main ( String args [ ] ) { if ( isPerfectSquare ( 35 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; if ( isPerfectSquare ( 49 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int findCount ( int d ) { return 9 * ( ( int ) ( Math . pow ( 10 , d - 1 ) ) - ( int ) ( Math . pow ( 9 , d - 1 ) ) ) ; } public static void main ( String args [ ] ) { int d = 1 ; System . out . println ( findCount ( d ) ) ; d = 2 ; System . out . println ( findCount ( d ) ) ; d = 4 ; System . out . println ( findCount ( d ) ) ; } }
class GFG { public static int countDyckPaths ( int n ) { int res = 1 ; for ( int i = 0 ; i < n ; ++ i ) { res *= ( 2 * n - i ) ; res /= ( i + 1 ) ; } return res / ( n + 1 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Number ▁ of ▁ Dyck ▁ Paths ▁ is ▁ " + countDyckPaths ( n ) ) ; } }
import java . lang . Math ; class GFG { public static void printDivisors ( int n ) { int i ; for ( i = 1 ; i * i < n ; i ++ ) { if ( n % i == 0 ) System . out . print ( i + " ▁ " ) ; } if ( i - ( n / i ) == 1 ) { i -- ; } for ( ; i >= 1 ; i -- ) { if ( n % i == 0 ) System . out . print ( n / i + " ▁ " ) ; } } public static void main ( String [ ] args ) { System . out . println ( " The ▁ divisors ▁ of ▁ 100 ▁ are : ▁ " ) ; printDivisors ( 100 ) ; } }
class GFG { static boolean isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 55 ; if ( isTriangular ( n ) ) System . out . print ( " The ▁ number ▁ " + " is ▁ a ▁ triangular ▁ number " ) ; else System . out . print ( " The ▁ number " + " ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }
import java . io . * ; class GFG { static boolean isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; float root1 = ( - b + ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && Math . floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && Math . floor ( root2 ) == root2 ) return true ; return false ; } public static void main ( String [ ] args ) { int num = 55 ; if ( isTriangular ( num ) ) System . out . println ( " The ▁ number ▁ is " + " ▁ a ▁ triangular ▁ number " ) ; else System . out . println ( " The ▁ number ▁ " + " is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }
import java . util . * ; class GFG { static String fractionToDecimal ( int numr , int denr ) { String res = " " ; HashMap < Integer , Integer > mp = new HashMap < > ( ) ; mp . clear ( ) ; int rem = numr % denr ; while ( ( rem != 0 ) && ( ! mp . containsKey ( rem ) ) ) { mp . put ( rem , res . length ( ) ) ; rem = rem * 10 ; int res_part = rem / denr ; res += String . valueOf ( res_part ) ; rem = rem % denr ; } if ( rem == 0 ) return " " ; else if ( mp . containsKey ( rem ) ) return res . substring ( mp . get ( rem ) ) ; return " " ; } public static void main ( String [ ] args ) { int numr = 50 , denr = 22 ; String res = fractionToDecimal ( numr , denr ) ; if ( res == " " ) System . out . print ( " No ▁ recurring ▁ sequence " ) ; else System . out . print ( " Recurring ▁ sequence ▁ is ▁ " + res ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { int c ; while ( a != 0 ) { c = a ; a = b % a ; b = c ; } return b ; } static void forbenius ( int X , int Y ) { if ( gcd ( X , Y ) != 1 ) { System . out . println ( " NA " ) ; return ; } int A = ( X * Y ) - ( X + Y ) ; int N = ( X - 1 ) * ( Y - 1 ) / 2 ; System . out . println ( " Largest ▁ Amount ▁ = ▁ " + A ) ; System . out . println ( " Total ▁ Count ▁ = ▁ " + N ) ; } public static void main ( String [ ] args ) { int X = 2 , Y = 5 ; forbenius ( X , Y ) ; X = 5 ; Y = 10 ; System . out . println ( ) ; forbenius ( X , Y ) ; } }
import java . io . * ; class GFG { static int has0 ( int x ) { while ( x != 0 ) { if ( x % 10 == 0 ) return 1 ; x /= 10 ; } return 0 ; } static int getCount ( int n ) { int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) count += has0 ( i ) ; return count ; } public static void main ( String args [ ] ) { int n = 107 ; System . out . println ( " Count ▁ of ▁ numbers ▁ from ▁ 1" + " ▁ to ▁ " + n + " ▁ is ▁ " + getCount ( n ) ) ; } }
class GFG { static long mulmod ( long a , long b , long mod ) { a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) { res = ( res + a ) % mod ; } a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static void main ( String [ ] args ) { long a = 9223372036854775807L , b = 9223372036854775807L ; System . out . println ( mulmod ( a , b , 100000000000L ) ) ; } }
class GFG { static int SieveOfAtkin ( int limit ) { if ( limit > 2 ) System . out . print ( 2 + " ▁ " ) ; if ( limit > 3 ) System . out . print ( 3 + " ▁ " ) ; boolean sieve [ ] = new boolean [ limit ] ; for ( int i = 0 ; i < limit ; i ++ ) sieve [ i ] = false ; for ( int x = 1 ; x * x < limit ; x ++ ) { for ( int y = 1 ; y * y < limit ; y ++ ) { int n = ( 4 * x * x ) + ( y * y ) ; if ( n <= limit && ( n % 12 == 1 n % 12 == 5 ) ) sieve [ n ] ^= true ; n = ( 3 * x * x ) + ( y * y ) ; if ( n <= limit && n % 12 == 7 ) sieve [ n ] ^= true ; n = ( 3 * x * x ) - ( y * y ) ; if ( x > y && n <= limit && n % 12 == 11 ) sieve [ n ] ^= true ; } } for ( int r = 5 ; r * r < limit ; r ++ ) { if ( sieve [ r ] ) { for ( int i = r * r ; i < limit ; i += r * r ) sieve [ i ] = false ; } } for ( int a = 5 ; a < limit ; a ++ ) if ( sieve [ a ] ) System . out . print ( a + " ▁ " ) ; return 0 ; } public static void main ( String [ ] args ) { int limit = 20 ; SieveOfAtkin ( limit ) ; } }
class GFG { static boolean squareRootExists ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) if ( ( x * x ) % p == n ) return true ; return false ; } public static void main ( String [ ] args ) { int p = 7 ; int n = 2 ; if ( squareRootExists ( n , p ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . io . * ; class GFG { static int modFact ( int n , int p ) { if ( n >= p ) return 0 ; int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = ( result * i ) % p ; return result ; } public static void main ( String [ ] args ) { int n = 25 , p = 29 ; System . out . print ( modFact ( n , p ) ) ; } }
import java . io . * ; class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int num [ ] , int rem [ ] , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } public static void main ( String args [ ] ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = num . length ; System . out . println ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
import java . io . * ; class GFG { static int findMinX ( int num [ ] , int rem [ ] , int k ) { while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } } public static void main ( String args [ ] ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = num . length ; System . out . println ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }
import java . io . * ; class GFG { public static int N = 30 ; public static int [ ] fib = new int [ N ] ; public static int largestFiboLessOrEqual ( int n ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; int i ; for ( i = 2 ; fib [ i - 1 ] <= n ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } return ( i - 2 ) ; } public static String fibonacciEncoding ( int n ) { int index = largestFiboLessOrEqual ( n ) ; char [ ] codeword = new char [ index + 3 ] ; int i = index ; while ( n > 0 ) { codeword [ i ] = '1' ; n = n - fib [ i ] ; i = i - 1 ; while ( i >= 0 && fib [ i ] > n ) { codeword [ i ] = '0' ; i = i - 1 ; } } codeword [ index + 1 ] = '1' ; codeword [ index + 2 ] = ' \0' ; String string = new String ( codeword ) ; return string ; } public static void main ( String [ ] args ) { int n = 143 ; System . out . println ( " Fibonacci ▁ code ▁ word ▁ for ▁ " + n + " ▁ is ▁ " + fibonacciEncoding ( n ) ) ; } }
import java . io . * ; class Numbers { static boolean isValid ( int n , int d ) { int digit = n % 10 ; int sum = digit ; if ( digit == d ) return false ; n /= 10 ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d digit <= sum ) return false ; else { sum += digit ; n /= 10 ; } } return true ; } static void printGoodNumber ( int L , int R , int d ) { for ( int i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ; } }
import java . util . * ; class GFG { static int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return n * ( n + 1 ) * ( 3 * m - n + 1 ) / 6 ; } public static void main ( String [ ] args ) { int m = 4 ; int n = 3 ; System . out . print ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( m , n ) ) ; } }
class GFG { public static int nearestSmallerEqFib ( int n ) { if ( n == 0 n == 1 ) return n ; int f1 = 0 , f2 = 1 , f3 = 1 ; while ( f3 <= n ) { f1 = f2 ; f2 = f3 ; f3 = f1 + f2 ; } return f2 ; } public static void printFibRepresntation ( int n ) { while ( n > 0 ) { int f = nearestSmallerEqFib ( n ) ; System . out . print ( f + " ▁ " ) ; n = n - f ; } } public static void main ( String [ ] args ) { int n = 30 ; System . out . println ( " Non - neighbouring ▁ Fibonacci ▁ " + " ▁ Representation ▁ of ▁ " + n + " ▁ is " ) ; printFibRepresntation ( n ) ; } }
class GFG { static int dp [ ] [ ] [ ] = new int [ 5001 ] [ 5001 ] [ 5 ] ; static int countWaysUtil ( int n , int parts , int nextPart ) { if ( parts == 0 && n == 0 ) return 1 ; if ( n <= 0 parts <= 0 ) return 0 ; if ( dp [ n ] [ nextPart ] [ parts ] != - 1 ) return dp [ n ] [ nextPart ] [ parts ] ; for ( int i = nextPart ; i <= n ; i ++ ) ans += countWaysUtil ( n - i , parts - 1 , i ) ; return ( dp [ n ] [ nextPart ] [ parts ] = ans ) ; } static int countWays ( int n ) { for ( int i = 0 ; i < 5001 ; i ++ ) { for ( int j = 0 ; j < 5001 ; j ++ ) { for ( int l = 0 ; l < 5 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } return countWaysUtil ( n , 4 , 1 ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( countWays ( n ) ) ; } }
import java . util . Vector ; import static java . lang . Math . sqrt ; import static java . lang . Math . floor ; class Test { static void simpleSieve ( int limit , Vector < Integer > prime ) { boolean mark [ ] = new boolean [ limit + 1 ] ; for ( int i = 0 ; i < mark . length ; i ++ ) mark [ i ] = true ; for ( int p = 2 ; p * p < limit ; p ++ ) { if ( mark [ p ] == true ) { for ( int i = p * p ; i < limit ; i += p ) mark [ i ] = false ; } } for ( int p = 2 ; p < limit ; p ++ ) { if ( mark [ p ] == true ) { prime . add ( p ) ; System . out . print ( p + " ▁ " ) ; } } } static void segmentedSieve ( int n ) { int limit = ( int ) ( floor ( sqrt ( n ) ) + 1 ) ; Vector < Integer > prime = new Vector < > ( ) ; simpleSieve ( limit , prime ) ; int low = limit ; int high = 2 * limit ; while ( low < n ) { if ( high >= n ) high = n ; boolean mark [ ] = new boolean [ limit + 1 ] ; for ( int i = 0 ; i < mark . length ; i ++ ) mark [ i ] = true ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { int loLim = ( int ) ( floor ( low / prime . get ( i ) ) * prime . get ( i ) ) ; if ( loLim < low ) loLim += prime . get ( i ) ; for ( int j = loLim ; j < high ; j += prime . get ( i ) ) mark [ j - low ] = false ; } for ( int i = low ; i < high ; i ++ ) if ( mark [ i - low ] == true ) System . out . print ( i + " ▁ " ) ; low = low + limit ; high = high + limit ; } } public static void main ( String args [ ] ) { int n = 100 ; System . out . println ( " Primes ▁ smaller ▁ than ▁ " + n + " : " ) ; segmentedSieve ( n ) ; } }
class GFG { static void printTwins ( int low , int high ) { boolean prime [ ] = new boolean [ high + 1 ] , twin = false ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( int i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { int a = i + 2 ; System . out . print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " + i + " , ▁ " + a + " ) " ) ; twin = true ; break ; } } if ( twin == false ) { System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } } public static void main ( String [ ] args ) { printTwins ( 10 , 100 ) ; } }
class GFG { static boolean isInside ( int circle_x , int circle_y , int rad , int x , int y ) { if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) return true ; else return false ; } public static void main ( String arg [ ] ) { int x = 1 , y = 1 ; int circle_x = 0 , circle_y = 1 , rad = 2 ; if ( isInside ( circle_x , circle_y , rad , x , y ) ) System . out . print ( " Inside " ) ; else System . out . print ( " Outside " ) ; } }
import java . util . * ; class GFG { static boolean isFancy ( String num ) { Map < Character , Character > fp = new HashMap < Character , Character > ( ) ; fp . put ( '0' , '0' ) ; fp . put ( '1' , '1' ) ; fp . put ( '6' , '9' ) ; fp . put ( '8' , '8' ) ; fp . put ( '9' , '6' ) ; int n = num . length ( ) ; int l = 0 , r = n - 1 ; while ( l <= r ) { if ( ! fp . containsKey ( num . charAt ( l ) ) || fp . get ( num . charAt ( l ) ) != num . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } public static void main ( String [ ] args ) { String str = "9088806" ; if ( isFancy ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int nextSparse ( int x ) { ArrayList < Integer > bin = new ArrayList < Integer > ( ) ; while ( x != 0 ) { bin . add ( x & 1 ) ; x >>= 1 ; } bin . add ( 0 ) ; int last_final = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( bin . get ( i ) == 1 && bin . get ( i - 1 ) == 1 && bin . get ( i + 1 ) != 1 ) { bin . set ( i + 1 , 1 ) ; for ( int j = i ; j >= last_final ; j -- ) bin . set ( j , 0 ) ; last_final = i + 1 ; } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += bin . get ( i ) * ( 1 << i ) ; return ans ; } public static void main ( String [ ] args ) { int x = 38 ; System . out . println ( " Next ▁ Sparse ▁ Number ▁ is ▁ " + nextSparse ( x ) ) ; } }
import java . util . * ; class GFG { static void ccountSubsets ( int arr [ ] , int n , int gcd [ ] , int m ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > subsets = new HashMap < Integer , Integer > ( ) ; int arrMax = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arrMax = Math . max ( arrMax , arr [ i ] ) ; if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } } for ( int i = arrMax ; i >= 1 ; i -- ) { int sub = 0 ; int add = 0 ; if ( freq . containsKey ( i ) ) add = freq . get ( i ) ; for ( int j = 2 ; j * i <= arrMax ; j ++ ) { if ( freq . containsKey ( i * j ) ) add += freq . get ( j * i ) ; sub += subsets . get ( j * i ) ; } subsets . put ( i , ( 1 << add ) - 1 - sub ) ; } for ( int i = 0 ; i < m ; i ++ ) System . out . print ( " Number ▁ of ▁ subsets ▁ with ▁ gcd ▁ " + gcd [ i ] + " ▁ is ▁ " + subsets . get ( gcd [ i ] ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int gcd [ ] = { 2 , 3 } ; int arr [ ] = { 9 , 6 , 2 } ; int n = arr . length ; int m = gcd . length ; ccountSubsets ( arr , n , gcd , m ) ; } }
import java . io . * ; class GFG { static int sumBitDifferences ( int arr [ ] , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) count ++ ; ans += ( count * ( n - count ) * 2 ) ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 5 } ; int n = arr . length ; System . out . println ( sumBitDifferences ( arr , n ) ) ; } }
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . printf ( " % d ▁ " , arr [ i ] ) ; System . out . println ( " " ) ; } static void generateUtil ( int x , int arr [ ] , int curr_sum , int curr_idx ) { if ( curr_sum == x ) { printArr ( arr , curr_idx ) ; return ; } int num = 1 ; while ( num <= x - curr_sum && ( curr_idx == 0 num <= arr [ curr_idx - 1 ] ) ) { arr [ curr_idx ] = num ; generateUtil ( x , arr , curr_sum + num , curr_idx + 1 ) ; num ++ ; } } static void generate ( int x ) { int arr [ ] = new int [ x ] ; generateUtil ( x , arr , 0 , 0 ) ; } public static void main ( String [ ] args ) { int x = 5 ; generate ( x ) ; } }
import java . util . * ; class GFG { static int eval ( int a , char op , int b ) { if ( op == ' + ' ) { return a + b ; } if ( op == ' - ' ) { return a - b ; } if ( op == ' * ' ) { return a * b ; } return Integer . MAX_VALUE ; } static Vector < Integer > evaluateAll ( String expr , int low , int high ) { Vector < Integer > res = new Vector < Integer > ( ) ; if ( low == high ) { res . add ( expr . charAt ( low ) - '0' ) ; return res ; } if ( low == ( high - 2 ) ) { int num = eval ( expr . charAt ( low ) - '0' , expr . charAt ( low + 1 ) , expr . charAt ( low + 2 ) - '0' ) ; res . add ( num ) ; return res ; } for ( int i = low + 1 ; i <= high ; i += 2 ) { Vector < Integer > l = evaluateAll ( expr , low , i - 1 ) ; Vector < Integer > r = evaluateAll ( expr , i + 1 , high ) ; for ( int s1 = 0 ; s1 < l . size ( ) ; s1 ++ ) { for ( int s2 = 0 ; s2 < r . size ( ) ; s2 ++ ) { int val = eval ( l . get ( s1 ) , expr . charAt ( i ) , r . get ( s2 ) ) ; res . add ( val ) ; } } } return res ; } public static void main ( String [ ] args ) { String expr = "1*2 + 3*4" ; int len = expr . length ( ) ; Vector < Integer > ans = evaluateAll ( expr , 0 , len - 1 ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . println ( ans . get ( i ) ) ; } } }
class GFG { static boolean isPerfect ( int n ) { int sum = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( i * i != n ) sum = sum + i + n / i ; else sum = sum + i ; } } if ( sum == n && n != 1 ) return true ; return false ; } public static void main ( String [ ] args ) { System . out . println ( " Below ▁ are ▁ all ▁ perfect " + " numbers ▁ till ▁ 10000" ) ; for ( int n = 2 ; n < 10000 ; n ++ ) if ( isPerfect ( n ) ) System . out . println ( n + " ▁ is ▁ a ▁ perfect ▁ number " ) ; } }
class GFG { static boolean checkUtil ( int num , int dig , int base ) { if ( dig == 1 && num < base ) return true ; if ( dig > 1 && num >= base ) return checkUtil ( num / base , -- dig , base ) ; return false ; } static boolean check ( int num , int dig ) { for ( int base = 2 ; base <= 32 ; base ++ ) if ( checkUtil ( num , dig , base ) ) return true ; return false ; } public static void main ( String [ ] args ) { int num = 8 ; int dig = 3 ; if ( check ( num , dig ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
class GFG { static boolean isLucky ( int n ) { boolean arr [ ] = new boolean [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) arr [ i ] = false ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1291 , 897 , 4566 , 1232 , 80 , 700 } ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) if ( isLucky ( arr [ i ] ) ) System . out . print ( arr [ i ] + " is Lucky NEW_LINE"); else System . out . print ( arr [ i ] + " is not Lucky NEW_LINE"); } }
import java . io . * ; class GFG { static int modInverse ( int a , int m ) { for ( int x = 1 ; x < m ; x ++ ) if ( ( ( a % m ) * ( x % m ) ) % m == 1 ) return x ; return 1 ; } public static void main ( String args [ ] ) { int a = 3 , m = 11 ; System . out . println ( modInverse ( a , m ) ) ; } }
import java . util . * ; class GFG { static int getRemainder ( int num , int divisor ) { while ( num >= divisor ) num -= divisor ; return num ; } public static void main ( String [ ] args ) { int num = 100 , divisor = 7 ; System . out . println ( getRemainder ( num , divisor ) ) ; } }
import java . io . * ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } static double sum ( int n ) { double sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 1.0 / factorial ( i ) ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sum ( n ) ) ; } }
class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } static long findWays ( int n ) { if ( ( n & 1 ) != 0 ) return 0 ; return catalan ( n / 2 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( " Total ▁ possible ▁ expressions ▁ of ▁ length ▁ " + n + " ▁ is ▁ " + findWays ( 6 ) ) ; } }
class GFG { static boolean isOperand ( char c ) { return ( c >= '0' && c <= '9' ) ; } static int value ( char c ) { return ( int ) ( c - '0' ) ; } static int evaluate ( String exp ) { if ( exp . length ( ) == 0 ) return - 1 ; int res = value ( exp . charAt ( 0 ) ) ; for ( int i = 1 ; i < exp . length ( ) ; i += 2 ) { char opr = exp . charAt ( i ) , opd = exp . charAt ( i + 1 ) ; if ( isOperand ( opd ) == false ) return - 1 ; if ( opr == ' + ' ) res += value ( opd ) ; else if ( opr == ' - ' ) res -= value ( opd ) ; else if ( opr == ' * ' ) res *= value ( opd ) ; else if ( opr == ' / ' ) res /= value ( opd ) ; else return - 1 ; } return res ; } public static void main ( String [ ] args ) { String expr1 = "1 + 2*5 + 3" ; int res = evaluate ( expr1 ) ; if ( res == - 1 ) System . out . println ( expr1 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr1 + " ▁ is ▁ " + res ) ; String expr2 = "1 + 2*3" ; res = evaluate ( expr2 ) ; if ( res == - 1 ) System . out . println ( expr2 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr2 + " ▁ is ▁ " + res ) ; String expr3 = "4-2 + 6*3" ; res = evaluate ( expr3 ) ; if ( res == - 1 ) System . out . println ( expr3 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr3 + " ▁ is ▁ " + res ) ; String expr4 = "1 + + 2" ; res = evaluate ( expr4 ) ; if ( res == - 1 ) System . out . println ( expr4 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr4 + " ▁ is ▁ " + res ) ; } }
import java . io . * ; class GFG { static int countSubsequences ( int arr [ ] ) { int odd = 0 ; int even = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) odd ++ ; else even ++ ; } return ( ( 1 << odd ) - 1 ) * ( 1 << even ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 } ; System . out . println ( countSubsequences ( arr ) ) ; } }
import java . io . * ; class GFG { public static int XorSum ( int A [ ] , int B [ ] , int N , int M ) { int ans = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( ans == - 1 ) ans = ( A [ i ] & B [ j ] ) ; else ans ^= ( A [ i ] & B [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 } ; int B [ ] = { 2 , 3 } ; int N = A . length ; int M = B . length ; System . out . println ( XorSum ( A , B , N , M ) ) ; } }
import java . util . * ; class GFG { static int countbitdiff ( int xo ) { int count = 0 ; while ( xo != 0 ) { if ( xo % 2 == 1 ) { count ++ ; } xo = xo / 2 ; } return count ; } static void shortestPath ( int n , int a , int b ) { int xorVal = a ^ b ; int cnt = countbitdiff ( xorVal ) ; if ( cnt % 2 == 0 ) System . out . print ( cnt / 2 ) ; else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; int a = 15 , b = 3 ; shortestPath ( n , a , b ) ; } }
static boolean getBit ( int num , int i ) { return ( ( num & ( 1 << i ) ) != 0 ) ; }
static int clearBit ( int num , int i ) { int mask = ~ ( 1 << i ) ; return num & mask ; }
import java . util . * ; class GFG { static int countSet ( int N ) { int ans = 0 ; while ( N > 0 ) { ans += N & 1 ; N >>= 1 ; } return ans ; } static int countSub ( int [ ] arr , int k ) { int ans = 0 ; int setK = 0 ; for ( int i = 0 ; i < 5 ; i ++ ) { if ( countSet ( arr [ i ] ) == k ) setK += 1 ; else setK = 0 ; ans += setK ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 5 , 6 } ; int K = 2 ; System . out . print ( countSub ( arr , K ) ) ; } }
import java . io . * ; class GFG { static void oddXorSubarray ( int a [ ] , int n ) { int odd = 0 ; int c_odd = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 ) { odd = ( odd == 0 ) ? 1 : 0 ; } if ( odd != 0 ) { c_odd ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { result += c_odd ; if ( a [ i ] % 2 != 0 ) { c_odd = ( n - i - c_odd ) ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 7 , 9 , 10 } ; int N = arr . length ; oddXorSubarray ( arr , N ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 2 , 3 } ; int [ ] arr2 = { 1 , 2 , 3 } ; int N = arr1 . length ; int M = arr2 . length ; Bitwise_AND_sum_i ( arr1 , arr2 , M , N ) ; } static void Bitwise_AND_sum_i ( int arr1 [ ] , int arr2 [ ] , int M , int N ) { int [ ] frequency = new int [ 32 ] ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int num = arr1 [ i ] ; while ( num != 0 ) { if ( ( num & 1 ) != 0 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { int num = arr2 [ i ] ; int value_at_that_bit = 1 ; int bitwise_AND_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( num & 1 ) != 0 ) { bitwise_AND_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } System . out . print ( bitwise_AND_sum + " ▁ " ) ; } } }
import java . util . * ; class GFG { static void Bitwise_OR_sum_i ( int arr1 [ ] , int arr2 [ ] , int M , int N ) { int frequency [ ] = new int [ 32 ] ; Arrays . fill ( frequency , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { int bit_position = 0 ; int num = arr1 [ i ] ; while ( num != 0 ) { if ( ( num & 1 ) != 0 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( int i = 0 ; i < M ; i ++ ) { int num = arr2 [ i ] ; int value_at_that_bit = 1 ; int bitwise_OR_sum = 0 ; for ( int bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( num & 1 ) != 0 ) { bitwise_OR_sum += N * value_at_that_bit ; } else { bitwise_OR_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } System . out . print ( bitwise_OR_sum + " ▁ " ) ; } return ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 1 , 2 , 3 } ; int N = arr1 . length ; int M = arr2 . length ; Bitwise_OR_sum_i ( arr1 , arr2 , M , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int [ ] arr , int k , int size ) { int count = 0 , x ; for ( int i = 0 ; i < size - 1 ; i ++ ) { for ( int j = i + 1 ; j < size ; j ++ ) { x = arr [ i ] | arr [ j ] ; if ( x == k ) count ++ ; } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 38 , 44 , 29 , 62 } ; int K = 46 ; int N = arr . length ; countPairs ( arr , K , N ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > [ ] adj = new Vector [ 100005 ] ; static int visited [ ] = new int [ 100005 ] ; static int ans = 0 ; static void dfs ( int node , int xorr , int k ) { visited [ node ] = 1 ; if ( node != 1 && xorr == k ) ans ++ ; for ( pair x : adj [ node ] ) { if ( visited [ x . first ] != 1 ) { int xorr1 = xorr ^ x . second ; dfs ( x . first , xorr1 , k ) ; } } } static void countNodes ( int N , int K , int R , int [ ] [ ] edges ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { int u = edges [ i ] [ 0 ] , v = edges [ i ] [ 1 ] , w = edges [ i ] [ 2 ] ; adj [ u ] . add ( new pair ( v , w ) ) ; adj [ v ] . add ( new pair ( u , w ) ) ; } dfs ( R , 0 , K ) ; System . out . print ( ans + "NEW_LINE"); } public static void main ( String [ ] args ) { int K = 0 , R = 1 ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < pair > ( ) ; int [ ] [ ] edges = { { 1 , 2 , 3 } , { 1 , 3 , 1 } , { 2 , 4 , 3 } , { 2 , 5 , 4 } , { 3 , 6 , 1 } , { 3 , 7 , 2 } } ; int N = edges . length ; countNodes ( N , K , R , edges ) ; } }
class GFG { static void findMinXORSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_xor = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_xor ^= arr [ i ] ; int min_xor = curr_xor ; for ( int i = k ; i < n ; i ++ ) { curr_xor ^= ( arr [ i ] ^ arr [ i - k ] ) ; if ( curr_xor < min_xor ) { min_xor = curr_xor ; res_index = ( i - k + 1 ) ; } } System . out . println ( min_xor ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; int k = 3 ; int n = arr . length ; findMinXORSubarray ( arr , n , k ) ; } }
import java . util . * ; class GFG { static int powerOfTwo ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( a [ i ] ) ) { mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; } else { mp . put ( a [ i ] , 1 ) ; } } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean f = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int s = ( 1 << j ) - a [ i ] ; if ( mp . containsKey ( s ) && ( mp . get ( s ) > 1 || mp . get ( s ) == 1 && s != a [ i ] ) ) f = true ; } if ( f == false ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 2 , 11 } ; int n = a . length ; System . out . print ( powerOfTwo ( a , n ) ) ; } }
import java . io . * ; class GFG { static void findArray ( int n , int a , int b , int c ) { int arr [ ] = new int [ n + 1 ] ; for ( int bit = 30 ; bit >= 0 ; bit -- ) { int set = a & ( 1 << bit ) ; if ( set != 0 ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] |= set ; } else { if ( ( b & ( 1 << bit ) ) != 0 ) { arr [ 0 ] |= ( 1 << bit ) ; if ( ( c & ( 1 << bit ) ) == 0 ) { arr [ 1 ] |= ( 1 << bit ) ; } } } } int aa = Integer . MAX_VALUE , bb = 0 , cc = 0 ; for ( int i = 0 ; i < n ; i ++ ) { aa &= arr [ i ] ; bb |= arr [ i ] ; cc ^= arr [ i ] ; } if ( a == aa && b == bb && c == cc ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 3 , a = 4 , b = 6 , c = 6 ; findArray ( n , a , b , c ) ; } }
class GFG { static int even , odd ; static void keep_count ( int arr [ ] , int N ) { int count ; for ( int i = 0 ; i < N ; i ++ ) { count = 0 ; while ( arr [ i ] != 0 ) { arr [ i ] = ( arr [ i ] - 1 ) & arr [ i ] ; count ++ ; } if ( count % 2 == 0 ) even ++ ; else odd ++ ; } return ; } static void solveQueries ( int arr [ ] , int n , int q [ ] , int m ) { even = 0 ; odd = 0 ; keep_count ( arr , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int X = q [ i ] ; int count = 0 ; while ( X != 0 ) { X = ( X - 1 ) & X ; count ++ ; } if ( count % 2 == 0 ) { System . out . print ( even + " ▁ " + odd + "NEW_LINE"); } else { System . out . print ( odd + " ▁ " + even + "NEW_LINE"); } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 4 , 5 , 3 } ; int n = arr . length ; int q [ ] = { 3 , 4 , 12 , 6 } ; int m = q . length ; solveQueries ( arr , n , q , m ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int isEvenParity ( int num ) { int parity = 0 ; int x = num ; while ( x != 0 ) { if ( ( x & 1 ) == 1 ) parity ++ ; x = x >> 1 ; } if ( parity % 2 == 0 ) return num ; else return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + isEvenParity ( i ) ; } } static void printSum ( int L , int R ) { System . out . println ( pref [ R ] - pref [ L - 1 ] ) ; } static void printSum ( int arr [ ] [ ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int N = 2 ; int [ ] [ ] Q = { { 1 , 10 } , { 121 , 211 } } ; printSum ( Q , N ) ; } }
import java . util . * ; class GFG { static int findLargestNumber ( int [ ] arr ) { int k = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int [ ] m = new int [ k + 1 ] ; for ( int n : arr ) ++ m [ n ] ; for ( int n = arr . length - 1 ; n > 0 ; -- n ) { if ( n == m [ n ] ) return n ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 5 , 2 , 4 , 5 } ; System . out . print ( findLargestNumber ( arr ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int NoCarrySum ( int N , int M ) { return N ^ M ; } public static void main ( String [ ] args ) { int N = 37 ; int M = 12 ; System . out . print ( NoCarrySum ( N , M ) ) ; } }
import java . util . * ; class GFG { static boolean CheckBits ( int N , int K ) { int check = 0 ; int count = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { if ( ( ( 1 << i ) & N ) > 0 ) { if ( check == 0 ) { check = 1 ; } else { if ( count < K ) { return false ; } } count = 0 ; } else { count ++ ; } } return true ; } public static void main ( String [ ] args ) { int N = 5 ; int K = 1 ; if ( CheckBits ( N , K ) ) { System . out . print ( " YES " ) ; } else { System . out . print ( " NO " ) ; } } }
static int countSubseq ( int arr [ ] , int n ) { int count = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; else mp . put ( arr [ i ] , 1 ) ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { if ( entry . getKey ( ) == 0 ) count += Math . pow ( 2 , entry . getValue ( ) ) - 1 ; else count += Math . pow ( 2 , entry . getValue ( ) - 1 ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 5 , 6 } ; int n = arr . length ; System . out . println ( countSubseq ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int calculate ( Vector < Integer > section , int pos ) { if ( pos < 0 ) return 0 ; Vector < Integer > on_section = new Vector < Integer > ( ) , off_section = new Vector < Integer > ( ) ; for ( int el : section ) { if ( ( ( el >> pos ) & 1 ) == 0 ) off_section . add ( el ) ; else on_section . add ( el ) ; } if ( off_section . size ( ) == 0 ) return calculate ( on_section , pos - 1 ) ; if ( on_section . size ( ) == 0 ) return calculate ( off_section , pos - 1 ) ; return Math . min ( calculate ( off_section , pos - 1 ) , calculate ( on_section , pos - 1 ) ) + ( 1 << pos ) ; } static int minXorValue ( int a [ ] , int n ) { Vector < Integer > section = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) section . add ( a [ i ] ) ; return calculate ( section , 30 ) ; } public static void main ( String [ ] args ) { int N = 4 ; int A [ ] = { 3 , 2 , 5 , 6 } ; System . out . print ( minXorValue ( A , N ) ) ; } }
class GFG { static void findNthNum ( int N ) { int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; System . out . print ( ( 1 << bit_L ) + ( 1 << bit_R ) + "NEW_LINE"); } public static void main ( String [ ] args ) { int N = 13 ; findNthNum ( N ) ; } }
class GFG { static int xorOfArray ( int arr [ ] , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; System . out . println ( xorOfArray ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void countEvenBit ( int A [ ] , int B [ ] , int n , int m ) { int i , j , cntOdd = 0 , cntEven = 0 ; for ( i = 0 ; i < n ; i ++ ) { int x = Integer . bitCount ( A [ i ] ) ; if ( x % 2 == 1 ) { cntEven ++ ; } else { cntOdd ++ ; } } int [ ] CountB = new int [ m ] ; for ( i = 0 ; i < m ; i ++ ) { int x = Integer . bitCount ( B [ i ] ) ; if ( x % 2 == 1 ) { CountB [ i ] = cntEven ; } else { CountB [ i ] = cntOdd ; } } for ( i = 0 ; i < m ; i ++ ) { System . out . print ( CountB [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int B [ ] = { 3 , 4 , 22 } ; countEvenBit ( A , B , 6 , 3 ) ; } }
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 " ) ; } }
class GFG { static int bitwiseOrTillN ( int n ) { int result = 2 ; for ( int i = 4 ; i <= n ; i = i + 2 ) { result = result | i ; } return result ; } static public void main ( String args [ ] ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }
class GFG { static int bitwiseOrTillN ( int n ) { if ( n < 2 ) return 0 ; int bitCount = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; return ( int ) Math . pow ( 2 , bitCount ) - 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( bitwiseOrTillN ( n ) ) ; } }
class GFG { static int bitwiseAndOdd ( int n ) { return 1 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( bitwiseAndOdd ( n ) ) ; } }
class GFG { static void genSequence ( int n , int val ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int x = i ^ ( i >> 1 ) ^ val ; System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; genSequence ( n , k ) ; } }
import java . util . * ; class GFG { static String winner ( String moves ) { HashMap < Character , Integer > data = new HashMap < Character , Integer > ( ) ; data . put ( ' R ' , 0 ) ; data . put ( ' P ' , 1 ) ; data . put ( ' S ' , 2 ) ; if ( moves . charAt ( 0 ) == moves . charAt ( 1 ) ) { return " Draw " ; } if ( ( ( data . get ( moves . charAt ( 0 ) ) | 1 << ( 2 ) ) - ( data . get ( moves . charAt ( 1 ) ) | 0 << ( 2 ) ) ) % 3 != 0 ) { return " A " ; } return " B " ; } static void performQueries ( String arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( winner ( arr [ i ] ) + "NEW_LINE"); } public static void main ( String [ ] args ) { String arr [ ] = { " RS " , " SR " , " SP " , " PP " } ; int n = arr . length ; performQueries ( arr , n ) ; } }
class GFG { static int maxConsecutiveOnes ( int x ) { int count = 0 ; while ( x != 0 ) { x = ( x & ( x << 1 ) ) ; count ++ ; } return count ; } static int maxOnes ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int currMax = maxConsecutiveOnes ( arr [ i ] ) ; ans = Math . max ( ans , currMax ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( maxOnes ( arr , n ) ) ; } }
class GFG { static int maxOR ( int L , int R ) { int maximum = Integer . MIN_VALUE ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }
class GFG { static int MAX = 64 ; static int maxOR ( int L , int R ) { if ( L == R ) { return L ; } int ans = 0 ; for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }
import java . util . * ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { Vector < Integer > filter = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . add ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_and = filter . get ( 0 ) ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_and &= filter . get ( i ) ; if ( c_and == m ) return filter . size ( ) ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . length ; int m = 3 ; System . out . println ( findLen ( arr , n , m ) ) ; } }
import java . util . * ; class GFG { static int findLen ( int arr [ ] , int n , int m ) { Vector < Integer > filter = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] m ) == m ) filter . add ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_or = filter . get ( 0 ) ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_or |= filter . get ( i ) ; if ( c_or == m ) return filter . size ( ) ; return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . length ; int m = 3 ; System . out . print ( findLen ( arr , n , m ) ) ; } }
class GFG { static int toggleBit ( int n , int k ) { return ( n ^ ( 1 << ( k - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . printf ( "%dNEW_LINE", toggleBit(n, k)); } }
class GFG { static int clearBit ( int n , int k ) { return ( n & ( ~ ( 1 << ( k - 1 ) ) ) ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 1 ; System . out . println ( clearBit ( n , k ) ) ; } }
class GFG { final static int MAX = 32 ; static int maximizeExpression ( int a , int b ) { int result = a ; for ( int bit = MAX - 1 ; bit >= 0 ; bit -- ) { int bitOfD = 1 << bit ; int x = b & bitOfD ; if ( x == bitOfD ) { int y = result & bitOfD ; if ( y == 0 ) { result = result ^ bitOfD ; } } } return result ; } public static void main ( String [ ] args ) { int a = 11 , b = 14 ; System . out . println ( maximizeExpression ( a , b ) ) ; } }
import java . io . * ; class GFG { static int cntElements ( int arr [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] == ( arr [ i + 1 ] ^ arr [ i + 2 ] ) ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 , 7 , 8 } ; int n = arr . length ; System . out . println ( cntElements ( arr , n ) ) ; } }
class GFG { static int xor_triplet ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j ; k < n ; k ++ ) { int xor1 = 0 , xor2 = 0 ; for ( int x = i ; x < j ; x ++ ) { xor1 ^= arr [ x ] ; } for ( int x = j ; x <= k ; x ++ ) { xor2 ^= arr [ x ] ; } if ( xor1 == xor2 ) { ans ++ ; } } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( xor_triplet ( arr , n ) ) ; } }
class GFG { static int lg = 31 ; static class TrieNode { TrieNode children [ ] ; int sum_of_indexes ; int number_of_indexes ; TrieNode ( ) { children = new TrieNode [ 2 ] ; this . children [ 0 ] = null ; this . children [ 1 ] = null ; this . sum_of_indexes = 0 ; this . number_of_indexes = 0 ; } } ; static void insert ( TrieNode node , int num , int index ) { for ( int bits = lg ; bits >= 0 ; bits -- ) { int curr_bit = ( num >> bits ) & 1 ; if ( node . children [ curr_bit ] == null ) { node . children [ curr_bit ] = new TrieNode ( ) ; } node = node . children [ curr_bit ] ; } node . sum_of_indexes += index ; node . number_of_indexes ++ ; } static int query ( TrieNode node , int num , int index ) { for ( int bits = lg ; bits >= 0 ; bits -- ) { int curr_bit = ( num >> bits ) & 1 ; if ( node . children [ curr_bit ] == null ) { return 0 ; } node = node . children [ curr_bit ] ; } int sz = node . number_of_indexes ; int sum = node . sum_of_indexes ; int ans = ( sz * index ) - ( sum ) ; return ans ; } static int no_of_triplets ( int arr [ ] , int n ) { int curr_xor = 0 ; int number_of_triplets = 0 ; TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int x = arr [ i ] ; insert ( root , curr_xor , i ) ; curr_xor ^= x ; number_of_triplets += query ( root , curr_xor , i ) ; } return number_of_triplets ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 2 , 7 } ; int n = arr . length ; System . out . println ( no_of_triplets ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int MAX = 10 ; static int findSubarray ( int array [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; mp . put ( 0 , 1 ) ; int answer = 0 ; int preXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int value = 1 ; preXor ^= array [ i ] ; for ( int j = 1 ; j <= MAX ; j ++ ) { int Y = value ^ preXor ; if ( mp . containsKey ( Y ) ) { answer += mp . get ( Y ) ; } value *= 2 ; } if ( mp . containsKey ( preXor ) ) { mp . put ( preXor , mp . get ( preXor ) + 1 ) ; } else { mp . put ( preXor , 1 ) ; } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 2 , 6 , 7 , 5 , 8 } ; int n = array . length ; System . out . println ( findSubarray ( array , n ) ) ; } }
class GFG { static int max_xor ( int arr [ ] , int n ) { int maxXor = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { maxXor = Math . max ( maxXor , arr [ i ] ^ arr [ j ] ) ; } } return maxXor ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . length ; System . out . println ( max_xor ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int max_xor ( int arr [ ] , int n ) { int maxx = 0 , mask = 0 ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 30 ; i >= 0 ; i -- ) { mask |= ( 1 << i ) ; for ( int j = 0 ; j < n ; ++ j ) { se . add ( arr [ j ] & mask ) ; } int newMaxx = maxx | ( 1 << i ) ; for ( int prefix : se ) { if ( se . contains ( newMaxx ^ prefix ) ) { maxx = newMaxx ; break ; } } se . clear ( ) ; } return maxx ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , 10 , 2 , 8 , 5 , 3 } ; int n = arr . length ; System . out . println ( max_xor ( arr , n ) ) ; } }
class GFG { static int CountTriplets ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int first = arr [ i ] ; for ( int j = i + 1 ; j < n ; j ++ ) { first ^= arr [ j ] ; if ( first == 0 ) ans += ( j - i ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 6 , 4 , 2 } ; int n = arr . length ; System . out . println ( CountTriplets ( arr , n ) ) ; } }
class GFG { static void findMajority ( int arr [ ] , int n ) { int len = 32 ; int number = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) number += ( 1 << i ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > ( n / 2 ) ) System . out . println ( number ) ; else System . out . println ( " Majority ▁ Element ▁ Not ▁ Present " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 } ; int n = arr . length ; findMajority ( arr , n ) ; } }
import java . util . * ; class GFG { static int countBits ( int a , int b ) { int count = 0 ; while ( a > 0 b > 0 ) { int last_bit_a = a & 1 ; int last_bit_b = b & 1 ; if ( last_bit_a != last_bit_b ) count ++ ; a = a >> 1 ; b = b >> 1 ; } return count ; } public static void main ( String [ ] args ) { int a = 10 , b = 7 ; System . out . println ( countBits ( a , b ) ) ; } }
class GFG { static int CountSetBits ( int n ) { if ( n == 0 ) return 0 ; if ( ( n & 1 ) == 1 ) return 1 + CountSetBits ( n >> 1 ) ; else return CountSetBits ( n >> 1 ) ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( CountSetBits ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int getSum ( int BITree [ ] , int index ) { int sum = 0 ; while ( index > 0 ) { sum += BITree [ index ] ; index -= index & ( - index ) ; } return sum ; } public static void updateBIT ( int BITree [ ] , int n , int index , int val ) { while ( index <= n ) { BITree [ index ] += val ; index += index & ( - index ) ; } } public static void convert ( int arr [ ] , int n ) { int [ ] temp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = arr [ i ] ; Arrays . sort ( temp ) ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = Arrays . binarySearch ( temp , arr [ i ] ) + 1 ; } } public static void findElements ( int arr [ ] , int n ) { convert ( arr , n ) ; int [ ] BIT = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; int [ ] smaller_right = new int [ n ] ; int [ ] greater_left = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smaller_right [ i ] = getSum ( BIT , arr [ i ] - 1 ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } System . out . print ( " Smaller ▁ right : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( smaller_right [ i ] + " ▁ " ) ; System . out . println ( ) ; for ( int i = 1 ; i <= n ; i ++ ) BIT [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { greater_left [ i ] = i - getSum ( BIT , arr [ i ] ) ; updateBIT ( BIT , n , arr [ i ] , 1 ) ; } System . out . print ( " Greater ▁ left : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( greater_left [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 2 , 3 , 0 , 11 , 4 } ; int n = arr . length ; findElements ( arr , n ) ; } }
import java . util . * ; class GFG { static final int N = 100005 ; static int n , k ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] al = new Vector [ N ] ; static long Ideal_pair ; static long [ ] bit = new long [ N ] ; static boolean [ ] root_node = new boolean [ N ] ; static long bit_q ( int i , int j ) { long sum = 0 ; while ( j > 0 ) { sum += bit [ j ] ; j -= ( j & ( j * - 1 ) ) ; } i -- ; while ( i > 0 ) { sum -= bit [ i ] ; i -= ( i & ( i * - 1 ) ) ; } return sum ; } static void bit_up ( int i , long diff ) { while ( i <= n ) { bit [ i ] += diff ; i += i & - i ; } } static void dfs ( int node ) { Ideal_pair += bit_q ( Math . max ( 1 , node - k ) , Math . min ( n , node + k ) ) ; bit_up ( node , 1 ) ; for ( int i = 0 ; i < al [ node ] . size ( ) ; i ++ ) dfs ( al [ node ] . get ( i ) ) ; bit_up ( node , - 1 ) ; } static void initialise ( ) { Ideal_pair = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { root_node [ i ] = true ; bit [ i ] = 0 ; } } static void Add_Edge ( int x , int y ) { al [ x ] . add ( y ) ; root_node [ y ] = false ; } static long Idealpairs ( ) { int r = - 1 ; for ( int i = 1 ; i <= n ; i ++ ) if ( root_node [ i ] ) { r = i ; break ; } dfs ( r ) ; return Ideal_pair ; } public static void main ( String [ ] args ) { n = 6 ; k = 3 ; for ( int i = 0 ; i < al . length ; i ++ ) al [ i ] = new Vector < Integer > ( ) ; initialise ( ) ; Add_Edge ( 1 , 2 ) ; Add_Edge ( 1 , 3 ) ; Add_Edge ( 3 , 4 ) ; Add_Edge ( 3 , 5 ) ; Add_Edge ( 3 , 6 ) ; System . out . print ( Idealpairs ( ) ) ; } }
import java . io . * ; class GFG { static int pairORSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] | arr [ j ] ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( pairORSum ( arr , n ) ) ; } }
class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countPrimePosition ( int arr [ ] ) { int c0 = 0 , c1 = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } System . out . println ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; System . out . println ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }
public class GFG { static int N = 101 ; static void prefix ( int arr [ ] [ ] , int prefix_xor [ ] [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ^ prefix_xor [ i - 1 ] [ j ] ^ prefix_xor [ i ] [ j - 1 ] ^ prefix_xor [ i - 1 ] [ j - 1 ] ; } } } static void Max_xor ( int prefix_xor [ ] [ ] , int n ) { int max_value = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { for ( int i1 = i ; i1 <= n ; i1 ++ ) { for ( int j1 = j ; j1 <= n ; j1 ++ ) { int x = 0 ; x ^= prefix_xor [ i1 ] [ j1 ] ; x ^= prefix_xor [ i - 1 ] [ j - 1 ] ; x ^= prefix_xor [ i1 ] [ j - 1 ] ; x ^= prefix_xor [ i - 1 ] [ j1 ] ; max_value = Math . max ( max_value , x ) ; } } } } System . out . println ( max_value ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int n = 4 ; int prefix_xor [ ] [ ] = new int [ N ] [ N ] ; prefix ( arr , prefix_xor , n ) ; Max_xor ( prefix_xor , n ) ; } }
class GFG { static long multiplyByFifteen ( long n ) { long prod = ( n << 4 ) ; prod = prod - n ; return prod ; } public static void main ( String [ ] args ) { long n = 7 ; System . out . print ( multiplyByFifteen ( n ) ) ; } }
import java . lang . Math ; import java . util . * ; class GFG { public static void findX ( int [ ] a , int n ) { Collections . sort ( Arrays . asList ( a ) , null ) ; int itr = a [ n - 1 ] ; int p = ( int ) ( Math . log ( itr ) / Math . log ( 2 ) ) + 1 ; int x = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) { x += 1 << i ; } } long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( x ^ a [ i ] ) ; System . out . println ( " X ▁ = ▁ " + x + " , ▁ Sum ▁ = ▁ " + sum ) ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; findX ( a , n ) ; } }
import java . util . * ; class GFG { static int [ ] arr = new int [ 33 ] ; static void balTernary ( int ter ) { int carry = 0 , base = 10 ; int i = 32 ; while ( ter > 0 ) { int rem = ter % base ; rem = rem + carry ; if ( rem == 0 ) { arr [ i -- ] = 0 ; carry = 0 ; } else if ( rem == 1 ) { arr [ i -- ] = 1 ; carry = 0 ; } else if ( rem == 2 ) { arr [ i -- ] = - 1 ; carry = 1 ; } else if ( rem == 3 ) { arr [ i -- ] = 0 ; carry = 1 ; } ter = ( int ) ( ter / base ) ; } if ( carry == 1 ) arr [ i ] = 1 ; } static int ternary ( int number ) { int ans = 0 , rem = 1 , base = 1 ; while ( number > 0 ) { rem = number % 3 ; ans = ans + rem * base ; number = ( int ) ( number / 3 ) ; base = base * 10 ; } return ans ; } public static void main ( String args [ ] ) { int number = 3056 ; int ter = ternary ( number ) ; Arrays . fill ( arr , 0 ) ; balTernary ( ter ) ; int i = 0 ; while ( arr [ i ] == 0 ) { i ++ ; } for ( int j = i ; j <= 32 ; j ++ ) { if ( arr [ j ] == - 1 ) System . out . print ( ' Z ' ) ; else System . out . print ( arr [ j ] ) ; } } }
class GFG { static void minAND ( int [ ] arr , int n ) { int s = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { s = s & arr [ i ] ; } System . out . println ( s ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . length ; minAND ( arr , n ) ; } }
class GFG { static boolean check ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == 1 ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , 1 , 0 , 1 } ; int n = a . length ; if ( check ( a , n ) == true ) System . out . println ( "YESNEW_LINE") ; else System . out . println ( "NONEW_LINE"); } }
class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void main ( String args [ ] ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . length ; findArray ( q , n ) ; } }
import java . io . * ; class GFG { static boolean isEqualBlock ( int n ) { int first_bit = n % 2 ; int first_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit && n > 0 ) { n = n / 2 ; first_count ++ ; } if ( n == 0 ) return false ; while ( n > 0 ) { first_bit = n % 2 ; int curr_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit ) { n = n / 2 ; curr_count ++ ; } if ( curr_count != first_count ) return false ; } return true ; } public static void main ( String [ ] args ) { int n = 51 ; if ( isEqualBlock ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . math . * ; public class GFG { static void binToDecimal3 ( int n ) { int a [ ] = new int [ 64 ] ; int x = 0 , i ; for ( i = 0 ; n > 0 ; i ++ ) { a [ i ] = n % 2 ; n /= 2 ; } x = ( i < 3 ) ? 3 : i ; int d = 0 , p = 0 ; for ( int j = x - 3 ; j < x ; j ++ ) d += a [ j ] * Math . pow ( 2 , p ++ ) ; System . out . print ( d + " ▁ " ) ; d = 0 ; p = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) d += a [ k ] * Math . pow ( 2 , p ++ ) ; System . out . print ( d ) ; } public static void main ( String [ ] args ) { int n = 86 ; binToDecimal3 ( n ) ; } }
import java . util . * ; class GFG { static void binToDecimal3 ( int n ) { int last_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; n = n >> 3 ; while ( n > 7 ) n = n >> 1 ; int first_3 = ( ( n & 4 ) + ( n & 2 ) + ( n & 1 ) ) ; System . out . println ( first_3 + " ▁ " + last_3 ) ; } public static void main ( String args [ ] ) { int n = 86 ; binToDecimal3 ( n ) ; } }
import java . io . * ; class GFG { static boolean checkEvenParity ( int x ) { int parity = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; parity ++ ; } if ( parity % 2 == 0 ) return true ; else return false ; } static long sumlist ( int a [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( checkEvenParity ( a [ i ] ) ) sum += a [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 5 , 9 } ; int n = arr . length ; System . out . println ( sumlist ( arr , n ) ) ; } }
class GFG { static int findOddPair ( int A [ ] , int N ) { int oddPair = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] A [ j ] ) % 2 != 0 ) oddPair ++ ; } } return oddPair ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( findOddPair ( A , N ) ) ; } }
import java . io . * ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 4 , 7 , 2 , 1 } ; int N = A . length ; System . out . println ( findevenPair ( A , N ) ) ; } }
import java . io . * ; class GFG { static int findEvenPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 != 0 ) count ++ ; } int totalPairs = ( N * ( N - 1 ) / 2 ) ; int oddEvenPairs = count * ( N - count ) ; return totalPairs - oddEvenPairs ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 4 , 7 , 2 , 1 } ; int n = a . length ; System . out . println ( findEvenPair ( a , n ) ) ; } }
import java . io . * ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] & A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 1 , 3 , 2 } ; int n = a . length ; System . out . println ( findevenPair ( a , n ) ) ; } }
import java . io . * ; class GFG { static int findevenPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] % 2 != 0 ) count ++ ; int oddCount = count * ( count - 1 ) / 2 ; return ( N * ( N - 1 ) / 2 ) - oddCount ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 1 , 3 , 2 } ; int n = a . length ; System . out . print ( findevenPair ( a , n ) ) ; } }
import java . io . * ; class GFG { static int calculate ( int X ) { int number_of_bits = 8 ; return ( ( 1 << number_of_bits ) - 1 ) ^ X ; } public static void main ( String [ ] args ) { int X = 4 ; System . out . println ( " Required ▁ Number ▁ is ▁ : ▁ " + calculate ( X ) ) ; } }
import java . io . * ; class GFG { static void ReplaceElements ( int arr [ ] , int n ) { int X = 0 ; for ( int i = 0 ; i < n ; ++ i ) { X ^= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = X ^ arr [ i ] ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 5 , 5 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
import java . io . * ; class GFG { static int countLeadingZeros ( int x ) { int y ; int n ; n = 32 ; y = x >> 16 ; if ( y != 0 ) { n = n - 16 ; x = y ; } y = x >> 8 ; if ( y != 0 ) { n = n - 8 ; x = y ; } y = x >> 4 ; if ( y != 0 ) { n = n - 4 ; x = y ; } y = x >> 2 ; if ( y != 0 ) { n = n - 2 ; x = y ; } y = x >> 1 ; if ( y != 0 ) return n - 2 ; return n - x ; } static int FindStringof1s ( int x , int n ) { int k , p ; p = 0 ; while ( x != 0 ) { k = countLeadingZeros ( x ) ; x = x << k ; p = p + k ; k = countLeadingZeros ( ~ x ) ; if ( k >= n ) return p + 1 ; x = x << k ; p = p + k ; } return - 1 ; } public static void main ( String [ ] args ) { int x = 35 ; int n = 2 ; System . out . println ( FindStringof1s ( x , n ) ) ; } }
class GFG { static byte sizeofInt = 8 ; static int countZeros ( int x ) { int total_bits = sizeofInt * 8 ; int res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } public static void main ( String [ ] args ) { int x = 101 ; System . out . println ( countZeros ( x ) ) ; } }
class GFG { static void LeadingZeros ( int x , int y ) { if ( ( x ^ y ) <= ( x & y ) ) System . out . print ( " Equal "); else if ( ( x & ( ~ y ) ) > y ) System . out . print ( y ) ; else System . out . print ( x ) ; } public static void main ( String [ ] args ) { int x = 10 , y = 16 ; LeadingZeros ( x , y ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int distintBitwiseOR ( int arr [ ] , int n ) { HashSet < Integer > ans = new HashSet < > ( ) ; HashSet < Integer > prev = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { HashSet < Integer > ne = new HashSet < > ( ) ; ne . add ( arr [ i ] ) ; for ( int x : prev ) { ne . add ( arr [ i ] x ) ; } for ( int x : ne ) { ans . add ( x ) ; } prev = ne ; } return ans . size ( ) ; } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] = { 1 , 2 , 4 } ; System . out . println ( distintBitwiseOR ( arr , n ) ) ; } }
class GfG { static void printSubSets ( int numOfBits , int num ) { if ( num >= 0 ) { System . out . print ( " { ▁ " ) ; subset ( numOfBits - 1 , num , numOfBits ) ; System . out . println ( " } " ) ; printSubSets ( numOfBits , num - 1 ) ; } else return ; } static void subset ( int nthBit , int num , int numOfBits ) { if ( nthBit >= 0 ) { if ( ( num & ( 1 << nthBit ) ) != 0 ) { System . out . print ( numOfBits - nthBit + " ▁ " ) ; } subset ( nthBit - 1 , num , numOfBits ) ; } else return ; } public static void main ( String [ ] args ) { int n = 4 ; printSubSets ( n , ( int ) ( Math . pow ( 2 , n ) ) - 1 ) ; } }
public class GFG { static int setRightmostUnsetBit ( int n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; return n | ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 21 ; System . out . println ( setRightmostUnsetBit ( n ) ) ; } }
public class GFG { static int AND ( int a [ ] , int n ) { int ans = a [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) ans &= a [ i ] ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; System . out . println ( AND ( a , n ) ) ; } }
import java . util . * ; class GFG { static void TwoscomplementbyXOR ( String str ) { int n = str . length ( ) ; boolean check_bit = false ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' && check_bit == false ) { continue ; } else { if ( check_bit == true ) { if ( str . charAt ( i ) == '0' ) str = str . substring ( 0 , i ) + '1' + str . substring ( i + 1 ) ; else str = str . substring ( 0 , i ) + '0' + str . substring ( i + 1 ) ; } check_bit = true ; } } if ( check_bit == false ) { System . out . println ( "1" + str ) ; } else System . out . println ( str ) ; } public static void main ( String [ ] args ) { String str = "101" ; TwoscomplementbyXOR ( str ) ; } }
class GFG { static boolean isKthBitSet ( int n , int k ) { if ( ( n >> ( k - 1 ) ) == 1 ) return true ; return false ; } static int setKthBit ( int n , int k ) { return ( ( 1 << ( k - 1 ) ) n ) ; } static boolean allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static boolean bitsAreInAltOrder ( int n ) { int num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } static boolean bitsAreInAltPatrnInGivenRange ( int n , int l , int r ) { int num , left_shift ; if ( isKthBitSet ( n , r ) ) { num = n ; left_shift = r ; } else { num = setKthBit ( n , ( r + 1 ) ) ; left_shift = r + 1 ; } num = num & ( ( 1 << left_shift ) - 1 ) ; num = num >> ( l - 1 ) ; return bitsAreInAltOrder ( num ) ; } public static void main ( String [ ] args ) { int n = 18 ; int l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static boolean bitsAreInAltPatrnInGivenTRange ( int n , int l , int r ) { int num , prev , curr ; num = n >> ( l - 1 ) ; prev = num & 1 ; num = num >> 1 ; for ( int i = 1 ; i <= ( r - l ) ; i ++ ) { curr = num & 1 ; if ( curr == prev ) return false ; prev = curr ; num = num >> 1 ; } return true ; } public static void main ( String [ ] args ) { int n = 18 ; int l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenTRange ( n , l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static long increment ( long i ) { i = - ( ~ i ) ; return i ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . print ( increment ( n ) ) ; } }
class GFG { static int bit_count ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n & ( n - 1 ) ; } return count ; } static int maxsum ( int [ ] arr , int n ) { int [ ] bits = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { bits [ i ] = bit_count ( arr [ i ] ) ; } int [ ] sum = new int [ 32 ] ; for ( int i = 0 ; i < n ; i ++ ) { sum [ bits [ i ] ] += arr [ i ] ; } int maximum = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { maximum = Math . max ( sum [ i ] , maximum ) ; } return maximum ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 8 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( maxsum ( arr , n ) ) ; } }
class GFG { static int xorPairSum ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum ^ ar [ i ] ; return 2 * sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( xorPairSum ( arr , n ) ) ; } }
import java . io . * ; class GFG { static boolean checkSame ( int n ) { int set = 0 ; int unset = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 1 ) set ++ ; else unset ++ ; n = n >> 1 ; } if ( set == unset ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 12 ; if ( checkSame ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
import java . util . Stack ; class GFG { static boolean isBalanced ( String s ) { Stack < Character > st = new Stack < Character > ( ) ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) { st . push ( s . charAt ( i ) ) ; } else if ( st . empty ( ) ) { return false ; } else { st . pop ( ) ; } } if ( ! st . empty ( ) ) { return false ; } return true ; } static boolean isBalancedSeq ( String s1 , String s2 ) { if ( isBalanced ( s1 + s2 ) ) { return true ; } return isBalanced ( s2 + s1 ) ; } public static void main ( String [ ] args ) { String s1 = " ) ( ) ( ( ) ) ) ) " ; String s2 = " ( ( ) ( ( ) ( " ; if ( isBalancedSeq ( s1 , s2 ) ) { System . out . println ( " Balanced " ) ; } else { System . out . println ( " Not ▁ Balanced " ) ; } } }
import java . io . * ; class GFG { static void KthCharacter ( int m , int n , int k ) { int distance = ( int ) Math . pow ( 2 , n ) ; int Block_number = k / distance ; int remaining = k % distance ; int s [ ] = new int [ 32 ] ; int x = 0 ; for ( ; m > 0 ; x ++ ) { s [ x ] = m % 2 ; m = m / 2 ; } int root = s [ x - 1 - Block_number ] ; if ( remaining == 0 ) { System . out . println ( root ) ; return ; } Boolean flip = true ; while ( remaining > 1 ) { if ( ( remaining & 1 ) > 0 ) { flip = ! flip ; } remaining = remaining >> 1 ; } if ( flip ) { System . out . println ( ( root > 0 ) ? 0 : 1 ) ; } else { System . out . println ( root ) ; } } public static void main ( String [ ] args ) { int m = 5 , k = 5 , n = 3 ; KthCharacter ( m , n , k ) ; } }
import java . io . * ; class GFG { static int getInteger ( int L , int R ) { int number = 0 ; for ( int i = L ; i <= R ; i ++ ) number += Math . pow ( 2 , i ) ; return number ; } public static void main ( String [ ] args ) { int L = 2 , R = 5 ; System . out . println ( getInteger ( L , R ) ) ; } }
import java . io . * ; class GFG { static int setbitsfromLtoR ( int L , int R ) { return ( 1 << ( R + 1 ) ) - ( 1 << L ) ; } public static void main ( String [ ] args ) { int L = 2 , R = 5 ; System . out . println ( setbitsfromLtoR ( L , R ) ) ; } }
import java . io . * ; class GFG { static int findXor ( int arr [ ] , int n ) { int xoR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xoR = xoR ^ arr [ i ] ; } return xoR * 2 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 6 } ; int n = arr . length ; System . out . println ( findXor ( arr , n ) ) ; } }
import java . util . * ; class GFG { static void setBitwiseORk ( int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( i n ) == n ) { v . add ( i ) ; } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; setBitwiseORk ( n ) ; } }
class GFG { static void printOdds ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = res ^ arr [ i ] ; int set_bit = res & ( ~ ( res - 1 ) ) ; int x = 0 , y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & set_bit ) != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } System . out . println ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 4 , 4 , 5 } ; int n = arr . length ; printOdds ( arr , n ) ; } }
import java . util . * ; class GFG { static void subsetBitwiseORk ( int arr [ ] , int n , int k ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] k ) == k ) { v . add ( arr [ i ] ) ; } } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans = ans | v . get ( i ) ; if ( ans != k ) { System . out . println ( " Subset ▁ does " + " ▁ not ▁ exist " ) ; return ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int k = 3 ; int arr [ ] = { 1 , 4 , 2 } ; int n = arr . length ; subsetBitwiseORk ( arr , n , k ) ; } }
import java . io . * ; class GFG { static int findEletobeInserted ( int A [ ] , int n , int k ) { int ans = k ; for ( int i = 0 ; i < n ; i ++ ) ans ^= A [ i ] ; return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = A . length ; int k = 10 ; System . out . println ( findEletobeInserted ( A , n , k ) + " ▁ has ▁ to ▁ be ▁ inserted ▁ in ▁ " + " the ▁ given ▁ array ▁ to ▁ make " + " ▁ xor ▁ sum ▁ of ▁ " + k ) ; } }
import java . io . * ; class GFG { static int BitCount [ ] = new int [ 10000 ] ; static void fillSetBitsMatrix ( int arr [ ] , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { long temp = arr [ j ] >> i ; if ( temp % 2 != 0 ) BitCount [ j ] += 1 ; } } for ( int i = 1 ; i < n ; i ++ ) BitCount [ i ] += BitCount [ i - 1 ] ; } static void Query ( int Q [ ] [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ( BitCount [ Q [ i ] [ 1 ] ] - BitCount [ Q [ i ] [ 0 ] - 1 ] ) ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 1 , 5 , 6 , 10 , 9 , 4 , 67 } ; int n = Arr . length ; fillSetBitsMatrix ( Arr , n ) ; int q = 2 ; int Q [ ] [ ] = { { 1 , 5 } , { 2 , 6 } } ; Query ( Q , q ) ; } }
import java . io . * ; class GFG { static void decimalToBinaryNumber ( int x , int n ) { int [ ] binaryNumber = new int [ x ] ; int i = 0 ; while ( x > 0 ) { binaryNumber [ i ] = x % 2 ; x = x / 2 ; i ++ ; } for ( int j = 0 ; j < n - i ; j ++ ) System . out . print ( '0' ) ; for ( int j = i - 1 ; j >= 0 ; j -- ) System . out . print ( binaryNumber [ j ] ) ; } static void generateGrayarr ( int n ) { int N = 1 << n ; for ( int i = 0 ; i < N ; i ++ ) { int x = i ^ ( i >> 1 ) ; decimalToBinaryNumber ( x , n ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; generateGrayarr ( n ) ; } }
class GFG { static final int BITS = 32 ; static int andSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < BITS ; i ++ ) { int countSetBits = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countSetBits ++ ; } int subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int size = 3 ; System . out . println ( andSum ( arr , size ) ) ; } }
import java . io . * ; class GFG { static long _popcnt32 ( long n ) { long count = 0 ; while ( n != 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static long maximize ( long a ) { long n = _popcnt32 ( a ) ; if ( n == 32 ) return a ; long res = ( 1 << n ) - 1 ; return ( res << ( 32 - n ) ) ; } public static void main ( String args [ ] ) { long a = 3 ; System . out . print ( maximize ( a ) ) ; } }
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int i = 3 ; System . out . print ( countSetBits ( i ) ) ; } }
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 " ) ; } }
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 " ) ; } }
import java . io . * ; class GFG { static boolean checkValidPair ( int num1 , int num2 ) { String s1 = Integer . toString ( num1 ) ; String s2 = Integer . toString ( num2 ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) for ( int j = 0 ; j < s2 . length ( ) ; j ++ ) if ( s1 . charAt ( i ) == s2 . charAt ( j ) ) return true ; return false ; } static int countPairs ( int [ ] arr , int n ) { int numberOfPairs = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( checkValidPair ( arr [ i ] , arr [ j ] ) ) numberOfPairs ++ ; return numberOfPairs ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 10 , 12 , 24 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int NUM_BITS = 32 ; static boolean isPowerOf2 ( int num ) { if ( num != 0 && ( num & ( num - 1 ) ) == 0 ) return true ; return false ; } static boolean checkSubsequence ( int [ ] arr , int n ) { if ( n == 1 ) return isPowerOf2 ( arr [ 0 ] ) ; int total = 0 ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) total = total | ( 1 << i ) ; for ( int i = 0 ; i < NUM_BITS ; i ++ ) { int ans = total ; for ( int j = 0 ; j < n ; j ++ ) { int p = arr [ j ] & ( 1 << i ) ; if ( p == 0 ) ans = ans & arr [ j ] ; } if ( isPowerOf2 ( ans ) ) return true ; } return false ; } public static void main ( String args [ ] ) { int [ ] arr = { 12 , 13 , 7 } ; int n = arr . length ; if ( checkSubsequence ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
class GFG { static String findWinner ( int A [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= A [ i ] ; if ( res == 0 n % 2 == 0 ) return " Alice " ; else return " Bob " ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 3 , 5 } ; int n = A . length ; System . out . print ( " Winner ▁ = ▁ " + findWinner ( A , n ) ) ; } }
class GFG { static void printSubsets ( int n ) { for ( int i = n ; i > 0 ; i = ( i - 1 ) & n ) System . out . print ( i + " ▁ " ) ; System . out . print ( " ▁ 0 ▁ " ) ; } public static void main ( String [ ] args ) { int n = 9 ; printSubsets ( n ) ; } }
import java . lang . * ; class GFG { static int maxXOR ( int n , int k ) { int c = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; return ( ( 1 << c ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 12 ; int k = 3 ; System . out . println ( maxXOR ( n , k ) ) ; } }
import java . io . * ; class GFG { static int count ( int n ) { int c = 0 ; while ( n != 0 ) { c ++ ; n = n >> 1 ; } return c ; } static int XOR ( int a , int b ) { int c = Math . min ( a , b ) ; int d = Math . max ( a , b ) ; if ( count ( c ) < count ( d ) ) c = c << ( count ( d ) - count ( c ) ) ; return ( c ^ d ) ; } public static void main ( String args [ ] ) { int a = 13 , b = 5 ; System . out . println ( XOR ( a , b ) ) ; } }
import java . util . * ; class GFG { static void CheckDivisibilty ( String A ) { int oddbits = 0 , evenbits = 0 ; for ( int counter = 0 ; counter < A . length ( ) ; counter ++ ) { if ( A . charAt ( counter ) == '1' ) { if ( counter % 2 == 0 ) { evenbits ++ ; } else { oddbits ++ ; } } } if ( Math . abs ( oddbits - evenbits ) % 3 == 0 ) { System . out . print ( " Yes " + "NEW_LINE"); } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { String A = "10101" ; CheckDivisibilty ( A ) ; } }
import java . util . * ; class GFG { static int swapBitsInPair ( int x ) { return ( ( x & 0b10101010 ) >> 1 ) | ( ( x & 0b01010101 ) << 1 ) ; } public static void main ( String [ ] args ) { int x = 4 ; System . out . print ( swapBitsInPair ( x ) ) ; } }
import java . io . * ; class GFG { static int setevenbits ( int n ) { int temp = n ; int count = 0 ; int res = 0 ; for ( temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return ( n & res ) ; } static int setoddbits ( int m ) { int count = 0 ; int res = 0 ; for ( int temp = m ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return ( m & res ) ; } static int getAlternateBits ( int n , int m ) { int tempn = setevenbits ( n ) ; int tempm = setoddbits ( m ) ; return ( tempn tempm ) ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 11 ; System . out . println ( getAlternateBits ( n , m ) ) ; } }
import java . io . * ; class GFG { static boolean isDivisibleBy10 ( char bin [ ] , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; int sum = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( bin [ i ] == '1' ) { int posFromRight = n - i - 1 ; if ( posFromRight % 4 == 1 ) sum = sum + 2 ; else if ( posFromRight % 4 == 2 ) sum = sum + 4 ; else if ( posFromRight % 4 == 3 ) sum = sum + 8 ; else if ( posFromRight % 4 == 0 ) sum = sum + 6 ; } } if ( sum % 10 == 0 ) return true ; return false ; } static boolean isDivisibleBy20 ( char bin [ ] , int n ) { if ( bin [ n - 1 ] == '1' ) return false ; return isDivisibleBy10 ( bin , n - 1 ) ; } public static void main ( String args [ ] ) { char bin [ ] = "101000" . toCharArray ( ) ; int n = bin . length ; if ( isDivisibleBy20 ( bin , n - 1 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int toggleBits ( int n1 , int n2 ) { return ( n1 ^ n2 ) ; } public static void main ( String args [ ] ) { int n1 = 2 , n2 = 5 ; System . out . println ( toggleBits ( n1 , n2 ) ) ; } }
import java . io . * ; class GFG { static int evenbittogglenumber ( int n ) { int res = 0 , count = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 1 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } public static void main ( String args [ ] ) { int n = 11 ; System . out . println ( evenbittogglenumber ( n ) ) ; } }
import java . io . * ; class GFG { static int takeLandFsetbits ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( ( n + 1 ) >> 1 ) + 1 ; } static int toggleFandLbits ( int n ) { if ( n == 1 ) return 0 ; return n ^ takeLandFsetbits ( n ) ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( toggleFandLbits ( n ) ) ; } }
class GFG { public static int reverseBits ( int n ) { int rev = 0 ; while ( n > 0 ) { rev <<= 1 ; if ( ( int ) ( n & 1 ) == 1 ) rev ^= 1 ; n >>= 1 ; } return rev ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . println ( reverseBits ( n ) ) ; } }
import java . io . * ; import java . math . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static boolean checkOdious ( int n ) { return ( countSetBits ( n ) % 2 == 1 ) ; } public static void main ( String args [ ] ) { int num = 32 ; if ( checkOdious ( num ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static int setleftmostunsetbit ( int n ) { if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = 0 ; for ( int temp = n , count = 0 ; temp > 0 ; temp >>= 1 , count ++ ) if ( ( temp & 1 ) == 0 ) pos = count ; return ( n | ( 1 << ( pos ) ) ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( setleftmostunsetbit ( n ) ) ; } }
public class Main { static int maxXorSum ( int n , int k ) { if ( k == 1 ) return n ; int res = 1 ; while ( res <= n ) res <<= 1 ; return res - 1 ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; System . out . print ( maxXorSum ( n , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getPosOfRightmostSetBit ( int n ) { return ( int ) ( Math . log ( n & - n ) / Math . log ( 2 ) ) ; } static int toggleLastKBits ( int n , int k ) { int num = ( 1 << k ) - 1 ; return ( n ^ num ) ; } static int incrementByOne ( int n ) { int k = getPosOfRightmostSetBit ( ~ n ) ; n = ( ( 1 << k ) n ) ; if ( k != 0 ) n = toggleLastKBits ( n , k ) ; return n ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( incrementByOne ( n ) ) ; } }
import java . io . * ; class GFG { static int togglebit ( int n ) { if ( n == 0 ) return 1 ; int i = n ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return i ^ n ; } static int xnor ( int num1 , int num2 ) { if ( num1 < num2 ) { int temp = num1 ; num1 = num2 ; num2 = temp ; } num1 = togglebit ( num1 ) ; return num1 ^ num2 ; } public static void main ( String [ ] args ) { int a = 10 , b = 20 ; System . out . println ( xnor ( a , b ) ) ; } }
class GFG { static void MaximumSum ( int a [ ] , int b [ ] , int n ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 |= a [ i ] ; sum2 |= b [ i ] ; } System . out . println ( sum1 + sum2 ) ; } public static void main ( String arg [ ] ) { int A [ ] = { 1 , 2 , 4 , 3 , 2 } ; int B [ ] = { 2 , 3 , 3 , 12 , 1 } ; int n = A . length ; MaximumSum ( A , B , n ) ; } }
class GFG { static int posOfRightmostSetBit ( int n ) { return ( int ) ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ; } static int posOfCarryBit ( int a , int b ) { return posOfRightmostSetBit ( a & b ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 2 ; System . out . print ( posOfCarryBit ( a , b ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean isPowerOfTwo ( int x ) { return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ; } static boolean differAtOneBitPos ( int a , int b ) { return isPowerOfTwo ( a ^ b ) ; } public static void main ( String args [ ] ) { int a = 13 , b = 9 ; if ( differAtOneBitPos ( a , b ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static long power2 ( long n ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return 2 ; return power2 ( n / 2 ) * power2 ( n / 2 ) ; } static long multiply ( long x , long n ) { return x * power2 ( n ) ; } public static void main ( String [ ] args ) { long x = 70 , n = 2 ; System . out . println ( multiply ( x , n ) ) ; } }
import java . util . * ; class GFG { static long multiply ( long x , long n ) { return x << n ; } public static void main ( String [ ] args ) { long x = 70 , n = 2 ; System . out . println ( multiply ( x , n ) ) ; } }
import java . util . * ; class GFG { static boolean isDivBy2PowerM ( int n , int m ) { if ( ( n & ( ( 1 << m ) - 1 ) ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 8 , m = 2 ; if ( isDivBy2PowerM ( n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int findWinner ( int N ) { return N & 1 ; } public static void main ( String [ ] args ) { int N = 15 ; if ( findWinner ( N ) == 1 ) System . out . println ( " Player ▁ A " ) ; else System . out . println ( " Player ▁ B " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countgroup ( int a [ ] , int n ) { int xs = 0 ; for ( int i = 0 ; i < n ; i ++ ) xs = xs ^ a [ i ] ; if ( xs == 0 ) return ( 1 << ( n - 1 ) ) - 1 ; return 0 ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; System . out . println ( countgroup ( a , n ) ) ; } }
import java . io . * ; class GFG { static int evenbittogglenumber ( int n ) { int res = 0 , count = 0 ; for ( int temp = n ; temp > 0 ; temp >>= 1 ) { if ( count % 2 == 0 ) res |= ( 1 << count ) ; count ++ ; } return n ^ res ; } public static void main ( String args [ ] ) { int n = 11 ; System . out . println ( evenbittogglenumber ( n ) ) ; } }
class GFG { static boolean isDivisibleby17 ( int n ) { if ( n == 0 n == 17 ) return true ; if ( n < 17 ) return false ; return isDivisibleby17 ( ( int ) ( n >> 4 ) - ( int ) ( n & 15 ) ) ; } public static void main ( String [ ] args ) { int n = 35 ; if ( isDivisibleby17 ( n ) == true ) System . out . printf ( " % d ▁ is ▁ divisible ▁ by ▁ 17" , n ) ; else System . out . printf ( " % d ▁ is ▁ not ▁ divisible ▁ by ▁ 17" , n ) ; } }
public class GFG { static long answer ( long n ) { long m = 2 ; long ans = 1 ; long r = 1 ; while ( r < n ) { r = ( ( long ) Math . pow ( 2 , m ) - 1 ) * ( ( long ) Math . pow ( 2 , m - 1 ) ) ; if ( r < n ) ans = r ; m ++ ; } return ans ; } public static void main ( String args [ ] ) { long n = 7 ; System . out . println ( answer ( n ) ) ; } }
class GFG { static int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } static void check ( int n ) { String s [ ] = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; System . out . println ( n + " ▁ is ▁ " + s [ val ] ) ; } public static void main ( String [ ] args ) { check ( 30 ) ; check ( - 20 ) ; check ( 0 ) ; } }
class GFG { static void compute ( long S , long X ) { long A = ( S - X ) / 2 ; int a = 0 , b = 0 ; final int LONG_FIELD_SIZE = 8 ; for ( int i = 0 ; i < 8 * LONG_FIELD_SIZE ; i ++ ) { long Xi = ( X & ( 1 << i ) ) ; long Ai = ( A & ( 1 << i ) ) ; if ( Xi == 0 && Ai == 0 ) { } else if ( Xi == 0 && Ai > 0 ) { a = ( ( 1 << i ) a ) ; b = ( ( 1 << i ) b ) ; } else if ( Xi > 0 && Ai == 0 ) { a = ( ( 1 << i ) a ) ; } { System . out . println ( " Not ▁ Possible " ) ; return ; } } System . out . println ( " a ▁ = ▁ " + a + " b = " + b);  } public static void main ( String [ ] args ) { long S = 17 , X = 13 ; compute ( S , X ) ; } }
import java . io . * ; class GFG { static boolean checking ( String s ) { int c = 0 ; int n = s . length ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '0' ) c ++ ; if ( c >= 6 && s . charAt ( i ) == '1' ) return true ; } return false ; } public static void main ( String [ ] args ) { String s = "100010001" ; if ( checking ( s ) ) System . out . println ( " Possible " ) ; else System . out . println ( " Not ▁ possible " ) ; } }
import java . io . * ; class GFG { public static int modifyBit ( int n , int p , int b ) { int mask = 1 << p ; return ( n & ~ mask ) | ( ( b << p ) & mask ) ; } public static void main ( String [ ] args ) { System . out . println ( modifyBit ( 6 , 2 , 0 ) ) ; System . out . println ( modifyBit ( 6 , 5 , 1 ) ) ; } }
class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countSetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return countSetBits ( n & num ) ; } public static void main ( String [ ] args ) { int n = 42 ; int l = 2 , r = 5 ; System . out . print ( countSetBitsInGivenRange ( n , l , r ) ) ; } }
class GFG { static int findUnique ( int a [ ] , int n , int k ) { byte sizeof_int = 4 ; int INT_SIZE = 8 * sizeof_int ; int count [ ] = new int [ INT_SIZE ] ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count [ i ] += 1 ; int res = 0 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) res += ( count [ i ] % k ) * ( 1 << i ) ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 2 , 5 , 2 , 2 , 6 , 6 } ; int n = a . length ; int k = 3 ; System . out . println ( findUnique ( a , n , k ) ) ; } }
import java . util . * ; class GFG { static boolean powerOfTwo ( int n ) { return ( ( n & n - 1 ) == 0 ) ; } static boolean onlyFirstAndLastAreSet ( int n ) { if ( n == 1 ) return true ; return powerOfTwo ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = Integer . parseUnsignedInt ( "9" ) ; if ( onlyFirstAndLastAreSet ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class AlternateSetBits { static boolean allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static boolean bitsAreInAltOrder ( int n ) { int num = n ^ ( n >>> 1 ) ; return allBitsAreSet ( num ) ; } public static void main ( String args [ ] ) { int n = 10 ; if ( bitsAreInAltOrder ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static int setBit ( int xorValue ) { int count = 0 ; while ( xorValue >= 1 ) { if ( xorValue % 2 == 1 ) count ++ ; xorValue /= 2 ; } return count ; } static int minFlip ( int n , int k ) { int size = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) + 1 ; int max = ( int ) Math . pow ( 2 , k ) - 1 ; max = max << ( size - k ) ; int xorValue = ( n ^ max ) ; return ( setBit ( xorValue ) ) ; } public static void main ( String [ ] args ) { int n = 27 , k = 3 ; System . out . println ( " Min ▁ Flips ▁ = ▁ " + minFlip ( n , k ) ) ; } }
import java . util . * ; class GFG { static int setallbitgivenrange ( int n , int l , int r ) { int range = ( ( ( 1 << ( l - 1 ) ) - 1 ) ^ ( ( 1 << ( r ) ) - 1 ) ) ; return ( n range ) ; } public static void main ( String [ ] args ) { int n = 17 , l = 2 , r = 3 ; System . out . println ( setallbitgivenrange ( n , l , r ) ) ; } }
import java . io . * ; class GFG { static int setBitNumber ( int n ) { if ( n == 0 ) return 0 ; int msb = 0 ; n = n / 2 ; while ( n != 0 ) { n = n / 2 ; msb ++ ; } return ( 1 << msb ) ; } public static void main ( String [ ] args ) { int n = 0 ; System . out . println ( setBitNumber ( n ) ) ; } }
class GFG { static int setBitNumber ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n = n + 1 ; return ( n >> 1 ) ; } public static void main ( String arg [ ] ) { int n = 273 ; System . out . print ( setBitNumber ( n ) ) ; } }
import java . io . * ; class GFG { static String allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( new_num == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 17 ; int l = 2 ; int r = 4 ; System . out . println ( allBitsSetInTheGivenRange ( n , l , r ) ) ; } }
class GFG { static boolean adjacentSet ( int n ) { int x = ( n & ( n >> 1 ) ) ; if ( x > 0 ) return true ; else return false ; } public static void main ( String args [ ] ) { int n = 3 ; if ( adjacentSet ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int getRightMostSetBit ( int n ) { return ( int ) ( ( Math . log ( n & - n ) ) / ( Math . log ( 2 ) ) ) + 1 ; } static int posOfRightMostSameBit ( int m , int n ) { return getRightMostSetBit ( ~ ( m ^ n ) ) ; } public static void main ( String [ ] args ) { int m = 16 , n = 7 ; System . out . print ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }
class GFG { static int posOfRightMostSameBit ( int m , int n ) { int loopCounter = 1 ; while ( m > 0 n > 0 ) { boolean a = m % 2 == 1 ; boolean b = n % 2 == 1 ; if ( ! ( a ^ b ) ) { return loopCounter ; } m = m >> 1 ; n = n >> 1 ; loopCounter ++ ; } return - 1 ; } public static void main ( String [ ] args ) { int m = 16 , n = 7 ; System . out . print ( " Position ▁ = ▁ " + posOfRightMostSameBit ( m , n ) ) ; } }
class GFG { static String allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( num == new_num ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 22 ; int l = 2 , r = 3 ; System . out . print ( allBitsSetInTheGivenRange ( n , l , r ) ) ; } }
import java . util . * ; class GFG { static void printNonConsecutive ( int n ) { int p = ( 1 << n ) ; for ( int i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 ; printNonConsecutive ( n ) ; } }
class GFG { static void shuffle ( int N , int key ) { int NO_OF_BITS = N ; int reverse_num = 0 , temp ; for ( int i = 0 ; i < NO_OF_BITS ; i ++ ) { temp = ( key & ( 1 << i ) ) ; if ( temp > 0 ) reverse_num |= ( 1 << ( ( NO_OF_BITS - 1 ) - i ) ) ; } System . out . print ( reverse_num ) ; } public static void main ( String [ ] args ) { int N = 3 ; int key = 3 ; shuffle ( N , key ) ; } }
public class Main { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }
class GFG { static int findMax ( int num ) { byte size_of_int = 4 ; int num_copy = num ; int j = size_of_int * 8 - 1 ; int i = 0 ; while ( i < j ) { int m = ( num_copy >> i ) & 1 ; int n = ( num_copy >> j ) & 1 ; if ( m > n ) { int x = ( 1 << i 1 << j ) ; num = num ^ x ; } i ++ ; j -- ; } return num ; } static public void main ( String [ ] args ) { int num = 4 ; System . out . println ( findMax ( num ) ) ; } }
class GFG { static boolean isAMultipleOf4 ( int n ) { if ( ( n & 3 ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( isAMultipleOf4 ( n ) ? " Yes " : " No " ) ; } }
class GFG { static int getPosOfRightmostSetBit ( int n ) { return ( int ) ( ( Math . log10 ( n & - n ) ) / ( Math . log10 ( 2 ) ) ) + 1 ; } static int setRightmostUnsetBit ( int n ) { if ( n == 0 ) return 1 ; if ( ( n & ( n + 1 ) ) == 0 ) return n ; int pos = getPosOfRightmostSetBit ( ~ n ) ; return ( ( 1 << ( pos - 1 ) ) n ) ; } public static void main ( String arg [ ] ) { int n = 21 ; System . out . print ( setRightmostUnsetBit ( n ) ) ; } }
class GFG { static int getFirstSetBitPos ( int n ) { return ( int ) ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ; } static int previousSmallerInteger ( int n ) { int pos = getFirstSetBitPos ( n ) ; return ( n & ~ ( 1 << ( pos - 1 ) ) ) ; } public static void main ( String [ ] args ) { int n = 25 ; System . out . print ( " Previous ▁ smaller ▁ Integer ▁ = " + previousSmallerInteger ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void createMap ( Map < String , Character > um ) { um . put ( "000" , '0' ) ; um . put ( "001" , '1' ) ; um . put ( "010" , '2' ) ; um . put ( "011" , '3' ) ; um . put ( "100" , '4' ) ; um . put ( "101" , '5' ) ; um . put ( "110" , '6' ) ; um . put ( "111" , '7' ) ; } static String convertBinToOct ( String bin ) { int l = bin . length ( ) ; int t = bin . indexOf ( ' . ' ) ; int len_left = t != - 1 ? t : l ; for ( int i = 1 ; i <= ( 3 - len_left % 3 ) % 3 ; i ++ ) bin = '0' + bin ; if ( t != - 1 ) { int len_right = l - len_left - 1 ; for ( int i = 1 ; i <= ( 3 - len_right % 3 ) % 3 ; i ++ ) bin = bin + '0' ; } Map < String , Character > bin_oct_map = new HashMap < String , Character > ( ) ; createMap ( bin_oct_map ) ; int i = 0 ; String octal = " " ; while ( true ) { octal += bin_oct_map . get ( bin . substring ( i , i + 3 ) ) ; i += 3 ; if ( i == bin . length ( ) ) break ; if ( bin . charAt ( i ) == ' . ' ) { octal += ' . ' ; i ++ ; } } return octal ; } public static void main ( String [ ] args ) { String bin = "1111001010010100001.010110110011011" ; System . out . println ( " Octal ▁ number ▁ = ▁ " + convertBinToOct ( bin ) ) ; } }
import java . io . * ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) return " No " ; n = n >> 1 ; } return " Yes " ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( areAllBitsSet ( n ) ) ; } }
import java . io . * ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; if ( ( ( n + 1 ) & n ) == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( areAllBitsSet ( n ) ) ; } }
class GFG { static int getFirstSetBitPos ( int n ) { return ( ( int ) ( Math . log ( n & - n ) / Math . log ( 2 ) ) + 1 ) - 1 ; } static int nextGreaterWithOneMoreSetBit ( int n ) { int pos = getFirstSetBitPos ( ~ n ) ; if ( pos > - 1 ) return ( 1 << pos ) | n ; return ( ( n << 1 ) + 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . print ( " Next ▁ greater ▁ integer ▁ = ▁ " + nextGreaterWithOneMoreSetBit ( n ) ) ; } }
import java . io . * ; class GFG { static long CountZeroBit ( long x ) { long count = 0 ; while ( x > 0 ) { if ( ( x & 1L ) == 0 ) count ++ ; x >>= 1L ; } return count ; } static long CountXORandSumEqual ( long x ) { long count = CountZeroBit ( x ) ; return ( 1L << count ) ; } public static void main ( String [ ] args ) { long x = 10 ; System . out . println ( CountXORandSumEqual ( x ) ) ; } }
class GFG { static int missingNumber ( int arr1 [ ] , int arr2 [ ] , int n ) { int mnum = 0 ; for ( int i = 0 ; i < n ; i ++ ) mnum = mnum ^ arr1 [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) mnum = mnum ^ arr2 [ i ] ; return mnum ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 4 , 8 , 1 , 3 , 7 } ; int arr2 [ ] = { 7 , 4 , 3 , 1 } ; int n = arr1 . length ; System . out . println ( " Missing ▁ number ▁ = ▁ " + missingNumber ( arr1 , arr2 , n ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class BitArray { int [ ] arr ; public BitArray ( int n ) { arr = new int [ ( n >> 5 ) + 1 ] ; } boolean get ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; return ( arr [ index ] & ( 1 << bitNo ) ) != 0 ; } void set ( int pos ) { int index = ( pos >> 5 ) ; int bitNo = ( pos & 0x1F ) ; arr [ index ] |= ( 1 << bitNo ) ; } static void checkDuplicates ( int [ ] arr ) { BitArray ba = new BitArray ( 320000 ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int num = arr [ i ] - 1 ; if ( ba . get ( num ) ) System . out . print ( num + " ▁ " ) ; else ba . set ( num ) ; } } public static void main ( String [ ] args ) throws java . lang . Exception { int [ ] arr = { 1 , 5 , 1 , 10 , 12 , 10 } ; checkDuplicates ( arr ) ; } }
class GFG { static int countValues ( int x ) { int count = 0 , n = 1 ; while ( x != 0 ) { if ( x % 2 == 0 ) count += n ; n *= 2 ; x /= 2 ; } return count ; } public static void main ( String [ ] args ) { int x = 10 ; System . out . println ( countValues ( x ) ) ; } }
class GFG { static void constructXOR ( int A [ ] , int n ) { int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) XOR ^= A [ i ] ; for ( int i = 0 ; i < n ; i ++ ) A [ i ] = XOR ^ A [ i ] ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 4 , 1 , 3 , 5 } ; int n = A . length ; constructXOR ( A , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
import java . io . * ; class GFG { static int bitCount ( int n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) ++ count ; n >>= 1 ; } return count ; } static long countPairsWithKDiff ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int xoredNum = arr [ i ] ^ arr [ j ] ; if ( k == bitCount ( xoredNum ) ) ++ ans ; } } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ " + countPairsWithKDiff ( arr , n , k ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static long kBitDifferencePairs ( int arr [ ] , int n , int k ) { int MAX = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; long [ ] count = new long [ MAX + 1 ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; long ans = 0 ; if ( k == 0 ) { for ( int i = 0 ; i <= MAX ; ++ i ) ans += ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ; return ans ; } for ( int i = 0 ; i <= MAX ; ++ i ) { if ( count [ i ] == 0 ) continue ; for ( int j = i + 1 ; j <= MAX ; ++ j ) { if ( Integer . bitCount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; k = 3 ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { public static int countTrailingZero ( int x ) { int count = 0 ; while ( ( x & 1 ) == 0 ) { x = x >> 1 ; count ++ ; } return count ; } public static void main ( String [ ] args ) { System . out . println ( countTrailingZero ( 11 ) ) ; } }
import java . io . * ; class GFG { static int countTrailingZero ( int x ) { int lookup [ ] = { 32 , 0 , 1 , 26 , 2 , 23 , 27 , 0 , 3 , 16 , 24 , 30 , 28 , 11 , 0 , 13 , 4 , 7 , 17 , 0 , 25 , 22 , 31 , 15 , 29 , 10 , 12 , 6 , 0 , 21 , 14 , 9 , 5 , 20 , 8 , 19 , 18 } ; return lookup [ ( - x & x ) % 37 ] ; } public static void main ( String [ ] args ) { System . out . println ( countTrailingZero ( 48 ) ) ; } }
class GFG { static int multiply ( int n , int m ) { int ans = 0 , count = 0 ; while ( m > 0 ) { if ( m % 2 == 1 ) ans += n << count ; count ++ ; m /= 2 ; } return ans ; } public static void main ( String [ ] args ) { int n = 20 , m = 13 ; System . out . print ( multiply ( n , m ) ) ; } }
import java . util . * ; class solution { static boolean EqualNumber ( int A , int B ) { if ( ( A ^ B ) != 0 ) return true ; else return false ; } public static void main ( String args [ ] ) { int A = 5 , B = 6 ; if ( EqualNumber ( A , B ) == false ) System . out . println ( 1 ) ; else System . out . println ( 0 ) ; } }
import java . io . * ; class GFG { static boolean areSetBitsIncreasing ( int n ) { int prev_count = Integer . MAX_VALUE ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = n / 2 ; int curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } static public void main ( String [ ] args ) { int n = 10 ; if ( areSetBitsIncreasing ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class Test { static boolean findPattern ( int n ) { int prev = n % 2 ; n = n / 2 ; while ( n > 0 ) { int curr = n % 2 ; if ( curr == prev ) return false ; prev = curr ; n = n / 2 ; } return true ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( findPattern ( n ) ? " Yes " : " No " ) ; } }
class GFG { static int msbPos ( long n ) { int msb_p = - 1 ; while ( n > 0 ) { n = n >> 1 ; msb_p ++ ; } return msb_p ; } static long andOperator ( long x , long y ) { while ( x > 0 && y > 0 ) { int msb_p1 = msbPos ( x ) ; int msb_p2 = msbPos ( y ) ; if ( msb_p1 != msb_p2 ) break ; long msb_val = ( 1 << msb_p1 ) ; res = res + msb_val ; x = x - msb_val ; y = y - msb_val ; } return res ; } public static void main ( String [ ] args ) { long x = 10 , y = 15 ; System . out . print ( andOperator ( x , y ) ) ; } }
import java . util . * ; class GFG { public static int multiplyTen ( int n ) { return ( n << 1 ) + ( n << 3 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( multiplyTen ( n ) ) ; } }
import java . util . * ; class GFG { public static int countValues ( int n ) { int unset_bits = 0 ; while ( n > 0 ) { if ( ( n & 1 ) == 0 ) unset_bits ++ ; n = n >> 1 ; } return 1 << unset_bits ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( countValues ( n ) ) ; } }
import java . io . * ; class GFG { static char findProffesion ( int level , int pos ) { if ( level == 1 ) return ' e ' ; if ( findProffesion ( level - 1 , ( pos + 1 ) / 2 ) == ' d ' ) return ( pos % 2 > 0 ) ? ' d ' : ' e ' ; return ( pos % 2 > 0 ) ? ' e ' : ' d ' ; } public static void main ( String [ ] args ) { int level = 4 , pos = 2 ; if ( findProffesion ( level , pos ) == ' e ' ) System . out . println ( " Engineer " ) ; else System . out . println ( " Doctor " ) ; } }
import java . io . * ; class GFG { static void printTwoSetBitNums ( int n ) { int x = 1 ; while ( n > 0 ) { int y = 0 ; while ( y < x ) { System . out . print ( ( ( 1 << x ) + ( 1 << y ) ) + " ▁ " ) ; n -- ; if ( n == 0 ) return ; y ++ ; } x ++ ; } } public static void main ( String [ ] args ) { int n = 4 ; printTwoSetBitNums ( n ) ; } }
class GFG { static int rand50 ( ) { return ( int ) ( 10 * Math . random ( ) ) & 1 ; } static int rand75 ( ) { return rand50 ( ) | rand50 ( ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 50 ; i ++ ) { System . out . print ( rand75 ( ) ) ; } } }
class GFG { static void printRepeatingEven ( int arr [ ] , int n ) { long _xor = 0L ; long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; _xor ^= pos ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = arr . length ; printRepeatingEven ( arr , n ) ; } }
import java . io . * ; class GFG { static int countSetBits ( int x ) { int count = 0 ; while ( x != 0 ) { x &= ( x - 1 ) ; count ++ ; } return count ; } static boolean isBleak ( int n ) { for ( int x = 1 ; x < n ; x ++ ) if ( x + countSetBits ( x ) == n ) return false ; return true ; } public static void main ( String args [ ] ) { if ( isBleak ( 3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isBleak ( 4 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { public static void main ( String [ ] args ) { System . out . print ( Integer . bitCount ( 4 ) + "NEW_LINE"); System . out . print ( Integer . bitCount ( 15 ) ) ; } }
import java . util . * ; class GFG { static final int INT_BITS = 32 ; static int maxSubarrayXOR ( int set [ ] , int n ) { int index = 0 ; for ( int i = INT_BITS - 1 ; i >= 0 ; i -- ) { int maxInd = index ; int maxEle = Integer . MIN_VALUE ; for ( int j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == - 2147483648 ) continue ; int temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } public static void main ( String arg [ ] ) { int set [ ] = { 9 , 8 , 5 } ; int n = set . length ; System . out . print ( " Max ▁ subset ▁ XOR ▁ is ▁ " ) ; System . out . print ( maxSubarrayXOR ( set , n ) ) ; } }
import java . util . * ; class GFG { static int findXOR ( int Set [ ] , int n ) { if ( n == 1 ) return Set [ 0 ] ; else return 0 ; } public static void main ( String arg [ ] ) { int Set [ ] = { 1 , 2 , 3 } ; int n = Set . length ; System . out . print ( " XOR ▁ of ▁ XOR ' s ▁ of ▁ all ▁ subsets ▁ is ▁ " + findXOR ( Set , n ) ) ; } }
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { int res = 0 ; for ( int i = 31 ; i >= 0 ; i -- ) { int b1 = ( ( x & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int b2 = ( ( y & ( 1 << i ) ) == 0 ) ? 0 : 1 ; int xoredBit = ( ( b1 & b2 ) != 0 ) ? 0 : ( b1 b2 ) ; res <<= 1 ; res |= xoredBit ; } return res ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + myXOR ( x , y ) ) ; } }
import java . io . * ; class GFG { static int myXOR ( int x , int y ) { return ( x y ) & ( ~ x ~ y ) ; } public static void main ( String [ ] args ) { int x = 3 , y = 5 ; System . out . println ( " XOR ▁ is ▁ " + ( myXOR ( x , y ) ) ) ; } }
import java . io . * ; class GFG { static int swapBits ( int n , int p1 , int p2 ) { int bit1 = ( n >> p1 ) & 1 ; int bit2 = ( n >> p2 ) & 1 ; int x = ( bit1 ^ bit2 ) ; x = ( x << p1 ) | ( x << p2 ) ; int result = n ^ x ; return result ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }
import java . io . * ; class GFG { public static int square ( int n ) { if ( n < 0 ) n = - n ; int res = n ; for ( int i = 1 ; i < n ; i ++ ) res += n ; return res ; } public static void main ( String [ ] args ) { for ( int n = 1 ; n <= 5 ; n ++ ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ n ^ 2 ▁ = ▁ " + square ( n ) ) ; } }
import java . io . * ; import java . util . * ; import java . math . * ; class GFG { static int N = 2 ; static boolean freivald ( int a [ ] [ ] , int b [ ] [ ] , int c [ ] [ ] ) { int r [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) r [ i ] = ( int ) ( Math . random ( ) ) % 2 ; int br [ ] = new int [ N ] ; Arrays . fill ( br , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) br [ i ] = br [ i ] + b [ i ] [ j ] * r [ j ] ; int cr [ ] = new int [ N ] ; Arrays . fill ( cr , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) cr [ i ] = cr [ i ] + c [ i ] [ j ] * r [ j ] ; int axbr [ ] = new int [ N ] ; Arrays . fill ( axbr , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) axbr [ i ] = axbr [ i ] + a [ i ] [ j ] * br [ j ] ; for ( int i = 0 ; i < N ; i ++ ) if ( axbr [ i ] - cr [ i ] != 0 ) return false ; return true ; } static boolean isProduct ( int a [ ] [ ] , int b [ ] [ ] , int c [ ] [ ] , int k ) { for ( int i = 0 ; i < k ; i ++ ) if ( freivald ( a , b , c ) == false ) return false ; return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 1 , 1 } , { 1 , 1 } } ; int b [ ] [ ] = { { 1 , 1 } , { 1 , 1 } } ; int c [ ] [ ] = { { 2 , 2 } , { 2 , 2 } } ; int k = 2 ; if ( isProduct ( a , b , c , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . io . * ; class GFG { static float calc_Expectation ( float a [ ] , float n ) { float prb = ( 1 / n ) ; float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] * prb ; return sum ; } public static void main ( String args [ ] ) { float expect , n = 6f ; float a [ ] = { 1f , 2f , 3f , 4f , 5f , 6f } ; expect = calc_Expectation ( a , n ) ; System . out . println ( " Expectation ▁ of ▁ array ▁ E ( X ) ▁ is ▁ : ▁ " + expect ) ; } }
import java . io . * ; class GFG { static long SumGPUtil ( long r , long n , long m ) { if ( n == 0 ) return 1 ; if ( n == 1 ) return ( 1 + r ) % m ; long ans ; if ( n % 2 == 1 ) { ans = ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n - 1 ) / 2 , m ) ; } else { ans = 1 + ( r * ( 1 + r ) * SumGPUtil ( ( r * r ) % m , ( n / 2 ) - 1 , m ) ) ; } return ( ans % m ) ; } static void SumGP ( long a , long r , long N , long M ) { long answer ; answer = a * SumGPUtil ( r , N , M ) ; answer = answer % M ; System . out . println ( answer ) ; } public static void main ( String [ ] args ) { long a = 1 ; long r = 4 ; long N = 10000 ; long M = 100000 ; SumGP ( a , r , N , M ) ; } }
class GFG { static int LongestSubarray ( int a [ ] , int n , int k ) { int [ ] pre = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > k ) pre [ i ] = 1 ; else pre [ i ] = - 1 ; } for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = pre [ i - 1 ] + pre [ i ] ; int len = 0 ; int lo = 1 , hi = n ; while ( lo <= hi ) { int mid = ( lo + hi ) / 2 ; boolean ok = false ; for ( int i = mid - 1 ; i < n ; i ++ ) { int x = pre [ i ] ; if ( i - mid >= 0 ) x -= pre [ i - mid ] ; if ( x > 0 ) { ok = true ; break ; } } if ( ok == true ) { len = mid ; lo = mid + 1 ; } else hi = mid - 1 ; } return len ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 3 , 4 , 5 , 3 , 7 } ; int k = 3 ; int n = a . length ; System . out . println ( LongestSubarray ( a , n , k ) ) ; } }
class GFG { static void findPoints ( int l1 , int r1 , int l2 , int r2 ) { int x = ( l1 != l2 ) ? Math . min ( l1 , l2 ) : - 1 ; int y = ( r1 != r2 ) ? Math . max ( r1 , r2 ) : - 1 ; System . out . println ( x + " ▁ " + y ) ; } public static void main ( String [ ] args ) { int l1 = 5 , r1 = 10 , l2 = 1 , r2 = 7 ; findPoints ( l1 , r1 , l2 , r2 ) ; } }
class GFG { static int fact ( int n ) { if ( n == 0 ) return 1 ; return n * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { System . out . println ( fact ( 5 ) ) ; } }
public class GFG { static void minDays ( int [ ] P , int [ ] arr ) { for ( int i = 1 ; i < P . length ; i ++ ) { P [ i ] += P [ i ] + P [ i - 1 ] ; } for ( int i = 0 ; i < arr . length ; i ++ ) { int index = binarySeach ( P , arr [ i ] ) ; if ( index != - 1 ) { System . out . print ( index + 1 + " ▁ " ) ; } else { System . out . print ( - 1 + " ▁ " ) ; } } } static int binarySeach ( int [ ] P , int N ) { int i = 0 ; int j = P . length - 1 ; int index = - 1 ; while ( i <= j ) { int mid = i + ( j - i ) / 2 ; if ( P [ mid ] >= N ) { index = mid ; j = mid - 1 ; } else { i = mid + 1 ; } } return index ; } public static void main ( String [ ] args ) { int [ ] arr = { 400 , 200 , 700 , 900 , 1400 } ; int [ ] P = { 100 , 300 , 400 , 500 , 600 } ; minDays ( P , arr ) ; } }
import java . util . * ; public class Main { static int countNumbers ( int L , int R , int K ) { ArrayList < Integer > list = new ArrayList < > ( ) ; for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) ) { list . add ( i ) ; } } int count = 0 ; for ( int i = 0 ; i < list . size ( ) ; i ++ ) { int right_index = search ( list , list . get ( i ) + K - 1 ) ; if ( right_index != - 1 ) count = Math . max ( count , right_index - i + 1 ) ; } return count ; } static int search ( ArrayList < Integer > list , int num ) { int low = 0 , high = list . size ( ) - 1 ; int ans = - 1 ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( list . get ( mid ) <= num ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } return ans ; } static boolean isPalindrome ( int n ) { int rev = 0 ; int temp = n ; while ( n > 0 ) { rev = rev * 10 + n % 10 ; n /= 10 ; } return rev == temp ; } public static void main ( String args [ ] ) { int L = 98 , R = 112 ; int K = 13 ; System . out . print ( countNumbers ( L , R , K ) ) ; } }
import java . util . * ; class GFG { public static int maximumProfit ( int arr [ ] ) { int ans = Integer . MIN_VALUE ; int price = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] <= arr [ j ] ) { count ++ ; } } if ( ans < count * arr [ i ] ) { price = arr [ i ] ; ans = count * arr [ i ] ; } } return price ; } public static void main ( String [ ] args ) { int arr [ ] = { 22 , 87 , 9 , 50 , 56 , 43 } ; System . out . print ( maximumProfit ( arr ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void mulsum ( int arr [ ] , int n , int N ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % N == 0 ) { sum = sum + arr [ i ] ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 } ; int n = arr . length ; int N = 3 ; mulsum ( arr , n , N ) ; } }
import java . util . * ; class GFG { static String newString ( String originalLabel , int limit ) { int n = originalLabel . length ( ) ; int [ ] charset = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) { charset [ originalLabel . charAt ( i ) - ' a ' ] ++ ; } StringBuilder newString = new StringBuilder ( n ) ; for ( int i = 25 ; i >= 0 ; i -- ) { int count = 0 ; while ( charset [ i ] > 0 ) { newString . append ( ( char ) ( i + ' a ' ) ) ; charset [ i ] -- ; count ++ ; if ( charset [ i ] > 0 && count == limit ) { Character next = nextAvailableChar ( charset , i ) ; if ( next == null ) return newString . toString ( ) ; newString . append ( next ) ; count = 0 ; } } } return newString . toString ( ) ; } static Character nextAvailableChar ( int [ ] charset , int start ) { for ( int i = start - 1 ; i >= 0 ; i -- ) { if ( charset [ i ] > 0 ) { charset [ i ] -- ; return ( char ) ( i + ' a ' ) ; } } return null ; } public static void main ( String [ ] args ) { String S = " ccbbb " ; int K = 2 ; System . out . println ( newString ( S , K ) ) ; } }
import java . io . * ; class GFG { static void swapEvenOdd ( int arr [ ] ) { int n = arr . length ; int o = - 1 , e = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) continue ; int r = - 1 ; if ( arr [ i ] % 2 == 0 ) { o ++ ; while ( arr [ o ] % 2 == 0 arr [ o ] < 0 ) o ++ ; r = o ; } else { e ++ ; while ( arr [ e ] % 2 == 1 arr [ e ] < 0 ) e ++ ; r = e ; } arr [ i ] *= - 1 ; arr [ r ] *= - 1 ; int tmp = arr [ i ] ; arr [ i ] = arr [ r ] ; arr [ r ] = tmp ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ( - 1 * arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 } ; swapEvenOdd ( arr ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static String rev ( String str ) { int st = 0 ; int ed = str . length ( ) - 1 ; char [ ] s = str . toCharArray ( ) ; while ( st < ed ) { char temp = s [ st ] ; s [ st ] = s [ ed ] ; s [ ed ] = temp ; st ++ ; ed -- ; } return ( s . toString ( ) ) ; } public static boolean check ( String a , String b , int n ) { int i = 0 ; int j = n - 1 ; while ( i < n ) { if ( a . charAt ( i ) != b . charAt ( j ) ) break ; i += 1 ; j -= 1 ; String xa = a . substring ( i , j + 1 ) ; String xb = b . substring ( i , j + 1 ) ; StringBuffer XA = new StringBuffer ( xa ) ; XA . reverse ( ) ; StringBuffer XB = new StringBuffer ( xb ) ; XB . reverse ( ) ; if ( xa == XA . toString ( ) || xb == XB . toString ( ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { String a = " xbdef " ; String b = " cabex " ; if ( check ( a , b , a . length ( ) ) || check ( b , a , a . length ( ) ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
import java . util . * ; class GFG { public static int maxEqualIdx ( int [ ] arr , int k ) { Arrays . sort ( arr ) ; int [ ] prefixSum = new int [ arr . length + 1 ] ; prefixSum [ 1 ] = arr [ 0 ] ; for ( int i = 1 ; i < prefixSum . length - 1 ; ++ i ) { prefixSum [ i + 1 ] = prefixSum [ i ] + arr [ i ] ; } int max = arr . length ; int min = 1 ; int ans = 1 ; while ( min <= max ) { int mid = ( max + min ) / 2 ; if ( check ( prefixSum , mid , k , arr ) ) { ans = mid ; min = mid + 1 ; } else { max = mid - 1 ; } } return ans ; } public static boolean check ( int [ ] pSum , int len , int k , int [ ] a ) { int i = 0 ; int j = len ; while ( j <= a . length ) { int maxSize = a [ j - 1 ] ; int totalNumbers = maxSize * len ; int currNumbers = pSum [ j ] - pSum [ i ] ; if ( currNumbers + k >= totalNumbers ) { return true ; } else { i ++ ; j ++ ; } } return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 } ; int k = 7 ; System . out . println ( maxEqualIdx ( arr , k ) ) ; } }
import java . util . * ; class GFG { static int productSum ( int [ ] arr ) { int cntOne = 0 ; int N = arr . length ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 1 ) cntOne ++ ; } return cntOne * ( cntOne - 1 ) / 2 ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 0 , 1 } ; System . out . println ( productSum ( arr ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int findWays ( int [ ] nums , int S ) { int sum = 0 ; for ( int i = 0 ; i < nums . length ; i ++ ) sum += nums [ i ] ; int [ ] [ ] memo = new int [ nums . length + 1 ] [ 2 * sum + 1 ] ; for ( int [ ] m : memo ) { Arrays . fill ( m , Integer . MIN_VALUE ) ; } return dfs ( memo , nums , S , 0 , 0 , sum ) ; } static int dfs ( int [ ] [ ] memo , int [ ] nums , int S , int curr_sum , int index , int sum ) { if ( index == nums . length ) { if ( S == curr_sum ) return 1 ; else return 0 ; } if ( memo [ index ] [ curr_sum + sum ] != Integer . MIN_VALUE ) { return memo [ index ] [ curr_sum + sum ] ; } int ans = dfs ( memo , nums , index + 1 , curr_sum + nums [ index ] , S , sum ) + dfs ( memo , nums , index + 1 , curr_sum - nums [ index ] , S , sum ) ; memo [ index ] [ curr_sum + sum ] = ans ; return ans ; } public static void main ( String [ ] args ) { int S = 3 ; int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int answer = findWays ( arr , S ) ; System . out . println ( answer ) ; } }
import java . util . * ; public class GFG { public static int findMaximumSum ( int [ ] a , int n ) { int prev_smaller [ ] = findPrevious ( a , n ) ; int next_smaller [ ] = findNext ( a , n ) ; int max_value = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_value = Math . max ( max_value , a [ i ] * ( next_smaller [ i ] - prev_smaller [ i ] - 1 ) ) ; } return max_value ; } public static int [ ] findPrevious ( int [ ] a , int n ) { int ps [ ] = new int [ n ] ; ps [ 0 ] = - 1 ; Stack < Integer > stack = new Stack < > ( ) ; stack . push ( 0 ) ; for ( int i = 1 ; i < a . length ; i ++ ) { while ( stack . size ( ) > 0 && a [ stack . peek ( ) ] >= a [ i ] ) stack . pop ( ) ; ps [ i ] = stack . size ( ) > 0 ? stack . peek ( ) : - 1 ; stack . push ( i ) ; } return ps ; } public static int [ ] findNext ( int [ ] a , int n ) { int ns [ ] = new int [ n ] ; ns [ n - 1 ] = n ; Stack < Integer > stack = new Stack < > ( ) ; stack . push ( n - 1 ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { while ( stack . size ( ) > 0 && a [ stack . peek ( ) ] >= a [ i ] ) stack . pop ( ) ; ns [ i ] = stack . size ( ) > 0 ? stack . peek ( ) : a . length ; stack . push ( i ) ; } return ns ; } public static void main ( String args [ ] ) { int n = 3 ; int a [ ] = { 80 , 48 , 82 } ; System . out . println ( findMaximumSum ( a , n ) ) ; } }
import java . util . * ; class GFG { static void max_length ( int N , int x , int [ ] v ) { int i , a ; List < Integer > preff = new Vector < Integer > ( ) ; List < Integer > suff = new Vector < Integer > ( ) ; int ct = 0 ; for ( i = 0 ; i < N ; i ++ ) { a = v [ i ] ; if ( a % x == 0 ) { ct += 1 ; } } if ( ct == N ) { System . out . print ( - 1 + "NEW_LINE"); return ; } v = reverse ( v ) ; suff . add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { suff . add ( v [ i ] + suff . get ( i - 1 ) ) ; } v = reverse ( v ) ; Collections . reverse ( suff ) ; preff . add ( v [ 0 ] ) ; for ( i = 1 ; i < N ; i ++ ) { preff . add ( v [ i ] + preff . get ( i - 1 ) ) ; } int ans = 0 ; int lp = 0 ; int rp = N - 1 ; for ( i = 0 ; i < N ; i ++ ) { if ( suff . get ( i ) % x != 0 && ( ans < ( N - 1 ) ) ) { lp = i ; rp = N - 1 ; ans = Math . max ( ans , N - i ) ; } if ( preff . get ( i ) % x != 0 && ( ans < ( i + 1 ) ) ) { lp = 0 ; rp = i ; ans = Math . max ( ans , i + 1 ) ; } } for ( i = lp ; i <= rp ; i ++ ) { System . out . print ( v [ i ] + " ▁ " ) ; } } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int x = 3 ; int [ ] v = { 1 , 3 , 2 , 6 } ; int N = v . length ; max_length ( N , x , v ) ; } }
public class GFG { static void maxUpdated ( int [ ] arr ) { int h_score = arr [ 0 ] ; int l_score = arr [ 0 ] ; int i = 1 , j = 1 ; for ( Integer n : arr ) { if ( h_score < n ) { h_score = n ; i ++ ; } if ( l_score > n ) { l_score = n ; j ++ ; } } System . out . print ( " Number ▁ of ▁ times ▁ maximum ▁ value ▁ " ) ; System . out . print ( " updated ▁ = ▁ " + i + "NEW_LINE"); System . out . print ( " Number ▁ of ▁ times ▁ minimum ▁ value ▁ " ) ; System . out . print ( " updated ▁ = ▁ " + j ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 20 , 22 } ; maxUpdated ( arr ) ; } }
class GFG { static void farthest_min ( int [ ] a , int n ) { int [ ] suffix_min = new int [ n ] ; suffix_min [ n - 1 ] = a [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffix_min [ i ] = Math . min ( suffix_min [ i + 1 ] , a [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { int low = i + 1 , high = n - 1 , ans = - 1 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( suffix_min [ mid ] < a [ i ] ) { ans = mid ; low = mid + 1 ; } else high = mid - 1 ; } System . out . print ( ans + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] a = { 3 , 1 , 5 , 2 , 4 } ; int n = a . length ; farthest_min ( a , n ) ; } }
import java . util . * ; class GFG { static void printElements ( int a [ ] , int n ) { Arrays . sort ( a ) ; a = reverse ( a ) ; int cnt = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] != a [ i + 1 ] ) { System . out . print ( a [ i ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); cnt = 1 ; } else cnt += 1 ; } System . out . print ( a [ n - 1 ] + " ▁ occurs ▁ " + cnt + " timesNEW_LINE"); } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 1 , 1 , 2 , 3 , 4 , 9 , 9 , 10 } ; int n = a . length ; printElements ( a , n ) ; } }
class GFG { static boolean checkIndices ( int Arr [ ] , int N ) { for ( int i = 0 ; i < N - 1 ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( Arr [ i ] != Arr [ j ] ) { if ( Arr [ Arr [ i ] - 1 ] == Arr [ Arr [ j ] - 1 ] ) return true ; } } } return false ; } public static void main ( String args [ ] ) { int Arr [ ] = { 3 , 2 , 1 , 1 , 4 } ; int N = Arr . length ; if ( checkIndices ( Arr , N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . ArrayList ; import java . util . HashMap ; import java . util . List ; import java . util . Map ; class Pair { public int x , y ; Pair ( int x , int y ) { this . x = x ; this . y = y ; } } class GFG { public static void findPairs ( int [ ] A ) { Map < Integer , List < Pair > > map = new HashMap < > ( ) ; for ( int i = 0 ; i < A . length - 1 ; i ++ ) { for ( int j = i + 1 ; j < A . length ; j ++ ) { int sum = A [ i ] + A [ j ] ; if ( map . containsKey ( sum ) ) { for ( Pair pair : map . get ( sum ) ) { int x = pair . x ; int y = pair . y ; if ( ( x != i && x != j ) && ( y != i && y != j ) ) { System . out . println ( " Pair ▁ First ( " + A [ i ] + " , ▁ " + A [ j ] + " ) " ) ; System . out . println ( " Pair ▁ Second ▁ ( " + A [ x ] + " , ▁ " + A [ y ] + " ) " ) ; return ; } } } map . putIfAbsent ( sum , new ArrayList < > ( ) ) ; map . get ( sum ) . add ( new Pair ( i , j ) ) ; } } System . out . print ( " No ▁ such ▁ non - overlapping ▁ pairs ▁ present " ) ; } public static void main ( String [ ] args ) { int [ ] A = { 8 , 4 , 7 , 8 , 4 } ; findPairs ( A ) ; } }
class GFG { static void printPairs ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] + arr [ j ] == sum ) System . out . println ( " ( " + arr [ i ] + " , ▁ " + arr [ j ] + " ) " ) ; } public static void main ( String [ ] arg ) { int arr [ ] = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
import java . util . * ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Arrays . sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int indexGreater = upper_bound ( arr , 0 , n , arr [ i ] + L ) ; int numberOfElements = indexGreater - ( i + 1 ) ; if ( numberOfElements >= 2 ) { ways += ( numberOfElements * ( numberOfElements - 1 ) / 2 ) ; } } return ways ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int L = 4 ; int ans = countTripletsLessThanL ( n , L , arr ) ; System . out . println ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }
public class RandomizedBinarySearch { public static int getRandom ( int x , int y ) { return ( x + ( int ) ( Math . random ( ) % ( y - x + 1 ) ) ) ; } public static int randomizedBinarySearch ( int arr [ ] , int low , int high , int key ) { if ( high >= low ) { int mid = getRandom ( low , high ) ; if ( arr [ mid ] == key ) return mid ; if ( arr [ mid ] > key ) return randomizedBinarySearch ( arr , low , mid - 1 , key ) ; return randomizedBinarySearch ( arr , mid + 1 , high , key ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int key = 10 ; int result = randomizedBinarySearch ( arr , 0 , n - 1 , key ) ; System . out . println ( ( result == - 1 ) ? " Element ▁ is ▁ not ▁ present ▁ in ▁ array " : " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }
import java . util . Arrays ; import java . util . List ; class Test { static String remVowel ( String str ) { Character vowels [ ] = { ' a ' , ' e ' , ' i ' , ' o ' , ' u ' , ' A ' , ' E ' , ' I ' , ' O ' , ' U ' } ; List < Character > al = Arrays . asList ( vowels ) ; StringBuffer sb = new StringBuffer ( str ) ; for ( int i = 0 ; i < sb . length ( ) ; i ++ ) { if ( al . contains ( sb . charAt ( i ) ) ) { sb . replace ( i , i + 1 , " " ) ; i -- ; } } return sb . toString ( ) ; } public static void main ( String [ ] args ) { String str = " GeeeksforGeeks ▁ - ▁ A ▁ Computer ▁ Science ▁ Portal ▁ for ▁ Geeks " ; System . out . println ( remVowel ( str ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int minCostToMakeElementEqual ( int a [ ] , int n ) { int y ; if ( n % 2 == 1 ) y = a [ n / 2 ] ; else y = ( a [ n / 2 ] + a [ ( n - 2 ) / 2 ] ) / 2 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) s += Math . abs ( a [ i ] - y ) ; return s ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 100 , 101 } ; int n = a . length ; System . out . println ( minCostToMakeElementEqual ( a , n ) ) ; } }
public static boolean isSorted ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) return false ; } return true ; } public static boolean sortPoss ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int idx = - 1 ; int minVar = arr [ i ] ; int j = i ; while ( j < n ) { if ( arr [ j ] < minVar ) { minVar = arr [ j ] ; idx = j ; } j = j + 2 ; } if ( idx != - 1 ) { int t ; t = arr [ i ] ; arr [ i ] = arr [ idx ] ; arr [ idx ] = t ; } } if ( isSorted ( arr , n ) ) return true ; else return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 5 , 1 , 2 , 6 } ; int n = arr . length ; if ( sortPoss ( arr , n ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }
import java . util . * ; class GFG { static long maxSum ( int [ ] a ) { int n = a . length ; int [ ] count = new int [ n + 1 ] ; for ( int x : a ) count [ Math . min ( x , n ) ] ++ ; int size = 0 ; long ans = 0 ; for ( int k = 1 ; k <= n ; k ++ ) { while ( count [ k ] > 0 && size < k ) { size ++ ; ans += size ; count [ k ] -- ; } ans += k * count [ k ] ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 5 , 1 } ; System . out . println ( maxSum ( arr ) ) ; } }
import java . util . * ; class GFG { public static void answerQueries ( int [ ] arr , int n , int [ ] [ ] queries ) { for ( int i = 0 ; i < queries . length ; i ++ ) { int ans = 0 ; int l = queries [ i ] [ 0 ] - 1 ; int r = queries [ i ] [ 1 ] - 1 ; Map < Integer , Integer > freq = new HashMap < > ( ) ; for ( int j = l ; j < r + 1 ; j ++ ) { freq . put ( arr [ j ] , freq . getOrDefault ( arr [ j ] , 0 ) + 1 ) ; } for ( int k : freq . keySet ( ) ) { ans += k * freq . get ( k ) ; } System . out . println ( ans ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 1 } ; int n = arr . length ; int [ ] [ ] queries = { { 1 , 2 } , { 1 , 3 } } ; answerQueries ( arr , n , queries ) ; } }
import java . util . * ; class GFG { static boolean compare ( int [ ] arr1 , int [ ] arr2 ) { for ( int i = 0 ; i < 256 ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static boolean search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int [ ] countP = new int [ 256 ] ; int [ ] countTW = new int [ 256 ] ; for ( int i = 0 ; i < 256 ; i ++ ) { countP [ i ] = 0 ; countTW [ i ] = 0 ; } for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) return true ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) return true ; return false ; } public static void main ( String [ ] args ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; if ( search ( pat , txt ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int PointInKSquares ( int n , int a [ ] , int k ) { Arrays . sort ( a ) ; return a [ n - k ] ; } public static void main ( String [ ] args ) { int k = 2 ; int [ ] a = { 1 , 2 , 3 , 4 } ; int n = a . length ; int x = PointInKSquares ( n , a , k ) ; System . out . println ( " ( " + x + " , ▁ " + x + " ) " ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static boolean PandigitalProduct_1_9 ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) if ( n % i == 0 && isPandigital ( " " + n + i + n / i ) ) return true ; return false ; } public static boolean isPandigital ( String str ) { if ( str . length ( ) != 9 ) return false ; char ch [ ] = str . toCharArray ( ) ; Arrays . sort ( ch ) ; return new String ( ch ) . equals ( "123456789" ) ; } public static void main ( String [ ] args ) { int n = 6952 ; if ( PandigitalProduct_1_9 ( n ) == true ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
import java . util . Arrays ; class GFG { static void printModeMedian ( int a [ ] , int n ) { int [ ] b = new int [ n ] ; int max = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int t = max + 1 ; int count [ ] = new int [ t ] ; for ( int i = 0 ; i < t ; i ++ ) { count [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { count [ a [ i ] ] ++ ; } int mode = 0 ; int k = count [ 0 ] ; for ( int i = 1 ; i < t ; i ++ ) { if ( count [ i ] > k ) { k = count [ i ] ; mode = i ; } } for ( int i = 1 ; i < t ; i ++ ) { count [ i ] = count [ i ] + count [ i - 1 ] ; } for ( int i = 0 ; i < n ; i ++ ) { b [ count [ a [ i ] ] - 1 ] = a [ i ] ; count [ a [ i ] ] -- ; } float median ; if ( n % 2 != 0 ) { median = b [ n / 2 ] ; } else { median = ( float ) ( ( b [ ( n - 1 ) / 2 ] + b [ ( n / 2 ) ] ) / 2.0 ) ; } System . out . println ( " median ▁ = ▁ " + median ) ; System . out . println ( " mode ▁ = ▁ " + mode ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 1 , 2 , 7 , 1 , 2 , 5 , 3 , 6 } ; int n = a . length ; printModeMedian ( a , n ) ; } }
import java . util . * ; import java . lang . * ; class GeeksforGeeks { final static int MAX = 26 ; static boolean function ( String str ) { int l = str . length ( ) ; int counter1 [ ] = new int [ MAX ] ; int counter2 [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { counter1 [ i ] = 0 ; counter2 [ i ] = 0 ; } for ( int i = 0 ; i < l / 2 ; i ++ ) counter1 [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = l / 2 ; i < l ; i ++ ) counter2 [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( counter2 [ i ] != counter1 [ i ] ) return true ; } return false ; } public static void main ( String args [ ] ) { String str = " abcasdsabcae " ; if ( function ( str ) ) System . out . print ( " Yes , ▁ both ▁ halves ▁ " + " differ ▁ by ▁ at ▁ least ▁ one ▁ character " ) ; else System . out . print ( " No , ▁ both ▁ halves ▁ " + " do ▁ not ▁ differ ▁ at ▁ all " ) ; } }
import java . util . Arrays ; import java . util . Comparator ; class Element { int index , no_of_fact ; public Element ( int i , int countFactors ) { index = i ; no_of_fact = countFactors ; } static int countFactors ( int n ) { int count = 0 ; int sq = ( int ) Math . sqrt ( n ) ; if ( sq * sq == n ) count ++ ; for ( int i = 1 ; i < Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) count += 2 ; } return count ; } static void printOnBasisOfFactors ( int arr [ ] , int n ) { Element num [ ] = new Element [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { num [ i ] = new Element ( i , countFactors ( arr [ i ] ) ) ; } Arrays . sort ( num , new Comparator < Element > ( ) { @ Override public int compare ( Element e1 , Element e2 ) { if ( e1 . no_of_fact == e2 . no_of_fact ) return e1 . index < e2 . index ? - 1 : 1 ; return e1 . no_of_fact > e2 . no_of_fact ? - 1 : 1 ; } } ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ num [ i ] . index ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 11 , 10 , 20 , 9 , 16 , 23 } ; printOnBasisOfFactors ( arr , arr . length ) ; } }
import java . util . Arrays ; class GFG { static int minDifferenceAmongMaxMin ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int res = 2147483647 ; for ( int i = 0 ; i <= ( N - K ) ; i ++ ) { int curSeqDiff = arr [ i + K - 1 ] - arr [ i ] ; res = Math . min ( res , curSeqDiff ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 30 , 100 , 101 , 102 } ; int N = arr . length ; int K = 3 ; System . out . print ( minDifferenceAmongMaxMin ( arr , N , K ) ) ; } }
class ArrayIndex { static int getIndexInSortedArray ( int arr [ ] , int n , int idx ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < arr [ idx ] ) result ++ ; if ( arr [ i ] == arr [ idx ] && i < idx ) result ++ ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 3 , 5 , 2 , 3 , 4 , 3 , 1 , 5 } ; int n = arr . length ; int idxOfEle = 5 ; System . out . println ( getIndexInSortedArray ( arr , n , idxOfEle ) ) ; } }
import java . io . * ; class GFG { static int binarySearch ( int a [ ] , int item , int low , int high ) { while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( item == a [ mid ] ) return mid + 1 ; else if ( item > a [ mid ] ) low = mid + 1 ; else high = mid - 1 ; } return low ; } static void insertionSort ( int a [ ] , int n ) { int i , loc , j , k , selected ; for ( i = 1 ; i < n ; ++ i ) { j = i - 1 ; selected = a [ i ] ; loc = binarySearch ( a , selected , 0 , j ) ; while ( j >= loc ) { a [ j + 1 ] = a [ j ] ; j -- ; } a [ j + 1 ] = selected ; } } public static void main ( String [ ] args ) { int a [ ] = { 37 , 23 , 0 , 17 , 12 , 72 , 31 , 46 , 100 , 88 , 54 } ; int n = a . length , i ; insertionSort ( a , n ) ; System . out . println ( " Sorted ▁ array : " ) ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
import java . util . ArrayList ; class GFG { public static boolean possible ( int x , int S , int N ) { int minSum = ( x * ( x + 1 ) ) / 2 ; int maxSum = ( x * ( ( 2 * N ) - x + 1 ) ) / 2 ; if ( S < minSum S > maxSum ) { return false ; } return true ; } public static void findPermutation ( int N , int L , int R , int S ) { int x = R - L + 1 ; if ( ! possible ( x , S , N ) ) { System . out . print ( - 1 ) ; return ; } else { ArrayList < Integer > v = new ArrayList < > ( ) ; for ( int i = N ; i >= 1 ; -- i ) { if ( ( S - i ) >= 0 && possible ( x - 1 , S - i , i - 1 ) ) { S = S - i ; x -- ; v . add ( i ) ; } if ( S == 0 ) { break ; } } if ( S != 0 ) { System . out . print ( - 1 ) ; return ; } ArrayList < Integer > v1 = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= N ; ++ i ) { boolean it = v . contains ( i ) ; if ( ! it ) { v1 . add ( i ) ; } } int j = 0 , f = 0 ; for ( int i = 1 ; i < L ; ++ i ) { System . out . print ( v1 . get ( j ) + " ▁ " ) ; j ++ ; } for ( int i = L ; i <= R ; ++ i ) { System . out . print ( v . get ( f ) + " ▁ " ) ; f ++ ; } for ( int i = R + 1 ; i <= N ; ++ i ) { System . out . print ( v1 . get ( j ) + " ▁ " ) ; j ++ ; } } return ; } public static void main ( String args [ ] ) { int N + C3 : AC3 = 6 , L = 3 , R = 5 , S = 8 ; findPermutation ( N , L , R , S ) ; } }
import java . util . ArrayList ; import java . util . Map ; import java . util . TreeMap ; class GFG { public static void skipIndices ( int N , int T , int arr [ ] ) { int sum = 0 ; TreeMap < Integer , Integer > count = new TreeMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { int d = sum + arr [ i ] - T ; int k = 0 ; if ( d > 0 ) { for ( Map . Entry < Integer , Integer > u : count . descendingMap ( ) . entrySet ( ) ) { int j = u . getKey ( ) ; int x = j * count . get ( j ) ; if ( d <= x ) { k += ( d + j - 1 ) / j ; break ; } k += count . get ( j ) ; d -= x ; } } sum += arr [ i ] ; if ( count . containsKey ( arr [ i ] ) ) { count . put ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; } else { count . put ( arr [ i ] , 1 ) ; } System . out . print ( k + " ▁ " ) ; } } public static void main ( String args [ ] ) { int N = 7 ; int T = 15 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; skipIndices ( N , T , arr ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int countTriplets ( int N , int K ) { if ( K % 2 == 0 ) { int x = N / K ; int y = ( N + ( K / 2 ) ) / K ; return x * x * x + y * y * y ; } else { int x = N / K ; return x * x * x ; } } public static void main ( String [ ] args ) { int N = 2 , K = 2 ; System . out . print ( countTriplets ( N , K ) ) ; } }
import java . util . * ; class GFG { static int ans ; static void subsetSum ( Vector < Integer > c ) { int L = c . size ( ) ; int mul = ( int ) Math . pow ( 2 , L - 1 ) ; for ( int i = 0 ; i < c . size ( ) ; i ++ ) ans += c . get ( i ) * mul ; } static void subsetGen ( int [ ] arr , int i , int n , Vector < Integer > c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . remove ( 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 } ; int n = arr . length ; Vector < Integer > c = new Vector < Integer > ( ) ; subsetGen ( arr , 0 , n , c ) ; System . out . println ( ans ) ; } }
class GFG { static int getMaxValue ( int arr [ ] , int arr_size ) { int i , first , second ; if ( arr_size < 2 ) { return 0 ; } first = second = Integer . MIN_VALUE ; for ( i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second && arr [ i ] != first ) { second = arr [ i ] ; } } if ( second == Integer . MIN_VALUE ) { return 0 ; } else { return second ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 1 , 8 } ; int n = arr . length ; System . out . println ( getMaxValue ( arr , n ) ) ; } }
class GFG { static int maximumResult ( int a , int b , int c ) { int countOfNegative = 0 ; int sum = a + b + c ; int product = a * b * c ; int largest = ( a > b ) ? ( ( a > c ) ? a : c ) : ( ( b > c ) ? b : c ) ; int smallest = ( a < b ) ? ( ( a < c ) ? a : c ) : ( ( b < c ) ? b : c ) ; if ( a < 0 ) countOfNegative ++ ; if ( b < 0 ) countOfNegative ++ ; if ( c < 0 ) countOfNegative ++ ; switch ( countOfNegative ) { case 0 : return ( sum - largest ) * largest ; case 1 : return ( product / smallest ) + smallest ; case 2 : return ( product / largest ) + largest ; case 3 : return ( sum - smallest ) * smallest ; } return - 1 ; } public static void main ( String [ ] args ) { int a = - 2 , b = - 1 , c = - 4 ; System . out . print ( maximumResult ( a , b , c ) ) ; } }
import java . util . * ; class GFG { static int check ( int n , List < Integer > marks ) { Integer x = Collections . max ( marks ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks . get ( i ) + bonus >= 50 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 5 ; List < Integer > marks = Arrays . asList ( 0 , 21 , 83 , 45 , 64 ) ; System . out . println ( check ( n , marks ) ) ; } }
import java . util . * ; class Solution { static final int MAX = 10000 ; static int hashTable [ ] = new int [ MAX ] ; static int minOperations ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) hashTable [ arr [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] != 0 ) { for ( int j = i ; j < n ; j ++ ) if ( arr [ j ] % arr [ i ] == 0 ) hashTable [ arr [ j ] ] = 0 ; res ++ ; } } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 } ; int n = arr . length ; System . out . print ( minOperations ( arr , n ) ) ; } }
class Process { public Process ( int pid , int bt , int art ) { this . pid = pid ; this . bt = bt ; this . art = art ; } } public class GFG { static void findWaitingTime ( Process proc [ ] , int n , int wt [ ] ) { int rt [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) rt [ i ] = proc [ i ] . bt ; int complete = 0 , t = 0 , minm = Integer . MAX_VALUE ; int shortest = 0 , finish_time ; boolean check = false ; while ( complete != n ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( proc [ j ] . art <= t ) && ( rt [ j ] < minm ) && rt [ j ] > 0 ) { minm = rt [ j ] ; shortest = j ; check = true ; } } if ( check == false ) { t ++ ; continue ; } rt [ shortest ] -- ; minm = rt [ shortest ] ; if ( minm == 0 ) minm = Integer . MAX_VALUE ; if ( rt [ shortest ] == 0 ) { complete ++ ; check = false ; finish_time = t + 1 ; wt [ shortest ] = finish_time - proc [ shortest ] . bt - proc [ shortest ] . art ; if ( wt [ shortest ] < 0 ) wt [ shortest ] = 0 ; } t ++ ; } } static void findTurnAroundTime ( Process proc [ ] , int n , int wt [ ] , int tat [ ] ) { for ( int i = 0 ; i < n ; i ++ ) tat [ i ] = proc [ i ] . bt + wt [ i ] ; } static void findavgTime ( Process proc [ ] , int n ) { int wt [ ] = new int [ n ] , tat [ ] = new int [ n ] ; int total_wt = 0 , total_tat = 0 ; findWaitingTime ( proc , n , wt ) ; findTurnAroundTime ( proc , n , wt , tat ) ; System . out . println ( " Processes ▁ " + " ▁ Burst ▁ time ▁ " + " ▁ Waiting ▁ time ▁ " + " ▁ Turn ▁ around ▁ time " ) ; for ( int i = 0 ; i < n ; i ++ ) { total_wt = total_wt + wt [ i ] ; total_tat = total_tat + tat [ i ] ; System . out . println ( " ▁ " + proc [ i ] . pid + " TABSYMBOL TABSYMBOL " + proc [ i ] . bt + " TABSYMBOL TABSYMBOL ▁ " + wt [ i ] + " TABSYMBOL TABSYMBOL " + tat [ i ] ) ; } System . out . println ( " Average ▁ waiting ▁ time ▁ = ▁ " + ( float ) total_wt / ( float ) n ) ; System . out . println ( " Average ▁ turn ▁ around ▁ time ▁ = ▁ " + ( float ) total_tat / ( float ) n ) ; } public static void main ( String [ ] args ) { Process proc [ ] = { new Process ( 1 , 6 , 1 ) , new Process ( 2 , 8 , 1 ) , new Process ( 3 , 7 , 2 ) , new Process ( 4 , 3 , 3 ) } ; findavgTime ( proc , proc . length ) ; } }
import java . io . * ; class GFG { static int MAX = 100001 ; static int minimumDeletions ( int A [ ] , int N ) { int [ ] [ ] dp = new int [ N ] [ MAX ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( A [ i ] >= A [ j ] ) { int diff = A [ i ] - A [ j ] ; for ( int k = 0 ; k <= diff ; k ++ ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] [ k ] + 1 ) ; } } } } int maxSetSize = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) maxSetSize = Math . max ( maxSetSize , dp [ N - 1 ] [ i ] ) ; return N - maxSetSize ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 5 , 7 , 20 , 21 } ; int N = A . length ; System . out . println ( minimumDeletions ( A , N ) ) ; } }
import java . util . * ; public class Main { static int findNumberOfStrings ( int N ) { Vector < Integer > counts = new Vector < Integer > ( ) ; for ( int i = 0 ; i < 5 ; i ++ ) { counts . add ( 1 ) ; } for ( int i = 2 ; i <= N ; i ++ ) { for ( int j = 3 ; j >= 0 ; j -- ) counts . set ( j , counts . get ( j ) + counts . get ( j + 1 ) ) ; } int ans = 0 ; for ( Integer c : counts ) ans += c ; return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . print ( findNumberOfStrings ( N ) ) ; } }
import java . util . * ; public class Main { static int dp [ ] [ ] = new int [ 1000 + 5 ] [ 9000 + 5 ] ; static int mod = 1000000007 ; public static int countNumbers ( int n , int x , int y , int sum ) { for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; if ( n == 0 ) { return check ( sum , x , y ) ; } if ( dp [ n ] [ sum ] != - 1 ) { return dp [ n ] [ sum ] % mod ; } int option1 = countNumbers ( n - 1 , x , y , sum + x ) % mod ; int option2 = countNumbers ( n - 1 , x , y , sum + y ) % mod ; return dp [ n ] [ sum ] = ( option1 + option2 ) % mod ; } public static int check ( int sum , int x , int y ) { while ( sum > 0 ) { int ln = sum % 10 ; if ( ln != x && ln != y ) { return 0 ; } sum /= 10 ; } return 1 ; } public static void main ( String args [ ] ) { int N = 3 , X = 1 , Y = 5 ; System . out . println ( countNumbers ( N , X , Y , 0 ) % mod ) ; } }
import java . util . * ; public class Main { public static int solve ( int a [ ] [ ] , String s , int n , int prev , int mask , int [ ] [ ] dp ) { if ( mask == 0 ) return 0 ; if ( dp [ mask ] [ prev + 1 ] != - 1 ) return dp [ mask ] [ prev + 1 ] ; int ans = 10000 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s . charAt ( i ) - ' a ' ; if ( check ( mask , id ) ) { if ( prev == - 1 ) { ans = Math . min ( ans , solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } else { ans = Math . min ( ans , a [ prev ] [ id ] + solve ( a , s , n , id , mask ^ ( 1 << id ) , dp ) ) ; } } } dp [ mask ] [ prev + 1 ] = ans ; return ans ; } public static boolean check ( int mask , int i ) { int c = ( mask & ( 1 << i ) ) ; return c != 0 ; } static void generatePermutation ( int mask , int n , int a [ ] [ ] , String s ) { int dp [ ] [ ] = new int [ ( 1 << n ) + 5 ] [ n + 5 ] ; for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int id = s . charAt ( i ) - ' a ' ; mask |= ( 1 << id ) ; } System . out . println ( solve ( a , s , n , - 1 , mask , dp ) ) ; } public static void main ( String args [ ] ) { int N = 5 ; String str = " abcde " ; int mat [ ] [ ] = { { 0 , 5 , 1 , 5 , 3 } , { 4 , 0 , 9 , 4 , 2 } , { 7 , 9 , 0 , 10 , 7 } , { 1 , 2 , 8 , 0 , 2 } , { 3 , 9 , 7 , 7 , 0 } } ; generatePermutation ( 0 , N , mat , str ) ; } }
import java . util . * ; public class Main { static int inf = 10000000 ; public static int minCost ( int a [ ] , int i , int j , int k , int [ ] prefix , int [ ] [ ] dp ) { if ( i >= j ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; int best_cost = inf ; for ( int pos = i ; pos < j ; pos ++ ) { int left = minCost ( a , i , pos , k , prefix , dp ) ; int right = minCost ( a , pos + 1 , j , k , prefix , dp ) ; best_cost = Math . min ( best_cost , left + right + ( k * Combine ( prefix , i , j ) ) ) ; } return dp [ i ] [ j ] = best_cost ; } public static int [ ] preprocess ( int [ ] a , int n ) { int p [ ] = new int [ n ] ; p [ 0 ] = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i ] = p [ i - 1 ] + a [ i ] ; return p ; } public static int Combine ( int [ ] p , int i , int j ) { if ( i == 0 ) return p [ j ] ; else return p [ j ] - p [ i - 1 ] ; } public static void main ( String args [ ] ) { int n = 4 ; int a [ ] = { 4 , 5 , 6 , 7 } ; int k = 3 ; int dp [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i [ ] : dp ) Arrays . fill ( i , - 1 ) ; int prefix [ ] = preprocess ( a , n ) ; System . out . println ( minCost ( a , 0 , n - 1 , k , prefix , dp ) ) ; } }
class GFG { static int minOperations ( int n ) { int dp [ ] = new int [ n + 1 ] ; dp [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ i ] = Integer . MAX_VALUE ; if ( i % 2 == 0 ) { int x = dp [ i / 2 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } if ( i % 3 == 0 ) { int x = dp [ i / 3 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } int x = dp [ i - 1 ] ; if ( x + 1 < dp [ i ] ) { dp [ i ] = x + 1 ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( minOperations ( n ) ) ; } }
import java . io . * ; class GFG { static int ReverseBitonic ( int arr [ ] , int N ) { int i , j ; int [ ] lds = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lds [ i ] = 1 ; } for ( i = 1 ; i < N ; i ++ ) { for ( j = 0 ; j < i ; j ++ ) { if ( arr [ i ] < arr [ j ] && lds [ i ] < lds [ j ] + 1 ) { lds [ i ] = lds [ j ] + 1 ; } } } int [ ] lis = new int [ N ] ; for ( i = 0 ; i < N ; i ++ ) { lis [ i ] = 1 ; } for ( i = N - 2 ; i >= 0 ; i -- ) { for ( j = N - 1 ; j > i ; j -- ) { if ( arr [ i ] < arr [ j ] && lis [ i ] < lis [ j ] + 1 ) { lis [ i ] = lis [ j ] + 1 ; } } } int max = lis [ 0 ] + lds [ 0 ] - 1 ; for ( i = 1 ; i < N ; i ++ ) { if ( lis [ i ] + lds [ i ] - 1 > max ) { max = lis [ i ] + lds [ i ] - 1 ; } } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ; int N = arr . length ; System . out . println ( " Length ▁ of ▁ LBS ▁ is ▁ " + ReverseBitonic ( arr , N ) ) ; } }
import java . io . * ; import java . util . ArrayList ; class GFG { static boolean findPath ( ArrayList < ArrayList < Integer > > maze , int xpos , int ypos , boolean key ) { if ( xpos < 0 || xpos >= maze . size ( ) || ypos < 0 || ypos >= maze . size ( ) ) return false ; if ( maze . get ( xpos ) . get ( ypos ) == '1' ) { if ( key == true ) if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , false ) || findPath ( maze , xpos , ypos + 1 , false ) ; } if ( xpos == maze . size ( ) - 1 && ypos == maze . size ( ) - 1 ) return true ; return findPath ( maze , xpos + 1 , ypos , key ) || findPath ( maze , xpos , ypos + 1 , key ) ; } static boolean mazeProb ( ArrayList < ArrayList < Integer > > maze , int xpos , int ypos ) { boolean key = true ; if ( findPath ( maze , xpos , ypos , key ) ) return true ; return false ; } public static void main ( String [ ] args ) { int size = 3 ; ArrayList < ArrayList < Integer > > maze = new ArrayList < ArrayList < Integer > > ( size ) ; for ( int i = 0 ; i < size ; i ++ ) { maze . add ( new ArrayList < Integer > ( ) ) ; } maze . get ( 0 ) . add ( 0 ) ; maze . get ( 0 ) . add ( 0 ) ; maze . get ( 0 ) . add ( 1 ) ; maze . get ( 1 ) . add ( 1 ) ; maze . get ( 1 ) . add ( 0 ) ; maze . get ( 1 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 1 ) ; maze . get ( 2 ) . add ( 0 ) ; if ( mazeProb ( maze , 0 , 0 ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GfG { static int FindMinimumCost ( int ind , int a [ ] , int n , int k , int dp [ ] ) { if ( ind == ( n - 1 ) ) return 0 ; else if ( dp [ ind ] != - 1 ) return dp [ ind ] ; else { int ans = Integer . MAX_VALUE ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ind + i < n ) ans = Math . min ( ans , Math . abs ( a [ ind + i ] - a [ ind ] ) + FindMinimumCost ( ind + i , a , n , k , dp ) ) ; else break ; } return dp [ ind ] = ans ; } } public static void main ( String [ ] args ) { int a [ ] = { 10 , 30 , 40 , 50 , 20 } ; int k = 3 ; int n = a . length ; int dp [ ] = new int [ n ] ; Arrays . fill ( dp , - 1 ) ; System . out . println ( FindMinimumCost ( 0 , a , n , k , dp ) ) ; } }
class GFG { static long answer ( int n ) { int [ ] dp = new int [ 10 ] ; int [ ] prev = new int [ 10 ] ; if ( n == 1 ) return 10 ; for ( int j = 0 ; j <= 9 ; j ++ ) dp [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 9 ; j ++ ) { prev [ j ] = dp [ j ] ; } for ( int j = 0 ; j <= 9 ; j ++ ) { if ( j == 0 ) dp [ j ] = prev [ j + 1 ] ; else if ( j == 9 ) dp [ j ] = prev [ j - 1 ] ; else dp [ j ] = prev [ j - 1 ] + prev [ j + 1 ] ; } } long sum = 0 ; for ( int j = 1 ; j <= 9 ; j ++ ) sum += dp [ j ] ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( answer ( n ) ) ; } }
class GFG { static final int MAX = 100000 ; static long prefix [ ] = new long [ MAX ] ; static void sieve_modified ( ) { for ( int i = 2 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } static long sumEvenFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void main ( String args [ ] ) { sieve_modified ( ) ; int l = 6 , r = 10 ; System . out . print ( sumEvenFactors ( l , r ) ) ; } }
class GFG1 { static int MAX = 100000 ; static long catalan [ ] = new long [ MAX ] ; static void catalanDP ( long n ) { catalan [ 0 ] = catalan [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { catalan [ i ] = 0 ; for ( int j = 0 ; j < i ; j ++ ) { catalan [ i ] += catalan [ j ] * catalan [ i - j - 1 ] ; } } } static int CatalanSequence ( int arr [ ] , int n ) { catalanDP ( n ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; int a = 1 , b = 1 ; int c ; s . add ( a ) ; if ( n >= 2 ) { s . add ( b ) ; } for ( int i = 2 ; i < n ; i ++ ) { s . add ( ( int ) catalan [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) { s . remove ( arr [ i ] ) ; } } return s . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 5 , 41 } ; int n = arr . length ; System . out . print ( CatalanSequence ( arr , n ) ) ; } }
import java . util . * ; class Solution { static final int M = 20 ; static int dp [ ] [ ] [ ] = new int [ M ] [ M ] [ 2 ] ; static int K ; static Vector < Integer > num ; static int countInRangeUtil ( int pos , int cnt , int tight ) { if ( pos == num . size ( ) ) { if ( cnt <= K ) return 1 ; return 0 ; } if ( dp [ pos ] [ cnt ] [ tight ] != - 1 ) return dp [ pos ] [ cnt ] [ tight ] ; int ans = 0 ; int limit = ( tight != 0 ? 9 : num . get ( pos ) ) ; for ( int dig = 0 ; dig <= limit ; dig ++ ) { int currCnt = cnt ; if ( dig != 0 ) currCnt ++ ; int currTight = tight ; if ( dig < num . get ( pos ) ) currTight = 1 ; ans += countInRangeUtil ( pos + 1 , currCnt , currTight ) ; } return dp [ pos ] [ cnt ] [ tight ] = ans ; } static int countInRange ( int x ) { num = new Vector < Integer > ( ) ; while ( x != 0 ) { num . add ( x % 10 ) ; x /= 10 ; } Collections . reverse ( num ) ; for ( int i = 0 ; i < M ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) for ( int k = 0 ; k < 2 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; return countInRangeUtil ( 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { int L = 1 , R = 1000 ; K = 3 ; System . out . println ( countInRange ( R ) - countInRange ( L - 1 ) ) ; L = 9995 ; R = 10005 ; K = 2 ; System . out . println ( countInRange ( R ) - countInRange ( L - 1 ) ) ; } }
public class GFG { static final int N = 1000 ; static int find ( int index , int openbrk , int n , int dp [ ] [ ] , int adj [ ] ) { if ( openbrk < 0 ) { return 0 ; } if ( index == n ) { if ( openbrk == 0 ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ openbrk ] != - 1 ) { return dp [ index ] [ openbrk ] ; } if ( adj [ index ] == 1 ) { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) ; } else { dp [ index ] [ openbrk ] = find ( index + 1 , openbrk + 1 , n , dp , adj ) + find ( index + 1 , openbrk - 1 , n , dp , adj ) ; } return dp [ index ] [ openbrk ] ; } public static void main ( String [ ] args ) { int dp [ ] [ ] = new int [ N ] [ N ] ; int n = 2 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int adj [ ] = { 1 , 0 , 0 , 0 } ; System . out . print ( find ( 0 , 0 , 2 * n , dp , adj ) ) ; } }
import java . io . * ; import java . lang . * ; public class GFG { static int ans ; static void findMaxValUtil ( int [ ] arr , int n , int num , int maxLimit , int ind ) { if ( ind == n ) { ans = Math . max ( ans , num ) ; return ; } if ( num - arr [ ind ] >= 0 ) { findMaxValUtil ( arr , n , num - arr [ ind ] , maxLimit , ind + 1 ) ; } if ( num + arr [ ind ] <= maxLimit ) { findMaxValUtil ( arr , n , num + arr [ ind ] , maxLimit , ind + 1 ) ; } } static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) { int ind = 0 ; findMaxValUtil ( arr , n , num , maxLimit , ind ) ; return ans ; } public static void main ( String args [ ] ) { int num = 1 ; int [ ] arr = { 3 , 10 , 6 , 4 , 5 } ; int n = arr . length ; int maxLimit = 15 ; System . out . print ( findMaxVal ( arr , n , num , maxLimit ) ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static List < Integer > set1 = new ArrayList < Integer > ( ) ; static List < Integer > set2 = new ArrayList < Integer > ( ) ; static void printSets ( ) { int i ; for ( i = 0 ; i < set1 . size ( ) ; i ++ ) { System . out . print ( set1 . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; for ( i = 0 ; i < set2 . size ( ) ; i ++ ) { System . out . print ( set2 . get ( i ) + " ▁ " ) ; } } static boolean findSets ( Integer [ ] arr , int n , int sum1 , int sum2 , int pos ) { if ( pos == n ) { if ( sum1 == sum2 ) { printSets ( ) ; return true ; } else return false ; } set1 . add ( arr [ pos ] ) ; boolean res = findSets ( arr , n , sum1 + arr [ pos ] , sum2 , pos + 1 ) ; if ( res == true ) return res ; set1 . remove ( set1 . size ( ) - 1 ) ; set2 . add ( arr [ pos ] ) ; res = findSets ( arr , n , sum1 , sum2 + arr [ pos ] , pos + 1 ) ; if ( res == false ) if ( set2 . size ( ) > 0 ) set2 . remove ( set2 . size ( ) - 1 ) ; return res ; } static boolean isPartitionPoss ( Integer [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; if ( sum % 2 != 0 ) return false ; return findSets ( arr , n , 0 , 0 , 0 ) ; } public static void main ( String args [ ] ) { Integer [ ] arr = { 5 , 5 , 1 , 11 } ; int n = arr . length ; if ( isPartitionPoss ( arr , n ) == false ) { System . out . print ( " - 1" ) ; } } }
class GFG { static final int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } public static void main ( String [ ] args ) { System . out . println ( getNumMonotone ( 10 ) ) ; } }
import java . util . * ; class GFG { static int sequence ( int n ) { int f [ ] = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; int i ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( sequence ( n ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static int lis ( int [ ] arr , int n ) { int [ ] mpis = new int [ n ] ; int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; for ( int k = 0 ; k < mpis . length ; k ++ ) { if ( mpis [ k ] > max ) { max = mpis [ k ] ; } } return max ; } static public void main ( String [ ] args ) { int [ ] arr = { 3 , 100 , 4 , 5 , 150 , 6 } ; int n = arr . length ; System . out . println ( lis ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } static void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( Hosoya ( i , j ) + " ▁ " ) ; System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printHosoya ( n ) ; } }
import java . util . * ; class Eulerian { public static int eulerian ( int n , int m ) { if ( m >= n n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; } public static void main ( String [ ] args ) { int n = 3 , m = 1 ; System . out . print ( eulerian ( n , m ) ) ; } }
import java . util . Arrays ; class GFG { static int largestSubset ( int [ ] a , int n ) { int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int mxm = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( a [ j ] % a [ i ] == 0 a [ i ] % a [ j ] == 0 ) { mxm = Math . max ( mxm , dp [ j ] ) ; } } dp [ i ] = 1 + mxm ; } return Arrays . stream ( dp ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 3 , 6 , 13 , 17 , 18 } ; int n = a . length ; System . out . println ( largestSubset ( a , n ) ) ; } }
import java . io . * ; class GFG { static int countFriendsPairings ( int n ) { int dp [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i <= 2 ) dp [ i ] = i ; else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
class GFG { static int countFriendsPairings ( int n ) { int a = 1 , b = 2 , c = 0 ; if ( n <= 2 ) { return n ; } for ( int i = 3 ; i <= n ; i ++ ) { c = b + ( i - 1 ) * a ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( countFriendsPairings ( n ) ) ; } }
import java . util . Arrays ; class GFG { static int R = 5 ; static int C = 3 ; static void precompute ( int mat [ ] [ ] , int ryt [ ] [ ] , int dwn [ ] [ ] ) { for ( int j = C - 1 ; j >= 0 ; j -- ) { for ( int i = 0 ; i < R ; ++ i ) { if ( mat [ i ] [ j ] == 0 ) { ryt [ i ] [ j ] = 0 ; else { ryt [ i ] [ j ] = ryt [ i ] [ j + 1 ] + 1 ; } } } for ( int i = R - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < C ; ++ j ) { if ( mat [ i ] [ j ] == 0 ) { dwn [ i ] [ j ] = 0 ; else { dwn [ i ] [ j ] = dwn [ i + 1 ] [ j ] + 1 ; } } } } static int solveRowSwap ( int ryt [ ] [ ] ) { int b [ ] = new int [ R ] , ans = 0 ; for ( int j = 0 ; j < C ; j ++ ) { for ( int i = 0 ; i < R ; i ++ ) { b [ i ] = ryt [ i ] [ j ] ; } Arrays . sort ( b ) ; for ( int i = 0 ; i < R ; ++ i ) { ans = Math . max ( ans , b [ i ] * ( R - i ) ) ; } } return ans ; } static int solveColumnSwap ( int dwn [ ] [ ] ) { int b [ ] = new int [ C ] , ans = 0 ; for ( int i = 0 ; i < R ; ++ i ) { for ( int j = 0 ; j < C ; ++ j ) { b [ j ] = dwn [ i ] [ j ] ; } Arrays . sort ( b ) ; for ( int k = 0 ; k < C ; ++ k ) { ans = Math . max ( ans , b [ k ] * ( C - k ) ) ; } } return ans ; } static void findMax1s ( int mat [ ] [ ] ) { int ryt [ ] [ ] = new int [ R + 2 ] [ C + 2 ] , dwn [ ] [ ] = new int [ R + 2 ] [ C + 2 ] ; precompute ( mat , ryt , dwn ) ; int rswap = solveRowSwap ( ryt ) ; int cswap = solveColumnSwap ( dwn ) ; if ( rswap > cswap ) { System . out . println ( "Row SwapNEW_LINE" + rswap); } else { System . out . println ( "Column SwapNEW_LINE" + cswap); } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 } , { 1 , 1 , 0 } , { 1 , 1 , 0 } , { 0 , 0 , 0 } , { 1 , 1 , 0 } } ; findMax1s ( mat ) ; } }
public class LCS_3Strings { static int lcsOf3 ( String X , String Y , String Z , int m , int n , int o ) { int [ ] [ ] [ ] L = new int [ m + 1 ] [ n + 1 ] [ o + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { for ( int k = 0 ; k <= o ; k ++ ) { if ( i == 0 j == 0 k == 0 ) L [ i ] [ j ] [ k ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) && X . charAt ( i - 1 ) == Z . charAt ( k - 1 ) ) L [ i ] [ j ] [ k ] = L [ i - 1 ] [ j - 1 ] [ k - 1 ] + 1 ; else L [ i ] [ j ] [ k ] = Math . max ( Math . max ( L [ i - 1 ] [ j ] [ k ] , L [ i ] [ j - 1 ] [ k ] ) , L [ i ] [ j ] [ k - 1 ] ) ; } } } return L [ m ] [ n ] [ o ] ; } public static void main ( String args [ ] ) { String X = " AGGT12" ; String Y = "12TXAYB " ; String Z = "12XBA " ; int m = X . length ( ) ; int n = Y . length ( ) ; int o = Z . length ( ) ; System . out . println ( " Length ▁ of ▁ LCS ▁ is ▁ " + lcsOf3 ( X , Y , Z , m , n , o ) ) ; } }
import java . lang . Math ; import java . util . * ; class GFG { static int longestSubsequence ( int n , int arr [ ] ) { if ( n == 1 ) return 1 ; Integer dp [ ] = new Integer [ n ] ; HashMap < Integer , Integer > mapp = new HashMap < > ( ) ; dp [ 0 ] = 1 ; mapp . put ( arr [ 0 ] , 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) == 1 ) dp [ i ] = dp [ i - 1 ] + 1 ; else { if ( mapp . containsKey ( arr [ i ] + 1 ) || mapp . containsKey ( arr [ i ] - 1 ) ) { dp [ i ] = 1 + Math . max ( mapp . getOrDefault ( arr [ i ] + 1 , 0 ) , mapp . getOrDefault ( arr [ i ] - 1 , 0 ) ) ; } else dp [ i ] = 1 ; } mapp . put ( arr [ i ] , dp [ i ] ) ; } return Collections . max ( Arrays . asList ( dp ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( longestSubsequence ( n , arr ) ) ; } }
import java . io . * ; import java . util . * ; class LRS { static int findLongestRepeatingSubSeq ( String str ) { int n = str . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str . charAt ( i - 1 ) == str . charAt ( j - 1 ) && i != j ) dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = Math . max ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ n ] ; } public static void main ( String [ ] args ) { String str = " aabb " ; System . out . println ( " The ▁ length ▁ of ▁ the ▁ largest ▁ subsequence ▁ that " + " ▁ repeats ▁ itself ▁ is ▁ : ▁ " + findLongestRepeatingSubSeq ( str ) ) ; } }
class GFG { static int [ ] [ ] [ ] [ ] lookup = new int [ 50 ] [ 1000 ] [ 1000 ] [ 2 ] ; static int countRec ( int digits , int esum , int osum , int isOdd , int n ) { if ( digits == n ) return ( esum - osum == 1 ) ? 1 : 0 ; if ( lookup [ digits ] [ esum ] [ osum ] [ isOdd ] != - 1 ) return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] ; int ans = 0 ; if ( isOdd == 1 ) for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum , osum + i , 0 , n ) ; for ( int i = 0 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return lookup [ digits ] [ esum ] [ osum ] [ isOdd ] = ans ; } static int finalCount ( int n ) { int digits = 0 ; for ( int i = 0 ; i < 50 ; i ++ ) for ( int j = 0 ; j < 1000 ; j ++ ) for ( int k = 0 ; k < 1000 ; k ++ ) for ( int l = 0 ; l < 2 ; l ++ ) lookup [ i ] [ j ] [ k ] [ l ] = - 1 ; int ans = 0 ; int esum = 0 , osum = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) ans += countRec ( digits + 1 , esum + i , osum , 1 , n ) ; return ans ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( " Count ▁ of ▁ " + n + " ▁ digit ▁ numbers ▁ is ▁ " + finalCount ( n ) ) ; } }
class GFG { static int numberOfPaths ( int m , int n ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } public static void main ( String args [ ] ) { System . out . println ( numberOfPaths ( 3 , 3 ) ) ; } }
import java . util . * ; class GFG { static int Solution ( int [ ] A ) { int ans = 2 ; int n = A . length ; if ( n <= 2 ) return n ; int [ ] llap = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) llap [ i ] = 2 ; Arrays . sort ( A ) ; for ( int j = n - 2 ; j >= 0 ; j -- ) { int i = j - 1 ; int k = j + 1 ; while ( i >= 0 && k < n ) { if ( A [ i ] + A [ k ] == 2 * A [ j ] ) { llap [ j ] = Math . max ( llap [ k ] + 1 , llap [ j ] ) ; ans = Math . max ( ans , llap [ j ] ) ; i -= 1 ; k += 1 ; } else if ( A [ i ] + A [ k ] < 2 * A [ j ] ) k += 1 ; else i -= 1 ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 9 , 4 , 7 , 2 , 10 } ; System . out . print ( Solution ( a ) + "NEW_LINE"); } }
import java . util . * ; class GFG { static int coinchange ( int [ ] a , int v , int n , int [ ] [ ] dp ) { if ( v == 0 ) return dp [ n ] [ v ] = 1 ; if ( n == 0 ) return 0 ; if ( dp [ n ] [ v ] != - 1 ) return dp [ n ] [ v ] ; if ( a [ n - 1 ] <= v ) { return dp [ n ] [ v ] = coinchange ( a , v - a [ n - 1 ] , n , dp ) + coinchange ( a , v , n - 1 , dp ) ; } return dp [ n ] [ v ] = coinchange ( a , v , n - 1 , dp ) ; } public static void main ( String [ ] args ) { int tc = 1 ; while ( tc != 0 ) { int n , v ; n = 3 ; v = 4 ; int [ ] a = { 1 , 2 , 3 } ; int [ ] [ ] dp = new int [ n + 1 ] [ v + 1 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; int res = coinchange ( a , v , n , dp ) ; System . out . println ( res ) ; tc -- ; } } }
import java . io . * ; class GFG { static void minumumOperation ( int N , int arr [ ] ) { int sum_arr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum_arr += arr [ i ] ; } if ( sum_arr >= N ) System . out . println ( sum_arr - N ) ; else System . out . println ( "1" ) ; } public static void main ( String [ ] args ) { int N = 4 ; int arr [ ] = { 8 , 4 , 6 , 2 } ; minumumOperation ( N , arr ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int factorial ( int N ) { if ( N == 0 N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }
import java . io . * ; class GFG { public static int Substr ( String s2 , String s1 ) { int i = 0 ; for ( ; i < s1 . length ( ) ; i ++ ) { if ( counter == s2 . length ( ) ) break ; if ( s2 . charAt ( counter ) == s1 . charAt ( i ) ) { counter ++ ; } else { if ( counter > 0 ) { i -= counter ; } counter = 0 ; } } return counter < s2 . length ( ) ? - 1 : i - counter ; } public static void main ( String [ ] args ) { String s1 = " geeksfffffoorrfoorforgeeks " ; System . out . println ( Substr ( " for " , s1 ) ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void manipulate ( String s ) { int n = s . length ( ) ; int i = 0 , j = 0 ; String res = " " ; while ( i < n ) { if ( s . charAt ( i ) == ' ▁ ' ) { res += util ( s . substring ( j , i ) ) ; res = res + " ▁ " ; j = i + 1 ; i = j + 1 ; } else { i ++ ; } } res = res + util ( s . substring ( j , i ) ) ; System . out . println ( res ) ; } public static String util ( String sub ) { int n = sub . length ( ) ; int i = 0 ; String ret = " " ; while ( i < n ) { int t = ( sub . charAt ( i ) - ' a ' ) + n - 1 - i ; char ch = ( char ) ( t % 26 + 97 ) ; ret = ret + String . valueOf ( ch ) ; i ++ ; } return ret ; } public static void main ( String [ ] args ) { String s = " acm ▁ fkz " ; manipulate ( s ) ; } }
class GFG { static int composite ( int n ) { int flag = 0 ; int c = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { if ( n % j == 0 ) { c += 1 ; } } if ( c >= 3 ) flag = 1 ; return flag ; } static void odd_indices ( int arr [ ] , int n ) { int sum = 0 ; for ( int k = 0 ; k < n ; k += 2 ) { int check = composite ( arr [ k ] ) ; if ( check == 1 ) sum += arr [ k ] ; } System . out . print ( sum + "NEW_LINE"); } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 5 , 8 , 16 , 25 } ; int n = arr . length ; odd_indices ( arr , n ) ; } }
class GFG { static int [ ] z_function ( char [ ] s ) { int n = s . length ; int [ ] z = new int [ n ] ; for ( int i = 1 , l = 0 , r = 0 ; i < n ; i ++ ) { if ( i <= r ) z [ i ] = Math . min ( r - i + 1 , z [ i - l ] ) ; while ( i + z [ i ] < n && s [ z [ i ] ] == s [ i + z [ i ] ] ) z [ i ] ++ ; if ( i + z [ i ] - 1 > r ) { l = i ; r = i + z [ i ] - 1 ; } } return z ; } static int n , len = 0 ; static int [ ] bit = new int [ 1000005 ] ; static String s ; static int [ ] z ; static void update ( int idx , int val ) { if ( idx == 0 ) return ; while ( idx <= n ) { bit [ idx ] += val ; idx += ( idx & - idx ) ; } } static int pref ( int idx ) { int ans = 0 ; while ( idx > 0 ) { ans += bit [ idx ] ; idx -= ( idx & - idx ) ; } return ans ; } public static void main ( String [ ] args ) { s = " geeksisforgeeksinplatformgeeks " ; z = new int [ s . length ( ) ] ; n = s . length ( ) ; z = z_function ( s . toCharArray ( ) ) ; for ( int i = 1 ; i < n ; i ++ ) { update ( z [ i ] , 1 ) ; } for ( int i = n - 1 ; i > 1 ; i -- ) { if ( z [ i ] != ( n - i ) ) continue ; if ( pref ( n ) - pref ( z [ i ] - 1 ) >= 2 ) { len = Math . max ( len , z [ i ] ) ; } } if ( len == 0 ) System . out . println ( " - 1" ) ; else System . out . println ( s . substring ( 0 , len ) ) ; } }
class GFG { static int endingWith ( String str , String suff ) { int cnt = 0 ; String words [ ] = str . split ( " ▁ " ) ; for ( int i = 0 ; i < words . length ; i ++ ) { if ( words [ i ] . endsWith ( suff ) ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String str = " GeeksForGeeks ▁ is ▁ a ▁ computer " + " ▁ science ▁ portal ▁ for ▁ geeks " ; String suff = " ks " ; System . out . print ( endingWith ( str , suff ) ) ; } }
class GFG { static boolean equalIgnoreCase ( String str1 , String str2 ) { int i = 0 ; str1 = str1 . toUpperCase ( ) ; str2 = str2 . toUpperCase ( ) ; int x = str1 . compareTo ( str2 ) ; if ( x != 0 ) { return false ; } else { return true ; } } static void equalIgnoreCaseUtil ( String str1 , String str2 ) { boolean res = equalIgnoreCase ( str1 , str2 ) ; if ( res == true ) { System . out . println ( " Same " ) ; } else { System . out . println ( " Not ▁ Same " ) ; } } public static void main ( String [ ] args ) { String str1 , str2 ; str1 = " Geeks " ; str2 = " geeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; str1 = " Geek " ; str2 = " geeksforgeeks " ; equalIgnoreCaseUtil ( str1 , str2 ) ; } }
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 " ) ; } } }
import java . util . * ; import java . lang . * ; class GFG { static String replaceConsonants ( String str ) { String res = " " ; int i = 0 , count = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) != ' a ' && str . charAt ( i ) != ' e ' && str . charAt ( i ) != ' i ' && str . charAt ( i ) != ' o ' && str . charAt ( i ) != ' u ' ) { i ++ ; count ++ ; } else { if ( count > 0 ) res += count ; res += str . charAt ( i ) ; i ++ ; count = 0 ; } } if ( count > 0 ) res += count ; return res ; } public static void main ( String [ ] args ) { String str = " abcdeiop " ; System . out . println ( replaceConsonants ( str ) ) ; } }
class GFG { static final int MAX = 26 ; static int countMinimalReplacements ( char [ ] s ) { int n = s . length ; int cnt = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { cnt += 1 ; if ( i != ( n - 1 ) ) { for ( char it : "012" . toCharArray ( ) ) { if ( it != s [ i + 1 ] && it != s [ i - 1 ] ) { s [ i ] = it ; break ; } } } { for ( char it : "012" . toCharArray ( ) ) { if ( it != s [ i - 1 ] ) { s [ i ] = it ; break ; } } } } } return cnt ; } public static void main ( String [ ] args ) { String s = "201220211" ; System . out . println ( countMinimalReplacements ( s . toCharArray ( ) ) ) ; } }
public class GFG { static int nextIndex ( String str , int start , char c ) { for ( int i = start ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == c ) return i ; } return - 1 ; } static int countSubStrings ( String str ) { int i , n = str . length ( ) ; int countX [ ] = new int [ n ] ; int count = 0 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == ' x ' ) count ++ ; countX [ i ] = count ; } int nextIndexX = nextIndex ( str , 0 , ' x ' ) ; int nextIndexY = nextIndex ( str , 0 , ' y ' ) ; count = 0 ; while ( nextIndexX != - 1 && nextIndexY != - 1 ) { if ( nextIndexX > nextIndexY ) { nextIndexY = nextIndex ( str , nextIndexY + 1 , ' y ' ) ; continue ; } else { count += countX [ nextIndexY ] ; nextIndexX = nextIndex ( str , nextIndexX + 1 , ' x ' ) ; } } return count ; } public static void main ( String [ ] args ) { String s = " xyyxx " ; System . out . println ( countSubStrings ( s ) ) ; } }
import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i ; if ( n == 1 ) { return false ; } for ( i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static boolean check_frequency ( char [ ] s ) { HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; { if ( m . containsKey ( s [ i ] ) ) { m . put ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . put ( s [ i ] , 1 ) ; } } for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { if ( m . get ( ch ) != null && m . get ( ch ) > 0 && ! isPrime ( m . get ( ch ) ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; if ( check_frequency ( s . toCharArray ( ) ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { public static String reduced_String ( int k , String s ) { if ( k == 1 ) { String ans = " " ; return ans ; } Stack < Pair > st = new Stack < Pair > ( ) ; int l = s . length ( ) ; int ctr = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . size ( ) == 0 ) { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; continue ; } if ( st . peek ( ) . c == s . charAt ( i ) ) { Pair p = st . peek ( ) ; st . pop ( ) ; p . ctr += 1 ; if ( p . ctr == k ) { continue ; } else { st . push ( p ) ; } } else { st . push ( new Pair ( s . charAt ( i ) , 1 ) ) ; } } String ans = " " ; while ( st . size ( ) > 0 ) { char c = st . peek ( ) . c ; int cnt = st . peek ( ) . ctr ; while ( cnt -- > 0 ) ans = c + ans ; st . pop ( ) ; } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; String st = " geeksforgeeks " ; String ans = reduced_String ( k , st ) ; System . out . println ( ans ) ; } static class Pair { char c ; int ctr ; Pair ( char c , int ctr ) { this . c = c ; this . ctr = ctr ; } } }
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int firstNonRepeating ( String str ) { int NO_OF_CHARS = 256 ; int arr [ ] = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) arr [ i ] = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( arr [ str . charAt ( i ) ] == - 1 ) arr [ str . charAt ( i ) ] = i ; else arr [ str . charAt ( i ) ] = - 2 ; } int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( arr [ i ] >= 0 ) res = Math . min ( res , arr [ i ] ) ; return res ; } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; int index = firstNonRepeating ( str ) ; if ( index == Integer . MAX_VALUE ) System . out . print ( " Either ▁ all ▁ characters ▁ are ▁ " + " repeating ▁ or ▁ string ▁ is ▁ empty " ) ; else System . out . print ( " First ▁ non - repeating ▁ character " + " ▁ is ▁ " + str . charAt ( index ) ) ; } }
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 ) ; } }
class LatinCipher { static void cipher ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ! Character . isLetter ( str . charAt ( i ) ) && str . charAt ( i ) != ' ▁ ' ) { System . out . println ( " Enter ▁ only ▁ alphabets ▁ and ▁ space " ) ; return ; } } System . out . println ( " Encrypted ▁ Code ▁ using ▁ Latin ▁ Alphabet " ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) { System . out . print ( str . charAt ( i ) - ' A ' + 1 + " ▁ " ) ; } else if ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { System . out . print ( str . charAt ( i ) - ' a ' + 1 + " ▁ " ) ; } if ( str . charAt ( i ) == ' ▁ ' ) System . out . print ( str . charAt ( i ) ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; cipher ( str ) ; } }
class GFG { static boolean checkPalin ( String word ) { int n = word . length ( ) ; word = word . toLowerCase ( ) ; for ( int i = 0 ; i < n ; i ++ , n -- ) if ( word . charAt ( i ) != word . charAt ( n - 1 ) ) return false ; return true ; } static int countPalin ( String str ) { str = str + " ▁ " ; String word = " " ; int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { if ( checkPalin ( word ) ) count ++ ; word = " " ; } } return count ; } public static void main ( String args [ ] ) { System . out . println ( countPalin ( " Madam ▁ " + " Arora ▁ teaches ▁ malayalam " ) ) ; System . out . println ( countPalin ( " Nitin ▁ " + " speaks ▁ malayalam " ) ) ; } }
import java . io . * ; class rtf { public static int n ; public static String z ; public static String s [ ] = new String [ 100 ] ; public static char w [ ] ; public static char letter ; public static boolean pre [ ] = new boolean [ 100 ] ; public static void verify ( int position , int index ) { int l = z . length ( ) ; int k = s [ index ] . length ( ) ; if ( position + k > l ) return ; boolean same = true ; for ( int i = position ; i < position + k ; i ++ ) { int n , n1 ; char ch = w [ i ] ; char ch1 = s [ index ] . charAt ( i - position ) ; if ( ch >= ' a ' && ch <= ' z ' ) n = ch - ' a ' ; else n = ch - ' A ' ; if ( ch1 >= ' a ' && ch1 <= ' z ' ) n1 = ch1 - ' a ' ; else n1 = ch1 - ' A ' ; if ( n != n1 ) same = false ; } if ( same == true ) { for ( int i = position ; i < position + k ; i ++ ) pre [ i ] = true ; return ; } } public static void solve ( ) { w = z . toCharArray ( ) ; letter = ' d ' ; int l = z . length ( ) ; int p = letter - ' a ' ; for ( int i = 0 ; i < 100 ; i ++ ) pre [ i ] = false ; for ( int i = 0 ; i < l ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) verify ( i , j ) ; } for ( int i = 0 ; i < l ; i ++ ) { if ( pre [ i ] == true ) { if ( w [ i ] == letter ) w [ i ] = ( letter == ' a ' ) ? ' b ' : ' a ' ; else if ( w [ i ] == ( char ) ( ( int ) ' A ' + p ) ) w [ i ] = ( letter == ' a ' ) ? ' B ' : ' A ' ; else if ( w [ i ] >= ' a ' && w [ i ] <= ' z ' ) w [ i ] = letter ; else if ( w [ i ] >= ' A ' && w [ i ] <= ' Z ' ) w [ i ] = ( char ) ( ( int ) ' A ' + p ) ; } } System . out . println ( w ) ; } public static void main ( String args [ ] ) { n = 3 ; s [ 0 ] = " etr " ; s [ 1 ] = " ed " ; s [ 2 ] = " ied " ; z = " PEtrUnited " ; solve ( ) ; } }
import java . util . * ; class GFG { static int calculate ( String N ) { int len = N . length ( ) ; int l = ( len ) / 2 ; int count = 0 ; for ( int i = 1 ; i <= l ; i ++ ) { String s = N . substring ( 0 , i ) ; int l1 = s . length ( ) ; String t = N . substring ( i , l1 + i ) ; if ( s . charAt ( 0 ) == '0' || t . charAt ( 0 ) == '0' ) continue ; if ( s . compareTo ( t ) == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String N = "2202200" ; System . out . print ( calculate ( N ) ) ; } }
import java . io . * ; class GFG { static int countOccurrences ( String s , int K ) { int n = s . length ( ) ; int C = 0 , c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' ) c1 ++ ; if ( s . charAt ( i ) == ' b ' ) { c2 ++ ; C += c1 ; } } return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ; } public static void main ( String [ ] args ) { String S = " abcb " ; int k = 2 ; System . out . println ( countOccurrences ( S , k ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void printStrongNess ( String input ) { int n = input . length ( ) ; boolean hasLower = false , hasUpper = false , hasDigit = false , specialChar = false ; Set < Character > set = new HashSet < Character > ( Arrays . asList ( ' ! ' , ' @ ' , ' # ' , ' $ ' , ' % ' , ' ^ ' , ' & ' , ' * ' , ' ( ' , ' ) ' , ' - ' , ' + ' ) ) ; for ( char i : input . toCharArray ( ) ) { if ( Character . isLowerCase ( i ) ) hasLower = true ; if ( Character . isUpperCase ( i ) ) hasUpper = true ; if ( Character . isDigit ( i ) ) hasDigit = true ; if ( set . contains ( i ) ) specialChar = true ; } System . out . print ( " Strength ▁ of ▁ password : - ▁ " ) ; if ( hasDigit && hasLower && hasUpper && specialChar && ( n >= 8 ) ) System . out . print ( " ▁ Strong " ) ; else if ( ( hasLower hasUpper specialChar ) && ( n >= 6 ) ) System . out . print ( " ▁ Moderate " ) ; else System . out . print ( " ▁ Weak " ) ; } public static void main ( String [ ] args ) { String input = " GeeksforGeeks ! @12" ; printStrongNess ( input ) ; } }
import java . io . * ; public class GFG { static String findString ( int n , int k ) { String res = " " ; for ( int i = 0 ; i < k ; i ++ ) res = res + ( char ) ( ' a ' + i ) ; int count = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { res = res + ( char ) ( ' a ' + count ) ; count ++ ; if ( count == k ) count = 0 ; } return res ; } static public void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( findString ( n , k ) ) ; } }
import java . util . Scanner ; public class CreditCard { public static void main ( String [ ] args ) { long number = 5196081888500645L ; System . out . println ( number + " ▁ is ▁ " + ( isValid ( number ) ? " valid " : " invalid " ) ) ; } public static boolean isValid ( long number ) { return ( getSize ( number ) >= 13 && getSize ( number ) <= 16 ) && ( prefixMatched ( number , 4 ) || prefixMatched ( number , 5 ) || prefixMatched ( number , 37 ) || prefixMatched ( number , 6 ) ) && ( ( sumOfDoubleEvenPlace ( number ) + sumOfOddPlace ( number ) ) % 10 == 0 ) ; } public static int sumOfDoubleEvenPlace ( long number ) { int sum = 0 ; String num = number + " " ; for ( int i = getSize ( number ) - 2 ; i >= 0 ; i -= 2 ) sum += getDigit ( Integer . parseInt ( num . charAt ( i ) + " " ) * 2 ) ; return sum ; } public static int getDigit ( int number ) { if ( number < 9 ) return number ; return number / 10 + number % 10 ; } public static int sumOfOddPlace ( long number ) { int sum = 0 ; String num = number + " " ; for ( int i = getSize ( number ) - 1 ; i >= 0 ; i -= 2 ) sum += Integer . parseInt ( num . charAt ( i ) + " " ) ; return sum ; } public static boolean prefixMatched ( long number , int d ) { return getPrefix ( number , getSize ( d ) ) == d ; } public static int getSize ( long d ) { String num = d + " " ; return num . length ( ) ; } public static long getPrefix ( long number , int k ) { if ( getSize ( number ) > k ) { String num = number + " " ; return Long . parseLong ( num . substring ( 0 , k ) ) ; } return number ; } }
import java . util . HashSet ; public class GFG { static boolean isVowel ( char x ) { return ( x == ' a ' x == ' e ' x == ' i ' x == ' o ' x == ' u ' ) ; } static void findSubstring ( String str ) { HashSet < Character > hash = new HashSet < Character > ( ) ; int start = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) == true ) { hash . add ( str . charAt ( i ) ) ; if ( hash . size ( ) == 5 ) System . out . print ( str . substring ( start , i + 1 ) + " ▁ " ) ; } else { start = i + 1 ; hash . clear ( ) ; } } } public static void main ( String [ ] args ) { String str = " aeoibsddaeiouudb " ; findSubstring ( str ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class gfg { public static String concatenatedString ( String s1 , String s2 ) { String res = " " ; int i ; HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( i = 0 ; i < s2 . length ( ) ; i ++ ) m . put ( s2 . charAt ( i ) , 1 ) ; for ( i = 0 ; i < s1 . length ( ) ; i ++ ) if ( ! m . containsKey ( s1 . charAt ( i ) ) ) res += s1 . charAt ( i ) ; else m . put ( s1 . charAt ( i ) , 2 ) ; for ( i = 0 ; i < s2 . length ( ) ; i ++ ) if ( m . get ( s2 . charAt ( i ) ) == 1 ) res += s2 . charAt ( i ) ; return res ; } public static void main ( String [ ] args ) { String s1 = " abcs " ; String s2 = " cxzca " ; System . out . println ( concatenatedString ( s1 , s2 ) ) ; } }
import java . util . HashSet ; public class Subsequence { static HashSet < String > st = new HashSet < > ( ) ; static void subsequence ( String str ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) { for ( int j = ( str . length ( ) - 1 ) ; j >= i ; j -- ) { if ( isConsonant ( str . charAt ( ( j ) ) ) ) { String str_sub = str . substring ( i , j + 1 ) ; st . add ( str_sub ) ; for ( int k = 1 ; k < str_sub . length ( ) - 1 ; k ++ ) { StringBuffer sb = new StringBuffer ( str_sub ) ; sb . deleteCharAt ( k ) ; subsequence ( sb . toString ( ) ) ; } } } } } } static boolean isVowel ( char c ) { return ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) ; } static boolean isConsonant ( char c ) { return ! isVowel ( c ) ; } public static void main ( String [ ] args ) { String s = " xabcef " ; subsequence ( s ) ; System . out . println ( st ) ; } }
class GFG { static String decode ( String str ) { String res = " " ; boolean found = false ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) { found = false ; continue ; } if ( ! found ) { if ( ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) || ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) ) { res += Character . toString ( str . charAt ( i ) ) ; found = true ; } } } return res . toLowerCase ( ) ; } public static void main ( String [ ] args ) { String in ; in = " A ▁ Step ▁ by ▁ Step ▁ Guide ▁ for ▁ Placement ▁ Preparation ▁ by ▁ GeeksforGeeks " ; System . out . println ( " Enciphered ▁ Message : ▁ " + decode ( in ) ) ; } }
public class GFG { static int isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; if ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) return 1 ; else return 0 ; } static int countVowels ( String str , int n ) { if ( n == 1 ) return isVowel ( str . charAt ( n - 1 ) ) ; return countVowels ( str , n - 1 ) + isVowel ( str . charAt ( n - 1 ) ) ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( countVowels ( str , str . length ( ) ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static String findSubString ( String str ) { int n = str . length ( ) ; int dist_count = 0 ; HashMap < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( str . charAt ( i ) ) ) { Integer a = mp . get ( str . charAt ( i ) ) ; mp . put ( str . charAt ( i ) , a + 1 ) ; } else { mp . put ( str . charAt ( i ) , 1 ) ; } } dist_count = mp . size ( ) ; int size = Integer . MAX_VALUE ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; int visited [ ] = new int [ 256 ] ; for ( int j = 0 ; j < 256 ; j ++ ) visited [ j ] = 0 ; String sub_str = " " ; for ( int j = i ; j < n ; j ++ ) { if ( visited [ str . charAt ( j ) ] == 0 ) { count ++ ; visited [ str . charAt ( j ) ] = 1 ; } sub_str += str . charAt ( j ) ; if ( count == dist_count ) break ; } if ( sub_str . length ( ) < size && count == dist_count ) { res = sub_str ; size = res . length ( ) ; } } return res ; } public static void main ( String [ ] args ) { String str = " aabcbcdbca " ; System . out . println ( " Smallest ▁ window ▁ containing ▁ all ▁ distinct " + " ▁ characters ▁ is : ▁ " + findSubString ( str ) ) ; } }
class Test { static void printRotatedString ( String str ) { int n = str . length ( ) ; StringBuffer sb = new StringBuffer ( str ) ; sb . append ( str ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j != n ; j ++ ) System . out . print ( sb . charAt ( i + j ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { String str = new String ( " geeks " ) ; printRotatedString ( str ) ; } }
public class GFG { static final int M = 26 ; static int getIdx ( char ch ) { return ( ch - ' a ' ) ; } static boolean allSame ( int freq [ ] , int N ) { int same = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( freq [ i ] > 0 ) { same = freq [ i ] ; break ; } } for ( int j = i + 1 ; j < N ; j ++ ) if ( freq [ j ] > 0 && freq [ j ] != same ) return false ; return true ; } static boolean possibleSameCharFreqByOneRemoval ( String str ) { int l = str . length ( ) ; int [ ] freq = new int [ M ] ; for ( int i = 0 ; i < l ; i ++ ) freq [ getIdx ( str . charAt ( i ) ) ] ++ ; if ( allSame ( freq , M ) ) return true ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { int i = getIdx ( c ) ; if ( freq [ i ] > 0 ) { freq [ i ] -- ; if ( allSame ( freq , M ) ) return true ; freq [ i ] ++ ; } } return false ; } public static void main ( String args [ ] ) { String str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str . charAt ( i ) - '0' ) ; else evenDigSum += ( str . charAt ( i ) - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76945" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . List ; public class Palin_pair2 { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; int id ; boolean isLeaf ; public TrieNode ( ) { isLeaf = false ; pos = new ArrayList < > ( ) ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; } } static boolean isPalindrome ( String str , int i , int len ) { while ( i < len ) { if ( str . charAt ( i ) != str . charAt ( len ) ) return false ; i ++ ; len -- ; } return true ; } static void insert ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = key . length ( ) - 1 ; level >= 0 ; level -- ) { int index = key . charAt ( level ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; if ( isPalindrome ( key , 0 , level ) ) ( pCrawl . pos ) . add ( id ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . id = id ; pCrawl . pos . add ( id ) ; pCrawl . isLeaf = true ; } static List < List < Integer > > result ; static void search ( TrieNode root , String key , int id ) { TrieNode pCrawl = root ; for ( int level = 0 ; level < key . length ( ) ; level ++ ) { int index = key . charAt ( level ) - ' a ' ; if ( pCrawl . id >= 0 && pCrawl . id != id && isPalindrome ( key , level , key . length ( ) - 1 ) ) { List < Integer > l = new ArrayList < > ( ) ; l . add ( id ) ; l . add ( pCrawl . id ) ; result . add ( l ) ; } if ( pCrawl . children [ index ] == null ) return ; pCrawl = pCrawl . children [ index ] ; } for ( int i : pCrawl . pos ) { if ( i == id ) continue ; List < Integer > l = new ArrayList < > ( ) ; l . add ( id ) ; l . add ( i ) ; result . add ( l ) ; } } static boolean checkPalindromePair ( List < String > vect ) { TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < vect . size ( ) ; i ++ ) insert ( root , vect . get ( i ) , i ) ; result = new ArrayList < > ( ) ; for ( int i = 0 ; i < vect . size ( ) ; i ++ ) { search ( root , vect . get ( i ) , i ) ; if ( result . size ( ) > 0 ) return true ; } return false ; } public static void main ( String args [ ] ) { List < String > vect = Arrays . asList ( " geekf " , " geeks " , " or " , " keeg " , " abc " , " bc " ) ; if ( checkPalindromePair ( vect ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; import java . lang . * ; public class BinaryS { public static String toString ( char [ ] a ) { String string = new String ( a ) ; return string ; } static void generate ( int k , char [ ] ch , int n ) { if ( n == k ) { System . out . print ( toString ( ch ) + " ▁ " ) ; return ; } if ( ch [ n - 1 ] == '0' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; ch [ n ] = '1' ; generate ( k , ch , n + 1 ) ; } if ( ch [ n - 1 ] == '1' ) { ch [ n ] = '0' ; generate ( k , ch , n + 1 ) ; } } static void fun ( int k ) { if ( k <= 0 ) { return ; } char [ ] ch = new char [ k ] ; ch [ 0 ] = '0' ; generate ( k , ch , 1 ) ; ch [ 0 ] = '1' ; generate ( k , ch , 1 ) ; } public static void main ( String args [ ] ) { int k = 3 ; fun ( k ) ; } }
public class GFG { static final int MAX_CHAR = 26 ; static boolean arekAnagrams ( String str1 , String str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] > count2 [ i ] ) count = count + Math . abs ( count1 [ i ] - count2 [ i ] ) ; return ( count <= k ) ; } public static void main ( String args [ ] ) { String str1 = " anagram " ; String str2 = " grammar " ; int k = 2 ; if ( arekAnagrams ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int removeSpaces ( char [ ] str ) { int count = 0 ; for ( int i = 0 ; i < str . length ; i ++ ) if ( str [ i ] != ' ▁ ' ) return count ; } public static void main ( String [ ] args ) { char str [ ] = " g ▁ eeks ▁ for ▁ ge ▁ eeks ▁ " . toCharArray ( ) ; int i = removeSpaces ( str ) ; System . out . println ( String . valueOf ( str ) . subSequence ( 0 , i ) ) ; } }
class CountSubString { int countSubStr ( char str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; } public static void main ( String [ ] args ) { CountSubString count = new CountSubString ( ) ; String string = "00100101" ; char str [ ] = string . toCharArray ( ) ; int n = str . length ; System . out . println ( count . countSubStr ( str , n ) ) ; } }
import java . io . * ; class GFG { static int count9s ( String number ) { int count = 0 ; int n = number . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = number . charAt ( i ) - '0' ; if ( number . charAt ( i ) == '9' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { sum = ( sum + number . charAt ( j ) - '0' ) % 9 ; if ( sum == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { System . out . println ( count9s ( "4189" ) ) ; System . out . println ( count9s ( "1809" ) ) ; } }
import java . util . * ; class GFG { static void divide ( String str , int n ) { if ( str . length ( ) % n != 0 ) { System . out . print ( " Invalid ▁ Input : ▁ String ▁ size " ) ; System . out . print ( " ▁ is ▁ not ▁ divisible ▁ by ▁ n " ) ; return ; } int parts = str . length ( ) / n ; int start = 0 ; int t = parts ; while ( start < str . length ( ) ) { String temp = new String ( str ) ; System . out . print ( temp . substring ( start , parts ) + "NEW_LINE"); start = parts ; parts += t ; } } public static void main ( String [ ] args ) { String str = " a _ simple _ divide _ String _ quest " ; divide ( str , 4 ) ; } }
import java . io . * ; import java . util . * ; class Sol { int [ ] [ ] countMap ; int cnt = Integer . MAX_VALUE ; public int minStickers ( String [ ] stickers , String target ) { if ( target == null ) return - 1 ; if ( target . length ( ) == 0 ) return 0 ; if ( stickers == null stickers . length == 0 ) return - 1 ; int m = stickers . length ; countMap = new int [ m ] [ 26 ] ; for ( int i = 0 ; i < stickers . length ; i ++ ) { String s = stickers [ i ] ; for ( char c : s . toCharArray ( ) ) { countMap [ i ] ++ ; } } count ( 0 , 0 , new int [ 26 ] , target , stickers ) ; return cnt == Integer . MAX_VALUE ? - 1 : cnt ; } private void count ( int curCnt , int pos , int [ ] charAvailable , String target , String [ ] stickers ) { if ( curCnt >= cnt ) return ; int m = stickers . length ; int n = target . length ( ) ; if ( pos == n ) { cnt = Math . min ( cnt , curCnt ) ; return ; } char c = target . charAt ( pos ) ; if ( charAvailable > 0 ) { charAvailable -- ; count ( curCnt , pos + 1 , charAvailable , target , stickers ) ; charAvailable ++ ; } else { for ( int i = 0 ; i < m ; i ++ ) { if ( countMap [ i ] == 0 ) continue ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] += countMap [ i ] [ j ] ; } count ( curCnt + 1 , pos , charAvailable , target , stickers ) ; for ( int j = 0 ; j < 26 ; j ++ ) { charAvailable [ j ] -= countMap [ i ] [ j ] ; } } } } } class GFG { public static void main ( String [ ] args ) { Sol st = new Sol ( ) ; String [ ] str = { " with " , " example " , " science " } ; String target = " thehat " ; int Result = st . minStickers ( str , target ) ; System . out . println ( Result ) ; } }
class GfG { static void breakLine ( ) { System . out . print ( " -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - "); } static int MAX = 10 ; static int arr [ ] = new int [ MAX ] , no ; static void nQueens ( int k , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) { display ( n ) ; } else { nQueens ( k + 1 , n ) ; } } } } static boolean canPlace ( int k , int i ) { for ( int j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( Math . abs ( arr [ j ] - i ) == Math . abs ( j - k ) ) ) { return false ; } } return true ; } static void display ( int n ) { breakLine ( ) ; System . out . print ( " Arrangement ▁ No . ▁ " + ++ no ) ; breakLine ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) { System . out . print ( " TABSYMBOL _ " ) ; } else { System . out . print ( " TABSYMBOL Q " ) ; } } System . out . println ( " " ) ; } breakLine ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; nQueens ( 1 , n ) ; } }
import java . io . * ; class GFG { static int R = 3 ; static int C = 10 ; static class Pair { boolean found ; int val ; Pair ( boolean x , int y ) { found = x ; val = y ; } } static Pair findLongestPathUtil ( int mat [ ] [ ] , int i , int j , int x , int y , boolean visited [ ] [ ] ) { if ( i == x && j == y ) return new Pair ( true , 0 ) ; if ( i < 0 i >= R j < 0 j >= C mat [ i ] [ j ] == 0 visited [ i ] [ j ] ) return new Pair ( false , Integer . MAX_VALUE ) ; visited [ i ] [ j ] = true ; int res = Integer . MIN_VALUE ; Pair sol = findLongestPathUtil ( mat , i , j - 1 , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i , j + 1 , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i - 1 , j , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; sol = findLongestPathUtil ( mat , i + 1 , j , x , y , visited ) ; if ( sol . found ) res = Math . max ( sol . val , res ) ; visited [ i ] [ j ] = false ; if ( res != Integer . MIN_VALUE ) return new Pair ( true , res + 1 ) ; else return new Pair ( false , Integer . MAX_VALUE ) ; } static void findLongestPath ( int mat [ ] [ ] , int i , int j , int x , int y ) { boolean visited [ ] [ ] = new boolean [ R ] [ C ] ; Pair p = findLongestPathUtil ( mat , i , j , x , y , visited ) ; if ( p . found ) System . out . println ( " Length ▁ of ▁ longest ▁ possible ▁ route ▁ is ▁ " + p . val ) ; else System . out . println ( " Destination ▁ not ▁ reachable ▁ from ▁ given ▁ source " ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 1 } , { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } } ; findLongestPath ( mat , 0 , 0 , 1 , 7 ) ; } }
public class GFG { static int V = 4 ; static void printSolution ( int [ ] color ) { System . out . println ( " Solution ▁ Exists : " + " ▁ Following ▁ are ▁ the ▁ assigned ▁ colors ▁ " ) ; for ( int i = 0 ; i < V ; i ++ ) System . out . print ( " ▁ " + color [ i ] ) ; System . out . println ( ) ; } static boolean isSafe ( boolean [ ] [ ] graph , int [ ] color ) { for ( int i = 0 ; i < V ; i ++ ) for ( int j = i + 1 ; j < V ; j ++ ) if ( graph [ i ] [ j ] && color [ j ] == color [ i ] ) return false ; return true ; } static boolean graphColoring ( boolean [ ] [ ] graph , int m , int i , int [ ] color ) { if ( i == V ) { if ( isSafe ( graph , color ) ) { printSolution ( color ) ; return true ; } return false ; } for ( int j = 1 ; j <= m ; j ++ ) { color [ i ] = j ; if ( graphColoring ( graph , m , i + 1 , color ) ) return true ; color [ i ] = 0 ; } return false ; } public static void main ( String [ ] args ) { boolean [ ] [ ] graph = { { false , true , true , true } , { true , false , true , false } , { true , true , false , true } , { true , false , true , false } , } ; int [ ] color = new int [ V ] ; for ( int i = 0 ; i < V ; i ++ ) color [ i ] = 0 ; if ( ! graphColoring ( graph , m , 0 , color ) ) System . out . println ( " Solution ▁ does ▁ not ▁ exist " ) ; } }
import java . util . HashSet ; class CP { public static void main ( String [ ] args ) { int [ ] A = { 11 , 15 , 7 , 19 } ; int N = A . length ; HashSet < Integer > set = new HashSet < > ( ) ; int i , j , res ; for ( i = 0 ; i < N ; ++ i ) for ( j = i , res = Integer . MAX_VALUE ; j < N ; ++ j ) { res &= A [ j ] ; set . add ( res ) ; } System . out . println ( set ) ; } }
import java . util . * ; import java . io . * ; public class RangeQueries { static class Query { long L ; long R ; } public static int upperBound ( ArrayList < Long > validNumbers , Long value ) { int low = 0 ; int high = validNumbers . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( value >= validNumbers . get ( mid ) ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void answerQueries ( ArrayList < Query > queries ) { Set < Long > allNum = new HashSet < > ( ) ; allNum . add ( 0L ) ; for ( int i = 0 ; i <= 60 ; i ++ ) { for ( int j = 0 ; j <= 60 ; j ++ ) { for ( int k = 0 ; k <= 60 ; k ++ ) { if ( i == j && j == k ) { allNum . add ( 1L << i ) ; } else if ( i == j && j != k ) { long toAdd = ( 1L << i ) + ( 1L << k ) ; allNum . add ( toAdd ) ; } else if ( i == k && k != j ) { long toAdd = ( 1L << i ) + ( 1L << j ) ; allNum . add ( toAdd ) ; } else if ( j == k && k != i ) { long toAdd = ( 1L << j ) + ( 1L << i ) ; allNum . add ( toAdd ) ; } else { long toAdd = ( 1L << i ) + ( 1L << j ) + ( 1L << k ) ; allNum . add ( toAdd ) ; } } } } ArrayList < Long > validNumbers = new ArrayList < > ( ) ; for ( Long num : allNum ) { validNumbers . add ( num ) ; } Collections . sort ( validNumbers ) ; for ( int i = 0 ; i < queries . size ( ) ; i ++ ) { long L = queries . get ( i ) . L ; long R = queries . get ( i ) . R ; if ( R < L ) { long temp = L ; L = R ; R = temp ; } if ( L == 0 ) { int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( indxOfLastNum + 1 ) ; } else { int indxOfFirstNum = upperBound ( validNumbers , L ) ; int indxOfLastNum = upperBound ( validNumbers , R ) ; System . out . println ( ( indxOfLastNum - indxOfFirstNum + 1 ) ) ; } } } public static void main ( String [ ] args ) { int Q = 2 ; ArrayList < Query > queries = new ArrayList < > ( ) ; Query q1 = new Query ( ) ; q1 . L = 3 ; q1 . R = 7 ; Query q2 = new Query ( ) ; q2 . L = 10 ; q2 . R = 16 ; queries . add ( q1 ) ; queries . add ( q2 ) ; answerQueries ( queries ) ; } }
import java . util . * ; class GFG { public static int minStepsRecur ( int height [ ] , int l , int r , int h ) { if ( l >= r ) return 0 ; int m = l ; for ( int i = l ; i < r ; i ++ ) if ( height [ i ] < height [ m ] ) m = i ; return Math . min ( r - l , minStepsRecur ( height , l , m , height [ m ] ) + minStepsRecur ( height , m + 1 , r , height [ m ] ) + height [ m ] - h ) ; } public static int minSteps ( int height [ ] , int N ) { return minStepsRecur ( height , 0 , N , 0 ) ; } public static void main ( String [ ] args ) { int height [ ] = { 2 , 1 , 2 , 5 , 1 } ; int N = height . length ; System . out . println ( minSteps ( height , N ) ) ; } }
import java . util . ArrayList ; class GFG { static class pair < T , V > { T fi ; V se ; pair ( T a , V b ) { this . fi = a ; this . se = b ; } } static void findPoint ( int n , ArrayList < pair < Integer , Integer > > p ) { for ( int i = 0 ; i < n * 4 + 1 ; ++ i ) { int x1 = ( int ) 2e9 , x2 = ( int ) - 2e9 ; int y1 = ( int ) 2e9 , y2 = ( int ) - 2e9 ; for ( int j = 0 ; j < n * 4 + 1 ; ++ j ) if ( i != j ) { x1 = Math . min ( x1 , p . get ( j ) . fi ) ; x2 = Math . max ( x2 , p . get ( j ) . fi ) ; y1 = Math . min ( y1 , p . get ( j ) . se ) ; y2 = Math . max ( y2 , p . get ( j ) . se ) ; } boolean ok = true ; int c1 = 0 , c2 = 0 ; int c3 = 0 , c4 = 0 ; for ( int j = 1 ; j < n * 4 + 1 ; ++ j ) if ( i != j ) { if ( ( p . get ( j ) . fi == x1 || p . get ( j ) . fi == x2 ) || ( ( p . get ( j ) . se == y1 || p . get ( j ) . se == y2 ) ) ) { if ( p . get ( j ) . fi == x1 ) ++ c1 ; if ( p . get ( j ) . fi == x2 ) ++ c2 ; if ( p . get ( j ) . se == y1 ) ++ c3 ; if ( p . get ( j ) . se == y2 ) ++ c4 ; } else ok = false ; } if ( ok && c1 >= n && c2 >= n && c3 >= n && c4 >= n && x2 - x1 == y2 - y1 ) { System . out . println ( p . get ( i ) . fi + " ▁ " + p . get ( i ) . se ) ; } } } public static void main ( String [ ] args ) { int N = 2 ; ArrayList < pair < Integer , Integer > > arr = new ArrayList < > ( ) ; arr . add ( new pair < Integer , Integer > ( 0 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 0 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 0 , 2 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 1 , 2 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 0 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 1 ) ) ; arr . add ( new pair < Integer , Integer > ( 2 , 2 ) ) ; findPoint ( N , arr ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int triacontagonalNum ( int n ) { return ( 28 * n * n - 26 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ triacontagonal ▁ Number ▁ is ▁ = ▁ " + triacontagonalNum ( n ) ) ; } }
class GFG { public static int hexacontagonNum ( int n ) { return ( 58 * n * n - 56 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ hexacontagon ▁ Number ▁ is ▁ = ▁ " + hexacontagonNum ( n ) ) ; } }
class GFG { public static int enneacontagonNum ( int n ) { return ( 88 * n * n - 86 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ enneacontagon ▁ Number ▁ is ▁ = ▁ " + enneacontagonNum ( n ) ) ; } }
class GFG { public static int triacontakaidigonNum ( int n ) { return ( 30 * n * n - 28 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ triacontakaidigon ▁ Number ▁ is ▁ = ▁ " + triacontakaidigonNum ( n ) ) ; } }
class GFG { public static int IcosihexagonalNum ( int n ) { return ( 24 * n * n - 22 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ Icosihexagonal ▁ Number ▁ is ▁ = ▁ " + IcosihexagonalNum ( n ) ) ; } }
class GFG { public static int icosikaioctagonalNum ( int n ) { return ( 26 * n * n - 24 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ icosikaioctagonal ▁ Number ▁ is ▁ = ▁ " + icosikaioctagonalNum ( n ) ) ; } }
import java . util . * ; class GFG { static int octacontagonNum ( int n ) { return ( 78 * n * n - 76 * n ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( "3rd ▁ octacontagon ▁ Number ▁ is ▁ = ▁ " + octacontagonNum ( n ) ) ; } }
import java . util . * ; class GFG { static int hectagonNum ( int n ) { return ( 98 * n * n - 96 * n ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . print ( "3rd ▁ hectagon ▁ Number ▁ is ▁ = ▁ " + hectagonNum ( n ) ) ; } }
import java . util . * ; class GFG { static int tetracontagonNum ( int n ) { return ( 38 * n * n - 36 * n ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( "3rd ▁ tetracontagon ▁ Number ▁ is ▁ = ▁ " + tetracontagonNum ( n ) ) ; } }
import java . lang . Math ; class GFG1 { static void Perimeter ( double a , double b ) { double Perimeter ; Perimeter = ( double ) 2 * 3.14 * Math . sqrt ( ( a * a + b * b ) / ( 2 * 1.0 ) ) ; System . out . println ( " Perimeter : ▁ " + Perimeter ) ; } public static void main ( String [ ] args ) { double a = 3 , b = 2 ; Perimeter ( a , b ) ; } }
import java . lang . Math ; class cfg { static double ReuleauxArea ( double a ) { if ( a < 0 ) return - 1 ; double A = 0.70477 * Math . pow ( a , 2 ) ; return A ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( ReuleauxArea ( a ) ) ; }
import java . io . * ; class GFG { static double hexagonside ( double a ) { if ( a < 0 ) return - 1 ; double x = ( 0.5176 * a ) ; return x ; } public static void main ( String [ ] args ) { double a = 6 ; System . out . println ( hexagonside ( a ) ) ; } }
class CLG { static float hexagonside ( float a ) { if ( a < 0 ) return - 1 ; float x = a / 3 ; return x ; } public static void main ( String [ ] args ) { float a = 6 ; System . out . println ( hexagonside ( a ) ) ; } }
import java . io . * ; class GFG { static float findVolume ( float a ) { if ( a < 0 ) return - 1 ; float r = a / 2 ; float h = a ; float V = ( float ) ( 3.14 * Math . pow ( r , 2 ) * h ) ; return V ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . print ( findVolume ( a ) ) ; } }
class GFG { static int findSegment ( int n , int m , int [ ] segment_length ) { double meet_point = ( 1.0 * n ) / 2.0 ; int sum = 0 ; int segment_number = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sum += segment_length [ i ] ; if ( ( double ) sum == meet_point ) { segment_number = - 1 ; break ; } if ( sum > meet_point ) { segment_number = i + 1 ; break ; } } return segment_number ; } public static void main ( String [ ] args ) { int n = 13 ; int m = 3 ; int [ ] segment_length = new int [ ] { 3 , 2 , 8 } ; int ans = findSegment ( n , m , segment_length ) ; System . out . println ( ans ) ; } }
public class GFG { static long countMaxIntersect ( long n ) { return ( n ) * ( n - 1 ) / 2 ; } public static void main ( String args [ ] ) { long n = 8 ; System . out . println ( countMaxIntersect ( n ) ) ; } }
class solution { static int checkpoint ( int h , int k , int x , int y , int a ) { int p = ( int ) Math . pow ( ( y - k ) , 2 ) - 4 * a * ( x - h ) ; return p ; } public static void main ( String arr [ ] ) { int h = 0 , k = 0 , x = 2 , y = 1 , a = 4 ; if ( checkpoint ( h , k , x , y , a ) > 0 ) System . out . println ( " Outside " ) ; else if ( checkpoint ( h , k , x , y , a ) == 0 ) System . out . println ( " On ▁ the ▁ parabola " ) ; else System . out . println ( " Inside " ) ; } }
public class GFG { public static float circlearea ( double a , double b ) { if ( a < 0 b < 0 ) return - 1 ; float A = ( float ) ( ( 3.14 * Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) / ( 4 * ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 8 , b = 10 ; System . out . println ( circlearea ( a , b ) ) ; } }
class GFG { static float circlearea ( float l , float b ) { if ( l < 0 b < 0 ) return - 1 ; if ( l < b ) return ( float ) ( 3.14 * Math . pow ( l / 2 , 2 ) ) ; else return ( float ) ( 3.14 * Math . pow ( b / 2 , 2 ) ) ; } public static void main ( String [ ] args ) { float l = 4 , b = 8 ; System . out . println ( circlearea ( l , b ) ) ; } }
import java . io . * ; class GFG { static int centered_cube ( int n ) { return ( 2 * n + 1 ) * ( n * n + n + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( n + " th ▁ Centered " + " ▁ cube ▁ number : ▁ " ) ; System . out . println ( centered_cube ( n ) ) ; n = 10 ; System . out . print ( n + " th ▁ Centered " + " ▁ cube ▁ number : ▁ " ) ; System . out . println ( centered_cube ( n ) ) ; } }
import java . io . * ; class GFG { public static void main ( String [ ] args ) { double side = 3 ; System . out . print ( " Volume ▁ of ▁ octahedron ▁ = ▁ " ) ; System . out . println ( vol_of_octahedron ( side ) ) ; } static double vol_of_octahedron ( double side ) { return ( ( side * side * side ) * ( Math . sqrt ( 2 ) / 3 ) ) ; } }
import java . util . * ; import java . lang . * ; class GfG { public static float volumeOfEllipsoid ( float r1 , float r2 , float r3 ) { float pi = ( float ) 3.14 ; return ( float ) 1.33 * pi * r1 * r2 * r3 ; } public static void main ( String args [ ] ) { float r1 = ( float ) 2.3 , r2 = ( float ) 3.4 , r3 = ( float ) 5.7 ; System . out . println ( " volume ▁ of ▁ ellipsoid ▁ is ▁ : ▁ " + volumeOfEllipsoid ( r1 , r2 , r3 ) ) ; } }
import java . util . * ; import java . lang . * ; class GfG { public static float volumeTriangular ( int a , int b , int h ) { float vol = ( float ) ( 0.1666 ) * a * b * h ; return vol ; } public static float volumeSquare ( int b , int h ) { float vol = ( float ) ( 0.33 ) * b * b * h ; return vol ; } public static float volumePentagonal ( int a , int b , int h ) { float vol = ( float ) ( 0.83 ) * a * b * h ; return vol ; } public static float volumeHexagonal ( int a , int b , int h ) { float vol = ( float ) a * b * h ; return vol ; } public static void main ( String argc [ ] ) { int b = 4 , h = 9 , a = 4 ; System . out . println ( " Volume ▁ of ▁ triangular " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeTriangular ( a , b , h ) ) ; System . out . println ( " Volume ▁ of ▁ square ▁ base " + " ▁ pyramid ▁ is ▁ " + volumeSquare ( b , h ) ) ; System . out . println ( " Volume ▁ of ▁ pentagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumePentagonal ( a , b , h ) ) ; System . out . println ( " Volume ▁ of ▁ Hexagonal " + " ▁ base ▁ pyramid ▁ is ▁ " + volumeHexagonal ( a , b , h ) ) ; } }
import java . io . * ; class GFG { static double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; } public static void main ( String arg [ ] ) { double side = 4 ; System . out . print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " + areaOctagon ( side ) ) ; } }
public class Diagonals { static int numberOfDiagonals ( int n ) { return n * ( n - 3 ) / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( n + " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ) ; System . out . println ( numberOfDiagonals ( n ) + " ▁ diagonals " ) ; } }
class Point { double x , y ; public Point ( double x , double y ) { this . x = x ; this . y = y ; } static void displayPoint ( Point p ) { System . out . println ( " ( " + p . x + " , ▁ " + p . y + " ) " ) ; } } class Test { static Point lineLineIntersection ( Point A , Point B , Point C , Point D ) { double a1 = B . y - A . y ; double b1 = A . x - B . x ; double c1 = a1 * ( A . x ) + b1 * ( A . y ) ; double a2 = D . y - C . y ; double b2 = C . x - D . x ; double c2 = a2 * ( C . x ) + b2 * ( C . y ) ; double determinant = a1 * b2 - a2 * b1 ; if ( determinant == 0 ) { return new Point ( Double . MAX_VALUE , Double . MAX_VALUE ) ; } else { double x = ( b2 * c1 - b1 * c2 ) / determinant ; double y = ( a1 * c2 - a2 * c1 ) / determinant ; return new Point ( x , y ) ; } } public static void main ( String args [ ] ) { Point A = new Point ( 1 , 1 ) ; Point B = new Point ( 4 , 4 ) ; Point C = new Point ( 1 , 8 ) ; Point D = new Point ( 2 , 4 ) ; Point intersection = lineLineIntersection ( A , B , C , D ) ; if ( intersection . x == Double . MAX_VALUE && intersection . y == Double . MAX_VALUE ) { System . out . println ( " The ▁ given ▁ lines ▁ AB ▁ and ▁ CD ▁ are ▁ parallel . " ) ; } else { System . out . print ( " The ▁ intersection ▁ of ▁ the ▁ given ▁ lines ▁ AB ▁ " + " and ▁ CD ▁ is : ▁ " ) ; Point . displayPoint ( intersection ) ; } } }
class GFG { static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return Math . ceil ( d / ( 2 * r ) ) ; } public static void main ( String arg [ ] ) { int r = 2 , x1 = 0 , y1 = 0 ; int x2 = 0 , y2 = 4 ; System . out . print ( ( int ) minRevolutions ( r , x1 , y1 , x2 , y2 ) ) ; } }
import java . util . * ; class GFG { public static void preprocess ( int p [ ] , int x [ ] , int y [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) p [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Arrays . sort ( p ) ; } public static int query ( int p [ ] , int n , int rad ) { int start = 0 , end = n - 1 ; while ( ( end - start ) > 1 ) { int mid = ( start + end ) / 2 ; double tp = Math . sqrt ( p [ mid ] ) ; if ( tp > ( rad * 1.0 ) ) end = mid - 1 ; else start = mid ; } double tp1 = Math . sqrt ( p [ start ] ) ; double tp2 = Math . sqrt ( p [ end ] ) ; if ( tp1 > ( rad * 1.0 ) ) return 0 ; else if ( tp2 <= ( rad * 1.0 ) ) return end + 1 ; else return start + 1 ; } public static void main ( String [ ] args ) { int x [ ] = { 1 , 2 , 3 , - 1 , 4 } ; int y [ ] = { 1 , 2 , 3 , - 1 , 4 } ; int n = x . length ; int p [ ] = new int [ n ] ; preprocess ( p , x , y , n ) ; System . out . println ( query ( p , n , 3 ) ) ; System . out . println ( query ( p , n , 32 ) ) ; } }
public class GFG { final static double eps = ( double ) 1e-6 ; static double getArea ( double base , double hypotenuse ) { double height = Math . sqrt ( hypotenuse * hypotenuse - base * base ) ; return 0.5 * base * height ; } static void printRightAngleTriangle ( int hypotenuse , int area ) { int hsquare = hypotenuse * hypotenuse ; double sideForMaxArea = Math . sqrt ( hsquare / 2.0 ) ; double maxArea = getArea ( sideForMaxArea , hypotenuse ) ; if ( area > maxArea ) { System . out . print ( " Not ▁ possible " ) ; return ; } double low = 0.0 ; double high = sideForMaxArea ; double base = 0 ; while ( Math . abs ( high - low ) > eps ) { base = ( low + high ) / 2.0 ; if ( getArea ( base , hypotenuse ) >= area ) { high = base ; } else { low = base ; } } double height = Math . sqrt ( hsquare - base * base ) ; System . out . println ( Math . round ( base ) + " ▁ " + Math . round ( height ) ) ; } static public void main ( String [ ] args ) { int hypotenuse = 5 ; int area = 6 ; printRightAngleTriangle ( hypotenuse , area ) ; } }
class GFG { static int countLattice ( int r ) { if ( r <= 0 ) return 0 ; int result = 4 ; for ( int x = 1 ; x < r ; x ++ ) { int ySquare = r * r - x * x ; int y = ( int ) Math . sqrt ( ySquare ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } public static void main ( String arg [ ] ) { int r = 5 ; System . out . println ( countLattice ( r ) ) ; } }
import java . util . * ; class GFG { public static void main ( String [ ] args ) { int [ ] arr = { 3 , 6 , 3 , 2 , 1 , 3 , 9 } ; int K = 2 , M = 4 ; System . out . println ( countSubArrays ( arr , K , M ) ) ; } public static int countSubArrays ( int [ ] arr , int K , int M ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < K ; i ++ ) { sum += arr [ i ] ; } if ( sum >= K * M ) count ++ ; for ( int i = K ; i < arr . length ; i ++ ) { sum += ( arr [ i ] - arr [ i - K ] ) ; if ( sum >= K * M ) count ++ ; } return count ; } }
import java . util . * ; class GFG { static int find_Numb_ways ( int n ) { int odd_indices = n / 2 ; int even_indices = ( n / 2 ) + ( n % 2 ) ; int arr_odd = ( int ) Math . pow ( 4 , odd_indices ) ; int arr_even = ( int ) Math . pow ( 5 , even_indices ) ; return arr_odd * arr_even ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( find_Numb_ways ( n ) ) ; } }
import java . util . * ; class GFG { static void performQueries ( int n , int q , int k , int [ ] arr , int [ ] [ ] query ) { for ( int i = 1 ; i <= q ; i ++ ) { int count = 0 ; if ( query [ i - 1 ] [ 0 ] == 1 ) { for ( int j = query [ i - 1 ] [ 1 ] ; j <= query [ i - 1 ] [ 2 ] ; j ++ ) { if ( arr [ j ] == k ) count ++ ; } System . out . print ( count + "NEW_LINE"); } else { arr [ query [ i - 1 ] [ 1 ] ] = query [ i - 1 ] [ 2 ] ; } } } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 5 , 7 , 6 , 9 , 0 , 0 , 0 , 0 , 5 , 6 , 7 , 3 , 9 , 0 , 7 , 0 , 9 , 0 } ; int Q = 5 ; int [ ] [ ] query = { { 1 , 5 , 14 } , { 2 , 6 , 1 } , { 1 , 0 , 8 } , { 2 , 13 , 0 } , { 1 , 6 , 18 } } ; int N = arr . length ; int K = 0 ; performQueries ( N , Q , K , arr , query ) ; } }
import java . io . * ; class GFG { public static int findSmallestNumK ( int n ) { int cummAnd = n ; int i = n - 1 ; while ( cummAnd != 0 ) { cummAnd = cummAnd & i ; if ( cummAnd == 0 ) { return i ; } i -- ; } return - 1 ; } public static void main ( String [ ] args ) { int N = 17 ; int lastNum = findSmallestNumK ( N ) ; int K = lastNum == - 1 ? lastNum : N - lastNum ; System . out . println ( K ) ; } }
import java . io . * ; class GFG { public static String NimGame ( int N , int A , int B ) { int sum = A + B ; if ( N % sum <= A - 1 ) return " Bob " ; else return " Alice " ; } public static void main ( String [ ] args ) { int N = 3 , A = 1 , B = 2 ; System . out . println ( NimGame ( N , A , B ) ) ; } }
class GFG { static void compute ( int [ ] query ) { int [ ] v = new int [ 100001 ] ; int [ ] dp = new int [ 100001 ] ; v [ 1 ] = 1 ; v [ 2 ] = 0 ; dp [ 1 ] = 0 ; dp [ 2 ] = 0 ; for ( int i = 3 ; i < 100001 ; i ++ ) { if ( i % 2 != 0 ) { if ( ( i / 2 ) % 2 == 0 ) { v [ i ] = 1 ; dp [ i ] = dp [ i - 1 ] ; } else { v [ i ] = v [ i / 2 ] + 1 ; dp [ i ] = dp [ i - 1 ] + v [ i ] - 1 ; } } else { v [ i ] = 0 ; dp [ i ] = dp [ i - 1 ] ; } } for ( int x : query ) System . out . print ( dp [ x ] + "NEW_LINE"); } public static void main ( String [ ] args ) { int [ ] query = { 5 , 2 } ; compute ( query ) ; } }
import java . util . * ; import java . math . * ; class GFG { static BigInteger findCycles ( int N ) { BigInteger res , finalResult ; long val = 2 * N - 1 ; String st = String . valueOf ( val ) ; BigInteger str = new BigInteger ( st ) ; String n1 = String . valueOf ( ( N - 1 ) ) ; String n2 = String . valueOf ( ( N - 2 ) ) ; BigInteger a = new BigInteger ( n1 ) ; BigInteger b = new BigInteger ( n2 ) ; res = a . multiply ( b ) ; finalResult = res . add ( str ) ; return finalResult ; } public static void main ( String args [ ] ) throws Exception { int N = 5 ; System . out . println ( findCycles ( N ) ) ; } }
import java . util . * ; class GFG { static int find ( int a [ ] , int n , int k ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < n ; ++ i ) { v . add ( a [ i - 1 ] - a [ i ] ) ; } Collections . sort ( v ) ; int res = a [ n - 1 ] - a [ 0 ] ; for ( int i = 0 ; i < k - 1 ; ++ i ) { res += v . get ( i ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 15 , 16 , 23 , 42 } ; int N = arr . length ; int K = 3 ; System . out . print ( find ( arr , N , K ) + "NEW_LINE"); } }
import java . util . * ; class TestClass { public static long power ( long a , long b , long c ) { long result = 1 ; while ( b > 0 ) { if ( ( b & 1 ) = = 1 ) { result = ( result * a ) % c ; } b / = 2 ; a = ( a * a ) % c ; } return result ; } public static long calculate ( long X , long N ) { int a [ ] = new int [ 10 ] ; int cyclicity [ ] = new int [ 11 ] ; cyclicity [ 1 ] = 1 ; cyclicity [ 2 ] = 4 ; cyclicity [ 3 ] = 4 ; cyclicity [ 4 ] = 2 ; cyclicity [ 5 ] = 1 ; cyclicity [ 6 ] = 1 ; cyclicity [ 7 ] = 4 ; cyclicity [ 8 ] = 4 ; cyclicity [ 9 ] = 2 ; cyclicity [ 10 ] = 1 ; if ( N = = 0 N = = 1 ) { return ( X % 10 ) ; } else if ( N = = 2 N = = 3 N = = 4 ) { long temp = ( long ) 1e18 ; a [ 2 ] = 2 ; a [ 3 ] = 6 ; a [ 4 ] = 4 ; long v = X % 10 ; if ( v ! = 0 ) { int u = cyclicity [ ( int ) v ] ; int r = a [ ( int ) N ] % u ; if ( r = = 0 ) { if ( v = = 2 v = = 4 v = = 6 v = = 8 ) { return 6 ; } else if ( v = = 5 ) { return 5 ; } else if ( v = = 1 v = = 3 v = = 7 v = = 9 ) { return 1 ; } } else { return ( power ( v , r , temp ) % 10 ) ; } } else { return 0 ; } } return 1 ; } public static void main ( String args [ ] ) throws Exception { int X = 18 ; int N = 4 ; long result = calculate ( X , N ) ; System . out . println ( result ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void LIS ( int arr [ ] , int n ) { TreeMap < Integer , List < Integer > > m = new TreeMap < Integer , List < Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { List < Integer > indexes ; if ( m . containsKey ( arr [ i ] ) ) { indexes = m . get ( arr [ i ] ) ; } else { indexes = new ArrayList < Integer > ( ) ; } indexes . add ( i ) ; m . put ( arr [ i ] , indexes ) ; } int k = n ; int ans = 0 ; for ( Map . Entry < Integer , List < Integer > > it : m . entrySet ( ) ) { List < Integer > indexes = it . getValue ( ) ; if ( indexes . get ( indexes . size ( ) - 1 ) < k ) { k = indexes . get ( 0 ) ; ans ++ ; } else k = lower_bound ( indexes , k ) ; } System . out . println ( ans ) ; } static int lower_bound ( List < Integer > indexes , int k ) { int low = 0 , high = indexes . size ( ) - 1 ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( indexes . get ( mid ) < k ) low = mid + 1 ; else high = mid ; } return indexes . get ( low ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 1 , 2 } ; int n = arr . length ; LIS ( arr , n ) ; } }
import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void IsPossible ( int a , int b , int x , int y ) { int g = gcd ( x , y ) ; int initial = gcd ( a , b ) ; if ( initial == g ) { System . out . print ( "POSSIBLENEW_LINE"); } else { System . out . print ( "NOT POSSIBLENEW_LINE"); } } public static void main ( String args [ ] ) { int A = 1 , B = 2 , X = 4 , Y = 11 ; IsPossible ( A , B , X , Y ) ; A = 2 ; B = 2 ; X = 3 ; Y = 6 ; IsPossible ( A , B , X , Y ) ; } }
import java . util . * ; class GFG { static int mod = 1000000007 ; static int waysToColor ( int arr [ ] , int n , int k ) { int powOf2 [ ] = new int [ 500 ] ; int [ ] [ ] c = new int [ 500 ] [ 500 ] ; for ( int i = 0 ; i <= n ; i ++ ) { c [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j <= i ; j ++ ) { c [ i ] [ j ] = ( c [ i - 1 ] [ j ] + c [ i - 1 ] [ j - 1 ] ) % mod ; } } powOf2 [ 0 ] = powOf2 [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { powOf2 [ i ] = powOf2 [ i - 1 ] * 2 % mod ; } int rem = n - k ; arr [ k ++ ] = n + 1 ; Arrays . sort ( arr ) ; int answer = 1 ; for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ i ] - ( i - 1 >= 0 ? arr [ i - 1 ] : 0 ) - 1 ; answer *= c [ rem ] [ x ] % mod * ( i != 0 && i != k - 1 ? powOf2 [ x ] : 1 ) % mod ; rem -= x ; } return answer ; } public static void main ( String [ ] args ) { int N = 6 ; int K = 3 ; int arr [ ] = { 1 , 2 , 6 , 0 } ; System . out . print ( waysToColor ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static void printArray ( int [ ] a , int n ) { Arrays . sort ( a ) ; int i = 0 ; int j = n - 1 ; while ( i <= j ) { if ( i == j ) { System . out . print ( a [ i ] + " ▁ " ) ; } else { System . out . print ( a [ j ] + " ▁ " ) ; System . out . print ( a [ i ] + " ▁ " ) ; } i = i + 1 ; j = j - 1 ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n1 = arr1 . length ; printArray ( arr1 , n1 ) ; } }
class GFG { static int SmallestDiv ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return i ; } } return n ; } public static void main ( String [ ] args ) { int X = 385 ; int ans = SmallestDiv ( X ) ; System . out . print ( ans + "NEW_LINE"); } }
import java . util . * ; class GFG { static void maximumArea ( int l , int b , int x , int y ) { int left , right , above , below ; left = x * b ; right = ( l - x - 1 ) * b ; above = l * y ; below = ( b - y - 1 ) * l ; System . out . print ( Math . max ( Math . max ( left , right ) , Math . max ( above , below ) ) ) ; } public static void main ( String [ ] args ) { int L = 8 , B = 8 ; int X = 0 , Y = 0 ; maximumArea ( L , B , X , Y ) ; } }
class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int n , int k , int s ) { int vis [ ] = new int [ n ] ; int cnt = 0 ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n && cnt < k ; i += 2 ) { arr [ i ] = s ; vis [ i ] = 1 ; cnt ++ ; } int val = s ; if ( s % 2 == 0 ) val ++ ; else val = val + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] == 0 ) { arr [ i ] = val ; } } printArr ( arr , n ) ; } public static void main ( String [ ] args ) { int n = 8 , k = 3 , s = 12 ; findArray ( n , k , s ) ; } }
import java . util . * ; class GFG { static int N = 100000 ; static int prime [ ] = new int [ N ] ; static int divi [ ] = new int [ N ] ; static int sum [ ] = new int [ N ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < N ; i += p ) prime [ i ] = 0 ; } } } static void DivisorCount ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { divi [ j ] ++ ; } } } static void pre ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( prime [ divi [ i ] ] == 1 ) { sum [ i ] = i ; } } for ( int i = 1 ; i < N ; i ++ ) sum [ i ] += sum [ i - 1 ] ; } public static void main ( String args [ ] ) { int l = 5 , r = 8 ; SieveOfEratosthenes ( ) ; DivisorCount ( ) ; pre ( ) ; System . out . println ( sum [ r ] - sum [ l - 1 ] ) ; } }
import java . util . * ; class GFG { static final int M = 1000000007 ; static long power ( long x , int y , int p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long n , int p ) { return power ( n , p - 2 , p ) ; } static long nCrModPFermat ( long n , int r , int p ) { if ( r == 0 ) return 1 ; long fac [ ] = new long [ ( int ) n + 1 ] ; fac [ 0 ] = 1 ; int i ; for ( i = 1 ; i <= n ; i ++ ) fac [ i ] = fac [ i - 1 ] * i % p ; return ( fac [ ( int ) n ] * modInverse ( fac [ r ] , p ) % p * modInverse ( fac [ ( int ) n - r ] , p ) % p ) % p ; } static long countOdd ( long n ) { long x = n / 2 ; if ( n % 2 == 1 ) x ++ ; return x ; } static long counteEven ( long n ) { long x = n / 2 ; return x ; } static long CountEvenSumSequences ( long n ) { long count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) { int even = i , odd = ( int ) n - i ; if ( odd % 2 == 1 ) continue ; long tot = ( power ( countOdd ( n ) , odd , M ) * nCrModPFermat ( n , odd , M ) ) % M ; tot = ( tot * power ( counteEven ( n ) , i , M ) ) % M ; count += tot ; count %= M ; } return count ; } public static void main ( String [ ] args ) { long n = 5 ; System . out . println ( CountEvenSumSequences ( n ) ) ; } }
import java . io . * ; class GFG { static void checkEvenOdd ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { System . out . print ( " Even " ) ; return ; } } System . out . println ( " Odd " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 12 , 20 , 36 , 38 } ; int n = arr . length ; checkEvenOdd ( arr , n ) ; } }
class GFG { static int MAXN = 30 ; static int countMaxLength ( int N ) { int res ; int pre [ ] = new int [ MAXN ] ; int p = 1 ; pre [ 0 ] = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { p *= 2 ; pre [ i ] = pre [ i - 1 ] + i * p ; } int ind = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( pre [ i ] >= N ) { ind = i ; break ; } } int x = N - pre [ ind - 1 ] ; int y = 2 * ind - 1 ; if ( x >= y ) res = Math . min ( x , y ) ; else res = Math . max ( x , 2 * ( ind - 2 ) + 1 ) ; return res ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . println ( countMaxLength ( N ) ) ; } }
class GFG { static int factorialUsingRecursion ( int n ) { if ( n == 0 ) return 1 ; return n * factorialUsingRecursion ( n - 1 ) ; } static int factorialUsingIteration ( int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; } public static void main ( String [ ] args ) { int num = 5 ; System . out . println ( " Factorial ▁ of ▁ " + num + " ▁ using ▁ Recursion ▁ is : ▁ " + factorialUsingRecursion ( 5 ) ) ; System . out . println ( " Factorial ▁ of ▁ " + num + " ▁ using ▁ Iteration ▁ is : ▁ " + factorialUsingIteration ( 5 ) ) ; } }
import java . util . * ; class GFG { public static int __ ( int x , int y ) { return x ; } public static void revereseArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n / 2 ; i ++ ) arr [ i ] = __ ( arr [ n - i - 1 ] , arr [ n - i - 1 ] = arr [ i ] ) ; } public static void printArray ( int [ ] arr , int size ) { for ( int i = 0 ; i < size ; i ++ ) System . out . print ( Integer . toString ( arr [ i ] ) + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printArray ( arr , n ) ; revereseArray ( arr , n ) ; System . out . println ( " Reversed ▁ array ▁ is " ) ; printArray ( arr , n ) ; } }
public class GFG { static int numberofSubsequences ( int a [ ] , int L , int R , int X , int n ) { int ans = 0 ; for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int cnt = 0 , sum = 0 ; int minVal = Integer . MAX_VALUE , maxVal = Integer . MIN_VALUE ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) == 0 ) { cnt += 1 ; sum += a [ j ] ; maxVal = Math . max ( maxVal , a [ j ] ) ; minVal = Math . min ( minVal , a [ j ] ) ; } } if ( cnt >= 2 && sum >= L && sum <= R && ( maxVal - minVal >= X ) ) { ans += 1 ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 20 , 30 , 25 } ; int L = 40 , R = 50 , X = 10 ; int N = a . length ; System . out . println ( numberofSubsequences ( a , L , R , X , N ) ) ; } }
import java . io . * ; class GFG { static int Minimum ( int N , int K ) { int sum = N * ( N + 1 ) / 2 ; if ( K > sum ) return - 1 ; if ( K <= N ) return 1 ; sum = 0 ; int count = 0 ; while ( N >= 1 && sum < K ) { count += 1 ; sum += N ; N -= 1 ; } return count ; } public static void main ( String [ ] args ) { int N = 5 , K = 10 ; System . out . println ( Minimum ( N , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int largestNum ( int [ ] arr ) { Set < Integer > set = new HashSet < > ( ) ; int res = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { set . add ( arr [ i ] ) ; if ( set . contains ( - 1 * arr [ i ] ) ) { res = Math . max ( res , Math . abs ( arr [ i ] ) ) ; } } return res ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , - 2 , 5 , - 3 } ; System . out . println ( largestNum ( arr ) ) ; } }
public class GFG { static void findElement ( int [ ] A , int N , int X ) { int i = 0 ; int Comparisons = 0 ; int T = 1 ; Comparisons ++ ; if ( N % 2 == 1 ) { i = 1 ; T *= ( A [ 0 ] - X ) ; } for ( ; i < N ; i += 2 ) { Comparisons += 1 ; T *= ( A [ i ] - X ) ; T *= ( A [ i + 1 ] - X ) ; } Comparisons += 1 ; if ( T == 0 ) { System . out . println ( " Yes ▁ " + Comparisons ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] A = { - 3 , 5 , 11 , 3 , 100 , 2 , 88 , 22 , 7 , 900 , 23 , 4 , 1 } ; int N = A . length ; int X = 1 ; findElement ( A , N , X ) ; } }
import java . util . * ; public class GFG { public static int find ( Vector < Integer > arr , int N , int key ) { int l = 0 ; int h = N - 1 ; while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr . get ( mid ) == key ) return mid ; if ( arr . get ( l ) >= arr . get ( mid ) ) { if ( arr . get ( l ) >= key && key >= arr . get ( mid ) ) h = mid - 1 ; else l = mid + 1 ; } else { if ( arr . get ( mid ) >= key && key >= arr . get ( h ) ) l = mid + 1 ; else h = mid - 1 ; } } return - 1 ; } public static void main ( String args [ ] ) { Vector < Integer > arr = new Vector < Integer > ( ) ; arr . add ( 10 ) ; arr . add ( 8 ) ; arr . add ( 6 ) ; arr . add ( 5 ) ; arr . add ( 2 ) ; arr . add ( 1 ) ; arr . add ( 13 ) ; arr . add ( 12 ) ; int N = arr . size ( ) ; int key = 8 ; int ans = find ( arr , N , key ) ; System . out . println ( ans ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int getCountPairs ( int arr [ ] , int N , int S ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 0 ) ; mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } int twice_count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . get ( S - arr [ i ] ) != null ) { twice_count += mp . get ( S - arr [ i ] ) ; } if ( S - arr [ i ] == arr [ i ] ) twice_count -- ; } return twice_count / 2 ; } public static int countPairs ( int arr [ ] , int N ) { int count = 0 ; HashSet < Integer > S = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) S . add ( arr [ i ] ) ; for ( int ele : S ) { int sum = 2 * ele ; count += getCountPairs ( arr , N , sum ) ; } return count ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = { 4 , 2 , 5 , 1 , 3 , 5 } ; System . out . println ( countPairs ( arr , N ) ) ; } }
class GFG { static void maximumValue ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { ans = Math . max ( ans , arr [ i ] + arr [ j ] + i - j ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . length ; maximumValue ( arr , N ) ; } }
class GFG { static void maximumValue ( int arr [ ] , int n ) { int maxvalue = arr [ 0 ] ; int result = 0 ; for ( int i = 1 ; i < n ; i ++ ) { result = Math . max ( result , maxvalue + arr [ i ] - i ) ; maxvalue = Math . max ( maxvalue , arr [ i ] + i ) ; } System . out . println ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 9 , 3 , 6 , 5 } ; int N = arr . length ; maximumValue ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { public static int lower_bound ( int arr [ ] , int key ) { int low = 0 ; int high = arr . length - 1 ; while ( low < high ) { int mid = low + ( high - low ) / 2 ; if ( arr [ mid ] >= key ) { high = mid ; } else { low = mid + 1 ; } } return low ; } static int closestValue ( int A [ ] , int k ) { int close = A [ A . length - 1 ] ; int it = lower_bound ( A , k ) ; if ( it != A . length ) { close = A [ it ] ; if ( it != 0 ) { if ( ( k - A [ it - 1 ] ) < ( close - k ) ) { close = A [ it - 1 ] ; } } } return close ; } static void minPossible ( int arr [ ] , int brr [ ] , int crr [ ] ) { Arrays . sort ( arr ) ; Arrays . sort ( crr ) ; int minimum = Integer . MAX_VALUE ; for ( int val : brr ) { int arr_close = closestValue ( arr , val ) ; int crr_close = closestValue ( crr , val ) ; if ( Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) < minimum ) minimum = Math . abs ( val - arr_close ) + Math . abs ( val - crr_close ) ; } System . out . println ( minimum ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 8 , 5 } ; int b [ ] = { 2 , 9 } ; int c [ ] = { 5 , 4 } ; minPossible ( a , b , c ) ; } }
import java . util . * ; public class GFG { static class pair { int first , second , third ; pair ( int first , int second , int third ) { this . first = first ; this . second = second ; this . third = third ; } } static int direction [ ] [ ] = { { 1 , 0 } , { 0 , - 1 } , { - 1 , 0 } , { 0 , 1 } } ; static int maximumTime ( int arr [ ] [ ] ) { int n = arr . length ; int m = arr [ 0 ] . length ; boolean visited [ ] [ ] = new boolean [ n ] [ m ] ; Queue < pair > q = new LinkedList < > ( ) ; int uninfected_count = 0 ; int time = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( arr [ i ] [ j ] == 2 ) { q . add ( new pair ( i , j , 0 ) ) ; visited [ i ] [ j ] = true ; } if ( arr [ i ] [ j ] == 1 ) { uninfected_count ++ ; } } } while ( ! q . isEmpty ( ) ) { pair current = q . peek ( ) ; time = current . third ; q . poll ( ) ; for ( int [ ] it : direction ) { int i = current . first + it [ 0 ] ; int j = current . second + it [ 1 ] ; if ( i < 0 j < 0 i >= n j >= m arr [ i ] [ j ] != 1 visited [ i ] [ j ] ) { continue ; } q . add ( new pair ( i , j , time + 1 ) ) ; visited [ i ] [ j ] = true ; uninfected_count -- ; } } if ( uninfected_count != 0 ) return - 1 ; return time ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 2 , 1 , 0 , 2 , 1 } , { 1 , 0 , 1 , 2 , 1 } , { 1 , 0 , 0 , 2 , 1 } } ; System . out . println ( maximumTime ( arr ) ) ; } }
import java . util . Arrays ; import java . util . Collections ; class GFG { static int minimum_swaps ( Integer arr [ ] , int n ) { int count = 0 ; int max_el = Collections . max ( Arrays . asList ( arr ) ) ; int min_el = Collections . min ( Arrays . asList ( arr ) ) ; if ( min_el == max_el ) return 0 ; int index_max = - 1 ; int index_min = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == max_el && index_max == - 1 ) { index_max = i ; } if ( arr [ i ] == min_el ) { index_min = i ; } } count += index_max ; count += ( n - 1 - index_min ) ; if ( index_min < index_max ) count -= 1 ; return count ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 2 , 4 , 1 , 6 , 5 } ; int N = arr . length ; System . out . println ( minimum_swaps ( arr , N ) ) ; } }
import java . util . * ; class GFG { static int ans ; static void DFS ( int src , ArrayList < ArrayList < Integer > > Adj , int arr [ ] , int currentMin , int currentMax ) { ans = Math . max ( ans , Math . max ( Math . abs ( currentMax - arr [ src - 1 ] ) , Math . abs ( currentMin - arr [ src - 1 ] ) ) ) ; currentMin = Math . min ( currentMin , arr [ src - 1 ] ) ; currentMax = Math . min ( currentMax , arr [ src - 1 ] ) ; for ( Integer child : Adj . get ( src ) ) { DFS ( child , Adj , arr , currentMin , currentMax ) ; } } static void getMaximumDifference ( int Edges [ ] [ ] , int arr [ ] , int N , int M ) { ans = 0 ; ArrayList < ArrayList < Integer > > Adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . add ( new ArrayList < > ( ) ) ; for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; Adj . get ( u ) . add ( v ) ; } DFS ( 1 , Adj , arr , arr [ 0 ] , arr [ 0 ] ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 5 , M = 4 ; int Edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 4 , 5 } , { 1 , 3 } } ; int arr [ ] = { 13 , 8 , 3 , 15 , 18 } ; getMaximumDifference ( Edges , arr , N , M ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void isComment ( String S ) { char line [ ] = S . toCharArray ( ) ; if ( line [ 0 ] == ' / ' && line [ 1 ] == ' / ' ) { System . out . println ( " It ▁ is ▁ a ▁ single - line ▁ comment " ) ; return ; } if ( line [ 0 ] == ' / ' && line [ 1 ] == ' * ' && line [ line . length - 2 ] == ' * ' && line [ line . length - 1 ] == ' / ' ) { System . out . println ( " It ▁ is ▁ a ▁ multi - line ▁ comment " ) ; return ; } System . out . println ( " It ▁ is ▁ not ▁ a ▁ comment " ) ; } public static void main ( String [ ] args ) { String line = " GeeksForGeeks ▁ GeeksForGeeks " ; isComment ( line ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void longestSequence ( int arr [ ] , int N ) { if ( N == 0 ) { System . out . println ( 0 ) ; return ; } Arrays . sort ( arr ) ; int maxLen = 1 ; int len = 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] arr [ i ] == arr [ i - 1 ] + 1 ) { len ++ ; maxLen = Math . max ( maxLen , len ) ; } else { len = 1 ; } } System . out . println ( maxLen ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 5 , 4 , 8 , 4 } ; int N = arr . length ; longestSequence ( arr , N ) ; } }
import java . util . * ; class GFG { static void minSum ( int arr [ ] , int N , int Q [ ] , int M ) { for ( int i = 0 ; i < M ; i ++ ) { int val = Q [ i ] ; int front = 0 , rear = 0 ; for ( int j = 0 ; j < N ; j ++ ) { front += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } for ( int j = N - 1 ; j >= 0 ; j -- ) { rear += arr [ j ] ; if ( arr [ j ] == val ) { break ; } } System . out . print ( Math . min ( front , rear ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 6 , 7 , 4 , 5 , 1 } ; int N = arr . length ; int Q [ ] = { 7 , 6 } ; int M = Q . length ; minSum ( arr , N , Q , M ) ; } }
import java . util . * ; class GFG { static void findMinLength ( int arr [ ] , int N ) { int front = 0 , back = N - 1 ; while ( front < back ) { int x = arr [ front ] ; if ( arr [ front ] != arr [ back ] ) break ; while ( arr [ front ] == x && front <= back ) front ++ ; while ( arr [ back ] == x && front <= back ) back -- ; } System . out . println ( back - front + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 1 , 2 , 2 , 1 } ; int N = arr . length ; findMinLength ( arr , N ) ; } }
public class GFG { static void prefixArr ( int arr [ ] , int prefix [ ] [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i == 0 ) { prefix [ i ] [ 0 ] = arr [ i ] ; prefix [ i ] [ 1 ] = arr [ i ] ; } else { prefix [ i ] [ 0 ] = Math . max ( prefix [ i - 1 ] [ 0 ] , arr [ i ] ) ; prefix [ i ] [ 1 ] = Math . min ( prefix [ i - 1 ] [ 1 ] , arr [ i ] ) ; } } } static void suffixArr ( int arr [ ] , int suffix [ ] [ ] , int N ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { if ( i == N - 1 ) { suffix [ i ] [ 0 ] = arr [ i ] ; suffix [ i ] [ 1 ] = arr [ i ] ; } else { suffix [ i ] [ 0 ] = Math . max ( suffix [ i + 1 ] [ 0 ] , arr [ i ] ) ; suffix [ i ] [ 1 ] = Math . min ( suffix [ i + 1 ] [ 1 ] , arr [ i ] ) ; } } } static void maxAndmin ( int prefix [ ] [ ] , int suffix [ ] [ ] , int N , int L , int R ) { int maximum , minimum ; if ( L == 0 && R == N - 1 ) { System . out . println ( " No ▁ maximum ▁ and ▁ minimum ▁ value " ) ; return ; } else if ( L == 0 ) { maximum = suffix [ R + 1 ] [ 0 ] ; minimum = suffix [ R + 1 ] [ 1 ] ; } else if ( R == N - 1 ) { maximum = prefix [ L - 1 ] [ 0 ] ; minimum = prefix [ R - 1 ] [ 1 ] ; } else { maximum = Math . max ( prefix [ L - 1 ] [ 0 ] , suffix [ R + 1 ] [ 0 ] ) ; minimum = Math . min ( prefix [ L - 1 ] [ 1 ] , suffix [ R + 1 ] [ 1 ] ) ; } System . out . println ( maximum + " ▁ " + minimum ) ; } static void MinMaxQueries ( int a [ ] , int Q [ ] [ ] ) { int N = a . length ; int q = Q . length ; int prefix [ ] [ ] = new int [ N ] [ 2 ] ; int suffix [ ] [ ] = new int [ N ] [ 2 ] ; prefixArr ( a , prefix , N ) ; suffixArr ( a , suffix , N ) ; for ( int i = 0 ; i < q ; i ++ ) { int L = Q [ i ] [ 0 ] ; int R = Q [ i ] [ 1 ] ; maxAndmin ( prefix , suffix , N , L , R ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 8 , 3 , 5 , 7 , 4 } ; int queries [ ] [ ] = { { 4 , 6 } , { 0 , 4 } , { 3 , 7 } , { 2 , 5 } } ; MinMaxQueries ( arr , queries ) ; } }
import java . io . * ; class GFG { static String finddigits ( String s ) { String [ ] num = { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " } ; int [ ] arr = new int [ 10 ] ; String ans = " " ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' z ' ) arr [ 0 ] ++ ; if ( s . charAt ( i ) == ' w ' ) arr [ 2 ] ++ ; if ( s . charAt ( i ) == ' g ' ) arr [ 8 ] ++ ; if ( s . charAt ( i ) == ' x ' ) arr [ 6 ] ++ ; if ( s . charAt ( i ) == ' v ' ) arr [ 5 ] ++ ; if ( s . charAt ( i ) == ' o ' ) arr [ 1 ] ++ ; if ( s . charAt ( i ) == ' s ' ) arr [ 7 ] ++ ; if ( s . charAt ( i ) == ' f ' ) arr [ 4 ] ++ ; if ( s . charAt ( i ) == ' h ' ) arr [ 3 ] ++ ; if ( s . charAt ( i ) == ' i ' ) arr [ 9 ] ++ ; } arr [ 7 ] -= arr [ 6 ] ; arr [ 5 ] -= arr [ 7 ] ; arr [ 4 ] -= arr [ 5 ] ; arr [ 1 ] -= ( arr [ 2 ] + arr [ 4 ] + arr [ 0 ] ) ; arr [ 3 ] -= arr [ 8 ] ; arr [ 9 ] -= ( arr [ 5 ] + arr [ 6 ] + arr [ 8 ] ) ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) { ans += ( char ) ( i + '0' ) ; } } return ans ; } public static void main ( String [ ] args ) { String s = " owoftnuoer " ; System . out . println ( finddigits ( s ) ) ; } }
import java . io . * ; class GFG { static void isBinaryStringsEqual ( String str1 , String str2 ) { int str1Zeros = 0 , str1Ones = 0 ; int str2Zeros = 0 , str2Ones = 0 ; int flag = 0 ; int curStr1Ones = 0 ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 . charAt ( i ) == '1' ) { str1Ones ++ ; } else if ( str1 . charAt ( i ) == '0' ) { str1Zeros ++ ; } if ( str2 . charAt ( i ) == '1' ) { str2Ones ++ ; } else if ( str2 . charAt ( i ) == '0' ) { str2Zeros ++ ; } } if ( str1Zeros != str2Zeros && str1Ones != str2Ones ) { System . out . println ( " Not ▁ Possible " ) ; } else { for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { if ( str1 . charAt ( i ) == '0' && curStr1Ones > 0 ) { str1 = str1 . substring ( 0 , i ) + '1' + str1 . substring ( i + 1 ) ; curStr1Ones -- ; } if ( str1 . charAt ( i ) == '0' && curStr1Ones == 0 ) { flag ++ ; break ; } if ( str1 . charAt ( i ) == '1' && str2 . charAt ( i ) == '0' ) { str1 = str1 . substring ( 0 , i ) + '0' + str1 . substring ( i + 1 ) ; curStr1Ones ++ ; } } } if ( flag == 0 ) { System . out . println ( " Possible " ) ; } else { System . out . println ( " Not ▁ Possible " ) ; } } } public static void main ( String [ ] args ) { String str1 = "0110" ; String str2 = "0011" ; isBinaryStringsEqual ( str1 , str2 ) ; } }
import java . util . * ; class GFG { static int maxN = 100 ; static int Tree [ ] = new int [ 4 * maxN ] ; static void build ( int arr [ ] , int index , int s , int e ) { if ( s == e ) Tree [ index ] = arr [ s ] ; else { int m = ( s + e ) / 2 ; build ( arr , 2 * index , s , m ) ; build ( arr , 2 * index + 1 , m + 1 , e ) ; Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static int atleast_x ( int index , int s , int e , int ql , int qr , int x ) { if ( ql > e qr < s ) return - 1 ; if ( s <= ql && e <= qr ) { if ( Tree [ index ] < x ) return - 1 ; while ( s != e ) { int m = ( s + e ) / 2 ; if ( Tree [ 2 * index ] >= x ) { e = m ; index = 2 * index ; } else { s = m + 1 ; index = 2 * index + 1 ; } } return s ; } int m = ( s + e ) / 2 ; int val = atleast_x ( 2 * index , s , m , ql , qr , x ) ; if ( val != - 1 ) return val ; return atleast_x ( 2 * index + 1 , m + 1 , e , ql , qr , x ) ; } static void update ( int index , int s , int e , int new_val , int pos ) { if ( s == e ) Tree [ index ] = new_val ; else { int m = ( s + e ) / 2 ; if ( pos <= m ) { update ( 2 * index , s , m , new_val , pos ) ; } else { update ( 2 * index + 1 , m + 1 , e , new_val , pos ) ; } Tree [ index ] = Math . max ( Tree [ 2 * index ] , Tree [ 2 * index + 1 ] ) ; } } static void printAnswer ( int [ ] arr , int n ) { build ( arr , 1 , 0 , n - 1 ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 2 ) ) ; arr [ 2 ] = 5 ; update ( 1 , 0 , n - 1 , 5 , 2 ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 4 ) ) ; System . out . println ( atleast_x ( 1 , 0 , n - 1 , 0 , n - 1 , 0 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 4 , 6 } ; int N = arr . length ; printAnswer ( arr , N ) ; } }
import java . util . * ; public class GFG { static int delCost ( String s , int [ ] cost ) { int ans = 0 ; HashMap < Character , Integer > forMax = new HashMap < > ( ) ; HashMap < Character , Integer > forTot = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! forMax . containsKey ( s . charAt ( i ) ) ) { forMax . put ( s . charAt ( i ) , cost [ i ] ) ; } else { forMax . put ( s . charAt ( i ) , Math . max ( cost [ i ] , forMax . get ( s . charAt ( i ) ) ) ) ; } if ( ! forTot . containsKey ( s . charAt ( i ) ) ) { forTot . put ( s . charAt ( i ) , cost [ i ] ) ; } else { forTot . put ( s . charAt ( i ) , forTot . get ( s . charAt ( i ) ) + cost [ i ] ) ; } } for ( Map . Entry < Character , Integer > i : forMax . entrySet ( ) ) { ans += forTot . get ( i . getKey ( ) ) - i . getValue ( ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " AAABBB " ; int [ ] cost = { 1 , 2 , 3 , 4 , 5 , 6 } ; System . out . println ( delCost ( s , cost ) ) ; } }
import java . util . * ; class GFG { static class TreeNode { int val ; TreeNode left ; TreeNode right ; TreeNode ( int data ) { this . val = data ; left = null ; right = null ; } } ; static int find_ht ( TreeNode root ) { if ( root == null ) return 0 ; if ( root . left == null && root . right == null ) return 1 ; return Math . max ( find_ht ( root . left ) , find_ht ( root . right ) ) + 1 ; } static TreeNode find_node ( TreeNode root , TreeNode req_node ) { if ( root == null ) return req_node ; int left_ht = find_ht ( root . left ) ; int right_ht = find_ht ( root . right ) ; if ( left_ht > right_ht ) { req_node = find_node ( root . left , req_node ) ; } else if ( right_ht > left_ht ) { req_node = find_node ( root . right , req_node ) ; } else { req_node = root ; return req_node ; } return req_node ; } public static void main ( String [ ] args ) { TreeNode root = new TreeNode ( 1 ) ; root . left = new TreeNode ( 2 ) ; root . right = new TreeNode ( 3 ) ; TreeNode req_node = null ; req_node = find_node ( root , req_node ) ; System . out . print ( req_node . val ) ; } }
import java . util . * ; class GFG { static boolean isSpiralSorted ( int [ ] arr , int n ) { int start = 0 ; int end = n - 1 ; while ( start < end ) { if ( arr [ start ] > arr [ end ] ) { return false ; } start ++ ; if ( arr [ end ] > arr [ start ] ) { return false ; } end -- ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 10 , 14 , 20 , 18 , 12 , 5 } ; int N = arr . length ; if ( isSpiralSorted ( arr , N ) != false ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
import java . io . Console ; import java . util . HashMap ; import java . util . Map ; class GFG { static int maxMatch ( int [ ] A , int [ ] B ) { HashMap < Integer , Integer > Aindex = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > diff = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < A . length ; i ++ ) { Aindex . put ( A [ i ] , i ) ; } for ( int i = 0 ; i < B . length ; i ++ ) { if ( i - Aindex . get ( B [ i ] ) < 0 ) { if ( ! diff . containsKey ( A . length + i - Aindex . get ( B [ i ] ) ) ) { diff . put ( A . length + i - Aindex . get ( B [ i ] ) , 1 ) ; } else { diff . put ( A . length + i - Aindex . get ( B [ i ] ) , diff . get ( A . length + i - Aindex . get ( B [ i ] ) ) + 1 ) ; } } else { if ( ! diff . containsKey ( i - Aindex . get ( B [ i ] ) ) ) { diff . put ( i - Aindex . get ( B [ i ] ) , 1 ) ; } else { diff . put ( i - Aindex . get ( B [ i ] ) , diff . get ( i - Aindex . get ( B [ i ] ) ) + 1 ) ; } } } int max = 0 ; for ( Map . Entry < Integer , Integer > ele : diff . entrySet ( ) ) { if ( ele . getValue ( ) > max ) { max = ele . getValue ( ) ; } } return max ; } public static void main ( String [ ] args ) { int [ ] A = { 5 , 3 , 7 , 9 , 8 } ; int [ ] B = { 8 , 7 , 3 , 5 , 9 } ; System . out . println ( maxMatch ( A , B ) ) ; } }
import java . util . * ; class GFG { static int MaxNonEmpSubSeq ( int a [ ] , int n ) { int sum = 0 ; int max = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( max < a [ i ] ) { max = a [ i ] ; } } if ( max <= 0 ) { return max ; } for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > 0 ) { sum += a [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , 11 , - 4 , 2 , - 3 , - 10 } ; int N = arr . length ; System . out . println ( MaxNonEmpSubSeq ( arr , N ) ) ; } }
class GFG { static long getcount ( long n , int k ) { long res = ( n >> ( k + 1 ) ) << k ; if ( ( ( n >> k ) & 1 ) != 0 ) res += n & ( ( 1 << k ) - 1 ) ; return res ; } public static void main ( String [ ] args ) { long N = 14 ; int K = 2 ; System . out . println ( getcount ( N + 1 , K ) ) ; } }
import java . util . * ; class GFG { static void splitString ( String S , int N ) { char c = S . charAt ( N - 1 ) ; int f = 0 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( S . charAt ( i ) == c ) { f = 1 ; break ; } } if ( f > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { String S = " abcdab " ; int N = S . length ( ) ; splitString ( S , N ) ; } }
import java . util . * ; class GFG { static char longestLastingBulb ( int [ ] onTime , char [ ] s ) { char ans ; int n = onTime . length ; int maxDur = Integer . MIN_VALUE ; int maxPos = Integer . MIN_VALUE ; int currentDiff = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i == 0 ) { currentDiff = onTime [ i ] ; maxDur = currentDiff ; maxPos = i ; } else { currentDiff = onTime [ i ] - onTime [ i - 1 ] ; if ( maxDur < currentDiff ) { maxDur = currentDiff ; maxPos = i ; } else { if ( maxDur == currentDiff ) { char one = s [ i ] ; char two = s [ maxPos ] ; if ( one > two ) { maxDur = currentDiff ; maxPos = i ; } } } } } ans = s [ maxPos ] ; return ans ; } public static void main ( String [ ] args ) { String S = " spuda " ; int [ ] arr = { 12 , 23 , 36 , 46 , 62 } ; System . out . print ( longestLastingBulb ( arr , S . toCharArray ( ) ) ) ; } }
import java . util . ArrayList ; import java . util . Collections ; import java . util . Comparator ; class GFG { static ArrayList < int [ ] > tup = new ArrayList < > ( ) ; static void findIntersectingRange ( int N , int ranges [ ] [ ] ) { int curr ; int currPos ; for ( int i = 0 ; i < N ; i ++ ) { int x , y ; x = ranges [ i ] [ 0 ] ; y = ranges [ i ] [ 1 ] ; int [ ] arr = { x , y , i + 1 } ; tup . add ( arr ) ; } Collections . sort ( tup , new Comparator < int [ ] > ( ) { public int compare ( int [ ] a , int [ ] b ) { if ( a [ 0 ] == b [ 0 ] ) { if ( a [ 1 ] == b [ 1 ] ) { return a [ 2 ] - b [ 2 ] ; } else { return a [ 1 ] - b [ 1 ] ; } } return a [ 0 ] - b [ 0 ] ; } } ) ; curr = tup . get ( 0 ) [ 1 ] ; currPos = tup . get ( 0 ) [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { int Q = tup . get ( i - 1 ) [ 0 ] ; int R = tup . get ( i ) [ 0 ] ; if ( Q == R ) { if ( tup . get ( i - 1 ) [ 1 ] < tup . get ( i ) [ 1 ] ) System . out . print ( tup . get ( i - 1 ) [ 2 ] + " ▁ " + tup . get ( i ) [ 2 ] ) ; else System . out . print ( tup . get ( i ) [ 2 ] + " ▁ " + tup . get ( i - 1 ) [ 2 ] ) ; return ; } int T = tup . get ( i ) [ 1 ] ; if ( T <= curr ) { System . out . print ( tup . get ( i ) [ 2 ] + " ▁ " + currPos ) ; return ; } else { curr = T ; currPos = tup . get ( i ) [ 2 ] ; } } System . out . print ( " - 1 ▁ - 1" ) ; } public static void main ( String [ ] args ) { int N = 5 ; int ranges [ ] [ ] = { { 1 , 5 } , { 2 , 10 } , { 3 , 10 } , { 2 , 2 } , { 2 , 15 } } ; findIntersectingRange ( N , ranges ) ; } }
import java . util . * ; class GFG { static void printNGE ( int arr [ ] , int n ) { Stack < Integer > s = new Stack < > ( ) ; int nge [ ] = new int [ n ] ; int i = 0 ; for ( i = 0 ; i < n ; i ++ ) { nge [ i ] = - 1 ; } i = 0 ; while ( i < 2 * n ) { while ( ! s . isEmpty ( ) && arr [ i % n ] > arr [ s . peek ( ) ] ) { nge [ s . peek ( ) ] = arr [ i % n ] ; s . pop ( ) ; } s . push ( i % n ) ; i ++ ; } for ( i = 0 ; i < n ; i ++ ) { System . out . print ( nge [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , - 2 , 5 , 8 } ; int N = arr . length ; printNGE ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void findWordsSameRow ( List < String > arr ) { Map < Character , Integer > mp = new HashMap < Character , Integer > ( ) ; mp . put ( ' q ' , 1 ) ; mp . put ( ' w ' , 1 ) ; mp . put ( ' e ' , 1 ) ; mp . put ( ' r ' , 1 ) ; mp . put ( ' t ' , 1 ) ; mp . put ( ' y ' , 1 ) ; mp . put ( ' u ' , 1 ) ; mp . put ( ' i ' , 1 ) ; mp . put ( ' o ' , 1 ) ; mp . put ( ' p ' , 1 ) ; mp . put ( ' a ' , 2 ) ; mp . put ( ' s ' , 2 ) ; mp . put ( ' d ' , 2 ) ; mp . put ( ' f ' , 2 ) ; mp . put ( ' g ' , 2 ) ; mp . put ( ' h ' , 2 ) ; mp . put ( ' j ' , 2 ) ; mp . put ( ' k ' , 2 ) ; mp . put ( ' l ' , 2 ) ; mp . put ( ' z ' , 3 ) ; mp . put ( ' x ' , 3 ) ; mp . put ( ' c ' , 3 ) ; mp . put ( ' v ' , 3 ) ; mp . put ( ' b ' , 3 ) ; mp . put ( ' n ' , 3 ) ; mp . put ( ' m ' , 3 ) ; for ( String word : arr ) { if ( word . length ( ) != 0 ) { boolean flag = true ; int rowNum = mp . get ( Character . toLowerCase ( word . charAt ( 0 ) ) ) ; int M = word . length ( ) ; for ( int i = 1 ; i < M ; i ++ ) { if ( mp . get ( Character . toLowerCase ( word . charAt ( i ) ) ) != rowNum ) { flag = false ; break ; } } if ( flag ) { System . out . print ( word + " ▁ " ) ; } } } } public static void main ( String [ ] args ) { List < String > words = Arrays . asList ( " Yeti " , " Had " , " GFG " , " comment " ) ; findWordsSameRow ( words ) ; } }
import java . io . * ; class GFG { static int count_zeroes ( int n , String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == '0' ) cnt ++ ; } return cnt ; } static String kReducingStringUtil ( int n , int k , String str , int no_of_zeroes ) { int zeroes_in_2k = ( ( no_of_zeroes ) * ( 2 * k ) ) / n ; int ones_in_2k = 2 * k - zeroes_in_2k ; String temp_str = " " ; for ( int i = 0 ; i < ( zeroes_in_2k ) / 2 ; i ++ ) { temp_str += '0' ; } for ( int i = 0 ; i < ones_in_2k ; i ++ ) { temp_str += '1' ; } for ( int i = 0 ; i < ( zeroes_in_2k ) / 2 ; i ++ ) { temp_str += '0' ; } String final_str = " " ; for ( int i = 0 ; i < n / ( 2 * k ) ; i ++ ) { final_str += ( temp_str ) ; } for ( int i = 0 ; i < n % ( 2 * k ) ; i ++ ) { final_str += temp_str . charAt ( i ) ; } return final_str ; } static String kReducingString ( int n , int k , String str ) { int no_of_zeroes = count_zeroes ( n , str ) ; int no_of_ones = n - no_of_zeroes ; if ( no_of_zeroes == 0 no_of_zeroes == n ) { return str ; } if ( k == 1 ) { if ( no_of_zeroes == 0 no_of_zeroes == n ) { return str ; } else { return " Not ▁ Possible " ; } } boolean check = false ; for ( int i = ( n / k ) ; i < n ; i += ( n / k ) ) { if ( no_of_zeroes == i no_of_ones == i ) { check = true ; break ; } } if ( check == false ) { return " Not ▁ Possible " ; } return kReducingStringUtil ( n , k , str , no_of_zeroes ) ; } public static void main ( String [ ] args ) { String str = "0000100001100001" ; int K = 4 ; int N = str . length ( ) ; System . out . println ( kReducingString ( N , K , str ) ) ; } }
import java . util . * ; class GFG { static int countSum ( int a [ ] , int n , int sum ) { int i , j , k ; int count = 0 ; HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( i = 0 ; i < n - 1 ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum && m . containsKey ( sum - temp ) ) count += m . get ( sum - temp ) ; } for ( j = 0 ; j < i ; j ++ ) { int temp = a [ i ] + a [ j ] ; if ( temp < sum ) if ( m . containsKey ( temp ) ) m . put ( temp , m . get ( temp ) + 1 ) ; else m . put ( temp , 1 ) ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 3 , 1 , 2 , 4 } ; int S = 13 ; int N = arr . length ; System . out . print ( countSum ( arr , N , S ) ) ; } }
import java . util . * ; class GFG { static final int MOD = 1000000007 ; static long modPower ( long x , int y ) { long res = 1 ; x = x % MOD ; if ( x == 0 ) return 0 ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % MOD ; y = y / 2 ; x = ( x * x ) % MOD ; } return res ; } static int countPairs ( long arr [ ] , int N ) { int pairCount = 0 ; HashMap < Long , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( hash . containsKey ( arr [ i ] ) ) { hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } else { hash . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < N ; i ++ ) { long modularInverse = modPower ( arr [ i ] , MOD - 2 ) ; if ( hash . containsKey ( modularInverse ) ) pairCount += hash . get ( modularInverse ) ; if ( arr [ i ] == modularInverse ) { pairCount -- ; } } return pairCount / 2 ; } public static void main ( String [ ] args ) { long arr [ ] = { 2 , 236426 , 280311812 , 500000004 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int N = 9 ; static boolean isinRange ( int [ ] [ ] board ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( board [ i ] [ j ] <= 0 board [ i ] [ j ] > 9 ) { return false ; } } } return true ; } static boolean isValidSudoku ( int board [ ] [ ] ) { if ( isinRange ( board ) == false ) { return false ; } boolean [ ] unique = new boolean [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { Arrays . fill ( unique , false ) ; for ( int j = 0 ; j < N ; j ++ ) { int Z = board [ i ] [ j ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( int i = 0 ; i < N ; i ++ ) { Arrays . fill ( unique , false ) ; for ( int j = 0 ; j < N ; j ++ ) { int Z = board [ j ] [ i ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } for ( int i = 0 ; i < N - 2 ; i += 3 ) { for ( int j = 0 ; j < N - 2 ; j += 3 ) { Arrays . fill ( unique , false ) ; for ( int k = 0 ; k < 3 ; k ++ ) { for ( int l = 0 ; l < 3 ; l ++ ) { int X = i + k ; int Y = j + l ; int Z = board [ X ] [ Y ] ; if ( unique [ Z ] ) { return false ; } unique [ Z ] = true ; } } } } return true ; } public static void main ( String [ ] args ) { int [ ] [ ] board = { { 7 , 9 , 2 , 1 , 5 , 4 , 3 , 8 , 6 } , { 6 , 4 , 3 , 8 , 2 , 7 , 1 , 5 , 9 } , { 8 , 5 , 1 , 3 , 9 , 6 , 7 , 2 , 4 } , { 2 , 6 , 5 , 9 , 7 , 3 , 8 , 4 , 1 } , { 4 , 8 , 9 , 5 , 6 , 1 , 2 , 7 , 3 } , { 3 , 1 , 7 , 4 , 8 , 2 , 9 , 6 , 5 } , { 1 , 3 , 6 , 7 , 4 , 8 , 5 , 9 , 2 } , { 9 , 7 , 4 , 2 , 1 , 5 , 6 , 3 , 8 } , { 5 , 2 , 8 , 6 , 3 , 9 , 4 , 1 , 7 } } ; if ( isValidSudoku ( board ) ) { System . out . println ( " Valid " ) ; } else { System . out . println ( " Not ▁ Valid " ) ; } } }
import java . util . * ; class GFG { static int computePos ( int arr [ ] , int n , int value ) { if ( value < arr [ 0 ] ) return 0 ; if ( value > arr [ n - 1 ] ) return n - 1 ; int start = 0 ; int end = n - 1 ; while ( start < end ) { int mid = ( start + end + 1 ) / 2 ; if ( arr [ mid ] >= value ) end = mid - 1 ; else start = mid ; } return start ; } static void countShift ( int arr [ ] , int n , Vector < Vector < Integer > > queries ) { for ( Vector < Integer > q : queries ) { int index = q . get ( 0 ) ; int update = q . get ( 1 ) ; int newElement = arr [ index ] + update ; int newIndex = computePos ( arr , n , newElement ) ; System . out . print ( Math . abs ( newIndex - index ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int N = arr . length ; Vector < Vector < Integer > > queries = new Vector < > ( ) ; Vector < Integer > v = new Vector < > ( ) ; Vector < Integer > v1 = new Vector < > ( ) ; v . add ( 0 ) ; v . add ( - 1 ) ; queries . add ( v ) ; v1 . add ( 4 ) ; v1 . add ( - 11 ) ; queries . add ( v1 ) ; countShift ( arr , N , queries ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return temp ; } static int ans ; static void countReqNodes ( Node root , int minNodeVal ) { if ( root == null ) return ; if ( root . key <= minNodeVal ) { minNodeVal = root . key ; ans ++ ; } countReqNodes ( root . left , minNodeVal ) ; countReqNodes ( root . right , minNodeVal ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 6 ) ; root . left . right = newNode ( 7 ) ; root . right . left = newNode ( 3 ) ; root . right . right = newNode ( 9 ) ; int minNodeVal = Integer . MAX_VALUE ; ans = 0 ; countReqNodes ( root , minNodeVal ) ; System . out . print ( ans ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minElements ( int [ ] A , int [ ] B , int N , int M ) { Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < A . length ; i ++ ) { map . put ( A [ i ] , i ) ; } ArrayList < Integer > subseq = new ArrayList < > ( ) ; int l = 0 , r = - 1 ; for ( int i = 0 ; i < M ; i ++ ) { if ( map . containsKey ( B [ i ] ) ) { int e = map . get ( B [ i ] ) ; while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( subseq . get ( m ) < e ) l = m + 1 ; else r = m - 1 ; } if ( r + 1 < subseq . size ( ) ) { subseq . set ( r + 1 , e ) ; } else { subseq . add ( e ) ; } l = 0 ; r = subseq . size ( ) - 1 ; } } return N - subseq . size ( ) ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 2 , 3 , 4 , 5 } ; int [ ] B = { 2 , 5 , 6 , 4 , 9 , 12 } ; int M = A . length ; int N = B . length ; System . out . println ( minElements ( A , B , M , N ) ) ; } }
import java . io . * ; class GFG { public static boolean palindrome ( int [ ] a , int i , int j ) { while ( i < j ) { if ( a [ i ] != a [ j ] ) return false ; i ++ ; j -- ; } return true ; } static int findSubArray ( int [ ] arr , int k ) { int n = arr . length ; for ( int i = 0 ; i <= n - k ; i ++ ) { if ( palindrome ( arr , i , i + k - 1 ) ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 5 , 1 , 3 } ; int k = 4 ; int ans = findSubArray ( arr , k ) ; if ( ans == - 1 ) System . out . print ( - 1 + "NEW_LINE"); else { for ( int i = ans ; i < ans + k ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . print ( "NEW_LINE"); } } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean sfpa ( int V , int src , int Edges [ ] [ ] , int M ) { Vector < pair > [ ] g = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) { g [ i ] = new Vector < pair > ( ) ; } for ( int i = 0 ; i < M ; i ++ ) { int u = Edges [ i ] [ 0 ] ; int v = Edges [ i ] [ 1 ] ; int w = Edges [ i ] [ 2 ] ; g [ u ] . add ( new pair ( v , w ) ) ; } int [ ] dist = new int [ V ] ; Arrays . fill ( dist , Integer . MAX_VALUE ) ; boolean [ ] inQueue = new boolean [ V ] ; int [ ] cnt = new int [ V ] ; dist [ src ] = 0 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( src ) ; inQueue [ src ] = true ; while ( ! q . isEmpty ( ) ) { int u = q . peek ( ) ; q . remove ( ) ; inQueue [ u ] = false ; for ( pair x : g [ u ] ) { int v = x . first ; int cost = x . second ; if ( dist [ v ] > dist [ u ] + cost ) { dist [ v ] = dist [ u ] + cost ; if ( ! inQueue [ v ] ) { q . add ( v ) ; inQueue [ v ] = true ; cnt [ v ] ++ ; if ( cnt [ v ] >= V ) return true ; } } } } return false ; } public static void main ( String [ ] args ) { int N = 4 ; int src = 0 ; int M = 4 ; int Edges [ ] [ ] = { { 0 , 1 , 1 } , { 1 , 2 , - 1 } , { 2 , 3 , - 1 } , { 3 , 0 , - 1 } } ; if ( sfpa ( N , src , Edges , M ) == true ) System . out . print ( " Yes " + "NEW_LINE"); else System . out . print ( " No " + "NEW_LINE"); } }
class GFG { static final int INF = 99999 ; static int floyd_warshall ( int [ ] [ ] graph , int V ) { int [ ] [ ] dist = new int [ V ] [ V ] ; int i , j , k ; for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { dist [ i ] [ j ] = graph [ i ] [ j ] ; } } for ( k = 0 ; k < V ; k ++ ) { for ( i = 0 ; i < V ; i ++ ) { for ( j = 0 ; j < V ; j ++ ) { if ( dist [ i ] [ k ] + dist [ k ] [ j ] < dist [ i ] [ j ] ) { dist [ i ] [ j ] = dist [ i ] [ k ] + dist [ k ] [ j ] ; } } } } int sum = 0 ; for ( i = 0 ; i < V ; i ++ ) { for ( j = i + 1 ; j < V ; j ++ ) { sum += dist [ i ] [ j ] ; } } return sum ; } static int sumOfshortestPath ( int N , int E , int edges [ ] [ ] ) { int [ ] [ ] g = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { g [ i ] [ j ] = INF ; } } for ( int i = 0 ; i < E ; i ++ ) { int u = edges [ i ] [ 0 ] ; int v = edges [ i ] [ 1 ] ; int w = edges [ i ] [ 2 ] ; g [ u ] [ v ] = w ; g [ v ] [ u ] = w ; } return floyd_warshall ( g , N ) ; } public static void main ( String [ ] args ) { int N = 4 ; int E = 3 ; int Edges [ ] [ ] = { { 0 , 1 , 1 } , { 1 , 2 , 2 } , { 2 , 3 , 3 } } ; System . out . print ( sumOfshortestPath ( N , E , Edges ) ) ; } }
import java . io . * ; class GFG { static void print_kth_string ( String s , int K ) { int N = s . length ( ) ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' ▁ ' ) K -- ; if ( K == 1 ) break ; } while ( i ++ < N - 1 && s . charAt ( i ) != ' ▁ ' ) System . out . print ( s . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String s = "10 ▁ 20 ▁ 30 ▁ 40" ; int K = 4 ; print_kth_string ( s , K ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void minPeaks ( ArrayList < Integer > list ) { int n = list . size ( ) ; ArrayList < Integer > result = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int min = Integer . MAX_VALUE ; int index = - 1 ; int size = list . size ( ) ; for ( int j = 0 ; j < size ; j ++ ) { if ( j == 0 && j + 1 < size ) { if ( list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( j == size - 1 && j - 1 >= 0 ) { if ( list . get ( j ) > list . get ( j - 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } else if ( size == 1 ) { min = list . get ( j ) ; index = j ; } else if ( list . get ( j ) > list . get ( j - 1 ) && list . get ( j ) > list . get ( j + 1 ) && min > list . get ( j ) ) { min = list . get ( j ) ; index = j ; } } list . remove ( index ) ; result . add ( min ) ; } System . out . println ( result ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( Arrays . asList ( 1 , 9 , 7 , 8 , 2 , 6 ) ) ; minPeaks ( arr ) ; } }
class GFG { static int binarySearch ( int arr [ ] , int N , int X ) { int start = 0 ; int end = N ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( X == arr [ mid ] ) { return mid ; } else if ( X < arr [ mid ] ) { start = mid + 1 ; } else { end = mid - 1 ; } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = arr . length ; int X = 5 ; System . out . println ( binarySearch ( arr , N , X ) ) ; } }
import java . io . * ; class GFG { public static boolean check_both_present ( int [ ] arr , int N , int a , int b ) { boolean f1 = false , f2 = false ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == a ) { f1 = true ; } if ( arr [ i ] == b ) { f2 = true ; } } if ( f1 && f2 ) { return true ; } else { return false ; } } public static void print_array ( int [ ] ans , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void build_AP ( int N , int a , int b ) { int [ ] arr = new int [ N ] ; int [ ] ans = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) ans [ i ] = Integer . MAX_VALUE ; int flag = 0 ; if ( a > b ) { a += ( b - ( b = a ) ) ; } int diff = b - a ; for ( int start = 1 ; start <= a ; start ++ ) { for ( int d = 1 ; d <= diff ; d ++ ) { arr [ 0 ] = start ; for ( int i = 1 ; i < N ; i ++ ) { arr [ i ] = arr [ i - 1 ] + d ; } if ( check_both_present ( arr , N , a , b ) && arr [ N - 1 ] < ans [ N - 1 ] ) { for ( int i = 0 ; i < N ; i ++ ) { ans [ i ] = arr [ i ] ; } } } } print_array ( ans , N ) ; } public static void main ( String [ ] args ) { int N = 5 , A = 20 , B = 50 ; build_AP ( N , A , B ) ; } }
import java . util . * ; import java . io . * ; import java . lang . Math ; class GFG { static String isPrefixOfWord ( String sentence , String Word ) { String a [ ] = sentence . split ( " ▁ " ) ; ArrayList < String > v = new ArrayList < > ( ) ; for ( String e : a ) v . add ( e ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = 0 ; j < v . get ( i ) . length ( ) ; j ++ ) { if ( v . get ( i ) . charAt ( j ) != Word . charAt ( j ) ) break ; else if ( j == Word . length ( ) - 1 ) return v . get ( i ) ; } } return " - 1" ; } public static void main ( final String [ ] args ) { String s = " Welcome ▁ to ▁ Geeksforgeeks " ; String word = " Gee " ; System . out . println ( isPrefixOfWord ( s , word ) ) ; } }
import java . util . * ; class GFG { public static class Pair { int idx ; String psf ; int jmps ; Pair ( int idx , String psf , int jmps ) { this . idx = idx ; this . psf = psf ; this . jmps = jmps ; } } public static int [ ] minJumps ( int [ ] arr ) { int dp [ ] = new int [ arr . length ] ; Arrays . fill ( dp , Integer . MAX_VALUE ) ; int n = dp . length ; dp [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int steps = arr [ i ] ; int min = Integer . MAX_VALUE ; for ( int j = 1 ; j <= steps && i + j < n ; j ++ ) { if ( dp [ i + j ] != Integer . MAX_VALUE && dp [ i + j ] < min ) { min = dp [ i + j ] ; } } if ( min != Integer . MAX_VALUE ) dp [ i ] = min + 1 ; } return dp ; } public static void possiblePath ( int [ ] arr , int [ ] dp ) { Queue < Pair > queue = new LinkedList < > ( ) ; queue . add ( new Pair ( 0 , " " + 0 , dp [ 0 ] ) ) ; while ( queue . size ( ) > 0 ) { Pair tmp = queue . remove ( ) ; if ( tmp . jmps == 0 ) { System . out . println ( tmp . psf ) ; continue ; } for ( int step = 1 ; step <= arr [ tmp . idx ] ; step ++ ) { if ( tmp . idx + step < arr . length && tmp . jmps - 1 == dp [ tmp . idx + step ] ) { queue . add ( new Pair ( tmp . idx + step , tmp . psf + " ▁ - > ▁ " + ( tmp . idx + step ) , tmp . jmps - 1 ) ) ; } } } } public static void Solution ( int arr [ ] ) { int dp [ ] = minJumps ( arr ) ; possiblePath ( arr , dp ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 3 , 0 , 2 , 1 , 2 , 4 , 2 , 0 , 0 } ; int size = arr . length ; Solution ( arr ) ; } }
import java . util . * ; import java . io . * ; class GFG { static void EvenLIS ( int arr [ ] ) { int n = arr . length ; int lis [ ] = new int [ n ] ; Arrays . fill ( lis , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( Math . abs ( arr [ i ] ) > Math . abs ( arr [ j ] ) && Math . abs ( arr [ i ] ) % 2 == 0 && Math . abs ( arr [ j ] ) % 2 == 0 && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; } } int maxlen = 0 ; for ( int i = 0 ; i < n ; i ++ ) maxlen = Math . max ( maxlen , lis [ i ] ) ; System . out . println ( maxlen ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 11 , - 22 , 43 , - 54 , 66 , 5 } ; int N = arr . length ; EvenLIS ( arr ) ; } }
class GFG { static int res = 0 ; static class Node { int data ; Node left ; Node right ; Node ( int val ) { data = val ; left = right = null ; } } ; static int Sum ( Node root ) { if ( root == null ) { return 0 ; } int leftSubSum = Sum ( root . left ) ; int rightSubSum = Sum ( root . right ) ; if ( root . left != null && root . right != null && leftSubSum == rightSubSum ) res += 1 ; return root . data + leftSubSum + rightSubSum ; } public static void main ( String [ ] args ) { Node root = new Node ( 9 ) ; root . left = new Node ( 2 ) ; root . left . left = new Node ( - 1 ) ; root . left . right = new Node ( 3 ) ; root . right = new Node ( 4 ) ; root . right . right = new Node ( 0 ) ; res = 0 ; Sum ( root ) ; System . out . print ( res ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left ; Node right ; Node ( int val ) { data = val ; left = right = null ; } } ; static void PrintAlternate ( Node root ) { Queue < Node > Q = new LinkedList < > ( ) ; Q . add ( root ) ; while ( ! Q . isEmpty ( ) ) { int N = Q . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Node temp = Q . peek ( ) ; Q . remove ( ) ; if ( i % 2 == 0 ) { System . out . print ( temp . data + " ▁ " ) ; } if ( temp . left != null ) { Q . add ( temp . left ) ; } if ( temp . right != null ) { Q . add ( temp . right ) ; } } System . out . println ( ) ; } } public static void main ( String [ ] args ) { Node root ; root = new Node ( 71 ) ; root . left = new Node ( 88 ) ; root . right = new Node ( 99 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 5 ) ; root . right . left = new Node ( 6 ) ; root . right . right = new Node ( 7 ) ; root . left . left . left = new Node ( 8 ) ; root . left . left . right = new Node ( 9 ) ; root . left . right . left = new Node ( 10 ) ; root . left . right . right = new Node ( 11 ) ; root . right . left . right = new Node ( 13 ) ; root . right . right . left = new Node ( 14 ) ; PrintAlternate ( root ) ; } }
import java . util . * ; public class gfg { static int [ ] countingSubarray ( int arr [ ] , int n ) { int a [ ] = new int [ n ] ; int nsml [ ] = new int [ n ] ; Arrays . fill ( nsml , - 1 ) ; int nsmr [ ] = new int [ n ] ; Arrays . fill ( nsmr , n ) ; Stack < Integer > st = new Stack < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( st . size ( ) > 0 && arr [ ( int ) st . peek ( ) ] >= arr [ i ] ) st . pop ( ) ; nsmr [ i ] = ( st . size ( ) > 0 ) ? ( int ) st . peek ( ) : n ; st . push ( i ) ; } while ( st . size ( ) > 0 ) st . pop ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . size ( ) > 0 && arr [ ( int ) st . peek ( ) ] >= arr [ i ] ) st . pop ( ) ; nsml [ i ] = ( st . size ( ) > 0 ) ? ( int ) st . peek ( ) : - 1 ; st . push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { nsml [ i ] ++ ; nsmr [ i ] -- ; int r = nsmr [ i ] - i + 1 ; int l = i - nsml [ i ] + 1 ; a [ i ] = r * l ; } return a ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 3 , 2 , 4 , 1 , 5 } ; int a [ ] = countingSubarray ( arr , N ) ; System . out . print ( " [ " ) ; int n = a . length - 1 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " , ▁ " ) ; System . out . print ( a [ n ] + " ] " ) ; } }
class GFG { static String reorderString ( char [ ] A , char [ ] B ) { int size_a = A . length ; int size_b = B . length ; int freq [ ] = new int [ 300 ] ; for ( int i = 0 ; i < size_b ; i ++ ) freq [ B [ i ] ] ++ ; for ( int i = 0 ; i < size_a ; i ++ ) freq [ A [ i ] ] -- ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) { if ( freq [ j ] < 0 ) return " - 1" ; } String answer = " " ; for ( int j = ' a ' ; j < A [ 0 ] ; j ++ ) while ( freq [ j ] > 0 ) { answer += j ; freq [ j ] -- ; } int first = A [ 0 ] ; for ( int j = 0 ; j < size_a ; j ++ ) { if ( A [ j ] > A [ 0 ] ) break ; if ( A [ j ] < A [ 0 ] ) { answer += String . valueOf ( A ) ; A = new char [ A . length ] ; break ; } } while ( freq [ first ] > 0 ) { answer += String . valueOf ( ( char ) first ) ; -- freq [ first ] ; } answer += String . valueOf ( A ) ; for ( int j = ' a ' ; j <= ' z ' ; j ++ ) while ( freq [ j ] -- > 0 ) answer += ( ( char ) j ) ; return answer ; } public static void main ( String [ ] args ) { String A = " aa " ; String B = " ababab " ; System . out . print ( reorderString ( A . toCharArray ( ) , B . toCharArray ( ) ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void maxConcurrentMeetingSlot ( int [ ] [ ] meetings ) { Arrays . sort ( meetings , ( a , b ) -> ( a [ 0 ] != b [ 0 ] ) ? a [ 0 ] - b [ 0 ] : a [ 1 ] - b [ 1 ] ) ; PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; pq . add ( meetings [ 0 ] [ 1 ] ) ; int max_len = 0 , max_start = 0 ; int max_end = 0 ; for ( int [ ] k : meetings ) { while ( ! pq . isEmpty ( ) && k [ 0 ] >= pq . peek ( ) ) pq . poll ( ) ; pq . add ( k [ 1 ] ) ; if ( pq . size ( ) > max_len ) { max_len = pq . size ( ) ; max_start = k [ 0 ] ; max_end = pq . peek ( ) ; } } System . out . println ( max_start + " ▁ " + max_end ) ; } public static void main ( String [ ] args ) { int meetings [ ] [ ] = { { 100 , 200 } , { 50 , 300 } , { 300 , 400 } } ; maxConcurrentMeetingSlot ( meetings ) ; } }
import java . util . * ; class GFG { static int func ( int n ) { int count = 0 ; if ( n == 2 n == 1 ) return 1 ; if ( n % 2 == 0 ) count = 1 + func ( n / 2 ) ; if ( n % 2 != 0 ) count = 1 + func ( n * 3 + 1 ) ; return count ; } static void findKthElement ( int l , int r , int k ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; for ( int i = l ; i <= r ; i ++ ) arr . add ( i ) ; ArrayList < List < Integer > > result = new ArrayList < > ( ) ; for ( int i : arr ) result . add ( Arrays . asList ( i , func ( i ) ) ) ; Collections . sort ( result , ( s1 , s2 ) -> s1 . get ( 1 ) - s2 . get ( 1 ) ) ; System . out . println ( result . get ( k - 1 ) . get ( 0 ) ) ; } public static void main ( String [ ] args ) { int l = 12 ; int r = 15 ; int k = 2 ; findKthElement ( l , r , k ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static void checkHex ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { char ch = s . charAt ( i ) ; if ( ( ch < '0' ch > '9' ) && ( ch < ' A ' ch > ' F ' ) ) { System . out . println ( " No " ) ; return ; } } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { String s = " BF57C " ; checkHex ( s ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxWidth ( int N , int M , ArrayList < Integer > cost , ArrayList < ArrayList < Integer > > s ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } for ( int i = 0 ; i < M ; i ++ ) { adj . get ( s . get ( i ) . get ( 0 ) ) . add ( s . get ( i ) . get ( 1 ) ) ; } int result = 0 ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; while ( q . size ( ) != 0 ) { int count = q . size ( ) ; result = Math . max ( count , result ) ; while ( count -- > 0 ) { int temp = q . remove ( ) ; for ( int i = 0 ; i < adj . get ( temp ) . size ( ) ; i ++ ) { q . add ( adj . get ( temp ) . get ( i ) ) ; } } } return result ; } public static void main ( String [ ] args ) { int N = 11 , M = 10 ; ArrayList < ArrayList < Integer > > edges = new ArrayList < ArrayList < Integer > > ( ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , 1 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , 2 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , 3 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 4 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 1 , 5 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 3 , 6 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 4 , 7 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 10 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 8 ) ) ) ; edges . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 9 ) ) ) ; ArrayList < Integer > cost = new ArrayList < Integer > ( Arrays . asList ( 1 , 2 , - 1 , 3 , 4 , 5 , 8 , 2 , 6 , 12 , 7 ) ) ; System . out . println ( maxWidth ( N , M , cost , edges ) ) ; } }
import java . util . * ; class GFG { static final int N = 100005 ; static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int LISPrime ( int arr [ ] , int n ) { int [ ] lisp = new int [ n ] ; boolean [ ] prime = new boolean [ N + 1 ] ; for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , N ) ; lisp [ 0 ] = prime [ arr [ 0 ] ] ? 1 : 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ! prime [ arr [ i ] ] ) { lisp [ i ] = 0 ; continue ; } lisp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( prime [ arr [ j ] ] && arr [ i ] > arr [ j ] && lisp [ i ] < lisp [ j ] + 1 ) { lisp [ i ] = lisp [ j ] + 1 ; } } } return Arrays . stream ( lisp ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 3 , 2 , 5 , 1 , 7 } ; int M = arr . length ; System . out . print ( LISPrime ( arr , M ) ) ; } }
import java . io . * ; class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } static long findWays ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; return catalan ( n / 2 ) ; } static void countNonNPeriodic ( int N ) { System . out . println ( findWays ( 2 * N ) - findWays ( N ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; countNonNPeriodic ( N ) ; } }
class GFG { static void checkSameDiag ( int li [ ] [ ] , int x , int y , int m , int n ) { int I = 0 , J = 0 ; int P = 0 , Q = 0 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( li [ i ] [ j ] == x ) { I = i ; J = j ; } if ( li [ i ] [ j ] == y ) { P = i ; Q = j ; } } } if ( P - Q == I - J P + Q == I + J ) { System . out . println ( " YES " ) ; } else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int m = 6 ; int n = 5 ; int [ ] [ ] li = { { 32 , 94 , 99 , 26 , 82 } , { 51 , 69 , 52 , 63 , 17 } , { 90 , 36 , 88 , 55 , 33 } , { 93 , 42 , 73 , 39 , 28 } , { 81 , 31 , 83 , 53 , 10 } , { 12 , 29 , 85 , 80 , 87 } } ; int x = 42 ; int y = 80 ; checkSameDiag ( li , x , y , m , n ) ; } }
import java . util . * ; class GFG { static int isValid ( int n , int m , int k ) { int step2 = n - m ; int cnt = ( step2 * ( step2 + 1 ) ) / 2 ; if ( cnt - m == k ) return 0 ; if ( cnt - m > k ) return 1 ; return - 1 ; } static void countOfOperations ( int n , int k ) { int start = 0 , end = n ; boolean ok = true ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int temp = isValid ( n , mid , k ) ; if ( temp == 0 ) { ok = false ; System . out . print ( mid ) ; break ; } else if ( temp == 1 ) { start = mid + 1 ; } else { end = mid - 1 ; } } if ( ok ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int N = 5 , K = 4 ; countOfOperations ( N , K ) ; } }
import java . io . * ; class GFG { public static boolean nonLower ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ! Character . isLowerCase ( s . charAt ( i ) ) ) { return true ; } } return false ; } public static void printCodes ( String output [ ] ) { for ( int i = 0 ; i < output . length ; i ++ ) { if ( nonLower ( output [ i ] ) ) continue ; System . out . println ( output [ i ] ) ; } } public static char getChar ( int n ) { return ( char ) ( n + 96 ) ; } public static String [ ] getCode ( String str ) { if ( str . length ( ) == 0 ) { String ans [ ] = { " " } ; return ans ; } String output1 [ ] = getCode ( str . substring ( 1 ) ) ; String output2 [ ] = new String [ 0 ] ; int firstDigit = ( str . charAt ( 0 ) - '0' ) ; int firstTwoDigit = 0 ; if ( str . length ( ) >= 2 ) { firstTwoDigit = ( str . charAt ( 0 ) - '0' ) * 10 + ( str . charAt ( 1 ) - '0' ) ; if ( firstTwoDigit >= 10 && firstTwoDigit <= 26 ) { output2 = getCode ( str . substring ( 2 ) ) ; } } String output [ ] = new String [ output1 . length + output2 . length ] ; int k = 0 ; for ( int i = 0 ; i < output1 . length ; i ++ ) { char ch = getChar ( firstDigit ) ; output [ i ] = ch + output1 [ i ] ; k ++ ; } for ( int i = 0 ; i < output2 . length ; i ++ ) { char ch = getChar ( firstTwoDigit ) ; output [ k ] = ch + output2 [ i ] ; k ++ ; } return output ; } public static void main ( String [ ] args ) { String input = "101" ; String output [ ] = getCode ( input ) ; printCodes ( output ) ; } }
import java . util . * ; class GFG { static int isprm ( int n ) { if ( n <= 1 ) return 0 ; if ( n <= 3 ) return 1 ; if ( n % 2 == 0 n % 3 == 0 ) return 0 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) return 0 ; } return 1 ; } static int countprime ( int n ) { int count = 0 ; Vector < Integer > primevector = new Vector < > ( ) ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isprm ( i ) == 1 ) { primevector . add ( i ) ; } } int sum = primevector . elementAt ( 0 ) ; for ( int i = 1 ; i < primevector . size ( ) ; i ++ ) { sum += primevector . elementAt ( i ) ; if ( sum > n ) break ; if ( isprm ( sum ) == 1 ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int N = 45 ; System . out . print ( countprime ( N ) ) ; } }
import java . util . * ; class GFG { static int TEN = 10 ; static void updateFreq ( int n , int freq [ ] ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int freqA [ ] = new int [ TEN ] ; int freqB [ ] = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } static boolean isPowerOf2 ( int N ) { for ( int i = 0 ; i < 32 ; i ++ ) { if ( areAnagrams ( 1 << i , N ) ) { System . out . print ( ( 1 << i ) ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { int N = 46 ; if ( ! isPowerOf2 ( N ) ) { System . out . print ( " No " ) ; } } }
import java . io . * ; class GFG { static int right_angled ( int n ) { int count = 0 ; for ( int z = 1 ; z <= n ; z ++ ) { for ( int y = 1 ; y <= z ; y ++ ) { for ( int x = 1 ; x <= y ; x ++ ) { if ( ( x * x ) + ( y * y ) == ( z * z ) ) { count ++ ; } } } } return count ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( right_angled ( n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int count ( String s ) { int cnt = 0 ; for ( char c : s . toCharArray ( ) ) { cnt += c == '0' ? 1 : 0 ; } if ( cnt % 3 != 0 ) return 0 ; int res = 0 , k = cnt / 3 , sum = 0 ; Map < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum += s . charAt ( i ) == '0' ? 1 : 0 ; if ( sum == 2 * k && map . containsKey ( k ) && i < s . length ( ) - 1 && i > 0 ) { res += map . get ( k ) ; } map . put ( sum , map . getOrDefault ( sum , 0 ) + 1 ) ; } return res ; } public static void main ( String [ ] args ) { String str = "01010" ; System . out . println ( count ( str ) ) ; } }
import java . util . Vector ; class GFG { static int dfs ( int src , int dest , int [ ] vis , Vector < Integer > [ ] adj ) { vis [ src ] = 1 ; if ( src == dest ) { return 1 ; } for ( int u : adj [ src ] ) { if ( vis [ u ] == 0 ) { int temp = dfs ( u , dest , vis , adj ) ; if ( temp != 0 ) { return temp + 1 ; } } } return 0 ; } static int countNodes ( int V , int E , int src , int dest , int edges [ ] [ ] ) { Vector < Integer > [ ] adj = new Vector [ V + 1 ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; for ( int i = 0 ; i < E ; i ++ ) { adj [ edges [ i ] [ 0 ] ] . add ( edges [ i ] [ 1 ] ) ; adj [ edges [ i ] [ 1 ] ] . add ( edges [ i ] [ 0 ] ) ; } int vis [ ] = new int [ V + 1 ] ; int count = dfs ( src , dest , vis , adj ) ; return count - 2 ; } public static void main ( String [ ] args ) { int V = 8 , E = 7 ; int src = 5 , dest = 2 ; int edges [ ] [ ] = { { 1 , 4 } , { 4 , 5 } , { 4 , 2 } , { 2 , 6 } , { 6 , 3 } , { 2 , 7 } , { 3 , 8 } } ; System . out . print ( countNodes ( V , E , src , dest , edges ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static long GCD ( long a , long b ) { if ( a == 0 ) return b ; return GCD ( b % a , a ) ; } static long LCM ( long a , long b ) { return ( a * b ) / GCD ( a , b ) ; } static void checkPairwiseCoPrime ( int A [ ] , int n ) { long prod = 1 ; long lcm = 1 ; for ( int i = 0 ; i < n ; i ++ ) { prod *= A [ i ] ; lcm = LCM ( A [ i ] , lcm ) ; } if ( prod == lcm ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 5 } ; int n = A . length ; checkPairwiseCoPrime ( A , n ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int maxDiffSubsets ( int [ ] arr ) { int totalSum = 0 ; boolean pos = false , neg = false ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . length ; i ++ ) { totalSum += Math . abs ( arr [ i ] ) ; if ( arr [ i ] > 0 ) pos = true ; if ( arr [ i ] < 0 ) neg = true ; if ( arr [ i ] < min ) min = arr [ i ] ; } if ( pos && neg ) return totalSum ; else return totalSum - 2 * min ; } public static void main ( String [ ] args ) { int [ ] S = { 1 , 2 , 1 } ; int N = S . length ; if ( N < 2 ) System . out . println ( " Not ▁ Possible " ) ; else System . out . println ( maxDiffSubsets ( S ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int minDiffSubArray ( int arr [ ] , int n ) { int [ ] prefix_sum = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; int [ ] suffix_sum = new int [ n ] ; suffix_sum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix_sum [ i ] = suffix_sum [ i + 1 ] + arr [ i ] ; int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int diff = Math . abs ( prefix_sum [ i ] - suffix_sum [ i + 1 ] ) ; if ( diff < minDiff ) minDiff = diff ; } return minDiff ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 9 , 5 , 10 } ; int n = arr . length ; System . out . println ( minDiffSubArray ( arr , n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void dailyTemperatures ( int [ ] T ) { int n = T . length ; int [ ] daysOfWait = new int [ n ] ; Arrays . fill ( daysOfWait , - 1 ) ; Stack < Integer > s = new Stack < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! s . isEmpty ( ) && T [ s . peek ( ) ] < T [ i ] ) { daysOfWait [ s . peek ( ) ] = i - s . peek ( ) ; s . pop ( ) ; } s . push ( i ) ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( daysOfWait [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 73 , 74 , 75 , 71 , 69 , 72 , 76 , 73 } ; dailyTemperatures ( arr ) ; } }
import java . util . * ; class GFG { static int timeT = 0 ; static void dfs ( int u , int p , int dis , int vis [ ] , int distance [ ] , int parent [ ] , int preTime [ ] , int postTime [ ] , ArrayList < ArrayList < Integer > > Adj ) { distance [ u ] = dis ; parent [ u ] = p ; vis [ u ] = 1 ; timeT ++ ; preTime [ u ] = timeT ; for ( int i = 0 ; i < Adj . get ( u ) . size ( ) ; i ++ ) { if ( vis [ Adj . get ( u ) . get ( i ) ] == 0 ) { dfs ( Adj . get ( u ) . get ( i ) , u , dis + 1 , vis , distance , parent , preTime , postTime , Adj ) ; } } timeT ++ ; postTime [ u ] = timeT ; } static void addEdge ( ArrayList < ArrayList < Integer > > Adj , int u , int v ) { Adj . get ( u ) . add ( v ) ; Adj . get ( v ) . add ( u ) ; } static void findNodeU ( int N , int V , int Vertices [ ] , int Edges [ ] [ ] ) { int vis [ ] = new int [ N + 1 ] ; int distance [ ] = new int [ N + 1 ] ; int parent [ ] = new int [ N + 1 ] ; int preTime [ ] = new int [ N + 1 ] ; int postTime [ ] = new int [ N + 1 ] ; ArrayList < ArrayList < Integer > > Adj = new ArrayList < > ( ) ; for ( int i = 0 ; i < N + 1 ; i ++ ) Adj . add ( new ArrayList < Integer > ( ) ) ; int u = 0 , v ; for ( int i = 0 ; i < N - 1 ; i ++ ) { addEdge ( Adj , Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } dfs ( 1 , 0 , 0 , vis , distance , parent , preTime , postTime , Adj ) ; int maximumDistance = 0 ; maximumDistance = 0 ; for ( int k = 0 ; k < V ; k ++ ) { if ( maximumDistance < distance [ Vertices [ k ] ] ) { maximumDistance = distance [ Vertices [ k ] ] ; u = Vertices [ k ] ; } if ( parent [ Vertices [ k ] ] != 0 ) { Vertices [ k ] = parent [ Vertices [ k ] ] ; } } boolean ans = true ; boolean flag ; for ( int k = 0 ; k < V ; k ++ ) { if ( preTime [ Vertices [ k ] ] <= preTime [ u ] && postTime [ Vertices [ k ] ] >= postTime [ u ] ) flag = true ; else flag = false ; ans = ans & flag ; } if ( ans ) System . out . println ( u ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int N = 10 ; int V = 5 ; int Vertices [ ] = { 4 , 3 , 8 , 9 , 10 } ; int Edges [ ] [ ] = { { 1 , 2 } , { 1 , 3 } , { 1 , 4 } , { 2 , 5 } , { 2 , 6 } , { 3 , 7 } , { 7 , 8 } , { 7 , 9 } , { 9 , 10 } } ; findNodeU ( N , V , Vertices , Edges ) ; } }
import java . util . * ; class GFG { static int ans = 0 , allsum = 0 ; static Vector < Integer > [ ] edges = new Vector [ 100001 ] ; static void addedge ( int a , int b ) { edges [ a ] . add ( b ) ; edges [ b ] . add ( a ) ; } static void findCost ( int r , int p , int arr [ ] ) { int i , cur ; for ( i = 0 ; i < edges [ r ] . size ( ) ; i ++ ) { cur = edges [ r ] . get ( i ) ; if ( cur == p ) continue ; findCost ( cur , r , arr ) ; arr [ r ] += arr [ cur ] ; } int t1 = arr [ r ] ; int t2 = allsum - t1 ; if ( t1 * t2 > ans ) { ans = t1 * t2 ; } } static void maximumCost ( int r , int p , int N , int M , int arr [ ] , int Edges [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { allsum += arr [ i ] ; } for ( int i = 0 ; i < M ; i ++ ) { addedge ( Edges [ i ] [ 0 ] , Edges [ i ] [ 1 ] ) ; } findCost ( r , p , arr ) ; } public static void main ( String [ ] args ) { int a , b , N = 6 ; int arr [ ] = { 13 , 8 , 7 , 4 , 5 , 9 } ; int M = 5 ; int Edges [ ] [ ] = { { 0 , 1 } , { 1 , 2 } , { 1 , 4 } , { 3 , 4 } , { 4 , 5 } } ; for ( int i = 0 ; i < edges . length ; i ++ ) edges [ i ] = new Vector < Integer > ( ) ; maximumCost ( 1 , - 1 , N , M , arr , Edges ) ; System . out . print ( ans ) ; } }
import java . util . * ; import java . io . * ; class GFG { static int inf = ( int ) 1e18 ; static int minimumReplacement ( int [ ] arr , int N , int K ) { int ans = inf ; ArrayList < Integer > max_values = new ArrayList < > ( ) ; ArrayList < Integer > min_values = new ArrayList < > ( ) ; Map < Integer , Integer > sum_equal_to_x = new HashMap < > ( ) ; for ( int i = 0 ; i < N / 2 ; i ++ ) { int mn = Math . min ( arr [ i ] , arr [ N - i - 1 ] ) ; int mx = Math . max ( arr [ i ] , arr [ N - i - 1 ] ) ; sum_equal_to_x . put ( arr [ i ] + arr [ N - i - 1 ] , sum_equal_to_x . getOrDefault ( arr [ i ] + arr [ N - i - 1 ] , 0 ) + 1 ) ; min_values . add ( mn ) ; max_values . add ( mx ) ; } Collections . sort ( max_values ) ; Collections . sort ( min_values ) ; for ( int x = 2 ; x <= 2 * K ; x ++ ) { int mp1 = max_values . indexOf ( x - K ) ; int mp2 = min_values . indexOf ( x ) ; int rep2 = mp1 + ( N / 2 - mp2 ) ; int rep0 = sum_equal_to_x . getOrDefault ( x , - 1 ) ; int rep1 = ( N / 2 - rep2 - rep0 ) ; ans = Math . min ( ans , rep2 * 2 + rep1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 3 ; int arr [ ] = { 1 , 2 , 2 , 1 } ; System . out . print ( minimumReplacement ( arr , N , K ) ) ; } }
import java . util . * ; class GFG { static int maxsubStringLength ( char [ ] S , int N ) { int arr [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) if ( S [ i ] == ' a ' S [ i ] == ' e ' S [ i ] == ' i ' S [ i ] == ' o ' S [ i ] == ' u ' ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; int maxLen = 0 ; int curr_sum = 0 ; HashMap < Integer , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { curr_sum += arr [ i ] ; if ( curr_sum == 0 ) maxLen = Math . max ( maxLen , i + 1 ) ; if ( hash . containsKey ( curr_sum ) ) maxLen = Math . max ( maxLen , i - hash . get ( curr_sum ) ) ; else hash . put ( curr_sum , i ) ; } return maxLen ; } public static void main ( String [ ] args ) { String S = " geeksforgeeks " ; int n = S . length ( ) ; System . out . print ( maxsubStringLength ( S . toCharArray ( ) , n ) ) ; } }
import java . util . * ; class GFG { static HashMap < Integer , Boolean > mp = new HashMap < > ( ) ; static class Node { int key ; Node left , right ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . key = key ; temp . left = temp . right = null ; return ( temp ) ; } static boolean deleteNode ( int nodeVal ) { return mp . containsKey ( nodeVal ) ; } static Node treePruning ( Node root , Vector < Node > result ) { if ( root == null ) return null ; root . left = treePruning ( root . left , result ) ; root . right = treePruning ( root . right , result ) ; if ( deleteNode ( root . key ) ) { if ( root . left != null ) { result . add ( root . left ) ; } if ( root . right != null ) { result . add ( root . right ) ; } return null ; } return root ; } static void printInorderTree ( Node root ) { if ( root == null ) return ; printInorderTree ( root . left ) ; System . out . print ( root . key + " ▁ " ) ; printInorderTree ( root . right ) ; } static void printForests ( Node root , int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { mp . put ( arr [ i ] , true ) ; } Vector < Node > result = new Vector < > ( ) ; if ( treePruning ( root , result ) != null ) result . add ( root ) ; for ( int i = 0 ; i < result . size ( ) ; i ++ ) { printInorderTree ( result . get ( i ) ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 12 ) ; root . right = newNode ( 13 ) ; root . right . left = newNode ( 14 ) ; root . right . right = newNode ( 15 ) ; root . right . left . left = newNode ( 21 ) ; root . right . left . right = newNode ( 22 ) ; root . right . right . left = newNode ( 23 ) ; root . right . right . right = newNode ( 24 ) ; int arr [ ] = { 14 , 23 , 1 } ; int n = arr . length ; printForests ( root , arr , n ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxDistance ( int [ ] arr , int K ) { Map < Integer , Integer > map = new HashMap < > ( ) ; int maxDist = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ! map . containsKey ( arr [ i ] ) ) map . put ( arr [ i ] , i ) ; if ( map . containsKey ( arr [ i ] + K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] + K ) ) ; } if ( map . containsKey ( arr [ i ] - K ) ) { maxDist = Math . max ( maxDist , i - map . get ( arr [ i ] - K ) ) ; } } if ( maxDist == 0 ) return - 1 ; else return maxDist ; } public static void main ( String args [ ] ) { int [ ] arr = { 11 , 2 , 3 , 8 , 5 , 2 } ; int K = 2 ; System . out . println ( maxDistance ( arr , K ) ) ; } }
class GFG { static void solve ( int n ) { int ans = 0 ; for ( int y = n + 1 ; y <= n * n + n ; y ++ ) { if ( ( n * n ) % ( y - n ) == 0 ) { ans += 1 ; } } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int n = 5 ; solve ( n ) ; } }
class GFG { public static long reverse ( long num ) { long rev = 0 ; while ( num > 0 ) { int r = ( int ) ( num % 10 ) ; rev = rev * 10 + r ; num /= 10 ; } return rev ; } public static long count ( int N , int A , int B ) { long l = ( long ) Math . pow ( 10 , N - 1 ) , r = ( long ) Math . pow ( 10 , N ) - 1 ; if ( l == 1 ) l = 0 ; long ans = 0 ; for ( long i = l ; i <= r ; i ++ ) { int even_sum = 0 , odd_sum = 0 ; long itr = 0 , num = reverse ( i ) ; while ( num > 0 ) { if ( itr % 2 == 0 ) odd_sum += num % 10 ; else even_sum += num % 10 ; num /= 10 ; itr ++ ; } if ( even_sum % A == 0 && odd_sum % B == 0 ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , A = 5 , B = 3 ; System . out . println ( count ( N , A , B ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { private static List < List < Integer > > constructTree ( int n , int [ ] [ ] edges ) { List < List < Integer > > adjl = new ArrayList < List < Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { adjl . add ( new ArrayList < Integer > ( ) ) ; } for ( int [ ] e : edges ) { int u = e [ 0 ] ; int v = e [ 1 ] ; adjl . get ( u ) . add ( v ) ; adjl . get ( v ) . add ( u ) ; } return adjl ; } private static void DFS ( List < List < Integer > > adjl , int [ ] parent , int u , int p ) { parent [ u ] = p ; for ( int v : adjl . get ( u ) ) { if ( v != p ) { DFS ( adjl , parent , v , u ) ; } } } private static int [ ] valuesFromChildren ( int [ ] parent , int [ ] values ) { int [ ] valuesChildren = new int [ parent . length ] ; for ( int i = 0 ; i < parent . length ; i ++ ) { if ( parent [ i ] == - 1 ) continue ; else { int p = parent [ i ] ; valuesChildren [ p ] += values [ i ] ; } } return valuesChildren ; } private static int findSumOfValues ( int v , int [ ] parent , int [ ] valuesChildren ) { int cur_node = v ; int sum = 0 ; while ( cur_node != - 1 ) { sum += valuesChildren [ cur_node ] ; cur_node = parent [ cur_node ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 8 ; int [ ] [ ] edges = { { 0 , 1 } , { 0 , 2 } , { 0 , 3 } , { 1 , 4 } , { 1 , 5 } , { 4 , 7 } , { 3 , 6 } } ; int v = 7 ; int [ ] values = new int [ ] { 1 , 2 , 3 , 0 , 0 , 4 , 3 , 6 } ; List < List < Integer > > adjl = constructTree ( n , edges ) ; int [ ] parent = new int [ n ] ; DFS ( adjl , parent , 0 , - 1 ) ; int [ ] valuesChildren = valuesFromChildren ( parent , values ) ; int sum = findSumOfValues ( v , parent , valuesChildren ) ; sum += values [ 0 ] ; System . out . println ( sum ) ; } }
class GFG { static boolean isVowel ( char c ) { if ( c == ' a ' c == ' e ' c == ' i ' c == ' o ' c == ' u ' ) return true ; return false ; } static void countSubstrings ( String s , int n ) { int temp [ ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum += ( isVowel ( s . charAt ( i ) ) ? 1 : 0 ) ; sum %= 2 ; temp [ sum ] ++ ; } result += ( ( temp [ 0 ] * ( temp [ 0 ] - 1 ) ) / 2 ) ; result += ( ( temp [ 1 ] * ( temp [ 1 ] - 1 ) ) / 2 ) ; System . out . println ( result ) ; } public static void main ( String [ ] args ) { int n = 5 ; String s = " abcde " ; countSubstrings ( s , n ) ; } }
import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class pair { Node first ; int second ; pair ( Node first , int second ) { this . first = first ; this . second = second ; } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static int findLevel ( Node root , int k , int level ) { Queue < pair > qt = new LinkedList < > ( ) ; qt . add ( new pair ( root , 0 ) ) ; int count = 1 , b = 0 , a = 0 ; while ( ! qt . isEmpty ( ) ) { pair temp = qt . peek ( ) ; qt . poll ( ) ; if ( count == 1 ) { b = temp . second ; } if ( temp . first . left != null ) { qt . add ( new pair ( temp . first . left , 2 * temp . second ) ) ; } if ( temp . first . right != null ) { qt . add ( new pair ( temp . first . right , 2 * temp . second + 1 ) ) ; } count -- ; if ( count == 0 ) { if ( ( b - a + 1 ) == k ) return level ; pair secondLabel = qt . peek ( ) ; a = secondLabel . second ; level += 1 ; count = qt . size ( ) ; } } return - 1 ; } public static void main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 2 ) ; root . right . right = newNode ( 8 ) ; root . left . left = newNode ( 7 ) ; root . left . left . left = newNode ( 5 ) ; root . left . right = newNode ( 3 ) ; root . left . right . right = newNode ( 4 ) ; int k = 4 ; System . out . println ( findLevel ( root , k , 1 ) ) ; } }
import java . util . * ; class GFG { static boolean possible ( int arr [ ] , int N , int mid , int K ) { int add = 0 ; for ( int i = N / 2 - ( N + 1 ) % 2 ; i < N ; ++ i ) { if ( mid - arr [ i ] > 0 ) { add += ( mid - arr [ i ] ) ; if ( add > K ) return false ; } } if ( add <= K ) return true ; else return false ; } static int findMaxMedian ( int arr [ ] , int N , int K ) { int low = 1 ; int mx = 0 ; for ( int i = 0 ; i < N ; ++ i ) { mx = Math . max ( mx , arr [ i ] ) ; } int high = K + mx ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( possible ( arr , N , mid , K ) ) { low = mid + 1 ; } else { high = mid - 1 ; } } if ( N % 2 == 0 ) { if ( low - 1 < arr [ N / 2 ] ) { return ( arr [ N / 2 ] + low - 1 ) / 2 ; } } return low - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 6 } ; int K = 10 ; int N = arr . length ; Arrays . sort ( arr ) ; System . out . println ( findMaxMedian ( arr , N , K ) ) ; } }
class GFG { public static boolean countLessThanMid ( int mid , int N , int M , int K ) { int count = 0 ; for ( int i = 1 ; i <= Math . min ( N , mid ) ; ++ i ) { count = count + Math . min ( mid / i , M ) ; } if ( count >= K ) return false ; else return true ; } public static int findKthElement ( int N , int M , int K ) { int low = 1 , high = N * M ; while ( low <= high ) { int mid = low + ( high - low ) / 2 ; if ( countLessThanMid ( mid , N , M , K ) ) low = mid + 1 ; else high = mid - 1 ; } return high + 1 ; } public static void main ( String [ ] args ) { int N = 2 , M = 3 ; int K = 5 ; System . out . println ( findKthElement ( N , M , K ) ) ; } }
import java . io . * ; class GFG { public static int minOperations ( String S , int K ) { int ans = 0 ; for ( int i = 0 ; i < K ; i ++ ) { int zero = 0 , one = 0 ; for ( int j = i ; j < S . length ( ) ; j += K ) { if ( S . charAt ( j ) == '0' ) zero ++ ; else one ++ ; } ans += Math . min ( zero , one ) ; } return ans ; } public static void main ( String args [ ] ) { String S = "110100101" ; int K = 3 ; System . out . println ( minOperations ( S , K ) ) ; } }
import java . util . * ; class GFG { static void countSubString ( String S , char C ) { int count = 0 ; int conCount = 0 ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { char ch = S . charAt ( i ) ; if ( ch == C ) conCount ++ ; else { count += ( conCount * ( conCount + 1 ) ) / 2 ; conCount = 0 ; } } count += ( conCount * ( conCount + 1 ) ) / 2 ; System . out . println ( count ) ; } public static void main ( String s [ ] ) { String S = " geeksforgeeks " ; char C = ' e ' ; countSubString ( S , C ) ; } }
import java . util . * ; class GFG { static int getCount ( int N , List < String > s ) { Map < List < Integer > , Integer > mp = new HashMap < > ( ) ; int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { List < Integer > a = new ArrayList < > ( 26 ) ; for ( int k = 0 ; k < 26 ; k ++ ) a . add ( 0 ) ; for ( int j = 0 ; j < s . get ( i ) . length ( ) ; j ++ ) { a . set ( ( s . get ( i ) . charAt ( j ) - ' a ' ) , a . get ( s . get ( i ) . charAt ( j ) - ' a ' ) + 1 ) ; } for ( int j = 0 ; j < 26 ; j ++ ) { a . set ( j , a . get ( j ) % 2 ) ; } ans += mp . getOrDefault ( a , 0 ) ; for ( int j = 0 ; j < 26 ; j ++ ) { List < Integer > changedCount = new ArrayList < > ( ) ; changedCount . addAll ( a ) ; if ( a . get ( j ) == 0 ) changedCount . set ( j , 1 ) ; else changedCount . set ( j , 0 ) ; ans += mp . getOrDefault ( changedCount , 0 ) ; } mp . put ( a , mp . getOrDefault ( a , 0 ) + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 6 ; List < String > A = Arrays . asList ( " aab " , " abcac " , " dffe " , " ed " , " aa " , " aade " ) ; System . out . print ( getCount ( N , A ) ) ; } }
import java . util . * ; class GFG { static class Node { int key ; Node left , right ; } ; static int kth_smallest ; public static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static boolean KSmallestPerfectBST ( Node root , int k , int treeSize ) { if ( root == null ) return false ; int median_loc = ( treeSize / 2 ) + 1 ; if ( k == median_loc ) { kth_smallest = root . key ; return true ; } int newTreeSize = treeSize / 2 ; if ( k < median_loc ) { return KSmallestPerfectBST ( root . left , k , newTreeSize ) ; } int newK = k - median_loc ; return KSmallestPerfectBST ( root . right , newK , newTreeSize ) ; } public static void main ( String [ ] args ) { Node root = null ; root = insert ( root , 50 ) ; insert ( root , 30 ) ; insert ( root , 20 ) ; insert ( root , 40 ) ; insert ( root , 70 ) ; insert ( root , 60 ) ; insert ( root , 80 ) ; insert ( root , 14 ) ; insert ( root , 25 ) ; insert ( root , 35 ) ; insert ( root , 45 ) ; insert ( root , 55 ) ; insert ( root , 65 ) ; insert ( root , 75 ) ; insert ( root , 85 ) ; int n = 15 , k = 5 ; if ( KSmallestPerfectBST ( root , k , n ) ) { System . out . print ( kth_smallest + " ▁ " ) ; } } }
import java . util . * ; class GFG { static boolean checkrules ( String s ) { if ( s . length ( ) == 0 ) return true ; if ( s . charAt ( 0 ) != '1' ) return false ; if ( s . length ( ) > 2 ) { if ( s . charAt ( 1 ) == '0' && s . charAt ( 2 ) == '0' ) return checkrules ( s . substring ( 3 ) ) ; } return checkrules ( s . substring ( 1 ) ) ; } public static void main ( String [ ] args ) { String str = "1111" ; if ( checkrules ( str ) ) { System . out . print ( " Valid ▁ String " ) ; } else { System . out . print ( " Invalid ▁ String " ) ; } } }
class GFG { static boolean isNudeNum ( int n ) { int copy , length , flag = 0 ; copy = n ; String temp ; temp = String . valueOf ( copy ) ; length = temp . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { int num = temp . charAt ( i ) - '0' ; if ( num == 0 n % num != 0 ) { flag = 1 ; } } if ( flag == 1 ) return false ; else return true ; } static int longestNudeSubseq ( int arr [ ] , int n ) { int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isNudeNum ( arr [ i ] ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 34 , 34 , 2 , 2 , 3 , 333 , 221 , 32 } ; int n = arr . length ; System . out . print ( longestNudeSubseq ( arr , n ) + "NEW_LINE"); } }
class GFG { public static int longestSubarray ( int [ ] a , int n ) { int [ ] l = new int [ n ] ; int [ ] r = new int [ n ] ; for ( int i = 0 , count = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; else { l [ i ] = count ; count = 0 ; } } for ( int i = n - 1 , count = 0 ; i >= 0 ; i -- ) { if ( a [ i ] == 1 ) count ++ ; else { r [ i ] = count ; count = 0 ; } } int ans = - 1 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] == 0 ) ans = Math . max ( ans , l [ i ] + r [ i ] ) ; } return ans < 0 ? n : ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 1 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( longestSubarray ( arr , n ) ) ; } }
import java . util . Arrays ; class GFG { static void maxSide ( int a [ ] , int n ) { int sideLength = 0 ; Arrays . sort ( a ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( a [ i ] > sideLength ) { sideLength ++ ; } else { break ; } } System . out . println ( sideLength ) ; } public static void main ( String [ ] args ) { int N = 6 ; int arr [ ] = new int [ ] { 3 , 2 , 1 , 5 , 2 , 4 } ; maxSide ( arr , N ) ; } }
import java . util . * ; class GFG { public static void minDiff ( long [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) a [ i ] *= 2 ; } Arrays . sort ( a ) ; long mindifference = a [ 1 ] - a [ 0 ] ; for ( int i = 1 ; i < a . length ; i ++ ) { mindifference = Math . min ( mindifference , a [ i ] - a [ i - 1 ] ) ; } System . out . println ( mindifference ) ; } public static void main ( String [ ] args ) { long [ ] arr = { 3 , 8 , 13 , 30 , 50 } ; int n = arr . length ; minDiff ( arr , n ) ; } }
import java . util . * ; class GFG { static Vector < Integer > v = new Vector < Integer > ( ) ; static int bin ( int val , int k , int n ) { int lo = 0 ; int hi = n ; int mid ; int ans = - 1 ; while ( lo <= hi ) { mid = lo + ( hi - lo ) / 2 ; if ( v . get ( mid ) - val <= k ) { lo = mid + 1 ; ans = mid ; } else hi = mid - 1 ; } return ans ; } static void findSubarraySumK ( int arr [ ] , int N , int K ) { int sum = 0 ; v . add ( 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; v . add ( sum ) ; } int l = 0 , ans = 0 , r ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { r = bin ( v . get ( i ) , K , N ) ; ans = Math . max ( ans , r - i ) ; } System . out . print ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 8 , 14 , 9 , 4 , 11 , 10 } ; int N = arr . length ; int K = 13 ; findSubarraySumK ( arr , N , K ) ; } }
class GFG { static int missingElement ( int arr [ ] , int n ) { int max_ele = arr [ 0 ] ; int min_ele = arr [ 0 ] ; int x = 0 ; int d ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_ele ) max_ele = arr [ i ] ; if ( arr [ i ] < min_ele ) min_ele = arr [ i ] ; } d = ( max_ele - min_ele ) / n ; for ( int i = 0 ; i < n ; i ++ ) { x = x ^ arr [ i ] ; } for ( int i = 0 ; i <= n ; i ++ ) { x = x ^ ( min_ele + ( i * d ) ) ; } return x ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 12 , 3 , 6 , 15 , 18 } ; int n = arr . length ; int element = missingElement ( arr , n ) ; System . out . print ( element ) ; } }
import java . util . * ; class GFG { static int splitArray ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int result = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . length ; System . out . print ( splitArray ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int findValue ( int R , int C ) { int k = ( R * ( R - 1 ) ) / 2 + 1 ; int diff = R + 1 ; for ( int i = 1 ; i < C ; i ++ ) { k = ( k + diff ) ; diff ++ ; } return k ; } public static void main ( String [ ] args ) { int R = 4 ; int C = 4 ; int k = findValue ( R , C ) ; System . out . println ( k ) ; } }
import java . util . * ; class GFG { static final int N = 3 ; static final int M = 4 ; static int FindColumn ( int mat [ ] [ ] ) { int row = 0 , col = M - 1 ; int flag = 0 ; while ( row < N && col >= 0 ) { if ( mat [ row ] [ col ] == 1 ) { col -- ; flag = 1 ; } else { row ++ ; } } col ++ ; if ( flag != 0 ) return col + 1 ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 0 , 0 , 1 , 1 } } ; System . out . print ( FindColumn ( mat ) ) ; } }
import java . util . * ; class GFG { static void findNumbers ( int N , int M ) { int m = M ; Map < Integer , Integer > remLen = new HashMap < > ( ) ; int len , remainder = 0 ; for ( len = 1 ; len <= N + 1 ; ++ len ) { remainder = M % N ; if ( ! remLen . containsKey ( remainder ) ) { remLen . put ( remainder , len ) ; } else { break ; } M = M * 10 + m ; M = M % N ; } int LenA = len ; int LenB = remLen . getOrDefault ( remainder , 0 ) ; for ( int i = 0 ; i < LenB ; ++ i ) System . out . print ( m ) ; System . out . print ( " ▁ " ) ; for ( int i = 0 ; i < LenA ; ++ i ) System . out . print ( m ) ; } public static void main ( String [ ] args ) { int N = 8 , M = 2 ; findNumbers ( N , M ) ; } }
import java . util . * ; class GFG { static String findSmallest ( char [ ] s ) { int len = s . length ; int [ ] loccur = new int [ 26 ] ; Arrays . fill ( loccur , - 1 ) ; for ( int i = len - 1 ; i >= 0 ; -- i ) { int chI = s [ i ] - ' a ' ; if ( loccur [ chI ] == - 1 ) { loccur [ chI ] = i ; } } char [ ] sorted_s = s ; Arrays . sort ( sorted_s ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( s [ i ] != sorted_s [ i ] ) { int chI = sorted_s [ i ] - ' a ' ; int last_occ = loccur [ chI ] ; char temp = s [ last_occ ] ; s [ last_occ ] = s [ i ] ; s [ i ] = temp ; break ; } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = " geeks " ; System . out . print ( findSmallest ( s . toCharArray ( ) ) ) ; } }
class GFG { static int segment_tree [ ] = new int [ 4 * 1000000 ] ; static int build ( int A [ ] , int start , int end , int node ) { if ( start == end ) segment_tree [ node ] = A [ start ] ; else { int mid = ( start + end ) / 2 ; segment_tree [ node ] = Math . max ( build ( A , start , mid , 2 * node + 1 ) , build ( A , mid + 1 , end , 2 * node + 2 ) ) ; } return segment_tree [ node ] ; } static int query ( int start , int end , int l , int r , int node ) { if ( start > r end < l ) return - 1 ; if ( start >= l && end <= r ) return segment_tree [ node ] ; int mid = ( start + end ) / 2 ; return Math . max ( query ( start , mid , l , r , 2 * node + 1 ) , query ( mid + 1 , end , l , r , 2 * node + 2 ) ) ; } static int longestSubArray ( int A [ ] , int N , int K ) { int res = 1 ; int preSum [ ] = new int [ N + 1 ] ; preSum [ 0 ] = A [ 0 ] ; for ( int i = 0 ; i < N ; i ++ ) preSum [ i + 1 ] = preSum [ i ] + A [ i ] ; build ( A , 0 , N - 1 , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) { int start = i , end = N - 1 , mid , max_index = i ; while ( start <= end ) { mid = ( start + end ) / 2 ; int max_element = query ( 0 , N - 1 , i , mid , 0 ) ; int expected_sum = ( mid - i + 1 ) * max_element ; int actual_sum = preSum [ mid + 1 ] - preSum [ i ] ; if ( expected_sum - actual_sum <= K ) { start = mid + 1 ; max_index = Math . max ( max_index , mid ) ; } else { end = mid - 1 ; } } res = Math . max ( res , max_index - i + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 4 , 6 , 7 } , K = 6 ; int N = arr . length ; System . out . println ( longestSubArray ( arr , N , K ) ) ; } }
class GFG { static int countNums ( int l , int r ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { int lastDigit = ( i % 10 ) ; if ( ( lastDigit % 10 ) == 2 || ( lastDigit % 10 ) == 3 || ( lastDigit % 10 ) == 9 ) { cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int l = 11 , r = 33 ; System . out . print ( countNums ( l , r ) ) ; } }
class GFG { static int findMinimumK ( int a [ ] , int n , int s ) { int maximum = a [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) { maximum = Math . max ( maximum , a [ i ] ) ; } int low = 1 , high = maximum + 1 ; int ans = high ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( int ) ( a [ i ] / mid ) ; } if ( sum > s ) low = mid + 1 ; else { ans = Math . min ( ans , mid ) ; high = mid - 1 ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . length ; int s = 27 ; System . out . println ( findMinimumK ( a , n , s ) ) ; } }
class GFG { static int [ ] [ ] SparseTable = new int [ 100001 ] [ 51 ] ; static void buildSparseTable ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { SparseTable [ i ] [ 0 ] = a [ i ] ; } for ( int j = 1 ; j <= 19 ; j ++ ) { for ( int i = 0 ; i <= n - ( 1 << j ) ; i ++ ) { SparseTable [ i ] [ j ] = __gcd ( SparseTable [ i ] [ j - 1 ] , SparseTable [ i + ( 1 << ( j - 1 ) ) ] [ j - 1 ] ) ; } } } static int queryForGCD ( int L , int R ) { int returnValue ; int j = ( int ) ( Math . log ( R - L + 1 ) ) ; returnValue = __gcd ( SparseTable [ L ] [ j ] , SparseTable [ R - ( 1 << j ) + 1 ] [ j ] ) ; return returnValue ; } static int nextPosition ( int tempGCD , int startPointer , int prevEndPointer , int n ) { int high = n - 1 ; int low = prevEndPointer ; int mid = prevEndPointer ; int nextPos = prevEndPointer ; while ( high >= low ) { mid = ( ( high + low ) >> 1 ) ; if ( queryForGCD ( startPointer , mid ) == tempGCD ) { nextPos = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } return nextPos + 1 ; } static int calculateSum ( int a [ ] , int n ) { buildSparseTable ( a , n ) ; int endPointer , startPointer , prevEndPointer , tempGCD ; int tempAns = 0 ; for ( int i = 0 ; i < n ; i ++ ) { endPointer = i ; startPointer = i ; prevEndPointer = i ; tempGCD = a [ i ] ; while ( endPointer < n ) { endPointer = nextPosition ( tempGCD , startPointer , prevEndPointer , n ) ; tempAns += ( ( endPointer - prevEndPointer ) * tempGCD ) ; prevEndPointer = endPointer ; if ( endPointer < n ) { tempGCD = __gcd ( tempGCD , a [ endPointer ] ) ; } } } return tempAns ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 6 ; int a [ ] = { 2 , 2 , 2 , 3 , 5 , 5 } ; System . out . println ( calculateSum ( a , n ) ) ; } }
class GFG { static int partition ( int arr [ ] , int low , int high ) { int temp ; int pivot = arr [ high ] ; int i = ( low - 1 ) ; for ( int j = low ; j <= high - 1 ; j ++ ) { if ( arr [ j ] <= pivot ) { i ++ ; temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } temp = arr [ i + 1 ] ; arr [ i + 1 ] = arr [ high ] ; arr [ high ] = temp ; return ( i + 1 ) ; } static int kthSmallest ( int a [ ] , int left , int right , int k ) { while ( left <= right ) { int pivotIndex = partition ( a , left , right ) ; if ( pivotIndex == k - 1 ) return a [ pivotIndex ] ; else if ( pivotIndex > k - 1 ) right = pivotIndex - 1 ; else left = pivotIndex + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 4 , 5 , 8 , 11 , 6 , 26 } ; int n = arr . length ; int k = 5 ; System . out . println ( " K - th ▁ smallest ▁ element ▁ is ▁ " + kthSmallest ( arr , 0 , n - 1 , k ) ) ; } }
import java . util . Arrays ; class GFG { static void Max_Sum ( int arr [ ] , int n , int k ) { int p = n ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= k ) { p = i ; break ; } } int maxsum = 0 , a = 0 , b = 0 ; for ( int i = 0 ; i < p ; i ++ ) { for ( int j = i + 1 ; j < p ; j ++ ) { if ( arr [ i ] + arr [ j ] < k && arr [ i ] + arr [ j ] > maxsum ) { maxsum = arr [ i ] + arr [ j ] ; a = arr [ i ] ; b = arr [ j ] ; } } } System . out . print ( a + " ▁ " + b ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 20 , 110 , 100 , 10 } ; int k = 85 ; int n = arr . length ; Max_Sum ( arr , n , k ) ; } }
class GFG { static boolean isPalindrome ( String str , int i , int j ) { while ( i < j ) { if ( str . charAt ( i ) != str . charAt ( j ) ) { return false ; } i ++ ; j -- ; } return true ; } static int maxLenPalindrome ( String str , int n , char ch ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ch ) { for ( int j = n - 1 ; j >= i ; j -- ) { if ( str . charAt ( j ) == ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } public static void main ( String [ ] args ) { String str = " lapqooqpqpl " ; int n = str . length ( ) ; char ch = ' p ' ; System . out . println ( maxLenPalindrome ( str , n , ch ) ) ; } }
import java . util . * ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . valueOf ( s ) ; } static boolean isPossible ( Vector < String > v , String str ) { str = countingsort ( str . toCharArray ( ) ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { String temp = v . get ( i ) + v . get ( j ) ; temp = countingsort ( temp . toCharArray ( ) ) ; if ( temp . equals ( str ) ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; Vector < String > v = new Vector < String > ( Arrays . asList ( arr ) ) ; if ( isPossible ( v , str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
public class GFG { static final int MAX = 256 ; static int freq [ ] [ ] ; static void preCalculate ( String str , int n ) { freq [ ( int ) str . charAt ( 0 ) ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str . charAt ( i ) ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } static int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } static String firstNonRepeating ( String str , int n , int l , int r ) { for ( int i = l ; i < r ; i ++ ) { char ch = str . charAt ( i ) ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ ] [ ] = { { 0 , 3 } , { 2 , 3 } , { 5 , 12 } } ; int q = queries . length ; freq = new int [ MAX ] [ n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( firstNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } } }
import java . util . * ; class GFG { static int maxLength ( String s , int n ) { int ans = Integer . MIN_VALUE ; Vector < Integer > A = new Vector < Integer > ( ) ; Vector < Integer > L = new Vector < Integer > ( ) ; Vector < Integer > R = new Vector < Integer > ( ) ; int [ ] freq = new int [ n + 5 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( s . charAt ( j ) - ' a ' == i ) count ++ ; freq [ j ] = count ; } for ( int j = 1 ; j < n ; j ++ ) { L . add ( ( 2 * freq [ j - 1 ] ) - j ) ; R . add ( ( 2 * freq [ j ] ) - j ) ; } int max_len = Integer . MIN_VALUE ; int min_val = Integer . MAX_VALUE ; for ( int j = 0 ; j < L . size ( ) ; j ++ ) { min_val = Math . min ( min_val , L . get ( j ) ) ; A . add ( min_val ) ; int l = 0 , r = j ; while ( l <= r ) { int mid = ( l + r ) >> 1 ; if ( A . get ( mid ) <= R . get ( j ) ) { max_len = Math . max ( max_len , j - mid + 1 ) ; r = mid - 1 ; } else { l = mid + 1 ; } } } ans = Math . max ( ans , max_len ) ; A . clear ( ) ; R . clear ( ) ; L . clear ( ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " ababbbacbcbcca " ; int n = s . length ( ) ; System . out . println ( maxLength ( s , n ) ) ; } }
class GFG { static void Printksubstring ( String str , int n , int k ) { int total = ( n * ( n + 1 ) ) / 2 ; if ( k > total ) { System . out . printf ( "-1NEW_LINE"); return ; } int substring [ ] = new int [ n + 1 ] ; substring [ 0 ] = 0 ; int temp = n ; for ( int i = 1 ; i <= n ; i ++ ) { substring [ i ] = substring [ i - 1 ] + temp ; temp -- ; } int l = 1 ; int h = n ; int start = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( substring [ m ] > k ) { start = m ; h = m - 1 ; } else if ( substring [ m ] < k ) { l = m + 1 ; } else { start = m ; break ; } } int end = n - ( substring [ start ] - k ) ; for ( int i = start - 1 ; i < end ; i ++ ) { System . out . print ( str . charAt ( i ) ) ; } } public static void main ( String [ ] args ) { String str = " abc " ; int k = 4 ; int n = str . length ( ) ; Printksubstring ( str , n , k ) ; } }
class GFG { static int maxSum ( int arr [ ] , int n , int K ) { int maximum = Integer . MIN_VALUE ; int [ ] sum = new int [ n ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i + K < n ) sum [ i ] = sum [ i + K ] + arr [ i ] ; else sum [ i ] = arr [ i ] ; maximum = Math . max ( maximum , sum [ i ] ) ; } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 4 , 7 , 2 } ; int n = arr . length ; int K = 2 ; System . out . print ( maxSum ( arr , n , K ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( String s1 , int n1 , String s2 , int n2 ) { int [ ] freq1 = new int [ 26 ] ; int [ ] freq2 = new int [ 26 ] ; Arrays . fill ( freq1 , 0 ) ; Arrays . fill ( freq2 , 0 ) ; int i , count = 0 ; for ( i = 0 ; i < n1 ; i ++ ) freq1 [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( i = 0 ; i < n2 ; i ++ ) freq2 [ s2 . charAt ( i ) - ' a ' ] ++ ; for ( i = 0 ; i < 26 ; i ++ ) count += ( Math . min ( freq1 [ i ] , freq2 [ i ] ) ) ; return count ; } public static void main ( String args [ ] ) { String s1 = " geeksforgeeks " , s2 = " platformforgeeks " ; int n1 = s1 . length ( ) , n2 = s2 . length ( ) ; System . out . println ( countPairs ( s1 , n1 , s2 , n2 ) ) ; } }
class GFG { static void findpair ( int l , int r ) { int c = 0 ; for ( int i = l ; i <= r ; i ++ ) { for ( int j = i + 1 ; j <= r ; j ++ ) { if ( j % i == 0 && j != i ) { System . out . println ( i + " , ▁ " + j ) ; c = 1 ; break ; } } if ( c == 1 ) break ; } } public static void main ( String args [ ] ) { int l = 1 , r = 10 ; findpair ( l , r ) ; } }
import java . util . * ; class GFG { static boolean check ( int arr [ ] , int N , int K ) { HashSet < Integer > unique = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) unique . add ( arr [ i ] ) ; if ( unique . size ( ) == K ) return true ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 } ; int N = arr . length ; int K = 3 ; if ( check ( arr , N , K ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { static int minIndex ( int arr [ ] , int n , int pos ) { int num = arr [ pos ] ; int i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 1 , 5 , 2 } ; int n = arr . length ; int pos = 4 ; System . out . println ( minIndex ( arr , n , pos ) ) ; } }
class GFG { static int minIndex ( int arr [ ] , int pos ) { int low = 0 ; int high = pos ; int i = pos ; while ( low < high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] != arr [ pos ] ) { low = mid + 1 ; } else { high = mid - 1 ; i = mid ; if ( mid > 0 && arr [ mid - 1 ] != arr [ pos ] ) { break ; } } } return arr [ low ] == arr [ pos ] ? low : i ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 1 , 5 , 2 } ; } }
import java . util . * ; class GFG { static int findAnswer ( String str1 , String str2 , int n ) { int l = 0 , r = 0 ; int ans = 2 ; for ( int i = 0 ; i < n ; ++ i ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { l = i ; break ; } } for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( str1 . charAt ( i ) != str2 . charAt ( i ) ) { r = i ; break ; } } if ( r < l ) return 26 * ( n + 1 ) ; else if ( l == r ) return ans ; else { for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( i - 1 ) ) { ans -- ; break ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { if ( str1 . charAt ( i - 1 ) != str2 . charAt ( i ) ) { ans -- ; break ; } } return ans ; } } public static void main ( String args [ ] ) { String str1 = " toy " , str2 = " try " ; int n = str1 . length ( ) ; System . out . println ( findAnswer ( str1 , str2 , n ) ) ; } }
import java . util . * ; class solution { static int findMinDifference ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int diff1 = arr [ n - 1 ] - arr [ 1 ] ; int diff2 = arr [ n - 2 ] - arr [ 0 ] ; return Math . min ( diff1 , diff2 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . length ; System . out . print ( findMinDifference ( arr , n ) ) ; } }
public class GFG { static int findMinDifference ( int arr [ ] , int n ) { int min , secondMin , max , secondMax ; min = secondMax = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 0 ] : arr [ 1 ] ; max = secondMin = ( arr [ 0 ] < arr [ 1 ] ) ? arr [ 1 ] : arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } else if ( arr [ i ] < min ) { secondMin = min ; min = arr [ i ] ; } else if ( arr [ i ] < secondMin ) { secondMin = arr [ i ] ; } } int diff = Math . min ( max - secondMin , secondMax - min ) ; return diff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMinDifference ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int smallest_pair ( int [ ] a , int n ) { int min = Integer . MAX_VALUE , secondMin = Integer . MAX_VALUE ; for ( int j = 0 ; j < n ; j ++ ) { if ( a [ j ] < min ) { secondMin = min ; min = a [ j ] ; } else if ( ( a [ j ] < secondMin ) && a [ j ] != min ) secondMin = a [ j ] ; } return ( secondMin + min ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( smallest_pair ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int longestsubarray ( int arr [ ] , int n , int k ) { int current_count = 0 ; int max_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) current_count ++ ; else current_count = 0 ; max_count = Math . max ( current_count , max_count ) ; } return max_count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 11 , 32 , 64 , 88 } ; int n = arr . length ; int k = 8 ; System . out . println ( longestsubarray ( arr , n , k ) ) ; } }
import java . util . HashMap ; class geeks { public static void removeElements ( int [ ] arr , int n , int k ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( ! mp . containsKey ( arr [ i ] ) ) mp . put ( arr [ i ] , 1 ) ; else { int x = mp . get ( arr [ i ] ) ; mp . put ( arr [ i ] , ++ x ) ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) >= k ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; removeElements ( arr , n , k ) ; } }
import java . util . HashMap ; import java . util . Map ; class GFG { static void RemoveElements ( int arr [ ] , int n , int k ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . containsKey ( arr [ i ] ) && mp . get ( arr [ i ] ) <= k ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; RemoveElements ( arr , n , k ) ; } }
import java . util . * ; class GFG { static int majorityInMatrix ( int arr [ ] [ ] ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . containsKey ( arr [ i ] [ j ] ) ) mp . put ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) ; else mp . put ( arr [ i ] [ j ] , 1 ) ; } } int countMajority = 0 ; Iterator < HashMap . Entry < Integer , Integer > > itr = mp . entrySet ( ) . iterator ( ) ; while ( itr . hasNext ( ) ) { HashMap . Entry < Integer , Integer > entry = itr . next ( ) ; if ( entry . getValue ( ) >= ( ( N * M ) / 2 ) ) { countMajority ++ ; } } return countMajority ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 2 } , { 1 , 3 , 2 } , { 1 , 2 , 6 } } ; System . out . println ( majorityInMatrix ( mat ) ) ; } }
import java . io . * ; class GFG { static int search ( int ar [ ] , int size ) { int a = 0 , b = size - 1 ; int mid = 0 ; while ( ( b - a ) > 1 ) { mid = ( a + b ) / 2 ; if ( ( ar [ a ] - a ) != ( ar [ mid ] - mid ) ) b = mid ; else if ( ( ar [ b ] - b ) != ( ar [ mid ] - mid ) ) a = mid ; } return ( ar [ a ] + 1 ) ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 8 } ; int size = ar . length ; System . out . println ( " Missing ▁ number : ▁ " + search ( ar , size ) ) ; } }
class GFG { static int deleteElement ( int A [ ] , int L , int R , int N ) { int i , j = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( i <= L i >= R ) { A [ j ] = A [ i ] ; j ++ ; } } return j ; } public static void main ( String args [ ] ) { int A [ ] = new int [ ] { 5 , 8 , 11 , 15 , 26 , 14 , 19 , 17 , 10 , 14 } ; int L = 2 , R = 7 ; int n = A . length ; int res_size = deleteElement ( A , L , R , n ) ; for ( int i = 0 ; i < res_size ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static int findFirst ( int arr [ ] , int n , int x ) { Arrays . sort ( arr ) ; int ptr = lowerBound ( arr , 0 , n , x ) ; return ( arr [ ptr ] != x ) ? - 1 : ( ptr ) ; } static int lowerBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void main ( String [ ] args ) { int x = 20 , arr [ ] = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . length ; System . out . println ( findFirst ( arr , n , x ) ) ; } }
public class GFG { static int findFirst ( int arr [ ] , int n , int x ) { int count = 0 ; boolean isX = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) { isX = true ; } else if ( arr [ i ] < x ) { count ++ ; } } return ( isX == false ) ? - 1 : count ; } public static void main ( String [ ] args ) { int x = 20 , arr [ ] = { 10 , 30 , 20 , 50 , 20 } ; int n = arr . length ; System . out . println ( findFirst ( arr , n , x ) ) ; } }
import java . util . * ; class GFG { public static int findDuplicate ( int [ ] arr ) { int slow = arr [ 0 ] ; int fast = arr [ 0 ] ; do { slow = arr [ slow ] ; fast = arr [ arr [ fast ] ] ; } while ( slow != fast ) ; int ptr1 = arr [ 0 ] ; int ptr2 = slow ; while ( ptr1 != ptr2 ) { ptr1 = arr [ ptr1 ] ; ptr2 = arr [ ptr2 ] ; } return ptr1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 2 , 1 } ; System . out . println ( " " + findDuplicate ( arr ) ) ; System . exit ( 0 ) ; } }
public class GFG { final static int MAX_CHAR = 26 ; static void printGreaterCount ( String str ) { int len = str . length ( ) ; int ans [ ] = new int [ len ] , count [ ] = new int [ MAX_CHAR ] ; for ( int i = len - 1 ; i >= 0 ; i -- ) { count [ str . charAt ( i ) - ' a ' ] ++ ; for ( int j = str . charAt ( i ) - ' a ' + 1 ; j < MAX_CHAR ; j ++ ) { ans [ i ] += count [ j ] ; } } for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } static public void main ( String [ ] args ) { String str = " abcd " ; printGreaterCount ( str ) ; } }
import java . util . * ; class GFG { static void printPairs ( int arr [ ] , int n , int sum ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int rem = sum - arr [ i ] ; if ( mp . containsKey ( rem ) ) { int count = mp . get ( rem ) ; for ( int j = 0 ; j < count ; j ++ ) System . out . print ( " ( " + rem + " , ▁ " + arr [ i ] + " ) " + "NEW_LINE"); } if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 , - 1 , 5 } ; int n = arr . length ; int sum = 6 ; printPairs ( arr , n , sum ) ; } }
import java . io . * ; class GFG { static int getCount ( int M , int N ) { int count = 0 ; if ( M == 1 ) return N ; if ( N == 1 ) return M ; if ( N > M ) { for ( int i = 1 ; i <= M ; i ++ ) { int numerator = N * i - N + M - i ; int denominator = M - 1 ; if ( numerator % denominator == 0 ) { int j = numerator / denominator ; if ( j >= 1 && j <= N ) count ++ ; } } } else { for ( int j = 1 ; j <= N ; j ++ ) { int numerator = M * j - M + N - j ; int denominator = N - 1 ; if ( numerator % denominator == 0 ) { int i = numerator / denominator ; if ( i >= 1 && i <= M ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int M = 3 , N = 5 ; System . out . println ( getCount ( M , N ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Arrays . sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { int mostDistantDistance = arr [ k ] - arr [ i ] ; if ( mostDistantDistance <= L ) { ways ++ ; } } } } return ways ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int L = 3 ; int ans = countTripletsLessThanL ( n , L , arr ) ; System . out . println ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int binary_lower ( int value , int arr [ ] , int n ) { int start = 0 ; int end = n - 1 ; int ans = - 1 ; int mid ; while ( start <= end ) { mid = ( start + end ) / 2 ; if ( arr [ mid ] >= value ) { end = mid - 1 ; ans = mid ; } else { start = mid + 1 ; } } return ans ; } static int countTriplet ( int arr [ ] , int n , int k ) { int count = 0 ; Arrays . sort ( arr ) ; for ( int i = 2 ; i < n ; i ++ ) { int cur = binary_lower ( arr [ i ] - k , arr , n ) ; if ( cur <= i - 2 ) { count += ( ( i - cur ) * ( i - cur - 1 ) ) / 2 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 } ; int k = 1 ; int n = arr . length ; System . out . println ( countTriplet ( arr , n , k ) ) ; } }
class GFG { static void replaceOriginal ( String s , int n ) { char r [ ] = new char [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { r [ i ] = s . charAt ( n - 1 - i ) ; if ( s . charAt ( i ) != ' a ' && s . charAt ( i ) != ' e ' && s . charAt ( i ) != ' i ' && s . charAt ( i ) != ' o ' && s . charAt ( i ) != ' u ' ) { System . out . print ( r [ i ] ) ; } } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; replaceOriginal ( s , n ) ; } }
class GFG { static boolean swapElement ( int [ ] arr1 , int [ ] arr2 , int n ) { int wrongIdx = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr1 [ i ] < arr1 [ i - 1 ] ) { wrongIdx = i ; } } int maximum = Integer . MIN_VALUE ; int maxIdx = - 1 ; boolean res = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr2 [ i ] > maximum && arr2 [ i ] >= arr1 [ wrongIdx - 1 ] ) { if ( wrongIdx + 1 <= n - 1 && arr2 [ i ] <= arr1 [ wrongIdx + 1 ] ) { maximum = arr2 [ i ] ; maxIdx = i ; res = true ; } } } if ( res ) { swap ( arr1 , wrongIdx , arr2 , maxIdx ) ; } return res ; } static void swap ( int [ ] a , int wrongIdx , int [ ] b , int maxIdx ) { int c = a [ wrongIdx ] ; a [ wrongIdx ] = b [ maxIdx ] ; b [ maxIdx ] = c ; } static void getSortedArray ( int arr1 [ ] , int arr2 [ ] , int n ) { if ( swapElement ( arr1 , arr2 , n ) ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr1 [ i ] + " ▁ " ) ; } } else { System . out . println ( " Not ▁ Possible " ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 7 , 4 , 10 } ; int arr2 [ ] = { 2 , 1 , 6 , 8 , 9 } ; int n = arr1 . length ; getSortedArray ( arr1 , arr2 , n ) ; } }
class GFG { static final int MAXN = 1000 ; static class Node { int start , end ; int length ; int [ ] insertionEdge = new int [ 26 ] ; int suffixEdge ; } ; static Node root1 , root2 ; static Node [ ] tree = new Node [ MAXN ] ; static int currNode ; static char [ ] s ; static int ptr ; static void insert ( int currIndex ) { int temp = currNode ; while ( true ) { int currLength = tree [ temp ] . length ; if ( currIndex - currLength >= 1 && ( s [ currIndex ] == s [ currIndex - currLength - 1 ] ) ) break ; temp = tree [ temp ] . suffixEdge ; } if ( tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] != 0 ) { currNode = tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] ; return ; } ptr ++ ; tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] = ptr ; tree [ ptr ] . end = currIndex ; tree [ ptr ] . length = tree [ temp ] . length + 2 ; tree [ ptr ] . start = tree [ ptr ] . end - tree [ ptr ] . length + 1 ; currNode = ptr ; temp = tree [ temp ] . suffixEdge ; if ( tree [ currNode ] . length == 1 ) { tree [ currNode ] . suffixEdge = 2 ; return ; } while ( true ) { int currLength = tree [ temp ] . length ; if ( currIndex - currLength >= 1 && ( s [ currIndex ] == s [ currIndex - currLength - 1 ] ) ) break ; temp = tree [ temp ] . suffixEdge ; } tree [ currNode ] . suffixEdge = tree [ temp ] . insertionEdge [ s [ currIndex ] - ' a ' ] ; } public static void main ( String [ ] args ) { root1 = new Node ( ) ; root1 . length = - 1 ; root1 . suffixEdge = 1 ; root2 = new Node ( ) ; root2 . length = 0 ; root2 . suffixEdge = 1 ; for ( int i = 0 ; i < MAXN ; i ++ ) tree [ i ] = new Node ( ) ; tree [ 1 ] = root1 ; tree [ 2 ] = root2 ; ptr = 2 ; currNode = 1 ; s = " forgeeksskeegfor " . toCharArray ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) insert ( i ) ; int last = ptr ; for ( int i = tree [ last ] . start ; i <= tree [ last ] . end ; i ++ ) System . out . print ( s [ i ] ) ; } }
import java . util . * ; class GFG { static class Node { char data ; Node next ; } ; static Node head_ref ; static char maxChar ( Node head ) { Node p = head ; int max = - 1 ; char res = '0' ; while ( p != null ) { Node q = p . next ; int count = 1 ; while ( q != null ) { if ( p . data == q . data ) count ++ ; q = q . next ; } if ( max < count ) { res = p . data ; max = count ; } p = p . next ; } return res ; } static void push ( char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; } public static void main ( String [ ] args ) { head_ref = null ; String str = " skeegforskeeg " ; char st [ ] = str . toCharArray ( ) ; int i ; for ( i = 0 ; i < st . length ; i ++ ) push ( st [ i ] ) ; System . out . print ( maxChar ( head_ref ) ) ; } }
import java . io . * ; class GFG { static int findLastIndex ( String str , Character x ) { int index = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) == x ) index = i ; return index ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; Character x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) System . out . println ( " Character ▁ not ▁ found " ) ; else System . out . println ( " Last ▁ index ▁ is ▁ " + index ) ; } }
import java . io . * ; class GFG { static int findLastIndex ( String str , Character x ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == x ) return i ; return - 1 ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; Character x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) System . out . println ( " Character ▁ not ▁ found " ) ; else System . out . println ( " Last ▁ index ▁ is ▁ " + index ) ; } }
import java . util . * ; class Middle { public static int middleOfThree ( int a , int b , int c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } } public static void main ( String [ ] args ) { int a = 20 , b = 30 , c = 40 ; System . out . println ( middleOfThree ( a , b , c ) ) ; } }
import java . io . * ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } public static void main ( String [ ] args ) { int l = 1 ; int r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . println ( countMaxSetBits ( l , r ) ) ; } }
class GFG { static class Node { int key ; Node left , right ; } ; static Node newNode ( int item ) { Node temp = new Node ( ) ; temp . key = item ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node node , int key ) { if ( node == null ) return newNode ( key ) ; if ( key < node . key ) node . left = insert ( node . left , key ) ; else if ( key > node . key ) node . right = insert ( node . right , key ) ; return node ; } static int findFloor ( Node root , int key ) { Node curr = root , ans = null ; while ( curr != null ) { if ( curr . key <= key ) { ans = curr ; curr = curr . right ; } else curr = curr . left ; } if ( ans != null ) return ans . key ; return - 1 ; } public static void main ( String [ ] args ) { int N = 25 ; Node root = new Node ( ) ; insert ( root , 19 ) ; insert ( root , 2 ) ; insert ( root , 1 ) ; insert ( root , 3 ) ; insert ( root , 12 ) ; insert ( root , 9 ) ; insert ( root , 21 ) ; insert ( root , 19 ) ; insert ( root , 25 ) ; System . out . printf ( " % d " , findFloor ( root , N ) ) ; } }
import java . io . * ; public class GFG { static int findS ( int s ) { int l = 1 , r = ( s / 2 ) + 1 ; while ( l <= r ) { int mid = ( l + r ) / 2 ; int sum = mid * ( mid + 1 ) / 2 ; if ( sum == s ) return mid ; else if ( sum > s ) r = mid - 1 ; else l = mid + 1 ; } return - 1 ; } static public void main ( String [ ] args ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) System . out . println ( " - 1" ) ; else System . out . println ( n ) ; } }
import java . util . * ; class GFG { static int compareStrings ( String str1 , String str2 ) { int i = 0 ; while ( i < str1 . length ( ) - 1 && str1 . charAt ( i ) == str2 . charAt ( i ) ) i ++ ; if ( str1 . charAt ( i ) > str2 . charAt ( i ) ) return - 1 ; if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) return 1 ; else return 0 ; } static int searchStr ( String [ ] arr , String str , int first , int last ) { if ( first > last ) return - 1 ; int mid = ( last + first ) / 2 ; if ( arr [ mid ] . isEmpty ( ) ) { int left = mid - 1 ; int right = mid + 1 ; while ( true ) { if ( left < right && right > last ) return - 1 ; if ( right <= last && ! arr [ right ] . isEmpty ( ) ) { mid = right ; break ; } if ( left >= right && ! arr [ left ] . isEmpty ( ) ) { mid = left ; break ; } right ++ ; left -- ; } } if ( compareStrings ( str , arr [ mid ] ) == 0 ) return mid ; if ( compareStrings ( str , arr [ mid ] ) < 0 ) return searchStr ( arr , str , mid + 1 , last ) ; return searchStr ( arr , str , first , mid - 1 ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " for " , " " , " " , " " , " geeks " , " ide " , " " , " practice " , " " , " " , " quiz " , " " , " " } ; String str = " quiz " ; int n = arr . length ; System . out . println ( searchStr ( arr , str , 0 , n - 1 ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int countReduction ( int num ) { int ans = 0 ; while ( num >= 10 ) { int temp = num ; num = 1 ; while ( temp > 0 ) { int digit = temp % 10 ; temp = temp / 10 ; num *= digit ; } ans ++ ; } return ans ; } int x1 = countReduction ( x ) ; int y1 = countReduction ( y ) ; if ( x1 < y1 ) return - 1 ; return 1 ; } } ) ; static void sortArray ( Integer a [ ] , int n ) { Arrays . sort ( a , new Comparator < Integer > ( ) { public int compare ( Integer x , Integer y ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { Integer arr [ ] = { 39 , 999 , 4 , 9876 } ; int N = arr . length ; sortArray ( arr , N ) ; } }
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 ) ) ; } }
import java . util . * ; import java . util . Arrays ; class GFG { static int [ ] [ ] transpose ( int [ ] [ ] mat , int row , int col ) { int [ ] [ ] tr = new int [ col ] [ row ] ; for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) { tr [ j ] [ i ] = mat [ i ] [ j ] ; } } return tr ; } static void RowWiseSort ( int [ ] [ ] B ) { for ( int i = 0 ; i < ( int ) B . length ; i ++ ) { Arrays . sort ( B [ i ] ) ; } } static void sortCol ( int [ ] [ ] mat , int N , int M ) { int [ ] [ ] B = transpose ( mat , N , M ) ; RowWiseSort ( B ) ; mat = transpose ( B , M , N ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 6 , 10 } , { 8 , 5 , 9 } , { 9 , 4 , 15 } , { 7 , 3 , 60 } } ; int N = mat . length ; int M = mat [ 0 ] . length ; sortCol ( mat , N , M ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static void largestArea ( int N , int M , int [ ] H , int [ ] V ) { Set < Integer > s1 = new HashSet < > ( ) ; Set < Integer > s2 = new HashSet < > ( ) ; for ( int i = 1 ; i <= N + 1 ; i ++ ) s1 . add ( i ) ; for ( int i = 1 ; i <= M + 1 ; i ++ ) s2 . add ( i ) ; for ( int i = 0 ; i < H . length ; i ++ ) { s1 . remove ( H [ i ] ) ; } for ( int i = 0 ; i < V . length ; i ++ ) { s2 . remove ( V [ i ] ) ; } int [ ] list1 = new int [ s1 . size ( ) ] ; int [ ] list2 = new int [ s2 . size ( ) ] ; int i = 0 ; Iterator it1 = s1 . iterator ( ) ; while ( it1 . hasNext ( ) ) { list1 [ i ++ ] = ( int ) it1 . next ( ) ; } i = 0 ; Iterator it2 = s2 . iterator ( ) ; while ( it2 . hasNext ( ) ) { list2 [ i ++ ] = ( int ) it2 . next ( ) ; } Arrays . sort ( list1 ) ; Arrays . sort ( list2 ) ; int maxH = 0 , p1 = 0 , maxV = 0 , p2 = 0 ; for ( int j = 0 ; j < list1 . length ; j ++ ) { maxH = Math . max ( maxH , list1 [ j ] - p1 ) ; p1 = list1 [ j ] ; } for ( int j = 0 ; j < list2 . length ; j ++ ) { maxV = Math . max ( maxV , list2 [ j ] - p2 ) ; p2 = list2 [ j ] ; } System . out . println ( maxV * maxH ) ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; int [ ] H = { 2 } ; int [ ] V = { 2 } ; largestArea ( N , M , H , V ) ; } }
import java . util . * ; class GFG { static int minimum_sum ( int [ ] A , int N ) { Arrays . sort ( A ) ; int sum = 2147483647 ; for ( int i = 0 ; i <= N - 3 ; i ++ ) { sum = Math . min ( sum , Math . abs ( A [ i ] - A [ i + 1 ] ) + Math . abs ( A [ i + 1 ] - A [ i + 2 ] ) ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] A = { 1 , 1 , 2 , 3 } ; int N = A . length ; System . out . print ( minimum_sum ( A , N ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { static void sameChar ( String S , int N ) { char temp [ ] = S . toCharArray ( ) ; Arrays . sort ( temp ) ; String s = new String ( temp ) ; int mid = s . charAt ( N / 2 ) ; int total_operations = 0 ; for ( int i = 0 ; i < N ; i ++ ) { total_operations += Math . abs ( ( ( s . charAt ( i ) - 0 ) - mid ) ) ; } System . out . print ( total_operations ) ; } public static void main ( String [ ] args ) { String S = " geeks " ; int N = S . length ( ) ; sameChar ( S , N ) ; } }
import java . util . * ; class GFG { static int getCount ( int arr [ ] , int N ) { int count = 0 ; if ( N < 3 ) return 0 ; for ( int b = 0 ; b < N ; b ++ ) { for ( int a = 0 ; a < N ; a ++ ) { if ( a == b ) continue ; for ( int c = 0 ; c < N ; c ++ ) { if ( c == a c == b ) continue ; int d = arr [ b ] * arr [ b ] / 4 ; if ( arr [ a ] * arr <= d ) count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . print ( getCount ( arr , N ) ) ; } }
import java . util . * ; class GFG { static boolean sameStrings ( String str1 , String str2 ) { int N = str1 . length ( ) ; int M = str2 . length ( ) ; if ( N != M ) { return false ; } int [ ] a = new int [ 256 ] ; int [ ] b = new int [ 256 ] ; for ( int i = 0 ; i < N ; i ++ ) { a [ str1 . charAt ( i ) - ' a ' ] ++ ; b [ str2 . charAt ( i ) - ' a ' ] ++ ; } int i = 0 ; while ( i < 256 ) { if ( ( a [ i ] == 0 && b [ i ] == 0 ) || ( a [ i ] != 0 && b [ i ] != 0 ) ) { i ++ ; } else { return false ; } } Arrays . sort ( a ) ; Arrays . sort ( b ) ; for ( i = 0 ; i < 256 ; i ++ ) { if ( a [ i ] != b [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { String S1 = " cabbba " , S2 = " abbccc " ; if ( sameStrings ( S1 , S2 ) ) System . out . print ( " YES " + "NEW_LINE"); else System . out . print ( " ▁ NO " + "NEW_LINE"); } }
import java . util . * ; import java . lang . * ; class GFG { static int minSteps ( StringBuilder A , StringBuilder B , int M , int N ) { if ( A . charAt ( 0 ) > B . charAt ( 0 ) ) return 0 ; if ( B . charAt ( 0 ) > A . charAt ( 0 ) ) { return 1 ; } if ( M <= N && A . charAt ( 0 ) == B . charAt ( 0 ) && count ( A , A . charAt ( 0 ) ) == M && count ( B , B . charAt ( 0 ) ) == N ) return - 1 ; for ( int i = 1 ; i < N ; i ++ ) { if ( B . charAt ( i ) > B . charAt ( 0 ) ) return 1 ; } for ( int i = 1 ; i < M ; i ++ ) { if ( A . charAt ( i ) < A . charAt ( 0 ) ) return 1 ; } for ( int i = 1 ; i < M ; i ++ ) { if ( A . charAt ( i ) > A . charAt ( 0 ) ) { swap ( A , i , B , 0 ) ; swap ( A , 0 , B , 0 ) ; return 2 ; } } for ( int i = 1 ; i < N ; i ++ ) { if ( B . charAt ( i ) < B . charAt ( 0 ) ) { swap ( A , 0 , B , i ) ; swap ( A , 0 , B , 0 ) ; return 2 ; } } return 0 ; } static int count ( StringBuilder a , char c ) { int count = 0 ; for ( int i = 0 ; i < a . length ( ) ; i ++ ) if ( a . charAt ( i ) == c ) count ++ ; return count ; } static void swap ( StringBuilder s1 , int index1 , StringBuilder s2 , int index2 ) { char c = s1 . charAt ( index1 ) ; s1 . setCharAt ( index1 , s2 . charAt ( index2 ) ) ; s2 . setCharAt ( index2 , c ) ; } public static void main ( String [ ] args ) { StringBuilder A = new StringBuilder ( " adsfd " ) ; StringBuilder B = new StringBuilder ( " dffff " ) ; int M = A . length ( ) ; int N = B . length ( ) ; System . out . println ( minSteps ( A , B , M , N ) ) ; } }
import java . io . * ; import java . util . Arrays ; class GFG { public static boolean findAns ( int [ ] A , int N , int X ) { if ( check ( A , X ) ) { return true ; } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int gcd = gcd ( A [ i ] , A [ j ] ) ; if ( gcd != 1 ) { if ( gcd <= X ) { return true ; } } } } return false ; } public static boolean check ( int [ ] A , int X ) { for ( int i = 0 ; i < A . length ; i ++ ) { if ( A [ i ] > X ) { return false ; } } return true ; } public static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int X = 4 ; int [ ] A = { 2 , 1 , 5 , 3 , 6 } ; int N = 5 ; System . out . println ( findAns ( A , N , X ) ) ; } }
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 ) ) ; } }
import java . util . * ; class GFG { static int splitArray ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int result = Integer . MAX_VALUE ; for ( int i = 1 ; i < N ; i ++ ) { result = Math . min ( result , arr [ i ] - arr [ i - 1 ] ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 2 , 6 , 4 } ; int N = arr . length ; System . out . print ( splitArray ( arr , N ) ) ; } }
import java . util . * ; class GFG { static void maxSum ( int arr [ ] , int brr [ ] , int N , int K ) { Vector < Integer > crr = new Vector < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { crr . add ( arr [ i ] ) ; } for ( int i = 0 ; i < K ; i ++ ) { crr . add ( brr [ i ] ) ; } Collections . sort ( crr ) ; Collections . reverse ( crr ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( crr . get ( i ) > 0 ) { sum += crr . get ( i ) ; } else { break ; } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 2 , - 1 , 4 , 5 } ; int N = arr . length ; int brr [ ] = { 1 , 2 , 3 , 2 } ; int K = brr . length ; maxSum ( arr , brr , N , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void countPairs ( int [ ] A , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { int xo = ( A [ i ] ^ A [ j ] ) ; int mx = Math . max ( A [ i ] , A [ j ] ) ; if ( xo > mx ) { count ++ ; } } } System . out . println ( count ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 3 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class Pair { int first , second ; Pair ( int first , int second ) { this . first = first ; this . second = second ; } } class GFG { static void findTheOrder ( int arr [ ] , String s , int N ) { List < Integer > ans = new ArrayList < Integer > ( ) ; List < Pair > A = new ArrayList < Pair > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { Pair p = new Pair ( arr [ i ] , i + 1 ) ; A . add ( p ) ; } Collections . sort ( A , ( p1 , p2 ) -> p1 . first - p2 . first ) ; PriorityQueue < Pair > q = new PriorityQueue < Pair > ( N , ( p1 , p2 ) -> p2 . first - p1 . first ) ; int index = 0 ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( s . charAt ( i ) == '0' ) { ans . add ( A . get ( index ) . second ) ; q . add ( A . get ( index ) ) ; index ++ ; } else { ans . add ( q . peek ( ) . second ) ; q . poll ( ) ; } } for ( int i : ans ) { System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 ; int arr [ ] = { 2 , 1 , 3 } ; String s = "001011" ; findTheOrder ( arr , s , N ) ; } }
import java . util . * ; class GFG { static void evenOddDiff ( int a [ ] , int n , int k ) { int j = - 1 ; Vector < Integer > even = new Vector < > ( ) ; Vector < Integer > odd = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 0 ) { even . add ( a [ i ] ) ; } else odd . add ( a [ i ] ) ; } j ++ ; Collections . sort ( even ) ; Collections . reverse ( even ) ; Collections . sort ( odd ) ; Collections . reverse ( odd ) ; int evenSum = 0 , oddSum = 0 ; for ( int i = 0 ; i < k ; i ++ ) { evenSum += even . get ( i ) ; } for ( int i = 0 ; i < k ; i ++ ) { oddSum += odd . get ( i ) ; } System . out . print ( Math . abs ( evenSum - oddSum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 3 , 4 , 5 } ; int N = arr . length ; int K = 2 ; evenOddDiff ( arr , N , K ) ; } }
import java . util . * ; class GFG { static void printArr ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void rearrangeArr ( int arr [ ] , int N ) { int totalSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { totalSum += arr [ i ] ; } if ( totalSum == 0 ) { System . out . print ( " - 1" + "NEW_LINE"); } else if ( totalSum > 0 ) { Arrays . sort ( arr ) ; arr = reverse ( arr ) ; printArr ( arr , N ) ; } else { Arrays . sort ( arr ) ; printArr ( arr , N ) ; } } static int [ ] reverse ( int a [ ] ) { int i , n = a . length , t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } return a ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , - 2 , 3 } ; int N = arr . length ; rearrangeArr ( arr , N ) ; } }
import java . io . * ; import java . util . * ; class GFG { private static int [ ] findSubsets ( int [ ] arr ) { HashMap < Integer , Integer > M = new HashMap < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { M . put ( arr [ i ] , M . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; } int [ ] subsets = new int [ M . size ( ) ] ; int i = 0 ; for ( Map . Entry < Integer , Integer > playerEntry : M . entrySet ( ) ) { subsets [ i ++ ] = playerEntry . getValue ( ) ; } return subsets ; } private static boolean subsetSum ( int [ ] subsets , int target ) { boolean [ ] [ ] dp = new boolean [ subsets . length + 1 ] [ target + 1 ] ; for ( int i = 0 ; i < dp . length ; i ++ ) dp [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= subsets . length ; i ++ ) { for ( int j = 1 ; j <= target ; j ++ ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; if ( j >= subsets [ i - 1 ] ) { dp [ i ] [ j ] |= dp [ i - 1 ] [ j - subsets [ i - 1 ] ] ; } } } return dp [ subsets . length ] [ target ] ; } public static void divideInto2Subset ( int [ ] arr ) { int [ ] subsets = findSubsets ( arr ) ; if ( ( arr . length ) % 2 == 1 ) { System . out . println ( " No " ) ; return ; } boolean isPossible = subsetSum ( subsets , arr . length / 2 ) ; if ( isPossible ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 2 , 3 } ; divideInto2Subset ( arr ) ; } }
import java . util . * ; class GFG { public static class Pair { int val ; int idx ; Pair ( int val , int idx ) { this . val = val ; this . idx = idx ; } } public static void main ( String [ ] args ) { int n = 5 ; int [ ] arr = { 4 , 7 , 2 , 3 , 9 } ; System . out . println ( minOperations ( arr , n ) ) ; } public static int minOperations ( int [ ] arr , int n ) { Pair [ ] num = new Pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { num [ i ] = new Pair ( arr [ i ] , i ) ; } Arrays . sort ( num , ( Pair a , Pair b ) -> a . val - b . val ) ; int res = 1 ; int streak = 1 ; int prev = num [ 0 ] . idx ; for ( int i = 1 ; i < n ; i ++ ) { if ( prev < num [ i ] . idx ) { res ++ ; streak = Math . max ( res , streak ) ; } else res = 1 ; prev = num [ i ] . idx ; } return n - streak ; } }
import java . util . * ; class GFG { static int MOD = 1000000007 ; static int solve ( Vector < Integer > values , Vector < Integer > salary ) { int ret = 1 ; int amt = 0 ; Collections . sort ( values ) ; Collections . sort ( salary ) ; while ( salary . size ( ) > 0 ) { while ( values . size ( ) > 0 && values . get ( values . size ( ) - 1 ) >= salary . get ( salary . size ( ) - 1 ) ) { amt ++ ; values . remove ( values . size ( ) - 1 ) ; } if ( amt == 0 ) return 0 ; ret *= amt -- ; ret %= MOD ; salary . remove ( salary . size ( ) - 1 ) ; } return ret ; } public static void main ( String [ ] args ) { Vector < Integer > values = new Vector < Integer > ( ) ; values . add ( 1 ) ; values . add ( 2 ) ; Vector < Integer > salary = new Vector < Integer > ( ) ; salary . add ( 2 ) ; System . out . print ( solve ( values , salary ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static final int maxN = 201 ; static int n1 , n2 , n3 ; static int [ ] [ ] [ ] dp = new int [ maxN ] [ maxN ] [ maxN ] ; static int getMaxSum ( int i , int j , int k , int arr1 [ ] , int arr2 [ ] , int arr3 [ ] ) { int cnt = 0 ; if ( i >= n1 ) cnt ++ ; if ( j >= n2 ) cnt ++ ; if ( k >= n3 ) cnt ++ ; if ( cnt >= 2 ) return 0 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; int ans = 0 ; if ( i < n1 && j < n2 ) ans = Math . max ( ans , getMaxSum ( i + 1 , j + 1 , k , arr1 , arr2 , arr3 ) + arr1 [ i ] * arr2 [ j ] ) ; if ( i < n1 && k < n3 ) ans = Math . max ( ans , getMaxSum ( i + 1 , j , k + 1 , arr1 , arr2 , arr3 ) + arr1 [ i ] * arr3 [ k ] ) ; if ( j < n2 && k < n3 ) ans = Math . max ( ans , getMaxSum ( i , j + 1 , k + 1 , arr1 , arr2 , arr3 ) + arr2 [ j ] * arr3 [ k ] ) ; dp [ i ] [ j ] [ k ] = ans ; return dp [ i ] [ j ] [ k ] ; } static void reverse ( int [ ] tmp ) { int i , k , t ; int n = tmp . length ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = tmp [ i ] ; tmp [ i ] = tmp [ n - i - 1 ] ; tmp [ n - i - 1 ] = t ; } } static int maxProductSum ( int arr1 [ ] , int arr2 [ ] , int arr3 [ ] ) { for ( int i = 0 ; i < dp . length ; i ++ ) for ( int j = 0 ; j < dp [ 0 ] . length ; j ++ ) for ( int k = 0 ; k < dp [ j ] [ 0 ] . length ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; Arrays . sort ( arr1 ) ; reverse ( arr1 ) ; Arrays . sort ( arr2 ) ; reverse ( arr2 ) ; Arrays . sort ( arr3 ) ; reverse ( arr3 ) ; return getMaxSum ( 0 , 0 , 0 , arr1 , arr2 , arr3 ) ; } public static void main ( String [ ] args ) { n1 = 2 ; int arr1 [ ] = { 3 , 5 } ; n2 = 2 ; int arr2 [ ] = { 2 , 1 } ; n3 = 3 ; int arr3 [ ] = { 4 , 3 , 5 } ; System . out . println ( maxProductSum ( arr1 , arr2 , arr3 ) ) ; } }
import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int ans = 0 ; Arrays . sort ( arr ) ; int left = 0 , right = 1 ; while ( right < n ) { if ( arr [ left ] == arr [ right ] ) ans += right - left ; else left = right ; right ++ ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 3 , 2 , 3 } ; int N = arr . length ; System . out . print ( countPairs ( arr , N ) ) ; } }
import java . util . * ; class Pair implements Comparable < Pair > { int first , second ; Pair ( int x , int y ) { first = x ; second = y ; } public int compareTo ( Pair p ) { return p . first - first ; } } class GFG { static int maximiseScore ( int A [ ] , int B [ ] , int K , int N ) { ArrayList < Pair > pairs = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { pairs . add ( new Pair ( A [ i ] , B [ i ] ) ) ; } Collections . sort ( pairs ) ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pairs . get ( i ) . second == 0 ) { sum += pairs . get ( i ) . first ; } else if ( pairs . get ( i ) . second == 1 ) { if ( K > 0 ) { sum += pairs . get ( i ) . first ; K -- ; } else { sum -= pairs . get ( i ) . first ; } } } return sum ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 4 , 6 , 2 , 8 } ; int B [ ] = { 1 , 0 , 1 , 1 , 0 } ; int K = 2 ; int N = A . length ; System . out . print ( maximiseScore ( A , B , K , N ) ) ; } }
import java . util . * ; class GFG { public static int sortString ( String S ) { Vector < Integer > sorted = new Vector < Integer > ( ) ; Vector < Integer > original = new Vector < Integer > ( ) ; boolean insert = false ; if ( S . length ( ) == 1 ) { System . out . println ( 0 ) ; } int curr = 1 ; for ( int i = 0 ; i < ( S . length ( ) - 1 ) ; i ++ ) { if ( S . charAt ( i ) == S . charAt ( i + 1 ) ) { curr += 1 ; insert = false ; } else { sorted . add ( curr ) ; original . add ( curr ) ; curr = 1 ; insert = true ; } } if ( ( S . charAt ( S . length ( ) - 1 ) != S . charAt ( S . length ( ) - 2 ) ) insert == false ) { sorted . add ( curr ) ; original . add ( curr ) ; } Collections . sort ( sorted ) ; int t_cost = 0 ; for ( int i = 0 ; i < sorted . size ( ) ; i ++ ) { t_cost += Math . abs ( sorted . get ( i ) - original . get ( i ) ) ; } return ( t_cost / 2 ) ; } public static void main ( String [ ] args ) { String S = " aabbcccdeffffggghhhhhii " ; System . out . print ( sortString ( S ) ) ; } }
import java . util . * ; class GFG { static void findTriplet ( int arr [ ] , int N ) { Arrays . sort ( arr ) ; int flag = 0 , i ; for ( i = N - 1 ; i - 2 >= 0 ; i -- ) { if ( arr [ i - 2 ] + arr [ i - 1 ] > arr [ i ] ) { flag = 1 ; break ; } } if ( flag != 0 ) { System . out . println ( arr [ i - 2 ] + " ▁ " + arr [ i - 1 ] + " ▁ " + arr [ i ] ) ; } else { System . out . println ( - 1 ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 10 , 3 , 5 } ; int N = arr . length ; findTriplet ( arr , N ) ; } }
import java . util . Arrays ; class GFG { static int numberofpairs ( int [ ] arr , int N ) { int answer = 0 ; Arrays . sort ( arr ) ; int minDiff = 10000000 ; for ( int i = 0 ; i < N - 1 ; i ++ ) minDiff = Math . min ( minDiff , arr [ i + 1 ] - arr [ i ] ) ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i + 1 ] - arr [ i ] == minDiff ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 } ; int N = arr . length ; System . out . print ( numberofpairs ( arr , N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int letters [ ] = { 4 , 3 , 3 , 3 , 4 , 4 , 3 , 5 , 5 , 4 } ; static int sumOfLetters ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += letters [ n % 10 ] ; n = n / 10 ; } return sum ; } static void sortArr ( int arr [ ] , int n ) { ArrayList < int [ ] > vp = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . add ( new int [ ] { sumOfLetters ( arr [ i ] ) , arr [ i ] } ) ; } Collections . sort ( vp , ( a , b ) -> a [ 0 ] - b [ 0 ] ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) System . out . print ( vp . get ( i ) [ 1 ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 31 , 18 } ; int n = arr . length ; sortArr ( arr , n ) ; } }
import java . io . * ; import java . util . * ; static int block ; class Query { int L , R , index ; int armstrong ; Query ( int L , int R , int index , int armstrong ) { this . L = L ; this . R = R ; this . index = index ; this . armstrong = armstrong ; } } class GFG { static int count_Armstrong ; Arrays . sort ( q , ( Query x , Query y ) -> { if ( x . L / block != y . L / block ) return x . L / block - y . L / block ; return x . R - y . R ; } ) ; x . index - y . index ) ; for ( int i = 0 ; i < m ; i ++ ) { System . out . println ( q [ i ] . armstrong ) ; } } static boolean isArmstrong ( int x ) { int n = String . valueOf ( x ) . length ( ) ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp /= 10 ; } if ( sum1 == x ) return true ; return false ; } static void add ( int currL , int a [ ] ) { if ( isArmstrong ( a [ currL ] ) ) count_Armstrong ++ ; } static void remove ( int currR , int a [ ] ) { if ( isArmstrong ( a [ currR ] ) ) count_Armstrong -- ; } static void queryResults ( int a [ ] , int n , Query q [ ] , int m ) { count_Armstrong = 0 ; block = ( int ) ( Math . sqrt ( n ) ) ; int currL = 0 , currR = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] . L , R = q [ i ] . R ; while ( currR <= R ) { add ( currR , a ) ; currR ++ ; } while ( currL > L ) { add ( currL - 1 , a ) ; currL -- ; } while ( currR > R + 1 ) { remove ( currR - 1 , a ) ; currR -- ; } while ( currL < L ) { remove ( currL , a ) ; currL ++ ; } q [ i ] . armstrong = count_Armstrong ; } } static void printResults ( Query q [ ] , int m ) { Arrays . sort ( q , ( Query x , Query y ) -> public static void main ( String [ ] args ) { int arr [ ] = { 18 , 153 , 8 , 9 , 14 , 5 } ; int n = arr . length ; Query q [ ] = new Query [ 2 ] ; q [ 0 ] = new Query ( 0 , 5 , 0 , 0 ) ; q [ 1 ] = new Query ( 3 , 5 , 1 , 0 ) ; int m = q . length ; queryResults ( arr , n , q , m ) ; printResults ( q , m ) ; } }
class GFG { static void sortArray ( int A [ ] , int N ) { int x = 0 , y = 0 , z = 0 ; if ( N % 4 == 0 N % 4 == 1 ) { for ( int i = 0 ; i < N / 2 ; i ++ ) { x = i ; if ( i % 2 == 0 ) { y = N - i - 2 ; z = N - i - 1 ; } A [ z ] = A [ y ] ; A [ y ] = A [ x ] ; A [ x ] = x + 1 ; } System . out . print ( " Sorted ▁ Array : ▁ " ) ; for ( int i = 0 ; i < N ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } else { System . out . print ( " - 1" ) ; } } public static void main ( String [ ] args ) { int A [ ] = { 5 , 4 , 3 , 2 , 1 } ; int N = A . length ; sortArray ( A , N ) ; } }
import java . util . * ; class GFG { static class node { int val ; node right , left ; } ; static node newnode ( int key ) { node temp = new node ( ) ; temp . val = key ; temp . right = null ; temp . left = null ; return temp ; } static void func ( node root ) { Queue < node > q = new LinkedList < > ( ) ; boolean t = true ; q . add ( root ) ; int i = - 1 , j = - 1 ; while ( ! q . isEmpty ( ) ) { int h = q . size ( ) ; while ( h > 0 ) { root = q . peek ( ) ; if ( i == - 1 ) { j = root . val ; } if ( i == - 2 ) { if ( j <= root . val ) { j = root . val ; i = - 3 ; } else { t = false ; break ; } } if ( root . left != null ) { q . add ( root . left ) ; } if ( root . right != null ) { q . add ( root . right ) ; } h = h - 1 ; q . remove ( ) ; } i = - 2 ; if ( t == false ) { break ; } } if ( t ) System . out . print ( " true " + "NEW_LINE"); else System . out . print ( " false " + "NEW_LINE"); } public static void main ( String [ ] args ) { node root = newnode ( 10 ) ; root . right = newnode ( 50 ) ; root . right . right = newnode ( 15 ) ; root . left = newnode ( 20 ) ; root . left . left = newnode ( 50 ) ; root . left . right = newnode ( 23 ) ; root . right . left = newnode ( 10 ) ; func ( root ) ; } }
import java . util . * ; class GFG { static int calculate_minimum_split ( int n , int a [ ] , int k ) { Integer [ ] p = new Integer [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) p [ i - 1 ] = a [ i ] - a [ i - 1 ] ; Arrays . sort ( p , new Comparator < Integer > ( ) { public int compare ( Integer a , Integer b ) { return b - a ; } } ) ; int min_sum = 0 ; for ( int i = 0 ; i < k - 1 ; i ++ ) min_sum += p [ i ] ; int res = a [ n - 1 ] - a [ 0 ] - min_sum ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 15 , 16 , 23 , 42 } ; int k = 3 ; int n = arr . length ; System . out . println ( calculate_minimum_split ( n , arr , k ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { if ( year2 . compareTo ( year1 ) > 0 ) return - 1 ; else if ( year2 . compareTo ( year1 ) < 0 ) return 1 ; else if ( month2 . compareTo ( month1 ) > 0 ) return - 1 ; else if ( month2 . compareTo ( month1 ) < 0 ) return 1 ; else if ( day2 . compareTo ( day1 ) > 0 ) return - 1 ; else return 1 ; } } ) ; static void printDatesAscending ( ArrayList < String > arr ) { Collections . sort ( arr , new Comparator < > ( ) { public int compare ( String date1 , String date2 ) { String day1 = date1 . substring ( 0 , 2 ) ; String month1 = date1 . substring ( 3 , 5 ) ; String year1 = date1 . substring ( 6 ) ; String day2 = date2 . substring ( 0 , 2 ) ; String month2 = date2 . substring ( 3 , 5 ) ; String year2 = date2 . substring ( 6 ) ; for ( int i = 0 ; i < arr . size ( ) ; i ++ ) System . out . println ( arr . get ( i ) ) ; } public static void main ( String [ ] args ) { ArrayList < String > arr = new ArrayList < > ( ) ; arr . add ( "25-08-1996" ) ; arr . add ( "03-08-1970" ) ; arr . add ( "09-04-1994" ) ; arr . add ( "29-08-1996" ) ; arr . add ( "14-02-1972" ) ; printDatesAscending ( arr ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static class pair { int first ; String second ; pair ( int first , String second ) { this . first = first ; this . second = second ; } } static boolean isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ( ch == ' A ' ch == ' E ' ch == ' I ' ch == ' O ' ch == ' U ' ) ; } static int countVowels ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isVowel ( str . charAt ( i ) ) ) ++ count ; return count ; } static void sortArr ( String arr [ ] , int n ) { ArrayList < pair > vp = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { vp . add ( new pair ( countVowels ( arr [ i ] ) , arr [ i ] ) ) ; } Collections . sort ( vp , ( a , b ) -> a . first - b . first ) ; for ( int i = 0 ; i < vp . size ( ) ; i ++ ) System . out . print ( vp . get ( i ) . second + " ▁ " ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " lmno " , " pqrst " , " aeiou " , " xyz " } ; int n = arr . length ; sortArr ( arr , n ) ; } }
import java . util . * ; class GFG { static int findK ( int arr [ ] , int size , int N ) { Arrays . sort ( arr ) ; int temp_sum = 0 ; for ( int i = 0 ; i < size ; i ++ ) { temp_sum += arr [ i ] ; if ( N - temp_sum == arr [ i ] * ( size - i - 1 ) ) { return arr [ i ] ; } } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 10 , 4 , 8 } ; int size = arr . length ; int N = 16 ; System . out . print ( findK ( arr , size , N ) ) ; } }
class GFG { static int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( ( arr [ l ] == arr [ mid ] ) && ( arr [ h ] == arr [ mid ] ) ) { l ++ ; h -- ; return search ( arr , l , h , key ) ; } else if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; else return search ( arr , mid + 1 , h , key ) ; } else if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 1 , 2 , 3 , 3 } ; int n = arr . length ; int key = 3 ; System . out . println ( search ( arr , 0 , n - 1 , key ) ) ; } }
import java . util . * ; class GFG { static long minimumCost ( long price [ ] , int n ) { Arrays . sort ( price ) ; long totalCost = 0 ; for ( int i = n - 1 ; i > 1 ; i -= 2 ) { if ( i == 2 ) { totalCost += price [ 2 ] + price [ 0 ] ; } else { long price_first = price [ i ] + price [ 0 ] + 2 * price [ 1 ] ; long price_second = price [ i ] + price [ i - 1 ] + 2 * price [ 0 ] ; totalCost += Math . min ( price_first , price_second ) ; } } if ( n == 1 ) { totalCost += price [ 0 ] ; } else { totalCost += price [ 1 ] ; } return totalCost ; } public static void main ( String [ ] args ) { long price [ ] = { 30 , 40 , 60 , 70 } ; int n = price . length ; System . out . println ( minimumCost ( price , n ) ) ; } }
import java . util . * ; class GFG { static void unique_combination ( int l , int sum , int K , Vector < Integer > local , Vector < Integer > A ) { if ( sum == K ) { System . out . print ( " { " ) ; for ( int i = 0 ; i < local . size ( ) ; i ++ ) { if ( i != 0 ) System . out . print ( " ▁ " ) ; System . out . print ( local . get ( i ) ) ; if ( i != local . size ( ) - 1 ) System . out . print ( " , ▁ " ) ; } System . out . println ( " } " ) ; return ; } for ( int i = l ; i < A . size ( ) ; i ++ ) { if ( sum + A . get ( i ) > K ) continue ; if ( i > l && A . get ( i ) == A . get ( i - 1 ) ) continue ; local . add ( A . get ( i ) ) ; unique_combination ( i + 1 , sum + A . get ( i ) , K , local , A ) ; local . remove ( local . size ( ) - 1 ) ; } } static void Combination ( Vector < Integer > A , int K ) { Collections . sort ( A ) ; Vector < Integer > local = new Vector < Integer > ( ) ; unique_combination ( 0 , 0 , K , local , A ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 10 , 1 , 2 , 7 , 6 , 1 , 5 } ; Vector < Integer > A = new Vector < > ( Arrays . asList ( arr ) ) ; int K = 8 ; Combination ( A , K ) ; } }
class GFG { public static int solution ( int arr [ ] , int x ) { int closestSum = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . length ; i ++ ) { for ( int j = i + 1 ; j < arr . length ; j ++ ) { for ( int k = j + 1 ; k < arr . length ; k ++ ) { if ( Math . abs ( x - closestSum ) > Math . abs ( x - ( arr [ i ] + arr [ j ] + arr [ k ] ) ) ) closestSum = ( arr [ i ] + arr [ j ] + arr [ k ] ) ; } } } return closestSum ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 2 , 1 , - 4 } ; int x = 1 ; System . out . print ( solution ( arr , x ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void addEdge ( ArrayList < ArrayList < Integer > > adj , int u , int v ) { adj . get ( u ) . add ( v ) ; } static void getindeg ( ArrayList < ArrayList < Integer > > adj , int V , ArrayList < Integer > indeg ) { for ( int i = 0 ; i < V ; i ++ ) { for ( int x : adj . get ( i ) ) { indeg . set ( x , indeg . get ( x ) + 1 ) ; } } } static ArrayList < Integer > topo ( ArrayList < ArrayList < Integer > > adj , int V , ArrayList < Integer > indeg ) { Queue < Integer > q = new LinkedList < > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { if ( indeg . get ( i ) == 0 ) { q . add ( i ) ; } } ArrayList < Integer > res = new ArrayList < Integer > ( ) ; while ( q . size ( ) > 0 ) { int u = q . poll ( ) ; res . add ( u ) ; for ( int x : adj . get ( u ) ) { indeg . set ( x , indeg . get ( x ) - 1 ) ; if ( indeg . get ( x ) == 0 ) { q . add ( x ) ; } } } return res ; } static ArrayList < Integer > makearray ( ArrayList < ArrayList < Integer > > v , int V ) { ArrayList < ArrayList < Integer > > adj = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { adj . add ( new ArrayList < Integer > ( ) ) ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { for ( int j = 0 ; j < v . get ( i ) . size ( ) - 1 ; j ++ ) { addEdge ( adj , v . get ( i ) . get ( j ) , v . get ( i ) . get ( j + 1 ) ) ; } } ArrayList < Integer > indeg = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < V ; i ++ ) { indeg . add ( 0 ) ; } getindeg ( adj , V , indeg ) ; ArrayList < Integer > res = topo ( adj , V , indeg ) ; return res ; } public static void main ( String [ ] args ) { int n = 10 ; ArrayList < ArrayList < Integer > > subseqs = new ArrayList < ArrayList < Integer > > ( ) ; subseqs . add ( new ArrayList < Integer > ( Arrays . asList ( 9 , 1 , 2 , 8 , 3 ) ) ) ; subseqs . add ( new ArrayList < Integer > ( Arrays . asList ( 6 , 1 , 2 ) ) ) ; subseqs . add ( new ArrayList < Integer > ( Arrays . asList ( 9 , 6 , 3 , 4 ) ) ) ; subseqs . add ( new ArrayList < Integer > ( Arrays . asList ( 5 , 2 , 7 ) ) ) ; subseqs . add ( new ArrayList < Integer > ( Arrays . asList ( 0 , 9 , 5 , 4 ) ) ) ; ArrayList < Integer > res = makearray ( subseqs , n ) ; for ( int x : res ) { System . out . print ( x + " ▁ " ) ; } } }
class GFG { static boolean existsTriplet ( int a [ ] , int b [ ] , int c [ ] , int x , int l1 , int l2 , int l3 ) { if ( l2 <= l1 && l2 <= l3 ) { swap ( l2 , l1 ) ; swap ( a , b ) ; } else if ( l3 <= l1 && l3 <= l2 ) { swap ( l3 , l1 ) ; swap ( a , c ) ; } for ( int i = 0 ; i < l1 ; i ++ ) { int j = 0 , k = l3 - 1 ; while ( j < l2 && k >= 0 ) { if ( a [ i ] + b [ j ] + c [ k ] == x ) return true ; if ( a [ i ] + b [ j ] + c [ k ] < x ) j ++ ; else k -- ; } } return false ; } private static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } private static void swap ( int [ ] x , int [ ] y ) { int [ ] temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 7 , 8 , 10 , 15 } ; int b [ ] = { 1 , 6 , 7 , 8 } ; int c [ ] = { 4 , 5 , 5 } ; int l1 = a . length ; int l2 = b . length ; int l3 = c . length ; int x = 14 ; if ( existsTriplet ( a , b , c , x , l1 , l2 , l3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static Vector < Long > pr = new Vector < > ( ) ; static boolean prime [ ] = new boolean [ 10000000 + 1 ] ; static void sieve ( long n ) { for ( int i = 2 ; i <= n ; i += 1 ) { prime [ i ] = true ; } for ( int p = 2 ; ( int ) p * ( int ) p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = ( int ) p * ( int ) p ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] ) pr . add ( ( long ) p ) ; } static long SemiPrimeSum ( long N ) { long ans = 0 ; for ( int i = 0 ; i < pr . size ( ) ; i += 1 ) { for ( int j = i ; j < pr . size ( ) ; j += 1 ) { if ( ( long ) pr . get ( i ) * ( long ) pr . get ( j ) > N ) break ; ans += ( long ) pr . get ( i ) * ( long ) pr . get ( j ) ; } } return ans ; } public static void main ( String [ ] args ) { long N = 6 ; sieve ( N ) ; System . out . println ( SemiPrimeSum ( N ) ) ; } }
import java . util . Arrays ; class GFG { static void compressArr ( int arr [ ] , int n ) { int i = 0 , j = 0 ; Arrays . sort ( arr ) ; while ( i < n ) { j = i ; while ( ( j + 1 < n ) && ( arr [ j + 1 ] == arr [ j ] + 1 ) ) { j ++ ; } if ( i == j ) { System . out . print ( arr [ i ] + " ▁ " ) ; i ++ ; } else { System . out . print ( arr [ i ] + " - " + arr [ j ] + " ▁ " ) ; i = j + 1 ; } } } public static void main ( String [ ] args ) { int n = 7 ; int arr [ ] = { 1 , 3 , 4 , 5 , 6 , 9 , 10 } ; compressArr ( arr , n ) ; } }
import java . util . Arrays ; import java . io . * ; class GFG { static void pendulumArrangement ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int odd , temp , in , pos ; pos = n - 1 ; if ( n % 2 == 0 ) odd = n - 1 ; else odd = n - 2 ; while ( odd > 0 ) { temp = arr [ odd ] ; in = odd ; while ( in != pos ) { arr [ in ] = arr [ in + 1 ] ; in ++ ; } arr [ in ] = temp ; odd = odd - 2 ; pos = pos - 1 ; } int start = 0 , end = ( n - 1 ) / 2 ; for ( ; start < end ; start ++ , end -- ) { temp = arr [ start ] ; arr [ start ] = arr [ end ] ; arr [ end ] = temp ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 2 , 4 , 55 , 6 , 8 } ; int n = arr . length ; pendulumArrangement ( arr , n ) ; } }
import java . util . * ; public class GFG { public static class Node { int data ; Node left , right , root ; Node ( int data ) { this . data = data ; } } public static Node AddNode ( Node root , int data ) { if ( root == null ) { root = new Node ( data ) ; return root ; } if ( root . data < data ) root . right = AddNode ( root . right , data ) ; else if ( root . data > data ) root . left = AddNode ( root . left , data ) ; return root ; } public static void TargetPair ( Node node , int tar ) { ArrayList < Node > LeftList = new ArrayList < > ( ) ; ArrayList < Node > RightList = new ArrayList < > ( ) ; Node curr_left = node ; Node curr_right = node ; while ( curr_left != null || curr_right != null || LeftList . size ( ) > 0 && RightList . size ( ) > 0 ) { while ( curr_left != null ) { LeftList . add ( curr_left ) ; curr_left = curr_left . left ; } while ( curr_right != null ) { RightList . add ( curr_right ) ; curr_right = curr_right . right ; } Node LeftNode = LeftList . get ( LeftList . size ( ) - 1 ) ; Node RightNode = RightList . get ( RightList . size ( ) - 1 ) ; int leftVal = LeftNode . data ; int rightVal = RightNode . data ; if ( leftVal >= rightVal ) break ; if ( leftVal + rightVal < tar ) { LeftList . remove ( LeftList . size ( ) - 1 ) ; curr_left = LeftNode . right ; } else if ( leftVal + rightVal > tar ) { RightList . remove ( RightList . size ( ) - 1 ) ; curr_right = RightNode . left ; } else { System . out . println ( LeftNode . data + " ▁ " + RightNode . data ) ; RightList . remove ( RightList . size ( ) - 1 ) ; LeftList . remove ( LeftList . size ( ) - 1 ) ; curr_left = LeftNode . right ; curr_right = RightNode . left ; } } } public static void main ( String [ ] b ) { Node root = null ; root = AddNode ( root , 2 ) ; root = AddNode ( root , 6 ) ; root = AddNode ( root , 5 ) ; root = AddNode ( root , 3 ) ; root = AddNode ( root , 4 ) ; root = AddNode ( root , 1 ) ; root = AddNode ( root , 7 ) ; int sum = 8 ; TargetPair ( root , sum ) ; } }
import java . util . Arrays ; class GFG { static int findNumber ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int max = arr [ n - 1 ] ; int table [ ] = new int [ ( 2 * max ) + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) table [ i ] = Integer . MAX_VALUE ; int ans = - 1 ; for ( int i = 1 ; i < ( 2 * max ) + 1 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] <= i ) { int res = table [ i - arr [ j ] ] ; if ( res != Integer . MAX_VALUE && res + 1 < table [ i ] ) table [ i ] = res + 1 ; } } if ( i > arr [ n - 1 ] && table [ i ] == Integer . MAX_VALUE ) { ans = i ; break ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 15 } ; int n = arr . length ; System . out . println ( findNumber ( arr , n ) ) ; } }
import java . util . * ; class GfG { static boolean check ( char s [ ] ) { int l = s . length ; Arrays . sort ( s ) ; for ( int i = 1 ; i < l ; i ++ ) { if ( s [ i ] - s [ i - 1 ] != 1 ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = " dcef " ; if ( check ( str . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; String str1 = " xyza " ; if ( check ( str1 . toCharArray ( ) ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
class GFG { public static void printArr ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] ) ; } public static int compare ( int num1 , int num2 ) { String A = Integer . toString ( num1 ) ; String B = Integer . toString ( num2 ) ; return ( A + B ) . compareTo ( B + A ) ; } public static void printSmallest ( int N , int [ ] arr ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( compare ( arr [ i ] , arr [ j ] ) > 0 ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } printArr ( arr , N ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 6 , 2 , 9 , 21 , 1 } ; int N = arr . length ; printSmallest ( N , arr ) ; } }
import java . util . * ; class GFG { static int countPoints ( int n , int m , int a [ ] , int [ ] b , int x , int y ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; int j = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < m ) { if ( a [ i ] + y < b [ j ] ) break ; if ( b [ j ] >= a [ i ] - x && b [ j ] <= a [ i ] + y ) { count ++ ; j ++ ; break ; } else j ++ ; } } return count ; } public static void main ( String args [ ] ) { int x = 1 , y = 4 ; int [ ] a = { 1 , 5 } ; int n = a . length ; int [ ] b = { 1 , 1 , 2 } ; int m = a . length ; System . out . println ( countPoints ( n , m , a , b , x , y ) ) ; } }
import java . util . * ; class GFG { static int N = 4 ; static void merge_and_sort ( int output [ ] , int arr [ ] [ ] , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { output [ i * n + j ] = arr [ i ] [ j ] ; } } Arrays . sort ( output ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 5 , 7 , 15 , 18 } , { 1 , 8 , 9 , 17 } , { 1 , 4 , 7 , 7 } } ; int n = N ; int k = arr . length ; int output [ ] = new int [ n * k ] ; merge_and_sort ( output , arr , n , k ) ; for ( int i = 0 ; i < n * k ; i ++ ) System . out . print ( output [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { public static int minOperations ( int n , int m , int k , int matrix [ ] [ ] ) { Vector < Integer > arr = new Vector < > ( ) ; int mod ; if ( matrix [ 0 ] [ 0 ] < 0 ) { mod = k - ( Math . abs ( matrix [ 0 ] [ 0 ] ) % k ) ; } else { mod = matrix [ 0 ] [ 0 ] % k ; } for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr . add ( matrix [ i ] [ j ] ) ; int val = matrix [ i ] [ j ] ; if ( val < 0 ) { int res = k - ( Math . abs ( val ) % k ) ; if ( res != mod ) { return - 1 ; } } else { int foo = matrix [ i ] [ j ] ; if ( foo % k != mod ) { return - 1 ; } } } } Collections . sort ( arr ) ; int median = arr . get ( ( n * m ) / 2 ) ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . abs ( arr . get ( i ) - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr . get ( ( ( n * m ) / 2 ) - 1 ) ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr . get ( i ) - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . length ; int m = matrix [ 0 ] . length ; int k = 2 ; System . out . println ( minOperations ( n , m , k , matrix ) ) ; } }
import java . util . * ; class GFG { static int minSubarray ( int A [ ] , int n ) { int minValue = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] < minValue ) minValue = A [ i ] ; } int maxValue = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > maxValue ) maxValue = A [ i ] ; } int pos_min = - 1 , pos_max = - 1 , ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] == minValue ) pos_min = i ; if ( A [ i ] == maxValue ) pos_max = i ; if ( pos_max != - 1 && pos_min != - 1 ) ans = Math . min ( ans , Math . abs ( pos_min - pos_max ) + 1 ) ; } return ans ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 5 , 9 , 7 , 1 , 9 , 4 } ; int n = A . length ; System . out . println ( minSubarray ( A , n ) ) ; } }
import java . util . * ; class solution { static long minimumMoves ( int a [ ] , int n ) { long operations = 0 ; Arrays . sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) operations += ( long ) Math . abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 3 , 2 } ; int n = arr . length ; System . out . print ( minimumMoves ( arr , n ) ) ; } }
import java . io . * ; class GFG { static void printCase ( int n ) { if ( n <= 2 ) { System . out . print ( - 1 ) ; return ; } for ( int i = n ; i >= 1 ; i -- ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 ; printCase ( n ) ; } }
import java . util . ArrayList ; import java . util . Collections ; import java . util . Comparator ; class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findMissingNumber ( ArrayList < Pair > ranges , int m ) { Collections . sort ( ranges , new Comparator < Pair > ( ) { public int compare ( Pair first , Pair second ) { if ( first . first == second . first ) { return first . second - second . second ; } return first . first - second . first ; } } ) ; ArrayList < Integer > ans = new ArrayList < > ( ) ; int prev = 0 ; for ( int j = 0 ; j < ranges . size ( ) ; j ++ ) { int start = ranges . get ( j ) . first ; int end = ranges . get ( j ) . second ; for ( int i = prev + 1 ; i < start ; i ++ ) ans . add ( i ) ; prev = end ; } for ( int i = prev + 1 ; i <= m ; i ++ ) ans . add ( i ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { if ( ans . get ( i ) <= m ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 2 , M = 6 ; ArrayList < Pair > ranges = new ArrayList < > ( ) ; ranges . add ( new Pair ( 1 , 2 ) ) ; ranges . add ( new Pair ( 4 , 5 ) ) ; findMissingNumber ( ranges , M ) ; } }
import java . util . Arrays ; class GFG { static boolean check ( int n , int k , int [ ] a , int [ ] b ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; boolean fl = false ; int ind = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { if ( fl == true ) { return false ; } fl = true ; ind = i ; } } if ( ind == - 1 | Math . abs ( a [ ind ] - b [ ind ] ) <= k ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 2 , k = 4 ; int [ ] a = { 1 , 5 } ; int b [ ] = { 1 , 1 } ; if ( check ( n , k , a , b ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . Arrays ; class GFG { static int MOD = 1000000007 ; static int SubseqWidths ( int [ ] A , int n ) { Arrays . sort ( A ) ; int [ ] pow2 = new int [ n ] ; pow2 [ 0 ] = 1 ; for ( int i = 1 ; i < n ; ++ i ) pow2 [ i ] = ( pow2 [ i - 1 ] * 2 ) % MOD ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) ans = ( ans + ( pow2 [ i ] - pow2 [ n - 1 - i ] ) * A [ i ] ) % MOD ; return ans ; } public static void main ( String [ ] args ) { int [ ] A = new int [ ] { 5 , 6 , 4 , 3 , 8 } ; int n = A . length ; System . out . println ( SubseqWidths ( A , n ) ) ; } }
import java . util . * ; class GFG { public static int maxArrayCover ( int [ ] a , int n , int x ) { Arrays . sort ( a ) ; int cc = 0 ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; if ( s > x ) { break ; } cc += 1 ; } if ( Arrays . stream ( a ) . sum ( ) == x ) { return n ; } else { if ( cc == n ) { return n - 1 ; } else { return cc ; } } } public static void main ( String [ ] args ) { int n = 3 ; int x = 70 ; int [ ] a = new int [ ] { 10 , 20 , 30 } ; System . out . println ( maxArrayCover ( a , n , x ) ) ; System . exit ( 0 ) ; } }
import java . util . * ; class GFG { static void QRsort ( int arr [ ] , int size ) { int MAX = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; int MIN = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; System . out . println ( " Maximum ▁ Element ▁ found ▁ is ▁ : ▁ " + MAX ) ; System . out . println ( " Minimum ▁ Element ▁ found ▁ is ▁ : ▁ " + MIN ) ; int COL = MIN ; int ROW = MAX / MIN + 1 ; int [ ] [ ] matrix = new int [ ROW ] [ COL ] ; for ( int i = 0 ; i < size ; i ++ ) { int quotient = arr [ i ] / MIN ; int remainder = arr [ i ] % MIN ; matrix [ quotient ] [ remainder ] = arr [ i ] ; } int k = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( matrix [ i ] [ j ] != 0 ) { arr [ k ++ ] = matrix [ i ] [ j ] ; } } } } static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 7 , 4 , 8 , 2 , 6 } ; int size = arr . length ; System . out . println ( " Initial ▁ Array ▁ : ▁ " ) ; printArray ( arr , size ) ; QRsort ( arr , size ) ; System . out . println ( " Array ▁ after ▁ sorting ▁ : ▁ " ) ; printArray ( arr , size ) ; } }
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 " ) ; } }
import java . io . * ; import java . util . * ; public class GFG { static int maximumSOP ( int [ ] a , int [ ] b ) { int sop = 0 ; int n = a . length ; Arrays . sort ( a ) ; Arrays . sort ( b ) ; for ( int i = 0 ; i < n ; i ++ ) { sop += a [ i ] * b [ i ] ; } return sop ; } public static void main ( String args [ ] ) { int [ ] A = { 1 , 2 , 3 } ; int [ ] B = { 4 , 5 , 1 } ; System . out . println ( maximumSOP ( A , B ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int countTriplets ( int arr [ ] , int n , int m ) { int count = 0 ; Arrays . sort ( arr ) ; int end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { start = 0 ; mid = end - 1 ; while ( start < mid ) { long prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; int m = 1 ; System . out . println ( countTriplets ( arr , n , m ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void sort3 ( int arr [ ] , int temp [ ] ) { if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } if ( arr [ 2 ] < arr [ 1 ] ) { temp [ 0 ] = arr [ 1 ] ; arr [ 1 ] = arr [ 2 ] ; arr [ 2 ] = temp [ 0 ] ; if ( arr [ 1 ] < arr [ 0 ] ) { temp [ 0 ] = arr [ 0 ] ; arr [ 0 ] = arr [ 1 ] ; arr [ 1 ] = temp [ 0 ] ; } } } public static void main ( String args [ ] ) { int a [ ] = new int [ ] { 10 , 12 , 5 } ; int temp1 [ ] = new int [ 10 ] ; sort3 ( a , temp1 ) ; for ( int i = 0 ; i < 3 ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } }
import java . util . Arrays ; class GFG { static void merge ( int [ ] arr , int beg , int mid , int end , int maxele ) { int i = beg ; int j = mid + 1 ; int k = beg ; while ( i <= mid && j <= end ) { if ( arr [ i ] % maxele <= arr [ j ] % maxele ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } else { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } } while ( i <= mid ) { arr [ k ] = arr [ k ] + ( arr [ i ] % maxele ) * maxele ; k ++ ; i ++ ; } while ( j <= end ) { arr [ k ] = arr [ k ] + ( arr [ j ] % maxele ) * maxele ; k ++ ; j ++ ; } for ( i = beg ; i <= end ; i ++ ) { arr [ i ] = arr [ i ] / maxele ; } } static void mergeSortRec ( int [ ] arr , int beg , int end , int maxele ) { if ( beg < end ) { int mid = ( beg + end ) / 2 ; mergeSortRec ( arr , beg , mid , maxele ) ; mergeSortRec ( arr , mid + 1 , end , maxele ) ; merge ( arr , beg , mid , end , maxele ) ; } } static void mergeSort ( int [ ] arr , int n ) { int maxele = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) + 1 ; mergeSortRec ( arr , 0 , n - 1 , maxele ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 999 , 612 , 589 , 856 , 56 , 945 , 243 } ; int n = arr . length ; mergeSort ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void printTriplets ( int arr [ ] , int n , int sum ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { int j = i + 1 , k = n - 1 ; while ( j < k ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= sum ) k -- ; else { for ( int x = j + 1 ; x <= k ; x ++ ) System . out . println ( arr [ i ] + " , ▁ " + arr [ j ] + " , ▁ " + arr [ x ] ) ; j ++ ; } } } } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 1 , 3 , 4 , 7 } ; int n = arr . length ; int sum = 12 ; printTriplets ( arr , n , sum ) ; } }
import java . util . * ; class GFG { static String areBookingsPossible ( int [ ] A , int [ ] B , int K ) { Arrays . sort ( A ) ; Arrays . sort ( B ) ; for ( int i = 0 ; i < A . length ; i ++ ) { if ( i + K < A . length && A [ i + K ] < B [ i ] ) { return " No " ; } } return " Yes " ; } public static void main ( String [ ] s ) { int [ ] arrival = { 1 , 2 , 3 } ; int [ ] departure = { 2 , 3 , 4 } ; int K = 1 ; System . out . print ( areBookingsPossible ( arrival , departure , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static void printVector ( Vector < Integer > V ) { for ( int i = 0 ; i < V . size ( ) ; i ++ ) { System . out . print ( V . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } static void insertionSortRecursive ( Vector < Integer > V , int N ) { if ( N <= 1 ) return ; insertionSortRecursive ( V , N - 1 ) ; int j = N - 1 ; while ( j > 0 && V . get ( j ) < V . get ( j - 1 ) ) { int temp = V . get ( j ) ; V . set ( j , V . get ( j - 1 ) ) ; V . set ( j - 1 , temp ) ; j -= 1 ; } } public static void main ( String [ ] args ) { Vector < Integer > A = new Vector < Integer > ( ) ; A . add ( 0 , 9 ) ; A . add ( 1 , 8 ) ; A . add ( 2 , 7 ) ; A . add ( 3 , 5 ) ; A . add ( 4 , 2 ) ; A . add ( 5 , 1 ) ; A . add ( 6 , 2 ) ; A . add ( 7 , 3 ) ; System . out . print ( " Array : ▁ " ) ; printVector ( A ) ; System . out . print ( " After ▁ Sorting ▁ : " ) ; insertionSortRecursive ( A , A . size ( ) ) ; printVector ( A ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minElements ( int arr [ ] , int n ) { int halfSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) halfSum = halfSum + arr [ i ] ; halfSum = halfSum / 2 ; Arrays . sort ( arr ) ; int res = 0 , curr_sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { curr_sum += arr [ i ] ; res ++ ; if ( curr_sum > halfSum ) return res ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 1 , 7 , 1 } ; int n = arr . length ; System . out . println ( minElements ( arr , n ) ) ; } }
public class MergeTwoSortedArrays { private static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } private static void merge ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { int temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } public static void main ( String [ ] args ) { int [ ] a1 = { 10 , 27 , 38 , 43 , 82 } ; int [ ] a2 = { 3 , 9 } ; merge ( a1 , a2 , a1 . length , a2 . length ) ; System . out . print ( " First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a1 . length ; i ++ ) { System . out . print ( a1 [ i ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " Second ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a2 . length ; i ++ ) { System . out . print ( a2 [ i ] + " ▁ " ) ; } } }
import java . util . * ; import java . lang . * ; import java . io . * ; class MergeTwoSorted { public static void mergeArrays ( int [ ] arr1 , int [ ] arr2 , int n1 , int n2 , int [ ] arr3 ) { int i = 0 , j = 0 , k = 0 ; while ( i < n1 && j < n2 ) { if ( arr1 [ i ] < arr2 [ j ] ) arr3 [ k ++ ] = arr1 [ i ++ ] ; else arr3 [ k ++ ] = arr2 [ j ++ ] ; } while ( i < n1 ) arr3 [ k ++ ] = arr1 [ i ++ ] ; while ( j < n2 ) arr3 [ k ++ ] = arr2 [ j ++ ] ; } public static void main ( String [ ] args ) { int [ ] arr1 = { 1 , 3 , 5 , 7 } ; int n1 = arr1 . length ; int [ ] arr2 = { 2 , 4 , 6 , 8 } ; int n2 = arr2 . length ; int [ ] arr3 = new int [ n1 + n2 ] ; mergeArrays ( arr1 , arr2 , n1 , n2 , arr3 ) ; System . out . println ( " Array ▁ after ▁ merging " ) ; for ( int i = 0 ; i < n1 + n2 ; i ++ ) System . out . print ( arr3 [ i ] + " ▁ " ) ; } }
import java . util . * ; import java . io . * ; class GFG { public static void sortSquares ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; Arrays . sort ( arr ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( " Before ▁ sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; sortSquares ( arr ) ; System . out . println ( " " ) ; System . out . println ( " After ▁ Sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }
import java . util . * ; class GFG { static boolean isPossible ( Integer a [ ] , int b [ ] , int n , int k ) { Arrays . sort ( a , Collections . reverseOrder ( ) ) ; Arrays . sort ( b ) ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] + b [ i ] < k ) return false ; return true ; } public static void main ( String [ ] args ) { Integer a [ ] = { 2 , 1 , 3 } ; int b [ ] = { 7 , 8 , 9 } ; int k = 10 ; int n = a . length ; if ( isPossible ( a , b , n , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
import java . util . * ; class GFG { static int findMinDiff ( int arr [ ] , int n , int m ) { if ( m == 0 n == 0 ) return 0 ; Arrays . sort ( arr ) ; if ( n < m ) return - 1 ; int min_diff = Integer . MAX_VALUE ; for ( int i = 0 ; i + m - 1 < n ; i ++ ) { int diff = arr [ i + m - 1 ] - arr [ i ] ; if ( diff < min_diff ) min_diff = diff ; } return min_diff ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 4 , 7 , 9 , 2 , 23 , 25 , 41 , 30 , 40 , 28 , 42 , 30 , 44 , 48 , 43 , 50 } ; int n = arr . length ; System . out . println ( " Minimum ▁ difference ▁ is ▁ " + findMinDiff ( arr , n , m ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int setBitCount ( int num ) { int count = 0 ; while ( num != 0 ) { if ( ( num & 1 ) != 0 ) count ++ ; num >>= 1 ; } return count ; } static void sortBySetBitCount ( int [ ] arr , int n ) { ArrayList < ArrayList < Integer > > count = new ArrayList < ArrayList < Integer > > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { count . add ( new ArrayList < Integer > ( Arrays . asList ( ( - 1 ) * setBitCount ( arr [ i ] ) , arr [ i ] ) ) ) ; } Collections . sort ( count , new Comparator < ArrayList < Integer > > ( ) { @ Override public int compare ( ArrayList < Integer > o1 , ArrayList < Integer > o2 ) { return o1 . get ( 0 ) . compareTo ( o2 . get ( 0 ) ) ; } } ) ; for ( int i = 0 ; i < count . size ( ) ; i ++ ) { System . out . print ( count . get ( i ) . get ( 1 ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; sortBySetBitCount ( arr , n ) ; } }
import java . util . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( Math . abs ( arr [ i ] ) ) ; return s . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }
import java . io . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }
class GFG { static int MAX = 100 ; public static void sortStrings ( String [ ] arr , int n ) { String temp ; for ( int j = 0 ; j < n - 1 ; j ++ ) { for ( int i = j + 1 ; i < n ; i ++ ) { if ( arr [ j ] . compareTo ( arr [ i ] ) > 0 ) { temp = arr [ j ] ; arr [ j ] = arr [ i ] ; arr [ i ] = temp ; } } } } public static void main ( String [ ] args ) { String [ ] arr = { " GeeksforGeeks " , " Quiz " , " Practice " , " Gblogs " , " Coding " } ; int n = arr . length ; sortStrings ( arr , n ) ; System . out . println ( " Strings ▁ in ▁ sorted ▁ order ▁ are ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( " String ▁ " + ( i + 1 ) + " ▁ is ▁ " + arr [ i ] ) ; } }
import java . io . * ; class GFG { static void sortByOneSwap ( int arr [ ] , int n ) { for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] < arr [ i - 1 ] ) { int j = i - 1 ; while ( j >= 0 && arr [ i ] < arr [ j ] ) j -- ; int temp = arr [ i ] ; arr [ i ] = arr [ j + 1 ] ; arr [ j + 1 ] = temp ; break ; } } } static void printArray ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 30 , 20 , 40 , 50 , 60 , 70 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; sortByOneSwap ( arr , n ) ; System . out . println ( " Sorted ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; } }
public class GFG { static int canReach ( String s , int L , int R ) { int dp [ ] = new int [ s . length ( ) ] ; dp [ 0 ] = 1 ; int pre = 0 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { if ( i >= L ) { pre += dp [ i - L ] ; } if ( i > R ) { pre -= dp [ i - R - 1 ] ; } if ( pre > 0 && s . charAt ( i ) == '0' ) dp [ i ] = 1 ; else dp [ i ] = 0 ; } return dp [ s . length ( ) - 1 ] ; } public static void main ( String [ ] args ) { String S = "01101110" ; int L = 2 , R = 3 ; if ( canReach ( S , L , R ) == 1 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
import java . util . * ; class GFG { static String performOperation ( char [ ] S , int N ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { if ( S [ i ] == ' a ' ) { S [ i ] = ' b ' ; } else { S [ i ] = ' a ' ; } } else { if ( S [ i ] == ' z ' ) { S [ i ] = ' y ' ; } else { S [ i ] = ' z ' ; } } } return String . valueOf ( S ) ; } public static void main ( String [ ] args ) { String S = " giad " ; int N = S . length ( ) ; System . out . print ( performOperation ( S . toCharArray ( ) , N ) ) ; } }
import java . util . * ; class GFG { static boolean check ( int x , int N ) { while ( true ) { N -= x ; if ( x < 10 ) break ; int temp2 = 0 ; while ( x > 0 ) { temp2 += ( x % 10 ) ; x = ( int ) x / 10 ; } x = temp2 ; } if ( ( x < 10 ) && ( N == 0 ) ) { return true ; } return false ; } static int countNoOfsuchX ( int N ) { int k = ( int ) ( Math . log10 ( N ) ) + 1 ; int count = 1 ; for ( int x = ( N - ( k * ( k + 1 ) * 5 ) ) ; x <= N ; x ++ ) { if ( check ( x , N ) ) { count += 1 ; } } return count ; } public static void main ( String [ ] args ) { int N = 9399 ; System . out . println ( countNoOfsuchX ( N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static long proc ( List < Integer > v ) { int n = v . size ( ) ; int low = v . get ( n - 1 ) , high = v . get ( n - 1 ) ; int p1 = n , p2 = n ; long ans = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int x = v . get ( i ) ; if ( x < low ) { low = x ; ans = 0 ; } else if ( x > high ) { high = x ; ans = 0 ; } if ( x == low ) p1 = i ; if ( x == high ) p2 = i ; ans += n - Math . max ( p1 , p2 ) ; } return ans ; } static long subarray ( List < Integer > v ) { int n = v . size ( ) ; if ( n <= 1 ) return n ; long ans = proc ( v ) ; int low = v . get ( 0 ) , pos_low = 0 ; int high = v . get ( 0 ) , pos_high = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int x = v . get ( i ) ; if ( x < low ) { low = x ; pos_low = i ; } else if ( x > high ) { high = x ; pos_high = i ; } } List < Integer > u = new ArrayList < > ( Collections . nCopies ( n , 0 ) ) ; Collections . copy ( u , v ) ; u . remove ( pos_low ) ; ans = Math . max ( ans , proc ( u ) ) ; List < Integer > w = new ArrayList < > ( Collections . nCopies ( n , 0 ) ) ; Collections . copy ( w , v ) ; w . remove ( pos_high ) ; return Math . max ( ans , proc ( w ) ) ; } public static void main ( String [ ] args ) { List < Integer > arr = Arrays . asList ( 7 , 2 , 5 , 4 , 3 , 1 ) ; System . out . println ( subarray ( arr ) ) ; } }
import java . util . * ; class GFG { static int find ( ArrayList < Integer > arr , int N ) { int Sum = 0 ; for ( int item : arr ) Sum += item ; if ( Sum % N == 1 ) return - 1 ; int k = Sum / N ; int ans = 0 ; int i = 0 ; while ( i < N ) { ans = ans + Math . abs ( k - arr . get ( i ) ) ; i += 1 ; } return ans / 2 ; } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < > ( ) ; arr . add ( 5 ) ; arr . add ( 4 ) ; arr . add ( 1 ) ; arr . add ( 10 ) ; int N = arr . size ( ) ; System . out . println ( find ( arr , N ) ) ; } }
import java . io . * ; class GFG { static int minimumSteps ( int a , int b ) { int cnt = 0 ; a = Math . abs ( a - b ) ; cnt = ( a / 5 ) + ( a % 5 ) / 2 + ( a % 5 ) % 2 ; return cnt ; } public static void main ( String [ ] args ) { int A = 3 , B = 9 ; System . out . println ( minimumSteps ( A , B ) ) ; } }
import java . util . * ; class GFG { static int shortestDistance ( String s , int n ) { int [ ] visited = new int [ 128 ] ; Arrays . fill ( visited , - 1 ) ; int ans = Integer . MAX_VALUE ; for ( int right = 0 ; right < n ; right ++ ) { char c = s . charAt ( right ) ; int left = visited ; if ( left != - 1 ) ans = Math . min ( ans , right - left - 1 ) ; visited = right ; } return ans == Integer . MAX_VALUE ? - 1 : ans ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = 13 ; System . out . print ( shortestDistance ( s , n ) ) ; } }
import java . util . * ; public class GFG { static String smallestNonSubsequence ( String S , int N ) { int freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( S . charAt ( i ) == ' a ' ) freq ++ ; String ans = " " ; for ( int i = 0 ; i < f req ; i ++ ) { ans += ' a ' ; } if ( freq == N ) ans = ans . replace ( ans . charAt ( freq - 1 ) , ' b ' ) ; else ans += ' a ' ; return ans ; } public static void main ( String args [ ] ) { String S = " abcdefghijklmnopqrstuvwxyz " ; int N = S . length ( ) ; System . out . println ( smallestNonSubsequence ( S , N ) ) ; } }
import java . util . * ; class GFG { static void possibleNumbers ( int N , int M , int A , int B ) { if ( A > B ) { int temp = A ; A = B ; B = temp ; } int number = N + M * A ; System . out . print ( number + " ▁ " ) ; if ( A != B ) { for ( int i = 0 ; i < M ; i ++ ) { number = number - A + B ; System . out . print ( number + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int N = 5 , M = 3 , A = 4 , B = 6 ; possibleNumbers ( N , M , A , B ) ; } }
import java . util . * ; class GFG { static int countUnsetBits ( int N ) { int c = 0 ; while ( N != 0 ) { if ( N % 2 == 0 ) { c += 1 ; } N = N >> 1 ; } return c ; } static void countBitwiseZero ( int N ) { int unsetBits = countUnsetBits ( N ) ; System . out . print ( 1 << unsetBits ) ; } public static void main ( String [ ] args ) { int N = 9 ; countBitwiseZero ( N ) ; } }
public class GFG { static int maxXORUtil ( int arr [ ] , int N , int xrr , int orr ) { if ( N == 0 ) return xrr ^ orr ; int x = maxXORUtil ( arr , N - 1 , xrr ^ orr , arr [ N - 1 ] ) ; int y = maxXORUtil ( arr , N - 1 , xrr , orr arr [ N - 1 ] ) ; return Math . max ( x , y ) ; } static int maximumXOR ( int arr [ ] , int N ) { return maxXORUtil ( arr , N , 0 , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 7 } ; int N = arr . length ; System . out . println ( maximumXOR ( arr , N ) ) ; } }
import java . util . ArrayList ; import java . util . Arrays ; import java . util . HashMap ; public class GFG { static void leastBricks ( ArrayList < ArrayList < Integer > > wall ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; int res = 0 ; for ( ArrayList < Integer > list : wall ) { int width = 0 ; for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { width += list . get ( i ) ; map . put ( width , map . getOrDefault ( width , 0 ) + 1 ) ; res = Math . max ( res , map . getOrDefault ( width , 0 ) ) ; } } System . out . println ( wall . size ( ) - res ) ; } public static void main ( String [ ] args ) { ArrayList < ArrayList < Integer > > arr = new ArrayList < > ( ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 1 , 2 , 2 , 1 ) ) ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 3 , 1 , 2 ) ) ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 1 , 3 , 2 ) ) ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 2 , 4 ) ) ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 3 , 1 , 2 ) ) ) ; arr . add ( new ArrayList < > ( Arrays . asList ( 1 , 3 , 1 , 1 ) ) ) ; leastBricks ( arr ) ; } }
import java . util . * ; class GFG { static void findPermutation ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 5 ; findPermutation ( N ) ; } }
import java . util . Arrays ; class GFG { static int numberOfWays ( int N , int X , int Y ) { int S1 = ( N - 1 ) * X + Y ; int S2 = ( N - 1 ) * Y + X ; return ( S2 - S1 + 1 ) ; } public static void main ( String [ ] args ) { int N = 3 ; int X = 13 ; int Y = 15 ; System . out . println ( numberOfWays ( N , X , Y ) ) ; } }
import java . io . * ; class GFG { static int MinimumOperations ( int [ ] A , int N , int K ) { int Count = 0 ; int i = 0 ; while ( i < N - 1 ) { i = i + K - 1 ; Count ++ ; } return Count ; } public static void main ( String [ ] args ) { int [ ] A = { 5 , 4 , 3 , 1 , 2 } ; int K = 3 ; int N = A . length ; System . out . println ( MinimumOperations ( A , N , K ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static boolean is_prime ( long n ) { if ( n == 1 ) { return false ; } for ( long i = 2 ; i <= ( long ) Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static void consecutive_primes ( long n ) { long first = - 1 , second = - 1 ; for ( long i = ( long ) Math . sqrt ( n ) ; i >= 2 ; i -- ) { if ( is_prime ( i ) ) { first = i ; break ; } } for ( long i = ( long ) Math . sqrt ( n ) + 1 ; i <= n / 2 ; i ++ ) { if ( is_prime ( i ) ) { second = i ; break ; } } if ( first * second >= n ) { System . out . println ( first + " ▁ " + second ) ; } else { for ( long i = second + 1 ; i <= n ; i ++ ) { if ( is_prime ( i ) ) { System . out . println ( second + " ▁ " + i ) ; return ; } } } } public static void main ( String args [ ] ) { long n = 14 ; consecutive_primes ( n ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void countDecrements ( long arr [ ] [ ] ) { int n = arr . length ; int m = arr [ 0 ] . length ; int count_1 = 0 ; int count_2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_1 ++ ; if ( 1 - ( i + j ) % 2 == arr [ i ] [ j ] % 2 ) count_2 ++ ; } } System . out . println ( Math . min ( count_1 , count_2 ) ) ; } public static void main ( String [ ] args ) { long arr [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 2 , 3 } , { 1 , 2 , 3 } } ; countDecrements ( arr ) ; } }
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 ) ; } }
import java . io . * ; class GFG { static int distIntegers ( int L , int R ) { return 2 * R - 2 * L + 1 ; } public static void main ( String [ ] args ) { int L = 3 , R = 8 ; System . out . println ( distIntegers ( L , R ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void evenXorSubarray ( int arr [ ] , int n ) { int ans = 0 ; int freq [ ] = { 0 , 0 } ; int XOR = 0 ; for ( int i = 0 ; i < n ; i ++ ) { XOR = XOR ^ arr [ i ] ; if ( XOR % 2 == 0 ) { ans += freq [ 0 ] + 1 ; freq [ 0 ] ++ ; } else { ans += freq [ 1 ] ; freq [ 1 ] ++ ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int N = arr . length ; evenXorSubarray ( arr , N ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static int countOccurrences ( int N , int X ) { int count = 0 ; for ( int i = 1 ; i < Math . sqrt ( X ) ; i ++ ) { if ( X % i == 0 ) { int a = i ; int b = X / i ; if ( a <= N && b <= N ) { if ( a == b ) count += 1 ; else count += 2 ; } } } return count ; } public static void main ( String [ ] args ) { int N = 7 ; int X = 12 ; System . out . println ( countOccurrences ( N , X ) ) ; } }
import java . util . * ; class GFG { static void findPerfectIndex ( int N , int K ) { int i = 0 ; for ( ; i < K ; i ++ ) { System . out . print ( ( N - K + 1 ) + i + " ▁ " ) ; } for ( ; i < N ; i ++ ) { System . out . print ( i - K + 1 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 10 , K = 3 ; findPerfectIndex ( N , K ) ; } }
import java . io . * ; class GFG { static void countPairs ( long L , long R ) { long firstNum = 2 * L ; long lastNum = 2 * R ; long Cntpairs = lastNum - firstNum + 1 ; System . out . println ( Cntpairs ) ; } public static void main ( String [ ] args ) { long L = 2 , R = 3 ; countPairs ( L , R ) ; } }
import java . util . * ; class GFG { static void countPairs ( int A [ ] , int n ) { int count = ( n * ( n - 1 ) ) / 2 ; Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( A [ i ] ) ) { mp . put ( A [ i ] , mp . get ( A [ i ] ) + 1 ) ; } else { mp . put ( A [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int c = it . getValue ( ) ; count = count - ( c * ( c - 1 ) ) / 2 ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 4 , 7 } ; int N = A . length ; countPairs ( A , N ) ; } }
import java . util . * ; class GFG { static void arrayElementEqual ( int arr [ ] , int N ) { int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } if ( sum % N == 0 ) { System . out . print ( " Yes " ) ; } else { System . out . print ( " No " + "NEW_LINE"); } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 6 , 4 } ; int N = arr . length ; arrayElementEqual ( arr , N ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static String check ( int current_row , int current_col , int destination_row , int destination_col ) { if ( current_row == destination_row ) return " POSSIBLE " ; else if ( current_col == destination_col ) return " POSSIBLE " ; else return " NOT ▁ POSSIBLE " ; } public static void main ( String [ ] args ) { int current_row = 8 ; int current_col = 8 ; int destination_row = 8 ; int destination_col = 4 ; String output = check ( current_row , current_col , destination_row , destination_col ) ; System . out . println ( output ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int isDiv ( int N , int X , int M , int res ) { if ( X == 0 ) { if ( N % M == 0 ) { res = N ; return res ; } return res ; } for ( int i = 0 ; i < 9 ; i ++ ) { int temp = isDiv ( N * 10 + i , X - 1 , M , res ) ; if ( temp != - 1 ) { return temp ; } } return res ; } public static void main ( String [ ] args ) throws java . lang . Exception { int N = 4 , M = 50 , X = 2 ; int res = - 1 ; res = isDiv ( N , X , M , res ) ; System . out . println ( res ) ; } }
import java . io . * ; class GFG { public static void minimumMoves ( int [ ] arr , int N ) { int sum = 0 ; int maxelement = - 1 ; if ( N == 2 ) { System . out . print ( "0" ) ; return ; } for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; maxelement = Math . max ( maxelement , arr [ i ] ) ; } int k = ( sum + N - 2 ) / ( N - 1 ) ; k = Math . max ( maxelement , k ) ; int ans = k * ( N - 1 ) - sum ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 7 } ; int N = arr . length ; minimumMoves ( arr , N ) ; } }
class GFG { public static void printSubsequence ( int N ) { System . out . print ( " { ▁ " ) ; for ( int i = 1 ; i < N - 1 ; i ++ ) { System . out . print ( i + " , ▁ " ) ; } System . out . println ( N - 1 + " ▁ } " ) ; System . out . print ( " { ▁ " + N + " ▁ } " ) ; } public static void main ( String [ ] args ) { int N = 8 ; printSubsequence ( N ) ; } }
class GFG { public static void array_divisbleby_k ( int N , int K ) { boolean flag = false ; int d1 = 0 , d2 = 0 ; for ( int i = 2 ; i * i <= K ; i ++ ) { if ( K % i == 0 ) { flag = true ; d1 = i ; d2 = K / i ; break ; } } if ( flag ) { for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 1 ) { System . out . print ( d2 + " ▁ " ) ; } else { System . out . print ( d1 + " ▁ " ) ; } } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int N = 5 , K = 21 ; array_divisbleby_k ( N , K ) ; } }
import java . io . * ; class GFG { public static void print ( char arr [ ] [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { char a = arr [ i ] [ j ] ; if ( ( i + j ) % 2 == 0 && a == ' F ' ) { arr [ i ] [ j ] = '1' ; } else if ( a == ' F ' ) { arr [ i ] [ j ] = '2' ; } } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( arr [ i ] [ j ] ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 , m = 4 ; char arr [ ] [ ] = { { ' F ' , ' F ' , ' F ' , ' F ' } , { ' F ' , ' O ' , ' F ' , ' F ' } , { ' F ' , ' F ' , ' O ' , ' F ' } , { ' F ' , ' F ' , ' F ' , ' F ' } } ; print ( arr , n , m ) ; } }
import java . io . * ; class GFG { static String removeDuplicateLetters ( String s ) { int [ ] cnt = new int [ 26 ] ; int [ ] vis = new int [ 26 ] ; int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ s . charAt ( i ) - ' a ' ] ++ ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] -- ; if ( vis [ s . charAt ( i ) - ' a ' ] == 0 ) { int size = res . length ( ) ; while ( size > 0 && res . charAt ( size - 1 ) > s . charAt ( i ) && cnt [ res . charAt ( size - 1 ) - ' a ' ] > 0 ) { vis [ res . charAt ( size - 1 ) - ' a ' ] = 0 ; res = res . substring ( 0 , size - 1 ) ; size -- ; } res += s . charAt ( i ) ; vis [ s . charAt ( i ) - ' a ' ] = 1 ; } } return res ; } public static void main ( String [ ] args ) { String S = " acbc " ; System . out . println ( removeDuplicateLetters ( S ) ) ; } }
import java . util . * ; class GFG { static int MaximumSides ( int n ) { if ( n < 4 ) return - 1 ; return n % 2 == 0 ? n / 2 : - 1 ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . print ( MaximumSides ( N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void minCost ( String s , int k ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int [ ] a = new int [ 26 ] ; for ( int j = i ; j < n ; j += k ) { a [ s . charAt ( j ) - ' a ' ] ++ ; } int min_cost = Integer . MAX_VALUE ; for ( int ch = 0 ; ch < 26 ; ch ++ ) { int cost = 0 ; for ( int tr = 0 ; tr < 26 ; tr ++ ) cost += Math . abs ( ch - tr ) * a [ tr ] ; min_cost = Math . min ( min_cost , cost ) ; } ans += min_cost ; } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { String S = " abcdefabc " ; int K = 3 ; minCost ( S , K ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int minAbsDiff ( int N ) { if ( N % 4 == 0 N % 4 == 3 ) { return 0 ; } return 1 ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( minAbsDiff ( N ) ) ; } }
import java . util . * ; class GFG { static void countPairs ( int arr [ ] , int N ) { int countPowerof2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( Integer . bitCount ( arr [ i ] ) == 1 ) countPowerof2 ++ ; } int desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) / 2 ; System . out . print ( desiredPairs + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 7 , 2 } ; int N = arr . length ; countPairs ( arr , N ) ; } }
import java . util . * ; class GFG { static final int MAXN = 1000000 ; static int is_prime [ ] ; static int count_of_primes [ ] ; static void sieve ( ) { is_prime = new int [ MAXN ] ; count_of_primes = new int [ MAXN ] ; Arrays . fill ( is_prime , 0 ) ; Arrays . fill ( count_of_primes , 0 ) ; for ( int i = 3 ; i < MAXN ; i += 2 ) { is_prime [ i ] = 1 ; } for ( int i = 3 ; i * i < MAXN ; i += 2 ) { if ( is_prime [ i ] == 1 ) { for ( int j = i * i ; j < MAXN ; j += i ) { is_prime [ j ] = 0 ; } } } is_prime [ 2 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) count_of_primes [ i ] = count_of_primes [ i - 1 ] + is_prime [ i ] ; } static long power ( long x , long y , long p ) { long result = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) result = ( result * x ) % p ; x = ( x * x ) % p ; y >>= 1 ; } return result ; } static void numberOfWays ( int N ) { long count = count_of_primes [ N ] - 1 ; long mod = 1000000007 ; long answer = power ( 2 , count , mod ) ; if ( N == 1 ) answer = 0 ; long ans = answer ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { sieve ( ) ; int N = 7 ; numberOfWays ( N ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static void maxSumOfSquares ( int N , int S ) { int res = 0 ; if ( S < N S > 9 * N ) { System . out . println ( - 1 ) ; return ; } S = S - N ; int c = 0 ; while ( S > 0 ) { c ++ ; if ( S / 8 > 0 ) { res += 9 * 9 ; S -= 8 ; } else { res += ( S + 1 ) * ( S + 1 ) ; break ; } } res = res + ( N - c ) ; System . out . println ( res ) ; } public static void main ( String args [ ] ) { int N = 3 ; int S = 12 ; maxSumOfSquares ( N , S ) ; } }
import java . util . * ; class GFG { static int ncr ( int n , int k ) { int res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static int countPath ( int N , int M , int K ) { int answer ; if ( K >= 2 ) answer = 0 ; else if ( K == 0 ) answer = ncr ( N + M - 2 , N - 1 ) ; else { answer = ncr ( N + M - 2 , N - 1 ) ; int X = ( N - 1 ) / 2 + ( M - 1 ) / 2 ; int Y = ( N - 1 ) / 2 ; int midCount = ncr ( X , Y ) ; X = ( ( N - 1 ) - ( N - 1 ) / 2 ) + ( ( M - 1 ) - ( M - 1 ) / 2 ) ; Y = ( ( N - 1 ) - ( N - 1 ) / 2 ) ; midCount *= ncr ( X , Y ) ; answer -= midCount ; } return answer ; } public static void main ( String [ ] args ) { int N = 3 ; int M = 3 ; int K = 1 ; System . out . print ( countPath ( N , M , K ) ) ; } }
import java . util . * ; class GFG { static int calculate_steps ( Integer arr [ ] , int n , int minimum ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int val = arr [ i ] ; if ( arr [ i ] > minimum ) { arr [ i ] = arr [ i ] - minimum ; count += arr [ i ] / 5 ; arr [ i ] = arr [ i ] % 5 ; count += arr [ i ] / 2 ; arr [ i ] = arr [ i ] % 2 ; if ( arr [ i ] > 0 ) { count ++ ; } } arr [ i ] = val ; } return count ; } static int solve ( Integer arr [ ] , int n ) { Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; int minimum = arr [ n - 1 ] ; int count1 = 0 , count2 = 0 , count3 = 0 ; count1 = calculate_steps ( arr , n , minimum ) ; count2 = calculate_steps ( arr , n , minimum - 1 ) ; count3 = calculate_steps ( arr , n , minimum - 2 ) ; return Math . min ( count1 , Math . min ( count2 , count3 ) ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 3 , 6 , 6 } ; int N = arr . length ; System . out . print ( solve ( arr , N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static String mergePalindromes ( StringBuilder S , StringBuilder P ) { Map < Character , Integer > mapS = new HashMap < > ( ) , mapP = new HashMap < > ( ) , mapT = new HashMap < > ( ) ; int n = S . length ( ) , m = P . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mapS . put ( S . charAt ( i ) , mapS . getOrDefault ( S . charAt ( i ) , 0 ) + 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { mapP . put ( P . charAt ( i ) , mapP . getOrDefault ( P . charAt ( i ) , 0 ) + 1 ) ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) ) ; mapS . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapS . getOrDefault ( i , 0 ) - 1 ) ; mapS . put ( i , 1 ) ; } if ( mapP . getOrDefault ( i , 0 ) % 2 == 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) ) ; mapP . put ( i , 0 ) ; } else { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + mapP . getOrDefault ( i , 0 ) - 1 ) ; mapP . put ( i , 1 ) ; } } int check = 0 ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) > 0 && mapP . getOrDefault ( i , 0 ) > 0 ) { mapT . put ( i , mapT . getOrDefault ( i , 0 ) + 2 ) ; check = 1 ; break ; } } StringBuilder half1 = new StringBuilder ( ) , half2 = new StringBuilder ( ) ; for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { for ( int j = 0 ; ( 2 * j ) < mapT . getOrDefault ( i , 0 ) ; j ++ ) { half1 . append ( i ) ; half2 . append ( i ) ; } } StringBuilder tmp = half2 . reverse ( ) ; if ( check == 1 ) { return half1 . append ( tmp ) . toString ( ) ; } for ( char i = ' a ' ; i <= ' z ' ; i ++ ) { if ( mapS . getOrDefault ( i , 0 ) > 0 || mapP . getOrDefault ( i , 0 ) > 0 ) { half1 . append ( i ) ; return half1 . append ( tmp ) . toString ( ) ; } } return half1 . append ( tmp ) . toString ( ) ; } public static void main ( String [ ] args ) { StringBuilder S = new StringBuilder ( " aeabb " ) ; StringBuilder P = new StringBuilder ( " dfedf " ) ; System . out . println ( mergePalindromes ( S , P ) ) ; } }
import java . util . * ; class GFG { static int countOfSubarray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) arr [ i ] -- ; int [ ] pref = new int [ N ] ; pref [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) pref [ i ] = pref [ i - 1 ] + arr [ i ] ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int answer = 0 ; mp . put ( 0 , 1 ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( mp . containsKey ( pref [ i ] ) ) { answer += mp . get ( pref [ i ] ) ; mp . put ( pref [ i ] , mp . get ( pref [ i ] ) + 1 ) ; } else { mp . put ( pref [ i ] , 1 ) ; } } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 } ; int N = arr . length ; System . out . print ( countOfSubarray ( arr , N ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static void printsubset ( int n , int k ) { int count = 0 , x = 0 ; ArrayList < Integer > vec = new ArrayList < > ( ) ; while ( n != 0 ) { x = n & 1 ; if ( x != 0 ) { vec . add ( ( int ) Math . pow ( k , count ) ) ; } n = n >> 1 ; count ++ ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) System . out . print ( vec . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 7 , k = 4 ; printsubset ( n , k ) ; } }
import java . util . * ; class GFG { static int solve ( String s , int x , int y ) { int res = 0 ; for ( char c : s . toCharArray ( ) ) { if ( c - '0' == x ) { res ++ ; x = x + y ; y = x - y ; x = x - y ; } } if ( x != y && res % 2 == 1 ) -- res ; return res ; } static int find_min ( String s ) { int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < 10 ; j ++ ) { count = Math . max ( count , solve ( s , i , j ) ) ; } } return count ; } public static void main ( String [ ] args ) { String s = "100120013" ; int n = s . length ( ) ; int answer = find_min ( s ) ; System . out . print ( ( n - answer ) ) ; } }
import java . util . * ; class GFG { static final int N = 10000 ; @ SuppressWarnings ( " unchecked " ) static Vector < Integer > [ ] adj = new Vector [ N ] ; static int used [ ] = new int [ N ] ; static int max_matching ; static void AddEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } static void Matching_dfs ( int u , int p ) { for ( int i = 0 ; i < adj [ u ] . size ( ) ; i ++ ) { if ( adj [ u ] . get ( i ) != p ) { Matching_dfs ( adj [ u ] . get ( i ) , u ) ; } } if ( used [ u ] == 0 && used [ p ] == 0 && p != 0 ) { max_matching ++ ; used [ u ] = used [ p ] = 1 ; } } static void maxMatching ( ) { Matching_dfs ( 1 , 0 ) ; System . out . print ( max_matching + "NEW_LINE"); } public static void main ( String [ ] args ) { for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; AddEdge ( 1 , 2 ) ; AddEdge ( 1 , 3 ) ; AddEdge ( 3 , 4 ) ; AddEdge ( 3 , 5 ) ; maxMatching ( ) ; } }
import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int find_max ( Vector < pair > v , int n ) { int count = 0 ; if ( n >= 2 ) count = 2 ; else count = 1 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( v . get ( i - 1 ) . first < ( v . get ( i ) . first - v . get ( i ) . second ) ) count ++ ; else if ( v . get ( i + 1 ) . first > ( v . get ( i ) . first + v . get ( i ) . second ) ) { count ++ ; v . get ( i ) . first = v . get ( i ) . first + v . get ( i ) . second ; } else continue ; } return count ; } public static void main ( String [ ] args ) { int n = 3 ; Vector < pair > v = new Vector < > ( ) ; v . add ( new pair ( 10 , 20 ) ) ; v . add ( new pair ( 15 , 10 ) ) ; v . add ( new pair ( 20 , 16 ) ) ; System . out . print ( find_max ( v , n ) ) ; } }
class GCD { static String gcd ( String str1 , String str2 ) { if ( str1 . length ( ) < str2 . length ( ) ) { return gcd ( str2 , str1 ) ; } else if ( ! str1 . startsWith ( str2 ) ) { return " " ; } else if ( str2 . isEmpty ( ) ) { return str1 ; } else { return gcd ( str1 . substring ( str2 . length ( ) ) , str2 ) ; } } static String findGCD ( String arr [ ] , int n ) { String result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { result = gcd ( result , arr [ i ] ) ; } return result ; } public static void main ( String [ ] args ) { String arr [ ] = new String [ ] { " GFGGFG " , " GFGGFG " , " GFGGFGGFGGFG " } ; int n = arr . length ; System . out . println ( findGCD ( arr , n ) ) ; } }
import java . util . * ; class GFG { static final int N = 4 ; static final int M = 4 ; static String balancedMatrix ( int mat [ ] [ ] ) { boolean is_balanced = true ; for ( int i = 0 ; i < N && is_balanced ; i ++ ) { for ( int j = 0 ; j < M && is_balanced ; j ++ ) { if ( ( i == 0 i == N - 1 ) && ( j == 0 j == M - 1 ) ) { if ( mat [ i ] [ j ] >= 2 ) is_balanced = false ; } else if ( i == 0 i == N - 1 j == 0 j == M - 1 ) { if ( mat [ i ] [ j ] >= 3 ) is_balanced = false ; } else { if ( mat [ i ] [ j ] >= 4 ) is_balanced = false ; } } } if ( is_balanced ) return " Balanced " ; else return " Unbalanced " ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 3 , 5 , 2 , 6 } , { 5 , 3 , 6 , 1 } , { 9 , 5 , 6 , 0 } } ; System . out . print ( balancedMatrix ( mat ) ) ; } }
import java . util . * ; class GFG { static String unixTimeToHumanReadable ( int seconds ) { String ans = " " ; int daysOfMonth [ ] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 } ; int currYear , daysTillNow , extraTime , extraDays , index , date , month , hours , minutes , secondss , flag = 0 ; daysTillNow = seconds / ( 24 * 60 * 60 ) ; extraTime = seconds % ( 24 * 60 * 60 ) ; currYear = 1970 ; while ( daysTillNow >= 365 ) { if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) { daysTillNow -= 366 ; } else { daysTillNow -= 365 ; } currYear += 1 ; } extraDays = daysTillNow + 1 ; if ( currYear % 400 == 0 || ( currYear % 4 == 0 && currYear % 100 != 0 ) ) flag = 1 ; month = 0 ; index = 0 ; if ( flag == 1 ) { while ( true ) { if ( index == 1 ) { if ( extraDays - 29 < 0 ) break ; month += 1 ; extraDays -= 29 ; } else { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; } index += 1 ; } } else { while ( true ) { if ( extraDays - daysOfMonth [ index ] < 0 ) { break ; } month += 1 ; extraDays -= daysOfMonth [ index ] ; index += 1 ; } } if ( extraDays > 0 ) { month += 1 ; date = extraDays ; } else { if ( month == 2 && flag == 1 ) date = 29 ; else { date = daysOfMonth [ month - 1 ] ; } } hours = extraTime / 3600 ; minutes = ( extraTime % 3600 ) / 60 ; secondss = ( extraTime % 3600 ) % 60 ; ans += String . valueOf ( date ) ; ans += " / " ; ans += String . valueOf ( month ) ; ans += " / " ; ans += String . valueOf ( currYear ) ; ans += " ▁ " ; ans += String . valueOf ( hours ) ; ans += " : " ; ans += String . valueOf ( minutes ) ; ans += " : " ; ans += String . valueOf ( secondss ) ; return ans ; } public static void main ( String [ ] args ) { int T = 1595497956 ; String ans = unixTimeToHumanReadable ( T ) ; System . out . print ( ans + "NEW_LINE"); } }
import java . io . * ; import java . util . * ; class GFG { static double AreaofRectangle ( int L , int W ) { double area = ( W + L ) * ( W + L ) / 2 ; return area ; } public static void main ( String args [ ] ) { int L = 18 ; int W = 12 ; System . out . println ( AreaofRectangle ( L , W ) ) ; } }
class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; int [ ] dp = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; dp [ 2 ] = 2 ; dp [ 3 ] = 3 ; int sqr ; for ( int i = 4 ; i <= n ; i ++ ) { sqr = ( int ) Math . sqrt ( i ) ; int best = Integer . MAX_VALUE ; while ( sqr > 1 ) { if ( i % sqr == 0 ) { best = Math . min ( best , 1 + dp [ sqr ] ) ; } sqr -- ; } best = Math . min ( best , 1 + dp [ i - 1 ] ) ; dp [ i ] = best ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( downToZero ( n ) ) ; } }
class GFG { static int downToZero ( int n ) { if ( n <= 3 ) return n ; return n % 2 == 0 ? 3 : 4 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( downToZero ( n ) ) ; } }
class GFG { static int minimumrequired ( int A [ ] , int N ) { int K = N ; int ans = 0 ; while ( K > 0 ) { if ( K % 2 == 1 ) { ans = K ; break ; } int ispalindrome = 1 ; for ( int i = 0 ; i < K / 2 ; i ++ ) { if ( A [ i ] != A [ K - 1 - i ] ) ispalindrome = 0 ; } if ( ispalindrome == 1 ) { ans = K / 2 ; K /= 2 ; } else { ans = K ; break ; } } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 1 , 1 , 2 , 2 , 1 } ; int N = a . length ; System . out . println ( minimumrequired ( a , N ) ) ; } }
class GFG { static int getMaxDifference ( int N ) { int M = - 1 ; int maxDiff = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int diff = ( N ^ i ) - ( N & i ) ; if ( diff >= maxDiff ) { maxDiff = diff ; M = i ; } } return M ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( getMaxDifference ( N ) ) ; } }
import java . util . * ; class GFG { static double cont [ ] [ ] = new double [ 1000 ] [ 1000 ] ; static void num_of_containers ( int n , double x ) { int count = 0 ; cont [ 1 ] [ 1 ] = x ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { if ( cont [ i ] [ j ] >= ( double ) 1 ) { count ++ ; cont [ i + 1 ] [ j ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; cont [ i + 1 ] [ j + 1 ] += ( cont [ i ] [ j ] - ( double ) 1 ) / ( double ) 2 ; } } } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int n = 3 ; double x = 5 ; num_of_containers ( n , x ) ; } }
import java . util . * ; class GFG { public static boolean findTriplet ( int [ ] arr ) { int n = arr . length ; Stack < Integer > st = new Stack < > ( ) ; int h3 = Integer . MIN_VALUE ; int h1 = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { h1 = arr [ i ] ; while ( ! st . empty ( ) && st . peek ( ) < arr [ i ] ) { h3 = st . peek ( ) ; st . pop ( ) ; } st . push ( arr [ i ] ) ; if ( h1 < h3 ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 5 , 6 } ; if ( findTriplet ( arr ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
import java . util . * ; class GFG { static void printVector ( ArrayList < Integer > arr ) { if ( arr . size ( ) != 1 ) { for ( int i = 0 ; i < arr . size ( ) ; i ++ ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } } static void findWays ( ArrayList < Integer > arr , int i , int n ) { if ( n == 0 ) printVector ( arr ) ; for ( int j = i ; j <= n ; j ++ ) { arr . add ( j ) ; findWays ( arr , j , n - j ) ; arr . remove ( arr . size ( ) - 1 ) ; } } public static void main ( String [ ] args ) { int n = 4 ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; findWays ( arr , 1 , n ) ; } }
import java . util . * ; class GFG { static int distinctNumbers ( int arr [ ] , int m , int n ) { Map < Integer , Integer > count = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) count . put ( arr [ i ] , count . getOrDefault ( arr [ i ] , 0 ) + 1 ) ; int [ ] fre_arr = new int [ n + 1 ] ; for ( Integer it : count . values ( ) ) { fre_arr [ it ] ++ ; } int ans = count . size ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = fre_arr [ i ] ; if ( temp == 0 ) continue ; int t = Math . min ( temp , m / i ) ; ans -= t ; m -= i * t ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 1 , 5 , 3 , 5 , 1 , 3 } ; int n = arr . length ; int m = 2 ; System . out . println ( distinctNumbers ( arr , m , n ) ) ; } }
class GFG { static int M = 4 ; static int N = 5 ; public static void no_of_moves ( int [ ] [ ] Matrix , int x , int y ) { int moves = 0 ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( Matrix [ i ] [ j ] != 0 ) { moves += Math . abs ( x - i ) ; moves += Math . abs ( y - j ) ; } } } System . out . println ( moves ) ; } public static void main ( String [ ] args ) { int x = 3 ; int y = 2 ; int [ ] [ ] Matrix = { { 1 , 0 , 1 , 1 , 0 } , { 0 , 1 , 1 , 0 , 1 } , { 0 , 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 0 , 0 } } ; int num = 1 ; no_of_moves ( Matrix , x , y ) ; } }
import java . util . HashMap ; import java . util . Map ; public class Main { public static void Maximum_subsequence ( int [ ] A , int N ) { HashMap < Integer , Integer > frequency = new HashMap < > ( ) ; int max_freq = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( frequency . containsKey ( A [ i ] ) ) { frequency . replace ( A [ i ] , frequency . get ( A [ i ] ) + 1 ) ; } else { frequency . put ( A [ i ] , 1 ) ; } } for ( Map . Entry it : frequency . entrySet ( ) ) { if ( ( int ) it . getValue ( ) > max_freq ) { max_freq = ( int ) it . getValue ( ) ; } } System . out . println ( max_freq ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 6 , 5 , 2 , 4 , 5 , 2 } ; int N = arr . length ; Maximum_subsequence ( arr , N ) ; } }
class GFG { static void removeAll ( int arr [ ] , int n ) { if ( arr [ 0 ] < arr [ n - 1 ] ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 10 , 4 , 7 , 1 , 3 , 6 } ; int size = Arr . length ; removeAll ( Arr , size ) ; } }
import java . util . * ; class GFG { public static void FindSubarray ( int [ ] arr , int n ) { if ( n == 1 ) { System . out . println ( " No ▁ such ▁ subarray ! " ) ; } int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , - 1 ) ; vis [ arr [ 0 ] ] = 0 ; int len = Integer . MAX_VALUE , flag = 0 ; int start = 0 , end = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int t = arr [ i ] ; if ( vis [ t ] != - 1 ) { int distance = i - vis [ t ] + 1 ; if ( distance < len ) { len = distance ; start = vis [ t ] ; end = i ; } flag = 1 ; } vis [ t ] = i ; } if ( flag == 0 ) System . out . println ( " No ▁ such ▁ subarray ! " ) ; else { for ( int i = start ; i <= end ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 2 , 4 , 5 } ; int n = arr . length ; FindSubarray ( arr , n ) ; } }
import java . util . * ; class GFG { public static int countSubstrings ( String s ) { int n = s . length ( ) ; int answer = ( n * ( n - 1 ) ) / 2 ; int cnt = 1 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i - 1 ) ) cnt ++ ; else { v . add ( cnt ) ; cnt = 1 ; } } if ( cnt > 0 ) v . add ( cnt ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { answer -= ( v . get ( i ) + v . get ( i + 1 ) - 1 ) ; } return answer ; } public static void main ( String [ ] args ) { String s = "00111" ; System . out . print ( countSubstrings ( s ) ) ; } }
class GFG { public static int get_palindrome_time ( String str ) { int hh , mm ; hh = ( str . charAt ( 0 ) - 48 ) * 10 + ( str . charAt ( 1 ) - 48 ) ; mm = ( str . charAt ( 3 ) - 48 ) * 10 + ( str . charAt ( 4 ) - 48 ) ; int requiredTime = 0 ; while ( hh % 10 != mm / 10 hh / 10 != mm % 10 ) { ++ mm ; if ( mm == 60 ) { mm = 0 ; ++ hh ; } if ( hh == 24 ) hh = 0 ; ++ requiredTime ; } return requiredTime ; } public static void main ( String [ ] args ) { String str = "05:39" ; System . out . println ( get_palindrome_time ( str ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int maximumSubarraySum ( int a [ ] , int n , ArrayList < List < Integer > > subarrays ) { int i , maxsum = 0 ; int [ ] prefixArray = new int [ n ] ; for ( i = 0 ; i < subarrays . size ( ) ; ++ i ) { prefixArray [ subarrays . get ( i ) . get ( 0 ) - 1 ] ++ ; prefixArray [ subarrays . get ( i ) . get ( 1 ) ] -- ; } for ( i = 1 ; i < n ; i ++ ) { prefixArray [ i ] += prefixArray [ i - 1 ] ; } Arrays . sort ( prefixArray ) ; Arrays . sort ( a ) ; for ( i = 0 ; i < n ; i ++ ) maxsum += a [ i ] * prefixArray [ i ] ; return maxsum ; } public static void main ( String [ ] args ) { int n = 6 ; int a [ ] = { 4 , 1 , 2 , 1 , 9 , 2 } ; ArrayList < List < Integer > > subarrays = new ArrayList < > ( ) ; subarrays . add ( Arrays . asList ( 1 , 2 ) ) ; subarrays . add ( Arrays . asList ( 1 , 3 ) ) ; subarrays . add ( Arrays . asList ( 1 , 4 ) ) ; subarrays . add ( Arrays . asList ( 3 , 4 ) ) ; System . out . println ( maximumSubarraySum ( a , n , subarrays ) ) ; } }
import java . util . * ; class GFG { public static void DivideString ( String s , int n , int k ) { int i , c = 0 , no = 1 ; int c1 = 0 , c2 = 0 ; int [ ] fr = new int [ 26 ] ; char [ ] ans = new char [ n ] ; for ( i = 0 ; i < n ; i ++ ) { fr [ s . charAt ( i ) - ' a ' ] ++ ; } char ch = ' a ' , ch1 = ' a ' ; for ( i = 0 ; i < 26 ; i ++ ) { if ( fr [ i ] == k ) { c ++ ; } if ( fr [ i ] > k && fr [ i ] != 2 * k ) { c1 ++ ; ch = ( char ) ( i + ' a ' ) ; } if ( fr [ i ] == 2 * k ) { c2 ++ ; ch1 = ( char ) ( i + ' a ' ) ; } } for ( i = 0 ; i < n ; i ++ ) ans [ i ] = '1' ; HashMap < Character , Integer > mp = new HashMap < > ( ) ; if ( c % 2 == 0 c1 > 0 c2 > 0 ) { for ( i = 0 ; i < n ; i ++ ) { if ( fr [ s . charAt ( i ) - ' a ' ] == k ) { if ( mp . containsKey ( s . charAt ( i ) ) ) { ans [ i ] = '2' ; } else { if ( no <= ( c / 2 ) ) { ans [ i ] = '2' ; no ++ ; mp . replace ( s . charAt ( i ) , 1 ) ; } } } } if ( ( c % 2 == 1 ) && ( c1 > 0 ) ) { no = 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ch && no <= k ) { ans [ i ] = '2' ; no ++ ; } } } if ( c % 2 == 1 && c1 == 0 ) { no = 1 ; int flag = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ch1 && no <= k ) { ans [ i ] = '2' ; no ++ ; } if ( fr [ s . charAt ( i ) - ' a ' ] == k && flag == 0 && ans [ i ] == '1' ) { ans [ i ] = '2' ; flag = 1 ; } } } System . out . println ( ans ) ; } else { System . out . println ( " NO " ) ; } } public static void main ( String [ ] args ) { String S = " abbbccc " ; int N = S . length ( ) ; int K = 1 ; DivideString ( S , N , K ) ; } }
class GFG { public static boolean isPalindrome ( int l , int r , String s ) { while ( l <= r ) { if ( s . charAt ( l ) != s . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } public static int numWays ( String s ) { int n = s . length ( ) ; int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isPalindrome ( 0 , i , s ) && isPalindrome ( i + 1 , n - 1 , s ) ) { ans ++ ; } } return ans ; } public static void main ( String args [ ] ) { String S = " aaaaa " ; System . out . println ( numWays ( S ) ) ; } }
import java . util . Arrays ; class GFG { public static void numberofsubstrings ( String str , int k , char charArray [ ] ) { int N = str . length ( ) ; int available [ ] = new int [ 26 ] ; Arrays . fill ( available , 0 ) ; for ( int i = 0 ; i < k ; i ++ ) { available [ charArray [ i ] - ' a ' ] = 1 ; } int lastPos = - 1 ; int ans = ( N * ( N + 1 ) ) / 2 ; for ( int i = 0 ; i < N ; i ++ ) { if ( available [ str . charAt ( i ) - ' a ' ] == 0 ) { ans -= ( ( i - lastPos ) * ( N - i ) ) ; lastPos = i ; } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { String str = " abcb " ; int k = 2 ; char [ ] charArray = { ' a ' , ' b ' } ; numberofsubstrings ( str , k , charArray ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int getMinJumps ( String s ) { Vector < Integer > ones = new Vector < Integer > ( ) ; int jumps = 0 , median = 0 , ind = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) ones . add ( i ) ; } if ( ones . size ( ) == 0 ) return jumps ; median = ( int ) ones . get ( ones . size ( ) / 2 ) ; ind = median ; for ( int i = ind ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) { jumps += ind - i ; ind -- ; } } ind = median ; for ( int i = ind ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' ) { jumps += i - ind ; ind ++ ; } } return jumps ; } public static void main ( String [ ] args ) { String S = "00100000010011" ; System . out . println ( getMinJumps ( S ) ) ; } }
import java . util . * ; class GFG { static String check ( int S , int prices [ ] , int type [ ] , int n ) { for ( int j = 0 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( ( type [ j ] == 0 && type [ k ] == 1 ) || ( type [ j ] == 1 && type [ k ] == 0 ) ) { if ( prices [ j ] + prices [ k ] <= S ) { return " Yes " ; } } } } return " No " ; } public static void main ( String [ ] args ) { int prices [ ] = { 3 , 8 , 6 , 5 } ; int type [ ] = { 0 , 1 , 1 , 0 } ; int S = 10 ; int n = 4 ; System . out . print ( check ( S , prices , type , n ) ) ; } }
import java . util . * ; import java . lang . * ; class GFG { static int numPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 2 , 3 , 3 } ; int n = arr . length ; System . out . println ( numPairs ( arr , n ) ) ; } }
import java . util . * ; class GFG { static int BalancedPartition ( String str , int n ) { if ( n == 0 ) return 0 ; int r = 0 , l = 0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' R ' ) { r ++ ; } else if ( str . charAt ( i ) == ' L ' ) { l ++ ; } if ( r == l ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String str = " LLRRRLLRRL " ; int n = str . length ( ) ; System . out . print ( BalancedPartition ( str , n ) + "NEW_LINE"); } }
class GFG { static int minOccupiedPosition ( int A [ ] , int n ) { int minPos = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] > 0 ) { ++ minPos ; i += 2 ; } } return minPos ; } public static void main ( String [ ] args ) { int A [ ] = { 8 , 0 , 7 , 0 , 0 , 6 } ; int n = A . length ; System . out . print ( minOccupiedPosition ( A , n ) ) ; } }
import java . io . * ; import java . lang . Math ; class GFG { static int findMinimumK ( int a [ ] , int n , int S ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return ( int ) Math . ceil ( ( ( S + 1 ) * 1.0 ) / ( sum * 1.0 ) ) ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 7 , 8 , 10 , 12 , 19 } ; int n = a . length ; int S = 200 ; System . out . print ( findMinimumK ( a , n , S ) ) ; } }
import java . util . * ; class GFG { static int largestNum ( int n ) { int num = 0 ; for ( int i = 0 ; i <= 32 ; i ++ ) { int x = ( 1 << i ) ; if ( ( x - 1 ) <= n ) num = ( 1 << i ) - 1 ; else break ; } return num ; } public static void main ( String args [ ] ) { int N = 345 ; System . out . print ( largestNum ( N ) ) ; } }
import java . io . * ; class GFG { static void findString ( int N , int K ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( ( char ) ( ' A ' + i % K ) ) ; } } public static void main ( String [ ] args ) { int N = 10 ; int K = 3 ; findString ( N , K ) ; } }
import java . io . * ; class GFG { static int count ( String s ) { int N , i , cnt = 0 , ans = 0 ; N = s . length ( ) ; for ( i = 0 ; i < N ; i ++ ) { if ( s . charAt ( i ) == ' R ' ) cnt ++ ; if ( s . charAt ( i ) == ' L ' ) ans += cnt ; } return ans ; } static public void main ( String [ ] args ) { String s = " RRLL " ; System . out . println ( count ( s ) ) ; } }
class GFG { static void segments ( int n ) { if ( n == 1 n == 0 ) { return ; } if ( n % 2 == 0 ) { System . out . print ( "1" ) ; segments ( n - 2 ) ; } else if ( n % 2 == 1 ) { System . out . print ( "7" ) ; segments ( n - 3 ) ; } } public static void main ( String [ ] args ) { int n ; n = 11 ; segments ( n ) ; } }
import java . util . * ; class GFG { static void horizontalFill ( int records [ ] , int tape [ ] , int nt ) { int sum = 0 ; int Retrieval_Time = 0 ; double Mrt ; int current = 0 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < nt ; i ++ ) { v . clear ( ) ; Retrieval_Time = 0 ; sum = 0 ; System . out . print ( " tape ▁ " + ( i + 1 ) + " ▁ : ▁ [ ▁ " ) ; sum += records [ current ] ; while ( sum <= tape [ i ] ) { System . out . print ( records [ current ] + " ▁ " ) ; v . add ( records [ current ] ) ; current ++ ; sum += records [ current ] ; } System . out . print ( " ] " ) ; for ( int j = 0 ; j < v . size ( ) ; j ++ ) { Retrieval_Time += v . get ( j ) * ( v . size ( ) - j ) ; } Mrt = ( double ) Retrieval_Time / v . size ( ) ; System . out . print ( " TABSYMBOL MRT ▁ : ▁ " + Mrt + "NEW_LINE"); } } public static void main ( String [ ] args ) { int records [ ] = { 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 } ; int tape [ ] = { 25 , 80 , 160 } ; int n = records . length ; int m = tape . length ; Arrays . sort ( records ) ; horizontalFill ( records , tape , m ) ; } }
class GFG { final static int MAX_SIZE = 10 ; static void convolution ( int [ ] x , int [ ] h , int n , int m ) { int row_vec [ ] = new int [ MAX_SIZE ] ; int col_vec [ ] = new int [ MAX_SIZE ] ; int out [ ] = new int [ MAX_SIZE ] ; int circular_shift_mat [ ] [ ] = new int [ MAX_SIZE ] [ MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; } System . out . print ( out [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int x [ ] = { 5 , 7 , 3 , 2 } ; int n = x . length ; int h [ ] = { 1 , 5 } ; int m = h . length ; convolution ( x , h , n , m ) ; } }
class GFG { static int max_palindrome ( String [ ] s , int n ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] . length ( ) % 2 != 0 ) { flag = 1 ; } } if ( flag == 1 ) { return n ; } int z = 0 ; int o = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < s [ i ] . length ( ) ; j ++ ) { if ( s [ i ] . charAt ( j ) == '0' ) z += 1 ; else o += 1 ; } } if ( o % 2 == 0 && z % 2 == 0 ) { return n ; } else { return n - 1 ; } } public static void main ( String [ ] args ) { int n = 3 ; String s [ ] = { "1110" , "100110" , "010101" } ; System . out . println ( max_palindrome ( s , n ) ) ; } }
class GFG { static int minCost ( int [ ] cost , int n ) { int totalCost = 0 ; int boardingBus = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( cost [ boardingBus ] > cost [ i ] ) { totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) ; boardingBus = i ; } } totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) ; return totalCost ; } public static void main ( String [ ] args ) { int [ ] cost = { 4 , 7 , 8 , 3 , 4 } ; int n = cost . length ; System . out . print ( minCost ( cost , n ) ) ; } }
class GFG { static int minFlipsSub ( String mat [ ] , int i , int j ) { int cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] . charAt ( j ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] . charAt ( j + 1 ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j + 1 ) == '1' ) cnt1 ++ ; else cnt0 ++ ; return Math . min ( cnt0 , cnt1 ) ; } static int minFlips ( String mat [ ] , int r , int c ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < r - 1 ; i ++ ) { for ( int j = 0 ; j < c - 1 ; j ++ ) { res = Math . min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } public static void main ( String [ ] args ) { String mat [ ] = { "0101" , "0101" , "0101" } ; int r = mat . length ; int c = mat [ 0 ] . length ( ) ; System . out . print ( minFlips ( mat , r , c ) ) ; } }
class GFG { static int findK ( int n , int k ) { int pos ; if ( n % 2 == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } static int countSetBits ( int n , int k ) { int kth = findK ( n , k ) ; int count = 0 ; while ( kth > 0 ) { count += kth & 1 ; kth >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 18 , k = 12 ; System . out . println ( countSetBits ( n , k ) ) ; } }
import java . util . * ; class GFG { static int minCost ( char [ ] str1 , char [ ] str2 , int n ) { int cost = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str1 [ i ] != str2 [ i ] ) { if ( i < n - 1 && str1 [ i + 1 ] != str2 [ i + 1 ] ) { swap ( str1 , i , i + 1 ) ; cost ++ ; } else { cost ++ ; } } } return cost ; } static void swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public static void main ( String [ ] args ) { String str1 = " abb " , str2 = " bba " ; int n = str1 . length ( ) ; System . out . println ( minCost ( str1 . toCharArray ( ) , str2 . toCharArray ( ) , n ) ) ; } }
import java . io . * ; class GFG { static void find_set ( int n ) { if ( n <= 2 ) { System . out . println ( " - 1" ) ; return ; } int sum1 = ( n * ( n - 1 ) ) / 2 ; int sum2 = n ; System . out . println ( sum1 + " ▁ " + sum2 ) ; } public static void main ( String [ ] args ) { int n = 8 ; find_set ( n ) ; } }
class GFG { static String get_maximum ( char [ ] s , int a [ ] ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] - '0' < a [ s [ i ] - '0' ] ) { int j = i ; while ( j < n && ( s [ j ] - '0' <= a [ s [ j ] - '0' ] ) ) { s [ j ] = ( char ) ( '0' + a [ s [ j ] - '0' ] ) ; j ++ ; } return String . valueOf ( s ) ; } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = "1337" ; int a [ ] = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 } ; System . out . println ( get_maximum ( s . toCharArray ( ) , a ) ) ; } }
import java . lang . Math ; public class GfG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . sqrt ( n ) ; n -= ( largest * largest ) ; steps ++ ; } return steps ; } public static void main ( String [ ] args ) { int n = 85 ; System . out . println ( countSteps ( n ) ) ; } }
import java . util . Arrays ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSum ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && k > 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; i ++ ; } if ( k % 2 == 1 ) { int min = 0 ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ min ] > arr [ i ] ) min = i ; arr [ min ] *= - 1 ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 5 , 4 , 1 , 3 , 2 } ; int n = arr . length ; int k = 4 ; System . out . println ( maxSum ( arr , n , k ) ) ; } }
class GFG { static int Maxsum ( int c1 , int c2 , int c3 , int c4 ) { int sum = 0 ; int two34 = Math . min ( c2 , Math . min ( c3 , c4 ) ) ; sum = two34 * 234 ; c2 -= two34 ; sum += Math . min ( c2 , c1 ) * 12 ; return sum ; } public static void main ( String [ ] args ) { int c1 = 5 , c2 = 2 , c3 = 3 , c4 = 4 ; System . out . println ( Maxsum ( c1 , c2 , c3 , c4 ) ) ; } }
class GFG { static void printArray ( int N , int arr [ ] ) { for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void replacedArray ( int N , int arr [ ] ) { int pos_sum , neg_sum , i , j , diff ; pos_sum = 0 ; neg_sum = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; if ( arr [ i ] > 0 ) pos_sum += arr [ i ] ; else neg_sum += arr [ i ] ; arr [ i ] = Math . abs ( diff ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , - 1 , 2 , 3 , - 2 } ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; int arr1 [ ] = { - 3 , - 4 , - 2 , 5 , 1 , - 2 } ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ; } }
import java . io . * ; class GFG { static int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; ans += n * ( a / n ) * ( b / n ) ; ans += ( a / n ) * ( b % n ) ; ans += ( a % n ) * ( b / n ) ; ans += ( ( a % n ) + ( b % n ) ) / n ; return ans ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . println ( findCountOfPairs ( a , b , n ) ) ; } }
class GFG { static void findArray ( int N , int P ) { int ans = ( P * ( P + 1 ) ) / 2 + ( N - P ) ; int arr [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= P ; i ++ ) { arr [ i ] = i ; } for ( int i = P + 1 ; i <= N ; i ++ ) { arr [ i ] = 1 ; } System . out . print ( " The ▁ Minimum ▁ Possible ▁ Sum ▁ is : ▁ " + ans + "NEW_LINE"); System . out . print ( "The Array Elements are: NEW_LINE"); for ( int i = 1 ; i <= N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 5 , P = 3 ; findArray ( N , P ) ; } }
class GFG { static String encryptString ( String str , int n ) { int i = 0 , cnt = 0 ; String encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str . charAt ( i ) ; i ++ ; } return encryptedStr ; } public static void main ( String [ ] args ) { String str = " geeks " ; int n = str . length ( ) ; System . out . println ( encryptString ( str , n ) ) ; } }
import java . io . * ; class GFG { static void findIntersection ( int intervals [ ] [ ] , int N ) { int l = intervals [ 0 ] [ 0 ] ; int r = intervals [ 0 ] [ 1 ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( intervals [ i ] [ 0 ] > r intervals [ i ] [ 1 ] < l ) { System . out . println ( - 1 ) ; return ; } else { l = Math . max ( l , intervals [ i ] [ 0 ] ) ; r = Math . min ( r , intervals [ i ] [ 1 ] ) ; } } System . out . println ( " [ " + l + " , ▁ " + r + " ] " ) ; } public static void main ( String [ ] args ) { int intervals [ ] [ ] = { { 1 , 6 } , { 2 , 8 } , { 3 , 10 } , { 5 , 8 } } ; int N = intervals . length ; findIntersection ( intervals , N ) ; } }
class GFG { static int cmp ( int a , int b ) { if ( a > b ) return 1 ; else if ( a == b ) return 0 ; else return - 1 ; } static int maxSubarraySize ( int [ ] arr , int n ) { int ans = 1 ; int anchor = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int c = cmp ( arr [ i - 1 ] , arr [ i ] ) ; if ( c == 0 ) anchor = i ; else if ( i == n - 1 || c * cmp ( arr [ i ] , arr [ i + 1 ] ) != - 1 ) { ans = Math . max ( ans , i - anchor + 1 ) ; anchor = i ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 4 , 2 , 10 , 7 , 8 , 8 , 1 , 9 } ; int n = arr . length ; System . out . println ( maxSubarraySize ( arr , n ) ) ; } }
class GFG { static int ValidPairs ( int arr [ ] , int n ) { int [ ] count = new int [ 121 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] += 1 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) continue ; if ( Math . abs ( arr [ i ] - arr [ j ] ) % 2 == 1 ) continue ; ans += count [ arr [ i ] ] * count [ arr [ j ] ] ; if ( arr [ i ] == arr [ j ] ) ans -= count [ arr [ i ] ] ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 16 , 17 , 18 } ; int n = arr . length ; System . out . println ( ValidPairs ( arr , n ) ) ; } }
import java . util . * ; class Main { public static void check_distribution ( int n , int k , int age [ ] , int candy [ ] ) { int mxage = age [ 0 ] ; for ( int i = 0 ; i < age . length ; i ++ ) { if ( mxage < age [ i ] ) { mxage = age [ i ] ; } } int mxcandy = candy [ 0 ] ; for ( int i = 0 ; i < candy . length ; i ++ ) { if ( mxcandy < candy [ i ] ) { mxcandy = candy [ i ] ; } } int fr1 [ ] = new int [ mxage + 1 ] ; Arrays . fill ( fr1 , 0 ) ; int fr2 [ ] = new int [ mxcandy + 1 ] ; Arrays . fill ( fr2 , 0 ) ; for ( int j = 0 ; j < n ; j ++ ) { fr1 [ age [ j ] ] += 1 ; } for ( int j = 0 ; j < k ; j ++ ) { fr2 [ candy [ j ] ] += 1 ; } k = 0 ; boolean Tf = true ; for ( int j = 0 ; j < mxage ; j ++ ) { if ( fr1 [ j ] == 0 ) continue ; boolean flag = false ; while ( k < mxcandy ) { if ( fr1 [ j ] <= fr2 [ k ] ) { flag = true ; break ; } k += 1 ; } k = k + 1 ; if ( flag == false ) { Tf = false ; break ; } } if ( Tf ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int age [ ] = { 5 , 15 , 10 } ; int candy [ ] = { 2 , 2 , 2 , 3 , 3 , 4 } ; int n = age . length ; int k = candy . length ; check_distribution ( n , k , age , candy ) ; } }
class GFG { static boolean checkPalindrome ( String s ) { String x = reverse ( s ) ; return x . equals ( s ) ; } static String findStringB ( String A ) { String B = reverse ( A ) ; B = B + A ; if ( checkPalindrome ( A ) ) { return A ; } return B ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String A = " ab " ; System . out . println ( findStringB ( A ) ) ; } }
import java . lang . * ; import java . util . * ; class GFG { static int minChanges ( int [ ] A , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { if ( ( i - 1 >= 0 ) && A [ i - 1 ] == 1 && A [ i + 1 ] == 1 && A [ i ] == 0 ) { A [ i + 1 ] = 0 ; cnt ++ ; } } return cnt ; } public static void main ( String args [ ] ) { int [ ] A = { 1 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 0 } ; int n = A . length ; System . out . print ( minChanges ( A , n ) ) ; } }
class GFG { static void completeSequence ( String s ) { int n = s . length ( ) ; int open = 0 , close = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) open ++ ; else close ++ ; if ( close > open ) { System . out . print ( " IMPOSSIBLE " ) ; return ; } } System . out . print ( s ) ; for ( int i = 0 ; i < open - close ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String [ ] args ) { String s = " ( ( ) ( ( ) ( " ; completeSequence ( s ) ; } }
class GFG { static void smallestPermute ( int n ) { char res [ ] = new char [ n + 1 ] ; if ( n % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } } else { for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } res [ n - 1 ] = ( char ) ( 48 + n - 2 ) ; res [ n - 2 ] = ( char ) ( 48 + n ) ; res [ n - 3 ] = ( char ) ( 48 + n - 1 ) ; } res [ n ] = ' \0' ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res [ i ] ) ; } } public static void main ( String [ ] args ) { int n = 7 ; smallestPermute ( n ) ; } }
class GFG { static int minInsertions ( int [ ] H , int n , int K ) { int inser = 0 ; for ( int i = 1 ; i < n ; ++ i ) { float diff = Math . abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += Math . ceil ( diff / K ) - 1 ; } return inser ; } public static void main ( String [ ] args ) { int [ ] H = new int [ ] { 2 , 4 , 8 , 16 } ; int K = 3 ; int n = H . length ; System . out . println ( minInsertions ( H , n , K ) ) ; } }
class GFG { static int count_minimum_operations ( long n ) { int count = 0 ; while ( n > 1 ) { if ( n % 3 == 0 ) n /= 3 ; else if ( n % 3 == 1 ) n -- ; else { if ( n == 2 ) n -- ; else n ++ ; } count ++ ; } return count ; } public static void main ( String [ ] args ) { long n = 4 ; long ans = count_minimum_operations ( n ) ; System . out . println ( ans ) ; } }
import java . util . * ; class GFG { static int minDiff ( int n , int x , int A [ ] ) { int mn = A [ 0 ] , mx = A [ 0 ] ; for ( int i = 0 ; i < n ; ++ i ) { mn = Math . min ( mn , A [ i ] ) ; mx = Math . max ( mx , A [ i ] ) ; } return Math . max ( 0 , mx - mn - 2 * x ) ; } public static void main ( String [ ] args ) { int n = 3 , x = 3 ; int A [ ] = { 1 , 3 , 6 } ; System . out . println ( minDiff ( n , x , A ) ) ; } }
import java . util . * ; class GFG { static void maxSum ( int [ ] a , int n ) { Vector < Integer > l = new Vector < Integer > ( ) ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += Math . abs ( a [ i ] ) ; if ( a [ i ] >= 0 ) continue ; if ( i == 0 ) l . add ( i + 1 ) ; else { l . add ( i + 1 ) ; l . add ( i ) ; } } System . out . println ( s ) ; for ( int i = 0 ; i < l . size ( ) ; i ++ ) System . out . print ( l . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] = { 1 , - 2 , - 3 , 4 } ; maxSum ( a , n ) ; } }
public class GFG { static void LengthLCP ( String x , String y ) { int fr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < b ; i ++ ) { fr [ y . charAt ( i ) - 97 ] += 1 ; } int c = 0 ; for ( int i = 0 ; i < a ; i ++ ) { if ( fr [ x . charAt ( i ) - 97 ] > 0 ) { c += 1 ; fr [ x . charAt ( i ) - 97 ] -= 1 ; } else break ; } System . out . println ( ( c ) ) ; } public static void main ( String args [ ] ) { String x = " here " , y = " there " ; LengthLCP ( x , y ) ; } }
import java . util . * ; class solution { static void CountPair ( int L , int R ) { int x = ( R - L + 1 ) ; System . out . println ( x / 2 + "NEW_LINE"); } public static void main ( String args [ ] ) { int L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ; } }
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int results ( int n , int k ) { return ( int ) Math . round ( Math . pow ( n , ( 1.0 / Math . pow ( 2.0 , k ) ) ) ) ; } public static void main ( String args [ ] ) { int k = 3 , n = 100000000 ; System . out . print ( " Chocolates ▁ left ▁ after ▁ " + k + " ▁ iterations ▁ are ▁ " + results ( n , k ) ) ; } }
class GFG { public static int [ ] getSubArray ( int [ ] arr , int n , int K ) { int i = - 1 ; int j = - 1 ; int currSum = 0 ; int [ ] result = { i , j , Math . abs ( K - Math . abs ( currSum ) ) } ; for ( i = 0 ; i < n ; i ++ ) { currSum = 0 ; for ( j = i ; j < n ; j ++ ) { currSum += arr [ j ] ; int currDev = Math . abs ( K - Math . abs ( currSum ) ) ; if ( currDev < result [ 2 ] ) { result [ 0 ] = i ; result [ 1 ] = j ; result [ 2 ] = currDev ; } if ( currDev == 0 ) return result ; } } return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 15 , - 3 , 5 , 2 , 7 , 6 , 34 , - 6 } ; int n = arr . length ; int K = 50 ; int [ ] ans = getSubArray ( arr , n , K ) ; if ( ans [ 0 ] == - 1 ) { System . out . println ( " The ▁ empty ▁ array ▁ " + " shows ▁ minimum ▁ Deviation " ) ; } else { for ( int i = ans [ 0 ] ; i <= ans [ 1 ] ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } } }
import java . util . Arrays ; class GFG { static void longestSubsequence ( int a [ ] , int n , int q [ ] , int m ) { Arrays . sort ( a ) ; int sum = 0 ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; double av = ( double ) ( sum ) / ( double ) ( i + 1 ) ; b [ i ] = ( ( int ) ( av + 1 ) ) ; } Arrays . sort ( b ) ; for ( int i = 0 ; i < m ; i ++ ) { int k = q [ i ] ; int longest = upperBound ( b , 0 , n , k ) ; System . out . println ( " Answer ▁ to ▁ Query " + ( i + 1 ) + " : ▁ " + longest ) ; } } private static int upperBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 5 , 4 } ; int n = a . length ; int q [ ] = { 4 , 2 , 1 , 5 } ; int m = q . length ; longestSubsequence ( a , n , q , m ) ; } }
import java . io . * ; class GFG { static int steps ( int N , int M ) { if ( N == 1 ) return 0 ; return M ; return 2 * M + ( N - 3 ) ; } public static void main ( String [ ] args ) { int N = 4 , M = 4 ; System . out . print ( steps ( N , M ) ) ; } }
import java . util . PriorityQueue ; class GFG { public static int DecreasingArray ( int a [ ] , int n ) { int sum = 0 , dif = 0 ; PriorityQueue < Integer > pq = new PriorityQueue < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! pq . isEmpty ( ) && pq . element ( ) < a [ i ] ) { dif = a [ i ] - pq . element ( ) ; sum += dif ; pq . remove ( ) ; } pq . add ( a [ i ] ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 1 , 2 , 1 } ; int n = a . length ; System . out . println ( DecreasingArray ( a , n ) ) ; } }
import java . util . Arrays ; class GFG { static void subsequence ( char s [ ] , char t [ ] , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = ' z ' ; ch >= ' a ' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) { t [ size ++ ] = ch ; new_last = i ; } } last = new_last ; } } t [ size ] = ' \0' ; } public static void main ( String [ ] args ) { char s [ ] = { ' b ' , ' a ' , ' n ' , ' a ' , ' n ' , ' a ' } ; int n = s . length ; int k = 2 ; char t [ ] = new char [ n ] ; subsequence ( s , t , n - 1 , k ) ; for ( int i = 0 ; i < t . length ; i ++ ) if ( t [ i ] != 0 ) System . out . print ( t [ i ] ) ; } }
public class BalanceParan { static long swapCount ( String s ) { char [ ] chars = s . toCharArray ( ) ; int countLeft = 0 , countRight = 0 ; int swap = 0 , imbalance = 0 ; for ( int i = 0 ; i < chars . length ; i ++ ) { if ( chars [ i ] == ' [ ' ) { countLeft ++ ; if ( imbalance > 0 ) { swap += imbalance ; imbalance -- ; } } else if ( chars [ i ] == ' ] ' ) { countRight ++ ; imbalance = ( countRight - countLeft ) ; } } return swap ; } public static void main ( String args [ ] ) { String s = " [ ] ] [ ] [ " ; System . out . println ( swapCount ( s ) ) ; s = " [ [ ] [ ] ] " ; System . out . println ( swapCount ( s ) ) ; } }
import java . util . HashMap ; import java . util . HashSet ; import java . util . Iterator ; class Test { static int pageFaults ( int pages [ ] , int n , int capacity ) { HashSet < Integer > s = new HashSet < > ( capacity ) ; HashMap < Integer , Integer > indexes = new HashMap < > ( ) ; int page_faults = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . size ( ) < capacity ) { if ( ! s . contains ( pages [ i ] ) ) { s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } else { if ( ! s . contains ( pages [ i ] ) ) { int lru = Integer . MAX_VALUE , val = Integer . MIN_VALUE ; Iterator < Integer > itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { int temp = itr . next ( ) ; if ( indexes . get ( temp ) < lru ) { lru = indexes . get ( temp ) ; val = temp ; } } s . remove ( val ) ; indexes . remove ( val ) ; s . add ( pages [ i ] ) ; page_faults ++ ; } indexes . put ( pages [ i ] , i ) ; } } return page_faults ; } public static void main ( String args [ ] ) { int pages [ ] = { 7 , 0 , 1 , 2 , 0 , 3 , 0 , 4 , 2 , 3 , 0 , 3 , 2 } ; int capacity = 4 ; System . out . println ( pageFaults ( pages , pages . length , capacity ) ) ; } }
import java . util . * ; class GFG { static void bestpermutation ( ArrayList < Integer > arr , int k , int n ) { HashMap < Integer , Integer > h = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { h . put ( arr . get ( i ) , i ) ; } if ( n <= k ) { Collections . sort ( arr , Collections . reverseOrder ( ) ) ; } else { for ( int j = n ; j >= 1 ; j -- ) { if ( k > 0 ) { int initial_index = h . get ( j ) ; int best_index = n - j ; if ( initial_index != best_index ) { h . put ( j , best_index ) ; int element = arr . get ( best_index ) ; h . put ( element , initial_index ) ; int temp = arr . get ( best_index ) ; arr . set ( best_index , arr . get ( initial_index ) ) ; arr . set ( initial_index , temp ) ; k -- ; } } } } } public static void main ( String [ ] args ) { ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 3 ) ; arr . add ( 1 ) ; arr . add ( 4 ) ; arr . add ( 2 ) ; arr . add ( 5 ) ; int k = 10 ; int n = arr . size ( ) ; bestpermutation ( arr , k , n ) ; System . out . print ( " Largest ▁ possible ▁ permutation ▁ after ▁ " + k + " ▁ swaps ▁ is ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr . get ( i ) + " ▁ " ) ; } }
public class GFG { static void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ ] = new int [ n ] ; for ( int i = 0 ; i < allocation . length ; i ++ ) allocation [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int bestIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { if ( bestIdx == - 1 ) bestIdx = j ; else if ( blockSize [ bestIdx ] > blockSize [ j ] ) bestIdx = j ; } } if ( bestIdx != - 1 ) { allocation [ i ] = bestIdx ; blockSize [ bestIdx ] -= processSize [ i ] ; } } System . out . println ( " Process No . Process Size Block no . "); for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + ( i + 1 ) + " TABSYMBOL TABSYMBOL " + processSize [ i ] + " TABSYMBOL TABSYMBOL " ) ; if ( allocation [ i ] != - 1 ) System . out . print ( allocation [ i ] + 1 ) ; else System . out . print ( " Not ▁ Allocated " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int blockSize [ ] = { 100 , 500 , 200 , 300 , 600 } ; int processSize [ ] = { 212 , 417 , 112 , 426 } ; int m = blockSize . length ; int n = processSize . length ; bestFit ( blockSize , m , processSize , n ) ; } }
class GFG { static int bestFit ( int weight [ ] , int n , int c ) { int res = 0 ; int [ ] bin_rem = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int min = c + 1 , bi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] < min ) { bi = j ; min = bin_rem [ j ] - weight [ i ] ; } } if ( min == c + 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ bi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int [ ] weight = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Best ▁ Fit ▁ : ▁ " + bestFit ( weight , n , c ) ) ; } }
class GFG { static int worstFit ( int weight [ ] , int n , int c ) { int res = 0 ; int bin_rem [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int j ; int mx = - 1 , wi = 0 ; for ( j = 0 ; j < res ; j ++ ) { if ( bin_rem [ j ] >= weight [ i ] && bin_rem [ j ] - weight [ i ] > mx ) { wi = j ; mx = bin_rem [ j ] - weight [ i ] ; } } if ( mx == - 1 ) { bin_rem [ res ] = c - weight [ i ] ; res ++ ; } bin_rem [ wi ] -= weight [ i ] ; } return res ; } public static void main ( String [ ] args ) { int weight [ ] = { 2 , 5 , 4 , 7 , 1 , 3 , 8 } ; int c = 10 ; int n = weight . length ; System . out . print ( " Number ▁ of ▁ bins ▁ required ▁ in ▁ Worst ▁ Fit ▁ : ▁ " + worstFit ( weight , n , c ) ) ; } }
class GFG { static int getMax ( int arr [ ] , int n ) { int result = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > result ) result = arr [ i ] ; return result ; } static boolean isPossible ( int time , int K , int job [ ] , int n ) { int cnt = 1 ; int curr_time = 0 ; for ( int i = 0 ; i < n ; ) { if ( curr_time + job [ i ] > time ) { curr_time = 0 ; cnt ++ ; } else { curr_time += job [ i ] ; i ++ ; } } return ( cnt <= K ) ; } static int findMinTime ( int K , int T , int job [ ] , int n ) { int end = 0 , start = 0 ; for ( int i = 0 ; i < n ; ++ i ) end += job [ i ] ; int ans = end ; int job_max = getMax ( job , n ) ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( mid >= job_max && isPossible ( mid , K , job , n ) ) { ans = Math . min ( ans , mid ) ; end = mid - 1 ; } else start = mid + 1 ; } return ( ans * T ) ; } public static void main ( String arg [ ] ) { int job [ ] = { 10 , 7 , 8 , 12 , 6 , 8 } ; int n = job . length ; int k = 4 , T = 5 ; System . out . println ( findMinTime ( k , T , job , n ) ) ; } }
class GFG { static void numberOfWays ( int n , int k ) { int [ ] dp = new int [ 1000 ] ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 0 ; } dp [ 0 ] = 1 ; for ( int i = 1 ; i <= k ; i ++ ) { int numWays = 0 ; for ( int j = 0 ; j < n ; j ++ ) { numWays += dp [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { dp [ j ] = numWays - dp [ j ] ; } } System . out . println ( dp [ 0 ] + "NEW_LINE"); } public static void main ( String args [ ] ) { int N = 5 , K = 3 ; numberOfWays ( N , K ) ; } }
import java . io . * ; class GFG { static int totalSubarrays ( int arr [ ] , int n , int k ) { int ans = 0 , i = 0 ; while ( i < n ) { if ( arr [ i ] > k ) { i ++ ; continue ; } int count = 0 ; while ( i < n && arr [ i ] <= k ) { i ++ ; count ++ ; } ans += ( ( count * ( count + 1 ) ) / 2 ) ; } return ans ; } static int countSubarrays ( int arr [ ] , int n , int k ) { int count1 = totalSubarrays ( arr , n , k - 1 ) ; int count2 = totalSubarrays ( arr , n , k ) ; int ans = count2 - count1 ; return ans ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; int arr [ ] = { 2 , 1 , 3 , 4 } ; System . out . println ( countSubarrays ( arr , n , k ) ) ; } }
import java . io . * ; class GFG { static int M = 1000000007 ; static int waysOfDecoding ( String s ) { long first = 1 , second = s . charAt ( 0 ) == ' * ' ? 9 : s . charAt ( 0 ) == '0' ? 0 : 1 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { long temp = second ; if ( s . charAt ( i ) == ' * ' ) { second = 9 * second ; if ( s . charAt ( i - 1 ) == '1' ) second = ( second + 9 * first ) % M ; else if ( s . charAt ( i - 1 ) == '2' ) second = ( second + 6 * first ) % M ; else if ( s . charAt ( i - 1 ) == ' * ' ) second = ( second + 15 * first ) % M ; } else { second = s . charAt ( i ) != '0' ? second : 0 ; if ( s . charAt ( i - 1 ) == '1' ) second = ( second + first ) % M ; else if ( s . charAt ( i - 1 ) == '2' && s . charAt ( i ) <= '6' ) second = ( second + first ) % M ; else if ( s . charAt ( i - 1 ) == ' * ' ) second = ( second + ( s . charAt ( i ) <= '6' ? 2 : 1 ) * first ) % M ; } first = temp ; } return ( int ) second ; } public static void main ( String [ ] args ) { String s = " * " ; System . out . println ( waysOfDecoding ( s ) ) ; } }
import java . io . * ; class GFG { public static int maxSumWO3Consec ( int A [ ] , int N ) { if ( N == 1 ) return A [ 0 ] ; if ( N == 2 ) return A [ 0 ] + A [ 1 ] ; int third = A [ 0 ] ; int second = third + A [ 1 ] ; int first = Math . max ( second , A [ 1 ] + A [ 2 ] ) ; int sum = Math . max ( Math . max ( third , second ) , first ) ; for ( int i = 3 ; i < N ; i ++ ) { sum = Math . max ( Math . max ( first , second + A [ i ] ) , third + A [ i ] + A [ i - 1 ] ) ; third = second ; second = first ; first = sum ; } return sum ; } public static void main ( String [ ] args ) { int A [ ] = { 3000 , 2000 , 1000 , 3 , 10 } ; int N = A . length ; int res = maxSumWO3Consec ( A , N ) ; System . out . println ( res ) ; } }
class GFG { public static void longestSubSequence ( int [ ] [ ] A , int N ) { int [ ] dp = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { dp [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( A [ j ] [ 0 ] < A [ i ] [ 0 ] && A [ j ] [ 1 ] > A [ i ] [ 1 ] ) { dp [ i ] = Math . max ( dp [ i ] , dp [ j ] + 1 ) ; } } } System . out . println ( dp [ N - 1 ] ) ; } public static void main ( String args [ ] ) { int [ ] [ ] A = { { 1 , 2 } , { 2 , 2 } , { 3 , 1 } } ; int N = A . length ; longestSubSequence ( A , N ) ; } }
public class GFG { static int findMinOperations ( int n ) { int i = 0 ; int dp [ ] = new int [ n + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { dp [ i ] = 999999 ; } dp [ 2 ] = 0 ; for ( i = 2 ; i < n + 1 ; i ++ ) { if ( i * 5 <= n ) { dp [ i * 5 ] = Math . min ( dp [ i * 5 ] , dp [ i ] + 1 ) ; } if ( i + 3 <= n ) { dp [ i + 3 ] = Math . min ( dp [ i + 3 ] , dp [ i ] + 1 ) ; } } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 28 ; int m = findMinOperations ( n ) ; if ( m != 9999 ) System . out . println ( m ) ; else System . out . println ( - 1 ) ; } }
public class Main { static int countSortedArrays ( int n , int m ) { int [ ] dp = new int [ ( m + 1 ) ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ j ] = dp [ j - 1 ] + dp [ j ] ; } } return dp [ m ] ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . print ( countSortedArrays ( n , m ) ) ; } }
import java . util . * ; class GFG { static int count0 ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '0' ) { count ++ ; } } return count ; } static int solve ( String [ ] vec , int A , int B , int idx ) { if ( idx == vec . length A + B == 0 ) { return 0 ; } int zero = count0 ( vec [ idx ] ) ; int one = vec [ idx ] . length ( ) - zero ; int inc = 0 ; if ( zero <= A && one <= B ) { inc = 1 + solve ( vec , A - zero , B - one , idx + 1 ) ; } int exc = solve ( vec , A , B , idx + 1 ) ; return Math . max ( inc , exc ) ; } static int MaxSubsetlength ( String [ ] arr , int A , int B ) { return solve ( arr , A , B , 0 ) ; } public static void main ( String [ ] args ) { String [ ] arr = { "1" , "0" , "10" } ; int A = 1 , B = 1 ; System . out . print ( MaxSubsetlength ( arr , A , B ) ) ; } }
import java . util . * ; class GFG { static boolean isUnique ( String s ) { HashSet < Character > set = new HashSet < > ( ) ; for ( char c : s . toCharArray ( ) ) set . add ( c ) ; return set . size ( ) == 1 ; } static int maxScore ( String s , int [ ] a ) { int n = s . length ( ) ; if ( n == 0 ) return 0 ; if ( n == 1 ) return a [ 0 ] ; int mx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { String sub = s . substring ( i , j + 1 ) ; if ( isUnique ( sub ) ) mx = Math . max ( mx , a [ sub . length ( ) - 1 ] + maxScore ( s . substring ( 0 , i ) + s . substring ( j + 1 ) , a ) ) ; } } return mx ; } public static void main ( String args [ ] ) { String s = "011" ; int a [ ] = { 1 , 3 , 1 } ; System . out . print ( maxScore ( s , a ) ) ; } }
import java . util . * ; class GFG { static int size = 100 ; static int [ ] [ ] ans = new int [ size ] [ size ] ; static void initialize ( ) { for ( int i = 0 ; i < size ; i ++ ) { for ( int j = 0 ; j < size ; j ++ ) { ans [ i ] [ j ] = 0 ; } } } static int numberOfUniqueOutcomes ( int n , int s ) { if ( s < n ) ans [ n ] [ s ] = 0 ; else if ( n == 1 n == s ) ans [ n ] [ s ] = 1 ; else if ( ans [ n ] [ s ] == 0 ) { ans [ n ] [ s ] = numberOfUniqueOutcomes ( n - 1 , s - 1 ) + numberOfUniqueOutcomes ( n - 1 , s - 2 ) ; } return ans [ n ] [ s ] ; } public static void main ( String args [ ] ) { initialize ( ) ; int N = 5 , S = 8 ; System . out . println ( numberOfUniqueOutcomes ( N , S ) ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void printMinimumRemovals ( String str , String X ) { int N = str . length ( ) ; int M = X . length ( ) ; int dp [ ] [ ] = new int [ N ] [ M ] ; for ( int j = 0 ; j < M ; j ++ ) { if ( str . charAt ( 0 ) == X . charAt ( j ) ) { dp [ 0 ] [ j ] = 1 ; } } for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( str . charAt ( i ) == X . charAt ( j ) ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; if ( j != 0 ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } else { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } System . out . println ( dp [ N - 1 ] [ M - 1 ] ) ; } public static void main ( String [ ] args ) { String str = " btagd " ; String X = " bad " ; printMinimumRemovals ( str , X ) ; } }
import java . util . * ; class GFG { static int dp [ ] = new int [ 100000 ] ; static int findWays ( int x ) { if ( x < 0 ) return 0 ; if ( x == 0 ) return 1 ; if ( x == 1 ) return 2 ; if ( x == 2 ) return 4 ; if ( dp [ x ] != - 1 ) return dp [ x ] ; int count = findWays ( x - 1 ) ; count += findWays ( x - 2 ) ; count += findWays ( x - 3 ) ; dp [ x ] = count ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 4 ; Arrays . fill ( dp , - 1 ) ; System . out . print ( findWays ( n ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int maxSubarraySum ( int [ ] sum , int k , int row ) { int curSum = 0 , curMax = Integer . MIN_VALUE ; Set < Integer > sumSet = new HashSet < Integer > ( ) ; sumSet . add ( 0 ) ; for ( int r = 0 ; r < row ; ++ r ) { curSum += sum [ r ] ; ArrayList < Integer > list = new ArrayList < Integer > ( ) ; list . addAll ( sumSet ) ; int it = Integer . MIN_VALUE ; for ( int e : list ) if ( e >= ( curSum - k ) ) { it = e ; break ; } if ( it != Integer . MIN_VALUE ) { curMax = Math . max ( curMax , curSum - it ) ; } sumSet . add ( curSum ) ; } return curMax ; } static void maxSumSubmatrix ( int [ ] [ ] matrix , int k ) { int row = matrix . length ; int col = matrix [ 0 ] . length ; int ret = Integer . MIN_VALUE ; for ( int i = 0 ; i < col ; ++ i ) { int [ ] sum = new int [ row ] ; for ( int j = i ; j < col ; ++ j ) { for ( int r = 0 ; r < row ; ++ r ) { sum [ r ] += matrix [ r ] [ j ] ; } int curMax = maxSubarraySum ( sum , k , row ) ; ret = Math . max ( ret , curMax ) ; } } System . out . print ( ret ) ; } public static void main ( String [ ] args ) { int [ ] [ ] matrix = { { 5 , - 4 , - 3 , 4 } , { - 3 , - 4 , 4 , 5 } , { 5 , 1 , 5 , - 4 } } ; int K = 8 ; maxSumSubmatrix ( matrix , K ) ; } }
import java . io . * ; import java . lang . * ; import java . util . * ; class GFG { static void minCost ( int costs [ ] [ ] , int N ) { if ( N == 0 ) return ; int dp [ ] [ ] = new int [ N ] [ 3 ] ; dp [ 0 ] [ 0 ] = costs [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = costs [ 0 ] [ 1 ] ; dp [ 0 ] [ 2 ] = costs [ 0 ] [ 2 ] ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] [ 0 ] = Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 0 ] ; dp [ i ] [ 1 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 2 ] ) + costs [ i ] [ 1 ] ; dp [ i ] [ 2 ] = Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) + costs [ i ] [ 2 ] ; } System . out . println ( Math . min ( dp [ N - 1 ] [ 0 ] , Math . min ( dp [ N - 1 ] [ 1 ] , dp [ N - 1 ] [ 2 ] ) ) ) ; } public static void main ( String [ ] args ) { int costs [ ] [ ] = { { 14 , 2 , 11 } , { 11 , 14 , 5 } , { 14 , 3 , 10 } } ; int N = costs . length ; minCost ( costs , N ) ; } }
class GFG { static int [ ] [ ] dp = new int [ 2001 ] [ 2001 ] ; static int solve ( int [ ] A , int i , int sum , int N ) { int res = 2001 ; if ( sum < 0 || ( i == N && sum != 0 ) ) { return 2001 ; } if ( sum == 0 i >= N ) { return dp [ i ] [ sum ] = 0 ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } res = Math . min ( solve ( A , i + 1 , sum - A [ i ] , N ) + 1 , solve ( A , i + 1 , sum , N ) ) ; return dp [ i ] [ sum ] = res ; } static void minOp ( int [ ] A , int N ) { int sum = 0 ; for ( int it : A ) { sum += it ; } if ( sum % 2 == 0 ) { for ( int i = 0 ; i < 2001 ; i ++ ) { for ( int j = 0 ; j < 2001 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } int ans = solve ( A , 0 , sum / 2 , N ) ; if ( ans < 0 ans > N ) { System . out . print ( " - 1" + "NEW_LINE"); } else { System . out . print ( ans + "NEW_LINE"); } } else { System . out . print ( " - 1" + "NEW_LINE"); } } public static void main ( String [ ] args ) { int [ ] A = { 2 , 3 , 1 , 4 } ; int N = A . length ; minOp ( A , N ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] [ ] dp = new int [ 101 ] [ 101 ] [ 1001 ] ; static int countAverage ( int n , int K , int [ ] arr ) { dp [ 0 ] [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int k = 0 ; k < n ; k ++ ) { for ( int s = 0 ; s <= 100 ; s ++ ) { dp [ i + 1 ] [ k + 1 ] [ s + arr [ i ] ] += dp [ i ] [ k ] [ s ] ; dp [ i + 1 ] [ k ] [ s ] += dp [ i ] [ k ] [ s ] ; } } } int cnt = 0 ; for ( int j = 1 ; j <= n ; j ++ ) { cnt += dp [ n ] [ j ] [ K * j ] ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 7 , 8 , 9 } ; int K = 8 ; int N = arr . length ; System . out . print ( countAverage ( N , K , arr ) ) ; } }
import java . util . * ; class GFG { static int [ ] [ ] [ ] [ ] dp = new int [ 50 ] [ 50 ] [ 50 ] [ 50 ] ; static int FindMaxS ( int X , int Y , int Z , int n , int [ ] A , int [ ] B , int [ ] C ) { if ( X < 0 Y < 0 Z < 0 ) return Integer . MIN_VALUE ; if ( n < 0 ) return 0 ; if ( dp [ n ] [ X ] [ Y ] [ Z ] != - 1 ) { return dp [ n ] [ X ] [ Y ] [ Z ] ; } int ch = A [ n ] + FindMaxS ( X - 1 , Y , Z , n - 1 , A , B , C ) ; int ca = B [ n ] + FindMaxS ( X , Y - 1 , Z , n - 1 , A , B , C ) ; int co = C [ n ] + FindMaxS ( X , Y , Z - 1 , n - 1 , A , B , C ) ; int no = FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ; int maximum = Math . max ( ch , Math . max ( ca , Math . max ( co , no ) ) ) ; return dp [ n ] [ X ] [ Y ] [ Z ] = maximum ; } public static void main ( String [ ] args ) { int X = 1 ; int Y = 1 ; int Z = 1 ; int [ ] A = { 10 , 0 , 5 } ; int [ ] B = { 5 , 10 , 0 } ; int [ ] C = { 0 , 5 , 10 } ; int n = B . length ; for ( int i = 0 ; i < 50 ; i ++ ) for ( int j = 0 ; j < 50 ; j ++ ) for ( int k = 0 ; k < 50 ; k ++ ) for ( int l = 0 ; l < 50 ; l ++ ) dp [ i ] [ j ] [ k ] [ l ] = - 1 ; System . out . print ( FindMaxS ( X , Y , Z , n - 1 , A , B , C ) ) ; } }
import java . util . * ; class GFG { static double find ( int N , int sum ) { if ( sum > 6 * N sum < N ) return 0 ; if ( N == 1 ) { if ( sum >= 1 && sum <= 6 ) return 1.0 / 6 ; else return 0 ; } double s = 0 ; for ( int i = 1 ; i <= 6 ; i ++ ) s = s + find ( N - 1 , sum - i ) / 6 ; return s ; } public static void main ( String [ ] args ) { int N = 4 , a = 13 , b = 17 ; double probability = 0.0 ; for ( int sum = a ; sum <= b ; sum ++ ) probability = probability + find ( N , sum ) ; System . out . format ( " % .6f " , probability ) ; } }
import java . io . * ; import java . util . * ; class GFG { static int MOD = 1000000007 ; static Integer array [ ] = { 1 , 2 , 4 , 8 , 16 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 } ; static Vector < Integer > MEM = new Vector < Integer > ( Arrays . asList ( array ) ) ; static int mod_pow2 ( int n ) { while ( n >= MEM . size ( ) ) MEM . add ( ( MEM . get ( MEM . size ( ) - 1 ) * 2 ) % MOD ) ; return MEM . get ( n ) ; } static int inversions ( char [ ] bstr ) { int total = 0 , zeros = 0 , questions = 0 ; int j = bstr . length - 1 ; for ( int i = 0 ; i < bstr . length / 2 ; i ++ ) { char temp = bstr [ i ] ; bstr [ i ] = bstr [ j ] ; bstr [ j ] = temp ; j -- ; } for ( char x : bstr ) { int q ; if ( x == '1' ) { int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; } else if ( x == '0' ) { zeros += 1 ; } else { total *= 2 ; int z = zeros * mod_pow2 ( questions ) ; if ( questions == 0 ) q = 0 ; else q = questions * mod_pow2 ( questions - 1 ) ; total = ( total + z + q ) % MOD ; questions += 1 ; } } return total ; } public static void main ( String [ ] args ) { char S [ ] = " ? 0 ? " . toCharArray ( ) ; System . out . println ( inversions ( S ) ) ; } }
import java . io . * ; import java . util . * ; class GFG { public static int minRemovalsUtil ( int [ ] arr ) { int result = 0 ; if ( arr . length < 3 ) { return - 1 ; } int [ ] leftIncreasing = new int [ arr . length ] ; int [ ] rightIncreasing = new int [ arr . length ] ; for ( int i = 1 ; i < arr . length ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { leftIncreasing [ i ] = Math . max ( leftIncreasing [ i ] , leftIncreasing [ j ] + 1 ) ; } } } for ( int i = arr . length - 2 ; i >= 0 ; i -- ) { for ( int j = arr . length - 1 ; j > i ; j -- ) { if ( arr [ j ] < arr [ i ] ) { rightIncreasing [ i ] = Math . max ( rightIncreasing [ i ] , rightIncreasing [ j ] + 1 ) ; } } } for ( int i = 0 ; i < arr . length ; i ++ ) { if ( leftIncreasing [ i ] != 0 && rightIncreasing [ i ] != 0 ) { result = Math . max ( result , leftIncreasing [ i ] + rightIncreasing [ i ] ) ; } } return arr . length - ( result + 1 ) ; } public static void minRemovals ( int [ ] arr ) { int ans = minRemovalsUtil ( arr ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 1 , 5 , 6 , 2 , 3 , 1 } ; minRemovals ( arr ) ; } }
import java . util . * ; import java . lang . * ; class Gfg { static void bfs ( int n , int [ ] a , ArrayList < ArrayList < Integer > > invGr , int [ ] ans , int parity ) { Queue < Integer > q = new LinkedList < > ( ) ; int [ ] vis = new int [ n + 1 ] ; int [ ] dist = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { if ( ( ( a [ i ] + parity ) & 1 ) != 0 ) { q . add ( i ) ; vis [ i ] = 1 ; } } while ( ! q . isEmpty ( ) ) { int v = q . peek ( ) ; q . poll ( ) ; for ( Integer u : invGr . get ( v ) ) { if ( vis [ u ] == 0 ) { dist [ u ] = dist [ v ] + 1 ; vis [ u ] = 1 ; if ( ( a [ u ] + parity ) % 2 == 0 ) { if ( ans [ u ] == - 1 ) ans [ u ] = dist [ u ] ; } q . add ( u ) ; } } } } static void minJumps ( int [ ] a , int [ ] jump , int n ) { ArrayList < ArrayList < Integer > > invGr = new ArrayList < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) invGr . add ( new ArrayList < Integer > ( ) ) ; int [ ] ans = new int [ n + 1 ] ; Arrays . fill ( ans , - 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i + jump [ i ] >= 1 && i + jump [ i ] <= n ) { invGr . get ( i + jump [ i ] ) . add ( i ) ; } if ( i - jump [ i ] >= 1 && i - jump [ i ] <= n ) { invGr . get ( i - jump [ i ] ) . add ( i ) ; } } bfs ( n , a , invGr , ans , 0 ) ; bfs ( n , a , invGr , ans , 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 0 , 4 , 2 , 5 , 2 , 1 } ; int [ ] jump = { 0 , 1 , 2 , 3 , 1 , 2 } ; int N = arr . length ; minJumps ( arr , jump , N - 1 ) ; } }
import java . util . * ; class GFG { static ArrayList < ArrayList < ArrayList < Integer > > > dp ; static int countPaths = 0 ; static boolean isPerfectSquare ( int n ) { double sr = Math . sqrt ( n ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static void countUniquePaths ( int a [ ] [ ] , int m , int n , int ans ) { dp . get ( 0 ) . get ( 0 ) . add ( a [ 0 ] [ 0 ] ) ; for ( int i = 1 ; i < m ; i ++ ) { a [ i ] [ 0 ] *= a [ i - 1 ] [ 0 ] ; dp . get ( i ) . get ( 0 ) . add ( a [ i ] [ 0 ] ) ; } for ( int i = 1 ; i < n ; i ++ ) { a [ 0 ] [ i ] *= a [ 0 ] [ i - 1 ] ; dp . get ( 0 ) . get ( i ) . add ( a [ 0 ] [ i ] ) ; } for ( int i = 1 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { ArrayList < Integer > top = dp . get ( i - 1 ) . get ( j ) ; ArrayList < Integer > left = dp . get ( i ) . get ( j - 1 ) ; ArrayList < Integer > curr = new ArrayList < > ( ) ; for ( int k = 0 ; k < top . size ( ) ; k ++ ) { curr . add ( top . get ( k ) * a [ i ] [ j ] ) ; } for ( int k = 0 ; k < left . size ( ) ; k ++ ) { curr . add ( left . get ( k ) * a [ i ] [ j ] ) ; } dp . get ( i ) . set ( j , curr ) ; } } for ( Integer i : dp . get ( m - 1 ) . get ( n - 1 ) ) { if ( isPerfectSquare ( i ) ) { countPaths ++ ; } } } public static void main ( String [ ] args ) { int M = 3 , N = 4 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 1 } , { 3 , 1 , 2 , 4 } , { 2 , 3 , 1 , 1 } } ; dp = new ArrayList < > ( ) ; for ( int i = 0 ; i < 105 ; i ++ ) { dp . add ( new ArrayList < > ( ) ) ; for ( int j = 0 ; j < 105 ; j ++ ) dp . get ( i ) . add ( new ArrayList < Integer > ( ) ) ; } countUniquePaths ( mat , M , N , 1 ) ; System . out . println ( countPaths ) ; } }
import java . util . * ; class GFG { static final int MAX = 100000 ; static int [ ] dp = new int [ MAX + 1 ] ; static List < Integer > denomination = new LinkedList < Integer > ( ) ; static int countMinCoins ( int n , int C [ ] , int m ) { if ( n == 0 ) { dp [ 0 ] = 0 ; return 0 ; } if ( dp [ n ] != - 1 ) return dp [ n ] ; int ret = Integer . MAX_VALUE ; for ( int i = 0 ; i < m ; i ++ ) { if ( C [ i ] <= n ) { int x = countMinCoins ( n - C [ i ] , C , m ) ; if ( x != Integer . MAX_VALUE ) ret = Math . min ( ret , 1 + x ) ; } } dp [ n ] = ret ; return ret ; } static void findSolution ( int n , int C [ ] , int m ) { if ( n == 0 ) { for ( int it : denomination ) { System . out . print ( it + " ▁ " ) ; } return ; } for ( int i = 0 ; i < m ; i ++ ) { if ( n - C [ i ] >= 0 && dp [ n - C [ i ] ] + 1 == dp [ n ] ) { denomination . add ( C [ i ] ) ; findSolution ( n - C [ i ] , C , m ) ; break ; } } } static void countMinCoinsUtil ( int X , int C [ ] , int N ) { for ( int i = 0 ; i < dp . length ; i ++ ) dp [ i ] = - 1 ; int isPossible = countMinCoins ( X , C , N ) ; if ( isPossible == Integer . MAX_VALUE ) { System . out . print ( " - 1" ) ; } else { findSolution ( X , C , N ) ; } } public static void main ( String [ ] args ) { int X = 21 ; int arr [ ] = { 2 , 3 , 4 , 5 } ; int N = arr . length ; countMinCoinsUtil ( X , arr , N ) ; } }
import java . util . * ; class GFG { static void findWays ( int N ) { int [ ] dp = new int [ N + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 6 ; j ++ ) { if ( i - j >= 0 ) { dp [ i ] = dp [ i ] + dp [ i - j ] ; } } } System . out . print ( dp [ N ] ) ; } public static void main ( String [ ] args ) { int N = 4 ; findWays ( N ) ; } }
import java . util . * ; class GFG { static int binCoff ( int n , int r ) { int val = 1 ; int i ; if ( r > ( n - r ) ) { r = ( n - r ) ; } for ( i = 0 ; i < r ; i ++ ) { val *= ( n - i ) ; val /= ( i + 1 ) ; } return val ; } static int findWays ( int n ) { n -- ; int a , b , ans ; a = binCoff ( 2 * n , n ) ; b = a / ( n + 1 ) ; ans = b ; return ans ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( findWays ( n ) ) ; } }
class GFG { static int [ ] [ ] dp = new int [ 1010 ] [ 1010 ] ; static int solve ( String s , String t ) { int n = s . length ( ) ; int r = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; if ( i > 0 ) { dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j ] ) ; } if ( s . charAt ( i ) == t . charAt ( j ) ) { int ans = 1 ; if ( i > 0 && j > 0 ) { ans = 1 + dp [ i - 1 ] [ j - 1 ] ; } dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , ans ) ; r = Math . max ( r , dp [ i ] [ j ] ) ; } } } return ( n - r ) ; } public static void main ( String [ ] args ) { String s = " abcde " ; String t = " edacb " ; System . out . print ( solve ( s , t ) ) ; } }
import java . util . * ; class GFG { static int longestSubstring ( String s , int n ) { Map < Integer , Integer > index = new HashMap < > ( ) ; int answer = 0 ; int mask = 0 ; index . put ( mask , - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { int temp = ( int ) s . charAt ( i ) - 97 ; mask ^= ( 1 << temp ) ; if ( index . containsKey ( mask ) ) { answer = Math . max ( answer , i - index . get ( mask ) ) ; } else index . put ( mask , i ) ; for ( int j = 0 ; j < 26 ; j ++ ) { int mask2 = mask ^ ( 1 << j ) ; if ( index . containsKey ( mask2 ) ) { answer = Math . max ( answer , i - index . get ( mask2 ) ) ; } } } return answer ; } public static void main ( String [ ] args ) { String s = " adbabd " ; int n = s . length ( ) ; System . out . print ( longestSubstring ( s , n ) ) ; } }
class GFG { static int [ ] [ ] dp = new int [ 15 ] [ 10 ] ; static int solve ( int n , int last , int k ) { if ( n == 1 ) { if ( last == k ) { return ( k - 1 ) ; } else { return 1 ; } } if ( dp [ n ] [ last ] == 1 ) return dp [ n ] [ last ] ; if ( last == k ) { return dp [ n ] [ last ] = ( k - 1 ) * solve ( n - 1 , k , k ) + ( k - 1 ) * solve ( n - 1 , 1 , k ) ; } else { return dp [ n ] [ last ] = solve ( n - 1 , k , k ) ; } } public static void main ( String [ ] args ) { int n = 2 , k = 3 ; int x = solve ( n , k , k ) + solve ( n , 1 , k ) ; System . out . print ( x ) ; } }
import java . util . * ; class GFG { static void print ( int [ ] occ , String s ) { for ( int i = 1 ; i <= s . length ( ) - 1 ; i ++ ) { System . out . print ( s . substring ( 0 , i ) + " ▁ occurs ▁ " + occ [ i ] + " ▁ times . " + "NEW_LINE"); } } static int [ ] prefix_function ( String s ) { int [ ] LPS = new int [ s . length ( ) ] ; LPS [ 0 ] = 0 ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { int j = LPS [ i - 1 ] ; while ( j > 0 && s . charAt ( i ) != s . charAt ( j ) ) { j = LPS [ j - 1 ] ; } if ( s . charAt ( i ) == s . charAt ( j ) ) { LPS [ i ] = j + 1 ; } else { LPS [ i ] = 0 ; } } return LPS ; } static void count_occurence ( String s ) { int n = s . length ( ) ; int [ ] LPS = prefix_function ( s ) ; int [ ] occ = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { occ [ LPS [ i ] ] ++ ; } for ( int i = n - 1 ; i > 0 ; i -- ) { occ [ LPS [ i - 1 ] ] += occ [ i ] ; } for ( int i = 0 ; i <= n ; i ++ ) occ [ i ] ++ ; print ( occ , s ) ; } public static void main ( String [ ] args ) { String A = " ABACABA " ; count_occurence ( A ) ; } }
class GFG { static int maximumSum ( int A [ ] , int B [ ] , int length , int X , int Y ) { int l = length ; int l1 = Math . min ( length , X ) ; int l2 = Math . min ( length , Y ) ; int dp [ ] [ ] = new int [ l1 + 1 ] [ l2 + 1 ] ; int max_sum = Integer . MIN_VALUE ; for ( int i = 1 ; i <= l1 ; i ++ ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + A [ i - 1 ] ; max_sum = Math . max ( max_sum , dp [ i ] [ 0 ] ) ; } for ( int i = 1 ; i <= l2 ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + B [ i - 1 ] ; max_sum = Math . max ( max_sum , dp [ 0 ] [ i ] ) ; } for ( int i = 1 ; i <= l1 ; i ++ ) { for ( int j = 1 ; j <= l2 ; j ++ ) { if ( i + j <= l ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] + A [ i + j - 1 ] , dp [ i ] [ j - 1 ] + B [ i + j - 1 ] ) ; max_sum = Math . max ( dp [ i ] [ j ] , max_sum ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int A [ ] = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int B [ ] = new int [ ] { 5 , 4 , 3 , 2 , 1 } ; int X = 3 , Y = 2 ; int N = A . length ; System . out . println ( maximumSum ( A , B , N , X , Y ) ) ; } }
import java . util . * ; class GFG { static void printpenta ( int n ) { if ( n < 0 ) return ; int first = 0 ; int second = 0 ; int third = 0 ; int fourth = 0 ; int fifth = 1 ; int curr = 0 ; if ( n == 0 n == 1 n == 2 n == 3 ) System . out . print ( first + "NEW_LINE"); else if ( n == 5 ) System . out . print ( fifth + "NEW_LINE"); else { for ( int i = 5 ; i < n ; i ++ ) { curr = first + second + third + fourth + fifth ; first = second ; second = third ; third = fourth ; fourth = fifth ; fifth = curr ; } System . out . print ( curr + "NEW_LINE"); } } public static void main ( String [ ] args ) { int n = 10 ; printpenta ( n ) ; } }
class GFG { static int [ ] [ ] [ ] dp = new int [ 100001 ] [ 20 ] [ 2 ] ; static int possibleBinaries ( int pos , int ones , int sum , int k ) { if ( ones == k ) return 0 ; if ( pos == 0 ) return ( sum == 0 ) ? 1 : 0 ; if ( dp [ pos ] [ ones ] [ sum ] != - 1 ) return dp [ pos ] [ ones ] [ sum ] ; int ret = possibleBinaries ( pos - 1 , ones + 1 , ( sum + 1 ) % 2 , k ) + possibleBinaries ( pos - 1 , 0 , sum , k ) ; dp [ pos ] [ ones ] [ sum ] = ret ; return dp [ pos ] [ ones ] [ sum ] ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 2 ; for ( int i = 0 ; i < 100001 ; i ++ ) { for ( int j = 0 ; j < 20 ; j ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } System . out . print ( possibleBinaries ( N , 0 , 0 , K ) ) ; } }
class GFG { static int [ ] [ ] [ ] dp = new int [ 101 ] [ 101 ] [ 101 ] ; static int N , M , K ; static int MOD = 1000000007 ; public static void Initialize ( ) { for ( int i = 0 ; i <= 100 ; i ++ ) for ( int j = 0 ; j <= 100 ; j ++ ) for ( int z = 0 ; z <= 100 ; z ++ ) dp [ i ] [ j ] [ z ] = - 1 ; } public static int CountWays ( int i , int j , int k ) { if ( i >= N i < 0 j >= M j < 0 k < 0 ) return 0 ; if ( i == 0 && j == 0 && k == 0 ) return 1 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; else dp [ i ] [ j ] [ k ] = ( CountWays ( i + 1 , j , k - 1 ) % MOD + CountWays ( i - 1 , j , k - 1 ) % MOD + CountWays ( i , j - 1 , k - 1 ) % MOD + CountWays ( i , j + 1 , k - 1 ) % MOD + CountWays ( i , j , k - 1 ) % MOD ) % MOD ; return dp [ i ] [ j ] [ k ] ; } public static void main ( String [ ] args ) { N = 3 ; M = 3 ; K = 4 ; Initialize ( ) ; System . out . println ( CountWays ( 0 , 0 , K ) ) ; } }
import java . util . * ; class GFG { static int INF = ( int ) ( 1e9 + 9 ) ; static class TrieNode { TrieNode [ ] child = new TrieNode [ 26 ] ; } ; static void insert ( int idx , String s , TrieNode root ) { TrieNode temp = root ; for ( int i = idx ; i < s . length ( ) ; i ++ ) { if ( temp . child [ s . charAt ( i ) - ' a ' ] == null ) temp . child [ s . charAt ( i ) - ' a ' ] = new TrieNode ( ) ; temp = temp . child [ s . charAt ( i ) - ' a ' ] ; } } static int minCuts ( String S1 , String S2 ) { int n1 = S1 . length ( ) ; int n2 = S2 . length ( ) ; TrieNode root = new TrieNode ( ) ; for ( int i = 0 ; i < n2 ; i ++ ) { insert ( i , S2 , root ) ; } int [ ] dp = new int [ n1 + 1 ] ; Arrays . fill ( dp , INF ) ; dp [ 0 ] = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { TrieNode temp = root ; for ( int j = i + 1 ; j <= n1 ; j ++ ) { if ( temp . child [ S1 . charAt ( j - 1 ) - ' a ' ] == null ) break ; dp [ j ] = Math . min ( dp [ j ] , dp [ i ] + 1 ) ; temp = temp . child [ S1 . charAt ( j - 1 ) - ' a ' ] ; } } if ( dp [ n1 ] >= INF ) return - 1 ; else return dp [ n1 ] ; } public static void main ( String [ ] args ) { String S1 = " abcdab " ; String S2 = " dabc " ; System . out . print ( minCuts ( S1 , S2 ) ) ; } }
import java . util . * ; class GFG { static int findMinPrizes ( int arr [ ] , int n ) { int totalPrizes = 0 , j , x , y ; for ( int i = 0 ; i < n ; i ++ ) { x = 1 ; j = i ; while ( j > 0 && arr [ j ] > arr [ j - 1 ] ) { x ++ ; j -- ; } j = i ; y = 1 ; while ( j < n - 1 && arr [ j ] > arr [ j + 1 ] ) { y ++ ; j ++ ; } totalPrizes += Math . max ( x , y ) ; } System . out . print ( totalPrizes + "NEW_LINE"); return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; findMinPrizes ( arr , n ) ; } }
import java . util . * ; class GFG { static Vector < Integer > minSqrNum ( int n ) { int [ ] arr = new int [ n + 1 ] ; int k = 0 ; int [ ] sqrNum = new int [ n + 1 ] ; Vector < Integer > v = new Vector < > ( ) ; arr [ 0 ] = 0 ; sqrNum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { arr [ i ] = arr [ i - 1 ] + 1 ; sqrNum [ i ] = 1 ; k = 1 ; while ( k * k <= i ) { if ( arr [ i ] > arr [ i - k * k ] + 1 ) { arr [ i ] = arr [ i - k * k ] + 1 ; sqrNum [ i ] = k * k ; } k ++ ; } } while ( n > 0 ) { v . add ( sqrNum [ n ] ) ; n -= sqrNum [ n ] ; } return v ; } public static void main ( String [ ] args ) { int n = 10 ; Vector < Integer > v ; v = minSqrNum ( n ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { System . out . print ( v . elementAt ( i ) ) ; if ( i + 1 != v . size ( ) ) System . out . print ( " ▁ + ▁ " ) ; } } }
