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 |
|---|---|---|---|---|---|
java-cpp-REORDER_A_ARRAY_ACCORDING_TO_GIVEN_INDEXES_1 | java-cpp | static void reorder ( ) {
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
while ( index [ i ] != i ) {
int oldTargetI = index [ index [ i ] ] ;
char oldTargetE = ( char ) arr [ index [ i ] ] ;
arr [ index [ i ] ] = arr [ i ] ;
index [ index [ i ] ] = index [ i ] ;
index [ i ] = old... | void reorder ( int arr [ ] , int index [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( index [ i ] != i ) {
int oldTargetI = index [ index [ i ] ] ;
char oldTargetE = arr [ index [ i ] ] ;
arr [ index [ i ] ] = arr [ i ] ;
index [ index [ i ] ] = index [ i ] ;
index [ ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void reorder ( ) {
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
while ( index [ i ] != i ) {
int oldTargetI = index [ index [ i ] ] ;
char oldTargetE = ( char ) arr [ index [ i ] ] ;
arr [ index [ i ] ] = arr [ i ] ;
index [ index [ i ] ] = index [ i ] ;
index [ i ... |
java-cpp-LENGTH_LONGEST_SUB_STRING_CAN_MAKE_REMOVED | java-cpp | static int longestNull ( String str ) {
ArrayList < Pair > arr = new ArrayList < > ( ) ;
arr . add ( new Pair ( ' @ ' , - 1 ) ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
++ i ) {
arr . add ( new Pair ( str . charAt ( i ) , i ) ) ;
while ( arr . size ( ) >= 3 && arr . get ( arr . si... | int longestNull ( string str ) {
vector < pair < char , int >> arr ;
arr . push_back ( {
'@' , - 1 }
) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
++ i ) {
arr . push_back ( {
str [ i ] , i }
) ;
while ( arr . size ( ) >= 3 && arr [ arr . size ( ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int longestNull ( String str ) {
ArrayList < Pair > arr = new ArrayList < > ( ) ;
arr . add ( new Pair ( ' @ ' , - 1 ) ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
++ i ) {
arr . add ( new Pair ( str . charAt ( i ) , i ) ) ;
while ( arr . size ( ) >= 3 && arr . get ( a... |
java-cpp-SUM_FIBONACCI_NUMBERS | java-cpp | static int calculateSum ( int n ) {
if ( n <= 0 ) return 0 ;
int fibo [ ] = new int [ n + 1 ] ;
fibo [ 0 ] = 0 ;
fibo [ 1 ] = 1 ;
int sum = fibo [ 0 ] + fibo [ 1 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ;
sum += fibo [ i ] ;
}
return sum ;
}
| int calculateSum ( int n ) {
if ( n <= 0 ) return 0 ;
int fibo [ n + 1 ] ;
fibo [ 0 ] = 0 , fibo [ 1 ] = 1 ;
int sum = fibo [ 0 ] + fibo [ 1 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ;
sum += fibo [ i ] ;
}
return sum ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int calculateSum ( int n ) {
if ( n <= 0 ) return 0 ;
int fibo [ ] = new int [ n + 1 ] ;
fibo [ 0 ] = 0 ;
fibo [ 1 ] = 1 ;
int sum = fibo [ 0 ] + fibo [ 1 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ;
sum += fibo [ i ] ;
}
return sum ;
}... |
java-cpp-SHORTEST_PATH_EXACTLY_K_EDGES_DIRECTED_WEIGHTED_GRAPH | java-cpp | int shortestPath ( int graph [ ] [ ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 0 ;
if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ;
if ( k <= 0 ) return INF ;
int res = INF ;
for ( int i = 0 ;
i < V ;
i ++ ) {
if ( graph [ u ] [ i ] != INF && u != i && v != i ) {
... | INT_MAX int shortestPath ( int graph [ ] [ V ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 0 ;
if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ;
if ( k <= 0 ) return INF ;
int res = INF ;
for ( int i = 0 ;
i < V ;
i ++ ) {
if ( graph [ u ] [ i ] != INF && u != i && v ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int shortestPath ( int graph [ ] [ ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 0 ;
if ( k == 1 && graph [ u ] [ v ] != INF ) return graph [ u ] [ v ] ;
if ( k <= 0 ) return INF ;
int res = INF ;
for ( int i = 0 ;
i < V ;
i ++ ) {
if ( graph [ u ] [ i ] != INF && u != i && v != ... |
java-cpp-SUM_SQUARES_BINOMIAL_COEFFICIENTS | java-cpp | static int sumofsquare ( int n ) {
int [ ] [ ] C = new int [ 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 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int sumofsquare ( int n ) {
int [ ] [ ] C = new int [ 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 ] ;
... |
java-cpp-CONSTRUCT_ARRAY_PAIR_SUM_ARRAY | java-cpp | static 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 ] ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ] ;
}
</Code> |
java-cpp-FIND_PATTERNS_101_GIVEN_STRING | java-cpp | static int patternCount ( String str ) {
char last = str . charAt ( 0 ) ;
int i = 1 , counter = 0 ;
while ( i < str . length ( ) ) {
if ( str . charAt ( i ) == ' 0 ' && last == ' 1 ' ) {
while ( str . charAt ( i ) == ' 0 ' ) i ++ ;
if ( str . charAt ( i ) == ' 1 ' ) counter ++ ;
}
last = s... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int patternCount ( String str ) {
char last = str . charAt ( 0 ) ;
int i = 1 , counter = 0 ;
while ( i < str . length ( ) ) {
if ( str . charAt ( i ) == ' 0 ' && last == ' 1 ' ) {
while ( str . charAt ( i ) == ' 0 ' ) i ++ ;
if ( str . charAt ( i ) == ' 1 ' ) counter ++ ;
}
l... |
java-cpp-RANGE_QUERIES_FOR_FREQUENCIES_OF_ARRAY_ELEMENTS | java-cpp | public static 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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static 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 ;
}
</Code> |
java-cpp-FINDING_VERTEX_FOCUS_DIRECTRIX_PARABOLA | java-cpp | static void parabola ( float a , float b , float c ) {
System . out . println ( "Vertex: (" + ( - b / ( 2 * a ) ) + ", " + ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) + ")" ) ;
System . out . println ( "Focus: (" + ( - b / ( 2 * a ) ) + ", " + ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) + ")" ) ;
System .... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void parabola ( float a , float b , float c ) {
System . out . println ( "Vertex: (" + ( - b / ( 2 * a ) ) + ", " + ( ( ( 4 * a * c ) - ( b * b ) ) / ( 4 * a ) ) + ")" ) ;
System . out . println ( "Focus: (" + ( - b / ( 2 * a ) ) + ", " + ( ( ( 4 * a * c ) - ( b * b ) + 1 ) / ( 4 * a ) ) + ")" ) ;
S... |
java-cpp-MINIMUM_SUM_PRODUCT_TWO_ARRAYS | java-cpp | static int minproduct ( int a [ ] , int b [ ] , int n , int k ) {
int diff = 0 , res = 0 ;
int temp = 0 ;
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 ) t... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minproduct ( int a [ ] , int b [ ] , int n , int k ) {
int diff = 0 , res = 0 ;
int temp = 0 ;
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 ] ... |
java-cpp-LONGEST_SUBSEQUENCE_WHERE_EVERY_CHARACTER_APPEARS_AT_LEAST_K_TIMES | java-cpp | static void longestSubseqWithK ( String str , int k ) {
int n = str . length ( ) ;
int freq [ ] = new int [ MAX_CHARS ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
freq [ str . charAt ( i ) - ' a ' ] ++ ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( freq [ str . charAt ( i ) - ' a ' ] >= k ) {
Sys... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void longestSubseqWithK ( String str , int k ) {
int n = str . length ( ) ;
int freq [ ] = new int [ MAX_CHARS ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
freq [ str . charAt ( i ) - ' a ' ] ++ ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( freq [ str . charAt ( i ) - ' a ' ] >= k ) {
... |
java-cpp-GIVEN_A_SORTED_AND_ROTATED_ARRAY_FIND_IF_THERE_IS_A_PAIR_WITH_A_GIVEN_SUM_1 | java-cpp | static 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 ) % ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 +... |
java-cpp-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_2 | java-cpp | public static 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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static 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 ;
}
</Code> |
java-cpp-NEXT_POWER_OF_2 | java-cpp | static int nextPowerOf2 ( int n ) {
int count = 0 ;
if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int nextPowerOf2 ( int n ) {
int count = 0 ;
if ( n > 0 && ( n & ( n - 1 ) ) == 0 ) return n ;
while ( n != 0 ) {
n >>= 1 ;
count += 1 ;
}
return 1 << count ;
}
</Code> |
java-cpp-MAXIMUM_SUBARRAY_SUM_ARRAY_CREATED_REPEATED_CONCATENATION | java-cpp | static int maxSubArraySumRepeated ( int a [ ] , int n , int k ) {
int max_so_far = 0 ;
int 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_her... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxSubArraySumRepeated ( int a [ ] , int n , int k ) {
int max_so_far = 0 ;
int 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_end... |
java-cpp-STACK_SET_3_REVERSE_STRING_USING_STACK | java-cpp | static void reverse ( char str [ ] ) {
int n = str . length , i ;
for ( i = 0 ;
i < n / 2 ;
i ++ ) {
swap ( str , i , n - i - 1 ) ;
}
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void reverse ( char str [ ] ) {
int n = str . length , i ;
for ( i = 0 ;
i < n / 2 ;
i ++ ) {
swap ( str , i , n - i - 1 ) ;
}
}
</Code> |
java-cpp-LEXICOGRAPHICALLY_NEXT_STRING | java-cpp | public static String nextWord ( String str ) {
if ( str == "" ) return "a" ;
int i = str . length ( ) - 1 ;
while ( str . charAt ( i ) == ' z ' && i >= 0 ) i -- ;
if ( i == - 1 ) str = str + ' a ' ;
else str = str . substring ( 0 , i ) + ( char ) ( ( int ) ( str . charAt ( i ) ) + 1 ) + str . substring ( i + ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static String nextWord ( String str ) {
if ( str == "" ) return "a" ;
int i = str . length ( ) - 1 ;
while ( str . charAt ( i ) == ' z ' && i >= 0 ) i -- ;
if ( i == - 1 ) str = str + ' a ' ;
else str = str . substring ( 0 , i ) + ( char ) ( ( int ) ( str . charAt ( i ) ) + 1 ) + str . substring... |
java-cpp-SUM_BINOMIAL_COEFFICIENTS | java-cpp | static int binomialCoeffSum ( int n ) {
int C [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= Math . 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 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int binomialCoeffSum ( int n ) {
int C [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= Math . 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... |
java-cpp-CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD | java-cpp | public static boolean isEven ( int n ) {
return ( n % 2 == 0 ) ;
}
| bool isEven ( int n ) {
return ( n % 2 == 0 ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static boolean isEven ( int n ) {
return ( n % 2 == 0 ) ;
}
</Code> |
java-cpp-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY | java-cpp | static 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 ) ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ) ,... |
java-cpp-MAXIMUM_AND_MINIMUM_IN_A_SQUARE_MATRIX | java-cpp | static void maxMin ( int arr [ ] [ ] , int n ) {
int min = + 2147483647 ;
int max = - 2147483648 ;
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 - ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void maxMin ( int arr [ ] [ ] , int n ) {
int min = + 2147483647 ;
int max = - 2147483648 ;
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 ]... |
java-cpp-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO_1 | java-cpp | static boolean isPowerOfTwo ( int x ) {
return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ;
}
| int bool isPowerOfTwo ( int x ) {
return x && ( ! ( x & ( x - 1 ) ) ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isPowerOfTwo ( int x ) {
return x != 0 && ( ( x & ( x - 1 ) ) == 0 ) ;
}
</Code> |
java-cpp-COUNTS_PATHS_POINT_REACH_ORIGIN | java-cpp | static int countPaths ( int n , int m ) {
if ( n == 0 || 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countPaths ( int n , int m ) {
if ( n == 0 || m == 0 ) return 1 ;
return ( countPaths ( n - 1 , m ) + countPaths ( n , m - 1 ) ) ;
}
</Code> |
java-cpp-FIND_RECTANGLE_BINARY_MATRIX_CORNERS_1_1 | java-cpp | static boolean isRectangle ( int matrix [ ] [ ] ) {
int rows = matrix . length ;
if ( rows == 0 ) return false ;
int columns = matrix [ 0 ] . length ;
HashMap < Integer , HashSet < Integer >> table = new HashMap < > ( ) ;
for ( int i = 0 ;
i < rows ;
i ++ ) {
for ( int j = 0 ;
j < columns - 1 ;
... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isRectangle ( int matrix [ ] [ ] ) {
int rows = matrix . length ;
if ( rows == 0 ) return false ;
int columns = matrix [ 0 ] . length ;
HashMap < Integer , HashSet < Integer >> table = new HashMap < > ( ) ;
for ( int i = 0 ;
i < rows ;
i ++ ) {
for ( int j = 0 ;
j < columns -... |
java-cpp-PROGRAM_DECIMAL_BINARY_CONVERSION | java-cpp | static void decToBinary ( int n ) {
int [ ] binaryNum = new int [ 32 ] ;
int i = 0 ;
while ( n > 0 ) {
binaryNum [ i ] = n % 2 ;
n = n / 2 ;
i ++ ;
}
for ( int j = i - 1 ;
j >= 0 ;
j -- ) System . out . print ( binaryNum [ j ] ) ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void decToBinary ( int n ) {
int [ ] binaryNum = new int [ 32 ] ;
int i = 0 ;
while ( n > 0 ) {
binaryNum [ i ] = n % 2 ;
n = n / 2 ;
i ++ ;
}
for ( int j = i - 1 ;
j >= 0 ;
j -- ) System . out . print ( binaryNum [ j ] ) ;
}
</Code> |
java-cpp-FRIENDS_PAIRING_PROBLEM_1 | java-cpp | static 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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ;
}
</Code> |
java-cpp-MAXIMUM_SUBARRAY_SUM_USING_PREFIX_SUM | java-cpp | static int maximumSumSubarray ( int arr [ ] , int n ) {
int min_prefix_sum = 0 ;
int res = Integer . MIN_VALUE ;
int prefix_sum [ ] = new int [ 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 ++ )... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maximumSumSubarray ( int arr [ ] , int n ) {
int min_prefix_sum = 0 ;
int res = Integer . MIN_VALUE ;
int prefix_sum [ ] = new int [ 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 ;
... |
java-cpp-NUMBER_SUBSEQUENCES_AB_STRING_REPEATED_K_TIMES | java-cpp | static int countOccurrences ( String s , int K ) {
int n = s . length ( ) ;
int C = 0 , c1 = 0 , c2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s . charAt ( i ) == ' a ' ) c1 ++ ;
if ( s . charAt ( i ) == ' b ' ) {
c2 ++ ;
C += c1 ;
}
}
return C * K + ( K * ( K - 1 ) / 2 ) * c1 *... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countOccurrences ( String s , int K ) {
int n = s . length ( ) ;
int C = 0 , c1 = 0 , c2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s . charAt ( i ) == ' a ' ) c1 ++ ;
if ( s . charAt ( i ) == ' b ' ) {
c2 ++ ;
C += c1 ;
}
}
return C * K + ( K * ( K - 1 ) / 2 )... |
java-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_13_NOT | java-cpp | static boolean checkDivisibility ( String num ) {
int length = num . length ( ) ;
if ( length == 1 && num . charAt ( 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 ( ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean checkDivisibility ( String num ) {
int length = num . length ( ) ;
if ( length == 1 && num . charAt ( 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 ;
... |
java-cpp-NUMBER_UNIQUE_RECTANGLES_FORMED_USING_N_UNIT_SQUARES | java-cpp | static int countRect ( int n ) {
int ans = 0 ;
for ( int length = 1 ;
length <= Math . sqrt ( n ) ;
++ length ) for ( int height = length ;
height * length <= n ;
++ height ) ans ++ ;
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countRect ( int n ) {
int ans = 0 ;
for ( int length = 1 ;
length <= Math . sqrt ( n ) ;
++ length ) for ( int height = length ;
height * length <= n ;
++ height ) ans ++ ;
return ans ;
}
</Code> |
java-cpp-COUNT_SET_BITS_IN_AN_INTEGER_2 | java-cpp | static int countSetBits ( int n ) {
int count = 0 ;
while ( n > 0 ) {
n &= ( n - 1 ) ;
count ++ ;
}
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countSetBits ( int n ) {
int count = 0 ;
while ( n > 0 ) {
n &= ( n - 1 ) ;
count ++ ;
}
return count ;
}
</Code> |
java-cpp-LONGEST_SUBARRAY_COUNT_1S_ONE_COUNT_0S | java-cpp | static int lenOfLongSubarr ( int arr [ ] , int n ) {
HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ;
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 . containsKey ( sum ) ) um... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int lenOfLongSubarr ( int arr [ ] , int n ) {
HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ;
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 . containsKey ( sum... |
java-cpp-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER_1 | java-cpp | static int factorial ( int n ) {
int res = 1 , i ;
for ( i = 2 ;
i <= n ;
i ++ ) res *= i ;
return res ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int factorial ( int n ) {
int res = 1 , i ;
for ( i = 2 ;
i <= n ;
i ++ ) res *= i ;
return res ;
}
</Code> |
java-cpp-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY | java-cpp | static int minJumps ( int arr [ ] , int l , int h ) {
if ( h == l ) return 0 ;
if ( arr [ l ] == 0 ) return Integer . MAX_VALUE ;
int min = Integer . MAX_VALUE ;
for ( int i = l + 1 ;
i <= h && i <= l + arr [ l ] ;
i ++ ) {
int jumps = minJumps ( arr , i , h ) ;
if ( jumps != Integer . MAX_VALUE && ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minJumps ( int arr [ ] , int l , int h ) {
if ( h == l ) return 0 ;
if ( arr [ l ] == 0 ) return Integer . MAX_VALUE ;
int min = Integer . MAX_VALUE ;
for ( int i = l + 1 ;
i <= h && i <= l + arr [ l ] ;
i ++ ) {
int jumps = minJumps ( arr , i , h ) ;
if ( jumps != Integer . MAX_VA... |
java-cpp-MINIMUM_TIME_WRITE_CHARACTERS_USING_INSERT_DELETE_COPY_OPERATION | java-cpp | static int minTimeForWritingChars ( int N , int insert , int remove , int copy ) {
if ( N == 0 ) return 0 ;
if ( N == 1 ) return insert ;
int dp [ ] = new int [ N + 1 ] ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
if ( i % 2 == 0 ) dp [ i ] = Math . min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ;
els... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minTimeForWritingChars ( int N , int insert , int remove , int copy ) {
if ( N == 0 ) return 0 ;
if ( N == 1 ) return insert ;
int dp [ ] = new int [ N + 1 ] ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
if ( i % 2 == 0 ) dp [ i ] = Math . min ( dp [ i - 1 ] + insert , dp [ i / 2 ] + copy ) ;
... |
java-cpp-PRINTING_STRING_PLUS_PATTERN_MATRIX | java-cpp | static void carveCross ( String str ) {
int n = str . length ( ) ;
if ( n % 2 == 0 ) {
System . out . print ( "Not possible. Please enter " + "odd length string.\n" ) ;
}
else {
char arr [ ] [ ] = new char [ max ] [ max ] ;
int m = n / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( i... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void carveCross ( String str ) {
int n = str . length ( ) ;
if ( n % 2 == 0 ) {
System . out . print ( "Not possible. Please enter " + "odd length string.\n" ) ;
}
else {
char arr [ ] [ ] = new char [ max ] [ max ] ;
int m = n / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
... |
java-cpp-SUBARRAYS_DISTINCT_ELEMENTS | java-cpp | public static int sumoflength ( int [ ] arr , int n ) {
Set < Integer > s = new HashSet < > ( ) ;
int j = 0 , ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < n && ! s . contains ( arr [ j ] ) ) {
s . add ( arr [ i ] ) ;
j ++ ;
}
ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ;
s... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int sumoflength ( int [ ] arr , int n ) {
Set < Integer > s = new HashSet < > ( ) ;
int j = 0 , ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( j < n && ! s . contains ( arr [ j ] ) ) {
s . add ( arr [ i ] ) ;
j ++ ;
}
ans += ( ( j - i ) * ( j - i + 1 ) ) / 2 ... |
java-cpp-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_1 | java-cpp | static void findTriplets ( int arr [ ] , int n ) {
boolean found = false ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
HashSet < Integer > s = new HashSet < Integer > ( ) ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int x = - ( arr [ i ] + arr [ j ] ) ;
if ( s . contains ( x ) ) {
Sys... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void findTriplets ( int arr [ ] , int n ) {
boolean found = false ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
HashSet < Integer > s = new HashSet < Integer > ( ) ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int x = - ( arr [ i ] + arr [ j ] ) ;
if ( s . contains ( x ) ) {
... |
java-cpp-MAXIMUM_LENGTH_PREFIX_ONE_STRING_OCCURS_SUBSEQUENCE_ANOTHER | java-cpp | static int maxPrefix ( String s , String t ) {
int count = 0 ;
for ( int i = 0 ;
i < t . length ( ) ;
i ++ ) {
if ( count == t . length ( ) ) break ;
if ( t . charAt ( i ) == s . charAt ( count ) ) count ++ ;
}
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxPrefix ( String s , String t ) {
int count = 0 ;
for ( int i = 0 ;
i < t . length ( ) ;
i ++ ) {
if ( count == t . length ( ) ) break ;
if ( t . charAt ( i ) == s . charAt ( count ) ) count ++ ;
}
return count ;
}
</Code> |
java-cpp-MOVE_VE_ELEMENTS_END_ORDER_EXTRA_SPACE_ALLOWED | java-cpp | static void segregateElements ( int arr [ ] , int n ) {
int temp [ ] = new int [ 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 ] ;
for... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void segregateElements ( int arr [ ] , int n ) {
int temp [ ] = new int [ 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 ] ... |
java-cpp-MERGING_INTERVALS | java-cpp | public static void mergeIntervals ( Interval arr [ ] ) {
Arrays . sort ( arr , new Comparator < Interval > ( ) {
public int compare ( Interval i1 , Interval i2 ) {
return i2 . start - i1 . start ;
}
}
) ;
int index = 0 ;
for ( int i = 1 ;
i < arr . length ;
i ++ ) {
if ( arr [ index ] . ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static void mergeIntervals ( Interval arr [ ] ) {
Arrays . sort ( arr , new Comparator < Interval > ( ) {
public int compare ( Interval i1 , Interval i2 ) {
return i2 . start - i1 . start ;
}
}
) ;
int index = 0 ;
for ( int i = 1 ;
i < arr . length ;
i ++ ) {
if ( arr [ ind... |
java-cpp-PROGRAM_FIND_CORRELATION_COEFFICIENT | java-cpp | static 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 = sq... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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... |
java-cpp-MAXIMUM_AREA_RECTANGLE_PICKING_FOUR_SIDES_ARRAY | java-cpp | static int findArea ( Integer arr [ ] , int n ) {
Arrays . sort ( arr , Collections . reverseOrder ( ) ) ;
int [ ] dimension = {
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 ] * di... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findArea ( Integer arr [ ] , int n ) {
Arrays . sort ( arr , Collections . reverseOrder ( ) ) ;
int [ ] dimension = {
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... |
java-cpp-MINIMUM_INCREMENT_K_OPERATIONS_MAKE_ELEMENTS_EQUAL | java-cpp | static int minOps ( int arr [ ] , int n , int k ) {
Arrays . sort ( arr ) ;
int max = arr [ arr . length - 1 ] ;
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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minOps ( int arr [ ] , int n , int k ) {
Arrays . sort ( arr ) ;
int max = arr [ arr . length - 1 ] ;
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 ;
}
</Code> |
java-cpp-SQUARE_ROOT_OF_AN_INTEGER | java-cpp | static 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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ;
}
</Code> |
java-cpp-CHECK_WHETHER_LARGE_NUMBER_DIVISIBLE_7 | java-cpp | static boolean isDivisible7 ( String num ) {
int n = num . length ( ) ;
if ( n == 0 && num . charAt ( 0 ) == ' 0 ' ) return true ;
if ( n % 3 == 1 ) num = "00" + num ;
if ( n % 3 == 2 ) num = "0" + num ;
n = num . length ( ) ;
int gSum = 0 , p = 1 ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
int grou... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isDivisible7 ( String num ) {
int n = num . length ( ) ;
if ( n == 0 && num . charAt ( 0 ) == ' 0 ' ) return true ;
if ( n % 3 == 1 ) num = "00" + num ;
if ( n % 3 == 2 ) num = "0" + num ;
n = num . length ( ) ;
int gSum = 0 , p = 1 ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
i... |
java-cpp-CONSTRUCT_GRAPH_GIVEN_DEGREES_VERTICES | java-cpp | static void printMat ( int degseq [ ] , int n ) {
int [ ] [ ] mat = new int [ n ] [ n ] ;
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 ] [ 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printMat ( int degseq [ ] , int n ) {
int [ ] [ ] mat = new int [ n ] [ n ] ;
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 ] [ j ] =... |
java-cpp-MINIMUM_TIME_REQUIRED_PRODUCE_M_ITEMS | java-cpp | static int minTime ( int [ ] arr , int n , int m ) {
int t = 0 ;
while ( true ) {
int items = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) items += ( t / arr [ i ] ) ;
if ( items >= m ) return t ;
t ++ ;
}
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minTime ( int [ ] arr , int n , int m ) {
int t = 0 ;
while ( true ) {
int items = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) items += ( t / arr [ i ] ) ;
if ( items >= m ) return t ;
t ++ ;
}
}
</Code> |
java-cpp-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX_1 | java-cpp | static int MaximumDecimalValue ( int mat [ ] [ ] , int n ) {
int dp [ ] [ ] = new int [ n ] [ n ] ;
if ( mat [ 0 ] [ 0 ] == 1 ) {
dp [ 0 ] [ 0 ] = 1 ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( mat [ 0 ] [ i ] == 1 ) {
dp [ 0 ] [ i ] = ( int ) ( dp [ 0 ] [ i - 1 ] + Math . pow ( 2 , i ) ) ;
... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int MaximumDecimalValue ( int mat [ ] [ ] , int n ) {
int dp [ ] [ ] = new int [ n ] [ n ] ;
if ( mat [ 0 ] [ 0 ] == 1 ) {
dp [ 0 ] [ 0 ] = 1 ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( mat [ 0 ] [ i ] == 1 ) {
dp [ 0 ] [ i ] = ( int ) ( dp [ 0 ] [ i - 1 ] + Math . pow ( 2 , i ) ... |
java-cpp-CHECK_DIVISIBILITY_LARGE_NUMBER_999 | java-cpp | static boolean isDivisible999 ( String num ) {
int n = num . length ( ) ;
if ( n == 0 && num . charAt ( 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 . charAt ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isDivisible999 ( String num ) {
int n = num . length ( ) ;
if ( n == 0 && num . charAt ( 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 . ... |
java-cpp-MAXIMUM_SUM_BITONIC_SUBARRAY | java-cpp | static int maxSumBitonicSubArr ( int arr [ ] , int n ) {
int [ ] msis = new int [ n ] ;
int [ ] msds = new int [ n ] ;
int max_sum = Integer . MIN_VALUE ;
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 ] ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxSumBitonicSubArr ( int arr [ ] , int n ) {
int [ ] msis = new int [ n ] ;
int [ ] msds = new int [ n ] ;
int max_sum = Integer . MIN_VALUE ;
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... |
java-cpp-FIND_MINIMUM_NUMBER_DIVIDED_MAKE_NUMBER_PERFECT_SQUARE | java-cpp | static int findMinNumber ( int n ) {
int count = 0 , ans = 1 ;
while ( n % 2 == 0 ) {
count ++ ;
n /= 2 ;
}
if ( count % 2 == 1 ) ans *= 2 ;
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i += 2 ) {
count = 0 ;
while ( n % i == 0 ) {
count ++ ;
n /= i ;
}
if ( count % 2 == ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMinNumber ( int n ) {
int count = 0 , ans = 1 ;
while ( n % 2 == 0 ) {
count ++ ;
n /= 2 ;
}
if ( count % 2 == 1 ) ans *= 2 ;
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i += 2 ) {
count = 0 ;
while ( n % i == 0 ) {
count ++ ;
n /= i ;
}
if ( count ... |
java-cpp-GCD_ELEMENTS_GIVEN_RANGE | java-cpp | static int rangeGCD ( int n , int m ) {
return ( n == m ) ? n : 1 ;
}
| int rangeGCD ( int n , int m ) {
return ( n == m ) ? n : 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int rangeGCD ( int n , int m ) {
return ( n == m ) ? n : 1 ;
}
</Code> |
java-cpp-PROGRAMMING_PUZZLE_ASSIGN_VALUE_WITHOUT_CONTROL_STATEMENT | java-cpp | static int assignValue ( int a , int b , int x ) {
int 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int assignValue ( int a , int b , int x ) {
int arr [ ] = {
a , b }
;
return ( arr [ x ] ) ;
}
</Code> |
java-cpp-TRIANGULAR_NUMBERS | java-cpp | static boolean 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 ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean 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 ;
}
</Code> |
java-cpp-PANGRAM_CHECKING | java-cpp | public static boolean checkPangram ( String str ) {
boolean [ ] mark = new boolean [ 26 ] ;
int index = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( ' A ' <= str . charAt ( i ) && str . charAt ( i ) <= ' Z ' ) index = str . charAt ( i ) - ' A ' ;
else if ( ' a ' <= str . charAt ( i ) && ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static boolean checkPangram ( String str ) {
boolean [ ] mark = new boolean [ 26 ] ;
int index = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( ' A ' <= str . charAt ( i ) && str . charAt ( i ) <= ' Z ' ) index = str . charAt ( i ) - ' A ' ;
else if ( ' a ' <= str . charAt ( ... |
java-cpp-INTERPOLATION_SEARCH | java-cpp | static int interpolationSearch ( int x ) {
int lo = 0 , hi = ( arr . length - 1 ) ;
while ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) {
if ( lo == hi ) {
if ( arr [ lo ] == x ) return lo ;
return - 1 ;
}
int pos = lo + ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int interpolationSearch ( int x ) {
int lo = 0 , hi = ( arr . length - 1 ) ;
while ( lo <= hi && x >= arr [ lo ] && x <= arr [ hi ] ) {
if ( lo == hi ) {
if ( arr [ lo ] == x ) return lo ;
return - 1 ;
}
int pos = lo + ( ( ( hi - lo ) / ( arr [ hi ] - arr [ lo ] ) ) * ( x - arr... |
java-cpp-FITTING_SHELVES_PROBLEM | java-cpp | static 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 ;
wal... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ;
... |
java-cpp-FIND_REPETITIVE_ELEMENT_1_N_1_3 | java-cpp | static int findRepeating ( int arr [ ] , int n ) {
int missingElement = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int element = arr [ Math . abs ( arr [ i ] ) ] ;
if ( element < 0 ) {
missingElement = arr [ i ] ;
break ;
}
arr [ Math . abs ( arr [ i ] ) ] = - arr [ Math . abs ( arr [ i ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findRepeating ( int arr [ ] , int n ) {
int missingElement = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int element = arr [ Math . abs ( arr [ i ] ) ] ;
if ( element < 0 ) {
missingElement = arr [ i ] ;
break ;
}
arr [ Math . abs ( arr [ i ] ) ] = - arr [ Math . abs ( a... |
java-cpp-K_LARGESTOR_SMALLEST_ELEMENTS_IN_AN_ARRAY | java-cpp | public static void kLargest ( Integer [ ] arr , int k ) {
Arrays . sort ( arr , Collections . reverseOrder ( ) ) ;
for ( int i = 0 ;
i < k ;
i ++ ) System . out . print ( arr [ i ] + " " ) ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static void kLargest ( Integer [ ] arr , int k ) {
Arrays . sort ( arr , Collections . reverseOrder ( ) ) ;
for ( int i = 0 ;
i < k ;
i ++ ) System . out . print ( arr [ i ] + " " ) ;
}
</Code> |
java-cpp-DYNAMIC_PROGRAMMING_SET_10_0_1_KNAPSACK_PROBLEM_1 | java-cpp | static int knapSack ( int W , int wt [ ] , int val [ ] , int n ) {
int i , w ;
int K [ ] [ ] = new int [ 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 ( v... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int knapSack ( int W , int wt [ ] , int val [ ] , int n ) {
int i , w ;
int K [ ] [ ] = new int [ 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 ] = ... |
java-cpp-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_2 | java-cpp | static boolean isPowerOfFour ( int n ) {
return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ( n & 0xAAAAAAAA ) == 0 ;
}
| bool isPowerOfFour ( unsigned int n ) {
return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ! ( n & 0xAAAAAAAA ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isPowerOfFour ( int n ) {
return n != 0 && ( ( n & ( n - 1 ) ) == 0 ) && ( n & 0xAAAAAAAA ) == 0 ;
}
</Code> |
java-cpp-C_PROGRAM_FACTORIAL_NUMBER | java-cpp | static int factorial ( int n ) {
if ( n == 0 ) return 1 ;
return 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int factorial ( int n ) {
if ( n == 0 ) return 1 ;
return n * factorial ( n - 1 ) ;
}
</Code> |
java-cpp-COUNT_POSSIBLE_GROUPS_SIZE_2_3_SUM_MULTIPLE_3 | java-cpp | int findgroups ( int arr [ ] , int n ) {
int c [ ] = new int [ ] {
0 , 0 , 0 }
;
int 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 ]... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int findgroups ( int arr [ ] , int n ) {
int c [ ] = new int [ ] {
0 , 0 , 0 }
;
int 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 ) * ( ... |
java-cpp-K_TH_LARGEST_SUM_CONTIGUOUS_SUBARRAY | java-cpp | static int kthLargestSum ( int arr [ ] , int n , int k ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
sum [ 1 ] = arr [ 0 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ;
PriorityQueue < Integer > Q = new PriorityQueue < Integer > ( ) ;
for ( int i = 1 ;
i <= n ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int kthLargestSum ( int arr [ ] , int n , int k ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
sum [ 1 ] = arr [ 0 ] ;
for ( int i = 2 ;
i <= n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i - 1 ] ;
PriorityQueue < Integer > Q = new PriorityQueue < Integer > ( ) ;
for ( int i = 1 ;
... |
java-cpp-FIND_A_SPECIFIC_PAIR_IN_MATRIX_1 | java-cpp | static int findMaxValue ( int N , int mat [ ] [ ] ) {
int maxValue = Integer . MIN_VALUE ;
int maxArr [ ] [ ] = new int [ 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 ) ma... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMaxValue ( int N , int mat [ ] [ ] ) {
int maxValue = Integer . MIN_VALUE ;
int maxArr [ ] [ ] = new int [ 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 ] > ma... |
java-cpp-K_TH_PRIME_FACTOR_GIVEN_NUMBER | java-cpp | static int kPrimeFactor ( int n , int k ) {
while ( n % 2 == 0 ) {
k -- ;
n = n / 2 ;
if ( k == 0 ) return 2 ;
}
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i = i + 2 ) {
while ( n % i == 0 ) {
if ( k == 1 ) return i ;
k -- ;
n = n / i ;
}
}
if ( n > 2 && k == 1 ) retu... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int kPrimeFactor ( int n , int k ) {
while ( n % 2 == 0 ) {
k -- ;
n = n / 2 ;
if ( k == 0 ) return 2 ;
}
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i = i + 2 ) {
while ( n % i == 0 ) {
if ( k == 1 ) return i ;
k -- ;
n = n / i ;
}
}
if ( n > 2 && k == 1... |
java-cpp-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_2 | java-cpp | static void printDistinct ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
if ( ! set . contains ( arr [ i ] ) ) {
set . add ( arr [ i ] ) ;
System . out . print ( arr [ i ] + " " ) ;
}
}
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printDistinct ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
if ( ! set . contains ( arr [ i ] ) ) {
set . add ( arr [ i ] ) ;
System . out . print ( arr [ i ] + " " ) ;
}
}
}
</Code> |
java-cpp-PROGRAM_DECIMAL_BINARY_CONVERSION_1 | java-cpp | public void decToBinary ( int n ) {
for ( int i = 31 ;
i >= 0 ;
i -- ) {
int k = n >> i ;
if ( ( k & 1 ) > 0 ) System . out . print ( "1" ) ;
else System . out . print ( "0" ) ;
}
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public void decToBinary ( int n ) {
for ( int i = 31 ;
i >= 0 ;
i -- ) {
int k = n >> i ;
if ( ( k & 1 ) > 0 ) System . out . print ( "1" ) ;
else System . out . print ( "0" ) ;
}
}
</Code> |
java-cpp-SUPER_PRIME | java-cpp | static void SieveOfEratosthenes ( int n , boolean 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 [ ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void SieveOfEratosthenes ( int n , boolean 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 ) isP... |
java-cpp-COUNT_OPERATIONS_MAKE_STRINGAB_FREE | java-cpp | static int abFree ( char [ ] s ) {
int b_count = 0 ;
int res = 0 ;
for ( int i = 0 ;
i < s . length ;
i ++ ) {
if ( s [ s . length - i - 1 ] == ' 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int abFree ( char [ ] s ) {
int b_count = 0 ;
int res = 0 ;
for ( int i = 0 ;
i < s . length ;
i ++ ) {
if ( s [ s . length - i - 1 ] == ' a ' ) {
res = ( res + b_count ) ;
b_count = ( b_count * 2 ) ;
}
else {
b_count += 1 ;
}
}
return res ;
}
</Code> |
java-cpp-RECURSIVE_SOLUTION_COUNT_SUBSTRINGS_FIRST_LAST_CHARACTERS | java-cpp | static 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 . charAt ( i ) == str . charAt ( j ) ) res ++... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 . charAt ( i ) == str . charAt ( j ) )... |
java-cpp-FIND_THE_FIRST_MISSING_NUMBER | java-cpp | 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 ) ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
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... |
java-cpp-PROGRAM_FOR_FACTORIAL_OF_A_NUMBER | java-cpp | static int factorial ( int n ) {
if ( n == 0 ) return 1 ;
return 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int factorial ( int n ) {
if ( n == 0 ) return 1 ;
return n * factorial ( n - 1 ) ;
}
</Code> |
java-cpp-MINIMUM_ROTATIONS_UNLOCK_CIRCULAR_LOCK | java-cpp | static int minRotation ( int input , int unlock_code ) {
int rotation = 0 ;
int input_digit , code_digit ;
while ( input > 0 || unlock_code > 0 ) {
input_digit = input % 10 ;
code_digit = unlock_code % 10 ;
rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( input_digi... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minRotation ( int input , int unlock_code ) {
int rotation = 0 ;
int input_digit , code_digit ;
while ( input > 0 || unlock_code > 0 ) {
input_digit = input % 10 ;
code_digit = unlock_code % 10 ;
rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( inp... |
java-cpp-LARGEST_SUBSET_WHOSE_ALL_ELEMENTS_ARE_FIBONACCI_NUMBERS | java-cpp | public static void findFibSubset ( Integer [ ] x ) {
Integer max = Collections . max ( Arrays . asList ( x ) ) ;
List < Integer > fib = new ArrayList < Integer > ( ) ;
List < Integer > result = new ArrayList < Integer > ( ) ;
Integer a = 0 ;
Integer b = 1 ;
while ( b < max ) {
Integer c = a + b ;
a ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static void findFibSubset ( Integer [ ] x ) {
Integer max = Collections . max ( Arrays . asList ( x ) ) ;
List < Integer > fib = new ArrayList < Integer > ( ) ;
List < Integer > result = new ArrayList < Integer > ( ) ;
Integer a = 0 ;
Integer b = 1 ;
while ( b < max ) {
Integer c = a + b ;... |
java-cpp-MARKOV_MATRIX | java-cpp | static boolean checkMarkov ( double m [ ] [ ] ) {
for ( int i = 0 ;
i < m . length ;
i ++ ) {
double sum = 0 ;
for ( int j = 0 ;
j < m [ i ] . length ;
j ++ ) sum = sum + m [ i ] [ j ] ;
if ( sum != 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean checkMarkov ( double m [ ] [ ] ) {
for ( int i = 0 ;
i < m . length ;
i ++ ) {
double sum = 0 ;
for ( int j = 0 ;
j < m [ i ] . length ;
j ++ ) sum = sum + m [ i ] [ j ] ;
if ( sum != 1 ) return false ;
}
return true ;
}
</Code> |
java-cpp-SEARCH_INSERT_AND_DELETE_IN_A_SORTED_ARRAY_1 | java-cpp | static 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 ) ;
}
| 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static 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 ) ;
}
</Code> |
java-cpp-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_2 | java-cpp | static void recursiveReverse ( char [ ] str , int i ) {
int n = str . length ;
if ( i == n / 2 ) return ;
swap ( str , i , n - i - 1 ) ;
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void recursiveReverse ( char [ ] str , int i ) {
int n = str . length ;
if ( i == n / 2 ) return ;
swap ( str , i , n - i - 1 ) ;
recursiveReverse ( str , i + 1 ) ;
}
</Code> |
java-cpp-CHECK_IF_ARRAY_ELEMENTS_ARE_CONSECUTIVE | java-cpp | boolean 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 ) {
boolean visited [ ] = new boolean [ n ] ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( visited [ arr [ i ] - min ] != fa... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
boolean 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 ) {
boolean visited [ ] = new boolean [ n ] ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( visited [ arr [ i ] - min ... |
java-cpp-MODULUS_TWO_FLOAT_DOUBLE_NUMBERS | java-cpp | static double findMod ( double a , double b ) {
if ( a < 0 ) a = - a ;
if ( b < 0 ) b = - b ;
double 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static double findMod ( double a , double b ) {
if ( a < 0 ) a = - a ;
if ( b < 0 ) b = - b ;
double mod = a ;
while ( mod >= b ) mod = mod - b ;
if ( a < 0 ) return - mod ;
return mod ;
}
</Code> |
java-cpp-SUM_OF_ALL_SUBSTRINGS_OF_A_STRING_REPRESENTING_A_NUMBER | java-cpp | public static int sumOfSubstrings ( String num ) {
int n = num . length ( ) ;
int sumofdigit [ ] = new int [ n ] ;
sumofdigit [ 0 ] = num . charAt ( 0 ) - ' 0 ' ;
int res = sumofdigit [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int numi = num . charAt ( i ) - ' 0 ' ;
sumofdigit [ i ] = ( i + 1 ) * ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int sumOfSubstrings ( String num ) {
int n = num . length ( ) ;
int sumofdigit [ ] = new int [ n ] ;
sumofdigit [ 0 ] = num . charAt ( 0 ) - ' 0 ' ;
int res = sumofdigit [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int numi = num . charAt ( i ) - ' 0 ' ;
sumofdigit [ i ] = ( i +... |
java-cpp-FIND_PAIR_WITH_GREATEST_PRODUCT_IN_ARRAY_1 | java-cpp | static int findGreatest ( int arr [ ] , int n ) {
Map < Integer , Integer > m = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( m . containsKey ( arr [ i ] ) ) {
m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ;
}
else {
m . put ( arr [ i ] , m . get ( arr [ i ] ) ) ;
... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findGreatest ( int arr [ ] , int n ) {
Map < Integer , Integer > m = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( m . containsKey ( arr [ i ] ) ) {
m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ;
}
else {
m . put ( arr [ i ] , m . get ( arr [ i ] ) ... |
java-cpp-FIND_THE_MISSING_NUMBER_1 | java-cpp | static int getMissingNo ( int a [ ] , int n ) {
int total = 1 ;
for ( int i = 2 ;
i <= ( n + 1 ) ;
i ++ ) {
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int getMissingNo ( int a [ ] , int n ) {
int total = 1 ;
for ( int i = 2 ;
i <= ( n + 1 ) ;
i ++ ) {
total += i ;
total -= a [ i - 2 ] ;
}
return total ;
}
</Code> |
java-cpp-NEXT_POWER_OF_2_2 | java-cpp | static int nextPowerOf2 ( int n ) {
n -- ;
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n ++ ;
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int nextPowerOf2 ( int n ) {
n -- ;
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n ++ ;
return n ;
}
</Code> |
java-cpp-C_PROGRAM_ADDITION_TWO_MATRICES | java-cpp | static void add ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) {
int i , j ;
for ( i = 0 ;
i < N ;
i ++ ) for ( j = 0 ;
j < N ;
j ++ ) 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void add ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) {
int i , j ;
for ( i = 0 ;
i < N ;
i ++ ) for ( j = 0 ;
j < N ;
j ++ ) C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ;
}
</Code> |
java-cpp-ENTRINGER_NUMBER_1 | java-cpp | static int zigzag ( int n , int k ) {
int dp [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ;
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 <= Math . min ( i , k ) ;
j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int zigzag ( int n , int k ) {
int dp [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ;
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 <= Math . min ( i , k ) ;
j ++ ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ]... |
java-cpp-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE_1 | java-cpp | static int eggDrop ( int n , int k ) {
int eggFloor [ ] [ ] = new int [ 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 ;
... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int eggDrop ( int n , int k ) {
int eggFloor [ ] [ ] = new int [ 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 <=... |
java-cpp-PRINT_A_CLOSEST_STRING_THAT_DOES_NOT_CONTAIN_ADJACENT_DUPLICATES | java-cpp | public static String noAdjacentDup ( String s1 ) {
int n = s1 . length ( ) ;
char [ ] s = s1 . toCharArray ( ) ;
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... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static String noAdjacentDup ( String s1 ) {
int n = s1 . length ( ) ;
char [ ] s = s1 . toCharArray ( ) ;
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 ] ++ ;
... |
java-cpp-MODIFY_ARRAY_TO_MAXIMIZE_SUM_OF_ADJACENT_DIFFERENCES | java-cpp | static int maximumDifferenceSum ( int arr [ ] , int N ) {
int dp [ ] [ ] = new int [ 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 ] = Math . max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + Math . abs ( 1 - arr [ ... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maximumDifferenceSum ( int arr [ ] , int N ) {
int dp [ ] [ ] = new int [ 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 ] = Math . max ( dp [ i ] [ 0 ] , dp [ i ] [ 1 ] + Math . abs ( 1 -... |
java-cpp-MINIMUM_INSERTIONS_SORT_ARRAY | java-cpp | static int minInsertionStepToSortArray ( int arr [ ] , int N ) {
int [ ] lis = new int [ 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... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minInsertionStepToSortArray ( int arr [ ] , int N ) {
int [ ] lis = new int [ 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 ... |
java-cpp-EFFICIENT_WAY_CHECK_WHETHER_N_TH_FIBONACCI_NUMBER_MULTIPLE_10 | java-cpp | static boolean isMultipleOf10 ( int n ) {
if ( n % 15 == 0 ) return true ;
return false ;
}
| bool isMultipleOf10 ( int n ) {
return ( n % 15 == 0 ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isMultipleOf10 ( int n ) {
if ( n % 15 == 0 ) return true ;
return false ;
}
</Code> |
java-cpp-HARDY_RAMANUJAN_THEOREM | java-cpp | static int exactPrimeFactorCount ( int n ) {
int count = 0 ;
if ( n % 2 == 0 ) {
count ++ ;
while ( n % 2 == 0 ) n = n / 2 ;
}
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i = i + 2 ) {
if ( n % i == 0 ) {
count ++ ;
while ( n % i == 0 ) n = n / i ;
}
}
if ( n > 2 ) count ++ ;
... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int exactPrimeFactorCount ( int n ) {
int count = 0 ;
if ( n % 2 == 0 ) {
count ++ ;
while ( n % 2 == 0 ) n = n / 2 ;
}
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i = i + 2 ) {
if ( n % i == 0 ) {
count ++ ;
while ( n % i == 0 ) n = n / i ;
}
}
if ( n > 2 ) coun... |
java-cpp-QUERIES_FOR_CHARACTERS_IN_A_REPEATED_STRING | java-cpp | static void query ( String s , int i , int j ) {
int n = s . length ( ) ;
i %= n ;
j %= n ;
if ( s . charAt ( i ) == s . charAt ( j ) ) System . out . println ( "Yes" ) ;
else System . out . println ( "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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void query ( String s , int i , int j ) {
int n = s . length ( ) ;
i %= n ;
j %= n ;
if ( s . charAt ( i ) == s . charAt ( j ) ) System . out . println ( "Yes" ) ;
else System . out . println ( "No" ) ;
}
</Code> |
java-cpp-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY_1 | java-cpp | static int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) {
return - 1 ;
}
Arrays . sort ( arr ) ;
return Math . 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) {
return - 1 ;
}
Arrays . sort ( arr ) ;
return Math . max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] ) ;
}
</Code> |
java-cpp-FIND_SUBARRAY_LEAST_AVERAGE | java-cpp | static void findMinAvgSubarray ( 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 < min_sum... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void findMinAvgSubarray ( 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 < ... |
java-cpp-COUNT_POSSIBLE_PATHS_SOURCE_DESTINATION_EXACTLY_K_EDGES | java-cpp | int countwalks ( int graph [ ] [ ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 1 ;
if ( k == 1 && graph [ u ] [ v ] == 1 ) 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 ) ;
ret... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int countwalks ( int graph [ ] [ ] , int u , int v , int k ) {
if ( k == 0 && u == v ) return 1 ;
if ( k == 1 && graph [ u ] [ v ] == 1 ) 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 ) ... |
java-cpp-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY_1 | java-cpp | static int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
HashSet < Integer > Hash = new HashSet < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
Hash . add ( arr [ i ] ) ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i ] * ar... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
HashSet < Integer > Hash = new HashSet < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
Hash . add ( arr [ i ] ) ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i... |
java-cpp-SERIES_LARGEST_GCD_SUM_EQUALS_N | java-cpp | static void print_sequence ( int n , int k ) {
int b = n / ( k * ( k + 1 ) / 2 ) ;
if ( b == 0 ) {
System . out . println ( "-1" ) ;
}
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... | 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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void print_sequence ( int n , int k ) {
int b = n / ( k * ( k + 1 ) / 2 ) ;
if ( b == 0 ) {
System . out . println ( "-1" ) ;
}
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 <=... |
java-cpp-SMALLEST_POWER_OF_2_GREATER_THAN_OR_EQUAL_TO_N_2 | java-cpp | static int nextPowerOf2 ( int n ) {
n -- ;
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n ++ ;
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 java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int nextPowerOf2 ( int n ) {
n -- ;
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n ++ ;
return n ;
}
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.