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-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY_1 | python-cpp | def maxSum ( arr , n ) :
cum_sum = 0
for i in range ( 0 , n ) :
cum_sum += arr [ i ]
curr_val = 0
for i in range ( 0 , n ) :
curr_val += i * arr [ i ]
res = curr_val
for i in range ( 1 , n ) :
next_val = ( curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 )... | int maxSum ( int arr [ ] , int n ) {
int cum_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) cum_sum += arr [ i ] ;
int curr_val = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) curr_val += i * arr [ i ] ;
int res = curr_val ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int next_val = curr_val - ( cum_sum - arr [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSum ( arr , n ) :
cum_sum = 0
for i in range ( 0 , n ) :
cum_sum += arr [ i ]
curr_val = 0
for i in range ( 0 , n ) :
curr_val += i * arr [ i ]
res = curr_val
for i in range ( 1 , n ) :
next_val = ( curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( ... |
python-cpp-CALCULATE_ANGLE_HOUR_HAND_MINUTE_HAND | python-cpp | def calcAngle ( h , m ) :
if ( h < 0 or m < 0 or h > 12 or m > 60 ) :
print ( 'Wrong input' )
if ( h == 12 ) :
h = 0
if ( m == 60 ) :
m = 0
hour_angle = 0.5 * ( h * 60 + m )
minute_angle = 6 * m
angle = abs ( hour_angle - minute_angle )
angle = min ( 360 - angle , ang... | int calcAngle ( double h , double m ) {
if ( h < 0 || m < 0 || h > 12 || m > 60 ) printf ( "Wrong input" ) ;
if ( h == 12 ) h = 0 ;
if ( m == 60 ) m = 0 ;
int hour_angle = 0.5 * ( h * 60 + m ) ;
int minute_angle = 6 * m ;
int angle = abs ( hour_angle - minute_angle ) ;
angle = min ( 360 - angle , angle ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def calcAngle ( h , m ) :
if ( h < 0 or m < 0 or h > 12 or m > 60 ) :
print ( 'Wrong input' )
if ( h == 12 ) :
h = 0
if ( m == 60 ) :
m = 0
hour_angle = 0.5 * ( h * 60 + m )
minute_angle = 6 * m
angle = abs ( hour_angle - minute_angle )
angle = min ( 360 - angl... |
python-cpp-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS_1 | python-cpp | def smallest ( x , y , z ) :
if ( not ( y / x ) ) :
return y if ( not ( y / z ) ) else z
return x if ( not ( x / z ) ) else z
| int smallest ( int x , int y , int z ) {
if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ;
return ( ! ( x / z ) ) ? x : z ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def smallest ( x , y , z ) :
if ( not ( y / x ) ) :
return y if ( not ( y / z ) ) else z
return x if ( not ( x / z ) ) else z
</Code> |
python-cpp-FIND_ONE_EXTRA_CHARACTER_STRING_1 | python-cpp | def findExtraCharcter ( strA , strB ) :
res = 0
for i in range ( 0 , len ( strA ) ) :
res = res ^ ( ord ) ( strA [ i ] )
for i in range ( 0 , len ( strB ) ) :
res = res ^ ( ord ) ( strB [ i ] )
return ( ( chr ) ( res ) ) ;
| char findExtraCharcter ( string strA , string strB ) {
int res = 0 , i ;
for ( i = 0 ;
i < strA . length ( ) ;
i ++ ) {
res ^= strA [ i ] ;
}
for ( i = 0 ;
i < strB . length ( ) ;
i ++ ) {
res ^= strB [ i ] ;
}
return ( ( char ) ( res ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findExtraCharcter ( strA , strB ) :
res = 0
for i in range ( 0 , len ( strA ) ) :
res = res ^ ( ord ) ( strA [ i ] )
for i in range ( 0 , len ( strB ) ) :
res = res ^ ( ord ) ( strB [ i ] )
return ( ( chr ) ( res ) ) ;
</Code> |
python-cpp-RECAMANS_SEQUENCE_1 | python-cpp | def recaman ( n ) :
if ( n <= 0 ) :
return
print ( 0 , "," , end = '' )
s = set ( [ ] )
s.add ( 0 )
prev = 0
for i in range ( 1 , n ) :
curr = prev - i
if ( curr < 0 or curr in s ) :
curr = prev + i
s.add ( curr )
print ( curr , "," , end = '' ... | void recaman ( int n ) {
if ( n <= 0 ) return ;
printf ( "%d, " , 0 ) ;
unordered_set < int > s ;
s . insert ( 0 ) ;
int prev = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = prev - i ;
if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ;
s . insert ( curr ) ;
printf ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def recaman ( n ) :
if ( n <= 0 ) :
return
print ( 0 , "," , end = '' )
s = set ( [ ] )
s.add ( 0 )
prev = 0
for i in range ( 1 , n ) :
curr = prev - i
if ( curr < 0 or curr in s ) :
curr = prev + i
s.add ( curr )
print ( curr , "," , en... |
python-cpp-PROGRAM_FIND_SMALLEST_DIFFERENCE_ANGLES_TWO_PARTS_GIVEN_CIRCLE | python-cpp | def findMinimumAngle ( arr , n ) :
l = 0
_sum = 0
ans = 360
for i in range ( n ) :
_sum += arr [ i ]
while _sum >= 180 :
ans = min ( ans , 2 * abs ( 180 - _sum ) )
_sum -= arr [ l ]
l += 1
ans = min ( ans , 2 * abs ( 180 - _sum ) )
return a... | int findMinimumAngle ( int arr [ ] , int n ) {
int l = 0 , sum = 0 , ans = 360 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
while ( sum >= 180 ) {
ans = min ( ans , 2 * abs ( 180 - sum ) ) ;
sum -= arr [ l ] ;
l ++ ;
}
ans = min ( ans , 2 * abs ( 180 - sum ) ) ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMinimumAngle ( arr , n ) :
l = 0
_sum = 0
ans = 360
for i in range ( n ) :
_sum += arr [ i ]
while _sum >= 180 :
ans = min ( ans , 2 * abs ( 180 - _sum ) )
_sum -= arr [ l ]
l += 1
ans = min ( ans , 2 * abs ( 180 - _sum ) )
r... |
python-cpp-COUNT_GFG_SUBSEQUENCES_GIVEN_STRING | python-cpp | def countSubsequence ( s , n ) :
cntG = 0
cntF = 0
result = 0
C = 0
for i in range ( n ) :
if ( s [ i ] == 'G' ) :
cntG += 1
result += C
continue
if ( s [ i ] == 'F' ) :
cntF += 1
C += cntG
continue
else ... | void countSubsequence ( char s [ ] , int n ) {
int cntG = 0 , cntF = 0 , result = 0 , C = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
switch ( s [ i ] ) {
case 'G' : cntG ++ ;
result += C ;
break ;
case 'F' : cntF ++ ;
C += cntG ;
break ;
default : continue ;
}
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSubsequence ( s , n ) :
cntG = 0
cntF = 0
result = 0
C = 0
for i in range ( n ) :
if ( s [ i ] == 'G' ) :
cntG += 1
result += C
continue
if ( s [ i ] == 'F' ) :
cntF += 1
C += cntG
continue
... |
python-cpp-MAXIMUM_DIFFERENCE_BETWEEN_FREQUENCY_OF_TWO_ELEMENTS_SUCH_THAT_ELEMENT_HAVING_GREATER_FREQUENCY_IS_ALSO_GREATER | python-cpp | def maxdiff ( arr , n ) :
freq = defaultdict ( lambda : 0 )
for i in range ( n ) :
freq [ arr [ i ] ] += 1
ans = 0
for i in range ( n ) :
for j in range ( n ) :
if freq [ arr [ i ] ] > freq [ arr [ j ] ] and arr [ i ] > arr [ j ] :
ans = max ( ans , freq [ arr... | int maxdiff ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [ j ] ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxdiff ( arr , n ) :
freq = defaultdict ( lambda : 0 )
for i in range ( n ) :
freq [ arr [ i ] ] += 1
ans = 0
for i in range ( n ) :
for j in range ( n ) :
if freq [ arr [ i ] ] > freq [ arr [ j ] ] and arr [ i ] > arr [ j ] :
ans = max ( ans , fre... |
python-cpp-CHECK_IF_TWO_ARRAYS_ARE_EQUAL_OR_NOT | python-cpp | def areEqual ( arr1 , arr2 , n , m ) :
if ( n != m ) :
return False ;
arr1.sort ( ) ;
arr2.sort ( ) ;
for i in range ( 0 , n - 1 ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return False ;
return True ;
| bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
if ( n != m ) return false ;
sort ( arr1 , arr1 + n ) ;
sort ( arr2 , arr2 + m ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def areEqual ( arr1 , arr2 , n , m ) :
if ( n != m ) :
return False ;
arr1.sort ( ) ;
arr2.sort ( ) ;
for i in range ( 0 , n - 1 ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return False ;
return True ;
</Code> |
python-cpp-FRIENDS_PAIRING_PROBLEM_2 | python-cpp | def countFriendsPairings ( n ) :
a , b , c = 1 , 2 , 0
if ( n <= 2 ) :
return n
for i in range ( 3 , n + 1 ) :
c = b + ( i - 1 ) * a
a = b
b = c
return c
| int countFriendsPairings ( int n ) {
int a = 1 , b = 2 , c = 0 ;
if ( n <= 2 ) {
return n ;
}
for ( int i = 3 ;
i <= n ;
i ++ ) {
c = b + ( i - 1 ) * a ;
a = b ;
b = c ;
}
return c ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countFriendsPairings ( n ) :
a , b , c = 1 , 2 , 0
if ( n <= 2 ) :
return n
for i in range ( 3 , n + 1 ) :
c = b + ( i - 1 ) * a
a = b
b = c
return c
</Code> |
python-cpp-FINDING_POWER_PRIME_NUMBER_P_N | python-cpp | def PowerOFPINnfactorial ( n , p ) :
ans = 0
temp = p
while ( temp <= n ) :
ans += n / temp
temp = temp * p
return ans
| int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
int temp = p ;
while ( temp <= n ) {
ans += n / temp ;
temp = temp * p ;
}
return ans ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def PowerOFPINnfactorial ( n , p ) :
ans = 0
temp = p
while ( temp <= n ) :
ans += n / temp
temp = temp * p
return ans
</Code> |
python-cpp-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF | python-cpp | def findLength ( str ) :
n = len ( str )
maxlen = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n , 2 ) :
length = j - i + 1
leftsum = 0
rightsum = 0
for k in range ( 0 , int ( length / 2 ) ) :
leftsum += ( int ( str [ i + k ] )... | int findLength ( char * str ) {
int n = strlen ( str ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j += 2 ) {
int length = j - i + 1 ;
int leftsum = 0 , rightsum = 0 ;
for ( int k = 0 ;
k < length / 2 ;
k ++ ) {
leftsum ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLength ( str ) :
n = len ( str )
maxlen = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n , 2 ) :
length = j - i + 1
leftsum = 0
rightsum = 0
for k in range ( 0 , int ( length / 2 ) ) :
leftsum += ( int ( str [ i ... |
python-cpp-ANALYSIS_OF_ALGORITHMS_SET_2_ASYMPTOTIC_ANALYSIS | python-cpp | def search ( arr , n , x ) :
i = 0
for i in range ( i , n ) :
if ( arr [ i ] == x ) :
return i
return - 1
| int search ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , n , x ) :
i = 0
for i in range ( i , n ) :
if ( arr [ i ] == x ) :
return i
return - 1
</Code> |
python-cpp-SUBARRAYSUBSTRING_VS_SUBSEQUENCE_AND_PROGRAMS_TO_GENERATE_THEM | python-cpp | def subArray ( arr , n ) :
for i in range ( 0 , n ) :
for j in range ( i , n ) :
for k in range ( i , j + 1 ) :
print ( arr [ k ] , end = "" )
print ( "\n" , end = "" )
| void subArray ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
for ( int k = i ;
k <= j ;
k ++ ) cout << arr [ k ] << " " ;
cout << endl ;
}
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def subArray ( arr , n ) :
for i in range ( 0 , n ) :
for j in range ( i , n ) :
for k in range ( i , j + 1 ) :
print ( arr [ k ] , end = "" )
print ( "\n" , end = "" )
</Code> |
python-cpp-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8_1 | python-cpp | def isSubSeqDivisible ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n + 1 ) ]
arr = [ 0 for i in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
arr [ i ] = int ( str [ i - 1 ] )
for i in range ( 1 , n + 1 ) :
dp [ i ] [ arr [ i ] % 8 ] = 1
fo... | bool isSubSeqDivisible ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ 10 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
int arr [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) arr [ i ] = str [ i - 1 ] - '0' ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] [ arr [ i ] % 8 ] = 1 ;
for ( ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isSubSeqDivisible ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n + 1 ) ]
arr = [ 0 for i in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
arr [ i ] = int ( str [ i - 1 ] )
for i in range ( 1 , n + 1 ) :
dp [ i ] [ arr [ i ] % 8 ] = 1
... |
python-cpp-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM | python-cpp | def solve ( i , par , a , n , k , current_ans ) :
if ( par > k ) :
return 0
global ans
if ( par == k and i == n - 1 ) :
ans = min ( ans , current_ans )
return 0
for j in range ( i + 1 , n ) :
solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a ... | void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) {
if ( par > k ) return ;
if ( par == k && i == n - 1 ) {
ans = min ( ans , current_ans ) ;
return ;
}
for ( int j = i + 1 ;
j < n ;
j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def solve ( i , par , a , n , k , current_ans ) :
if ( par > k ) :
return 0
global ans
if ( par == k and i == n - 1 ) :
ans = min ( ans , current_ans )
return 0
for j in range ( i + 1 , n ) :
solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] )... |
python-cpp-SQUARE_ROOT_OF_A_PERFECT_SQUARE | python-cpp | def squareRoot ( n ) :
x = n
y = 1
e = 0.000001
while ( x - y > e ) :
x = ( x + y ) / 2
y = n / x
return x
| public : float squareRoot ( float n ) {
float x = n ;
float y = 1 ;
float e = 0.000001 ;
while ( x - y > e ) {
x = ( x + y ) / 2 ;
y = n / x ;
}
return x ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def squareRoot ( n ) :
x = n
y = 1
e = 0.000001
while ( x - y > e ) :
x = ( x + y ) / 2
y = n / x
return x
</Code> |
python-cpp-COUNT_NUMBER_PAIRS_N_B_N_GCD_B_B | python-cpp | def CountPairs ( n ) :
k = n
imin = 1
ans = 0
while ( imin <= n ) :
imax = n / k
ans += k * ( imax - imin + 1 )
imin = imax + 1
k = n / imin
return ans
| int CountPairs ( int n ) {
int k = n ;
int imin = 1 ;
int ans = 0 ;
while ( imin <= n ) {
int imax = n / k ;
ans += k * ( imax - imin + 1 ) ;
imin = imax + 1 ;
k = n / imin ;
}
return ans ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def CountPairs ( n ) :
k = n
imin = 1
ans = 0
while ( imin <= n ) :
imax = n / k
ans += k * ( imax - imin + 1 )
imin = imax + 1
k = n / imin
return ans
</Code> |
python-cpp-MULTIPLY_AN_INTEGER_WITH_3_5 | python-cpp | def multiplyWith3Point5 ( x ) :
return ( x << 1 ) + x + ( x >> 1 )
| int multiplyWith3Point5 ( int x ) {
return ( x << 1 ) + x + ( x >> 1 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def multiplyWith3Point5 ( x ) :
return ( x << 1 ) + x + ( x >> 1 )
</Code> |
python-cpp-LEXICOGRAPHICALLY_PREVIOUS_PERMUTATION_IN_C | python-cpp | def prevPermutation ( str ) :
n = len ( str ) - 1
i = n
while ( i > 0 and str [ i - 1 ] <= str [ i ] ) :
i -= 1
if ( i <= 0 ) :
return False
j = i - 1
while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) :
j += 1
str = list ( str )
temp = str [ i - 1 ]
str ... | bool prevPermutation ( string & str ) {
int n = str . length ( ) - 1 ;
int i = n ;
while ( i > 0 && str [ i - 1 ] <= str [ i ] ) i -- ;
if ( i <= 0 ) return false ;
int j = i - 1 ;
while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) j ++ ;
swap ( str [ i - 1 ] , str [ j ] ) ;
reverse ( str . begin ( ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def prevPermutation ( str ) :
n = len ( str ) - 1
i = n
while ( i > 0 and str [ i - 1 ] <= str [ i ] ) :
i -= 1
if ( i <= 0 ) :
return False
j = i - 1
while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) :
j += 1
str = list ( str )
temp = str [ i - 1 ]
... |
python-cpp-AREA_OF_A_POLYGON_WITH_GIVEN_N_ORDERED_VERTICES | python-cpp | def polygonArea ( X , Y , n ) :
area = 0.0
j = n - 1
for i in range ( 0 , n ) :
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] )
j = i
return int ( abs ( area / 2.0 ) )
| double polygonArea ( double X [ ] , double Y [ ] , int n ) {
double area = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ;
j = i ;
}
return abs ( area / 2.0 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def polygonArea ( X , Y , n ) :
area = 0.0
j = n - 1
for i in range ( 0 , n ) :
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] )
j = i
return int ( abs ( area / 2.0 ) )
</Code> |
python-cpp-EQUILIBRIUM_INDEX_OF_AN_ARRAY | python-cpp | def equilibrium ( arr ) :
leftsum = 0
rightsum = 0
n = len ( arr )
for i in range ( n ) :
leftsum = 0
rightsum = 0
for j in range ( i ) :
leftsum += arr [ j ]
for j in range ( i + 1 , n ) :
rightsum += arr [ j ]
if leftsum == rightsum :
... | int equilibrium ( int arr [ ] , int n ) {
int i , j ;
int leftsum , rightsum ;
for ( i = 0 ;
i < n ;
++ i ) {
leftsum = 0 ;
for ( j = 0 ;
j < i ;
j ++ ) leftsum += arr [ j ] ;
rightsum = 0 ;
for ( j = i + 1 ;
j < n ;
j ++ ) rightsum += arr [ j ] ;
if ( leftsum == rightsum )... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def equilibrium ( arr ) :
leftsum = 0
rightsum = 0
n = len ( arr )
for i in range ( n ) :
leftsum = 0
rightsum = 0
for j in range ( i ) :
leftsum += arr [ j ]
for j in range ( i + 1 , n ) :
rightsum += arr [ j ]
if leftsum == rightsu... |
python-cpp-COUNT_WAYS_DIVIDE_CIRCLE_USING_N_NON_INTERSECTING_CHORDS | python-cpp | def chordCnt ( A ) :
n = 2 * A
dpArray = [ 0 ] * ( n + 1 )
dpArray [ 0 ] = 1
dpArray [ 2 ] = 1
for i in range ( 4 , n + 1 , 2 ) :
for j in range ( 0 , i - 1 , 2 ) :
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] )
return int ( dpArray [ n ] )
| int chordCnt ( int A ) {
int n = 2 * A ;
int dpArray [ n + 1 ] = {
0 }
;
dpArray [ 0 ] = 1 ;
dpArray [ 2 ] = 1 ;
for ( int i = 4 ;
i <= n ;
i += 2 ) {
for ( int j = 0 ;
j < i - 1 ;
j += 2 ) {
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def chordCnt ( A ) :
n = 2 * A
dpArray = [ 0 ] * ( n + 1 )
dpArray [ 0 ] = 1
dpArray [ 2 ] = 1
for i in range ( 4 , n + 1 , 2 ) :
for j in range ( 0 , i - 1 , 2 ) :
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] )
return int ( dpArray [ n ] )
</Code> |
python-cpp-FIND_HARMONIC_MEAN_USING_ARITHMETIC_MEAN_GEOMETRIC_MEAN | python-cpp | def compute ( a , b ) :
AM = ( a + b ) / 2
GM = math.sqrt ( a * b )
HM = ( GM * GM ) / AM
return HM
| double compute ( int a , int b ) {
double AM , GM , HM ;
AM = ( a + b ) / 2 ;
GM = sqrt ( a * b ) ;
HM = ( GM * GM ) / AM ;
return HM ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def compute ( a , b ) :
AM = ( a + b ) / 2
GM = math.sqrt ( a * b )
HM = ( GM * GM ) / AM
return HM
</Code> |
python-cpp-MAJORITY_ELEMENT | python-cpp | def findMajority ( arr , n ) :
maxCount = 0 ;
index = - 1
for i in range ( n ) :
count = 0
for j in range ( n ) :
if ( arr [ i ] == arr [ j ] ) :
count += 1
if ( count > maxCount ) :
maxCount = count
index = i
if ( maxCount > n ... | void findMajority ( int arr [ ] , int n ) {
int maxCount = 0 ;
int index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count > maxCount ) {
maxCount = count ;
index = i ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMajority ( arr , n ) :
maxCount = 0 ;
index = - 1
for i in range ( n ) :
count = 0
for j in range ( n ) :
if ( arr [ i ] == arr [ j ] ) :
count += 1
if ( count > maxCount ) :
maxCount = count
index = i
if ( maxCou... |
python-cpp-CHECK_NUMBER_IS_PERFECT_SQUARE_USING_ADDITIONSUBTRACTION | python-cpp | def isPerfectSquare ( n ) :
i = 1
the_sum = 0
while the_sum < n :
the_sum += i
if the_sum == n :
return True
i += 2
return False
| bool isPerfectSquare ( int n ) {
for ( int sum = 0 , i = 1 ;
sum < n ;
i += 2 ) {
sum += i ;
if ( sum == n ) return true ;
}
return false ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPerfectSquare ( n ) :
i = 1
the_sum = 0
while the_sum < n :
the_sum += i
if the_sum == n :
return True
i += 2
return False
</Code> |
python-cpp-FIND_THE_SMALLEST_WINDOW_IN_A_STRING_CONTAINING_ALL_CHARACTERS_OF_ANOTHER_STRING | python-cpp | def findSubString ( string , pat ) :
len1 = len ( string )
len2 = len ( pat )
if len1 < len2 :
print ( "No such window exists" )
return ""
hash_pat = [ 0 ] * no_of_chars
hash_str = [ 0 ] * no_of_chars
for i in range ( 0 , len2 ) :
hash_pat [ ord ( pat [ i ] ) ] += 1
s... | string findSubString ( string str , string pat ) {
int len1 = str . length ( ) ;
int len2 = pat . length ( ) ;
if ( len1 < len2 ) {
cout << "No such window exists" ;
return "" ;
}
int hash_pat [ no_of_chars ] = {
0 }
;
int hash_str [ no_of_chars ] = {
0 }
;
for ( int i = ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSubString ( string , pat ) :
len1 = len ( string )
len2 = len ( pat )
if len1 < len2 :
print ( "No such window exists" )
return ""
hash_pat = [ 0 ] * no_of_chars
hash_str = [ 0 ] * no_of_chars
for i in range ( 0 , len2 ) :
hash_pat [ ord ( pat [ i ] ) ] += ... |
python-cpp-NUMBER_SUBSEQUENCES_STRING_DIVISIBLE_N | python-cpp | def countDivisibleSubseq ( str , n ) :
l = len ( str )
dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ]
dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1
for i in range ( 1 , l ) :
dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1
for j in range ( n ) :
... | int countDivisibleSubseq ( string str , int n ) {
int len = str . length ( ) ;
int dp [ len ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ ( str [ 0 ] - '0' ) % n ] ++ ;
for ( int i = 1 ;
i < len ;
i ++ ) {
dp [ i ] [ ( str [ i ] - '0' ) % n ] ++ ;
for ( int j = 0 ;
j < n ;
j ++ ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countDivisibleSubseq ( str , n ) :
l = len ( str )
dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ]
dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1
for i in range ( 1 , l ) :
dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1
for j in range ( n ) :
... |
python-cpp-MINIMUM_POSSIBLE_VALUE_AI_AJ_K_GIVEN_ARRAY_K | python-cpp | def pairs ( arr , n , k ) :
smallest = 999999999999
count = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if abs ( arr [ i ] + arr [ j ] - k ) < smallest :
smallest = abs ( arr [ i ] + arr [ j ] - k )
count = 1
elif abs ( arr [ i ] + ... | void pairs ( int arr [ ] , int n , int k ) {
int smallest = INT_MAX ;
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) {
smallest = abs ( arr [ i ] + arr [ j ] - k ) ;
count = 1 ;
}
else if (... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def pairs ( arr , n , k ) :
smallest = 999999999999
count = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if abs ( arr [ i ] + arr [ j ] - k ) < smallest :
smallest = abs ( arr [ i ] + arr [ j ] - k )
count = 1
elif abs ( arr [... |
python-cpp-ZECKENDORFS_THEOREM_NON_NEIGHBOURING_FIBONACCI_REPRESENTATION | python-cpp | def nearestSmallerEqFib ( n ) :
if ( n == 0 or n == 1 ) :
return n
f1 , f2 , f3 = 0 , 1 , 1
while ( f3 <= n ) :
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
return f2 ;
| int nearestSmallerEqFib ( int n ) {
if ( n == 0 || n == 1 ) return n ;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while ( f3 <= n ) {
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
return f2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def nearestSmallerEqFib ( n ) :
if ( n == 0 or n == 1 ) :
return n
f1 , f2 , f3 = 0 , 1 , 1
while ( f3 <= n ) :
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
return f2 ;
</Code> |
python-cpp-SHUFFLE_A_DECK_OF_CARDS_3 | python-cpp | def shuffle ( card , n ) :
for i in range ( n ) :
r = i + ( random.randint ( 0 , 55 ) % ( 52 - i ) )
tmp = card [ i ]
card [ i ] = card [ r ]
card [ r ] = tmp
| void shuffle ( int card [ ] , int n ) {
srand ( time ( 0 ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int r = i + ( rand ( ) % ( 52 - i ) ) ;
swap ( card [ i ] , card [ r ] ) ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def shuffle ( card , n ) :
for i in range ( n ) :
r = i + ( random.randint ( 0 , 55 ) % ( 52 - i ) )
tmp = card [ i ]
card [ i ] = card [ r ]
card [ r ] = tmp
</Code> |
python-cpp-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM_1 | python-cpp | def maxLen ( arr ) :
hash_map = { }
max_len = 0
curr_sum = 0
for i in range ( len ( arr ) ) :
curr_sum += arr [ i ]
if arr [ i ] is 0 and max_len is 0 :
max_len = 1
if curr_sum is 0 :
max_len = i + 1
if curr_sum in hash_map :
max_len = ... | int maxLen ( int arr [ ] , int n ) {
unordered_map < int , int > presum ;
int sum = 0 ;
int max_len = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ;
if ( sum == 0 ) max_len = i + 1 ;
if ( presum . find ( sum ) != presum . end ( )... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxLen ( arr ) :
hash_map = { }
max_len = 0
curr_sum = 0
for i in range ( len ( arr ) ) :
curr_sum += arr [ i ]
if arr [ i ] is 0 and max_len is 0 :
max_len = 1
if curr_sum is 0 :
max_len = i + 1
if curr_sum in hash_map :
max... |
python-cpp-LENGTH_LONGEST_BALANCED_SUBSEQUENCE | python-cpp | def maxLength ( s , n ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
for i in range ( n - 1 ) :
if ( s [ i ] == '(' and s [ i + 1 ] == ')' ) :
dp [ i ] [ i + 1 ] = 2
for l in range ( 2 , n ) :
i = - 1
for j in range ( l , n ) :
i += 1
... | int maxLength ( char s [ ] , int n ) {
int dp [ n ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( s [ i ] == '(' && s [ i + 1 ] == ')' ) dp [ i ] [ i + 1 ] = 2 ;
for ( int l = 2 ;
l < n ;
l ++ ) {
for ( int i = 0 , j = l ;
j < n ;
i ++ , j ++ ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxLength ( s , n ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
for i in range ( n - 1 ) :
if ( s [ i ] == '(' and s [ i + 1 ] == ')' ) :
dp [ i ] [ i + 1 ] = 2
for l in range ( 2 , n ) :
i = - 1
for j in range ( l , n ) :
i += 1
... |
python-cpp-PROGRAM_PRINT_SUM_GIVEN_NTH_TERM_1 | python-cpp | def summingSeries ( n ) :
return math.pow ( n , 2 )
| int summingSeries ( long n ) {
return pow ( n , 2 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def summingSeries ( n ) :
return math.pow ( n , 2 )
</Code> |
python-cpp-SUBSET_SUM_PROBLEM_OSUM_SPACE | python-cpp | def isSubsetSum ( arr , n , sum ) :
subset = [ [ False for j in range ( sum + 1 ) ] for i in range ( 3 ) ]
for i in range ( n + 1 ) :
for j in range ( sum + 1 ) :
if ( j == 0 ) :
subset [ i % 2 ] [ j ] = True
elif ( i == 0 ) :
subset [ i % 2 ] [ j ... | bool isSubsetSum ( int arr [ ] , int n , int sum ) {
bool subset [ 2 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= sum ;
j ++ ) {
if ( j == 0 ) subset [ i % 2 ] [ j ] = true ;
else if ( i == 0 ) subset [ i % 2 ] [ j ] = false ;
else if ( arr [ i - 1 ] <... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isSubsetSum ( arr , n , sum ) :
subset = [ [ False for j in range ( sum + 1 ) ] for i in range ( 3 ) ]
for i in range ( n + 1 ) :
for j in range ( sum + 1 ) :
if ( j == 0 ) :
subset [ i % 2 ] [ j ] = True
elif ( i == 0 ) :
subset [ i % 2... |
python-cpp-FIND_INDEX_OF_AN_EXTRA_ELEMENT_PRESENT_IN_ONE_SORTED_ARRAY | python-cpp | def findExtra ( arr1 , arr2 , n ) :
for i in range ( 0 , n ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return i
return n
| int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ;
return n ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findExtra ( arr1 , arr2 , n ) :
for i in range ( 0 , n ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return i
return n
</Code> |
python-cpp-NUMBER_OF_PERMUTATION_WITH_K_INVERSIONS | python-cpp | def numberOfPermWithKInversion ( N , K ) :
if ( N == 0 ) : return 0
if ( K == 0 ) : return 1
if ( memo [ N ] [ K ] != 0 ) :
return memo [ N ] [ K ]
sum = 0
for i in range ( K + 1 ) :
if ( i <= N - 1 ) :
sum += numberOfPermWithKInversion ( N - 1 , K - i )
memo [ N ] [ ... | int numberOfPermWithKInversion ( int N , int K ) {
if ( N == 0 ) return 0 ;
if ( K == 0 ) return 1 ;
if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ;
int sum = 0 ;
for ( int i = 0 ;
i <= K ;
i ++ ) {
if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ;
}
memo [ N ] [ K ] =... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfPermWithKInversion ( N , K ) :
if ( N == 0 ) : return 0
if ( K == 0 ) : return 1
if ( memo [ N ] [ K ] != 0 ) :
return memo [ N ] [ K ]
sum = 0
for i in range ( K + 1 ) :
if ( i <= N - 1 ) :
sum += numberOfPermWithKInversion ( N - 1 , K - i )
memo [... |
python-cpp-DELETE_CONSECUTIVE_WORDS_SEQUENCE_1 | python-cpp | def removeConsecutiveSame ( v ) :
st = [ ]
for i in range ( len ( v ) ) :
if ( len ( st ) == 0 ) :
st.append ( v [ i ] )
else :
Str = st [ - 1 ]
if ( Str == v [ i ] ) :
st.pop ( )
else :
st.append ( v [ i ] )
ret... | int removeConsecutiveSame ( vector < string > v ) {
stack < string > st ;
for ( int i = 0 ;
i < v . size ( ) ;
i ++ ) {
if ( st . empty ( ) ) st . push ( v [ i ] ) ;
else {
string str = st . top ( ) ;
if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ;
else st . push ( v [ i ] ) ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def removeConsecutiveSame ( v ) :
st = [ ]
for i in range ( len ( v ) ) :
if ( len ( st ) == 0 ) :
st.append ( v [ i ] )
else :
Str = st [ - 1 ]
if ( Str == v [ i ] ) :
st.pop ( )
else :
st.append ( v [ i ] )
... |
python-cpp-MINIMUM_INDEX_SUM_COMMON_ELEMENTS_TWO_LISTS | python-cpp | def find ( list1 , list2 ) :
res = [ ]
max_possible_sum = len ( list1 ) + len ( list2 ) - 2
for sum in range ( max_possible_sum + 1 ) :
for i in range ( sum + 1 ) :
if ( i < len ( list1 ) and ( sum - i ) < len ( list2 ) and list1 [ i ] == list2 [ sum - i ] ) :
res.append ... | void find ( vector < string > list1 , vector < string > list2 ) {
vector < string > res ;
int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ;
for ( int sum = 0 ;
sum <= max_possible_sum ;
sum ++ ) {
for ( int i = 0 ;
i <= sum ;
i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < list2 .... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def find ( list1 , list2 ) :
res = [ ]
max_possible_sum = len ( list1 ) + len ( list2 ) - 2
for sum in range ( max_possible_sum + 1 ) :
for i in range ( sum + 1 ) :
if ( i < len ( list1 ) and ( sum - i ) < len ( list2 ) and list1 [ i ] == list2 [ sum - i ] ) :
res.... |
python-cpp-NON_REPEATING_ELEMENT | python-cpp | def firstNonRepeating ( arr , n ) :
for i in range ( n ) :
j = 0
while ( j < n ) :
if ( i != j and arr [ i ] == arr [ j ] ) :
break
j += 1
if ( j == n ) :
return arr [ i ]
return - 1
| int firstNonRepeating ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < n ;
j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ;
if ( j == n ) return arr [ i ] ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def firstNonRepeating ( arr , n ) :
for i in range ( n ) :
j = 0
while ( j < n ) :
if ( i != j and arr [ i ] == arr [ j ] ) :
break
j += 1
if ( j == n ) :
return arr [ i ]
return - 1
</Code> |
python-cpp-HOW_TO_BEGIN_WITH_COMPETITIVE_PROGRAMMING | python-cpp | def search ( arr , x ) :
n = len ( arr )
for j in range ( 0 , n ) :
if ( x == arr [ j ] ) :
return j
return - 1
| int search ( int arr [ ] , int n , int x ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , x ) :
n = len ( arr )
for j in range ( 0 , n ) :
if ( x == arr [ j ] ) :
return j
return - 1
</Code> |
python-cpp-SUM_MIDDLE_ROW_COLUMN_MATRIX | python-cpp | def middlesum ( mat , n ) :
row_sum = 0
col_sum = 0
for i in range ( n ) :
row_sum += mat [ n // 2 ] [ i ]
print ( "Sum of middle row = " , row_sum )
for i in range ( n ) :
col_sum += mat [ i ] [ n // 2 ]
print ( "Sum of middle column = " , col_sum )
| void middlesum ( int mat [ ] [ MAX ] , int n ) {
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) row_sum += mat [ n / 2 ] [ i ] ;
cout << "Sum of middle row = " << row_sum << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) col_sum += mat [ i ] [ n / 2 ] ;
cout << "Sum of middle column = " << ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def middlesum ( mat , n ) :
row_sum = 0
col_sum = 0
for i in range ( n ) :
row_sum += mat [ n // 2 ] [ i ]
print ( "Sum of middle row = " , row_sum )
for i in range ( n ) :
col_sum += mat [ i ] [ n // 2 ]
print ( "Sum of middle column = " , col_sum )
</Code> |
python-cpp-MULTIPLY_LARGE_INTEGERS_UNDER_LARGE_MODULO | python-cpp | def moduloMultiplication ( a , b , mod ) :
res = 0 ;
a = a % mod ;
while ( b ) :
if ( b & 1 ) :
res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
return res ;
| long long moduloMultiplication ( long long a , long long b , long long mod ) {
long long res = 0 ;
a %= mod ;
while ( b ) {
if ( b & 1 ) res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def moduloMultiplication ( a , b , mod ) :
res = 0 ;
a = a % mod ;
while ( b ) :
if ( b & 1 ) :
res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
return res ;
</Code> |
python-cpp-GENERATE_PYTHAGOREAN_TRIPLETS | python-cpp | def pythagoreanTriplets ( limits ) :
c , m = 0 , 2
while c < limits :
for n in range ( 1 , m ) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits :
break
print ( a , b , c )
m = m + 1
| void pythagoreanTriplets ( int limit ) {
int a , b , c = 0 ;
int m = 2 ;
while ( c < limit ) {
for ( int n = 1 ;
n < m ;
++ n ) {
a = m * m - n * n ;
b = 2 * m * n ;
c = m * m + n * n ;
if ( c > limit ) break ;
printf ( "%d %d %d\n" , a , b , c ) ;
}
m ++ ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def pythagoreanTriplets ( limits ) :
c , m = 0 , 2
while c < limits :
for n in range ( 1 , m ) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits :
break
print ( a , b , c )
m = m + 1
</Code> |
python-cpp-SUM_PAIRWISE_PRODUCTS_2 | python-cpp | def findSum ( n ) :
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24
| long long int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( n ) :
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24
</Code> |
python-cpp-PROGRAM_FOR_NTH_FIBONACCI_NUMBER_2 | python-cpp | def fib ( n ) :
if ( n == 0 ) :
return 0
if ( n == 1 or n == 2 ) :
f [ n ] = 1
return ( f [ n ] )
if ( f [ n ] ) :
return f [ n ]
if ( n & 1 ) :
k = ( n + 1 ) // 2
else :
k = n // 2
if ( ( n & 1 ) ) :
f [ n ] = ( fib ( k ) * fib ( k ) + fib... | int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] ) return f [ n ] ;
int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ;
return f... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def fib ( n ) :
if ( n == 0 ) :
return 0
if ( n == 1 or n == 2 ) :
f [ n ] = 1
return ( f [ n ] )
if ( f [ n ] ) :
return f [ n ]
if ( n & 1 ) :
k = ( n + 1 ) // 2
else :
k = n // 2
if ( ( n & 1 ) ) :
f [ n ] = ( fib ( k ) * fib ( k ... |
python-cpp-GIVEN_P_AND_N_FIND_THE_LARGEST_X_SUCH_THAT_PX_DIVIDES_N_2 | python-cpp | def largestPower ( n , p ) :
x = 0
while n :
n /= p
x += n
return x
| int largestPower ( int n , int p ) {
int x = 0 ;
while ( n ) {
n /= p ;
x += n ;
}
return x ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def largestPower ( n , p ) :
x = 0
while n :
n /= p
x += n
return x
</Code> |
python-cpp-PROGRAM_PRINT_IDENTITY_MATRIX_1 | python-cpp | def isIdentity ( mat , N ) :
for row in range ( N ) :
for col in range ( N ) :
if ( row == col and mat [ row ] [ col ] != 1 ) :
return False
elif ( row != col and mat [ row ] [ col ] != 0 ) :
return False
return True
| bool isIdentity ( int mat [ ] [ MAX ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isIdentity ( mat , N ) :
for row in range ( N ) :
for col in range ( N ) :
if ( row == col and mat [ row ] [ col ] != 1 ) :
return False
elif ( row != col and mat [ row ] [ col ] != 0 ) :
return False
return True
</Code> |
python-cpp-MINIMUM_CELLS_REQUIRED_REACH_DESTINATION_JUMPS_EQUAL_CELL_VALUES | python-cpp | def minCells ( mat , m , n ) :
dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( m ) :
for j in range ( n ) :
if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) :
... | int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) {
int dp [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = INT_MAX ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( dp [ i ] [ ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minCells ( mat , m , n ) :
dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( m ) :
for j in range ( n ) :
if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) :
... |
python-cpp-REARRANGE_BINARY_STRING_ALTERNATE_X_Y_OCCURRENCES | python-cpp | def arrangeString ( str1 , x , y ) :
count_0 = 0
count_1 = 0
n = len ( str1 )
for i in range ( n ) :
if str1 [ i ] == '0' :
count_0 += 1
else :
count_1 += 1
while count_0 > 0 or count_1 > 0 :
for i in range ( 0 , x ) :
if count_0 > 0 :
... | void arrangeString ( string str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str [ i ] == '0' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j < x && count_0 > 0... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def arrangeString ( str1 , x , y ) :
count_0 = 0
count_1 = 0
n = len ( str1 )
for i in range ( n ) :
if str1 [ i ] == '0' :
count_0 += 1
else :
count_1 += 1
while count_0 > 0 or count_1 > 0 :
for i in range ( 0 , x ) :
if count_0 > 0... |
python-cpp-MINIMUM_NUMBER_DELETIONS_MAKE_STRING_PALINDROME_SET_2 | python-cpp | def getLevenstein ( inpt ) :
revInput = inpt [ : : - 1 ]
n = len ( inpt )
dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ 0 ] [ i ] = i
dp [ i ] [ 0 ] = i
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
... | int getLevenstein ( string const & input ) {
string revInput ( input . rbegin ( ) , input . rend ( ) ) ;
int n = input . size ( ) ;
vector < vector < int >> dp ( n + 1 , vector < int > ( n + 1 , - 1 ) ) ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
}
for ( int i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getLevenstein ( inpt ) :
revInput = inpt [ : : - 1 ]
n = len ( inpt )
dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ 0 ] [ i ] = i
dp [ i ] [ 0 ] = i
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
... |
python-cpp-DYNAMIC_PROGRAMMING_SET_34_ASSEMBLY_LINE_SCHEDULING | python-cpp | def carAssembly ( a , t , e , x ) :
NUM_STATION = len ( a [ 0 ] )
T1 = [ 0 for i in range ( NUM_STATION ) ]
T2 = [ 0 for i in range ( NUM_STATION ) ]
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ]
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ]
for i in range ( 1 , NUM_STATION ) :
T1 [ i ] = min ( T1 [ i - 1 ] +... | int carAssembly ( int a [ ] [ NUM_STATION ] , int t [ ] [ NUM_STATION ] , int * e , int * x ) {
int T1 [ NUM_STATION ] , T2 [ NUM_STATION ] , i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def carAssembly ( a , t , e , x ) :
NUM_STATION = len ( a [ 0 ] )
T1 = [ 0 for i in range ( NUM_STATION ) ]
T2 = [ 0 for i in range ( NUM_STATION ) ]
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ]
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ]
for i in range ( 1 , NUM_STATION ) :
T1 [ i ] = min ( T1 [ i ... |
python-cpp-BASIC_AND_EXTENDED_EUCLIDEAN_ALGORITHMS | python-cpp | def gcd ( a , b ) :
if a == 0 :
return b
return gcd ( b % a , a )
| int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def gcd ( a , b ) :
if a == 0 :
return b
return gcd ( b % a , a )
</Code> |
python-cpp-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_2 | python-cpp | def findTriplets ( arr , n ) :
found = False
arr.sort ( )
for i in range ( 0 , n - 1 ) :
l = i + 1
r = n - 1
x = arr [ i ]
while ( l < r ) :
if ( x + arr [ l ] + arr [ r ] == 0 ) :
print ( x , arr [ l ] , arr [ r ] )
l += 1
... | void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
printf ( "%d %d %d\n" , x , arr [ l ] , a... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findTriplets ( arr , n ) :
found = False
arr.sort ( )
for i in range ( 0 , n - 1 ) :
l = i + 1
r = n - 1
x = arr [ i ]
while ( l < r ) :
if ( x + arr [ l ] + arr [ r ] == 0 ) :
print ( x , arr [ l ] , arr [ r ] )
l += 1
... |
python-cpp-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES_2 | python-cpp | def getOddOccurrence ( arr ) :
res = 0
for element in arr :
res = res ^ element
return res
| int getOddOccurrence ( int ar [ ] , int ar_size ) {
int res = 0 ;
for ( int i = 0 ;
i < ar_size ;
i ++ ) res = res ^ ar [ i ] ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getOddOccurrence ( arr ) :
res = 0
for element in arr :
res = res ^ element
return res
</Code> |
python-cpp-DYNAMIC_PROGRAMMING_HIGH_EFFORT_VS_LOW_EFFORT_TASKS_PROBLEM | python-cpp | def maxTasks ( high , low , n ) :
if ( n <= 0 ) :
return 0
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) )
| int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxTasks ( high , low , n ) :
if ( n <= 0 ) :
return 0
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) )
</Code> |
python-cpp-FIND_TWO_SIDES_RIGHT_ANGLE_TRIANGLE | python-cpp | def printOtherSides ( n ) :
if ( n & 1 ) :
if ( n == 1 ) :
print ( - 1 )
else :
b = ( n * n - 1 ) // 2
c = ( n * n + 1 ) // 2
print ( "b =" , b , ", c =" , c )
else :
if ( n == 2 ) :
print ( - 1 )
else :
b = ... | void printOtherSides ( int n ) {
if ( n & 1 ) {
if ( n == 1 ) cout << - 1 << endl ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
cout << "b = " << b << ", c = " << c << endl ;
}
}
else {
if ( n == 2 ) cout << - 1 << endl ;
else {
int b = n * n / 4 - 1 ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printOtherSides ( n ) :
if ( n & 1 ) :
if ( n == 1 ) :
print ( - 1 )
else :
b = ( n * n - 1 ) // 2
c = ( n * n + 1 ) // 2
print ( "b =" , b , ", c =" , c )
else :
if ( n == 2 ) :
print ( - 1 )
else :
... |
python-cpp-MINIMUM_LENGTH_UNSORTED_SUBARRAY_SORTING_WHICH_MAKES_THE_COMPLETE_ARRAY_SORTED | python-cpp | def printUnsorted ( arr , n ) :
e = n - 1
for s in range ( 0 , n - 1 ) :
if arr [ s ] > arr [ s + 1 ] :
break
if s == n - 1 :
print ( "The complete array is sorted" )
exit ( )
e = n - 1
while e > 0 :
if arr [ e ] < arr [ e - 1 ] :
break
... | void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
cout << "The complete array is sorted" ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e -- ) {
if ( arr [ e ]... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printUnsorted ( arr , n ) :
e = n - 1
for s in range ( 0 , n - 1 ) :
if arr [ s ] > arr [ s + 1 ] :
break
if s == n - 1 :
print ( "The complete array is sorted" )
exit ( )
e = n - 1
while e > 0 :
if arr [ e ] < arr [ e - 1 ] :
break
... |
python-cpp-CHECK_TWO_GIVEN_CIRCLES_TOUCH_INTERSECT | python-cpp | def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) :
distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 )
radSumSq = ( r1 + r2 ) * ( r1 + r2 )
if ( distSq == radSumSq ) :
return 1
elif ( distSq > radSumSq ) :
return - 1
else :
return 0
| int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) :
distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 )
radSumSq = ( r1 + r2 ) * ( r1 + r2 )
if ( distSq == radSumSq ) :
return 1
elif ( distSq > radSumSq ) :
return - 1
else :
return 0
</Code> |
python-cpp-COUNT_DISTINCT_ELEMENTS_IN_EVERY_WINDOW_OF_SIZE_K | python-cpp | def countDistinct ( arr , k , n ) :
mp = defaultdict ( lambda : 0 )
dist_count = 0
for i in range ( k ) :
if mp [ arr [ i ] ] == 0 :
dist_count += 1
mp [ arr [ i ] ] += 1
print ( dist_count )
for i in range ( k , n ) :
if mp [ arr [ i - k ] ] == 1 :
di... | void countDistinct ( int arr [ ] , int k , int n ) {
map < int , int > hm ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hm [ arr [ i ] ] == 0 ) {
dist_count ++ ;
}
hm [ arr [ i ] ] += 1 ;
}
cout << dist_count << endl ;
for ( int i = k ;
i < n ;
i ++ ) {
if ( hm... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countDistinct ( arr , k , n ) :
mp = defaultdict ( lambda : 0 )
dist_count = 0
for i in range ( k ) :
if mp [ arr [ i ] ] == 0 :
dist_count += 1
mp [ arr [ i ] ] += 1
print ( dist_count )
for i in range ( k , n ) :
if mp [ arr [ i - k ] ] == 1 :
... |
python-cpp-PROGRAM_TO_FIND_THE_AREA_OF_PENTAGON | python-cpp | def findArea ( a ) :
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4
return area
| float findArea ( float a ) {
float area ;
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findArea ( a ) :
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4
return area
</Code> |
python-cpp-COUNT_ROTATIONS_DIVISIBLE_8 | python-cpp | def countRotationsDivBy8 ( n ) :
l = len ( n )
count = 0
if ( l == 1 ) :
oneDigit = int ( n [ 0 ] )
if ( oneDigit % 8 == 0 ) :
return 1
return 0
if ( l == 2 ) :
first = int ( n [ 0 ] ) * 10 + int ( n [ 1 ] )
second = int ( n [ 1 ] ) * 10 + int ( n [ 0 ... | int countRotationsDivBy8 ( string n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n [ 0 ] - '0' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ;
int second = ( n [ 1 ] - '0' ) * 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countRotationsDivBy8 ( n ) :
l = len ( n )
count = 0
if ( l == 1 ) :
oneDigit = int ( n [ 0 ] )
if ( oneDigit % 8 == 0 ) :
return 1
return 0
if ( l == 2 ) :
first = int ( n [ 0 ] ) * 10 + int ( n [ 1 ] )
second = int ( n [ 1 ] ) * 10 + int (... |
python-cpp-LONGEST_COMMON_SUBSEQUENCE_WITH_AT_MOST_K_CHANGES_ALLOWED | python-cpp | def lcs ( dp , arr1 , n , arr2 , m , k ) :
if k < 0 :
return - ( 10 ** 7 )
if n < 0 or m < 0 :
return 0
ans = dp [ n ] [ m ] [ k ]
if ans != - 1 :
return ans
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) )
if arr1 [ n - 1 ... | int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) {
if ( k < 0 ) return - 1e7 ;
if ( n < 0 || m < 0 ) return 0 ;
int & ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lcs ( dp , arr1 , n , arr2 , m , k ) :
if k < 0 :
return - ( 10 ** 7 )
if n < 0 or m < 0 :
return 0
ans = dp [ n ] [ m ] [ k ]
if ans != - 1 :
return ans
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) )
if arr1 [... |
python-cpp-DYNAMIC_PROGRAMMING_SET_5_EDIT_DISTANCE_1 | python-cpp | def editDistDP ( str1 , str2 , m , n ) :
dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if i == 0 :
dp [ i ] [ j ] = j
elif j == 0 :
dp [ i ] [ j ] = i
elif str1 [ ... | int editDistDP ( string str1 , string str2 , 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 == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def editDistDP ( str1 , str2 , m , n ) :
dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if i == 0 :
dp [ i ] [ j ] = j
elif j == 0 :
dp [ i ] [ j ] = i
elif ... |
python-cpp-FIND_NUMBER_TRANSFORMATION_MAKE_TWO_MATRIX_EQUAL | python-cpp | def countOps ( A , B , m , n ) :
for i in range ( n ) :
for j in range ( m ) :
A [ i ] [ j ] -= B [ i ] [ j ]
for i in range ( 1 , n ) :
for j in range ( 1 , n ) :
if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) :
return - 1
r... | int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countOps ( A , B , m , n ) :
for i in range ( n ) :
for j in range ( m ) :
A [ i ] [ j ] -= B [ i ] [ j ]
for i in range ( 1 , n ) :
for j in range ( 1 , n ) :
if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ 0 ] != 0 ) :
return - ... |
python-cpp-LONGEST_ALTERNATING_POSITIVE_NEGATIVE_SUBARRAY_STARTING_EVERY_INDEX | python-cpp | def longestAlternating ( arr , n ) :
count = [ None ] * n
count [ n - 1 ] = 1
i = n - 2
while i >= 0 :
if ( arr [ i ] * arr [ i + 1 ] < 0 ) :
count [ i ] = count [ i + 1 ] + 1
else :
count [ i ] = 1
i = i - 1
i = 0
while i < n :
print ( cou... | void longestAlternating ( int arr [ ] , int n ) {
int count [ n ] ;
count [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ;
else count [ i ] = 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << count [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def longestAlternating ( arr , n ) :
count = [ None ] * n
count [ n - 1 ] = 1
i = n - 2
while i >= 0 :
if ( arr [ i ] * arr [ i + 1 ] < 0 ) :
count [ i ] = count [ i + 1 ] + 1
else :
count [ i ] = 1
i = i - 1
i = 0
while i < n :
prin... |
python-cpp-FIND_ALL_DIVISORS_OF_A_NATURAL_NUMBER_SET_2 | python-cpp | def printDivisors ( n ) :
list = [ ]
for i in range ( 1 , int ( math.sqrt ( n ) + 1 ) ) :
if ( n % i == 0 ) :
if ( n / i == i ) :
print ( i , end = " " )
else :
print ( i , end = " " )
list.append ( int ( n / i ) )
for i in list... | void printDivisors ( int n ) {
vector < int > v ;
for ( int i = 1 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) printf ( "%d " , i ) ;
else {
printf ( "%d " , i ) ;
v . push_back ( n / i ) ;
}
}
}
for ( int i = v . size ( ) - 1 ;
i >= 0 ;
i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printDivisors ( n ) :
list = [ ]
for i in range ( 1 , int ( math.sqrt ( n ) + 1 ) ) :
if ( n % i == 0 ) :
if ( n / i == i ) :
print ( i , end = " " )
else :
print ( i , end = " " )
list.append ( int ( n / i ) )
for i ... |
python-cpp-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER | python-cpp | def isPower ( x , y ) :
if ( x == 1 ) :
return ( y == 1 )
pow = 1
while ( pow < y ) :
pow = pow * x
return ( pow == y )
| bool isPower ( int x , long int y ) {
if ( x == 1 ) return ( y == 1 ) ;
long int pow = 1 ;
while ( pow < y ) pow *= x ;
return ( pow == y ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPower ( x , y ) :
if ( x == 1 ) :
return ( y == 1 )
pow = 1
while ( pow < y ) :
pow = pow * x
return ( pow == y )
</Code> |
python-cpp-MAXIMUM_SUM_SUBSEQUENCE_LEAST_K_DISTANT_ELEMENTS | python-cpp | def maxSum ( arr , N , k ) :
MS = [ 0 for i in range ( N ) ]
MS [ N - 1 ] = arr [ N - 1 ]
for i in range ( N - 2 , - 1 , - 1 ) :
if ( i + k + 1 >= N ) :
MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] )
else :
MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] )
... | int maxSum ( int arr [ ] , int N , int k ) {
int MS [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ;
}
return MS [ 0 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSum ( arr , N , k ) :
MS = [ 0 for i in range ( N ) ]
MS [ N - 1 ] = arr [ N - 1 ]
for i in range ( N - 2 , - 1 , - 1 ) :
if ( i + k + 1 >= N ) :
MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] )
else :
MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + ... |
python-cpp-GROUP_OCCURRENCES_CHARACTERS_ACCORDING_FIRST_APPEARANCE | python-cpp | def printGrouped ( string ) :
n = len ( string )
count = [ 0 ] * MAX_CHAR
for i in range ( n ) :
count [ ord ( string [ i ] ) - ord ( "a" ) ] += 1
for i in range ( n ) :
while count [ ord ( string [ i ] ) - ord ( "a" ) ] :
print ( string [ i ] , end = "" )
count [... | void printGrouped ( string str ) {
int n = str . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str [ i ] - 'a' ] -- ) cout << str [ i ] ;
count [ str [ i ] -... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printGrouped ( string ) :
n = len ( string )
count = [ 0 ] * MAX_CHAR
for i in range ( n ) :
count [ ord ( string [ i ] ) - ord ( "a" ) ] += 1
for i in range ( n ) :
while count [ ord ( string [ i ] ) - ord ( "a" ) ] :
print ( string [ i ] , end = "" )
... |
python-cpp-COUNT_SORTED_ROWS_MATRIX | python-cpp | def sortedCount ( mat , r , c ) :
result = 0
for i in range ( r ) :
j = 0
for j in range ( c - 1 ) :
if mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] :
break
if j == c - 2 :
result += 1
for i in range ( 0 , r ) :
j = 0
for j in range (... | int sortedCount ( int mat [ ] [ MAX ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortedCount ( mat , r , c ) :
result = 0
for i in range ( r ) :
j = 0
for j in range ( c - 1 ) :
if mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] :
break
if j == c - 2 :
result += 1
for i in range ( 0 , r ) :
j = 0
for j in ... |
python-cpp-COUNT_DISTINCT_SUBSEQUENCES | python-cpp | def countSub ( ss ) :
last = [ - 1 for i in range ( MAX_CHAR + 1 ) ]
n = len ( ss )
dp = [ - 2 for i in range ( n + 1 ) ]
dp [ 0 ] = 1
for i in range ( 1 , n + 1 ) :
dp [ i ] = 2 * dp [ i - 1 ]
if last [ ord ( ss [ i - 1 ] ) ] != - 1 :
dp [ i ] = dp [ i ] - dp [ last [ or... | int countSub ( string str ) {
vector < int > last ( MAX_CHAR , - 1 ) ;
int n = str . length ( ) ;
int dp [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ str [ i - 1 ] ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ str [ i - 1 ] ] ] ;
last ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSub ( ss ) :
last = [ - 1 for i in range ( MAX_CHAR + 1 ) ]
n = len ( ss )
dp = [ - 2 for i in range ( n + 1 ) ]
dp [ 0 ] = 1
for i in range ( 1 , n + 1 ) :
dp [ i ] = 2 * dp [ i - 1 ]
if last [ ord ( ss [ i - 1 ] ) ] != - 1 :
dp [ i ] = dp [ i ] - dp [ la... |
python-cpp-LARGEST_INCREASING_SUBSEQUENCE_OF_CONSECUTIVE_INTEGERS | python-cpp | def findLIS ( A , n ) :
hash = dict ( )
LIS_size , LIS_index = 1 , 0
hash [ A [ 0 ] ] = 1
for i in range ( 1 , n ) :
if A [ i ] - 1 not in hash :
hash [ A [ i ] - 1 ] = 0
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1
if LIS_size < hash [ A [ i ] ] :
LIS_size... | int findLIS ( int A [ ] , int n ) {
unordered_map < int , int > hash ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash [ A [ 0 ] ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ;
if ( LIS_size < hash [ A [ i ] ] ) {
LIS_size = hash [ A [ i ] ] ;
LIS_i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLIS ( A , n ) :
hash = dict ( )
LIS_size , LIS_index = 1 , 0
hash [ A [ 0 ] ] = 1
for i in range ( 1 , n ) :
if A [ i ] - 1 not in hash :
hash [ A [ i ] - 1 ] = 0
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1
if LIS_size < hash [ A [ i ] ] :
L... |
python-cpp-FIND_THE_MAXIMUM_OF_MINIMUMS_FOR_EVERY_WINDOW_SIZE_IN_A_GIVEN_ARRAY | python-cpp | def printMaxOfMin ( arr , n ) :
for k in range ( 1 , n + 1 ) :
maxOfMin = INT_MIN
for i in range ( n - k + 1 ) :
min = arr [ i ]
for j in range ( k ) :
if ( arr [ i + j ] < min ) :
min = arr [ i + j ]
if ( min > maxOfMin ) :
... | void printMaxOfMin ( int arr [ ] , int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = INT_MIN ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printMaxOfMin ( arr , n ) :
for k in range ( 1 , n + 1 ) :
maxOfMin = INT_MIN
for i in range ( n - k + 1 ) :
min = arr [ i ]
for j in range ( k ) :
if ( arr [ i + j ] < min ) :
min = arr [ i + j ]
if ( min > maxOfMin ... |
python-cpp-MAXIMUM_NUMBER_OF_TRAILING_ZEROS_IN_THE_PRODUCT_OF_THE_SUBSETS_OF_SIZE_K | python-cpp | def maximumZeros ( arr , n , k ) :
global MAX5
subset = [ [ - 1 ] * ( MAX5 + 5 ) for _ in range ( k + 1 ) ]
subset [ 0 ] [ 0 ] = 0
for p in arr :
pw2 , pw5 = 0 , 0
while not p % 2 :
pw2 += 1
p //= 2
while not p % 5 :
pw5 += 1
p //= ... | int maximumZeros ( int * arr , int n , int k ) {
int subset [ k + 1 ] [ MAX5 + 5 ] ;
memset ( subset , - 1 , sizeof ( subset ) ) ;
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
pw2 ++ ;
arr [ p ] /= 2 ;
}
while ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maximumZeros ( arr , n , k ) :
global MAX5
subset = [ [ - 1 ] * ( MAX5 + 5 ) for _ in range ( k + 1 ) ]
subset [ 0 ] [ 0 ] = 0
for p in arr :
pw2 , pw5 = 0 , 0
while not p % 2 :
pw2 += 1
p //= 2
while not p % 5 :
pw5 += 1
... |
python-cpp-LARGEST_DIVISIBLE_PAIRS_SUBSET | python-cpp | def largestSubset ( a , n ) :
a.sort ( )
dp = [ 0 for i in range ( n ) ]
dp [ n - 1 ] = 1
for i in range ( n - 2 , - 1 , - 1 ) :
mxm = 0
for j in range ( i + 1 , n ) :
if a [ j ] % a [ i ] == 0 :
mxm = max ( mxm , dp [ j ] )
dp [ i ] = 1 + mxm
retu... | int largestSubset ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int dp [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( a [ j ] % a [ i ] == 0 ) mxm = max ( mxm , dp [ j ] ) ;
dp [ i ] = 1 + mxm ;
}
return * max... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def largestSubset ( a , n ) :
a.sort ( )
dp = [ 0 for i in range ( n ) ]
dp [ n - 1 ] = 1
for i in range ( n - 2 , - 1 , - 1 ) :
mxm = 0
for j in range ( i + 1 , n ) :
if a [ j ] % a [ i ] == 0 :
mxm = max ( mxm , dp [ j ] )
dp [ i ] = 1 + mxm
... |
python-cpp-FIND_KTH_CHARACTER_OF_DECRYPTED_STRING | python-cpp | def encodedChar ( str , k ) :
expand = ""
freq = 0
i = 0
while ( i < len ( str ) ) :
temp = ""
freq = 0
while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( 'a' ) and ord ( str [ i ] ) <= ord ( 'z' ) ) :
temp += str [ i ]
i += 1
while ( i < len (... | char encodedChar ( string str , int k ) {
string expand = "" ;
string temp ;
int freq = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
) {
temp = "" ;
freq = 0 ;
while ( str [ i ] >= 'a' && str [ i ] <= 'z' ) {
temp . push_back ( str [ i ] ) ;
i ++ ;
}
while ( str [ i ] >= '1' && ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def encodedChar ( str , k ) :
expand = ""
freq = 0
i = 0
while ( i < len ( str ) ) :
temp = ""
freq = 0
while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( 'a' ) and ord ( str [ i ] ) <= ord ( 'z' ) ) :
temp += str [ i ]
i += 1
while ( i ... |
python-cpp-ELEMENTS_TO_BE_ADDED_SO_THAT_ALL_ELEMENTS_OF_A_RANGE_ARE_PRESENT_IN_ARRAY_1 | python-cpp | def countNum ( arr , n ) :
s = dict ( )
count , maxm , minm = 0 , - 10 ** 9 , 10 ** 9
for i in range ( n ) :
s [ arr [ i ] ] = 1
if ( arr [ i ] < minm ) :
minm = arr [ i ]
if ( arr [ i ] > maxm ) :
maxm = arr [ i ]
for i in range ( minm , maxm + 1 ) :
... | int countNum ( int arr [ ] , int n ) {
unordered_set < int > s ;
int count = 0 , maxm = INT_MIN , minm = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm = arr [ i ] ;
}
for ( int i = minm ;
i <= m... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countNum ( arr , n ) :
s = dict ( )
count , maxm , minm = 0 , - 10 ** 9 , 10 ** 9
for i in range ( n ) :
s [ arr [ i ] ] = 1
if ( arr [ i ] < minm ) :
minm = arr [ i ]
if ( arr [ i ] > maxm ) :
maxm = arr [ i ]
for i in range ( minm , maxm + 1 )... |
python-cpp-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY_1 | python-cpp | def countPairs ( arr , n ) :
mp = dict ( )
for i in range ( n ) :
if arr [ i ] in mp.keys ( ) :
mp [ arr [ i ] ] += 1
else :
mp [ arr [ i ] ] = 1
ans = 0
for it in mp :
count = mp [ it ]
ans += ( count * ( count - 1 ) ) // 2
return ans
| int countPairs ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( auto it = mp . begin ( ) ;
it != mp . end ( ) ;
it ++ ) {
int count = it -> second ;
ans += ( count * ( count - 1 ) ) / 2 ;
}
return ans ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countPairs ( arr , n ) :
mp = dict ( )
for i in range ( n ) :
if arr [ i ] in mp.keys ( ) :
mp [ arr [ i ] ] += 1
else :
mp [ arr [ i ] ] = 1
ans = 0
for it in mp :
count = mp [ it ]
ans += ( count * ( count - 1 ) ) // 2
return ans
... |
python-cpp-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY_1 | python-cpp | def maxDiff ( arr , n ) :
result = 0
arr.sort ( )
for i in range ( n - 1 ) :
if ( abs ( arr [ i ] ) != abs ( arr [ i + 1 ] ) ) :
result += abs ( arr [ i ] )
else :
pass
if ( arr [ n - 2 ] != arr [ n - 1 ] ) :
result += abs ( arr [ n - 1 ] )
return resu... | int maxDiff ( int arr [ ] , int n ) {
int result = 0 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ;
return result ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxDiff ( arr , n ) :
result = 0
arr.sort ( )
for i in range ( n - 1 ) :
if ( abs ( arr [ i ] ) != abs ( arr [ i + 1 ] ) ) :
result += abs ( arr [ i ] )
else :
pass
if ( arr [ n - 2 ] != arr [ n - 1 ] ) :
result += abs ( arr [ n - 1 ] )
retu... |
python-cpp-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS | python-cpp | def middleOfThree ( a , b , c ) :
if ( ( a < b and b < c ) or ( c < b and b < a ) ) :
return b
if ( ( b < a and a < c ) or ( c < a and a < b ) ) :
return a
else :
return c
| int middleOfThree ( int a , int b , int c ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def middleOfThree ( a , b , c ) :
if ( ( a < b and b < c ) or ( c < b and b < a ) ) :
return b
if ( ( b < a and a < c ) or ( c < a and a < b ) ) :
return a
else :
return c
</Code> |
python-cpp-COUNT_ARRAYS_CONSECUTIVE_ELEMENT_DIFFERENT_VALUES | python-cpp | def countarray ( n , k , x ) :
dp = list ( )
dp.append ( 0 )
dp.append ( 1 )
i = 2
while i < n :
dp.append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] )
i = i + 1
return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
| int countarray ( int n , int k , int x ) {
int dp [ MAXN ] = {
0 }
;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countarray ( n , k , x ) :
dp = list ( )
dp.append ( 0 )
dp.append ( 1 )
i = 2
while i < n :
dp.append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] )
i = i + 1
return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
</Code> |
python-cpp-NEWMAN_SHANKS_WILLIAMS_PRIME | python-cpp | def nswp ( n ) :
if n == 0 or n == 1 :
return 1
return 2 * nswp ( n - 1 ) + nswp ( n - 2 )
| int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def nswp ( n ) :
if n == 0 or n == 1 :
return 1
return 2 * nswp ( n - 1 ) + nswp ( n - 2 )
</Code> |
python-cpp-DYNAMIC_PROGRAMMING_SET_1 | python-cpp | def fib ( n , lookup ) :
if n == 0 or n == 1 :
lookup [ n ] = n
if lookup [ n ] is None :
lookup [ n ] = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup )
return lookup [ n ]
| int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def fib ( n , lookup ) :
if n == 0 or n == 1 :
lookup [ n ] = n
if lookup [ n ] is None :
lookup [ n ] = fib ( n - 1 , lookup ) + fib ( n - 2 , lookup )
return lookup [ n ]
</Code> |
python-cpp-COUNT_NUMBERS_CAN_CONSTRUCTED_USING_TWO_NUMBERS | python-cpp | def countNums ( n , x , y ) :
arr = [ False for i in range ( n + 2 ) ]
if ( x <= n ) :
arr [ x ] = True
if ( y <= n ) :
arr [ y ] = True
result = 0
for i in range ( min ( x , y ) , n + 1 ) :
if ( arr [ i ] ) :
if ( i + x <= n ) :
arr [ i + x ] = Tr... | int countNums ( int n , int x , int y ) {
vector < bool > arr ( n + 1 , false ) ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
if ( i + y <= n )... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countNums ( n , x , y ) :
arr = [ False for i in range ( n + 2 ) ]
if ( x <= n ) :
arr [ x ] = True
if ( y <= n ) :
arr [ y ] = True
result = 0
for i in range ( min ( x , y ) , n + 1 ) :
if ( arr [ i ] ) :
if ( i + x <= n ) :
arr [ i + x... |
python-cpp-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING | python-cpp | def countPS ( str ) :
N = len ( str )
cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ]
for i in range ( N ) :
cps [ i ] [ i ] = 1
for L in range ( 2 , N + 1 ) :
for i in range ( N ) :
k = L + i - 1
if ( k < N ) :
if ( str [ i ] == ... | int countPS ( string str ) {
int N = str . length ( ) ;
int cps [ N + 1 ] [ N + 1 ] ;
memset ( cps , 0 , sizeof ( cps ) ) ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
if ( str... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countPS ( str ) :
N = len ( str )
cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ]
for i in range ( N ) :
cps [ i ] [ i ] = 1
for L in range ( 2 , N + 1 ) :
for i in range ( N ) :
k = L + i - 1
if ( k < N ) :
if ( str [ ... |
python-cpp-CONVERT_DECIMAL_FRACTION_BINARY_NUMBER | python-cpp | def decimalToBinary ( num , k_prec ) :
binary = ""
Integral = int ( num )
fractional = num - Integral
while ( Integral ) :
rem = Integral % 2
binary += str ( rem )
Integral //= 2
binary = binary [ : : - 1 ]
binary += '.'
while ( k_prec ) :
fractional *= 2
... | string decimalToBinary ( double num , int k_prec ) {
string binary = "" ;
int Integral = num ;
double fractional = num - Integral ;
while ( Integral ) {
int rem = Integral % 2 ;
binary . push_back ( rem + '0' ) ;
Integral /= 2 ;
}
reverse ( binary . begin ( ) , binary . end ( ) ) ;
binary . pu... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def decimalToBinary ( num , k_prec ) :
binary = ""
Integral = int ( num )
fractional = num - Integral
while ( Integral ) :
rem = Integral % 2
binary += str ( rem )
Integral //= 2
binary = binary [ : : - 1 ]
binary += '.'
while ( k_prec ) :
fractional *=... |
python-cpp-DYNAMIC_PROGRAMMING_SET_37_BOOLEAN_PARENTHESIZATION_PROBLEM | python-cpp | def countParenth ( symb , oper , n ) :
F = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
for i in range ( n ) :
if symb [ i ] == 'F' :
F [ i ] [ i ] = 1
else :
F [ i ] [ i ] = 0
if s... | int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ n ] [ n ] , T [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == 'F' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == 'T' ) ? 1 : 0 ;
}
for ( int gap = 1 ;
gap < n ;
++ gap ) {
for ( int i = 0 , j =... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countParenth ( symb , oper , n ) :
F = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( n + 1 ) ]
for i in range ( n ) :
if symb [ i ] == 'F' :
F [ i ] [ i ] = 1
else :
F [ i ] [ i ] = 0
... |
python-cpp-COUNT_SET_BITS_IN_AN_INTEGER_1 | python-cpp | def countSetBits ( n ) :
if ( n == 0 ) :
return 0
else :
return ( n & 1 ) + countSetBits ( n >> 1 )
| int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSetBits ( n ) :
if ( n == 0 ) :
return 0
else :
return ( n & 1 ) + countSetBits ( n >> 1 )
</Code> |
python-cpp-LONGEST_SPAN_SUM_TWO_BINARY_ARRAYS | python-cpp | def longestCommonSum ( arr1 , arr2 , n ) :
maxLen = 0
for i in range ( 0 , n ) :
sum1 = 0
sum2 = 0
for j in range ( i , n ) :
sum1 += arr1 [ j ]
sum2 += arr2 [ j ]
if ( sum1 == sum2 ) :
len = j - i + 1
if ( len > maxLen ... | int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def longestCommonSum ( arr1 , arr2 , n ) :
maxLen = 0
for i in range ( 0 , n ) :
sum1 = 0
sum2 = 0
for j in range ( i , n ) :
sum1 += arr1 [ j ]
sum2 += arr2 [ j ]
if ( sum1 == sum2 ) :
len = j - i + 1
if ( len > ... |
python-cpp-MAXIMUM_TRIPLET_SUM_ARRAY | python-cpp | def maxTripletSum ( arr , n ) :
sm = - 1000000
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
for k in range ( j + 1 , n ) :
if ( sm < ( arr [ i ] + arr [ j ] + arr [ k ] ) ) :
sm = arr [ i ] + arr [ j ] + arr [ k ]
return sm
| int maxTripletSum ( int arr [ ] , int n ) {
int sum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxTripletSum ( arr , n ) :
sm = - 1000000
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
for k in range ( j + 1 , n ) :
if ( sm < ( arr [ i ] + arr [ j ] + arr [ k ] ) ) :
sm = arr [ i ] + arr [ j ] + arr [ k ]
return sm
</Code> |
python-cpp-EVALUATE_A_BOOLEAN_EXPRESSION_REPRESENTED_AS_STRING | python-cpp | def evaluateBoolExpr ( s ) :
n = len ( s )
for i in range ( 0 , n - 2 , 2 ) :
if ( s [ i + 1 ] == "A" ) :
if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) :
s [ i + 2 ] = "0"
else :
s [ i + 2 ] = "1"
elif ( s [ i + 1 ] == "B" ) :
if ... | int evaluateBoolExpr ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( s [ i + 1 ] == 'A' ) {
if ( s [ i + 2 ] == '0' || s [ i ] == '0' ) s [ i + 2 ] = '0' ;
else s [ i + 2 ] = '1' ;
}
else if ( s [ i + 1 ] == 'B' ) {
if ( s [ i + 2 ] == '1' || s [ i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def evaluateBoolExpr ( s ) :
n = len ( s )
for i in range ( 0 , n - 2 , 2 ) :
if ( s [ i + 1 ] == "A" ) :
if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) :
s [ i + 2 ] = "0"
else :
s [ i + 2 ] = "1"
elif ( s [ i + 1 ] == "B" ) :
... |
python-cpp-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN_2 | python-cpp | def findNth ( n ) :
nthElement = 19 + ( n - 1 ) * 9
outliersCount = int ( math.log10 ( nthElement ) ) - 1
nthElement += 9 * outliersCount
return nthElement
| int findNth ( int n ) {
int nthElement = 19 + ( n - 1 ) * 9 ;
int outliersCount = ( int ) log10 ( nthElement ) - 1 ;
nthElement += 9 * outliersCount ;
return nthElement ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findNth ( n ) :
nthElement = 19 + ( n - 1 ) * 9
outliersCount = int ( math.log10 ( nthElement ) ) - 1
nthElement += 9 * outliersCount
return nthElement
</Code> |
python-cpp-COUNT_ARITHMETIC_PROGRESSION_SUBSEQUENCES_ARRAY | python-cpp | def numofAP ( a , n ) :
minarr = + 2147483647
maxarr = - 2147483648
for i in range ( n ) :
minarr = min ( minarr , a [ i ] )
maxarr = max ( maxarr , a [ i ] )
dp = [ 0 for i in range ( n + 1 ) ]
ans = n + 1
for d in range ( ( minarr - maxarr ) , ( maxarr - minarr ) + 1 ) :
... | int numofAP ( int a [ ] , int n ) {
int minarr = INT_MAX , maxarr = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
minarr = min ( minarr , a [ i ] ) ;
maxarr = max ( maxarr , a [ i ] ) ;
}
int dp [ n ] , sum [ MAX ] ;
int ans = n + 1 ;
for ( int d = ( minarr - maxarr ) ;
d <= ( maxarr - minarr )... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numofAP ( a , n ) :
minarr = + 2147483647
maxarr = - 2147483648
for i in range ( n ) :
minarr = min ( minarr , a [ i ] )
maxarr = max ( maxarr , a [ i ] )
dp = [ 0 for i in range ( n + 1 ) ]
ans = n + 1
for d in range ( ( minarr - maxarr ) , ( maxarr - minarr ) + 1 ) :... |
python-cpp-FIND_NUMBER_OF_SOLUTIONS_OF_A_LINEAR_EQUATION_OF_N_VARIABLES | python-cpp | def countSol ( coeff , start , end , rhs ) :
if ( rhs == 0 ) :
return 1
result = 0
for i in range ( start , end + 1 ) :
if ( coeff [ i ] <= rhs ) :
result += countSol ( coeff , i , end , rhs - coeff [ i ] )
return result
| int countSol ( int coeff [ ] , int start , int end , int rhs ) {
if ( rhs == 0 ) return 1 ;
int result = 0 ;
for ( int i = start ;
i <= end ;
i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ;
return result ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSol ( coeff , start , end , rhs ) :
if ( rhs == 0 ) :
return 1
result = 0
for i in range ( start , end + 1 ) :
if ( coeff [ i ] <= rhs ) :
result += countSol ( coeff , i , end , rhs - coeff [ i ] )
return result
</Code> |
python-cpp-MINIMUM_NUMBER_SWAPS_REQUIRED_SORT_ARRAY | python-cpp | def minSwaps ( arr ) :
n = len ( arr )
arrpos = [ * enumerate ( arr ) ]
arrpos.sort ( key = lambda it : it [ 1 ] )
vis = { k : False for k in range ( n ) }
ans = 0
for i in range ( n ) :
if vis [ i ] or arrpos [ i ] [ 0 ] == i :
continue
cycle_size = 0
j = i
... | int minSwaps ( int arr [ ] , int n ) {
pair < int , int > arrPos [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arrPos [ i ] . first = arr [ i ] ;
arrPos [ i ] . second = i ;
}
sort ( arrPos , arrPos + n ) ;
vector < bool > vis ( n , false ) ;
int ans = 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 minSwaps ( arr ) :
n = len ( arr )
arrpos = [ * enumerate ( arr ) ]
arrpos.sort ( key = lambda it : it [ 1 ] )
vis = { k : False for k in range ( n ) }
ans = 0
for i in range ( n ) :
if vis [ i ] or arrpos [ i ] [ 0 ] == i :
continue
cycle_size = 0
... |
python-cpp-CIRCULAR_MATRIX_CONSTRUCT_A_MATRIX_WITH_NUMBERS_1_TO_MN_IN_SPIRAL_WAY | python-cpp | def spiralFill ( m , n , a ) :
val = 1
k , l = 0 , 0
while ( k < m and l < n ) :
for i in range ( l , n ) :
a [ k ] [ i ] = val
val += 1
k += 1
for i in range ( k , m ) :
a [ i ] [ n - 1 ] = val
val += 1
n -= 1
if ( k < ... | void spiralFill ( int m , int n , int a [ ] [ MAX ] ) {
int val = 1 ;
int k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( int i = l ;
i < n ;
++ i ) a [ k ] [ i ] = val ++ ;
k ++ ;
for ( int i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = val ++ ;
n -- ;
if ( k < m ) {
for ( ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def spiralFill ( m , n , a ) :
val = 1
k , l = 0 , 0
while ( k < m and l < n ) :
for i in range ( l , n ) :
a [ k ] [ i ] = val
val += 1
k += 1
for i in range ( k , m ) :
a [ i ] [ n - 1 ] = val
val += 1
n -= 1
if... |
python-cpp-GIVEN_TWO_SORTED_ARRAYS_NUMBER_X_FIND_PAIR_WHOSE_SUM_CLOSEST_X | python-cpp | def printClosest ( ar1 , ar2 , m , n , x ) :
diff = sys.maxsize
l = 0
r = n - 1
while ( l < m and r >= 0 ) :
if abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff :
res_l = l
res_r = r
diff = abs ( ar1 [ l ] + ar2 [ r ] - x )
if ar1 [ l ] + ar2 [ r ] > x :
... | void printClosest ( int ar1 [ ] , int ar2 [ ] , int m , int n , int x ) {
int diff = INT_MAX ;
int res_l , res_r ;
int l = 0 , r = n - 1 ;
while ( l < m && r >= 0 ) {
if ( abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( ar1 [ l ] + ar2 [ r ] - x ) ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printClosest ( ar1 , ar2 , m , n , x ) :
diff = sys.maxsize
l = 0
r = n - 1
while ( l < m and r >= 0 ) :
if abs ( ar1 [ l ] + ar2 [ r ] - x ) < diff :
res_l = l
res_r = r
diff = abs ( ar1 [ l ] + ar2 [ r ] - x )
if ar1 [ l ] + ar2 [ r ] > x ... |
python-cpp-NUMBER_VISIBLE_BOXES_PUTTING_ONE_INSIDE_ANOTHER | python-cpp | def minimumBox ( arr , n ) :
q = collections.deque ( [ ] )
arr.sort ( )
q.append ( arr [ 0 ] )
for i in range ( 1 , n ) :
now = q [ 0 ]
if ( arr [ i ] >= 2 * now ) :
q.popleft ( )
q.append ( arr [ i ] )
return len ( q )
| int minimumBox ( int arr [ ] , int n ) {
queue < int > q ;
sort ( arr , arr + n ) ;
q . push ( arr [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int now = q . front ( ) ;
if ( arr [ i ] >= 2 * now ) q . pop ( ) ;
q . push ( arr [ i ] ) ;
}
return q . size ( ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minimumBox ( arr , n ) :
q = collections.deque ( [ ] )
arr.sort ( )
q.append ( arr [ 0 ] )
for i in range ( 1 , n ) :
now = q [ 0 ]
if ( arr [ i ] >= 2 * now ) :
q.popleft ( )
q.append ( arr [ i ] )
return len ( q )
</Code> |
python-cpp-INTEGER_POSITIVE_VALUE_POSITIVE_NEGATIVE_VALUE_ARRAY | python-cpp | def findInteger ( arr , n ) :
hash = dict ( )
maximum = 0
for i in arr :
if ( i < 0 ) :
if abs ( i ) not in hash.keys ( ) :
hash [ abs ( i ) ] = - 1
else :
hash [ abs ( i ) ] -= 1
else :
hash [ i ] = hash.get ( i , 0 ) + 1
... | int findInteger ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
int maximum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] < 0 ) hash [ abs ( arr [ i ] ) ] -= 1 ;
else hash [ arr [ i ] ] += 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) if ( hash [ arr [ i ] ] != 0 ) return arr [... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findInteger ( arr , n ) :
hash = dict ( )
maximum = 0
for i in arr :
if ( i < 0 ) :
if abs ( i ) not in hash.keys ( ) :
hash [ abs ( i ) ] = - 1
else :
hash [ abs ( i ) ] -= 1
else :
hash [ i ] = hash.get ( i , 0 ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.