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-PROGRAM_CALCULATE_VOLUME_OCTAHEDRON | java-cpp | static double vol_of_octahedron ( double side ) {
return ( ( side * side * side ) * ( Math . sqrt ( 2 ) / 3 ) ) ;
}
| double vol_of_octahedron ( double side ) {
return ( ( side * side * side ) * ( sqrt ( 2 ) / 3 ) ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static double vol_of_octahedron ( double side ) {
return ( ( side * side * side ) * ( Math . sqrt ( 2 ) / 3 ) ) ;
}
</Code> |
java-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX | java-cpp | static int numberOfPaths ( int m , int n ) {
if ( m == 1 || n == 1 ) return 1 ;
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ;
}
| int numberOfPaths ( int m , int n ) {
if ( m == 1 || n == 1 ) return 1 ;
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int numberOfPaths ( int m , int n ) {
if ( m == 1 || n == 1 ) return 1 ;
return numberOfPaths ( m - 1 , n ) + numberOfPaths ( m , n - 1 ) ;
}
</Code> |
java-cpp-BINARY_SEARCH | java-cpp | int binarySearch ( int arr [ ] , int l , int r , int x ) {
if ( r >= l ) {
int mid = l + ( r - l ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ;
return binarySearch ( arr , mid + 1 , r , x ) ;
}
return - 1 ;
}
| int binarySearch ( int arr [ ] , int l , int r , int x ) {
if ( r >= l ) {
int mid = l + ( r - l ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ;
return binarySearch ( arr , mid + 1 , r , x ) ;
}
return - 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int binarySearch ( int arr [ ] , int l , int r , int x ) {
if ( r >= l ) {
int mid = l + ( r - l ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ;
return binarySearch ( arr , mid + 1 , r , x ) ;
}
return - 1 ;
}
</Code> |
java-cpp-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_1 | java-cpp | static int numberOfPaths ( int m , int n ) {
int count [ ] [ ] = new int [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) count [ i ] [ 0 ] = 1 ;
for ( int j = 0 ;
j < n ;
j ++ ) count [ 0 ] [ j ] = 1 ;
for ( int i = 1 ;
i < m ;
i ++ ) {
for ( int j = 1 ;
j < n ;
j ++ ) count [ i ] [ j ] =... | int numberOfPaths ( int m , int n ) {
int count [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) count [ i ] [ 0 ] = 1 ;
for ( int j = 0 ;
j < n ;
j ++ ) count [ 0 ] [ j ] = 1 ;
for ( int i = 1 ;
i < m ;
i ++ ) {
for ( int j = 1 ;
j < n ;
j ++ ) count [ i ] [ j ] = count [ i - 1 ] [ j ] + ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int numberOfPaths ( int m , int n ) {
int count [ ] [ ] = new int [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) count [ i ] [ 0 ] = 1 ;
for ( int j = 0 ;
j < n ;
j ++ ) count [ 0 ] [ j ] = 1 ;
for ( int i = 1 ;
i < m ;
i ++ ) {
for ( int j = 1 ;
j < n ;
j ++ ) count [ i ] ... |
java-cpp-NUMBER_N_DIGIT_STEPPING_NUMBERS | java-cpp | static long answer ( int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ;
if ( n == 1 ) return 10 ;
for ( int j = 0 ;
j <= 9 ;
j ++ ) dp [ 1 ] [ j ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= 9 ;
j ++ ) {
if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ;
... | long long answer ( int n ) {
int dp [ n + 1 ] [ 10 ] ;
if ( n == 1 ) return 10 ;
for ( int j = 0 ;
j <= 9 ;
j ++ ) dp [ 1 ] [ j ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= 9 ;
j ++ ) {
if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ;
else if ( j == 9... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static long answer ( int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ;
if ( n == 1 ) return 10 ;
for ( int j = 0 ;
j <= 9 ;
j ++ ) dp [ 1 ] [ j ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= 9 ;
j ++ ) {
if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1... |
java-cpp-PARTITION_INTO_TWO_SUBARRAYS_OF_LENGTHS_K_AND_N_K_SUCH_THAT_THE_DIFFERENCE_OF_SUMS_IS_MAXIMUM | java-cpp | static int maxDifference ( int arr [ ] , int N , int k ) {
int M , S = 0 , S1 = 0 , max_difference = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) S += arr [ i ] ;
int temp ;
for ( int i = 0 ;
i < N ;
i ++ ) {
for ( int j = i + 1 ;
j < N ;
j ++ ) {
if ( arr [ i ] < arr [ j ] ) {
temp = ... | int maxDifference ( int arr [ ] , int N , int k ) {
int M , S = 0 , S1 = 0 , max_difference = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) S += arr [ i ] ;
sort ( arr , arr + N , greater < int > ( ) ) ;
M = max ( k , N - k ) ;
for ( int i = 0 ;
i < M ;
i ++ ) S1 += arr [ i ] ;
max_difference = S1 - ( S - S1... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxDifference ( int arr [ ] , int N , int k ) {
int M , S = 0 , S1 = 0 , max_difference = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) S += arr [ i ] ;
int temp ;
for ( int i = 0 ;
i < N ;
i ++ ) {
for ( int j = i + 1 ;
j < N ;
j ++ ) {
if ( arr [ i ] < arr [ j ] ) {
... |
java-cpp-MAXIMUM_SUM_SUBARRAY_REMOVING_ONE_ELEMENT | java-cpp | static int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) {
int fw [ ] = new int [ n ] ;
int bw [ ] = new int [ n ] ;
int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ;
fw [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
cur_max = Math . max ( arr [ i ] , cur_max + arr [ i ] ) ;
max_so... | int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) {
int fw [ n ] , bw [ n ] ;
int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ;
fw [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
cur_max = max ( arr [ i ] , cur_max + arr [ i ] ) ;
max_so_far = max ( max_so_far , cur_max ) ;
fw [ i... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxSumSubarrayRemovingOneEle ( int arr [ ] , int n ) {
int fw [ ] = new int [ n ] ;
int bw [ ] = new int [ n ] ;
int cur_max = arr [ 0 ] , max_so_far = arr [ 0 ] ;
fw [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
cur_max = Math . max ( arr [ i ] , cur_max + arr [ i ] ) ;
... |
java-cpp-MOVE_SPACES_FRONT_STRING_SINGLE_TRAVERSAL | java-cpp | static void moveSpaceInFront ( char str [ ] ) {
int i = str . length - 1 ;
for ( int j = i ;
j >= 0 ;
j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ;
while ( i >= 0 ) str [ i -- ] = ' ▁ ' ;
}
| void moveSpaceInFront ( char str [ ] ) {
int i = strlen ( str ) ;
for ( int j = i ;
j >= 0 ;
j -- ) if ( str [ j ] != ' ' ) str [ i -- ] = str [ j ] ;
while ( i >= 0 ) str [ i -- ] = ' ' ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void moveSpaceInFront ( char str [ ] ) {
int i = str . length - 1 ;
for ( int j = i ;
j >= 0 ;
j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ;
while ( i >= 0 ) str [ i -- ] = ' ▁ ' ;
}
</Code> |
java-cpp-RECAMANS_SEQUENCE | java-cpp | static void recaman ( int n ) {
int arr [ ] = new int [ n ] ;
arr [ 0 ] = 0 ;
System . out . print ( arr [ 0 ] + " ," ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = arr [ i - 1 ] - i ;
int j ;
for ( j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ j ] == curr ) || curr < 0 ) {
curr... | int recaman ( int n ) {
int arr [ n ] ;
arr [ 0 ] = 0 ;
printf ( "%d, " , arr [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = arr [ i - 1 ] - i ;
int j ;
for ( j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ j ] == curr ) || curr < 0 ) {
curr = arr [ i - 1 ] + i ;
bre... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void recaman ( int n ) {
int arr [ ] = new int [ n ] ;
arr [ 0 ] = 0 ;
System . out . print ( arr [ 0 ] + " ," ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = arr [ i - 1 ] - i ;
int j ;
for ( j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ j ] == curr ) || curr < 0 ) {
... |
java-cpp-MAXIMUM_POINTS_COLLECTED_BY_TWO_PERSONS_ALLOWED_TO_MEET_ONCE | java-cpp | static int findMaxPoints ( int A [ ] [ ] ) {
int [ ] [ ] P1S = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P1E = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P2S = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P2E = new int [ M + 2 ] [ N + 2 ] ;
for ( int i = 1 ;
i <= N ;
i ++ ) for ( int j = 1 ;
j <= M ;
j ++... | int findMaxPoints ( int A [ ] [ M ] ) {
int P1S [ M + 1 ] [ N + 1 ] , P1E [ M + 1 ] [ N + 1 ] ;
memset ( P1S , 0 , sizeof ( P1S ) ) ;
memset ( P1E , 0 , sizeof ( P1E ) ) ;
int P2S [ M + 1 ] [ N + 1 ] , P2E [ M + 1 ] [ N + 1 ] ;
memset ( P2S , 0 , sizeof ( P2S ) ) ;
memset ( P2E , 0 , sizeof ( P2E ) ) ;
fo... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMaxPoints ( int A [ ] [ ] ) {
int [ ] [ ] P1S = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P1E = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P2S = new int [ M + 2 ] [ N + 2 ] ;
int [ ] [ ] P2E = new int [ M + 2 ] [ N + 2 ] ;
for ( int i = 1 ;
i <= N ;
i ++ ) for ( int j = 1 ;
j <= M ;... |
java-cpp-SPACE_OPTIMIZED_DP_SOLUTION_0_1_KNAPSACK_PROBLEM | java-cpp | static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int mat [ ] [ ] = new int [ 2 ] [ W + 1 ] ;
int i = 0 ;
while ( i < n ) {
int j = 0 ;
if ( i % 2 != 0 ) {
while ( ++ j <= W ) {
if ( wt [ i ] <= j ) {
mat [ 1 ] [ j ] = Math . max ( val [ i ] + mat [ 0 ] [ j - wt [ ... | int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int mat [ 2 ] [ W + 1 ] ;
memset ( mat , 0 , sizeof ( mat ) ) ;
int i = 0 ;
while ( i < n ) {
int j = 0 ;
if ( i % 2 != 0 ) {
while ( ++ j <= W ) {
if ( wt [ i ] <= j ) mat [ 1 ] [ j ] = max ( val [ i ] + mat [ 0 ] [ j - wt [ i ] ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) {
int mat [ ] [ ] = new int [ 2 ] [ W + 1 ] ;
int i = 0 ;
while ( i < n ) {
int j = 0 ;
if ( i % 2 != 0 ) {
while ( ++ j <= W ) {
if ( wt [ i ] <= j ) {
mat [ 1 ] [ j ] = Math . max ( val [ i ] + mat [ 0 ] [ j ... |
java-cpp-MULTIPLICATIVE_INVERSE_UNDER_MODULO_M | java-cpp | static int modInverse ( int a , int m ) {
a = a % m ;
for ( int x = 1 ;
x < m ;
x ++ ) if ( ( a * x ) % m == 1 ) return x ;
return 1 ;
}
| int modInverse ( int a , int m ) {
a = a % m ;
for ( int x = 1 ;
x < m ;
x ++ ) if ( ( a * x ) % m == 1 ) return x ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int modInverse ( int a , int m ) {
a = a % m ;
for ( int x = 1 ;
x < m ;
x ++ ) if ( ( a * x ) % m == 1 ) return x ;
return 1 ;
}
</Code> |
java-cpp-LENGTH_OF_THE_LONGEST_ARITHMATIC_PROGRESSION_IN_A_SORTED_ARRAY | java-cpp | static int lenghtOfLongestAP ( int set [ ] , int n ) {
if ( n <= 2 ) return n ;
int L [ ] [ ] = new int [ n ] [ n ] ;
int llap = 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) L [ i ] [ n - 1 ] = 2 ;
for ( int j = n - 2 ;
j >= 1 ;
j -- ) {
int i = j - 1 , k = j + 1 ;
while ( i >= 0 && k <= n - 1 ) {
... | int lenghtOfLongestAP ( int set [ ] , int n ) {
if ( n <= 2 ) return n ;
int L [ n ] [ n ] ;
int llap = 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) L [ i ] [ n - 1 ] = 2 ;
for ( int j = n - 2 ;
j >= 1 ;
j -- ) {
int i = j - 1 , k = j + 1 ;
while ( i >= 0 && k <= n - 1 ) {
if ( set [ i ] + set [... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int lenghtOfLongestAP ( int set [ ] , int n ) {
if ( n <= 2 ) return n ;
int L [ ] [ ] = new int [ n ] [ n ] ;
int llap = 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) L [ i ] [ n - 1 ] = 2 ;
for ( int j = n - 2 ;
j >= 1 ;
j -- ) {
int i = j - 1 , k = j + 1 ;
while ( i >= 0 && k <= n - 1 ... |
java-cpp-COUNT_BINARY_DIGIT_NUMBERS_SMALLER_N | java-cpp | static int countOfBinaryNumberLessThanN ( int N ) {
Queue < Integer > q = new LinkedList < > ( ) ;
q . add ( 1 ) ;
int cnt = 0 ;
int t ;
while ( q . size ( ) > 0 ) {
t = q . peek ( ) ;
q . remove ( ) ;
if ( t <= N ) {
cnt ++ ;
q . add ( t * 10 ) ;
q . add ( t * 10 + 1 ) ;
}
... | int countOfBinaryNumberLessThanN ( int N ) {
queue < int > q ;
q . push ( 1 ) ;
int cnt = 0 ;
int t ;
while ( ! q . empty ( ) ) {
t = q . front ( ) ;
q . pop ( ) ;
if ( t <= N ) {
cnt ++ ;
q . push ( t * 10 ) ;
q . push ( t * 10 + 1 ) ;
}
}
return cnt ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countOfBinaryNumberLessThanN ( int N ) {
Queue < Integer > q = new LinkedList < > ( ) ;
q . add ( 1 ) ;
int cnt = 0 ;
int t ;
while ( q . size ( ) > 0 ) {
t = q . peek ( ) ;
q . remove ( ) ;
if ( t <= N ) {
cnt ++ ;
q . add ( t * 10 ) ;
q . add ( t * 10 + 1 ) ;
... |
java-cpp-FIND_INDEX_GIVEN_FIBONACCI_NUMBER_CONSTANT_TIME | java-cpp | static int findIndex ( int n ) {
if ( n <= 1 ) return n ;
int a = 0 , b = 1 , c = 1 ;
int res = 1 ;
while ( c < n ) {
c = a + b ;
res ++ ;
a = b ;
b = c ;
}
return res ;
}
| int findIndex ( int n ) {
if ( n <= 1 ) return n ;
int a = 0 , b = 1 , c = 1 ;
int res = 1 ;
while ( c < n ) {
c = a + b ;
res ++ ;
a = b ;
b = c ;
}
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findIndex ( int n ) {
if ( n <= 1 ) return n ;
int a = 0 , b = 1 , c = 1 ;
int res = 1 ;
while ( c < n ) {
c = a + b ;
res ++ ;
a = b ;
b = c ;
}
return res ;
}
</Code> |
java-cpp-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL_1 | java-cpp | static void substringConversions ( String str , int k , int b ) {
int i = 0 , sum = 0 , counter = k - 1 ;
for ( i = 0 ;
i < k ;
i ++ ) {
sum = ( int ) ( sum + ( ( str . charAt ( i ) - ' 0 ' ) * Math . pow ( b , counter ) ) ) ;
counter -- ;
}
System . out . print ( sum + " " ) ;
int prev = sum ;
... | int substringConversions ( string str , int k , int b ) {
int i = 0 , sum = 0 , counter = k - 1 ;
for ( i ;
i < k ;
i ++ ) {
sum = sum + ( ( str . at ( i ) - '0' ) * pow ( b , counter ) ) ;
counter -- ;
}
cout << sum << " " ;
int prev = sum ;
sum = 0 , counter = 0 ;
for ( i ;
i < str . size ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void substringConversions ( String str , int k , int b ) {
int i = 0 , sum = 0 , counter = k - 1 ;
for ( i = 0 ;
i < k ;
i ++ ) {
sum = ( int ) ( sum + ( ( str . charAt ( i ) - ' 0 ' ) * Math . pow ( b , counter ) ) ) ;
counter -- ;
}
System . out . print ( sum + " " ) ;
int prev = s... |
java-cpp-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE_1 | java-cpp | static int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) {
int maxSum = 0 ;
Arrays . sort ( arr ) ;
for ( int i = N - 1 ;
i > 0 ;
-- i ) {
if ( arr [ i ] - arr [ i - 1 ] < k ) {
maxSum += arr [ i ] ;
maxSum += arr [ i - 1 ] ;
-- i ;
}
}
return maxSum ;
}
| int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) {
int maxSum = 0 ;
sort ( arr , arr + N ) ;
for ( int i = N - 1 ;
i > 0 ;
-- i ) {
if ( arr [ i ] - arr [ i - 1 ] < k ) {
maxSum += arr [ i ] ;
maxSum += arr [ i - 1 ] ;
-- i ;
}
}
return maxSum ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int k ) {
int maxSum = 0 ;
Arrays . sort ( arr ) ;
for ( int i = N - 1 ;
i > 0 ;
-- i ) {
if ( arr [ i ] - arr [ i - 1 ] < k ) {
maxSum += arr [ i ] ;
maxSum += arr [ i - 1 ] ;
-- i ;
}
}
return maxSum ;
... |
java-cpp-LONGEST_REPEATED_SUBSEQUENCE_1 | java-cpp | static String longestRepeatedSubSeq ( String str ) {
int n = str . length ( ) ;
int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= n ;
j ++ ) if ( str . c... | string longestRepeatedSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= n ;
j ++ ) if ( str [ i - 1 ] == str [ j - 1 ] &... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static String longestRepeatedSubSeq ( String str ) {
int n = str . length ( ) ;
int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= n ;
j ++ ) if ( ... |
java-cpp-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP_1 | java-cpp | static boolean isHeap ( int arr [ ] , int n ) {
for ( int i = 0 ;
i <= ( n - 2 ) / 2 ;
i ++ ) {
if ( arr [ 2 * i + 1 ] > arr [ i ] ) {
return false ;
}
if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) {
return false ;
}
}
return true ;
}
| bool isHeap ( int arr [ ] , int n ) {
for ( int i = 0 ;
i <= ( n - 2 ) / 2 ;
i ++ ) {
if ( arr [ 2 * i + 1 ] > arr [ i ] ) return false ;
if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) return false ;
}
return true ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isHeap ( int arr [ ] , int n ) {
for ( int i = 0 ;
i <= ( n - 2 ) / 2 ;
i ++ ) {
if ( arr [ 2 * i + 1 ] > arr [ i ] ) {
return false ;
}
if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) {
return false ;
}
}
return true ;
}
</Code> |
java-cpp-MINIMUM_COST_SORT_MATRIX_NUMBERS_0_N2_1 | java-cpp | public static int calculateEnergy ( int mat [ ] [ ] , int n ) {
int i_des , j_des , q ;
int tot_energy = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
q = mat [ i ] [ j ] / n ;
i_des = q ;
j_des = mat [ i ] [ j ] - ( n * q ) ;
tot_energy += Math ... | int calculateEnergy ( int mat [ SIZE ] [ SIZE ] , int n ) {
int i_des , j_des , q ;
int tot_energy = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
q = mat [ i ] [ j ] / n ;
i_des = q ;
j_des = mat [ i ] [ j ] - ( n * q ) ;
tot_energy += abs ( i_d... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int calculateEnergy ( int mat [ ] [ ] , int n ) {
int i_des , j_des , q ;
int tot_energy = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
q = mat [ i ] [ j ] / n ;
i_des = q ;
j_des = mat [ i ] [ j ] - ( n * q ) ;
tot_energy +... |
java-cpp-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS | java-cpp | static void rearrange ( int a [ ] , int size ) {
int positive = 0 , negative = 1 , temp ;
while ( true ) {
while ( positive < size && a [ positive ] >= 0 ) positive += 2 ;
while ( negative < size && a [ negative ] <= 0 ) negative += 2 ;
if ( positive < size && negative < size ) {
temp = a [ positi... | void rearrange ( int a [ ] , int size ) {
int positive = 0 , negative = 1 ;
while ( true ) {
while ( positive < size && a [ positive ] >= 0 ) positive += 2 ;
while ( negative < size && a [ negative ] <= 0 ) negative += 2 ;
if ( positive < size && negative < size ) swap ( a [ positive ] , a [ negative ] ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void rearrange ( int a [ ] , int size ) {
int positive = 0 , negative = 1 , temp ;
while ( true ) {
while ( positive < size && a [ positive ] >= 0 ) positive += 2 ;
while ( negative < size && a [ negative ] <= 0 ) negative += 2 ;
if ( positive < size && negative < size ) {
temp = a [... |
java-cpp-PRINT_MATRIX_ANTISPIRAL_FORM | java-cpp | public static void antiSpiralTraversal ( int m , int n , int a [ ] [ ] ) {
int i , k = 0 , l = 0 ;
Stack < Integer > stk = new Stack < Integer > ( ) ;
while ( k <= m && l <= n ) {
for ( i = l ;
i <= n ;
++ i ) stk . push ( a [ k ] [ i ] ) ;
k ++ ;
for ( i = k ;
i <= m ;
++ i ) stk . pu... | void antiSpiralTraversal ( int m , int n , int a [ R ] [ C ] ) {
int i , k = 0 , l = 0 ;
stack < int > stk ;
while ( k <= m && l <= n ) {
for ( i = l ;
i <= n ;
++ i ) stk . push ( a [ k ] [ i ] ) ;
k ++ ;
for ( i = k ;
i <= m ;
++ i ) stk . push ( a [ i ] [ n ] ) ;
n -- ;
if (... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static void antiSpiralTraversal ( int m , int n , int a [ ] [ ] ) {
int i , k = 0 , l = 0 ;
Stack < Integer > stk = new Stack < Integer > ( ) ;
while ( k <= m && l <= n ) {
for ( i = l ;
i <= n ;
++ i ) stk . push ( a [ k ] [ i ] ) ;
k ++ ;
for ( i = k ;
i <= m ;
++ i ) s... |
java-cpp-RETURN_A_PAIR_WITH_MAXIMUM_PRODUCT_IN_ARRAY_OF_INTEGERS_1 | java-cpp | static void maxProduct ( int arr [ ] , int n ) {
if ( n < 2 ) {
System . out . println ( "No pairs exists" ) ;
return ;
}
if ( n == 2 ) {
System . out . println ( arr [ 0 ] + " " + arr [ 1 ] ) ;
return ;
}
int posa = Integer . MIN_VALUE , posb = Integer . MIN_VALUE ;
int nega = Integer . MIN... | void maxProduct ( int arr [ ] , int n ) {
if ( n < 2 ) {
cout << "No pairs exists\n" ;
return ;
}
if ( n == 2 ) {
cout << arr [ 0 ] << " " << arr [ 1 ] << endl ;
return ;
}
int posa = INT_MIN , posb = INT_MIN ;
int nega = INT_MIN , negb = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void maxProduct ( int arr [ ] , int n ) {
if ( n < 2 ) {
System . out . println ( "No pairs exists" ) ;
return ;
}
if ( n == 2 ) {
System . out . println ( arr [ 0 ] + " " + arr [ 1 ] ) ;
return ;
}
int posa = Integer . MIN_VALUE , posb = Integer . MIN_VALUE ;
int nega = Intege... |
java-cpp-MAXIMIZE_ARRAY_ELEMENTS_UPTO_GIVEN_NUMBER | java-cpp | static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) {
int ind ;
int val ;
int [ ] [ ] dp = new int [ n ] [ maxLimit + 1 ] ;
for ( ind = 0 ;
ind < n ;
ind ++ ) {
for ( val = 0 ;
val <= maxLimit ;
val ++ ) {
if ( ind == 0 ) {
if ( num - arr [ ind ] == val || num + ... | int findMaxVal ( int arr [ ] , int n , int num , int maxLimit ) {
int ind ;
int val ;
int dp [ n ] [ maxLimit + 1 ] ;
for ( ind = 0 ;
ind < n ;
ind ++ ) {
for ( val = 0 ;
val <= maxLimit ;
val ++ ) {
if ( ind == 0 ) {
if ( num - arr [ ind ] == val || num + arr [ ind ] == val ) {
... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMaxVal ( int [ ] arr , int n , int num , int maxLimit ) {
int ind ;
int val ;
int [ ] [ ] dp = new int [ n ] [ maxLimit + 1 ] ;
for ( ind = 0 ;
ind < n ;
ind ++ ) {
for ( val = 0 ;
val <= maxLimit ;
val ++ ) {
if ( ind == 0 ) {
if ( num - arr [ ind ] == val ||... |
java-cpp-PROGRAM_FOR_DEADLOCK_FREE_CONDITION_IN_OPERATING_SYSTEM | java-cpp | static int Resources ( int process , int need ) {
int minResources = 0 ;
minResources = process * ( need - 1 ) + 1 ;
return minResources ;
}
| int Resources ( int process , int need ) {
int minResources = 0 ;
minResources = process * ( need - 1 ) + 1 ;
return minResources ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int Resources ( int process , int need ) {
int minResources = 0 ;
minResources = process * ( need - 1 ) + 1 ;
return minResources ;
}
</Code> |
java-cpp-BUBBLE_SORT | java-cpp | void bubbleSort ( int arr [ ] ) {
int n = arr . length ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = 0 ;
j < n - i - 1 ;
j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) {
int temp = arr [ j ] ;
arr [ j ] = arr [ j + 1 ] ;
arr [ j + 1 ] = temp ;
}
}
| void bubbleSort ( int arr [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) for ( j = 0 ;
j < n - i - 1 ;
j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) swap ( & arr [ j ] , & arr [ j + 1 ] ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
void bubbleSort ( int arr [ ] ) {
int n = arr . length ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = 0 ;
j < n - i - 1 ;
j ++ ) if ( arr [ j ] > arr [ j + 1 ] ) {
int temp = arr [ j ] ;
arr [ j ] = arr [ j + 1 ] ;
arr [ j + 1 ] = temp ;
}
}
</Code> |
java-cpp-SHORTEST_COMMON_SUPERSEQUENCE_1 | java-cpp | static int superSeq ( String X , String Y , int m , int n ) {
int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( X . charAt ( i -... | int superSeq ( char * X , char * Y , int m , int n ) {
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( ! i ) dp [ i ] [ j ] = j ;
else if ( ! j ) dp [ i ] [ j ] = i ;
else if ( X [ i - 1 ] == Y [ j - 1 ] ) dp [ i ] [ j ] = 1... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int superSeq ( String X , String Y , int m , int n ) {
int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( X . charA... |
java-cpp-NUMBER_NON_NEGATIVE_INTEGRAL_SOLUTIONS_B_C_N | java-cpp | static int countIntegralSolutions ( int n ) {
int result = 0 ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n - i ;
j ++ ) for ( int k = 0 ;
k <= ( n - i - j ) ;
k ++ ) if ( i + j + k == n ) result ++ ;
return result ;
}
| int countIntegralSolutions ( int n ) {
int result = 0 ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n - i ;
j ++ ) for ( int k = 0 ;
k <= ( n - i - j ) ;
k ++ ) if ( i + j + k == n ) result ++ ;
return result ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countIntegralSolutions ( int n ) {
int result = 0 ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n - i ;
j ++ ) for ( int k = 0 ;
k <= ( n - i - j ) ;
k ++ ) if ( i + j + k == n ) result ++ ;
return result ;
}
</Code> |
java-cpp-MINIMUM_NUMBER_OF_JUMPS_TO_REACH_END_OF_A_GIVEN_ARRAY_2 | java-cpp | static int minJumps ( int arr [ ] , int n ) {
int [ ] jumps = new int [ n ] ;
int min ;
jumps [ n - 1 ] = 0 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] == 0 ) jumps [ i ] = Integer . MAX_VALUE ;
else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ;
else {
min = Integer . MAX_VA... | int minJumps ( int arr [ ] , int n ) {
int * jumps = new int [ n ] ;
int min ;
jumps [ n - 1 ] = 0 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] == 0 ) jumps [ i ] = INT_MAX ;
else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ;
else {
min = INT_MAX ;
for ( int j = i + 1 ;... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minJumps ( int arr [ ] , int n ) {
int [ ] jumps = new int [ n ] ;
int min ;
jumps [ n - 1 ] = 0 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] == 0 ) jumps [ i ] = Integer . MAX_VALUE ;
else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ;
else {
min = Integer .... |
java-cpp-FIND_MINIMUM_ADJUSTMENT_COST_OF_AN_ARRAY | java-cpp | static int minAdjustmentCost ( int A [ ] , int n , int target ) {
int [ ] [ ] dp = new int [ n ] [ M + 1 ] ;
for ( int j = 0 ;
j <= M ;
j ++ ) dp [ 0 ] [ j ] = Math . abs ( j - A [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= M ;
j ++ ) {
dp [ i ] [ j ] = Integer . M... | int minAdjustmentCost ( int A [ ] , int n , int target ) {
int dp [ n ] [ M + 1 ] ;
for ( int j = 0 ;
j <= M ;
j ++ ) dp [ 0 ] [ j ] = abs ( j - A [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= M ;
j ++ ) {
dp [ i ] [ j ] = INT_MAX ;
for ( int k = max ( j - tar... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minAdjustmentCost ( int A [ ] , int n , int target ) {
int [ ] [ ] dp = new int [ n ] [ M + 1 ] ;
for ( int j = 0 ;
j <= M ;
j ++ ) dp [ 0 ] [ j ] = Math . abs ( j - A [ 0 ] ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j <= M ;
j ++ ) {
dp [ i ] [ j ] = Inte... |
java-cpp-KTH_SMALLESTLARGEST_ELEMENT_UNSORTED_ARRAY | java-cpp | public static int kthSmallest ( Integer [ ] arr , int k ) {
Arrays . sort ( arr ) ;
return arr [ k - 1 ] ;
}
| int kthSmallest ( int arr [ ] , int n , int k ) {
sort ( arr , arr + n ) ;
return arr [ k - 1 ] ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int kthSmallest ( Integer [ ] arr , int k ) {
Arrays . sort ( arr ) ;
return arr [ k - 1 ] ;
}
</Code> |
java-cpp-FIND_THE_MINIMUM_COST_TO_REACH_A_DESTINATION_WHERE_EVERY_STATION_IS_CONNECTED_IN_ONE_DIRECTION | java-cpp | static int minCost ( int cost [ ] [ ] ) {
int dist [ ] = new int [ N ] ;
for ( int i = 0 ;
i < N ;
i ++ ) dist [ i ] = INF ;
dist [ 0 ] = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [... | INT_MAX int minCost ( int cost [ ] [ N ] ) {
int dist [ N ] ;
for ( int i = 0 ;
i < N ;
i ++ ) dist [ i ] = INF ;
dist [ 0 ] = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost [ i ] [ j ] ;
re... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minCost ( int cost [ ] [ ] ) {
int dist [ ] = new int [ N ] ;
for ( int i = 0 ;
i < N ;
i ++ ) dist [ i ] = INF ;
dist [ 0 ] = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = i + 1 ;
j < N ;
j ++ ) if ( dist [ j ] > dist [ i ] + cost [ i ] [ j ] ) dist [ j ] = dist [ i ] + cost ... |
java-cpp-REVERSE_STRING_WITHOUT_USING_ANY_TEMPORARY_VARIABLE | java-cpp | static String reversingString ( char [ ] str , int start , int end ) {
while ( start < end ) {
str [ start ] ^= str [ end ] ;
str [ end ] ^= str [ start ] ;
str [ start ] ^= str [ end ] ;
++ start ;
-- end ;
}
return String . valueOf ( str ) ;
}
| string reversingString ( string str , int start , int end ) {
while ( start < end ) {
str [ start ] ^= str [ end ] ;
str [ end ] ^= str [ start ] ;
str [ start ] ^= str [ end ] ;
++ start ;
-- end ;
}
return str ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static String reversingString ( char [ ] str , int start , int end ) {
while ( start < end ) {
str [ start ] ^= str [ end ] ;
str [ end ] ^= str [ start ] ;
str [ start ] ^= str [ end ] ;
++ start ;
-- end ;
}
return String . valueOf ( str ) ;
}
</Code> |
java-cpp-MINIMAL_MOVES_FORM_STRING_ADDING_CHARACTERS_APPENDING_STRING | java-cpp | static int minimalSteps ( String s , int n ) {
int [ ] dp = new int [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = Integer . MAX_VALUE ;
String s1 = "" , s2 = "" ;
dp [ 0 ] = 1 ;
s1 += s . charAt ( 0 ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
s1 += s . charAt ( i ) ;
s2 = s . substring ( i ... | int minimalSteps ( string s , int n ) {
int dp [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = INT_MAX ;
string s1 = "" , s2 = "" ;
dp [ 0 ] = 1 ;
s1 += s [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) {
s1 += s [ i ] ;
s2 = s . substr ( i + 1 , i + 1 ) ;
dp [ i ] = min ( dp [ i ] , dp [ i... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minimalSteps ( String s , int n ) {
int [ ] dp = new int [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = Integer . MAX_VALUE ;
String s1 = "" , s2 = "" ;
dp [ 0 ] = 1 ;
s1 += s . charAt ( 0 ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
s1 += s . charAt ( i ) ;
s2 = s . substri... |
java-cpp-MINIMUM_COST_TO_FILL_GIVEN_WEIGHT_IN_A_BAG | java-cpp | public static int MinimumCost ( int cost [ ] , int n , int W ) {
Vector < Integer > val = new Vector < Integer > ( ) ;
Vector < Integer > wt = new Vector < Integer > ( ) ;
int size = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( cost [ i ] != - 1 ) {
val . add ( cost [ i ] ) ;
wt . add ( i + 1... | int MinimumCost ( int cost [ ] , int n , int W ) {
vector < int > val , wt ;
int size = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( cost [ i ] != - 1 ) {
val . push_back ( cost [ i ] ) ;
wt . push_back ( i + 1 ) ;
size ++ ;
}
}
n = size ;
int min_cost [ n + 1 ] [ W + 1 ] ;
fo... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int MinimumCost ( int cost [ ] , int n , int W ) {
Vector < Integer > val = new Vector < Integer > ( ) ;
Vector < Integer > wt = new Vector < Integer > ( ) ;
int size = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( cost [ i ] != - 1 ) {
val . add ( cost [ i ] ) ;
wt . add ... |
java-cpp-SORT_EVEN_PLACED_ELEMENTS_INCREASING_ODD_PLACED_DECREASING_ORDER | java-cpp | static void bitonicGenerator ( int arr [ ] , int n ) {
Vector < Integer > evenArr = new Vector < Integer > ( ) ;
Vector < Integer > oddArr = new Vector < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i % 2 != 1 ) {
evenArr . add ( arr [ i ] ) ;
}
else {
oddArr . add ( arr [ i... | void bitonicGenerator ( int arr [ ] , int n ) {
vector < int > evenArr ;
vector < int > oddArr ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ! ( i % 2 ) ) evenArr . push_back ( arr [ i ] ) ;
else oddArr . push_back ( arr [ i ] ) ;
}
sort ( evenArr . begin ( ) , evenArr . end ( ) ) ;
sort ( oddArr . ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void bitonicGenerator ( int arr [ ] , int n ) {
Vector < Integer > evenArr = new Vector < Integer > ( ) ;
Vector < Integer > oddArr = new Vector < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i % 2 != 1 ) {
evenArr . add ( arr [ i ] ) ;
}
else {
oddArr . add ( ... |
java-cpp-COUNT_DIVISIBLE_PAIRS_ARRAY | java-cpp | static int countDivisibles ( int arr [ ] , int n ) {
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ;
return res ;
}
| int countDivisibles ( int arr [ ] , int n ) {
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ;
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int countDivisibles ( int arr [ ] , int n ) {
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ;
return res ;
}
</Code> |
java-cpp-DYNAMIC_PROGRAMMING_SET_3_LONGEST_INCREASING_SUBSEQUENCE | java-cpp | static int lis ( int arr [ ] , int n ) {
max_ref = 1 ;
_lis ( arr , n ) ;
return max_ref ;
}
| int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int lis ( int arr [ ] , int n ) {
max_ref = 1 ;
_lis ( arr , n ) ;
return max_ref ;
}
</Code> |
java-cpp-FIND_FIRST_REPEATING_ELEMENT_ARRAY_INTEGERS | java-cpp | static void printFirstRepeating ( int arr [ ] ) {
int min = - 1 ;
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i = arr . length - 1 ;
i >= 0 ;
i -- ) {
if ( set . contains ( arr [ i ] ) ) min = i ;
else set . add ( arr [ i ] ) ;
}
if ( min != - 1 ) System . out . println ( "The first ... | void printFirstRepeating ( int arr [ ] , int n ) {
int min = - 1 ;
set < int > myset ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( myset . find ( arr [ i ] ) != myset . end ( ) ) min = i ;
else myset . insert ( arr [ i ] ) ;
}
if ( min != - 1 ) cout << "The first repeating element is " << arr [ m... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printFirstRepeating ( int arr [ ] ) {
int min = - 1 ;
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i = arr . length - 1 ;
i >= 0 ;
i -- ) {
if ( set . contains ( arr [ i ] ) ) min = i ;
else set . add ( arr [ i ] ) ;
}
if ( min != - 1 ) System . out . println ( "The... |
java-cpp-FIND_MINIMUM_NUMBER_OF_COINS_THAT_MAKE_A_CHANGE | java-cpp | static int minCoins ( int coins [ ] , int m , int V ) {
if ( V == 0 ) return 0 ;
int res = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < m ;
i ++ ) {
if ( coins [ i ] <= V ) {
int sub_res = minCoins ( coins , m , V - coins [ i ] ) ;
if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < res ) res ... | int minCoins ( int coins [ ] , int m , int V ) {
if ( V == 0 ) return 0 ;
int res = INT_MAX ;
for ( int i = 0 ;
i < m ;
i ++ ) {
if ( coins [ i ] <= V ) {
int sub_res = minCoins ( coins , m , V - coins [ i ] ) ;
if ( sub_res != INT_MAX && sub_res + 1 < res ) res = sub_res + 1 ;
}
}
ret... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minCoins ( int coins [ ] , int m , int V ) {
if ( V == 0 ) return 0 ;
int res = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < m ;
i ++ ) {
if ( coins [ i ] <= V ) {
int sub_res = minCoins ( coins , m , V - coins [ i ] ) ;
if ( sub_res != Integer . MAX_VALUE && sub_res + 1 < res... |
java-cpp-FIND_PAIRS_GIVEN_SUM_ELEMENTS_PAIR_DIFFERENT_ROWS | java-cpp | static void pairSum ( int mat [ ] [ ] , int n , int sum ) {
for ( int i = 0 ;
i < n ;
i ++ ) Arrays . sort ( mat [ i ] ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int left = 0 , right = n - 1 ;
while ( left < n && right >= 0 ) {
if ( ( ma... | void pairSum ( int mat [ ] [ MAX ] , int n , int sum ) {
for ( int i = 0 ;
i < n ;
i ++ ) sort ( mat [ i ] , mat [ i ] + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int left = 0 , right = n - 1 ;
while ( left < n && right >= 0 ) {
if ( ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void pairSum ( int mat [ ] [ ] , int n , int sum ) {
for ( int i = 0 ;
i < n ;
i ++ ) Arrays . sort ( mat [ i ] ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int left = 0 , right = n - 1 ;
while ( left < n && right >= 0 ) {
if... |
java-cpp-MINIMUM_COST_CONNECT_WEIGHTED_NODES_REPRESENTED_ARRAY | java-cpp | static int minimum_cost ( int a [ ] , int n ) {
int mn = Integer . MAX_VALUE ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
mn = Math . min ( a [ i ] , mn ) ;
sum += a [ i ] ;
}
return mn * ( sum - mn ) ;
}
| int minimum_cost ( int a [ ] , int n ) {
int mn = INT_MAX ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
mn = min ( a [ i ] , mn ) ;
sum += a [ i ] ;
}
return mn * ( sum - mn ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int minimum_cost ( int a [ ] , int n ) {
int mn = Integer . MAX_VALUE ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
mn = Math . min ( a [ i ] , mn ) ;
sum += a [ i ] ;
}
return mn * ( sum - mn ) ;
}
</Code> |
java-cpp-DYNAMIC_PROGRAMMING_SET_12_LONGEST_PALINDROMIC_SUBSEQUENCE | java-cpp | static int lps ( String seq ) {
int n = seq . length ( ) ;
int i , j , cl ;
int L [ ] [ ] = new int [ n ] [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) L [ i ] [ i ] = 1 ;
for ( cl = 2 ;
cl <= n ;
cl ++ ) {
for ( i = 0 ;
i < n - cl + 1 ;
i ++ ) {
j = i + cl - 1 ;
if ( seq . charAt ( i )... | int lps ( char * str ) {
int n = strlen ( str ) ;
int i , j , cl ;
int L [ n ] [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) L [ i ] [ i ] = 1 ;
for ( cl = 2 ;
cl <= n ;
cl ++ ) {
for ( i = 0 ;
i < n - cl + 1 ;
i ++ ) {
j = i + cl - 1 ;
if ( str [ i ] == str [ j ] && cl == 2 ) L [ i ] [... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int lps ( String seq ) {
int n = seq . length ( ) ;
int i , j , cl ;
int L [ ] [ ] = new int [ n ] [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) L [ i ] [ i ] = 1 ;
for ( cl = 2 ;
cl <= n ;
cl ++ ) {
for ( i = 0 ;
i < n - cl + 1 ;
i ++ ) {
j = i + cl - 1 ;
if ( seq . charA... |
java-cpp-PROGRAM_CENSOR_WORD_ASTERISKS_SENTENCE | java-cpp | static String censor ( String text , String word ) {
String [ ] word_list = text . split ( "\\s+" ) ;
String result = "" ;
String stars = "" ;
for ( int i = 0 ;
i < word . length ( ) ;
i ++ ) stars += ' * ' ;
int index = 0 ;
for ( String i : word_list ) {
if ( i . compareTo ( word ) == 0 ) word_list... | string censor ( string text , string word ) {
vector < string > word_list ;
boost :: split ( word_list , text , boost :: is_any_of ( "\\ +" ) ) ;
string result = "" ;
string stars = "" ;
for ( int i = 0 ;
i < word . size ( ) ;
i ++ ) stars += '*' ;
int index = 0 ;
for ( string i : word_list ) {
if... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static String censor ( String text , String word ) {
String [ ] word_list = text . split ( "\\s+" ) ;
String result = "" ;
String stars = "" ;
for ( int i = 0 ;
i < word . length ( ) ;
i ++ ) stars += ' * ' ;
int index = 0 ;
for ( String i : word_list ) {
if ( i . compareTo ( word ) == 0 ) wo... |
java-cpp-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT | java-cpp | static int isPowerOfFour ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 4 != 0 ) return 0 ;
n = n / 4 ;
}
return 1 ;
}
| public : bool isPowerOfFour ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 4 != 0 ) return 0 ;
n = n / 4 ;
}
return 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int isPowerOfFour ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 4 != 0 ) return 0 ;
n = n / 4 ;
}
return 1 ;
}
</Code> |
java-cpp-AREA_OF_THE_CIRCLE_THAT_HAS_A_SQUARE_AND_A_CIRCLE_INSCRIBED_IN_IT | java-cpp | static float getArea ( int a ) {
float area = ( float ) ( Math . PI * a * a ) / 4 ;
return area ;
}
| float getArea ( int a ) {
float area = ( M_PI * a * a ) / 4.0 ;
return area ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static float getArea ( int a ) {
float area = ( float ) ( Math . PI * a * a ) / 4 ;
return area ;
}
</Code> |
java-cpp-SUM_MANHATTAN_DISTANCES_PAIRS_POINTS | java-cpp | static int distancesum ( int x [ ] , int y [ ] , int n ) {
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ;
return sum ;
}
| int distancesum ( int x [ ] , int y [ ] , int n ) {
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) sum += ( abs ( x [ i ] - x [ j ] ) + abs ( y [ i ] - y [ j ] ) ) ;
return sum ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int distancesum ( int x [ ] , int y [ ] , int n ) {
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) sum += ( Math . abs ( x [ i ] - x [ j ] ) + Math . abs ( y [ i ] - y [ j ] ) ) ;
return sum ;
}
</Code> |
java-cpp-FIND_ROW_NUMBER_BINARY_MATRIX_MAXIMUM_NUMBER_1S | java-cpp | static void findMax ( int arr [ ] [ ] ) {
int row = 0 , i , j ;
for ( i = 0 , j = N - 1 ;
i < N ;
i ++ ) {
while ( j >= 0 && arr [ i ] [ j ] == 1 ) {
row = i ;
j -- ;
}
}
System . out . print ( "Row number = " + ( row + 1 ) ) ;
System . out . print ( ", MaxCount = " + ( N - 1 - j ) ) ;... | void findMax ( int arr [ ] [ N ] ) {
int row = 0 , i , j ;
for ( i = 0 , j = N - 1 ;
i < N ;
i ++ ) {
while ( arr [ i ] [ j ] == 1 && j >= 0 ) {
row = i ;
j -- ;
}
}
cout << "Row number = " << row + 1 ;
cout << ", MaxCount = " << N - 1 - j ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void findMax ( int arr [ ] [ ] ) {
int row = 0 , i , j ;
for ( i = 0 , j = N - 1 ;
i < N ;
i ++ ) {
while ( j >= 0 && arr [ i ] [ j ] == 1 ) {
row = i ;
j -- ;
}
}
System . out . print ( "Row number = " + ( row + 1 ) ) ;
System . out . print ( ", MaxCount = " + ( N - 1 - ... |
java-cpp-LAST_NON_ZERO_DIGIT_FACTORIAL | java-cpp | static int lastNon0Digit ( int n ) {
if ( n < 10 ) return dig [ n ] ;
if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
}
| int lastNon0Digit ( int n ) {
if ( n < 10 ) return dig [ n ] ;
if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int lastNon0Digit ( int n ) {
if ( n < 10 ) return dig [ n ] ;
if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ;
}
</Code> |
java-cpp-EVEN_FIBONACCI_NUMBERS_SUM | java-cpp | static int evenFibSum ( int limit ) {
if ( limit < 2 ) return 0 ;
long ef1 = 0 , ef2 = 2 ;
long sum = ef1 + ef2 ;
while ( ef2 <= limit ) {
long ef3 = 4 * ef2 + ef1 ;
if ( ef3 > limit ) break ;
ef1 = ef2 ;
ef2 = ef3 ;
sum += ef2 ;
}
return ( int ) sum ;
}
| int evenFibSum ( int limit ) {
if ( limit < 2 ) return 0 ;
long long int ef1 = 0 , ef2 = 2 ;
long long int sum = ef1 + ef2 ;
while ( ef2 <= limit ) {
long long int ef3 = 4 * ef2 + ef1 ;
if ( ef3 > limit ) break ;
ef1 = ef2 ;
ef2 = ef3 ;
sum += ef2 ;
}
return sum ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int evenFibSum ( int limit ) {
if ( limit < 2 ) return 0 ;
long ef1 = 0 , ef2 = 2 ;
long sum = ef1 + ef2 ;
while ( ef2 <= limit ) {
long ef3 = 4 * ef2 + ef1 ;
if ( ef3 > limit ) break ;
ef1 = ef2 ;
ef2 = ef3 ;
sum += ef2 ;
}
return ( int ) sum ;
}
</Code> |
java-cpp-SEARCH_AN_ELEMENT_IN_AN_ARRAY_WHERE_DIFFERENCE_BETWEEN_ADJACENT_ELEMENTS_IS_1 | java-cpp | static int search ( int arr [ ] , int n , int x ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . abs ( arr [ i ] - x ) ;
}
System . out . println ( "number is not" + " present!" ) ;
return - 1 ;
}
| int search ( int arr [ ] , int n , int x ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + abs ( arr [ i ] - x ) ;
}
cout << "number is not present!" ;
return - 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int search ( int arr [ ] , int n , int x ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . abs ( arr [ i ] - x ) ;
}
System . out . println ( "number is not" + " present!" ) ;
return - 1 ;
}
</Code> |
java-cpp-CHECK_STRING_CAN_OBTAINED_ROTATING_ANOTHER_STRING_2_PLACES | java-cpp | static boolean isRotated ( String str1 , String str2 ) {
if ( str1 . length ( ) != str2 . length ( ) ) return false ;
String clock_rot = "" ;
String anticlock_rot = "" ;
int len = str2 . length ( ) ;
anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len ) + str2 . substring ( 0 , len - 2 ) ;
cloc... | bool isRotated ( string str1 , string str2 ) {
if ( str1 . length ( ) != str2 . length ( ) ) return false ;
string clock_rot = "" ;
string anticlock_rot = "" ;
int len = str2 . length ( ) ;
anticlock_rot = anticlock_rot + str2 . substr ( len - 2 , 2 ) + str2 . substr ( 0 , len - 2 ) ;
clock_rot = clock_rot ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isRotated ( String str1 , String str2 ) {
if ( str1 . length ( ) != str2 . length ( ) ) return false ;
String clock_rot = "" ;
String anticlock_rot = "" ;
int len = str2 . length ( ) ;
anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len ) + str2 . substring ( 0 , len - 2 ) ;... |
java-cpp-HOSOYAS_TRIANGLE | java-cpp | static void printHosoya ( int n ) {
int dp [ ] [ ] = new int [ N ] [ N ] ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ;
dp [ 1 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ;
else dp ... | void printHosoya ( int n ) {
int dp [ N ] [ N ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = dp [ 1 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ;
e... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printHosoya ( int n ) {
int dp [ ] [ ] = new int [ N ] [ N ] ;
dp [ 0 ] [ 0 ] = dp [ 1 ] [ 0 ] = 1 ;
dp [ 1 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i > j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i - 2 ] [ j ] ;
e... |
java-cpp-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S | java-cpp | int findSubArray ( int arr [ ] , int n ) {
int sum = 0 ;
int maxsize = - 1 , startindex = 0 ;
int endindex = 0 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( arr [ j ] == 0 ) sum += - 1 ;
else sum += 1 ;
... | int findSubArray ( int arr [ ] , int n ) {
int sum = 0 ;
int maxsize = - 1 , startindex ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
( arr [ j ] == 0 ) ? ( sum += - 1 ) : ( sum += 1 ) ;
if ( sum == 0 && maxsize... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int findSubArray ( int arr [ ] , int n ) {
int sum = 0 ;
int maxsize = - 1 , startindex = 0 ;
int endindex = 0 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
sum = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( arr [ j ] == 0 ) sum += - 1 ;
else sum +... |
java-cpp-RECURSIVE_INSERTION_SORT | java-cpp | static void insertionSortRecursive ( int arr [ ] , int n ) {
if ( n <= 1 ) return ;
insertionSortRecursive ( arr , n - 1 ) ;
int last = arr [ n - 1 ] ;
int j = n - 2 ;
while ( j >= 0 && arr [ j ] > last ) {
arr [ j + 1 ] = arr [ j ] ;
j -- ;
}
arr [ j + 1 ] = last ;
}
| void insertionSortRecursive ( int arr [ ] , int n ) {
if ( n <= 1 ) return ;
insertionSortRecursive ( arr , n - 1 ) ;
int last = arr [ n - 1 ] ;
int j = n - 2 ;
while ( j >= 0 && arr [ j ] > last ) {
arr [ j + 1 ] = arr [ j ] ;
j -- ;
}
arr [ j + 1 ] = last ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void insertionSortRecursive ( int arr [ ] , int n ) {
if ( n <= 1 ) return ;
insertionSortRecursive ( arr , n - 1 ) ;
int last = arr [ n - 1 ] ;
int j = n - 2 ;
while ( j >= 0 && arr [ j ] > last ) {
arr [ j + 1 ] = arr [ j ] ;
j -- ;
}
arr [ j + 1 ] = last ;
}
</Code> |
java-cpp-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY_1 | java-cpp | static int findDiff ( int arr [ ] , int n ) {
Map < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . containsKey ( arr [ i ] ) ) {
mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ;
}
else {
mp . put ( arr [ i ] , 1 ) ;
}
}
int max_co... | int findDiff ( int arr [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ arr [ i ] ] ++ ;
int max_count = 0 , min_count = n ;
for ( auto x : hm ) {
max_count = max ( max_count , x . second ) ;
min_count = min ( min_count , x . second ) ;
}
return ( max_count ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findDiff ( int arr [ ] , int n ) {
Map < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . containsKey ( arr [ i ] ) ) {
mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ;
}
else {
mp . put ( arr [ i ] , 1 ) ;
}
}
int... |
java-cpp-GAME_REPLACING_ARRAY_ELEMENTS | java-cpp | public static int playGame ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i : arr ) set . add ( i ) ;
return ( set . size ( ) % 2 == 0 ) ? 1 : 2 ;
}
| int playGame ( int arr [ ] , int n ) {
unordered_set < int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash . insert ( arr [ i ] ) ;
return ( hash . size ( ) % 2 == 0 ? 1 : 2 ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int playGame ( int arr [ ] ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i : arr ) set . add ( i ) ;
return ( set . size ( ) % 2 == 0 ) ? 1 : 2 ;
}
</Code> |
java-cpp-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS_1 | java-cpp | static int subset ( int arr [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ;
int res = 0 ;
for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) r... | int subset ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int res = 0 ;
for ( auto x : mp ) res = max ( res , x . second ) ;
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int subset ( int arr [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ;
int res = 0 ;
for ( Map . Entry < Integer , Integer > entry : mp . entrySet ... |
java-cpp-PAIR_WITH_GIVEN_PRODUCT_SET_1_FIND_IF_ANY_PAIR_EXISTS_1 | java-cpp | static boolean isProduct ( int arr [ ] , int n , int x ) {
HashSet < Integer > hset = new HashSet < > ( ) ;
if ( n < 2 ) return false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if ( hset .... | bool isProduct ( int arr [ ] , int n , int x ) {
if ( n < 2 ) return false ;
unordered_set < int > s ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if ( s . find ( x / arr [ i ] ) != s . end (... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static boolean isProduct ( int arr [ ] , int n , int x ) {
HashSet < Integer > hset = new HashSet < > ( ) ;
if ( n < 2 ) return false ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == 0 ) {
if ( x == 0 ) return true ;
else continue ;
}
if ( x % arr [ i ] == 0 ) {
if (... |
java-cpp-MAXIMUM_AVERAGE_SUM_PARTITION_ARRAY | java-cpp | static double largestSumOfAverages ( int [ ] A , int K ) {
int n = A . length ;
double [ ] pre_sum = new double [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double [ ] dp = new double [ n ] ;
double sum = 0 ;
for ( int i = 0 ;
i < n ;... | double largestSumOfAverages ( vector < int > & A , int K ) {
int n = A . size ( ) ;
double pre_sum [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double dp [ n ] = {
0 }
;
double sum = 0 ;
for ( int i = 0 ;
i < n ;
i +... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static double largestSumOfAverages ( int [ ] A , int K ) {
int n = A . length ;
double [ ] pre_sum = new double [ n + 1 ] ;
pre_sum [ 0 ] = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) pre_sum [ i + 1 ] = pre_sum [ i ] + A [ i ] ;
double [ ] dp = new double [ n ] ;
double sum = 0 ;
for ( int i = 0 ;
... |
java-cpp-MINIMUM_STEPS_MINIMIZE_N_PER_GIVEN_CONDITION | java-cpp | static int getMinSteps ( int n ) {
int table [ ] = new int [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 > 0 ) ) table [ i / 2 ] = Math . min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 > 0 ) ) table [ i / 3 ] = Ma... | int getMinSteps ( int n ) {
int table [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 ) ) table [ i / 2 ] = min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 ) ) table [ i / 3 ] = min ( table [ i ] + 1 , table [ i / 3 ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int getMinSteps ( int n ) {
int table [ ] = new int [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) table [ i ] = n - i ;
for ( int i = n ;
i >= 1 ;
i -- ) {
if ( ! ( i % 2 > 0 ) ) table [ i / 2 ] = Math . min ( table [ i ] + 1 , table [ i / 2 ] ) ;
if ( ! ( i % 3 > 0 ) ) table [ i / 3... |
java-cpp-N_TH_TERM_SERIES_2_12_36_80_150 | java-cpp | public static int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
| int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int nthTerm ( int n ) {
return ( n * n ) + ( n * n * n ) ;
}
</Code> |
java-cpp-FIND_MAXIMUM_AVERAGE_SUBARRAY_OF_K_LENGTH_1 | java-cpp | static int findMaxAverage ( int arr [ ] , int n , int k ) {
if ( k > n ) return - 1 ;
int sum = arr [ 0 ] ;
for ( int i = 1 ;
i < k ;
i ++ ) sum += arr [ i ] ;
int max_sum = sum , max_end = k - 1 ;
for ( int i = k ;
i < n ;
i ++ ) {
sum = sum + arr [ i ] - arr [ i - k ] ;
if ( sum > max_sum ) ... | int findMaxAverage ( int arr [ ] , int n , int k ) {
if ( k > n ) return - 1 ;
int sum = arr [ 0 ] ;
for ( int i = 1 ;
i < k ;
i ++ ) sum += arr [ i ] ;
int max_sum = sum , max_end = k - 1 ;
for ( int i = k ;
i < n ;
i ++ ) {
int sum = sum + arr [ i ] - arr [ i - k ] ;
if ( sum > max_sum ) {
... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMaxAverage ( int arr [ ] , int n , int k ) {
if ( k > n ) return - 1 ;
int sum = arr [ 0 ] ;
for ( int i = 1 ;
i < k ;
i ++ ) sum += arr [ i ] ;
int max_sum = sum , max_end = k - 1 ;
for ( int i = k ;
i < n ;
i ++ ) {
sum = sum + arr [ i ] - arr [ i - k ] ;
if ( sum > max... |
java-cpp-DYNAMIC_PROGRAMMING_SET_11_EGG_DROPPING_PUZZLE | java-cpp | static int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = Integer . MAX_VALUE ;
int x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return min + ... | int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = INT_MAX , x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return min + 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int eggDrop ( int n , int k ) {
if ( k == 1 || k == 0 ) return k ;
if ( n == 1 ) return k ;
int min = Integer . MAX_VALUE ;
int x , res ;
for ( x = 1 ;
x <= k ;
x ++ ) {
res = Math . max ( eggDrop ( n - 1 , x - 1 ) , eggDrop ( n , k - x ) ) ;
if ( res < min ) min = res ;
}
return... |
java-cpp-PROGRAM_COUNT_OCCURRENCE_GIVEN_CHARACTER_STRING | java-cpp | public static int count ( String s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) {
if ( s . charAt ( i ) == c ) res ++ ;
}
return res ;
}
| int count ( string s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) if ( s [ i ] == c ) res ++ ;
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int count ( String s , char c ) {
int res = 0 ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) {
if ( s . charAt ( i ) == c ) res ++ ;
}
return res ;
}
</Code> |
java-cpp-MINIMUM_ROTATIONS_REQUIRED_GET_STRING | java-cpp | static int findRotations ( String str ) {
String tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
String substring = tmp . substring ( i , str . length ( ) ) ;
if ( str == substring ) return i ;
}
return n ;
}
| int findRotations ( string str ) {
string tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
string substring = tmp . substr ( i , str . size ( ) ) ;
if ( str == substring ) return i ;
}
return n ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findRotations ( String str ) {
String tmp = str + str ;
int n = str . length ( ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
String substring = tmp . substring ( i , str . length ( ) ) ;
if ( str == substring ) return i ;
}
return n ;
}
</Code> |
java-cpp-SHUFFLE_A_GIVEN_ARRAY | java-cpp | static void randomize ( int arr [ ] , int n ) {
Random r = new Random ( ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = r . nextInt ( i + 1 ) ;
int temp = arr [ i ] ;
arr [ i ] = arr [ j ] ;
arr [ j ] = temp ;
}
System . out . println ( Arrays . toString ( arr ) ) ;
}
| void randomize ( int arr [ ] , int n ) {
srand ( time ( NULL ) ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = rand ( ) % ( i + 1 ) ;
swap ( & arr [ i ] , & arr [ j ] ) ;
}
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void randomize ( int arr [ ] , int n ) {
Random r = new Random ( ) ;
for ( int i = n - 1 ;
i > 0 ;
i -- ) {
int j = r . nextInt ( i + 1 ) ;
int temp = arr [ i ] ;
arr [ i ] = arr [ j ] ;
arr [ j ] = temp ;
}
System . out . println ( Arrays . toString ( arr ) ) ;
}
</Code> |
java-cpp-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_2 | java-cpp | static int findLength ( String str , int n ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str . charAt ( i - 1 ) - ' 0 ' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
i +... | int findLength ( string str , int n ) {
int sum [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str [ i - 1 ] - '0' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
i ++ ) {
int j = i + len - 1 ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findLength ( String str , int n ) {
int sum [ ] = new int [ n + 1 ] ;
sum [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum [ i ] = ( sum [ i - 1 ] + str . charAt ( i - 1 ) - ' 0 ' ) ;
int ans = 0 ;
for ( int len = 2 ;
len <= n ;
len += 2 ) {
for ( int i = 0 ;
i <= n - len ;
... |
java-cpp-REPLACE_OCCURRENCES_STRING_AB_C_WITHOUT_USING_EXTRA_SPACE_1 | java-cpp | static void translate ( char str [ ] ) {
int len = str . length ;
if ( len < 2 ) return ;
int i = 0 ;
int j = 0 ;
while ( j < len - 1 ) {
if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) {
j = j + 2 ;
str [ i ++ ] = ' C ' ;
continue ;
}
str [ i ++ ] = str [ j ++ ] ;
}
if (... | void translate ( char * str ) {
int len = strlen ( str ) ;
if ( len < 2 ) return ;
int i = 0 ;
int j = 0 ;
while ( j < len - 1 ) {
if ( str [ j ] == 'A' && str [ j + 1 ] == 'B' ) {
j = j + 2 ;
str [ i ++ ] = 'C' ;
continue ;
}
str [ i ++ ] = str [ j ++ ] ;
}
if ( j == len - 1... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void translate ( char str [ ] ) {
int len = str . length ;
if ( len < 2 ) return ;
int i = 0 ;
int j = 0 ;
while ( j < len - 1 ) {
if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) {
j = j + 2 ;
str [ i ++ ] = ' C ' ;
continue ;
}
str [ i ++ ] = str [ j ++ ] ;
}... |
java-cpp-NUMBER_WHICH_HAS_THE_MAXIMUM_NUMBER_OF_DISTINCT_PRIME_FACTORS_IN_RANGE_M_TO_N | java-cpp | static int maximumNumberDistinctPrimeRange ( int m , int n ) {
long factorCount [ ] = new long [ n + 1 ] ;
boolean prime [ ] = new boolean [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
factorCount [ i ] = 0 ;
prime [ i ] = true ;
}
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( prime [ i ] == ... | int maximumNumberDistinctPrimeRange ( int m , int n ) {
long long factorCount [ n + 1 ] ;
bool prime [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
factorCount [ i ] = 0 ;
prime [ i ] = true ;
}
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( prime [ i ] == true ) {
factorCount [ i ] = 1 ;... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maximumNumberDistinctPrimeRange ( int m , int n ) {
long factorCount [ ] = new long [ n + 1 ] ;
boolean prime [ ] = new boolean [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
factorCount [ i ] = 0 ;
prime [ i ] = true ;
}
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( prime [ ... |
java-cpp-PROGRAM_NTH_CATALAN_NUMBER | java-cpp | int catalan ( int n ) {
int res = 0 ;
if ( n <= 1 ) {
return 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
res += catalan ( i ) * catalan ( n - i - 1 ) ;
}
return res ;
}
| unsigned long int catalan ( unsigned int n ) {
if ( n <= 1 ) return 1 ;
unsigned long int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) res += catalan ( i ) * catalan ( n - i - 1 ) ;
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int catalan ( int n ) {
int res = 0 ;
if ( n <= 1 ) {
return 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
res += catalan ( i ) * catalan ( n - i - 1 ) ;
}
return res ;
}
</Code> |
java-cpp-SUM_SERIES_12_32_52_2N_12 | java-cpp | static int sumOfSeries ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ;
return sum ;
}
| int sumOfSeries ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ;
return sum ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int sumOfSeries ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) sum = sum + ( 2 * i - 1 ) * ( 2 * i - 1 ) ;
return sum ;
}
</Code> |
java-cpp-MINIMUM_REVOLUTIONS_MOVE_CENTER_CIRCLE_TARGET | java-cpp | static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return Math . ceil ( d / ( 2 * r ) ) ;
}
| int minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return ceil ( d / ( 2 * r ) ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static double minRevolutions ( double r , int x1 , int y1 , int x2 , int y2 ) {
double d = Math . sqrt ( ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ) ;
return Math . ceil ( d / ( 2 * r ) ) ;
}
</Code> |
java-cpp-PRINT_STRING_SPECIFIED_CHARACTER_OCCURRED_GIVEN_NO_TIMES | java-cpp | static void printString ( String str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
System . out . println ( str ) ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . length ( ) ... | void printString ( string str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
cout << str ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . length ( ) - 1 ) cout << str . substr ( i + 1 ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printString ( String str , char ch , int count ) {
int occ = 0 , i ;
if ( count == 0 ) {
System . out . println ( str ) ;
return ;
}
for ( i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ch ) occ ++ ;
if ( occ == count ) break ;
}
if ( i < str . leng... |
java-cpp-FINDING_THE_MAXIMUM_SQUARE_SUB_MATRIX_WITH_ALL_EQUAL_ELEMENTS | java-cpp | static int largestKSubmatrix ( int [ ] [ ] a ) {
int [ ] [ ] dp = new int [ Row ] [ Col ] ;
int result = 0 ;
for ( int i = 0 ;
i < Row ;
i ++ ) {
for ( int j = 0 ;
j < Col ;
j ++ ) {
if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ;
else {
if ( a [ i ] [ j ] == a [ i - 1 ] [ j ] && ... | int largestKSubmatrix ( int a [ ] [ Col ] ) {
int dp [ Row ] [ Col ] ;
memset ( dp , sizeof ( dp ) , 0 ) ;
int result = 0 ;
for ( int i = 0 ;
i < Row ;
i ++ ) {
for ( int j = 0 ;
j < Col ;
j ++ ) {
if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ;
else {
if ( a [ i ] [ j ] == a [ ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int largestKSubmatrix ( int [ ] [ ] a ) {
int [ ] [ ] dp = new int [ Row ] [ Col ] ;
int result = 0 ;
for ( int i = 0 ;
i < Row ;
i ++ ) {
for ( int j = 0 ;
j < Col ;
j ++ ) {
if ( i == 0 || j == 0 ) dp [ i ] [ j ] = 1 ;
else {
if ( a [ i ] [ j ] == a [ i - 1 ] [ ... |
java-cpp-PASCAL_TRIANGLE | java-cpp | public static void printPascal ( int n ) {
int [ ] [ ] arr = new int [ n ] [ n ] ;
for ( int line = 0 ;
line < n ;
line ++ ) {
for ( int i = 0 ;
i <= line ;
i ++ ) {
if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ;
else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ line - 1 ... | void printPascal ( int n ) {
int arr [ n ] [ n ] ;
for ( int line = 0 ;
line < n ;
line ++ ) {
for ( int i = 0 ;
i <= line ;
i ++ ) {
if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ;
else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ line - 1 ] [ i ] ;
cout << arr [ li... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static void printPascal ( int n ) {
int [ ] [ ] arr = new int [ n ] [ n ] ;
for ( int line = 0 ;
line < n ;
line ++ ) {
for ( int i = 0 ;
i <= line ;
i ++ ) {
if ( line == i || i == 0 ) arr [ line ] [ i ] = 1 ;
else arr [ line ] [ i ] = arr [ line - 1 ] [ i - 1 ] + arr [ li... |
java-cpp-NUMBER_INDEXES_EQUAL_ELEMENTS_GIVEN_RANGE | java-cpp | static int answer_query ( int a [ ] , int n , int l , int r ) {
int count = 0 ;
for ( int i = l ;
i < r ;
i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ;
return count ;
}
| int answer_query ( int a [ ] , int n , int l , int r ) {
int count = 0 ;
for ( int i = l ;
i < r ;
i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ;
return count ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int answer_query ( int a [ ] , int n , int l , int r ) {
int count = 0 ;
for ( int i = l ;
i < r ;
i ++ ) if ( a [ i ] == a [ i + 1 ] ) count += 1 ;
return count ;
}
</Code> |
java-cpp-CASSINIS_IDENTITY | java-cpp | static int cassini ( int n ) {
return ( n & 1 ) != 0 ? - 1 : 1 ;
}
| int cassini ( int n ) {
return ( n & 1 ) ? - 1 : 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int cassini ( int n ) {
return ( n & 1 ) != 0 ? - 1 : 1 ;
}
</Code> |
java-cpp-COMPUTE_AVERAGE_TWO_NUMBERS_WITHOUT_OVERFLOW | java-cpp | static int compute_average ( int a , int b ) {
return ( a + b ) / 2 ;
}
| int compute_average ( int a , int b ) {
return ( a + b ) / 2 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int compute_average ( int a , int b ) {
return ( a + b ) / 2 ;
}
</Code> |
java-cpp-NUMBER_OF_WAYS_TO_ARRANGE_N_ITEMS_UNDER_GIVEN_CONSTRAINTS | java-cpp | static int waysToArrange ( int N , int K , int [ ] k ) {
int [ ] [ ] C = new int [ N + 1 ] [ N + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= N ;
i ++ ) {
for ( j = 0 ;
j <= i ;
j ++ ) {
if ( j == 0 || j == i ) {
C [ i ] [ j ] = 1 ;
}
else {
C [ i ] [ j ] = ( C [ i - 1 ] ... | int waysToArrange ( int N , int K , int k [ ] ) {
int C [ N + 1 ] [ N + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= N ;
i ++ ) {
for ( j = 0 ;
j <= i ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ;
}
}
int dp ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int waysToArrange ( int N , int K , int [ ] k ) {
int [ ] [ ] C = new int [ N + 1 ] [ N + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= N ;
i ++ ) {
for ( j = 0 ;
j <= i ;
j ++ ) {
if ( j == 0 || j == i ) {
C [ i ] [ j ] = 1 ;
}
else {
C [ i ] [ j ] = ( C [ i... |
java-cpp-FIND_DIMENSIONS_RIGHT_ANGLED_TRIANGLE | java-cpp | static void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
System . out . println ( "Not Possible" ) ;
return ;
}
double apb = Math . sqrt ( H * H + 4 * A ) ;
double asb = Math . sqrt ( H * H - 4 * A ) ;
System . out . println ( "P = " + Math . round ( ( ( apb - asb ) / 2.0 ) * 100.0 ) / 100.0 )... | void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
cout << "Not Possible\n" ;
return ;
}
double apb = sqrt ( H * H + 4 * A ) ;
double asb = sqrt ( H * H - 4 * A ) ;
cout . precision ( 2 ) ;
cout << "P = " << fixed << ( apb - asb ) / 2.0 << "\n" ;
cout << "B = " << ( apb + asb ) / 2.0 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void findDimen ( int H , int A ) {
if ( H * H < 4 * A ) {
System . out . println ( "Not Possible" ) ;
return ;
}
double apb = Math . sqrt ( H * H + 4 * A ) ;
double asb = Math . sqrt ( H * H - 4 * A ) ;
System . out . println ( "P = " + Math . round ( ( ( apb - asb ) / 2.0 ) * 100.0 ) / ... |
java-cpp-MAXIMUM_TRIPLET_SUM_ARRAY_1 | java-cpp | static int maxTripletSum ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ;
}
| int maxTripletSum ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxTripletSum ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
return arr [ n - 1 ] + arr [ n - 2 ] + arr [ n - 3 ] ;
}
</Code> |
java-cpp-FIND_MAXIMUM_DOT_PRODUCT_TWO_ARRAYS_INSERTION_0S | java-cpp | static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ... | long long int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
long long int dp [ n + 1 ] [ m + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [ j - 1 ] * B [ i - 1 ] ) ) , dp [ i ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int MaxDotProduct ( int A [ ] , int B [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
for ( int [ ] row : dp ) Arrays . fill ( row , 0 ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= m ;
j ++ ) dp [ i ] [ j ] = Math . max ( ( dp [ i - 1 ] [ j - 1 ] + ( A [... |
java-cpp-FIND_MINIMUM_DIFFERENCE_PAIR_1 | java-cpp | static int findMinDiff ( int [ ] arr , int n ) {
Arrays . sort ( arr ) ;
int diff = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ;
return diff ;
}
| int findMinDiff ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
int diff = INT_MAX ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ;
return diff ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findMinDiff ( int [ ] arr , int n ) {
Arrays . sort ( arr ) ;
int diff = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( arr [ i + 1 ] - arr [ i ] < diff ) diff = arr [ i + 1 ] - arr [ i ] ;
return diff ;
}
</Code> |
java-cpp-FIND_LAST_INDEX_CHARACTER_STRING_1 | java-cpp | static int findLastIndex ( String str , Character x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str . charAt ( i ) == x ) return i ;
return - 1 ;
}
| int findLastIndex ( string & str , char x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str [ i ] == x ) return i ;
return - 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int findLastIndex ( String str , Character x ) {
for ( int i = str . length ( ) - 1 ;
i >= 0 ;
i -- ) if ( str . charAt ( i ) == x ) return i ;
return - 1 ;
}
</Code> |
java-cpp-MAXIMUM_DISTINCT_LOWERCASE_ALPHABETS_TWO_UPPERCASE | java-cpp | static int maxLower ( String str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ ] = new int [ MAX_CHAR ] ;
for ( ;
i < n ;
i ++ ) {
if ... | int maxLower ( string str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str [ i ] >= 'A' && str [ i ] <= 'Z' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ MAX_CHAR ] = {
0 }
;
for ( ;
i < n ;
i ++ ) {
if ( str [ i ] >= 'A' ... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxLower ( String str ) {
int n = str . length ( ) ;
int i = 0 ;
for ( ;
i < n ;
i ++ ) {
if ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) {
i ++ ;
break ;
}
}
int maxCount = 0 ;
int count [ ] = new int [ MAX_CHAR ] ;
for ( ;
i < n ;
i ++ ) {
... |
java-cpp-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS | java-cpp | static void findElements ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ;
if ( count >= 2 ) System . out . print ( arr [ i ] + " " ) ;
}
}
| void findElements ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ;
if ( count >= 2 ) cout << arr [ i ] << " " ;
}
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void findElements ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ;
if ( count >= 2 ) System . out . print ( arr [ i ] + " " ) ;
}
}
</Code> |
java-cpp-NTH_EVEN_LENGTH_PALINDROME | java-cpp | static String evenlength ( String n ) {
String res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n . charAt ( j ) ;
return res ;
}
| string evenlength ( string n ) {
string res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n [ j ] ;
return res ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static String evenlength ( String n ) {
String res = n ;
for ( int j = n . length ( ) - 1 ;
j >= 0 ;
-- j ) res += n . charAt ( j ) ;
return res ;
}
</Code> |
java-cpp-LARGEST_SUBARRAY_WITH_EQUAL_NUMBER_OF_0S_AND_1S_1 | java-cpp | int maxLen ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
int start_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
}
for ( int i = 0 ;
i < n... | int maxLen ( int arr [ ] , int n ) {
unordered_map < int , int > hM ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
if ( sum == 0 ) {
max_len = i... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
int maxLen ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
int ending_index = - 1 ;
int start_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = ( arr [ i ] == 0 ) ? - 1 : 1 ;
}
for ( int i = 0 ;
... |
java-cpp-COUNT_NUMBER_OF_STRINGS_MADE_OF_R_G_AND_B_USING_GIVEN_COMBINATION | java-cpp | static int possibleStrings ( int n , int r , int b , int g ) {
int fact [ ] = new int [ n + 1 ] ;
fact [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) fact [ i ] = fact [ i - 1 ] * i ;
int left = n - ( r + g + b ) ;
int sum = 0 ;
for ( int i = 0 ;
i <= left ;
i ++ ) {
for ( int j = 0 ;
j <= le... | int possibleStrings ( int n , int r , int b , int g ) {
int fact [ n + 1 ] ;
fact [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) fact [ i ] = fact [ i - 1 ] * i ;
int left = n - ( r + g + b ) ;
int sum = 0 ;
for ( int i = 0 ;
i <= left ;
i ++ ) {
for ( int j = 0 ;
j <= left - i ;
j ++ ) {... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int possibleStrings ( int n , int r , int b , int g ) {
int fact [ ] = new int [ n + 1 ] ;
fact [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) fact [ i ] = fact [ i - 1 ] * i ;
int left = n - ( r + g + b ) ;
int sum = 0 ;
for ( int i = 0 ;
i <= left ;
i ++ ) {
for ( int j = 0 ;
... |
java-cpp-NUMBER_WAYS_INSERT_CHARACTER_INCREASE_LCS_ONE | java-cpp | static int numberofways ( String A , String B , int N , int M ) {
Vector < Integer > [ ] pos = new Vector [ MAX ] ;
for ( int i = 0 ;
i < MAX ;
i ++ ) pos [ i ] = new Vector < > ( ) ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B . charAt ( i ) ] . add ( i + 1 ) ;
int [ ] [ ] dpl = new int [ N + 2 ] [ M + 2 ... | int numberofways ( string A , string B , int N , int M ) {
vector < int > pos [ MAX ] ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B [ i ] ] . push_back ( i + 1 ) ;
int dpl [ N + 2 ] [ M + 2 ] ;
memset ( dpl , 0 , sizeof ( dpl ) ) ;
for ( int i = 1 ;
i <= N ;
i ++ ) {
for ( int j = 1 ;
j <= M ;
... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int numberofways ( String A , String B , int N , int M ) {
Vector < Integer > [ ] pos = new Vector [ MAX ] ;
for ( int i = 0 ;
i < MAX ;
i ++ ) pos [ i ] = new Vector < > ( ) ;
for ( int i = 0 ;
i < M ;
i ++ ) pos [ B . charAt ( i ) ] . add ( i + 1 ) ;
int [ ] [ ] dpl = new int [ N + 2 ] [... |
java-cpp-PROGRAM_FIND_CIRCUMFERENCE_CIRCLE | java-cpp | static double circumference ( double r ) {
double PI = 3.1415 ;
double cir = 2 * PI * r ;
return cir ;
}
| double circumference ( double r ) {
double cir = 2 * PI * r ;
return cir ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static double circumference ( double r ) {
double PI = 3.1415 ;
double cir = 2 * PI * r ;
return cir ;
}
</Code> |
java-cpp-CONVERT_SUBSTRINGS_LENGTH_K_BASE_B_DECIMAL | java-cpp | static void substringConversions ( String str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . length ( ) ;
i ++ ) {
String sub = str . substring ( i , i + k ) ;
int sum = 0 , counter = 0 ;
for ( int j = sub . length ( ) - 1 ;
j >= 0 ;
j -- ) {
sum = ( int ) ( sum + ( ( sub . charAt ... | int substringConversions ( string str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . size ( ) ;
i ++ ) {
string sub = str . substr ( i , k ) ;
int sum = 0 , counter = 0 ;
for ( int i = sub . size ( ) - 1 ;
i >= 0 ;
i -- ) {
sum = sum + ( ( sub . at ( i ) - '0' ) * pow ( b , counter... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void substringConversions ( String str , int k , int b ) {
for ( int i = 0 ;
i + k <= str . length ( ) ;
i ++ ) {
String sub = str . substring ( i , i + k ) ;
int sum = 0 , counter = 0 ;
for ( int j = sub . length ( ) - 1 ;
j >= 0 ;
j -- ) {
sum = ( int ) ( sum + ( ( sub . ... |
java-cpp-MAXIMUM_PATH_SUM_TRIANGLE | java-cpp | static int maxPathSum ( int tri [ ] [ ] , int m , int n ) {
for ( int i = m - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j <= i ;
j ++ ) {
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ;
else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ;
}
}
... | int maxPathSum ( int tri [ ] [ N ] , int m , int n ) {
for ( int i = m - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j <= i ;
j ++ ) {
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ;
else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ;
}
}
ret... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int maxPathSum ( int tri [ ] [ ] , int m , int n ) {
for ( int i = m - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j <= i ;
j ++ ) {
if ( tri [ i + 1 ] [ j ] > tri [ i + 1 ] [ j + 1 ] ) tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ;
else tri [ i ] [ j ] += tri [ i + 1 ] [ j + 1 ] ;
... |
java-cpp-SUM_MINIMUM_MAXIMUM_ELEMENTS_SUBARRAYS_SIZE_K | java-cpp | public static int SumOfKsubArray ( int arr [ ] , int k ) {
int sum = 0 ;
Deque < Integer > S = new LinkedList < > ( ) , G = new LinkedList < > ( ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ;
while ( ! G . isEm... | int SumOfKsubArray ( int arr [ ] , int n , int k ) {
int sum = 0 ;
deque < int > S ( k ) , G ( k ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ( ! S . empty ( ) ) && arr [ S . back ( ) ] >= arr [ i ] ) S . pop_back ( ) ;
while ( ( ! G . empty ( ) ) && arr [ G . back ( ) ] <= arr [ i ] ) G .... | Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
public static int SumOfKsubArray ( int arr [ ] , int k ) {
int sum = 0 ;
Deque < Integer > S = new LinkedList < > ( ) , G = new LinkedList < > ( ) ;
int i = 0 ;
for ( i = 0 ;
i < k ;
i ++ ) {
while ( ! S . isEmpty ( ) && arr [ S . peekLast ( ) ] >= arr [ i ] ) S . removeLast ( ) ;
while ( ! G... |
java-cpp-TURN_OFF_THE_RIGHTMOST_SET_BIT | java-cpp | static int fun ( int n ) {
return n & ( n - 1 ) ;
}
| int fun ( unsigned int n ) {
return n & ( n - 1 ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int fun ( int n ) {
return n & ( n - 1 ) ;
}
</Code> |
java-cpp-FIND_MIRROR_IMAGE_POINT_2_D_PLANE | java-cpp | static pair mirrorImage ( double a , double b , double c , double x1 , double y1 ) {
double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ;
double x = temp * a + x1 ;
double y = temp * b + y1 ;
return new pair ( x , y ) ;
}
| pair < double , double > mirrorImage ( double a , double b , double c , double x1 , double y1 ) {
double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ;
double x = temp * a + x1 ;
double y = temp * b + y1 ;
return make_pair ( x , y ) ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static pair mirrorImage ( double a , double b , double c , double x1 , double y1 ) {
double temp = - 2 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ;
double x = temp * a + x1 ;
double y = temp * b + y1 ;
return new pair ( x , y ) ;
}
</Code> |
java-cpp-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY_1 | java-cpp | static void printDistinct ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
System . out . print ( arr [ i ] + " " ) ;
}
}
| void printDistinct ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
cout << arr [ i ] << " " ;
}
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printDistinct ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ;
System . out . print ( arr [ i ] + " " ) ;
}
}
</Code> |
java-cpp-UNION_AND_INTERSECTION_OF_TWO_SORTED_ARRAYS_2_1 | java-cpp | static void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) {
int i = 0 , j = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) i ++ ;
else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ;
else {
System . out . print ( arr2 [ j ++ ] + " " ) ;
i ++ ;
}
}
}
| int printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) {
int i = 0 , j = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) i ++ ;
else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ;
else {
cout << arr2 [ j ] << " " ;
i ++ ;
j ++ ;
}
}
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static void printIntersection ( int arr1 [ ] , int arr2 [ ] , int m , int n ) {
int i = 0 , j = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) i ++ ;
else if ( arr2 [ j ] < arr1 [ i ] ) j ++ ;
else {
System . out . print ( arr2 [ j ++ ] + " " ) ;
i ++ ;
}
}
}
</C... |
java-cpp-SEARCHING_ARRAY_ADJACENT_DIFFER_K | java-cpp | static int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . max ( 1 , Math . abs ( arr [ i ] - x ) / k ) ;
}
System . out . println ( "number is " + "not present!" ) ;
return - 1 ;
}
| int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + max ( 1 , abs ( arr [ i ] - x ) / k ) ;
}
cout << "number is not present!" ;
return - 1 ;
}
| Translate the given code from java to cpp. The input Code is marked with <Code> and </Code>. | <Code>
static int search ( int arr [ ] , int n , int x , int k ) {
int i = 0 ;
while ( i < n ) {
if ( arr [ i ] == x ) return i ;
i = i + Math . max ( 1 , Math . abs ( arr [ i ] - x ) / k ) ;
}
System . out . println ( "number is " + "not present!" ) ;
return - 1 ;
}
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.