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-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX_1
cpp-python
long long int MaximumDecimalValue ( int mat [ ] [ N ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; if ( mat [ 0 ] [ 0 ] == 1 ) dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( mat [ 0 ] [ i ] == 1 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + pow ( 2 , i ) ; else dp [ 0...
def MaximumDecimalValue ( mat , n ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] if ( mat [ 0 ] [ 0 ] == 1 ) : dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n ) : if ( mat [ 0 ] [ i ] == 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 2 ** i else : dp [ 0 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long int MaximumDecimalValue ( int mat [ ] [ N ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; if ( mat [ 0 ] [ 0 ] == 1 ) dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( mat [ 0 ] [ i ] == 1 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + pow ( 2 , i ) ; else...
cpp-python-CHECK_DIVISIBILITY_LARGE_NUMBER_999
cpp-python
bool isDivisible999 ( string num ) { int n = num . length ( ) ; if ( n == 0 && num [ 0 ] == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num [ i ++ ] - '0' ) * 100 ; g...
def isDivisible999 ( num ) : n = len ( num ) ; if ( n == 0 or num [ 0 ] == '0' ) : return true if ( ( n % 3 ) == 1 ) : num = "00" + num if ( ( n % 3 ) == 2 ) : num = "0" + num gSum = 0 for i in range ( 0 , n , 3 ) : group = 0 group += ( ord ( num [ i ] ) -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDivisible999 ( string num ) { int n = num . length ( ) ; if ( n == 0 && num [ 0 ] == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num [ i ++ ] - '0' ) * 100 ...
cpp-python-MAXIMUM_SUM_BITONIC_SUBARRAY
cpp-python
int maxSumBitonicSubArr ( int arr [ ] , int n ) { int msis [ n ] , msds [ n ] ; int max_sum = INT_MIN ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; fo...
def maxSumBitonicSubArr ( arr , n ) : msis = [ None ] * n msds = [ None ] * n max_sum = 0 msis [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : if ( arr [ i ] > arr [ i - 1 ] ) : msis [ i ] = msis [ i - 1 ] + arr [ i ] else : msis [ i ] = arr [ i ] msds [ n - ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSumBitonicSubArr ( int arr [ ] , int n ) { int msis [ n ] , msds [ n ] ; int max_sum = INT_MIN ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ]...
cpp-python-FIND_MINIMUM_NUMBER_DIVIDED_MAKE_NUMBER_PERFECT_SQUARE
cpp-python
int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 ) ans *= 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 ) ans *= i ; } if ...
def findMinNumber ( n ) : count = 0 ans = 1 while n % 2 == 0 : count += 1 n //= 2 if count % 2 is not 0 : ans *= 2 for i in range ( 3 , ( int ) ( math.sqrt ( n ) ) + 1 , 2 ) : count = 0 while n % i == 0 : count += 1 n //= i if c...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 ) ans *= 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 ) ans *= i ; ...
cpp-python-GCD_ELEMENTS_GIVEN_RANGE
cpp-python
int rangeGCD ( int n , int m ) { return ( n == m ) ? n : 1 ; }
def rangeGCD ( n , m ) : return n if ( n == m ) else 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int rangeGCD ( int n , int m ) { return ( n == m ) ? n : 1 ; } </Code>
cpp-python-PROGRAMMING_PUZZLE_ASSIGN_VALUE_WITHOUT_CONTROL_STATEMENT
cpp-python
int assignValue ( int a , int b , bool x ) { int arr [ ] = { a , b } ; return ( arr [ x ] ) ; }
def assignValue ( a , b , x ) : arr = [ a , b ] return ( arr [ x ] )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int assignValue ( int a , int b , bool x ) { int arr [ ] = { a , b } ; return ( arr [ x ] ) ; } </Code>
cpp-python-TRIANGULAR_NUMBERS
cpp-python
bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; }
def isTriangular ( num ) : if ( num < 0 ) : return False sum , n = 0 , 1 while ( sum <= num ) : sum = sum + n if ( sum == num ) : return True n += 1 return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } </Code>
cpp-python-PANGRAM_CHECKING
cpp-python
bool checkPangram ( string & str ) { vector < bool > mark ( 26 , false ) ; int index ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( 'A' <= str [ i ] && str [ i ] <= 'Z' ) index = str [ i ] - 'A' ; else if ( 'a' <= str [ i ] && str [ i ] <= 'z' ) index = str [ i ] - 'a' ; mark [ index ] =...
def checkPangram ( s ) : List = [ ] for i in range ( 26 ) : List.append ( False ) for c in s.lower ( ) : if not c == " " : List [ ord ( c ) - ord ( 'a' ) ] = True for ch in List : if ch == False : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkPangram ( string & str ) { vector < bool > mark ( 26 , false ) ; int index ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( 'A' <= str [ i ] && str [ i ] <= 'Z' ) index = str [ i ] - 'A' ; else if ( 'a' <= str [ i ] && str [ i ] <= 'z' ) index = str [ i ] - 'a' ; mark [ in...
cpp-python-INTERPOLATION_SEARCH
cpp-python
int interpolationSearch ( int arr [ ] , int n , int x ) { int lo = 0 , hi = ( n - 1 ) ; while ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { if ( lo == hi ) { if ( arr [ lo ] == x ) return lo ; return - 1 ; } int pos = lo + ( ( ( double ) ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * (...
def interpolationSearch ( arr , n , x ) : lo = 0 hi = ( n - 1 ) while lo <= hi and x >= arr [ lo ] and x <= arr [ hi ] : if lo == hi : if arr [ lo ] == x : return lo ; return - 1 ; pos = lo + int ( ( ( float ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int interpolationSearch ( int arr [ ] , int n , int x ) { int lo = 0 , hi = ( n - 1 ) ; while ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { if ( lo == hi ) { if ( arr [ lo ] == x ) return lo ; return - 1 ; } int pos = lo + ( ( ( double ) ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ...
cpp-python-FITTING_SHELVES_PROBLEM
cpp-python
void minSpacePreferLarge ( int wall , int m , int n ) { int num_m = 0 , num_n = 0 , min_empty = wall ; int p = 0 , q = 0 , rem ; while ( wall >= n ) { p = wall / m ; rem = wall % m ; if ( rem <= min_empty ) { num_m = p ; num_n = q ; min_empty = rem ; } q += 1 ; wall = wal...
def minSpacePreferLarge ( w , m , n ) : num_m = 0 num_n = 0 rem = w p = 0 q = 0 r = 0 while ( w >= n ) : p = w / m r = w % m if ( r <= rem ) : num_m = p num_n = q rem = r q += 1 w -= n print ( str ( int ( num_m )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void minSpacePreferLarge ( int wall , int m , int n ) { int num_m = 0 , num_n = 0 , min_empty = wall ; int p = 0 , q = 0 , rem ; while ( wall >= n ) { p = wall / m ; rem = wall % m ; if ( rem <= min_empty ) { num_m = p ; num_n = q ; min_empty = rem ; } q += 1 ; wal...
cpp-python-FIND_REPETITIVE_ELEMENT_1_N_1_3
cpp-python
int findRepeating ( int arr [ ] , int n ) { int missingElement = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int element = arr [ abs ( arr [ i ] ) ] ; if ( element < 0 ) { missingElement = arr [ i ] ; break ; } arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ; } return abs ( m...
def findRepeating ( arr , n ) : missingElement = 0 for i in range ( 0 , n ) : element = arr [ abs ( arr [ i ] ) ] if ( element < 0 ) : missingElement = arr [ i ] break arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] return abs ( missingElement )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findRepeating ( int arr [ ] , int n ) { int missingElement = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int element = arr [ abs ( arr [ i ] ) ] ; if ( element < 0 ) { missingElement = arr [ i ] ; break ; } arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ; } return ...
cpp-python-K_LARGESTOR_SMALLEST_ELEMENTS_IN_AN_ARRAY
cpp-python
void kLargest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n , greater < int > ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " " ; }
def kLargest ( arr , k ) : arr.sort ( reverse = True ) for i in range ( k ) : print ( arr [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void kLargest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n , greater < int > ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " " ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_10_0_1_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-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_2
cpp-python
bool isPowerOfFour ( unsigned int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ; }
def isPowerOfFour ( n ) : return ( n != 0 and ( ( n & ( n - 1 ) ) == 0 ) and not ( n & 0xAAAAAAAA ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPowerOfFour ( unsigned int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ; } </Code>
cpp-python-C_PROGRAM_FACTORIAL_NUMBER
cpp-python
unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; }
def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } </Code>
cpp-python-COUNT_POSSIBLE_GROUPS_SIZE_2_3_SUM_MULTIPLE_3
cpp-python
int findgroups ( int arr [ ] , int n ) { int c [ 3 ] = { 0 } , i ; int res = 0 ; for ( i = 0 ; i < n ; i ++ ) c [ arr [ i ] % 3 ] ++ ; res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) ; res += c [ 1 ] * c [ 2 ] ; res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 ; res += ...
def findgroups ( arr , n ) : c = [ 0 , 0 , 0 ] res = 0 for i in range ( 0 , n ) : c [ arr [ i ] % 3 ] += 1 res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) res += c [ 1 ] * c [ 2 ] res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c [ 1 ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findgroups ( int arr [ ] , int n ) { int c [ 3 ] = { 0 } , i ; int res = 0 ; for ( i = 0 ; i < n ; i ++ ) c [ arr [ i ] % 3 ] ++ ; res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) ; res += c [ 1 ] * c [ 2 ] ; res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 ; ...
cpp-python-K_TH_LARGEST_SUM_CONTIGUOUS_SUBARRAY
cpp-python
int kthLargestSum ( int arr [ ] , int n , int k ) { int sum [ n + 1 ] ; sum [ 0 ] = 0 ; sum [ 1 ] = arr [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ; priority_queue < int , vector < int > , greater < int >> Q ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int...
def kthLargestSum ( arr , n , k ) : sum = [ ] sum.append ( 0 ) sum.append ( arr [ 0 ] ) for i in range ( 2 , n + 1 ) : sum.append ( sum [ i - 1 ] + arr [ i - 1 ] ) Q = [ ] heapq.heapify ( Q ) for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : x = sum [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kthLargestSum ( int arr [ ] , int n , int k ) { int sum [ n + 1 ] ; sum [ 0 ] = 0 ; sum [ 1 ] = arr [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ; priority_queue < int , vector < int > , greater < int >> Q ; for ( int i = 1 ; i <= n ; i ++ ) { fo...
cpp-python-FIND_A_SPECIFIC_PAIR_IN_MATRIX_1
cpp-python
int findMaxValue ( int mat [ ] [ N ] ) { int maxValue = INT_MIN ; int maxArr [ N ] [ N ] ; maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] ; int maxv = mat [ N - 1 ] [ N - 1 ] ; for ( int j = N - 2 ; j >= 0 ; j -- ) { if ( mat [ N - 1 ] [ j ] > maxv ) maxv = mat [ N - 1 ] [ j ] ; maxArr [ N -...
def findMaxValue ( mat ) : maxValue = - sys.maxsize - 1 maxArr = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] maxv = mat [ N - 1 ] [ N - 1 ] for j in range ( N - 2 , - 1 , - 1 ) : if ( mat [ N - 1 ] [ j ] > maxv ) : maxv...
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 ; int maxArr [ N ] [ N ] ; maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] ; int maxv = mat [ N - 1 ] [ N - 1 ] ; for ( int j = N - 2 ; j >= 0 ; j -- ) { if ( mat [ N - 1 ] [ j ] > maxv ) maxv = mat [ N - 1 ] [ j ] ; maxAr...
cpp-python-K_TH_PRIME_FACTOR_GIVEN_NUMBER
cpp-python
int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; retur...
def kPrimeFactor ( n , k ) : while ( n % 2 == 0 ) : k = k - 1 n = n / 2 if ( k == 0 ) : return 2 i = 3 while i <= math.sqrt ( n ) : while ( n % i == 0 ) : if ( k == 1 ) : return i k = k - 1 n = n / i i = ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; ...
cpp-python-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_2
cpp-python
void printDistinct ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) == s . end ( ) ) { s . insert ( arr [ i ] ) ; cout << arr [ i ] << " " ; } } }
def printDistinct ( arr , n ) : s = dict ( ) for i in range ( n ) : if ( arr [ i ] not in s.keys ( ) ) : s [ arr [ i ] ] = arr [ i ] print ( arr [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printDistinct ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) == s . end ( ) ) { s . insert ( arr [ i ] ) ; cout << arr [ i ] << " " ; } } } </Code>
cpp-python-PROGRAM_DECIMAL_BINARY_CONVERSION_1
cpp-python
int decToBinary ( int n ) { for ( int i = 31 ; i >= 0 ; i -- ) { int k = n >> i ; if ( k & 1 ) cout << "1" ; else cout << "0" ; } }
def decToBinary ( n ) : for i in range ( 31 , - 1 , - 1 ) : k = n >> i if ( k & 1 ) : print ( "1" , end = "" ) else : print ( "0" , end = "" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int decToBinary ( int n ) { for ( int i = 31 ; i >= 0 ; i -- ) { int k = n >> i ; if ( k & 1 ) cout << "1" ; else cout << "0" ; } } </Code>
cpp-python-SUPER_PRIME
cpp-python
bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = fals...
def SieveOfEratosthenes ( n , isPrime ) : isPrime [ 0 ] = isPrime [ 1 ] = False for i in range ( 2 , n + 1 ) : isPrime [ i ] = True for p in range ( 2 , n + 1 ) : if ( p * p <= n and isPrime [ p ] == True ) : for i in range ( p * 2 , n + 1 , p ) : isPrime [ i ] = ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ]...
cpp-python-COUNT_OPERATIONS_MAKE_STRINGAB_FREE
cpp-python
int abFree ( string s ) { int n = s . length ( ) ; char char_array [ n + 1 ] ; strcpy ( char_array , s . c_str ( ) ) ; int b_count = 0 ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( char_array [ n - i - 1 ] == 'a' ) { res = ( res + b_count ) ; b_count = ( b_count * 2 ) ; } ...
def abFree ( s ) : b_count = 0 res = 0 for i in range ( len ( s ) ) : if s [ ~ i ] == 'a' : res = ( res + b_count ) b_count = ( b_count * 2 ) else : b_count += 1 return res
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int abFree ( string s ) { int n = s . length ( ) ; char char_array [ n + 1 ] ; strcpy ( char_array , s . c_str ( ) ) ; int b_count = 0 ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( char_array [ n - i - 1 ] == 'a' ) { res = ( res + b_count ) ; b_count = ( b_count * 2 ) ; ...
cpp-python-RECURSIVE_SOLUTION_COUNT_SUBSTRINGS_FIRST_LAST_CHARACTERS
cpp-python
int countSubstrs ( string str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str [ i ] == str [ j ] ) res ++ ; return res ; }
def countSubstrs ( str , i , j , n ) : if ( n == 1 ) : return 1 if ( n <= 0 ) : return 0 res = ( countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ) if ( str [ i ] == str [ j ] ) : res += 1 retur...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSubstrs ( string str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str [ i ] == str [ j ] ) res ++ ; return res ; ...
cpp-python-FIND_THE_FIRST_MISSING_NUMBER
cpp-python
int findFirstMissing ( int array [ ] , int start , int end ) { if ( start > end ) return end + 1 ; if ( start != array [ start ] ) return start ; int mid = ( start + end ) / 2 ; if ( array [ mid ] == mid ) return findFirstMissing ( array , mid + 1 , end ) ; return findFirstMissing ( array , start , mid ) ; }
def findFirstMissing ( array , start , end ) : if ( start > end ) : return end + 1 if ( start != array [ start ] ) : return start mid = int ( ( start + end ) / 2 ) if ( array [ mid ] == mid ) : return findFirstMissing ( array , mid + 1 , end ) return findFirstMissing ( array ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findFirstMissing ( int array [ ] , int start , int end ) { if ( start > end ) return end + 1 ; if ( start != array [ start ] ) return start ; int mid = ( start + end ) / 2 ; if ( array [ mid ] == mid ) return findFirstMissing ( array , mid + 1 , end ) ; return findFirstMissing ( array , start , mid...
cpp-python-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER
cpp-python
unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; }
def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } </Code>
cpp-python-MINIMUM_ROTATIONS_UNLOCK_CIRCULAR_LOCK
cpp-python
int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input || unlock_code ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; input /= 10...
def minRotation ( input , unlock_code ) : rotation = 0 while ( input > 0 or unlock_code > 0 ) : input_digit = input % 10 code_digit = unlock_code % 10 rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) input = int ( input / 10 ) u...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input || unlock_code ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; inpu...
cpp-python-LARGEST_SUBSET_WHOSE_ALL_ELEMENTS_ARE_FIBONACCI_NUMBERS
cpp-python
void findFibSubset ( int arr [ ] , int n ) { int max = * std :: max_element ( arr , arr + n ) ; int a = 0 , b = 1 ; unordered_set < int > hash ; hash . insert ( a ) ; hash . insert ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; hash . insert ( b ) ; } for ( int i = 0 ; i <...
def findFibSubset ( arr , n ) : m = max ( arr ) a = 0 b = 1 hash = [ ] hash.append ( a ) hash.append ( b ) while ( b < m ) : c = a + b a = b b = c hash.append ( b ) for i in range ( n ) : if arr [ i ] in hash : print ( arr [ i ] , end =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findFibSubset ( int arr [ ] , int n ) { int max = * std :: max_element ( arr , arr + n ) ; int a = 0 , b = 1 ; unordered_set < int > hash ; hash . insert ( a ) ; hash . insert ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; hash . insert ( b ) ; } for ( int i = 0 ...
cpp-python-MARKOV_MATRIX
cpp-python
bool checkMarkov ( double m [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; }
def checkMarkov ( m ) : for i in range ( 0 , len ( m ) ) : sm = 0 for j in range ( 0 , len ( m [ i ] ) ) : sm = sm + m [ i ] [ j ] if ( sm != 1 ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkMarkov ( double m [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; } </Code>
cpp-python-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY_1
cpp-python
int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; }
def insertSorted ( arr , n , key , capacity ) : if ( n >= capacity ) : return n i = n - 1 while i >= 0 and arr [ i ] > key : arr [ i + 1 ] = arr [ i ] i -= 1 arr [ i + 1 ] = key return ( n + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; } </Code>
cpp-python-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_2
cpp-python
void recursiveReverse ( string & str , int i = 0 ) { int n = str . length ( ) ; if ( i == n / 2 ) return ; swap ( str [ i ] , str [ n - i - 1 ] ) ; recursiveReverse ( str , i + 1 ) ; }
def recursiveReverse ( str , i = 0 ) : n = len ( str ) if i == n // 2 : return str [ i ] , str [ n - i - 1 ] = str [ n - i - 1 ] , str [ i ] recursiveReverse ( str , i + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void recursiveReverse ( string & str , int i = 0 ) { int n = str . length ( ) ; if ( i == n / 2 ) return ; swap ( str [ i ] , str [ n - i - 1 ] ) ; recursiveReverse ( str , i + 1 ) ; } </Code>
cpp-python-CHECK_IF_ARRAY_ELEMENTS_ARE_CONSECUTIVE
cpp-python
bool areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { bool * visited = ( bool * ) calloc ( n , sizeof ( bool ) ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i...
def areConsecutive ( arr , n ) : if ( n < 1 ) : return False Min = min ( arr ) Max = max ( arr ) if ( Max - Min + 1 == n ) : visited = [ False for i in range ( n ) ] for i in range ( n ) : if ( visited [ arr [ i ] - Min ] != False ) : return False ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { bool * visited = ( bool * ) calloc ( n , sizeof ( bool ) ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ ...
cpp-python-MODULUS_TWO_FLOAT_DOUBLE_NUMBERS
cpp-python
double findMod ( double a , double b ) { double mod ; if ( a < 0 ) mod = - a ; else mod = a ; if ( b < 0 ) b = - b ; while ( mod >= b ) mod = mod - b ; if ( a < 0 ) return - mod ; return mod ; }
def findMod ( a , b ) : if ( a < 0 ) : a = - a if ( b < 0 ) : b = - b mod = a while ( mod >= b ) : mod = mod - b if ( a < 0 ) : return - mod return mod
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double findMod ( double a , double b ) { double mod ; if ( a < 0 ) mod = - a ; else mod = a ; if ( b < 0 ) b = - b ; while ( mod >= b ) mod = mod - b ; if ( a < 0 ) return - mod ; return mod ; } </Code>
cpp-python-SUM_OF_ALL_SUBSTRINGS_OF_A_STRING_REPRESENTING_A_NUMBER
cpp-python
int sumOfSubstrings ( string num ) { int n = num . length ( ) ; int sumofdigit [ n ] ; sumofdigit [ 0 ] = toDigit ( num [ 0 ] ) ; int res = sumofdigit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; ...
def sumOfSubstrings ( num ) : n = len ( num ) sumofdigit = [ ] sumofdigit.append ( int ( num [ 0 ] ) ) res = sumofdigit [ 0 ] for i in range ( 1 , n ) : numi = int ( num [ i ] ) sumofdigit.append ( ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ) res += sumofdigit [ i ] ret...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumOfSubstrings ( string num ) { int n = num . length ( ) ; int sumofdigit [ n ] ; sumofdigit [ 0 ] = toDigit ( num [ 0 ] ) ; int res = sumofdigit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ...
cpp-python-FIND_PAIR_WITH_GREATEST_PRODUCT_IN_ARRAY_1
cpp-python
int findGreatest ( int arr [ ] , int n ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; sort ( arr , arr + n ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ ...
def findGreatest ( arr , n ) : m = dict ( ) for i in arr : m [ i ] = m.get ( i , 0 ) + 1 arr = sorted ( arr ) for i in range ( n - 1 , 0 , - 1 ) : j = 0 while ( j < i and arr [ j ] <= sqrt ( arr [ i ] ) ) : if ( arr [ i ] % arr [ j ] == 0 ) : result = ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findGreatest ( int arr [ ] , int n ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; sort ( arr , arr + n ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] %...
cpp-python-FIND_THE_MISSING_NUMBER_1
cpp-python
int getMissingNo ( int a [ ] , int n ) { int i , total = 1 ; for ( i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; }
def getMissingNo ( a , n ) : i , total = 0 , 1 for i in range ( 2 , n + 2 ) : total += i total -= a [ i - 2 ] return total
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getMissingNo ( int a [ ] , int n ) { int i , total = 1 ; for ( i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; } </Code>
cpp-python-NEXT_POWER_OF_2_2
cpp-python
unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; }
def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; } </Code>
cpp-python-C_PROGRAM_ADDITION_TWO_MATRICES
cpp-python
void add ( 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 add ( 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 add ( 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-ENTRINGER_NUMBER_1
cpp-python
int zigzag ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1...
def zigzag ( n , k ) : dp = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int zigzag ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp ...
cpp-python-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE_1
cpp-python
int eggDrop ( int n , int k ) { int eggFloor [ n + 1 ] [ k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i ] [ 1 ] = 1 ; eggFloor [ i ] [ 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 ] [ j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 2 ...
def eggDrop ( n , k ) : eggFloor = [ [ 0 for x in range ( k + 1 ) ] for x in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : eggFloor [ i ] [ 1 ] = 1 eggFloor [ i ] [ 0 ] = 0 for j in range ( 1 , k + 1 ) : eggFloor [ 1 ] [ j ] = j for i in range ( 2 , n + 1 ) : for j in ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int eggDrop ( int n , int k ) { int eggFloor [ n + 1 ] [ k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i ] [ 1 ] = 1 ; eggFloor [ i ] [ 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 ] [ j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for (...
cpp-python-PRINT_A_CLOSEST_STRING_THAT_DOES_NOT_CONTAIN_ADJACENT_DUPLICATES
cpp-python
string noAdjacentDup ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = 'a' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return s ; }
def noAdjacentDup ( s ) : n = len ( s ) for i in range ( 1 , n ) : if ( s [ i ] == s [ i - 1 ] ) : s [ i ] = "a" while ( s [ i ] == s [ i - 1 ] or ( i + 1 < n and s [ i ] == s [ i + 1 ] ) ) : s [ i ] += 1 i += 1 return s
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string noAdjacentDup ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = 'a' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return s ; } </Code>
cpp-python-MODIFY_ARRAY_TO_MAXIMIZE_SUM_OF_ADJACENT_DIFFERENCES
cpp-python
int maximumDifferenceSum ( int arr [ ] , int N ) { int dp [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 ; for ( int i = 0 ; i < ( N - 1 ) ; i ++ ) { dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) ; dp [ i + 1 ] [ 1 ] = max ...
def maximumDifferenceSum ( arr , N ) : dp = [ [ 0 , 0 ] for i in range ( N ) ] for i in range ( N ) : dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 for i in range ( N - 1 ) : dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) dp [ i + 1 ] [ 1 ] = max ( dp [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maximumDifferenceSum ( int arr [ ] , int N ) { int dp [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 ; for ( int i = 0 ; i < ( N - 1 ) ; i ++ ) { dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) ; dp [ i + 1 ] [ 1 ]...
cpp-python-MINIMUM_INSERTIONS_SORT_ARRAY
cpp-python
int minInsertionStepToSortArray ( int arr [ ] , int N ) { int lis [ N ] ; 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 ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; for ( in...
def minInsertionStepToSortArray ( arr , N ) : lis = [ 0 ] * N 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 lis [ i ] < lis [ j ] + 1 ) : lis [ i ] = lis [ j ] + 1 max = 0 for i in ra...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minInsertionStepToSortArray ( int arr [ ] , int N ) { int lis [ N ] ; 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 ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; f...
cpp-python-EFFICIENT_WAY_CHECK_WHETHER_N_TH_FIBONACCI_NUMBER_MULTIPLE_10
cpp-python
bool isMultipleOf10 ( int n ) { return ( n % 15 == 0 ) ; }
def isMultipleOf10 ( n ) : return ( n % 15 == 0 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isMultipleOf10 ( int n ) { return ( n % 15 == 0 ) ; } </Code>
cpp-python-HARDY_RAMANUJAN_THEOREM
cpp-python
int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count...
def exactPrimeFactorCount ( n ) : count = 0 if ( n % 2 == 0 ) : count = count + 1 while ( n % 2 == 0 ) : n = int ( n / 2 ) i = 3 while ( i <= int ( math.sqrt ( n ) ) ) : if ( n % i == 0 ) : count = count + 1 while ( n % i == 0 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; retur...
cpp-python-QUERIES_FOR_CHARACTERS_IN_A_REPEATED_STRING
cpp-python
void query ( char s [ ] , int i , int j ) { int n = strlen ( s ) ; i %= n ; j %= n ; ( s [ i ] == s [ j ] ) ? ( cout << "Yes" << endl ) : ( cout << "No" << endl ) ; }
def query ( s , i , j ) : n = len ( s ) i %= n j %= n print ( "Yes" ) if s [ i ] == s [ j ] else print ( "No" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void query ( char s [ ] , int i , int j ) { int n = strlen ( s ) ; i %= n ; j %= n ; ( s [ i ] == s [ j ] ) ? ( cout << "Yes" << endl ) : ( cout << "No" << endl ) ; } </Code>
cpp-python-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY_1
cpp-python
int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return - 1 ; sort ( arr , arr + n ) ; return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; }
def maxProduct ( arr , n ) : if n < 3 : return - 1 arr.sort ( ) return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return - 1 ; sort ( arr , arr + n ) ; return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; } </Code>
cpp-python-FIND_SUBARRAY_LEAST_AVERAGE
cpp-python
void findMinAvgSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < ...
def findMinAvgSubarray ( arr , n , k ) : if ( n < k ) : return 0 res_index = 0 curr_sum = 0 for i in range ( k ) : curr_sum += arr [ i ] min_sum = curr_sum for i in range ( k , n ) : curr_sum += arr [ i ] - arr [ i - k ] if ( curr_sum < min_sum ) : min_sum = c...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findMinAvgSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr...
cpp-python-COUNT_POSSIBLE_PATHS_SOURCE_DESTINATION_EXACTLY_K_EDGES
cpp-python
int countwalks ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 1 ; if ( k == 1 && graph [ u ] [ v ] ) return 1 ; if ( k <= 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < V ; i ++ ) if ( graph [ u ] [ i ] == 1 ) count += countwalks ( graph , i , v , k - 1 ) ; return...
def countwalks ( graph , u , v , k ) : if ( k == 0 and u == v ) : return 1 if ( k == 1 and graph [ u ] [ v ] ) : return 1 if ( k <= 0 ) : return 0 count = 0 for i in range ( 0 , V ) : if ( graph [ u ] [ i ] == 1 ) : count += countwalks ( graph , i , v , k ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countwalks ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 1 ; if ( k == 1 && graph [ u ] [ v ] ) return 1 ; if ( k <= 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < V ; i ++ ) if ( graph [ u ] [ i ] == 1 ) count += countwalks ( graph , i , v , k - 1 ) ; ...
cpp-python-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY_1
cpp-python
int countPairs ( int arr [ ] , int n ) { int result = 0 ; set < int > Hash ; for ( int i = 0 ; i < n ; i ++ ) Hash . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . find ( product )...
def countPairs ( arr , n ) : result = 0 Hash = set ( ) for i in range ( n ) : Hash.add ( arr [ i ] ) for i in range ( n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] if product in ( Hash ) : result += 1 return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairs ( int arr [ ] , int n ) { int result = 0 ; set < int > Hash ; for ( int i = 0 ; i < n ; i ++ ) Hash . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . find ( pr...
cpp-python-SERIES_LARGEST_GCD_SUM_EQUALS_N
cpp-python
void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { cout << - 1 << endl ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / ...
def print_sequence ( n , k ) : b = int ( n / ( k * ( k + 1 ) / 2 ) ) if b == 0 : print ( "-1" ) else : r = 1 x = 1 while x ** 2 <= n : if n % x != 0 : continue elif x <= b and x > r : r = x elif n / x <= b an...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { cout << - 1 << endl ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r...
cpp-python-SMALLEST_POWER_OF_2_GREATER_THAN_OR_EQUAL_TO_N_2
cpp-python
unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; }
def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; } </Code>
cpp-python-MAXIMIZE_ARRJ_ARRI_ARRL_ARRK_SUCH_THAT_I_J_K_L
cpp-python
- 100000000 int findMaxValue ( int arr [ ] , int n ) { if ( n < 4 ) { printf ( "The array should have atlest 4 elements\n" ) ; return MIN ; } int table1 [ n + 1 ] , table2 [ n ] , table3 [ n - 1 ] , table4 [ n - 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) table1 [ i ] = table2 [ i ] = table3 [ i ] = tab...
def findMaxValue ( arr , n ) : if n < 4 : print ( "The array should have atlest 4 elements" ) return MIN table1 , table2 = [ MIN ] * ( n + 1 ) , [ MIN ] * n table3 , table4 = [ MIN ] * ( n - 1 ) , [ MIN ] * ( n - 2 ) for i in range ( n - 1 , - 1 , - 1 ) : table1 [ i ] = max ( tab...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> - 100000000 int findMaxValue ( int arr [ ] , int n ) { if ( n < 4 ) { printf ( "The array should have atlest 4 elements\n" ) ; return MIN ; } int table1 [ n + 1 ] , table2 [ n ] , table3 [ n - 1 ] , table4 [ n - 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) table1 [ i ] = table2 [ i ] = table3 [ i ...
cpp-python-PRINT_ARRAY_STRINGS_SORTED_ORDER_WITHOUT_COPYING_ONE_STRING_ANOTHER
cpp-python
void printInSortedOrder ( string arr [ ] , int n ) { int index [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) index [ i ] = i ; for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . compare ( arr [ index [ j ] ] ) > 0 )...
def printInSortedOrder ( arr , n ) : index = [ 0 ] * n for i in range ( n ) : index [ i ] = i for i in range ( n - 1 ) : min = i for j in range ( i + 1 , n ) : if ( arr [ index [ min ] ] > arr [ index [ j ] ] ) : min = j if ( min != i ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printInSortedOrder ( string arr [ ] , int n ) { int index [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) index [ i ] = i ; for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . compare ( arr [ index [ j ] ] ...
cpp-python-SQUARED_TRIANGULAR_NUMBER_SUM_CUBES
cpp-python
int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; }
def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n * n n += 1 n -= 1 if _sum == s : return n return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; } </Code>
cpp-python-RECURSION
cpp-python
void printFun ( int test ) { if ( test < 1 ) return ; else { cout << test << " " ; printFun ( test - 1 ) ; cout << test << " " ; return ; } }
def printFun ( test ) : if ( test < 1 ) : return else : print ( test , end = " " ) printFun ( test - 1 ) print ( test , end = " " ) return
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printFun ( int test ) { if ( test < 1 ) return ; else { cout << test << " " ; printFun ( test - 1 ) ; cout << test << " " ; return ; } } </Code>
cpp-python-SHIFT_MATRIX_ELEMENTS_K
cpp-python
void shiftMatrixByK ( int mat [ N ] [ N ] , int k ) { if ( k > N ) { cout << "shifting is not possible" << endl ; return ; } int j = 0 ; while ( j < N ) { for ( int i = k ; i < N ; i ++ ) cout << mat [ j ] [ i ] << " " ; for ( int i = 0 ; i < k ; i ++ ) cout << mat [ j ] [ i ] <<...
def shiftMatrixByK ( mat , k ) : if ( k > N ) : print ( "shifting is"" not possible" ) return j = 0 while ( j < N ) : for i in range ( k , N ) : print ( "{} ".format ( mat [ j ] [ i ] ) , end = "" ) for i in range ( 0 , k ) : print ( "{} ".format ( mat...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void shiftMatrixByK ( int mat [ N ] [ N ] , int k ) { if ( k > N ) { cout << "shifting is not possible" << endl ; return ; } int j = 0 ; while ( j < N ) { for ( int i = k ; i < N ; i ++ ) cout << mat [ j ] [ i ] << " " ; for ( int i = 0 ; i < k ; i ++ ) cout << mat [ j ] [...
cpp-python-SORTING_USING_TRIVIAL_HASH_FUNCTION_1
cpp-python
void sortUsingHash ( int a [ ] , int n ) { int max = * std :: max_element ( a , a + n ) ; int min = abs ( * std :: min_element ( a , a + n ) ) ; int hashpos [ max + 1 ] = { 0 } ; int hashneg [ min + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= ...
def sortUsingHash ( a , n ) : Max = max ( a ) Min = abs ( min ( a ) ) hashpos = [ 0 ] * ( Max + 1 ) hashneg = [ 0 ] * ( Min + 1 ) for i in range ( 0 , n ) : if a [ i ] >= 0 : hashpos [ a [ i ] ] += 1 else : hashneg [ abs ( a [ i ] ) ] += 1 for i in range (...
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 min = abs ( * std :: min_element ( a , a + n ) ) ; int hashpos [ max + 1 ] = { 0 } ; int hashneg [ min + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ ...
cpp-python-FIND_PAIRS_IN_ARRAY_WHOSE_SUMS_ALREADY_EXIST_IN_ARRAY
cpp-python
void findPair ( int arr [ ] , int n ) { bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { cout << arr [ i ] << " " << arr [ j ] << endl ; ...
def findPair ( arr , n ) : found = False for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : for k in range ( 0 , n ) : if ( arr [ i ] + arr [ j ] == arr [ k ] ) : print ( arr [ i ] , arr [ j ] ) found = True if ( found == Fal...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findPair ( int arr [ ] , int n ) { bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { cout << arr [ i ] << " " << arr [ j ] << en...
cpp-python-FIND_PAIR_MAXIMUM_GCD_ARRAY_1
cpp-python
int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = max ( high , arr [ i ] ) ; int count [ high + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { ...
def findMaxGCD ( arr , n ) : high = 0 for i in range ( 0 , n ) : high = max ( high , arr [ i ] ) count = [ 0 ] * ( high + 1 ) for i in range ( 0 , n ) : count [ arr [ i ] ] += 1 counter = 0 for i in range ( high , 0 , - 1 ) : j = i while ( j <= high ) : ...
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 count [ high + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ...
cpp-python-EULERS_CRITERION_CHECK_IF_SQUARE_ROOT_UNDER_MODULO_P_EXISTS
cpp-python
bool squareRootExists ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) if ( ( x * x ) % p == n ) return true ; return false ; }
def squareRootExists ( n , p ) : n = n % p for x in range ( 2 , p , 1 ) : if ( ( x * x ) % p == n ) : return True return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool squareRootExists ( int n , int p ) { n = n % p ; for ( int x = 2 ; x < p ; x ++ ) if ( ( x * x ) % p == n ) return true ; return false ; } </Code>
cpp-python-COUNT_ENTRIES_EQUAL_TO_X_IN_A_SPECIAL_MATRIX
cpp-python
int count ( int n , int x ) { int count = = 0 ; for ( int i = 1 ; i <= n && i <= x ; i ++ ) { if ( x / i <= n && x % i == 0 ) count ++ ; } return count ; }
def count ( n , x ) : cnt = 0 for i in range ( 1 , n + 1 ) : if i <= x : if x // i <= n and x % i == 0 : cnt += 1 return cnt
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int count ( int n , int x ) { int count = = 0 ; for ( int i = 1 ; i <= n && i <= x ; i ++ ) { if ( x / i <= n && x % i == 0 ) count ++ ; } return count ; } </Code>
cpp-python-SIZE_SUBARRAY_MAXIMUM_SUM
cpp-python
int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; ...
def maxSubArraySum ( a , size ) : max_so_far = - maxsize - 1 max_ending_here = 0 start = 0 end = 0 s = 0 for i in range ( 0 , size ) : max_ending_here += a [ i ] if max_so_far < max_ending_here : max_so_far = max_ending_here start = s end = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = INT_MIN , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = ...
cpp-python-SMALLEST_SUBARRAY_WITH_ALL_OCCURRENCES_OF_A_MOST_FREQUENT_ELEMENT
cpp-python
void smallestSubsegment ( int a [ ] , int n ) { unordered_map < int , int > left ; unordered_map < int , int > count ; int mx = 0 ; int mn , strindex ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count [ x ] == 0 ) { left [ x ] = i ; count [ x ] = 1 ; } else count...
def smallestSubsegment ( a , n ) : left = dict ( ) count = dict ( ) mx = 0 mn , strindex = 0 , 0 for i in range ( n ) : x = a [ i ] if ( x not in count.keys ( ) ) : left [ x ] = i count [ x ] = 1 else : count [ x ] += 1 if ( count [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void smallestSubsegment ( int a [ ] , int n ) { unordered_map < int , int > left ; unordered_map < int , int > count ; int mx = 0 ; int mn , strindex ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; if ( count [ x ] == 0 ) { left [ x ] = i ; count [ x ] = 1 ; } els...
cpp-python-FIND_INDEX_MAXIMUM_OCCURRING_ELEMENT_EQUAL_PROBABILITY
cpp-python
void findRandomIndexOfMax ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] += 1 ; int max_element ; int max_so_far = INT_MIN ; for ( pair < int , int > p : freq ) { if ( p . second > max_so_far ) { max_so_far = p . second ; ma...
def findRandomIndexOfMax ( arr , n ) : mp = dict ( ) for i in range ( n ) : if ( arr [ i ] in mp ) : mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1 else : mp [ arr [ i ] ] = 1 max_element = - 323567 max_so_far = - 323567 for p in mp : if ( mp [ p ] > max_so_f...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findRandomIndexOfMax ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] += 1 ; int max_element ; int max_so_far = INT_MIN ; for ( pair < int , int > p : freq ) { if ( p . second > max_so_far ) { max_so_far = p . second ; ...
cpp-python-FIND_MINIMUM_RADIUS_ATLEAST_K_POINT_LIE_INSIDE_CIRCLE
cpp-python
int minRadius ( int k , int x [ ] , int y [ ] , int n ) { int dis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; sort ( dis , dis + n ) ; return dis [ k - 1 ] ; }
def minRadius ( k , x , y , n ) : dis = [ 0 ] * n for i in range ( 0 , n ) : dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] dis.sort ( ) return dis [ k - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minRadius ( int k , int x [ ] , int y [ ] , int n ) { int dis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; sort ( dis , dis + n ) ; return dis [ k - 1 ] ; } </Code>
cpp-python-FIND_THE_ELEMENT_THAT_APPEARS_ONCE_2
cpp-python
int singleNumber ( int a [ ] , int n ) { unordered_set < int > s ( a , a + n ) ; int arr_sum = accumulate ( a , a + n , 0 ) ; int set_sum = accumulate ( s . begin ( ) , s . end ( ) , 0 ) ; return ( 3 * set_sum - arr_sum ) / 2 ; }
def singleNumber ( nums ) : return ( 3 * sum ( set ( nums ) ) - sum ( nums ) ) / 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int singleNumber ( int a [ ] , int n ) { unordered_set < int > s ( a , a + n ) ; int arr_sum = accumulate ( a , a + n , 0 ) ; int set_sum = accumulate ( s . begin ( ) , s . end ( ) , 0 ) ; return ( 3 * set_sum - arr_sum ) / 2 ; } </Code>
cpp-python-QUERIES_COUNTS_ARRAY_ELEMENTS_VALUES_GIVEN_RANGE
cpp-python
int countInRange ( int arr [ ] , int n , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; }
def countInRange ( arr , n , x , y ) : count = 0 ; for i in range ( n ) : if ( arr [ i ] >= x and arr [ i ] <= y ) : count += 1 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countInRange ( int arr [ ] , int n , int x , int y ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ; } return count ; } </Code>
cpp-python-FIND_SMALLEST_NUMBER_WITH_GIVEN_NUMBER_OF_DIGITS_AND_DIGIT_SUM
cpp-python
void findSmallest ( int m , int s ) { if ( s == 0 ) { ( m == 1 ) ? cout << "Smallest number is " << 0 : cout << "Not possible" ; return ; } if ( s > 9 * m ) { cout << "Not possible" ; return ; } int res [ m ] ; s -= 1 ; for ( int i = m - 1 ; i > 0 ; i -- ) { if ( s > 9 ) { re...
def findSmallest ( m , s ) : if ( s == 0 ) : if ( m == 1 ) : print ( "Smallest number is 0" ) else : print ( "Not possible" ) return if ( s > 9 * m ) : print ( "Not possible" ) return res = [ 0 for i in range ( m + 1 ) ] s -= 1 for i in...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findSmallest ( int m , int s ) { if ( s == 0 ) { ( m == 1 ) ? cout << "Smallest number is " << 0 : cout << "Not possible" ; return ; } if ( s > 9 * m ) { cout << "Not possible" ; return ; } int res [ m ] ; s -= 1 ; for ( int i = m - 1 ; i > 0 ; i -- ) { if ( s > 9 ) { ...
cpp-python-POSSIBILITY_OF_A_WORD_FROM_A_GIVEN_SET_OF_CHARACTERS
cpp-python
bool isPresent ( string s , string q ) { int freq [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s [ i ] ] ++ ; for ( int i = 0 ; i < q . length ( ) ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } return tru...
def isPresent ( s , q ) : freq = [ 0 ] * MAX_CHAR for i in range ( 0 , len ( s ) ) : freq [ ord ( s [ i ] ) ] += 1 for i in range ( 0 , len ( q ) ) : freq [ ord ( q [ i ] ) ] -= 1 if ( freq [ ord ( q [ i ] ) ] < 0 ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPresent ( string s , string q ) { int freq [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s [ i ] ] ++ ; for ( int i = 0 ; i < q . length ( ) ; i ++ ) { freq [ q [ i ] ] -- ; if ( freq [ q [ i ] ] < 0 ) return false ; } ret...
cpp-python-SORT_ARRAY_APPLYING_GIVEN_EQUATION
cpp-python
void sortArray ( int arr [ ] , int n , int A , int B , int C ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = A * arr [ i ] * arr [ i ] + B * arr [ i ] + C ; int index , maximum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( maximum < arr [ i ] ) { index = i ; maximum = arr [ i ] ; ...
def sortArray ( arr , n , A , B , C ) : for i in range ( n ) : arr [ i ] = ( A * arr [ i ] * arr [ i ] + B * arr [ i ] + C ) index = - ( sys.maxsize - 1 ) maximum = - ( sys.maxsize - 1 ) for i in range ( n ) : if maximum < arr [ i ] : index = i maximum = arr [ i ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void sortArray ( int arr [ ] , int n , int A , int B , int C ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = A * arr [ i ] * arr [ i ] + B * arr [ i ] + C ; int index , maximum = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( maximum < arr [ i ] ) { index = i ; maximum = arr [ i...
cpp-python-DISTANCE_NEAREST_CELL_1_BINARY_MATRIX
cpp-python
void printDistance ( int mat [ N ] [ M ] ) { int ans [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) ans [ i ] [ j ] = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) for ( int l = 0 ; l ...
def printDistance ( mat ) : global N , M ans = [ [ None ] * M for i in range ( N ) ] for i in range ( N ) : for j in range ( M ) : ans [ i ] [ j ] = 999999999999 for i in range ( N ) : for j in range ( M ) : for k in range ( N ) : for l in range ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printDistance ( int mat [ N ] [ M ] ) { int ans [ N ] [ M ] ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) ans [ i ] [ j ] = INT_MAX ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) { for ( int k = 0 ; k < N ; k ++ ) for ( int l = 0 ;...
cpp-python-FIND_LAST_INDEX_CHARACTER_STRING
cpp-python
int findLastIndex ( string & str , char x ) { int index = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; }
def findLastIndex ( str , x ) : index = - 1 for i in range ( 0 , len ( str ) ) : if str [ i ] == x : index = i return index
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLastIndex ( string & str , char x ) { int index = - 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( str [ i ] == x ) index = i ; return index ; } </Code>
cpp-python-PRINT_TRIPLETS_SORTED_ARRAY_FORM_AP
cpp-python
void printAllAPTriplets ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; if ( s . find ( arr [ i ] - diff ) != s . end ( ) ) cout << arr [ i ] - diff << " " << arr [ i ] <<...
def printAllAPTriplets ( arr , n ) : s = [ ] for i in range ( 0 , n - 1 ) : for j in range ( i + 1 , n ) : diff = arr [ j ] - arr [ i ] if ( ( arr [ i ] - diff ) in arr ) : print ( "{} {} {}".format ( ( arr [ i ] - diff ) , arr [ i ] , arr [ j ] ) , end = "\n" ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printAllAPTriplets ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int diff = arr [ j ] - arr [ i ] ; if ( s . find ( arr [ i ] - diff ) != s . end ( ) ) cout << arr [ i ] - diff << " " << arr [...
cpp-python-REMOVE_BRACKETS_ALGEBRAIC_STRING_CONTAINING_OPERATORS
cpp-python
char * simplify ( string str ) { int len = str . length ( ) ; char * res = new char ( len ) ; int index = 0 , i = 0 ; stack < int > s ; s . push ( 0 ) ; while ( i < len ) { if ( str [ i ] == '+' ) { if ( s . top ( ) == 1 ) res [ index ++ ] = '-' ; if ( s . top ( ) == 0 ) res [ index ++ ] = '...
def simplify ( Str ) : Len = len ( Str ) res = [ None ] * Len index = 0 i = 0 s = [ ] s.append ( 0 ) while ( i < Len ) : if ( Str [ i ] == '+' ) : if ( s [ - 1 ] == 1 ) : res [ index ] = '-' index += 1 if ( s [ - 1 ] == 0 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> char * simplify ( string str ) { int len = str . length ( ) ; char * res = new char ( len ) ; int index = 0 , i = 0 ; stack < int > s ; s . push ( 0 ) ; while ( i < len ) { if ( str [ i ] == '+' ) { if ( s . top ( ) == 1 ) res [ index ++ ] = '-' ; if ( s . top ( ) == 0 ) res [ index +...
cpp-python-CALCULATE_VOLUME_DODECAHEDRON
cpp-python
double vol_of_dodecahedron ( int side ) { return ( ( ( 15 + ( 7 * ( sqrt ( 5 ) ) ) ) / 4 ) * ( pow ( side , 3 ) ) ) ; }
def vol_of_dodecahedron ( side ) : return ( ( ( 15 + ( 7 * ( math.sqrt ( 5 ) ) ) ) / 4 ) * ( math.pow ( side , 3 ) ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double vol_of_dodecahedron ( int side ) { return ( ( ( 15 + ( 7 * ( sqrt ( 5 ) ) ) ) / 4 ) * ( pow ( side , 3 ) ) ) ; } </Code>
cpp-python-WAYS_TRANSFORMING_ONE_STRING_REMOVING_0_CHARACTERS
cpp-python
int countTransformation ( string a , string b ) { int n = a . size ( ) , m = b . size ( ) ; if ( m == 0 ) return 1 ; int dp [ m + 1 ] [ n + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( i == 0 ) { if ( j == 0 ...
def countTransformation ( a , b ) : n = len ( a ) m = len ( b ) if m == 0 : return 1 dp = [ [ 0 ] * ( n + 1 ) for _ in range ( m + 1 ) ] for i in range ( m ) : for j in range ( i , n ) : if i == 0 : if j == 0 : if a [ j ] == b [ i ] : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countTransformation ( string a , string b ) { int n = a . size ( ) , m = b . size ( ) ; if ( m == 0 ) return 1 ; int dp [ m + 1 ] [ n + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( i == 0 ) { if ( ...
cpp-python-SUM_NODES_K_TH_LEVEL_TREE_REPRESENTED_STRING_1
cpp-python
int sumAtKthLevel ( string tree , int k , int & i , int level ) { if ( tree [ i ++ ] == '(' ) { if ( tree [ i ] == ')' ) return 0 ; int sum = 0 ; if ( level == k ) sum = tree [ i ] - '0' ; int leftsum = sumAtKthLevel ( tree , k , ++ i , level + 1 ) ; int rightsum = sumAtKthLevel ( tree , k , ++ i ...
def sumAtKthLevel ( tree , k , i , level ) : if ( tree [ i [ 0 ] ] == '(' ) : i [ 0 ] += 1 if ( tree [ i [ 0 ] ] == ')' ) : return 0 sum = 0 if ( level == k ) : sum = int ( tree [ i [ 0 ] ] ) i [ 0 ] += 1 leftsum = sumAtKthLevel ( tree , k , i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumAtKthLevel ( string tree , int k , int & i , int level ) { if ( tree [ i ++ ] == '(' ) { if ( tree [ i ] == ')' ) return 0 ; int sum = 0 ; if ( level == k ) sum = tree [ i ] - '0' ; int leftsum = sumAtKthLevel ( tree , k , ++ i , level + 1 ) ; int rightsum = sumAtKthLevel ( tree , k ...
cpp-python-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_1
cpp-python
void printRepeating ( int arr [ ] , int size ) { int * count = new int [ sizeof ( int ) * ( size - 2 ) ] ; int i ; cout << " Repeating elements are " ; for ( i = 0 ; i < size ; i ++ ) { if ( count [ arr [ i ] ] == 1 ) cout << arr [ i ] << " " ; else count [ arr [ i ] ] ++ ; } }
def printRepeating ( arr , size ) : count = [ 0 ] * size print ( " Repeating elements are " , end = "" ) for i in range ( 0 , size ) : if ( count [ arr [ i ] ] == 1 ) : print ( arr [ i ] , end = " " ) else : count [ arr [ i ] ] = count [ arr [ i ] ] + 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printRepeating ( int arr [ ] , int size ) { int * count = new int [ sizeof ( int ) * ( size - 2 ) ] ; int i ; cout << " Repeating elements are " ; for ( i = 0 ; i < size ; i ++ ) { if ( count [ arr [ i ] ] == 1 ) cout << arr [ i ] << " " ; else count [ arr [ i ] ] ++ ; } } </Code>
cpp-python-COUNT_PAIRS_DIFFERENCE_EQUAL_K_1
cpp-python
int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; sort ( arr , arr + n ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } return co...
def countPairsWithDiffK ( arr , n , k ) : count = 0 arr.sort ( ) l = 0 r = 0 while r < n : if arr [ r ] - arr [ l ] == k : count += 1 l += 1 r += 1 elif arr [ r ] - arr [ l ] > k : l += 1 else : r += 1 return cou...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; sort ( arr , arr + n ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } re...
cpp-python-DYNAMIC_PROGRAMMING_SUBSET_SUM_PROBLEM_1
cpp-python
bool isSubsetSum ( int set [ ] , int n , int sum ) { bool subset [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; j ++ ) ...
def isSubsetSum ( set , n , sum ) : subset = ( [ [ False for i in range ( sum + 1 ) ] for i in range ( n + 1 ) ] ) for i in range ( n + 1 ) : subset [ i ] [ 0 ] = True for i in range ( 1 , sum + 1 ) : subset [ 0 ] [ i ] = False for i in range ( 1 , n + 1 ) : for j...
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 ) { bool subset [ n + 1 ] [ sum + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) subset [ i ] [ 0 ] = true ; for ( int i = 1 ; i <= sum ; i ++ ) subset [ 0 ] [ i ] = false ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sum ; ...
cpp-python-MAJORITY_ELEMENT_1
cpp-python
void findMajority ( int arr [ ] , int size ) { unordered_map < int , int > m ; for ( int i = 0 ; i < size ; i ++ ) m [ arr [ i ] ] ++ ; int count = 0 ; for ( auto i : m ) { if ( i . second > size / 2 ) { count = 1 ; cout << "Majority found :- " << i . first << endl ; break ; } } ...
def findMajority ( arr , size ) : m = { } for i in range ( size ) : if arr [ i ] in m : m [ arr [ i ] ] += 1 else : m [ arr [ i ] ] = 1 count = 0 for key in m : if m [ key ] > size / 2 : count = 1 print ( "Majority found :-" , key )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findMajority ( int arr [ ] , int size ) { unordered_map < int , int > m ; for ( int i = 0 ; i < size ; i ++ ) m [ arr [ i ] ] ++ ; int count = 0 ; for ( auto i : m ) { if ( i . second > size / 2 ) { count = 1 ; cout << "Majority found :- " << i . first << endl ; break ; ...
cpp-python-DYNAMIC_PROGRAMMING_SET_10_0_1_KNAPSACK_PROBLEM
cpp-python
int knapSack ( int W , int wt [ ] , int val [ ] , int n ) { if ( n == 0 || W == 0 ) return 0 ; if ( wt [ n - 1 ] > W ) return knapSack ( W , wt , val , n - 1 ) ; else return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) ) ; }
def knapSack ( W , wt , val , n ) : if n == 0 or W == 0 : return 0 if ( wt [ n - 1 ] > W ) : return knapSack ( W , wt , val , n - 1 ) else : return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) )
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 ) { if ( n == 0 || W == 0 ) return 0 ; if ( wt [ n - 1 ] > W ) return knapSack ( W , wt , val , n - 1 ) ; else return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) ) ; } </Code>
cpp-python-PRINT_REVERSE_STRING_REMOVING_VOWELS
cpp-python
void replaceOriginal ( string s , int n ) { string r ( n , ' ' ) ; for ( int i = 0 ; i < n ; i ++ ) { r [ i ] = s [ n - 1 - i ] ; if ( s [ i ] != 'a' && s [ i ] != 'e' && s [ i ] != 'i' && s [ i ] != 'o' && s [ i ] != 'u' ) { cout << r [ i ] ; } } cout << endl ; }
def replaceOriginal ( s , n ) : r = [ ' ' ] * n for i in range ( n ) : r [ i ] = s [ n - 1 - i ] if ( s [ i ] != 'a' and s [ i ] != 'e' and s [ i ] != 'i' and s [ i ] != 'o' and s [ i ] != 'u' ) : print ( r [ i ] , end = "" ) print ( )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void replaceOriginal ( string s , int n ) { string r ( n , ' ' ) ; for ( int i = 0 ; i < n ; i ++ ) { r [ i ] = s [ n - 1 - i ] ; if ( s [ i ] != 'a' && s [ i ] != 'e' && s [ i ] != 'i' && s [ i ] != 'o' && s [ i ] != 'u' ) { cout << r [ i ] ; } } cout << endl ; } </Code>
cpp-python-FIND_PAIRS_IN_ARRAY_WHOSE_SUMS_ALREADY_EXIST_IN_ARRAY_1
cpp-python
void findPair ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( arr [ i ] ) ; bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( s . find ( arr [ i ] + arr [ j ] ) != s . end ( ) ) { ...
def findPair ( arr , n ) : s = { i : 1 for i in arr } found = False for i in range ( n ) : for j in range ( i + 1 , n ) : if arr [ i ] + arr [ j ] in s.keys ( ) : print ( arr [ i ] , arr [ j ] ) found = True if found == False : print ( "Not exi...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findPair ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) s . insert ( arr [ i ] ) ; bool found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( s . find ( arr [ i ] + arr [ j ] ) != s . end ( ) ) {...
cpp-python-NUMBER_SUBSTRINGS_STRING
cpp-python
int countNonEmptySubstr ( string str ) { int n = str . length ( ) ; return n * ( n + 1 ) / 2 ; }
def countNonEmptySubstr ( str ) : n = len ( str ) return int ( n * ( n + 1 ) / 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countNonEmptySubstr ( string str ) { int n = str . length ( ) ; return n * ( n + 1 ) / 2 ; } </Code>
cpp-python-NUMBER_DIGITS_PRODUCT_TWO_NUMBERS
cpp-python
int countDigits ( int a , int b ) { int count = 0 ; int p = abs ( a * b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; }
def countDigits ( a , b ) : count = 0 p = abs ( a * b ) if ( p == 0 ) : return 1 while ( p > 0 ) : count = count + 1 p = p // 10 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countDigits ( int a , int b ) { int count = 0 ; int p = abs ( a * b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } </Code>
cpp-python-MAXIMUM_PROFIT_BY_BUYING_AND_SELLING_A_SHARE_AT_MOST_TWICE
cpp-python
int maxProfit ( int price [ ] , int n ) { int * profit = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) profit [ i ] = 0 ; int max_price = price [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( price [ i ] > max_price ) max_price = price [ i ] ; profit [ i ] = max ( profit [ i + 1 ] , ...
def maxProfit ( price , n ) : profit = [ 0 ] * n max_price = price [ n - 1 ] for i in range ( n - 2 , 0 , - 1 ) : if price [ i ] > max_price : max_price = price [ i ] profit [ i ] = max ( profit [ i + 1 ] , max_price - price [ i ] ) min_price = price [ 0 ] for i in range ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxProfit ( int price [ ] , int n ) { int * profit = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) profit [ i ] = 0 ; int max_price = price [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( price [ i ] > max_price ) max_price = price [ i ] ; profit [ i ] = max ( profit [ i +...
cpp-python-COUNT_DERANGEMENTS_PERMUTATION_SUCH_THAT_NO_ELEMENT_APPEARS_IN_ITS_ORIGINAL_POSITION_1
cpp-python
int countDer ( int n ) { int der [ n + 1 ] ; der [ 0 ] = 1 ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; }
def countDer ( n ) : der = [ 0 for i in range ( n + 1 ) ] der [ 0 ] = 1 der [ 1 ] = 0 der [ 2 ] = 1 for i in range ( 3 , n + 1 ) : der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) return der [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countDer ( int n ) { int der [ n + 1 ] ; der [ 0 ] = 1 ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; } </Code>
cpp-python-MAXIMUM_PATH_SUM_MATRIX
cpp-python
int findMaxPath ( int mat [ ] [ M ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( j > 0 && j < M - 1 ) mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ] [ j - 1 ] , mat [ i - 1 ] [ j + 1 ] ) ) ; else if ( j > 0 ) mat [ i ] [ j ] += max ( ma...
def findMaxPath ( mat ) : res = - 1 for i in range ( M ) : res = max ( res , mat [ 0 ] [ i ] ) for i in range ( 1 , N ) : res = - 1 for j in range ( M ) : if ( j > 0 and j < M - 1 ) : mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ] [ j -...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMaxPath ( int mat [ ] [ M ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( j > 0 && j < M - 1 ) mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ] [ j - 1 ] , mat [ i - 1 ] [ j + 1 ] ) ) ; else if ( j > 0 ) mat [ i ] [ j ] += m...
cpp-python-CHECK_WHETHER_ARITHMETIC_PROGRESSION_CAN_FORMED_GIVEN_ARRAY
cpp-python
bool checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; }
def checkIsAP ( arr , n ) : if ( n == 1 ) : return True arr.sort ( ) d = arr [ 1 ] - arr [ 0 ] for i in range ( 2 , n ) : if ( arr [ i ] - arr [ i - 1 ] != d ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; sort ( arr , arr + n ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; } </Code>
cpp-python-CHECK_LARGE_NUMBER_DIVISIBLE_11_NOT
cpp-python
int check ( string str ) { int n = str . length ( ) ; int oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - '0' ) ; else evenDigSum += ( str [ i ] - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; }
def check ( st ) : n = len ( st ) oddDigSum = 0 evenDigSum = 0 for i in range ( 0 , n ) : if ( i % 2 == 0 ) : oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) ) else : evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) ) return ( ( oddDigSum - evenDigSum ) % 11 ==...
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 oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - '0' ) ; else evenDigSum += ( str [ i ] - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ; } </Code>
cpp-python-SWAP_TWO_NUMBERS_WITHOUT_USING_TEMPORARY_VARIABLE
cpp-python
void swap ( int * xp , int * yp ) { * xp = * xp ^ * yp ; * yp = * xp ^ * yp ; * xp = * xp ^ * yp ; }
def swap ( xp , yp ) : xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ] yp [ 0 ] = xp [ 0 ] ^ yp [ 0 ] xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void swap ( int * xp , int * yp ) { * xp = * xp ^ * yp ; * yp = * xp ^ * yp ; * xp = * xp ^ * yp ; } </Code>
cpp-python-AREA_SQUARE_CIRCUMSCRIBED_CIRCLE
cpp-python
int find_Area ( int r ) { return ( 2 * r * r ) ; }
def find_Area ( r ) : return ( 2 * r * r )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int find_Area ( int r ) { return ( 2 * r * r ) ; } </Code>
cpp-python-PROGRAM_TO_CHECK_IF_A_GIVEN_NUMBER_IS_LUCKY_ALL_DIGITS_ARE_DIFFERENT
cpp-python
bool isLucky ( int n ) { bool arr [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) arr [ i ] = false ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; }
def isLucky ( n ) : ar = [ 0 ] * 10 while ( n > 0 ) : digit = math.floor ( n % 10 ) if ( ar [ digit ] ) : return 0 ar [ digit ] = 1 n = n / 10 return 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isLucky ( int n ) { bool arr [ 10 ] ; for ( int i = 0 ; i < 10 ; i ++ ) arr [ i ] = false ; while ( n > 0 ) { int digit = n % 10 ; if ( arr [ digit ] ) return false ; arr [ digit ] = true ; n = n / 10 ; } return true ; } </Code>
cpp-python-COUNT_NUMBER_WAYS_JUMP_REACH_END
cpp-python
void countWaysToJump ( int arr [ ] , int n ) { int count_jump [ n ] ; memset ( count_jump , 0 , sizeof ( count_jump ) ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ; for ( int j = i + 1 ; j < n - 1 && j <= arr [ i ] + i ; j ++ ) if ( count_jump [...
def countWaysToJump ( arr , n ) : count_jump = [ 0 for i in range ( n ) ] for i in range ( n - 2 , - 1 , - 1 ) : if ( arr [ i ] >= n - i - 1 ) : count_jump [ i ] += 1 j = i + 1 while ( j < n - 1 and j <= arr [ i ] + i ) : if ( count_jump [ j ] != - 1 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void countWaysToJump ( int arr [ ] , int n ) { int count_jump [ n ] ; memset ( count_jump , 0 , sizeof ( count_jump ) ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ; for ( int j = i + 1 ; j < n - 1 && j <= arr [ i ] + i ; j ++ ) if ( count...
cpp-python-PROGRAM_DISPLAY_ASTROLOGICAL_SIGN_ZODIAC_SIGN_GIVEN_DATE_BIRTH
cpp-python
void zodiac_sign ( int day , string month ) { string astro_sign = "" ; if ( month == "december" ) { if ( day < 22 ) astro_sign = "Sagittarius" ; else astro_sign = "capricorn" ; } else if ( month == "january" ) { if ( day < 20 ) astro_sign = "Capricorn" ; else astro_sign = "aquarius" ; } else...
def zodiac_sign ( day , month ) : if month == 'december' : astro_sign = 'Sagittarius' if ( day < 22 ) else 'capricorn' elif month == 'january' : astro_sign = 'Capricorn' if ( day < 20 ) else 'aquarius' elif month == 'february' : astro_sign = 'Aquarius' if ( day < 19 ) else 'pisces' ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void zodiac_sign ( int day , string month ) { string astro_sign = "" ; if ( month == "december" ) { if ( day < 22 ) astro_sign = "Sagittarius" ; else astro_sign = "capricorn" ; } else if ( month == "january" ) { if ( day < 20 ) astro_sign = "Capricorn" ; else astro_sign = "aquarius" ; }...
cpp-python-FIND_NUMBER_OF_SOLUTIONS_OF_A_LINEAR_EQUATION_OF_N_VARIABLES_1
cpp-python
int countSol ( int coeff [ ] , int n , int rhs ) { int dp [ rhs + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; }
def countSol ( coeff , n , rhs ) : dp = [ 0 for i in range ( rhs + 1 ) ] dp [ 0 ] = 1 for i in range ( n ) : for j in range ( coeff [ i ] , rhs + 1 ) : dp [ j ] += dp [ j - coeff [ i ] ] return dp [ rhs ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSol ( int coeff [ ] , int n , int rhs ) { int dp [ rhs + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = coeff [ i ] ; j <= rhs ; j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ; return dp [ rhs ] ; } </Code>
cpp-python-PROGRAM_TO_CHECK_IF_A_MATRIX_IS_SYMMETRIC
cpp-python
bool isSymmetric ( int mat [ ] [ MAX ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) return false ; return true ; }
def isSymmetric ( mat , N ) : for i in range ( N ) : for j in range ( N ) : if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isSymmetric ( int mat [ ] [ MAX ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) return false ; return true ; } </Code>
cpp-python-FIND_RECTANGLE_BINARY_MATRIX_CORNERS_1
cpp-python
bool isRectangle ( const vector < vector < int >> & m ) { int rows = m . size ( ) ; if ( rows == 0 ) return false ; int columns = m [ 0 ] . size ( ) ; for ( int y1 = 0 ; y1 < rows ; y1 ++ ) for ( int x1 = 0 ; x1 < columns ; x1 ++ ) if ( m [ y1 ] [ x1 ] == 1 ) for ( int y2 = y1 + 1 ; y2 < rows ; y2 +...
def isRectangle ( m ) : rows = len ( m ) if ( rows == 0 ) : return False columns = len ( m [ 0 ] ) for y1 in range ( rows ) : for x1 in range ( columns ) : if ( m [ y1 ] [ x1 ] == 1 ) : for y2 in range ( y1 + 1 , rows ) : for x2 in range ( ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isRectangle ( const vector < vector < int >> & m ) { int rows = m . size ( ) ; if ( rows == 0 ) return false ; int columns = m [ 0 ] . size ( ) ; for ( int y1 = 0 ; y1 < rows ; y1 ++ ) for ( int x1 = 0 ; x1 < columns ; x1 ++ ) if ( m [ y1 ] [ x1 ] == 1 ) for ( int y2 = y1 + 1 ; y2 < rows ;...