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-DIVISIBILITY_9_USING_BITWISE_OPERATORS
cpp-python
bool isDivBy9 ( int n ) { if ( n == 0 || n == 9 ) return true ; if ( n < 9 ) return false ; return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) ) ; }
def isDivBy9 ( n ) : if ( n == 0 or n == 9 ) : return True if ( n < 9 ) : return False return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isDivBy9 ( int n ) { if ( n == 0 || n == 9 ) return true ; if ( n < 9 ) return false ; return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) ) ; } </Code>
cpp-python-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-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_2
cpp-python
void printRepeating ( int arr [ ] , int size ) { int Xor = arr [ 0 ] ; int set_bit_no ; int i ; int n = size - 2 ; int x = 0 , y = 0 ; for ( i = 1 ; i < size ; i ++ ) Xor ^= arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) Xor ^= i ; set_bit_no = Xor & ~ ( Xor - 1 ) ; for ( i = 0 ; i < size ; i +...
def printRepeating ( arr , size ) : xor = arr [ 0 ] n = size - 2 x = 0 y = 0 for i in range ( 1 , size ) : xor ^= arr [ i ] for i in range ( 1 , n + 1 ) : xor ^= i set_bit_no = xor & ~ ( xor - 1 ) for i in range ( 0 , size ) : if ( arr [ i ] & set_bit_no ) : ...
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 Xor = arr [ 0 ] ; int set_bit_no ; int i ; int n = size - 2 ; int x = 0 , y = 0 ; for ( i = 1 ; i < size ; i ++ ) Xor ^= arr [ i ] ; for ( i = 1 ; i <= n ; i ++ ) Xor ^= i ; set_bit_no = Xor & ~ ( Xor - 1 ) ; for ( i = 0 ; i < size ...
cpp-python-COUNT_DISTINCT_OCCURRENCES_AS_A_SUBSEQUENCE
cpp-python
int findSubsequenceCount ( string S , string T ) { int m = T . length ( ) , n = S . length ( ) ; if ( m > n ) return 0 ; int mat [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= m ; i ++ )...
def findSubsequenceCount ( S , T ) : m = len ( T ) n = len ( S ) if m > n : return 0 mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ] for i in range ( 1 , m + 1 ) : mat [ i ] [ 0 ] = 0 for j in range ( n + 1 ) : mat [ 0 ] [ j ] = 1 for i in range ( 1...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSubsequenceCount ( string S , string T ) { int m = T . length ( ) , n = S . length ( ) ; if ( m > n ) return 0 ; int mat [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= m ; ...
cpp-python-RECURSIVELY_BREAK_NUMBER_3_PARTS_GET_MAXIMUM_SUM_1
cpp-python
int breakSum ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = 0 , dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ; return dp [ n ] ; }
def breakSum ( n ) : dp = [ 0 ] * ( n + 1 ) dp [ 0 ] = 0 dp [ 1 ] = 1 for i in range ( 2 , n + 1 ) : dp [ i ] = max ( dp [ int ( i / 2 ) ] + dp [ int ( i / 3 ) ] + dp [ int ( i / 4 ) ] , i ) return dp [ n ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int breakSum ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = 0 , dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ; return dp [ n ] ; } </Code>
cpp-python-PROGRAM_DECIMAL_HEXADECIMAL_CONVERSION
cpp-python
void decToHexa ( int n ) { char hexaDeciNum [ 100 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = temp + 48 ; i ++ ; } else { hexaDeciNum [ i ] = temp + 55 ; i ++ ; } n = n / 16 ; } for ( int j = i - 1...
def decToHexa ( n ) : hexaDeciNum = [ '0' ] * 100 i = 0 while ( n != 0 ) : temp = 0 temp = n % 16 if ( temp < 10 ) : hexaDeciNum [ i ] = chr ( temp + 48 ) i = i + 1 else : hexaDeciNum [ i ] = chr ( temp + 55 ) i = i + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void decToHexa ( int n ) { char hexaDeciNum [ 100 ] ; int i = 0 ; while ( n != 0 ) { int temp = 0 ; temp = n % 16 ; if ( temp < 10 ) { hexaDeciNum [ i ] = temp + 48 ; i ++ ; } else { hexaDeciNum [ i ] = temp + 55 ; i ++ ; } n = n / 16 ; } for ( int j ...
cpp-python-STEINS_ALGORITHM_FOR_FINDING_GCD_1
cpp-python
int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( ~ a & 1 ) { if ( b & 1 ) return gcd ( a >> 1 , b ) ; else return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ~ b & 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; ...
def gcd ( a , b ) : if ( a == b ) : return a if ( a == 0 ) : return b if ( b == 0 ) : return a if ( ( ~ a & 1 ) == 1 ) : if ( ( b & 1 ) == 1 ) : return gcd ( a >> 1 , b ) else : return ( gcd ( a >> 1 , b >> 1 ) << 1 ) if ( ( ~ b & 1 ) =...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( ~ a & 1 ) { if ( b & 1 ) return gcd ( a >> 1 , b ) ; else return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ~ b & 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 ,...
cpp-python-DETECT_IF_TWO_INTEGERS_HAVE_OPPOSITE_SIGNS
cpp-python
bool oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; }
def oppositeSigns ( x , y ) : return ( ( x ^ y ) < 0 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } </Code>
cpp-python-PROGRAM_CHECK_ARRAY_SORTED_NOT_ITERATIVE_RECURSIVE_1
cpp-python
bool arraySortedOrNot ( int arr [ ] , int n ) { if ( n == 0 || n == 1 ) return true ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ; return true ; }
def arraySortedOrNot ( arr , n ) : if ( n == 0 or n == 1 ) : return True for i in range ( 1 , n ) : if ( arr [ i - 1 ] > arr [ i ] ) : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool arraySortedOrNot ( int arr [ ] , int n ) { if ( n == 0 || n == 1 ) return true ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ; return true ; } </Code>
cpp-python-CONVERT_STRING_BINARY_SEQUENCE
cpp-python
void strToBinary ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { int val = int ( s [ i ] ) ; string bin = "" ; while ( val > 0 ) { ( val % 2 ) ? bin . push_back ( '1' ) : bin . push_back ( '0' ) ; val /= 2 ; } reverse ( bin . begin ( ) , bin . end ( ) ...
def strToBinary ( s ) : bin_conv = [ ] for c in s : ascii_val = ord ( c ) binary_val = bin ( ascii_val ) bin_conv.append ( binary_val [ 2 : ] ) return ( ' '.join ( bin_conv ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void strToBinary ( string s ) { int n = s . length ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { int val = int ( s [ i ] ) ; string bin = "" ; while ( val > 0 ) { ( val % 2 ) ? bin . push_back ( '1' ) : bin . push_back ( '0' ) ; val /= 2 ; } reverse ( bin . begin ( ) , bin . e...
cpp-python-GENERATE_PYTHAGORAS_TRIPLET_SINGLE_INTEGER
cpp-python
void evaluate ( long long int n ) { if ( n == 1 || n == 2 ) printf ( "No Pythagoras Triplet exists" ) ; else if ( n % 2 == 0 ) { long long int var = 1LL * n * n / 4 ; printf ( "Pythagoras Triplets exist i.e. " ) ; printf ( "%lld %lld %lld" , n , var - 1 , var + 1 ) ; } else if ( n % 2 != 0 ) { l...
def evaluate ( n ) : if ( n == 1 or n == 2 ) : print ( "No Pythagoras" + " Triplet exists" ) elif ( n % 2 == 0 ) : var = n * n / 4 print ( "Pythagoras Triplets" + " exist i.e." , end = "" ) print ( int ( n ) , "" , int ( var - 1 ) , "" , int ( var + 1 ) ) elif ( n % 2 != 0 ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void evaluate ( long long int n ) { if ( n == 1 || n == 2 ) printf ( "No Pythagoras Triplet exists" ) ; else if ( n % 2 == 0 ) { long long int var = 1LL * n * n / 4 ; printf ( "Pythagoras Triplets exist i.e. " ) ; printf ( "%lld %lld %lld" , n , var - 1 , var + 1 ) ; } else if ( n % 2 != 0 ) ...
cpp-python-CHECK_INTEGER_OVERFLOW_MULTIPLICATION
cpp-python
bool isOverflow ( long long a , long long b ) { if ( a == 0 || b == 0 ) return false ; long long result = a * b ; if ( a == result / b ) return false ; else return true ; }
def isOverflow ( a , b ) : if ( a == 0 or b == 0 ) : return False result = a * b if ( result >= 9223372036854775807 or result <= - 9223372036854775808 ) : result = 0 if ( a == ( result // b ) ) : print ( result // b ) return False else : return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isOverflow ( long long a , long long b ) { if ( a == 0 || b == 0 ) return false ; long long result = a * b ; if ( a == result / b ) return false ; else return true ; } </Code>
cpp-python-FIND_EXPRESSION_DUPLICATE_PARENTHESIS_NOT
cpp-python
bool findDuplicateparenthesis ( string str ) { stack < char > Stack ; for ( char ch : str ) { if ( ch == ')' ) { char top = Stack . top ( ) ; Stack . pop ( ) ; int elementsInside = 0 ; while ( top != '(' ) { elementsInside ++ ; top = Stack . top ( ) ; Stack . pop ...
def findDuplicateparenthesis ( string ) : Stack = [ ] for ch in string : if ch == ')' : top = Stack.pop ( ) elementsInside = 0 while top != '(' : elementsInside += 1 top = Stack.pop ( ) if elementsInside < 1 : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool findDuplicateparenthesis ( string str ) { stack < char > Stack ; for ( char ch : str ) { if ( ch == ')' ) { char top = Stack . top ( ) ; Stack . pop ( ) ; int elementsInside = 0 ; while ( top != '(' ) { elementsInside ++ ; top = Stack . top ( ) ; Stack...
cpp-python-COUNT_ROTATIONS_DIVISIBLE_4
cpp-python
int countRotations ( string n ) { int len = n . length ( ) ; if ( len == 1 ) { int oneDigit = n . at ( 0 ) - '0' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } int twoDigit , count = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n . at ( i ) - '0' ) * 10 + ( n . at ( i ...
def countRotations ( n ) : l = len ( n ) if ( l == 1 ) : oneDigit = ( int ) ( n [ 0 ] ) if ( oneDigit % 4 == 0 ) : return 1 return 0 count = 0 for i in range ( 0 , l - 1 ) : twoDigit = ( int ) ( n [ i ] ) * 10 + ( int ) ( n [ i + 1 ] ) if ( twoDigit % ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countRotations ( string n ) { int len = n . length ( ) ; if ( len == 1 ) { int oneDigit = n . at ( 0 ) - '0' ; if ( oneDigit % 4 == 0 ) return 1 ; return 0 ; } int twoDigit , count = 0 ; for ( int i = 0 ; i < ( len - 1 ) ; i ++ ) { twoDigit = ( n . at ( i ) - '0' ) * 10 + ( n . ...
cpp-python-FIND_DISTINCT_INTEGERS_FOR_A_TRIPLET_WITH_GIVEN_PRODUCT
cpp-python
void findTriplets ( int x ) { vector < int > fact ; unordered_set < int > factors ; for ( int i = 2 ; i <= sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . push_back ( i ) ; if ( x / i != i ) fact . push_back ( x / i ) ; factors . insert ( i ) ; factors . insert ( x / i ) ; } ...
def findTriplets ( x ) : fact = [ ] factors = set ( ) for i in range ( 2 , int ( sqrt ( x ) ) ) : if ( x % i == 0 ) : fact.append ( i ) if ( x / i != i ) : fact.append ( x // i ) factors.add ( i ) factors.add ( x // i ) found = Fals...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findTriplets ( int x ) { vector < int > fact ; unordered_set < int > factors ; for ( int i = 2 ; i <= sqrt ( x ) ; i ++ ) { if ( x % i == 0 ) { fact . push_back ( i ) ; if ( x / i != i ) fact . push_back ( x / i ) ; factors . insert ( i ) ; factors . insert ( x / i ) ; ...
cpp-python-PRINT_WAYS_BREAK_STRING_BRACKET_FORM
cpp-python
void findCombinations ( string str , int index , string out ) { if ( index == str . length ( ) ) cout << out << endl ; for ( int i = index ; i < str . length ( ) ; i ++ ) findCombinations ( str , i + 1 , out + "(" + str . substr ( index , i + 1 - index ) + ")" ) ; }
def findCombinations ( string , index , out ) : if index == len ( string ) : print ( out ) for i in range ( index , len ( string ) , 1 ) : findCombinations ( string , i + 1 , out + "(" + string [ index : i + 1 ] + ")" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findCombinations ( string str , int index , string out ) { if ( index == str . length ( ) ) cout << out << endl ; for ( int i = index ; i < str . length ( ) ; i ++ ) findCombinations ( str , i + 1 , out + "(" + str . substr ( index , i + 1 - index ) + ")" ) ; } </Code>
cpp-python-EFFICIENT_SEARCH_IN_AN_ARRAY_WHERE_DIFFERENCE_BETWEEN_ADJACENT_IS_1
cpp-python
int search ( int arr [ ] , int n , int x ) { int i = 0 ; while ( i <= n - 1 ) { if ( arr [ i ] == x ) return i ; i += abs ( arr [ i ] - x ) ; } return - 1 ; }
def search ( arr , n , x ) : i = 0 while ( i <= n - 1 ) : if ( arr [ i ] == x ) : return i i += abs ( arr [ i ] - x ) 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 = 0 ; while ( i <= n - 1 ) { if ( arr [ i ] == x ) return i ; i += abs ( arr [ i ] - x ) ; } return - 1 ; } </Code>
cpp-python-FIND_ELEMENTS_PRESENT_FIRST_ARRAY_NOT_SECOND_1
cpp-python
void findMissing ( int a [ ] , int b [ ] , int n , int m ) { unordered_set < int > s ; for ( int i = 0 ; i < m ; i ++ ) s . insert ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s . find ( a [ i ] ) == s . end ( ) ) cout << a [ i ] << " " ; }
def findMissing ( a , b , n , m ) : s = dict ( ) for i in range ( m ) : s [ b [ i ] ] = 1 for i in range ( n ) : if a [ i ] not in s.keys ( ) : print ( a [ i ] , end = " " )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findMissing ( int a [ ] , int b [ ] , int n , int m ) { unordered_set < int > s ; for ( int i = 0 ; i < m ; i ++ ) s . insert ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( s . find ( a [ i ] ) == s . end ( ) ) cout << a [ i ] << " " ; } </Code>
cpp-python-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS_1
cpp-python
int countPairs ( string str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; ( i + j ) < n && j <= MAX_CHAR ; j ++ ) if ( ( abs ( str [ i + j ] - str [ i ] ) == j ) ) result ++ ; return result ; }
def countPairs ( str1 ) : result = 0 ; n = len ( str1 ) for i in range ( 0 , n ) : for j in range ( 1 , MAX_CHAR + 1 ) : if ( ( i + j ) < n ) : if ( ( abs ( ord ( str1 [ i + j ] ) - ord ( str1 [ 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 = 1 ; ( i + j ) < n && j <= MAX_CHAR ; j ++ ) if ( ( abs ( str [ i + j ] - str [ i ] ) == j ) ) result ++ ; return result ; } </Code>
cpp-python-FIND_MAXIMUM_SUM_POSSIBLE_EQUAL_SUM_THREE_STACKS
cpp-python
int maxSum ( int stack1 [ ] , int stack2 [ ] , int stack3 [ ] , int n1 , int n2 , int n3 ) { int sum1 = 0 , sum2 = 0 , sum3 = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) sum1 += stack1 [ i ] ; for ( int i = 0 ; i < n2 ; i ++ ) sum2 += stack2 [ i ] ; for ( int i = 0 ; i < n3 ; i ++ ) sum3 += stack3 [ i ] ;...
def maxSum ( stack1 , stack2 , stack3 , n1 , n2 , n3 ) : sum1 , sum2 , sum3 = 0 , 0 , 0 for i in range ( n1 ) : sum1 += stack1 [ i ] for i in range ( n2 ) : sum2 += stack2 [ i ] for i in range ( n3 ) : sum3 += stack3 [ i ] top1 , top2 , top3 = 0 , 0 , 0 ans = 0 while ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSum ( int stack1 [ ] , int stack2 [ ] , int stack3 [ ] , int n1 , int n2 , int n3 ) { int sum1 = 0 , sum2 = 0 , sum3 = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) sum1 += stack1 [ i ] ; for ( int i = 0 ; i < n2 ; i ++ ) sum2 += stack2 [ i ] ; for ( int i = 0 ; i < n3 ; i ++ ) sum3 += stack3 ...
cpp-python-MINIMUM_SUM_TWO_NUMBERS_FORMED_DIGITS_ARRAY
cpp-python
int solve ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; }
def solve ( arr , n ) : arr.sort ( ) a = 0 b = 0 for i in range ( n ) : if ( i % 2 != 0 ) : a = a * 10 + arr [ i ] else : b = b * 10 + arr [ i ] return a + b
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int solve ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int a = 0 , b = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i & 1 ) a = a * 10 + arr [ i ] ; else b = b * 10 + arr [ i ] ; } return a + b ; } </Code>
cpp-python-FIND_FIRST_NATURAL_NUMBER_WHOSE_FACTORIAL_DIVISIBLE_X
cpp-python
int firstFactorialDivisibleNumber ( int x ) { int i = 1 ; int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; }
def firstFactorialDivisibleNumber ( x ) : i = 1 fact = 1 for i in range ( 1 , x ) : fact = fact * i if ( fact % x == 0 ) : break return i
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int firstFactorialDivisibleNumber ( int x ) { int i = 1 ; int fact = 1 ; for ( i = 1 ; i < x ; i ++ ) { fact = fact * i ; if ( fact % x == 0 ) break ; } return i ; } </Code>
cpp-python-COUNT_MINIMUM_STEPS_GET_GIVEN_DESIRED_ARRAY
cpp-python
int countMinOperations ( unsigned int target [ ] , int n ) { int result = 0 ; while ( 1 ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( target [ i ] & 1 ) break ; else if ( target [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ; i...
def countMinOperations ( target , n ) : result = 0 while ( True ) : zero_count = 0 i = 0 while ( i < n ) : if ( ( target [ i ] & 1 ) > 0 ) : break elif ( target [ i ] == 0 ) : zero_count += 1 i += 1 if ( zero_cou...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countMinOperations ( unsigned int target [ ] , int n ) { int result = 0 ; while ( 1 ) { int zero_count = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( target [ i ] & 1 ) break ; else if ( target [ i ] == 0 ) zero_count ++ ; } if ( zero_count == n ) return result ...
cpp-python-SORT_ARRAY_CONTAIN_1_N_VALUES
cpp-python
void sortit ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = i + 1 ; } }
def sortit ( arr , n ) : for i in range ( n ) : arr [ i ] = i + 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void sortit ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = i + 1 ; } } </Code>
cpp-python-CHANGE_BITS_CAN_MADE_ONE_FLIP
cpp-python
bool canMakeAllSame ( string str ) { int zeros = 0 , ones = 0 ; for ( char ch : str ) ( ch == '0' ) ? ++ zeros : ++ ones ; return ( zeros == 1 || ones == 1 ) ; }
def canMakeAllSame ( str ) : zeros = 0 ones = 0 for i in range ( 0 , len ( str ) ) : ch = str [ i ] if ( ch == '0' ) : zeros = zeros + 1 else : ones = ones + 1 return ( zeros == 1 or ones == 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool canMakeAllSame ( string str ) { int zeros = 0 , ones = 0 ; for ( char ch : str ) ( ch == '0' ) ? ++ zeros : ++ ones ; return ( zeros == 1 || ones == 1 ) ; } </Code>
cpp-python-SUM_ELEMENTS_K1TH_K2TH_SMALLEST_ELEMENTS_1
cpp-python
void minheapify ( int a [ ] , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { swap ( a [ small ] , a [ index ] ) ; minheapify ( a , small ) ; ...
def minheapify ( a , index ) : small = index l = 2 * index + 1 r = 2 * index + 2 if ( l < n and a [ l ] < a [ small ] ) : small = l if ( r < n and a [ r ] < a [ small ] ) : small = r if ( small != index ) : ( a [ small ] , a [ index ] ) = ( a [ index ] , a [ small ] ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void minheapify ( int a [ ] , int index ) { int small = index ; int l = 2 * index + 1 ; int r = 2 * index + 2 ; if ( l < n && a [ l ] < a [ small ] ) small = l ; if ( r < n && a [ r ] < a [ small ] ) small = r ; if ( small != index ) { swap ( a [ small ] , a [ index ] ) ; minheapify ( a , sma...
cpp-python-FIND_MINIMUM_SHIFT_LONGEST_COMMON_PREFIX
cpp-python
void KMP ( int m , int n , string str2 , string str1 ) { int pos = 0 , len = 0 ; int p [ m + 1 ] ; int k = 0 ; p [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && str1 [ k ] != str1 [ i - 1 ] ) k = p [ k ] ; if ( str1 [ k ] == str1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } for ( in...
def KMP ( m , n , str2 , str1 ) : pos = 0 Len = 0 p = [ 0 for i in range ( m + 1 ) ] k = 0 for i in range ( 2 , n + 1 ) : while ( k > 0 and str1 [ k ] != str1 [ i - 1 ] ) : k = p [ k ] if ( str1 [ k ] == str1 [ i - 1 ] ) : k += 1 p [ i ] = k j = 0 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void KMP ( int m , int n , string str2 , string str1 ) { int pos = 0 , len = 0 ; int p [ m + 1 ] ; int k = 0 ; p [ 1 ] = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { while ( k > 0 && str1 [ k ] != str1 [ i - 1 ] ) k = p [ k ] ; if ( str1 [ k ] == str1 [ i - 1 ] ) ++ k ; p [ i ] = k ; } f...
cpp-python-SWAP_MAJOR_MINOR_DIAGONALS_SQUARE_MATRIX
cpp-python
void swapDiagonal ( int matrix [ ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) swap ( matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] ) ; }
def swapDiagonal ( matrix ) : for i in range ( N ) : matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] = \ matrix [ i ] [ N - i - 1 ] , matrix [ i ] [ i ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void swapDiagonal ( int matrix [ ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) swap ( matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] ) ; } </Code>
cpp-python-REPRESENT_NUMBER_SUM_MINIMUM_POSSIBLE_PSUEDOBINARY_NUMBERS
cpp-python
void psuedoBinary ( int n ) { while ( n > 0 ) { int temp = n , m = 0 , p = 1 ; while ( temp ) { int rem = temp % 10 ; temp = temp / 10 ; if ( rem != 0 ) m += p ; p *= 10 ; } cout << m << " " ; n = n - m ; } }
def psuedoBinary ( n ) : while ( n > 0 ) : temp = n m = 0 p = 1 while ( temp ) : rem = temp % 10 temp = int ( temp / 10 ) if ( rem != 0 ) : m += p p *= 10 print ( m , end = " " ) n = n - m
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void psuedoBinary ( int n ) { while ( n > 0 ) { int temp = n , m = 0 , p = 1 ; while ( temp ) { int rem = temp % 10 ; temp = temp / 10 ; if ( rem != 0 ) m += p ; p *= 10 ; } cout << m << " " ; n = n - m ; } } </Code>
cpp-python-MINIMUM_NUMBER_POINTS_REMOVED_GET_REMAINING_POINTS_ONE_SIDE_AXIS
cpp-python
int findmin ( Point p [ ] , int n ) { int a = 0 , b = 0 , c = 0 , d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] . x <= 0 ) a ++ ; else if ( p [ i ] . x >= 0 ) b ++ ; if ( p [ i ] . y >= 0 ) c ++ ; else if ( p [ i ] . y <= 0 ) d ++ ; } return min ( { a , b , c , d } ) ; } ...
def findmin ( p , n ) : a , b , c , d = 0 , 0 , 0 , 0 for i in range ( n ) : if ( p [ i ] [ 0 ] <= 0 ) : a += 1 elif ( p [ i ] [ 0 ] >= 0 ) : b += 1 if ( p [ i ] [ 1 ] >= 0 ) : c += 1 elif ( p [ i ] [ 1 ] <= 0 ) : d += 1 return ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findmin ( Point p [ ] , int n ) { int a = 0 , b = 0 , c = 0 , d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] . x <= 0 ) a ++ ; else if ( p [ i ] . x >= 0 ) b ++ ; if ( p [ i ] . y >= 0 ) c ++ ; else if ( p [ i ] . y <= 0 ) d ++ ; } return min ( { a , b , c , d } )...
cpp-python-FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_CHANGE_1
cpp-python
int minCoins ( int coins [ ] , int m , int V ) { int table [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = INT_MAX ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) if ( coins [ j ] <= i ) { int sub_res = table [ i - coins [ j ] ] ; ...
def minCoins ( coins , m , V ) : table = [ 0 for i in range ( V + 1 ) ] table [ 0 ] = 0 for i in range ( 1 , V + 1 ) : table [ i ] = sys.maxsize for i in range ( 1 , V + 1 ) : for j in range ( m ) : if ( coins [ j ] <= i ) : sub_res = table [ i - coins [ j ] ]...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minCoins ( int coins [ ] , int m , int V ) { int table [ V + 1 ] ; table [ 0 ] = 0 ; for ( int i = 1 ; i <= V ; i ++ ) table [ i ] = INT_MAX ; for ( int i = 1 ; i <= V ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) if ( coins [ j ] <= i ) { int sub_res = table [ i - coins [ j ]...
cpp-python-MAKE_LARGEST_PALINDROME_CHANGING_K_DIGITS
cpp-python
string maximumPalinUsingKChanges ( string str , int k ) { string palin = str ; int l = 0 ; int r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { palin [ l ] = palin [ r ] = max ( str [ l ] , str [ r ] ) ; k -- ; } l ++ ; r -- ; } if ( k < 0 ) return "No...
def maximumPalinUsingKChanges ( strr , k ) : palin = strr l = 0 r = len ( strr ) - 1 while ( l <= r ) : if ( strr [ l ] != strr [ r ] ) : palin [ l ] = palin [ r ] = max ( strr [ l ] , strr [ r ] ) k -= 1 l += 1 r -= 1 if ( k < 0 ) : return "No...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string maximumPalinUsingKChanges ( string str , int k ) { string palin = str ; int l = 0 ; int r = str . length ( ) - 1 ; while ( l < r ) { if ( str [ l ] != str [ r ] ) { palin [ l ] = palin [ r ] = max ( str [ l ] , str [ r ] ) ; k -- ; } l ++ ; r -- ; } if ( k < 0 ) ret...
cpp-python-MINIMUM_ROOMS_FOR_M_EVENTS_OF_N_BATCHES_WITH_GIVEN_SCHEDULE
cpp-python
int findMinRooms ( string slots [ ] , int n , int m ) { int counts [ m ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( slots [ i ] [ j ] == '1' ) counts [ j ] ++ ; return * max_element ( counts , counts + m ) ; }
def findMinRooms ( slots , n , m ) : counts = [ 0 ] * m for i in range ( n ) : for j in range ( m ) : if ( slots [ i ] [ j ] == '1' ) : counts [ j ] += 1 return max ( counts )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinRooms ( string slots [ ] , int n , int m ) { int counts [ m ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( slots [ i ] [ j ] == '1' ) counts [ j ] ++ ; return * max_element ( counts , counts + m ) ; } </Code>
cpp-python-MAXIMUM_PATH_SUM_POSITION_JUMPS_DIVISIBILITY_CONDITION
cpp-python
void printMaxSum ( int arr [ ] , int n ) { int dp [ n ] ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; int maxi = 0 ; for ( int j = 1 ; j <= sqrt ( i + 1 ) ; j ++ ) { if ( ( ( i + 1 ) % j == 0 ) && ( i + 1 ) != j ) { if ( dp [ j - ...
def printMaxSum ( arr , n ) : dp = [ 0 for i in range ( n ) ] for i in range ( n ) : dp [ i ] = arr [ i ] maxi = 0 for j in range ( 1 , int ( ( i + 1 ) ** 0.5 ) + 1 ) : if ( ( i + 1 ) % j == 0 and ( i + 1 ) != j ) : if ( dp [ j - 1 ] > maxi ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printMaxSum ( int arr [ ] , int n ) { int dp [ n ] ; memset ( dp , 0 , sizeof dp ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; int maxi = 0 ; for ( int j = 1 ; j <= sqrt ( i + 1 ) ; j ++ ) { if ( ( ( i + 1 ) % j == 0 ) && ( i + 1 ) != j ) { if ( dp...
cpp-python-MAXIMUM_SUM_ALTERNATING_SUBSEQUENCE_SUM
cpp-python
int maxAlternateSum ( int arr [ ] , int n ) { if ( n == 1 ) return arr [ 0 ] ; int dec [ n ] ; memset ( dec , 0 , sizeof ( dec ) ) ; int inc [ n ] ; memset ( inc , 0 , sizeof ( inc ) ) ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j...
def maxAlternateSum ( arr , n ) : if ( n == 1 ) : return arr [ 0 ] dec = [ 0 for i in range ( n + 1 ) ] inc = [ 0 for i in range ( n + 1 ) ] dec [ 0 ] = inc [ 0 ] = arr [ 0 ] flag = 0 for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ j ] > arr [ i ] ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxAlternateSum ( int arr [ ] , int n ) { if ( n == 1 ) return arr [ 0 ] ; int dec [ n ] ; memset ( dec , 0 , sizeof ( dec ) ) ; int inc [ n ] ; memset ( inc , 0 , sizeof ( inc ) ) ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ...
cpp-python-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY
cpp-python
void printRepeating ( int arr [ ] , int size ) { int i , j ; printf ( " Repeating elements are " ) ; for ( i = 0 ; i < size ; i ++ ) for ( j = i + 1 ; j < size ; j ++ ) if ( arr [ i ] == arr [ j ] ) cout << arr [ i ] << " " ; }
def printRepeating ( arr , size ) : print ( "Repeating elements are " , end = '' ) for i in range ( 0 , size ) : for j in range ( i + 1 , size ) : if arr [ i ] == arr [ j ] : print ( arr [ i ] , end = ' ' )
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 i , j ; printf ( " Repeating elements are " ) ; for ( i = 0 ; i < size ; i ++ ) for ( j = i + 1 ; j < size ; j ++ ) if ( arr [ i ] == arr [ j ] ) cout << arr [ i ] << " " ; } </Code>
cpp-python-MINIMUM_PRODUCT_K_INTEGERS_ARRAY_POSITIVE_INTEGERS
cpp-python
int minProduct ( int arr [ ] , int n , int k ) { priority_queue < int , vector < int > , greater < int >> pq ; for ( int i = 0 ; i < n ; i ++ ) pq . push ( arr [ i ] ) ; int count = 0 , ans = 1 ; while ( pq . empty ( ) == false && count < k ) { ans = ans * pq . top ( ) ; pq . pop ( ) ; count ++ ...
def minProduct ( arr , n , k ) : heapq.heapify ( arr ) count = 0 ans = 1 while ( arr ) and count < k : x = heapq.heappop ( arr ) ans = ans * x count = count + 1 return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minProduct ( int arr [ ] , int n , int k ) { priority_queue < int , vector < int > , greater < int >> pq ; for ( int i = 0 ; i < n ; i ++ ) pq . push ( arr [ i ] ) ; int count = 0 , ans = 1 ; while ( pq . empty ( ) == false && count < k ) { ans = ans * pq . top ( ) ; pq . pop ( ) ; co...
cpp-python-C_PROGRAM_FIND_LARGEST_ELEMENT_ARRAY_1
cpp-python
int largest ( int arr [ ] , int n ) { return * max_element ( arr , arr + n ) ; }
def largest ( arr , n ) : return max ( arr )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int largest ( int arr [ ] , int n ) { return * max_element ( arr , arr + n ) ; } </Code>
cpp-python-PROGRAM_TO_EFFICIENTLY_CALCULATE_EX
cpp-python
float exponential ( int n , float x ) { float sum = 1.0f ; for ( int i = n - 1 ; i > 0 ; -- i ) sum = 1 + x * sum / i ; return sum ; }
def exponential ( n , x ) : sum = 1.0 for i in range ( n , 0 , - 1 ) : sum = 1 + x * sum / i print ( "e^x =" , sum )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float exponential ( int n , float x ) { float sum = 1.0f ; for ( int i = n - 1 ; i > 0 ; -- i ) sum = 1 + x * sum / i ; return sum ; } </Code>
cpp-python-FIRST_ELEMENT_OCCURRING_K_TIMES_ARRAY
cpp-python
int firstElement ( int arr [ ] , int n , int k ) { unordered_map < int , int > count_map ; for ( int i = 0 ; i < n ; i ++ ) count_map [ arr [ i ] ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( count_map [ arr [ i ] ] == k ) return arr [ i ] ; return - 1 ; }
def firstElement ( arr , n , k ) : count_map = { } for i in range ( 0 , n ) : if ( arr [ i ] in count_map.keys ( ) ) : count_map [ arr [ i ] ] += 1 else : count_map [ arr [ i ] ] = 1 i += 1 for i in range ( 0 , n ) : if ( count_map [ arr [ i ] ] == k )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int firstElement ( int arr [ ] , int n , int k ) { unordered_map < int , int > count_map ; for ( int i = 0 ; i < n ; i ++ ) count_map [ arr [ i ] ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( count_map [ arr [ i ] ] == k ) return arr [ i ] ; return - 1 ; } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_36_CUT_A_ROPE_TO_MAXIMIZE_PRODUCT_1
cpp-python
int maxProd ( int n ) { if ( n == 2 || n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; }
def maxProd ( n ) : if ( n == 2 or n == 3 ) : return ( n - 1 ) res = 1 while ( n > 4 ) : n -= 3 res *= 3 return ( n * res )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxProd ( int n ) { if ( n == 2 || n == 3 ) return ( n - 1 ) ; int res = 1 ; while ( n > 4 ) { n -= 3 ; res *= 3 ; } return ( n * res ) ; } </Code>
cpp-python-MAXIMUM_SUM_2_X_N_GRID_NO_TWO_ELEMENTS_ADJACENT
cpp-python
int maxSum ( int grid [ 2 ] [ MAX ] , int n ) { int incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = max ( excl , incl ) ; incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ; excl = excl_new ; } return max ( ex...
def maxSum ( grid , n ) : incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) excl = 0 for i in range ( 1 , n ) : excl_new = max ( excl , incl ) incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) excl = excl_new return max ( excl , incl )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxSum ( int grid [ 2 ] [ MAX ] , int n ) { int incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ; int excl = 0 , excl_new ; for ( int i = 1 ; i < n ; i ++ ) { excl_new = max ( excl , incl ) ; incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ; excl = excl_new ; } return m...
cpp-python-FIND_ROTATION_COUNT_ROTATED_SORTED_ARRAY_1
cpp-python
int countRotations ( int arr [ ] , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ]...
def countRotations ( arr , low , high ) : if ( high < low ) : return 0 if ( high == low ) : return low mid = low + ( high - low ) / 2 mid = int ( mid ) if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) : return ( mid + 1 ) if ( mid > low and arr [ mid ] < arr [ mid - 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countRotations ( int arr [ ] , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [...
cpp-python-SUM_MATRIX_ELEMENT_ELEMENT_INTEGER_DIVISION_ROW_COLUMN_1
cpp-python
int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; }
def findSum ( n ) : ans = 0 temp = 0 for i in range ( 1 , n + 1 ) : if temp < n : temp = i - 1 num = 1 while temp < n : if temp + i <= n : ans += i * num else : ans += ( n - temp ) * num ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return a...
cpp-python-CHECK_HALF_STRING_CHARACTER_FREQUENCY_CHARACTER
cpp-python
bool checkCorrectOrNot ( string s ) { int count1 [ MAX_CHAR ] = { 0 } ; int count2 [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count1 [ s [ i ] - 'a' ] ++ ; count...
def checkCorrectOrNot ( s ) : global MAX_CHAR count1 = [ 0 ] * MAX_CHAR count2 = [ 0 ] * MAX_CHAR n = len ( s ) if n == 1 : return true i = 0 j = n - 1 while ( i < j ) : count1 [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1 count2 [ ord ( s [ j ] ) - ord ( 'a' ) ] += 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkCorrectOrNot ( string s ) { int count1 [ MAX_CHAR ] = { 0 } ; int count2 [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count1 [ s [ i ] - 'a' ] ++ ; ...
cpp-python-SUM_FACTORS_NUMBER
cpp-python
int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; }
def divSum ( n ) : result = 0 for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) : if ( n % i == 0 ) : if ( i == ( n / i ) ) : result = result + i else : result = result + ( i + n // i ) return ( result + n + 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int divSum ( int n ) { int result = 0 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i == ( n / i ) ) result += i ; else result += ( i + n / i ) ; } } return ( result + n + 1 ) ; } </Code>
cpp-python-ROTATE_MATRIX_180_DEGREE
cpp-python
void rotateMatrix ( int mat [ ] [ N ] ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j >= 0 ; j -- ) printf ( "%d " , mat [ i ] [ j ] ) ; printf ( "\n" ) ; } }
def rotateMatrix ( mat ) : i = N - 1 while ( i >= 0 ) : j = N - 1 while ( j >= 0 ) : print ( mat [ i ] [ j ] , end = " " ) j = j - 1 print ( ) i = i - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void rotateMatrix ( int mat [ ] [ N ] ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = N - 1 ; j >= 0 ; j -- ) printf ( "%d " , mat [ i ] [ j ] ) ; printf ( "\n" ) ; } } </Code>
cpp-python-PRINT_MATRIX_SPIRAL_FORM_STARTING_POINT
cpp-python
void printSpiral ( int mat [ ] [ MAX ] , int r , int c ) { int i , a = 0 , b = 2 ; int low_row = ( 0 > a ) ? 0 : a ; int low_column = ( 0 > b ) ? 0 : b - 1 ; int high_row = ( ( a + 1 ) >= r ) ? r - 1 : a + 1 ; int high_column = ( ( b + 1 ) >= c ) ? c - 1 : b + 1 ; while ( ( low_row > 0 - r && low_column > 0...
def printSpiral ( mat , r , c ) : a = 0 b = 2 low_row = 0 if ( 0 > a ) else a low_column = 0 if ( 0 > b ) else b - 1 high_row = r - 1 if ( ( a + 1 ) >= r ) else a + 1 high_column = c - 1 if ( ( b + 1 ) >= c ) else b + 1 while ( ( low_row > 0 - r and low_column > 0 - c ) ) : i = low_c...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSpiral ( int mat [ ] [ MAX ] , int r , int c ) { int i , a = 0 , b = 2 ; int low_row = ( 0 > a ) ? 0 : a ; int low_column = ( 0 > b ) ? 0 : b - 1 ; int high_row = ( ( a + 1 ) >= r ) ? r - 1 : a + 1 ; int high_column = ( ( b + 1 ) >= c ) ? c - 1 : b + 1 ; while ( ( low_row > 0 - r && low_col...
cpp-python-COUNT_EVEN_LENGTH_BINARY_SEQUENCES_WITH_SAME_SUM_OF_FIRST_AND_SECOND_HALF_BITS_1
cpp-python
int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; }
def countSeq ( n ) : nCr = 1 res = 1 for r in range ( 1 , n + 1 ) : nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; return res ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; } </Code>
cpp-python-PROGRAM_CALCULATE_AREA_OCTAGON
cpp-python
double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ; }
def areaOctagon ( side ) : return ( 2 * ( 1 + ( math.sqrt ( 2 ) ) ) * side * side )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ; } </Code>
cpp-python-MAXIMUM_CONSECUTIVE_NUMBERS_PRESENT_ARRAY
cpp-python
int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ) != S ...
def findLongestConseqSubseq ( arr , n ) : S = set ( ) for i in range ( n ) : S.add ( arr [ i ] ) ans = 0 for i in range ( n ) : if S.__contains__ ( arr [ i ] ) : j = arr [ i ] while ( S.__contains__ ( j ) ) : j += 1 ans = max ( ans , j ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ...
cpp-python-SHORTEST_POSSIBLE_COMBINATION_TWO_STRINGS
cpp-python
void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( a [ i...
def printSuperSeq ( a , b ) : m = len ( a ) n = len ( b ) dp = [ [ 0 ] * ( n + 1 ) for i in range ( m + 1 ) ] for i in range ( 0 , m + 1 ) : for j in range ( 0 , n + 1 ) : if not i : dp [ i ] [ j ] = j elif not j : dp [ i ] [ j ] = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ...
cpp-python-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_2
cpp-python
int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; }
def numberOfPaths ( p , q ) : dp = [ 1 for i in range ( q ) ] for i in range ( p - 1 ) : for j in range ( 1 , q ) : dp [ j ] += dp [ j - 1 ] return dp [ q - 1 ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } </Code>
cpp-python-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_1
cpp-python
bool isPowerOfFour ( unsigned int n ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; }
def isPowerOfFour ( n ) : count = 0 if ( n and ( not ( n & ( n - 1 ) ) ) ) : while ( n > 1 ) : n >>= 1 count += 1 if ( count % 2 == 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 isPowerOfFour ( unsigned int n ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } </Code>
cpp-python-FIND_SUM_ODD_FACTORS_NUMBER
cpp-python
int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } re...
def sumofoddFactors ( n ) : res = 1 while n % 2 == 0 : n = n // 2 for i in range ( 3 , int ( math.sqrt ( n ) + 1 ) ) : count = 0 curr_sum = 1 curr_term = 1 while n % i == 0 : count += 1 n = n // i curr_term *= i curr_sum...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; }...
cpp-python-FIND_THE_ELEMENT_BEFORE_WHICH_ALL_THE_ELEMENTS_ARE_SMALLER_THAN_IT_AND_AFTER_WHICH_ALL_ARE_GREATER_THAN_IT
cpp-python
int findElement ( int arr [ ] , int n ) { int leftMax [ n ] ; leftMax [ 0 ] = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( leftMax [ i ] < arr [ i ] && rightMin > arr [ i...
def findElement ( arr , n ) : leftMax = [ None ] * n leftMax [ 0 ] = float ( '-inf' ) for i in range ( 1 , n ) : leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) rightMin = float ( 'inf' ) for i in range ( n - 1 , - 1 , - 1 ) : if leftMax [ i ] < arr [ i ] and rightMin > arr...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findElement ( int arr [ ] , int n ) { int leftMax [ n ] ; leftMax [ 0 ] = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( leftMax [ i ] < arr [ i ] && rightMin > ...
cpp-python-SEARCH_AN_ELEMENT_IN_A_SORTED_AND_PIVOTED_ARRAY
cpp-python
int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; return search ( arr , mid + 1 , h ...
def search ( arr , l , h , key ) : if l > h : return - 1 mid = ( l + h ) // 2 if arr [ mid ] == key : return mid if arr [ l ] <= arr [ mid ] : if key >= arr [ l ] and key <= arr [ mid ] : return search ( arr , l , mid - 1 , key ) return search ( arr , mid + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; return search ( arr , mid +...
cpp-python-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION
cpp-python
long int exponentiation ( long int base , long int exp ) { if ( exp == 0 ) return 1 ; if ( exp == 1 ) return base % N ; long int t = exponentiation ( base , exp / 2 ) ; t = ( t * t ) % N ; if ( exp % 2 == 0 ) return t ; else return ( ( base % N ) * t ) % N ; }
def exponentiation ( bas , exp ) : if ( exp == 0 ) : return 1 if ( exp == 1 ) : return bas % N t = exponentiation ( bas , int ( exp / 2 ) ) t = ( t * t ) % N if ( exp % 2 == 0 ) : return t else : return ( ( bas % N ) * t ) % N
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long int exponentiation ( long int base , long int exp ) { if ( exp == 0 ) return 1 ; if ( exp == 1 ) return base % N ; long int t = exponentiation ( base , exp / 2 ) ; t = ( t * t ) % N ; if ( exp % 2 == 0 ) return t ; else return ( ( base % N ) * t ) % N ; } </Code>
cpp-python-DIVISIBILITY_CHECK
cpp-python
void divisibilityCheck ( int arr [ ] , int n ) { unordered_set < int > s ; int max_ele = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { s . insert ( arr [ i ] ) ; max_ele = max ( max_ele , arr [ i ] ) ; } unordered_set < int > res ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 0 ) ...
def divisibilityCheck ( arr , n ) : s = dict ( ) max_ele = - 10 ** 9 for i in range ( n ) : s [ arr [ i ] ] = 1 max_ele = max ( max_ele , arr [ i ] ) res = dict ( ) for i in range ( n ) : if ( arr [ i ] != 0 ) : for j in range ( arr [ i ] * 2 , max_ele + 1 , arr [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void divisibilityCheck ( int arr [ ] , int n ) { unordered_set < int > s ; int max_ele = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { s . insert ( arr [ i ] ) ; max_ele = max ( max_ele , arr [ i ] ) ; } unordered_set < int > res ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] ...
cpp-python-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_3
cpp-python
void printRepeating ( int arr [ ] , int size ) { int i ; cout << "The repeating elements are" ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ abs ( arr [ i ] ) ] > 0 ) arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ; else cout << " " << abs ( arr [ i ] ) << " " ; } }
def printRepeating ( arr , size ) : print ( " The repeating elements are" , end = " " ) for i in range ( 0 , size ) : if ( arr [ abs ( arr [ i ] ) ] > 0 ) : arr [ abs ( arr [ i ] ) ] = ( - 1 ) * arr [ abs ( arr [ i ] ) ] else : print ( abs ( arr [ i ] ) , end = " " )
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 i ; cout << "The repeating elements are" ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ abs ( arr [ i ] ) ] > 0 ) arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ; else cout << " " << abs ( arr [ i ] ) << " " ; } } </Code>
cpp-python-DYNAMIC_PROGRAMMING_SET_17_PALINDROME_PARTITIONING
cpp-python
int minPalPartion ( char * str ) { int n = strlen ( str ) ; int C [ n ] [ n ] ; bool P [ n ] [ n ] ; int i , j , k , L ; for ( i = 0 ; i < n ; i ++ ) { P [ i ] [ i ] = true ; C [ i ] [ i ] = 0 ; } for ( L = 2 ; L <= n ; L ++ ) { for ( i = 0 ; i < n - L + 1 ; i ++ ) { j = ...
def minPalPartion ( str ) : n = len ( str ) C = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] P = [ [ False for i in range ( n ) ] for i in range ( n ) ] j = 0 k = 0 L = 0 for i in range ( n ) : P [ i ] [ i ] = True C [ i ] [ i ] = 0 for L in range ( 2 , n + 1 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minPalPartion ( char * str ) { int n = strlen ( str ) ; int C [ n ] [ n ] ; bool P [ n ] [ n ] ; int i , j , k , L ; for ( i = 0 ; i < n ; i ++ ) { P [ i ] [ i ] = true ; C [ i ] [ i ] = 0 ; } for ( L = 2 ; L <= n ; L ++ ) { for ( i = 0 ; i < n - L + 1 ; i ++ ) { ...
cpp-python-HEIGHT_COMPLETE_BINARY_TREE_HEAP_N_NODES
cpp-python
int height ( int N ) { return ceil ( log2 ( N + 1 ) ) - 1 ; }
def height ( N ) : return math.ceil ( math.log2 ( N + 1 ) ) - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int height ( int N ) { return ceil ( log2 ( N + 1 ) ) - 1 ; } </Code>
cpp-python-DISTRIBUTING_ITEMS_PERSON_CANNOT_TAKE_TWO_ITEMS_TYPE_1
cpp-python
bool checkCount ( int arr [ ] , int n , int k ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; for ( auto x : hash ) if ( x . second > 2 * k ) return false ; return true ; }
def checkCount ( arr , n , k ) : mp = defaultdict ( lambda : 0 ) for i in range ( n ) : mp [ arr [ i ] ] += 1 for key , values in mp.items ( ) : if values > 2 * k : return False return True
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool checkCount ( int arr [ ] , int n , int k ) { unordered_map < int , int > hash ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr [ i ] ] ++ ; for ( auto x : hash ) if ( x . second > 2 * k ) return false ; return true ; } </Code>
cpp-python-REMOVE_CONSECUTIVE_DUPLICATES_STRING
cpp-python
void removeDuplicates ( char S [ ] ) { int n = strlen ( S ) ; if ( n < 2 ) return ; int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } j ++ ; S [ j ] = '\0' ; }
def removeDuplicates ( S ) : n = len ( S ) if ( n < 2 ) : return j = 0 for i in range ( n ) : if ( S [ j ] != S [ i ] ) : j += 1 S [ j ] = S [ i ] j += 1 S = S [ : j ] return S
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void removeDuplicates ( char S [ ] ) { int n = strlen ( S ) ; if ( n < 2 ) return ; int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } j ++ ; S [ j ] = '\0' ; } </Code>
cpp-python-THIRD_LARGEST_ELEMENT_ARRAY_DISTINCT_ELEMENTS_1
cpp-python
void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " Invalid Input " ) ; return ; } int first = arr [ 0 ] , second = INT_MIN , third = INT_MIN ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ; ...
def thirdLargest ( arr , arr_size ) : if ( arr_size < 3 ) : print ( " Invalid Input " ) return first = arr [ 0 ] second = - sys.maxsize third = - sys.maxsize for i in range ( 1 , arr_size ) : if ( arr [ i ] > first ) : third = second second = first ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { printf ( " Invalid Input " ) ; return ; } int first = arr [ 0 ] , second = INT_MIN , third = INT_MIN ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { third = second ; second = first ...
cpp-python-LONGEST_INCREASING_ODD_EVEN_SUBSEQUENCE
cpp-python
int longOddEvenIncSeq ( int arr [ ] , int n ) { int lioes [ n ] ; int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) ...
def longOddEvenIncSeq ( arr , n ) : lioes = list ( ) maxLen = 0 for i in range ( n ) : lioes.append ( 1 ) i = 1 for i in range ( n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and ( arr [ i ] + arr [ j ] ) % 2 != 0 and lioes [ i ] < lioes [ j ] + 1 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int longOddEvenIncSeq ( int arr [ ] , int n ) { int lioes [ n ] ; int maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) lioes [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ]...
cpp-python-ADD_1_TO_A_GIVEN_NUMBER_1
cpp-python
int addOne ( int x ) { return ( - ( ~ x ) ) ; }
def addOne ( x ) : return ( - ( ~ x ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int addOne ( int x ) { return ( - ( ~ x ) ) ; } </Code>
cpp-python-NUMBER_DECIMAL_NUMBERS_LENGTH_K_STRICT_MONOTONE
cpp-python
int getNumStrictMonotone ( int len ) { int DP [ len ] [ DP_s ] ; memset ( DP , 0 , sizeof ( DP ) ) ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ] ; re...
def getNumStrictMonotone ( ln ) : DP = [ [ 0 ] * DP_s for _ in range ( ln ) ] for i in range ( DP_s ) : DP [ 0 ] [ i ] = i + 1 for i in range ( 1 , ln ) : for j in range ( 1 , DP_s ) : DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ] return DP [ ln - 1 ] [ DP_s - ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int getNumStrictMonotone ( int len ) { int DP [ len ] [ DP_s ] ; memset ( DP , 0 , sizeof ( DP ) ) ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ]...
cpp-python-SEARCH_ALMOST_SORTED_ARRAY
cpp-python
int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return binary...
def binarySearch ( arr , l , r , x ) : if ( r >= l ) : mid = int ( l + ( r - l ) / 2 ) if ( arr [ mid ] == x ) : return mid if ( mid > l and arr [ mid - 1 ] == x ) : return ( mid - 1 ) if ( mid < r and arr [ mid + 1 ] == x ) : return ( mid + 1 ) if ( a...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return...
cpp-python-BRESENHAMS_LINE_GENERATION_ALGORITHM
cpp-python
void bresenham ( int x1 , int y1 , int x2 , int y2 ) { int m_new = 2 * ( y2 - y1 ) ; int slope_error_new = m_new - ( x2 - x1 ) ; for ( int x = x1 , y = y1 ; x <= x2 ; x ++ ) { cout << "(" << x << "," << y << ")\n" ; slope_error_new += m_new ; if ( slope_error_new >= 0 ) { y ++ ; slope_...
def bresenham ( x1 , y1 , x2 , y2 ) : m_new = 2 * ( y2 - y1 ) slope_error_new = m_new - ( x2 - x1 ) y = y1 for x in range ( x1 , x2 + 1 ) : print ( "(" , x , "," , y , ")\n" ) slope_error_new = slope_error_new + m_new if ( slope_error_new >= 0 ) : y = y + 1 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void bresenham ( int x1 , int y1 , int x2 , int y2 ) { int m_new = 2 * ( y2 - y1 ) ; int slope_error_new = m_new - ( x2 - x1 ) ; for ( int x = x1 , y = y1 ; x <= x2 ; x ++ ) { cout << "(" << x << "," << y << ")\n" ; slope_error_new += m_new ; if ( slope_error_new >= 0 ) { y ++ ; ...
cpp-python-HIGHWAY_BILLBOARD_PROBLEM
cpp-python
int maxRevenue ( int m , int x [ ] , int revenue [ ] , int n , int t ) { int maxRev [ m + 1 ] ; memset ( maxRev , 0 , sizeof ( maxRev ) ) ; int nxtbb = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ; else { if ( i <...
def maxRevenue ( m , x , revenue , n , t ) : maxRev = [ 0 ] * ( m + 1 ) nxtbb = 0 for i in range ( 1 , m + 1 ) : if ( nxtbb < n ) : if ( x [ nxtbb ] != i ) : maxRev [ i ] = maxRev [ i - 1 ] else : if ( i <= t ) : maxRev [ i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxRevenue ( int m , int x [ ] , int revenue [ ] , int n , int t ) { int maxRev [ m + 1 ] ; memset ( maxRev , 0 , sizeof ( maxRev ) ) ; int nxtbb = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ; else { i...
cpp-python-PROGRAM_FIND_LINE_PASSING_2_POINTS
cpp-python
pair < double , double > void lineFromPoints ( pdd P , pdd Q ) { double a = Q . second - P . second ; double b = P . first - Q . first ; double c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { cout << "The line passing through points P and Q is: " << a << "x " << b << "y = " << c << endl ; } ...
def lineFromPoints ( P , Q ) : a = Q [ 1 ] - P [ 1 ] b = P [ 0 ] - Q [ 0 ] c = a * ( P [ 0 ] ) + b * ( P [ 1 ] ) if ( b < 0 ) : print ( "The line passing through points P and Q is:" , a , "x " , b , "y = " , c , "\n" ) else : print ( "The line passing through points P and Q is: " , a...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> pair < double , double > void lineFromPoints ( pdd P , pdd Q ) { double a = Q . second - P . second ; double b = P . first - Q . first ; double c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { cout << "The line passing through points P and Q is: " << a << "x " << b << "y = " << c << endl ;...
cpp-python-CHINESE_REMAINDER_THEOREM_SET_1_INTRODUCTION
cpp-python
int findMinX ( int num [ ] , int rem [ ] , int k ) { int x = 1 ; while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } return x ; }
def findMinX ( num , rem , k ) : x = 1 while ( True ) : j = 0 while ( j < k ) : if ( x % num [ j ] != rem [ j ] ) : break j += 1 if ( j == k ) : return x x += 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findMinX ( int num [ ] , int rem [ ] , int k ) { int x = 1 ; while ( true ) { int j ; for ( j = 0 ; j < k ; j ++ ) if ( x % num [ j ] != rem [ j ] ) break ; if ( j == k ) return x ; x ++ ; } return x ; } </Code>
cpp-python-FIND_A_TRIPLET_THAT_SUM_TO_A_GIVEN_VALUE
cpp-python
bool find3Numbers ( int A [ ] , int arr_size , int sum ) { int l , r ; for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { for ( int j = i + 1 ; j < arr_size - 1 ; j ++ ) { for ( int k = j + 1 ; k < arr_size ; k ++ ) { if ( A [ i ] + A [ j ] + A [ k ] == sum ) { cout <<...
def find3Numbers ( A , arr_size , sum ) : for i in range ( 0 , arr_size - 2 ) : for j in range ( i + 1 , arr_size - 1 ) : for k in range ( j + 1 , arr_size ) : if A [ i ] + A [ j ] + A [ k ] == sum : print ( "Triplet is" , A [ i ] , ", " , A [ j ] , ", " , A [...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool find3Numbers ( int A [ ] , int arr_size , int sum ) { int l , r ; for ( int i = 0 ; i < arr_size - 2 ; i ++ ) { for ( int j = i + 1 ; j < arr_size - 1 ; j ++ ) { for ( int k = j + 1 ; k < arr_size ; k ++ ) { if ( A [ i ] + A [ j ] + A [ k ] == sum ) { ...
cpp-python-K_MAXIMUM_SUM_COMBINATIONS_TWO_ARRAYS
cpp-python
void KMaxCombinations ( int A [ ] , int B [ ] , int N , int K ) { priority_queue < int > pq ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) pq . push ( A [ i ] + B [ j ] ) ; int count = 0 ; while ( count < K ) { cout << pq . top ( ) << endl ; pq . pop ( ) ; count ++ ; }...
def KMaxCombinations ( A , B , N , K ) : pq = PriorityQueue ( ) for i in range ( 0 , N ) : for j in range ( 0 , N ) : a = A [ i ] + B [ j ] pq.put ( ( - a , a ) ) count = 0 while ( count < K ) : print ( pq.get ( ) [ 1 ] ) count = count + 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void KMaxCombinations ( int A [ ] , int B [ ] , int N , int K ) { priority_queue < int > pq ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) pq . push ( A [ i ] + B [ j ] ) ; int count = 0 ; while ( count < K ) { cout << pq . top ( ) << endl ; pq . pop ( ) ; count +...
cpp-python-TRIANGULAR_MATCHSTICK_NUMBER
cpp-python
int numberOfSticks ( int x ) { return ( 3 * x * ( x + 1 ) ) / 2 ; }
def numberOfSticks ( x ) : return ( 3 * x * ( x + 1 ) ) / 2
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int numberOfSticks ( int x ) { return ( 3 * x * ( x + 1 ) ) / 2 ; } </Code>
cpp-python-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS_1
cpp-python
int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ s [ i ] - 'a' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; ...
def countSubstringWithEqualEnds ( s ) : result = 0 n = len ( s ) count = [ 0 ] * MAX_CHAR for i in range ( n ) : count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1 for i in range ( MAX_CHAR ) : result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) return result
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; int count [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) count [ s [ i ] - 'a' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 )...
cpp-python-DIVISIBILITY_BY_7
cpp-python
int isDivisibleBy7 ( int num ) { if ( num < 0 ) return isDivisibleBy7 ( - num ) ; if ( num == 0 || num == 7 ) return 1 ; if ( num < 10 ) return 0 ; return isDivisibleBy7 ( num / 10 - 2 * ( num - num / 10 * 10 ) ) ; }
def isDivisibleBy7 ( num ) : if num < 0 : return isDivisibleBy7 ( - num ) if ( num == 0 or num == 7 ) : return True if ( num < 10 ) : return False return isDivisibleBy7 ( num // 10 - 2 * ( num - num // 10 * 10 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int isDivisibleBy7 ( int num ) { if ( num < 0 ) return isDivisibleBy7 ( - num ) ; if ( num == 0 || num == 7 ) return 1 ; if ( num < 10 ) return 0 ; return isDivisibleBy7 ( num / 10 - 2 * ( num - num / 10 * 10 ) ) ; } </Code>
cpp-python-FIND_ONE_MULTIPLE_REPEATING_ELEMENTS_READ_ARRAY
cpp-python
int findRepeatingNumber ( const int arr [ ] , int n ) { int sq = sqrt ( n ) ; int range = ( n / sq ) + 1 ; int count [ range ] = { 0 } ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < rang...
def findRepeatingNumber ( arr , n ) : sq = sqrt ( n ) range__ = int ( ( n / sq ) + 1 ) count = [ 0 for i in range ( range__ ) ] for i in range ( 0 , n + 1 , 1 ) : count [ int ( ( arr [ i ] - 1 ) / sq ) ] += 1 selected_block = range__ - 1 for i in range ( 0 , range__ - 1 , 1 ) : i...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findRepeatingNumber ( const int arr [ ] , int n ) { int sq = sqrt ( n ) ; int range = ( n / sq ) + 1 ; int count [ range ] = { 0 } ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i...
cpp-python-FIND_N_TH_ELEMENT_FROM_STERNS_DIATOMIC_SERIES
cpp-python
int findSDSFunc ( int n ) { int DP [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; }
def findSDSFunc ( n ) : DP = [ 0 ] * ( n + 1 ) DP [ 0 ] = 0 DP [ 1 ] = 1 for i in range ( 2 , n + 1 ) : if ( int ( i % 2 ) == 0 ) : DP [ i ] = DP [ int ( i / 2 ) ] else : DP [ i ] = ( DP [ int ( ( i - 1 ) / 2 ) ] + DP [ int ( ( i + 1 ) / 2 ) ] ) return DP [ n ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findSDSFunc ( int n ) { int DP [ n + 1 ] ; DP [ 0 ] = 0 ; DP [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ; else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ; } return DP [ n ] ; } </Code>
cpp-python-RECURSIVE_C_PROGRAM_LINEARLY_SEARCH_ELEMENT_GIVEN_ARRAY
cpp-python
int recSearch ( int arr [ ] , int l , int r , int x ) { if ( r < l ) return - 1 ; if ( arr [ l ] == x ) return l ; if ( arr [ r ] == x ) return r ; return recSearch ( arr , l + 1 , r - 1 , x ) ; }
def recSearch ( arr , l , r , x ) : if r < l : return - 1 if arr [ l ] == x : return l if arr [ r ] == x : return r return recSearch ( arr , l + 1 , r - 1 , x )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int recSearch ( int arr [ ] , int l , int r , int x ) { if ( r < l ) return - 1 ; if ( arr [ l ] == x ) return l ; if ( arr [ r ] == x ) return r ; return recSearch ( arr , l + 1 , r - 1 , x ) ; } </Code>
cpp-python-CHECK_LARGE_NUMBER_DIVISIBLE_4_NOT
cpp-python
bool check ( string str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str [ 0 ] - '0' ) % 4 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; return ( ( second_last * 10 + last ) % 4 == 0 ) ; }
def check ( st ) : n = len ( st ) if ( n == 0 ) : return False if ( n == 1 ) : return ( ( st [ 0 ] - '0' ) % 4 == 0 ) last = ( int ) ( st [ n - 1 ] ) second_last = ( int ) ( st [ n - 2 ] ) return ( ( second_last * 10 + last ) % 4 == 0 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool check ( string str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str [ 0 ] - '0' ) % 4 == 0 ) ; int last = str [ n - 1 ] - '0' ; int second_last = str [ n - 2 ] - '0' ; return ( ( second_last * 10 + last ) % 4 == 0 ) ; } </Code>
cpp-python-LOWER_CASE_UPPER_CASE_INTERESTING_FACT
cpp-python
string to_upper ( string & in ) { for ( int i = 0 ; i < in . length ( ) ; i ++ ) if ( 'a' <= in [ i ] <= 'z' ) in [ i ] = in [ i ] - 'a' + 'A' ; return in ; }
def to_upper ( string ) : for i in range ( len ( string ) ) : if ( 'a' <= string [ i ] <= 'z' ) : string = ( string [ 0 : i ] + chr ( ord ( string [ i ] ) - ord ( 'a' ) + ord ( 'A' ) ) + string [ i + 1 : ] ) return string
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string to_upper ( string & in ) { for ( int i = 0 ; i < in . length ( ) ; i ++ ) if ( 'a' <= in [ i ] <= 'z' ) in [ i ] = in [ i ] - 'a' + 'A' ; return in ; } </Code>
cpp-python-REMOVE_MINIMUM_NUMBER_CHARACTERS_TWO_STRINGS_BECOME_ANAGRAM
cpp-python
int remAnagram ( string str1 , string str2 ) { int count1 [ CHARS ] = { 0 } , count2 [ CHARS ] = { 0 } ; for ( int i = 0 ; str1 [ i ] != '\0' ; i ++ ) count1 [ str1 [ i ] - 'a' ] ++ ; for ( int i = 0 ; str2 [ i ] != '\0' ; i ++ ) count2 [ str2 [ i ] - 'a' ] ++ ;...
def remAnagram ( str1 , str2 ) : count1 = [ 0 ] * CHARS count2 = [ 0 ] * CHARS i = 0 while i < len ( str1 ) : count1 [ ord ( str1 [ i ] ) - ord ( 'a' ) ] += 1 i += 1 i = 0 while i < len ( str2 ) : count2 [ ord ( str2 [ i ] ) - ord ( 'a' ) ] += 1 i += 1 result ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int remAnagram ( string str1 , string str2 ) { int count1 [ CHARS ] = { 0 } , count2 [ CHARS ] = { 0 } ; for ( int i = 0 ; str1 [ i ] != '\0' ; i ++ ) count1 [ str1 [ i ] - 'a' ] ++ ; for ( int i = 0 ; str2 [ i ] != '\0' ; i ++ ) count2 [ str2 [ i ] - 'a'...
cpp-python-SORT_ARRAY_WAVE_FORM_2_1
cpp-python
void sortInWave ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( & arr [ i ] , & arr [ i - 1 ] ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( & arr [ i ] , & arr [ i + 1 ] ) ; } }
def sortInWave ( arr , n ) : for i in range ( 0 , n , 2 ) : if ( i > 0 and arr [ i ] < arr [ i - 1 ] ) : arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ] if ( i < n - 1 and arr [ i ] < arr [ i + 1 ] ) : arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ]
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void sortInWave ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( & arr [ i ] , & arr [ i - 1 ] ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( & arr [ i ] , & arr [ i + 1 ] ) ; } } </Code>
cpp-python-FORM_MINIMUM_NUMBER_FROM_GIVEN_SEQUENCE_1
cpp-python
string getMinNumberForPattern ( string seq ) { int n = seq . length ( ) ; if ( n >= 9 ) return "-1" ; string result ( n + 1 , ' ' ) ; int count = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == n || seq [ i ] == 'I' ) { for ( int j = i - 1 ; j >= - 1 ; j -- ) { result [ j +...
def getMinNumberForPattern ( seq ) : n = len ( seq ) if ( n >= 9 ) : return "-1" result = [ None ] * ( n + 1 ) count = 1 for i in range ( n + 1 ) : if ( i == n or seq [ i ] == 'I' ) : for j in range ( i - 1 , - 2 , - 1 ) : result [ j + 1 ] = int ( '0' + st...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string getMinNumberForPattern ( string seq ) { int n = seq . length ( ) ; if ( n >= 9 ) return "-1" ; string result ( n + 1 , ' ' ) ; int count = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == n || seq [ i ] == 'I' ) { for ( int j = i - 1 ; j >= - 1 ; j -- ) { resul...
cpp-python-CHECK_IF_STRING_REMAINS_PALINDROME_AFTER_REMOVING_GIVEN_NUMBER_OF_CHARACTERS
cpp-python
bool isPossible ( string str , int n ) { int len = str . length ( ) ; if ( len >= n ) return true ; return false ; }
def isPossible ( str , n ) : l = len ( str ) if ( l >= n ) : return True return False
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPossible ( string str , int n ) { int len = str . length ( ) ; if ( len >= n ) return true ; return false ; } </Code>
cpp-python-FIND_CENTER_CIRCLE_USING_ENDPOINTS_DIAMETER
cpp-python
void center ( int x1 , int x2 , int y1 , int y2 ) { cout << ( float ) ( x1 + x2 ) / 2 << ", " << ( float ) ( y1 + y2 ) / 2 ; }
def center ( x1 , x2 , y1 , y2 ) : print ( int ( ( x1 + x2 ) / 2 ) , end = "" ) print ( "," , int ( ( y1 + y2 ) / 2 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void center ( int x1 , int x2 , int y1 , int y2 ) { cout << ( float ) ( x1 + x2 ) / 2 << ", " << ( float ) ( y1 + y2 ) / 2 ; } </Code>
cpp-python-GREEDY_ALGORITHM_TO_FIND_MINIMUM_NUMBER_OF_COINS
cpp-python
void findMin ( int V ) { vector < int > ans ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( V >= deno [ i ] ) { V -= deno [ i ] ; ans . push_back ( deno [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << " " ; }
def findMin ( V ) : deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ] n = len ( deno ) ans = [ ] i = n - 1 while ( i >= 0 ) : while ( V >= deno [ i ] ) : V -= deno [ i ] ans.append ( deno [ i ] ) i -= 1 for i in range ( len ( ans ) ) : print (...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void findMin ( int V ) { vector < int > ans ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( V >= deno [ i ] ) { V -= deno [ i ] ; ans . push_back ( deno [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) cout << ans [ i ] << " " ; } </Code>
cpp-python-HOW_TO_AVOID_OVERFLOW_IN_MODULAR_MULTIPLICATION
cpp-python
ll mulmod ( ll a , ll b , ll mod ) { ll res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; }
def mulmod ( a , b , mod ) : res = 0 a = a % mod while ( b > 0 ) : if ( b % 2 == 1 ) : res = ( res + a ) % mod a = ( a * 2 ) % mod b //= 2 return res % mod
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> ll mulmod ( ll a , ll b , ll mod ) { ll res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } </Code>
cpp-python-FIND_NTH_TERM_DRAGON_CURVE_SEQUENCE
cpp-python
string Dragon_Curve_Sequence ( int n ) { string s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { string temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { temp += s [ j ] ; if ( prev == '0' ) { temp += one ; prev =...
def Dragon_Curve_Sequence ( n ) : s = "1" for i in range ( 2 , n + 1 ) : temp = "1" prev = '1' zero = '0' one = '1' for j in range ( len ( s ) ) : temp += s [ j ] if ( prev == '0' ) : temp += one prev = one ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string Dragon_Curve_Sequence ( int n ) { string s = "1" ; for ( int i = 2 ; i <= n ; i ++ ) { string temp = "1" ; char prev = '1' , zero = '0' , one = '1' ; for ( int j = 0 ; j < s . length ( ) ; j ++ ) { temp += s [ j ] ; if ( prev == '0' ) { temp += one ; ...
cpp-python-MINIMUM_POSITIVE_POINTS_TO_REACH_DESTINATION
cpp-python
int minInitialPoints ( int points [ ] [ C ] ) { int dp [ R ] [ C ] ; int m = R , n = C ; dp [ m - 1 ] [ n - 1 ] = points [ m - 1 ] [ n - 1 ] > 0 ? 1 : abs ( points [ m - 1 ] [ n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i ] [ n - 1 ] = max ( dp [ i + 1 ] [ n - 1 ] - points [ i ] [ n - 1 ] , 1...
def minInitialPoints ( points ) : dp = [ [ 0 for x in range ( C + 1 ) ] for y in range ( R + 1 ) ] m , n = R , C if points [ m - 1 ] [ n - 1 ] > 0 : dp [ m - 1 ] [ n - 1 ] = 1 else : dp [ m - 1 ] [ n - 1 ] = abs ( points [ m - 1 ] [ n - 1 ] ) + 1 for i in range ( m - 2 , - 1 , - 1 ) ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int minInitialPoints ( int points [ ] [ C ] ) { int dp [ R ] [ C ] ; int m = R , n = C ; dp [ m - 1 ] [ n - 1 ] = points [ m - 1 ] [ n - 1 ] > 0 ? 1 : abs ( points [ m - 1 ] [ n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i ] [ n - 1 ] = max ( dp [ i + 1 ] [ n - 1 ] - points [ i ] [ n - ...
cpp-python-FIND_REPETITIVE_ELEMENT_1_N_1
cpp-python
int findRepeating ( int arr [ ] , int n ) { return accumulate ( arr , arr + n , 0 ) - ( ( n - 1 ) * n / 2 ) ; }
def findRepeating ( arr , n ) : return sum ( arr ) - ( ( ( n - 1 ) * n ) // 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findRepeating ( int arr [ ] , int n ) { return accumulate ( arr , arr + n , 0 ) - ( ( n - 1 ) * n / 2 ) ; } </Code>
cpp-python-C_PROGRAM_FIND_SECOND_FREQUENT_CHARACTER
cpp-python
char getSecondMostFreq ( string str ) { int count [ NO_OF_CHARS ] = { 0 } , i ; for ( i = 0 ; str [ i ] ; i ++ ) ( count [ str [ i ] ] ) ++ ; int first = 0 , second = 0 ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] > count [ first ] ) { second = first ; ...
def getSecondMostFreq ( str ) : NO_OF_CHARS = 256 count = [ 0 ] * NO_OF_CHARS for i in range ( len ( str ) ) : count [ ord ( str [ i ] ) ] += 1 first , second = 0 , 0 for i in range ( NO_OF_CHARS ) : if count [ i ] > count [ first ] : second = first first = i ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> char getSecondMostFreq ( string str ) { int count [ NO_OF_CHARS ] = { 0 } , i ; for ( i = 0 ; str [ i ] ; i ++ ) ( count [ str [ i ] ] ) ++ ; int first = 0 , second = 0 ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] > count [ first ] ) { second = f...
cpp-python-SUM_TWO_LARGE_NUMBERS
cpp-python
string findSum ( string str1 , string str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) swap ( str1 , str2 ) ; string str = "" ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; reverse ( str1 . begin ( ) , str1 . end ( ) ) ; reverse ( str2 . begin ( ) , str2 . end ( ) ) ; int carry = 0 ; for ( i...
def findSum ( str1 , str2 ) : if ( len ( str1 ) > len ( str2 ) ) : t = str1 str1 = str2 str2 = t str = "" n1 = len ( str1 ) n2 = len ( str2 ) str1 = str1 [ : : - 1 ] str2 = str2 [ : : - 1 ] carry = 0 for i in range ( n1 ) : sum = ( ( ord ( str1 [ i ] ) - 4...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string findSum ( string str1 , string str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) swap ( str1 , str2 ) ; string str = "" ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; reverse ( str1 . begin ( ) , str1 . end ( ) ) ; reverse ( str2 . begin ( ) , str2 . end ( ) ) ; int carry = 0 ; ...
cpp-python-NEXT_GREATER_ELEMENT
cpp-python
void printNGE ( int arr [ ] , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { next = arr [ j ] ; break ; } } cout << arr [ i ] << " -- " << next << endl ; } }
def printNGE ( arr ) : for i in range ( 0 , len ( arr ) , 1 ) : next = - 1 for j in range ( i + 1 , len ( arr ) , 1 ) : if arr [ i ] < arr [ j ] : next = arr [ j ] break print ( str ( arr [ i ] ) + " -- " + str ( next ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printNGE ( int arr [ ] , int n ) { int next , i , j ; for ( i = 0 ; i < n ; i ++ ) { next = - 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { next = arr [ j ] ; break ; } } cout << arr [ i ] << " -- " << next << endl ; } } ...
java-cpp-PAIRS_OF_POSITIVE_NEGATIVE_VALUES_IN_AN_ARRAY
java-cpp
public static void printPairs ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) retu...
void printPairs ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = ...
Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>.
<Code> public static void printPairs ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0...
java-cpp-C_PROGRAM_FIND_AREA_TRIANGLE
java-cpp
static float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { System . out . println ( "Not a valid triangle" ) ; System . exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s ...
float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { cout << "Not a valid trianglen" ; exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; }
Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>.
<Code> static float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { System . out . println ( "Not a valid triangle" ) ; System . exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b )...
java-cpp-CHECK_ARRAY_MAJORITY_ELEMENT
java-cpp
static boolean isMajority ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; else mp . put ( a [ i ] , 1 ) ; for ( Map . Entry < Integer , In...
bool isMajority ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] ++ ; for ( auto x : mp ) if ( x . second >= n / 2 ) return true ; return false ; }
Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>.
<Code> static boolean isMajority ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ; else mp . put ( a [ i ] , 1 ) ; for ( Map . Entry < Integ...
java-cpp-NUMBER_N_DIGITS_NON_DECREASING_INTEGERS
java-cpp
static int nonDecNums ( int n ) { int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ]...
int nonDecNums ( int n ) { int a [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ; return a [...
Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>.
<Code> static int nonDecNums ( int n ) { int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + ...