import java . io . * ; class GFG { static int numberOfDistinct ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { ans ++ ; if ( ( n / i ) != i ) ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( numberOfDistinct ( n ) ) ; } }

import math NEW_LINE def numberOfDistinct ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE if ( ( n // i ) != i ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( numberOfDistinct ( n ) ) NEW_LINE DEDENT


class GFG { static boolean isVowel ( char c ) { return ( c == ' A ' || c == ' E ' || c == ' I ' || c == ' O ' || c == ' U ' || c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ) ; } static String pigLatin ( String s ) { int len = s . length ( ) ; int index = - 1 ; for ( int i = 0 ; i < len ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) { index = i ; break ; } } if ( index == - 1 ) return " - 1" ; return s . substring ( index ) + s . substring ( 0 , index ) + " ay " ; } public static void main ( String [ ] args ) { String str = pigLatin ( " graphic " ) ; if ( str == " - 1" ) System . out . print ( " No ▁ vowels ▁ found . " + " Pig ▁ Latin ▁ not ▁ possible " ) ; else System . out . print ( str ) ; } }

def isVowel ( c ) : NEW_LINE INDENT return ( c == ' A ' or c == ' E ' or c == ' I ' or c == ' O ' or c == ' U ' or c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) ; NEW_LINE DEDENT def pigLatin ( s ) : NEW_LINE INDENT length = len ( s ) ; NEW_LINE index = - 1 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT index = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return " - 1" ; NEW_LINE DEDENT return s [ index : ] + s [ 0 : index ] + " ay " ; NEW_LINE DEDENT str = pigLatin ( " graphic " ) ; NEW_LINE if ( str == " - 1" ) : NEW_LINE INDENT print ( " No ▁ vowels ▁ found . ▁ Pig ▁ Latin ▁ not ▁ possible " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( str ) ; NEW_LINE DEDENT


class GFG { static int countMinSwaps ( String st ) { int min_swaps = 0 ; int odd_0 = 0 , even_0 = 0 ; int odd_1 = 0 , even_1 = 0 ; int n = st . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( st . charAt ( i ) == '1' ) even_1 ++ ; else even_0 ++ ; } else { if ( st . charAt ( i ) == '1' ) odd_1 ++ ; else odd_0 ++ ; } } int cnt_swaps_1 = Math . min ( even_0 , odd_1 ) ; int cnt_swaps_2 = Math . min ( even_1 , odd_0 ) ; return Math . min ( cnt_swaps_1 , cnt_swaps_2 ) ; } public static void main ( String [ ] args ) { String st = "000111" ; System . out . println ( countMinSwaps ( st ) ) ; } }

def countMinSwaps ( st ) : NEW_LINE INDENT min_swaps = 0 NEW_LINE odd_0 , even_0 = 0 , 0 NEW_LINE odd_1 , even_1 = 0 , 0 NEW_LINE n = len ( st ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT if st [ i ] == "1" : NEW_LINE INDENT even_1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even_0 += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if st [ i ] == "1" : NEW_LINE INDENT odd_1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd_0 += 1 NEW_LINE DEDENT DEDENT DEDENT cnt_swaps_1 = min ( even_0 , odd_1 ) NEW_LINE cnt_swaps_2 = min ( even_1 , odd_0 ) NEW_LINE return min ( cnt_swaps_1 , cnt_swaps_2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "000111" NEW_LINE print ( countMinSwaps ( st ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 3 ; static void rotateMatrix ( int mat [ ] [ ] ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j >= 0 ; j -- ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] [ ] mat = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; rotateMatrix ( mat ) ; } }

N = 3 ; NEW_LINE def rotateMatrix ( mat ) : NEW_LINE INDENT i = N - 1 ; NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT j = N - 1 ; NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT print ( ) ; NEW_LINE i = i - 1 ; NEW_LINE DEDENT DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; NEW_LINE rotateMatrix ( mat ) ; NEW_LINE


import java . io . * ; class GFG { static int nCr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int total_ways ( int N , int X ) { return ( nCr ( N - 1 , X - 1 ) + nCr ( N - 1 , X ) ) ; } public static void main ( String [ ] args ) { int N = 5 , X = 3 ; System . out . println ( total_ways ( N , X ) ) ; } }

def nCr ( n , r ) : NEW_LINE INDENT ans = 1 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT ans *= ( n - r + i ) ; NEW_LINE ans //= i ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def total_ways ( N , X ) : NEW_LINE INDENT return ( nCr ( N - 1 , X - 1 ) + nCr ( N - 1 , X ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; X = 3 ; NEW_LINE print ( total_ways ( N , X ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int totalSumDivisibleByNum ( int digit , int number ) { int firstnum = ( int ) Math . pow ( 10 , digit - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; int count = ( ( lastnum - firstnum ) / number + 1 ) ; return ( ( lastnum + firstnum ) * count ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 , number = 7 ; System . out . println ( totalSumDivisibleByNum ( n , number ) ) ; } }

def totalSumDivisibleByNum ( digit , number ) : NEW_LINE INDENT firstnum = pow ( 10 , digit - 1 ) NEW_LINE lastnum = pow ( 10 , digit ) NEW_LINE firstnum = ( firstnum - firstnum % number ) + number NEW_LINE lastnum = ( lastnum - lastnum % number ) NEW_LINE count = ( ( lastnum - firstnum ) / number + 1 ) NEW_LINE return int ( ( ( lastnum + firstnum ) * count ) / 2 ) NEW_LINE DEDENT digit = 3 ; num = 7 NEW_LINE print ( totalSumDivisibleByNum ( digit , num ) ) NEW_LINE


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } public static void main ( String args [ ] ) { if ( isPrime ( 11 ) ) System . out . println ( " ▁ true " ) ; else System . out . println ( " ▁ false " ) ; if ( isPrime ( 15 ) ) System . out . println ( " ▁ true " ) ; else System . out . println ( " ▁ false " ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT print ( " true " ) if isPrime ( 11 ) else print ( " false " ) NEW_LINE print ( " true " ) if isPrime ( 14 ) else print ( " false " ) NEW_LINE


import java . util . * ; class GFG { static void PowerOfTwo ( int [ ] x , int n ) { Arrays . sort ( x ) ; ArrayList < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j < 31 ; ++ j ) { int lx = x [ i ] - ( 1 << j ) ; int rx = x [ i ] + ( 1 << j ) ; boolean isl = Arrays . binarySearch ( x , lx ) < 0 ? false : true ; boolean isr = Arrays . binarySearch ( x , rx ) < 0 ? false : true ; if ( isl && isr && res . size ( ) < 3 ) { res . clear ( ) ; res . add ( lx ) ; res . add ( x [ i ] ) ; res . add ( rx ) ; } if ( isl && res . size ( ) < 2 ) { res . clear ( ) ; res . add ( lx ) ; res . add ( x [ i ] ) ; } if ( isr && res . size ( ) < 2 ) { res . clear ( ) ; res . add ( x [ i ] ) ; res . add ( rx ) ; } } } if ( res . size ( ) == 0 ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] a = { 3 , 4 , 5 , 6 , 7 } ; int n = a . length ; PowerOfTwo ( a , n ) ; } }

def PowerOfTwo ( x , n ) : NEW_LINE INDENT x . sort ( ) NEW_LINE res = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , 31 ) : NEW_LINE INDENT lx = x [ i ] - ( 1 << j ) NEW_LINE rx = x [ i ] + ( 1 << j ) NEW_LINE if lx in x : NEW_LINE INDENT isl = True NEW_LINE DEDENT else : NEW_LINE INDENT isl = False NEW_LINE DEDENT if rx in x : NEW_LINE INDENT isr = True NEW_LINE DEDENT else : NEW_LINE INDENT isr = False NEW_LINE DEDENT if ( isl and isr and len ( res ) < 3 ) : NEW_LINE INDENT res = [ lx , x [ i ] , rx ] NEW_LINE DEDENT if ( isl and len ( res ) < 2 ) : NEW_LINE INDENT res = [ lx , x [ i ] ] NEW_LINE DEDENT if ( isr and len ( res ) < 2 ) : NEW_LINE INDENT res = [ x [ i ] , rx ] NEW_LINE DEDENT DEDENT DEDENT if ( not len ( res ) ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for it in res : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( a ) NEW_LINE PowerOfTwo ( a , n ) NEW_LINE DEDENT


class GFG { static void printPermutation ( int n , int k ) { for ( int i = 1 ; i <= n ; i ++ ) { int x = 2 * i - 1 ; int y = 2 * i ; if ( i <= k ) System . out . print ( y + " ▁ " + x + " ▁ " ) ; else System . out . print ( x + " ▁ " + y + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 2 , k = 1 ; printPermutation ( n , k ) ; } }

def printPermutation ( n , k ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT x = 2 * i - 1 ; NEW_LINE y = 2 * i ; NEW_LINE if ( i <= k ) : NEW_LINE INDENT print ( y , x , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( x , y , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; k = 1 ; NEW_LINE printPermutation ( n , k ) ; NEW_LINE DEDENT


import java . util . * ; public class GFG { static int [ ] getFrequencies ( String str ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } return freq ; } static int countMinParts ( String str ) { int n = str . length ( ) ; int freq [ ] = getFrequencies ( str ) ; List < Integer > oddFreq = new ArrayList < > ( ) ; List < Integer > evenFreq = new ArrayList < > ( ) ; int i , sumEven = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; if ( freq [ i ] % 2 == 0 ) evenFreq . add ( freq [ i ] ) ; else oddFreq . add ( freq [ i ] ) ; } for ( i = 0 ; i < evenFreq . size ( ) ; i ++ ) { sumEven += evenFreq . get ( i ) ; } if ( oddFreq . size ( ) == 0 ) return 1 ; if ( sumEven == 0 ) { if ( oddFreq . size ( ) == 1 ) return 1 ; return 0 ; } i = 0 ; while ( i < oddFreq . size ( ) ) { if ( ( sumEven / 2 ) % oddFreq . size ( ) == 0 ) return oddFreq . size ( ) ; if ( oddFreq . get ( i ) == 1 ) { i ++ ; continue ; } sumEven += 2 ; oddFreq . set ( i , oddFreq . get ( i ) - 2 ) ; } return n ; } public static void main ( String [ ] args ) { String s = " noonpeep " ; System . out . println ( countMinParts ( s ) ) ; } }

def getFrequencies ( string ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT return freq NEW_LINE DEDENT def countMinParts ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE freq = getFrequencies ( string ) NEW_LINE oddFreq = [ ] NEW_LINE evenFreq = [ ] NEW_LINE sumEven = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if freq [ i ] == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT if freq [ i ] % 2 == 0 : NEW_LINE INDENT evenFreq . append ( freq [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT oddFreq . append ( freq [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( evenFreq ) ) : NEW_LINE INDENT sumEven += evenFreq [ i ] NEW_LINE DEDENT if len ( oddFreq ) == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if sumEven == 0 : NEW_LINE INDENT if len ( oddFreq ) == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT i = 0 NEW_LINE while ( i < len ( oddFreq ) ) : NEW_LINE INDENT if ( ( sumEven / 2 ) % len ( oddFreq ) == 0 ) : NEW_LINE INDENT return len ( oddFreq ) NEW_LINE DEDENT if ( oddFreq [ i ] == 1 ) : NEW_LINE INDENT i += 1 NEW_LINE continue NEW_LINE DEDENT sumEven += 2 NEW_LINE oddFreq [ i ] = oddFreq [ i ] - 2 NEW_LINE DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " noonpeep " NEW_LINE print ( countMinParts ( s ) ) NEW_LINE DEDENT


class GFG { static boolean isPossible ( int a [ ] , int n ) { int k = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != i + 1 ) k ++ ; } if ( k <= 2 ) return true ; return false ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 2 , 3 , 4 , 1 } ; int n = a . length ; if ( isPossible ( a , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isPossible ( a , n ) : NEW_LINE INDENT k = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] != i + 1 ) : NEW_LINE INDENT k += 1 ; NEW_LINE DEDENT DEDENT if ( k <= 2 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 2 , 3 , 4 , 1 ] ; NEW_LINE n = len ( a ) ; NEW_LINE if ( isPossible ( a , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { public static long maxOR ( long arr [ ] , int n , int k , int x ) { long preSum [ ] = new long [ n + 1 ] ; long suffSum [ ] = new long [ n + 1 ] ; long res = 0 , pow = 1 ; for ( int i = 0 ; i < k ; i ++ ) pow *= x ; preSum [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) preSum [ i + 1 ] = preSum [ i ] | arr [ i ] ; suffSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] ; res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , preSum [ i ] | ( arr [ i ] * pow ) | suffSum [ i + 1 ] ) ; return res ; } public static void main ( String args [ ] ) { long arr [ ] = { 1 , 2 , 4 , 8 } ; int n = 4 ; int k = 2 , x = 3 ; long ans = maxOR ( arr , n , k , x ) ; System . out . println ( ans ) ; } }

def maxOR ( arr , n , k , x ) : NEW_LINE INDENT preSum = [ 0 ] * ( n + 1 ) NEW_LINE suffSum = [ 0 ] * ( n + 1 ) NEW_LINE pow = 1 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT pow *= x NEW_LINE DEDENT preSum [ 0 ] = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT preSum [ i + 1 ] = preSum [ i ] | arr [ i ] NEW_LINE DEDENT suffSum [ n ] = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT suffSum [ i ] = suffSum [ i + 1 ] | arr [ i ] NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT res = max ( res , preSum [ i ] | ( arr [ i ] * pow ) | suffSum [ i + 1 ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE x = 3 NEW_LINE print ( maxOR ( arr , n , k , x ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX = 1000001 ; static int countNum ( int N , int K ) { int [ ] sumPF = new int [ MAX ] ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == 0 ) { for ( int j = i ; j < N ; j += i ) { sumPF [ j ] += i ; } } } int count = 0 ; for ( int i = 2 ; i < N ; i ++ ) { if ( sumPF [ i ] == K ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 20 , K = 7 ; System . out . println ( countNum ( N , K ) ) ; } }

MAX = 1000001 NEW_LINE def countNum ( N , K ) : NEW_LINE INDENT sumPF = [ 0 ] * MAX ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( sumPF [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT sumPF [ j ] += i ; NEW_LINE DEDENT DEDENT DEDENT count = 0 ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( sumPF [ i ] == K ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 20 ; K = 7 ; NEW_LINE print ( countNum ( N , K ) ) ; NEW_LINE DEDENT


class GFG { static int getMaxLength ( boolean arr [ ] , int n ) { int count = 0 ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == false ) count = 0 ; else { count ++ ; result = Math . max ( result , count ) ; } } return result ; } public static void main ( String [ ] args ) { boolean arr [ ] = { true , true , false , false , true , false , true , false , true , true , true , true } ; int n = arr . length ; System . out . println ( getMaxLength ( arr , n ) ) ; } }

def getMaxLength ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = 0 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE result = max ( result , count ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMaxLength ( arr , n ) ) NEW_LINE


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

def countIntegralSolutions ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT for k in range ( n + 1 ) : NEW_LINE INDENT if i + j + k == n : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return result NEW_LINE DEDENT n = 3 NEW_LINE print ( countIntegralSolutions ( n ) ) NEW_LINE


class GFG { static double square ( double a ) { if ( a < 0 ) return - 1 ; double x = 0.464 * a ; return x ; } public static void main ( String [ ] args ) { double a = 5 ; System . out . println ( square ( a ) ) ; } }

def square ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.464 * a NEW_LINE return x NEW_LINE DEDENT a = 5 NEW_LINE print ( square ( a ) ) NEW_LINE


class GFG { static int nthTerm ( int N ) { int nth = 0 ; if ( N % 2 == 1 ) nth = ( N * N ) + 1 ; else nth = ( N * N ) - 1 ; return nth ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( nthTerm ( N ) + " \n " ) ; } }

def nthTerm ( N ) : NEW_LINE INDENT nth = 0 ; NEW_LINE if ( N % 2 == 1 ) : NEW_LINE INDENT nth = ( N * N ) + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT nth = ( N * N ) - 1 ; NEW_LINE DEDENT return nth ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE print ( nthTerm ( N ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int max_intersection ( int center [ ] , int length , int k ) { Arrays . sort ( center ) ; if ( center [ 2 ] - center [ 0 ] >= 2 * k + length ) { return 0 ; } else if ( center [ 2 ] - center [ 0 ] >= 2 * k ) { return ( 2 * k - ( center [ 2 ] - center [ 0 ] - length ) ) ; } else return length ; } public static void main ( String args [ ] ) { int center [ ] = { 1 , 2 , 3 } ; int L = 1 ; int K = 1 ; System . out . println ( max_intersection ( center , L , K ) ) ; } }

def max_intersection ( center , length , k ) : NEW_LINE INDENT center . sort ( ) ; NEW_LINE if ( center [ 2 ] - center [ 0 ] >= 2 * k + length ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( center [ 2 ] - center [ 0 ] >= 2 * k ) : NEW_LINE INDENT return ( 2 * k - ( center [ 2 ] - center [ 0 ] - length ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return length ; NEW_LINE DEDENT DEDENT center = [ 1 , 2 , 3 ] ; NEW_LINE L = 1 ; NEW_LINE K = 1 ; NEW_LINE print ( max_intersection ( center , L , K ) ) ; NEW_LINE


import java . io . * ; class GFG { static boolean isValid ( long i , long m ) { long sq_i = ( long ) Math . sqrt ( i ) ; long sq = Math . min ( m , sq_i ) ; for ( long j = 2 ; j <= sq ; j ++ ) if ( i % j == 0 ) return false ; return true ; } static void findLargestNum ( long n , long m ) { for ( long i = n ; i > m ; i -- ) { if ( isValid ( i , m ) ) { System . out . println ( i ) ; return ; } } System . out . println ( " Number ▁ Doesn ' t ▁ Exists " ) ; } public static void main ( String [ ] args ) { long n = 16 , m = 3 ; findLargestNum ( n , m ) ; } }

import math NEW_LINE def isValid ( i , m ) : NEW_LINE INDENT sq_i = math . sqrt ( i ) NEW_LINE sq = min ( m , sq_i ) NEW_LINE for j in range ( 2 , sq + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def findLargestNum ( n , m ) : NEW_LINE INDENT for i in range ( n , m , - 1 ) : NEW_LINE INDENT if ( isValid ( i , m ) ) : NEW_LINE INDENT print ( " { } \n " . format ( i ) ) ; NEW_LINE return NEW_LINE DEDENT DEDENT print ( " Number ▁ Doesn ' t ▁ Exists \n " ) NEW_LINE DEDENT n = 16 NEW_LINE m = 3 NEW_LINE findLargestNum ( n , m ) NEW_LINE


class GFG { static int countSquares ( int n ) { return ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( n ) ) ; } }

def countSquares ( n ) : NEW_LINE INDENT return ( ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( " Count ▁ of ▁ squares ▁ is ▁ " , countSquares ( n ) ) NEW_LINE


import java . io . * ; import java . util . * ; import java . text . * ; import java . math . * ; import java . util . regex . * ; class GFG { public static void maxOverlap ( int [ ] start , int [ ] end , int n ) { int maxa = Arrays . stream ( start ) . max ( ) . getAsInt ( ) ; int maxb = Arrays . stream ( end ) . max ( ) . getAsInt ( ) ; int maxc = Math . max ( maxa , maxb ) ; int [ ] x = new int [ maxc + 2 ] ; Arrays . fill ( x , 0 ) ; int cur = 0 , idx = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ++ x [ start [ i ] ] ; -- x [ end [ i ] + 1 ] ; } int maxy = Integer . MIN_VALUE ; for ( int i = 0 ; i <= maxc ; i ++ ) { cur += x [ i ] ; if ( maxy < cur ) { maxy = cur ; idx = i ; } } System . out . println ( " Maximum ▁ value ▁ is : " + maxy + " ▁ at ▁ position : ▁ " + idx + " " ) ; } public static void main ( String [ ] args ) { int [ ] start = new int [ ] { 13 , 28 , 29 , 14 , 40 , 17 , 3 } ; int [ ] end = new int [ ] { 107 , 95 , 111 , 105 , 70 , 127 , 74 } ; int n = start . length ; maxOverlap ( start , end , n ) ; } }

import sys NEW_LINE def maxOverlap ( start , end ) : NEW_LINE INDENT n = len ( start ) NEW_LINE maxa = max ( start ) NEW_LINE maxb = max ( end ) NEW_LINE maxc = max ( maxa , maxb ) NEW_LINE x = ( maxc + 2 ) * [ 0 ] NEW_LINE cur = 0 ; idx = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT x [ start [ i ] ] += 1 NEW_LINE x [ end [ i ] + 1 ] -= 1 NEW_LINE DEDENT maxy = - 1 NEW_LINE for i in range ( 0 , maxc + 1 ) : NEW_LINE INDENT cur += x [ i ] NEW_LINE if maxy < cur : NEW_LINE INDENT maxy = cur NEW_LINE idx = i NEW_LINE DEDENT DEDENT print ( " Maximum ▁ value ▁ is : ▁ { 0 : d } " . format ( maxy ) , " ▁ at ▁ position : ▁ { 0 : d } " . format ( idx ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT start = [ 13 , 28 , 29 , 14 , 40 , 17 , 3 ] NEW_LINE end = [ 107 , 95 , 111 , 105 , 70 , 127 , 74 ] NEW_LINE maxOverlap ( start , end ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void constructDp ( int dp [ ] [ ] , String str ) { int l = str . length ( ) ; int [ ] [ ] isPalin = new int [ l + 1 ] [ l + 1 ] ; for ( int i = 0 ; i <= l ; i ++ ) { for ( int j = 0 ; j <= l ; j ++ ) { isPalin [ i ] [ j ] = dp [ i ] [ j ] = 0 ; } } for ( int i = l - 1 ; i >= 0 ; i -- ) { isPalin [ i ] [ i ] = 1 ; dp [ i ] [ i ] = 1 ; for ( int j = i + 1 ; j < l ; j ++ ) { isPalin [ i ] [ j ] = ( str . charAt ( i ) == str . charAt ( j ) && ( i + 1 > j - 1 || ( isPalin [ i + 1 ] [ j - 1 ] ) != 0 ) ) ? 1 : 0 ; dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] + isPalin [ i ] [ j ] ; } } } static int countOfPalindromeInRange ( int dp [ ] [ ] , int l , int r ) { return dp [ l ] [ r ] ; } public static void main ( String args [ ] ) { int MAX = 50 ; String str = " xyaabax " ; int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; constructDp ( dp , str ) ; int l = 3 ; int r = 5 ; System . out . println ( countOfPalindromeInRange ( dp , l , r ) ) ; } }

M = 50 NEW_LINE def constructDP ( dp , string ) : NEW_LINE INDENT l = len ( string ) NEW_LINE isPalin = [ [ 0 for i in range ( l + 1 ) ] for j in range ( l + 1 ) ] NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT isPalin [ i ] [ i ] , dp [ i ] [ i ] = 1 , 1 NEW_LINE for j in range ( i + 1 , l ) : NEW_LINE INDENT isPalin [ i ] [ j ] = ( string [ i ] == string [ j ] and ( i + 1 > j - 1 or isPalin [ i + 1 ] [ j - 1 ] ) ) NEW_LINE dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] + isPalin [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT def countOfPalindromeInRange ( dp , l , r ) : NEW_LINE INDENT return dp [ l ] [ r ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " xyaabax " NEW_LINE dp = [ [ 0 for i in range ( M ) ] for j in range ( M ) ] NEW_LINE constructDP ( dp , string ) NEW_LINE l , r = 3 , 5 NEW_LINE print ( countOfPalindromeInRange ( dp , l , r ) ) NEW_LINE DEDENT


class GFG { public static int areCornerEqual ( String s ) { int n = s . length ( ) ; if ( n < 2 ) return - 1 ; if ( s . charAt ( 0 ) == s . charAt ( n - 1 ) ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { String s = " GfG " ; int res = areCornerEqual ( s ) ; if ( res == - 1 ) System . out . println ( " Invalid ▁ Input " ) ; else if ( res == 1 ) System . out . println ( " Equal " ) ; else System . out . println ( " Not ▁ Equal " ) ; } }

st = " GfG " NEW_LINE if ( st [ 0 ] == st [ - 1 ] ) : NEW_LINE INDENT print ( " Equal " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Equal " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public void bfs ( int x , int num ) { Queue < Integer > q = new LinkedList < Integer > ( ) ; q . add ( num ) ; while ( ! q . isEmpty ( ) ) { num = q . peek ( ) ; q . poll ( ) ; if ( num <= x ) { System . out . print ( num + " ▁ " ) ; int last_digit = num % 10 ; if ( last_digit == 0 ) { q . add ( ( num * 10 ) + ( last_digit + 1 ) ) ; } else if ( last_digit == 9 ) { q . add ( ( num * 10 ) + ( last_digit - 1 ) ) ; } else { q . add ( ( num * 10 ) + ( last_digit - 1 ) ) ; q . add ( ( num * 10 ) + ( last_digit + 1 ) ) ; } } } } public void printJumping ( int x ) { System . out . print ( "0 ▁ " ) ; for ( int i = 1 ; i <= 9 && i <= x ; i ++ ) { this . bfs ( x , i ) ; } } public static void main ( String [ ] args ) throws IOException { int x = 40 ; GFG obj = new GFG ( ) ; obj . printJumping ( x ) ; } }

class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . lst = [ ] NEW_LINE DEDENT def is_empty ( self ) : NEW_LINE INDENT return self . lst == [ ] NEW_LINE DEDENT def enqueue ( self , elem ) : NEW_LINE INDENT self . lst . append ( elem ) NEW_LINE DEDENT def dequeue ( self ) : NEW_LINE INDENT return self . lst . pop ( 0 ) NEW_LINE DEDENT DEDENT def bfs ( x , num ) : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . enqueue ( num ) NEW_LINE while ( not q . is_empty ( ) ) : NEW_LINE INDENT num = q . dequeue ( ) NEW_LINE if num <= x : NEW_LINE INDENT print ( str ( num ) , end = ' ▁ ' ) NEW_LINE last_dig = num % 10 NEW_LINE if last_dig == 0 : NEW_LINE INDENT q . enqueue ( ( num * 10 ) + ( last_dig + 1 ) ) NEW_LINE DEDENT elif last_dig == 9 : NEW_LINE INDENT q . enqueue ( ( num * 10 ) + ( last_dig - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT q . enqueue ( ( num * 10 ) + ( last_dig - 1 ) ) NEW_LINE q . enqueue ( ( num * 10 ) + ( last_dig + 1 ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def printJumping ( x ) : NEW_LINE INDENT print ( str ( 0 ) , end = ' ▁ ' ) NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT bfs ( x , i ) NEW_LINE DEDENT DEDENT x = 40 NEW_LINE printJumping ( x ) NEW_LINE


import java . util . * ; class GFG { public static int findMinRec ( int A [ ] , int n ) { if ( n == 1 ) return A [ 0 ] ; return Math . min ( A [ n - 1 ] , findMinRec ( A , n - 1 ) ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 4 , 45 , 6 , - 50 , 10 , 2 } ; int n = A . length ; System . out . println ( findMinRec ( A , n ) ) ; } }

def findMinRec ( A , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return A [ 0 ] NEW_LINE DEDENT return min ( A [ n - 1 ] , findMinRec ( A , n - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 4 , 45 , 6 , - 50 , 10 , 2 ] NEW_LINE n = len ( A ) NEW_LINE print ( findMinRec ( A , n ) ) NEW_LINE DEDENT


class GFG { static boolean check ( int arr [ ] , int n ) { int modify = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] / 2 ; modify ++ ; } for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( ( arr [ i - 1 ] < arr [ i ] && arr [ i + 1 ] < arr [ i ] ) || ( arr [ i - 1 ] > arr [ i ] && arr [ i + 1 ] > arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; if ( arr [ i ] == arr [ i - 1 ] || arr [ i ] == arr [ i + 1 ] ) return false ; modify ++ ; } } if ( arr [ n - 1 ] < arr [ n - 2 ] ) modify ++ ; if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 8 , 6 , 9 , 12 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def check ( arr , n ) : NEW_LINE INDENT modify = 0 NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT arr [ 0 ] = arr [ 1 ] // 2 NEW_LINE modify += 1 NEW_LINE DEDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( ( arr [ i - 1 ] < arr [ i ] and arr [ i + 1 ] < arr [ i ] ) or ( arr [ i - 1 ] > arr [ i ] and arr [ i + 1 ] > arr [ i ] ) ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) // 2 NEW_LINE if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT modify += 1 NEW_LINE DEDENT DEDENT if ( arr [ n - 1 ] < arr [ n - 2 ] ) : NEW_LINE INDENT modify += 1 NEW_LINE DEDENT if ( modify > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 8 , 6 , 9 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static boolean isPerfectSquare ( int N ) { double sr = Math . sqrt ( N ) ; return ( sr - Math . floor ( sr ) ) == 0 ; } static void BetrothedNumbers ( int n , int m ) { int Sum1 = 1 ; int Sum2 = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { Sum1 += i + ( isPerfectSquare ( n ) ? 0 : n / i ) ; } } for ( int i = 2 ; i <= Math . sqrt ( m ) ; i ++ ) { if ( m % i == 0 ) { Sum2 += i + ( isPerfectSquare ( m ) ? 0 : m / i ) ; } } if ( ( n + 1 == Sum2 ) && ( m + 1 == Sum1 ) ) { System . out . print ( " YES " + " \n " ) ; } else { System . out . print ( " NO " + " \n " ) ; } } public static void main ( String [ ] args ) { int N = 9504 ; int M = 20734 ; BetrothedNumbers ( N , M ) ; } }

from math import sqrt , floor NEW_LINE def isPerfectSquare ( N ) : NEW_LINE INDENT sr = sqrt ( N ) NEW_LINE return ( sr - floor ( sr ) ) == 0 NEW_LINE DEDENT def BetrothedNumbers ( n , m ) : NEW_LINE INDENT Sum1 = 1 NEW_LINE Sum2 = 1 NEW_LINE for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( isPerfectSquare ( n ) ) : NEW_LINE INDENT Sum1 += i NEW_LINE DEDENT else : NEW_LINE INDENT Sum1 += i + n / i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , int ( sqrt ( m ) ) + 1 , 1 ) : NEW_LINE INDENT if ( m % i == 0 ) : NEW_LINE INDENT if ( isPerfectSquare ( m ) ) : NEW_LINE INDENT Sum2 += i NEW_LINE DEDENT else : NEW_LINE INDENT Sum2 += i + ( m / i ) NEW_LINE DEDENT DEDENT DEDENT if ( ( n + 1 == Sum2 ) and ( m + 1 == Sum1 ) ) : 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 = 9504 NEW_LINE M = 20734 NEW_LINE BetrothedNumbers ( N , M ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximumXor ( int arr [ ] , int n ) { Stack < Integer > sForward = new Stack < Integer > ( ) , sBackward = new Stack < Integer > ( ) ; int ans = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! sForward . isEmpty ( ) && arr [ i ] < arr [ sForward . peek ( ) ] ) { ans = Math . max ( ans , arr [ i ] ^ arr [ sForward . peek ( ) ] ) ; sForward . pop ( ) ; } sForward . add ( i ) ; while ( ! sBackward . isEmpty ( ) && arr [ n - i - 1 ] < arr [ sBackward . peek ( ) ] ) { ans = Math . max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward . peek ( ) ] ) ; sBackward . pop ( ) ; } sBackward . add ( n - i - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 2 } ; int n = arr . length ; System . out . print ( maximumXor ( arr , n ) ) ; } }

def maximumXor ( arr : list , n : int ) -> int : NEW_LINE INDENT sForward , sBackward = [ ] , [ ] NEW_LINE ans = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT while len ( sForward ) > 0 and arr [ i ] < arr [ sForward [ - 1 ] ] : NEW_LINE INDENT ans = max ( ans , arr [ i ] ^ arr [ sForward [ - 1 ] ] ) NEW_LINE sForward . pop ( ) NEW_LINE DEDENT sForward . append ( i ) NEW_LINE while len ( sBackward ) > 0 and arr [ n - i - 1 ] < arr [ sBackward [ - 1 ] ] : NEW_LINE INDENT ans = max ( ans , arr [ n - i - 1 ] ^ arr [ sBackward [ - 1 ] ] ) NEW_LINE sBackward . pop ( ) NEW_LINE DEDENT sBackward . append ( n - i - 1 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumXor ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxDiff ( int arr [ ] , int n ) { int maxDiff = - 1 ; int maxRight = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] > maxRight ) maxRight = arr [ i ] ; else { int diff = maxRight - arr [ i ] ; if ( diff > maxDiff ) { maxDiff = diff ; } } } return maxDiff ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; int n = arr . length ; System . out . println ( " Maximum ▁ difference ▁ is ▁ " + maxDiff ( arr , n ) ) ; } }

def maxDiff ( arr , n ) : NEW_LINE INDENT maxDiff = - 1 NEW_LINE maxRight = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] > maxRight ) : NEW_LINE INDENT maxRight = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT diff = maxRight - arr [ i ] NEW_LINE if ( diff > maxDiff ) : NEW_LINE INDENT maxDiff = diff NEW_LINE DEDENT DEDENT DEDENT return maxDiff NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 90 , 10 , 110 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , n ) ) NEW_LINE DEDENT


class GFG { public static int CntDivbyX ( int arr [ ] , int n , int x ) { int number = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { number = ( number * 2 + arr [ i ] ) % x ; if ( number == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 0 } ; int n = 7 ; int x = 2 ; System . out . print ( CntDivbyX ( arr , n , x ) ) ; } }

def CntDivbyX ( arr , n , x ) : NEW_LINE INDENT number = 0 NEW_LINE count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT number = ( number * 2 + arr [ i ] ) % x NEW_LINE if number == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 0 , 1 , 0 , 1 , 1 , 0 ] NEW_LINE n = 7 NEW_LINE x = 2 NEW_LINE print ( CntDivbyX ( arr , n , x ) ) NEW_LINE


class GFG { static int sumOfFactors ( int n ) { int sum = 0 ; for ( int f = 1 ; f <= n / 2 ; f ++ ) { if ( n % f == 0 ) { sum += f ; } } return sum ; } static int getSum ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 && arr [ i ] == sumOfFactors ( arr [ i ] ) ) { sum += arr [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 17 , 6 , 10 , 6 , 4 } ; int n = arr . length ; System . out . print ( getSum ( arr , n ) ) ; } }

def sumOfFactors ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for f in range ( 1 , n // 2 + 1 ) : NEW_LINE INDENT if ( n % f == 0 ) : NEW_LINE INDENT sum += f NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def getSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 and arr [ i ] == sumOfFactors ( arr [ i ] ) ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ 17 , 6 , 10 , 6 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getSum ( arr , n ) ) NEW_LINE


class GfG { static long getTrailingZeroes ( long n ) { long count = 0 ; long val , powerTwo = 2 ; do { val = n / powerTwo ; count += val ; powerTwo *= 2 ; } while ( val != 0 ) ; return ( count / 4 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( getTrailingZeroes ( n ) ) ; } }

def getTrailingZeroes ( n ) : NEW_LINE INDENT count = 0 NEW_LINE val , powerTwo = 1 , 2 NEW_LINE while ( val != 0 ) : NEW_LINE INDENT val = n // powerTwo NEW_LINE count += val NEW_LINE powerTwo *= 2 NEW_LINE DEDENT return ( count // 4 ) NEW_LINE DEDENT n = 6 NEW_LINE print ( getTrailingZeroes ( n ) ) NEW_LINE


import java . util . * ; class Solution { static int maxGCD ( int N , int P ) { int ans = 1 ; Map < Integer , Integer > prime_factors = new HashMap < Integer , Integer > ( ) ; for ( int i = 2 ; i * i <= P ; i ++ ) { while ( P % i == 0 ) { if ( prime_factors . get ( i ) == null ) prime_factors . put ( i , 1 ) ; else prime_factors . put ( i , ( prime_factors . get ( i ) + 1 ) ) ; P /= i ; } } if ( P != 1 ) if ( prime_factors . get ( P ) == null ) prime_factors . put ( P , 1 ) ; else prime_factors . put ( P , ( prime_factors . get ( P ) + 1 ) ) ; Set < Map . Entry < Integer , Integer > > st = prime_factors . entrySet ( ) ; for ( Map . Entry < Integer , Integer > me : st ) { ans *= Math . pow ( me . getKey ( ) , me . getValue ( ) / N ) ; } return ans ; } public static void main ( String args [ ] ) { int N = 3 , P = 24 ; System . out . println ( maxGCD ( N , P ) ) ; } }

from math import sqrt NEW_LINE def maxGCD ( N , P ) : NEW_LINE INDENT ans = 1 NEW_LINE prime_factors = { } NEW_LINE for i in range ( 2 , int ( sqrt ( P ) + 1 ) ) : NEW_LINE INDENT while ( P % i == 0 ) : NEW_LINE INDENT if i not in prime_factors : NEW_LINE INDENT prime_factors [ i ] = 0 NEW_LINE DEDENT prime_factors [ i ] += 1 NEW_LINE P //= i NEW_LINE DEDENT DEDENT if ( P != 1 ) : NEW_LINE INDENT prime_factors [ P ] += 1 NEW_LINE DEDENT for key , value in prime_factors . items ( ) : NEW_LINE INDENT ans *= pow ( key , value // N ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , P = 3 , 24 NEW_LINE print ( maxGCD ( N , P ) ) NEW_LINE DEDENT


class GFG { static int findNumber ( int n , int m ) { int num = ( m - 1 ) * 9 + n ; return num ; } public static void main ( String args [ ] ) { int n = 2 , m = 5 ; System . out . print ( findNumber ( n , m ) ) ; } }

def findNumber ( n , m ) : NEW_LINE INDENT num = ( m - 1 ) * 9 + n ; NEW_LINE return num ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE m = 5 ; NEW_LINE print ( findNumber ( n , m ) ) NEW_LINE DEDENT


public class GFG { static boolean checkPrime ( int numberToCheck ) { if ( numberToCheck == 1 ) { return false ; } for ( int i = 2 ; i * i <= numberToCheck ; i ++ ) { if ( numberToCheck % i == 0 ) { return false ; } } return true ; } static int primeSum ( int l , int r ) { int sum = 0 ; for ( int i = r ; i >= l ; i -- ) { boolean isPrime = checkPrime ( i ) ; if ( isPrime ) { sum = sum + i ; } } return sum ; } public static void main ( String [ ] args ) { int l = 4 , r = 13 ; System . out . println ( primeSum ( l , r ) ) ; } }

from math import sqrt NEW_LINE def checkPrime ( numberToCheck ) : NEW_LINE INDENT if numberToCheck == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( numberToCheck ) ) + 1 ) : NEW_LINE INDENT if numberToCheck % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def primeSum ( l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( r , ( l - 1 ) , - 1 ) : NEW_LINE INDENT isPrime = checkPrime ( i ) NEW_LINE if ( isPrime ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r = 4 , 13 NEW_LINE print ( primeSum ( l , r ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countways ( int a [ ] , int n ) { int cnt [ ] = new int [ n ] ; int s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { s += a [ i ] ; } if ( s % 3 != 0 ) return 0 ; s /= 3 ; int ss = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { ss += a [ i ] ; if ( ss == s ) cnt [ i ] = 1 ; } for ( int i = n - 2 ; i >= 0 ; i -- ) cnt [ i ] += cnt [ i + 1 ] ; int ans = 0 ; ss = 0 ; for ( int i = 0 ; i + 2 < n ; i ++ ) { ss += a [ i ] ; if ( ss == s ) ans += cnt [ i + 2 ] ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 0 , 3 } ; System . out . println ( countways ( a , n ) ) ; } }

def countways ( a , n ) : NEW_LINE INDENT cnt = [ 0 for i in range ( n ) ] NEW_LINE s = 0 NEW_LINE s = sum ( a ) NEW_LINE if ( s % 3 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s //= 3 NEW_LINE ss = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT ss += a [ i ] NEW_LINE if ( ss == s ) : NEW_LINE INDENT cnt [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT cnt [ i ] += cnt [ i + 1 ] NEW_LINE DEDENT ans = 0 NEW_LINE ss = 0 NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT ss += a [ i ] NEW_LINE if ( ss == s ) : NEW_LINE INDENT ans += cnt [ i + 2 ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE a = [ 1 , 2 , 3 , 0 , 3 ] NEW_LINE print ( countways ( a , n ) ) NEW_LINE


class GFG { static void lower ( int matrix [ ] [ ] , int row , int col ) { int i , j ; for ( i = 0 ; i < row ; i ++ ) { for ( j = 0 ; j < col ; j ++ ) { if ( i < j ) { System . out . print ( "0" + " ▁ " ) ; } else System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } static void upper ( int matrix [ ] [ ] , int row , int col ) { int i , j ; for ( i = 0 ; i < row ; i ++ ) { for ( j = 0 ; j < col ; j ++ ) { if ( i > j ) { System . out . print ( "0" + " ▁ " ) ; } else System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int matrix [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int row = 3 , col = 3 ; System . out . println ( " Lower ▁ triangular ▁ matrix : ▁ " ) ; lower ( matrix , row , col ) ; System . out . println ( " Upper ▁ triangular ▁ matrix : ▁ " ) ; upper ( matrix , row , col ) ; } }

def lower ( matrix , row , col ) : NEW_LINE INDENT for i in range ( 0 , row ) : NEW_LINE INDENT for j in range ( 0 , col ) : NEW_LINE INDENT if ( i < j ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( matrix [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( " ▁ " ) ; NEW_LINE DEDENT DEDENT def upper ( matrix , row , col ) : NEW_LINE INDENT for i in range ( 0 , row ) : NEW_LINE INDENT for j in range ( 0 , col ) : NEW_LINE INDENT if ( i > j ) : NEW_LINE INDENT print ( "0" , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( matrix [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( " ▁ " ) ; NEW_LINE DEDENT DEDENT matrix = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; NEW_LINE row = 3 ; NEW_LINE col = 3 ; NEW_LINE print ( " Lower ▁ triangular ▁ matrix : ▁ " ) ; NEW_LINE lower ( matrix , row , col ) ; NEW_LINE print ( " Upper ▁ triangular ▁ matrix : ▁ " ) ; NEW_LINE upper ( matrix , row , col ) ; NEW_LINE


public class GFG { static void find_array ( int [ ] a , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] += x ; System . out . print ( a [ i ] + " ▁ " ) ; x = Math . max ( x , a [ i ] ) ; } } public static void main ( String [ ] args ) { int [ ] a = { 40 , 12 , 62 } ; int n = a . length ; find_array ( a , n ) ; } }

def find_array ( a , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] += x ; NEW_LINE print ( a [ i ] , end = ' ▁ ' ) ; NEW_LINE x = max ( x , a [ i ] ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 40 , 12 , 62 ] ; NEW_LINE n = len ( a ) ; NEW_LINE find_array ( a , n ) ; NEW_LINE DEDENT


class GFG { static int MAX = 100005 ; static boolean [ ] prime = new boolean [ MAX ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < MAX ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } } static int countPrimes ( int n ) { SieveOfEratosthenes ( ) ; int cnt = 0 ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime [ i ] && prime [ i - 2 ] ) cnt ++ ; } return cnt ; } public static void main ( String [ ] args ) { int n = 11 ; System . out . print ( countPrimes ( n ) ) ; } }

MAX = 100005 NEW_LINE prime = [ True for i in range ( MAX ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( MAX ) : NEW_LINE INDENT if ( p * p > MAX ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , MAX , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def countPrimes ( n ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE cnt = 0 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( prime [ i ] and prime [ i - 2 ] ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT n = 11 NEW_LINE print ( countPrimes ( n ) ) NEW_LINE


import java . util . * ; class GFG { static void reverseColumns ( int arr [ ] [ ] ) { for ( int i = 0 ; i < arr [ 0 ] . length ; i ++ ) for ( int j = 0 , k = arr [ 0 ] . length - 1 ; j < k ; j ++ , k -- ) { int temp = arr [ j ] [ i ] ; arr [ j ] [ i ] = arr [ k ] [ i ] ; arr [ k ] [ i ] = temp ; } } static void transpose ( int arr [ ] [ ] ) { for ( int i = 0 ; i < arr . length ; i ++ ) for ( int j = i ; j < arr [ 0 ] . length ; j ++ ) { int temp = arr [ j ] [ i ] ; arr [ j ] [ i ] = arr [ i ] [ j ] ; arr [ i ] [ j ] = temp ; } } static void printMatrix ( int arr [ ] [ ] ) { for ( int i = 0 ; i < arr . length ; i ++ ) { for ( int j = 0 ; j < arr [ 0 ] . length ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( " " ) ; } } static void rotate90 ( int arr [ ] [ ] ) { transpose ( arr ) ; reverseColumns ( arr ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate90 ( arr ) ; printMatrix ( arr ) ; } }

R = 4 NEW_LINE C = 4 NEW_LINE def reverseColumns ( arr ) : NEW_LINE INDENT for i in range ( C ) : NEW_LINE INDENT j = 0 NEW_LINE k = C - 1 NEW_LINE while j < k : NEW_LINE INDENT t = arr [ j ] [ i ] NEW_LINE arr [ j ] [ i ] = arr [ k ] [ i ] NEW_LINE arr [ k ] [ i ] = t NEW_LINE j += 1 NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT DEDENT def transpose ( arr ) : NEW_LINE INDENT for i in range ( R ) : NEW_LINE INDENT for j in range ( i , C ) : NEW_LINE INDENT t = arr [ i ] [ j ] NEW_LINE arr [ i ] [ j ] = arr [ j ] [ i ] NEW_LINE arr [ j ] [ i ] = t NEW_LINE DEDENT DEDENT DEDENT def printMatrix ( arr ) : NEW_LINE INDENT for i in range ( R ) : NEW_LINE INDENT for j in range ( C ) : NEW_LINE INDENT print ( str ( arr [ i ] [ j ] ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def rotate90 ( arr ) : NEW_LINE INDENT transpose ( arr ) NEW_LINE reverseColumns ( arr ) NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; NEW_LINE rotate90 ( arr ) NEW_LINE printMatrix ( arr ) NEW_LINE


import java . io . * ; class GFG { static int countWays ( int n , int m ) { int count [ ] = new int [ n + 1 ] ; count [ 0 ] = 0 ; int i ; for ( i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; } public static void main ( String [ ] args ) { int n = 7 ; int m = 4 ; System . out . println ( " Number ▁ of ▁ ways ▁ = ▁ " + countWays ( n , m ) ) ; } }

def countWays ( n , m ) : NEW_LINE INDENT count = [ ] NEW_LINE for i in range ( n + 2 ) : NEW_LINE INDENT count . append ( 0 ) NEW_LINE DEDENT count [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i > m ) : NEW_LINE INDENT count [ i ] = count [ i - 1 ] + count [ i - m ] NEW_LINE DEDENT elif ( i < m ) : NEW_LINE INDENT count [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ i ] = 2 NEW_LINE DEDENT DEDENT return count [ n ] NEW_LINE DEDENT n = 7 NEW_LINE m = 4 NEW_LINE print ( " Number ▁ of ▁ ways ▁ = ▁ " , countWays ( n , m ) ) NEW_LINE


class GFG { static int minLength ( int n ) { int ans = n / 26 ; if ( n % 26 != 0 ) ans ++ ; return ans ; } static String minString ( int n ) { int ans = n / 26 ; String res = " " ; while ( ans -- != 0 ) { res = res + " z " ; } if ( n % 26 != 0 ) { res = res + ( char ) ( ( n % 26 ) + 96 ) ; } return res ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( minLength ( n ) ) ; System . out . println ( minString ( n ) ) ; } }

def minLength ( n ) : NEW_LINE INDENT ans = n // 26 NEW_LINE if ( n % 26 != 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def minString ( n ) : NEW_LINE INDENT ans = n // 26 NEW_LINE res = " " NEW_LINE while ( ans ) : NEW_LINE INDENT res = res + " z " NEW_LINE ans -= 1 NEW_LINE DEDENT if ( n % 26 != 0 ) : NEW_LINE INDENT res = res + chr ( ( n % 26 ) + 96 ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 50 ; NEW_LINE print ( minLength ( n ) ) NEW_LINE print ( minString ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int Largestpower ( int n , int p ) { int ans = 0 ; while ( n > 0 ) { n /= p ; ans += n ; } return ans ; } public static void main ( String [ ] args ) { int n = 10 ; int p = 3 ; System . out . println ( " ▁ The ▁ largest ▁ power ▁ of ▁ " + p + " ▁ that ▁ divides ▁ " + n + " ! ▁ is ▁ " + Largestpower ( n , p ) ) ; } }

def largestPower ( n , p ) : NEW_LINE INDENT x = 0 NEW_LINE while n : NEW_LINE INDENT n /= p NEW_LINE x += n NEW_LINE DEDENT return x NEW_LINE DEDENT n = 10 ; p = 3 NEW_LINE print ( " The ▁ largest ▁ power ▁ of ▁ % d ▁ that ▁ divides ▁ % d ! ▁ is ▁ % d \n " % ( p , n , largestPower ( n , p ) ) ) NEW_LINE


class GFG { public static void main ( String [ ] args ) { int i , j , r , N , N1 ; N1 = 3 ; N = 0 ; r = 5 ; for ( i = 0 ; i < r ; i ++ ) { if ( i <= r / 2 ) { N = N1 ; for ( j = 0 ; j <= i ; j ++ ) { N ++ ; } N1 = N ; for ( j = 0 ; j <= i ; j ++ ) { N -- ; System . out . print ( N + " ▁ " ) ; } System . out . println ( ) ; } else { for ( j = i ; j < r ; j ++ ) { N -- ; System . out . print ( N + " ▁ " ) ; } System . out . println ( ) ; } } } } ;

if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N1 = 3 NEW_LINE N = 0 ; NEW_LINE r = 5 ; NEW_LINE for i in range ( r ) : NEW_LINE INDENT if i <= r // 2 : NEW_LINE INDENT N = N1 NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT N += 1 NEW_LINE DEDENT N1 = N NEW_LINE for j in range ( i + 1 ) : NEW_LINE INDENT N -= 1 NEW_LINE print ( N , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT for j in range ( i , r ) : NEW_LINE INDENT N -= 1 NEW_LINE print ( N , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT DEDENT


import java . io . * ; class GFG { static long findTriangularNumber ( int n ) { return ( n * ( n + 1 ) ) / 2 ; } static void printSeries ( int n ) { long prev = 0 ; long curr ; for ( int i = 1 ; i <= n ; i ++ ) { curr = findTriangularNumber ( i ) ; curr = curr + prev ; System . out . print ( curr + " ▁ " ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }

def findTriangularNumber ( n ) : NEW_LINE INDENT return ( n * ( n + 1 ) ) / 2 NEW_LINE DEDENT def printSeries ( n ) : NEW_LINE INDENT prev = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT curr = findTriangularNumber ( i ) NEW_LINE curr = int ( curr + prev ) NEW_LINE print ( curr , end = ' ▁ ' ) NEW_LINE prev = curr NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printSeries ( n ) NEW_LINE


import java . io . * ; class GFG { static int getMin ( int arr [ ] , int n ) { int res = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) res = Math . min ( res , arr [ i ] ) ; return res ; } static int getMax ( int arr [ ] , int n ) { int res = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) res = Math . max ( res , arr [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . length ; System . out . println ( " Minimum ▁ element " + " ▁ of ▁ array : ▁ " + getMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ element " + " ▁ of ▁ array : ▁ " + getMax ( arr , n ) ) ; } }

def getMin ( arr , n ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT def getMax ( arr , n ) : NEW_LINE INDENT res = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = max ( res , arr [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 12 , 1234 , 45 , 67 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ array : " , getMin ( arr , n ) ) NEW_LINE print ( " Maximum ▁ element ▁ of ▁ array : " , getMax ( arr , n ) ) NEW_LINE


static void printSeqRecur ( int num , int pos , int k , int n ) { if ( pos == k ) { System . out . print ( num + " ▁ " ) ; return ; } for ( int i = 1 ; i <= n ; i ++ ) { printSeqRecur ( num * 10 + i , pos + 1 , k , n ) ; } } static void printSequences ( int k , int n ) { printSeqRecur ( 0 , 0 , k , n ) ; }

def printSeqRecur ( num , n , k ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT print ( num ) NEW_LINE return NEW_LINE DEDENT for _ in range ( 1 , k + 1 ) : NEW_LINE INDENT printSeqRecur ( num * 10 + _ , n - 1 , k ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 3 NEW_LINE n = 2 NEW_LINE printSeqRecur ( 0 , n , k ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static float emi_calculator ( float p , float r , float t ) { float emi ; r = r / ( 12 * 100 ) ; t = t * 12 ; emi = ( p * r * ( float ) Math . pow ( 1 + r , t ) ) / ( float ) ( Math . pow ( 1 + r , t ) - 1 ) ; return ( emi ) ; } static public void main ( String [ ] args ) { float principal , rate , time , emi ; principal = 10000 ; rate = 10 ; time = 2 ; emi = emi_calculator ( principal , rate , time ) ; System . out . println ( " Monthly ▁ EMI ▁ is ▁ = ▁ " + emi ) ; } }

def emi_calculator ( p , r , t ) : NEW_LINE INDENT r = r / ( 12 * 100 ) NEW_LINE t = t * 12 NEW_LINE emi = ( p * r * pow ( 1 + r , t ) ) / ( pow ( 1 + r , t ) - 1 ) NEW_LINE return emi NEW_LINE DEDENT principal = 10000 ; NEW_LINE rate = 10 ; NEW_LINE time = 2 ; NEW_LINE emi = emi_calculator ( principal , rate , time ) ; NEW_LINE print ( " Monthly ▁ EMI ▁ is = ▁ " , emi ) NEW_LINE


class GFG { static int minOperations ( int n , String a , String b , String c ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { char x = a . charAt ( i ) ; char y = b . charAt ( i ) ; char z = c . charAt ( i ) ; if ( x == y && y == z ) ; else if ( x == y || y == z || x == z ) { ans ++ ; } else { ans += 2 ; } } return ans ; } public static void main ( String [ ] args ) { String a = " place " ; String b = " abcde " ; String c = " plybe " ; int n = a . length ( ) ; System . out . print ( minOperations ( n , a , b , c ) ) ; } }

def minOperations ( n , a , b , c ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = a [ i ] NEW_LINE y = b [ i ] NEW_LINE z = c [ i ] NEW_LINE if ( x == y and y == z ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( x == y or y == z or x == z ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " place " NEW_LINE b = " abcde " NEW_LINE c = " plybe " NEW_LINE n = len ( a ) NEW_LINE print ( minOperations ( n , a , b , c ) ) NEW_LINE DEDENT


class GFG { static void findRealAndImag ( String s ) { int l = s . length ( ) ; int i ; if ( s . indexOf ( ' + ' ) != - 1 ) { i = s . indexOf ( ' + ' ) ; } else { i = s . indexOf ( ' - ' ) ; } String real = s . substring ( 0 , i ) ; String imaginary = s . substring ( i + 1 , l - 1 ) ; System . out . println ( " Real ▁ part : ▁ " + real ) ; System . out . println ( " Imaginary ▁ part : ▁ " + imaginary ) ; } public static void main ( String [ ] args ) { String s = "3 + 4i " ; findRealAndImag ( s ) ; } }

def findRealAndImag ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE if ( s . find ( ' + ' ) != - 1 ) : NEW_LINE INDENT i = s . find ( ' + ' ) NEW_LINE DEDENT else : NEW_LINE INDENT i = s . find ( ' - ' ) ; NEW_LINE DEDENT real = s [ : i ] NEW_LINE imaginary = s [ i + 1 : l - 1 ] NEW_LINE print ( " Real ▁ part : " , real ) NEW_LINE print ( " Imaginary ▁ part : " , imaginary ) NEW_LINE DEDENT s = "3 + 4i " ; NEW_LINE findRealAndImag ( s ) ; NEW_LINE


class GFG { static void convert_str ( String str , int k ) { char [ ] s = str . toCharArray ( ) ; int l = s . length ; for ( int i = 0 ; i < l ; i ++ ) { int ascii = ( int ) s [ i ] ; if ( ascii >= ' a ' && ascii <= ' z ' && __gcd ( ascii , k ) == 1 ) { char c = ( char ) ( s [ i ] - 32 ) ; s [ i ] = c ; } } System . out . println ( String . valueOf ( s ) ) ; } static int __gcd ( int a , int b ) { if ( a == 0 ) { return b ; } if ( b == 0 ) { return a ; } if ( a == b ) { return a ; } if ( a > b ) { return __gcd ( a - b , b ) ; } return __gcd ( a , b - a ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int k = 4 ; convert_str ( s , k ) ; } }

from math import gcd NEW_LINE def convert_str ( s , k ) : NEW_LINE INDENT modified_string = " " NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT ascii = ord ( s [ i ] ) NEW_LINE if ( ascii >= ord ( ' a ' ) and ascii <= ord ( ' z ' ) and gcd ( ascii , k ) == 1 ) : NEW_LINE INDENT modified_string += chr ( ascii - 32 ) NEW_LINE DEDENT else : NEW_LINE INDENT modified_string += s [ i ] NEW_LINE DEDENT DEDENT print ( modified_string ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE k = 4 NEW_LINE convert_str ( s , k ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int power ( int x , int y ) { int res = 1 ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = res * x ; y = y >> 1 ; x = x * x ; } return res ; } public static void main ( String [ ] args ) { int x = 3 ; int y = 5 ; System . out . println ( " Power ▁ is ▁ " + power ( x , y ) ) ; } }

def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) == 1 ) : NEW_LINE INDENT res = res * x NEW_LINE DEDENT y = y >> 1 NEW_LINE x = x * x NEW_LINE DEDENT return res NEW_LINE DEDENT x = 3 NEW_LINE y = 5 NEW_LINE print ( " Power ▁ is ▁ " , power ( x , y ) ) NEW_LINE


class GFG { static class Node { int data ; Node children [ ] ; Node ( int n , int data ) { children = new Node [ n ] ; this . data = data ; } } static void inorder ( Node node ) { if ( node == null ) return ; int total = node . children . length ; for ( int i = 0 ; i < total - 1 ; i ++ ) inorder ( node . children [ i ] ) ; System . out . print ( " " + node . data + " ▁ " ) ; inorder ( node . children [ total - 1 ] ) ; } public static void main ( String [ ] args ) { int n = 3 ; Node root = new Node ( n , 1 ) ; root . children [ 0 ] = new Node ( n , 2 ) ; root . children [ 1 ] = new Node ( n , 3 ) ; root . children [ 2 ] = new Node ( n , 4 ) ; root . children [ 0 ] . children [ 0 ] = new Node ( n , 5 ) ; root . children [ 0 ] . children [ 1 ] = new Node ( n , 6 ) ; root . children [ 0 ] . children [ 2 ] = new Node ( n , 7 ) ; inorder ( root ) ; } }

class GFG : NEW_LINE INDENT class Node : NEW_LINE INDENT def __init__ ( self , n , data ) : NEW_LINE INDENT self . children = [ None ] * n NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def inorder ( self , node ) : NEW_LINE INDENT if node == None : NEW_LINE INDENT return NEW_LINE DEDENT total = len ( node . children ) NEW_LINE for i in range ( total - 1 ) : NEW_LINE INDENT self . inorder ( node . children [ i ] ) NEW_LINE DEDENT print ( node . data , end = " ▁ " ) NEW_LINE self . inorder ( node . children [ total - 1 ] ) NEW_LINE DEDENT def main ( self ) : NEW_LINE INDENT n = 3 NEW_LINE root = self . Node ( n , 1 ) NEW_LINE root . children [ 0 ] = self . Node ( n , 2 ) NEW_LINE root . children [ 1 ] = self . Node ( n , 3 ) NEW_LINE root . children [ 2 ] = self . Node ( n , 4 ) NEW_LINE root . children [ 0 ] . children [ 0 ] = self . Node ( n , 5 ) NEW_LINE root . children [ 0 ] . children [ 1 ] = self . Node ( n , 6 ) NEW_LINE root . children [ 0 ] . children [ 2 ] = self . Node ( n , 7 ) NEW_LINE self . inorder ( root ) NEW_LINE DEDENT DEDENT ob = GFG ( ) NEW_LINE ob . main ( ) NEW_LINE


import java . util . * ; class Solution { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static void storePath ( Node root , Map < Integer , Integer > mp , int XOR ) { if ( root == null ) return ; mp . put ( root . data , XOR ^ root . data ) ; XOR ^= root . data ; if ( root . left != null ) storePath ( root . left , mp , XOR ) ; if ( root . right != null ) storePath ( root . right , mp , XOR ) ; } static int findXORPath ( Map < Integer , Integer > mp , int node1 , int node2 ) { return mp . get ( node1 ) ^ mp . get ( node2 ) ; } public static void main ( String args [ ] ) { Node root = getNode ( 0 ) ; root . left = getNode ( 1 ) ; root . left . left = getNode ( 3 ) ; root . left . left . left = getNode ( 7 ) ; root . left . right = getNode ( 4 ) ; root . left . right . left = getNode ( 8 ) ; root . left . right . right = getNode ( 9 ) ; root . right = getNode ( 2 ) ; root . right . left = getNode ( 5 ) ; root . right . right = getNode ( 6 ) ; int XOR = 0 ; Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int node1 = 3 ; int node2 = 5 ; storePath ( root , mp , XOR ) ; System . out . println ( findXORPath ( mp , node1 , node2 ) ) ; } }

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 getNode ( data ) : NEW_LINE INDENT newNode = Node ( 0 ) NEW_LINE newNode . data = data NEW_LINE newNode . left = newNode . right = None NEW_LINE return newNode NEW_LINE DEDENT mp = dict ( ) NEW_LINE def storePath ( root , XOR ) : NEW_LINE INDENT global mp NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT mp [ root . data ] = XOR ^ root . data ; NEW_LINE XOR = XOR ^ root . data NEW_LINE if ( root . left != None ) : NEW_LINE INDENT storePath ( root . left , XOR ) NEW_LINE DEDENT if ( root . right != None ) : NEW_LINE INDENT storePath ( root . right , XOR ) NEW_LINE DEDENT DEDENT def findXORPath ( node1 , node2 ) : NEW_LINE INDENT global mp NEW_LINE return mp . get ( node1 , 0 ) ^ mp . get ( node2 , 0 ) NEW_LINE DEDENT root = getNode ( 0 ) NEW_LINE root . left = getNode ( 1 ) NEW_LINE root . left . left = getNode ( 3 ) NEW_LINE root . left . left . left = getNode ( 7 ) NEW_LINE root . left . right = getNode ( 4 ) NEW_LINE root . left . right . left = getNode ( 8 ) NEW_LINE root . left . right . right = getNode ( 9 ) NEW_LINE root . right = getNode ( 2 ) NEW_LINE root . right . left = getNode ( 5 ) NEW_LINE root . right . right = getNode ( 6 ) NEW_LINE XOR = 0 NEW_LINE node1 = 3 NEW_LINE node2 = 5 NEW_LINE storePath ( root , XOR ) NEW_LINE print ( findXORPath ( node1 , node2 ) ) NEW_LINE


import java . util . * ; class GFG { static boolean checkPermutation ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; Arrays . fill ( prefix , 0 ) ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int lsum = prefix [ i ] ; int rsum = sum - prefix [ i ] ; int l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = arr . length ; if ( checkPermutation ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkPermutation ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT prefix = [ 0 ] * ( n + 1 ) ; NEW_LINE prefix [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT lsum = prefix [ i ] ; NEW_LINE rsum = sum - prefix [ i ] ; NEW_LINE l_len = i + 1 NEW_LINE r_len = n - i - 1 ; NEW_LINE if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) and ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 4 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkPermutation ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


class GFG { static boolean isOperand ( char c ) { return ( c >= '0' && c <= '9' ) ; } static int value ( char c ) { return ( int ) ( c - '0' ) ; } static int evaluate ( String exp ) { if ( exp . length ( ) == 0 ) return - 1 ; int res = value ( exp . charAt ( 0 ) ) ; for ( int i = 1 ; i < exp . length ( ) ; i += 2 ) { char opr = exp . charAt ( i ) , opd = exp . charAt ( i + 1 ) ; if ( isOperand ( opd ) == false ) return - 1 ; if ( opr == ' + ' ) res += value ( opd ) ; else if ( opr == ' - ' ) res -= value ( opd ) ; else if ( opr == ' * ' ) res *= value ( opd ) ; else if ( opr == ' / ' ) res /= value ( opd ) ; else return - 1 ; } return res ; } public static void main ( String [ ] args ) { String expr1 = "1 + 2*5 + 3" ; int res = evaluate ( expr1 ) ; if ( res == - 1 ) System . out . println ( expr1 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr1 + " ▁ is ▁ " + res ) ; String expr2 = "1 + 2*3" ; res = evaluate ( expr2 ) ; if ( res == - 1 ) System . out . println ( expr2 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr2 + " ▁ is ▁ " + res ) ; String expr3 = "4-2 + 6*3" ; res = evaluate ( expr3 ) ; if ( res == - 1 ) System . out . println ( expr3 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr3 + " ▁ is ▁ " + res ) ; String expr4 = "1 + + 2" ; res = evaluate ( expr4 ) ; if ( res == - 1 ) System . out . println ( expr4 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr4 + " ▁ is ▁ " + res ) ; } }

def isOperand ( c ) : NEW_LINE INDENT return ( c >= '0' and c <= '9' ) ; NEW_LINE DEDENT def value ( c ) : NEW_LINE INDENT return ord ( c ) - ord ( '0' ) ; NEW_LINE DEDENT def evaluate ( exp ) : NEW_LINE INDENT len1 = len ( exp ) ; NEW_LINE if ( len1 == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT res = value ( exp [ 0 ] ) ; NEW_LINE for i in range ( 1 , len1 , 2 ) : NEW_LINE INDENT opr = exp [ i ] ; NEW_LINE opd = exp [ i + 1 ] ; NEW_LINE if ( isOperand ( opd ) == False ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( opr == ' + ' ) : NEW_LINE INDENT res += value ( opd ) ; NEW_LINE DEDENT elif ( opr == ' - ' ) : NEW_LINE INDENT res -= int ( value ( opd ) ) ; NEW_LINE DEDENT elif ( opr == ' * ' ) : NEW_LINE INDENT res *= int ( value ( opd ) ) ; NEW_LINE DEDENT elif ( opr == ' / ' ) : NEW_LINE INDENT res /= int ( value ( opd ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT expr1 = "1 + 2*5 + 3" ; NEW_LINE res = evaluate ( expr1 ) ; NEW_LINE print ( expr1 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr1 , " is " , res ) ; NEW_LINE expr2 = "1 + 2*3" ; NEW_LINE res = evaluate ( expr2 ) ; NEW_LINE print ( expr2 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr2 , " is " , res ) ; NEW_LINE expr3 = "4-2 + 6*3" ; NEW_LINE res = evaluate ( expr3 ) ; NEW_LINE print ( expr3 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr3 , " is " , res ) ; NEW_LINE expr4 = "1 + + 2" ; NEW_LINE res = evaluate ( expr4 ) ; NEW_LINE print ( expr4 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr4 , " is " , res ) ; NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; } static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void seqExistUtil ( Node ptr , int seq [ ] , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static boolean seqExist ( Node root , int seq [ ] , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int seq [ ] = { 4 , 6 , 8 , 14 } ; int n = seq . length ; if ( seqExist ( root , seq , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

class 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 insert ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return Node ( key ) NEW_LINE DEDENT if root . data > key : NEW_LINE INDENT root . left = insert ( root . left , key ) NEW_LINE DEDENT else : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def seqExistUtil ( ptr , seq , index ) : NEW_LINE INDENT if ptr == None : NEW_LINE INDENT return NEW_LINE DEDENT seqExistUtil ( ptr . left , seq , index ) NEW_LINE if ptr . data == seq [ index [ 0 ] ] : NEW_LINE INDENT index [ 0 ] += 1 NEW_LINE DEDENT seqExistUtil ( ptr . right , seq , index ) NEW_LINE DEDENT def seqExist ( root , seq , n ) : NEW_LINE INDENT index = [ 0 ] NEW_LINE seqExistUtil ( root , seq , index ) NEW_LINE if index [ 0 ] == n : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 3 ) NEW_LINE root = insert ( root , 6 ) NEW_LINE root = insert ( root , 1 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 7 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 13 ) NEW_LINE seq = [ 4 , 6 , 8 , 14 ] NEW_LINE n = len ( seq ) NEW_LINE if seqExist ( root , seq , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


public class Main { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( countSetBits ( i ) == 2 ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( findSum ( n ) ) NEW_LINE


class LeftRotate { static void preprocess ( int arr [ ] , int n , int temp [ ] ) { for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } static void leftRotate ( int arr [ ] , int n , int k , int temp [ ] ) { int start = k % n ; for ( int i = start ; i < start + n ; i ++ ) System . out . print ( temp [ i ] + " ▁ " ) ; System . out . print ( " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int temp [ ] = new int [ 2 * n ] ; preprocess ( arr , n , temp ) ; int k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ; } }

def preprocess ( arr , n ) : NEW_LINE INDENT temp = [ None ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp [ i ] = temp [ i + n ] = arr [ i ] NEW_LINE DEDENT return temp NEW_LINE DEDENT def leftRotate ( arr , n , k , temp ) : NEW_LINE INDENT start = k % n NEW_LINE for i in range ( start , start + n ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE temp = preprocess ( arr , n ) NEW_LINE k = 2 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 3 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 4 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE


import java . io . * ; class GFG { static int getRemainder ( int num , int divisor ) { if ( divisor == 0 ) { System . out . println ( " Error : ▁ divisor ▁ " + " can ' t ▁ be ▁ zero ▁ \n " ) ; return - 1 ; } if ( divisor < 0 ) divisor = - divisor ; if ( num < 0 ) num = - num ; int i = 1 ; int product = 0 ; while ( product <= num ) { product = divisor * i ; i ++ ; } return num - ( product - divisor ) ; } public static void main ( String [ ] args ) { System . out . println ( getRemainder ( 100 , 7 ) ) ; } }

def getRemainder ( num , divisor ) : NEW_LINE INDENT if ( divisor == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( divisor < 0 ) : NEW_LINE INDENT divisor = - divisor NEW_LINE DEDENT if ( num < 0 ) : NEW_LINE INDENT num = - num NEW_LINE DEDENT i = 1 NEW_LINE product = 0 NEW_LINE while ( product <= num ) : NEW_LINE INDENT product = divisor * i NEW_LINE i += 1 NEW_LINE DEDENT return num - ( product - divisor ) NEW_LINE DEDENT num = 100 NEW_LINE divisor = 7 NEW_LINE print ( getRemainder ( num , divisor ) ) NEW_LINE


import java . util . Arrays ; class GFG1 { static long findMinValue ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; double val = 0 ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { long arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . length ; System . out . println ( findMinValue ( arr , n ) ) ; } }

import math NEW_LINE def findMinValue ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val += ( math . log ( arr [ i ] ) ) NEW_LINE DEDENT left = arr [ 0 ] NEW_LINE right = arr [ n - 1 ] + 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE temp = n * ( math . log ( mid ) ) NEW_LINE if ( val < temp ) : NEW_LINE INDENT ans = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 10 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinValue ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; System . out . print ( ch ) ; num = 0 ; } } } public static void main ( String args [ ] ) { String str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; } }

def asciiToSentence ( string , length ) : NEW_LINE INDENT num = 0 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT num = num * 10 + ( ord ( string [ i ] ) - ord ( '0' ) ) ; NEW_LINE if ( num >= 32 and num <= 122 ) : NEW_LINE INDENT ch = chr ( num ) ; NEW_LINE print ( ch , end = " " ) ; NEW_LINE num = 0 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "7110110110711510211111471101101107115" ; NEW_LINE length = len ( string ) ; NEW_LINE asciiToSentence ( string , length ) ; NEW_LINE DEDENT


class GFG { static boolean isValidISBN ( String isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn . charAt ( i ) - '0' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn . charAt ( 9 ) ; if ( last != ' X ' && ( last < '0' || last > '9' ) ) return false ; sum += ( ( last == ' X ' ) ? 10 : ( last - '0' ) ) ; return ( sum % 11 == 0 ) ; } public static void main ( String [ ] args ) { String isbn = "007462542X " ; if ( isValidISBN ( isbn ) ) System . out . print ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }

def isValidISBN ( isbn ) : NEW_LINE INDENT if len ( isbn ) != 10 : NEW_LINE INDENT return False NEW_LINE DEDENT _sum = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT if 0 <= int ( isbn [ i ] ) <= 9 : NEW_LINE INDENT _sum += int ( isbn [ i ] ) * ( 10 - i ) NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( isbn [ 9 ] != ' X ' and 0 <= int ( isbn [ 9 ] ) <= 9 ) : NEW_LINE INDENT return False NEW_LINE DEDENT _sum += 10 if isbn [ 9 ] == ' X ' else int ( isbn [ 9 ] ) NEW_LINE return ( _sum % 11 == 0 ) NEW_LINE DEDENT isbn = "007462542X " NEW_LINE if isValidISBN ( isbn ) : NEW_LINE INDENT print ( ' Valid ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT


class GFG { static int maxProdSum ( int arr [ ] , int n ) { int [ ] prefixArraySum = new int [ n ] ; int maxProduct = 0 ; prefixArraySum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixArraySum [ i ] = prefixArraySum [ i - 1 ] + arr [ i ] ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int leftArraySum = prefixArraySum [ i ] ; int rightArraySum = prefixArraySum [ n - 1 ] - prefixArraySum [ i ] ; int k = leftArraySum * rightArraySum ; if ( k > maxProduct ) { maxProduct = k ; } } return maxProduct ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 10 , 1 , 7 , 2 , 9 } ; int n = arr . length ; System . out . print ( maxProdSum ( arr , n ) ) ; } }

def maxProdSum ( arr , n ) : NEW_LINE INDENT prefixArraySum = [ 0 ] * n NEW_LINE maxProduct = 0 NEW_LINE prefixArraySum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixArraySum [ i ] = prefixArraySum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT leftArraySum = prefixArraySum [ i ] NEW_LINE rightArraySum = prefixArraySum [ n - 1 ] -   \ NEW_LINE INDENT prefixArraySum [ i ] NEW_LINE DEDENT k = leftArraySum * rightArraySum NEW_LINE if ( k > maxProduct ) : NEW_LINE INDENT maxProduct = k NEW_LINE DEDENT DEDENT return maxProduct NEW_LINE DEDENT arr = [ 4 , 10 , 1 , 7 , 2 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxProdSum ( arr , n ) ) NEW_LINE


public class GFG { public int lengthOfLastWord ( final String a ) { int len = 0 ; String x = a . trim ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( x . charAt ( i ) == ' ▁ ' ) len = 0 ; else len ++ ; } return len ; } public static void main ( String [ ] args ) { String input = " Geeks ▁ For ▁ Geeks ▁ ▁ " ; GFG gfg = new GFG ( ) ; System . out . println ( " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " + gfg . lengthOfLastWord ( input ) ) ; } }

def lengthOfLastWord ( a ) : NEW_LINE INDENT l = 0 NEW_LINE x = a . strip ( ) NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT if x [ i ] == " ▁ " : NEW_LINE INDENT l = 0 NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT inp = " Geeks ▁ For ▁ Geeks ▁ " NEW_LINE print ( " The ▁ length ▁ of ▁ last ▁ word ▁ is " , lengthOfLastWord ( inp ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static int rangesum ( int n , int l , int r ) { int [ ] arr = new int [ n ] ; int c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } int sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 12 ; int l = 1 , r = 11 ; System . out . println ( rangesum ( n , l , r ) ) ; } }

def rangesum ( n , l , r ) : NEW_LINE INDENT arr = [ 0 ] * n ; NEW_LINE c = 1 ; i = 0 ; NEW_LINE while ( c <= n ) : NEW_LINE INDENT arr [ i ] = c ; NEW_LINE i += 1 ; NEW_LINE c += 2 ; NEW_LINE DEDENT c = 2 ; NEW_LINE while ( c <= n ) : NEW_LINE INDENT arr [ i ] = c ; NEW_LINE i += 1 ; NEW_LINE c += 2 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for i in range ( l - 1 , r , 1 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 ; NEW_LINE l , r = 1 , 11 ; NEW_LINE print ( rangesum ( n , l , r ) ) ; NEW_LINE DEDENT


class GFG { public static boolean check ( int [ ] arr , int n ) { int modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( int i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] || arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 11 , 3 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def check ( arr , n ) : NEW_LINE INDENT modify = 0 NEW_LINE if ( arr [ n - 1 ] >= arr [ n - 2 ] ) : NEW_LINE INDENT arr [ n - 1 ] = arr [ n - 2 ] - 1 NEW_LINE modify += 1 NEW_LINE DEDENT if ( arr [ 0 ] <= arr [ 1 ] ) : NEW_LINE INDENT arr [ 0 ] = arr [ 0 ] + 1 NEW_LINE modify += 1 NEW_LINE DEDENT for i in range ( n - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] <= arr [ i ] and arr [ i + 1 ] <= arr [ i ] ) or   \ NEW_LINE ( arr [ i - 1 ] >= arr [ i ] and arr [ i + 1 ] >= arr [ i ] ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) // 2 NEW_LINE modify += 1 NEW_LINE if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT if ( modify > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 11 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int CntcontSubs ( int a [ ] , int n ) { int c = 0 , d = 0 , i , sum = 1 , j ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 || a [ i ] % 4 == 0 ) d ++ ; sum = a [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { sum = sum * a [ j ] ; if ( sum % 2 != 0 || sum % 4 == 0 ) c ++ ; } sum = 1 ; } return c + d ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 2 , 9 , 8 } ; int n = arr . length ; System . out . print ( CntcontSubs ( arr , n ) ) ; } }

def CntcontSubs ( a , n ) : NEW_LINE INDENT c = 0 NEW_LINE d = 0 NEW_LINE sum = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 != 0 or a [ i ] % 4 == 0 ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT sum = a [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = sum * a [ j ] NEW_LINE if ( sum % 2 != 0 or sum % 4 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT sum = 1 NEW_LINE DEDENT return c + d NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 2 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CntcontSubs ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int equalize ( int arr [ ] , int n , int k ) { int min_ele = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; boolean flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( ( arr [ i ] + k ) >= min_ele + k && min_ele + k >= ( arr [ i ] - k ) ) ) { flag = false ; break ; } } if ( flag ) { return min_ele + k ; } else return - 1 ; } public static void main ( String [ ] args ) { int K = 2 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . print ( equalize ( arr , N , K ) ) ; } }

def equalize ( arr , n , k ) : NEW_LINE INDENT min_ele = min ( arr ) ; NEW_LINE flag = True ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not ( ( arr [ i ] + k ) >= ( min_ele + k ) and ( min_ele + k ) >= ( arr [ i ] - k ) ) ) : NEW_LINE INDENT flag = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return min_ele + k ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 2 ; NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) NEW_LINE print ( equalize ( arr , N , K ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void main ( String [ ] args ) { String num = "1234" ; System . out . println ( divisible ( num ) ) ; } }

def divisible ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += int ( num [ i ] ) ; NEW_LINE DEDENT if ( sum % 3 == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( sum % 3 == int ( num [ i ] ) % 3 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT if ( n == 2 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return 2 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "1234" ; NEW_LINE print ( divisible ( num ) ) ; NEW_LINE DEDENT


class GFG { static int get ( int x , int y , int z ) { if ( x > z ) return - 1 ; int val = z - x ; int div = ( z - x ) / y ; int ans = div * y + x ; return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 5 , z = 8 ; System . out . print ( get ( x , y , z ) + " \n " ) ; } }

def get ( x , y , z ) : NEW_LINE INDENT if ( x > z ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT val = z - x NEW_LINE div = ( z - x ) // y NEW_LINE ans = div * y + x NEW_LINE return ans NEW_LINE DEDENT x = 1 NEW_LINE y = 5 NEW_LINE z = 8 NEW_LINE print ( get ( x , y , z ) ) NEW_LINE


import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; str1 = new StringBuilder ( str1 ) . reverse ( ) . toString ( ) ; str2 = new StringBuilder ( str2 ) . reverse ( ) . toString ( ) ; int carry = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n1 ; i < n2 ; i ++ ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; str = new StringBuilder ( str ) . reverse ( ) . toString ( ) ; return str ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }

def findSum ( str1 , str2 ) : NEW_LINE INDENT if ( len ( str1 ) > len ( str2 ) ) : NEW_LINE INDENT t = str1 ; NEW_LINE str1 = str2 ; NEW_LINE str2 = t ; NEW_LINE DEDENT str = " " ; NEW_LINE n1 = len ( str1 ) ; NEW_LINE n2 = len ( str2 ) ; NEW_LINE str1 = str1 [ : : - 1 ] ; NEW_LINE str2 = str2 [ : : - 1 ] ; NEW_LINE carry = 0 ; NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT sum = ( ( ord ( str1 [ i ] ) - 48 ) + ( ( ord ( str2 [ i ] ) - 48 ) + carry ) ) ; NEW_LINE str += chr ( sum % 10 + 48 ) ; NEW_LINE carry = int ( sum / 10 ) ; NEW_LINE DEDENT for i in range ( n1 , n2 ) : NEW_LINE INDENT sum = ( ( ord ( str2 [ i ] ) - 48 ) + carry ) ; NEW_LINE str += chr ( sum % 10 + 48 ) ; NEW_LINE carry = ( int ) ( sum / 10 ) ; NEW_LINE DEDENT if ( carry ) : NEW_LINE INDENT str += chr ( carry + 48 ) ; NEW_LINE DEDENT str = str [ : : - 1 ] ; NEW_LINE return str ; NEW_LINE DEDENT str1 = "12" ; NEW_LINE str2 = "198111" ; NEW_LINE print ( findSum ( str1 , str2 ) ) ; NEW_LINE


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

def FindElement ( A , n ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( 0 , i , 1 ) : NEW_LINE INDENT if ( A [ j ] >= A [ i ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( A [ j ] <= A [ i ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return A [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 3 , 2 , 5 , 8 , 6 , 7 ] NEW_LINE n = len ( A ) NEW_LINE print ( FindElement ( A , n ) ) NEW_LINE DEDENT


import java . util . Scanner ; class Node { int data ; public Node left ; public Node right ; Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public static Node insert ( Node root , int data ) { if ( root == null ) return new Node ( data ) ; if ( data < root . data ) root . left = insert ( root . left , data ) ; if ( data > root . data ) root . right = insert ( root . right , data ) ; return root ; } public static void inOrder ( Node root ) { if ( root == null ) return ; inOrder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inOrder ( root . right ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 , 4 , 4 } ; int n = arr . length ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) { root = insert ( root , arr [ i ] ) ; } inOrder ( root ) ; } }

class newNode : 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 insert ( root , data ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return newNode ( data ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( data < root . data ) : NEW_LINE INDENT root . left = insert ( root . left , data ) ; NEW_LINE DEDENT if ( data > root . data ) : NEW_LINE INDENT root . right = insert ( root . right , data ) ; NEW_LINE DEDENT return root ; NEW_LINE DEDENT DEDENT def inOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT else : NEW_LINE INDENT inOrder ( root . left ) ; NEW_LINE print ( root . data , end = " ▁ " ) ; NEW_LINE inOrder ( root . right ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 2 , 5 , 4 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE root = None ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT root = insert ( root , arr [ i ] ) ; NEW_LINE DEDENT inOrder ( root ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean makeAndCheckString ( Vector < String > words , String str ) { int n = words . size ( ) ; boolean first = false , second = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( words . get ( i ) == str ) return true ; if ( str . charAt ( 0 ) == words . get ( i ) . charAt ( 1 ) ) first = true ; if ( str . charAt ( 1 ) == words . get ( i ) . charAt ( 0 ) ) second = true ; if ( first && second ) return true ; } return false ; } public static void main ( String [ ] args ) { String str = " ya " ; String [ ] array = { " ah " , " oy " , " to " , " ha " } ; Vector < String > words = new Vector < String > ( Arrays . asList ( array ) ) ; if ( makeAndCheckString ( words , str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def makeAndCheckString ( words , str ) : NEW_LINE INDENT n = len ( words ) NEW_LINE first = second = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if words [ i ] == str : NEW_LINE INDENT return True NEW_LINE DEDENT if str [ 0 ] == words [ i ] [ 1 ] : NEW_LINE INDENT first = True NEW_LINE DEDENT if str [ 1 ] == words [ i ] [ 0 ] : NEW_LINE INDENT second = True NEW_LINE DEDENT if first and second : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT str = ' ya ' NEW_LINE words = [ ' ah ' , ' oy ' , ' to ' , ' ha ' ] NEW_LINE if makeAndCheckString ( words , str ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximumOccurrence ( String s ) { int n = s . length ( ) ; HashMap < String , Integer > freq = new HashMap < String , Integer > ( ) ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { String temp = " " ; temp += s . charAt ( i ) ; if ( freq . containsKey ( temp ) ) { freq . put ( temp , freq . get ( temp ) + 1 ) ; } else { freq . put ( temp , 1 ) ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { String temp = " " ; temp += s . charAt ( i ) ; temp += s . charAt ( j ) ; if ( freq . containsKey ( temp ) ) freq . put ( temp , freq . get ( temp ) + 1 ) ; else freq . put ( temp , 1 ) ; } } int answer = Integer . MIN_VALUE ; for ( int it : freq . values ( ) ) answer = Math . max ( answer , it ) ; return answer ; } public static void main ( String [ ] args ) { String s = " xxxyy " ; System . out . print ( maximumOccurrence ( s ) ) ; } }

def maximumOccurrence ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE freq = { } NEW_LINE for i in s : NEW_LINE INDENT temp = " " NEW_LINE temp += i NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = " " NEW_LINE temp += s [ i ] NEW_LINE temp += s [ j ] NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT DEDENT answer = - 10 ** 9 NEW_LINE for it in freq : NEW_LINE INDENT answer = max ( answer , freq [ it ] ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " xxxyy " NEW_LINE print ( maximumOccurrence ( s ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int factors ( int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . containsKey ( i ) ) { prime . put ( i , prime . get ( i ) + 1 ) ; } else { prime . put ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . containsKey ( n ) ) { prime . put ( n , prime . get ( n ) + 1 ) ; } else { prime . put ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; for ( Map . Entry < Integer , Integer > it : prime . entrySet ( ) ) { ans1 *= 2 * it . getValue ( ) + 1 ; ans2 *= it . getValue ( ) + 1 ; } return ans1 - ans2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( factors ( n ) ) ; n = 8 ; System . out . println ( factors ( n ) ) ; } }

import math as mt NEW_LINE def factors ( n ) : NEW_LINE INDENT prime = dict ( ) NEW_LINE for i in range ( 2 , mt . ceil ( mt . sqrt ( n + 1 ) ) ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT if i in prime . keys ( ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT n = n // i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT if n in prime . keys ( ) : NEW_LINE INDENT prime [ n ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ n ] = 1 NEW_LINE DEDENT DEDENT ans1 = 1 NEW_LINE ans2 = 1 NEW_LINE for it in prime : NEW_LINE INDENT ans1 *= 2 * prime [ it ] + 1 NEW_LINE ans2 *= prime [ it ] + 1 NEW_LINE DEDENT return ans1 - ans2 NEW_LINE DEDENT n = 5 NEW_LINE print ( factors ( n ) ) NEW_LINE n = 8 NEW_LINE print ( factors ( n ) ) NEW_LINE


class GFG { static int countPairs ( int A , int B ) { int cnt = 0 ; for ( int i = 0 ; i <= A ; ++ i ) { for ( int j = i ; j <= A ; ++ j ) { int AND = i & j ; int OR = i | j ; if ( OR == A && AND == B ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int A = 3 , B = 0 ; System . out . println ( countPairs ( A , B ) ) ; } }

def countPairs ( A , B ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , A + 1 ) : NEW_LINE INDENT for j in range ( i , A + 1 ) : NEW_LINE INDENT AND = i & j NEW_LINE OR = i | j NEW_LINE if ( OR == A and AND == B ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 3 NEW_LINE B = 0 NEW_LINE print ( countPairs ( A , B ) ) NEW_LINE DEDENT