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 |
|---|---|---|---|---|---|
cpp-java-PAIRS_OF_POSITIVE_NEGATIVE_VALUES_IN_AN_ARRAY | cpp-java | void printPairs ( int arr [ ] , int n ) {
vector < int > v ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ;
if ( v . size ( ) == 0 ) return ;
sort ( v . begin ( ) , v . end ( ) ) ;
for ( int i = ... | public static void printPairs ( int arr [ ] , int n ) {
Vector < Integer > v = new Vector < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ;
if ( v . size ( ) == 0 ) retu... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printPairs ( int arr [ ] , int n ) {
vector < int > v ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ;
if ( v . size ( ) == 0 ) return ;
sort ( v . begin ( ) , v . end ( ) ) ;
for ( i... |
cpp-java-C_PROGRAM_FIND_AREA_TRIANGLE | cpp-java | float findArea ( float a , float b , float c ) {
if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) {
cout << "Not a valid trianglen" ;
exit ( 0 ) ;
}
float s = ( a + b + c ) / 2 ;
return sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ;
}
| static float findArea ( float a , float b , float c ) {
if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) {
System . out . println ( "Not a valid triangle" ) ;
System . exit ( 0 ) ;
}
float s = ( a + b + c ) / 2 ;
return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
float findArea ( float a , float b , float c ) {
if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) {
cout << "Not a valid trianglen" ;
exit ( 0 ) ;
}
float s = ( a + b + c ) / 2 ;
return sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ;
}
</Code> |
cpp-java-CHECK_ARRAY_MAJORITY_ELEMENT | cpp-java | bool isMajority ( int a [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ a [ i ] ] ++ ;
for ( auto x : mp ) if ( x . second >= n / 2 ) return true ;
return false ;
}
| static boolean isMajority ( int a [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ;
else mp . put ( a [ i ] , 1 ) ;
for ( Map . Entry < Integer , In... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isMajority ( int a [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ a [ i ] ] ++ ;
for ( auto x : mp ) if ( x . second >= n / 2 ) return true ;
return false ;
}
</Code> |
cpp-java-NUMBER_N_DIGITS_NON_DECREASING_INTEGERS | cpp-java | int nonDecNums ( int n ) {
int a [ n + 1 ] [ 10 ] ;
for ( int i = 0 ;
i <= 9 ;
i ++ ) a [ 0 ] [ i ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) a [ i ] [ 9 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 8 ;
j >= 0 ;
j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ;
return a [... | static int nonDecNums ( int n ) {
int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ;
for ( int i = 0 ;
i <= 9 ;
i ++ ) a [ 0 ] [ i ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) a [ i ] [ 9 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 8 ;
j >= 0 ;
j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ]... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int nonDecNums ( int n ) {
int a [ n + 1 ] [ 10 ] ;
for ( int i = 0 ;
i <= 9 ;
i ++ ) a [ 0 ] [ i ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) a [ i ] [ 9 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 8 ;
j >= 0 ;
j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ;
ret... |
cpp-java-PROGRAM_FIRST_FIT_ALGORITHM_MEMORY_MANAGEMENT | cpp-java | void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ n ] ;
memset ( allocation , - 1 , sizeof ( allocation ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
allocation [ i ... | static void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ ] = new int [ n ] ;
for ( int i = 0 ;
i < allocation . length ;
i ++ ) allocation [ i ] = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ n ] ;
memset ( allocation , - 1 , sizeof ( allocation ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
allocati... |
cpp-java-NON_REPEATING_ELEMENT_1 | cpp-java | int firstNonRepeating ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( mp [ arr [ i ] ] == 1 ) return arr [ i ] ;
return - 1 ;
}
| static int firstNonRepeating ( 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 ] , 1 ) ;
}
}
for ( ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int firstNonRepeating ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( mp [ arr [ i ] ] == 1 ) return arr [ i ] ;
return - 1 ;
}
</Code> |
cpp-java-DISTRIBUTING_ALL_BALLS_WITHOUT_REPETITION | cpp-java | bool distributingBalls ( int k , int n , string str ) {
int a [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ str [ i ] - 'a' ] ++ ;
}
for ( int i = 0 ;
i < MAX_CHAR ;
i ++ ) if ( a [ i ] > k ) return false ;
return true ;
}
| static boolean distributingBalls ( long k , long n , String str ) {
int [ ] a = new int [ MAX_CHAR ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ str . charAt ( i ) - ' a ' ] ++ ;
}
for ( int i = 0 ;
i < MAX_CHAR ;
i ++ ) if ( a [ i ] > k ) return false ;
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool distributingBalls ( int k , int n , string str ) {
int a [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ str [ i ] - 'a' ] ++ ;
}
for ( int i = 0 ;
i < MAX_CHAR ;
i ++ ) if ( a [ i ] > k ) return false ;
return true ;
}
</Code> |
cpp-java-BREAK_NUMBER_THREE_PARTS_1 | cpp-java | ll count_of_ways ( ll n ) {
ll count ;
count = ( n + 1 ) * ( n + 2 ) / 2 ;
return count ;
}
| static long count_of_ways ( long n ) {
long count = 0 ;
count = ( n + 1 ) * ( n + 2 ) / 2 ;
return count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
ll count_of_ways ( ll n ) {
ll count ;
count = ( n + 1 ) * ( n + 2 ) / 2 ;
return count ;
}
</Code> |
cpp-java-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY_1 | cpp-java | int maxSum ( int arr [ ] , int n ) {
int cum_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) cum_sum += arr [ i ] ;
int curr_val = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) curr_val += i * arr [ i ] ;
int res = curr_val ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int next_val = curr_val - ( cum_sum - arr [ i... | static int maxSum ( int arr [ ] , int n ) {
int cum_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) cum_sum += arr [ i ] ;
int curr_val = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) curr_val += i * arr [ i ] ;
int res = curr_val ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int next_val = curr_val - ( cum_sum - ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSum ( int arr [ ] , int n ) {
int cum_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) cum_sum += arr [ i ] ;
int curr_val = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) curr_val += i * arr [ i ] ;
int res = curr_val ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int next_val = curr_val - ( cum_sum - ... |
cpp-java-CALCULATE_ANGLE_HOUR_HAND_MINUTE_HAND | cpp-java | int calcAngle ( double h , double m ) {
if ( h < 0 || m < 0 || h > 12 || m > 60 ) printf ( "Wrong input" ) ;
if ( h == 12 ) h = 0 ;
if ( m == 60 ) m = 0 ;
int hour_angle = 0.5 * ( h * 60 + m ) ;
int minute_angle = 6 * m ;
int angle = abs ( hour_angle - minute_angle ) ;
angle = min ( 360 - angle , angle ) ... | static int calcAngle ( double h , double m ) {
if ( h < 0 || m < 0 || h > 12 || m > 60 ) System . out . println ( "Wrong input" ) ;
if ( h == 12 ) h = 0 ;
if ( m == 60 ) m = 0 ;
int hour_angle = ( int ) ( 0.5 * ( h * 60 + m ) ) ;
int minute_angle = ( int ) ( 6 * m ) ;
int angle = Math . abs ( hour_angle - m... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int calcAngle ( double h , double m ) {
if ( h < 0 || m < 0 || h > 12 || m > 60 ) printf ( "Wrong input" ) ;
if ( h == 12 ) h = 0 ;
if ( m == 60 ) m = 0 ;
int hour_angle = 0.5 * ( h * 60 + m ) ;
int minute_angle = 6 * m ;
int angle = abs ( hour_angle - minute_angle ) ;
angle = min ( 360 - angle , a... |
cpp-java-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS_1 | cpp-java | int smallest ( int x , int y , int z ) {
if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ;
return ( ! ( x / z ) ) ? x : z ;
}
| static int smallest ( int x , int y , int z ) {
if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ;
return ( ( x / z ) != 1 ) ? x : z ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int smallest ( int x , int y , int z ) {
if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ;
return ( ! ( x / z ) ) ? x : z ;
}
</Code> |
cpp-java-FIND_ONE_EXTRA_CHARACTER_STRING_1 | cpp-java | char findExtraCharcter ( string strA , string strB ) {
int res = 0 , i ;
for ( i = 0 ;
i < strA . length ( ) ;
i ++ ) {
res ^= strA [ i ] ;
}
for ( i = 0 ;
i < strB . length ( ) ;
i ++ ) {
res ^= strB [ i ] ;
}
return ( ( char ) ( res ) ) ;
}
| static char findExtraCharcter ( String strA , String strB ) {
int res = 0 , i ;
for ( i = 0 ;
i < strA . length ( ) ;
i ++ ) {
res ^= strA . charAt ( i ) ;
}
for ( i = 0 ;
i < strB . length ( ) ;
i ++ ) {
res ^= strB . charAt ( i ) ;
}
return ( ( char ) ( res ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
char findExtraCharcter ( string strA , string strB ) {
int res = 0 , i ;
for ( i = 0 ;
i < strA . length ( ) ;
i ++ ) {
res ^= strA [ i ] ;
}
for ( i = 0 ;
i < strB . length ( ) ;
i ++ ) {
res ^= strB [ i ] ;
}
return ( ( char ) ( res ) ) ;
}
</Code> |
cpp-java-RECAMANS_SEQUENCE_1 | cpp-java | void recaman ( int n ) {
if ( n <= 0 ) return ;
printf ( "%d, " , 0 ) ;
unordered_set < int > s ;
s . insert ( 0 ) ;
int prev = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = prev - i ;
if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ;
s . insert ( curr ) ;
printf ... | static void recaman ( int n ) {
if ( n <= 0 ) return ;
System . out . printf ( "%d, " , 0 ) ;
HashSet < Integer > s = new HashSet < Integer > ( ) ;
s . add ( 0 ) ;
int prev = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = prev - i ;
if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void recaman ( int n ) {
if ( n <= 0 ) return ;
printf ( "%d, " , 0 ) ;
unordered_set < int > s ;
s . insert ( 0 ) ;
int prev = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = prev - i ;
if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ;
s . insert ( curr ) ;
... |
cpp-java-PROGRAM_FIND_SMALLEST_DIFFERENCE_ANGLES_TWO_PARTS_GIVEN_CIRCLE | cpp-java | int findMinimumAngle ( int arr [ ] , int n ) {
int l = 0 , sum = 0 , ans = 360 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
while ( sum >= 180 ) {
ans = min ( ans , 2 * abs ( 180 - sum ) ) ;
sum -= arr [ l ] ;
l ++ ;
}
ans = min ( ans , 2 * abs ( 180 - sum ) ) ;
}
... | public static int findMinimumAngle ( int arr [ ] , int n ) {
int l = 0 , sum = 0 , ans = 360 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
while ( sum >= 180 ) {
ans = Math . min ( ans , 2 * Math . abs ( 180 - sum ) ) ;
sum -= arr [ l ] ;
l ++ ;
}
ans = Math . min ( a... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findMinimumAngle ( int arr [ ] , int n ) {
int l = 0 , sum = 0 , ans = 360 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
while ( sum >= 180 ) {
ans = min ( ans , 2 * abs ( 180 - sum ) ) ;
sum -= arr [ l ] ;
l ++ ;
}
ans = min ( ans , 2 * abs ( 180 - sum ) )... |
cpp-java-COUNT_GFG_SUBSEQUENCES_GIVEN_STRING | cpp-java | void countSubsequence ( char s [ ] , int n ) {
int cntG = 0 , cntF = 0 , result = 0 , C = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
switch ( s [ i ] ) {
case 'G' : cntG ++ ;
result += C ;
break ;
case 'F' : cntF ++ ;
C += cntG ;
break ;
default : continue ;
}
}
... | static void countSubsequence ( String s , int n ) {
int cntG = 0 , cntF = 0 , result = 0 , C = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
switch ( s . charAt ( i ) ) {
case ' G ' : cntG ++ ;
result += C ;
break ;
case ' F ' : cntF ++ ;
C += cntG ;
break ;
default : cont... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void countSubsequence ( char s [ ] , int n ) {
int cntG = 0 , cntF = 0 , result = 0 , C = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
switch ( s [ i ] ) {
case 'G' : cntG ++ ;
result += C ;
break ;
case 'F' : cntF ++ ;
C += cntG ;
break ;
default : continue ;
... |
cpp-java-MAXIMUM_DIFFERENCE_BETWEEN_FREQUENCY_OF_TWO_ELEMENTS_SUCH_THAT_ELEMENT_HAVING_GREATER_FREQUENCY_IS_ALSO_GREATER | cpp-java | int maxdiff ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [ j ] ) ... | static int maxdiff ( int arr [ ] , int n ) {
Map < Integer , Integer > freq = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) freq . put ( arr [ i ] , freq . get ( arr [ i ] ) == null ? 1 : freq . get ( arr [ i ] ) + 1 ) ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxdiff ( int arr [ ] , int n ) {
unordered_map < int , int > freq ;
for ( int i = 0 ;
i < n ;
i ++ ) freq [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [... |
cpp-java-CHECK_IF_TWO_ARRAYS_ARE_EQUAL_OR_NOT | cpp-java | bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
if ( n != m ) return false ;
sort ( arr1 , arr1 + n ) ;
sort ( arr2 , arr2 + m ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ;
return true ;
}
| public static boolean areEqual ( int arr1 [ ] , int arr2 [ ] ) {
int n = arr1 . length ;
int m = arr2 . length ;
if ( n != m ) return false ;
Arrays . sort ( arr1 ) ;
Arrays . sort ( arr2 ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ;
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
if ( n != m ) return false ;
sort ( arr1 , arr1 + n ) ;
sort ( arr2 , arr2 + m ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ;
return true ;
}
</Code> |
cpp-java-FRIENDS_PAIRING_PROBLEM_2 | cpp-java | int countFriendsPairings ( int n ) {
int a = 1 , b = 2 , c = 0 ;
if ( n <= 2 ) {
return n ;
}
for ( int i = 3 ;
i <= n ;
i ++ ) {
c = b + ( i - 1 ) * a ;
a = b ;
b = c ;
}
return c ;
}
| static int countFriendsPairings ( int n ) {
int a = 1 , b = 2 , c = 0 ;
if ( n <= 2 ) {
return n ;
}
for ( int i = 3 ;
i <= n ;
i ++ ) {
c = b + ( i - 1 ) * a ;
a = b ;
b = c ;
}
return c ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countFriendsPairings ( int n ) {
int a = 1 , b = 2 , c = 0 ;
if ( n <= 2 ) {
return n ;
}
for ( int i = 3 ;
i <= n ;
i ++ ) {
c = b + ( i - 1 ) * a ;
a = b ;
b = c ;
}
return c ;
}
</Code> |
cpp-java-FINDING_POWER_PRIME_NUMBER_P_N | cpp-java | int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
int temp = p ;
while ( temp <= n ) {
ans += n / temp ;
temp = temp * p ;
}
return ans ;
}
| static int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
int count = 0 , temp = i ;
while ( temp % p == 0 ) {
count ++ ;
temp = temp / p ;
}
ans += count ;
}
return ans ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
int temp = p ;
while ( temp <= n ) {
ans += n / temp ;
temp = temp * p ;
}
return ans ;
}
</Code> |
cpp-java-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF | cpp-java | int findLength ( char * str ) {
int n = strlen ( str ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j += 2 ) {
int length = j - i + 1 ;
int leftsum = 0 , rightsum = 0 ;
for ( int k = 0 ;
k < length / 2 ;
k ++ ) {
leftsum ... | static int findLength ( String str ) {
int n = str . length ( ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j += 2 ) {
int length = j - i + 1 ;
int leftsum = 0 , rightsum = 0 ;
for ( int k = 0 ;
k < length / 2 ;
k ++ ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findLength ( char * str ) {
int n = strlen ( str ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j += 2 ) {
int length = j - i + 1 ;
int leftsum = 0 , rightsum = 0 ;
for ( int k = 0 ;
k < length / 2 ;
k ++ ) {
l... |
cpp-java-ANALYSIS_OF_ALGORITHMS_SET_2_ASYMPTOTIC_ANALYSIS | cpp-java | int search ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| static int search ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) {
return i ;
}
}
return - 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int search ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
</Code> |
cpp-java-SUBARRAYSUBSTRING_VS_SUBSEQUENCE_AND_PROGRAMS_TO_GENERATE_THEM | cpp-java | void subArray ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
for ( int k = i ;
k <= j ;
k ++ ) cout << arr [ k ] << " " ;
cout << endl ;
}
}
}
| static void subArray ( int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
for ( int k = i ;
k <= j ;
k ++ ) System . out . print ( arr [ k ] + " " ) ;
}
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void subArray ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
for ( int k = i ;
k <= j ;
k ++ ) cout << arr [ k ] << " " ;
cout << endl ;
}
}
}
</Code> |
cpp-java-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8_1 | cpp-java | bool isSubSeqDivisible ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ 10 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
int arr [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) arr [ i ] = str [ i - 1 ] - '0' ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] [ arr [ i ] % 8 ] = 1 ;
for ( ... | static boolean isSubSeqDivisible ( String str ) {
int n = str . length ( ) ;
int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ;
int arr [ ] = new int [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) arr [ i ] = ( int ) ( str . charAt ( i - 1 ) - ' 0 ' ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] [ arr [... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isSubSeqDivisible ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ 10 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
int arr [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) arr [ i ] = str [ i - 1 ] - '0' ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] [ arr [ i ] % 8 ] = 1 ;
... |
cpp-java-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM | cpp-java | void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) {
if ( par > k ) return ;
if ( par == k && i == n - 1 ) {
ans = min ( ans , current_ans ) ;
return ;
}
for ( int j = i + 1 ;
j < n ;
j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a [ j... | static void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) {
if ( par > k ) return ;
if ( par == k && i == n - 1 ) {
ans = Math . min ( ans , current_ans ) ;
return ;
}
for ( int j = i + 1 ;
j < n ;
j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) {
if ( par > k ) return ;
if ( par == k && i == n - 1 ) {
ans = min ( ans , current_ans ) ;
return ;
}
for ( int j = i + 1 ;
j < n ;
j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ... |
cpp-java-SQUARE_ROOT_OF_A_PERFECT_SQUARE | cpp-java | public : float squareRoot ( float n ) {
float x = n ;
float y = 1 ;
float e = 0.000001 ;
while ( x - y > e ) {
x = ( x + y ) / 2 ;
y = n / x ;
}
return x ;
}
| static float squareRoot ( float n ) {
float x = n ;
float y = 1 ;
double e = 0.000001 ;
while ( x - y > e ) {
x = ( x + y ) / 2 ;
y = n / x ;
}
return x ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
public : float squareRoot ( float n ) {
float x = n ;
float y = 1 ;
float e = 0.000001 ;
while ( x - y > e ) {
x = ( x + y ) / 2 ;
y = n / x ;
}
return x ;
}
</Code> |
cpp-java-COUNT_NUMBER_PAIRS_N_B_N_GCD_B_B | cpp-java | int CountPairs ( int n ) {
int k = n ;
int imin = 1 ;
int ans = 0 ;
while ( imin <= n ) {
int imax = n / k ;
ans += k * ( imax - imin + 1 ) ;
imin = imax + 1 ;
k = n / imin ;
}
return ans ;
}
| static int CountPairs ( int n ) {
int k = n ;
int imin = 1 ;
int ans = 0 ;
while ( imin <= n ) {
int imax = n / k ;
ans += k * ( imax - imin + 1 ) ;
imin = imax + 1 ;
k = n / imin ;
}
return ans ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int CountPairs ( int n ) {
int k = n ;
int imin = 1 ;
int ans = 0 ;
while ( imin <= n ) {
int imax = n / k ;
ans += k * ( imax - imin + 1 ) ;
imin = imax + 1 ;
k = n / imin ;
}
return ans ;
}
</Code> |
cpp-java-MULTIPLY_AN_INTEGER_WITH_3_5 | cpp-java | int multiplyWith3Point5 ( int x ) {
return ( x << 1 ) + x + ( x >> 1 ) ;
}
| static int multiplyWith3Point5 ( int x ) {
return ( x << 1 ) + x + ( x >> 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int multiplyWith3Point5 ( int x ) {
return ( x << 1 ) + x + ( x >> 1 ) ;
}
</Code> |
cpp-java-LEXICOGRAPHICALLY_PREVIOUS_PERMUTATION_IN_C | cpp-java | bool prevPermutation ( string & str ) {
int n = str . length ( ) - 1 ;
int i = n ;
while ( i > 0 && str [ i - 1 ] <= str [ i ] ) i -- ;
if ( i <= 0 ) return false ;
int j = i - 1 ;
while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) j ++ ;
swap ( str [ i - 1 ] , str [ j ] ) ;
reverse ( str . begin ( ... | static boolean prevPermutation ( char [ ] str ) {
int n = str . length - 1 ;
int i = n ;
while ( i > 0 && str [ i - 1 ] <= str [ i ] ) {
i -- ;
}
if ( i <= 0 ) {
return false ;
}
int j = i - 1 ;
while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) {
j ++ ;
}
swap ( str , i - 1 , j ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool prevPermutation ( string & str ) {
int n = str . length ( ) - 1 ;
int i = n ;
while ( i > 0 && str [ i - 1 ] <= str [ i ] ) i -- ;
if ( i <= 0 ) return false ;
int j = i - 1 ;
while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) j ++ ;
swap ( str [ i - 1 ] , str [ j ] ) ;
reverse ( str . b... |
cpp-java-AREA_OF_A_POLYGON_WITH_GIVEN_N_ORDERED_VERTICES | cpp-java | double polygonArea ( double X [ ] , double Y [ ] , int n ) {
double area = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ;
j = i ;
}
return abs ( area / 2.0 ) ;
}
| public static double polygonArea ( double X [ ] , double Y [ ] , int n ) {
double area = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ;
j = i ;
}
return Math . abs ( area / 2.0 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
double polygonArea ( double X [ ] , double Y [ ] , int n ) {
double area = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ;
j = i ;
}
return abs ( area / 2.0 ) ;
}
</Code> |
cpp-java-EQUILIBRIUM_INDEX_OF_AN_ARRAY | cpp-java | int equilibrium ( int arr [ ] , int n ) {
int i , j ;
int leftsum , rightsum ;
for ( i = 0 ;
i < n ;
++ i ) {
leftsum = 0 ;
for ( j = 0 ;
j < i ;
j ++ ) leftsum += arr [ j ] ;
rightsum = 0 ;
for ( j = i + 1 ;
j < n ;
j ++ ) rightsum += arr [ j ] ;
if ( leftsum == rightsum )... | int equilibrium ( int arr [ ] , int n ) {
int i , j ;
int leftsum , rightsum ;
for ( i = 0 ;
i < n ;
++ i ) {
leftsum = 0 ;
for ( j = 0 ;
j < i ;
j ++ ) leftsum += arr [ j ] ;
rightsum = 0 ;
for ( j = i + 1 ;
j < n ;
j ++ ) rightsum += arr [ j ] ;
if ( leftsum == rightsum )... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int equilibrium ( int arr [ ] , int n ) {
int i , j ;
int leftsum , rightsum ;
for ( i = 0 ;
i < n ;
++ i ) {
leftsum = 0 ;
for ( j = 0 ;
j < i ;
j ++ ) leftsum += arr [ j ] ;
rightsum = 0 ;
for ( j = i + 1 ;
j < n ;
j ++ ) rightsum += arr [ j ] ;
if ( leftsum == rig... |
cpp-java-COUNT_WAYS_DIVIDE_CIRCLE_USING_N_NON_INTERSECTING_CHORDS | cpp-java | int chordCnt ( int A ) {
int n = 2 * A ;
int dpArray [ n + 1 ] = {
0 }
;
dpArray [ 0 ] = 1 ;
dpArray [ 2 ] = 1 ;
for ( int i = 4 ;
i <= n ;
i += 2 ) {
for ( int j = 0 ;
j < i - 1 ;
j += 2 ) {
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ;
}
... | static int chordCnt ( int A ) {
int n = 2 * A ;
int [ ] dpArray = new int [ n + 1 ] ;
dpArray [ 0 ] = 1 ;
dpArray [ 2 ] = 1 ;
for ( int i = 4 ;
i <= n ;
i += 2 ) {
for ( int j = 0 ;
j < i - 1 ;
j += 2 ) {
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ;
}
}
return dpA... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int chordCnt ( int A ) {
int n = 2 * A ;
int dpArray [ n + 1 ] = {
0 }
;
dpArray [ 0 ] = 1 ;
dpArray [ 2 ] = 1 ;
for ( int i = 4 ;
i <= n ;
i += 2 ) {
for ( int j = 0 ;
j < i - 1 ;
j += 2 ) {
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ;
... |
cpp-java-FIND_HARMONIC_MEAN_USING_ARITHMETIC_MEAN_GEOMETRIC_MEAN | cpp-java | double compute ( int a , int b ) {
double AM , GM , HM ;
AM = ( a + b ) / 2 ;
GM = sqrt ( a * b ) ;
HM = ( GM * GM ) / AM ;
return HM ;
}
| static double compute ( int a , int b ) {
double AM , GM , HM ;
AM = ( a + b ) / 2 ;
GM = Math . sqrt ( a * b ) ;
HM = ( GM * GM ) / AM ;
return HM ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
double compute ( int a , int b ) {
double AM , GM , HM ;
AM = ( a + b ) / 2 ;
GM = sqrt ( a * b ) ;
HM = ( GM * GM ) / AM ;
return HM ;
}
</Code> |
cpp-java-MAJORITY_ELEMENT | cpp-java | void findMajority ( int arr [ ] , int n ) {
int maxCount = 0 ;
int index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count > maxCount ) {
maxCount = count ;
index = i ;
... | static void findMajority ( int arr [ ] , int n ) {
int maxCount = 0 ;
int index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count > maxCount ) {
maxCount = count ;
index ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void findMajority ( int arr [ ] , int n ) {
int maxCount = 0 ;
int index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count > maxCount ) {
maxCount = count ;
index ... |
cpp-java-CHECK_NUMBER_IS_PERFECT_SQUARE_USING_ADDITIONSUBTRACTION | cpp-java | bool isPerfectSquare ( int n ) {
for ( int sum = 0 , i = 1 ;
sum < n ;
i += 2 ) {
sum += i ;
if ( sum == n ) return true ;
}
return false ;
}
| static boolean isPerfectSquare ( int n ) {
for ( int sum = 0 , i = 1 ;
sum < n ;
i += 2 ) {
sum += i ;
if ( sum == n ) return true ;
}
return false ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPerfectSquare ( int n ) {
for ( int sum = 0 , i = 1 ;
sum < n ;
i += 2 ) {
sum += i ;
if ( sum == n ) return true ;
}
return false ;
}
</Code> |
cpp-java-FIND_THE_SMALLEST_WINDOW_IN_A_STRING_CONTAINING_ALL_CHARACTERS_OF_ANOTHER_STRING | cpp-java | string findSubString ( string str , string pat ) {
int len1 = str . length ( ) ;
int len2 = pat . length ( ) ;
if ( len1 < len2 ) {
cout << "No such window exists" ;
return "" ;
}
int hash_pat [ no_of_chars ] = {
0 }
;
int hash_str [ no_of_chars ] = {
0 }
;
for ( int i = ... | static String findSubString ( String str , String pat ) {
int len1 = str . length ( ) ;
int len2 = pat . length ( ) ;
if ( len1 < len2 ) {
System . out . println ( "No such window exists" ) ;
return "" ;
}
int hash_pat [ ] = new int [ no_of_chars ] ;
int hash_str [ ] = new int [ no_of_chars ] ;
fo... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string findSubString ( string str , string pat ) {
int len1 = str . length ( ) ;
int len2 = pat . length ( ) ;
if ( len1 < len2 ) {
cout << "No such window exists" ;
return "" ;
}
int hash_pat [ no_of_chars ] = {
0 }
;
int hash_str [ no_of_chars ] = {
0 }
;
for ( i... |
cpp-java-NUMBER_SUBSEQUENCES_STRING_DIVISIBLE_N | cpp-java | int countDivisibleSubseq ( string str , int n ) {
int len = str . length ( ) ;
int dp [ len ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ ( str [ 0 ] - '0' ) % n ] ++ ;
for ( int i = 1 ;
i < len ;
i ++ ) {
dp [ i ] [ ( str [ i ] - '0' ) % n ] ++ ;
for ( int j = 0 ;
j < n ;
j ++ ... | static int countDivisibleSubseq ( String str , int n ) {
int len = str . length ( ) ;
int dp [ ] [ ] = new int [ len ] [ n ] ;
dp [ 0 ] [ ( str . charAt ( 0 ) - ' 0 ' ) % n ] ++ ;
for ( int i = 1 ;
i < len ;
i ++ ) {
dp [ i ] [ ( str . charAt ( i ) - ' 0 ' ) % n ] ++ ;
for ( int j = 0 ;
j < n ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countDivisibleSubseq ( string str , int n ) {
int len = str . length ( ) ;
int dp [ len ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ ( str [ 0 ] - '0' ) % n ] ++ ;
for ( int i = 1 ;
i < len ;
i ++ ) {
dp [ i ] [ ( str [ i ] - '0' ) % n ] ++ ;
for ( int j = 0 ;
j < n ;
... |
cpp-java-MINIMUM_POSSIBLE_VALUE_AI_AJ_K_GIVEN_ARRAY_K | cpp-java | void pairs ( int arr [ ] , int n , int k ) {
int smallest = INT_MAX ;
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) {
smallest = abs ( arr [ i ] + arr [ j ] - k ) ;
count = 1 ;
}
else if (... | static void pairs ( int arr [ ] , int n , int k ) {
int smallest = Integer . MAX_VALUE ;
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( Math . abs ( arr [ i ] + arr [ j ] - k ) < smallest ) {
smallest = Math . abs ( arr [ i ] + arr [ j ] - k ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void pairs ( int arr [ ] , int n , int k ) {
int smallest = INT_MAX ;
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) {
smallest = abs ( arr [ i ] + arr [ j ] - k ) ;
count = 1 ;
}
el... |
cpp-java-ZECKENDORFS_THEOREM_NON_NEIGHBOURING_FIBONACCI_REPRESENTATION | cpp-java | int nearestSmallerEqFib ( int n ) {
if ( n == 0 || n == 1 ) return n ;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while ( f3 <= n ) {
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
return f2 ;
}
| public static int nearestSmallerEqFib ( int n ) {
if ( n == 0 || n == 1 ) return n ;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while ( f3 <= n ) {
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
return f2 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int nearestSmallerEqFib ( int n ) {
if ( n == 0 || n == 1 ) return n ;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while ( f3 <= n ) {
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
return f2 ;
}
</Code> |
cpp-java-SHUFFLE_A_DECK_OF_CARDS_3 | cpp-java | void shuffle ( int card [ ] , int n ) {
srand ( time ( 0 ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int r = i + ( rand ( ) % ( 52 - i ) ) ;
swap ( card [ i ] , card [ r ] ) ;
}
}
| public static void shuffle ( int card [ ] , int n ) {
Random rand = new Random ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int r = i + rand . nextInt ( 52 - i ) ;
int temp = card [ r ] ;
card [ r ] = card [ i ] ;
card [ i ] = temp ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void shuffle ( int card [ ] , int n ) {
srand ( time ( 0 ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int r = i + ( rand ( ) % ( 52 - i ) ) ;
swap ( card [ i ] , card [ r ] ) ;
}
}
</Code> |
cpp-java-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM_1 | cpp-java | int maxLen ( int arr [ ] , int n ) {
unordered_map < int , int > presum ;
int sum = 0 ;
int max_len = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ;
if ( sum == 0 ) max_len = i + 1 ;
if ( presum . find ( sum ) != presum . end ( )... | static int maxLen ( int arr [ ] ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
sum += arr [ i ] ;
if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ;
if ( sum == 0 ) max_len = i + 1 ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxLen ( int arr [ ] , int n ) {
unordered_map < int , int > presum ;
int sum = 0 ;
int max_len = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ;
if ( sum == 0 ) max_len = i + 1 ;
if ( presum . find ( sum ) != presum . ... |
cpp-java-LENGTH_LONGEST_BALANCED_SUBSEQUENCE | cpp-java | int maxLength ( char s [ ] , int n ) {
int dp [ n ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( s [ i ] == '(' && s [ i + 1 ] == ')' ) dp [ i ] [ i + 1 ] = 2 ;
for ( int l = 2 ;
l < n ;
l ++ ) {
for ( int i = 0 , j = l ;
j < n ;
i ++ , j ++ ) {
... | static int maxLength ( String s , int n ) {
int dp [ ] [ ] = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( s . charAt ( i ) == ' ( ' && s . charAt ( i + 1 ) == ' ) ' ) dp [ i ] [ i + 1 ] = 2 ;
for ( int l = 2 ;
l < n ;
l ++ ) {
for ( int i = 0 , j = l ;
j < n ;
i ++ , j ++ ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxLength ( char s [ ] , int n ) {
int dp [ n ] [ n ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( s [ i ] == '(' && s [ i + 1 ] == ')' ) dp [ i ] [ i + 1 ] = 2 ;
for ( int l = 2 ;
l < n ;
l ++ ) {
for ( int i = 0 , j = l ;
j < n ;
i ++ , j ++ ) ... |
cpp-java-PROGRAM_PRINT_SUM_GIVEN_NTH_TERM_1 | cpp-java | int summingSeries ( long n ) {
return pow ( n , 2 ) ;
}
| static int summingSeries ( long n ) {
return ( int ) Math . pow ( n , 2 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int summingSeries ( long n ) {
return pow ( n , 2 ) ;
}
</Code> |
cpp-java-SUBSET_SUM_PROBLEM_OSUM_SPACE | cpp-java | bool isSubsetSum ( int arr [ ] , int n , int sum ) {
bool subset [ 2 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= sum ;
j ++ ) {
if ( j == 0 ) subset [ i % 2 ] [ j ] = true ;
else if ( i == 0 ) subset [ i % 2 ] [ j ] = false ;
else if ( arr [ i - 1 ] <... | static boolean isSubsetSum ( int arr [ ] , int n , int sum ) {
boolean subset [ ] [ ] = new boolean [ 2 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= sum ;
j ++ ) {
if ( j == 0 ) subset [ i % 2 ] [ j ] = true ;
else if ( i == 0 ) subset [ i % 2 ] [ j ] = fals... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isSubsetSum ( int arr [ ] , int n , int sum ) {
bool subset [ 2 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= sum ;
j ++ ) {
if ( j == 0 ) subset [ i % 2 ] [ j ] = true ;
else if ( i == 0 ) subset [ i % 2 ] [ j ] = false ;
else if ( arr [ i ... |
cpp-java-FIND_INDEX_OF_AN_EXTRA_ELEMENT_PRESENT_IN_ONE_SORTED_ARRAY | cpp-java | int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ;
return n ;
}
| static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ;
return n ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ;
return n ;
}
</Code> |
cpp-java-NUMBER_OF_PERMUTATION_WITH_K_INVERSIONS | cpp-java | int numberOfPermWithKInversion ( int N , int K ) {
if ( N == 0 ) return 0 ;
if ( K == 0 ) return 1 ;
if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ;
int sum = 0 ;
for ( int i = 0 ;
i <= K ;
i ++ ) {
if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ;
}
memo [ N ] [ K ] =... | static int numberOfPermWithKInversion ( int N , int K ) {
if ( N == 0 ) return 0 ;
if ( K == 0 ) return 1 ;
if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ;
int sum = 0 ;
for ( int i = 0 ;
i <= K ;
i ++ ) {
if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ;
}
memo [ N ] ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int numberOfPermWithKInversion ( int N , int K ) {
if ( N == 0 ) return 0 ;
if ( K == 0 ) return 1 ;
if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ;
int sum = 0 ;
for ( int i = 0 ;
i <= K ;
i ++ ) {
if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ;
}
memo [ N ] ... |
cpp-java-DELETE_CONSECUTIVE_WORDS_SEQUENCE_1 | cpp-java | int removeConsecutiveSame ( vector < string > v ) {
stack < string > st ;
for ( int i = 0 ;
i < v . size ( ) ;
i ++ ) {
if ( st . empty ( ) ) st . push ( v [ i ] ) ;
else {
string str = st . top ( ) ;
if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ;
else st . push ( v [ i ] ) ;
... | static int removeConsecutiveSame ( Vector < String > v ) {
Stack < String > st = new Stack < > ( ) ;
for ( int i = 0 ;
i < v . size ( ) ;
i ++ ) {
if ( st . empty ( ) ) st . push ( v . get ( i ) ) ;
else {
String str = st . peek ( ) ;
if ( str . equals ( v . get ( i ) ) ) st . pop ( ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int removeConsecutiveSame ( vector < string > v ) {
stack < string > st ;
for ( int i = 0 ;
i < v . size ( ) ;
i ++ ) {
if ( st . empty ( ) ) st . push ( v [ i ] ) ;
else {
string str = st . top ( ) ;
if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ;
else st . push ( v [ i ] ... |
cpp-java-MINIMUM_INDEX_SUM_COMMON_ELEMENTS_TWO_LISTS | cpp-java | void find ( vector < string > list1 , vector < string > list2 ) {
vector < string > res ;
int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ;
for ( int sum = 0 ;
sum <= max_possible_sum ;
sum ++ ) {
for ( int i = 0 ;
i <= sum ;
i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < list2 .... | static void find ( Vector < String > list1 , Vector < String > list2 ) {
Vector < String > res = new Vector < > ( ) ;
int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ;
for ( int sum = 0 ;
sum <= max_possible_sum ;
sum ++ ) {
for ( int i = 0 ;
i <= sum ;
i ++ ) if ( i < list1 . size ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void find ( vector < string > list1 , vector < string > list2 ) {
vector < string > res ;
int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ;
for ( int sum = 0 ;
sum <= max_possible_sum ;
sum ++ ) {
for ( int i = 0 ;
i <= sum ;
i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < ... |
cpp-java-NON_REPEATING_ELEMENT | cpp-java | int firstNonRepeating ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < n ;
j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ;
if ( j == n ) return arr [ i ] ;
}
return - 1 ;
}
| static int firstNonRepeating ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < n ;
j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ;
if ( j == n ) return arr [ i ] ;
}
return - 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int firstNonRepeating ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < n ;
j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ;
if ( j == n ) return arr [ i ] ;
}
return - 1 ;
}
</Code> |
cpp-java-HOW_TO_BEGIN_WITH_COMPETITIVE_PROGRAMMING | cpp-java | int search ( int arr [ ] , int n , int x ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| static int search ( int arr [ ] , int n , int x ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int search ( int arr [ ] , int n , int x ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
</Code> |
cpp-java-SUM_MIDDLE_ROW_COLUMN_MATRIX | cpp-java | void middlesum ( int mat [ ] [ MAX ] , int n ) {
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) row_sum += mat [ n / 2 ] [ i ] ;
cout << "Sum of middle row = " << row_sum << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) col_sum += mat [ i ] [ n / 2 ] ;
cout << "Sum of middle column = " << ... | static void middlesum ( int mat [ ] [ ] , int n ) {
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) row_sum += mat [ n / 2 ] [ i ] ;
System . out . println ( "Sum of middle row = " + row_sum ) ;
for ( int i = 0 ;
i < n ;
i ++ ) col_sum += mat [ i ] [ n / 2 ] ;
System . out . println (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void middlesum ( int mat [ ] [ MAX ] , int n ) {
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) row_sum += mat [ n / 2 ] [ i ] ;
cout << "Sum of middle row = " << row_sum << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) col_sum += mat [ i ] [ n / 2 ] ;
cout << "Sum of middle column ... |
cpp-java-MULTIPLY_LARGE_INTEGERS_UNDER_LARGE_MODULO | cpp-java | long long moduloMultiplication ( long long a , long long b , long long mod ) {
long long res = 0 ;
a %= mod ;
while ( b ) {
if ( b & 1 ) res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
}
return res ;
}
| static long moduloMultiplication ( long a , long b , long mod ) {
long res = 0 ;
a %= mod ;
while ( b > 0 ) {
if ( ( b & 1 ) > 0 ) {
res = ( res + a ) % mod ;
}
a = ( 2 * a ) % mod ;
b >>= 1 ;
}
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long moduloMultiplication ( long long a , long long b , long long mod ) {
long long res = 0 ;
a %= mod ;
while ( b ) {
if ( b & 1 ) res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
}
return res ;
}
</Code> |
cpp-java-GENERATE_PYTHAGOREAN_TRIPLETS | cpp-java | void pythagoreanTriplets ( int limit ) {
int a , b , c = 0 ;
int m = 2 ;
while ( c < limit ) {
for ( int n = 1 ;
n < m ;
++ n ) {
a = m * m - n * n ;
b = 2 * m * n ;
c = m * m + n * n ;
if ( c > limit ) break ;
printf ( "%d %d %d\n" , a , b , c ) ;
}
m ++ ;
}
}
| static void pythagoreanTriplets ( int limit ) {
int a , b , c = 0 ;
int m = 2 ;
while ( c < limit ) {
for ( int n = 1 ;
n < m ;
++ n ) {
a = m * m - n * n ;
b = 2 * m * n ;
c = m * m + n * n ;
if ( c > limit ) break ;
System . out . println ( a + " " + b + " " + c ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void pythagoreanTriplets ( int limit ) {
int a , b , c = 0 ;
int m = 2 ;
while ( c < limit ) {
for ( int n = 1 ;
n < m ;
++ n ) {
a = m * m - n * n ;
b = 2 * m * n ;
c = m * m + n * n ;
if ( c > limit ) break ;
printf ( "%d %d %d\n" , a , b , c ) ;
}
m ++ ;... |
cpp-java-SUM_PAIRWISE_PRODUCTS_2 | cpp-java | long long int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
| static int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
</Code> |
cpp-java-PROGRAM_FOR_NTH_FIBONACCI_NUMBER_2 | cpp-java | int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] ) return f [ n ] ;
int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ;
return f... | public static int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] != 0 ) return f [ n ] ;
int k = ( n & 1 ) == 1 ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) == 1 ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] ) return f [ n ] ;
int k = ( n & 1 ) ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ;
r... |
cpp-java-GIVEN_P_AND_N_FIND_THE_LARGEST_X_SUCH_THAT_PX_DIVIDES_N_2 | cpp-java | int largestPower ( int n , int p ) {
int x = 0 ;
while ( n ) {
n /= p ;
x += n ;
}
return x ;
}
| static int Largestpower ( int n , int p ) {
int ans = 0 ;
while ( n > 0 ) {
n /= p ;
ans += n ;
}
return ans ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int largestPower ( int n , int p ) {
int x = 0 ;
while ( n ) {
n /= p ;
x += n ;
}
return x ;
}
</Code> |
cpp-java-PROGRAM_PRINT_IDENTITY_MATRIX_1 | cpp-java | bool isIdentity ( int mat [ ] [ MAX ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ;
}
| static boolean isIdentity ( int mat [ ] [ ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ;... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isIdentity ( int mat [ ] [ MAX ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ... |
cpp-java-MINIMUM_CELLS_REQUIRED_REACH_DESTINATION_JUMPS_EQUAL_CELL_VALUES | cpp-java | int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) {
int dp [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = INT_MAX ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( dp [ i ] [ ... | static int minCells ( int mat [ ] [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minCells ( int mat [ SIZE ] [ SIZE ] , int m , int n ) {
int dp [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = INT_MAX ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( dp [... |
cpp-java-REARRANGE_BINARY_STRING_ALTERNATE_X_Y_OCCURRENCES | cpp-java | void arrangeString ( string str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str [ i ] == '0' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j < x && count_0 > 0... | static void arrangeString ( String str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str . charAt ( i ) == ' 0 ' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void arrangeString ( string str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str [ i ] == '0' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j < x && coun... |
cpp-java-MINIMUM_NUMBER_DELETIONS_MAKE_STRING_PALINDROME_SET_2 | cpp-java | int getLevenstein ( string const & input ) {
string revInput ( input . rbegin ( ) , input . rend ( ) ) ;
int n = input . size ( ) ;
vector < vector < int >> dp ( n + 1 , vector < int > ( n + 1 , - 1 ) ) ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
}
for ( int i... | static int getLevenstein ( StringBuilder input ) {
StringBuilder revInput = new StringBuilder ( input ) ;
revInput = revInput . reverse ( ) ;
int n = input . length ( ) ;
int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getLevenstein ( string const & input ) {
string revInput ( input . rbegin ( ) , input . rend ( ) ) ;
int n = input . size ( ) ;
vector < vector < int >> dp ( n + 1 , vector < int > ( n + 1 , - 1 ) ) ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
}
for ... |
cpp-java-DYNAMIC_PROGRAMMING_SET_34_ASSEMBLY_LINE_SCHEDULING | cpp-java | int carAssembly ( int a [ ] [ NUM_STATION ] , int t [ ] [ NUM_STATION ] , int * e , int * x ) {
int T1 [ NUM_STATION ] , T2 [ NUM_STATION ] , i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min ( T1 [ i - 1 ] + a [ 0 ] [ i... | static int carAssembly ( int a [ ] [ ] , int t [ ] [ ] , int e [ ] , int x [ ] ) {
int T1 [ ] = new int [ NUM_STATION ] ;
int T2 [ ] = new int [ NUM_STATION ] ;
int i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int carAssembly ( int a [ ] [ NUM_STATION ] , int t [ ] [ NUM_STATION ] , int * e , int * x ) {
int T1 [ NUM_STATION ] , T2 [ NUM_STATION ] , i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min ( T1 [ i - 1 ] + a [ ... |
cpp-java-BASIC_AND_EXTENDED_EUCLIDEAN_ALGORITHMS | cpp-java | int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
| public static int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
return gcd ( b % a , a ) ;
}
</Code> |
cpp-java-FIND_TRIPLETS_ARRAY_WHOSE_SUM_EQUAL_ZERO_2 | cpp-java | void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
printf ( "%d %d %d\n" , x , arr [ l ] , a... | static void findTriplets ( int arr [ ] , int n ) {
boolean found = false ;
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
System . out . print ( x + " " )... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void findTriplets ( int arr [ ] , int n ) {
bool found = false ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int l = i + 1 ;
int r = n - 1 ;
int x = arr [ i ] ;
while ( l < r ) {
if ( x + arr [ l ] + arr [ r ] == 0 ) {
printf ( "%d %d %d\n" , x , arr [ ... |
cpp-java-FIND_THE_NUMBER_OCCURRING_ODD_NUMBER_OF_TIMES_2 | cpp-java | int getOddOccurrence ( int ar [ ] , int ar_size ) {
int res = 0 ;
for ( int i = 0 ;
i < ar_size ;
i ++ ) res = res ^ ar [ i ] ;
return res ;
}
| int getOddOccurrence ( int ar [ ] , int ar_size ) {
int i ;
int res = 0 ;
for ( i = 0 ;
i < ar_size ;
i ++ ) {
res = res ^ ar [ i ] ;
}
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getOddOccurrence ( int ar [ ] , int ar_size ) {
int res = 0 ;
for ( int i = 0 ;
i < ar_size ;
i ++ ) res = res ^ ar [ i ] ;
return res ;
}
</Code> |
cpp-java-DYNAMIC_PROGRAMMING_HIGH_EFFORT_VS_LOW_EFFORT_TASKS_PROBLEM | cpp-java | int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
| static int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return Math . max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxTasks ( int high [ ] , int low [ ] , int n ) {
if ( n <= 0 ) return 0 ;
return max ( high [ n - 1 ] + maxTasks ( high , low , ( n - 2 ) ) , low [ n - 1 ] + maxTasks ( high , low , ( n - 1 ) ) ) ;
}
</Code> |
cpp-java-FIND_TWO_SIDES_RIGHT_ANGLE_TRIANGLE | cpp-java | void printOtherSides ( int n ) {
if ( n & 1 ) {
if ( n == 1 ) cout << - 1 << endl ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
cout << "b = " << b << ", c = " << c << endl ;
}
}
else {
if ( n == 2 ) cout << - 1 << endl ;
else {
int b = n * n / 4 - 1 ... | static void printOtherSides ( int n ) {
if ( n % 2 != 0 ) {
if ( n == 1 ) System . out . println ( "-1" ) ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
System . out . println ( "b = " + b + ", c = " + c ) ;
}
}
else {
if ( n == 2 ) System . out . println ( "-1"... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printOtherSides ( int n ) {
if ( n & 1 ) {
if ( n == 1 ) cout << - 1 << endl ;
else {
int b = ( n * n - 1 ) / 2 ;
int c = ( n * n + 1 ) / 2 ;
cout << "b = " << b << ", c = " << c << endl ;
}
}
else {
if ( n == 2 ) cout << - 1 << endl ;
else {
int b = n * n /... |
cpp-java-MINIMUM_LENGTH_UNSORTED_SUBARRAY_SORTING_WHICH_MAKES_THE_COMPLETE_ARRAY_SORTED | cpp-java | void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
cout << "The complete array is sorted" ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e -- ) {
if ( arr [ e ]... | static void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
System . out . println ( "The complete array is sorted" ) ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printUnsorted ( int arr [ ] , int n ) {
int s = 0 , e = n - 1 , i , max , min ;
for ( s = 0 ;
s < n - 1 ;
s ++ ) {
if ( arr [ s ] > arr [ s + 1 ] ) break ;
}
if ( s == n - 1 ) {
cout << "The complete array is sorted" ;
return ;
}
for ( e = n - 1 ;
e > 0 ;
e -- ) {
if ( ar... |
cpp-java-CHECK_TWO_GIVEN_CIRCLES_TOUCH_INTERSECT | cpp-java | int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
| static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) {
int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ;
int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ;
if ( distSq == radSumSq ) return 1 ;
else if ( distSq > radSumSq ) return - 1 ;
else return 0 ;
}
</Code> |
cpp-java-COUNT_DISTINCT_ELEMENTS_IN_EVERY_WINDOW_OF_SIZE_K | cpp-java | void countDistinct ( int arr [ ] , int k , int n ) {
map < int , int > hm ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hm [ arr [ i ] ] == 0 ) {
dist_count ++ ;
}
hm [ arr [ i ] ] += 1 ;
}
cout << dist_count << endl ;
for ( int i = k ;
i < n ;
i ++ ) {
if ( hm... | static void countDistinct ( int arr [ ] , int k ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hM . get ( arr [ i ] ) == null ) {
hM . put ( arr [ i ] , 1 ) ;
dist_count ++ ;
}
else {
int... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void countDistinct ( int arr [ ] , int k , int n ) {
map < int , int > hm ;
int dist_count = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) {
if ( hm [ arr [ i ] ] == 0 ) {
dist_count ++ ;
}
hm [ arr [ i ] ] += 1 ;
}
cout << dist_count << endl ;
for ( int i = k ;
i < n ;
i ++ ) {
... |
cpp-java-PROGRAM_TO_FIND_THE_AREA_OF_PENTAGON | cpp-java | float findArea ( float a ) {
float area ;
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
| static float findArea ( float a ) {
float area ;
area = ( float ) ( Math . sqrt ( 5 * ( 5 + 2 * ( Math . sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
float findArea ( float a ) {
float area ;
area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 ;
return area ;
}
</Code> |
cpp-java-COUNT_ROTATIONS_DIVISIBLE_8 | cpp-java | int countRotationsDivBy8 ( string n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n [ 0 ] - '0' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ;
int second = ( n [ 1 ] - '0' ) * 1... | static int countRotationsDivBy8 ( String n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n . charAt ( 0 ) - ' 0 ' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n . charAt ( 0 ) - ' 0 ' ) * 10 + ( n . charAt ( 1 ) - ' 0 ' ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countRotationsDivBy8 ( string n ) {
int len = n . length ( ) ;
int count = 0 ;
if ( len == 1 ) {
int oneDigit = n [ 0 ] - '0' ;
if ( oneDigit % 8 == 0 ) return 1 ;
return 0 ;
}
if ( len == 2 ) {
int first = ( n [ 0 ] - '0' ) * 10 + ( n [ 1 ] - '0' ) ;
int second = ( n [ 1 ] - '0... |
cpp-java-LONGEST_COMMON_SUBSEQUENCE_WITH_AT_MOST_K_CHANGES_ALLOWED | cpp-java | int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) {
if ( k < 0 ) return - 1e7 ;
if ( n < 0 || m < 0 ) return 0 ;
int & ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , ... | static int lcs ( int [ ] [ ] [ ] dp , int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) {
if ( k < 0 ) return - 10000000 ;
if ( n < 0 || m < 0 ) return 0 ;
int ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
try {
ans = Math . max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int lcs ( int dp [ MAX ] [ MAX ] [ MAX ] , int arr1 [ ] , int n , int arr2 [ ] , int m , int k ) {
if ( k < 0 ) return - 1e7 ;
if ( n < 0 || m < 0 ) return 0 ;
int & ans = dp [ n ] [ m ] [ k ] ;
if ( ans != - 1 ) return ans ;
ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , ... |
cpp-java-DYNAMIC_PROGRAMMING_SET_5_EDIT_DISTANCE_1 | cpp-java | int editDistDP ( string str1 , string str2 , int m , int n ) {
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( str1 [ i - 1 ] == str2 [ j - 1 ] ... | static int editDistDP ( String str1 , String str2 , 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 ( str1 . c... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int editDistDP ( string str1 , string str2 , int m , int n ) {
int dp [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) {
for ( int j = 0 ;
j <= n ;
j ++ ) {
if ( i == 0 ) dp [ i ] [ j ] = j ;
else if ( j == 0 ) dp [ i ] [ j ] = i ;
else if ( str1 [ i - 1 ] == str2 [ j... |
cpp-java-FIND_NUMBER_TRANSFORMATION_MAKE_TWO_MATRIX_EQUAL | cpp-java | int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [... | static int countOps ( int A [ ] [ ] , int B [ ] [ ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A [ 0 ] [ ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countOps ( int A [ ] [ MAX ] , int B [ ] [ MAX ] , int m , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) A [ i ] [ j ] -= B [ i ] [ j ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 1 ;
j < m ;
j ++ ) if ( A [ i ] [ j ] - A [ i ] [ 0 ] - A [ 0 ] [ j ] + A ... |
cpp-java-LONGEST_ALTERNATING_POSITIVE_NEGATIVE_SUBARRAY_STARTING_EVERY_INDEX | cpp-java | void longestAlternating ( int arr [ ] , int n ) {
int count [ n ] ;
count [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ;
else count [ i ] = 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << count [ i ] << " " ;
}
| public static void longestAlternating ( int arr [ ] , int n ) {
int [ ] count = new int [ n ] ;
count [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ;
else count [ i ] = 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) Syste... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void longestAlternating ( int arr [ ] , int n ) {
int count [ n ] ;
count [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
if ( arr [ i ] * arr [ i + 1 ] < 0 ) count [ i ] = count [ i + 1 ] + 1 ;
else count [ i ] = 1 ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << count [ i ] << " "... |
cpp-java-FIND_ALL_DIVISORS_OF_A_NATURAL_NUMBER_SET_2 | cpp-java | void printDivisors ( int n ) {
vector < int > v ;
for ( int i = 1 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) printf ( "%d " , i ) ;
else {
printf ( "%d " , i ) ;
v . push_back ( n / i ) ;
}
}
}
for ( int i = v . size ( ) - 1 ;
i >= 0 ;
i ... | static void printDivisors ( int n ) {
Vector < Integer > v = new Vector < > ( ) ;
for ( int i = 1 ;
i <= Math . sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) System . out . printf ( "%d " , i ) ;
else {
System . out . printf ( "%d " , i ) ;
v . add ( n / i ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printDivisors ( int n ) {
vector < int > v ;
for ( int i = 1 ;
i <= sqrt ( n ) ;
i ++ ) {
if ( n % i == 0 ) {
if ( n / i == i ) printf ( "%d " , i ) ;
else {
printf ( "%d " , i ) ;
v . push_back ( n / i ) ;
}
}
}
for ( int i = v . size ( ) - 1 ;
i >= 0... |
cpp-java-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER | cpp-java | bool isPower ( int x , long int y ) {
if ( x == 1 ) return ( y == 1 ) ;
long int pow = 1 ;
while ( pow < y ) pow *= x ;
return ( pow == y ) ;
}
| public static boolean isPower ( int x , int y ) {
if ( x == 1 ) return ( y == 1 ) ;
int pow = 1 ;
while ( pow < y ) pow = pow * x ;
return ( pow == y ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPower ( int x , long int y ) {
if ( x == 1 ) return ( y == 1 ) ;
long int pow = 1 ;
while ( pow < y ) pow *= x ;
return ( pow == y ) ;
}
</Code> |
cpp-java-MAXIMUM_SUM_SUBSEQUENCE_LEAST_K_DISTANT_ELEMENTS | cpp-java | int maxSum ( int arr [ ] , int N , int k ) {
int MS [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ;
}
return MS [ 0 ] ;
}
| static int maxSum ( int arr [ ] , int N , int k ) {
int MS [ ] = new int [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = Math . max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = Math . max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSum ( int arr [ ] , int N , int k ) {
int MS [ N ] ;
MS [ N - 1 ] = arr [ N - 1 ] ;
for ( int i = N - 2 ;
i >= 0 ;
i -- ) {
if ( i + k + 1 >= N ) MS [ i ] = max ( arr [ i ] , MS [ i + 1 ] ) ;
else MS [ i ] = max ( arr [ i ] + MS [ i + k + 1 ] , MS [ i + 1 ] ) ;
}
return MS [ 0 ] ;
}
... |
cpp-java-GROUP_OCCURRENCES_CHARACTERS_ACCORDING_FIRST_APPEARANCE | cpp-java | void printGrouped ( string str ) {
int n = str . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str [ i ] - 'a' ] -- ) cout << str [ i ] ;
count [ str [ i ] -... | static void printGrouped ( String str ) {
int n = str . length ( ) ;
int count [ ] = new int [ MAX_CHAR ] ;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str . charAt ( i ) - ' a ' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str . charAt ( i ) - ' a ' ] != 0 ) {
System . out . print (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printGrouped ( string str ) {
int n = str . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ str [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < n ;
i ++ ) {
while ( count [ str [ i ] - 'a' ] -- ) cout << str [ i ] ;
count [ str ... |
cpp-java-COUNT_SORTED_ROWS_MATRIX | cpp-java | int sortedCount ( int mat [ ] [ MAX ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
... | static int sortedCount ( int mat [ ] [ ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
int j... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int sortedCount ( int mat [ ] [ MAX ] , int r , int c ) {
int result = 0 ;
for ( int i = 0 ;
i < r ;
i ++ ) {
int j ;
for ( j = 0 ;
j < c - 1 ;
j ++ ) if ( mat [ i ] [ j + 1 ] <= mat [ i ] [ j ] ) break ;
if ( j == c - 1 ) result ++ ;
}
for ( int i = 0 ;
i < r ;
i ++ ) {
i... |
cpp-java-COUNT_DISTINCT_SUBSEQUENCES | cpp-java | int countSub ( string str ) {
vector < int > last ( MAX_CHAR , - 1 ) ;
int n = str . length ( ) ;
int dp [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ str [ i - 1 ] ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ str [ i - 1 ] ] ] ;
last ... | static int countSub ( String str ) {
int [ ] last = new int [ MAX_CHAR ] ;
Arrays . fill ( last , - 1 ) ;
int n = str . length ( ) ;
int [ ] dp = new int [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ ( int ) str . charAt ( i - 1 ) ] != -... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSub ( string str ) {
vector < int > last ( MAX_CHAR , - 1 ) ;
int n = str . length ( ) ;
int dp [ n + 1 ] ;
dp [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] = 2 * dp [ i - 1 ] ;
if ( last [ str [ i - 1 ] ] != - 1 ) dp [ i ] = dp [ i ] - dp [ last [ str [ i - 1 ] ] ] ;
... |
cpp-java-LARGEST_INCREASING_SUBSEQUENCE_OF_CONSECUTIVE_INTEGERS | cpp-java | int findLIS ( int A [ ] , int n ) {
unordered_map < int , int > hash ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash [ A [ 0 ] ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ;
if ( LIS_size < hash [ A [ i ] ] ) {
LIS_size = hash [ A [ i ] ] ;
LIS_i... | static void findLIS ( int A [ ] , int n ) {
Map < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash . put ( A [ 0 ] , 1 ) ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash . put ( A [ i ] , hash . get ( A [ i ] - 1 ) == null ? 1 : hash . get ( A [ i... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findLIS ( int A [ ] , int n ) {
unordered_map < int , int > hash ;
int LIS_size = 1 ;
int LIS_index = 0 ;
hash [ A [ 0 ] ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] = hash [ A [ i ] - 1 ] + 1 ;
if ( LIS_size < hash [ A [ i ] ] ) {
LIS_size = hash [ A [ i ] ] ;
... |
cpp-java-FIND_THE_MAXIMUM_OF_MINIMUMS_FOR_EVERY_WINDOW_SIZE_IN_A_GIVEN_ARRAY | cpp-java | void printMaxOfMin ( int arr [ ] , int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = INT_MIN ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}
... | static void printMaxOfMin ( int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = Integer . MIN_VALUE ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printMaxOfMin ( int arr [ ] , int n ) {
for ( int k = 1 ;
k <= n ;
k ++ ) {
int maxOfMin = INT_MIN ;
for ( int i = 0 ;
i <= n - k ;
i ++ ) {
int min = arr [ i ] ;
for ( int j = 1 ;
j < k ;
j ++ ) {
if ( arr [ i + j ] < min ) min = arr [ i + j ] ;
}... |
cpp-java-MAXIMUM_NUMBER_OF_TRAILING_ZEROS_IN_THE_PRODUCT_OF_THE_SUBSETS_OF_SIZE_K | cpp-java | int maximumZeros ( int * arr , int n , int k ) {
int subset [ k + 1 ] [ MAX5 + 5 ] ;
memset ( subset , - 1 , sizeof ( subset ) ) ;
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
pw2 ++ ;
arr [ p ] /= 2 ;
}
while ... | static int maximumZeros ( int arr [ ] , int n , int k ) {
int subset [ ] [ ] = new int [ k + 1 ] [ MAX5 + 5 ] ;
for ( int [ ] row : subset ) {
Arrays . fill ( row , - 1 ) ;
}
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maximumZeros ( int * arr , int n , int k ) {
int subset [ k + 1 ] [ MAX5 + 5 ] ;
memset ( subset , - 1 , sizeof ( subset ) ) ;
subset [ 0 ] [ 0 ] = 0 ;
for ( int p = 0 ;
p < n ;
p ++ ) {
int pw2 = 0 , pw5 = 0 ;
while ( arr [ p ] % 2 == 0 ) {
pw2 ++ ;
arr [ p ] /= 2 ;
}
... |
cpp-java-LARGEST_DIVISIBLE_PAIRS_SUBSET | cpp-java | int largestSubset ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int dp [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( a [ j ] % a [ i ] == 0 ) mxm = max ( mxm , dp [ j ] ) ;
dp [ i ] = 1 + mxm ;
}
return * max... | static int largestSubset ( int [ ] a , int n ) {
Arrays . sort ( a ) ;
int [ ] dp = new int [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( a [ j ] % a [ i ] == 0 ) {
mxm = Math . max ( mxm , dp [ j ] ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int largestSubset ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int dp [ n ] ;
dp [ n - 1 ] = 1 ;
for ( int i = n - 2 ;
i >= 0 ;
i -- ) {
int mxm = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( a [ j ] % a [ i ] == 0 ) mxm = max ( mxm , dp [ j ] ) ;
dp [ i ] = 1 + mxm ;
}
retur... |
cpp-java-FIND_KTH_CHARACTER_OF_DECRYPTED_STRING | cpp-java | char encodedChar ( string str , int k ) {
string expand = "" ;
string temp ;
int freq = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
) {
temp = "" ;
freq = 0 ;
while ( str [ i ] >= 'a' && str [ i ] <= 'z' ) {
temp . push_back ( str [ i ] ) ;
i ++ ;
}
while ( str [ i ] >= '1' && ... | static char encodedChar ( String str , int k ) {
String expand = "" ;
String temp = "" ;
int freq = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
) {
temp = "" ;
freq = 0 ;
while ( i < str . length ( ) && str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) {
temp += str . charAt ( ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
char encodedChar ( string str , int k ) {
string expand = "" ;
string temp ;
int freq = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
) {
temp = "" ;
freq = 0 ;
while ( str [ i ] >= 'a' && str [ i ] <= 'z' ) {
temp . push_back ( str [ i ] ) ;
i ++ ;
}
while ( str [ i ] >= ... |
cpp-java-ELEMENTS_TO_BE_ADDED_SO_THAT_ALL_ELEMENTS_OF_A_RANGE_ARE_PRESENT_IN_ARRAY_1 | cpp-java | int countNum ( int arr [ ] , int n ) {
unordered_set < int > s ;
int count = 0 , maxm = INT_MIN , minm = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm = arr [ i ] ;
}
for ( int i = minm ;
i <= m... | static int countNum ( int arr [ ] , int n ) {
HashSet < Integer > s = new HashSet < > ( ) ;
int count = 0 , maxm = Integer . MIN_VALUE , minm = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . add ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm =... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countNum ( int arr [ ] , int n ) {
unordered_set < int > s ;
int count = 0 , maxm = INT_MIN , minm = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
if ( arr [ i ] < minm ) minm = arr [ i ] ;
if ( arr [ i ] > maxm ) maxm = arr [ i ] ;
}
for ( int i = minm ;
... |
cpp-java-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY_1 | cpp-java | int countPairs ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( auto it = mp . begin ( ) ;
it != mp . end ( ) ;
it ++ ) {
int count = it -> second ;
ans += ( count * ( count - 1 ) ) / 2 ;
}
return ans ;
}
| public static int countPairs ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hm = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ;
else hm . put ( arr [ i ] , 1 ) ;
}
int ans = 0 ;
for ( Map... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countPairs ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
for ( int i = 0 ;
i < n ;
i ++ ) mp [ arr [ i ] ] ++ ;
int ans = 0 ;
for ( auto it = mp . begin ( ) ;
it != mp . end ( ) ;
it ++ ) {
int count = it -> second ;
ans += ( count * ( count - 1 ) ) / 2 ;
}
return ans... |
cpp-java-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY_1 | cpp-java | int maxDiff ( int arr [ ] , int n ) {
int result = 0 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ;
return result ;
}
| static int maxDiff ( int [ ] arr , int n ) {
int result = 0 ;
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += Math . abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += Math . abs ( arr [ n - 1 ] ) ;
return ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxDiff ( int arr [ ] , int n ) {
int result = 0 ;
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] != arr [ i + 1 ] ) result += abs ( arr [ i ] ) ;
else i ++ ;
}
if ( arr [ n - 2 ] != arr [ n - 1 ] ) result += abs ( arr [ n - 1 ] ) ;
return result ;
}
<... |
cpp-java-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS | cpp-java | int middleOfThree ( int a , int b , int c ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
| public static int middleOfThree ( int a , int b , int c ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int middleOfThree ( int a , int b , int c ) {
if ( ( a < b && b < c ) || ( c < b && b < a ) ) return b ;
else if ( ( b < a && a < c ) || ( c < a && a < b ) ) return a ;
else return c ;
}
</Code> |
cpp-java-COUNT_ARRAYS_CONSECUTIVE_ELEMENT_DIFFERENT_VALUES | cpp-java | int countarray ( int n , int k , int x ) {
int dp [ MAXN ] = {
0 }
;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
| public static int countarray ( int n , int k , int x ) {
int [ ] dp = new int [ 109 ] ;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countarray ( int n , int k , int x ) {
int dp [ MAXN ] = {
0 }
;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
for ( int i = 2 ;
i < n ;
i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ;
return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ;
}
</Code> |
cpp-java-NEWMAN_SHANKS_WILLIAMS_PRIME | cpp-java | int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
| static int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int nswp ( int n ) {
if ( n == 0 || n == 1 ) return 1 ;
return 2 * nswp ( n - 1 ) + nswp ( n - 2 ) ;
}
</Code> |
cpp-java-DYNAMIC_PROGRAMMING_SET_1 | cpp-java | int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
| int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int fib ( int n ) {
if ( lookup [ n ] == NIL ) {
if ( n <= 1 ) lookup [ n ] = n ;
else lookup [ n ] = fib ( n - 1 ) + fib ( n - 2 ) ;
}
return lookup [ n ] ;
}
</Code> |
cpp-java-COUNT_NUMBERS_CAN_CONSTRUCTED_USING_TWO_NUMBERS | cpp-java | int countNums ( int n , int x , int y ) {
vector < bool > arr ( n + 1 , false ) ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
if ( i + y <= n )... | static int countNums ( int n , int x , int y ) {
boolean [ ] arr = new boolean [ n + 1 ] ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = Math . min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
i... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countNums ( int n , int x , int y ) {
vector < bool > arr ( n + 1 , false ) ;
if ( x <= n ) arr [ x ] = true ;
if ( y <= n ) arr [ y ] = true ;
int result = 0 ;
for ( int i = min ( x , y ) ;
i <= n ;
i ++ ) {
if ( arr [ i ] ) {
if ( i + x <= n ) arr [ i + x ] = true ;
if ( i + y... |
cpp-java-COUNT_PALINDROMIC_SUBSEQUENCE_GIVEN_STRING | cpp-java | int countPS ( string str ) {
int N = str . length ( ) ;
int cps [ N + 1 ] [ N + 1 ] ;
memset ( cps , 0 , sizeof ( cps ) ) ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
if ( str... | static int countPS ( String str ) {
int N = str . length ( ) ;
int [ ] [ ] cps = new int [ N + 1 ] [ N + 1 ] ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
if ( k < N ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countPS ( string str ) {
int N = str . length ( ) ;
int cps [ N + 1 ] [ N + 1 ] ;
memset ( cps , 0 , sizeof ( cps ) ) ;
for ( int i = 0 ;
i < N ;
i ++ ) cps [ i ] [ i ] = 1 ;
for ( int L = 2 ;
L <= N ;
L ++ ) {
for ( int i = 0 ;
i < N ;
i ++ ) {
int k = L + i - 1 ;
i... |
cpp-java-CONVERT_DECIMAL_FRACTION_BINARY_NUMBER | cpp-java | string decimalToBinary ( double num , int k_prec ) {
string binary = "" ;
int Integral = num ;
double fractional = num - Integral ;
while ( Integral ) {
int rem = Integral % 2 ;
binary . push_back ( rem + '0' ) ;
Integral /= 2 ;
}
reverse ( binary . begin ( ) , binary . end ( ) ) ;
binary . pu... | static String decimalToBinary ( double num , int k_prec ) {
String binary = "" ;
int Integral = ( int ) num ;
double fractional = num - Integral ;
while ( Integral > 0 ) {
int rem = Integral % 2 ;
binary += ( ( char ) ( rem + ' 0 ' ) ) ;
Integral /= 2 ;
}
binary = reverse ( binary ) ;
binary +... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string decimalToBinary ( double num , int k_prec ) {
string binary = "" ;
int Integral = num ;
double fractional = num - Integral ;
while ( Integral ) {
int rem = Integral % 2 ;
binary . push_back ( rem + '0' ) ;
Integral /= 2 ;
}
reverse ( binary . begin ( ) , binary . end ( ) ) ;
bina... |
cpp-java-DYNAMIC_PROGRAMMING_SET_37_BOOLEAN_PARENTHESIZATION_PROBLEM | cpp-java | int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ n ] [ n ] , T [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == 'F' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == 'T' ) ? 1 : 0 ;
}
for ( int gap = 1 ;
gap < n ;
++ gap ) {
for ( int i = 0 , j =... | static int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ ] [ ] = new int [ n ] [ n ] ;
int T [ ] [ ] = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == ' F ' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == ' T ' ) ? 1 : 0 ;
}
for ( int gap = 1... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countParenth ( char symb [ ] , char oper [ ] , int n ) {
int F [ n ] [ n ] , T [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
F [ i ] [ i ] = ( symb [ i ] == 'F' ) ? 1 : 0 ;
T [ i ] [ i ] = ( symb [ i ] == 'T' ) ? 1 : 0 ;
}
for ( int gap = 1 ;
gap < n ;
++ gap ) {
for ( int i = ... |
cpp-java-COUNT_SET_BITS_IN_AN_INTEGER_1 | cpp-java | int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
| public static int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return ( n & 1 ) + countSetBits ( n >> 1 ) ;
}
</Code> |
cpp-java-LONGEST_SPAN_SUM_TWO_BINARY_ARRAYS | cpp-java | int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;
... | static int longestCommonSum ( int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;
if ( len > maxLen ) max... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int longestCommonSum ( bool arr1 [ ] , bool arr2 [ ] , int n ) {
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum1 = 0 , sum2 = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum1 += arr1 [ j ] ;
sum2 += arr2 [ j ] ;
if ( sum1 == sum2 ) {
int len = j - i + 1 ;... |
cpp-java-MAXIMUM_TRIPLET_SUM_ARRAY | cpp-java | int maxTripletSum ( int arr [ ] , int n ) {
int sum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
| static int maxTripletSum ( int arr [ ] , int n ) {
int sum = - 1000000 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxTripletSum ( int arr [ ] , int n ) {
int sum = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) if ( sum < arr [ i ] + arr [ j ] + arr [ k ] ) sum = arr [ i ] + arr [ j ] + arr [ k ] ;
return sum ;
}
</Code> |
cpp-java-EVALUATE_A_BOOLEAN_EXPRESSION_REPRESENTED_AS_STRING | cpp-java | int evaluateBoolExpr ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( s [ i + 1 ] == 'A' ) {
if ( s [ i + 2 ] == '0' || s [ i ] == '0' ) s [ i + 2 ] = '0' ;
else s [ i + 2 ] = '1' ;
}
else if ( s [ i + 1 ] == 'B' ) {
if ( s [ i + 2 ] == '1' || s [ i... | static int evaluateBoolExpr ( StringBuffer s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( i + 1 < n && i + 2 < n ) {
if ( s . charAt ( i + 1 ) == ' A ' ) {
if ( s . charAt ( i + 2 ) == ' 0 ' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , ' 0 ' ) ;
else s .... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int evaluateBoolExpr ( string s ) {
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( s [ i + 1 ] == 'A' ) {
if ( s [ i + 2 ] == '0' || s [ i ] == '0' ) s [ i + 2 ] = '0' ;
else s [ i + 2 ] = '1' ;
}
else if ( s [ i + 1 ] == 'B' ) {
if ( s [ i + 2 ] == '1' |... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.