id
stringlengths 2
6
| java
stringlengths 48
5.92k
| python
stringlengths 33
11.1k
|
---|---|---|
T40100 | class GfG { static int getCount ( String a , String b ) { if ( b . length ( ) % a . length ( ) != 0 ) return - 1 ; int count = b . length ( ) / a . length ( ) ; String str = " " ; for ( int i = 0 ; i < count ; i ++ ) { str = str + a ; } if ( str . equals ( b ) ) return count ; return - 1 ; } public static void main ( String [ ] args ) { String a = " geeks " ; String b = " geeksgeeks " ; System . out . println ( getCount ( a , b ) ) ; } }
| def getCount ( a , b ) : NEW_LINE INDENT if ( len ( b ) % len ( a ) != 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT count = int ( len ( b ) / len ( a ) ) NEW_LINE a = a * count NEW_LINE if ( a == b ) : NEW_LINE INDENT return count NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = ' geeks ' NEW_LINE b = ' geeksgeeks ' NEW_LINE print ( getCount ( a , b ) ) NEW_LINE DEDENT
|
T40101 | import java . io . * ; class GFG { public int nthTerm ( int N ) { return ( N * N * N ) + ( N * N ) + N ; } public static void main ( String [ ] args ) { int N = 3 ; GFG a = new GFG ( ) ; System . out . println ( a . nthTerm ( N ) ) ; } }
| def nthTerm ( n ) : NEW_LINE INDENT return ( N * N * N ) + ( N * N ) + N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
|
T40102 | import java . io . * ; class Nth { public static int nthTerm ( int N ) { return N * N + ( N + 1 ) * ( N + 1 ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }
| def nthTerm ( N ) : NEW_LINE INDENT return N * N + ( N + 1 ) * ( N + 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT
|
T40103 | import java . io . * ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long powGCD ( long a , long n , long b ) { for ( int i = 0 ; i < n ; i ++ ) a = a * a ; return gcd ( a , b ) ; } public static void main ( String [ ] args ) { long a = 10 , b = 5 , n = 2 ; System . out . println ( powGCD ( a , n , b ) ) ; } }
| def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def powGCD ( a , n , b ) : NEW_LINE INDENT for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT a = a * a NEW_LINE DEDENT return gcd ( a , b ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 5 NEW_LINE n = 2 NEW_LINE print ( powGCD ( a , n , b ) ) NEW_LINE DEDENT
|
T40104 | import java . io . * ; class GFG { static Boolean divisibleBy20 ( String num ) { int lastTwoDigits = Integer . parseInt ( num . substring ( num . length ( ) - 2 , num . length ( ) ) ) ; return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ; } public static void main ( String [ ] args ) { String num = "63284689320" ; if ( divisibleBy20 ( num ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| import math NEW_LINE def divisibleBy20 ( num ) : NEW_LINE INDENT lastTwoDigits = int ( num [ - 2 : ] ) NEW_LINE return ( ( lastTwoDigits % 5 == 0 and lastTwoDigits % 4 == 0 ) ) NEW_LINE DEDENT num = "63284689320" NEW_LINE if ( divisibleBy20 ( num ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40105 | import java . util . * ; class GFG { static int findNumOfPair ( int a [ ] , int n ) { Arrays . sort ( a ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( a [ i ] <= 0 ) continue ; int len = a . length ; int j = 0 ; while ( j < len ) { if ( a [ j ] == n ) { return j ; } else { j = j + 1 ; } } ans += i - j ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , - 2 , 1 } ; int n = a . length ; int ans = findNumOfPair ( a , n ) ; System . out . println ( ans ) ; } }
| from bisect import bisect_left as lower_bound NEW_LINE def findNumOfPair ( a , n ) : NEW_LINE INDENT a = sorted ( a ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] <= 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT j = lower_bound ( a , - a [ i ] + 1 ) NEW_LINE ans += i - j NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 3 , - 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE ans = findNumOfPair ( a , n ) NEW_LINE print ( ans ) NEW_LINE DEDENT
|
T40106 | import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_cicumscribed ( double a ) { return ( a * a * ( PI / 3 ) ) ; } public static void main ( String [ ] args ) { double a = 6.0 ; System . out . println ( " Area β of β circumscribed β circle β is β : " + area_cicumscribed ( a ) ) ; } }
| PI = 3.14159265 NEW_LINE def area_cicumscribed ( a ) : NEW_LINE INDENT return ( a * a * ( PI / 3 ) ) NEW_LINE DEDENT a = 6.0 NEW_LINE print ( " Area β of β circumscribed β circle β is β : % f " % area_cicumscribed ( a ) ) NEW_LINE
|
T40107 | import java . util . * ; class GFG { static int max_element ( int arr [ ] ) { int max_e = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr . length ; i ++ ) { max_e = Math . max ( max_e , arr [ i ] ) ; } return max_e ; } static int nonPrimeSum ( int arr [ ] , int n ) { int max_val = max_element ( arr ) ; boolean prime [ ] = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i < prime . length ; 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 sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ! prime [ arr [ i ] ] ) sum += arr [ i ] ; return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 7 , 4 , 9 , 8 } ; int n = arr . length ; System . out . println ( nonPrimeSum ( arr , n ) ) ; } }
| from math import sqrt NEW_LINE def nonPrimeSum ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True ] * ( max_val + 1 ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( not prime [ arr [ i ] ] ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 3 , 7 , 4 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( nonPrimeSum ( arr , n ) ) NEW_LINE DEDENT
|
T40108 | import java . io . * ; import java . util . HashMap ; import java . util . Map . Entry ; import java . util . Map ; import java . lang . * ; class GFG { static int maxlenAP ( int a [ ] , int n , int d ) { HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; int maxt = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( a [ i ] - i * d ) ) { int freq = m . get ( a [ i ] - i * d ) ; freq ++ ; m . put ( a [ i ] - i * d , freq ) ; } else { m . put ( a [ i ] - i * d , 1 ) ; } } for ( Entry < Integer , Integer > val : m . entrySet ( ) ) { if ( maxt < val . getValue ( ) ) maxt = val . getValue ( ) ; } return maxt ; } public static void main ( String [ ] args ) { int n = 10 , d = 3 ; int a [ ] = new int [ ] { 1 , 4 , 2 , 5 , 20 , 11 , 56 , 100 , 20 , 23 } ; System . out . print ( maxlenAP ( a , n , d ) + " \n " ) ; } }
| def maxlenAP ( a , n , d ) : NEW_LINE INDENT m = dict ( ) NEW_LINE maxt = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] - i * d ) in m : NEW_LINE INDENT m [ a [ i ] - i * d ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ a [ i ] - i * d ] = 1 NEW_LINE DEDENT DEDENT for it in m : NEW_LINE INDENT if m [ it ] > maxt : NEW_LINE INDENT maxt = m [ it ] NEW_LINE DEDENT DEDENT return maxt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , d = 10 , 3 NEW_LINE a = [ 1 , 4 , 2 , 5 , 20 , 11 , 56 , 100 , 20 , 23 ] NEW_LINE print ( maxlenAP ( a , n , d ) ) NEW_LINE DEDENT
|
T40109 | import java . util . * ; class GFG { static void findSet ( int arr [ ] , int n , int k , int m ) { Vector < Integer > [ ] remainder_set = new Vector [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { remainder_set [ i ] = new Vector < Integer > ( ) ; } for ( int i = 0 ; i < n ; i ++ ) { int rem = arr [ i ] % k ; remainder_set [ rem ] . add ( arr [ i ] ) ; } for ( int i = 0 ; i < k ; i ++ ) { if ( remainder_set [ i ] . size ( ) >= m ) { System . out . println ( " Yes " ) ; for ( int j = 0 ; j < m ; j ++ ) System . out . print ( remainder_set [ i ] . get ( j ) + " β " ) ; return ; } } System . out . print ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 8 , 9 , 12 , 13 , 7 , 11 , 15 } ; int k = 4 ; int m = 3 ; int n = arr . length ; findSet ( arr , n , k , m ) ; } }
| def findSet ( arr , k , m ) : NEW_LINE INDENT arr_size = len ( arr ) ; NEW_LINE remainder_set = [ 0 ] * k ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT remainder_set [ i ] = [ ] ; NEW_LINE DEDENT for i in range ( arr_size ) : NEW_LINE INDENT rem = arr [ i ] % k ; NEW_LINE remainder_set [ rem ] . append ( arr [ i ] ) ; NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT if ( len ( remainder_set [ i ] ) >= m ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE for j in range ( m ) : NEW_LINE INDENT print ( remainder_set [ i ] [ j ] , end = " " ) ; NEW_LINE print ( " β " , end = " " ) ; NEW_LINE DEDENT return ; NEW_LINE DEDENT DEDENT print ( " No " ) ; NEW_LINE DEDENT arr = [ 5 , 8 , 9 , 12 , 13 , 7 , 11 , 15 ] ; NEW_LINE k = 4 ; NEW_LINE m = 3 ; NEW_LINE findSet ( arr , k , m ) ; NEW_LINE
|
T40110 | import java . io . * ; class GFG { static int N = 4 ; static boolean isScalarMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 2 , 0 , 0 , 0 } , { 0 , 2 , 0 , 0 } , { 0 , 0 , 2 , 0 } , { 0 , 0 , 0 , 2 } } ; if ( isScalarMatrix ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| N = 4 NEW_LINE def isScalarMatrix ( mat ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , N - 1 ) : NEW_LINE INDENT if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT mat = [ [ 2 , 0 , 0 , 0 ] , [ 0 , 2 , 0 , 0 ] , [ 0 , 0 , 2 , 0 ] , [ 0 , 0 , 0 , 2 ] ] NEW_LINE if ( isScalarMatrix ( mat ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40111 | class GFG { static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) { double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ; return Math . ceil ( d / ( 2 * r ) ) ; } public static void main ( String arg [ ] ) { int r = 2 , x1 = 0 , y1 = 0 ; int x2 = 0 , y2 = 4 ; System . out . print ( ( int ) minRevolutions ( r , x1 , y1 , x2 , y2 ) ) ; } }
| import math NEW_LINE def minRevolutions ( r , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT d = math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) NEW_LINE return math . ceil ( d // ( 2 * r ) ) NEW_LINE DEDENT r = 2 NEW_LINE x1 = 0 NEW_LINE y1 = 0 NEW_LINE x2 = 0 NEW_LINE y2 = 4 NEW_LINE print ( minRevolutions ( r , x1 , y1 , x2 , y2 ) ) NEW_LINE
|
T40112 | import java . util . * ; class GFG { static void convert12 ( String str ) { int h1 = ( int ) str . charAt ( 0 ) - '0' ; int h2 = ( int ) str . charAt ( 1 ) - '0' ; int hh = h1 * 10 + h2 ; String Meridien ; if ( hh < 12 ) { Meridien = " AM " ; } else Meridien = " PM " ; hh %= 12 ; if ( hh == 0 ) { System . out . print ( "12" ) ; for ( int i = 2 ; i < 8 ; ++ i ) { System . out . print ( str . charAt ( i ) ) ; } } else { System . out . print ( hh ) ; for ( int i = 2 ; i < 8 ; ++ i ) { System . out . print ( str . charAt ( i ) ) ; } } System . out . println ( " β " + Meridien ) ; } public static void main ( String ar [ ] ) { String str = "17:35:20" ; convert12 ( str ) ; } }
| def convert12 ( str ) : NEW_LINE INDENT h1 = ord ( str [ 0 ] ) - ord ( '0' ) ; NEW_LINE h2 = ord ( str [ 1 ] ) - ord ( '0' ) ; NEW_LINE hh = h1 * 10 + h2 ; NEW_LINE Meridien = " " ; NEW_LINE if ( hh < 12 ) : NEW_LINE INDENT Meridien = " AM " ; NEW_LINE DEDENT else : NEW_LINE INDENT Meridien = " PM " ; NEW_LINE DEDENT hh %= 12 ; NEW_LINE if ( hh == 0 ) : NEW_LINE INDENT print ( "12" , end = " " ) ; NEW_LINE for i in range ( 2 , 8 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( hh , end = " " ) ; NEW_LINE for i in range ( 2 , 8 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) ; NEW_LINE DEDENT DEDENT print ( " β " + Meridien ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "17:35:20" ; NEW_LINE convert12 ( str ) ; NEW_LINE DEDENT
|
T40113 | class GFG { static boolean isDigitPresent ( int m , boolean hash [ ] ) { while ( m > 0 ) { if ( hash [ m % 10 ] ) return true ; m = m / 10 ; } return false ; } static int countDivisibles ( int n ) { boolean hash [ ] = new boolean [ 10 ] ; int m = n ; while ( m > 0 ) { hash [ m % 10 ] = true ; m = m / 10 ; } int ans = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( isDigitPresent ( i , hash ) ) ans ++ ; if ( n / i != i ) { if ( isDigitPresent ( n / i , hash ) ) ans ++ ; } } } return ans ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . print ( countDivisibles ( n ) ) ; } }
| import math NEW_LINE def isDigitPresent ( m , Hash ) : NEW_LINE INDENT while ( m ) : NEW_LINE INDENT if ( Hash [ m % 10 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT m = m // 10 NEW_LINE DEDENT return False NEW_LINE DEDENT def countDivisibles ( n ) : NEW_LINE INDENT Hash = [ False for i in range ( 10 ) ] NEW_LINE m = n NEW_LINE while ( m ) : NEW_LINE INDENT Hash [ m % 10 ] = True NEW_LINE m = m // 10 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( isDigitPresent ( i , Hash ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if ( n // i != i ) : NEW_LINE INDENT if ( isDigitPresent ( n // i , Hash ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT n = 15 NEW_LINE print ( countDivisibles ( n ) ) NEW_LINE
|
T40114 | import java . io . * ; class GFG { static boolean findGreater ( int x , int y ) { if ( x > y ) { return false ; } else { return true ; } } public static void main ( String [ ] args ) { int x = 4 ; int y = 9 ; if ( findGreater ( x , y ) ) System . out . println ( "1" ) ; else System . out . println ( "2" ) ; } }
| def findGreater ( x , y ) : NEW_LINE INDENT if ( x > y ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT x = 4 ; NEW_LINE y = 9 ; NEW_LINE if ( findGreater ( x , y ) ) : NEW_LINE INDENT print ( "1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "2" ) ; NEW_LINE DEDENT
|
T40115 | import java . util . * ; class GFG { static void triangular_series ( int n ) { int i , j = 1 , k = 1 ; for ( i = 1 ; i <= n ; i ++ ) { System . out . printf ( " % d β " , k ) ; j = j + 1 ; k = k + j ; } } public static void main ( String [ ] args ) { int n = 5 ; triangular_series ( n ) ; } }
| def triangular_series ( n ) : NEW_LINE INDENT j = 1 NEW_LINE k = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( k , end = ' β ' ) NEW_LINE j = j + 1 NEW_LINE k = k + j NEW_LINE DEDENT DEDENT n = 5 NEW_LINE triangular_series ( n ) NEW_LINE
|
T40116 | import java . io . * ; class GFG { static int N = 3 ; static int maxPathSum ( int tri [ ] [ ] , int m , int n ) { for ( int i = m - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j <= i ; j ++ ) { if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ; } } return tri [ 0 ] [ 0 ] ; } public static void main ( String [ ] args ) { int tri [ ] [ ] = { { 1 , 0 , 0 } , { 4 , 8 , 0 } , { 1 , 5 , 3 } } ; System . out . println ( maxPathSum ( tri , 2 , 2 ) ) ; } }
| N = 3 NEW_LINE def maxPathSum ( tri , m , n ) : NEW_LINE INDENT for i in range ( m - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT return tri [ 0 ] [ 0 ] NEW_LINE DEDENT tri = [ [ 1 , 0 , 0 ] , [ 4 , 8 , 0 ] , [ 1 , 5 , 3 ] ] NEW_LINE print ( maxPathSum ( tri , 2 , 2 ) ) NEW_LINE
|
T40117 | import java . io . * ; class GFG { static int findSubSequence ( String s , int num ) { int res = 0 ; int i = 0 ; while ( num > 0 ) { if ( ( num & 1 ) == 1 ) res += s . charAt ( i ) - '0' ; i ++ ; num = num >> 1 ; } return res ; } static int combinedSum ( String s ) { int n = s . length ( ) ; int c_sum = 0 ; int range = ( 1 << n ) - 1 ; for ( int i = 0 ; i <= range ; i ++ ) c_sum += findSubSequence ( s , i ) ; return c_sum ; } public static void main ( String [ ] args ) { String s = "123" ; System . out . println ( combinedSum ( s ) ) ; } }
| def findSubSequence ( s , num ) : NEW_LINE INDENT res = 0 NEW_LINE i = 0 NEW_LINE while ( num ) : NEW_LINE INDENT if ( num & 1 ) : NEW_LINE INDENT res += ord ( s [ i ] ) - ord ( '0' ) NEW_LINE DEDENT i += 1 NEW_LINE num = num >> 1 NEW_LINE DEDENT return res NEW_LINE DEDENT def combinedSum ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE c_sum = 0 NEW_LINE ran = ( 1 << n ) - 1 NEW_LINE for i in range ( ran + 1 ) : NEW_LINE INDENT c_sum += findSubSequence ( s , i ) NEW_LINE DEDENT return c_sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "123" NEW_LINE print ( combinedSum ( s ) ) NEW_LINE DEDENT
|
T40118 | class GFG { static String sumBaseB ( String a , String b , int base ) { int len_a , len_b ; len_a = a . length ( ) ; len_b = b . length ( ) ; String sum , s ; s = " " ; sum = " " ; int diff ; diff = Math . abs ( len_a - len_b ) ; for ( int i = 1 ; i <= diff ; i ++ ) s += "0" ; if ( len_a < len_b ) a = s + a ; else b = s + b ; int curr , carry = 0 ; for ( int i = Math . max ( len_a , len_b ) - 1 ; i > - 1 ; i -- ) { curr = carry + ( a . charAt ( i ) - '0' ) + ( b . charAt ( i ) - '0' ) ; carry = curr / base ; curr = curr % base ; sum = ( char ) ( curr + '0' ) + sum ; } if ( carry > 0 ) sum = ( char ) ( carry + '0' ) + sum ; return sum ; } public static void main ( String [ ] args ) { String a , b , sum ; int base ; a = "123" ; b = "234" ; base = 6 ; sum = sumBaseB ( a , b , base ) ; System . out . println ( sum ) ; } }
| def sumBaseB ( a , b , base ) : NEW_LINE INDENT len_a = len ( a ) NEW_LINE len_b = len ( b ) NEW_LINE s = " " ; NEW_LINE sum = " " ; NEW_LINE diff = abs ( len_a - len_b ) ; NEW_LINE for i in range ( 1 , diff + 1 ) : NEW_LINE INDENT s += "0" NEW_LINE DEDENT if ( len_a < len_b ) : NEW_LINE INDENT a = s + a NEW_LINE DEDENT else : NEW_LINE INDENT b = s + b ; NEW_LINE DEDENT carry = 0 ; NEW_LINE for i in range ( max ( len_a , len_b ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT curr = carry + ( ord ( a [ i ] ) - ord ( '0' ) ) + ( ord ( b [ i ] ) - ord ( '0' ) ) ; NEW_LINE carry = curr // base NEW_LINE curr = curr % base ; NEW_LINE sum = chr ( curr + ord ( '0' ) ) + sum NEW_LINE DEDENT if ( carry > 0 ) : NEW_LINE INDENT sum = chr ( carry + ord ( '0' ) ) + sum ; NEW_LINE DEDENT return sum NEW_LINE DEDENT a = "123" NEW_LINE b = "234" NEW_LINE base = 6 NEW_LINE sum = sumBaseB ( a , b , base ) ; NEW_LINE print ( sum ) NEW_LINE
|
T40119 | import java . util . * ; class GFG { static int minAbsDiff ( int n ) { int left = ( int ) Math . pow ( 2 , ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ) ; int right = left * 2 ; return Math . min ( ( n - left ) , ( right - n ) ) ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( minAbsDiff ( n ) ) ; } }
| from math import floor , log2 NEW_LINE def minAbsDiff ( n ) : NEW_LINE INDENT left = pow ( 2 , floor ( log2 ( n ) ) ) NEW_LINE right = left * 2 NEW_LINE return min ( ( n - left ) , ( right - n ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 15 NEW_LINE print ( minAbsDiff ( n ) ) NEW_LINE DEDENT
|
T40120 | class GFG { static class Node { int data ; Node next ; Node prev ; } ; static Node insertNode ( Node start , int value ) { if ( start == null ) { Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = new_node . prev = new_node ; start = new_node ; return new_node ; } Node last = ( start ) . prev ; Node new_node = new Node ( ) ; new_node . data = value ; new_node . next = start ; ( start ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return start ; } static void displayList ( Node start ) { Node temp = start ; while ( temp . next != start ) { System . out . printf ( " % d β " , temp . data ) ; temp = temp . next ; } System . out . printf ( " % d β " , temp . data ) ; } static int searchList ( Node start , int search ) { Node temp = start ; int count = 0 , flag = 0 , value ; if ( temp == null ) return - 1 ; else { while ( temp . next != start ) { count ++ ; if ( temp . data == search ) { flag = 1 ; count -- ; break ; } temp = temp . next ; } if ( temp . data == search ) { count ++ ; flag = 1 ; } if ( flag == 1 ) System . out . println ( " \n " + search + " β found β at β location β " + count ) ; else System . out . println ( " \n " + search + " β not β found " ) ; } return - 1 ; } public static void main ( String args [ ] ) { Node start = null ; start = insertNode ( start , 4 ) ; start = insertNode ( start , 5 ) ; start = insertNode ( start , 7 ) ; start = insertNode ( start , 8 ) ; start = insertNode ( start , 6 ) ; System . out . printf ( " Created β circular β doubly β linked β list β is : β " ) ; displayList ( start ) ; searchList ( start , 5 ) ; } }
| import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def insertNode ( start , value ) : NEW_LINE INDENT if ( start == None ) : NEW_LINE INDENT new_node = Node ( value ) NEW_LINE new_node . data = value NEW_LINE new_node . next = new_node NEW_LINE new_node . prev = new_node NEW_LINE start = new_node NEW_LINE return new_node NEW_LINE DEDENT last = start . prev NEW_LINE new_node = Node ( value ) NEW_LINE new_node . data = value NEW_LINE new_node . next = start NEW_LINE ( start ) . prev = new_node NEW_LINE new_node . prev = last NEW_LINE last . next = new_node NEW_LINE return start NEW_LINE DEDENT def displayList ( start ) : NEW_LINE INDENT temp = start NEW_LINE while ( temp . next != start ) : NEW_LINE INDENT print ( temp . data , end = " β " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE DEDENT def searchList ( start , search ) : NEW_LINE INDENT temp = start NEW_LINE count = 0 NEW_LINE flag = 0 NEW_LINE value = 0 NEW_LINE if ( temp == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT while ( temp . next != start ) : NEW_LINE INDENT count = count + 1 NEW_LINE if ( temp . data == search ) : NEW_LINE INDENT flag = 1 NEW_LINE count = count - 1 NEW_LINE break NEW_LINE DEDENT temp = temp . next NEW_LINE DEDENT if ( temp . data == search ) : NEW_LINE INDENT count = count + 1 NEW_LINE flag = 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( search , " found β at β location β " , count ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( search , " β not β found " ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT start = None NEW_LINE start = insertNode ( start , 4 ) NEW_LINE start = insertNode ( start , 5 ) NEW_LINE start = insertNode ( start , 7 ) NEW_LINE start = insertNode ( start , 8 ) NEW_LINE start = insertNode ( start , 6 ) NEW_LINE print ( " Created β circular β doubly β linked β list β is : β " , end = " " ) NEW_LINE displayList ( start ) NEW_LINE searchList ( start , 5 ) NEW_LINE DEDENT
|
T40121 | public class Improve { static void pattern ( int nos , int i , int space ) { char prt = ' $ ' ; int s , j ; for ( s = nos ; s >= 1 ; s -- ) { System . out . print ( " β β " ) ; } for ( j = 1 ; j <= i ; j ++ ) { if ( space != 0 ) { if ( i == 9 && j == 1 ) { continue ; } } if ( i == 1 || i == 9 ) { System . out . print ( prt + " " ) ; } else if ( j == 1 || j == i ) { System . out . print ( prt + " " ) ; } else { System . out . print ( " β β " ) ; } } } public static void main ( String args [ ] ) { int i , nos = 0 , nosp = - 1 , nbsp = - 1 ; for ( i = 9 ; i >= 1 ; i -= 2 ) { pattern ( nos , i , 0 ) ; pattern ( nosp , i , 1 ) ; pattern ( nbsp , i , 1 ) ; System . out . println ( ) ; nos ++ ; nosp = nosp + 2 ; nbsp = nbsp + 2 ; } nos = 3 ; nosp = 5 ; nbsp = 5 ; for ( i = 3 ; i <= 9 ; i += 2 ) { pattern ( nos , i , 0 ) ; pattern ( nosp , i , 1 ) ; pattern ( nbsp , i , 1 ) ; System . out . println ( ) ; nos -- ; nosp = nosp - 2 ; nbsp = nbsp - 2 ; } } }
| def pattern ( nos , i , space ) : NEW_LINE INDENT prt = ' $ ' NEW_LINE for s in range ( nos , 0 , - 1 ) : NEW_LINE INDENT print ( end = " β β " ) NEW_LINE DEDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( space != 0 ) : NEW_LINE INDENT if ( i == 9 and j == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT if ( i == 1 or i == 9 ) : NEW_LINE INDENT print ( prt , end = " " ) NEW_LINE DEDENT elif ( j == 1 or j == i ) : NEW_LINE INDENT print ( prt , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( end = " β β " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT nos = 0 NEW_LINE nosp = - 1 NEW_LINE nbsp = - 1 NEW_LINE for i in range ( 9 , 0 , - 2 ) : NEW_LINE INDENT pattern ( nos , i , 0 ) NEW_LINE pattern ( nosp , i , 1 ) NEW_LINE pattern ( nbsp , i , 1 ) NEW_LINE print ( ) NEW_LINE nos += 1 NEW_LINE nosp = nosp + 2 NEW_LINE nbsp = nbsp + 2 NEW_LINE DEDENT nos = 3 NEW_LINE nosp = 5 NEW_LINE nbsp = 5 NEW_LINE for i in range ( 3 , 10 , 2 ) : NEW_LINE INDENT pattern ( nos , i , 0 ) NEW_LINE pattern ( nosp , i , 1 ) NEW_LINE pattern ( nbsp , i , 1 ) NEW_LINE print ( ) NEW_LINE nos -= 1 NEW_LINE nosp = nosp - 2 NEW_LINE nbsp = nbsp - 2 NEW_LINE DEDENT DEDENT
|
T40122 | import java . util . * ; class solution { static boolean prime [ ] = new boolean [ 100006 ] ; static void SieveOfEratosthenes ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } } static void sortedArray ( int arr [ ] , int n ) { SieveOfEratosthenes ( 100005 ) ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == false ) v . add ( arr [ i ] ) ; } Collections . sort ( v ) ; int j = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ arr [ i ] ] == true ) System . out . print ( arr [ i ] + " β " ) ; else { System . out . print ( v . get ( j ) + " β " ) ; j ++ ; } } } public static void main ( String args [ ] ) { int n = 6 ; int arr [ ] = { 100 , 11 , 500 , 2 , 17 , 1 } ; sortedArray ( arr , n ) ; } }
| from math import sqrt NEW_LINE prime = [ 0 ] * 100005 NEW_LINE def SieveOfEratosthenes ( n ) : NEW_LINE INDENT for i in range ( len ( prime ) ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , n , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sortedArray ( arr , n ) : NEW_LINE INDENT SieveOfEratosthenes ( 100005 ) NEW_LINE v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] == 0 : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] == True : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( v [ j ] , end = " β " ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE arr = [ 100 , 11 , 500 , 2 , 17 , 1 ] NEW_LINE sortedArray ( arr , n ) NEW_LINE DEDENT
|
T40123 | public class GFG { static int msbPos ( int n ) { int pos = 0 ; while ( n != 0 ) { pos ++ ; n = n >> 1 ; } return pos ; } static int josephify ( int n ) { int position = msbPos ( n ) ; int j = 1 << ( position - 1 ) ; n = n ^ j ; n = n << 1 ; n = n | 1 ; return n ; } public static void main ( String [ ] args ) { int n = 41 ; System . out . println ( josephify ( n ) ) ; } }
| def msbPos ( n ) : NEW_LINE INDENT pos = 0 NEW_LINE while n != 0 : NEW_LINE INDENT pos += 1 NEW_LINE n = n >> 1 NEW_LINE DEDENT return pos NEW_LINE DEDENT def josephify ( n ) : NEW_LINE INDENT position = msbPos ( n ) NEW_LINE j = 1 << ( position - 1 ) NEW_LINE n = n ^ j NEW_LINE n = n << 1 NEW_LINE n = n | 1 NEW_LINE return n NEW_LINE DEDENT n = 41 NEW_LINE print ( josephify ( n ) ) NEW_LINE
|
T40124 | import java . util . * ; class GFG { static boolean isPalindrome ( int freq [ ] , int k ) { int flag = 0 ; int length = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] == 0 ) continue ; else if ( freq [ i ] == 1 ) flag = 1 ; else { if ( freq [ i ] % 2 == 1 ) flag = 1 ; length += freq [ i ] / 2 ; } } if ( k % 2 == 1 ) { if ( 2 * length + flag >= k ) return true ; } else { if ( 2 * length >= k ) return true ; } return false ; } static boolean check ( String str , int m , int k ) { int [ ] freq = new int [ 26 ] ; for ( int i = 0 ; i < m ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; if ( isPalindrome ( freq , k ) ) return true ; for ( int i = m ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i - m ) - ' a ' ] -- ; freq [ str . charAt ( i ) - ' a ' ] ++ ; if ( isPalindrome ( freq , k ) ) return true ; } return false ; } static int find ( String str , int n , int k ) { int l = k ; int h = n ; int ans = - 1 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( check ( str , m , k ) ) { ans = m ; h = m - 1 ; } else l = m + 1 ; } return ans ; } public static void main ( String [ ] args ) { String str = " abcda " ; int n = str . length ( ) ; int k = 2 ; System . out . println ( find ( str , n , k ) ) ; } }
| def isPalindrome ( freq , k ) : NEW_LINE INDENT flag = 0 NEW_LINE length = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( freq [ i ] == 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( freq [ i ] & 1 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT length += freq [ i ] // 2 NEW_LINE DEDENT DEDENT if ( k & 1 ) : NEW_LINE INDENT if ( 2 * length + flag >= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( 2 * length >= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def check ( str , m , k ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT if ( isPalindrome ( freq , k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( m , len ( str ) , 1 ) : NEW_LINE INDENT freq [ ord ( str [ i - m ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( isPalindrome ( freq , k ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def find ( str , n , k ) : NEW_LINE INDENT l = k NEW_LINE h = n NEW_LINE ans = - 1 NEW_LINE while ( l <= h ) : NEW_LINE INDENT m = ( l + h ) // 2 NEW_LINE if ( check ( str , m , k ) ) : NEW_LINE INDENT ans = m NEW_LINE h = m - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " abcda " NEW_LINE n = len ( str ) NEW_LINE k = 2 NEW_LINE print ( find ( str , n , k ) ) NEW_LINE DEDENT
|
T40125 | import java . io . * ; import java . util . * ; class GFG { static void distSumRec ( int arr [ ] , int n , int sum , int currindex , HashSet < Integer > s ) { if ( currindex > n ) return ; if ( currindex == n ) { s . add ( sum ) ; return ; } distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) ; distSumRec ( arr , n , sum , currindex + 1 , s ) ; } static void printDistSum ( int arr [ ] , int n ) { HashSet < Integer > s = new HashSet < > ( ) ; distSumRec ( arr , n , 0 , 0 , s ) ; for ( int i : s ) System . out . print ( i + " β " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; printDistSum ( arr , n ) ; } }
| def distSumRec ( arr , n , sum , currindex , s ) : NEW_LINE INDENT if ( currindex > n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( currindex == n ) : NEW_LINE INDENT s . add ( sum ) NEW_LINE return NEW_LINE DEDENT distSumRec ( arr , n , sum + arr [ currindex ] , currindex + 1 , s ) NEW_LINE distSumRec ( arr , n , sum , currindex + 1 , s ) NEW_LINE DEDENT def printDistSum ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE distSumRec ( arr , n , 0 , 0 , s ) NEW_LINE for i in s : NEW_LINE INDENT print ( i , end = " β " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printDistSum ( arr , n ) NEW_LINE DEDENT
|
T40126 | class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) { return b ; } return gcd ( b % a , a ) ; } static long divTermCount ( long a , long b , long c , long num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; } static long findNthTerm ( int a , int b , int c , long n ) { long low = 1 , high = Long . MAX_VALUE , mid ; while ( low < high ) { mid = low + ( high - low ) / 2 ; if ( divTermCount ( a , b , c , mid ) < n ) { low = mid + 1 ; } else { high = mid ; } } return low ; } public static void main ( String args [ ] ) { int a = 2 , b = 3 , c = 5 , n = 100 ; System . out . println ( findNthTerm ( a , b , c , n ) ) ; } }
| import sys NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def divTermCount ( a , b , c , num ) : NEW_LINE INDENT return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; NEW_LINE DEDENT def findNthTerm ( a , b , c , n ) : NEW_LINE INDENT low = 1 ; high = sys . maxsize ; mid = 0 ; NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = low + ( high - low ) / 2 ; NEW_LINE if ( divTermCount ( a , b , c , mid ) < n ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = mid ; NEW_LINE DEDENT DEDENT return int ( low ) ; NEW_LINE DEDENT a = 2 ; b = 3 ; c = 5 ; n = 100 ; NEW_LINE print ( findNthTerm ( a , b , c , n ) ) ; NEW_LINE
|
T40127 | class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 4 ; arr [ 2 ] = 7 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 4 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 7 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( printNthElement ( n ) ) ; } }
| def printNthElement ( n ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) ; NEW_LINE arr [ 1 ] = 4 NEW_LINE arr [ 2 ] = 7 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i // 2 ] * 10 + 4 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = arr [ ( i // 2 ) - 1 ] * 10 + 7 NEW_LINE DEDENT DEDENT return arr [ n ] NEW_LINE DEDENT n = 6 NEW_LINE print ( printNthElement ( n ) ) NEW_LINE
|
T40128 | import java . io . * ; class Nth { public int nthTerm ( int N ) { return ( N * ( ( N % 2 ) + ( N % 3 ) ) ) ; } } class GFG { public static void main ( String [ ] args ) { int N = 5 ; Nth a = new Nth ( ) ; System . out . println ( " Nth β term β for β N β = β " + N + " β : β " + a . nthTerm ( N ) ) ; } }
| def nthTerm ( N ) : NEW_LINE INDENT return ( N * ( ( N % 2 ) + ( N % 3 ) ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( " Nth β term β for β N β = β " , N , " β : β " , nthTerm ( N ) ) NEW_LINE DEDENT
|
T40129 | import java . util . * ; class GFG { static int countSubSeq ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) { count ++ ; } } return ( int ) ( Math . pow ( 2 , count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 6 } ; int n = arr . length ; int k = 3 ; System . out . println ( countSubSeq ( arr , n , k ) ) ; } }
| def countSubSeq ( arr , n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return ( 2 ** count - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 3 ; NEW_LINE print ( countSubSeq ( arr , n , k ) ) ; NEW_LINE DEDENT
|
T40130 | import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static Scanner scn = new Scanner ( System . in ) ; public static int CountDecreasingPathsCell ( int mat [ ] [ ] , int dp [ ] [ ] , int n , int x , int y ) { if ( dp [ x ] [ y ] != - 1 ) return dp [ x ] [ y ] ; int delta [ ] [ ] = { { 0 , 1 } , { 1 , 0 } , { - 1 , 0 } , { 0 , - 1 } } ; int newx , newy ; int ans = 1 ; for ( int i = 0 ; i < 4 ; i ++ ) { newx = x + delta [ i ] [ 0 ] ; newy = y + delta [ i ] [ 1 ] ; if ( newx >= 0 && newx < n && newy >= 0 && newy < n && mat [ newx ] [ newy ] < mat [ x ] [ y ] ) { ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) ; } } return dp [ x ] [ y ] = ans ; } public static int countDecreasingPathsMatrix ( int n , int mat [ ] [ ] ) { int dp [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = - 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) ; return sum ; } public static void main ( String [ ] args ) { int n = 2 ; int mat [ ] [ ] = { { 1 , 2 } , { 1 , 3 } } ; System . out . println ( countDecreasingPathsMatrix ( n , mat ) ) ; } }
| MAX = 100 NEW_LINE def CountDecreasingPathsCell ( mat , dp , n , x , y ) : NEW_LINE INDENT if ( dp [ x ] [ y ] != - 1 ) : NEW_LINE INDENT return dp [ x ] [ y ] NEW_LINE DEDENT delta = [ [ 0 , 1 ] , [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , - 1 ] ] NEW_LINE newx , newy = 0 , 0 NEW_LINE ans = 1 NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT newx = x + delta [ i ] [ 0 ] NEW_LINE newy = y + delta [ i ] [ 1 ] NEW_LINE if ( newx >= 0 and newx < n and newy >= 0 and newy < n and mat [ newx ] [ newy ] < mat [ x ] [ y ] ) : NEW_LINE INDENT ans += CountDecreasingPathsCell ( mat , dp , n , newx , newy ) NEW_LINE DEDENT DEDENT dp [ x ] [ y ] = ans NEW_LINE return dp [ x ] [ y ] NEW_LINE DEDENT def countDecreasingPathsMatrix ( n , mat ) : NEW_LINE INDENT dp = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT l = [ ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT l . append ( - 1 ) NEW_LINE DEDENT dp . append ( l ) NEW_LINE DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT sum += CountDecreasingPathsCell ( mat , dp , n , i , j ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 2 NEW_LINE mat = [ [ 1 , 2 ] , [ 1 , 3 ] ] NEW_LINE print ( countDecreasingPathsMatrix ( n , mat ) ) NEW_LINE
|
T40131 | class GFG { static void printAP ( int a , int d , int n ) { int curr_term ; curr_term = a ; for ( int i = 1 ; i <= n ; i ++ ) { System . out . print ( curr_term + " β " ) ; curr_term = curr_term + d ; } } public static void main ( String [ ] args ) { int a = 2 ; int d = 1 ; int n = 5 ; printAP ( a , d , n ) ; } }
| def printAP ( a , d , n ) : NEW_LINE INDENT curr_term NEW_LINE DEDENT curr_term = a NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE DEDENT print ( curr_term , end = ' β ' ) NEW_LINE INDENT curr_term = curr_term + d NEW_LINE DEDENT a = 2 NEW_LINE d = 1 NEW_LINE n = 5 NEW_LINE printAP ( a , d , n ) NEW_LINE
|
T40132 | import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int countWays ( String s ) { int count [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; count [ s . charAt ( 0 ) - ' a ' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; } public static void main ( String ags [ ] ) { String s = " acbbcc " ; System . out . println ( countWays ( s ) ) ; } }
| def countWays ( s ) : NEW_LINE INDENT count = [ 0 ] * 26 ; NEW_LINE for x in s : NEW_LINE INDENT count [ ord ( x ) - ord ( ' a ' ) ] = ( count [ ord ( x ) - ord ( ' a ' ) ] ) + 1 ; NEW_LINE DEDENT count [ ord ( s [ 0 ] ) - ord ( ' a ' ) ] = 1 ; NEW_LINE ans = 1 ; NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( count [ i ] != 0 ) : NEW_LINE INDENT ans *= count [ i ] ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " acbbcc " ; NEW_LINE print ( countWays ( s ) ) ; NEW_LINE DEDENT
|
T40133 | class GFG { static int findDigitalRoot ( int num ) { int sum = Integer . MAX_VALUE , tempNum = num ; while ( sum >= 10 ) { sum = 0 ; while ( tempNum > 0 ) { sum += tempNum % 10 ; tempNum /= 10 ; } tempNum = sum ; } return sum ; } static void findAnswer ( int X , int N ) { int counter = 0 ; for ( int i = 1 ; counter < N ; ++ i ) { int digitalRoot = findDigitalRoot ( i ) ; if ( digitalRoot == X ) { ++ counter ; } if ( counter == N ) { System . out . print ( i ) ; break ; } } } public static void main ( String args [ ] ) { int X = 1 , N = 3 ; findAnswer ( X , N ) ; } }
| import sys NEW_LINE def findDigitalRoot ( num ) : NEW_LINE INDENT sum = sys . maxsize ; NEW_LINE tempNum = num ; NEW_LINE while ( sum >= 10 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( tempNum > 0 ) : NEW_LINE INDENT sum += tempNum % 10 ; NEW_LINE tempNum //= 10 ; NEW_LINE DEDENT tempNum = sum ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def findAnswer ( X , N ) : NEW_LINE INDENT counter = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( counter < N ) : NEW_LINE INDENT i += 1 ; NEW_LINE digitalRoot = findDigitalRoot ( i ) ; NEW_LINE if ( digitalRoot == X ) : NEW_LINE INDENT counter += 1 ; NEW_LINE DEDENT if ( counter == N ) : NEW_LINE INDENT print ( i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 1 ; NEW_LINE N = 3 ; NEW_LINE findAnswer ( X , N ) ; NEW_LINE DEDENT
|
T40134 | import java . util . Arrays ; class CoinChange { static long countWays ( int S [ ] , int m , int n ) { long [ ] table = new long [ n + 1 ] ; Arrays . fill ( table , 0 ) ; table [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = S [ i ] ; j <= n ; j ++ ) table [ j ] += table [ j - S [ i ] ] ; return table [ n ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int m = arr . length ; int n = 4 ; System . out . println ( countWays ( arr , m , n ) ) ; } }
| def count ( S , m , n ) : NEW_LINE INDENT table = [ [ 0 for x in range ( m ) ] for x in range ( n + 1 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT table [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT x = table [ i - S [ j ] ] [ j ] if i - S [ j ] >= 0 else 0 NEW_LINE y = table [ i ] [ j - 1 ] if j >= 1 else 0 NEW_LINE table [ i ] [ j ] = x + y NEW_LINE DEDENT DEDENT return table [ n ] [ m - 1 ] NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE m = len ( arr ) NEW_LINE n = 4 NEW_LINE print ( count ( arr , m , n ) ) NEW_LINE
|
T40135 | class GFG { static int MAX = 26 ; static int largestSubSeq ( String arr [ ] , int n ) { int [ ] count = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { String str = arr [ i ] ; boolean [ ] hash = new boolean [ MAX ] ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) { hash [ str . charAt ( j ) - ' a ' ] = true ; } for ( int j = 0 ; j < MAX ; j ++ ) { if ( hash [ j ] ) count [ j ] ++ ; } } int max = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( max < count [ i ] ) max = count [ i ] ; } return max ; } public static void main ( String [ ] args ) { String arr [ ] = { " ab " , " bc " , " de " } ; int n = arr . length ; System . out . println ( largestSubSeq ( arr , n ) ) ; } }
| MAX = 26 NEW_LINE def largestSubSeq ( arr , n ) : NEW_LINE INDENT count = [ 0 ] * MAX NEW_LINE for i in range ( n ) : NEW_LINE INDENT string = arr [ i ] NEW_LINE _hash = [ False ] * MAX NEW_LINE for j in range ( len ( string ) ) : NEW_LINE INDENT _hash [ ord ( string [ j ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT for j in range ( MAX ) : NEW_LINE INDENT if _hash [ j ] == True : NEW_LINE INDENT count [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT return max ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " ab " , " bc " , " de " ] NEW_LINE n = len ( arr ) NEW_LINE print ( largestSubSeq ( arr , n ) ) NEW_LINE DEDENT
|
T40136 | class GFG { static int findXOR ( int n ) { int mod = n % 4 ; if ( mod == 0 ) return n ; else if ( mod == 1 ) return 1 ; else if ( mod == 2 ) return n + 1 ; else if ( mod == 3 ) return 0 ; return 0 ; } static int findXOR ( int l , int r ) { return ( findXOR ( l - 1 ) ^ findXOR ( r ) ) ; } public static void main ( String [ ] args ) { int l = 4 , r = 8 ; System . out . println ( findXOR ( l , r ) ) ; } }
| from operator import xor NEW_LINE def findXOR ( n ) : NEW_LINE INDENT mod = n % 4 ; NEW_LINE if ( mod == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT elif ( mod == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( mod == 2 ) : NEW_LINE INDENT return n + 1 ; NEW_LINE DEDENT elif ( mod == 3 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def findXORFun ( l , r ) : NEW_LINE INDENT return ( xor ( findXOR ( l - 1 ) , findXOR ( r ) ) ) ; NEW_LINE DEDENT l = 4 ; r = 8 ; NEW_LINE print ( findXORFun ( l , r ) ) ; NEW_LINE
|
T40137 | class GFG { static long numbers ( int n ) { return ( long ) ( Math . pow ( 2 , n + 1 ) ) - 2 ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( numbers ( n ) ) ; } }
| def numbers ( n ) : NEW_LINE INDENT return pow ( 2 , n + 1 ) - 2 NEW_LINE DEDENT n = 2 NEW_LINE print ( numbers ( n ) ) NEW_LINE
|
T40138 | import java . io . * ; class GFG { static int mod = 1000000007 ; static int sumOddFibonacci ( int n ) { int Sum [ ] = new int [ n + 1 ] ; Sum [ 0 ] = 0 ; Sum [ 1 ] = 1 ; Sum [ 2 ] = 2 ; Sum [ 3 ] = 5 ; Sum [ 4 ] = 10 ; Sum [ 5 ] = 23 ; for ( int i = 6 ; i <= n ; i ++ ) { Sum [ i ] = ( ( Sum [ i - 1 ] + ( 4 * Sum [ i - 2 ] ) % mod - ( 4 * Sum [ i - 3 ] ) % mod + mod ) % mod + ( Sum [ i - 4 ] - Sum [ i - 5 ] + mod ) % mod ) % mod ; } return Sum [ n ] ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( sumOddFibonacci ( n ) ) ; } }
| mod = 1000000007 ; NEW_LINE def sumOddFibonacci ( n ) : NEW_LINE INDENT Sum = [ 0 ] * ( n + 1 ) ; NEW_LINE Sum [ 0 ] = 0 ; NEW_LINE Sum [ 1 ] = 1 ; NEW_LINE Sum [ 2 ] = 2 ; NEW_LINE Sum [ 3 ] = 5 ; NEW_LINE Sum [ 4 ] = 10 ; NEW_LINE Sum [ 5 ] = 23 ; NEW_LINE for i in range ( 6 , n + 1 ) : NEW_LINE INDENT Sum [ i ] = ( ( Sum [ i - 1 ] + ( 4 * Sum [ i - 2 ] ) % mod - ( 4 * Sum [ i - 3 ] ) % mod + mod ) % mod + ( Sum [ i - 4 ] - Sum [ i - 5 ] + mod ) % mod ) % mod ; NEW_LINE DEDENT return Sum [ n ] ; NEW_LINE DEDENT n = 6 ; NEW_LINE print ( sumOddFibonacci ( n ) ) ; NEW_LINE
|
T40139 | public class CountXor { static int countXorPair ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( ( arr [ i ] ^ arr [ j ] ) % 2 == 1 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; System . out . println ( countXorPair ( arr , arr . length ) ) ; } }
| def countXorPair ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( ( arr [ i ] ^ arr [ j ] ) % 2 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countXorPair ( arr , n ) ) NEW_LINE DEDENT
|
T40140 | public class GFG { static float squareArea ( float l , float b , float h ) { if ( l < 0 || b < 0 || h < 0 ) return - 1 ; float a = ( l * b ) / ( l + b ) ; return a * a ; } public static void main ( String [ ] args ) { float l = 5 , b = 12 , h = 13 ; System . out . println ( squareArea ( l , b , h ) ) ; } }
| def squareArea ( l , b , h ) : NEW_LINE INDENT if l < 0 or b < 0 or h < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = ( l * b ) / ( l + b ) NEW_LINE return a * a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , b , h = 5 , 12 , 13 NEW_LINE print ( round ( squareArea ( l , b , h ) , 4 ) ) NEW_LINE DEDENT
|
T40141 | class GFG { static void findNthTerm ( int n ) { if ( n % 2 == 0 ) { n = n / 2 ; n = 2 * ( n - 1 ) ; System . out . println ( n / 2 ) ; } else { n = ( n / 2 ) + 1 ; n = 2 * ( n - 1 ) ; System . out . println ( n ) ; } } public static void main ( String args [ ] ) { int X = 10 ; findNthTerm ( X ) ; X = 7 ; findNthTerm ( X ) ; } }
| def findNthTerm ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE n = 2 * ( n - 1 ) NEW_LINE print ( n // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT n = ( n // 2 ) + 1 NEW_LINE n = 2 * ( n - 1 ) NEW_LINE print ( n ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 10 NEW_LINE findNthTerm ( X ) ; NEW_LINE X = 7 ; NEW_LINE findNthTerm ( X ) NEW_LINE DEDENT
|
T40142 | class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxCommonFactors ( int a , int b ) { int __gcd = gcd ( a , b ) ; int ans = 1 ; for ( int i = 2 ; i * i <= __gcd ; i ++ ) { if ( __gcd % i == 0 ) { ans ++ ; while ( __gcd % i == 0 ) __gcd /= i ; } } if ( __gcd != 1 ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int a = 12 , b = 18 ; System . out . println ( maxCommonFactors ( a , b ) ) ; } }
| import math NEW_LINE def maxCommonFactors ( a , b ) : NEW_LINE INDENT gcd = math . gcd ( a , b ) NEW_LINE ans = 1 ; NEW_LINE i = 2 NEW_LINE while ( i * i <= gcd ) : NEW_LINE INDENT if ( gcd % i == 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE while ( gcd % i == 0 ) : NEW_LINE INDENT gcd = gcd // i NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( gcd != 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT a = 12 NEW_LINE b = 18 NEW_LINE print ( maxCommonFactors ( a , b ) ) NEW_LINE
|
T40143 | import java . io . * ; class GFG { static int averageOdd ( int n ) { if ( n % 2 == 0 ) { System . out . println ( " Invalid β Input " ) ; return - 1 ; } return ( n + 1 ) / 2 ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( averageOdd ( n ) ) ; } }
| def averageOdd ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " Invalid β Input " ) NEW_LINE return - 1 NEW_LINE DEDENT return ( n + 1 ) // 2 NEW_LINE DEDENT n = 15 NEW_LINE print ( averageOdd ( n ) ) NEW_LINE
|
T40144 | import java . io . * ; import java . util . * ; class GFG { static boolean Div_by_8 ( int n ) { return ( ( ( n >> 3 ) << 3 ) == n ) ; } public static void main ( String [ ] args ) { int n = 16 ; if ( Div_by_8 ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
| import math NEW_LINE def Div_by_8 ( n ) : NEW_LINE INDENT return ( ( ( n >> 3 ) << 3 ) == n ) NEW_LINE DEDENT n = 16 NEW_LINE if ( Div_by_8 ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
|
T40145 | import java . io . * ; class GFG { static int minMoves ( int n , int a [ ] , int k ) { int ct1 [ ] = new int [ k ] ; int ct0 [ ] = new int [ k ] ; int moves = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] == 1 ) ct1 [ i % k ] ++ ; else ct0 [ i % k ] ++ ; for ( int i = 0 ; i < k ; i ++ ) moves += Math . min ( ct1 [ i ] , ct0 [ i ] ) ; return moves ; } public static void main ( String [ ] args ) { int k = 2 ; int a [ ] = { 1 , 0 , 0 , 0 , 1 , 0 } ; int n = a . length ; System . out . println ( minMoves ( n , a , k ) ) ; } }
| def minMoves ( n , a , k ) : NEW_LINE INDENT ct1 = [ 0 for i in range ( k ) ] NEW_LINE ct0 = [ 0 for i in range ( k ) ] NEW_LINE moves = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 1 ) : NEW_LINE INDENT ct1 [ i % k ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ct0 [ i % k ] += 1 NEW_LINE DEDENT DEDENT for i in range ( k ) : NEW_LINE INDENT moves += min ( ct1 [ i ] , ct0 [ i ] ) NEW_LINE DEDENT return moves NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 2 NEW_LINE a = [ 1 , 0 , 0 , 0 , 1 , 0 ] NEW_LINE n = len ( a ) NEW_LINE print ( minMoves ( n , a , k ) ) NEW_LINE DEDENT
|
T40146 | import java . util . Arrays ; class GFG { static int minCost ( String str , int K ) { int n = str . length ( ) ; int res = 999999999 , count = 0 , a , b ; int cnt [ ] = new int [ 27 ] ; Arrays . fill ( cnt , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) cnt [ str . charAt ( i ) - ' a ' + 1 ] ++ ; for ( int i = 1 ; i < ( 26 - K + 1 ) ; i ++ ) { a = i ; b = i + K ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= a && j >= b ) count = count + ( Math . min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] ; else if ( j <= a && j <= b ) count = count + ( Math . min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } for ( int i = 26 - K + 1 ; i <= 26 ; i ++ ) { a = i ; b = ( i + K ) % 26 ; count = 0 ; for ( int j = 1 ; j <= 26 ; j ++ ) { if ( cnt [ j ] > 0 ) { if ( j >= b && j <= a ) count = count + ( Math . min ( j - b , a - j ) ) * cnt [ j ] ; } } res = Math . min ( res , count ) ; } return res ; } public static void main ( String [ ] args ) { String str = " abcdefghi " ; int K = 2 ; System . out . println ( minCost ( str , K ) ) ; } }
| def minCost ( str1 , K ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE res = 999999999 NEW_LINE count = 0 NEW_LINE cnt = [ 0 for i in range ( 27 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt [ ord ( str1 [ i ] ) - ord ( ' a ' ) + 1 ] += 1 NEW_LINE DEDENT for i in range ( 1 , 26 - K + 1 , 1 ) : NEW_LINE INDENT a = i NEW_LINE b = i + K NEW_LINE count = 0 NEW_LINE for j in range ( 1 , 27 , 1 ) : NEW_LINE INDENT if ( cnt [ j ] > 0 ) : NEW_LINE INDENT if ( j >= a and j >= b ) : NEW_LINE INDENT count = count + ( min ( j - b , 25 - j + a + 1 ) ) * cnt [ j ] NEW_LINE DEDENT elif ( j <= a and j <= b ) : NEW_LINE INDENT count = count + ( min ( a - j , 25 + j - b + 1 ) ) * cnt [ j ] NEW_LINE DEDENT DEDENT DEDENT res = min ( res , count ) NEW_LINE DEDENT for i in range ( 26 - K + 1 , 27 , 1 ) : NEW_LINE INDENT a = i NEW_LINE b = ( i + K ) % 26 NEW_LINE count = 0 NEW_LINE for j in range ( 1 , 27 , 1 ) : NEW_LINE INDENT if ( cnt [ j ] > 0 ) : NEW_LINE INDENT if ( j >= b and j <= a ) : NEW_LINE INDENT count = count + ( min ( j - b , a - j ) ) * cnt [ j ] NEW_LINE DEDENT DEDENT DEDENT res = min ( res , count ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abcdefghi " NEW_LINE K = 2 NEW_LINE print ( minCost ( str1 , K ) ) NEW_LINE DEDENT
|
T40147 | import java . util . * ; class GFG { static boolean isPossible ( int x , int y ) { if ( ( x - y ) == 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int x = 100 , y = 98 ; if ( isPossible ( x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
| def isPossible ( x , y ) : NEW_LINE INDENT if ( ( x - y ) == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT x = 100 NEW_LINE y = 98 NEW_LINE if ( isPossible ( x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
T40148 | class GFG { static int kN = 1000000 ; static int maxPeople ( int p ) { long [ ] sums = new long [ kN ] ; sums [ 0 ] = 0 ; for ( int i = 1 ; i < kN ; i ++ ) sums [ i ] = ( long ) ( i * i ) + sums [ i - 1 ] ; int it = lower_bound ( sums , 0 , kN , p ) ; if ( it > p ) { -- it ; } return it ; } private static int lower_bound ( long [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( element > a [ middle ] ) low = middle + 1 ; else high = middle ; } return low ; } public static void main ( String [ ] args ) { int p = 14 ; System . out . println ( maxPeople ( p ) ) ; } }
| kN = 1000000 ; NEW_LINE def maxPeople ( p ) : NEW_LINE INDENT sums = [ 0 ] * kN ; NEW_LINE sums [ 0 ] = 0 ; NEW_LINE for i in range ( 1 , kN ) : NEW_LINE INDENT sums [ i ] = ( i * i ) + sums [ i - 1 ] ; NEW_LINE DEDENT it = lower_bound ( sums , 0 , kN , p ) ; NEW_LINE if ( it > p ) : NEW_LINE INDENT it -= 1 ; NEW_LINE DEDENT return it ; NEW_LINE DEDENT def lower_bound ( a , low , high , element ) : NEW_LINE INDENT while ( low < high ) : NEW_LINE INDENT middle = int ( low + ( high - low ) / 2 ) ; NEW_LINE if ( element > a [ middle ] ) : NEW_LINE INDENT low = middle + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = middle ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 14 ; NEW_LINE print ( maxPeople ( p ) ) ; NEW_LINE DEDENT
|
T40149 | import java . util . * ; class GFG { static int sum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static void firstN ( int n ) { int num = 19 , cnt = 1 ; while ( cnt != n ) { if ( sum ( num ) == 10 ) { System . out . print ( num + " β " ) ; cnt ++ ; } num += 9 ; } } public static void main ( String [ ] args ) { int n = 10 ; firstN ( n ) ; } }
| def sum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT sum = sum + n % 10 ; NEW_LINE n = n // 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def firstN ( n ) : NEW_LINE INDENT num = 19 ; cnt = 1 ; NEW_LINE while ( cnt != n ) : NEW_LINE INDENT if ( sum ( num ) == 10 ) : NEW_LINE INDENT print ( num , end = " β " ) ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT num += 9 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE firstN ( n ) ; NEW_LINE DEDENT
|
T40150 | class GFG { static boolean isPrime ( int n ) { int flag = 1 ; for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { flag = 0 ; break ; } } return ( flag == 1 ? true : false ) ; } static boolean isPerfectSquare ( int x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static int countInterestingPrimes ( int n ) { int answer = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) { for ( int j = 1 ; j * j * j * j <= i ; j ++ ) { if ( isPerfectSquare ( i - j * j * j * j ) ) { answer ++ ; break ; } } } } return answer ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( countInterestingPrimes ( N ) ) ; } }
| import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT flag = 1 NEW_LINE i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return ( True if flag == 1 else False ) NEW_LINE DEDENT def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def countInterestingPrimes ( n ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( isPrime ( i ) != None ) : NEW_LINE INDENT j = 1 NEW_LINE while ( j * j * j * j <= i ) : NEW_LINE INDENT if ( isPerfectSquare ( i - j * j * j * j ) ) : NEW_LINE INDENT answer += 1 NEW_LINE break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE print ( countInterestingPrimes ( N ) ) NEW_LINE DEDENT
|
T40151 | import java . io . * ; class GFG { static void angleequichord ( int z ) { System . out . println ( " The β angle β subtended β at β the β center β is β " + z + " β degrees " ) ; } public static void main ( String [ ] args ) { int z = 48 ; angleequichord ( z ) ; } }
| def angleequichord ( z ) : NEW_LINE INDENT print ( " The β angle β subtended β at " , " the β center β is " , z , " degrees " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT z = 48 ; NEW_LINE angleequichord ( z ) ; NEW_LINE DEDENT
|
T40152 | class GFG { static int n , prod ; static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; return head_ref ; } static void productOfLastN_Nodes ( Node head ) { if ( head == null ) return ; productOfLastN_Nodes ( head . next ) ; if ( n > 0 ) { prod = prod * head . data ; -- n ; } } static int productOfLastN_NodesUtil ( Node head ) { if ( n <= 0 ) return 0 ; prod = 1 ; productOfLastN_Nodes ( head ) ; return prod ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , 12 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; n = 2 ; System . out . print ( productOfLastN_NodesUtil ( head ) ) ; } }
| n , prod = 0 , 0 ; NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_Node = Node ( 0 ) ; NEW_LINE new_Node . data = new_data ; NEW_LINE new_Node . next = head_ref ; NEW_LINE head_ref = new_Node ; NEW_LINE return head_ref ; NEW_LINE DEDENT def productOfLastN_Nodes ( head ) : NEW_LINE INDENT global n , prod ; NEW_LINE if ( head == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT productOfLastN_Nodes ( head . next ) ; NEW_LINE if ( n > 0 ) : NEW_LINE INDENT prod = prod * head . data ; NEW_LINE n -= 1 ; NEW_LINE DEDENT DEDENT def productOfLastN_NodesUtil ( head ) : NEW_LINE INDENT global n , prod ; NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT prod = 1 ; NEW_LINE productOfLastN_Nodes ( head ) ; NEW_LINE return prod ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None ; NEW_LINE n = 2 ; NEW_LINE head = push ( head , 12 ) ; NEW_LINE head = push ( head , 4 ) ; NEW_LINE head = push ( head , 8 ) ; NEW_LINE head = push ( head , 6 ) ; NEW_LINE head = push ( head , 10 ) ; NEW_LINE print ( productOfLastN_NodesUtil ( head ) ) ; NEW_LINE DEDENT
|
T40153 | class GFG { static void canMake ( int n , int ar [ ] ) { int sum = 0 , maxx = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += ar [ i ] ; maxx = Math . max ( maxx , ar [ i ] ) ; } if ( n == 1 || sum % 2 == 1 || sum - maxx < maxx ) { System . out . print ( " No \n " ) ; } else { System . out . print ( " Yes \n " ) ; } } public static void main ( String [ ] args ) { int n = 6 ; int arr [ ] = { 1 , 1 , 2 , 3 , 6 , 11 } ; canMake ( n , arr ) ; } }
| def canMake ( n , ar ) : NEW_LINE INDENT sum = 0 ; maxx = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ar [ i ] ; NEW_LINE maxx = max ( maxx , ar [ i ] ) ; NEW_LINE DEDENT if ( n == 1 or sum % 2 == 1 or sum - maxx < maxx ) : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE arr = [ 1 , 1 , 2 , 3 , 6 , 11 ] ; NEW_LINE canMake ( n , arr ) ; NEW_LINE DEDENT
|
T40154 | class GFG { static void SieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = false ; } } } static void findPrimePair ( int n ) { int flag = 0 ; boolean [ ] isPrime = new boolean [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 2 ; i < n ; i ++ ) { int x = n / i ; if ( isPrime [ i ] && isPrime [ x ] && x != i && x * i == n ) { System . out . println ( i + " β " + x ) ; flag = 1 ; return ; } } if ( flag == 0 ) System . out . println ( " No β such β pair β found " ) ; } public static void main ( String [ ] args ) { int n = 39 ; findPrimePair ( n ) ; } }
| from math import * NEW_LINE def SieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] , isPrime [ 1 ] = False , False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT for p in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if isPrime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def findPrimePair ( n ) : NEW_LINE INDENT flag = 0 NEW_LINE isPrime = [ False ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , isPrime ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT x = int ( n / i ) NEW_LINE if ( isPrime [ i ] & isPrime [ x ] and x != i and x * i == n ) : NEW_LINE INDENT print ( i , x ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if not flag : NEW_LINE INDENT print ( " No β such β pair β found " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 39 ; NEW_LINE findPrimePair ( n ) NEW_LINE DEDENT
|
T40155 | import java . util . * ; class GFG { static int findLargest ( int n , Vector < Integer > v ) { int flag = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( v . get ( i ) == 0 ) flag = 1 ; sum += v . get ( i ) ; } if ( flag != 1 ) System . out . println ( " Not β possible " ) ; else { Collections . sort ( v , Collections . reverseOrder ( ) ) ; if ( v . get ( 0 ) == 0 ) { System . out . println ( "0" ) ; return 0 ; } else { int flags = 0 ; int y = ( int ) ( sum % 3 ) ; if ( y != 0 ) { for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v . get ( i ) % 3 == y ) { v . remove ( i ) ; flags = 1 ; break ; } } if ( flags == 0 ) { y = 3 - y ; int cnt = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( v . get ( i ) % 3 == y ) { v . remove ( i ) ; cnt ++ ; if ( cnt >= 2 ) break ; } } } } if ( v . get ( 0 ) == 0 ) System . out . println ( "0" ) ; else for ( Integer i : v ) { System . out . print ( i ) ; } } } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 } ; int n = 11 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) v . add ( i , arr [ i ] ) ; findLargest ( n , v ) ; } }
| def findLargest ( n , v ) : NEW_LINE INDENT flag = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT sum += v [ i ] NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not β possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . sort ( reverse = True ) NEW_LINE if ( v [ 0 ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE y = sum % 3 NEW_LINE if ( y != 0 ) : NEW_LINE INDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT y = 3 - y NEW_LINE cnt = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE cnt += 1 NEW_LINE if ( cnt >= 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT if ( v [ 0 ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in ( v ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 11 NEW_LINE v = [ 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 ] NEW_LINE findLargest ( n , v ) NEW_LINE DEDENT
|
T40156 | class GFG { private static int gcd ( int reduceNum , int b ) { return b == 0 ? reduceNum : gcd ( b , reduceNum % b ) ; } private static int reduceB ( int a , String b ) { int result = 0 ; for ( int i = 0 ; i < b . length ( ) ; i ++ ) { result = ( result * 10 + b . charAt ( i ) - '0' ) % a ; } return result ; } private static int gcdLarge ( int a , String b ) { int num = reduceB ( a , b ) ; return gcd ( num , a ) ; } public static void main ( String [ ] args ) { int a = 1221 ; String b = "19837658191095787329" ; if ( a == 0 ) System . out . println ( b ) ; else System . out . println ( gcdLarge ( a , b ) ) ; } }
| def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def reduceB ( a , b ) : NEW_LINE INDENT mod = 0 NEW_LINE for i in range ( 0 , len ( b ) ) : NEW_LINE INDENT mod = ( mod * 10 + ord ( b [ i ] ) ) % a NEW_LINE DEDENT return mod NEW_LINE DEDENT def gcdLarge ( a , b ) : NEW_LINE INDENT num = reduceB ( a , b ) NEW_LINE return gcd ( a , num ) NEW_LINE DEDENT a = 1221 NEW_LINE b = "1234567891011121314151617181920212223242526272829" NEW_LINE if a == 0 : NEW_LINE INDENT print ( b ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( gcdLarge ( a , b ) ) NEW_LINE DEDENT
|
T40157 | class Solution { static void countOddRotations ( int n ) { int odd_count = 0 , even_count = 0 ; do { int digit = n % 10 ; if ( digit % 2 == 1 ) odd_count ++ ; else even_count ++ ; n = n / 10 ; } while ( n != 0 ) ; System . out . println ( " Odd β = β " + odd_count ) ; System . out . println ( " Even β = β " + even_count ) ; } public static void main ( String [ ] args ) { int n = 1234 ; countOddRotations ( n ) ; } }
| def countOddRotations ( n ) : NEW_LINE INDENT odd_count = 0 ; even_count = 0 NEW_LINE while n != 0 : NEW_LINE INDENT digit = n % 10 NEW_LINE if digit % 2 == 0 : NEW_LINE INDENT odd_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even_count += 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT print ( " Odd β = " , odd_count ) NEW_LINE print ( " Even β = " , even_count ) NEW_LINE DEDENT n = 1234 NEW_LINE countOddRotations ( n ) NEW_LINE
|
T40158 | import java . util . HashMap ; import java . util . Map ; public class GfG { static int findSubarraySum ( int arr [ ] , int n , int sum ) { HashMap < Integer , Integer > prevSum = new HashMap < > ( ) ; int res = 0 ; int currsum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { currsum += arr [ i ] ; if ( currsum == sum ) res ++ ; if ( prevSum . containsKey ( currsum - sum ) ) res += prevSum . get ( currsum - sum ) ; Integer count = prevSum . get ( currsum ) ; if ( count == null ) prevSum . put ( currsum , 1 ) ; else prevSum . put ( currsum , count + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , - 2 , - 20 , 10 } ; int sum = - 10 ; int n = arr . length ; System . out . println ( findSubarraySum ( arr , n , sum ) ) ; } }
| from collections import defaultdict NEW_LINE def findSubarraySum ( arr , n , Sum ) : NEW_LINE INDENT prevSum = defaultdict ( lambda : 0 ) NEW_LINE res = 0 NEW_LINE currsum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT currsum += arr [ i ] NEW_LINE if currsum == Sum : NEW_LINE INDENT res += 1 NEW_LINE DEDENT if ( currsum - Sum ) in prevSum : NEW_LINE INDENT res += prevSum [ currsum - Sum ] NEW_LINE DEDENT prevSum [ currsum ] += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 2 , - 2 , - 20 , 10 ] NEW_LINE Sum = - 10 NEW_LINE n = len ( arr ) NEW_LINE print ( findSubarraySum ( arr , n , Sum ) ) NEW_LINE DEDENT
|
T40159 | import java . util . * ; class GFG { static int N = 1000 ; static int lastElement ( int a [ ] , int n ) { int steps = 1 ; Vector < Integer > [ ] v = new Vector [ N ] ; for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; if ( n == 1 ) return a [ 0 ] ; for ( int i = 0 ; i < n ; i += 2 ) v [ steps ] . add ( a [ i ] | a [ i + 1 ] ) ; while ( v [ steps ] . size ( ) > 1 ) { steps += 1 ; for ( int i = 0 ; i < v [ steps - 1 ] . size ( ) ; i += 2 ) { if ( steps % 2 == 1 ) v [ steps ] . add ( v [ steps - 1 ] . get ( i ) | v [ steps - 1 ] . get ( i + 1 ) ) ; else v [ steps ] . add ( v [ steps - 1 ] . get ( i ) ^ v [ steps - 1 ] . get ( i + 1 ) ) ; } } return v [ steps ] . get ( 0 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 5 , 6 } ; int n = a . length ; int index = 0 ; int value = 2 ; a [ 0 ] = 2 ; System . out . println ( lastElement ( a , n ) ) ; index = 3 ; value = 5 ; a [ index ] = value ; System . out . println ( lastElement ( a , n ) ) ; } }
| N = 1000 NEW_LINE def lastElement ( a , n ) : NEW_LINE INDENT steps = 1 NEW_LINE v = [ [ ] for i in range ( n ) ] NEW_LINE if n == 1 : return a [ 0 ] NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT v [ steps ] . append ( a [ i ] | a [ i + 1 ] ) NEW_LINE DEDENT while len ( v [ steps ] ) > 1 : NEW_LINE INDENT steps += 1 NEW_LINE for i in range ( 0 , len ( v [ steps - 1 ] ) , 2 ) : NEW_LINE INDENT if steps & 1 : NEW_LINE INDENT v [ steps ] . append ( v [ steps - 1 ] [ i ] | v [ steps - 1 ] [ i + 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT v [ steps ] . append ( v [ steps - 1 ] [ i ] ^ v [ steps - 1 ] [ i + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return v [ steps ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 4 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE index , value , a [ 0 ] = 0 , 2 , 2 NEW_LINE print ( lastElement ( a , n ) ) NEW_LINE index , value = 3 , 5 NEW_LINE value = 5 NEW_LINE a [ index ] = value NEW_LINE print ( lastElement ( a , n ) ) NEW_LINE DEDENT
|
T40160 | import java . util . * ; class GFG { static int findMaxDiff ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " Invalid β " ) ; return 0 ; } int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( res < ( arr [ i ] - arr [ j ] - i + j ) ) res = ( arr [ i ] - arr [ j ] - i + j ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 15 , 4 , 12 , 13 } ; int n = arr . length ; System . out . print ( findMaxDiff ( arr , n ) ) ; } }
| def findMaxDiff ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " Invalid β " ) NEW_LINE return 0 NEW_LINE DEDENT res = - 2147483648 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( res < ( arr [ i ] - arr [ j ] - i + j ) ) : NEW_LINE INDENT res = ( arr [ i ] - arr [ j ] - i + j ) NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 9 , 15 , 4 , 12 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxDiff ( arr , n ) ) NEW_LINE
|
T40161 | import java . util . * ; class GFG { static int toK ( int N , int K ) { int w = 1 ; int s = 0 ; while ( N != 0 ) { int r = N % K ; N = N / K ; s = r * w + s ; w *= 10 ; } return s ; } static boolean check ( int N ) { boolean fl = false ; while ( N != 0 ) { int r = N % 10 ; N = N / 10 ; if ( fl == true && r == 0 ) return false ; if ( r > 0 ) { fl = false ; continue ; } fl = true ; } return true ; } static void hasConsecutiveZeroes ( int N , int K ) { int z = toK ( N , K ) ; if ( check ( z ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 15 ; int K = 8 ; hasConsecutiveZeroes ( N , K ) ; } }
| def hasConsecutiveZeroes ( N , K ) : NEW_LINE INDENT z = toK ( N , K ) NEW_LINE if ( check ( z ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT def toK ( N , K ) : NEW_LINE INDENT w = 1 NEW_LINE s = 0 NEW_LINE while ( N != 0 ) : NEW_LINE INDENT r = N % K NEW_LINE N = N // K NEW_LINE s = r * w + s NEW_LINE w * = 10 NEW_LINE DEDENT return s NEW_LINE DEDENT def check ( N ) : NEW_LINE INDENT fl = False NEW_LINE while ( N != 0 ) : NEW_LINE INDENT r = N % 10 NEW_LINE N = N // 10 NEW_LINE if ( fl == True and r == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( r > 0 ) : NEW_LINE INDENT fl = False NEW_LINE continue NEW_LINE DEDENT fl = True NEW_LINE DEDENT return True NEW_LINE DEDENT N , K = 15 , 8 NEW_LINE hasConsecutiveZeroes ( N , K ) NEW_LINE
|
T40162 | class Subset_sum { static int countStrings ( int n ) { int a [ ] = new int [ n ] ; int b [ ] = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return a [ n - 1 ] + b [ n - 1 ] ; } public static void main ( String args [ ] ) { System . out . println ( countStrings ( 3 ) ) ; } }
| def countStrings ( n ) : NEW_LINE INDENT a = [ 0 for i in range ( n ) ] NEW_LINE b = [ 0 for i in range ( n ) ] NEW_LINE a [ 0 ] = b [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] + b [ i - 1 ] NEW_LINE b [ i ] = a [ i - 1 ] NEW_LINE DEDENT return a [ n - 1 ] + b [ n - 1 ] NEW_LINE DEDENT print ( countStrings ( 3 ) ) NEW_LINE
|
T40163 | class gfg { static boolean isPrime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static int sum ( int l , int r ) { int sum = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( isPrime ( i ) ) continue ; for ( int j = 2 ; j < i ; j ++ ) { if ( i % j == 0 && isPrime ( j ) ) sum += j ; } } return sum ; } public static void main ( String [ ] args ) { int l = 18 , r = 25 ; System . out . println ( sum ( l , r ) ) ; } }
| def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def sum ( l , r ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( 2 , i ) : NEW_LINE INDENT if ( i % j == 0 and isPrime ( j ) ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 18 NEW_LINE r = 25 NEW_LINE print ( sum ( l , r ) ) NEW_LINE DEDENT
|
T40164 | class GFG { static int lagDuration ( int h1 , int m1 , int h2 , int m2 , int k ) { int lag , t1 , t2 ; t1 = ( h1 + k ) * 60 + m1 ; t2 = h2 * 60 + m2 ; lag = t1 - t2 ; return lag ; } public static void main ( String args [ ] ) { int h1 = 12 , m1 = 0 ; int h2 = 12 , m2 = 58 ; int k = 1 ; int lag = lagDuration ( h1 , m1 , h2 , m2 , k ) ; System . out . println ( " Lag β = β " + lag + " β minutes " ) ; } }
| def lagDuration ( h1 , m1 , h2 , m2 , k ) : NEW_LINE INDENT lag , t1 , t2 = 0 , 0 , 0 NEW_LINE t1 = ( h1 + k ) * 60 + m1 NEW_LINE t2 = h2 * 60 + m2 NEW_LINE lag = t1 - t2 NEW_LINE return lag NEW_LINE DEDENT h1 , m1 = 12 , 0 NEW_LINE h2 , m2 = 12 , 58 NEW_LINE k = 1 NEW_LINE lag = lagDuration ( h1 , m1 , h2 , m2 , k ) NEW_LINE print ( " Lag β = " , lag , " minutes " ) NEW_LINE
|
T40165 | public class GFG { static double ReuleauxArea ( float a ) { if ( a < 0 ) return - 1 ; double A = ( double ) 0.70477 * Math . pow ( a , 2 ) ; return A ; } public static void main ( String args [ ] ) { float a = 6 ; System . out . println ( ReuleauxArea ( a ) ) ; } }
| import math as mt NEW_LINE def ReuleauxArea ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 0.70477 * pow ( a , 2 ) NEW_LINE DEDENT a = 6 NEW_LINE print ( ReuleauxArea ( a ) ) NEW_LINE
|
T40166 | class GFG { static int maximumSumSubarray ( int arr [ ] , int n ) { int min_prefix_sum = 0 ; int res = Integer . MIN_VALUE ; int prefix_sum [ ] = new int [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { res = Math . max ( res , prefix_sum [ i ] - min_prefix_sum ) ; min_prefix_sum = Math . min ( min_prefix_sum , prefix_sum [ i ] ) ; } return res ; } public static void main ( String [ ] args ) { int arr1 [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n1 = arr1 . length ; System . out . println ( maximumSumSubarray ( arr1 , n1 ) ) ; int arr2 [ ] = { 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 } ; int n2 = arr2 . length ; System . out . println ( maximumSumSubarray ( arr2 , n2 ) ) ; } }
| import math NEW_LINE def maximumSumSubarray ( arr , n ) : NEW_LINE INDENT min_prefix_sum = 0 NEW_LINE res = - math . inf NEW_LINE prefix_sum = [ ] NEW_LINE prefix_sum . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix_sum . append ( prefix_sum [ i - 1 ] + arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT res = max ( res , prefix_sum [ i ] - min_prefix_sum ) NEW_LINE min_prefix_sum = min ( min_prefix_sum , prefix_sum [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr1 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( maximumSumSubarray ( arr1 , n1 ) ) NEW_LINE arr2 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( maximumSumSubarray ( arr2 , n2 ) ) NEW_LINE
|
T40167 | import java . util . * ; class GFG { static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += ( n % 10 ) ; n /= 10 ; } return sum ; } static boolean isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } static boolean isDigitSumPalindrome ( int n ) { int sum = digitSum ( n ) ; if ( isPalindrome ( sum ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 56 ; if ( isDigitSumPalindrome ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def digitSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += ( n % 10 ) ; NEW_LINE n //= 10 ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def isPalindrome ( n ) : NEW_LINE INDENT divisor = 1 ; NEW_LINE while ( n // divisor >= 10 ) : NEW_LINE INDENT divisor *= 10 ; NEW_LINE DEDENT while ( n != 0 ) : NEW_LINE INDENT leading = n // divisor ; NEW_LINE trailing = n % 10 ; NEW_LINE if ( leading != trailing ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT n = ( n % divisor ) // 10 ; NEW_LINE divisor = divisor // 100 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def isDigitSumPalindrome ( n ) : NEW_LINE INDENT sum = digitSum ( n ) ; NEW_LINE if ( isPalindrome ( sum ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 56 ; NEW_LINE if ( isDigitSumPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
|
T40168 | import java . util . * ; class GFG { static int sumEqualProduct ( int a [ ] , int n ) { int zero = 0 , two = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } if ( a [ i ] == 2 ) { two ++ ; } } int cnt = ( zero * ( zero - 1 ) ) / 2 + ( two * ( two - 1 ) ) / 2 ; return cnt ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 3 , 4 , 2 , 6 } ; int n = a . length ; System . out . print ( sumEqualProduct ( a , n ) ) ; } }
| def sumEqualProduct ( a , n ) : NEW_LINE INDENT zero = 0 NEW_LINE two = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] == 0 : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT if a [ i ] == 2 : NEW_LINE INDENT two += 1 NEW_LINE DEDENT DEDENT cnt = ( zero * ( zero - 1 ) ) // 2 + \ NEW_LINE INDENT ( two * ( two - 1 ) ) // 2 NEW_LINE DEDENT return cnt NEW_LINE DEDENT a = [ 2 , 2 , 3 , 4 , 2 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( sumEqualProduct ( a , n ) ) NEW_LINE
|
T40169 | import java . util . * ; class GFG { static void maxProduct ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . println ( " No β pairs β exists " ) ; return ; } int a = arr [ 0 ] , b = arr [ 1 ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] * arr [ j ] > a * b ) { a = arr [ i ] ; b = arr [ j ] ; } System . out . println ( " Max β product β pair β is β { " + a + " , β " + b + " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 6 , 7 , 0 } ; int n = arr . length ; maxProduct ( arr , n ) ; } }
| def maxProduct ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " No β pairs β exists " ) NEW_LINE return NEW_LINE DEDENT a = arr [ 0 ] ; b = arr [ 1 ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] > a * b ) : NEW_LINE INDENT a = arr [ i ] ; b = arr [ j ] NEW_LINE DEDENT DEDENT DEDENT print ( " Max β product β pair β is β { " , a , " , " , b , " } " , sep = " " ) NEW_LINE DEDENT arr = [ 1 , 4 , 3 , 6 , 7 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE maxProduct ( arr , n ) NEW_LINE
|
T40170 | import java . io . * ; class GFG { static boolean isPowerOfTwo ( int n ) { if ( n == 0 ) return false ; while ( n != 1 ) { if ( n % 2 != 0 ) return false ; n = n / 2 ; } return true ; } public static void main ( String args [ ] ) { if ( isPowerOfTwo ( 31 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( isPowerOfTwo ( 64 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def isPowerOfTwo ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT while ( n != 1 ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = n // 2 NEW_LINE DEDENT return True NEW_LINE DEDENT if ( isPowerOfTwo ( 31 ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT if ( isPowerOfTwo ( 64 ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT
|
T40171 | import java . util . * ; class MyDS { ArrayList < Integer > arr ; HashMap < Integer , Integer > hash ; public MyDS ( ) { arr = new ArrayList < Integer > ( ) ; hash = new HashMap < Integer , Integer > ( ) ; } void add ( int x ) { if ( hash . get ( x ) != null ) return ; int s = arr . size ( ) ; arr . add ( x ) ; hash . put ( x , s ) ; } void remove ( int x ) { Integer index = hash . get ( x ) ; if ( index == null ) return ; hash . remove ( x ) ; int size = arr . size ( ) ; Integer last = arr . get ( size - 1 ) ; Collections . swap ( arr , index , size - 1 ) ; arr . remove ( size - 1 ) ; hash . put ( last , index ) ; } int getRandom ( ) { Random rand = new Random ( ) ; int index = rand . nextInt ( arr . size ( ) ) ; return arr . get ( index ) ; } Integer search ( int x ) { return hash . get ( x ) ; } } class Main { public static void main ( String [ ] args ) { MyDS ds = new MyDS ( ) ; ds . add ( 10 ) ; ds . add ( 20 ) ; ds . add ( 30 ) ; ds . add ( 40 ) ; System . out . println ( ds . search ( 30 ) ) ; ds . remove ( 20 ) ; ds . add ( 50 ) ; System . out . println ( ds . search ( 50 ) ) ; System . out . println ( ds . getRandom ( ) ) ; } }
| import random NEW_LINE class MyDS : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . arr = [ ] NEW_LINE self . hashd = { } NEW_LINE DEDENT def add ( self , x ) : NEW_LINE INDENT if x in self . hashd : NEW_LINE INDENT return NEW_LINE DEDENT s = len ( self . arr ) NEW_LINE self . arr . append ( x ) NEW_LINE self . hashd [ x ] = s NEW_LINE DEDENT def remove ( self , x ) : NEW_LINE INDENT index = self . hashd . get ( x , None ) NEW_LINE if index == None : NEW_LINE INDENT return NEW_LINE DEDENT del self . hashd [ x ] NEW_LINE size = len ( self . arr ) NEW_LINE last = self . arr [ size - 1 ] NEW_LINE self . arr [ index ] , \ NEW_LINE self . arr [ size - 1 ] = self . arr [ size - 1 ] , \ NEW_LINE INDENT self . arr [ index ] NEW_LINE DEDENT del self . arr [ - 1 ] NEW_LINE self . hashd [ last ] = index NEW_LINE DEDENT def getRandom ( self ) : NEW_LINE INDENT index = random . randrange ( 0 , len ( self . arr ) ) NEW_LINE return self . arr [ index ] NEW_LINE DEDENT def search ( self , x ) : NEW_LINE INDENT return self . hashd . get ( x , None ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ds = MyDS ( ) NEW_LINE ds . add ( 10 ) NEW_LINE ds . add ( 20 ) NEW_LINE ds . add ( 30 ) NEW_LINE ds . add ( 40 ) NEW_LINE print ( ds . search ( 30 ) ) NEW_LINE ds . remove ( 20 ) NEW_LINE ds . add ( 50 ) NEW_LINE print ( ds . search ( 50 ) ) NEW_LINE print ( ds . getRandom ( ) ) NEW_LINE DEDENT
|
T40172 | class GFG { static int setAllBitsAfterMSB ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return n ; } static int toggle ( int n ) { n = n ^ setAllBitsAfterMSB ( n ) ; return n ; } public static void main ( String arg [ ] ) { int n = 10 ; n = toggle ( n ) ; System . out . print ( n ) ; } }
| def setAllBitsAfterMSB ( n ) : NEW_LINE INDENT n |= n >> 1 NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE return n NEW_LINE DEDENT def toggle ( n ) : NEW_LINE INDENT n = n ^ setAllBitsAfterMSB ( n ) NEW_LINE return n NEW_LINE DEDENT n = 10 NEW_LINE n = toggle ( n ) NEW_LINE print ( n ) NEW_LINE
|
T40173 | import java . util . * ; import java . lang . * ; import java . io . * ; class exp_sq { static long N = 1000000007L ; public static void main ( String [ ] args ) { long base = 5 ; long exp = 100000 ; long modulo = exponentiation ( base , exp ) ; System . out . println ( modulo ) ; } static long exponentiation ( long base , long exp ) { if ( exp == 0 ) return 1 ; if ( exp == 1 ) return base % N ; long t = exponentiation ( base , exp / 2 ) ; t = ( t * t ) % N ; if ( exp % 2 == 0 ) return t ; else return ( ( base % N ) * t ) % N ; } }
| N = 1000000007 ; NEW_LINE def exponentiation ( bas , exp ) : NEW_LINE INDENT if ( exp == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( exp == 1 ) : NEW_LINE INDENT return bas % N ; NEW_LINE DEDENT t = exponentiation ( bas , int ( exp / 2 ) ) ; NEW_LINE t = ( t * t ) % N ; NEW_LINE if ( exp % 2 == 0 ) : NEW_LINE INDENT return t ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( bas % N ) * t ) % N ; NEW_LINE DEDENT DEDENT bas = 5 ; NEW_LINE exp = 100000 ; NEW_LINE modulo = exponentiation ( bas , exp ) ; NEW_LINE print ( modulo ) ; NEW_LINE
|
T40174 | import javafx . util . Pair ; import java . util . ArrayList ; import java . util . * ; class GfG { public static int minSwaps ( int [ ] arr ) { int n = arr . length ; ArrayList < Pair < Integer , Integer > > arrpos = new ArrayList < Pair < Integer , Integer > > ( ) ; for ( int i = 0 ; i < n ; i ++ ) arrpos . add ( new Pair < Integer , Integer > ( arr [ i ] , i ) ) ; arrpos . sort ( new Comparator < Pair < Integer , Integer > > ( ) { @ Override public int compare ( Pair < Integer , Integer > o1 , Pair < Integer , Integer > o2 ) { if ( o1 . getKey ( ) > o2 . getKey ( ) ) return - 1 ; else if ( o1 . getKey ( ) . equals ( o2 . getKey ( ) ) ) return 0 ; else return 1 ; } } ) ; Boolean [ ] vis = new Boolean [ n ] ; Arrays . fill ( vis , false ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( vis [ i ] || arrpos . get ( i ) . getValue ( ) == i ) continue ; int cycle_size = 0 ; int j = i ; while ( ! vis [ j ] ) { vis [ j ] = true ; j = arrpos . get ( j ) . getValue ( ) ; cycle_size ++ ; } if ( cycle_size > 0 ) { ans += ( cycle_size - 1 ) ; } } return ans ; } } class MinSwaps { public static void main ( String [ ] args ) { int [ ] a = { 1 , 5 , 4 , 3 , 2 } ; GfG g = new GfG ( ) ; System . out . println ( g . minSwaps ( a ) ) ; } }
| def minSwaps ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE arrpos = [ * enumerate ( arr ) ] NEW_LINE arrpos . sort ( key = lambda it : it [ 1 ] ) NEW_LINE vis = { k : False for k in range ( n ) } NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if vis [ i ] or arrpos [ i ] [ 0 ] == i : NEW_LINE INDENT continue NEW_LINE DEDENT cycle_size = 0 NEW_LINE j = i NEW_LINE while not vis [ j ] : NEW_LINE INDENT vis [ j ] = True NEW_LINE j = arrpos [ j ] [ 0 ] NEW_LINE cycle_size += 1 NEW_LINE DEDENT if cycle_size > 0 : NEW_LINE INDENT ans += ( cycle_size - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 5 , 4 , 3 , 2 ] NEW_LINE print ( minSwaps ( arr ) ) NEW_LINE
|
T40175 | class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPrime ( n ) && ( n % 4 == 1 ) ) { 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 n = 13 NEW_LINE if ( isPrime ( n ) and ( n % 4 == 1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT
|
T40176 | import java . util . * ; class GFG { static int minimumChanges ( int arr [ ] , int n , int d ) { int maxFreq = - 1 ; HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int a0 = arr [ i ] - ( i ) * d ; if ( freq . containsKey ( a0 ) ) { freq . put ( a0 , freq . get ( a0 ) + 1 ) ; } else freq . put ( a0 , 1 ) ; if ( freq . get ( a0 ) > maxFreq ) maxFreq = freq . get ( a0 ) ; } return ( n - maxFreq ) ; } public static void main ( String args [ ] ) { int n = 5 , d = 1 ; int arr [ ] = { 1 , 3 , 3 , 4 , 6 } ; System . out . println ( minimumChanges ( arr , n , d ) ) ; } }
| def minimumChanges ( arr , n , d ) : NEW_LINE INDENT maxFreq = - 2147483648 NEW_LINE freq = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT a0 = arr [ i ] - i * d NEW_LINE if a0 in freq : NEW_LINE INDENT freq [ a0 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ a0 ] = 1 NEW_LINE DEDENT if freq [ a0 ] > maxFreq : NEW_LINE INDENT maxFreq = freq [ a0 ] NEW_LINE DEDENT DEDENT return ( n - maxFreq ) NEW_LINE DEDENT n = 5 NEW_LINE d = 1 NEW_LINE arr = [ 1 , 3 , 3 , 4 , 6 ] NEW_LINE ans = minimumChanges ( arr , n , d ) NEW_LINE print ( ans ) NEW_LINE
|
T40177 | class GFG { static int min_changes ( int a [ ] , int n ) { int ans_a = 0 , ans_b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { if ( a [ i ] == 0 ) ans_a ++ ; else ans_b ++ ; } else { if ( a [ i ] == 0 ) ans_b ++ ; else ans_a ++ ; } } return Math . min ( ans_a , ans_b ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 } ; int n = a . length ; System . out . println ( min_changes ( a , n ) ) ; } }
| def min_changes ( a , n ) : NEW_LINE INDENT ans_a = 0 ; NEW_LINE ans_b = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT ans_a += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans_b += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT ans_b += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans_a += 1 ; NEW_LINE DEDENT DEDENT DEDENT return min ( ans_a , ans_b ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 0 , 0 , 1 , 0 , 0 , 1 , 0 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( min_changes ( a , n ) ) ; NEW_LINE DEDENT
|
T40178 | public class GFG { static boolean rightRotationDivisor ( int N ) { int lastDigit = N % 10 ; int rightRotation = ( int ) ( lastDigit * Math . pow ( 10 , ( int ) ( Math . log10 ( N ) ) ) + Math . floor ( N / 10 ) ) ; return ( rightRotation % N == 0 ) ; } static void generateNumbers ( int m ) { for ( int i = ( int ) Math . pow ( 10 , ( m - 1 ) ) ; i < Math . pow ( 10 , m ) ; i ++ ) if ( rightRotationDivisor ( i ) ) System . out . println ( i ) ; } public static void main ( String args [ ] ) { int m = 3 ; generateNumbers ( m ) ; } }
| from math import log10 NEW_LINE def rightRotationDivisor ( N ) : NEW_LINE INDENT lastDigit = N % 10 NEW_LINE rightRotation = ( lastDigit * 10 ** int ( log10 ( N ) ) + N // 10 ) NEW_LINE return rightRotation % N == 0 NEW_LINE DEDENT def generateNumbers ( m ) : NEW_LINE INDENT for i in range ( 10 ** ( m - 1 ) , 10 ** m ) : NEW_LINE INDENT if rightRotationDivisor ( i ) : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT DEDENT m = 3 NEW_LINE generateNumbers ( m ) NEW_LINE
|
T40179 | class GFG { final static int MAX = 26 ; static int minOperation ( String str , int len ) { int first [ ] = new int [ MAX ] ; int last [ ] = new int [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { first [ i ] = - 1 ; last [ i ] = - 1 ; } for ( int i = 0 ; i < len ; i ++ ) { int index = ( str . charAt ( i ) - ' a ' ) ; if ( first [ index ] == - 1 ) first [ index ] = i ; last [ index ] = i ; } int minOp = - 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( first [ i ] == - 1 || first [ i ] == last [ i ] ) continue ; int cnt = len - ( last [ i ] - first [ i ] + 1 ) ; if ( minOp == - 1 || cnt < minOp ) minOp = cnt ; } return minOp ; } public static void main ( String [ ] args ) { String str = " abcda " ; int len = str . length ( ) ; System . out . println ( minOperation ( str , len ) ) ; } }
| MAX = 26 ; NEW_LINE def minOperation ( str , len ) : NEW_LINE INDENT first , last = [ 0 ] * MAX , [ 0 ] * MAX ; NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT first [ i ] = - 1 ; NEW_LINE last [ i ] = - 1 ; NEW_LINE DEDENT for i in range ( len ) : NEW_LINE INDENT index = ( ord ( str [ i ] ) - ord ( ' a ' ) ) ; NEW_LINE if ( first [ index ] == - 1 ) : NEW_LINE INDENT first [ index ] = i ; NEW_LINE DEDENT last [ index ] = i ; NEW_LINE DEDENT minOp = - 1 ; NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( first [ i ] == - 1 or first [ i ] == last [ i ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT cnt = len - ( last [ i ] - first [ i ] + 1 ) ; NEW_LINE if ( minOp == - 1 or cnt < minOp ) : NEW_LINE INDENT minOp = cnt ; NEW_LINE DEDENT DEDENT return minOp ; NEW_LINE DEDENT str = " abcda " ; NEW_LINE len = len ( str ) ; NEW_LINE print ( minOperation ( str , len ) ) ; NEW_LINE
|
T40180 | import java . io . * ; class GFG { static void printPrevSmaller ( int [ ] arr , int n ) { System . out . print ( " _ , β " ) ; for ( int i = 1 ; i < n ; i ++ ) { int j ; for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] < arr [ i ] ) { System . out . print ( arr [ j ] + " , β " ) ; break ; } } if ( j == - 1 ) System . out . print ( " _ , β " ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 0 , 2 , 5 } ; int n = arr . length ; printPrevSmaller ( arr , n ) ; } }
| def printPrevSmaller ( arr , n ) : NEW_LINE INDENT print ( " _ , β " , end = " " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i - 1 , - 2 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT print ( arr [ j ] , " , β " , end = " " ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( j == - 1 ) : NEW_LINE INDENT print ( " _ , β " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 3 , 0 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printPrevSmaller ( arr , n ) NEW_LINE
|
T40181 | import java . io . * ; class GFG { static float successiveChange ( int arr [ ] , int N ) { float var1 , var2 , result = 0 ; var1 = arr [ 0 ] ; var2 = arr [ 1 ] ; result = var1 + var2 + ( ( var1 * var2 ) / 100 ) ; for ( int i = 2 ; i < N ; i ++ ) result = result + arr [ i ] + ( ( result * arr [ i ] ) / 100 ) ; return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 10 } ; int N = arr . length ; float result = successiveChange ( arr , N ) ; System . out . println ( " Percentage β change β is β = β " + result + " β % " ) ; } }
| def successiveChange ( arr , N ) : NEW_LINE INDENT result = 0 ; NEW_LINE var1 = arr [ 0 ] ; NEW_LINE var2 = arr [ 1 ] ; NEW_LINE result = float ( var1 + var2 + ( float ( var1 * var2 ) / 100 ) ) ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT result = ( result + arr [ i ] + ( float ( result * arr [ i ] ) / 100 ) ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT arr = [ 10 , 20 , 30 , 10 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE result = successiveChange ( arr , N ) ; NEW_LINE print ( " Percentage β change β is β = β % .2f " % ( result ) , " % " ) ; NEW_LINE
|
T40182 | class GfG { static String leastLexiString ( String s ) { if ( s . length ( ) == 1 ) return s ; String x = leastLexiString ( s . substring ( 0 , s . length ( ) / 2 ) ) ; String y = leastLexiString ( s . substring ( s . length ( ) / 2 ) ) ; return String . valueOf ( ( x + y ) . compareTo ( y + x ) ) ; } static boolean areEquivalent ( String a , String b ) { return ! ( leastLexiString ( a ) . equals ( leastLexiString ( b ) ) ) ; } public static void main ( String [ ] args ) { String a = " aaba " ; String b = " abaa " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; a = " aabb " ; b = " abab " ; if ( areEquivalent ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def leastLexiString ( s ) : NEW_LINE INDENT if ( len ( s ) & 1 != 0 ) : NEW_LINE INDENT return s NEW_LINE DEDENT x = leastLexiString ( s [ 0 : int ( len ( s ) / 2 ) ] ) NEW_LINE y = leastLexiString ( s [ int ( len ( s ) / 2 ) : len ( s ) ] ) NEW_LINE return min ( x + y , y + x ) NEW_LINE DEDENT def areEquivalent ( a , b ) : NEW_LINE INDENT return ( leastLexiString ( a ) == leastLexiString ( b ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = " aaba " NEW_LINE b = " abaa " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT a = " aabb " NEW_LINE b = " abab " NEW_LINE if ( areEquivalent ( a , b ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
|
T40183 | import java . io . * ; class GFG { static int MAX = 1000000 ; static int maximumOccurredElement ( int [ ] L , int [ ] R , int n ) { int [ ] arr = new int [ MAX ] ; int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { arr [ L [ i ] ] += 1 ; arr [ R [ i ] + 1 ] -= 1 ; if ( R [ i ] > maxi ) { maxi = R [ i ] ; } } int msum = arr [ 0 ] ; int ind = 0 ; for ( int i = 1 ; i < maxi + 1 ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( msum < arr [ i ] ) { msum = arr [ i ] ; ind = i ; } } return ind ; } static public void main ( String [ ] args ) { int [ ] L = { 1 , 4 , 9 , 13 , 21 } ; int [ ] R = { 15 , 8 , 12 , 20 , 30 } ; int n = L . length ; System . out . println ( maximumOccurredElement ( L , R , n ) ) ; } }
| MAX = 1000000 NEW_LINE def maximumOccurredElement ( L , R , n ) : NEW_LINE INDENT arr = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT arr [ L [ i ] ] += 1 NEW_LINE arr [ R [ i ] + 1 ] -= 1 NEW_LINE DEDENT msum = arr [ 0 ] NEW_LINE for i in range ( 1 , MAX , 1 ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE if ( msum < arr [ i ] ) : NEW_LINE INDENT msum = arr [ i ] NEW_LINE ind = i NEW_LINE DEDENT DEDENT return ind NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = [ 1 , 4 , 9 , 13 , 21 ] NEW_LINE R = [ 15 , 8 , 12 , 20 , 30 ] NEW_LINE n = len ( L ) NEW_LINE print ( maximumOccurredElement ( L , R , n ) ) NEW_LINE DEDENT
|
T40184 | import java . io . * ; class GFG { static int centeredoctagonalNumber ( int n ) { return 4 * n * ( n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . print ( n + " th β centered β " + " octagonal β number : β " ) ; System . out . println ( centeredoctagonalNumber ( n ) ) ; n = 11 ; System . out . print ( n + " th β centered β " + " octagonal β number : β " ) ; System . out . println ( centeredoctagonalNumber ( n ) ) ; } }
| def cen_octagonalnum ( n ) : NEW_LINE INDENT return ( 4 * n * n - 4 * n + 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE print ( n , " th β Centered " , " octagonal β number : β " , cen_octagonalnum ( n ) ) NEW_LINE n = 11 NEW_LINE print ( n , " th β Centered " , " octagonal β number : β " , cen_octagonalnum ( n ) ) NEW_LINE DEDENT
|
T40185 | class Main { static int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ 20 ] ; arr [ 0 ] = 12 ; arr [ 1 ] = 16 ; arr [ 2 ] = 20 ; arr [ 3 ] = 40 ; arr [ 4 ] = 50 ; arr [ 5 ] = 70 ; int capacity = arr . length ; int n = 6 ; int key = 26 ; System . out . print ( " \n Before β Insertion : β " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " β " ) ; n = insertSorted ( arr , n , key , capacity ) ; System . out . print ( " \n After β Insertion : β " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " β " ) ; } }
| def insertSorted ( arr , n , key , capacity ) : NEW_LINE INDENT if ( n >= capacity ) : NEW_LINE INDENT return n NEW_LINE DEDENT i = n - 1 NEW_LINE while i >= 0 and arr [ i ] > key : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT arr [ i + 1 ] = key NEW_LINE return ( n + 1 ) NEW_LINE DEDENT arr = [ 12 , 16 , 20 , 40 , 50 , 70 ] NEW_LINE for i in range ( 20 ) : NEW_LINE INDENT arr . append ( 0 ) NEW_LINE DEDENT capacity = len ( arr ) NEW_LINE n = 6 NEW_LINE key = 26 NEW_LINE print ( " Before β Insertion : β " , end = " β " ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT n = insertSorted ( arr , n , key , capacity ) NEW_LINE print ( " \n After β Insertion : β " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " β " ) NEW_LINE DEDENT
|
T40186 | import java . util . * ; class Longest { public static void longestAlternating ( int arr [ ] , int n ) { int [ ] count = new int [ n ] ; count [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ; else count [ i ] = 1 ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( count [ i ] + " β " ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 5 , - 1 , - 1 , 2 , - 2 , - 3 } ; int n = 6 ; longestAlternating ( a , n ) ; } }
| def longestAlternating ( arr , n ) : NEW_LINE INDENT count = [ None ] * n NEW_LINE count [ n - 1 ] = 1 NEW_LINE i = n - 2 NEW_LINE while i >= 0 : NEW_LINE INDENT if ( arr [ i ] * arr [ i + 1 ] < 0 ) : NEW_LINE INDENT count [ i ] = count [ i + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT count [ i ] = 1 ; NEW_LINE DEDENT i = i - 1 NEW_LINE DEDENT i = 0 NEW_LINE while i < n : NEW_LINE INDENT print ( count [ i ] , end = " β " ) NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT a = [ - 5 , - 1 , - 1 , 2 , - 2 , - 3 ] NEW_LINE n = len ( a ) NEW_LINE longestAlternating ( a , n ) ; NEW_LINE
|
T40187 | class GFG { static int getPeriod ( int n ) { int rem = 1 ; for ( int i = 1 ; i <= n + 1 ; i ++ ) rem = ( 10 * rem ) % n ; int d = rem ; int count = 0 ; do { rem = ( 10 * rem ) % n ; count ++ ; } while ( rem != d ) ; return count ; } public static void main ( String [ ] args ) { System . out . println ( getPeriod ( 3 ) ) ; System . out . println ( getPeriod ( 7 ) ) ; } }
| def getPeriod ( n ) : NEW_LINE INDENT rem = 1 NEW_LINE for i in range ( 1 , n + 2 ) : NEW_LINE INDENT rem = ( 10 * rem ) % n NEW_LINE DEDENT d = rem NEW_LINE count = 0 NEW_LINE rem = ( 10 * rem ) % n NEW_LINE count += 1 NEW_LINE while rem != d : NEW_LINE INDENT rem = ( 10 * rem ) % n NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( getPeriod ( 3 ) ) NEW_LINE print ( getPeriod ( 7 ) ) NEW_LINE DEDENT
|
T40188 | import java . util . * ; class GFG { static String reversingString ( char [ ] str , int start , int end ) { while ( start < end ) { str [ start ] ^= str [ end ] ; str [ end ] ^= str [ start ] ; str [ start ] ^= str [ end ] ; ++ start ; -- end ; } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String s = " GeeksforGeeks " ; System . out . println ( reversingString ( s . toCharArray ( ) , 0 , 12 ) ) ; } }
| def reversingString ( str , start , end ) : NEW_LINE INDENT while ( start < end ) : NEW_LINE INDENT str = ( str [ : start ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ start + 1 : ] ) ; NEW_LINE str = ( str [ : end ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ end + 1 : ] ) ; NEW_LINE str = ( str [ : start ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ start + 1 : ] ) ; NEW_LINE start += 1 ; NEW_LINE end -= 1 ; NEW_LINE DEDENT return str ; NEW_LINE DEDENT s = " GeeksforGeeks " ; NEW_LINE print ( reversingString ( s , 0 , 12 ) ) ; NEW_LINE
|
T40189 | import java . util . HashSet ; class GFG { static void Maximum_xor_Triplet ( int n , int a [ ] ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { s . add ( a [ i ] ^ a [ j ] ) ; } } int ans = 0 ; for ( Integer i : s ) { for ( int j = 0 ; j < n ; j ++ ) { ans = Math . max ( ans , i ^ a [ j ] ) ; } } System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 8 , 15 } ; int n = a . length ; Maximum_xor_Triplet ( n , a ) ; } }
| def Maximum_xor_Triplet ( n , a ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT s . add ( a [ i ] ^ a [ j ] ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in s : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT ans = max ( ans , i ^ a [ j ] ) NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 3 , 8 , 15 ] NEW_LINE n = len ( a ) NEW_LINE Maximum_xor_Triplet ( n , a ) NEW_LINE DEDENT
|
T40190 | class GFG { final static int N = 2 ; final static int M = 3 ; static int traverseMatrix ( int arr [ ] [ ] , int current_row , int current_col ) { if ( current_col >= M ) return 0 ; if ( current_row >= N ) return 1 ; System . out . print ( arr [ current_row ] [ current_col ] + " , β " ) ; if ( traverseMatrix ( arr , current_row , current_col + 1 ) == 1 ) return 1 ; return traverseMatrix ( arr , current_row + 1 , 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; traverseMatrix ( arr , 0 , 0 ) ; } }
| N = 2 NEW_LINE M = 3 NEW_LINE def traverseMatrix ( arr , current_row , current_col ) : NEW_LINE INDENT if ( current_col >= M ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( current_row >= N ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT print ( arr [ current_row ] [ current_col ] , end = " , β " ) ; NEW_LINE if ( traverseMatrix ( arr , current_row , current_col + 1 ) == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return traverseMatrix ( arr , current_row + 1 , 0 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] ; NEW_LINE traverseMatrix ( arr , 0 , 0 ) ; NEW_LINE DEDENT
|
T40191 | public class GFG { static final int OUT = 0 ; static final int IN = 1 ; static int countWords ( String str ) { int state = OUT ; int wc = 0 ; int i = 0 ; while ( i < str . length ( ) ) { if ( str . charAt ( i ) == ' β ' || str . charAt ( i ) == ' \n ' || str . charAt ( i ) == ' \t ' ) state = OUT ; else if ( state == OUT ) { state = IN ; ++ wc ; } ++ i ; } return wc ; } public static void main ( String args [ ] ) { String str = " One β two β β β β β β β three \n β four\tfive β β " ; System . out . println ( " No β of β words β : β " + countWords ( str ) ) ; } }
| OUT = 0 NEW_LINE IN = 1 NEW_LINE def countWords ( string ) : NEW_LINE INDENT state = OUT NEW_LINE wc = 0 NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == ' β ' or string [ i ] == ' \n ' or string [ i ] == ' \t ' ) : NEW_LINE INDENT state = OUT NEW_LINE DEDENT elif state == OUT : NEW_LINE INDENT state = IN NEW_LINE wc += 1 NEW_LINE DEDENT DEDENT return wc NEW_LINE DEDENT string = " One β two β β β β β β β β β three \n β four\tfive β " NEW_LINE print ( " No . β of β words β : β " + str ( countWords ( string ) ) ) NEW_LINE
|
T40192 | import java . util . * ; class GFG { static void prime ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; Vector < Boolean > prime = new Vector < Boolean > ( ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . add ( Boolean . TRUE ) ; prime . add ( 0 , Boolean . FALSE ) ; prime . add ( 1 , Boolean . FALSE ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . add ( i , Boolean . FALSE ) ; } } int minimum = Integer . MAX_VALUE ; int maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) if ( prime . get ( arr [ i ] ) ) { minimum = Math . min ( minimum , arr [ i ] ) ; maximum = Math . max ( maximum , arr [ i ] ) ; } System . out . println ( " Minimum β : β " + minimum ) ; System . out . println ( " Maximum β : β " + maximum ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; prime ( arr , n ) ; } }
| import math as mt NEW_LINE def Prime ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , mt . ceil ( mt . sqrt ( max_val ) ) ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT minimum = 10 ** 9 NEW_LINE maximum = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] == True ) : NEW_LINE INDENT minimum = min ( minimum , arr [ i ] ) NEW_LINE maximum = max ( maximum , arr [ i ] ) NEW_LINE DEDENT DEDENT print ( " Minimum β : β " , minimum ) NEW_LINE print ( " Maximum β : β " , maximum ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE Prime ( arr , n ) NEW_LINE
|
T40193 | class GFG { static int matrix [ ] [ ] = new int [ 100 ] [ 100 ] ; static void printRequiredMatrix ( int n ) { if ( n == 1 ) { System . out . println ( "1" ) ; } else if ( n % 2 != 0 ) { System . out . println ( " - 1" ) ; } else { for ( int i = 0 ; i < n ; i ++ ) { matrix [ i ] [ i ] = n ; } int u = n - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { matrix [ i ] [ u ] = i + 1 ; for ( int j = 1 ; j < n / 2 ; j ++ ) { int a = ( i + j ) % ( n - 1 ) ; int b = ( i - j + n - 1 ) % ( n - 1 ) ; if ( a < b ) { int temp = a ; a = b ; b = temp ; } matrix [ b ] [ a ] = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " β " ) ; System . out . println ( ) ; } } System . out . println ( ) ; } public static void main ( String [ ] args ) { int n = 1 ; printRequiredMatrix ( n ) ; n = 3 ; printRequiredMatrix ( n ) ; n = 6 ; printRequiredMatrix ( n ) ; } }
| import numpy as np ; NEW_LINE matrix = np . zeros ( ( 100 , 100 ) ) ; NEW_LINE def printRequiredMatrix ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( "1" ) ; NEW_LINE DEDENT elif ( n % 2 != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT matrix [ i ] [ i ] = n ; NEW_LINE DEDENT u = n - 1 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT matrix [ i ] [ u ] = i + 1 ; NEW_LINE for j in range ( 1 , n // 2 ) : NEW_LINE INDENT a = ( i + j ) % ( n - 1 ) ; NEW_LINE b = ( i - j + n - 1 ) % ( n - 1 ) ; NEW_LINE if ( a < b ) : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT matrix [ b ] [ a ] = i + 1 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT matrix [ i ] [ j ] = matrix [ j ] [ i ] + n ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( matrix [ i ] [ j ] , end = " β " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1 ; NEW_LINE printRequiredMatrix ( n ) ; NEW_LINE n = 3 ; NEW_LINE printRequiredMatrix ( n ) ; NEW_LINE n = 6 ; NEW_LINE printRequiredMatrix ( n ) ; NEW_LINE DEDENT
|
T40194 | import java . io . * ; class GFG { static int rangeGCD ( int n , int m ) { return ( n == m ) ? n : 1 ; } public static void main ( String [ ] args ) { int n = 475 ; int m = 475 ; System . out . println ( rangeGCD ( n , m ) ) ; } }
| def rangeGCD ( n , m ) : NEW_LINE INDENT return n if ( n == m ) else 1 NEW_LINE DEDENT n , m = 475 , 475 NEW_LINE print ( rangeGCD ( n , m ) ) NEW_LINE
|
T40195 | class GFG { public static long productPrimeFactors ( int n ) { long product = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( n % i == 0 ) { boolean isPrime = true ; for ( int j = 2 ; j <= i / 2 ; j ++ ) { if ( i % j == 0 ) { isPrime = false ; break ; } } if ( isPrime ) { product = product * i ; } } } return product ; } public static void main ( String [ ] args ) { int n = 44 ; System . out . print ( productPrimeFactors ( n ) ) ; } }
| def productPrimeFactors ( n ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT isPrime = 1 NEW_LINE for j in range ( 2 , int ( i / 2 + 1 ) ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT isPrime = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( isPrime ) : NEW_LINE INDENT product = product * i NEW_LINE DEDENT DEDENT DEDENT return product NEW_LINE DEDENT n = 44 NEW_LINE print ( productPrimeFactors ( n ) ) NEW_LINE
|
T40196 | import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float trapezoidarea ( float r ) { if ( r < 0 ) return - 1 ; float a = ( 3 * ( float ) Math . sqrt ( 3 ) * ( float ) Math . pow ( r , 2 ) ) / 4 ; return a ; } public static void main ( String args [ ] ) { float r = 5 ; System . out . printf ( " % .3f " , trapezoidarea ( r ) ) ; } }
| from math import * NEW_LINE def trapezoidarea ( r ) : NEW_LINE INDENT if r < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = ( 3 * sqrt ( 3 ) * pow ( r , 2 ) ) / 4 NEW_LINE return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 NEW_LINE print ( round ( trapezoidarea ( r ) , 3 ) ) NEW_LINE DEDENT
|
T40197 | class GFG { static int findThirdDigit ( int n ) { if ( n < 3 ) return 0 ; return ( n & 1 ) > 0 ? 1 : 6 ; } public static void main ( String args [ ] ) { int n = 7 ; System . out . println ( findThirdDigit ( n ) ) ; } }
| def findThirdDigit ( n ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 if n and 1 else 6 NEW_LINE DEDENT n = 7 NEW_LINE print ( findThirdDigit ( n ) ) NEW_LINE
|
T40198 | import java . util . * ; class GFG { static int minimumRemoval ( int n , int a [ ] ) { Map < Integer , Integer > c = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( c . containsKey ( a [ i ] ) ) { c . put ( a [ i ] , c . get ( a [ i ] ) + 1 ) ; } else { c . put ( a [ i ] , 1 ) ; } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { boolean ok = false ; for ( int j = 0 ; j < 31 ; j ++ ) { int x = ( 1 << j ) - a [ i ] ; if ( ( c . get ( x ) != null && ( c . get ( x ) > 1 ) ) || c . get ( x ) != null && ( c . get ( x ) == 1 && x != a [ i ] ) ) { ok = true ; break ; } } if ( ! ok ) ans ++ ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 7 , 1 , 5 , 4 , 9 } ; int n = a . length ; System . out . println ( minimumRemoval ( n , a ) ) ; } }
| def minimumRemoval ( n , a ) : NEW_LINE INDENT c = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ a [ i ] ] += 1 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ok = False ; NEW_LINE for j in range ( 31 ) : NEW_LINE INDENT x = ( 1 << j ) - a [ i ] ; NEW_LINE if ( x in c and ( c [ x ] > 1 or ( c [ x ] == 1 and x != a [ i ] ) ) ) : NEW_LINE INDENT ok = True ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( not ok ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 7 , 1 , 5 , 4 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( minimumRemoval ( n , a ) ) ; NEW_LINE DEDENT
|
T40199 | class GFG { static boolean isPossible ( int x , int y , int k ) { int minMoves = Math . abs ( x ) + Math . abs ( y ) ; if ( k >= minMoves && ( k - minMoves ) % 2 == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int x = 5 , y = 8 , k = 20 ; if ( isPossible ( x , y , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
| def isPossible ( x , y , k ) : NEW_LINE INDENT minMoves = abs ( x ) + abs ( y ) NEW_LINE if ( k >= minMoves and ( k - minMoves ) % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT x = 5 NEW_LINE y = 8 NEW_LINE k = 20 NEW_LINE if ( isPossible ( x , y , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|