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-K_TH_MISSING_ELEMENT_INCREASING_SEQUENCE_NOT_PRESENT_GIVEN_SEQUENCE
python-cpp
def find ( a , b , k , n1 , n2 ) : s = set ( ) for i in range ( n2 ) : s.add ( b [ i ] ) missing = 0 for i in range ( n1 ) : if a [ i ] not in s : missing += 1 if missing == k : return a [ i ] return - 1
int find ( int a [ ] , int b [ ] , int k , int n1 , int n2 ) { unordered_set < int > s ; for ( int i = 0 ; i < n2 ; i ++ ) s . insert ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( s . find ( a [ i ] ) == s . end ( ) ) missing ++ ; if ( missing == k ) return a [ i ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def find ( a , b , k , n1 , n2 ) : s = set ( ) for i in range ( n2 ) : s.add ( b [ i ] ) missing = 0 for i in range ( n1 ) : if a [ i ] not in s : missing += 1 if missing == k : return a [ i ] return - 1 </Code>
python-cpp-FIND_SUM_NODES_GIVEN_PERFECT_BINARY_TREE_1
python-cpp
def sumNodes ( l ) : leafNodeCount = math.pow ( 2 , l - 1 ) sumLastLevel = 0 sumLastLevel = ( ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ) sum = sumLastLevel * l return int ( sum )
int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; int sumLastLevel = 0 ; sumLastLevel = ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ; int sum = sumLastLevel * l ; return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumNodes ( l ) : leafNodeCount = math.pow ( 2 , l - 1 ) sumLastLevel = 0 sumLastLevel = ( ( leafNodeCount * ( leafNodeCount + 1 ) ) / 2 ) sum = sumLastLevel * l return int ( sum ) </Code>
python-cpp-LEONARDO_NUMBER_1
python-cpp
def leonardo ( n ) : dp = [ ] dp.append ( 1 ) dp.append ( 1 ) for i in range ( 2 , n + 1 ) : dp.append ( dp [ i - 1 ] + dp [ i - 2 ] + 1 ) return dp [ n ]
int leonardo ( int n ) { int dp [ n + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] + 1 ; return dp [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def leonardo ( n ) : dp = [ ] dp.append ( 1 ) dp.append ( 1 ) for i in range ( 2 , n + 1 ) : dp.append ( dp [ i - 1 ] + dp [ i - 2 ] + 1 ) return dp [ n ] </Code>
python-cpp-COUNT_NATURAL_NUMBERS_WHOSE_PERMUTATION_GREATER_NUMBER
python-cpp
def countNumber ( n ) : result = 0 for i in range ( 1 , 10 ) : s = [ ] if ( i <= n ) : s.append ( i ) result += 1 while len ( s ) != 0 : tp = s [ - 1 ] s.pop ( ) for j in range ( tp % 10 , 10 ) : x = tp * 10 + j ...
int countNumber ( int n ) { int result = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { stack < int > s ; if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . top ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countNumber ( n ) : result = 0 for i in range ( 1 , 10 ) : s = [ ] if ( i <= n ) : s.append ( i ) result += 1 while len ( s ) != 0 : tp = s [ - 1 ] s.pop ( ) for j in range ( tp % 10 , 10 ) : x = tp * ...
python-cpp-CHECK_WHETHER_GIVEN_DEGREES_VERTICES_REPRESENT_GRAPH_TREE
python-cpp
def check ( degree , n ) : deg_sum = sum ( degree ) if ( 2 * ( n - 1 ) == deg_sum ) : return True else : return False
bool check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) deg_sum += degree [ i ] ; return ( 2 * ( n - 1 ) == deg_sum ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def check ( degree , n ) : deg_sum = sum ( degree ) if ( 2 * ( n - 1 ) == deg_sum ) : return True else : return False </Code>
python-cpp-FIND_SUM_EVEN_INDEX_BINOMIAL_COEFFICIENTS_1
python-cpp
def evenbinomialCoeffSum ( n ) : return ( 1 << ( n - 1 ) )
int evenbinomialCoeffSum ( int n ) { return ( 1 << ( n - 1 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def evenbinomialCoeffSum ( n ) : return ( 1 << ( n - 1 ) ) </Code>
python-cpp-FIND_MINIMUM_ELEMENT_IN_A_SORTED_AND_ROTATED_ARRAY
python-cpp
def findMin ( arr , low , high ) : if high < low : return arr [ 0 ] if high == low : return arr [ low ] mid = int ( ( low + high ) / 2 ) if mid < high and arr [ mid + 1 ] < arr [ mid ] : return arr [ mid + 1 ] if mid > low and arr [ mid ] < arr [ mid - 1 ] : return ar...
int findMin ( int arr [ ] , int low , int high ) { if ( high < low ) return arr [ 0 ] ; if ( high == low ) return arr [ low ] ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return arr [ mid + 1 ] ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return arr [ mid ] ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMin ( arr , low , high ) : if high < low : return arr [ 0 ] if high == low : return arr [ low ] mid = int ( ( low + high ) / 2 ) if mid < high and arr [ mid + 1 ] < arr [ mid ] : return arr [ mid + 1 ] if mid > low and arr [ mid ] < arr [ mid - 1 ] : re...
python-cpp-SQUARE_PYRAMIDAL_NUMBER_SUM_SQUARES
python-cpp
def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n n += 1 n -= 1 if _sum == s : return n return - 1
int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n ; if ( sum == s ) return n ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n n += 1 n -= 1 if _sum == s : return n return - 1 </Code>
python-cpp-RETURN_MAXIMUM_OCCURRING_CHARACTER_IN_THE_INPUT_STRING
python-cpp
def getMaxOccuringChar ( str ) : count = [ 0 ] * ASCII_SIZE max = - 1 c = '' for i in str : count [ ord ( i ) ] += 1 ; for i in str : if max < count [ ord ( i ) ] : max = count [ ord ( i ) ] c = i return c
char getMaxOccuringChar ( char * str ) { int count [ ASCII_SIZE ] = { 0 } ; int len = strlen ( str ) ; int max = 0 ; char result ; for ( int i = 0 ; i < len ; i ++ ) { count [ str [ i ] ] ++ ; if ( max < count [ str [ i ] ] ) { max = count [ str [ i ] ] ; re...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getMaxOccuringChar ( str ) : count = [ 0 ] * ASCII_SIZE max = - 1 c = '' for i in str : count [ ord ( i ) ] += 1 ; for i in str : if max < count [ ord ( i ) ] : max = count [ ord ( i ) ] c = i return c </Code>
python-cpp-COUNT_STRINGS_CAN_FORMED_USING_B_C_GIVEN_CONSTRAINTS_1
python-cpp
def countStr ( n ) : return ( 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) // 2 ) )
int countStr ( int n ) { return 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) / 2 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countStr ( n ) : return ( 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) // 2 ) ) </Code>
python-cpp-GIVEN_N_X_N_SQUARE_MATRIX_FIND_SUM_SUB_SQUARES_SIZE_K_X_K
python-cpp
def printSumSimple ( mat , k ) : if ( k > n ) : return for i in range ( n - k + 1 ) : for j in range ( n - k + 1 ) : sum = 0 for p in range ( i , k + i ) : for q in range ( j , k + j ) : sum += mat [ p ] [ q ] print ( sum , ...
void printSumSimple ( int mat [ ] [ n ] , int k ) { if ( k > n ) return ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { for ( int j = 0 ; j < n - k + 1 ; j ++ ) { int sum = 0 ; for ( int p = i ; p < k + i ; p ++ ) for ( int q = j ; q < k + j ; q ++ ) sum += mat [ p ]...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printSumSimple ( mat , k ) : if ( k > n ) : return for i in range ( n - k + 1 ) : for j in range ( n - k + 1 ) : sum = 0 for p in range ( i , k + i ) : for q in range ( j , k + j ) : sum += mat [ p ] [ q ] print (...
python-cpp-COUNT_OF_N_DIGIT_NUMBERS_WHOSE_SUM_OF_DIGITS_EQUALS_TO_GIVEN_SUM
python-cpp
def findCount ( n , sum ) : start = math.pow ( 10 , n - 1 ) end = math.pow ( 10 , n ) - 1 count = 0 i = start while ( i <= end ) : cur = 0 temp = i while ( temp != 0 ) : cur += temp % 10 temp = temp // 10 if ( cur == sum ) : count =...
void findCount ( int n , int sum ) { int start = pow ( 10 , n - 1 ) ; int end = pow ( 10 , n ) - 1 ; int count = 0 ; int i = start ; while ( i <= end ) { int cur = 0 ; int temp = i ; while ( temp != 0 ) { cur += temp % 10 ; temp = temp / 10 ; } if ( cur == sum ) { count +...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findCount ( n , sum ) : start = math.pow ( 10 , n - 1 ) end = math.pow ( 10 , n ) - 1 count = 0 i = start while ( i <= end ) : cur = 0 temp = i while ( temp != 0 ) : cur += temp % 10 temp = temp // 10 if ( cur == sum ) : ...
python-cpp-COUNT_GROUPINGS_NUMBER_SUM_DIGITS_EVERY_SUB_GROUP_LESS_EQUALS_IMMEDIATE_RIGHT_SUB_GROUP_1
python-cpp
def countGroups ( position , previous_sum , length , num ) : if ( position == length ) : return 1 if ( dp [ position ] [ previous_sum ] != - 1 ) : return dp [ position ] [ previous_sum ] dp [ position ] [ previous_sum ] = 0 res = 0 sum = 0 for i in range ( position , length ) : ...
int countGroups ( int position , int previous_sum , int length , char * num ) { if ( position == length ) return 1 ; if ( dp [ position ] [ previous_sum ] != - 1 ) return dp [ position ] [ previous_sum ] ; dp [ position ] [ previous_sum ] = 0 ; int res = 0 ; int sum = 0 ; for ( int i = position ; i < leng...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countGroups ( position , previous_sum , length , num ) : if ( position == length ) : return 1 if ( dp [ position ] [ previous_sum ] != - 1 ) : return dp [ position ] [ previous_sum ] dp [ position ] [ previous_sum ] = 0 res = 0 sum = 0 for i in range ( position , lengt...
python-cpp-MAXIMUM_POINTS_INTERSECTION_N_CIRCLES
python-cpp
def intersection ( n ) : return n * ( n - 1 )
int intersection ( int n ) { return n * ( n - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def intersection ( n ) : return n * ( n - 1 ) </Code>
python-cpp-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS_2
python-cpp
def findSum ( n ) : n -= 1 sum = 0 sum += ( n * ( n + 1 ) ) / 2 sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 return int ( sum )
int findSum ( int n ) { n -- ; int sum = 0 ; sum += ( n * ( n + 1 ) ) / 2 ; sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; return sum ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSum ( n ) : n -= 1 sum = 0 sum += ( n * ( n + 1 ) ) / 2 sum += ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 return int ( sum ) </Code>
python-cpp-FIND_REPEATING_ELEMENT_SORTED_ARRAY_SIZE_N
python-cpp
def findRepeatingElement ( arr , low , high ) : if low > high : return - 1 mid = ( low + high ) / 2 if ( arr [ mid ] != mid + 1 ) : if ( mid > 0 and arr [ mid ] == arr [ mid - 1 ] ) : return mid return findRepeatingElement ( arr , low , mid - 1 ) return findRepeatingE...
int findRepeatingElement ( int arr [ ] , int low , int high ) { if ( low > high ) return - 1 ; int mid = ( low + high ) / 2 ; if ( arr [ mid ] != mid + 1 ) { if ( mid > 0 && arr [ mid ] == arr [ mid - 1 ] ) return mid ; return findRepeatingElement ( arr , low , mid - 1 ) ; } return findRepeatingElemen...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findRepeatingElement ( arr , low , high ) : if low > high : return - 1 mid = ( low + high ) / 2 if ( arr [ mid ] != mid + 1 ) : if ( mid > 0 and arr [ mid ] == arr [ mid - 1 ] ) : return mid return findRepeatingElement ( arr , low , mid - 1 ) return findRep...
python-cpp-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS
python-cpp
def smallest ( x , y , z ) : c = 0 while ( x and y and z ) : x = x - 1 y = y - 1 z = z - 1 c = c + 1 return c
int smallest ( int x , int y , int z ) { int c = 0 ; while ( x && y && z ) { x -- ; y -- ; z -- ; c ++ ; } return c ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def smallest ( x , y , z ) : c = 0 while ( x and y and z ) : x = x - 1 y = y - 1 z = z - 1 c = c + 1 return c </Code>
python-cpp-PATH_MAXIMUM_AVERAGE_VALUE
python-cpp
def maxAverageOfPath ( cost , N ) : dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] for i in range ( 1 , N ) : dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] for j in range ( 1 , N ) : dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [...
double maxAverageOfPath ( int cost [ M ] [ M ] , int N ) { int dp [ N + 1 ] [ N + 1 ] ; dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] ; for ( int j = 1 ; j < N ; j ++ ) dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] + cost [ 0 ] [ j ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxAverageOfPath ( cost , N ) : dp = [ [ 0 for i in range ( N + 1 ) ] for j in range ( N + 1 ) ] dp [ 0 ] [ 0 ] = cost [ 0 ] [ 0 ] for i in range ( 1 , N ) : dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + cost [ i ] [ 0 ] for j in range ( 1 , N ) : dp [ 0 ] [ j ] = dp [ 0 ] [ j - 1 ] +...
python-cpp-COUNT_PAIRS_DIFFERENCE_EQUAL_K
python-cpp
def countPairsWithDiffK ( arr , n , k ) : count = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : count += 1 return count
int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPairsWithDiffK ( arr , n , k ) : count = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : count += 1 return count </Code>
python-cpp-LONGEST_SUBSEQUENCE_DIFFERENCE_ADJACENTS_ONE_SET_2
python-cpp
def longLenSub ( arr , n ) : um = defaultdict ( lambda : 0 ) longLen = 0 for i in range ( n ) : len1 = 0 if ( arr [ i - 1 ] in um and len1 < um [ arr [ i ] - 1 ] ) : len1 = um [ arr [ i ] - 1 ] if ( arr [ i ] + 1 in um and len1 < um [ arr [ i ] + 1 ] ) : len1 ...
int longLenSub ( int arr [ ] , int n ) { unordered_map < int , int > um ; int longLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = 0 ; if ( um . find ( arr [ i ] - 1 ) != um . end ( ) && len < um [ arr [ i ] - 1 ] ) len = um [ arr [ i ] - 1 ] ; if ( um . find ( arr [ i ] + 1 ) != um . end ( ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longLenSub ( arr , n ) : um = defaultdict ( lambda : 0 ) longLen = 0 for i in range ( n ) : len1 = 0 if ( arr [ i - 1 ] in um and len1 < um [ arr [ i ] - 1 ] ) : len1 = um [ arr [ i ] - 1 ] if ( arr [ i ] + 1 in um and len1 < um [ arr [ i ] + 1 ] ) : ...
python-cpp-RECURSIVE_PROGRAM_PRIME_NUMBER
python-cpp
def isPrime ( n , i = 2 ) : if ( n <= 2 ) : return True if ( n == 2 ) else False if ( n % i == 0 ) : return False if ( i * i > n ) : return true return isPrime ( n , i + 1 )
bool isPrime ( int n , int i = 2 ) { if ( n <= 2 ) return ( n == 2 ) ? true : false ; if ( n % i == 0 ) return false ; if ( i * i > n ) return true ; return isPrime ( n , i + 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPrime ( n , i = 2 ) : if ( n <= 2 ) : return True if ( n == 2 ) else False if ( n % i == 0 ) : return False if ( i * i > n ) : return true return isPrime ( n , i + 1 ) </Code>
python-cpp-FIND_LAST_DIGIT_FACTORIAL_DIVIDES_FACTORIAL_B
python-cpp
def computeLastDigit ( A , B ) : variable = 1 if ( A == B ) : return 1 elif ( ( B - A ) >= 5 ) : return 0 else : for i in range ( A + 1 , B + 1 ) : variable = ( variable * ( i % 10 ) ) % 10 return variable % 10
int computeLastDigit ( long long int A , long long int B ) { int variable = 1 ; if ( A == B ) return 1 ; else if ( ( B - A ) >= 5 ) return 0 ; else { for ( long long int i = A + 1 ; i <= B ; i ++ ) variable = ( variable * ( i % 10 ) ) % 10 ; return variable % 10 ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def computeLastDigit ( A , B ) : variable = 1 if ( A == B ) : return 1 elif ( ( B - A ) >= 5 ) : return 0 else : for i in range ( A + 1 , B + 1 ) : variable = ( variable * ( i % 10 ) ) % 10 return variable % 10 </Code>
python-cpp-MINIMUM_OPERATIONS_REQUIRED_SET_ELEMENTS_BINARY_MATRIX
python-cpp
def minOperation ( arr ) : ans = 0 for i in range ( N - 1 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( arr [ i ] [ j ] == 0 ) : ans += 1 for k in range ( i + 1 ) : for h in range ( j + 1 ) : if ( arr [...
int minOperation ( bool arr [ N ] [ M ] ) { int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { for ( int j = M - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] == 0 ) { ans ++ ; for ( int k = 0 ; k <= i ; k ++ ) { for ( int h = 0 ; h <= j ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minOperation ( arr ) : ans = 0 for i in range ( N - 1 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( arr [ i ] [ j ] == 0 ) : ans += 1 for k in range ( i + 1 ) : for h in range ( j + 1 ) : if ...
python-cpp-NEXT_POWER_OF_2_1
python-cpp
def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p </Code>
python-cpp-MINIMIZE_THE_MAXIMUM_DIFFERENCE_BETWEEN_THE_HEIGHTS
python-cpp
def getMinDiff ( arr , n , k ) : if ( n == 1 ) : return 0 arr.sort ( ) ans = arr [ n - 1 ] - arr [ 0 ] small = arr [ 0 ] + k big = arr [ n - 1 ] - k if ( small > big ) : small , big = big , small for i in range ( 1 , n - 1 ) : subtract = arr [ i ] - k add = ar...
int getMinDiff ( int arr [ ] , int n , int k ) { if ( n == 1 ) return 0 ; sort ( arr , arr + n ) ; int ans = arr [ n - 1 ] - arr [ 0 ] ; int small = arr [ 0 ] + k ; int big = arr [ n - 1 ] - k ; if ( small > big ) swap ( small , big ) ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int subtract = arr [ i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getMinDiff ( arr , n , k ) : if ( n == 1 ) : return 0 arr.sort ( ) ans = arr [ n - 1 ] - arr [ 0 ] small = arr [ 0 ] + k big = arr [ n - 1 ] - k if ( small > big ) : small , big = big , small for i in range ( 1 , n - 1 ) : subtract = arr [ i ] - k a...
python-cpp-DIFFERENT_WAYS_SUM_N_USING_NUMBERS_GREATER_EQUAL_M
python-cpp
def numberofways ( n , m ) : dp = np.zeros ( ( n + 2 , n + 2 ) ) dp [ 0 ] [ n + 1 ] = 1 for k in range ( n , m - 1 , - 1 ) : for i in range ( n + 1 ) : dp [ i ] [ k ] = dp [ i ] [ k + 1 ] if ( i - k >= 0 ) : dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - k ] [ k...
int numberofways ( int n , int m ) { int dp [ n + 2 ] [ n + 2 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ n + 1 ] = 1 ; for ( int k = n ; k >= m ; k -- ) { for ( int i = 0 ; i <= n ; i ++ ) { dp [ i ] [ k ] = dp [ i ] [ k + 1 ] ; if ( i - k >= 0 ) dp [ i ] [ k ] = ( dp [ i ] [...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def numberofways ( n , m ) : dp = np.zeros ( ( n + 2 , n + 2 ) ) dp [ 0 ] [ n + 1 ] = 1 for k in range ( n , m - 1 , - 1 ) : for i in range ( n + 1 ) : dp [ i ] [ k ] = dp [ i ] [ k + 1 ] if ( i - k >= 0 ) : dp [ i ] [ k ] = ( dp [ i ] [ k ] + dp [ i - ...
python-cpp-FIND_WHETHER_GIVEN_INTEGER_POWER_3_NOT
python-cpp
def check ( n ) : return 1162261467 % n == 0
bool check ( int n ) { return 1162261467 % n == 0 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def check ( n ) : return 1162261467 % n == 0 </Code>
python-cpp-NEWMAN_CONWAY_SEQUENCE
python-cpp
def sequence ( n ) : if n == 1 or n == 2 : return 1 else : return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ;
int sequence ( int n ) { if ( n == 1 || n == 2 ) return 1 ; else return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sequence ( n ) : if n == 1 or n == 2 : return 1 else : return sequence ( sequence ( n - 1 ) ) + sequence ( n - sequence ( n - 1 ) ) ; </Code>
python-cpp-SMALLEST_DERANGEMENT_SEQUENCE
python-cpp
def generate_derangement ( N ) : S = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 ) : S [ i ] = i D = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 , 2 ) : if i == N : D [ N ] = S [ N - 1 ] D [ N - 1 ] = S [ N ] else : D [ i ] = i + 1 ...
void generate_derangement ( int N ) { int S [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) S [ i ] = i ; int D [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 2 ) { if ( i == N && i % N != 0 ) { int temp = D [ N ] D [ N ] = D [ N - 1 ] ; D [ N - 1 ] = temp ; } else { D [ i ] = i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def generate_derangement ( N ) : S = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 ) : S [ i ] = i D = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 , 2 ) : if i == N : D [ N ] = S [ N - 1 ] D [ N - 1 ] = S [ N ] else : D [ i ] = i + 1 ...
python-cpp-NUMBER_OF_TRIANGLES_IN_DIRECTED_AND_UNDIRECTED_GRAPHS
python-cpp
def countTriangle ( g , isDirected ) : nodes = len ( g ) count_Triangle = 0 for i in range ( nodes ) : for j in range ( nodes ) : for k in range ( nodes ) : if ( i != j and i != k and j != k and g [ i ] [ j ] and g [ j ] [ k ] and g [ k ] [ i ] ) : cou...
int countTriangle ( int graph [ V ] [ V ] , bool isDirected ) { int count_Triangle = 0 ; for ( int i = 0 ; i < V ; i ++ ) { for ( int j = 0 ; j < V ; j ++ ) { for ( int k = 0 ; k < V ; k ++ ) { if ( graph [ i ] [ j ] && graph [ j ] [ k ] && graph [ k ] [ i ] ) count_Triangl...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countTriangle ( g , isDirected ) : nodes = len ( g ) count_Triangle = 0 for i in range ( nodes ) : for j in range ( nodes ) : for k in range ( nodes ) : if ( i != j and i != k and j != k and g [ i ] [ j ] and g [ j ] [ k ] and g [ k ] [ i ] ) : ...
python-cpp-HEIGHT_N_ARY_TREE_PARENT_ARRAY_GIVEN
python-cpp
def findHeight ( parent , n ) : res = 0 for i in range ( n ) : p = i current = 1 while ( parent [ p ] != - 1 ) : current += 1 p = parent [ p ] res = max ( res , current ) return res
int findHeight ( int * parent , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int p = i , current = 1 ; while ( parent [ p ] != - 1 ) { current ++ ; p = parent [ p ] ; } res = max ( res , current ) ; } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findHeight ( parent , n ) : res = 0 for i in range ( n ) : p = i current = 1 while ( parent [ p ] != - 1 ) : current += 1 p = parent [ p ] res = max ( res , current ) return res </Code>
python-cpp-LEONARDO_NUMBER
python-cpp
def leonardo ( n ) : if ( n == 0 or n == 1 ) : return 1 return ( leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 )
int leonardo ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def leonardo ( n ) : if ( n == 0 or n == 1 ) : return 1 return ( leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ) </Code>
python-cpp-FIND_SIZE_OF_THE_LARGEST_FORMED_BY_ALL_ONES_IN_A_BINARY_MATRIX
python-cpp
def findLargestPlus ( mat ) : left = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] right = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] top = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] bottom = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] for i in range ( N ) : t...
int findLargestPlus ( int mat [ N ] [ N ] ) { int left [ N ] [ N ] , right [ N ] [ N ] , top [ N ] [ N ] , bottom [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { top [ 0 ] [ i ] = mat [ 0 ] [ i ] ; bottom [ N - 1 ] [ i ] = mat [ N - 1 ] [ i ] ; left [ i ] [ 0 ] = mat [ i ] [ 0 ] ; right [ i ] [...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findLargestPlus ( mat ) : left = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] right = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] top = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] bottom = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] for i in range ( N ) : ...
python-cpp-CENTER_ELEMENT_OF_MATRIX_EQUALS_SUMS_OF_HALF_DIAGONALS
python-cpp
def HalfDiagonalSums ( mat , n ) : diag1_left = 0 diag1_right = 0 diag2_left = 0 diag2_right = 0 i = 0 j = n - 1 while i < n : if ( i < n // 2 ) : diag1_left += mat [ i ] [ i ] diag2_left += mat [ j ] [ i ] elif ( i > n // 2 ) : diag1_right...
bool HalfDiagonalSums ( int mat [ ] [ MAX ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else if ( i >...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def HalfDiagonalSums ( mat , n ) : diag1_left = 0 diag1_right = 0 diag2_left = 0 diag2_right = 0 i = 0 j = n - 1 while i < n : if ( i < n // 2 ) : diag1_left += mat [ i ] [ i ] diag2_left += mat [ j ] [ i ] elif ( i > n // 2 ) : diag...
python-cpp-SUM_NODES_K_TH_LEVEL_TREE_REPRESENTED_STRING
python-cpp
def sumAtKthLevel ( tree , k ) : level = - 1 sum = 0 n = len ( tree ) for i in range ( n ) : if ( tree [ i ] == '(' ) : level += 1 elif ( tree [ i ] == ')' ) : level -= 1 else : if ( level == k ) : sum += ( ord ( tree [ i ] ) - ...
int sumAtKthLevel ( string tree , int k ) { int level = - 1 ; int sum = 0 ; int n = tree . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( tree [ i ] == '(' ) level ++ ; else if ( tree [ i ] == ')' ) level -- ; else { if ( level == k ) sum += ( tree [ i ] - '0' ) ; } } return ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumAtKthLevel ( tree , k ) : level = - 1 sum = 0 n = len ( tree ) for i in range ( n ) : if ( tree [ i ] == '(' ) : level += 1 elif ( tree [ i ] == ')' ) : level -= 1 else : if ( level == k ) : sum += ( ord ( tree [ i...
python-cpp-LEIBNIZ_HARMONIC_TRIANGLE
python-cpp
def LeibnizHarmonicTriangle ( n ) : C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 0 , n + 1 ) : for j in range ( 0 , min ( i , n ) + 1 ) : if ( j == 0 or j == i ) : C [ i ] [ j ] = 1 else : C [ i ] [ j ] = ( C [ i...
void LeibnizHarmonicTriangle ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } for ( int i = ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def LeibnizHarmonicTriangle ( n ) : C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 0 , n + 1 ) : for j in range ( 0 , min ( i , n ) + 1 ) : if ( j == 0 or j == i ) : C [ i ] [ j ] = 1 else : C [ i ] [ j ] = ...
python-cpp-WAYS_SPLIT_STRING_PARTITION_STARTS_DISTINCT_CHARACTER
python-cpp
def countWays ( s ) : count = [ 0 ] * 26 for x in s : count [ ord ( x ) - ord ( 'a' ) ] = ( count [ ord ( x ) - ord ( 'a' ) ] ) + 1 count [ ord ( s [ 0 ] ) - ord ( 'a' ) ] = 1 ans = 1 for i in range ( 26 ) : if ( count [ i ] != 0 ) : ans *= count [ i ] return ans
int countWays ( string s ) { int count [ 26 ] = { 0 } ; for ( char x : s ) count [ x - 'a' ] ++ ; count [ s [ 0 ] - 'a' ] = 1 ; int ans = 1 ; for ( int i = 0 ; i < 26 ; ++ i ) if ( count [ i ] != 0 ) ans *= count [ i ] ; return ans ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countWays ( s ) : count = [ 0 ] * 26 for x in s : count [ ord ( x ) - ord ( 'a' ) ] = ( count [ ord ( x ) - ord ( 'a' ) ] ) + 1 count [ ord ( s [ 0 ] ) - ord ( 'a' ) ] = 1 ans = 1 for i in range ( 26 ) : if ( count [ i ] != 0 ) : ans *= count [ i ] return a...
python-cpp-COUNT_NUMBER_WAYS_TILE_FLOOR_SIZE_N_X_M_USING_1_X_M_SIZE_TILES
python-cpp
def countWays ( n , m ) : count = [ ] for i in range ( n + 2 ) : count.append ( 0 ) count [ 0 ] = 0 for i in range ( 1 , n + 1 ) : if ( i > m ) : count [ i ] = count [ i - 1 ] + count [ i - m ] elif ( i < m ) : count [ i ] = 1 else : co...
int countWays ( int n , int m ) { int count [ n + 1 ] ; count [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countWays ( n , m ) : count = [ ] for i in range ( n + 2 ) : count.append ( 0 ) count [ 0 ] = 0 for i in range ( 1 , n + 1 ) : if ( i > m ) : count [ i ] = count [ i - 1 ] + count [ i - m ] elif ( i < m ) : count [ i ] = 1 else : ...
python-cpp-COMMON_ELEMENTS_IN_ALL_ROWS_OF_A_GIVEN_MATRIX
python-cpp
def printCommonElements ( mat ) : mp = dict ( ) for j in range ( N ) : mp [ mat [ 0 ] [ j ] ] = 1 for i in range ( 1 , M ) : for j in range ( N ) : if ( mat [ i ] [ j ] in mp.keys ( ) and mp [ mat [ i ] [ j ] ] == i ) : mp [ mat [ i ] [ j ] ] = i + 1 ...
void printCommonElements ( int mat [ M ] [ N ] ) { unordered_map < int , int > mp ; for ( int j = 0 ; j < N ; j ++ ) mp [ mat [ 0 ] [ j ] ] = 1 ; for ( int i = 1 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mp [ mat [ i ] [ j ] ] == i ) { mp [ mat [ i ] [ j ] ] = i ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printCommonElements ( mat ) : mp = dict ( ) for j in range ( N ) : mp [ mat [ 0 ] [ j ] ] = 1 for i in range ( 1 , M ) : for j in range ( N ) : if ( mat [ i ] [ j ] in mp.keys ( ) and mp [ mat [ i ] [ j ] ] == i ) : mp [ mat [ i ] [ j ] ] = i + 1 ...
python-cpp-FLOOR_IN_A_SORTED_ARRAY
python-cpp
def floorSearch ( arr , low , high , x ) : if ( low > high ) : return - 1 if ( x >= arr [ high ] ) : return high mid = int ( ( low + high ) / 2 ) if ( arr [ mid ] == x ) : return mid if ( mid > 0 and arr [ mid - 1 ] <= x and x < arr [ mid ] ) : return mid - 1 if (...
int floorSearch ( int arr [ ] , int n , int x ) { if ( x >= arr [ n - 1 ] ) return n - 1 ; if ( x < arr [ 0 ] ) return - 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > x ) return ( i - 1 ) ; return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def floorSearch ( arr , low , high , x ) : if ( low > high ) : return - 1 if ( x >= arr [ high ] ) : return high mid = int ( ( low + high ) / 2 ) if ( arr [ mid ] == x ) : return mid if ( mid > 0 and arr [ mid - 1 ] <= x and x < arr [ mid ] ) : return mid - 1 ...
python-cpp-SMALLEST_POWER_OF_2_GREATER_THAN_OR_EQUAL_TO_N_1
python-cpp
def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned int p = 1 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( p < n ) p <<= 1 ; return p ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextPowerOf2 ( n ) : p = 1 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( p < n ) : p <<= 1 return p </Code>
python-cpp-KTH_ADJACENT_NODE_GRAPH_VERTEX_WEIGHT
python-cpp
def printkthnode ( adj , wt , n , k ) : for i in range ( n ) : adj [ i ].sort ( ) for i in range ( n ) : if ( len ( adj [ i ] ) >= k ) : print ( adj [ i ] [ len ( adj [ i ] ) - k ] [ 1 ] , end = " " ) else : print ( "-1" , end = " " )
void printkthnode ( vector < pair < int , int >> adj [ ] , int wt [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) sort ( adj [ i ] . begin ( ) , adj [ i ] . end ( ) ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( adj [ i ] . size ( ) >= k ) cout << adj [ i ] [ adj [ i ] . size ( ) - k ] . second ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printkthnode ( adj , wt , n , k ) : for i in range ( n ) : adj [ i ].sort ( ) for i in range ( n ) : if ( len ( adj [ i ] ) >= k ) : print ( adj [ i ] [ len ( adj [ i ] ) - k ] [ 1 ] , end = " " ) else : print ( "-1" , end = " " ) </Code>
python-cpp-LONGEST_SUBARRAY_NOT_K_DISTINCT_ELEMENTS
python-cpp
def longest ( a , n , k ) : freq = [ 0 ] * n start = 0 end = 0 now = 0 l = 0 for i in range ( n ) : freq [ a [ i ] ] += 1 if ( freq [ a [ i ] ] == 1 ) : now += 1 while ( now > k ) : freq [ a [ l ] ] -= 1 if ( freq [ a [ l ] ] == 0 ) : ...
void longest ( int a [ ] , int n , int k ) { unordered_map < int , int > freq ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ] ] == ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longest ( a , n , k ) : freq = [ 0 ] * n start = 0 end = 0 now = 0 l = 0 for i in range ( n ) : freq [ a [ i ] ] += 1 if ( freq [ a [ i ] ] == 1 ) : now += 1 while ( now > k ) : freq [ a [ l ] ] -= 1 if ( freq [ a [ l ] ] == ...
python-cpp-MINIMUM_FLIP_REQUIRED_MAKE_BINARY_MATRIX_SYMMETRIC_1
python-cpp
def minimumflip ( mat , n ) : flip = 0 for i in range ( n ) : for j in range ( i ) : if mat [ i ] [ j ] != mat [ j ] [ i ] : flip += 1 return flip
int minimumflip ( int mat [ ] [ N ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minimumflip ( mat , n ) : flip = 0 for i in range ( n ) : for j in range ( i ) : if mat [ i ] [ j ] != mat [ j ] [ i ] : flip += 1 return flip </Code>
python-cpp-RECURSIVELY_BREAK_NUMBER_3_PARTS_GET_MAXIMUM_SUM
python-cpp
def breakSum ( n ) : if ( n == 0 or n == 1 ) : return n return max ( ( breakSum ( n // 2 ) + breakSum ( n // 3 ) + breakSum ( n // 4 ) ) , n )
int breakSum ( int n ) { if ( n == 0 || n == 1 ) return n ; return max ( ( breakSum ( n / 2 ) + breakSum ( n / 3 ) + breakSum ( n / 4 ) ) , n ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def breakSum ( n ) : if ( n == 0 or n == 1 ) : return n return max ( ( breakSum ( n // 2 ) + breakSum ( n // 3 ) + breakSum ( n // 4 ) ) , n ) </Code>
python-cpp-COUNT_PAIRS_TWO_SORTED_ARRAYS_WHOSE_SUM_EQUAL_GIVEN_VALUE_X
python-cpp
def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 for i in range ( m ) : for j in range ( n ) : if arr1 [ i ] + arr2 [ j ] == x : count = count + 1 return count
int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 for i in range ( m ) : for j in range ( n ) : if arr1 [ i ] + arr2 [ j ] == x : count = count + 1 return count </Code>
python-cpp-LONGEST_ALTERNATING_SUB_ARRAY_STARTING_EVERY_INDEX_BINARY_ARRAY
python-cpp
def alternateSubarray ( arr , n ) : len = [ ] for i in range ( n + 1 ) : len.append ( 0 ) len [ n - 1 ] = 1 for i in range ( n - 2 , - 1 , - 1 ) : if ( arr [ i ] ^ arr [ i + 1 ] == True ) : len [ i ] = len [ i + 1 ] + 1 else : len [ i ] = 1 for i in ra...
void alternateSubarray ( bool arr [ ] , int n ) { int len [ n ] ; len [ n - 1 ] = 1 ; for ( int i = n - 2 ; i >= 0 ; -- i ) { if ( arr [ i ] ^ arr [ i + 1 ] == 1 ) len [ i ] = len [ i + 1 ] + 1 ; else len [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) cout << len [ i ] << " " ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def alternateSubarray ( arr , n ) : len = [ ] for i in range ( n + 1 ) : len.append ( 0 ) len [ n - 1 ] = 1 for i in range ( n - 2 , - 1 , - 1 ) : if ( arr [ i ] ^ arr [ i + 1 ] == True ) : len [ i ] = len [ i + 1 ] + 1 else : len [ i ] = 1 for ...
python-cpp-LEXICOGRAPHICALLY_LARGEST_SUBSEQUENCE_EVERY_CHARACTER_OCCURS_LEAST_K_TIMES
python-cpp
def subsequence ( s , t , n , k ) : last = 0 cnt = 0 new_last = 0 size = 0 string = 'zyxwvutsrqponmlkjihgfedcba' for ch in string : cnt = 0 for i in range ( last , n ) : if s [ i ] == ch : cnt += 1 if cnt >= k : for i in range ( las...
void subsequence ( char s [ ] , char t [ ] , int n , int k ) { int last = 0 , cnt = 0 , new_last = 0 , size = 0 ; for ( char ch = 'z' ; ch >= 'a' ; ch -- ) { cnt = 0 ; for ( int i = last ; i < n ; i ++ ) { if ( s [ i ] == ch ) cnt ++ ; } if ( cnt >= k ) { for ( int i = last ;...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def subsequence ( s , t , n , k ) : last = 0 cnt = 0 new_last = 0 size = 0 string = 'zyxwvutsrqponmlkjihgfedcba' for ch in string : cnt = 0 for i in range ( last , n ) : if s [ i ] == ch : cnt += 1 if cnt >= k : for i in rang...
python-cpp-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM
python-cpp
def maxLen ( arr ) : max_len = 0 for i in range ( len ( arr ) ) : curr_sum = 0 for j in range ( i , len ( arr ) ) : curr_sum += arr [ j ] if curr_sum == 0 : max_len = max ( max_len , j - i + 1 ) return max_len
int maxLen ( int arr [ ] , int n ) { int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_sum += arr [ j ] ; if ( curr_sum == 0 ) max_len = max ( max_len , j - i + 1 ) ; } } return max_len ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxLen ( arr ) : max_len = 0 for i in range ( len ( arr ) ) : curr_sum = 0 for j in range ( i , len ( arr ) ) : curr_sum += arr [ j ] if curr_sum == 0 : max_len = max ( max_len , j - i + 1 ) return max_len </Code>
python-cpp-DICE_THROW_PROBLEM
python-cpp
def findWays ( m , n , x ) : table = [ [ 0 ] * ( x + 1 ) for i in range ( n + 1 ) ] for j in range ( 1 , min ( m + 1 , x + 1 ) ) : table [ 1 ] [ j ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( 1 , x + 1 ) : for k in range ( 1 , min ( m + 1 , j ) ) : table ...
int findWays ( int m , int n , int x ) { int table [ n + 1 ] [ x + 1 ] ; memset ( table , 0 , sizeof ( table ) ) ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) for ( int j = 1 ; j <= x ; j ++ ) for ( int k = 1 ; k <= m && k < j ; k ++ ) ta...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findWays ( m , n , x ) : table = [ [ 0 ] * ( x + 1 ) for i in range ( n + 1 ) ] for j in range ( 1 , min ( m + 1 , x + 1 ) ) : table [ 1 ] [ j ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( 1 , x + 1 ) : for k in range ( 1 , min ( m + 1 , j ) ) : ...
python-cpp-CHECK_HALF_STRING_CHARACTER_FREQUENCY_CHARACTER_1
python-cpp
def checkCorrectOrNot ( s ) : global MAX_CHAR count = [ 0 ] * MAX_CHAR n = len ( s ) if n == 1 : return true i = 0 j = n - 1 while i < j : count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1 count [ ord ( s [ j ] ) - ord ( 'a' ) ] -= 1 i += 1 j -= 1 for i in range ( ...
bool checkCorrectOrNot ( string s ) { int count [ MAX_CHAR ] = { 0 } ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s [ i ] - 'a' ] ++ ; count [ s [ j ] - 'a' ] -- ; } for ( int i = 0 ; i < MAX_CH...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkCorrectOrNot ( s ) : global MAX_CHAR count = [ 0 ] * MAX_CHAR n = len ( s ) if n == 1 : return true i = 0 j = n - 1 while i < j : count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1 count [ ord ( s [ j ] ) - ord ( 'a' ) ] -= 1 i += 1 j -= 1 for i in r...
python-cpp-FIND_NUMBER_OF_TRIANGLES_POSSIBLE
python-cpp
def findnumberofTriangles ( arr ) : n = len ( arr ) arr.sort ( ) count = 0 for i in range ( 0 , n - 2 ) : k = i + 2 for j in range ( i + 1 , n ) : while ( k < n and arr [ i ] + arr [ j ] > arr [ k ] ) : k += 1 if ( k > j ) : count +...
int findNumberOfTriangles ( int arr [ ] , int n ) { qsort ( arr , n , sizeof ( arr [ 0 ] ) , comp ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findnumberofTriangles ( arr ) : n = len ( arr ) arr.sort ( ) count = 0 for i in range ( 0 , n - 2 ) : k = i + 2 for j in range ( i + 1 , n ) : while ( k < n and arr [ i ] + arr [ j ] > arr [ k ] ) : k += 1 if ( k > j ) : ...
python-cpp-TILING_WITH_DOMINOES
python-cpp
def countWays ( n ) : A = [ 0 ] * ( n + 1 ) B = [ 0 ] * ( n + 1 ) A [ 0 ] = 1 A [ 1 ] = 0 B [ 0 ] = 0 B [ 1 ] = 1 for i in range ( 2 , n + 1 ) : A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] B [ i ] = A [ i - 1 ] + B [ i - 2 ] return A [ n ]
int countWays ( int n ) { int A [ n + 1 ] , B [ n + 1 ] ; A [ 0 ] = 1 , A [ 1 ] = 0 , B [ 0 ] = 0 , B [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ; B [ i ] = A [ i - 1 ] + B [ i - 2 ] ; } return A [ n ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countWays ( n ) : A = [ 0 ] * ( n + 1 ) B = [ 0 ] * ( n + 1 ) A [ 0 ] = 1 A [ 1 ] = 0 B [ 0 ] = 0 B [ 1 ] = 1 for i in range ( 2 , n + 1 ) : A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] B [ i ] = A [ i - 1 ] + B [ i - 2 ] return A [ n ] </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_4_LONGEST_COMMON_SUBSEQUENCE
python-cpp
def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; </Code>
python-cpp-MINIMIZE_SUM_PRODUCT_TWO_ARRAYS_PERMUTATIONS_ALLOWED
python-cpp
def minValue ( A , B , n ) : sorted ( A ) sorted ( B ) result = 0 for i in range ( n ) : result += ( A [ i ] * B [ n - i - 1 ] ) return result
int minValue ( int A [ ] , int B [ ] , int n ) { sort ( A , A + n ) ; sort ( B , B + n ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ; return result ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minValue ( A , B , n ) : sorted ( A ) sorted ( B ) result = 0 for i in range ( n ) : result += ( A [ i ] * B [ n - i - 1 ] ) return result </Code>
python-cpp-CHANGE_BITS_CAN_MADE_ONE_FLIP_1
python-cpp
def isOneFlip ( str ) : sum = 0 n = len ( str ) for i in range ( 0 , n ) : sum += int ( str [ i ] ) - int ( '0' ) return ( sum == n - 1 or sum == 1 )
bool isOneFlip ( string str ) { int sum = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) sum += str [ i ] - '0' ; return ( sum == n - 1 || sum == 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isOneFlip ( str ) : sum = 0 n = len ( str ) for i in range ( 0 , n ) : sum += int ( str [ i ] ) - int ( '0' ) return ( sum == n - 1 or sum == 1 ) </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_8_MATRIX_CHAIN_MULTIPLICATION
python-cpp
def MatrixChainOrder ( p , i , j ) : if i == j : return 0 _min = sys.maxsize for k in range ( i , j ) : count = ( MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ) if count < _min : _min = count return _min
int MatrixChainOrder ( int p [ ] , int i , int j ) { if ( i == j ) return 0 ; int k ; int min = INT_MAX ; int count ; for ( k = i ; k < j ; k ++ ) { count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ; if ( count < min ) min = count ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def MatrixChainOrder ( p , i , j ) : if i == j : return 0 _min = sys.maxsize for k in range ( i , j ) : count = ( MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ) if count < _min : _min = count return _min ...
python-cpp-MULTIPLY_TWO_NUMBERS_WITHOUT_USING_MULTIPLY_DIVISION_BITWISE_OPERATORS_AND_NO_LOOPS
python-cpp
def multiply ( x , y ) : if ( y == 0 ) : return 0 if ( y > 0 ) : return ( x + multiply ( x , y - 1 ) ) if ( y < 0 ) : return - multiply ( x , - y )
public : int multiply ( int x , int y ) { if ( y == 0 ) return 0 ; if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ; if ( y < 0 ) return - multiply ( x , - y ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def multiply ( x , y ) : if ( y == 0 ) : return 0 if ( y > 0 ) : return ( x + multiply ( x , y - 1 ) ) if ( y < 0 ) : return - multiply ( x , - y ) </Code>
python-cpp-COUNT_NUMBER_OF_WAYS_TO_PARTITION_A_SET_INTO_K_SUBSETS_1
python-cpp
def countP ( n , k ) : dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] for i in range ( n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( k + 1 ) : dp [ 0 ] [ k ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : if ( j == 1 or i == j ) : ...
int countP ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 ] [ k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= i ; j ++ ) if ( j == 1 || i == j ) dp [ i ] [ j ] = 1 ; else dp [ i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countP ( n , k ) : dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] for i in range ( n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( k + 1 ) : dp [ 0 ] [ k ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : if ( j == 1 or i ==...
python-cpp-PROGRAM_WORST_FIT_ALGORITHM_MEMORY_MANAGEMENT
python-cpp
def worstFit ( blockSize , m , processSize , n ) : allocation = [ - 1 ] * n for i in range ( n ) : wstIdx = - 1 for j in range ( m ) : if blockSize [ j ] >= processSize [ i ] : if wstIdx == - 1 : wstIdx = j elif blockSize [ wstIdx ]...
void worstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int wstIdx = - 1 ; for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def worstFit ( blockSize , m , processSize , n ) : allocation = [ - 1 ] * n for i in range ( n ) : wstIdx = - 1 for j in range ( m ) : if blockSize [ j ] >= processSize [ i ] : if wstIdx == - 1 : wstIdx = j elif blockSize [ w...
python-cpp-MAXIMIZE_VOLUME_CUBOID_GIVEN_SUM_SIDES_1
python-cpp
def maxvolume ( s ) : length = int ( s / 3 ) s -= length breadth = s / 2 height = s - breadth return int ( length * breadth * height )
int maxvolume ( int s ) { int length = s / 3 ; s -= length ; int breadth = s / 2 ; int height = s - breadth ; return length * breadth * height ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxvolume ( s ) : length = int ( s / 3 ) s -= length breadth = s / 2 height = s - breadth return int ( length * breadth * height ) </Code>
python-cpp-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_1
python-cpp
def findLength ( string ) : n = len ( string ) maxlen = 0 Sum = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( 0 , n ) : Sum [ i ] [ i ] = int ( string [ i ] ) for length in range ( 2 , n + 1 ) : for i in range ( 0 , n - length + 1 ) : j = i + length...
int findLength ( char * str ) { int n = strlen ( str ) ; int maxlen = 0 ; int sum [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) sum [ i ] [ i ] = str [ i ] - '0' ; for ( int len = 2 ; len <= n ; len ++ ) { for ( int i = 0 ; i < n - len + 1 ; i ++ ) { int j = i + len - 1 ; int ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findLength ( string ) : n = len ( string ) maxlen = 0 Sum = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] for i in range ( 0 , n ) : Sum [ i ] [ i ] = int ( string [ i ] ) for length in range ( 2 , n + 1 ) : for i in range ( 0 , n - length + 1 ) : j = i +...
python-cpp-FIND_SUBARRAY_WITH_GIVEN_SUM_1
python-cpp
def subArraySum ( arr , n , sum ) : curr_sum = arr [ 0 ] start = 0 i = 1 while i <= n : while curr_sum > sum and start < i - 1 : curr_sum = curr_sum - arr [ start ] start += 1 if curr_sum == sum : print ( "Sum found between indexes" ) print...
int subArraySum ( int arr [ ] , int n , int sum ) { int curr_sum = arr [ 0 ] , start = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { while ( curr_sum > sum && start < i - 1 ) { curr_sum = curr_sum - arr [ start ] ; start ++ ; } if ( curr_sum == sum ) { cout << "Sum found between indexes ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def subArraySum ( arr , n , sum ) : curr_sum = arr [ 0 ] start = 0 i = 1 while i <= n : while curr_sum > sum and start < i - 1 : curr_sum = curr_sum - arr [ start ] start += 1 if curr_sum == sum : print ( "Sum found between indexes" ) ...
python-cpp-COMMON_CHARACTERS_N_STRINGS
python-cpp
def commonCharacters ( strings , n ) : prim = [ True ] * MAX_CHAR for i in range ( n ) : sec = [ False ] * MAX_CHAR for j in range ( len ( strings [ i ] ) ) : if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ] ) : sec [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ]...
void commonCharacters ( string str [ ] , int n ) { bool prim [ MAX_CHAR ] ; memset ( prim , true , sizeof ( prim ) ) ; for ( int i = 0 ; i < n ; i ++ ) { bool sec [ MAX_CHAR ] = { false } ; for ( int j = 0 ; str [ i ] [ j ] ; j ++ ) { if ( prim [ str [ i ] [ j ] - 'a'...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def commonCharacters ( strings , n ) : prim = [ True ] * MAX_CHAR for i in range ( n ) : sec = [ False ] * MAX_CHAR for j in range ( len ( strings [ i ] ) ) : if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ] ) : sec [ ord ( strings [ i ] [ j ] ) - ord ( ...
python-cpp-CHECK_WHETHER_SECOND_STRING_CAN_FORMED_FIRST_STRING_USING_COUNT_ARRAY
python-cpp
def canMakeStr2 ( s1 , s2 ) : count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) } for i in range ( len ( s1 ) ) : count [ s1 [ i ] ] += 1 for i in range ( len ( s2 ) ) : if count [ s2 [ i ] ] == 0 : return False count [ s2 [ i ] ] -= 1 return True
bool canMakeStr2 ( string str1 , string str2 ) { int count [ MAX ] = { 0 } ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count [ str1 [ i ] ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { if ( count [ str2 [ i ] ] == 0 ) return false ; count [ str2 [ i ] ] --...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def canMakeStr2 ( s1 , s2 ) : count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) } for i in range ( len ( s1 ) ) : count [ s1 [ i ] ] += 1 for i in range ( len ( s2 ) ) : if count [ s2 [ i ] ] == 0 : return False count [ s2 [ i ] ] -= 1 return True </Code>
python-cpp-MAGIC_SQUARE
python-cpp
def generateSquare ( n ) : magicSquare = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] i = n / 2 j = n - 1 num = 1 while num <= ( n * n ) : if i == - 1 and j == n : j = n - 2 i = 0 else : if j == n : j = 0 if i < 0...
void generateSquare ( int n ) { int magicSquare [ n ] [ n ] ; memset ( magicSquare , 0 , sizeof ( magicSquare ) ) ; int i = n / 2 ; int j = n - 1 ; for ( int num = 1 ; num <= n * n ; ) { if ( i == - 1 && j == n ) { j = n - 2 ; i = 0 ; } else { if ( j == n ) j = 0 ; if (...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def generateSquare ( n ) : magicSquare = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] i = n / 2 j = n - 1 num = 1 while num <= ( n * n ) : if i == - 1 and j == n : j = n - 2 i = 0 else : if j == n : j = 0 i...
python-cpp-FIND_NUMBER_TIMES_STRING_OCCURS_GIVEN_STRING
python-cpp
def count ( a , b , m , n ) : if ( ( m == 0 and n == 0 ) or n == 0 ) : return 1 if ( m == 0 ) : return 0 if ( a [ m - 1 ] == b [ n - 1 ] ) : return ( count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ) else : return count ( a , b , m - 1 , n )
int count ( string a , string b , int m , int n ) { if ( ( m == 0 && n == 0 ) || n == 0 ) return 1 ; if ( m == 0 ) return 0 ; if ( a [ m - 1 ] == b [ n - 1 ] ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ; else return count ( a , b , m - 1 , n ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def count ( a , b , m , n ) : if ( ( m == 0 and n == 0 ) or n == 0 ) : return 1 if ( m == 0 ) : return 0 if ( a [ m - 1 ] == b [ n - 1 ] ) : return ( count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ) else : return count ( a , b , m - 1 , n ) </Code>
python-cpp-COUNT_ARRAYS_ADJACENT_ELEMENTS_ONE_DIVIDE_ANOTHER
python-cpp
def numofArray ( n , m ) : dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] di = [ [ ] for i in range ( MAX ) ] mu = [ [ ] for i in range ( MAX ) ] for i in range ( 1 , m + 1 ) : for j in range ( 2 * i , m + 1 , i ) : di [ j ].append ( i ) mu [ i ].append ( j ...
int numofArray ( int n , int m ) { int dp [ MAX ] [ MAX ] ; vector < int > di [ MAX ] ; vector < int > mu [ MAX ] ; memset ( dp , 0 , sizeof dp ) ; memset ( di , 0 , sizeof di ) ; memset ( mu , 0 , sizeof mu ) ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j += i ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def numofArray ( n , m ) : dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] di = [ [ ] for i in range ( MAX ) ] mu = [ [ ] for i in range ( MAX ) ] for i in range ( 1 , m + 1 ) : for j in range ( 2 * i , m + 1 , i ) : di [ j ].append ( i ) mu [ i ].appe...
python-cpp-COUNT_1S_SORTED_BINARY_ARRAY
python-cpp
def countOnes ( arr , low , high ) : if high >= low : mid = low + ( high - low ) / 2 if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) : return mid + 1 if arr [ mid ] == 1 : return countOnes ( arr , ( mid + 1 ) , high ) return countOnes (...
int countOnes ( bool arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr , low ,...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countOnes ( arr , low , high ) : if high >= low : mid = low + ( high - low ) / 2 if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) : return mid + 1 if arr [ mid ] == 1 : return countOnes ( arr , ( mid + 1 ) , high ) return coun...
python-cpp-CEILING_IN_A_SORTED_ARRAY_1
python-cpp
def ceilSearch ( arr , low , high , x ) : if x <= arr [ low ] : return low if x > arr [ high ] : return - 1 mid = ( low + high ) / 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : if mid + 1 <= high and x <= arr [ mid + 1 ] : return mid + 1 ...
int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1 ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def ceilSearch ( arr , low , high , x ) : if x <= arr [ low ] : return low if x > arr [ high ] : return - 1 mid = ( low + high ) / 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : if mid + 1 <= high and x <= arr [ mid + 1 ] : return mid + 1...
python-cpp-DECIMAL_BINARY_CONVERSION_WITHOUT_USING_ARITHMETIC_OPERATORS
python-cpp
def decToBin ( n ) : if ( n == 0 ) : return "0" bin = "" while ( n > 0 ) : if ( n & 1 == 0 ) : bin = '0' + bin else : bin = '1' + bin n = n >> 1 return bin
string decToBin ( int n ) { if ( n == 0 ) return "0" ; string bin = "" ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def decToBin ( n ) : if ( n == 0 ) : return "0" bin = "" while ( n > 0 ) : if ( n & 1 == 0 ) : bin = '0' + bin else : bin = '1' + bin n = n >> 1 return bin </Code>
python-cpp-HOW_WILL_YOU_PRINT_NUMBERS_FROM_1_TO_200_WITHOUT_USING_LOOP
python-cpp
def printNos ( n ) : if n > 0 : printNos ( n - 1 ) print ( n , end = ' ' )
public : void printNos ( unsigned int n ) { if ( n > 0 ) { printNos ( n - 1 ) ; cout << n << " " ; } return ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printNos ( n ) : if n > 0 : printNos ( n - 1 ) print ( n , end = ' ' ) </Code>
python-cpp-PREFIX_SUM_2D_ARRAY
python-cpp
def prefixSum2D ( a ) : global C , R psa = [ [ 0 for x in range ( C ) ] for y in range ( R ) ] psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] for i in range ( 1 , C ) : psa [ 0 ] [ i ] = ( psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ) for i in range ( 0 , R ) : psa [ i ] [ 0 ] = ( psa [ i - 1 ] [ 0 ] + a [...
void prefixSum2D ( int a [ ] [ C ] ) { int psa [ R ] [ C ] ; psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] ; for ( int i = 1 ; i < C ; i ++ ) psa [ 0 ] [ i ] = psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ; for ( int i = 0 ; i < R ; i ++ ) psa [ i ] [ 0 ] = psa [ i - 1 ] [ 0 ] + a [ i ] [ 0 ] ; for ( int i = 1 ; i < R ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def prefixSum2D ( a ) : global C , R psa = [ [ 0 for x in range ( C ) ] for y in range ( R ) ] psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] for i in range ( 1 , C ) : psa [ 0 ] [ i ] = ( psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ) for i in range ( 0 , R ) : psa [ i ] [ 0 ] = ( psa [ i - 1 ] [ 0 ...
python-cpp-EULERIAN_NUMBER
python-cpp
def eulerian ( n , m ) : if ( m >= n or n == 0 ) : return 0 if ( m == 0 ) : return 1 return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) )
int eulerian ( int n , int m ) { if ( m >= n || n == 0 ) return 0 ; if ( m == 0 ) return 1 ; return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def eulerian ( n , m ) : if ( m >= n or n == 0 ) : return 0 if ( m == 0 ) : return 1 return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ) </Code>
python-cpp-FIND_SUM_UNIQUE_SUB_ARRAY_SUM_GIVEN_ARRAY
python-cpp
def findSubarraySum ( arr , n ) : res = 0 m = dict ( ) for i in range ( n ) : Sum = 0 for j in range ( i , n ) : Sum += arr [ j ] m [ Sum ] = m.get ( Sum , 0 ) + 1 for x in m : if m [ x ] == 1 : res += x return res
long long int findSubarraySum ( int arr [ ] , int n ) { int res = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) { int sum = 0 ; for ( int j = i ; j < n ; j ++ ) { sum += arr [ j ] ; m [ sum ] ++ ; } } for ( auto x : m ) if ( x . second == 1 ) res += x...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findSubarraySum ( arr , n ) : res = 0 m = dict ( ) for i in range ( n ) : Sum = 0 for j in range ( i , n ) : Sum += arr [ j ] m [ Sum ] = m.get ( Sum , 0 ) + 1 for x in m : if m [ x ] == 1 : res += x return res </Code>
python-cpp-WRITE_AN_EFFICIENT_METHOD_TO_CHECK_IF_A_NUMBER_IS_MULTIPLE_OF_3
python-cpp
def isMultipleOf3 ( n ) : odd_count = 0 even_count = 0 if ( n < 0 ) : n = - n if ( n == 0 ) : return 1 if ( n == 1 ) : return 0 while ( n ) : if ( n & 1 ) : odd_count += 1 if ( n & 2 ) : even_count += 1 n = n >> 2 return...
int isMultipleOf3 ( int n ) { int odd_count = 0 ; int even_count = 0 ; if ( n < 0 ) n = - n ; if ( n == 0 ) return 1 ; if ( n == 1 ) return 0 ; while ( n ) { if ( n & 1 ) odd_count ++ ; if ( n & 2 ) even_count ++ ; n = n >> 2 ; } return isMultipleOf3 ( abs ( odd_count - even_count ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isMultipleOf3 ( n ) : odd_count = 0 even_count = 0 if ( n < 0 ) : n = - n if ( n == 0 ) : return 1 if ( n == 1 ) : return 0 while ( n ) : if ( n & 1 ) : odd_count += 1 if ( n & 2 ) : even_count += 1 n = n >> 2 ...
python-cpp-LCS_FORMED_CONSECUTIVE_SEGMENTS_LEAST_LENGTH_K
python-cpp
def longestSubsequenceCommonSegment ( k , s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : for j in range ( 1 , m + 1 ) : lc...
int longestSubsequenceCommonSegment ( int k , string s1 , string s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; int lcs [ n + 1 ] [ m + 1 ] ; int cnt [ n + 1 ] [ m + 1 ] ; memset ( lcs , 0 , sizeof ( lcs ) ) ; memset ( cnt , 0 , sizeof ( cnt ) ) ; for ( int i = 1 ; i <= n ; i ++ ) { fo...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longestSubsequenceCommonSegment ( k , s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : for j in range ( 1 , m + 1 ) : ...
python-cpp-PROGRAM_AREA_SQUARE
python-cpp
def areaSquare ( side ) : area = side * side return area
int areaSquare ( int side ) { int area = side * side ; return area ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def areaSquare ( side ) : area = side * side return area </Code>
python-cpp-EULERIAN_PATH_UNDIRECTED_GRAPH
python-cpp
def findpath ( graph ) : n = len ( graph ) numofadj = list ( ) for i in range ( n ) : numofadj.append ( sum ( graph [ i ] ) ) startpoint = 0 numofodd = 0 for i in range ( n - 1 , - 1 , - 1 ) : if ( numofadj [ i ] % 2 == 1 ) : numofodd += 1 startpoint = i ...
void findpath ( int graph [ ] [ 5 ] , int n ) { vector < int > numofadj ; for ( int i = 0 ; i < n ; i ++ ) numofadj . push_back ( accumulate ( graph [ i ] , graph [ i ] + 5 , 0 ) ) ; int startpoint = 0 , numofodd = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( numofadj [ i ] % 2 == 1 ) { nu...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findpath ( graph ) : n = len ( graph ) numofadj = list ( ) for i in range ( n ) : numofadj.append ( sum ( graph [ i ] ) ) startpoint = 0 numofodd = 0 for i in range ( n - 1 , - 1 , - 1 ) : if ( numofadj [ i ] % 2 == 1 ) : numofodd += 1 startpoin...
python-cpp-LCS_LONGEST_COMMON_SUBSEQUENCE_THREE_STRINGS_1
python-cpp
def lcsOf3 ( i , j , k ) : if ( i == - 1 or j == - 1 or k == - 1 ) : return 0 if ( dp [ i ] [ j ] [ k ] != - 1 ) : return dp [ i ] [ j ] [ k ] if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) : dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) return dp [ i ] [ j ] ...
int lcsOf3 ( int i , int j , int k ) { if ( i == - 1 || j == - 1 || k == - 1 ) return 0 ; if ( dp [ i ] [ j ] [ k ] != - 1 ) return dp [ i ] [ j ] [ k ] ; if ( X [ i ] == Y [ j ] && Y [ j ] == Z [ k ] ) return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) ; else return dp [ i ] [ j ] [ k ] = max (...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lcsOf3 ( i , j , k ) : if ( i == - 1 or j == - 1 or k == - 1 ) : return 0 if ( dp [ i ] [ j ] [ k ] != - 1 ) : return dp [ i ] [ j ] [ k ] if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) : dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 ) return dp [ i ]...
python-cpp-FIND_ELEMENTS_LARGER_HALF_ELEMENTS_ARRAY
python-cpp
def findLarger ( arr , n ) : x = sorted ( arr ) for i in range ( n / 2 , n ) : print ( x [ i ] ) ,
void findLarger ( int arr [ ] , int n ) { sort ( arr , arr + n ) ; for ( int i = n - 1 ; i >= n / 2 ; i -- ) cout << arr [ i ] << " " ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findLarger ( arr , n ) : x = sorted ( arr ) for i in range ( n / 2 , n ) : print ( x [ i ] ) , </Code>
python-cpp-SUM_DIAGONALS_SPIRAL_ODD_ORDER_SQUARE_MATRIX
python-cpp
def spiralDiaSum ( n ) : if n == 1 : return 1 return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) )
int spiralDiaSum ( int n ) { if ( n == 1 ) return 1 ; return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def spiralDiaSum ( n ) : if n == 1 : return 1 return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) </Code>
python-cpp-COUNT_NUMBER_OF_OCCURRENCES_OR_FREQUENCY_IN_A_SORTED_ARRAY
python-cpp
def countOccurrences ( arr , n , x ) : res = 0 for i in range ( n ) : if x == arr [ i ] : res += 1 return res
int countOccurrences ( int arr [ ] , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countOccurrences ( arr , n , x ) : res = 0 for i in range ( n ) : if x == arr [ i ] : res += 1 return res </Code>
python-cpp-SUM_ELEMENTS_K1TH_K2TH_SMALLEST_ELEMENTS
python-cpp
def sumBetweenTwoKth ( arr , n , k1 , k2 ) : arr.sort ( ) result = 0 for i in range ( k1 , k2 - 1 ) : result += arr [ i ] return result
int sumBetweenTwoKth ( int arr [ ] , int n , int k1 , int k2 ) { sort ( arr , arr + n ) ; return accumulate ( arr + k1 , arr + k2 - 1 , 0 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumBetweenTwoKth ( arr , n , k1 , k2 ) : arr.sort ( ) result = 0 for i in range ( k1 , k2 - 1 ) : result += arr [ i ] return result </Code>
python-cpp-FIND_NUMBER_PAIRS_ARRAY_XOR_0_1
python-cpp
def calculate ( a ) : maximum = max ( a ) frequency = [ 0 for x in range ( maximum + 1 ) ] for i in a : frequency [ i ] += 1 answer = 0 for i in frequency : answer = answer + i * ( i - 1 ) // 2 return answer
int calculate ( int a [ ] , int n ) { int * maximum = max_element ( a , a + 5 ) ; int frequency [ * maximum + 1 ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { frequency [ a [ i ] ] += 1 ; } int answer = 0 ; for ( int i = 0 ; i < ( * maximum ) + 1 ; i ++ ) { answer...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def calculate ( a ) : maximum = max ( a ) frequency = [ 0 for x in range ( maximum + 1 ) ] for i in a : frequency [ i ] += 1 answer = 0 for i in frequency : answer = answer + i * ( i - 1 ) // 2 return answer </Code>
python-cpp-DOUBLE_FACTORIAL_1
python-cpp
def doublefactorial ( n ) : res = 1 for i in range ( n , - 1 , - 2 ) : if ( i == 0 or i == 1 ) : return res else : res *= i
unsigned int doublefactorial ( unsigned int n ) { int res = 1 ; for ( int i = n ; i >= 0 ; i = i - 2 ) { if ( i == 0 || i == 1 ) return res ; else res *= i ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def doublefactorial ( n ) : res = 1 for i in range ( n , - 1 , - 2 ) : if ( i == 0 or i == 1 ) : return res else : res *= i </Code>
python-cpp-LONGEST_COMMON_SUBSEQUENCE
python-cpp
def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
int lcs ( char * X , char * Y , int m , int n ) { if ( m == 0 || n == 0 ) return 0 ; if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lcs ( X , Y , m , n ) : if m == 0 or n == 0 : return 0 ; elif X [ m - 1 ] == Y [ n - 1 ] : return 1 + lcs ( X , Y , m - 1 , n - 1 ) ; else : return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ; </Code>
python-cpp-MAXIMUM_PRODUCT_SUBSET_ARRAY
python-cpp
def maxProductSubset ( a , n ) : if n == 1 : return a [ 0 ] max_neg = - 999999999999 count_neg = 0 count_zero = 0 prod = 1 for i in range ( n ) : if a [ i ] == 0 : count_zero += 1 continue if a [ i ] < 0 : count_neg += 1 max...
int maxProductSubset ( int a [ ] , int n ) { if ( n == 1 ) return a [ 0 ] ; int max_neg = INT_MIN ; 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 ) { count_neg ++ ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxProductSubset ( a , n ) : if n == 1 : return a [ 0 ] max_neg = - 999999999999 count_neg = 0 count_zero = 0 prod = 1 for i in range ( n ) : if a [ i ] == 0 : count_zero += 1 continue if a [ i ] < 0 : count_neg += 1 ...
python-cpp-TEMPLE_OFFERINGS
python-cpp
def offeringNumber ( n , templeHeight ) : sum = 0 for i in range ( n ) : left = 0 right = 0 for j in range ( i - 1 , - 1 , - 1 ) : if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) : left += 1 else : break for j in range ( ...
int offeringNumber ( int n , int templeHeight [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int left = 0 , right = 0 ; for ( int j = i - 1 ; j >= 0 ; -- j ) { if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ; else break ; } for ( int j = i + 1 ; j < ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def offeringNumber ( n , templeHeight ) : sum = 0 for i in range ( n ) : left = 0 right = 0 for j in range ( i - 1 , - 1 , - 1 ) : if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) : left += 1 else : break for j in r...
python-cpp-FIND_SUM_NODES_GIVEN_PERFECT_BINARY_TREE
python-cpp
def SumNodes ( l ) : leafNodeCount = pow ( 2 , l - 1 ) vec = [ [ ] for i in range ( l ) ] for i in range ( 1 , leafNodeCount + 1 ) : vec [ l - 1 ].append ( i ) for i in range ( l - 2 , - 1 , - 1 ) : k = 0 while ( k < len ( vec [ i + 1 ] ) - 1 ) : vec [ i ].append ( ve...
int sumNodes ( int l ) { int leafNodeCount = pow ( 2 , l - 1 ) ; vector < int > vec [ l ] ; for ( int i = 1 ; i <= leafNodeCount ; i ++ ) vec [ l - 1 ] . push_back ( i ) ; for ( int i = l - 2 ; i >= 0 ; i -- ) { int k = 0 ; while ( k < vec [ i + 1 ] . size ( ) - 1 ) { vec [ i ] . push_back...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def SumNodes ( l ) : leafNodeCount = pow ( 2 , l - 1 ) vec = [ [ ] for i in range ( l ) ] for i in range ( 1 , leafNodeCount + 1 ) : vec [ l - 1 ].append ( i ) for i in range ( l - 2 , - 1 , - 1 ) : k = 0 while ( k < len ( vec [ i + 1 ] ) - 1 ) : vec [ i ].appe...
python-cpp-CONSTRUCT_LEXICOGRAPHICALLY_SMALLEST_PALINDROME
python-cpp
def constructPalin ( string , l ) : string = list ( string ) i = - 1 j = l while i < j : i += 1 j -= 1 if ( string [ i ] == string [ j ] and string [ i ] != '*' ) : continue elif ( string [ i ] == string [ j ] and string [ i ] == '*' ) : string [ i...
string constructPalin ( string str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != '*' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == '*' ) { str [ i ] = 'a' ; str [ j ] = 'a' ; continue ; } else...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def constructPalin ( string , l ) : string = list ( string ) i = - 1 j = l while i < j : i += 1 j -= 1 if ( string [ i ] == string [ j ] and string [ i ] != '*' ) : continue elif ( string [ i ] == string [ j ] and string [ i ] == '*' ) : str...
python-cpp-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY
python-cpp
def countPairs ( arr , n ) : ans = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : ans += 1 return ans
int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPairs ( arr , n ) : ans = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : ans += 1 return ans </Code>
python-cpp-FAST_MULTIPLICATION_METHOD_WITHOUT_USING_MULTIPLICATION_OPERATOR_RUSSIAN_PEASANTS_ALGORITHM
python-cpp
def russianPeasant ( a , b ) : res = 0 while ( b > 0 ) : if ( b & 1 ) : res = res + a a = a << 1 b = b >> 1 return res
unsigned int russianPeasant ( unsigned int a , unsigned int b ) { int res = 0 ; while ( b > 0 ) { if ( b & 1 ) res = res + a ; a = a << 1 ; b = b >> 1 ; } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def russianPeasant ( a , b ) : res = 0 while ( b > 0 ) : if ( b & 1 ) : res = res + a a = a << 1 b = b >> 1 return res </Code>
python-cpp-CAESAR_CIPHER
python-cpp
def encrypt ( text , s ) : result = "" for i in range ( len ( text ) ) : char = text [ i ] if ( char.isupper ( ) ) : result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 ) else : result += chr ( ( ord ( char ) + s - 97 ) % 26 + 97 ) return result
string encrypt ( string text , int s ) { string result = "" ; for ( int i = 0 ; i < text . length ( ) ; i ++ ) { if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ) ; else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ) ; } return result ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def encrypt ( text , s ) : result = "" for i in range ( len ( text ) ) : char = text [ i ] if ( char.isupper ( ) ) : result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 ) else : result += chr ( ( ord ( char ) + s - 97 ) % 26 + 97 ) return result </Code...
python-cpp-PROGRAM_DISTANCE_TWO_POINTS_EARTH
python-cpp
def distance ( lat1 , lat2 , lon1 , lon2 ) : lon1 = radians ( lon1 ) lon2 = radians ( lon2 ) lat1 = radians ( lat1 ) lat2 = radians ( lat2 ) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2 c = 2 * asin ( sqrt ( a ) ) ...
long double distance ( long double lat1 , long double long1 , long double lat2 , long double long2 ) { lat1 = toRadians ( lat1 ) ; long1 = toRadians ( long1 ) ; lat2 = toRadians ( lat2 ) ; long2 = toRadians ( long2 ) ; long double dlong = long2 - long1 ; long double dlat = lat2 - lat1 ; long double ans = ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def distance ( lat1 , lat2 , lon1 , lon2 ) : lon1 = radians ( lon1 ) lon2 = radians ( lon2 ) lat1 = radians ( lat1 ) lat2 = radians ( lat2 ) dlon = lon2 - lon1 dlat = lat2 - lat1 a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2 c = 2 * asin ( sqrt ( ...
python-cpp-NTH_EVEN_FIBONACCI_NUMBER
python-cpp
def evenFib ( n ) : if ( n < 1 ) : return n if ( n == 1 ) : return 2 return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) )
long int evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def evenFib ( n ) : if ( n < 1 ) : return n if ( n == 1 ) : return 2 return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) </Code>
python-cpp-STEINS_ALGORITHM_FOR_FINDING_GCD
python-cpp
def gcd ( a , b ) : if ( a == 0 ) : return b if ( b == 0 ) : return a k = 0 while ( ( ( a
int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def gcd ( a , b ) : if ( a == 0 ) : return b if ( b == 0 ) : return a k = 0 while ( ( ( a </Code>
python-cpp-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO
python-cpp
def isPowerOfTwo ( n ) : if ( n == 0 ) : return False while ( n != 1 ) : if ( n % 2 != 0 ) : return False n = n // 2 return True
bool isPowerOfTwo ( int n ) { if ( n == 0 ) return 0 ; while ( n != 1 ) { if ( n % 2 != 0 ) return 0 ; n = n / 2 ; } return 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPowerOfTwo ( n ) : if ( n == 0 ) : return False while ( n != 1 ) : if ( n % 2 != 0 ) : return False n = n // 2 return True </Code>
python-cpp-SUM_PAIRWISE_PRODUCTS
python-cpp
def findSum ( n ) : sm = 0 for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : sm = sm + i * j return sm
long long int findSum ( int n ) { long long int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = i ; j <= n ; j ++ ) sum = sum + 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 ) : sm = 0 for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : sm = sm + i * j return sm </Code>
python-cpp-SUM_BINOMIAL_COEFFICIENTS_1
python-cpp
def binomialCoeffSum ( n ) : return ( 1 << n )
int binomialCoeffSum ( int n ) { return ( 1 << n ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binomialCoeffSum ( n ) : return ( 1 << n ) </Code>