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_1
cpp-python
long long int findSum ( int n ) { long long int multiTerms = n * ( n + 1 ) / 2 ; long long int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; }
def findSum ( n ) : multiTerms = n * ( n + 1 ) // 2 sm = multiTerms for i in range ( 2 , n + 1 ) : multiTerms = multiTerms - ( i - 1 ) sm = sm + multiTerms * i return sm
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int findSum ( int n ) { long long int multiTerms = n * ( n + 1 ) / 2 ; long long int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; } </Code>
cpp-python-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE
cpp-python
void recursiveReverse ( string & str ) { stack < char > st ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) st . push ( str [ i ] ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { str [ i ] = st . top ( ) ; st . pop ( ) ; } }
def recursiveReverse ( str ) : stack = [ ] for i in range ( len ( str ) ) : stack.append ( str [ i ] ) for i in range ( len ( str ) ) : str [ i ] = stack.pop ( )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void recursiveReverse ( string & str ) { stack < char > st ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) st . push ( str [ i ] ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { str [ i ] = st . top ( ) ; st . pop ( ) ; } } </Code>
cpp-python-FIND_SUBARRAY_WITH_GIVEN_SUM
cpp-python
int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { cout << "Sum found between indexes " << i << " and " << j - 1 ; return 1 ; } ...
def subArraySum ( arr , n , sum ) : for i in range ( n ) : curr_sum = arr [ i ] j = i + 1 while j <= n : if curr_sum == sum : print ( "Sum found between" ) print ( "indexes %d and %d" % ( i , j - 1 ) ) return 1 if curr_s...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { cout << "Sum found between indexes " << i << " and " << j - 1 ; return 1 ; ...
cpp-python-FIND_THE_ELEMENT_THAT_APPEARS_ONCE_1
cpp-python
int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & x ) sum ++ ; } if ( sum % 3 ) result |= x ; } return result ; }
def getSingle ( arr , n ) : result = 0 for i in range ( 0 , INT_SIZE ) : sm = 0 x = ( 1 << i ) for j in range ( 0 , n ) : if ( arr [ j ] & x ) : sm = sm + 1 if ( sm % 3 ) : result = result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & x ) sum ++ ; } if ( sum % 3 ) result |= x ; } return result ; } </Code>
cpp-python-COMPUTE_MODULUS_DIVISION_BY_A_POWER_OF_2_NUMBER
cpp-python
unsigned int getModulo ( unsigned int n , unsigned int d ) { return ( n & ( d - 1 ) ) ; }
def getModulo ( n , d ) : return ( n & ( d - 1 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int getModulo ( unsigned int n , unsigned int d ) { return ( n & ( d - 1 ) ) ; } </Code>
cpp-python-QUICK_WAY_CHECK_CHARACTERS_STRING
cpp-python
bool allCharactersSame ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s [ i ] != s [ 0 ] ) return false ; return true ; }
def allCharactersSame ( s ) : n = len ( s ) for i in range ( 1 , n ) : if s [ i ] != s [ 0 ] : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool allCharactersSame ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s [ i ] != s [ 0 ] ) return false ; return true ; } </Code>
cpp-python-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX
cpp-python
long long int maxDecimalValue ( int mat [ ] [ N ] , int i , int j , int p ) { if ( i >= N || j >= N ) return 0 ; int result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) return pow ( 2 , p ) + result ; else return result ; }
def maxDecimalValue ( mat , i , j , p ) : if i >= N or j >= N : return 0 result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) if mat [ i ] [ j ] == 1 : return pow ( 2 , p ) + result else : return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int maxDecimalValue ( int mat [ ] [ N ] , int i , int j , int p ) { if ( i >= N || j >= N ) return 0 ; int result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) return pow ( 2 , p ) + result ; else return result ...
cpp-python-COUNT_BALANCED_BINARY_TREES_HEIGHT_H
cpp-python
long long int countBT ( int h ) { long long int dp [ h + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % mod + dp [ i - 1 ] ) % mod ) % mod ; } return dp [ h ] ; }
def countBT ( h ) : MOD = 1000000007 dp = [ 0 for i in range ( h + 1 ) ] dp [ 0 ] = 1 dp [ 1 ] = 1 for i in range ( 2 , h + 1 ) : dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD return dp [ h ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int countBT ( int h ) { long long int dp [ h + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % mod + dp [ i - 1 ] ) % mod ) % mod ; } return dp [ h ] ; } </Code>
cpp-python-COUNTING_INVERSIONS
cpp-python
int getInvCount ( int arr [ ] , int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; }
def getInvCount ( arr , n ) : inv_count = 0 for i in range ( n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : inv_count += 1 return inv_count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getInvCount ( int arr [ ] , int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; } </Code>
cpp-python-COORDINATES_RECTANGLE_GIVEN_POINTS_LIE_INSIDE
cpp-python
void printRect ( int X [ ] , int Y [ ] , int n ) { int Xmax = * max_element ( X , X + n ) ; int Xmin = * min_element ( X , X + n ) ; int Ymax = * max_element ( Y , Y + n ) ; int Ymin = * min_element ( Y , Y + n ) ; cout << "{" << Xmin << ", " << Ymin << "}" << endl ; cout << "{" << Xmin << ", " << Ymax << "...
def printRect ( X , Y , n ) : Xmax = max ( X ) Xmin = min ( X ) Ymax = max ( Y ) Ymin = min ( Y ) print ( "{" , Xmin , ", " , Ymin , "}" , sep = "" ) print ( "{" , Xmin , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymin ,...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printRect ( int X [ ] , int Y [ ] , int n ) { int Xmax = * max_element ( X , X + n ) ; int Xmin = * min_element ( X , X + n ) ; int Ymax = * max_element ( Y , Y + n ) ; int Ymin = * min_element ( Y , Y + n ) ; cout << "{" << Xmin << ", " << Ymin << "}" << endl ; cout << "{" << Xmin << ", " << Ym...
cpp-python-DELANNOY_NUMBER_1
cpp-python
int dealnnoy ( int n , int m ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j -...
def dealnnoy ( n , m ) : dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ] for i in range ( m ) : dp [ 0 ] [ i ] = 1 for i in range ( 1 , m + 1 ) : dp [ i ] [ 0 ] = 1 for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : dp [ i ] [ j ] = dp [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int dealnnoy ( int n , int m ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ...
cpp-python-MODULAR_EXPONENTIATION_POWER_IN_MODULAR_ARITHMETIC
cpp-python
int power ( int x , unsigned int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; }
def power ( x , y , p ) : res = 1 x = x % p while ( y > 0 ) : if ( ( y & 1 ) == 1 ) : res = ( res * x ) % p y = y >> 1 x = ( x * x ) % p return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int power ( int x , unsigned int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } </Code>
cpp-python-PROGRAM_CHECK_ISBN
cpp-python
bool isValidISBN ( string & isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn [ i ] - '0' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn [ 9 ] ; if ( last !...
def isValidISBN ( isbn ) : if len ( isbn ) != 10 : return False _sum = 0 for i in range ( 9 ) : if 0 <= int ( isbn [ i ] ) <= 9 : _sum += int ( isbn [ i ] ) * ( 10 - i ) else : return False if ( isbn [ 9 ] != 'X' and 0 <= int ( isbn [ 9 ] ) <= 9 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isValidISBN ( string & isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn [ i ] - '0' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn [ 9 ] ; if (...
cpp-python-NEXT_GREATER_FREQUENCY_ELEMENT
cpp-python
void NFG ( int a [ ] , int n , int freq [ ] ) { stack < int > s ; s . push ( 0 ) ; int res [ n ] = { 0 } ; for ( int i = 1 ; i < n ; i ++ ) { if ( freq [ a [ s . top ( ) ] ] > freq [ a [ i ] ] ) s . push ( i ) ; else { while ( freq [ a [ s . top ( ) ] ] < freq [ a [ i ] ] &...
def NFG ( a , n ) : if ( n <= 0 ) : print ( "List empty" ) return [ ] stack = [ 0 ] * n freq = { } for i in a : freq [ a [ i ] ] = 0 for i in a : freq [ a [ i ] ] += 1 res = [ 0 ] * n top = - 1 top += 1 stack [ top ] = 0 for i in range ( 1 , n ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void NFG ( int a [ ] , int n , int freq [ ] ) { stack < int > s ; s . push ( 0 ) ; int res [ n ] = { 0 } ; for ( int i = 1 ; i < n ; i ++ ) { if ( freq [ a [ s . top ( ) ] ] > freq [ a [ i ] ] ) s . push ( i ) ; else { while ( freq [ a [ s . top ( ) ] ] < freq [ a [ ...
cpp-python-SWAP_BITS_IN_A_GIVEN_NUMBER
cpp-python
int swapBits ( unsigned int x , unsigned int p1 , unsigned int p2 , unsigned int n ) { unsigned int set1 = ( x >> p1 ) & ( ( 1U << n ) - 1 ) ; unsigned int set2 = ( x >> p2 ) & ( ( 1U << n ) - 1 ) ; unsigned int Xor = ( set1 ^ set2 ) ; Xor = ( Xor << p1 )
def swapBits ( x , p1 , p2 , n ) : set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) xor = ( set1 ^ set2 ) xor = ( xor << p1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int swapBits ( unsigned int x , unsigned int p1 , unsigned int p2 , unsigned int n ) { unsigned int set1 = ( x >> p1 ) & ( ( 1U << n ) - 1 ) ; unsigned int set2 = ( x >> p2 ) & ( ( 1U << n ) - 1 ) ; unsigned int Xor = ( set1 ^ set2 ) ; Xor = ( Xor << p1 ) </Code>
cpp-python-PRINT_EQUAL_SUM_SETS_ARRAY_PARTITION_PROBLEM_SET_2
cpp-python
void printEqualSumSets ( int arr [ ] , int n ) { int i , currSum ; int sum = accumulate ( arr , arr + n , 0 ) ; if ( sum & 1 ) { cout << "-1" ; return ; } int k = sum >> 1 ; bool dp [ n + 1 ] [ k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; i ++ )...
def printEqualSumSets ( arr , n ) : sum_array = sum ( arr ) if ( sum_array & 1 ) : print ( "-1" ) return k = sum_array >> 1 dp = np.zeros ( ( n + 1 , k + 1 ) ) for i in range ( 1 , k + 1 ) : dp [ 0 ] [ i ] = False for i in range ( n + 1 ) : dp [ i ] [ 0 ] = True ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printEqualSumSets ( int arr [ ] , int n ) { int i , currSum ; int sum = accumulate ( arr , arr + n , 0 ) ; if ( sum & 1 ) { cout << "-1" ; return ; } int k = sum >> 1 ; bool dp [ n + 1 ] [ k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; ...
cpp-python-HYPERCUBE_GRAPH
cpp-python
int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; }
def power ( n ) : if n == 1 : return 2 return 2 * power ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; } </Code>
cpp-python-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS_2
cpp-python
void findElements ( int arr [ ] , int n ) { int first = INT_MIN , second = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( ar...
def findElements ( arr , n ) : first = - sys.maxsize second = - sys.maxsize for i in range ( 0 , n ) : if ( arr [ i ] > first ) : second = first first = arr [ i ] elif ( arr [ i ] > second ) : second = arr [ i ] for i in range ( 0 , n ) : if ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findElements ( int arr [ ] , int n ) { int first = INT_MIN , second = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) ...
cpp-python-DISTRIBUTING_M_ITEMS_CIRCLE_SIZE_N_STARTING_K_TH_POSITION
cpp-python
int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; }
def lastPosition ( n , m , k ) : if ( m <= n - k + 1 ) : return m + k - 1 m = m - ( n - k + 1 ) if ( m % n == 0 ) : return n else : return m % n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } </Code>
cpp-python-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_1
cpp-python
void reverseStr ( string & str ) { int n = str . length ( ) ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) swap ( str [ i ] , str [ j ] ) ; }
def reverseStr ( str ) : n = len ( str ) i , j = 0 , n - 1 while i < j : str [ i ] , str [ j ] = str [ j ] , str [ i ] i += 1 j -= 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void reverseStr ( string & str ) { int n = str . length ( ) ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) swap ( str [ i ] , str [ j ] ) ; } </Code>
cpp-python-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING
cpp-python
int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low + 1 ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; else break ; } return max ; }
def findMaximum ( arr , low , high ) : max = arr [ low ] i = low for i in range ( high + 1 ) : if arr [ i ] > max : max = arr [ i ] return max
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 ) { int max = arr [ low ] ; int i ; for ( i = low + 1 ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; else break ; } return max ; } </Code>
cpp-python-LONGEST_PREFIX_ALSO_SUFFIX_1
cpp-python
int longestPrefixSuffix ( string s ) { int n = s . length ( ) ; int lps [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } ...
def longestPrefixSuffix ( s ) : n = len ( s ) lps = [ 0 ] * n l = 0 i = 1 while ( i < n ) : if ( s [ i ] == s [ l ] ) : l = l + 1 lps [ i ] = l i = i + 1 else : if ( l != 0 ) : l = lps [ l - 1 ] else : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longestPrefixSuffix ( string s ) { int n = s . length ( ) ; int lps [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; ...
cpp-python-SUM_AREA_RECTANGLES_POSSIBLE_ARRAY
cpp-python
int MaxTotalRectangleArea ( int a [ ] , int n ) { sort ( a , a + n , greater < int > ( ) ) ; int sum = 0 ; bool flag = false ; int len ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] || a [ i ] - a [ i + 1 ] == 1 ) && ( ! flag ) ) { flag = true ; len = a [ i + 1 ] ; ...
def MaxTotalRectangleArea ( a , n ) : a.sort ( reverse = True ) sum = 0 flag = False len = 0 i = 0 while ( i < n - 1 ) : if ( i != 0 ) : i = i + 1 if ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == False ) : flag = True l...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int MaxTotalRectangleArea ( int a [ ] , int n ) { sort ( a , a + n , greater < int > ( ) ) ; int sum = 0 ; bool flag = false ; int len ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] || a [ i ] - a [ i + 1 ] == 1 ) && ( ! flag ) ) { flag = true ; len = a [ i + 1 ]...
cpp-python-PROGRAM_TO_PRINT_FIRST_N_FIBONACCI_NUMBERS
cpp-python
void printFibonacciNumbers ( int n ) { int f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; for ( i = 1 ; i <= n ; i ++ ) { cout << f2 << " " ; int next = f1 + f2 ; f1 = f2 ; f2 = next ; } }
def printFibonacciNumbers ( n ) : f1 = 0 f2 = 1 if ( n < 1 ) : return for x in range ( 0 , n ) : print ( f2 , end = " " ) next = f1 + f2 f1 = f2 f2 = next
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printFibonacciNumbers ( int n ) { int f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; for ( i = 1 ; i <= n ; i ++ ) { cout << f2 << " " ; int next = f1 + f2 ; f1 = f2 ; f2 = next ; } } </Code>
cpp-python-LEXICOGRAPHICALLY_SMALLEST_ARRAY_K_CONSECUTIVE_SWAPS
cpp-python
void minimizeWithKSwaps ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 && k > 0 ; ++ i ) { int pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( j - i > k ) break ; if ( arr [ j ] < arr [ pos ] ) pos = j ; } for ( int j = pos ; j > i ; -- j ) swap ( a...
def minimizeWithKSwaps ( arr , n , k ) : for i in range ( n - 1 ) : pos = i for j in range ( i + 1 , n ) : if ( j - i > k ) : break if ( arr [ j ] < arr [ pos ] ) : pos = j for j in range ( pos , i , - 1 ) : arr [ j ] , arr ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void minimizeWithKSwaps ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 && k > 0 ; ++ i ) { int pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( j - i > k ) break ; if ( arr [ j ] < arr [ pos ] ) pos = j ; } for ( int j = pos ; j > i ; -- j ) s...
cpp-python-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP
cpp-python
bool isHeap ( int arr [ ] , int i , int n ) { if ( i > ( n - 2 ) / 2 ) return true ; if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) return true ; return false ; }
def isHeap ( arr , i , n ) : if i > int ( ( n - 2 ) / 2 ) : return True if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) : return True return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isHeap ( int arr [ ] , int i , int n ) { if ( i > ( n - 2 ) / 2 ) return true ; if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) return true ; return false ; } </Code>
cpp-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_3
cpp-python
int findLength ( string str , int n ) { int ans = 0 ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { int l = i , r = i + 1 ; int lsum = 0 , rsum = 0 ; while ( r < n && l >= 0 ) { lsum += str [ l ] - '0' ; rsum += str [ r ] - '0' ; if ( lsum == rsum ) ans = max ( ans , r - l + 1 ) ; ...
def findLength ( st , n ) : total = [ 0 ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : total [ i ] = ( total [ i - 1 ] + int ( st [ i - 1 ] ) - int ( '0' ) ) ans = 0 l = 2 while ( l <= n ) : for i in range ( n - l + 1 ) : j = i + l - 1 if ( total [ i + int ( l /...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLength ( string str , int n ) { int ans = 0 ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { int l = i , r = i + 1 ; int lsum = 0 , rsum = 0 ; while ( r < n && l >= 0 ) { lsum += str [ l ] - '0' ; rsum += str [ r ] - '0' ; if ( lsum == rsum ) ans = max ( ans , r - l + 1 ) ;...
cpp-python-COUNT_SUBARRAYS_WITH_SAME_EVEN_AND_ODD_ELEMENTS
cpp-python
int countSubarrays ( int arr [ ] , int n ) { int difference = 0 ; int ans = 0 ; int hash_positive [ n + 1 ] , hash_negative [ n + 1 ] ; fill_n ( hash_positive , n + 1 , 0 ) ; fill_n ( hash_negative , n + 1 , 0 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 == 1...
def countSubarrays ( arr , n ) : difference = 0 ans = 0 hash_positive = [ 0 ] * ( n + 1 ) hash_negative = [ 0 ] * ( n + 1 ) hash_positive [ 0 ] = 1 for i in range ( n ) : if ( arr [ i ] & 1 == 1 ) : difference = difference + 1 else : difference = differenc...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSubarrays ( int arr [ ] , int n ) { int difference = 0 ; int ans = 0 ; int hash_positive [ n + 1 ] , hash_negative [ n + 1 ] ; fill_n ( hash_positive , n + 1 , 0 ) ; fill_n ( hash_negative , n + 1 , 0 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] &...
cpp-python-KNAPSACK_PROBLEM_1
cpp-python
int knapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ...
def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( val [ i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int knapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [...
cpp-python-C_PROGRAM_SUBTRACTION_MATICES
cpp-python
void multiply ( int A [ ] [ N ] , int B [ ] [ N ] , int C [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; }
def multiply ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void multiply ( int A [ ] [ N ] , int B [ ] [ N ] , int C [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; } </Code>
cpp-python-MAXIMUM_VALUE_CHOICE_EITHER_DIVIDING_CONSIDERING
cpp-python
int maxDP ( int n ) { int res [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; }
def maxDP ( n ) : res = list ( ) res.append ( 0 ) res.append ( 1 ) i = 2 while i < n + 1 : res.append ( max ( i , ( res [ int ( i / 2 ) ] + res [ int ( i / 3 ) ] + res [ int ( i / 4 ) ] + res [ int ( i / 5 ) ] ) ) ) i = i + 1 return res [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxDP ( int n ) { int res [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } </Code>
cpp-python-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY
cpp-python
int findDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = max ( max_count , count ) ; min_count ...
def findDiff ( arr , n ) : arr.sort ( ) count = 0 max_count = 0 min_count = n for i in range ( 0 , ( n - 1 ) ) : if arr [ i ] == arr [ i + 1 ] : count += 1 continue else : max_count = max ( max_count , count ) min_count = min ( min_count , coun...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = max ( max_count , count ) ; min...
cpp-python-NUMBER_SINK_NODES_GRAPH
cpp-python
int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int mark [ n ] ; memset ( mark , 0 , sizeof mark ) ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( ! mark [ i ] ) count ++ ; return count ; }
def countSink ( n , m , edgeFrom , edgeTo ) : mark = [ 0 ] * ( n + 1 ) for i in range ( m ) : mark [ edgeFrom [ i ] ] = 1 count = 0 for i in range ( 1 , n + 1 ) : if ( not mark [ i ] ) : count += 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int mark [ n ] ; memset ( mark , 0 , sizeof mark ) ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( ! mark [ i ] ) count ++ ; return count ; } </Code>
cpp-python-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX
cpp-python
int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; }
def countNegative ( M , n , m ) : count = 0 for i in range ( n ) : for j in range ( m ) : if M [ i ] [ j ] < 0 : count += 1 else : break return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; } </Code>
cpp-python-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_1
cpp-python
int middleOfThree ( int a , int b , int c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } }
def middleOfThree ( a , b , c ) : if a > b : if ( b > c ) : return b elif ( a > c ) : return c else : return a else : if ( a > c ) : return a elif ( b > c ) : return c else : return b
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 ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } } </Code>
cpp-python-COUNT_SUBSTRINGS_BINARY_STRING_CONTAINING_K_ONES
cpp-python
int countOfSubstringWithKOnes ( string s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int freq [ N + 1 ] = { 0 } ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s [ i ] - '0' ) ; if ( countOfOne >= K ) { res += freq [ c...
def countOfSubstringWithKOnes ( s , K ) : N = len ( s ) res = 0 countOfOne = 0 freq = [ 0 for i in range ( N + 1 ) ] freq [ 0 ] = 1 for i in range ( 0 , N , 1 ) : countOfOne += ord ( s [ i ] ) - ord ( '0' ) if ( countOfOne >= K ) : res += freq [ countOfOne - K ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countOfSubstringWithKOnes ( string s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int freq [ N + 1 ] = { 0 } ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s [ i ] - '0' ) ; if ( countOfOne >= K ) { res += f...
cpp-python-FIND_FOUR_ELEMENTS_A_B_C_AND_D_IN_AN_ARRAY_SUCH_THAT_AB_CD
cpp-python
bool findPairs ( int arr [ ] , int n ) { map < int , pair < int , int >> Hash ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( Hash . find ( sum ) == Hash . end ( ) ) Hash [ sum ] = make_pair ( i , j ) ; else { ...
def findPairs ( arr , n ) : Hash = { } for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : sum = arr [ i ] + arr [ j ] if sum in Hash.keys ( ) : prev = Hash.get ( sum ) print ( str ( prev ) + " and (%d, %d)" % ( arr [ i ] , arr [ j ] ) ) return True else : Hash [ su...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool findPairs ( int arr [ ] , int n ) { map < int , pair < int , int >> Hash ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( Hash . find ( sum ) == Hash . end ( ) ) Hash [ sum ] = make_pair ( i , j ) ; else ...
cpp-python-LINEAR_SEARCH
cpp-python
int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; }
def search ( arr , n , x ) : for i in range ( 0 , n ) : if ( arr [ i ] == x ) : return i return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; } </Code>
cpp-python-CHECK_POSSIBLE_TRANSFORM_ONE_STRING_ANOTHER
cpp-python
bool check ( string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; bool dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 . lengt...
def check ( s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) dp = ( [ [ False for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] ) dp [ 0 ] [ 0 ] = True for i in range ( len ( s1 ) ) : for j in range ( len ( s2 ) + 1 ) : if ( dp [ i ] [ j ] ) : if ( ( j < len ( s2 ) a...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool check ( string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; bool dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 ...
cpp-python-PROGRAM_BINARY_DECIMAL_CONVERSION_1
cpp-python
int binaryToDecimal ( string n ) { string num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; }
def binaryToDecimal ( n ) : num = n dec_value = 0 base1 = 1 len1 = len ( num ) for i in range ( len1 - 1 , - 1 , - 1 ) : if ( num [ i ] == '1' ) : dec_value += base1 base1 = base1 * 2 return dec_value
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int binaryToDecimal ( string n ) { string num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } </Code>
cpp-python-COUNT_WAYS_BUILD_STREET_GIVEN_CONSTRAINTS
cpp-python
long countWays ( int n ) { long dp [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ] [ n ] + dp [ 1 ] [ n ] ; ...
def countWays ( n ) : dp = [ [ 0 ] * ( n + 1 ) for i in range ( 2 ) ] dp [ 0 ] [ 1 ] = 1 dp [ 1 ] [ 1 ] = 2 for i in range ( 2 , n + 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] dp [ 1 ] [ i ] = ( dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ) return dp [ 0 ] [ n ] +...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long countWays ( int n ) { long dp [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ] [ n ] + dp [ 1 ] [...
cpp-python-MIRROR_CHARACTERS_STRING
cpp-python
string compute ( string str , int n ) { string reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ; int l = str . length ( ) ; for ( int i = n ; i < l ; i ++ ) str [ i ] = reverseAlphabet [ str [ i ] - 'a' ] ; return str ; }
def compute ( st , n ) : reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" l = len ( st ) answer = "" for i in range ( 0 , n ) : answer = answer + st [ i ] for i in range ( n , l ) : answer = ( answer + reverseAlphabet [ ord ( st [ i ] ) - ord ( 'a' ) ] ) return answer
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string compute ( string str , int n ) { string reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ; int l = str . length ( ) ; for ( int i = n ; i < l ; i ++ ) str [ i ] = reverseAlphabet [ str [ i ] - 'a' ] ; return str ; } </Code>
cpp-python-MAXIMUM_LENGTH_SUBSEQUENCE_DIFFERENCE_ADJACENT_ELEMENTS_EITHER_0_1
cpp-python
int maxLenSub ( int arr [ ] , int n ) { int mls [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i = 0 ; ...
def maxLenSub ( arr , n ) : mls = [ ] max = 0 for i in range ( n ) : mls.append ( 1 ) for i in range ( n ) : for j in range ( i ) : if ( abs ( arr [ i ] - arr [ j ] ) <= 1 and mls [ i ] < mls [ j ] + 1 ) : mls [ i ] = mls [ j ] + 1 for i in range ( n ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLenSub ( int arr [ ] , int n ) { int mls [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i =...
cpp-python-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION_1
cpp-python
long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; }
def exponentiation ( bas , exp ) : t = 1 while ( exp > 0 ) : if ( exp % 2 != 0 ) : t = ( t * bas ) % N bas = ( bas * bas ) % N exp = int ( exp / 2 ) return t % N
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; } </Code>
cpp-python-NEWMAN_CONWAY_SEQUENCE_1
cpp-python
int sequence ( int n ) { int f [ n + 1 ] ; int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; }
def sequence ( n ) : f = array.array ( 'i' , [ 0 , 1 , 1 ] ) for i in range ( 3 , n + 1 ) : r = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] f.append ( r ) ; return r
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sequence ( int n ) { int f [ n + 1 ] ; int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_15_LONGEST_BITONIC_SUBSEQUENCE
cpp-python
int lbs ( int arr [ ] , int n ) { int i , j ; int * lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int * lds = new int [ n ] ; for (...
def lbs ( arr ) : n = len ( arr ) lis = [ 1 for i in range ( n + 1 ) ] for i in range ( 1 , n ) : for j in range ( 0 , i ) : if ( ( arr [ i ] > arr [ j ] ) and ( lis [ i ] < lis [ j ] + 1 ) ) : lis [ i ] = lis [ j ] + 1 lds = [ 1 for i in range ( n + 1 ) ] for i i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int lbs ( int arr [ ] , int n ) { int i , j ; int * lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int * lds = new int [ n ] ; ...
cpp-python-MAXIMUM_SUM_INCREASING_ORDER_ELEMENTS_N_ARRAYS
cpp-python
int maximumSum ( int a [ ] [ M ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a [ i ] , a [ i ] + M ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] <...
def maximumSum ( a , n ) : global M for i in range ( 0 , n ) : a [ i ].sort ( ) sum = a [ n - 1 ] [ M - 1 ] prev = a [ n - 1 ] [ M - 1 ] for i in range ( n - 2 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( a [ i ] [ j ] < prev ) : prev = a [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maximumSum ( int a [ ] [ M ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a [ i ] , a [ i ] + M ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] ...
cpp-python-MASTER_THEOREM_SUBTRACT_CONQUER_RECURRENCES
cpp-python
int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; }
def fib ( n ) : if ( n <= 1 ) : return n return fib ( n - 1 ) + fib ( n - 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } </Code>
cpp-python-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS
cpp-python
int countPairs ( string str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ; return result ; }
def countPairs ( str1 ) : result = 0 ; n = len ( str1 ) for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( abs ( ord ( str1 [ i ] ) - ord ( str1 [ j ] ) ) == abs ( i - j ) ) : result += 1 ; return result ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairs ( string str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ; return result ; } </Code>
cpp-python-CHECK_IF_ALL_THE_ELEMENTS_CAN_BE_MADE_OF_SAME_PARITY_BY_INVERTING_ADJACENT_ELEMENTS
cpp-python
bool flipsPossible ( int a [ ] , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] & 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 && count_even % 2 ) return false ; else return true ; }
def flipsPossible ( a , n ) : count_odd = 0 count_even = 0 for i in range ( n ) : if ( a [ i ] & 1 ) : count_odd += 1 else : count_even += 1 if ( count_odd % 2 and count_even % 2 ) : return False else : return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool flipsPossible ( int a [ ] , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] & 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 && count_even % 2 ) return false ; else return true ; } </Code>
cpp-python-CHECK_GIVEN_ARRAY_CONTAINS_DUPLICATE_ELEMENTS_WITHIN_K_DISTANCE
cpp-python
bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) { unordered_set < int > myset ; for ( int i = 0 ; i < n ; i ++ ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ; myset . insert ( arr [ i ] ) ; if ( i >= k ) myset . erase ( arr [ i - k ] ) ; } return false ; }
def checkDuplicatesWithinK ( arr , n , k ) : myset = [ ] for i in range ( n ) : if arr [ i ] in myset : return True myset.append ( arr [ i ] ) if ( i >= k ) : myset.remove ( arr [ i - k ] ) return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) { unordered_set < int > myset ; for ( int i = 0 ; i < n ; i ++ ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ; myset . insert ( arr [ i ] ) ; if ( i >= k ) myset . erase ( arr [ i - k ] ) ; } return false ;...
cpp-python-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS_1
cpp-python
void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) { cout << " \nDiagonal one : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ i ] * mat [ i ] [ i ] << " " ; } cout << " \n\nDiagonal two : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ row - i - 1 ] ...
def diagonalsquare ( mat , row , column ) : print ( "Diagonal one : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ i ] * mat [ i ] [ i ] , end = "" ) print ( "\n\nDiagonal two : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ row - i - 1 ] * mat [ i ] [ r...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) { cout << " \nDiagonal one : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ i ] * mat [ i ] [ i ] << " " ; } cout << " \n\nDiagonal two : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ row - i...
cpp-python-FIND_THE_LARGEST_RECTANGLE_OF_1S_WITH_SWAPPING_OF_COLUMNS_ALLOWED
cpp-python
int maxArea ( bool mat [ R ] [ C ] ) { int hist [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } for ( int i = 0 ; i < R ; i ++ ) {...
def maxArea ( mat ) : hist = [ [ 0 for i in range ( C + 1 ) ] for i in range ( R + 1 ) ] for i in range ( 0 , C , 1 ) : hist [ 0 ] [ i ] = mat [ 0 ] [ i ] for j in range ( 1 , R , 1 ) : if ( ( mat [ j ] [ i ] == 0 ) ) : hist [ j ] [ i ] = 0 else : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxArea ( bool mat [ R ] [ C ] ) { int hist [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } for ( int i = 0 ; i < R ; i...
cpp-python-LONGEST_SUBSEQUENCE_SUCH_THAT_DIFFERENCE_BETWEEN_ADJACENTS_IS_ONE
cpp-python
int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [ i ] , ...
def longestSubseqWithDiffOne ( arr , n ) : dp = [ 1 for i in range ( n ) ] for i in range ( n ) : for j in range ( i ) : if ( ( arr [ i ] == arr [ j ] + 1 ) or ( arr [ i ] == arr [ j ] - 1 ) ) : dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) result = 1 for i in range ( n ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [...
cpp-python-MAXIMUM_NUMBER_OF_SQUARES_THAT_CAN_BE_FIT_IN_A_RIGHT_ANGLE_ISOSCELES_TRIANGLE
cpp-python
int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; }
def maxSquare ( b , m ) : return ( b / m - 1 ) * ( b / m ) / 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; } </Code>
cpp-python-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE
cpp-python
int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int dp [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] ...
def maxSumPairWithDifferenceLessThanK ( arr , N , K ) : arr.sort ( ) dp = [ 0 ] * N dp [ 0 ] = 0 for i in range ( 1 , N ) : dp [ i ] = dp [ i - 1 ] if ( arr [ i ] - arr [ i - 1 ] < K ) : if ( i >= 2 ) : dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + ar...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int dp [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr...
cpp-python-MINIMUM_OPERATION_MAKE_ELEMENTS_EQUAL_ARRAY
cpp-python
int minOperation ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 ; for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ; return ( n - max_count ) ; }
def minOperation ( arr , n ) : Hash = defaultdict ( lambda : 0 ) for i in range ( 0 , n ) : Hash [ arr [ i ] ] += 1 max_count = 0 for i in Hash : if max_count < Hash [ i ] : max_count = Hash [ i ] return n - max_count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minOperation ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 ; for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ; return ( n - max_count ) ; } </Code>
cpp-python-FIND_K_PAIRS_SMALLEST_SUMS_TWO_ARRAYS
cpp-python
void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { cout << "k pairs don't exist" ; return ; } int index2 [ n1 ] ; memset ( index2 , 0 , sizeof ( index2 ) ) ; while ( k > 0 ) { int min_sum = INT_MAX ; int min_index = 0 ; for ( int i1 = 0 ; ...
def kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) : if ( k > n1 * n2 ) : print ( "k pairs don't exist" ) return index2 = [ 0 for i in range ( n1 ) ] while ( k > 0 ) : min_sum = sys.maxsize min_index = 0 for i1 in range ( 0 , n1 , 1 ) : if ( index2 [ i1 ] < n...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { cout << "k pairs don't exist" ; return ; } int index2 [ n1 ] ; memset ( index2 , 0 , sizeof ( index2 ) ) ; while ( k > 0 ) { int min_sum = INT_MAX ; int min_index = 0 ; for ( int i1 =...
cpp-python-PRINT_A_GIVEN_MATRIX_IN_SPIRAL_FORM
cpp-python
void spiralPrint ( int m , int n , int a [ R ] [ C ] ) { int i , k = 0 , l = 0 ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) { cout << a [ k ] [ i ] << " " ; } k ++ ; for ( i = k ; i < m ; ++ i ) { cout << a [ i ] [ n - 1 ] << " " ; } n -- ; if ( k...
def spiralPrint ( m , n , a ) : k = 0 l = 0 while ( k < m and l < n ) : for i in range ( l , n ) : print ( a [ k ] [ i ] , end = " " ) k += 1 for i in range ( k , m ) : print ( a [ i ] [ n - 1 ] , end = " " ) n -= 1 if ( k < m ) : for i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void spiralPrint ( int m , int n , int a [ R ] [ C ] ) { int i , k = 0 , l = 0 ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) { cout << a [ k ] [ i ] << " " ; } k ++ ; for ( i = k ; i < m ; ++ i ) { cout << a [ i ] [ n - 1 ] << " " ; } n -- ; ...
cpp-python-CALCULATE_SUM_OF_ALL_NUMBERS_PRESENT_IN_A_STRING
cpp-python
int findSum ( string str ) { string temp = "" ; int sum = 0 ; for ( char ch : str ) { if ( isdigit ( ch ) ) temp += ch ; else { sum += atoi ( temp . c_str ( ) ) ; temp = "" ; } } return sum + atoi ( temp . c_str ( ) ) ; }
def findSum ( str1 ) : temp = "" Sum = 0 for ch in str1 : if ( ch.isdigit ( ) ) : temp += ch else : Sum += int ( temp ) temp = "0" return Sum + int ( temp )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( string str ) { string temp = "" ; int sum = 0 ; for ( char ch : str ) { if ( isdigit ( ch ) ) temp += ch ; else { sum += atoi ( temp . c_str ( ) ) ; temp = "" ; } } return sum + atoi ( temp . c_str ( ) ) ; } </Code>
cpp-python-NTH_PENTAGONAL_NUMBER
cpp-python
int pentagonalNum ( int n ) { return ( 3 * n * n - n ) / 2 ; }
def pentagonalNum ( n ) : return ( 3 * n * n - n ) / 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int pentagonalNum ( int n ) { return ( 3 * n * n - n ) / 2 ; } </Code>
cpp-python-TOTAL_NUMBER_OF_NON_DECREASING_NUMBERS_WITH_N_DIGITS_1
cpp-python
long long int countNonDecreasing ( int n ) { int N = 10 ; long long count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { count *= ( N + i - 1 ) ; count /= i ; } return count ; }
def countNonDecreasing ( n ) : N = 10 count = 1 for i in range ( 1 , n + 1 ) : count = int ( count * ( N + i - 1 ) ) count = int ( count / i ) return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int countNonDecreasing ( int n ) { int N = 10 ; long long count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { count *= ( N + i - 1 ) ; count /= i ; } return count ; } </Code>
cpp-python-FREQUENT_ELEMENT_ARRAY_1
cpp-python
int mostFrequent ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 , res = - 1 ; for ( auto i : hash ) { if ( max_count < i . second ) { res = i . first ; max_count = i . second ; } } return res ; }...
def mostFrequent ( arr , n ) : Hash = dict ( ) for i in range ( n ) : if arr [ i ] in Hash.keys ( ) : Hash [ arr [ i ] ] += 1 else : Hash [ arr [ i ] ] = 1 max_count = 0 res = - 1 for i in Hash : if ( max_count < Hash [ i ] ) : res = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int mostFrequent ( int arr [ ] , int n ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; int max_count = 0 , res = - 1 ; for ( auto i : hash ) { if ( max_count < i . second ) { res = i . first ; max_count = i . second ; } } return ...
cpp-python-K_TH_ELEMENT_TWO_SORTED_ARRAYS
cpp-python
int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int sorted1 [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ...
def kth ( arr1 , arr2 , m , n , k ) : sorted1 = [ 0 ] * ( m + n ) i = 0 j = 0 d = 0 while ( i < m and j < n ) : if ( arr1 [ i ] < arr2 [ j ] ) : sorted1 [ d ] = arr1 [ i ] i += 1 else : sorted1 [ d ] = arr2 [ j ] j += 1 d += 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int sorted1 [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ ...
cpp-python-DYNAMIC_PROGRAMMING_SET_9_BINOMIAL_COEFFICIENT
cpp-python
int binomialCoeff ( int n , int k ) { if ( k == 0 || k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; }
def binomialCoeff ( n , k ) : if k == 0 or k == n : return 1 return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int binomialCoeff ( int n , int k ) { if ( k == 0 || k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } </Code>
cpp-python-STRING_CONTAINING_FIRST_LETTER_EVERY_WORD_GIVEN_STRING_SPACES
cpp-python
string firstLetterWord ( string str ) { string result = "" ; bool v = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ' ) v = true ; else if ( str [ i ] != ' ' && v == true ) { result . push_back ( str [ i ] ) ; v = false ; } } return result ; }
def firstLetterWord ( str ) : result = "" v = True for i in range ( len ( str ) ) : if ( str [ i ] == ' ' ) : v = True elif ( str [ i ] != ' ' and v == True ) : result += ( str [ i ] ) v = False return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string firstLetterWord ( string str ) { string result = "" ; bool v = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str [ i ] == ' ' ) v = true ; else if ( str [ i ] != ' ' && v == true ) { result . push_back ( str [ i ] ) ; v = false ; } } return result ; } ...
cpp-python-REARRANGE_ARRAY_SUCH_THAT_EVEN_POSITIONED_ARE_GREATER_THAN_ODD
cpp-python
void assign ( int a [ ] , int n ) { sort ( a , a + n ) ; int ans [ n ] ; int p = 0 , q = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ; else ans [ i ] = a [ p ++ ] ; } for ( int i = 0 ; i < n ; i ++ ) cout << ans [ i ] << " " ; }
def assign ( a , n ) : a.sort ( ) ans = [ 0 ] * n p = 0 q = n - 1 for i in range ( n ) : if ( i + 1 ) % 2 == 0 : ans [ i ] = a [ q ] q = q - 1 else : ans [ i ] = a [ p ] p = p + 1 for i in range ( n ) : print ( ans [ i ] , e...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void assign ( int a [ ] , int n ) { sort ( a , a + n ) ; int ans [ n ] ; int p = 0 , q = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ; else ans [ i ] = a [ p ++ ] ; } for ( int i = 0 ; i < n ; i ++ ) cout << ans [ i ] << " " ; } </Code>
cpp-python-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY
cpp-python
void printDistinct ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) cout << arr [ i ] << " " ; } }
def printDistinct ( arr , n ) : for i in range ( 0 , n ) : d = 0 for j in range ( 0 , i ) : if ( arr [ i ] == arr [ j ] ) : d = 1 break if ( d == 0 ) : print ( arr [ i ] )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printDistinct ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) cout << arr [ i ] << " " ; } } </Code>
cpp-python-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2
cpp-python
int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; }
def countSolutions ( n ) : res = 0 x = 0 while ( x * x < n ) : y = 0 while ( x * x + y * y < n ) : res = res + 1 y = y + 1 x = x + 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; } </Code>
cpp-python-FIND_RELATIVE_COMPLEMENT_TWO_SORTED_ARRAYS
cpp-python
void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { cout << arr1 [ i ] << " " ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ] ) { ...
def relativeComplement ( arr1 , arr2 , n , m ) : i = 0 j = 0 while ( i < n and j < m ) : if ( arr1 [ i ] < arr2 [ j ] ) : print ( arr1 [ i ] , "" , end = "" ) i += 1 elif ( arr1 [ i ] > arr2 [ j ] ) : j += 1 elif ( arr1 [ i ] == arr2 [ j ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { cout << arr1 [ i ] << " " ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ i ] == arr2 [ j ...
cpp-python-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D_1
cpp-python
int findFourElements ( int arr [ ] , int n ) { unordered_map < int , pair < int , int >> mp ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) mp [ arr [ i ] + arr [ j ] ] = { i , j } ; int d = INT_MIN ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int ...
def findFourElements ( arr , n ) : mp = dict ( ) for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : mp [ arr [ i ] + arr [ j ] ] = ( i , j ) d = - 10 ** 9 for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : abs_diff = abs ( arr [ i ] - arr [ j ] ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findFourElements ( int arr [ ] , int n ) { unordered_map < int , pair < int , int >> mp ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) mp [ arr [ i ] + arr [ j ] ] = { i , j } ; int d = INT_MIN ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for...
cpp-python-COUNT_TRAILING_ZEROES_FACTORIAL_NUMBER
cpp-python
int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; }
def findTrailingZeros ( n ) : count = 0 i = 5 while ( n / i >= 1 ) : count += int ( n / i ) i *= 5 return int ( count )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } </Code>
cpp-python-PRIMALITY_TEST_SET_5USING_LUCAS_LEHMER_SERIES
cpp-python
bool isPrime ( int p ) { long long checkNumber = pow ( 2 , p ) - 1 ; long long nextval = 4 % checkNumber ; for ( int i = 1 ; i < p - 1 ; i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ; return ( nextval == 0 ) ; }
def isPrime ( p ) : checkNumber = 2 ** p - 1 nextval = 4 % checkNumber for i in range ( 1 , p - 1 ) : nextval = ( nextval * nextval - 2 ) % checkNumber if ( nextval == 0 ) : return True else : return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPrime ( int p ) { long long checkNumber = pow ( 2 , p ) - 1 ; long long nextval = 4 % checkNumber ; for ( int i = 1 ; i < p - 1 ; i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ; return ( nextval == 0 ) ; } </Code>
cpp-python-COUNT_OF_SUB_STRINGS_THAT_DO_NOT_CONTAIN_ALL_THE_CHARACTERS_FROM_THE_SET_A_B_C_AT_THE_SAME_TIME
cpp-python
int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == 'a' ) { a_index = i + 1 ; ans -= min ( b_index , c_index ) ; } else if ( str [ i ] == 'b' ) { ...
def CountSubString ( Str , n ) : ans = ( n * ( n + 1 ) ) // 2 a_index = 0 b_index = 0 c_index = 0 for i in range ( n ) : if ( Str [ i ] == 'a' ) : a_index = i + 1 ans -= min ( b_index , c_index ) elif ( Str [ i ] == 'b' ) : b_index = i + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == 'a' ) { a_index = i + 1 ; ans -= min ( b_index , c_index ) ; } else if ( str [ i ] == 'b...
cpp-python-CHECK_GIVEN_STRING_ROTATION_PALINDROME
cpp-python
bool isPalindrome ( string str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; }
def isPalindrome ( string ) : l = 0 h = len ( string ) - 1 while h > l : l += 1 h -= 1 if string [ l - 1 ] != string [ h + 1 ] : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPalindrome ( string str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ; return true ; } </Code>
cpp-python-MAXIMUM_NUMBER_CHOCOLATES_DISTRIBUTED_EQUALLY_AMONG_K_STUDENTS
cpp-python
int maxNumOfChocolates ( int arr [ ] , int n , int k ) { unordered_map < int , int > um ; int sum [ n ] , curr_rem ; int maxSum = 0 ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { curr_rem = sum [ i ] % k ; ...
def maxNumOfChocolates ( arr , n , k ) : um , curr_rem , maxSum = { } , 0 , 0 sm = [ 0 ] * n sm [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : sm [ i ] = sm [ i - 1 ] + arr [ i ] for i in range ( n ) : curr_rem = sm [ i ] % k if ( not curr_rem and maxSum < sm [ i ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxNumOfChocolates ( int arr [ ] , int n , int k ) { unordered_map < int , int > um ; int sum [ n ] , curr_rem ; int maxSum = 0 ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { curr_rem = sum [ i ] % ...
cpp-python-DECODE_STRING_RECURSIVELY_ENCODED_COUNT_FOLLOWED_SUBSTRING
cpp-python
string decode ( string str ) { stack < int > integerstack ; stack < char > stringstack ; string temp = "" , result = "" ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( str [ i ] >= '0' && str [ i ] <= '9' ) { while ( str [ i ] >= '0' && str [ i ] <= '9' ) { cou...
def decode ( Str ) : integerstack = [ ] stringstack = [ ] temp = "" result = "" for i in range ( len ( Str ) ) : count = 0 if ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : while ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : count = count * 10 + ord ( Str [ i ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string decode ( string str ) { stack < int > integerstack ; stack < char > stringstack ; string temp = "" , result = "" ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( str [ i ] >= '0' && str [ i ] <= '9' ) { while ( str [ i ] >= '0' && str [ i ] <= '9' ) { ...
cpp-python-MINIMUM_SUM_CHOOSING_MINIMUM_PAIRS_ARRAY
cpp-python
int minSum ( int A [ ] , int n ) { int min_val = * min_element ( A , A + n ) ; return ( min_val * ( n - 1 ) ) ; }
def minSum ( A ) : min_val = min ( A ) ; return min_val * ( len ( A ) - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minSum ( int A [ ] , int n ) { int min_val = * min_element ( A , A + n ) ; return ( min_val * ( n - 1 ) ) ; } </Code>
cpp-python-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER_1
cpp-python
bool isPower ( int x , int y ) { int res1 = log ( y ) / log ( x ) ; double res2 = log ( y ) / log ( x ) ; return ( res1 == res2 ) ; }
def isPower ( x , y ) : res1 = math.log ( y ) / math.log ( x ) res2 = math.log ( y ) / math.log ( x ) return 1 if ( res1 == res2 ) else 0
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPower ( int x , int y ) { int res1 = log ( y ) / log ( x ) ; double res2 = log ( y ) / log ( x ) ; return ( res1 == res2 ) ; } </Code>
cpp-python-COUNT_WORDS_APPEAR_EXACTLY_TWO_TIMES_ARRAY_WORDS
cpp-python
int countWords ( string str [ ] , int n ) { unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] += 1 ; int res = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) if ( ( it -> second == 2 ) ) res ++ ; return res ; }
def countWords ( stri , n ) : m = dict ( ) for i in range ( n ) : m [ stri [ i ] ] = m.get ( stri [ i ] , 0 ) + 1 res = 0 for i in m.values ( ) : if i == 2 : res += 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countWords ( string str [ ] , int n ) { unordered_map < string , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ str [ i ] ] += 1 ; int res = 0 ; for ( auto it = m . begin ( ) ; it != m . end ( ) ; it ++ ) if ( ( it -> second == 2 ) ) res ++ ; return res ; } </Code>
cpp-python-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D
cpp-python
int findLargestd ( int S [ ] , int n ) { bool found = false ; sort ( S , S + n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; for ( int l...
def findLargestd ( S , n ) : found = False S.sort ( ) for i in range ( n - 1 , - 1 , - 1 ) : for j in range ( 0 , n ) : if ( i == j ) : continue for k in range ( j + 1 , n ) : if ( i == k ) : continue for l i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLargestd ( int S [ ] , int n ) { bool found = false ; sort ( S , S + n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; for ...
cpp-python-MAXIMUM_MINIMUM_VALUES_ALGEBRAIC_EXPRESSION
cpp-python
e9 int minMaxValues ( int arr [ ] , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { ...
def minMaxValues ( arr , n , m ) : sum = 0 INF = 1000000000 MAX = 50 for i in range ( 0 , ( n + m ) ) : sum += arr [ i ] arr [ i ] += 50 dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( 0 , ( n + m ) ) : fo...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> e9 int minMaxValues ( int arr [ ] , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < ( n + m ) ; i ...
cpp-python-PRINT_POSSIBLE_EDGES_TREE_GIVEN_DIAMETER_HEIGHT_VERTICES
cpp-python
void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { cout << "1 2" << endl ; return ; } cout << "-1" << endl ; return ; } if ( d > 2 * h ) { cout << "-1" << endl ; return ; } for ( int i = 1 ; i <= h ; i ++ ) cout << i << " " << i + 1...
def constructTree ( n , d , h ) : if d == 1 : if n == 2 and h == 1 : print ( "1 2" ) return 0 print ( "-1" ) return 0 if d > 2 * h : print ( "-1" ) return 0 for i in range ( 1 , h + 1 ) : print ( i , " " , i + 1 ) if d > h : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { cout << "1 2" << endl ; return ; } cout << "-1" << endl ; return ; } if ( d > 2 * h ) { cout << "-1" << endl ; return ; } for ( int i = 1 ; i <= h ; i ++ ) cout << i << " " <...
cpp-python-NUMBER_RECTANGLES_NM_GRID
cpp-python
int rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; }
def rectCount ( n , m ) : return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } </Code>
cpp-python-SORTING_USING_TRIVIAL_HASH_FUNCTION
cpp-python
void sortUsingHash ( int a [ ] , int n ) { int max = * std :: max_element ( a , a + n ) ; int hash [ max + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] += 1 ; for ( int i = 0 ; i <= max ; i ++ ) { if ( hash [ i ] ) { for ( int j = 0 ; j < hash...
def sortUsingHash ( a , n ) : Max = max ( a ) Hash = [ 0 ] * ( Max + 1 ) for i in range ( 0 , n ) : Hash [ a [ i ] ] += 1 for i in range ( 0 , Max + 1 ) : if Hash [ i ] != 0 : for j in range ( 0 , Hash [ i ] ) : print ( i , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void sortUsingHash ( int a [ ] , int n ) { int max = * std :: max_element ( a , a + n ) ; int hash [ max + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] += 1 ; for ( int i = 0 ; i <= max ; i ++ ) { if ( hash [ i ] ) { for ( int j = 0 ; j...
cpp-python-SUBARRAY_NO_PAIR_SUM_DIVISIBLE_K
cpp-python
void subarrayDivisibleByK ( int arr [ ] , int n , int k ) { map < int , int > mp ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr [ s ] ...
def subarrayDivisibleByK ( arr , n , k ) : mp = [ 0 ] * 1000 s = 0 e = 0 maxs = 0 maxe = 0 mp [ arr [ 0 ] % k ] = mp [ arr [ 0 ] % k ] + 1 for i in range ( 1 , n ) : mod = arr [ i ] % k while ( mp [ k - mod ] != 0 or ( mod == 0 and mp [ mod ] != 0 ) ) : mp [ arr [ s ] % k ] =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void subarrayDivisibleByK ( int arr [ ] , int n , int k ) { map < int , int > mp ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { mp [ arr...
cpp-python-CHECK_DIVISIBILITY_BINARY_STRING_2K
cpp-python
bool isDivisible ( char str [ ] , int k ) { int n = strlen ( str ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ; return ( c == k ) ; }
def isDivisible ( str , k ) : n = len ( str ) c = 0 for i in range ( 0 , k ) : if ( str [ n - i - 1 ] == '0' ) : c += 1 return ( c == k )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDivisible ( char str [ ] , int k ) { int n = strlen ( str ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ; return ( c == k ) ; } </Code>
cpp-python-NUMBER_DIGITS_REMOVED_MAKE_NUMBER_DIVISIBLE_3
cpp-python
int divisible ( string num ) { int n = num . length ( ) ; int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; retu...
def divisible ( num ) : n = len ( num ) sum = 0 for i in range ( n ) : sum += int ( num [ i ] ) if ( sum % 3 == 0 ) : return 0 if ( n == 1 ) : return - 1 for i in range ( n ) : if ( sum % 3 == int ( num [ i ] ) % 3 ) : return 1 if ( n == 2 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int divisible ( string num ) { int n = num . length ( ) ; int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ;...
cpp-python-SHORTEST_COMMON_SUPERSEQUENCE
cpp-python
int superSeq ( char * X , char * Y , int m , int n ) { if ( ! m ) return n ; if ( ! n ) return m ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; }
def superSeq ( X , Y , m , n ) : if ( not m ) : return n if ( not n ) : return m if ( X [ m - 1 ] == Y [ n - 1 ] ) : return 1 + superSeq ( X , Y , m - 1 , n - 1 ) return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int superSeq ( char * X , char * Y , int m , int n ) { if ( ! m ) return n ; if ( ! n ) return m ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; } </Code>
cpp-python-PROGRAM_SWAP_UPPER_DIAGONAL_ELEMENTS_LOWER_DIAGONAL_ELEMENTS_MATRIX
cpp-python
void swapUpperToLower ( int arr [ n ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ;...
def swapUpperToLower ( arr ) : n = 4 ; for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; for i in range ( 0 , n ) : for j in range ( 0 , n ) : print (...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void swapUpperToLower ( int arr [ n ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int ...
cpp-python-SUM_K_TH_GROUP_ODD_POSITIVE_NUMBERS
cpp-python
int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- ) { sum += cur ; cur += 2 ; } return sum ; }
def kthgroupsum ( k ) : cur = int ( ( k * ( k - 1 ) ) + 1 ) sum = 0 while k : sum += cur cur += 2 k = k - 1 return sum
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- ) { sum += cur ; cur += 2 ; } return sum ; } </Code>
cpp-python-CHECK_GIVEN_MATRIX_IS_MAGIC_SQUARE_OR_NOT
cpp-python
bool isMagicSquare ( int mat [ ] [ N ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0 ; ...
def isMagicSquare ( mat ) : s = 0 for i in range ( 0 , N ) : s = s + mat [ i ] [ i ] s2 = 0 for i in range ( 0 , N ) : s2 = s2 + mat [ i ] [ N - i - 1 ] if ( s != s2 ) : return False for i in range ( 0 , N ) : rowSum = 0 for j in range ( 0 , N ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isMagicSquare ( int mat [ ] [ N ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = 0...
cpp-python-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX_1
cpp-python
bool isScalarMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; }
def isScalarMatrix ( mat ) : for i in range ( 0 , N ) : for j in range ( 0 , N ) : if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : return False for i in range ( 0 , N - 1 ) : if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) : return False return True...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isScalarMatrix ( int mat [ N ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; } </C...
cpp-python-LONGEST_SUBARRAY_SUM_DIVISIBLE_K
cpp-python
int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) { unordered_map < int , int > um ; int mod_arr [ n ] , max = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { if...
def longSubarrWthSumDivByK ( arr , n , k ) : um = { i : 0 for i in range ( 8 ) } mod_arr = [ 0 for i in range ( n ) ] max = 0 curr_sum = 0 for i in range ( n ) : curr_sum += arr [ i ] mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k for i in range ( n ) : if ( mod_arr [ i ] =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) { unordered_map < int , int > um ; int mod_arr [ n ] , max = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ; } for ( int i = 0 ; i < n ; i ++ ) {...
cpp-python-REVERSE_A_STRING_USING_RECURSION
cpp-python
void reverse ( string str ) { if ( str . size ( ) == 0 ) { return ; } reverse ( str . substr ( 1 ) ) ; cout << str [ 0 ] ; }
def reverse ( string ) : if len ( string ) == 0 : return temp = string [ 0 ] reverse ( string [ 1 : ] ) print ( temp , end = '' )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void reverse ( string str ) { if ( str . size ( ) == 0 ) { return ; } reverse ( str . substr ( 1 ) ) ; cout << str [ 0 ] ; } </Code>
cpp-python-BALANCED_EXPRESSIONS_SUCH_THAT_GIVEN_POSITIONS_HAVE_OPENING_BRACKETS
cpp-python
long long arrangeBraces ( int n , int pos [ ] , int k ) { bool h [ N ] ; int dp [ N ] [ N ] ; memset ( h , 0 , sizeof h ) ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < k ; i ++ ) h [ pos [ i ] ] = 1 ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; ...
def arrangeBraces ( n , pos , k ) : h = [ False for i in range ( N ) ] dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ] for i in range ( k ) : h [ pos [ i ] ] = 1 dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , 2 * n + 1 ) : for j in range ( 2 * n + 1 ) : if ( h [ i ] ) :...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long arrangeBraces ( int n , int pos [ ] , int k ) { bool h [ N ] ; int dp [ N ] [ N ] ; memset ( h , 0 , sizeof h ) ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < k ; i ++ ) h [ pos [ i ] ] = 1 ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0...
cpp-python-DIVISIBILITY_BY_12_FOR_A_LARGE_NUMBER
cpp-python
bool isDvisibleBy12 ( string num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num [ num . length ( ) - 1 ] ; if ( d1 % 2 != 0 ) return ( 0 ) ; int d2 = ( int ) num [ num . length ( ) - 2 ] ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num [ i ] ; return ...
def isDvisibleBy12 ( num ) : if ( len ( num ) >= 3 ) : d1 = int ( num [ len ( num ) - 1 ] ) if ( d1 % 2 != 0 ) : return False d2 = int ( num [ len ( num ) - 2 ] ) sum = 0 for i in range ( 0 , len ( num ) ) : sum += int ( num [ i ] ) return ( su...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDvisibleBy12 ( string num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num [ num . length ( ) - 1 ] ; if ( d1 % 2 != 0 ) return ( 0 ) ; int d2 = ( int ) num [ num . length ( ) - 2 ] ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num [ i ] ; ...
cpp-python-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY
cpp-python
int maxDiff ( int arr [ ] , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { bool isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ...
def maxDiff ( arr , n ) : SubsetSum_1 = 0 SubsetSum_2 = 0 for i in range ( 0 , n ) : isSingleOccurance = True for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : isSingleOccurance = False arr [ i ] = arr [ j ] = 0 break ...
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 SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { bool isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = a...
cpp-python-LEXICOGRAPHICALLY_SMALLEST_STRING_OBTAINED_CONCATENATING_ARRAY
cpp-python
string lexSmallest ( string a [ ] , int n ) { sort ( a , a + n , compare ) ; string answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer += a [ i ] ; return answer ; }
def lexSmallest ( a , n ) : a.sort ( reverse = True ) answer = "" for i in range ( n ) : answer += a [ i ] return answer
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string lexSmallest ( string a [ ] , int n ) { sort ( a , a + n , compare ) ; string answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer += a [ i ] ; return answer ; } </Code>
cpp-python-MAXIMUM_WEIGHT_PATH_ENDING_ELEMENT_LAST_ROW_MATRIX
cpp-python
int maxCost ( int mat [ ] [ MAX ] , int N ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; ...
def maxCost ( mat , N ) : dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] for i in range ( 1 , N ) : dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] for i in range ( 1 , N ) : for j in range ( 1 , min ( i + 1 , N ) ) : dp [ i ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxCost ( int mat [ ] [ MAX ] , int N ) { int dp [ N ] [ N ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j ...