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-NUMBER_N_DIGIT_STEPPING_NUMBERS | python-cpp | def answer ( n ) :
dp = [ [ 0 for x in range ( 10 ) ] for y in range ( n + 1 ) ]
if ( n == 1 ) :
return 10
for j in range ( 10 ) :
dp [ 1 ] [ j ] = 1
for i in range ( 2 , n + 1 ) :
for j in range ( 10 ) :
if ( j == 0 ) :
dp [ i ] [ j ] = dp [ i - 1 ] [... | long long answer ( int n ) {
int dp [ n + 1 ] [ 10 ] ;
if ( n == 1 ) return 10 ;
for ( int j = 0 ;
j <= 9 ;
j ++ ) dp [ 1 ] [ j ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= 9 ;
j ++ ) {
if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ;
else if ( j == 9... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def answer ( n ) :
dp = [ [ 0 for x in range ( 10 ) ] for y in range ( n + 1 ) ]
if ( n == 1 ) :
return 10
for j in range ( 10 ) :
dp [ 1 ] [ j ] = 1
for i in range ( 2 , n + 1 ) :
for j in range ( 10 ) :
if ( j == 0 ) :
dp [ i ] [ j ] = dp [ i ... |
python-cpp-PARTITION_INTO_TWO_SUBARRAYS_OF_LENGTHS_K_AND_N_K_SUCH_THAT_THE_DIFFERENCE_OF_SUMS_IS_MAXIMUM | python-cpp | def maxDifference ( arr , N , k ) :
S = 0
S1 = 0
max_difference = 0
for i in range ( N ) :
S += arr [ i ]
arr.sort ( reverse = True )
M = max ( k , N - k )
for i in range ( M ) :
S1 += arr [ i ]
max_difference = S1 - ( S - S1 )
return max_difference
| int maxDifference ( int arr [ ] , int N , int k ) {
int M , S = 0 , S1 = 0 , max_difference = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) S += arr [ i ] ;
sort ( arr , arr + N , greater < int > ( ) ) ;
M = max ( k , N - k ) ;
for ( int i = 0 ;
i < M ;
i ++ ) S1 += arr [ i ] ;
max_difference = S1 - ( S - S1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxDifference ( arr , N , k ) :
S = 0
S1 = 0
max_difference = 0
for i in range ( N ) :
S += arr [ i ]
arr.sort ( reverse = True )
M = max ( k , N - k )
for i in range ( M ) :
S1 += arr [ i ]
max_difference = S1 - ( S - S1 )
return max_difference
</Code> |
python-cpp-MAXIMUM_SUM_SUBARRAY_REMOVING_ONE_ELEMENT | python-cpp | def maxSumSubarrayRemovingOneEle ( arr , n ) :
fw = [ 0 for k in range ( n ) ]
bw = [ 0 for k in range ( n ) ]
cur_max , max_so_far = arr [ 0 ] , arr [ 0 ]
for i in range ( n ) :
cur_max = max ( arr [ i ] , cur_max + arr [ i ] )
max_so_far = max ( max_so_far , cur_max )
fw [ i ] ... | int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) {
int fw [ n ] , bw [ n ] ;
int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ;
fw [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
cur_max = max ( arr [ i ] , cur_max + arr [ i ] ) ;
max_so_far = max ( max_so_far , cur_max ) ;
fw [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSumSubarrayRemovingOneEle ( arr , n ) :
fw = [ 0 for k in range ( n ) ]
bw = [ 0 for k in range ( n ) ]
cur_max , max_so_far = arr [ 0 ] , arr [ 0 ]
for i in range ( n ) :
cur_max = max ( arr [ i ] , cur_max + arr [ i ] )
max_so_far = max ( max_so_far , cur_max )
fw... |
python-cpp-MOVE_SPACES_FRONT_STRING_SINGLE_TRAVERSAL | python-cpp | def moveSpaceInFront ( s ) :
i = len ( s ) - 1
for j in range ( i , - 1 , - 1 ) :
if ( s [ j ] != ' ' ) :
s = s [ : i ] + s [ j ] + s [ i + 1 : ]
i -= 1
while ( i >= 0 ) :
s = s [ : i ] + ' ' + s [ i + 1 : ]
i -= 1
return s
| void moveSpaceInFront ( char str [ ] ) {
int i = strlen ( str ) ;
for ( int j = i ;
j >= 0 ;
j -- ) if ( str [ j ] != ' ' ) str [ i -- ] = str [ j ] ;
while ( i >= 0 ) str [ i -- ] = ' ' ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def moveSpaceInFront ( s ) :
i = len ( s ) - 1
for j in range ( i , - 1 , - 1 ) :
if ( s [ j ] != ' ' ) :
s = s [ : i ] + s [ j ] + s [ i + 1 : ]
i -= 1
while ( i >= 0 ) :
s = s [ : i ] + ' ' + s [ i + 1 : ]
i -= 1
return s
</Code> |
python-cpp-RECAMANS_SEQUENCE | python-cpp | def recaman ( n ) :
arr = [ 0 ] * n
arr [ 0 ] = 0
print ( arr [ 0 ] , end = ", " )
for i in range ( 1 , n ) :
curr = arr [ i - 1 ] - i
for j in range ( 0 , i ) :
if ( ( arr [ j ] == curr ) or curr < 0 ) :
curr = arr [ i - 1 ] + i
break
... | int recaman ( int n ) {
int arr [ n ] ;
arr [ 0 ] = 0 ;
printf ( "%d, " , arr [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = arr [ i - 1 ] - i ;
int j ;
for ( j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ j ] == curr ) || curr < 0 ) {
curr = arr [ i - 1 ] + i ;
bre... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def recaman ( n ) :
arr = [ 0 ] * n
arr [ 0 ] = 0
print ( arr [ 0 ] , end = ", " )
for i in range ( 1 , n ) :
curr = arr [ i - 1 ] - i
for j in range ( 0 , i ) :
if ( ( arr [ j ] == curr ) or curr < 0 ) :
curr = arr [ i - 1 ] + i
break
... |
python-cpp-MAXIMUM_POINTS_COLLECTED_BY_TWO_PERSONS_ALLOWED_TO_MEET_ONCE | python-cpp | def findMaxPoints ( A ) :
P1S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P1E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P2S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P2E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
for i in r... | int findMaxPoints ( int A [ ] [ M ] ) {
int P1S [ M + 1 ] [ N + 1 ] , P1E [ M + 1 ] [ N + 1 ] ;
memset ( P1S , 0 , sizeof ( P1S ) ) ;
memset ( P1E , 0 , sizeof ( P1E ) ) ;
int P2S [ M + 1 ] [ N + 1 ] , P2E [ M + 1 ] [ N + 1 ] ;
memset ( P2S , 0 , sizeof ( P2S ) ) ;
memset ( P2E , 0 , sizeof ( P2E ) ) ;
fo... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMaxPoints ( A ) :
P1S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P1E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P2S = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
P2E = [ [ 0 for i in range ( N + 2 ) ] for j in range ( M + 2 ) ]
for... |
python-cpp-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM | python-cpp | def KnapSack ( val , wt , n , W ) :
mat = [ [ 0 for i in range ( W + 1 ) ] for i in range ( 2 ) ]
i = 0
while i < n :
j = 0
if i % 2 == 0 :
while j < W :
j += 1
if wt [ i ] <= j :
mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ ... | int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int mat [ 2 ] [ W + 1 ] ;
memset ( mat , 0 , sizeof ( mat ) ) ;
int i = 0 ;
while ( i < n ) {
int j = 0 ;
if ( i % 2 != 0 ) {
while ( ++ j <= W ) {
if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def KnapSack ( val , wt , n , W ) :
mat = [ [ 0 for i in range ( W + 1 ) ] for i in range ( 2 ) ]
i = 0
while i < n :
j = 0
if i % 2 == 0 :
while j < W :
j += 1
if wt [ i ] <= j :
mat [ 1 ] [ j ] = max ( val [ i ] + mat [... |
python-cpp-MULTIPLICATIVE_INVERSE_UNDER_MODULO_M | python-cpp | def modInverse ( a , m ) :
a = a % m
for x in range ( 1 , m ) :
if ( ( a * x ) % m == 1 ) :
return x
return 1
| int modInverse ( int a , int m ) {
a = a % m ;
for ( int x = 1 ;
x < m ;
x ++ ) if ( ( a * x ) % m == 1 ) return x ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def modInverse ( a , m ) :
a = a % m
for x in range ( 1 , m ) :
if ( ( a * x ) % m == 1 ) :
return x
return 1
</Code> |
python-cpp-LENGTH_OF_THE_LONGEST_ARITHMATIC_PROGRESSION_IN_A_SORTED_ARRAY | python-cpp | def lenghtOfLongestAP ( set , n ) :
if ( n <= 2 ) :
return n
L = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
llap = 2
for i in range ( n ) :
L [ i ] [ n - 1 ] = 2
for j in range ( n - 2 , 0 , - 1 ) :
i = j - 1
k = j + 1
while ( i >= 0 and k <= n - 1 ) ... | int lenghtOfLongestAP ( int set [ ] , int n ) {
if ( n <= 2 ) return n ;
int L [ n ] [ n ] ;
int llap = 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) L [ i ] [ n - 1 ] = 2 ;
for ( int j = n - 2 ;
j >= 1 ;
j -- ) {
int i = j - 1 , k = j + 1 ;
while ( i >= 0 && k <= n - 1 ) {
if ( set [ i ] + set [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lenghtOfLongestAP ( set , n ) :
if ( n <= 2 ) :
return n
L = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
llap = 2
for i in range ( n ) :
L [ i ] [ n - 1 ] = 2
for j in range ( n - 2 , 0 , - 1 ) :
i = j - 1
k = j + 1
while ( i >= 0 and k <= n... |
python-cpp-COUNT_BINARY_DIGIT_NUMBERS_SMALLER_N | python-cpp | def countOfBinaryNumberLessThanN ( N ) :
q = deque ( )
q.append ( 1 )
cnt = 0
while ( q ) :
t = q.popleft ( )
if ( t <= N ) :
cnt = cnt + 1
q.append ( t * 10 )
q.append ( t * 10 + 1 )
return cnt
| int countOfBinaryNumberLessThanN ( int N ) {
queue < int > q ;
q . push ( 1 ) ;
int cnt = 0 ;
int t ;
while ( ! q . empty ( ) ) {
t = q . front ( ) ;
q . pop ( ) ;
if ( t <= N ) {
cnt ++ ;
q . push ( t * 10 ) ;
q . push ( t * 10 + 1 ) ;
}
}
return cnt ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countOfBinaryNumberLessThanN ( N ) :
q = deque ( )
q.append ( 1 )
cnt = 0
while ( q ) :
t = q.popleft ( )
if ( t <= N ) :
cnt = cnt + 1
q.append ( t * 10 )
q.append ( t * 10 + 1 )
return cnt
</Code> |
python-cpp-FIND_INDEX_GIVEN_FIBONACCI_NUMBER_CONSTANT_TIME | python-cpp | def findIndex ( n ) :
if ( n <= 1 ) :
return n
a = 0
b = 1
c = 1
res = 1
while ( c < n ) :
c = a + b
res = res + 1
a = b
b = c
return res
| int findIndex ( int n ) {
if ( n <= 1 ) return n ;
int a = 0 , b = 1 , c = 1 ;
int res = 1 ;
while ( c < n ) {
c = a + b ;
res ++ ;
a = b ;
b = c ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findIndex ( n ) :
if ( n <= 1 ) :
return n
a = 0
b = 1
c = 1
res = 1
while ( c < n ) :
c = a + b
res = res + 1
a = b
b = c
return res
</Code> |
python-cpp-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL_1 | python-cpp | def substringConversions ( str1 , k , b ) :
for i in range ( 0 , len ( str1 ) - k + 1 ) :
sub = str1 [ i : k + i ]
Sum = 0
counter = 0
for i in range ( len ( sub ) - 1 , - 1 , - 1 ) :
Sum = ( Sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) )
... | int substringConversions ( string str , int k , int b ) {
int i = 0 , sum = 0 , counter = k - 1 ;
for ( i ;
i < k ;
i ++ ) {
sum = sum + ( ( str . at ( i ) - '0' ) * pow ( b , counter ) ) ;
counter -- ;
}
cout << sum << " " ;
int prev = sum ;
sum = 0 , counter = 0 ;
for ( i ;
i < str . size ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def substringConversions ( str1 , k , b ) :
for i in range ( 0 , len ( str1 ) - k + 1 ) :
sub = str1 [ i : k + i ]
Sum = 0
counter = 0
for i in range ( len ( sub ) - 1 , - 1 , - 1 ) :
Sum = ( Sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) )
... |
python-cpp-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE_1 | python-cpp | def maxSumPairWithDifferenceLessThanK ( arr , N , k ) :
maxSum = 0
arr.sort ( )
i = N - 1
while ( i >= 0 ) :
if ( arr [ i ] - arr [ i - 1 ] < k ) :
maxSum += arr [ i ]
maxSum += arr [ i - 1 ]
i -= 1
i -= 1
return maxSum
| int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) {
int maxSum = 0 ;
sort ( arr , arr + N ) ;
for ( int i = N - 1 ;
i > 0 ;
-- i ) {
if ( arr [ i ] - arr [ i - 1 ] < k ) {
maxSum += arr [ i ] ;
maxSum += arr [ i - 1 ] ;
-- i ;
}
}
return maxSum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSumPairWithDifferenceLessThanK ( arr , N , k ) :
maxSum = 0
arr.sort ( )
i = N - 1
while ( i >= 0 ) :
if ( arr [ i ] - arr [ i - 1 ] < k ) :
maxSum += arr [ i ]
maxSum += arr [ i - 1 ]
i -= 1
i -= 1
return maxSum
</Code> |
python-cpp-LONGEST_REPEATED_SUBSEQUENCE_1 | python-cpp | def longestRepeatedSubSeq ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ... | string longestRepeatedSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= n ;
j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] &... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def longestRepeatedSubSeq ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [... |
python-cpp-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP_1 | python-cpp | def isHeap ( arr , n ) :
for i in range ( int ( ( n - 2 ) / 2 ) + 1 ) :
if arr [ 2 * i + 1 ] > arr [ i ] :
return False
if ( 2 * i + 2 < n and arr [ 2 * i + 2 ] > arr [ i ] ) :
return False
return True
| bool isHeap ( int arr [ ] , int n ) {
for ( int i = 0 ;
i <= ( n - 2 ) / 2 ;
i ++ ) {
if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ;
if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > 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 isHeap ( arr , n ) :
for i in range ( int ( ( n - 2 ) / 2 ) + 1 ) :
if arr [ 2 * i + 1 ] > arr [ i ] :
return False
if ( 2 * i + 2 < n and arr [ 2 * i + 2 ] > arr [ i ] ) :
return False
return True
</Code> |
python-cpp-MINIMUM_COST_SORT_MATRIX_NUMBERS_0_N2_1 | python-cpp | def calculateEnergy ( mat , n ) :
tot_energy = 0
for i in range ( n ) :
for j in range ( n ) :
q = mat [ i ] [ j ] // n
i_des = q
j_des = mat [ i ] [ j ] - ( n * q )
tot_energy += ( abs ( i_des - i ) + abs ( j_des - j ) )
return tot_energy
| int calculateEnergy ( int mat [ SIZE ] [ SIZE ] , int n ) {
int i_des , j_des , q ;
int tot_energy = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
q = mat [ i ] [ j ] / n ;
i_des = q ;
j_des = mat [ i ] [ j ] - ( n * q ) ;
tot_energy += abs ( i_d... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def calculateEnergy ( mat , n ) :
tot_energy = 0
for i in range ( n ) :
for j in range ( n ) :
q = mat [ i ] [ j ] // n
i_des = q
j_des = mat [ i ] [ j ] - ( n * q )
tot_energy += ( abs ( i_des - i ) + abs ( j_des - j ) )
return tot_energy
</Co... |
python-cpp-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS | python-cpp | def rearrange ( a , size ) :
positive = 0
negative = 1
while ( True ) :
while ( positive < size and a [ positive ] >= 0 ) :
positive = positive + 2
while ( negative < size and a [ negative ] <= 0 ) :
negative = negative + 2
if ( positive < size and negative < ... | void rearrange ( int a [ ] , int size ) {
int positive = 0 , negative = 1 ;
while ( true ) {
while ( positive < size && a [ positive ] >= 0 ) positive += 2 ;
while ( negative < size && a [ negative ] <= 0 ) negative += 2 ;
if ( positive < size && negative < size ) swap ( a [ positive ] , a [ negative ] ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def rearrange ( a , size ) :
positive = 0
negative = 1
while ( True ) :
while ( positive < size and a [ positive ] >= 0 ) :
positive = positive + 2
while ( negative < size and a [ negative ] <= 0 ) :
negative = negative + 2
if ( positive < size and nega... |
python-cpp-PRINT_MATRIX_ANTISPIRAL_FORM | python-cpp | def antiSpiralTraversal ( m , n , a ) :
k = 0
l = 0
stk = [ ]
while ( k <= m and l <= n ) :
for i in range ( l , n + 1 ) :
stk.append ( a [ k ] [ i ] )
k += 1
for i in range ( k , m + 1 ) :
stk.append ( a [ i ] [ n ] )
n -= 1
if ( k <= m ) ... | void antiSpiralTraversal ( int m , int n , int a [ R ] [ C ] ) {
int i , k = 0 , l = 0 ;
stack < int > stk ;
while ( k <= m && l <= n ) {
for ( i = l ;
i <= n ;
++ i ) stk . push ( a [ k ] [ i ] ) ;
k ++ ;
for ( i = k ;
i <= m ;
++ i ) stk . push ( a [ i ] [ n ] ) ;
n -- ;
if (... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def antiSpiralTraversal ( m , n , a ) :
k = 0
l = 0
stk = [ ]
while ( k <= m and l <= n ) :
for i in range ( l , n + 1 ) :
stk.append ( a [ k ] [ i ] )
k += 1
for i in range ( k , m + 1 ) :
stk.append ( a [ i ] [ n ] )
n -= 1
if ( k ... |
python-cpp-RETURN_A_PAIR_WITH_MAXIMUM_PRODUCT_IN_ARRAY_OF_INTEGERS_1 | python-cpp | def maxProduct ( arr , n ) :
if ( n < 2 ) :
print ( "No pairs exists" )
return
if ( n == 2 ) :
print ( arr [ 0 ] , " " , arr [ 1 ] )
return
posa = 0
posb = 0
nega = 0
negb = 0
for i in range ( n ) :
if ( arr [ i ] > posa ) :
posb = posa
... | void maxProduct ( int arr [ ] , int n ) {
if ( n < 2 ) {
cout << "No pairs exists\n" ;
return ;
}
if ( n == 2 ) {
cout << arr [ 0 ] << " " << arr [ 1 ] << endl ;
return ;
}
int posa = INT_MIN , posb = INT_MIN ;
int nega = INT_MIN , negb = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxProduct ( arr , n ) :
if ( n < 2 ) :
print ( "No pairs exists" )
return
if ( n == 2 ) :
print ( arr [ 0 ] , " " , arr [ 1 ] )
return
posa = 0
posb = 0
nega = 0
negb = 0
for i in range ( n ) :
if ( arr [ i ] > posa ) :
posb = p... |
python-cpp-MAXIMIZE_ARRAY_ELEMENTS_UPTO_GIVEN_NUMBER | python-cpp | def findMaxVal ( arr , n , num , maxLimit ) :
ind = - 1
val = - 1
dp = [ [ 0 for i in range ( maxLimit + 1 ) ] for j in range ( n ) ]
for ind in range ( n ) :
for val in range ( maxLimit + 1 ) :
if ( ind == 0 ) :
if ( num - arr [ ind ] == val or num + arr [ ind ] == v... | int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) {
int ind ;
int val ;
int dp [ n ] [ maxLimit + 1 ] ;
for ( ind = 0 ;
ind < n ;
ind ++ ) {
for ( val = 0 ;
val <= maxLimit ;
val ++ ) {
if ( ind == 0 ) {
if ( num - arr [ ind ] == val || num + arr [ ind ] == val ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMaxVal ( arr , n , num , maxLimit ) :
ind = - 1
val = - 1
dp = [ [ 0 for i in range ( maxLimit + 1 ) ] for j in range ( n ) ]
for ind in range ( n ) :
for val in range ( maxLimit + 1 ) :
if ( ind == 0 ) :
if ( num - arr [ ind ] == val or num + arr [ ind... |
python-cpp-PROGRAM_FOR_DEADLOCK_FREE_CONDITION_IN_OPERATING_SYSTEM | python-cpp | def Resources ( process , need ) :
minResources = 0
minResources = process * ( need - 1 ) + 1
return minResources
| int Resources ( int process , int need ) {
int minResources = 0 ;
minResources = process * ( need - 1 ) + 1 ;
return minResources ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def Resources ( process , need ) :
minResources = 0
minResources = process * ( need - 1 ) + 1
return minResources
</Code> |
python-cpp-BUBBLE_SORT | python-cpp | def bubbleSort ( arr ) :
n = len ( arr )
for i in range ( n ) :
for j in range ( 0 , n - i - 1 ) :
if arr [ j ] > arr [ j + 1 ] :
arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ]
| void bubbleSort ( int arr [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) for ( j = 0 ;
j < n - i - 1 ;
j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) swap ( & arr [ j ] , & arr [ j + 1 ] ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def bubbleSort ( arr ) :
n = len ( arr )
for i in range ( n ) :
for j in range ( 0 , n - i - 1 ) :
if arr [ j ] > arr [ j + 1 ] :
arr [ j ] , arr [ j + 1 ] = arr [ j + 1 ] , arr [ j ]
</Code> |
python-cpp-SHORTEST_COMMON_SUPERSEQUENCE_1 | python-cpp | def superSeq ( X , Y , m , n ) :
dp = [ [ 0 ] * ( n + 2 ) for i in range ( m + 2 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if ( not i ) : dp [ i ] [ j ] = j
elif ( not j ) : dp [ i ] [ j ] = i
elif ( X [ i - 1 ] == Y [ j - 1 ] ) :
dp [ ... | int superSeq ( char * X , char * Y , int m , int n ) {
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 ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i ] [ j ] = 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def superSeq ( X , Y , m , n ) :
dp = [ [ 0 ] * ( n + 2 ) for i in range ( m + 2 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if ( not i ) : dp [ i ] [ j ] = j
elif ( not j ) : dp [ i ] [ j ] = i
elif ( X [ i - 1 ] == Y [ j - 1 ] ) :
... |
python-cpp-NUMBER_NON_NEGATIVE_INTEGRAL_SOLUTIONS_B_C_N | python-cpp | def countIntegralSolutions ( n ) :
result = 0
for i in range ( n + 1 ) :
for j in range ( n + 1 ) :
for k in range ( n + 1 ) :
if i + j + k == n :
result += 1
return result
| int countIntegralSolutions ( int n ) {
int result = 0 ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n - i ;
j ++ ) for ( int k = 0 ;
k <= ( n - i - j ) ;
k ++ ) if ( i + j + k == n ) result ++ ;
return result ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countIntegralSolutions ( n ) :
result = 0
for i in range ( n + 1 ) :
for j in range ( n + 1 ) :
for k in range ( n + 1 ) :
if i + j + k == n :
result += 1
return result
</Code> |
python-cpp-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY_2 | python-cpp | def minJumps ( arr , n ) :
jumps = [ 0 for i in range ( n ) ]
for i in range ( n - 2 , - 1 , - 1 ) :
if ( arr [ i ] == 0 ) :
jumps [ i ] = float ( 'inf' )
elif ( arr [ i ] >= n - i - 1 ) :
jumps [ i ] = 1
else :
min = float ( 'inf' )
for j ... | int minJumps ( int arr [ ] , int n ) {
int * jumps = new int [ n ] ;
int min ;
jumps [ n - 1 ] = 0 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] == 0 ) jumps [ i ] = INT_MAX ;
else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ;
else {
min = INT_MAX ;
for ( int j = i + 1 ;... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minJumps ( arr , n ) :
jumps = [ 0 for i in range ( n ) ]
for i in range ( n - 2 , - 1 , - 1 ) :
if ( arr [ i ] == 0 ) :
jumps [ i ] = float ( 'inf' )
elif ( arr [ i ] >= n - i - 1 ) :
jumps [ i ] = 1
else :
min = float ( 'inf' )
... |
python-cpp-FIND_MINIMUM_ADJUSTMENT_COST_OF_AN_ARRAY | python-cpp | def minAdjustmentCost ( A , n , target ) :
dp = [ [ 0 for i in range ( M + 1 ) ] for i in range ( n ) ]
for j in range ( M + 1 ) :
dp [ 0 ] [ j ] = abs ( j - A [ 0 ] )
for i in range ( 1 , n ) :
for j in range ( M + 1 ) :
dp [ i ] [ j ] = 100000000
for k in range ( ma... | int minAdjustmentCost ( int A [ ] , int n , int target ) {
int dp [ n ] [ M + 1 ] ;
for ( int j = 0 ;
j <= M ;
j ++ ) dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= M ;
j ++ ) {
dp [ i ] [ j ] = INT_MAX ;
for ( int k = max ( j - tar... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minAdjustmentCost ( A , n , target ) :
dp = [ [ 0 for i in range ( M + 1 ) ] for i in range ( n ) ]
for j in range ( M + 1 ) :
dp [ 0 ] [ j ] = abs ( j - A [ 0 ] )
for i in range ( 1 , n ) :
for j in range ( M + 1 ) :
dp [ i ] [ j ] = 100000000
for k in ran... |
python-cpp-KTH_SMALLESTLARGEST_ELEMENT_UNSORTED_ARRAY | python-cpp | def kthSmallest ( arr , n , k ) :
arr.sort ( )
return arr [ k - 1 ]
| int kthSmallest ( int arr [ ] , int n , int k ) {
sort ( arr , arr + n ) ;
return arr [ k - 1 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def kthSmallest ( arr , n , k ) :
arr.sort ( )
return arr [ k - 1 ]
</Code> |
python-cpp-FIND_THE_MINIMUM_COST_TO_REACH_A_DESTINATION_WHERE_EVERY_STATION_IS_CONNECTED_IN_ONE_DIRECTION | python-cpp | def minCost ( cost ) :
dist = [ 0 for i in range ( N ) ]
for i in range ( N ) :
dist [ i ] = INF
dist [ 0 ] = 0
for i in range ( N ) :
for j in range ( i + 1 , N ) :
if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) :
dist [ j ] = dist [ i ] + cost [ i ] [ j ]... | INT_MAX int minCost ( int cost [ ] [ N ] ) {
int dist [ N ] ;
for ( int i = 0 ;
i < N ;
i ++ ) dist [ i ] = INF ;
dist [ 0 ] = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ;
re... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minCost ( cost ) :
dist = [ 0 for i in range ( N ) ]
for i in range ( N ) :
dist [ i ] = INF
dist [ 0 ] = 0
for i in range ( N ) :
for j in range ( i + 1 , N ) :
if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) :
dist [ j ] = dist [ i ] + cost [ i ... |
python-cpp-REVERSE_STRING_WITHOUT_USING_ANY_TEMPORARY_VARIABLE | python-cpp | def reversingString ( str , start , end ) :
while ( start < end ) :
str = ( str [ : start ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ start + 1 : ] )
str = ( str [ : end ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ end + 1 : ] )
str = ( str [ : start ] ... | string reversingString ( string str , int start , int end ) {
while ( start < end ) {
str [ start ] ^= str [ end ] ;
str [ end ] ^= str [ start ] ;
str [ start ] ^= str [ end ] ;
++ start ;
-- end ;
}
return str ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def reversingString ( str , start , end ) :
while ( start < end ) :
str = ( str [ : start ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ start + 1 : ] )
str = ( str [ : end ] + chr ( ord ( str [ start ] ) ^ ord ( str [ end ] ) ) + str [ end + 1 : ] )
str = ( str [ : s... |
python-cpp-MINIMAL_MOVES_FORM_STRING_ADDING_CHARACTERS_APPENDING_STRING | python-cpp | def minimalSteps ( s , n ) :
dp = [ INT_MAX for i in range ( n ) ]
s1 = ""
s2 = ""
dp [ 0 ] = 1
s1 += s [ 0 ]
for i in range ( 1 , n ) :
s1 += s [ i ]
s2 = s [ i + 1 : i + 1 + i + 1 ]
dp [ i ] = min ( dp [ i ] , dp [ i - 1 ] + 1 )
if ( s1 == s2 ) :
dp ... | int minimalSteps ( string s , int n ) {
int dp [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = INT_MAX ;
string s1 = "" , s2 = "" ;
dp [ 0 ] = 1 ;
s1 += s [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
s1 += s [ i ] ;
s2 = s . substr ( i + 1 , i + 1 ) ;
dp [ i ] = min ( dp [ i ] , dp [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minimalSteps ( s , n ) :
dp = [ INT_MAX for i in range ( n ) ]
s1 = ""
s2 = ""
dp [ 0 ] = 1
s1 += s [ 0 ]
for i in range ( 1 , n ) :
s1 += s [ i ]
s2 = s [ i + 1 : i + 1 + i + 1 ]
dp [ i ] = min ( dp [ i ] , dp [ i - 1 ] + 1 )
if ( s1 == s2 ) :
... |
python-cpp-MINIMUM_COST_TO_FILL_GIVEN_WEIGHT_IN_A_BAG | python-cpp | def MinimumCost ( cost , n , W ) :
val = list ( )
wt = list ( )
size = 0
for i in range ( n ) :
if ( cost [ i ] != - 1 ) :
val.append ( cost [ i ] )
wt.append ( i + 1 )
size += 1
n = size
min_cost = [ [ 0 for i in range ( W + 1 ) ] for j in range ( n +... | int MinimumCost ( int cost [ ] , int n , int W ) {
vector < int > val , wt ;
int size = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( cost [ i ] != - 1 ) {
val . push_back ( cost [ i ] ) ;
wt . push_back ( i + 1 ) ;
size ++ ;
}
}
n = size ;
int min_cost [ n + 1 ] [ W + 1 ] ;
fo... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def MinimumCost ( cost , n , W ) :
val = list ( )
wt = list ( )
size = 0
for i in range ( n ) :
if ( cost [ i ] != - 1 ) :
val.append ( cost [ i ] )
wt.append ( i + 1 )
size += 1
n = size
min_cost = [ [ 0 for i in range ( W + 1 ) ] for j in rang... |
python-cpp-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER | python-cpp | def bitonicGenerator ( arr , n ) :
evenArr = [ ]
oddArr = [ ]
for i in range ( n ) :
if ( ( i % 2 ) == 0 ) :
evenArr.append ( arr [ i ] )
else :
oddArr.append ( arr [ i ] )
evenArr = sorted ( evenArr )
oddArr = sorted ( oddArr )
oddArr = oddArr [ : : - 1 ]... | void bitonicGenerator ( int arr [ ] , int n ) {
vector < int > evenArr ;
vector < int > oddArr ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ! ( i % 2 ) ) evenArr . push_back ( arr [ i ] ) ;
else oddArr . push_back ( arr [ i ] ) ;
}
sort ( evenArr . begin ( ) , evenArr . end ( ) ) ;
sort ( oddArr . ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def bitonicGenerator ( arr , n ) :
evenArr = [ ]
oddArr = [ ]
for i in range ( n ) :
if ( ( i % 2 ) == 0 ) :
evenArr.append ( arr [ i ] )
else :
oddArr.append ( arr [ i ] )
evenArr = sorted ( evenArr )
oddArr = sorted ( oddArr )
oddArr = oddArr [ : ... |
python-cpp-COUNT_DIVISIBLE_PAIRS_ARRAY | python-cpp | def countDivisibles ( arr , n ) :
res = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) :
res += 1
return res
| int countDivisibles ( int arr [ ] , int n ) {
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countDivisibles ( arr , n ) :
res = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) :
res += 1
return res
</Code> |
python-cpp-DYNAMIC_PROGRAMMING_SET_3_LONGEST_INCREASING_SUBSEQUENCE | python-cpp | def lis ( arr ) :
global maximum
n = len ( arr )
maximum = 1
_lis ( arr , n )
return maximum
| int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lis ( arr ) :
global maximum
n = len ( arr )
maximum = 1
_lis ( arr , n )
return maximum
</Code> |
python-cpp-FIND_FIRST_REPEATING_ELEMENT_ARRAY_INTEGERS | python-cpp | def printFirstRepeating ( arr , n ) :
Min = - 1
myset = dict ( )
for i in range ( n - 1 , - 1 , - 1 ) :
if arr [ i ] in myset.keys ( ) :
Min = i
else :
myset [ arr [ i ] ] = 1
if ( Min != - 1 ) :
print ( "The first repeating element is" , arr [ Min ] )
... | void printFirstRepeating ( int arr [ ] , int n ) {
int min = - 1 ;
set < int > myset ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( myset . find ( arr [ i ] ) != myset . end ( ) ) min = i ;
else myset . insert ( arr [ i ] ) ;
}
if ( min != - 1 ) cout << "The first repeating element is " << arr [ m... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printFirstRepeating ( arr , n ) :
Min = - 1
myset = dict ( )
for i in range ( n - 1 , - 1 , - 1 ) :
if arr [ i ] in myset.keys ( ) :
Min = i
else :
myset [ arr [ i ] ] = 1
if ( Min != - 1 ) :
print ( "The first repeating element is" , arr [ Min ... |
python-cpp-FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_CHANGE | python-cpp | def minCoins ( coins , m , V ) :
if ( V == 0 ) :
return 0
res = sys.maxsize
for i in range ( 0 , m ) :
if ( coins [ i ] <= V ) :
sub_res = minCoins ( coins , m , V - coins [ i ] )
if ( sub_res != sys.maxsize and sub_res + 1 < res ) :
res = sub_res + 1
... | int minCoins ( int coins [ ] , int m , int V ) {
if ( V == 0 ) return 0 ;
int res = INT_MAX ;
for ( int i = 0 ;
i < m ;
i ++ ) {
if ( coins [ i ] <= V ) {
int sub_res = minCoins ( coins , m , V - coins [ i ] ) ;
if ( sub_res != INT_MAX && sub_res + 1 < res ) res = sub_res + 1 ;
}
}
ret... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minCoins ( coins , m , V ) :
if ( V == 0 ) :
return 0
res = sys.maxsize
for i in range ( 0 , m ) :
if ( coins [ i ] <= V ) :
sub_res = minCoins ( coins , m , V - coins [ i ] )
if ( sub_res != sys.maxsize and sub_res + 1 < res ) :
res = sub_r... |
python-cpp-FIND_PAIRS_GIVEN_SUM_ELEMENTS_PAIR_DIFFERENT_ROWS | python-cpp | def pairSum ( mat , n , sum ) :
for i in range ( n ) :
mat [ i ].sort ( )
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
left = 0
right = n - 1
while ( left < n and right >= 0 ) :
if ( ( mat [ i ] [ left ] + mat [ j ] [ right ] ) == ... | void pairSum ( int mat [ ] [ MAX ] , int n , int sum ) {
for ( int i = 0 ;
i < n ;
i ++ ) sort ( mat [ i ] , mat [ i ] + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int left = 0 , right = n - 1 ;
while ( left < n && right >= 0 ) {
if ( ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def pairSum ( mat , n , sum ) :
for i in range ( n ) :
mat [ i ].sort ( )
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
left = 0
right = n - 1
while ( left < n and right >= 0 ) :
if ( ( mat [ i ] [ left ] + mat [ j ] [ right ... |
python-cpp-MINIMUM_COST_CONNECT_WEIGHTED_NODES_REPRESENTED_ARRAY | python-cpp | def minimum_cost ( a , n ) :
mn = sys.maxsize
sum = 0
for i in range ( n ) :
mn = min ( a [ i ] , mn )
sum += a [ i ]
return mn * ( sum - mn )
| int minimum_cost ( int a [ ] , int n ) {
int mn = INT_MAX ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
mn = min ( a [ i ] , mn ) ;
sum += a [ i ] ;
}
return mn * ( sum - mn ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minimum_cost ( a , n ) :
mn = sys.maxsize
sum = 0
for i in range ( n ) :
mn = min ( a [ i ] , mn )
sum += a [ i ]
return mn * ( sum - mn )
</Code> |
python-cpp-DYNAMIC_PROGRAMMING_SET_12_LONGEST_PALINDROMIC_SUBSEQUENCE | python-cpp | def lps ( str ) :
n = len ( str )
L = [ [ 0 for x in range ( n ) ] for x in range ( n ) ]
for i in range ( n ) :
L [ i ] [ i ] = 1
for cl in range ( 2 , n + 1 ) :
for i in range ( n - cl + 1 ) :
j = i + cl - 1
if str [ i ] == str [ j ] and cl == 2 :
... | int lps ( char * str ) {
int n = strlen ( str ) ;
int i , j , cl ;
int L [ n ] [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) L [ i ] [ i ] = 1 ;
for ( cl = 2 ;
cl <= n ;
cl ++ ) {
for ( i = 0 ;
i < n - cl + 1 ;
i ++ ) {
j = i + cl - 1 ;
if ( str [ i ] == str [ j ] && cl == 2 ) L [ i ] [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lps ( str ) :
n = len ( str )
L = [ [ 0 for x in range ( n ) ] for x in range ( n ) ]
for i in range ( n ) :
L [ i ] [ i ] = 1
for cl in range ( 2 , n + 1 ) :
for i in range ( n - cl + 1 ) :
j = i + cl - 1
if str [ i ] == str [ j ] and cl == 2 :
... |
python-cpp-PROGRAM_CENSOR_WORD_ASTERISKS_SENTENCE | python-cpp | def censor ( text , word ) :
word_list = text.split ( )
result = ''
stars = '*' * len ( word )
count = 0
index = 0
for i in word_list :
if i == word :
word_list [ index ] = stars
index += 1
result = ' '.join ( word_list )
return result
| string censor ( string text , string word ) {
vector < string > word_list ;
boost :: split ( word_list , text , boost :: is_any_of ( "\\ +" ) ) ;
string result = "" ;
string stars = "" ;
for ( int i = 0 ;
i < word . size ( ) ;
i ++ ) stars += '*' ;
int index = 0 ;
for ( string i : word_list ) {
if... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def censor ( text , word ) :
word_list = text.split ( )
result = ''
stars = '*' * len ( word )
count = 0
index = 0
for i in word_list :
if i == word :
word_list [ index ] = stars
index += 1
result = ' '.join ( word_list )
return result
</Code> |
python-cpp-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT | python-cpp | def isPowerOfFour ( n ) :
if ( n == 0 ) :
return False
while ( n != 1 ) :
if ( n % 4 != 0 ) :
return False
n = n // 4
return True
| public : bool isPowerOfFour ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 4 != 0 ) return 0 ;
n = n / 4 ;
}
return 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPowerOfFour ( n ) :
if ( n == 0 ) :
return False
while ( n != 1 ) :
if ( n % 4 != 0 ) :
return False
n = n // 4
return True
</Code> |
python-cpp-AREA_OF_THE_CIRCLE_THAT_HAS_A_SQUARE_AND_A_CIRCLE_INSCRIBED_IN_IT | python-cpp | def getArea ( a ) :
area = ( math.pi * a * a ) / 4
return area
| float getArea ( int a ) {
float area = ( M_PI * a * a ) / 4.0 ;
return area ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getArea ( a ) :
area = ( math.pi * a * a ) / 4
return area
</Code> |
python-cpp-SUM_MANHATTAN_DISTANCES_PAIRS_POINTS | python-cpp | def distancesum ( x , y , n ) :
sum = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) )
return sum
| int distancesum ( int x [ ] , int y [ ] , int n ) {
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) ;
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def distancesum ( x , y , n ) :
sum = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) )
return sum
</Code> |
python-cpp-FIND_ROW_NUMBER_BINARY_MATRIX_MAXIMUM_NUMBER_1S | python-cpp | def findMax ( arr ) :
row = 0
j = N - 1
for i in range ( 0 , N ) :
while ( arr [ i ] [ j ] == 1 and j >= 0 ) :
row = i
j -= 1
print ( "Row number = " , row + 1 , ", MaxCount = " , N - 1 - j )
| void findMax ( int arr [ ] [ N ] ) {
int row = 0 , i , j ;
for ( i = 0 , j = N - 1 ;
i < N ;
i ++ ) {
while ( arr [ i ] [ j ] == 1 && j >= 0 ) {
row = i ;
j -- ;
}
}
cout << "Row number = " << row + 1 ;
cout << ", MaxCount = " << N - 1 - j ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMax ( arr ) :
row = 0
j = N - 1
for i in range ( 0 , N ) :
while ( arr [ i ] [ j ] == 1 and j >= 0 ) :
row = i
j -= 1
print ( "Row number = " , row + 1 , ", MaxCount = " , N - 1 - j )
</Code> |
python-cpp-LAST_NON_ZERO_DIGIT_FACTORIAL | python-cpp | def lastNon0Digit ( n ) :
if ( n < 10 ) :
return dig [ n ]
if ( ( ( n // 10 ) % 10 ) % 2 == 0 ) :
return ( 6 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10
else :
return ( 4 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10
return 0
| int lastNon0Digit ( int n ) {
if ( n < 10 ) return dig [ n ] ;
if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lastNon0Digit ( n ) :
if ( n < 10 ) :
return dig [ n ]
if ( ( ( n // 10 ) % 10 ) % 2 == 0 ) :
return ( 6 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10
else :
return ( 4 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10
return 0
</Code> |
python-cpp-EVEN_FIBONACCI_NUMBERS_SUM | python-cpp | def evenFibSum ( limit ) :
if ( limit < 2 ) :
return 0
ef1 = 0
ef2 = 2
sm = ef1 + ef2
while ( ef2 <= limit ) :
ef3 = 4 * ef2 + ef1
if ( ef3 > limit ) :
break
ef1 = ef2
ef2 = ef3
sm = sm + ef2
return sm
| int evenFibSum ( int limit ) {
if ( limit < 2 ) return 0 ;
long long int ef1 = 0 , ef2 = 2 ;
long long int sum = ef1 + ef2 ;
while ( ef2 <= limit ) {
long long int ef3 = 4 * ef2 + ef1 ;
if ( ef3 > limit ) break ;
ef1 = ef2 ;
ef2 = ef3 ;
sum += ef2 ;
}
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def evenFibSum ( limit ) :
if ( limit < 2 ) :
return 0
ef1 = 0
ef2 = 2
sm = ef1 + ef2
while ( ef2 <= limit ) :
ef3 = 4 * ef2 + ef1
if ( ef3 > limit ) :
break
ef1 = ef2
ef2 = ef3
sm = sm + ef2
return sm
</Code> |
python-cpp-SEARCH_AN_ELEMENT_IN_AN_ARRAY_WHERE_DIFFERENCE_BETWEEN_ADJACENT_ELEMENTS_IS_1 | python-cpp | def search ( arr , n , x ) :
i = 0
while ( i < n ) :
if ( arr [ i ] == x ) :
return i
i = i + abs ( arr [ i ] - x )
print ( "number is not present!" )
return - 1
| int search ( int arr [ ] , int n , int x ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + abs ( arr [ i ] - x ) ;
}
cout << "number is not present!" ;
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 ) :
if ( arr [ i ] == x ) :
return i
i = i + abs ( arr [ i ] - x )
print ( "number is not present!" )
return - 1
</Code> |
python-cpp-CHECK_STRING_CAN_OBTAINED_ROTATING_ANOTHER_STRING_2_PLACES | python-cpp | def isRotated ( str1 , str2 ) :
if ( len ( str1 ) != len ( str2 ) ) :
return False
clock_rot = ""
anticlock_rot = ""
l = len ( str2 )
anticlock_rot = ( anticlock_rot + str2 [ l - 2 : ] + str2 [ 0 : l - 2 ] )
clock_rot = clock_rot + str2 [ 2 : ] + str2 [ 0 : 2 ]
return ( str1 == clock... | bool isRotated ( string str1 , string str2 ) {
if ( str1 . length ( ) != str2 . length ( ) ) return false ;
string clock_rot = "" ;
string anticlock_rot = "" ;
int len = str2 . length ( ) ;
anticlock_rot = anticlock_rot + str2 . substr ( len - 2 , 2 ) + str2 . substr ( 0 , len - 2 ) ;
clock_rot = clock_rot ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isRotated ( str1 , str2 ) :
if ( len ( str1 ) != len ( str2 ) ) :
return False
clock_rot = ""
anticlock_rot = ""
l = len ( str2 )
anticlock_rot = ( anticlock_rot + str2 [ l - 2 : ] + str2 [ 0 : l - 2 ] )
clock_rot = clock_rot + str2 [ 2 : ] + str2 [ 0 : 2 ]
return ( str1 =... |
python-cpp-HOSOYAS_TRIANGLE | python-cpp | def printHosoya ( n ) :
dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ]
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1
for i in range ( 2 , n ) :
for j in range ( n ) :
if ( i > j ) :
dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] )
... | void printHosoya ( int n ) {
int dp [ N ] [ N ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ;
e... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printHosoya ( n ) :
dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ]
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1
for i in range ( 2 , n ) :
for j in range ( n ) :
if ( i > j ) :
dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] )
... |
python-cpp-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S | python-cpp | def findSubArray ( arr , n ) :
sum = 0
maxsize = - 1
for i in range ( 0 , n - 1 ) :
sum = - 1 if ( arr [ i ] == 0 ) else 1
for j in range ( i + 1 , n ) :
sum = sum + ( - 1 ) if ( arr [ j ] == 0 ) else sum + 1
if ( sum == 0 and maxsize < j - i + 1 ) :
m... | int findSubArray ( int arr [ ] , int n ) {
int sum = 0 ;
int maxsize = - 1 , startindex ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
( arr [ j ] == 0 ) ? ( sum += - 1 ) : ( sum += 1 ) ;
if ( sum == 0 && maxsize... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSubArray ( arr , n ) :
sum = 0
maxsize = - 1
for i in range ( 0 , n - 1 ) :
sum = - 1 if ( arr [ i ] == 0 ) else 1
for j in range ( i + 1 , n ) :
sum = sum + ( - 1 ) if ( arr [ j ] == 0 ) else sum + 1
if ( sum == 0 and maxsize < j - i + 1 ) :
... |
python-cpp-RECURSIVE_INSERTION_SORT | python-cpp | def insertionSortRecursive ( arr , n ) :
if n <= 1 :
return
insertionSortRecursive ( arr , n - 1 )
last = arr [ n - 1 ]
j = n - 2
while ( j >= 0 and arr [ j ] > last ) :
arr [ j + 1 ] = arr [ j ]
j = j - 1
arr [ j + 1 ] = last
| void insertionSortRecursive ( int arr [ ] , int n ) {
if ( n <= 1 ) return ;
insertionSortRecursive ( arr , n - 1 ) ;
int last = arr [ n - 1 ] ;
int j = n - 2 ;
while ( j >= 0 && arr [ j ] > last ) {
arr [ j + 1 ] = arr [ j ] ;
j -- ;
}
arr [ j + 1 ] = last ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def insertionSortRecursive ( arr , n ) :
if n <= 1 :
return
insertionSortRecursive ( arr , n - 1 )
last = arr [ n - 1 ]
j = n - 2
while ( j >= 0 and arr [ j ] > last ) :
arr [ j + 1 ] = arr [ j ]
j = j - 1
arr [ j + 1 ] = last
</Code> |
python-cpp-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY_1 | python-cpp | def findDiff ( arr , n ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
max_count = 0 min_count = n
for key , values in mp.items ( ) :
max_count = max ( max_count , values )
min_count = min ( min_count , values )
return max_count - min_count
| int findDiff ( int arr [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ arr [ i ] ] ++ ;
int max_count = 0 , min_count = n ;
for ( auto x : hm ) {
max_count = max ( max_count , x . second ) ;
min_count = min ( min_count , x . second ) ;
}
return ( max_count ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findDiff ( arr , n ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
max_count = 0 min_count = n
for key , values in mp.items ( ) :
max_count = max ( max_count , values )
min_count = min ( min_count , values )
return max_count - min_c... |
python-cpp-GAME_REPLACING_ARRAY_ELEMENTS | python-cpp | def playGame ( arr , n ) :
s = set ( )
for i in range ( n ) :
s.add ( arr [ i ] )
return 1 if len ( s ) % 2 == 0 else 2
| int playGame ( int arr [ ] , int n ) {
unordered_set < int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash . insert ( arr [ i ] ) ;
return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def playGame ( arr , n ) :
s = set ( )
for i in range ( n ) :
s.add ( arr [ i ] )
return 1 if len ( s ) % 2 == 0 else 2
</Code> |
python-cpp-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS_1 | python-cpp | def subset ( arr , n ) :
mp = { i : 0 for i in range ( 10 ) }
for i in range ( n ) :
mp [ arr [ i ] ] += 1
res = 0
for key , value in mp.items ( ) :
res = max ( res , value )
return res
| int subset ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int res = 0 ;
for ( auto x : mp ) res = max ( res , x . second ) ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def subset ( arr , n ) :
mp = { i : 0 for i in range ( 10 ) }
for i in range ( n ) :
mp [ arr [ i ] ] += 1
res = 0
for key , value in mp.items ( ) :
res = max ( res , value )
return res
</Code> |
python-cpp-PAIR_WITH_GIVEN_PRODUCT_SET_1_FIND_IF_ANY_PAIR_EXISTS_1 | python-cpp | def isProduct ( arr , n , x ) :
if n < 2 :
return False
s = set ( )
for i in range ( 0 , n ) :
if arr [ i ] == 0 :
if x == 0 :
return True
else :
continue
if x % arr [ i ] == 0 :
if x // arr [ i ] in s :
... | bool isProduct ( int arr [ ] , int n , int x ) {
if ( n < 2 ) return false ;
unordered_set < int > s ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if ( s . find ( x / arr [ i ] ) != s . end (... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isProduct ( arr , n , x ) :
if n < 2 :
return False
s = set ( )
for i in range ( 0 , n ) :
if arr [ i ] == 0 :
if x == 0 :
return True
else :
continue
if x % arr [ i ] == 0 :
if x // arr [ i ] in s :
... |
python-cpp-MAXIMUM_AVERAGE_SUM_PARTITION_ARRAY | python-cpp | def largestSumOfAverages ( A , K ) :
n = len ( A )
pre_sum = [ 0 ] * ( n + 1 )
pre_sum [ 0 ] = 0
for i in range ( n ) :
pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ]
dp = [ 0 ] * n
sum = 0
for i in range ( n ) :
dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i )
for k... | double largestSumOfAverages ( vector < int > & A , int K ) {
int n = A . size ( ) ;
double pre_sum [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double dp [ n ] = {
0 }
;
double sum = 0 ;
for ( int i = 0 ;
i < n ;
i +... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def largestSumOfAverages ( A , K ) :
n = len ( A )
pre_sum = [ 0 ] * ( n + 1 )
pre_sum [ 0 ] = 0
for i in range ( n ) :
pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ]
dp = [ 0 ] * n
sum = 0
for i in range ( n ) :
dp [ i ] = ( pre_sum [ n ] - pre_sum [ i ] ) / ( n - i )
... |
python-cpp-MINIMUM_STEPS_MINIMIZE_N_PER_GIVEN_CONDITION | python-cpp | def getMinSteps ( n ) :
table = [ 0 ] * ( n + 1 )
for i in range ( n + 1 ) :
table [ i ] = n - i
for i in range ( n , 0 , - 1 ) :
if ( not ( i % 2 ) ) :
table [ i // 2 ] = min ( table [ i ] + 1 , table [ i // 2 ] )
if ( not ( i % 3 ) ) :
table [ i // 3 ] = min... | int getMinSteps ( int n ) {
int table [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 ) ) table [ i / 2 ] = min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 ) ) table [ i / 3 ] = min ( table [ i ] + 1 , table [ i / 3 ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getMinSteps ( n ) :
table = [ 0 ] * ( n + 1 )
for i in range ( n + 1 ) :
table [ i ] = n - i
for i in range ( n , 0 , - 1 ) :
if ( not ( i % 2 ) ) :
table [ i // 2 ] = min ( table [ i ] + 1 , table [ i // 2 ] )
if ( not ( i % 3 ) ) :
table [ i // 3 ... |
python-cpp-N_TH_TERM_SERIES_2_12_36_80_150 | python-cpp | def nthTerm ( n ) :
return ( n * n ) + ( n * n * n )
| int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def nthTerm ( n ) :
return ( n * n ) + ( n * n * n )
</Code> |
python-cpp-FIND_MAXIMUM_AVERAGE_SUBARRAY_OF_K_LENGTH_1 | python-cpp | def findMaxAverage ( arr , n , k ) :
if ( k > n ) :
return - 1
sum = arr [ 0 ]
for i in range ( 1 , k ) :
sum += arr [ i ]
max_sum = sum
max_end = k - 1
for i in range ( k , n ) :
sum = sum + arr [ i ] - arr [ i - k ]
if ( sum > max_sum ) :
max_sum = s... | int findMaxAverage ( int arr [ ] , int n , int k ) {
if ( k > n ) return - 1 ;
int sum = arr [ 0 ] ;
for ( int i = 1 ;
i < k ;
i ++ ) sum += arr [ i ] ;
int max_sum = sum , max_end = k - 1 ;
for ( int i = k ;
i < n ;
i ++ ) {
int sum = sum + arr [ i ] - arr [ i - k ] ;
if ( sum > max_sum ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMaxAverage ( arr , n , k ) :
if ( k > n ) :
return - 1
sum = arr [ 0 ]
for i in range ( 1 , k ) :
sum += arr [ i ]
max_sum = sum
max_end = k - 1
for i in range ( k , n ) :
sum = sum + arr [ i ] - arr [ i - k ]
if ( sum > max_sum ) :
max_... |
python-cpp-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE | python-cpp | def eggDrop ( n , k ) :
if ( k == 1 or k == 0 ) :
return k
if ( n == 1 ) :
return k
min = sys.maxsize
for x in range ( 1 , k + 1 ) :
res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) )
if ( res < min ) :
min = res
return min + 1
| int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = INT_MAX , x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return min + 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def eggDrop ( n , k ) :
if ( k == 1 or k == 0 ) :
return k
if ( n == 1 ) :
return k
min = sys.maxsize
for x in range ( 1 , k + 1 ) :
res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) )
if ( res < min ) :
min = res
return min + 1
</Code> |
python-cpp-PROGRAM_COUNT_OCCURRENCE_GIVEN_CHARACTER_STRING | python-cpp | def count ( s , c ) :
res = 0
for i in range ( len ( s ) ) :
if ( s [ i ] == c ) :
res = res + 1
return res
| int count ( string s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) if ( s [ i ] == c ) res ++ ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def count ( s , c ) :
res = 0
for i in range ( len ( s ) ) :
if ( s [ i ] == c ) :
res = res + 1
return res
</Code> |
python-cpp-MINIMUM_ROTATIONS_REQUIRED_GET_STRING | python-cpp | def findRotations ( str ) :
tmp = str + str
n = len ( str )
for i in range ( 1 , n + 1 ) :
substring = tmp [ i : n ]
if ( str == substring ) :
return i
return n
| int findRotations ( string str ) {
string tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
string substring = tmp . substr ( i , str . size ( ) ) ;
if ( str == substring ) return i ;
}
return n ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findRotations ( str ) :
tmp = str + str
n = len ( str )
for i in range ( 1 , n + 1 ) :
substring = tmp [ i : n ]
if ( str == substring ) :
return i
return n
</Code> |
python-cpp-SHUFFLE_A_GIVEN_ARRAY | python-cpp | def randomize ( arr , n ) :
for i in range ( n - 1 , 0 , - 1 ) :
j = random.randint ( 0 , i + 1 )
arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
return arr
| void randomize ( int arr [ ] , int n ) {
srand ( time ( NULL ) ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = rand ( ) % ( i + 1 ) ;
swap ( & arr [ i ] , & arr [ j ] ) ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def randomize ( arr , n ) :
for i in range ( n - 1 , 0 , - 1 ) :
j = random.randint ( 0 , i + 1 )
arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
return arr
</Code> |
python-cpp-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_2 | python-cpp | def findLength ( string , n ) :
Sum = [ 0 ] * ( n + 1 )
for i in range ( 1 , n + 1 ) :
Sum [ i ] = ( Sum [ i - 1 ] + int ( string [ i - 1 ] ) )
ans = 0
for length in range ( 2 , n + 1 , 2 ) :
for i in range ( 0 , n - length + 1 ) :
j = i + length - 1
if ( Sum [ i ... | int findLength ( string str , int n ) {
int sum [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str [ i - 1 ] - '0' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
i ++ ) {
int j = i + len - 1 ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLength ( string , n ) :
Sum = [ 0 ] * ( n + 1 )
for i in range ( 1 , n + 1 ) :
Sum [ i ] = ( Sum [ i - 1 ] + int ( string [ i - 1 ] ) )
ans = 0
for length in range ( 2 , n + 1 , 2 ) :
for i in range ( 0 , n - length + 1 ) :
j = i + length - 1
if ( S... |
python-cpp-REPLACE_OCCURRENCES_STRING_AB_C_WITHOUT_USING_EXTRA_SPACE_1 | python-cpp | def translate ( st ) :
l = len ( st )
if ( l < 2 ) :
return
i = 0
j = 0
while ( j < l - 1 ) :
if ( st [ j ] == 'A' and st [ j + 1 ] == 'B' ) :
j += 2
st [ i ] = 'C'
i += 1
continue
st [ i ] = st [ j ]
i += 1
j +=... | void translate ( char * str ) {
int len = strlen ( str ) ;
if ( len < 2 ) return ;
int i = 0 ;
int j = 0 ;
while ( j < len - 1 ) {
if ( str [ j ] == 'A' && str [ j + 1 ] == 'B' ) {
j = j + 2 ;
str [ i ++ ] = 'C' ;
continue ;
}
str [ i ++ ] = str [ j ++ ] ;
}
if ( j == len - 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def translate ( st ) :
l = len ( st )
if ( l < 2 ) :
return
i = 0
j = 0
while ( j < l - 1 ) :
if ( st [ j ] == 'A' and st [ j + 1 ] == 'B' ) :
j += 2
st [ i ] = 'C'
i += 1
continue
st [ i ] = st [ j ]
i += 1
... |
python-cpp-NUMBER_WHICH_HAS_THE_MAXIMUM_NUMBER_OF_DISTINCT_PRIME_FACTORS_IN_RANGE_M_TO_N | python-cpp | def maximumNumberDistinctPrimeRange ( m , n ) :
factorCount = [ 0 ] * ( n + 1 )
prime = [ False ] * ( n + 1 )
for i in range ( n + 1 ) :
factorCount [ i ] = 0
prime [ i ] = True
for i in range ( 2 , n + 1 ) :
if ( prime [ i ] == True ) :
factorCount [ i ] = 1
... | int maximumNumberDistinctPrimeRange ( int m , int n ) {
long long factorCount [ n + 1 ] ;
bool prime [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
factorCount [ i ] = 0 ;
prime [ i ] = true ;
}
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( prime [ i ] == true ) {
factorCount [ i ] = 1 ;... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maximumNumberDistinctPrimeRange ( m , n ) :
factorCount = [ 0 ] * ( n + 1 )
prime = [ False ] * ( n + 1 )
for i in range ( n + 1 ) :
factorCount [ i ] = 0
prime [ i ] = True
for i in range ( 2 , n + 1 ) :
if ( prime [ i ] == True ) :
factorCount [ i ] = 1
... |
python-cpp-PROGRAM_NTH_CATALAN_NUMBER | python-cpp | def catalan ( n ) :
if n <= 1 :
return 1
res = 0
for i in range ( n ) :
res += catalan ( i ) * catalan ( n - i - 1 )
return res
| unsigned long int catalan ( unsigned int n ) {
if ( n <= 1 ) return 1 ;
unsigned long int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) res += catalan ( i ) * catalan ( n - i - 1 ) ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def catalan ( n ) :
if n <= 1 :
return 1
res = 0
for i in range ( n ) :
res += catalan ( i ) * catalan ( n - i - 1 )
return res
</Code> |
python-cpp-SUM_SERIES_12_32_52_2N_12 | python-cpp | def sumOfSeries ( n ) :
sum = 0
for i in range ( 1 , n + 1 ) :
sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 )
return sum
| int sumOfSeries ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ;
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sumOfSeries ( n ) :
sum = 0
for i in range ( 1 , n + 1 ) :
sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 )
return sum
</Code> |
python-cpp-MINIMUM_REVOLUTIONS_MOVE_CENTER_CIRCLE_TARGET | python-cpp | def minRevolutions ( r , x1 , y1 , x2 , y2 ) :
d = math.sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) )
return math.ceil ( d // ( 2 * r ) )
| int minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return ceil ( d / ( 2 * r ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minRevolutions ( r , x1 , y1 , x2 , y2 ) :
d = math.sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) )
return math.ceil ( d // ( 2 * r ) )
</Code> |
python-cpp-PRINT_STRING_SPECIFIED_CHARACTER_OCCURRED_GIVEN_NO_TIMES | python-cpp | def printString ( str , ch , count ) :
occ , i = 0 , 0
if ( count == 0 ) :
print ( str )
for i in range ( len ( str ) ) :
if ( str [ i ] == ch ) :
occ += 1
if ( occ == count ) :
break
if ( i < len ( str ) - 1 ) :
print ( str [ i + 1 : len ( str ) -... | void printString ( string str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
cout << str ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . length ( ) - 1 ) cout << str . substr ( i + 1 ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printString ( str , ch , count ) :
occ , i = 0 , 0
if ( count == 0 ) :
print ( str )
for i in range ( len ( str ) ) :
if ( str [ i ] == ch ) :
occ += 1
if ( occ == count ) :
break
if ( i < len ( str ) - 1 ) :
print ( str [ i + 1 : len ( ... |
python-cpp-FINDING_THE_MAXIMUM_SQUARE_SUB_MATRIX_WITH_ALL_EQUAL_ELEMENTS | python-cpp | def largestKSubmatrix ( a ) :
dp = [ [ 0 for x in range ( Row ) ] for y in range ( Col ) ]
result = 0
for i in range ( Row ) :
for j in range ( Col ) :
if ( i == 0 or j == 0 ) :
dp [ i ] [ j ] = 1
else :
if ( a [ i ] [ j ] == a [ i - 1 ] [ j ] ... | int largestKSubmatrix ( int a [ ] [ Col ] ) {
int dp [ Row ] [ Col ] ;
memset ( dp , sizeof ( dp ) , 0 ) ;
int result = 0 ;
for ( int i = 0 ;
i < Row ;
i ++ ) {
for ( int j = 0 ;
j < Col ;
j ++ ) {
if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ;
else {
if ( a [ i ] [ j ] == a [ ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def largestKSubmatrix ( a ) :
dp = [ [ 0 for x in range ( Row ) ] for y in range ( Col ) ]
result = 0
for i in range ( Row ) :
for j in range ( Col ) :
if ( i == 0 or j == 0 ) :
dp [ i ] [ j ] = 1
else :
if ( a [ i ] [ j ] == a [ i - 1 ]... |
python-cpp-PASCAL_TRIANGLE | python-cpp | def printPascal ( n : int ) :
arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for line in range ( 0 , n ) :
for i in range ( 0 , line + 1 ) :
if ( i is 0 or i is line ) :
arr [ line ] [ i ] = 1
print ( arr [ line ] [ i ] , end = "" )
else... | void printPascal ( int n ) {
int arr [ n ] [ n ] ;
for ( int line = 0 ;
line < n ;
line ++ ) {
for ( int i = 0 ;
i <= line ;
i ++ ) {
if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ;
else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ line - 1 ] [ i ] ;
cout << arr [ li... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printPascal ( n : int ) :
arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for line in range ( 0 , n ) :
for i in range ( 0 , line + 1 ) :
if ( i is 0 or i is line ) :
arr [ line ] [ i ] = 1
print ( arr [ line ] [ i ] , end = "" )
... |
python-cpp-NUMBER_INDEXES_EQUAL_ELEMENTS_GIVEN_RANGE | python-cpp | def answer_query ( a , n , l , r ) :
count = 0
for i in range ( l , r ) :
if ( a [ i ] == a [ i + 1 ] ) :
count += 1
return count
| int answer_query ( int a [ ] , int n , int l , int r ) {
int count = 0 ;
for ( int i = l ;
i < r ;
i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ;
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def answer_query ( a , n , l , r ) :
count = 0
for i in range ( l , r ) :
if ( a [ i ] == a [ i + 1 ] ) :
count += 1
return count
</Code> |
python-cpp-CASSINIS_IDENTITY | python-cpp | def cassini ( n ) :
return - 1 if ( n & 1 ) else 1
| int cassini ( int n ) {
return ( n & 1 ) ? - 1 : 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def cassini ( n ) :
return - 1 if ( n & 1 ) else 1
</Code> |
python-cpp-COMPUTE_AVERAGE_TWO_NUMBERS_WITHOUT_OVERFLOW | python-cpp | def compute_average ( a , b ) :
return floor ( ( a + b ) / 2 )
| int compute_average ( int a , int b ) {
return ( a + b ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def compute_average ( a , b ) :
return floor ( ( a + b ) / 2 )
</Code> |
python-cpp-NUMBER_OF_WAYS_TO_ARRANGE_N_ITEMS_UNDER_GIVEN_CONSTRAINTS | python-cpp | def waysToArrange ( N , K , k ) :
C = np.zeros ( ( N + 1 , N + 1 ) )
for i in range ( N + 1 ) :
for j in range ( i + 1 ) :
if ( j == 0 or j == i ) :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] )
dp = np.... | int waysToArrange ( int N , int K , int k [ ] ) {
int C [ N + 1 ] [ N + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= N ;
i ++ ) {
for ( j = 0 ;
j <= i ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ;
}
}
int dp ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def waysToArrange ( N , K , k ) :
C = np.zeros ( ( N + 1 , N + 1 ) )
for i in range ( N + 1 ) :
for j in range ( i + 1 ) :
if ( j == 0 or j == i ) :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] )
d... |
python-cpp-FIND_DIMENSIONS_RIGHT_ANGLED_TRIANGLE | python-cpp | def findDimen ( H , A ) :
if H * H < 4 * A :
print ( "Not Possible" )
return
apb = sqrt ( H * H + 4 * A )
asb = sqrt ( H * H - 4 * A )
print ( "P = " , "%.2f" % ( ( apb - asb ) / 2.0 ) )
print ( "B = " , "%.2f" % ( ( apb + asb ) / 2.0 ) )
| void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
cout << "Not Possible\n" ;
return ;
}
double apb = sqrt ( H * H + 4 * A ) ;
double asb = sqrt ( H * H - 4 * A ) ;
cout . precision ( 2 ) ;
cout << "P = " << fixed << ( apb - asb ) / 2.0 << "\n" ;
cout << "B = " << ( apb + asb ) / 2.0 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findDimen ( H , A ) :
if H * H < 4 * A :
print ( "Not Possible" )
return
apb = sqrt ( H * H + 4 * A )
asb = sqrt ( H * H - 4 * A )
print ( "P = " , "%.2f" % ( ( apb - asb ) / 2.0 ) )
print ( "B = " , "%.2f" % ( ( apb + asb ) / 2.0 ) )
</Code> |
python-cpp-MAXIMUM_TRIPLET_SUM_ARRAY_1 | python-cpp | def maxTripletSum ( arr , n ) :
arr.sort ( )
return ( arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] )
| int maxTripletSum ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxTripletSum ( arr , n ) :
arr.sort ( )
return ( arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] )
</Code> |
python-cpp-FIND_MAXIMUM_DOT_PRODUCT_TWO_ARRAYS_INSERTION_0S | python-cpp | def MaxDotProduct ( A , B , m , n ) :
dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( 1 , n + 1 , 1 ) :
for j in range ( i , m + 1 , 1 ) :
dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] )
return dp ... | long long int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
long long int dp [ n + 1 ] [ m + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def MaxDotProduct ( A , B , m , n ) :
dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( 1 , n + 1 , 1 ) :
for j in range ( i , m + 1 , 1 ) :
dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ] [ j - 1 ] )
ret... |
python-cpp-FIND_MINIMUM_DIFFERENCE_PAIR_1 | python-cpp | def findMinDiff ( arr , n ) :
arr = sorted ( arr )
diff = 10 ** 20
for i in range ( n - 1 ) :
if arr [ i + 1 ] - arr [ i ] < diff :
diff = arr [ i + 1 ] - arr [ i ]
return diff
| int findMinDiff ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int diff = INT_MAX ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ;
return diff ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMinDiff ( arr , n ) :
arr = sorted ( arr )
diff = 10 ** 20
for i in range ( n - 1 ) :
if arr [ i + 1 ] - arr [ i ] < diff :
diff = arr [ i + 1 ] - arr [ i ]
return diff
</Code> |
python-cpp-FIND_LAST_INDEX_CHARACTER_STRING_1 | python-cpp | def findLastIndex ( str , x ) :
for i in range ( len ( str ) - 1 , - 1 , - 1 ) :
if ( str [ i ] == x ) :
return i
return - 1
| int findLastIndex ( string & str , char x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str [ i ] == x ) return i ;
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLastIndex ( str , x ) :
for i in range ( len ( str ) - 1 , - 1 , - 1 ) :
if ( str [ i ] == x ) :
return i
return - 1
</Code> |
python-cpp-MAXIMUM_DISTINCT_LOWERCASE_ALPHABETS_TWO_UPPERCASE | python-cpp | def maxLower ( str ) :
n = len ( str )
i = 0
for i in range ( n ) :
if str [ i ] >= 'A' and str [ i ] <= 'Z' :
i += 1
break
maxCount = 0
count = [ ]
for j in range ( MAX_CHAR ) :
count.append ( 0 )
for j in range ( i , n ) :
if str [ j ] >= 'A'... | int maxLower ( string str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ MAX_CHAR ] = {
0 }
;
for ( ;
i < n ;
i ++ ) {
if ( str [ i ] >= 'A' ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxLower ( str ) :
n = len ( str )
i = 0
for i in range ( n ) :
if str [ i ] >= 'A' and str [ i ] <= 'Z' :
i += 1
break
maxCount = 0
count = [ ]
for j in range ( MAX_CHAR ) :
count.append ( 0 )
for j in range ( i , n ) :
if str [ j ]... |
python-cpp-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS | python-cpp | def findElements ( arr , n ) :
for i in range ( n ) :
count = 0
for j in range ( 0 , n ) :
if arr [ j ] > arr [ i ] :
count = count + 1
if count >= 2 :
print ( arr [ i ] , end = " " )
| void findElements ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ;
if ( count >= 2 ) cout << arr [ i ] << " " ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findElements ( arr , n ) :
for i in range ( n ) :
count = 0
for j in range ( 0 , n ) :
if arr [ j ] > arr [ i ] :
count = count + 1
if count >= 2 :
print ( arr [ i ] , end = " " )
</Code> |
python-cpp-NTH_EVEN_LENGTH_PALINDROME | python-cpp | def evenlength ( n ) :
res = n
for j in range ( len ( n ) - 1 , - 1 , - 1 ) :
res += n [ j ]
return res
| string evenlength ( string n ) {
string res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n [ j ] ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def evenlength ( n ) :
res = n
for j in range ( len ( n ) - 1 , - 1 , - 1 ) :
res += n [ j ]
return res
</Code> |
python-cpp-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S_1 | python-cpp | def maxLen ( arr , n ) :
hash_map = { }
curr_sum = 0
max_len = 0
ending_index = - 1
for i in range ( 0 , n ) :
if ( arr [ i ] == 0 ) :
arr [ i ] = - 1
else :
arr [ i ] = 1
for i in range ( 0 , n ) :
curr_sum = curr_sum + arr [ i ]
if ( curr... | int maxLen ( int arr [ ] , int n ) {
unordered_map < int , int > hM ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
if ( sum == 0 ) {
max_len = i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxLen ( arr , n ) :
hash_map = { }
curr_sum = 0
max_len = 0
ending_index = - 1
for i in range ( 0 , n ) :
if ( arr [ i ] == 0 ) :
arr [ i ] = - 1
else :
arr [ i ] = 1
for i in range ( 0 , n ) :
curr_sum = curr_sum + arr [ i ]
if... |
python-cpp-COUNT_NUMBER_OF_STRINGS_MADE_OF_R_G_AND_B_USING_GIVEN_COMBINATION | python-cpp | def possibleStrings ( n , r , b , g ) :
fact = [ 0 for i in range ( n + 1 ) ]
fact [ 0 ] = 1
for i in range ( 1 , n + 1 , 1 ) :
fact [ i ] = fact [ i - 1 ] * i
left = n - ( r + g + b )
sum = 0
for i in range ( 0 , left + 1 , 1 ) :
for j in range ( 0 , left - i + 1 , 1 ) :
... | int possibleStrings ( int n , int r , int b , int g ) {
int fact [ n + 1 ] ;
fact [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) fact [ i ] = fact [ i - 1 ] * i ;
int left = n - ( r + g + b ) ;
int sum = 0 ;
for ( int i = 0 ;
i <= left ;
i ++ ) {
for ( int j = 0 ;
j <= left - i ;
j ++ ) {... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def possibleStrings ( n , r , b , g ) :
fact = [ 0 for i in range ( n + 1 ) ]
fact [ 0 ] = 1
for i in range ( 1 , n + 1 , 1 ) :
fact [ i ] = fact [ i - 1 ] * i
left = n - ( r + g + b )
sum = 0
for i in range ( 0 , left + 1 , 1 ) :
for j in range ( 0 , left - i + 1 , 1 ) :
... |
python-cpp-NUMBER_WAYS_INSERT_CHARACTER_INCREASE_LCS_ONE | python-cpp | def numberofways ( A , B , N , M ) :
pos = [ [ ] for _ in range ( MAX ) ]
for i in range ( M ) :
pos [ ord ( B [ i ] ) ].append ( i + 1 )
dpl = [ [ 0 ] * ( M + 2 ) for _ in range ( N + 2 ) ]
for i in range ( 1 , N + 1 ) :
for j in range ( 1 , M + 1 ) :
if A [ i - 1 ] == B [ j... | int numberofways ( string A , string B , int N , int M ) {
vector < int > pos [ MAX ] ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B [ i ] ] . push_back ( i + 1 ) ;
int dpl [ N + 2 ] [ M + 2 ] ;
memset ( dpl , 0 , sizeof ( dpl ) ) ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
for ( int j = 1 ;
j <= M ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberofways ( A , B , N , M ) :
pos = [ [ ] for _ in range ( MAX ) ]
for i in range ( M ) :
pos [ ord ( B [ i ] ) ].append ( i + 1 )
dpl = [ [ 0 ] * ( M + 2 ) for _ in range ( N + 2 ) ]
for i in range ( 1 , N + 1 ) :
for j in range ( 1 , M + 1 ) :
if A [ i - 1 ] =... |
python-cpp-PROGRAM_FIND_CIRCUMFERENCE_CIRCLE | python-cpp | def circumference ( r ) :
return ( 2 * PI * r )
| double circumference ( double r ) {
double cir = 2 * PI * r ;
return cir ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def circumference ( r ) :
return ( 2 * PI * r )
</Code> |
python-cpp-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL | python-cpp | def substringConversions ( s , k , b ) :
l = len ( s )
for i in range ( l ) :
if ( ( i + k ) < l + 1 ) :
sub = s [ i : i + k ]
sum , counter = 0 , 0
for i in range ( len ( sub ) - 1 , - 1 , - 1 ) :
sum = sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * po... | int substringConversions ( string str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . size ( ) ;
i ++ ) {
string sub = str . substr ( i , k ) ;
int sum = 0 , counter = 0 ;
for ( int i = sub . size ( ) - 1 ;
i >= 0 ;
i -- ) {
sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , counter... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def substringConversions ( s , k , b ) :
l = len ( s )
for i in range ( l ) :
if ( ( i + k ) < l + 1 ) :
sub = s [ i : i + k ]
sum , counter = 0 , 0
for i in range ( len ( sub ) - 1 , - 1 , - 1 ) :
sum = sum + ( ( ord ( sub [ i ] ) - ord ( '0' )... |
python-cpp-MAXIMUM_PATH_SUM_TRIANGLE | python-cpp | def maxPathSum ( tri , m , n ) :
for i in range ( m - 1 , - 1 , - 1 ) :
for j in range ( i + 1 ) :
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) :
tri [ i ] [ j ] += tri [ i + 1 ] [ j ]
else :
tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ]
retur... | int maxPathSum ( int tri [ ] [ N ] , int m , int n ) {
for ( int i = m - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j <= i ;
j ++ ) {
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ;
else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ;
}
}
ret... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxPathSum ( tri , m , n ) :
for i in range ( m - 1 , - 1 , - 1 ) :
for j in range ( i + 1 ) :
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) :
tri [ i ] [ j ] += tri [ i + 1 ] [ j ]
else :
tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ]
... |
python-cpp-SUM_MINIMUM_MAXIMUM_ELEMENTS_SUBARRAYS_SIZE_K | python-cpp | def SumOfKsubArray ( arr , n , k ) :
Sum = 0
S = deque ( )
G = deque ( )
for i in range ( k ) :
while ( len ( S ) > 0 and arr [ S [ - 1 ] ] >= arr [ i ] ) :
S.pop ( )
while ( len ( G ) > 0 and arr [ G [ - 1 ] ] <= arr [ i ] ) :
G.pop ( )
G.append ( i )
... | int SumOfKsubArray ( int arr [ ] , int n , int k ) {
int sum = 0 ;
deque < int > S ( k ) , G ( k ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ;
while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ] ) G .... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def SumOfKsubArray ( arr , n , k ) :
Sum = 0
S = deque ( )
G = deque ( )
for i in range ( k ) :
while ( len ( S ) > 0 and arr [ S [ - 1 ] ] >= arr [ i ] ) :
S.pop ( )
while ( len ( G ) > 0 and arr [ G [ - 1 ] ] <= arr [ i ] ) :
G.pop ( )
G.append ( ... |
python-cpp-TURN_OFF_THE_RIGHTMOST_SET_BIT | python-cpp | def fun ( n ) :
return n & ( n - 1 )
| int fun ( unsigned 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 fun ( n ) :
return n & ( n - 1 )
</Code> |
python-cpp-FIND_MIRROR_IMAGE_POINT_2_D_PLANE | python-cpp | def mirrorImage ( a , b , c , x1 , y1 ) :
temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b )
x = temp * a + x1
y = temp * b + y1
return ( x , y )
| pair < double , double > mirrorImage ( double a , double b , double c , double x1 , double y1 ) {
double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ;
double x = temp * a + x1 ;
double y = temp * b + y1 ;
return make_pair ( x , y ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def mirrorImage ( a , b , c , x1 , y1 ) :
temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b )
x = temp * a + x1
y = temp * b + y1
return ( x , y )
</Code> |
python-cpp-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_1 | python-cpp | def printDistinct ( arr , n ) :
arr.sort ( )
for i in range ( n ) :
if ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) :
while ( i < n - 1 and ( arr [ i ] == arr [ i + 1 ] ) ) :
i += 1
else :
print ( arr [ i ] , end = " " )
| void printDistinct ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
cout << arr [ i ] << " " ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printDistinct ( arr , n ) :
arr.sort ( )
for i in range ( n ) :
if ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) :
while ( i < n - 1 and ( arr [ i ] == arr [ i + 1 ] ) ) :
i += 1
else :
print ( arr [ i ] , end = " " )
</Code> |
python-cpp-UNION_AND_INTERSECTION_OF_TWO_SORTED_ARRAYS_2_1 | python-cpp | def printIntersection ( arr1 , arr2 , m , n ) :
i , j = 0 , 0
while i < m and j < n :
if arr1 [ i ] < arr2 [ j ] :
i += 1
elif arr2 [ j ] < arr1 [ i ] :
j += 1
else :
print ( arr2 [ j ] )
j += 1
i += 1
| int printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) {
int i = 0 , j = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) i ++ ;
else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ;
else {
cout << arr2 [ j ] << " " ;
i ++ ;
j ++ ;
}
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printIntersection ( arr1 , arr2 , m , n ) :
i , j = 0 , 0
while i < m and j < n :
if arr1 [ i ] < arr2 [ j ] :
i += 1
elif arr2 [ j ] < arr1 [ i ] :
j += 1
else :
print ( arr2 [ j ] )
j += 1
i += 1
</Code> |
python-cpp-SEARCHING_ARRAY_ADJACENT_DIFFER_K | python-cpp | def search ( arr , n , x , k ) :
i = 0
while ( i < n ) :
if ( arr [ i ] == x ) :
return i
i = i + max ( 1 , int ( abs ( arr [ i ] - x ) / k ) )
print ( "number is not present!" )
return - 1
| int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + max ( 1 , abs ( arr [ i ] - x ) / k ) ;
}
cout << "number is not present!" ;
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 , k ) :
i = 0
while ( i < n ) :
if ( arr [ i ] == x ) :
return i
i = i + max ( 1 , int ( abs ( arr [ i ] - x ) / k ) )
print ( "number is not present!" )
return - 1
</Code> |
python-cpp-REARRANGE_A_STRING_IN_SORTED_ORDER_FOLLOWED_BY_THE_INTEGER_SUM | python-cpp | def arrangeString ( string ) :
char_count = [ 0 ] * MAX_CHAR
s = 0
for i in range ( len ( string ) ) :
if string [ i ] >= "A" and string [ i ] <= "Z" :
char_count [ ord ( string [ i ] ) - ord ( "A" ) ] += 1
else :
s += ord ( string [ i ] ) - ord ( "0" )
res = ""
... | string arrangeString ( string str ) {
int char_count [ MAX_CHAR ] = {
0 }
;
int sum = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) char_count [ str [ i ] - 'A' ] ++ ;
else sum = sum + ( str [ i ] - '0' ) ;
}
string res = ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def arrangeString ( string ) :
char_count = [ 0 ] * MAX_CHAR
s = 0
for i in range ( len ( string ) ) :
if string [ i ] >= "A" and string [ i ] <= "Z" :
char_count [ ord ( string [ i ] ) - ord ( "A" ) ] += 1
else :
s += ord ( string [ i ] ) - ord ( "0" )
res... |
python-cpp-COUNT_NUMBER_BINARY_STRINGS_WITHOUT_CONSECUTIVE_1S | python-cpp | def countStrings ( n ) :
a = [ 0 for i in range ( n ) ]
b = [ 0 for i in range ( n ) ]
a [ 0 ] = b [ 0 ] = 1
for i in range ( 1 , n ) :
a [ i ] = a [ i - 1 ] + b [ i - 1 ]
b [ i ] = a [ i - 1 ]
return a [ n - 1 ] + b [ n - 1 ]
| int countStrings ( int n ) {
int a [ n ] , b [ n ] ;
a [ 0 ] = b [ 0 ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
a [ i ] = a [ i - 1 ] + b [ i - 1 ] ;
b [ i ] = a [ i - 1 ] ;
}
return a [ n - 1 ] + b [ n - 1 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countStrings ( n ) :
a = [ 0 for i in range ( n ) ]
b = [ 0 for i in range ( n ) ]
a [ 0 ] = b [ 0 ] = 1
for i in range ( 1 , n ) :
a [ i ] = a [ i - 1 ] + b [ i - 1 ]
b [ i ] = a [ i - 1 ]
return a [ n - 1 ] + b [ n - 1 ]
</Code> |
python-cpp-MINIMUM_SUM_SUBSEQUENCE_LEAST_ONE_EVERY_FOUR_CONSECUTIVE_ELEMENTS_PICKED | python-cpp | def minSum ( arr , n ) :
dp = [ 0 ] * n
if ( n == 1 ) :
return arr [ 0 ]
if ( n == 2 ) :
return min ( arr [ 0 ] , arr [ 1 ] )
if ( n == 3 ) :
return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) )
if ( n == 4 ) :
return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( ar... | int minSum ( int arr [ ] , int n ) {
int dp [ n ] ;
if ( n == 1 ) return arr [ 0 ] ;
if ( n == 2 ) return min ( arr [ 0 ] , arr [ 1 ] ) ;
if ( n == 3 ) return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) ) ;
if ( n == 4 ) return min ( min ( arr [ 0 ] , arr [ 1 ] ) , min ( arr [ 2 ] , arr [ 3 ] ) ) ;
dp [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minSum ( arr , n ) :
dp = [ 0 ] * n
if ( n == 1 ) :
return arr [ 0 ]
if ( n == 2 ) :
return min ( arr [ 0 ] , arr [ 1 ] )
if ( n == 3 ) :
return min ( arr [ 0 ] , min ( arr [ 1 ] , arr [ 2 ] ) )
if ( n == 4 ) :
return min ( min ( arr [ 0 ] , arr [ 1 ] ) , m... |
python-cpp-CONVERT_SENTENCE_EQUIVALENT_MOBILE_NUMERIC_KEYPAD_SEQUENCE | python-cpp | def printSequence ( arr , input ) :
n = len ( input )
output = ""
for i in range ( n ) :
if ( input [ i ] == ' ' ) :
output = output + "0"
else :
position = ord ( input [ i ] ) - ord ( 'A' )
output = output + arr [ position ]
return output
| string printSequence ( string arr [ ] , string input ) {
string output = "" ;
int n = input . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( input [ i ] == ' ' ) output = output + "0" ;
else {
int position = input [ i ] - 'A' ;
output = output + arr [ position ] ;
}
}
return ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printSequence ( arr , input ) :
n = len ( input )
output = ""
for i in range ( n ) :
if ( input [ i ] == ' ' ) :
output = output + "0"
else :
position = ord ( input [ i ] ) - ord ( 'A' )
output = output + arr [ position ]
return output
</Co... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.