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-SUM_SQUARES_BINOMIAL_COEFFICIENTS
python-cpp
def sumofsquare ( n ) : C = [ [ 0 for i in range ( n + 1 ) ] for j 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 -...
int sumofsquare ( 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 (...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumofsquare ( n ) : C = [ [ 0 for i in range ( n + 1 ) ] for j 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 ...
python-cpp-CONSTRUCT_ARRAY_PAIR_SUM_ARRAY
python-cpp
def constructArr ( arr , pair , n ) : arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) // 2 for i in range ( 1 , n ) : arr [ i ] = pair [ i - 1 ] - arr [ 0 ]
void constructArr ( int arr [ ] , int pair [ ] , int n ) { arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) / 2 ; for ( int i = 1 ; i < n ; i ++ ) arr [ i ] = pair [ i - 1 ] - arr [ 0 ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def constructArr ( arr , pair , n ) : arr [ 0 ] = ( pair [ 0 ] + pair [ 1 ] - pair [ n - 1 ] ) // 2 for i in range ( 1 , n ) : arr [ i ] = pair [ i - 1 ] - arr [ 0 ] </Code>
python-cpp-FIND_PATTERNS_101_GIVEN_STRING
python-cpp
def patternCount ( str ) : last = str [ 0 ] i = 1 counter = 0 while ( i < len ( str ) ) : if ( str [ i ] == '0' and last == '1' ) : while ( str [ i ] == '0' ) : i += 1 if ( str [ i ] == '1' ) : counter += 1 last = str [ i ] ...
int patternCount ( string str ) { char last = str [ 0 ] ; int i = 1 , counter = 0 ; while ( i < str . size ( ) ) { if ( str [ i ] == '0' && last == '1' ) { while ( str [ i ] == '0' ) i ++ ; if ( str [ i ] == '1' ) counter ++ ; } last = str [ i ] ; i ++ ; } return counter ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def patternCount ( str ) : last = str [ 0 ] i = 1 counter = 0 while ( i < len ( str ) ) : if ( str [ i ] == '0' and last == '1' ) : while ( str [ i ] == '0' ) : i += 1 if ( str [ i ] == '1' ) : counter += 1 last = str [ i...
python-cpp-RANGE_QUERIES_FOR_FREQUENCIES_OF_ARRAY_ELEMENTS
python-cpp
def findFrequency ( arr , n , left , right , element ) : count = 0 for i in range ( left - 1 , right ) : if ( arr [ i ] == element ) : count += 1 return count
int findFrequency ( int arr [ ] , int n , int left , int right , int element ) { int count = 0 ; for ( int i = left - 1 ; i <= right ; ++ i ) if ( arr [ i ] == element ) ++ count ; return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findFrequency ( arr , n , left , right , element ) : count = 0 for i in range ( left - 1 , right ) : if ( arr [ i ] == element ) : count += 1 return count </Code>
python-cpp-FINDING_VERTEX_FOCUS_DIRECTRIX_PARABOLA
python-cpp
def parabola ( a , b , c ) : print ( "Vertex: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) , ")" , sep = "" ) print ( "Focus: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) , ")" , sep = "" ) print ( "Directrix: y=" , c - ( ( b * b ) + ...
void parabola ( float a , float b , float c ) { cout << "Vertex: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) << ")" << endl ; cout << "Focus: (" << ( - b / ( 2 * a ) ) << ", " << ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) << ")" << endl ; cout << "Directrix: y=" << c ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def parabola ( a , b , c ) : print ( "Vertex: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) , ")" , sep = "" ) print ( "Focus: (" , ( - b / ( 2 * a ) ) , ", " , ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) , ")" , sep = "" ) print ( "Directrix: y=" , c - ( ( b *...
python-cpp-MINIMUM_SUM_PRODUCT_TWO_ARRAYS
python-cpp
def minproduct ( a , b , n , k ) : diff = 0 res = 0 for i in range ( n ) : pro = a [ i ] * b [ i ] res = res + pro if ( pro < 0 and b [ i ] < 0 ) : temp = ( a [ i ] + 2 * k ) * b [ i ] elif ( pro < 0 and a [ i ] < 0 ) : temp = ( a [ i ] - 2 * k ) * b [...
int minproduct ( int a [ ] , int b [ ] , int n , int k ) { int diff = 0 , res = 0 ; int temp ; for ( int i = 0 ; i < n ; i ++ ) { int pro = a [ i ] * b [ i ] ; res = res + pro ; if ( pro < 0 && b [ i ] < 0 ) temp = ( a [ i ] + 2 * k ) * b [ i ] ; else if ( pro < 0 && a [ i ] < 0 ) temp = ( a [...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minproduct ( a , b , n , k ) : diff = 0 res = 0 for i in range ( n ) : pro = a [ i ] * b [ i ] res = res + pro if ( pro < 0 and b [ i ] < 0 ) : temp = ( a [ i ] + 2 * k ) * b [ i ] elif ( pro < 0 and a [ i ] < 0 ) : temp = ( a [ i ] - 2 * k ...
python-cpp-LONGEST_SUBSEQUENCE_WHERE_EVERY_CHARACTER_APPEARS_AT_LEAST_K_TIMES
python-cpp
def longestSubseqWithK ( str , k ) : n = len ( str ) freq = [ 0 ] * MAX_CHARS for i in range ( n ) : freq [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 for i in range ( n ) : if ( freq [ ord ( str [ i ] ) - ord ( 'a' ) ] >= k ) : print ( str [ i ] , end = "" )
void longestSubseqWithK ( string str , int k ) { int n = str . size ( ) ; int freq [ MAX_CHARS ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) freq [ str [ i ] - 'a' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( freq [ str [ i ] - 'a' ] >= k ) cout << str [ i ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def longestSubseqWithK ( str , k ) : n = len ( str ) freq = [ 0 ] * MAX_CHARS for i in range ( n ) : freq [ ord ( str [ i ] ) - ord ( 'a' ) ] += 1 for i in range ( n ) : if ( freq [ ord ( str [ i ] ) - ord ( 'a' ) ] >= k ) : print ( str [ i ] , end = "" ) </Code>
python-cpp-GIVEN_A_SORTED_AND_ROTATED_ARRAY_FIND_IF_THERE_IS_A_PAIR_WITH_A_GIVEN_SUM_1
python-cpp
def pairsInSortedRotated ( arr , n , x ) : for i in range ( n ) : if arr [ i ] > arr [ i + 1 ] : break l = ( i + 1 ) % n r = i cnt = 0 while ( l != r ) : if arr [ l ] + arr [ r ] == x : cnt += 1 if l == ( r - 1 + n ) % n : return cn...
int pairsInSortedRotated ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; int cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def pairsInSortedRotated ( arr , n , x ) : for i in range ( n ) : if arr [ i ] > arr [ i + 1 ] : break l = ( i + 1 ) % n r = i cnt = 0 while ( l != r ) : if arr [ l ] + arr [ r ] == x : cnt += 1 if l == ( r - 1 + n ) % n : re...
python-cpp-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_2
python-cpp
def middleOfThree ( a , b , c ) : x = a - b y = b - c z = a - c if x * y > 0 : return b elif ( x * z > 0 ) : return else : return a
int middleOfThree ( int a , int b , int c ) { int x = a - b ; int y = b - c ; int z = a - c ; if ( x * y > 0 ) return b ; else if ( x * z > 0 ) return c ; else return a ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def middleOfThree ( a , b , c ) : x = a - b y = b - c z = a - c if x * y > 0 : return b elif ( x * z > 0 ) : return else : return a </Code>
python-cpp-NEXT_POWER_OF_2
python-cpp
def nextPowerOf2 ( n ) : count = 0 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( n != 0 ) : n >>= 1 count += 1 return 1 << count
unsigned int nextPowerOf2 ( unsigned int n ) { unsigned count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) return n ; while ( n != 0 ) { n >>= 1 ; count += 1 ; } return 1 << count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextPowerOf2 ( n ) : count = 0 if ( n and not ( n & ( n - 1 ) ) ) : return n while ( n != 0 ) : n >>= 1 count += 1 return 1 << count </Code>
python-cpp-MAXIMUM_SUBARRAY_SUM_ARRAY_CREATED_REPEATED_CONCATENATION
python-cpp
def maxSubArraySumRepeated ( a , n , k ) : max_so_far = - 2147483648 max_ending_here = 0 for i in range ( n * k ) : max_ending_here = max_ending_here + a [ i % n ] if ( max_so_far < max_ending_here ) : max_so_far = max_ending_here if ( max_ending_here < 0 ) : ...
int maxSubArraySumRepeated ( int a [ ] , int n , int k ) { int max_so_far = INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < n * k ; i ++ ) { max_ending_here = max_ending_here + a [ i % n ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_endin...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSubArraySumRepeated ( a , n , k ) : max_so_far = - 2147483648 max_ending_here = 0 for i in range ( n * k ) : max_ending_here = max_ending_here + a [ i % n ] if ( max_so_far < max_ending_here ) : max_so_far = max_ending_here if ( max_ending_here < 0 ) : ...
python-cpp-STACK_SET_3_REVERSE_STRING_USING_STACK
python-cpp
def reverse ( string ) : string = string [ : : - 1 ] return string
void reverse ( char str [ ] ) { int n = strlen ( str ) , i ; for ( i = 0 ; i < n / 2 ; i ++ ) swap ( & str [ i ] , & str [ n - i - 1 ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def reverse ( string ) : string = string [ : : - 1 ] return string </Code>
python-cpp-LEXICOGRAPHICALLY_NEXT_STRING
python-cpp
def nextWord ( s ) : if ( s == " " ) : return "a" i = len ( s ) - 1 while ( s [ i ] == 'z' and i >= 0 ) : i -= 1 if ( i == - 1 ) : s = s + 'a' else : s = s.replace ( s [ i ] , chr ( ord ( s [ i ] ) + 1 ) , 1 ) return s
string nextWord ( string s ) { if ( s == "" ) return "a" ; int i = s . length ( ) - 1 ; while ( s [ i ] == 'z' && i >= 0 ) i -- ; if ( i == - 1 ) s = s + 'a' ; else s [ i ] ++ ; return s ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextWord ( s ) : if ( s == " " ) : return "a" i = len ( s ) - 1 while ( s [ i ] == 'z' and i >= 0 ) : i -= 1 if ( i == - 1 ) : s = s + 'a' else : s = s.replace ( s [ i ] , chr ( ord ( s [ i ] ) + 1 ) , 1 ) return s </Code>
python-cpp-SUM_BINOMIAL_COEFFICIENTS
python-cpp
def binomialCoeffSum ( n ) : C = [ [ 0 ] * ( n + 2 ) for i in range ( 0 , n + 2 ) ] 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 ] +...
int binomialCoeffSum ( int n ) { int C [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int sum = 0 ; for ( ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binomialCoeffSum ( n ) : C = [ [ 0 ] * ( n + 2 ) for i in range ( 0 , n + 2 ) ] 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 ...
python-cpp-CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD
python-cpp
def isEven ( n ) : return ( n % 2 == 0 )
bool isEven ( int n ) { return ( n % 2 == 0 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isEven ( n ) : return ( n % 2 == 0 ) </Code>
python-cpp-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY
python-cpp
def binarySearch ( arr , low , high , key ) : if ( high < low ) : return - 1 mid = ( low + high ) / 2 if ( key == arr [ int ( mid ) ] ) : return mid if ( key > arr [ int ( mid ) ] ) : return binarySearch ( arr , ( mid + 1 ) , high , key ) return ( binarySearch ( arr , low , (...
int binarySearch ( int arr [ ] , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def binarySearch ( arr , low , high , key ) : if ( high < low ) : return - 1 mid = ( low + high ) / 2 if ( key == arr [ int ( mid ) ] ) : return mid if ( key > arr [ int ( mid ) ] ) : return binarySearch ( arr , ( mid + 1 ) , high , key ) return ( binarySearch ( arr , ...
python-cpp-MAXIMUM_AND_MINIMUM_IN_A_SQUARE_MATRIX
python-cpp
def MAXMIN ( arr , n ) : MIN = 10 ** 9 MAX = - 10 ** 9 for i in range ( n ) : for j in range ( n // 2 + 1 ) : if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) : if ( MIN > arr [ i ] [ n - j - 1 ] ) : MIN = arr [ i ] [ n - j - 1 ] if ( M...
void maxMin ( int arr [ ] [ MAX ] , int n ) { int min = INT_MAX ; int max = INT_MIN ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= n / 2 ; j ++ ) { if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) { if ( min > arr [ i ] [ n - j - 1 ] ) min = arr [ i ] [ n - j - 1 ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def MAXMIN ( arr , n ) : MIN = 10 ** 9 MAX = - 10 ** 9 for i in range ( n ) : for j in range ( n // 2 + 1 ) : if ( arr [ i ] [ j ] > arr [ i ] [ n - j - 1 ] ) : if ( MIN > arr [ i ] [ n - j - 1 ] ) : MIN = arr [ i ] [ n - j - 1 ] ...
python-cpp-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO_1
python-cpp
def isPowerOfTwo ( x ) : return ( x and ( not ( x & ( x - 1 ) ) ) )
int bool isPowerOfTwo ( int x ) { return x && ( ! ( x & ( x - 1 ) ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPowerOfTwo ( x ) : return ( x and ( not ( x & ( x - 1 ) ) ) ) </Code>
python-cpp-COUNTS_PATHS_POINT_REACH_ORIGIN
python-cpp
def countPaths ( n , m ) : if ( n == 0 or m == 0 ) : return 1 return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) )
int countPaths ( int n , int m ) { if ( n == 0 || m == 0 ) return 1 ; return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPaths ( n , m ) : if ( n == 0 or m == 0 ) : return 1 return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) </Code>
python-cpp-FIND_RECTANGLE_BINARY_MATRIX_CORNERS_1_1
python-cpp
def isRectangle ( matrix ) : rows = len ( matrix ) if ( rows == 0 ) : return False columns = len ( matrix [ 0 ] ) table = { } for i in range ( rows ) : for j in range ( columns - 1 ) : for k in range ( j + 1 , columns ) : if ( matrix [ i ] [ j ] == 1 and m...
bool isRectangle ( const vector < vector < int >> & matrix ) { int rows = matrix . size ( ) ; if ( rows == 0 ) return false ; int columns = matrix [ 0 ] . size ( ) ; unordered_map < int , unordered_set < int >> table ; for ( int i = 0 ; i < rows ; ++ i ) { for ( int j = 0 ; j < columns - 1 ; +...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isRectangle ( matrix ) : rows = len ( matrix ) if ( rows == 0 ) : return False columns = len ( matrix [ 0 ] ) table = { } for i in range ( rows ) : for j in range ( columns - 1 ) : for k in range ( j + 1 , columns ) : if ( matrix [ i ] [ j ] == ...
python-cpp-PROGRAM_DECIMAL_BINARY_CONVERSION
python-cpp
def decToBinary ( n ) : binaryNum = [ 0 ] * n i = 0 while ( n > 0 ) : binaryNum [ i ] = n % 2 n = int ( n / 2 ) i += 1 for j in range ( i - 1 , - 1 , - 1 ) : print ( binaryNum [ j ] , end = "" )
void decToBinary ( int n ) { int binaryNum [ 32 ] ; int i = 0 ; while ( n > 0 ) { binaryNum [ i ] = n % 2 ; n = n / 2 ; i ++ ; } for ( int j = i - 1 ; j >= 0 ; j -- ) cout << binaryNum [ j ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def decToBinary ( n ) : binaryNum = [ 0 ] * n i = 0 while ( n > 0 ) : binaryNum [ i ] = n % 2 n = int ( n / 2 ) i += 1 for j in range ( i - 1 , - 1 , - 1 ) : print ( binaryNum [ j ] , end = "" ) </Code>
python-cpp-FRIENDS_PAIRING_PROBLEM_1
python-cpp
def countFriendsPairings ( n ) : dp = [ - 1 ] * 100 if ( dp [ n ] != - 1 ) : return dp [ n ] if ( n > 2 ) : dp [ n ] = ( countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ) return dp [ n ] else : dp [ n ] = n return dp [ n ]
int countFriendsPairings ( int n ) { if ( dp [ n ] != - 1 ) return dp [ n ] ; if ( n > 2 ) return dp [ n ] = countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ; else return dp [ n ] = n ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countFriendsPairings ( n ) : dp = [ - 1 ] * 100 if ( dp [ n ] != - 1 ) : return dp [ n ] if ( n > 2 ) : dp [ n ] = ( countFriendsPairings ( n - 1 ) + ( n - 1 ) * countFriendsPairings ( n - 2 ) ) return dp [ n ] else : dp [ n ] = n return dp [ n ] </Cod...
python-cpp-MAXIMUM_SUBARRAY_SUM_USING_PREFIX_SUM
python-cpp
def maximumSumSubarray ( arr , n ) : min_prefix_sum = 0 res = - math.inf prefix_sum = [ ] prefix_sum.append ( arr [ 0 ] ) for i in range ( 1 , n ) : prefix_sum.append ( prefix_sum [ i - 1 ] + arr [ i ] ) for i in range ( n ) : res = max ( res , prefix_sum [ i ] - min_prefix_sum )...
int maximumSumSubarray ( int arr [ ] , int n ) { int min_prefix_sum = 0 ; int res = numeric_limits < int > :: min ( ) ; int prefix_sum [ n ] ; prefix_sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix_sum [ i ] = prefix_sum [ i - 1 ] + arr [ i ] ; 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 maximumSumSubarray ( arr , n ) : min_prefix_sum = 0 res = - math.inf prefix_sum = [ ] prefix_sum.append ( arr [ 0 ] ) for i in range ( 1 , n ) : prefix_sum.append ( prefix_sum [ i - 1 ] + arr [ i ] ) for i in range ( n ) : res = max ( res , prefix_sum [ i ] - min_prefi...
python-cpp-NUMBER_SUBSEQUENCES_AB_STRING_REPEATED_K_TIMES
python-cpp
def countOccurrences ( s , K ) : n = len ( s ) c1 = 0 c2 = 0 C = 0 for i in range ( n ) : if s [ i ] == 'a' : c1 += 1 if s [ i ] == 'b' : c2 += 1 C += c1 return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2
int countOccurrences ( string s , int K ) { int n = s . length ( ) ; int C , c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] == 'a' ) c1 ++ ; if ( s [ i ] == 'b' ) { c2 ++ ; C += c1 ; } } return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countOccurrences ( s , K ) : n = len ( s ) c1 = 0 c2 = 0 C = 0 for i in range ( n ) : if s [ i ] == 'a' : c1 += 1 if s [ i ] == 'b' : c2 += 1 C += c1 return C * K + ( K * ( K - 1 ) / 2 ) * c1 * c2 </Code>
python-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_13_NOT
python-cpp
def checkDivisibility ( num ) : length = len ( num ) if ( length == 1 and num [ 0 ] == '0' ) : return True if ( length % 3 == 1 ) : num = str ( num ) + "00" length += 2 elif ( length % 3 == 2 ) : num = str ( num ) + "0" length += 1 sum = 0 p = 1 for i ...
bool checkDivisibility ( string num ) { int length = num . size ( ) ; if ( length == 1 && num [ 0 ] == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkDivisibility ( num ) : length = len ( num ) if ( length == 1 and num [ 0 ] == '0' ) : return True if ( length % 3 == 1 ) : num = str ( num ) + "00" length += 2 elif ( length % 3 == 2 ) : num = str ( num ) + "0" length += 1 sum = 0 p = 1 ...
python-cpp-NUMBER_UNIQUE_RECTANGLES_FORMED_USING_N_UNIT_SQUARES
python-cpp
def countRect ( n ) : ans = 0 for length in range ( 1 , int ( math.sqrt ( n ) ) + 1 ) : height = length while ( height * length <= n ) : ans += 1 height += 1 return ans
int countRect ( int n ) { int ans = 0 ; for ( int length = 1 ; length <= sqrt ( n ) ; ++ length ) for ( int height = length ; height * length <= n ; ++ height ) ans ++ ; return ans ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countRect ( n ) : ans = 0 for length in range ( 1 , int ( math.sqrt ( n ) ) + 1 ) : height = length while ( height * length <= n ) : ans += 1 height += 1 return ans </Code>
python-cpp-COUNT_SET_BITS_IN_AN_INTEGER_2
python-cpp
def countSetBits ( n ) : count = 0 while ( n ) : n &= ( n - 1 ) count += 1 return count
public : unsigned int countSetBits ( int n ) { unsigned int count = 0 ; while ( n ) { n &= ( n - 1 ) ; count ++ ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countSetBits ( n ) : count = 0 while ( n ) : n &= ( n - 1 ) count += 1 return count </Code>
python-cpp-LONGEST_SUBARRAY_COUNT_1S_ONE_COUNT_0S
python-cpp
def lenOfLongSubarr ( arr , n ) : um = { i : 0 for i in range ( 10 ) } sum = 0 maxLen = 0 for i in range ( n ) : if arr [ i ] == 0 : sum += - 1 else : sum += 1 if ( sum == 1 ) : maxLen = i + 1 elif ( sum not in um ) : um [ s...
int lenOfLongSubarr ( int arr [ ] , int n ) { unordered_map < int , int > um ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] == 0 ? - 1 : 1 ; if ( sum == 1 ) maxLen = i + 1 ; else if ( um . find ( sum ) == um . end ( ) ) um [ sum ] = i ; if ( um . find ( sum - ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def lenOfLongSubarr ( arr , n ) : um = { i : 0 for i in range ( 10 ) } sum = 0 maxLen = 0 for i in range ( n ) : if arr [ i ] == 0 : sum += - 1 else : sum += 1 if ( sum == 1 ) : maxLen = i + 1 elif ( sum not in um ) : ...
python-cpp-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER_1
python-cpp
def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
unsigned int factorial ( unsigned int n ) { int res = 1 , i ; for ( i = 2 ; i <= n ; i ++ ) res *= i ; return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 ) </Code>
python-cpp-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY
python-cpp
def minJumps ( arr , l , h ) : if ( h == l ) : return 0 if ( arr [ l ] == 0 ) : return float ( 'inf' ) min = float ( 'inf' ) for i in range ( l + 1 , h + 1 ) : if ( i < l + arr [ l ] + 1 ) : jumps = minJumps ( arr , i , h ) if ( jumps != float ( 'inf' ) an...
int minJumps ( int arr [ ] , int n ) { if ( n == 1 ) return 0 ; int res = INT_MAX ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( i + arr [ i ] >= n - 1 ) { int sub_res = minJumps ( arr , i + 1 ) ; if ( sub_res != INT_MAX ) res = min ( res , sub_res + 1 ) ; } } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minJumps ( arr , l , h ) : if ( h == l ) : return 0 if ( arr [ l ] == 0 ) : return float ( 'inf' ) min = float ( 'inf' ) for i in range ( l + 1 , h + 1 ) : if ( i < l + arr [ l ] + 1 ) : jumps = minJumps ( arr , i , h ) if ( jumps != float ( 'in...
python-cpp-MINIMUM_TIME_WRITE_CHARACTERS_USING_INSERT_DELETE_COPY_OPERATION
python-cpp
def minTimeForWritingChars ( N , insrt , remov , cpy ) : if N == 0 : return 0 if N == 1 : return insrt dp = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 ) : if i % 2 == 0 : dp [ i ] = min ( dp [ i - 1 ] + insrt , dp [ i // 2 ] + cpy ) else : dp [ i...
int minTimeForWritingChars ( int N , int insert , int remove , int copy ) { if ( N == 0 ) return 0 ; if ( N == 1 ) return insert ; int dp [ N + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 1 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) dp [ i ] = min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minTimeForWritingChars ( N , insrt , remov , cpy ) : if N == 0 : return 0 if N == 1 : return insrt dp = [ 0 ] * ( N + 1 ) for i in range ( 1 , N + 1 ) : if i % 2 == 0 : dp [ i ] = min ( dp [ i - 1 ] + insrt , dp [ i // 2 ] + cpy ) else : ...
python-cpp-PRINTING_STRING_PLUS_PATTERN_MATRIX
python-cpp
def carveCross ( str ) : n = len ( str ) if ( n % 2 == 0 ) : print ( "Not possible.Please enter " , "odd length string.\n" ) else : arr = [ [ False for x in range ( max ) ] for y in range ( max ) ] m = n // 2 for i in range ( n ) : for j in range ( n ) : ...
void carveCross ( string str ) { int n = str . length ( ) ; if ( n % 2 == 0 ) { cout << "Not possible. Please enter " << "odd length string.\n" ; } else { char arr [ max ] [ max ] ; int m = n / 2 ; for ( int i = 0 ; i < n ; i ++ ) { 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 carveCross ( str ) : n = len ( str ) if ( n % 2 == 0 ) : print ( "Not possible.Please enter " , "odd length string.\n" ) else : arr = [ [ False for x in range ( max ) ] for y in range ( max ) ] m = n // 2 for i in range ( n ) : for j in range ( n ) : ...
python-cpp-SUBARRAYS_DISTINCT_ELEMENTS
python-cpp
def sumoflength ( arr , n ) : s = [ ] j = 0 ans = 0 for i in range ( n ) : while ( j < n and ( arr [ j ] not in s ) ) : s.append ( arr [ j ] ) j += 1 ans += ( ( j - i ) * ( j - i + 1 ) ) // 2 s.remove ( arr [ i ] ) return ans
int sumoflength ( int arr [ ] , int n ) { unordered_set < int > s ; int j = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( j < n && s . find ( arr [ j ] ) == s . end ( ) ) { s . insert ( arr [ j ] ) ; j ++ ; } ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ; s . erase ( arr [ i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumoflength ( arr , n ) : s = [ ] j = 0 ans = 0 for i in range ( n ) : while ( j < n and ( arr [ j ] not in s ) ) : s.append ( arr [ j ] ) j += 1 ans += ( ( j - i ) * ( j - i + 1 ) ) // 2 s.remove ( arr [ i ] ) return ans </Code>
python-cpp-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_1
python-cpp
def findTriplets ( arr , n ) : found = False for i in range ( n - 1 ) : s = set ( ) for j in range ( i + 1 , n ) : x = - ( arr [ i ] + arr [ j ] ) if x in s : print ( x , arr [ i ] , arr [ j ] ) found = True else : ...
void findTriplets ( int arr [ ] , int n ) { bool found = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { unordered_set < int > s ; for ( int j = i + 1 ; j < n ; j ++ ) { int x = - ( arr [ i ] + arr [ j ] ) ; if ( s . find ( x ) != s . end ( ) ) { printf ( "%d %d %d\n" , x , ar...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findTriplets ( arr , n ) : found = False for i in range ( n - 1 ) : s = set ( ) for j in range ( i + 1 , n ) : x = - ( arr [ i ] + arr [ j ] ) if x in s : print ( x , arr [ i ] , arr [ j ] ) found = True else : ...
python-cpp-MAXIMUM_LENGTH_PREFIX_ONE_STRING_OCCURS_SUBSEQUENCE_ANOTHER
python-cpp
def maxPrefix ( s , t ) : count = 0 for i in range ( 0 , len ( t ) ) : if ( count == len ( s ) ) : break if ( t [ i ] == s [ count ] ) : count = count + 1 return count
int maxPrefix ( char s [ ] , char t [ ] ) { int count = 0 ; for ( int i = 0 ; i < strlen ( t ) ; i ++ ) { if ( count == strlen ( s ) ) break ; if ( t [ i ] == s [ count ] ) count ++ ; } return count ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxPrefix ( s , t ) : count = 0 for i in range ( 0 , len ( t ) ) : if ( count == len ( s ) ) : break if ( t [ i ] == s [ count ] ) : count = count + 1 return count </Code>
python-cpp-MOVE_VE_ELEMENTS_END_ORDER_EXTRA_SPACE_ALLOWED
python-cpp
def segregateElements ( arr , n ) : temp = [ 0 for k in range ( n ) ] j = 0 for i in range ( n ) : if ( arr [ i ] >= 0 ) : temp [ j ] = arr [ i ] j += 1 if ( j == n or j == 0 ) : return for i in range ( n ) : if ( arr [ i ] < 0 ) : temp [ j...
void segregateElements ( int arr [ ] , int n ) { int temp [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] >= 0 ) temp [ j ++ ] = arr [ i ] ; if ( j == n || j == 0 ) return ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < 0 ) temp [ j ++ ] = arr [ i ] ; memcpy ( arr , temp , si...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def segregateElements ( arr , n ) : temp = [ 0 for k in range ( n ) ] j = 0 for i in range ( n ) : if ( arr [ i ] >= 0 ) : temp [ j ] = arr [ i ] j += 1 if ( j == n or j == 0 ) : return for i in range ( n ) : if ( arr [ i ] < 0 ) : t...
python-cpp-MERGING_INTERVALS
python-cpp
def mergeIntervals ( arr ) : arr.sort ( key = lambda x : x [ 0 ] ) m = [ ] s = - 10000 max = - 100000 for i in range ( len ( arr ) ) : a = arr [ i ] if a [ 0 ] > max : if i != 0 : m.append ( [ s , max ] ) max = a [ 1 ] s = a [ 0 ] ...
void mergeIntervals ( Interval arr [ ] , int n ) { sort ( arr , arr + n , mycomp ) ; int index = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ index ] . e >= arr [ i ] . s ) { arr [ index ] . e = max ( arr [ index ] . e , arr [ i ] . e ) ; arr [ index ] . s = min ( arr [ index ] . s , arr ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def mergeIntervals ( arr ) : arr.sort ( key = lambda x : x [ 0 ] ) m = [ ] s = - 10000 max = - 100000 for i in range ( len ( arr ) ) : a = arr [ i ] if a [ 0 ] > max : if i != 0 : m.append ( [ s , max ] ) max = a [ 1 ] s = a ...
python-cpp-PROGRAM_FIND_CORRELATION_COEFFICIENT
python-cpp
def correlationCoefficient ( X , Y , n ) : sum_X = 0 sum_Y = 0 sum_XY = 0 squareSum_X = 0 squareSum_Y = 0 i = 0 while i < n : sum_X = sum_X + X [ i ] sum_Y = sum_Y + Y [ i ] sum_XY = sum_XY + X [ i ] * Y [ i ] squareSum_X = squareSum_X + X [ i ] * X [ i ] ...
float correlationCoefficient ( int X [ ] , int Y [ ] , int n ) { int sum_X = 0 , sum_Y = 0 , sum_XY = 0 ; int squareSum_X = 0 , squareSum_Y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum_X = sum_X + X [ i ] ; sum_Y = sum_Y + Y [ i ] ; sum_XY = sum_XY + X [ i ] * Y [ i ] ; squareSum_X = squareSum...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def correlationCoefficient ( X , Y , n ) : sum_X = 0 sum_Y = 0 sum_XY = 0 squareSum_X = 0 squareSum_Y = 0 i = 0 while i < n : sum_X = sum_X + X [ i ] sum_Y = sum_Y + Y [ i ] sum_XY = sum_XY + X [ i ] * Y [ i ] squareSum_X = squareSum_X + X [ i ] * X [ i...
python-cpp-MAXIMUM_AREA_RECTANGLE_PICKING_FOUR_SIDES_ARRAY
python-cpp
def findArea ( arr , n ) : arr.sort ( reverse = True ) dimension = [ 0 , 0 ] i = 0 j = 0 while ( i < n - 1 and j < 2 ) : if ( arr [ i ] == arr [ i + 1 ] ) : dimension [ j ] = arr [ i ] j += 1 i += 1 i += 1 return ( dimension [ 0 ] * dimension [...
int findArea ( int arr [ ] , int n ) { sort ( arr , arr + n , greater < int > ( ) ) ; int dimension [ 2 ] = { 0 , 0 } ; for ( int i = 0 , j = 0 ; i < n - 1 && j < 2 ; i ++ ) if ( arr [ i ] == arr [ i + 1 ] ) dimension [ j ++ ] = arr [ i ++ ] ; return ( dimension [ 0 ] * dimension [ 1 ] ) ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findArea ( arr , n ) : arr.sort ( reverse = True ) dimension = [ 0 , 0 ] i = 0 j = 0 while ( i < n - 1 and j < 2 ) : if ( arr [ i ] == arr [ i + 1 ] ) : dimension [ j ] = arr [ i ] j += 1 i += 1 i += 1 return ( dimension [ 0 ] * dime...
python-cpp-MINIMUM_INCREMENT_K_OPERATIONS_MAKE_ELEMENTS_EQUAL
python-cpp
def minOps ( arr , n , k ) : max1 = max ( arr ) res = 0 for i in range ( 0 , n ) : if ( ( max1 - arr [ i ] ) % k != 0 ) : return - 1 else : res += ( max1 - arr [ i ] ) / k return int ( res )
int minOps ( int arr [ ] , int n , int k ) { int max = * max_element ( arr , arr + n ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( max - arr [ i ] ) % k != 0 ) return - 1 ; else res += ( max - arr [ i ] ) / k ; } return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minOps ( arr , n , k ) : max1 = max ( arr ) res = 0 for i in range ( 0 , n ) : if ( ( max1 - arr [ i ] ) % k != 0 ) : return - 1 else : res += ( max1 - arr [ i ] ) / k return int ( res ) </Code>
python-cpp-SQUARE_ROOT_OF_AN_INTEGER
python-cpp
def floorSqrt ( x ) : if ( x == 0 or x == 1 ) : return x i = 1 result = 1 while ( result <= x ) : i += 1 result = i * i return i - 1
int floorSqrt ( int x ) { if ( x == 0 || x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def floorSqrt ( x ) : if ( x == 0 or x == 1 ) : return x i = 1 result = 1 while ( result <= x ) : i += 1 result = i * i return i - 1 </Code>
python-cpp-CHECK_WHETHER_LARGE_NUMBER_DIVISIBLE_7
python-cpp
def isdivisible7 ( num ) : n = len ( num ) if ( n == 0 and num [ 0 ] == '\n' ) : return 1 if ( n % 3 == 1 ) : num = str ( num ) + "00" n += 2 elif ( n % 3 == 2 ) : num = str ( num ) + "0" n += 1 GSum = 0 p = 1 for i in range ( n - 1 , - 1 , - 1 ) : ...
int isdivisible7 ( char num [ ] ) { int n = strlen ( num ) , gSum ; if ( n == 0 && num [ 0 ] == '\n' ) return 1 ; if ( n % 3 == 1 ) { strcat ( num , "00" ) ; n += 2 ; } else if ( n % 3 == 2 ) { strcat ( num , "0" ) ; n ++ ; } int i , GSum = 0 , p = 1 ; for ( i = n - 1 ; i >= 0 ; i --...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isdivisible7 ( num ) : n = len ( num ) if ( n == 0 and num [ 0 ] == '\n' ) : return 1 if ( n % 3 == 1 ) : num = str ( num ) + "00" n += 2 elif ( n % 3 == 2 ) : num = str ( num ) + "0" n += 1 GSum = 0 p = 1 for i in range ( n - 1 , - 1 , - 1 ...
python-cpp-CONSTRUCT_GRAPH_GIVEN_DEGREES_VERTICES
python-cpp
def printMat ( degseq , n ) : mat = [ [ 0 ] * n for i in range ( n ) ] for i in range ( n ) : for j in range ( i + 1 , n ) : if ( degseq [ i ] > 0 and degseq [ j ] > 0 ) : degseq [ i ] -= 1 degseq [ j ] -= 1 mat [ i ] [ j ] = 1 ...
void printMat ( int degseq [ ] , int n ) { int mat [ n ] [ n ] ; memset ( mat , 0 , sizeof ( mat ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( degseq [ i ] > 0 && degseq [ j ] > 0 ) { degseq [ i ] -- ; degseq [ j ] -- ; mat [ i ]...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printMat ( degseq , n ) : mat = [ [ 0 ] * n for i in range ( n ) ] for i in range ( n ) : for j in range ( i + 1 , n ) : if ( degseq [ i ] > 0 and degseq [ j ] > 0 ) : degseq [ i ] -= 1 degseq [ j ] -= 1 mat [ i ] [ j ] = 1 ...
python-cpp-MINIMUM_TIME_REQUIRED_PRODUCE_M_ITEMS
python-cpp
def minTime ( arr , n , m ) : t = 0 while ( 1 ) : items = 0 for i in range ( n ) : items += ( t // arr [ i ] ) if ( items >= m ) : return t t += 1
int minTime ( int arr [ ] , int n , int m ) { int t = 0 ; while ( 1 ) { int items = 0 ; for ( int i = 0 ; i < n ; i ++ ) items += ( t / arr [ i ] ) ; if ( items >= m ) return t ; t ++ ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minTime ( arr , n , m ) : t = 0 while ( 1 ) : items = 0 for i in range ( n ) : items += ( t // arr [ i ] ) if ( items >= m ) : return t t += 1 </Code>
python-cpp-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX_1
python-cpp
def MaximumDecimalValue ( mat , n ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] if ( mat [ 0 ] [ 0 ] == 1 ) : dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n ) : if ( mat [ 0 ] [ i ] == 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 2 ** i else : dp [ 0 ...
long long int MaximumDecimalValue ( int mat [ ] [ N ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; if ( mat [ 0 ] [ 0 ] == 1 ) dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( mat [ 0 ] [ i ] == 1 ) dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + pow ( 2 , i ) ; else dp [ 0...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def MaximumDecimalValue ( mat , n ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] if ( mat [ 0 ] [ 0 ] == 1 ) : dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n ) : if ( mat [ 0 ] [ i ] == 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + 2 ** i else : ...
python-cpp-CHECK_DIVISIBILITY_LARGE_NUMBER_999
python-cpp
def isDivisible999 ( num ) : n = len ( num ) ; if ( n == 0 or num [ 0 ] == '0' ) : return true if ( ( n % 3 ) == 1 ) : num = "00" + num if ( ( n % 3 ) == 2 ) : num = "0" + num gSum = 0 for i in range ( 0 , n , 3 ) : group = 0 group += ( ord ( num [ i ] ) -...
bool isDivisible999 ( string num ) { int n = num . length ( ) ; if ( n == 0 && num [ 0 ] == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num [ i ++ ] - '0' ) * 100 ; g...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isDivisible999 ( num ) : n = len ( num ) ; if ( n == 0 or num [ 0 ] == '0' ) : return true if ( ( n % 3 ) == 1 ) : num = "00" + num if ( ( n % 3 ) == 2 ) : num = "0" + num gSum = 0 for i in range ( 0 , n , 3 ) : group = 0 group += ( ord ( num [ ...
python-cpp-MAXIMUM_SUM_BITONIC_SUBARRAY
python-cpp
def maxSumBitonicSubArr ( arr , n ) : msis = [ None ] * n msds = [ None ] * n max_sum = 0 msis [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : if ( arr [ i ] > arr [ i - 1 ] ) : msis [ i ] = msis [ i - 1 ] + arr [ i ] else : msis [ i ] = arr [ i ] msds [ n - ...
int maxSumBitonicSubArr ( int arr [ ] , int n ) { int msis [ n ] , msds [ n ] ; int max_sum = INT_MIN ; msis [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) msis [ i ] = msis [ i - 1 ] + arr [ i ] ; else msis [ i ] = arr [ i ] ; msds [ n - 1 ] = arr [ n - 1 ] ; fo...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxSumBitonicSubArr ( arr , n ) : msis = [ None ] * n msds = [ None ] * n max_sum = 0 msis [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : if ( arr [ i ] > arr [ i - 1 ] ) : msis [ i ] = msis [ i - 1 ] + arr [ i ] else : msis [ i ] = arr [ i ] msds...
python-cpp-FIND_MINIMUM_NUMBER_DIVIDED_MAKE_NUMBER_PERFECT_SQUARE
python-cpp
def findMinNumber ( n ) : count = 0 ans = 1 while n % 2 == 0 : count += 1 n //= 2 if count % 2 is not 0 : ans *= 2 for i in range ( 3 , ( int ) ( math.sqrt ( n ) ) + 1 , 2 ) : count = 0 while n % i == 0 : count += 1 n //= i if c...
int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 ) ans *= 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 ) ans *= i ; } if ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMinNumber ( n ) : count = 0 ans = 1 while n % 2 == 0 : count += 1 n //= 2 if count % 2 is not 0 : ans *= 2 for i in range ( 3 , ( int ) ( math.sqrt ( n ) ) + 1 , 2 ) : count = 0 while n % i == 0 : count += 1 n //= i ...
python-cpp-GCD_ELEMENTS_GIVEN_RANGE
python-cpp
def rangeGCD ( n , m ) : return n if ( n == m ) else 1
int rangeGCD ( int n , int m ) { return ( n == m ) ? n : 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def rangeGCD ( n , m ) : return n if ( n == m ) else 1 </Code>
python-cpp-PROGRAMMING_PUZZLE_ASSIGN_VALUE_WITHOUT_CONTROL_STATEMENT
python-cpp
def assignValue ( a , b , x ) : arr = [ a , b ] return ( arr [ x ] )
int assignValue ( int a , int b , bool x ) { int arr [ ] = { a , b } ; return ( arr [ x ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def assignValue ( a , b , x ) : arr = [ a , b ] return ( arr [ x ] ) </Code>
python-cpp-TRIANGULAR_NUMBERS
python-cpp
def isTriangular ( num ) : if ( num < 0 ) : return False sum , n = 0 , 1 while ( sum <= num ) : sum = sum + n if ( sum == num ) : return True n += 1 return False
bool isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isTriangular ( num ) : if ( num < 0 ) : return False sum , n = 0 , 1 while ( sum <= num ) : sum = sum + n if ( sum == num ) : return True n += 1 return False </Code>
python-cpp-PANGRAM_CHECKING
python-cpp
def checkPangram ( s ) : List = [ ] for i in range ( 26 ) : List.append ( False ) for c in s.lower ( ) : if not c == " " : List [ ord ( c ) - ord ( 'a' ) ] = True for ch in List : if ch == False : return False return True
bool checkPangram ( string & str ) { vector < bool > mark ( 26 , false ) ; int index ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( 'A' <= str [ i ] && str [ i ] <= 'Z' ) index = str [ i ] - 'A' ; else if ( 'a' <= str [ i ] && str [ i ] <= 'z' ) index = str [ i ] - 'a' ; mark [ index ] =...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkPangram ( s ) : List = [ ] for i in range ( 26 ) : List.append ( False ) for c in s.lower ( ) : if not c == " " : List [ ord ( c ) - ord ( 'a' ) ] = True for ch in List : if ch == False : return False return True </Code>
python-cpp-INTERPOLATION_SEARCH
python-cpp
def interpolationSearch ( arr , n , x ) : lo = 0 hi = ( n - 1 ) while lo <= hi and x >= arr [ lo ] and x <= arr [ hi ] : if lo == hi : if arr [ lo ] == x : return lo ; return - 1 ; pos = lo + int ( ( ( float ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) ...
int interpolationSearch ( int arr [ ] , int n , int x ) { int lo = 0 , hi = ( n - 1 ) ; while ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) { if ( lo == hi ) { if ( arr [ lo ] == x ) return lo ; return - 1 ; } int pos = lo + ( ( ( double ) ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * (...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def interpolationSearch ( arr , n , x ) : lo = 0 hi = ( n - 1 ) while lo <= hi and x >= arr [ lo ] and x <= arr [ hi ] : if lo == hi : if arr [ lo ] == x : return lo ; return - 1 ; pos = lo + int ( ( ( float ( hi - lo ) / ( arr [ hi ] - arr [ lo...
python-cpp-FITTING_SHELVES_PROBLEM
python-cpp
def minSpacePreferLarge ( w , m , n ) : num_m = 0 num_n = 0 rem = w p = 0 q = 0 r = 0 while ( w >= n ) : p = w / m r = w % m if ( r <= rem ) : num_m = p num_n = q rem = r q += 1 w -= n print ( str ( int ( num_m )...
void minSpacePreferLarge ( int wall , int m , int n ) { int num_m = 0 , num_n = 0 , min_empty = wall ; int p = 0 , q = 0 , rem ; while ( wall >= n ) { p = wall / m ; rem = wall % m ; if ( rem <= min_empty ) { num_m = p ; num_n = q ; min_empty = rem ; } q += 1 ; wall = wal...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minSpacePreferLarge ( w , m , n ) : num_m = 0 num_n = 0 rem = w p = 0 q = 0 r = 0 while ( w >= n ) : p = w / m r = w % m if ( r <= rem ) : num_m = p num_n = q rem = r q += 1 w -= n print ( str ( int ( ...
python-cpp-FIND_REPETITIVE_ELEMENT_1_N_1_3
python-cpp
def findRepeating ( arr , n ) : missingElement = 0 for i in range ( 0 , n ) : element = arr [ abs ( arr [ i ] ) ] if ( element < 0 ) : missingElement = arr [ i ] break arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] return abs ( missingElement )
int findRepeating ( int arr [ ] , int n ) { int missingElement = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int element = arr [ abs ( arr [ i ] ) ] ; if ( element < 0 ) { missingElement = arr [ i ] ; break ; } arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ; } return abs ( m...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findRepeating ( arr , n ) : missingElement = 0 for i in range ( 0 , n ) : element = arr [ abs ( arr [ i ] ) ] if ( element < 0 ) : missingElement = arr [ i ] break arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] return abs ( missingElement )...
python-cpp-K_LARGESTOR_SMALLEST_ELEMENTS_IN_AN_ARRAY
python-cpp
def kLargest ( arr , k ) : arr.sort ( reverse = True ) for i in range ( k ) : print ( arr [ i ] , end = " " )
void kLargest ( int arr [ ] , int n , int k ) { sort ( arr , arr + n , greater < int > ( ) ) ; for ( int i = 0 ; i < k ; i ++ ) cout << arr [ i ] << " " ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def kLargest ( arr , k ) : arr.sort ( reverse = True ) for i in range ( k ) : print ( arr [ i ] , end = " " ) </Code>
python-cpp-DYNAMIC_PROGRAMMING_SET_10_0_1_KNAPSACK_PROBLEM_1
python-cpp
def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( val [ i...
int knapSack ( int W , int wt [ ] , int val [ ] , int n ) { int i , w ; int K [ n + 1 ] [ W + 1 ] ; for ( i = 0 ; i <= n ; i ++ ) { for ( w = 0 ; w <= W ; w ++ ) { if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ; else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ i - 1 ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x in range ( n + 1 ) ] for i in range ( n + 1 ) : for w in range ( W + 1 ) : if i == 0 or w == 0 : K [ i ] [ w ] = 0 elif wt [ i - 1 ] <= w : K [ i ] [ w ] = max ( ...
python-cpp-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_2
python-cpp
def isPowerOfFour ( n ) : return ( n != 0 and ( ( n & ( n - 1 ) ) == 0 ) and not ( n & 0xAAAAAAAA ) )
bool isPowerOfFour ( unsigned int n ) { return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isPowerOfFour ( n ) : return ( n != 0 and ( ( n & ( n - 1 ) ) == 0 ) and not ( n & 0xAAAAAAAA ) ) </Code>
python-cpp-C_PROGRAM_FACTORIAL_NUMBER
python-cpp
def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 ) </Code>
python-cpp-COUNT_POSSIBLE_GROUPS_SIZE_2_3_SUM_MULTIPLE_3
python-cpp
def findgroups ( arr , n ) : c = [ 0 , 0 , 0 ] res = 0 for i in range ( 0 , n ) : c [ arr [ i ] % 3 ] += 1 res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) res += c [ 1 ] * c [ 2 ] res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c [ 1 ] ...
int findgroups ( int arr [ ] , int n ) { int c [ 3 ] = { 0 } , i ; int res = 0 ; for ( i = 0 ; i < n ; i ++ ) c [ arr [ i ] % 3 ] ++ ; res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) ; res += c [ 1 ] * c [ 2 ] ; res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 ; res += ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findgroups ( arr , n ) : c = [ 0 , 0 , 0 ] res = 0 for i in range ( 0 , n ) : c [ arr [ i ] % 3 ] += 1 res += ( ( c [ 0 ] * ( c [ 0 ] - 1 ) ) >> 1 ) res += c [ 1 ] * c [ 2 ] res += ( c [ 0 ] * ( c [ 0 ] - 1 ) * ( c [ 0 ] - 2 ) ) / 6 res += ( c [ 1 ] * ( c [ 1 ] - 1 ) * ( c...
python-cpp-K_TH_LARGEST_SUM_CONTIGUOUS_SUBARRAY
python-cpp
def kthLargestSum ( arr , n , k ) : sum = [ ] sum.append ( 0 ) sum.append ( arr [ 0 ] ) for i in range ( 2 , n + 1 ) : sum.append ( sum [ i - 1 ] + arr [ i - 1 ] ) Q = [ ] heapq.heapify ( Q ) for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : x = sum [ ...
int kthLargestSum ( int arr [ ] , int n , int k ) { int sum [ n + 1 ] ; sum [ 0 ] = 0 ; sum [ 1 ] = arr [ 0 ] ; for ( int i = 2 ; i <= n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ; priority_queue < int , vector < int > , greater < int >> Q ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def kthLargestSum ( arr , n , k ) : sum = [ ] sum.append ( 0 ) sum.append ( arr [ 0 ] ) for i in range ( 2 , n + 1 ) : sum.append ( sum [ i - 1 ] + arr [ i - 1 ] ) Q = [ ] heapq.heapify ( Q ) for i in range ( 1 , n + 1 ) : for j in range ( i , n + 1 ) : x =...
python-cpp-FIND_A_SPECIFIC_PAIR_IN_MATRIX_1
python-cpp
def findMaxValue ( mat ) : maxValue = - sys.maxsize - 1 maxArr = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] maxv = mat [ N - 1 ] [ N - 1 ] for j in range ( N - 2 , - 1 , - 1 ) : if ( mat [ N - 1 ] [ j ] > maxv ) : maxv...
int findMaxValue ( int mat [ ] [ N ] ) { int maxValue = INT_MIN ; int maxArr [ N ] [ N ] ; maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] ; int maxv = mat [ N - 1 ] [ N - 1 ] ; for ( int j = N - 2 ; j >= 0 ; j -- ) { if ( mat [ N - 1 ] [ j ] > maxv ) maxv = mat [ N - 1 ] [ j ] ; maxArr [ N -...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaxValue ( mat ) : maxValue = - sys.maxsize - 1 maxArr = [ [ 0 for x in range ( N ) ] for y in range ( N ) ] maxArr [ N - 1 ] [ N - 1 ] = mat [ N - 1 ] [ N - 1 ] maxv = mat [ N - 1 ] [ N - 1 ] for j in range ( N - 2 , - 1 , - 1 ) : if ( mat [ N - 1 ] [ j ] > maxv ) : ...
python-cpp-K_TH_PRIME_FACTOR_GIVEN_NUMBER
python-cpp
def kPrimeFactor ( n , k ) : while ( n % 2 == 0 ) : k = k - 1 n = n / 2 if ( k == 0 ) : return 2 i = 3 while i <= math.sqrt ( n ) : while ( n % i == 0 ) : if ( k == 1 ) : return i k = k - 1 n = n / i i = ...
int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; retur...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def kPrimeFactor ( n , k ) : while ( n % 2 == 0 ) : k = k - 1 n = n / 2 if ( k == 0 ) : return 2 i = 3 while i <= math.sqrt ( n ) : while ( n % i == 0 ) : if ( k == 1 ) : return i k = k - 1 n = n / i ...
python-cpp-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_2
python-cpp
def printDistinct ( arr , n ) : s = dict ( ) for i in range ( n ) : if ( arr [ i ] not in s.keys ( ) ) : s [ arr [ i ] ] = arr [ i ] print ( arr [ i ] , end = " " )
void printDistinct ( int arr [ ] , int n ) { unordered_set < int > s ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . find ( arr [ i ] ) == s . end ( ) ) { s . insert ( arr [ i ] ) ; cout << arr [ i ] << " " ; } } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printDistinct ( arr , n ) : s = dict ( ) for i in range ( n ) : if ( arr [ i ] not in s.keys ( ) ) : s [ arr [ i ] ] = arr [ i ] print ( arr [ i ] , end = " " ) </Code>
python-cpp-PROGRAM_DECIMAL_BINARY_CONVERSION_1
python-cpp
def decToBinary ( n ) : for i in range ( 31 , - 1 , - 1 ) : k = n >> i if ( k & 1 ) : print ( "1" , end = "" ) else : print ( "0" , end = "" )
int decToBinary ( int n ) { for ( int i = 31 ; i >= 0 ; i -- ) { int k = n >> i ; if ( k & 1 ) cout << "1" ; else cout << "0" ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def decToBinary ( n ) : for i in range ( 31 , - 1 , - 1 ) : k = n >> i if ( k & 1 ) : print ( "1" , end = "" ) else : print ( "0" , end = "" ) </Code>
python-cpp-SUPER_PRIME
python-cpp
def SieveOfEratosthenes ( n , isPrime ) : isPrime [ 0 ] = isPrime [ 1 ] = False for i in range ( 2 , n + 1 ) : isPrime [ i ] = True for p in range ( 2 , n + 1 ) : if ( p * p <= n and isPrime [ p ] == True ) : for i in range ( p * 2 , n + 1 , p ) : isPrime [ i ] = ...
bool SieveOfEratosthenes ( int n , bool isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i <= n ; i += p ) isPrime [ i ] = fals...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def SieveOfEratosthenes ( n , isPrime ) : isPrime [ 0 ] = isPrime [ 1 ] = False for i in range ( 2 , n + 1 ) : isPrime [ i ] = True for p in range ( 2 , n + 1 ) : if ( p * p <= n and isPrime [ p ] == True ) : for i in range ( p * 2 , n + 1 , p ) : isPrime [...
python-cpp-COUNT_OPERATIONS_MAKE_STRINGAB_FREE
python-cpp
def abFree ( s ) : b_count = 0 res = 0 for i in range ( len ( s ) ) : if s [ ~ i ] == 'a' : res = ( res + b_count ) b_count = ( b_count * 2 ) else : b_count += 1 return res
int abFree ( string s ) { int n = s . length ( ) ; char char_array [ n + 1 ] ; strcpy ( char_array , s . c_str ( ) ) ; int b_count = 0 ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( char_array [ n - i - 1 ] == 'a' ) { res = ( res + b_count ) ; b_count = ( b_count * 2 ) ; } ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def abFree ( s ) : b_count = 0 res = 0 for i in range ( len ( s ) ) : if s [ ~ i ] == 'a' : res = ( res + b_count ) b_count = ( b_count * 2 ) else : b_count += 1 return res </Code>
python-cpp-RECURSIVE_SOLUTION_COUNT_SUBSTRINGS_FIRST_LAST_CHARACTERS
python-cpp
def countSubstrs ( str , i , j , n ) : if ( n == 1 ) : return 1 if ( n <= 0 ) : return 0 res = ( countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ) if ( str [ i ] == str [ j ] ) : res += 1 retur...
int countSubstrs ( string str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str [ i ] == str [ j ] ) res ++ ; return res ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countSubstrs ( str , i , j , n ) : if ( n == 1 ) : return 1 if ( n <= 0 ) : return 0 res = ( countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ) if ( str [ i ] == str [ j ] ) : res += 1 ...
python-cpp-FIND_THE_FIRST_MISSING_NUMBER
python-cpp
def findFirstMissing ( array , start , end ) : if ( start > end ) : return end + 1 if ( start != array [ start ] ) : return start mid = int ( ( start + end ) / 2 ) if ( array [ mid ] == mid ) : return findFirstMissing ( array , mid + 1 , end ) return findFirstMissing ( array ...
int findFirstMissing ( int array [ ] , int start , int end ) { if ( start > end ) return end + 1 ; if ( start != array [ start ] ) return start ; int mid = ( start + end ) / 2 ; if ( array [ mid ] == mid ) return findFirstMissing ( array , mid + 1 , end ) ; return findFirstMissing ( array , start , mid ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findFirstMissing ( array , start , end ) : if ( start > end ) : return end + 1 if ( start != array [ start ] ) : return start mid = int ( ( start + end ) / 2 ) if ( array [ mid ] == mid ) : return findFirstMissing ( array , mid + 1 , end ) return findFirstMissing (...
python-cpp-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER
python-cpp
def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 )
unsigned int factorial ( unsigned int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def factorial ( n ) : return 1 if ( n == 1 or n == 0 ) else n * factorial ( n - 1 ) </Code>
python-cpp-MINIMUM_ROTATIONS_UNLOCK_CIRCULAR_LOCK
python-cpp
def minRotation ( input , unlock_code ) : rotation = 0 while ( input > 0 or unlock_code > 0 ) : input_digit = input % 10 code_digit = unlock_code % 10 rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) input = int ( input / 10 ) u...
int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input || unlock_code ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; input /= 10...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minRotation ( input , unlock_code ) : rotation = 0 while ( input > 0 or unlock_code > 0 ) : input_digit = input % 10 code_digit = unlock_code % 10 rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) input = int ( input / 10 ) ...
python-cpp-LARGEST_SUBSET_WHOSE_ALL_ELEMENTS_ARE_FIBONACCI_NUMBERS
python-cpp
def findFibSubset ( arr , n ) : m = max ( arr ) a = 0 b = 1 hash = [ ] hash.append ( a ) hash.append ( b ) while ( b < m ) : c = a + b a = b b = c hash.append ( b ) for i in range ( n ) : if arr [ i ] in hash : print ( arr [ i ] , end =...
void findFibSubset ( int arr [ ] , int n ) { int max = * std :: max_element ( arr , arr + n ) ; int a = 0 , b = 1 ; unordered_set < int > hash ; hash . insert ( a ) ; hash . insert ( b ) ; while ( b < max ) { int c = a + b ; a = b ; b = c ; hash . insert ( b ) ; } for ( int i = 0 ; i <...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findFibSubset ( arr , n ) : m = max ( arr ) a = 0 b = 1 hash = [ ] hash.append ( a ) hash.append ( b ) while ( b < m ) : c = a + b a = b b = c hash.append ( b ) for i in range ( n ) : if arr [ i ] in hash : print ( arr [ i ] ...
python-cpp-MARKOV_MATRIX
python-cpp
def checkMarkov ( m ) : for i in range ( 0 , len ( m ) ) : sm = 0 for j in range ( 0 , len ( m [ i ] ) ) : sm = sm + m [ i ] [ j ] if ( sm != 1 ) : return False return True
bool checkMarkov ( double m [ ] [ n ] ) { for ( int i = 0 ; i < n ; i ++ ) { double sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum = sum + m [ i ] [ j ] ; if ( sum != 1 ) return false ; } return true ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def checkMarkov ( m ) : for i in range ( 0 , len ( m ) ) : sm = 0 for j in range ( 0 , len ( m [ i ] ) ) : sm = sm + m [ i ] [ j ] if ( sm != 1 ) : return False return True </Code>
python-cpp-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY_1
python-cpp
def insertSorted ( arr , n , key , capacity ) : if ( n >= capacity ) : return n i = n - 1 while i >= 0 and arr [ i ] > key : arr [ i + 1 ] = arr [ i ] i -= 1 arr [ i + 1 ] = key return ( n + 1 )
int insertSorted ( int arr [ ] , int n , int key , int capacity ) { if ( n >= capacity ) return n ; int i ; for ( i = n - 1 ; ( i >= 0 && arr [ i ] > key ) ; i -- ) arr [ i + 1 ] = arr [ i ] ; arr [ i + 1 ] = key ; return ( n + 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def insertSorted ( arr , n , key , capacity ) : if ( n >= capacity ) : return n i = n - 1 while i >= 0 and arr [ i ] > key : arr [ i + 1 ] = arr [ i ] i -= 1 arr [ i + 1 ] = key return ( n + 1 ) </Code>
python-cpp-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_2
python-cpp
def recursiveReverse ( str , i = 0 ) : n = len ( str ) if i == n // 2 : return str [ i ] , str [ n - i - 1 ] = str [ n - i - 1 ] , str [ i ] recursiveReverse ( str , i + 1 )
void recursiveReverse ( string & str , int i = 0 ) { int n = str . length ( ) ; if ( i == n / 2 ) return ; swap ( str [ i ] , str [ n - i - 1 ] ) ; recursiveReverse ( str , i + 1 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def recursiveReverse ( str , i = 0 ) : n = len ( str ) if i == n // 2 : return str [ i ] , str [ n - i - 1 ] = str [ n - i - 1 ] , str [ i ] recursiveReverse ( str , i + 1 ) </Code>
python-cpp-CHECK_IF_ARRAY_ELEMENTS_ARE_CONSECUTIVE
python-cpp
def areConsecutive ( arr , n ) : if ( n < 1 ) : return False Min = min ( arr ) Max = max ( arr ) if ( Max - Min + 1 == n ) : visited = [ False for i in range ( n ) ] for i in range ( n ) : if ( visited [ arr [ i ] - Min ] != False ) : return False ...
bool areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { bool * visited = ( bool * ) calloc ( n , sizeof ( bool ) ) ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def areConsecutive ( arr , n ) : if ( n < 1 ) : return False Min = min ( arr ) Max = max ( arr ) if ( Max - Min + 1 == n ) : visited = [ False for i in range ( n ) ] for i in range ( n ) : if ( visited [ arr [ i ] - Min ] != False ) : return Fal...
python-cpp-MODULUS_TWO_FLOAT_DOUBLE_NUMBERS
python-cpp
def findMod ( a , b ) : if ( a < 0 ) : a = - a if ( b < 0 ) : b = - b mod = a while ( mod >= b ) : mod = mod - b if ( a < 0 ) : return - mod return mod
double findMod ( double a , double b ) { double mod ; if ( a < 0 ) mod = - a ; else mod = a ; if ( b < 0 ) b = - b ; while ( mod >= b ) mod = mod - b ; if ( a < 0 ) return - mod ; return mod ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMod ( a , b ) : if ( a < 0 ) : a = - a if ( b < 0 ) : b = - b mod = a while ( mod >= b ) : mod = mod - b if ( a < 0 ) : return - mod return mod </Code>
python-cpp-SUM_OF_ALL_SUBSTRINGS_OF_A_STRING_REPRESENTING_A_NUMBER
python-cpp
def sumOfSubstrings ( num ) : n = len ( num ) sumofdigit = [ ] sumofdigit.append ( int ( num [ 0 ] ) ) res = sumofdigit [ 0 ] for i in range ( 1 , n ) : numi = int ( num [ i ] ) sumofdigit.append ( ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ) res += sumofdigit [ i ] ret...
int sumOfSubstrings ( string num ) { int n = num . length ( ) ; int sumofdigit [ n ] ; sumofdigit [ 0 ] = toDigit ( num [ 0 ] ) ; int res = sumofdigit [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int numi = toDigit ( num [ i ] ) ; sumofdigit [ i ] = ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ; ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def sumOfSubstrings ( num ) : n = len ( num ) sumofdigit = [ ] sumofdigit.append ( int ( num [ 0 ] ) ) res = sumofdigit [ 0 ] for i in range ( 1 , n ) : numi = int ( num [ i ] ) sumofdigit.append ( ( i + 1 ) * numi + 10 * sumofdigit [ i - 1 ] ) res += sumofdigit [ i ] ...
python-cpp-FIND_PAIR_WITH_GREATEST_PRODUCT_IN_ARRAY_1
python-cpp
def findGreatest ( arr , n ) : m = dict ( ) for i in arr : m [ i ] = m.get ( i , 0 ) + 1 arr = sorted ( arr ) for i in range ( n - 1 , 0 , - 1 ) : j = 0 while ( j < i and arr [ j ] <= sqrt ( arr [ i ] ) ) : if ( arr [ i ] % arr [ j ] == 0 ) : result = ...
int findGreatest ( int arr [ ] , int n ) { unordered_map < int , int > m ; for ( int i = 0 ; i < n ; i ++ ) m [ arr [ i ] ] ++ ; sort ( arr , arr + n ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findGreatest ( arr , n ) : m = dict ( ) for i in arr : m [ i ] = m.get ( i , 0 ) + 1 arr = sorted ( arr ) for i in range ( n - 1 , 0 , - 1 ) : j = 0 while ( j < i and arr [ j ] <= sqrt ( arr [ i ] ) ) : if ( arr [ i ] % arr [ j ] == 0 ) : re...
python-cpp-FIND_THE_MISSING_NUMBER_1
python-cpp
def getMissingNo ( a , n ) : i , total = 0 , 1 for i in range ( 2 , n + 2 ) : total += i total -= a [ i - 2 ] return total
int getMissingNo ( int a [ ] , int n ) { int i , total = 1 ; for ( i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def getMissingNo ( a , n ) : i , total = 0 , 1 for i in range ( 2 , n + 2 ) : total += i total -= a [ i - 2 ] return total </Code>
python-cpp-NEXT_POWER_OF_2_2
python-cpp
def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n
unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n </Code>
python-cpp-C_PROGRAM_ADDITION_TWO_MATRICES
python-cpp
def add ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ]
void add ( int A [ ] [ N ] , int B [ ] [ N ] , int C [ ] [ N ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def add ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] </Code>
python-cpp-ENTRINGER_NUMBER_1
python-cpp
def zigzag ( n , k ) : dp = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ i ...
int zigzag ( int n , int k ) { int dp [ n + 1 ] [ k + 1 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def zigzag ( n , k ) : dp = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1...
python-cpp-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE_1
python-cpp
def eggDrop ( n , k ) : eggFloor = [ [ 0 for x in range ( k + 1 ) ] for x in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : eggFloor [ i ] [ 1 ] = 1 eggFloor [ i ] [ 0 ] = 0 for j in range ( 1 , k + 1 ) : eggFloor [ 1 ] [ j ] = j for i in range ( 2 , n + 1 ) : for j in ...
int eggDrop ( int n , int k ) { int eggFloor [ n + 1 ] [ k + 1 ] ; int res ; int i , j , x ; for ( i = 1 ; i <= n ; i ++ ) { eggFloor [ i ] [ 1 ] = 1 ; eggFloor [ i ] [ 0 ] = 0 ; } for ( j = 1 ; j <= k ; j ++ ) eggFloor [ 1 ] [ j ] = j ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 2 ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def eggDrop ( n , k ) : eggFloor = [ [ 0 for x in range ( k + 1 ) ] for x in range ( n + 1 ) ] for i in range ( 1 , n + 1 ) : eggFloor [ i ] [ 1 ] = 1 eggFloor [ i ] [ 0 ] = 0 for j in range ( 1 , k + 1 ) : eggFloor [ 1 ] [ j ] = j for i in range ( 2 , n + 1 ) : fo...
python-cpp-PRINT_A_CLOSEST_STRING_THAT_DOES_NOT_CONTAIN_ADJACENT_DUPLICATES
python-cpp
def noAdjacentDup ( s ) : n = len ( s ) for i in range ( 1 , n ) : if ( s [ i ] == s [ i - 1 ] ) : s [ i ] = "a" while ( s [ i ] == s [ i - 1 ] or ( i + 1 < n and s [ i ] == s [ i + 1 ] ) ) : s [ i ] += 1 i += 1 return s
string noAdjacentDup ( string s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( s [ i ] == s [ i - 1 ] ) { s [ i ] = 'a' ; while ( s [ i ] == s [ i - 1 ] || ( i + 1 < n && s [ i ] == s [ i + 1 ] ) ) s [ i ] ++ ; i ++ ; } } return s ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def noAdjacentDup ( s ) : n = len ( s ) for i in range ( 1 , n ) : if ( s [ i ] == s [ i - 1 ] ) : s [ i ] = "a" while ( s [ i ] == s [ i - 1 ] or ( i + 1 < n and s [ i ] == s [ i + 1 ] ) ) : s [ i ] += 1 i += 1 return s </Code>
python-cpp-MODIFY_ARRAY_TO_MAXIMIZE_SUM_OF_ADJACENT_DIFFERENCES
python-cpp
def maximumDifferenceSum ( arr , N ) : dp = [ [ 0 , 0 ] for i in range ( N ) ] for i in range ( N ) : dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 for i in range ( N - 1 ) : dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) dp [ i + 1 ] [ 1 ] = max ( dp [ i ...
int maximumDifferenceSum ( int arr [ ] , int N ) { int dp [ N ] [ 2 ] ; for ( int i = 0 ; i < N ; i ++ ) dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 ; for ( int i = 0 ; i < ( N - 1 ) ; i ++ ) { dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) ; dp [ i + 1 ] [ 1 ] = max ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maximumDifferenceSum ( arr , N ) : dp = [ [ 0 , 0 ] for i in range ( N ) ] for i in range ( N ) : dp [ i ] [ 0 ] = dp [ i ] [ 1 ] = 0 for i in range ( N - 1 ) : dp [ i + 1 ] [ 0 ] = max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + abs ( 1 - arr [ i ] ) ) dp [ i + 1 ] [ 1 ] = max ( ...
python-cpp-MINIMUM_INSERTIONS_SORT_ARRAY
python-cpp
def minInsertionStepToSortArray ( arr , N ) : lis = [ 0 ] * N for i in range ( N ) : lis [ i ] = 1 for i in range ( 1 , N ) : for j in range ( i ) : if ( arr [ i ] >= arr [ j ] and lis [ i ] < lis [ j ] + 1 ) : lis [ i ] = lis [ j ] + 1 max = 0 for i in ra...
int minInsertionStepToSortArray ( int arr [ ] , int N ) { int lis [ N ] ; for ( int i = 0 ; i < N ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] >= arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int max = 0 ; for ( in...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def minInsertionStepToSortArray ( arr , N ) : lis = [ 0 ] * N for i in range ( N ) : lis [ i ] = 1 for i in range ( 1 , N ) : for j in range ( i ) : if ( arr [ i ] >= arr [ j ] and lis [ i ] < lis [ j ] + 1 ) : lis [ i ] = lis [ j ] + 1 max = 0 for ...
python-cpp-EFFICIENT_WAY_CHECK_WHETHER_N_TH_FIBONACCI_NUMBER_MULTIPLE_10
python-cpp
def isMultipleOf10 ( n ) : return ( n % 15 == 0 )
bool isMultipleOf10 ( int n ) { return ( n % 15 == 0 ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def isMultipleOf10 ( n ) : return ( n % 15 == 0 ) </Code>
python-cpp-HARDY_RAMANUJAN_THEOREM
python-cpp
def exactPrimeFactorCount ( n ) : count = 0 if ( n % 2 == 0 ) : count = count + 1 while ( n % 2 == 0 ) : n = int ( n / 2 ) i = 3 while ( i <= int ( math.sqrt ( n ) ) ) : if ( n % i == 0 ) : count = count + 1 while ( n % i == 0 ) : ...
int exactPrimeFactorCount ( int n ) { int count = 0 ; if ( n % 2 == 0 ) { count ++ ; while ( n % 2 == 0 ) n = n / 2 ; } for ( int i = 3 ; i <= sqrt ( n ) ; i = i + 2 ) { if ( n % i == 0 ) { count ++ ; while ( n % i == 0 ) n = n / i ; } } if ( n > 2 ) count ++ ; return count...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def exactPrimeFactorCount ( n ) : count = 0 if ( n % 2 == 0 ) : count = count + 1 while ( n % 2 == 0 ) : n = int ( n / 2 ) i = 3 while ( i <= int ( math.sqrt ( n ) ) ) : if ( n % i == 0 ) : count = count + 1 while ( n % i == 0 ) : ...
python-cpp-QUERIES_FOR_CHARACTERS_IN_A_REPEATED_STRING
python-cpp
def query ( s , i , j ) : n = len ( s ) i %= n j %= n print ( "Yes" ) if s [ i ] == s [ j ] else print ( "No" )
void query ( char s [ ] , int i , int j ) { int n = strlen ( s ) ; i %= n ; j %= n ; ( s [ i ] == s [ j ] ) ? ( cout << "Yes" << endl ) : ( cout << "No" << endl ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def query ( s , i , j ) : n = len ( s ) i %= n j %= n print ( "Yes" ) if s [ i ] == s [ j ] else print ( "No" ) </Code>
python-cpp-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY_1
python-cpp
def maxProduct ( arr , n ) : if n < 3 : return - 1 arr.sort ( ) return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] )
int maxProduct ( int arr [ ] , int n ) { if ( n < 3 ) return - 1 ; sort ( arr , arr + n ) ; return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def maxProduct ( arr , n ) : if n < 3 : return - 1 arr.sort ( ) return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) </Code>
python-cpp-FIND_SUBARRAY_LEAST_AVERAGE
python-cpp
def findMinAvgSubarray ( arr , n , k ) : if ( n < k ) : return 0 res_index = 0 curr_sum = 0 for i in range ( k ) : curr_sum += arr [ i ] min_sum = curr_sum for i in range ( k , n ) : curr_sum += arr [ i ] - arr [ i - k ] if ( curr_sum < min_sum ) : min_sum = c...
void findMinAvgSubarray ( int arr [ ] , int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMinAvgSubarray ( arr , n , k ) : if ( n < k ) : return 0 res_index = 0 curr_sum = 0 for i in range ( k ) : curr_sum += arr [ i ] min_sum = curr_sum for i in range ( k , n ) : curr_sum += arr [ i ] - arr [ i - k ] if ( curr_sum < min_sum ) : min_...
python-cpp-COUNT_POSSIBLE_PATHS_SOURCE_DESTINATION_EXACTLY_K_EDGES
python-cpp
def countwalks ( graph , u , v , k ) : if ( k == 0 and u == v ) : return 1 if ( k == 1 and graph [ u ] [ v ] ) : return 1 if ( k <= 0 ) : return 0 count = 0 for i in range ( 0 , V ) : if ( graph [ u ] [ i ] == 1 ) : count += countwalks ( graph , i , v , k ...
int countwalks ( int graph [ ] [ V ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 1 ; if ( k == 1 && graph [ u ] [ v ] ) return 1 ; if ( k <= 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < V ; i ++ ) if ( graph [ u ] [ i ] == 1 ) count += countwalks ( graph , i , v , k - 1 ) ; return...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countwalks ( graph , u , v , k ) : if ( k == 0 and u == v ) : return 1 if ( k == 1 and graph [ u ] [ v ] ) : return 1 if ( k <= 0 ) : return 0 count = 0 for i in range ( 0 , V ) : if ( graph [ u ] [ i ] == 1 ) : count += countwalks ( graph , i ,...
python-cpp-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY_1
python-cpp
def countPairs ( arr , n ) : result = 0 Hash = set ( ) for i in range ( n ) : Hash.add ( arr [ i ] ) for i in range ( n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] if product in ( Hash ) : result += 1 return result
int countPairs ( int arr [ ] , int n ) { int result = 0 ; set < int > Hash ; for ( int i = 0 ; i < n ; i ++ ) Hash . insert ( arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; if ( Hash . find ( product )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def countPairs ( arr , n ) : result = 0 Hash = set ( ) for i in range ( n ) : Hash.add ( arr [ i ] ) for i in range ( n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] if product in ( Hash ) : result += 1 return result...
python-cpp-SERIES_LARGEST_GCD_SUM_EQUALS_N
python-cpp
def print_sequence ( n , k ) : b = int ( n / ( k * ( k + 1 ) / 2 ) ) if b == 0 : print ( "-1" ) else : r = 1 x = 1 while x ** 2 <= n : if n % x != 0 : continue elif x <= b and x > r : r = x elif n / x <= b an...
void print_sequence ( int n , int k ) { int b = n / ( k * ( k + 1 ) / 2 ) ; if ( b == 0 ) { cout << - 1 << endl ; } else { int r = 1 ; for ( int x = 1 ; x * x <= n ; x ++ ) { if ( n % x != 0 ) continue ; if ( x <= b && x > r ) r = x ; if ( n / x <= b && n / x > r ) r = n / ...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def print_sequence ( n , k ) : b = int ( n / ( k * ( k + 1 ) / 2 ) ) if b == 0 : print ( "-1" ) else : r = 1 x = 1 while x ** 2 <= n : if n % x != 0 : continue elif x <= b and x > r : r = x elif n / x ...
python-cpp-SMALLEST_POWER_OF_2_GREATER_THAN_OR_EQUAL_TO_N_2
python-cpp
def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n
unsigned int nextPowerOf2 ( unsigned int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def nextPowerOf2 ( n ) : n -= 1 n |= n >> 1 n |= n >> 2 n |= n >> 4 n |= n >> 8 n |= n >> 16 n += 1 return n </Code>
python-cpp-MAXIMIZE_ARRJ_ARRI_ARRL_ARRK_SUCH_THAT_I_J_K_L
python-cpp
def findMaxValue ( arr , n ) : if n < 4 : print ( "The array should have atlest 4 elements" ) return MIN table1 , table2 = [ MIN ] * ( n + 1 ) , [ MIN ] * n table3 , table4 = [ MIN ] * ( n - 1 ) , [ MIN ] * ( n - 2 ) for i in range ( n - 1 , - 1 , - 1 ) : table1 [ i ] = max ( tab...
- 100000000 int findMaxValue ( int arr [ ] , int n ) { if ( n < 4 ) { printf ( "The array should have atlest 4 elements\n" ) ; return MIN ; } int table1 [ n + 1 ] , table2 [ n ] , table3 [ n - 1 ] , table4 [ n - 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) table1 [ i ] = table2 [ i ] = table3 [ i ] = tab...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findMaxValue ( arr , n ) : if n < 4 : print ( "The array should have atlest 4 elements" ) return MIN table1 , table2 = [ MIN ] * ( n + 1 ) , [ MIN ] * n table3 , table4 = [ MIN ] * ( n - 1 ) , [ MIN ] * ( n - 2 ) for i in range ( n - 1 , - 1 , - 1 ) : table1 [ i ] = ma...
python-cpp-PRINT_ARRAY_STRINGS_SORTED_ORDER_WITHOUT_COPYING_ONE_STRING_ANOTHER
python-cpp
def printInSortedOrder ( arr , n ) : index = [ 0 ] * n for i in range ( n ) : index [ i ] = i for i in range ( n - 1 ) : min = i for j in range ( i + 1 , n ) : if ( arr [ index [ min ] ] > arr [ index [ j ] ] ) : min = j if ( min != i ) : ...
void printInSortedOrder ( string arr [ ] , int n ) { int index [ n ] ; int i , j , min ; for ( i = 0 ; i < n ; i ++ ) index [ i ] = i ; for ( i = 0 ; i < n - 1 ; i ++ ) { min = i ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ index [ min ] ] . compare ( arr [ index [ j ] ] ) > 0 )...
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printInSortedOrder ( arr , n ) : index = [ 0 ] * n for i in range ( n ) : index [ i ] = i for i in range ( n - 1 ) : min = i for j in range ( i + 1 , n ) : if ( arr [ index [ min ] ] > arr [ index [ j ] ] ) : min = j if ( min != i ) : ...
python-cpp-SQUARED_TRIANGULAR_NUMBER_SUM_CUBES
python-cpp
def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n * n n += 1 n -= 1 if _sum == s : return n return - 1
int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n * n * n ; if ( sum == s ) return n ; } return - 1 ; }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def findS ( s ) : _sum = 0 n = 1 while ( _sum < s ) : _sum += n * n * n n += 1 n -= 1 if _sum == s : return n return - 1 </Code>
python-cpp-RECURSION
python-cpp
def printFun ( test ) : if ( test < 1 ) : return else : print ( test , end = " " ) printFun ( test - 1 ) print ( test , end = " " ) return
void printFun ( int test ) { if ( test < 1 ) return ; else { cout << test << " " ; printFun ( test - 1 ) ; cout << test << " " ; return ; } }
Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>.
<Code> def printFun ( test ) : if ( test < 1 ) : return else : print ( test , end = " " ) printFun ( test - 1 ) print ( test , end = " " ) return </Code>