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
python-cpp-EVALUATE_AN_ARRAY_EXPRESSION_WITH_NUMBERS_AND
python-cpp
def calculateSum ( arr , n ) : if ( n == 0 ) : return 0 s = arr [ 0 ] value = int ( s ) sum = value for i in range ( 2 , n , 2 ) : s = arr [ i ] value = int ( s ) operation = arr [ i - 1 ] [ 0 ] if ( operation == '+' ) : sum += value else :...
int calculateSum ( string arr [ ] , int n ) { if ( n == 0 ) return 0 ; string s = arr [ 0 ] ; int value = stoi ( s ) ; int sum = value ; for ( int i = 2 ; i < n ; i = i + 2 ) { s = arr [ i ] ; int value = stoi ( s ) ; char operation = arr [ i - 1 ] [ 0 ] ; if ( operation == '+' ) sum += va...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def calculateSum ( arr , n ) : if ( n == 0 ) : return 0 s = arr [ 0 ] value = int ( s ) sum = value for i in range ( 2 , n , 2 ) : s = arr [ i ] value = int ( s ) operation = arr [ i - 1 ] [ 0 ] if ( operation == '+' ) : sum += value ...
python-cpp-CHECK_WHETHER_POINT_EXISTS_CIRCLE_SECTOR_NOT
python-cpp
def checkPoint ( radius , x , y , percent , startAngle ) : endAngle = 360 / percent + startAngle polarradius = math.sqrt ( x * x + y * y ) Angle = math.atan ( y / x ) if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) : print ( "Point (" , x , "," , y , ") " "exist in the ...
void checkPoint ( int radius , int x , int y , float percent , float startAngle ) { float endAngle = 360 / percent + startAngle ; float polarradius = sqrt ( x * x + y * y ) ; float Angle = atan ( y / x ) ; if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( "Point (%d, %d) exist in ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkPoint ( radius , x , y , percent , startAngle ) : endAngle = 360 / percent + startAngle polarradius = math.sqrt ( x * x + y * y ) Angle = math.atan ( y / x ) if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) : print ( "Point (" , x , "," , y , ") " "exist ...
python-cpp-PRINTING_ITEMS_01_KNAPSACK
python-cpp
def printknapSack ( W , wt , val , n ) : K = [ [ 0 for w in range ( W + 1 ) ] for i 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 ( va...
void printknapSack ( 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 [ ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printknapSack ( W , wt , val , n ) : K = [ [ 0 for w in range ( W + 1 ) ] for i 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 ] = m...
python-cpp-SEQUENCES_GIVEN_LENGTH_EVERY_ELEMENT_EQUAL_TWICE_PREVIOUS_1
python-cpp
def getTotalNumberOfSequences ( m , n ) : T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : if i == 0 or j == 0 : T [ i ] [ j ] = 0 elif i < j : T [ i ] [ j ] = 0 elif ...
int getTotalNumberOfSequences ( int m , int n ) { int T [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) { for ( int j = 0 ; j < n + 1 ; j ++ ) { if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ; else if ( i < j ) T [ i ] [ j ] = 0 ; else if ( j == 1 ) T [ i ] [ j ] = i ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getTotalNumberOfSequences ( m , n ) : T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ] for i in range ( m + 1 ) : for j in range ( n + 1 ) : if i == 0 or j == 0 : T [ i ] [ j ] = 0 elif i < j : T [ i ] [ j ] = 0 ...
python-cpp-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS
python-cpp
def findSum ( n ) : arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( n ) : for j in range ( n ) : arr [ i ] [ j ] = abs ( i - j ) sum = 0 for i in range ( n ) : for j in range ( n ) : sum += arr [ i ] [ j ] return sum
int findSum ( int n ) { int arr [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) arr [ i ] [ j ] = abs ( i - j ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) sum += arr [ i ] [ j ] ; return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSum ( n ) : arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( n ) : for j in range ( n ) : arr [ i ] [ j ] = abs ( i - j ) sum = 0 for i in range ( n ) : for j in range ( n ) : sum += arr [ i ] [ j ] return sum </Code>
python-cpp-REPLACE_CHARACTER_C1_C2_C2_C1_STRING_S
python-cpp
def replace ( s , c1 , c2 ) : l = len ( s ) for i in range ( l ) : if ( s [ i ] == c1 ) : s = s [ 0 : i ] + c2 + s [ i + 1 : ] elif ( s [ i ] == c2 ) : s = s [ 0 : i ] + c1 + s [ i + 1 : ] return s
string replace ( string s , char c1 , char c2 ) { int l = s . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( s [ i ] == c1 ) s [ i ] = c2 ; else if ( s [ i ] == c2 ) s [ i ] = c1 ; } return s ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def replace ( s , c1 , c2 ) : l = len ( s ) for i in range ( l ) : if ( s [ i ] == c1 ) : s = s [ 0 : i ] + c2 + s [ i + 1 : ] elif ( s [ i ] == c2 ) : s = s [ 0 : i ] + c1 + s [ i + 1 : ] return s </Code>
python-cpp-DISCRETE_LOGARITHM_FIND_INTEGER_K_AK_CONGRUENT_MODULO_B
python-cpp
def discreteLogarithm ( a , b , m ) : n = int ( math.sqrt ( m ) + 1 ) an = 1 for i in range ( n ) : an = ( an * a ) % m value = [ 0 ] * m cur = an for i in range ( 1 , n + 1 ) : if ( value [ cur ] == 0 ) : value [ cur ] = i cur = ( cur * an ) % m cur = b ...
int discreteLogarithm ( int a , int b , int m ) { int n = ( int ) sqrt ( m ) + 1 ; int an = 1 ; for ( int i = 0 ; i < n ; ++ i ) an = ( an * a ) % m ; unordered_map < int , int > value ; for ( int i = 1 , cur = an ; i <= n ; ++ i ) { if ( ! value [ cur ] ) value [ cur ] = i ; cur = ( cur * an ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def discreteLogarithm ( a , b , m ) : n = int ( math.sqrt ( m ) + 1 ) an = 1 for i in range ( n ) : an = ( an * a ) % m value = [ 0 ] * m cur = an for i in range ( 1 , n + 1 ) : if ( value [ cur ] == 0 ) : value [ cur ] = i cur = ( cur * an ) % m cu...
python-cpp-FIND_LARGEST_PRIME_FACTOR_NUMBER
python-cpp
def maxPrimeFactors ( n ) : maxPrime = - 1 while n % 2 == 0 : maxPrime = 2 n >>= 1 for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 , 2 ) : while n % i == 0 : maxPrime = i n = n / i if n > 2 : maxPrime = n return int ( maxPrime )
long long maxPrimeFactors ( long long n ) { long long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) maxPrime = n ; return maxPrime ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxPrimeFactors ( n ) : maxPrime = - 1 while n % 2 == 0 : maxPrime = 2 n >>= 1 for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 , 2 ) : while n % i == 0 : maxPrime = i n = n / i if n > 2 : maxPrime = n return int ( maxPrime ) </Code...
python-cpp-POSSIBLE_FORM_TRIANGLE_ARRAY_VALUES
python-cpp
def isPossibleTriangle ( arr , N ) : if N < 3 : return False arr.sort ( ) for i in range ( N - 2 ) : if arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] : return True
bool isPossibleTriangle ( int arr [ ] , int N ) { if ( N < 3 ) return false ; sort ( arr , arr + N ) ; for ( int i = 0 ; i < N - 2 ; i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPossibleTriangle ( arr , N ) : if N < 3 : return False arr.sort ( ) for i in range ( N - 2 ) : if arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] : return True </Code>
python-cpp-COUNT_NUMBER_INCREASING_SUBSEQUENCES_SIZE_K
python-cpp
def numOfIncSubseqOfSizeK ( arr , n , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ] for i in range ( n ) : dp [ 0 ] [ i ] = 1 for l in range ( 1 , k ) : for i in range ( l , n ) : dp [ l ] [ i ] = 0 for j in range ( l - 1 , i ) : if ( ...
int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) { int dp [ k ] [ n ] , sum = 0 ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int l = 1 ; l < k ; l ++ ) { for ( int i = l ; i < n ; i ++ ) { dp [ l ] [ i ] = 0 ; for ( in...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def numOfIncSubseqOfSizeK ( arr , n , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ] for i in range ( n ) : dp [ 0 ] [ i ] = 1 for l in range ( 1 , k ) : for i in range ( l , n ) : dp [ l ] [ i ] = 0 for j in range ( l - 1 , i ) : ...
python-cpp-MINIMUM_COST_MAKE_LONGEST_COMMON_SUBSEQUENCE_LENGTH_K
python-cpp
def solve ( X , Y , l , r , k , dp ) : if k == 0 : return 0 if l < 0 or r < 0 : return 1000000000 if dp [ l ] [ r ] [ k ] != - 1 : return dp [ l ] [ r ] [ k ] cost = ( ( ord ( X [ l ] ) - ord ( 'a' ) ) ^ ( ord ( Y [ r ] ) - ord ( 'a' ) ) ) dp [ l ] [ r ] [ k ] = min ( [ cost ...
int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) { if ( ! k ) return 0 ; if ( l < 0
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solve ( X , Y , l , r , k , dp ) : if k == 0 : return 0 if l < 0 or r < 0 : return 1000000000 if dp [ l ] [ r ] [ k ] != - 1 : return dp [ l ] [ r ] [ k ] cost = ( ( ord ( X [ l ] ) - ord ( 'a' ) ) ^ ( ord ( Y [ r ] ) - ord ( 'a' ) ) ) dp [ l ] [ r ] [ k ] = min ( ...
python-cpp-REMAINDER_7_LARGE_NUMBERS
python-cpp
def remainderWith7 ( num ) : series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] series_index = 0 result = 0 for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) : digit = ord ( num [ i ] ) - 48 result += digit * series [ series_index ] series_index = ( series_index + 1 ) % 6 result %...
int remainderWith7 ( string num ) { int series [ ] = { 1 , 3 , 2 , - 1 , - 3 , - 2 } ; int series_index = 0 ; int result = 0 ; for ( int i = num . size ( ) - 1 ; i >= 0 ; i -- ) { int digit = num [ i ] - '0' ; result += digit * series [ series_index ] ; series_index = ( s...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def remainderWith7 ( num ) : series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ] series_index = 0 result = 0 for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) : digit = ord ( num [ i ] ) - 48 result += digit * series [ series_index ] series_index = ( series_index + 1 ) % 6 r...
python-cpp-GIVEN_TWO_STRINGS_FIND_FIRST_STRING_SUBSEQUENCE_SECOND
python-cpp
def isSubSequence ( string1 , string2 , m , n ) : if m == 0 : return True if n == 0 : return False if string1 [ m - 1 ] == string2 [ n - 1 ] : return isSubSequence ( string1 , string2 , m - 1 , n - 1 ) return isSubSequence ( string1 , string2 , m , n - 1 )
bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) { if ( m == 0 ) return true ; if ( n == 0 ) return false ; if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ; return isSubSequence ( str1 , str2 , m , n - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isSubSequence ( string1 , string2 , m , n ) : if m == 0 : return True if n == 0 : return False if string1 [ m - 1 ] == string2 [ n - 1 ] : return isSubSequence ( string1 , string2 , m - 1 , n - 1 ) return isSubSequence ( string1 , string2 , m , n - 1 ) </Code>
python-cpp-PROGRAM_TO_FIND_REMAINDER_WITHOUT_USING_MODULO_OR_OPERATOR
python-cpp
def getRemainder ( num , divisor ) : return ( num - divisor * ( num // divisor ) )
int getRemainder ( int num , int divisor ) { return ( num - divisor * ( num / divisor ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getRemainder ( num , divisor ) : return ( num - divisor * ( num // divisor ) ) </Code>
python-cpp-MINIMUM_PRODUCT_SUBSET_ARRAY
python-cpp
def minProductSubset ( a , n ) : if ( n == 1 ) : return a [ 0 ] max_neg = float ( '-inf' ) min_pos = float ( 'inf' ) count_neg = 0 count_zero = 0 prod = 1 for i in range ( 0 , n ) : if ( a [ i ] == 0 ) : count_zero = count_zero + 1 continue if ...
int minProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; int min_pos = INT_MAX ; int count_neg = 0 , count_zero = 0 ; int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { count_zero ++ ; continue ; } if ( a [ i ] < 0 )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minProductSubset ( a , n ) : if ( n == 1 ) : return a [ 0 ] max_neg = float ( '-inf' ) min_pos = float ( 'inf' ) count_neg = 0 count_zero = 0 prod = 1 for i in range ( 0 , n ) : if ( a [ i ] == 0 ) : count_zero = count_zero + 1 continue ...
python-cpp-HOW_CAN_WE_SUM_THE_DIGITS_OF_A_GIVEN_NUMBER_IN_SINGLE_STATEMENT
python-cpp
def getSum ( n ) : sum = 0 while ( n != 0 ) : sum = sum + int ( n % 10 ) n = int ( n / 10 ) return sum
public : int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getSum ( n ) : sum = 0 while ( n != 0 ) : sum = sum + int ( n % 10 ) n = int ( n / 10 ) return sum </Code>
python-cpp-COUNT_DIGITS_FACTORIAL_SET_1
python-cpp
def findDigits ( n ) : if ( n < 0 ) : return 0 if ( n <= 1 ) : return 1 digits = 0 for i in range ( 2 , n + 1 ) : digits += math.log10 ( i ) return math.floor ( digits ) + 1
int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += log10 ( i ) ; return floor ( digits ) + 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findDigits ( n ) : if ( n < 0 ) : return 0 if ( n <= 1 ) : return 1 digits = 0 for i in range ( 2 , n + 1 ) : digits += math.log10 ( i ) return math.floor ( digits ) + 1 </Code>
python-cpp-SORT_ARRAY_TWO_HALVES_SORTED
python-cpp
def mergeTwoHalf ( A , n ) : A.sort ( )
void mergeTwoHalf ( int A [ ] , int n ) { sort ( A , A + n ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def mergeTwoHalf ( A , n ) : A.sort ( ) </Code>
python-cpp-CREATE_A_MATRIX_WITH_ALTERNATING_RECTANGLES_OF_0_AND_X
python-cpp
def fill0X ( m , n ) : i , k , l = 0 , 0 , 0 r = m c = n a = [ [ None ] * n for i in range ( m ) ] x = 'X' while k < m and l < n : for i in range ( l , n ) : a [ k ] [ i ] = x k += 1 for i in range ( k , m ) : a [ i ] [ n - 1 ] = x n -= 1 ...
void fill0X ( int m , int n ) { int i , k = 0 , l = 0 ; int r = m , c = n ; char a [ m ] [ n ] ; char x = 'X' ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) a [ k ] [ i ] = x ; k ++ ; for ( i = k ; i < m ; ++ i ) a [ i ] [ n - 1 ] = x ; n -- ; if ( k < m ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def fill0X ( m , n ) : i , k , l = 0 , 0 , 0 r = m c = n a = [ [ None ] * n for i in range ( m ) ] x = 'X' while k < m and l < n : for i in range ( l , n ) : a [ k ] [ i ] = x k += 1 for i in range ( k , m ) : a [ i ] [ n - 1 ] = x n...
python-cpp-MINIMUM_PERIMETER_N_BLOCKS
python-cpp
def minPerimeter ( n ) : l = math.sqrt ( n ) sq = l * l if ( sq == n ) : return l * 4 else : row = n / l perimeter = 2 * ( l + row ) if ( n % l != 0 ) : perimeter += 2 return perimeter
int minPerimeter ( int n ) { int l = sqrt ( n ) ; int sq = l * l ; if ( sq == n ) return l * 4 ; else { long long int row = n / l ; long long int perimeter = 2 * ( l + row ) ; if ( n % l != 0 ) perimeter += 2 ; return perimeter ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minPerimeter ( n ) : l = math.sqrt ( n ) sq = l * l if ( sq == n ) : return l * 4 else : row = n / l perimeter = 2 * ( l + row ) if ( n % l != 0 ) : perimeter += 2 return perimeter </Code>
python-cpp-FIND_POSSIBLE_ROTATE_PAGE_ANGLE_NOT
python-cpp
def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) if ( dis1 != dis2 ) : print ( "No" ) elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : print ( "No" ) e...
void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) { long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ; long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) cout << "No" ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) if ( dis1 != dis2 ) : print ( "No" ) elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : print ( "No" ...
python-cpp-PROGRAM_FIND_SLOPE_LINE
python-cpp
def slope ( x1 , y1 , x2 , y2 ) : return ( float ) ( y2 - y1 ) / ( x2 - x1 )
float slope ( float x1 , float y1 , float x2 , float y2 ) { return ( y2 - y1 ) / ( x2 - x1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def slope ( x1 , y1 , x2 , y2 ) : return ( float ) ( y2 - y1 ) / ( x2 - x1 ) </Code>
python-cpp-FIND_NUMBER_PERFECT_SQUARES_TWO_GIVEN_NUMBERS_1
python-cpp
def CountSquares ( a , b ) : return ( math.floor ( math.sqrt ( b ) ) - math.ceil ( math.sqrt ( a ) ) + 1 )
int countSquares ( int a , int b ) { return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def CountSquares ( a , b ) : return ( math.floor ( math.sqrt ( b ) ) - math.ceil ( math.sqrt ( a ) ) + 1 ) </Code>
python-cpp-COUNT_BINARY_STRINGS_K_TIMES_APPEARING_ADJACENT_TWO_SET_BITS
python-cpp
def countStrings ( n , k ) : dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ] dp [ 1 ] [ 0 ] [ 0 ] = 1 dp [ 1 ] [ 0 ] [ 1 ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( k + 1 ) : dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ ...
int countStrings ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] [ 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp [ ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countStrings ( n , k ) : dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ] dp [ 1 ] [ 0 ] [ 0 ] = 1 dp [ 1 ] [ 0 ] [ 1 ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( k + 1 ) : dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [...
python-cpp-FIND_SMALLEST_RANGE_CONTAINING_ELEMENTS_FROM_K_LISTS
python-cpp
def findSmallestRange ( arr , n , k ) : i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 for i in range ( k + 1 ) : ptr [ i ] = 0 minrange = 10 ** 9 while ( 1 ) : minind = - 1 minval = 10 ** 9 maxval = - 10 ** 9 flag =...
void findSmallestRange ( int arr [ ] [ N ] , int n , int k ) { int i , minval , maxval , minrange , minel , maxel , flag , minind ; for ( i = 0 ; i <= k ; i ++ ) ptr [ i ] = 0 ; minrange = INT_MAX ; while ( 1 ) { minind = - 1 ; minval = INT_MAX ; maxval = INT_MIN ; flag = 0 ; for ( i = 0...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSmallestRange ( arr , n , k ) : i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 for i in range ( k + 1 ) : ptr [ i ] = 0 minrange = 10 ** 9 while ( 1 ) : minind = - 1 minval = 10 ** 9 maxval = - 10 ** 9 ...
python-cpp-LEXICOGRAPHICAL_CONCATENATION_SUBSTRINGS_STRING
python-cpp
def lexicographicSubConcat ( s ) : n = len ( s ) sub_count = ( n * ( n + 1 ) ) // 2 arr = [ 0 ] * sub_count index = 0 for i in range ( n ) : for j in range ( 1 , n - i + 1 ) : arr [ index ] = s [ i : i + j ] index += 1 arr.sort ( ) res = "" for i in range ...
string lexicographicSubConcat ( string s ) { int n = s . length ( ) ; int sub_count = n * ( n + 1 ) / 2 ; string arr [ sub_count ] ; int index = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int len = 1 ; len <= n - i ; len ++ ) arr [ index ++ ] = s . substr ( i , len ) ; sort ( arr , arr + sub_count )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lexicographicSubConcat ( s ) : n = len ( s ) sub_count = ( n * ( n + 1 ) ) // 2 arr = [ 0 ] * sub_count index = 0 for i in range ( n ) : for j in range ( 1 , n - i + 1 ) : arr [ index ] = s [ i : i + j ] index += 1 arr.sort ( ) res = "" for i in...
python-cpp-FIND_MAXIMUM_ELEMENT_ROW_MATRIX
python-cpp
def maxelement ( arr ) : no_of_rows = len ( arr ) no_of_column = len ( arr [ 0 ] ) for i in range ( no_of_rows ) : max1 = 0 for j in range ( no_of_column ) : if arr [ i ] [ j ] > max1 : max1 = arr [ i ] [ j ] print ( max1 )
void maxelement ( int no_of_rows , int arr [ ] [ N ] ) { int i = 0 ; int max = 0 ; int result [ no_of_rows ] ; while ( i < no_of_rows ) { for ( int j = 0 ; j < N ; j ++ ) { if ( arr [ i ] [ j ] > max ) { max = arr [ i ] [ j ] ; } } result [ i ] = max ; max = 0 ; i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxelement ( arr ) : no_of_rows = len ( arr ) no_of_column = len ( arr [ 0 ] ) for i in range ( no_of_rows ) : max1 = 0 for j in range ( no_of_column ) : if arr [ i ] [ j ] > max1 : max1 = arr [ i ] [ j ] print ( max1 ) </Code>
python-cpp-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX_1
python-cpp
def countNegative ( M , n , m ) : count = 0 i = 0 j = m - 1 while j >= 0 and i < n : if M [ i ] [ j ] < 0 : count += ( j + 1 ) i += 1 else : j -= 1 return count
int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; int i = 0 ; int j = m - 1 ; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1 ; i += 1 ; } else j -= 1 ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countNegative ( M , n , m ) : count = 0 i = 0 j = m - 1 while j >= 0 and i < n : if M [ i ] [ j ] < 0 : count += ( j + 1 ) i += 1 else : j -= 1 return count </Code>
python-cpp-FIND_POSITION_GIVEN_NUMBER_AMONG_NUMBERS_MADE_4_7
python-cpp
def findpos ( n ) : i = 0 j = len ( n ) pos = 0 while ( i < j ) : if ( n [ i ] == '4' ) : pos = pos * 2 + 1 if ( n [ i ] == '7' ) : pos = pos * 2 + 2 i = i + 1 return pos
int findpos ( string n ) { int i = 0 , pos = 0 ; while ( n [ i ] != '\0' ) { switch ( n [ i ] ) { case '4' : pos = pos * 2 + 1 ; break ; case '7' : pos = pos * 2 + 2 ; break ; } i ++ ; } return pos ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findpos ( n ) : i = 0 j = len ( n ) pos = 0 while ( i < j ) : if ( n [ i ] == '4' ) : pos = pos * 2 + 1 if ( n [ i ] == '7' ) : pos = pos * 2 + 2 i = i + 1 return pos </Code>
python-cpp-FIND_VALUE_OF_Y_MOD_2_RAISED_TO_POWER_X
python-cpp
def yMod ( y , x ) : return ( y % pow ( 2 , x ) )
long long int yMod ( long long int y , long long int x ) { if ( log2 ( y ) < x ) return y ; if ( x > 63 ) return y ; return ( y % ( 1 << x ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def yMod ( y , x ) : return ( y % pow ( 2 , x ) ) </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_14_MAXIMUM_SUM_INCREASING_SUBSEQUENCE
python-cpp
def maxSumIS ( arr , n ) : max = 0 msis = [ 0 for x in range ( n ) ] for i in range ( n ) : msis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) : msis [ i ] = msis [ j ] ...
int maxSumIS ( int arr [ ] , int n ) { int i , j , max = 0 ; int msis [ n ] ; for ( i = 0 ; i < n ; i ++ ) msis [ i ] = arr [ i ] ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ; fo...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSumIS ( arr , n ) : max = 0 msis = [ 0 for x in range ( n ) ] for i in range ( n ) : msis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) : msis [ i ] = msis...
python-cpp-LENGTH_LONGEST_BALANCED_SUBSEQUENCE_1
python-cpp
def maxLength ( s , n ) : invalidOpenBraces = 0 invalidCloseBraces = 0 for i in range ( n ) : if ( s [ i ] == '(' ) : invalidOpenBraces += 1 else : if ( invalidOpenBraces == 0 ) : invalidCloseBraces += 1 else : invalidOpenBr...
int maxLength ( char s [ ] , int n ) { int invalidOpenBraces = 0 ; int invalidCloseBraces = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == '(' ) { invalidOpenBraces ++ ; } else { if ( invalidOpenBraces == 0 ) { invalidCloseBraces ++ ; } else { invali...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxLength ( s , n ) : invalidOpenBraces = 0 invalidCloseBraces = 0 for i in range ( n ) : if ( s [ i ] == '(' ) : invalidOpenBraces += 1 else : if ( invalidOpenBraces == 0 ) : invalidCloseBraces += 1 else : invali...
python-cpp-MAXIMUM_PRODUCT_INCREASING_SUBSEQUENCE
python-cpp
def lis ( arr , n ) : mpis = [ 0 ] * ( n ) for i in range ( n ) : mpis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) : mpis [ i ] = mpis [ j ] * arr [ i ] return max...
ll lis ( ll arr [ ] , ll n ) { ll mpis [ n ] ; for ( int i = 0 ; i < n ; i ++ ) mpis [ i ] = arr [ i ] ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ; return * max_elemen...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lis ( arr , n ) : mpis = [ 0 ] * ( n ) for i in range ( n ) : mpis [ i ] = arr [ i ] for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) : mpis [ i ] = mpis [ j ] * arr [ i ] ret...
python-cpp-PETERSON_GRAPH
python-cpp
def findthepath ( S , v ) : result [ 0 ] = v for i in range ( 1 , len ( S ) ) : if ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) ] or adj [ ord ( S [ i ] ) - ord ( 'A' ) ] [ v ] ) : v = ord ( S [ i ] ) - ord ( 'A' ) elif ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) + 5 ] or adj [ ord ( ...
bool findthepath ( char * S , int v ) { result [ 0 ] = v + '0' ; for ( int i = 1 ; S [ i ] ; i ++ ) { if ( adj [ v ] [ S [ i ] - 'A' ] || adj [ S [ i ] - 'A' ] [ v ] ) { v = S [ i ] - 'A' ; } else if ( adj [ v ] [ S [ i ] - 'A' + 5 ] || adj [ S [ i ] - 'A' + 5 ] [ v ] ) { v = S [ i ] - '...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findthepath ( S , v ) : result [ 0 ] = v for i in range ( 1 , len ( S ) ) : if ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) ] or adj [ ord ( S [ i ] ) - ord ( 'A' ) ] [ v ] ) : v = ord ( S [ i ] ) - ord ( 'A' ) elif ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) + 5 ] or adj [...
python-cpp-CHECK_QUEUE_CAN_SORTED_ANOTHER_QUEUE_USING_STACK
python-cpp
def checkSorted ( n , q ) : st = [ ] expected = 1 fnt = None while ( not q.empty ( ) ) : fnt = q.queue [ 0 ] q.get ( ) if ( fnt == expected ) : expected += 1 else : if ( len ( st ) == 0 ) : st.append ( fnt ) elif ( len (...
bool checkSorted ( int n , queue < int > & q ) { stack < int > st ; int expected = 1 ; int fnt ; while ( ! q . empty ( ) ) { fnt = q . front ( ) ; q . pop ( ) ; if ( fnt == expected ) expected ++ ; else { if ( st . empty ( ) ) { st . push ( fnt ) ; } else if ( ! st . em...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkSorted ( n , q ) : st = [ ] expected = 1 fnt = None while ( not q.empty ( ) ) : fnt = q.queue [ 0 ] q.get ( ) if ( fnt == expected ) : expected += 1 else : if ( len ( st ) == 0 ) : st.append ( fnt ) elif ...
python-cpp-GENERATING_DISTINCT_SUBSEQUENCES_OF_A_GIVEN_STRING_IN_LEXICOGRAPHIC_ORDER
python-cpp
def generate ( st , s ) : if len ( s ) == 0 : return if s not in st : st.add ( s ) for i in range ( len ( s ) ) : t = list ( s ).copy ( ) t.remove ( s [ i ] ) t = ''.join ( t ) generate ( st , t ) return
void generate ( set < string > & st , string s ) { if ( s . size ( ) == 0 ) return ; if ( st . find ( s ) == st . end ( ) ) { st . insert ( s ) ; for ( int i = 0 ; i < s . size ( ) ; i ++ ) { string t = s ; t . erase ( i , 1 ) ; generate ( st , t ) ; } } return ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def generate ( st , s ) : if len ( s ) == 0 : return if s not in st : st.add ( s ) for i in range ( len ( s ) ) : t = list ( s ).copy ( ) t.remove ( s [ i ] ) t = ''.join ( t ) generate ( st , t ) return </Code>
python-cpp-RECURSIVE_FUNCTIONS
python-cpp
def tower ( n , sourcePole , destinationPole , auxiliaryPole ) : if ( 0 == n ) : return tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) print ( "Move the disk" , sourcePole , "from" , sourcePole , "to" , destinationPole ) tower ( n - 1 , auxiliaryPole , destinationPole , sourcePol...
void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) { if ( 0 == n ) return ; tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ; cout << "Move the disk " << n << " from " << sourcePole << " to " << destinationPole << endl ; tower ( n - 1 , auxiliaryPole , destinationP...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def tower ( n , sourcePole , destinationPole , auxiliaryPole ) : if ( 0 == n ) : return tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) print ( "Move the disk" , sourcePole , "from" , sourcePole , "to" , destinationPole ) tower ( n - 1 , auxiliaryPole , destinationPole , so...
python-cpp-PIZZA_CUT_PROBLEM_CIRCLE_DIVISION_LINES
python-cpp
def findMaximumPieces ( n ) : return int ( 1 + n * ( n + 1 ) / 2 )
int findMaximumPieces ( int n ) { return 1 + n * ( n + 1 ) / 2 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaximumPieces ( n ) : return int ( 1 + n * ( n + 1 ) / 2 ) </Code>
python-cpp-LEXICOGRAPHICALLY_MINIMUM_STRING_ROTATION
python-cpp
def minLexRotation ( str_ ) : n = len ( str_ ) arr = [ 0 ] * n concat = str_ + str_ for i in range ( n ) : arr [ i ] = concat [ i : n + i ] arr.sort ( ) return arr [ 0 ]
string minLexRotation ( string str ) { int n = str . length ( ) ; string arr [ n ] ; string concat = str + str ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = concat . substr ( i , n ) ; sort ( arr , arr + n ) ; return arr [ 0 ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minLexRotation ( str_ ) : n = len ( str_ ) arr = [ 0 ] * n concat = str_ + str_ for i in range ( n ) : arr [ i ] = concat [ i : n + i ] arr.sort ( ) return arr [ 0 ] </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_28_MINIMUM_INSERTIONS_TO_FORM_A_PALINDROME
python-cpp
def findMinInsertions ( str , l , h ) : if ( l > h ) : return sys.maxsize if ( l == h ) : return 0 if ( l == h - 1 ) : return 0 if ( str [ l ] == str [ h ] ) else 1 if ( str [ l ] == str [ h ] ) : return findMinInsertions ( str , l + 1 , h - 1 ) else : return ...
int findMinInsertions ( char str [ ] , int l , int h ) { if ( l > h ) return INT_MAX ; if ( l == h ) return 0 ; if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ; return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , findMinIns...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMinInsertions ( str , l , h ) : if ( l > h ) : return sys.maxsize if ( l == h ) : return 0 if ( l == h - 1 ) : return 0 if ( str [ l ] == str [ h ] ) else 1 if ( str [ l ] == str [ h ] ) : return findMinInsertions ( str , l + 1 , h - 1 ) else : ...
python-cpp-PROGRAM_OCTAL_DECIMAL_CONVERSION
python-cpp
def octalToDecimal ( n ) : num = n dec_value = 0 base = 1 temp = num while ( temp ) : last_digit = temp % 10 temp = int ( temp / 10 ) dec_value += last_digit * base base = base * 8 return dec_value
int octalToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 8 ; } return dec_value ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def octalToDecimal ( n ) : num = n dec_value = 0 base = 1 temp = num while ( temp ) : last_digit = temp % 10 temp = int ( temp / 10 ) dec_value += last_digit * base base = base * 8 return dec_value </Code>
python-cpp-FIND_THE_MISSING_NUMBER
python-cpp
def getMissingNo ( A ) : n = len ( A ) total = ( n + 1 ) * ( n + 2 ) / 2 sum_of_A = sum ( A ) return total - sum_of_A
int getMissingNo ( int a [ ] , int n ) { int total = ( n + 1 ) * ( n + 2 ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) total -= a [ i ] ; return total ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getMissingNo ( A ) : n = len ( A ) total = ( n + 1 ) * ( n + 2 ) / 2 sum_of_A = sum ( A ) return total - sum_of_A </Code>
python-cpp-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING
python-cpp
def printSquares ( n ) : square = 0 ; prev_x = 0 ; for x in range ( 0 , n ) : square = ( square + x + prev_x ) print ( square , end = " " ) prev_x = x
void printSquares ( int n ) { int square = 0 , prev_x = 0 ; for ( int x = 0 ; x < n ; x ++ ) { square = ( square + x + prev_x ) ; cout << square << " " ; prev_x = x ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printSquares ( n ) : square = 0 ; prev_x = 0 ; for x in range ( 0 , n ) : square = ( square + x + prev_x ) print ( square , end = " " ) prev_x = x </Code>
python-cpp-PERMUTE_TWO_ARRAYS_SUM_EVERY_PAIR_GREATER_EQUAL_K
python-cpp
def isPossible ( a , b , n , k ) : a.sort ( reverse = True ) b.sort ( ) for i in range ( n ) : if ( a [ i ] + b [ i ] < k ) : return False return True
bool isPossible ( int a [ ] , int b [ ] , int n , int k ) { sort ( a , a + n ) ; sort ( b , b + n , greater < int > ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) if ( a [ i ] + b [ i ] < k ) return false ; return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPossible ( a , b , n , k ) : a.sort ( reverse = True ) b.sort ( ) for i in range ( n ) : if ( a [ i ] + b [ i ] < k ) : return False return True </Code>
python-cpp-CHECK_WHETHER_GIVEN_CIRCLE_RESIDE_BOUNDARY_MAINTAINED_OUTER_CIRCLE_INNER_CIRCLE
python-cpp
def fitOrNotFit ( R , r , x , y , rad ) : val = math.sqrt ( math.pow ( x , 2 ) + math.pow ( y , 2 ) ) if ( val + rad <= R and val - rad >= R - r ) : print ( "Fits\n" ) else : print ( "Doesn't Fit" )
void fitOrNotFit ( int R , int r , int x , int y , int rad ) { double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ; if ( val + rad <= R && val - rad >= R - r ) cout << "Fits\n" ; else cout << "Doesn't Fit\n" ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def fitOrNotFit ( R , r , x , y , rad ) : val = math.sqrt ( math.pow ( x , 2 ) + math.pow ( y , 2 ) ) if ( val + rad <= R and val - rad >= R - r ) : print ( "Fits\n" ) else : print ( "Doesn't Fit" ) </Code>
python-cpp-NTH_MULTIPLE_NUMBER_FIBONACCI_SERIES
python-cpp
def findPosition ( k , n ) : f1 = 0 f2 = 1 i = 2 while i != 0 : f3 = f1 + f2 f1 = f2 f2 = f3 if f2 % k == 0 : return n * i i += 1 return
int findPosition ( int k , int n ) { unsigned long long int f1 = 0 , f2 = 1 , f3 ; for ( int i = 2 ; i <= MAX ; i ++ ) { f3 = f1 + f2 ; f1 = f2 ; f2 = f3 ; if ( f2 % k == 0 ) return n * i ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findPosition ( k , n ) : f1 = 0 f2 = 1 i = 2 while i != 0 : f3 = f1 + f2 f1 = f2 f2 = f3 if f2 % k == 0 : return n * i i += 1 return </Code>
python-cpp-PRINT_N_TERMS_NEWMAN_CONWAY_SEQUENCE
python-cpp
def sequence ( n ) : f = [ 0 , 1 , 1 ] print ( f [ 1 ] , end = " " ) , print ( f [ 2 ] , end = " " ) , for i in range ( 3 , n + 1 ) : f.append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ) print ( f [ i ] , end = " " ) ,
void sequence ( int n ) { int f [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; cout << f [ 1 ] << " " << f [ 2 ] << " " ; for ( int i = 3 ; i <= n ; i ++ ) { f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; cout << f [ i ] << " " ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sequence ( n ) : f = [ 0 , 1 , 1 ] print ( f [ 1 ] , end = " " ) , print ( f [ 2 ] , end = " " ) , for i in range ( 3 , n + 1 ) : f.append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ) print ( f [ i ] , end = " " ) , </Code>
python-cpp-MINIMUM_NUMBER_OF_MANIPULATIONS_REQUIRED_TO_MAKE_TWO_STRINGS_ANAGRAM_WITHOUT_DELETION_OF_CHARACTER
python-cpp
def countManipulations ( s1 , s2 ) : count = 0 char_count = [ 0 ] * 26 for i in range ( 26 ) : char_count [ i ] = 0 for i in range ( len ( s1 ) ) : char_count [ ord ( s1 [ i ] ) - ord ( 'a' ) ] += 1 for i in range ( len ( s2 ) ) : char_count [ ord ( s2 [ i ] ) - ord ( 'a' ) ]...
int countManipulations ( string s1 , string s2 ) { int count = 0 ; int char_count [ 26 ] ; for ( int i = 0 ; i < 26 ; i ++ ) { char_count [ i ] = 0 ; } for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 [ i ] - 'a' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { char_...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countManipulations ( s1 , s2 ) : count = 0 char_count = [ 0 ] * 26 for i in range ( 26 ) : char_count [ i ] = 0 for i in range ( len ( s1 ) ) : char_count [ ord ( s1 [ i ] ) - ord ( 'a' ) ] += 1 for i in range ( len ( s2 ) ) : char_count [ ord ( s2 [ i ] ) - ord ( ...
python-cpp-COUNT_NUMBER_WAYS_REACH_GIVEN_SCORE_GAME
python-cpp
def count ( n ) : table = [ 0 for i in range ( n + 1 ) ] table [ 0 ] = 1 for i in range ( 3 , n + 1 ) : table [ i ] += table [ i - 3 ] for i in range ( 5 , n + 1 ) : table [ i ] += table [ i - 5 ] for i in range ( 10 , n + 1 ) : table [ i ] += table [ i - 10 ] return tabl...
int count ( int n ) { int table [ n + 1 ] , i ; for ( int j = 0 ; j < n + 1 ; j ++ ) table [ j ] = 0 ; table [ 0 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) table [ i ] += table [ i - 3 ] ; for ( i = 5 ; i <= n ; i ++ ) table [ i ] += table [ i - 5 ] ; for ( i = 10 ; i <= n ; i ++ ) table [ i ] +=...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def count ( n ) : table = [ 0 for i in range ( n + 1 ) ] table [ 0 ] = 1 for i in range ( 3 , n + 1 ) : table [ i ] += table [ i - 3 ] for i in range ( 5 , n + 1 ) : table [ i ] += table [ i - 5 ] for i in range ( 10 , n + 1 ) : table [ i ] += table [ i - 10 ] retu...
python-cpp-FIND_LENGTH_LONGEST_SUBSEQUENCE_ONE_STRING_SUBSTRING_ANOTHER_STRING
python-cpp
def maxSubsequenceSubstring ( x , y , n , m ) : dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : if ( x [ j - 1 ] == y [ i - 1 ] ) : dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] else : ...
int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) { int dp [ MAX ] [ MAX ] ; for ( int i = 0 ; i <= m ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( x [ j - 1 ] == y [ ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSubsequenceSubstring ( x , y , n , m ) : dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ] for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : if ( x [ j - 1 ] == y [ i - 1 ] ) : dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] else :...
python-cpp-LONGEST_REPEATING_SUBSEQUENCE
python-cpp
def findLongestRepeatingSubSeq ( str ) : n = len ( str ) dp = [ [ 0 ] * ( n + 1 ) ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : for j in range ( 1 , n + 1 ) : if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) : dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] else : ...
int findLongestRepeatingSubSeq ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( str [ i - 1 ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findLongestRepeatingSubSeq ( str ) : n = len ( str ) dp = [ [ 0 ] * ( n + 1 ) ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : for j in range ( 1 , n + 1 ) : if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) : dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ] ...
python-cpp-PROGRAM_FIND_STRING_START_END_GEEKS
python-cpp
def isCornerPresent ( str , corner ) : n = len ( str ) cl = len ( corner ) if ( n < cl ) : return False return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) )
bool isCornerPresent ( string str , string corner ) { int n = str . length ( ) ; int cl = corner . length ( ) ; if ( n < cl ) return false ; return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isCornerPresent ( str , corner ) : n = len ( str ) cl = len ( corner ) if ( n < cl ) : return False return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) ) </Code>
python-cpp-MEDIAN_OF_TWO_SORTED_ARRAYS
python-cpp
def getMedian ( ar1 , ar2 , n ) : i = 0 j = 0 m1 = - 1 m2 = - 1 count = 0 while count < n + 1 : count += 1 if i == n : m1 = m2 m2 = ar2 [ 0 ] break elif j == n : m1 = m2 m2 = ar1 [ 0 ] break i...
int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) { int i = 0 ; int j = 0 ; int count ; int m1 = - 1 , m2 = - 1 ; for ( count = 0 ; count <= n ; count ++ ) { if ( i == n ) { m1 = m2 ; m2 = ar2 [ 0 ] ; break ; } else if ( j == n ) { m1 = m2 ; m2 = ar1 [ 0 ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getMedian ( ar1 , ar2 , n ) : i = 0 j = 0 m1 = - 1 m2 = - 1 count = 0 while count < n + 1 : count += 1 if i == n : m1 = m2 m2 = ar2 [ 0 ] break elif j == n : m1 = m2 m2 = ar1 [ 0 ] break ...
python-cpp-COUNT_ZEROS_IN_A_ROW_WISE_AND_COLUMN_WISE_SORTED_MATRIX
python-cpp
def countZeroes ( mat ) : N = 5 ; row = N - 1 ; col = 0 ; count = 0 ; while ( col < N ) : while ( mat [ row ] [ col ] ) : if ( row < 0 ) : return count ; row = row - 1 ; count = count + ( row + 1 ) ; col = col + 1 ; return count ;
int countZeroes ( int mat [ N ] [ N ] ) { int row = N - 1 , col = 0 ; int count = 0 ; while ( col < N ) { while ( mat [ row ] [ col ] ) if ( -- row < 0 ) return count ; count += ( row + 1 ) ; col ++ ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countZeroes ( mat ) : N = 5 ; row = N - 1 ; col = 0 ; count = 0 ; while ( col < N ) : while ( mat [ row ] [ col ] ) : if ( row < 0 ) : return count ; row = row - 1 ; count = count + ( row + 1 ) ; col = col + 1 ; return co...
python-cpp-JUMP_SEARCH
python-cpp
def jumpSearch ( arr , x , n ) : step = math.sqrt ( n ) prev = 0 while arr [ int ( min ( step , n ) - 1 ) ] < x : prev = step step += math.sqrt ( n ) if prev >= n : return - 1 while arr [ int ( prev ) ] < x : prev += 1 if prev == min ( step , n ) : ...
int jumpSearch ( int arr [ ] , int x , int n ) { int step = sqrt ( n ) ; int prev = 0 ; while ( arr [ min ( step , n ) - 1 ] < x ) { prev = step ; step += sqrt ( n ) ; if ( prev >= n ) return - 1 ; } while ( arr [ prev ] < x ) { prev ++ ; if ( prev == min ( step , n ) ) return - 1 ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def jumpSearch ( arr , x , n ) : step = math.sqrt ( n ) prev = 0 while arr [ int ( min ( step , n ) - 1 ) ] < x : prev = step step += math.sqrt ( n ) if prev >= n : return - 1 while arr [ int ( prev ) ] < x : prev += 1 if prev == min ( step , n ...
python-cpp-FIND_X_Y_SATISFYING_AX_N
python-cpp
def solution ( a , b , n ) : i = 0 while i * a <= n : if ( n - ( i * a ) ) % b == 0 : print ( "x = " , i , ", y = " , int ( ( n - ( i * a ) ) / b ) ) return 0 i = i + 1 print ( "No solution" )
void solution ( int a , int b , int n ) { for ( int i = 0 ; i * a <= n ; i ++ ) { if ( ( n - ( i * a ) ) % b == 0 ) { cout << "x = " << i << ", y = " << ( n - ( i * a ) ) / b ; return ; } } cout << "No solution" ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solution ( a , b , n ) : i = 0 while i * a <= n : if ( n - ( i * a ) ) % b == 0 : print ( "x = " , i , ", y = " , int ( ( n - ( i * a ) ) / b ) ) return 0 i = i + 1 print ( "No solution" ) </Code>
python-cpp-DIVIDE_LARGE_NUMBER_REPRESENTED_STRING
python-cpp
def longDivision ( number , divisor ) : ans = "" idx = 0 temp = ord ( number [ idx ] ) - ord ( '0' ) while ( temp < divisor ) : temp = ( temp * 10 + ord ( number [ idx + 1 ] ) - ord ( '0' ) ) idx += 1 idx += 1 while ( ( len ( number ) ) > idx ) : ans += chr ( math.floor (...
string longDivision ( string number , int divisor ) { string ans ; int idx = 0 ; int temp = number [ idx ] - '0' ; while ( temp < divisor ) temp = temp * 10 + ( number [ ++ idx ] - '0' ) ; while ( number . size ( ) > idx ) { ans += ( temp / divisor ) + '0' ; temp = ( temp % divisor ) * 10 + number [ +...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longDivision ( number , divisor ) : ans = "" idx = 0 temp = ord ( number [ idx ] ) - ord ( '0' ) while ( temp < divisor ) : temp = ( temp * 10 + ord ( number [ idx + 1 ] ) - ord ( '0' ) ) idx += 1 idx += 1 while ( ( len ( number ) ) > idx ) : ans += chr ( math....
python-cpp-SORT_AN_ARRAY_OF_0S_1S_AND_2S
python-cpp
def sort012 ( a , arr_size ) : lo = 0 hi = arr_size - 1 mid = 0 while mid <= hi : if a [ mid ] == 0 : a [ lo ] , a [ mid ] = a [ mid ] , a [ lo ] lo = lo + 1 mid = mid + 1 elif a [ mid ] == 1 : mid = mid + 1 else : a [ m...
void sort012 ( int a [ ] , int arr_size ) { int lo = 0 ; int hi = arr_size - 1 ; int mid = 0 ; while ( mid <= hi ) { switch ( a [ mid ] ) { case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ; break ; case 1 : mid ++ ; break ; case 2 : swap ( a [ mid ] , a [ hi -- ] ) ; break ;...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sort012 ( a , arr_size ) : lo = 0 hi = arr_size - 1 mid = 0 while mid <= hi : if a [ mid ] == 0 : a [ lo ] , a [ mid ] = a [ mid ] , a [ lo ] lo = lo + 1 mid = mid + 1 elif a [ mid ] == 1 : mid = mid + 1 else : ...
python-cpp-CHECK_IF_A_GIVEN_ARRAY_CAN_REPRESENT_PREORDER_TRAVERSAL_OF_BINARY_SEARCH_TREE
python-cpp
def canRepresentBST ( pre ) : s = [ ] root = INT_MIN for value in pre : if value < root : return False while ( len ( s ) > 0 and s [ - 1 ] < value ) : root = s.pop ( ) s.append ( value ) return True
bool canRepresentBST ( int pre [ ] , int n ) { stack < int > s ; int root = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( pre [ i ] < root ) return false ; while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) { root = s . top ( ) ; s . pop ( ) ; } s . push ( pre [ i ] ) ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def canRepresentBST ( pre ) : s = [ ] root = INT_MIN for value in pre : if value < root : return False while ( len ( s ) > 0 and s [ - 1 ] < value ) : root = s.pop ( ) s.append ( value ) return True </Code>
python-cpp-FIND_K_SUCH_THAT_ALL_ELEMENTS_IN_KTH_ROW_ARE_0_AND_KTH_COLUMN_ARE_1_IN_A_BOOLEAN_MATRIX
python-cpp
def find ( arr ) : n = len ( arr ) i = 0 j = n - 1 res = - 1 while i < n and j >= 0 : if arr [ i ] [ j ] == 0 : while j >= 0 and ( arr [ i ] [ j ] == 0 or i == j ) : j -= 1 if j == - 1 : res = i break else : ...
int find ( bool arr [ n ] [ n ] ) { int i = 0 , j = n - 1 ; int res = - 1 ; while ( i < n && j >= 0 ) { if ( arr [ i ] [ j ] == 0 ) { while ( j >= 0 && ( arr [ i ] [ j ] == 0 || i == j ) ) j -- ; if ( j == - 1 ) { res = i ; break ; } else i ++ ; } else { w...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def find ( arr ) : n = len ( arr ) i = 0 j = n - 1 res = - 1 while i < n and j >= 0 : if arr [ i ] [ j ] == 0 : while j >= 0 and ( arr [ i ] [ j ] == 0 or i == j ) : j -= 1 if j == - 1 : res = i break ...
python-cpp-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS_1
python-cpp
def printArray ( a , n ) : for i in a : print ( i , end = " " ) print ( )
void printArray ( int * a , int n ) { for ( int i = 0 ; i < n ; i ++ ) cout << a [ i ] << " " ; cout << endl ; return ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printArray ( a , n ) : for i in a : print ( i , end = " " ) print ( ) </Code>
python-cpp-SORT_ARRAY_CONVERTING_ELEMENTS_SQUARES_1
python-cpp
def sortSquares ( arr , n ) : K = 0 for K in range ( n ) : if ( arr [ K ] >= 0 ) : break i = K - 1 j = K ind = 0 temp = [ 0 ] * n while ( i >= 0 and j < n ) : if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) : temp [ ind ] = arr [ i ] * arr [ i ] ...
void sortSquares ( int arr [ ] , int n ) { int K = 0 ; for ( K = 0 ; K < n ; K ++ ) if ( arr [ K ] >= 0 ) break ; int i = K - 1 ; int j = K ; int ind = 0 ; int temp [ n ] ; while ( i >= 0 && j < n ) { if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) { temp [ ind ] = arr [ i ] * arr [ i ]...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sortSquares ( arr , n ) : K = 0 for K in range ( n ) : if ( arr [ K ] >= 0 ) : break i = K - 1 j = K ind = 0 temp = [ 0 ] * n while ( i >= 0 and j < n ) : if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) : temp [ ind ] = arr [ i ] * arr ...
python-cpp-PRINT_MAXIMUM_SHORTEST_DISTANCE
python-cpp
def find_maximum ( a , n , k ) : b = dict ( ) for i in range ( n ) : x = a [ i ] d = min ( 1 + i , n - i ) if x not in b.keys ( ) : b [ x ] = d else : b [ x ] = min ( d , b [ x ] ) ans = 10 ** 9 for i in range ( n ) : x = a [ i ] if...
int find_maximum ( int a [ ] , int n , int k ) { unordered_map < int , int > b ; for ( int i = 0 ; i < n ; i ++ ) { int x = a [ i ] ; int d = min ( 1 + i , n - i ) ; if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ; else b [ x ] = min ( d , b [ x ] ) ; } int ans = INT_MAX ; for ( int i = ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def find_maximum ( a , n , k ) : b = dict ( ) for i in range ( n ) : x = a [ i ] d = min ( 1 + i , n - i ) if x not in b.keys ( ) : b [ x ] = d else : b [ x ] = min ( d , b [ x ] ) ans = 10 ** 9 for i in range ( n ) : x = a [ i ] ...
python-cpp-MAXIMUM_CONSECUTIVE_REPEATING_CHARACTER_STRING_1
python-cpp
def maxRepeating ( str ) : n = len ( str ) count = 0 res = str [ 0 ] cur_count = 1 for i in range ( n ) : if ( i < n - 1 and str [ i ] == str [ i + 1 ] ) : cur_count += 1 else : if cur_count > count : count = cur_count res = str...
char maxRepeating ( string str ) { int n = str . length ( ) ; int count = 0 ; char res = str [ 0 ] ; int cur_count = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxRepeating ( str ) : n = len ( str ) count = 0 res = str [ 0 ] cur_count = 1 for i in range ( n ) : if ( i < n - 1 and str [ i ] == str [ i + 1 ] ) : cur_count += 1 else : if cur_count > count : count = cur_count re...
python-cpp-ARRAY_RANGE_QUERIES_ELEMENTS_FREQUENCY_VALUE
python-cpp
def solveQuery ( start , end , arr ) : frequency = dict ( ) for i in range ( start , end + 1 ) : if arr [ i ] in frequency.keys ( ) : frequency [ arr [ i ] ] += 1 else : frequency [ arr [ i ] ] = 1 count = 0 for x in frequency : if x == frequency [ x ] : ...
int solveQuery ( int start , int end , int arr [ ] ) { unordered_map < int , int > frequency ; for ( int i = start ; i <= end ; i ++ ) frequency [ arr [ i ] ] ++ ; int count = 0 ; for ( auto x : frequency ) if ( x . first == x . second ) count ++ ; return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solveQuery ( start , end , arr ) : frequency = dict ( ) for i in range ( start , end + 1 ) : if arr [ i ] in frequency.keys ( ) : frequency [ arr [ i ] ] += 1 else : frequency [ arr [ i ] ] = 1 count = 0 for x in frequency : if x == frequency [ ...
python-cpp-PYTHON_PROGRAM_FIND_PERIMETER_CIRCUMFERENCE_SQUARE_RECTANGLE
python-cpp
def Circumference ( a ) : return ( 4 * a )
int Circumference ( int a ) { return 4 * a ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def Circumference ( a ) : return ( 4 * a ) </Code>
python-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_3
python-cpp
def numberOfPaths ( m , n ) : for i in range ( n , ( m + n - 1 ) ) : path *= i path //= ( i - n + 1 ) return path
int numberOfPaths ( int m , int n ) { int path = 1 ; for ( int i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def numberOfPaths ( m , n ) : for i in range ( n , ( m + n - 1 ) ) : path *= i path //= ( i - n + 1 ) return path </Code>
python-cpp-POINT_CLIPPING_ALGORITHM_COMPUTER_GRAPHICS
python-cpp
def pointClip ( XY , n , Xmin , Ymin , Xmax , Ymax ) : print ( "Point inside the viewing pane:" ) for i in range ( n ) : if ( ( XY [ i ] [ 0 ] >= Xmin ) and ( XY [ i ] [ 0 ] <= Xmax ) ) : if ( ( XY [ i ] [ 1 ] >= Ymin ) and ( XY [ i ] [ 1 ] <= Ymax ) ) : print ( "[" , XY [ i ...
void pointClip ( int XY [ ] [ 2 ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) { cout << "Point inside the viewing pane:" << endl ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) { if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i ] [ 1 ] <= Ymax ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def pointClip ( XY , n , Xmin , Ymin , Xmax , Ymax ) : print ( "Point inside the viewing pane:" ) for i in range ( n ) : if ( ( XY [ i ] [ 0 ] >= Xmin ) and ( XY [ i ] [ 0 ] <= Xmax ) ) : if ( ( XY [ i ] [ 1 ] >= Ymin ) and ( XY [ i ] [ 1 ] <= Ymax ) ) : print ( "[" , ...
python-cpp-MAXIMUM_SUM_INCREASING_SUBSEQUENCE_FROM_A_PREFIX_AND_A_GIVEN_ELEMENT_AFTER_PREFIX_IS_MUST
python-cpp
def pre_compute ( a , n , index , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] for i in range ( n ) : if a [ i ] > a [ 0 ] : dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] else : dp [ 0 ] [ i ] = a [ i ] for i in range ( 1 , n ) : for j in range ( n ) : ...
ll pre_compute ( ll a [ ] , ll n , ll index , ll k ) { ll dp [ n ] [ n ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ; else dp [ 0 ] [ i ] = a [ i ] ; } for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def pre_compute ( a , n , index , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] for i in range ( n ) : if a [ i ] > a [ 0 ] : dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] else : dp [ 0 ] [ i ] = a [ i ] for i in range ( 1 , n ) : for j in range (...
python-cpp-SPLIT_ARRAY_ADD_FIRST_PART_END
python-cpp
def splitArr ( arr , n , k ) : for i in range ( 0 , k ) : x = arr [ 0 ] for j in range ( 0 , n - 1 ) : arr [ j ] = arr [ j + 1 ] arr [ n - 1 ] = x
void splitArr ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ 0 ] ; for ( int j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def splitArr ( arr , n , k ) : for i in range ( 0 , k ) : x = arr [ 0 ] for j in range ( 0 , n - 1 ) : arr [ j ] = arr [ j + 1 ] arr [ n - 1 ] = x </Code>
python-cpp-MINIMUM_LENGTH_SUBARRAY_SUM_GREATER_GIVEN_VALUE_1
python-cpp
def smallestSubWithSum ( arr , n , x ) : curr_sum = 0 min_len = n + 1 start = 0 end = 0 while ( end < n ) : while ( curr_sum <= x and end < n ) : if ( curr_sum <= 0 and x > 0 ) : start = end curr_sum = 0 curr_sum += arr [ end ] ...
int smallestSubWithSum ( int arr [ ] , int n , int x ) { int curr_sum = 0 , min_len = n + 1 ; int start = 0 , end = 0 ; while ( end < n ) { while ( curr_sum <= x && end < n ) { if ( curr_sum <= 0 && x > 0 ) { start = end ; curr_sum = 0 ; } curr_sum += arr [ end ++ ] ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def smallestSubWithSum ( arr , n , x ) : curr_sum = 0 min_len = n + 1 start = 0 end = 0 while ( end < n ) : while ( curr_sum <= x and end < n ) : if ( curr_sum <= 0 and x > 0 ) : start = end curr_sum = 0 curr_sum += arr [ end ] ...
python-cpp-ARRAY_ELEMENT_MOVED_K_USING_SINGLE_MOVES
python-cpp
def winner ( a , n , k ) : if k >= n - 1 : return n best = 0 times = 0 for i in range ( n ) : if a [ i ] > best : best = a [ i ] if i == True : times = 1 else : times += 1 if times >= k : return best retu...
int winner ( int a [ ] , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def winner ( a , n , k ) : if k >= n - 1 : return n best = 0 times = 0 for i in range ( n ) : if a [ i ] > best : best = a [ i ] if i == True : times = 1 else : times += 1 if times >= k : return best ...
python-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_20
python-cpp
def divisibleBy20 ( num ) : lastTwoDigits = int ( num [ - 2 : ] ) return ( ( lastTwoDigits % 5 == 0 and lastTwoDigits % 4 == 0 ) )
bool divisibleBy20 ( string num ) { int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ; return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def divisibleBy20 ( num ) : lastTwoDigits = int ( num [ - 2 : ] ) return ( ( lastTwoDigits % 5 == 0 and lastTwoDigits % 4 == 0 ) ) </Code>
python-cpp-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2_1
python-cpp
def countSolutions ( n ) : x = 0 res = 0 yCount = 0 while ( yCount * yCount < n ) : yCount = yCount + 1 while ( yCount != 0 ) : res = res + yCount x = x + 1 while ( yCount != 0 and ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) : yCount = yCount - 1 ...
int countSolutions ( int n ) { int x = 0 , yCount , res = 0 ; for ( yCount = 0 ; yCount * yCount < n ; yCount ++ ) ; while ( yCount != 0 ) { res += yCount ; x ++ ; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ; } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countSolutions ( n ) : x = 0 res = 0 yCount = 0 while ( yCount * yCount < n ) : yCount = yCount + 1 while ( yCount != 0 ) : res = res + yCount x = x + 1 while ( yCount != 0 and ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) : yCount = yCount...
python-cpp-FIND_NUMBER_OF_TRIANGLES_POSSIBLE_1
python-cpp
def CountTriangles ( A ) : n = len ( A ) A.sort ( ) count = 0 for i in range ( n - 1 , 0 , - 1 ) : l = 0 r = i - 1 while ( l < r ) : if ( A [ l ] + A [ r ] > A [ i ] ) : count += r - l r -= 1 else : l += 1 ...
void CountTriangles ( vector < int > A ) { int n = A . size ( ) ; sort ( A . begin ( ) , A . end ( ) ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def CountTriangles ( A ) : n = len ( A ) A.sort ( ) count = 0 for i in range ( n - 1 , 0 , - 1 ) : l = 0 r = i - 1 while ( l < r ) : if ( A [ l ] + A [ r ] > A [ i ] ) : count += r - l r -= 1 else : l ...
python-cpp-NEXT_HIGHER_NUMBER_WITH_SAME_NUMBER_OF_SET_BITS
python-cpp
def snoob ( x ) : next = 0 if ( x ) : rightOne = x & - ( x ) nextHigherOneBit = x + int ( rightOne ) rightOnesPattern = x ^ int ( nextHigherOneBit ) rightOnesPattern = ( int ( rightOnesPattern ) / int ( rightOne ) ) rightOnesPattern = int ( rightOnesPattern ) >> 2 ...
uint_t snoob ( uint_t x ) { uint_t rightOne ; uint_t nextHigherOneBit ; uint_t rightOnesPattern ; uint_t next = 0 ; if ( x ) { rightOne = x & - ( signed ) x ; nextHigherOneBit = x + rightOne ; rightOnesPattern = x ^ nextHigherOneBit ; rightOnesPattern = ( rightOnesPattern ) / rightOne ; ri...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def snoob ( x ) : next = 0 if ( x ) : rightOne = x & - ( x ) nextHigherOneBit = x + int ( rightOne ) rightOnesPattern = x ^ int ( nextHigherOneBit ) rightOnesPattern = ( int ( rightOnesPattern ) / int ( rightOne ) ) rightOnesPattern = int ( rightOnesPattern ) >> 2 ...
python-cpp-CHECK_ARRAY_REPRESENTS_INORDER_BINARY_SEARCH_TREE_NOT
python-cpp
def isInorder ( arr , n ) : if ( n == 0 or n == 1 ) : return True for i in range ( 1 , n , 1 ) : if ( arr [ i - 1 ] > arr [ i ] ) : return False return True
bool isInorder ( int arr [ ] , int n ) { if ( n == 0 || n == 1 ) return true ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ; return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isInorder ( arr , n ) : if ( n == 0 or n == 1 ) : return True for i in range ( 1 , n , 1 ) : if ( arr [ i - 1 ] > arr [ i ] ) : return False return True </Code>
python-cpp-LONGEST_INCREASING_SUBSEQUENCE
python-cpp
def lis ( arr ) : global maximum n = len ( arr ) maximum = 1 _lis ( arr , n ) return maximum
int lis ( int arr [ ] , int n ) { int max = 1 ; _lis ( arr , n , & max ) ; return max ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lis ( arr ) : global maximum n = len ( arr ) maximum = 1 _lis ( arr , n ) return maximum </Code>
python-cpp-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY
python-cpp
def maxProduct ( arr , n ) : if n < 3 : return - 1 max_product = - ( sys.maxsize - 1 ) for i in range ( 0 , n - 2 ) : for j in range ( i + 1 , n - 1 ) : for k in range ( j + 1 , n ) : max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) return...
int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return - 1 ; int max_product = INT_MIN ; for ( int i = 0 ; i < n - 2 ; i ++ ) for ( int j = i + 1 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ; return max_product...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxProduct ( arr , n ) : if n < 3 : return - 1 max_product = - ( sys.maxsize - 1 ) for i in range ( 0 , n - 2 ) : for j in range ( i + 1 , n - 1 ) : for k in range ( j + 1 , n ) : max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ...
python-cpp-FREQUENCY_ELEMENT_UNSORTED_ARRAY
python-cpp
def countFreq ( a , n ) : hm = { } for i in range ( 0 , n ) : hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1 st = set ( ) for x in hm : st.add ( ( x , hm [ x ] ) ) cumul = 0 for x in sorted ( st ) : cumul += x [ 1 ] print ( x [ 0 ] , cumul )
void countFreq ( int a [ ] , int n ) { unordered_map < int , int > hm ; for ( int i = 0 ; i < n ; i ++ ) hm [ a [ i ] ] ++ ; set < pair < int , int >> st ; for ( auto x : hm ) { st . insert ( { x . first , x . second } ) ; } int cumul = 0 ; for ( auto x : st ) { cumul += x ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countFreq ( a , n ) : hm = { } for i in range ( 0 , n ) : hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1 st = set ( ) for x in hm : st.add ( ( x , hm [ x ] ) ) cumul = 0 for x in sorted ( st ) : cumul += x [ 1 ] print ( x [ 0 ] , cumul ) </Code>
python-cpp-COMPUTE_NCR_P_SET_1_INTRODUCTION_AND_DYNAMIC_PROGRAMMING_SOLUTION
python-cpp
def nCrModp ( n , r , p ) : C = [ 0 for i in range ( r + 1 ) ] C [ 0 ] = 1 for i in range ( 1 , n + 1 ) : for j in range ( min ( i , r ) , 0 , - 1 ) : C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p return C [ r ]
int nCrModp ( int n , int r , int p ) { int C [ r + 1 ] ; memset ( C , 0 , sizeof ( C ) ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nCrModp ( n , r , p ) : C = [ 0 for i in range ( r + 1 ) ] C [ 0 ] = 1 for i in range ( 1 , n + 1 ) : for j in range ( min ( i , r ) , 0 , - 1 ) : C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p return C [ r ] </Code>
python-cpp-COCKTAIL_SORT
python-cpp
def cocktailSort ( a ) : n = len ( a ) swapped = True start = 0 end = n - 1 while ( swapped == True ) : swapped = False for i in range ( start , end ) : if ( a [ i ] > a [ i + 1 ] ) : a [ i ] , a [ i + 1 ] = a [ i + 1 ] , a [ i ] swapped = ...
void CocktailSort ( int a [ ] , int n ) { bool swapped = true ; int start = 0 ; int end = n - 1 ; while ( swapped ) { swapped = false ; for ( int i = start ; i < end ; ++ i ) { if ( a [ i ] > a [ i + 1 ] ) { swap ( a [ i ] , a [ i + 1 ] ) ; swapped = true ; } } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def cocktailSort ( a ) : n = len ( a ) swapped = True start = 0 end = n - 1 while ( swapped == True ) : swapped = False for i in range ( start , end ) : if ( a [ i ] > a [ i + 1 ] ) : a [ i ] , a [ i + 1 ] = a [ i + 1 ] , a [ i ] swa...
python-cpp-WORD_WRAP_PROBLEM_SPACE_OPTIMIZED_SOLUTION
python-cpp
def solveWordWrap ( arr , n , k ) : dp = [ 0 ] * n ans = [ 0 ] * n dp [ n - 1 ] = 0 ans [ n - 1 ] = n - 1 for i in range ( n - 2 , - 1 , - 1 ) : currlen = - 1 dp [ i ] = sys.maxsize for j in range ( i , n ) : currlen += ( arr [ j ] + 1 ) if ( currlen >...
void solveWordWrap ( int arr [ ] , int n , int k ) { int i , j ; int currlen ; int cost ; int dp [ n ] ; int ans [ n ] ; dp [ n - 1 ] = 0 ; ans [ n - 1 ] = n - 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { currlen = - 1 ; dp [ i ] = INT_MAX ; for ( j = i ; j < n ; j ++ ) { currle...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solveWordWrap ( arr , n , k ) : dp = [ 0 ] * n ans = [ 0 ] * n dp [ n - 1 ] = 0 ans [ n - 1 ] = n - 1 for i in range ( n - 2 , - 1 , - 1 ) : currlen = - 1 dp [ i ] = sys.maxsize for j in range ( i , n ) : currlen += ( arr [ j ] + 1 ) if ( cu...
python-cpp-BINARY_SEARCH_1
python-cpp
def binarySearch ( arr , l , r , x ) : while l <= r : mid = l + ( r - l ) // 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : l = mid + 1 else : r = mid - 1 return - 1
int binarySearch ( int arr [ ] , int l , int r , int x ) { while ( l <= r ) { int m = l + ( r - l ) / 2 ; if ( arr [ m ] == x ) return m ; if ( arr [ m ] < x ) l = m + 1 ; else r = m - 1 ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binarySearch ( arr , l , r , x ) : while l <= r : mid = l + ( r - l ) // 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : l = mid + 1 else : r = mid - 1 return - 1 </Code>
python-cpp-MINIMUM_XOR_VALUE_PAIR
python-cpp
def minXOR ( arr , n ) : arr.sort ( ) min_xor = 999999 val = 0 for i in range ( 0 , n - 1 ) : for j in range ( i + 1 , n - 1 ) : val = arr [ i ] ^ arr [ j ] min_xor = min ( min_xor , val ) return min_xor
int minXOR ( int arr [ ] , int n ) { int min_xor = INT_MAX ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) min_xor = min ( min_xor , arr [ i ] ^ arr [ j ] ) ; return min_xor ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minXOR ( arr , n ) : arr.sort ( ) min_xor = 999999 val = 0 for i in range ( 0 , n - 1 ) : for j in range ( i + 1 , n - 1 ) : val = arr [ i ] ^ arr [ j ] min_xor = min ( min_xor , val ) return min_xor </Code>
python-cpp-FIND_SUM_EVEN_FACTORS_NUMBER
python-cpp
def sumofFactors ( n ) : if ( n % 2 != 0 ) : return 0 res = 1 for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) : count = 0 curr_sum = 1 curr_term = 1 while ( n % i == 0 ) : count = count + 1 n = n // i if ( i == 2 and count ==...
int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 , curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; cu...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumofFactors ( n ) : if ( n % 2 != 0 ) : return 0 res = 1 for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) : count = 0 curr_sum = 1 curr_term = 1 while ( n % i == 0 ) : count = count + 1 n = n // i if ( i == 2 and c...
python-cpp-FIRST_UPPERCASE_LETTER_IN_A_STRING_ITERATIVE_AND_RECURSIVE_1
python-cpp
def first ( str , i ) : if ( str [ i ] == '\0' ) : return 0 if ( str [ i ].isupper ( ) ) : return str [ i ] return first ( str , i + 1 )
char first ( string str , int i = 0 ) { if ( str [ i ] == '\0' ) return 0 ; if ( isupper ( str [ i ] ) ) return str [ i ] ; return first ( str , i + 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def first ( str , i ) : if ( str [ i ] == '\0' ) : return 0 if ( str [ i ].isupper ( ) ) : return str [ i ] return first ( str , i + 1 ) </Code>
python-cpp-MAXIMUM_GAMES_PLAYED_WINNER
python-cpp
def maxGameByWinner ( N ) : dp = [ 0 for i in range ( N ) ] dp [ 0 ] = 1 dp [ 1 ] = 2 i = 1 while dp [ i ] <= N : i = i + 1 dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] return ( i - 1 )
int maxGameByWinner ( int N ) { int dp [ N ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 2 ; int i = 2 ; do { dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ; } while ( dp [ i ++ ] <= N ) ; return ( i - 2 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxGameByWinner ( N ) : dp = [ 0 for i in range ( N ) ] dp [ 0 ] = 1 dp [ 1 ] = 2 i = 1 while dp [ i ] <= N : i = i + 1 dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] return ( i - 1 ) </Code>
python-cpp-EQUILIBRIUM_INDEX_OF_AN_ARRAY_1
python-cpp
def equilibrium ( arr ) : total_sum = sum ( arr ) leftsum = 0 for i , num in enumerate ( arr ) : total_sum -= num if leftsum == total_sum : return i leftsum += num return - 1
int equilibrium ( int arr [ ] , int n ) { int sum = 0 ; int leftsum = 0 ; for ( int i = 0 ; i < n ; ++ i ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; ++ i ) { sum -= arr [ i ] ; if ( leftsum == sum ) return i ; leftsum += arr [ i ] ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def equilibrium ( arr ) : total_sum = sum ( arr ) leftsum = 0 for i , num in enumerate ( arr ) : total_sum -= num if leftsum == total_sum : return i leftsum += num return - 1 </Code>
python-cpp-PROBABILITY_THREE_RANDOMLY_CHOSEN_NUMBERS_AP
python-cpp
def procal ( n ) : return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 )
double procal ( int n ) { return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def procal ( n ) : return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) </Code>
python-cpp-REARRANGE_ARRAY_MAXIMUM_MINIMUM_FORM
python-cpp
def rearrange ( arr , n ) : temp = n * [ None ] small , large = 0 , n - 1 flag = True for i in range ( n ) : if flag is True : temp [ i ] = arr [ large ] large -= 1 else : temp [ i ] = arr [ small ] small += 1 flag = bool ( 1 - flag...
void rearrange ( int arr [ ] , int n ) { int temp [ n ] ; int small = 0 , large = n - 1 ; int flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( flag ) temp [ i ] = arr [ large -- ] ; else temp [ i ] = arr [ small ++ ] ; flag = ! flag ; } for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = t...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def rearrange ( arr , n ) : temp = n * [ None ] small , large = 0 , n - 1 flag = True for i in range ( n ) : if flag is True : temp [ i ] = arr [ large ] large -= 1 else : temp [ i ] = arr [ small ] small += 1 flag = bool ( 1...
python-cpp-PRINT_DIGITS_POSITION_REMOVED_MAKE_NUMBER_DIVISIBLE_6
python-cpp
def greatest ( s ) : n = len ( s ) a = [ 0 for i in range ( n ) ] Sum = 0 for i in range ( n ) : a [ i ] = ord ( s [ i ] ) - ord ( '0' ) Sum += a [ i ] if ( a [ n - 1 ] % 2 ) : if ( a [ n - 2 ] % 2 != 0 or ( Sum - a [ n - 1 ] ) % 3 != 0 ) : print ( "-1" ) ...
void greatest ( string s ) { int n = s . length ( ) ; int a [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] = s [ i ] - '0' ; sum += a [ i ] ; } if ( a [ n - 1 ] % 2 ) { if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) { cout << "-1" << endl ; } el...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def greatest ( s ) : n = len ( s ) a = [ 0 for i in range ( n ) ] Sum = 0 for i in range ( n ) : a [ i ] = ord ( s [ i ] ) - ord ( '0' ) Sum += a [ i ] if ( a [ n - 1 ] % 2 ) : if ( a [ n - 2 ] % 2 != 0 or ( Sum - a [ n - 1 ] ) % 3 != 0 ) : print ( "-1" ) ...
python-cpp-CHECK_IF_X_CAN_GIVE_CHANGE_TO_EVERY_PERSON_IN_THE_QUEUE
python-cpp
def isChangeable ( notes , n ) : fiveCount = 0 tenCount = 0 for i in range ( n ) : if ( notes [ i ] == 5 ) : fiveCount += 1 elif ( notes [ i ] == 10 ) : if ( fiveCount > 0 ) : fiveCount -= 1 tenCount += 1 else : ...
int isChangeable ( int notes [ ] , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isChangeable ( notes , n ) : fiveCount = 0 tenCount = 0 for i in range ( n ) : if ( notes [ i ] == 5 ) : fiveCount += 1 elif ( notes [ i ] == 10 ) : if ( fiveCount > 0 ) : fiveCount -= 1 tenCount += 1 else : ...
python-cpp-LONGEST_COMMON_SUBSTRING
python-cpp
def LCSubStr ( X , Y , m , n ) : LCSuff = [ [ 0 for k in range ( n + 1 ) ] for l in range ( m + 1 ) ] result = 0 for i in range ( m + 1 ) : for j in range ( n + 1 ) : if ( i == 0 or j == 0 ) : LCSuff [ i ] [ j ] = 0 elif ( X [ i - 1 ] == Y [ j - 1 ] ) : ...
int LCSubStr ( char * X , char * Y , int m , int n ) { int LCSuff [ m + 1 ] [ n + 1 ] ; int result = 0 ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 || j == 0 ) LCSuff [ i ] [ j ] = 0 ; else if ( X [ i - 1 ] == Y [ j - 1 ] ) { LCSuff [ i ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def LCSubStr ( X , Y , m , n ) : LCSuff = [ [ 0 for k in range ( n + 1 ) ] for l in range ( m + 1 ) ] result = 0 for i in range ( m + 1 ) : for j in range ( n + 1 ) : if ( i == 0 or j == 0 ) : LCSuff [ i ] [ j ] = 0 elif ( X [ i - 1 ] == Y [ j - 1 ] ) :...
python-cpp-MAXIMUM_SUBSEQUENCE_SUM_SUCH_THAT_NO_THREE_ARE_CONSECUTIVE_1
python-cpp
def maxSumWO3Consec ( n ) : if ( sum [ n ] != - 1 ) : return sum [ n ] if ( n == 0 ) : sum [ n ] = 0 return sum [ n ] if ( n == 1 ) : sum [ n ] = arr [ 0 ] return sum [ n ] if ( n == 2 ) : sum [ n ] = arr [ 1 ] + arr [ 0 ] return sum [ n ] sum ...
int maxSumWO3Consec ( int n ) { if ( sum [ n ] != - 1 ) return sum [ n ] ; if ( n == 0 ) return sum [ n ] = 0 ; if ( n == 1 ) return sum [ n ] = arr [ 0 ] ; if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; return sum [ n ] = max ( max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n -...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSumWO3Consec ( n ) : if ( sum [ n ] != - 1 ) : return sum [ n ] if ( n == 0 ) : sum [ n ] = 0 return sum [ n ] if ( n == 1 ) : sum [ n ] = arr [ 0 ] return sum [ n ] if ( n == 2 ) : sum [ n ] = arr [ 1 ] + arr [ 0 ] return sum [ n ] ...
python-cpp-FIBONACCI_MODULO_P
python-cpp
def findMinZero ( p ) : first = 1 second = 1 number = 2 next = 1 while ( next ) : next = ( first + second ) % p first = second second = next number = number + 1 return number
int findMinZero ( int p ) { int first = 1 , second = 1 , number = 2 , next = 1 ; while ( next ) { next = ( first + second ) % p ; first = second ; second = next ; number ++ ; } return number ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMinZero ( p ) : first = 1 second = 1 number = 2 next = 1 while ( next ) : next = ( first + second ) % p first = second second = next number = number + 1 return number </Code>
python-cpp-REORDER_A_ARRAY_ACCORDING_TO_GIVEN_INDEXES_1
python-cpp
def reorder ( arr , index , n ) : for i in range ( 0 , n ) : while ( index [ i ] != i ) : oldTargetI = index [ index [ i ] ] oldTargetE = arr [ index [ i ] ] arr [ index [ i ] ] = arr [ i ] index [ index [ i ] ] = index [ i ] index [ i ] = oldTarge...
void reorder ( int arr [ ] , int index [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { while ( index [ i ] != i ) { int oldTargetI = index [ index [ i ] ] ; char oldTargetE = arr [ index [ i ] ] ; arr [ index [ i ] ] = arr [ i ] ; index [ index [ i ] ] = index [ i ] ; index [ ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def reorder ( arr , index , n ) : for i in range ( 0 , n ) : while ( index [ i ] != i ) : oldTargetI = index [ index [ i ] ] oldTargetE = arr [ index [ i ] ] arr [ index [ i ] ] = arr [ i ] index [ index [ i ] ] = index [ i ] index [ i ] = o...
python-cpp-LENGTH_LONGEST_SUB_STRING_CAN_MAKE_REMOVED
python-cpp
def longestNull ( S ) : arr = [ ] arr.append ( [ '@' , - 1 ] ) maxlen = 0 for i in range ( len ( S ) ) : arr.append ( [ S [ i ] , i ] ) while ( len ( arr ) >= 3 and arr [ len ( arr ) - 3 ] [ 0 ] == '1' and arr [ len ( arr ) - 2 ] [ 0 ] == '0' and arr [ len ( arr ) - 1 ] [ 0 ] == '0' ) : ...
int longestNull ( string str ) { vector < pair < char , int >> arr ; arr . push_back ( { '@' , - 1 } ) ; int maxlen = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { arr . push_back ( { str [ i ] , i } ) ; while ( arr . size ( ) >= 3 && arr [ arr . size ( ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longestNull ( S ) : arr = [ ] arr.append ( [ '@' , - 1 ] ) maxlen = 0 for i in range ( len ( S ) ) : arr.append ( [ S [ i ] , i ] ) while ( len ( arr ) >= 3 and arr [ len ( arr ) - 3 ] [ 0 ] == '1' and arr [ len ( arr ) - 2 ] [ 0 ] == '0' and arr [ len ( arr ) - 1 ] [ 0 ] == '...
python-cpp-SUM_FIBONACCI_NUMBERS
python-cpp
def calculateSum ( n ) : if ( n <= 0 ) : return 0 fibo = [ 0 ] * ( n + 1 ) fibo [ 1 ] = 1 sm = fibo [ 0 ] + fibo [ 1 ] for i in range ( 2 , n + 1 ) : fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] sm = sm + fibo [ i ] return sm
int calculateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ n + 1 ] ; fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ; int sum = fibo [ 0 ] + fibo [ 1 ] ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += fibo [ i ] ; } return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def calculateSum ( n ) : if ( n <= 0 ) : return 0 fibo = [ 0 ] * ( n + 1 ) fibo [ 1 ] = 1 sm = fibo [ 0 ] + fibo [ 1 ] for i in range ( 2 , n + 1 ) : fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] sm = sm + fibo [ i ] return sm </Code>
python-cpp-SHORTEST_PATH_EXACTLY_K_EDGES_DIRECTED_WEIGHTED_GRAPH
python-cpp
def shortestPath ( graph , u , v , k ) : V = 4 INF = 999999999999 if k == 0 and u == v : return 0 if k == 1 and graph [ u ] [ v ] != INF : return graph [ u ] [ v ] if k <= 0 : return INF res = INF for i in range ( V ) : if graph [ u ] [ i ] != INF and u != i a...
INT_MAX int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 0 ; if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ; if ( k <= 0 ) return INF ; int res = INF ; for ( int i = 0 ; i < V ; i ++ ) { if ( graph [ u ] [ i ] != INF && u != i && v ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def shortestPath ( graph , u , v , k ) : V = 4 INF = 999999999999 if k == 0 and u == v : return 0 if k == 1 and graph [ u ] [ v ] != INF : return graph [ u ] [ v ] if k <= 0 : return INF res = INF for i in range ( V ) : if graph [ u ] [ i ] != INF and u...