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-SHIFT_MATRIX_ELEMENTS_K | python-cpp | def shiftMatrixByK ( mat , k ) :
if ( k > N ) :
print ( "shifting is"" not possible" )
return
j = 0
while ( j < N ) :
for i in range ( k , N ) :
print ( "{} ".format ( mat [ j ] [ i ] ) , end = "" )
for i in range ( 0 , k ) :
print ( "{} ".format ( mat... | void shiftMatrixByK ( int mat [ N ] [ N ] , int k ) {
if ( k > N ) {
cout << "shifting is not possible" << endl ;
return ;
}
int j = 0 ;
while ( j < N ) {
for ( int i = k ;
i < N ;
i ++ ) cout << mat [ j ] [ i ] << " " ;
for ( int i = 0 ;
i < k ;
i ++ ) cout << mat [ j ] [ i ] <<... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def shiftMatrixByK ( mat , k ) :
if ( k > N ) :
print ( "shifting is"" not possible" )
return
j = 0
while ( j < N ) :
for i in range ( k , N ) :
print ( "{} ".format ( mat [ j ] [ i ] ) , end = "" )
for i in range ( 0 , k ) :
print ( "{} ".forma... |
python-cpp-SORTING_USING_TRIVIAL_HASH_FUNCTION_1 | python-cpp | def sortUsingHash ( a , n ) :
Max = max ( a )
Min = abs ( min ( a ) )
hashpos = [ 0 ] * ( Max + 1 )
hashneg = [ 0 ] * ( Min + 1 )
for i in range ( 0 , n ) :
if a [ i ] >= 0 :
hashpos [ a [ i ] ] += 1
else :
hashneg [ abs ( a [ i ] ) ] += 1
for i in range (... | void sortUsingHash ( int a [ ] , int n ) {
int max = * std :: max_element ( a , a + n ) ;
int min = abs ( * std :: min_element ( a , a + n ) ) ;
int hashpos [ max + 1 ] = {
0 }
;
int hashneg [ min + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] >= ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortUsingHash ( a , n ) :
Max = max ( a )
Min = abs ( min ( a ) )
hashpos = [ 0 ] * ( Max + 1 )
hashneg = [ 0 ] * ( Min + 1 )
for i in range ( 0 , n ) :
if a [ i ] >= 0 :
hashpos [ a [ i ] ] += 1
else :
hashneg [ abs ( a [ i ] ) ] += 1
for i in ... |
python-cpp-FIND_PAIRS_IN_ARRAY_WHOSE_SUMS_ALREADY_EXIST_IN_ARRAY | python-cpp | def findPair ( arr , n ) :
found = False
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
for k in range ( 0 , n ) :
if ( arr [ i ] + arr [ j ] == arr [ k ] ) :
print ( arr [ i ] , arr [ j ] )
found = True
if ( found == Fal... | void findPair ( int arr [ ] , int n ) {
bool found = false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
for ( int k = 0 ;
k < n ;
k ++ ) {
if ( arr [ i ] + arr [ j ] == arr [ k ] ) {
cout << arr [ i ] << " " << arr [ j ] << endl ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findPair ( arr , n ) :
found = False
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
for k in range ( 0 , n ) :
if ( arr [ i ] + arr [ j ] == arr [ k ] ) :
print ( arr [ i ] , arr [ j ] )
found = True
if ( found... |
python-cpp-FIND_PAIR_MAXIMUM_GCD_ARRAY_1 | python-cpp | def findMaxGCD ( arr , n ) :
high = 0
for i in range ( 0 , n ) :
high = max ( high , arr [ i ] )
count = [ 0 ] * ( high + 1 )
for i in range ( 0 , n ) :
count [ arr [ i ] ] += 1
counter = 0
for i in range ( high , 0 , - 1 ) :
j = i
while ( j <= high ) :
... | int findMaxGCD ( int arr [ ] , int n ) {
int high = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) high = max ( high , arr [ i ] ) ;
int count [ high + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ arr [ i ] ] ++ ;
int counter = 0 ;
for ( int i = high ;
i >= 1 ;
i -- ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMaxGCD ( arr , n ) :
high = 0
for i in range ( 0 , n ) :
high = max ( high , arr [ i ] )
count = [ 0 ] * ( high + 1 )
for i in range ( 0 , n ) :
count [ arr [ i ] ] += 1
counter = 0
for i in range ( high , 0 , - 1 ) :
j = i
while ( j <= high ) :
... |
python-cpp-EULERS_CRITERION_CHECK_IF_SQUARE_ROOT_UNDER_MODULO_P_EXISTS | python-cpp | def squareRootExists ( n , p ) :
n = n % p
for x in range ( 2 , p , 1 ) :
if ( ( x * x ) % p == n ) :
return True
return False
| bool squareRootExists ( int n , int p ) {
n = n % p ;
for ( int x = 2 ;
x < p ;
x ++ ) if ( ( x * x ) % p == n ) return true ;
return false ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def squareRootExists ( n , p ) :
n = n % p
for x in range ( 2 , p , 1 ) :
if ( ( x * x ) % p == n ) :
return True
return False
</Code> |
python-cpp-COUNT_ENTRIES_EQUAL_TO_X_IN_A_SPECIAL_MATRIX | python-cpp | def count ( n , x ) :
cnt = 0
for i in range ( 1 , n + 1 ) :
if i <= x :
if x // i <= n and x % i == 0 :
cnt += 1
return cnt
| int count ( int n , int x ) {
int count = = 0 ;
for ( int i = 1 ;
i <= n && i <= x ;
i ++ ) {
if ( x / i <= n && x % i == 0 ) count ++ ;
}
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def count ( n , x ) :
cnt = 0
for i in range ( 1 , n + 1 ) :
if i <= x :
if x // i <= n and x % i == 0 :
cnt += 1
return cnt
</Code> |
python-cpp-SIZE_SUBARRAY_MAXIMUM_SUM | python-cpp | def maxSubArraySum ( a , size ) :
max_so_far = - maxsize - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range ( 0 , size ) :
max_ending_here += a [ i ]
if max_so_far < max_ending_here :
max_so_far = max_ending_here
start = s
end = i
... | int maxSubArraySum ( int a [ ] , int size ) {
int max_so_far = INT_MIN , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ;
for ( int i = 0 ;
i < size ;
i ++ ) {
max_ending_here += a [ i ] ;
if ( max_so_far < max_ending_here ) {
max_so_far = max_ending_here ;
start = s ;
end = i ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSubArraySum ( a , size ) :
max_so_far = - maxsize - 1
max_ending_here = 0
start = 0
end = 0
s = 0
for i in range ( 0 , size ) :
max_ending_here += a [ i ]
if max_so_far < max_ending_here :
max_so_far = max_ending_here
start = s
en... |
python-cpp-SMALLEST_SUBARRAY_WITH_ALL_OCCURRENCES_OF_A_MOST_FREQUENT_ELEMENT | python-cpp | def smallestSubsegment ( a , n ) :
left = dict ( )
count = dict ( )
mx = 0
mn , strindex = 0 , 0
for i in range ( n ) :
x = a [ i ]
if ( x not in count.keys ( ) ) :
left [ x ] = i
count [ x ] = 1
else :
count [ x ] += 1
if ( count [... | void smallestSubsegment ( int a [ ] , int n ) {
unordered_map < int , int > left ;
unordered_map < int , int > count ;
int mx = 0 ;
int mn , strindex ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
if ( count [ x ] == 0 ) {
left [ x ] = i ;
count [ x ] = 1 ;
}
else count... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def smallestSubsegment ( a , n ) :
left = dict ( )
count = dict ( )
mx = 0
mn , strindex = 0 , 0
for i in range ( n ) :
x = a [ i ]
if ( x not in count.keys ( ) ) :
left [ x ] = i
count [ x ] = 1
else :
count [ x ] += 1
if ( ... |
python-cpp-FIND_INDEX_MAXIMUM_OCCURRING_ELEMENT_EQUAL_PROBABILITY | python-cpp | def findRandomIndexOfMax ( arr , n ) :
mp = dict ( )
for i in range ( n ) :
if ( arr [ i ] in mp ) :
mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1
else :
mp [ arr [ i ] ] = 1
max_element = - 323567
max_so_far = - 323567
for p in mp :
if ( mp [ p ] > max_so_f... | void findRandomIndexOfMax ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] += 1 ;
int max_element ;
int max_so_far = INT_MIN ;
for ( pair < int , int > p : freq ) {
if ( p . second > max_so_far ) {
max_so_far = p . second ;
ma... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findRandomIndexOfMax ( arr , n ) :
mp = dict ( )
for i in range ( n ) :
if ( arr [ i ] in mp ) :
mp [ arr [ i ] ] = mp [ arr [ i ] ] + 1
else :
mp [ arr [ i ] ] = 1
max_element = - 323567
max_so_far = - 323567
for p in mp :
if ( mp [ p ] > m... |
python-cpp-FIND_MINIMUM_RADIUS_ATLEAST_K_POINT_LIE_INSIDE_CIRCLE | python-cpp | def minRadius ( k , x , y , n ) :
dis = [ 0 ] * n
for i in range ( 0 , n ) :
dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ]
dis.sort ( )
return dis [ k - 1 ]
| int minRadius ( int k , int x [ ] , int y [ ] , int n ) {
int dis [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ;
sort ( dis , dis + n ) ;
return dis [ k - 1 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minRadius ( k , x , y , n ) :
dis = [ 0 ] * n
for i in range ( 0 , n ) :
dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ]
dis.sort ( )
return dis [ k - 1 ]
</Code> |
python-cpp-FIND_THE_ELEMENT_THAT_APPEARS_ONCE_2 | python-cpp | def singleNumber ( nums ) :
return ( 3 * sum ( set ( nums ) ) - sum ( nums ) ) / 2
| int singleNumber ( int a [ ] , int n ) {
unordered_set < int > s ( a , a + n ) ;
int arr_sum = accumulate ( a , a + n , 0 ) ;
int set_sum = accumulate ( s . begin ( ) , s . end ( ) , 0 ) ;
return ( 3 * set_sum - arr_sum ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def singleNumber ( nums ) :
return ( 3 * sum ( set ( nums ) ) - sum ( nums ) ) / 2
</Code> |
python-cpp-QUERIES_COUNTS_ARRAY_ELEMENTS_VALUES_GIVEN_RANGE | python-cpp | def countInRange ( arr , n , x , y ) :
count = 0 ;
for i in range ( n ) :
if ( arr [ i ] >= x and arr [ i ] <= y ) :
count += 1
return count
| int countInRange ( int arr [ ] , int n , int x , int y ) {
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] >= x && arr [ i ] <= y ) count ++ ;
}
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countInRange ( arr , n , x , y ) :
count = 0 ;
for i in range ( n ) :
if ( arr [ i ] >= x and arr [ i ] <= y ) :
count += 1
return count
</Code> |
python-cpp-FIND_SMALLEST_NUMBER_WITH_GIVEN_NUMBER_OF_DIGITS_AND_DIGIT_SUM | python-cpp | def findSmallest ( m , s ) :
if ( s == 0 ) :
if ( m == 1 ) :
print ( "Smallest number is 0" )
else :
print ( "Not possible" )
return
if ( s > 9 * m ) :
print ( "Not possible" )
return
res = [ 0 for i in range ( m + 1 ) ]
s -= 1
for i in... | void findSmallest ( int m , int s ) {
if ( s == 0 ) {
( m == 1 ) ? cout << "Smallest number is " << 0 : cout << "Not possible" ;
return ;
}
if ( s > 9 * m ) {
cout << "Not possible" ;
return ;
}
int res [ m ] ;
s -= 1 ;
for ( int i = m - 1 ;
i > 0 ;
i -- ) {
if ( s > 9 ) {
re... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSmallest ( m , s ) :
if ( s == 0 ) :
if ( m == 1 ) :
print ( "Smallest number is 0" )
else :
print ( "Not possible" )
return
if ( s > 9 * m ) :
print ( "Not possible" )
return
res = [ 0 for i in range ( m + 1 ) ]
s -= 1
f... |
python-cpp-POSSIBILITY_OF_A_WORD_FROM_A_GIVEN_SET_OF_CHARACTERS | python-cpp | def isPresent ( s , q ) :
freq = [ 0 ] * MAX_CHAR
for i in range ( 0 , len ( s ) ) :
freq [ ord ( s [ i ] ) ] += 1
for i in range ( 0 , len ( q ) ) :
freq [ ord ( q [ i ] ) ] -= 1
if ( freq [ ord ( q [ i ] ) ] < 0 ) :
return False
return True
| bool isPresent ( string s , string q ) {
int freq [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) freq [ s [ i ] ] ++ ;
for ( int i = 0 ;
i < q . length ( ) ;
i ++ ) {
freq [ q [ i ] ] -- ;
if ( freq [ q [ i ] ] < 0 ) return false ;
}
return tru... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPresent ( s , q ) :
freq = [ 0 ] * MAX_CHAR
for i in range ( 0 , len ( s ) ) :
freq [ ord ( s [ i ] ) ] += 1
for i in range ( 0 , len ( q ) ) :
freq [ ord ( q [ i ] ) ] -= 1
if ( freq [ ord ( q [ i ] ) ] < 0 ) :
return False
return True
</Code> |
python-cpp-SORT_ARRAY_APPLYING_GIVEN_EQUATION | python-cpp | def sortArray ( arr , n , A , B , C ) :
for i in range ( n ) :
arr [ i ] = ( A * arr [ i ] * arr [ i ] + B * arr [ i ] + C )
index = - ( sys.maxsize - 1 )
maximum = - ( sys.maxsize - 1 )
for i in range ( n ) :
if maximum < arr [ i ] :
index = i
maximum = arr [ i ]... | void sortArray ( int arr [ ] , int n , int A , int B , int C ) {
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = A * arr [ i ] * arr [ i ] + B * arr [ i ] + C ;
int index , maximum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( maximum < arr [ i ] ) {
index = i ;
maximum = arr [ i ] ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortArray ( arr , n , A , B , C ) :
for i in range ( n ) :
arr [ i ] = ( A * arr [ i ] * arr [ i ] + B * arr [ i ] + C )
index = - ( sys.maxsize - 1 )
maximum = - ( sys.maxsize - 1 )
for i in range ( n ) :
if maximum < arr [ i ] :
index = i
maximum = ar... |
python-cpp-DISTANCE_NEAREST_CELL_1_BINARY_MATRIX | python-cpp | def printDistance ( mat ) :
global N , M
ans = [ [ None ] * M for i in range ( N ) ]
for i in range ( N ) :
for j in range ( M ) :
ans [ i ] [ j ] = 999999999999
for i in range ( N ) :
for j in range ( M ) :
for k in range ( N ) :
for l in range ( ... | void printDistance ( int mat [ N ] [ M ] ) {
int ans [ N ] [ M ] ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < M ;
j ++ ) ans [ i ] [ j ] = INT_MAX ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < M ;
j ++ ) {
for ( int k = 0 ;
k < N ;
k ++ ) for ( int l = 0 ;
l ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printDistance ( mat ) :
global N , M
ans = [ [ None ] * M for i in range ( N ) ]
for i in range ( N ) :
for j in range ( M ) :
ans [ i ] [ j ] = 999999999999
for i in range ( N ) :
for j in range ( M ) :
for k in range ( N ) :
for l in r... |
python-cpp-FIND_LAST_INDEX_CHARACTER_STRING | python-cpp | def findLastIndex ( str , x ) :
index = - 1
for i in range ( 0 , len ( str ) ) :
if str [ i ] == x :
index = i
return index
| int findLastIndex ( string & str , char x ) {
int index = - 1 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) if ( str [ i ] == x ) index = i ;
return index ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLastIndex ( str , x ) :
index = - 1
for i in range ( 0 , len ( str ) ) :
if str [ i ] == x :
index = i
return index
</Code> |
python-cpp-PRINT_TRIPLETS_SORTED_ARRAY_FORM_AP | python-cpp | def printAllAPTriplets ( arr , n ) :
s = [ ]
for i in range ( 0 , n - 1 ) :
for j in range ( i + 1 , n ) :
diff = arr [ j ] - arr [ i ]
if ( ( arr [ i ] - diff ) in arr ) :
print ( "{} {} {}".format ( ( arr [ i ] - diff ) , arr [ i ] , arr [ j ] ) , end = "\n" )
... | void printAllAPTriplets ( int arr [ ] , int n ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int diff = arr [ j ] - arr [ i ] ;
if ( s . find ( arr [ i ] - diff ) != s . end ( ) ) cout << arr [ i ] - diff << " " << arr [ i ] <<... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printAllAPTriplets ( arr , n ) :
s = [ ]
for i in range ( 0 , n - 1 ) :
for j in range ( i + 1 , n ) :
diff = arr [ j ] - arr [ i ]
if ( ( arr [ i ] - diff ) in arr ) :
print ( "{} {} {}".format ( ( arr [ i ] - diff ) , arr [ i ] , arr [ j ] ) , end = "... |
python-cpp-REMOVE_BRACKETS_ALGEBRAIC_STRING_CONTAINING_OPERATORS | python-cpp | def simplify ( Str ) :
Len = len ( Str )
res = [ None ] * Len
index = 0
i = 0
s = [ ]
s.append ( 0 )
while ( i < Len ) :
if ( Str [ i ] == '+' ) :
if ( s [ - 1 ] == 1 ) :
res [ index ] = '-'
index += 1
if ( s [ - 1 ] == 0 ) :
... | char * simplify ( string str ) {
int len = str . length ( ) ;
char * res = new char ( len ) ;
int index = 0 , i = 0 ;
stack < int > s ;
s . push ( 0 ) ;
while ( i < len ) {
if ( str [ i ] == '+' ) {
if ( s . top ( ) == 1 ) res [ index ++ ] = '-' ;
if ( s . top ( ) == 0 ) res [ index ++ ] = '... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def simplify ( Str ) :
Len = len ( Str )
res = [ None ] * Len
index = 0
i = 0
s = [ ]
s.append ( 0 )
while ( i < Len ) :
if ( Str [ i ] == '+' ) :
if ( s [ - 1 ] == 1 ) :
res [ index ] = '-'
index += 1
if ( s [ - 1 ] == 0... |
python-cpp-CALCULATE_VOLUME_DODECAHEDRON | python-cpp | def vol_of_dodecahedron ( side ) :
return ( ( ( 15 + ( 7 * ( math.sqrt ( 5 ) ) ) ) / 4 ) * ( math.pow ( side , 3 ) ) )
| double vol_of_dodecahedron ( int side ) {
return ( ( ( 15 + ( 7 * ( sqrt ( 5 ) ) ) ) / 4 ) * ( pow ( side , 3 ) ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def vol_of_dodecahedron ( side ) :
return ( ( ( 15 + ( 7 * ( math.sqrt ( 5 ) ) ) ) / 4 ) * ( math.pow ( side , 3 ) ) )
</Code> |
python-cpp-WAYS_TRANSFORMING_ONE_STRING_REMOVING_0_CHARACTERS | python-cpp | def countTransformation ( a , b ) :
n = len ( a )
m = len ( b )
if m == 0 :
return 1
dp = [ [ 0 ] * ( n + 1 ) for _ in range ( m + 1 ) ]
for i in range ( m ) :
for j in range ( i , n ) :
if i == 0 :
if j == 0 :
if a [ j ] == b [ i ] :
... | int countTransformation ( string a , string b ) {
int n = a . size ( ) , m = b . size ( ) ;
if ( m == 0 ) return 1 ;
int dp [ m + 1 ] [ n + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
if ( i == 0 ) {
if ( j == 0 ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countTransformation ( a , b ) :
n = len ( a )
m = len ( b )
if m == 0 :
return 1
dp = [ [ 0 ] * ( n + 1 ) for _ in range ( m + 1 ) ]
for i in range ( m ) :
for j in range ( i , n ) :
if i == 0 :
if j == 0 :
if a [ j ] == b [ ... |
python-cpp-SUM_NODES_K_TH_LEVEL_TREE_REPRESENTED_STRING_1 | python-cpp | def sumAtKthLevel ( tree , k , i , level ) :
if ( tree [ i [ 0 ] ] == '(' ) :
i [ 0 ] += 1
if ( tree [ i [ 0 ] ] == ')' ) :
return 0
sum = 0
if ( level == k ) :
sum = int ( tree [ i [ 0 ] ] )
i [ 0 ] += 1
leftsum = sumAtKthLevel ( tree , k , i ... | int sumAtKthLevel ( string tree , int k , int & i , int level ) {
if ( tree [ i ++ ] == '(' ) {
if ( tree [ i ] == ')' ) return 0 ;
int sum = 0 ;
if ( level == k ) sum = tree [ i ] - '0' ;
int leftsum = sumAtKthLevel ( tree , k , ++ i , level + 1 ) ;
int rightsum = sumAtKthLevel ( tree , k , ++ i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sumAtKthLevel ( tree , k , i , level ) :
if ( tree [ i [ 0 ] ] == '(' ) :
i [ 0 ] += 1
if ( tree [ i [ 0 ] ] == ')' ) :
return 0
sum = 0
if ( level == k ) :
sum = int ( tree [ i [ 0 ] ] )
i [ 0 ] += 1
leftsum = sumAtKthLevel ( tree ,... |
python-cpp-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_1 | python-cpp | def printRepeating ( arr , size ) :
count = [ 0 ] * size
print ( " Repeating elements are " , end = "" )
for i in range ( 0 , size ) :
if ( count [ arr [ i ] ] == 1 ) :
print ( arr [ i ] , end = " " )
else :
count [ arr [ i ] ] = count [ arr [ i ] ] + 1
| void printRepeating ( int arr [ ] , int size ) {
int * count = new int [ sizeof ( int ) * ( size - 2 ) ] ;
int i ;
cout << " Repeating elements are " ;
for ( i = 0 ;
i < size ;
i ++ ) {
if ( count [ arr [ i ] ] == 1 ) cout << arr [ i ] << " " ;
else count [ arr [ i ] ] ++ ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printRepeating ( arr , size ) :
count = [ 0 ] * size
print ( " Repeating elements are " , end = "" )
for i in range ( 0 , size ) :
if ( count [ arr [ i ] ] == 1 ) :
print ( arr [ i ] , end = " " )
else :
count [ arr [ i ] ] = count [ arr [ i ] ] + 1
</Code... |
python-cpp-COUNT_PAIRS_DIFFERENCE_EQUAL_K_1 | python-cpp | def countPairsWithDiffK ( arr , n , k ) :
count = 0
arr.sort ( )
l = 0
r = 0
while r < n :
if arr [ r ] - arr [ l ] == k :
count += 1
l += 1
r += 1
elif arr [ r ] - arr [ l ] > k :
l += 1
else :
r += 1
return cou... | int countPairsWithDiffK ( int arr [ ] , int n , int k ) {
int count = 0 ;
sort ( arr , arr + n ) ;
int l = 0 ;
int r = 0 ;
while ( r < n ) {
if ( arr [ r ] - arr [ l ] == k ) {
count ++ ;
l ++ ;
r ++ ;
}
else if ( arr [ r ] - arr [ l ] > k ) l ++ ;
else r ++ ;
}
return co... | 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
arr.sort ( )
l = 0
r = 0
while r < n :
if arr [ r ] - arr [ l ] == k :
count += 1
l += 1
r += 1
elif arr [ r ] - arr [ l ] > k :
l += 1
else :
r += 1
ret... |
python-cpp-DYNAMIC_PROGRAMMING_SUBSET_SUM_PROBLEM_1 | python-cpp | def isSubsetSum ( set , n , sum ) :
subset = ( [ [ False for i in range ( sum + 1 ) ] for i in range ( n + 1 ) ] )
for i in range ( n + 1 ) :
subset [ i ] [ 0 ] = True
for i in range ( 1 , sum + 1 ) :
subset [ 0 ] [ i ] = False
for i in range ( 1 , n + 1 ) :
for j... | bool isSubsetSum ( int set [ ] , int n , int sum ) {
bool subset [ n + 1 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) subset [ i ] [ 0 ] = true ;
for ( int i = 1 ;
i <= sum ;
i ++ ) subset [ 0 ] [ i ] = false ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= sum ;
j ++ ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isSubsetSum ( set , n , sum ) :
subset = ( [ [ False for i in range ( sum + 1 ) ] for i in range ( n + 1 ) ] )
for i in range ( n + 1 ) :
subset [ i ] [ 0 ] = True
for i in range ( 1 , sum + 1 ) :
subset [ 0 ] [ i ] = False
for i in range ( 1 , n + 1 ) :
... |
python-cpp-MAJORITY_ELEMENT_1 | python-cpp | def findMajority ( arr , size ) :
m = { }
for i in range ( size ) :
if arr [ i ] in m :
m [ arr [ i ] ] += 1
else :
m [ arr [ i ] ] = 1
count = 0
for key in m :
if m [ key ] > size / 2 :
count = 1
print ( "Majority found :-" , key )... | void findMajority ( int arr [ ] , int size ) {
unordered_map < int , int > m ;
for ( int i = 0 ;
i < size ;
i ++ ) m [ arr [ i ] ] ++ ;
int count = 0 ;
for ( auto i : m ) {
if ( i . second > size / 2 ) {
count = 1 ;
cout << "Majority found :- " << i . first << endl ;
break ;
}
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMajority ( arr , size ) :
m = { }
for i in range ( size ) :
if arr [ i ] in m :
m [ arr [ i ] ] += 1
else :
m [ arr [ i ] ] = 1
count = 0
for key in m :
if m [ key ] > size / 2 :
count = 1
print ( "Majority found :-" ... |
python-cpp-DYNAMIC_PROGRAMMING_SET_10_0_1_KNAPSACK_PROBLEM | python-cpp | def knapSack ( W , wt , val , n ) :
if n == 0 or W == 0 :
return 0
if ( wt [ n - 1 ] > W ) :
return knapSack ( W , wt , val , n - 1 )
else :
return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) )
| int knapSack ( int W , int wt [ ] , int val [ ] , int n ) {
if ( n == 0 || W == 0 ) return 0 ;
if ( wt [ n - 1 ] > W ) return knapSack ( W , wt , val , n - 1 ) ;
else return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def knapSack ( W , wt , val , n ) :
if n == 0 or W == 0 :
return 0
if ( wt [ n - 1 ] > W ) :
return knapSack ( W , wt , val , n - 1 )
else :
return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) )
</Code> |
python-cpp-PRINT_REVERSE_STRING_REMOVING_VOWELS | python-cpp | def replaceOriginal ( s , n ) :
r = [ ' ' ] * n
for i in range ( n ) :
r [ i ] = s [ n - 1 - i ]
if ( s [ i ] != 'a' and s [ i ] != 'e' and s [ i ] != 'i' and s [ i ] != 'o' and s [ i ] != 'u' ) :
print ( r [ i ] , end = "" )
print ( )
| void replaceOriginal ( string s , int n ) {
string r ( n , ' ' ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
r [ i ] = s [ n - 1 - i ] ;
if ( s [ i ] != 'a' && s [ i ] != 'e' && s [ i ] != 'i' && s [ i ] != 'o' && s [ i ] != 'u' ) {
cout << r [ i ] ;
}
}
cout << endl ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def replaceOriginal ( s , n ) :
r = [ ' ' ] * n
for i in range ( n ) :
r [ i ] = s [ n - 1 - i ]
if ( s [ i ] != 'a' and s [ i ] != 'e' and s [ i ] != 'i' and s [ i ] != 'o' and s [ i ] != 'u' ) :
print ( r [ i ] , end = "" )
print ( )
</Code> |
python-cpp-FIND_PAIRS_IN_ARRAY_WHOSE_SUMS_ALREADY_EXIST_IN_ARRAY_1 | python-cpp | def findPair ( arr , n ) :
s = { i : 1 for i in arr }
found = False
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if arr [ i ] + arr [ j ] in s.keys ( ) :
print ( arr [ i ] , arr [ j ] )
found = True
if found == False :
print ( "Not exi... | void findPair ( int arr [ ] , int n ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < n ;
i ++ ) s . insert ( arr [ i ] ) ;
bool found = false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( s . find ( arr [ i ] + arr [ j ] ) != s . end ( ) ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findPair ( arr , n ) :
s = { i : 1 for i in arr }
found = False
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if arr [ i ] + arr [ j ] in s.keys ( ) :
print ( arr [ i ] , arr [ j ] )
found = True
if found == False :
print ( "... |
python-cpp-NUMBER_SUBSTRINGS_STRING | python-cpp | def countNonEmptySubstr ( str ) :
n = len ( str )
return int ( n * ( n + 1 ) / 2 )
| int countNonEmptySubstr ( string str ) {
int n = str . length ( ) ;
return n * ( n + 1 ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countNonEmptySubstr ( str ) :
n = len ( str )
return int ( n * ( n + 1 ) / 2 )
</Code> |
python-cpp-NUMBER_DIGITS_PRODUCT_TWO_NUMBERS | python-cpp | def countDigits ( a , b ) :
count = 0
p = abs ( a * b )
if ( p == 0 ) :
return 1
while ( p > 0 ) :
count = count + 1
p = p // 10
return count
| int countDigits ( int a , int b ) {
int count = 0 ;
int p = abs ( a * b ) ;
if ( p == 0 ) return 1 ;
while ( p > 0 ) {
count ++ ;
p = p / 10 ;
}
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countDigits ( a , b ) :
count = 0
p = abs ( a * b )
if ( p == 0 ) :
return 1
while ( p > 0 ) :
count = count + 1
p = p // 10
return count
</Code> |
python-cpp-MAXIMUM_PROFIT_BY_BUYING_AND_SELLING_A_SHARE_AT_MOST_TWICE | python-cpp | def maxProfit ( price , n ) :
profit = [ 0 ] * n
max_price = price [ n - 1 ]
for i in range ( n - 2 , 0 , - 1 ) :
if price [ i ] > max_price :
max_price = price [ i ]
profit [ i ] = max ( profit [ i + 1 ] , max_price - price [ i ] )
min_price = price [ 0 ]
for i in range ... | int maxProfit ( int price [ ] , int n ) {
int * profit = new int [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) profit [ i ] = 0 ;
int max_price = price [ n - 1 ] ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( price [ i ] > max_price ) max_price = price [ i ] ;
profit [ i ] = max ( profit [ i + 1 ] , ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxProfit ( price , n ) :
profit = [ 0 ] * n
max_price = price [ n - 1 ]
for i in range ( n - 2 , 0 , - 1 ) :
if price [ i ] > max_price :
max_price = price [ i ]
profit [ i ] = max ( profit [ i + 1 ] , max_price - price [ i ] )
min_price = price [ 0 ]
for i in... |
python-cpp-COUNT_DERANGEMENTS_PERMUTATION_SUCH_THAT_NO_ELEMENT_APPEARS_IN_ITS_ORIGINAL_POSITION_1 | python-cpp | def countDer ( n ) :
der = [ 0 for i in range ( n + 1 ) ]
der [ 0 ] = 1
der [ 1 ] = 0
der [ 2 ] = 1
for i in range ( 3 , n + 1 ) :
der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] )
return der [ n ]
| int countDer ( int n ) {
int der [ n + 1 ] ;
der [ 0 ] = 1 ;
der [ 1 ] = 0 ;
der [ 2 ] = 1 ;
for ( int i = 3 ;
i <= n ;
++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ;
return der [ n ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countDer ( n ) :
der = [ 0 for i in range ( n + 1 ) ]
der [ 0 ] = 1
der [ 1 ] = 0
der [ 2 ] = 1
for i in range ( 3 , n + 1 ) :
der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] )
return der [ n ]
</Code> |
python-cpp-MAXIMUM_PATH_SUM_MATRIX | python-cpp | def findMaxPath ( mat ) :
res = - 1
for i in range ( M ) :
res = max ( res , mat [ 0 ] [ i ] )
for i in range ( 1 , N ) :
res = - 1
for j in range ( M ) :
if ( j > 0 and j < M - 1 ) :
mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ] [ j -... | int findMaxPath ( int mat [ ] [ M ] ) {
for ( int i = 1 ;
i < N ;
i ++ ) {
for ( int j = 0 ;
j < M ;
j ++ ) {
if ( j > 0 && j < M - 1 ) mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ] [ j - 1 ] , mat [ i - 1 ] [ j + 1 ] ) ) ;
else if ( j > 0 ) mat [ i ] [ j ] += max ( ma... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMaxPath ( mat ) :
res = - 1
for i in range ( M ) :
res = max ( res , mat [ 0 ] [ i ] )
for i in range ( 1 , N ) :
res = - 1
for j in range ( M ) :
if ( j > 0 and j < M - 1 ) :
mat [ i ] [ j ] += max ( mat [ i - 1 ] [ j ] , max ( mat [ i - 1 ... |
python-cpp-CHECK_WHETHER_ARITHMETIC_PROGRESSION_CAN_FORMED_GIVEN_ARRAY | python-cpp | def checkIsAP ( arr , n ) :
if ( n == 1 ) : return True
arr.sort ( )
d = arr [ 1 ] - arr [ 0 ]
for i in range ( 2 , n ) :
if ( arr [ i ] - arr [ i - 1 ] != d ) :
return False
return True
| bool checkIsAP ( int arr [ ] , int n ) {
if ( n == 1 ) return true ;
sort ( arr , arr + n ) ;
int d = arr [ 1 ] - arr [ 0 ] ;
for ( int i = 2 ;
i < n ;
i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def checkIsAP ( arr , n ) :
if ( n == 1 ) : return True
arr.sort ( )
d = arr [ 1 ] - arr [ 0 ]
for i in range ( 2 , n ) :
if ( arr [ i ] - arr [ i - 1 ] != d ) :
return False
return True
</Code> |
python-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_11_NOT | python-cpp | def check ( st ) :
n = len ( st )
oddDigSum = 0
evenDigSum = 0
for i in range ( 0 , n ) :
if ( i % 2 == 0 ) :
oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) )
else :
evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) )
return ( ( oddDigSum - evenDigSum ) % 11 ==... | int check ( string str ) {
int n = str . length ( ) ;
int oddDigSum = 0 , evenDigSum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - '0' ) ;
else evenDigSum += ( str [ i ] - '0' ) ;
}
return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def check ( st ) :
n = len ( st )
oddDigSum = 0
evenDigSum = 0
for i in range ( 0 , n ) :
if ( i % 2 == 0 ) :
oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) )
else :
evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) )
return ( ( oddDigSum - evenDigSum ) ... |
python-cpp-SWAP_TWO_NUMBERS_WITHOUT_USING_TEMPORARY_VARIABLE | python-cpp | def swap ( xp , yp ) :
xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
yp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
| void swap ( int * xp , int * yp ) {
* xp = * xp ^ * yp ;
* yp = * xp ^ * yp ;
* xp = * xp ^ * yp ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def swap ( xp , yp ) :
xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
yp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
xp [ 0 ] = xp [ 0 ] ^ yp [ 0 ]
</Code> |
python-cpp-AREA_SQUARE_CIRCUMSCRIBED_CIRCLE | python-cpp | def find_Area ( r ) :
return ( 2 * r * r )
| int find_Area ( int r ) {
return ( 2 * r * r ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def find_Area ( r ) :
return ( 2 * r * r )
</Code> |
python-cpp-PROGRAM_TO_CHECK_IF_A_GIVEN_NUMBER_IS_LUCKY_ALL_DIGITS_ARE_DIFFERENT | python-cpp | def isLucky ( n ) :
ar = [ 0 ] * 10
while ( n > 0 ) :
digit = math.floor ( n % 10 )
if ( ar [ digit ] ) :
return 0
ar [ digit ] = 1
n = n / 10
return 1
| bool isLucky ( int n ) {
bool arr [ 10 ] ;
for ( int i = 0 ;
i < 10 ;
i ++ ) arr [ i ] = false ;
while ( n > 0 ) {
int digit = n % 10 ;
if ( arr [ digit ] ) return false ;
arr [ digit ] = true ;
n = n / 10 ;
}
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isLucky ( n ) :
ar = [ 0 ] * 10
while ( n > 0 ) :
digit = math.floor ( n % 10 )
if ( ar [ digit ] ) :
return 0
ar [ digit ] = 1
n = n / 10
return 1
</Code> |
python-cpp-COUNT_NUMBER_WAYS_JUMP_REACH_END | python-cpp | def countWaysToJump ( arr , n ) :
count_jump = [ 0 for i in range ( n ) ]
for i in range ( n - 2 , - 1 , - 1 ) :
if ( arr [ i ] >= n - i - 1 ) :
count_jump [ i ] += 1
j = i + 1
while ( j < n - 1 and j <= arr [ i ] + i ) :
if ( count_jump [ j ] != - 1 ) :
... | void countWaysToJump ( int arr [ ] , int n ) {
int count_jump [ n ] ;
memset ( count_jump , 0 , sizeof ( count_jump ) ) ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] >= n - i - 1 ) count_jump [ i ] ++ ;
for ( int j = i + 1 ;
j < n - 1 && j <= arr [ i ] + i ;
j ++ ) if ( count_jump [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countWaysToJump ( arr , n ) :
count_jump = [ 0 for i in range ( n ) ]
for i in range ( n - 2 , - 1 , - 1 ) :
if ( arr [ i ] >= n - i - 1 ) :
count_jump [ i ] += 1
j = i + 1
while ( j < n - 1 and j <= arr [ i ] + i ) :
if ( count_jump [ j ] != - 1 ) :
... |
python-cpp-PROGRAM_DISPLAY_ASTROLOGICAL_SIGN_ZODIAC_SIGN_GIVEN_DATE_BIRTH | python-cpp | def zodiac_sign ( day , month ) :
if month == 'december' :
astro_sign = 'Sagittarius' if ( day < 22 ) else 'capricorn'
elif month == 'january' :
astro_sign = 'Capricorn' if ( day < 20 ) else 'aquarius'
elif month == 'february' :
astro_sign = 'Aquarius' if ( day < 19 ) else 'pisces'
... | void zodiac_sign ( int day , string month ) {
string astro_sign = "" ;
if ( month == "december" ) {
if ( day < 22 ) astro_sign = "Sagittarius" ;
else astro_sign = "capricorn" ;
}
else if ( month == "january" ) {
if ( day < 20 ) astro_sign = "Capricorn" ;
else astro_sign = "aquarius" ;
}
else... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def zodiac_sign ( day , month ) :
if month == 'december' :
astro_sign = 'Sagittarius' if ( day < 22 ) else 'capricorn'
elif month == 'january' :
astro_sign = 'Capricorn' if ( day < 20 ) else 'aquarius'
elif month == 'february' :
astro_sign = 'Aquarius' if ( day < 19 ) else 'pi... |
python-cpp-FIND_NUMBER_OF_SOLUTIONS_OF_A_LINEAR_EQUATION_OF_N_VARIABLES_1 | python-cpp | def countSol ( coeff , n , rhs ) :
dp = [ 0 for i in range ( rhs + 1 ) ]
dp [ 0 ] = 1
for i in range ( n ) :
for j in range ( coeff [ i ] , rhs + 1 ) :
dp [ j ] += dp [ j - coeff [ i ] ]
return dp [ rhs ]
| int countSol ( int coeff [ ] , int n , int rhs ) {
int dp [ rhs + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = coeff [ i ] ;
j <= rhs ;
j ++ ) dp [ j ] += dp [ j - coeff [ i ] ] ;
return dp [ rhs ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSol ( coeff , n , rhs ) :
dp = [ 0 for i in range ( rhs + 1 ) ]
dp [ 0 ] = 1
for i in range ( n ) :
for j in range ( coeff [ i ] , rhs + 1 ) :
dp [ j ] += dp [ j - coeff [ i ] ]
return dp [ rhs ]
</Code> |
python-cpp-PROGRAM_TO_CHECK_IF_A_MATRIX_IS_SYMMETRIC | python-cpp | def isSymmetric ( mat , N ) :
for i in range ( N ) :
for j in range ( N ) :
if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) :
return False
return True
| bool isSymmetric ( int mat [ ] [ MAX ] , int N ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isSymmetric ( mat , N ) :
for i in range ( N ) :
for j in range ( N ) :
if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) :
return False
return True
</Code> |
python-cpp-FIND_RECTANGLE_BINARY_MATRIX_CORNERS_1 | python-cpp | def isRectangle ( m ) :
rows = len ( m )
if ( rows == 0 ) :
return False
columns = len ( m [ 0 ] )
for y1 in range ( rows ) :
for x1 in range ( columns ) :
if ( m [ y1 ] [ x1 ] == 1 ) :
for y2 in range ( y1 + 1 , rows ) :
for x2 in range ( ... | bool isRectangle ( const vector < vector < int >> & m ) {
int rows = m . size ( ) ;
if ( rows == 0 ) return false ;
int columns = m [ 0 ] . size ( ) ;
for ( int y1 = 0 ;
y1 < rows ;
y1 ++ ) for ( int x1 = 0 ;
x1 < columns ;
x1 ++ ) if ( m [ y1 ] [ x1 ] == 1 ) for ( int y2 = y1 + 1 ;
y2 < rows ;
y2 +... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isRectangle ( m ) :
rows = len ( m )
if ( rows == 0 ) :
return False
columns = len ( m [ 0 ] )
for y1 in range ( rows ) :
for x1 in range ( columns ) :
if ( m [ y1 ] [ x1 ] == 1 ) :
for y2 in range ( y1 + 1 , rows ) :
for x2 in r... |
python-cpp-DIVISIBILITY_9_USING_BITWISE_OPERATORS | python-cpp | def isDivBy9 ( n ) :
if ( n == 0 or n == 9 ) :
return True
if ( n < 9 ) :
return False
return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) )
| bool isDivBy9 ( int n ) {
if ( n == 0 || n == 9 ) return true ;
if ( n < 9 ) return false ;
return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isDivBy9 ( n ) :
if ( n == 0 or n == 9 ) :
return True
if ( n < 9 ) :
return False
return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) )
</Code> |
python-cpp-KNAPSACK_PROBLEM | python-cpp | def knapSack ( W , wt , val , n ) :
if n == 0 or W == 0 :
return 0
if ( wt [ n - 1 ] > W ) :
return knapSack ( W , wt , val , n - 1 )
else :
return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) )
| int knapSack ( int W , int wt [ ] , int val [ ] , int n ) {
if ( n == 0 || W == 0 ) return 0 ;
if ( wt [ n - 1 ] > W ) return knapSack ( W , wt , val , n - 1 ) ;
else return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def knapSack ( W , wt , val , n ) :
if n == 0 or W == 0 :
return 0
if ( wt [ n - 1 ] > W ) :
return knapSack ( W , wt , val , n - 1 )
else :
return max ( val [ n - 1 ] + knapSack ( W - wt [ n - 1 ] , wt , val , n - 1 ) , knapSack ( W , wt , val , n - 1 ) )
</Code> |
python-cpp-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_2 | python-cpp | def printRepeating ( arr , size ) :
xor = arr [ 0 ]
n = size - 2
x = 0
y = 0
for i in range ( 1 , size ) :
xor ^= arr [ i ]
for i in range ( 1 , n + 1 ) :
xor ^= i
set_bit_no = xor & ~ ( xor - 1 )
for i in range ( 0 , size ) :
if ( arr [ i ] & set_bit_no ) :
... | void printRepeating ( int arr [ ] , int size ) {
int Xor = arr [ 0 ] ;
int set_bit_no ;
int i ;
int n = size - 2 ;
int x = 0 , y = 0 ;
for ( i = 1 ;
i < size ;
i ++ ) Xor ^= arr [ i ] ;
for ( i = 1 ;
i <= n ;
i ++ ) Xor ^= i ;
set_bit_no = Xor & ~ ( Xor - 1 ) ;
for ( i = 0 ;
i < size ;
i +... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printRepeating ( arr , size ) :
xor = arr [ 0 ]
n = size - 2
x = 0
y = 0
for i in range ( 1 , size ) :
xor ^= arr [ i ]
for i in range ( 1 , n + 1 ) :
xor ^= i
set_bit_no = xor & ~ ( xor - 1 )
for i in range ( 0 , size ) :
if ( arr [ i ] & set_bit_no ) ... |
python-cpp-COUNT_DISTINCT_OCCURRENCES_AS_A_SUBSEQUENCE | python-cpp | def findSubsequenceCount ( S , T ) :
m = len ( T )
n = len ( S )
if m > n :
return 0
mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ]
for i in range ( 1 , m + 1 ) :
mat [ i ] [ 0 ] = 0
for j in range ( n + 1 ) :
mat [ 0 ] [ j ] = 1
for i in range ( 1... | int findSubsequenceCount ( string S , string T ) {
int m = T . length ( ) , n = S . length ( ) ;
if ( m > n ) return 0 ;
int mat [ m + 1 ] [ n + 1 ] ;
for ( int i = 1 ;
i <= m ;
i ++ ) mat [ i ] [ 0 ] = 0 ;
for ( int j = 0 ;
j <= n ;
j ++ ) mat [ 0 ] [ j ] = 1 ;
for ( int i = 1 ;
i <= m ;
i ++ )... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSubsequenceCount ( S , T ) :
m = len ( T )
n = len ( S )
if m > n :
return 0
mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ]
for i in range ( 1 , m + 1 ) :
mat [ i ] [ 0 ] = 0
for j in range ( n + 1 ) :
mat [ 0 ] [ j ] = 1
for i in ra... |
python-cpp-RECURSIVELY_BREAK_NUMBER_3_PARTS_GET_MAXIMUM_SUM_1 | python-cpp | def breakSum ( n ) :
dp = [ 0 ] * ( n + 1 )
dp [ 0 ] = 0
dp [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
dp [ i ] = max ( dp [ int ( i / 2 ) ] + dp [ int ( i / 3 ) ] + dp [ int ( i / 4 ) ] , i )
return dp [ n ]
| int breakSum ( int n ) {
int dp [ n + 1 ] ;
dp [ 0 ] = 0 , dp [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) dp [ i ] = max ( dp [ i / 2 ] + dp [ i / 3 ] + dp [ i / 4 ] , i ) ;
return dp [ n ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def breakSum ( n ) :
dp = [ 0 ] * ( n + 1 )
dp [ 0 ] = 0
dp [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
dp [ i ] = max ( dp [ int ( i / 2 ) ] + dp [ int ( i / 3 ) ] + dp [ int ( i / 4 ) ] , i )
return dp [ n ]
</Code> |
python-cpp-PROGRAM_DECIMAL_HEXADECIMAL_CONVERSION | python-cpp | def decToHexa ( n ) :
hexaDeciNum = [ '0' ] * 100
i = 0
while ( n != 0 ) :
temp = 0
temp = n % 16
if ( temp < 10 ) :
hexaDeciNum [ i ] = chr ( temp + 48 )
i = i + 1
else :
hexaDeciNum [ i ] = chr ( temp + 55 )
i = i + 1
... | void decToHexa ( int n ) {
char hexaDeciNum [ 100 ] ;
int i = 0 ;
while ( n != 0 ) {
int temp = 0 ;
temp = n % 16 ;
if ( temp < 10 ) {
hexaDeciNum [ i ] = temp + 48 ;
i ++ ;
}
else {
hexaDeciNum [ i ] = temp + 55 ;
i ++ ;
}
n = n / 16 ;
}
for ( int j = i - 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def decToHexa ( n ) :
hexaDeciNum = [ '0' ] * 100
i = 0
while ( n != 0 ) :
temp = 0
temp = n % 16
if ( temp < 10 ) :
hexaDeciNum [ i ] = chr ( temp + 48 )
i = i + 1
else :
hexaDeciNum [ i ] = chr ( temp + 55 )
i = i + 1
... |
python-cpp-STEINS_ALGORITHM_FOR_FINDING_GCD_1 | python-cpp | def gcd ( a , b ) :
if ( a == b ) :
return a
if ( a == 0 ) :
return b
if ( b == 0 ) :
return a
if ( ( ~ a & 1 ) == 1 ) :
if ( ( b & 1 ) == 1 ) :
return gcd ( a >> 1 , b )
else :
return ( gcd ( a >> 1 , b >> 1 ) << 1 )
if ( ( ~ b & 1 ) =... | int gcd ( int a , int b ) {
if ( a == b ) return a ;
if ( a == 0 ) return b ;
if ( b == 0 ) return a ;
if ( ~ a & 1 ) {
if ( b & 1 ) return gcd ( a >> 1 , b ) ;
else return gcd ( a >> 1 , b >> 1 ) << 1 ;
}
if ( ~ b & 1 ) return gcd ( a , b >> 1 ) ;
if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def gcd ( a , b ) :
if ( a == b ) :
return a
if ( a == 0 ) :
return b
if ( b == 0 ) :
return a
if ( ( ~ a & 1 ) == 1 ) :
if ( ( b & 1 ) == 1 ) :
return gcd ( a >> 1 , b )
else :
return ( gcd ( a >> 1 , b >> 1 ) << 1 )
if ( ( ~ b ... |
python-cpp-DETECT_IF_TWO_INTEGERS_HAVE_OPPOSITE_SIGNS | python-cpp | def oppositeSigns ( x , y ) :
return ( ( x ^ y ) < 0 )
| bool oppositeSigns ( int x , int y ) {
return ( ( x ^ y ) < 0 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def oppositeSigns ( x , y ) :
return ( ( x ^ y ) < 0 )
</Code> |
python-cpp-PROGRAM_CHECK_ARRAY_SORTED_NOT_ITERATIVE_RECURSIVE_1 | python-cpp | def arraySortedOrNot ( arr , n ) :
if ( n == 0 or n == 1 ) :
return True
for i in range ( 1 , n ) :
if ( arr [ i - 1 ] > arr [ i ] ) :
return False
return True
| bool arraySortedOrNot ( int arr [ ] , int n ) {
if ( n == 0 || n == 1 ) return true ;
for ( int i = 1 ;
i < n ;
i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def arraySortedOrNot ( arr , n ) :
if ( n == 0 or n == 1 ) :
return True
for i in range ( 1 , n ) :
if ( arr [ i - 1 ] > arr [ i ] ) :
return False
return True
</Code> |
python-cpp-CONVERT_STRING_BINARY_SEQUENCE | python-cpp | def strToBinary ( s ) :
bin_conv = [ ]
for c in s :
ascii_val = ord ( c )
binary_val = bin ( ascii_val )
bin_conv.append ( binary_val [ 2 : ] )
return ( ' '.join ( bin_conv ) )
| void strToBinary ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
int val = int ( s [ i ] ) ;
string bin = "" ;
while ( val > 0 ) {
( val % 2 ) ? bin . push_back ( '1' ) : bin . push_back ( '0' ) ;
val /= 2 ;
}
reverse ( bin . begin ( ) , bin . end ( ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def strToBinary ( s ) :
bin_conv = [ ]
for c in s :
ascii_val = ord ( c )
binary_val = bin ( ascii_val )
bin_conv.append ( binary_val [ 2 : ] )
return ( ' '.join ( bin_conv ) )
</Code> |
python-cpp-GENERATE_PYTHAGORAS_TRIPLET_SINGLE_INTEGER | python-cpp | def evaluate ( n ) :
if ( n == 1 or n == 2 ) :
print ( "No Pythagoras" + " Triplet exists" )
elif ( n % 2 == 0 ) :
var = n * n / 4
print ( "Pythagoras Triplets" + " exist i.e." , end = "" )
print ( int ( n ) , "" , int ( var - 1 ) , "" , int ( var + 1 ) )
elif ( n % 2 != 0 ) ... | void evaluate ( long long int n ) {
if ( n == 1 || n == 2 ) printf ( "No Pythagoras Triplet exists" ) ;
else if ( n % 2 == 0 ) {
long long int var = 1LL * n * n / 4 ;
printf ( "Pythagoras Triplets exist i.e. " ) ;
printf ( "%lld %lld %lld" , n , var - 1 , var + 1 ) ;
}
else if ( n % 2 != 0 ) {
l... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def evaluate ( n ) :
if ( n == 1 or n == 2 ) :
print ( "No Pythagoras" + " Triplet exists" )
elif ( n % 2 == 0 ) :
var = n * n / 4
print ( "Pythagoras Triplets" + " exist i.e." , end = "" )
print ( int ( n ) , "" , int ( var - 1 ) , "" , int ( var + 1 ) )
elif ( n % 2 ... |
python-cpp-CHECK_INTEGER_OVERFLOW_MULTIPLICATION | python-cpp | def isOverflow ( a , b ) :
if ( a == 0 or b == 0 ) :
return False
result = a * b
if ( result >= 9223372036854775807 or result <= - 9223372036854775808 ) :
result = 0
if ( a == ( result // b ) ) :
print ( result // b )
return False
else :
return True
| bool isOverflow ( long long a , long long b ) {
if ( a == 0 || b == 0 ) return false ;
long long result = a * b ;
if ( a == result / b ) return false ;
else return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isOverflow ( a , b ) :
if ( a == 0 or b == 0 ) :
return False
result = a * b
if ( result >= 9223372036854775807 or result <= - 9223372036854775808 ) :
result = 0
if ( a == ( result // b ) ) :
print ( result // b )
return False
else :
return True
</... |
python-cpp-FIND_EXPRESSION_DUPLICATE_PARENTHESIS_NOT | python-cpp | def findDuplicateparenthesis ( string ) :
Stack = [ ]
for ch in string :
if ch == ')' :
top = Stack.pop ( )
elementsInside = 0
while top != '(' :
elementsInside += 1
top = Stack.pop ( )
if elementsInside < 1 :
... | bool findDuplicateparenthesis ( string str ) {
stack < char > Stack ;
for ( char ch : str ) {
if ( ch == ')' ) {
char top = Stack . top ( ) ;
Stack . pop ( ) ;
int elementsInside = 0 ;
while ( top != '(' ) {
elementsInside ++ ;
top = Stack . top ( ) ;
Stack . pop ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findDuplicateparenthesis ( string ) :
Stack = [ ]
for ch in string :
if ch == ')' :
top = Stack.pop ( )
elementsInside = 0
while top != '(' :
elementsInside += 1
top = Stack.pop ( )
if elementsInside < 1 :
... |
python-cpp-COUNT_ROTATIONS_DIVISIBLE_4 | python-cpp | def countRotations ( n ) :
l = len ( n )
if ( l == 1 ) :
oneDigit = ( int ) ( n [ 0 ] )
if ( oneDigit % 4 == 0 ) :
return 1
return 0
count = 0
for i in range ( 0 , l - 1 ) :
twoDigit = ( int ) ( n [ i ] ) * 10 + ( int ) ( n [ i + 1 ] )
if ( twoDigit % ... | int countRotations ( string n ) {
int len = n . length ( ) ;
if ( len == 1 ) {
int oneDigit = n . at ( 0 ) - '0' ;
if ( oneDigit % 4 == 0 ) return 1 ;
return 0 ;
}
int twoDigit , count = 0 ;
for ( int i = 0 ;
i < ( len - 1 ) ;
i ++ ) {
twoDigit = ( n . at ( i ) - '0' ) * 10 + ( n . at ( i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countRotations ( n ) :
l = len ( n )
if ( l == 1 ) :
oneDigit = ( int ) ( n [ 0 ] )
if ( oneDigit % 4 == 0 ) :
return 1
return 0
count = 0
for i in range ( 0 , l - 1 ) :
twoDigit = ( int ) ( n [ i ] ) * 10 + ( int ) ( n [ i + 1 ] )
if ( twoD... |
python-cpp-FIND_DISTINCT_INTEGERS_FOR_A_TRIPLET_WITH_GIVEN_PRODUCT | python-cpp | def findTriplets ( x ) :
fact = [ ]
factors = set ( )
for i in range ( 2 , int ( sqrt ( x ) ) ) :
if ( x % i == 0 ) :
fact.append ( i )
if ( x / i != i ) :
fact.append ( x // i )
factors.add ( i )
factors.add ( x // i )
found = Fals... | void findTriplets ( int x ) {
vector < int > fact ;
unordered_set < int > factors ;
for ( int i = 2 ;
i <= sqrt ( x ) ;
i ++ ) {
if ( x % i == 0 ) {
fact . push_back ( i ) ;
if ( x / i != i ) fact . push_back ( x / i ) ;
factors . insert ( i ) ;
factors . insert ( x / i ) ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findTriplets ( x ) :
fact = [ ]
factors = set ( )
for i in range ( 2 , int ( sqrt ( x ) ) ) :
if ( x % i == 0 ) :
fact.append ( i )
if ( x / i != i ) :
fact.append ( x // i )
factors.add ( i )
factors.add ( x // i )
found... |
python-cpp-PRINT_WAYS_BREAK_STRING_BRACKET_FORM | python-cpp | def findCombinations ( string , index , out ) :
if index == len ( string ) :
print ( out )
for i in range ( index , len ( string ) , 1 ) :
findCombinations ( string , i + 1 , out + "(" + string [ index : i + 1 ] + ")" )
| void findCombinations ( string str , int index , string out ) {
if ( index == str . length ( ) ) cout << out << endl ;
for ( int i = index ;
i < str . length ( ) ;
i ++ ) findCombinations ( str , i + 1 , out + "(" + str . substr ( index , i + 1 - index ) + ")" ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findCombinations ( string , index , out ) :
if index == len ( string ) :
print ( out )
for i in range ( index , len ( string ) , 1 ) :
findCombinations ( string , i + 1 , out + "(" + string [ index : i + 1 ] + ")" )
</Code> |
python-cpp-EFFICIENT_SEARCH_IN_AN_ARRAY_WHERE_DIFFERENCE_BETWEEN_ADJACENT_IS_1 | python-cpp | def search ( arr , n , x ) :
i = 0
while ( i <= n - 1 ) :
if ( arr [ i ] == x ) :
return i
i += abs ( arr [ i ] - x )
return - 1
| int search ( int arr [ ] , int n , int x ) {
int i = 0 ;
while ( i <= n - 1 ) {
if ( arr [ i ] == x ) return i ;
i += abs ( arr [ i ] - x ) ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , n , x ) :
i = 0
while ( i <= n - 1 ) :
if ( arr [ i ] == x ) :
return i
i += abs ( arr [ i ] - x )
return - 1
</Code> |
python-cpp-FIND_ELEMENTS_PRESENT_FIRST_ARRAY_NOT_SECOND_1 | python-cpp | def findMissing ( a , b , n , m ) :
s = dict ( )
for i in range ( m ) :
s [ b [ i ] ] = 1
for i in range ( n ) :
if a [ i ] not in s.keys ( ) :
print ( a [ i ] , end = " " )
| void findMissing ( int a [ ] , int b [ ] , int n , int m ) {
unordered_set < int > s ;
for ( int i = 0 ;
i < m ;
i ++ ) s . insert ( b [ i ] ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( s . find ( a [ i ] ) == s . end ( ) ) cout << a [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMissing ( a , b , n , m ) :
s = dict ( )
for i in range ( m ) :
s [ b [ i ] ] = 1
for i in range ( n ) :
if a [ i ] not in s.keys ( ) :
print ( a [ i ] , end = " " )
</Code> |
python-cpp-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS_1 | python-cpp | def countPairs ( str1 ) :
result = 0 ;
n = len ( str1 )
for i in range ( 0 , n ) :
for j in range ( 1 , MAX_CHAR + 1 ) :
if ( ( i + j ) < n ) :
if ( ( abs ( ord ( str1 [ i + j ] ) - ord ( str1 [ i ] ) ) == j ) ) :
result += 1 ;
return result
| int countPairs ( string str ) {
int result = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 1 ;
( i + j ) < n && j <= MAX_CHAR ;
j ++ ) if ( ( abs ( str [ i + j ] - str [ i ] ) == j ) ) result ++ ;
return result ;
}
| 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 ( 1 , MAX_CHAR + 1 ) :
if ( ( i + j ) < n ) :
if ( ( abs ( ord ( str1 [ i + j ] ) - ord ( str1 [ i ] ) ) == j ) ) :
result += 1 ;
return result... |
python-cpp-FIND_MAXIMUM_SUM_POSSIBLE_EQUAL_SUM_THREE_STACKS | python-cpp | def maxSum ( stack1 , stack2 , stack3 , n1 , n2 , n3 ) :
sum1 , sum2 , sum3 = 0 , 0 , 0
for i in range ( n1 ) :
sum1 += stack1 [ i ]
for i in range ( n2 ) :
sum2 += stack2 [ i ]
for i in range ( n3 ) :
sum3 += stack3 [ i ]
top1 , top2 , top3 = 0 , 0 , 0
ans = 0
while ... | int maxSum ( int stack1 [ ] , int stack2 [ ] , int stack3 [ ] , int n1 , int n2 , int n3 ) {
int sum1 = 0 , sum2 = 0 , sum3 = 0 ;
for ( int i = 0 ;
i < n1 ;
i ++ ) sum1 += stack1 [ i ] ;
for ( int i = 0 ;
i < n2 ;
i ++ ) sum2 += stack2 [ i ] ;
for ( int i = 0 ;
i < n3 ;
i ++ ) sum3 += stack3 [ i ] ;... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSum ( stack1 , stack2 , stack3 , n1 , n2 , n3 ) :
sum1 , sum2 , sum3 = 0 , 0 , 0
for i in range ( n1 ) :
sum1 += stack1 [ i ]
for i in range ( n2 ) :
sum2 += stack2 [ i ]
for i in range ( n3 ) :
sum3 += stack3 [ i ]
top1 , top2 , top3 = 0 , 0 , 0
ans = 0
... |
python-cpp-MINIMUM_SUM_TWO_NUMBERS_FORMED_DIGITS_ARRAY | python-cpp | def solve ( arr , n ) :
arr.sort ( )
a = 0 b = 0
for i in range ( n ) :
if ( i % 2 != 0 ) :
a = a * 10 + arr [ i ]
else :
b = b * 10 + arr [ i ]
return a + b
| int solve ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int a = 0 , b = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i & 1 ) a = a * 10 + arr [ i ] ;
else b = b * 10 + arr [ i ] ;
}
return a + b ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def solve ( arr , n ) :
arr.sort ( )
a = 0 b = 0
for i in range ( n ) :
if ( i % 2 != 0 ) :
a = a * 10 + arr [ i ]
else :
b = b * 10 + arr [ i ]
return a + b
</Code> |
python-cpp-FIND_FIRST_NATURAL_NUMBER_WHOSE_FACTORIAL_DIVISIBLE_X | python-cpp | def firstFactorialDivisibleNumber ( x ) :
i = 1
fact = 1
for i in range ( 1 , x ) :
fact = fact * i
if ( fact % x == 0 ) :
break
return i
| int firstFactorialDivisibleNumber ( int x ) {
int i = 1 ;
int fact = 1 ;
for ( i = 1 ;
i < x ;
i ++ ) {
fact = fact * i ;
if ( fact % x == 0 ) break ;
}
return i ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def firstFactorialDivisibleNumber ( x ) :
i = 1
fact = 1
for i in range ( 1 , x ) :
fact = fact * i
if ( fact % x == 0 ) :
break
return i
</Code> |
python-cpp-COUNT_MINIMUM_STEPS_GET_GIVEN_DESIRED_ARRAY | python-cpp | def countMinOperations ( target , n ) :
result = 0
while ( True ) :
zero_count = 0
i = 0
while ( i < n ) :
if ( ( target [ i ] & 1 ) > 0 ) :
break
elif ( target [ i ] == 0 ) :
zero_count += 1
i += 1
if ( zero_cou... | int countMinOperations ( unsigned int target [ ] , int n ) {
int result = 0 ;
while ( 1 ) {
int zero_count = 0 ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( target [ i ] & 1 ) break ;
else if ( target [ i ] == 0 ) zero_count ++ ;
}
if ( zero_count == n ) return result ;
i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countMinOperations ( target , n ) :
result = 0
while ( True ) :
zero_count = 0
i = 0
while ( i < n ) :
if ( ( target [ i ] & 1 ) > 0 ) :
break
elif ( target [ i ] == 0 ) :
zero_count += 1
i += 1
if ( z... |
python-cpp-SORT_ARRAY_CONTAIN_1_N_VALUES | python-cpp | def sortit ( arr , n ) :
for i in range ( n ) :
arr [ i ] = i + 1
| void sortit ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = i + 1 ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortit ( arr , n ) :
for i in range ( n ) :
arr [ i ] = i + 1
</Code> |
python-cpp-CHANGE_BITS_CAN_MADE_ONE_FLIP | python-cpp | def canMakeAllSame ( str ) :
zeros = 0
ones = 0
for i in range ( 0 , len ( str ) ) :
ch = str [ i ]
if ( ch == '0' ) :
zeros = zeros + 1
else :
ones = ones + 1
return ( zeros == 1 or ones == 1 )
| bool canMakeAllSame ( string str ) {
int zeros = 0 , ones = 0 ;
for ( char ch : str ) ( ch == '0' ) ? ++ zeros : ++ ones ;
return ( zeros == 1 || ones == 1 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def canMakeAllSame ( str ) :
zeros = 0
ones = 0
for i in range ( 0 , len ( str ) ) :
ch = str [ i ]
if ( ch == '0' ) :
zeros = zeros + 1
else :
ones = ones + 1
return ( zeros == 1 or ones == 1 )
</Code> |
python-cpp-SUM_ELEMENTS_K1TH_K2TH_SMALLEST_ELEMENTS_1 | python-cpp | def minheapify ( a , index ) :
small = index
l = 2 * index + 1
r = 2 * index + 2
if ( l < n and a [ l ] < a [ small ] ) :
small = l
if ( r < n and a [ r ] < a [ small ] ) :
small = r
if ( small != index ) :
( a [ small ] , a [ index ] ) = ( a [ index ] , a [ small ] )
... | void minheapify ( int a [ ] , int index ) {
int small = index ;
int l = 2 * index + 1 ;
int r = 2 * index + 2 ;
if ( l < n && a [ l ] < a [ small ] ) small = l ;
if ( r < n && a [ r ] < a [ small ] ) small = r ;
if ( small != index ) {
swap ( a [ small ] , a [ index ] ) ;
minheapify ( a , small ) ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minheapify ( a , index ) :
small = index
l = 2 * index + 1
r = 2 * index + 2
if ( l < n and a [ l ] < a [ small ] ) :
small = l
if ( r < n and a [ r ] < a [ small ] ) :
small = r
if ( small != index ) :
( a [ small ] , a [ index ] ) = ( a [ index ] , a [ small ... |
python-cpp-FIND_MINIMUM_SHIFT_LONGEST_COMMON_PREFIX | python-cpp | def KMP ( m , n , str2 , str1 ) :
pos = 0
Len = 0
p = [ 0 for i in range ( m + 1 ) ]
k = 0
for i in range ( 2 , n + 1 ) :
while ( k > 0 and str1 [ k ] != str1 [ i - 1 ] ) :
k = p [ k ]
if ( str1 [ k ] == str1 [ i - 1 ] ) :
k += 1
p [ i ] = k
j = 0
... | void KMP ( int m , int n , string str2 , string str1 ) {
int pos = 0 , len = 0 ;
int p [ m + 1 ] ;
int k = 0 ;
p [ 1 ] = 0 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
while ( k > 0 && str1 [ k ] != str1 [ i - 1 ] ) k = p [ k ] ;
if ( str1 [ k ] == str1 [ i - 1 ] ) ++ k ;
p [ i ] = k ;
}
for ( in... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def KMP ( m , n , str2 , str1 ) :
pos = 0
Len = 0
p = [ 0 for i in range ( m + 1 ) ]
k = 0
for i in range ( 2 , n + 1 ) :
while ( k > 0 and str1 [ k ] != str1 [ i - 1 ] ) :
k = p [ k ]
if ( str1 [ k ] == str1 [ i - 1 ] ) :
k += 1
p [ i ] = k
... |
python-cpp-SWAP_MAJOR_MINOR_DIAGONALS_SQUARE_MATRIX | python-cpp | def swapDiagonal ( matrix ) :
for i in range ( N ) :
matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] = \
matrix [ i ] [ N - i - 1 ] , matrix [ i ] [ i ]
| void swapDiagonal ( int matrix [ ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) swap ( matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def swapDiagonal ( matrix ) :
for i in range ( N ) :
matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] = \
matrix [ i ] [ N - i - 1 ] , matrix [ i ] [ i ]
</Code> |
python-cpp-REPRESENT_NUMBER_SUM_MINIMUM_POSSIBLE_PSUEDOBINARY_NUMBERS | python-cpp | def psuedoBinary ( n ) :
while ( n > 0 ) :
temp = n
m = 0
p = 1
while ( temp ) :
rem = temp % 10
temp = int ( temp / 10 )
if ( rem != 0 ) :
m += p
p *= 10
print ( m , end = " " )
n = n - m
| void psuedoBinary ( int n ) {
while ( n > 0 ) {
int temp = n , m = 0 , p = 1 ;
while ( temp ) {
int rem = temp % 10 ;
temp = temp / 10 ;
if ( rem != 0 ) m += p ;
p *= 10 ;
}
cout << m << " " ;
n = n - m ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def psuedoBinary ( n ) :
while ( n > 0 ) :
temp = n
m = 0
p = 1
while ( temp ) :
rem = temp % 10
temp = int ( temp / 10 )
if ( rem != 0 ) :
m += p
p *= 10
print ( m , end = " " )
n = n - m
</Code> |
python-cpp-MINIMUM_NUMBER_POINTS_REMOVED_GET_REMAINING_POINTS_ONE_SIDE_AXIS | python-cpp | def findmin ( p , n ) :
a , b , c , d = 0 , 0 , 0 , 0
for i in range ( n ) :
if ( p [ i ] [ 0 ] <= 0 ) :
a += 1
elif ( p [ i ] [ 0 ] >= 0 ) :
b += 1
if ( p [ i ] [ 1 ] >= 0 ) :
c += 1
elif ( p [ i ] [ 1 ] <= 0 ) :
d += 1
return ... | int findmin ( Point p [ ] , int n ) {
int a = 0 , b = 0 , c = 0 , d = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( p [ i ] . x <= 0 ) a ++ ;
else if ( p [ i ] . x >= 0 ) b ++ ;
if ( p [ i ] . y >= 0 ) c ++ ;
else if ( p [ i ] . y <= 0 ) d ++ ;
}
return min ( {
a , b , c , d }
) ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findmin ( p , n ) :
a , b , c , d = 0 , 0 , 0 , 0
for i in range ( n ) :
if ( p [ i ] [ 0 ] <= 0 ) :
a += 1
elif ( p [ i ] [ 0 ] >= 0 ) :
b += 1
if ( p [ i ] [ 1 ] >= 0 ) :
c += 1
elif ( p [ i ] [ 1 ] <= 0 ) :
d += 1
... |
python-cpp-FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_CHANGE_1 | python-cpp | def minCoins ( coins , m , V ) :
table = [ 0 for i in range ( V + 1 ) ]
table [ 0 ] = 0
for i in range ( 1 , V + 1 ) :
table [ i ] = sys.maxsize
for i in range ( 1 , V + 1 ) :
for j in range ( m ) :
if ( coins [ j ] <= i ) :
sub_res = table [ i - coins [ j ] ]... | int minCoins ( int coins [ ] , int m , int V ) {
int table [ V + 1 ] ;
table [ 0 ] = 0 ;
for ( int i = 1 ;
i <= V ;
i ++ ) table [ i ] = INT_MAX ;
for ( int i = 1 ;
i <= V ;
i ++ ) {
for ( int j = 0 ;
j < m ;
j ++ ) if ( coins [ j ] <= i ) {
int sub_res = table [ i - coins [ j ] ] ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minCoins ( coins , m , V ) :
table = [ 0 for i in range ( V + 1 ) ]
table [ 0 ] = 0
for i in range ( 1 , V + 1 ) :
table [ i ] = sys.maxsize
for i in range ( 1 , V + 1 ) :
for j in range ( m ) :
if ( coins [ j ] <= i ) :
sub_res = table [ i - coins ... |
python-cpp-MAKE_LARGEST_PALINDROME_CHANGING_K_DIGITS | python-cpp | def maximumPalinUsingKChanges ( strr , k ) :
palin = strr
l = 0
r = len ( strr ) - 1
while ( l <= r ) :
if ( strr [ l ] != strr [ r ] ) :
palin [ l ] = palin [ r ] = max ( strr [ l ] , strr [ r ] )
k -= 1
l += 1
r -= 1
if ( k < 0 ) :
return "No... | string maximumPalinUsingKChanges ( string str , int k ) {
string palin = str ;
int l = 0 ;
int r = str . length ( ) - 1 ;
while ( l < r ) {
if ( str [ l ] != str [ r ] ) {
palin [ l ] = palin [ r ] = max ( str [ l ] , str [ r ] ) ;
k -- ;
}
l ++ ;
r -- ;
}
if ( k < 0 ) return "No... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maximumPalinUsingKChanges ( strr , k ) :
palin = strr
l = 0
r = len ( strr ) - 1
while ( l <= r ) :
if ( strr [ l ] != strr [ r ] ) :
palin [ l ] = palin [ r ] = max ( strr [ l ] , strr [ r ] )
k -= 1
l += 1
r -= 1
if ( k < 0 ) :
ret... |
python-cpp-MINIMUM_ROOMS_FOR_M_EVENTS_OF_N_BATCHES_WITH_GIVEN_SCHEDULE | python-cpp | def findMinRooms ( slots , n , m ) :
counts = [ 0 ] * m
for i in range ( n ) :
for j in range ( m ) :
if ( slots [ i ] [ j ] == '1' ) :
counts [ j ] += 1
return max ( counts )
| int findMinRooms ( string slots [ ] , int n , int m ) {
int counts [ m ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) if ( slots [ i ] [ j ] == '1' ) counts [ j ] ++ ;
return * max_element ( counts , counts + m ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMinRooms ( slots , n , m ) :
counts = [ 0 ] * m
for i in range ( n ) :
for j in range ( m ) :
if ( slots [ i ] [ j ] == '1' ) :
counts [ j ] += 1
return max ( counts )
</Code> |
python-cpp-MAXIMUM_PATH_SUM_POSITION_JUMPS_DIVISIBILITY_CONDITION | python-cpp | def printMaxSum ( arr , n ) :
dp = [ 0 for i in range ( n ) ]
for i in range ( n ) :
dp [ i ] = arr [ i ]
maxi = 0
for j in range ( 1 , int ( ( i + 1 ) ** 0.5 ) + 1 ) :
if ( ( i + 1 ) % j == 0 and ( i + 1 ) != j ) :
if ( dp [ j - 1 ] > maxi ) :
... | void printMaxSum ( int arr [ ] , int n ) {
int dp [ n ] ;
memset ( dp , 0 , sizeof dp ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
dp [ i ] = arr [ i ] ;
int maxi = 0 ;
for ( int j = 1 ;
j <= sqrt ( i + 1 ) ;
j ++ ) {
if ( ( ( i + 1 ) % j == 0 ) && ( i + 1 ) != j ) {
if ( dp [ j - ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printMaxSum ( arr , n ) :
dp = [ 0 for i in range ( n ) ]
for i in range ( n ) :
dp [ i ] = arr [ i ]
maxi = 0
for j in range ( 1 , int ( ( i + 1 ) ** 0.5 ) + 1 ) :
if ( ( i + 1 ) % j == 0 and ( i + 1 ) != j ) :
if ( dp [ j - 1 ] > maxi ) :
... |
python-cpp-MAXIMUM_SUM_ALTERNATING_SUBSEQUENCE_SUM | python-cpp | def maxAlternateSum ( arr , n ) :
if ( n == 1 ) :
return arr [ 0 ]
dec = [ 0 for i in range ( n + 1 ) ]
inc = [ 0 for i in range ( n + 1 ) ]
dec [ 0 ] = inc [ 0 ] = arr [ 0 ]
flag = 0
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ j ] > arr [ i ] ) :
... | int maxAlternateSum ( int arr [ ] , int n ) {
if ( n == 1 ) return arr [ 0 ] ;
int dec [ n ] ;
memset ( dec , 0 , sizeof ( dec ) ) ;
int inc [ n ] ;
memset ( inc , 0 , sizeof ( inc ) ) ;
dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ;
int flag = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxAlternateSum ( arr , n ) :
if ( n == 1 ) :
return arr [ 0 ]
dec = [ 0 for i in range ( n + 1 ) ]
inc = [ 0 for i in range ( n + 1 ) ]
dec [ 0 ] = inc [ 0 ] = arr [ 0 ]
flag = 0
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ j ] > arr [ i ] ... |
python-cpp-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY | python-cpp | def printRepeating ( arr , size ) :
print ( "Repeating elements are " , end = '' )
for i in range ( 0 , size ) :
for j in range ( i + 1 , size ) :
if arr [ i ] == arr [ j ] :
print ( arr [ i ] , end = ' ' )
| void printRepeating ( int arr [ ] , int size ) {
int i , j ;
printf ( " Repeating elements are " ) ;
for ( i = 0 ;
i < size ;
i ++ ) for ( j = i + 1 ;
j < size ;
j ++ ) if ( arr [ i ] == arr [ j ] ) cout << arr [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printRepeating ( arr , size ) :
print ( "Repeating elements are " , end = '' )
for i in range ( 0 , size ) :
for j in range ( i + 1 , size ) :
if arr [ i ] == arr [ j ] :
print ( arr [ i ] , end = ' ' )
</Code> |
python-cpp-MINIMUM_PRODUCT_K_INTEGERS_ARRAY_POSITIVE_INTEGERS | python-cpp | def minProduct ( arr , n , k ) :
heapq.heapify ( arr )
count = 0
ans = 1
while ( arr ) and count < k :
x = heapq.heappop ( arr )
ans = ans * x
count = count + 1
return ans
| int minProduct ( int arr [ ] , int n , int k ) {
priority_queue < int , vector < int > , greater < int >> pq ;
for ( int i = 0 ;
i < n ;
i ++ ) pq . push ( arr [ i ] ) ;
int count = 0 , ans = 1 ;
while ( pq . empty ( ) == false && count < k ) {
ans = ans * pq . top ( ) ;
pq . pop ( ) ;
count ++ ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minProduct ( arr , n , k ) :
heapq.heapify ( arr )
count = 0
ans = 1
while ( arr ) and count < k :
x = heapq.heappop ( arr )
ans = ans * x
count = count + 1
return ans
</Code> |
python-cpp-C_PROGRAM_FIND_LARGEST_ELEMENT_ARRAY_1 | python-cpp | def largest ( arr , n ) :
return max ( arr )
| int largest ( int arr [ ] , int n ) {
return * max_element ( arr , arr + n ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def largest ( arr , n ) :
return max ( arr )
</Code> |
python-cpp-PROGRAM_TO_EFFICIENTLY_CALCULATE_EX | python-cpp | def exponential ( n , x ) :
sum = 1.0
for i in range ( n , 0 , - 1 ) :
sum = 1 + x * sum / i
print ( "e^x =" , sum )
| float exponential ( int n , float x ) {
float sum = 1.0f ;
for ( int i = n - 1 ;
i > 0 ;
-- i ) sum = 1 + x * sum / i ;
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def exponential ( n , x ) :
sum = 1.0
for i in range ( n , 0 , - 1 ) :
sum = 1 + x * sum / i
print ( "e^x =" , sum )
</Code> |
python-cpp-FIRST_ELEMENT_OCCURRING_K_TIMES_ARRAY | python-cpp | def firstElement ( arr , n , k ) :
count_map = { }
for i in range ( 0 , n ) :
if ( arr [ i ] in count_map.keys ( ) ) :
count_map [ arr [ i ] ] += 1
else :
count_map [ arr [ i ] ] = 1
i += 1
for i in range ( 0 , n ) :
if ( count_map [ arr [ i ] ] == k )... | int firstElement ( int arr [ ] , int n , int k ) {
unordered_map < int , int > count_map ;
for ( int i = 0 ;
i < n ;
i ++ ) count_map [ arr [ i ] ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( count_map [ arr [ i ] ] == k ) return arr [ i ] ;
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def firstElement ( arr , n , k ) :
count_map = { }
for i in range ( 0 , n ) :
if ( arr [ i ] in count_map.keys ( ) ) :
count_map [ arr [ i ] ] += 1
else :
count_map [ arr [ i ] ] = 1
i += 1
for i in range ( 0 , n ) :
if ( count_map [ arr [ i ] ]... |
python-cpp-DYNAMIC_PROGRAMMING_SET_36_CUT_A_ROPE_TO_MAXIMIZE_PRODUCT_1 | python-cpp | def maxProd ( n ) :
if ( n == 2 or n == 3 ) :
return ( n - 1 )
res = 1
while ( n > 4 ) :
n -= 3
res *= 3
return ( n * res )
| int maxProd ( int n ) {
if ( n == 2 || n == 3 ) return ( n - 1 ) ;
int res = 1 ;
while ( n > 4 ) {
n -= 3 ;
res *= 3 ;
}
return ( n * res ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxProd ( n ) :
if ( n == 2 or n == 3 ) :
return ( n - 1 )
res = 1
while ( n > 4 ) :
n -= 3
res *= 3
return ( n * res )
</Code> |
python-cpp-MAXIMUM_SUM_2_X_N_GRID_NO_TWO_ELEMENTS_ADJACENT | python-cpp | def maxSum ( grid , n ) :
incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] )
excl = 0
for i in range ( 1 , n ) :
excl_new = max ( excl , incl )
incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] )
excl = excl_new
return max ( excl , incl )
| int maxSum ( int grid [ 2 ] [ MAX ] , int n ) {
int incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] ) ;
int excl = 0 , excl_new ;
for ( int i = 1 ;
i < n ;
i ++ ) {
excl_new = max ( excl , incl ) ;
incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] ) ;
excl = excl_new ;
}
return max ( ex... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSum ( grid , n ) :
incl = max ( grid [ 0 ] [ 0 ] , grid [ 1 ] [ 0 ] )
excl = 0
for i in range ( 1 , n ) :
excl_new = max ( excl , incl )
incl = excl + max ( grid [ 0 ] [ i ] , grid [ 1 ] [ i ] )
excl = excl_new
return max ( excl , incl )
</Code> |
python-cpp-FIND_ROTATION_COUNT_ROTATED_SORTED_ARRAY_1 | python-cpp | def countRotations ( arr , low , high ) :
if ( high < low ) :
return 0
if ( high == low ) :
return low
mid = low + ( high - low ) / 2
mid = int ( mid )
if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) :
return ( mid + 1 )
if ( mid > low and arr [ mid ] < arr [ mid - 1 ... | int countRotations ( int arr [ ] , int low , int high ) {
if ( high < low ) return 0 ;
if ( high == low ) return low ;
int mid = low + ( high - low ) / 2 ;
if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ;
if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ;
if ( arr [ high ]... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countRotations ( arr , low , high ) :
if ( high < low ) :
return 0
if ( high == low ) :
return low
mid = low + ( high - low ) / 2
mid = int ( mid )
if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) :
return ( mid + 1 )
if ( mid > low and arr [ mid ] < arr [ m... |
python-cpp-SUM_MATRIX_ELEMENT_ELEMENT_INTEGER_DIVISION_ROW_COLUMN_1 | python-cpp | def findSum ( n ) :
ans = 0 temp = 0
for i in range ( 1 , n + 1 ) :
if temp < n :
temp = i - 1
num = 1
while temp < n :
if temp + i <= n :
ans += i * num
else :
ans += ( n - temp ) * num
... | int findSum ( int n ) {
int ans = 0 , temp = 0 , num ;
for ( int i = 1 ;
i <= n && temp < n ;
i ++ ) {
temp = i - 1 ;
num = 1 ;
while ( temp < n ) {
if ( temp + i <= n ) ans += ( i * num ) ;
else ans += ( ( n - temp ) * num ) ;
temp += i ;
num ++ ;
}
}
return ans ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( n ) :
ans = 0 temp = 0
for i in range ( 1 , n + 1 ) :
if temp < n :
temp = i - 1
num = 1
while temp < n :
if temp + i <= n :
ans += i * num
else :
ans += ( n - temp ) * num
... |
python-cpp-CHECK_HALF_STRING_CHARACTER_FREQUENCY_CHARACTER | python-cpp | def checkCorrectOrNot ( s ) :
global MAX_CHAR
count1 = [ 0 ] * MAX_CHAR
count2 = [ 0 ] * MAX_CHAR
n = len ( s )
if n == 1 :
return true
i = 0 j = n - 1
while ( i < j ) :
count1 [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1
count2 [ ord ( s [ j ] ) - ord ( 'a' ) ] += 1
... | bool checkCorrectOrNot ( string s ) {
int count1 [ MAX_CHAR ] = {
0 }
;
int count2 [ MAX_CHAR ] = {
0 }
;
int n = s . length ( ) ;
if ( n == 1 ) return true ;
for ( int i = 0 , j = n - 1 ;
i < j ;
i ++ , j -- ) {
count1 [ s [ i ] - 'a' ] ++ ;
count... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def checkCorrectOrNot ( s ) :
global MAX_CHAR
count1 = [ 0 ] * MAX_CHAR
count2 = [ 0 ] * MAX_CHAR
n = len ( s )
if n == 1 :
return true
i = 0 j = n - 1
while ( i < j ) :
count1 [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1
count2 [ ord ( s [ j ] ) - ord ( 'a' ) ] += 1... |
python-cpp-SUM_FACTORS_NUMBER | python-cpp | def divSum ( n ) :
result = 0
for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) :
if ( n % i == 0 ) :
if ( i == ( n / i ) ) :
result = result + i
else :
result = result + ( i + n // i )
return ( result + n + 1 )
| int divSum ( int n ) {
int result = 0 ;
for ( int i = 2 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( i == ( n / i ) ) result += i ;
else result += ( i + n / i ) ;
}
}
return ( result + n + 1 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def divSum ( n ) :
result = 0
for i in range ( 2 , ( int ) ( math.sqrt ( n ) ) + 1 ) :
if ( n % i == 0 ) :
if ( i == ( n / i ) ) :
result = result + i
else :
result = result + ( i + n // i )
return ( result + n + 1 )
</Code> |
python-cpp-ROTATE_MATRIX_180_DEGREE | python-cpp | def rotateMatrix ( mat ) :
i = N - 1
while ( i >= 0 ) :
j = N - 1
while ( j >= 0 ) :
print ( mat [ i ] [ j ] , end = " " )
j = j - 1
print ( )
i = i - 1
| void rotateMatrix ( int mat [ ] [ N ] ) {
for ( int i = N - 1 ;
i >= 0 ;
i -- ) {
for ( int j = N - 1 ;
j >= 0 ;
j -- ) printf ( "%d " , mat [ i ] [ j ] ) ;
printf ( "\n" ) ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def rotateMatrix ( mat ) :
i = N - 1
while ( i >= 0 ) :
j = N - 1
while ( j >= 0 ) :
print ( mat [ i ] [ j ] , end = " " )
j = j - 1
print ( )
i = i - 1
</Code> |
python-cpp-PRINT_MATRIX_SPIRAL_FORM_STARTING_POINT | python-cpp | def printSpiral ( mat , r , c ) :
a = 0
b = 2
low_row = 0 if ( 0 > a ) else a
low_column = 0 if ( 0 > b ) else b - 1
high_row = r - 1 if ( ( a + 1 ) >= r ) else a + 1
high_column = c - 1 if ( ( b + 1 ) >= c ) else b + 1
while ( ( low_row > 0 - r and low_column > 0 - c ) ) :
i = low_c... | void printSpiral ( int mat [ ] [ MAX ] , int r , int c ) {
int i , a = 0 , b = 2 ;
int low_row = ( 0 > a ) ? 0 : a ;
int low_column = ( 0 > b ) ? 0 : b - 1 ;
int high_row = ( ( a + 1 ) >= r ) ? r - 1 : a + 1 ;
int high_column = ( ( b + 1 ) >= c ) ? c - 1 : b + 1 ;
while ( ( low_row > 0 - r && low_column > 0... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printSpiral ( mat , r , c ) :
a = 0
b = 2
low_row = 0 if ( 0 > a ) else a
low_column = 0 if ( 0 > b ) else b - 1
high_row = r - 1 if ( ( a + 1 ) >= r ) else a + 1
high_column = c - 1 if ( ( b + 1 ) >= c ) else b + 1
while ( ( low_row > 0 - r and low_column > 0 - c ) ) :
i ... |
python-cpp-COUNT_EVEN_LENGTH_BINARY_SEQUENCES_WITH_SAME_SUM_OF_FIRST_AND_SECOND_HALF_BITS_1 | python-cpp | def countSeq ( n ) :
nCr = 1
res = 1
for r in range ( 1 , n + 1 ) :
nCr = ( nCr * ( n + 1 - r ) ) / r ;
res += nCr * nCr ;
return res ;
| int countSeq ( int n ) {
int nCr = 1 , res = 1 ;
for ( int r = 1 ;
r <= n ;
r ++ ) {
nCr = ( nCr * ( n + 1 - r ) ) / r ;
res += nCr * nCr ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSeq ( n ) :
nCr = 1
res = 1
for r in range ( 1 , n + 1 ) :
nCr = ( nCr * ( n + 1 - r ) ) / r ;
res += nCr * nCr ;
return res ;
</Code> |
python-cpp-PROGRAM_CALCULATE_AREA_OCTAGON | python-cpp | def areaOctagon ( side ) :
return ( 2 * ( 1 + ( math.sqrt ( 2 ) ) ) * side * side )
| double areaOctagon ( double side ) {
return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def areaOctagon ( side ) :
return ( 2 * ( 1 + ( math.sqrt ( 2 ) ) ) * side * side )
</Code> |
python-cpp-MAXIMUM_CONSECUTIVE_NUMBERS_PRESENT_ARRAY | python-cpp | def findLongestConseqSubseq ( arr , n ) :
S = set ( )
for i in range ( n ) :
S.add ( arr [ i ] )
ans = 0
for i in range ( n ) :
if S.__contains__ ( arr [ i ] ) :
j = arr [ i ]
while ( S.__contains__ ( j ) ) :
j += 1
ans = max ( ans , j ... | int findLongestConseqSubseq ( int arr [ ] , int n ) {
unordered_set < int > S ;
for ( int i = 0 ;
i < n ;
i ++ ) S . insert ( arr [ i ] ) ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) {
int j = arr [ i ] ;
while ( S . find ( j ) != S ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLongestConseqSubseq ( arr , n ) :
S = set ( )
for i in range ( n ) :
S.add ( arr [ i ] )
ans = 0
for i in range ( n ) :
if S.__contains__ ( arr [ i ] ) :
j = arr [ i ]
while ( S.__contains__ ( j ) ) :
j += 1
ans = max ( a... |
python-cpp-SHORTEST_POSSIBLE_COMBINATION_TWO_STRINGS | python-cpp | def printSuperSeq ( a , b ) :
m = len ( a )
n = len ( b )
dp = [ [ 0 ] * ( n + 1 ) for i in range ( m + 1 ) ]
for i in range ( 0 , m + 1 ) :
for j in range ( 0 , n + 1 ) :
if not i :
dp [ i ] [ j ] = j
elif not j :
dp [ i ] [ j ] = i
... | void printSuperSeq ( string & a , string & b ) {
int m = a . length ( ) , n = b . length ( ) ;
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( ! i ) dp [ i ] [ j ] = j ;
else if ( ! j ) dp [ i ] [ j ] = i ;
else if ( a [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printSuperSeq ( a , b ) :
m = len ( a )
n = len ( b )
dp = [ [ 0 ] * ( n + 1 ) for i in range ( m + 1 ) ]
for i in range ( 0 , m + 1 ) :
for j in range ( 0 , n + 1 ) :
if not i :
dp [ i ] [ j ] = j
elif not j :
dp [ i ] [ j ] = i... |
python-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_2 | python-cpp | def numberOfPaths ( p , q ) :
dp = [ 1 for i in range ( q ) ]
for i in range ( p - 1 ) :
for j in range ( 1 , q ) :
dp [ j ] += dp [ j - 1 ]
return dp [ q - 1 ]
| int numberOfPaths ( int m , int n ) {
int dp [ n ] = {
1 }
;
dp [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 1 ;
j < n ;
j ++ ) {
dp [ j ] += dp [ j - 1 ] ;
}
}
return dp [ n - 1 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfPaths ( p , q ) :
dp = [ 1 for i in range ( q ) ]
for i in range ( p - 1 ) :
for j in range ( 1 , q ) :
dp [ j ] += dp [ j - 1 ]
return dp [ q - 1 ]
</Code> |
python-cpp-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_1 | python-cpp | def isPowerOfFour ( n ) :
count = 0
if ( n and ( not ( n & ( n - 1 ) ) ) ) :
while ( n > 1 ) :
n >>= 1
count += 1
if ( count % 2 == 0 ) :
return True
else :
return False
| bool isPowerOfFour ( unsigned int n ) {
int count = 0 ;
if ( n && ! ( n & ( n - 1 ) ) ) {
while ( n > 1 ) {
n >>= 1 ;
count += 1 ;
}
return ( count % 2 == 0 ) ? 1 : 0 ;
}
return 0 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPowerOfFour ( n ) :
count = 0
if ( n and ( not ( n & ( n - 1 ) ) ) ) :
while ( n > 1 ) :
n >>= 1
count += 1
if ( count % 2 == 0 ) :
return True
else :
return False
</Code> |
python-cpp-FIND_SUM_ODD_FACTORS_NUMBER | python-cpp | def sumofoddFactors ( n ) :
res = 1
while n % 2 == 0 :
n = n // 2
for i in range ( 3 , int ( math.sqrt ( n ) + 1 ) ) :
count = 0
curr_sum = 1
curr_term = 1
while n % i == 0 :
count += 1
n = n // i
curr_term *= i
curr_sum... | int sumofoddFactors ( int n ) {
int res = 1 ;
while ( n % 2 == 0 ) n = n / 2 ;
for ( int i = 3 ;
i <= sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 ;
int curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
curr_term *= i ;
curr_sum += curr_term ;
}
re... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sumofoddFactors ( n ) :
res = 1
while n % 2 == 0 :
n = n // 2
for i in range ( 3 , int ( math.sqrt ( n ) + 1 ) ) :
count = 0
curr_sum = 1
curr_term = 1
while n % i == 0 :
count += 1
n = n // i
curr_term *= i
c... |
python-cpp-FIND_THE_ELEMENT_BEFORE_WHICH_ALL_THE_ELEMENTS_ARE_SMALLER_THAN_IT_AND_AFTER_WHICH_ALL_ARE_GREATER_THAN_IT | python-cpp | def findElement ( arr , n ) :
leftMax = [ None ] * n
leftMax [ 0 ] = float ( '-inf' )
for i in range ( 1 , n ) :
leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] )
rightMin = float ( 'inf' )
for i in range ( n - 1 , - 1 , - 1 ) :
if leftMax [ i ] < arr [ i ] and rightMin > arr... | int findElement ( int arr [ ] , int n ) {
int leftMax [ n ] ;
leftMax [ 0 ] = INT_MIN ;
for ( int i = 1 ;
i < n ;
i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ;
int rightMin = INT_MAX ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( leftMax [ i ] < arr [ i ] && rightMin > arr [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findElement ( arr , n ) :
leftMax = [ None ] * n
leftMax [ 0 ] = float ( '-inf' )
for i in range ( 1 , n ) :
leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] )
rightMin = float ( 'inf' )
for i in range ( n - 1 , - 1 , - 1 ) :
if leftMax [ i ] < arr [ i ] and rightMi... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.