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

def countP ( n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 0 NEW_LINE DEDENT for i in range ( k + 1 ) : NEW_LINE INDENT dp [ 0 ] [ k ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT if ( j == 1 or i == j ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( j * dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n ] [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( countP ( 5 , 2 ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int getLargestSum ( int N ) { int max_sum = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = i + 1 ; j <= N ; j ++ ) { if ( i * j % ( i + j ) == 0 ) max_sum = Math . max ( max_sum , i + j ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int N = 25 ; int max_sum = getLargestSum ( N ) ; System . out . print ( max_sum ) ; } }

def getLargestSum ( N ) : NEW_LINE INDENT max_sum = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , N + 1 , 1 ) : NEW_LINE INDENT if ( i * j % ( i + j ) == 0 ) : NEW_LINE INDENT max_sum = max ( max_sum , i + j ) NEW_LINE DEDENT DEDENT DEDENT return max_sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE max_sum = getLargestSum ( N ) NEW_LINE print ( max_sum ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findRemainders ( long n ) { HashSet < Long > vc = new HashSet < Long > ( ) ; for ( long i = 1 ; i <= Math . ceil ( Math . sqrt ( n ) ) ; i ++ ) vc . add ( n / i ) ; for ( long i = ( long ) ( n / Math . ceil ( Math . sqrt ( n ) ) - 1 ) ; i >= 0 ; i -- ) vc . add ( i ) ; for ( long it : vc ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { long n = 5 ; findRemainders ( n ) ; } }

from math import ceil , floor , sqrt NEW_LINE def findRemainders ( n ) : NEW_LINE INDENT vc = dict ( ) NEW_LINE for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT vc [ n // i ] = 1 NEW_LINE DEDENT for i in range ( n // ceil ( sqrt ( n ) ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT vc [ i ] = 1 NEW_LINE DEDENT for it in sorted ( vc ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE findRemainders ( n ) NEW_LINE


import java . util . * ; class GFG { static int findRotations ( String str ) { String tmp = str + str ; int n = str . length ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { String substring = tmp . substring ( i , str . length ( ) ) ; if ( str == substring ) return i ; } return n ; } public static void main ( String [ ] args ) { String str = " abc " ; System . out . println ( findRotations ( str ) ) ; } }

def findRotations ( str ) : NEW_LINE INDENT tmp = str + str NEW_LINE n = len ( str ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT substring = tmp [ i : i + n ] NEW_LINE if ( str == substring ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abc " NEW_LINE print ( findRotations ( str ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int calculate ( String N ) { int len = N . length ( ) ; int l = ( len ) / 2 ; int count = 0 ; for ( int i = 1 ; i <= l ; i ++ ) { String s = N . substring ( 0 , i ) ; int l1 = s . length ( ) ; String t = N . substring ( i , l1 + i ) ; if ( s . charAt ( 0 ) == '0' || t . charAt ( 0 ) == '0' ) continue ; if ( s . compareTo ( t ) == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { String N = "2202200" ; System . out . print ( calculate ( N ) ) ; } }

def calculate ( N ) : NEW_LINE INDENT length = len ( N ) NEW_LINE l = int ( ( length ) / 2 ) NEW_LINE count = 0 NEW_LINE for i in range ( l + 1 ) : NEW_LINE INDENT s = N [ 0 : 0 + i ] NEW_LINE l1 = len ( s ) NEW_LINE t = N [ i : l1 + i ] NEW_LINE try : NEW_LINE INDENT if s [ 0 ] == '0' or t [ 0 ] == '0' : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT except : NEW_LINE INDENT continue NEW_LINE DEDENT if s == t : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT N = str ( "2202200" ) NEW_LINE print ( calculate ( N ) ) NEW_LINE


class GFG { static void findTriangles ( int n ) { int num ; num = n * ( n - 4 ) ; System . out . println ( num ) ; } public static void main ( String [ ] args ) { int n ; n = 6 ; findTriangles ( n ) ; } }

def findTriangles ( n ) : NEW_LINE INDENT num = n * ( n - 4 ) NEW_LINE print ( num ) NEW_LINE DEDENT n = 6 NEW_LINE findTriangles ( n ) NEW_LINE


import java . util . HashMap ; class GFG { static void ReplaceElementsByFrequency ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) == null ) { mp . put ( arr [ i ] , 1 ) ; } else { mp . put ( arr [ i ] , ( mp . get ( arr [ i ] ) + 1 ) ) ; } } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) != null ) { arr [ i ] = mp . get ( arr [ i ] ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 2 , 2 , 5 } ; int n = arr . length ; ReplaceElementsByFrequency ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }

def ReplaceElementsByFrequency ( arr , n ) : NEW_LINE INDENT mp = { i : 0 for i in range ( len ( arr ) ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = mp [ arr [ i ] ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 2 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElementsByFrequency ( arr , n ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


class GFG { static int MAX = 1000 ; static boolean isPermutedMatrix ( int mat [ ] [ ] , int n ) { String str_cat = " " ; for ( int i = 0 ; i < n ; i ++ ) { str_cat = str_cat + " - " + String . valueOf ( mat [ 0 ] [ i ] ) ; } str_cat = str_cat + str_cat ; for ( int i = 1 ; i < n ; i ++ ) { String curr_str = " " ; for ( int j = 0 ; j < n ; j ++ ) { curr_str = curr_str + " - " + String . valueOf ( mat [ i ] [ j ] ) ; } if ( str_cat . contentEquals ( curr_str ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 4 ; int mat [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 4 , 1 , 2 , 3 } , { 3 , 4 , 1 , 2 } , { 2 , 3 , 4 , 1 } } ; if ( isPermutedMatrix ( mat , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }

MAX = 1000 NEW_LINE def isPermutedMatrix ( mat , n ) : NEW_LINE INDENT str_cat = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT str_cat = str_cat + " - " + str ( mat [ 0 ] [ i ] ) NEW_LINE DEDENT str_cat = str_cat + str_cat NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr_str = " " NEW_LINE for j in range ( n ) : NEW_LINE INDENT curr_str = curr_str + " - " + str ( mat [ i ] [ j ] ) NEW_LINE DEDENT if ( str_cat . find ( curr_str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE mat = [ [ 1 , 2 , 3 , 4 ] , [ 4 , 1 , 2 , 3 ] , [ 3 , 4 , 1 , 2 ] , [ 2 , 3 , 4 , 1 ] ] NEW_LINE if ( isPermutedMatrix ( mat , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class Solution { static void niceIndices ( int A [ ] , int n ) { int sum = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { m . put ( A [ i ] , ( m . get ( A [ i ] ) == null ) ? 0 : m . get ( A [ i ] ) + 1 ) ; sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { int k = sum - A [ i ] ; if ( k % 2 == 0 ) { k = k >> 1 ; if ( m . containsKey ( k ) ) { if ( ( A [ i ] == k && m . get ( k ) > 1 ) || ( A [ i ] != k ) ) System . out . print ( ( i + 1 ) + " ▁ " ) ; } } } } public static void main ( String args [ ] ) { int A [ ] = { 8 , 3 , 5 , 2 } ; int n = A . length ; niceIndices ( A , n ) ; } }

from collections import defaultdict NEW_LINE def niceIndices ( A , n ) : NEW_LINE INDENT Sum = 0 NEW_LINE m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ A [ i ] ] += 1 NEW_LINE Sum += A [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT k = Sum - A [ i ] NEW_LINE if k % 2 == 0 : NEW_LINE INDENT k = k >> 1 NEW_LINE if k in m : NEW_LINE INDENT if ( ( A [ i ] == k and m [ k ] > 1 ) or ( A [ i ] != k ) ) : NEW_LINE INDENT print ( ( i + 1 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 8 , 3 , 5 , 2 ] NEW_LINE n = len ( A ) NEW_LINE niceIndices ( A , n ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class Main { static class Pair { int first ; int second ; public Pair ( int x , int y ) { this . first = x ; this . second = y ; } } static void min_subarray ( int [ ] a , int n ) { Stack < Pair > st = new Stack < Pair > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int count = 1 ; while ( st . empty ( ) == false && st . peek ( ) . first > a [ i ] ) { count += st . peek ( ) . second ; st . pop ( ) ; } st . push ( new Pair ( a [ i ] , count ) ) ; System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int [ ] a = { 5 , 4 , 3 , 2 , 1 } ; int n = a . length ; min_subarray ( a , n ) ; } }

def min_subarray ( a , n ) : NEW_LINE INDENT st = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT count = 1 ; NEW_LINE while len ( st ) != 0 and st [ - 1 ] [ 0 ] > a [ i ] : NEW_LINE INDENT count += st [ - 1 ] [ 1 ] ; NEW_LINE st . pop ( ) ; NEW_LINE DEDENT st . append ( ( a [ i ] , count ) ) ; NEW_LINE print ( count , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 4 , 3 , 2 , 1 ] ; NEW_LINE n = len ( a ) ; NEW_LINE min_subarray ( a , n ) ; NEW_LINE DEDENT


class GFG { static int MAX = 100002 ; static void printUniqueElementsInSuffix ( int [ ] arr , int n , int [ ] q , int m ) { int [ ] aux = new int [ MAX ] ; int [ ] mark = new int [ MAX ] ; aux [ n - 1 ] = 1 ; mark [ arr [ n - 1 ] ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( mark [ arr [ i ] ] == 0 ) { aux [ i ] = aux [ i + 1 ] + 1 ; mark [ arr [ i ] ] = 1 ; } else { aux [ i ] = aux [ i + 1 ] ; } } for ( int i = 0 ; i < m ; i ++ ) { System . out . println ( aux [ q [ i ] - 1 ] ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 10000 , 999 } ; int n = arr . length ; int q [ ] = { 1 , 3 , 6 } ; int m = q . length ; printUniqueElementsInSuffix ( arr , n , q , m ) ; } }

MAX = 100002 ; NEW_LINE def printUniqueElementsInSuffix ( arr , n , q , m ) : NEW_LINE INDENT aux = [ 0 ] * MAX ; NEW_LINE mark = [ 0 ] * MAX ; NEW_LINE aux [ n - 1 ] = 1 ; NEW_LINE mark [ arr [ n - 1 ] ] = 1 ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( mark [ arr [ i ] ] == 0 ) : NEW_LINE INDENT aux [ i ] = aux [ i + 1 ] + 1 ; NEW_LINE mark [ arr [ i ] ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT aux [ i ] = aux [ i + 1 ] ; NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT print ( aux [ q [ i ] - 1 ] ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 1 , 2 , 3 , 4 , 10000 , 999 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE q = [ 1 , 3 , 6 ] ; NEW_LINE m = len ( q ) ; NEW_LINE printUniqueElementsInSuffix ( arr , n , q , m ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int sum ( int a , int b ) { int sum = 0 ; for ( int i = 1 ; i <= Math . min ( a , b ) ; i ++ ) if ( a % i == 0 && b % i == 0 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int A = 10 , B = 15 ; System . out . print ( " Sum ▁ = ▁ " + sum ( A , B ) ) ; } }

def sum ( a , b ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , min ( a , b ) ) : NEW_LINE INDENT if ( a % i == 0 and b % i == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT A = 10 NEW_LINE B = 15 NEW_LINE print ( " Sum ▁ = " , sum ( A , B ) ) NEW_LINE


import java . util . Vector ; class GFG { static void findPermutation ( int n ) { Vector < Integer > res = new Vector < Integer > ( ) ; int en = 2 , on = 1 ; if ( n % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { res . add ( en ) ; en += 2 ; } else { res . add ( on ) ; on += 2 ; } } } else { for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) { res . add ( en ) ; en += 2 ; } else { res . add ( on ) ; on += 2 ; } } res . add ( n ) ; res . add ( n - 2 ) ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int n = 9 ; findPermutation ( n ) ; } }

def findPermutation ( n ) : NEW_LINE INDENT res = [ ] NEW_LINE en , on = 2 , 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res . append ( en ) NEW_LINE en += 2 NEW_LINE DEDENT else : NEW_LINE INDENT res . append ( on ) NEW_LINE on += 2 NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n - 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res . append ( en ) NEW_LINE en += 2 NEW_LINE DEDENT else : NEW_LINE INDENT res . append ( on ) NEW_LINE on += 2 NEW_LINE DEDENT DEDENT res . append ( n ) NEW_LINE res . append ( n - 2 ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 ; NEW_LINE findPermutation ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n == 0 || n == 1 ) { return false ; } for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static int sumAscii ( String str , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime ( i + 1 ) ) { sum += ( int ) ( str . charAt ( i ) ) ; } } return sum ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; System . out . println ( sumAscii ( str , n ) ) ; } }

from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def sumAscii ( string , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPrime ( i + 1 ) ) : NEW_LINE INDENT sum += ord ( string [ i ] ) ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE n = len ( string ) ; NEW_LINE print ( sumAscii ( string , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > v = new Vector < > ( ) ; static int [ ] [ ] [ ] [ ] dp = new int [ 18 ] [ 180 ] [ 180 ] [ 2 ] ; static int memo ( int index , int evenSum , int oddSum , int tight ) { if ( index == v . size ( ) ) { if ( evenSum > oddSum ) { return 1 ; } else { return 0 ; } } if ( dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != - 1 ) { return dp [ index ] [ evenSum ] [ oddSum ] [ tight ] ; } int limit = ( tight > 0 ) ? v . get ( index ) : 9 ; int ans = 0 ; for ( int d = 0 ; d <= limit ; d ++ ) { int currTight = 0 ; if ( d == v . get ( index ) ) { currTight = tight ; } if ( d % 2 != 0 ) { ans += memo ( index + 1 , evenSum , oddSum + d , currTight ) ; } else { ans += memo ( index + 1 , evenSum + d , oddSum , currTight ) ; } } dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans ; return ans ; } static int CountNum ( int n ) { v . clear ( ) ; while ( n > 0 ) { v . add ( n % 10 ) ; n = n / 10 ; } Collections . reverse ( v ) ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 180 ; j ++ ) { for ( int k = 0 ; k < 180 ; k ++ ) { for ( int l = 0 ; l < 2 ; l ++ ) { dp [ i ] [ j ] [ k ] [ l ] = - 1 ; } } } } return memo ( 0 , 0 , 0 , 1 ) ; } public static void main ( String [ ] args ) { int L , R ; L = 2 ; R = 10 ; System . out . println ( CountNum ( R ) - CountNum ( L - 1 ) ) ; } }

def memo ( index , evenSum , oddSum , tight ) : NEW_LINE INDENT if index == len ( v ) : NEW_LINE INDENT if evenSum > oddSum : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if dp [ index ] [ evenSum ] [ oddSum ] [ tight ] != - 1 : NEW_LINE INDENT return dp [ index ] [ evenSum ] [ oddSum ] [ tight ] NEW_LINE DEDENT limit = v [ index ] if tight else 9 NEW_LINE ans = 0 NEW_LINE for d in range ( limit + 1 ) : NEW_LINE INDENT currTight = 0 NEW_LINE if d == v [ index ] : NEW_LINE INDENT currTight = tight NEW_LINE DEDENT if d % 2 != 0 : NEW_LINE INDENT ans += memo ( index + 1 , evenSum , oddSum + d , currTight ) NEW_LINE DEDENT else : NEW_LINE INDENT ans += memo ( index + 1 , evenSum + d , oddSum , currTight ) NEW_LINE DEDENT DEDENT dp [ index ] [ evenSum ] [ oddSum ] [ tight ] = ans NEW_LINE return ans NEW_LINE DEDENT def countNum ( n ) : NEW_LINE INDENT global dp , v NEW_LINE v . clear ( ) NEW_LINE num = [ ] NEW_LINE while n : NEW_LINE INDENT v . append ( n % 10 ) NEW_LINE n //= 10 NEW_LINE DEDENT v . reverse ( ) NEW_LINE dp = [ [ [ [ - 1 , - 1 ] for i in range ( 180 ) ] for j in range ( 180 ) ] for k in range ( 18 ) ] NEW_LINE return memo ( 0 , 0 , 0 , 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT dp = [ ] NEW_LINE v = [ ] NEW_LINE L = 2 NEW_LINE R = 10 NEW_LINE print ( countNum ( R ) - countNum ( L - 1 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int Circular ( int n ) { int Result = 1 ; while ( n > 0 ) { Result = Result * n ; n -- ; } return Result ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( Circular ( n - 1 ) ) ; } }

def Circular ( n ) : NEW_LINE INDENT Result = 1 NEW_LINE while n > 0 : NEW_LINE INDENT Result = Result * n NEW_LINE n -= 1 NEW_LINE DEDENT return Result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( Circular ( n - 1 ) ) NEW_LINE DEDENT


import java . util . * ; import java . util . HashMap ; import java . util . Map ; class GFG { static String Hex ( int num ) { HashMap < Integer , Character > m = new HashMap < Integer , Character > ( ) ; char digit = '0' ; char c = ' a ' ; for ( int i = 0 ; i <= 15 ; i ++ ) { if ( i < 10 ) { m . put ( i , digit ) ; digit ++ ; } else { m . put ( i , c ) ; c ++ ; } } String res = " " ; if ( num == 0 ) { return "0" ; } if ( num > 0 ) { while ( num != 0 ) { res = m . get ( num % 16 ) + res ; num /= 16 ; } } else { int n = num ; while ( n != 0 ) { res = m . get ( n % 16 ) + res ; n /= 16 ; } } return res ; } public static void main ( String [ ] args ) { int x = 134 , y = - 1 , z = - 234 ; System . out . println ( " Hexa ▁ representation ▁ for " ) ; System . out . println ( x + " ▁ is ▁ " + Hex ( x ) ) ; System . out . println ( y + " ▁ is ▁ " + Hex ( y ) ) ; System . out . println ( z + " ▁ is ▁ " + Hex ( z ) ) ; } }

def Hex ( num ) : NEW_LINE INDENT m = dict . fromkeys ( range ( 16 ) , 0 ) ; NEW_LINE digit = ord ( '0' ) ; NEW_LINE c = ord ( ' a ' ) ; NEW_LINE for i in range ( 16 ) : NEW_LINE INDENT if ( i < 10 ) : NEW_LINE INDENT m [ i ] = chr ( digit ) ; NEW_LINE digit += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT m [ i ] = chr ( c ) ; NEW_LINE c += 1 NEW_LINE DEDENT DEDENT res = " " ; NEW_LINE if ( not num ) : NEW_LINE INDENT return "0" ; NEW_LINE DEDENT if ( num > 0 ) : NEW_LINE INDENT while ( num ) : NEW_LINE INDENT res = m [ num % 16 ] + res ; NEW_LINE num //= 16 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT n = num + 2 ** 32 ; NEW_LINE while ( n ) : NEW_LINE INDENT res = m [ n % 16 ] + res ; NEW_LINE n //= 16 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 134 ; y = - 1 ; z = - 234 ; NEW_LINE print ( " Hexa ▁ representation ▁ for " ) ; NEW_LINE print ( x , " is " , Hex ( x ) ) ; NEW_LINE print ( y , " is " , Hex ( y ) ) ; NEW_LINE print ( z , " is " , Hex ( z ) ) ; NEW_LINE DEDENT


class GFG { static int getMin ( int arr [ ] , int n , int k ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . min ( res , arr [ i ] ) ; } } return res ; } static int getMax ( int arr [ ] , int n , int k ) { int res = Integer . MIN_VALUE ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { res = Math . max ( res , arr [ i ] ) ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 1230 , 45 , 67 , 1 } ; int k = 10 ; int n = arr . length ; System . out . println ( " Minimum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMin ( arr , n , k ) ) ; System . out . println ( " Maximum ▁ element ▁ of ▁ array ▁ which ▁ is ▁ divisible ▁ by ▁ k : ▁ " + getMax ( arr , n , k ) ) ; } }

import sys NEW_LINE def getMin ( arr , n , k ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = min ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT def getMax ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT res = max ( res , arr [ i ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1230 , 45 , 67 , 1 ] NEW_LINE k = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMin ( arr , n , k ) ) NEW_LINE print ( " Maximum ▁ element ▁ of ▁ array ▁ which " , " is ▁ divisible ▁ by ▁ k : ▁ " , getMax ( arr , n , k ) ) NEW_LINE DEDENT


class GFG { static final int MAX = 100 ; static void printPrincipalDiagonal ( int mat [ ] [ ] , int n ) { System . out . print ( " Principal ▁ Diagonal : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( mat [ i ] [ i ] + " , ▁ " ) ; } System . out . println ( ) ; } static void printSecondaryDiagonal ( int mat [ ] [ ] , int n ) { System . out . print ( " Secondary ▁ Diagonal : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( mat [ i ] [ n - i - 1 ] + " , ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; int a [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printPrincipalDiagonal ( a , n ) ; printSecondaryDiagonal ( a , n ) ; } }

MAX = 100 ; NEW_LINE def printPrincipalDiagonal ( mat , n ) : NEW_LINE INDENT print ( " Principal ▁ Diagonal : ▁ " , end = " " ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mat [ i ] [ i ] , end = " , ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT def printSecondaryDiagonal ( mat , n ) : NEW_LINE INDENT print ( " Secondary ▁ Diagonal : ▁ " , end = " " ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mat [ i ] [ n - i - 1 ] , end = " , ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE a = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] ; NEW_LINE printPrincipalDiagonal ( a , n ) ; NEW_LINE printSecondaryDiagonal ( a , n ) ; NEW_LINE DEDENT


public class GfG { public static int mostCommon ( int lower , int higher ) { if ( lower == higher ) return lower ; else return 2 ; } public static void main ( String [ ] args ) { int lower = 10 ; int higher = 20 ; System . out . println ( " The ▁ most ▁ frequent ▁ factor ▁ " + mostCommon ( lower , higher ) ) ; } }

def mostCommon ( lower , higher ) : NEW_LINE INDENT if ( lower == higher ) : NEW_LINE INDENT return lower NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT lower = 10 NEW_LINE higher = 20 NEW_LINE print ( " The ▁ most ▁ frequent ▁ factor " , mostCommon ( lower , higher ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX = 1000000 ; static int [ ] Sieve = new int [ MAX + 1 ] ; static void constructSieve ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { if ( Sieve [ i ] == 0 ) { for ( int j = 2 * i ; j <= MAX ; j += i ) { int temp = j ; while ( temp > 1 && temp % i == 0 ) { Sieve [ j ] ++ ; temp = temp / i ; } } } } } static void checkElements ( int A [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( Sieve [ A [ i ] ] == k ) { System . out . println ( " YES " ) ; } else { System . out . println ( " No " ) ; } } } public static void main ( String [ ] args ) { constructSieve ( ) ; int k = 3 ; int A [ ] = { 12 , 36 , 42 , 72 } ; int n = A . length ; checkElements ( A , n , k ) ; } }

MAX = 1000000 NEW_LINE Sieve = [ 0 ] * ( MAX + 1 ) NEW_LINE def constructSieve ( ) : NEW_LINE INDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( Sieve [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , MAX + 1 , i ) : NEW_LINE INDENT temp = j ; NEW_LINE while ( temp > 1 and temp % i == 0 ) : NEW_LINE INDENT Sieve [ j ] += 1 ; NEW_LINE temp = temp // i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def checkElements ( A , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( Sieve [ A [ i ] ] == k ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT constructSieve ( ) ; NEW_LINE k = 3 ; NEW_LINE A = [ 12 , 36 , 42 , 72 ] ; NEW_LINE n = len ( A ) ; NEW_LINE checkElements ( A , n , k ) ; NEW_LINE DEDENT


class GFG { static long computeXOR ( int n ) { int x = n & 3 ; switch ( x ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; case 3 : return 0 ; } return 0 ; } public static void main ( String args [ ] ) { int l = 1 , r = 4 ; System . out . println ( computeXOR ( r ) ^ computeXOR ( l - 1 ) ) ; } }

def computeXOR ( n ) : NEW_LINE INDENT switch = { 0 : n , 1 : 1 , 2 : n + 1 , 3 : 0 , } NEW_LINE return switch . get ( n & 3 , " " ) NEW_LINE DEDENT l = 1 NEW_LINE r = 4 NEW_LINE print ( computeXOR ( r ) ^ computeXOR ( l - 1 ) ) NEW_LINE


import java . io . * ; class GFG { static int numofstring ( int n , int m ) { if ( n == 1 ) return m ; if ( n == 2 ) return m * ( m - 1 ) ; return m * ( m - 1 ) * ( int ) Math . pow ( m - 2 , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 2 , m = 3 ; System . out . println ( numofstring ( n , m ) ) ; } }

def numofstring ( n , m ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return m NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return m * ( m - 1 ) NEW_LINE DEDENT return m * ( m - 1 ) * pow ( m - 2 , n - 2 ) NEW_LINE DEDENT n = 2 NEW_LINE m = 3 NEW_LINE print ( numofstring ( n , m ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static int Largercount ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { int count = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) { int key = arr1 [ i ] ; if ( mp . containsKey ( key ) ) { int freq = mp . get ( key ) ; freq ++ ; mp . put ( key , freq ) ; } else mp . put ( key , 1 ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr2 [ i ] ) && mp . get ( arr2 [ i ] ) != 0 ) { int freq = mp . get ( arr2 [ i ] ) ; freq -- ; mp . put ( arr2 [ i ] , freq ) ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( mp . get ( arr1 [ i ] ) != 0 ) { count ++ ; mp . put ( arr1 [ i ] , 0 ) ; } } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = new int [ ] { 2 , 4 , 4 , 6 , 6 , 6 , 8 , 9 } ; int arr2 [ ] = new int [ ] { 2 , 2 , 4 , 6 , 6 } ; System . out . print ( Largercount ( arr1 , arr2 , 8 , 5 ) ) ; } }

def Largercount ( arr1 , arr2 , m , n ) : NEW_LINE INDENT count = 0 NEW_LINE mp = dict ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT mp [ arr1 [ i ] ] = mp . get ( arr1 [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr2 [ i ] in mp . keys ( ) and mp [ arr2 [ i ] ] != 0 ) : NEW_LINE INDENT mp [ arr2 [ i ] ] -= 1 NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT if ( mp [ arr1 [ i ] ] != 0 ) : NEW_LINE INDENT count += 1 NEW_LINE mp [ arr1 [ i ] ] = 0 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 2 , 4 , 4 , 6 , 6 , 6 , 8 , 9 ] NEW_LINE arr2 = [ 2 , 2 , 4 , 6 , 6 ] NEW_LINE print ( Largercount ( arr1 , arr2 , 8 , 5 ) ) NEW_LINE


class GFG { static void MinimumSwapOperations ( String s ) { boolean zero_exist = false ; boolean multiple_of_2 = false ; int sum = 0 ; int index_of_zero = 0 ; boolean more_zero = false ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int val = s . charAt ( i ) - '0' ; if ( zero_exist == true ) more_zero = true ; if ( val == 0 ) { zero_exist = true ; index_of_zero = i ; } sum += val ; } if ( zero_exist == false || sum % 3 != 0 ) { System . out . println ( " - 1" ) ; return ; } for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int val = s . charAt ( i ) - '0' ; if ( val % 2 == 0 && i != index_of_zero ) multiple_of_2 = true ; } if ( multiple_of_2 == false ) { System . out . println ( " - 1" ) ; return ; } int last_val = s . charAt ( s . length ( ) - 1 ) - '0' ; int second_last_val = s . charAt ( s . length ( ) - 2 ) - '0' ; if ( last_val == 0 && second_last_val % 2 == 0 ) System . out . println ( 0 ) ; else if ( ( last_val == 0 && second_last_val % 2 != 0 ) || ( last_val % 2 == 0 && second_last_val == 0 ) ) System . out . println ( 1 ) ; else if ( more_zero == true && ( last_val == 0 && second_last_val % 2 != 0 ) ) System . out . println ( 1 ) ; else System . out . println ( 2 ) ; } public static void main ( String [ ] args ) { String N = "20" ; MinimumSwapOperations ( N ) ; } }

def MinimumSwapOperations ( s ) : NEW_LINE INDENT zero_exist = False NEW_LINE multiple_of_2 = False NEW_LINE sum = 0 NEW_LINE index_of_zero = 0 NEW_LINE more_zero = False NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( zero_exist == True ) : NEW_LINE INDENT more_zero = True NEW_LINE DEDENT if ( val == 0 ) : NEW_LINE INDENT zero_exist = True NEW_LINE index_of_zero = i NEW_LINE DEDENT sum += val NEW_LINE DEDENT if ( zero_exist == False or sum % 3 != 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( val % 2 == 0 and i != index_of_zero ) : NEW_LINE INDENT multiple_of_2 = True NEW_LINE DEDENT DEDENT if ( multiple_of_2 == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT last_val = ord ( s [ len ( s ) - 1 ] ) - ord ( '0' ) NEW_LINE second_last_val = ord ( s [ len ( s ) - 2 ] ) - ord ( '0' ) NEW_LINE if ( last_val == 0 and second_last_val % 2 == 0 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT elif ( ( last_val == 0 and second_last_val % 2 != 0 ) or ( last_val % 2 == 0 and second_last_val == 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT elif ( more_zero == True and ( last_val == 0 and second_last_val % 2 != 0 ) ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 2 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = "20" NEW_LINE MinimumSwapOperations ( N ) NEW_LINE DEDENT


public class GFG { static int maxOnesIndex ( boolean arr [ ] , int n ) { int start = 0 ; int end = 0 ; int maxIndex = - 1 ; int lastInd = - 1 ; int maxCnt = 0 ; while ( end < n ) { while ( end < n && arr [ end ] ) { end ++ ; } if ( maxCnt < end - start && lastInd != - 1 ) { maxCnt = end - start ; maxIndex = lastInd ; } start = lastInd + 1 ; lastInd = end ; end ++ ; } if ( maxCnt < end - start && lastInd != - 1 ) { maxCnt = end - start ; maxIndex = lastInd ; } return maxIndex ; } static public void main ( String [ ] args ) { boolean arr [ ] = { true , true , false , false , true , false , true , true , true , false , true , true , true , } ; int n = arr . length ; System . out . println ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " + maxOnesIndex ( arr , n ) ) ; } }

def maxOnesIndex ( arr , n ) : NEW_LINE INDENT start = 0 NEW_LINE end = 0 NEW_LINE maxIndex = - 1 NEW_LINE lastInd = - 1 NEW_LINE maxCnt = 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT while ( end < n and arr [ end ] ) : NEW_LINE INDENT end += 1 NEW_LINE DEDENT if ( maxCnt < end - start and lastInd != - 1 ) : NEW_LINE INDENT maxCnt = end - start NEW_LINE maxIndex = lastInd NEW_LINE DEDENT start = lastInd + 1 NEW_LINE lastInd = end NEW_LINE end += 1 NEW_LINE DEDENT if ( maxCnt < end - start and lastInd != - 1 ) : NEW_LINE INDENT maxCnt = end - start NEW_LINE maxIndex = lastInd NEW_LINE DEDENT return maxIndex NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " , maxOnesIndex ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean checking ( String s ) { int c = 0 ; int n = s . length ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '0' ) c ++ ; if ( c >= 6 && s . charAt ( i ) == '1' ) return true ; } return false ; } public static void main ( String [ ] args ) { String s = "100010001" ; if ( checking ( s ) ) System . out . println ( " Possible " ) ; else System . out . println ( " Not ▁ possible " ) ; } }

def checking ( s ) : NEW_LINE INDENT c = 0 NEW_LINE n = len ( s ) NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT if ( c >= 6 and s [ i ] == '1' ) : NEW_LINE INDENT return True NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "100010001" NEW_LINE if ( checking ( s ) ) : NEW_LINE INDENT print ( " Possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int trailingZero ( int x ) { int i = 5 , count = 0 ; while ( x > i ) { count = count + x / i ; i = i * 5 ; } return count ; } static int countProductTrailing ( int M , int N ) { return trailingZero ( N ) + trailingZero ( M ) ; } public static void main ( String args [ ] ) { int N = 67 , M = 98 ; System . out . println ( countProductTrailing ( N , M ) ) ; } }

def trailingZero ( x ) : NEW_LINE INDENT i = 5 NEW_LINE count = 0 NEW_LINE while ( x > i ) : NEW_LINE INDENT count = count + x // i NEW_LINE i = i * 5 NEW_LINE DEDENT return count NEW_LINE DEDENT def countProductTrailing ( M , N ) : NEW_LINE INDENT return trailingZero ( N ) + trailingZero ( M ) NEW_LINE DEDENT N = 67 NEW_LINE M = 98 NEW_LINE print ( countProductTrailing ( N , M ) ) NEW_LINE


import java . io . * ; class GFG { static int countReduce ( String str ) { int n = str . length ( ) ; int res = 0 ; for ( int i = 0 ; i < n / 2 ; i ++ ) res += Math . abs ( str . charAt ( i ) - str . charAt ( n - i - 1 ) ) ; return res ; } public static void main ( String [ ] args ) { String str = " abcd " ; System . out . println ( countReduce ( str ) ) ; } }

def countReduce ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE res = 0 NEW_LINE for i in range ( 0 , int ( n / 2 ) ) : NEW_LINE INDENT res += abs ( int ( ord ( str [ i ] ) ) - int ( ord ( str [ n - i - 1 ] ) ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT str = " abcd " NEW_LINE print ( countReduce ( str ) ) NEW_LINE


import java . io . * ; class GFG { static int findEle ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == sum - arr [ i ] ) return arr [ i ] ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; System . out . print ( findEle ( arr , n ) ) ; } }

def findEle ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == sum - arr [ i ] : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findEle ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; int third = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; System . out . printf ( " The ▁ third ▁ Largest ▁ " + " element ▁ is ▁ % d \n " , third ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . length ; thirdLargest ( arr , n ) ; } }

import sys NEW_LINE def thirdLargest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 3 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) NEW_LINE return NEW_LINE DEDENT first = arr [ 0 ] NEW_LINE for i in range ( 1 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT first = arr [ i ] NEW_LINE DEDENT DEDENT second = - sys . maxsize NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > second and arr [ i ] < first ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT third = - sys . maxsize NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > third and arr [ i ] < second ) : NEW_LINE INDENT third = arr [ i ] NEW_LINE DEDENT DEDENT print ( " The ▁ Third ▁ Largest " , " element ▁ is " , third ) NEW_LINE DEDENT arr = [ 12 , 13 , 1 , 10 , 34 , 16 ] NEW_LINE n = len ( arr ) NEW_LINE thirdLargest ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static int countOp ( int arr [ ] , int n , int k ) { int operations = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) > k ) { int absDiff = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; int currOp = absDiff - k ; if ( arr [ i ] < arr [ i - 1 ] ) arr [ i ] += currOp ; else arr [ i ] -= currOp ; operations += currOp ; } } return operations ; } static public void main ( String [ ] arg ) { int arr [ ] = { 1 , 2 , 5 , 9 } ; int n = arr . length ; int k = 2 ; System . out . println ( countOp ( arr , n , k ) ) ; } }

def countOp ( arr , n , k ) : NEW_LINE INDENT operations = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] - arr [ i - 1 ] ) > k ) : NEW_LINE INDENT absDiff = abs ( arr [ i ] - arr [ i - 1 ] ) ; NEW_LINE currOp = absDiff - k ; NEW_LINE if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT arr [ i ] += currOp ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] -= currOp ; NEW_LINE DEDENT operations += currOp ; NEW_LINE DEDENT DEDENT return operations ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( countOp ( arr , n , k ) ) ; NEW_LINE DEDENT


class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } public static void main ( String [ ] args ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }

def countNumbers ( l , r ) : NEW_LINE INDENT return ( ( r // 6 ) - ( l - 1 ) // 6 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 1000 ; r = 6000 ; NEW_LINE print ( countNumbers ( l , r ) ) ; NEW_LINE DEDENT


import java . io . * ; public class GFG { static boolean isPrime ( int a [ ] , int lower , int upper ) { int n = 0 ; for ( int i = lower - 1 ; i <= upper - 1 ; i ++ ) n += a [ i ] ; if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 5 , 5 , 4 , 7 , 8 , 9 } ; int lower = 3 , upper = 6 ; if ( isPrime ( a , lower , upper ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isPrime ( a , lower , upper ) : NEW_LINE INDENT n = 0 NEW_LINE for i in range ( lower - 1 , upper ) : NEW_LINE INDENT n = n + a [ i ] NEW_LINE DEDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = [ 1 , 2 , 3 , 5 , 5 , 4 , 7 , 8 , 9 ] NEW_LINE lower = 3 NEW_LINE upper = 6 NEW_LINE if ( isPrime ( a , lower , upper ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String search ( int arr [ ] , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 1 , 5 , 8 } ; int n = arr . length ; int x = 1 ; System . out . println ( search ( arr , n , x ) ) ; } }

def search ( arr , n , x ) : NEW_LINE INDENT if ( arr [ n - 1 ] == x ) : NEW_LINE INDENT return " Found " NEW_LINE DEDENT backup = arr [ n - 1 ] NEW_LINE arr [ n - 1 ] = x NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT arr [ n - 1 ] = backup NEW_LINE if ( i < n - 1 ) : NEW_LINE INDENT return " Found " NEW_LINE DEDENT return " Not ▁ Found " NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT arr = [ 4 , 6 , 1 , 5 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE x = 1 NEW_LINE print ( search ( arr , n , x ) ) NEW_LINE


class GFG { static int CountOfOddSum ( int a [ ] [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; int [ ] [ ] cnt = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( a [ i ] [ j ] % 2 == 0 ) { cnt [ i ] [ 0 ] ++ ; } else { cnt [ i ] [ 1 ] ++ ; } } } dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) ; } return dp [ n - 1 ] [ 1 ] ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 } , { 3 , 6 } } ; int n = a . length ; int ans = CountOfOddSum ( a , n ) ; System . out . println ( ans ) ; } }

def CountOfOddSum ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE cnt = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT if ( a [ i ] [ j ] % 2 == 0 ) : NEW_LINE INDENT cnt [ i ] [ 0 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ i ] [ 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) NEW_LINE dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) NEW_LINE DEDENT return dp [ n - 1 ] [ 1 ] NEW_LINE DEDENT a = [ [ 1 , 2 ] , [ 3 , 6 ] ] NEW_LINE n = len ( a ) NEW_LINE ans = CountOfOddSum ( a , n ) NEW_LINE print ( ans ) NEW_LINE


class sol { static boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void main ( String args [ ] ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isPossible ( l , m , x , y ) : NEW_LINE INDENT if ( l * m == x + y ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 3 ; m = 2 ; x = 4 ; y = 2 ; NEW_LINE if ( isPossible ( l , m , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; public class GFG { static int intersection ( int n ) { return n * ( n - 1 ) ; } public static void main ( String [ ] args ) throws IOException { System . out . println ( intersection ( 3 ) ) ; } }

def intersection ( n ) : NEW_LINE INDENT return n * ( n - 1 ) ; NEW_LINE DEDENT print ( intersection ( 3 ) ) NEW_LINE


public class GFG { static int Race ( int B , int C ) { int result = 0 ; result = ( ( C * 100 ) / B ) ; return 100 - result ; } public static void main ( String [ ] args ) { int B = 10 ; int C = 28 ; B = 100 - B ; C = 100 - C ; System . out . println ( Race ( B , C ) + " ▁ meters " ) ; } }

def Race ( B , C ) : NEW_LINE INDENT result = 0 ; NEW_LINE result = ( ( C * 100 ) // B ) NEW_LINE return 100 - result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT B = 10 NEW_LINE C = 28 NEW_LINE B = 100 - B ; NEW_LINE C = 100 - C ; NEW_LINE print ( str ( Race ( B , C ) ) + " ▁ meters " ) NEW_LINE DEDENT


class Test { static int arr [ ] = new int [ ] { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; static void findMinAvgSubarray ( int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } System . out . println ( " Subarray ▁ between ▁ [ " + res_index + " , ▁ " + ( res_index + k - 1 ) + " ] ▁ has ▁ minimum ▁ average " ) ; } public static void main ( String [ ] args ) { int k = 3 ; findMinAvgSubarray ( arr . length , k ) ; } }

def findMinAvgSubarray ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : return 0 NEW_LINE res_index = 0 NEW_LINE curr_sum = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE DEDENT min_sum = curr_sum NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - k ] NEW_LINE if ( curr_sum < min_sum ) : NEW_LINE INDENT min_sum = curr_sum NEW_LINE res_index = ( i - k + 1 ) NEW_LINE DEDENT DEDENT print ( " Subarray ▁ between ▁ [ " , res_index , " , ▁ " , ( res_index + k - 1 ) , " ] ▁ has ▁ minimum ▁ average " ) NEW_LINE DEDENT arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] NEW_LINE k = 3 NEW_LINE n = len ( arr ) NEW_LINE findMinAvgSubarray ( arr , n , k ) NEW_LINE


class GFG { static String isEvenOrOdd ( int L , int R ) { int oddCount = ( R - L ) / 2 ; if ( R % 2 == 1 || L % 2 == 1 ) oddCount ++ ; if ( oddCount % 2 == 0 ) return " Even " ; else return " Odd " ; } public static void main ( String [ ] args ) { int L = 5 , R = 15 ; System . out . println ( isEvenOrOdd ( L , R ) ) ; } }

def isEvenOrOdd ( L , R ) : NEW_LINE INDENT oddCount = ( R - L ) / 2 NEW_LINE if ( R % 2 == 1 or L % 2 == 1 ) : NEW_LINE INDENT oddCount = oddCount + 1 NEW_LINE DEDENT if ( oddCount % 2 == 0 ) : NEW_LINE INDENT return " Even " NEW_LINE DEDENT else : NEW_LINE INDENT return " Odd " NEW_LINE DEDENT DEDENT L = 5 NEW_LINE R = 15 NEW_LINE print ( isEvenOrOdd ( L , R ) ) ; NEW_LINE


class GFG { static double nthRoot ( long A , long N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = Integer . MAX_VALUE ; double xK = 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 ; } static boolean check ( long no , int k ) { double kth_root = nthRoot ( no , k ) ; long num = ( long ) kth_root ; if ( Math . abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } static void printExpo ( long arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int K = 6 ; long arr [ ] = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = arr . length ; printExpo ( arr , n , K ) ; } }

def nthRoot ( A , N ) : NEW_LINE INDENT xPre = 7 NEW_LINE eps = 1e-3 NEW_LINE delX = 10 ** 9 NEW_LINE xK = 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 def check ( no , k ) : NEW_LINE INDENT kth_root = nthRoot ( no , k ) NEW_LINE num = int ( kth_root ) NEW_LINE if ( abs ( num - kth_root ) < 1e-4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def printExpo ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( check ( arr [ i ] , k ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 6 NEW_LINE arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ] NEW_LINE n = len ( arr ) NEW_LINE printExpo ( arr , n , K ) NEW_LINE DEDENT


import java . util . * ; class GFG { static String integer_to_string ( int n , int base ) { String str = " " ; while ( n > 0 ) { int digit = n % base ; n /= base ; str += ( char ) ( digit + '0' ) ; } return str ; } static int isPalindrome ( int i , int k ) { int temp = i ; int m = 0 ; while ( temp > 0 ) { m = temp % 10 + m * 10 ; temp /= 10 ; } if ( m == i ) { String str = integer_to_string ( m , k ) ; StringBuilder sb = new StringBuilder ( str ) ; String str1 = sb . reverse ( ) . toString ( ) ; if ( str . equals ( str1 ) ) { return i ; } } return 0 ; } static void sumPalindrome ( int n , int k ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) { sum += isPalindrome ( i , k ) ; } System . out . println ( " Total ▁ sum ▁ is ▁ " + sum ) ; } public static void main ( String [ ] args ) { int n = 100 ; int k = 2 ; sumPalindrome ( n , k ) ; } }

def integer_to_string ( n , base ) : NEW_LINE INDENT str = " " ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % base ; NEW_LINE n = int ( n / base ) ; NEW_LINE str = chr ( digit + ord ( '0' ) ) + str ; NEW_LINE DEDENT return str ; NEW_LINE DEDENT def isPalindrome ( i , k ) : NEW_LINE INDENT temp = i ; NEW_LINE m = 0 ; NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT m = ( temp % 10 ) + ( m * 10 ) ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE DEDENT if ( m == i ) : NEW_LINE INDENT str = integer_to_string ( m , k ) ; NEW_LINE str1 = str ; NEW_LINE if ( str [ : : - 1 ] == str1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT def sumPalindrome ( n , k ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += isPalindrome ( i , k ) ; NEW_LINE DEDENT print ( " Total ▁ sum ▁ is " , sum ) ; NEW_LINE DEDENT n = 100 ; NEW_LINE k = 2 ; NEW_LINE sumPalindrome ( n , k ) ; NEW_LINE


class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; int a = 1 ; int cnt = 0 ; boolean flag = true ; sum += a ; while ( cnt < N ) { int nextElement ; if ( flag == true ) { nextElement = a * 2 ; sum += nextElement ; flag = ! flag ; } else { nextElement = a * 3 / 2 ; sum += nextElement ; flag = ! flag ; } a = nextElement ; cnt ++ ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int N = 8 ; printSeriesSum ( N ) ; } }

def printSeriesSum ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE a = 1 ; NEW_LINE cnt = 0 ; NEW_LINE flag = True ; NEW_LINE sum += a ; NEW_LINE while ( cnt < N ) : NEW_LINE INDENT nextElement = None ; NEW_LINE if ( flag ) : NEW_LINE INDENT nextElement = a * 2 ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT else : NEW_LINE INDENT nextElement = a * ( 3 / 2 ) ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT a = nextElement ; NEW_LINE cnt += 1 NEW_LINE DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE printSeriesSum ( N ) ; NEW_LINE DEDENT


public class ReplaceDigits { static int replace0with5 ( int number ) { return number += calculateAddedValue ( number ) ; } private static int calculateAddedValue ( int number ) { int result = 0 ; int decimalPlace = 1 ; if ( number == 0 ) { result += ( 5 * decimalPlace ) ; } while ( number > 0 ) { if ( number % 10 == 0 ) result += ( 5 * decimalPlace ) ; number /= 10 ; decimalPlace *= 10 ; } return result ; } public static void main ( String [ ] args ) { System . out . print ( replace0with5 ( 1020 ) ) ; } }

def replace0with5 ( number ) : NEW_LINE INDENT number += calculateAddedValue ( number ) NEW_LINE return number NEW_LINE DEDENT def calculateAddedValue ( number ) : NEW_LINE INDENT result = 0 NEW_LINE decimalPlace = 1 NEW_LINE if ( number == 0 ) : NEW_LINE INDENT result += ( 5 * decimalPlace ) NEW_LINE DEDENT while ( number > 0 ) : NEW_LINE INDENT if ( number % 10 == 0 ) : NEW_LINE INDENT result += ( 5 * decimalPlace ) NEW_LINE DEDENT number //= 10 NEW_LINE decimalPlace *= 10 NEW_LINE DEDENT return result NEW_LINE DEDENT print ( replace0with5 ( 1020 ) ) NEW_LINE


import java . util . * ; class GFG { static class Node { int elementID ; int stepsToeliminate ; public Node ( int elementID , int stepsToeliminate ) { super ( ) ; this . elementID = elementID ; this . stepsToeliminate = stepsToeliminate ; } } ; static void minSteps ( int arr [ ] , int N ) { Stack < Node > s = new Stack < Node > ( ) ; s . add ( new Node ( 0 , - 1 ) ) ; int maxStepsToeliminate = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { int stepsToeliminate = 1 ; while ( ! s . isEmpty ( ) ) { if ( arr [ s . peek ( ) . elementID ] >= arr [ i ] ) { stepsToeliminate = Math . max ( stepsToeliminate , s . peek ( ) . stepsToeliminate + 1 ) ; s . pop ( ) ; } else { break ; } } if ( s . isEmpty ( ) ) { stepsToeliminate = - 1 ; } maxStepsToeliminate = Math . max ( maxStepsToeliminate , stepsToeliminate ) ; s . add ( new Node ( i , stepsToeliminate ) ) ; } System . out . print ( ( maxStepsToeliminate < 0 ? 0 : maxStepsToeliminate ) + " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 7 , 5 } ; int size = arr . length ; minSteps ( arr , size ) ; } }

def minSteps ( arr , N ) : NEW_LINE INDENT s = [ ] ; NEW_LINE s . append ( ( 0 , - 1 ) ) ; NEW_LINE maxStepsToeliminate = - 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT stepsToeliminate = 1 ; NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT if ( arr [ s [ - 1 ] [ 0 ] ] >= arr [ i ] ) : NEW_LINE INDENT stepsToeliminate = max ( stepsToeliminate , s [ - 1 ] [ 1 ] + 1 ) ; NEW_LINE s . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT stepsToeliminate = - 1 ; NEW_LINE DEDENT maxStepsToeliminate = max ( maxStepsToeliminate , stepsToeliminate ) ; NEW_LINE s . append ( ( i , stepsToeliminate ) ) ; NEW_LINE DEDENT print ( 0 if ( maxStepsToeliminate < 0 ) else maxStepsToeliminate ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 7 , 5 ] ; NEW_LINE size = len ( arr ) ; NEW_LINE minSteps ( arr , size ) ; NEW_LINE DEDENT


class GFG { static final double EPSILON = 0.001 ; static double func ( double x ) { return x * x * x - x * x + 2 ; } static double derivFunc ( double x ) { return 3 * x * x - 2 * x ; } static void newtonRaphson ( double x ) { double h = func ( x ) / derivFunc ( x ) ; while ( Math . abs ( h ) >= EPSILON ) { h = func ( x ) / derivFunc ( x ) ; x = x - h ; } System . out . print ( " The ▁ value ▁ of ▁ the " + " ▁ root ▁ is ▁ : ▁ " + Math . round ( x * 100.0 ) / 100.0 ) ; } public static void main ( String [ ] args ) { double x0 = - 20 ; newtonRaphson ( x0 ) ; } }

def func ( x ) : NEW_LINE INDENT return x * x * x - x * x + 2 NEW_LINE DEDENT def derivFunc ( x ) : NEW_LINE INDENT return 3 * x * x - 2 * x NEW_LINE DEDENT def newtonRaphson ( x ) : NEW_LINE INDENT h = func ( x ) / derivFunc ( x ) NEW_LINE while abs ( h ) >= 0.0001 : NEW_LINE INDENT h = func ( x ) / derivFunc ( x ) NEW_LINE x = x - h NEW_LINE DEDENT print ( " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ : ▁ " , " % .4f " % x ) NEW_LINE DEDENT x0 = - 20 NEW_LINE newtonRaphson ( x0 ) NEW_LINE


import java . util . * ; class GFG { static int minimalSteps ( String s , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = Integer . MAX_VALUE ; String s1 = " " , s2 = " " ; dp [ 0 ] = 1 ; s1 += s . charAt ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { s1 += s . charAt ( i ) ; s2 = s . substring ( i + 1 , i + 1 ) ; dp [ i ] = Math . min ( dp [ i ] , dp [ i - 1 ] + 1 ) ; if ( s1 == s2 ) dp [ i * 2 + 1 ] = Math . min ( dp [ i ] + 1 , dp [ i * 2 + 1 ] ) ; } return dp [ n - 1 ] ; } public static void main ( String args [ ] ) { String s = " aaaaaaaa " ; int n = s . length ( ) ; System . out . println ( minimalSteps ( s , n ) / 2 ) ; } }

INT_MAX = 100000000 NEW_LINE def minimalSteps ( s , n ) : NEW_LINE INDENT dp = [ INT_MAX for i in range ( n ) ] NEW_LINE s1 = " " NEW_LINE s2 = " " NEW_LINE dp [ 0 ] = 1 NEW_LINE s1 += s [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT s1 += s [ i ] NEW_LINE s2 = s [ i + 1 : i + 1 + i + 1 ] NEW_LINE dp [ i ] = min ( dp [ i ] , dp [ i - 1 ] + 1 ) NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT dp [ i * 2 + 1 ] = min ( dp [ i ] + 1 , dp [ i * 2 + 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n - 1 ] NEW_LINE DEDENT s = " aaaaaaaa " NEW_LINE n = len ( s ) NEW_LINE print ( minimalSteps ( s , n ) ) NEW_LINE


public class minFlips { static int findFlips ( String str , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str . charAt ( i ) ) res ++ ; last = str . charAt ( i ) ; } return res / 2 ; } public static void main ( String [ ] args ) { String str = "00011110001110" ; int n = str . length ( ) ; System . out . println ( findFlips ( str , n ) ) ; } }

def findFlips ( str , n ) : NEW_LINE INDENT last = ' ▁ ' NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( last != str [ i ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT last = str [ i ] NEW_LINE DEDENT return res // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "00011110001110" NEW_LINE n = len ( str ) NEW_LINE print ( findFlips ( str , n ) ) NEW_LINE DEDENT


class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printPaths ( Node node ) { int path [ ] = new int [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } void printPathsRecur ( Node node , int path [ ] , int pathLen ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) printArray ( path , pathLen ) ; else { printPathsRecur ( node . left , path , pathLen ) ; printPathsRecur ( node . right , path , pathLen ) ; } } void printArray ( int ints [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) System . out . print ( ints [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . printPaths ( tree . root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . left = None NEW_LINE DEDENT DEDENT def printRoute ( stack , root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT stack . append ( root . data ) NEW_LINE if ( root . left == None and root . right == None ) : NEW_LINE INDENT print ( ' ▁ ' . join ( [ str ( i ) for i in stack ] ) ) NEW_LINE DEDENT printRoute ( stack , root . left ) NEW_LINE printRoute ( stack , root . right ) NEW_LINE stack . pop ( ) NEW_LINE DEDENT root = Node ( 1 ) ; NEW_LINE root . left = Node ( 2 ) ; NEW_LINE root . right = Node ( 3 ) ; NEW_LINE root . left . left = Node ( 4 ) ; NEW_LINE root . left . right = Node ( 5 ) ; NEW_LINE printRoute ( [ ] , root ) NEW_LINE


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

def countStr ( n ) : NEW_LINE INDENT return ( 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) // 2 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( countStr ( n ) ) NEW_LINE DEDENT


class GFG { static int a = 0 , b = 0 ; static int isPrime ( int x ) { int s = ( int ) Math . sqrt ( x ) ; for ( int i = 2 ; i <= s ; i ++ ) if ( x % i == 0 ) return 0 ; return 1 ; } static void Num ( int x ) { for ( int i = 2 ; i <= x / 2 ; i ++ ) { if ( isPrime ( i ) != 0 && isPrime ( x - i ) != 0 ) { a = i ; b = x - i ; return ; } } } static void generate ( int n ) { if ( n <= 7 ) System . out . println ( " Impossible " + " ▁ to ▁ form " ) ; if ( n % 2 != 0 ) { Num ( n - 5 ) ; System . out . println ( "2 ▁ 3 ▁ " + a + " ▁ " + b ) ; } else { Num ( n - 4 ) ; System . out . println ( "2 ▁ 2 ▁ " + a + " ▁ " + b ) ; } } public static void main ( String [ ] args ) { int n = 28 ; generate ( n ) ; } }

import math ; NEW_LINE def isPrime ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE for i in range ( 2 , s + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def Num ( x ) : NEW_LINE INDENT ab = [ 0 ] * 2 NEW_LINE for i in range ( 2 , int ( x / 2 ) + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) != 0 and isPrime ( x - i ) != 0 ) : NEW_LINE INDENT ab [ 0 ] = i NEW_LINE ab [ 1 ] = x - i NEW_LINE return ab NEW_LINE DEDENT DEDENT DEDENT def generate ( n ) : NEW_LINE INDENT if ( n <= 7 ) : NEW_LINE INDENT print ( " Impossible ▁ to ▁ form " ) NEW_LINE DEDENT if ( n % 2 != 0 ) : NEW_LINE INDENT ab = Num ( n - 5 ) NEW_LINE print ( "2 ▁ 3" , ab [ 0 ] , ab [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ab = Num ( n - 4 ) NEW_LINE print ( "2 ▁ 2" , ab [ 0 ] , ab [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 28 NEW_LINE generate ( n ) NEW_LINE DEDENT


class GFG { static int countSubStr ( String str , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) ans += ( i + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = "10010" ; int len = str . length ( ) ; System . out . println ( countSubStr ( str , len ) ) ; } }

def countSubStr ( strr , lenn ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == '0' ) : NEW_LINE INDENT ans += ( i + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT strr = "10010" NEW_LINE lenn = len ( strr ) NEW_LINE print ( countSubStr ( strr , lenn ) ) NEW_LINE


import java . util . * ; class GFG { static void sieve ( int n , boolean [ ] prime ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= n ; j += i ) { prime [ j ] = false ; } } } } static int sumOfSubset ( int a [ ] , int n ) { int maxi = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ maxi + 1 ] ; sieve ( maxi , prime ) ; int times = ( int ) Math . pow ( 2 , n - 1 ) ; int sum = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ a [ i ] ] ) sum = ( int ) ( sum * ( Math . pow ( a [ i ] , times ) ) ) ; } return sum ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 7 } ; int n = a . length ; System . out . println ( sumOfSubset ( a , n ) ) ; } }

prime = [ True for i in range ( 100 ) ] NEW_LINE def sieve ( n , prime ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , n + 1 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def SumOfSubset ( a , n ) : NEW_LINE INDENT maxi = max ( a ) NEW_LINE sieve ( maxi , prime ) NEW_LINE times = pow ( 2 , n - 1 ) NEW_LINE Sum = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ a [ i ] ] ) : NEW_LINE INDENT Sum = Sum * ( pow ( a [ i ] , times ) ) NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT a = [ 3 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( SumOfSubset ( a , n ) ) NEW_LINE


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int i , j ; System . out . println ( " Repeated ▁ Elements ▁ are ▁ : " ) ; for ( i = 0 ; i < size ; i ++ ) { for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { RepeatElement repeat = new RepeatElement ( ) ; int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . length ; repeat . printRepeating ( arr , arr_size ) ; } }

def printRepeating ( arr , size ) : NEW_LINE INDENT print ( " Repeating ▁ elements ▁ are ▁ " , end = ' ' ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT if arr [ i ] == arr [ j ] : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


class MaximumDiffrence { int maxDiff ( int arr [ ] , int arr_size ) { int max_diff = arr [ 1 ] - arr [ 0 ] ; int i , j ; for ( i = 0 ; i < arr_size ; i ++ ) { for ( j = i + 1 ; j < arr_size ; j ++ ) { if ( arr [ j ] - arr [ i ] > max_diff ) max_diff = arr [ j ] - arr [ i ] ; } } return max_diff ; } public static void main ( String [ ] args ) { MaximumDiffrence maxdif = new MaximumDiffrence ( ) ; int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; System . out . println ( " Maximum ▁ differnce ▁ is ▁ " + maxdif . maxDiff ( arr , 5 ) ) ; } }

def maxDiff ( arr , arr_size ) : NEW_LINE INDENT max_diff = arr [ 1 ] - arr [ 0 ] NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT for j in range ( i + 1 , arr_size ) : NEW_LINE INDENT if ( arr [ j ] - arr [ i ] > max_diff ) : NEW_LINE INDENT max_diff = arr [ j ] - arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return max_diff NEW_LINE DEDENT arr = [ 1 , 2 , 90 , 10 , 110 ] NEW_LINE size = len ( arr ) NEW_LINE print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , size ) ) NEW_LINE


class GFG { static void fibonacci ( double n ) { double fib ; for ( double i = 0 ; i < n ; i ++ ) { fib = ( Math . pow ( ( 1 + Math . sqrt ( 5 ) ) , i ) - Math . pow ( ( 1 - Math . sqrt ( 5 ) ) , i ) ) / ( Math . pow ( 2 , i ) * Math . sqrt ( 5 ) ) ; System . out . print ( ( int ) fib + " ▁ " ) ; } } public static void main ( String [ ] args ) { double n = 8 ; fibonacci ( n ) ; } }

import math NEW_LINE def fibonacci ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT fib = ( ( pow ( ( 1 + math . sqrt ( 5 ) ) , i ) - pow ( ( 1 - math . sqrt ( 5 ) ) , i ) ) / ( pow ( 2 , i ) * math . sqrt ( 5 ) ) ) ; NEW_LINE print ( int ( fib ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 8 ; NEW_LINE fibonacci ( n ) ; NEW_LINE


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

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


import java . io . * ; class Gfg { static int N = 3 ; static void swapDiagonal ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { int temp = matrix [ i ] [ i ] ; matrix [ i ] [ i ] = matrix [ i ] [ N - i - 1 ] ; matrix [ i ] [ N - i - 1 ] = temp ; } } public static void main ( String arg [ ] ) { int matrix [ ] [ ] = { { 0 , 1 , 2 } , { 3 , 4 , 5 } , { 6 , 7 , 8 } } ; swapDiagonal ( matrix ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } }

N = 3 NEW_LINE def swapDiagonal ( matrix ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] =   \ NEW_LINE INDENT matrix [ i ] [ N - i - 1 ] , matrix [ i ] [ i ] NEW_LINE DEDENT DEDENT DEDENT matrix = [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] NEW_LINE swapDiagonal ( matrix ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( matrix [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int l = i + 1 ; int r = n - 1 ; int x = arr [ i ] ; while ( l < r ) { if ( x + arr [ l ] + arr [ r ] == sum ) { System . out . println ( x + " ▁ " + arr [ l ] + " ▁ " + arr [ r ] ) ; l ++ ; r -- ; } else if ( x + arr [ l ] + arr [ r ] < sum ) l ++ ; else r -- ; } } } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . length ; findTriplets ( arr , n , sum ) ; } }

def findTriplets ( arr , n , sum ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT l = i + 1 ; NEW_LINE r = n - 1 ; NEW_LINE x = arr [ i ] ; NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( x + arr [ l ] + arr [ r ] == sum ) : NEW_LINE INDENT print ( x , arr [ l ] , arr [ r ] ) ; NEW_LINE l = l + 1 ; NEW_LINE r = r - 1 ; NEW_LINE DEDENT elif ( x + arr [ l ] + arr [ r ] < sum ) : NEW_LINE INDENT l = l + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT r = r - 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] ; NEW_LINE sum = - 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE findTriplets ( arr , n , sum ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node next ; } static Node head = null ; static Node Reverse ( Node head , int k ) { Stack < Node > mystack = new Stack < Node > ( ) ; Node current = head ; Node prev = null ; while ( current != null ) { int count = 0 ; while ( current != null && count < k ) { mystack . push ( current ) ; current = current . next ; count ++ ; } while ( mystack . size ( ) > 0 ) { if ( prev == null ) { prev = mystack . peek ( ) ; head = prev ; mystack . pop ( ) ; } else { prev . next = mystack . peek ( ) ; prev = prev . next ; mystack . pop ( ) ; } } } prev . next = null ; return head ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } public static void main ( String [ ] args ) { push ( 9 ) ; push ( 8 ) ; push ( 7 ) ; push ( 6 ) ; push ( 5 ) ; push ( 4 ) ; push ( 3 ) ; push ( 2 ) ; push ( 1 ) ; System . out . println ( " Given ▁ linked ▁ list ▁ " ) ; printList ( head ) ; head = Reverse ( head , 3 ) ; System . out . println ( ) ; System . out . println ( " Reversed ▁ Linked ▁ list ▁ " ) ; printList ( head ) ; } }

class Node ( object ) : NEW_LINE INDENT __slots__ = ' data ' , ' next ' NEW_LINE def __init__ ( self , data = None , next = None ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT def __repr__ ( self ) : NEW_LINE INDENT return repr ( self . data ) NEW_LINE DEDENT DEDENT class LinkedList ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def __repr__ ( self ) : NEW_LINE INDENT nodes = [ ] NEW_LINE curr = self . head NEW_LINE while curr : NEW_LINE INDENT nodes . append ( repr ( curr ) ) NEW_LINE curr = curr . next NEW_LINE DEDENT return ' [ ' + ' , ▁ ' . join ( nodes ) + ' ] ' NEW_LINE DEDENT def prepend ( self , data ) : NEW_LINE INDENT self . head = Node ( data = data , next = self . head ) NEW_LINE DEDENT def reverse ( self , k = 1 ) : NEW_LINE INDENT if self . head is None : NEW_LINE INDENT return NEW_LINE DEDENT curr = self . head NEW_LINE prev = None NEW_LINE new_stack = [ ] NEW_LINE while curr is not None : NEW_LINE INDENT val = 0 NEW_LINE while curr is not None and val < k : NEW_LINE INDENT new_stack . append ( curr . data ) NEW_LINE curr = curr . next NEW_LINE val += 1 NEW_LINE DEDENT while new_stack : NEW_LINE INDENT if prev is None : NEW_LINE INDENT prev = Node ( new_stack . pop ( ) ) NEW_LINE self . head = prev NEW_LINE DEDENT else : NEW_LINE INDENT prev . next = Node ( new_stack . pop ( ) ) NEW_LINE prev = prev . next NEW_LINE DEDENT DEDENT DEDENT prev . next = None NEW_LINE return self . head NEW_LINE DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . prepend ( 9 ) NEW_LINE llist . prepend ( 8 ) NEW_LINE llist . prepend ( 7 ) NEW_LINE llist . prepend ( 6 ) NEW_LINE llist . prepend ( 5 ) NEW_LINE llist . prepend ( 4 ) NEW_LINE llist . prepend ( 3 ) NEW_LINE llist . prepend ( 2 ) NEW_LINE llist . prepend ( 1 ) NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE print ( llist ) NEW_LINE llist . head = llist . reverse ( 3 ) NEW_LINE print ( " Reversed ▁ Linked ▁ list " ) NEW_LINE print ( llist ) NEW_LINE


import java . io . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }

def distinctCount ( arr , n ) : NEW_LINE INDENT count = n ; NEW_LINE i = 0 ; j = n - 1 ; sum = 0 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT while ( i != j and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE DEDENT while ( i != j and arr [ j ] == arr [ j - 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sum = arr [ i ] + arr [ j ] ; NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT elif ( sum < 0 ) : NEW_LINE INDENT i = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT j = j - 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " , distinctCount ( arr , n ) ) ; NEW_LINE


import java . io . * ; class Combination { static void combinationUtil ( int arr [ ] , int data [ ] , int start , int end , int index , int r ) { if ( index == r ) { for ( int j = 0 ; j < r ; j ++ ) System . out . print ( data [ j ] + " ▁ " ) ; System . out . println ( " " ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { data [ index ] = arr [ i ] ; combinationUtil ( arr , data , i + 1 , end , index + 1 , r ) ; } } static void printCombination ( int arr [ ] , int n , int r ) { int data [ ] = new int [ r ] ; combinationUtil ( arr , data , 0 , n - 1 , 0 , r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int r = 3 ; int n = arr . length ; printCombination ( arr , n , r ) ; } }

def printCombination ( arr , n , r ) : NEW_LINE INDENT data = [ 0 ] * r ; NEW_LINE combinationUtil ( arr , data , 0 , n - 1 , 0 , r ) ; NEW_LINE DEDENT def combinationUtil ( arr , data , start , end , index , r ) : NEW_LINE INDENT if ( index == r ) : NEW_LINE INDENT for j in range ( r ) : NEW_LINE INDENT print ( data [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE return ; NEW_LINE DEDENT i = start ; NEW_LINE while ( i <= end and end - i + 1 >= r - index ) : NEW_LINE INDENT data [ index ] = arr [ i ] ; NEW_LINE combinationUtil ( arr , data , i + 1 , end , index + 1 , r ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE r = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE printCombination ( arr , n , r ) ; NEW_LINE


import java . io . * ; class GFG { static void halfsquare ( int n , int x , int y ) { int half = n / 2 ; if ( ( half == x || half == x - 1 ) && ( half == y || half == y - 1 ) ) System . out . println ( " NO " ) ; else System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int n = 100 ; int x = 51 , y = 100 ; halfsquare ( n , x , y ) ; } }

def halfsquare ( n , x , y ) : NEW_LINE INDENT half = n // 2 NEW_LINE if ( ( half == x or half == x - 1 ) and ( half == y or half == y - 1 ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 NEW_LINE x , y = 51 , 100 NEW_LINE halfsquare ( n , x , y ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int mostFrequent ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( mostFrequent ( arr , n ) ) ; } }

def mostFrequent ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE max_count = 1 ; res = arr [ 0 ] ; curr_count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT curr_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ i - 1 ] NEW_LINE DEDENT curr_count = 1 NEW_LINE DEDENT DEDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ n - 1 ] NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( mostFrequent ( arr , n ) ) NEW_LINE


import java . util . Arrays ; import java . io . * ; class GFG { static int items ( int n , int a [ ] ) { Arrays . sort ( a ) ; int z = a [ n - 1 ] ; int x = 1 ; int s = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { s += a [ i ] ; if ( s <= z ) x += 1 ; else break ; } return x ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 5 , 3 , 1 , 5 , 6 } ; System . out . println ( items ( n , a ) ) ; } }

def items ( n , a ) : NEW_LINE INDENT a . sort ( ) NEW_LINE z = a [ n - 1 ] NEW_LINE x = 1 NEW_LINE s = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT s += a [ i ] NEW_LINE if ( s <= z ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT n = 5 NEW_LINE a = [ 5 , 3 , 1 , 5 , 6 ] NEW_LINE print ( items ( n , a ) ) NEW_LINE


import java . util . * ; class GFG { static int leastValue ( int P , int A , int N , int a [ ] ) { int ans = - 1 ; float tmp = Float . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { float t = ( float ) ( P - a [ i ] * 0.006 ) ; if ( Math . abs ( t - A ) < tmp ) { tmp = Math . abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } public static void main ( String [ ] args ) { int N = 2 , P = 12 , A = 2005 ; int a [ ] = { 1000 , 2000 } ; System . out . println ( leastValue ( P , A , N , a ) ) ; } }

def leastValue ( P , A , N , a ) : NEW_LINE INDENT ans = - 1 NEW_LINE tmp = float ( ' inf ' ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = P - a [ i ] * 0.006 NEW_LINE if abs ( t - A ) < tmp : NEW_LINE INDENT tmp = abs ( t - A ) NEW_LINE ans = i NEW_LINE DEDENT DEDENT return a [ ans ] NEW_LINE DEDENT N , P , A = 2 , 12 , 5 NEW_LINE a = [ 1000 , 2000 ] NEW_LINE print ( leastValue ( P , A , N , a ) ) NEW_LINE


class GFG { static int getSingle ( int arr [ ] , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos | ( ones & arr [ i ] ) ; ones = ones ^ arr [ i ] ; common_bit_mask = ~ ( ones & twos ) ; ones &= common_bit_mask ; twos &= common_bit_mask ; } return ones ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 3 , 2 , 3 } ; int n = arr . length ; System . out . println ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }

def getSingle ( arr , n ) : NEW_LINE INDENT ones = 0 NEW_LINE twos = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT twos = twos | ( ones & arr [ i ] ) NEW_LINE ones = ones ^ arr [ i ] NEW_LINE common_bit_mask = ~ ( ones & twos ) NEW_LINE ones &= common_bit_mask NEW_LINE twos &= common_bit_mask NEW_LINE DEDENT return ones NEW_LINE DEDENT arr = [ 3 , 3 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( arr , n ) ) NEW_LINE


import java . io . * ; import java . math . * ; public class GFG { int getMinNum ( int a , int b , int c ) { if ( c < a || c > b ) { return c ; } int x = ( ( b / c ) * c ) + c ; return x ; } public static void main ( String args [ ] ) { int a = 2 ; int b = 4 ; int c = 4 ; GFG g = new GFG ( ) ; System . out . println ( g . getMinNum ( a , b , c ) ) ; } }

def getMinNum ( a , b , c ) : NEW_LINE INDENT if ( c < a or c > b ) : NEW_LINE INDENT return c NEW_LINE DEDENT x = ( ( b // c ) * c ) + c NEW_LINE return x NEW_LINE DEDENT a , b , c = 2 , 4 , 4 NEW_LINE print ( getMinNum ( a , b , c ) ) NEW_LINE


class GFG { static class Point { int x , y ; } static boolean doOverlap ( Point l1 , Point r1 , Point l2 , Point r2 ) { if ( l1 . x >= r2 . x || l2 . x >= r1 . x ) { return false ; } if ( l1 . y <= r2 . y || l2 . y <= r1 . y ) { return false ; } return true ; } public static void main ( String [ ] args ) { Point l1 = new Point ( ) , r1 = new Point ( ) , l2 = new Point ( ) , r2 = new Point ( ) ; l1 . x = 0 ; l1 . y = 10 ; r1 . x = 10 ; r1 . y = 0 ; l2 . x = 5 ; l2 . y = 5 ; r2 . x = 15 ; r2 . y = 0 ; if ( doOverlap ( l1 , r1 , l2 , r2 ) ) { System . out . println ( " Rectangles ▁ Overlap " ) ; } else { System . out . println ( " Rectangles ▁ Don ' t ▁ Overlap " ) ; } } }

class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def doOverlap ( l1 , r1 , l2 , r2 ) : NEW_LINE INDENT if ( l1 . x >= r2 . x or l2 . x >= r1 . x ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( l1 . y <= r2 . y or l2 . y <= r1 . y ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l1 = Point ( 0 , 10 ) NEW_LINE r1 = Point ( 10 , 0 ) NEW_LINE l2 = Point ( 5 , 5 ) NEW_LINE r2 = Point ( 15 , 0 ) NEW_LINE if ( doOverlap ( l1 , r1 , l2 , r2 ) ) : NEW_LINE INDENT print ( " Rectangles ▁ Overlap " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Rectangles ▁ Don ' t ▁ Overlap " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . ceil ( Math . log ( n + 2 ) / Math . log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } StringBuilder sb = new StringBuilder ( right ) ; sb . reverse ( ) ; right = sb . toString ( ) ; String res = left + right ; return res ; } public static void main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; System . out . println ( ans ) ; } }

from math import ceil , log2 NEW_LINE def solve ( n , x , y ) : NEW_LINE INDENT length = ceil ( log2 ( n + 2 ) ) - 1 ; NEW_LINE rank = n - ( 1 << length ) + 1 ; NEW_LINE left = " " ; right = " " ; NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT mask = ( 1 << i ) ; NEW_LINE bit = ( mask & rank ) ; NEW_LINE if ( bit ) : NEW_LINE INDENT left += y ; NEW_LINE right += y ; NEW_LINE DEDENT else : NEW_LINE INDENT left += x ; NEW_LINE right += x ; NEW_LINE DEDENT DEDENT right = right [ : : - 1 ] ; NEW_LINE res = left + right ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 23 ; NEW_LINE x = '4' ; NEW_LINE y = '5' ; NEW_LINE ans = solve ( n , x , y ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static float Area ( float a , float b ) { if ( a < 0 && b < 0 ) return - 1 ; float h = ( float ) Math . sqrt ( ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) / ( Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) ) ) ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 4 ; System . out . println ( Area ( a , b ) ) ; } }

import math ; NEW_LINE def Area ( a , b ) : NEW_LINE INDENT if ( a < 0 and b < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT h = math . sqrt ( ( ( pow ( a , 2 ) + pow ( b , 2 ) ) / ( pow ( a , 2 ) * pow ( b , 2 ) ) ) ) ; NEW_LINE A = 0.70477 * pow ( h , 2 ) ; NEW_LINE return A ; NEW_LINE DEDENT a = 5 ; NEW_LINE b = 4 ; NEW_LINE print ( round ( Area ( a , b ) , 7 ) ) ; NEW_LINE


import java . util . HashMap ; class GFG { 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 ] ) && map . get ( 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 ] ) && map . get ( a [ i ] ) ) return 1 ; map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( b [ i ] ) && map . get ( 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 , False ) ; 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 ] in map : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT map . clear ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if b [ i ] in map : 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 . io . * ; class GFG { static void printReverseFloyd ( int n ) { int curr_val = n * ( n + 1 ) / 2 ; for ( int i = n ; i >= 1 ; i -- ) { for ( int j = i ; j >= 1 ; j -- ) { System . out . printf ( " % 2d ▁ ▁ " , curr_val -- ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; printReverseFloyd ( n ) ; } }

def printReverseFloyd ( n ) : NEW_LINE INDENT curr_val = int ( n * ( n + 1 ) / 2 ) NEW_LINE for i in range ( n + 1 , 1 , - 1 ) : NEW_LINE INDENT for j in range ( i , 1 , - 1 ) : NEW_LINE INDENT print ( curr_val , end = " ▁ ▁ " ) NEW_LINE curr_val -= 1 NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT n = 7 NEW_LINE printReverseFloyd ( n ) NEW_LINE


public class Discriminant { static int disc ( int a , int b , int c ) { int dis = ( int ) Math . pow ( b , 2 ) - ( 4 * a * c ) ; return dis ; } public static void main ( String args [ ] ) { int a = 20 ; int b = 30 ; int c = 10 ; int discriminant = disc ( a , b , c ) ; if ( discriminant > 0 ) { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Positive " ) ; System . out . println ( " Hence ▁ Two ▁ Solutions " ) ; } else if ( discriminant == 0 ) { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Zero " ) ; System . out . println ( " Hence ▁ One ▁ Solution " ) ; } else { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Negative " ) ; System . out . println ( " Hence ▁ No ▁ Real ▁ Solutions " ) ; } } }

def discriminant ( a , b , c ) : NEW_LINE INDENT discriminant = ( b ** 2 ) - ( 4 * a * c ) NEW_LINE if discriminant > 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Positive " ) NEW_LINE print ( ' Hence ▁ Two ▁ Solutions ' ) NEW_LINE DEDENT elif discriminant == 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Zero " ) NEW_LINE print ( ' Hence ▁ One ▁ Solution ' ) NEW_LINE DEDENT elif discriminant < 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Negative " ) NEW_LINE print ( ' Hence ▁ No ▁ Real ▁ Solutions ' ) NEW_LINE DEDENT DEDENT a = 20 NEW_LINE b = 30 NEW_LINE c = 10 NEW_LINE discriminant ( a , b , c ) NEW_LINE


import java . io . * ; public class GFG { void sum ( int [ ] arr , int n ) { int i = n ; if ( arr [ i ] < 9 ) { arr [ i ] = arr [ i ] + 1 ; return ; } arr [ i ] = 0 ; i -- ; sum ( arr , i ) ; if ( arr [ 0 ] > 0 ) System . out . print ( arr [ 0 ] + " , ▁ " ) ; for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i < n ) System . out . print ( " , ▁ " ) ; } } static public void main ( String [ ] args ) { GFG obj = new GFG ( ) ; int n = 4 ; int [ ] arr = { 0 , 1 , 9 , 3 , 9 } ; obj . sum ( arr , n ) ; } }

def sum ( arr , n ) : NEW_LINE INDENT i = n NEW_LINE if ( arr [ i ] < 9 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] + 1 NEW_LINE return NEW_LINE DEDENT arr [ i ] = 0 NEW_LINE i -= 1 NEW_LINE sum ( arr , i ) NEW_LINE if ( arr [ 0 ] > 0 ) : NEW_LINE INDENT print ( arr [ 0 ] , " , ▁ " , end = " " ) NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) NEW_LINE if ( i < n ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE arr = [ 0 , 1 , 9 , 3 , 9 ] NEW_LINE sum ( arr , n ) NEW_LINE


import java . util . * ; class GFG { public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 12 , 13 , 5 , 19 } ; int n = arr . length ; int k = 3 ; primeSumAndProduct ( arr , n , k ) ; } static boolean [ ] SieveOfEratosthenes ( int max_val ) { boolean [ ] prime = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } return prime ; } static void primeSumAndProduct ( int arr [ ] , int n , int k ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , arr [ i ] ) ; boolean [ ] prime = SieveOfEratosthenes ( max_val ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; PriorityQueue < Integer > maxHeap = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; PriorityQueue < Integer > minHeap = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( prime [ arr [ i ] ] ) { minHeap . add ( arr [ i ] ) ; maxHeap . add ( arr [ i ] ) ; } long minProduct = 1 , maxProduct = 1 , minSum = 0 , maxSum = 0 ; while ( k > 0 ) { k -- ; minProduct *= minHeap . peek ( ) ; maxProduct *= maxHeap . peek ( ) ; minSum += minHeap . peek ( ) ; maxSum += maxHeap . peek ( ) ; minHeap . remove ( ) ; maxHeap . remove ( ) ; } System . out . println ( " Sum ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is ▁ " + minSum ) ; System . out . println ( " Sum ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is ▁ " + maxSum ) ; System . out . println ( " Product ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is ▁ " + minProduct ) ; System . out . println ( " Product ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is ▁ " + maxProduct ) ; } }

import heapq NEW_LINE def SieveOfEratosthenes ( max_val ) : NEW_LINE INDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE p = 2 NEW_LINE while p * p <= max_val : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for j in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT return prime NEW_LINE DEDENT def primeSumAndProduct ( arr , n , k ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = SieveOfEratosthenes ( max_val ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE Heap = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT Heap . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT minProduct = 1 NEW_LINE maxProduct = 1 NEW_LINE minSum = 0 NEW_LINE maxSum = 0 NEW_LINE min_k = heapq . nsmallest ( k , Heap ) NEW_LINE max_k = heapq . nlargest ( k , Heap ) NEW_LINE minSum = sum ( min_k ) NEW_LINE maxSum = sum ( max_k ) NEW_LINE for val in min_k : NEW_LINE INDENT minProduct *= val NEW_LINE DEDENT for val in max_k : NEW_LINE INDENT maxProduct *= val NEW_LINE DEDENT print ( " Sum ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is " , minSum ) NEW_LINE print ( " Sum ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is " , maxSum ) NEW_LINE print ( " Product ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is " , minProduct ) NEW_LINE print ( " Product ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is " , maxProduct ) NEW_LINE DEDENT arr = [ 4 , 2 , 12 , 13 , 5 , 19 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE primeSumAndProduct ( arr , n , k ) NEW_LINE


class GFG { static class Data { double x , y ; public Data ( double x , double y ) { super ( ) ; this . x = x ; this . y = y ; } } ; static double inv_interpolate ( Data [ ] d , int n , double y ) { double x = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { double xi = d [ i ] . x ; for ( j = 0 ; j < n ; j ++ ) { if ( j != i ) { xi = xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ; } } x += xi ; } return x ; } public static void main ( String [ ] args ) { Data [ ] d = { new Data ( 1.27 , 2.3 ) , new Data ( 2.25 , 2.95 ) , new Data ( 2.5 , 3.5 ) , new Data ( 3.6 , 5.1 ) } ; int n = 4 ; double y = 4.5 ; System . out . printf ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : ▁ % .5f " , inv_interpolate ( d , n , y ) ) ; } }

class Data : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def inv_interpolate ( d : list , n : int , y : float ) -> float : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xi = d [ i ] . x NEW_LINE for j in range ( n ) : NEW_LINE INDENT if j != i : NEW_LINE INDENT xi = ( xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ) NEW_LINE DEDENT DEDENT x += xi NEW_LINE DEDENT return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT d = [ Data ( 1.27 , 2.3 ) , Data ( 2.25 , 2.95 ) , Data ( 2.5 , 3.5 ) , Data ( 3.6 , 5.1 ) ] NEW_LINE n = 4 NEW_LINE y = 4.5 NEW_LINE print ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : " , round ( inv_interpolate ( d , n , y ) , 5 ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int nx = 0 , ny = 0 ; static boolean check ( String s , int p , int q ) { for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) == ' x ' ) nx ++ ; else ny ++ ; } if ( ( nx * ny ) != ( p + q ) ) return true ; else return false ; } public static String smallestPermutation ( String s , int p , int q ) { if ( check ( s , p , q ) == true ) { return " Impossible " ; } char tempArray [ ] = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; String str = new String ( tempArray ) ; int a = nx * ny , b = 0 , i = 0 , j = 0 ; if ( a == p && b == q ) { return str ; } while ( 1 > 0 ) { for ( i = 0 ; i < str . length ( ) - 1 ; ++ i ) { if ( str . charAt ( i ) == ' x ' && str . charAt ( i + 1 ) == ' y ' ) break ; } for ( j = i ; j < str . length ( ) - 1 ; j ++ ) { if ( str . charAt ( j ) == ' x ' && str . charAt ( j + 1 ) == ' y ' ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setCharAt ( j , str . charAt ( j + 1 ) ) ; sb . setCharAt ( j + 1 , str . charAt ( j ) ) ; str = sb . toString ( ) ; a -- ; b ++ ; if ( a == p && b == q ) { return str ; } } } } } public static void main ( String [ ] args ) { String s = " yxxyx " ; int p = 3 , q = 3 ; System . out . print ( smallestPermutation ( s , p , q ) ) ; } }

def check ( s , p , q ) : NEW_LINE INDENT global nx NEW_LINE global ny NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if s [ i ] == ' x ' : NEW_LINE INDENT nx += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ny += 1 NEW_LINE DEDENT DEDENT if nx * ny != p + q : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def smallestPermutation ( s , p , q ) : NEW_LINE INDENT if check ( s , p , q ) == 1 : NEW_LINE INDENT return " Impossible " NEW_LINE DEDENT s = sorted ( s ) NEW_LINE a , b , i = nx * ny , 0 , 0 NEW_LINE if a == p and b == q : NEW_LINE INDENT return ' ' . join ( s ) NEW_LINE DEDENT while True : NEW_LINE INDENT for i in range ( 0 , len ( s ) - 1 ) : NEW_LINE INDENT if s [ i ] == ' x ' and s [ i + 1 ] == ' y ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( i , len ( s ) - 1 ) : NEW_LINE INDENT if s [ j ] == ' x ' and s [ j + 1 ] == ' y ' : NEW_LINE INDENT s [ j ] , s [ j + 1 ] = s [ j + 1 ] , s [ j ] NEW_LINE a -= 1 NEW_LINE b += 1 NEW_LINE if a == p and b == q : NEW_LINE INDENT return ' ' . join ( s ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT nx , ny = 0 , 0 NEW_LINE s = " yxxyx " NEW_LINE p , q = 3 , 3 NEW_LINE print ( smallestPermutation ( s , p , q ) ) NEW_LINE DEDENT