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-MINIMUM_OPERATION_MAKE_ELEMENTS_EQUAL_ARRAY | python-cpp | def minOperation ( arr , n ) :
Hash = defaultdict ( lambda : 0 )
for i in range ( 0 , n ) :
Hash [ arr [ i ] ] += 1
max_count = 0
for i in Hash :
if max_count < Hash [ i ] :
max_count = Hash [ i ]
return n - max_count
| int minOperation ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 ;
for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ;
return ( n - max_count ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minOperation ( arr , n ) :
Hash = defaultdict ( lambda : 0 )
for i in range ( 0 , n ) :
Hash [ arr [ i ] ] += 1
max_count = 0
for i in Hash :
if max_count < Hash [ i ] :
max_count = Hash [ i ]
return n - max_count
</Code> |
python-cpp-FIND_K_PAIRS_SMALLEST_SUMS_TWO_ARRAYS | python-cpp | def kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) :
if ( k > n1 * n2 ) :
print ( "k pairs don't exist" )
return
index2 = [ 0 for i in range ( n1 ) ]
while ( k > 0 ) :
min_sum = sys.maxsize
min_index = 0
for i1 in range ( 0 , n1 , 1 ) :
if ( index2 [ i1 ] < n... | void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) {
if ( k > n1 * n2 ) {
cout << "k pairs don't exist" ;
return ;
}
int index2 [ n1 ] ;
memset ( index2 , 0 , sizeof ( index2 ) ) ;
while ( k > 0 ) {
int min_sum = INT_MAX ;
int min_index = 0 ;
for ( int i1 = 0 ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) :
if ( k > n1 * n2 ) :
print ( "k pairs don't exist" )
return
index2 = [ 0 for i in range ( n1 ) ]
while ( k > 0 ) :
min_sum = sys.maxsize
min_index = 0
for i1 in range ( 0 , n1 , 1 ) :
if ( index2 [ i... |
python-cpp-PRINT_A_GIVEN_MATRIX_IN_SPIRAL_FORM | python-cpp | def spiralPrint ( m , n , a ) :
k = 0 l = 0
while ( k < m and l < n ) :
for i in range ( l , n ) :
print ( a [ k ] [ i ] , end = " " )
k += 1
for i in range ( k , m ) :
print ( a [ i ] [ n - 1 ] , end = " " )
n -= 1
if ( k < m ) :
for i... | void spiralPrint ( int m , int n , int a [ R ] [ C ] ) {
int i , k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) {
cout << a [ k ] [ i ] << " " ;
}
k ++ ;
for ( i = k ;
i < m ;
++ i ) {
cout << a [ i ] [ n - 1 ] << " " ;
}
n -- ;
if ( k... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def spiralPrint ( m , n , a ) :
k = 0 l = 0
while ( k < m and l < n ) :
for i in range ( l , n ) :
print ( a [ k ] [ i ] , end = " " )
k += 1
for i in range ( k , m ) :
print ( a [ i ] [ n - 1 ] , end = " " )
n -= 1
if ( k < m ) :
... |
python-cpp-CALCULATE_SUM_OF_ALL_NUMBERS_PRESENT_IN_A_STRING | python-cpp | def findSum ( str1 ) :
temp = ""
Sum = 0
for ch in str1 :
if ( ch.isdigit ( ) ) :
temp += ch
else :
Sum += int ( temp )
temp = "0"
return Sum + int ( temp )
| int findSum ( string str ) {
string temp = "" ;
int sum = 0 ;
for ( char ch : str ) {
if ( isdigit ( ch ) ) temp += ch ;
else {
sum += atoi ( temp . c_str ( ) ) ;
temp = "" ;
}
}
return sum + atoi ( temp . c_str ( ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( str1 ) :
temp = ""
Sum = 0
for ch in str1 :
if ( ch.isdigit ( ) ) :
temp += ch
else :
Sum += int ( temp )
temp = "0"
return Sum + int ( temp )
</Code> |
python-cpp-NTH_PENTAGONAL_NUMBER | python-cpp | def pentagonalNum ( n ) :
return ( 3 * n * n - n ) / 2
| int pentagonalNum ( int n ) {
return ( 3 * n * n - n ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def pentagonalNum ( n ) :
return ( 3 * n * n - n ) / 2
</Code> |
python-cpp-TOTAL_NUMBER_OF_NON_DECREASING_NUMBERS_WITH_N_DIGITS_1 | python-cpp | def countNonDecreasing ( n ) :
N = 10
count = 1
for i in range ( 1 , n + 1 ) :
count = int ( count * ( N + i - 1 ) )
count = int ( count / i )
return count
| long long int countNonDecreasing ( int n ) {
int N = 10 ;
long long count = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
count *= ( N + i - 1 ) ;
count /= i ;
}
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countNonDecreasing ( n ) :
N = 10
count = 1
for i in range ( 1 , n + 1 ) :
count = int ( count * ( N + i - 1 ) )
count = int ( count / i )
return count
</Code> |
python-cpp-FREQUENT_ELEMENT_ARRAY_1 | python-cpp | def mostFrequent ( arr , n ) :
Hash = dict ( )
for i in range ( n ) :
if arr [ i ] in Hash.keys ( ) :
Hash [ arr [ i ] ] += 1
else :
Hash [ arr [ i ] ] = 1
max_count = 0
res = - 1
for i in Hash :
if ( max_count < Hash [ i ] ) :
res = i
... | int mostFrequent ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 , res = - 1 ;
for ( auto i : hash ) {
if ( max_count < i . second ) {
res = i . first ;
max_count = i . second ;
}
}
return res ;
}... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def mostFrequent ( arr , n ) :
Hash = dict ( )
for i in range ( n ) :
if arr [ i ] in Hash.keys ( ) :
Hash [ arr [ i ] ] += 1
else :
Hash [ arr [ i ] ] = 1
max_count = 0
res = - 1
for i in Hash :
if ( max_count < Hash [ i ] ) :
res =... |
python-cpp-K_TH_ELEMENT_TWO_SORTED_ARRAYS | python-cpp | def kth ( arr1 , arr2 , m , n , k ) :
sorted1 = [ 0 ] * ( m + n )
i = 0
j = 0
d = 0
while ( i < m and j < n ) :
if ( arr1 [ i ] < arr2 [ j ] ) :
sorted1 [ d ] = arr1 [ i ]
i += 1
else :
sorted1 [ d ] = arr2 [ j ]
j += 1
d += 1
... | int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) {
int sorted1 [ m + n ] ;
int i = 0 , j = 0 , d = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ;
else sorted1 [ d ++ ] = arr2 [ j ++ ] ;
}
while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def kth ( arr1 , arr2 , m , n , k ) :
sorted1 = [ 0 ] * ( m + n )
i = 0
j = 0
d = 0
while ( i < m and j < n ) :
if ( arr1 [ i ] < arr2 [ j ] ) :
sorted1 [ d ] = arr1 [ i ]
i += 1
else :
sorted1 [ d ] = arr2 [ j ]
j += 1
d... |
python-cpp-DYNAMIC_PROGRAMMING_SET_9_BINOMIAL_COEFFICIENT | python-cpp | def binomialCoeff ( n , k ) :
if k == 0 or k == n :
return 1
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k )
| int binomialCoeff ( int n , int k ) {
if ( k == 0 || k == n ) return 1 ;
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def binomialCoeff ( n , k ) :
if k == 0 or k == n :
return 1
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k )
</Code> |
python-cpp-STRING_CONTAINING_FIRST_LETTER_EVERY_WORD_GIVEN_STRING_SPACES | python-cpp | def firstLetterWord ( str ) :
result = ""
v = True
for i in range ( len ( str ) ) :
if ( str [ i ] == ' ' ) :
v = True
elif ( str [ i ] != ' ' and v == True ) :
result += ( str [ i ] )
v = False
return result
| string firstLetterWord ( string str ) {
string result = "" ;
bool v = true ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == ' ' ) v = true ;
else if ( str [ i ] != ' ' && v == true ) {
result . push_back ( str [ i ] ) ;
v = false ;
}
}
return result ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def firstLetterWord ( str ) :
result = ""
v = True
for i in range ( len ( str ) ) :
if ( str [ i ] == ' ' ) :
v = True
elif ( str [ i ] != ' ' and v == True ) :
result += ( str [ i ] )
v = False
return result
</Code> |
python-cpp-REARRANGE_ARRAY_SUCH_THAT_EVEN_POSITIONED_ARE_GREATER_THAN_ODD | python-cpp | def assign ( a , n ) :
a.sort ( )
ans = [ 0 ] * n
p = 0
q = n - 1
for i in range ( n ) :
if ( i + 1 ) % 2 == 0 :
ans [ i ] = a [ q ]
q = q - 1
else :
ans [ i ] = a [ p ]
p = p + 1
for i in range ( n ) :
print ( ans [ i ] , e... | void assign ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int ans [ n ] ;
int p = 0 , q = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ;
else ans [ i ] = a [ p ++ ] ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << ans [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def assign ( a , n ) :
a.sort ( )
ans = [ 0 ] * n
p = 0
q = n - 1
for i in range ( n ) :
if ( i + 1 ) % 2 == 0 :
ans [ i ] = a [ q ]
q = q - 1
else :
ans [ i ] = a [ p ]
p = p + 1
for i in range ( n ) :
print ( ans [ ... |
python-cpp-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY | python-cpp | def printDistinct ( arr , n ) :
for i in range ( 0 , n ) :
d = 0
for j in range ( 0 , i ) :
if ( arr [ i ] == arr [ j ] ) :
d = 1
break
if ( d == 0 ) :
print ( arr [ i ] )
| void printDistinct ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] == arr [ j ] ) break ;
if ( i == j ) 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 ) :
for i in range ( 0 , n ) :
d = 0
for j in range ( 0 , i ) :
if ( arr [ i ] == arr [ j ] ) :
d = 1
break
if ( d == 0 ) :
print ( arr [ i ] )
</Code> |
python-cpp-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2 | python-cpp | def countSolutions ( n ) :
res = 0
x = 0
while ( x * x < n ) :
y = 0
while ( x * x + y * y < n ) :
res = res + 1
y = y + 1
x = x + 1
return res
| int countSolutions ( int n ) {
int res = 0 ;
for ( int x = 0 ;
x * x < n ;
x ++ ) for ( int y = 0 ;
x * x + y * y < n ;
y ++ ) res ++ ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSolutions ( n ) :
res = 0
x = 0
while ( x * x < n ) :
y = 0
while ( x * x + y * y < n ) :
res = res + 1
y = y + 1
x = x + 1
return res
</Code> |
python-cpp-FIND_RELATIVE_COMPLEMENT_TWO_SORTED_ARRAYS | python-cpp | def relativeComplement ( arr1 , arr2 , n , m ) :
i = 0
j = 0
while ( i < n and j < m ) :
if ( arr1 [ i ] < arr2 [ j ] ) :
print ( arr1 [ i ] , "" , end = "" )
i += 1
elif ( arr1 [ i ] > arr2 [ j ] ) :
j += 1
elif ( arr1 [ i ] == arr2 [ j ] ) :
... | void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
int i = 0 , j = 0 ;
while ( i < n && j < m ) {
if ( arr1 [ i ] < arr2 [ j ] ) {
cout << arr1 [ i ] << " " ;
i ++ ;
}
else if ( arr1 [ i ] > arr2 [ j ] ) {
j ++ ;
}
else if ( arr1 [ i ] == arr2 [ j ] ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def relativeComplement ( arr1 , arr2 , n , m ) :
i = 0
j = 0
while ( i < n and j < m ) :
if ( arr1 [ i ] < arr2 [ j ] ) :
print ( arr1 [ i ] , "" , end = "" )
i += 1
elif ( arr1 [ i ] > arr2 [ j ] ) :
j += 1
elif ( arr1 [ i ] == arr2 [ j ] )... |
python-cpp-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D_1 | python-cpp | def findFourElements ( arr , n ) :
mp = dict ( )
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
mp [ arr [ i ] + arr [ j ] ] = ( i , j )
d = - 10 ** 9
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
abs_diff = abs ( arr [ i ] - arr [ j ] )
... | int findFourElements ( int arr [ ] , int n ) {
unordered_map < int , pair < int , int >> mp ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) mp [ arr [ i ] + arr [ j ] ] = {
i , j }
;
int d = INT_MIN ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findFourElements ( arr , n ) :
mp = dict ( )
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
mp [ arr [ i ] + arr [ j ] ] = ( i , j )
d = - 10 ** 9
for i in range ( n - 1 ) :
for j in range ( i + 1 , n ) :
abs_diff = abs ( arr [ i ] - arr [ j ... |
python-cpp-COUNT_TRAILING_ZEROES_FACTORIAL_NUMBER | python-cpp | def findTrailingZeros ( n ) :
count = 0
i = 5
while ( n / i >= 1 ) :
count += int ( n / i )
i *= 5
return int ( count )
| int findTrailingZeros ( int n ) {
int count = 0 ;
for ( int i = 5 ;
n / i >= 1 ;
i *= 5 ) count += n / i ;
return count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findTrailingZeros ( n ) :
count = 0
i = 5
while ( n / i >= 1 ) :
count += int ( n / i )
i *= 5
return int ( count )
</Code> |
python-cpp-PRIMALITY_TEST_SET_5USING_LUCAS_LEHMER_SERIES | python-cpp | def isPrime ( p ) :
checkNumber = 2 ** p - 1
nextval = 4 % checkNumber
for i in range ( 1 , p - 1 ) :
nextval = ( nextval * nextval - 2 ) % checkNumber
if ( nextval == 0 ) : return True
else : return False
| bool isPrime ( int p ) {
long long checkNumber = pow ( 2 , p ) - 1 ;
long long nextval = 4 % checkNumber ;
for ( int i = 1 ;
i < p - 1 ;
i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ;
return ( nextval == 0 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPrime ( p ) :
checkNumber = 2 ** p - 1
nextval = 4 % checkNumber
for i in range ( 1 , p - 1 ) :
nextval = ( nextval * nextval - 2 ) % checkNumber
if ( nextval == 0 ) : return True
else : return False
</Code> |
python-cpp-COUNT_OF_SUB_STRINGS_THAT_DO_NOT_CONTAIN_ALL_THE_CHARACTERS_FROM_THE_SET_A_B_C_AT_THE_SAME_TIME | python-cpp | def CountSubString ( Str , n ) :
ans = ( n * ( n + 1 ) ) // 2
a_index = 0
b_index = 0
c_index = 0
for i in range ( n ) :
if ( Str [ i ] == 'a' ) :
a_index = i + 1
ans -= min ( b_index , c_index )
elif ( Str [ i ] == 'b' ) :
b_index = i + 1
... | int CountSubstring ( char str [ ] , int n ) {
int ans = ( n * ( n + 1 ) ) / 2 ;
int a_index = 0 ;
int b_index = 0 ;
int c_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( str [ i ] == 'a' ) {
a_index = i + 1 ;
ans -= min ( b_index , c_index ) ;
}
else if ( str [ i ] == 'b' ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def CountSubString ( Str , n ) :
ans = ( n * ( n + 1 ) ) // 2
a_index = 0
b_index = 0
c_index = 0
for i in range ( n ) :
if ( Str [ i ] == 'a' ) :
a_index = i + 1
ans -= min ( b_index , c_index )
elif ( Str [ i ] == 'b' ) :
b_index = i + 1
... |
python-cpp-CHECK_GIVEN_STRING_ROTATION_PALINDROME | python-cpp | def isPalindrome ( string ) :
l = 0
h = len ( string ) - 1
while h > l :
l += 1
h -= 1
if string [ l - 1 ] != string [ h + 1 ] :
return False
return True
| bool isPalindrome ( string str ) {
int l = 0 ;
int h = str . length ( ) - 1 ;
while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPalindrome ( string ) :
l = 0
h = len ( string ) - 1
while h > l :
l += 1
h -= 1
if string [ l - 1 ] != string [ h + 1 ] :
return False
return True
</Code> |
python-cpp-MAXIMUM_NUMBER_CHOCOLATES_DISTRIBUTED_EQUALLY_AMONG_K_STUDENTS | python-cpp | def maxNumOfChocolates ( arr , n , k ) :
um , curr_rem , maxSum = { } , 0 , 0
sm = [ 0 ] * n
sm [ 0 ] = arr [ 0 ]
for i in range ( 1 , n ) :
sm [ i ] = sm [ i - 1 ] + arr [ i ]
for i in range ( n ) :
curr_rem = sm [ i ] % k
if ( not curr_rem and maxSum < sm [ i ] ) :
... | int maxNumOfChocolates ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int sum [ n ] , curr_rem ;
int maxSum = 0 ;
sum [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_rem = sum [ i ] % k ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxNumOfChocolates ( arr , n , k ) :
um , curr_rem , maxSum = { } , 0 , 0
sm = [ 0 ] * n
sm [ 0 ] = arr [ 0 ]
for i in range ( 1 , n ) :
sm [ i ] = sm [ i - 1 ] + arr [ i ]
for i in range ( n ) :
curr_rem = sm [ i ] % k
if ( not curr_rem and maxSum < sm [ i ] ) :
... |
python-cpp-DECODE_STRING_RECURSIVELY_ENCODED_COUNT_FOLLOWED_SUBSTRING | python-cpp | def decode ( Str ) :
integerstack = [ ]
stringstack = [ ]
temp = ""
result = ""
for i in range ( len ( Str ) ) :
count = 0
if ( Str [ i ] >= '0' and Str [ i ] <= '9' ) :
while ( Str [ i ] >= '0' and Str [ i ] <= '9' ) :
count = count * 10 + ord ( Str [ i ]... | string decode ( string str ) {
stack < int > integerstack ;
stack < char > stringstack ;
string temp = "" , result = "" ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
int count = 0 ;
if ( str [ i ] >= '0' && str [ i ] <= '9' ) {
while ( str [ i ] >= '0' && str [ i ] <= '9' ) {
cou... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def decode ( Str ) :
integerstack = [ ]
stringstack = [ ]
temp = ""
result = ""
for i in range ( len ( Str ) ) :
count = 0
if ( Str [ i ] >= '0' and Str [ i ] <= '9' ) :
while ( Str [ i ] >= '0' and Str [ i ] <= '9' ) :
count = count * 10 + ord ( St... |
python-cpp-MINIMUM_SUM_CHOOSING_MINIMUM_PAIRS_ARRAY | python-cpp | def minSum ( A ) :
min_val = min ( A ) ;
return min_val * ( len ( A ) - 1 )
| int minSum ( int A [ ] , int n ) {
int min_val = * min_element ( A , A + n ) ;
return ( min_val * ( n - 1 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minSum ( A ) :
min_val = min ( A ) ;
return min_val * ( len ( A ) - 1 )
</Code> |
python-cpp-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER_1 | python-cpp | def isPower ( x , y ) :
res1 = math.log ( y ) / math.log ( x )
res2 = math.log ( y ) / math.log ( x )
return 1 if ( res1 == res2 ) else 0
| bool isPower ( int x , int y ) {
int res1 = log ( y ) / log ( x ) ;
double res2 = log ( y ) / log ( x ) ;
return ( res1 == res2 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPower ( x , y ) :
res1 = math.log ( y ) / math.log ( x )
res2 = math.log ( y ) / math.log ( x )
return 1 if ( res1 == res2 ) else 0
</Code> |
python-cpp-COUNT_WORDS_APPEAR_EXACTLY_TWO_TIMES_ARRAY_WORDS | python-cpp | def countWords ( stri , n ) :
m = dict ( )
for i in range ( n ) :
m [ stri [ i ] ] = m.get ( stri [ i ] , 0 ) + 1
res = 0
for i in m.values ( ) :
if i == 2 :
res += 1
return res
| int countWords ( string str [ ] , int n ) {
unordered_map < string , int > m ;
for ( int i = 0 ;
i < n ;
i ++ ) m [ str [ i ] ] += 1 ;
int res = 0 ;
for ( auto it = m . begin ( ) ;
it != m . end ( ) ;
it ++ ) if ( ( it -> second == 2 ) ) res ++ ;
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countWords ( stri , n ) :
m = dict ( )
for i in range ( n ) :
m [ stri [ i ] ] = m.get ( stri [ i ] , 0 ) + 1
res = 0
for i in m.values ( ) :
if i == 2 :
res += 1
return res
</Code> |
python-cpp-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D | python-cpp | def findLargestd ( S , n ) :
found = False
S.sort ( )
for i in range ( n - 1 , - 1 , - 1 ) :
for j in range ( 0 , n ) :
if ( i == j ) :
continue
for k in range ( j + 1 , n ) :
if ( i == k ) :
continue
for l i... | int findLargestd ( int S [ ] , int n ) {
bool found = false ;
sort ( S , S + n ) ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i == j ) continue ;
for ( int k = j + 1 ;
k < n ;
k ++ ) {
if ( i == k ) continue ;
for ( int l... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLargestd ( S , n ) :
found = False
S.sort ( )
for i in range ( n - 1 , - 1 , - 1 ) :
for j in range ( 0 , n ) :
if ( i == j ) :
continue
for k in range ( j + 1 , n ) :
if ( i == k ) :
continue
... |
python-cpp-MAXIMUM_MINIMUM_VALUES_ALGEBRAIC_EXPRESSION | python-cpp | def minMaxValues ( arr , n , m ) :
sum = 0
INF = 1000000000
MAX = 50
for i in range ( 0 , ( n + m ) ) :
sum += arr [ i ]
arr [ i ] += 50
dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( 0 , ( n + m ) ) :
fo... | e9 int minMaxValues ( int arr [ ] , int n , int m ) {
int sum = 0 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
sum += arr [ i ] ;
arr [ i ] += 50 ;
}
bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minMaxValues ( arr , n , m ) :
sum = 0
INF = 1000000000
MAX = 50
for i in range ( 0 , ( n + m ) ) :
sum += arr [ i ]
arr [ i ] += 50
dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( 0 , ( n + m ) ) :
... |
python-cpp-PRINT_POSSIBLE_EDGES_TREE_GIVEN_DIAMETER_HEIGHT_VERTICES | python-cpp | def constructTree ( n , d , h ) :
if d == 1 :
if n == 2 and h == 1 :
print ( "1 2" )
return 0
print ( "-1" )
return 0
if d > 2 * h :
print ( "-1" )
return 0
for i in range ( 1 , h + 1 ) :
print ( i , " " , i + 1 )
if d > h :
... | void constructTree ( int n , int d , int h ) {
if ( d == 1 ) {
if ( n == 2 && h == 1 ) {
cout << "1 2" << endl ;
return ;
}
cout << "-1" << endl ;
return ;
}
if ( d > 2 * h ) {
cout << "-1" << endl ;
return ;
}
for ( int i = 1 ;
i <= h ;
i ++ ) cout << i << " " << i + 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def constructTree ( n , d , h ) :
if d == 1 :
if n == 2 and h == 1 :
print ( "1 2" )
return 0
print ( "-1" )
return 0
if d > 2 * h :
print ( "-1" )
return 0
for i in range ( 1 , h + 1 ) :
print ( i , " " , i + 1 )
if d > h :
... |
python-cpp-NUMBER_RECTANGLES_NM_GRID | python-cpp | def rectCount ( n , m ) :
return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4
| int rectCount ( int n , int m ) {
return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def rectCount ( n , m ) :
return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4
</Code> |
python-cpp-SORTING_USING_TRIVIAL_HASH_FUNCTION | python-cpp | def sortUsingHash ( a , n ) :
Max = max ( a )
Hash = [ 0 ] * ( Max + 1 )
for i in range ( 0 , n ) :
Hash [ a [ i ] ] += 1
for i in range ( 0 , Max + 1 ) :
if Hash [ i ] != 0 :
for j in range ( 0 , Hash [ i ] ) :
print ( i , end = " " )
| void sortUsingHash ( int a [ ] , int n ) {
int max = * std :: max_element ( a , a + n ) ;
int hash [ max + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ a [ i ] ] += 1 ;
for ( int i = 0 ;
i <= max ;
i ++ ) {
if ( hash [ i ] ) {
for ( int j = 0 ;
j < hash... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortUsingHash ( a , n ) :
Max = max ( a )
Hash = [ 0 ] * ( Max + 1 )
for i in range ( 0 , n ) :
Hash [ a [ i ] ] += 1
for i in range ( 0 , Max + 1 ) :
if Hash [ i ] != 0 :
for j in range ( 0 , Hash [ i ] ) :
print ( i , end = " " )
</Code> |
python-cpp-SUBARRAY_NO_PAIR_SUM_DIVISIBLE_K | python-cpp | def subarrayDivisibleByK ( arr , n , k ) :
mp = [ 0 ] * 1000
s = 0 e = 0 maxs = 0 maxe = 0
mp [ arr [ 0 ] % k ] = mp [ arr [ 0 ] % k ] + 1
for i in range ( 1 , n ) :
mod = arr [ i ] % k
while ( mp [ k - mod ] != 0 or ( mod == 0 and mp [ mod ] != 0 ) ) :
mp [ arr [ s ] % k ] =... | void subarrayDivisibleByK ( int arr [ ] , int n , int k ) {
map < int , int > mp ;
int s = 0 , e = 0 , maxs = 0 , maxe = 0 ;
mp [ arr [ 0 ] % k ] ++ ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int mod = arr [ i ] % k ;
while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) {
mp [ arr [ s ] ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def subarrayDivisibleByK ( arr , n , k ) :
mp = [ 0 ] * 1000
s = 0 e = 0 maxs = 0 maxe = 0
mp [ arr [ 0 ] % k ] = mp [ arr [ 0 ] % k ] + 1
for i in range ( 1 , n ) :
mod = arr [ i ] % k
while ( mp [ k - mod ] != 0 or ( mod == 0 and mp [ mod ] != 0 ) ) :
mp [ arr [ s ] ... |
python-cpp-CHECK_DIVISIBILITY_BINARY_STRING_2K | python-cpp | def isDivisible ( str , k ) :
n = len ( str )
c = 0
for i in range ( 0 , k ) :
if ( str [ n - i - 1 ] == '0' ) :
c += 1
return ( c == k )
| bool isDivisible ( char str [ ] , int k ) {
int n = strlen ( str ) ;
int c = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ;
return ( c == k ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isDivisible ( str , k ) :
n = len ( str )
c = 0
for i in range ( 0 , k ) :
if ( str [ n - i - 1 ] == '0' ) :
c += 1
return ( c == k )
</Code> |
python-cpp-NUMBER_DIGITS_REMOVED_MAKE_NUMBER_DIVISIBLE_3 | python-cpp | def divisible ( num ) :
n = len ( num )
sum = 0
for i in range ( n ) :
sum += int ( num [ i ] )
if ( sum % 3 == 0 ) :
return 0
if ( n == 1 ) :
return - 1
for i in range ( n ) :
if ( sum % 3 == int ( num [ i ] ) % 3 ) :
return 1
if ( n == 2 ) :
... | int divisible ( string num ) {
int n = num . length ( ) ;
int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ;
if ( sum % 3 == 0 ) return 0 ;
if ( n == 1 ) return - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ;
if ( n == 2 ) return - 1 ;
retu... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def divisible ( num ) :
n = len ( num )
sum = 0
for i in range ( n ) :
sum += int ( num [ i ] )
if ( sum % 3 == 0 ) :
return 0
if ( n == 1 ) :
return - 1
for i in range ( n ) :
if ( sum % 3 == int ( num [ i ] ) % 3 ) :
return 1
if ( n == 2 )... |
python-cpp-SHORTEST_COMMON_SUPERSEQUENCE | python-cpp | def superSeq ( X , Y , m , n ) :
if ( not m ) : return n
if ( not n ) : return m
if ( X [ m - 1 ] == Y [ n - 1 ] ) :
return 1 + superSeq ( X , Y , m - 1 , n - 1 )
return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) )
| int superSeq ( char * X , char * Y , int m , int n ) {
if ( ! m ) return n ;
if ( ! n ) return m ;
if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ;
return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 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 ) :
if ( not m ) : return n
if ( not n ) : return m
if ( X [ m - 1 ] == Y [ n - 1 ] ) :
return 1 + superSeq ( X , Y , m - 1 , n - 1 )
return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) )
</Code> |
python-cpp-PROGRAM_SWAP_UPPER_DIAGONAL_ELEMENTS_LOWER_DIAGONAL_ELEMENTS_MATRIX | python-cpp | def swapUpperToLower ( arr ) :
n = 4 ;
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
for i in range ( 0 , n ) :
for j in range ( 0 , n ) :
print (... | void swapUpperToLower ( int arr [ n ] [ n ] ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
}
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def swapUpperToLower ( arr ) :
n = 4 ;
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
for i in range ( 0 , n ) :
for j in range ( 0 , n ) :
... |
python-cpp-SUM_K_TH_GROUP_ODD_POSITIVE_NUMBERS | python-cpp | def kthgroupsum ( k ) :
cur = int ( ( k * ( k - 1 ) ) + 1 )
sum = 0
while k :
sum += cur
cur += 2
k = k - 1
return sum
| int kthgroupsum ( int k ) {
int cur = ( k * ( k - 1 ) ) + 1 ;
int sum = 0 ;
while ( k -- ) {
sum += cur ;
cur += 2 ;
}
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def kthgroupsum ( k ) :
cur = int ( ( k * ( k - 1 ) ) + 1 )
sum = 0
while k :
sum += cur
cur += 2
k = k - 1
return sum
</Code> |
python-cpp-CHECK_GIVEN_MATRIX_IS_MAGIC_SQUARE_OR_NOT | python-cpp | def isMagicSquare ( mat ) :
s = 0
for i in range ( 0 , N ) :
s = s + mat [ i ] [ i ]
s2 = 0
for i in range ( 0 , N ) :
s2 = s2 + mat [ i ] [ N - i - 1 ]
if ( s != s2 ) :
return False
for i in range ( 0 , N ) :
rowSum = 0
for j in range ( 0 , N ) :
... | bool isMagicSquare ( int mat [ ] [ N ] ) {
int sum = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) sum = sum + mat [ i ] [ i ] ;
for ( int i = 0 ;
i < N ;
i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ;
if ( sum != sum2 ) return false ;
for ( int i = 0 ;
i < N ;
i ++ ) {
int rowSum = 0 ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isMagicSquare ( mat ) :
s = 0
for i in range ( 0 , N ) :
s = s + mat [ i ] [ i ]
s2 = 0
for i in range ( 0 , N ) :
s2 = s2 + mat [ i ] [ N - i - 1 ]
if ( s != s2 ) :
return False
for i in range ( 0 , N ) :
rowSum = 0
for j in range ( 0 , N ) :
... |
python-cpp-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX_1 | python-cpp | def isScalarMatrix ( mat ) :
for i in range ( 0 , N ) :
for j in range ( 0 , N ) :
if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) :
return False
for i in range ( 0 , N - 1 ) :
if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) :
return False
return True... | bool isScalarMatrix ( int mat [ N ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
for ( int i = 0 ;
i < N - 1 ;
i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isScalarMatrix ( mat ) :
for i in range ( 0 , N ) :
for j in range ( 0 , N ) :
if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) :
return False
for i in range ( 0 , N - 1 ) :
if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) :
return False
retu... |
python-cpp-LONGEST_SUBARRAY_SUM_DIVISIBLE_K | python-cpp | def longSubarrWthSumDivByK ( arr , n , k ) :
um = { i : 0 for i in range ( 8 ) }
mod_arr = [ 0 for i in range ( n ) ]
max = 0
curr_sum = 0
for i in range ( n ) :
curr_sum += arr [ i ]
mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k
for i in range ( n ) :
if ( mod_arr [ i ] =... | int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int mod_arr [ n ] , max = 0 ;
int curr_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_sum += arr [ i ] ;
mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
if... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def longSubarrWthSumDivByK ( arr , n , k ) :
um = { i : 0 for i in range ( 8 ) }
mod_arr = [ 0 for i in range ( n ) ]
max = 0
curr_sum = 0
for i in range ( n ) :
curr_sum += arr [ i ]
mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k
for i in range ( n ) :
if ( mod_arr ... |
python-cpp-REVERSE_A_STRING_USING_RECURSION | python-cpp | def reverse ( string ) :
if len ( string ) == 0 :
return
temp = string [ 0 ]
reverse ( string [ 1 : ] )
print ( temp , end = '' )
| void reverse ( string str ) {
if ( str . size ( ) == 0 ) {
return ;
}
reverse ( str . substr ( 1 ) ) ;
cout << str [ 0 ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def reverse ( string ) :
if len ( string ) == 0 :
return
temp = string [ 0 ]
reverse ( string [ 1 : ] )
print ( temp , end = '' )
</Code> |
python-cpp-BALANCED_EXPRESSIONS_SUCH_THAT_GIVEN_POSITIONS_HAVE_OPENING_BRACKETS | python-cpp | def arrangeBraces ( n , pos , k ) :
h = [ False for i in range ( N ) ]
dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ]
for i in range ( k ) :
h [ pos [ i ] ] = 1
dp [ 0 ] [ 0 ] = 1
for i in range ( 1 , 2 * n + 1 ) :
for j in range ( 2 * n + 1 ) :
if ( h [ i ] ) :... | long long arrangeBraces ( int n , int pos [ ] , int k ) {
bool h [ N ] ;
int dp [ N ] [ N ] ;
memset ( h , 0 , sizeof h ) ;
memset ( dp , 0 , sizeof dp ) ;
for ( int i = 0 ;
i < k ;
i ++ ) h [ pos [ i ] ] = 1 ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i <= 2 * n ;
i ++ ) {
for ( int j = 0 ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def arrangeBraces ( n , pos , k ) :
h = [ False for i in range ( N ) ]
dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ]
for i in range ( k ) :
h [ pos [ i ] ] = 1
dp [ 0 ] [ 0 ] = 1
for i in range ( 1 , 2 * n + 1 ) :
for j in range ( 2 * n + 1 ) :
if ( h [ ... |
python-cpp-DIVISIBILITY_BY_12_FOR_A_LARGE_NUMBER | python-cpp | def isDvisibleBy12 ( num ) :
if ( len ( num ) >= 3 ) :
d1 = int ( num [ len ( num ) - 1 ] )
if ( d1 % 2 != 0 ) :
return False
d2 = int ( num [ len ( num ) - 2 ] )
sum = 0
for i in range ( 0 , len ( num ) ) :
sum += int ( num [ i ] )
return ( su... | bool isDvisibleBy12 ( string num ) {
if ( num . length ( ) >= 3 ) {
int d1 = ( int ) num [ num . length ( ) - 1 ] ;
if ( d1 % 2 != 0 ) return ( 0 ) ;
int d2 = ( int ) num [ num . length ( ) - 2 ] ;
int sum = 0 ;
for ( int i = 0 ;
i < num . length ( ) ;
i ++ ) sum += num [ i ] ;
return ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isDvisibleBy12 ( num ) :
if ( len ( num ) >= 3 ) :
d1 = int ( num [ len ( num ) - 1 ] )
if ( d1 % 2 != 0 ) :
return False
d2 = int ( num [ len ( num ) - 2 ] )
sum = 0
for i in range ( 0 , len ( num ) ) :
sum += int ( num [ i ] )
retu... |
python-cpp-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY | python-cpp | def maxDiff ( arr , n ) :
SubsetSum_1 = 0
SubsetSum_2 = 0
for i in range ( 0 , n ) :
isSingleOccurance = True
for j in range ( i + 1 , n ) :
if ( arr [ i ] == arr [ j ] ) :
isSingleOccurance = False
arr [ i ] = arr [ j ] = 0
break
... | int maxDiff ( int arr [ ] , int n ) {
int SubsetSum_1 = 0 , SubsetSum_2 = 0 ;
for ( int i = 0 ;
i <= n - 1 ;
i ++ ) {
bool isSingleOccurance = true ;
for ( int j = i + 1 ;
j <= n - 1 ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) {
isSingleOccurance = false ;
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 ) :
SubsetSum_1 = 0
SubsetSum_2 = 0
for i in range ( 0 , n ) :
isSingleOccurance = True
for j in range ( i + 1 , n ) :
if ( arr [ i ] == arr [ j ] ) :
isSingleOccurance = False
arr [ i ] = arr [ j ] = 0
... |
python-cpp-LEXICOGRAPHICALLY_SMALLEST_STRING_OBTAINED_CONCATENATING_ARRAY | python-cpp | def lexSmallest ( a , n ) :
a.sort ( reverse = True )
answer = ""
for i in range ( n ) :
answer += a [ i ]
return answer
| string lexSmallest ( string a [ ] , int n ) {
sort ( a , a + n , compare ) ;
string answer = "" ;
for ( int i = 0 ;
i < n ;
i ++ ) answer += a [ i ] ;
return answer ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lexSmallest ( a , n ) :
a.sort ( reverse = True )
answer = ""
for i in range ( n ) :
answer += a [ i ]
return answer
</Code> |
python-cpp-MAXIMUM_WEIGHT_PATH_ENDING_ELEMENT_LAST_ROW_MATRIX | python-cpp | def maxCost ( mat , N ) :
dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ]
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ]
for i in range ( 1 , N ) :
dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ]
for i in range ( 1 , N ) :
for j in range ( 1 , min ( i + 1 , N ) ) :
dp [ i ] ... | int maxCost ( int mat [ ] [ MAX ] , int N ) {
int dp [ N ] [ N ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) for ( int j = 1 ;
j < i + 1 && j < N ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxCost ( mat , N ) :
dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ]
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ]
for i in range ( 1 , N ) :
dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ]
for i in range ( 1 , N ) :
for j in range ( 1 , min ( i + 1 , N ) ) :
dp... |
python-cpp-WAYS_REMOVE_ONE_ELEMENT_BINARY_STRING_XOR_BECOMES_ZERO | python-cpp | def xorZero ( str ) :
one_count = 0
zero_count = 0
n = len ( str )
for i in range ( 0 , n , 1 ) :
if ( str [ i ] == '1' ) :
one_count += 1
else :
zero_count += 1
if ( one_count % 2 == 0 ) :
return zero_count
return one_count
| int xorZero ( string str ) {
int one_count = 0 , zero_count = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( str [ i ] == '1' ) one_count ++ ;
else zero_count ++ ;
if ( one_count % 2 == 0 ) return zero_count ;
return one_count ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def xorZero ( str ) :
one_count = 0
zero_count = 0
n = len ( str )
for i in range ( 0 , n , 1 ) :
if ( str [ i ] == '1' ) :
one_count += 1
else :
zero_count += 1
if ( one_count % 2 == 0 ) :
return zero_count
return one_count
</Code> |
python-cpp-N_TH_ROOT_NUMBER | python-cpp | def nthRoot ( A , N ) :
xPre = random.randint ( 1 , 101 ) % 10
eps = 0.001
delX = 2147483647
xK = 0.0
while ( delX > eps ) :
xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N
delX = abs ( xK - xPre )
xPre = xK
return xK
| double nthRoot ( int A , int N ) {
double xPre = rand ( ) % 10 ;
double eps = 1 e - 3 ;
double delX = INT_MAX ;
double xK ;
while ( delX > eps ) {
xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ;
delX = abs ( xK - xPre ) ;
xPre = xK ;
}
return xK ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def nthRoot ( A , N ) :
xPre = random.randint ( 1 , 101 ) % 10
eps = 0.001
delX = 2147483647
xK = 0.0
while ( delX > eps ) :
xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N
delX = abs ( xK - xPre )
xPre = xK
return xK
</Code> |
python-cpp-RETURN_PREVIOUS_ELEMENT_IN_AN_EXPANDING_MATRIX | python-cpp | def findLeft ( str ) :
n = len ( str ) - 1
while ( n > 0 ) :
if ( str [ n ] == 'd' ) :
str = str [ 0 : n ] + 'c' + str [ n + 1 : ]
break
if ( str [ n ] == 'b' ) :
str = str [ 0 : n ] + 'a' + str [ n + 1 : ]
break
if ( str [ n ] == 'a' ) :
... | string findLeft ( string str ) {
int n = str . length ( ) ;
while ( n -- ) {
if ( str [ n ] == 'd' ) {
str [ n ] = 'c' ;
break ;
}
if ( str [ n ] == 'b' ) {
str [ n ] = 'a' ;
break ;
}
if ( str [ n ] == 'a' ) str [ n ] = 'b' ;
else if ( str [ n ] == 'c' ) str [ n ] = ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findLeft ( str ) :
n = len ( str ) - 1
while ( n > 0 ) :
if ( str [ n ] == 'd' ) :
str = str [ 0 : n ] + 'c' + str [ n + 1 : ]
break
if ( str [ n ] == 'b' ) :
str = str [ 0 : n ] + 'a' + str [ n + 1 : ]
break
if ( str [ n ] == 'a... |
python-cpp-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS | python-cpp | def subset ( ar , n ) :
res = 0
ar.sort ( )
for i in range ( 0 , n ) :
count = 1
for i in range ( n - 1 ) :
if ar [ i ] == ar [ i + 1 ] :
count += 1
else :
break
res = max ( res , count )
return res
| int subset ( int ar [ ] , int n ) {
int res = 0 ;
sort ( ar , ar + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 1 ;
for ( ;
i < n - 1 ;
i ++ ) {
if ( ar [ i ] == ar [ i + 1 ] ) count ++ ;
else break ;
}
res = max ( res , count ) ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def subset ( ar , n ) :
res = 0
ar.sort ( )
for i in range ( 0 , n ) :
count = 1
for i in range ( n - 1 ) :
if ar [ i ] == ar [ i + 1 ] :
count += 1
else :
break
res = max ( res , count )
return res
</Code> |
python-cpp-SMALLEST_SUBARRAY_K_DISTINCT_NUMBERS | python-cpp | def minRange ( arr , n , k ) :
l = 0
r = n
for i in range ( n ) :
s = [ ]
for j in range ( i , n ) :
s.append ( arr [ j ] )
if ( len ( s ) == k ) :
if ( ( j - i ) < ( r - l ) ) :
r = j
l = i
break... | void minRange ( int arr [ ] , int n , int k ) {
int l = 0 , r = n ;
for ( int i = 0 ;
i < n ;
i ++ ) {
unordered_set < int > s ;
int j ;
for ( j = i ;
j < n ;
j ++ ) {
s . insert ( arr [ j ] ) ;
if ( s . size ( ) == k ) {
if ( ( j - i ) < ( r - l ) ) {
r = j ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minRange ( arr , n , k ) :
l = 0
r = n
for i in range ( n ) :
s = [ ]
for j in range ( i , n ) :
s.append ( arr [ j ] )
if ( len ( s ) == k ) :
if ( ( j - i ) < ( r - l ) ) :
r = j
l = i
... |
python-cpp-OVERLAPPING_SUM_TWO_ARRAY | python-cpp | def findSum ( A , B , n ) :
Hash = defaultdict ( lambda : 0 )
for i in range ( 0 , n ) :
Hash [ A [ i ] ] += 1
Hash [ B [ i ] ] += 1
Sum = 0
for x in Hash :
if Hash [ x ] == 1 :
Sum += x
return Sum
| int findSum ( int A [ ] , int B [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] ++ ;
hash [ B [ i ] ] ++ ;
}
int sum = 0 ;
for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ;
return sum ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( A , B , n ) :
Hash = defaultdict ( lambda : 0 )
for i in range ( 0 , n ) :
Hash [ A [ i ] ] += 1
Hash [ B [ i ] ] += 1
Sum = 0
for x in Hash :
if Hash [ x ] == 1 :
Sum += x
return Sum
</Code> |
python-cpp-LONGEST_PALINDROME_SUBSTRING_SET_1 | python-cpp | def printSubStr ( st , low , high ) :
sys.stdout.write ( st [ low : high + 1 ] )
sys.stdout.flush ( )
return ''
| void printSubStr ( string str , int low , int high ) {
for ( int i = low ;
i <= high ;
++ i ) cout << str [ i ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printSubStr ( st , low , high ) :
sys.stdout.write ( st [ low : high + 1 ] )
sys.stdout.flush ( )
return ''
</Code> |
python-cpp-FIND_SUM_EVEN_INDEX_BINOMIAL_COEFFICIENTS | python-cpp | def evenSum ( n ) :
C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ]
for i in range ( 0 , n + 1 ) :
for j in range ( 0 , min ( i , n + 1 ) ) :
if j == 0 or j == i :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [... | int evenSum ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
for ( int... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def evenSum ( n ) :
C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ]
for i in range ( 0 , n + 1 ) :
for j in range ( 0 , min ( i , n + 1 ) ) :
if j == 0 or j == i :
C [ i ] [ j ] = 1
else :
C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ... |
python-cpp-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N_1 | python-cpp | def getMinSquares ( n ) :
dp = [ 0 , 1 , 2 , 3 ]
for i in range ( 4 , n + 1 ) :
dp.append ( i )
for x in range ( 1 , int ( ceil ( sqrt ( i ) ) ) + 1 ) :
temp = x * x ;
if temp > i :
break
else :
dp [ i ] = min ( dp [ i ] , 1 + d... | int getMinSquares ( int n ) {
int * dp = new int [ n + 1 ] ;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
dp [ 2 ] = 2 ;
dp [ 3 ] = 3 ;
for ( int i = 4 ;
i <= n ;
i ++ ) {
dp [ i ] = i ;
for ( int x = 1 ;
x <= ceil ( sqrt ( i ) ) ;
x ++ ) {
int temp = x * x ;
if ( temp > i ) break ;
e... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getMinSquares ( n ) :
dp = [ 0 , 1 , 2 , 3 ]
for i in range ( 4 , n + 1 ) :
dp.append ( i )
for x in range ( 1 , int ( ceil ( sqrt ( i ) ) ) + 1 ) :
temp = x * x ;
if temp > i :
break
else :
dp [ i ] = min ( dp [ i ] ... |
python-cpp-SEGREGATE_EVEN_ODD_NUMBERS_SET_3 | python-cpp | def arrayEvenAndOdd ( arr , n ) :
i = - 1
j = 0
while ( j != n ) :
if ( arr [ j ] % 2 == 0 ) :
i = i + 1
arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
j = j + 1
for i in arr :
print ( str ( i ) + " " , end = '' )
| void arrayEvenAndOdd ( int arr [ ] , int n ) {
int i = - 1 , j = 0 ;
int t ;
while ( j != n ) {
if ( arr [ j ] % 2 == 0 ) {
i ++ ;
swap ( arr [ i ] , arr [ j ] ) ;
}
j ++ ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << arr [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def arrayEvenAndOdd ( arr , n ) :
i = - 1
j = 0
while ( j != n ) :
if ( arr [ j ] % 2 == 0 ) :
i = i + 1
arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ]
j = j + 1
for i in arr :
print ( str ( i ) + " " , end = '' )
</Code> |
python-cpp-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS | python-cpp | def diagonalsquare ( mat , row , column ) :
print ( "Diagonal one : " , end = "" )
for i in range ( 0 , row ) :
for j in range ( 0 , column ) :
if ( i == j ) :
print ( "{} ".format ( mat [ i ] [ j ] * mat [ i ] [ j ] ) , end = "" )
print ( " \n\nDiagonal two : " , end = "... | void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) {
cout << "Diagonal one : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
for ( int j = 0 ;
j < column ;
j ++ ) if ( i == j ) cout << mat [ i ] [ j ] * mat [ i ] [ j ] << " " ;
}
cout << " \n\nDiagonal two : " ;
for ( int i = 0 ;
i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def diagonalsquare ( mat , row , column ) :
print ( "Diagonal one : " , end = "" )
for i in range ( 0 , row ) :
for j in range ( 0 , column ) :
if ( i == j ) :
print ( "{} ".format ( mat [ i ] [ j ] * mat [ i ] [ j ] ) , end = "" )
print ( " \n\nDiagonal two : " , ... |
python-cpp-CHECK_SUMS_TH_ROW_TH_COLUMN_MATRIX | python-cpp | def areSumSame ( a , n , m ) :
sum1 = 0
sum2 = 0
for i in range ( 0 , n ) :
sum1 = 0
sum2 = 0
for j in range ( 0 , m ) :
sum1 += a [ i ] [ j ]
sum2 += a [ j ] [ i ]
if ( sum1 == sum2 ) :
return 1
return 0
| bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) {
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum1 = 0 , sum2 = 0 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
sum1 += a [ i ] [ j ] ;
sum2 += a [ j ] [ i ] ;
}
if ( sum1 == sum2 ) return true ;
}
return false... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def areSumSame ( a , n , m ) :
sum1 = 0
sum2 = 0
for i in range ( 0 , n ) :
sum1 = 0
sum2 = 0
for j in range ( 0 , m ) :
sum1 += a [ i ] [ j ]
sum2 += a [ j ] [ i ]
if ( sum1 == sum2 ) :
return 1
return 0
</Code> |
python-cpp-GIVEN_SORTED_ARRAY_NUMBER_X_FIND_PAIR_ARRAY_WHOSE_SUM_CLOSEST_X | python-cpp | def printClosest ( arr , n , x ) :
res_l , res_r = 0 , 0
l , r , diff = 0 , n - 1 , MAX_VAL
while r > l :
if abs ( arr [ l ] + arr [ r ] - x ) < diff :
res_l = l
res_r = r
diff = abs ( arr [ l ] + arr [ r ] - x )
if arr [ l ] + arr [ r ] > x :
... | void printClosest ( int arr [ ] , int n , int x ) {
int res_l , res_r ;
int l = 0 , r = n - 1 , diff = INT_MAX ;
while ( r > l ) {
if ( abs ( arr [ l ] + arr [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( arr [ l ] + arr [ r ] - x ) ;
}
if ( arr [ l ] + arr [ r ] > x ) r... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printClosest ( arr , n , x ) :
res_l , res_r = 0 , 0
l , r , diff = 0 , n - 1 , MAX_VAL
while r > l :
if abs ( arr [ l ] + arr [ r ] - x ) < diff :
res_l = l
res_r = r
diff = abs ( arr [ l ] + arr [ r ] - x )
if arr [ l ] + arr [ r ] > x :
... |
python-cpp-GIVEN_BINARY_STRING_COUNT_NUMBER_SUBSTRINGS_START_END_1_1 | python-cpp | def countSubStr ( st , n ) :
m = 0
for i in range ( 0 , n ) :
if ( st [ i ] == '1' ) :
m = m + 1
return m * ( m - 1 ) // 2
| int countSubStr ( char str [ ] ) {
int m = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
i ++ ) {
if ( str [ i ] == '1' ) m ++ ;
}
return m * ( m - 1 ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSubStr ( st , n ) :
m = 0
for i in range ( 0 , n ) :
if ( st [ i ] == '1' ) :
m = m + 1
return m * ( m - 1 ) // 2
</Code> |
python-cpp-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS | python-cpp | def countSubstringWithEqualEnds ( s ) :
result = 0
n = len ( s )
for i in range ( n ) :
for j in range ( i , n ) :
if ( s [ i ] == s [ j ] ) :
result = result + 1
return result
| int countSubstringWithEqualEnds ( string s ) {
int result = 0 ;
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i ;
j < n ;
j ++ ) if ( s [ i ] == s [ j ] ) result ++ ;
return result ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSubstringWithEqualEnds ( s ) :
result = 0
n = len ( s )
for i in range ( n ) :
for j in range ( i , n ) :
if ( s [ i ] == s [ j ] ) :
result = result + 1
return result
</Code> |
python-cpp-CALCULATE_MAXIMUM_VALUE_USING_SIGN_TWO_NUMBERS_STRING | python-cpp | def calcMaxValue ( str ) :
res = ord ( str [ 0 ] ) - 48
for i in range ( 1 , len ( str ) ) :
if ( str [ i ] == '0' or str [ i ] == '1' or res < 2 ) :
res += ord ( str [ i ] ) - 48
else :
res *= ord ( str [ i ] ) - 48
return res
| int calcMaxValue ( string str ) {
int res = str [ 0 ] - '0' ;
for ( int i = 1 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ) ;
else res *= ( str [ i ] - '0' ) ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def calcMaxValue ( str ) :
res = ord ( str [ 0 ] ) - 48
for i in range ( 1 , len ( str ) ) :
if ( str [ i ] == '0' or str [ i ] == '1' or res < 2 ) :
res += ord ( str [ i ] ) - 48
else :
res *= ord ( str [ i ] ) - 48
return res
</Code> |
python-cpp-PROGRAM_BEST_FIT_ALGORITHM_MEMORY_MANAGEMENT | python-cpp | def bestFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
bestIdx = - 1
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
if bestIdx == - 1 :
bestIdx = j
elif blockSize [ bestId... | void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ n ] ;
memset ( allocation , - 1 , sizeof ( allocation ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int bestIdx = - 1 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def bestFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
bestIdx = - 1
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
if bestIdx == - 1 :
bestIdx = j
elif blockSize [... |
python-cpp-FREQUENCY_ELEMENT_UNSORTED_ARRAY_1 | python-cpp | def countFreq ( a , n ) :
hm = dict ( )
for i in range ( n ) :
hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1
cumul = 0
for i in range ( n ) :
cumul += hm [ a [ i ] ]
if ( hm [ a [ i ] ] > 0 ) :
print ( a [ i ] , "->" , cumul )
hm [ a [ i ] ] = 0
| void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
int cumul = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
cumul += hm [ a [ i ] ] ;
if ( hm [ a [ i ] ] ) {
cout << a [ i ] << "->" << cumul << endl ;
}
hm [ a [ i ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countFreq ( a , n ) :
hm = dict ( )
for i in range ( n ) :
hm [ a [ i ] ] = hm.get ( a [ i ] , 0 ) + 1
cumul = 0
for i in range ( n ) :
cumul += hm [ a [ i ] ]
if ( hm [ a [ i ] ] > 0 ) :
print ( a [ i ] , "->" , cumul )
hm [ a [ i ] ] = 0
</Code> |
python-cpp-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8 | python-cpp | def isSubSeqDivisible ( st ) :
l = len ( st )
arr = [ 0 ] * l
for i in range ( 0 , l ) :
for j in range ( i , l ) :
for k in range ( j , l ) :
if ( arr [ i ] % 8 == 0 ) :
return True
elif ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 and i ... | bool isSubSeqDivisible ( string str ) {
for ( int i = 0 ;
i < l ;
i ++ ) {
for ( int j = i ;
j < l ;
j ++ ) {
for ( int k = j ;
k < l ;
k ++ ) {
if ( arr [ i ] % 8 == 0 ) return true ;
else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isSubSeqDivisible ( st ) :
l = len ( st )
arr = [ 0 ] * l
for i in range ( 0 , l ) :
for j in range ( i , l ) :
for k in range ( j , l ) :
if ( arr [ i ] % 8 == 0 ) :
return True
elif ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0... |
python-cpp-COMPUTE_THE_INTEGER_ABSOLUTE_VALUE_ABS_WITHOUT_BRANCHING | python-cpp | def getAbs ( n ) :
mask = n >> ( SIZE_INT * CHARBIT - 1 )
return ( ( n + mask ) ^ mask )
| unsigned int getAbs ( int n ) {
int const mask = n >> ( sizeof ( int ) * CHARBIT - 1 ) ;
return ( ( n + mask ) ^ mask ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getAbs ( n ) :
mask = n >> ( SIZE_INT * CHARBIT - 1 )
return ( ( n + mask ) ^ mask )
</Code> |
python-cpp-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY | python-cpp | def countPairs ( arr , n ) :
result = 0 ;
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
product = arr [ i ] * arr [ j ] ;
for k in range ( 0 , n ) :
if ( arr [ k ] == product ) :
result = result + 1 ;
break ;
... | int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i ] * arr [ j ] ;
for ( int k = 0 ;
k < n ;
k ++ ) {
if ( arr [ k ] == product ) {
result ++ ;
b... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countPairs ( arr , n ) :
result = 0 ;
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
product = arr [ i ] * arr [ j ] ;
for k in range ( 0 , n ) :
if ( arr [ k ] == product ) :
result = result + 1 ;
brea... |
python-cpp-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM_1 | python-cpp | def minCost ( a , n , k ) :
dp = [ [ inf for i in range ( k + 1 ) ] for j in range ( n + 1 ) ]
dp [ 0 ] [ 0 ] = 0
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , k + 1 ) :
for m in range ( i - 1 , - 1 , - 1 ) :
dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ m ] [ j - 1 ... | int minCost ( int a [ ] , int n , int k ) {
int dp [ n + 1 ] [ k + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= k ;
j ++ ) dp [ i ] [ j ] = inf ;
dp [ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= k ;
j ++ ) for ( int m = i - 1 ;
m >= 0 ;
m -- ) d... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minCost ( a , n , k ) :
dp = [ [ inf for i in range ( k + 1 ) ] for j in range ( n + 1 ) ]
dp [ 0 ] [ 0 ] = 0
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , k + 1 ) :
for m in range ( i - 1 , - 1 , - 1 ) :
dp [ i ] [ j ] = min ( dp [ i ] [ j ] , dp [ m ] [... |
python-cpp-CHECK_GRAPHS_CYCLE_ODD_LENGTH | python-cpp | def containsOdd ( G , src ) :
global V
colorArr = [ - 1 ] * V
colorArr [ src ] = 1
q = queue.Queue ( )
q.put ( src )
while ( not q.empty ( ) ) :
u = q.get ( )
if ( G [ u ] [ u ] == 1 ) :
return True
for v in range ( V ) :
if ( G [ u ] [ v ] and col... | bool containsOdd ( int G [ ] [ V ] , int src ) {
int colorArr [ V ] ;
for ( int i = 0 ;
i < V ;
++ i ) colorArr [ i ] = - 1 ;
colorArr [ src ] = 1 ;
queue < int > q ;
q . push ( src ) ;
while ( ! q . empty ( ) ) {
int u = q . front ( ) ;
q . pop ( ) ;
if ( G [ u ] [ u ] == 1 ) return true ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def containsOdd ( G , src ) :
global V
colorArr = [ - 1 ] * V
colorArr [ src ] = 1
q = queue.Queue ( )
q.put ( src )
while ( not q.empty ( ) ) :
u = q.get ( )
if ( G [ u ] [ u ] == 1 ) :
return True
for v in range ( V ) :
if ( G [ u ] [ v ] ... |
python-cpp-COUNT_SET_BITS_IN_AN_INTEGER_3 | python-cpp | def countSetBits ( n ) :
if ( n == 0 ) :
return 0
else :
return 1 + countSetBits ( n & ( n - 1 ) )
| int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return 1 + countSetBits ( n & ( 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 1 + countSetBits ( n & ( n - 1 ) )
</Code> |
python-cpp-CHECK_STRING_FOLLOWS_ANBN_PATTERN_NOT | python-cpp | def isAnBn ( str ) :
n = len ( str )
for i in range ( n ) :
if ( str [ i ] != 'a' ) :
break
if ( i * 2 != n ) :
return False
for j in range ( i , n ) :
if ( str [ j ] != 'b' ) :
return False
return True
| bool isAnBn ( string str ) {
int n = str . length ( ) ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) if ( str [ i ] != 'a' ) break ;
if ( i * 2 != n ) return false ;
int j ;
for ( j = i ;
j < n ;
j ++ ) if ( str [ j ] != 'b' ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isAnBn ( str ) :
n = len ( str )
for i in range ( n ) :
if ( str [ i ] != 'a' ) :
break
if ( i * 2 != n ) :
return False
for j in range ( i , n ) :
if ( str [ j ] != 'b' ) :
return False
return True
</Code> |
python-cpp-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N | python-cpp | def getMinSquares ( n ) :
if n <= 3 :
return n ;
res = n
for x in range ( 1 , n + 1 ) :
temp = x * x ;
if temp > n :
break
else :
res = min ( res , 1 + getMinSquares ( n - temp ) )
return res ;
| int getMinSquares ( unsigned int n ) {
if ( sqrt ( n ) - floor ( sqrt ( n ) ) == 0 ) return 1 ;
if ( n <= 3 ) return n ;
int res = n ;
for ( int x = 1 ;
x <= n ;
x ++ ) {
int temp = x * x ;
if ( temp > n ) break ;
else res = min ( res , 1 + getMinSquares ( n - temp ) ) ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getMinSquares ( n ) :
if n <= 3 :
return n ;
res = n
for x in range ( 1 , n + 1 ) :
temp = x * x ;
if temp > n :
break
else :
res = min ( res , 1 + getMinSquares ( n - temp ) )
return res ;
</Code> |
python-cpp-MAXIMUM_DISTANCE_TWO_OCCURRENCES_ELEMENT_ARRAY | python-cpp | def maxDistance ( arr , n ) :
mp = { }
maxDict = 0
for i in range ( n ) :
if arr [ i ] not in mp.keys ( ) :
mp [ arr [ i ] ] = i
else :
maxDict = max ( maxDict , i - mp [ arr [ i ] ] )
return maxDict
| int maxDistance ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
int max_dist = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ;
else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ;
}
return max_dist ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxDistance ( arr , n ) :
mp = { }
maxDict = 0
for i in range ( n ) :
if arr [ i ] not in mp.keys ( ) :
mp [ arr [ i ] ] = i
else :
maxDict = max ( maxDict , i - mp [ arr [ i ] ] )
return maxDict
</Code> |
python-cpp-MAXIMUM_SUBSEQUENCE_SUM_SUCH_THAT_NO_THREE_ARE_CONSECUTIVE | python-cpp | def maxSumWO3Consec ( arr , n ) :
sum = [ 0 for k in range ( n ) ]
if n >= 1 :
sum [ 0 ] = arr [ 0 ]
if n >= 2 :
sum [ 1 ] = arr [ 0 ] + arr [ 1 ]
if n > 2 :
sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) )
for i in range ( 3 , n ) :
... | int maxSumWO3Consec ( int arr [ ] , int n ) {
int sum [ n ] ;
if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ;
if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ;
if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ;
for ( int i = 3 ;
i < n ;
i ++ ) sum [ i ] = max ( max ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSumWO3Consec ( arr , n ) :
sum = [ 0 for k in range ( n ) ]
if n >= 1 :
sum [ 0 ] = arr [ 0 ]
if n >= 2 :
sum [ 1 ] = arr [ 0 ] + arr [ 1 ]
if n > 2 :
sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) )
for i in range ( 3 , n ) ... |
python-cpp-EULERS_FOUR_SQUARE_IDENTITY_1 | python-cpp | def checkEulerFourSquareIdentity ( a , b ) :
ab = a * b
flag = False
i = 0
while i * i <= ab :
j = i
while i * i + j * j <= ab :
k = j
while i * i + j * j + k * k <= ab :
l = ( ab - ( i * i + j * j + k * k ) ) ** ( 0.5 )
if l == int... | void checkEulerFourSquareIdentity ( int a , int b ) {
int ab = a * b ;
bool flag = false ;
int i = 0 ;
while ( i * i <= ab ) {
int j = i ;
while ( i * i + j * j <= ab ) {
int k = j ;
while ( i * i + j * j + k * k <= ab ) {
double l = sqrt ( ab - ( i * i + j * j + k * k ) ) ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def checkEulerFourSquareIdentity ( a , b ) :
ab = a * b
flag = False
i = 0
while i * i <= ab :
j = i
while i * i + j * j <= ab :
k = j
while i * i + j * j + k * k <= ab :
l = ( ab - ( i * i + j * j + k * k ) ) ** ( 0.5 )
if l... |
python-cpp-NOBLE_INTEGERS_IN_AN_ARRAY_COUNT_OF_GREATER_ELEMENTS_IS_EQUAL_TO_VALUE_1 | python-cpp | def nobleInteger ( arr ) :
arr.sort ( )
n = len ( arr )
for i in range ( n - 1 ) :
if arr [ i ] == arr [ i + 1 ] :
continue
if arr [ i ] == n - i - 1 :
return arr [ i ]
if arr [ n - 1 ] == 0 :
return arr [ n - 1 ]
return - 1
| int nobleInteger ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] == arr [ i + 1 ] ) continue ;
if ( arr [ i ] == n - i - 1 ) return arr [ i ] ;
}
if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ;
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def nobleInteger ( arr ) :
arr.sort ( )
n = len ( arr )
for i in range ( n - 1 ) :
if arr [ i ] == arr [ i + 1 ] :
continue
if arr [ i ] == n - i - 1 :
return arr [ i ]
if arr [ n - 1 ] == 0 :
return arr [ n - 1 ]
return - 1
</Code> |
python-cpp-COUNT_NUMBER_ISLANDS_EVERY_ISLAND_SEPARATED_LINE | python-cpp | def countIslands ( mat ) :
count = 0
for i in range ( 0 , M ) :
for j in range ( 0 , N ) :
if ( mat [ i ] [ j ] == 'X' ) :
if ( ( i == 0 or mat [ i - 1 ] [ j ] == 'O' ) and ( j == 0 or mat [ i ] [ j - 1 ] == 'O' ) ) :
count = count + 1
return count
| int countIslands ( int mat [ ] [ N ] ) {
int count = 0 ;
for ( int i = 0 ;
i < M ;
i ++ ) {
for ( int j = 0 ;
j < N ;
j ++ ) {
if ( mat [ i ] [ j ] == 'X' ) {
if ( ( i == 0 || mat [ i - 1 ] [ j ] == 'O' ) && ( j == 0 || mat [ i ] [ j - 1 ] == 'O' ) ) count ++ ;
}
}
}
retu... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countIslands ( mat ) :
count = 0
for i in range ( 0 , M ) :
for j in range ( 0 , N ) :
if ( mat [ i ] [ j ] == 'X' ) :
if ( ( i == 0 or mat [ i - 1 ] [ j ] == 'O' ) and ( j == 0 or mat [ i ] [ j - 1 ] == 'O' ) ) :
count = count + 1
return co... |
python-cpp-SELECT_A_RANDOM_NUMBER_FROM_STREAM_WITH_O1_SPACE | python-cpp | def selectRandom ( x ) :
res = 0
count = 0
count += 1
if ( count == 1 ) :
res = x
else :
i = random.randrange ( count )
if ( i == count - 1 ) :
res = x
return res
| int selectRandom ( int x ) {
static int res ;
static int count = 0 ;
count ++ ;
if ( count == 1 ) res = x ;
else {
int i = rand ( ) % count ;
if ( i == count - 1 ) res = x ;
}
return res ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def selectRandom ( x ) :
res = 0
count = 0
count += 1
if ( count == 1 ) :
res = x
else :
i = random.randrange ( count )
if ( i == count - 1 ) :
res = x
return res
</Code> |
python-cpp-A_PRODUCT_ARRAY_PUZZLE_1 | python-cpp | def productArray ( arr , n ) :
if n == 1 :
print ( 0 )
return
i , temp = 1 , 1
prod = [ 1 for i in range ( n ) ]
for i in range ( n ) :
prod [ i ] = temp
temp *= arr [ i ]
temp = 1
for i in range ( n - 1 , - 1 , - 1 ) :
prod [ i ] *= temp
temp *= a... | void productArray ( int arr [ ] , int n ) {
if ( n == 1 ) {
cout << 0 ;
return ;
}
int i , temp = 1 ;
int * prod = new int [ ( sizeof ( int ) * n ) ] ;
memset ( prod , 1 , n ) ;
for ( i = 0 ;
i < n ;
i ++ ) {
prod [ i ] = temp ;
temp *= arr [ i ] ;
}
temp = 1 ;
for ( i = n - 1 ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def productArray ( arr , n ) :
if n == 1 :
print ( 0 )
return
i , temp = 1 , 1
prod = [ 1 for i in range ( n ) ]
for i in range ( n ) :
prod [ i ] = temp
temp *= arr [ i ]
temp = 1
for i in range ( n - 1 , - 1 , - 1 ) :
prod [ i ] *= temp
te... |
python-cpp-SORT_1_N_SWAPPING_ADJACENT_ELEMENTS | python-cpp | def sortedAfterSwap ( A , B , n ) :
for i in range ( 0 , n - 1 ) :
if ( B [ i ] == 1 ) :
j = i
while ( B [ j ] == 1 ) :
j = j + 1
A = A [ 0 : i ] + sorted ( A [ i : j + 1 ] ) + A [ j + 1 : ]
i = j
for i in range ( 0 , n ) :
if ( A [... | bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) {
if ( B [ i ] ) {
j = i ;
while ( B [ j ] ) j ++ ;
sort ( A + i , A + 1 + j ) ;
i = j ;
}
}
for ( i = 0 ;
i < n ;
i ++ ) {
if ( A [ i ] != i + 1 ) return false ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortedAfterSwap ( A , B , n ) :
for i in range ( 0 , n - 1 ) :
if ( B [ i ] == 1 ) :
j = i
while ( B [ j ] == 1 ) :
j = j + 1
A = A [ 0 : i ] + sorted ( A [ i : j + 1 ] ) + A [ j + 1 : ]
i = j
for i in range ( 0 , n ) :
i... |
python-cpp-FIND_THE_MAXIMUM_SUBARRAY_XOR_IN_A_GIVEN_ARRAY | python-cpp | def maxSubarrayXOR ( arr , n ) :
ans = - 2147483648
for i in range ( n ) :
curr_xor = 0
for j in range ( i , n ) :
curr_xor = curr_xor ^ arr [ j ]
ans = max ( ans , curr_xor )
return ans
| int maxSubarrayXOR ( int arr [ ] , int n ) {
int ans = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int curr_xor = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
curr_xor = curr_xor ^ arr [ j ] ;
ans = max ( ans , curr_xor ) ;
}
}
return ans ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxSubarrayXOR ( arr , n ) :
ans = - 2147483648
for i in range ( n ) :
curr_xor = 0
for j in range ( i , n ) :
curr_xor = curr_xor ^ arr [ j ]
ans = max ( ans , curr_xor )
return ans
</Code> |
python-cpp-MINIMUM_COST_MAKE_ARRAY_SIZE_1_REMOVING_LARGER_PAIRS | python-cpp | def cost ( a , n ) :
return ( ( n - 1 ) * min ( a ) )
| int cost ( int a [ ] , int n ) {
return ( n - 1 ) * ( * min_element ( a , a + n ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def cost ( a , n ) :
return ( ( n - 1 ) * min ( a ) )
</Code> |
python-cpp-REMOVE_MINIMUM_ELEMENTS_EITHER_SIDE_2MIN_MAX | python-cpp | def minRemovalsDP ( arr , n ) :
longest_start = - 1
longest_end = 0
for start in range ( n ) :
min = sys.maxsize
max = - sys.maxsize
for end in range ( start , n ) :
val = arr [ end ]
if ( val < min ) :
min = val
if ( val > max ) :
... | int minRemovalsDP ( int arr [ ] , int n ) {
int longest_start = - 1 , longest_end = 0 ;
for ( int start = 0 ;
start < n ;
start ++ ) {
int min = INT_MAX , max = INT_MIN ;
for ( int end = start ;
end < n ;
end ++ ) {
int val = arr [ end ] ;
if ( val < min ) min = val ;
if ( val ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minRemovalsDP ( arr , n ) :
longest_start = - 1
longest_end = 0
for start in range ( n ) :
min = sys.maxsize
max = - sys.maxsize
for end in range ( start , n ) :
val = arr [ end ]
if ( val < min ) :
min = val
if ( val > m... |
python-cpp-COUNT_INVERSIONS_OF_SIZE_THREE_IN_A_GIVE_ARRAY_1 | python-cpp | def getInvCount ( arr , n ) :
invcount = 0
for i in range ( 1 , n - 1 ) :
small = 0
for j in range ( i + 1 , n ) :
if ( arr [ i ] > arr [ j ] ) :
small += 1
great = 0
for j in range ( i - 1 , - 1 , - 1 ) :
if ( arr [ i ] < arr [ j ] ) :
... | int getInvCount ( int arr [ ] , int n ) {
int invcount = 0 ;
for ( int i = 1 ;
i < n - 1 ;
i ++ ) {
int small = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ;
int great = 0 ;
for ( int j = i - 1 ;
j >= 0 ;
j -- ) if ( arr [ i ] < arr [ j ] ) grea... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getInvCount ( arr , n ) :
invcount = 0
for i in range ( 1 , n - 1 ) :
small = 0
for j in range ( i + 1 , n ) :
if ( arr [ i ] > arr [ j ] ) :
small += 1
great = 0
for j in range ( i - 1 , - 1 , - 1 ) :
if ( arr [ i ] < arr [ j ] ... |
python-cpp-PROGRAM_FOR_SURFACE_AREA_OF_OCTAHEDRON | python-cpp | def surface_area_octahedron ( side ) :
return ( 2 * ( math.sqrt ( 3 ) ) * ( side * side ) )
| double surface_area_octahedron ( double side ) {
return ( 2 * ( sqrt ( 3 ) ) * ( side * side ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def surface_area_octahedron ( side ) :
return ( 2 * ( math.sqrt ( 3 ) ) * ( side * side ) )
</Code> |
python-cpp-GNOME_SORT_A_STUPID_ONE | python-cpp | def gnomeSort ( arr , n ) :
index = 0
while index < n :
if index == 0 :
index = index + 1
if arr [ index ] >= arr [ index - 1 ] :
index = index + 1
else :
arr [ index ] , arr [ index - 1 ] = arr [ index - 1 ] , arr [ index ]
index = index -... | void gnomeSort ( int arr [ ] , int n ) {
int index = 0 ;
while ( index < n ) {
if ( index == 0 ) index ++ ;
if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ;
else {
swap ( arr [ index ] , arr [ index - 1 ] ) ;
index -- ;
}
}
return ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def gnomeSort ( arr , n ) :
index = 0
while index < n :
if index == 0 :
index = index + 1
if arr [ index ] >= arr [ index - 1 ] :
index = index + 1
else :
arr [ index ] , arr [ index - 1 ] = arr [ index - 1 ] , arr [ index ]
index = ... |
python-cpp-COUNT_FREQUENCY_K_MATRIX_SIZE_N_MATRIXI_J_IJ | python-cpp | def find ( n , k ) :
if ( n + 1 >= k ) :
return ( k - 1 )
else :
return ( 2 * n + 1 - k )
| int find ( int n , int k ) {
if ( n + 1 >= k ) return ( k - 1 ) ;
else return ( 2 * n + 1 - k ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def find ( n , k ) :
if ( n + 1 >= k ) :
return ( k - 1 )
else :
return ( 2 * n + 1 - k )
</Code> |
python-cpp-UNIQUE_CELLS_BINARY_MATRIX | python-cpp | def countUnique ( mat , n , m ) :
rowsum = [ 0 ] * n
colsum = [ 0 ] * m
for i in range ( n ) :
for j in range ( m ) :
if ( mat [ i ] [ j ] != 0 ) :
rowsum [ i ] += 1
colsum [ j ] += 1
uniquecount = 0
for i in range ( n ) :
for j in range ( ... | int countUnique ( int mat [ ] [ MAX ] , int n , int m ) {
int rowsum [ n ] , colsum [ m ] ;
memset ( colsum , 0 , sizeof ( colsum ) ) ;
memset ( rowsum , 0 , sizeof ( rowsum ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) if ( mat [ i ] [ j ] ) {
rowsum [ i ] ++ ;
colsum ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countUnique ( mat , n , m ) :
rowsum = [ 0 ] * n
colsum = [ 0 ] * m
for i in range ( n ) :
for j in range ( m ) :
if ( mat [ i ] [ j ] != 0 ) :
rowsum [ i ] += 1
colsum [ j ] += 1
uniquecount = 0
for i in range ( n ) :
for j in r... |
python-cpp-GENERATE_BINARY_PERMUTATIONS_1S_0S_EVERY_POINT_PERMUTATIONS | python-cpp | def generate ( ones , zeroes , str , len1 ) :
if ( len1 == len ( str ) ) :
print ( str , end = " " )
return
generate ( ones + 1 , zeroes , str + "1" , len1 )
if ( ones > zeroes ) :
generate ( ones , zeroes + 1 , str + "0" , len1 )
| void generate ( int ones , int zeroes , string str , int len ) {
if ( len == str . length ( ) ) {
cout << str << " " ;
return ;
}
generate ( ones + 1 , zeroes , str + "1" , len ) ;
if ( ones > zeroes ) generate ( ones , zeroes + 1 , str + "0" , len ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def generate ( ones , zeroes , str , len1 ) :
if ( len1 == len ( str ) ) :
print ( str , end = " " )
return
generate ( ones + 1 , zeroes , str + "1" , len1 )
if ( ones > zeroes ) :
generate ( ones , zeroes + 1 , str + "0" , len1 )
</Code> |
python-cpp-MID_POINT_CIRCLE_DRAWING_ALGORITHM | python-cpp | def midPointCircleDraw ( x_centre , y_centre , r ) :
x = r
y = 0
print ( "(" , x + x_centre , ", " , y + y_centre , ")" , sep = "" , end = "" )
if ( r > 0 ) :
print ( "(" , x + x_centre , ", " , - y + y_centre , ")" , sep = "" , end = "" )
print ( "(" , y + x_centre , ", " , x + y_centre... | void midPointCircleDraw ( int x_centre , int y_centre , int r ) {
int x = r , y = 0 ;
cout << "(" << x + x_centre << ", " << y + y_centre << ") " ;
if ( r > 0 ) {
cout << "(" << x + x_centre << ", " << - y + y_centre << ") " ;
cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;
cout << "(" <... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def midPointCircleDraw ( x_centre , y_centre , r ) :
x = r
y = 0
print ( "(" , x + x_centre , ", " , y + y_centre , ")" , sep = "" , end = "" )
if ( r > 0 ) :
print ( "(" , x + x_centre , ", " , - y + y_centre , ")" , sep = "" , end = "" )
print ( "(" , y + x_centre , ", " , x + y... |
python-cpp-FIND_FACTORIAL_NUMBERS_LESS_EQUAL_N | python-cpp | def printFactorialNums ( n ) :
fact = 1
x = 2
while fact <= n :
print ( fact , end = " " )
fact = fact * x
x += 1
| void printFactorialNums ( int n ) {
int fact = 1 ;
int x = 2 ;
while ( fact <= n ) {
cout << fact << " " ;
fact = fact * x ;
x ++ ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printFactorialNums ( n ) :
fact = 1
x = 2
while fact <= n :
print ( fact , end = " " )
fact = fact * x
x += 1
</Code> |
python-cpp-FRIENDS_PAIRING_PROBLEM | python-cpp | def countFriendsPairings ( n ) :
dp = [ 0 for i in range ( n + 1 ) ]
for i in range ( n + 1 ) :
if ( i <= 2 ) :
dp [ i ] = i
else :
dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ]
return dp [ n ]
| int countFriendsPairings ( int n ) {
int dp [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
if ( i <= 2 ) dp [ i ] = i ;
else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ;
}
return dp [ n ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countFriendsPairings ( n ) :
dp = [ 0 for i in range ( n + 1 ) ]
for i in range ( n + 1 ) :
if ( i <= 2 ) :
dp [ i ] = i
else :
dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ]
return dp [ n ]
</Code> |
python-cpp-PROGRAM_FIND_REMAINDER_LARGE_NUMBER_DIVIDED_11 | python-cpp | def remainder ( st ) :
ln = len ( st )
rem = 0
for i in range ( 0 , ln ) :
num = rem * 10 + ( int ) ( st [ i ] )
rem = num % 11
return rem
| int remainder ( string str ) {
int len = str . length ( ) ;
int num , rem = 0 ;
for ( int i = 0 ;
i < len ;
i ++ ) {
num = rem * 10 + ( str [ i ] - '0' ) ;
rem = num % 11 ;
}
return rem ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def remainder ( st ) :
ln = len ( st )
rem = 0
for i in range ( 0 , ln ) :
num = rem * 10 + ( int ) ( st [ i ] )
rem = num % 11
return rem
</Code> |
python-cpp-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN | python-cpp | def findNth ( n ) :
count = 0
for curr in itertools.count ( ) :
sum = 0
x = curr
while ( x ) :
sum = sum + x % 10
x = x // 10
if ( sum == 10 ) :
count = count + 1
if ( count == n ) :
return curr
return - 1
| int findNth ( int n ) {
int count = 0 ;
for ( int curr = 1 ;
;
curr ++ ) {
int sum = 0 ;
for ( int x = curr ;
x > 0 ;
x = x / 10 ) sum = sum + x % 10 ;
if ( sum == 10 ) count ++ ;
if ( count == n ) return curr ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findNth ( n ) :
count = 0
for curr in itertools.count ( ) :
sum = 0
x = curr
while ( x ) :
sum = sum + x % 10
x = x // 10
if ( sum == 10 ) :
count = count + 1
if ( count == n ) :
return curr
return - 1
</Code... |
python-cpp-SORT_1_N_SWAPPING_ADJACENT_ELEMENTS_1 | python-cpp | def sortedAfterSwap ( A , B , n ) :
for i in range ( 0 , n - 1 ) :
if B [ i ] :
if A [ i ] != i + 1 :
A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ]
for i in range ( n ) :
if A [ i ] != i + 1 :
return False
return True
| bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) {
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( B [ i ] ) {
if ( A [ i ] != i + 1 ) swap ( A [ i ] , A [ i + 1 ] ) ;
}
}
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( A [ i ] != i + 1 ) return false ;
}
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortedAfterSwap ( A , B , n ) :
for i in range ( 0 , n - 1 ) :
if B [ i ] :
if A [ i ] != i + 1 :
A [ i ] , A [ i + 1 ] = A [ i + 1 ] , A [ i ]
for i in range ( n ) :
if A [ i ] != i + 1 :
return False
return True
</Code> |
python-cpp-MAXIMIZE_ARRAY_SUN_AFTER_K_NEGATION_OPERATIONS | python-cpp | def maximumSum ( arr , n , k ) :
for i in range ( 1 , k + 1 ) :
min = + 2147483647
index = - 1
for j in range ( n ) :
if ( arr [ j ] < min ) :
min = arr [ j ]
index = j
if ( min == 0 ) :
break
arr [ index ] = - arr [ ind... | int maximumSum ( int arr [ ] , int n , int k ) {
for ( int i = 1 ;
i <= k ;
i ++ ) {
int min = INT_MAX ;
int index = - 1 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ j ] < min ) {
min = arr [ j ] ;
index = j ;
}
}
if ( min == 0 ) break ;
arr [ index ]... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maximumSum ( arr , n , k ) :
for i in range ( 1 , k + 1 ) :
min = + 2147483647
index = - 1
for j in range ( n ) :
if ( arr [ j ] < min ) :
min = arr [ j ]
index = j
if ( min == 0 ) :
break
arr [ index ] = - ar... |
python-cpp-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING_1 | python-cpp | def printSquares ( n ) :
square = 0
odd = 1
for x in range ( 0 , n ) :
print ( square , end = " " )
square = square + odd
odd = odd + 2
| void printSquares ( int n ) {
int square = 0 , odd = 1 ;
for ( int x = 0 ;
x < n ;
x ++ ) {
cout << square << " " ;
square = square + odd ;
odd = odd + 2 ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printSquares ( n ) :
square = 0
odd = 1
for x in range ( 0 , n ) :
print ( square , end = " " )
square = square + odd
odd = odd + 2
</Code> |
python-cpp-CHECK_REVERSING_SUB_ARRAY_MAKE_ARRAY_SORTED_1 | python-cpp | def checkReverse ( arr , n ) :
if ( n == 1 ) :
return True
i = 1
for i in range ( 1 , n ) :
if arr [ i - 1 ] < arr [ i ] :
if ( i == n ) :
return True
j = i
while ( arr [ j ] < arr [ j - 1 ] ) :
if ( i > 1 and arr [ j ] < arr [ i - 2 ] ) :
... | bool checkReverse ( int arr [ ] , int n ) {
if ( n == 1 ) return true ;
int i ;
for ( i = 1 ;
i < n && arr [ i - 1 ] < arr [ i ] ;
i ++ ) ;
if ( i == n ) return true ;
int j = i ;
while ( arr [ j ] < arr [ j - 1 ] ) {
if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) return false ;
j ++ ;
}
if ( j =... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def checkReverse ( arr , n ) :
if ( n == 1 ) :
return True
i = 1
for i in range ( 1 , n ) :
if arr [ i - 1 ] < arr [ i ] :
if ( i == n ) :
return True
j = i
while ( arr [ j ] < arr [ j - 1 ] ) :
if ( i > 1 and arr [ j ] < arr [ i - 2 ] ) :
... |
python-cpp-PROGRAM_CALCULATE_VOLUME_OCTAHEDRON | python-cpp | def vol_of_octahedron ( side ) :
return ( ( side * side * side ) * ( math.sqrt ( 2 ) / 3 ) )
| double vol_of_octahedron ( double side ) {
return ( ( side * side * side ) * ( sqrt ( 2 ) / 3 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def vol_of_octahedron ( side ) :
return ( ( side * side * side ) * ( math.sqrt ( 2 ) / 3 ) )
</Code> |
python-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX | python-cpp | def numberOfPaths ( m , n ) :
if ( m == 1 or n == 1 ) :
return 1
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 )
| int numberOfPaths ( int m , int n ) {
if ( m == 1 || n == 1 ) return 1 ;
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfPaths ( m , n ) :
if ( m == 1 or n == 1 ) :
return 1
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 )
</Code> |
python-cpp-BINARY_SEARCH | python-cpp | def binarySearch ( arr , l , r , x ) :
if r >= l :
mid = l + ( r - l ) // 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] > x :
return binarySearch ( arr , l , mid - 1 , x )
else :
return binarySearch ( arr , mid + 1 , r , x )
else :
... | int binarySearch ( int arr [ ] , int l , int r , int x ) {
if ( r >= l ) {
int mid = l + ( r - l ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ;
return binarySearch ( arr , mid + 1 , r , x ) ;
}
return - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def binarySearch ( arr , l , r , x ) :
if r >= l :
mid = l + ( r - l ) // 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] > x :
return binarySearch ( arr , l , mid - 1 , x )
else :
return binarySearch ( arr , mid + 1 , r , x )
else :... |
python-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_1 | python-cpp | def numberOfPaths ( m , n ) :
count = [ [ 0 for x in range ( m ) ] for y in range ( n ) ]
for i in range ( m ) :
count [ i ] [ 0 ] = 1 ;
for j in range ( n ) :
count [ 0 ] [ j ] = 1 ;
for i in range ( 1 , m ) :
for j in range ( n ) :
count [ i ] [ j ] = count [ i - 1 ... | int numberOfPaths ( int m , int n ) {
int count [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) count [ i ] [ 0 ] = 1 ;
for ( int j = 0 ;
j < n ;
j ++ ) count [ 0 ] [ j ] = 1 ;
for ( int i = 1 ;
i < m ;
i ++ ) {
for ( int j = 1 ;
j < n ;
j ++ ) count [ i ] [ j ] = count [ i - 1 ] [ j ] + ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfPaths ( m , n ) :
count = [ [ 0 for x in range ( m ) ] for y in range ( n ) ]
for i in range ( m ) :
count [ i ] [ 0 ] = 1 ;
for j in range ( n ) :
count [ 0 ] [ j ] = 1 ;
for i in range ( 1 , m ) :
for j in range ( n ) :
count [ i ] [ j ] = count [... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.