import java . util . * ; class GFG { static boolean check_prime ( int n ) { if ( n <= 1 ) { return false ; } if ( n <= 3 ) { return true ; } if ( n % 2 == 0 || n % 3 == 0 ) { return false ; } for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static int countPrimeFrequent ( String s ) { int count = 0 ; Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( mp . containsKey ( s . charAt ( i ) ) ) { mp . put ( s . charAt ( i ) , mp . get ( s . charAt ( i ) ) + 1 ) ; } else { mp . put ( s . charAt ( i ) , 1 ) ; } } for ( Map . Entry < Character , Integer > entry : mp . entrySet ( ) ) { if ( check_prime ( entry . getValue ( ) ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; System . out . println ( countPrimeFrequent ( s ) ) ; } }

def check_prime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , n + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countPrimeFrequent ( s ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT mp . setdefault ( s [ i ] , 0 ) NEW_LINE mp [ s [ i ] ] += 1 NEW_LINE DEDENT for i in mp . keys ( ) : NEW_LINE INDENT if ( check_prime ( mp [ i ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE print ( countPrimeFrequent ( s ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { public static int squaresum ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( squaresum ( n ) ) ; } }

def squaresum ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 NEW_LINE DEDENT n = 4 NEW_LINE print ( squaresum ( n ) ) ; NEW_LINE


import java . io . * ; class GFG { static float rhombusarea ( float l , float b ) { if ( l < 0 || b < 0 ) return - 1 ; return ( l * b ) / 2 ; } public static void main ( String [ ] args ) { float l = 16 , b = 6 ; System . out . println ( rhombusarea ( l , b ) ) ; } }

def rhombusarea ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( l * b ) / 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 16 NEW_LINE b = 6 NEW_LINE print ( rhombusarea ( l , b ) ) NEW_LINE DEDENT


class GFG { static int MinimumX ( int a , int b , int c , int k ) { int x = Integer . MAX_VALUE ; if ( k <= c ) return 0 ; int h = k - c ; int l = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) { x = Math . min ( x , m ) ; h = m - 1 ; } else if ( ( a * m * m ) + ( b * m ) < ( k - c ) ) l = m + 1 ; else return m ; } return x ; } public static void main ( String [ ] args ) { int a = 3 , b = 2 , c = 4 , k = 15 ; System . out . println ( MinimumX ( a , b , c , k ) ) ; } }

def MinimumX ( a , b , c , k ) : NEW_LINE INDENT x = 10 ** 9 NEW_LINE if ( k <= c ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT h = k - c NEW_LINE l = 0 NEW_LINE while ( l <= h ) : NEW_LINE INDENT m = ( l + h ) // 2 NEW_LINE if ( ( a * m * m ) + ( b * m ) > ( k - c ) ) : NEW_LINE INDENT x = min ( x , m ) NEW_LINE h = m - 1 NEW_LINE DEDENT elif ( ( a * m * m ) + ( b * m ) < ( k - c ) ) : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return m NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT a , b , c , k = 3 , 2 , 4 , 15 NEW_LINE print ( MinimumX ( a , b , c , k ) ) NEW_LINE


import java . util . * ; class GFG { static int maxSize = ( int ) ( 1e5 + 5 ) ; static boolean [ ] isFib = new boolean [ maxSize ] ; static int [ ] prefix = new int [ maxSize ] ; static int digitSum ( int num ) { int s = 0 ; while ( num != 0 ) { s = s + num % 10 ; num = num / 10 ; } return s ; } static void generateFibonacci ( ) { Arrays . fill ( isFib , false ) ; int prev = 0 , curr = 1 ; isFib [ prev ] = isFib [ curr ] = true ; while ( curr < maxSize ) { int temp = curr + prev ; if ( temp < maxSize ) isFib [ temp ] = true ; prev = curr ; curr = temp ; } } static void precompute ( int k ) { generateFibonacci ( ) ; for ( int i = 1 ; i < maxSize ; i ++ ) { int sum = digitSum ( i ) ; if ( isFib [ sum ] == true && sum % k == 0 ) { prefix [ i ] ++ ; } } for ( int i = 1 ; i < maxSize ; i ++ ) { prefix [ i ] = prefix [ i ] + prefix [ i - 1 ] ; } } static void performQueries ( int k , int q , int [ ] [ ] query ) { precompute ( k ) ; for ( int i = 0 ; i < q ; i ++ ) { int l = query [ i ] [ 0 ] , r = query [ i ] [ 1 ] ; int cnt = prefix [ r ] - prefix [ l - 1 ] ; System . out . print ( cnt + " \n " ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] query = { { 1 , 11 } , { 5 , 15 } , { 2 , 24 } } ; int k = 2 , q = query . length ; performQueries ( k , q , query ) ; } }

maxSize = 100005 NEW_LINE isFib = [ False ] * ( maxSize ) NEW_LINE prefix = [ 0 ] * maxSize NEW_LINE def digitSum ( num ) : NEW_LINE INDENT s = 0 NEW_LINE while ( num != 0 ) : NEW_LINE INDENT s = s + num % 10 NEW_LINE num = num // 10 NEW_LINE DEDENT return s NEW_LINE DEDENT def generateFibonacci ( ) : NEW_LINE INDENT global isFib NEW_LINE prev = 0 NEW_LINE curr = 1 NEW_LINE isFib [ prev ] = True NEW_LINE isFib [ curr ] = True NEW_LINE while ( curr < maxSize ) : NEW_LINE INDENT temp = curr + prev NEW_LINE if temp < maxSize : NEW_LINE INDENT isFib [ temp ] = True NEW_LINE DEDENT prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def precompute ( k ) : NEW_LINE INDENT generateFibonacci ( ) NEW_LINE global prefix NEW_LINE for i in range ( 1 , maxSize ) : NEW_LINE INDENT sum = digitSum ( i ) NEW_LINE if ( isFib [ sum ] == True and sum % k == 0 ) : NEW_LINE INDENT prefix [ i ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , maxSize ) : NEW_LINE INDENT prefix [ i ] = prefix [ i ] + prefix [ i - 1 ] NEW_LINE DEDENT DEDENT def performQueries ( k , q , query ) : NEW_LINE INDENT precompute ( k ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT l = query [ i ] [ 0 ] NEW_LINE r = query [ i ] [ 1 ] NEW_LINE cnt = prefix [ r ] - prefix [ l - 1 ] NEW_LINE print ( cnt ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT query = [ [ 1 , 11 ] , [ 5 , 15 ] , [ 2 , 24 ] ] NEW_LINE k = 2 NEW_LINE q = len ( query ) NEW_LINE performQueries ( k , q , query ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static double MinimumMail ( int n , int k , int x ) { double m = ( n - 1 ) + Math . ceil ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + Math . ceil ( n * 1.0 / x ) * ( k - n ) ; return m ; } public static void main ( String [ ] args ) { int N = 4 ; int K = 9 ; int X = 2 ; System . out . print ( ( int ) MinimumMail ( N , K , X ) + " \n " ) ; } }

import math NEW_LINE def MinimumMail ( n , k , x ) : NEW_LINE INDENT m = ( ( n - 1 ) + int ( math . ceil ( ( n - 1 ) * 1.0 / x ) * ( n - 1 ) + math . ceil ( n * 1.0 / x ) * ( k - n ) ) ) ; NEW_LINE return m ; NEW_LINE DEDENT N = 4 ; NEW_LINE K = 9 ; NEW_LINE X = 2 ; NEW_LINE print ( MinimumMail ( N , K , X ) ) ; NEW_LINE


import java . io . * ; class GFG { static int palindrome ( int arr [ ] , int begin , int end ) { if ( begin >= end ) { return 1 ; } if ( arr [ begin ] == arr [ end ] ) { return palindrome ( arr , begin + 1 , end - 1 ) ; } else { return 0 ; } } public static void main ( String [ ] args ) { int a [ ] = { 3 , 6 , 0 , 6 , 3 } ; int n = a . length ; if ( palindrome ( a , 0 , n - 1 ) == 1 ) System . out . print ( " Palindrome " ) ; else System . out . println ( " Not ▁ Palindrome " ) ; } }

def palindrome ( arr , begin , end ) : NEW_LINE INDENT if ( begin >= end ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( arr [ begin ] == arr [ end ] ) : NEW_LINE INDENT return palindrome ( arr , begin + 1 , end - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 6 , 0 , 6 , 3 ] NEW_LINE n = len ( a ) NEW_LINE if ( palindrome ( a , 0 , n - 1 ) == 1 ) : NEW_LINE INDENT print ( " Palindrome " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Palindrome " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int totalPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = countSetBits ( arr [ i ] ) ; if ( m . containsKey ( count ) ) m . put ( count , m . get ( count ) + 1 ) ; else m . put ( count , 1 ) ; } int result = 0 ; for ( Map . Entry < Integer , Integer > entry : m . entrySet ( ) ) { int value = entry . getValue ( ) ; result += ( ( value * ( value - 1 ) ) / 2 ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 5 , 3 , 9 , 1 , 2 } ; int n = arr . length ; System . out . println ( totalPairs ( arr , n ) ) ; } }

def totalPairs ( arr , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = bin ( arr [ i ] ) . count ( '1' ) NEW_LINE m [ x ] = m . get ( x , 0 ) + 1 ; NEW_LINE DEDENT result = 0 NEW_LINE for it in m : NEW_LINE INDENT result += ( m [ it ] * ( m [ it ] - 1 ) ) // 2 NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 7 , 5 , 3 , 9 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( totalPairs ( arr , n ) ) NEW_LINE


import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; public class GFG { static List < Integer > factors = new ArrayList < > ( ) ; static void findFactors ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . add ( factors . size ( ) , i ) ; } else { factors . add ( factors . size ( ) , n / i ) ; factors . add ( factors . size ( ) , i ) ; } } } } static int findProduct ( int n ) { int product = - 1 ; int si = factors . size ( ) ; for ( int i = 0 ; i < si ; i ++ ) { for ( int j = 0 ; j < si ; j ++ ) { for ( int k = 0 ; k < si ; k ++ ) { int s = factors . get ( i ) + factors . get ( j ) + factors . get ( k ) ; if ( Collections . binarySearch ( factors , n - s ) >= 0 ) { int p = factors . get ( i ) * factors . get ( j ) * factors . get ( k ) * ( n - s ) ; if ( p > product ) { product = p ; } } } } } return product ; } public static void main ( String [ ] args ) { int n = 10 ; findFactors ( n ) ; Collections . sort ( factors ) ; System . out . println ( findProduct ( n ) ) ; } }

factors = [ ] NEW_LINE def findFactors ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if i * i > n : NEW_LINE INDENT break NEW_LINE DEDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n / i ) == i ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . append ( n // i ) NEW_LINE factors . append ( i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def findProduct ( n ) : NEW_LINE INDENT product = - 1 NEW_LINE si = len ( factors ) NEW_LINE for i in range ( si ) : NEW_LINE INDENT for j in range ( si ) : NEW_LINE INDENT for k in range ( si ) : NEW_LINE INDENT s = factors [ i ] + factors [ j ] + factors [ k ] NEW_LINE if ( ( n - s ) in factors ) : NEW_LINE INDENT p = factors [ i ] * factors [ j ] *   \ NEW_LINE INDENT factors [ k ] * ( n - s ) NEW_LINE DEDENT if ( p > product ) : NEW_LINE INDENT product = p NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return product NEW_LINE DEDENT n = 10 NEW_LINE findFactors ( n ) NEW_LINE factors = sorted ( factors ) NEW_LINE print ( findProduct ( n ) ) NEW_LINE


class GFG { static boolean checkSpecialPrime ( boolean [ ] sieve , int num ) { while ( num > 0 ) { if ( sieve [ num ] ) { return false ; } num /= 10 ; } return true ; } static void findSpecialPrime ( int N ) { boolean [ ] sieve = new boolean [ N * 10 + 1 ] ; sieve [ 0 ] = sieve [ 1 ] = true ; for ( int i = 2 ; i <= N * 10 ; i ++ ) { if ( ! sieve [ i ] ) { for ( int j = i * i ; j <= N * 10 ; j += i ) { sieve [ j ] = true ; } } } while ( true ) { if ( checkSpecialPrime ( sieve , N ) ) { System . out . println ( N ) ; break ; } else N ++ ; } } public static void main ( String [ ] args ) { int N = 379 ; findSpecialPrime ( N ) ; N = 100 ; findSpecialPrime ( N ) ; } }

def checkSpecialPrime ( sieve , num ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT if ( sieve [ num ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT num = int ( num / 10 ) NEW_LINE DEDENT return True NEW_LINE DEDENT def findSpecialPrime ( N ) : NEW_LINE INDENT sieve = [ True for i in range ( N * 10 + 1 ) ] NEW_LINE sieve [ 0 ] = False NEW_LINE sieve [ 1 ] = False NEW_LINE for i in range ( 2 , N * 10 + 1 ) : NEW_LINE INDENT if ( sieve [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N * 10 + 1 , i ) : NEW_LINE INDENT sieve [ j ] = False NEW_LINE DEDENT DEDENT DEDENT while ( True ) : NEW_LINE INDENT if ( checkSpecialPrime ( sieve , N ) ) : NEW_LINE INDENT print ( N ) NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT N += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 379 NEW_LINE findSpecialPrime ( N ) NEW_LINE N = 100 NEW_LINE findSpecialPrime ( N ) NEW_LINE DEDENT


import java . util . HashMap ; class GFG { public static int countOccurence ( int n , int [ ] arr , int k ) { int cnt , ans = 0 ; HashMap < Integer , Boolean > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { cnt = 0 ; if ( hash . get ( arr [ i ] ) != null && hash . get ( arr [ i ] ) == true ) continue ; hash . put ( arr [ i ] , true ) ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] == arr [ i ] ) cnt ++ ; if ( cnt >= k ) break ; } if ( cnt >= k ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 3 } ; int n = arr . length ; int k = 1 ; System . out . println ( countOccurence ( n , arr , k ) ) ; } }

def countOccurrence ( n , arr , k ) : NEW_LINE INDENT cnt , ans = 0 , 0 NEW_LINE Hash = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE if ( arr [ i ] in Hash . keys ( ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT Hash [ arr [ i ] ] = 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] == arr [ i ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT if ( cnt >= k ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( cnt >= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 1 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 1 NEW_LINE print ( countOccurrence ( n , arr , k ) ) NEW_LINE


import java . math . * ; class GFG { public static void cal_IST ( int h , double r ) { double IST = ( h * r * 1.0 ) / 360 ; int int_IST = ( int ) IST ; int float_IST = ( int ) Math . ceil ( ( int ) ( ( IST - int_IST ) * 60 ) ) ; System . out . println ( int_IST + " : " + float_IST ) ; } public static void main ( String [ ] args ) { int h = 20 ; double r = 150 ; cal_IST ( h , r ) ; } }

from math import ceil NEW_LINE def cal_IST ( h , r ) : NEW_LINE INDENT IST = round ( ( h * r * 1.0 ) / 360 , 3 ) ; NEW_LINE int_IST = int ( IST ) ; NEW_LINE float_IST = ceil ( ( IST - int_IST ) * 60 ) ; NEW_LINE print ( int_IST , " : " , float_IST ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT h = 20 ; NEW_LINE r = 150 ; NEW_LINE cal_IST ( h , r ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 1000000 ; static boolean [ ] prime = new boolean [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * p ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int sumPrime ( int k ) { SieveOfEratosthenes ( ) ; Vector < Integer > v = new Vector < > ( ) ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) v . add ( i ) ; } int sum = 0 ; int skip = ( k * ( k - 1 ) ) / 2 ; while ( k > 0 ) { sum += v . get ( skip ) ; skip ++ ; k -- ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . println ( sumPrime ( k ) ) ; } }

from math import sqrt NEW_LINE MAX = 1000000 NEW_LINE prime = [ True ] * MAX NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumPrime ( k ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE DEDENT DEDENT sum = 0 ; NEW_LINE skip = ( k * ( k - 1 ) ) // 2 ; NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sum += v [ skip ] ; NEW_LINE skip += 1 ; NEW_LINE k -= 1 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 ; NEW_LINE print ( sumPrime ( k ) ) ; NEW_LINE DEDENT


import java . util . * ; class lics { static int LongIncrConseqSubseq ( int arr [ ] , int n ) { HashMap < Integer , Integer > map = new HashMap < > ( ) ; map . put ( arr [ 0 ] , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( map . containsKey ( arr [ i ] - 1 ) ) { map . put ( arr [ i ] , map . get ( arr [ i ] - 1 ) + 1 ) ; map . remove ( arr [ i ] - 1 ) ; } else { map . put ( arr [ i ] , 1 ) ; } } return Collections . max ( map . values ( ) ) ; } public static void main ( String args [ ] ) { Scanner sc = new Scanner ( System . in ) ; int n = sc . nextInt ( ) ; int arr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = sc . nextInt ( ) ; System . out . println ( LongIncrConseqSubseq ( arr , n ) ) ; } }

from collections import defaultdict NEW_LINE import sys NEW_LINE def longestSubsequence ( a , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE dp = [ 0 for i in range ( n ) ] NEW_LINE maximum = - sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] - 1 in mp : NEW_LINE INDENT lastIndex = mp [ a [ i ] - 1 ] - 1 NEW_LINE dp [ i ] = 1 + dp [ lastIndex ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT mp [ a [ i ] ] = i + 1 NEW_LINE maximum = max ( maximum , dp [ i ] ) NEW_LINE DEDENT return maximum NEW_LINE DEDENT a = [ 3 , 10 , 3 , 11 , 4 , 5 , 6 , 7 , 8 , 12 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestSubsequence ( a , n ) ) NEW_LINE


class GFG { static int parity ( int a ) { return a % 3 ; } static int solve ( int [ ] array , int size ) { int operations = 0 ; for ( int i = 0 ; i < size - 1 ; i ++ ) { if ( parity ( array [ i ] ) == parity ( array [ i + 1 ] ) ) { operations ++ ; if ( i + 2 < size ) { int pari1 = parity ( array [ i ] ) ; int pari2 = parity ( array [ i + 2 ] ) ; if ( pari1 == pari2 ) { if ( pari1 == 0 ) array [ i + 1 ] = 1 ; else if ( pari1 == 1 ) array [ i + 1 ] = 0 ; else array [ i + 1 ] = 1 ; } else { if ( ( pari1 == 0 && pari2 == 1 ) || ( pari1 == 1 && pari2 == 0 ) ) array [ i + 1 ] = 2 ; if ( ( pari1 == 1 && pari2 == 2 ) || ( pari1 == 2 && pari2 == 1 ) ) array [ i + 1 ] = 0 ; if ( ( pari1 == 2 && pari2 == 0 ) || ( pari1 == 0 && pari2 == 2 ) ) array [ i + 1 ] = 1 ; } } } } return operations ; } public static void main ( String arg [ ] ) { int [ ] array = { 2 , 1 , 3 , 0 } ; int size = array . length ; System . out . println ( solve ( array , size ) ) ; } }

def parity ( a ) : NEW_LINE INDENT return a % 3 NEW_LINE DEDENT def solve ( array , size ) : NEW_LINE INDENT operations = 0 NEW_LINE for i in range ( 0 , size - 1 ) : NEW_LINE INDENT if parity ( array [ i ] ) == parity ( array [ i + 1 ] ) : NEW_LINE INDENT operations += 1 NEW_LINE if i + 2 < size : NEW_LINE INDENT pari1 = parity ( array [ i ] ) NEW_LINE pari2 = parity ( array [ i + 2 ] ) NEW_LINE if pari1 == pari2 : NEW_LINE INDENT if pari1 == 0 : NEW_LINE INDENT array [ i + 1 ] = 1 NEW_LINE DEDENT elif pari1 == 1 : NEW_LINE INDENT array [ i + 1 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT array [ i + 1 ] = 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( ( pari1 == 0 and pari2 == 1 ) or ( pari1 == 1 and pari2 == 0 ) ) : NEW_LINE INDENT array [ i + 1 ] = 2 NEW_LINE DEDENT if ( ( pari1 == 1 and pari2 == 2 ) or ( pari1 == 2 and pari2 == 1 ) ) : NEW_LINE INDENT array [ i + 1 ] = 0 NEW_LINE DEDENT if ( ( pari1 == 2 and pari2 == 0 ) and ( pari1 == 0 and pari2 == 2 ) ) : NEW_LINE INDENT array [ i + 1 ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return operations NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT array = [ 2 , 1 , 3 , 0 ] NEW_LINE size = len ( array ) NEW_LINE print ( solve ( array , size ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int getLeftMostZero ( Queue < Integer > zero ) { if ( zero . isEmpty ( ) ) return - 1 ; zero . remove ( ) ; return 0 ; } static int getLeftMostOne ( Queue < Integer > one ) { if ( one . isEmpty ( ) ) return - 1 ; one . remove ( ) ; return 1 ; } static int getLeftMostElement ( Queue < Integer > zero , Queue < Integer > one ) { if ( zero . isEmpty ( ) && one . isEmpty ( ) ) return - 1 ; else if ( zero . isEmpty ( ) ) { one . remove ( ) ; return 1 ; } else if ( one . isEmpty ( ) ) { zero . remove ( ) ; return 0 ; } int res = ( zero . peek ( ) < one . peek ( ) ) ? 0 : 1 ; if ( res == 0 ) zero . remove ( ) ; else one . remove ( ) ; return res ; } static void performQueries ( int arr [ ] , int n , int queries [ ] , int q ) { Queue < Integer > zero = new LinkedList < > ( ) ; Queue < Integer > one = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) zero . add ( i ) ; else one . add ( i ) ; } for ( int i = 0 ; i < q ; i ++ ) { int type = queries [ i ] ; switch ( type ) { case 1 : System . out . println ( getLeftMostZero ( zero ) ) ; break ; case 2 : System . out . println ( getLeftMostOne ( one ) ) ; break ; case 3 : System . out . println ( getLeftMostElement ( zero , one ) ) ; break ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; int queries [ ] = { 1 , 3 , 1 } ; int q = queries . length ; performQueries ( arr , n , queries , q ) ; } }

from collections import deque NEW_LINE zero = deque ( ) NEW_LINE one = deque ( ) NEW_LINE def getLeftMostZero ( ) : NEW_LINE INDENT if not len ( zero ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT zero . popleft ( ) NEW_LINE return 0 NEW_LINE DEDENT def getLeftMostOne ( ) : NEW_LINE INDENT if not len ( one ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT one . popleft ( ) NEW_LINE return 1 NEW_LINE DEDENT def getLeftMostElement ( ) : NEW_LINE INDENT if not len ( zero ) and not len ( one ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif not len ( zero ) : NEW_LINE INDENT one . popleft ( ) NEW_LINE return 1 NEW_LINE DEDENT elif not len ( one ) : NEW_LINE INDENT zero . popleft ( ) NEW_LINE return 0 NEW_LINE DEDENT res = 0 if zero [ 0 ] < one [ 0 ] else 1 NEW_LINE if res == 0 : NEW_LINE INDENT zero . popleft ( ) NEW_LINE DEDENT else : NEW_LINE INDENT one . popleft ( ) NEW_LINE DEDENT return res NEW_LINE DEDENT def performQueries ( arr : list , n : int , queries : list , q : int ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT zero . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT one . append ( i ) NEW_LINE DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT type = queries [ i ] NEW_LINE if type == 1 : NEW_LINE INDENT print ( getLeftMostZero ( ) ) NEW_LINE DEDENT elif type == 2 : NEW_LINE INDENT print ( getLeftMostOne ( ) ) NEW_LINE DEDENT elif type == 3 : NEW_LINE INDENT print ( getLeftMostElement ( ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE queries = [ 1 , 3 , 1 ] NEW_LINE q = len ( queries ) NEW_LINE performQueries ( arr , n , queries , q ) NEW_LINE DEDENT


class Test { static void convertOpposite ( StringBuffer str ) { int ln = str . length ( ) ; for ( int i = 0 ; i < ln ; i ++ ) { Character c = str . charAt ( i ) ; if ( Character . isLowerCase ( c ) ) str . replace ( i , i + 1 , Character . toUpperCase ( c ) + " " ) ; else str . replace ( i , i + 1 , Character . toLowerCase ( c ) + " " ) ; } } public static void main ( String [ ] args ) { StringBuffer str = new StringBuffer ( " GeEkSfOrGeEkS " ) ; convertOpposite ( str ) ; System . out . println ( str ) ; } }

def convertOpposite ( str ) : NEW_LINE INDENT ln = len ( str ) NEW_LINE for i in range ( ln ) : NEW_LINE INDENT if str [ i ] >= ' a ' and str [ i ] <= ' z ' : NEW_LINE INDENT str [ i ] = chr ( ord ( str [ i ] ) - 32 ) NEW_LINE DEDENT elif str [ i ] >= ' A ' and str [ i ] <= ' Z ' : NEW_LINE INDENT str [ i ] = chr ( ord ( str [ i ] ) + 32 ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " GeEkSfOrGeEkS " NEW_LINE str = list ( str ) NEW_LINE convertOpposite ( str ) NEW_LINE str = ' ' . join ( str ) NEW_LINE print ( str ) NEW_LINE DEDENT


import java . util . * ; class solution { static void printThreeParts ( int N ) { if ( N % 3 == 0 ) System . out . println ( " x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " + ( N - 2 ) ) ; else System . out . println ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " + ( N - 3 ) ) ; } public static void main ( String args [ ] ) { int N = 10 ; printThreeParts ( N ) ; } }

def printThreeParts ( N ) : NEW_LINE INDENT if ( N % 3 == 0 ) : NEW_LINE INDENT print ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 1 , ▁ z ▁ = ▁ " , N - 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ x ▁ = ▁ 1 , ▁ y ▁ = ▁ 2 , ▁ z ▁ = ▁ " , N - 3 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE printThreeParts ( N ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int previousFibonacci ( int n ) { double a = n / ( ( 1 + Math . sqrt ( 5 ) ) / 2.0 ) ; return ( int ) Math . round ( a ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( previousFibonacci ( n ) ) ; } }

from math import * NEW_LINE def previousFibonacci ( n ) : NEW_LINE INDENT a = n / ( ( 1 + sqrt ( 5 ) ) / 2.0 ) NEW_LINE return round ( a ) NEW_LINE DEDENT n = 8 NEW_LINE print ( previousFibonacci ( n ) ) NEW_LINE


import java . io . * ; class GFG { static void printCollatz ( int n ) { while ( n != 1 ) { System . out . print ( n + " ▁ " ) ; if ( ( n & 1 ) == 1 ) n = 3 * n + 1 ; else n = n / 2 ; } System . out . print ( n ) ; } public static void main ( String [ ] args ) { printCollatz ( 6 ) ; } }

def printCollatz ( n ) : NEW_LINE INDENT while n != 1 : NEW_LINE INDENT print ( n , end = ' ▁ ' ) NEW_LINE if n & 1 : NEW_LINE INDENT n = 3 * n + 1 NEW_LINE DEDENT else : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT DEDENT print ( n ) NEW_LINE DEDENT printCollatz ( 6 ) NEW_LINE


class GFG { static int kthSmallest ( int n , int k ) { return ( 2 * k ) ; } public static void main ( String args [ ] ) { int n = 8 , k = 4 ; System . out . print ( kthSmallest ( n , k ) ) ; } }

def kthSmallest ( n , k ) : NEW_LINE INDENT return 2 * k NEW_LINE DEDENT n = 8 ; k = 4 NEW_LINE print ( kthSmallest ( n , k ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { public static int M = 100 ; static int minAdjustmentCost ( int A [ ] , int n , int target ) { int [ ] [ ] dp = new int [ n ] [ M + 1 ] ; for ( int j = 0 ; j <= M ; j ++ ) dp [ 0 ] [ j ] = Math . abs ( j - A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j <= M ; j ++ ) { dp [ i ] [ j ] = Integer . MAX_VALUE ; int k = Math . max ( j - target , 0 ) ; for ( ; k <= Math . min ( M , j + target ) ; k ++ ) dp [ i ] [ j ] = Math . min ( dp [ i ] [ j ] , dp [ i - 1 ] [ k ] + Math . abs ( A [ i ] - j ) ) ; } } int res = Integer . MAX_VALUE ; for ( int j = 0 ; j <= M ; j ++ ) res = Math . min ( res , dp [ n - 1 ] [ j ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 } ; int n = arr . length ; int target = 10 ; System . out . println ( " Minimum ▁ adjustment ▁ cost ▁ is ▁ " + minAdjustmentCost ( arr , n , target ) ) ; } }

M = 100 NEW_LINE def minAdjustmentCost ( A , n , target ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( M + 1 ) ] for i in range ( n ) ] NEW_LINE for j in range ( M + 1 ) : NEW_LINE INDENT dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( M + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 100000000 NEW_LINE for k in range ( max ( j - target , 0 ) , min ( M , j + target ) + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ i - 1 ] [ k ] + abs ( A [ i ] - j ) ) NEW_LINE DEDENT DEDENT DEDENT res = 10000000 NEW_LINE for j in range ( M + 1 ) : NEW_LINE INDENT res = min ( res , dp [ n - 1 ] [ j ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 55 , 77 , 52 , 61 , 39 , 6 , 25 , 60 , 49 , 47 ] NEW_LINE n = len ( arr ) NEW_LINE target = 10 NEW_LINE print ( " Minimum ▁ adjustment ▁ cost ▁ is " , minAdjustmentCost ( arr , n , target ) , sep = ' ▁ ' ) NEW_LINE


public class MaxLengthBitonic { static int maxLenBitonic ( int [ ] A , int n ) { if ( n == 0 ) return 0 ; int maxLen = 1 ; int start = 0 ; int nextStart = 0 ; int j = 0 ; while ( j < n - 1 ) { while ( j < n - 1 && A [ j ] <= A [ j + 1 ] ) j ++ ; while ( j < n - 1 && A [ j ] >= A [ j + 1 ] ) { if ( j < n - 1 && A [ j ] > A [ j + 1 ] ) nextStart = j + 1 ; j ++ ; } maxLen = Math . max ( maxLen , j - ( start - 1 ) ) ; start = nextStart ; } return maxLen ; } public static void main ( String [ ] args ) { int A [ ] = { 12 , 4 , 78 , 90 , 45 , 23 } ; System . out . println ( " Length ▁ of ▁ maximal ▁ length ▁ bitonic ▁ " + " subarray ▁ is ▁ " + maxLenBitonic ( A , A . length ) ) ; } }

def bitonic ( A , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT maxLen = 1 ; NEW_LINE start = 0 ; NEW_LINE nextStart = 0 ; NEW_LINE j = 0 ; NEW_LINE while ( j < n - 1 ) : NEW_LINE INDENT while ( j < n - 1 and A [ j ] <= A [ j + 1 ] ) : NEW_LINE INDENT j = j + 1 ; NEW_LINE DEDENT while ( j < n - 1 and A [ j ] >= A [ j + 1 ] ) : NEW_LINE INDENT if ( j < n - 1 and A [ j ] > A [ j + 1 ] ) : NEW_LINE INDENT nextStart = j + 1 ; NEW_LINE DEDENT j = j + 1 ; NEW_LINE DEDENT maxLen = max ( maxLen , j - ( start - 1 ) ) ; NEW_LINE start = nextStart ; NEW_LINE DEDENT return maxLen ; NEW_LINE DEDENT A = [ 12 , 4 , 78 , 90 , 45 , 23 ] ; NEW_LINE n = len ( A ) ; NEW_LINE print ( " Length ▁ of ▁ max ▁ length ▁ Bitonic ▁ Subarray ▁ is " , bitonic ( A , n ) ) ; NEW_LINE


import java . util . * ; class GFG { static int TOTAL_BITS = 32 ; static boolean isPossible ( int n ) { int cnt = Integer . bitCount ( n ) ; if ( cnt == TOTAL_BITS / 2 ) return true ; return false ; } static public void main ( String [ ] arr ) { int n = 524280 ; if ( isPossible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

TOTAL_BITS = 32 ; NEW_LINE def isPossible ( n ) : NEW_LINE INDENT cnt = bin ( n ) . count ( '1' ) ; NEW_LINE if ( cnt == TOTAL_BITS // 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 524280 ; NEW_LINE if ( isPossible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; import java . util . * ; public class MAX_LEN_0_1 { public static void main ( String args [ ] ) throws IOException { String str = "101001000" ; HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; map . put ( 0 , - 1 ) ; int res = 0 ; int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == '0' ) count_0 ++ ; else count_1 ++ ; if ( map . containsKey ( count_1 - count_0 ) ) res = Math . max ( res , ( i - map . get ( count_1 - count_0 ) ) ) ; else map . put ( count_1 - count_0 , i ) ; } System . out . println ( " Length ▁ of ▁ longest ▁ balanced ▁ sub ▁ string ▁ = ▁ " + res ) ; } }

def stringLen ( str ) : NEW_LINE INDENT m = dict ( ) NEW_LINE m [ 0 ] = - 1 NEW_LINE count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE res = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] == '0' : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT if m . get ( count_1 - count_0 ) : NEW_LINE INDENT res = max ( res , i - m [ count_1 - count_0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT m [ count_1 - count_0 ] = i NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT str = "101001000" NEW_LINE print ( " Length ▁ of ▁ longest ▁ balanced " " ▁ sub ▁ string ▁ = ▁ " , stringLen ( str ) ) NEW_LINE


import java . util . * ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { int subSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { subSum += arr [ j ] ; } arr [ i ] -= subSum ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 40 , 25 , 12 , 10 } ; int n = arr . length ; System . out . println ( sumModArr ( arr , n ) ) ; } }

def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumModArr ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT subSum = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT subSum += arr [ j ] NEW_LINE DEDENT arr [ i ] -= subSum NEW_LINE DEDENT return sumArr ( arr , n ) NEW_LINE DEDENT arr = [ 40 , 25 , 12 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumModArr ( arr , n ) ) NEW_LINE


class GFG { public static boolean isCubeFree ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i * i <= n ; i ++ ) if ( n % ( i * i * i ) == 0 ) return false ; return true ; } public static void printCubeFree ( int n ) { for ( int i = 2 ; i <= n ; i ++ ) { if ( isCubeFree ( i ) ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int n = 20 ; printCubeFree ( n ) ; } }

import math NEW_LINE def isCubeFree ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( n ** ( 1 / 3 ) + 1 ) ) : NEW_LINE INDENT if ( n % ( i * i * i ) == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def printCubeFree ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( isCubeFree ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 20 NEW_LINE printCubeFree ( n ) NEW_LINE


import java . util . * ; class GFG { public static int minOperation ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; else hash . put ( arr [ i ] , 1 ) ; int max_count = 0 ; Set < Integer > s = hash . keySet ( ) ; for ( int i : s ) if ( max_count < hash . get ( i ) ) max_count = hash . get ( i ) ; return ( n - max_count ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . print ( minOperation ( arr , n ) ) ; } }

from collections import defaultdict NEW_LINE def minOperation ( arr , n ) : NEW_LINE INDENT Hash = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT max_count = 0 NEW_LINE for i in Hash : NEW_LINE INDENT if max_count < Hash [ i ] : NEW_LINE INDENT max_count = Hash [ i ] NEW_LINE DEDENT DEDENT return n - max_count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minOperation ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void calcSum ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i <= n - k ; i ++ ) { int sum = 0 ; for ( int j = i ; j < k + i ; j ++ ) sum += arr [ j ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; int k = 3 ; calcSum ( arr , n , k ) ; } }

def calcSum ( arr , n , k ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , k + i ) : NEW_LINE INDENT sum += arr [ j ] NEW_LINE DEDENT print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE calcSum ( arr , n , k ) NEW_LINE


class GFG { static int binomialCoeff ( int n , int k ) { int [ ] C = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int count_of_subarrays ( int N ) { int count = binomialCoeff ( 2 * N - 1 , N ) ; return count ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . print ( count_of_subarrays ( N ) + " \n " ) ; } }

def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ 0 ] * ( k + 1 ) ; NEW_LINE C [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] ; NEW_LINE DEDENT DEDENT return C [ k ] ; NEW_LINE DEDENT def count_of_subarrays ( N ) : NEW_LINE INDENT count = binomialCoeff ( 2 * N - 1 , N ) ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 ; NEW_LINE print ( count_of_subarrays ( N ) ) ; NEW_LINE DEDENT


import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; class Gfg { static boolean checkStackPermutation ( int ip [ ] , int op [ ] , int n ) { Queue < Integer > input = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { input . add ( ip [ i ] ) ; } Queue < Integer > output = new LinkedList < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { output . add ( op [ i ] ) ; } Stack < Integer > tempStack = new Stack < > ( ) ; while ( ! input . isEmpty ( ) ) { int ele = input . poll ( ) ; if ( ele == output . peek ( ) ) { output . poll ( ) ; while ( ! tempStack . isEmpty ( ) ) { if ( tempStack . peek ( ) == output . peek ( ) ) { tempStack . pop ( ) ; output . poll ( ) ; } else break ; } } else { tempStack . push ( ele ) ; } } return ( input . isEmpty ( ) && tempStack . isEmpty ( ) ) ; } public static void main ( String [ ] args ) { int input [ ] = { 1 , 2 , 3 } ; int output [ ] = { 2 , 1 , 3 } ; int n = 3 ; if ( checkStackPermutation ( input , output , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " Not ▁ Possible " ) ; } }

from queue import Queue NEW_LINE def checkStackPermutation ( ip , op , n ) : NEW_LINE INDENT Input = Queue ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT Input . put ( ip [ i ] ) NEW_LINE DEDENT output = Queue ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT output . put ( op [ i ] ) NEW_LINE DEDENT tempStack = [ ] NEW_LINE while ( not Input . empty ( ) ) : NEW_LINE INDENT ele = Input . queue [ 0 ] NEW_LINE Input . get ( ) NEW_LINE if ( ele == output . queue [ 0 ] ) : NEW_LINE INDENT output . get ( ) NEW_LINE while ( len ( tempStack ) != 0 ) : NEW_LINE INDENT if ( tempStack [ - 1 ] == output . queue [ 0 ] ) : NEW_LINE INDENT tempStack . pop ( ) NEW_LINE output . get ( ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT tempStack . append ( ele ) NEW_LINE DEDENT DEDENT return ( Input . empty ( ) and len ( tempStack ) == 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Input = [ 1 , 2 , 3 ] NEW_LINE output = [ 2 , 1 , 3 ] NEW_LINE n = 3 NEW_LINE if ( checkStackPermutation ( Input , output , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public static int first ( int a , int b , int c ) { a %= b ; for ( int i = 1 ; i <= b ; i ++ ) { a = a * 10 ; if ( a / b == c ) return i ; a %= b ; } return - 1 ; } public static void main ( String argc [ ] ) { int a = 1 , b = 4 , c = 5 ; System . out . println ( first ( a , b , c ) ) ; } }

def first ( a , b , c ) : NEW_LINE INDENT a %= b NEW_LINE for i in range ( 1 , b + 1 ) : NEW_LINE INDENT a = a * 10 NEW_LINE if int ( a / b ) == c : NEW_LINE INDENT return i NEW_LINE DEDENT a %= b NEW_LINE DEDENT return - 1 NEW_LINE DEDENT a = 1 NEW_LINE b = 4 NEW_LINE c = 5 NEW_LINE print ( first ( a , b , c ) ) NEW_LINE


class GFG { static int countFreq ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; int res = 0 ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) { if ( txt . charAt ( i + j ) != pat . charAt ( j ) ) { break ; } } if ( j == M ) { res ++ ; j = 0 ; } } return res ; } static public void main ( String [ ] args ) { String txt = " dhimanman " ; String pat = " man " ; System . out . println ( countFreq ( pat , txt ) ) ; } }

def countFreq ( pat , txt ) : NEW_LINE INDENT M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE res = 0 NEW_LINE for i in range ( N - M + 1 ) : NEW_LINE INDENT j = 0 NEW_LINE for j in range ( M ) : NEW_LINE INDENT if ( txt [ i + j ] != pat [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == M - 1 ) : NEW_LINE INDENT res += 1 NEW_LINE j = 0 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT txt = " dhimanman " NEW_LINE pat = " man " NEW_LINE print ( countFreq ( pat , txt ) ) NEW_LINE DEDENT


class GFG { static class Pair { int first , second ; public Pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void lineFromPoints ( Pair P , Pair Q ) { int a = Q . second - P . second ; int b = P . first - Q . first ; int c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { System . out . println ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " + a + " x ▁ " + b + " y ▁ = ▁ " + c ) ; } else { System . out . println ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " + a + " x ▁ + ▁ " + b + " y ▁ = ▁ " + c ) ; } } public static void main ( String [ ] args ) { Pair P = new Pair ( 3 , 2 ) ; Pair Q = new Pair ( 2 , 6 ) ; lineFromPoints ( P , Q ) ; } }

def lineFromPoints ( P , Q ) : NEW_LINE INDENT a = Q [ 1 ] - P [ 1 ] NEW_LINE b = P [ 0 ] - Q [ 0 ] NEW_LINE c = a * ( P [ 0 ] ) + b * ( P [ 1 ] ) NEW_LINE if ( b < 0 ) : NEW_LINE INDENT print ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : " , a , " x ▁ " , b , " y ▁ = ▁ " , c , " \n " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ line ▁ passing ▁ through ▁ points ▁ P ▁ and ▁ Q ▁ is : ▁ " , a , " x ▁ + ▁ " , b , " y ▁ = ▁ " , c , " \n " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = [ 3 , 2 ] NEW_LINE Q = [ 2 , 6 ] NEW_LINE lineFromPoints ( P , Q ) NEW_LINE DEDENT


class GFG { public static int countDigits ( int num ) { int cnt = 0 ; while ( num > 0 ) { cnt ++ ; num /= 10 ; } return cnt ; } public static int sumFromStart ( int num , int n , int rem ) { num /= ( ( int ) Math . pow ( 10 , rem ) ) ; int sum = 0 ; while ( num > 0 ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static int sumFromEnd ( int num , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ( num % 10 ) ; num /= 10 ; } return sum ; } public static float getAverage ( int n , int k , int l ) { int totalDigits = countDigits ( n ) ; if ( totalDigits < ( k + l ) ) return - 1 ; int sum1 = sumFromEnd ( n , l ) ; int sum2 = sumFromStart ( n , k , totalDigits - k ) ; return ( ( float ) ( sum1 + sum2 ) / ( float ) ( k + l ) ) ; } public static void main ( String args [ ] ) { int n = 123456 , k = 2 , l = 3 ; System . out . print ( getAverage ( n , k , l ) ) ; } }

from math import pow NEW_LINE def countDigits ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE num //= 10 NEW_LINE DEDENT return cnt NEW_LINE DEDENT def sumFromStart ( num , n , rem ) : NEW_LINE INDENT num //= pow ( 10 , rem ) NEW_LINE sum = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT sum += ( num % 10 ) NEW_LINE num //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumFromEnd ( num , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ( num % 10 ) NEW_LINE num //= 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def getAverage ( n , k , l ) : NEW_LINE INDENT totalDigits = countDigits ( n ) NEW_LINE if ( totalDigits < ( k + l ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT sum1 = sumFromEnd ( n , l ) NEW_LINE sum2 = sumFromStart ( n , k , totalDigits - k ) NEW_LINE return ( sum1 + sum2 ) / ( k + l ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 123456 NEW_LINE k = 2 NEW_LINE l = 3 NEW_LINE print ( getAverage ( n , k , l ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int sumDivisibles ( int A , int B , int M ) { int sum = 0 ; for ( int i = A ; i <= B ; i ++ ) if ( i % M == 0 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; System . out . print ( sumDivisibles ( A , B , M ) + " \n " ) ; } }

def sumDivisibles ( A , B , M ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( A , B + 1 ) : NEW_LINE INDENT if ( i % M == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 6 NEW_LINE B = 15 NEW_LINE M = 3 NEW_LINE print ( sumDivisibles ( A , B , M ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int surfaceArea ( int b , int s ) { return 2 * b * s + ( int ) Math . pow ( b , 2 ) ; } public static void main ( String [ ] args ) { int b = 3 , s = 4 ; System . out . println ( surfaceArea ( b , s ) ) ; } }

def surfaceArea ( b , s ) : NEW_LINE INDENT return 2 * b * s + pow ( b , 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT b = 3 NEW_LINE s = 4 NEW_LINE print ( surfaceArea ( b , s ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void generateArr ( int [ ] arr , int n ) { if ( n <= 1 ) return ; int prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] & arr [ 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int curr = arr [ i ] ; arr [ i ] = prev & arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev & arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; generateArr ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def generateArr ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( arr [ 0 ] ) ; NEW_LINE return ; NEW_LINE DEDENT barr = [ 0 ] * n ; NEW_LINE barr [ 0 ] = arr [ 0 ] & arr [ 1 ] ; NEW_LINE barr [ n - 1 ] = arr [ n - 1 ] & arr [ n - 2 ] ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT barr [ i ] = arr [ i - 1 ] & arr [ i + 1 ] ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( barr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE generateArr ( arr , n ) ; NEW_LINE DEDENT


class GFG { static int MaximumDivisors ( int X , int Y ) { int [ ] arr = new int [ Y - X + 1 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) arr [ i ] = 0 ; int mx = 0 ; int cnt = 0 ; for ( int i = 1 ; i * i <= Y ; i ++ ) { int sq = i * i ; int first_divisible ; if ( ( X / i ) * i >= X ) first_divisible = ( X / i ) * i ; else first_divisible = ( X / i + 1 ) * i ; for ( int j = first_divisible ; j <= Y ; j += i ) { if ( j < sq ) continue ; else if ( j == sq ) arr [ j - X ] ++ ; else arr [ j - X ] += 2 ; } } for ( int i = X ; i <= Y ; i ++ ) { if ( arr [ i - X ] > mx ) { cnt = 1 ; mx = arr [ i - X ] ; } else if ( arr [ i - X ] == mx ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int X = 1 , Y = 10 ; System . out . println ( MaximumDivisors ( X , Y ) ) ; } }

def MaximumDivisors ( X , Y ) : NEW_LINE INDENT arr = [ 0 ] * ( Y - X + 1 ) NEW_LINE mx = 0 NEW_LINE cnt = 0 NEW_LINE i = 1 NEW_LINE while i * i <= Y : NEW_LINE INDENT sq = i * i NEW_LINE if ( ( X // i ) * i >= X ) : NEW_LINE INDENT first_divisible = ( X // i ) * i NEW_LINE DEDENT else : NEW_LINE INDENT first_divisible = ( X // i + 1 ) * i NEW_LINE DEDENT for j in range ( first_divisible , Y + 1 , i ) : NEW_LINE INDENT if j < sq : NEW_LINE INDENT continue NEW_LINE DEDENT elif j == sq : NEW_LINE INDENT arr [ j - X ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ j - X ] += 2 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for i in range ( X , Y + 1 ) : NEW_LINE INDENT if arr [ i - X ] > mx : NEW_LINE INDENT cnt = 1 NEW_LINE mx = arr [ i - X ] NEW_LINE DEDENT elif arr [ i - X ] == mx : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 1 NEW_LINE Y = 10 NEW_LINE print ( MaximumDivisors ( X , Y ) ) NEW_LINE DEDENT


class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int sameRemainder ( int a , int b , int c ) { int a1 = ( b - a ) , b1 = ( c - b ) , c1 = ( c - a ) ; return gcd ( a1 , gcd ( b1 , c1 ) ) ; } public static void main ( String [ ] args ) { int a = 62 , b = 132 , c = 237 ; System . out . println ( sameRemainder ( a , b , c ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def sameRemainder ( a , b , c ) : NEW_LINE INDENT a1 = ( b - a ) NEW_LINE b1 = ( c - b ) NEW_LINE c1 = ( c - a ) NEW_LINE return gcd ( a1 , gcd ( b1 , c1 ) ) NEW_LINE DEDENT a = 62 NEW_LINE b = 132 NEW_LINE c = 237 NEW_LINE print ( sameRemainder ( a , b , c ) ) NEW_LINE


import java . io . * ; class GFG { static int findStarNum ( int n ) { return ( 6 * n * ( n - 1 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( findStarNum ( n ) ) ; } }

def findStarNum ( n ) : NEW_LINE INDENT return ( 6 * n * ( n - 1 ) + 1 ) NEW_LINE DEDENT n = 3 NEW_LINE print ( findStarNum ( n ) ) NEW_LINE


import java . io . * ; public class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static void countCoprimePairs ( int n ) { int count = 0 ; System . out . println ( " The ▁ co - ▁ prime ▁ pairs ▁ are : ▁ " ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( __gcd ( i , ( n / i ) ) == 1 ) { System . out . print ( " ( " + i + " , ▁ " + ( n / i ) + " ) \n " ) ; count ++ ; } } } System . out . println ( " \n Number ▁ of ▁ coprime ▁ pairs ▁ : ▁ " + count ) ; } public static void main ( String [ ] args ) { int N = 120 ; countCoprimePairs ( N ) ; } }

from math import * NEW_LINE def countCoprimePairs ( n ) : NEW_LINE INDENT count = 0 NEW_LINE print ( " The ▁ co - prime ▁ pairs ▁ are : ▁ " ) NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT if gcd ( i , n // i ) == 1 : NEW_LINE INDENT print ( " ( " , i , " , " , ( n // i ) , " ) " ) NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT print ( " Number ▁ of ▁ coprime ▁ pairs ▁ : ▁ " , count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 120 NEW_LINE countCoprimePairs ( N ) NEW_LINE DEDENT


class GFG { static void hollowSquare ( int rows ) { int i , j ; for ( i = 1 ; i <= rows ; i ++ ) { if ( i == 1 || i == rows ) for ( j = 1 ; j <= rows ; j ++ ) System . out . print ( " * " ) ; else for ( j = 1 ; j <= rows ; j ++ ) if ( j == 1 || j == rows ) System . out . print ( " * " ) ; else System . out . print ( " ▁ " ) ; System . out . print ( " \n " ) ; } } static void solidSquare ( int rows ) { int i , j ; for ( i = 1 ; i <= rows ; i ++ ) { for ( j = 1 ; j <= rows ; j ++ ) System . out . print ( " * " ) ; System . out . print ( " \n " ) ; } } static void printPattern ( int rows ) { System . out . print ( " \n Solid ▁ Square : \n " ) ; solidSquare ( rows ) ; System . out . print ( " \n Hollow ▁ Square : \n " ) ; hollowSquare ( rows ) ; } public static void main ( String [ ] args ) { int rows = 5 ; printPattern ( rows ) ; } }

def hollowSquare ( rows ) : NEW_LINE INDENT for i in range ( 1 , rows + 1 ) : NEW_LINE INDENT if ( i == 1 or i == rows ) : NEW_LINE INDENT for j in range ( 1 , rows + 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for j in range ( 1 , rows + 1 ) : NEW_LINE INDENT if ( j == 1 or j == rows ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT def solidSquare ( rows ) : NEW_LINE INDENT for i in range ( 1 , rows ) : NEW_LINE INDENT for j in range ( 1 , rows + 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def printPattern ( rows ) : NEW_LINE INDENT print ( " Solid ▁ Square : " ) NEW_LINE solidSquare ( rows ) NEW_LINE print ( " \n Hollow ▁ Square : " ) NEW_LINE hollowSquare ( rows ) NEW_LINE DEDENT rows = 5 NEW_LINE printPattern ( rows ) NEW_LINE


import java . util . * ; public class GfG { public static int doMatch ( String A , String B ) { for ( int i = 0 ; i < A . length ( ) ; i ++ ) { if ( A . charAt ( i ) != ' * ' && B . charAt ( i ) != ' * ' ) { if ( A . charAt ( i ) != B . charAt ( i ) ) return 0 ; } } return 1 ; } public static void main ( String [ ] args ) { String A = " gee * sforgeeks " ; String B = " geeksforgeeks " ; System . out . println ( doMatch ( A , B ) ) ; } }

def doMatch ( A , B ) : NEW_LINE INDENT for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] != ' * ' and B [ i ] != ' * ' : NEW_LINE INDENT if A [ i ] != B [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " gee * sforgeeks " NEW_LINE B = " geeksforgeeks " NEW_LINE print ( int ( doMatch ( A , B ) ) ) NEW_LINE DEDENT


class GFG { static int larrgestPalindrome ( int n ) { int upper_limit = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { upper_limit *= 10 ; upper_limit += 9 ; } int lower_limit = 1 + upper_limit / 10 ; int max_product = 0 ; for ( int i = upper_limit ; i >= lower_limit ; i -- ) { for ( int j = i ; j >= lower_limit ; j -- ) { int product = i * j ; if ( product < max_product ) break ; int number = product ; int reverse = 0 ; while ( number != 0 ) { reverse = reverse * 10 + number % 10 ; number /= 10 ; } if ( product == reverse && product > max_product ) max_product = product ; } } return max_product ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . print ( larrgestPalindrome ( n ) ) ; } }

def larrgestPalindrome ( n ) : NEW_LINE INDENT upper_limit = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT upper_limit = upper_limit * 10 NEW_LINE upper_limit = upper_limit + 9 NEW_LINE DEDENT lower_limit = 1 + upper_limit // 10 NEW_LINE max_product = 0 NEW_LINE for i in range ( upper_limit , lower_limit - 1 , - 1 ) : NEW_LINE INDENT for j in range ( i , lower_limit - 1 , - 1 ) : NEW_LINE INDENT product = i * j NEW_LINE if ( product < max_product ) : NEW_LINE INDENT break NEW_LINE DEDENT number = product NEW_LINE reverse = 0 NEW_LINE while ( number != 0 ) : NEW_LINE INDENT reverse = reverse * 10 + number % 10 NEW_LINE number = number // 10 NEW_LINE DEDENT if ( product == reverse and product > max_product ) : NEW_LINE INDENT max_product = product NEW_LINE DEDENT DEDENT DEDENT return max_product NEW_LINE DEDENT n = 2 NEW_LINE print ( larrgestPalindrome ( n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GFG { public static int findNth ( int n ) { int count = 0 ; for ( int curr = 19 ; ; curr += 9 ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } } public static void main ( String [ ] args ) { System . out . print ( findNth ( 5 ) ) ; } }

def findNth ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE curr = 19 ; NEW_LINE while ( True ) : NEW_LINE INDENT sum = 0 ; NEW_LINE x = curr ; NEW_LINE while ( x > 0 ) : NEW_LINE INDENT sum = sum + x % 10 ; NEW_LINE x = int ( x / 10 ) ; NEW_LINE DEDENT if ( sum == 10 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return curr ; NEW_LINE DEDENT curr += 9 ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT print ( findNth ( 5 ) ) ; NEW_LINE


class GFG { static int minCost ( int [ ] [ ] graph , int n ) { int cost1 = 0 , cost2 = 0 ; boolean [ ] start = new boolean [ n + 1 ] ; boolean [ ] end = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { int a = graph [ i ] [ 0 ] ; int b = graph [ i ] [ 1 ] ; int c = graph [ i ] [ 2 ] ; if ( start [ a ] || end [ b ] ) { cost2 += c ; start [ b ] = true ; end [ a ] = true ; } else { cost1 += c ; start [ a ] = true ; end [ b ] = true ; } } return Math . min ( cost1 , cost2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] [ ] graph = { { 1 , 2 , 7 } , { 5 , 1 , 8 } , { 5 , 4 , 5 } , { 3 , 4 , 1 } , { 3 , 2 , 10 } } ; int ans = minCost ( graph , n ) ; System . out . println ( ans ) ; } }

def minCost ( graph , n ) : NEW_LINE INDENT cost1 , cost2 = 0 , 0 ; NEW_LINE start = [ False ] * ( n + 1 ) ; NEW_LINE end = [ False ] * ( n + 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT a = graph [ i ] [ 0 ] ; NEW_LINE b = graph [ i ] [ 1 ] ; NEW_LINE c = graph [ i ] [ 2 ] ; NEW_LINE if ( start [ a ] or end [ b ] ) : NEW_LINE INDENT cost2 += c ; NEW_LINE start [ b ] = True ; NEW_LINE end [ a ] = True ; NEW_LINE DEDENT else : NEW_LINE INDENT cost1 += c ; NEW_LINE start [ a ] = True ; NEW_LINE end [ b ] = True ; NEW_LINE DEDENT DEDENT return min ( cost1 , cost2 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE graph = [ [ 1 , 2 , 7 ] , [ 5 , 1 , 8 ] , [ 5 , 4 , 5 ] , [ 3 , 4 , 1 ] , [ 3 , 2 , 10 ] ] ; NEW_LINE ans = minCost ( graph , n ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int exponentMod ( int A , int B , int C ) { if ( A == 0 ) return 0 ; if ( B == 0 ) return 1 ; long y ; if ( B % 2 == 0 ) { y = exponentMod ( A , B / 2 , C ) ; y = ( y * y ) % C ; } else { y = A % C ; y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C ; } return ( int ) ( ( y + C ) % C ) ; } public static void main ( String args [ ] ) { int A = 2 , B = 5 , C = 13 ; System . out . println ( " Power ▁ is ▁ " + exponentMod ( A , B , C ) ) ; } }

def exponentMod ( A , B , C ) : NEW_LINE INDENT if ( A == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( B == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT y = 0 NEW_LINE if ( B % 2 == 0 ) : NEW_LINE INDENT y = exponentMod ( A , B / 2 , C ) NEW_LINE y = ( y * y ) % C NEW_LINE DEDENT else : NEW_LINE INDENT y = A % C NEW_LINE y = ( y * exponentMod ( A , B - 1 , C ) % C ) % C NEW_LINE DEDENT return ( ( y + C ) % C ) NEW_LINE DEDENT A = 2 NEW_LINE B = 5 NEW_LINE C = 13 NEW_LINE print ( " Power ▁ is " , exponentMod ( A , B , C ) ) NEW_LINE


class GFG { static void swap ( int xp , int yp ) { if ( xp == yp ) return ; xp = xp + yp ; yp = xp - yp ; xp = xp - yp ; } public static void main ( String [ ] args ) { int x = 10 ; swap ( x , x ) ; System . out . println ( " After ▁ swap ( & x , ▁ & x ) : ▁ x ▁ = ▁ " + x ) ; } }

def swap ( xp , yp ) : NEW_LINE INDENT if ( xp [ 0 ] == yp [ 0 ] ) : NEW_LINE INDENT return NEW_LINE DEDENT xp [ 0 ] = xp [ 0 ] + yp [ 0 ] NEW_LINE yp [ 0 ] = xp [ 0 ] - yp [ 0 ] NEW_LINE xp [ 0 ] = xp [ 0 ] - yp [ 0 ] NEW_LINE DEDENT x = [ 10 ] NEW_LINE swap ( x , x ) NEW_LINE print ( " After ▁ swap ( & x , ▁ & x ) : ▁ x ▁ = ▁ " , x [ 0 ] ) NEW_LINE


import java . util . * ; class GFG { static int R = 3 , C = 3 ; static boolean safe ( int x , int y ) { if ( x < R && y < C && x >= 0 && y >= 0 ) return true ; return false ; } static class pair < T , R > { T first ; R second ; pair ( T t , R r ) { first = t ; second = r ; } } static int matrixJump ( int M [ ] [ ] , int R1 , int C1 ) { Queue < pair < Integer , pair < Integer , Integer > > > q = new LinkedList < > ( ) ; q . add ( new pair ( 1 , new pair ( R1 , C1 ) ) ) ; while ( q . size ( ) > 0 ) { int x = q . peek ( ) . second . first ; int y = q . peek ( ) . second . second ; int no_of_cells = q . peek ( ) . first ; q . remove ( ) ; if ( x == R - 1 && y == C - 1 ) return no_of_cells ; int v = M [ x ] [ y ] ; if ( safe ( x + v , y ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x + v , y ) ) ) ; if ( safe ( x , y + v ) ) q . add ( new pair ( no_of_cells + 1 , new pair ( x , y + v ) ) ) ; } return - 1 ; } public static void main ( String ars [ ] ) { int M [ ] [ ] = { { 2 , 4 , 2 } , { 5 , 3 , 8 } , { 1 , 1 , 1 } } ; System . out . print ( matrixJump ( M , 0 , 0 ) ) ; } }

R , C = 3 , 3 NEW_LINE def safe ( x , y ) : NEW_LINE INDENT if x < R and y < C and x >= 0 and y >= 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def matrixJump ( M , R1 , C1 ) : NEW_LINE INDENT q = [ ] NEW_LINE q . append ( ( 1 , ( R1 , C1 ) ) ) NEW_LINE while len ( q ) != 0 : NEW_LINE INDENT x = q [ 0 ] [ 1 ] [ 0 ] NEW_LINE y = q [ 0 ] [ 1 ] [ 1 ] NEW_LINE no_of_cells = q [ 0 ] [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if x == R - 1 and y == C - 1 : NEW_LINE INDENT return no_of_cells NEW_LINE DEDENT v = M [ x ] [ y ] NEW_LINE if safe ( x + v , y ) : NEW_LINE INDENT q . append ( ( no_of_cells + 1 , ( x + v , y ) ) ) NEW_LINE DEDENT if safe ( x , y + v ) : NEW_LINE INDENT q . append ( ( no_of_cells + 1 , ( x , y + v ) ) ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT M = [ [ 2 , 4 , 2 ] , [ 5 , 3 , 8 ] , [ 1 , 1 , 1 ] ] NEW_LINE print ( matrixJump ( M , 0 , 0 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static double getProbability ( double p , double q ) { p /= 100 ; q /= 100 ; double probability = ( p * p + q * q ) / ( p + q ) ; return probability ; } public static void main ( String [ ] args ) { double p , q ; p = 80 ; q = 40 ; System . out . println ( getProbability ( p , q ) ) ; } }

def getProbability ( p , q ) : NEW_LINE INDENT p /= 100 NEW_LINE q /= 100 NEW_LINE probability = ( p * p + q * q ) / ( p + q ) NEW_LINE return probability NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = 80 NEW_LINE q = 40 NEW_LINE print ( getProbability ( p , q ) ) NEW_LINE DEDENT


class GFG { static int product ( int x ) { int prod = 1 ; while ( x > 0 ) { prod *= ( x % 10 ) ; x /= 10 ; } return prod ; } static int findNumber ( int l , int r ) { String b = Integer . toString ( r ) ; int ans = r ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { if ( b . charAt ( i ) == '0' ) continue ; char [ ] curr = b . toCharArray ( ) ; curr [ i ] = ( char ) ( ( ( int ) ( curr [ i ] - ( int ) '0' ) - 1 ) + ( int ) ( '0' ) ) ; for ( int j = i + 1 ; j < curr . length ; j ++ ) curr [ j ] = '9' ; int num = 0 ; for ( int j = 0 ; j < curr . length ; j ++ ) num = num * 10 + ( curr [ j ] - '0' ) ; if ( num >= l && product ( ans ) < product ( num ) ) ans = num ; } return ans ; } public static void main ( String [ ] args ) { int l = 1 , r = 10 ; System . out . println ( findNumber ( l , r ) ) ; l = 51 ; r = 62 ; System . out . println ( findNumber ( l , r ) ) ; } }

def product ( x ) : NEW_LINE INDENT prod = 1 NEW_LINE while ( x ) : NEW_LINE INDENT prod *= ( x % 10 ) NEW_LINE x //= 10 ; NEW_LINE DEDENT return prod NEW_LINE DEDENT def findNumber ( l , r ) : NEW_LINE INDENT a = str ( l ) ; NEW_LINE b = str ( r ) ; NEW_LINE ans = r NEW_LINE for i in range ( len ( b ) ) : NEW_LINE INDENT if ( b [ i ] == '0' ) : NEW_LINE INDENT continue NEW_LINE DEDENT curr = list ( b ) NEW_LINE curr [ i ] = str ( ( ( ord ( curr [ i ] ) - ord ( '0' ) ) - 1 ) + ord ( '0' ) ) NEW_LINE for j in range ( i + 1 , len ( curr ) ) : NEW_LINE INDENT curr [ j ] = str ( ord ( '9' ) ) NEW_LINE DEDENT num = 0 NEW_LINE for c in curr : NEW_LINE INDENT num = num * 10 + ( int ( c ) - ord ( '0' ) ) NEW_LINE DEDENT if ( num >= l and product ( ans ) < product ( num ) ) : NEW_LINE INDENT ans = num NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r = 1 , 10 NEW_LINE print ( findNumber ( l , r ) ) NEW_LINE l , r = 51 , 62 NEW_LINE print ( findNumber ( l , r ) ) NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int sumAP ( int n , int d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static int sumMultiples ( int A , int B , int n ) { n -- ; int common = ( A * B ) / __gcd ( A , B ) ; return sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ; } public static void main ( String [ ] args ) { int n = 100 , A = 5 , B = 10 ; System . out . println ( " Sum ▁ = ▁ " + sumMultiples ( A , B , n ) ) ; } }

from math import gcd , sqrt NEW_LINE def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) NEW_LINE return ( n ) * ( 1 + n ) * d / 2 NEW_LINE DEDENT def sumMultiples ( A , B , n ) : NEW_LINE INDENT n -= 1 NEW_LINE common = int ( ( A * B ) / gcd ( A , B ) ) NEW_LINE return ( sumAP ( n , A ) + sumAP ( n , B ) - sumAP ( n , common ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE A = 5 NEW_LINE B = 10 NEW_LINE print ( " Sum ▁ = " , int ( sumMultiples ( A , B , n ) ) ) NEW_LINE DEDENT


class GFG { public static int countunsetbits ( int n ) { int count = 0 ; for ( int x = 1 ; x <= n ; x = x << 1 ) if ( ( x & n ) == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( countunsetbits ( n ) ) ; } }

def countunsetbits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE x = 1 NEW_LINE while ( x < n + 1 ) : NEW_LINE INDENT if ( ( x & n ) == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT x = x << 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 17 NEW_LINE print ( countunsetbits ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static int gcdFibonacciFreq ( int arr [ ] , int n ) { HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ) ; int i ; HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , 1 ) ; } } int gcd = 0 ; for ( Map . Entry < Integer , Integer > it : m . entrySet ( ) ) { if ( hash . contains ( it . getValue ( ) ) ) { gcd = __gcd ( gcd , it . getKey ( ) ) ; } } return gcd ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 6 , 5 , 6 , 6 , 5 , 5 } ; int n = arr . length ; System . out . print ( gcdFibonacciFreq ( arr , n ) ) ; } }

from collections import defaultdict NEW_LINE import math NEW_LINE def createHash ( hash1 , maxElement ) : NEW_LINE INDENT prev , curr = 0 , 1 NEW_LINE hash1 . add ( prev ) NEW_LINE hash1 . add ( curr ) NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE if temp <= maxElement : NEW_LINE INDENT hash1 . add ( temp ) NEW_LINE DEDENT prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def gcdFibonacciFreq ( arr , n ) : NEW_LINE INDENT hash1 = set ( ) NEW_LINE createHash ( hash1 , max ( arr ) ) NEW_LINE m = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 NEW_LINE DEDENT gcd = 0 NEW_LINE for it in m . keys ( ) : NEW_LINE INDENT if ( m [ it ] in hash1 ) : NEW_LINE INDENT gcd = math . gcd ( gcd , it ) NEW_LINE DEDENT DEDENT return gcd NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 6 , 5 , 6 , 6 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( gcdFibonacciFreq ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void LeadingZeros ( int x , int y ) { if ( ( x ^ y ) <= ( x & y ) ) System . out . print ( " \n Equal " ) ; else if ( ( x & ( ~ y ) ) > y ) System . out . print ( y ) ; else System . out . print ( x ) ; } public static void main ( String [ ] args ) { int x = 10 , y = 16 ; LeadingZeros ( x , y ) ; } }

def LeadingZeros ( x , y ) : NEW_LINE INDENT if ( ( x ^ y ) <= ( x & y ) ) : NEW_LINE INDENT print ( " Equal " ) NEW_LINE DEDENT elif ( ( x & ( ~ y ) ) > y ) : NEW_LINE INDENT print ( y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( x ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 10 NEW_LINE y = 16 NEW_LINE LeadingZeros ( x , y ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countOccurrences ( int x , int d ) { int count = 0 ; while ( x > 0 ) { if ( x % 10 == d ) count ++ ; x = x / 10 ; } return count ; } static int maxOccurring ( int x ) { if ( x < 0 ) x = - x ; int result = 0 ; int max_count = 1 ; for ( int d = 0 ; d <= 9 ; d ++ ) { int count = countOccurrences ( x , d ) ; if ( count >= max_count ) { max_count = count ; result = d ; } } return result ; } public static void main ( String [ ] args ) { int x = 1223355 ; System . out . println ( " Max ▁ occurring ▁ digit ▁ is ▁ " + maxOccurring ( x ) ) ; } }

def countOccurrences ( x , d ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( x ) : NEW_LINE INDENT if ( x % 10 == d ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT x = int ( x / 10 ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT def maxOccurring ( x ) : NEW_LINE INDENT if ( x < 0 ) : NEW_LINE INDENT x = - x ; NEW_LINE DEDENT result = 0 ; NEW_LINE max_count = 1 ; NEW_LINE for d in range ( 10 ) : NEW_LINE INDENT count = countOccurrences ( x , d ) ; NEW_LINE if ( count >= max_count ) : NEW_LINE INDENT max_count = count ; NEW_LINE result = d ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT x = 1223355 ; NEW_LINE print ( " Max ▁ occurring ▁ digit ▁ is " , maxOccurring ( x ) ) ; NEW_LINE


import java . util . * ; class GFG { static int isPowerOf2 ( String s ) { char [ ] str = s . toCharArray ( ) ; int len_str = s . length ( ) ; int num = 0 ; if ( len_str == 1 && str [ len_str - 1 ] == '1' ) return 0 ; while ( len_str != 1 || str [ len_str - 1 ] != '1' ) { if ( ( str [ len_str - 1 ] - '0' ) % 2 == 1 ) return 0 ; int j = 0 ; for ( int i = 0 ; i < len_str ; i ++ ) { num = num * 10 + ( int ) str [ i ] - ( int ) '0' ; if ( num < 2 ) { if ( i != 0 ) str [ j ++ ] = '0' ; continue ; } str [ j ++ ] = ( char ) ( ( int ) ( num / 2 ) + ( int ) '0' ) ; num = ( num ) - ( num / 2 ) * 2 ; } str [ j ] = ' \0' ; len_str = j ; } return 1 ; } public static void main ( String [ ] args ) { String str1 = "124684622466842024680246842024662202000002" ; String str2 = "1" ; String str3 = "128" ; System . out . println ( isPowerOf2 ( str1 ) + " \n " + isPowerOf2 ( str2 ) + " \n " + isPowerOf2 ( str3 ) ) ; } }

def isPowerOf2 ( sttr ) : NEW_LINE INDENT len_str = len ( sttr ) ; NEW_LINE sttr = list ( sttr ) ; NEW_LINE num = 0 ; NEW_LINE if ( len_str == 1 and sttr [ len_str - 1 ] == '1' ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT while ( len_str != 1 or sttr [ len_str - 1 ] != '1' ) : NEW_LINE INDENT if ( ( ord ( sttr [ len_str - 1 ] ) - ord ( '0' ) ) % 2 == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT j = 0 ; NEW_LINE for i in range ( len_str ) : NEW_LINE INDENT num = num * 10 + ( ord ( sttr [ i ] ) - ord ( '0' ) ) ; NEW_LINE if ( num < 2 ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT sttr [ j ] = '0' ; NEW_LINE j += 1 ; NEW_LINE DEDENT continue ; NEW_LINE DEDENT sttr [ j ] = chr ( ( num // 2 ) + ord ( '0' ) ) ; NEW_LINE j += 1 ; NEW_LINE num = ( num ) - ( num // 2 ) * 2 ; NEW_LINE DEDENT len_str = j ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT str1 = "124684622466842024680246842024662202000002" ; NEW_LINE str2 = "1" ; NEW_LINE str3 = "128" ; NEW_LINE print ( " " , isPowerOf2 ( str1 ) , " \n " , isPowerOf2 ( str2 ) , " \n " , isPowerOf2 ( str3 ) ) ; NEW_LINE


import java . io . * ; class GFG { static void solve ( int d1 , int d2 , int d3 ) { int maxx = Math . max ( d1 , Math . max ( d2 , d3 ) ) ; int sum = ( d1 + d2 + d3 ) ; if ( 2 * maxx > sum || sum % 2 == 1 ) { System . out . print ( " - 1" ) ; return ; } int x1 = 0 , y1 = 0 ; int x2 = d1 , y2 = 0 ; int x3 = ( d1 + d2 - d3 ) / 2 ; int y3 = ( d2 + d3 - d1 ) / 2 ; System . out . print ( " ( " + x1 + " , ▁ " + y1 + " ) , ▁ ( " + x2 + " , ▁ " + y2 + " ) ▁ and ▁ ( " + x3 + " , ▁ " + y3 + " ) " ) ; } public static void main ( String [ ] args ) { int d1 = 3 , d2 = 4 , d3 = 5 ; solve ( d1 , d2 , d3 ) ; } }

def solve ( d1 , d2 , d3 ) : NEW_LINE INDENT maxx = max ( d1 , max ( d2 , d3 ) ) NEW_LINE sum = ( d1 + d2 + d3 ) NEW_LINE if ( 2 * maxx > sum or sum % 2 == 1 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT x1 = 0 NEW_LINE y1 = 0 NEW_LINE x2 = d1 NEW_LINE y2 = 0 NEW_LINE x3 = ( d1 + d2 - d3 ) // 2 NEW_LINE y3 = ( d2 + d3 - d1 ) // 2 NEW_LINE print ( " ( " , x1 , " , " , y1 , " ) , ▁ ( " , x2 , " , " , y2 , " ) ▁ and ▁ ( " , x3 , " , " , y3 , " ) " ) NEW_LINE DEDENT d1 = 3 NEW_LINE d2 = 4 NEW_LINE d3 = 5 NEW_LINE solve ( d1 , d2 , d3 ) NEW_LINE


import java . io . * ; class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += i * ( n - i ) ; return 2 * sum ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += i * ( n - i ) NEW_LINE DEDENT return 2 * sum NEW_LINE DEDENT n = 3 NEW_LINE print ( findSum ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int N = 3 ; static void multiply ( int mat [ ] [ ] , int res [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { res [ i ] [ j ] = 0 ; for ( int k = 0 ; k < N ; k ++ ) res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; } } } static boolean InvolutoryMatrix ( int mat [ ] [ ] ) { int res [ ] [ ] = new int [ N ] [ N ] ; multiply ( mat , res ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j && res [ i ] [ j ] != 1 ) return false ; if ( i != j && res [ i ] [ j ] != 0 ) return false ; } } return true ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 0 , 0 } , { 0 , - 1 , 0 } , { 0 , 0 , - 1 } } ; if ( InvolutoryMatrix ( mat ) ) System . out . println ( " Involutory ▁ Matrix " ) ; else System . out . println ( " Not ▁ Involutory ▁ Matrix " ) ; } }

N = 3 ; NEW_LINE def multiply ( mat , res ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT res [ i ] [ j ] = 0 ; NEW_LINE for k in range ( N ) : NEW_LINE INDENT res [ i ] [ j ] += mat [ i ] [ k ] * mat [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT def InvolutoryMatrix ( mat ) : NEW_LINE INDENT res = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] ; NEW_LINE res = multiply ( mat , res ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( i == j and res [ i ] [ j ] != 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( i != j and res [ i ] [ j ] != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT mat = [ [ 1 , 0 , 0 ] , [ 0 , - 1 , 0 ] , [ 0 , 0 , - 1 ] ] ; NEW_LINE if ( InvolutoryMatrix ( mat ) ) : NEW_LINE INDENT print ( " Involutory ▁ Matrix " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Involutory ▁ Matrix " ) ; NEW_LINE DEDENT


class GFG { static void unsort ( int l , int r , int a [ ] , int k ) { if ( k < 1 || l + 1 == r ) return ; k -= 2 ; int mid = ( l + r ) / 2 ; int temp = a [ mid - 1 ] ; a [ mid - 1 ] = a [ mid ] ; a [ mid ] = temp ; unsort ( l , mid , a , k ) ; unsort ( mid , r , a , k ) ; } static void arrayWithKCalls ( int n , int k ) { if ( k % 2 == 0 ) { System . out . print ( " NO ▁ SOLUTION " ) ; return ; } int a [ ] = new int [ n + 1 ] ; a [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) a [ i ] = i + 1 ; k -- ; unsort ( 0 , n , a , k ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 17 ; arrayWithKCalls ( n , k ) ; } }

def unsort ( l , r , a , k ) : NEW_LINE INDENT if ( k < 1 or l + 1 == r ) : NEW_LINE INDENT return NEW_LINE DEDENT k -= 2 NEW_LINE mid = ( l + r ) // 2 NEW_LINE temp = a [ mid - 1 ] NEW_LINE a [ mid - 1 ] = a [ mid ] NEW_LINE a [ mid ] = temp NEW_LINE unsort ( l , mid , a , k ) NEW_LINE unsort ( mid , r , a , k ) NEW_LINE DEDENT def arrayWithKCalls ( n , k ) : NEW_LINE INDENT if ( k % 2 == 0 ) : NEW_LINE INDENT print ( " NO ▁ SOLUTION " ) NEW_LINE return NEW_LINE DEDENT a = [ 0 for i in range ( n + 2 ) ] NEW_LINE a [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = i + 1 NEW_LINE DEDENT k -= 1 NEW_LINE unsort ( 0 , n , a , k ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE k = 17 NEW_LINE arrayWithKCalls ( n , k ) NEW_LINE


import java . util . * ; class GFG { static void printNumbers ( int a [ ] , int n ) { Map < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int num = a [ i ] ; for ( int j = 1 ; j * j <= num ; j ++ ) { if ( num % j == 0 ) { if ( j != 1 ) { if ( mpp . containsKey ( j ) ) { mpp . put ( j , mpp . get ( j ) + 1 ) ; } else { mpp . put ( j , 1 ) ; } } if ( ( num / j ) != j ) { if ( mpp . containsKey ( num / j ) ) { mpp . put ( num / j , mpp . get ( num / j ) + 1 ) ; } else { mpp . put ( num / j , 1 ) ; } } } } } int maxi = 0 ; for ( Map . Entry < Integer , Integer > it : mpp . entrySet ( ) ) { maxi = Math . max ( it . getValue ( ) , maxi ) ; } for ( Map . Entry < Integer , Integer > it : mpp . entrySet ( ) ) { if ( it . getValue ( ) == maxi ) System . out . print ( it . getKey ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 12 , 15 , 27 , 20 , 40 } ; int n = a . length ; printNumbers ( a , n ) ; } }

def printNumbers ( a , n ) : NEW_LINE INDENT mpp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT num = a [ i ] NEW_LINE for j in range ( 1 , num + 1 ) : NEW_LINE INDENT if j * j > num : NEW_LINE INDENT break NEW_LINE DEDENT if ( num % j == 0 ) : NEW_LINE INDENT if ( j != 1 ) : NEW_LINE INDENT mpp [ j ] = mpp . get ( j , 0 ) + 1 NEW_LINE DEDENT if ( ( num // j ) != j ) : NEW_LINE INDENT mpp [ num // j ] = mpp . get ( num // j , 0 ) + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT maxi = 0 NEW_LINE for it in mpp : NEW_LINE INDENT maxi = max ( mpp [ it ] , maxi ) NEW_LINE DEDENT for it in mpp : NEW_LINE INDENT if ( mpp [ it ] == maxi ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT a = [ 12 , 15 , 27 , 20 , 40 ] NEW_LINE n = len ( a ) NEW_LINE printNumbers ( a , n ) NEW_LINE


import java . util . * ; class GFG { static int minimumProduct ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; int product = 1 ; for ( int i = 0 ; i < n ; i += 2 ) { product *= ( arr [ i ] + arr [ i + 1 ] ) ; } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 1 , 7 , 8 } ; int n = arr . length ; System . out . print ( minimumProduct ( arr , n ) + " \n " ) ; } }

def minimumProduct ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE product = 1 NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT product *= ( arr [ i ] + arr [ i + 1 ] ) NEW_LINE DEDENT return product NEW_LINE DEDENT arr = [ 1 , 6 , 3 , 1 , 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumProduct ( arr , n ) ) NEW_LINE


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isDivisible ( int n ) { if ( isPrime ( n + 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { int n = 6 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 and n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 and n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isDivisible ( n ) : NEW_LINE INDENT if ( isPrime ( n + 1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static void pythagoreanTriplet ( int n ) { for ( int i = 1 ; i <= n / 3 ; i ++ ) { for ( int j = i + 1 ; j <= n / 2 ; j ++ ) { int k = n - i - j ; if ( i * i + j * j == k * k ) { System . out . print ( i + " , ▁ " + j + " , ▁ " + k ) ; return ; } } } System . out . print ( " No ▁ Triplet " ) ; } public static void main ( String arg [ ] ) { int n = 12 ; pythagoreanTriplet ( n ) ; } }

def pythagoreanTriplet ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( n / 3 ) + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , int ( n / 2 ) + 1 ) : NEW_LINE INDENT k = n - i - j NEW_LINE if ( i * i + j * j == k * k ) : NEW_LINE INDENT print ( i , " , ▁ " , j , " , ▁ " , k , sep = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " No ▁ Triplet " ) NEW_LINE DEDENT n = 12 NEW_LINE pythagoreanTriplet ( n ) NEW_LINE


import java . util . * ; class GFG { static int N = 100000 ; static boolean prime [ ] = new boolean [ N ] ; static void SieveOfEratosthenes ( ) { Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= N ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < N ; i += p ) prime [ i ] = false ; } } } static int longestPrimeSubarray ( int arr [ ] , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; int primecount = 0 , res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = primecount ; if ( prime [ arr [ i ] ] ) { primecount ++ ; } else primecount = 0 ; } primecount = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right [ i ] = primecount ; if ( prime [ arr [ i ] ] ) { primecount ++ ; } else primecount = 0 ; } for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , left [ i ] + right [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 8 , 5 , 7 , 9 , 5 , 7 } ; SieveOfEratosthenes ( ) ; int n = arr . length ; System . out . println ( " largest ▁ length ▁ of ▁ PrimeSubarray ▁ " + longestPrimeSubarray ( arr , n ) ) ; } }

from math import sqrt NEW_LINE N = 100000 NEW_LINE prime = [ True for i in range ( N + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT k = int ( sqrt ( N ) ) + 1 NEW_LINE for p in range ( 2 , k , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , N + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def longestPrimeSubarray ( arr , n ) : NEW_LINE INDENT left = [ 0 for i in range ( n ) ] NEW_LINE right = [ 0 for i in range ( n ) ] NEW_LINE primecount = 0 NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left [ i ] = primecount NEW_LINE if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT primecount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT primecount = 0 NEW_LINE DEDENT DEDENT primecount = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT right [ i ] = primecount NEW_LINE if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT primecount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT primecount = 0 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT res = max ( res , left [ i ] + right [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 8 , 5 , 7 , 9 , 5 , 7 ] NEW_LINE SieveOfEratosthenes ( ) NEW_LINE n = len ( arr ) NEW_LINE print ( " largest ▁ length ▁ of ▁ PrimeSubarray " , longestPrimeSubarray ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static int findMinimumAdjacentSwaps ( int arr [ ] , int N ) { boolean [ ] visited = new boolean [ N + 1 ] ; int minimumSwaps = 0 ; Arrays . fill ( visited , false ) ; for ( int i = 0 ; i < 2 * N ; i ++ ) { if ( visited [ arr [ i ] ] == false ) { visited [ arr [ i ] ] = true ; int count = 0 ; for ( int j = i + 1 ; j < 2 * N ; j ++ ) { if ( visited [ arr [ j ] ] == false ) count ++ ; else if ( arr [ i ] == arr [ j ] ) minimumSwaps += count ; } } } return minimumSwaps ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 3 , 1 , 2 } ; int N = arr . length ; N /= 2 ; System . out . println ( findMinimumAdjacentSwaps ( arr , N ) ) ; } }

def findMinimumAdjacentSwaps ( arr , N ) : NEW_LINE INDENT visited = [ False ] * ( N + 1 ) NEW_LINE minimumSwaps = 0 NEW_LINE for i in range ( 2 * N ) : NEW_LINE INDENT if ( visited [ arr [ i ] ] == False ) : NEW_LINE INDENT visited [ arr [ i ] ] = True NEW_LINE count = 0 NEW_LINE for j in range ( i + 1 , 2 * N ) : NEW_LINE INDENT if ( visited [ arr [ j ] ] == False ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT elif ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT minimumSwaps += count NEW_LINE DEDENT DEDENT DEDENT DEDENT return minimumSwaps NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 1 , 2 ] NEW_LINE N = len ( arr ) NEW_LINE N //= 2 NEW_LINE print ( findMinimumAdjacentSwaps ( arr , N ) ) NEW_LINE DEDENT


class GFG { static boolean isSpiltPossible ( int n , int a [ ] ) { int sum = 0 , c1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; if ( a [ i ] == 1 ) { c1 ++ ; } } if ( sum % 2 != 0 ) return false ; if ( ( sum / 2 ) % 2 == 0 ) return true ; if ( c1 > 0 ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 3 ; int a [ ] = { 1 , 1 , 2 } ; if ( isSpiltPossible ( n , a ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isSpiltPossible ( n , a ) : NEW_LINE INDENT Sum = 0 NEW_LINE c1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE if ( a [ i ] == 1 ) : NEW_LINE INDENT c1 += 1 NEW_LINE DEDENT DEDENT if ( Sum % 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( ( Sum // 2 ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( c1 > 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 3 NEW_LINE a = [ 1 , 1 , 2 ] NEW_LINE if ( isSpiltPossible ( n , a ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


class GfG { static int sumOfDigits ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static int countTheNumbers ( int l , int r , int y ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( sumOfDigits ( i ) == y ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int l = 0 , r = 10 , y = 1 ; System . out . println ( countTheNumbers ( l , r , y ) ) ; } }

def sumOfDigits ( n ) : NEW_LINE INDENT Sum = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT Sum = Sum + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT return Sum NEW_LINE DEDENT def countTheNumbers ( l , r , y ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( sumOfDigits ( i ) == y ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l , r , y = 0 , 10 , 1 NEW_LINE print ( countTheNumbers ( l , r , y ) ) NEW_LINE


class GFG { static int [ ] numberOfLines ( String S , int [ ] widths ) { if ( S . isEmpty ( ) ) return new int [ ] { 0 , 0 } ; int lines = 1 , width = 0 ; for ( char character : S . toCharArray ( ) ) { int w = widths [ character - ' a ' ] ; width += w ; if ( width >= 10 ) { lines ++ ; width = w ; } } return new int [ ] { lines , width } ; } public static void main ( String [ ] args ) { String S = " bbbcccdddaa " ; int widths [ ] = { 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; int [ ] ans = numberOfLines ( S , widths ) ; System . out . print ( ans [ 0 ] + " ▁ " + ans [ 1 ] + " \n " ) ; } }

def numberOfLines ( S , widths ) : NEW_LINE INDENT if ( S == " " ) : NEW_LINE INDENT return 0 , 0 NEW_LINE DEDENT lines , width = 1 , 0 NEW_LINE for c in S : NEW_LINE INDENT w = widths [ ord ( c ) - ord ( ' a ' ) ] NEW_LINE width += w NEW_LINE if width > 10 : NEW_LINE INDENT lines += 1 NEW_LINE width = w NEW_LINE DEDENT DEDENT return lines , width NEW_LINE DEDENT S = " bbbcccdddaa " NEW_LINE Widths = [ 4 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE print ( numberOfLines ( S , Widths ) ) NEW_LINE


import java . util . * ; class GFG { public static int maxSum ( int arr [ ] , int n , int k ) { if ( n < k ) { System . out . println ( " Invalid " ) ; return - 1 ; } int res = 0 ; for ( int i = 0 ; i < k ; i ++ ) res += arr [ i ] ; int curr_sum = res ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; res = Math . max ( res , curr_sum ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 } ; int k = 4 ; int n = arr . length ; System . out . println ( maxSum ( arr , n , k ) ) ; } }

def maxSum ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE return - 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT res += arr [ i ] NEW_LINE DEDENT curr_sum = res NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - k ] NEW_LINE res = max ( res , curr_sum ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 ] NEW_LINE k = 4 NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n , k ) ) NEW_LINE


class GFG { static byte longSize = 8 ; static int ULL_SIZE = 8 * longSize ; static boolean bit_anagram_check ( long a ) { return ( Integer . bitCount ( ( int ) a ) == ( ULL_SIZE >> 1 ) ) ; } public static void main ( String [ ] args ) { long a = 4294967295L ; System . out . println ( bit_anagram_check ( a ) ) ; } }

ULL_SIZE = 64 NEW_LINE def bit_anagram_check ( a ) : NEW_LINE INDENT return ( bin ( a ) . count ( "1" ) == ( ULL_SIZE >> 1 ) ) NEW_LINE DEDENT a = 4294967295 NEW_LINE print ( int ( bit_anagram_check ( a ) ) ) NEW_LINE


class GFG { static boolean CheckIfstartsWithOne ( int n , int b ) { int m = ( int ) ( Math . log10 ( n ) / Math . log10 ( 2 ) ) ; for ( int i = 1 ; i <= m ; i ++ ) { if ( n >= ( int ) Math . pow ( b , i ) && n <= 2 * ( int ) Math . pow ( b , i ) - 1 ) return true ; } return false ; } public static void main ( String args [ ] ) { System . out . println ( CheckIfstartsWithOne ( 6 , 4 ) ? " Yes " : " No " ) ; System . out . println ( CheckIfstartsWithOne ( 24 , 2 ) ? " Yes " : " No " ) ; System . out . println ( CheckIfstartsWithOne ( 24 , 7 ) ? " Yes " : " No " ) ; System . out . println ( CheckIfstartsWithOne ( 24 , 15 ) ? " Yes " : " No " ) ; } }

import math NEW_LINE def CheckIfstartsWithOne ( n , b ) : NEW_LINE INDENT m = ( int ) ( math . log2 ( n ) ) ; NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT x = ( int ) ( math . pow ( b , i ) ) ; NEW_LINE if n >= x and n <= 2 * x - 1 : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT def printYesORno ( n , b ) : NEW_LINE INDENT if CheckIfstartsWithOne ( n , b ) == 1 : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT if CheckIfstartsWithOne ( n , b ) == 0 : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT printYesORno ( 6 , 4 ) ; NEW_LINE printYesORno ( 24 , 2 ) ; NEW_LINE printYesORno ( 24 , 7 ) ; NEW_LINE printYesORno ( 24 , 15 ) ; NEW_LINE


class GFG { public static void printCubeFree ( int n ) { boolean [ ] cubFree = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) cubFree [ i ] = true ; for ( int i = 2 ; i * i * i <= n ; i ++ ) { if ( cubFree [ i ] ) { for ( int multiple = 1 ; i * i * i * multiple <= n ; multiple ++ ) { cubFree [ i * i * i * multiple ] = false ; } } } for ( int i = 2 ; i <= n ; i ++ ) { if ( cubFree [ i ] == true ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { printCubeFree ( 20 ) ; } }

def printCubeFree ( n ) : NEW_LINE INDENT cubFree = [ 1 ] * ( n + 1 ) ; NEW_LINE i = 2 ; NEW_LINE while ( i * i * i <= n ) : NEW_LINE INDENT if ( cubFree [ i ] == 1 ) : NEW_LINE INDENT multiple = 1 ; NEW_LINE while ( i * i * i * multiple <= n ) : NEW_LINE INDENT cubFree [ i * i * i * multiple ] = 0 ; NEW_LINE multiple += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( cubFree [ i ] == 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT printCubeFree ( 20 ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void minBinary ( int n ) { int [ ] digit = new int [ 10 ] ; int len = 0 ; while ( n > 0 ) { digit [ len ++ ] = n % 10 ; n /= 10 ; } digit = reverse ( digit , 0 , len - 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { ans = Math . max ( ans , digit [ i ] ) ; } System . out . print ( " Minimum ▁ Number ▁ of ▁ binary " + " ▁ Strings ▁ needed : ▁ " + ans + " \n " ) ; for ( int i = 1 ; i <= ans ; i ++ ) { int num = 0 ; for ( int j = 0 ; j < len ; j ++ ) { if ( digit [ j ] > 0 ) { num = num * 10 + 1 ; digit [ j ] -- ; } else { num *= 10 ; } } System . out . print ( num + " ▁ " ) ; } } static int [ ] reverse ( int str [ ] , int start , int end ) { int temp ; while ( start <= end ) { temp = str [ start ] ; str [ start ] = str [ end ] ; str [ end ] = temp ; start ++ ; end -- ; } return str ; } public static void main ( String [ ] args ) { int n = 564 ; minBinary ( n ) ; } }

def minBinary ( n ) : NEW_LINE INDENT digit = [ 0 for i in range ( 3 ) ] NEW_LINE len = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit [ len ] = n % 10 NEW_LINE len += 1 NEW_LINE n //= 10 NEW_LINE DEDENT digit = digit [ : : - 1 ] NEW_LINE ans = 0 NEW_LINE for i in range ( len ) : NEW_LINE INDENT ans = max ( ans , digit [ i ] ) NEW_LINE DEDENT print ( " Minimum ▁ Number ▁ of ▁ binary ▁ strings ▁ needed : " , ans ) NEW_LINE for i in range ( 1 , ans + 1 , 1 ) : NEW_LINE INDENT num = 0 NEW_LINE for j in range ( 0 , len , 1 ) : NEW_LINE INDENT if ( digit [ j ] > 0 ) : NEW_LINE INDENT num = num * 10 + 1 NEW_LINE digit [ j ] -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT num *= 10 NEW_LINE DEDENT DEDENT print ( num , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 564 NEW_LINE minBinary ( n ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int find_n ( int [ ] a ) { int j = 0 , k = 0 , s = 0 ; int x = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int y = Arrays . stream ( a ) . min ( ) . getAsInt ( ) ; for ( int i : a ) { if ( a [ i ] == x ) { s = i ; break ; } } for ( int i : a ) { if ( i != x && i <= y && i != 0 ) { a [ j ] += 1 ; a [ s ] -= 1 ; x -= 1 ; k += 1 ; j += 1 ; } else if ( i != 0 ) { j += 1 ; } } for ( int i : a ) { if ( a [ i ] != x ) { k = - 1 ; break ; } } return k ; } public static void main ( String [ ] args ) { int [ ] a = { 1 , 6 , 1 , 1 , 1 } ; System . out . println ( find_n ( a ) ) ; } }

def find_n ( a ) : NEW_LINE INDENT j , k = 0 , 0 NEW_LINE x = max ( a ) NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] == x ) : NEW_LINE INDENT s = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in a : NEW_LINE INDENT if ( i != x and i <= min ( a ) and i != ' \0' ) : NEW_LINE INDENT a [ j ] += 1 NEW_LINE a [ s ] -= 1 NEW_LINE x -= 1 NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( i != ' \0' ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT for i in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] != x ) : NEW_LINE INDENT k = - 1 NEW_LINE DEDENT break NEW_LINE DEDENT return k NEW_LINE DEDENT a = [ 1 , 6 , 1 , 1 , 1 ] NEW_LINE print ( find_n ( a ) ) NEW_LINE


class Main { static int findMaximum ( int arr [ ] , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] > arr [ mid - 1 ] ) return arr [ mid ] ; if ( arr [ mid ] > arr [ mid + 1 ] && arr [ mid ] < arr [ mid - 1 ] ) return findMaximum ( arr , low , mid - 1 ) ; else return findMaximum ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 50 , 10 , 9 , 7 , 6 } ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }

def findMaximum ( arr , low , high ) : NEW_LINE INDENT if low == high : NEW_LINE INDENT return arr [ low ] NEW_LINE DEDENT if high == low + 1 and arr [ low ] >= arr [ high ] : NEW_LINE INDENT return arr [ low ] ; NEW_LINE DEDENT if high == low + 1 and arr [ low ] < arr [ high ] : NEW_LINE INDENT return arr [ high ] NEW_LINE DEDENT mid = ( low + high ) // 2 NEW_LINE if arr [ mid ] > arr [ mid + 1 ] and arr [ mid ] > arr [ mid - 1 ] : NEW_LINE INDENT return arr [ mid ] NEW_LINE DEDENT if arr [ mid ] > arr [ mid + 1 ] and arr [ mid ] < arr [ mid - 1 ] : NEW_LINE INDENT return findMaximum ( arr , low , mid - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return findMaximum ( arr , mid + 1 , high ) NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 50 , 10 , 9 , 7 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ maximum ▁ element ▁ is ▁ % d " % findMaximum ( arr , 0 , n - 1 ) ) NEW_LINE