import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < p_size ; i += p ) { prime [ i ] = false ; } } } } static void sumProdOfPrimeFreq ( char [ ] s ) { boolean [ ] prime = new boolean [ s . length + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , s . length + 1 ) ; int i , j ; Map < Character , Integer > mp = new HashMap < > ( ) ; for ( i = 0 ; i < s . length ; i ++ ) { mp . put ( s [ i ] , mp . get ( s [ i ] ) == null ? 1 : mp . get ( s [ i ] ) + 1 ) ; } int sum = 0 , product = 1 ; for ( Map . Entry < Character , Integer > it : mp . entrySet ( ) ) { if ( prime [ it . getValue ( ) ] ) { sum += it . getValue ( ) ; product *= it . getValue ( ) ; } } System . out . print ( " Sum ▁ = ▁ " + sum ) ; System . out . println ( " \n Product ▁ = ▁ " + product ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; sumProdOfPrimeFreq ( s . toCharArray ( ) ) ; } }

def SieveofEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , p_size + 1 ) : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , p_size + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumProdOfPrimeFreq ( s ) : NEW_LINE INDENT prime = [ True ] * ( len ( s ) + 2 ) NEW_LINE SieveofEratosthenes ( prime , len ( s ) + 1 ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] = ( m [ s [ i ] ] + 1 ) if s [ i ] in m else 1 NEW_LINE DEDENT s = 0 NEW_LINE product = 1 NEW_LINE for it in m : NEW_LINE INDENT if prime [ m [ it ] ] : NEW_LINE INDENT s += m [ it ] NEW_LINE product *= m [ it ] NEW_LINE DEDENT DEDENT print ( " Sum ▁ = " , s ) NEW_LINE print ( " Product ▁ = " , product ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE sumProdOfPrimeFreq ( s ) NEW_LINE DEDENT


class GFG { static int fac ( int num ) { if ( num == 0 ) return 1 ; int fact = 1 ; for ( int i = 1 ; i <= num ; i ++ ) fact = fact * i ; return fact ; } static void Tanx_expansion ( int terms , int x ) { double sum = 0 ; for ( int i = 1 ; i <= terms ; i += 1 ) { double B = 0 ; int Bn = 2 * i ; for ( int k = 0 ; k <= Bn ; k ++ ) { double temp = 0 ; for ( int r = 0 ; r <= k ; r ++ ) temp = temp + Math . pow ( - 1 , r ) * fac ( k ) * Math . pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; B = B + temp / ( ( double ) ( k + 1 ) ) ; } sum = sum + Math . pow ( - 4 , i ) * ( 1 - Math . pow ( 4 , i ) ) * B * Math . pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; } System . out . printf ( " % .9f " , sum ) ; } public static void main ( String [ ] args ) { int n = 6 , x = 1 ; Tanx_expansion ( n , x ) ; } }

def fac ( num ) : NEW_LINE INDENT if ( num == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT fact = 1 ; NEW_LINE for i in range ( 1 , num + 1 ) : NEW_LINE INDENT fact = fact * i ; NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def Tanx_expansion ( terms , x ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , terms + 1 ) : NEW_LINE INDENT B = 0 ; NEW_LINE Bn = 2 * i ; NEW_LINE for k in range ( Bn + 1 ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for r in range ( 0 , k + 1 ) : NEW_LINE INDENT temp = temp + pow ( - 1 , r ) * fac ( k )   \ NEW_LINE INDENT * pow ( r , Bn ) / ( fac ( r ) * fac ( k - r ) ) ; NEW_LINE DEDENT DEDENT B = B + temp / ( ( k + 1 ) ) ; NEW_LINE DEDENT sum = sum + pow ( - 4 , i ) * ( 1 - pow ( 4 , i ) )   \ NEW_LINE INDENT * B * pow ( x , 2 * i - 1 ) / fac ( 2 * i ) ; NEW_LINE DEDENT DEDENT print ( " % .9f " % ( sum ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , x = 6 , 1 ; NEW_LINE Tanx_expansion ( n , x ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int countFactors ( int n ) { int factors = 0 ; for ( int i = 2 ; i * i <= n ; i ++ ) { while ( n % i == 0 ) { n /= i ; factors += 1 ; } } if ( n != 1 ) factors ++ ; return factors ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int minOperations ( int A , int B ) { int g = __gcd ( A , B ) ; A /= g ; B /= g ; return countFactors ( A ) + countFactors ( B ) ; } public static void main ( String [ ] args ) { int A = 10 , B = 15 ; System . out . println ( minOperations ( A , B ) ) ; } }

from math import sqrt , gcd NEW_LINE def countFactors ( n ) : NEW_LINE INDENT factors = 0 ; NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT n //= i NEW_LINE factors += 1 NEW_LINE DEDENT DEDENT if ( n != 1 ) : NEW_LINE INDENT factors += 1 NEW_LINE DEDENT return factors NEW_LINE DEDENT def minOperations ( A , B ) : NEW_LINE INDENT g = gcd ( A , B ) NEW_LINE A //= g NEW_LINE B //= g NEW_LINE return countFactors ( A ) + countFactors ( B ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , B = 10 , 15 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GfG { static void solution ( int a , int b , int n ) { for ( int i = 0 ; i * a <= n ; i ++ ) { if ( ( n - ( i * a ) ) % b == 0 ) { System . out . println ( " x ▁ = ▁ " + i + " , ▁ y ▁ = ▁ " + ( n - ( i * a ) ) / b ) ; return ; } } System . out . println ( " No ▁ solution " ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 3 , n = 7 ; solution ( a , b , n ) ; } }

def solution ( a , b , n ) : NEW_LINE INDENT i = 0 NEW_LINE while i * a <= n : NEW_LINE INDENT if ( n - ( i * a ) ) % b == 0 : NEW_LINE INDENT print ( " x ▁ = ▁ " , i , " , ▁ y ▁ = ▁ " , int ( ( n - ( i * a ) ) / b ) ) NEW_LINE return 0 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT print ( " No ▁ solution " ) NEW_LINE DEDENT a = 2 NEW_LINE b = 3 NEW_LINE n = 7 NEW_LINE solution ( a , b , n ) NEW_LINE


import java . io . * ; class GFG { static int getCount ( int arr [ ] , int n , int num1 , int num2 ) { int i = 0 ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == num1 ) break ; if ( i >= n - 1 ) return 0 ; int j ; for ( j = n - 1 ; j >= i + 1 ; j -- ) if ( arr [ j ] == num2 ) break ; if ( j == i ) return 0 ; return ( j - i - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 } ; int n = arr . length ; int num1 = 5 , num2 = 4 ; System . out . println ( getCount ( arr , n , num1 , num2 ) ) ; } }

def getCount ( arr , n , num1 , num2 ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == num1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i >= n - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for j in range ( n - 1 , i + 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] == num2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == i ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( j - i - 1 ) NEW_LINE DEDENT arr = [ 3 , 5 , 7 , 6 , 4 , 9 , 12 , 4 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE num1 = 5 NEW_LINE num2 = 4 NEW_LINE print ( getCount ( arr , n , num1 , num2 ) ) NEW_LINE


public class GFG { public static void getSmallestAndLargest ( String s , int k ) { String currStr = s . substring ( 0 , k ) ; String lexMin = currStr ; String lexMax = currStr ; for ( int i = k ; i < s . length ( ) ; i ++ ) { currStr = currStr . substring ( 1 , k ) + s . charAt ( i ) ; if ( lexMax . compareTo ( currStr ) < 0 ) lexMax = currStr ; if ( lexMin . compareTo ( currStr ) > 0 ) lexMin = currStr ; } System . out . println ( lexMin ) ; System . out . println ( lexMax ) ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int k = 3 ; getSmallestAndLargest ( str , k ) ; } }

def getSmallestAndLargest ( s , k ) : NEW_LINE INDENT currStr = s [ : k ] NEW_LINE lexMin = currStr NEW_LINE lexMax = currStr NEW_LINE for i in range ( k , len ( s ) ) : NEW_LINE INDENT currStr = currStr [ 1 : k ] + s [ i ] NEW_LINE if ( lexMax < currStr ) : NEW_LINE INDENT lexMax = currStr NEW_LINE DEDENT if ( lexMin > currStr ) : NEW_LINE INDENT lexMin = currStr NEW_LINE DEDENT DEDENT print ( lexMin ) NEW_LINE print ( lexMax ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " GeeksForGeeks " NEW_LINE k = 3 NEW_LINE getSmallestAndLargest ( str1 , k ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int sumOfDigitsSingle ( int x ) { int ans = 0 ; while ( x != 0 ) { ans += x % 10 ; x /= 10 ; } return ans ; } static int closest ( int x ) { int ans = 0 ; while ( ans * 10 + 9 <= x ) ans = ans * 10 + 9 ; return ans ; } static int sumOfDigitsTwoParts ( int N ) { int A = closest ( N ) ; return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) ; } public static void main ( String args [ ] ) { int N = 35 ; System . out . print ( sumOfDigitsTwoParts ( N ) ) ; } }

def sumOfDigitsSingle ( x ) : NEW_LINE INDENT ans = 0 NEW_LINE while x : NEW_LINE INDENT ans += x % 10 NEW_LINE x //= 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT def closest ( x ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( ans * 10 + 9 <= x ) : NEW_LINE INDENT ans = ans * 10 + 9 NEW_LINE DEDENT return ans NEW_LINE DEDENT def sumOfDigitsTwoParts ( N ) : NEW_LINE INDENT A = closest ( N ) NEW_LINE return sumOfDigitsSingle ( A ) + sumOfDigitsSingle ( N - A ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 35 NEW_LINE print ( sumOfDigitsTwoParts ( N ) ) NEW_LINE DEDENT


class GFG { static void initialize ( int mul [ ] , int div [ ] , int size ) { for ( int i = 1 ; i < size ; i ++ ) { mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; } } static void update ( int l , int r , int x , int mul [ ] , int div [ ] ) { mul [ l ] *= x ; div [ r + 1 ] *= x ; } static void printArray ( int ar [ ] , int mul [ ] , int div [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { ar [ i ] = ar [ i ] * mul [ i ] ; System . out . print ( ar [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int ar [ ] = { 10 , 5 , 20 , 40 } ; int n = ar . length ; int [ ] mul = new int [ n + 1 ] ; int [ ] div = new int [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mul [ i ] = div [ i ] = 1 ; } update ( 0 , 1 , 10 , mul , div ) ; update ( 1 , 3 , 20 , mul , div ) ; update ( 2 , 2 , 2 , mul , div ) ; initialize ( mul , div , n + 1 ) ; printArray ( ar , mul , div , n ) ; } }

def initialize ( mul , div , size ) : NEW_LINE INDENT for i in range ( 1 , size ) : NEW_LINE INDENT mul [ i ] = ( mul [ i ] * mul [ i - 1 ] ) / div [ i ] ; NEW_LINE DEDENT DEDENT def update ( l , r , x , mul , div ) : NEW_LINE INDENT mul [ l ] *= x ; NEW_LINE div [ r + 1 ] *= x ; NEW_LINE DEDENT def printArray ( ar , mul , div , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT ar [ i ] = ar [ i ] * mul [ i ] ; NEW_LINE print ( int ( ar [ i ] ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ 10 , 5 , 20 , 40 ] ; NEW_LINE n = len ( ar ) ; NEW_LINE mul = [ 0 ] * ( n + 1 ) ; NEW_LINE div = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT mul [ i ] = div [ i ] = 1 ; NEW_LINE DEDENT update ( 0 , 1 , 10 , mul , div ) ; NEW_LINE update ( 1 , 3 , 20 , mul , div ) ; NEW_LINE update ( 2 , 2 , 2 , mul , div ) ; NEW_LINE initialize ( mul , div , n + 1 ) ; NEW_LINE printArray ( ar , mul , div , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean areSetBitsIncreasing ( int n ) { int prev_count = Integer . MAX_VALUE ; while ( n > 0 ) { while ( n > 0 && n % 2 == 0 ) n = n / 2 ; int curr_count = 1 ; while ( n > 0 && n % 2 == 1 ) { n = n / 2 ; curr_count ++ ; } if ( curr_count >= prev_count ) return false ; prev_count = curr_count ; } return true ; } static public void main ( String [ ] args ) { int n = 10 ; if ( areSetBitsIncreasing ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

import sys NEW_LINE def areSetBitsIncreasing ( n ) : NEW_LINE INDENT prev_count = sys . maxsize NEW_LINE while ( n > 0 ) : NEW_LINE INDENT while ( n > 0 and n % 2 == 0 ) : NEW_LINE INDENT n = int ( n / 2 ) NEW_LINE DEDENT curr_count = 1 NEW_LINE while ( n > 0 and n % 2 == 1 ) : NEW_LINE INDENT n = n / 2 NEW_LINE curr_count += 1 NEW_LINE DEDENT if ( curr_count >= prev_count ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev_count = curr_count NEW_LINE DEDENT return True NEW_LINE DEDENT n = 10 NEW_LINE if ( areSetBitsIncreasing ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void D_Pattern ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( j == 1 || ( ( i == 0 || i == n - 1 ) && ( j > 1 && j < n - 2 ) ) || ( j == n - 2 && i != 0 && i != n - 1 ) ) System . out . print ( " * " ) ; else System . out . print ( " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 9 ; D_Pattern ( n ) ; } }

def D_Pattern ( string , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( j == 1 or ( ( i == 0 or i == n - 1 ) and ( j > 1 and j < n - 2 ) ) or ( j == n - 2 and i != 0 and i != n - 1 ) ) : NEW_LINE INDENT string = string + " * " NEW_LINE DEDENT else : NEW_LINE INDENT string = string + " ▁ " NEW_LINE DEDENT DEDENT string = string + " \n " NEW_LINE DEDENT return ( string ) ; NEW_LINE DEDENT string = " " ; NEW_LINE n = 9 NEW_LINE print ( D_Pattern ( string , n ) ) ; NEW_LINE


import java . util . Arrays ; public class Main { public static int nobleInteger ( int arr [ ] ) { Arrays . sort ( arr ) ; int n = arr . length ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) continue ; if ( arr [ i ] == n - i - 1 ) return arr [ i ] ; } if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ; return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) System . out . println ( " The ▁ noble ▁ integer ▁ is ▁ " + res ) ; else System . out . println ( " No ▁ Noble ▁ Integer ▁ Found " ) ; } }

def nobleInteger ( arr ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE n = len ( arr ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if arr [ i ] == arr [ i + 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT if arr [ i ] == n - i - 1 : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT if arr [ n - 1 ] == 0 : NEW_LINE INDENT return arr [ n - 1 ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr = [ 10 , 3 , 20 , 40 , 2 ] NEW_LINE res = nobleInteger ( arr ) NEW_LINE if res != - 1 : NEW_LINE INDENT print ( " The ▁ noble ▁ integer ▁ is " , res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Noble ▁ Intger ▁ Found " ) NEW_LINE DEDENT


class GFG { static void CountWords ( String str , int k ) { int sum = 0 ; int NumberOfWords = 0 ; int counter = 0 ; int len = str . length ( ) ; for ( int i = 0 ; i < len ; ++ i ) { if ( str . charAt ( i ) == ' ▁ ' ) { if ( sum < k ) { counter ++ ; } sum = 0 ; NumberOfWords ++ ; } else { sum += str . charAt ( i ) ; } } NumberOfWords ++ ; if ( sum < k ) { counter ++ ; } System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ " + " of ▁ ASCII ▁ values ▁ less ▁ than ▁ k ▁ = ▁ " + counter ) ; System . out . println ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ " + " ASCII ▁ values ▁ greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = ▁ " + ( NumberOfWords - counter ) ) ; } public static void main ( String [ ] args ) { String str = " Learn ▁ how ▁ to ▁ code " ; int k = 400 ; CountWords ( str , k ) ; } }

def CountWords ( str , k ) : NEW_LINE INDENT sum = 0 NEW_LINE NumberOfWords = 0 NEW_LINE counter = 0 NEW_LINE l = len ( str ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( str [ i ] == ' ▁ ' ) : NEW_LINE INDENT if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT sum = 0 NEW_LINE NumberOfWords += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += ord ( str [ i ] ) NEW_LINE DEDENT DEDENT NumberOfWords += 1 NEW_LINE if ( sum < k ) : NEW_LINE INDENT counter += 1 NEW_LINE DEDENT print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII " , " values ▁ less ▁ than ▁ k ▁ = " , counter ) NEW_LINE print ( " Number ▁ of ▁ words ▁ having ▁ sum ▁ of ▁ ASCII ▁ values " , " greater ▁ than ▁ or ▁ equal ▁ to ▁ k ▁ = " , NumberOfWords - counter ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " Learn ▁ how ▁ to ▁ code " NEW_LINE k = 400 NEW_LINE CountWords ( str , k ) NEW_LINE DEDENT


import java . util . * ; class GFG { static char [ ] CHARS = "0123456789ABCDEF " . toCharArray ( ) ; static int DIGITS = 16 ; static boolean isDivisible ( String s , int m ) { Map < Character , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < DIGITS ; i ++ ) { mp . put ( CHARS [ i ] , i ) ; } int r = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { r = ( r * 16 + mp . get ( s . charAt ( i ) ) ) % m ; } if ( r == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = "10" ; int m = 3 ; if ( isDivisible ( s , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

CHARS = "0123456789ABCDEF " ; NEW_LINE DIGITS = 16 ; NEW_LINE def isDivisible ( s , m ) : NEW_LINE INDENT mp = dict . fromkeys ( CHARS , 0 ) ; NEW_LINE for i in range ( DIGITS ) : NEW_LINE INDENT mp [ CHARS [ i ] ] = i ; NEW_LINE DEDENT r = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT r = ( r * 16 + mp [ s [ i ] ] ) % m ; NEW_LINE DEDENT if ( not r ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "10" ; NEW_LINE m = 3 ; NEW_LINE if ( isDivisible ( s , m ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


public class GCD { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int countPairs ( int G , int L ) { int count = 0 ; int p = G * L ; for ( int a = 1 ; a <= L ; a ++ ) if ( ( p % a == 0 ) && gcd ( a , p / a ) == G ) count ++ ; return count ; } public static void main ( String [ ] args ) { int G = 2 , L = 12 ; System . out . print ( " Total ▁ possible ▁ pair ▁ with ▁ GCD ▁ " + G ) ; System . out . print ( " ▁ & ▁ LCM ▁ " + L ) ; System . out . print ( " ▁ = ▁ " + countPairs ( G , L ) ) ; } }

import math NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return math . gcd ( b % a , a ) NEW_LINE DEDENT def countPairs ( G , L ) : NEW_LINE INDENT count = 0 NEW_LINE p = G * L NEW_LINE for a in range ( 1 , L + 1 ) : NEW_LINE INDENT if ( not ( p % a ) and math . gcd ( a , p // a ) == G ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT G = 2 NEW_LINE L = 12 NEW_LINE print ( " Total ▁ possible ▁ pair ▁ with ▁ GCD ▁ " , G , end = " " ) NEW_LINE print ( " ▁ & ▁ LCM ▁ " , L , end = " " ) NEW_LINE print ( " ▁ = ▁ " , countPairs ( G , L ) ) NEW_LINE DEDENT


class GFG { static int n = 3 ; static int maxV = 20 ; static int [ ] [ ] [ ] dp = new int [ n ] [ n ] [ maxV ] ; static int [ ] [ ] [ ] v = new int [ n ] [ n ] [ maxV ] ; static int countWays ( int i , int j , int x , int arr [ ] [ ] ) { if ( i == n || j == n ) { return 0 ; } x = ( x & arr [ i ] [ j ] ) ; if ( x == 0 ) { return 0 ; } if ( i == n - 1 && j == n - 1 ) { return 1 ; } if ( v [ i ] [ j ] [ x ] == 1 ) { return dp [ i ] [ j ] [ x ] ; } v [ i ] [ j ] [ x ] = 1 ; dp [ i ] [ j ] [ x ] = countWays ( i + 1 , j , x , arr ) + countWays ( i , j + 1 , x , arr ) ; return dp [ i ] [ j ] [ x ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 1 } , { 1 , 1 , 0 } , { 2 , 1 , 1 } } ; System . out . println ( countWays ( 0 , 0 , arr [ 0 ] [ 0 ] , arr ) ) ; } }

n = 3 NEW_LINE maxV = 20 NEW_LINE dp = [ [ [ 0 for i in range ( maxV ) ] for i in range ( n ) ] for i in range ( n ) ] NEW_LINE v = [ [ [ 0 for i in range ( maxV ) ] for i in range ( n ) ] for i in range ( n ) ] NEW_LINE def countWays ( i , j , x , arr ) : NEW_LINE INDENT if ( i == n or j == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT x = ( x & arr [ i ] [ j ] ) NEW_LINE if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == n - 1 and j == n - 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( v [ i ] [ j ] [ x ] ) : NEW_LINE INDENT return dp [ i ] [ j ] [ x ] NEW_LINE DEDENT v [ i ] [ j ] [ x ] = 1 NEW_LINE dp [ i ] [ j ] [ x ] = countWays ( i + 1 , j , x , arr ) +   \ NEW_LINE INDENT countWays ( i , j + 1 , x , arr ) ; NEW_LINE DEDENT return dp [ i ] [ j ] [ x ] NEW_LINE DEDENT arr = [ [ 1 , 2 , 1 ] , [ 1 , 1 , 0 ] , [ 2 , 1 , 1 ] ] NEW_LINE print ( countWays ( 0 , 0 , arr [ 0 ] [ 0 ] , arr ) ) NEW_LINE


class EggDrop { static int max ( int a , int b ) { return ( a > b ) ? a : b ; } static int eggDrop ( int n , int k ) { int eggFloor [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i ] [ 1 ] = 1 ; eggFloor [ i ] [ 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 ] [ j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 2 ; j <= k ; j ++ ) { eggFloor [ i ] [ j ] = Integer . MAX_VALUE ; for ( x = 1 ; x <= j ; x ++ ) { res = 1 + max ( eggFloor [ i - 1 ] [ x - 1 ] , eggFloor [ i ] [ j - x ] ) ; if ( res < eggFloor [ i ] [ j ] ) eggFloor [ i ] [ j ] = res ; } } } return eggFloor [ n ] [ k ] ; } public static void main ( String args [ ] ) { int n = 2 , k = 10 ; System . out . println ( " Minimum ▁ number ▁ of ▁ trials ▁ in ▁ worst ▁ case ▁ with ▁ " + n + " ▁ ▁ eggs ▁ and ▁ " + k + " ▁ floors ▁ is ▁ " + eggDrop ( n , k ) ) ; } }

INT_MAX = 32767 NEW_LINE def eggDrop ( n , k ) : NEW_LINE INDENT eggFloor = [ [ 0 for x in range ( k + 1 ) ] for x in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT eggFloor [ i ] [ 1 ] = 1 NEW_LINE eggFloor [ i ] [ 0 ] = 0 NEW_LINE DEDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT eggFloor [ 1 ] [ j ] = j NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 2 , k + 1 ) : NEW_LINE INDENT eggFloor [ i ] [ j ] = INT_MAX NEW_LINE for x in range ( 1 , j + 1 ) : NEW_LINE INDENT res = 1 + max ( eggFloor [ i - 1 ] [ x - 1 ] , eggFloor [ i ] [ j - x ] ) NEW_LINE if res < eggFloor [ i ] [ j ] : NEW_LINE INDENT eggFloor [ i ] [ j ] = res NEW_LINE DEDENT DEDENT DEDENT DEDENT return eggFloor [ n ] [ k ] NEW_LINE DEDENT n = 2 NEW_LINE k = 36 NEW_LINE print ( " Minimum ▁ number ▁ of ▁ trials ▁ in ▁ worst ▁ case ▁ with " + str ( n ) + " eggs ▁ and ▁ " + str ( k ) + " ▁ floors ▁ is ▁ " + str ( eggDrop ( n , k ) ) ) NEW_LINE


import java . util . Scanner ; public class Prism { public static void Calculate_area ( ) { double b = 3 , h = 4 , s1 = 3 , s2 = 6 ; double s3 = 6 , Ht = 8 , SA ; SA = b * h + ( s1 + s2 + s3 ) * Ht ; System . out . printf ( " The ▁ area ▁ of ▁ triangular ▁ prism ▁ is ▁ : ▁ % f " , SA ) ; } public static void main ( String [ ] args ) { Calculate_area ( ) ; } }

def Calculate_area ( ) : NEW_LINE INDENT b = 3 NEW_LINE h = 4 NEW_LINE s1 = 3 NEW_LINE s2 = 6 NEW_LINE s3 = 6 NEW_LINE Ht = 8 NEW_LINE SA = b * h + ( s1 + s2 + s3 ) * Ht NEW_LINE print ( " The ▁ area ▁ of ▁ triangular ▁ prism ▁ is ▁ : " , SA ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Calculate_area ( ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int findNumber ( int n ) { int x = ( int ) Math . floor ( ( - 1 + Math . sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ; int base = ( x * ( x + 1 ) ) / 2 + 1 ; return n - base + 1 ; } public static void main ( String [ ] args ) { int n = 55 ; System . out . println ( findNumber ( n ) ) ; } }

import math NEW_LINE def findNumber ( n ) : NEW_LINE INDENT x = int ( math . floor ( ( - 1 + math . sqrt ( 1 + 8 * n - 8 ) ) / 2 ) ) NEW_LINE base = ( x * ( x + 1 ) ) / 2 + 1 NEW_LINE return n - base + 1 NEW_LINE DEDENT n = 55 NEW_LINE print ( findNumber ( n ) ) NEW_LINE


import java . lang . * ; class GFG { static int minSwap ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( arr [ i ] <= k ) ++ count ; int bad = 0 ; for ( int i = 0 ; i < count ; ++ i ) if ( arr [ i ] > k ) ++ bad ; int ans = bad ; for ( int i = 0 , j = count ; j < n ; ++ i , ++ j ) { if ( arr [ i ] > k ) -- bad ; if ( arr [ j ] > k ) ++ bad ; ans = Math . min ( ans , bad ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 5 , 6 , 3 } ; int n = arr . length ; int k = 3 ; System . out . print ( minSwap ( arr , n , k ) + " \n " ) ; int arr1 [ ] = { 2 , 7 , 9 , 5 , 8 , 7 , 4 } ; n = arr1 . length ; k = 5 ; System . out . print ( minSwap ( arr1 , n , k ) ) ; } }

def minSwap ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] <= k ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT bad = 0 NEW_LINE for i in range ( 0 , count ) : NEW_LINE INDENT if ( arr [ i ] > k ) : NEW_LINE INDENT bad = bad + 1 NEW_LINE DEDENT DEDENT ans = bad NEW_LINE j = count NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( j == n ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( arr [ i ] > k ) : NEW_LINE INDENT bad = bad - 1 NEW_LINE DEDENT if ( arr [ j ] > k ) : NEW_LINE INDENT bad = bad + 1 NEW_LINE DEDENT ans = min ( ans , bad ) NEW_LINE j = j + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 5 , 6 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( minSwap ( arr , n , k ) ) NEW_LINE arr1 = [ 2 , 7 , 9 , 5 , 8 , 7 , 4 ] NEW_LINE n = len ( arr1 ) NEW_LINE k = 5 NEW_LINE print ( minSwap ( arr1 , n , k ) ) NEW_LINE


public class GFG { static int kthOdd ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) k -- ; if ( k == 0 ) return arr [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 2 ; System . out . print ( kthOdd ( arr , n , k ) ) ; } }

def kthOdd ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT k -= 1 ; NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT return arr [ i ] ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( kthOdd ( arr , n , k ) ) ; NEW_LINE


import java . util . * ; class GFG { static String balancedBrackets ( String str ) { int dep = 0 ; int minDep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; if ( minDep > dep ) minDep = dep ; } if ( minDep < 0 ) { for ( int i = 0 ; i < Math . abs ( minDep ) ; i ++ ) str = ' ( ' + str ; } dep = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) dep ++ ; else dep -- ; } if ( dep != 0 ) { for ( int i = 0 ; i < dep ; i ++ ) str = str + ' ) ' ; } return str ; } public static void main ( String [ ] args ) { String str = " ) ) ) ( ) " ; System . out . println ( balancedBrackets ( str ) ) ; } }

def balancedBrackets ( Str ) : NEW_LINE INDENT dep = 0 NEW_LINE minDep = 0 NEW_LINE for i in Str : NEW_LINE INDENT if ( i == ' ( ' ) : NEW_LINE INDENT dep += 1 NEW_LINE DEDENT else : NEW_LINE INDENT dep -= 1 NEW_LINE DEDENT if ( minDep > dep ) : NEW_LINE INDENT minDep = dep NEW_LINE DEDENT DEDENT if ( minDep < 0 ) : NEW_LINE INDENT for i in range ( abs ( minDep ) ) : NEW_LINE INDENT Str = ' ( ' + Str NEW_LINE DEDENT DEDENT dep = 0 NEW_LINE for i in Str : NEW_LINE INDENT if ( i == ' ( ' ) : NEW_LINE INDENT dep += 1 NEW_LINE DEDENT else : NEW_LINE INDENT dep -= 1 NEW_LINE DEDENT DEDENT if ( dep != 0 ) : NEW_LINE INDENT for i in range ( dep ) : NEW_LINE INDENT Str = Str + ' ) ' NEW_LINE DEDENT DEDENT return Str NEW_LINE DEDENT Str = " ) ) ) ( ) " NEW_LINE print ( balancedBrackets ( Str ) ) NEW_LINE


class GFG { static boolean isKAlternating ( String s , int k ) { if ( s . length ( ) < k ) return false ; int checker = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int bitAtIndex = s . charAt ( i ) - ' a ' ; if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) { return false ; } checker = checker | ( 1 << bitAtIndex ) ; } for ( int i = k ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i - k ) != s . charAt ( i ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " acdeac " ; int K = 4 ; if ( isKAlternating ( str , K ) ) System . out . print ( " Yes " + " \n " ) ; else System . out . print ( " No " + " \n " ) ; } }

def isKAlternating ( s , k ) : NEW_LINE INDENT if ( len ( s ) < k ) : NEW_LINE INDENT return False NEW_LINE DEDENT checker = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT bitAtIndex = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE if ( ( checker & ( 1 << bitAtIndex ) ) > 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT checker = checker | ( 1 << bitAtIndex ) NEW_LINE DEDENT for i in range ( k , len ( s ) ) : NEW_LINE INDENT if ( s [ i - k ] != s [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = " acdeac " NEW_LINE K = 4 NEW_LINE if ( isKAlternating ( st , K ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; import java . lang . * ; class GFG { public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 15 , g ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; a = 35 ; b = 10 ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; a = 31 ; b = 2 ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; } }

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 a = 10 NEW_LINE b = 15 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE a = 35 NEW_LINE b = 10 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE a = 31 NEW_LINE b = 2 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE


import java . util . * ; class CountSubsequences { public static int productSubSeqCount ( ArrayList < Integer > arr , int k ) { int n = arr . size ( ) ; int dp [ ] [ ] = new int [ k + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { dp [ i ] [ j ] = dp [ i ] [ j - 1 ] ; if ( arr . get ( j - 1 ) <= i && arr . get ( j - 1 ) > 0 ) dp [ i ] [ j ] += dp [ i / arr . get ( j - 1 ) ] [ j - 1 ] + 1 ; } } return dp [ k ] [ n ] ; } public static void main ( String args [ ] ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; A . add ( 1 ) ; A . add ( 2 ) ; A . add ( 3 ) ; A . add ( 4 ) ; int k = 10 ; System . out . println ( productSubSeqCount ( A , k ) ) ; } }

def productSubSeqCount ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( k + 1 ) ] NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ j - 1 ] NEW_LINE if arr [ j - 1 ] <= i and arr [ j - 1 ] > 0 : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i // arr [ j - 1 ] ] [ j - 1 ] + 1 NEW_LINE DEDENT DEDENT DEDENT return dp [ k ] [ n ] NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 ] NEW_LINE k = 10 NEW_LINE print ( productSubSeqCount ( A , k ) ) NEW_LINE


import java . util . * ; class GFG { public static void printLogestIncSubArr ( int arr [ ] , int n ) { int max = 1 , len = 1 , maxIndex = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) len ++ ; else { if ( max < len ) { max = len ; maxIndex = i - max ; } len = 1 ; } } if ( max < len ) { max = len ; maxIndex = n - max ; } for ( int i = maxIndex ; i < max + maxIndex ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 3 , 5 , 7 , 8 , 9 , 1 , 2 } ; int n = arr . length ; printLogestIncSubArr ( arr , n ) ; } }

def printLogestIncSubArr ( arr , n ) : NEW_LINE INDENT m = 1 NEW_LINE l = 1 NEW_LINE maxIndex = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT l = l + 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( m < l ) : NEW_LINE INDENT m = l NEW_LINE maxIndex = i - m NEW_LINE DEDENT l = 1 NEW_LINE DEDENT DEDENT if ( m < l ) : NEW_LINE INDENT m = l NEW_LINE maxIndex = n - m NEW_LINE DEDENT for i in range ( maxIndex , ( m + maxIndex ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 5 , 6 , 3 , 5 , 7 , 8 , 9 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE printLogestIncSubArr ( arr , n ) NEW_LINE


import java . io . * ; class GFG { static int findProduct ( int arr [ ] , int n , int k ) { int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { prod *= arr [ i ] ; } } return prod ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 16 , 10 , 9 , 6 , 7 , 17 } ; int n = arr . length ; int k = 3 ; System . out . println ( findProduct ( arr , n , k ) ) ; } }

def findProduct ( arr , n , k ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT prod *= arr [ i ] NEW_LINE DEDENT DEDENT return prod NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 15 , 16 , 10 , 9 , 6 , 7 , 17 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( findProduct ( arr , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void isPerfectSquare ( int x ) { int sr = ( int ) Math . sqrt ( x ) ; if ( sr * sr == x ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int n = 7 , k = 2 ; isPerfectSquare ( n + k ) ; } }

from math import sqrt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = int ( sqrt ( x ) ) ; NEW_LINE if ( sr * sr == x ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 ; k = 2 ; NEW_LINE isPerfectSquare ( n + k ) ; NEW_LINE DEDENT


class GFG { final static int MOD = 1000000007 ; static long modInv ( long a ) { long p = MOD - 2 ; long s = 1 ; while ( p != 1 ) { if ( p % 2 == 1 ) s = ( s * a ) % MOD ; a = ( a * a ) % MOD ; p /= 2 ; } return ( a * s ) % MOD ; } static long findCnt ( char x , String y ) { long ans = 0 ; long fact [ ] = new long [ y . length ( ) + 1 ] ; for ( int i = 0 ; i < y . length ( ) + 1 ; i ++ ) fact [ i ] = 1 ; for ( int i = 1 ; i <= y . length ( ) ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; for ( int i = 0 ; i < y . length ( ) ; i ++ ) { if ( y . charAt ( i ) == x ) { ans += ( modInv ( fact [ i ] ) * modInv ( fact [ y . length ( ) - i - 1 ] ) ) % MOD ; ans %= MOD ; } } ans *= fact [ ( y . length ( ) - 1 ) ] ; ans %= MOD ; return ans ; } public static void main ( String [ ] args ) { char x = ' a ' ; String y = " xxayy " ; System . out . println ( findCnt ( x , y ) ) ; } }

MOD = 1000000007 ; NEW_LINE def modInv ( a , p = MOD - 2 ) : NEW_LINE INDENT s = 1 ; NEW_LINE while ( p != 1 ) : NEW_LINE INDENT if ( p % 2 ) : NEW_LINE INDENT s = ( s * a ) % MOD ; NEW_LINE DEDENT a = ( a * a ) % MOD ; NEW_LINE p //= 2 ; NEW_LINE DEDENT return ( a * s ) % MOD ; NEW_LINE DEDENT def findCnt ( x , y ) : NEW_LINE INDENT ans = 0 ; NEW_LINE fact = [ 1 ] * ( len ( y ) + 1 ) ; NEW_LINE for i in range ( 1 , len ( y ) ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; NEW_LINE DEDENT for i in range ( len ( y ) ) : NEW_LINE INDENT if ( y [ i ] == x ) : NEW_LINE INDENT ans += ( modInv ( fact [ i ] ) * modInv ( fact [ len ( y ) - i - 1 ] ) ) % MOD ; NEW_LINE ans %= MOD ; NEW_LINE DEDENT DEDENT ans *= fact [ ( len ( y ) - 1 ) ] ; NEW_LINE ans %= MOD ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = ' a ' ; NEW_LINE y = " xxayy " ; NEW_LINE print ( findCnt ( x , y ) ) ; NEW_LINE DEDENT


class GFG { static boolean isPowerOfTwo ( int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int previousPowerOfTwo ( int n ) { while ( ( n & n - 1 ) > 1 ) { n = n & n - 1 ; } return n ; } static boolean checkSum ( int n ) { if ( n == 0 || n == 1 ) return false ; else if ( isPowerOfTwo ( n ) ) { System . out . println ( n / 2 + " ▁ " + n / 2 ) ; } else { int x = previousPowerOfTwo ( n ) ; int y = n - x ; if ( isPowerOfTwo ( y ) ) { System . out . println ( x + " ▁ " + y ) ; return true ; } } return false ; } public static void main ( String [ ] argc ) { int n1 = 20 ; if ( checkSum ( n1 ) == false ) System . out . println ( " No " ) ; System . out . println ( ) ; int n2 = 11 ; if ( checkSum ( n2 ) == false ) System . out . println ( " No " ) ; } }

def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def previousPowerOfTwo ( n ) : NEW_LINE INDENT while ( n & n - 1 ) : NEW_LINE INDENT n = n & n - 1 NEW_LINE DEDENT return n NEW_LINE DEDENT def checkSum ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( isPowerOfTwo ( n ) ) : NEW_LINE INDENT print ( n // 2 , n // 2 ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT x = previousPowerOfTwo ( n ) NEW_LINE y = n - x ; NEW_LINE if ( isPowerOfTwo ( y ) ) : NEW_LINE INDENT print ( x , y ) NEW_LINE return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT n1 = 20 NEW_LINE if ( checkSum ( n1 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT n2 = 11 NEW_LINE if ( checkSum ( n2 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void main ( String args [ ] ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . length ; findArray ( q , n ) ; } }

def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def findArray ( q , n ) : NEW_LINE INDENT arr = [ None ] * n NEW_LINE k = 0 NEW_LINE for j in range ( int ( n / 4 ) ) : NEW_LINE INDENT ans = q [ k ] ^ q [ k + 3 ] NEW_LINE arr [ k + 1 ] = q [ k + 1 ] ^ ans NEW_LINE arr [ k + 2 ] = q [ k + 2 ] ^ ans NEW_LINE arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) NEW_LINE arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) NEW_LINE k += 4 NEW_LINE DEDENT printArray ( arr , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = [ 4 , 1 , 7 , 0 ] NEW_LINE n = len ( q ) NEW_LINE findArray ( q , n ) NEW_LINE DEDENT


class GFG { static int countWays ( int n , int arr [ ] , int len ) { if ( n == 0 ) return 1 ; int no_ways = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( n - arr [ i ] >= 0 ) { no_ways += countWays ( n - arr [ i ] , arr , len ) ; } } return no_ways ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 5 } ; int len = arr . length ; ; int n = 5 ; System . out . println ( countWays ( n , arr , len ) ) ; } }

def countWays ( n , arr ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT no_ways = 0 NEW_LINE for i in arr : NEW_LINE INDENT if ( n - i >= 0 ) : NEW_LINE INDENT no_ways = no_ways + countWays ( n - i , arr ) NEW_LINE DEDENT DEDENT return no_ways NEW_LINE DEDENT arr = [ 1 , 3 , 5 ] NEW_LINE n = 5 NEW_LINE print ( countWays ( n , arr ) ) NEW_LINE


class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 3 ; System . out . println ( minPower ( n , x ) ) ; } }

def minPower ( n , x ) : NEW_LINE INDENT if ( x == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT ans = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( n % x ) NEW_LINE n //= x NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE x = 3 NEW_LINE print ( minPower ( n , x ) ) NEW_LINE


import java . util . * ; class GFG { static boolean pointsAreOnSameSideOfLine ( int a , int b , int c , int x1 , int y1 , int x2 , int y2 ) { int fx1 ; int fx2 ; fx1 = a * x1 + b * y1 - c ; fx2 = a * x2 + b * y2 - c ; if ( ( fx1 * fx2 ) > 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 1 , b = 1 , c = 1 ; int x1 = 1 , y1 = 1 ; int x2 = 2 , y2 = 1 ; if ( pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT fx1 = 0 NEW_LINE fx2 = 0 NEW_LINE fx1 = a * x1 + b * y1 - c NEW_LINE fx2 = a * x2 + b * y2 - c NEW_LINE if ( ( fx1 * fx2 ) > 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a , b , c = 1 , 1 , 1 NEW_LINE x1 , y1 = 1 , 1 NEW_LINE x2 , y2 = 2 , 1 NEW_LINE if ( pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { Node left ; Node right ; int data ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static void PrintLeafLeftToRight ( Node root ) { Stack < Node > s1 = new Stack < > ( ) ; Stack < Node > s2 = new Stack < > ( ) ; ; s1 . push ( root ) ; while ( ! s1 . empty ( ) ) { Node curr = s1 . peek ( ) ; s1 . pop ( ) ; if ( curr . left != null ) s1 . push ( curr . left ) ; if ( curr . right != null ) s1 . push ( curr . right ) ; else if ( curr . left == null && curr . right == null ) s2 . push ( curr ) ; } while ( ! s2 . empty ( ) ) { System . out . print ( s2 . peek ( ) . data + " ▁ " ) ; s2 . pop ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 11 ) ; root . right . right . left = newNode ( 8 ) ; PrintLeafLeftToRight ( root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def PrintLeafLeftToRight ( root ) : NEW_LINE INDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE s1 . append ( root ) NEW_LINE while len ( s1 ) != 0 : NEW_LINE INDENT curr = s1 . pop ( ) NEW_LINE if curr . left : NEW_LINE INDENT s1 . append ( curr . left ) NEW_LINE DEDENT if curr . right : NEW_LINE INDENT s1 . append ( curr . right ) NEW_LINE DEDENT elif not curr . left and not curr . right : NEW_LINE INDENT s2 . append ( curr ) NEW_LINE DEDENT DEDENT while len ( s2 ) != 0 : NEW_LINE INDENT print ( s2 . pop ( ) . data , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 10 ) NEW_LINE root . left . left . right = Node ( 11 ) NEW_LINE root . right . right . left = Node ( 8 ) NEW_LINE PrintLeafLeftToRight ( root ) NEW_LINE DEDENT


public class Linked_list_2D_Matrix { static class Node { int data ; Node right ; Node down ; } ; static Node construct ( int arr [ ] [ ] , int i , int j , int m , int n ) { if ( i > n - 1 || j > m - 1 ) return null ; Node temp = new Node ( ) ; temp . data = arr [ i ] [ j ] ; temp . right = construct ( arr , i , j + 1 , m , n ) ; temp . down = construct ( arr , i + 1 , j , m , n ) ; return temp ; } static void display ( Node head ) { Node Rp ; Node Dp = head ; while ( Dp != null ) { Rp = Dp ; while ( Rp != null ) { System . out . print ( Rp . data + " ▁ " ) ; Rp = Rp . right ; } System . out . println ( ) ; Dp = Dp . down ; } } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int m = 3 , n = 3 ; Node head = construct ( arr , 0 , 0 , m , n ) ; display ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . down = None NEW_LINE DEDENT DEDENT def construct ( arr , i , j , m , n ) : NEW_LINE INDENT if ( i > n - 1 or j > m - 1 ) : NEW_LINE INDENT return None NEW_LINE DEDENT temp = Node ( arr [ i ] [ j ] ) NEW_LINE temp . data = arr [ i ] [ j ] NEW_LINE temp . right = construct ( arr , i , j + 1 , m , n ) NEW_LINE temp . down = construct ( arr , i + 1 , j , m , n ) NEW_LINE return temp NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT Dp = head NEW_LINE while ( Dp ) : NEW_LINE INDENT Rp = Dp NEW_LINE while ( Rp ) : NEW_LINE INDENT print ( Rp . data , end = " ▁ " ) NEW_LINE Rp = Rp . right NEW_LINE DEDENT print ( ) NEW_LINE Dp = Dp . down NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE m , n = 3 , 3 NEW_LINE head = construct ( arr , 0 , 0 , m , n ) NEW_LINE display ( head ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } public static void main ( String args [ ] ) throws IOException { System . out . println ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }

def unitDigitXRaisedY ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( y ) : NEW_LINE INDENT res = ( res * x ) % 10 NEW_LINE DEDENT return res NEW_LINE DEDENT print ( unitDigitXRaisedY ( 4 , 2 ) ) NEW_LINE


import java . util . * ; public class GFG { static long getChicks ( int n ) { int size = Math . max ( n , 7 ) ; long [ ] dp = new long [ size ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < 6 ; i ++ ) { dp [ i ] = dp [ i - 1 ] * 3 ; } dp [ 6 ] = 726 ; for ( int i = 8 ; i <= n ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] / 3 ) ) * 3 ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( getChicks ( n ) ) ; } }

def getChicks ( n ) : NEW_LINE INDENT size = max ( n , 7 ) ; NEW_LINE dp = [ 0 ] * size ; NEW_LINE dp [ 0 ] = 0 ; NEW_LINE dp [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , 7 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] * 3 ; NEW_LINE DEDENT dp [ 6 ] = 726 ; NEW_LINE for i in range ( 8 , n + 1 ) : NEW_LINE INDENT dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] // 3 ) ) * 3 ; NEW_LINE DEDENT return dp [ n ] ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( getChicks ( n ) ) ; NEW_LINE


public class GFG { static int findOnBulbs ( int numberOfBulbs ) { int onBulbs = 0 ; int bulb = 1 ; int person = 1 ; for ( bulb = 1 ; bulb <= numberOfBulbs ; bulb ++ ) { int factors = 0 ; for ( person = 1 ; person * person <= numberOfBulbs ; person ++ ) { if ( bulb % person == 0 ) { factors ++ ; if ( bulb / person != person ) { factors ++ ; } } } if ( factors % 2 == 1 ) { System . out . println ( " Light ▁ bulb ▁ " + bulb + " ▁ will ▁ be ▁ on " ) ; onBulbs ++ ; } } return onBulbs ; } public static void main ( String [ ] args ) { int numberOfBulbs = 1000 ; int onBulbs = findOnBulbs ( numberOfBulbs ) ; System . out . println ( " Total ▁ " + onBulbs + " ▁ light ▁ bulbs ▁ will ▁ be ▁ on ▁ in ▁ the ▁ end ▁ out ▁ of ▁ " + numberOfBulbs + " ▁ light ▁ bulbs " ) ; } }

def findOnBulbs ( numberOfBulbs ) : NEW_LINE INDENT onBulbs = 0 NEW_LINE bulb = 1 NEW_LINE person = 1 NEW_LINE for bulb in range ( 1 , numberOfBulbs + 1 ) : NEW_LINE INDENT factors = 0 NEW_LINE for person in range ( 1 , int ( numberOfBulbs ** ( 0.5 ) ) + 1 ) : NEW_LINE INDENT if bulb % person == 0 : NEW_LINE INDENT factors += 1 NEW_LINE if bulb // person != person : NEW_LINE INDENT factors += 1 NEW_LINE DEDENT DEDENT DEDENT if factors % 2 == 1 : NEW_LINE INDENT print ( " Light ▁ bulb " , bulb , " will ▁ be ▁ on " ) NEW_LINE onBulbs += 1 NEW_LINE DEDENT DEDENT return onBulbs NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT numberOfBulbs = 1000 NEW_LINE onBulbs = findOnBulbs ( numberOfBulbs ) NEW_LINE print ( " Total " , onBulbs , " light ▁ bulbs ▁ will " , " be ▁ on ▁ in ▁ the ▁ end ▁ out ▁ of " , numberOfBulbs , " light ▁ bulbs " ) NEW_LINE DEDENT


class GFG { final static int fact [ ] = new int [ 21 ] ; static void preCompute ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < 18 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) ; } static void nextFactorial ( int N ) { for ( int i = 0 ; i < 21 ; i ++ ) { if ( N < fact [ i ] ) { System . out . println ( fact [ i ] ) ; break ; } } } public static void main ( String [ ] args ) { preCompute ( ) ; int N = 120 ; nextFactorial ( N ) ; } }

fact = [ 0 ] * 21 NEW_LINE def preCompute ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , 18 ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) NEW_LINE DEDENT DEDENT def nextFactorial ( N ) : NEW_LINE INDENT for i in range ( 21 ) : NEW_LINE INDENT if N < fact [ i ] : NEW_LINE INDENT print ( fact [ i ] ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT preCompute ( ) NEW_LINE N = 120 NEW_LINE nextFactorial ( N ) NEW_LINE


class GFG { static int sticks [ ] = { 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 } ; static int number [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch >= ' A ' && ch <= ' Z ' ) { cnt += sticks [ ch - ' A ' ] ; } else { cnt += number [ ch - '0' ] ; } } return cnt ; } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; System . out . println ( countSticks ( str ) ) ; } }

sticks = [ 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 ] ; NEW_LINE number = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; NEW_LINE def countSticks ( string ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT ch = string [ i ] ; NEW_LINE if ( ch >= ' A ' and ch <= ' Z ' ) : NEW_LINE INDENT cnt += sticks [ ord ( ch ) - ord ( ' A ' ) ] ; NEW_LINE DEDENT else : NEW_LINE INDENT cnt += number [ ord ( ch ) - ord ( '0' ) ] ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GEEKSFORGEEKS " ; NEW_LINE print ( countSticks ( string ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static long centeredTridecagonalNum ( long n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; n = 10 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; } }

def centeredTridecagonalNum ( n ) : NEW_LINE INDENT return ( 13 * n * ( n - 1 ) + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE n = 10 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; import java . io . * ; class GFG { static int closetAND ( int arr [ ] , int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 10 } ; int n = arr . length ; int k = 2 ; System . out . println ( closetAND ( arr , n , k ) ) ; } }

def closetAND ( arr , n , k ) : NEW_LINE INDENT ans = 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT X = arr [ i ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT X &= arr [ j ] NEW_LINE ans = min ( ans , abs ( k - X ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 7 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 ; NEW_LINE print ( closetAND ( arr , n , k ) ) NEW_LINE


class GFG { static int summation ( int n ) { int abs_sum = n * ( n + 1 ) / 2 ; int sign = n + 1 % 2 == 0 ? 1 : - 1 ; int result_sum = sign * abs_sum ; return result_sum ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( summation ( N ) ) ; } }

def summation ( n ) : NEW_LINE INDENT abs_sum = n * ( n + 1 ) // 2 ; NEW_LINE sign = 1 if ( ( n + 1 ) % 2 == 0 ) else - 1 ; NEW_LINE result_sum = sign * abs_sum ; NEW_LINE return result_sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( summation ( N ) ) ; NEW_LINE DEDENT


class GFG { static int longest_substring ( String s , int k ) { int n = s . length ( ) ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( i < n && s . charAt ( i ) == '0' ) { x ++ ; i ++ ; } ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 || ans != n ) return ans ; else return ( ans / 2 ) * k ; } public static void main ( String [ ] args ) { String s = "010001000" ; int k = 4 ; System . out . println ( longest_substring ( s , k ) ) ; } }

def longest_substring ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( k > 1 ) : NEW_LINE INDENT s += s NEW_LINE n *= 2 NEW_LINE DEDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( i < n and s [ i ] == '0' ) : NEW_LINE INDENT x , i = x + 1 , i + 1 NEW_LINE DEDENT ans = max ( ans , x ) NEW_LINE i += 1 NEW_LINE DEDENT if ( k == 1 or ans != n ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans // 2 ) * k NEW_LINE DEDENT DEDENT s = "010001000" NEW_LINE k = 4 NEW_LINE print ( longest_substring ( s , k ) ) NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node head ; static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; head = head_ref ; } static int productOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; Stack < Integer > st = new Stack < Integer > ( ) ; int prod = 1 ; while ( head != null ) { st . push ( head . data ) ; head = head . next ; } while ( n -- > 0 ) { prod *= st . peek ( ) ; st . pop ( ) ; } return prod ; } public static void main ( String [ ] args ) { head = null ; push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; int n = 2 ; System . out . println ( productOfLastN_NodesUtil ( head , n ) ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT head = None NEW_LINE def push ( head_ref , new_data ) : NEW_LINE INDENT global head NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE head = head_ref NEW_LINE DEDENT def productOfLastN_NodesUtil ( head , n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT st = [ ] NEW_LINE prod = 1 NEW_LINE while ( head != None ) : NEW_LINE INDENT st . append ( head . data ) NEW_LINE head = head . next NEW_LINE DEDENT while ( n > 0 ) : NEW_LINE INDENT n = n - 1 NEW_LINE prod *= st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT return prod NEW_LINE DEDENT head = None NEW_LINE push ( head , 12 ) NEW_LINE push ( head , 4 ) NEW_LINE push ( head , 8 ) NEW_LINE push ( head , 6 ) NEW_LINE push ( head , 10 ) NEW_LINE n = 2 NEW_LINE print ( productOfLastN_NodesUtil ( head , n ) ) NEW_LINE


import java . util . * ; class GFG { static int subsetSum ( int arr [ ] , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . length ; System . out . print ( subsetSum ( arr , n , 0 , sum , 0 ) ) ; } }

def subsetSum ( arr , n , i , sum , count ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) NEW_LINE count = subsetSum ( arr , n , i + 1 , sum , count ) NEW_LINE return count NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE sum = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( subsetSum ( arr , n , 0 , sum , 0 ) ) NEW_LINE


class Test { static int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ; static int largest ( ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } public static void main ( String [ ] args ) { System . out . println ( " Largest ▁ in ▁ given ▁ array ▁ is ▁ " + largest ( ) ) ; } }

def largest ( arr , n ) : NEW_LINE INDENT return ( max ( arr ) ) NEW_LINE DEDENT arr = [ 10 , 324 , 45 , 90 , 9808 ] NEW_LINE n = len ( arr ) NEW_LINE Ans = largest ( arr , n ) NEW_LINE print ( " Largest ▁ in ▁ given ▁ array ▁ is " , Ans ) NEW_LINE


class GFG { static long factorial ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } static void printNComposite ( int n ) { long fact = factorial ( n + 1 ) ; for ( int i = 2 ; i <= n + 1 ; ++ i ) { System . out . print ( fact + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printNComposite ( n ) ; } }

def factorial ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def printNComposite ( n ) : NEW_LINE INDENT fact = factorial ( n + 1 ) ; NEW_LINE for i in range ( 2 , n + 2 ) : NEW_LINE INDENT print ( fact + i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE printNComposite ( n ) ; NEW_LINE


class GFG { static boolean isPower ( int n ) { for ( int x = 2 ; x <= Math . sqrt ( n ) ; x ++ ) { int p = x ; while ( p <= n ) { p = p * x ; if ( p == n ) return true ; } } return false ; } public static void main ( String [ ] args ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) System . out . print ( i + " ▁ " ) ; } }

import math NEW_LINE def isPower ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for x in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT p = x NEW_LINE while ( p <= n ) : NEW_LINE INDENT p = p * x NEW_LINE if ( p == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if ( isPower ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . awt . Point ; import static java . lang . Math . PI ; import static java . lang . Math . sqrt ; import static java . lang . Math . acos ; class Test { static int lengthSquare ( Point p1 , Point p2 ) { int xDiff = p1 . x - p2 . x ; int yDiff = p1 . y - p2 . y ; return xDiff * xDiff + yDiff * yDiff ; } static void printAngle ( Point A , Point B , Point C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = ( float ) sqrt ( a2 ) ; float b = ( float ) sqrt ( b2 ) ; float c = ( float ) sqrt ( c2 ) ; float alpha = ( float ) acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = ( float ) acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = ( float ) acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = ( float ) ( alpha * 180 / PI ) ; betta = ( float ) ( betta * 180 / PI ) ; gamma = ( float ) ( gamma * 180 / PI ) ; System . out . println ( " alpha ▁ : ▁ " + alpha ) ; System . out . println ( " betta ▁ : ▁ " + betta ) ; System . out . println ( " gamma ▁ : ▁ " + gamma ) ; } public static void main ( String [ ] args ) { Point A = new Point ( 0 , 0 ) ; Point B = new Point ( 0 , 1 ) ; Point C = new Point ( 1 , 0 ) ; printAngle ( A , B , C ) ; } }

import math NEW_LINE def lengthSquare ( X , Y ) : NEW_LINE INDENT xDiff = X [ 0 ] - Y [ 0 ] NEW_LINE yDiff = X [ 1 ] - Y [ 1 ] NEW_LINE return xDiff * xDiff + yDiff * yDiff NEW_LINE DEDENT def printAngle ( A , B , C ) : NEW_LINE INDENT a2 = lengthSquare ( B , C ) NEW_LINE b2 = lengthSquare ( A , C ) NEW_LINE c2 = lengthSquare ( A , B ) NEW_LINE a = math . sqrt ( a2 ) ; NEW_LINE b = math . sqrt ( b2 ) ; NEW_LINE c = math . sqrt ( c2 ) ; NEW_LINE alpha = math . acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; NEW_LINE betta = math . acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; NEW_LINE gamma = math . acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; NEW_LINE alpha = alpha * 180 / math . pi ; NEW_LINE betta = betta * 180 / math . pi ; NEW_LINE gamma = gamma * 180 / math . pi ; NEW_LINE print ( " alpha ▁ : ▁ % f " % ( alpha ) ) NEW_LINE print ( " betta ▁ : ▁ % f " % ( betta ) ) NEW_LINE print ( " gamma ▁ : ▁ % f " % ( gamma ) ) NEW_LINE DEDENT A = ( 0 , 0 ) NEW_LINE B = ( 0 , 1 ) NEW_LINE C = ( 1 , 0 ) NEW_LINE printAngle ( A , B , C ) ; NEW_LINE


import java . util . Deque ; import java . util . LinkedList ; public class SlidingWindow { static void printMax ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < Integer > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ! Qi . isEmpty ( ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } for ( ; i < n ; ++ i ) { System . out . print ( arr [ Qi . peek ( ) ] + " ▁ " ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peek ( ) <= i - k ) Qi . removeFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } System . out . print ( arr [ Qi . peek ( ) ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int k = 3 ; printMax ( arr , arr . length , k ) ; } }

from collections import deque NEW_LINE def printMax ( arr , n , k ) : NEW_LINE INDENT Qi = deque ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) ; NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( str ( arr [ Qi [ 0 ] ] ) + " ▁ " , end = " " ) NEW_LINE while Qi and Qi [ 0 ] <= i - k : NEW_LINE INDENT Qi . popleft ( ) NEW_LINE DEDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) NEW_LINE DEDENT print ( str ( arr [ Qi [ 0 ] ] ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 1 , 78 , 90 , 57 , 89 , 56 ] NEW_LINE k = 3 NEW_LINE printMax ( arr , len ( arr ) , k ) NEW_LINE DEDENT


class GFG { static int findSmallNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } static int findLargeNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } static int sumDivisibles ( int A , int B , int M ) { int first = findSmallNum ( A , M ) ; int last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; int n = ( B / M ) - ( A - 1 ) / M ; return n * ( first + last ) / 2 ; } public static void main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; System . out . print ( sumDivisibles ( A , B , M ) ) ; } }

def findSmallNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT def findLargeNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N + K - rem NEW_LINE DEDENT DEDENT def sumDivisibles ( A , B , M ) : NEW_LINE INDENT sum = 0 NEW_LINE first = findSmallNum ( A , M ) NEW_LINE last = findLargeNum ( B , M ) NEW_LINE if ( first < A ) : NEW_LINE INDENT first += M NEW_LINE DEDENT if ( last > B ) : NEW_LINE INDENT first -= M NEW_LINE DEDENT n = ( B // M ) - ( A - 1 ) // M NEW_LINE return n * ( first + last ) // 2 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


class GfG { static boolean isDefeat ( String s1 , String s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) || ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) ) continue ; else if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == ' Z ' ) || ( s1 . charAt ( i ) == ' Z ' && s2 . charAt ( i ) == '0' ) ) continue ; else { return true ; } } return false ; } public static void main ( String [ ] args ) { String s1 = ( "01001101ZZ " ) ; String s2 = ( "10Z1001000" ) ; int n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) System . out . println ( " Defeat " ) ; else System . out . println ( " Victory " ) ; } }

def isDefeat ( s1 , s2 , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( ( s1 [ i ] == '0' and s2 [ i ] == '1' ) or ( s1 [ i ] == '1' and s2 [ i ] == '0' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( ( s1 [ i ] == '0' and s2 [ i ] == ' Z ' ) or ( s1 [ i ] == ' Z ' and s2 [ i ] == '0' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s1 = "01001101ZZ " NEW_LINE s2 = "10Z1001000" NEW_LINE n = 10 NEW_LINE if ( isDefeat ( s1 , s2 , n ) ) : NEW_LINE INDENT print ( " Defeat " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Victory " ) NEW_LINE DEDENT


public class GFG { static final int NO_OF_CHARS = 256 ; static void printDistinct ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) != ' ▁ ' ) count [ ( int ) str . charAt ( i ) ] ++ ; int n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ ( int ) str . charAt ( i ) ] == 1 ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks " ; printDistinct ( str ) ; } }

NO_OF_CHARS = 256 NEW_LINE def printDistinct ( str ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] != ' ▁ ' ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT DEDENT n = i NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( count [ ord ( str [ i ] ) ] == 1 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " GeeksforGeeks " NEW_LINE printDistinct ( str ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int divisor ( int a ) { int div = 1 , count = 0 ; for ( int i = 2 ; i <= Math . sqrt ( a ) ; i ++ ) { while ( a % i == 0 ) { count ++ ; a = a / i ; } div = div * ( count + 1 ) ; count = 0 ; } if ( a > 1 ) { div = div * ( 2 ) ; } return div ; } static int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = divisor ( i ) ; if ( divCount % 2 == 1 ) { ++ res ; } } return res ; } public static void main ( String [ ] args ) { int a = 1 , b = 10 ; System . out . println ( OddDivCount ( a , b ) ) ; } }

def divisor ( a ) : NEW_LINE INDENT div = 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 2 , int ( pow ( a , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT while ( a % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE a = a / i ; NEW_LINE DEDENT div = div * ( count + 1 ) ; NEW_LINE count = 0 ; NEW_LINE DEDENT if ( a > 1 ) : NEW_LINE INDENT div = div * ( 2 ) ; NEW_LINE DEDENT return div ; NEW_LINE DEDENT def OddDivCount ( a , b ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT divCount = divisor ( i ) ; NEW_LINE if ( divCount % 2 ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b = 1 , 10 ; NEW_LINE print ( OddDivCount ( a , b ) ) ; NEW_LINE DEDENT


class GFG { static final int BITS = 32 ; static int andSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < BITS ; i ++ ) { int countSetBits = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countSetBits ++ ; } int subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int size = 3 ; System . out . println ( andSum ( arr , size ) ) ; } }

BITS = 32 ; NEW_LINE def andSum ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , BITS ) : NEW_LINE INDENT countSetBits = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT countSetBits = ( countSetBits + 1 ) NEW_LINE DEDENT DEDENT subset = ( ( 1 << countSetBits ) - 1 ) NEW_LINE subset = ( subset * ( 1 << i ) ) NEW_LINE ans = ans + subset NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE size = len ( arr ) NEW_LINE print ( andSum ( arr , size ) ) NEW_LINE


import java . io . * ; class GFG { static void centeredHexagonalSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( 3 * i * ( i - 1 ) + 1 + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; centeredHexagonalSeries ( n ) ; } }

def centeredHexagonalSeries ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( 3 * i * ( i - 1 ) + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE centeredHexagonalSeries ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; if ( ( ( n + 1 ) & n ) == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( areAllBitsSet ( n ) ) ; } }

def areAllBitsSet ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT n = 7 NEW_LINE print ( areAllBitsSet ( n ) ) NEW_LINE


class Solution { static final int max_val = 257 ; static int PrimeCharacters ( String s ) { boolean prime [ ] = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( prime [ ( int ) ( s . charAt ( i ) ) ] ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " geeksforgeeks " ; System . out . print ( PrimeCharacters ( S ) ) ; } }

from math import sqrt NEW_LINE max_val = 257 NEW_LINE def PrimeCharacters ( s ) : NEW_LINE INDENT prime = [ True ] * ( max_val + 1 ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( prime [ ord ( s [ i ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " ; NEW_LINE print ( PrimeCharacters ( S ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String tidyNum ( String str1 , int len ) { char [ ] str = str1 . toCharArray ( ) ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = "11333445538" ; int len = str . length ( ) ; System . out . println ( tidyNum ( str , len ) ) ; } }

def tidyNum ( str , len ) : NEW_LINE INDENT for i in range ( len - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] > str [ i + 1 ] ) : NEW_LINE INDENT str [ i ] -= 1 NEW_LINE for j in range ( i + 1 , len ) : NEW_LINE INDENT str [ j ] = 9 NEW_LINE DEDENT DEDENT DEDENT return str NEW_LINE DEDENT str = [ 1 , 1 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 3 , 8 ] NEW_LINE len = len ( str ) NEW_LINE num = tidyNum ( str , len ) NEW_LINE for i in range ( 0 , len ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT


class GFG { static int find_sub ( String s , int k ) { int len = s . length ( ) ; int lp = 0 , rp = 0 ; int ans = 0 ; int [ ] hash_char = new int [ 26 ] ; for ( ; rp < len ; rp ++ ) { hash_char [ s . charAt ( rp ) - ' a ' ] ++ ; while ( hash_char [ s . charAt ( rp ) - ' a ' ] > k ) { hash_char [ s . charAt ( lp ) - ' a ' ] -- ; lp ++ ; } ans += rp - lp + 1 ; } return ans ; } public static void main ( String [ ] args ) { String S = " aaabb " ; int k = 2 ; System . out . println ( find_sub ( S , k ) ) ; } }

def find_sub ( s , k ) : NEW_LINE INDENT Len = len ( s ) NEW_LINE lp , rp = 0 , 0 NEW_LINE ans = 0 NEW_LINE hash_char = [ 0 for i in range ( 256 ) ] NEW_LINE for rp in range ( Len ) : NEW_LINE INDENT hash_char [ ord ( s [ rp ] ) ] += 1 NEW_LINE while ( hash_char [ ord ( s [ rp ] ) ] > k ) : NEW_LINE INDENT hash_char [ ord ( s [ lp ] ) ] -= 1 NEW_LINE lp += 1 NEW_LINE DEDENT ans += rp - lp + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " aaabb " NEW_LINE k = 2 ; NEW_LINE print ( find_sub ( s , k ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX_CHAR = 256 ; static int countSubStrings ( String s , String anotherStr , int k ) { boolean illegal [ ] = new boolean [ MAX_CHAR ] ; for ( int i = 0 ; i < anotherStr . length ( ) ; i ++ ) { illegal [ anotherStr . charAt ( i ) ] = true ; } HashSet < String > us = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { String ss = " " ; int count = 0 ; for ( int j = i ; j < s . length ( ) ; ++ j ) { if ( illegal [ s . charAt ( j ) ] ) { ++ count ; } ss = ss + s . charAt ( j ) ; if ( count <= k ) { us . add ( ss ) ; } else { break ; } } } return us . size ( ) ; } public static void main ( String [ ] args ) { String str = " acbacbacaa " ; String anotherStr = " abcdefghijklmnopqrstuvwxyz " ; int k = 2 ; System . out . println ( countSubStrings ( str , anotherStr , k ) ) ; } }

MAX_CHAR = 256 NEW_LINE def countSubStrings ( s , anotherStr , k ) : NEW_LINE INDENT illegal = [ False ] * MAX_CHAR NEW_LINE for i in range ( len ( anotherStr ) ) : NEW_LINE INDENT illegal [ ord ( anotherStr [ i ] ) ] = True NEW_LINE DEDENT us = set ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ss = " " NEW_LINE count = 0 NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT if ( illegal [ ord ( s [ j ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ss = ss + s [ j ] NEW_LINE if ( count <= k ) : NEW_LINE INDENT us . add ( ss ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return len ( us ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " acbacbacaa " NEW_LINE anotherStr = " abcdefghijklmnopqrstuvwxyz " NEW_LINE k = 2 NEW_LINE print ( countSubStrings ( string , anotherStr , k ) ) NEW_LINE DEDENT


class GFG { static int Count ( String s , int pos ) { int c = s . charAt ( pos - 1 ) ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s . charAt ( i ) == c ) counter = counter + 1 ; } return counter ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . length ( ) ; int query [ ] = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; System . out . println ( Count ( s , pos ) ) ; } } }

def Count ( s , pos ) : NEW_LINE INDENT c = s [ pos - 1 ] NEW_LINE counter = 0 NEW_LINE for i in range ( pos - 1 ) : NEW_LINE INDENT if s [ i ] == c : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abacsddaa " NEW_LINE n = len ( s ) NEW_LINE query = [ 9 , 3 , 2 ] NEW_LINE Q = len ( query ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT pos = query [ i ] NEW_LINE print ( Count ( s , pos ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void recaman ( int n ) { if ( n <= 0 ) return ; System . out . printf ( " % d , ▁ " , 0 ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ; s . add ( curr ) ; System . out . printf ( " % d , ▁ " , curr ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }

def recaman ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( 0 , " , " , end = ' ' ) NEW_LINE s = set ( [ ] ) NEW_LINE s . add ( 0 ) NEW_LINE prev = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = prev - i NEW_LINE if ( curr < 0 or curr in s ) : NEW_LINE INDENT curr = prev + i NEW_LINE DEDENT s . add ( curr ) NEW_LINE print ( curr , " , " , end = ' ' ) NEW_LINE prev = curr NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxProduct ( int a [ ] , int n ) { int [ ] product = new int [ n ] ; int maxA [ ] = new int [ n ] ; int maxProd = 0 ; int maxArr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { product [ i ] = a [ ( i + 1 ) % n ] * a [ ( i + ( n - 1 ) ) % n ] ; if ( maxProd < product [ i ] ) { maxProd = product [ i ] ; } } return maxProd ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 6 , 4 , 3 , 2 } ; int n = a . length ; System . out . println ( maxProduct ( a , n ) ) ; } }

def maxProduct ( a , n ) : NEW_LINE INDENT product = [ 0 ] * n ; NEW_LINE maxA = [ 0 ] * n ; NEW_LINE maxProd = 0 ; NEW_LINE maxArr = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT product [ i ] = a [ ( i + 1 ) % n ] * a [ ( i + ( n - 1 ) ) % n ] ; NEW_LINE if ( maxProd < product [ i ] ) : NEW_LINE INDENT maxProd = product [ i ] ; NEW_LINE DEDENT DEDENT return maxProd ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 6 , 4 , 3 , 2 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( maxProduct ( a , n ) ) ; NEW_LINE DEDENT


class GFG { static int maxIncSubarr ( int a [ ] , int n ) { int pre [ ] = new int [ n ] ; int pos [ ] = new int [ n ] ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } int ans = 0 ; l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = Math . max ( ans , l ) ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = Math . max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int n = arr . length ; System . out . println ( maxIncSubarr ( arr , n ) ) ; } }

def maxIncSubarr ( a , n ) : NEW_LINE INDENT pre = [ 0 ] * n ; NEW_LINE pos = [ 0 ] * n ; NEW_LINE pre [ 0 ] = 1 ; NEW_LINE pos [ n - 1 ] = 1 ; NEW_LINE l = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pre [ i ] = 1 ; NEW_LINE DEDENT DEDENT l = 1 ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT pos [ i ] = pos [ i + 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos [ i ] = 1 ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE l = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l = 1 ; NEW_LINE DEDENT ans = max ( ans , l ) ; NEW_LINE DEDENT for i in range ( 1 , n - 3 ) : NEW_LINE INDENT if ( a [ i - 1 ] < a [ i + 1 ] ) : NEW_LINE INDENT ans = max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxIncSubarr ( arr , n ) ) ; NEW_LINE DEDENT


public class NthNodePostOrder { static int flag = 0 ; public static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) System . out . print ( root . data ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } } class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ;

class createNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT flag = [ 0 ] NEW_LINE def NthPostordernode ( root , N ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( flag [ 0 ] <= N [ 0 ] ) : NEW_LINE INDENT NthPostordernode ( root . left , N ) NEW_LINE NthPostordernode ( root . right , N ) NEW_LINE flag [ 0 ] += 1 NEW_LINE if ( flag [ 0 ] == N [ 0 ] ) : NEW_LINE INDENT print ( root . data ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = createNode ( 25 ) NEW_LINE root . left = createNode ( 20 ) NEW_LINE root . right = createNode ( 30 ) NEW_LINE root . left . left = createNode ( 18 ) NEW_LINE root . left . right = createNode ( 22 ) NEW_LINE root . right . left = createNode ( 24 ) NEW_LINE root . right . right = createNode ( 32 ) NEW_LINE N = [ 6 ] NEW_LINE NthPostordernode ( root , N ) NEW_LINE DEDENT


class GFG { static int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input > 0 || unlock_code > 0 ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( input_digit - code_digit ) ) ; input /= 10 ; unlock_code /= 10 ; } return rotation ; } public static void main ( String [ ] args ) { int input = 28756 ; int unlock_code = 98234 ; System . out . println ( " Minimum ▁ Rotation ▁ = ▁ " + minRotation ( input , unlock_code ) ) ; } }

def minRotation ( input , unlock_code ) : NEW_LINE INDENT rotation = 0 ; NEW_LINE while ( input > 0 or unlock_code > 0 ) : NEW_LINE INDENT input_digit = input % 10 ; NEW_LINE code_digit = unlock_code % 10 ; NEW_LINE rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; NEW_LINE input = int ( input / 10 ) ; NEW_LINE unlock_code = int ( unlock_code / 10 ) ; NEW_LINE DEDENT return rotation ; NEW_LINE DEDENT input = 28756 ; NEW_LINE unlock_code = 98234 ; NEW_LINE print ( " Minimum ▁ Rotation ▁ = " , minRotation ( input , unlock_code ) ) ; NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int sum = 0 ; int i ; int pivot = findPivot ( arr , n ) ; int diff = n - 1 - pivot ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + ( ( i + diff ) % n ) * arr [ i ] ; } return sum ; } static int findPivot ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i + 1 ) % n ] ) return i ; } return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; int max = maxSum ( arr , n ) ; System . out . println ( max ) ; } }

def maxSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE pivot = findPivot ( arr , n ) NEW_LINE diff = n - 1 - pivot NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( ( i + diff ) % n ) * arr [ i ] ; NEW_LINE DEDENT return sum NEW_LINE DEDENT def findPivot ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ ( i + 1 ) % n ] ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 3 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE max = maxSum ( arr , n ) NEW_LINE print ( max ) NEW_LINE DEDENT


class GFG { static int countOrderedPairs ( int N ) { int count_pairs = 0 ; for ( int i = 1 ; i <= ( int ) Math . sqrt ( N - 1 ) ; ++ i ) { for ( int j = i ; j * i < N ; ++ j ) ++ count_pairs ; } count_pairs *= 2 ; count_pairs -= ( int ) ( Math . sqrt ( N - 1 ) ) ; return count_pairs ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( countOrderedPairs ( N ) ) ; } }

from math import sqrt NEW_LINE def countOrderedPairs ( N ) : NEW_LINE INDENT count_pairs = 0 NEW_LINE p = int ( sqrt ( N - 1 ) ) + 1 NEW_LINE q = int ( sqrt ( N ) ) + 2 NEW_LINE for i in range ( 1 , p , 1 ) : NEW_LINE INDENT for j in range ( i , q , 1 ) : NEW_LINE INDENT count_pairs += 1 NEW_LINE DEDENT DEDENT count_pairs *= 2 NEW_LINE count_pairs -= int ( sqrt ( N - 1 ) ) NEW_LINE return count_pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( countOrderedPairs ( N ) ) NEW_LINE DEDENT


class GFG { static boolean checkFibinnary ( int n ) { int prev_last = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 && prev_last != 0 ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; if ( checkFibinnary ( n ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def checkFibinnary ( n ) : NEW_LINE INDENT prev_last = 0 NEW_LINE while ( n ) : NEW_LINE INDENT if ( ( n & 1 ) and prev_last ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev_last = n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 10 NEW_LINE if ( checkFibinnary ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX_INDEX = 51 ; static int MAX_SUM = 2505 ; static int [ ] [ ] [ ] dp = new int [ MAX_INDEX ] [ MAX_SUM ] [ MAX_INDEX ] ; static int waysutil ( int index , int sum , int count , Vector < Integer > arr , int K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } int remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } int average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index ] [ sum ] [ count ] != - 1 ) { return dp [ index ] [ sum ] [ count ] ; } int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr . get ( index ) , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index ] [ sum ] [ count ] = total ; return total ; } static int ways ( int N , int K , int [ ] arr ) { Vector < Integer > Arr = new Vector < > ( ) ; Arr . add ( - 1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . add ( arr [ i ] ) ; } for ( int i = 0 ; i < MAX_INDEX ; i ++ ) { for ( int j = 0 ; j < MAX_SUM ; j ++ ) { for ( int l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i ] [ j ] [ l ] = - 1 ; } } } int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 5 ; System . out . println ( ways ( N , K , arr ) ) ; } }

import numpy as np NEW_LINE MAX_INDEX = 51 NEW_LINE MAX_SUM = 2505 NEW_LINE dp = np . ones ( ( MAX_INDEX , MAX_SUM , MAX_INDEX ) ) * - 1 ; NEW_LINE def waysutil ( index , sum , count , arr , K ) : NEW_LINE INDENT if ( index < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( index == 0 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT remainder = sum % count ; NEW_LINE if ( remainder != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT average = sum // count ; NEW_LINE if ( average == K ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT if ( dp [ index ] [ sum ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ sum ] [ count ] ; NEW_LINE DEDENT dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; NEW_LINE pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; NEW_LINE total = pick + dontpick ; NEW_LINE dp [ index ] [ sum ] [ count ] = total ; NEW_LINE return total ; NEW_LINE DEDENT def ways ( N , K , arr ) : NEW_LINE INDENT Arr = [ ] ; NEW_LINE Arr . append ( - 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT Arr . append ( arr [ i ] ) ; NEW_LINE DEDENT answer = waysutil ( N , 0 , 0 , Arr , K ) ; NEW_LINE return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 5 ; NEW_LINE print ( ways ( N , K , arr ) ) ; NEW_LINE DEDENT


class GFG { static boolean check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { return true ; } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; if ( check ( arr , n ) ) { System . out . println ( " Odd ▁ Bit - wise ▁ OR " ) ; } else { System . out . println ( " Even ▁ Bit - wise ▁ OR " ) ; } } }

def check ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] & 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE if check ( arr , n ) : NEW_LINE INDENT print ( " Odd ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT DEDENT


class GFG { static final int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 204 , b = 240 ; if ( areAnagrams ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

TEN = 10 NEW_LINE def updateFreq ( n , freq ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT digit = n % TEN NEW_LINE freq [ digit ] += 1 NEW_LINE n //= TEN NEW_LINE DEDENT DEDENT def areAnagrams ( a , b ) : NEW_LINE INDENT freqA = [ 0 ] * TEN NEW_LINE freqB = [ 0 ] * TEN NEW_LINE updateFreq ( a , freqA ) NEW_LINE updateFreq ( b , freqB ) NEW_LINE for i in range ( TEN ) : NEW_LINE INDENT if ( freqA [ i ] != freqB [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = 240 NEW_LINE b = 204 NEW_LINE if ( areAnagrams ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printArray ( int N , int SUM , int K ) { int minSum = ( N * ( N + 1 ) ) / 2 ; int maxSum = ( N * K ) - ( N * ( N - 1 ) ) / 2 ; if ( minSum > SUM || maxSum < SUM ) { System . out . println ( " Not ▁ Possible " ) ; return ; } int arr [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i ] = i ; int sum = minSum ; for ( int i = N ; i >= 1 ; i -- ) { int x = sum + ( K - i ) ; if ( x < SUM ) { sum = sum + ( K - i ) ; arr [ i ] = K ; K -- ; } else { arr [ i ] += ( SUM - sum ) ; sum = SUM ; break ; } } for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 3 , SUM = 15 , K = 8 ; printArray ( N , SUM , K ) ; } }

def printArray ( N , SUM , K ) : NEW_LINE INDENT minSum = ( N * ( N + 1 ) ) / 2 NEW_LINE maxSum = ( N * K ) - ( N * ( N - 1 ) ) / 2 NEW_LINE if ( minSum > SUM or maxSum < SUM ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT arr = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT arr [ i ] = i NEW_LINE DEDENT sum = minSum NEW_LINE i = N NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT x = sum + ( K - i ) NEW_LINE if ( x < SUM ) : NEW_LINE INDENT sum = sum + ( K - i ) NEW_LINE arr [ i ] = K NEW_LINE K -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] += ( SUM - sum ) NEW_LINE sum = SUM NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( int ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE SUM = 15 NEW_LINE K = 8 NEW_LINE printArray ( N , SUM , K ) NEW_LINE DEDENT


class Gfg { static int arr [ ] = { 1 , 5 , 6 } ; static int countWays ( int N ) { int count [ ] = new int [ N + 1 ] ; count [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 0 ; j < arr . length ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( " Total ▁ number ▁ of ▁ ways ▁ = ▁ " + countWays ( N ) ) ; } }

def countWays ( arr , m , N ) : NEW_LINE INDENT count = [ 0 for i in range ( N + 1 ) ] NEW_LINE count [ 0 ] = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( i >= arr [ j ] ) : NEW_LINE INDENT count [ i ] += count [ i - arr [ j ] ] NEW_LINE DEDENT DEDENT DEDENT return count [ N ] NEW_LINE DEDENT arr = [ 1 , 5 , 6 ] NEW_LINE m = len ( arr ) NEW_LINE N = 7 NEW_LINE print ( " Total ▁ number ▁ of ▁ ways ▁ = ▁ " , countWays ( arr , m , N ) ) NEW_LINE


import java . util . * ; class GFG { static int check ( int n , List < Integer > marks ) { Integer x = Collections . max ( marks ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks . get ( i ) + bonus >= 50 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 5 ; List < Integer > marks = Arrays . asList ( 0 , 21 , 83 , 45 , 64 ) ; System . out . println ( check ( n , marks ) ) ; } }

def check ( n , marks ) : NEW_LINE INDENT x = max ( marks ) NEW_LINE bonus = 100 - x NEW_LINE c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( marks [ i ] + bonus >= 50 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT n = 5 NEW_LINE marks = [ 0 , 21 , 83 , 45 , 64 ] NEW_LINE print ( check ( n , marks ) ) NEW_LINE


import java . io . * ; class GFG { static int aliquotSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( n % i == 0 ) sum += i ; return sum ; } public static void main ( String args [ ] ) throws IOException { int n = 12 ; System . out . println ( aliquotSum ( n ) ) ; } }

def aliquotSum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT sm = sm + i NEW_LINE DEDENT DEDENT return sm NEW_LINE DEDENT n = 12 NEW_LINE print ( aliquotSum ( n ) ) NEW_LINE


class GFG { static int [ ] find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return new int [ ] { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return new int [ ] { mid , n } ; } return find ( dividend , divisor , start , end ) ; } static int [ ] divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } public static void main ( String [ ] args ) { int dividend = 10 , divisor = 3 ; int [ ] ans = divide ( dividend , divisor ) ; System . out . print ( ans [ 0 ] + " , ▁ " ) ; System . out . print ( ans [ 1 ] + " \n " ) ; } }

def find ( dividend , divisor , start , end ) : NEW_LINE INDENT if ( start > end ) : NEW_LINE INDENT return ( 0 , dividend ) ; NEW_LINE DEDENT mid = start + ( end - start ) // 2 ; NEW_LINE n = dividend - divisor * mid ; NEW_LINE if ( n > divisor ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT elif ( n < 0 ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( n == divisor ) : NEW_LINE INDENT mid += 1 ; NEW_LINE n = 0 ; NEW_LINE DEDENT return ( mid , n ) ; NEW_LINE DEDENT return find ( dividend , divisor , start , end ) ; NEW_LINE DEDENT def divide ( dividend , divisor ) : NEW_LINE INDENT return find ( dividend , divisor , 1 , dividend ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT dividend = 10 ; divisor = 3 ; NEW_LINE ans = divide ( dividend , divisor ) ; NEW_LINE print ( ans [ 0 ] , " , ▁ " , ans [ 1 ] ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { int first = Integer . MIN_VALUE ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < second ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }

import sys NEW_LINE def findElements ( arr , n ) : NEW_LINE INDENT first = - sys . maxsize NEW_LINE second = - sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] < second ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , - 6 , 3 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findElements ( arr , n ) NEW_LINE