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-CONVERT_ALTERNATE_CHARACTERS_STRING_UPPER_CASE
python-cpp
def convertOpposite ( str ) : ln = len ( str ) for i in range ( ln ) : if str [ i ] >= 'a' and str [ i ] <= 'z' : str [ i ] = chr ( ord ( str [ i ] ) - 32 ) elif str [ i ] >= 'A' and str [ i ] <= 'Z' : str [ i ] = chr ( ord ( str [ i ] ) + 32 )
void convertOpposite ( string & str ) { int ln = str . length ( ) ; for ( int i = 0 ; i < ln ; i ++ ) { if ( str [ i ] >= 'a' && str [ i ] <= 'z' ) str [ i ] = str [ i ] - 32 ; else if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) str [ i ] = str [ i ] + 32 ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def convertOpposite ( str ) : ln = len ( str ) for i in range ( ln ) : if str [ i ] >= 'a' and str [ i ] <= 'z' : str [ i ] = chr ( ord ( str [ i ] ) - 32 ) elif str [ i ] >= 'A' and str [ i ] <= 'Z' : str [ i ] = chr ( ord ( str [ i ] ) + 32 ) </Code>
python-cpp-PROGRAM_DECIMAL_BINARY_CONVERSION_2
python-cpp
def decimalToBinary ( N ) : B_Number = 0 cnt = 0 while ( N != 0 ) : rem = N % 2 c = pow ( 10 , cnt ) B_Number += rem * c N //= 2 cnt += 1 return B_Number
unsigned long long int int decimalToBinary ( int N ) { ull B_Number = 0 ; int cnt = 0 ; while ( N != 0 ) { int rem = N % 2 ; ull c = pow ( 10 , cnt ) ; B_Number += rem * c ; N /= 2 ; cnt ++ ; } return B_Number ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def decimalToBinary ( N ) : B_Number = 0 cnt = 0 while ( N != 0 ) : rem = N % 2 c = pow ( 10 , cnt ) B_Number += rem * c N //= 2 cnt += 1 return B_Number </Code>
python-cpp-FIND_REPETITIVE_ELEMENT_1_N_1_1
python-cpp
def findRepeating ( arr , n ) : s = set ( ) for i in range ( n ) : if arr [ i ] in s : return arr [ i ] s.add ( arr [ i ] ) rteurn - 1
int findRepeating ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) != s . end ( ) ) return arr [ i ] ; s . insert ( arr [ i ] ) ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findRepeating ( arr , n ) : s = set ( ) for i in range ( n ) : if arr [ i ] in s : return arr [ i ] s.add ( arr [ i ] ) rteurn - 1 </Code>
python-cpp-PROGRAM_TO_FIND_TRANSPOSE_OF_A_MATRIX_2
python-cpp
def transpose ( A ) : for i in range ( N ) : for j in range ( i + 1 , N ) : A [ i ] [ j ] , A [ j ] [ i ] = A [ j ] [ i ] , A [ i ] [ j ]
void transpose ( int A [ ] [ N ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = i + 1 ; j < N ; j ++ ) swap ( A [ i ] [ j ] , A [ j ] [ i ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def transpose ( A ) : for i in range ( N ) : for j in range ( i + 1 , N ) : A [ i ] [ j ] , A [ j ] [ i ] = A [ j ] [ i ] , A [ i ] [ j ] </Code>
python-cpp-PRINT_POSSIBLE_SUMS_CONSECUTIVE_NUMBERS_SUM_N
python-cpp
def findConsecutive ( N ) : start = 1 end = ( N + 1 ) // 2 while ( start < end ) : Sum = 0 for i in range ( start , end + 1 ) : Sum = Sum + i if ( Sum == N ) : for j in range ( start , i + 1 ) : print ( j , end = " " ) ...
void findConsecutive ( int N ) { int start = 1 , end = ( N + 1 ) / 2 ; while ( start < end ) { int sum = 0 ; for ( int i = start ; i <= end ; i ++ ) { sum = sum + i ; if ( sum == N ) { for ( int j = start ; j <= i ; j ++ ) printf ( "%d " , j ) ; printf ( "...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findConsecutive ( N ) : start = 1 end = ( N + 1 ) // 2 while ( start < end ) : Sum = 0 for i in range ( start , end + 1 ) : Sum = Sum + i if ( Sum == N ) : for j in range ( start , i + 1 ) : print ( j , end = " " ) ...
python-cpp-PRINT_POSSIBLE_STRINGS_CAN_MADE_PLACING_SPACES_2
python-cpp
def printSubsequences ( str ) : n = len ( str ) opsize = int ( pow ( 2 , n - 1 ) ) for counter in range ( opsize ) : for j in range ( n ) : print ( str [ j ] , end = "" ) if ( counter & ( 1 << j ) ) : print ( "" , end = "" ) print ( "\n" , end = "" )
void printSubsequences ( string str ) { int n = str . length ( ) ; unsigned int opsize = pow ( 2 , n - 1 ) ; for ( int counter = 0 ; counter < opsize ; counter ++ ) { for ( int j = 0 ; j < n ; j ++ ) { cout << str [ j ] ; if ( counter & ( 1 << j ) ) cout << " " ; } cout << endl...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printSubsequences ( str ) : n = len ( str ) opsize = int ( pow ( 2 , n - 1 ) ) for counter in range ( opsize ) : for j in range ( n ) : print ( str [ j ] , end = "" ) if ( counter & ( 1 << j ) ) : print ( "" , end = "" ) print ( "\n" , end =...
python-cpp-MAXIMUM_AREA_QUADRILATERAL
python-cpp
def maxArea ( a , b , c , d ) : semiperimeter = ( a + b + c + d ) / 2 return math.sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) )
double maxArea ( double a , double b , double c , double d ) { double semiperimeter = ( a + b + c + d ) / 2 ; return sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxArea ( a , b , c , d ) : semiperimeter = ( a + b + c + d ) / 2 return math.sqrt ( ( semiperimeter - a ) * ( semiperimeter - b ) * ( semiperimeter - c ) * ( semiperimeter - d ) ) </Code>
python-cpp-POSITION_OF_RIGHTMOST_SET_BIT_2
python-cpp
def Right_most_setbit ( num ) : pos = 1 for i in range ( INT_SIZE ) : if not ( num & ( 1 << i ) ) : pos += 1 else : break return pos
int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ! ( num & ( 1 << i ) ) ) pos ++ ; else break ; } return pos ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def Right_most_setbit ( num ) : pos = 1 for i in range ( INT_SIZE ) : if not ( num & ( 1 << i ) ) : pos += 1 else : break return pos </Code>
python-cpp-CONVERTING_DECIMAL_NUMBER_LYING_BETWEEN_1_TO_3999_TO_ROMAN_NUMERALS
python-cpp
def printRoman ( number ) : num = [ 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 ] sym = [ "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" ] i = 12 while number : div = number // num [ i ] number %= num [ i ] while div : ...
int printRoman ( int number ) { int num [ ] = { 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 } ; string sym [ ] = { "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" } ; int i = 12 ; while ( number > 0 ) { int div = number...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printRoman ( number ) : num = [ 1 , 4 , 5 , 9 , 10 , 40 , 50 , 90 , 100 , 400 , 500 , 900 , 1000 ] sym = [ "I" , "IV" , "V" , "IX" , "X" , "XL" , "L" , "XC" , "C" , "CD" , "D" , "CM" , "M" ] i = 12 while number : div = number // num [ i ] number %= num [ i ] while div ...
python-cpp-DYNAMIC_PROGRAMMING_SUBSET_SUM_PROBLEM
python-cpp
def isSubsetSum ( set , n , sum ) : if ( sum == 0 ) : return True if ( n == 0 and sum != 0 ) : return False if ( set [ n - 1 ] > sum ) : return isSubsetSum ( set , n - 1 , sum ) return isSubsetSum ( set , n - 1 , sum ) or isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] )
bool isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isSubsetSum ( set , n , sum ) : if ( sum == 0 ) : return True if ( n == 0 and sum != 0 ) : return False if ( set [ n - 1 ] > sum ) : return isSubsetSum ( set , n - 1 , sum ) return isSubsetSum ( set , n - 1 , sum ) or isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ...
python-cpp-NUMBER_DIGITS_PRODUCT_TWO_NUMBERS_1
python-cpp
def countDigits ( a , b ) : if ( a == 0 or b == 0 ) : return 1 return math.floor ( math.log10 ( abs ( a ) ) + math.log10 ( abs ( b ) ) ) + 1
int countDigits ( int a , int b ) { if ( a == 0 || b == 0 ) return 1 ; return floor ( log10 ( abs ( a ) ) + log10 ( abs ( b ) ) ) + 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countDigits ( a , b ) : if ( a == 0 or b == 0 ) : return 1 return math.floor ( math.log10 ( abs ( a ) ) + math.log10 ( abs ( b ) ) ) + 1 </Code>
python-cpp-WAYS_TO_WRITE_N_AS_SUM_OF_TWO_OR_MORE_POSITIVE_INTEGERS
python-cpp
def CountWays ( n ) : table = [ 0 ] * ( n + 1 ) table [ 0 ] = 1 for i in range ( 1 , n ) : for j in range ( i , n + 1 ) : table [ j ] += table [ j - i ] return table [ n ]
int countWays ( int n ) { int table [ n + 1 ] ; memset ( table , 0 , sizeof ( table ) ) ; table [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) table [ j ] += table [ j - i ] ; return table [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def CountWays ( n ) : table = [ 0 ] * ( n + 1 ) table [ 0 ] = 1 for i in range ( 1 , n ) : for j in range ( i , n + 1 ) : table [ j ] += table [ j - i ] return table [ n ] </Code>
python-cpp-SORT_EVEN_NUMBERS_ASCENDING_ORDER_SORT_ODD_NUMBERS_DESCENDING_ORDER_1
python-cpp
def twoWaySort ( arr , n ) : for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1 arr.sort ( ) for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1
void twoWaySort ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ; sort ( arr , arr + n ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] & 1 ) arr [ i ] *= - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def twoWaySort ( arr , n ) : for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1 arr.sort ( ) for i in range ( 0 , n ) : if ( arr [ i ] & 1 ) : arr [ i ] *= - 1 </Code>
python-cpp-FINDING_POWER_PRIME_NUMBER_P_N_1
python-cpp
def PowerOFPINnfactorial ( n , p ) : ans = 0 temp = p while ( temp <= n ) : ans += n / temp temp = temp * p return int ( ans )
int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def PowerOFPINnfactorial ( n , p ) : ans = 0 temp = p while ( temp <= n ) : ans += n / temp temp = temp * p return int ( ans ) </Code>
python-cpp-FIND_A_SPECIFIC_PAIR_IN_MATRIX
python-cpp
def findMaxValue ( mat ) : maxValue = 0 for a in range ( N - 1 ) : for b in range ( N - 1 ) : for d in range ( a + 1 , N ) : for e in range ( b + 1 , N ) : if maxValue < int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) : maxValue = int ( m...
int findMaxValue ( int mat [ ] [ N ] ) { int maxValue = INT_MIN ; for ( int a = 0 ; a < N - 1 ; a ++ ) for ( int b = 0 ; b < N - 1 ; b ++ ) for ( int d = a + 1 ; d < N ; d ++ ) for ( int e = b + 1 ; e < N ; e ++ ) if ( maxValue < ( mat [ d ] [ e ] - mat [ a ] [ b ] ) ) maxValue = mat [ d ] [ e ] - m...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaxValue ( mat ) : maxValue = 0 for a in range ( N - 1 ) : for b in range ( N - 1 ) : for d in range ( a + 1 , N ) : for e in range ( b + 1 , N ) : if maxValue < int ( mat [ d ] [ e ] - mat [ a ] [ b ] ) : maxValue = ...
python-cpp-FIND_THREE_ELEMENT_FROM_DIFFERENT_THREE_ARRAYS_SUCH_THAT_THAT_A_B_C_K_1
python-cpp
def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) : s = set ( ) for i in range ( n1 ) : s.add ( a1 [ i ] ) for i in range ( n2 ) : for j in range ( n3 ) : if sum - a2 [ i ] - a3 [ j ] in s : return True return False
bool findTriplet ( int a1 [ ] , int a2 [ ] , int a3 [ ] , int n1 , int n2 , int n3 , int sum ) { unordered_set < int > s ; for ( int i = 0 ; i < n1 ; i ++ ) s . insert ( a1 [ i ] ) ; for ( int i = 0 ; i < n2 ; i ++ ) { for ( int j = 0 ; j < n3 ; j ++ ) { if ( s . find ( sum - a2 [ i ] - ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findTriplet ( a1 , a2 , a3 , n1 , n2 , n3 , sum ) : s = set ( ) for i in range ( n1 ) : s.add ( a1 [ i ] ) for i in range ( n2 ) : for j in range ( n3 ) : if sum - a2 [ i ] - a3 [ j ] in s : return True return False </Code>
python-cpp-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES
python-cpp
def getOddOccurrence ( arr , arr_size ) : for i in range ( 0 , arr_size ) : count = 0 for j in range ( 0 , arr_size ) : if arr [ i ] == arr [ j ] : count += 1 if ( count % 2 != 0 ) : return arr [ i ] return - 1
int getOddOccurrence ( int arr [ ] , int arr_size ) { for ( int i = 0 ; i < arr_size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < arr_size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) count ++ ; } if ( count % 2 != 0 ) return arr [ i ] ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getOddOccurrence ( arr , arr_size ) : for i in range ( 0 , arr_size ) : count = 0 for j in range ( 0 , arr_size ) : if arr [ i ] == arr [ j ] : count += 1 if ( count % 2 != 0 ) : return arr [ i ] return - 1 </Code>
python-cpp-PRIME_FACTORS_BIG_NUMBER
python-cpp
def factorize ( n ) : count = 0 while ( ( n % 2 > 0 ) == False ) : n >>= 1 count += 1 if ( count > 0 ) : print ( 2 , count ) for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 ) : count = 0 while ( n % i == 0 ) : count += 1 n = int ( n / i ) ...
void factorize ( long long n ) { int count = 0 ; while ( ! ( n % 2 ) ) { n >>= 1 ; count ++ ; } if ( count ) cout << 2 << " " << count << endl ; for ( long long i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n = n / i ; } if ( count ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def factorize ( n ) : count = 0 while ( ( n % 2 > 0 ) == False ) : n >>= 1 count += 1 if ( count > 0 ) : print ( 2 , count ) for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 ) : count = 0 while ( n % i == 0 ) : count += 1 n = int ( n...
python-cpp-SMALLEST_DIFFERENCE_PAIR_VALUES_TWO_UNSORTED_ARRAYS
python-cpp
def findSmallestDifference ( A , B , m , n ) : A.sort ( ) B.sort ( ) a = 0 b = 0 result = sys.maxsize while ( a < m and b < n ) : if ( abs ( A [ a ] - B [ b ] ) < result ) : result = abs ( A [ a ] - B [ b ] ) if ( A [ a ] < B [ b ] ) : a += 1 else ...
int findSmallestDifference ( int A [ ] , int B [ ] , int m , int n ) { sort ( A , A + m ) ; sort ( B , B + n ) ; int a = 0 , b = 0 ; int result = INT_MAX ; while ( a < m && b < n ) { if ( abs ( A [ a ] - B [ b ] ) < result ) result = abs ( A [ a ] - B [ b ] ) ; if ( A [ a ] < B [ b ] ) a ++ ; else...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSmallestDifference ( A , B , m , n ) : A.sort ( ) B.sort ( ) a = 0 b = 0 result = sys.maxsize while ( a < m and b < n ) : if ( abs ( A [ a ] - B [ b ] ) < result ) : result = abs ( A [ a ] - B [ b ] ) if ( A [ a ] < B [ b ] ) : a += 1 ...
python-cpp-CHECK_GIVEN_STRING_CAN_SPLIT_FOUR_DISTINCT_STRINGS
python-cpp
def check ( s ) : if ( len ( s ) >= 10 ) : return True for i in range ( 1 , len ( s ) ) : for j in range ( i + 1 , len ( s ) ) : for k in range ( j + 1 , len ( s ) ) : s1 = s [ 0 : i ] s2 = s [ i : j - i ] s3 = s [ j : k - j ] ...
bool check ( string s ) { if ( s . size ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . size ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . size ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . size ( ) ; k ++ ) { string s1 = s . substr ( 0 , i ) ; string s2 = s . sub...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def check ( s ) : if ( len ( s ) >= 10 ) : return True for i in range ( 1 , len ( s ) ) : for j in range ( i + 1 , len ( s ) ) : for k in range ( j + 1 , len ( s ) ) : s1 = s [ 0 : i ] s2 = s [ i : j - i ] s3 = s [ j : k - j ] ...
python-cpp-CHECK_POSSIBLE_PATH_2D_MATRIX
python-cpp
def isPath ( arr ) : arr [ 0 ] [ 0 ] = 1 for i in range ( 1 , row ) : if ( arr [ i ] [ 0 ] != - 1 ) : arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] for j in range ( 1 , col ) : if ( arr [ 0 ] [ j ] != - 1 ) : arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] for i in range ( 1 , row ...
bool isPath ( int arr [ row ] [ col ] ) { arr [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < row ; i ++ ) if ( arr [ i ] [ 0 ] != - 1 ) arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] ; for ( int j = 1 ; j < col ; j ++ ) if ( arr [ 0 ] [ j ] != - 1 ) arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] ; for ( int i = 1 ; i < row ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPath ( arr ) : arr [ 0 ] [ 0 ] = 1 for i in range ( 1 , row ) : if ( arr [ i ] [ 0 ] != - 1 ) : arr [ i ] [ 0 ] = arr [ i - 1 ] [ 0 ] for j in range ( 1 , col ) : if ( arr [ 0 ] [ j ] != - 1 ) : arr [ 0 ] [ j ] = arr [ 0 ] [ j - 1 ] for i in range ( 1...
python-cpp-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER_1
python-cpp
def bitonicGenerator ( arr , n ) : i = 1 j = n - 1 if ( j % 2 != 0 ) : j = j - 1 while ( i < j ) : arr [ j ] , arr [ i ] = arr [ i ] , arr [ j ] i = i + 2 j = j - 2 arr_f = [ ] arr_s = [ ] for i in range ( int ( ( n + 1 ) / 2 ) ) : arr_f.append ( arr [...
void bitonicGenerator ( int arr [ ] , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { swap ( arr [ i ] , arr [ j ] ) ; i += 2 ; j -= 2 ; } sort ( arr , arr + ( n + 1 ) / 2 ) ; sort ( arr + ( n + 1 ) / 2 , arr + n , greater < int > ( ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def bitonicGenerator ( arr , n ) : i = 1 j = n - 1 if ( j % 2 != 0 ) : j = j - 1 while ( i < j ) : arr [ j ] , arr [ i ] = arr [ i ] , arr [ j ] i = i + 2 j = j - 2 arr_f = [ ] arr_s = [ ] for i in range ( int ( ( n + 1 ) / 2 ) ) : arr_f.append ...
python-cpp-REMOVE_ARRAY_END_ELEMENT_MAXIMIZE_SUM_PRODUCT
python-cpp
def solve ( dp , a , low , high , turn ) : if ( low == high ) : return a [ low ] * turn if ( dp [ low ] [ high ] != 0 ) : return dp [ low ] [ high ] dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low , high -...
int solve ( int dp [ ] [ MAX ] , int a [ ] , int low , int high , int turn ) { if ( low == high ) return a [ low ] * turn ; if ( dp [ low ] [ high ] != 0 ) return dp [ low ] [ high ] ; dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solve ( dp , a , low , high , turn ) : if ( low == high ) : return a [ low ] * turn if ( dp [ low ] [ high ] != 0 ) : return dp [ low ] [ high ] dp [ low ] [ high ] = max ( a [ low ] * turn + solve ( dp , a , low + 1 , high , turn + 1 ) , a [ high ] * turn + solve ( dp , a , low ,...
python-cpp-DIRECTION_LAST_SQUARE_BLOCK
python-cpp
def direction ( R , C ) : if ( R != C and R % 2 == 0 and C % 2 != 0 and R < C ) : print ( "Left" ) return if ( R != C and R % 2 == 0 and C % 2 == 0 and R > C ) : print ( "Up" ) return if R == C and R % 2 != 0 and C % 2 != 0 : print ( "Right" ) return if R ...
void direction ( ll R , ll C ) { if ( R != C && R % 2 == 0 && C % 2 != 0 && R < C ) { cout << "Left" << endl ; return ; } if ( R != C && R % 2 != 0 && C % 2 == 0 && R > C ) { cout << "Up" << endl ; return ; } if ( R == C && R % 2 != 0 && C % 2 != 0 ) { cout << "Right" << endl ; return ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def direction ( R , C ) : if ( R != C and R % 2 == 0 and C % 2 != 0 and R < C ) : print ( "Left" ) return if ( R != C and R % 2 == 0 and C % 2 == 0 and R > C ) : print ( "Up" ) return if R == C and R % 2 != 0 and C % 2 != 0 : print ( "Right" ) return ...
python-cpp-SPLIT_NUMERIC_ALPHABETIC_AND_SPECIAL_SYMBOLS_FROM_A_STRING
python-cpp
def splitString ( str ) : alpha = "" num = "" special = "" for i in range ( len ( str ) ) : if ( str [ i ].isdigit ( ) ) : num = num + str [ i ] elif ( ( str [ i ] >= 'A' and str [ i ] <= 'Z' ) or ( str [ i ] >= 'a' and str [ i ] <= 'z' ) ) : alpha += str [ i ] ...
void splitString ( string str ) { string alpha , num , special ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( isdigit ( str [ i ] ) ) num . push_back ( str [ i ] ) ; else if ( ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) || ( str [ i ] >= 'a' && str [ i ] <= 'z' ) ) alpha . push_back ( str [ i ] )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def splitString ( str ) : alpha = "" num = "" special = "" for i in range ( len ( str ) ) : if ( str [ i ].isdigit ( ) ) : num = num + str [ i ] elif ( ( str [ i ] >= 'A' and str [ i ] <= 'Z' ) or ( str [ i ] >= 'a' and str [ i ] <= 'z' ) ) : alpha += str [...
python-cpp-FIND_MEDIAN_ROW_WISE_SORTED_MATRIX
python-cpp
def binaryMedian ( m , r , d ) : mi = m [ 0 ] [ 0 ] mx = 0 for i in range ( r ) : if m [ i ] [ 0 ] < mi : mi = m [ i ] [ 0 ] if m [ i ] [ d - 1 ] > mx : mx = m [ i ] [ d - 1 ] desired = ( r * d + 1 ) // 2 while ( mi < mx ) : mid = mi + ( mx - mi ) // 2...
int binaryMedian ( int m [ ] [ MAX ] , int r , int c ) { int min = INT_MAX , max = INT_MIN ; for ( int i = 0 ; i < r ; i ++ ) { if ( m [ i ] [ 0 ] < min ) min = m [ i ] [ 0 ] ; if ( m [ i ] [ c - 1 ] > max ) max = m [ i ] [ c - 1 ] ; } int desired = ( r * c + 1 ) / 2 ; while ( min < max ) { in...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binaryMedian ( m , r , d ) : mi = m [ 0 ] [ 0 ] mx = 0 for i in range ( r ) : if m [ i ] [ 0 ] < mi : mi = m [ i ] [ 0 ] if m [ i ] [ d - 1 ] > mx : mx = m [ i ] [ d - 1 ] desired = ( r * d + 1 ) // 2 while ( mi < mx ) : mid = mi + ( mx - mi...
python-cpp-FIND_DUPLICATES_GIVEN_ARRAY_ELEMENTS_NOT_LIMITED_RANGE
python-cpp
def printDuplicates ( arr ) : dict = { } for ele in arr : try : dict [ ele ] += 1 except : dict [ ele ] = 1 for item in dict : if ( dict [ item ] > 1 ) : print ( item , end = " " ) print ( "\n" )
void printDuplicates ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; bool dup = false ; unordered_map < int , int > :: iterator itr ; for ( itr = freq . begin ( ) ; itr != freq . end ( ) ; itr ++ ) { if ( itr -> second > 1 ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printDuplicates ( arr ) : dict = { } for ele in arr : try : dict [ ele ] += 1 except : dict [ ele ] = 1 for item in dict : if ( dict [ item ] > 1 ) : print ( item , end = " " ) print ( "\n" ) </Code>
python-cpp-PRINT_FIBONACCI_SEQUENCE_USING_2_VARIABLES
python-cpp
def fib ( n ) : a = 0 b = 1 if ( n >= 0 ) : print ( a , end = ' ' ) if ( n >= 1 ) : print ( b , end = ' ' ) for i in range ( 2 , n + 1 ) : c = a + b print ( c , end = ' ' ) a = b b = c
void fib ( int n ) { int a = 0 , b = 1 , c ; if ( n >= 0 ) cout << a << " " ; if ( n >= 1 ) cout << b << " " ; for ( int i = 2 ; i <= n ; i ++ ) { c = a + b ; cout << c << " " ; a = b ; b = c ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def fib ( n ) : a = 0 b = 1 if ( n >= 0 ) : print ( a , end = ' ' ) if ( n >= 1 ) : print ( b , end = ' ' ) for i in range ( 2 , n + 1 ) : c = a + b print ( c , end = ' ' ) a = b b = c </Code>
python-cpp-SWAP_TWO_NIBBLES_BYTE
python-cpp
def swapNibbles ( x ) : return ( ( x & 0x0F ) << 4
int swapNibbles ( int x ) { return ( ( x & 0x0F ) << 4
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def swapNibbles ( x ) : return ( ( x & 0x0F ) << 4 </Code>
python-cpp-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING_1
python-cpp
def countPS ( i , j ) : if ( i >= n or j < 0 ) : return 0 if ( dp [ i ] [ j ] != - 1 ) : return dp [ i ] [ j ] if ( abs ( i - j ) == 1 ) : if ( str [ i ] == str [ j ] ) : dp [ i ] [ j ] = 3 return dp [ i ] [ j ] else : dp [ i ] [ j ] = 2 ...
int countPS ( int i , int j ) { if ( i >= n || j < 0 ) return 0 ; if ( dp [ i ] [ j ] != - 1 ) return dp [ i ] [ j ] ; if ( abs ( i - j ) == 1 ) { if ( str [ i ] == str [ j ] ) return dp [ i ] [ j ] = 3 ; else return dp [ i ] [ j ] = 2 ; } if ( i == j ) return dp [ 1 ] [ j ] = 1 ; else if ( str [ i ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPS ( i , j ) : if ( i >= n or j < 0 ) : return 0 if ( dp [ i ] [ j ] != - 1 ) : return dp [ i ] [ j ] if ( abs ( i - j ) == 1 ) : if ( str [ i ] == str [ j ] ) : dp [ i ] [ j ] = 3 return dp [ i ] [ j ] else : dp [ i ] [ j ]...
python-cpp-SUM_DIVISORS_1_N_1
python-cpp
def divisorSum ( n ) : sum = 0 for i in range ( 1 , n + 1 ) : sum += int ( n / i ) * i return int ( sum )
int divisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) sum += ( n / i ) * i ; return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def divisorSum ( n ) : sum = 0 for i in range ( 1 , n + 1 ) : sum += int ( n / i ) * i return int ( sum ) </Code>
python-cpp-ADD_1_TO_A_GIVEN_NUMBER
python-cpp
def addOne ( x ) : m = 1 while ( x & m ) : x = x ^ m m <<= 1 x = x ^ m return x
int addOne ( int x ) { int m = 1 ; while ( x & m ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def addOne ( x ) : m = 1 while ( x & m ) : x = x ^ m m <<= 1 x = x ^ m return x </Code>
python-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_9_NOT
python-cpp
def check ( st ) : n = len ( st ) digitSum = 0 for i in range ( 0 , n ) : digitSum = digitSum + ( int ) ( st [ i ] ) return ( digitSum % 9 == 0 )
int check ( string str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str [ i ] - '0' ) ; return ( digitSum % 9 == 0 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def check ( st ) : n = len ( st ) digitSum = 0 for i in range ( 0 , n ) : digitSum = digitSum + ( int ) ( st [ i ] ) return ( digitSum % 9 == 0 ) </Code>
python-cpp-FIND_PAIR_MAXIMUM_GCD_ARRAY
python-cpp
def findMaxGCD ( arr , n ) : high = 0 i = 0 while i < n : high = max ( high , arr [ i ] ) i = i + 1 divisors = [ 0 ] * ( high + 1 ) i = 0 while i < n : j = 1 while j <= math.sqrt ( arr [ i ] ) : if ( arr [ i ] % j == 0 ) : divisors [ j ...
int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = max ( high , arr [ i ] ) ; int divisors [ high + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 1 ; j <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaxGCD ( arr , n ) : high = 0 i = 0 while i < n : high = max ( high , arr [ i ] ) i = i + 1 divisors = [ 0 ] * ( high + 1 ) i = 0 while i < n : j = 1 while j <= math.sqrt ( arr [ i ] ) : if ( arr [ i ] % j == 0 ) : diviso...
python-cpp-CONVERT_STRICTLY_INCREASING_ARRAY_MINIMUM_CHANGES
python-cpp
def minRemove ( arr , n ) : LIS = [ 0 for i in range ( n ) ] len = 0 for i in range ( n ) : LIS [ i ] = 1 for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) : LIS [ i ] = max ( LIS [ i ] , ...
int minRemove ( int arr [ ] , int n ) { int LIS [ n ] , len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) { LIS [ i ] = max ( LIS ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minRemove ( arr , n ) : LIS = [ 0 for i in range ( n ) ] len = 0 for i in range ( n ) : LIS [ i ] = 1 for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) : LIS [ i ] = max ( LIS [...
python-cpp-SEARCHING_FOR_PATTERNS_SET_2_KMP_ALGORITHM
python-cpp
def computeLPSArray ( pat , M , lps ) : len = 0 lps [ 0 ] i = 1 while i < M : if pat [ i ] == pat [ len ] : len += 1 lps [ i ] = len i += 1 else : if len != 0 : len = lps [ len - 1 ] else : lps [ ...
void computeLPSArray ( char * pat , int M , int * lps ) { int len = 0 ; lps [ 0 ] = 0 ; int i = 1 ; while ( i < M ) { if ( pat [ i ] == pat [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } else { lp...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def computeLPSArray ( pat , M , lps ) : len = 0 lps [ 0 ] i = 1 while i < M : if pat [ i ] == pat [ len ] : len += 1 lps [ i ] = len i += 1 else : if len != 0 : len = lps [ len - 1 ] else : ...
python-cpp-COUNT_MINIMUM_NUMBER_SUBSETS_SUBSEQUENCES_CONSECUTIVE_NUMBERS
python-cpp
def numofsubset ( arr , n ) : x = sorted ( arr ) count = 1 for i in range ( 0 , n - 1 ) : if ( x [ i ] + 1 != x [ i + 1 ] ) : count = count + 1 return count
int numofsubset ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def numofsubset ( arr , n ) : x = sorted ( arr ) count = 1 for i in range ( 0 , n - 1 ) : if ( x [ i ] + 1 != x [ i + 1 ] ) : count = count + 1 return count </Code>
python-cpp-STOOGE_SORT
python-cpp
def stoogesort ( arr , l , h ) : if l >= h : return if arr [ l ] > arr [ h ] : t = arr [ l ] arr [ l ] = arr [ h ] arr [ h ] = t if h - l + 1 > 2 : t = ( int ) ( ( h - l + 1 ) / 3 ) stoogesort ( arr , l , ( h - t ) ) stoogesort ( arr , l + t , ( h ) ) ...
void stoogesort ( int arr [ ] , int l , int h ) { if ( l >= h ) return ; if ( arr [ l ] > arr [ h ] ) swap ( arr [ l ] , arr [ h ] ) ; if ( h - l + 1 > 2 ) { int t = ( h - l + 1 ) / 3 ; stoogesort ( arr , l , h - t ) ; stoogesort ( arr , l + t , h ) ; stoogesort ( arr , l , h - t ) ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def stoogesort ( arr , l , h ) : if l >= h : return if arr [ l ] > arr [ h ] : t = arr [ l ] arr [ l ] = arr [ h ] arr [ h ] = t if h - l + 1 > 2 : t = ( int ) ( ( h - l + 1 ) / 3 ) stoogesort ( arr , l , ( h - t ) ) stoogesort ( arr , l + t , (...
python-cpp-FIND_ARRANGEMENT_QUEUE_GIVEN_TIME
python-cpp
def solve ( n , t , p ) : s = list ( p ) for i in range ( 0 , t ) : for j in range ( 0 , n - 1 ) : if ( s [ j ] == 'B' and s [ j + 1 ] == 'G' ) : temp = s [ j ] s [ j ] = s [ j + 1 ] s [ j + 1 ] = temp j = j + 1 print ( ''.j...
void solve ( int n , int t , string s ) { for ( int i = 0 ; i < t ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) if ( s [ j ] == 'B' && s [ j + 1 ] == 'G' ) { char temp = s [ j ] ; s [ j ] = s [ j + 1 ] ; s [ j + 1 ] = temp ; j ++ ; } cout << s ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def solve ( n , t , p ) : s = list ( p ) for i in range ( 0 , t ) : for j in range ( 0 , n - 1 ) : if ( s [ j ] == 'B' and s [ j + 1 ] == 'G' ) : temp = s [ j ] s [ j ] = s [ j + 1 ] s [ j + 1 ] = temp j = j + 1 print...
python-cpp-HOW_TO_TURN_OFF_A_PARTICULAR_BIT_IN_A_NUMBER
python-cpp
def turnOffK ( n , k ) : if ( k <= 0 ) : return n return ( n & ~ ( 1 << ( k - 1 ) ) )
int turnOffK ( int n , int k ) { if ( k <= 0 ) return n ; return ( n & ~ ( 1 << ( k - 1 ) ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def turnOffK ( n , k ) : if ( k <= 0 ) : return n return ( n & ~ ( 1 << ( k - 1 ) ) ) </Code>
python-cpp-FINDING_K_MODULUS_ARRAY_ELEMENT
python-cpp
def printEqualModNumbers ( arr , n ) : arr.sort ( ) d = arr [ n - 1 ] - arr [ 0 ] v = [ ] i = 1 while ( i * i <= d ) : if ( d % i == 0 ) : v.append ( i ) if ( i != d / i ) : v.append ( d / i ) i += 1 for i in range ( len ( v ) ) : t...
void printEqualModNumbers ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int d = arr [ n - 1 ] - arr [ 0 ] ; vector < int > v ; for ( int i = 1 ; i * i <= d ; i ++ ) { if ( d % i == 0 ) { v . push_back ( i ) ; if ( i != d / i ) v . push_back ( d / i ) ; } } for ( int i = 0 ; i ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printEqualModNumbers ( arr , n ) : arr.sort ( ) d = arr [ n - 1 ] - arr [ 0 ] v = [ ] i = 1 while ( i * i <= d ) : if ( d % i == 0 ) : v.append ( i ) if ( i != d / i ) : v.append ( d / i ) i += 1 for i in range ( len ( v ) ) : ...
python-cpp-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING_1
python-cpp
def findMaximum ( arr , low , high ) : if low == high : return arr [ low ] if high == low + 1 and arr [ low ] >= arr [ high ] : return arr [ low ] if high == low + 1 and arr [ low ] < arr [ high ] : return arr [ high ] mid = ( low + high ) // 2 if arr [ mid ] > arr [ mid + 1 ...
int findMaximum ( int arr [ ] , int low , int high ) { if ( low == high ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] >= arr [ high ] ) return arr [ low ] ; if ( ( high == low + 1 ) && arr [ low ] < arr [ high ] ) return arr [ high ] ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] > arr [ m...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaximum ( arr , low , high ) : if low == high : return arr [ low ] if high == low + 1 and arr [ low ] >= arr [ high ] : return arr [ low ] if high == low + 1 and arr [ low ] < arr [ high ] : return arr [ high ] mid = ( low + high ) // 2 if arr [ mid ] > arr [ m...
python-cpp-SMALLEST_LENGTH_STRING_WITH_REPEATED_REPLACEMENT_OF_TWO_DISTINCT_ADJACENT
python-cpp
def stringReduction ( str ) : n = len ( str ) count = [ 0 ] * 3 for i in range ( n ) : count [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) : return n if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % 2 ) == ...
int stringReduction ( string str ) { int n = str . length ( ) ; int count [ 3 ] = { 0 } ; for ( int i = 0 ; i < n ; ++ i ) count [ str [ i ] - 'a' ] ++ ; if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) return n ; if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def stringReduction ( str ) : n = len ( str ) count = [ 0 ] * 3 for i in range ( n ) : count [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) : return n if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % ...
python-cpp-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM_1
python-cpp
def KnapSack ( val , wt , n , W ) : dp = [ 0 ] * ( W + 1 ) for i in range ( n ) : for j in range ( W , wt [ i ] , - 1 ) : dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) return dp [ W ]
int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int dp [ W + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def KnapSack ( val , wt , n , W ) : dp = [ 0 ] * ( W + 1 ) for i in range ( n ) : for j in range ( W , wt [ i ] , - 1 ) : dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) return dp [ W ] </Code>
python-cpp-SUM_PAIRWISE_PRODUCTS_1
python-cpp
def findSum ( n ) : multiTerms = n * ( n + 1 ) // 2 sm = multiTerms for i in range ( 2 , n + 1 ) : multiTerms = multiTerms - ( i - 1 ) sm = sm + multiTerms * i return sm
long long int findSum ( int n ) { long long int multiTerms = n * ( n + 1 ) / 2 ; long long int sum = multiTerms ; for ( int i = 2 ; i <= n ; i ++ ) { multiTerms = multiTerms - ( i - 1 ) ; sum = sum + multiTerms * i ; } return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSum ( n ) : multiTerms = n * ( n + 1 ) // 2 sm = multiTerms for i in range ( 2 , n + 1 ) : multiTerms = multiTerms - ( i - 1 ) sm = sm + multiTerms * i return sm </Code>
python-cpp-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE
python-cpp
def recursiveReverse ( str ) : stack = [ ] for i in range ( len ( str ) ) : stack.append ( str [ i ] ) for i in range ( len ( str ) ) : str [ i ] = stack.pop ( )
void recursiveReverse ( string & str ) { stack < char > st ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) st . push ( str [ i ] ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { str [ i ] = st . top ( ) ; st . pop ( ) ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def recursiveReverse ( str ) : stack = [ ] for i in range ( len ( str ) ) : stack.append ( str [ i ] ) for i in range ( len ( str ) ) : str [ i ] = stack.pop ( ) </Code>
python-cpp-FIND_SUBARRAY_WITH_GIVEN_SUM
python-cpp
def subArraySum ( arr , n , sum ) : for i in range ( n ) : curr_sum = arr [ i ] j = i + 1 while j <= n : if curr_sum == sum : print ( "Sum found between" ) print ( "indexes %d and %d" % ( i , j - 1 ) ) return 1 if curr_s...
int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum , i , j ; for ( i = 0 ; i < n ; i ++ ) { curr_sum = arr [ i ] ; for ( j = i + 1 ; j <= n ; j ++ ) { if ( curr_sum == sum ) { cout << "Sum found between indexes " << i << " and " << j - 1 ; return 1 ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def subArraySum ( arr , n , sum ) : for i in range ( n ) : curr_sum = arr [ i ] j = i + 1 while j <= n : if curr_sum == sum : print ( "Sum found between" ) print ( "indexes %d and %d" % ( i , j - 1 ) ) return 1 if...
python-cpp-FIND_THE_ELEMENT_THAT_APPEARS_ONCE_1
python-cpp
def getSingle ( arr , n ) : result = 0 for i in range ( 0 , INT_SIZE ) : sm = 0 x = ( 1 << i ) for j in range ( 0 , n ) : if ( arr [ j ] & x ) : sm = sm + 1 if ( sm % 3 ) : result = result
int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] & x ) sum ++ ; } if ( sum % 3 ) result |= x ; } return result ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getSingle ( arr , n ) : result = 0 for i in range ( 0 , INT_SIZE ) : sm = 0 x = ( 1 << i ) for j in range ( 0 , n ) : if ( arr [ j ] & x ) : sm = sm + 1 if ( sm % 3 ) : result = result </Code>
python-cpp-COMPUTE_MODULUS_DIVISION_BY_A_POWER_OF_2_NUMBER
python-cpp
def getModulo ( n , d ) : return ( n & ( d - 1 ) )
unsigned int getModulo ( unsigned int n , unsigned int d ) { return ( n & ( d - 1 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getModulo ( n , d ) : return ( n & ( d - 1 ) ) </Code>
python-cpp-QUICK_WAY_CHECK_CHARACTERS_STRING
python-cpp
def allCharactersSame ( s ) : n = len ( s ) for i in range ( 1 , n ) : if s [ i ] != s [ 0 ] : return False return True
bool allCharactersSame ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s [ i ] != s [ 0 ] ) return false ; return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def allCharactersSame ( s ) : n = len ( s ) for i in range ( 1 , n ) : if s [ i ] != s [ 0 ] : return False return True </Code>
python-cpp-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX
python-cpp
def maxDecimalValue ( mat , i , j , p ) : if i >= N or j >= N : return 0 result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) if mat [ i ] [ j ] == 1 : return pow ( 2 , p ) + result else : return result
long long int maxDecimalValue ( int mat [ ] [ N ] , int i , int j , int p ) { if ( i >= N || j >= N ) return 0 ; int result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) return pow ( 2 , p ) + result ; else return result ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxDecimalValue ( mat , i , j , p ) : if i >= N or j >= N : return 0 result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) if mat [ i ] [ j ] == 1 : return pow ( 2 , p ) + result else : return result </Code>
python-cpp-COUNT_BALANCED_BINARY_TREES_HEIGHT_H
python-cpp
def countBT ( h ) : MOD = 1000000007 dp = [ 0 for i in range ( h + 1 ) ] dp [ 0 ] = 1 dp [ 1 ] = 1 for i in range ( 2 , h + 1 ) : dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD return dp [ h ]
long long int countBT ( int h ) { long long int dp [ h + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % mod + dp [ i - 1 ] ) % mod ) % mod ; } return dp [ h ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countBT ( h ) : MOD = 1000000007 dp = [ 0 for i in range ( h + 1 ) ] dp [ 0 ] = 1 dp [ 1 ] = 1 for i in range ( 2 , h + 1 ) : dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD return dp [ h ] </Code>
python-cpp-COUNTING_INVERSIONS
python-cpp
def getInvCount ( arr , n ) : inv_count = 0 for i in range ( n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : inv_count += 1 return inv_count
int getInvCount ( int arr [ ] , int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getInvCount ( arr , n ) : inv_count = 0 for i in range ( n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : inv_count += 1 return inv_count </Code>
python-cpp-COORDINATES_RECTANGLE_GIVEN_POINTS_LIE_INSIDE
python-cpp
def printRect ( X , Y , n ) : Xmax = max ( X ) Xmin = min ( X ) Ymax = max ( Y ) Ymin = min ( Y ) print ( "{" , Xmin , ", " , Ymin , "}" , sep = "" ) print ( "{" , Xmin , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymin ,...
void printRect ( int X [ ] , int Y [ ] , int n ) { int Xmax = * max_element ( X , X + n ) ; int Xmin = * min_element ( X , X + n ) ; int Ymax = * max_element ( Y , Y + n ) ; int Ymin = * min_element ( Y , Y + n ) ; cout << "{" << Xmin << ", " << Ymin << "}" << endl ; cout << "{" << Xmin << ", " << Ymax << "...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printRect ( X , Y , n ) : Xmax = max ( X ) Xmin = min ( X ) Ymax = max ( Y ) Ymin = min ( Y ) print ( "{" , Xmin , ", " , Ymin , "}" , sep = "" ) print ( "{" , Xmin , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " ,...
python-cpp-DELANNOY_NUMBER_1
python-cpp
def dealnnoy ( n , m ) : dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ] for i in range ( m ) : dp [ 0 ] [ i ] = 1 for i in range ( 1 , m + 1 ) : dp [ i ] [ 0 ] = 1 for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : dp [ i ] [ j ] = dp [ i ...
int dealnnoy ( int n , int m ) { int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i <= m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 1 ] [ j -...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def dealnnoy ( n , m ) : dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ] for i in range ( m ) : dp [ 0 ] [ i ] = 1 for i in range ( 1 , m + 1 ) : dp [ i ] [ 0 ] = 1 for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : dp [ i ] [ j ] = ...
python-cpp-MODULAR_EXPONENTIATION_POWER_IN_MODULAR_ARITHMETIC
python-cpp
def power ( x , y , p ) : res = 1 x = x % p while ( y > 0 ) : if ( ( y & 1 ) == 1 ) : res = ( res * x ) % p y = y >> 1 x = ( x * x ) % p return res
int power ( int x , unsigned int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( y & 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def power ( x , y , p ) : res = 1 x = x % p while ( y > 0 ) : if ( ( y & 1 ) == 1 ) : res = ( res * x ) % p y = y >> 1 x = ( x * x ) % p return res </Code>
python-cpp-PROGRAM_CHECK_ISBN
python-cpp
def isValidISBN ( isbn ) : if len ( isbn ) != 10 : return False _sum = 0 for i in range ( 9 ) : if 0 <= int ( isbn [ i ] ) <= 9 : _sum += int ( isbn [ i ] ) * ( 10 - i ) else : return False if ( isbn [ 9 ] != 'X' and 0 <= int ( isbn [ 9 ] ) <= 9 ) : ...
bool isValidISBN ( string & isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn [ i ] - '0' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn [ 9 ] ; if ( last !...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isValidISBN ( isbn ) : if len ( isbn ) != 10 : return False _sum = 0 for i in range ( 9 ) : if 0 <= int ( isbn [ i ] ) <= 9 : _sum += int ( isbn [ i ] ) * ( 10 - i ) else : return False if ( isbn [ 9 ] != 'X' and 0 <= int ( isbn [ 9 ] ) <= 9 ) :...
python-cpp-NEXT_GREATER_FREQUENCY_ELEMENT
python-cpp
def NFG ( a , n ) : if ( n <= 0 ) : print ( "List empty" ) return [ ] stack = [ 0 ] * n freq = { } for i in a : freq [ a [ i ] ] = 0 for i in a : freq [ a [ i ] ] += 1 res = [ 0 ] * n top = - 1 top += 1 stack [ top ] = 0 for i in range ( 1 , n ) : ...
void NFG ( int a [ ] , int n , int freq [ ] ) { stack < int > s ; s . push ( 0 ) ; int res [ n ] = { 0 } ; for ( int i = 1 ; i < n ; i ++ ) { if ( freq [ a [ s . top ( ) ] ] > freq [ a [ i ] ] ) s . push ( i ) ; else { while ( freq [ a [ s . top ( ) ] ] < freq [ a [ i ] ] &...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def NFG ( a , n ) : if ( n <= 0 ) : print ( "List empty" ) return [ ] stack = [ 0 ] * n freq = { } for i in a : freq [ a [ i ] ] = 0 for i in a : freq [ a [ i ] ] += 1 res = [ 0 ] * n top = - 1 top += 1 stack [ top ] = 0 for i in range ( 1 ,...
python-cpp-SWAP_BITS_IN_A_GIVEN_NUMBER
python-cpp
def swapBits ( x , p1 , p2 , n ) : set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) xor = ( set1 ^ set2 ) xor = ( xor << p1 )
int swapBits ( unsigned int x , unsigned int p1 , unsigned int p2 , unsigned int n ) { unsigned int set1 = ( x >> p1 ) & ( ( 1U << n ) - 1 ) ; unsigned int set2 = ( x >> p2 ) & ( ( 1U << n ) - 1 ) ; unsigned int Xor = ( set1 ^ set2 ) ; Xor = ( Xor << p1 )
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def swapBits ( x , p1 , p2 , n ) : set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) xor = ( set1 ^ set2 ) xor = ( xor << p1 ) </Code>
python-cpp-PRINT_EQUAL_SUM_SETS_ARRAY_PARTITION_PROBLEM_SET_2
python-cpp
def printEqualSumSets ( arr , n ) : sum_array = sum ( arr ) if ( sum_array & 1 ) : print ( "-1" ) return k = sum_array >> 1 dp = np.zeros ( ( n + 1 , k + 1 ) ) for i in range ( 1 , k + 1 ) : dp [ 0 ] [ i ] = False for i in range ( n + 1 ) : dp [ i ] [ 0 ] = True ...
void printEqualSumSets ( int arr [ ] , int n ) { int i , currSum ; int sum = accumulate ( arr , arr + n , 0 ) ; if ( sum & 1 ) { cout << "-1" ; return ; } int k = sum >> 1 ; bool dp [ n + 1 ] [ k + 1 ] ; for ( i = 1 ; i <= k ; i ++ ) dp [ 0 ] [ i ] = false ; for ( i = 0 ; i <= n ; i ++ )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printEqualSumSets ( arr , n ) : sum_array = sum ( arr ) if ( sum_array & 1 ) : print ( "-1" ) return k = sum_array >> 1 dp = np.zeros ( ( n + 1 , k + 1 ) ) for i in range ( 1 , k + 1 ) : dp [ 0 ] [ i ] = False for i in range ( n + 1 ) : dp [ i ] [ 0 ] =...
python-cpp-HYPERCUBE_GRAPH
python-cpp
def power ( n ) : if n == 1 : return 2 return 2 * power ( n - 1 )
int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def power ( n ) : if n == 1 : return 2 return 2 * power ( n - 1 ) </Code>
python-cpp-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS_2
python-cpp
def findElements ( arr , n ) : first = - sys.maxsize second = - sys.maxsize for i in range ( 0 , n ) : if ( arr [ i ] > first ) : second = first first = arr [ i ] elif ( arr [ i ] > second ) : second = arr [ i ] for i in range ( 0 , n ) : if ( ...
void findElements ( int arr [ ] , int n ) { int first = INT_MIN , second = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( ar...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findElements ( arr , n ) : first = - sys.maxsize second = - sys.maxsize for i in range ( 0 , n ) : if ( arr [ i ] > first ) : second = first first = arr [ i ] elif ( arr [ i ] > second ) : second = arr [ i ] for i in range ( 0 , n ) : ...
python-cpp-DISTRIBUTING_M_ITEMS_CIRCLE_SIZE_N_STARTING_K_TH_POSITION
python-cpp
def lastPosition ( n , m , k ) : if ( m <= n - k + 1 ) : return m + k - 1 m = m - ( n - k + 1 ) if ( m % n == 0 ) : return n else : return m % n
int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lastPosition ( n , m , k ) : if ( m <= n - k + 1 ) : return m + k - 1 m = m - ( n - k + 1 ) if ( m % n == 0 ) : return n else : return m % n </Code>
python-cpp-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_1
python-cpp
def reverseStr ( str ) : n = len ( str ) i , j = 0 , n - 1 while i < j : str [ i ] , str [ j ] = str [ j ] , str [ i ] i += 1 j -= 1
void reverseStr ( string & str ) { int n = str . length ( ) ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) swap ( str [ i ] , str [ j ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def reverseStr ( str ) : n = len ( str ) i , j = 0 , n - 1 while i < j : str [ i ] , str [ j ] = str [ j ] , str [ i ] i += 1 j -= 1 </Code>
python-cpp-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING
python-cpp
def findMaximum ( arr , low , high ) : max = arr [ low ] i = low for i in range ( high + 1 ) : if arr [ i ] > max : max = arr [ i ] return max
int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low + 1 ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; else break ; } return max ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaximum ( arr , low , high ) : max = arr [ low ] i = low for i in range ( high + 1 ) : if arr [ i ] > max : max = arr [ i ] return max </Code>
python-cpp-LONGEST_PREFIX_ALSO_SUFFIX_1
python-cpp
def longestPrefixSuffix ( s ) : n = len ( s ) lps = [ 0 ] * n l = 0 i = 1 while ( i < n ) : if ( s [ i ] == s [ l ] ) : l = l + 1 lps [ i ] = l i = i + 1 else : if ( l != 0 ) : l = lps [ l - 1 ] else : ...
int longestPrefixSuffix ( string s ) { int n = s . length ( ) ; int lps [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s [ i ] == s [ len ] ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { len = lps [ len - 1 ] ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longestPrefixSuffix ( s ) : n = len ( s ) lps = [ 0 ] * n l = 0 i = 1 while ( i < n ) : if ( s [ i ] == s [ l ] ) : l = l + 1 lps [ i ] = l i = i + 1 else : if ( l != 0 ) : l = lps [ l - 1 ] else :...
python-cpp-SUM_AREA_RECTANGLES_POSSIBLE_ARRAY
python-cpp
def MaxTotalRectangleArea ( a , n ) : a.sort ( reverse = True ) sum = 0 flag = False len = 0 i = 0 while ( i < n - 1 ) : if ( i != 0 ) : i = i + 1 if ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == False ) : flag = True l...
int MaxTotalRectangleArea ( int a [ ] , int n ) { sort ( a , a + n , greater < int > ( ) ) ; int sum = 0 ; bool flag = false ; int len ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] || a [ i ] - a [ i + 1 ] == 1 ) && ( ! flag ) ) { flag = true ; len = a [ i + 1 ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def MaxTotalRectangleArea ( a , n ) : a.sort ( reverse = True ) sum = 0 flag = False len = 0 i = 0 while ( i < n - 1 ) : if ( i != 0 ) : i = i + 1 if ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == False ) : flag = True ...
python-cpp-PROGRAM_TO_PRINT_FIRST_N_FIBONACCI_NUMBERS
python-cpp
def printFibonacciNumbers ( n ) : f1 = 0 f2 = 1 if ( n < 1 ) : return for x in range ( 0 , n ) : print ( f2 , end = " " ) next = f1 + f2 f1 = f2 f2 = next
void printFibonacciNumbers ( int n ) { int f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; for ( i = 1 ; i <= n ; i ++ ) { cout << f2 << " " ; int next = f1 + f2 ; f1 = f2 ; f2 = next ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printFibonacciNumbers ( n ) : f1 = 0 f2 = 1 if ( n < 1 ) : return for x in range ( 0 , n ) : print ( f2 , end = " " ) next = f1 + f2 f1 = f2 f2 = next </Code>
python-cpp-LEXICOGRAPHICALLY_SMALLEST_ARRAY_K_CONSECUTIVE_SWAPS
python-cpp
def minimizeWithKSwaps ( arr , n , k ) : for i in range ( n - 1 ) : pos = i for j in range ( i + 1 , n ) : if ( j - i > k ) : break if ( arr [ j ] < arr [ pos ] ) : pos = j for j in range ( pos , i , - 1 ) : arr [ j ] , arr ...
void minimizeWithKSwaps ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 && k > 0 ; ++ i ) { int pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( j - i > k ) break ; if ( arr [ j ] < arr [ pos ] ) pos = j ; } for ( int j = pos ; j > i ; -- j ) swap ( a...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minimizeWithKSwaps ( arr , n , k ) : for i in range ( n - 1 ) : pos = i for j in range ( i + 1 , n ) : if ( j - i > k ) : break if ( arr [ j ] < arr [ pos ] ) : pos = j for j in range ( pos , i , - 1 ) : arr [ j ]...
python-cpp-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP
python-cpp
def isHeap ( arr , i , n ) : if i > int ( ( n - 2 ) / 2 ) : return True if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) : return True return False
bool isHeap ( int arr [ ] , int i , int n ) { if ( i > ( n - 2 ) / 2 ) return true ; if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) return true ; return false ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isHeap ( arr , i , n ) : if i > int ( ( n - 2 ) / 2 ) : return True if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) : return True return False </Code>
python-cpp-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_3
python-cpp
def findLength ( st , n ) : total = [ 0 ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : total [ i ] = ( total [ i - 1 ] + int ( st [ i - 1 ] ) - int ( '0' ) ) ans = 0 l = 2 while ( l <= n ) : for i in range ( n - l + 1 ) : j = i + l - 1 if ( total [ i + int ( l /...
int findLength ( string str , int n ) { int ans = 0 ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { int l = i , r = i + 1 ; int lsum = 0 , rsum = 0 ; while ( r < n && l >= 0 ) { lsum += str [ l ] - '0' ; rsum += str [ r ] - '0' ; if ( lsum == rsum ) ans = max ( ans , r - l + 1 ) ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findLength ( st , n ) : total = [ 0 ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : total [ i ] = ( total [ i - 1 ] + int ( st [ i - 1 ] ) - int ( '0' ) ) ans = 0 l = 2 while ( l <= n ) : for i in range ( n - l + 1 ) : j = i + l - 1 if ( total [ i + in...
python-cpp-COUNT_SUBARRAYS_WITH_SAME_EVEN_AND_ODD_ELEMENTS
python-cpp
def countSubarrays ( arr , n ) : difference = 0 ans = 0 hash_positive = [ 0 ] * ( n + 1 ) hash_negative = [ 0 ] * ( n + 1 ) hash_positive [ 0 ] = 1 for i in range ( n ) : if ( arr [ i ] & 1 == 1 ) : difference = difference + 1 else : difference = differenc...
int countSubarrays ( int arr [ ] , int n ) { int difference = 0 ; int ans = 0 ; int hash_positive [ n + 1 ] , hash_negative [ n + 1 ] ; fill_n ( hash_positive , n + 1 , 0 ) ; fill_n ( hash_negative , n + 1 , 0 ) ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] & 1 == 1...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countSubarrays ( arr , n ) : difference = 0 ans = 0 hash_positive = [ 0 ] * ( n + 1 ) hash_negative = [ 0 ] * ( n + 1 ) hash_positive [ 0 ] = 1 for i in range ( n ) : if ( arr [ i ] & 1 == 1 ) : difference = difference + 1 else : difference = di...
python-cpp-KNAPSACK_PROBLEM_1
python-cpp
def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( val [ i...
int knapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( ...
python-cpp-C_PROGRAM_SUBTRACTION_MATICES
python-cpp
def multiply ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ]
void multiply ( int A [ ] [ N ] , int B [ ] [ N ] , int C [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def multiply ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] </Code>
python-cpp-MAXIMUM_VALUE_CHOICE_EITHER_DIVIDING_CONSIDERING
python-cpp
def maxDP ( n ) : res = list ( ) res.append ( 0 ) res.append ( 1 ) i = 2 while i < n + 1 : res.append ( max ( i , ( res [ int ( i / 2 ) ] + res [ int ( i / 3 ) ] + res [ int ( i / 4 ) ] + res [ int ( i / 5 ) ] ) ) ) i = i + 1 return res [ n ]
int maxDP ( int n ) { int res [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxDP ( n ) : res = list ( ) res.append ( 0 ) res.append ( 1 ) i = 2 while i < n + 1 : res.append ( max ( i , ( res [ int ( i / 2 ) ] + res [ int ( i / 3 ) ] + res [ int ( i / 4 ) ] + res [ int ( i / 5 ) ] ) ) ) i = i + 1 return res [ n ] </Code>
python-cpp-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY
python-cpp
def findDiff ( arr , n ) : arr.sort ( ) count = 0 max_count = 0 min_count = n for i in range ( 0 , ( n - 1 ) ) : if arr [ i ] == arr [ i + 1 ] : count += 1 continue else : max_count = max ( max_count , count ) min_count = min ( min_count , coun...
int findDiff ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = max ( max_count , count ) ; min_count ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findDiff ( arr , n ) : arr.sort ( ) count = 0 max_count = 0 min_count = n for i in range ( 0 , ( n - 1 ) ) : if arr [ i ] == arr [ i + 1 ] : count += 1 continue else : max_count = max ( max_count , count ) min_count = min ( min_count...
python-cpp-NUMBER_SINK_NODES_GRAPH
python-cpp
def countSink ( n , m , edgeFrom , edgeTo ) : mark = [ 0 ] * ( n + 1 ) for i in range ( m ) : mark [ edgeFrom [ i ] ] = 1 count = 0 for i in range ( 1 , n + 1 ) : if ( not mark [ i ] ) : count += 1 return count
int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int mark [ n ] ; memset ( mark , 0 , sizeof mark ) ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( ! mark [ i ] ) count ++ ; return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countSink ( n , m , edgeFrom , edgeTo ) : mark = [ 0 ] * ( n + 1 ) for i in range ( m ) : mark [ edgeFrom [ i ] ] = 1 count = 0 for i in range ( 1 , n + 1 ) : if ( not mark [ i ] ) : count += 1 return count </Code>
python-cpp-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX
python-cpp
def countNegative ( M , n , m ) : count = 0 for i in range ( n ) : for j in range ( m ) : if M [ i ] [ j ] < 0 : count += 1 else : break return count
int countNegative ( int M [ ] [ 4 ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; }
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 for i in range ( n ) : for j in range ( m ) : if M [ i ] [ j ] < 0 : count += 1 else : break return count </Code>
python-cpp-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_1
python-cpp
def middleOfThree ( a , b , c ) : if a > b : if ( b > c ) : return b elif ( a > c ) : return c else : return a else : if ( a > c ) : return a elif ( b > c ) : return c else : return b
int middleOfThree ( int a , int b , int c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def middleOfThree ( a , b , c ) : if a > b : if ( b > c ) : return b elif ( a > c ) : return c else : return a else : if ( a > c ) : return a elif ( b > c ) : return c else : return b ...
python-cpp-COUNT_SUBSTRINGS_BINARY_STRING_CONTAINING_K_ONES
python-cpp
def countOfSubstringWithKOnes ( s , K ) : N = len ( s ) res = 0 countOfOne = 0 freq = [ 0 for i in range ( N + 1 ) ] freq [ 0 ] = 1 for i in range ( 0 , N , 1 ) : countOfOne += ord ( s [ i ] ) - ord ( '0' ) if ( countOfOne >= K ) : res += freq [ countOfOne - K ] ...
int countOfSubstringWithKOnes ( string s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int freq [ N + 1 ] = { 0 } ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s [ i ] - '0' ) ; if ( countOfOne >= K ) { res += freq [ c...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countOfSubstringWithKOnes ( s , K ) : N = len ( s ) res = 0 countOfOne = 0 freq = [ 0 for i in range ( N + 1 ) ] freq [ 0 ] = 1 for i in range ( 0 , N , 1 ) : countOfOne += ord ( s [ i ] ) - ord ( '0' ) if ( countOfOne >= K ) : res += freq [ countOfOne - K ...
python-cpp-FIND_FOUR_ELEMENTS_A_B_C_AND_D_IN_AN_ARRAY_SUCH_THAT_AB_CD
python-cpp
def findPairs ( arr , n ) : Hash = { } for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : sum = arr [ i ] + arr [ j ] if sum in Hash.keys ( ) : prev = Hash.get ( sum ) print ( str ( prev ) + " and (%d, %d)" % ( arr [ i ] , arr [ j ] ) ) return True else : Hash [ su...
bool findPairs ( int arr [ ] , int n ) { map < int , pair < int , int >> Hash ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( Hash . find ( sum ) == Hash . end ( ) ) Hash [ sum ] = make_pair ( i , j ) ; else { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findPairs ( arr , n ) : Hash = { } for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : sum = arr [ i ] + arr [ j ] if sum in Hash.keys ( ) : prev = Hash.get ( sum ) print ( str ( prev ) + " and (%d, %d)" % ( arr [ i ] , arr [ j ] ) ) return True else : Ha...
python-cpp-LINEAR_SEARCH
python-cpp
def search ( arr , n , x ) : for i in range ( 0 , n ) : if ( arr [ i ] == x ) : return i return - 1
int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def search ( arr , n , x ) : for i in range ( 0 , n ) : if ( arr [ i ] == x ) : return i return - 1 </Code>
python-cpp-CHECK_POSSIBLE_TRANSFORM_ONE_STRING_ANOTHER
python-cpp
def check ( s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) dp = ( [ [ False for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] ) dp [ 0 ] [ 0 ] = True for i in range ( len ( s1 ) ) : for j in range ( len ( s2 ) + 1 ) : if ( dp [ i ] [ j ] ) : if ( ( j < len ( s2 ) a...
bool check ( string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; bool dp [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < s1 . lengt...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def check ( s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) dp = ( [ [ False for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] ) dp [ 0 ] [ 0 ] = True for i in range ( len ( s1 ) ) : for j in range ( len ( s2 ) + 1 ) : if ( dp [ i ] [ j ] ) : if ( ( j < len (...
python-cpp-PROGRAM_BINARY_DECIMAL_CONVERSION_1
python-cpp
def binaryToDecimal ( n ) : num = n dec_value = 0 base1 = 1 len1 = len ( num ) for i in range ( len1 - 1 , - 1 , - 1 ) : if ( num [ i ] == '1' ) : dec_value += base1 base1 = base1 * 2 return dec_value
int binaryToDecimal ( string n ) { string num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num [ i ] == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binaryToDecimal ( n ) : num = n dec_value = 0 base1 = 1 len1 = len ( num ) for i in range ( len1 - 1 , - 1 , - 1 ) : if ( num [ i ] == '1' ) : dec_value += base1 base1 = base1 * 2 return dec_value </Code>
python-cpp-COUNT_WAYS_BUILD_STREET_GIVEN_CONSTRAINTS
python-cpp
def countWays ( n ) : dp = [ [ 0 ] * ( n + 1 ) for i in range ( 2 ) ] dp [ 0 ] [ 1 ] = 1 dp [ 1 ] [ 1 ] = 2 for i in range ( 2 , n + 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] dp [ 1 ] [ i ] = ( dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ) return dp [ 0 ] [ n ] +...
long countWays ( int n ) { long dp [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ] [ n ] + dp [ 1 ] [ n ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countWays ( n ) : dp = [ [ 0 ] * ( n + 1 ) for i in range ( 2 ) ] dp [ 0 ] [ 1 ] = 1 dp [ 1 ] [ 1 ] = 2 for i in range ( 2 , n + 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] dp [ 1 ] [ i ] = ( dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ) return dp [ 0 ] ...
python-cpp-MIRROR_CHARACTERS_STRING
python-cpp
def compute ( st , n ) : reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" l = len ( st ) answer = "" for i in range ( 0 , n ) : answer = answer + st [ i ] for i in range ( n , l ) : answer = ( answer + reverseAlphabet [ ord ( st [ i ] ) - ord ( 'a' ) ] ) return answer
string compute ( string str , int n ) { string reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ; int l = str . length ( ) ; for ( int i = n ; i < l ; i ++ ) str [ i ] = reverseAlphabet [ str [ i ] - 'a' ] ; return str ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def compute ( st , n ) : reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" l = len ( st ) answer = "" for i in range ( 0 , n ) : answer = answer + st [ i ] for i in range ( n , l ) : answer = ( answer + reverseAlphabet [ ord ( st [ i ] ) - ord ( 'a' ) ] ) return answer </Cod...
python-cpp-MAXIMUM_LENGTH_SUBSEQUENCE_DIFFERENCE_ADJACENT_ELEMENTS_EITHER_0_1
python-cpp
def maxLenSub ( arr , n ) : mls = [ ] max = 0 for i in range ( n ) : mls.append ( 1 ) for i in range ( n ) : for j in range ( i ) : if ( abs ( arr [ i ] - arr [ j ] ) <= 1 and mls [ i ] < mls [ j ] + 1 ) : mls [ i ] = mls [ j ] + 1 for i in range ( n ) : ...
int maxLenSub ( int arr [ ] , int n ) { int mls [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls [ j ] + 1 ; for ( int i = 0 ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxLenSub ( arr , n ) : mls = [ ] max = 0 for i in range ( n ) : mls.append ( 1 ) for i in range ( n ) : for j in range ( i ) : if ( abs ( arr [ i ] - arr [ j ] ) <= 1 and mls [ i ] < mls [ j ] + 1 ) : mls [ i ] = mls [ j ] + 1 for i in range ( ...
python-cpp-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION_1
python-cpp
def exponentiation ( bas , exp ) : t = 1 while ( exp > 0 ) : if ( exp % 2 != 0 ) : t = ( t * bas ) % N bas = ( bas * bas ) % N exp = int ( exp / 2 ) return t % N
long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def exponentiation ( bas , exp ) : t = 1 while ( exp > 0 ) : if ( exp % 2 != 0 ) : t = ( t * bas ) % N bas = ( bas * bas ) % N exp = int ( exp / 2 ) return t % N </Code>
python-cpp-NEWMAN_CONWAY_SEQUENCE_1
python-cpp
def sequence ( n ) : f = array.array ( 'i' , [ 0 , 1 , 1 ] ) for i in range ( 3 , n + 1 ) : r = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] f.append ( r ) ; return r
int sequence ( int n ) { int f [ n + 1 ] ; int i ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sequence ( n ) : f = array.array ( 'i' , [ 0 , 1 , 1 ] ) for i in range ( 3 , n + 1 ) : r = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] f.append ( r ) ; return r </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_15_LONGEST_BITONIC_SUBSEQUENCE
python-cpp
def lbs ( arr ) : n = len ( arr ) lis = [ 1 for i in range ( n + 1 ) ] for i in range ( 1 , n ) : for j in range ( 0 , i ) : if ( ( arr [ i ] > arr [ j ] ) and ( lis [ i ] < lis [ j ] + 1 ) ) : lis [ i ] = lis [ j ] + 1 lds = [ 1 for i in range ( n + 1 ) ] for i i...
int lbs ( int arr [ ] , int n ) { int i , j ; int * lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int * lds = new int [ n ] ; for (...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lbs ( arr ) : n = len ( arr ) lis = [ 1 for i in range ( n + 1 ) ] for i in range ( 1 , n ) : for j in range ( 0 , i ) : if ( ( arr [ i ] > arr [ j ] ) and ( lis [ i ] < lis [ j ] + 1 ) ) : lis [ i ] = lis [ j ] + 1 lds = [ 1 for i in range ( n + 1 ) ] ...
python-cpp-MAXIMUM_SUM_INCREASING_ORDER_ELEMENTS_N_ARRAYS
python-cpp
def maximumSum ( a , n ) : global M for i in range ( 0 , n ) : a [ i ].sort ( ) sum = a [ n - 1 ] [ M - 1 ] prev = a [ n - 1 ] [ M - 1 ] for i in range ( n - 2 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( a [ i ] [ j ] < prev ) : prev = a [ ...
int maximumSum ( int a [ ] [ M ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a [ i ] , a [ i ] + M ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] <...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maximumSum ( a , n ) : global M for i in range ( 0 , n ) : a [ i ].sort ( ) sum = a [ n - 1 ] [ M - 1 ] prev = a [ n - 1 ] [ M - 1 ] for i in range ( n - 2 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( a [ i ] [ j ] < prev ) : prev...
python-cpp-MASTER_THEOREM_SUBTRACT_CONQUER_RECURRENCES
python-cpp
def fib ( n ) : if ( n <= 1 ) : return n return fib ( n - 1 ) + fib ( n - 2 )
int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def fib ( n ) : if ( n <= 1 ) : return n return fib ( n - 1 ) + fib ( n - 2 ) </Code>
python-cpp-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS
python-cpp
def countPairs ( str1 ) : result = 0 ; n = len ( str1 ) for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( abs ( ord ( str1 [ i ] ) - ord ( str1 [ j ] ) ) == abs ( i - j ) ) : result += 1 ; return result ;
int countPairs ( string str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ; return result ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPairs ( str1 ) : result = 0 ; n = len ( str1 ) for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( abs ( ord ( str1 [ i ] ) - ord ( str1 [ j ] ) ) == abs ( i - j ) ) : result += 1 ; return result ; </Code>
python-cpp-CHECK_IF_ALL_THE_ELEMENTS_CAN_BE_MADE_OF_SAME_PARITY_BY_INVERTING_ADJACENT_ELEMENTS
python-cpp
def flipsPossible ( a , n ) : count_odd = 0 count_even = 0 for i in range ( n ) : if ( a [ i ] & 1 ) : count_odd += 1 else : count_even += 1 if ( count_odd % 2 and count_even % 2 ) : return False else : return True
bool flipsPossible ( int a [ ] , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] & 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 && count_even % 2 ) return false ; else return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def flipsPossible ( a , n ) : count_odd = 0 count_even = 0 for i in range ( n ) : if ( a [ i ] & 1 ) : count_odd += 1 else : count_even += 1 if ( count_odd % 2 and count_even % 2 ) : return False else : return True </Code>
python-cpp-CHECK_GIVEN_ARRAY_CONTAINS_DUPLICATE_ELEMENTS_WITHIN_K_DISTANCE
python-cpp
def checkDuplicatesWithinK ( arr , n , k ) : myset = [ ] for i in range ( n ) : if arr [ i ] in myset : return True myset.append ( arr [ i ] ) if ( i >= k ) : myset.remove ( arr [ i - k ] ) return False
bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) { unordered_set < int > myset ; for ( int i = 0 ; i < n ; i ++ ) { if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ; myset . insert ( arr [ i ] ) ; if ( i >= k ) myset . erase ( arr [ i - k ] ) ; } return false ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkDuplicatesWithinK ( arr , n , k ) : myset = [ ] for i in range ( n ) : if arr [ i ] in myset : return True myset.append ( arr [ i ] ) if ( i >= k ) : myset.remove ( arr [ i - k ] ) return False </Code>
python-cpp-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS_1
python-cpp
def diagonalsquare ( mat , row , column ) : print ( "Diagonal one : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ i ] * mat [ i ] [ i ] , end = "" ) print ( "\n\nDiagonal two : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ row - i - 1 ] * mat [ i ] [ r...
void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) { cout << " \nDiagonal one : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ i ] * mat [ i ] [ i ] << " " ; } cout << " \n\nDiagonal two : " ; for ( int i = 0 ; i < row ; i ++ ) { cout << mat [ i ] [ row - i - 1 ] ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def diagonalsquare ( mat , row , column ) : print ( "Diagonal one : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ i ] * mat [ i ] [ i ] , end = "" ) print ( "\n\nDiagonal two : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ row - i - 1 ] * mat [ ...
python-cpp-FIND_THE_LARGEST_RECTANGLE_OF_1S_WITH_SWAPPING_OF_COLUMNS_ALLOWED
python-cpp
def maxArea ( mat ) : hist = [ [ 0 for i in range ( C + 1 ) ] for i in range ( R + 1 ) ] for i in range ( 0 , C , 1 ) : hist [ 0 ] [ i ] = mat [ 0 ] [ i ] for j in range ( 1 , R , 1 ) : if ( ( mat [ j ] [ i ] == 0 ) ) : hist [ j ] [ i ] = 0 else : ...
int maxArea ( bool mat [ R ] [ C ] ) { int hist [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } for ( int i = 0 ; i < R ; i ++ ) {...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxArea ( mat ) : hist = [ [ 0 for i in range ( C + 1 ) ] for i in range ( R + 1 ) ] for i in range ( 0 , C , 1 ) : hist [ 0 ] [ i ] = mat [ 0 ] [ i ] for j in range ( 1 , R , 1 ) : if ( ( mat [ j ] [ i ] == 0 ) ) : hist [ j ] [ i ] = 0 else : ...
python-cpp-LONGEST_SUBSEQUENCE_SUCH_THAT_DIFFERENCE_BETWEEN_ADJACENTS_IS_ONE
python-cpp
def longestSubseqWithDiffOne ( arr , n ) : dp = [ 1 for i in range ( n ) ] for i in range ( n ) : for j in range ( i ) : if ( ( arr [ i ] == arr [ j ] + 1 ) or ( arr [ i ] == arr [ j ] - 1 ) ) : dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) result = 1 for i in range ( n ...
int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [ i ] , ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longestSubseqWithDiffOne ( arr , n ) : dp = [ 1 for i in range ( n ) ] for i in range ( n ) : for j in range ( i ) : if ( ( arr [ i ] == arr [ j ] + 1 ) or ( arr [ i ] == arr [ j ] - 1 ) ) : dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) result = 1 for i in ran...
python-cpp-MAXIMUM_NUMBER_OF_SQUARES_THAT_CAN_BE_FIT_IN_A_RIGHT_ANGLE_ISOSCELES_TRIANGLE
python-cpp
def maxSquare ( b , m ) : return ( b / m - 1 ) * ( b / m ) / 2
int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSquare ( b , m ) : return ( b / m - 1 ) * ( b / m ) / 2 </Code>
python-cpp-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE
python-cpp
def maxSumPairWithDifferenceLessThanK ( arr , N , K ) : arr.sort ( ) dp = [ 0 ] * N dp [ 0 ] = 0 for i in range ( 1 , N ) : dp [ i ] = dp [ i - 1 ] if ( arr [ i ] - arr [ i - 1 ] < K ) : if ( i >= 2 ) : dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + ar...
int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) { sort ( arr , arr + N ) ; int dp [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSumPairWithDifferenceLessThanK ( arr , N , K ) : arr.sort ( ) dp = [ 0 ] * N dp [ 0 ] = 0 for i in range ( 1 , N ) : dp [ i ] = dp [ i - 1 ] if ( arr [ i ] - arr [ i - 1 ] < K ) : if ( i >= 2 ) : dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i...