id
stringlengths 2
6
| java
stringlengths 48
5.92k
| python
stringlengths 33
11.1k
|
---|---|---|
T40000 | import java . io . * ; public class GFG { static void printDiagonalSums ( int [ ] [ ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { principal += mat [ i ] [ i ] ; secondary += mat [ i ] [ n - i - 1 ] ; } System . out . println ( " Principal β Diagonal : " + principal ) ; System . out . println ( " Secondary β Diagonal : " + secondary ) ; } static public void main ( String [ ] args ) { int [ ] [ ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }
| MAX = 100 NEW_LINE def printDiagonalSums ( mat , n ) : NEW_LINE INDENT principal = 0 NEW_LINE secondary = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT principal += mat [ i ] [ i ] NEW_LINE secondary += mat [ i ] [ n - i - 1 ] NEW_LINE DEDENT print ( " Principal β Diagonal : " , principal ) NEW_LINE print ( " Secondary β Diagonal : " , secondary ) NEW_LINE DEDENT a = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] NEW_LINE printDiagonalSums ( a , 4 ) NEW_LINE
|
T40001 | import java . io . * ; class GFG { static void check ( int n , int m ) { if ( n == 2 || m == 2 || n % m == 0 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int m = 3 , n = 9 ; check ( n , m ) ; } }
| def check ( n , m ) : NEW_LINE INDENT if ( n == 2 or m == 2 or n % m == 0 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT m = 3 NEW_LINE n = 9 NEW_LINE check ( n , m ) NEW_LINE
|
T40002 | class GFG { static boolean onlyFirstAndLastAreSet ( int n ) { if ( n == 1 ) return true ; if ( n == 2 ) return false ; return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) ; } public static void main ( String [ ] args ) { int n = 9 ; if ( onlyFirstAndLastAreSet ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def onlyFirstAndLastAreSet ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( ( ( n - 1 ) & ( n - 2 ) ) == 0 ) NEW_LINE DEDENT n = 9 NEW_LINE if ( onlyFirstAndLastAreSet ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40003 | public class GfG { public static int removeZero ( int n ) { int res = 0 ; int d = 1 ; while ( n > 0 ) { if ( n % 10 != 0 ) { res += ( n % 10 ) * d ; d *= 10 ; } n /= 10 ; } return res ; } public static boolean isEqual ( int a , int b ) { if ( removeZero ( a ) + removeZero ( b ) == removeZero ( a + b ) ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 105 , b = 106 ; if ( isEqual ( a , b ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def removeZero ( n ) : NEW_LINE INDENT res = 0 NEW_LINE d = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n % 10 != 0 ) : NEW_LINE INDENT res += ( n % 10 ) * d NEW_LINE d *= 10 NEW_LINE DEDENT n //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT def isEqual ( a , b ) : NEW_LINE INDENT if ( removeZero ( a ) + removeZero ( b ) == removeZero ( a + b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a = 105 NEW_LINE b = 106 NEW_LINE if ( isEqual ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40004 | import java . math . BigInteger ; class Test { static int FindLCM ( int a , int b ) { return ( a * b ) / new BigInteger ( a + " " ) . gcd ( new BigInteger ( b + " " ) ) . intValue ( ) ; } static int rangeDivisor ( int m , int n , int a , int b ) { int lcm = FindLCM ( a , b ) ; int a_divisor = n / a - ( m - 1 ) / a ; int b_divisor = n / b - ( m - 1 ) / b ; int common_divisor = n / lcm - ( m - 1 ) / lcm ; int ans = a_divisor + b_divisor - common_divisor ; return ans ; } public static void main ( String args [ ] ) { int m = 3 , n = 11 , a = 2 , b = 3 ; System . out . println ( rangeDivisor ( m , n , a , b ) ) ; m = 11 ; n = 1000000 ; a = 6 ; b = 35 ; System . out . println ( rangeDivisor ( m , n , a , b ) ) ; } }
| def __gcd ( x , y ) : NEW_LINE INDENT if x > y : NEW_LINE INDENT small = y NEW_LINE DEDENT else : NEW_LINE INDENT small = x NEW_LINE DEDENT for i in range ( 1 , small + 1 ) : NEW_LINE INDENT if ( ( x % i == 0 ) and ( y % i == 0 ) ) : NEW_LINE INDENT gcd = i NEW_LINE DEDENT DEDENT return gcd NEW_LINE DEDENT def FindLCM ( a , b ) : NEW_LINE INDENT return ( a * b ) / __gcd ( a , b ) ; NEW_LINE DEDENT def rangeDivisor ( m , n , a , b ) : NEW_LINE INDENT lcm = FindLCM ( a , b ) NEW_LINE a_divisor = int ( n / a - ( m - 1 ) / a ) NEW_LINE b_divisor = int ( n / b - ( m - 1 ) / b ) NEW_LINE common_divisor = int ( n / lcm - ( m - 1 ) / lcm ) NEW_LINE ans = a_divisor + b_divisor - common_divisor NEW_LINE return ans NEW_LINE DEDENT m = 3 NEW_LINE n = 11 NEW_LINE a = 2 NEW_LINE b = 3 ; NEW_LINE print ( rangeDivisor ( m , n , a , b ) ) NEW_LINE m = 11 NEW_LINE n = 1000000 NEW_LINE a = 6 NEW_LINE b = 35 NEW_LINE print ( rangeDivisor ( m , n , a , b ) ) NEW_LINE
|
T40005 | import java . io . * ; class GFG { static void solve ( long a , long b ) { if ( a > 0 && b > 0 ) { System . out . println ( " Positive " ) ; } else if ( a <= 0 && b >= 0 ) { System . out . println ( " Zero " ) ; } else { long n = Math . abs ( a - b ) + 1 ; if ( n % 2 == 0 ) { System . out . println ( " Positive " ) ; } else { System . out . println ( " Negative " ) ; } } } public static void main ( String [ ] args ) { int a = - 10 , b = - 2 ; solve ( a , b ) ; } }
| def solve ( a , b ) : NEW_LINE INDENT if ( a > 0 and b > 0 ) : NEW_LINE INDENT print ( " Positive " ) NEW_LINE DEDENT elif ( a <= 0 and b >= 0 ) : NEW_LINE INDENT print ( " Zero " ) NEW_LINE DEDENT else : NEW_LINE INDENT n = abs ( a - b ) + 1 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " Positive " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Negative " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = - 10 NEW_LINE b = - 2 NEW_LINE solve ( a , b ) NEW_LINE DEDENT
|
T40006 | import java . util . * ; class GFG { static ArrayList < Integer > allPrimes = new ArrayList < Integer > ( ) ; static void sieve ( int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == false ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = true ; } } for ( int p = 2 ; p <= n ; p ++ ) if ( prime [ p ] == false ) allPrimes . add ( p ) ; } static int factorialDivisors ( int n ) { sieve ( n ) ; int result = 1 ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { int p = allPrimes . get ( i ) ; int exp = 0 ; while ( p <= n ) { exp = exp + ( n / p ) ; p = p * allPrimes . get ( i ) ; } result = result * ( ( int ) Math . pow ( allPrimes . get ( i ) , exp + 1 ) - 1 ) / ( allPrimes . get ( i ) - 1 ) ; } return result ; } public static void main ( String [ ] args ) { System . out . println ( factorialDivisors ( 4 ) ) ; } }
| allPrimes = [ ] ; NEW_LINE def sieve ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) ; NEW_LINE p = 2 ; NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT for p in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT allPrimes . append ( p ) ; NEW_LINE DEDENT DEDENT DEDENT def factorialDivisors ( n ) : NEW_LINE INDENT sieve ( n ) ; NEW_LINE result = 1 ; NEW_LINE for i in range ( len ( allPrimes ) ) : NEW_LINE INDENT p = allPrimes [ i ] ; NEW_LINE exp = 0 ; NEW_LINE while ( p <= n ) : NEW_LINE INDENT exp = exp + int ( n / p ) ; NEW_LINE p = p * allPrimes [ i ] ; NEW_LINE DEDENT result = int ( result * ( pow ( allPrimes [ i ] , exp + 1 ) - 1 ) / ( allPrimes [ i ] - 1 ) ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT print ( factorialDivisors ( 4 ) ) ; NEW_LINE
|
T40007 | public class AQW { static int fib [ ] = new int [ 43 ] ; static void fibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 2 ; for ( int i = 2 ; i < 43 ; i ++ ) fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } static int rec ( int x , int y , int last ) { if ( y == 0 ) { if ( x == 0 ) return 1 ; return 0 ; } int sum = 0 ; for ( int i = last ; i >= 0 && fib [ i ] * y >= x ; i -- ) { if ( fib [ i ] > x ) continue ; sum += rec ( x - fib [ i ] , y - 1 , i ) ; } return sum ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int n = 13 , k = 3 ; System . out . println ( " Possible β ways β are : β " + rec ( n , k , 42 ) ) ; } }
| fib = [ 0 ] * 43 NEW_LINE def fibonacci ( ) : NEW_LINE INDENT fib [ 0 ] = 1 NEW_LINE fib [ 1 ] = 2 NEW_LINE for i in range ( 2 , 43 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DEDENT def rec ( x , y , last ) : NEW_LINE INDENT if y == 0 : NEW_LINE INDENT if x == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT Sum , i = 0 , last NEW_LINE while i >= 0 and fib [ i ] * y >= x : NEW_LINE INDENT if fib [ i ] > x : NEW_LINE INDENT i -= 1 NEW_LINE continue NEW_LINE DEDENT Sum += rec ( x - fib [ i ] , y - 1 , i ) NEW_LINE i -= 1 NEW_LINE DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT fibonacci ( ) NEW_LINE n , k = 13 , 3 NEW_LINE print ( " Possible β ways β are : " , rec ( n , k , 42 ) ) NEW_LINE DEDENT
|
T40008 | class GFG { static boolean isVowel ( char ch ) { if ( ch == ' a ' || ch == ' e ' || ch == ' i ' || ch == ' o ' || ch == ' u ' ) return true ; return false ; } static int lcs ( String X , String Y , int m , int n ) { int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= m ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { if ( i == 0 || j == 0 ) L [ i ] [ j ] = 0 ; else if ( ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) && isVowel ( X . charAt ( i - 1 ) ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } public static void main ( String [ ] args ) { String X = " aieef " ; String Y = " klaief " ; int m = X . length ( ) ; int n = Y . length ( ) ; System . out . println ( " Length β of β LCS β = β " + lcs ( X , Y , m , n ) ) ; } }
| def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def lcs ( X , Y , m , n ) : NEW_LINE INDENT L = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] NEW_LINE i , j = 0 , 0 NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT L [ i ] [ j ] = 0 NEW_LINE DEDENT elif ( ( X [ i - 1 ] == Y [ j - 1 ] ) and isVowel ( X [ i - 1 ] ) ) : NEW_LINE INDENT L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return L [ m ] [ n ] NEW_LINE DEDENT X = " aieef " NEW_LINE Y = " klaief " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE print ( " Length β of β LCS β = " , lcs ( X , Y , m , n ) ) NEW_LINE
|
T40009 | class GfG { static void palindrome ( int arr [ ] , int n ) { int flag = 0 ; for ( int i = 0 ; i <= n / 2 && n != 0 ; i ++ ) { if ( arr [ i ] != arr [ n - i - 1 ] ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( " Not β Palindrome " ) ; else System . out . println ( " Palindrome " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 , 1 } ; int n = arr . length ; palindrome ( arr , n ) ; } }
| def palindrome ( arr , n ) : NEW_LINE INDENT flag = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i <= n // 2 and n != 0 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ n - i - 1 ] ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " Not β Palindrome " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Palindrome " ) ; NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 2 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE palindrome ( arr , n ) ; NEW_LINE
|
T40010 | class GFG { static int countSolutions ( int n , int val ) { int total = 0 ; if ( n == 1 && val >= 0 ) return 1 ; for ( int i = 0 ; i <= val ; i ++ ) { total += countSolutions ( n - 1 , val - i ) ; } return total ; } public static void main ( String [ ] args ) { int n = 5 ; int val = 20 ; System . out . print ( countSolutions ( n , val ) ) ; } }
| def countSolutions ( n , val ) : NEW_LINE INDENT total = 0 NEW_LINE if n == 1 and val >= 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( val + 1 ) : NEW_LINE INDENT total += countSolutions ( n - 1 , val - i ) NEW_LINE DEDENT return total NEW_LINE DEDENT n = 5 NEW_LINE val = 20 NEW_LINE print ( countSolutions ( n , val ) ) NEW_LINE
|
T40011 | class GFG { static boolean isPrime ( long n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( long i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static boolean isDiffPrime ( long a , long b ) { if ( isPrime ( a + b ) && a - b == 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { long a = 6 , b = 5 ; if ( isDiffPrime ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| 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 def isDiffPrime ( a , b ) : NEW_LINE INDENT if ( isPrime ( a + b ) and a - b == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT a = 6 NEW_LINE b = 5 NEW_LINE if ( isDiffPrime ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40012 | import java . util . * ; class GFG { static int CalculateDifference ( int arr [ ] , int n ) { int max_val = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > max_val ) max_val = arr [ i ] ; } boolean [ ] prime = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int S1 = 0 , S2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { S1 += arr [ i ] ; } else if ( arr [ i ] != 1 ) { S2 += arr [ i ] ; } } return Math . abs ( S2 - S1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . length ; System . out . println ( CalculateDifference ( arr , n ) ) ; } }
| import sys NEW_LINE def CalculateDifference ( arr , n ) : NEW_LINE INDENT max_val = - 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] > max_val ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while ( p * p <= max_val ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT S1 = 0 NEW_LINE S2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT S1 += arr [ i ] NEW_LINE DEDENT elif arr [ i ] != 1 : NEW_LINE INDENT S2 += arr [ i ] NEW_LINE DEDENT DEDENT return abs ( S2 - S1 ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CalculateDifference ( arr , n ) ) NEW_LINE
|
T40013 | class GFG { static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) != 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int NearestElement ( int A , int D , int P ) { if ( A == 0 ) return 0 ; else if ( D == 0 ) return - 1 ; else { int X = power ( D , P - 2 , P ) ; return ( X * ( P - A ) ) % P ; } } public static void main ( String args [ ] ) { int A = 4 , D = 9 , P = 11 ; A %= P ; D %= P ; System . out . println ( NearestElement ( A , D , P ) ) ; } }
| def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while y > 0 : NEW_LINE INDENT if y & 1 : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def NearestElement ( A , D , P ) : NEW_LINE INDENT if A == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif D == 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT X = power ( D , P - 2 , P ) NEW_LINE return ( X * ( P - A ) ) % P NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , D , P = 4 , 9 , 11 NEW_LINE A %= P NEW_LINE D %= P NEW_LINE print ( NearestElement ( A , D , P ) ) NEW_LINE DEDENT
|
T40014 | import java . util . Arrays ; class GFG { static long mod = 1000000007 ; static long [ ] [ ] arr = new long [ 1001 ] [ 1001 ] ; static void Preprocess ( ) { arr [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 1000 ; ++ i ) { arr [ i ] [ 0 ] = 1 ; for ( int j = 1 ; j < i ; ++ j ) { arr [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ) % mod ; } arr [ i ] [ i ] = 1 ; } } static long powmod ( long a , long n ) { if ( n == 0 ) { return 1 ; } long pt = powmod ( a , n / 2 ) ; pt = ( pt * pt ) % mod ; if ( n % 2 == 1 ) { return ( pt * a ) % mod ; } else { return pt ; } } static long CountSubset ( int [ ] val , int n ) { long ans = powmod ( 2 , n - 1 ) ; Arrays . sort ( val ) ; for ( int i = 0 ; i < n ; ++ i ) { int j = i + 1 ; while ( j < n && val [ j ] == val [ i ] ) { int r = n - 1 - j ; int l = i ; ans = ( ans + arr [ l + r ] [ l ] ) % mod ; j ++ ; } } return ans ; } public static void main ( String [ ] args ) { Preprocess ( ) ; int val [ ] = { 2 , 3 , 2 } ; int n = val . length ; System . out . println ( CountSubset ( val , n ) ) ; } }
| mod = 1000000007 NEW_LINE arr = [ [ None for i in range ( 1001 ) ] for j in range ( 1001 ) ] NEW_LINE def Preprocess ( ) : NEW_LINE INDENT arr [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , 1001 ) : NEW_LINE INDENT arr [ i ] [ 0 ] = 1 NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT arr [ i ] [ j ] = ( arr [ i - 1 ] [ j - 1 ] + arr [ i - 1 ] [ j ] ) % mod NEW_LINE DEDENT arr [ i ] [ i ] = 1 NEW_LINE DEDENT DEDENT def powmod ( a , n ) : NEW_LINE INDENT if not n : NEW_LINE INDENT return 1 NEW_LINE DEDENT pt = powmod ( a , n // 2 ) NEW_LINE pt = ( pt * pt ) % mod NEW_LINE if n % 2 : NEW_LINE INDENT return ( pt * a ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT return pt NEW_LINE DEDENT DEDENT def CountSubset ( val , n ) : NEW_LINE INDENT ans = powmod ( 2 , n - 1 ) NEW_LINE val . sort ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < n and val [ j ] == val [ i ] : NEW_LINE INDENT r = n - 1 - j NEW_LINE l = i NEW_LINE ans = ( ans + arr [ l + r ] [ l ] ) % mod NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Preprocess ( ) NEW_LINE val = [ 2 , 3 , 2 ] NEW_LINE n = len ( val ) NEW_LINE print ( CountSubset ( val , n ) ) NEW_LINE DEDENT
|
T40015 | import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countStrings ( String s ) { int sum = 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == ' $ ' ) sum *= 21 ; else if ( s . charAt ( i ) == ' $ ' ) sum *= 5 ; } return sum ; } public static void main ( String args [ ] ) { String str = " s $ $ e $ " ; System . out . println ( countStrings ( str ) ) ; } }
| def countStrings ( s ) : NEW_LINE INDENT sum = 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( i % 2 == 0 and s [ i ] == ' $ ' ) : NEW_LINE INDENT sum *= 21 NEW_LINE DEDENT elif ( s [ i ] == ' $ ' ) : NEW_LINE INDENT sum *= 5 NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " s $ $ e $ " NEW_LINE print ( countStrings ( str ) ) NEW_LINE DEDENT
|
T40016 | public class GFG { static final int MAX_CHAR = 26 ; static boolean areKAnagrams ( String str1 , String str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int [ ] hash_str1 = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) hash_str1 [ str1 . charAt ( i ) - ' a ' ] ++ ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash_str1 [ str2 . charAt ( i ) - ' a ' ] > 0 ) hash_str1 [ str2 . charAt ( i ) - ' a ' ] -- ; else count ++ ; if ( count > k ) return false ; } return true ; } public static void main ( String args [ ] ) { String str1 = " fodr " ; String str2 = " gork " ; int k = 2 ; if ( areKAnagrams ( str1 , str2 , k ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| MAX_CHAR = 26 ; NEW_LINE def areKAnagrams ( str1 , str2 , k ) : NEW_LINE INDENT n = len ( str1 ) ; NEW_LINE if ( len ( str2 ) != n ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT hash_str1 = [ 0 ] * ( MAX_CHAR ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash_str1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( hash_str1 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] > 0 ) : NEW_LINE INDENT hash_str1 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT if ( count > k ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT str1 = " fodr " ; NEW_LINE str2 = " gork " ; NEW_LINE k = 2 ; NEW_LINE if ( areKAnagrams ( str1 , str2 , k ) == True ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT
|
T40017 | class GFG { public static void main ( String [ ] args ) { int a = 2 ; float b = 2.0f ; double c = 2.0003 ; char d = ' D ' ; System . out . printf ( " Integer β value β is β = β % d " , a ) ; System . out . printf ( " \n Float β value β is β = β % f " , b ) ; System . out . printf ( " \n Double β value β is β = β % f " , c ) ; System . out . printf ( " \n Char β value β is β = β % c " , d ) ; } }
| a = 2 NEW_LINE b = 2.0 NEW_LINE c = 2.0003 NEW_LINE d = ' D ' NEW_LINE print ( " Integer β value β is β = β " , a ) ; NEW_LINE print ( " \n Float β value β is β = β " , b ) ; NEW_LINE print ( " \n Double β value β is β = β " , c ) ; NEW_LINE print ( " \n Char β value β is β = β " , d ) ; NEW_LINE
|
T40018 | import java . util . * ; class GFG { static String maxValue ( char [ ] a , char [ ] b ) { Arrays . sort ( b ) ; int n = a . length ; int m = b . length ; int j = m - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( j < 0 ) break ; if ( b [ j ] > a [ i ] ) { a [ i ] = b [ j ] ; j -- ; } } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String a = "1234" ; String b = "4321" ; System . out . print ( maxValue ( a . toCharArray ( ) , b . toCharArray ( ) ) ) ; } }
| def maxValue ( a , b ) : NEW_LINE INDENT b = sorted ( b ) NEW_LINE bi = [ i for i in b ] NEW_LINE ai = [ i for i in a ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE j = m - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( j < 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( bi [ j ] > ai [ i ] ) : NEW_LINE INDENT ai [ i ] = bi [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT x = " " . join ( ai ) NEW_LINE return x NEW_LINE DEDENT a = "1234" NEW_LINE b = "4321" NEW_LINE print ( maxValue ( a , b ) ) NEW_LINE
|
T40019 | class GFG { static int printSum ( int a , int b ) { int res = 0 ; int temp1 = 0 , temp2 = 0 ; while ( a != 0 ) { temp1 = temp1 * 10 + ( a % 10 ) ; a /= 10 ; } a = temp1 ; while ( b != 0 ) { temp2 = temp2 * 10 + ( b % 10 ) ; b /= 10 ; } b = temp2 ; while ( a != 0 ) { int sum = ( a % 10 + b % 10 ) ; if ( sum / 10 == 0 ) { res = res * 10 + sum ; } else { temp1 = 0 ; while ( sum != 0 ) { temp1 = temp1 * 10 + ( sum % 10 ) ; sum /= 10 ; } sum = temp1 ; while ( sum != 0 ) { res = res * 10 + ( sum % 10 ) ; sum /= 10 ; } } a /= 10 ; b /= 10 ; } return res ; } public static void main ( String [ ] args ) { int a = 7752 , b = 8834 ; System . out . println ( printSum ( a , b ) ) ; } }
| def printSum ( a , b ) : NEW_LINE INDENT res , temp1 , temp2 = 0 , 0 , 0 NEW_LINE while a > 0 : NEW_LINE INDENT temp1 = temp1 * 10 + ( a % 10 ) NEW_LINE a //= 10 NEW_LINE DEDENT a = temp1 NEW_LINE while b > 0 : NEW_LINE INDENT temp2 = temp2 * 10 + ( b % 10 ) NEW_LINE b //= 10 NEW_LINE DEDENT b = temp2 NEW_LINE while a : NEW_LINE INDENT Sum = a % 10 + b % 10 NEW_LINE if Sum // 10 == 0 : NEW_LINE INDENT res = res * 10 + Sum NEW_LINE DEDENT else : NEW_LINE INDENT temp1 = 0 NEW_LINE while Sum > 0 : NEW_LINE INDENT temp1 = temp1 * 10 + ( Sum % 10 ) NEW_LINE Sum //= 10 NEW_LINE DEDENT Sum = temp1 NEW_LINE while Sum > 0 : NEW_LINE INDENT res = res * 10 + ( Sum % 10 ) NEW_LINE Sum //= 10 NEW_LINE DEDENT DEDENT a //= 10 NEW_LINE b //= 10 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 7752 , 8834 NEW_LINE print ( printSum ( a , b ) ) NEW_LINE DEDENT
|
T40020 | import java . util . Arrays ; public class GFG { static String removeDupsSorted ( String str ) { int res_ind = 1 , ip_ind = 1 ; char arr [ ] = str . toCharArray ( ) ; while ( ip_ind != arr . length ) { if ( arr [ ip_ind ] != arr [ ip_ind - 1 ] ) { arr [ res_ind ] = arr [ ip_ind ] ; res_ind ++ ; } ip_ind ++ ; } str = new String ( arr ) ; return str . substring ( 0 , res_ind ) ; } static String removeDups ( String str ) { char temp [ ] = str . toCharArray ( ) ; Arrays . sort ( temp ) ; str = new String ( temp ) ; return removeDupsSorted ( str ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( removeDups ( str ) ) ; } }
| def toMutable ( string ) : NEW_LINE INDENT temp = [ ] NEW_LINE for x in string : NEW_LINE INDENT temp . append ( x ) NEW_LINE DEDENT return temp NEW_LINE DEDENT def toString ( List ) : NEW_LINE INDENT return ' ' . join ( List ) NEW_LINE DEDENT def removeDupsSorted ( List ) : NEW_LINE INDENT res_ind = 1 NEW_LINE ip_ind = 1 NEW_LINE while ip_ind != len ( List ) : NEW_LINE INDENT if List [ ip_ind ] != List [ ip_ind - 1 ] : NEW_LINE INDENT List [ res_ind ] = List [ ip_ind ] NEW_LINE res_ind += 1 NEW_LINE DEDENT ip_ind += 1 NEW_LINE DEDENT string = toString ( List [ 0 : res_ind ] ) NEW_LINE return string NEW_LINE DEDENT def removeDups ( string ) : NEW_LINE INDENT List = toMutable ( string ) NEW_LINE List . sort ( ) NEW_LINE return removeDupsSorted ( List ) NEW_LINE DEDENT string = " geeksforgeeks " NEW_LINE print removeDups ( string ) NEW_LINE
|
T40021 | class GFG { static float rectanglearea ( float r ) { if ( r < 0 ) return - 1 ; float a = r * r ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( ( int ) rectanglearea ( r ) ) ; } }
| def rectanglearea ( r ) : NEW_LINE INDENT if r < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = r * r NEW_LINE return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 NEW_LINE print ( rectanglearea ( r ) ) NEW_LINE DEDENT
|
T40022 | import java . util . * ; class solution { static void MakePreSum ( int [ ] arr , int [ ] presum , int n ) { presum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) presum [ i ] = presum [ i - 1 ] + arr [ i ] ; } static int BinaryLifting ( int [ ] presum , int n , int x ) { int pos = 0 ; int LOGN = ( int ) Math . log ( n ) ; if ( x <= presum [ 0 ] ) return 0 ; for ( int i = LOGN ; i >= 0 ; i -- ) { if ( pos + ( 1 << i ) < n && presum [ pos + ( 1 << i ) ] < x ) { pos += ( 1 << i ) ; } } return pos + 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 5 , 7 , 1 , 6 , 9 , 12 , 4 , 6 } ; int x = 8 ; int n = arr . length ; int [ ] presum = new int [ n ] ; Arrays . fill ( presum , 0 ) ; MakePreSum ( arr , presum , n ) ; System . out . println ( BinaryLifting ( presum , n , x ) ) ; } }
| import math NEW_LINE def MakePreSum ( arr , presum , n ) : NEW_LINE INDENT presum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT presum [ i ] = presum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT DEDENT def BinaryLifting ( presum , n , x ) : NEW_LINE INDENT pos = 0 NEW_LINE LOGN = int ( math . log2 ( n ) ) NEW_LINE if ( x <= presum [ 0 ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( LOGN , - 1 , - 1 ) : NEW_LINE INDENT if ( pos + ( 1 << i ) < n and presum [ pos + ( 1 << i ) ] < x ) : NEW_LINE INDENT pos += ( 1 << i ) NEW_LINE DEDENT DEDENT return pos + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 5 , 7 , 1 , 6 , 9 , 12 , 4 , 6 ] NEW_LINE x = 8 NEW_LINE n = len ( arr ) NEW_LINE presum = [ 0 ] * n NEW_LINE MakePreSum ( arr , presum , n ) NEW_LINE print ( BinaryLifting ( presum , n , x ) ) NEW_LINE DEDENT
|
T40023 | class GFG { static boolean isPalindrome ( String str , int i , int j ) { while ( i < j ) { if ( str . charAt ( i ) != str . charAt ( j ) ) { return false ; } i ++ ; j -- ; } return true ; } static int maxLenPalindrome ( String str , int n , char ch ) { int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ch ) { for ( int j = n - 1 ; j >= i ; j -- ) { if ( str . charAt ( j ) == ch ) { if ( isPalindrome ( str , i , j ) ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; break ; } } } } } return maxLen ; } public static void main ( String [ ] args ) { String str = " lapqooqpqpl " ; int n = str . length ( ) ; char ch = ' p ' ; System . out . println ( maxLenPalindrome ( str , n , ch ) ) ; } }
| def isPalindrome ( str , i , j ) : NEW_LINE INDENT while ( i < j ) : NEW_LINE INDENT if ( str [ i ] != str [ j ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def maxLenPalindrome ( str , n , ch ) : NEW_LINE INDENT maxLen = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ch ) : NEW_LINE INDENT for j in range ( n - 1 , i + 1 , - 1 ) : NEW_LINE INDENT if ( str [ j ] == ch ) : NEW_LINE INDENT if ( isPalindrome ( str , i , j ) ) : NEW_LINE INDENT maxLen = max ( maxLen , j - i + 1 ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return maxLen ; NEW_LINE DEDENT str = " lapqooqpqpl " ; NEW_LINE n = len ( str ) ; NEW_LINE ch = ' p ' ; NEW_LINE print ( maxLenPalindrome ( str , n , ch ) ) ; NEW_LINE
|
T40024 | class GFG { static int calculateLeaps ( int n ) { if ( n == 0 || n == 1 ) { return 1 ; } else { int leaps = 0 ; for ( int i = 0 ; i < n ; i ++ ) leaps += calculateLeaps ( i ) ; return leaps ; } } public static void main ( String [ ] args ) { System . out . println ( calculateLeaps ( 4 ) ) ; } }
| def calculateLeaps ( n ) : NEW_LINE INDENT if n == 0 or n == 1 : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT leaps = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT leaps = leaps + calculateLeaps ( i ) ; NEW_LINE DEDENT return leaps ; NEW_LINE DEDENT DEDENT print ( calculateLeaps ( 4 ) ) ; NEW_LINE
|
T40025 | import java . util . * ; class GFG { static ArrayList < Integer > seive ( int n ) { ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime . add ( 0 ) ; int p = 2 ; while ( p * p <= n ) { if ( prime . get ( p ) == 0 ) { for ( int i = 2 * p ; i < n + 1 ; i += p ) prime . set ( i , 1 ) ; } p += 1 ; } ArrayList < Integer > allPrimes = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( prime . get ( i ) == 0 ) allPrimes . add ( i ) ; } return allPrimes ; } static ArrayList < Integer > distPrime ( ArrayList < Integer > arr , ArrayList < Integer > allPrimes ) { ArrayList < Integer > list1 = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < allPrimes . size ( ) ; i ++ ) { for ( int j = 0 ; j < arr . size ( ) ; j ++ ) { if ( arr . get ( j ) % allPrimes . get ( i ) == 0 ) { list1 . add ( allPrimes . get ( i ) ) ; break ; } } } return list1 ; } public static void main ( String args [ ] ) { ArrayList < Integer > allPrimes = new ArrayList < Integer > ( seive ( 10000 ) ) ; ArrayList < Integer > arr = new ArrayList < Integer > ( ) ; arr . add ( 15 ) ; arr . add ( 30 ) ; arr . add ( 60 ) ; ArrayList < Integer > ans = new ArrayList < Integer > ( distPrime ( arr , allPrimes ) ) ; System . out . print ( " [ " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " β " ) ; System . out . print ( " ] " ) ; } }
| def seive ( n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT allPrimes = [ x for x in range ( 2 , n ) if prime [ x ] ] NEW_LINE return allPrimes NEW_LINE DEDENT def distPrime ( arr , allPrimes ) : NEW_LINE INDENT list1 = list ( ) NEW_LINE for i in allPrimes : NEW_LINE INDENT for j in arr : NEW_LINE INDENT if ( j % i == 0 ) : NEW_LINE INDENT list1 . append ( i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT return list1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT allPrimes = seive ( 10000 ) NEW_LINE arr = [ 15 , 30 , 60 ] NEW_LINE ans = distPrime ( arr , allPrimes ) NEW_LINE print ( ans ) NEW_LINE DEDENT
|
T40026 | import java . io . * ; class GFG { static int per ( int n ) { if ( n == 0 ) return 3 ; if ( n == 1 ) return 0 ; if ( n == 2 ) return 2 ; return per ( n - 2 ) + per ( n - 3 ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( per ( n ) ) ; } }
| def per ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 3 ; NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT return per ( n - 2 ) + per ( n - 3 ) ; NEW_LINE DEDENT n = 9 ; NEW_LINE print ( per ( n ) ) ; NEW_LINE
|
T40027 | class GFG { static int findMax ( int arr [ ] , int low , int high ) { if ( high < low ) return arr [ 0 ] ; if ( high == low ) return arr [ low ] ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return arr [ mid ] ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return arr [ mid - 1 ] ; } if ( arr [ low ] > arr [ mid ] ) { return findMax ( arr , low , mid - 1 ) ; } else { return findMax ( arr , mid + 1 , high ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( findMax ( arr , 0 , n - 1 ) ) ; } }
| def findMax ( arr , low , high ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT if ( high == low ) : NEW_LINE INDENT return arr [ low ] NEW_LINE DEDENT mid = low + ( high - low ) // 2 NEW_LINE if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) : NEW_LINE INDENT return arr [ mid ] NEW_LINE DEDENT if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) : NEW_LINE INDENT return arr [ mid - 1 ] NEW_LINE DEDENT if ( arr [ low ] > arr [ mid ] ) : NEW_LINE INDENT return findMax ( arr , low , mid - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return findMax ( arr , mid + 1 , high ) NEW_LINE DEDENT DEDENT arr = [ 5 , 6 , 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMax ( arr , 0 , n - 1 ) ) NEW_LINE
|
T40028 | class GFG { static void printGP ( int a , int r , int n ) { int curr_term ; for ( int i = 0 ; i < n ; i ++ ) { curr_term = a * ( int ) Math . pow ( r , i ) ; System . out . print ( curr_term + " β " ) ; } } public static void main ( String [ ] args ) { int a = 2 ; int r = 3 ; int n = 5 ; printGP ( a , r , n ) ; } }
| def printGP ( a , r , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT curr_term = a * pow ( r , i ) NEW_LINE print ( curr_term , end = " β " ) NEW_LINE DEDENT DEDENT a = 2 NEW_LINE r = 3 NEW_LINE n = 5 NEW_LINE printGP ( a , r , n ) NEW_LINE
|
T40029 | class GFG { static int Minimum_Operations ( int [ ] a , int n ) { int [ ] np = new int [ n + 1 ] ; np [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { np [ i ] = np [ i + 1 ] ; if ( a [ i ] <= 0 ) np [ i ] ++ ; } int pos = 0 ; int ans = n ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( a [ i ] >= 0 ) pos ++ ; ans = Math . min ( ans , pos + np [ i + 1 ] ) ; } return ans ; } public static void main ( String args [ ] ) { int [ ] a = { - 1 , 0 , 1 , 2 } ; int n = a . length ; System . out . print ( Minimum_Operations ( a , n ) ) ; } }
| def Minimum_Operations ( a , n ) : NEW_LINE INDENT np = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT np [ i ] = np [ i + 1 ] NEW_LINE if ( a [ i ] <= 0 ) : NEW_LINE INDENT np [ i ] += 1 NEW_LINE DEDENT DEDENT pos = 0 NEW_LINE ans = n NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT pos += 1 NEW_LINE DEDENT ans = min ( ans , pos + np [ i + 1 ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ - 1 , 0 , 1 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( Minimum_Operations ( a , n ) ) NEW_LINE
|
T40030 | import java . util . ArrayList ; import java . util . Collections ; class GFG { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { ArrayList V = new ArrayList ( ) ; for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { V . add ( A [ j ] ) ; } } Collections . sort ( V ) ; return ( ( int ) V . get ( K - 1 ) ) ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . length ; System . out . println ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }
| def KthMinValAfterMconcatenate ( A , N , M , K ) : NEW_LINE INDENT V = [ ] NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT V . append ( A [ j ] ) NEW_LINE DEDENT DEDENT V . sort ( ) NEW_LINE return V [ K - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 1 , 2 ] NEW_LINE M , K = 3 , 4 NEW_LINE N = len ( A ) NEW_LINE print ( KthMinValAfterMconcatenate ( A , N , M , K ) ) NEW_LINE DEDENT
|
T40031 | import java . util . * ; class GFG { public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int i ; for ( i = 0 ; i < 5 ; i ++ ) System . out . printf ( " % d β " , a [ i ] ) ; } }
| a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE for i in range ( 5 ) : NEW_LINE INDENT print ( a [ i ] , end = " β " ) ; NEW_LINE DEDENT
|
T40032 | import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int edgeCover ( int n ) { int result = 0 ; result = ( int ) Math . ceil ( ( double ) n / 2.0 ) ; return result ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . print ( edgeCover ( n ) ) ; } }
| import math NEW_LINE def edgeCover ( n ) : NEW_LINE INDENT result = 0 NEW_LINE result = math . ceil ( n / 2.0 ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE print ( int ( edgeCover ( n ) ) ) NEW_LINE DEDENT
|
T40033 | import java . util . * ; class GFG { static void findTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = sum - ( arr [ i ] + arr [ j ] ) ; if ( s . contains ( x ) ) System . out . printf ( " % d β % d β % d \n " , x , arr [ i ] , arr [ j ] ) ; else s . add ( arr [ j ] ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . length ; findTriplets ( arr , n , sum ) ; } }
| import math as mt NEW_LINE def findTriplets ( arr , n , Sum ) : NEW_LINE INDENT for i in range ( n - 1 ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT x = Sum - ( arr [ i ] + arr [ j ] ) NEW_LINE if x in s . keys ( ) : NEW_LINE INDENT print ( x , arr [ i ] , arr [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT s [ arr [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] NEW_LINE Sum = - 2 NEW_LINE n = len ( arr ) NEW_LINE findTriplets ( arr , n , Sum ) NEW_LINE
|
T40034 | class GFG { static final int N = 3 , M = 3 ; static boolean check ( int a [ ] [ ] , int b [ ] [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) { a [ i ] [ j ] ^= 1 ; a [ 0 ] [ 0 ] ^= 1 ; a [ 0 ] [ j ] ^= 1 ; a [ i ] [ 0 ] ^= 1 ; } } } for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] != b [ i ] [ j ] ) return false ; } } return true ; } public static void main ( String args [ ] ) { int a [ ] [ ] = { { 0 , 1 , 0 } , { 0 , 1 , 0 } , { 1 , 0 , 0 } } ; int b [ ] [ ] = { { 1 , 0 , 0 } , { 1 , 0 , 0 } , { 1 , 0 , 0 } } ; if ( check ( a , b ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
| N = 3 NEW_LINE M = 3 NEW_LINE def check ( a , b ) : NEW_LINE INDENT for i in range ( 1 , N , 1 ) : NEW_LINE INDENT for j in range ( 1 , M , 1 ) : NEW_LINE INDENT if ( a [ i ] [ j ] != b [ i ] [ j ] ) : NEW_LINE INDENT a [ i ] [ j ] ^= 1 NEW_LINE a [ 0 ] [ 0 ] ^= 1 NEW_LINE a [ 0 ] [ j ] ^= 1 NEW_LINE a [ i ] [ 0 ] ^= 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( a [ i ] [ j ] != b [ i ] [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ [ 0 , 1 , 0 ] , [ 0 , 1 , 0 ] , [ 1 , 0 , 0 ] ] NEW_LINE b = [ [ 1 , 0 , 0 ] , [ 1 , 0 , 0 ] , [ 1 , 0 , 0 ] ] NEW_LINE if ( check ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
|
T40035 | import java . util . * ; class Solution { static String OctToBin ( String octnum ) { long i = 0 ; String binary = " " ; while ( i < octnum . length ( ) ) { char c = octnum . charAt ( ( int ) i ) ; switch ( c ) { case '0' : binary += "000" ; break ; case '1' : binary += "001" ; break ; case '2' : binary += "010" ; break ; case '3' : binary += "011" ; break ; case '4' : binary += "100" ; break ; case '5' : binary += "101" ; break ; case '6' : binary += "110" ; break ; case '7' : binary += "111" ; break ; default : System . out . println ( " \n Invalid β Octal β Digit β " + octnum . charAt ( ( int ) i ) ) ; break ; } i ++ ; } return binary ; } public static void main ( String args [ ] ) { String octnum = "345" ; System . out . println ( " Equivalent β Binary β Value β = β " + OctToBin ( octnum ) ) ; } }
| def OctToBin ( octnum ) : NEW_LINE INDENT binary = " " NEW_LINE while octnum != 0 : NEW_LINE INDENT d = int ( octnum % 10 ) NEW_LINE if d == 0 : NEW_LINE INDENT binary = " " . join ( [ "000" , binary ] ) NEW_LINE DEDENT elif d == 1 : NEW_LINE INDENT binary = " " . join ( [ "001" , binary ] ) NEW_LINE DEDENT elif d == 2 : NEW_LINE INDENT binary = " " . join ( [ "010" , binary ] ) NEW_LINE DEDENT elif d == 3 : NEW_LINE INDENT binary = " " . join ( [ "011" , binary ] ) NEW_LINE DEDENT elif d == 4 : NEW_LINE INDENT binary = " " . join ( [ "100" , binary ] ) NEW_LINE DEDENT elif d == 5 : NEW_LINE INDENT binary = " " . join ( [ "101" , binary ] ) NEW_LINE DEDENT elif d == 6 : NEW_LINE INDENT binary = " " . join ( [ "110" , binary ] ) NEW_LINE DEDENT elif d == 7 : NEW_LINE INDENT binary = " " . join ( [ "111" , binary ] ) NEW_LINE DEDENT else : NEW_LINE INDENT binary = " Invalid β Octal β Digit " NEW_LINE break NEW_LINE DEDENT octnum = int ( octnum / 10 ) NEW_LINE DEDENT return binary NEW_LINE DEDENT octnum = 345 NEW_LINE final_bin = " " + OctToBin ( octnum ) NEW_LINE print ( " Equivalent β Binary β Value β = " , final_bin ) NEW_LINE
|
T40036 | import java . io . * ; class GFG { static int findNumber ( int n ) { int num = n - 1 ; num = 2 * ( int ) Math . pow ( 4 , num ) ; num = ( int ) Math . floor ( num / 3.0 ) ; return num ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( findNumber ( n ) ) ; } }
| def findNumber ( n ) : NEW_LINE INDENT num = n - 1 ; NEW_LINE num = 2 * ( 4 ** num ) ; NEW_LINE num = num // 3 ; NEW_LINE return num ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( findNumber ( n ) ) ; NEW_LINE DEDENT
|
T40037 | import java . io . * ; public class GFG { static boolean allCharactersSame ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s . charAt ( i ) != s . charAt ( 0 ) ) return false ; return true ; } static public void main ( String [ ] args ) { String s = " aaa " ; if ( allCharactersSame ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def allCharactersSame ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if s [ i ] != s [ 0 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aaa " NEW_LINE if allCharactersSame ( s ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
|
T40038 | import java . io . * ; class GFG { static int findevenPair ( int [ ] A , int N ) { int i , j ; int evenPair = 0 ; for ( i = 0 ; i < N ; i ++ ) { for ( j = i + 1 ; j < N ; j ++ ) { if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) evenPair ++ ; } } return evenPair ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 4 , 7 , 2 , 1 } ; int N = A . length ; System . out . println ( findevenPair ( A , N ) ) ; } }
| def findevenPair ( A , N ) : NEW_LINE INDENT evenPair = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( ( A [ i ] ^ A [ j ] ) % 2 == 0 ) : NEW_LINE INDENT evenPair += 1 NEW_LINE DEDENT DEDENT DEDENT return evenPair ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT A = [ 5 , 4 , 7 , 2 , 1 ] NEW_LINE N = len ( A ) NEW_LINE print ( findevenPair ( A , N ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT
|
T40039 | import java . io . * ; import java . util . * ; class GFG { static int Sum ( int N ) { int SumOfPrimeDivisors [ ] = new int [ N + 1 ] ; for ( int i = 2 ; i <= N ; ++ i ) { if ( SumOfPrimeDivisors [ i ] == 0 ) { for ( int j = i ; j <= N ; j += i ) { SumOfPrimeDivisors [ j ] += i ; } } } return SumOfPrimeDivisors [ N ] ; } public static void main ( String args [ ] ) { int N = 60 ; System . out . print ( " Sum β of β prime β " + " divisors β of β 60 β is β " + Sum ( N ) + " \n " ) ; } }
| def Sum ( N ) : NEW_LINE INDENT SumOfPrimeDivisors = [ 0 ] * ( N + 1 ) NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( SumOfPrimeDivisors [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i , N + 1 , i ) : NEW_LINE INDENT SumOfPrimeDivisors [ j ] += i NEW_LINE DEDENT DEDENT DEDENT return SumOfPrimeDivisors [ N ] NEW_LINE DEDENT N = 60 NEW_LINE print ( " Sum β of β prime " , " divisors β of β 60 β is " , Sum ( N ) ) ; NEW_LINE
|
T40040 | class GFG { final static int MAX_SIZE = 10 ; static void convolution ( int [ ] x , int [ ] h , int n , int m ) { int row_vec [ ] = new int [ MAX_SIZE ] ; int col_vec [ ] = new int [ MAX_SIZE ] ; int out [ ] = new int [ MAX_SIZE ] ; int circular_shift_mat [ ] [ ] = new int [ MAX_SIZE ] [ MAX_SIZE ] ; int maxSize = n > m ? n : m ; for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= n ) { row_vec [ i ] = 0 ; } else { row_vec [ i ] = x [ i ] ; } } for ( int i = 0 ; i < maxSize ; i ++ ) { if ( i >= m ) { col_vec [ i ] = 0 ; } else { col_vec [ i ] = h [ i ] ; } } int k = 0 , d = 0 ; for ( int i = 0 ; i < maxSize ; i ++ ) { int curIndex = k - d ; for ( int j = 0 ; j < maxSize ; j ++ ) { circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; curIndex ++ ; } k = maxSize ; d ++ ; } for ( int i = 0 ; i < maxSize ; i ++ ) { for ( int j = 0 ; j < maxSize ; j ++ ) { out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; } System . out . print ( out [ i ] + " β " ) ; } } public static void main ( String [ ] args ) { int x [ ] = { 5 , 7 , 3 , 2 } ; int n = x . length ; int h [ ] = { 1 , 5 } ; int m = h . length ; convolution ( x , h , n , m ) ; } }
| MAX_SIZE = 10 ; NEW_LINE def convolution ( x , h , n , m ) : NEW_LINE INDENT row_vec = [ 0 ] * MAX_SIZE ; NEW_LINE col_vec = [ 0 ] * MAX_SIZE ; NEW_LINE out = [ 0 ] * MAX_SIZE ; NEW_LINE circular_shift_mat = [ [ 0 for i in range ( MAX_SIZE ) ] for j in range ( MAX_SIZE ) ] ; NEW_LINE if ( n > m ) : NEW_LINE INDENT maxSize = n ; NEW_LINE DEDENT else : NEW_LINE INDENT maxSize = m ; NEW_LINE DEDENT for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT row_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT row_vec [ i ] = x [ i ] ; NEW_LINE DEDENT DEDENT for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= m ) : NEW_LINE INDENT col_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT col_vec [ i ] = h [ i ] ; NEW_LINE DEDENT DEDENT k = 0 ; NEW_LINE d = 0 ; NEW_LINE for i in range ( maxSize ) : NEW_LINE INDENT curIndex = k - d ; NEW_LINE for j in range ( maxSize ) : NEW_LINE INDENT circular_shift_mat [ j ] [ i ] = \ NEW_LINE row_vec [ curIndex % maxSize ] ; NEW_LINE curIndex += 1 ; NEW_LINE DEDENT k = maxSize ; NEW_LINE d += 1 ; NEW_LINE DEDENT for i in range ( maxSize ) : NEW_LINE INDENT for j in range ( maxSize ) : NEW_LINE INDENT out [ i ] += circular_shift_mat [ i ] [ j ] * \ NEW_LINE INDENT col_vec [ j ] ; NEW_LINE DEDENT DEDENT print ( out [ i ] , end = " β " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = [ 5 , 7 , 3 , 2 ] ; NEW_LINE n = len ( x ) ; NEW_LINE h = [ 1 , 5 ] ; NEW_LINE m = len ( h ) ; NEW_LINE convolution ( x , h , n , m ) ; NEW_LINE DEDENT
|
T40041 | import java . io . * ; class GFG { static int minimumChar ( String S1 , String S2 ) { int n = S1 . length ( ) ; int m = S2 . length ( ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < m - n + 1 ; i ++ ) { int minRemovedChar = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( S1 . charAt ( j ) != S2 . charAt ( i + j ) ) { minRemovedChar ++ ; } } ans = Math . min ( minRemovedChar , ans ) ; } return ans ; } public static void main ( String [ ] args ) { String S1 = " abc " ; String S2 = " paxzk " ; System . out . println ( minimumChar ( S1 , S2 ) ) ; } }
| import sys NEW_LINE def minimumChar ( S1 , S2 ) : NEW_LINE INDENT n , m = len ( S1 ) , len ( S2 ) NEW_LINE ans = sys . maxsize NEW_LINE for i in range ( m - n + 1 ) : NEW_LINE INDENT minRemovedChar = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( S1 [ j ] != S2 [ i + j ] ) : NEW_LINE INDENT minRemovedChar += 1 NEW_LINE DEDENT DEDENT ans = min ( minRemovedChar , ans ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S1 = " abc " NEW_LINE S2 = " paxzk " NEW_LINE print ( minimumChar ( S1 , S2 ) ) NEW_LINE DEDENT
|
T40042 | class GFG { static String permutation ( int arr [ ] , int N ) { int [ ] hash = new int [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 1 ; i <= N ; i ++ ) { if ( hash [ i ] != 1 ) return " No " ; } return " Yes " ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 5 , 5 , 3 } ; int n = arr . length ; System . out . print ( permutation ( arr , n ) + " \n " ) ; } }
| def permutation ( arr , N ) : NEW_LINE INDENT hash = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT hash [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( hash [ i ] != 1 ) : NEW_LINE INDENT return " No " ; NEW_LINE DEDENT DEDENT return " Yes " ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 5 , 5 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( permutation ( arr , n ) ) ; NEW_LINE DEDENT
|
T40043 | class GFG { static double areaCube ( double a ) { return ( a * a * a ) ; } static double surfaceCube ( double a ) { return ( 6 * a * a ) ; } public static void main ( String [ ] args ) { double a = 5 ; System . out . println ( " Area β = β " + areaCube ( a ) ) ; System . out . println ( " Total β surface β area β = β " + surfaceCube ( a ) ) ; } }
| def areaCube ( a ) : NEW_LINE INDENT return ( a * a * a ) NEW_LINE DEDENT def surfaceCube ( a ) : NEW_LINE INDENT return ( 6 * a * a ) NEW_LINE DEDENT a = 5 NEW_LINE print ( " Area β = " , areaCube ( a ) ) NEW_LINE print ( " Total β surface β area β = " , surfaceCube ( a ) ) NEW_LINE
|
T40044 | import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { return k * k * k ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }
| def kthgroupsum ( k ) : NEW_LINE INDENT return k * k * k NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE
|
T40045 | class GFG { static boolean EqualNumbers ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] % 2 == 0 ) { a [ i ] /= 2 ; } while ( a [ i ] % 3 == 0 ) { a [ i ] /= 3 ; } if ( a [ i ] != a [ 0 ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 50 , 75 , 150 } ; int n = a . length ; if ( EqualNumbers ( a , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
| def EqualNumbers ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT while a [ i ] % 2 == 0 : NEW_LINE INDENT a [ i ] //= 2 NEW_LINE DEDENT while a [ i ] % 3 == 0 : NEW_LINE INDENT a [ i ] //= 3 NEW_LINE DEDENT if a [ i ] != a [ 0 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 50 , 75 , 150 ] NEW_LINE n = len ( a ) NEW_LINE if EqualNumbers ( a , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
|
T40046 | import java . io . * ; class GFG { static int find_sum ( int n , int k ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int power = k ; while ( power <= n ) { total_sum -= power ; power *= k ; } return total_sum ; } public static void main ( String [ ] args ) { int n = 11 , k = 2 ; System . out . println ( find_sum ( n , k ) ) ; } }
| def find_sum ( n , k ) : NEW_LINE INDENT total_sum = ( n * ( n + 1 ) ) // 2 NEW_LINE power = k NEW_LINE while power <= n : NEW_LINE INDENT total_sum -= power NEW_LINE power *= k NEW_LINE DEDENT return total_sum NEW_LINE DEDENT n = 11 ; k = 2 NEW_LINE print ( find_sum ( n , k ) ) NEW_LINE
|
T40047 | class GFG { static void reverseArray ( int arr [ ] , int n ) { int x = ( Integer . MIN_VALUE / Integer . MAX_VALUE ) ; for ( int i = 0 ; i < n / 2 ; i ++ ) swap ( arr , i , n + ( x * i ) + x ) ; } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 7 , 2 , 1 , 6 } ; int n = arr . length ; reverseArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " β " ) ; } }
| def reverseArray ( arr , n ) : NEW_LINE INDENT import sys NEW_LINE x = - sys . maxsize // sys . maxsize NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT arr [ i ] , arr [ n + ( x * i ) + x ] = arr [ n + ( x * i ) + x ] , arr [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 7 , 2 , 1 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE reverseArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT DEDENT
|
T40048 | import java . io . * ; import java . lang . * ; class GfG { public static int findLCM ( int a , int b ) { int lar = Math . max ( a , b ) ; int small = Math . min ( a , b ) ; for ( int i = lar ; ; i += lar ) { if ( i % small == 0 ) return i ; } } public static void main ( String [ ] argc ) { int a = 5 , b = 7 ; System . out . println ( " LCM β of β " + a + " β and β " + b + " β is β " + findLCM ( a , b ) ) ; } }
| import sys NEW_LINE def findLCM ( a , b ) : NEW_LINE INDENT lar = max ( a , b ) NEW_LINE small = min ( a , b ) NEW_LINE i = lar NEW_LINE while ( 1 ) : NEW_LINE INDENT if ( i % small == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT i += lar NEW_LINE DEDENT DEDENT a = 5 NEW_LINE b = 7 NEW_LINE print ( " LCM β of β " , a , " β and β " , b , " β is β " , findLCM ( a , b ) , sep = " " ) NEW_LINE
|
T40049 | class GFG { static int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ k ] [ n ] , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 ] [ i ] = 1 ; } for ( int l = 1 ; l < k ; l ++ ) { for ( int i = l ; i < n ; i ++ ) { dp [ l ] [ i ] = 0 ; for ( int j = l - 1 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] ) { dp [ l ] [ i ] += dp [ l - 1 ] [ j ] ; } } } } for ( int i = k - 1 ; i < n ; i ++ ) { sum += dp [ k - 1 ] [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 } ; int n = arr . length ; int k = 4 ; System . out . print ( " Number β of β Increasing β Subsequences β of β size β " + k + " β = β " + numOfIncSubseqOfSizeK ( arr , n , k ) ) ; } }
| import math as mt NEW_LINE def numOfIncSubseqOfSizeK ( arr , n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 NEW_LINE DEDENT for l in range ( 1 , k ) : NEW_LINE INDENT for i in range ( l , n ) : NEW_LINE INDENT dp [ l ] [ i ] = 0 NEW_LINE for j in range ( l - 1 , i ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT dp [ l ] [ i ] += dp [ l - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT Sum = 0 NEW_LINE for i in range ( k - 1 , n ) : NEW_LINE INDENT Sum += dp [ k - 1 ] [ i ] NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 12 , 8 , 11 , 13 , 10 , 15 , 14 , 16 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE print ( " Number β of β Increasing β Subsequences β of β size " , k , " = " , numOfIncSubseqOfSizeK ( arr , n , k ) ) NEW_LINE
|
T40050 | class GFG { static int frequencyDigits ( int n , int d ) { int c = 0 ; while ( n > 0 ) { if ( n % 10 == d ) c ++ ; n = n / 10 ; } return c ; } public static void main ( String args [ ] ) { int N = 1122322 ; int D = 2 ; System . out . println ( frequencyDigits ( N , D ) ) ; } }
| def frequencyDigits ( n , d ) : NEW_LINE INDENT c = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n % 10 == d ) : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT n = int ( n / 10 ) ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT N = 1122322 ; NEW_LINE D = 2 ; NEW_LINE print ( frequencyDigits ( N , D ) ) ; NEW_LINE
|
T40051 | import java . io . * ; import java . util . * ; class GFG { static int maxOfSegmentMins ( int [ ] a , int n , int k ) { if ( k == 1 ) { Arrays . sort ( a ) ; return a [ 0 ] ; } if ( k == 2 ) return Math . max ( a [ 0 ] , a [ n - 1 ] ) ; return a [ n - 1 ] ; } static public void main ( String [ ] args ) { int [ ] a = { - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 } ; int n = a . length ; int k = 2 ; System . out . println ( maxOfSegmentMins ( a , n , k ) ) ; } }
| def maxOfSegmentMins ( a , n , k ) : NEW_LINE INDENT if k == 1 : NEW_LINE INDENT return min ( a ) NEW_LINE DEDENT if k == 2 : NEW_LINE INDENT return max ( a [ 0 ] , a [ n - 1 ] ) NEW_LINE DEDENT return max ( a ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ - 10 , - 9 , - 8 , 2 , 7 , - 6 , - 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 2 NEW_LINE print ( maxOfSegmentMins ( a , n , k ) ) NEW_LINE DEDENT
|
T40052 | public class Test { public static int findMajority ( int arr [ ] , int n ) { return arr [ n / 2 ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; System . out . println ( findMajority ( arr , n ) ) ; } }
| def findMajority ( arr , n ) : NEW_LINE INDENT return arr [ int ( n / 2 ) ] NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMajority ( arr , n ) ) NEW_LINE
|
T40053 | class GFG { static String prevNum ( String str ) { int len = str . length ( ) ; int index = - 1 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) > str . charAt ( i + 1 ) ) { index = i ; break ; } } int smallGreatDgt = - 1 ; for ( int i = len - 1 ; i > index ; i -- ) { if ( str . charAt ( i ) < str . charAt ( index ) ) { if ( smallGreatDgt == - 1 ) { smallGreatDgt = i ; } else if ( str . charAt ( i ) >= str . charAt ( smallGreatDgt ) ) { smallGreatDgt = i ; } } } if ( index == - 1 ) { return " - 1" ; } if ( smallGreatDgt != - 1 ) { str = swap ( str , index , smallGreatDgt ) ; return str ; } return " - 1" ; } static String swap ( String str , int i , int j ) { char ch [ ] = str . toCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . valueOf ( ch ) ; } public static void main ( String [ ] args ) { String str = "34125" ; System . out . println ( prevNum ( str ) ) ; } }
| import sys NEW_LINE def prevNum ( string , n ) : NEW_LINE INDENT index = - 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if int ( string [ i ] ) > int ( string [ i + 1 ] ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT smallGreatDgt = - 1 NEW_LINE for i in range ( n - 1 , index , - 1 ) : NEW_LINE INDENT if ( smallGreatDgt == - 1 and int ( string [ i ] ) < int ( string [ index ] ) ) : NEW_LINE INDENT smallGreatDgt = i NEW_LINE DEDENT elif ( index > - 1 and int ( string [ i ] ) >= int ( string [ smallGreatDgt ] ) and int ( string [ i ] ) < int ( string [ index ] ) ) : NEW_LINE INDENT smallGreatDgt = i NEW_LINE DEDENT DEDENT if index == - 1 : NEW_LINE INDENT return " " . join ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT ( string [ index ] , string [ smallGreatDgt ] ) = ( string [ smallGreatDgt ] , string [ index ] ) NEW_LINE DEDENT return " " . join ( string ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n_str = "34125" NEW_LINE ans = prevNum ( list ( n_str ) , len ( n_str ) ) NEW_LINE print ( ans ) NEW_LINE DEDENT
|
T40054 | import java . util . * ; class GFG { static int minSteps ( int arr [ ] , int n ) { boolean [ ] v = new boolean [ n ] ; Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 0 ) ; int depth = 0 ; while ( q . size ( ) > 0 ) { int x = q . size ( ) ; while ( x -- > 0 ) { int i = q . peek ( ) ; q . poll ( ) ; if ( v [ i ] ) continue ; if ( i == n - 1 ) return depth ; v [ i ] = true ; if ( i + arr [ i ] < n ) q . add ( i + arr [ i ] ) ; if ( i - arr [ i ] >= 0 ) q . add ( i - arr [ i ] ) ; } depth ++ ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( minSteps ( arr , n ) ) ; } }
| def minSteps ( arr , n ) : NEW_LINE INDENT v = [ 0 for i in range ( n ) ] NEW_LINE q = [ ] NEW_LINE q . append ( 0 ) NEW_LINE depth = 0 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT x = len ( q ) NEW_LINE while ( x >= 1 ) : NEW_LINE INDENT i = q [ 0 ] NEW_LINE q . remove ( i ) NEW_LINE x -= 1 NEW_LINE if ( v [ i ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT if ( i == n - 1 ) : NEW_LINE INDENT return depth NEW_LINE DEDENT v [ i ] = 1 NEW_LINE if ( i + arr [ i ] < n ) : NEW_LINE INDENT q . append ( i + arr [ i ] ) NEW_LINE DEDENT if ( i - arr [ i ] >= 0 ) : NEW_LINE INDENT q . append ( i - arr [ i ] ) NEW_LINE DEDENT DEDENT depth += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSteps ( arr , n ) ) NEW_LINE DEDENT
|
T40055 | import java . util . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( arr [ i ] * i ) ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 6 , 1 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
| def maxSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] * i NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 3 , 5 , 6 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n ) ) NEW_LINE
|
T40056 | class GFG { static int firstFactorialDivisibleNumber ( int x ) { int i = 1 ; int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } public static void main ( String [ ] args ) { int x = 16 ; System . out . print ( firstFactorialDivisibleNumber ( x ) ) ; } }
| def firstFactorialDivisibleNumber ( x ) : NEW_LINE INDENT i = 1 ; NEW_LINE fact = 1 ; NEW_LINE for i in range ( 1 , x ) : NEW_LINE INDENT fact = fact * i NEW_LINE if ( fact % x == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return i NEW_LINE DEDENT x = 16 NEW_LINE print ( firstFactorialDivisibleNumber ( x ) ) NEW_LINE
|
T40057 | import java . lang . * ; public class GFG { static int cost ( int [ ] a , int n ) { int min = a [ 0 ] ; for ( int i = 1 ; i < a . length ; i ++ ) { if ( a [ i ] < min ) min = a [ i ] ; } return ( n - 1 ) * min ; } static public void main ( String [ ] args ) { int [ ] a = { 4 , 3 , 2 } ; int n = a . length ; System . out . println ( cost ( a , n ) ) ; } }
| def cost ( a , n ) : NEW_LINE INDENT return ( ( n - 1 ) * min ( a ) ) NEW_LINE DEDENT a = [ 4 , 3 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( cost ( a , n ) ) NEW_LINE
|
T40058 | import java . util . Arrays ; class GFG { static boolean prime [ ] = new boolean [ 100005 ] ; static void SieveOfEratosthenes ( int n ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n ; i += p ) { prime [ i ] = false ; } } } } static int xorPrimes ( int arr [ ] , int n ) { SieveOfEratosthenes ( 100005 ) ; int xorVal = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { xorVal = xorVal ^ arr [ i ] ; } } return xorVal ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 6 , 100 , 17 } ; int n = arr . length ; System . out . println ( xorPrimes ( arr , n ) ) ; } }
| prime = [ True ] * ( 100005 ) NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def xorPrimes ( arr , n ) : NEW_LINE INDENT SieveOfEratosthenes ( 100004 ) NEW_LINE xorVal = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT xorVal = xorVal ^ arr [ i ] NEW_LINE DEDENT DEDENT return xorVal NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 6 , 100 , 17 ] NEW_LINE n = len ( arr ) NEW_LINE print ( xorPrimes ( arr , n ) ) NEW_LINE DEDENT
|
T40059 | class GFG { static boolean doesContainB ( int a , int b , int c ) { if ( a == b ) { return true ; } if ( ( b - a ) * c > 0 && ( b - a ) % c == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int a = 1 , b = 7 , c = 3 ; if ( doesContainB ( a , b , c ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }
| def doesContainB ( a , b , c ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ( b - a ) * c > 0 and ( b - a ) % c == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , c = 1 , 7 , 3 NEW_LINE if ( doesContainB ( a , b , c ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
|
T40060 | class GFG { static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( int ) ( ( gap / 2 ) + ( gap % 2 ) ) ; } static void mergeTwoSortedArray ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int x = Math . min ( n , m ) ; for ( int i = 0 ; i < x ; i ++ ) { if ( arr1 [ n - i - 1 ] > arr2 [ i ] ) { int temp = arr1 [ n - i - 1 ] ; arr1 [ n - i - 1 ] = arr2 [ i ] ; arr2 [ i ] = temp ; } } for ( int gap = nextGap ( n ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( int i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } } for ( int gap = nextGap ( m ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( int i = 0 ; i + gap < m ; i ++ ) if ( arr2 [ i ] > arr2 [ i + gap ] ) { int temp = arr2 [ i ] ; arr2 [ i ] = arr2 [ i + gap ] ; arr2 [ i + gap ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr1 [ i ] + " β " ) ; for ( int j = 0 ; j < m ; j ++ ) System . out . print ( arr2 [ j ] + " β " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 5 , 9 , 10 , 15 , 20 } ; int n = arr1 . length ; int arr2 [ ] = { 2 , 3 , 8 , 13 } ; int m = arr2 . length ; mergeTwoSortedArray ( arr1 , arr2 , n , m ) ; } }
| def nextGap ( gap ) : NEW_LINE INDENT if ( gap <= 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT res = ( gap // 2 ) + ( gap % 2 ) ; NEW_LINE return res ; NEW_LINE DEDENT def mergeTwoSortedArray ( arr1 , arr2 , n , m ) : NEW_LINE INDENT x = min ( n , m ) ; NEW_LINE for i in range ( x ) : NEW_LINE INDENT if ( arr1 [ n - i - 1 ] > arr2 [ i ] ) : NEW_LINE INDENT arr1 [ n - i - 1 ] , arr2 [ i ] = arr2 [ i ] , arr1 [ n - i - 1 ] ; NEW_LINE DEDENT DEDENT gap = nextGap ( n ) ; NEW_LINE while gap > 0 : NEW_LINE INDENT i = 0 ; NEW_LINE while i + gap < n : NEW_LINE INDENT if ( arr1 [ i ] > arr1 [ i + gap ] ) : NEW_LINE INDENT arr1 [ i ] , arr1 [ i + gap ] = arr1 [ i + gap ] , arr1 [ i ] ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT gap = nextGap ( gap ) NEW_LINE DEDENT gap = nextGap ( m ) ; NEW_LINE while gap > 0 : NEW_LINE INDENT i = 0 NEW_LINE while i + gap < m : NEW_LINE INDENT if ( arr2 [ i ] > arr2 [ i + gap ] ) : NEW_LINE INDENT arr2 [ i ] , arr2 [ i + gap ] = arr2 [ i + gap ] , arr2 [ i ] ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT gap = nextGap ( gap ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr1 [ i ] , end = " β " ) ; NEW_LINE DEDENT for j in range ( m ) : NEW_LINE INDENT print ( arr2 [ j ] , end = " β " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 5 , 9 , 10 , 15 , 20 ] ; NEW_LINE n = len ( arr1 ) ; NEW_LINE arr2 = [ 2 , 3 , 8 , 13 ] ; NEW_LINE m = len ( arr2 ) ; NEW_LINE mergeTwoSortedArray ( arr1 , arr2 , n , m ) ; NEW_LINE DEDENT
|
T40061 | class GFG { static void WindowtoViewport ( int x_w , int y_w , int x_wmax , int y_wmax , int x_wmin , int y_wmin , int x_vmax , int y_vmax , int x_vmin , int y_vmin ) { int x_v , y_v ; float sx , sy ; sx = ( float ) ( x_vmax - x_vmin ) / ( x_wmax - x_wmin ) ; sy = ( float ) ( y_vmax - y_vmin ) / ( y_wmax - y_wmin ) ; x_v = ( int ) ( x_vmin + ( float ) ( ( x_w - x_wmin ) * sx ) ) ; y_v = ( int ) ( y_vmin + ( float ) ( ( y_w - y_wmin ) * sy ) ) ; System . out . printf ( " The β point β on β viewport : β ( % d , β % d β ) \n β " , x_v , y_v ) ; } public static void main ( String [ ] args ) { int x_wmax = 80 , y_wmax = 80 , x_wmin = 20 , y_wmin = 40 ; int x_vmax = 60 , y_vmax = 60 , x_vmin = 30 , y_vmin = 40 ; int x_w = 30 , y_w = 80 ; WindowtoViewport ( 30 , 80 , 80 , 80 , 20 , 40 , 60 , 60 , 30 , 40 ) ; } }
| def WindowtoViewport ( x_w , y_w , x_wmax , y_wmax , x_wmin , y_wmin , x_vmax , y_vmax , x_vmin , y_vmin ) : NEW_LINE INDENT sx = ( x_vmax - x_vmin ) / ( x_wmax - x_wmin ) NEW_LINE sy = ( y_vmax - y_vmin ) / ( y_wmax - y_wmin ) NEW_LINE x_v = x_vmin + ( ( x_w - x_wmin ) * sx ) NEW_LINE y_v = y_vmin + ( ( y_w - y_wmin ) * sy ) NEW_LINE print ( " The β point β on β viewport : ( " , int ( x_v ) , " , " , int ( y_v ) , " ) " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x_wmax = 80 NEW_LINE y_wmax = 80 NEW_LINE x_wmin = 20 NEW_LINE y_wmin = 40 NEW_LINE x_vmax = 60 NEW_LINE y_vmax = 60 NEW_LINE x_vmin = 30 NEW_LINE y_vmin = 40 NEW_LINE x_w = 30 NEW_LINE y_w = 80 NEW_LINE WindowtoViewport ( 30 , 80 , 80 , 80 , 20 , 40 , 60 , 60 , 30 , 40 ) NEW_LINE DEDENT
|
T40062 | import java . util . * ; class GFG { static int MAX = 26 ; static String countingsort ( char [ ] s ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < s . length ; i ++ ) { count [ s [ i ] - ' a ' ] ++ ; } int index = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { int j = 0 ; while ( j < count [ i ] ) { s [ index ++ ] = ( char ) ( i + ' a ' ) ; j ++ ; } } return String . valueOf ( s ) ; } static boolean isPossible ( Vector < String > v , String str ) { str = countingsort ( str . toCharArray ( ) ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { String temp = v . get ( i ) + v . get ( j ) ; temp = countingsort ( temp . toCharArray ( ) ) ; if ( temp . equals ( str ) ) { return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " amazon " ; String [ ] arr = { " fds " , " oxq " , " zoa " , " epw " , " amn " } ; Vector < String > v = new Vector < String > ( Arrays . asList ( arr ) ) ; if ( isPossible ( v , str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| MAX = 26 NEW_LINE def countingsort ( s ) : NEW_LINE INDENT count = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT index = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < count [ i ] ) : NEW_LINE INDENT s = s . replace ( s [ index ] , chr ( 97 + i ) ) NEW_LINE index += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def isPossible ( v , str1 ) : NEW_LINE INDENT countingsort ( str1 ) ; NEW_LINE for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len ( v ) , 1 ) : NEW_LINE INDENT temp = v [ i ] + v [ j ] NEW_LINE countingsort ( temp ) NEW_LINE if ( temp == str1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " amazon " NEW_LINE v = [ " fds " , " oxq " , " zoa " , " epw " , " amn " ] NEW_LINE if ( isPossible ( v , str1 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
|
T40063 | import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void primesieve ( boolean [ ] prime ) { prime [ 1 ] = false ; for ( int p = 2 ; p * p <= 650 ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= 650 ; i += p ) prime [ i ] = false ; } } } static pair sum_sqsum ( int n ) { int sum = 0 ; int sqsum = 0 ; int x ; while ( n > 0 ) { x = n % 10 ; sum += x ; sqsum += x * x ; n /= 10 ; } return ( new pair ( sum , sqsum ) ) ; } static int countnumber ( int L , int R ) { boolean [ ] prime = new boolean [ 651 ] ; Arrays . fill ( prime , true ) ; primesieve ( prime ) ; int cnt = 0 ; for ( int i = L ; i <= R ; i ++ ) { pair digit = sum_sqsum ( i ) ; if ( prime [ digit . first ] && prime [ digit . second ] ) { cnt += 1 ; } } return cnt ; } public static void main ( String [ ] args ) { int L = 10 ; int R = 20 ; System . out . println ( countnumber ( L , R ) ) ; } }
| from math import sqrt NEW_LINE def primesieve ( prime ) : NEW_LINE INDENT prime [ 1 ] = False ; NEW_LINE for p in range ( 2 , int ( sqrt ( 650 ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , 651 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def sum_sqsum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE sqsum = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT x = n % 10 ; NEW_LINE sum += x ; NEW_LINE sqsum += x * x ; NEW_LINE n //= 10 ; NEW_LINE DEDENT return ( sum , sqsum ) ; NEW_LINE DEDENT def countnumber ( L , R ) : NEW_LINE INDENT prime = [ True ] * 651 ; NEW_LINE primesieve ( prime ) ; NEW_LINE cnt = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT digit = sum_sqsum ( i ) ; NEW_LINE if ( prime [ digit [ 0 ] ] and prime [ digit [ 1 ] ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 10 ; NEW_LINE R = 20 ; NEW_LINE print ( countnumber ( L , R ) ) ; NEW_LINE DEDENT
|
T40064 | class GFG { static float sumOfAP ( float a , float d , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + a ; a = a + d ; } return sum ; } public static void main ( String args [ ] ) { int n = 20 ; float a = 2.5f , d = 1.5f ; System . out . println ( sumOfAP ( a , d , n ) ) ; } }
| def sumOfAP ( a , d , n ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT sum = sum + a NEW_LINE a = a + d NEW_LINE i = i + 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 20 NEW_LINE a = 2.5 NEW_LINE d = 1.5 NEW_LINE print ( sumOfAP ( a , d , n ) ) NEW_LINE
|
T40065 | import java . math . * ; class GFG { static int factorial ( int n ) { int fact = 1 , i ; if ( n == 0 ) return 1 ; for ( i = 1 ; i <= n ; i ++ ) fact *= i ; return fact ; } static void findmiddle ( int A , int X , int n ) { int i , j , aPow , xPow ; float middleTerm1 , middleTerm2 ; if ( n % 2 == 0 ) { i = n / 2 ; aPow = ( int ) Math . pow ( A , n - i ) ; xPow = ( int ) Math . pow ( X , i ) ; middleTerm1 = ( ( float ) factorial ( n ) / ( factorial ( n - i ) * factorial ( i ) ) ) * aPow * xPow ; System . out . println ( " MiddleTerm β = β " + middleTerm1 ) ; } else { i = ( n - 1 ) / 2 ; j = ( n + 1 ) / 2 ; aPow = ( int ) Math . pow ( A , n - i ) ; xPow = ( int ) Math . pow ( X , i ) ; middleTerm1 = ( ( float ) factorial ( n ) / ( factorial ( n - i ) * factorial ( i ) ) ) * aPow * xPow ; aPow = ( int ) Math . pow ( A , n - j ) ; xPow = ( int ) Math . pow ( X , j ) ; middleTerm2 = ( ( float ) factorial ( n ) / ( factorial ( n - j ) * factorial ( j ) ) ) * aPow * xPow ; System . out . println ( " MiddleTerm1 β = β " + middleTerm1 ) ; System . out . println ( " MiddleTerm2 β = β " + middleTerm2 ) ; } } public static void main ( String [ ] args ) { int n = 6 , A = 2 , X = 4 ; findmiddle ( A , X , n ) ; } }
| import math NEW_LINE def factorial ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT fact = fact * i NEW_LINE DEDENT return fact ; NEW_LINE DEDENT def findMiddleTerm ( A , X , n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT i = int ( n / 2 ) NEW_LINE aPow = int ( math . pow ( A , n - i ) ) NEW_LINE xPow = int ( math . pow ( X , i ) ) NEW_LINE middleTerm1 = ( ( math . factorial ( n ) / ( math . factorial ( n - i ) * math . factorial ( i ) ) ) * aPow * xPow ) NEW_LINE print ( " MiddleTerm β = β { } " . format ( middleTerm1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT i = int ( ( n - 1 ) / 2 ) NEW_LINE j = int ( ( n + 1 ) / 2 ) NEW_LINE aPow = int ( math . pow ( A , n - i ) ) NEW_LINE xPow = int ( math . pow ( X , i ) ) NEW_LINE middleTerm1 = ( ( math . factorial ( n ) / ( math . factorial ( n - i ) * math . factorial ( i ) ) ) * aPow * xPow ) NEW_LINE aPow = int ( math . pow ( A , n - j ) ) NEW_LINE xPow = int ( math . pow ( X , j ) ) NEW_LINE middleTerm2 = ( ( math . factorial ( n ) / ( math . factorial ( n - j ) * math . factorial ( j ) ) ) * aPow * xPow ) NEW_LINE print ( " MiddleTerm1 β = β { } " . format ( int ( middleTerm1 ) ) ) NEW_LINE print ( " MiddleTerm2 β = β { } " . format ( int ( middleTerm2 ) ) ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE A = 2 NEW_LINE X = 3 NEW_LINE findMiddleTerm ( A , X , n ) NEW_LINE
|
T40066 | class GFG { static int maxOR ( int arr [ ] , int n ) { int maxVal = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { maxVal = Math . max ( maxVal , arr [ i ] | arr [ j ] ) ; } return maxVal ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 12 , 16 } ; int n = arr . length ; System . out . println ( maxOR ( arr , n ) ) ; } }
| def maxOR ( arr , n ) : NEW_LINE INDENT maxVal = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT maxVal = max ( maxVal , arr [ i ] | arr [ j ] ) ; NEW_LINE DEDENT DEDENT return maxVal ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 8 , 12 , 16 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxOR ( arr , n ) ) ; NEW_LINE DEDENT
|
T40067 | class GFG { static int xor_triplet ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j ; k < n ; k ++ ) { int xor1 = 0 , xor2 = 0 ; for ( int x = i ; x < j ; x ++ ) { xor1 ^= arr [ x ] ; } for ( int x = j ; x <= k ; x ++ ) { xor2 ^= arr [ x ] ; } if ( xor1 == xor2 ) { ans ++ ; } } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( xor_triplet ( arr , n ) ) ; } }
| def xor_triplet ( arr , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT xor1 = 0 ; xor2 = 0 ; NEW_LINE for x in range ( i , j ) : NEW_LINE INDENT xor1 ^= arr [ x ] ; NEW_LINE DEDENT for x in range ( j , k + 1 ) : NEW_LINE INDENT xor2 ^= arr [ x ] ; NEW_LINE DEDENT if ( xor1 == xor2 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( xor_triplet ( arr , n ) ) ; NEW_LINE DEDENT
|
T40068 | class GFG { static int getMax ( int inputArray [ ] ) { int maxValue = inputArray [ 0 ] ; for ( int i = 1 ; i < inputArray . length ; i ++ ) { if ( inputArray [ i ] > maxValue ) { maxValue = inputArray [ i ] ; } } return maxValue ; } static int minSteps ( int arr [ ] , int n ) { int maxVal = getMax ( arr ) ; return maxVal ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 } ; int n = arr . length ; System . out . println ( minSteps ( arr , n ) ) ; } }
| def minSteps ( arr , n ) : NEW_LINE INDENT maxVal = max ( arr ) NEW_LINE return maxVal NEW_LINE DEDENT arr = [ 1 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minSteps ( arr , n ) ) NEW_LINE
|
T40069 | public class GFG { static int NumberofTimes ( String str ) { int temporary_sum = 0 , count = 0 ; while ( str . length ( ) > 1 ) { temporary_sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) temporary_sum += ( str . charAt ( i ) - '0' ) ; str = temporary_sum + " " ; count ++ ; } return count ; } public static void main ( String [ ] args ) { String s = "991" ; System . out . println ( NumberofTimes ( s ) ) ; } }
| def NumberofTimes ( s ) : NEW_LINE INDENT temporary_sum = 0 NEW_LINE count = 0 NEW_LINE while ( len ( s ) > 1 ) : NEW_LINE INDENT temporary_sum = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT temporary_sum += ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT s = str ( temporary_sum ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "991" NEW_LINE print ( NumberofTimes ( s ) ) NEW_LINE DEDENT
|
T40070 | import java . util . ArrayList ; public class GFG { static boolean checkPalindrome ( String str ) { int len = str . length ( ) ; len -- ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( len ) ) return false ; len -- ; } return true ; } static void printSolution ( ArrayList < ArrayList < String > > partitions ) { for ( ArrayList < String > i : partitions ) { for ( String j : i ) { System . out . print ( j + " β " ) ; } System . out . println ( ) ; } } static ArrayList < ArrayList < String > > addStrings ( ArrayList < ArrayList < String > > v , String s , ArrayList < String > temp , int index ) { int len = s . length ( ) ; String str = " " ; ArrayList < String > current = new ArrayList < > ( temp ) ; if ( index == 0 ) temp . clear ( ) ; for ( int i = index ; i < len ; ++ i ) { str = str + s . charAt ( i ) ; if ( checkPalindrome ( str ) ) { temp . add ( str ) ; if ( i + 1 < len ) { v = addStrings ( v , s , temp , i + 1 ) ; } else { v . add ( temp ) ; } temp = new ArrayList < > ( current ) ; } } return v ; } static void partition ( String s , ArrayList < ArrayList < String > > v ) { ArrayList < String > temp = new ArrayList < > ( ) ; v = addStrings ( v , s , temp , 0 ) ; printSolution ( v ) ; } public static void main ( String args [ ] ) { String s = " geeks " ; ArrayList < ArrayList < String > > partitions = new ArrayList < > ( ) ; partition ( s , partitions ) ; } }
| def checkPalindrome ( string ) : NEW_LINE INDENT length = len ( string ) NEW_LINE length -= 1 NEW_LINE for i in range ( length ) : NEW_LINE INDENT if string [ i ] != string [ length ] : NEW_LINE INDENT return False NEW_LINE DEDENT length -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def printSolution ( partitions ) : NEW_LINE INDENT for i in range ( len ( partitions ) ) : NEW_LINE INDENT for j in range ( len ( partitions [ i ] ) ) : NEW_LINE INDENT print ( partitions [ i ] [ j ] , end = " β " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def addStrings ( v , s , temp , index ) : NEW_LINE INDENT length = len ( s ) NEW_LINE string = " " NEW_LINE current = temp [ : ] NEW_LINE if index == 0 : NEW_LINE INDENT temp = [ ] NEW_LINE DEDENT for i in range ( index , length ) : NEW_LINE INDENT string += s [ i ] NEW_LINE if checkPalindrome ( string ) : NEW_LINE INDENT temp . append ( string ) NEW_LINE if i + 1 < length : NEW_LINE INDENT addStrings ( v , s , temp [ : ] , i + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT v . append ( temp ) NEW_LINE DEDENT temp = current NEW_LINE DEDENT DEDENT DEDENT def partition ( s , v ) : NEW_LINE INDENT temp = [ ] NEW_LINE addStrings ( v , s , temp [ : ] , 0 ) NEW_LINE printSolution ( v ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeks " NEW_LINE partitions = [ ] NEW_LINE partition ( s , partitions ) NEW_LINE DEDENT
|
T40071 | import java . util . Arrays ; class GFG { static int solve ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 != 0 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 8 , 4 , 5 , 2 , 3 } ; int n = arr . length ; System . out . print ( " Sum β is β " + solve ( arr , n ) ) ; } }
| def solve ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE a = 0 ; b = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT a = a * 10 + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT b = b * 10 + arr [ i ] NEW_LINE DEDENT DEDENT return a + b NEW_LINE DEDENT arr = [ 6 , 8 , 4 , 5 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Sum β is β " , solve ( arr , n ) ) NEW_LINE
|
T40072 | class GFG { static int countX ( int a , int b ) { if ( b > a ) return 0 ; else if ( a == b ) return - 1 ; else { int x = a - b , ans = 0 ; for ( int i = 1 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) { int d1 = i , d2 = b - 1 ; if ( i * i != x ) d2 = x / i ; if ( d1 > b ) ans ++ ; if ( d2 > b ) ans ++ ; } } return ans ; } } static public void main ( String args [ ] ) { int a = 21 , b = 5 ; System . out . println ( countX ( a , b ) ) ; } }
| def countX ( a , b ) : NEW_LINE INDENT if ( b > a ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( a == b ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT x = a - b NEW_LINE ans = 0 NEW_LINE i = 1 NEW_LINE while i * i <= x : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT d1 = i NEW_LINE d2 = b - 1 NEW_LINE if ( i * i != x ) : NEW_LINE INDENT d2 = x // i NEW_LINE DEDENT if ( d1 > b ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if ( d2 > b ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 21 NEW_LINE b = 5 NEW_LINE print ( countX ( a , b ) ) NEW_LINE DEDENT
|
T40073 | public class GFG { static int findLargestDivisor ( int n ) { for ( int i = 2 ; i < Math . sqrt ( n ) + 1 ; i ++ ) { while ( n % ( i * i ) == 0 ) { n = n / i ; } } return n ; } public static void main ( String args [ ] ) { int n = 12 ; System . out . println ( findLargestDivisor ( n ) ) ; n = 97 ; System . out . println ( findLargestDivisor ( n ) ) ; } }
| import math NEW_LINE def findLargestDivisor ( n ) : NEW_LINE INDENT for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT while ( n % ( i * i ) == 0 ) : NEW_LINE INDENT n = n // i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE print ( findLargestDivisor ( n ) ) NEW_LINE n = 97 NEW_LINE print ( findLargestDivisor ( n ) ) NEW_LINE DEDENT
|
T40074 | public class GFG { static int isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; if ( ch == ' A ' || ch == ' E ' || ch == ' I ' || ch == ' O ' || ch == ' U ' ) return 1 ; else return 0 ; } static int countVowels ( String str , int n ) { if ( n == 1 ) return isVowel ( str . charAt ( n - 1 ) ) ; return countVowels ( str , n - 1 ) + isVowel ( str . charAt ( n - 1 ) ) ; } public static void main ( String args [ ] ) { String str = " abc β de " ; System . out . println ( countVowels ( str , str . length ( ) ) ) ; } }
| def isVowel ( ch ) : NEW_LINE INDENT return ch . upper ( ) in [ ' A ' , ' E ' , ' I ' , ' O ' , ' U ' ] NEW_LINE DEDENT def countVovels ( str , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return isVowel ( str [ n - 1 ] ) ; NEW_LINE DEDENT return ( countVovels ( str , n - 1 ) + isVowel ( str [ n - 1 ] ) ) ; NEW_LINE DEDENT str = " abc β de " ; NEW_LINE print ( countVovels ( str , len ( str ) ) ) NEW_LINE
|
T40075 | class GFG { static void rgb_to_hsv ( double r , double g , double b ) { r = r / 255.0 ; g = g / 255.0 ; b = b / 255.0 ; double cmax = Math . max ( r , Math . max ( g , b ) ) ; double cmin = Math . min ( r , Math . min ( g , b ) ) ; double diff = cmax - cmin ; double h = - 1 , s = - 1 ; if ( cmax == cmin ) h = 0 ; else if ( cmax == r ) h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 ; else if ( cmax == g ) h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 ; else if ( cmax == b ) h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 ; if ( cmax == 0 ) s = 0 ; else s = ( diff / cmax ) * 100 ; double v = cmax * 100 ; System . out . println ( " ( " + h + " β " + s + " β " + v + " ) " ) ; } public static void main ( String [ ] args ) { rgb_to_hsv ( 129 , 88 , 47 ) ; } }
| def rgb_to_hsv ( r , g , b ) : NEW_LINE INDENT r , g , b = r / 255.0 , g / 255.0 , b / 255.0 NEW_LINE cmax = max ( r , g , b ) NEW_LINE cmin = min ( r , g , b ) NEW_LINE diff = cmax - cmin NEW_LINE if cmax == cmin : NEW_LINE INDENT h = 0 NEW_LINE DEDENT elif cmax == r : NEW_LINE INDENT h = ( 60 * ( ( g - b ) / diff ) + 360 ) % 360 NEW_LINE DEDENT elif cmax == g : NEW_LINE INDENT h = ( 60 * ( ( b - r ) / diff ) + 120 ) % 360 NEW_LINE DEDENT elif cmax == b : NEW_LINE INDENT h = ( 60 * ( ( r - g ) / diff ) + 240 ) % 360 NEW_LINE DEDENT if cmax == 0 : NEW_LINE INDENT s = 0 NEW_LINE DEDENT else : NEW_LINE INDENT s = ( diff / cmax ) * 100 NEW_LINE DEDENT v = cmax * 100 NEW_LINE return h , s , v NEW_LINE DEDENT print ( rgb_to_hsv ( 129 , 88 , 47 ) ) NEW_LINE
|
T40076 | class GFG { static int flipBit ( int a ) { if ( ~ a == 0 ) { return 8 * sizeof ( ) ; } int currLen = 0 , prevLen = 0 , maxLen = 0 ; while ( a != 0 ) { if ( ( a & 1 ) == 1 ) { currLen ++ ; } else if ( ( a & 1 ) == 0 ) { prevLen = ( a & 2 ) == 0 ? 0 : currLen ; currLen = 0 ; } maxLen = Math . max ( prevLen + currLen , maxLen ) ; a >>= 1 ; } return maxLen + 1 ; } static byte sizeof ( ) { byte sizeOfInteger = 8 ; return sizeOfInteger ; } public static void main ( String [ ] args ) { System . out . println ( flipBit ( 13 ) ) ; System . out . println ( flipBit ( 1775 ) ) ; System . out . println ( flipBit ( 15 ) ) ; } }
| def flipBit ( a ) : NEW_LINE INDENT if ( ~ a == 0 ) : NEW_LINE INDENT return 8 * sizeof ( ) ; NEW_LINE DEDENT currLen = 0 ; NEW_LINE prevLen = 0 ; NEW_LINE maxLen = 0 ; NEW_LINE while ( a > 0 ) : NEW_LINE INDENT if ( ( a & 1 ) == 1 ) : NEW_LINE INDENT currLen += 1 ; NEW_LINE DEDENT elif ( ( a & 1 ) == 0 ) : NEW_LINE INDENT prevLen = 0 if ( ( a & 2 ) == 0 ) else currLen ; NEW_LINE currLen = 0 ; NEW_LINE DEDENT maxLen = max ( prevLen + currLen , maxLen ) ; NEW_LINE a >>= 1 ; NEW_LINE DEDENT return maxLen + 1 ; NEW_LINE DEDENT print ( flipBit ( 13 ) ) ; NEW_LINE print ( flipBit ( 1775 ) ) ; NEW_LINE print ( flipBit ( 15 ) ) ; NEW_LINE
|
T40077 | import java . io . * ; class GFG { static int longestSubarray ( int a [ ] , int n , int x ) { int count = 0 ; int length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > x ) { count += 1 ; } else { length = Math . max ( length , count ) ; count = 0 ; } } if ( count > 0 ) length = Math . max ( length , count ) ; return length ; } public static void main ( String [ ] args ) { int [ ] a = { 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 } ; int n = a . length ; int k = 13 ; System . out . println ( longestSubarray ( a , n , k ) ) ; } }
| def longestSubarray ( a , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE length = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] > x ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT length = max ( length , count ) NEW_LINE count = 0 NEW_LINE DEDENT DEDENT if ( count > 0 ) : NEW_LINE INDENT length = max ( length , count ) NEW_LINE DEDENT return length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 8 , 25 , 10 , 19 , 19 , 18 , 20 , 11 , 18 ] NEW_LINE n = len ( a ) NEW_LINE k = 13 NEW_LINE print ( longestSubarray ( a , n , k ) ) NEW_LINE DEDENT
|
T40078 | import java . io . * ; class GFG { static int numberOfWays ( int x ) { if ( x == 0 || x == 1 ) return 1 ; else return numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ; } public static void main ( String [ ] args ) { int x = 3 ; System . out . println ( numberOfWays ( x ) ) ; } }
| def numberOfWays ( x ) : NEW_LINE INDENT if x == 0 or x == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( numberOfWays ( x - 1 ) + ( x - 1 ) * numberOfWays ( x - 2 ) ) NEW_LINE DEDENT DEDENT x = 3 NEW_LINE print ( numberOfWays ( x ) ) NEW_LINE
|
T40079 | class GFG { static String commonPrefixUtil ( String str1 , String str2 ) { String result = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; for ( int i = 0 , j = 0 ; i <= n1 - 1 && j <= n2 - 1 ; i ++ , j ++ ) { if ( str1 . charAt ( i ) != str2 . charAt ( j ) ) { break ; } result += str1 . charAt ( i ) ; } return ( result ) ; } static String commonPrefix ( String arr [ ] , int n ) { String prefix = arr [ 0 ] ; for ( int i = 1 ; i <= n - 1 ; i ++ ) { prefix = commonPrefixUtil ( prefix , arr [ i ] ) ; } return ( prefix ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) > 0 ) { System . out . printf ( " The β longest β common β prefix β is β - β % s " , ans ) ; } else { System . out . printf ( " There β is β no β common β prefix " ) ; } } }
| def commonPrefixUtil ( str1 , str2 ) : NEW_LINE INDENT result = " " ; NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while i <= n1 - 1 and j <= n2 - 1 : NEW_LINE INDENT if ( str1 [ i ] != str2 [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT result += str1 [ i ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return ( result ) NEW_LINE DEDENT def commonPrefix ( arr , n ) : NEW_LINE INDENT prefix = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix = commonPrefixUtil ( prefix , arr [ i ] ) NEW_LINE DEDENT return ( prefix ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE n = len ( arr ) NEW_LINE ans = commonPrefix ( arr , n ) NEW_LINE if ( len ( ans ) ) : NEW_LINE INDENT print ( " The β longest β common β prefix β is β - " , ans ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There β is β no β common β prefix " ) NEW_LINE DEDENT DEDENT
|
T40080 | import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public double CountDigits ( int n ) { if ( n == 1 ) return 1 ; double sum = 0 ; for ( int i = 2 ; i <= n ; ++ i ) { sum += ( ( double ) Math . log ( i ) / ( double ) Math . log ( 10 ) ) ; } sum *= n ; return Math . ceil ( sum ) ; } public static void main ( String args [ ] ) { GFG g = new GFG ( ) ; int N = 5 ; System . out . println ( g . CountDigits ( N ) ) ; } }
| import math as ma NEW_LINE def CountDigits ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT sum += ma . log ( i , 10 ) NEW_LINE DEDENT sum *= n NEW_LINE return ma . ceil ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( CountDigits ( N ) ) NEW_LINE DEDENT
|
T40081 | class GFG { static void printEgyptian ( int nr , int dr ) { if ( dr == 0 || nr == 0 ) { return ; } if ( dr % nr == 0 ) { System . out . print ( "1 / " + dr / nr ) ; return ; } if ( nr % dr == 0 ) { System . out . print ( nr / dr ) ; return ; } if ( nr > dr ) { System . out . print ( nr / dr + " β + β " ) ; printEgyptian ( nr % dr , dr ) ; return ; } int n = dr / nr + 1 ; System . out . print ( "1 / " + n + " β + β " ) ; printEgyptian ( nr * n - dr , dr * n ) ; } public static void main ( String [ ] args ) { int nr = 6 , dr = 14 ; System . out . print ( " Egyptian β Fraction β Representation β of β " + nr + " / " + dr + " β is \n β " ) ; printEgyptian ( nr , dr ) ; } }
| import math NEW_LINE def egyptianFraction ( nr , dr ) : NEW_LINE INDENT print ( " The β Egyptian β Fraction β " + " Representation β of β { 0 } / {1 } β is " . format ( nr , dr ) , end = " \n " ) NEW_LINE ef = [ ] NEW_LINE while nr != 0 : NEW_LINE INDENT x = math . ceil ( dr / nr ) NEW_LINE ef . append ( x ) NEW_LINE nr = x * nr - dr NEW_LINE dr = dr * x NEW_LINE DEDENT for i in range ( len ( ef ) ) : NEW_LINE INDENT if i != len ( ef ) - 1 : NEW_LINE INDENT print ( " β 1 / { 0 } β + " . format ( ef [ i ] ) , end = " β " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " β 1 / { 0 } " . format ( ef [ i ] ) , end = " β " ) NEW_LINE DEDENT DEDENT DEDENT egyptianFraction ( 6 , 14 ) NEW_LINE
|
T40082 | class demo { public static double sumOfTheSeries ( int n ) { double sum = 0.0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += 1.0 / ( i * ( i + 1 ) ) ; return sum ; } public static void main ( String args [ ] ) { int n = 10 ; System . out . println ( sumOfTheSeries ( n ) ) ; } }
| def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += 1.0 / ( i * ( i + 1 ) ) ; NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ans = sumOfTheSeries ( 10 ) NEW_LINE print ( round ( ans , 6 ) ) NEW_LINE DEDENT
|
T40083 | import java . io . * ; import java . util . * ; class GFG { static void find_max ( int [ ] A , int N , int K ) { HashMap < Integer , Integer > Count = new HashMap < > ( ) ; for ( int i = 0 ; i < K - 1 ; i ++ ) if ( Count . containsKey ( A [ i ] ) ) Count . put ( A [ i ] , 1 + Count . get ( A [ i ] ) ) ; else Count . put ( A [ i ] , 1 ) ; TreeSet < Integer > Myset = new TreeSet < Integer > ( ) ; for ( Map . Entry x : Count . entrySet ( ) ) { if ( Integer . parseInt ( String . valueOf ( x . getValue ( ) ) ) == 1 ) Myset . add ( Integer . parseInt ( String . valueOf ( x . getKey ( ) ) ) ) ; } for ( int i = K - 1 ; i < N ; i ++ ) { if ( Count . containsKey ( A [ i ] ) ) Count . put ( A [ i ] , 1 + Count . get ( A [ i ] ) ) ; else Count . put ( A [ i ] , 1 ) ; if ( Integer . parseInt ( String . valueOf ( Count . get ( A [ i ] ) ) ) == 1 ) Myset . add ( A [ i ] ) ; else Myset . remove ( A [ i ] ) ; if ( Myset . size ( ) == 0 ) System . out . println ( " Nothing " ) ; else System . out . println ( Myset . last ( ) ) ; int x = A [ i - K + 1 ] ; Count . put ( x , Count . get ( x ) - 1 ) ; if ( Integer . parseInt ( String . valueOf ( Count . get ( x ) ) ) == 1 ) Myset . add ( x ) ; if ( Integer . parseInt ( String . valueOf ( Count . get ( x ) ) ) == 0 ) Myset . remove ( x ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 1 , 2 , 2 , 3 , 3 } ; int n = a . length ; int k = 3 ; find_max ( a , n , k ) ; } }
| def find_max ( A , N , K ) : NEW_LINE INDENT Count = dict ( ) NEW_LINE for i in range ( K - 1 ) : NEW_LINE INDENT Count [ A [ i ] ] = Count . get ( A [ i ] , 0 ) + 1 NEW_LINE DEDENT Myset = dict ( ) NEW_LINE for x in Count : NEW_LINE INDENT if ( Count [ x ] == 1 ) : NEW_LINE INDENT Myset [ x ] = 1 NEW_LINE DEDENT DEDENT for i in range ( K - 1 , N ) : NEW_LINE INDENT Count [ A [ i ] ] = Count . get ( A [ i ] , 0 ) + 1 NEW_LINE if ( Count [ A [ i ] ] == 1 ) : NEW_LINE INDENT Myset [ A [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT del Myset [ A [ i ] ] NEW_LINE DEDENT if ( len ( Myset ) == 0 ) : NEW_LINE INDENT print ( " Nothing " ) NEW_LINE DEDENT else : NEW_LINE INDENT maxm = - 10 ** 9 NEW_LINE for i in Myset : NEW_LINE INDENT maxm = max ( i , maxm ) NEW_LINE DEDENT print ( maxm ) NEW_LINE DEDENT x = A [ i - K + 1 ] NEW_LINE if x in Count . keys ( ) : NEW_LINE INDENT Count [ x ] -= 1 NEW_LINE if ( Count [ x ] == 1 ) : NEW_LINE INDENT Myset [ x ] = 1 NEW_LINE DEDENT if ( Count [ x ] == 0 ) : NEW_LINE INDENT del Myset [ x ] NEW_LINE DEDENT DEDENT DEDENT DEDENT a = [ 1 , 2 , 2 , 3 , 3 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE find_max ( a , n , k ) NEW_LINE
|
T40084 | class GFG { static int MAX = 1000 ; static int NthPrime ( int n ) { int count = 0 ; int i ; for ( i = 2 ; i <= MAX ; i ++ ) { int check = 0 ; for ( int j = 2 ; j <= Math . sqrt ( i ) ; j ++ ) { if ( i % j == 0 ) { check = 1 ; break ; } } if ( check == 0 ) count ++ ; if ( count == n ) { return i ; } } return 0 ; } static int NthFib ( int n ) { int [ ] f = new int [ n + 2 ] ; int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; for ( i = 2 ; i <= n ; i ++ ) { f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } return f [ n ] ; } static void findNthTerm ( int n ) { if ( n % 2 == 0 ) { n = n / 2 ; n = NthPrime ( n ) ; System . out . println ( n ) ; } else { n = ( n / 2 ) + 1 ; n = NthFib ( n - 1 ) ; System . out . println ( n ) ; } } public static void main ( String [ ] args ) { int X = 5 ; findNthTerm ( X ) ; X = 10 ; findNthTerm ( X ) ; } }
| from math import sqrt NEW_LINE MAX = 1000 NEW_LINE def NthPrime ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT check = 0 NEW_LINE for j in range ( 2 , int ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if i % j == 0 : NEW_LINE INDENT check = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if check == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if count == n : NEW_LINE INDENT return i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT def NthFib ( n ) : NEW_LINE INDENT f = [ 0 ] * ( n + 2 ) NEW_LINE f [ 0 ] , f [ 1 ] = 0 , 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] + f [ i - 2 ] NEW_LINE DEDENT return f [ n ] NEW_LINE DEDENT def findNthTerm ( n ) : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT n //= 2 NEW_LINE n = NthPrime ( n ) NEW_LINE print ( n ) NEW_LINE DEDENT else : NEW_LINE INDENT n = ( n // 2 ) + 1 NEW_LINE n = NthFib ( n - 1 ) NEW_LINE print ( n ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 5 NEW_LINE findNthTerm ( X ) NEW_LINE X = 10 NEW_LINE findNthTerm ( X ) NEW_LINE DEDENT
|
T40085 | class Test { static boolean checkYear ( int year ) { return ( ( ( year % 4 == 0 ) && ( year % 100 != 0 ) ) || ( year % 400 == 0 ) ) ; } public static void main ( String [ ] args ) { int year = 2000 ; System . out . println ( checkYear ( 2000 ) ? " Leap β Year " : " Not β a β Leap β Year " ) ; } }
| def checkYear ( year ) : NEW_LINE INDENT return ( ( ( year % 4 == 0 ) and ( year % 100 != 0 ) ) or ( year % 400 == 0 ) ) ; NEW_LINE DEDENT year = 2000 NEW_LINE if ( checkYear ( year ) ) : NEW_LINE INDENT print ( " Leap β Year " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not β a β Leap β Year " ) NEW_LINE DEDENT
|
T40086 | class GfG { static void twoParts ( String str ) { int flag = 0 ; String a = " " ; char [ ] gfg = str . toCharArray ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( gfg [ i ] == '4' ) { gfg [ i ] = '3' ; a += '1' ; flag = 1 ; } else if ( flag != 0 ) a += '0' ; } str = new String ( gfg ) ; System . out . print ( str + " β " + a ) ; } public static void main ( String [ ] args ) { String str = "9441" ; twoParts ( str ) ; } }
| def twoParts ( string ) : NEW_LINE INDENT flag = 0 ; NEW_LINE a = " " ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == '4' ) : NEW_LINE INDENT string [ i ] = '3' ; NEW_LINE a += '1' ; NEW_LINE flag = 1 ; NEW_LINE DEDENT elif ( flag ) : NEW_LINE INDENT a += '0' ; NEW_LINE DEDENT DEDENT string = " " . join ( string ) ; NEW_LINE print ( string , a ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "9441" ; NEW_LINE twoParts ( list ( string ) ) ; NEW_LINE DEDENT
|
T40087 | import java . io . * ; class GFG { static int lcs ( String X , String Y , int m , int n ) { int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 || j == 0 ) L [ i ] [ j ] = 0 ; else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; else L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } return L [ m ] [ n ] ; } static int findMinCost ( String X , String Y , int costX , int costY ) { int m = X . length ( ) ; int n = Y . length ( ) ; int len_LCS ; len_LCS = lcs ( X , Y , m , n ) ; return costX * ( m - len_LCS ) + costY * ( n - len_LCS ) ; } public static void main ( String [ ] args ) { String X = " ef " ; String Y = " gh " ; System . out . println ( " Minimum β Cost β to β make β two β strings β " + " β identical β is β = β " + findMinCost ( X , Y , 10 , 20 ) ) ; } }
| def lcs ( X , Y , m , n ) : NEW_LINE INDENT L = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if i == 0 or j == 0 : NEW_LINE INDENT L [ i ] [ j ] = 0 NEW_LINE DEDENT elif X [ i - 1 ] == Y [ j - 1 ] : NEW_LINE INDENT L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return L [ m ] [ n ] NEW_LINE DEDENT def findMinCost ( X , Y , costX , costY ) : NEW_LINE INDENT m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE len_LCS = lcs ( X , Y , m , n ) NEW_LINE return ( costX * ( m - len_LCS ) + costY * ( n - len_LCS ) ) NEW_LINE DEDENT X = " ef " NEW_LINE Y = " gh " NEW_LINE print ( ' Minimum β Cost β to β make β two β strings β ' , end = ' ' ) NEW_LINE print ( ' identical β is β = β ' , findMinCost ( X , Y , 10 , 20 ) ) NEW_LINE
|
T40088 | class GFG { static double calculate_angle ( int n , int i , int j , int k ) { int x , y ; if ( i < j ) x = j - i ; else x = j + n - i ; if ( j < k ) y = k - j ; else y = k + n - j ; double ang1 = ( 180 * x ) / n ; double ang2 = ( 180 * y ) / n ; double ans = 180 - ang1 - ang2 ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int a1 = 1 ; int a2 = 2 ; int a3 = 5 ; System . out . println ( ( int ) calculate_angle ( n , a1 , a2 , a3 ) ) ; } }
| def calculate_angle ( n , i , j , k ) : NEW_LINE INDENT x , y = 0 , 0 NEW_LINE if ( i < j ) : NEW_LINE INDENT x = j - i NEW_LINE DEDENT else : NEW_LINE INDENT x = j + n - i NEW_LINE DEDENT if ( j < k ) : NEW_LINE INDENT y = k - j NEW_LINE DEDENT else : NEW_LINE INDENT y = k + n - j NEW_LINE DEDENT ang1 = ( 180 * x ) // n NEW_LINE ang2 = ( 180 * y ) // n NEW_LINE ans = 180 - ang1 - ang2 NEW_LINE return ans NEW_LINE DEDENT n = 5 NEW_LINE a1 = 1 NEW_LINE a2 = 2 NEW_LINE a3 = 5 NEW_LINE print ( calculate_angle ( n , a1 , a2 , a3 ) ) NEW_LINE
|
T40089 | import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int k ) { Arrays . sort ( arr ) ; int pair = 0 ; int index = 0 ; while ( index < arr . length - 1 ) { if ( arr [ index + 1 ] - arr [ index ] <= k ) { pair += 1 ; index += 2 ; } else { index += 1 ; } } return pair ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 7 , 5 } ; int k = 2 ; int count = countPairs ( arr , k ) ; System . out . println ( count ) ; } }
| def countPairs ( arr , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE pair = 0 NEW_LINE index = 0 NEW_LINE while ( index < len ( arr ) - 1 ) : NEW_LINE INDENT if arr [ index + 1 ] - arr [ index ] <= k : NEW_LINE INDENT pair += 1 NEW_LINE index += 2 NEW_LINE DEDENT else : NEW_LINE INDENT index += 1 NEW_LINE DEDENT DEDENT return pair NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 7 , 5 ] NEW_LINE k = 2 NEW_LINE count = countPairs ( arr , k ) NEW_LINE print ( count ) NEW_LINE DEDENT
|
T40090 | import java . io . * ; class GFG { static void leftRotate ( int arr [ ] , int n , int k ) { for ( int i = k ; i < k + n ; i ++ ) System . out . print ( arr [ i % n ] + " β " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int k = 2 ; leftRotate ( arr , n , k ) ; System . out . println ( ) ; k = 3 ; leftRotate ( arr , n , k ) ; System . out . println ( ) ; k = 4 ; leftRotate ( arr , n , k ) ; System . out . println ( ) ; } }
| def leftRotate ( arr , n , k ) : NEW_LINE INDENT for i in range ( k , k + n ) : NEW_LINE INDENT print ( str ( arr [ i % n ] ) , end = " β " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 3 , 5 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 ; NEW_LINE leftRotate ( arr , n , k ) NEW_LINE print ( ) NEW_LINE k = 3 ; NEW_LINE leftRotate ( arr , n , k ) NEW_LINE print ( ) NEW_LINE k = 4 NEW_LINE leftRotate ( arr , n , k ) NEW_LINE print ( ) NEW_LINE
|
T40091 | class GFG { static int getPairs ( int a [ ] ) { int n = a . length ; int count = ( n * ( n - 1 ) ) / 2 ; return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 3 , 1 } ; System . out . println ( getPairs ( a ) ) ; } }
| def getPairs ( a ) : NEW_LINE INDENT n = len ( a ) NEW_LINE count = ( n * ( n - 1 ) ) // 2 NEW_LINE return count NEW_LINE DEDENT a = [ 2 , 4 , 3 , 1 ] NEW_LINE print ( getPairs ( a ) ) NEW_LINE
|
T40092 | class GFG { static int count ( int n ) { return ( int ) ( 15 * Math . pow ( 16 , n - 1 ) ) ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( count ( n ) ) ; } }
| def count ( n ) : NEW_LINE INDENT return 15 * pow ( 16 , n - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE print ( count ( n ) ) ; NEW_LINE DEDENT
|
T40093 | import java . io . * ; class GFG { static int countPairs ( int [ ] arr1 , int [ ] arr2 , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count β = β " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }
| def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if arr1 [ i ] + arr2 [ j ] == x : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count β = β " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE
|
T40094 | import java . io . * ; class GFG { static int zzis ( int arr [ ] , int n ) { int Z [ ] [ ] = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) Z [ i ] [ 0 ] = Z [ i ] [ 1 ] = 1 ; int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] < arr [ i ] && Z [ i ] [ 0 ] < Z [ j ] [ 1 ] + 1 ) Z [ i ] [ 0 ] = Z [ j ] [ 1 ] + 1 ; if ( arr [ j ] > arr [ i ] && Z [ i ] [ 1 ] < Z [ j ] [ 0 ] + 1 ) Z [ i ] [ 1 ] = Z [ j ] [ 0 ] + 1 ; } if ( res < Math . max ( Z [ i ] [ 0 ] , Z [ i ] [ 1 ] ) ) res = Math . max ( Z [ i ] [ 0 ] , Z [ i ] [ 1 ] ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 } ; int n = arr . length ; System . out . println ( " Length β of β Longest β " + " Zig - Zag β subsequence β is β " + zzis ( arr , n ) ) ; } }
| def zzis ( arr , n ) : NEW_LINE INDENT Z = [ [ 1 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE res = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] and Z [ i ] [ 0 ] < Z [ j ] [ 1 ] + 1 ) : NEW_LINE INDENT Z [ i ] [ 0 ] = Z [ j ] [ 1 ] + 1 NEW_LINE DEDENT if ( arr [ j ] > arr [ i ] and Z [ i ] [ 1 ] < Z [ j ] [ 0 ] + 1 ) : NEW_LINE INDENT Z [ i ] [ 1 ] = Z [ j ] [ 0 ] + 1 NEW_LINE DEDENT DEDENT if ( res < max ( Z [ i ] [ 0 ] , Z [ i ] [ 1 ] ) ) : NEW_LINE INDENT res = max ( Z [ i ] [ 0 ] , Z [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 49 , 50 , 31 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length β of β Longest β Zig - Zag β subsequence β is " , zzis ( arr , n ) ) NEW_LINE
|
T40095 | class GFG { static String constructPalin ( char [ ] str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) { str [ i ] = ' a ' ; str [ j ] = ' a ' ; continue ; } else if ( str [ i ] == ' * ' ) { str [ i ] = str [ j ] ; continue ; } else if ( str [ j ] == ' * ' ) { str [ j ] = str [ i ] ; continue ; } System . out . println ( " Not β Possible " ) ; return " " ; } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = " bca * xc * * b " ; int len = str . length ( ) ; System . out . println ( constructPalin ( str . toCharArray ( ) , len ) ) ; } }
| def constructPalin ( string , l ) : NEW_LINE INDENT string = list ( string ) NEW_LINE i = - 1 NEW_LINE j = l NEW_LINE while i < j : NEW_LINE INDENT i += 1 NEW_LINE j -= 1 NEW_LINE if ( string [ i ] == string [ j ] and string [ i ] != ' * ' ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( string [ i ] == string [ j ] and string [ i ] == ' * ' ) : NEW_LINE INDENT string [ i ] = ' a ' NEW_LINE string [ j ] = ' a ' NEW_LINE continue NEW_LINE DEDENT elif string [ i ] == ' * ' : NEW_LINE INDENT string [ i ] = string [ j ] NEW_LINE continue NEW_LINE DEDENT elif string [ j ] == ' * ' : NEW_LINE INDENT string [ j ] = string [ i ] NEW_LINE continue NEW_LINE DEDENT print ( " Not β Possible " ) NEW_LINE return " " NEW_LINE DEDENT return ' ' . join ( string ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " bca * xc * * b " NEW_LINE l = len ( string ) NEW_LINE print ( constructPalin ( string , l ) ) NEW_LINE DEDENT
|
T40096 | class GFG { static int countOperations ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { arr [ i ] ++ ; arr [ i + 1 ] ++ ; count += 2 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) return - 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; System . out . print ( countOperations ( arr , n ) ) ; } }
| def countOperations ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT arr [ i ] += 1 ; NEW_LINE arr [ i + 1 ] += 1 ; NEW_LINE count += 2 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countOperations ( arr , n ) ) ; NEW_LINE DEDENT
|
T40097 | class Sum { void sumOfPrevK ( int N , int K ) { int arr [ ] = new int [ N ] ; int prevsum = 0 ; arr [ 0 ] = 1 ; for ( int i = 0 ; i < N - 1 ; i ++ ) { if ( i <= K ) { arr [ i + 1 ] = arr [ i ] + prevsum ; prevsum = arr [ i + 1 ] ; } else { arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; prevsum = arr [ i ] + prevsum ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " β " ) ; } } public static void main ( String args [ ] ) { Sum s = new Sum ( ) ; int N = 8 , K = 3 ; s . sumOfPrevK ( N , K ) ; } }
| def sumOfPrevK ( N , K ) : NEW_LINE INDENT arr = [ 0 ] * N ; NEW_LINE prevsum = 0 ; NEW_LINE arr [ 0 ] = 1 ; NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( i <= K ) : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] + prevsum ; NEW_LINE prevsum = arr [ i + 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; NEW_LINE prevsum = arr [ i ] + prevsum ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; NEW_LINE K = 3 ; NEW_LINE sumOfPrevK ( N , K ) ; NEW_LINE DEDENT
|
T40098 | public class GFG { static void find ( int n ) { int b = n ; int a = b * ( n - 1 ) ; if ( a * b > n && a / b < n ) { System . out . print ( " a β = β " + a + " , β b β = β " + b ) ; } else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; find ( n ) ; } }
| def find ( n ) : NEW_LINE INDENT b = n NEW_LINE a = b * ( n - 1 ) NEW_LINE if a * b > n and a // b < n : NEW_LINE INDENT print ( " a β = β { } , β b β = β { } " . format ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE find ( n ) NEW_LINE DEDENT
|
T40099 | import java . util . * ; class GFG { static double ReuleauxArea ( double r ) { if ( r < 0 ) return - 1 ; double A = 0.70477 * 2 * Math . pow ( r , 2 ) ; return A ; } public static void main ( String args [ ] ) { double r = 6 ; System . out . println ( ReuleauxArea ( r ) ) ; } }
| import math as mt NEW_LINE def ReuleauxArea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = 0.70477 * 2 * pow ( r , 2 ) NEW_LINE return A NEW_LINE DEDENT r = 6 NEW_LINE print ( ReuleauxArea ( r ) ) NEW_LINE
|