import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void makePairs ( Vector < Integer > pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) pairs . add ( sumOfDigits ) ; } } static int countCoPrime ( int a , int b ) { Vector < Integer > pairs = new Vector < Integer > ( ) ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pairs . size ( ) ; j ++ ) if ( GCD ( pairs . get ( i ) , pairs . get ( j ) ) == 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int a = 12 , b = 15 ; System . out . println ( countCoPrime ( a , b ) ) ; } }

from math import gcd NEW_LINE def makePairs ( pairs , a , b ) : NEW_LINE INDENT for i in range ( a , b + 1 , 1 ) : NEW_LINE INDENT sumOfDigits = 0 NEW_LINE k = i NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sumOfDigits += k % 10 NEW_LINE k = int ( k / 10 ) NEW_LINE DEDENT if ( sumOfDigits <= 162 ) : NEW_LINE INDENT pairs . append ( sumOfDigits ) NEW_LINE DEDENT DEDENT DEDENT def countCoPrime ( a , b ) : NEW_LINE INDENT pairs = [ ] NEW_LINE makePairs ( pairs , a , b ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( pairs ) , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len ( pairs ) , 1 ) : NEW_LINE INDENT if ( gcd ( pairs [ i ] , pairs [ j ] ) == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 12 NEW_LINE b = 15 NEW_LINE print ( countCoPrime ( a , b ) ) NEW_LINE DEDENT


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 ) ; } }

def print1 ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def sort ( a , n ) : NEW_LINE INDENT for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for j in range ( n , n - i , - 1 ) : NEW_LINE INDENT if ( a [ j ] > a [ j - 1 ] ) : NEW_LINE INDENT a [ j ] , a [ j - 1 ] = a [ j - 1 ] , a [ j ] NEW_LINE DEDENT DEDENT DEDENT print1 ( a , n ) NEW_LINE DEDENT n = 7 NEW_LINE a = [ 2 , 4 , 3 , 2 , 4 , 5 , 3 ] NEW_LINE sort ( a , n - 1 ) NEW_LINE


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 " ) ; } }

def isPalindrome ( num ) : NEW_LINE INDENT s = str ( num ) NEW_LINE low = 0 NEW_LINE high = len ( s ) - 1 NEW_LINE while ( low < high ) : NEW_LINE INDENT if ( s [ low ] != s [ high ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 123.321 NEW_LINE if ( isPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { public static int solve ( int [ ] A , int n ) { int [ ] [ ] dp = new int [ 2000 ] [ 2000 ] ; int flag = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += A [ i ] ; for ( int i = - sum ; i <= sum ; i ++ ) { try { dp [ 0 ] [ i ] = Integer . MAX_VALUE ; } catch ( Exception e ) { } } dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) { try { dp [ flag ] [ j ] = Integer . MAX_VALUE ; if ( j - A [ i - 1 ] <= sum && j - A [ i - 1 ] >= - sum ) dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] ; if ( j + A [ i - 1 ] <= sum && j + A [ i - 1 ] >= - sum && dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != Integer . MAX_VALUE ) dp [ flag ] [ j ] = Math . min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) ; } catch ( Exception e ) { } } flag = flag ^ 1 ; } for ( int i = 0 ; i <= sum ; i ++ ) { if ( dp [ flag ^ 1 ] [ i ] != Integer . MAX_VALUE ) return dp [ flag ^ 1 ] [ i ] ; } return n - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( solve ( arr , n ) ) ; } }

def solve ( A , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2000 ) ] for i in range ( 2000 ) ] NEW_LINE flag = 1 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT for i in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 10 ** 9 NEW_LINE DEDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ flag ] [ j ] = 10 ** 9 NEW_LINE if ( j - A [ i - 1 ] <= sum and j - A [ i - 1 ] >= - sum ) : NEW_LINE INDENT dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] NEW_LINE DEDENT if ( j + A [ i - 1 ] <= sum and j + A [ i - 1 ] >= - sum and dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != 10 ** 9 ) : NEW_LINE INDENT dp [ flag ] [ j ] = min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT flag = flag ^ 1 NEW_LINE DEDENT for i in range ( sum + 1 ) : NEW_LINE INDENT if ( dp [ flag ^ 1 ] [ i ] != 10 ** 9 ) : NEW_LINE INDENT return dp [ flag ^ 1 ] [ i ] NEW_LINE DEDENT DEDENT return n - 1 NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( solve ( arr , n ) ) NEW_LINE


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 ) ; } }

def recaman ( n ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE arr [ 0 ] = 0 NEW_LINE print ( arr [ 0 ] , end = " , ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = arr [ i - 1 ] - i NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if ( ( arr [ j ] == curr ) or curr < 0 ) : NEW_LINE INDENT curr = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = curr NEW_LINE print ( arr [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE


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 ) ; } }

def find_composite_nos ( n ) : NEW_LINE INDENT print ( 9 * n , 8 * n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE find_composite_nos ( n ) ; NEW_LINE DEDENT


public class GFG { static char encodedChar ( String str , int k ) { String expand = " " ; String temp = " " ; int freq = 0 ; for ( int i = 0 ; i < str . length ( ) ; ) { temp = " " ; freq = 0 ; 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 ) ) ; } }

def encodedChar ( str , k ) : NEW_LINE INDENT expand = " " NEW_LINE freq = 0 NEW_LINE i = 0 NEW_LINE while ( i < len ( str ) ) : NEW_LINE INDENT temp = " " NEW_LINE freq = 0 NEW_LINE while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( ' a ' ) and ord ( str [ i ] ) <= ord ( ' z ' ) ) : NEW_LINE INDENT temp += str [ i ] NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( '1' ) and ord ( str [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT freq = freq * 10 + ord ( str [ i ] ) - ord ( '0' ) NEW_LINE i += 1 NEW_LINE DEDENT for j in range ( 1 , freq + 1 , 1 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT DEDENT if ( freq == 0 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT return expand [ k - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ab4c12ed3" NEW_LINE k = 21 NEW_LINE print ( encodedChar ( str , k ) ) NEW_LINE DEDENT


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int count [ ] = new int [ size ] ; int i ; System . out . println ( " Repeated ▁ elements ▁ are ▁ : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( count [ arr [ i ] ] == 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; else count [ arr [ i ] ] ++ ; } } public static void main ( String [ ] args ) { RepeatElement repeat = new RepeatElement ( ) ; int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . length ; repeat . printRepeating ( arr , arr_size ) ; } }

def printRepeating ( arr , size ) : NEW_LINE INDENT count = [ 0 ] * size NEW_LINE print ( " ▁ Repeating ▁ elements ▁ are ▁ " , end = " " ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT if ( count [ arr [ i ] ] == 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT count [ arr [ i ] ] = count [ arr [ i ] ] + 1 NEW_LINE DEDENT DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


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 ) ) ; } }

def solve ( s ) : NEW_LINE INDENT area = ( 1.732 * s ** 2 ) / 8 NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE print ( solve ( n ) ) NEW_LINE DEDENT


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 " ) ; } }

_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class getnode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getlevel ( root , data ) : NEW_LINE INDENT q = [ ] NEW_LINE level = 1 NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp == None ) : NEW_LINE INDENT if len ( q ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( q [ 0 ] != None ) : NEW_LINE INDENT q . append ( None ) NEW_LINE DEDENT level += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( temp . data == data ) : NEW_LINE INDENT return level NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getnode ( 20 ) NEW_LINE root . left = getnode ( 10 ) NEW_LINE root . right = getnode ( 30 ) NEW_LINE root . left . left = getnode ( 5 ) NEW_LINE root . left . right = getnode ( 15 ) NEW_LINE root . left . right . left = getnode ( 12 ) NEW_LINE root . right . left = getnode ( 25 ) NEW_LINE root . right . right = getnode ( 40 ) NEW_LINE level = getlevel ( root , 30 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 30 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 30 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 12 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 12 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 12 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 25 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 25 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 25 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 27 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 27 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 27 ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int S = 0 ; int P = 1 ; int x , y ; int D ; int n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * ( n + 1 ) / 2 ; P = P / fact ( n ) ; D = ( int ) Math . sqrt ( S * S - 4 * P ) ; x = ( D + S ) / 2 ; y = ( S - D ) / 2 ; System . out . println ( " The ▁ two ▁ repeating ▁ elements ▁ are ▁ : " ) ; System . out . print ( x + " ▁ " + y ) ; } int fact ( int n ) { return ( n == 0 ) ? 1 : n * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { RepeatElement repeat = new RepeatElement ( ) ; int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . length ; repeat . printRepeating ( arr , arr_size ) ; } }

import math NEW_LINE def printRepeating ( arr , size ) : NEW_LINE INDENT S = 0 ; NEW_LINE P = 1 ; NEW_LINE n = size - 2 NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT S = S + arr [ i ] NEW_LINE P = P * arr [ i ] NEW_LINE DEDENT S = S - n * ( n + 1 ) // 2 NEW_LINE P = P // fact ( n ) NEW_LINE D = math . sqrt ( S * S - 4 * P ) NEW_LINE x = ( D + S ) // 2 NEW_LINE y = ( S - D ) // 2 NEW_LINE print ( " The ▁ two ▁ Repeating ▁ elements ▁ are ▁ " , ( int ) ( x ) , " ▁ & ▁ " , ( int ) ( y ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n * fact ( n - 1 ) ) NEW_LINE DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


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 sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE INT_MIN = - ( sys . maxsize - 1 ) ; NEW_LINE def minimumAdjacentDifference ( a , n , k ) : NEW_LINE INDENT minDiff = INT_MAX ; NEW_LINE for i in range ( k + 1 ) : NEW_LINE INDENT maxDiff = INT_MIN ; NEW_LINE for j in range ( n - k - 1 ) : NEW_LINE INDENT for p in range ( i , i + j + 1 ) : NEW_LINE INDENT maxDiff = max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; NEW_LINE DEDENT DEDENT minDiff = min ( minDiff , maxDiff ) ; NEW_LINE DEDENT return minDiff ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE k = 2 ; NEW_LINE a = [ 3 , 7 , 8 , 10 , 14 ] ; NEW_LINE print ( minimumAdjacentDifference ( a , n , k ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int setBitNumber ( int n ) { if ( n == 0 ) return 0 ; int msb = 0 ; while ( n != 0 ) { n = n / 2 ; msb ++ ; } return ( 1 << msb ) ; } public static void main ( String [ ] args ) { int n = 0 ; System . out . println ( setBitNumber ( n ) ) ; } }

def setBitNumber ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT msb = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = int ( n / 2 ) ; NEW_LINE msb += 1 ; NEW_LINE DEDENT return ( 1 << msb ) ; NEW_LINE DEDENT n = 0 ; NEW_LINE print ( setBitNumber ( n ) ) ; NEW_LINE


class GFG { static int countSetBits ( int n ) { n ++ ; int powerOf2 = 2 ; int cnt = n / 2 ; while ( powerOf2 <= n ) { int totalPairs = n / powerOf2 ; cnt += ( totalPairs / 2 ) * powerOf2 ; cnt += ( totalPairs % 2 == 1 ) ? ( n % powerOf2 ) : 0 ; powerOf2 <<= 1 ; } return cnt ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . println ( countSetBits ( n ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT n += 1 ; NEW_LINE powerOf2 = 2 ; NEW_LINE cnt = n // 2 ; NEW_LINE while ( powerOf2 <= n ) : NEW_LINE INDENT totalPairs = n // powerOf2 ; NEW_LINE cnt += ( totalPairs // 2 ) * powerOf2 ; NEW_LINE if ( totalPairs & 1 ) : NEW_LINE INDENT cnt += ( n % powerOf2 ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 0 NEW_LINE DEDENT powerOf2 <<= 1 ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 14 ; NEW_LINE print ( countSetBits ( n ) ) ; NEW_LINE DEDENT


import java . lang . * ; import java . lang . Math ; import java . io . * ; class GFG { static double getValue ( double n ) { double result = ( Math . floor ( Math . log ( n + 1 ) / Math . log ( 2 ) ) ) ; return Math . pow ( 2 , result ) ; } public static void main ( String [ ] args ) { double n = 9 ; System . out . println ( getValue ( n ) ) ; n = 1025 ; System . out . println ( getValue ( n ) ) ; } }

import math NEW_LINE def getValue ( n ) : NEW_LINE INDENT result = int ( math . floor ( math . log ( n + 1 ) / math . log ( 2 ) ) ) NEW_LINE return int ( math . pow ( 2 , result ) ) NEW_LINE DEDENT n = 9 NEW_LINE print ( getValue ( n ) ) NEW_LINE n = 1025 NEW_LINE print ( getValue ( n ) ) NEW_LINE


import java . io . * ; class GFG { static void printLargestDivisible ( int n , int a [ ] ) { int i , c0 = 0 , c5 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) c0 ++ ; else c5 ++ ; } c5 = ( int ) Math . floor ( c5 / 9 ) * 9 ; if ( c0 == 0 ) System . out . print ( - 1 ) ; else if ( c5 == 0 ) System . out . println ( 0 ) ; else { for ( i = 0 ; i < c5 ; i ++ ) System . out . print ( 5 ) ; for ( i = 0 ; i < c0 ; i ++ ) System . out . print ( 0 ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 0 , 5 , 5 } ; int n = a . length ; printLargestDivisible ( n , a ) ; } }

from math import * NEW_LINE def printLargestDivisible ( n , a ) : NEW_LINE INDENT c0 , c5 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] == 0 : NEW_LINE INDENT c0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT c5 += 1 NEW_LINE DEDENT DEDENT c5 = floor ( c5 / 9 ) * 9 NEW_LINE if c0 == 0 : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE DEDENT elif c5 == 0 : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( c5 ) : NEW_LINE INDENT print ( 5 , end = " " ) NEW_LINE DEDENT for i in range ( c0 ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 0 , 5 , 5 ] NEW_LINE n = len ( a ) NEW_LINE printLargestDivisible ( n , a ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void sortWithRemainderP ( int arr [ ] , int n , int k , int p ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == p ) { v1 . add ( i ) ; v2 . add ( arr [ i ] ) ; } } Collections . sort ( v2 ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) arr [ v1 . get ( i ) ] = v2 . get ( i ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 255 , 16 , 2 , 4 , 0 } ; int n = arr . length ; int k = 2 ; int p = 0 ; sortWithRemainderP ( arr , n , k , p ) ; } }

def sortWithRemainderP ( arr , n , k , p ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % k == p ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE v2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v2 . sort ( reverse = False ) NEW_LINE for i in range ( 0 , len ( v1 ) , 1 ) : NEW_LINE INDENT arr [ v1 [ i ] ] = v2 [ i ] NEW_LINE DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 255 , 16 , 2 , 4 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE p = 0 NEW_LINE sortWithRemainderP ( arr , n , k , p ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void checkCollision ( int a , int b , int c , int x , int y , int radius ) { double dist = ( Math . abs ( a * x + b * y + c ) ) / Math . sqrt ( a * a + b * b ) ; if ( radius == dist ) System . out . println ( " Touch " ) ; else if ( radius > dist ) System . out . println ( " Intersect " ) ; else System . out . println ( " Outside " ) ; } public static void main ( String [ ] args ) { int radius = 5 ; int x = 0 , y = 0 ; int a = 3 , b = 4 , c = 25 ; checkCollision ( a , b , c , x , y , radius ) ; } }

import math NEW_LINE def checkCollision ( a , b , c , x , y , radius ) : NEW_LINE INDENT dist = ( ( abs ( a * x + b * y + c ) ) / math . sqrt ( a * a + b * b ) ) NEW_LINE if ( radius == dist ) : NEW_LINE INDENT print ( " Touch " ) NEW_LINE DEDENT elif ( radius > dist ) : NEW_LINE INDENT print ( " Intersect " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Outside " ) NEW_LINE DEDENT DEDENT radius = 5 NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE a = 3 NEW_LINE b = 4 NEW_LINE c = 25 NEW_LINE checkCollision ( a , b , c , x , y , radius ) NEW_LINE


import java . util . * ; class GFG { static int countSubArrayProductLessThanK ( ArrayList < Integer > a , long k ) { int n = a . size ( ) ; long p = 1 ; int res = 0 ; for ( int start = 0 , end = 0 ; end < n ; end ++ ) { p *= a . get ( end ) ; while ( start < end && p >= k ) p /= a . get ( start ++ ) ; if ( p < k ) { int len = end - start + 1 ; res += len ; } } return res ; } public static void main ( String [ ] args ) { ArrayList < Integer > al = new ArrayList < Integer > ( ) ; al . add ( 1 ) ; al . add ( 2 ) ; al . add ( 3 ) ; al . add ( 4 ) ; System . out . println ( countSubArrayProductLessThanK ( al , 10 ) ) ; ArrayList < Integer > al1 = new ArrayList < Integer > ( ) ; al1 . add ( 1 ) ; al1 . add ( 9 ) ; al1 . add ( 2 ) ; al1 . add ( 8 ) ; al1 . add ( 6 ) ; al1 . add ( 4 ) ; al1 . add ( 3 ) ; System . out . println ( countSubArrayProductLessThanK ( al1 , 100 ) ) ; ArrayList < Integer > al2 = new ArrayList < Integer > ( ) ; al2 . add ( 5 ) ; al2 . add ( 3 ) ; al2 . add ( 2 ) ; System . out . println ( countSubArrayProductLessThanK ( al2 , 16 ) ) ; ArrayList < Integer > al3 = new ArrayList < Integer > ( ) ; al3 . add ( 100 ) ; al3 . add ( 200 ) ; System . out . println ( countSubArrayProductLessThanK ( al3 , 100 ) ) ; ArrayList < Integer > al4 = new ArrayList < Integer > ( ) ; al4 . add ( 100 ) ; al4 . add ( 200 ) ; System . out . println ( countSubArrayProductLessThanK ( al3 , 101 ) ) ; } }

def countSubArrayProductLessThanK ( a , k ) : NEW_LINE INDENT n = len ( a ) NEW_LINE p = 1 NEW_LINE res = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT p *= a [ end ] NEW_LINE while ( start < end and p >= k ) : NEW_LINE INDENT p = int ( p // a [ start ] ) NEW_LINE start += 1 NEW_LINE DEDENT if ( p < k ) : NEW_LINE INDENT l = end - start + 1 NEW_LINE res += l NEW_LINE DEDENT end += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( countSubArrayProductLessThanK ( [ 1 , 2 , 3 , 4 ] , 10 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 1 , 9 , 2 , 8 , 6 , 4 , 3 ] , 100 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 5 , 3 , 2 ] , 16 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 100 , 200 ] , 100 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 100 , 200 ] , 101 ) ) NEW_LINE DEDENT


import java . lang . * ; import java . util . * ; class GFG { static void countDigit ( int n ) { int temp = n , sum = 0 , product = 1 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) { sum += d ; product *= d ; } } System . out . print ( " Sum ▁ = ▁ " + sum ) ; System . out . print ( " \n Product ▁ = ▁ " + product ) ; } public static void main ( String args [ ] ) { int n = 1012 ; countDigit ( n ) ; } }

def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE product = 1 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d > 0 and n % d == 0 ) : NEW_LINE INDENT sum += d NEW_LINE product *= d NEW_LINE DEDENT DEDENT print ( " Sum ▁ = " , sum ) NEW_LINE print ( " Product ▁ = " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1012 NEW_LINE countDigit ( n ) NEW_LINE DEDENT


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 ) ) ; } }

INT_BITS = 32 NEW_LINE def maxSubarrayXOR ( set , n ) : NEW_LINE INDENT index = 0 NEW_LINE for i in range ( INT_BITS - 1 , - 1 , - 1 ) : NEW_LINE INDENT maxInd = index NEW_LINE maxEle = - 2147483648 NEW_LINE for j in range ( index , n ) : NEW_LINE INDENT if ( ( set [ j ] & ( 1 << i ) ) != 0 and set [ j ] > maxEle ) : NEW_LINE INDENT maxEle = set [ j ] NEW_LINE maxInd = j NEW_LINE DEDENT DEDENT if ( maxEle == - 2147483648 ) : NEW_LINE INDENT continue NEW_LINE DEDENT temp = set [ index ] NEW_LINE set [ index ] = set [ maxInd ] NEW_LINE set [ maxInd ] = temp NEW_LINE maxInd = index NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( j != maxInd and ( set [ j ] & ( 1 << i ) ) != 0 ) : NEW_LINE INDENT set [ j ] = set [ j ] ^ set [ maxInd ] NEW_LINE DEDENT DEDENT index = index + 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = res ^ set [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT set = [ 9 , 8 , 5 ] NEW_LINE n = len ( set ) NEW_LINE print ( " Max ▁ subset ▁ XOR ▁ is ▁ " , end = " " ) NEW_LINE print ( maxSubarrayXOR ( set , n ) ) NEW_LINE


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 ) ) ; } }

from math import sqrt NEW_LINE def maxVol ( P , A ) : NEW_LINE INDENT l = ( P - sqrt ( P * P - 24 * A ) ) / 12 NEW_LINE V = l * ( A / 2.0 - l * ( P / 4.0 - l ) ) NEW_LINE return V NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = 20 NEW_LINE A = 16 NEW_LINE print ( maxVol ( P , A ) ) NEW_LINE DEDENT


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 ) ; } }

def SubarraysWithSumS ( n , k , s ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT print ( s , end = " ▁ " ) NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( s + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE k = 2 NEW_LINE s = 3 NEW_LINE SubarraysWithSumS ( n , k , s ) NEW_LINE


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 ) ) ; } }

def sum_of_series ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT result = result - pow ( i , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT result = result + pow ( i , 2 ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE n = 10 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE DEDENT


import java . util . * ; public class ACE { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int find_LCM ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int CountPairs ( int n , int m , int A , int B ) { int cnt = 0 ; int lcm = find_LCM ( A , B ) ; for ( int i = 1 ; i <= n ; i ++ ) cnt += ( m + ( i % lcm ) ) / lcm ; return cnt ; } public static void main ( String [ ] args ) { int n = 60 , m = 90 , A = 5 , B = 10 ; System . out . println ( CountPairs ( n , m , A , B ) ) ; } }

from math import gcd NEW_LINE def find_LCM ( x , y ) : NEW_LINE INDENT return ( x * y ) // gcd ( x , y ) NEW_LINE DEDENT def CountPairs ( n , m , A , B ) : NEW_LINE INDENT cnt = 0 NEW_LINE lcm = find_LCM ( A , B ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT cnt += ( m + ( i % lcm ) ) // lcm NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m , A , B = 60 , 90 , 5 , 10 NEW_LINE print ( CountPairs ( n , m , A , B ) ) NEW_LINE DEDENT


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 ; } public static void main ( String [ ] args ) { int num = 4562 ; System . out . println ( " Reverse ▁ of ▁ no . ▁ is ▁ " + reversDigits ( num ) ) ; } }

n = 4562 ; NEW_LINE rev = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a = n % 10 NEW_LINE rev = rev * 10 + a NEW_LINE n = n / 10 NEW_LINE DEDENT print ( rev ) NEW_LINE


import java . util . * ; class GFG { static boolean [ ] Prime ; static void computePrime ( int N ) { Prime = new boolean [ N + 1 ] ; Arrays . fill ( Prime , true ) ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( int j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } static int countTwinPairs ( int arr [ ] , int n ) { int maxE = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; computePrime ( maxE ) ; int count = 0 ; int freq [ ] = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Prime [ arr [ i ] ] ) { if ( arr [ i ] + 2 < freq . length && freq [ arr [ i ] + 2 ] > 0 && Prime [ arr [ i ] + 2 ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 5 , 7 } ; int n = arr . length ; System . out . print ( countTwinPairs ( arr , n ) ) ; } }

def computePrime ( N ) : NEW_LINE INDENT Prime = [ True ] * ( N + 1 ) NEW_LINE Prime [ 0 ] = False NEW_LINE Prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= N : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return Prime NEW_LINE DEDENT def countTwinPairs ( arr , n ) : NEW_LINE INDENT maxE = max ( arr ) NEW_LINE Prime = computePrime ( maxE ) NEW_LINE count = 0 NEW_LINE freq = [ 0 ] * ( maxE + 2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( ( arr [ i ] + 2 ) <= ( maxE ) and freq [ arr [ i ] + 2 ] > 0 and Prime [ arr [ i ] + 2 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTwinPairs ( arr , n ) ) NEW_LINE DEDENT


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 ) ) ; } }

from math import log NEW_LINE def log_a_to_base_b ( a , b ) : NEW_LINE INDENT return log ( a ) // log ( b ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 2 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE a = 256 ; NEW_LINE b = 4 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE DEDENT


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 ) ) ; } }

def power ( a , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( a , n // 2 ) % mod NEW_LINE p = ( p * p ) % mod NEW_LINE if n & 1 == 1 : NEW_LINE INDENT p = ( p * a ) % mod NEW_LINE DEDENT return p NEW_LINE DEDENT def computeSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT maxs = ( power ( 2 , i ) - 1 + mod ) % mod NEW_LINE maxs = ( maxs * arr [ i ] ) % mod NEW_LINE mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod NEW_LINE mins = ( mins * arr [ i ] ) % mod NEW_LINE V = ( maxs - mins + mod ) % mod NEW_LINE Sum = ( Sum + V ) % mod NEW_LINE DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mod = 1000000007 NEW_LINE arr = [ 4 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( computeSum ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; public class GFG { static int check ( int A [ ] , int B [ ] , int N ) { PriorityQueue < Integer > pq1 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; PriorityQueue < Integer > pq2 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . add ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . add ( B [ i ] ) ; } int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 . peek ( ) . compareTo ( pq2 . peek ( ) ) == 1 ) { c ++ ; pq1 . poll ( ) ; pq2 . poll ( ) ; } else { if ( pq2 . size ( ) == 0 ) { break ; } pq2 . poll ( ) ; } } return ( c ) ; } public static void main ( String args [ ] ) { int A [ ] = { 10 , 3 , 7 , 5 , 8 } ; int B [ ] = { 8 , 6 , 2 , 5 , 9 } ; int N = A . length ; System . out . println ( check ( A , B , N ) ) ; } }

import heapq NEW_LINE def check ( A , B , N ) : NEW_LINE INDENT pq1 = [ ] NEW_LINE pq2 = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq1 , - A [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq2 , - B [ i ] ) NEW_LINE DEDENT c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if - pq1 [ 0 ] > - pq2 [ 0 ] : NEW_LINE INDENT c += 1 NEW_LINE heapq . heappop ( pq1 ) NEW_LINE heapq . heappop ( pq2 ) NEW_LINE DEDENT else : NEW_LINE INDENT if len ( pq2 ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT heapq . heappop ( pq2 ) NEW_LINE DEDENT DEDENT return ( c ) NEW_LINE DEDENT A = [ 10 , 3 , 7 , 5 , 8 ] NEW_LINE B = [ 8 , 6 , 2 , 5 , 9 ] NEW_LINE N = len ( A ) NEW_LINE print ( check ( A , B , N ) ) NEW_LINE


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 math NEW_LINE def numberofterm ( n , number ) : NEW_LINE INDENT firstnum = math . pow ( 10 , n - 1 ) NEW_LINE lastnum = math . pow ( 10 , n ) NEW_LINE count = 0 NEW_LINE for i in range ( int ( firstnum ) , int ( lastnum ) ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 3 NEW_LINE num = 7 NEW_LINE print ( numberofterm ( n , num ) ) NEW_LINE


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 " ) ; } }

def canConvert ( a , b ) : NEW_LINE INDENT while ( b > a ) : NEW_LINE INDENT if ( b % 10 == 1 ) : NEW_LINE INDENT b //= 10 ; NEW_LINE continue ; NEW_LINE DEDENT if ( b % 2 == 0 ) : NEW_LINE INDENT b /= 2 ; NEW_LINE continue ; NEW_LINE DEDENT return false ; NEW_LINE DEDENT if ( b == a ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 2 ; B = 82 ; NEW_LINE if ( canConvert ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


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 ) ; } }

from math import pow NEW_LINE def highestPower ( n , k ) : NEW_LINE INDENT i = 0 NEW_LINE a = pow ( n , i ) NEW_LINE while ( a <= k ) : NEW_LINE INDENT i += 1 NEW_LINE a = pow ( n , i ) NEW_LINE DEDENT return i - 1 NEW_LINE DEDENT b = [ 0 for i in range ( 50 ) ] NEW_LINE def PowerArray ( n , k ) : NEW_LINE INDENT while ( k ) : NEW_LINE INDENT t = highestPower ( n , k ) NEW_LINE if ( b [ t ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT b [ t ] = 1 NEW_LINE DEDENT k -= pow ( n , t ) NEW_LINE DEDENT for i in range ( 50 ) : NEW_LINE INDENT if ( b [ i ] ) : NEW_LINE INDENT print ( i , end = ' , ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 40 NEW_LINE PowerArray ( N , K ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findMaxGuests ( int arrl [ ] , int exit [ ] , int n ) { Arrays . sort ( arrl ) ; Arrays . sort ( exit ) ; int guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; int i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } System . out . println ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " + max_guests + " ▁ at ▁ time ▁ " + time ) ; } public static void main ( String [ ] args ) { int arrl [ ] = { 1 , 2 , 10 , 5 , 5 } ; int exit [ ] = { 4 , 5 , 12 , 9 , 12 } ; int n = arrl . length ; findMaxGuests ( arrl , exit , n ) ; } }

def findMaxGuests ( arrl , exit , n ) : NEW_LINE INDENT arrl . sort ( ) ; NEW_LINE exit . sort ( ) ; NEW_LINE guests_in = 1 ; NEW_LINE max_guests = 1 ; NEW_LINE time = arrl [ 0 ] ; NEW_LINE i = 1 ; NEW_LINE j = 0 ; NEW_LINE while ( i < n and j < n ) : NEW_LINE INDENT if ( arrl [ i ] <= exit [ j ] ) : NEW_LINE INDENT guests_in = guests_in + 1 ; NEW_LINE if ( guests_in > max_guests ) : NEW_LINE INDENT max_guests = guests_in ; NEW_LINE time = arrl [ i ] ; NEW_LINE DEDENT i = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT guests_in = guests_in - 1 ; NEW_LINE j = j + 1 ; NEW_LINE DEDENT DEDENT print ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = " , max_guests , " at ▁ time " , time ) NEW_LINE DEDENT arrl = [ 1 , 2 , 10 , 5 , 5 ] ; NEW_LINE exit = [ 4 , 5 , 12 , 9 , 12 ] ; NEW_LINE n = len ( arrl ) ; NEW_LINE findMaxGuests ( arrl , exit , n ) ; NEW_LINE


class GFG { static long spellsCount ( String num ) { int n = num . length ( ) ; long result = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && num . charAt ( i + 1 ) == num . charAt ( i ) ) { count ++ ; i ++ ; } result = result * ( long ) Math . pow ( 2 , count - 1 ) ; } return result ; } public static void main ( String [ ] args ) { String num = "11112" ; System . out . print ( spellsCount ( num ) ) ; } }

def spellsCount ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE result = 1 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT count = 1 ; NEW_LINE while ( i < n - 1 and num [ i + 1 ] == num [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT result = result * int ( pow ( 2 , count - 1 ) ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT num = "11112" ; NEW_LINE print ( spellsCount ( num ) ) ; NEW_LINE


public class Longest_common_prefix { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; boolean isLeaf ; public TrieNode ( ) { isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; } } ; static TrieNode root ; static int indexs ; static void insert ( String key ) { int length = key . length ( ) ; int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < length ; level ++ ) { index = key . charAt ( level ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isLeaf = true ; } static int countChildren ( TrieNode node ) { int count = 0 ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) { if ( node . children [ i ] != null ) { count ++ ; indexs = i ; } } return ( count ) ; } static String walkTrie ( ) { TrieNode pCrawl = root ; indexs = 0 ; String prefix = " " ; while ( countChildren ( pCrawl ) == 1 && pCrawl . isLeaf == false ) { pCrawl = pCrawl . children [ indexs ] ; prefix += ( char ) ( ' a ' + indexs ) ; } return prefix ; } static void constructTrie ( String arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) insert ( arr [ i ] ) ; return ; } static String commonPrefix ( String arr [ ] , int n ) { root = new TrieNode ( ) ; constructTrie ( arr , n ) ; return walkTrie ( ) ; } 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 " ) ; } }

ALPHABET_SIZE = 26 NEW_LINE indexs = 0 NEW_LINE class TrieNode : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . isLeaf = False NEW_LINE self . children = [ None ] * ALPHABET_SIZE NEW_LINE DEDENT DEDENT def insert ( key , root ) : NEW_LINE INDENT pCrawl = root NEW_LINE for level in range ( len ( key ) ) : NEW_LINE INDENT index = ord ( key [ level ] ) - ord ( ' a ' ) NEW_LINE if pCrawl . children [ index ] == None : NEW_LINE INDENT pCrawl . children [ index ] = TrieNode ( ) NEW_LINE DEDENT pCrawl = pCrawl . children [ index ] NEW_LINE DEDENT pCrawl . isLeaf = True NEW_LINE DEDENT def constructTrie ( arr , n , root ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT insert ( arr [ i ] , root ) NEW_LINE DEDENT DEDENT def countChildren ( node ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( ALPHABET_SIZE ) : NEW_LINE INDENT if node . children [ i ] != None : NEW_LINE INDENT count += 1 NEW_LINE global indexs NEW_LINE indexs = i NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def walkTrie ( root ) : NEW_LINE INDENT pCrawl = root NEW_LINE prefix = " " NEW_LINE while ( countChildren ( pCrawl ) == 1 and pCrawl . isLeaf == False ) : NEW_LINE INDENT pCrawl = pCrawl . children [ indexs ] NEW_LINE prefix += chr ( 97 + indexs ) NEW_LINE DEDENT return prefix or - 1 NEW_LINE DEDENT def commonPrefix ( arr , n , root ) : NEW_LINE INDENT constructTrie ( arr , n , root ) NEW_LINE return walkTrie ( root ) NEW_LINE DEDENT n = 4 NEW_LINE arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE root = TrieNode ( ) NEW_LINE print ( commonPrefix ( arr , n , root ) ) NEW_LINE


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 ) ) ; } }

def maxAND ( L , R ) : NEW_LINE INDENT maximum = L & R NEW_LINE for i in range ( L , R , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , R + 1 , 1 ) : NEW_LINE INDENT maximum = max ( maximum , ( i & j ) ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 632 NEW_LINE print ( maxAND ( L , R ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countSubArrays ( int arr [ ] , int n , int x ) { int len = 0 ; int count = 0 ; int binaryArr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= x ) binaryArr [ i ] = 1 ; else binaryArr [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( binaryArr [ i ] == 1 ) { int j ; for ( j = i + 1 ; j < n ; j ++ ) if ( binaryArr [ j ] != 1 ) break ; len = j - i ; count += ( len ) * ( len + 1 ) / 2 ; i = j ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 7 , 8 , 2 , 3 , 9 } ; int x = 6 ; int n = arr . length ; System . out . println ( countSubArrays ( arr , n , x ) ) ; } }

def countSubArrays ( arr , n , x ) : NEW_LINE INDENT len = 0 NEW_LINE count = 0 NEW_LINE binaryArr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] <= x ) : NEW_LINE INDENT binaryArr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT binaryArr [ i ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( binaryArr [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( binaryArr [ j ] != 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT len = j - i NEW_LINE count += ( len ) * ( int ) ( ( len + 1 ) / 2 ) NEW_LINE i = j NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 , 8 , 2 , 3 , 9 ] NEW_LINE x = 6 NEW_LINE n = len ( arr ) NEW_LINE print ( int ( countSubArrays ( arr , n , x ) ) ) NEW_LINE DEDENT


class GFG { static void alphabetPattern ( int N ) { int left = 0 , middle = N - 1 , right = N + 1 ; for ( int row = 0 ; row < 2 * N - 1 ; row ++ ) { if ( row < N ) System . out . print ( ++ left ) ; else System . out . print ( -- left ) ; for ( int col = 1 ; col < N - 1 ; col ++ ) { if ( row != N - 1 ) System . out . print ( " ▁ ▁ " ) ; else System . out . print ( " ▁ " + middle -- ) ; } if ( row < N ) System . out . print ( " ▁ " + -- right ) ; else System . out . print ( " ▁ " + ++ right ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 4 ; alphabetPattern ( N ) ; } }

def alphabetPattern ( N ) : NEW_LINE INDENT left , middle , right = 0 , N - 1 , N + 1 NEW_LINE for row in range ( 0 , 2 * N - 1 ) : NEW_LINE INDENT if row < N : NEW_LINE INDENT left += 1 NEW_LINE print ( left , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT left -= 1 NEW_LINE print ( left , end = " " ) NEW_LINE DEDENT for col in range ( 1 , N - 1 ) : NEW_LINE INDENT if row != N - 1 : NEW_LINE INDENT print ( " ▁ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " + str ( middle ) , end = " " ) NEW_LINE middle -= 1 NEW_LINE DEDENT DEDENT if row < N : NEW_LINE INDENT right -= 1 NEW_LINE print ( " ▁ " + str ( right ) , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT right += 1 NEW_LINE print ( " ▁ " + str ( right ) , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE alphabetPattern ( N ) NEW_LINE DEDENT


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 ; lbit = ( L >> i ) & 1 ; rbit = ( R >> i ) & 1 ; 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 ) ) ; } }

MAX = 64 ; NEW_LINE def maxOR ( L , R ) : NEW_LINE INDENT if ( L == R ) : NEW_LINE INDENT return L ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( MAX - 1 , - 1 , - 1 ) : NEW_LINE INDENT p = 1 << i ; NEW_LINE lbit = ( L >> i ) & 1 ; NEW_LINE rbit = ( R >> i ) & 1 ; NEW_LINE if ( ( rbit == 1 ) and ( lbit == 0 ) ) : NEW_LINE INDENT ans += ( p << 1 ) - 1 ; NEW_LINE break ; NEW_LINE DEDENT if ( rbit == 1 ) : NEW_LINE INDENT ans += p ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 4 ; R = 5 ; NEW_LINE print ( maxOR ( L , R ) ) ; NEW_LINE DEDENT


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 ) ) ; } }

def submatrixXor ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT top_left = ( i + 1 ) * ( j + 1 ) NEW_LINE bottom_right = ( n - i ) * ( n - j ) NEW_LINE if ( top_left % 2 == 1 and bottom_right % 2 == 1 ) : NEW_LINE INDENT ans = ( ans ^ arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE arr = [ [ 6 , 7 , 13 ] , [ 8 , 3 , 4 ] , [ 9 , 7 , 6 ] ] NEW_LINE print ( submatrixXor ( arr , n ) ) NEW_LINE


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 ) ; } }

def count ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE d , count = 0 , 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT prev = ord ( s [ i - k ] ) NEW_LINE d -= prev NEW_LINE d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE return count NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT s = " bcgabc " NEW_LINE k = 3 NEW_LINE ans = count ( s , k ) NEW_LINE print ( ans ) NEW_LINE


public class MatrixTraversal { private static void printMatrix ( int mat [ ] [ ] , int m , int n , int i , int j , int path [ ] , int idx ) { path [ idx ] = mat [ i ] [ j ] ; if ( i == m - 1 ) { for ( int k = j + 1 ; k < n ; k ++ ) { path [ idx + k - j ] = mat [ i ] [ k ] ; } for ( int l = 0 ; l < idx + n - j ; l ++ ) { System . out . print ( path [ l ] + " ▁ " ) ; } System . out . println ( ) ; return ; } if ( j == n - 1 ) { for ( int k = i + 1 ; k < m ; k ++ ) { path [ idx + k - i ] = mat [ k ] [ j ] ; } for ( int l = 0 ; l < idx + m - i ; l ++ ) { System . out . print ( path [ l ] + " ▁ " ) ; } System . out . println ( ) ; return ; } printMatrix ( mat , m , n , i + 1 , j , path , idx + 1 ) ; printMatrix ( mat , m , n , i , j + 1 , path , idx + 1 ) ; } public static void main ( String [ ] args ) { int m = 2 ; int n = 3 ; int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; int maxLengthOfPath = m + n - 1 ; printMatrix ( mat , m , n , 0 , 0 , new int [ maxLengthOfPath ] , 0 ) ; } }

def printAllPathsUtil ( mat , i , j , m , n , path , pi ) : NEW_LINE INDENT if ( i == m - 1 ) : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT path [ pi + k - j ] = mat [ i ] [ k ] NEW_LINE DEDENT for l in range ( pi + n - j ) : NEW_LINE INDENT print ( path [ l ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE return NEW_LINE DEDENT if ( j == n - 1 ) : NEW_LINE INDENT for k in range ( i , m ) : NEW_LINE INDENT path [ pi + k - i ] = mat [ k ] [ j ] NEW_LINE DEDENT for l in range ( pi + m - i ) : NEW_LINE INDENT print ( path [ l ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE return NEW_LINE DEDENT path [ pi ] = mat [ i ] [ j ] NEW_LINE printAllPathsUtil ( mat , i + 1 , j , m , n , path , pi + 1 ) NEW_LINE printAllPathsUtil ( mat , i , j + 1 , m , n , path , pi + 1 ) NEW_LINE DEDENT def printAllPaths ( mat , m , n ) : NEW_LINE INDENT path = [ 0 for i in range ( m + n ) ] NEW_LINE printAllPathsUtil ( mat , 0 , 0 , m , n , path , 0 ) NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] NEW_LINE printAllPaths ( mat , 2 , 3 ) NEW_LINE


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 ) ) ; } }

def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def FillPrefixSuffix ( prefix , arr , suffix , n ) : NEW_LINE INDENT prefix [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT suffix [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT DEDENT def GCDoutsideRange ( l , r , prefix , suffix , n ) : NEW_LINE INDENT if ( l == 0 ) : NEW_LINE INDENT return suffix [ r + 1 ] NEW_LINE DEDENT if ( r == n - 1 ) : NEW_LINE INDENT return prefix [ l - 1 ] NEW_LINE DEDENT return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) NEW_LINE DEDENT arr = [ 2 , 6 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE prefix = [ ] NEW_LINE suffix = [ ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT prefix . append ( 0 ) NEW_LINE suffix . append ( 0 ) NEW_LINE DEDENT FillPrefixSuffix ( prefix , arr , suffix , n ) NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 1 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 2 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE


import java . util . * ; class solution { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int lcmOfArray ( int arr [ ] , int n ) { if ( n < 1 ) return 0 ; int lcm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) lcm = ( lcm * arr [ i ] ) / gcd ( lcm , arr [ i ] ) ; return lcm ; } static int minPerfectSquare ( int arr [ ] , int n ) { int minPerfectSq ; int lcm = lcmOfArray ( arr , n ) ; minPerfectSq = ( int ) lcm ; int cnt = 0 ; while ( lcm > 1 && lcm % 2 == 0 ) { cnt ++ ; lcm /= 2 ; } if ( cnt % 2 != 0 ) minPerfectSq *= 2 ; int i = 3 ; while ( lcm > 1 ) { cnt = 0 ; while ( lcm % i == 0 ) { cnt ++ ; lcm /= i ; } if ( cnt % 2 != 0 ) minPerfectSq *= i ; i += 2 ; } return minPerfectSq ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 , 7 } ; int n = arr . length ; System . out . println ( minPerfectSquare ( arr , n ) ) ; } }

from math import gcd NEW_LINE def lcmOfArray ( arr , n ) : NEW_LINE INDENT if ( n < 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT lcm = arr [ 0 ] NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT lcm = int ( ( lcm * arr [ i ] ) / gcd ( lcm , arr [ i ] ) ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def minPerfectSquare ( arr , n ) : NEW_LINE INDENT lcm = lcmOfArray ( arr , n ) NEW_LINE minPerfectSq = int ( lcm ) NEW_LINE cnt = 0 NEW_LINE while ( lcm > 1 and lcm % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE lcm /= 2 NEW_LINE DEDENT if ( cnt % 2 != 0 ) : NEW_LINE INDENT minPerfectSq *= 2 NEW_LINE DEDENT i = 3 NEW_LINE while ( lcm > 1 ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE while ( lcm % i == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE lcm /= i NEW_LINE DEDENT if ( cnt % 2 != 0 ) : NEW_LINE INDENT minPerfectSq *= i NEW_LINE DEDENT i += 2 NEW_LINE DEDENT return minPerfectSq NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minPerfectSquare ( arr , n ) ) NEW_LINE DEDENT


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 " ) ; } }

MAX_DIGITS = 20 ; NEW_LINE def isOctal ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT if ( ( n % 10 ) >= 8 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT n = int ( n / 10 ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPalindrome ( n ) : NEW_LINE INDENT divide = 8 if ( isOctal ( n ) == False ) else 10 NEW_LINE octal = [ ] NEW_LINE while ( n != 0 ) : NEW_LINE INDENT octal . append ( n % divide ) NEW_LINE n = int ( n / divide ) NEW_LINE DEDENT j = len ( octal ) - 1 NEW_LINE k = 0 NEW_LINE while ( k <= j ) : NEW_LINE INDENT if ( octal [ j ] != octal [ k ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT j -= 1 NEW_LINE k += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 97 ; NEW_LINE if ( isPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


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 ) ; } }

def printOrder ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT for j in range ( n - 1 , n // 2 - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE printOrder ( arr , n ) NEW_LINE DEDENT


class GFG { static int Profit ( int costPrice , int sellingPrice ) { int profit = ( sellingPrice - costPrice ) ; return profit ; } static int Loss ( int costPrice , int sellingPrice ) { int Loss = ( costPrice - sellingPrice ) ; return Loss ; } public static void main ( String [ ] args ) { int costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) System . out . println ( " No ▁ profit ▁ nor ▁ Loss " ) ; else if ( sellingPrice > costPrice ) System . out . println ( Profit ( costPrice , sellingPrice ) + " ▁ Profit ▁ " ) ; else System . out . println ( Loss ( costPrice , sellingPrice ) + " ▁ Loss ▁ " ) ; } }

def Profit ( costPrice , sellingPrice ) : NEW_LINE INDENT profit = ( sellingPrice - costPrice ) NEW_LINE return profit NEW_LINE DEDENT def Loss ( costPrice , sellingPrice ) : NEW_LINE INDENT Loss = ( costPrice - sellingPrice ) NEW_LINE return Loss NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT costPrice , sellingPrice = 1500 , 2000 NEW_LINE if sellingPrice == costPrice : NEW_LINE INDENT print ( " No ▁ profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT elif sellingPrice > costPrice : NEW_LINE INDENT print ( Profit ( costPrice , sellingPrice ) , " Profit " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( Loss ( costPrice , sellingPrice ) , " Loss " ) NEW_LINE DEDENT DEDENT


class GFG { static int max_sum ( int arr [ ] , int n , int k ) { int dp [ ] = new int [ n ] ; dp [ 0 ] = Math . max ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 2 , 4 , 3 , 1 } ; int n = arr . length ; int k = 4 ; System . out . println ( max_sum ( arr , n , k ) ) ; } }

def max_sum ( arr , n , k ) : NEW_LINE INDENT dp = [ 0 ] * n ; NEW_LINE dp [ 0 ] = max ( 0 , arr [ 0 ] ) ; NEW_LINE i = 1 ; NEW_LINE while ( i < k ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT i = k ; NEW_LINE while ( i < n ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return dp [ n - 1 ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , - 2 , 4 , 3 , 1 ] ; NEW_LINE n = len ( arr ) NEW_LINE k = 4 ; NEW_LINE print ( max_sum ( arr , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public 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 ) ; } return res ; } public static void main ( String argc [ ] ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = arr . length ; System . out . println ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }

def checkBit ( pattern , arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( pattern & arr [ i ] ) == pattern ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def maxAND ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for bit in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT count = checkBit ( res | ( 1 << bit ) , arr , n ) NEW_LINE if ( count >= 2 ) : NEW_LINE INDENT res = res | ( 1 << bit ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 4 , 8 , 6 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ AND ▁ Value ▁ = ▁ " , maxAND ( arr , n ) ) NEW_LINE


class GFG { static final int N = 4 ; static int areSame ( int A [ ] [ ] , int B [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) if ( A [ i ] [ j ] != B [ i ] [ j ] ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; int B [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; if ( areSame ( A , B ) == 1 ) System . out . print ( " Matrices ▁ are ▁ identical " ) ; else System . out . print ( " Matrices ▁ are ▁ not ▁ identical " ) ; } }

N = 4 NEW_LINE def areSame ( A , B ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT A = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE B = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE if ( areSame ( A , B ) == 1 ) : NEW_LINE INDENT print ( " Matrices ▁ are ▁ identical " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Matrices ▁ are ▁ not ▁ identical " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void sortMultiples ( int arr [ ] , int n , int x ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { v . add ( arr [ i ] ) ; } } Collections . sort ( v , Collections . reverseOrder ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { arr [ i ] = v . get ( 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 [ ] = { 125 , 3 , 15 , 6 , 100 , 5 } ; int x = 5 ; int n = arr . length ; sortMultiples ( arr , n , x ) ; printArray ( arr , n ) ; } }

def sortMultiples ( arr , n , x ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = True ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT arr [ i ] = v [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 125 , 3 , 15 , 6 , 100 , 5 ] NEW_LINE x = 5 NEW_LINE n = len ( arr ) NEW_LINE sortMultiples ( arr , n , x ) NEW_LINE printArray ( arr , n ) NEW_LINE DEDENT


class GfG { static class Node { char data ; Node prev ; Node next ; } static Node head = null ; static void rotate ( int N ) { if ( N == 0 ) return ; Node current = head ; int count = 1 ; while ( count < N && current != null ) { current = current . next ; count ++ ; } if ( current == null ) return ; Node NthNode = current ; while ( current . next != null ) current = current . next ; current . next = head ; ( head ) . prev = current ; head = NthNode . next ; ( head ) . prev = null ; NthNode . next = null ; } static void push ( char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head ) ; if ( ( head ) != null ) ( head ) . prev = new_node ; head = new_node ; } static void printList ( Node node ) { while ( node != null && node . next != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } if ( node != null ) System . out . print ( node . data ) ; } public static void main ( String [ ] args ) { push ( ' e ' ) ; push ( ' d ' ) ; push ( ' c ' ) ; push ( ' b ' ) ; push ( ' a ' ) ; int N = 2 ; System . out . println ( " Given ▁ linked ▁ list ▁ " ) ; printList ( head ) ; rotate ( N ) ; System . out . println ( ) ; System . out . println ( " Rotated ▁ Linked ▁ list ▁ " ) ; printList ( head ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , next = None , prev = None , data = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . prev = prev NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def push ( head , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = head NEW_LINE new_node . prev = None NEW_LINE if head is not None : NEW_LINE INDENT head . prev = new_node NEW_LINE DEDENT head = new_node NEW_LINE return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT node = head NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE while ( node is not None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) , NEW_LINE last = node NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def rotate ( start , N ) : NEW_LINE INDENT if N == 0 : NEW_LINE INDENT return NEW_LINE DEDENT current = start NEW_LINE count = 1 NEW_LINE while count < N and current != None : NEW_LINE INDENT current = current . next NEW_LINE count += 1 NEW_LINE DEDENT if current == None : NEW_LINE INDENT return NEW_LINE DEDENT NthNode = current NEW_LINE while current . next != None : NEW_LINE INDENT current = current . next NEW_LINE DEDENT current . next = start NEW_LINE start . prev = current NEW_LINE start = NthNode . next NEW_LINE start . prev = None NEW_LINE NthNode . next = None NEW_LINE return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None NEW_LINE head = push ( head , ' e ' ) NEW_LINE head = push ( head , ' d ' ) NEW_LINE head = push ( head , ' c ' ) NEW_LINE head = push ( head , ' b ' ) NEW_LINE head = push ( head , ' a ' ) NEW_LINE printList ( head ) NEW_LINE print ( " \n " ) NEW_LINE N = 2 NEW_LINE head = rotate ( head , N ) NEW_LINE printList ( head ) NEW_LINE DEDENT


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 ) ; } }

def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return False NEW_LINE DEDENT def isDigitSumOne ( nm ) : NEW_LINE INDENT while ( nm > 9 ) : NEW_LINE INDENT sum_digit = 0 NEW_LINE while ( nm != 0 ) : NEW_LINE INDENT digit = nm % 10 NEW_LINE sum_digit = sum_digit + digit NEW_LINE nm = nm // 10 NEW_LINE DEDENT nm = sum_digit NEW_LINE DEDENT if ( nm == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printValidNums ( m , n ) : NEW_LINE INDENT for i in range ( m , n + 1 ) : NEW_LINE INDENT if ( isComposite ( i ) and isDigitSumOne ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT l = 10 NEW_LINE r = 100 NEW_LINE printValidNums ( m , n ) NEW_LINE


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 " ) ; } }

NO_OF_CHARS = 256 NEW_LINE def arePermutation ( str1 , str2 ) : NEW_LINE INDENT count1 = [ 0 ] * NO_OF_CHARS NEW_LINE count2 = [ 0 ] * NO_OF_CHARS NEW_LINE for i in str1 : NEW_LINE INDENT count1 [ ord ( i ) ] += 1 NEW_LINE DEDENT for i in str2 : NEW_LINE INDENT count2 [ ord ( i ) ] += 1 NEW_LINE DEDENT if len ( str1 ) != len ( str2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in xrange ( NO_OF_CHARS ) : NEW_LINE INDENT if count1 [ i ] != count2 [ i ] : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT str1 = " geeksforgeeks " NEW_LINE str2 = " forgeeksgeeks " NEW_LINE if arePermutation ( str1 , str2 ) : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT print " No " NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void majority ( int a [ ] , int p , int q , int size ) { int start = 0 , ends = q ; int [ ] arr = new int [ p ] ; int k = 0 ; while ( k < p ) { int one = 0 ; for ( int j = start ; j < ends ; j ++ ) { if ( a [ j ] == 1 ) { one ++ ; } } start = ends ; ends = ends + q ; arr [ k ] = one ; k ++ ; } start = 0 ; ends = q ; boolean found = false ; while ( ends > 0 ) { int dist_one = 0 ; for ( int i = 0 ; i < p ; i ++ ) if ( arr [ i ] > q / 2 ) dist_one ++ ; if ( dist_one > p / 2 ) { found = true ; System . out . println ( " Yes " ) ; return ; } start -- ; ends -- ; if ( start < 0 ) start = size + start ; int st = start , en = ends , l = 0 ; while ( en < size ) { if ( a [ st % size ] != a [ en % size ] ) { if ( a [ st % size ] == 1 ) arr [ l ] ++ ; else arr [ l ] -- ; } l ++ ; st = ( st + q ) ; en = en + q ; } } if ( found == false ) { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int p = 3 , q = 3 ; int n = p * q ; int a [ ] = { 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 } ; majority ( a , p , q , n ) ; } }

def majority ( a , p , q , size ) : NEW_LINE INDENT start = 0 NEW_LINE ends = q NEW_LINE arr = [ None ] * p NEW_LINE k = 0 NEW_LINE while ( k < p ) : NEW_LINE INDENT one = 0 NEW_LINE for j in range ( start , ends ) : NEW_LINE INDENT if ( a [ j ] == 1 ) : NEW_LINE INDENT one = one + 1 NEW_LINE DEDENT DEDENT start = ends NEW_LINE ends = ends + q NEW_LINE arr [ k ] = one NEW_LINE k = k + 1 NEW_LINE DEDENT start = 0 NEW_LINE ends = q NEW_LINE found = 0 NEW_LINE while ( ends > 0 ) : NEW_LINE INDENT dist_one = 0 NEW_LINE for i in range ( 0 , p ) : NEW_LINE INDENT if ( arr [ i ] > q / 2 ) : NEW_LINE INDENT dist_one = dist_one + 1 NEW_LINE DEDENT DEDENT if ( dist_one > p / 2 ) : NEW_LINE INDENT found = 1 NEW_LINE print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT start = start - 1 NEW_LINE ends = ends - 1 NEW_LINE if ( start < 0 ) : NEW_LINE INDENT start = size + start NEW_LINE DEDENT st = start NEW_LINE en = ends NEW_LINE l = 0 NEW_LINE while ( en < size ) : NEW_LINE INDENT if ( a [ st % size ] != a [ en % size ] ) : NEW_LINE INDENT if ( a [ st % size ] == 1 ) : NEW_LINE INDENT arr [ l ] = arr [ l ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ l ] = arr [ l ] - 1 NEW_LINE DEDENT DEDENT l = l + 1 NEW_LINE st = ( st + q ) NEW_LINE en = en + q NEW_LINE DEDENT DEDENT if ( found == 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT p = 3 NEW_LINE q = 3 NEW_LINE n = p * q NEW_LINE a = [ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 ] NEW_LINE majority ( a , p , q , n ) NEW_LINE


class GFG { static int nextGreater ( int N ) { int power_of_2 = 1 , shift_count = 0 ; while ( true ) { if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) break ; shift_count ++ ; power_of_2 = power_of_2 * 2 ; } return ( N + power_of_2 ) ; } public static void main ( String [ ] a ) { int N = 11 ; System . out . println ( " The ▁ next ▁ number ▁ is ▁ = ▁ " + nextGreater ( N ) ) ; } }

def nextGreater ( N ) : NEW_LINE INDENT power_of_2 = 1 ; NEW_LINE shift_count = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT shift_count += 1 ; NEW_LINE power_of_2 = power_of_2 * 2 ; NEW_LINE DEDENT return ( N + power_of_2 ) ; NEW_LINE DEDENT N = 11 ; NEW_LINE print ( " The ▁ next ▁ number ▁ is ▁ = " , nextGreater ( N ) ) ; NEW_LINE


import java . io . * ; class GFG { static int count ( String s , int len ) { int MAX = 1000 ; int cur = 0 , dig = 0 ; int [ ] sum = new int [ MAX ] ; int [ ] [ ] dp = new int [ MAX ] [ 3 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s . charAt ( i - 1 ) ) - 48 ; cur += dig ; cur %= 3 ; sum [ i ] = cur ; dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 2 ] ; dp [ i ] [ sum [ i ] ] ++ ; } int ans = 0 , dprev = 0 , value = 0 , dprev2 = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s . charAt ( i - 1 ) ) - 48 ; if ( dig == 8 ) ans ++ ; if ( i - 2 >= 0 ) { dprev = ( int ) ( s . charAt ( i - 2 ) ) - 48 ; value = dprev * 10 + dig ; if ( ( value % 8 == 0 ) && ( value % 3 != 0 ) ) ans ++ ; } if ( i - 3 >= 0 ) { dprev2 = ( int ) ( s . charAt ( i - 3 ) ) - 48 ; dprev = ( int ) ( s . charAt ( i - 2 ) ) - 48 ; value = dprev2 * 100 + dprev * 10 + dig ; if ( value % 8 != 0 ) continue ; ans += ( i - 2 ) ; ans -= ( dp [ i - 3 ] [ sum [ i ] ] ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = "6564525600" ; int len = str . length ( ) ; System . out . println ( count ( str , len ) ) ; } }

def count ( s , Len ) : NEW_LINE INDENT global MAX NEW_LINE cur = 0 NEW_LINE dig = 0 NEW_LINE Sum = [ 0 ] * MAX NEW_LINE dp = [ [ 0 , 0 , 0 ] for i in range ( MAX ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , Len + 1 ) : NEW_LINE INDENT dig = int ( s [ i - 1 ] ) - 48 NEW_LINE cur += dig NEW_LINE cur %= 3 NEW_LINE Sum [ i ] = cur NEW_LINE dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] NEW_LINE dp [ i ] [ 2 ] = dp [ i - 1 ] [ 2 ] NEW_LINE dp [ i ] [ Sum [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE dprev = 0 NEW_LINE value = 0 NEW_LINE dprev2 = 0 NEW_LINE for i in range ( 1 , Len + 1 ) : NEW_LINE INDENT dig = int ( s [ i - 1 ] ) - 48 NEW_LINE if dig == 8 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if i - 2 >= 0 : NEW_LINE INDENT dprev = int ( s [ i - 2 ] ) - 48 NEW_LINE value = dprev * 10 + dig NEW_LINE if ( value % 8 == 0 ) and ( value % 3 != 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if i - 3 >= 0 : NEW_LINE INDENT dprev2 = int ( s [ i - 3 ] ) - 48 NEW_LINE dprev = int ( s [ i - 2 ] ) - 48 NEW_LINE value = ( dprev2 * 100 + dprev * 10 + dig ) NEW_LINE if value % 8 != 0 : NEW_LINE INDENT continue NEW_LINE DEDENT ans += ( i - 2 ) NEW_LINE ans -= ( dp [ i - 3 ] [ Sum [ i ] ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT MAX = 1000 NEW_LINE Str = "6564525600" NEW_LINE Len = len ( Str ) NEW_LINE print ( count ( Str , Len ) ) NEW_LINE


import java . util . Arrays ; class GFG { static void printSortedLevels ( int arr [ ] , int n ) { int level = 0 ; for ( int i = 0 ; i < n ; level ++ ) { int cnt = ( int ) Math . pow ( 2 , level ) ; cnt -= 1 ; int j = Math . min ( i + cnt , n - 1 ) ; Arrays . sort ( arr , i , j + 1 ) ; while ( i <= j ) { System . out . print ( arr [ i ] + " ▁ " ) ; i ++ ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 4 , 9 , 2 , 1 } ; int n = arr . length ; printSortedLevels ( arr , n ) ; } }

from math import pow NEW_LINE def printSortedLevels ( arr , n ) : NEW_LINE INDENT level = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT cnt = int ( pow ( 2 , level ) ) NEW_LINE cnt -= 1 NEW_LINE j = min ( i + cnt , n - 1 ) NEW_LINE arr = arr [ : i ] + sorted ( arr [ i : j + 1 ] ) +   \ NEW_LINE INDENT arr [ j + 1 : ] NEW_LINE DEDENT while ( i <= j ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT print ( ) NEW_LINE level += 1 NEW_LINE DEDENT DEDENT arr = [ 5 , 6 , 4 , 9 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE printSortedLevels ( arr , n ) NEW_LINE


import java . util . Arrays ; class GFG { static int arr [ ] = { 100 , 1000 , 100 , 1000 , 1 } ; static int sum [ ] = new int [ 10000 ] ; static int maxSumWO3Consec ( int n ) { if ( sum [ n ] != - 1 ) return sum [ n ] ; if ( n == 0 ) return sum [ n ] = 0 ; if ( n == 1 ) return sum [ n ] = arr [ 0 ] ; if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; return sum [ n ] = Math . max ( Math . max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n - 1 ] ) , arr [ n - 2 ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } public static void main ( String [ ] args ) { int n = arr . length ; Arrays . fill ( sum , - 1 ) ; System . out . println ( maxSumWO3Consec ( n ) ) ; } }

arr = [ 100 , 1000 , 100 , 1000 , 1 ] NEW_LINE sum = [ - 1 ] * 10000 NEW_LINE def maxSumWO3Consec ( n ) : NEW_LINE INDENT if ( sum [ n ] != - 1 ) : NEW_LINE INDENT return sum [ n ] NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT sum [ n ] = 0 NEW_LINE return sum [ n ] NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT sum [ n ] = arr [ 0 ] NEW_LINE return sum [ n ] NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT sum [ n ] = arr [ 1 ] + arr [ 0 ] NEW_LINE return sum [ n ] NEW_LINE DEDENT sum [ n ] = max ( max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n - 1 ] ) , arr [ n - 2 ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) NEW_LINE return sum [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = len ( arr ) NEW_LINE print ( maxSumWO3Consec ( n ) ) NEW_LINE DEDENT


import java . util . HashSet ; import java . util . Set ; public class GfG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) s . add ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! s . contains ( a [ i ] ) ) 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 ) ; } }

def findMissing ( a , b , n , m ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT s [ b [ i ] ] = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in s . keys ( ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT a = [ 1 , 2 , 6 , 3 , 4 , 5 ] NEW_LINE b = [ 2 , 4 , 3 , 1 , 0 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE findMissing ( a , b , n , m ) NEW_LINE


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 ) ) ; } }

def ispowerof2 ( num ) : NEW_LINE INDENT if ( ( num & ( num - 1 ) ) == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 549755813888 NEW_LINE print ( ispowerof2 ( num ) ) NEW_LINE DEDENT


class GFG { static int factorial ( int n ) { return ( n == 1 || n == 0 ) ? 1 : factorial ( n - 1 ) * n ; } static int gcdOfFactorial ( int [ ] arr , int n ) { int minm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) minm = minm > arr [ i ] ? arr [ i ] : minm ; return factorial ( minm ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 12 , 122 , 34 , 15 } ; int n = arr . length ; System . out . println ( gcdOfFactorial ( arr , n ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT if n == 1 or n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return factorial ( n - 1 ) * n NEW_LINE DEDENT DEDENT def gcdOfFactorial ( arr , n ) : NEW_LINE INDENT minm = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if minm > arr [ i ] : NEW_LINE INDENT minm = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = minm NEW_LINE DEDENT DEDENT return factorial ( minm ) NEW_LINE DEDENT arr = [ 9 , 12 , 122 , 34 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( gcdOfFactorial ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void specialNumber ( int n ) { if ( n < 10 || n > 99 ) System . out . println ( " Invalid ▁ Input ! ▁ " + " Number ▁ should ▁ have ▁ " + "2 ▁ digits ▁ only " ) ; else { int first = n / 10 ; int last = n % 10 ; int sum = first + last ; int pro = first * last ; if ( ( sum + pro ) == n ) { System . out . println ( n + " ▁ is ▁ a ▁ Special " + " ▁ Two - Digit ▁ Number " ) ; } else { System . out . println ( n + " ▁ is ▁ Not ▁ a ▁ Special " + " ▁ Two - Digit ▁ Number " ) ; } } } public static void main ( String args [ ] ) { int n = 59 ; specialNumber ( n ) ; } }

def specialNumber ( n ) : NEW_LINE INDENT if ( n < 10 or n > 99 ) : NEW_LINE INDENT print ( " Invalid ▁ Input ! ▁ Number " , " ▁ should ▁ have ▁ 2 ▁ digits ▁ only " ) NEW_LINE DEDENT else : NEW_LINE INDENT first = n // 10 NEW_LINE last = n % 10 NEW_LINE sum = first + last NEW_LINE pro = first * last NEW_LINE if ( ( sum + pro ) == n ) : NEW_LINE INDENT print ( n , " ▁ is ▁ a ▁ Special ▁ " , " Two - Digit ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " ▁ is ▁ Not ▁ a ▁ " , " Special ▁ Two - Digit ▁ Number " ) NEW_LINE DEDENT DEDENT DEDENT n = 59 NEW_LINE specialNumber ( n ) NEW_LINE


import java . util . * ; public class GFG { static int LongestFibSubseq ( int A [ ] , int n ) { TreeSet < Integer > S = new TreeSet < > ( ) ; for ( int t : A ) { S . add ( t ) ; } int maxLen = 0 , x , y ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { x = A [ j ] ; y = A [ i ] + A [ j ] ; int length = 3 ; while ( S . contains ( y ) && ( y != S . last ( ) ) ) { int z = x + y ; x = y ; y = z ; maxLen = Math . max ( maxLen , ++ length ) ; } } } return maxLen >= 3 ? maxLen : 0 ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = A . length ; System . out . print ( LongestFibSubseq ( A , n ) ) ; } }

def LongestFibSubseq ( A , n ) : NEW_LINE INDENT S = set ( A ) NEW_LINE maxLen = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT x = A [ j ] NEW_LINE y = A [ i ] + A [ j ] NEW_LINE length = 2 NEW_LINE while y in S : NEW_LINE INDENT z = x + y NEW_LINE x = y NEW_LINE y = z NEW_LINE length += 1 NEW_LINE maxLen = max ( maxLen , length ) NEW_LINE DEDENT DEDENT DEDENT return maxLen if maxLen >= 3 else 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] NEW_LINE n = len ( A ) NEW_LINE print ( LongestFibSubseq ( A , n ) ) NEW_LINE DEDENT


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

def isInGivenBase ( Str , base ) : NEW_LINE INDENT if ( base > 16 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( base <= 10 ) : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) or ( ord ( Str [ i ] ) >= ord ( ' A ' ) and ord ( Str [ i ] ) < ( ord ( ' A ' ) + base - 10 ) ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT Str = " AF87" NEW_LINE if ( isInGivenBase ( Str , 16 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int SumF ( int S [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int ) ( S [ i ] * Math . pow ( 2 , n - i - 1 ) ) ; return sum ; } static int SumL ( int S [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int ) ( S [ i ] * Math . pow ( 2 , i ) ) ; return sum ; } static int sumSetDiff ( int S [ ] , int n ) { return SumL ( S , n ) - SumF ( S , n ) ; } public static void main ( String arg [ ] ) { int n = 4 ; int S [ ] = { 5 , 2 , 9 , 6 } ; System . out . println ( sumSetDiff ( S , n ) ) ; } }

def SumF ( S , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( S [ i ] * pow ( 2 , n - i - 1 ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def SumL ( S , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( S [ i ] * pow ( 2 , i ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumSetDiff ( S , n ) : NEW_LINE INDENT return SumL ( S , n ) - SumF ( S , n ) NEW_LINE DEDENT n = 4 NEW_LINE S = [ 5 , 2 , 9 , 6 ] NEW_LINE print ( sumSetDiff ( S , n ) ) NEW_LINE


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 ) ) ; } }

def smallestNumber ( n ) : NEW_LINE INDENT num = 0 NEW_LINE s = " " NEW_LINE duplicate = n NEW_LINE while ( n ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n //= 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT digit = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( ( digit & 1 ) == 0 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) + 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 1 NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2397 NEW_LINE print ( smallestNumber ( N ) ) NEW_LINE DEDENT


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 ) ) ; } }

def find_greatest_divisor ( l , r ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT return l ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT l = 2 ; NEW_LINE r = 12 ; NEW_LINE print ( find_greatest_divisor ( l , r ) ) ; NEW_LINE


import java . io . * ; class GFG { static void multiply ( int a [ ] [ ] , int b [ ] [ ] ) { int mul [ ] [ ] = new int [ 3 ] [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { mul [ i ] [ j ] = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; } } for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) a [ i ] [ j ] = mul [ i ] [ j ] ; } static int power ( int F [ ] [ ] , int n ) { int M [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 1 ) return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; } static int findNthTerm ( int n ) { int F [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; return power ( F , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " F ( 5 ) ▁ is ▁ " + findNthTerm ( n ) ) ; } }

def multiply ( a , b ) : NEW_LINE INDENT mul = [ [ 0 for x in range ( 3 ) ] for y in range ( 3 ) ] ; NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] = 0 ; NEW_LINE for k in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT a [ i ] [ j ] = mul [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return a ; NEW_LINE DEDENT def power ( F , n ) : NEW_LINE INDENT M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT power ( F , int ( n / 2 ) ) ; NEW_LINE F = multiply ( F , F ) ; NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT F = multiply ( F , M ) ; NEW_LINE DEDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT def findNthTerm ( n ) : NEW_LINE INDENT F = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE return power ( F , n - 2 ) ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( " F ( 5 ) ▁ is " , findNthTerm ( n ) ) ; NEW_LINE


import java . util . Scanner ; class Pattern { void display ( ) { int n = 5 ; int space = n / 2 , num = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= space ; j ++ ) System . out . print ( " ▁ " ) ; int count = num / 2 + 1 ; for ( int k = 1 ; k <= num ; k ++ ) { System . out . print ( count ) ; if ( k <= num / 2 ) count -- ; else count ++ ; } System . out . println ( ) ; if ( i <= n / 2 ) { space = space - 1 ; num = num + 2 ; } else { space = space + 1 ; num = num - 2 ; } } } public static void main ( String [ ] args ) { Pattern p = new Pattern ( ) ; p . display ( ) ; } }

def display ( ) : NEW_LINE INDENT n = 5 NEW_LINE space = n // 2 NEW_LINE num = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , space + 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT count = num // 2 + 1 NEW_LINE for k in range ( 1 , num + 1 ) : NEW_LINE INDENT print ( count , end = " " ) NEW_LINE if ( k <= num // 2 ) : NEW_LINE INDENT count = count - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT print ( ) NEW_LINE if ( i <= n // 2 ) : NEW_LINE INDENT space = space - 1 NEW_LINE num = num + 2 NEW_LINE DEDENT else : NEW_LINE INDENT space = space + 1 NEW_LINE num = num - 2 NEW_LINE DEDENT DEDENT DEDENT display ( ) NEW_LINE