id stringlengths 18 123 | pair stringclasses 6
values | Code1 stringlengths 41 1.98k | Code2 stringlengths 41 1.98k | instruction stringclasses 6
values | input stringlengths 56 2k |
|---|---|---|---|---|---|
cpp-python-CHECK_WHETHER_GIVEN_CIRCLE_RESIDE_BOUNDARY_MAINTAINED_OUTER_CIRCLE_INNER_CIRCLE | cpp-python | void fitOrNotFit ( int R , int r , int x , int y , int rad ) {
double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ;
if ( val + rad <= R && val - rad >= R - r ) cout << "Fits\n" ;
else cout << "Doesn't Fit\n" ;
}
| def fitOrNotFit ( R , r , x , y , rad ) :
val = math.sqrt ( math.pow ( x , 2 ) + math.pow ( y , 2 ) )
if ( val + rad <= R and val - rad >= R - r ) :
print ( "Fits\n" )
else :
print ( "Doesn't Fit" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void fitOrNotFit ( int R , int r , int x , int y , int rad ) {
double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ;
if ( val + rad <= R && val - rad >= R - r ) cout << "Fits\n" ;
else cout << "Doesn't Fit\n" ;
}
</Code> |
cpp-python-NTH_MULTIPLE_NUMBER_FIBONACCI_SERIES | cpp-python | int findPosition ( int k , int n ) {
unsigned long long int f1 = 0 , f2 = 1 , f3 ;
for ( int i = 2 ;
i <= MAX ;
i ++ ) {
f3 = f1 + f2 ;
f1 = f2 ;
f2 = f3 ;
if ( f2 % k == 0 ) return n * i ;
}
}
| def findPosition ( k , n ) :
f1 = 0
f2 = 1
i = 2
while i != 0 :
f3 = f1 + f2
f1 = f2
f2 = f3
if f2 % k == 0 :
return n * i
i += 1
return
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findPosition ( int k , int n ) {
unsigned long long int f1 = 0 , f2 = 1 , f3 ;
for ( int i = 2 ;
i <= MAX ;
i ++ ) {
f3 = f1 + f2 ;
f1 = f2 ;
f2 = f3 ;
if ( f2 % k == 0 ) return n * i ;
}
}
</Code> |
cpp-python-PRINT_N_TERMS_NEWMAN_CONWAY_SEQUENCE | cpp-python | void sequence ( int n ) {
int f [ n + 1 ] ;
f [ 0 ] = 0 ;
f [ 1 ] = 1 ;
f [ 2 ] = 1 ;
cout << f [ 1 ] << " " << f [ 2 ] << " " ;
for ( int i = 3 ;
i <= n ;
i ++ ) {
f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ;
cout << f [ i ] << " " ;
}
}
| def sequence ( n ) :
f = [ 0 , 1 , 1 ]
print ( f [ 1 ] , end = " " ) ,
print ( f [ 2 ] , end = " " ) ,
for i in range ( 3 , n + 1 ) :
f.append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] )
print ( f [ i ] , end = " " ) ,
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void sequence ( int n ) {
int f [ n + 1 ] ;
f [ 0 ] = 0 ;
f [ 1 ] = 1 ;
f [ 2 ] = 1 ;
cout << f [ 1 ] << " " << f [ 2 ] << " " ;
for ( int i = 3 ;
i <= n ;
i ++ ) {
f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ;
cout << f [ i ] << " " ;
}
}
</Code> |
cpp-python-MINIMUM_NUMBER_OF_MANIPULATIONS_REQUIRED_TO_MAKE_TWO_STRINGS_ANAGRAM_WITHOUT_DELETION_OF_CHARACTER | cpp-python | int countManipulations ( string s1 , string s2 ) {
int count = 0 ;
int char_count [ 26 ] ;
for ( int i = 0 ;
i < 26 ;
i ++ ) {
char_count [ i ] = 0 ;
}
for ( int i = 0 ;
i < s1 . length ( ) ;
i ++ ) char_count [ s1 [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < s2 . length ( ) ;
i ++ ) {
char_... | def countManipulations ( s1 , s2 ) :
count = 0
char_count = [ 0 ] * 26
for i in range ( 26 ) :
char_count [ i ] = 0
for i in range ( len ( s1 ) ) :
char_count [ ord ( s1 [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( len ( s2 ) ) :
char_count [ ord ( s2 [ i ] ) - ord ( 'a' ) ]... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countManipulations ( string s1 , string s2 ) {
int count = 0 ;
int char_count [ 26 ] ;
for ( int i = 0 ;
i < 26 ;
i ++ ) {
char_count [ i ] = 0 ;
}
for ( int i = 0 ;
i < s1 . length ( ) ;
i ++ ) char_count [ s1 [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < s2 . length ( ) ;
i ++ ) {
... |
cpp-python-COUNT_NUMBER_WAYS_REACH_GIVEN_SCORE_GAME | cpp-python | int count ( int n ) {
int table [ n + 1 ] , i ;
for ( int j = 0 ;
j < n + 1 ;
j ++ ) table [ j ] = 0 ;
table [ 0 ] = 1 ;
for ( i = 3 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 3 ] ;
for ( i = 5 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 5 ] ;
for ( i = 10 ;
i <= n ;
i ++ ) table [ i ] +=... | def count ( n ) :
table = [ 0 for i in range ( n + 1 ) ]
table [ 0 ] = 1
for i in range ( 3 , n + 1 ) :
table [ i ] += table [ i - 3 ]
for i in range ( 5 , n + 1 ) :
table [ i ] += table [ i - 5 ]
for i in range ( 10 , n + 1 ) :
table [ i ] += table [ i - 10 ]
return tabl... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int count ( int n ) {
int table [ n + 1 ] , i ;
for ( int j = 0 ;
j < n + 1 ;
j ++ ) table [ j ] = 0 ;
table [ 0 ] = 1 ;
for ( i = 3 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 3 ] ;
for ( i = 5 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 5 ] ;
for ( i = 10 ;
i <= n ;
i ++ ) table [... |
cpp-python-FIND_LENGTH_LONGEST_SUBSEQUENCE_ONE_STRING_SUBSTRING_ANOTHER_STRING | cpp-python | int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) {
int dp [ MAX ] [ MAX ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( x [ j - 1 ] == y [ ... | def maxSubsequenceSubstring ( x , y , n , m ) :
dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ]
for i in range ( 1 , m + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( x [ j - 1 ] == y [ i - 1 ] ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
else :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) {
int dp [ MAX ] [ MAX ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( x [ j - 1 ] ... |
cpp-python-LONGEST_REPEATING_SUBSEQUENCE | cpp-python | int findLongestRepeatingSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( str [ i - 1 ... | def findLongestRepeatingSubSeq ( str ) :
n = len ( str )
dp = [ [ 0 ] * ( n + 1 ) ] * ( n + 1 )
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
else :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findLongestRepeatingSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( str [... |
cpp-python-PROGRAM_FIND_STRING_START_END_GEEKS | cpp-python | bool isCornerPresent ( string str , string corner ) {
int n = str . length ( ) ;
int cl = corner . length ( ) ;
if ( n < cl ) return false ;
return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ;
}
| def isCornerPresent ( str , corner ) :
n = len ( str )
cl = len ( corner )
if ( n < cl ) :
return False
return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isCornerPresent ( string str , string corner ) {
int n = str . length ( ) ;
int cl = corner . length ( ) ;
if ( n < cl ) return false ;
return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ;
}
</Code> |
cpp-python-MEDIAN_OF_TWO_SORTED_ARRAYS | cpp-python | int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) {
int i = 0 ;
int j = 0 ;
int count ;
int m1 = - 1 , m2 = - 1 ;
for ( count = 0 ;
count <= n ;
count ++ ) {
if ( i == n ) {
m1 = m2 ;
m2 = ar2 [ 0 ] ;
break ;
}
else if ( j == n ) {
m1 = m2 ;
m2 = ar1 [ 0 ] ;
... | def getMedian ( ar1 , ar2 , n ) :
i = 0
j = 0
m1 = - 1
m2 = - 1
count = 0
while count < n + 1 :
count += 1
if i == n :
m1 = m2
m2 = ar2 [ 0 ]
break
elif j == n :
m1 = m2
m2 = ar1 [ 0 ]
break
i... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) {
int i = 0 ;
int j = 0 ;
int count ;
int m1 = - 1 , m2 = - 1 ;
for ( count = 0 ;
count <= n ;
count ++ ) {
if ( i == n ) {
m1 = m2 ;
m2 = ar2 [ 0 ] ;
break ;
}
else if ( j == n ) {
m1 = m2 ;
m2 = ar1 [ 0 ... |
cpp-python-COUNT_ZEROS_IN_A_ROW_WISE_AND_COLUMN_WISE_SORTED_MATRIX | cpp-python | int countZeroes ( int mat [ N ] [ N ] ) {
int row = N - 1 , col = 0 ;
int count = 0 ;
while ( col < N ) {
while ( mat [ row ] [ col ] ) if ( -- row < 0 ) return count ;
count += ( row + 1 ) ;
col ++ ;
}
return count ;
}
| def countZeroes ( mat ) :
N = 5 ;
row = N - 1 ;
col = 0 ;
count = 0 ;
while ( col < N ) :
while ( mat [ row ] [ col ] ) :
if ( row < 0 ) :
return count ;
row = row - 1 ;
count = count + ( row + 1 ) ;
col = col + 1 ;
return count ;
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countZeroes ( int mat [ N ] [ N ] ) {
int row = N - 1 , col = 0 ;
int count = 0 ;
while ( col < N ) {
while ( mat [ row ] [ col ] ) if ( -- row < 0 ) return count ;
count += ( row + 1 ) ;
col ++ ;
}
return count ;
}
</Code> |
cpp-python-JUMP_SEARCH | cpp-python | int jumpSearch ( int arr [ ] , int x , int n ) {
int step = sqrt ( n ) ;
int prev = 0 ;
while ( arr [ min ( step , n ) - 1 ] < x ) {
prev = step ;
step += sqrt ( n ) ;
if ( prev >= n ) return - 1 ;
}
while ( arr [ prev ] < x ) {
prev ++ ;
if ( prev == min ( step , n ) ) return - 1 ;
}
... | def jumpSearch ( arr , x , n ) :
step = math.sqrt ( n )
prev = 0
while arr [ int ( min ( step , n ) - 1 ) ] < x :
prev = step
step += math.sqrt ( n )
if prev >= n :
return - 1
while arr [ int ( prev ) ] < x :
prev += 1
if prev == min ( step , n ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int jumpSearch ( int arr [ ] , int x , int n ) {
int step = sqrt ( n ) ;
int prev = 0 ;
while ( arr [ min ( step , n ) - 1 ] < x ) {
prev = step ;
step += sqrt ( n ) ;
if ( prev >= n ) return - 1 ;
}
while ( arr [ prev ] < x ) {
prev ++ ;
if ( prev == min ( step , n ) ) return - 1 ;... |
cpp-python-FIND_X_Y_SATISFYING_AX_N | cpp-python | void solution ( int a , int b , int n ) {
for ( int i = 0 ;
i * a <= n ;
i ++ ) {
if ( ( n - ( i * a ) ) % b == 0 ) {
cout << "x = " << i << ", y = " << ( n - ( i * a ) ) / b ;
return ;
}
}
cout << "No solution" ;
}
| def solution ( a , b , n ) :
i = 0
while i * a <= n :
if ( n - ( i * a ) ) % b == 0 :
print ( "x = " , i , ", y = " , int ( ( n - ( i * a ) ) / b ) )
return 0
i = i + 1
print ( "No solution" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void solution ( int a , int b , int n ) {
for ( int i = 0 ;
i * a <= n ;
i ++ ) {
if ( ( n - ( i * a ) ) % b == 0 ) {
cout << "x = " << i << ", y = " << ( n - ( i * a ) ) / b ;
return ;
}
}
cout << "No solution" ;
}
</Code> |
cpp-python-DIVIDE_LARGE_NUMBER_REPRESENTED_STRING | cpp-python | string longDivision ( string number , int divisor ) {
string ans ;
int idx = 0 ;
int temp = number [ idx ] - '0' ;
while ( temp < divisor ) temp = temp * 10 + ( number [ ++ idx ] - '0' ) ;
while ( number . size ( ) > idx ) {
ans += ( temp / divisor ) + '0' ;
temp = ( temp % divisor ) * 10 + number [ +... | def longDivision ( number , divisor ) :
ans = ""
idx = 0
temp = ord ( number [ idx ] ) - ord ( '0' )
while ( temp < divisor ) :
temp = ( temp * 10 + ord ( number [ idx + 1 ] ) - ord ( '0' ) )
idx += 1
idx += 1
while ( ( len ( number ) ) > idx ) :
ans += chr ( math.floor (... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
string longDivision ( string number , int divisor ) {
string ans ;
int idx = 0 ;
int temp = number [ idx ] - '0' ;
while ( temp < divisor ) temp = temp * 10 + ( number [ ++ idx ] - '0' ) ;
while ( number . size ( ) > idx ) {
ans += ( temp / divisor ) + '0' ;
temp = ( temp % divisor ) * 10 + num... |
cpp-python-SORT_AN_ARRAY_OF_0S_1S_AND_2S | cpp-python | void sort012 ( int a [ ] , int arr_size ) {
int lo = 0 ;
int hi = arr_size - 1 ;
int mid = 0 ;
while ( mid <= hi ) {
switch ( a [ mid ] ) {
case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ;
break ;
case 1 : mid ++ ;
break ;
case 2 : swap ( a [ mid ] , a [ hi -- ] ) ;
break ;... | def sort012 ( a , arr_size ) :
lo = 0
hi = arr_size - 1
mid = 0
while mid <= hi :
if a [ mid ] == 0 :
a [ lo ] , a [ mid ] = a [ mid ] , a [ lo ]
lo = lo + 1
mid = mid + 1
elif a [ mid ] == 1 :
mid = mid + 1
else :
a [ m... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void sort012 ( int a [ ] , int arr_size ) {
int lo = 0 ;
int hi = arr_size - 1 ;
int mid = 0 ;
while ( mid <= hi ) {
switch ( a [ mid ] ) {
case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ;
break ;
case 1 : mid ++ ;
break ;
case 2 : swap ( a [ mid ] , a [ hi -- ] ) ;
... |
cpp-python-CHECK_IF_A_GIVEN_ARRAY_CAN_REPRESENT_PREORDER_TRAVERSAL_OF_BINARY_SEARCH_TREE | cpp-python | bool canRepresentBST ( int pre [ ] , int n ) {
stack < int > s ;
int root = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( pre [ i ] < root ) return false ;
while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) {
root = s . top ( ) ;
s . pop ( ) ;
}
s . push ( pre [ i ] ) ;
}
... | def canRepresentBST ( pre ) :
s = [ ]
root = INT_MIN
for value in pre :
if value < root :
return False
while ( len ( s ) > 0 and s [ - 1 ] < value ) :
root = s.pop ( )
s.append ( value )
return True
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool canRepresentBST ( int pre [ ] , int n ) {
stack < int > s ;
int root = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( pre [ i ] < root ) return false ;
while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) {
root = s . top ( ) ;
s . pop ( ) ;
}
s . push ( pre [ i ] ) ... |
cpp-python-FIND_K_SUCH_THAT_ALL_ELEMENTS_IN_KTH_ROW_ARE_0_AND_KTH_COLUMN_ARE_1_IN_A_BOOLEAN_MATRIX | cpp-python | int find ( bool arr [ n ] [ n ] ) {
int i = 0 , j = n - 1 ;
int res = - 1 ;
while ( i < n && j >= 0 ) {
if ( arr [ i ] [ j ] == 0 ) {
while ( j >= 0 && ( arr [ i ] [ j ] == 0 || i == j ) ) j -- ;
if ( j == - 1 ) {
res = i ;
break ;
}
else i ++ ;
}
else {
w... | def find ( arr ) :
n = len ( arr )
i = 0
j = n - 1
res = - 1
while i < n and j >= 0 :
if arr [ i ] [ j ] == 0 :
while j >= 0 and ( arr [ i ] [ j ] == 0 or i == j ) :
j -= 1
if j == - 1 :
res = i
break
else : ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int find ( bool arr [ n ] [ n ] ) {
int i = 0 , j = n - 1 ;
int res = - 1 ;
while ( i < n && j >= 0 ) {
if ( arr [ i ] [ j ] == 0 ) {
while ( j >= 0 && ( arr [ i ] [ j ] == 0 || i == j ) ) j -- ;
if ( j == - 1 ) {
res = i ;
break ;
}
else i ++ ;
}
else {
... |
cpp-python-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS_1 | cpp-python | void printArray ( int * a , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) cout << a [ i ] << " " ;
cout << endl ;
return ;
}
| def printArray ( a , n ) :
for i in a :
print ( i , end = " " )
print ( )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printArray ( int * a , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) cout << a [ i ] << " " ;
cout << endl ;
return ;
}
</Code> |
cpp-python-SORT_ARRAY_CONVERTING_ELEMENTS_SQUARES_1 | cpp-python | void sortSquares ( int arr [ ] , int n ) {
int K = 0 ;
for ( K = 0 ;
K < n ;
K ++ ) if ( arr [ K ] >= 0 ) break ;
int i = K - 1 ;
int j = K ;
int ind = 0 ;
int temp [ n ] ;
while ( i >= 0 && j < n ) {
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) {
temp [ ind ] = arr [ i ] * arr [ i ]... | def sortSquares ( arr , n ) :
K = 0
for K in range ( n ) :
if ( arr [ K ] >= 0 ) :
break
i = K - 1
j = K
ind = 0
temp = [ 0 ] * n
while ( i >= 0 and j < n ) :
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) :
temp [ ind ] = arr [ i ] * arr [ i ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void sortSquares ( int arr [ ] , int n ) {
int K = 0 ;
for ( K = 0 ;
K < n ;
K ++ ) if ( arr [ K ] >= 0 ) break ;
int i = K - 1 ;
int j = K ;
int ind = 0 ;
int temp [ n ] ;
while ( i >= 0 && j < n ) {
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) {
temp [ ind ] = arr [ i ] * ar... |
cpp-python-PRINT_MAXIMUM_SHORTEST_DISTANCE | cpp-python | int find_maximum ( int a [ ] , int n , int k ) {
unordered_map < int , int > b ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
int d = min ( 1 + i , n - i ) ;
if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ;
else b [ x ] = min ( d , b [ x ] ) ;
}
int ans = INT_MAX ;
for ( int i = ... | def find_maximum ( a , n , k ) :
b = dict ( )
for i in range ( n ) :
x = a [ i ]
d = min ( 1 + i , n - i )
if x not in b.keys ( ) :
b [ x ] = d
else :
b [ x ] = min ( d , b [ x ] )
ans = 10 ** 9
for i in range ( n ) :
x = a [ i ]
if... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int find_maximum ( int a [ ] , int n , int k ) {
unordered_map < int , int > b ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
int d = min ( 1 + i , n - i ) ;
if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ;
else b [ x ] = min ( d , b [ x ] ) ;
}
int ans = INT_MAX ;
for ( i... |
cpp-python-MAXIMUM_CONSECUTIVE_REPEATING_CHARACTER_STRING_1 | cpp-python | char maxRepeating ( string str ) {
int n = str . length ( ) ;
int count = 0 ;
char res = str [ 0 ] ;
int cur_count = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ;
else {
if ( cur_count > count ) {
count = cur_count ;
res ... | def maxRepeating ( str ) :
n = len ( str )
count = 0
res = str [ 0 ]
cur_count = 1
for i in range ( n ) :
if ( i < n - 1 and str [ i ] == str [ i + 1 ] ) :
cur_count += 1
else :
if cur_count > count :
count = cur_count
res = str... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
char maxRepeating ( string str ) {
int n = str . length ( ) ;
int count = 0 ;
char res = str [ 0 ] ;
int cur_count = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ;
else {
if ( cur_count > count ) {
count = cur_count ;
... |
cpp-python-ARRAY_RANGE_QUERIES_ELEMENTS_FREQUENCY_VALUE | cpp-python | int solveQuery ( int start , int end , int arr [ ] ) {
unordered_map < int , int > frequency ;
for ( int i = start ;
i <= end ;
i ++ ) frequency [ arr [ i ] ] ++ ;
int count = 0 ;
for ( auto x : frequency ) if ( x . first == x . second ) count ++ ;
return count ;
}
| def solveQuery ( start , end , arr ) :
frequency = dict ( )
for i in range ( start , end + 1 ) :
if arr [ i ] in frequency.keys ( ) :
frequency [ arr [ i ] ] += 1
else :
frequency [ arr [ i ] ] = 1
count = 0
for x in frequency :
if x == frequency [ x ] :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int solveQuery ( int start , int end , int arr [ ] ) {
unordered_map < int , int > frequency ;
for ( int i = start ;
i <= end ;
i ++ ) frequency [ arr [ i ] ] ++ ;
int count = 0 ;
for ( auto x : frequency ) if ( x . first == x . second ) count ++ ;
return count ;
}
</Code> |
cpp-python-PYTHON_PROGRAM_FIND_PERIMETER_CIRCUMFERENCE_SQUARE_RECTANGLE | cpp-python | int Circumference ( int a ) {
return 4 * a ;
}
| def Circumference ( a ) :
return ( 4 * a )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int Circumference ( int a ) {
return 4 * a ;
}
</Code> |
cpp-python-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_3 | cpp-python | int numberOfPaths ( int m , int n ) {
int path = 1 ;
for ( int i = n ;
i < ( m + n - 1 ) ;
i ++ ) {
path *= i ;
path /= ( i - n + 1 ) ;
}
return path ;
}
| def numberOfPaths ( m , n ) :
for i in range ( n , ( m + n - 1 ) ) :
path *= i
path //= ( i - n + 1 )
return path
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int numberOfPaths ( int m , int n ) {
int path = 1 ;
for ( int i = n ;
i < ( m + n - 1 ) ;
i ++ ) {
path *= i ;
path /= ( i - n + 1 ) ;
}
return path ;
}
</Code> |
cpp-python-POINT_CLIPPING_ALGORITHM_COMPUTER_GRAPHICS | cpp-python | void pointClip ( int XY [ ] [ 2 ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) {
cout << "Point inside the viewing pane:" << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) {
if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i ] [ 1 ] <= Ymax ... | def pointClip ( XY , n , Xmin , Ymin , Xmax , Ymax ) :
print ( "Point inside the viewing pane:" )
for i in range ( n ) :
if ( ( XY [ i ] [ 0 ] >= Xmin ) and ( XY [ i ] [ 0 ] <= Xmax ) ) :
if ( ( XY [ i ] [ 1 ] >= Ymin ) and ( XY [ i ] [ 1 ] <= Ymax ) ) :
print ( "[" , XY [ i ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void pointClip ( int XY [ ] [ 2 ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) {
cout << "Point inside the viewing pane:" << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) {
if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i ] [ 1 ] <... |
cpp-python-MAXIMUM_SUM_INCREASING_SUBSEQUENCE_FROM_A_PREFIX_AND_A_GIVEN_ELEMENT_AFTER_PREFIX_IS_MUST | cpp-python | ll pre_compute ( ll a [ ] , ll n , ll index , ll k ) {
ll dp [ n ] [ n ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ;
else dp [ 0 ] [ i ] = a [ i ] ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0... | def pre_compute ( a , n , index , k ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
for i in range ( n ) :
if a [ i ] > a [ 0 ] :
dp [ 0 ] [ i ] = a [ i ] + a [ 0 ]
else :
dp [ 0 ] [ i ] = a [ i ]
for i in range ( 1 , n ) :
for j in range ( n ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
ll pre_compute ( ll a [ ] , ll n , ll index , ll k ) {
ll dp [ n ] [ n ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ;
else dp [ 0 ] [ i ] = a [ i ] ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( in... |
cpp-python-SPLIT_ARRAY_ADD_FIRST_PART_END | cpp-python | void splitArr ( int arr [ ] , int n , int k ) {
for ( int i = 0 ;
i < k ;
i ++ ) {
int x = arr [ 0 ] ;
for ( int j = 0 ;
j < n - 1 ;
++ j ) arr [ j ] = arr [ j + 1 ] ;
arr [ n - 1 ] = x ;
}
}
| def splitArr ( arr , n , k ) :
for i in range ( 0 , k ) :
x = arr [ 0 ]
for j in range ( 0 , n - 1 ) :
arr [ j ] = arr [ j + 1 ]
arr [ n - 1 ] = x
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void splitArr ( int arr [ ] , int n , int k ) {
for ( int i = 0 ;
i < k ;
i ++ ) {
int x = arr [ 0 ] ;
for ( int j = 0 ;
j < n - 1 ;
++ j ) arr [ j ] = arr [ j + 1 ] ;
arr [ n - 1 ] = x ;
}
}
</Code> |
cpp-python-MINIMUM_LENGTH_SUBARRAY_SUM_GREATER_GIVEN_VALUE_1 | cpp-python | int smallestSubWithSum ( int arr [ ] , int n , int x ) {
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while ( end < n ) {
while ( curr_sum <= x && end < n ) {
if ( curr_sum <= 0 && x > 0 ) {
start = end ;
curr_sum = 0 ;
}
curr_sum += arr [ end ++ ] ;
}
... | def smallestSubWithSum ( arr , n , x ) :
curr_sum = 0
min_len = n + 1
start = 0
end = 0
while ( end < n ) :
while ( curr_sum <= x and end < n ) :
if ( curr_sum <= 0 and x > 0 ) :
start = end
curr_sum = 0
curr_sum += arr [ end ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int smallestSubWithSum ( int arr [ ] , int n , int x ) {
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while ( end < n ) {
while ( curr_sum <= x && end < n ) {
if ( curr_sum <= 0 && x > 0 ) {
start = end ;
curr_sum = 0 ;
}
curr_sum += arr [ end ++ ] ;
... |
cpp-python-ARRAY_ELEMENT_MOVED_K_USING_SINGLE_MOVES | cpp-python | int winner ( int a [ ] , int n , int k ) {
if ( k >= n - 1 ) return n ;
int best = 0 , times = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > best ) {
best = a [ i ] ;
if ( i ) times = 1 ;
}
else times += 1 ;
if ( times >= k ) return best ;
}
return best ;
}
| def winner ( a , n , k ) :
if k >= n - 1 :
return n
best = 0
times = 0
for i in range ( n ) :
if a [ i ] > best :
best = a [ i ]
if i == True :
times = 1
else :
times += 1
if times >= k :
return best
retu... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int winner ( int a [ ] , int n , int k ) {
if ( k >= n - 1 ) return n ;
int best = 0 , times = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > best ) {
best = a [ i ] ;
if ( i ) times = 1 ;
}
else times += 1 ;
if ( times >= k ) return best ;
}
return best ;
}
</Co... |
cpp-python-CHECK_LARGE_NUMBER_DIVISIBLE_20 | cpp-python | bool divisibleBy20 ( string num ) {
int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ;
return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ;
}
| def divisibleBy20 ( num ) :
lastTwoDigits = int ( num [ - 2 : ] )
return ( ( lastTwoDigits % 5 == 0 and lastTwoDigits % 4 == 0 ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool divisibleBy20 ( string num ) {
int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ;
return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ;
}
</Code> |
cpp-python-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2_1 | cpp-python | int countSolutions ( int n ) {
int x = 0 , yCount , res = 0 ;
for ( yCount = 0 ;
yCount * yCount < n ;
yCount ++ ) ;
while ( yCount != 0 ) {
res += yCount ;
x ++ ;
while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ;
}
return res ;
}
| def countSolutions ( n ) :
x = 0
res = 0
yCount = 0
while ( yCount * yCount < n ) :
yCount = yCount + 1
while ( yCount != 0 ) :
res = res + yCount
x = x + 1
while ( yCount != 0 and ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) :
yCount = yCount - 1
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countSolutions ( int n ) {
int x = 0 , yCount , res = 0 ;
for ( yCount = 0 ;
yCount * yCount < n ;
yCount ++ ) ;
while ( yCount != 0 ) {
res += yCount ;
x ++ ;
while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ;
}
return res ;
}
</Code> |
cpp-python-FIND_NUMBER_OF_TRIANGLES_POSSIBLE_1 | cpp-python | void CountTriangles ( vector < int > A ) {
int n = A . size ( ) ;
sort ( A . begin ( ) , A . end ( ) ) ;
int count = 0 ;
for ( int i = n - 1 ;
i >= 1 ;
i -- ) {
int l = 0 , r = i - 1 ;
while ( l < r ) {
if ( A [ l ] + A [ r ] > A [ i ] ) {
count += r - l ;
r -- ;
}
... | def CountTriangles ( A ) :
n = len ( A )
A.sort ( )
count = 0
for i in range ( n - 1 , 0 , - 1 ) :
l = 0
r = i - 1
while ( l < r ) :
if ( A [ l ] + A [ r ] > A [ i ] ) :
count += r - l
r -= 1
else :
l += 1
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void CountTriangles ( vector < int > A ) {
int n = A . size ( ) ;
sort ( A . begin ( ) , A . end ( ) ) ;
int count = 0 ;
for ( int i = n - 1 ;
i >= 1 ;
i -- ) {
int l = 0 , r = i - 1 ;
while ( l < r ) {
if ( A [ l ] + A [ r ] > A [ i ] ) {
count += r - l ;
r -- ;
}... |
cpp-python-NEXT_HIGHER_NUMBER_WITH_SAME_NUMBER_OF_SET_BITS | cpp-python | uint_t snoob ( uint_t x ) {
uint_t rightOne ;
uint_t nextHigherOneBit ;
uint_t rightOnesPattern ;
uint_t next = 0 ;
if ( x ) {
rightOne = x & - ( signed ) x ;
nextHigherOneBit = x + rightOne ;
rightOnesPattern = x ^ nextHigherOneBit ;
rightOnesPattern = ( rightOnesPattern ) / rightOne ;
ri... | def snoob ( x ) :
next = 0
if ( x ) :
rightOne = x & - ( x )
nextHigherOneBit = x + int ( rightOne )
rightOnesPattern = x ^ int ( nextHigherOneBit )
rightOnesPattern = ( int ( rightOnesPattern ) / int ( rightOne ) )
rightOnesPattern = int ( rightOnesPattern ) >> 2
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
uint_t snoob ( uint_t x ) {
uint_t rightOne ;
uint_t nextHigherOneBit ;
uint_t rightOnesPattern ;
uint_t next = 0 ;
if ( x ) {
rightOne = x & - ( signed ) x ;
nextHigherOneBit = x + rightOne ;
rightOnesPattern = x ^ nextHigherOneBit ;
rightOnesPattern = ( rightOnesPattern ) / rightOne ;... |
cpp-python-CHECK_ARRAY_REPRESENTS_INORDER_BINARY_SEARCH_TREE_NOT | cpp-python | bool isInorder ( int arr [ ] , int n ) {
if ( n == 0 || n == 1 ) return true ;
for ( int i = 1 ;
i < n ;
i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ;
return true ;
}
| def isInorder ( arr , n ) :
if ( n == 0 or n == 1 ) :
return True
for i in range ( 1 , n , 1 ) :
if ( arr [ i - 1 ] > arr [ i ] ) :
return False
return True
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isInorder ( int arr [ ] , int n ) {
if ( n == 0 || n == 1 ) return true ;
for ( int i = 1 ;
i < n ;
i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ;
return true ;
}
</Code> |
cpp-python-LONGEST_INCREASING_SUBSEQUENCE | cpp-python | int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
| def lis ( arr ) :
global maximum
n = len ( arr )
maximum = 1
_lis ( arr , n )
return maximum
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
</Code> |
cpp-python-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY | cpp-python | int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) return - 1 ;
int max_product = INT_MIN ;
for ( int i = 0 ;
i < n - 2 ;
i ++ ) for ( int j = i + 1 ;
j < n - 1 ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ;
return max_product... | def maxProduct ( arr , n ) :
if n < 3 :
return - 1
max_product = - ( sys.maxsize - 1 )
for i in range ( 0 , n - 2 ) :
for j in range ( i + 1 , n - 1 ) :
for k in range ( j + 1 , n ) :
max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] )
return... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) return - 1 ;
int max_product = INT_MIN ;
for ( int i = 0 ;
i < n - 2 ;
i ++ ) for ( int j = i + 1 ;
j < n - 1 ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ;
return max_... |
cpp-python-FREQUENCY_ELEMENT_UNSORTED_ARRAY | cpp-python | void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
set < pair < int , int >> st ;
for ( auto x : hm ) {
st . insert ( {
x . first , x . second }
) ;
}
int cumul = 0 ;
for ( auto x : st ) {
cumul += x ... | def countFreq ( a , n ) :
hm = { }
for i in range ( 0 , n ) :
hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1
st = set ( )
for x in hm :
st.add ( ( x , hm [ x ] ) )
cumul = 0
for x in sorted ( st ) :
cumul += x [ 1 ]
print ( x [ 0 ] , cumul )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
set < pair < int , int >> st ;
for ( auto x : hm ) {
st . insert ( {
x . first , x . second }
) ;
}
int cumul = 0 ;
for ( auto x : st ) {
cumu... |
cpp-python-COMPUTE_NCR_P_SET_1_INTRODUCTION_AND_DYNAMIC_PROGRAMMING_SOLUTION | cpp-python | int nCrModp ( int n , int r , int p ) {
int C [ r + 1 ] ;
memset ( C , 0 , sizeof ( C ) ) ;
C [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = min ( i , r ) ;
j > 0 ;
j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ;
}
return C [ r ] ;
}
| def nCrModp ( n , r , p ) :
C = [ 0 for i in range ( r + 1 ) ]
C [ 0 ] = 1
for i in range ( 1 , n + 1 ) :
for j in range ( min ( i , r ) , 0 , - 1 ) :
C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p
return C [ r ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int nCrModp ( int n , int r , int p ) {
int C [ r + 1 ] ;
memset ( C , 0 , sizeof ( C ) ) ;
C [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = min ( i , r ) ;
j > 0 ;
j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ;
}
return C [ r ] ;
}
</Code> |
cpp-python-COCKTAIL_SORT | cpp-python | void CocktailSort ( int a [ ] , int n ) {
bool swapped = true ;
int start = 0 ;
int end = n - 1 ;
while ( swapped ) {
swapped = false ;
for ( int i = start ;
i < end ;
++ i ) {
if ( a [ i ] > a [ i + 1 ] ) {
swap ( a [ i ] , a [ i + 1 ] ) ;
swapped = true ;
}
}
... | def cocktailSort ( a ) :
n = len ( a )
swapped = True
start = 0
end = n - 1
while ( swapped == True ) :
swapped = False
for i in range ( start , end ) :
if ( a [ i ] > a [ i + 1 ] ) :
a [ i ] , a [ i + 1 ] = a [ i + 1 ] , a [ i ]
swapped = ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void CocktailSort ( int a [ ] , int n ) {
bool swapped = true ;
int start = 0 ;
int end = n - 1 ;
while ( swapped ) {
swapped = false ;
for ( int i = start ;
i < end ;
++ i ) {
if ( a [ i ] > a [ i + 1 ] ) {
swap ( a [ i ] , a [ i + 1 ] ) ;
swapped = true ;
}
... |
cpp-python-WORD_WRAP_PROBLEM_SPACE_OPTIMIZED_SOLUTION | cpp-python | void solveWordWrap ( int arr [ ] , int n , int k ) {
int i , j ;
int currlen ;
int cost ;
int dp [ n ] ;
int ans [ n ] ;
dp [ n - 1 ] = 0 ;
ans [ n - 1 ] = n - 1 ;
for ( i = n - 2 ;
i >= 0 ;
i -- ) {
currlen = - 1 ;
dp [ i ] = INT_MAX ;
for ( j = i ;
j < n ;
j ++ ) {
currle... | def solveWordWrap ( arr , n , k ) :
dp = [ 0 ] * n
ans = [ 0 ] * n
dp [ n - 1 ] = 0
ans [ n - 1 ] = n - 1
for i in range ( n - 2 , - 1 , - 1 ) :
currlen = - 1
dp [ i ] = sys.maxsize
for j in range ( i , n ) :
currlen += ( arr [ j ] + 1 )
if ( currlen >... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void solveWordWrap ( int arr [ ] , int n , int k ) {
int i , j ;
int currlen ;
int cost ;
int dp [ n ] ;
int ans [ n ] ;
dp [ n - 1 ] = 0 ;
ans [ n - 1 ] = n - 1 ;
for ( i = n - 2 ;
i >= 0 ;
i -- ) {
currlen = - 1 ;
dp [ i ] = INT_MAX ;
for ( j = i ;
j < n ;
j ++ ) {
... |
cpp-python-BINARY_SEARCH_1 | cpp-python | int binarySearch ( int arr [ ] , int l , int r , int x ) {
while ( l <= r ) {
int m = l + ( r - l ) / 2 ;
if ( arr [ m ] == x ) return m ;
if ( arr [ m ] < x ) l = m + 1 ;
else r = m - 1 ;
}
return - 1 ;
}
| def binarySearch ( arr , l , r , x ) :
while l <= r :
mid = l + ( r - l ) // 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] < x :
l = mid + 1
else :
r = mid - 1
return - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int binarySearch ( int arr [ ] , int l , int r , int x ) {
while ( l <= r ) {
int m = l + ( r - l ) / 2 ;
if ( arr [ m ] == x ) return m ;
if ( arr [ m ] < x ) l = m + 1 ;
else r = m - 1 ;
}
return - 1 ;
}
</Code> |
cpp-python-MINIMUM_XOR_VALUE_PAIR | cpp-python | int minXOR ( int arr [ ] , int n ) {
int min_xor = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) min_xor = min ( min_xor , arr [ i ] ^ arr [ j ] ) ;
return min_xor ;
}
| def minXOR ( arr , n ) :
arr.sort ( )
min_xor = 999999
val = 0
for i in range ( 0 , n - 1 ) :
for j in range ( i + 1 , n - 1 ) :
val = arr [ i ] ^ arr [ j ]
min_xor = min ( min_xor , val )
return min_xor
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minXOR ( int arr [ ] , int n ) {
int min_xor = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) min_xor = min ( min_xor , arr [ i ] ^ arr [ j ] ) ;
return min_xor ;
}
</Code> |
cpp-python-FIND_SUM_EVEN_FACTORS_NUMBER | cpp-python | int sumofFactors ( int n ) {
if ( n % 2 != 0 ) return 0 ;
int res = 1 ;
for ( int i = 2 ;
i <= sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 , curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
if ( i == 2 && count == 1 ) curr_sum = 0 ;
curr_term *= i ;
cu... | def sumofFactors ( n ) :
if ( n % 2 != 0 ) :
return 0
res = 1
for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) :
count = 0
curr_sum = 1
curr_term = 1
while ( n % i == 0 ) :
count = count + 1
n = n // i
if ( i == 2 and count ==... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int sumofFactors ( int n ) {
if ( n % 2 != 0 ) return 0 ;
int res = 1 ;
for ( int i = 2 ;
i <= sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 , curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
if ( i == 2 && count == 1 ) curr_sum = 0 ;
curr_term *= i ;
... |
cpp-python-FIRST_UPPERCASE_LETTER_IN_A_STRING_ITERATIVE_AND_RECURSIVE_1 | cpp-python | char first ( string str , int i = 0 ) {
if ( str [ i ] == '\0' ) return 0 ;
if ( isupper ( str [ i ] ) ) return str [ i ] ;
return first ( str , i + 1 ) ;
}
| def first ( str , i ) :
if ( str [ i ] == '\0' ) :
return 0
if ( str [ i ].isupper ( ) ) :
return str [ i ]
return first ( str , i + 1 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
char first ( string str , int i = 0 ) {
if ( str [ i ] == '\0' ) return 0 ;
if ( isupper ( str [ i ] ) ) return str [ i ] ;
return first ( str , i + 1 ) ;
}
</Code> |
cpp-python-MAXIMUM_GAMES_PLAYED_WINNER | cpp-python | int maxGameByWinner ( int N ) {
int dp [ N ] ;
dp [ 0 ] = 1 ;
dp [ 1 ] = 2 ;
int i = 2 ;
do {
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
}
while ( dp [ i ++ ] <= N ) ;
return ( i - 2 ) ;
}
| def maxGameByWinner ( N ) :
dp = [ 0 for i in range ( N ) ]
dp [ 0 ] = 1
dp [ 1 ] = 2
i = 1
while dp [ i ] <= N :
i = i + 1
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ]
return ( i - 1 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxGameByWinner ( int N ) {
int dp [ N ] ;
dp [ 0 ] = 1 ;
dp [ 1 ] = 2 ;
int i = 2 ;
do {
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
}
while ( dp [ i ++ ] <= N ) ;
return ( i - 2 ) ;
}
</Code> |
cpp-python-EQUILIBRIUM_INDEX_OF_AN_ARRAY_1 | cpp-python | int equilibrium ( int arr [ ] , int n ) {
int sum = 0 ;
int leftsum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) sum += arr [ i ] ;
for ( int i = 0 ;
i < n ;
++ i ) {
sum -= arr [ i ] ;
if ( leftsum == sum ) return i ;
leftsum += arr [ i ] ;
}
return - 1 ;
}
| def equilibrium ( arr ) :
total_sum = sum ( arr )
leftsum = 0
for i , num in enumerate ( arr ) :
total_sum -= num
if leftsum == total_sum :
return i
leftsum += num
return - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int equilibrium ( int arr [ ] , int n ) {
int sum = 0 ;
int leftsum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) sum += arr [ i ] ;
for ( int i = 0 ;
i < n ;
++ i ) {
sum -= arr [ i ] ;
if ( leftsum == sum ) return i ;
leftsum += arr [ i ] ;
}
return - 1 ;
}
</Code> |
cpp-python-PROBABILITY_THREE_RANDOMLY_CHOSEN_NUMBERS_AP | cpp-python | double procal ( int n ) {
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ;
}
| def procal ( n ) :
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
double procal ( int n ) {
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ;
}
</Code> |
cpp-python-REARRANGE_ARRAY_MAXIMUM_MINIMUM_FORM | cpp-python | void rearrange ( int arr [ ] , int n ) {
int temp [ n ] ;
int small = 0 , large = n - 1 ;
int flag = true ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( flag ) temp [ i ] = arr [ large -- ] ;
else temp [ i ] = arr [ small ++ ] ;
flag = ! flag ;
}
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = t... | def rearrange ( arr , n ) :
temp = n * [ None ]
small , large = 0 , n - 1
flag = True
for i in range ( n ) :
if flag is True :
temp [ i ] = arr [ large ]
large -= 1
else :
temp [ i ] = arr [ small ]
small += 1
flag = bool ( 1 - flag... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void rearrange ( int arr [ ] , int n ) {
int temp [ n ] ;
int small = 0 , large = n - 1 ;
int flag = true ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( flag ) temp [ i ] = arr [ large -- ] ;
else temp [ i ] = arr [ small ++ ] ;
flag = ! flag ;
}
for ( int i = 0 ;
i < n ;
i ++ ) arr [ ... |
cpp-python-PRINT_DIGITS_POSITION_REMOVED_MAKE_NUMBER_DIVISIBLE_6 | cpp-python | void greatest ( string s ) {
int n = s . length ( ) ;
int a [ n ] ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ i ] = s [ i ] - '0' ;
sum += a [ i ] ;
}
if ( a [ n - 1 ] % 2 ) {
if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) {
cout << "-1" << endl ;
}
el... | def greatest ( s ) :
n = len ( s )
a = [ 0 for i in range ( n ) ]
Sum = 0
for i in range ( n ) :
a [ i ] = ord ( s [ i ] ) - ord ( '0' )
Sum += a [ i ]
if ( a [ n - 1 ] % 2 ) :
if ( a [ n - 2 ] % 2 != 0 or ( Sum - a [ n - 1 ] ) % 3 != 0 ) :
print ( "-1" )
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void greatest ( string s ) {
int n = s . length ( ) ;
int a [ n ] ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ i ] = s [ i ] - '0' ;
sum += a [ i ] ;
}
if ( a [ n - 1 ] % 2 ) {
if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) {
cout << "-1" << endl ;
}... |
cpp-python-CHECK_IF_X_CAN_GIVE_CHANGE_TO_EVERY_PERSON_IN_THE_QUEUE | cpp-python | int isChangeable ( int notes [ ] , int n ) {
int fiveCount = 0 ;
int tenCount = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( notes [ i ] == 5 ) fiveCount ++ ;
else if ( notes [ i ] == 10 ) {
if ( fiveCount > 0 ) {
fiveCount -- ;
tenCount ++ ;
}
else return 0 ;
}
... | def isChangeable ( notes , n ) :
fiveCount = 0
tenCount = 0
for i in range ( n ) :
if ( notes [ i ] == 5 ) :
fiveCount += 1
elif ( notes [ i ] == 10 ) :
if ( fiveCount > 0 ) :
fiveCount -= 1
tenCount += 1
else :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int isChangeable ( int notes [ ] , int n ) {
int fiveCount = 0 ;
int tenCount = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( notes [ i ] == 5 ) fiveCount ++ ;
else if ( notes [ i ] == 10 ) {
if ( fiveCount > 0 ) {
fiveCount -- ;
tenCount ++ ;
}
else return 0 ;
... |
cpp-python-LONGEST_COMMON_SUBSTRING | cpp-python | int LCSubStr ( char * X , char * Y , int m , int n ) {
int LCSuff [ m + 1 ] [ n + 1 ] ;
int result = 0 ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 || j == 0 ) LCSuff [ i ] [ j ] = 0 ;
else if ( X [ i - 1 ] == Y [ j - 1 ] ) {
LCSuff [ i ... | def LCSubStr ( X , Y , m , n ) :
LCSuff = [ [ 0 for k in range ( n + 1 ) ] for l in range ( m + 1 ) ]
result = 0
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if ( i == 0 or j == 0 ) :
LCSuff [ i ] [ j ] = 0
elif ( X [ i - 1 ] == Y [ j - 1 ] ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int LCSubStr ( char * X , char * Y , int m , int n ) {
int LCSuff [ m + 1 ] [ n + 1 ] ;
int result = 0 ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 || j == 0 ) LCSuff [ i ] [ j ] = 0 ;
else if ( X [ i - 1 ] == Y [ j - 1 ] ) {
LCSu... |
cpp-python-MAXIMUM_SUBSEQUENCE_SUM_SUCH_THAT_NO_THREE_ARE_CONSECUTIVE_1 | cpp-python | int maxSumWO3Consec ( int n ) {
if ( sum [ n ] != - 1 ) return sum [ n ] ;
if ( n == 0 ) return sum [ n ] = 0 ;
if ( n == 1 ) return sum [ n ] = arr [ 0 ] ;
if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ;
return sum [ n ] = max ( max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n -... | def maxSumWO3Consec ( n ) :
if ( sum [ n ] != - 1 ) :
return sum [ n ]
if ( n == 0 ) :
sum [ n ] = 0
return sum [ n ]
if ( n == 1 ) :
sum [ n ] = arr [ 0 ]
return sum [ n ]
if ( n == 2 ) :
sum [ n ] = arr [ 1 ] + arr [ 0 ]
return sum [ n ]
sum ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxSumWO3Consec ( int n ) {
if ( sum [ n ] != - 1 ) return sum [ n ] ;
if ( n == 0 ) return sum [ n ] = 0 ;
if ( n == 1 ) return sum [ n ] = arr [ 0 ] ;
if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ;
return sum [ n ] = max ( max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + ar... |
cpp-python-FIBONACCI_MODULO_P | cpp-python | int findMinZero ( int p ) {
int first = 1 , second = 1 , number = 2 , next = 1 ;
while ( next ) {
next = ( first + second ) % p ;
first = second ;
second = next ;
number ++ ;
}
return number ;
}
| def findMinZero ( p ) :
first = 1
second = 1
number = 2
next = 1
while ( next ) :
next = ( first + second ) % p
first = second
second = next
number = number + 1
return number
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findMinZero ( int p ) {
int first = 1 , second = 1 , number = 2 , next = 1 ;
while ( next ) {
next = ( first + second ) % p ;
first = second ;
second = next ;
number ++ ;
}
return number ;
}
</Code> |
cpp-python-REORDER_A_ARRAY_ACCORDING_TO_GIVEN_INDEXES_1 | cpp-python | void reorder ( int arr [ ] , int index [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( index [ i ] != i ) {
int oldTargetI = index [ index [ i ] ] ;
char oldTargetE = arr [ index [ i ] ] ;
arr [ index [ i ] ] = arr [ i ] ;
index [ index [ i ] ] = index [ i ] ;
index [ ... | def reorder ( arr , index , n ) :
for i in range ( 0 , n ) :
while ( index [ i ] != i ) :
oldTargetI = index [ index [ i ] ]
oldTargetE = arr [ index [ i ] ]
arr [ index [ i ] ] = arr [ i ]
index [ index [ i ] ] = index [ i ]
index [ i ] = oldTarge... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void reorder ( int arr [ ] , int index [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( index [ i ] != i ) {
int oldTargetI = index [ index [ i ] ] ;
char oldTargetE = arr [ index [ i ] ] ;
arr [ index [ i ] ] = arr [ i ] ;
index [ index [ i ] ] = index [ i ] ;
i... |
cpp-python-LENGTH_LONGEST_SUB_STRING_CAN_MAKE_REMOVED | cpp-python | int longestNull ( string str ) {
vector < pair < char , int >> arr ;
arr . push_back ( {
'@' , - 1 }
) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
++ i ) {
arr . push_back ( {
str [ i ] , i }
) ;
while ( arr . size ( ) >= 3 && arr [ arr . size ( ... | def longestNull ( S ) :
arr = [ ]
arr.append ( [ '@' , - 1 ] )
maxlen = 0
for i in range ( len ( S ) ) :
arr.append ( [ S [ i ] , i ] )
while ( len ( arr ) >= 3 and arr [ len ( arr ) - 3 ] [ 0 ] == '1' and arr [ len ( arr ) - 2 ] [ 0 ] == '0' and arr [ len ( arr ) - 1 ] [ 0 ] == '0' ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int longestNull ( string str ) {
vector < pair < char , int >> arr ;
arr . push_back ( {
'@' , - 1 }
) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
++ i ) {
arr . push_back ( {
str [ i ] , i }
) ;
while ( arr . size ( ) >= 3 && arr [ arr . ... |
cpp-python-SUM_FIBONACCI_NUMBERS | cpp-python | int calculateSum ( int n ) {
if ( n <= 0 ) return 0 ;
int fibo [ n + 1 ] ;
fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ;
int sum = fibo [ 0 ] + fibo [ 1 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ;
sum += fibo [ i ] ;
}
return sum ;
}
| def calculateSum ( n ) :
if ( n <= 0 ) :
return 0
fibo = [ 0 ] * ( n + 1 )
fibo [ 1 ] = 1
sm = fibo [ 0 ] + fibo [ 1 ]
for i in range ( 2 , n + 1 ) :
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ]
sm = sm + fibo [ i ]
return sm
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int calculateSum ( int n ) {
if ( n <= 0 ) return 0 ;
int fibo [ n + 1 ] ;
fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ;
int sum = fibo [ 0 ] + fibo [ 1 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ;
sum += fibo [ i ] ;
}
return sum ;
}
</Code> |
cpp-python-SHORTEST_PATH_EXACTLY_K_EDGES_DIRECTED_WEIGHTED_GRAPH | cpp-python | INT_MAX int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 0 ;
if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ;
if ( k <= 0 ) return INF ;
int res = INF ;
for ( int i = 0 ;
i < V ;
i ++ ) {
if ( graph [ u ] [ i ] != INF && u != i && v ... | def shortestPath ( graph , u , v , k ) :
V = 4
INF = 999999999999
if k == 0 and u == v :
return 0
if k == 1 and graph [ u ] [ v ] != INF :
return graph [ u ] [ v ]
if k <= 0 :
return INF
res = INF
for i in range ( V ) :
if graph [ u ] [ i ] != INF and u != i a... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
INT_MAX int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 0 ;
if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ;
if ( k <= 0 ) return INF ;
int res = INF ;
for ( int i = 0 ;
i < V ;
i ++ ) {
if ( graph [ u ] [ i ] != INF && u != ... |
cpp-python-SUM_SQUARES_BINOMIAL_COEFFICIENTS | cpp-python | int sumofsquare ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
for (... | def sumofsquare ( n ) :
C = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( 0 , n + 1 ) :
for j in range ( 0 , min ( i , n ) + 1 ) :
if ( j == 0 or j == i ) :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = ( C [ i - 1 ] [ j -... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int sumofsquare ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
... |
cpp-python-CONSTRUCT_ARRAY_PAIR_SUM_ARRAY | cpp-python | void constructArr ( int arr [ ] , int pair [ ] , int n ) {
arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ;
for ( int i = 1 ;
i < n ;
i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ;
}
| def constructArr ( arr , pair , n ) :
arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) // 2
for i in range ( 1 , n ) :
arr [ i ] = pair [ i - 1 ] - arr [ 0 ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void constructArr ( int arr [ ] , int pair [ ] , int n ) {
arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ;
for ( int i = 1 ;
i < n ;
i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ;
}
</Code> |
cpp-python-FIND_PATTERNS_101_GIVEN_STRING | cpp-python | int patternCount ( string str ) {
char last = str [ 0 ] ;
int i = 1 , counter = 0 ;
while ( i < str . size ( ) ) {
if ( str [ i ] == '0' && last == '1' ) {
while ( str [ i ] == '0' ) i ++ ;
if ( str [ i ] == '1' ) counter ++ ;
}
last = str [ i ] ;
i ++ ;
}
return counter ;
}
| def patternCount ( str ) :
last = str [ 0 ]
i = 1 counter = 0
while ( i < len ( str ) ) :
if ( str [ i ] == '0' and last == '1' ) :
while ( str [ i ] == '0' ) :
i += 1
if ( str [ i ] == '1' ) :
counter += 1
last = str [ i ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int patternCount ( string str ) {
char last = str [ 0 ] ;
int i = 1 , counter = 0 ;
while ( i < str . size ( ) ) {
if ( str [ i ] == '0' && last == '1' ) {
while ( str [ i ] == '0' ) i ++ ;
if ( str [ i ] == '1' ) counter ++ ;
}
last = str [ i ] ;
i ++ ;
}
return counter ;
}... |
cpp-python-RANGE_QUERIES_FOR_FREQUENCIES_OF_ARRAY_ELEMENTS | cpp-python | int findFrequency ( int arr [ ] , int n , int left , int right , int element ) {
int count = 0 ;
for ( int i = left - 1 ;
i <= right ;
++ i ) if ( arr [ i ] == element ) ++ count ;
return count ;
}
| def findFrequency ( arr , n , left , right , element ) :
count = 0
for i in range ( left - 1 , right ) :
if ( arr [ i ] == element ) :
count += 1
return count
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findFrequency ( int arr [ ] , int n , int left , int right , int element ) {
int count = 0 ;
for ( int i = left - 1 ;
i <= right ;
++ i ) if ( arr [ i ] == element ) ++ count ;
return count ;
}
</Code> |
cpp-python-FINDING_VERTEX_FOCUS_DIRECTRIX_PARABOLA | cpp-python | void parabola ( float a , float b , float c ) {
cout << "Vertex: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) << ")" << endl ;
cout << "Focus: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) << ")" << endl ;
cout << "Directrix: y=" << c ... | def parabola ( a , b , c ) :
print ( "Vertex: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) , ")" , sep = "" )
print ( "Focus: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) , ")" , sep = "" )
print ( "Directrix: y=" , c - ( ( b * b ) + ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void parabola ( float a , float b , float c ) {
cout << "Vertex: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) << ")" << endl ;
cout << "Focus: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) << ")" << endl ;
cout << "Directrix: y=... |
cpp-python-MINIMUM_SUM_PRODUCT_TWO_ARRAYS | cpp-python | int minproduct ( int a [ ] , int b [ ] , int n , int k ) {
int diff = 0 , res = 0 ;
int temp ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int pro = a [ i ] * b [ i ] ;
res = res + pro ;
if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ;
else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [... | def minproduct ( a , b , n , k ) :
diff = 0
res = 0
for i in range ( n ) :
pro = a [ i ] * b [ i ]
res = res + pro
if ( pro < 0 and b [ i ] < 0 ) :
temp = ( a [ i ] + 2 * k ) * b [ i ]
elif ( pro < 0 and a [ i ] < 0 ) :
temp = ( a [ i ] - 2 * k ) * b [... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minproduct ( int a [ ] , int b [ ] , int n , int k ) {
int diff = 0 , res = 0 ;
int temp ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int pro = a [ i ] * b [ i ] ;
res = res + pro ;
if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ;
else if ( pro < 0 && a [ i ] < 0 ) temp ... |
cpp-python-LONGEST_SUBSEQUENCE_WHERE_EVERY_CHARACTER_APPEARS_AT_LEAST_K_TIMES | cpp-python | void longestSubseqWithK ( string str , int k ) {
int n = str . size ( ) ;
int freq [ MAX_CHARS ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( freq [ str [ i ] - 'a' ] >= k ) cout << str [ i ] ;
}
| def longestSubseqWithK ( str , k ) :
n = len ( str )
freq = [ 0 ] * MAX_CHARS
for i in range ( n ) :
freq [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( n ) :
if ( freq [ ord ( str [ i ] ) - ord ( 'a' ) ] >= k ) :
print ( str [ i ] , end = "" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void longestSubseqWithK ( string str , int k ) {
int n = str . size ( ) ;
int freq [ MAX_CHARS ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( freq [ str [ i ] - 'a' ] >= k ) cout << str [ i ] ;
}
</Code> |
cpp-python-GIVEN_A_SORTED_AND_ROTATED_ARRAY_FIND_IF_THERE_IS_A_PAIR_WITH_A_GIVEN_SUM_1 | cpp-python | int pairsInSortedRotated ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ;
int l = ( i + 1 ) % n ;
int r = i ;
int cnt = 0 ;
while ( l != r ) {
if ( arr [ l ] + arr [ r ] == x ) {
cnt ++ ;
if ( l == ( r - 1 + n ) % n ) {
... | def pairsInSortedRotated ( arr , n , x ) :
for i in range ( n ) :
if arr [ i ] > arr [ i + 1 ] :
break
l = ( i + 1 ) % n
r = i
cnt = 0
while ( l != r ) :
if arr [ l ] + arr [ r ] == x :
cnt += 1
if l == ( r - 1 + n ) % n :
return cn... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int pairsInSortedRotated ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ;
int l = ( i + 1 ) % n ;
int r = i ;
int cnt = 0 ;
while ( l != r ) {
if ( arr [ l ] + arr [ r ] == x ) {
cnt ++ ;
if ( l == ( r - 1 + n ) % ... |
cpp-python-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_2 | cpp-python | int middleOfThree ( int a , int b , int c ) {
int x = a - b ;
int y = b - c ;
int z = a - c ;
if ( x * y > 0 ) return b ;
else if ( x * z > 0 ) return c ;
else return a ;
}
| def middleOfThree ( a , b , c ) :
x = a - b
y = b - c
z = a - c
if x * y > 0 :
return b
elif ( x * z > 0 ) :
return
else :
return a
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int middleOfThree ( int a , int b , int c ) {
int x = a - b ;
int y = b - c ;
int z = a - c ;
if ( x * y > 0 ) return b ;
else if ( x * z > 0 ) return c ;
else return a ;
}
</Code> |
cpp-python-NEXT_POWER_OF_2 | cpp-python | unsigned int nextPowerOf2 ( unsigned int n ) {
unsigned count = 0 ;
if ( n && ! ( n & ( n - 1 ) ) ) return n ;
while ( n != 0 ) {
n >>= 1 ;
count += 1 ;
}
return 1 << count ;
}
| def nextPowerOf2 ( n ) :
count = 0
if ( n and not ( n & ( n - 1 ) ) ) :
return n
while ( n != 0 ) :
n >>= 1
count += 1
return 1 << count
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
unsigned int nextPowerOf2 ( unsigned int n ) {
unsigned count = 0 ;
if ( n && ! ( n & ( n - 1 ) ) ) return n ;
while ( n != 0 ) {
n >>= 1 ;
count += 1 ;
}
return 1 << count ;
}
</Code> |
cpp-python-MAXIMUM_SUBARRAY_SUM_ARRAY_CREATED_REPEATED_CONCATENATION | cpp-python | int maxSubArraySumRepeated ( int a [ ] , int n , int k ) {
int max_so_far = INT_MIN , max_ending_here = 0 ;
for ( int i = 0 ;
i < n * k ;
i ++ ) {
max_ending_here = max_ending_here + a [ i % n ] ;
if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ;
if ( max_ending_here < 0 ) max_endin... | def maxSubArraySumRepeated ( a , n , k ) :
max_so_far = - 2147483648
max_ending_here = 0
for i in range ( n * k ) :
max_ending_here = max_ending_here + a [ i % n ]
if ( max_so_far < max_ending_here ) :
max_so_far = max_ending_here
if ( max_ending_here < 0 ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxSubArraySumRepeated ( int a [ ] , int n , int k ) {
int max_so_far = INT_MIN , max_ending_here = 0 ;
for ( int i = 0 ;
i < n * k ;
i ++ ) {
max_ending_here = max_ending_here + a [ i % n ] ;
if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ;
if ( max_ending_here < 0 ) ma... |
cpp-python-STACK_SET_3_REVERSE_STRING_USING_STACK | cpp-python | void reverse ( char str [ ] ) {
int n = strlen ( str ) , i ;
for ( i = 0 ;
i < n / 2 ;
i ++ ) swap ( & str [ i ] , & str [ n - i - 1 ] ) ;
}
| def reverse ( string ) :
string = string [ : : - 1 ]
return string
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void reverse ( char str [ ] ) {
int n = strlen ( str ) , i ;
for ( i = 0 ;
i < n / 2 ;
i ++ ) swap ( & str [ i ] , & str [ n - i - 1 ] ) ;
}
</Code> |
cpp-python-LEXICOGRAPHICALLY_NEXT_STRING | cpp-python | string nextWord ( string s ) {
if ( s == "" ) return "a" ;
int i = s . length ( ) - 1 ;
while ( s [ i ] == 'z' && i >= 0 ) i -- ;
if ( i == - 1 ) s = s + 'a' ;
else s [ i ] ++ ;
return s ;
}
| def nextWord ( s ) :
if ( s == " " ) :
return "a"
i = len ( s ) - 1
while ( s [ i ] == 'z' and i >= 0 ) :
i -= 1
if ( i == - 1 ) :
s = s + 'a'
else :
s = s.replace ( s [ i ] , chr ( ord ( s [ i ] ) + 1 ) , 1 )
return s
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
string nextWord ( string s ) {
if ( s == "" ) return "a" ;
int i = s . length ( ) - 1 ;
while ( s [ i ] == 'z' && i >= 0 ) i -- ;
if ( i == - 1 ) s = s + 'a' ;
else s [ i ] ++ ;
return s ;
}
</Code> |
cpp-python-SUM_BINOMIAL_COEFFICIENTS | cpp-python | int binomialCoeffSum ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
for ( ... | def binomialCoeffSum ( n ) :
C = [ [ 0 ] * ( n + 2 ) for i in range ( 0 , n + 2 ) ]
for i in range ( 0 , n + 1 ) :
for j in range ( 0 , min ( i , n ) + 1 ) :
if ( j == 0 or j == i ) :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] +... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int binomialCoeffSum ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
... |
cpp-python-CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD | cpp-python | bool isEven ( int n ) {
return ( n % 2 == 0 ) ;
}
| def isEven ( n ) :
return ( n % 2 == 0 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isEven ( int n ) {
return ( n % 2 == 0 ) ;
}
</Code> |
cpp-python-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY | cpp-python | int binarySearch ( int arr [ ] , int low , int high , int key ) {
if ( high < low ) return - 1 ;
int mid = ( low + high ) / 2 ;
if ( key == arr [ mid ] ) return mid ;
if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ;
return binarySearch ( arr , low , ( mid - 1 ) , key ) ;
}
| def binarySearch ( arr , low , high , key ) :
if ( high < low ) :
return - 1
mid = ( low + high ) / 2
if ( key == arr [ int ( mid ) ] ) :
return mid
if ( key > arr [ int ( mid ) ] ) :
return binarySearch ( arr , ( mid + 1 ) , high , key )
return ( binarySearch ( arr , low , (... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int binarySearch ( int arr [ ] , int low , int high , int key ) {
if ( high < low ) return - 1 ;
int mid = ( low + high ) / 2 ;
if ( key == arr [ mid ] ) return mid ;
if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ;
return binarySearch ( arr , low , ( mid - 1 ) , key ) ... |
cpp-python-MAXIMUM_AND_MINIMUM_IN_A_SQUARE_MATRIX | cpp-python | void maxMin ( int arr [ ] [ MAX ] , int n ) {
int min = INT_MAX ;
int max = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= n / 2 ;
j ++ ) {
if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) {
if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ] ;
... | def MAXMIN ( arr , n ) :
MIN = 10 ** 9
MAX = - 10 ** 9
for i in range ( n ) :
for j in range ( n // 2 + 1 ) :
if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) :
if ( MIN > arr [ i ] [ n - j - 1 ] ) :
MIN = arr [ i ] [ n - j - 1 ]
if ( M... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void maxMin ( int arr [ ] [ MAX ] , int n ) {
int min = INT_MAX ;
int max = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= n / 2 ;
j ++ ) {
if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) {
if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ... |
cpp-python-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO_1 | cpp-python | int bool isPowerOfTwo ( int x ) {
return x && ( ! ( x & ( x - 1 ) ) ) ;
}
| def isPowerOfTwo ( x ) :
return ( x and ( not ( x & ( x - 1 ) ) ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int bool isPowerOfTwo ( int x ) {
return x && ( ! ( x & ( x - 1 ) ) ) ;
}
</Code> |
cpp-python-COUNTS_PATHS_POINT_REACH_ORIGIN | cpp-python | int countPaths ( int n , int m ) {
if ( n == 0 || m == 0 ) return 1 ;
return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ;
}
| def countPaths ( n , m ) :
if ( n == 0 or m == 0 ) :
return 1
return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countPaths ( int n , int m ) {
if ( n == 0 || m == 0 ) return 1 ;
return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ;
}
</Code> |
cpp-python-FIND_RECTANGLE_BINARY_MATRIX_CORNERS_1_1 | cpp-python | bool isRectangle ( const vector < vector < int >> & matrix ) {
int rows = matrix . size ( ) ;
if ( rows == 0 ) return false ;
int columns = matrix [ 0 ] . size ( ) ;
unordered_map < int , unordered_set < int >> table ;
for ( int i = 0 ;
i < rows ;
++ i ) {
for ( int j = 0 ;
j < columns - 1 ;
+... | def isRectangle ( matrix ) :
rows = len ( matrix )
if ( rows == 0 ) :
return False
columns = len ( matrix [ 0 ] )
table = { }
for i in range ( rows ) :
for j in range ( columns - 1 ) :
for k in range ( j + 1 , columns ) :
if ( matrix [ i ] [ j ] == 1 and m... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isRectangle ( const vector < vector < int >> & matrix ) {
int rows = matrix . size ( ) ;
if ( rows == 0 ) return false ;
int columns = matrix [ 0 ] . size ( ) ;
unordered_map < int , unordered_set < int >> table ;
for ( int i = 0 ;
i < rows ;
++ i ) {
for ( int j = 0 ;
j < columns - 1 ... |
cpp-python-PROGRAM_DECIMAL_BINARY_CONVERSION | cpp-python | void decToBinary ( int n ) {
int binaryNum [ 32 ] ;
int i = 0 ;
while ( n > 0 ) {
binaryNum [ i ] = n % 2 ;
n = n / 2 ;
i ++ ;
}
for ( int j = i - 1 ;
j >= 0 ;
j -- ) cout << binaryNum [ j ] ;
}
| def decToBinary ( n ) :
binaryNum = [ 0 ] * n
i = 0
while ( n > 0 ) :
binaryNum [ i ] = n % 2
n = int ( n / 2 )
i += 1
for j in range ( i - 1 , - 1 , - 1 ) :
print ( binaryNum [ j ] , end = "" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void decToBinary ( int n ) {
int binaryNum [ 32 ] ;
int i = 0 ;
while ( n > 0 ) {
binaryNum [ i ] = n % 2 ;
n = n / 2 ;
i ++ ;
}
for ( int j = i - 1 ;
j >= 0 ;
j -- ) cout << binaryNum [ j ] ;
}
</Code> |
cpp-python-FRIENDS_PAIRING_PROBLEM_1 | cpp-python | int countFriendsPairings ( int n ) {
if ( dp [ n ] != - 1 ) return dp [ n ] ;
if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ;
else return dp [ n ] = n ;
}
| def countFriendsPairings ( n ) :
dp = [ - 1 ] * 100
if ( dp [ n ] != - 1 ) :
return dp [ n ]
if ( n > 2 ) :
dp [ n ] = ( countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) )
return dp [ n ]
else :
dp [ n ] = n
return dp [ n ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countFriendsPairings ( int n ) {
if ( dp [ n ] != - 1 ) return dp [ n ] ;
if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ;
else return dp [ n ] = n ;
}
</Code> |
cpp-python-MAXIMUM_SUBARRAY_SUM_USING_PREFIX_SUM | cpp-python | int maximumSumSubarray ( int arr [ ] , int n ) {
int min_prefix_sum = 0 ;
int res = numeric_limits < int > :: min ( ) ;
int prefix_sum [ 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 ++ ) {
... | def maximumSumSubarray ( arr , n ) :
min_prefix_sum = 0
res = - math.inf
prefix_sum = [ ]
prefix_sum.append ( arr [ 0 ] )
for i in range ( 1 , n ) :
prefix_sum.append ( prefix_sum [ i - 1 ] + arr [ i ] )
for i in range ( n ) :
res = max ( res , prefix_sum [ i ] - min_prefix_sum )... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maximumSumSubarray ( int arr [ ] , int n ) {
int min_prefix_sum = 0 ;
int res = numeric_limits < int > :: min ( ) ;
int prefix_sum [ 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 ++ )... |
cpp-python-NUMBER_SUBSEQUENCES_AB_STRING_REPEATED_K_TIMES | cpp-python | int countOccurrences ( string s , int K ) {
int n = s . length ( ) ;
int C , c1 = 0 , c2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s [ i ] == 'a' ) c1 ++ ;
if ( s [ i ] == 'b' ) {
c2 ++ ;
C += c1 ;
}
}
return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ;
}
| def countOccurrences ( s , K ) :
n = len ( s )
c1 = 0
c2 = 0
C = 0
for i in range ( n ) :
if s [ i ] == 'a' :
c1 += 1
if s [ i ] == 'b' :
c2 += 1
C += c1
return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countOccurrences ( string s , int K ) {
int n = s . length ( ) ;
int C , c1 = 0 , c2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s [ i ] == 'a' ) c1 ++ ;
if ( s [ i ] == 'b' ) {
c2 ++ ;
C += c1 ;
}
}
return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ;
}
</Code> |
cpp-python-CHECK_LARGE_NUMBER_DIVISIBLE_13_NOT | cpp-python | bool checkDivisibility ( string num ) {
int length = num . size ( ) ;
if ( length == 1 && num [ 0 ] == '0' ) return true ;
if ( length % 3 == 1 ) {
num += "00" ;
length += 2 ;
}
else if ( length % 3 == 2 ) {
num += "0" ;
length += 1 ;
}
int sum = 0 , p = 1 ;
for ( int i = length - 1 ;
... | def checkDivisibility ( num ) :
length = len ( num )
if ( length == 1 and num [ 0 ] == '0' ) :
return True
if ( length % 3 == 1 ) :
num = str ( num ) + "00"
length += 2
elif ( length % 3 == 2 ) :
num = str ( num ) + "0"
length += 1
sum = 0
p = 1
for i ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool checkDivisibility ( string num ) {
int length = num . size ( ) ;
if ( length == 1 && num [ 0 ] == '0' ) return true ;
if ( length % 3 == 1 ) {
num += "00" ;
length += 2 ;
}
else if ( length % 3 == 2 ) {
num += "0" ;
length += 1 ;
}
int sum = 0 , p = 1 ;
for ( int i = length -... |
cpp-python-NUMBER_UNIQUE_RECTANGLES_FORMED_USING_N_UNIT_SQUARES | cpp-python | int countRect ( int n ) {
int ans = 0 ;
for ( int length = 1 ;
length <= sqrt ( n ) ;
++ length ) for ( int height = length ;
height * length <= n ;
++ height ) ans ++ ;
return ans ;
}
| def countRect ( n ) :
ans = 0
for length in range ( 1 , int ( math.sqrt ( n ) ) + 1 ) :
height = length
while ( height * length <= n ) :
ans += 1
height += 1
return ans
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countRect ( int n ) {
int ans = 0 ;
for ( int length = 1 ;
length <= sqrt ( n ) ;
++ length ) for ( int height = length ;
height * length <= n ;
++ height ) ans ++ ;
return ans ;
}
</Code> |
cpp-python-COUNT_SET_BITS_IN_AN_INTEGER_2 | cpp-python | public : unsigned int countSetBits ( int n ) {
unsigned int count = 0 ;
while ( n ) {
n &= ( n - 1 ) ;
count ++ ;
}
return count ;
}
| def countSetBits ( n ) :
count = 0
while ( n ) :
n &= ( n - 1 )
count += 1
return count
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
public : unsigned int countSetBits ( int n ) {
unsigned int count = 0 ;
while ( n ) {
n &= ( n - 1 ) ;
count ++ ;
}
return count ;
}
</Code> |
cpp-python-LONGEST_SUBARRAY_COUNT_1S_ONE_COUNT_0S | cpp-python | int lenOfLongSubarr ( int arr [ ] , int n ) {
unordered_map < int , int > um ;
int sum = 0 , maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] == 0 ? - 1 : 1 ;
if ( sum == 1 ) maxLen = i + 1 ;
else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ;
if ( um . find ( sum - ... | def lenOfLongSubarr ( arr , n ) :
um = { i : 0 for i in range ( 10 ) }
sum = 0
maxLen = 0
for i in range ( n ) :
if arr [ i ] == 0 :
sum += - 1
else :
sum += 1
if ( sum == 1 ) :
maxLen = i + 1
elif ( sum not in um ) :
um [ s... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int lenOfLongSubarr ( int arr [ ] , int n ) {
unordered_map < int , int > um ;
int sum = 0 , maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] == 0 ? - 1 : 1 ;
if ( sum == 1 ) maxLen = i + 1 ;
else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ;
if ( um . find (... |
cpp-python-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER_1 | cpp-python | unsigned int factorial ( unsigned int n ) {
int res = 1 , i ;
for ( i = 2 ;
i <= n ;
i ++ ) res *= i ;
return res ;
}
| def factorial ( n ) :
return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
unsigned int factorial ( unsigned int n ) {
int res = 1 , i ;
for ( i = 2 ;
i <= n ;
i ++ ) res *= i ;
return res ;
}
</Code> |
cpp-python-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY | cpp-python | int minJumps ( int arr [ ] , int n ) {
if ( n == 1 ) return 0 ;
int res = INT_MAX ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( i + arr [ i ] >= n - 1 ) {
int sub_res = minJumps ( arr , i + 1 ) ;
if ( sub_res != INT_MAX ) res = min ( res , sub_res + 1 ) ;
}
}
return res ;
}
| def minJumps ( arr , l , h ) :
if ( h == l ) :
return 0
if ( arr [ l ] == 0 ) :
return float ( 'inf' )
min = float ( 'inf' )
for i in range ( l + 1 , h + 1 ) :
if ( i < l + arr [ l ] + 1 ) :
jumps = minJumps ( arr , i , h )
if ( jumps != float ( 'inf' ) an... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minJumps ( int arr [ ] , int n ) {
if ( n == 1 ) return 0 ;
int res = INT_MAX ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( i + arr [ i ] >= n - 1 ) {
int sub_res = minJumps ( arr , i + 1 ) ;
if ( sub_res != INT_MAX ) res = min ( res , sub_res + 1 ) ;
}
}
return res ;
}
</... |
cpp-python-MINIMUM_TIME_WRITE_CHARACTERS_USING_INSERT_DELETE_COPY_OPERATION | cpp-python | int minTimeForWritingChars ( int N , int insert , int remove , int copy ) {
if ( N == 0 ) return 0 ;
if ( N == 1 ) return insert ;
int dp [ N + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
if ( i % 2 == 0 ) dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy )... | def minTimeForWritingChars ( N , insrt , remov , cpy ) :
if N == 0 :
return 0
if N == 1 :
return insrt
dp = [ 0 ] * ( N + 1 )
for i in range ( 1 , N + 1 ) :
if i % 2 == 0 :
dp [ i ] = min ( dp [ i - 1 ] + insrt , dp [ i // 2 ] + cpy )
else :
dp [ i... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minTimeForWritingChars ( int N , int insert , int remove , int copy ) {
if ( N == 0 ) return 0 ;
if ( N == 1 ) return insert ;
int dp [ N + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
if ( i % 2 == 0 ) dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i / 2 ] +... |
cpp-python-PRINTING_STRING_PLUS_PATTERN_MATRIX | cpp-python | void carveCross ( string str ) {
int n = str . length ( ) ;
if ( n % 2 == 0 ) {
cout << "Not possible. Please enter " << "odd length string.\n" ;
}
else {
char arr [ max ] [ max ] ;
int m = n / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
... | def carveCross ( str ) :
n = len ( str )
if ( n % 2 == 0 ) :
print ( "Not possible.Please enter " , "odd length string.\n" )
else :
arr = [ [ False for x in range ( max ) ] for y in range ( max ) ]
m = n // 2
for i in range ( n ) :
for j in range ( n ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void carveCross ( string str ) {
int n = str . length ( ) ;
if ( n % 2 == 0 ) {
cout << "Not possible. Please enter " << "odd length string.\n" ;
}
else {
char arr [ max ] [ max ] ;
int m = n / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++... |
cpp-python-SUBARRAYS_DISTINCT_ELEMENTS | cpp-python | int sumoflength ( int arr [ ] , int n ) {
unordered_set < int > s ;
int j = 0 , ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < n && s . find ( arr [ j ] ) == s . end ( ) ) {
s . insert ( arr [ j ] ) ;
j ++ ;
}
ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ;
s . erase ( arr [ i... | def sumoflength ( arr , n ) :
s = [ ]
j = 0
ans = 0
for i in range ( n ) :
while ( j < n and ( arr [ j ] not in s ) ) :
s.append ( arr [ j ] )
j += 1
ans += ( ( j - i ) * ( j - i + 1 ) ) // 2
s.remove ( arr [ i ] )
return ans
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int sumoflength ( int arr [ ] , int n ) {
unordered_set < int > s ;
int j = 0 , ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < n && s . find ( arr [ j ] ) == s . end ( ) ) {
s . insert ( arr [ j ] ) ;
j ++ ;
}
ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ;
s . erase ( ... |
cpp-python-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_1 | cpp-python | void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
unordered_set < int > s ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int x = - ( arr [ i ] + arr [ j ] ) ;
if ( s . find ( x ) != s . end ( ) ) {
printf ( "%d %d %d\n" , x , ar... | def findTriplets ( arr , n ) :
found = False
for i in range ( n - 1 ) :
s = set ( )
for j in range ( i + 1 , n ) :
x = - ( arr [ i ] + arr [ j ] )
if x in s :
print ( x , arr [ i ] , arr [ j ] )
found = True
else :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
unordered_set < int > s ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int x = - ( arr [ i ] + arr [ j ] ) ;
if ( s . find ( x ) != s . end ( ) ) {
printf ( "%d %d %d\n" ,... |
cpp-python-MAXIMUM_LENGTH_PREFIX_ONE_STRING_OCCURS_SUBSEQUENCE_ANOTHER | cpp-python | int maxPrefix ( char s [ ] , char t [ ] ) {
int count = 0 ;
for ( int i = 0 ;
i < strlen ( t ) ;
i ++ ) {
if ( count == strlen ( s ) ) break ;
if ( t [ i ] == s [ count ] ) count ++ ;
}
return count ;
}
| def maxPrefix ( s , t ) :
count = 0
for i in range ( 0 , len ( t ) ) :
if ( count == len ( s ) ) :
break
if ( t [ i ] == s [ count ] ) :
count = count + 1
return count
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxPrefix ( char s [ ] , char t [ ] ) {
int count = 0 ;
for ( int i = 0 ;
i < strlen ( t ) ;
i ++ ) {
if ( count == strlen ( s ) ) break ;
if ( t [ i ] == s [ count ] ) count ++ ;
}
return count ;
}
</Code> |
cpp-python-MOVE_VE_ELEMENTS_END_ORDER_EXTRA_SPACE_ALLOWED | cpp-python | void segregateElements ( int arr [ ] , int n ) {
int temp [ n ] ;
int j = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ;
if ( j == n || j == 0 ) return ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ;
memcpy ( arr , temp , si... | def segregateElements ( arr , n ) :
temp = [ 0 for k in range ( n ) ]
j = 0
for i in range ( n ) :
if ( arr [ i ] >= 0 ) :
temp [ j ] = arr [ i ]
j += 1
if ( j == n or j == 0 ) :
return
for i in range ( n ) :
if ( arr [ i ] < 0 ) :
temp [ j... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void segregateElements ( int arr [ ] , int n ) {
int temp [ n ] ;
int j = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ;
if ( j == n || j == 0 ) return ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ;
memcpy ( arr , te... |
cpp-python-MERGING_INTERVALS | cpp-python | void mergeIntervals ( Interval arr [ ] , int n ) {
sort ( arr , arr + n , mycomp ) ;
int index = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( arr [ index ] . e >= arr [ i ] . s ) {
arr [ index ] . e = max ( arr [ index ] . e , arr [ i ] . e ) ;
arr [ index ] . s = min ( arr [ index ] . s , arr ... | def mergeIntervals ( arr ) :
arr.sort ( key = lambda x : x [ 0 ] )
m = [ ]
s = - 10000
max = - 100000
for i in range ( len ( arr ) ) :
a = arr [ i ]
if a [ 0 ] > max :
if i != 0 :
m.append ( [ s , max ] )
max = a [ 1 ]
s = a [ 0 ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void mergeIntervals ( Interval arr [ ] , int n ) {
sort ( arr , arr + n , mycomp ) ;
int index = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( arr [ index ] . e >= arr [ i ] . s ) {
arr [ index ] . e = max ( arr [ index ] . e , arr [ i ] . e ) ;
arr [ index ] . s = min ( arr [ index ] . s... |
cpp-python-PROGRAM_FIND_CORRELATION_COEFFICIENT | cpp-python | float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) {
int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ;
int squareSum_X = 0 , squareSum_Y = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum_X = sum_X + X [ i ] ;
sum_Y = sum_Y + Y [ i ] ;
sum_XY = sum_XY + X [ i ] * Y [ i ] ;
squareSum_X = squareSum... | def correlationCoefficient ( X , Y , n ) :
sum_X = 0
sum_Y = 0
sum_XY = 0
squareSum_X = 0
squareSum_Y = 0
i = 0
while i < n :
sum_X = sum_X + X [ i ]
sum_Y = sum_Y + Y [ i ]
sum_XY = sum_XY + X [ i ] * Y [ i ]
squareSum_X = squareSum_X + X [ i ] * X [ i ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) {
int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ;
int squareSum_X = 0 , squareSum_Y = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum_X = sum_X + X [ i ] ;
sum_Y = sum_Y + Y [ i ] ;
sum_XY = sum_XY + X [ i ] * Y [ i ] ;
squareSum_X = sq... |
cpp-python-MAXIMUM_AREA_RECTANGLE_PICKING_FOUR_SIDES_ARRAY | cpp-python | int findArea ( int arr [ ] , int n ) {
sort ( arr , arr + n , greater < int > ( ) ) ;
int dimension [ 2 ] = {
0 , 0 }
;
for ( int i = 0 , j = 0 ;
i < n - 1 && j < 2 ;
i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ;
return ( dimension [ 0 ] * dimension [ 1 ] ) ;
... | def findArea ( arr , n ) :
arr.sort ( reverse = True )
dimension = [ 0 , 0 ]
i = 0
j = 0
while ( i < n - 1 and j < 2 ) :
if ( arr [ i ] == arr [ i + 1 ] ) :
dimension [ j ] = arr [ i ]
j += 1
i += 1
i += 1
return ( dimension [ 0 ] * dimension [... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findArea ( int arr [ ] , int n ) {
sort ( arr , arr + n , greater < int > ( ) ) ;
int dimension [ 2 ] = {
0 , 0 }
;
for ( int i = 0 , j = 0 ;
i < n - 1 && j < 2 ;
i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ;
return ( dimension [ 0 ] * dimension [ 1 ... |
cpp-python-MINIMUM_INCREMENT_K_OPERATIONS_MAKE_ELEMENTS_EQUAL | cpp-python | int minOps ( int arr [ ] , int n , int k ) {
int max = * max_element ( arr , arr + n ) ;
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ;
else res += ( max - arr [ i ] ) / k ;
}
return res ;
}
| def minOps ( arr , n , k ) :
max1 = max ( arr )
res = 0
for i in range ( 0 , n ) :
if ( ( max1 - arr [ i ] ) % k != 0 ) :
return - 1
else :
res += ( max1 - arr [ i ] ) / k
return int ( res )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minOps ( int arr [ ] , int n , int k ) {
int max = * max_element ( arr , arr + n ) ;
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ;
else res += ( max - arr [ i ] ) / k ;
}
return res ;
}
</Code> |
cpp-python-SQUARE_ROOT_OF_AN_INTEGER | cpp-python | int floorSqrt ( int x ) {
if ( x == 0 || x == 1 ) return x ;
int i = 1 , result = 1 ;
while ( result <= x ) {
i ++ ;
result = i * i ;
}
return i - 1 ;
}
| def floorSqrt ( x ) :
if ( x == 0 or x == 1 ) :
return x
i = 1 result = 1
while ( result <= x ) :
i += 1
result = i * i
return i - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int floorSqrt ( int x ) {
if ( x == 0 || x == 1 ) return x ;
int i = 1 , result = 1 ;
while ( result <= x ) {
i ++ ;
result = i * i ;
}
return i - 1 ;
}
</Code> |
cpp-python-CHECK_WHETHER_LARGE_NUMBER_DIVISIBLE_7 | cpp-python | int isdivisible7 ( char num [ ] ) {
int n = strlen ( num ) , gSum ;
if ( n == 0 && num [ 0 ] == '\n' ) return 1 ;
if ( n % 3 == 1 ) {
strcat ( num , "00" ) ;
n += 2 ;
}
else if ( n % 3 == 2 ) {
strcat ( num , "0" ) ;
n ++ ;
}
int i , GSum = 0 , p = 1 ;
for ( i = n - 1 ;
i >= 0 ;
i --... | def isdivisible7 ( num ) :
n = len ( num )
if ( n == 0 and num [ 0 ] == '\n' ) :
return 1
if ( n % 3 == 1 ) :
num = str ( num ) + "00"
n += 2
elif ( n % 3 == 2 ) :
num = str ( num ) + "0"
n += 1
GSum = 0
p = 1
for i in range ( n - 1 , - 1 , - 1 ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int isdivisible7 ( char num [ ] ) {
int n = strlen ( num ) , gSum ;
if ( n == 0 && num [ 0 ] == '\n' ) return 1 ;
if ( n % 3 == 1 ) {
strcat ( num , "00" ) ;
n += 2 ;
}
else if ( n % 3 == 2 ) {
strcat ( num , "0" ) ;
n ++ ;
}
int i , GSum = 0 , p = 1 ;
for ( i = n - 1 ;
i >= 0 ;... |
cpp-python-CONSTRUCT_GRAPH_GIVEN_DEGREES_VERTICES | cpp-python | void printMat ( int degseq [ ] , int n ) {
int mat [ n ] [ n ] ;
memset ( mat , 0 , sizeof ( mat ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) {
degseq [ i ] -- ;
degseq [ j ] -- ;
mat [ i ]... | def printMat ( degseq , n ) :
mat = [ [ 0 ] * n for i in range ( n ) ]
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if ( degseq [ i ] > 0 and degseq [ j ] > 0 ) :
degseq [ i ] -= 1
degseq [ j ] -= 1
mat [ i ] [ j ] = 1
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printMat ( int degseq [ ] , int n ) {
int mat [ n ] [ n ] ;
memset ( mat , 0 , sizeof ( mat ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) {
degseq [ i ] -- ;
degseq [ j ] -- ;
ma... |
cpp-python-MINIMUM_TIME_REQUIRED_PRODUCE_M_ITEMS | cpp-python | int minTime ( int arr [ ] , int n , int m ) {
int t = 0 ;
while ( 1 ) {
int items = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) items += ( t / arr [ i ] ) ;
if ( items >= m ) return t ;
t ++ ;
}
}
| def minTime ( arr , n , m ) :
t = 0
while ( 1 ) :
items = 0
for i in range ( n ) :
items += ( t // arr [ i ] )
if ( items >= m ) :
return t
t += 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minTime ( int arr [ ] , int n , int m ) {
int t = 0 ;
while ( 1 ) {
int items = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) items += ( t / arr [ i ] ) ;
if ( items >= m ) return t ;
t ++ ;
}
}
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.