import java . util . * ; class GFG { static int MAX = 50002 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; public static void sieve ( ) { boolean isPrime [ ] = new boolean [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] == true ) primes . add ( p ) ; } public static int power ( int x , int y ) { int count = 0 ; int z = y ; while ( x >= z ) { count += ( x / z ) ; z *= y ; } return count ; } public static int modMult ( int a , int b , int mod ) { int res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static int countWays ( int n , int m ) { int ans = 1 ; for ( int i = 1 ; i < primes . size ( ) ; i ++ ) { int powers = power ( n , primes . get ( i ) ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } public static void main ( String [ ] args ) { sieve ( ) ; int n = 4 , m = 7 ; System . out . println ( countWays ( n , m ) ) ; } }

MAX = 50002 ; NEW_LINE primes = [ ] NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime = [ True ] * ( MAX ) NEW_LINE p = 2 NEW_LINE while p * p < MAX : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , MAX , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , MAX ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT primes . append ( p ) NEW_LINE DEDENT DEDENT DEDENT def power ( x , y ) : NEW_LINE INDENT count = 0 NEW_LINE z = y NEW_LINE while ( x >= z ) : NEW_LINE INDENT count += ( x // z ) NEW_LINE z *= y NEW_LINE DEDENT return count NEW_LINE DEDENT def modMult ( a , b , mod ) : NEW_LINE INDENT res = 0 NEW_LINE a = a % mod NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( b % 2 == 1 ) : NEW_LINE INDENT res = ( res + a ) % mod NEW_LINE DEDENT a = ( a * 2 ) % mod NEW_LINE b //= 2 NEW_LINE DEDENT return res % mod NEW_LINE DEDENT def countWays ( n , m ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , len ( primes ) ) : NEW_LINE INDENT powers = power ( n , primes [ i ] ) NEW_LINE if ( powers == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = modMult ( ans , powers + 1 , m ) % m NEW_LINE DEDENT if ( ( ( ans - 1 ) % m ) < 0 ) : NEW_LINE INDENT return ( ans - 1 + m ) % m NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans - 1 ) % m NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) NEW_LINE n = 4 NEW_LINE m = 7 NEW_LINE print ( countWays ( n , m ) ) NEW_LINE DEDENT


import java . util . Vector ; class GFG { static Vector < Integer > deleteElement ( int A [ ] , int L , int R , int N ) { Vector < Integer > B = new Vector < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( i <= L || i >= R ) { B . add ( A [ i ] ) ; } } return B ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 3 , 4 , 9 , 3 , 1 , 6 , 3 , 11 , 12 , 3 } ; int L = 2 , R = 7 ; int n = A . length ; Vector < Integer > res = deleteElement ( A , L , R , n ) ; for ( Integer x : res ) { System . out . print ( x + " ▁ " ) ; } } }

def deleteElement ( A , L , R , N ) : NEW_LINE INDENT B = [ ] NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE INDENT if ( i <= L or i >= R ) : NEW_LINE INDENT B . append ( A [ i ] ) NEW_LINE DEDENT DEDENT return B NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 5 , 3 , 4 , 9 , 3 , 1 , 6 , 3 , 11 , 12 , 3 ] NEW_LINE L = 2 NEW_LINE R = 7 NEW_LINE n = len ( A ) NEW_LINE res = deleteElement ( A , L , R , n ) NEW_LINE for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


class GFG { static int swapNibbles ( int x ) { return ( ( x & 0x0F ) << 4 | ( x & 0xF0 ) >> 4 ) ; } public static void main ( String arg [ ] ) { int x = 100 ; System . out . print ( swapNibbles ( x ) ) ; } }

def swapNibbles ( x ) : NEW_LINE INDENT return ( ( x & 0x0F ) << 4 | ( x & 0xF0 ) >> 4 ) NEW_LINE DEDENT x = 100 NEW_LINE print ( swapNibbles ( x ) ) NEW_LINE


import java . util . HashMap ; class GFG { public static int minOperations ( int [ ] a , int n , int K ) { HashMap < Integer , Boolean > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( a [ i ] ) ) return 0 ; map . put ( a [ i ] , true ) ; } int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = a [ i ] | K ; map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) map . put ( b [ i ] , true ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( a [ i ] ) ) return 1 ; } map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( b [ i ] ) ) return 2 ; map . put ( b [ i ] , true ) ; } return - 1 ; } public static void main ( String [ ] args ) { int K = 3 ; int [ ] a = { 1 , 9 , 4 , 3 } ; int n = a . length ; System . out . println ( minOperations ( a , n , K ) ) ; } }

def minOperations ( a , n , K ) : NEW_LINE INDENT map = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( map [ a [ i ] ] ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT map [ a [ i ] ] = True ; NEW_LINE DEDENT b = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT b [ i ] = a [ i ] | K ; NEW_LINE DEDENT map . clear ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE INDENT map [ b [ i ] ] = True ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in map : NEW_LINE INDENT pass NEW_LINE DEDENT elif ( map [ a [ i ] ] ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT map . clear ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( map [ b [ i ] ] ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT map [ b [ i ] ] = true ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 3 ; NEW_LINE a = [ 1 , 9 , 4 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( minOperations ( a , n , K ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static long kBitDifferencePairs ( int arr [ ] , int n , int k ) { int MAX = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; long [ ] count = new long [ MAX + 1 ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; long ans = 0 ; if ( k == 0 ) { for ( int i = 0 ; i <= MAX ; ++ i ) ans += ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ; return ans ; } for ( int i = 0 ; i <= MAX ; ++ i ) { if ( count [ i ] == 0 ) continue ; for ( int j = i + 1 ; j <= MAX ; ++ j ) { if ( Integer . bitCount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; k = 3 ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; } }

def kBitDifferencePairs ( arr , n , k ) : NEW_LINE INDENT MAX = max ( arr ) NEW_LINE count = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE if ( k == 0 ) : NEW_LINE INDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT ans += ( count [ i ] * ( count [ i ] - 1 ) ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT if ( count [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , MAX + 1 ) : NEW_LINE INDENT if ( bin ( i ^ j ) . count ( '1' ) == k ) : NEW_LINE INDENT ans += count [ i ] * count [ j ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT k = 2 NEW_LINE arr = [ 2 , 4 , 1 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Total ▁ pairs ▁ for ▁ k ▁ = " , k , " are " , kBitDifferencePairs ( arr , n , k ) ) NEW_LINE k = 3 NEW_LINE print ( " Total ▁ pairs ▁ for ▁ k ▁ = " , k , " are " , kBitDifferencePairs ( arr , n , k ) ) NEW_LINE


import java . io . * ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } public static void main ( String [ ] args ) { int n = 36 ; int res = makeOdd ( n ) ; System . out . println ( res ) ; } }

def makeOdd ( n ) : NEW_LINE INDENT if n % 2 != 0 : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 and ( int ) ( n / i ) % 2 == 1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT DEDENT n = 36 ; NEW_LINE print ( makeOdd ( n ) ) ; NEW_LINE


class GFG { static int getPairs ( int a [ ] ) { int count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < a . length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 3 , 1 } ; System . out . println ( getPairs ( a ) ) ; } }

def getPairs ( a ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] < a [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 4 , 3 , 1 ] NEW_LINE print ( getPairs ( a ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { System . out . printf ( " % .2f \n " , bal - x - 1.50 ) ; } else { System . out . printf ( " % .2f \n " , bal ) ; } } public static void main ( String [ ] args ) { int x = 50 ; float bal = ( float ) 100.50 ; findBalance ( x , bal ) ; } }

def findBalance ( x , bal ) : NEW_LINE INDENT if ( x % 10 == 0 and ( x + 1.50 ) <= bal ) : NEW_LINE INDENT print ( round ( bal - x - 1.50 , 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( round ( bal , 2 ) ) NEW_LINE DEDENT DEDENT x = 50 NEW_LINE bal = 100.50 NEW_LINE findBalance ( x , bal ) NEW_LINE


class Geeks { static int countways ( int n ) { if ( n == 1 ) return 0 ; else if ( n % 2 == 0 ) return 1 + countways ( n / 2 ) ; else return 1 + Math . min ( countways ( n - 1 ) , countways ( n + 1 ) ) ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( countways ( n ) ) ; } }

def countways ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT return 1 + countways ( n / 2 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + min ( countways ( n - 1 ) , countways ( n + 1 ) ) ; NEW_LINE DEDENT DEDENT n = 15 ; NEW_LINE print ( countways ( n ) ) ; NEW_LINE


class GFG { static class pair { double F , S ; public pair ( double F , double S ) { this . F = F ; this . S = S ; } public pair ( ) { } } static double minDistance ( pair A , pair B , pair E ) { pair AB = new pair ( ) ; AB . F = B . F - A . F ; AB . S = B . S - A . S ; pair BE = new pair ( ) ; BE . F = E . F - B . F ; BE . S = E . S - B . S ; pair AE = new pair ( ) ; AE . F = E . F - A . F ; AE . S = E . S - A . S ; double AB_BE , AB_AE ; AB_BE = ( AB . F * BE . F + AB . S * BE . S ) ; AB_AE = ( AB . F * AE . F + AB . S * AE . S ) ; double reqAns = 0 ; if ( AB_BE > 0 ) { double y = E . S - B . S ; double x = E . F - B . F ; reqAns = Math . sqrt ( x * x + y * y ) ; } else if ( AB_AE < 0 ) { double y = E . S - A . S ; double x = E . F - A . F ; reqAns = Math . sqrt ( x * x + y * y ) ; } else { double x1 = AB . F ; double y1 = AB . S ; double x2 = AE . F ; double y2 = AE . S ; double mod = Math . sqrt ( x1 * x1 + y1 * y1 ) ; reqAns = Math . abs ( x1 * y2 - y1 * x2 ) / mod ; } return reqAns ; } public static void main ( String [ ] args ) { pair A = new pair ( 0 , 0 ) ; pair B = new pair ( 2 , 0 ) ; pair E = new pair ( 1 , 1 ) ; System . out . print ( ( int ) minDistance ( A , B , E ) ) ; } }

from math import sqrt NEW_LINE def minDistance ( A , B , E ) : NEW_LINE INDENT AB = [ None , None ] ; NEW_LINE AB [ 0 ] = B [ 0 ] - A [ 0 ] ; NEW_LINE AB [ 1 ] = B [ 1 ] - A [ 1 ] ; NEW_LINE BE = [ None , None ] ; NEW_LINE BE [ 0 ] = E [ 0 ] - B [ 0 ] ; NEW_LINE BE [ 1 ] = E [ 1 ] - B [ 1 ] ; NEW_LINE AE = [ None , None ] ; NEW_LINE AE [ 0 ] = E [ 0 ] - A [ 0 ] ; NEW_LINE AE [ 1 ] = E [ 1 ] - A [ 1 ] ; NEW_LINE AB_BE = AB [ 0 ] * BE [ 0 ] + AB [ 1 ] * BE [ 1 ] ; NEW_LINE AB_AE = AB [ 0 ] * AE [ 0 ] + AB [ 1 ] * AE [ 1 ] ; NEW_LINE reqAns = 0 ; NEW_LINE if ( AB_BE > 0 ) : NEW_LINE INDENT y = E [ 1 ] - B [ 1 ] ; NEW_LINE x = E [ 0 ] - B [ 0 ] ; NEW_LINE reqAns = sqrt ( x * x + y * y ) ; NEW_LINE DEDENT elif ( AB_AE < 0 ) : NEW_LINE INDENT y = E [ 1 ] - A [ 1 ] ; NEW_LINE x = E [ 0 ] - A [ 0 ] ; NEW_LINE reqAns = sqrt ( x * x + y * y ) ; NEW_LINE DEDENT else : NEW_LINE INDENT x1 = AB [ 0 ] ; NEW_LINE y1 = AB [ 1 ] ; NEW_LINE x2 = AE [ 0 ] ; NEW_LINE y2 = AE [ 1 ] ; NEW_LINE mod = sqrt ( x1 * x1 + y1 * y1 ) ; NEW_LINE reqAns = abs ( x1 * y2 - y1 * x2 ) / mod ; NEW_LINE DEDENT return reqAns ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 0 , 0 ] ; NEW_LINE B = [ 2 , 0 ] ; NEW_LINE E = [ 1 , 1 ] ; NEW_LINE print ( minDistance ( A , B , E ) ) ; NEW_LINE DEDENT


class GFG { static void findNthNum ( int N ) { int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; System . out . print ( ( 1 << bit_L ) + ( 1 << bit_R ) + " \n " ) ; } public static void main ( String [ ] args ) { int N = 13 ; findNthNum ( N ) ; } }

def findNthNum ( N ) : NEW_LINE INDENT bit_L = 1 ; NEW_LINE last_num = 0 ; NEW_LINE while ( bit_L * ( bit_L + 1 ) / 2 < N ) : NEW_LINE INDENT last_num = last_num + bit_L ; NEW_LINE bit_L += 1 ; NEW_LINE DEDENT bit_R = N - last_num - 1 ; NEW_LINE print ( ( 1 << bit_L ) + ( 1 << bit_R ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 ; NEW_LINE findNthNum ( N ) ; NEW_LINE DEDENT


class GFG { static int solve ( int [ ] arr , int n ) { int k = arr . length ; int mp [ ] = new int [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = Math . max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = Math . max ( leftSegment , rightSegment ) ; int tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , 4 } ; System . out . println ( solve ( arr , N ) ) ; } }

def solve ( arr , n ) : NEW_LINE INDENT k = len ( arr ) NEW_LINE mp = [ False for i in range ( n + 2 ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT mp [ arr [ i ] ] = True NEW_LINE DEDENT leftSegment = arr [ 0 ] - 1 NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT leftSegment = max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) NEW_LINE DEDENT rightSegment = n - arr [ k - 1 ] NEW_LINE maxSegment = max ( leftSegment , rightSegment ) ; NEW_LINE tim = 0 NEW_LINE if ( maxSegment & 1 ) : NEW_LINE INDENT tim = ( maxSegment // 2 ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT tim = maxSegment // 2 NEW_LINE DEDENT return tim NEW_LINE DEDENT N = 5 NEW_LINE arr = [ 1 , 4 ] NEW_LINE print ( solve ( arr , N ) ) NEW_LINE


import java . io . * ; class GFG { static int findNthTerm ( int x , int y , int n ) { int [ ] f = new int [ 6 ] ; f [ 0 ] = x ; f [ 1 ] = y ; for ( int i = 2 ; i <= 5 ; i ++ ) f [ i ] = f [ i - 1 ] - f [ i - 2 ] ; return f [ n % 6 ] ; } public static void main ( String args [ ] ) { int x = 2 , y = 3 , n = 3 ; System . out . println ( findNthTerm ( x , y , n ) ) ; } }

def findNthTerm ( x , y , n ) : NEW_LINE INDENT f = [ 0 ] * 6 NEW_LINE f [ 0 ] = x NEW_LINE f [ 1 ] = y NEW_LINE for i in range ( 2 , 6 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] - f [ i - 2 ] NEW_LINE DEDENT return f [ n % 6 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , n = 2 , 3 , 3 NEW_LINE print ( findNthTerm ( x , y , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node next ; } static boolean isCircular ( Node head ) { if ( head == null ) return true ; Node node = head . next ; while ( node != null && node != head ) node = node . next ; return ( node == head ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; return temp ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; System . out . print ( isCircular ( head ) ? " Yes \n " : " No \n " ) ; head . next . next . next . next = head ; System . out . print ( isCircular ( head ) ? " Yes \n " : " No \n " ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT DEDENT def Circular ( head ) : NEW_LINE INDENT if head == None : NEW_LINE INDENT return True NEW_LINE DEDENT node = head . next NEW_LINE i = 0 NEW_LINE while ( ( node is not None ) and ( node is not head ) ) : NEW_LINE INDENT i = i + 1 NEW_LINE node = node . next NEW_LINE DEDENT return ( node == head ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT llist = LinkedList ( ) NEW_LINE llist . head = Node ( 1 ) NEW_LINE second = Node ( 2 ) NEW_LINE third = Node ( 3 ) NEW_LINE fourth = Node ( 4 ) NEW_LINE llist . head . next = second ; NEW_LINE second . next = third ; NEW_LINE third . next = fourth NEW_LINE if ( Circular ( llist . head ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT fourth . next = llist . head NEW_LINE if ( Circular ( llist . head ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int countDigit ( long n ) { return ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; } public static void main ( String [ ] args ) { long n = 345289467 ; System . out . print ( " Number ▁ of ▁ digits ▁ : ▁ " + countDigit ( n ) ) ; } }

import math NEW_LINE def countDigit ( n ) : NEW_LINE INDENT return math . floor ( math . log ( n , 10 ) + 1 ) NEW_LINE DEDENT n = 345289467 NEW_LINE print ( " Number ▁ of ▁ digits ▁ : ▁ % ▁ d " % ( countDigit ( n ) ) ) NEW_LINE


class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void findgcd ( int n , int x , int y ) { int g = gcd ( x , y ) ; for ( int i = 0 ; i < g ; i ++ ) System . out . print ( n ) ; } public static void main ( String [ ] args ) { int n = 123 , x = 5 , y = 2 ; findgcd ( n , x , y ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def findgcd ( n , x , y ) : NEW_LINE INDENT g = gcd ( x , y ) NEW_LINE for i in range ( g ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT n = 123 NEW_LINE x = 5 NEW_LINE y = 2 NEW_LINE findgcd ( n , x , y ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String args [ ] ) throws IOException { int n = 30 ; System . out . println ( sumofoddFactors ( n ) ) ; } }

import math NEW_LINE def sumofoddFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n = n // i NEW_LINE curr_term *= i NEW_LINE curr_sum += curr_term NEW_LINE DEDENT res *= curr_sum NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT res *= ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 30 NEW_LINE print ( sumofoddFactors ( n ) ) NEW_LINE


class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str . charAt ( i ) - '0' ) ; else evenDigSum += ( str . charAt ( i ) - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76945" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE oddDigSum = 0 NEW_LINE evenDigSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT DEDENT return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) NEW_LINE DEDENT st = "76945" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int numberOfMinutes ( int S , int S1 ) { int Min = 0 ; Min = ( int ) ( ( ( S - S1 ) / Math . floor ( S ) ) * 60 ) ; return Min ; } public static void main ( String [ ] args ) { int S = 30 , S1 = 10 ; System . out . println ( numberOfMinutes ( S , S1 ) + " ▁ min " ) ; } }

import math NEW_LINE def numberOfMinutes ( S , S1 ) : NEW_LINE INDENT Min = 0 ; NEW_LINE Min = ( ( S - S1 ) / math . floor ( S ) ) * 60 ; NEW_LINE return int ( Min ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , S1 = 30 , 10 ; NEW_LINE print ( numberOfMinutes ( S , S1 ) , " min " ) ; NEW_LINE DEDENT


class Test { static float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { System . out . println ( " Not ▁ a ▁ valid ▁ triangle " ) ; System . exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; } public static void main ( String [ ] args ) { float a = 3.0f ; float b = 4.0f ; float c = 5.0f ; System . out . println ( " Area ▁ is ▁ " + findArea ( a , b , c ) ) ; } }

def findArea ( a , b , c ) : NEW_LINE INDENT if ( a < 0 or b < 0 or c < 0 or ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) ) : NEW_LINE INDENT print ( ' Not ▁ a ▁ valid ▁ trianglen ' ) NEW_LINE return NEW_LINE DEDENT s = ( a + b + c ) / 2 NEW_LINE area = ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ** 0.5 NEW_LINE print ( ' Area ▁ of ▁ a ▁ traingle ▁ is ▁ % f ' % area ) NEW_LINE DEDENT a = 3.0 NEW_LINE b = 4.0 NEW_LINE c = 5.0 NEW_LINE findArea ( a , b , c ) NEW_LINE


class GFG { static int largestNumber ( int n ) { String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( ( ( int ) ( s . charAt ( i ) - '0' ) % 2 ) & 1 ) > 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( int ) ( s . charAt ( i ) - '0' ) ; num = num * 10 + ( ( int ) s . charAt ( index ) - ( int ) ( '0' ) - 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 8 ; return num ; } public static void main ( String [ ] args ) { int N = 24578 ; System . out . println ( largestNumber ( N ) ) ; } }

import math as mt NEW_LINE def largestNumber ( n ) : NEW_LINE INDENT s = " " NEW_LINE duplicate = n NEW_LINE while ( n > 0 ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n = n // 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( ord ( s [ i ] ) - ord ( '0' ) ) % 2 & 1 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) - 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 8 NEW_LINE DEDENT return num NEW_LINE DEDENT N = 24578 NEW_LINE print ( largestNumber ( N ) ) NEW_LINE


class GFG { static void generateSequence ( int n , int g ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i * g + " ▁ " ) ; ; } public static void main ( String [ ] args ) { int n = 6 , g = 5 ; generateSequence ( n , g ) ; } }

def generateSequence ( n , g ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( i * g , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , g = 6 , 5 NEW_LINE generateSequence ( n , g ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findCount ( String s , int n , int a , int b ) { int res = 0 ; char [ ] s1 = s . toCharArray ( ) ; for ( int i = 0 ; i < b ; i ++ ) { if ( i == a ) { if ( s1 [ n - i - 1 ] != '1' ) res += 1 ; } else { if ( s1 [ n - i - 1 ] != '0' ) res += 1 ; } } return res ; } static public void main ( String [ ] args ) { String str = "1001011001" ; int N = str . length ( ) ; int A = 3 , B = 6 ; System . out . println ( findCount ( str , N , A , B ) ) ; } }

def findCount ( s , n , a , b ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( b ) : NEW_LINE INDENT if ( i == a ) : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '1' ) NEW_LINE DEDENT else : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '0' ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "1001011001" NEW_LINE N = len ( str ) NEW_LINE A = 3 NEW_LINE B = 6 NEW_LINE print ( findCount ( str , N , A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } public static void main ( String [ ] args ) { int n = 4513 ; System . out . println ( getProduct ( n ) ) ; } }

def getProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = n // 10 NEW_LINE DEDENT return product NEW_LINE DEDENT n = 4513 NEW_LINE print ( getProduct ( n ) ) NEW_LINE


import java . util . * ; class GFG { static void sieve ( boolean [ ] primes , int x ) { primes [ 1 ] = true ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( primes [ i ] == false ) { for ( int j = 2 ; j * i <= x ; j ++ ) primes [ i * j ] = true ; } } } static int nDivisors ( boolean [ ] primes , int x , int a , int b , int n ) { int result = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i <= x ; i ++ ) if ( primes [ i ] == false ) v . add ( i ) ; for ( int i = a ; i <= b ; i ++ ) { int temp = i ; int total = 1 ; int j = 0 ; for ( int k = v . get ( j ) ; k * k <= temp ; k = v . get ( ++ j ) ) { int count = 0 ; while ( temp % k == 0 ) { count ++ ; temp = temp / k ; } total = total * ( count + 1 ) ; } if ( temp != 1 ) total = total * 2 ; if ( total == n ) result ++ ; } return result ; } static int countNDivisors ( int a , int b , int n ) { int x = ( int ) Math . sqrt ( b ) + 1 ; boolean [ ] primes = new boolean [ x + 1 ] ; sieve ( primes , x ) ; return nDivisors ( primes , x , a , b , n ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 7 , n = 2 ; System . out . println ( countNDivisors ( a , b , n ) ) ; } }

import math ; NEW_LINE def sieve ( primes , x ) : NEW_LINE INDENT primes [ 1 ] = False ; NEW_LINE i = 2 ; NEW_LINE while ( i * i <= x ) : NEW_LINE INDENT if ( primes [ i ] == True ) : NEW_LINE INDENT j = 2 ; NEW_LINE while ( j * i <= x ) : NEW_LINE INDENT primes [ i * j ] = False ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT def nDivisors ( primes , x , a , b , n ) : NEW_LINE INDENT result = 0 ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( 2 , x + 1 ) : NEW_LINE INDENT if ( primes [ i ] ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE DEDENT DEDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT temp = i ; NEW_LINE total = 1 ; NEW_LINE j = 0 ; NEW_LINE k = v [ j ] ; NEW_LINE while ( k * k <= temp ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( temp % k == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE temp = int ( temp / k ) ; NEW_LINE DEDENT total = total * ( count + 1 ) ; NEW_LINE j += 1 ; NEW_LINE k = v [ j ] ; NEW_LINE DEDENT if ( temp != 1 ) : NEW_LINE INDENT total = total * 2 ; NEW_LINE DEDENT if ( total == n ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT def countNDivisors ( a , b , n ) : NEW_LINE INDENT x = int ( math . sqrt ( b ) + 1 ) ; NEW_LINE primes = [ True ] * ( x + 1 ) ; NEW_LINE sieve ( primes , x ) ; NEW_LINE return nDivisors ( primes , x , a , b , n ) ; NEW_LINE DEDENT a = 1 ; NEW_LINE b = 7 ; NEW_LINE n = 2 ; NEW_LINE print ( countNDivisors ( a , b , n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int countEvenSum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; System . out . print ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " ) ; System . out . println ( countEvenSum ( arr , n ) ) ; } }

def countEvenSum ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , n , 1 ) : NEW_LINE INDENT sum = sum + arr [ j ] NEW_LINE if ( sum % 2 == 0 ) : NEW_LINE INDENT result = result + 1 NEW_LINE DEDENT DEDENT DEDENT return ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays " , " with ▁ even ▁ sum ▁ is " , countEvenSum ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void pattern ( int n ) { int i , j ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= ( 2 * n ) ; j ++ ) { if ( i > ( n - j + 1 ) ) System . out . print ( " ▁ " ) ; else System . out . print ( " * " ) ; if ( ( i + n ) > j ) System . out . print ( " ▁ " ) ; else System . out . print ( " * " ) ; } System . out . println ( " " ) ; } for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= ( 2 * n ) ; j ++ ) { if ( i < j ) System . out . print ( " ▁ " ) ; else System . out . print ( " * " ) ; if ( i <= ( ( 2 * n ) - j ) ) System . out . print ( " ▁ " ) ; else System . out . print ( " * " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { pattern ( 7 ) ; } }

def pattern ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n ) : NEW_LINE INDENT if i > ( n - j + 1 ) : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT if i + n - 1 > j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n ) : NEW_LINE INDENT if i < j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT if i < 2 * n - j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT pattern ( 7 ) ; NEW_LINE


class GFG { static int sum ( int arr [ ] , int l , int r ) { int s = 0 ; for ( int i = l ; i <= r ; i ++ ) { s += arr [ i ] ; } return s ; } static int dp [ ] [ ] [ ] = new int [ 101 ] [ 101 ] [ 101 ] ; static int solve ( int arr [ ] , int l , int r , int k ) { if ( r - l + 1 <= k ) return sum ( arr , l , r ) ; if ( dp [ l ] [ r ] [ k ] != 0 ) return dp [ l ] [ r ] [ k ] ; int sum_ = sum ( arr , l , r ) ; int len_r = ( r - l + 1 ) - k ; int len = ( r - l + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len - len_r + 1 ; i ++ ) { int sum_sub = sum ( arr , i + l , i + l + len_r - 1 ) ; ans = Math . max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; } dp [ l ] [ r ] [ k ] = ans ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 15 , 20 , 9 , 2 , 5 } , k = 2 ; int n = arr . length ; System . out . println ( solve ( arr , 0 , n - 1 , k ) ) ; } }

import numpy as np NEW_LINE def Sum ( arr , l , r ) : NEW_LINE INDENT s = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT s += arr [ i ] ; NEW_LINE DEDENT return s ; NEW_LINE DEDENT dp = np . zeros ( ( 101 , 101 , 101 ) ) ; NEW_LINE def solve ( arr , l , r , k ) : NEW_LINE INDENT if ( r - l + 1 <= k ) : NEW_LINE INDENT return Sum ( arr , l , r ) ; NEW_LINE DEDENT if ( dp [ l ] [ r ] [ k ] ) : NEW_LINE INDENT return dp [ l ] [ r ] [ k ] ; NEW_LINE DEDENT sum_ = Sum ( arr , l , r ) ; NEW_LINE len_r = ( r - l + 1 ) - k ; NEW_LINE length = ( r - l + 1 ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( length - len_r + 1 ) : NEW_LINE INDENT sum_sub = Sum ( arr , i + l , i + l + len_r - 1 ) ; NEW_LINE ans = max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; NEW_LINE DEDENT dp [ l ] [ r ] [ k ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 15 , 20 , 9 , 2 , 5 ] ; k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( solve ( arr , 0 , n - 1 , k ) ) ; NEW_LINE DEDENT


class GFG { static double nthRoot ( int A , int N ) { double xPre = Math . random ( ) % 10 ; double eps = 0.001 ; double delX = 2147483647 ; double xK = 0.0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } public static void main ( String [ ] args ) { int N = 4 ; int A = 81 ; double nthRootValue = nthRoot ( A , N ) ; System . out . println ( " Nth ▁ root ▁ is ▁ " + Math . round ( nthRootValue * 1000.0 ) / 1000.0 ) ; } }

import math NEW_LINE import random NEW_LINE def nthRoot ( A , N ) : NEW_LINE INDENT xPre = random . randint ( 1 , 101 ) % 10 NEW_LINE eps = 0.001 NEW_LINE delX = 2147483647 NEW_LINE xK = 0.0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK ; NEW_LINE DEDENT return xK NEW_LINE DEDENT N = 4 NEW_LINE A = 81 NEW_LINE nthRootValue = nthRoot ( A , N ) NEW_LINE print ( " Nth ▁ root ▁ is ▁ " , nthRootValue ) NEW_LINE


import java . util . * ; class GFG { static class pair { int first , second ; pair ( ) { first = 0 ; second = 0 ; } } static void printIndices ( int n , pair a [ ] ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] . second + " ▁ " ) ; System . out . println ( ) ; } static class sort implements Comparator < pair > { public int compare ( pair a , pair b ) { return a . first < b . first ? - 1 : 1 ; } } static void printPermutations ( int n , int a [ ] , int k ) { pair arr [ ] = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Arrays . sort ( arr , new sort ( ) ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { pair t = arr [ j ] ; arr [ j ] = arr [ j - 1 ] ; arr [ j - 1 ] = t ; break ; } } } printIndices ( n , arr ) ; } public static void main ( String arsg [ ] ) { int a [ ] = { 1 , 3 , 3 , 1 } ; int n = a . length ; int k = 3 ; printPermutations ( n , a , k ) ; } }

def printIndices ( n , a ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT def printPermutations ( n , a , k ) : NEW_LINE INDENT arr = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] [ 0 ] = a [ i ] NEW_LINE arr [ i ] [ 1 ] = i NEW_LINE DEDENT arr . sort ( reverse = False ) NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count < k ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT for i in range ( k - 1 ) : NEW_LINE INDENT printIndices ( n , arr ) NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ j - 1 ] NEW_LINE arr [ j - 1 ] = temp NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT printIndices ( n , arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 3 , 3 , 1 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE printPermutations ( n , a , k ) NEW_LINE DEDENT


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

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


import java . util . * ; class GFG { static int M = 4 ; static int N = 5 ; static void printCommonElements ( int mat [ ] [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int j = 0 ; j < N ; j ++ ) mp . put ( mat [ 0 ] [ j ] , 1 ) ; for ( int i = 1 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mp . get ( mat [ i ] [ j ] ) != null && mp . get ( mat [ i ] [ j ] ) == i ) { mp . put ( mat [ i ] [ j ] , i + 1 ) ; if ( i == M - 1 ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } } } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 1 , 4 , 8 } , { 3 , 7 , 8 , 5 , 1 } , { 8 , 7 , 7 , 3 , 1 } , { 8 , 1 , 2 , 7 , 9 } , } ; printCommonElements ( mat ) ; } }

M = 4 NEW_LINE N = 5 NEW_LINE def printCommonElements ( mat ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for j in range ( N ) : NEW_LINE INDENT mp [ mat [ 0 ] [ j ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( mat [ i ] [ j ] in mp . keys ( ) and mp [ mat [ i ] [ j ] ] == i ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = i + 1 NEW_LINE if i == M - 1 : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT mat = [ [ 1 , 2 , 1 , 4 , 8 ] , [ 3 , 7 , 8 , 5 , 1 ] , [ 8 , 7 , 7 , 3 , 1 ] , [ 8 , 1 , 2 , 7 , 9 ] ] NEW_LINE printCommonElements ( mat ) NEW_LINE


class GFG { static void longestString ( String str1 , String str2 ) { int count1 [ ] = new int [ 26 ] , count2 [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; } String result = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 1 ; j <= Math . min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) { result += ( char ) ( ' a ' + i ) ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { String str1 = " geeks " , str2 = " cake " ; longestString ( str1 , str2 ) ; } }

def longestString ( str1 , str2 ) : NEW_LINE INDENT count1 = [ 0 ] * 26 NEW_LINE count2 = [ 0 ] * 26 NEW_LINE for i in range ( len ( str1 ) ) : NEW_LINE INDENT count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( str2 ) ) : NEW_LINE INDENT count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT result = " " NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT for j in range ( 1 , min ( count1 [ i ] , count2 [ i ] ) + 1 ) : NEW_LINE INDENT result = result + chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " geeks " NEW_LINE str2 = " cake " NEW_LINE longestString ( str1 , str2 ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 2005 ; static Set < Integer > ans = new LinkedHashSet < Integer > ( ) ; static void dfs ( int num , int left , int ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . add ( num ) ; for ( int i = 0 ; i <= Math . min ( left , 9 ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } static int getKthNum ( int m , int k ) { dfs ( 0 , m , 0 ) ; int ct = 0 ; for ( int it : ans ) { ct ++ ; if ( ct == k ) { return it ; } } return - 1 ; } public static void main ( String [ ] args ) { int m = 5 , k = 3 ; System . out . println ( getKthNum ( m , k ) ) ; } }

N = 2005 NEW_LINE ans = dict ( ) NEW_LINE def dfs ( n , left , ct ) : NEW_LINE INDENT if ( ct >= 15 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( left == 0 ) : NEW_LINE INDENT ans [ n ] = 1 NEW_LINE DEDENT for i in range ( min ( left , 9 ) + 1 ) : NEW_LINE INDENT dfs ( n * 10 + i , left - i , ct + 1 ) NEW_LINE DEDENT DEDENT def getKthNum ( m , k ) : NEW_LINE INDENT dfs ( 0 , m , 0 ) NEW_LINE c = 0 NEW_LINE for it in sorted ( ans . keys ( ) ) : NEW_LINE INDENT c += 1 NEW_LINE if ( c == k ) : NEW_LINE INDENT return it NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT m = 5 NEW_LINE k = 3 NEW_LINE print ( getKthNum ( m , k ) ) NEW_LINE


public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 || head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; System . out . print ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) System . out . println ( answer . data ) ; else System . out . println ( " null " ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT new_node = Node ( data ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT def modularNode ( head , k ) : NEW_LINE INDENT if ( k <= 0 or head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT i = 1 NEW_LINE modularNode = None NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT modularNode = temp NEW_LINE DEDENT i = i + 1 NEW_LINE temp = temp . next NEW_LINE DEDENT return modularNode NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE k = 2 NEW_LINE answer = modularNode ( head , k ) NEW_LINE print ( " Modular ▁ node ▁ is " , end = ' ▁ ' ) NEW_LINE if ( answer != None ) : NEW_LINE INDENT print ( answer . data , end = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " ) NEW_LINE DEDENT DEDENT


class GFG { static int findMax ( int arr [ ] , int n ) { boolean [ ] divisible = new boolean [ n ] ; int res = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }

def findMax ( arr , n ) : NEW_LINE INDENT divisible = [ False ] * n ; NEW_LINE res = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( divisible [ i ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ j ] % arr [ i ] ) == 0 ) : NEW_LINE INDENT divisible [ j ] = True ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT res = max ( res , cnt ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findMax ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static int MAX = 10 ; static String getMaxEven ( char [ ] str , int len ) { int [ ] freq = new int [ MAX ] ; int i , minEvenDigit = MAX , minDigit = MAX ; for ( i = 0 ; i < len ; i ++ ) { int digit = str [ i ] - '0' ; freq [ digit ] ++ ; if ( digit % 2 == 0 ) minEvenDigit = Math . min ( digit , minEvenDigit ) ; minDigit = Math . min ( digit , minDigit ) ; } if ( minEvenDigit == MAX ) return " - 1" ; freq [ minEvenDigit ] -- ; freq [ minDigit ] -- ; i = 0 ; for ( int j = MAX - 1 ; j >= 0 ; j -- ) { for ( int k = 0 ; k < freq [ j ] ; k ++ ) str [ i ++ ] = ( char ) ( j + '0' ) ; if ( j == minEvenDigit ) str [ i ++ ] = ( char ) ( minDigit + '0' ) ; } str [ i - 1 ] = ( char ) ( minEvenDigit + '0' ) ; return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { char [ ] str = "1023422" . toCharArray ( ) ; int len = str . length ; System . out . println ( getMaxEven ( str , len ) ) ; } }

MAX = 10 NEW_LINE def getMaxEven ( string , length ) : NEW_LINE INDENT string = list ( string ) NEW_LINE freq = [ 0 ] * MAX NEW_LINE minEvenDigit = MAX ; NEW_LINE minDigit = MAX ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT digit = ord ( string [ i ] ) - ord ( '0' ) ; NEW_LINE freq [ digit ] += 1 ; NEW_LINE if ( digit % 2 == 0 ) : NEW_LINE INDENT minEvenDigit = min ( digit , minEvenDigit ) ; NEW_LINE DEDENT minDigit = min ( digit , minDigit ) ; NEW_LINE DEDENT if ( minEvenDigit == MAX ) : NEW_LINE INDENT return " - 1" ; NEW_LINE DEDENT freq [ minEvenDigit ] -= 1 ; NEW_LINE freq [ minDigit ] -= 1 ; NEW_LINE i = 0 ; NEW_LINE for j in range ( MAX - 1 , - 1 , - 1 ) : NEW_LINE INDENT for k in range ( freq [ j ] ) : NEW_LINE INDENT string [ i ] = chr ( j + ord ( '0' ) ) ; NEW_LINE i += 1 NEW_LINE DEDENT if ( j == minEvenDigit ) : NEW_LINE INDENT string [ i ] = chr ( minDigit + ord ( '0' ) ) ; NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return " " . join ( string ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "1023422" ; NEW_LINE length = len ( string ) ; NEW_LINE print ( getMaxEven ( string , length ) ) ; NEW_LINE DEDENT


class GFG { static char maxRepeating ( String str ) { int n = str . length ( ) ; int count = 0 ; char res = str . charAt ( 0 ) ; int cur_count = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res = str . charAt ( i ) ; } cur_count = 1 ; } } return res ; } public static void main ( String args [ ] ) { String str = " aaaabbaaccde " ; System . out . println ( maxRepeating ( str ) ) ; } }

def maxRepeating ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE count = 0 NEW_LINE res = str [ 0 ] NEW_LINE cur_count = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n - 1 and str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT cur_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if cur_count > count : NEW_LINE INDENT count = cur_count NEW_LINE res = str [ i ] NEW_LINE DEDENT cur_count = 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " aaaabbaaccde " NEW_LINE print ( maxRepeating ( str ) ) NEW_LINE DEDENT


class GFG { static boolean isSquare ( int num ) { int root = ( int ) Math . sqrt ( num ) ; return ( root * root ) == num ; } static boolean isCube ( int num ) { int root = ( int ) Math . cbrt ( num ) ; return ( root * root * root ) == num ; } static int countSC ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( isSquare ( i ) ) count ++ ; else if ( isCube ( i ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( " Number ▁ of ▁ squares ▁ " + " and ▁ cubes ▁ is ▁ " + countSC ( N ) ) ; } }

def isSquare ( num ) : NEW_LINE INDENT root = int ( num ** ( 1 / 2 ) ) NEW_LINE return ( root * root ) == num NEW_LINE DEDENT def isCube ( num ) : NEW_LINE INDENT root = int ( num ** ( 1 / 3 ) ) NEW_LINE return ( root * root * root ) == num NEW_LINE DEDENT def countSC ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if isSquare ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT elif isCube ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 20 NEW_LINE print ( " Number ▁ of ▁ squares ▁ and ▁ cubes ▁ is ▁ " , countSC ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void intersection ( int a [ ] , int b [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { System . out . print ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 } ; int b [ ] = { 3 , 3 , 5 } ; int n = a . length ; int m = b . length ; intersection ( a , b , n , m ) ; } }

def intersection ( a , b , n , m ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( a [ i ] > b [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( b [ j ] > a [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 ] NEW_LINE b = [ 3 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE intersection ( a , b , n , m ) NEW_LINE DEDENT


class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findReachable ( int [ ] arr , int D , int A , int B , int n ) { int gcd_AB = GCD ( A , B ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; int D = 4 , A = 4 , B = 6 ; System . out . println ( findReachable ( arr , D , A , B , n ) ) ; } }

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 def findReachable ( arr , D , A , B , n ) : NEW_LINE INDENT gcd_AB = GCD ( A , B ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - D ) % gcd_AB == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ 4 , 5 , 6 , 7 , 8 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE D = 4 ; A = 4 ; B = 6 ; NEW_LINE print ( findReachable ( arr , D , A , B , n ) ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static boolean isFullBinaryTree ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node . left == null && node . right == null ) continue ; if ( node . left == null || node . right == null ) return false ; q . add ( node . left ) ; q . add ( node . right ) ; } return true ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; if ( isFullBinaryTree ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

class getNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isFullBinaryTree ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( not len ( q ) ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left == None and node . right == None ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( node . left == None or node . right == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( node . left ) NEW_LINE q . append ( node . right ) NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( 1 ) NEW_LINE root . left = getNode ( 2 ) NEW_LINE root . right = getNode ( 3 ) NEW_LINE root . left . left = getNode ( 4 ) NEW_LINE root . left . right = getNode ( 5 ) NEW_LINE if ( isFullBinaryTree ( root ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) { if ( 0 == n ) return ; tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ; System . out . printf ( " Move ▁ the ▁ disk ▁ % d ▁ from ▁ % c ▁ to ▁ % c \n " , n , sourcePole , destinationPole ) ; tower ( n - 1 , auxiliaryPole , destinationPole , sourcePole ) ; } public static void main ( String [ ] args ) { tower ( 3 , ' S ' , ' D ' , ' A ' ) ; } }

def tower ( n , sourcePole , destinationPole , auxiliaryPole ) : NEW_LINE INDENT if ( 0 == n ) : NEW_LINE INDENT return NEW_LINE DEDENT tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) NEW_LINE print ( " Move ▁ the ▁ disk " , sourcePole , " from " , sourcePole , " to " , destinationPole ) NEW_LINE tower ( n - 1 , auxiliaryPole , destinationPole , sourcePole ) NEW_LINE DEDENT tower ( 3 , ' S ' , ' D ' , ' A ' ) NEW_LINE


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

import math NEW_LINE def fourthPowerSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum = sum + ( i * i * i * i ) NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 6 NEW_LINE print ( fourthPowerSum ( n ) ) NEW_LINE


import java . io . * ; import java . lang . * ; class GFG { public static int nthTerm ( int N ) { return Math . abs ( N * ( ( N - 1 ) * ( N - 3 ) * ( N - 5 ) ) ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( N ) : NEW_LINE INDENT return ( abs ( N * ( ( N - 1 ) * ( N - 3 ) * ( N - 5 ) ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void getAverageCountArray ( int [ ] a , int x , int N ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; int [ ] avg = new int [ N ] ; int val , av ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! map . containsKey ( a [ i ] ) ) { map . put ( a [ i ] , 1 ) ; } else { val = map . get ( a [ i ] ) ; val ++ ; map . remove ( a [ i ] ) ; map . put ( a [ i ] , val ) ; } } for ( int i = 0 ; i < N ; i ++ ) { av = ( a [ i ] + x ) / 2 ; if ( map . containsKey ( av ) ) { val = map . get ( av ) ; avg [ i ] = val ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( avg [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 2 , 0 , 4 , 6 , 2 } ; int x = 2 ; int N = a . length ; getAverageCountArray ( a , x , N ) ; } }

def getAverageCountArray ( a , x , N ) : NEW_LINE INDENT map = { } NEW_LINE avg = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if a [ i ] not in map : NEW_LINE INDENT map [ a [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT map [ a [ i ] ] += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT av = ( a [ i ] + x ) // 2 NEW_LINE if av in map : NEW_LINE INDENT val = map [ av ] NEW_LINE avg [ i ] = val NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( avg [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 0 , 4 , 6 , 2 ] NEW_LINE x = 2 NEW_LINE N = len ( a ) NEW_LINE getAverageCountArray ( a , x , N ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a | b ) & 1 ) == 0 ; ++ k ) { a >>= 1 ; b >>= 1 ; } while ( ( a & 1 ) == 0 ) a >>= 1 ; do { while ( ( b & 1 ) == 0 ) b >>= 1 ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } b = ( b - a ) ; } while ( b != 0 ) ; return a << k ; } public static void main ( String args [ ] ) { int a = 34 , b = 17 ; System . out . println ( " Gcd ▁ of ▁ given ▁ " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT k = 0 NEW_LINE while ( ( ( a | b ) & 1 ) == 0 ) : NEW_LINE INDENT a = a >> 1 NEW_LINE b = b >> 1 NEW_LINE k = k + 1 NEW_LINE DEDENT while ( ( a & 1 ) == 0 ) : NEW_LINE INDENT a = a >> 1 NEW_LINE DEDENT while ( b != 0 ) : NEW_LINE INDENT while ( ( b & 1 ) == 0 ) : NEW_LINE INDENT b = b >> 1 NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT temp = a NEW_LINE a = b NEW_LINE b = temp NEW_LINE DEDENT b = ( b - a ) NEW_LINE DEDENT return ( a << k ) NEW_LINE DEDENT a = 34 NEW_LINE b = 17 NEW_LINE print ( " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ " , gcd ( a , b ) ) NEW_LINE


import java . io . * ; class GFG { static int summation ( int n ) { int sum ; sum = ( int ) ( Math . pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( summation ( n ) ) ; } }

import math NEW_LINE def summation ( n ) : NEW_LINE INDENT return int ( ( pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ) ; NEW_LINE DEDENT print ( summation ( 5 ) ) ; NEW_LINE


import java . io . * ; import java . util . Arrays ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Arrays . sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { int mostDistantDistance = arr [ k ] - arr [ i ] ; if ( mostDistantDistance <= L ) { ways ++ ; } } } } return ways ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int L = 3 ; int ans = countTripletsLessThanL ( n , L , arr ) ; System . out . println ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }

def countTripletsLessThanL ( n , L , arr ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ways = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT mostDistantDistance = arr [ k ] - arr [ i ] NEW_LINE if ( mostDistantDistance <= L ) : NEW_LINE INDENT ways += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ways NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE L = 3 NEW_LINE ans = countTripletsLessThanL ( n , L , arr ) NEW_LINE print ( " Total ▁ Number ▁ of ▁ ways ▁ = " , ans ) NEW_LINE DEDENT


class GFG { static void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " - 1" ) ; return ; } int i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } System . out . println ( " n ▁ = ▁ " + first + " ▁ and ▁ r ▁ = ▁ " + second ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; findPair ( arr , n ) ; } }

def findPair ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT i = 0 NEW_LINE first = - 1 NEW_LINE second = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT print ( " n ▁ = " , first , " and ▁ r ▁ = " , second ) NEW_LINE DEDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE


import java . math . * ; class GFG { public static void checkType ( int arr [ ] , int n ) { if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] <= arr [ n - 1 ] ) System . out . println ( " Increasing " ) ; else if ( arr [ 0 ] >= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Decreasing " ) ; else if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Increasing ▁ then ▁ decreasing " ) ; else System . out . println ( " Decreasing ▁ then ▁ increasing " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ; int n = arr . length ; checkType ( arr , n ) ; } }

def checkType ( arr , n ) : NEW_LINE INDENT if ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] <= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] >= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Decreasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing ▁ then ▁ decreasing " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Decreasing ▁ then ▁ increasing " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE checkType ( arr , n ) ; NEW_LINE DEDENT


class GFG { static int countsubarray ( int array [ ] , int n , int k ) { int count = 0 ; int i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] <= k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul <= k ) count ++ ; else break ; } } return count ; } public static void main ( String args [ ] ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int k = 10 ; int size = array . length ; int count = countsubarray ( array , size , k ) ; System . out . print ( count ) ; } }

def countsubarray ( array , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if array [ i ] <= k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT mul = array [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT mul = mul * array [ j ] NEW_LINE if mul <= k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT array = [ 1 , 2 , 3 , 4 ] NEW_LINE k = 10 NEW_LINE size = len ( array ) NEW_LINE count = countsubarray ( array , size , k ) ; NEW_LINE print ( count , end = " ▁ " ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static int MaxSumDifference ( Integer [ ] a , int n ) { List < Integer > finalSequence = new ArrayList < Integer > ( ) ; Arrays . sort ( a ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { finalSequence . add ( a [ i ] ) ; finalSequence . add ( a [ n - i - 1 ] ) ; } if ( n % 2 != 0 ) finalSequence . add ( a [ n / 2 ] ) ; int MaximumSum = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { MaximumSum = MaximumSum + Math . abs ( finalSequence . get ( i ) - finalSequence . get ( i + 1 ) ) ; } MaximumSum = MaximumSum + Math . abs ( finalSequence . get ( n - 1 ) - finalSequence . get ( 0 ) ) ; return MaximumSum ; } public static void main ( String args [ ] ) { Integer [ ] a = { 1 , 2 , 4 , 8 } ; int n = a . length ; System . out . print ( MaxSumDifference ( a , n ) ) ; } }

import numpy as np NEW_LINE class GFG : NEW_LINE INDENT def MaxSumDifference ( a , n ) : NEW_LINE INDENT np . sort ( a ) ; NEW_LINE j = 0 NEW_LINE finalSequence = [ 0 for x in range ( n ) ] NEW_LINE for i in range ( 0 , int ( n / 2 ) ) : NEW_LINE INDENT finalSequence [ j ] = a [ i ] NEW_LINE finalSequence [ j + 1 ] = a [ n - i - 1 ] NEW_LINE j = j + 2 NEW_LINE DEDENT if ( n % 2 != 0 ) : NEW_LINE INDENT finalSequence [ n - 1 ] = a [ n / 2 ] NEW_LINE DEDENT MaximumSum = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT MaximumSum = ( MaximumSum + abs ( finalSequence [ i ] - finalSequence [ i + 1 ] ) ) NEW_LINE DEDENT MaximumSum = ( MaximumSum + abs ( finalSequence [ n - 1 ] - finalSequence [ 0 ] ) ) ; NEW_LINE print ( MaximumSum ) NEW_LINE DEDENT DEDENT a = [ 1 , 2 , 4 , 8 ] NEW_LINE n = len ( a ) NEW_LINE GFG . MaxSumDifference ( a , n ) ; NEW_LINE


import java . util . Arrays ; class GFG { static final int maximum = 1000 ; static int lcs ( String X , String Y , int m , int n , int dp [ ] [ ] ) { if ( m == 0 || n == 0 ) { return 0 ; } if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) { return dp [ m - 1 ] [ n - 1 ] ; } if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) { dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) ; return dp [ m - 1 ] [ n - 1 ] ; } else { dp [ m - 1 ] [ n - 1 ] = Math . max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) ; return dp [ m - 1 ] [ n - 1 ] ; } } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . length ( ) ; int n = Y . length ( ) ; int dp [ ] [ ] = new int [ m ] [ maximum ] ; for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } System . out . println ( " Length ▁ of ▁ LCS : ▁ " + lcs ( X , Y , m , n , dp ) ) ; } }

maximum = 1000 NEW_LINE def lcs ( X , Y , m , n , dp ) : NEW_LINE INDENT if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) : NEW_LINE INDENT return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] ) : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT DEDENT X = " AGGTAB " NEW_LINE Y = " GXTXAYB " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE dp = [ [ - 1 for i in range ( maximum ) ] for i in range ( m ) ] NEW_LINE print ( " Length ▁ of ▁ LCS : " , lcs ( X , Y , m , n , dp ) ) NEW_LINE


import java . util . HashMap ; class GFG { static String smallestFreq ( String S1 , String S2 ) { HashMap < String , Integer > banned = new HashMap < > ( ) ; for ( int i = 0 ; i < S2 . length ( ) ; i ++ ) { String s = " " ; while ( i < S2 . length ( ) && S2 . charAt ( i ) != ' ▁ ' ) s += S2 . charAt ( i ++ ) ; banned . put ( s , banned . get ( s ) == null ? 1 : banned . get ( s ) + 1 ) ; } HashMap < String , Integer > result = new HashMap < > ( ) ; String ans = " " ; int freq = 0 ; for ( int i = 0 ; i < S1 . length ( ) ; i ++ ) { String s = " " ; while ( i < S1 . length ( ) && S1 . charAt ( i ) != ' ▁ ' ) s += S1 . charAt ( i ++ ) ; if ( banned . get ( s ) == null ) { result . put ( s , result . get ( s ) == null ? 1 : result . get ( s ) + 1 ) ; if ( result . get ( s ) > freq || ( result . get ( s ) == freq && s . compareTo ( ans ) < 0 ) ) { ans = s ; freq = result . get ( s ) ; } } } return ans ; } public static void main ( String [ ] args ) { String S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " ; String S2 = " bad ▁ place " ; System . out . println ( smallestFreq ( S1 , S2 ) ) ; } }

from collections import defaultdict NEW_LINE def smallestFreq ( S1 , S2 ) : NEW_LINE INDENT banned = defaultdict ( lambda : 0 ) NEW_LINE i = 0 NEW_LINE while i < len ( S2 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S2 ) and S2 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S2 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE banned [ s ] += 1 NEW_LINE DEDENT result = defaultdict ( lambda : 0 ) NEW_LINE ans = " " NEW_LINE freq = 0 NEW_LINE i = 0 NEW_LINE while i < len ( S1 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S1 ) and S1 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE if banned [ s ] == 0 : NEW_LINE INDENT result [ s ] += 1 NEW_LINE if ( result [ s ] > freq or ( result [ s ] == freq and s < ans ) ) : NEW_LINE INDENT ans = s NEW_LINE freq = result [ s ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " NEW_LINE S2 = " bad ▁ place " NEW_LINE print ( smallestFreq ( S1 , S2 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int doublefactorial ( int n ) { int res = 1 ; for ( int i = n ; i >= 0 ; i = i - 2 ) { if ( i == 0 || i == 1 ) return res ; else res *= i ; } return res ; } public static void main ( String [ ] args ) { System . out . println ( " Double ▁ factorial " + " ▁ is ▁ " + doublefactorial ( 5 ) ) ; } }

def doublefactorial ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( n , - 1 , - 2 ) : NEW_LINE INDENT if ( i == 0 or i == 1 ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT else : NEW_LINE INDENT res *= i ; NEW_LINE DEDENT DEDENT DEDENT print ( " Double ▁ factorial ▁ is " , doublefactorial ( 5 ) ) ; NEW_LINE


class GFG { static String X , Y ; static int lcs ( int i , int j , int count ) { if ( i == 0 || j == 0 ) { return count ; } if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { count = lcs ( i - 1 , j - 1 , count + 1 ) ; } count = Math . max ( count , Math . max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) ; return count ; } public static void main ( String [ ] args ) { int n , m ; X = " abcdxyz " ; Y = " xyzabcd " ; n = X . length ( ) ; m = Y . length ( ) ; System . out . println ( lcs ( n , m , 0 ) ) ; } }

def lcs ( i , j , count ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT count = lcs ( i - 1 , j - 1 , count + 1 ) NEW_LINE DEDENT count = max ( count , max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " abcdxyz " NEW_LINE Y = " xyzabcd " NEW_LINE n = len ( X ) NEW_LINE m = len ( Y ) NEW_LINE print ( lcs ( n , m , 0 ) ) NEW_LINE DEDENT


class GFG { static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } public static void main ( String args [ ] ) { if ( isPower ( 27 , 729 ) ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }

import math NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = math . log ( y ) // math . log ( x ) ; NEW_LINE res2 = math . log ( y ) / math . log ( x ) ; NEW_LINE return 1 if ( res1 == res2 ) else 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( isPower ( 27 , 729 ) ) ; NEW_LINE DEDENT


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

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


class GFG { static String minWord = " " , maxWord = " " ; static void minMaxLengthWords ( String input ) { int len = input . length ( ) ; int si = 0 , ei = 0 ; int min_length = len , min_start_index = 0 , max_length = 0 , max_start_index = 0 ; while ( ei <= len ) { if ( ei < len && input . charAt ( ei ) != ' ▁ ' ) { ei ++ ; } else { int curr_length = ei - si ; if ( curr_length < min_length ) { min_length = curr_length ; min_start_index = si ; } if ( curr_length > max_length ) { max_length = curr_length ; max_start_index = si ; } ei ++ ; si = ei ; } } minWord = input . substring ( min_start_index , min_start_index + min_length ) ; maxWord = input . substring ( max_start_index , max_length ) ; } public static void main ( String [ ] args ) { String a = " GeeksforGeeks ▁ A ▁ Computer ▁ Science ▁ portal ▁ for ▁ Geeks " ; minMaxLengthWords ( a ) ; System . out . print ( " Minimum ▁ length ▁ word : ▁ " + minWord + " \n Maximum ▁ length ▁ word : ▁ " + maxWord ) ; } }

def minMaxLengthWords ( inp ) : NEW_LINE INDENT length = len ( inp ) NEW_LINE si = ei = 0 NEW_LINE min_length = length NEW_LINE min_start_index = max_length = max_start_index = 0 NEW_LINE while ei <= length : NEW_LINE INDENT if ( ei < length ) and ( inp [ ei ] != " ▁ " ) : NEW_LINE INDENT ei += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr_length = ei - si NEW_LINE if curr_length < min_length : NEW_LINE INDENT min_length = curr_length NEW_LINE min_start_index = si NEW_LINE DEDENT if curr_length > max_length : NEW_LINE INDENT max_length = curr_length NEW_LINE max_start_index = si NEW_LINE DEDENT ei += 1 NEW_LINE si = ei NEW_LINE DEDENT DEDENT minWord = inp [ min_start_index : min_start_index + min_length ] NEW_LINE maxWord = inp [ max_start_index : max_length ] NEW_LINE print ( " Minimum ▁ length ▁ word : ▁ " , minWord ) NEW_LINE print ( " Maximum ▁ length ▁ word : ▁ " , maxWord ) NEW_LINE DEDENT a = " GeeksforGeeks ▁ A ▁ Computer ▁ Science ▁ portal ▁ for ▁ Geeks " NEW_LINE minMaxLengthWords ( a ) NEW_LINE


import java . util . * ; class GFG { static void findMaximumBefore ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int currAns = - 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > currAns && arr [ j ] < arr [ i ] ) { currAns = arr [ j ] ; } } System . out . print ( currAns + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 6 , 8 , 5 } ; int n = arr . length ; findMaximumBefore ( arr , n ) ; } }

def findMaximumBefore ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT currAns = - 1 NEW_LINE for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] > currAns and arr [ j ] < arr [ i ] ) : NEW_LINE INDENT currAns = arr [ j ] NEW_LINE DEDENT DEDENT print ( currAns , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 6 , 8 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findMaximumBefore ( arr , n ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . Collections ; class GFG { static int minimumCostOfBreaking ( Integer X [ ] , Integer Y [ ] , int m , int n ) { int res = 0 ; Arrays . sort ( X , Collections . reverseOrder ( ) ) ; Arrays . sort ( Y , Collections . reverseOrder ( ) ) ; int hzntl = 1 , vert = 1 ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( X [ i ] > Y [ j ] ) { res += X [ i ] * vert ; hzntl ++ ; i ++ ; } else { res += Y [ j ] * hzntl ; vert ++ ; j ++ ; } } int total = 0 ; while ( i < m ) total += X [ i ++ ] ; res += total * vert ; total = 0 ; while ( j < n ) total += Y [ j ++ ] ; res += total * hzntl ; return res ; } public static void main ( String arg [ ] ) { int m = 6 , n = 4 ; Integer X [ ] = { 2 , 1 , 3 , 1 , 4 } ; Integer Y [ ] = { 4 , 1 , 2 } ; System . out . print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) ; } }

def minimumCostOfBreaking ( X , Y , m , n ) : NEW_LINE INDENT res = 0 NEW_LINE X . sort ( reverse = True ) NEW_LINE Y . sort ( reverse = True ) NEW_LINE hzntl = 1 ; vert = 1 NEW_LINE i = 0 ; j = 0 NEW_LINE while ( i < m and j < n ) : NEW_LINE INDENT if ( X [ i ] > Y [ j ] ) : NEW_LINE INDENT res += X [ i ] * vert NEW_LINE hzntl += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += Y [ j ] * hzntl NEW_LINE vert += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT total = 0 NEW_LINE while ( i < m ) : NEW_LINE INDENT total += X [ i ] NEW_LINE i += 1 NEW_LINE DEDENT res += total * vert NEW_LINE total = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT total += Y [ j ] NEW_LINE j += 1 NEW_LINE DEDENT res += total * hzntl NEW_LINE return res NEW_LINE DEDENT m = 6 ; n = 4 NEW_LINE X = [ 2 , 1 , 3 , 1 , 4 ] NEW_LINE Y = [ 4 , 1 , 2 ] NEW_LINE print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) NEW_LINE


public class GFG { public static int getSum ( int arr [ ] , int p ) { return arr . length ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 8 } ; int p = 7 ; System . out . print ( getSum ( arr , p ) ) ; } }

def getSum ( arr , p ) : NEW_LINE INDENT return len ( arr ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 8 ] NEW_LINE p = 7 NEW_LINE print ( getSum ( arr , p ) ) NEW_LINE DEDENT


class GFG { static int [ ] solve ( int n ) { int low = 1 , high = ( int ) 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = ( int ) 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; int ans [ ] = { r , c } ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] p = solve ( n ) ; System . out . println ( p [ 0 ] + " ▁ " + p [ 1 ] ) ; } }

def solve ( n ) : NEW_LINE INDENT low = 1 NEW_LINE high = 10 ** 4 NEW_LINE x , p = n , 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( x - sum >= 1 ) : NEW_LINE INDENT p = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT start , end , y , q = 1 , 10 ** 4 , 1 , 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( y + sum <= n ) : NEW_LINE INDENT q = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT x = x - ( p * ( p + 1 ) ) // 2 NEW_LINE y = y + ( q * ( q + 1 ) ) // 2 NEW_LINE r = x NEW_LINE c = q + 1 - n + y NEW_LINE return r , c NEW_LINE DEDENT n = 5 NEW_LINE r , c = solve ( n ) NEW_LINE print ( r , c ) NEW_LINE


class GFG { static int maxNormalSubString ( char [ ] P , char [ ] Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { int pos = P [ right ] - ' a ' ; if ( Q [ pos ] == '0' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { int pos = P [ left ] - ' a ' ; left ++ ; if ( Q [ pos ] == '0' ) count -- ; if ( count < K ) break ; } } return ans ; } public static void main ( String [ ] args ) { String P = " giraffe " , Q = "01111001111111111011111111" ; int K = 2 ; int N = P . length ( ) ; System . out . print ( maxNormalSubString ( P . toCharArray ( ) , Q . toCharArray ( ) , K , N ) ) ; } }

def maxNormalSubstring ( P , Q , K , N ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE left , right = 0 , 0 NEW_LINE ans = 0 NEW_LINE while ( right < N ) : NEW_LINE INDENT while ( right < N and count <= K ) : NEW_LINE INDENT pos = ord ( P [ right ] ) - ord ( ' a ' ) NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT if ( count + 1 > K ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT right += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT ans = max ( ans , right - left ) NEW_LINE DEDENT DEDENT while ( left < right ) : NEW_LINE INDENT pos = ord ( P [ left ] ) - ord ( ' a ' ) NEW_LINE left += 1 NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( count < K ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if ( __name__ == " _ _ main _ _ " ) : NEW_LINE INDENT P = " giraffe " NEW_LINE Q = "01111001111111111011111111" NEW_LINE K = 2 NEW_LINE N = len ( P ) NEW_LINE print ( maxNormalSubstring ( P , Q , K , N ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int getPrime ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; Vector < Boolean > prime = new Vector < > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . add ( i , Boolean . TRUE ) ; prime . add ( 1 , Boolean . FALSE ) ; prime . add ( 2 , Boolean . FALSE ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . add ( i , Boolean . FALSE ) ; } } int maximum = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . get ( arr [ i ] ) ) { maximum = Math . max ( maximum , arr [ i ] ) ; } } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = arr . length ; System . out . println ( getPrime ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def getPrime ( arr , n ) : NEW_LINE INDENT max_val = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > max_val ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT maximum = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT maximum = max ( maximum , arr [ i ] ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 10 , 15 , 7 , 6 , 8 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getPrime ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; int min = Integer . MAX_VALUE ; int level = 0 ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node == null ) { System . out . print ( " level ▁ " + level + " ▁ min ▁ is ▁ = ▁ " + min + " \n " ) ; if ( q . isEmpty ( ) ) break ; q . add ( null ) ; level ++ ; min = Integer . MAX_VALUE ; continue ; } if ( min > node . data ) min = node . data ; if ( node . left != null ) { q . add ( node . left ) ; } if ( node . right != null ) { q . add ( node . right ) ; } } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Every ▁ Level ▁ minimum ▁ is " + " \n " ) ; printPerLevelMinimum ( root ) ; } }

from queue import Queue NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def heightoftree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left = heightoftree ( root . left ) NEW_LINE right = heightoftree ( root . right ) NEW_LINE if left > right : NEW_LINE INDENT return left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return right + 1 NEW_LINE DEDENT DEDENT def printPerLevelMinimum ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = Queue ( ) NEW_LINE q . put ( root ) NEW_LINE q . put ( None ) NEW_LINE Min = 9999999999999 NEW_LINE level = 0 NEW_LINE while ( q . empty ( ) == False ) : NEW_LINE INDENT node = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( node == None ) : NEW_LINE INDENT print ( " level " , level , " min ▁ is ▁ = " , Min ) NEW_LINE if ( q . empty ( ) ) : NEW_LINE INDENT break NEW_LINE DEDENT q . put ( None ) NEW_LINE level += 1 NEW_LINE Min = 999999999999 NEW_LINE continue NEW_LINE DEDENT if ( Min > node . data ) : NEW_LINE INDENT Min = node . data NEW_LINE DEDENT if ( node . left != None ) : NEW_LINE INDENT q . put ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . put ( node . right ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( " Every ▁ Level ▁ minimum ▁ is " ) NEW_LINE printPerLevelMinimum ( root ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 10000 ; static int n , m ; static int [ ] dp = new int [ N ] ; static int minOperations ( int k ) { if ( k <= 0 || k >= 10000 ) return 1000000000 ; if ( k == m ) return 0 ; dp [ k ] = dp [ k ] ; if ( dp [ k ] != - 1 ) return dp [ k ] ; dp [ k ] = 1000000000 ; dp [ k ] = 1 + Math . min ( minOperations ( 2 * k ) , minOperations ( k - 1 ) ) ; return dp [ k ] ; } public static void main ( String [ ] args ) { n = 4 ; m = 6 ; Arrays . fill ( dp , - 1 ) ; System . out . println ( minOperations ( n ) ) ; } }

N = 1000 NEW_LINE dp = [ - 1 ] * N NEW_LINE def minOperations ( k ) : NEW_LINE INDENT if ( k <= 0 or k >= 1000 ) : NEW_LINE INDENT return 1e9 NEW_LINE DEDENT if ( k == m ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp [ k ] = dp [ k ] NEW_LINE if ( dp [ k ] != - 1 ) : NEW_LINE INDENT return dp [ k ] NEW_LINE DEDENT dp [ k ] = 1e9 NEW_LINE dp [ k ] = 1 + min ( minOperations ( 2 * k ) , minOperations ( k - 1 ) ) NEW_LINE return dp [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE m = 6 NEW_LINE print ( minOperations ( n ) ) NEW_LINE DEDENT


import java . io . * ; class MaxSumPath { static int maxSum ( int [ ] [ ] mat , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ ] [ ] = new int [ n ] [ n ] ; int maxSum = Integer . MIN_VALUE , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = Integer . MIN_VALUE ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 ] [ j - 1 ] ) ) max = dp [ i + 1 ] [ j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 ] [ j + 1 ] ) ) max = dp [ i + 1 ] [ j + 1 ] ; dp [ i ] [ j ] = mat [ i ] [ j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 ] [ j ] ) maxSum = dp [ 0 ] [ j ] ; return maxSum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , 6 , 1 , 7 } , { - 2 , 10 , 8 , - 1 } , { 3 , - 7 , - 9 , 11 } , { 12 , - 4 , 2 , 6 } } ; int n = 4 ; System . out . println ( " Maximum ▁ Sum ▁ = ▁ " + maxSum ( mat , n ) ) ; } }

SIZE = 10 NEW_LINE INT_MIN = - 10000000 NEW_LINE def maxSum ( mat , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return mat [ 0 ] [ 0 ] NEW_LINE DEDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE maxSum = INT_MIN NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT maxi = INT_MIN NEW_LINE if ( ( ( ( j - 1 ) >= 0 ) and ( maxi < dp [ i + 1 ] [ j - 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j - 1 ] NEW_LINE DEDENT if ( ( ( ( j + 1 ) < n ) and ( maxi < dp [ i + 1 ] [ j + 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j + 1 ] NEW_LINE DEDENT dp [ i ] [ j ] = mat [ i ] [ j ] + maxi NEW_LINE DEDENT DEDENT for j in range ( n ) : NEW_LINE INDENT if ( maxSum < dp [ 0 ] [ j ] ) : NEW_LINE INDENT maxSum = dp [ 0 ] [ j ] NEW_LINE DEDENT DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 5 , 6 , 1 , 7 ] , [ - 2 , 10 , 8 , - 1 ] , [ 3 , - 7 , - 9 , 11 ] , [ 12 , - 4 , 2 , 6 ] ] NEW_LINE n = 4 NEW_LINE print ( " Maximum ▁ Sum = " , maxSum ( mat , n ) ) NEW_LINE DEDENT


class GFG { static int countPaths ( int m , int n ) { int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i == 1 || j == 1 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; } } return dp [ m ] [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; int m = 5 ; System . out . println ( countPaths ( n , m ) ) ; } }

def countPaths ( m , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i == 1 or j == 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ m ] [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 5 NEW_LINE print ( countPaths ( n , m ) ) NEW_LINE DEDENT


class GFG { public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int count [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . length ; System . out . println ( findMaxGCD ( arr , n ) ) ; } }

def findMaxGCD ( arr , n ) : NEW_LINE INDENT high = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT count = [ 0 ] * ( high + 1 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT counter = 0 NEW_LINE for i in range ( high , 0 , - 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( j <= high ) : NEW_LINE INDENT if ( count [ j ] > 0 ) : NEW_LINE INDENT counter += count [ j ] NEW_LINE DEDENT j += i NEW_LINE if ( counter == 2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT counter = 0 NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxGCD ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int calculateEvenSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ 2 * n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = 0 ; for ( int i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + + calculateEvenSum ( n ) ) ; } }

def calculateEvenSum ( n ) : NEW_LINE INDENT if n <= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT fibo = [ 0 ] * ( 2 * n + 1 ) NEW_LINE fibo [ 0 ] , fibo [ 1 ] = 0 , 1 NEW_LINE sum = 0 NEW_LINE for i in range ( 2 , 2 * n + 1 ) : NEW_LINE INDENT fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] NEW_LINE if i % 2 == 0 : NEW_LINE INDENT sum += fibo [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE print ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto " , n , " terms : " , calculateEvenSum ( n ) ) NEW_LINE DEDENT


class GFG { static void printPattern ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( i ) ; System . out . print ( " \n " ) ; } for ( int i = n - 1 ; i > 0 ; i -- ) { for ( int j = i ; j > 0 ; j -- ) System . out . print ( i ) ; System . out . print ( " \n " ) ; } } public static void main ( String arg [ ] ) { int n = 8 ; printPattern ( n ) ; } }

def printPattern ( n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT n = 8 NEW_LINE printPattern ( n ) NEW_LINE


class GFGq { static int max_Element = ( int ) ( 1e6 + 5 ) ; static int sum1 [ ] = new int [ max_Element ] , sum2 [ ] = new int [ max_Element ] , sum3 [ ] = new int [ max_Element ] ; static void precomputation ( int arr [ ] , int n ) { for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum1 [ j ] += i ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum2 [ j ] += ( sum1 [ j ] - i ) * i ; for ( int i = 1 ; i < max_Element ; i ++ ) sum2 [ i ] /= 2 ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum3 [ j ] += i * ( sum2 [ j ] - i * ( sum1 [ j ] - i ) ) ; for ( int i = 1 ; i < max_Element ; i ++ ) sum3 [ i ] /= 3 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( sum3 [ arr [ i ] ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 5 , 6 } ; int n = arr . length ; precomputation ( arr , n ) ; } }

max_Element = 100005 NEW_LINE sum1 = [ 0 for i in range ( max_Element ) ] NEW_LINE sum2 = [ 0 for i in range ( max_Element ) ] NEW_LINE sum3 = [ 0 for i in range ( max_Element ) ] NEW_LINE def precomputation ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum1 [ j ] += i NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum2 [ j ] += ( sum1 [ j ] - i ) * i NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT sum2 [ i ] = int ( sum2 [ i ] / 2 ) NEW_LINE DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum3 [ j ] += i * ( sum2 [ j ] - i * ( sum1 [ j ] - i ) ) NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT sum3 [ i ] = int ( sum3 [ i ] / 3 ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( sum3 [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE precomputation ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 3 ; static int M = 3 ; static int sumMaxOccurring ( int arr [ ] [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . containsKey ( arr [ i ] [ j ] ) ) { mp . put ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) ; } else { mp . put ( arr [ i ] [ j ] , 1 ) ; } } } int sum = 0 ; int maxFreq = Integer . MIN_VALUE ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) { if ( itr . getValue ( ) > maxFreq ) { maxFreq = itr . getValue ( ) ; } } for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) { if ( itr . getValue ( ) == maxFreq ) { sum += ( itr . getKey ( ) ) * ( itr . getValue ( ) ) ; } } return sum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 5 , 6 } } ; System . out . println ( sumMaxOccurring ( mat ) ) ; } }

import sys NEW_LINE N = 3 NEW_LINE M = 3 NEW_LINE def sumMaxOccuring ( arr ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if arr [ i ] [ j ] in mp : NEW_LINE INDENT mp [ arr [ i ] [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT s = 0 NEW_LINE maxFreq = - sys . maxsize NEW_LINE for i in mp : NEW_LINE INDENT if mp [ i ] > maxFreq : NEW_LINE INDENT maxFreq = mp [ i ] NEW_LINE DEDENT DEDENT for i in mp : NEW_LINE INDENT if mp [ i ] == maxFreq : NEW_LINE INDENT s += i * mp [ i ] NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 5 , 6 ] ] NEW_LINE print ( sumMaxOccuring ( mat ) ) NEW_LINE DEDENT