import java . io . * ; class GFG { static int M = 4 ; static int arr [ ] [ ] = { { 1 , 7 , 3 , 4 } , { 4 , 2 , 5 , 1 } , { 9 , 5 , 1 , 8 } } ; static void sort ( int a [ ] [ ] , int row , int n ) { for ( int i = 0 ; i < M - 1 ; i ++ ) { if ( a [ row ] [ i ] > a [ row ] [ i + 1 ] ) { int temp = a [ row ] [ i ] ; a [ row ] [ i ] = a [ row ] [ i + 1 ] ; a [ row ] [ i + 1 ] = temp ; } } } static int maximumSum ( int a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a , i , n ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev ) { prev = a [ i ] [ j ] ; sum += prev ; break ; } } if ( j == - 1 ) return 0 ; } return sum ; } public static void main ( String args [ ] ) { int n = arr . length ; System . out . print ( maximumSum ( arr , n ) ) ; } }

M = 4 ; NEW_LINE def maximumSum ( a , n ) : NEW_LINE INDENT global M ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT a [ i ] . sort ( ) ; NEW_LINE DEDENT sum = a [ n - 1 ] [ M - 1 ] ; NEW_LINE prev = a [ n - 1 ] [ M - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( M - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] [ j ] < prev ) : NEW_LINE INDENT prev = a [ i ] [ j ] ; NEW_LINE sum += prev ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( j == - 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT arr = [ [ 1 , 7 , 3 , 4 ] , [ 4 , 2 , 5 , 1 ] , [ 9 , 5 , 1 , 8 ] ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maximumSum ( arr , n ) ) ; NEW_LINE


import java . util . * ; class solution { static boolean isDivisibleRec ( int x , int a , int b , int n ) { if ( x > n ) return false ; if ( n % x == 0 ) return true ; return ( isDivisibleRec ( x * 10 + a , a , b , n ) || isDivisibleRec ( x * 10 + b , a , b , n ) ) ; } static boolean isDivisible ( int a , int b , int n ) { return isDivisibleRec ( a , a , b , n ) || isDivisibleRec ( b , a , b , n ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 5 , n = 53 ; if ( isDivisible ( a , b , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isDivisibleRec ( x , a , b , n ) : NEW_LINE INDENT if ( x > n ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % x == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return ( isDivisibleRec ( x * 10 + a , a , b , n ) or isDivisibleRec ( x * 10 + b , a , b , n ) ) NEW_LINE DEDENT def isDivisible ( a , b , n ) : NEW_LINE INDENT return ( isDivisibleRec ( a , a , b , n ) or isDivisibleRec ( b , a , b , n ) ) NEW_LINE DEDENT a = 3 ; b = 5 ; n = 53 ; NEW_LINE if ( isDivisible ( a , b , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GfG { static int countWays ( int N ) { int E = ( N * ( N - 1 ) ) / 2 ; if ( N == 1 ) return 0 ; return ( int ) Math . pow ( 2 , E - 1 ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( countWays ( N ) ) ; } }

def countWays ( N ) : NEW_LINE INDENT E = ( N * ( N - 1 ) ) / 2 NEW_LINE if ( N == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return int ( pow ( 2 , E - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( countWays ( N ) ) NEW_LINE DEDENT


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

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


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static long repeatedSum ( long n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } public static long repeatedProduct ( long n ) { long prod = 1 ; while ( n > 0 || prod > 9 ) { if ( n == 0 ) { n = prod ; prod = 1 ; } prod *= n % 10 ; n /= 10 ; } return prod ; } public static long maxSumProduct ( long N ) { if ( N < 10 ) return N ; return Math . max ( repeatedSum ( N ) , repeatedProduct ( N ) ) ; } public static void main ( String [ ] args ) { long n = 631 ; System . out . println ( maxSumProduct ( n ) ) ; } }

def repeatedSum ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 9 if ( n % 9 == 0 ) else ( n % 9 ) NEW_LINE DEDENT def repeatedProduct ( n ) : NEW_LINE INDENT prod = 1 NEW_LINE while ( n > 0 or prod > 9 ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT n = prod NEW_LINE prod = 1 NEW_LINE DEDENT prod *= n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return prod NEW_LINE DEDENT def maxSumProduct ( N ) : NEW_LINE INDENT if ( N < 10 ) : NEW_LINE INDENT return N NEW_LINE DEDENT return max ( repeatedSum ( N ) , repeatedProduct ( N ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 631 NEW_LINE print ( maxSumProduct ( n ) ) NEW_LINE DEDENT


class GFG { static double dist ( double m , double b1 , double b2 ) { double d = Math . abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; return d ; } public static void main ( String [ ] args ) { double m = 2 , b1 = 4 , b2 = 3 ; System . out . println ( dist ( m , b1 , b2 ) ) ; } }

def dist ( m , b1 , b2 ) : NEW_LINE INDENT d = abs ( b2 - b1 ) / ( ( m * m ) - 1 ) ; NEW_LINE return d ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT m , b1 , b2 = 2 , 4 , 3 ; NEW_LINE print ( dist ( m , b1 , b2 ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int totalOperations ( String str , int len ) { HashMap < Character , Integer > h = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( h . containsKey ( str . charAt ( i ) ) ) h . put ( str . charAt ( i ) , h . get ( str . charAt ( i ) ) + 1 ) ; else h . put ( str . charAt ( i ) , 1 ) ; } Set < Map . Entry < Character , Integer > > set = h . entrySet ( ) ; HashSet < Integer > hs = new HashSet < Integer > ( ) ; for ( Map . Entry < Character , Integer > me : set ) hs . add ( me . getValue ( ) ) ; return hs . size ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . println ( totalOperations ( str , len ) ) ; } }

def totalOperations ( st , length ) : NEW_LINE INDENT d = { } NEW_LINE for i in range ( length ) : NEW_LINE INDENT if st [ i ] in d : NEW_LINE INDENT d [ st [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT d [ st [ i ] ] = 1 NEW_LINE DEDENT DEDENT valueSet = set ( ) NEW_LINE for key in d . keys ( ) : NEW_LINE INDENT valueSet . add ( d [ key ] ) NEW_LINE DEDENT return len ( valueSet ) NEW_LINE DEDENT st = " geeksforgeeks " NEW_LINE l = len ( st ) NEW_LINE print ( totalOperations ( st , l ) ) NEW_LINE


class ProductArray { void productArray ( int arr [ ] , int n ) { if ( n == 1 ) { System . out . print ( 0 ) ; return ; } int left [ ] = new int [ n ] ; int right [ ] = new int [ n ] ; int prod [ ] = new int [ n ] ; int i , j ; left [ 0 ] = 1 ; right [ n - 1 ] = 1 ; for ( i = 1 ; i < n ; i ++ ) left [ i ] = arr [ i - 1 ] * left [ i - 1 ] ; for ( j = n - 2 ; j >= 0 ; j -- ) right [ j ] = arr [ j + 1 ] * right [ j + 1 ] ; for ( i = 0 ; i < n ; i ++ ) prod [ i ] = left [ i ] * right [ i ] ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( prod [ i ] + " ▁ " ) ; return ; } public static void main ( String [ ] args ) { ProductArray pa = new ProductArray ( ) ; int arr [ ] = { 10 , 3 , 5 , 6 , 2 } ; int n = arr . length ; System . out . println ( " The ▁ product ▁ array ▁ is ▁ : ▁ " ) ; pa . productArray ( arr , n ) ; } }

def productArray ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( 0 ) NEW_LINE return NEW_LINE DEDENT left = [ 0 ] * n NEW_LINE right = [ 0 ] * n NEW_LINE prod = [ 0 ] * n NEW_LINE left [ 0 ] = 1 NEW_LINE right [ n - 1 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT left [ i ] = arr [ i - 1 ] * left [ i - 1 ] NEW_LINE DEDENT for j in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT right [ j ] = arr [ j + 1 ] * right [ j + 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT prod [ i ] = left [ i ] * right [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( prod [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 10 , 3 , 5 , 6 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ product ▁ array ▁ is : " ) NEW_LINE productArray ( arr , n ) NEW_LINE


class GFG { static long calculateSum ( int n , int k ) { long res = 1 ; long MOD = 1000000007 ; for ( int i = 0 ; i < k ; i ++ ) { res = ( res * n ) % MOD ; } return res ; } public static void main ( String [ ] args ) { int n = 4 ; int k = 3 ; System . out . print ( calculateSum ( n , k ) ) ; } } ;

def calculateSum ( n , k ) : NEW_LINE INDENT res = 1 NEW_LINE MOD = 1000000007 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT res = ( res * n ) % MOD NEW_LINE DEDENT return res NEW_LINE DEDENT n = 4 NEW_LINE k = 3 NEW_LINE print ( calculateSum ( n , k ) ) NEW_LINE


import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printAllDivisors ( int arr [ ] , int N ) { int g = arr [ 0 ] ; HashSet < Integer > divisors = new HashSet < Integer > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { g = gcd ( arr [ i ] , g ) ; } for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { divisors . add ( i ) ; if ( g / i != i ) divisors . add ( g / i ) ; } } for ( int it : divisors ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 90 , 12 , 18 , 30 , 24 } ; int n = arr . length ; printAllDivisors ( arr , n ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def printAllDivisors ( arr , N ) : NEW_LINE INDENT g = arr [ 0 ] NEW_LINE divisors = dict ( ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT g = gcd ( arr [ i ] , g ) NEW_LINE DEDENT for i in range ( 1 , g + 1 ) : NEW_LINE INDENT if i * i > g : NEW_LINE INDENT break NEW_LINE DEDENT if ( g % i == 0 ) : NEW_LINE INDENT divisors [ i ] = 1 NEW_LINE if ( g // i != i ) : NEW_LINE INDENT divisors [ g // i ] = 1 NEW_LINE DEDENT DEDENT DEDENT for it in sorted ( divisors ) : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 90 , 12 , 18 , 30 , 24 ] NEW_LINE n = len ( arr ) NEW_LINE printAllDivisors ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 100005 ; static Set < Integer > ABUNDANT ( ) { Set < Integer > v = new HashSet < > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { int sum = 1 ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum += j ; if ( i / j != j ) { sum += i / j ; } } } if ( sum > i ) { v . add ( i ) ; } } return v ; } static void SumOfAbundant ( int n ) { Set < Integer > v = ABUNDANT ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { if ( v . contains ( i ) & v . contains ( n - i ) ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; return ; } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 24 ; SumOfAbundant ( n ) ; } }

from math import sqrt NEW_LINE N = 100005 NEW_LINE def ABUNDANT ( ) : NEW_LINE INDENT v = set ( ) ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT sum = 1 NEW_LINE for j in range ( 2 , int ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT if ( i / j != j ) : NEW_LINE INDENT sum += i // j NEW_LINE DEDENT DEDENT if ( sum > i ) : NEW_LINE INDENT v . add ( i ) NEW_LINE DEDENT DEDENT return v NEW_LINE DEDENT def SumOfAbundant ( n ) : NEW_LINE INDENT v = ABUNDANT ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( list ( v ) . count ( i ) and list ( v ) . count ( n - i ) ) : NEW_LINE INDENT print ( i , " ▁ " , n - i ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 24 NEW_LINE SumOfAbundant ( n ) NEW_LINE DEDENT


import java . util . * ; import java . util . concurrent . ConcurrentHashMap ; class GFG { static int getMinCost ( int arr [ ] , int n , int cost [ ] ) { Map < Integer , Integer > mp = new ConcurrentHashMap < Integer , Integer > ( ) ; mp . clear ( ) ; mp . put ( 0 , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { int gcd = __gcd ( arr [ i ] , it . getKey ( ) ) ; if ( mp . containsKey ( gcd ) ) mp . put ( gcd , Math . min ( mp . get ( gcd ) , it . getValue ( ) + cost [ i ] ) ) ; else mp . put ( gcd , it . getValue ( ) + cost [ i ] ) ; } } if ( ! mp . containsKey ( 1 ) ) return - 1 ; else return mp . get ( 1 ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 12 , 1 } ; int cost [ ] = { 2 , 1 , 2 , 6 } ; int n = arr . length ; System . out . print ( getMinCost ( arr , n , cost ) ) ; } }

from math import gcd as __gcd NEW_LINE def getMinCost ( arr , n , cost ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE mp [ 0 ] = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for it in list ( mp ) : NEW_LINE INDENT gcd = __gcd ( arr [ i ] , it ) NEW_LINE if ( gcd in mp ) : NEW_LINE INDENT mp [ gcd ] = min ( mp [ gcd ] , mp [ it ] + cost [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT mp [ gcd ] = mp [ it ] + cost [ i ] NEW_LINE DEDENT DEDENT DEDENT if ( mp [ 1 ] == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return mp [ 1 ] NEW_LINE DEDENT DEDENT arr = [ 5 , 10 , 12 , 1 ] NEW_LINE cost = [ 2 , 1 , 2 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMinCost ( arr , n , cost ) ) NEW_LINE


import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; public class Queue_reverse { static Queue < Integer > queue ; static void Print ( ) { while ( ! queue . isEmpty ( ) ) { System . out . print ( queue . peek ( ) + " ▁ " ) ; queue . remove ( ) ; } } static Queue < Integer > reverseQueue ( Queue < Integer > q ) { if ( q . isEmpty ( ) ) return q ; int data = q . peek ( ) ; q . remove ( ) ; q = reverseQueue ( q ) ; q . add ( data ) ; return q ; } public static void main ( String args [ ] ) { queue = new LinkedList < Integer > ( ) ; queue . add ( 56 ) ; queue . add ( 27 ) ; queue . add ( 30 ) ; queue . add ( 45 ) ; queue . add ( 85 ) ; queue . add ( 92 ) ; queue . add ( 58 ) ; queue . add ( 80 ) ; queue . add ( 90 ) ; queue . add ( 100 ) ; queue = reverseQueue ( queue ) ; Print ( ) ; } }

class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . items = [ ] NEW_LINE DEDENT def isEmpty ( self ) : NEW_LINE INDENT return self . items == [ ] NEW_LINE DEDENT def add ( self , item ) : NEW_LINE INDENT self . items . append ( item ) NEW_LINE DEDENT def pop ( self ) : NEW_LINE INDENT return self . items . pop ( 0 ) NEW_LINE DEDENT def front ( self ) : NEW_LINE INDENT return self . items [ 0 ] NEW_LINE DEDENT def printQueue ( self ) : NEW_LINE INDENT for i in self . items : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT def reverseQueue ( q ) : NEW_LINE INDENT if ( q . isEmpty ( ) ) : NEW_LINE INDENT return NEW_LINE DEDENT data = q . front ( ) ; NEW_LINE q . pop ( ) ; NEW_LINE reverseQueue ( q ) NEW_LINE q . add ( data ) NEW_LINE DEDENT q = Queue ( ) NEW_LINE q . add ( 56 ) NEW_LINE q . add ( 27 ) NEW_LINE q . add ( 30 ) NEW_LINE q . add ( 45 ) NEW_LINE q . add ( 85 ) NEW_LINE q . add ( 92 ) NEW_LINE q . add ( 58 ) NEW_LINE q . add ( 80 ) NEW_LINE q . add ( 90 ) NEW_LINE q . add ( 100 ) NEW_LINE reverseQueue ( q ) NEW_LINE q . printQueue ( ) NEW_LINE


class GFG { static boolean checkcomposite ( int n ) { for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return true ; } } return false ; } static int power ( int x , int y , int mod ) { int res = 1 ; while ( y != 0 ) { if ( ( y & 1 ) == 1 ) { res = ( res * x ) % mod ; } y = y >> 1 ; x = ( x * x ) % mod ; } return res ; } static int Check ( int n , int a ) { if ( a > 1 && checkcomposite ( n ) && power ( a , n - 1 , n ) == 1 ) { return 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 645 ; int a = 2 ; System . out . println ( Check ( N , a ) ) ; } }

from math import sqrt NEW_LINE def checkcomposite ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def power ( x , y , mod ) : NEW_LINE INDENT res = 1 NEW_LINE while ( y ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % mod NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % mod NEW_LINE DEDENT return res NEW_LINE DEDENT def Check ( n , a ) : NEW_LINE INDENT if ( a > 1 and checkcomposite ( n ) and power ( a , n - 1 , n ) == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 645 NEW_LINE a = 2 NEW_LINE print ( Check ( N , a ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void reverse ( int a [ ] ) { int i , k , n = a . length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; Arrays . sort ( arr ) ; reverse ( arr ) ; System . out . println ( " Array ▁ after ▁ sorting ▁ : ▁ \n " ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }

if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE arr . sort ( reverse = True ) NEW_LINE print ( " Array ▁ after ▁ sorting ▁ : ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int maxProduct ( int arr [ ] , int n ) { int ans = - 1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] < arr [ j ] && arr [ j ] > arr [ k ] ) ans = Math . max ( ans , arr [ i ] * arr [ j ] * arr [ k ] ) ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 8 , 3 , 7 } ; int n = arr . length ; System . out . print ( maxProduct ( arr , n ) + " \n " ) ; } }

def maxProduct ( arr , n ) : NEW_LINE INDENT ans = - 1 NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] and arr [ j ] > arr [ k ] ) : NEW_LINE INDENT ans = max ( ans , arr [ i ] * arr [ j ] * arr [ k ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 8 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxProduct ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int [ ] compute_lps ( String s ) { int n = s . length ( ) ; int [ ] lps = new int [ n ] ; int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) len = lps [ len - 1 ] ; else { lps [ i ] = 0 ; i ++ ; } } } return lps ; } static void Longestsubstring ( String s ) { int [ ] lps = compute_lps ( s ) ; int n = s . length ( ) ; if ( lps [ n - 1 ] == 0 ) { System . out . println ( - 1 ) ; return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( lps [ i ] == lps [ n - 1 ] ) { System . out . println ( s . substring ( 0 , lps [ i ] ) ) ; return ; } } if ( lps [ lps [ n - 1 ] - 1 ] == 0 ) System . out . println ( - 1 ) ; else System . out . println ( s . substring ( 0 , lps [ lps [ n - 1 ] - 1 ] ) ) ; } public static void main ( String [ ] args ) { String s = " fixprefixsuffix " ; Longestsubstring ( s ) ; } }

def compute_lps ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE lps = [ 0 for i in range ( n ) ] NEW_LINE Len = 0 NEW_LINE lps [ 0 ] = 0 NEW_LINE i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( s [ i ] == s [ Len ] ) : NEW_LINE INDENT Len += 1 NEW_LINE lps [ i ] = Len NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( Len != 0 ) : NEW_LINE INDENT Len = lps [ Len - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lps [ i ] = 0 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT DEDENT return lps NEW_LINE DEDENT def Longestsubstring ( s ) : NEW_LINE INDENT lps = compute_lps ( s ) NEW_LINE n = len ( s ) NEW_LINE if ( lps [ n - 1 ] == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE exit ( ) NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( lps [ i ] == lps [ n - 1 ] ) : NEW_LINE INDENT print ( s [ 0 : lps [ i ] ] ) NEW_LINE exit ( ) NEW_LINE DEDENT DEDENT if ( lps [ lps [ n - 1 ] - 1 ] == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( s [ 0 : lps [ lps [ n - 1 ] - 1 ] ] ) NEW_LINE DEDENT DEDENT s = " fixprefixsuffix " NEW_LINE Longestsubstring ( s ) NEW_LINE


class GFG { static int convert0To5Rec ( int num ) { if ( num == 0 ) return 0 ; int digit = num % 10 ; if ( digit == 0 ) digit = 5 ; return convert0To5Rec ( num / 10 ) * 10 + digit ; } static int convert0To5 ( int num ) { if ( num == 0 ) return 5 ; else return convert0To5Rec ( num ) ; } public static void main ( String [ ] args ) { System . out . println ( convert0To5 ( 10120 ) ) ; } }

def convert0to5rec ( num ) : NEW_LINE INDENT if num == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT digit = num % 10 NEW_LINE if digit == 0 : NEW_LINE INDENT digit = 5 NEW_LINE DEDENT return convert0to5rec ( num / 10 ) * 10 + digit NEW_LINE DEDENT def convert0to5 ( num ) : NEW_LINE INDENT if num == 0 : NEW_LINE INDENT return 5 NEW_LINE DEDENT else : NEW_LINE INDENT return convert0to5rec ( num ) NEW_LINE DEDENT DEDENT num = 10120 NEW_LINE print convert0to5 ( num ) NEW_LINE


class GFG { static String FirstAndLast ( String str ) { char [ ] ch = str . toCharArray ( ) ; for ( int i = 0 ; i < ch . length ; i ++ ) { int k = i ; while ( i < ch . length && ch [ i ] != ' ▁ ' ) i ++ ; ch [ k ] = ( char ) ( ch [ k ] >= ' a ' && ch [ k ] <= ' z ' ? ( ( int ) ch [ k ] - 32 ) : ( int ) ch [ k ] ) ; ch [ i - 1 ] = ( char ) ( ch [ i - 1 ] >= ' a ' && ch [ i - 1 ] <= ' z ' ? ( ( int ) ch [ i - 1 ] - 32 ) : ( int ) ch [ i - 1 ] ) ; } return new String ( ch ) ; } public static void main ( String args [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; System . out . println ( str ) ; System . out . println ( FirstAndLast ( str ) ) ; } }

def FirstAndLast ( string ) : NEW_LINE INDENT ch = list ( string ) ; NEW_LINE i = 0 ; NEW_LINE while i < len ( ch ) : NEW_LINE INDENT k = i ; NEW_LINE while ( i < len ( ch ) and ch [ i ] != ' ▁ ' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( ord ( ch [ k ] ) >= 97 and ord ( ch [ k ] ) <= 122 ) : NEW_LINE INDENT ch [ k ] = chr ( ord ( ch [ k ] ) - 32 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ch [ k ] = ch [ k ] NEW_LINE DEDENT if ( ord ( ch [ i - 1 ] ) >= 90 and ord ( ch [ i - 1 ] ) <= 122 ) : NEW_LINE INDENT ch [ i - 1 ] = chr ( ord ( ch [ i - 1 ] ) - 32 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ch [ i - 1 ] = ch [ i - 1 ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return " " . join ( ch ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " Geeks ▁ for ▁ Geeks " ; NEW_LINE print ( string ) ; NEW_LINE print ( FirstAndLast ( string ) ) ; NEW_LINE DEDENT


import java . util . * ; class GfG { static int longLenStrictBitonicSub ( int arr [ ] , int n ) { HashMap < Integer , Integer > inc = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > dcr = new HashMap < Integer , Integer > ( ) ; int len_inc [ ] = new int [ n ] ; int len_dcr [ ] = new int [ n ] ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( inc . containsKey ( arr [ i ] - 1 ) ) len = inc . get ( arr [ i ] - 1 ) ; len_inc [ i ] = len + 1 ; inc . put ( arr [ i ] , len_inc [ i ] ) ; } for ( int i = n - 1 ; i >= 0 ; i -- ) { int len = 0 ; if ( dcr . containsKey ( arr [ i ] - 1 ) ) len = dcr . get ( arr [ i ] - 1 ) ; len_dcr [ i ] = len + 1 ; dcr . put ( arr [ i ] , len_dcr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) if ( longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) ) longLen = len_inc [ i ] + len_dcr [ i ] - 1 ; return longLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 } ; int n = arr . length ; System . out . println ( " Longest ▁ length ▁ strict ▁ " + " bitonic ▁ subsequence ▁ = ▁ " + longLenStrictBitonicSub ( arr , n ) ) ; } }

def longLenStrictBitonicSub ( arr , n ) : NEW_LINE INDENT inc , dcr = dict ( ) , dict ( ) NEW_LINE len_inc , len_dcr = [ 0 ] * n , [ 0 ] * n NEW_LINE longLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT len = 0 NEW_LINE if inc . get ( arr [ i ] - 1 ) in inc . values ( ) : NEW_LINE INDENT len = inc . get ( arr [ i ] - 1 ) NEW_LINE DEDENT inc [ arr [ i ] ] = len_inc [ i ] = len + 1 NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT len = 0 NEW_LINE if dcr . get ( arr [ i ] - 1 ) in dcr . values ( ) : NEW_LINE INDENT len = dcr . get ( arr [ i ] - 1 ) NEW_LINE DEDENT dcr [ arr [ i ] ] = len_dcr [ i ] = len + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if longLen < ( len_inc [ i ] + len_dcr [ i ] - 1 ) : NEW_LINE INDENT longLen = len_inc [ i ] + len_dcr [ i ] - 1 NEW_LINE DEDENT DEDENT return longLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 2 , 3 , 4 , 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Longest ▁ length ▁ strict ▁ bitonic ▁ subsequence ▁ = " , longLenStrictBitonicSub ( arr , n ) ) NEW_LINE DEDENT


class GFG { static class node { int data ; node next ; } static node add ( int data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static void printArr ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } static int findlength ( node head ) { node curr = head ; int cnt = 0 ; while ( curr != null ) { cnt ++ ; curr = curr . next ; } return cnt ; } static void convertArr ( node head ) { int len = findlength ( head ) ; int [ ] arr = new int [ len ] ; int index = 0 ; node curr = head ; while ( curr != null ) { arr [ index ++ ] = curr . data ; curr = curr . next ; } printArr ( arr , len ) ; } public static void main ( String [ ] args ) { node head = new node ( ) ; head = add ( 1 ) ; head . next = add ( 2 ) ; head . next . next = add ( 3 ) ; head . next . next . next = add ( 4 ) ; head . next . next . next . next = add ( 5 ) ; convertArr ( head ) ; } }

class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def add ( data ) : NEW_LINE INDENT newnode = node ( 0 ) NEW_LINE newnode . data = data NEW_LINE newnode . next = None NEW_LINE return newnode NEW_LINE DEDENT def printArr ( a , n ) : NEW_LINE INDENT i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT def findlength ( head ) : NEW_LINE INDENT curr = head NEW_LINE cnt = 0 NEW_LINE while ( curr != None ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE curr = curr . next NEW_LINE DEDENT return cnt NEW_LINE DEDENT def convertArr ( head ) : NEW_LINE INDENT len1 = findlength ( head ) NEW_LINE arr = [ ] NEW_LINE index = 0 NEW_LINE curr = head NEW_LINE while ( curr != None ) : NEW_LINE INDENT arr . append ( curr . data ) NEW_LINE curr = curr . next NEW_LINE DEDENT printArr ( arr , len1 ) NEW_LINE DEDENT head = node ( 0 ) NEW_LINE head = add ( 1 ) NEW_LINE head . next = add ( 2 ) NEW_LINE head . next . next = add ( 3 ) NEW_LINE head . next . next . next = add ( 4 ) NEW_LINE head . next . next . next . next = add ( 5 ) NEW_LINE convertArr ( head ) NEW_LINE


import java . util . * ; class geeks { public static int minOperations ( int [ ] a , int n , int K ) { HashMap < Integer , Boolean > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { try { if ( map . get ( a [ i ] ) ) return 1 ; } catch ( Exception e ) { } try { map . put ( a [ i ] , true ) ; } catch ( Exception e ) { } } 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 ] ) { try { map . put ( b [ i ] , true ) ; } catch ( Exception e ) { } } } for ( int i = 0 ; i < n ; i ++ ) { try { if ( map . get ( a [ i ] ) ) return 1 ; } catch ( Exception e ) { } } map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { try { if ( map . get ( b [ i ] ) ) return 2 ; } catch ( Exception e ) { } try { map . put ( b [ i ] , true ) ; } catch ( Exception e ) { } } return - 1 ; } public static void main ( String [ ] args ) { int K = 3 ; int [ ] a = { 1 , 2 , 3 , 7 } ; int n = a . length ; System . out . println ( minOperations ( a , n , K ) ) ; } }

from collections import defaultdict NEW_LINE def minOperations ( a , n , K ) : NEW_LINE INDENT Map = defaultdict ( lambda : False ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if Map [ a [ i ] ] == True : NEW_LINE INDENT return 0 NEW_LINE DEDENT Map [ a [ i ] ] = True NEW_LINE DEDENT b = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT b . append ( a [ i ] & K ) NEW_LINE DEDENT Map . clear ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if a [ i ] != b [ i ] : NEW_LINE INDENT Map [ b [ i ] ] = True NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if Map [ a [ i ] ] == True : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT Map . clear ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if Map [ b [ i ] ] == True : 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 , 2 , 3 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( minOperations ( a , n , K ) ) NEW_LINE DEDENT


class GFG { static boolean allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( num == new_num ) return true ; return false ; } static boolean bitsAreComplement ( int a , int b , int l , int r ) { int xor_value = a ^ b ; return allBitsSetInTheGivenRange ( xor_value , l , r ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 5 ; int l = 1 , r = 3 ; if ( bitsAreComplement ( a , b , l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def allBitsSetInTheGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE new_num = n & num NEW_LINE if ( num == new_num ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def bitsAreComplement ( a , b , l , r ) : NEW_LINE INDENT xor_value = a ^ b NEW_LINE return allBitsSetInTheGivenRange ( xor_value , l , r ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 10 NEW_LINE b = 5 NEW_LINE l = 1 NEW_LINE r = 3 NEW_LINE if ( bitsAreComplement ( a , b , l , r ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


public class GFG { static void equation_plane ( int x1 , int y1 , int z1 , int x2 , int y2 , int z2 , int x3 , int y3 , int z3 , int x , int y , int z ) { int a1 = x2 - x1 ; int b1 = y2 - y1 ; int c1 = z2 - z1 ; int a2 = x3 - x1 ; int b2 = y3 - y1 ; int c2 = z3 - z1 ; int a = b1 * c2 - b2 * c1 ; int b = a2 * c1 - a1 * c2 ; int c = a1 * b2 - b1 * a2 ; int d = ( - a * x1 - b * y1 - c * z1 ) ; if ( a * x + b * y + c * z + d == 0 ) System . out . println ( " Coplanar " ) ; else System . out . println ( " Not ▁ Coplanar " ) ; } public static void main ( String [ ] args ) { int x1 = 3 ; int y1 = 2 ; int z1 = - 5 ; int x2 = - 1 ; int y2 = 4 ; int z2 = - 3 ; int x3 = - 3 ; int y3 = 8 ; int z3 = - 5 ; int x4 = - 3 ; int y4 = 2 ; int z4 = 1 ; equation_plane ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 , x4 , y4 , z4 ) ; } }

def equation_plane ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 , x , y , z ) : NEW_LINE INDENT a1 = x2 - x1 NEW_LINE b1 = y2 - y1 NEW_LINE c1 = z2 - z1 NEW_LINE a2 = x3 - x1 NEW_LINE b2 = y3 - y1 NEW_LINE c2 = z3 - z1 NEW_LINE a = b1 * c2 - b2 * c1 NEW_LINE b = a2 * c1 - a1 * c2 NEW_LINE c = a1 * b2 - b1 * a2 NEW_LINE d = ( - a * x1 - b * y1 - c * z1 ) NEW_LINE if ( a * x + b * y + c * z + d == 0 ) : NEW_LINE INDENT print ( " Coplanar " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Coplanar " ) NEW_LINE DEDENT DEDENT x1 = 3 NEW_LINE y1 = 2 NEW_LINE z1 = - 5 NEW_LINE x2 = - 1 NEW_LINE y2 = 4 NEW_LINE z2 = - 3 NEW_LINE x3 = - 3 NEW_LINE y3 = 8 NEW_LINE z3 = - 5 NEW_LINE x4 = - 3 NEW_LINE y4 = 2 NEW_LINE z4 = 1 NEW_LINE equation_plane ( x1 , y1 , z1 , x2 , y2 , z2 , x3 , y3 , z3 , x4 , y4 , z4 ) NEW_LINE


import java . io . * ; class GFG { static int count9s ( String number ) { int count = 0 ; int n = number . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int sum = number . charAt ( i ) - '0' ; if ( number . charAt ( i ) == '9' ) count ++ ; for ( int j = i + 1 ; j < n ; j ++ ) { sum = ( sum + number . charAt ( j ) - '0' ) % 9 ; if ( sum == 0 ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { System . out . println ( count9s ( "4189" ) ) ; System . out . println ( count9s ( "1809" ) ) ; } }

def count9s ( number ) : NEW_LINE INDENT count = 0 NEW_LINE n = len ( number ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = ord ( number [ i ] ) - ord ( '0' ) NEW_LINE if ( number [ i ] == '9' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = ( sum + ord ( number [ j ] ) - ord ( '0' ) ) % 9 NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( count9s ( "4189" ) ) NEW_LINE print ( count9s ( "1809" ) ) NEW_LINE DEDENT


class GFG { static int finalNum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result = __gcd ( result , arr [ i ] ) ; } return result ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 6 , 36 } ; int n = arr . length ; System . out . print ( finalNum ( arr , n ) ) ; } }

from math import gcd as __gcd NEW_LINE def finalNum ( arr , n ) : NEW_LINE INDENT result = arr [ 0 ] NEW_LINE for i in arr : NEW_LINE INDENT result = __gcd ( result , i ) NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 9 , 6 , 36 ] NEW_LINE n = len ( arr ) NEW_LINE print ( finalNum ( arr , n ) ) NEW_LINE


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

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


class GFG { static int checkBitonic ( int arr [ ] , int n ) { int i , j ; for ( i = 1 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) continue ; if ( arr [ i ] <= arr [ i - 1 ] ) break ; } if ( i == n - 1 ) return 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ j - 1 ] ) continue ; if ( arr [ j ] >= arr [ j - 1 ] ) break ; } i = j ; if ( i != n ) return 0 ; return 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { - 3 , 9 , 7 , 20 , 17 , 5 , 1 } ; int n = arr . length ; System . out . println ( ( checkBitonic ( arr , n ) == 1 ) ? " YES " : " NO " ) ; } }

def checkBitonic ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n ) : NEW_LINE INDENT if arr [ i ] > arr [ i - 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if i == n - 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ j ] < arr [ j - 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i = j NEW_LINE if i != n - 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 3 , 9 , 7 , 20 , 17 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE if checkBitonic ( arr , n ) == 1 : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . util . Arrays ; class GFG { static void longestSubsequence ( int a [ ] , int n , int q [ ] , int m ) { Arrays . sort ( a ) ; int sum = 0 ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; double av = ( double ) ( sum ) / ( double ) ( i + 1 ) ; b [ i ] = ( ( int ) ( av + 1 ) ) ; } Arrays . sort ( b ) ; for ( int i = 0 ; i < m ; i ++ ) { int k = q [ i ] ; int longest = upperBound ( b , 0 , n , k ) ; System . out . println ( " Answer ▁ to ▁ Query " + ( i + 1 ) + " : ▁ " + longest ) ; } } private static int upperBound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 5 , 4 } ; int n = a . length ; int q [ ] = { 4 , 2 , 1 , 5 } ; int m = q . length ; longestSubsequence ( a , n , q , m ) ; } }

import bisect NEW_LINE def longestSubsequence ( a , n , q , m ) : NEW_LINE INDENT a . sort ( ) NEW_LINE Sum = 0 NEW_LINE b = [ None ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Sum += a [ i ] NEW_LINE av = Sum // ( i + 1 ) NEW_LINE b [ i ] = av + 1 NEW_LINE DEDENT b . sort ( ) NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT k = q [ i ] NEW_LINE longest = bisect . bisect_right ( b , k ) NEW_LINE print ( " Answer ▁ to ▁ Query " , i + 1 , " : " , longest ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 2 , 5 , 4 ] NEW_LINE n = len ( a ) NEW_LINE q = [ 4 , 2 , 1 , 5 ] NEW_LINE m = len ( q ) NEW_LINE longestSubsequence ( a , n , q , m ) NEW_LINE DEDENT


class GFG { static boolean isPrime ( int N ) { for ( int i = 2 ; i <= Math . sqrt ( N ) ; i ++ ) { if ( N % i == 0 ) return false ; } return true ; } static int getDifference ( int N ) { if ( N == 0 ) return 2 ; else if ( N == 1 ) return 1 ; else if ( isPrime ( N ) ) return 0 ; int aboveN = - 1 , belowN = - 1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPrime ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPrime ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; return Math . min ( diff1 , diff2 ) ; } public static void main ( String args [ ] ) { int N = 25 ; System . out . println ( getDifference ( N ) ) ; } }

from math import sqrt NEW_LINE def isPrime ( N ) : NEW_LINE INDENT k = int ( sqrt ( N ) ) + 1 NEW_LINE for i in range ( 2 , k , 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def getDifference ( N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT elif ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( isPrime ( N ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT aboveN = - 1 NEW_LINE belowN = - 1 NEW_LINE n1 = N + 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( n1 ) ) : NEW_LINE INDENT aboveN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 += 1 NEW_LINE DEDENT DEDENT n1 = N - 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( n1 ) ) : NEW_LINE INDENT belowN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 -= 1 NEW_LINE DEDENT DEDENT diff1 = aboveN - N NEW_LINE diff2 = N - belowN NEW_LINE return min ( diff1 , diff2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE print ( getDifference ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int minimumMoves ( int k , int l , int r ) { int count = r - l + 1 ; if ( count % k == 0 ) return 0 ; return ( k - ( count % k ) ) ; } public static void main ( String [ ] args ) { int k = 3 , l = 10 , r = 10 ; System . out . print ( minimumMoves ( k , l , r ) ) ; } }

def minimumMoves ( k , l , r ) : NEW_LINE INDENT count = r - l + 1 NEW_LINE if ( count % k == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( k - ( count % k ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 3 NEW_LINE l = 10 NEW_LINE r = 10 NEW_LINE print ( minimumMoves ( k , l , r ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findMaxXOR ( Vector < Integer > arr , int n ) { Vector < Integer > stack = new Vector < Integer > ( ) ; int res = 0 , l = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { while ( ! stack . isEmpty ( ) && stack . get ( stack . size ( ) - 1 ) < arr . get ( i ) ) { stack . remove ( stack . size ( ) - 1 ) ; l -- ; } stack . add ( arr . get ( i ) ) ; l ++ ; if ( l > 1 ) { res = Math . max ( res , stack . get ( l - 1 ) ^ stack . get ( l - 2 ) ) ; } } return res ; } public static void main ( String [ ] args ) { Integer [ ] temp = { 9 , 8 , 3 , 5 , 7 } ; Vector < Integer > arr = new Vector < > ( Arrays . asList ( temp ) ) ; int result1 = findMaxXOR ( arr , 5 ) ; Collections . reverse ( arr ) ; int result2 = findMaxXOR ( arr , 5 ) ; System . out . print ( Math . max ( result1 , result2 ) ) ; } }

from collections import deque NEW_LINE def maxXOR ( arr ) : NEW_LINE INDENT stack = deque ( ) NEW_LINE l = 0 NEW_LINE res1 = 0 NEW_LINE for i in arr : NEW_LINE INDENT while stack and stack [ - 1 ] < i : NEW_LINE INDENT stack . pop ( ) NEW_LINE l -= 1 NEW_LINE DEDENT stack . append ( i ) NEW_LINE l += 1 NEW_LINE if l > 1 : NEW_LINE INDENT res1 = max ( res1 , stack [ - 1 ] ^ stack [ - 2 ] ) NEW_LINE DEDENT DEDENT res2 = 0 NEW_LINE stack . clear ( ) NEW_LINE l = 0 NEW_LINE arr . reverse ( ) NEW_LINE for i in arr : NEW_LINE INDENT while stack and stack [ - 1 ] < i : NEW_LINE INDENT stack . pop ( ) NEW_LINE l -= 1 NEW_LINE DEDENT stack . append ( i ) NEW_LINE l += 1 NEW_LINE if l > 1 : NEW_LINE INDENT res2 = max ( res2 , stack [ - 1 ] ^ stack [ - 2 ] ) NEW_LINE DEDENT DEDENT return max ( res1 , res2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 9 , 8 , 3 , 5 , 7 ] NEW_LINE print ( maxXOR ( arr ) ) NEW_LINE DEDENT


class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node closestNodeUsingMorrisTraversal ( Node root , int key ) { int diff = Integer . MAX_VALUE ; Node curr = root ; Node closest = null ; while ( curr != null ) { if ( curr . left == null ) { if ( diff > Math . abs ( curr . data - key ) ) { diff = Math . abs ( curr . data - key ) ; closest = curr ; } curr = curr . right ; } else { Node pre = curr . left ; while ( pre . right != null && pre . right != curr ) pre = pre . right ; if ( pre . right == null ) { pre . right = curr ; curr = curr . left ; } else { pre . right = null ; if ( diff > Math . abs ( curr . data - key ) ) { diff = Math . abs ( curr . data - key ) ; closest = curr ; } curr = curr . right ; } } } return closest ; } public static void main ( String [ ] args ) { Node root = newNode ( 5 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 9 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 2 ) ; root . right . left = newNode ( 8 ) ; root . right . right = newNode ( 12 ) ; System . out . println ( closestNodeUsingMorrisTraversal ( root , 10 ) . data ) ; } }

_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def closestNodeUsingMorrisTraversal ( root , key ) : NEW_LINE INDENT diff = _MAX NEW_LINE curr = root NEW_LINE closest = 0 NEW_LINE while ( curr ) : NEW_LINE INDENT if ( curr . left == None ) : NEW_LINE INDENT if ( diff > abs ( curr . data - key ) ) : NEW_LINE INDENT diff = abs ( curr . data - key ) NEW_LINE closest = curr NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT else : NEW_LINE INDENT pre = curr . left NEW_LINE while ( pre . right != None and pre . right != curr ) : NEW_LINE INDENT pre = pre . right NEW_LINE DEDENT if ( pre . right == None ) : NEW_LINE INDENT pre . right = curr NEW_LINE curr = curr . left NEW_LINE DEDENT else : NEW_LINE INDENT pre . right = None NEW_LINE if ( diff > abs ( curr . data - key ) ) : NEW_LINE INDENT diff = abs ( curr . data - key ) NEW_LINE closest = curr NEW_LINE DEDENT curr = curr . right NEW_LINE DEDENT DEDENT DEDENT return closest NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 9 ) NEW_LINE root . left . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . right . right = newNode ( 12 ) NEW_LINE root . right . left = newNode ( 8 ) NEW_LINE print ( closestNodeUsingMorrisTraversal ( root , 10 ) . data ) NEW_LINE DEDENT


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

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT if ( isPrime ( 11 ) ) : NEW_LINE INDENT print ( " ▁ true " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ false " ) NEW_LINE DEDENT if ( isPrime ( 15 ) ) : NEW_LINE INDENT print ( " ▁ true " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ false " ) NEW_LINE DEDENT


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int xor = arr [ 0 ] ; int set_bit_no ; int i ; int n = size - 2 ; int x = 0 , y = 0 ; for ( i = 1 ; i < size ; i ++ ) xor ^= arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) xor ^= i ; set_bit_no = ( xor & ~ ( xor - 1 ) ) ; for ( i = 0 ; i < size ; i ++ ) { int a = arr [ i ] & set_bit_no ; if ( a != 0 ) x = x ^ arr [ i ] ; else y = y ^ arr [ i ] ; } for ( i = 1 ; i <= n ; i ++ ) { int a = i & set_bit_no ; if ( a != 0 ) x = x ^ i ; else y = y ^ i ; } System . out . println ( " The ▁ two ▁ reppeated ▁ elements ▁ are ▁ : " ) ; System . out . println ( x + " ▁ " + y ) ; } 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 xor = arr [ 0 ] NEW_LINE n = size - 2 NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT xor ^= arr [ i ] NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT xor ^= i NEW_LINE DEDENT set_bit_no = xor & ~ ( xor - 1 ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT if ( arr [ i ] & set_bit_no ) : NEW_LINE INDENT x = x ^ arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT y = y ^ arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i & set_bit_no ) : NEW_LINE INDENT x = x ^ i NEW_LINE DEDENT else : NEW_LINE INDENT y = y ^ i NEW_LINE DEDENT DEDENT print ( " The ▁ two ▁ repeating " , " elements ▁ are " , y , x ) NEW_LINE DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


import java . util . * ; class GFG { static void countFreq ( int arr [ ] , int n ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { System . out . println ( entry . getKey ( ) + " ▁ " + entry . getValue ( ) ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 } ; int n = arr . length ; countFreq ( arr , n ) ; } }

def countFreq ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT for x in mp : NEW_LINE INDENT print ( x , " ▁ " , mp [ x ] ) NEW_LINE DEDENT DEDENT arr = [ 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE countFreq ( arr , n ) NEW_LINE


class Geeks { static int reverse ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int countReverse ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( reverse ( arr [ i ] ) == arr [ j ] ) { res ++ ; } return res ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 61 , 12 , 21 , 25 } ; int n = a . length ; System . out . print ( countReverse ( a , n ) ) ; } }

def reverse ( num ) : NEW_LINE INDENT rev_num = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT rev_num = rev_num * 10 + num % 10 NEW_LINE num = num // 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT def countReverse ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( reverse ( arr [ i ] ) == arr [ j ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 16 , 61 , 12 , 21 , 25 ] NEW_LINE n = len ( a ) NEW_LINE print ( countReverse ( a , n ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public static int minSteps ( int arr [ ] , int n ) { int min_Steps = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) min_Steps += Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; else min_Steps += Math . abs ( arr [ i ] ) ; } return min_Steps ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 2 , 2 , 1 } ; int n = 4 ; System . out . println ( minSteps ( arr , n ) ) ; } }

def minSteps ( arr , n ) : NEW_LINE INDENT min_Steps = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i > 0 ) : NEW_LINE INDENT min_Steps += abs ( arr [ i ] - arr [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT min_Steps += abs ( arr [ i ] ) NEW_LINE DEDENT DEDENT return min_Steps NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSteps ( arr , n ) ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static final int MAX = 100 ; static boolean allones ( String s , int n ) { int co = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) if ( s . charAt ( i ) == '1' ) co += 1 ; return ( co == n ) ; } static int findlength ( int arr [ ] , String s , int n , int ind , int st , int dp [ ] [ ] ) { if ( ind >= n ) return 0 ; if ( dp [ ind ] [ st ] != - 1 ) return dp [ ind ] [ st ] ; if ( st == 0 ) return dp [ ind ] [ st ] = Math . max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ; else return dp [ ind ] [ st ] = Math . max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) ; } static int maxLen ( String s , int n ) { if ( allones ( s , n ) ) return - 1 ; int arr [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = ( s . charAt ( i ) == '0' ? 1 : - 1 ) ; int dp [ ] [ ] = new int [ MAX ] [ 3 ] ; for ( int [ ] row : dp ) Arrays . fill ( row , - 1 ) ; return findlength ( arr , s , n , 0 , 0 , dp ) ; } public static void main ( String [ ] args ) { String s = "11000010001" ; int n = 11 ; System . out . println ( maxLen ( s , n ) ) ; } }

MAX = 100 NEW_LINE def allones ( s , n ) : NEW_LINE INDENT co = 0 NEW_LINE for i in s : NEW_LINE INDENT co += 1 if i == '1' else 0 NEW_LINE DEDENT return co == n NEW_LINE DEDENT def findlength ( arr , s , n , ind , st , dp ) : NEW_LINE INDENT if ind >= n : NEW_LINE INDENT return 0 NEW_LINE DEDENT if dp [ ind ] [ st ] != - 1 : NEW_LINE INDENT return dp [ ind ] [ st ] NEW_LINE DEDENT if not st : NEW_LINE INDENT dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , ( findlength ( arr , s , n , ind + 1 , 0 , dp ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ ind ] [ st ] = max ( arr [ ind ] + findlength ( arr , s , n , ind + 1 , 1 , dp ) , 0 ) NEW_LINE DEDENT return dp [ ind ] [ st ] NEW_LINE DEDENT def maxLen ( s , n ) : NEW_LINE INDENT if allones ( s , n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT arr = [ 0 ] * MAX NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = 1 if s [ i ] == '0' else - 1 NEW_LINE DEDENT dp = [ [ - 1 ] * 3 for _ in range ( MAX ) ] NEW_LINE return findlength ( arr , s , n , 0 , 0 , dp ) NEW_LINE DEDENT s = "11000010001" NEW_LINE n = 11 NEW_LINE print ( maxLen ( s , n ) ) NEW_LINE


class GFG { static boolean isVowel ( char ch ) { switch ( ch ) { case ' a ' : case ' e ' : case ' i ' : case ' o ' : case ' u ' : return true ; default : return false ; } } static int vowelPairs ( String s , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) && isVowel ( s . charAt ( i + 1 ) ) ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String s = " abaebio " ; int n = s . length ( ) ; System . out . print ( vowelPairs ( s , n ) ) ; } }

def isVowel ( ch ) : NEW_LINE INDENT if ch in [ ' a ' , ' e ' , ' i ' , ' o ' , ' u ' ] : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def vowelPairs ( s , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) and isVowel ( s [ i + 1 ] ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT s = " abaebio " NEW_LINE n = len ( s ) NEW_LINE print ( vowelPairs ( s , n ) ) NEW_LINE


class GFG { static int Rate ( int N1 , int N2 ) { float rate = ( N2 - N1 ) * 100 / N1 ; return ( int ) rate ; } public static void main ( String [ ] args ) { int N1 = 100 , N2 = 120 ; System . out . println ( Rate ( N1 , N2 ) + " ▁ % " ) ; } }

def Rate ( N1 , N2 ) : NEW_LINE INDENT rate = ( N2 - N1 ) * 100 // ( N1 ) ; NEW_LINE return rate NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N1 = 100 NEW_LINE N2 = 120 NEW_LINE print ( Rate ( N1 , N2 ) , " ▁ % " ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static void calcFiboTerms ( ArrayList < Integer > fiboterms , int k ) { int i = 3 , nextTerm = 0 ; fiboterms . add ( 0 ) ; fiboterms . add ( 1 ) ; fiboterms . add ( 1 ) ; while ( true ) { nextTerm = fiboterms . get ( i - 1 ) + fiboterms . get ( i - 2 ) ; if ( nextTerm > k ) return ; fiboterms . add ( nextTerm ) ; i ++ ; } } public static int fibMinTerms ( int k ) { ArrayList < Integer > fiboterms = new ArrayList < Integer > ( ) ; calcFiboTerms ( fiboterms , k ) ; int count = 0 , j = fiboterms . size ( ) - 1 ; while ( k > 0 ) { count += ( k / fiboterms . get ( j ) ) ; k %= ( fiboterms . get ( j ) ) ; j -- ; } return count ; } public static void main ( String [ ] args ) { int k = 17 ; System . out . println ( fibMinTerms ( k ) ) ; } }

def calcFiboTerms ( fiboTerms , K ) : NEW_LINE INDENT i = 3 NEW_LINE fiboTerms . append ( 0 ) NEW_LINE fiboTerms . append ( 1 ) NEW_LINE fiboTerms . append ( 1 ) NEW_LINE while True : NEW_LINE INDENT nextTerm = ( fiboTerms [ i - 1 ] + fiboTerms [ i - 2 ] ) NEW_LINE if nextTerm > K : NEW_LINE INDENT return NEW_LINE DEDENT fiboTerms . append ( nextTerm ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def findMinTerms ( K ) : NEW_LINE INDENT fiboTerms = [ ] NEW_LINE calcFiboTerms ( fiboTerms , K ) NEW_LINE count , j = 0 , len ( fiboTerms ) - 1 NEW_LINE while K > 0 : NEW_LINE INDENT count += K // fiboTerms [ j ] NEW_LINE K %= fiboTerms [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 17 NEW_LINE print ( findMinTerms ( K ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { public static void main ( String [ ] args ) { int ax = 5 , ay = 0 ; int bx = 1 , by = 1 ; int cx = 2 , cy = 5 ; System . out . println ( ax + ( cx - bx ) + " , ▁ " + ay + ( cy - by ) ) ; } }

if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ax , ay = 5 , 0 NEW_LINE bx , by = 1 , 1 NEW_LINE cx , cy = 2 , 5 NEW_LINE print ( ax + cx - bx , " , " , ay + cy - by ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 1001 ; static int prefix [ ] [ ] = new int [ MAX ] [ MAX ] ; static int ar [ ] = new int [ MAX ] ; static void cal_prefix ( int n , int arr [ ] ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) ar [ i + 1 ] = arr [ i ] ; for ( i = 1 ; i <= 1000 ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) prefix [ i ] [ j ] = 0 ; for ( j = 1 ; j <= n ; j ++ ) { prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + ( int ) ( ar [ j ] <= i ? 1 : 0 ) ; } } } static int ksub ( int l , int r , int n , int k ) { int lo , hi , mid ; lo = 1 ; hi = 1000 ; while ( lo + 1 < hi ) { mid = ( lo + hi ) / 2 ; if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) hi = mid ; else lo = mid + 1 ; } if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) hi = lo ; return hi ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 2 , 3 , 5 , 7 , 6 } ; int n = arr . length ; int k = 4 ; cal_prefix ( n , arr ) ; int queries [ ] [ ] = { { 1 , n , 1 } , { 2 , n - 2 , 2 } , { 3 , n - 1 , 3 } } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) ) ; } }

MAX = 1001 NEW_LINE prefix = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE ar = [ 0 for i in range ( MAX ) ] NEW_LINE def cal_prefix ( n , arr ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT ar [ i + 1 ] = arr [ i ] NEW_LINE DEDENT for i in range ( 1 , 1001 , 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT prefix [ i ] [ j ] = 0 NEW_LINE DEDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ar [ j ] <= i : NEW_LINE INDENT k = 1 NEW_LINE DEDENT else : NEW_LINE INDENT k = 0 NEW_LINE DEDENT prefix [ i ] [ j ] = prefix [ i ] [ j - 1 ] + k NEW_LINE DEDENT DEDENT DEDENT def ksub ( l , r , n , k ) : NEW_LINE INDENT lo = 1 NEW_LINE hi = 1000 NEW_LINE while ( lo + 1 < hi ) : NEW_LINE INDENT mid = int ( ( lo + hi ) / 2 ) NEW_LINE if ( prefix [ mid ] [ r ] - prefix [ mid ] [ l - 1 ] >= k ) : NEW_LINE INDENT hi = mid NEW_LINE DEDENT else : NEW_LINE INDENT lo = mid + 1 NEW_LINE DEDENT DEDENT if ( prefix [ lo ] [ r ] - prefix [ lo ] [ l - 1 ] >= k ) : NEW_LINE INDENT hi = lo NEW_LINE DEDENT return hi NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 4 , 2 , 3 , 5 , 7 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE cal_prefix ( n , arr ) NEW_LINE queries = [ [ 1 , n , 1 ] , [ 2 , n - 2 , 2 ] , [ 3 , n - 1 , 3 ] ] NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( ksub ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] , n , queries [ i ] [ 2 ] ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int sumPairs ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ( arr [ i ] * ( 2 * n ) ) ; } return sum ; } static public void main ( String [ ] arg ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( sumPairs ( arr , n ) ) ; } }

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


class GFG { static void findSetBits ( int n ) { for ( int i = 0 ; i <= n ; i ++ ) System . out . print ( Integer . bitCount ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; findSetBits ( n ) ; } }

def count ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT count += n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def findSetBits ( n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT print ( count ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE findSetBits ( n ) NEW_LINE DEDENT


class GFG { static int ksmallest ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= k ) k ++ ; else break ; } return k ; } public static void main ( String args [ ] ) { int k = 1 ; int arr [ ] = { 1 } ; int n = arr . length ; System . out . println ( ksmallest ( arr , n , k ) ) ; } }

def ksmallest ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] <= k ) : NEW_LINE INDENT k = k + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return k ; NEW_LINE DEDENT k = 1 ; NEW_LINE arr = [ 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( ksmallest ( arr , n , k ) ) ; NEW_LINE


import java . util . * ; class Solution { static void printArray ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int l = 0 , r = n - 1 , flag = 0 , i ; int k = 2 ; while ( l <= r ) { if ( flag == 0 ) { for ( i = l ; i < l + k && i <= r ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 1 ; l = i ; } else { for ( i = r ; i > r - k && i >= l ; i -- ) System . out . print ( arr [ i ] + " ▁ " ) ; flag = 0 ; r = i ; } k ++ ; } } public static void main ( String args [ ] ) { int n = 6 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; printArray ( arr , n ) ; } }

def printArray ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE flag = 0 NEW_LINE k = 2 NEW_LINE while ( l <= r ) : NEW_LINE INDENT if ( flag == 0 ) : NEW_LINE INDENT i = l NEW_LINE while i < l + k and i <= r : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT flag = 1 NEW_LINE l = i NEW_LINE DEDENT else : NEW_LINE INDENT i = r NEW_LINE while i > r - k and i >= l : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i -= 1 NEW_LINE DEDENT flag = 0 NEW_LINE r = i NEW_LINE DEDENT k += 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE printArray ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int sumDigitSquare ( int n ) { int sq = 0 ; while ( n > 0 ) { int digit = n % 10 ; sq += digit * digit ; n = n / 10 ; } return sq ; } static boolean isHappy ( int n ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( n ) ; while ( true ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( ( s . contains ( n ) && n != ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) ) return false ; s . add ( n ) ; } } public static void main ( String [ ] args ) { int n = 23 ; if ( isHappy ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def sumDigitSquare ( n ) : NEW_LINE INDENT sq = 0 ; NEW_LINE while ( n != 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE sq += digit * digit NEW_LINE n = n // 10 NEW_LINE DEDENT return sq ; NEW_LINE DEDENT def isHappy ( n ) : NEW_LINE INDENT s = set ( ) NEW_LINE s . add ( n ) NEW_LINE while ( True ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT n = sumDigitSquare ( n ) NEW_LINE if n in s : NEW_LINE INDENT return False NEW_LINE DEDENT s . add ( n ) NEW_LINE DEDENT return false ; NEW_LINE DEDENT n = 4 NEW_LINE if ( isHappy ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { public static int arraySum ( int arr [ ] , int n ) { int x = ( n + 1 ) / 2 ; return ( arr [ 0 ] - 1 ) * n + x * x ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 11 , 12 , 13 , 12 , 11 , 10 } ; int n = arr . length ; System . out . print ( arraySum ( arr , n ) ) ; } }

def arraySum ( arr , n ) : NEW_LINE INDENT x = ( n + 1 ) / 2 NEW_LINE return ( arr [ 0 ] - 1 ) * n + x * x NEW_LINE DEDENT arr = [ 10 , 11 , 12 , 13 , 12 , 11 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( arraySum ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static ArrayList < Integer > addToArrayForm ( ArrayList < Integer > A , int K ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; int rem = 0 ; int i = 0 ; for ( i = A . size ( ) - 1 ; i >= 0 ; i -- ) { int my = A . get ( i ) + K % 10 + rem ; if ( my > 9 ) { rem = 1 ; v . add ( my % 10 ) ; } else { v . add ( my ) ; rem = 0 ; } K = K / 10 ; } while ( K > 0 ) { int my = K % 10 + rem ; v . add ( my % 10 ) ; if ( my / 10 > 0 ) rem = 1 ; else rem = 0 ; K = K / 10 ; } if ( rem > 0 ) v . add ( rem ) ; for ( int j = v . size ( ) - 1 ; j >= 0 ; j -- ) ans . add ( v . get ( j ) ) ; return ans ; } public static void main ( String [ ] args ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; A . add ( 2 ) ; A . add ( 7 ) ; A . add ( 4 ) ; int K = 181 ; ArrayList < Integer > ans = addToArrayForm ( A , K ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) ) ; } }

def addToArrayForm ( A , K ) : NEW_LINE INDENT v , ans = [ ] , [ ] NEW_LINE rem , i = 0 , 0 NEW_LINE for i in range ( len ( A ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT my = A [ i ] + ( K % 10 ) + rem NEW_LINE if my > 9 : NEW_LINE INDENT rem = 1 NEW_LINE v . append ( my % 10 ) NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( my ) NEW_LINE rem = 0 NEW_LINE DEDENT K = K // 10 NEW_LINE DEDENT while K > 0 : NEW_LINE INDENT my = ( K % 10 ) + rem NEW_LINE v . append ( my % 10 ) NEW_LINE if my // 10 > 0 : NEW_LINE INDENT rem = 1 NEW_LINE DEDENT else : NEW_LINE INDENT rem = 0 NEW_LINE DEDENT K = K // 10 NEW_LINE DEDENT if rem > 0 : NEW_LINE INDENT v . append ( rem ) NEW_LINE DEDENT for i in range ( len ( v ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT ans . append ( v [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 2 , 7 , 4 ] NEW_LINE K = 181 NEW_LINE ans = addToArrayForm ( A , K ) NEW_LINE for i in range ( 0 , len ( ans ) ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int maze [ ] [ ] ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } static int numberOfPaths ( int m , int n ) { int path = 1 ; for ( int i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; } static int solve ( int maze [ ] [ ] ) { int ans = numberOfPaths ( R , C ) - countPaths ( maze ) ; return ans ; } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( solve ( maze ) ) ; } }

R = 4 NEW_LINE C = 4 NEW_LINE def countPaths ( maze ) : NEW_LINE INDENT if ( maze [ 0 ] [ 0 ] == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( R ) : NEW_LINE INDENT if ( maze [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT maze [ i ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , C ) : NEW_LINE INDENT if ( maze [ 0 ] [ i ] == 0 ) : NEW_LINE INDENT maze [ 0 ] [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , R ) : NEW_LINE INDENT for j in range ( 1 , C ) : NEW_LINE INDENT if ( maze [ i ] [ j ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( maze [ i - 1 ] [ j ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) NEW_LINE DEDENT if ( maze [ i ] [ j - 1 ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if ( maze [ R - 1 ] [ C - 1 ] > 0 ) : NEW_LINE INDENT return maze [ R - 1 ] [ C - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def numberOfPaths ( m , n ) : NEW_LINE INDENT path = 1 NEW_LINE for i in range ( n , m + n - 1 ) : NEW_LINE INDENT path *= i NEW_LINE path //= ( i - n + 1 ) NEW_LINE DEDENT return path NEW_LINE DEDENT def solve ( maze ) : NEW_LINE INDENT ans = ( numberOfPaths ( R , C ) - countPaths ( maze ) ) NEW_LINE return ans NEW_LINE DEDENT maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE print ( solve ( maze ) ) NEW_LINE


class GFG { static void findDivisors ( int n ) { int [ ] div = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j * i <= n ; j ++ ) div [ i * j ] ++ ; } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( div [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; findDivisors ( n ) ; } }

def findDivisors ( n ) : NEW_LINE INDENT div = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if j * i <= n : NEW_LINE INDENT div [ i * j ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( div [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE findDivisors ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int n ) { int f = 1 ; for ( int i = 2 ; i <= n ; i ++ ) f *= i ; return f ; } static void series ( int A , int X , int n ) { int nFact = factorial ( n ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { int niFact = factorial ( n - i ) ; int iFact = factorial ( i ) ; int aPow = ( int ) Math . pow ( A , n - i ) ; int xPow = ( int ) Math . pow ( X , i ) ; System . out . print ( ( nFact * aPow * xPow ) / ( niFact * iFact ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int A = 3 , X = 4 , n = 5 ; series ( A , X , n ) ; } }

def factorial ( n ) : NEW_LINE INDENT f = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f *= i NEW_LINE DEDENT return f NEW_LINE DEDENT def series ( A , X , n ) : NEW_LINE INDENT nFact = factorial ( n ) NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT niFact = factorial ( n - i ) NEW_LINE iFact = factorial ( i ) NEW_LINE aPow = pow ( A , n - i ) NEW_LINE xPow = pow ( X , i ) NEW_LINE print ( int ( ( nFact * aPow * xPow ) / ( niFact * iFact ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = 3 ; X = 4 ; n = 5 NEW_LINE series ( A , X , n ) NEW_LINE


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

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


import java . util . * ; class GFG { static int find_maxm ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int ans = 0 ; for ( Map . Entry < Integer , Integer > x : mp . entrySet ( ) ) { int value = x . getKey ( ) ; int freq = x . getValue ( ) ; if ( value == freq ) { ans = Math . max ( ans , value ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 3 , 4 , 3 } ; int n = arr . length ; System . out . print ( find_maxm ( arr , n ) ) ; } }

def find_maxm ( arr , n ) : NEW_LINE INDENT mpp = { } NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] in mpp ) : NEW_LINE INDENT mpp . update ( { arr [ i ] : mpp [ arr [ i ] ] + 1 } ) NEW_LINE DEDENT else : NEW_LINE INDENT mpp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for value , freq in mpp . items ( ) : NEW_LINE INDENT if ( value == freq ) : NEW_LINE INDENT ans = max ( ans , value ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 2 , 2 , 3 , 4 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( find_maxm ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int countBits ( int a ) { int count = 0 ; while ( a > 0 ) { if ( ( a & 1 ) > 0 ) count += 1 ; a = a >> 1 ; } return count ; } static void insertionSort ( int arr [ ] , int aux [ ] , int n ) { for ( int i = 1 ; i < n ; i ++ ) { int key1 = aux [ i ] ; int key2 = arr [ i ] ; int j = i - 1 ; while ( j >= 0 && aux [ j ] < key1 ) { aux [ j + 1 ] = aux [ j ] ; arr [ j + 1 ] = arr [ j ] ; j = j - 1 ; } aux [ j + 1 ] = key1 ; arr [ j + 1 ] = key2 ; } } static void sortBySetBitCount ( int arr [ ] , int n ) { int aux [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = countBits ( arr [ i ] ) ; insertionSort ( arr , aux , n ) ; } static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; sortBySetBitCount ( arr , n ) ; printArr ( arr , n ) ; } }

def countBits ( a ) : NEW_LINE INDENT count = 0 NEW_LINE while ( a ) : NEW_LINE INDENT if ( a & 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT a = a >> 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def insertionSort ( arr , aux , n ) : NEW_LINE INDENT for i in range ( 1 , n , 1 ) : NEW_LINE INDENT key1 = aux [ i ] NEW_LINE key2 = arr [ i ] NEW_LINE j = i - 1 NEW_LINE while ( j >= 0 and aux [ j ] < key1 ) : NEW_LINE INDENT aux [ j + 1 ] = aux [ j ] NEW_LINE arr [ j + 1 ] = arr [ j ] NEW_LINE j = j - 1 NEW_LINE DEDENT aux [ j + 1 ] = key1 NEW_LINE arr [ j + 1 ] = key2 NEW_LINE DEDENT DEDENT def sortBySetBitCount ( arr , n ) : NEW_LINE INDENT aux = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT aux [ i ] = countBits ( arr [ i ] ) NEW_LINE DEDENT insertionSort ( arr , aux , n ) NEW_LINE DEDENT def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE sortBySetBitCount ( arr , n ) NEW_LINE printArr ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void printCount ( int a [ ] [ ] , int n , int m , int z [ ] , int l ) { for ( int i = 0 ; i < n ; i ++ ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int j = 0 ; j < m ; j ++ ) mp . put ( a [ i ] [ j ] , 1 ) ; int count = 0 ; for ( int j = 0 ; j < l ; j ++ ) { if ( mp . containsKey ( z [ j ] ) ) count += 1 ; } System . out . println ( " row " + ( i + 1 ) + " ▁ = ▁ " + count ) ; } } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 8 , 27 , 39 , 589 , 23 } , { 23 , 34 , 589 , 12 , 45 } , { 939 , 32 , 27 , 12 , 78 } , { 23 , 349 , 48 , 21 , 32 } } ; int arr [ ] = { 589 , 39 , 27 } ; int n = a . length ; int m = 5 ; int l = arr . length ; printCount ( a , n , m , arr , l ) ; } }

def printCount ( a , n , m , z , l ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for j in range ( m ) : NEW_LINE INDENT mp [ a [ i ] [ j ] ] = 1 NEW_LINE DEDENT count = 0 NEW_LINE for j in range ( l ) : NEW_LINE INDENT if z [ j ] in mp . keys ( ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( " row " , i + 1 , " ▁ = ▁ " , count ) NEW_LINE DEDENT DEDENT a = [ [ 8 , 27 , 39 , 589 , 23 ] , [ 23 , 34 , 589 , 12 , 45 ] , [ 939 , 32 , 27 , 12 , 78 ] , [ 23 , 349 , 48 , 21 , 32 ] ] NEW_LINE arr = [ 589 , 39 , 27 ] NEW_LINE n = len ( a ) NEW_LINE m = 5 NEW_LINE l = len ( arr ) NEW_LINE printCount ( a , n , m , arr , l ) NEW_LINE


import java . io . * ; class GFG { static int minimumNumbers ( int n , int s ) { if ( ( s % n ) > 0 ) return s / n + 1 ; else return s / n ; } public static void main ( String [ ] args ) { int n = 5 ; int s = 11 ; System . out . println ( minimumNumbers ( n , s ) ) ; } }

def minimumNumbers ( n , s ) : NEW_LINE INDENT if ( s % n ) : NEW_LINE INDENT return s / n + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return s / n ; NEW_LINE DEDENT DEDENT n = 5 ; NEW_LINE s = 11 ; NEW_LINE print ( int ( minimumNumbers ( n , s ) ) ) ; NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int product ( int x , int y ) { if ( x < y ) return product ( y , x ) ; else if ( y != 0 ) return ( x + product ( x , y - 1 ) ) ; else return 0 ; } public static void main ( String [ ] args ) { int x = 5 , y = 2 ; System . out . println ( product ( x , y ) ) ; } }

def product ( x , y ) : NEW_LINE INDENT if x < y : NEW_LINE INDENT return product ( y , x ) NEW_LINE DEDENT elif y != 0 : NEW_LINE INDENT return ( x + product ( x , y - 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT x = 5 NEW_LINE y = 2 NEW_LINE print ( product ( x , y ) ) NEW_LINE


class GFG { static void findSubSeq ( int arr [ ] , int n , int sum ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum < arr [ i ] ) arr [ i ] = - 1 ; else sum -= arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 17 , 25 , 46 , 94 , 201 , 400 } ; int n = arr . length ; int sum = 272 ; findSubSeq ( arr , n , sum ) ; } }

def findSubSeq ( arr , n , sum ) : NEW_LINE INDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( sum < arr [ i ] ) : NEW_LINE INDENT arr [ i ] = - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT sum -= arr [ i ] ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 17 , 25 , 46 , 94 , 201 , 400 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE sum = 272 ; NEW_LINE findSubSeq ( arr , n , sum ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static float circleradius ( float l , float b ) { if ( l < 0 || b < 0 ) return - 1 ; float r = ( float ) ( ( l * b ) / ( 2 * Math . sqrt ( ( Math . pow ( l , 2 ) + Math . pow ( b , 2 ) ) ) ) ) ; return r ; } public static void main ( String [ ] args ) { float l = 5 , b = 3 ; System . out . print ( circleradius ( l , b ) ) ; } }

from math import sqrt NEW_LINE def circleradius ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT r = ( l * b ) / ( 2 * sqrt ( ( pow ( l , 2 ) + pow ( b , 2 ) ) ) ) ; NEW_LINE return r NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 5 NEW_LINE b = 3 NEW_LINE print ( " { 0 : . 5 } " . format ( circleradius ( l , b ) ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int minOperations ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int maxFreq = Integer . MIN_VALUE ; maxFreq = Collections . max ( mp . entrySet ( ) , Comparator . comparingInt ( Map . Entry :: getKey ) ) . getValue ( ) ; return ( n - maxFreq ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; System . out . println ( minOperations ( arr , n ) ) ; } }

import sys NEW_LINE def minOperations ( arr , n ) : NEW_LINE INDENT mp = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 ; NEW_LINE DEDENT maxFreq = - ( sys . maxsize - 1 ) ; NEW_LINE for key in mp : NEW_LINE INDENT maxFreq = max ( maxFreq , mp [ key ] ) ; NEW_LINE DEDENT return ( n - maxFreq ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( minOperations ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static int swapBits ( int x ) { int even_bits = x & 0xAAAAAAAA ; int odd_bits = x & 0x55555555 ; even_bits >>= 1 ; odd_bits <<= 1 ; return ( even_bits | odd_bits ) ; } public static void main ( String [ ] args ) { int x = 23 ; System . out . println ( swapBits ( x ) ) ; } }

def swapBits ( x ) : NEW_LINE INDENT even_bits = x & 0xAAAAAAAA NEW_LINE odd_bits = x & 0x55555555 NEW_LINE even_bits >>= 1 NEW_LINE odd_bits <<= 1 NEW_LINE return ( even_bits | odd_bits ) NEW_LINE DEDENT x = 23 NEW_LINE print ( swapBits ( x ) ) NEW_LINE


import java . util . * ; class GFG { static int dfs ( int node , int parent , Vector < Integer > [ ] adj , boolean [ ] vis ) { int ans = 1 ; vis [ node ] = true ; for ( Integer it : adj [ node ] ) { if ( it != parent ) ans += dfs ( it , node , adj , vis ) ; } return ans ; } static int countPaths ( Vector < Integer > [ ] adj , int k , int maxn ) { boolean [ ] vis = new boolean [ maxn + 1 ] ; int ans = 0 ; Arrays . fill ( vis , false ) ; for ( int i = 1 ; i <= maxn ; i ++ ) { if ( ! vis [ i ] ) { int numNodes = dfs ( i , 0 , adj , vis ) ; ans += numNodes * ( numNodes - 1 ) / 2 ; } } return ans ; } static void addEdge ( Vector < Integer > [ ] adj , int u , int v , int k ) { if ( u > k && v > k ) { adj [ u ] . add ( v ) ; adj [ v ] . add ( u ) ; } } public static void main ( String [ ] args ) { int maxn = 12 ; @ SuppressWarnings ( " unchecked " ) Vector < Integer > [ ] adj = new Vector [ maxn + 1 ] ; for ( int i = 0 ; i < maxn + 1 ; i ++ ) { adj [ i ] = new Vector < > ( ) ; } int k = 3 ; addEdge ( adj , 2 , 11 , k ) ; addEdge ( adj , 2 , 6 , k ) ; addEdge ( adj , 5 , 11 , k ) ; addEdge ( adj , 5 , 10 , k ) ; addEdge ( adj , 5 , 12 , k ) ; addEdge ( adj , 6 , 7 , k ) ; addEdge ( adj , 6 , 8 , k ) ; System . out . println ( countPaths ( adj , k , maxn ) ) ; } }

def dfs ( node , parent , adj , vis ) : NEW_LINE INDENT ans = 1 NEW_LINE vis [ node ] = True NEW_LINE for it in adj [ node ] : NEW_LINE INDENT if it != parent : NEW_LINE INDENT ans += dfs ( it , node , adj , vis ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def countPaths ( adj , k , maxn ) : NEW_LINE INDENT vis = [ False ] * ( maxn + 1 ) NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , maxn + 1 ) : NEW_LINE INDENT if not vis [ i ] : NEW_LINE INDENT numNodes = dfs ( i , 0 , adj , vis ) NEW_LINE ans += numNodes * ( numNodes - 1 ) // 2 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def addEdge ( adj , u , v , k ) : NEW_LINE INDENT if u > k and v > k : NEW_LINE INDENT adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT maxn = 12 NEW_LINE adj = [ [ ] for i in range ( maxn + 1 ) ] NEW_LINE k = 3 NEW_LINE addEdge ( adj , 2 , 11 , k ) NEW_LINE addEdge ( adj , 2 , 6 , k ) NEW_LINE addEdge ( adj , 5 , 11 , k ) NEW_LINE addEdge ( adj , 5 , 10 , k ) NEW_LINE addEdge ( adj , 5 , 12 , k ) NEW_LINE addEdge ( adj , 6 , 7 , k ) NEW_LINE addEdge ( adj , 6 , 8 , k ) NEW_LINE print ( countPaths ( adj , k , maxn ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public static int longOddEvenIncSeq ( int arr [ ] , int n ) { int [ ] lioes = new int [ n ] ; int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) lioes [ i ] = lioes [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( maxLen < lioes [ i ] ) maxLen = lioes [ i ] ; return maxLen ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 } ; int n = 10 ; System . out . println ( " Longest ▁ Increasing ▁ Odd " + " ▁ Even ▁ Subsequence : ▁ " + longOddEvenIncSeq ( arr , n ) ) ; } }

def longOddEvenIncSeq ( arr , n ) : NEW_LINE INDENT lioes = list ( ) NEW_LINE maxLen = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lioes . append ( 1 ) NEW_LINE DEDENT i = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and ( arr [ i ] + arr [ j ] ) % 2 != 0 and lioes [ i ] < lioes [ j ] + 1 ) : NEW_LINE INDENT lioes [ i ] = lioes [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if maxLen < lioes [ i ] : NEW_LINE INDENT maxLen = lioes [ i ] NEW_LINE DEDENT DEDENT return maxLen NEW_LINE DEDENT arr = [ 1 , 12 , 2 , 22 , 5 , 30 , 31 , 14 , 17 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Longest ▁ Increasing ▁ Odd ▁ Even ▁ " + " Subsequence : ▁ " , longOddEvenIncSeq ( arr , n ) ) NEW_LINE


public class PermuteString { final static int MAX_CHAR = 26 ; final static int MAX_FACT = 20 ; static long fact [ ] = new long [ MAX_FACT ] ; static void precomputeFactorirals ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAX_FACT ; i ++ ) fact [ i ] = fact [ i - 1 ] * i ; } static void nPermute ( String str , int n ) { precomputeFactorirals ( ) ; int len = str . length ( ) ; int freq [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < len ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; String out = " " ; int sum = 10 ; int k = 0 ; while ( sum >= n ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( freq [ i ] == 0 ) continue ; freq [ i ] -- ; sum = 0 ; int xsum = ( int ) fact [ len - 1 - k ] ; for ( int j = 0 ; j < MAX_CHAR ; j ++ ) xsum /= fact [ freq [ j ] ] ; sum += xsum ; if ( sum >= n ) { out += ( char ) ( i + ' a ' ) ; k ++ ; n -= ( sum - xsum ) ; break ; } if ( sum < n ) freq [ i ] ++ ; } } for ( int i = MAX_CHAR - 1 ; k < len && i >= 0 ; i -- ) if ( freq [ i ] != 0 ) { out += ( char ) ( i + ' a ' ) ; freq [ i ++ ] -- ; } System . out . println ( out ) ; } public static void main ( String [ ] args ) { int n = 2 ; String str = " geeksquiz " ; nPermute ( str , n ) ; } }

MAX_CHAR = 26 NEW_LINE MAX_FACT = 20 NEW_LINE fact = [ None ] * ( MAX_FACT ) NEW_LINE def precomputeFactorials ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , MAX_FACT ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT DEDENT def nPermute ( string , n ) : NEW_LINE INDENT precomputeFactorials ( ) NEW_LINE length = len ( string ) NEW_LINE freq = [ 0 ] * ( MAX_CHAR ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT out = [ None ] * ( MAX_CHAR ) NEW_LINE Sum , k = 0 , 0 NEW_LINE while Sum != n : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if freq [ i ] == 0 : NEW_LINE INDENT continue NEW_LINE DEDENT freq [ i ] -= 1 NEW_LINE xsum = fact [ length - 1 - k ] NEW_LINE for j in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT xsum = xsum // fact [ freq [ j ] ] NEW_LINE DEDENT Sum += xsum NEW_LINE if Sum >= n : NEW_LINE INDENT out [ k ] = chr ( i + ord ( ' a ' ) ) NEW_LINE n -= Sum - xsum NEW_LINE k += 1 NEW_LINE break NEW_LINE DEDENT if Sum < n : NEW_LINE INDENT freq [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT i = MAX_CHAR - 1 NEW_LINE while k < length and i >= 0 : NEW_LINE INDENT if freq [ i ] : NEW_LINE INDENT out [ k ] = chr ( i + ord ( ' a ' ) ) NEW_LINE freq [ i ] -= 1 NEW_LINE i += 1 NEW_LINE k += 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT print ( ' ' . join ( out [ : k ] ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE string = " geeksquiz " NEW_LINE nPermute ( string , n ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int countTriplets ( int arr [ ] , int n , int m ) { int count = 0 ; Arrays . sort ( arr ) ; int end , start , mid ; for ( end = n - 1 ; end >= 2 ; end -- ) { start = 0 ; mid = end - 1 ; while ( start < mid ) { long prod = arr [ end ] * arr [ start ] * arr [ mid ] ; if ( prod > m ) mid -- ; else if ( prod < m ) start ++ ; else if ( prod == m ) { count ++ ; mid -- ; start ++ ; } } } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; int m = 1 ; System . out . println ( countTriplets ( arr , n , m ) ) ; } }

def countTriplets ( arr , n , m ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for end in range ( n - 1 , 1 , - 1 ) : NEW_LINE INDENT start = 0 NEW_LINE mid = end - 1 NEW_LINE while ( start < mid ) : NEW_LINE INDENT prod = ( arr [ end ] * arr [ start ] * arr [ mid ] ) NEW_LINE if ( prod > m ) : NEW_LINE INDENT mid -= 1 NEW_LINE DEDENT elif ( prod < m ) : NEW_LINE INDENT start += 1 NEW_LINE DEDENT elif ( prod == m ) : NEW_LINE INDENT count += 1 NEW_LINE mid -= 1 NEW_LINE start += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE m = 1 NEW_LINE print ( countTriplets ( arr , n , m ) ) NEW_LINE DEDENT


class GFG { static int countSteps ( int n ) { int steps = 0 ; while ( n > 0 ) { int largest = ( int ) Math . cbrt ( n ) ; n -= ( largest * largest * largest ) ; steps ++ ; } return steps ; } public static void main ( String [ ] args ) { int n = 150 ; System . out . print ( countSteps ( n ) ) ; } }

from math import floor NEW_LINE def countSteps ( n ) : NEW_LINE INDENT steps = 0 NEW_LINE while ( n ) : NEW_LINE INDENT largest = floor ( n ** ( 1 / 3 ) ) NEW_LINE n -= ( largest * largest * largest ) NEW_LINE steps += 1 NEW_LINE DEDENT return steps NEW_LINE DEDENT n = 150 NEW_LINE print ( countSteps ( n ) ) NEW_LINE


class GFG { static boolean isPalindrome ( int n ) { if ( n % 11 == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { System . out . println ( isPalindrome ( 123321 ) ? " Palindrome " : " Not ▁ Palindrome " ) ; } }

def isPalindrome ( n ) : NEW_LINE INDENT if n % 11 == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 123321 NEW_LINE if isPalindrome ( n ) : NEW_LINE INDENT print ( " Palindrome " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Palindrome " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int inv ( int a , int m ) { int m0 = m , t , q ; int x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static int findMinX ( int num [ ] , int rem [ ] , int k ) { int prod = 1 ; for ( int i = 0 ; i < k ; i ++ ) prod *= num [ i ] ; int result = 0 ; for ( int i = 0 ; i < k ; i ++ ) { int pp = prod / num [ i ] ; result += rem [ i ] * inv ( pp , num [ i ] ) * pp ; } return result % prod ; } public static void main ( String args [ ] ) { int num [ ] = { 3 , 4 , 5 } ; int rem [ ] = { 2 , 3 , 1 } ; int k = num . length ; System . out . println ( " x ▁ is ▁ " + findMinX ( num , rem , k ) ) ; } }

def inv ( a , m ) : NEW_LINE INDENT m0 = m NEW_LINE x0 = 0 NEW_LINE x1 = 1 NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = a // m NEW_LINE t = m NEW_LINE m = a % m NEW_LINE a = t NEW_LINE t = x0 NEW_LINE x0 = x1 - q * x0 NEW_LINE x1 = t NEW_LINE DEDENT if ( x1 < 0 ) : NEW_LINE INDENT x1 = x1 + m0 NEW_LINE DEDENT return x1 NEW_LINE DEDENT def findMinX ( num , rem , k ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT prod = prod * num [ i ] NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT pp = prod // num [ i ] NEW_LINE result = result + rem [ i ] * inv ( pp , num [ i ] ) * pp NEW_LINE DEDENT return result % prod NEW_LINE DEDENT num = [ 3 , 4 , 5 ] NEW_LINE rem = [ 2 , 3 , 1 ] NEW_LINE k = len ( num ) NEW_LINE print ( " x ▁ is ▁ " , findMinX ( num , rem , k ) ) NEW_LINE


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } static void printResult ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( gcd ( arr [ i ] , arr [ i - 1 ] ) != 1 ) count ++ ; System . out . println ( count ) ; System . out . print ( arr [ 0 ] + " ▁ " ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( gcd ( arr [ i ] , arr [ i - 1 ] ) != 1 ) System . out . print ( 1 + " ▁ " ) ; System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int A [ ] = { 5 , 10 , 20 } ; int n = A . length ; printResult ( A , n ) ; } }

from fractions import gcd NEW_LINE def printResult ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( gcd ( arr [ i ] , arr [ i - 1 ] ) != 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE print ( arr [ 0 ] , end = " ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( gcd ( arr [ i ] , arr [ i - 1 ] ) != 1 ) : NEW_LINE INDENT print ( 1 , end = " ▁ " ) NEW_LINE DEDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT A = [ 5 , 10 , 20 ] NEW_LINE n = len ( A ) NEW_LINE printResult ( A , n ) NEW_LINE


import java . util . * ; class GFG { static void print_max ( int a [ ] , int n , int k ) { int [ ] max_upto = new int [ n ] ; Stack < Integer > s = new Stack < > ( ) ; s . push ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! s . empty ( ) && a [ s . peek ( ) ] < a [ i ] ) { max_upto [ s . peek ( ) ] = i - 1 ; s . pop ( ) ; } s . push ( i ) ; } while ( ! s . empty ( ) ) { max_upto [ s . peek ( ) ] = n - 1 ; s . pop ( ) ; } int j = 0 ; for ( int i = 0 ; i <= n - k ; i ++ ) { while ( j < i || max_upto [ j ] < i + k - 1 ) { j ++ ; } System . out . print ( a [ j ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 7 , 2 , 4 , 6 , 8 , 2 , 1 , 5 } ; int n = a . length ; int k = 3 ; print_max ( a , n , k ) ; } }

def print_max ( a , n , k ) : NEW_LINE INDENT max_upto = [ 0 for i in range ( n ) ] NEW_LINE s = [ ] NEW_LINE s . append ( 0 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( len ( s ) > 0 and a [ s [ - 1 ] ] < a [ i ] ) : NEW_LINE INDENT max_upto [ s [ - 1 ] ] = i - 1 NEW_LINE del s [ - 1 ] NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT while ( len ( s ) > 0 ) : NEW_LINE INDENT max_upto [ s [ - 1 ] ] = n - 1 NEW_LINE del s [ - 1 ] NEW_LINE DEDENT j = 0 NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT while ( j < i or max_upto [ j ] < i + k - 1 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT print ( a [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT a = [ 9 , 7 , 2 , 4 , 6 , 8 , 2 , 1 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE print_max ( a , n , k ) NEW_LINE


import java . util . * ; class GFG { static Vector < Integer > PrimeFactors ( int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; int x = n ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( x % i == 0 ) { v . add ( i ) ; while ( x % i == 0 ) x /= i ; } } if ( x > 1 ) v . add ( x ) ; return v ; } static int GoodNumber ( int n ) { Vector < Integer > v = new Vector < Integer > ( PrimeFactors ( n ) ) ; int ans = 1 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans *= v . get ( i ) ; return ans ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( GoodNumber ( n ) ) ; } }

def PrimeFactors ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE x = n NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT v . append ( i ) NEW_LINE while ( x % i == 0 ) : NEW_LINE INDENT x //= i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( x > 1 ) : NEW_LINE INDENT v . append ( x ) NEW_LINE DEDENT return v NEW_LINE DEDENT def GoodNumber ( n ) : NEW_LINE INDENT v = PrimeFactors ( n ) NEW_LINE ans = 1 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT ans *= v [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE print ( GoodNumber ( n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int evenDecimalValue ( String str , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { result += ( n - i ) ; } } return result ; } public static void main ( String [ ] args ) { String str = "10010" ; int n = 5 ; System . out . println ( evenDecimalValue ( str , n ) ) ; } }

def evenDecimalValue ( str , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT result += ( n - i ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT str = "10010" NEW_LINE n = 5 NEW_LINE print ( evenDecimalValue ( str , n ) ) NEW_LINE


import java . util . Arrays ; public class GFG { static void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) != 0 ) arr [ i ] *= - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . length ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }

def twoWaySort ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE DEDENT DEDENT arr . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE twoWaySort ( arr , n ) ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT