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-SUM_PAIRWISE_PRODUCTS_2 | cpp-python | long long int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
| def findSum ( n ) :
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
long long int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
</Code> |
cpp-python-PROGRAM_FOR_NTH_FIBONACCI_NUMBER_2 | cpp-python | int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] ) return f [ n ] ;
int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ;
return f... | def fib ( n ) :
if ( n == 0 ) :
return 0
if ( n == 1 or n == 2 ) :
f [ n ] = 1
return ( f [ n ] )
if ( f [ n ] ) :
return f [ n ]
if ( n & 1 ) :
k = ( n + 1 ) // 2
else :
k = n // 2
if ( ( n & 1 ) ) :
f [ n ] = ( fib ( k ) * fib ( k ) + fib... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] ) return f [ n ] ;
int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ;
r... |
cpp-python-GIVEN_P_AND_N_FIND_THE_LARGEST_X_SUCH_THAT_PX_DIVIDES_N_2 | cpp-python | int largestPower ( int n , int p ) {
int x = 0 ;
while ( n ) {
n /= p ;
x += n ;
}
return x ;
}
| def largestPower ( n , p ) :
x = 0
while n :
n /= p
x += n
return x
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int largestPower ( int n , int p ) {
int x = 0 ;
while ( n ) {
n /= p ;
x += n ;
}
return x ;
}
</Code> |
cpp-python-PROGRAM_PRINT_IDENTITY_MATRIX_1 | cpp-python | bool isIdentity ( int mat [ ] [ MAX ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ;
}
| def isIdentity ( mat , N ) :
for row in range ( N ) :
for col in range ( N ) :
if ( row == col and mat [ row ] [ col ] != 1 ) :
return False
elif ( row != col and mat [ row ] [ col ] != 0 ) :
return False
return True
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isIdentity ( int mat [ ] [ MAX ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ... |
cpp-python-MINIMUM_CELLS_REQUIRED_REACH_DESTINATION_JUMPS_EQUAL_CELL_VALUES | cpp-python | int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) {
int dp [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = INT_MAX ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( dp [ i ] [ ... | def minCells ( mat , m , n ) :
dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( m ) :
for j in range ( n ) :
if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) {
int dp [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = INT_MAX ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( dp [... |
cpp-python-REARRANGE_BINARY_STRING_ALTERNATE_X_Y_OCCURRENCES | cpp-python | void arrangeString ( string str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str [ i ] == '0' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j < x && count_0 > 0... | def arrangeString ( str1 , x , y ) :
count_0 = 0
count_1 = 0
n = len ( str1 )
for i in range ( n ) :
if str1 [ i ] == '0' :
count_0 += 1
else :
count_1 += 1
while count_0 > 0 or count_1 > 0 :
for i in range ( 0 , x ) :
if count_0 > 0 :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void arrangeString ( string str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str [ i ] == '0' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j < x && coun... |
cpp-python-MINIMUM_NUMBER_DELETIONS_MAKE_STRING_PALINDROME_SET_2 | cpp-python | int getLevenstein ( string const & input ) {
string revInput ( input . rbegin ( ) , input . rend ( ) ) ;
int n = input . size ( ) ;
vector < vector < int >> dp ( n + 1 , vector < int > ( n + 1 , - 1 ) ) ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
}
for ( int i... | def getLevenstein ( inpt ) :
revInput = inpt [ : : - 1 ]
n = len ( inpt )
dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ 0 ] [ i ] = i
dp [ i ] [ 0 ] = i
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int getLevenstein ( string const & input ) {
string revInput ( input . rbegin ( ) , input . rend ( ) ) ;
int n = input . size ( ) ;
vector < vector < int >> dp ( n + 1 , vector < int > ( n + 1 , - 1 ) ) ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
}
for ... |
cpp-python-DYNAMIC_PROGRAMMING_SET_34_ASSEMBLY_LINE_SCHEDULING | cpp-python | int carAssembly ( int a [ ] [ NUM_STATION ] , int t [ ] [ NUM_STATION ] , int * e , int * x ) {
int T1 [ NUM_STATION ] , T2 [ NUM_STATION ] , i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i... | def carAssembly ( a , t , e , x ) :
NUM_STATION = len ( a [ 0 ] )
T1 = [ 0 for i in range ( NUM_STATION ) ]
T2 = [ 0 for i in range ( NUM_STATION ) ]
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ]
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ]
for i in range ( 1 , NUM_STATION ) :
T1 [ i ] = min ( T1 [ i - 1 ] +... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int carAssembly ( int a [ ] [ NUM_STATION ] , int t [ ] [ NUM_STATION ] , int * e , int * x ) {
int T1 [ NUM_STATION ] , T2 [ NUM_STATION ] , i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min ( T1 [ i - 1 ] + a [ ... |
cpp-python-BASIC_AND_EXTENDED_EUCLIDEAN_ALGORITHMS | cpp-python | int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
| def gcd ( a , b ) :
if a == 0 :
return b
return gcd ( b % a , a )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
</Code> |
cpp-python-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_2 | cpp-python | void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
printf ( "%d %d %d\n" , x , arr [ l ] , a... | def findTriplets ( arr , n ) :
found = False
arr.sort ( )
for i in range ( 0 , n - 1 ) :
l = i + 1
r = n - 1
x = arr [ i ]
while ( l < r ) :
if ( x + arr [ l ] + arr [ r ] == 0 ) :
print ( x , arr [ l ] , arr [ r ] )
l += 1
... | 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 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
printf ( "%d %d %d\n" , x , arr [ ... |
cpp-python-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES_2 | cpp-python | int getOddOccurrence ( int ar [ ] , int ar_size ) {
int res = 0 ;
for ( int i = 0 ;
i < ar_size ;
i ++ ) res = res ^ ar [ i ] ;
return res ;
}
| def getOddOccurrence ( arr ) :
res = 0
for element in arr :
res = res ^ element
return res
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int getOddOccurrence ( int ar [ ] , int ar_size ) {
int res = 0 ;
for ( int i = 0 ;
i < ar_size ;
i ++ ) res = res ^ ar [ i ] ;
return res ;
}
</Code> |
cpp-python-DYNAMIC_PROGRAMMING_HIGH_EFFORT_VS_LOW_EFFORT_TASKS_PROBLEM | cpp-python | int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
| def maxTasks ( high , low , n ) :
if ( n <= 0 ) :
return 0
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
</Code> |
cpp-python-FIND_TWO_SIDES_RIGHT_ANGLE_TRIANGLE | cpp-python | void printOtherSides ( int n ) {
if ( n & 1 ) {
if ( n == 1 ) cout << - 1 << endl ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
cout << "b = " << b << ", c = " << c << endl ;
}
}
else {
if ( n == 2 ) cout << - 1 << endl ;
else {
int b = n * n / 4 - 1 ... | def printOtherSides ( n ) :
if ( n & 1 ) :
if ( n == 1 ) :
print ( - 1 )
else :
b = ( n * n - 1 ) // 2
c = ( n * n + 1 ) // 2
print ( "b =" , b , ", c =" , c )
else :
if ( n == 2 ) :
print ( - 1 )
else :
b = ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printOtherSides ( int n ) {
if ( n & 1 ) {
if ( n == 1 ) cout << - 1 << endl ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
cout << "b = " << b << ", c = " << c << endl ;
}
}
else {
if ( n == 2 ) cout << - 1 << endl ;
else {
int b = n * n /... |
cpp-python-MINIMUM_LENGTH_UNSORTED_SUBARRAY_SORTING_WHICH_MAKES_THE_COMPLETE_ARRAY_SORTED | cpp-python | void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
cout << "The complete array is sorted" ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e -- ) {
if ( arr [ e ]... | def printUnsorted ( arr , n ) :
e = n - 1
for s in range ( 0 , n - 1 ) :
if arr [ s ] > arr [ s + 1 ] :
break
if s == n - 1 :
print ( "The complete array is sorted" )
exit ( )
e = n - 1
while e > 0 :
if arr [ e ] < arr [ e - 1 ] :
break
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
cout << "The complete array is sorted" ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e -- ) {
if ( ar... |
cpp-python-CHECK_TWO_GIVEN_CIRCLES_TOUCH_INTERSECT | cpp-python | int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
| def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) :
distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 )
radSumSq = ( r1 + r2 ) * ( r1 + r2 )
if ( distSq == radSumSq ) :
return 1
elif ( distSq > radSumSq ) :
return - 1
else :
return 0
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
</Code> |
cpp-python-COUNT_DISTINCT_ELEMENTS_IN_EVERY_WINDOW_OF_SIZE_K | cpp-python | void countDistinct ( int arr [ ] , int k , int n ) {
map < int , int > hm ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hm [ arr [ i ] ] == 0 ) {
dist_count ++ ;
}
hm [ arr [ i ] ] += 1 ;
}
cout << dist_count << endl ;
for ( int i = k ;
i < n ;
i ++ ) {
if ( hm... | def countDistinct ( arr , k , n ) :
mp = defaultdict ( lambda : 0 )
dist_count = 0
for i in range ( k ) :
if mp [ arr [ i ] ] == 0 :
dist_count += 1
mp [ arr [ i ] ] += 1
print ( dist_count )
for i in range ( k , n ) :
if mp [ arr [ i - k ] ] == 1 :
di... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void countDistinct ( int arr [ ] , int k , int n ) {
map < int , int > hm ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hm [ arr [ i ] ] == 0 ) {
dist_count ++ ;
}
hm [ arr [ i ] ] += 1 ;
}
cout << dist_count << endl ;
for ( int i = k ;
i < n ;
i ++ ) {
... |
cpp-python-PROGRAM_TO_FIND_THE_AREA_OF_PENTAGON | cpp-python | float findArea ( float a ) {
float area ;
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
| def findArea ( a ) :
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4
return area
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
float findArea ( float a ) {
float area ;
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
</Code> |
cpp-python-COUNT_ROTATIONS_DIVISIBLE_8 | cpp-python | int countRotationsDivBy8 ( string n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n [ 0 ] - '0' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ;
int second = ( n [ 1 ] - '0' ) * 1... | def countRotationsDivBy8 ( n ) :
l = len ( n )
count = 0
if ( l == 1 ) :
oneDigit = int ( n [ 0 ] )
if ( oneDigit % 8 == 0 ) :
return 1
return 0
if ( l == 2 ) :
first = int ( n [ 0 ] ) * 10 + int ( n [ 1 ] )
second = int ( n [ 1 ] ) * 10 + int ( n [ 0 ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countRotationsDivBy8 ( string n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n [ 0 ] - '0' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ;
int second = ( n [ 1 ] - '0... |
cpp-python-LONGEST_COMMON_SUBSEQUENCE_WITH_AT_MOST_K_CHANGES_ALLOWED | cpp-python | int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) {
if ( k < 0 ) return - 1e7 ;
if ( n < 0 || m < 0 ) return 0 ;
int & ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , ... | def lcs ( dp , arr1 , n , arr2 , m , k ) :
if k < 0 :
return - ( 10 ** 7 )
if n < 0 or m < 0 :
return 0
ans = dp [ n ] [ m ] [ k ]
if ans != - 1 :
return ans
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) )
if arr1 [ n - 1 ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) {
if ( k < 0 ) return - 1e7 ;
if ( n < 0 || m < 0 ) return 0 ;
int & ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , ... |
cpp-python-DYNAMIC_PROGRAMMING_SET_5_EDIT_DISTANCE_1 | cpp-python | int editDistDP ( string str1 , string str2 , int m , int n ) {
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ... | def editDistDP ( str1 , str2 , m , n ) :
dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if i == 0 :
dp [ i ] [ j ] = j
elif j == 0 :
dp [ i ] [ j ] = i
elif str1 [ ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int editDistDP ( string str1 , string str2 , int m , int n ) {
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( str1 [ i - 1 ] == str2 [ j... |
cpp-python-FIND_NUMBER_TRANSFORMATION_MAKE_TWO_MATRIX_EQUAL | cpp-python | int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [... | def countOps ( A , B , m , n ) :
for i in range ( n ) :
for j in range ( m ) :
A [ i ] [ j ] -= B [ i ] [ j ]
for i in range ( 1 , n ) :
for j in range ( 1 , n ) :
if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) :
return - 1
r... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A ... |
cpp-python-LONGEST_ALTERNATING_POSITIVE_NEGATIVE_SUBARRAY_STARTING_EVERY_INDEX | cpp-python | void longestAlternating ( int arr [ ] , int n ) {
int count [ 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 ++ ) cout << count [ i ] << " " ;
}
| def longestAlternating ( arr , n ) :
count = [ None ] * n
count [ n - 1 ] = 1
i = n - 2
while i >= 0 :
if ( arr [ i ] * arr [ i + 1 ] < 0 ) :
count [ i ] = count [ i + 1 ] + 1
else :
count [ i ] = 1
i = i - 1
i = 0
while i < n :
print ( cou... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void longestAlternating ( int arr [ ] , int n ) {
int count [ 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 ++ ) cout << count [ i ] << " "... |
cpp-python-FIND_ALL_DIVISORS_OF_A_NATURAL_NUMBER_SET_2 | cpp-python | void printDivisors ( int n ) {
vector < int > v ;
for ( int i = 1 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) printf ( "%d " , i ) ;
else {
printf ( "%d " , i ) ;
v . push_back ( n / i ) ;
}
}
}
for ( int i = v . size ( ) - 1 ;
i >= 0 ;
i ... | def printDivisors ( n ) :
list = [ ]
for i in range ( 1 , int ( math.sqrt ( n ) + 1 ) ) :
if ( n % i == 0 ) :
if ( n / i == i ) :
print ( i , end = " " )
else :
print ( i , end = " " )
list.append ( int ( n / i ) )
for i in list... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printDivisors ( int n ) {
vector < int > v ;
for ( int i = 1 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) printf ( "%d " , i ) ;
else {
printf ( "%d " , i ) ;
v . push_back ( n / i ) ;
}
}
}
for ( int i = v . size ( ) - 1 ;
i >= 0... |
cpp-python-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER | cpp-python | bool isPower ( int x , long int y ) {
if ( x == 1 ) return ( y == 1 ) ;
long int pow = 1 ;
while ( pow < y ) pow *= x ;
return ( pow == y ) ;
}
| def isPower ( x , y ) :
if ( x == 1 ) :
return ( y == 1 )
pow = 1
while ( pow < y ) :
pow = pow * x
return ( pow == y )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isPower ( int x , long int y ) {
if ( x == 1 ) return ( y == 1 ) ;
long int pow = 1 ;
while ( pow < y ) pow *= x ;
return ( pow == y ) ;
}
</Code> |
cpp-python-MAXIMUM_SUM_SUBSEQUENCE_LEAST_K_DISTANT_ELEMENTS | cpp-python | int maxSum ( int arr [ ] , int N , int k ) {
int MS [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ;
}
return MS [ 0 ] ;
}
| def maxSum ( arr , N , k ) :
MS = [ 0 for i in range ( N ) ]
MS [ N - 1 ] = arr [ N - 1 ]
for i in range ( N - 2 , - 1 , - 1 ) :
if ( i + k + 1 >= N ) :
MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] )
else :
MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] )
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxSum ( int arr [ ] , int N , int k ) {
int MS [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ;
}
return MS [ 0 ] ;
}
... |
cpp-python-GROUP_OCCURRENCES_CHARACTERS_ACCORDING_FIRST_APPEARANCE | cpp-python | void printGrouped ( string str ) {
int n = str . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str [ i ] - 'a' ] -- ) cout << str [ i ] ;
count [ str [ i ] -... | def printGrouped ( string ) :
n = len ( string )
count = [ 0 ] * MAX_CHAR
for i in range ( n ) :
count [ ord ( string [ i ] ) - ord ( "a" ) ] += 1
for i in range ( n ) :
while count [ ord ( string [ i ] ) - ord ( "a" ) ] :
print ( string [ i ] , end = "" )
count [... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printGrouped ( string str ) {
int n = str . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str [ i ] - 'a' ] -- ) cout << str [ i ] ;
count [ str ... |
cpp-python-COUNT_SORTED_ROWS_MATRIX | cpp-python | int sortedCount ( int mat [ ] [ MAX ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
... | def sortedCount ( mat , r , c ) :
result = 0
for i in range ( r ) :
j = 0
for j in range ( c - 1 ) :
if mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] :
break
if j == c - 2 :
result += 1
for i in range ( 0 , r ) :
j = 0
for j in range (... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int sortedCount ( int mat [ ] [ MAX ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
i... |
cpp-python-COUNT_DISTINCT_SUBSEQUENCES | cpp-python | int countSub ( string str ) {
vector < int > last ( MAX_CHAR , - 1 ) ;
int n = str . length ( ) ;
int dp [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ str [ i - 1 ] ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ str [ i - 1 ] ] ] ;
last ... | def countSub ( ss ) :
last = [ - 1 for i in range ( MAX_CHAR + 1 ) ]
n = len ( ss )
dp = [ - 2 for i in range ( n + 1 ) ]
dp [ 0 ] = 1
for i in range ( 1 , n + 1 ) :
dp [ i ] = 2 * dp [ i - 1 ]
if last [ ord ( ss [ i - 1 ] ) ] != - 1 :
dp [ i ] = dp [ i ] - dp [ last [ or... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countSub ( string str ) {
vector < int > last ( MAX_CHAR , - 1 ) ;
int n = str . length ( ) ;
int dp [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ str [ i - 1 ] ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ str [ i - 1 ] ] ] ;
... |
cpp-python-LARGEST_INCREASING_SUBSEQUENCE_OF_CONSECUTIVE_INTEGERS | cpp-python | int findLIS ( int A [ ] , int n ) {
unordered_map < int , int > hash ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash [ A [ 0 ] ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ;
if ( LIS_size < hash [ A [ i ] ] ) {
LIS_size = hash [ A [ i ] ] ;
LIS_i... | def findLIS ( A , n ) :
hash = dict ( )
LIS_size , LIS_index = 1 , 0
hash [ A [ 0 ] ] = 1
for i in range ( 1 , n ) :
if A [ i ] - 1 not in hash :
hash [ A [ i ] - 1 ] = 0
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1
if LIS_size < hash [ A [ i ] ] :
LIS_size... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findLIS ( int A [ ] , int n ) {
unordered_map < int , int > hash ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash [ A [ 0 ] ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ;
if ( LIS_size < hash [ A [ i ] ] ) {
LIS_size = hash [ A [ i ] ] ;
... |
cpp-python-FIND_THE_MAXIMUM_OF_MINIMUMS_FOR_EVERY_WINDOW_SIZE_IN_A_GIVEN_ARRAY | cpp-python | void printMaxOfMin ( int arr [ ] , int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = INT_MIN ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}
... | def printMaxOfMin ( arr , n ) :
for k in range ( 1 , n + 1 ) :
maxOfMin = INT_MIN
for i in range ( n - k + 1 ) :
min = arr [ i ]
for j in range ( k ) :
if ( arr [ i + j ] < min ) :
min = arr [ i + j ]
if ( min > maxOfMin ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printMaxOfMin ( int arr [ ] , int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = INT_MIN ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}... |
cpp-python-MAXIMUM_NUMBER_OF_TRAILING_ZEROS_IN_THE_PRODUCT_OF_THE_SUBSETS_OF_SIZE_K | cpp-python | int maximumZeros ( int * arr , int n , int k ) {
int subset [ k + 1 ] [ MAX5 + 5 ] ;
memset ( subset , - 1 , sizeof ( subset ) ) ;
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
pw2 ++ ;
arr [ p ] /= 2 ;
}
while ... | def maximumZeros ( arr , n , k ) :
global MAX5
subset = [ [ - 1 ] * ( MAX5 + 5 ) for _ in range ( k + 1 ) ]
subset [ 0 ] [ 0 ] = 0
for p in arr :
pw2 , pw5 = 0 , 0
while not p % 2 :
pw2 += 1
p //= 2
while not p % 5 :
pw5 += 1
p //= ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maximumZeros ( int * arr , int n , int k ) {
int subset [ k + 1 ] [ MAX5 + 5 ] ;
memset ( subset , - 1 , sizeof ( subset ) ) ;
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
pw2 ++ ;
arr [ p ] /= 2 ;
}
... |
cpp-python-LARGEST_DIVISIBLE_PAIRS_SUBSET | cpp-python | int largestSubset ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int dp [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( a [ j ] % a [ i ] == 0 ) mxm = max ( mxm , dp [ j ] ) ;
dp [ i ] = 1 + mxm ;
}
return * max... | def largestSubset ( a , n ) :
a.sort ( )
dp = [ 0 for i in range ( n ) ]
dp [ n - 1 ] = 1
for i in range ( n - 2 , - 1 , - 1 ) :
mxm = 0
for j in range ( i + 1 , n ) :
if a [ j ] % a [ i ] == 0 :
mxm = max ( mxm , dp [ j ] )
dp [ i ] = 1 + mxm
retu... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int largestSubset ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int dp [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( a [ j ] % a [ i ] == 0 ) mxm = max ( mxm , dp [ j ] ) ;
dp [ i ] = 1 + mxm ;
}
retur... |
cpp-python-FIND_KTH_CHARACTER_OF_DECRYPTED_STRING | cpp-python | char encodedChar ( string str , int k ) {
string expand = "" ;
string temp ;
int freq = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
) {
temp = "" ;
freq = 0 ;
while ( str [ i ] >= 'a' && str [ i ] <= 'z' ) {
temp . push_back ( str [ i ] ) ;
i ++ ;
}
while ( str [ i ] >= '1' && ... | def encodedChar ( str , k ) :
expand = ""
freq = 0
i = 0
while ( i < len ( str ) ) :
temp = ""
freq = 0
while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( 'a' ) and ord ( str [ i ] ) <= ord ( 'z' ) ) :
temp += str [ i ]
i += 1
while ( i < len (... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
char encodedChar ( string str , int k ) {
string expand = "" ;
string temp ;
int freq = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
) {
temp = "" ;
freq = 0 ;
while ( str [ i ] >= 'a' && str [ i ] <= 'z' ) {
temp . push_back ( str [ i ] ) ;
i ++ ;
}
while ( str [ i ] >= ... |
cpp-python-ELEMENTS_TO_BE_ADDED_SO_THAT_ALL_ELEMENTS_OF_A_RANGE_ARE_PRESENT_IN_ARRAY_1 | cpp-python | int countNum ( int arr [ ] , int n ) {
unordered_set < int > s ;
int count = 0 , maxm = INT_MIN , minm = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm = arr [ i ] ;
}
for ( int i = minm ;
i <= m... | def countNum ( arr , n ) :
s = dict ( )
count , maxm , minm = 0 , - 10 ** 9 , 10 ** 9
for i in range ( n ) :
s [ arr [ i ] ] = 1
if ( arr [ i ] < minm ) :
minm = arr [ i ]
if ( arr [ i ] > maxm ) :
maxm = arr [ i ]
for i in range ( minm , maxm + 1 ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countNum ( int arr [ ] , int n ) {
unordered_set < int > s ;
int count = 0 , maxm = INT_MIN , minm = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm = arr [ i ] ;
}
for ( int i = minm ;
... |
cpp-python-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY_1 | cpp-python | int countPairs ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( auto it = mp . begin ( ) ;
it != mp . end ( ) ;
it ++ ) {
int count = it -> second ;
ans += ( count * ( count - 1 ) ) / 2 ;
}
return ans ;
}
| def countPairs ( arr , n ) :
mp = dict ( )
for i in range ( n ) :
if arr [ i ] in mp.keys ( ) :
mp [ arr [ i ] ] += 1
else :
mp [ arr [ i ] ] = 1
ans = 0
for it in mp :
count = mp [ it ]
ans += ( count * ( count - 1 ) ) // 2
return ans
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countPairs ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( auto it = mp . begin ( ) ;
it != mp . end ( ) ;
it ++ ) {
int count = it -> second ;
ans += ( count * ( count - 1 ) ) / 2 ;
}
return ans... |
cpp-python-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY_1 | cpp-python | int maxDiff ( int arr [ ] , int n ) {
int result = 0 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ;
return result ;
}
| def maxDiff ( arr , n ) :
result = 0
arr.sort ( )
for i in range ( n - 1 ) :
if ( abs ( arr [ i ] ) != abs ( arr [ i + 1 ] ) ) :
result += abs ( arr [ i ] )
else :
pass
if ( arr [ n - 2 ] != arr [ n - 1 ] ) :
result += abs ( arr [ n - 1 ] )
return resu... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxDiff ( int arr [ ] , int n ) {
int result = 0 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ;
return result ;
}
<... |
cpp-python-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS | cpp-python | int middleOfThree ( int a , int b , int c ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
| def middleOfThree ( a , b , c ) :
if ( ( a < b and b < c ) or ( c < b and b < a ) ) :
return b
if ( ( b < a and a < c ) or ( c < a and a < b ) ) :
return a
else :
return c
| 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 ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
</Code> |
cpp-python-COUNT_ARRAYS_CONSECUTIVE_ELEMENT_DIFFERENT_VALUES | cpp-python | int countarray ( int n , int k , int x ) {
int dp [ MAXN ] = {
0 }
;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
| def countarray ( n , k , x ) :
dp = list ( )
dp.append ( 0 )
dp.append ( 1 )
i = 2
while i < n :
dp.append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] )
i = i + 1
return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countarray ( int n , int k , int x ) {
int dp [ MAXN ] = {
0 }
;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
</Code> |
cpp-python-NEWMAN_SHANKS_WILLIAMS_PRIME | cpp-python | int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
| def nswp ( n ) :
if n == 0 or n == 1 :
return 1
return 2 * nswp ( n - 1 ) + nswp ( n - 2 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
</Code> |
cpp-python-DYNAMIC_PROGRAMMING_SET_1 | cpp-python | int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
| def fib ( n , lookup ) :
if n == 0 or n == 1 :
lookup [ n ] = n
if lookup [ n ] is None :
lookup [ n ] = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup )
return lookup [ n ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
</Code> |
cpp-python-COUNT_NUMBERS_CAN_CONSTRUCTED_USING_TWO_NUMBERS | cpp-python | int countNums ( int n , int x , int y ) {
vector < bool > arr ( n + 1 , false ) ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
if ( i + y <= n )... | def countNums ( n , x , y ) :
arr = [ False for i in range ( n + 2 ) ]
if ( x <= n ) :
arr [ x ] = True
if ( y <= n ) :
arr [ y ] = True
result = 0
for i in range ( min ( x , y ) , n + 1 ) :
if ( arr [ i ] ) :
if ( i + x <= n ) :
arr [ i + x ] = Tr... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countNums ( int n , int x , int y ) {
vector < bool > arr ( n + 1 , false ) ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
if ( i + y... |
cpp-python-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING | cpp-python | int countPS ( string str ) {
int N = str . length ( ) ;
int cps [ N + 1 ] [ N + 1 ] ;
memset ( cps , 0 , sizeof ( cps ) ) ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
if ( str... | def countPS ( str ) :
N = len ( str )
cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ]
for i in range ( N ) :
cps [ i ] [ i ] = 1
for L in range ( 2 , N + 1 ) :
for i in range ( N ) :
k = L + i - 1
if ( k < N ) :
if ( str [ i ] == ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countPS ( string str ) {
int N = str . length ( ) ;
int cps [ N + 1 ] [ N + 1 ] ;
memset ( cps , 0 , sizeof ( cps ) ) ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
i... |
cpp-python-CONVERT_DECIMAL_FRACTION_BINARY_NUMBER | cpp-python | string decimalToBinary ( double num , int k_prec ) {
string binary = "" ;
int Integral = num ;
double fractional = num - Integral ;
while ( Integral ) {
int rem = Integral % 2 ;
binary . push_back ( rem + '0' ) ;
Integral /= 2 ;
}
reverse ( binary . begin ( ) , binary . end ( ) ) ;
binary . pu... | def decimalToBinary ( num , k_prec ) :
binary = ""
Integral = int ( num )
fractional = num - Integral
while ( Integral ) :
rem = Integral % 2
binary += str ( rem )
Integral //= 2
binary = binary [ : : - 1 ]
binary += '.'
while ( k_prec ) :
fractional *= 2
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
string decimalToBinary ( double num , int k_prec ) {
string binary = "" ;
int Integral = num ;
double fractional = num - Integral ;
while ( Integral ) {
int rem = Integral % 2 ;
binary . push_back ( rem + '0' ) ;
Integral /= 2 ;
}
reverse ( binary . begin ( ) , binary . end ( ) ) ;
bina... |
cpp-python-DYNAMIC_PROGRAMMING_SET_37_BOOLEAN_PARENTHESIZATION_PROBLEM | cpp-python | int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ n ] [ n ] , T [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == 'F' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == 'T' ) ? 1 : 0 ;
}
for ( int gap = 1 ;
gap < n ;
++ gap ) {
for ( int i = 0 , j =... | def countParenth ( symb , oper , n ) :
F = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
for i in range ( n ) :
if symb [ i ] == 'F' :
F [ i ] [ i ] = 1
else :
F [ i ] [ i ] = 0
if s... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ n ] [ n ] , T [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == 'F' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == 'T' ) ? 1 : 0 ;
}
for ( int gap = 1 ;
gap < n ;
++ gap ) {
for ( int i = ... |
cpp-python-COUNT_SET_BITS_IN_AN_INTEGER_1 | cpp-python | int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
| def countSetBits ( n ) :
if ( n == 0 ) :
return 0
else :
return ( n & 1 ) + countSetBits ( n >> 1 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
</Code> |
cpp-python-LONGEST_SPAN_SUM_TWO_BINARY_ARRAYS | cpp-python | int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;
... | def longestCommonSum ( arr1 , arr2 , n ) :
maxLen = 0
for i in range ( 0 , n ) :
sum1 = 0
sum2 = 0
for j in range ( i , n ) :
sum1 += arr1 [ j ]
sum2 += arr2 [ j ]
if ( sum1 == sum2 ) :
len = j - i + 1
if ( len > maxLen ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;... |
cpp-python-MAXIMUM_TRIPLET_SUM_ARRAY | cpp-python | int maxTripletSum ( int arr [ ] , int n ) {
int sum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
| def maxTripletSum ( arr , n ) :
sm = - 1000000
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
for k in range ( j + 1 , n ) :
if ( sm < ( arr [ i ] + arr [ j ] + arr [ k ] ) ) :
sm = arr [ i ] + arr [ j ] + arr [ k ]
return sm
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int maxTripletSum ( int arr [ ] , int n ) {
int sum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
</Code> |
cpp-python-EVALUATE_A_BOOLEAN_EXPRESSION_REPRESENTED_AS_STRING | cpp-python | int evaluateBoolExpr ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( s [ i + 1 ] == 'A' ) {
if ( s [ i + 2 ] == '0' || s [ i ] == '0' ) s [ i + 2 ] = '0' ;
else s [ i + 2 ] = '1' ;
}
else if ( s [ i + 1 ] == 'B' ) {
if ( s [ i + 2 ] == '1' || s [ i... | def evaluateBoolExpr ( s ) :
n = len ( s )
for i in range ( 0 , n - 2 , 2 ) :
if ( s [ i + 1 ] == "A" ) :
if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) :
s [ i + 2 ] = "0"
else :
s [ i + 2 ] = "1"
elif ( s [ i + 1 ] == "B" ) :
if ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int evaluateBoolExpr ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( s [ i + 1 ] == 'A' ) {
if ( s [ i + 2 ] == '0' || s [ i ] == '0' ) s [ i + 2 ] = '0' ;
else s [ i + 2 ] = '1' ;
}
else if ( s [ i + 1 ] == 'B' ) {
if ( s [ i + 2 ] == '1' |... |
cpp-python-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN_2 | cpp-python | int findNth ( int n ) {
int nthElement = 19 + ( n - 1 ) * 9 ;
int outliersCount = ( int ) log10 ( nthElement ) - 1 ;
nthElement += 9 * outliersCount ;
return nthElement ;
}
| def findNth ( n ) :
nthElement = 19 + ( n - 1 ) * 9
outliersCount = int ( math.log10 ( nthElement ) ) - 1
nthElement += 9 * outliersCount
return nthElement
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findNth ( int n ) {
int nthElement = 19 + ( n - 1 ) * 9 ;
int outliersCount = ( int ) log10 ( nthElement ) - 1 ;
nthElement += 9 * outliersCount ;
return nthElement ;
}
</Code> |
cpp-python-COUNT_ARITHMETIC_PROGRESSION_SUBSEQUENCES_ARRAY | cpp-python | int numofAP ( int a [ ] , int n ) {
int minarr = INT_MAX , maxarr = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
minarr = min ( minarr , a [ i ] ) ;
maxarr = max ( maxarr , a [ i ] ) ;
}
int dp [ n ] , sum [ MAX ] ;
int ans = n + 1 ;
for ( int d = ( minarr - maxarr ) ;
d <= ( maxarr - minarr )... | def numofAP ( a , n ) :
minarr = + 2147483647
maxarr = - 2147483648
for i in range ( n ) :
minarr = min ( minarr , a [ i ] )
maxarr = max ( maxarr , a [ i ] )
dp = [ 0 for i in range ( n + 1 ) ]
ans = n + 1
for d in range ( ( minarr - maxarr ) , ( maxarr - minarr ) + 1 ) :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int numofAP ( int a [ ] , int n ) {
int minarr = INT_MAX , maxarr = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
minarr = min ( minarr , a [ i ] ) ;
maxarr = max ( maxarr , a [ i ] ) ;
}
int dp [ n ] , sum [ MAX ] ;
int ans = n + 1 ;
for ( int d = ( minarr - maxarr ) ;
d <= ( maxarr - m... |
cpp-python-FIND_NUMBER_OF_SOLUTIONS_OF_A_LINEAR_EQUATION_OF_N_VARIABLES | cpp-python | int countSol ( int coeff [ ] , int start , int end , int rhs ) {
if ( rhs == 0 ) return 1 ;
int result = 0 ;
for ( int i = start ;
i <= end ;
i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ;
return result ;
}
| def countSol ( coeff , start , end , rhs ) :
if ( rhs == 0 ) :
return 1
result = 0
for i in range ( start , end + 1 ) :
if ( coeff [ i ] <= rhs ) :
result += countSol ( coeff , i , end , rhs - coeff [ i ] )
return result
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countSol ( int coeff [ ] , int start , int end , int rhs ) {
if ( rhs == 0 ) return 1 ;
int result = 0 ;
for ( int i = start ;
i <= end ;
i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ;
return result ;
}
</Code> |
cpp-python-MINIMUM_NUMBER_SWAPS_REQUIRED_SORT_ARRAY | cpp-python | int minSwaps ( int arr [ ] , int n ) {
pair < int , int > arrPos [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arrPos [ i ] . first = arr [ i ] ;
arrPos [ i ] . second = i ;
}
sort ( arrPos , arrPos + n ) ;
vector < bool > vis ( n , false ) ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
... | def minSwaps ( arr ) :
n = len ( arr )
arrpos = [ * enumerate ( arr ) ]
arrpos.sort ( key = lambda it : it [ 1 ] )
vis = { k : False for k in range ( n ) }
ans = 0
for i in range ( n ) :
if vis [ i ] or arrpos [ i ] [ 0 ] == i :
continue
cycle_size = 0
j = i
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minSwaps ( int arr [ ] , int n ) {
pair < int , int > arrPos [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arrPos [ i ] . first = arr [ i ] ;
arrPos [ i ] . second = i ;
}
sort ( arrPos , arrPos + n ) ;
vector < bool > vis ( n , false ) ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i +... |
cpp-python-CIRCULAR_MATRIX_CONSTRUCT_A_MATRIX_WITH_NUMBERS_1_TO_MN_IN_SPIRAL_WAY | cpp-python | void spiralFill ( int m , int n , int a [ ] [ MAX ] ) {
int val = 1 ;
int k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( int i = l ;
i < n ;
++ i ) a [ k ] [ i ] = val ++ ;
k ++ ;
for ( int i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = val ++ ;
n -- ;
if ( k < m ) {
for ( ... | def spiralFill ( m , n , a ) :
val = 1
k , l = 0 , 0
while ( k < m and l < n ) :
for i in range ( l , n ) :
a [ k ] [ i ] = val
val += 1
k += 1
for i in range ( k , m ) :
a [ i ] [ n - 1 ] = val
val += 1
n -= 1
if ( k < ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void spiralFill ( int m , int n , int a [ ] [ MAX ] ) {
int val = 1 ;
int k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( int i = l ;
i < n ;
++ i ) a [ k ] [ i ] = val ++ ;
k ++ ;
for ( int i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = val ++ ;
n -- ;
if ( k < m ) {
... |
cpp-python-GIVEN_TWO_SORTED_ARRAYS_NUMBER_X_FIND_PAIR_WHOSE_SUM_CLOSEST_X | cpp-python | void printClosest ( int ar1 [ ] , int ar2 [ ] , int m , int n , int x ) {
int diff = INT_MAX ;
int res_l , res_r ;
int l = 0 , r = n - 1 ;
while ( l < m && r >= 0 ) {
if ( abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( ar1 [ l ] + ar2 [ r ] - x ) ;
}
... | def printClosest ( ar1 , ar2 , m , n , x ) :
diff = sys.maxsize
l = 0
r = n - 1
while ( l < m and r >= 0 ) :
if abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff :
res_l = l
res_r = r
diff = abs ( ar1 [ l ] + ar2 [ r ] - x )
if ar1 [ l ] + ar2 [ r ] > x :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printClosest ( int ar1 [ ] , int ar2 [ ] , int m , int n , int x ) {
int diff = INT_MAX ;
int res_l , res_r ;
int l = 0 , r = n - 1 ;
while ( l < m && r >= 0 ) {
if ( abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( ar1 [ l ] + ar2 [ r ] - x ) ;... |
cpp-python-NUMBER_VISIBLE_BOXES_PUTTING_ONE_INSIDE_ANOTHER | cpp-python | int minimumBox ( int arr [ ] , int n ) {
queue < int > q ;
sort ( arr , arr + n ) ;
q . push ( arr [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int now = q . front ( ) ;
if ( arr [ i ] >= 2 * now ) q . pop ( ) ;
q . push ( arr [ i ] ) ;
}
return q . size ( ) ;
}
| def minimumBox ( arr , n ) :
q = collections.deque ( [ ] )
arr.sort ( )
q.append ( arr [ 0 ] )
for i in range ( 1 , n ) :
now = q [ 0 ]
if ( arr [ i ] >= 2 * now ) :
q.popleft ( )
q.append ( arr [ i ] )
return len ( q )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minimumBox ( int arr [ ] , int n ) {
queue < int > q ;
sort ( arr , arr + n ) ;
q . push ( arr [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int now = q . front ( ) ;
if ( arr [ i ] >= 2 * now ) q . pop ( ) ;
q . push ( arr [ i ] ) ;
}
return q . size ( ) ;
}
</Code> |
cpp-python-INTEGER_POSITIVE_VALUE_POSITIVE_NEGATIVE_VALUE_ARRAY | cpp-python | int findInteger ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
int maximum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] < 0 ) hash [ abs ( arr [ i ] ) ] -= 1 ;
else hash [ arr [ i ] ] += 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) if ( hash [ arr [ i ] ] != 0 ) return arr [... | def findInteger ( arr , n ) :
hash = dict ( )
maximum = 0
for i in arr :
if ( i < 0 ) :
if abs ( i ) not in hash.keys ( ) :
hash [ abs ( i ) ] = - 1
else :
hash [ abs ( i ) ] -= 1
else :
hash [ i ] = hash.get ( i , 0 ) + 1
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findInteger ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
int maximum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] < 0 ) hash [ abs ( arr [ i ] ) ] -= 1 ;
else hash [ arr [ i ] ] += 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) if ( hash [ arr [ i ] ] != 0 ) retur... |
cpp-python-CONVERT_ALTERNATE_CHARACTERS_STRING_UPPER_CASE | cpp-python | void convertOpposite ( string & str ) {
int ln = str . length ( ) ;
for ( int i = 0 ;
i < ln ;
i ++ ) {
if ( str [ i ] >= 'a' && str [ i ] <= 'z' ) str [ i ] = str [ i ] - 32 ;
else if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) str [ i ] = str [ i ] + 32 ;
}
}
| def convertOpposite ( str ) :
ln = len ( str )
for i in range ( ln ) :
if str [ i ] >= 'a' and str [ i ] <= 'z' :
str [ i ] = chr ( ord ( str [ i ] ) - 32 )
elif str [ i ] >= 'A' and str [ i ] <= 'Z' :
str [ i ] = chr ( ord ( str [ i ] ) + 32 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void convertOpposite ( string & str ) {
int ln = str . length ( ) ;
for ( int i = 0 ;
i < ln ;
i ++ ) {
if ( str [ i ] >= 'a' && str [ i ] <= 'z' ) str [ i ] = str [ i ] - 32 ;
else if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) str [ i ] = str [ i ] + 32 ;
}
}
</Code> |
cpp-python-PROGRAM_DECIMAL_BINARY_CONVERSION_2 | cpp-python | unsigned long long int int decimalToBinary ( int N ) {
ull B_Number = 0 ;
int cnt = 0 ;
while ( N != 0 ) {
int rem = N % 2 ;
ull c = pow ( 10 , cnt ) ;
B_Number += rem * c ;
N /= 2 ;
cnt ++ ;
}
return B_Number ;
}
| def decimalToBinary ( N ) :
B_Number = 0
cnt = 0
while ( N != 0 ) :
rem = N % 2
c = pow ( 10 , cnt )
B_Number += rem * c
N //= 2
cnt += 1
return B_Number
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
unsigned long long int int decimalToBinary ( int N ) {
ull B_Number = 0 ;
int cnt = 0 ;
while ( N != 0 ) {
int rem = N % 2 ;
ull c = pow ( 10 , cnt ) ;
B_Number += rem * c ;
N /= 2 ;
cnt ++ ;
}
return B_Number ;
}
</Code> |
cpp-python-FIND_REPETITIVE_ELEMENT_1_N_1_1 | cpp-python | int findRepeating ( int arr [ ] , int n ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s . find ( arr [ i ] ) != s . end ( ) ) return arr [ i ] ;
s . insert ( arr [ i ] ) ;
}
return - 1 ;
}
| def findRepeating ( arr , n ) :
s = set ( )
for i in range ( n ) :
if arr [ i ] in s :
return arr [ i ]
s.add ( arr [ i ] )
rteurn - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findRepeating ( int arr [ ] , int n ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s . find ( arr [ i ] ) != s . end ( ) ) return arr [ i ] ;
s . insert ( arr [ i ] ) ;
}
return - 1 ;
}
</Code> |
cpp-python-PROGRAM_TO_FIND_TRANSPOSE_OF_A_MATRIX_2 | cpp-python | void transpose ( int A [ ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) swap ( A [ i ] [ j ] , A [ j ] [ i ] ) ;
}
| def transpose ( A ) :
for i in range ( N ) :
for j in range ( i + 1 , N ) :
A [ i ] [ j ] , A [ j ] [ i ] = A [ j ] [ i ] , A [ i ] [ j ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void transpose ( int A [ ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) swap ( A [ i ] [ j ] , A [ j ] [ i ] ) ;
}
</Code> |
cpp-python-PRINT_POSSIBLE_SUMS_CONSECUTIVE_NUMBERS_SUM_N | cpp-python | void findConsecutive ( int N ) {
int start = 1 , end = ( N + 1 ) / 2 ;
while ( start < end ) {
int sum = 0 ;
for ( int i = start ;
i <= end ;
i ++ ) {
sum = sum + i ;
if ( sum == N ) {
for ( int j = start ;
j <= i ;
j ++ ) printf ( "%d " , j ) ;
printf ( "... | def findConsecutive ( N ) :
start = 1
end = ( N + 1 ) // 2
while ( start < end ) :
Sum = 0
for i in range ( start , end + 1 ) :
Sum = Sum + i
if ( Sum == N ) :
for j in range ( start , i + 1 ) :
print ( j , end = " " )
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void findConsecutive ( int N ) {
int start = 1 , end = ( N + 1 ) / 2 ;
while ( start < end ) {
int sum = 0 ;
for ( int i = start ;
i <= end ;
i ++ ) {
sum = sum + i ;
if ( sum == N ) {
for ( int j = start ;
j <= i ;
j ++ ) printf ( "%d " , j ) ;
pri... |
cpp-python-PRINT_POSSIBLE_STRINGS_CAN_MADE_PLACING_SPACES_2 | cpp-python | void printSubsequences ( string str ) {
int n = str . length ( ) ;
unsigned int opsize = pow ( 2 , n - 1 ) ;
for ( int counter = 0 ;
counter < opsize ;
counter ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
cout << str [ j ] ;
if ( counter & ( 1 << j ) ) cout << " " ;
}
cout << endl... | def printSubsequences ( str ) :
n = len ( str )
opsize = int ( pow ( 2 , n - 1 ) )
for counter in range ( opsize ) :
for j in range ( n ) :
print ( str [ j ] , end = "" )
if ( counter & ( 1 << j ) ) :
print ( "" , end = "" )
print ( "\n" , end = "" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printSubsequences ( string str ) {
int n = str . length ( ) ;
unsigned int opsize = pow ( 2 , n - 1 ) ;
for ( int counter = 0 ;
counter < opsize ;
counter ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
cout << str [ j ] ;
if ( counter & ( 1 << j ) ) cout << " " ;
}
cout ... |
cpp-python-MAXIMUM_AREA_QUADRILATERAL | cpp-python | double maxArea ( double a , double b , double c , double d ) {
double semiperimeter = ( a + b + c + d ) / 2 ;
return sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ;
}
| def maxArea ( a , b , c , d ) :
semiperimeter = ( a + b + c + d ) / 2
return math.sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
double maxArea ( double a , double b , double c , double d ) {
double semiperimeter = ( a + b + c + d ) / 2 ;
return sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ;
}
</Code> |
cpp-python-POSITION_OF_RIGHTMOST_SET_BIT_2 | cpp-python | int Right_most_setbit ( int num ) {
int pos = 1 ;
for ( int i = 0 ;
i < INT_SIZE ;
i ++ ) {
if ( ! ( num & ( 1 << i ) ) ) pos ++ ;
else break ;
}
return pos ;
}
| def Right_most_setbit ( num ) :
pos = 1
for i in range ( INT_SIZE ) :
if not ( num & ( 1 << i ) ) :
pos += 1
else :
break
return pos
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int Right_most_setbit ( int num ) {
int pos = 1 ;
for ( int i = 0 ;
i < INT_SIZE ;
i ++ ) {
if ( ! ( num & ( 1 << i ) ) ) pos ++ ;
else break ;
}
return pos ;
}
</Code> |
cpp-python-CONVERTING_DECIMAL_NUMBER_LYING_BETWEEN_1_TO_3999_TO_ROMAN_NUMERALS | cpp-python | int printRoman ( int number ) {
int num [ ] = {
1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 }
;
string sym [ ] = {
"I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" }
;
int i = 12 ;
while ( number > 0 ) {
int div = number... | def printRoman ( number ) :
num = [ 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 ]
sym = [ "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" ]
i = 12
while number :
div = number // num [ i ]
number %= num [ i ]
while div :
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int printRoman ( int number ) {
int num [ ] = {
1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 }
;
string sym [ ] = {
"I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" }
;
int i = 12 ;
while ( number > 0 ) {
int div =... |
cpp-python-DYNAMIC_PROGRAMMING_SUBSET_SUM_PROBLEM | cpp-python | bool isSubsetSum ( int set [ ] , int n , int sum ) {
if ( sum == 0 ) return true ;
if ( n == 0 && sum != 0 ) return false ;
if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ;
return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ;
}
| def isSubsetSum ( set , n , sum ) :
if ( sum == 0 ) :
return True
if ( n == 0 and sum != 0 ) :
return False
if ( set [ n - 1 ] > sum ) :
return isSubsetSum ( set , n - 1 , sum )
return isSubsetSum ( set , n - 1 , sum ) or isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isSubsetSum ( int set [ ] , int n , int sum ) {
if ( sum == 0 ) return true ;
if ( n == 0 && sum != 0 ) return false ;
if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ;
return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ;
}
</Code> |
cpp-python-NUMBER_DIGITS_PRODUCT_TWO_NUMBERS_1 | cpp-python | int countDigits ( int a , int b ) {
if ( a == 0 || b == 0 ) return 1 ;
return floor ( log10 ( abs ( a ) ) + log10 ( abs ( b ) ) ) + 1 ;
}
| def countDigits ( a , b ) :
if ( a == 0 or b == 0 ) :
return 1
return math.floor ( math.log10 ( abs ( a ) ) + math.log10 ( abs ( b ) ) ) + 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countDigits ( int a , int b ) {
if ( a == 0 || b == 0 ) return 1 ;
return floor ( log10 ( abs ( a ) ) + log10 ( abs ( b ) ) ) + 1 ;
}
</Code> |
cpp-python-WAYS_TO_WRITE_N_AS_SUM_OF_TWO_OR_MORE_POSITIVE_INTEGERS | cpp-python | int countWays ( int n ) {
int table [ n + 1 ] ;
memset ( table , 0 , sizeof ( table ) ) ;
table [ 0 ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = i ;
j <= n ;
j ++ ) table [ j ] += table [ j - i ] ;
return table [ n ] ;
}
| def CountWays ( n ) :
table = [ 0 ] * ( n + 1 )
table [ 0 ] = 1
for i in range ( 1 , n ) :
for j in range ( i , n + 1 ) :
table [ j ] += table [ j - i ]
return table [ n ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countWays ( int n ) {
int table [ n + 1 ] ;
memset ( table , 0 , sizeof ( table ) ) ;
table [ 0 ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = i ;
j <= n ;
j ++ ) table [ j ] += table [ j - i ] ;
return table [ n ] ;
}
</Code> |
cpp-python-SORT_EVEN_NUMBERS_ASCENDING_ORDER_SORT_ODD_NUMBERS_DESCENDING_ORDER_1 | cpp-python | void twoWaySort ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ;
}
| def twoWaySort ( arr , n ) :
for i in range ( 0 , n ) :
if ( arr [ i ] & 1 ) :
arr [ i ] *= - 1
arr.sort ( )
for i in range ( 0 , n ) :
if ( arr [ i ] & 1 ) :
arr [ i ] *= - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void twoWaySort ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ;
}
</Code> |
cpp-python-FINDING_POWER_PRIME_NUMBER_P_N_1 | cpp-python | int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
int temp = p ;
while ( temp <= n ) {
ans += n / temp ;
temp = temp * p ;
}
return ans ;
}
| def PowerOFPINnfactorial ( n , p ) :
ans = 0
temp = p
while ( temp <= n ) :
ans += n / temp
temp = temp * p
return int ( ans )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
int temp = p ;
while ( temp <= n ) {
ans += n / temp ;
temp = temp * p ;
}
return ans ;
}
</Code> |
cpp-python-FIND_A_SPECIFIC_PAIR_IN_MATRIX | cpp-python | int findMaxValue ( int mat [ ] [ N ] ) {
int maxValue = INT_MIN ;
for ( int a = 0 ;
a < N - 1 ;
a ++ ) for ( int b = 0 ;
b < N - 1 ;
b ++ ) for ( int d = a + 1 ;
d < N ;
d ++ ) for ( int e = b + 1 ;
e < N ;
e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxValue = mat [ d ] [ e ] - m... | def findMaxValue ( mat ) :
maxValue = 0
for a in range ( N - 1 ) :
for b in range ( N - 1 ) :
for d in range ( a + 1 , N ) :
for e in range ( b + 1 , N ) :
if maxValue < int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) :
maxValue = int ( m... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findMaxValue ( int mat [ ] [ N ] ) {
int maxValue = INT_MIN ;
for ( int a = 0 ;
a < N - 1 ;
a ++ ) for ( int b = 0 ;
b < N - 1 ;
b ++ ) for ( int d = a + 1 ;
d < N ;
d ++ ) for ( int e = b + 1 ;
e < N ;
e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxValue = mat [ d ] [ ... |
cpp-python-FIND_THREE_ELEMENT_FROM_DIFFERENT_THREE_ARRAYS_SUCH_THAT_THAT_A_B_C_K_1 | cpp-python | bool findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n1 ;
i ++ ) s . insert ( a1 [ i ] ) ;
for ( int i = 0 ;
i < n2 ;
i ++ ) {
for ( int j = 0 ;
j < n3 ;
j ++ ) {
if ( s . find ( sum - a2 [ i ] - ... | def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) :
s = set ( )
for i in range ( n1 ) :
s.add ( a1 [ i ] )
for i in range ( n2 ) :
for j in range ( n3 ) :
if sum - a2 [ i ] - a3 [ j ] in s :
return True
return False
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n1 ;
i ++ ) s . insert ( a1 [ i ] ) ;
for ( int i = 0 ;
i < n2 ;
i ++ ) {
for ( int j = 0 ;
j < n3 ;
j ++ ) {
if ( s . find ( sum - a2 [... |
cpp-python-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES | cpp-python | int getOddOccurrence ( int arr [ ] , int arr_size ) {
for ( int i = 0 ;
i < arr_size ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < arr_size ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count % 2 != 0 ) return arr [ i ] ;
}
return - 1 ;
}
| def getOddOccurrence ( arr , arr_size ) :
for i in range ( 0 , arr_size ) :
count = 0
for j in range ( 0 , arr_size ) :
if arr [ i ] == arr [ j ] :
count += 1
if ( count % 2 != 0 ) :
return arr [ i ]
return - 1
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int getOddOccurrence ( int arr [ ] , int arr_size ) {
for ( int i = 0 ;
i < arr_size ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < arr_size ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count % 2 != 0 ) return arr [ i ] ;
}
return - 1 ;
}
</Code> |
cpp-python-PRIME_FACTORS_BIG_NUMBER | cpp-python | void factorize ( long long n ) {
int count = 0 ;
while ( ! ( n % 2 ) ) {
n >>= 1 ;
count ++ ;
}
if ( count ) cout << 2 << " " << count << endl ;
for ( long long i = 3 ;
i <= sqrt ( n ) ;
i += 2 ) {
count = 0 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
}
if ( count ... | def factorize ( n ) :
count = 0
while ( ( n % 2 > 0 ) == False ) :
n >>= 1
count += 1
if ( count > 0 ) :
print ( 2 , count )
for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 ) :
count = 0
while ( n % i == 0 ) :
count += 1
n = int ( n / i )
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void factorize ( long long n ) {
int count = 0 ;
while ( ! ( n % 2 ) ) {
n >>= 1 ;
count ++ ;
}
if ( count ) cout << 2 << " " << count << endl ;
for ( long long i = 3 ;
i <= sqrt ( n ) ;
i += 2 ) {
count = 0 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
}
if (... |
cpp-python-SMALLEST_DIFFERENCE_PAIR_VALUES_TWO_UNSORTED_ARRAYS | cpp-python | int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) {
sort ( A , A + m ) ;
sort ( B , B + n ) ;
int a = 0 , b = 0 ;
int result = INT_MAX ;
while ( a < m && b < n ) {
if ( abs ( A [ a ] - B [ b ] ) < result ) result = abs ( A [ a ] - B [ b ] ) ;
if ( A [ a ] < B [ b ] ) a ++ ;
else... | def findSmallestDifference ( A , B , m , n ) :
A.sort ( )
B.sort ( )
a = 0
b = 0
result = sys.maxsize
while ( a < m and b < n ) :
if ( abs ( A [ a ] - B [ b ] ) < result ) :
result = abs ( A [ a ] - B [ b ] )
if ( A [ a ] < B [ b ] ) :
a += 1
else ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) {
sort ( A , A + m ) ;
sort ( B , B + n ) ;
int a = 0 , b = 0 ;
int result = INT_MAX ;
while ( a < m && b < n ) {
if ( abs ( A [ a ] - B [ b ] ) < result ) result = abs ( A [ a ] - B [ b ] ) ;
if ( A [ a ] < B [ b ] ) a ++ ;
... |
cpp-python-CHECK_GIVEN_STRING_CAN_SPLIT_FOUR_DISTINCT_STRINGS | cpp-python | bool check ( string s ) {
if ( s . size ( ) >= 10 ) return true ;
for ( int i = 1 ;
i < s . size ( ) ;
i ++ ) {
for ( int j = i + 1 ;
j < s . size ( ) ;
j ++ ) {
for ( int k = j + 1 ;
k < s . size ( ) ;
k ++ ) {
string s1 = s . substr ( 0 , i ) ;
string s2 = s . sub... | def check ( s ) :
if ( len ( s ) >= 10 ) :
return True
for i in range ( 1 , len ( s ) ) :
for j in range ( i + 1 , len ( s ) ) :
for k in range ( j + 1 , len ( s ) ) :
s1 = s [ 0 : i ]
s2 = s [ i : j - i ]
s3 = s [ j : k - j ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool check ( string s ) {
if ( s . size ( ) >= 10 ) return true ;
for ( int i = 1 ;
i < s . size ( ) ;
i ++ ) {
for ( int j = i + 1 ;
j < s . size ( ) ;
j ++ ) {
for ( int k = j + 1 ;
k < s . size ( ) ;
k ++ ) {
string s1 = s . substr ( 0 , i ) ;
string s2 = ... |
cpp-python-CHECK_POSSIBLE_PATH_2D_MATRIX | cpp-python | bool isPath ( int arr [ row ] [ col ] ) {
arr [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i < row ;
i ++ ) if ( arr [ i ] [ 0 ] != - 1 ) arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] ;
for ( int j = 1 ;
j < col ;
j ++ ) if ( arr [ 0 ] [ j ] != - 1 ) arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] ;
for ( int i = 1 ;
i < row ;
... | def isPath ( arr ) :
arr [ 0 ] [ 0 ] = 1
for i in range ( 1 , row ) :
if ( arr [ i ] [ 0 ] != - 1 ) :
arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ]
for j in range ( 1 , col ) :
if ( arr [ 0 ] [ j ] != - 1 ) :
arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ]
for i in range ( 1 , row ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
bool isPath ( int arr [ row ] [ col ] ) {
arr [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i < row ;
i ++ ) if ( arr [ i ] [ 0 ] != - 1 ) arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] ;
for ( int j = 1 ;
j < col ;
j ++ ) if ( arr [ 0 ] [ j ] != - 1 ) arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] ;
for ( int i = 1 ;
i <... |
cpp-python-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER_1 | cpp-python | void bitonicGenerator ( int arr [ ] , int n ) {
int i = 1 ;
int j = n - 1 ;
if ( j % 2 != 0 ) j -- ;
while ( i < j ) {
swap ( arr [ i ] , arr [ j ] ) ;
i += 2 ;
j -= 2 ;
}
sort ( arr , arr + ( n + 1 ) / 2 ) ;
sort ( arr + ( n + 1 ) / 2 , arr + n , greater < int > ( ) ) ;
}
| def bitonicGenerator ( arr , n ) :
i = 1
j = n - 1
if ( j % 2 != 0 ) :
j = j - 1
while ( i < j ) :
arr [ j ] , arr [ i ] = arr [ i ] , arr [ j ]
i = i + 2
j = j - 2
arr_f = [ ]
arr_s = [ ]
for i in range ( int ( ( n + 1 ) / 2 ) ) :
arr_f.append ( arr [... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void bitonicGenerator ( int arr [ ] , int n ) {
int i = 1 ;
int j = n - 1 ;
if ( j % 2 != 0 ) j -- ;
while ( i < j ) {
swap ( arr [ i ] , arr [ j ] ) ;
i += 2 ;
j -= 2 ;
}
sort ( arr , arr + ( n + 1 ) / 2 ) ;
sort ( arr + ( n + 1 ) / 2 , arr + n , greater < int > ( ) ) ;
}
</Code> |
cpp-python-REMOVE_ARRAY_END_ELEMENT_MAXIMIZE_SUM_PRODUCT | cpp-python | int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) {
if ( low == high ) return a [ low ] * turn ;
if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ;
dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a ... | def solve ( dp , a , low , high , turn ) :
if ( low == high ) :
return a [ low ] * turn
if ( dp [ low ] [ high ] != 0 ) :
return dp [ low ] [ high ]
dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high -... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) {
if ( low == high ) return a [ low ] * turn ;
if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ;
dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( ... |
cpp-python-DIRECTION_LAST_SQUARE_BLOCK | cpp-python | void direction ( ll R , ll C ) {
if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) {
cout << "Left" << endl ;
return ;
}
if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) {
cout << "Up" << endl ;
return ;
}
if ( R == C && R % 2 != 0 && C % 2 != 0 ) {
cout << "Right" << endl ;
return ... | def direction ( R , C ) :
if ( R != C and R % 2 == 0 and C % 2 != 0 and R < C ) :
print ( "Left" )
return
if ( R != C and R % 2 == 0 and C % 2 == 0 and R > C ) :
print ( "Up" )
return
if R == C and R % 2 != 0 and C % 2 != 0 :
print ( "Right" )
return
if R ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void direction ( ll R , ll C ) {
if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) {
cout << "Left" << endl ;
return ;
}
if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) {
cout << "Up" << endl ;
return ;
}
if ( R == C && R % 2 != 0 && C % 2 != 0 ) {
cout << "Right" << endl ;
... |
cpp-python-SPLIT_NUMERIC_ALPHABETIC_AND_SPECIAL_SYMBOLS_FROM_A_STRING | cpp-python | void splitString ( string str ) {
string alpha , num , special ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( isdigit ( str [ i ] ) ) num . push_back ( str [ i ] ) ;
else if ( ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) || ( str [ i ] >= 'a' && str [ i ] <= 'z' ) ) alpha . push_back ( str [ i ] )... | def splitString ( str ) :
alpha = ""
num = ""
special = ""
for i in range ( len ( str ) ) :
if ( str [ i ].isdigit ( ) ) :
num = num + str [ i ]
elif ( ( str [ i ] >= 'A' and str [ i ] <= 'Z' ) or ( str [ i ] >= 'a' and str [ i ] <= 'z' ) ) :
alpha += str [ i ]
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void splitString ( string str ) {
string alpha , num , special ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( isdigit ( str [ i ] ) ) num . push_back ( str [ i ] ) ;
else if ( ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) || ( str [ i ] >= 'a' && str [ i ] <= 'z' ) ) alpha . push_back ( str ... |
cpp-python-FIND_MEDIAN_ROW_WISE_SORTED_MATRIX | cpp-python | int binaryMedian ( int m [ ] [ MAX ] , int r , int c ) {
int min = INT_MAX , max = INT_MIN ;
for ( int i = 0 ;
i < r ;
i ++ ) {
if ( m [ i ] [ 0 ] < min ) min = m [ i ] [ 0 ] ;
if ( m [ i ] [ c - 1 ] > max ) max = m [ i ] [ c - 1 ] ;
}
int desired = ( r * c + 1 ) / 2 ;
while ( min < max ) {
in... | def binaryMedian ( m , r , d ) :
mi = m [ 0 ] [ 0 ]
mx = 0
for i in range ( r ) :
if m [ i ] [ 0 ] < mi :
mi = m [ i ] [ 0 ]
if m [ i ] [ d - 1 ] > mx :
mx = m [ i ] [ d - 1 ]
desired = ( r * d + 1 ) // 2
while ( mi < mx ) :
mid = mi + ( mx - mi ) // 2... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int binaryMedian ( int m [ ] [ MAX ] , int r , int c ) {
int min = INT_MAX , max = INT_MIN ;
for ( int i = 0 ;
i < r ;
i ++ ) {
if ( m [ i ] [ 0 ] < min ) min = m [ i ] [ 0 ] ;
if ( m [ i ] [ c - 1 ] > max ) max = m [ i ] [ c - 1 ] ;
}
int desired = ( r * c + 1 ) / 2 ;
while ( min < max ) {... |
cpp-python-FIND_DUPLICATES_GIVEN_ARRAY_ELEMENTS_NOT_LIMITED_RANGE | cpp-python | void printDuplicates ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] ++ ;
bool dup = false ;
unordered_map < int , int > :: iterator itr ;
for ( itr = freq . begin ( ) ;
itr != freq . end ( ) ;
itr ++ ) {
if ( itr -> second > 1 ) {
... | def printDuplicates ( arr ) :
dict = { }
for ele in arr :
try :
dict [ ele ] += 1
except :
dict [ ele ] = 1
for item in dict :
if ( dict [ item ] > 1 ) :
print ( item , end = " " )
print ( "\n" )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printDuplicates ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] ++ ;
bool dup = false ;
unordered_map < int , int > :: iterator itr ;
for ( itr = freq . begin ( ) ;
itr != freq . end ( ) ;
itr ++ ) {
if ( itr -> second > ... |
cpp-python-PRINT_FIBONACCI_SEQUENCE_USING_2_VARIABLES | cpp-python | void fib ( int n ) {
int a = 0 , b = 1 , c ;
if ( n >= 0 ) cout << a << " " ;
if ( n >= 1 ) cout << b << " " ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
c = a + b ;
cout << c << " " ;
a = b ;
b = c ;
}
}
| def fib ( n ) :
a = 0
b = 1
if ( n >= 0 ) :
print ( a , end = ' ' )
if ( n >= 1 ) :
print ( b , end = ' ' )
for i in range ( 2 , n + 1 ) :
c = a + b
print ( c , end = ' ' )
a = b
b = c
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void fib ( int n ) {
int a = 0 , b = 1 , c ;
if ( n >= 0 ) cout << a << " " ;
if ( n >= 1 ) cout << b << " " ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
c = a + b ;
cout << c << " " ;
a = b ;
b = c ;
}
}
</Code> |
cpp-python-SWAP_TWO_NIBBLES_BYTE | cpp-python | int swapNibbles ( int x ) {
return ( ( x & 0x0F ) << 4 | def swapNibbles ( x ) :
return ( ( x & 0x0F ) << 4 | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int swapNibbles ( int x ) {
return ( ( x & 0x0F ) << 4
</Code> |
cpp-python-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING_1 | cpp-python | int countPS ( int i , int j ) {
if ( i >= n || j < 0 ) return 0 ;
if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ;
if ( abs ( i - j ) == 1 ) {
if ( str [ i ] == str [ j ] ) return dp [ i ] [ j ] = 3 ;
else return dp [ i ] [ j ] = 2 ;
}
if ( i == j ) return dp [ 1 ] [ j ] = 1 ;
else if ( str [ i ... | def countPS ( i , j ) :
if ( i >= n or j < 0 ) :
return 0
if ( dp [ i ] [ j ] != - 1 ) :
return dp [ i ] [ j ]
if ( abs ( i - j ) == 1 ) :
if ( str [ i ] == str [ j ] ) :
dp [ i ] [ j ] = 3
return dp [ i ] [ j ]
else :
dp [ i ] [ j ] = 2
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int countPS ( int i , int j ) {
if ( i >= n || j < 0 ) return 0 ;
if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ;
if ( abs ( i - j ) == 1 ) {
if ( str [ i ] == str [ j ] ) return dp [ i ] [ j ] = 3 ;
else return dp [ i ] [ j ] = 2 ;
}
if ( i == j ) return dp [ 1 ] [ j ] = 1 ;
else if ( s... |
cpp-python-SUM_DIVISORS_1_N_1 | cpp-python | int divisorSum ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
++ i ) sum += ( n / i ) * i ;
return sum ;
}
| def divisorSum ( n ) :
sum = 0
for i in range ( 1 , n + 1 ) :
sum += int ( n / i ) * i
return int ( sum )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int divisorSum ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
++ i ) sum += ( n / i ) * i ;
return sum ;
}
</Code> |
cpp-python-ADD_1_TO_A_GIVEN_NUMBER | cpp-python | int addOne ( int x ) {
int m = 1 ;
while ( x & m ) {
x = x ^ m ;
m <<= 1 ;
}
x = x ^ m ;
return x ;
}
| def addOne ( x ) :
m = 1
while ( x & m ) :
x = x ^ m
m <<= 1
x = x ^ m
return x
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int addOne ( int x ) {
int m = 1 ;
while ( x & m ) {
x = x ^ m ;
m <<= 1 ;
}
x = x ^ m ;
return x ;
}
</Code> |
cpp-python-CHECK_LARGE_NUMBER_DIVISIBLE_9_NOT | cpp-python | int check ( string str ) {
int n = str . length ( ) ;
int digitSum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) digitSum += ( str [ i ] - '0' ) ;
return ( digitSum % 9 == 0 ) ;
}
| def check ( st ) :
n = len ( st )
digitSum = 0
for i in range ( 0 , n ) :
digitSum = digitSum + ( int ) ( st [ i ] )
return ( digitSum % 9 == 0 )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int check ( string str ) {
int n = str . length ( ) ;
int digitSum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) digitSum += ( str [ i ] - '0' ) ;
return ( digitSum % 9 == 0 ) ;
}
</Code> |
cpp-python-FIND_PAIR_MAXIMUM_GCD_ARRAY | cpp-python | int findMaxGCD ( int arr [ ] , int n ) {
int high = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) high = max ( high , arr [ i ] ) ;
int divisors [ high + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 1 ;
j <= sqrt ( arr [ i ] ) ;
j ++ ) {
if ( arr [ i ] % ... | def findMaxGCD ( arr , n ) :
high = 0
i = 0
while i < n :
high = max ( high , arr [ i ] )
i = i + 1
divisors = [ 0 ] * ( high + 1 )
i = 0
while i < n :
j = 1
while j <= math.sqrt ( arr [ i ] ) :
if ( arr [ i ] % j == 0 ) :
divisors [ j ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findMaxGCD ( int arr [ ] , int n ) {
int high = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) high = max ( high , arr [ i ] ) ;
int divisors [ high + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 1 ;
j <= sqrt ( arr [ i ] ) ;
j ++ ) {
if ( arr [... |
cpp-python-CONVERT_STRICTLY_INCREASING_ARRAY_MINIMUM_CHANGES | cpp-python | int minRemove ( int arr [ ] , int n ) {
int LIS [ n ] , len = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) LIS [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) {
LIS [ i ] = max ( LIS ... | def minRemove ( arr , n ) :
LIS = [ 0 for i in range ( n ) ]
len = 0
for i in range ( n ) :
LIS [ i ] = 1
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) :
LIS [ i ] = max ( LIS [ i ] , ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int minRemove ( int arr [ ] , int n ) {
int LIS [ n ] , len = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) LIS [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) {
LIS [ i ] = max... |
cpp-python-SEARCHING_FOR_PATTERNS_SET_2_KMP_ALGORITHM | cpp-python | void computeLPSArray ( char * pat , int M , int * lps ) {
int len = 0 ;
lps [ 0 ] = 0 ;
int i = 1 ;
while ( i < M ) {
if ( pat [ i ] == pat [ len ] ) {
len ++ ;
lps [ i ] = len ;
i ++ ;
}
else {
if ( len != 0 ) {
len = lps [ len - 1 ] ;
}
else {
lp... | def computeLPSArray ( pat , M , lps ) :
len = 0
lps [ 0 ]
i = 1
while i < M :
if pat [ i ] == pat [ len ] :
len += 1
lps [ i ] = len
i += 1
else :
if len != 0 :
len = lps [ len - 1 ]
else :
lps [ ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void computeLPSArray ( char * pat , int M , int * lps ) {
int len = 0 ;
lps [ 0 ] = 0 ;
int i = 1 ;
while ( i < M ) {
if ( pat [ i ] == pat [ len ] ) {
len ++ ;
lps [ i ] = len ;
i ++ ;
}
else {
if ( len != 0 ) {
len = lps [ len - 1 ] ;
}
else {
... |
cpp-python-COUNT_MINIMUM_NUMBER_SUBSETS_SUBSEQUENCES_CONSECUTIVE_NUMBERS | cpp-python | int numofsubset ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int count = 1 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ;
}
return count ;
}
| def numofsubset ( arr , n ) :
x = sorted ( arr )
count = 1
for i in range ( 0 , n - 1 ) :
if ( x [ i ] + 1 != x [ i + 1 ] ) :
count = count + 1
return count
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int numofsubset ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int count = 1 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ;
}
return count ;
}
</Code> |
cpp-python-STOOGE_SORT | cpp-python | void stoogesort ( int arr [ ] , int l , int h ) {
if ( l >= h ) return ;
if ( arr [ l ] > arr [ h ] ) swap ( arr [ l ] , arr [ h ] ) ;
if ( h - l + 1 > 2 ) {
int t = ( h - l + 1 ) / 3 ;
stoogesort ( arr , l , h - t ) ;
stoogesort ( arr , l + t , h ) ;
stoogesort ( arr , l , h - t ) ;
}
}
| def stoogesort ( arr , l , h ) :
if l >= h :
return
if arr [ l ] > arr [ h ] :
t = arr [ l ]
arr [ l ] = arr [ h ]
arr [ h ] = t
if h - l + 1 > 2 :
t = ( int ) ( ( h - l + 1 ) / 3 )
stoogesort ( arr , l , ( h - t ) )
stoogesort ( arr , l + t , ( h ) )
... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void stoogesort ( int arr [ ] , int l , int h ) {
if ( l >= h ) return ;
if ( arr [ l ] > arr [ h ] ) swap ( arr [ l ] , arr [ h ] ) ;
if ( h - l + 1 > 2 ) {
int t = ( h - l + 1 ) / 3 ;
stoogesort ( arr , l , h - t ) ;
stoogesort ( arr , l + t , h ) ;
stoogesort ( arr , l , h - t ) ;
}
}
... |
cpp-python-FIND_ARRANGEMENT_QUEUE_GIVEN_TIME | cpp-python | void solve ( int n , int t , string s ) {
for ( int i = 0 ;
i < t ;
i ++ ) for ( int j = 0 ;
j < n - 1 ;
j ++ ) if ( s [ j ] == 'B' && s [ j + 1 ] == 'G' ) {
char temp = s [ j ] ;
s [ j ] = s [ j + 1 ] ;
s [ j + 1 ] = temp ;
j ++ ;
}
cout << s ;
}
| def solve ( n , t , p ) :
s = list ( p )
for i in range ( 0 , t ) :
for j in range ( 0 , n - 1 ) :
if ( s [ j ] == 'B' and s [ j + 1 ] == 'G' ) :
temp = s [ j ]
s [ j ] = s [ j + 1 ]
s [ j + 1 ] = temp
j = j + 1
print ( ''.j... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void solve ( int n , int t , string s ) {
for ( int i = 0 ;
i < t ;
i ++ ) for ( int j = 0 ;
j < n - 1 ;
j ++ ) if ( s [ j ] == 'B' && s [ j + 1 ] == 'G' ) {
char temp = s [ j ] ;
s [ j ] = s [ j + 1 ] ;
s [ j + 1 ] = temp ;
j ++ ;
}
cout << s ;
}
</Code> |
cpp-python-HOW_TO_TURN_OFF_A_PARTICULAR_BIT_IN_A_NUMBER | cpp-python | int turnOffK ( int n , int k ) {
if ( k <= 0 ) return n ;
return ( n & ~ ( 1 << ( k - 1 ) ) ) ;
}
| def turnOffK ( n , k ) :
if ( k <= 0 ) :
return n
return ( n & ~ ( 1 << ( k - 1 ) ) )
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int turnOffK ( int n , int k ) {
if ( k <= 0 ) return n ;
return ( n & ~ ( 1 << ( k - 1 ) ) ) ;
}
</Code> |
cpp-python-FINDING_K_MODULUS_ARRAY_ELEMENT | cpp-python | void printEqualModNumbers ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int d = arr [ n - 1 ] - arr [ 0 ] ;
vector < int > v ;
for ( int i = 1 ;
i * i <= d ;
i ++ ) {
if ( d % i == 0 ) {
v . push_back ( i ) ;
if ( i != d / i ) v . push_back ( d / i ) ;
}
}
for ( int i = 0 ;
i ... | def printEqualModNumbers ( arr , n ) :
arr.sort ( )
d = arr [ n - 1 ] - arr [ 0 ]
v = [ ]
i = 1
while ( i * i <= d ) :
if ( d % i == 0 ) :
v.append ( i )
if ( i != d / i ) :
v.append ( d / i )
i += 1
for i in range ( len ( v ) ) :
t... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
void printEqualModNumbers ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int d = arr [ n - 1 ] - arr [ 0 ] ;
vector < int > v ;
for ( int i = 1 ;
i * i <= d ;
i ++ ) {
if ( d % i == 0 ) {
v . push_back ( i ) ;
if ( i != d / i ) v . push_back ( d / i ) ;
}
}
for ( int i = 0... |
cpp-python-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING_1 | cpp-python | int findMaximum ( int arr [ ] , int low , int high ) {
if ( low == high ) return arr [ low ] ;
if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ;
if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ;
int mid = ( low + high ) / 2 ;
if ( arr [ mid ] > arr [ m... | def findMaximum ( arr , low , high ) :
if low == high :
return arr [ low ]
if high == low + 1 and arr [ low ] >= arr [ high ] :
return arr [ low ]
if high == low + 1 and arr [ low ] < arr [ high ] :
return arr [ high ]
mid = ( low + high ) // 2
if arr [ mid ] > arr [ mid + 1 ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int findMaximum ( int arr [ ] , int low , int high ) {
if ( low == high ) return arr [ low ] ;
if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ;
if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ;
int mid = ( low + high ) / 2 ;
if ( arr [ mid ] > ... |
cpp-python-SMALLEST_LENGTH_STRING_WITH_REPEATED_REPLACEMENT_OF_TWO_DISTINCT_ADJACENT | cpp-python | int stringReduction ( string str ) {
int n = str . length ( ) ;
int count [ 3 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
++ i ) count [ str [ i ] - 'a' ] ++ ;
if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) return n ;
if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count ... | def stringReduction ( str ) :
n = len ( str )
count = [ 0 ] * 3
for i in range ( n ) :
count [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1
if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) :
return n
if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % 2 ) == ... | Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int stringReduction ( string str ) {
int n = str . length ( ) ;
int count [ 3 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
++ i ) count [ str [ i ] - 'a' ] ++ ;
if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) return n ;
if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && (... |
cpp-python-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM_1 | cpp-python | int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int dp [ W + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = W ;
j >= wt [ i ] ;
j -- ) dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ;
return dp [ W ] ;
}
| def KnapSack ( val , wt , n , W ) :
dp = [ 0 ] * ( W + 1 )
for i in range ( n ) :
for j in range ( W , wt [ i ] , - 1 ) :
dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] )
return dp [ W ]
| Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>. | <Code>
int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int dp [ W + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = W ;
j >= wt [ i ] ;
j -- ) dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ;
return dp [ W ] ;
}
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.