import java . util . * ; class GFG { static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static int numberSequence ( int n ) { if ( n == 1 ) return 1 ; String s = " " ; for ( int i = 1 ; i < n ; i ++ ) s += '1' ; s += '0' ; for ( int i = 1 ; i <= n ; i ++ ) s += '1' ; int num = binaryToDecimal ( s ) ; return num ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( numberSequence ( n ) ) ; } }

def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE l = len ( num ) NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def numberSequence ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT s = " " NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT s += '1' NEW_LINE DEDENT s += '0' NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT s += '1' NEW_LINE DEDENT num = binaryToDecimal ( s ) NEW_LINE return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( numberSequence ( n ) ) NEW_LINE DEDENT


import java . util . * ; public class GfG { static int digroot ( int n ) { int root = 0 ; while ( n > 0 || root > 9 ) { if ( n == 0 ) { n = root ; root = 0 ; } root += n % 10 ; n /= 10 ; } return root ; } public static void main ( String argc [ ] ) { int n = 65785412 ; System . out . println ( digroot ( n ) ) ; } }

import math NEW_LINE def digitalRoot ( num ) : NEW_LINE INDENT if ( num == "0" ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 0 , len ( num ) ) : NEW_LINE INDENT ans = ( ans + int ( num [ i ] ) ) % 9 NEW_LINE DEDENT if ( ans == 0 ) : NEW_LINE INDENT return 9 NEW_LINE DEDENT else : NEW_LINE INDENT return ans % 9 NEW_LINE DEDENT DEDENT num = "65785412" NEW_LINE print ( digitalRoot ( num ) ) NEW_LINE


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

def centeredOctahedral ( n ) : NEW_LINE INDENT return ( 2 * n + 1 ) * ( 2 * n * n + 2 * n + 3 ) // 3 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( centeredOctahedral ( n ) ) NEW_LINE n = 9 NEW_LINE print ( centeredOctahedral ( n ) ) NEW_LINE DEDENT


class GFG { static int setBitNumber ( int n ) { int k = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return ( int ) ( Math . pow ( 2 , k ) ) ; } public static void main ( String arg [ ] ) { int n = 273 ; System . out . print ( setBitNumber ( n ) ) ; } }

import math NEW_LINE def setBitNumber ( n ) : NEW_LINE INDENT k = int ( math . log ( n , 2 ) ) NEW_LINE return 2 ** k NEW_LINE DEDENT n = 273 NEW_LINE print ( setBitNumber ( n ) ) NEW_LINE


class Main { static void printUnsorted ( int arr [ ] , int n ) { int s = 0 , e = n - 1 , i , max , min ; for ( s = 0 ; s < n - 1 ; s ++ ) { if ( arr [ s ] > arr [ s + 1 ] ) break ; } if ( s == n - 1 ) { System . out . println ( " The ▁ complete ▁ array ▁ is ▁ sorted " ) ; return ; } for ( e = n - 1 ; e > 0 ; e -- ) { if ( arr [ e ] < arr [ e - 1 ] ) break ; } max = arr [ s ] ; min = arr [ s ] ; for ( i = s + 1 ; i <= e ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; if ( arr [ i ] < min ) min = arr [ i ] ; } for ( i = 0 ; i < s ; i ++ ) { if ( arr [ i ] > min ) { s = i ; break ; } } for ( i = n - 1 ; i >= e + 1 ; i -- ) { if ( arr [ i ] < max ) { e = i ; break ; } } System . out . println ( " ▁ The ▁ unsorted ▁ subarray ▁ which " + " ▁ makes ▁ the ▁ given ▁ array ▁ sorted ▁ lies " + " ▁ ▁ between ▁ the ▁ indices ▁ " + s + " ▁ and ▁ " + e ) ; return ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 } ; int arr_size = arr . length ; printUnsorted ( arr , arr_size ) ; } }

def printUnsorted ( arr , n ) : NEW_LINE INDENT e = n - 1 NEW_LINE for s in range ( 0 , n - 1 ) : NEW_LINE INDENT if arr [ s ] > arr [ s + 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if s == n - 1 : NEW_LINE INDENT print ( " The ▁ complete ▁ array ▁ is ▁ sorted " ) NEW_LINE exit ( ) NEW_LINE DEDENT e = n - 1 NEW_LINE while e > 0 : NEW_LINE INDENT if arr [ e ] < arr [ e - 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT e -= 1 NEW_LINE DEDENT max = arr [ s ] NEW_LINE min = arr [ s ] NEW_LINE for i in range ( s + 1 , e + 1 ) : NEW_LINE INDENT if arr [ i ] > max : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT if arr [ i ] < min : NEW_LINE INDENT min = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( s ) : NEW_LINE INDENT if arr [ i ] > min : NEW_LINE INDENT s = i NEW_LINE break NEW_LINE DEDENT DEDENT i = n - 1 NEW_LINE while i >= e + 1 : NEW_LINE INDENT if arr [ i ] < max : NEW_LINE INDENT e = i NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( " The ▁ unsorted ▁ subarray ▁ which ▁ makes ▁ the ▁ given ▁ array " ) NEW_LINE print ( " sorted ▁ lies ▁ between ▁ the ▁ indexes ▁ % d ▁ and ▁ % d " % ( s , e ) ) NEW_LINE DEDENT arr = [ 10 , 12 , 20 , 30 , 25 , 40 , 32 , 31 , 35 , 50 , 60 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printUnsorted ( arr , arr_size ) NEW_LINE


import java . io . * ; class GFG { static int minSwaps ( int arr [ ] , int n ) { int noOfOnes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) noOfOnes ++ ; } int x = noOfOnes ; int maxOnes = Integer . MIN_VALUE ; int preCompute [ ] = new int [ n ] ; if ( arr [ 0 ] == 1 ) preCompute [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { preCompute [ i ] = preCompute [ i - 1 ] + 1 ; } else preCompute [ i ] = preCompute [ i - 1 ] ; } for ( int i = x - 1 ; i < n ; i ++ ) { if ( i == ( x - 1 ) ) noOfOnes = preCompute [ i ] ; else noOfOnes = preCompute [ i ] - preCompute [ i - x ] ; if ( maxOnes < noOfOnes ) maxOnes = noOfOnes ; } int noOfZeroes = x - maxOnes ; return noOfZeroes ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 1 , 0 , 1 } ; int n = a . length ; System . out . println ( minSwaps ( a , n ) ) ; } }

def minSwaps ( arr , n ) : NEW_LINE INDENT noOfOnes = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT noOfOnes = noOfOnes + 1 NEW_LINE DEDENT DEDENT x = noOfOnes NEW_LINE maxOnes = - 2147483648 NEW_LINE preCompute = { } NEW_LINE if ( arr [ 0 ] == 1 ) : NEW_LINE INDENT preCompute [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT preCompute [ i ] = preCompute [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT preCompute [ i ] = preCompute [ i - 1 ] NEW_LINE DEDENT DEDENT for i in range ( x - 1 , n ) : NEW_LINE INDENT if ( i == ( x - 1 ) ) : NEW_LINE INDENT noOfOnes = preCompute [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT noOfOnes = preCompute [ i ] - preCompute [ i - x ] NEW_LINE DEDENT if ( maxOnes < noOfOnes ) : NEW_LINE INDENT maxOnes = noOfOnes NEW_LINE DEDENT DEDENT noOfZeroes = x - maxOnes NEW_LINE return noOfZeroes NEW_LINE DEDENT a = [ 1 , 0 , 1 , 0 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( minSwaps ( a , n ) ) NEW_LINE


import java . util . * ; class GFG { static int getSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { sum = sum + i ; } else { sum = sum + i ; sum = sum + ( n / i ) ; } } } return sum - n ; } static void printAliquot ( int n ) { System . out . printf ( " % d ▁ " , n ) ; TreeSet < Integer > s = new TreeSet < > ( ) ; s . add ( n ) ; int next = 0 ; while ( n > 0 ) { n = getSum ( n ) ; if ( s . contains ( n ) && n != s . last ( ) ) { System . out . print ( " \n Repeats ▁ with ▁ " + n ) ; break ; } System . out . print ( n + " ▁ " ) ; s . add ( n ) ; } } public static void main ( String [ ] args ) { printAliquot ( 12 ) ; } }

from math import sqrt NEW_LINE def getSum ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT if n // i == i : NEW_LINE INDENT summ += i NEW_LINE DEDENT else : NEW_LINE INDENT summ += i NEW_LINE summ += n // i NEW_LINE DEDENT DEDENT DEDENT return summ - n NEW_LINE DEDENT def printAliquot ( n ) : NEW_LINE INDENT print ( n , end = " ▁ " ) NEW_LINE s = set ( ) NEW_LINE s . add ( n ) NEW_LINE nextt = 0 NEW_LINE while n > 0 : NEW_LINE INDENT n = getSum ( n ) NEW_LINE if n in s : NEW_LINE INDENT print ( " Repeats ▁ with " , n ) NEW_LINE break NEW_LINE DEDENT print ( n , end = " ▁ " ) NEW_LINE s . add ( n ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT printAliquot ( 12 ) NEW_LINE DEDENT


class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static boolean coprime ( int a , int b ) { return ( gcd ( a , b ) == 1 ) ; } static void possibleTripletInRange ( int L , int R ) { boolean flag = false ; int possibleA = 0 , possibleB = 0 , possibleC = 0 ; for ( int a = L ; a <= R ; a ++ ) { for ( int b = a + 1 ; b <= R ; b ++ ) { for ( int c = b + 1 ; c <= R ; c ++ ) { if ( coprime ( a , b ) && coprime ( b , c ) && ! coprime ( a , c ) ) { flag = true ; possibleA = a ; possibleB = b ; possibleC = c ; break ; } } } } if ( flag == true ) { System . out . println ( " ( " + possibleA + " , ▁ " + possibleB + " , ▁ " + possibleC + " ) " + " ▁ is ▁ one ▁ such ▁ possible ▁ triplet ▁ " + " between ▁ " + L + " ▁ and ▁ " + R ) ; } else { System . out . println ( " No ▁ Such ▁ Triplet ▁ exists " + " between ▁ " + L + " ▁ and ▁ " + R ) ; } } public static void main ( String [ ] args ) { int L , R ; L = 2 ; R = 10 ; possibleTripletInRange ( L , R ) ; L = 23 ; R = 46 ; possibleTripletInRange ( L , R ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def coprime ( a , b ) : NEW_LINE INDENT return ( gcd ( a , b ) == 1 ) ; NEW_LINE DEDENT def possibleTripletInRange ( L , R ) : NEW_LINE INDENT flag = False ; NEW_LINE possibleA = 0 ; NEW_LINE possibleB = 0 ; NEW_LINE possibleC = 0 ; NEW_LINE for a in range ( L , R + 1 ) : NEW_LINE INDENT for b in range ( a + 1 , R + 1 ) : NEW_LINE INDENT for c in range ( b + 1 , R + 1 ) : NEW_LINE INDENT if ( coprime ( a , b ) and coprime ( b , c ) and coprime ( a , c ) == False ) : NEW_LINE INDENT flag = True ; NEW_LINE possibleA = a ; NEW_LINE possibleB = b ; NEW_LINE possibleC = c ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( flag == True ) : NEW_LINE INDENT print ( " ( " , possibleA , " , " , possibleB , " , " , possibleC , " ) ▁ is ▁ one ▁ such " , " possible ▁ triplet ▁ between " , L , " and " , R ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Such ▁ Triplet ▁ exists ▁ between " , L , " and " , R ) ; NEW_LINE DEDENT DEDENT L = 2 ; NEW_LINE R = 10 ; NEW_LINE possibleTripletInRange ( L , R ) ; NEW_LINE L = 23 ; NEW_LINE R = 46 ; NEW_LINE possibleTripletInRange ( L , R ) ; NEW_LINE


import java . util . * ; class GFG { static int Get_Minimum ( int [ ] q ) { int minimum = 0 ; int sum = 0 ; for ( int i = 0 ; i < q . length - 1 ; i ++ ) { sum += q [ i ] ; if ( sum < minimum ) minimum = sum ; } return minimum ; } static int [ ] Find_Permutation ( int [ ] q , int n ) { int [ ] p = new int [ n ] ; int min_value = Get_Minimum ( q ) ; p [ 0 ] = 1 - min_value ; for ( int i = 0 ; i < n - 1 ; i ++ ) p [ i + 1 ] = p [ i ] + q [ i ] ; boolean okay = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] < 1 || p [ i ] > n ) okay = false ; Set < Integer > w = new HashSet < > ( ) ; if ( w . size ( ) != n ) okay = true ; } if ( okay ) return p ; else return new int [ ] { - 1 } ; } public static void main ( String args [ ] ) { int [ ] q = { - 2 , 1 } ; int n = q . length + 1 ; System . out . print ( " [ ▁ " ) ; for ( int i : Find_Permutation ( q , n ) ) System . out . print ( i + " ▁ " ) ; System . out . print ( " ] " ) ; } }

def Get_Minimum ( q ) : NEW_LINE INDENT minimum = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT sum += q [ i ] NEW_LINE if sum < minimum : NEW_LINE INDENT minimum = sum NEW_LINE DEDENT DEDENT return minimum NEW_LINE DEDENT def Find_Permutation ( q ) : NEW_LINE INDENT p = [ 0 ] * n NEW_LINE min_value = Get_Minimum ( q ) NEW_LINE p [ 0 ] = 1 - min_value NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT p [ i + 1 ] = p [ i ] + q [ i ] NEW_LINE DEDENT okay = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT if p [ i ] < 1 or p [ i ] > n : NEW_LINE INDENT okay = False NEW_LINE DEDENT DEDENT if len ( set ( p ) ) != n : NEW_LINE INDENT okay = False NEW_LINE DEDENT if okay : NEW_LINE INDENT return p NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT q = [ - 2 , 1 ] NEW_LINE n = len ( q ) + 1 NEW_LINE print ( Find_Permutation ( q ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static float polyarea ( float n , float a ) { if ( a < 0 && n < 0 ) return - 1 ; float A = ( a * a * n ) / ( float ) ( 4 * Math . tan ( ( 180 / n ) * 3.14159 / 180 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 9 , n = 6 ; System . out . println ( polyarea ( n , a ) ) ; } }

from math import tan NEW_LINE def polyarea ( n , a ) : NEW_LINE INDENT if ( a < 0 and n < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = ( a * a * n ) / ( 4 * tan ( ( 180 / n ) * 3.14159 / 180 ) ) NEW_LINE return A NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 9 NEW_LINE n = 6 NEW_LINE print ( ' { 0 : . 6 } ' . format ( polyarea ( n , a ) ) ) NEW_LINE DEDENT


class shortest_path { static int INF = Integer . MAX_VALUE , N = 4 ; static int minCost ( int cost [ ] [ ] ) { int dist [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) dist [ i ] = INF ; dist [ 0 ] = 0 ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ; return dist [ N - 1 ] ; } public static void main ( String args [ ] ) { int cost [ ] [ ] = { { 0 , 15 , 80 , 90 } , { INF , 0 , 40 , 50 } , { INF , INF , 0 , 70 } , { INF , INF , INF , 0 } } ; System . out . println ( " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " + N + " ▁ is ▁ " + minCost ( cost ) ) ; } }

INF = 2147483647 NEW_LINE N = 4 NEW_LINE def minCost ( cost ) : NEW_LINE INDENT dist = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT dist [ i ] = INF NEW_LINE DEDENT dist [ 0 ] = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) : NEW_LINE INDENT dist [ j ] = dist [ i ] + cost [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return dist [ N - 1 ] NEW_LINE DEDENT cost = [ [ 0 , 15 , 80 , 90 ] , [ INF , 0 , 40 , 50 ] , [ INF , INF , 0 , 70 ] , [ INF , INF , INF , 0 ] ] NEW_LINE print ( " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " , N , " ▁ is ▁ " , minCost ( cost ) ) NEW_LINE


import java . io . * ; class GFG { static void rotateMatrix ( int N , int mat [ ] [ ] ) { for ( int x = 0 ; x < N / 2 ; x ++ ) { for ( int y = x ; y < N - x - 1 ; y ++ ) { int temp = mat [ x ] [ y ] ; mat [ x ] [ y ] = mat [ y ] [ N - 1 - x ] ; mat [ y ] [ N - 1 - x ] = mat [ N - 1 - x ] [ N - 1 - y ] ; mat [ N - 1 - x ] [ N - 1 - y ] = mat [ N - 1 - y ] [ x ] ; mat [ N - 1 - y ] [ x ] = temp ; } } } static void displayMatrix ( int N , int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( " ▁ " + mat [ i ] [ j ] ) ; System . out . print ( " \n " ) ; } System . out . print ( " \n " ) ; } public static void main ( String [ ] args ) { int N = 4 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotateMatrix ( N , mat ) ; displayMatrix ( N , mat ) ; } }

N = 4 NEW_LINE def rotateMatrix ( mat ) : NEW_LINE INDENT for x in range ( 0 , int ( N / 2 ) ) : NEW_LINE INDENT for y in range ( x , N - x - 1 ) : NEW_LINE INDENT temp = mat [ x ] [ y ] NEW_LINE mat [ x ] [ y ] = mat [ y ] [ N - 1 - x ] NEW_LINE mat [ y ] [ N - 1 - x ] = mat [ N - 1 - x ] [ N - 1 - y ] NEW_LINE mat [ N - 1 - x ] [ N - 1 - y ] = mat [ N - 1 - y ] [ x ] NEW_LINE mat [ N - 1 - y ] [ x ] = temp NEW_LINE DEDENT DEDENT DEDENT def displayMatrix ( mat ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT mat = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] NEW_LINE mat = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE rotateMatrix ( mat ) NEW_LINE displayMatrix ( mat ) NEW_LINE


class GFG { static void kthString ( int n , int k ) { int total = 0 ; int i = 1 ; while ( total < k ) { total = total + n - i ; i ++ ; } int first_y_position = i - 1 ; int second_y_position = k - ( total - n + first_y_position ) ; for ( int j = 1 ; j < first_y_position ; j ++ ) System . out . print ( " x " ) ; System . out . print ( " y " ) ; int j = first_y_position + 1 ; while ( second_y_position > 1 ) { System . out . print ( " x " ) ; second_y_position -- ; j ++ ; } System . out . print ( " y " ) ; while ( j < n ) { System . out . print ( " x " ) ; j ++ ; } } public static void main ( String [ ] args ) { int n = 5 ; int k = 7 ; kthString ( n , k ) ; } }

def kthString ( n , k ) : NEW_LINE INDENT total = 0 NEW_LINE i = 1 NEW_LINE while ( total < k ) : NEW_LINE INDENT total = total + n - i NEW_LINE i += 1 NEW_LINE DEDENT first_y_position = i - 1 NEW_LINE second_y_position = k - ( total - n + first_y_position ) NEW_LINE for j in range ( 1 , first_y_position , 1 ) : NEW_LINE INDENT print ( " x " , end = " " ) NEW_LINE DEDENT print ( " y " , end = " " ) NEW_LINE j = first_y_position + 1 NEW_LINE while ( second_y_position > 1 ) : NEW_LINE INDENT print ( " x " , end = " " ) NEW_LINE second_y_position -= 1 NEW_LINE j += 1 NEW_LINE DEDENT print ( " y " , end = " " ) NEW_LINE while ( j < n ) : NEW_LINE INDENT print ( " x " ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE k = 7 NEW_LINE kthString ( n , k ) NEW_LINE DEDENT


class GFG { static void findDimen ( int H , int A ) { if ( H * H < 4 * A ) { System . out . println ( " Not ▁ Possible " ) ; return ; } double apb = Math . sqrt ( H * H + 4 * A ) ; double asb = Math . sqrt ( H * H - 4 * A ) ; System . out . println ( " P ▁ = ▁ " + Math . round ( ( ( apb - asb ) / 2.0 ) * 100.0 ) / 100.0 ) ; System . out . print ( " B ▁ = ▁ " + Math . round ( ( ( apb + asb ) / 2.0 ) * 100.0 ) / 100.0 ) ; } public static void main ( String [ ] args ) { int H = 5 ; int A = 6 ; findDimen ( H , A ) ; } }

from math import sqrt NEW_LINE def findDimen ( H , A ) : NEW_LINE INDENT if H * H < 4 * A : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT apb = sqrt ( H * H + 4 * A ) NEW_LINE asb = sqrt ( H * H - 4 * A ) NEW_LINE print ( " P ▁ = ▁ " , " % .2f " % ( ( apb - asb ) / 2.0 ) ) NEW_LINE print ( " B ▁ = ▁ " , " % .2f " % ( ( apb + asb ) / 2.0 ) ) NEW_LINE DEDENT H = 5 NEW_LINE A = 6 NEW_LINE findDimen ( H , A ) NEW_LINE


import java . util . HashSet ; class GFG { public static int countIdenticalRows ( int mat [ ] [ ] ) { int count = 0 ; for ( int i = 0 ; i < mat . length ; i ++ ) { HashSet < Integer > hs = new HashSet < > ( ) ; for ( int j = 0 ; j < mat [ i ] . length ; j ++ ) { hs . add ( mat [ i ] [ j ] ) ; } if ( hs . size ( ) == 1 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 2 , 3 } , { 5 , 5 , 5 } } ; System . out . print ( countIdenticalRows ( mat ) ) ; } }

def countIdenticalRows ( mat ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( mat ) ) : NEW_LINE INDENT hs = dict ( ) NEW_LINE for j in range ( len ( mat [ i ] ) ) : NEW_LINE INDENT hs [ mat [ i ] [ j ] ] = 1 NEW_LINE DEDENT if ( len ( hs ) == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT mat = [ [ 1 , 1 , 1 ] , [ 1 , 2 , 3 ] , [ 5 , 5 , 5 ] ] NEW_LINE print ( countIdenticalRows ( mat ) ) NEW_LINE


class GFG { static int n = 3 ; static int MAX = 30 ; static int dp [ ] [ ] [ ] = new int [ n ] [ n ] [ MAX ] ; static boolean v [ ] [ ] [ ] = new boolean [ n ] [ n ] [ MAX ] ; static int findCount ( int mat [ ] [ ] , int i , int j , int m ) { if ( i == 0 && j == 0 ) { if ( m == mat [ 0 ] [ 0 ] ) return 1 ; else return 0 ; } if ( m < 0 ) return 0 ; if ( i < 0 || j < 0 ) return 0 ; if ( v [ i ] [ j ] [ m ] ) return dp [ i ] [ j ] [ m ] ; v [ i ] [ j ] [ m ] = true ; dp [ i ] [ j ] [ m ] = findCount ( mat , i - 1 , j , m - mat [ i ] [ j ] ) + findCount ( mat , i , j - 1 , m - mat [ i ] [ j ] ) ; return dp [ i ] [ j ] [ m ] ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int m = 5 ; System . out . println ( findCount ( mat , n - 1 , n - 1 , m ) ) ; } }

n = 3 NEW_LINE MAX = 60 NEW_LINE dp = [ [ [ 0 for i in range ( 30 ) ] for i in range ( 30 ) ] for i in range ( MAX + 1 ) ] NEW_LINE v = [ [ [ 0 for i in range ( 30 ) ] for i in range ( 30 ) ] for i in range ( MAX + 1 ) ] NEW_LINE def findCount ( mat , i , j , m ) : NEW_LINE INDENT if ( i == 0 and j == 0 ) : NEW_LINE INDENT if ( m == mat [ 0 ] [ 0 ] ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( m < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i < 0 or j < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( v [ i ] [ j ] [ m ] > 0 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ m ] NEW_LINE DEDENT v [ i ] [ j ] [ m ] = True NEW_LINE dp [ i ] [ j ] [ m ] = ( findCount ( mat , i - 1 , j , m - mat [ i ] [ j ] ) + findCount ( mat , i , j - 1 , m - mat [ i ] [ j ] ) ) NEW_LINE return dp [ i ] [ j ] [ m ] NEW_LINE DEDENT mat = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] NEW_LINE m = 5 NEW_LINE print ( findCount ( mat , n - 1 , n - 1 , m ) ) NEW_LINE


class GFG { static int findCnt ( int [ ] arr , int n , int k ) { int ret = 0 ; int i = 0 ; while ( i < n ) { int j = i + 1 ; while ( j < n && arr [ j ] >= arr [ j - 1 ] ) j ++ ; int x = Math . max ( 0 , j - i - k ) ; ret += ( ( j - i ) * ( j - i + 1 ) ) / 2 - ( x * ( x + 1 ) ) / 2 ; i = j ; } return ret ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; int k = 2 ; System . out . println ( findCnt ( arr , n , k ) ) ; } }

def findCnt ( arr , n , k ) : NEW_LINE INDENT ret = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT j = i + 1 ; NEW_LINE while ( j < n and arr [ j ] >= arr [ j - 1 ] ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT x = max ( 0 , j - i - k ) ; NEW_LINE ret += ( ( j - i ) * ( j - i + 1 ) ) // 2 -   \ NEW_LINE INDENT ( x * ( x + 1 ) ) / 2 ; NEW_LINE DEDENT i = j ; NEW_LINE DEDENT return ret ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( findCnt ( arr , n , k ) ) ; NEW_LINE DEDENT


class GFG { static int gcd ( int a , int b ) { if ( ( a % b ) == 0 ) return b ; return gcd ( b , a % b ) ; } static int firstFactorialDivisibleNumber ( int x ) { int i = 1 ; int new_x = x ; for ( i = 1 ; i < x ; i ++ ) { new_x /= gcd ( i , new_x ) ; if ( new_x == 1 ) break ; } return i ; } static int countFactorialXNotY ( int x , int y ) { return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) ; } public static void main ( String [ ] args ) { int x = 15 , y = 25 ; System . out . print ( countFactorialXNotY ( x , y ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( ( a % b ) == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def firstFactorialDivisibleNumber ( x ) : NEW_LINE INDENT i = 1 NEW_LINE new_x = x NEW_LINE for i in range ( 1 , x ) : NEW_LINE INDENT new_x /= gcd ( i , new_x ) NEW_LINE if ( new_x == 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT def countFactorialXNotY ( x , y ) : NEW_LINE INDENT return ( firstFactorialDivisibleNumber ( y ) - firstFactorialDivisibleNumber ( x ) ) NEW_LINE DEDENT x = 15 NEW_LINE y = 25 NEW_LINE print ( countFactorialXNotY ( x , y ) ) NEW_LINE


import java . io . * ; class GFG { static int findNthOddDigitNumber ( int n ) { int count = 0 ; for ( int i = 1 ; ; i ++ ) { int num = i ; boolean isMadeOfOdd = true ; while ( num != 0 ) { if ( num % 10 == 0 || num % 10 == 2 || num % 10 == 4 || num % 10 == 6 || num % 10 == 8 ) { isMadeOfOdd = false ; break ; } num = num / 10 ; } if ( isMadeOfOdd == true ) count ++ ; if ( count == n ) return i ; } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findNthOddDigitNumber ( n ) ) ; } }

def findNthOddDigitNumber ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while True : NEW_LINE INDENT num = i NEW_LINE isMadeOfOdd = True NEW_LINE while num != 0 : NEW_LINE INDENT if ( num % 10 == 0 or num % 10 == 2 or num % 10 == 4 or num % 10 == 6 or num % 10 == 8 ) : NEW_LINE INDENT isMadeOfOdd = False NEW_LINE break NEW_LINE DEDENT num /= 10 NEW_LINE DEDENT if isMadeOfOdd == True : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == n : NEW_LINE INDENT return i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE print ( findNthOddDigitNumber ( n ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void findTangent ( int A , int x , int y ) { int dif = A - x * 2 ; if ( y == ( 2 * x - x * x ) ) { if ( dif < 0 ) System . out . println ( " y ▁ = ▁ " + dif + " x " + ( x * dif + y ) ) ; else if ( dif > 0 ) System . out . println ( " y ▁ = ▁ " + dif + " x + " + - x * dif + y ) ; else System . out . println ( " Not ▁ possible " ) ; } } public static void main ( String args [ ] ) { int A = 2 , x = 2 , y = 0 ; findTangent ( A , x , y ) ; } }

def findTangent ( A , x , y ) : NEW_LINE INDENT dif = A - x * 2 NEW_LINE if y == ( 2 * x - x * x ) : NEW_LINE INDENT if dif < 0 : NEW_LINE INDENT print ( " y ▁ = " , dif , " x " , ( x * dif ) + ( y ) ) NEW_LINE DEDENT elif dif > 0 : NEW_LINE INDENT print ( " y ▁ = " , dif , " x + " , - x * dif + y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , x , y = 2 , 2 , 0 NEW_LINE findTangent ( A , x , y ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int sumSubarrayMins ( int A [ ] , int n ) { int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; Stack < pair > s1 = new Stack < pair > ( ) ; Stack < pair > s2 = new Stack < pair > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int cnt = 1 ; while ( ! s1 . isEmpty ( ) && ( s1 . peek ( ) . first ) > A [ i ] ) { cnt += s1 . peek ( ) . second ; s1 . pop ( ) ; } s1 . push ( new pair ( A [ i ] , cnt ) ) ; left [ i ] = cnt ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { int cnt = 1 ; while ( ! s2 . isEmpty ( ) && ( s2 . peek ( ) . first ) >= A [ i ] ) { cnt += s2 . peek ( ) . second ; s2 . pop ( ) ; } s2 . push ( new pair ( A [ i ] , cnt ) ) ; right [ i ] = cnt ; } int result = 0 ; for ( int i = 0 ; i < n ; ++ i ) result = ( result + A [ i ] * left [ i ] * right [ i ] ) ; return result ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 1 , 2 , 4 } ; int n = A . length ; System . out . println ( sumSubarrayMins ( A , n ) ) ; } }

def sumSubarrayMins ( A , n ) : NEW_LINE INDENT left , right = [ None ] * n , [ None ] * n NEW_LINE s1 , s2 = [ ] , [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT cnt = 1 NEW_LINE while len ( s1 ) > 0 and s1 [ - 1 ] [ 0 ] > A [ i ] : NEW_LINE INDENT cnt += s1 [ - 1 ] [ 1 ] NEW_LINE s1 . pop ( ) NEW_LINE DEDENT s1 . append ( [ A [ i ] , cnt ] ) NEW_LINE left [ i ] = cnt NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT cnt = 1 NEW_LINE while len ( s2 ) > 0 and s2 [ - 1 ] [ 0 ] > A [ i ] : NEW_LINE INDENT cnt += s2 [ - 1 ] [ 1 ] NEW_LINE s2 . pop ( ) NEW_LINE DEDENT s2 . append ( [ A [ i ] , cnt ] ) NEW_LINE right [ i ] = cnt NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT result += A [ i ] * left [ i ] * right [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 1 , 2 , 4 ] NEW_LINE n = len ( A ) NEW_LINE print ( sumSubarrayMins ( A , n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int missingNum ( int arr [ ] , int n ) { List < Integer > list = new ArrayList < > ( arr . length ) ; for ( int i : arr ) { list . add ( Integer . valueOf ( i ) ) ; } int minvalue = Collections . min ( list ) ; ; int xornum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xornum ^= ( minvalue ) ^ arr [ i ] ; minvalue ++ ; } return xornum ^ minvalue ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 12 , 11 , 15 } ; int n = arr . length ; System . out . println ( missingNum ( arr , n ) ) ; } }

def missingNum ( arr , n ) : NEW_LINE INDENT minvalue = min ( arr ) NEW_LINE xornum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT xornum ^= ( minvalue ) ^ arr [ i ] NEW_LINE minvalue = minvalue + 1 NEW_LINE DEDENT return xornum ^ minvalue NEW_LINE DEDENT arr = [ 13 , 12 , 11 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( missingNum ( arr , n ) ) NEW_LINE


public class GFG { static void profitLoss ( int N , int M ) { if ( N == M ) System . out . print ( " No ▁ Profit ▁ nor ▁ Loss " ) ; else { float result = 0 ; result = ( float ) ( Math . abs ( N - M ) ) / M ; if ( N - M < 0 ) System . out . print ( " Loss ▁ = ▁ - " + result * 100 + " % " ) ; else System . out . print ( " Profit ▁ = ▁ " + result * 100 + " % " ) ; } } public static void main ( String [ ] args ) { int N = 8 , M = 9 ; profitLoss ( N , M ) ; } }

def profitLoss ( N , M ) : NEW_LINE INDENT if ( N == M ) : NEW_LINE INDENT print ( " No ▁ Profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT else : NEW_LINE INDENT result = 0.0 NEW_LINE result = float ( abs ( N - M ) ) / M NEW_LINE if ( N - M < 0 ) : NEW_LINE INDENT print ( " Loss ▁ = ▁ - " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Profit ▁ = ▁ " , ' { 0 : . 6 } ' . format ( result * 100 ) , " % " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE M = 9 NEW_LINE profitLoss ( N , M ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean arePermutations ( int a [ ] , int b [ ] , int n , int m ) { int sum1 = 0 , sum2 = 0 , mul1 = 1 , mul2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 += a [ i ] ; mul1 *= a [ i ] ; } for ( int i = 0 ; i < m ; i ++ ) { sum2 += b [ i ] ; mul2 *= b [ i ] ; } return ( ( sum1 == sum2 ) && ( mul1 == mul2 ) ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 } ; int b [ ] = { 3 , 1 , 2 } ; int n = a . length ; int m = b . length ; if ( arePermutations ( a , b , n , m ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def arePermutations ( a , b , n , m ) : NEW_LINE INDENT sum1 , sum2 , mul1 , mul2 = 0 , 0 , 1 , 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum1 += a [ i ] NEW_LINE mul1 *= a [ i ] NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT sum2 += b [ i ] NEW_LINE mul2 *= b [ i ] NEW_LINE DEDENT return ( ( sum1 == sum2 ) and ( mul1 == mul2 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 2 ] NEW_LINE b = [ 3 , 1 , 2 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE if arePermutations ( a , b , n , m ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static void ladder_pattern ( int N ) { for ( int i = 0 ; i <= N ; i ++ ) { System . out . println ( " * ▁ ▁ ▁ * " ) ; System . out . println ( " * ▁ ▁ ▁ * " ) ; if ( i < N ) { System . out . println ( " * * * * * " ) ; } } } public static void main ( String args [ ] ) { int N = 3 ; ladder_pattern ( N ) ; } }

def ladder_pattern ( N ) : NEW_LINE INDENT for i in range ( N + 1 ) : NEW_LINE INDENT print ( " * ▁ ▁ ▁ * " ) ; NEW_LINE print ( " * ▁ ▁ ▁ * " ) ; NEW_LINE if ( i < N ) : NEW_LINE INDENT print ( " * * * * * " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 ; NEW_LINE ladder_pattern ( N ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxLines ( int n , int x1 [ ] , int y1 [ ] , int x2 [ ] , int y2 [ ] ) { Set < Double > s = new HashSet < Double > ( ) ; double slope ; for ( int i = 0 ; i < n ; ++ i ) { if ( x1 [ i ] == x2 [ i ] ) slope = Integer . MAX_VALUE ; else slope = ( y2 [ i ] - y1 [ i ] ) * 1.0 / ( x2 [ i ] - x1 [ i ] ) * 1.0 ; s . add ( slope ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int n = 2 , x1 [ ] = { 1 , 2 } , y1 [ ] = { 1 , 2 } , x2 [ ] = { 2 , 4 } , y2 [ ] = { 2 , 10 } ; System . out . print ( maxLines ( n , x1 , y1 , x2 , y2 ) ) ; } }

import sys NEW_LINE def maxLines ( n , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT s = [ ] ; NEW_LINE slope = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( x1 [ i ] == x2 [ i ] ) : NEW_LINE INDENT slope = sys . maxsize ; NEW_LINE DEDENT else : NEW_LINE INDENT slope = ( y2 [ i ] - y1 [ i ] ) * 1.0 / ( x2 [ i ] - x1 [ i ] ) * 1.0 ; NEW_LINE DEDENT s . append ( slope ) ; NEW_LINE DEDENT return len ( s ) ; NEW_LINE DEDENT n = 2 ; NEW_LINE x1 = [ 1 , 2 ] ; NEW_LINE y1 = [ 1 , 2 ] ; NEW_LINE x2 = [ 2 , 4 ] ; NEW_LINE y2 = [ 2 , 10 ] ; NEW_LINE print ( maxLines ( n , x1 , y1 , x2 , y2 ) ) ; NEW_LINE


import java . util . * ; class GFG { static boolean productsDistinct ( int N ) { String s = " " ; while ( N > 0 ) { s += ( char ) ( N % 10 + '0' ) ; N /= 10 ; } s = reverse ( s ) ; int sz = s . length ( ) ; HashSet < Integer > se = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < sz ; i ++ ) { int product = 1 ; for ( int j = i ; j < sz ; j ++ ) { product *= ( int ) ( s . charAt ( j ) - '0' ) ; if ( se . contains ( product ) ) return false ; else se . add ( product ) ; } } return true ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { int N = 2345 ; if ( productsDistinct ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def productsDistinct ( N ) : NEW_LINE INDENT s = " " NEW_LINE while ( N ) : NEW_LINE INDENT s += chr ( N % 10 + ord ( '0' ) ) NEW_LINE N //= 10 NEW_LINE DEDENT s = s [ : : - 1 ] NEW_LINE sz = len ( s ) NEW_LINE se = [ ] NEW_LINE for i in range ( sz ) : NEW_LINE INDENT product = 1 NEW_LINE for j in range ( i , sz , 1 ) : NEW_LINE INDENT product *= ord ( s [ j ] ) - ord ( '0' ) NEW_LINE for p in range ( len ( se ) ) : NEW_LINE INDENT if se [ p ] == product : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT se . append ( product ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2345 NEW_LINE if ( productsDistinct ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static boolean isPalindrome ( String s , int i , int j ) { while ( i < j ) { if ( s . charAt ( i ) != s . charAt ( j ) ) return false ; i ++ ; j -- ; } return true ; } static int getIndex ( String s1 , String s2 , int len ) { int i = 0 , j = len - 1 ; while ( i < j ) { if ( s1 . charAt ( i ) != s2 . charAt ( j ) ) { break ; } i ++ ; j -- ; } if ( i == j ) { return i - 1 ; } else if ( isPalindrome ( s2 , i , j ) ) return i - 1 ; else if ( isPalindrome ( s1 , i , j ) ) return j ; return - 1 ; } public static void main ( String args [ ] ) { String s1 = " abcdf " , s2 = " sfgba " ; int len = s1 . length ( ) ; System . out . println ( getIndex ( s1 , s2 , len ) ) ; } }

def isPalindrome ( s , i , j ) : NEW_LINE INDENT while ( i < j ) : NEW_LINE INDENT if ( s [ i ] != s [ j ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def getIndex ( s1 , s2 , length ) : NEW_LINE INDENT i = 0 ; j = length - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( s1 [ i ] != s2 [ j ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT return i - 1 ; NEW_LINE DEDENT elif ( isPalindrome ( s2 , i , j ) ) : NEW_LINE INDENT return i - 1 ; NEW_LINE DEDENT elif ( isPalindrome ( s1 , i , j ) ) : NEW_LINE INDENT return j ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abcdf " ; NEW_LINE s2 = " sfgba " ; NEW_LINE length = len ( s1 ) ; NEW_LINE print ( getIndex ( s1 , s2 , length ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static double maxDistance ( int n , int fuel ) { double dist_covered = 0 ; while ( n > 0 ) { dist_covered += ( double ) fuel / n ; n -= 1 ; } return dist_covered ; } public static void main ( String [ ] args ) { int n = 3 ; int fuel = 100 ; System . out . println ( " Maximum ▁ distance ▁ possible ▁ with ▁ " + n + " ▁ bikes ▁ is ▁ " + maxDistance ( n , fuel ) ) ; } }

def maxDistance ( n , fuel ) : NEW_LINE INDENT dist_covered = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT dist_covered = dist_covered + ( fuel / n ) NEW_LINE n = n - 1 NEW_LINE DEDENT return dist_covered NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE fuel = 100 NEW_LINE print ( " Maximum ▁ distance ▁ possible ▁ with " , n , " bikes ▁ is " , maxDistance ( n , fuel ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void modularInverse ( int n , int prime ) { int dp [ ] = new int [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ prime % i ] * ( prime - prime / i ) % prime ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( dp [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 , prime = 17 ; modularInverse ( n , prime ) ; } }

def modularInverse ( n , prime ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE dp [ 0 ] = dp [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ prime % i ] * ( prime - prime // i ) % prime NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( dp [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE prime = 17 NEW_LINE modularInverse ( n , prime ) NEW_LINE


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public static int CountSpecialPalindrome ( String str ) { int n = str . length ( ) ; int result = 0 ; int [ ] sameChar = new int [ n ] ; for ( int v = 0 ; v < n ; v ++ ) sameChar [ v ] = 0 ; int i = 0 ; while ( i < n ) { int sameCharCount = 1 ; int j = i + 1 ; while ( j < n && str . charAt ( i ) == str . charAt ( j ) ) { sameCharCount ++ ; j ++ ; } result += ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; sameChar [ i ] = sameCharCount ; i = j ; } for ( int j = 1 ; j < n ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) ) sameChar [ j ] = sameChar [ j - 1 ] ; if ( j > 0 && j < ( n - 1 ) && ( str . charAt ( j - 1 ) == str . charAt ( j + 1 ) && str . charAt ( j ) != str . charAt ( j - 1 ) ) ) result += Math . min ( sameChar [ j - 1 ] , sameChar [ j + 1 ] ) ; } return result - n ; } public static void main ( String args [ ] ) { String str = " abccba " ; System . out . print ( CountSpecialPalindrome ( str ) ) ; } }

def CountSpecialPalindrome ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE result = 0 ; NEW_LINE sameChar = [ 0 ] * n ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT sameCharCount = 1 ; NEW_LINE j = i + 1 ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( str [ i ] != str [ j ] ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sameCharCount += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT result += int ( sameCharCount * ( sameCharCount + 1 ) / 2 ) ; NEW_LINE sameChar [ i ] = sameCharCount ; NEW_LINE i = j ; NEW_LINE DEDENT for j in range ( 1 , n ) : NEW_LINE INDENT if ( str [ j ] == str [ j - 1 ] ) : NEW_LINE INDENT sameChar [ j ] = sameChar [ j - 1 ] ; NEW_LINE DEDENT if ( j > 0 and j < ( n - 1 ) and ( str [ j - 1 ] == str [ j + 1 ] and str [ j ] != str [ j - 1 ] ) ) : NEW_LINE INDENT result += ( sameChar [ j - 1 ] if ( sameChar [ j - 1 ] < sameChar [ j + 1 ] ) else sameChar [ j + 1 ] ) ; NEW_LINE DEDENT DEDENT return result - n ; NEW_LINE DEDENT str = " abccba " ; NEW_LINE print ( CountSpecialPalindrome ( str ) ) ; NEW_LINE


class GFG { static boolean LiesInsieRectangle ( int a , int b , int x , int y ) { if ( x - y - b <= 0 && x - y + b >= 0 && x + y - 2 * a + b <= 0 && x + y - b >= 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 7 , b = 2 , x = 4 , y = 5 ; if ( LiesInsieRectangle ( a , b , x , y ) ) System . out . println ( " Given ▁ point ▁ lies ▁ " + " inside ▁ the ▁ rectangle " ) ; else System . out . println ( " Given ▁ point ▁ does ▁ not ▁ " + " lie ▁ on ▁ the ▁ rectangle " ) ; } }

def LiesInsieRectangle ( a , b , x , y ) : NEW_LINE INDENT if ( x - y - b <= 0 and x - y + b >= 0 and x + y - 2 * a + b <= 0 and x + y - b >= 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , x , y = 7 , 2 , 4 , 5 NEW_LINE if LiesInsieRectangle ( a , b , x , y ) : NEW_LINE INDENT print ( " Given ▁ point ▁ lies ▁ inside " " ▁ the ▁ rectangle " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Given ▁ point ▁ does ▁ not ▁ lie " " ▁ on ▁ the ▁ rectangle " ) NEW_LINE DEDENT DEDENT


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

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


import java . util . * ; class GFG { static void FindRank ( int arr [ ] , int length ) { System . out . print ( "1" + " ▁ " ) ; for ( int i = 1 ; i < arr . length ; i ++ ) { int rank = 1 ; for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) rank ++ ; } System . out . print ( rank + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 88 , 14 , 69 , 30 , 29 , 89 } ; int len = arr . length ; FindRank ( arr , len ) ; } }

def FindRank ( arr , length ) : NEW_LINE INDENT print ( 1 , end = " ▁ " ) NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT rank = 1 NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT rank = rank + 1 NEW_LINE DEDENT DEDENT print ( rank , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 88 , 14 , 69 , 30 , 29 , 89 ] NEW_LINE length = len ( arr ) NEW_LINE FindRank ( arr , length ) NEW_LINE DEDENT


class GFG { static int MAX = 100001 ; static int [ ] perfectDiv = new int [ MAX ] ; static void precomputeCounts ( ) { for ( int i = 1 ; i * i < MAX ; ++ i ) { for ( int j = i * i ; j < MAX ; j += i * i ) ++ perfectDiv [ j ] ; } } static int countPerfectDivisors ( int n ) { return perfectDiv [ n ] ; } public static void main ( String [ ] args ) { precomputeCounts ( ) ; int n = 16 ; System . out . println ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; n = 12 ; System . out . println ( " Total ▁ perfect ▁ divisors ▁ of ▁ " + n + " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; } }

MAX = 100001 NEW_LINE perfectDiv = [ 0 ] * MAX NEW_LINE def precomputeCounts ( ) : NEW_LINE INDENT i = 1 NEW_LINE while i * i < MAX : NEW_LINE INDENT for j in range ( i * i , MAX , i * i ) : NEW_LINE INDENT perfectDiv [ j ] += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT def countPerfectDivisors ( n ) : NEW_LINE INDENT return perfectDiv [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT precomputeCounts ( ) NEW_LINE n = 16 NEW_LINE print ( " Total ▁ perfect ▁ divisors ▁ of ▁ " , n , " ▁ = ▁ " , countPerfectDivisors ( n ) ) NEW_LINE n = 12 NEW_LINE print ( " Total ▁ perfect ▁ divisors ▁ of ▁ " , n , " ▁ = ▁ " , countPerfectDivisors ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void kFactors ( int n , int k ) { ArrayList < Integer > P = new ArrayList < Integer > ( ) ; while ( n % 2 == 0 ) { P . add ( 2 ) ; n /= 2 ; } for ( int i = 3 ; i * i <= n ; i = i + 2 ) { while ( n % i == 0 ) { n = n / i ; P . add ( i ) ; } } if ( n > 2 ) P . add ( n ) ; if ( P . size ( ) < k ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) System . out . print ( P . get ( i ) + " , ▁ " ) ; int product = 1 ; for ( int i = k - 1 ; i < P . size ( ) ; i ++ ) product = product * P . get ( i ) ; System . out . println ( product ) ; } public static void main ( String [ ] args ) { int n = 54 , k = 3 ; kFactors ( n , k ) ; } }

import math as mt NEW_LINE def kFactors ( n , k ) : NEW_LINE INDENT a = list ( ) NEW_LINE while n % 2 == 0 : NEW_LINE INDENT a . append ( 2 ) NEW_LINE n = n // 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( n ) ) , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT n = n / i ; NEW_LINE a . append ( i ) NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT a . append ( n ) NEW_LINE DEDENT if len ( a ) < k : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( k - 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " , ▁ " ) NEW_LINE DEDENT product = 1 NEW_LINE for i in range ( k - 1 , len ( a ) ) : NEW_LINE INDENT product *= a [ i ] NEW_LINE DEDENT print ( product ) NEW_LINE DEDENT n , k = 54 , 3 NEW_LINE kFactors ( n , k ) NEW_LINE


import java . util . * ; class solution { static int nthTerm ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) + n - 2 ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) + n - 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE


class GFG { static int findMaximumDiff ( int [ ] a , int n ) { int ind1 = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( a [ 0 ] != a [ i ] ) { ind1 = i ; break ; } } int ind2 = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ n - 1 ] != a [ i ] ) { ind2 = ( n - 1 - i ) ; break ; } } return Math . max ( ind1 , ind2 ) ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 2 , 3 , 2 , 3 } ; int n = a . length ; System . out . println ( findMaximumDiff ( a , n ) ) ; } }

def findMaximumDiff ( a , n ) : NEW_LINE INDENT ind1 = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ 0 ] != a [ i ] ) : NEW_LINE INDENT ind1 = i NEW_LINE break NEW_LINE DEDENT DEDENT ind2 = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ n - 1 ] != a [ i ] ) : NEW_LINE INDENT ind2 = ( n - 1 - i ) NEW_LINE break NEW_LINE DEDENT DEDENT return max ( ind1 , ind2 ) NEW_LINE DEDENT a = [ 1 , 2 , 3 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( findMaximumDiff ( a , n ) ) NEW_LINE


class GFG { static int getMinimumSum ( int arr [ ] , int n ) { int fMin = Integer . MAX_VALUE , sMin = Integer . MAX_VALUE , tMin = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < fMin ) { tMin = sMin ; sMin = fMin ; fMin = arr [ i ] ; } else if ( arr [ i ] < sMin ) { tMin = sMin ; sMin = arr [ i ] ; } else if ( arr [ i ] < tMin ) { tMin = arr [ i ] ; } } return ( fMin + sMin + tMin ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , - 1 , 5 , - 2 } ; int n = arr . length ; System . out . print ( getMinimumSum ( arr , n ) + " \n " ) ; } }

import sys NEW_LINE def getMinimumSum ( arr , n ) : NEW_LINE INDENT fMin = sys . maxsize ; NEW_LINE sMin = sys . maxsize ; NEW_LINE tMin = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < fMin ) : NEW_LINE INDENT tMin = sMin ; NEW_LINE sMin = fMin ; NEW_LINE fMin = arr [ i ] ; NEW_LINE DEDENT elif ( arr [ i ] < sMin ) : NEW_LINE INDENT tMin = sMin ; NEW_LINE sMin = arr [ i ] ; NEW_LINE DEDENT elif ( arr [ i ] < tMin ) : NEW_LINE INDENT tMin = arr [ i ] ; NEW_LINE DEDENT DEDENT return ( fMin + sMin + tMin ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , - 1 , 5 , - 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( getMinimumSum ( arr , n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static float findArea ( float a ) { float area ; area = ( float ) ( 5 * Math . sqrt ( 3 ) * a * a ) ; return area ; } static float findVolume ( float a ) { float volume ; volume = ( float ) ( ( ( float ) 5 / 12 ) * ( 3 + Math . sqrt ( 5 ) ) * a * a * a ) ; return volume ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( " Area : ▁ " + findArea ( a ) ) ; System . out . println ( " Volume : ▁ " + findVolume ( a ) ) ; } }

from math import sqrt NEW_LINE def findArea ( a ) : NEW_LINE INDENT area = 5 * sqrt ( 3 ) * a * a NEW_LINE return area NEW_LINE DEDENT def findVolume ( a ) : NEW_LINE INDENT volume = ( ( 5 / 12 ) * ( 3 + sqrt ( 5 ) ) * a * a * a ) NEW_LINE return volume NEW_LINE DEDENT a = 5 NEW_LINE print ( " Area : ▁ " , findArea ( a ) ) NEW_LINE print ( " Volume : ▁ " , findVolume ( a ) ) NEW_LINE


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

def count ( n , x ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if i <= x : NEW_LINE INDENT if x // i <= n and x % i == 0 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT n = 8 NEW_LINE x = 24 NEW_LINE print ( count ( n , x ) ) NEW_LINE


class GFG { static void pairwiseDifference ( int arr [ ] , int n ) { int diff ; for ( int i = 0 ; i < n - 1 ; i ++ ) { diff = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; System . out . print ( diff + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 10 , 15 , 5 , 6 } ; int n = arr . length ; pairwiseDifference ( arr , n ) ; } }

def pairwiseDifference ( arr , n ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT diff = abs ( arr [ i ] - arr [ i + 1 ] ) NEW_LINE print ( diff , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 10 , 15 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE pairwiseDifference ( arr , n ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int factorial ( int N ) { if ( N == 0 || N == 1 ) return 1 ; return N * factorial ( N - 1 ) ; } static int nthTerm ( int N ) { return ( factorial ( N ) * ( N + 2 ) / 2 ) ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }

def factorial ( N ) : NEW_LINE INDENT if N == 0 or N == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return N * factorial ( N - 1 ) NEW_LINE DEDENT def nthTerm ( N ) : NEW_LINE INDENT return ( factorial ( N ) * ( N + 2 ) // 2 ) NEW_LINE DEDENT N = 6 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE


import java . io . * ; class GFG { static boolean isPowerOf ( long val , int base ) { while ( val > 1 ) { if ( val % base != 0 ) return false ; val /= base ; } return true ; } static int numberOfPartitions ( String binaryNo ) { int i , j , n = binaryNo . length ( ) ; long val ; int dp [ ] = new int [ n ] ; dp [ n - 1 ] = ( ( ( binaryNo . charAt ( n - 1 ) - '0' ) == 0 ) ? - 1 : 1 ) ; for ( i = n - 2 ; i >= 0 ; i -- ) { val = 0 ; if ( ( binaryNo . charAt ( i ) - '0' ) == 0 ) { dp [ i ] = - 1 ; continue ; } dp [ i ] = Integer . MAX_VALUE ; for ( j = i ; j < n ; j ++ ) { val = ( val * 2 ) + ( long ) ( binaryNo . charAt ( j ) - '0' ) ; if ( isPowerOf ( val , 4 ) || isPowerOf ( val , 6 ) ) { if ( j == n - 1 ) { dp [ i ] = 1 ; } else { if ( dp [ j + 1 ] != - 1 ) dp [ i ] = Math . min ( dp [ i ] , dp [ j + 1 ] + 1 ) ; } } } if ( dp [ i ] == Integer . MAX_VALUE ) dp [ i ] = - 1 ; } return dp [ 0 ] ; } public static void main ( String [ ] args ) { String binaryNo = "100110110" ; System . out . println ( numberOfPartitions ( binaryNo ) ) ; } }

import sys NEW_LINE def isPowerOf ( val , base ) : NEW_LINE INDENT while ( val > 1 ) : NEW_LINE INDENT if ( val % base != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT val //= base NEW_LINE DEDENT return True NEW_LINE DEDENT def numberOfPartitions ( binaryNo ) : NEW_LINE INDENT n = len ( binaryNo ) NEW_LINE dp = [ 0 ] * n NEW_LINE if ( ( ord ( binaryNo [ n - 1 ] ) - ord ( '0' ) ) == 0 ) : NEW_LINE INDENT dp [ n - 1 ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ n - 1 ] = 1 NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT val = 0 NEW_LINE if ( ( ord ( binaryNo [ i ] ) - ord ( '0' ) ) == 0 ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE continue NEW_LINE DEDENT dp [ i ] = sys . maxsize NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT val = ( val * 2 ) + ( ord ( binaryNo [ j ] ) - ord ( '0' ) ) NEW_LINE if ( isPowerOf ( val , 4 ) or isPowerOf ( val , 6 ) ) : NEW_LINE INDENT if ( j == n - 1 ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( dp [ j + 1 ] != - 1 ) : NEW_LINE INDENT dp [ i ] = min ( dp [ i ] , dp [ j + 1 ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( dp [ i ] == sys . maxsize ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT DEDENT return dp [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT binaryNo = "100110110" NEW_LINE print ( numberOfPartitions ( binaryNo ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean ifPossible ( int arr [ ] , int n ) { int copy [ ] = Arrays . copyOf ( arr , arr . length ) ; Arrays . sort ( copy ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( arr [ i ] == copy [ i ] ) && ! ( arr [ n - 1 - i ] == copy [ i ] ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 } ; int n = arr . length ; if ( ifPossible ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def ifPossible ( arr , n ) : NEW_LINE INDENT cp = [ 0 ] * n NEW_LINE cp = arr NEW_LINE cp . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( not ( arr [ i ] == cp [ i ] ) and not ( arr [ n - 1 - i ] == cp [ i ] ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 7 , 6 , 4 , 5 , 3 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE if ( ifPossible ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int calculate_sum ( int a , int N ) { int m = N / a ; int sum = m * ( m + 1 ) / 2 ; int ans = a * sum ; return ans ; } public static void main ( String [ ] args ) { int a = 7 , N = 49 ; System . out . println ( " Sum ▁ of ▁ multiples ▁ of ▁ " + a + " ▁ up ▁ to ▁ " + N + " ▁ = ▁ " + calculate_sum ( a , N ) ) ; } }

def calculate_sum ( a , N ) : NEW_LINE INDENT m = N / a NEW_LINE sum = m * ( m + 1 ) / 2 NEW_LINE ans = a * sum NEW_LINE print ( " Sum ▁ of ▁ multiples ▁ of ▁ " , a , " ▁ up ▁ to ▁ " , N , " ▁ = ▁ " , ans ) NEW_LINE DEDENT calculate_sum ( 7 , 49 ) NEW_LINE


class GFG { static int findMod4 ( String s , int n ) { int k ; if ( n == 1 ) k = s . charAt ( 0 ) - '0' ; else k = ( s . charAt ( n - 2 ) - '0' ) * 10 + s . charAt ( n - 1 ) - '0' ; return ( k % 4 ) ; } public static void main ( String [ ] args ) { String s = "81" ; int n = s . length ( ) ; System . out . println ( findMod4 ( s , n ) ) ; } }

def findMod4 ( s , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT k = ord ( s [ 0 ] ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT k = ( ( ord ( s [ n - 2 ] ) - ord ( '0' ) ) * 10 + ord ( s [ n - 1 ] ) - ord ( '0' ) ) NEW_LINE DEDENT return ( k % 4 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "81" NEW_LINE n = len ( s ) NEW_LINE print ( findMod4 ( s , n ) ) NEW_LINE DEDENT


class GFG { static boolean isDivisible ( long n ) { while ( n / 100 != 0 ) { long d = n % 10 ; n /= 10 ; n += d * 7 ; } return ( n % 23 == 0 ) ; } public static void main ( String [ ] args ) { long n = 1191216 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n += d * 7 NEW_LINE DEDENT return ( n % 23 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1191216 NEW_LINE if ( isDivisible ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int longest_gap ( int N ) { int distance = 0 , count = 0 , first_1 = - 1 , last_1 = - 1 ; while ( N != 0 ) { count ++ ; int r = N & 1 ; if ( r == 1 ) { first_1 = first_1 == - 1 ? count : first_1 ; last_1 = count ; } N = N / 2 ; } if ( last_1 <= first_1 ) { return - 1 ; } else { distance = ( last_1 - first_1 - 1 ) ; return distance ; } } public static void main ( String [ ] args ) { int N = 131 ; System . out . println ( longest_gap ( N ) ) ; N = 8 ; System . out . println ( longest_gap ( N ) ) ; N = 17 ; System . out . println ( longest_gap ( N ) ) ; N = 33 ; System . out . println ( longest_gap ( N ) ) ; } }

def longest_gap ( N ) : NEW_LINE INDENT distance = 0 NEW_LINE count = 0 NEW_LINE first_1 = - 1 NEW_LINE last_1 = - 1 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE r = N & 1 NEW_LINE if ( r == 1 ) : NEW_LINE INDENT if first_1 == - 1 : NEW_LINE INDENT first_1 = count NEW_LINE DEDENT else : NEW_LINE INDENT first_1 = first_1 NEW_LINE DEDENT last_1 = count NEW_LINE DEDENT N = N // 2 NEW_LINE DEDENT if ( last_1 <= first_1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT distance = last_1 - first_1 - 1 NEW_LINE return distance NEW_LINE DEDENT DEDENT N = 131 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 8 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 17 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE N = 33 NEW_LINE print ( longest_gap ( N ) ) NEW_LINE


import java . util . * ; class GFG { static void printStrings ( String s , int l , int m ) { Set < String > c = new LinkedHashSet < > ( ) ; s = s + s ; for ( int i = 0 ; i < l ; i ++ ) { c . add ( s . substring ( i , i + m ) ) ; } Iterator itr = c . iterator ( ) ; while ( itr . hasNext ( ) ) { String a = ( String ) itr . next ( ) ; System . out . print ( a + " ▁ " ) ; } c . clear ( ) ; } public static void main ( String [ ] args ) { String str = " saurav " ; int N = str . length ( ) ; int M = 4 ; printStrings ( str , N , M ) ; } }

def printStrings ( s , l , m ) : NEW_LINE INDENT c = set ( ) NEW_LINE s = s + s NEW_LINE for i in range ( l ) : NEW_LINE INDENT c . add ( s [ i : i + m ] ) NEW_LINE DEDENT for i in c : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " saurav " NEW_LINE N = len ( string ) NEW_LINE M = 4 NEW_LINE printStrings ( string , N , M ) NEW_LINE DEDENT


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

def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def calculateSeries ( n ) : NEW_LINE INDENT return factorial ( n + 1 ) - 1 NEW_LINE DEDENT n = 3 NEW_LINE print ( calculateSeries ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int maxCountAB ( String s [ ] , int n ) { int A = 0 , B = 0 , BA = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { String S = s [ i ] ; int L = S . length ( ) ; for ( int j = 0 ; j < L - 1 ; j ++ ) { if ( S . charAt ( j ) == ' A ' && S . charAt ( j + 1 ) == ' B ' ) { ans ++ ; } } if ( S . charAt ( 0 ) == ' B ' && S . charAt ( L - 1 ) == ' A ' ) BA ++ ; else if ( S . charAt ( 0 ) == ' B ' ) B ++ ; else if ( S . charAt ( L - 1 ) == ' A ' ) A ++ ; } if ( BA == 0 ) ans += Math . min ( B , A ) ; else if ( A + B == 0 ) ans += BA - 1 ; else ans += BA + Math . min ( B , A ) ; return ans ; } public static void main ( String [ ] args ) { String s [ ] = { " ABCA " , " BOOK " , " BAND " } ; int n = s . length ; System . out . println ( maxCountAB ( s , n ) ) ; } }

def maxCountAB ( s , n ) : NEW_LINE INDENT A = 0 NEW_LINE B = 0 NEW_LINE BA = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT S = s [ i ] NEW_LINE L = len ( S ) NEW_LINE for j in range ( L - 1 ) : NEW_LINE INDENT if ( S [ j ] == ' A ' and S [ j + 1 ] == ' B ' ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( S [ 0 ] == ' B ' and S [ L - 1 ] == ' A ' ) : NEW_LINE INDENT BA += 1 NEW_LINE DEDENT elif ( S [ 0 ] == ' B ' ) : NEW_LINE INDENT B += 1 NEW_LINE DEDENT elif ( S [ L - 1 ] == ' A ' ) : NEW_LINE INDENT A += 1 NEW_LINE DEDENT DEDENT if ( BA == 0 ) : NEW_LINE INDENT ans += min ( B , A ) NEW_LINE DEDENT elif ( A + B == 0 ) : NEW_LINE INDENT ans += BA - 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += BA + min ( B , A ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = [ " ABCA " , " BOOK " , " BAND " ] NEW_LINE n = len ( s ) NEW_LINE print ( maxCountAB ( s , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void anglechordtang ( int z ) { System . out . print ( " The ▁ angle ▁ between ▁ tangent " + " ▁ and ▁ the ▁ chord ▁ is ▁ " + z + " ▁ degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; anglechordtang ( z ) ; } }

def anglechordtang ( z ) : NEW_LINE INDENT print ( " The ▁ angle ▁ between ▁ tangent " , " and ▁ the ▁ chord ▁ is " , z , " degrees " ) ; NEW_LINE DEDENT z = 48 ; NEW_LINE anglechordtang ( z ) ; NEW_LINE


import java . util . Vector ; class GFG { static Vector < Integer > permutation = new Vector < > ( ) ; static void printPermutation ( ) { permutation . stream ( ) . forEach ( ( i ) -> { System . out . print ( i + " ▁ " ) ; } ) ; } static int give_random_number ( int l , int r ) { int x = ( int ) ( Math . random ( ) % ( r - l + 1 ) + l ) ; return x ; } static void generate_random_permutation ( int l , int r ) { if ( l > r ) return ; int n = give_random_number ( l , r ) ; permutation . add ( n ) ; generate_random_permutation ( l , n - 1 ) ; generate_random_permutation ( n + 1 , r ) ; } public static void main ( String [ ] args ) { int l = 5 ; int r = 15 ; generate_random_permutation ( l , r ) ; printPermutation ( ) ; } }

import random NEW_LINE permutation = [ ] NEW_LINE def printPermutation ( ) : NEW_LINE INDENT for i in permutation : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT def give_random_number ( l , r ) : NEW_LINE INDENT x = random . randint ( l , r ) NEW_LINE return x NEW_LINE DEDENT def generate_random_permutation ( l , r ) : NEW_LINE INDENT if ( l > r ) : NEW_LINE INDENT return NEW_LINE DEDENT n = give_random_number ( l , r ) NEW_LINE permutation . append ( n ) NEW_LINE generate_random_permutation ( l , n - 1 ) NEW_LINE generate_random_permutation ( n + 1 , r ) NEW_LINE DEDENT l = 5 NEW_LINE r = 15 NEW_LINE generate_random_permutation ( l , r ) NEW_LINE printPermutation ( ) NEW_LINE


class GFG { static int [ ] [ ] dp = new int [ 1001 ] [ 1001 ] ; static int find_max ( int i , int sum , int [ ] v , int k ) { if ( i == v . length ) return 0 ; if ( dp [ i ] [ sum ] != - 1 ) return dp [ i ] [ sum ] ; int ans = 0 ; if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) ans = find_max ( i + 1 , sum , v , k ) ; if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) ans = Math . max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) ; return dp [ i ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 43 , 1 , 17 , 26 , 15 } ; int k = 16 ; for ( int i = 0 ; i < 1001 ; i ++ ) for ( int j = 0 ; j < 1001 ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . print ( find_max ( 0 , 0 , arr , k ) ) ; } }

dp = [ [ - 1 for i in range ( 1001 ) ] for j in range ( 1001 ) ] NEW_LINE def find_max ( i , sum , v , k ) : NEW_LINE INDENT if ( i == len ( v ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ sum ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( ( sum + find_max ( i + 1 , sum , v , k ) ) % k == 0 ) : NEW_LINE INDENT ans = find_max ( i + 1 , sum , v , k ) NEW_LINE DEDENT if ( ( sum + v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) % k == 0 ) : NEW_LINE INDENT ans = max ( ans , v [ i ] + find_max ( i + 1 , ( sum + v [ i ] ) % k , v , k ) ) NEW_LINE DEDENT dp [ i ] [ sum ] = ans NEW_LINE return dp [ i ] [ sum ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 43 , 1 , 17 , 26 , 15 ] NEW_LINE k = 16 NEW_LINE print ( find_max ( 0 , 0 , arr , k ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int cnt_neg ; static boolean exists_zero ; static void preProcess ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) cnt_neg ++ ; if ( arr [ i ] == 0 ) exists_zero = true ; } } static boolean isPossible ( int k ) { if ( ! exists_zero ) { if ( k >= cnt_neg && ( k - cnt_neg ) % 2 == 0 ) return true ; else return false ; } else { if ( k >= cnt_neg ) return true ; else return false ; } } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 2 , - 3 , 4 , 5 } ; int n = arr . length ; preProcess ( arr , n ) ; int queries [ ] = { 1 , 2 , 3 , 4 } ; int q = arr . length ; for ( int i = 0 ; i < q ; i ++ ) { if ( isPossible ( queries [ i ] ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } } }

cnt_neg = 0 ; NEW_LINE exists_zero = None ; NEW_LINE def preProcess ( arr , n ) : NEW_LINE INDENT global cnt_neg NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT cnt_neg += 1 ; NEW_LINE DEDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT exists_zero = True ; NEW_LINE DEDENT DEDENT DEDENT def isPossible ( k ) : NEW_LINE INDENT if ( not exists_zero ) : NEW_LINE INDENT if ( k >= cnt_neg and ( k - cnt_neg ) % 2 == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( k >= cnt_neg ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , 2 , - 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE preProcess ( arr , n ) ; NEW_LINE queries = [ 1 , 2 , 3 , 4 ] ; NEW_LINE q = len ( queries ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT if ( isPossible ( queries [ i ] ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT DEDENT


class GFG { static double findHypotenuse ( double side1 , double side2 ) { double h = Math . sqrt ( ( side1 * side1 ) + ( side2 * side2 ) ) ; return h ; } public static void main ( String s [ ] ) { int side1 = 3 , side2 = 4 ; System . out . printf ( " % .2f " , findHypotenuse ( side1 , side2 ) ) ; } }

def findHypotenuse ( side1 , side2 ) : NEW_LINE INDENT h = ( ( ( side1 * side1 ) + ( side2 * side2 ) ) ** ( 1 / 2 ) ) ; NEW_LINE return h ; NEW_LINE DEDENT side1 = 3 ; NEW_LINE side2 = 4 ; NEW_LINE print ( findHypotenuse ( side1 , side2 ) ) ; NEW_LINE


class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; Node prev ; boolean isBST ( ) { prev = null ; return isBST ( root ) ; } boolean isBST ( Node node ) { if ( node != null ) { if ( ! isBST ( node . left ) ) return false ; if ( prev != null && node . data <= prev . data ) return false ; prev = node ; return isBST ( node . right ) ; } return true ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 4 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 3 ) ; if ( tree . isBST ( ) ) System . out . println ( " IS ▁ BST " ) ; else System . out . println ( " Not ▁ a ▁ BST " ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT prev = None NEW_LINE def isbst ( root ) : NEW_LINE INDENT global prev NEW_LINE prev = None NEW_LINE return isbst_rec ( root ) NEW_LINE DEDENT def isbst_rec ( root ) : NEW_LINE INDENT global prev NEW_LINE if root is None : NEW_LINE INDENT return True NEW_LINE DEDENT if isbst_rec ( root . left ) is False : NEW_LINE INDENT return False NEW_LINE DEDENT if prev is not None and prev . data > root . data : NEW_LINE INDENT return False NEW_LINE DEDENT prev = root NEW_LINE return isbst_rec ( root . right ) NEW_LINE DEDENT root = Node ( 4 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE if isbst ( root ) : NEW_LINE INDENT print ( " is ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " not ▁ a ▁ BST " ) NEW_LINE DEDENT


import java . io . * ; class Numbers { static boolean isValid ( int n , int d ) { int digit = n % 10 ; int sum = digit ; if ( digit == d ) return false ; n /= 10 ; while ( n > 0 ) { digit = n % 10 ; if ( digit == d || digit <= sum ) return false ; else { sum += digit ; n /= 10 ; } } return true ; } static void printGoodNumber ( int L , int R , int d ) { for ( int i = L ; i <= R ; i ++ ) { if ( isValid ( i , d ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int L = 410 , R = 520 , d = 3 ; printGoodNumber ( L , R , d ) ; } }

def isValid ( n , d ) : NEW_LINE INDENT digit = n % 10 ; NEW_LINE sum = digit ; NEW_LINE if ( digit == d ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT n = int ( n / 10 ) ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 ; NEW_LINE if ( digit == d or digit <= sum ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT sum += digit ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def printGoodNumber ( L , R , d ) : NEW_LINE INDENT for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( isValid ( i , d ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT L = 410 ; NEW_LINE R = 520 ; NEW_LINE d = 3 ; NEW_LINE printGoodNumber ( L , R , d ) ; NEW_LINE


import java . util . * ; class GFG { static int findSubarrCnt ( int arr [ ] , int n ) { int ans = 0 , XOR = 0 ; int prefix [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; ++ i ) { XOR = XOR ^ arr [ i ] ; prefix [ i ] = XOR ; } HashMap < Integer , Integer > evenGroup = new HashMap < > ( ) ; HashMap < Integer , Integer > oddGroup = new HashMap < > ( ) ; oddGroup . put ( 0 , 1 ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( i % 2 == 1 ) { if ( oddGroup . containsKey ( prefix [ i ] ) ) { ans += oddGroup . get ( prefix [ i ] ) ; oddGroup . put ( prefix [ i ] , oddGroup . get ( prefix [ i ] + 1 ) ) ; } else { oddGroup . put ( prefix [ i ] , 1 ) ; } } else { if ( evenGroup . containsKey ( prefix [ i ] ) ) { ans += evenGroup . get ( prefix [ i ] ) ; evenGroup . put ( prefix [ i ] , evenGroup . get ( prefix [ i ] + 1 ) ) ; } else { evenGroup . put ( prefix [ i ] , 1 ) ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 3 , 7 , 6 } ; int N = arr . length ; System . out . println ( findSubarrCnt ( arr , N ) ) ; } }

def findSubarrCnt ( arr , n ) : NEW_LINE INDENT ans = 0 ; XOR = 0 ; NEW_LINE prefix = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT XOR = XOR ^ arr [ i ] ; NEW_LINE prefix [ i ] = XOR ; NEW_LINE DEDENT oddGroup = dict . fromkeys ( prefix , 0 ) NEW_LINE evenGroup = dict . fromkeys ( prefix , 0 ) NEW_LINE oddGroup [ 0 ] = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i & 1 ) : NEW_LINE INDENT ans += oddGroup [ prefix [ i ] ] ; NEW_LINE oddGroup [ prefix [ i ] ] += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans += evenGroup [ prefix [ i ] ] ; NEW_LINE evenGroup [ prefix [ i ] ] += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 ; NEW_LINE arr = [ 3 , 2 , 2 , 3 , 7 , 6 ] ; NEW_LINE print ( findSubarrCnt ( arr , N ) ) ; NEW_LINE DEDENT


import java . lang . * ; import java . io . * ; import java . util . * ; class GFG { static char reVal ( int num ) { if ( num >= 0 && num <= 9 ) return ( char ) ( num + 48 ) ; else return ( char ) ( num - 10 + 65 ) ; } static String fromDeci ( int base1 , int inputNum ) { String s = " " ; while ( inputNum > 0 ) { s += reVal ( inputNum % base1 ) ; inputNum /= base1 ; } StringBuilder ix = new StringBuilder ( ) ; ix . append ( s ) ; return new String ( ix . reverse ( ) ) ; } public static void main ( String [ ] args ) { int inputNum = 282 , base1 = 16 ; System . out . println ( " Equivalent ▁ of ▁ " + inputNum + " ▁ in ▁ base ▁ " + base1 + " ▁ is ▁ " + fromDeci ( base1 , inputNum ) ) ; } }

def reVal ( num ) : NEW_LINE INDENT if ( num >= 0 and num <= 9 ) : NEW_LINE INDENT return chr ( num + ord ( '0' ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return chr ( num - 10 + ord ( ' A ' ) ) ; NEW_LINE DEDENT DEDENT def strev ( str ) : NEW_LINE INDENT len = len ( str ) ; NEW_LINE for i in range ( int ( len / 2 ) ) : NEW_LINE INDENT temp = str [ i ] ; NEW_LINE str [ i ] = str [ len - i - 1 ] ; NEW_LINE str [ len - i - 1 ] = temp ; NEW_LINE DEDENT DEDENT def fromDeci ( res , base , inputNum ) : NEW_LINE INDENT index = 0 ; NEW_LINE while ( inputNum > 0 ) : NEW_LINE INDENT res += reVal ( inputNum % base ) ; NEW_LINE inputNum = int ( inputNum / base ) ; NEW_LINE DEDENT res = res [ : : - 1 ] ; NEW_LINE return res ; NEW_LINE DEDENT inputNum = 282 ; NEW_LINE base = 16 ; NEW_LINE res = " " ; NEW_LINE print ( " Equivalent ▁ of " , inputNum , " in ▁ base " , base , " is " , fromDeci ( res , base , inputNum ) ) ; NEW_LINE


import java . io . * ; class minOeration { static void printMinOp ( int arr [ ] ) { int arraySum , smallest , arr_size = arr . length ; arraySum = 0 ; smallest = arr [ 0 ] ; for ( int i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] < smallest ) smallest = arr [ i ] ; arraySum += arr [ i ] ; } int minOperation = arraySum - arr_size * smallest ; System . out . println ( " Minimum ▁ Operation ▁ = ▁ " + minOperation ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 2 , 4 , 3 } ; printMinOp ( arr ) ; } }

def arraySum ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT sm = sm + arr [ i ] NEW_LINE i = i + 1 NEW_LINE DEDENT return sm NEW_LINE DEDENT def smallest ( arr , n ) : NEW_LINE INDENT small = 1000000000 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] < small ) : NEW_LINE INDENT small = arr [ i ] NEW_LINE DEDENT DEDENT return small NEW_LINE DEDENT def minOp ( arr , n ) : NEW_LINE INDENT sm = arraySum ( arr , n ) NEW_LINE small = smallest ( arr , n ) NEW_LINE minOperation = sm - ( n * small ) NEW_LINE return minOperation NEW_LINE DEDENT arr = [ 5 , 6 , 2 , 4 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ Operation ▁ = ▁ " , minOp ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static void maxLengthArr ( int x , int y ) { boolean [ ] ex = new boolean [ ( 1 << x ) ] ; ex [ 0 ] = true ; Vector < Integer > pre = new Vector < Integer > ( ) ; pre . add ( 0 ) ; for ( int i = 1 ; i < ( 1 << x ) ; i ++ ) { if ( ex [ i ^ y ] ) continue ; pre . add ( i ) ; ex [ i ] = true ; } if ( pre . size ( ) == 1 ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 1 ; i < pre . size ( ) ; i ++ ) System . out . print ( ( pre . get ( i ) ^ pre . get ( i - 1 ) ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int X = 3 , Y = 5 ; maxLengthArr ( X , Y ) ; } }

def maxLengthArr ( x , y ) : NEW_LINE INDENT ex = [ 0 ] * ( 1 << x ) ; NEW_LINE ex [ 0 ] = 1 ; NEW_LINE pre = [ 0 ] ; NEW_LINE for i in range ( 1 , ( 1 << x ) ) : NEW_LINE INDENT if ( ex [ i ^ y ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT pre . append ( i ) ; NEW_LINE ex [ i ] = 1 ; NEW_LINE DEDENT if ( len ( pre ) == 1 ) : NEW_LINE INDENT print ( " - 1" , end = " " ) ; NEW_LINE return ; NEW_LINE DEDENT for i in range ( 1 , len ( pre ) ) : NEW_LINE INDENT print ( pre [ i ] ^ pre [ i - 1 ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 3 ; Y = 5 ; NEW_LINE maxLengthArr ( X , Y ) ; NEW_LINE DEDENT


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

def leonardo ( n ) : NEW_LINE INDENT dp = [ ] ; NEW_LINE dp . append ( 1 ) ; NEW_LINE dp . append ( 1 ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT dp . append ( dp [ i - 1 ] + dp [ i - 2 ] + 1 ) ; NEW_LINE DEDENT return dp [ n ] ; NEW_LINE DEDENT print ( leonardo ( 3 ) ) ; NEW_LINE


class GFG { public static double maxAverageOfPath ( int cost [ ] [ ] , int N ) { int dp [ ] [ ] = new int [ N + 1 ] [ N + 1 ] ; dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < N ; j ++ ) dp [ i ] [ j ] = Math . max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) + cost [ i ] [ j ] ; return ( double ) dp [ N - 1 ] [ N - 1 ] / ( 2 * N - 1 ) ; } public static void main ( String [ ] args ) { int cost [ ] [ ] = { { 1 , 2 , 3 } , { 6 , 5 , 4 } , { 7 , 3 , 9 } } ; System . out . println ( maxAverageOfPath ( cost , 3 ) ) ; } }

M = 100 NEW_LINE def maxAverageOfPath ( cost , N ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] NEW_LINE DEDENT for j in range ( 1 , N ) : NEW_LINE INDENT dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] NEW_LINE DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( 1 , N ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) + cost [ i ] [ j ] NEW_LINE DEDENT DEDENT return dp [ N - 1 ] [ N - 1 ] / ( 2 * N - 1 ) NEW_LINE DEDENT cost = [ [ 1 , 2 , 3 ] , [ 6 , 5 , 4 ] , [ 7 , 3 , 9 ] ] NEW_LINE print ( maxAverageOfPath ( cost , 3 ) ) NEW_LINE


import java . io . * ; class GFG { static int countMultiples ( int n ) { return n / 3 + n / 7 - n / 21 ; } public static void main ( String args [ ] ) { System . out . println ( " Count ▁ = ▁ " + countMultiples ( 25 ) ) ; } }

def countMultiples ( n ) : NEW_LINE INDENT return n / 3 + n / 7 - n / 21 ; NEW_LINE DEDENT n = ( ( int ) ( countMultiples ( 25 ) ) ) ; NEW_LINE print ( " Count ▁ = " , n ) ; NEW_LINE


class GFG { static int lcm ( int a , int b ) { int GCD = __gcd ( a , b ) ; return ( a * b ) / GCD ; } static int MinLCM ( int a [ ] , int n ) { int [ ] Prefix = new int [ n + 2 ] ; int [ ] Suffix = new int [ n + 2 ] ; Prefix [ 1 ] = a [ 0 ] ; for ( int i = 2 ; i <= n ; i += 1 ) { Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; } Suffix [ n ] = a [ n - 1 ] ; for ( int i = n - 1 ; i >= 1 ; i -= 1 ) { Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; } int ans = Math . min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; for ( int i = 2 ; i < n ; i += 1 ) { ans = Math . min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; } return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 5 , 15 , 9 , 36 } ; int n = a . length ; System . out . println ( MinLCM ( a , n ) ) ; } }

from math import gcd NEW_LINE def lcm ( a , b ) : NEW_LINE INDENT GCD = gcd ( a , b ) ; NEW_LINE return ( a * b ) // GCD ; NEW_LINE DEDENT def MinLCM ( a , n ) : NEW_LINE INDENT Prefix = [ 0 ] * ( n + 2 ) ; NEW_LINE Suffix = [ 0 ] * ( n + 2 ) ; NEW_LINE Prefix [ 1 ] = a [ 0 ] ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT Prefix [ i ] = lcm ( Prefix [ i - 1 ] , a [ i - 1 ] ) ; NEW_LINE DEDENT Suffix [ n ] = a [ n - 1 ] ; NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT Suffix [ i ] = lcm ( Suffix [ i + 1 ] , a [ i - 1 ] ) ; NEW_LINE DEDENT ans = min ( Suffix [ 2 ] , Prefix [ n - 1 ] ) ; NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT ans = min ( ans , lcm ( Prefix [ i - 1 ] , Suffix [ i + 1 ] ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 15 , 9 , 36 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( MinLCM ( a , n ) ) ; NEW_LINE DEDENT


public class UboundedKnapsack { private static int max ( int i , int j ) { return ( i > j ) ? i : j ; } private static int unboundedKnapsack ( int W , int n , int [ ] val , int [ ] wt ) { int dp [ ] = new int [ W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( wt [ j ] <= i ) { dp [ i ] = max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) ; } } } return dp [ W ] ; } public static void main ( String [ ] args ) { int W = 100 ; int val [ ] = { 10 , 30 , 20 } ; int wt [ ] = { 5 , 10 , 15 } ; int n = val . length ; System . out . println ( unboundedKnapsack ( W , n , val , wt ) ) ; } }

def unboundedKnapsack ( W , n , val , wt ) : NEW_LINE INDENT dp = [ 0 for i in range ( W + 1 ) ] NEW_LINE ans = 0 NEW_LINE for i in range ( W + 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( wt [ j ] <= i ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - wt [ j ] ] + val [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ W ] NEW_LINE DEDENT W = 100 NEW_LINE val = [ 10 , 30 , 20 ] NEW_LINE wt = [ 5 , 10 , 15 ] NEW_LINE n = len ( val ) NEW_LINE print ( unboundedKnapsack ( W , n , val , wt ) ) NEW_LINE


import java . util . * ; class GFG { public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } public static void main ( String [ ] args ) { int n = 5 , m = 4 ; System . out . println ( rectCount ( n , m ) ) ; } }

def rectCount ( n , m ) : NEW_LINE INDENT return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4 NEW_LINE DEDENT n , m = 5 , 4 NEW_LINE print ( rectCount ( n , m ) ) NEW_LINE


import java . io . * ; class GFG { static int pairAndSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) ans += arr [ i ] & arr [ j ] ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 10 , 15 } ; int n = arr . length ; System . out . println ( pairAndSum ( arr , n ) ) ; } }

def pairAndSum ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( ( i + 1 ) , n ) : NEW_LINE INDENT ans = ans + arr [ i ] & arr [ j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 10 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( pairAndSum ( arr , n ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int findOptimalSolution ( int [ ] a , int N ) { Arrays . sort ( a ) ; int points = 0 ; for ( int i = 0 ; i < N ; i ++ ) { points += a [ i ] * i ; } return points ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 4 , 2 , 3 , 9 } ; int N = a . length ; System . out . println ( findOptimalSolution ( a , N ) ) ; } }

def findOptimalSolution ( a , N ) : NEW_LINE INDENT a . sort ( ) NEW_LINE points = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT points += a [ i ] * i NEW_LINE DEDENT return points NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 2 , 3 , 9 ] NEW_LINE N = len ( a ) NEW_LINE print ( findOptimalSolution ( a , N ) ) NEW_LINE DEDENT


class gfg { public void decToBinary ( int n ) { for ( int i = 31 ; i >= 0 ; i -- ) { int k = n >> i ; if ( ( k & 1 ) > 0 ) System . out . print ( "1" ) ; else System . out . print ( "0" ) ; } } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; int n = 32 ; g . decToBinary ( n ) ; } }

def decToBinary ( n ) : NEW_LINE INDENT for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT k = n >> i ; NEW_LINE if ( k & 1 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT n = 32 ; NEW_LINE decToBinary ( n ) ; NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { static boolean isPrime ( int n ) { if ( n % 2 == 0 ) return false ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { if ( n % i == 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { if ( isPrime ( 19 ) ) 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 if isPrime ( 11 ) : NEW_LINE INDENT print ( " true " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " false " ) NEW_LINE DEDENT


class GFG { static char [ ] stack ; static int top = - 1 ; static void push ( char ele ) { stack [ ++ top ] = ele ; } static char pop ( ) { return stack [ top -- ] ; } static int isPalindrome ( char str [ ] ) { int length = str . length ; stack = new char [ length * 4 ] ; int i , mid = length / 2 ; for ( i = 0 ; i < mid ; i ++ ) { push ( str [ i ] ) ; } if ( length % 2 != 0 ) { i ++ ; } while ( i < length ) { char ele = pop ( ) ; if ( ele != str [ i ] ) return 0 ; i ++ ; } return 1 ; } public static void main ( String [ ] args ) { char str [ ] = " madam " . toCharArray ( ) ; if ( isPalindrome ( str ) == 1 ) { System . out . printf ( " Yes " ) ; } else { System . out . printf ( " No " ) ; } } }

stack = [ ] NEW_LINE top = - 1 NEW_LINE def push ( ele : str ) : NEW_LINE INDENT global top NEW_LINE top += 1 NEW_LINE stack [ top ] = ele NEW_LINE DEDENT def pop ( ) : NEW_LINE INDENT global top NEW_LINE ele = stack [ top ] NEW_LINE top -= 1 NEW_LINE return ele NEW_LINE DEDENT def isPalindrome ( string : str ) -> bool : NEW_LINE INDENT global stack NEW_LINE length = len ( string ) NEW_LINE stack = [ '0' ] * ( length + 1 ) NEW_LINE mid = length // 2 NEW_LINE i = 0 NEW_LINE while i < mid : NEW_LINE INDENT push ( string [ i ] ) NEW_LINE i += 1 NEW_LINE DEDENT if length % 2 != 0 : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while i < length : NEW_LINE INDENT ele = pop ( ) NEW_LINE if ele != string [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " madam " NEW_LINE if isPalindrome ( string ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int binomialCoeffSum ( int n ) { int C [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= Math . min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int sum = 0 ; for ( int i = 0 ; i <= n ; i ++ ) sum += C [ n ] [ i ] ; return sum ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( binomialCoeffSum ( n ) ) ; } }

import math NEW_LINE def binomialCoeffSum ( n ) : NEW_LINE INDENT C = [ [ 0 ] * ( n + 2 ) for i in range ( 0 , n + 2 ) ] NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , min ( i , n ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT sum += C [ n ] [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 4 NEW_LINE print ( binomialCoeffSum ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int ans ; static void subsetSum ( Vector < Integer > c ) { int L = c . size ( ) ; int mul = ( int ) Math . pow ( 2 , L - 1 ) ; for ( int i = 0 ; i < c . size ( ) ; i ++ ) ans += c . get ( i ) * mul ; } static void subsetGen ( int [ ] arr , int i , int n , Vector < Integer > c ) { if ( i == n ) { subsetSum ( c ) ; return ; } subsetGen ( arr , i + 1 , n , c ) ; c . add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n , c ) ; c . remove ( 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 } ; int n = arr . length ; Vector < Integer > c = new Vector < Integer > ( ) ; subsetGen ( arr , 0 , n , c ) ; System . out . println ( ans ) ; } }

c = [ ] NEW_LINE ans = 0 NEW_LINE def subsetSum ( ) : NEW_LINE INDENT global ans NEW_LINE L = len ( c ) NEW_LINE mul = pow ( 2 , L - 1 ) NEW_LINE i = 0 NEW_LINE while ( i < len ( c ) ) : NEW_LINE INDENT ans += c [ i ] * mul NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def subsetGen ( arr , i , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT subsetSum ( ) NEW_LINE return NEW_LINE DEDENT subsetGen ( arr , i + 1 , n ) NEW_LINE c . append ( arr [ i ] ) NEW_LINE subsetGen ( arr , i + 1 , n ) NEW_LINE c . pop ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE subsetGen ( arr , 0 , n ) NEW_LINE print ( ans ) NEW_LINE DEDENT


import java . io . * ; class GFG { static float findVolume ( float l , float b , float h ) { float volume = ( l * b * h ) / 2 ; return volume ; } public static void main ( String [ ] args ) { float l = 18 , b = 12 , h = 9 ; System . out . println ( " Volume ▁ of ▁ triangular ▁ prism : ▁ " + findVolume ( l , b , h ) ) ; } }

def findVolume ( l , b , h ) : NEW_LINE INDENT return ( ( l * b * h ) / 2 ) NEW_LINE DEDENT l = 18 NEW_LINE b = 12 NEW_LINE h = 9 NEW_LINE print ( " Volume ▁ of ▁ triangular ▁ prism : ▁ " , findVolume ( l , b , h ) ) NEW_LINE


import java . lang . Math ; class GFG { static long nthTermOfTheSeries ( int n ) { long nthTerm ; if ( n % 2 == 0 ) nthTerm = ( long ) Math . pow ( n - 1 , 2 ) + n ; else nthTerm = ( long ) Math . pow ( n + 1 , 2 ) + n ; return nthTerm ; } public static void main ( String [ ] args ) { int n ; n = 8 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 12 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 102 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 999 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; n = 9999 ; System . out . println ( nthTermOfTheSeries ( n ) ) ; } }

from math import pow NEW_LINE def nthTermOfTheSeries ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT nthTerm = pow ( n - 1 , 2 ) + n NEW_LINE DEDENT else : NEW_LINE INDENT nthTerm = pow ( n + 1 , 2 ) + n NEW_LINE DEDENT return nthTerm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 12 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 102 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 999 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE n = 9999 NEW_LINE print ( int ( nthTermOfTheSeries ( n ) ) ) NEW_LINE DEDENT


class GFG { static int largestSquareFactor ( int num ) { int answer = 1 ; for ( int i = 2 ; i < Math . sqrt ( num ) ; ++ i ) { int cnt = 0 ; int j = i ; while ( num % j == 0 ) { cnt ++ ; j *= i ; } if ( ( cnt & 1 ) != 0 ) { cnt -- ; answer *= Math . pow ( i , cnt ) ; } else { answer *= Math . pow ( i , cnt ) ; } } return answer ; } public static void main ( String args [ ] ) { int N = 420 ; System . out . println ( largestSquareFactor ( N ) ) ; } }

import math NEW_LINE def largestSquareFactor ( num ) : NEW_LINE INDENT answer = 1 NEW_LINE for i in range ( 2 , int ( math . sqrt ( num ) ) ) : NEW_LINE INDENT cnt = 0 NEW_LINE j = i NEW_LINE while ( num % j == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE j *= i NEW_LINE DEDENT if ( cnt & 1 ) : NEW_LINE INDENT cnt -= 1 NEW_LINE answer *= pow ( i , cnt ) NEW_LINE DEDENT else : NEW_LINE INDENT answer *= pow ( i , cnt ) NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 420 NEW_LINE print ( largestSquareFactor ( N ) ) NEW_LINE DEDENT


import java . io . * ; import java . math . * ; class GFG { static int sumOfDigitsFrom1ToN ( int n ) { int d = ( int ) ( Math . log10 ( n ) ) ; int a [ ] = new int [ d + 1 ] ; a [ 0 ] = 0 ; a [ 1 ] = 45 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( Math . ceil ( Math . pow ( 10 , i - 1 ) ) ) ; return sumOfDigitsFrom1ToNUtil ( n , a ) ; } static int sumOfDigitsFrom1ToNUtil ( int n , int a [ ] ) { if ( n < 10 ) return ( n * ( n + 1 ) / 2 ) ; int d = ( int ) ( Math . log10 ( n ) ) ; int p = ( int ) ( Math . ceil ( Math . pow ( 10 , d ) ) ) ; int msd = n / p ; return ( msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToNUtil ( n % p , a ) ) ; } public static void main ( String args [ ] ) { int n = 328 ; System . out . println ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }

import math NEW_LINE def sumOfDigitsFrom1ToN ( n ) : NEW_LINE INDENT d = int ( math . log ( n , 10 ) ) NEW_LINE a = [ 0 ] * ( d + 1 ) NEW_LINE a [ 0 ] = 0 NEW_LINE a [ 1 ] = 45 NEW_LINE for i in range ( 2 , d + 1 ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] * 10 + 45 *   \ NEW_LINE INDENT int ( math . ceil ( pow ( 10 , i - 1 ) ) ) NEW_LINE DEDENT DEDENT return sumOfDigitsFrom1ToNUtil ( n , a ) NEW_LINE DEDENT def sumOfDigitsFrom1ToNUtil ( n , a ) : NEW_LINE INDENT if ( n < 10 ) : NEW_LINE INDENT return ( n * ( n + 1 ) ) // 2 NEW_LINE DEDENT d = int ( math . log ( n , 10 ) ) NEW_LINE p = int ( math . ceil ( pow ( 10 , d ) ) ) NEW_LINE msd = n // p NEW_LINE return ( msd * a [ d ] + ( msd * ( msd - 1 ) // 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToNUtil ( n % p , a ) ) NEW_LINE DEDENT n = 328 NEW_LINE print ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ from ▁ 1 ▁ to " , n , " is " , sumOfDigitsFrom1ToN ( n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int countZeroso ( int [ ] a , int n ) { int count2 = 0 , count5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] = a [ i ] / 2 ; count2 ++ ; } while ( a [ i ] % 5 == 0 ) { a [ i ] = a [ i ] / 5 ; count5 ++ ; } } return ( count2 < count5 ) ? count2 : count5 ; } public static void main ( String argc [ ] ) { int [ ] a = new int [ ] { 10 , 100 , 20 , 30 , 50 , 91 , 12 , 80 } ; int n = 8 ; System . out . println ( countZeroso ( a , n ) ) ; } }

def countZeros ( a , n ) : NEW_LINE INDENT count2 = 0 NEW_LINE count5 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT while ( a [ i ] % 2 == 0 ) : NEW_LINE INDENT a [ i ] = a [ i ] // 2 NEW_LINE count2 = count2 + 1 NEW_LINE DEDENT while ( a [ i ] % 5 == 0 ) : NEW_LINE INDENT a [ i ] = a [ i ] // 5 NEW_LINE count5 = count5 + 1 NEW_LINE DEDENT DEDENT if ( count2 < count5 ) : NEW_LINE INDENT return count2 NEW_LINE DEDENT else : NEW_LINE INDENT return count5 NEW_LINE DEDENT DEDENT a = [ 10 , 100 , 20 , 30 , 50 , 90 , 12 , 80 ] NEW_LINE n = len ( a ) NEW_LINE print ( countZeros ( a , n ) ) NEW_LINE


import java . util . HashMap ; class GFG { static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) map . put ( arr [ i ] - i , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) map . put ( arr [ i ] - i , map . get ( arr [ i ] - i ) + 1 ) ; int res = 0 ; for ( int x : map . values ( ) ) { int cnt = x ; res += ( ( cnt * ( cnt - 1 ) ) / 2 ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 6 , 7 , 9 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

def countPairs ( arr , n ) : NEW_LINE INDENT map = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT map [ arr [ i ] - i ] = map . get ( arr [ i ] - i , 0 ) + 1 NEW_LINE DEDENT res = 0 NEW_LINE for x in map : NEW_LINE INDENT cnt = map [ x ] NEW_LINE res += ( ( cnt * ( cnt - 1 ) ) // 2 ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 5 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE


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

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


class GFG { static int minAbsDiff ( int n ) { int mod = n % 4 ; if ( mod == 0 || mod == 3 ) { return 0 ; } return 1 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( minAbsDiff ( n ) ) ; } }

def minAbsDiff ( n ) : NEW_LINE INDENT mod = n % 4 ; NEW_LINE if ( mod == 0 or mod == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( minAbsDiff ( n ) ) NEW_LINE DEDENT