class GFG { static int MAX = 64 ; static int getBitmask ( char [ ] s ) { int temp = 0 ; for ( int j = 0 ; j < s . length ; j ++ ) { switch ( s [ j ] ) { case ' s ' : temp = temp | ( 1 ) ; break ; case ' t ' : temp = temp | ( 2 ) ; break ; case ' r ' : temp = temp | ( 4 ) ; break ; case ' i ' : temp = temp | ( 8 ) ; break ; case ' n ' : temp = temp | ( 16 ) ; break ; case ' g ' : temp = temp | ( 32 ) ; break ; default : break ; } } return temp ; } static int countPairs ( String arr [ ] , int n ) { int [ ] bitMask = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) bitMask [ getBitmask ( arr [ i ] . toCharArray ( ) ) ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i ; j < MAX ; j ++ ) { if ( ( i | j ) == ( MAX - 1 ) ) { if ( i == j ) cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) / 2 ) ; else cnt += ( bitMask [ i ] * bitMask [ j ] ) ; } } } return cnt ; } public static void main ( String [ ] args ) { String arr [ ] = { " strrr " , " strring " , " gstrin " } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

MAX = 64 NEW_LINE def getBitmask ( s ) : NEW_LINE INDENT temp = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ j ] == ' s ' ) : NEW_LINE INDENT temp = temp | 1 NEW_LINE DEDENT elif ( s [ j ] == ' t ' ) : NEW_LINE INDENT temp = temp | 2 NEW_LINE DEDENT elif ( s [ j ] == ' r ' ) : NEW_LINE INDENT temp = temp | 4 NEW_LINE DEDENT elif ( s [ j ] == ' i ' ) : NEW_LINE INDENT temp = temp | 8 NEW_LINE DEDENT elif ( s [ j ] == ' n ' ) : NEW_LINE INDENT temp = temp | 16 NEW_LINE DEDENT elif ( s [ j ] == ' g ' ) : NEW_LINE INDENT temp = temp | 32 NEW_LINE DEDENT DEDENT return temp NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT bitMask = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT bitMask [ getBitmask ( arr [ i ] ) ] += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( i , MAX ) : NEW_LINE INDENT if ( ( i | j ) == ( MAX - 1 ) ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += ( bitMask [ i ] * bitMask [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ " strrr " , " strring " , " gstrin " ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE


class GFG { static int numberOfCuts ( int M , int N ) { int result = 0 ; result = ( M - 1 ) * ( N - 1 ) ; return result ; } public static void main ( String args [ ] ) { int M = 4 , N = 4 ; int Cuts = numberOfCuts ( M , N ) ; System . out . println ( " Maximum ▁ cuts ▁ = ▁ " + Cuts ) ; } }

def numberOfCuts ( M , N ) : NEW_LINE INDENT result = 0 NEW_LINE result = ( M - 1 ) * ( N - 1 ) NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M , N = 4 , 4 NEW_LINE Cuts = numberOfCuts ( M , N ) NEW_LINE print ( " Maximum ▁ cuts ▁ = ▁ " , Cuts ) NEW_LINE DEDENT


import java . util . Arrays ; class Test { static int subsetPairNotDivisibleByK ( int arr [ ] , int N , int K ) { int f [ ] = new int [ K ] ; Arrays . fill ( f , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) f [ arr [ i ] % K ] ++ ; if ( K % 2 == 0 ) f [ K / 2 ] = Math . min ( f [ K / 2 ] , 1 ) ; int res = Math . min ( f [ 0 ] , 1 ) ; for ( int i = 1 ; i <= K / 2 ; i ++ ) res += Math . max ( f [ i ] , f [ K - i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 2 , 9 , 1 } ; int N = arr . length ; int K = 3 ; System . out . print ( subsetPairNotDivisibleByK ( arr , N , K ) ) ; } }

def subsetPairNotDivisibleByK ( arr , N , K ) : NEW_LINE INDENT f = [ 0 for i in range ( K ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT f [ arr [ i ] % K ] += 1 NEW_LINE DEDENT if ( K % 2 == 0 ) : NEW_LINE INDENT f [ K // 2 ] = min ( f [ K // 2 ] , 1 ) NEW_LINE DEDENT res = min ( f [ 0 ] , 1 ) NEW_LINE for i in range ( 1 , ( K // 2 ) + 1 ) : NEW_LINE INDENT res += max ( f [ i ] , f [ K - i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 3 , 7 , 2 , 9 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( subsetPairNotDivisibleByK ( arr , N , K ) ) NEW_LINE


class GFG { static boolean SieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isPrime [ i ] = false ; } } return false ; } static void findPrimePair ( int n ) { boolean isPrime [ ] = new boolean [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime [ i ] && isPrime [ n - i ] ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; return ; } } } public static void main ( String [ ] args ) { int n = 74 ; findPrimePair ( n ) ; } }

def SieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT i = p * p NEW_LINE while ( i <= n ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def findPrimePair ( n ) : NEW_LINE INDENT isPrime = [ 0 ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , isPrime ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( isPrime [ i ] and isPrime [ n - i ] ) : NEW_LINE INDENT print ( i , ( n - i ) ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT n = 74 NEW_LINE findPrimePair ( n ) NEW_LINE


import java . util . * ; class GFG { static int calculateSum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } public static void main ( String ar [ ] ) { int n = 8 ; System . out . println ( calculateSum ( n ) ) ; } }

def solve_sum ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return ( n + 1 ) / 2 NEW_LINE DEDENT return - n / 2 NEW_LINE DEDENT n = 8 NEW_LINE print ( int ( solve_sum ( n ) ) ) NEW_LINE


import java . io . * ; class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; int max_sum = maxSubArraySum ( a , n ) ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + max_sum ) ; } }

def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = a [ 0 ] NEW_LINE curr_max = a [ 0 ] NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT curr_max = max ( a [ i ] , curr_max + a [ i ] ) NEW_LINE max_so_far = max ( max_so_far , curr_max ) NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE print " Maximum ▁ contiguous ▁ sum ▁ is " , maxSubArraySum ( a , len ( a ) ) NEW_LINE


class GFG { static int numberSequence ( int n ) { int num = ( int ) ( Math . pow ( 4 , n ) - Math . pow ( 2 , n ) ) - 1 ; return num ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( numberSequence ( n ) ) ; } }

def numberSequence ( n ) : NEW_LINE INDENT num = pow ( 4 , n ) - pow ( 2 , n ) - 1 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 . io . * ; class GFG { static long removeRecur ( long n ) { long prev_digit = n % 10 ; long pow = 10 ; long res = prev_digit ; while ( n > 0 ) { long curr_digit = n % 10 ; if ( curr_digit != prev_digit ) { res += curr_digit * pow ; prev_digit = curr_digit ; pow *= 10 ; } n = n / 10 ; } return res ; } public static void main ( String [ ] args ) { long n = 12224 ; System . out . println ( removeRecur ( n ) ) ; } }

def removeRecur ( n ) : NEW_LINE INDENT prev_digit = n % 10 NEW_LINE pow = 10 NEW_LINE res = prev_digit NEW_LINE while ( n ) : NEW_LINE INDENT curr_digit = n % 10 NEW_LINE if ( curr_digit != prev_digit ) : NEW_LINE INDENT res += curr_digit * pow NEW_LINE prev_digit = curr_digit NEW_LINE pow *= 10 NEW_LINE DEDENT n = int ( n / 10 ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12224 NEW_LINE print ( removeRecur ( n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static float rainDayProbability ( int a [ ] , int n ) { float count = 0 , m ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; } m = count / n ; return m ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; int n = a . length ; System . out . print ( rainDayProbability ( a , n ) ) ; } }

def rainDayProbability ( a , n ) : NEW_LINE INDENT count = a . count ( 1 ) NEW_LINE m = count / n NEW_LINE return m NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( rainDayProbability ( a , n ) ) NEW_LINE DEDENT


class GFG { static double expect ( double m , double n ) { double ans = 0.0 , i ; for ( i = m ; i > 0 ; i -- ) ans += ( Math . pow ( i / m , n ) - Math . pow ( ( i - 1 ) / m , n ) ) * i ; return ans ; } public static void main ( String [ ] args ) { double m = 6 , n = 3 ; System . out . println ( String . format ( " % .5f " , expect ( m , n ) ) ) ; } }

def expect ( m , n ) : NEW_LINE INDENT ans = 0.0 NEW_LINE i = m NEW_LINE while ( i ) : NEW_LINE INDENT ans += ( pow ( i / m , n ) - pow ( ( i - 1 ) / m , n ) ) * i NEW_LINE i -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m , n = 6 , 3 NEW_LINE print ( expect ( m , n ) ) NEW_LINE DEDENT


public class GFG { static int findElement ( int arr [ ] , int n ) { int [ ] prefixSum = new int [ n ] ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; int [ ] suffixSum = new int [ n ] ; suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( prefixSum [ i ] == suffixSum [ i ] ) return arr [ i ] ; return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 2 , 5 } ; int n = arr . length ; System . out . println ( findElement ( arr , n ) ) ; } }

def findElement ( arr , n ) : NEW_LINE INDENT prefixSum = [ 0 ] * n NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT suffixSum = [ 0 ] * n NEW_LINE suffixSum [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT if prefixSum [ i ] == suffixSum [ i ] : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findElement ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void find_numbers ( int N ) { if ( N == 1 ) { System . out . print ( - 1 ) ; } else { System . out . print ( N + " ▁ " + ( N + 1 ) + " ▁ " + ( N * ( N + 1 ) ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; find_numbers ( N ) ; } }

def find_numbers ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( - 1 , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( N , N + 1 , N * ( N + 1 ) ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE find_numbers ( N ) ; NEW_LINE DEDENT


import java . io . * ; import java . math . * ; import java . text . * ; import java . util . * ; import java . util . regex . * ; class GFG { static int summingSeries ( long n ) { int S = 0 ; for ( i = 1 ; i <= n ; i ++ ) S += i * i - ( i - 1 ) * ( i - 1 ) ; return S ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " + summingSeries ( n ) ) ; } }

def summingSeries ( n ) : NEW_LINE INDENT S = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT S += i * i - ( i - 1 ) * ( i - 1 ) NEW_LINE DEDENT return S NEW_LINE DEDENT n = 100 NEW_LINE print ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( n ) , sep = " " ) NEW_LINE


class GFG { static int Log2n ( int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } static boolean isPowerOfTwo ( int n ) { return n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; return Log2n ( n ) + 1 ; } public static void main ( String [ ] args ) { int n = 0 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }

def Log2n ( n ) : NEW_LINE INDENT if ( n > 1 ) : NEW_LINE INDENT return ( 1 + Log2n ( n / 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def isPowerOfTwo ( n ) : NEW_LINE INDENT return n and ( not ( n & ( n - 1 ) ) ) NEW_LINE DEDENT def findPosition ( n ) : NEW_LINE INDENT if ( not isPowerOfTwo ( n ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return Log2n ( n ) + 1 NEW_LINE DEDENT n = 0 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT n = 12 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT n = 128 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximize ( int A1 [ ] , int A2 [ ] , int n , int x , int y ) { int [ ] c = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = A2 [ i ] - A1 [ i ] ; sum += A1 [ i ] ; } int temp ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( c [ i ] < c [ i + 1 ] ) { temp = c [ i ] ; c [ i ] = c [ i + 1 ] ; c [ i + 1 ] = temp ; } } int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += c [ i ] ; if ( i + 1 >= ( n - x ) ) maxi = Math . max ( sum , maxi ) ; } return maxi ; } public static void main ( String args [ ] ) { int A1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int A2 [ ] = { 5 , 4 , 3 , 2 , 1 } ; int n = 5 ; int x = 3 , y = 3 ; System . out . println ( maximize ( A1 , A2 , n , x , y ) ) ; } }

def maximize ( A1 , A2 , n , x , y ) : NEW_LINE INDENT c = [ 0 for i in range ( n ) ] NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = A2 [ i ] - A1 [ i ] NEW_LINE Sum += A1 [ i ] NEW_LINE DEDENT c . sort ( ) NEW_LINE c = c [ : : - 1 ] NEW_LINE maxi = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += c [ i ] NEW_LINE if ( i + 1 >= ( n - x ) ) : NEW_LINE INDENT maxi = max ( Sum , maxi ) NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT A1 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE A2 = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE n = 5 NEW_LINE x , y = 3 , 3 NEW_LINE print ( maximize ( A1 , A2 , n , x , y ) ) NEW_LINE


class GFG { static int max = 100 ; public static void main ( String [ ] args ) { long [ ] arr = new long [ max ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; int i = 0 ; for ( i = 2 ; i < max ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; for ( i = 1 ; i < max - 1 ; i ++ ) { if ( ( arr [ i ] % 10 == 0 ) && ( arr [ i + 1 ] % 10 == 1 ) ) break ; } System . out . println ( " Sequence ▁ is ▁ repeating ▁ after ▁ index ▁ " + i ) ; } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT max = 100 NEW_LINE arr = [ 0 for i in range ( max ) ] NEW_LINE arr [ 0 ] = 0 NEW_LINE arr [ 1 ] = 1 NEW_LINE for i in range ( 2 , max ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] NEW_LINE DEDENT for i in range ( 1 , max - 1 ) : NEW_LINE INDENT if ( ( arr [ i ] % 10 == 0 ) and ( arr [ i + 1 ] % 10 == 1 ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( " Sequence ▁ is ▁ repeating ▁ after ▁ index " , i ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void maxLengthSubSeq ( int a [ ] , int n ) { int temp [ ] = new int [ n ] ; int print [ ] = new int [ n ] ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = 0 ; int x = 0 ; temp [ j ++ ] = a [ x ] ; x = a [ x ] + x ; while ( x < n ) { temp [ j ++ ] = a [ x ] ; x = a [ x ] + x ; } if ( y < j ) { for ( int k = 0 ; k < j ; k ++ ) { print [ k ] = temp [ k ] ; y = j ; } } } for ( int i = 0 ; i < y ; i ++ ) System . out . print ( print [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; maxLengthSubSeq ( a , n ) ; } }

def maxLengthSubSeq ( a , n ) : NEW_LINE INDENT temp = [ 0 for i in range ( n ) ] NEW_LINE print1 = [ 0 for i in range ( n ) ] NEW_LINE y = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT j = 0 NEW_LINE x = 0 NEW_LINE temp [ j ] = a [ x ] NEW_LINE j += 1 NEW_LINE x = a [ x ] + x NEW_LINE while ( x < n ) : NEW_LINE INDENT temp [ j ] = a [ x ] NEW_LINE j += 1 NEW_LINE x = a [ x ] + x NEW_LINE DEDENT if ( y < j ) : NEW_LINE INDENT for k in range ( 0 , j , 1 ) : NEW_LINE INDENT print1 [ k ] = temp [ k ] NEW_LINE y = j NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , y , 1 ) : NEW_LINE INDENT print ( print1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE maxLengthSubSeq ( a , n ) NEW_LINE DEDENT


class GFG { static double areaCuboid ( double l , double h , double w ) { return ( l * h * w ) ; } static double surfaceAreaCuboid ( double l , double h , double w ) { return ( 2 * l * w + 2 * w * h + 2 * l * h ) ; } public static void main ( String [ ] args ) { double l = 1 ; double h = 5 ; double w = 7 ; System . out . println ( " Area ▁ = ▁ " + areaCuboid ( l , h , w ) ) ; System . out . println ( " Total ▁ Surface ▁ Area ▁ = ▁ " + surfaceAreaCuboid ( l , h , w ) ) ; } }

def volumeCuboid ( l , h , w ) : NEW_LINE INDENT return ( l * h * w ) NEW_LINE DEDENT def surfaceAreaCuboid ( l , h , w ) : NEW_LINE INDENT return ( 2 * l * w + 2 * w * h + 2 * l * h ) NEW_LINE DEDENT l = 1 NEW_LINE h = 5 NEW_LINE w = 7 NEW_LINE print ( " Volume ▁ = " , volumeCuboid ( l , h , w ) ) NEW_LINE print ( " Total ▁ Surface ▁ Area ▁ = " , surfaceAreaCuboid ( l , h , w ) ) NEW_LINE


class GFG { static boolean oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } public static void main ( String [ ] args ) { int x = 100 , y = - 100 ; if ( oppositeSigns ( x , y ) == true ) System . out . println ( " Signs ▁ are ▁ opposite " ) ; else System . out . println ( " Signs ▁ are ▁ not ▁ opposite " ) ; } }

def oppositeSigns ( x , y ) : NEW_LINE INDENT return ( ( x ^ y ) < 0 ) ; NEW_LINE DEDENT x = 100 NEW_LINE y = 1 NEW_LINE if ( oppositeSigns ( x , y ) == True ) : NEW_LINE INDENT print " Signs ▁ are ▁ opposite " NEW_LINE DEDENT else : NEW_LINE INDENT print " Signs ▁ are ▁ not ▁ opposite " NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void noOfSquares ( int x1 , int y1 , int x2 , int y2 ) { int dx = Math . abs ( x2 - x1 ) ; int dy = Math . abs ( y2 - y1 ) ; int ans = dx + dy - __gcd ( dx , dy ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 1 , x2 = 4 , y2 = 3 ; noOfSquares ( x1 , y1 , x2 , y2 ) ; } }

from math import gcd NEW_LINE def noOfSquares ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT dx = abs ( x2 - x1 ) ; NEW_LINE dy = abs ( y2 - y1 ) ; NEW_LINE ans = dx + dy - gcd ( dx , dy ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 = 1 ; y1 = 1 ; x2 = 4 ; y2 = 3 ; NEW_LINE noOfSquares ( x1 , y1 , x2 , y2 ) ; NEW_LINE DEDENT


public class Optimal_BST2 { static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) { int cost [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cost [ i ] [ i ] = freq [ i ] ; for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i <= n - L + 1 ; i ++ ) { int j = i + L - 1 ; cost [ i ] [ j ] = Integer . MAX_VALUE ; for ( int r = i ; r <= j ; r ++ ) { int c = ( ( r > i ) ? cost [ i ] [ r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 ] [ j ] : 0 ) + sum ( freq , i , j ) ; if ( c < cost [ i ] [ j ] ) cost [ i ] [ j ] = c ; } } } return cost [ 0 ] [ n - 1 ] ; } static int sum ( int freq [ ] , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) { if ( k >= freq . length ) continue ; s += freq [ k ] ; } return s ; } public static void main ( String [ ] args ) { int keys [ ] = { 10 , 12 , 20 } ; int freq [ ] = { 34 , 8 , 50 } ; int n = keys . length ; System . out . println ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }

INT_MAX = 2147483647 NEW_LINE def optimalSearchTree ( keys , freq , n ) : NEW_LINE INDENT cost = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cost [ i ] [ i ] = freq [ i ] NEW_LINE DEDENT for L in range ( 2 , n + 1 ) : NEW_LINE INDENT for i in range ( n - L + 2 ) : NEW_LINE INDENT j = i + L - 1 NEW_LINE if i >= n or j >= n : NEW_LINE INDENT break NEW_LINE DEDENT cost [ i ] [ j ] = INT_MAX NEW_LINE for r in range ( i , j + 1 ) : NEW_LINE INDENT c = 0 NEW_LINE if ( r > i ) : NEW_LINE INDENT c += cost [ i ] [ r - 1 ] NEW_LINE DEDENT if ( r < j ) : NEW_LINE INDENT c += cost [ r + 1 ] [ j ] NEW_LINE DEDENT c += sum ( freq , i , j ) NEW_LINE if ( c < cost [ i ] [ j ] ) : NEW_LINE INDENT cost [ i ] [ j ] = c NEW_LINE DEDENT DEDENT DEDENT DEDENT return cost [ 0 ] [ n - 1 ] NEW_LINE DEDENT def sum ( freq , i , j ) : NEW_LINE INDENT s = 0 NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT s += freq [ k ] NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT keys = [ 10 , 12 , 20 ] NEW_LINE freq = [ 34 , 8 , 50 ] NEW_LINE n = len ( keys ) NEW_LINE print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) ) NEW_LINE DEDENT


class GFG { static float normal ( float m , float n ) { float N = ( float ) ( ( Math . abs ( m ) * Math . abs ( n ) ) / Math . sqrt ( ( Math . abs ( m ) * Math . abs ( m ) ) + ( Math . abs ( n ) * Math . abs ( n ) ) ) ) ; return N ; } public static void main ( String [ ] args ) { float m = - 5 , n = 3 ; System . out . println ( normal ( m , n ) ) ; } }

import math ; NEW_LINE def normal ( m , n ) : NEW_LINE INDENT N = ( ( abs ( m ) * abs ( n ) ) / math . sqrt ( ( abs ( m ) * abs ( m ) ) + ( abs ( n ) * abs ( n ) ) ) ) ; NEW_LINE return N ; NEW_LINE DEDENT m = - 5 ; n = 3 ; NEW_LINE print ( normal ( m , n ) ) ; NEW_LINE


class GFG { static int mod = 1000000007 ; static int [ ] [ ] dp = new int [ 55 ] [ 55 ] ; static int NoofWays ( int face , int throwsVal , int sum ) { if ( sum == 0 && throwsVal == 0 ) { return 1 ; } if ( sum < 0 || throwsVal == 0 ) { return 0 ; } if ( dp [ throwsVal ] [ sum ] != - 1 ) { return dp [ throwsVal ] [ sum ] ; } int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throwsVal - 1 , sum - i ) ; } return dp [ throwsVal ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int faces = 6 , throwsVal = 3 , sum = 12 ; for ( int i = 0 ; i < 55 ; i ++ ) { for ( int j = 0 ; j < 55 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( NoofWays ( faces , throwsVal , sum ) ) ; } }

import numpy as np NEW_LINE mod = 1000000007 ; NEW_LINE dp = np . zeros ( ( 55 , 55 ) ) ; NEW_LINE def NoofWays ( face , throws , sum ) : NEW_LINE INDENT if ( sum == 0 and throws == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( sum < 0 or throws == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ throws ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ throws ] [ sum ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , face + 1 ) : NEW_LINE INDENT ans += NoofWays ( face , throws - 1 , sum - i ) ; NEW_LINE DEDENT dp [ throws ] [ sum ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT faces = 6 ; throws = 3 ; sum = 12 ; NEW_LINE for i in range ( 55 ) : NEW_LINE INDENT for j in range ( 55 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( NoofWays ( faces , throws , sum ) ) ; NEW_LINE DEDENT


class Test { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 100 , n ; static int [ ] store = new int [ MAX ] ; static int [ ] [ ] graph = new int [ MAX ] [ MAX ] ; static int [ ] d = new int [ MAX ] ; static boolean is_clique ( int b ) { for ( int i = 1 ; i < b ; i ++ ) { for ( int j = i + 1 ; j < b ; j ++ ) if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) return false ; } return true ; } static int maxCliques ( int i , int l ) { int max_ = 0 ; for ( int j = i + 1 ; j <= n ; j ++ ) { store [ l ] = j ; if ( is_clique ( l + 1 ) ) { max_ = Math . max ( max_ , l ) ; max_ = Math . max ( max_ , maxCliques ( j , l + 1 ) ) ; } } return max_ ; } public static void main ( String [ ] args ) { int [ ] [ ] edges = { { 1 , 2 } , { 2 , 3 } , { 3 , 1 } , { 4 , 3 } , { 4 , 1 } , { 4 , 2 } } ; int size = edges . length ; n = 4 ; for ( int i = 0 ; i < size ; i ++ ) { graph [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = 1 ; graph [ edges [ i ] [ 1 ] ] [ edges [ i ] [ 0 ] ] = 1 ; d [ edges [ i ] [ 0 ] ] ++ ; d [ edges [ i ] [ 1 ] ] ++ ; } System . out . print ( maxCliques ( 0 , 1 ) ) ; } }

MAX = 100 ; NEW_LINE n = 0 ; NEW_LINE store = [ 0 ] * MAX ; NEW_LINE graph = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] ; NEW_LINE d = [ 0 ] * MAX ; NEW_LINE def is_clique ( b ) : NEW_LINE INDENT for i in range ( 1 , b ) : NEW_LINE INDENT for j in range ( i + 1 , b ) : NEW_LINE INDENT if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT def maxCliques ( i , l ) : NEW_LINE INDENT max_ = 0 ; NEW_LINE for j in range ( i + 1 , n + 1 ) : NEW_LINE INDENT store [ l ] = j ; NEW_LINE if ( is_clique ( l + 1 ) ) : NEW_LINE INDENT max_ = max ( max_ , l ) ; NEW_LINE max_ = max ( max_ , maxCliques ( j , l + 1 ) ) ; NEW_LINE DEDENT DEDENT return max_ ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 1 ] , [ 4 , 3 ] , [ 4 , 1 ] , [ 4 , 2 ] ] ; NEW_LINE size = len ( edges ) ; NEW_LINE n = 4 ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT graph [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = 1 ; NEW_LINE graph [ edges [ i ] [ 1 ] ] [ edges [ i ] [ 0 ] ] = 1 ; NEW_LINE d [ edges [ i ] [ 0 ] ] += 1 ; NEW_LINE d [ edges [ i ] [ 1 ] ] += 1 ; NEW_LINE DEDENT print ( maxCliques ( 0 , 1 ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int countDistinct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }

def countDistinct ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE res = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT while ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT res += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countDistinct ( arr , n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int MAXN = 100 ; static int maxValue ( int n , int m , int x [ ] , int y [ ] , int a [ ] ) { int c [ ] [ ] = new int [ MAXN ] [ MAXN ] ; for ( int i = 0 ; i < m ; ++ i ) { for ( int p = 0 ; p < n ; ++ p ) { for ( int q = 0 ; q < n ; ++ q ) { int dx = Math . abs ( p - x [ i ] ) ; int dy = Math . abs ( q - y [ i ] ) ; int d = Math . max ( dx , dy ) ; c [ p ] [ q ] += Math . max ( 0 , a [ i ] - d ) ; } } } int res = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { res = Math . max ( res , c [ i ] [ j ] ) ; } } return res ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 2 ; int x [ ] = { 3 , 7 } ; int y [ ] = { 3 , 7 } ; int a [ ] = { 4 , 3 } ; System . out . println ( maxValue ( n , m , x , y , a ) ) ; } }

MAXN = 100 NEW_LINE def maxValue ( n , m , x , y , a ) : NEW_LINE INDENT c = [ [ 0 for x in range ( MAXN ) ] for y in range ( MAXN ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for p in range ( n ) : NEW_LINE INDENT for q in range ( n ) : NEW_LINE INDENT dx = abs ( p - x [ i ] ) NEW_LINE dy = abs ( q - y [ i ] ) NEW_LINE d = max ( dx , dy ) NEW_LINE c [ p ] [ q ] += max ( 0 , a [ i ] - d ) NEW_LINE DEDENT DEDENT DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT res = max ( res , c [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE m = 2 NEW_LINE x = [ 3 , 7 ] NEW_LINE y = [ 3 , 7 ] NEW_LINE a = [ 4 , 3 ] NEW_LINE print ( maxValue ( n , m , x , y , a ) ) NEW_LINE DEDENT


class GFG { static int numberOfPermutations ( int a [ ] , int n ) { int sumEven , sumOdd , c = 0 ; do { sumEven = sumOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumOdd += a [ i ] ; } else { sumEven += a [ i ] ; } } if ( sumOdd == sumEven ) { c ++ ; } } while ( next_permutation ( a ) ) ; return c ; } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( int b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; System . out . println ( numberOfPermutations ( a , n ) ) ; } }

def next_permutation ( arr ) : NEW_LINE INDENT arrCount = len ( arr ) ; NEW_LINE i = arrCount - 1 ; NEW_LINE while ( i > 0 and arr [ i ] <= arr [ i - 1 ] ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT if ( i <= 0 ) : NEW_LINE INDENT return [ False , arr ] ; NEW_LINE DEDENT pivotIndex = i - 1 ; NEW_LINE j = arrCount - 1 ; NEW_LINE while ( arr [ j ] <= arr [ pivotIndex ] ) : NEW_LINE INDENT j -= 1 ; NEW_LINE DEDENT temp = arr [ pivotIndex ] ; NEW_LINE arr [ pivotIndex ] = arr [ j ] ; NEW_LINE arr [ j ] = temp ; NEW_LINE j = arrCount - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT temp = arr [ i ] ; NEW_LINE arr [ i ] = arr [ j ] ; NEW_LINE arr [ j ] = temp ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return [ True , arr ] ; NEW_LINE DEDENT def numberOfPermutations ( a , n ) : NEW_LINE INDENT sumEven = 0 ; NEW_LINE sumOdd = 0 ; NEW_LINE c = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT sumEven = 0 ; NEW_LINE sumOdd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT sumOdd += a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT sumEven += a [ i ] ; NEW_LINE DEDENT DEDENT if ( sumOdd == sumEven ) : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT xx = next_permutation ( a ) ; NEW_LINE if ( xx [ 0 ] == False ) : NEW_LINE INDENT break ; NEW_LINE DEDENT a = xx [ 1 ] ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT a = [ 1 , 2 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( numberOfPermutations ( a , n ) ) ; NEW_LINE


class GFG { static int minOperations ( int A , int B ) { if ( A > B ) { A = A + B ; B = A - B ; A = A - B ; } B = B / __gcd ( A , B ) ; return B - 1 ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A = 7 , B = 15 ; System . out . print ( minOperations ( A , B ) + " \n " ) ; } }

import math NEW_LINE def minOperations ( A , B ) : NEW_LINE INDENT if ( A > B ) : NEW_LINE INDENT swap ( A , B ) NEW_LINE DEDENT B = B // math . gcd ( A , B ) ; NEW_LINE return B - 1 NEW_LINE DEDENT A = 7 NEW_LINE B = 15 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE


class GFG { static int MAX = 10 ; static int [ ] [ ] [ ] F = new int [ MAX ] [ MAX ] [ MAX ] ; static int noOfAssignments ( String s , int n , int i , int c_x , int c_y ) { if ( F [ i ] [ c_x ] [ c_y ] != - 1 ) return F [ i ] [ c_x ] [ c_y ] ; if ( i == n ) { F [ i ] [ c_x ] [ c_y ] = ( c_x == 0 && c_y == 0 ) ? 1 : 0 ; return F [ i ] [ c_x ] [ c_y ] ; } if ( s . charAt ( i ) == ' ( ' ) { F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( s , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( s , n , i + 1 , c_x , c_y + 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } F [ i ] [ c_x ] [ c_y ] = 0 ; if ( c_x != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x , c_y - 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } public static void main ( String [ ] args ) { String s = " ( ( ) ) " ; int n = s . length ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) F [ i ] [ j ] [ k ] = - 1 ; System . out . println ( noOfAssignments ( s , n , 0 , 0 , 0 ) ) ; } }

MAX = 10 NEW_LINE F = [ [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX ) ] for k in range ( MAX ) ] NEW_LINE def noOfAssignments ( S , n , i , c_x , c_y ) : NEW_LINE INDENT if F [ i ] [ c_x ] [ c_y ] != - 1 : NEW_LINE INDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if i == n : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] = not c_x and not c_y NEW_LINE return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if S [ i ] == ' ( ' : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] =   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x + 1 , c_y ) +   \ NEW_LINE noOfAssignments ( S , n , i + 1 , c_x , c_y + 1 ) NEW_LINE DEDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT F [ i ] [ c_x ] [ c_y ] = 0 NEW_LINE if c_x : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] +=   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x - 1 , c_y ) NEW_LINE DEDENT DEDENT if c_y : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] +=   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x , c_y - 1 ) NEW_LINE DEDENT DEDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ( ( ) ) " NEW_LINE n = len ( S ) NEW_LINE print ( noOfAssignments ( S , n , 0 , 0 , 0 ) ) NEW_LINE DEDENT


public class GFG { static boolean isVowel ( char c ) { c = Character . toLowerCase ( c ) ; if ( c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ) return true ; return false ; } static String firstXvowels ( String s , int x ) { String result = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) result += s . charAt ( i ) ; if ( result . length ( ) == x ) { return result ; } } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int x = 3 ; System . out . println ( firstXvowels ( str , x ) ) ; } }

def isVowel ( c ) : NEW_LINE INDENT c = c . lower ( ) NEW_LINE if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def firstXvowels ( s , x ) : NEW_LINE INDENT result = " " NEW_LINE for i in range ( 0 , len ( s ) , 1 ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT result += s [ i ] NEW_LINE DEDENT if ( len ( result ) == x ) : NEW_LINE INDENT return result NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " GeeksForGeeks " NEW_LINE x = 3 NEW_LINE print ( firstXvowels ( str , x ) ) NEW_LINE DEDENT


class GFG { static int countSum ( int N , int L , int R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 4 , L = 4 , R = 6 ; System . out . print ( countSum ( N , L , R ) ) ; } }

def countSum ( N , L , R ) : NEW_LINE INDENT if ( L > R ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( N == 1 ) : NEW_LINE INDENT return R - L + 1 ; NEW_LINE DEDENT if ( N > 1 ) : NEW_LINE INDENT return ( N - 2 ) * ( R - L ) + 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , L , R = 4 , 4 , 6 ; NEW_LINE print ( countSum ( N , L , R ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int lenOfLongSubarr ( int arr [ ] , int n ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( ! um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - 1 ) ) { if ( maxLen < ( i - um . get ( sum - 1 ) ) ) maxLen = i - um . get ( sum - 1 ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Length ▁ = ▁ " + lenOfLongSubarr ( arr , n ) ) ; } }

def lenOfLongSubarr ( arr , n ) : NEW_LINE INDENT um = { i : 0 for i in range ( 10 ) } NEW_LINE sum = 0 NEW_LINE maxLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == 0 : NEW_LINE INDENT sum += - 1 NEW_LINE DEDENT else : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT if ( sum == 1 ) : NEW_LINE INDENT maxLen = i + 1 NEW_LINE DEDENT elif ( sum not in um ) : NEW_LINE INDENT um [ sum ] = i NEW_LINE DEDENT if ( ( sum - 1 ) in um ) : NEW_LINE INDENT if ( maxLen < ( i - um [ sum - 1 ] ) ) : NEW_LINE INDENT maxLen = i - um [ sum - 1 ] NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 1 , 0 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ = " , lenOfLongSubarr ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int lps ( String str ) { int n = str . length ( ) ; int L [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) L [ i ] [ i ] = 1 ; for ( int cl = 2 ; cl <= n ; cl ++ ) { for ( int i = 0 ; i < n - cl + 1 ; i ++ ) { int j = i + cl - 1 ; if ( str . charAt ( i ) == str . charAt ( j ) && cl == 2 ) L [ i ] [ j ] = 2 ; else if ( str . charAt ( i ) == str . charAt ( j ) ) L [ i ] [ j ] = L [ i + 1 ] [ j - 1 ] + 2 ; else L [ i ] [ j ] = Integer . max ( L [ i ] [ j - 1 ] , L [ i + 1 ] [ j ] ) ; } } return L [ 0 ] [ n - 1 ] ; } static int minimumNumberOfDeletions ( String str ) { int n = str . length ( ) ; int len = lps ( str ) ; return ( n - len ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( " Minimum ▁ number ▁ " + " of ▁ deletions ▁ = ▁ " + minimumNumberOfDeletions ( str ) ) ; } }

def lps ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE L = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT L [ i ] [ i ] = 1 NEW_LINE DEDENT for cl in range ( 2 , n + 1 ) : NEW_LINE INDENT for i in range ( n - cl + 1 ) : NEW_LINE INDENT j = i + cl - 1 NEW_LINE if ( str [ i ] == str [ j ] and cl == 2 ) : NEW_LINE INDENT L [ i ] [ j ] = 2 NEW_LINE DEDENT elif ( str [ i ] == str [ j ] ) : NEW_LINE INDENT L [ i ] [ j ] = L [ i + 1 ] [ j - 1 ] + 2 NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] = max ( L [ i ] [ j - 1 ] , L [ i + 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return L [ 0 ] [ n - 1 ] NEW_LINE DEDENT def minimumNumberOfDeletions ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE l = lps ( str ) NEW_LINE return ( n - l ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE print ( " Minimum ▁ number ▁ of ▁ deletions ▁ = ▁ " , minimumNumberOfDeletions ( str ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static int minRemove ( int a [ ] , int b [ ] , int n , int m ) { HashMap < Integer , Integer > countA = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > countB = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( countA . containsKey ( a [ i ] ) ) countA . put ( a [ i ] , countA . get ( a [ i ] ) + 1 ) ; else countA . put ( a [ i ] , 1 ) ; } for ( int i = 0 ; i < m ; i ++ ) { if ( countB . containsKey ( b [ i ] ) ) countB . put ( b [ i ] , countB . get ( b [ i ] ) + 1 ) ; else countB . put ( b [ i ] , 1 ) ; } int res = 0 ; Set < Integer > s = countA . keySet ( ) ; for ( int x : s ) if ( countB . containsKey ( x ) ) res += Math . min ( countB . get ( x ) , countA . get ( x ) ) ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int b [ ] = { 2 , 3 , 4 , 5 , 8 } ; int n = a . length ; int m = b . length ; System . out . println ( minRemove ( a , b , n , m ) ) ; } }

def minRemove ( a , b , n , m ) : NEW_LINE INDENT countA = dict ( ) NEW_LINE countB = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT countA [ a [ i ] ] = countA . get ( a [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT countB [ b [ i ] ] = countB . get ( b [ i ] , 0 ) + 1 NEW_LINE DEDENT res = 0 NEW_LINE for x in countA : NEW_LINE INDENT if x in countB . keys ( ) : NEW_LINE INDENT res += min ( countA [ x ] , countB [ x ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 ] NEW_LINE b = [ 2 , 3 , 4 , 5 , 8 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( minRemove ( a , b , n , m ) ) NEW_LINE


class GFG { static int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( spiralDiaSum ( n ) ) ; } }

def spiralDiaSum ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) NEW_LINE DEDENT n = 7 ; NEW_LINE print ( spiralDiaSum ( n ) ) NEW_LINE


import java . util . LinkedList ; import java . util . Queue ; public class NonReapatingCQueue { final static int MAX_CHAR = 26 ; static void firstNonRepeating ( String str ) { int [ ] charCount = new int [ MAX_CHAR ] ; Queue < Character > q = new LinkedList < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; q . add ( ch ) ; charCount [ ch - ' a ' ] ++ ; while ( ! q . isEmpty ( ) ) { if ( charCount [ q . peek ( ) - ' a ' ] > 1 ) q . remove ( ) ; else { System . out . print ( q . peek ( ) + " ▁ " ) ; break ; } } if ( q . isEmpty ( ) ) System . out . print ( - 1 + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { String str = " aabc " ; firstNonRepeating ( str ) ; } }

from queue import Queue NEW_LINE def firstnonrepeating ( Str ) : NEW_LINE INDENT global MAX_CHAR NEW_LINE q = Queue ( ) NEW_LINE charCount = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( len ( Str ) ) : NEW_LINE INDENT q . put ( Str [ i ] ) NEW_LINE charCount [ ord ( Str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT if ( charCount [ ord ( q . queue [ 0 ] ) - ord ( ' a ' ) ] > 1 ) : NEW_LINE INDENT q . get ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( q . queue [ 0 ] , end = " ▁ " ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( q . empty ( ) ) : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT MAX_CHAR = 26 NEW_LINE Str = " aabc " NEW_LINE firstnonrepeating ( Str ) NEW_LINE


public class GFG { static int power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return ( int ) res ; } static void printLastKDigits ( int a , int b , int k ) { System . out . print ( " Last ▁ " + k + " ▁ digits ▁ of ▁ " + a + " ^ " + b + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= k ; i ++ ) temp *= 10 ; temp = power ( a , b , temp ) ; for ( int i = 0 ; i < k - Integer . toString ( temp ) . length ( ) ; i ++ ) System . out . print ( 0 ) ; if ( temp != 0 ) System . out . print ( temp ) ; } public static void main ( String [ ] args ) { int a = 11 ; int b = 3 ; int k = 2 ; printLastKDigits ( a , b , k ) ; } }

def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def numberOfDigits ( x ) : NEW_LINE INDENT i = 0 NEW_LINE while ( x ) : NEW_LINE INDENT x //= 10 NEW_LINE i += 1 NEW_LINE DEDENT return i NEW_LINE DEDENT def printLastKDigits ( a , b , k ) : NEW_LINE INDENT print ( " Last ▁ " + str ( k ) + " ▁ digits ▁ of ▁ " + str ( a ) + " ^ " + str ( b ) , end = " ▁ = ▁ " ) NEW_LINE temp = 1 NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT temp *= 10 NEW_LINE DEDENT temp = power ( a , b , temp ) NEW_LINE for i in range ( k - numberOfDigits ( temp ) ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT if ( temp ) : NEW_LINE INDENT print ( temp ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 11 NEW_LINE b = 3 NEW_LINE k = 2 NEW_LINE printLastKDigits ( a , b , k ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printSorted ( int a [ ] , int n ) { int front = - 1 , back = - 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] < a [ i - 1 ] ) { front = i - 1 ; break ; } } for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] > a [ i + 1 ] ) { back = i + 1 ; break ; } } if ( front == - 1 && back == - 1 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . println ( a [ i ] + " ▁ " ) ; return ; } while ( front <= back ) { int temp = a [ front ] ; a [ front ] = a [ back ] ; a [ back ] = temp ; front ++ ; back -- ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 7 , 6 , 5 , 4 , 3 , 2 , 8 } ; int n = a . length ; printSorted ( a , n ) ; ; } }

def printSorted ( a , n ) : NEW_LINE INDENT front = - 1 NEW_LINE back = - 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i - 1 ] ) : NEW_LINE INDENT front = i - 1 NEW_LINE break NEW_LINE DEDENT DEDENT i = n - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( a [ i ] > a [ i + 1 ] ) : NEW_LINE INDENT back = i + 1 NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( front == - 1 and back == - 1 ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT while ( front <= back ) : NEW_LINE INDENT temp = a [ front ] NEW_LINE a [ front ] = a [ back ] NEW_LINE a [ back ] = temp NEW_LINE front += 1 NEW_LINE back -= 1 NEW_LINE DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 7 , 6 , 5 , 4 , 3 , 2 , 8 ] NEW_LINE n = len ( a ) NEW_LINE printSorted ( a , n ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . Arrays ; public class Count_Subsequences { static final int MAX_CHAR = 256 ; static int countSub ( String str ) { int [ ] last = new int [ MAX_CHAR ] ; Arrays . fill ( last , - 1 ) ; int n = str . length ( ) ; int [ ] dp = new int [ n + 1 ] ; dp [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] = 2 * dp [ i - 1 ] ; if ( last [ ( int ) str . charAt ( i - 1 ) ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ ( int ) str . charAt ( i - 1 ) ] ] ; last [ ( int ) str . charAt ( i - 1 ) ] = ( i - 1 ) ; } return dp [ n ] ; } public static void main ( String args [ ] ) { System . out . println ( countSub ( " gfg " ) ) ; } }

MAX_CHAR = 256 NEW_LINE def countSub ( ss ) : NEW_LINE INDENT last = [ - 1 for i in range ( MAX_CHAR + 1 ) ] NEW_LINE n = len ( ss ) NEW_LINE dp = [ - 2 for i in range ( n + 1 ) ] NEW_LINE dp [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] = 2 * dp [ i - 1 ] NEW_LINE if last [ ord ( ss [ i - 1 ] ) ] != - 1 : NEW_LINE INDENT dp [ i ] = dp [ i ] - dp [ last [ ord ( ss [ i - 1 ] ) ] ] NEW_LINE DEDENT last [ ord ( ss [ i - 1 ] ) ] = i - 1 NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT print ( countSub ( " gfg " ) ) NEW_LINE


import java . io . * ; class GFG { static int sumOfSeries ( int n ) { int ans = 0 ; for ( int i = 1 ; i <= n ; i ++ ) ans += i * i * i * i ; return ans ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( sumOfSeries ( n ) ) ; } }

def sumOfSeries ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ans + i * i * i * i NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 4 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE


class GfG { static int LengthlongestPrefixSuffix ( String s ) { int n = s . length ( ) ; int lps [ ] = new int [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lps [ i ] = 0 ; i ++ ; } } } int res = lps [ n - 1 ] ; return ( res > n / 2 ) ? n / 2 : res ; } static String longestPrefixSuffix ( String s ) { int len = LengthlongestPrefixSuffix ( s ) ; String prefix = " " ; for ( int i = 0 ; i < len ; i ++ ) prefix += s . charAt ( i ) ; return prefix ; } public static void main ( String [ ] args ) { String s = " abcab " ; String ans = longestPrefixSuffix ( s ) ; if ( ans == " " ) System . out . println ( " - 1" ) ; else System . out . println ( ans ) ; } }

def LengthlongestPrefixSuffix ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lps = [ 0 for i in range ( n ) ] NEW_LINE len1 = 0 NEW_LINE i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( s [ i ] == s [ len1 ] ) : NEW_LINE INDENT len1 += 1 NEW_LINE lps [ i ] = len1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( len1 != 0 ) : NEW_LINE INDENT len1 = lps [ len1 - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lps [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT DEDENT res = lps [ n - 1 ] NEW_LINE if ( res > int ( n / 2 ) ) : NEW_LINE INDENT return int ( n / 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return res NEW_LINE DEDENT DEDENT def longestPrefixSuffix ( s ) : NEW_LINE INDENT len1 = LengthlongestPrefixSuffix ( s ) NEW_LINE prefix = " " NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT prefix += s [ i ] NEW_LINE DEDENT return prefix NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " abcab " NEW_LINE ans = longestPrefixSuffix ( s ) NEW_LINE if ( ans == " " ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void sortPerfectSquare ( int arr [ ] , int n ) { int a ; float b ; Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { b = ( float ) Math . sqrt ( arr [ i ] ) ; a = ( int ) b ; if ( a == b ) { v1 . add ( i ) ; v2 . add ( arr [ i ] ) ; } } Collections . sort ( v2 ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v1 . get ( j ) == i ) { arr [ i ] = v2 . get ( j ) ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 44 , 100 , 81 , 21 , 64 } ; int n = arr . length ; sortPerfectSquare ( arr , n ) ; } }

from math import sqrt NEW_LINE def sortPerfectSquare ( arr , n ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT b = sqrt ( arr [ i ] ) NEW_LINE a = int ( b ) NEW_LINE if a == b : NEW_LINE INDENT v1 . append ( i ) NEW_LINE v2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v2 . sort ( ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if v1 [ j ] == i : NEW_LINE INDENT arr [ i ] = v2 [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 9 , 44 , 100 , 81 , 21 , 64 ] NEW_LINE n = len ( arr ) NEW_LINE sortPerfectSquare ( arr , n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static ArrayList < Integer > v = new ArrayList < Integer > ( ) ; static void multiply ( int x ) { int carry = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { int res = carry + v . get ( i ) * x ; v . set ( i , res % 10 ) ; carry = res / 10 ; } while ( carry != 0 ) { v . add ( carry % 10 ) ; carry /= 10 ; } } static int findSumOfDigits ( int n ) { v . add ( 1 ) ; for ( int i = 1 ; i <= n ; i ++ ) multiply ( i ) ; int sum = 0 ; int size = v . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) sum += v . get ( i ) ; return sum ; } public static void main ( String [ ] args ) { int n = 1000 ; System . out . println ( findSumOfDigits ( n ) ) ; } }

def multiply ( v , x ) : NEW_LINE INDENT carry = 0 NEW_LINE size = len ( v ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT res = carry + v [ i ] * x NEW_LINE v [ i ] = res % 10 NEW_LINE carry = res // 10 NEW_LINE DEDENT while ( carry != 0 ) : NEW_LINE INDENT v . append ( carry % 10 ) NEW_LINE carry //= 10 NEW_LINE DEDENT DEDENT def findSumOfDigits ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE v . append ( 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT multiply ( v , i ) NEW_LINE DEDENT sum = 0 NEW_LINE size = len ( v ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT sum += v [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 NEW_LINE print ( findSumOfDigits ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int max_bitwise_or ( int L , int R ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) , v3 = new Vector < Integer > ( ) ; int z = 0 , i , ans = 0 , cnt = 1 ; while ( L > 0 ) { v1 . add ( L % 2 ) ; L = L / 2 ; } while ( R > 0 ) { v2 . add ( R % 2 ) ; R = R / 2 ; } while ( v1 . size ( ) != v2 . size ( ) ) { v1 . add ( 0 ) ; } for ( i = v2 . size ( ) - 1 ; i >= 0 ; i -- ) { if ( v2 . get ( i ) == 1 && v1 . get ( i ) == 0 && z == 0 ) { z = 1 ; continue ; } if ( z == 1 ) { v1 . remove ( i ) ; v1 . add ( i , 1 ) ; } } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { v3 . add ( v2 . get ( i ) | v1 . get ( i ) ) ; } for ( i = 0 ; i < v2 . size ( ) ; i ++ ) { if ( v3 . get ( i ) == 1 ) { ans += cnt ; } cnt *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int L = 10 , R = 20 ; System . out . println ( max_bitwise_or ( L , R ) ) ; } }

def max_bitwise_or ( L , R ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE v3 = [ ] NEW_LINE z = 0 NEW_LINE i = 0 NEW_LINE ans = 0 NEW_LINE cnt = 1 NEW_LINE while ( L > 0 ) : NEW_LINE INDENT v1 . append ( L % 2 ) NEW_LINE L = L // 2 NEW_LINE DEDENT while ( R > 0 ) : NEW_LINE INDENT v2 . append ( R % 2 ) NEW_LINE R = R // 2 NEW_LINE DEDENT while ( len ( v1 ) != len ( v2 ) ) : NEW_LINE INDENT v1 . append ( 0 ) NEW_LINE DEDENT for i in range ( len ( v2 ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( v2 [ i ] == 1 and v1 [ i ] == 0 and z == 0 ) : NEW_LINE INDENT z = 1 NEW_LINE continue NEW_LINE DEDENT if ( z == 1 ) : NEW_LINE INDENT v1 [ i ] = 1 NEW_LINE DEDENT DEDENT for i in range ( len ( v2 ) ) : NEW_LINE INDENT v3 . append ( v2 [ i ] | v1 [ i ] ) NEW_LINE DEDENT for i in range ( len ( v2 ) ) : NEW_LINE INDENT if ( v3 [ i ] == 1 ) : NEW_LINE INDENT ans += cnt NEW_LINE DEDENT cnt *= 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT L = 10 NEW_LINE R = 20 NEW_LINE print ( max_bitwise_or ( L , R ) ) NEW_LINE


import java . util . * ; class GFG { static int cnt = 0 ; static Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; static Vector < String > weight = new Vector < String > ( ) ; static boolean isPalindrome ( String x ) { int n = x . length ( ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) { if ( x . charAt ( i ) != x . charAt ( n - 1 - i ) ) return false ; } return true ; } static void dfs ( int node , int parent ) { String x = weight . get ( node ) ; if ( isPalindrome ( x ) ) cnt += 1 ; for ( int i = 0 ; i < graph . get ( node ) . size ( ) ; i ++ ) { if ( graph . get ( node ) . get ( i ) == parent ) continue ; dfs ( graph . get ( node ) . get ( i ) , node ) ; } } public static void main ( String args [ ] ) { weight . add ( " " ) ; weight . add ( " abc " ) ; weight . add ( " aba " ) ; weight . add ( " bcb " ) ; weight . add ( " moh " ) ; weight . add ( " aa " ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 3 ) ; graph . get ( 2 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . println ( cnt ) ; } }

cnt = 0 NEW_LINE graph = [ 0 ] * 100 NEW_LINE for i in range ( 100 ) : NEW_LINE INDENT graph [ i ] = [ ] NEW_LINE DEDENT weight = [ "0" ] * 100 NEW_LINE def isPalindrome ( x ) : NEW_LINE INDENT n = len ( x ) NEW_LINE for i in range ( 0 , n // 2 ) : NEW_LINE INDENT if x [ i ] != x [ n - 1 - i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def dfs ( node , parent ) : NEW_LINE INDENT global cnt NEW_LINE x = weight [ node ] NEW_LINE if ( isPalindrome ( x ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT for to in graph [ node ] : NEW_LINE INDENT if to == parent : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT weight [ 0 ] = " " NEW_LINE weight [ 1 ] = " abc " NEW_LINE weight [ 2 ] = " aba " NEW_LINE weight [ 3 ] = " bcb " NEW_LINE weight [ 4 ] = " moh " NEW_LINE weight [ 5 ] = " aa " NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( cnt ) NEW_LINE DEDENT


public class GFG { static void printPattern ( int n ) { int arr [ ] [ ] = new int [ n ] [ n ] ; int k , i , j , p = 1 , f ; for ( k = 0 ; k < n ; k ++ ) { j = k ; i = 0 ; while ( j >= 0 ) { arr [ i ] [ j ] = p ; p ++ ; i = i + 1 ; j = j - 1 ; } } for ( k = 1 ; k < n ; k ++ ) { i = k ; j = n - 1 ; f = k ; while ( j >= f ) { arr [ i ] [ j ] = p ; p ++ ; i = i + 1 ; j = j - 1 ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j < n ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; printPattern ( n ) ; } }

def printPattern ( n ) : NEW_LINE INDENT arr = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE p = 1 NEW_LINE for k in range ( n ) : NEW_LINE INDENT j = k NEW_LINE i = 0 NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT arr [ i ] [ j ] = p NEW_LINE p += 1 NEW_LINE i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT DEDENT for k in range ( 1 , n , 1 ) : NEW_LINE INDENT i = k NEW_LINE j = n - 1 NEW_LINE f = k NEW_LINE while ( j >= f ) : NEW_LINE INDENT arr [ i ] [ j ] = p NEW_LINE p += 1 NEW_LINE i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT for j in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE printPattern ( n ) NEW_LINE DEDENT


public class GFG { static int longestCommon ( int n , int m ) { int mx = - Integer . MAX_VALUE ; String s1 = Integer . toBinaryString ( n ) ; String s2 = Integer . toBinaryString ( m ) ; String res = null ; int len = s1 . length ( ) ; int l = len ; while ( len > 0 ) { for ( int i = 0 ; i < l - len + 1 ; i ++ ) { String temp = s1 . substring ( i , i + len ) ; int tlen = temp . length ( ) ; if ( tlen > mx && s2 . contains ( temp ) ) { res = temp ; mx = tlen ; } } len = len - 1 ; } if ( res == " " ) return - 1 ; return Integer . parseInt ( res , 2 ) ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 11 ; System . out . println ( " Longest ▁ common ▁ decimal ▁ value ▁ : ▁ " + longestCommon ( m , n ) ) ; } }

def getDecimal ( s ) : NEW_LINE INDENT lenn = len ( s ) NEW_LINE ans = 0 NEW_LINE j = 0 NEW_LINE for i in range ( lenn - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT ans += pow ( 2 , j ) NEW_LINE DEDENT j += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT def convertToBinary ( n ) : NEW_LINE INDENT return bin ( n ) [ 2 : ] NEW_LINE DEDENT def longestCommon ( n , m ) : NEW_LINE INDENT mx = - 10 ** 9 NEW_LINE s1 = convertToBinary ( n ) NEW_LINE s2 = convertToBinary ( m ) NEW_LINE res = " " NEW_LINE lenn = len ( s1 ) NEW_LINE l = lenn NEW_LINE while ( lenn > 0 ) : NEW_LINE INDENT for i in range ( l - lenn + 1 ) : NEW_LINE INDENT temp = s1 [ i : lenn + 1 ] NEW_LINE tlenn = len ( temp ) NEW_LINE if ( tlenn > mx and ( s2 . find ( temp ) != - 1 ) ) : NEW_LINE INDENT res = temp NEW_LINE mx = tlenn NEW_LINE DEDENT DEDENT lenn = lenn - 1 NEW_LINE DEDENT if ( res == " " ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return getDecimal ( res ) NEW_LINE DEDENT n = 10 NEW_LINE m = 11 NEW_LINE print ( " longest ▁ common ▁ decimal ▁ value ▁ : ▁ " , longestCommon ( m , n ) ) NEW_LINE


import java . lang . * ; import java . util . * ; class GFG { static boolean func ( int a [ ] , int k ) { for ( int i = 0 ; i < a . length ; i ++ ) if ( a [ i ] % k == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int [ ] a = { 14 , 27 , 38 , 76 , 84 } ; int k = 19 ; boolean res = func ( a , k ) ; System . out . println ( res ) ; } }

def func ( a , k , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] % k == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT a = [ 14 , 27 , 38 , 76 , 84 ] NEW_LINE k = 19 NEW_LINE res = func ( a , k , 5 ) NEW_LINE if ( res ) : NEW_LINE INDENT print ( " true " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " false " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isDivisible ( long n ) { while ( n / 100 > 0 ) { int last_digit = ( int ) n % 10 ; n /= 10 ; n += last_digit * 3 ; } return ( n % 29 == 0 ) ; } public static void main ( String [ ] args ) { long n = 348 ; if ( isDivisible ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isDivisible ( n ) : NEW_LINE INDENT while ( int ( n / 100 ) ) : NEW_LINE INDENT last_digit = int ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE n += last_digit * 3 NEW_LINE DEDENT return ( n % 29 == 0 ) NEW_LINE DEDENT n = 348 NEW_LINE if ( isDivisible ( n ) != 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isPerfectSquare ( int n ) { int sq = ( int ) Math . sqrt ( n ) ; return ( n == sq * sq ) ; } static int countPerfectDivisors ( int n ) { int count = 0 ; for ( int i = 1 ; i * i <= n ; ++ i ) { if ( n % i == 0 ) { if ( isPerfectSquare ( i ) ) ++ count ; if ( n / i != i && isPerfectSquare ( n / i ) ) ++ count ; } } return count ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . print ( " Total ▁ perfect ▁ " + " divisors ▁ of ▁ " + n ) ; System . out . println ( " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; n = 12 ; System . out . print ( " Total ▁ perfect ▁ " + " divisors ▁ of ▁ " + n ) ; System . out . println ( " ▁ = ▁ " + countPerfectDivisors ( n ) ) ; } }

import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sq = ( int ) ( math . sqrt ( x ) ) NEW_LINE return ( x == sq * sq ) NEW_LINE DEDENT def countPerfectDivisors ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 1 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if isPerfectSquare ( i ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT if n / i != i and isPerfectSquare ( n / i ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT print ( " Total ▁ perfect ▁ divisor ▁ of ▁ 16 ▁ = ▁ " , countPerfectDivisors ( 16 ) ) NEW_LINE print ( " Total ▁ perfect ▁ divisor ▁ of ▁ 12 ▁ = ▁ " , countPerfectDivisors ( 12 ) ) NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node next ; } static Node head = null ; static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head ) ; ( head ) = new_node ; } static Node reverseList ( Node head ) { Stack < Node > stk = new Stack < Node > ( ) ; Node ptr = head ; while ( ptr . next != null ) { stk . push ( ptr ) ; ptr = ptr . next ; } head = ptr ; while ( ! stk . isEmpty ( ) ) { ptr . next = stk . peek ( ) ; ptr = ptr . next ; stk . pop ( ) ; } ptr . next = null ; return head ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String [ ] args ) { push ( 5 ) ; push ( 4 ) ; push ( 3 ) ; push ( 2 ) ; push ( 1 ) ; head = reverseList ( head ) ; printList ( head ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data , next ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( new_data , self . head ) NEW_LINE self . head = new_node NEW_LINE DEDENT def reverseList ( self ) : NEW_LINE INDENT stk = [ ] NEW_LINE ptr = self . head NEW_LINE while ptr . next != None : NEW_LINE INDENT stk . append ( ptr ) NEW_LINE ptr = ptr . next NEW_LINE DEDENT self . head = ptr NEW_LINE while len ( stk ) != 0 : NEW_LINE INDENT ptr . next = stk . pop ( ) NEW_LINE ptr = ptr . next NEW_LINE DEDENT ptr . next = None NEW_LINE DEDENT def printList ( self ) : NEW_LINE INDENT curr = self . head NEW_LINE while curr : NEW_LINE INDENT print ( curr . data , end = " ▁ " ) NEW_LINE curr = curr . next NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT linkedList = LinkedList ( ) NEW_LINE linkedList . push ( 5 ) NEW_LINE linkedList . push ( 4 ) NEW_LINE linkedList . push ( 3 ) NEW_LINE linkedList . push ( 2 ) NEW_LINE linkedList . push ( 1 ) NEW_LINE linkedList . reverseList ( ) NEW_LINE linkedList . printList ( ) NEW_LINE DEDENT


public class Main { static int countWays ( int n ) { int count = 0 ; for ( int i = 1 ; i * i < n ; i ++ ) if ( n % i == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( countWays ( n ) ) ; } }

def countWays ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while ( ( i * i ) < n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT n = 12 NEW_LINE print ( countWays ( n ) ) NEW_LINE


import static java . lang . Math . pow ; class GFG { static double sum ( int x , int n ) { double i , total = 1.0 ; for ( i = 1 ; i <= n ; i ++ ) total = total + ( Math . pow ( x , i ) / i ) ; return total ; } public static void main ( String [ ] args ) { int x = 2 ; int n = 5 ; System . out . printf ( " % .2f " , sum ( x , n ) ) ; } }

def SUM ( x , n ) : NEW_LINE INDENT total = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT total = total + ( ( x ** i ) / i ) NEW_LINE DEDENT return total NEW_LINE DEDENT x = 2 NEW_LINE n = 5 NEW_LINE s = SUM ( x , n ) NEW_LINE print ( round ( s , 2 ) ) NEW_LINE


class GFG { public static void sequence ( int n ) { int f [ ] = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; System . out . print ( f [ 1 ] + " ▁ " + f [ 2 ] + " ▁ " ) ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; System . out . print ( f [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 13 ; sequence ( n ) ; } }

def sequence ( n ) : NEW_LINE INDENT f = [ 0 , 1 , 1 ] NEW_LINE print ( f [ 1 ] , end = " ▁ " ) , NEW_LINE print ( f [ 2 ] , end = " ▁ " ) , NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT f . append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ) NEW_LINE print ( f [ i ] , end = " ▁ " ) , NEW_LINE DEDENT DEDENT n = 13 NEW_LINE sequence ( n ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } public static int findGCD ( ArrayList < Integer > arr , int n ) { int result = arr . get ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) result = gcd ( arr . get ( i ) , result ) ; return result ; } public static void compute ( ArrayList < Integer > arr , int n ) { ArrayList < Integer > answer = new ArrayList < Integer > ( ) ; int GCD_of_array = findGCD ( arr , n ) ; if ( GCD_of_array == arr . get ( 0 ) ) { answer . add ( arr . get ( 0 ) ) ; for ( int i = 1 ; i < n ; i ++ ) { answer . add ( arr . get ( 0 ) ) ; answer . add ( arr . get ( i ) ) ; } for ( int i = 0 ; i < answer . size ( ) ; i ++ ) System . out . print ( answer . get ( i ) + " ▁ " ) ; } else System . out . print ( " No ▁ array ▁ " + " can ▁ be ▁ build " ) ; } public static void main ( String args [ ] ) { int n = 3 ; Integer input [ ] = { 2 , 5 , 6 , 7 , 11 } ; HashSet < Integer > GCD = new HashSet < Integer > ( Arrays . asList ( input ) ) ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; for ( int v : GCD ) arr . add ( v ) ; compute ( arr , n ) ; } }

from math import gcd NEW_LINE def findGCD ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = gcd ( arr [ i ] , result ) NEW_LINE DEDENT return result NEW_LINE DEDENT def compute ( arr , n ) : NEW_LINE INDENT answer = [ ] NEW_LINE GCD_of_array = findGCD ( arr , n ) NEW_LINE if ( GCD_of_array == arr [ 0 ] ) : NEW_LINE INDENT answer . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT answer . append ( arr [ 0 ] ) NEW_LINE answer . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( len ( answer ) ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " No ▁ array ▁ can ▁ be ▁ build " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE input = [ 2 , 5 , 6 , 7 , 11 ] NEW_LINE GCD = set ( ) NEW_LINE for i in range ( len ( input ) ) : NEW_LINE INDENT GCD . add ( input [ i ] ) NEW_LINE DEDENT arr = [ ] NEW_LINE for i in GCD : NEW_LINE INDENT arr . append ( i ) NEW_LINE DEDENT compute ( arr , n ) NEW_LINE DEDENT


class GFG { static int maxRectangles ( int L , int B , int l , int b ) { int horizontal = 0 , vertical = 0 ; if ( l <= L && b <= B ) { int columns = B / b ; int rows = L / l ; horizontal = rows * columns ; } if ( l <= B && b <= L ) { int columns = L / b ; int rows = B / l ; vertical = rows * columns ; } return Math . max ( horizontal , vertical ) ; } public static void main ( String [ ] args ) { int L = 10 , B = 7 , l = 4 , b = 3 ; System . out . print ( maxRectangles ( L , B , l , b ) ) ; } }

def maxRectangles ( L , B , l , b ) : NEW_LINE INDENT horizontal , vertical = 0 , 0 NEW_LINE if l <= L and b <= B : NEW_LINE INDENT columns = B // b NEW_LINE rows = L // l NEW_LINE horizontal = rows * columns NEW_LINE DEDENT if l <= B and b <= L : NEW_LINE INDENT columns = L // b NEW_LINE rows = B // l NEW_LINE vertical = rows * columns NEW_LINE DEDENT return max ( horizontal , vertical ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L , B , l , b = 10 , 7 , 4 , 3 NEW_LINE print ( maxRectangles ( L , B , l , b ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int INT_MAX = 2147483647 ; static int isKthBitSet ( int x , int k ) { return ( ( x & ( 1 << ( k - 1 ) ) ) > 0 ) ? 1 : 0 ; } static int leftmostSetBit ( int x ) { int count = 0 ; while ( x > 0 ) { count ++ ; x = x >> 1 ; } return count ; } static int isBinPalindrome ( int x ) { int l = leftmostSetBit ( x ) ; int r = 1 ; while ( l > r ) { if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) return 0 ; l -- ; r ++ ; } return 1 ; } static int findNthPalindrome ( int n ) { int pal_count = 0 ; int i = 0 ; for ( i = 1 ; i <= INT_MAX ; i ++ ) { if ( isBinPalindrome ( i ) > 0 ) { pal_count ++ ; } if ( pal_count == n ) break ; } return i ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( findNthPalindrome ( n ) ) ; } }

INT_MAX = 2147483647 NEW_LINE def isKthBitSet ( x , k ) : NEW_LINE INDENT return 1 if ( x & ( 1 << ( k - 1 ) ) ) else 0 NEW_LINE DEDENT def leftmostSetBit ( x ) : NEW_LINE INDENT count = 0 NEW_LINE while ( x ) : NEW_LINE INDENT count += 1 NEW_LINE x = x >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def isBinPalindrome ( x ) : NEW_LINE INDENT l = leftmostSetBit ( x ) NEW_LINE r = 1 NEW_LINE while ( l > r ) : NEW_LINE INDENT if ( isKthBitSet ( x , l ) != isKthBitSet ( x , r ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l -= 1 NEW_LINE r += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def findNthPalindrome ( n ) : NEW_LINE INDENT pal_count = 0 NEW_LINE i = 0 NEW_LINE for i in range ( 1 , INT_MAX + 1 ) : NEW_LINE INDENT if ( isBinPalindrome ( i ) ) : NEW_LINE INDENT pal_count += 1 NEW_LINE DEDENT if ( pal_count == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 NEW_LINE print ( findNthPalindrome ( n ) ) NEW_LINE DEDENT


class Main { static int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; else return ceilSearch ( arr , mid + 1 , high , x ) ; } else { if ( mid - 1 >= low && x > arr [ mid - 1 ] ) return mid ; else return ceilSearch ( arr , low , mid - 1 , x ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = arr . length ; int x = 8 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) System . out . println ( " Ceiling ▁ of ▁ " + x + " ▁ doesn ' t ▁ exist ▁ in ▁ array " ) ; else System . out . println ( " ceiling ▁ of ▁ " + x + " ▁ is ▁ " + arr [ index ] ) ; } }

def ceilSearch ( arr , low , high , x ) : NEW_LINE INDENT if x <= arr [ low ] : NEW_LINE INDENT return low NEW_LINE DEDENT if x > arr [ high ] : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( low + high ) / 2 ; NEW_LINE if arr [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] < x : NEW_LINE INDENT if mid + 1 <= high and x <= arr [ mid + 1 ] : NEW_LINE INDENT return mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ceilSearch ( arr , mid + 1 , high , x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if mid - 1 >= low and x > arr [ mid - 1 ] : NEW_LINE INDENT return mid NEW_LINE DEDENT else : NEW_LINE INDENT return ceilSearch ( arr , low , mid - 1 , x ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ] NEW_LINE n = len ( arr ) NEW_LINE x = 20 NEW_LINE index = ceilSearch ( arr , 0 , n - 1 , x ) ; NEW_LINE if index == - 1 : NEW_LINE INDENT print ( " Ceiling ▁ of ▁ % d ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " % x ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ceiling ▁ of ▁ % d ▁ is ▁ % d " % ( x , arr [ index ] ) ) NEW_LINE DEDENT


class GFG { static final int N = 4 ; static void subtract ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; int B [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; int C [ ] [ ] = new int [ N ] [ N ] ; int i , j ; subtract ( A , B , C ) ; System . out . print ( " Result ▁ matrix ▁ is ▁ \n " ) ; for ( i = 0 ; i < N ; i ++ ) { for ( j = 0 ; j < N ; j ++ ) System . out . print ( C [ i ] [ j ] + " ▁ " ) ; System . out . print ( " \n " ) ; } } }

N = 4 NEW_LINE def subtract ( A , B , C ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT A = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE B = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE C = A [ : ] [ : ] NEW_LINE subtract ( A , B , C ) NEW_LINE print ( " Result ▁ matrix ▁ is " ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( C [ i ] [ j ] , " ▁ " , end = ' ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT


class GFG { static int N = 8 ; static int INF = Integer . MAX_VALUE ; public static int shortestDist ( int [ ] [ ] graph ) { int [ ] dist = new int [ N ] ; dist [ N - 1 ] = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { dist [ i ] = INF ; for ( int j = i ; j < N ; j ++ ) { if ( graph [ i ] [ j ] == INF ) { continue ; } dist [ i ] = Math . min ( dist [ i ] , graph [ i ] [ j ] + dist [ j ] ) ; } } return dist [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] [ ] graph = new int [ ] [ ] { { INF , 1 , 2 , 5 , INF , INF , INF , INF } , { INF , INF , INF , INF , 4 , 11 , INF , INF } , { INF , INF , INF , INF , 9 , 5 , 16 , INF } , { INF , INF , INF , INF , INF , INF , 2 , INF } , { INF , INF , INF , INF , INF , INF , INF , 18 } , { INF , INF , INF , INF , INF , INF , INF , 13 } , { INF , INF , INF , INF , INF , INF , INF , 2 } } ; System . out . println ( shortestDist ( graph ) ) ; } }

def shortestDist ( graph ) : NEW_LINE INDENT global INF NEW_LINE dist = [ 0 ] * N NEW_LINE dist [ N - 1 ] = 0 NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT dist [ i ] = INF NEW_LINE for j in range ( N ) : NEW_LINE INDENT if graph [ i ] [ j ] == INF : NEW_LINE INDENT continue NEW_LINE DEDENT dist [ i ] = min ( dist [ i ] , graph [ i ] [ j ] + dist [ j ] ) NEW_LINE DEDENT DEDENT return dist [ 0 ] NEW_LINE DEDENT N = 8 NEW_LINE INF = 999999999999 NEW_LINE graph = [ [ INF , 1 , 2 , 5 , INF , INF , INF , INF ] , [ INF , INF , INF , INF , 4 , 11 , INF , INF ] , [ INF , INF , INF , INF , 9 , 5 , 16 , INF ] , [ INF , INF , INF , INF , INF , INF , 2 , INF ] , [ INF , INF , INF , INF , INF , INF , INF , 18 ] , [ INF , INF , INF , INF , INF , INF , INF , 13 ] , [ INF , INF , INF , INF , INF , INF , INF , 2 ] ] NEW_LINE print ( shortestDist ( graph ) ) NEW_LINE


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair Multiply ( pair p , pair q , int M ) { int x = ( ( p . first * q . first ) % M - ( p . second * q . second ) % M + M ) % M ; int y = ( ( p . first * q . second ) % M + ( p . second * q . first ) % M ) % M ; return new pair ( x , y ) ; } static pair compPow ( pair complex , int k , int M ) { pair res = new pair ( 1 , 0 ) ; while ( k > 0 ) { if ( k % 2 == 1 ) { res = Multiply ( res , complex , M ) ; } complex = Multiply ( complex , complex , M ) ; k = k >> 1 ; } return res ; } public static void main ( String [ ] args ) { int A = 7 , B = 3 , k = 10 , M = 97 ; pair ans = compPow ( new pair ( A , B ) , k , M ) ; System . out . println ( ans . first + " ▁ + ▁ i " + ans . second ) ; } }

def Multiply ( p , q , M ) : NEW_LINE INDENT x = ( ( p [ 0 ] * q [ 0 ] ) % M -   \ ( p [ 1 ] * q [ 1 ] ) % M + M ) % M NEW_LINE y = ( ( p [ 0 ] * q [ 1 ] ) % M +   \ ( p [ 1 ] * q [ 0 ] ) % M ) % M NEW_LINE return [ x , y ] NEW_LINE DEDENT def compPow ( complex , k , M ) : NEW_LINE INDENT res = [ 1 , 0 ] NEW_LINE while ( k > 0 ) : NEW_LINE INDENT if ( k & 1 ) : NEW_LINE INDENT res = Multiply ( res , complex , M ) NEW_LINE DEDENT complex = Multiply ( complex , complex , M ) NEW_LINE k = k >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 7 NEW_LINE B = 3 NEW_LINE k = 10 NEW_LINE M = 97 NEW_LINE ans = compPow ( [ A , B ] , k , M ) NEW_LINE print ( ans [ 0 ] , " + ▁ i " , end = " " ) NEW_LINE print ( ans [ 1 ] ) NEW_LINE DEDENT


import java . util . * ; class GfG { static int getMedian ( int [ ] a , int [ ] b , int startA , int startB , int endA , int endB ) { if ( endA - startA == 1 ) { return ( Math . max ( a [ startA ] , b [ startB ] ) + Math . min ( a [ endA ] , b [ endB ] ) ) / 2 ; } int m1 = median ( a , startA , endA ) ; int m2 = median ( b , startB , endB ) ; if ( m1 == m2 ) { return m1 ; } else if ( m1 < m2 ) { return getMedian ( a , b , ( endA + startA + 1 ) / 2 , startB , endA , ( endB + startB + 1 ) / 2 ) ; } else { return getMedian ( a , b , startA , ( endB + startB + 1 ) / 2 , ( endA + startA + 1 ) / 2 , endB ) ; } } static int median ( int [ ] arr , int start , int end ) { int n = end - start + 1 ; if ( n % 2 == 0 ) { return ( arr [ start + ( n / 2 ) ] + arr [ start + ( n / 2 - 1 ) ] ) / 2 ; } else { return arr [ start + n / 2 ] ; } } public static void main ( String [ ] args ) { int ar1 [ ] = { 1 , 2 , 3 , 6 } ; int ar2 [ ] = { 4 , 6 , 8 , 10 } ; int n1 = ar1 . length ; int n2 = ar2 . length ; if ( n1 != n2 ) { System . out . println ( " Doesn ' t ▁ work ▁ for ▁ arrays ▁ " + " of ▁ unequal ▁ size " ) ; } else if ( n1 == 0 ) { System . out . println ( " Arrays ▁ are ▁ empty . " ) ; } else if ( n1 == 1 ) { System . out . println ( ( ar1 [ 0 ] + ar2 [ 0 ] ) / 2 ) ; } else { System . out . println ( " Median ▁ is ▁ " + getMedian ( ar1 , ar2 , 0 , 0 , ar1 . length - 1 , ar2 . length - 1 ) ) ; } } }

def getMedian ( arr1 , arr2 , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT elif n == 1 : NEW_LINE INDENT return ( arr1 [ 0 ] + arr2 [ 1 ] ) / 2 NEW_LINE DEDENT elif n == 2 : NEW_LINE INDENT return ( max ( arr1 [ 0 ] , arr2 [ 0 ] ) + min ( arr1 [ 1 ] , arr2 [ 1 ] ) ) / 2 NEW_LINE DEDENT else : NEW_LINE INDENT m1 = median ( arr1 , n ) NEW_LINE m2 = median ( arr2 , n ) NEW_LINE if m1 > m2 : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT return getMedian ( arr1 [ : int ( n / 2 ) + 1 ] , arr2 [ int ( n / 2 ) - 1 : ] , int ( n / 2 ) + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return getMedian ( arr1 [ : int ( n / 2 ) + 1 ] , arr2 [ int ( n / 2 ) : ] , int ( n / 2 ) + 1 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT return getMedian ( arr1 [ int ( n / 2 - 1 ) : ] , arr2 [ : int ( n / 2 + 1 ) ] , int ( n / 2 ) + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return getMedian ( arr1 [ int ( n / 2 ) : ] , arr2 [ 0 : int ( n / 2 ) + 1 ] , int ( n / 2 ) + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def median ( arr , n ) : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT return ( arr [ int ( n / 2 ) ] + arr [ int ( n / 2 ) - 1 ] ) / 2 NEW_LINE DEDENT else : NEW_LINE INDENT return arr [ int ( n / 2 ) ] NEW_LINE DEDENT DEDENT arr1 = [ 1 , 2 , 3 , 6 ] NEW_LINE arr2 = [ 4 , 6 , 8 , 10 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( int ( getMedian ( arr1 , arr2 , n ) ) ) NEW_LINE


import java . util . Arrays ; public class GfG { public static void FindAllElements ( int n , int k ) { int sum = k ; int [ ] A = new int [ k ] ; Arrays . fill ( A , 0 , k , 1 ) ; for ( int i = k - 1 ; i >= 0 ; -- i ) { while ( sum + A [ i ] <= n ) { sum += A [ i ] ; A [ i ] *= 2 ; } } if ( sum != n ) { System . out . print ( " Impossible " ) ; } else { for ( int i = 0 ; i < k ; ++ i ) System . out . print ( A [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 12 ; int k = 6 ; FindAllElements ( n , k ) ; } }

def FindAllElements ( n , k ) : NEW_LINE INDENT sum = k NEW_LINE A = [ 1 for i in range ( k ) ] NEW_LINE i = k - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT while ( sum + A [ i ] <= n ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE A [ i ] *= 2 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( sum != n ) : NEW_LINE INDENT print ( " Impossible " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 0 , k , 1 ) : NEW_LINE INDENT print ( A [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE k = 6 NEW_LINE FindAllElements ( n , k ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class Graph { int V ; Vector < Integer > [ ] adj ; Graph ( int V ) { this . V = V ; this . adj = new Vector [ V ] ; for ( int i = 0 ; i < V ; i ++ ) adj [ i ] = new Vector < Integer > ( ) ; } void addEdge ( int u , int v ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } int countEdges ( ) { int sum = 0 ; for ( int i = 0 ; i < V ; i ++ ) sum += adj [ i ] . size ( ) ; return sum / 2 ; } } class GFG { public static void main ( String [ ] args ) throws IOException { int V = 9 ; Graph g = new Graph ( V ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 7 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 1 , 7 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 2 , 8 ) ; g . addEdge ( 2 , 5 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 3 , 5 ) ; g . addEdge ( 4 , 5 ) ; g . addEdge ( 5 , 6 ) ; g . addEdge ( 6 , 7 ) ; g . addEdge ( 6 , 8 ) ; g . addEdge ( 7 , 8 ) ; System . out . println ( g . countEdges ( ) ) ; } }

class Graph : NEW_LINE INDENT def __init__ ( self , V ) : NEW_LINE INDENT self . V = V NEW_LINE self . adj = [ [ ] for i in range ( V ) ] NEW_LINE DEDENT def addEdge ( self , u , v ) : NEW_LINE INDENT self . adj [ u ] . append ( v ) NEW_LINE self . adj [ v ] . append ( u ) NEW_LINE DEDENT def countEdges ( self ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( self . V ) : NEW_LINE INDENT Sum += len ( self . adj [ i ] ) NEW_LINE DEDENT return Sum // 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT V = 9 NEW_LINE g = Graph ( V ) NEW_LINE g . addEdge ( 0 , 1 ) NEW_LINE g . addEdge ( 0 , 7 ) NEW_LINE g . addEdge ( 1 , 2 ) NEW_LINE g . addEdge ( 1 , 7 ) NEW_LINE g . addEdge ( 2 , 3 ) NEW_LINE g . addEdge ( 2 , 8 ) NEW_LINE g . addEdge ( 2 , 5 ) NEW_LINE g . addEdge ( 3 , 4 ) NEW_LINE g . addEdge ( 3 , 5 ) NEW_LINE g . addEdge ( 4 , 5 ) NEW_LINE g . addEdge ( 5 , 6 ) NEW_LINE g . addEdge ( 6 , 7 ) NEW_LINE g . addEdge ( 6 , 8 ) NEW_LINE g . addEdge ( 7 , 8 ) NEW_LINE print ( g . countEdges ( ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean ispower ( long n ) { if ( n < 125 ) { return ( n == 1 || n == 5 || n == 25 ) ; } if ( n % 125 != 0 ) { return false ; } else { return ispower ( n / 125 ) ; } } static long number ( String s , int i , int j ) { long ans = 0 ; for ( int x = i ; x < j ; x ++ ) { ans = ans * 2 + ( s . charAt ( x ) - '0' ) ; } return ans ; } static int minCuts ( String s , int n ) { int [ ] dp = new int [ n + 1 ] ; Arrays . fill ( dp , n + 1 ) ; dp [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( s . charAt ( i - 1 ) == '0' ) { continue ; } for ( int j = 0 ; j < i ; j ++ ) { if ( s . charAt ( j ) == '0' ) { continue ; } long num = number ( s , j , i ) ; if ( ! ispower ( num ) ) { continue ; } dp [ i ] = Math . min ( dp [ i ] , dp [ j ] + 1 ) ; } } return ( ( dp [ n ] < n + 1 ) ? dp [ n ] : - 1 ) ; } public static void main ( String [ ] args ) { String s = "101101101" ; int n = s . length ( ) ; System . out . println ( minCuts ( s , n ) ) ; } }

def ispower ( n ) : NEW_LINE INDENT if ( n < 125 ) : NEW_LINE INDENT return ( n == 1 or n == 5 or n == 25 ) NEW_LINE DEDENT if ( n % 125 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ispower ( n // 125 ) NEW_LINE DEDENT DEDENT def number ( s , i , j ) : NEW_LINE INDENT ans = 0 NEW_LINE for x in range ( i , j ) : NEW_LINE INDENT ans = ans * 2 + ( ord ( s [ x ] ) - ord ( '0' ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT def minCuts ( s , n ) : NEW_LINE INDENT dp = [ n + 1 for i in range ( n + 1 ) ] NEW_LINE dp [ 0 ] = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( s [ i - 1 ] == '0' ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i ) : NEW_LINE INDENT if ( s [ j ] == '0' ) : NEW_LINE INDENT continue NEW_LINE DEDENT num = number ( s , j , i ) NEW_LINE if ( not ispower ( num ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT dp [ i ] = min ( dp [ i ] , dp [ j ] + 1 ) NEW_LINE DEDENT DEDENT if dp [ n ] < n + 1 : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "101101101" NEW_LINE n = len ( s ) NEW_LINE print ( minCuts ( s , n ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; import java . math . BigInteger ; class GFG { static boolean getParity ( int n ) { boolean parity = false ; while ( n != 0 ) { parity = ! parity ; n = n & ( n - 1 ) ; } return parity ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( " Parity ▁ of ▁ no ▁ " + n + " ▁ = ▁ " + ( getParity ( n ) ? " odd " : " even " ) ) ; } }

def getParity ( n ) : NEW_LINE INDENT parity = 0 NEW_LINE while n : NEW_LINE INDENT parity = ~ parity NEW_LINE n = n & ( n - 1 ) NEW_LINE DEDENT return parity NEW_LINE DEDENT n = 7 NEW_LINE print ( " Parity ▁ of ▁ no ▁ " , n , " ▁ = ▁ " , ( " odd " if getParity ( n ) else " even " ) ) NEW_LINE


import java . util . ArrayList ; class GFG { static boolean CheckArray ( int arr [ ] , int n ) { int prod = 1 ; ArrayList < Integer > freq = new ArrayList < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { freq . add ( arr [ i ] ) ; prod *= arr [ i ] ; } int root = ( int ) Math . sqrt ( prod ) ; if ( root * root == prod ) { if ( freq . contains ( root ) & freq . lastIndexOf ( root ) != ( freq . size ( ) ) ) { return true ; } } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 12 , 3 , 2 } ; int n = arr . length ; if ( CheckArray ( arr , n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

import math NEW_LINE def CheckArray ( arr , n ) : NEW_LINE INDENT prod = 1 NEW_LINE freq = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq . append ( arr [ i ] ) NEW_LINE prod *= arr [ i ] NEW_LINE DEDENT root = math . sqrt ( prod ) NEW_LINE if ( root * root == prod ) : NEW_LINE INDENT if root in freq : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 12 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( CheckArray ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


class GFG { static boolean canBePicked ( int digits [ ] , int num ) { int copyDigits [ ] = digits . clone ( ) ; while ( num > 0 ) { int digit = num % 10 ; if ( copyDigits [ digit ] == 0 ) return false ; else copyDigits [ digit ] -- ; num /= 10 ; } return true ; } static int countAlphabets ( int n ) { int i , count = 0 ; int digits [ ] = new int [ 10 ] ; while ( n > 0 ) { int digit = n % 10 ; digits [ digit ] ++ ; n /= 10 ; } for ( i = ' a ' ; i <= ' z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; for ( i = ' A ' ; i <= ' Z ' ; i ++ ) if ( canBePicked ( digits , i ) ) count ++ ; return count ; } public static void main ( String [ ] args ) { int n = 1623455078 ; System . out . println ( countAlphabets ( n ) ) ; } }

import math NEW_LINE def canBePicked ( digits , num ) : NEW_LINE INDENT copyDigits = [ ] ; NEW_LINE for i in range ( len ( digits ) ) : NEW_LINE INDENT copyDigits . append ( digits [ i ] ) ; NEW_LINE DEDENT while ( num > 0 ) : NEW_LINE INDENT digit = num % 10 ; NEW_LINE if ( copyDigits [ digit ] == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT copyDigits [ digit ] -= 1 ; NEW_LINE DEDENT num = math . floor ( num / 10 ) ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def countAlphabets ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE digits = [ 0 ] * 10 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % 10 ; NEW_LINE digits [ digit ] += 1 ; NEW_LINE n = math . floor ( n / 10 ) ; NEW_LINE DEDENT for i in range ( ord ( ' a ' ) , ord ( ' z ' ) + 1 ) : NEW_LINE INDENT if ( canBePicked ( digits , i ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT for i in range ( ord ( ' A ' ) , ord ( ' Z ' ) + 1 ) : NEW_LINE INDENT if ( canBePicked ( digits , i ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT n = 1623455078 ; NEW_LINE print ( countAlphabets ( n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int findMaxDiff ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . println ( " Invalid ▁ " ) ; return 0 ; } int min_val = Integer . MAX_VALUE , max_val = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - i ) > max_val ) max_val = arr [ i ] - i ; if ( ( arr [ i ] - i ) < min_val ) min_val = arr [ i ] - i ; } return ( max_val - min_val ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 15 , 4 , 12 , 13 } ; int n = arr . length ; System . out . println ( findMaxDiff ( arr , n ) ) ; } }

import sys NEW_LINE def findMaxDiff ( a , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " Invalid ▁ " ) NEW_LINE return 0 NEW_LINE DEDENT min_val = sys . maxsize NEW_LINE max_val = - sys . maxsize - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] - i ) > max_val ) : NEW_LINE INDENT max_val = a [ i ] - i NEW_LINE DEDENT if ( ( a [ i ] - i ) < min_val ) : NEW_LINE INDENT min_val = a [ i ] - i NEW_LINE DEDENT DEDENT return ( max_val - min_val ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 15 , 4 , 12 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxDiff ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int validPosition ( int arr [ ] , int N , int K ) { int count = 0 , sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { sum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 6 , 7 } , K = 4 ; int N = arr . length ; System . out . println ( validPosition ( arr , N , K ) ) ; } }

def validPosition ( arr , N , K ) : NEW_LINE INDENT count = 0 ; sum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( ( arr [ i ] + K ) > ( sum - arr [ i ] ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ 2 , 1 , 6 , 7 ] ; NEW_LINE K = 4 ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( validPosition ( arr , N , K ) ) ; NEW_LINE


import java . io . * ; class GFG { static Boolean isPentagonal ( int N ) { double n = ( 1 + Math . sqrt ( 24 * N + 1 ) ) / 6 ; return ( n - ( int ) n ) == 0 ; } public static void main ( String [ ] args ) { int N = 19 ; if ( isPentagonal ( N ) ) System . out . println ( N + " ▁ is ▁ pentagonal ▁ " ) ; else System . out . println ( N + " ▁ is ▁ not ▁ pentagonal " ) ; } }

import math as m NEW_LINE def isPentagonal ( n ) : NEW_LINE INDENT n = ( 1 + m . sqrt ( 24 * N + 1 ) ) / 6 NEW_LINE return ( ( n - int ( n ) ) == 0 ) NEW_LINE DEDENT N = 19 NEW_LINE if ( isPentagonal ( N ) ) : NEW_LINE INDENT print ( N , " ▁ is ▁ pentagonal ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( N , " ▁ is ▁ not ▁ pentagonal " ) NEW_LINE DEDENT


import java . util . * ; class solution { static int fact ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result *= i ; return result ; } static int getResult ( String str , char ch ) { int has [ ] = new int [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) has [ i ] = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) has [ str . charAt ( i ) - ' A ' ] ++ ; int particular = has [ ch - ' A ' ] ; if ( particular == 0 ) return 0 ; has [ ch - ' A ' ] = 0 ; int total = str . length ( ) ; total = total - particular + 1 ; int result = fact ( total ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( has [ i ] > 1 ) { result = result / fact ( has [ i ] ) ; } } return result ; } public static void main ( String args [ ] ) { String str = " MISSISSIPPI " ; System . out . println ( getResult ( str , ' S ' ) ) ; } }

def fact ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT result *= i NEW_LINE DEDENT return result NEW_LINE DEDENT def getResult ( string , ch ) : NEW_LINE INDENT has = [ 0 ] * 26 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT has [ ord ( string [ i ] ) - ord ( ' A ' ) ] += 1 NEW_LINE DEDENT particular = has [ ord ( ch ) - ord ( ' A ' ) ] NEW_LINE if particular == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT has [ ord ( ch ) - ord ( ' A ' ) ] = 0 NEW_LINE total = len ( string ) NEW_LINE total = total - particular + 1 NEW_LINE result = fact ( total ) NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if has [ i ] > 1 : NEW_LINE INDENT result /= fact ( has [ i ] ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " MISSISSIPPI " NEW_LINE print ( getResult ( string , ' S ' ) ) NEW_LINE DEDENT


class GfG { public static void main ( String [ ] args ) { int a = 10 , b = 5 ; if ( b > 0 ) { while ( b > 0 ) { a ++ ; b -- ; } } if ( b < 0 ) { while ( b < 0 ) { a -- ; b ++ ; } } System . out . println ( " Sum ▁ is : ▁ " + a ) ; } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 5 NEW_LINE if b > 0 : NEW_LINE INDENT while b > 0 : NEW_LINE INDENT a = a + 1 NEW_LINE b = b - 1 NEW_LINE DEDENT DEDENT if b < 0 : NEW_LINE INDENT while b < 0 : NEW_LINE INDENT a = a - 1 NEW_LINE b = b + 1 NEW_LINE DEDENT DEDENT print ( " Sum ▁ is : ▁ " , a ) NEW_LINE DEDENT