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-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS | cpp-java | int countPairs ( string str ) {
int result = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ;
return result ;
}
| static int countPairs ( String str ) {
int result = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( Math . abs ( str . charAt ( i ) - str . charAt ( j ) ) == Math . abs ( i - j ) ) result ++ ;
return result ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countPairs ( string str ) {
int result = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++ ;
return result ;
}
</Code> |
cpp-java-CHECK_IF_ALL_THE_ELEMENTS_CAN_BE_MADE_OF_SAME_PARITY_BY_INVERTING_ADJACENT_ELEMENTS | cpp-java | bool flipsPossible ( int a [ ] , int n ) {
int count_odd = 0 , count_even = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] & 1 ) count_odd ++ ;
else count_even ++ ;
}
if ( count_odd % 2 && count_even % 2 ) return false ;
else return true ;
}
| static boolean flipsPossible ( int [ ] a , int n ) {
int count_odd = 0 , count_even = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ;
else count_even ++ ;
}
if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ;
else return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool flipsPossible ( int a [ ] , int n ) {
int count_odd = 0 , count_even = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] & 1 ) count_odd ++ ;
else count_even ++ ;
}
if ( count_odd % 2 && count_even % 2 ) return false ;
else return true ;
}
</Code> |
cpp-java-CHECK_GIVEN_ARRAY_CONTAINS_DUPLICATE_ELEMENTS_WITHIN_K_DISTANCE | cpp-java | bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) {
unordered_set < int > myset ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ;
myset . insert ( arr [ i ] ) ;
if ( i >= k ) myset . erase ( arr [ i - k ] ) ;
}
return false ;
}
| static boolean checkDuplicatesWithinK ( int arr [ ] , int k ) {
HashSet < Integer > set = new HashSet < > ( ) ;
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
if ( set . contains ( arr [ i ] ) ) return true ;
set . add ( arr [ i ] ) ;
if ( i >= k ) set . remove ( arr [ i - k ] ) ;
}
return false ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool checkDuplicatesWithinK ( int arr [ ] , int n , int k ) {
unordered_set < int > myset ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( myset . find ( arr [ i ] ) != myset . end ( ) ) return true ;
myset . insert ( arr [ i ] ) ;
if ( i >= k ) myset . erase ( arr [ i - k ] ) ;
}
return false ;... |
cpp-java-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS_1 | cpp-java | void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) {
cout << " \nDiagonal one : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
cout << mat [ i ] [ i ] * mat [ i ] [ i ] << " " ;
}
cout << " \n\nDiagonal two : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
cout << mat [ i ] [ row - i - 1 ] ... | static void diagonalsquare ( int mat [ ] [ ] , int row , int column ) {
System . out . print ( " Diagonal one : " ) ;
for ( int i = 0 ;
i < row ;
i ++ ) {
System . out . print ( mat [ i ] [ i ] * mat [ i ] [ i ] + " " ) ;
}
System . out . println ( ) ;
System . out . print ( " Diagonal two : " ) ;
f... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) {
cout << " \nDiagonal one : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
cout << mat [ i ] [ i ] * mat [ i ] [ i ] << " " ;
}
cout << " \n\nDiagonal two : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
cout << mat [ i ] [ row - i... |
cpp-java-FIND_THE_LARGEST_RECTANGLE_OF_1S_WITH_SWAPPING_OF_COLUMNS_ALLOWED | cpp-java | int maxArea ( bool mat [ R ] [ C ] ) {
int hist [ R + 1 ] [ C + 1 ] ;
for ( int i = 0 ;
i < C ;
i ++ ) {
hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ;
for ( int j = 1 ;
j < R ;
j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ;
}
for ( int i = 0 ;
i < R ;
i ++ ) {... | static int maxArea ( int mat [ ] [ ] ) {
int hist [ ] [ ] = new int [ R + 1 ] [ C + 1 ] ;
for ( int i = 0 ;
i < C ;
i ++ ) {
hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ;
for ( int j = 1 ;
j < R ;
j ++ ) {
hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ;
}
}
for ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxArea ( bool mat [ R ] [ C ] ) {
int hist [ R + 1 ] [ C + 1 ] ;
for ( int i = 0 ;
i < C ;
i ++ ) {
hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ;
for ( int j = 1 ;
j < R ;
j ++ ) hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ;
}
for ( int i = 0 ;
i < R ;
i... |
cpp-java-LONGEST_SUBSEQUENCE_SUCH_THAT_DIFFERENCE_BETWEEN_ADJACENTS_IS_ONE | cpp-java | int longestSubseqWithDiffOne ( int arr [ ] , int n ) {
int dp [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [ i ] , ... | static int longestSubseqWithDiffOne ( int arr [ ] , int n ) {
int dp [ ] = new int [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int longestSubseqWithDiffOne ( int arr [ ] , int n ) {
int dp [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ] = max ( dp [... |
cpp-java-MAXIMUM_NUMBER_OF_SQUARES_THAT_CAN_BE_FIT_IN_A_RIGHT_ANGLE_ISOSCELES_TRIANGLE | cpp-java | int maxSquare ( int b , int m ) {
return ( b / m - 1 ) * ( b / m ) / 2 ;
}
| static int maxSquare ( int b , int m ) {
return ( b / m - 1 ) * ( b / m ) / 2 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSquare ( int b , int m ) {
return ( b / m - 1 ) * ( b / m ) / 2 ;
}
</Code> |
cpp-java-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE | cpp-java | int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) {
sort ( arr , arr + N ) ;
int dp [ N ] ;
dp [ 0 ] = 0 ;
for ( int i = 1 ;
i < N ;
i ++ ) {
dp [ i ] = dp [ i - 1 ] ;
if ( arr [ i ] - arr [ i - 1 ] < K ) {
if ( i >= 2 ) dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] ... | static int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) {
Arrays . sort ( arr ) ;
int dp [ ] = new int [ N ] ;
dp [ 0 ] = 0 ;
for ( int i = 1 ;
i < N ;
i ++ ) {
dp [ i ] = dp [ i - 1 ] ;
if ( arr [ i ] - arr [ i - 1 ] < K ) {
if ( i >= 2 ) dp [ i ] = Math . max ( dp [ i ] ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) {
sort ( arr , arr + N ) ;
int dp [ N ] ;
dp [ 0 ] = 0 ;
for ( int i = 1 ;
i < N ;
i ++ ) {
dp [ i ] = dp [ i - 1 ] ;
if ( arr [ i ] - arr [ i - 1 ] < K ) {
if ( i >= 2 ) dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr... |
cpp-java-MINIMUM_OPERATION_MAKE_ELEMENTS_EQUAL_ARRAY | cpp-java | int minOperation ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 ;
for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ;
return ( n - max_count ) ;
}
| public static int minOperation ( int arr [ ] , int n ) {
HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ;
else hash . put ( arr [ i ] , 1 ) ;
int max... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minOperation ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 ;
for ( auto i : hash ) if ( max_count < i . second ) max_count = i . second ;
return ( n - max_count ) ;
}
</Code> |
cpp-java-FIND_K_PAIRS_SMALLEST_SUMS_TWO_ARRAYS | cpp-java | void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) {
if ( k > n1 * n2 ) {
cout << "k pairs don't exist" ;
return ;
}
int index2 [ n1 ] ;
memset ( index2 , 0 , sizeof ( index2 ) ) ;
while ( k > 0 ) {
int min_sum = INT_MAX ;
int min_index = 0 ;
for ( int i1 = 0 ;
... | static void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) {
if ( k > n1 * n2 ) {
System . out . print ( "k pairs don't exist" ) ;
return ;
}
int index2 [ ] = new int [ n1 ] ;
while ( k > 0 ) {
int min_sum = Integer . MAX_VALUE ;
int min_index = 0 ;
for ( int i1 = 0 ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) {
if ( k > n1 * n2 ) {
cout << "k pairs don't exist" ;
return ;
}
int index2 [ n1 ] ;
memset ( index2 , 0 , sizeof ( index2 ) ) ;
while ( k > 0 ) {
int min_sum = INT_MAX ;
int min_index = 0 ;
for ( int i1 =... |
cpp-java-PRINT_A_GIVEN_MATRIX_IN_SPIRAL_FORM | cpp-java | void spiralPrint ( int m , int n , int a [ R ] [ C ] ) {
int i , k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) {
cout << a [ k ] [ i ] << " " ;
}
k ++ ;
for ( i = k ;
i < m ;
++ i ) {
cout << a [ i ] [ n - 1 ] << " " ;
}
n -- ;
if ( k... | static void spiralPrint ( int m , int n , int a [ ] [ ] ) {
int i , k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) {
System . out . print ( a [ k ] [ i ] + " " ) ;
}
k ++ ;
for ( i = k ;
i < m ;
++ i ) {
System . out . print ( a [ i ] [ n - 1 ] + ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void spiralPrint ( int m , int n , int a [ R ] [ C ] ) {
int i , k = 0 , l = 0 ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) {
cout << a [ k ] [ i ] << " " ;
}
k ++ ;
for ( i = k ;
i < m ;
++ i ) {
cout << a [ i ] [ n - 1 ] << " " ;
}
n -- ;
... |
cpp-java-CALCULATE_SUM_OF_ALL_NUMBERS_PRESENT_IN_A_STRING | cpp-java | int findSum ( string str ) {
string temp = "" ;
int sum = 0 ;
for ( char ch : str ) {
if ( isdigit ( ch ) ) temp += ch ;
else {
sum += atoi ( temp . c_str ( ) ) ;
temp = "" ;
}
}
return sum + atoi ( temp . c_str ( ) ) ;
}
| static int findSum ( String str ) {
String temp = "" ;
int sum = 0 ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
char ch = str . charAt ( i ) ;
if ( Character . isDigit ( ch ) ) temp += ch ;
else {
sum += Integer . parseInt ( temp ) ;
temp = "0" ;
}
}
return sum + Integer... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findSum ( string str ) {
string temp = "" ;
int sum = 0 ;
for ( char ch : str ) {
if ( isdigit ( ch ) ) temp += ch ;
else {
sum += atoi ( temp . c_str ( ) ) ;
temp = "" ;
}
}
return sum + atoi ( temp . c_str ( ) ) ;
}
</Code> |
cpp-java-NTH_PENTAGONAL_NUMBER | cpp-java | int pentagonalNum ( int n ) {
return ( 3 * n * n - n ) / 2 ;
}
| int pentagonalNum ( int n ) {
return ( 3 * n * n - n ) / 2 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int pentagonalNum ( int n ) {
return ( 3 * n * n - n ) / 2 ;
}
</Code> |
cpp-java-TOTAL_NUMBER_OF_NON_DECREASING_NUMBERS_WITH_N_DIGITS_1 | cpp-java | long long int countNonDecreasing ( int n ) {
int N = 10 ;
long long count = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
count *= ( N + i - 1 ) ;
count /= i ;
}
return count ;
}
| static long countNonDecreasing ( int n ) {
int N = 10 ;
long count = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
count *= ( N + i - 1 ) ;
count /= i ;
}
return count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long int countNonDecreasing ( int n ) {
int N = 10 ;
long long count = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
count *= ( N + i - 1 ) ;
count /= i ;
}
return count ;
}
</Code> |
cpp-java-FREQUENT_ELEMENT_ARRAY_1 | cpp-java | int mostFrequent ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 , res = - 1 ;
for ( auto i : hash ) {
if ( max_count < i . second ) {
res = i . first ;
max_count = i . second ;
}
}
return res ;
}... | static int mostFrequent ( int arr [ ] , int n ) {
Map < Integer , Integer > hp = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int key = arr [ i ] ;
if ( hp . containsKey ( key ) ) {
int freq = hp . get ( key ) ;
freq ++ ;
hp . put ( key , freq ) ;
}
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int mostFrequent ( int arr [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
int max_count = 0 , res = - 1 ;
for ( auto i : hash ) {
if ( max_count < i . second ) {
res = i . first ;
max_count = i . second ;
}
}
return ... |
cpp-java-K_TH_ELEMENT_TWO_SORTED_ARRAYS | cpp-java | int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) {
int sorted1 [ m + n ] ;
int i = 0 , j = 0 , d = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ;
else sorted1 [ d ++ ] = arr2 [ j ++ ] ;
}
while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ... | static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) {
int [ ] sorted1 = new int [ m + n ] ;
int i = 0 , j = 0 , d = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ;
else sorted1 [ d ++ ] = arr2 [ j ++ ] ;
}
while ( i < m ) sorted1 [ d ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) {
int sorted1 [ m + n ] ;
int i = 0 , j = 0 , d = 0 ;
while ( i < m && j < n ) {
if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ;
else sorted1 [ d ++ ] = arr2 [ j ++ ] ;
}
while ( i < m ) sorted1 [ d ++ ] = arr1 [ ... |
cpp-java-DYNAMIC_PROGRAMMING_SET_9_BINOMIAL_COEFFICIENT | cpp-java | int binomialCoeff ( int n , int k ) {
if ( k == 0 || k == n ) return 1 ;
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ;
}
| static int binomialCoeff ( int n , int k ) {
if ( k == 0 || k == n ) return 1 ;
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int binomialCoeff ( int n , int k ) {
if ( k == 0 || k == n ) return 1 ;
return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ;
}
</Code> |
cpp-java-STRING_CONTAINING_FIRST_LETTER_EVERY_WORD_GIVEN_STRING_SPACES | cpp-java | string firstLetterWord ( string str ) {
string result = "" ;
bool v = true ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == ' ' ) v = true ;
else if ( str [ i ] != ' ' && v == true ) {
result . push_back ( str [ i ] ) ;
v = false ;
}
}
return result ;
}
| static String firstLetterWord ( String str ) {
String result = "" ;
boolean v = true ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ' ▁ ' ) {
v = true ;
}
else if ( str . charAt ( i ) != ' ▁ ' && v == true ) {
result += ( str . charAt ( i ) ) ;
v =... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string firstLetterWord ( string str ) {
string result = "" ;
bool v = true ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == ' ' ) v = true ;
else if ( str [ i ] != ' ' && v == true ) {
result . push_back ( str [ i ] ) ;
v = false ;
}
}
return result ;
}
... |
cpp-java-REARRANGE_ARRAY_SUCH_THAT_EVEN_POSITIONED_ARE_GREATER_THAN_ODD | cpp-java | void assign ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int ans [ n ] ;
int p = 0 , q = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ;
else ans [ i ] = a [ p ++ ] ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << ans [ i ] << " " ;
}
| static void assign ( int a [ ] , int n ) {
Arrays . sort ( a ) ;
int ans [ ] = new int [ n ] ;
int p = 0 , q = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ;
else ans [ i ] = a [ p ++ ] ;
}
for ( int i = 0 ;
i < n ;
i ++ ) System . out . print (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void assign ( int a [ ] , int n ) {
sort ( a , a + n ) ;
int ans [ n ] ;
int p = 0 , q = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ;
else ans [ i ] = a [ p ++ ] ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << ans [ i ] << " " ;
}
</Code> |
cpp-java-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY | cpp-java | void printDistinct ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] == arr [ j ] ) break ;
if ( i == j ) cout << arr [ i ] << " " ;
}
}
| static void printDistinct ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] == arr [ j ] ) break ;
if ( i == j ) System . out . print ( arr [ i ] + " " ) ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printDistinct ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] == arr [ j ] ) break ;
if ( i == j ) cout << arr [ i ] << " " ;
}
}
</Code> |
cpp-java-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2 | cpp-java | int countSolutions ( int n ) {
int res = 0 ;
for ( int x = 0 ;
x * x < n ;
x ++ ) for ( int y = 0 ;
x * x + y * y < n ;
y ++ ) res ++ ;
return res ;
}
| static int countSolutions ( int n ) {
int res = 0 ;
for ( int x = 0 ;
x * x < n ;
x ++ ) for ( int y = 0 ;
x * x + y * y < n ;
y ++ ) res ++ ;
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSolutions ( int n ) {
int res = 0 ;
for ( int x = 0 ;
x * x < n ;
x ++ ) for ( int y = 0 ;
x * x + y * y < n ;
y ++ ) res ++ ;
return res ;
}
</Code> |
cpp-java-FIND_RELATIVE_COMPLEMENT_TWO_SORTED_ARRAYS | cpp-java | void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
int i = 0 , j = 0 ;
while ( i < n && j < m ) {
if ( arr1 [ i ] < arr2 [ j ] ) {
cout << arr1 [ i ] << " " ;
i ++ ;
}
else if ( arr1 [ i ] > arr2 [ j ] ) {
j ++ ;
}
else if ( arr1 [ i ] == arr2 [ j ] ) {
... | static void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
int i = 0 , j = 0 ;
while ( i < n && j < m ) {
if ( arr1 [ i ] < arr2 [ j ] ) {
System . out . print ( arr1 [ i ] + " " ) ;
i ++ ;
}
else if ( arr1 [ i ] > arr2 [ j ] ) {
j ++ ;
}
else if ( arr1 [ ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) {
int i = 0 , j = 0 ;
while ( i < n && j < m ) {
if ( arr1 [ i ] < arr2 [ j ] ) {
cout << arr1 [ i ] << " " ;
i ++ ;
}
else if ( arr1 [ i ] > arr2 [ j ] ) {
j ++ ;
}
else if ( arr1 [ i ] == arr2 [ j ... |
cpp-java-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D_1 | cpp-java | int findFourElements ( int arr [ ] , int n ) {
unordered_map < int , pair < int , int >> mp ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) mp [ arr [ i ] + arr [ j ] ] = {
i , j }
;
int d = INT_MIN ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int ... | static int findFourElements ( int [ ] arr , int n ) {
HashMap < Integer , Indexes > map = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
map . put ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ;
}
}
int d = Integer . MIN_VALUE ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findFourElements ( int arr [ ] , int n ) {
unordered_map < int , pair < int , int >> mp ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) mp [ arr [ i ] + arr [ j ] ] = {
i , j }
;
int d = INT_MIN ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
for... |
cpp-java-COUNT_TRAILING_ZEROES_FACTORIAL_NUMBER | cpp-java | int findTrailingZeros ( int n ) {
int count = 0 ;
for ( int i = 5 ;
n / i >= 1 ;
i *= 5 ) count += n / i ;
return count ;
}
| static int findTrailingZeros ( int n ) {
int count = 0 ;
for ( int i = 5 ;
n / i >= 1 ;
i *= 5 ) count += n / i ;
return count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findTrailingZeros ( int n ) {
int count = 0 ;
for ( int i = 5 ;
n / i >= 1 ;
i *= 5 ) count += n / i ;
return count ;
}
</Code> |
cpp-java-PRIMALITY_TEST_SET_5USING_LUCAS_LEHMER_SERIES | cpp-java | bool isPrime ( int p ) {
long long checkNumber = pow ( 2 , p ) - 1 ;
long long nextval = 4 % checkNumber ;
for ( int i = 1 ;
i < p - 1 ;
i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ;
return ( nextval == 0 ) ;
}
| static boolean isPrime ( int p ) {
double checkNumber = Math . pow ( 2 , p ) - 1 ;
double nextval = 4 % checkNumber ;
for ( int i = 1 ;
i < p - 1 ;
i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ;
return ( nextval == 0 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPrime ( int p ) {
long long checkNumber = pow ( 2 , p ) - 1 ;
long long nextval = 4 % checkNumber ;
for ( int i = 1 ;
i < p - 1 ;
i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ;
return ( nextval == 0 ) ;
}
</Code> |
cpp-java-COUNT_OF_SUB_STRINGS_THAT_DO_NOT_CONTAIN_ALL_THE_CHARACTERS_FROM_THE_SET_A_B_C_AT_THE_SAME_TIME | cpp-java | int CountSubstring ( char str [ ] , int n ) {
int ans = ( n * ( n + 1 ) ) / 2 ;
int a_index = 0 ;
int b_index = 0 ;
int c_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( str [ i ] == 'a' ) {
a_index = i + 1 ;
ans -= min ( b_index , c_index ) ;
}
else if ( str [ i ] == 'b' ) {
... | static int CountSubstring ( char str [ ] , int n ) {
int ans = ( n * ( n + 1 ) ) / 2 ;
int a_index = 0 ;
int b_index = 0 ;
int c_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( str [ i ] == ' a ' ) {
a_index = i + 1 ;
ans -= Math . min ( b_index , c_index ) ;
}
else if ( str [ ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int CountSubstring ( char str [ ] , int n ) {
int ans = ( n * ( n + 1 ) ) / 2 ;
int a_index = 0 ;
int b_index = 0 ;
int c_index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( str [ i ] == 'a' ) {
a_index = i + 1 ;
ans -= min ( b_index , c_index ) ;
}
else if ( str [ i ] == 'b... |
cpp-java-CHECK_GIVEN_STRING_ROTATION_PALINDROME | cpp-java | bool isPalindrome ( string str ) {
int l = 0 ;
int h = str . length ( ) - 1 ;
while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ;
return true ;
}
| static boolean isPalindrome ( String str ) {
int l = 0 ;
int h = str . length ( ) - 1 ;
while ( h > l ) if ( str . charAt ( l ++ ) != str . charAt ( h -- ) ) return false ;
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPalindrome ( string str ) {
int l = 0 ;
int h = str . length ( ) - 1 ;
while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false ;
return true ;
}
</Code> |
cpp-java-MAXIMUM_NUMBER_CHOCOLATES_DISTRIBUTED_EQUALLY_AMONG_K_STUDENTS | cpp-java | int maxNumOfChocolates ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int sum [ n ] , curr_rem ;
int maxSum = 0 ;
sum [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_rem = sum [ i ] % k ;
... | static int maxNumOfChocolates ( int arr [ ] , int n , int k ) {
HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ;
int [ ] sum = new int [ n ] ;
int curr_rem ;
int maxSum = 0 ;
sum [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ;
for ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxNumOfChocolates ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int sum [ n ] , curr_rem ;
int maxSum = 0 ;
sum [ 0 ] = arr [ 0 ] ;
for ( int i = 1 ;
i < n ;
i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_rem = sum [ i ] % ... |
cpp-java-DECODE_STRING_RECURSIVELY_ENCODED_COUNT_FOLLOWED_SUBSTRING | cpp-java | string decode ( string str ) {
stack < int > integerstack ;
stack < char > stringstack ;
string temp = "" , result = "" ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
int count = 0 ;
if ( str [ i ] >= '0' && str [ i ] <= '9' ) {
while ( str [ i ] >= '0' && str [ i ] <= '9' ) {
cou... | static String decode ( String str ) {
Stack < Integer > integerstack = new Stack < > ( ) ;
Stack < Character > stringstack = new Stack < > ( ) ;
String temp = "" , result = "" ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
int count = 0 ;
if ( Character . isDigit ( str . charAt ( i ) ) ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string decode ( string str ) {
stack < int > integerstack ;
stack < char > stringstack ;
string temp = "" , result = "" ;
for ( int i = 0 ;
i < str . length ( ) ;
i ++ ) {
int count = 0 ;
if ( str [ i ] >= '0' && str [ i ] <= '9' ) {
while ( str [ i ] >= '0' && str [ i ] <= '9' ) {
... |
cpp-java-MINIMUM_SUM_CHOOSING_MINIMUM_PAIRS_ARRAY | cpp-java | int minSum ( int A [ ] , int n ) {
int min_val = * min_element ( A , A + n ) ;
return ( min_val * ( n - 1 ) ) ;
}
| static int minSum ( int [ ] A , int n ) {
int min_val = Arrays . stream ( A ) . min ( ) . getAsInt ( ) ;
return ( min_val * ( n - 1 ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minSum ( int A [ ] , int n ) {
int min_val = * min_element ( A , A + n ) ;
return ( min_val * ( n - 1 ) ) ;
}
</Code> |
cpp-java-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER_1 | cpp-java | bool isPower ( int x , int y ) {
int res1 = log ( y ) / log ( x ) ;
double res2 = log ( y ) / log ( x ) ;
return ( res1 == res2 ) ;
}
| static boolean isPower ( int x , int y ) {
int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ;
double res2 = Math . log ( y ) / Math . log ( x ) ;
return ( res1 == res2 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPower ( int x , int y ) {
int res1 = log ( y ) / log ( x ) ;
double res2 = log ( y ) / log ( x ) ;
return ( res1 == res2 ) ;
}
</Code> |
cpp-java-COUNT_WORDS_APPEAR_EXACTLY_TWO_TIMES_ARRAY_WORDS | cpp-java | int countWords ( string str [ ] , int n ) {
unordered_map < string , int > m ;
for ( int i = 0 ;
i < n ;
i ++ ) m [ str [ i ] ] += 1 ;
int res = 0 ;
for ( auto it = m . begin ( ) ;
it != m . end ( ) ;
it ++ ) if ( ( it -> second == 2 ) ) res ++ ;
return res ;
}
| static int countWords ( String str [ ] , int n ) {
HashMap < String , Integer > m = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( m . containsKey ( str [ i ] ) ) {
int get = m . get ( str [ i ] ) ;
m . put ( str [ i ] , get + 1 ) ;
}
else {
m . put ( str [ i ] , 1 )... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countWords ( string str [ ] , int n ) {
unordered_map < string , int > m ;
for ( int i = 0 ;
i < n ;
i ++ ) m [ str [ i ] ] += 1 ;
int res = 0 ;
for ( auto it = m . begin ( ) ;
it != m . end ( ) ;
it ++ ) if ( ( it -> second == 2 ) ) res ++ ;
return res ;
}
</Code> |
cpp-java-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D | cpp-java | int findLargestd ( int S [ ] , int n ) {
bool found = false ;
sort ( S , S + n ) ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i == j ) continue ;
for ( int k = j + 1 ;
k < n ;
k ++ ) {
if ( i == k ) continue ;
for ( int l... | static int findLargestd ( int [ ] S , int n ) {
boolean found = false ;
Arrays . sort ( S ) ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i == j ) continue ;
for ( int k = j + 1 ;
k < n ;
k ++ ) {
if ( i == k ) continue ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findLargestd ( int S [ ] , int n ) {
bool found = false ;
sort ( S , S + n ) ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( i == j ) continue ;
for ( int k = j + 1 ;
k < n ;
k ++ ) {
if ( i == k ) continue ;
for ... |
cpp-java-MAXIMUM_MINIMUM_VALUES_ALGEBRAIC_EXPRESSION | cpp-java | e9 int minMaxValues ( int arr [ ] , int n , int m ) {
int sum = 0 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
sum += arr [ i ] ;
arr [ i ] += 50 ;
}
bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
... | static void minMaxValues ( int [ ] arr , int n , int m ) {
int sum = 0 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
sum += arr [ i ] ;
arr [ i ] += 50 ;
}
boolean dp [ ] [ ] = new boolean [ MAX + 1 ] [ MAX * MAX + 1 ] ;
dp [ 0 ] [ 0 ] = true ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
f... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
e9 int minMaxValues ( int arr [ ] , int n , int m ) {
int sum = 0 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ++ ) {
sum += arr [ i ] ;
arr [ i ] += 50 ;
}
bool dp [ MAX + 1 ] [ MAX * MAX + 1 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < ( n + m ) ;
i ... |
cpp-java-PRINT_POSSIBLE_EDGES_TREE_GIVEN_DIAMETER_HEIGHT_VERTICES | cpp-java | void constructTree ( int n , int d , int h ) {
if ( d == 1 ) {
if ( n == 2 && h == 1 ) {
cout << "1 2" << endl ;
return ;
}
cout << "-1" << endl ;
return ;
}
if ( d > 2 * h ) {
cout << "-1" << endl ;
return ;
}
for ( int i = 1 ;
i <= h ;
i ++ ) cout << i << " " << i + 1... | static void constructTree ( int n , int d , int h ) {
if ( d == 1 ) {
if ( n == 2 && h == 1 ) {
System . out . println ( "1 2" ) ;
return ;
}
System . out . println ( "-1" ) ;
return ;
}
if ( d > 2 * h ) {
System . out . println ( "-1" ) ;
return ;
}
for ( int i = 1 ;
i <... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void constructTree ( int n , int d , int h ) {
if ( d == 1 ) {
if ( n == 2 && h == 1 ) {
cout << "1 2" << endl ;
return ;
}
cout << "-1" << endl ;
return ;
}
if ( d > 2 * h ) {
cout << "-1" << endl ;
return ;
}
for ( int i = 1 ;
i <= h ;
i ++ ) cout << i << " " <... |
cpp-java-NUMBER_RECTANGLES_NM_GRID | cpp-java | int rectCount ( int n , int m ) {
return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ;
}
| public static long rectCount ( int n , int m ) {
return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int rectCount ( int n , int m ) {
return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ;
}
</Code> |
cpp-java-SORTING_USING_TRIVIAL_HASH_FUNCTION | cpp-java | void sortUsingHash ( int a [ ] , int n ) {
int max = * std :: max_element ( a , a + n ) ;
int hash [ max + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ a [ i ] ] += 1 ;
for ( int i = 0 ;
i <= max ;
i ++ ) {
if ( hash [ i ] ) {
for ( int j = 0 ;
j < hash... | static void sortUsingHash ( int a [ ] , int n ) {
int max = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ;
int hash [ ] = new int [ max + 1 ] ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ a [ i ] ] += 1 ;
for ( int i = 0 ;
i <= max ;
i ++ ) {
if ( hash [ i ] != 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 sortUsingHash ( int a [ ] , int n ) {
int max = * std :: max_element ( a , a + n ) ;
int hash [ max + 1 ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ a [ i ] ] += 1 ;
for ( int i = 0 ;
i <= max ;
i ++ ) {
if ( hash [ i ] ) {
for ( int j = 0 ;
j... |
cpp-java-SUBARRAY_NO_PAIR_SUM_DIVISIBLE_K | cpp-java | void subarrayDivisibleByK ( int arr [ ] , int n , int k ) {
map < int , int > mp ;
int s = 0 , e = 0 , maxs = 0 , maxe = 0 ;
mp [ arr [ 0 ] % k ] ++ ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int mod = arr [ i ] % k ;
while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) {
mp [ arr [ s ] ... | static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) {
int [ ] mp = new int [ 1000 ] ;
int s = 0 , e = 0 , maxs = 0 , maxe = 0 ;
mp [ arr [ 0 ] % k ] ++ ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int mod = arr [ i ] % k ;
while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void subarrayDivisibleByK ( int arr [ ] , int n , int k ) {
map < int , int > mp ;
int s = 0 , e = 0 , maxs = 0 , maxe = 0 ;
mp [ arr [ 0 ] % k ] ++ ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int mod = arr [ i ] % k ;
while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) {
mp [ arr... |
cpp-java-CHECK_DIVISIBILITY_BINARY_STRING_2K | cpp-java | bool isDivisible ( char str [ ] , int k ) {
int n = strlen ( str ) ;
int c = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ;
return ( c == k ) ;
}
| static boolean isDivisible ( String str , int k ) {
int n = str . length ( ) ;
int c = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) if ( str . charAt ( n - i - 1 ) == ' 0 ' ) c ++ ;
return ( c == k ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isDivisible ( char str [ ] , int k ) {
int n = strlen ( str ) ;
int c = 0 ;
for ( int i = 0 ;
i < k ;
i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++ ;
return ( c == k ) ;
}
</Code> |
cpp-java-NUMBER_DIGITS_REMOVED_MAKE_NUMBER_DIVISIBLE_3 | cpp-java | int divisible ( string num ) {
int n = num . length ( ) ;
int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ;
if ( sum % 3 == 0 ) return 0 ;
if ( n == 1 ) return - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ;
if ( n == 2 ) return - 1 ;
retu... | static int divisible ( String num ) {
int n = num . length ( ) ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) sum += ( int ) ( num . charAt ( i ) ) ;
if ( sum % 3 == 0 ) return 0 ;
if ( n == 1 ) return - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( sum % 3 == ( num . charAt ( i ) - ' 0 ' ) % 3 ) retu... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int divisible ( string num ) {
int n = num . length ( ) ;
int sum = accumulate ( begin ( num ) , end ( num ) , 0 ) - '0' * 1 ;
if ( sum % 3 == 0 ) return 0 ;
if ( n == 1 ) return - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( sum % 3 == ( num [ i ] - '0' ) % 3 ) return 1 ;
if ( n == 2 ) return - 1 ;... |
cpp-java-SHORTEST_COMMON_SUPERSEQUENCE | cpp-java | int superSeq ( char * X , char * Y , int m , int n ) {
if ( ! m ) return n ;
if ( ! n ) return m ;
if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ;
return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ;
}
| static int superSeq ( String X , String Y , int m , int n ) {
if ( m == 0 ) return n ;
if ( n == 0 ) return m ;
if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ;
return 1 + Math . min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int superSeq ( char * X , char * Y , int m , int n ) {
if ( ! m ) return n ;
if ( ! n ) return m ;
if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ;
return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ;
}
</Code> |
cpp-java-PROGRAM_SWAP_UPPER_DIAGONAL_ELEMENTS_LOWER_DIAGONAL_ELEMENTS_MATRIX | cpp-java | void swapUpperToLower ( int arr [ n ] [ n ] ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
}
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;... | static void swapUpperToLower ( int arr [ ] [ ] ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
}
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void swapUpperToLower ( int arr [ n ] [ n ] ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int temp = arr [ i ] [ j ] ;
arr [ i ] [ j ] = arr [ j ] [ i ] ;
arr [ j ] [ i ] = temp ;
}
}
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int ... |
cpp-java-SUM_K_TH_GROUP_ODD_POSITIVE_NUMBERS | cpp-java | int kthgroupsum ( int k ) {
int cur = ( k * ( k - 1 ) ) + 1 ;
int sum = 0 ;
while ( k -- ) {
sum += cur ;
cur += 2 ;
}
return sum ;
}
| public static int kthgroupsum ( int k ) {
int cur = ( k * ( k - 1 ) ) + 1 ;
int sum = 0 ;
while ( k -- > 0 ) {
sum += cur ;
cur += 2 ;
}
return sum ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int kthgroupsum ( int k ) {
int cur = ( k * ( k - 1 ) ) + 1 ;
int sum = 0 ;
while ( k -- ) {
sum += cur ;
cur += 2 ;
}
return sum ;
}
</Code> |
cpp-java-CHECK_GIVEN_MATRIX_IS_MAGIC_SQUARE_OR_NOT | cpp-java | bool isMagicSquare ( int mat [ ] [ N ] ) {
int sum = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) sum = sum + mat [ i ] [ i ] ;
for ( int i = 0 ;
i < N ;
i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ;
if ( sum != sum2 ) return false ;
for ( int i = 0 ;
i < N ;
i ++ ) {
int rowSum = 0 ;
... | static boolean isMagicSquare ( int mat [ ] [ ] ) {
int sum = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) sum = sum + mat [ i ] [ i ] ;
for ( int i = 0 ;
i < N ;
i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ;
if ( sum != sum2 ) return false ;
for ( int i = 0 ;
i < N ;
i ++ ) {
int rowSum = ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isMagicSquare ( int mat [ ] [ N ] ) {
int sum = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < N ;
i ++ ) sum = sum + mat [ i ] [ i ] ;
for ( int i = 0 ;
i < N ;
i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ;
if ( sum != sum2 ) return false ;
for ( int i = 0 ;
i < N ;
i ++ ) {
int rowSum = 0... |
cpp-java-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX_1 | cpp-java | bool isScalarMatrix ( int mat [ N ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
for ( int i = 0 ;
i < N - 1 ;
i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ;
return true ;
}
| static boolean isScalarMatrix ( int mat [ ] [ ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
for ( int i = 0 ;
i < N - 1 ;
i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ;
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isScalarMatrix ( int mat [ N ] [ N ] ) {
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ;
for ( int i = 0 ;
i < N - 1 ;
i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ;
return true ;
}
</C... |
cpp-java-LONGEST_SUBARRAY_SUM_DIVISIBLE_K | cpp-java | int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int mod_arr [ n ] , max = 0 ;
int curr_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_sum += arr [ i ] ;
mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {
if... | static int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) {
HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ;
int mod_arr [ ] = new int [ n ] ;
int max = 0 ;
int curr_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_sum += arr [ i ] ;
mod_arr [ i ] = ( ( curr_sum ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) {
unordered_map < int , int > um ;
int mod_arr [ n ] , max = 0 ;
int curr_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
curr_sum += arr [ i ] ;
mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ;
}
for ( int i = 0 ;
i < n ;
i ++ ) {... |
cpp-java-REVERSE_A_STRING_USING_RECURSION | cpp-java | void reverse ( string str ) {
if ( str . size ( ) == 0 ) {
return ;
}
reverse ( str . substr ( 1 ) ) ;
cout << str [ 0 ] ;
}
| void reverse ( String str ) {
if ( ( str == null ) || ( str . length ( ) <= 1 ) ) System . out . println ( str ) ;
else {
System . out . print ( str . charAt ( str . length ( ) - 1 ) ) ;
reverse ( str . substring ( 0 , str . length ( ) - 1 ) ) ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void reverse ( string str ) {
if ( str . size ( ) == 0 ) {
return ;
}
reverse ( str . substr ( 1 ) ) ;
cout << str [ 0 ] ;
}
</Code> |
cpp-java-BALANCED_EXPRESSIONS_SUCH_THAT_GIVEN_POSITIONS_HAVE_OPENING_BRACKETS | cpp-java | long long arrangeBraces ( int n , int pos [ ] , int k ) {
bool h [ N ] ;
int dp [ N ] [ N ] ;
memset ( h , 0 , sizeof h ) ;
memset ( dp , 0 , sizeof dp ) ;
for ( int i = 0 ;
i < k ;
i ++ ) h [ pos [ i ] ] = 1 ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i <= 2 * n ;
i ++ ) {
for ( int j = 0 ;
... | static long arrangeBraces ( int n , int pos [ ] , int k ) {
boolean h [ ] = new boolean [ N ] ;
int dp [ ] [ ] = new int [ N ] [ N ] ;
for ( int i = 0 ;
i < k ;
i ++ ) {
h [ pos [ i ] ] = true ;
}
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i <= 2 * n ;
i ++ ) {
for ( int j = 0 ;
j <= 2 * n ;... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long arrangeBraces ( int n , int pos [ ] , int k ) {
bool h [ N ] ;
int dp [ N ] [ N ] ;
memset ( h , 0 , sizeof h ) ;
memset ( dp , 0 , sizeof dp ) ;
for ( int i = 0 ;
i < k ;
i ++ ) h [ pos [ i ] ] = 1 ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 1 ;
i <= 2 * n ;
i ++ ) {
for ( int j = 0... |
cpp-java-DIVISIBILITY_BY_12_FOR_A_LARGE_NUMBER | cpp-java | bool isDvisibleBy12 ( string num ) {
if ( num . length ( ) >= 3 ) {
int d1 = ( int ) num [ num . length ( ) - 1 ] ;
if ( d1 % 2 != 0 ) return ( 0 ) ;
int d2 = ( int ) num [ num . length ( ) - 2 ] ;
int sum = 0 ;
for ( int i = 0 ;
i < num . length ( ) ;
i ++ ) sum += num [ i ] ;
return ... | static boolean isDvisibleBy12 ( String num ) {
if ( num . length ( ) >= 3 ) {
int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ;
if ( d1 % 2 != 0 ) return false ;
int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ;
int sum = 0 ;
for ( int i = 0 ;
i < num . length ( ) ;
i ++ ) su... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isDvisibleBy12 ( string num ) {
if ( num . length ( ) >= 3 ) {
int d1 = ( int ) num [ num . length ( ) - 1 ] ;
if ( d1 % 2 != 0 ) return ( 0 ) ;
int d2 = ( int ) num [ num . length ( ) - 2 ] ;
int sum = 0 ;
for ( int i = 0 ;
i < num . length ( ) ;
i ++ ) sum += num [ i ] ;
... |
cpp-java-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY | cpp-java | int maxDiff ( int arr [ ] , int n ) {
int SubsetSum_1 = 0 , SubsetSum_2 = 0 ;
for ( int i = 0 ;
i <= n - 1 ;
i ++ ) {
bool isSingleOccurance = true ;
for ( int j = i + 1 ;
j <= n - 1 ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) {
isSingleOccurance = false ;
arr [ i ] = arr [ j ... | static int maxDiff ( int [ ] arr , int n ) {
int SubsetSum_1 = 0 , SubsetSum_2 = 0 ;
for ( int i = 0 ;
i <= n - 1 ;
i ++ ) {
boolean isSingleOccurance = true ;
for ( int j = i + 1 ;
j <= n - 1 ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) {
isSingleOccurance = false ;
arr [ i ] ... | 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 SubsetSum_1 = 0 , SubsetSum_2 = 0 ;
for ( int i = 0 ;
i <= n - 1 ;
i ++ ) {
bool isSingleOccurance = true ;
for ( int j = i + 1 ;
j <= n - 1 ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) {
isSingleOccurance = false ;
arr [ i ] = a... |
cpp-java-LEXICOGRAPHICALLY_SMALLEST_STRING_OBTAINED_CONCATENATING_ARRAY | cpp-java | string lexSmallest ( string a [ ] , int n ) {
sort ( a , a + n , compare ) ;
string answer = "" ;
for ( int i = 0 ;
i < n ;
i ++ ) answer += a [ i ] ;
return answer ;
}
| static String lexsmallest ( String a [ ] , int n ) {
sort ( a , n ) ;
String answer = "" ;
for ( int i = 0 ;
i < n ;
i ++ ) answer += a [ i ] ;
return answer ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string lexSmallest ( string a [ ] , int n ) {
sort ( a , a + n , compare ) ;
string answer = "" ;
for ( int i = 0 ;
i < n ;
i ++ ) answer += a [ i ] ;
return answer ;
}
</Code> |
cpp-java-MAXIMUM_WEIGHT_PATH_ENDING_ELEMENT_LAST_ROW_MATRIX | cpp-java | int maxCost ( int mat [ ] [ MAX ] , int N ) {
int dp [ N ] [ N ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) for ( int j = 1 ;
j < i + 1 && j < N ;
... | public static int maxCost ( int mat [ ] [ ] , int N ) {
int dp [ ] [ ] = new int [ N ] [ N ] ;
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) for ( int j = 1 ;
j < i + 1 && j < N ;
j ++ ) dp... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxCost ( int mat [ ] [ MAX ] , int N ) {
int dp [ N ] [ N ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ;
for ( int i = 1 ;
i < N ;
i ++ ) for ( int j = 1 ;
j < i + 1 && j ... |
cpp-java-WAYS_REMOVE_ONE_ELEMENT_BINARY_STRING_XOR_BECOMES_ZERO | cpp-java | int xorZero ( string str ) {
int one_count = 0 , zero_count = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( str [ i ] == '1' ) one_count ++ ;
else zero_count ++ ;
if ( one_count % 2 == 0 ) return zero_count ;
return one_count ;
}
| static int xorZero ( String s ) {
int one_count = 0 , zero_count = 0 ;
char [ ] str = s . toCharArray ( ) ;
int n = str . length ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( str [ i ] == ' 1 ' ) one_count ++ ;
else zero_count ++ ;
if ( one_count % 2 == 0 ) return zero_count ;
return one_count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int xorZero ( string str ) {
int one_count = 0 , zero_count = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( str [ i ] == '1' ) one_count ++ ;
else zero_count ++ ;
if ( one_count % 2 == 0 ) return zero_count ;
return one_count ;
}
</Code> |
cpp-java-N_TH_ROOT_NUMBER | cpp-java | double nthRoot ( int A , int N ) {
double xPre = rand ( ) % 10 ;
double eps = 1 e - 3 ;
double delX = INT_MAX ;
double xK ;
while ( delX > eps ) {
xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ;
delX = abs ( xK - xPre ) ;
xPre = xK ;
}
return xK ;
}
| static double nthRoot ( int A , int N ) {
double xPre = Math . random ( ) % 10 ;
double eps = 0.001 ;
double delX = 2147483647 ;
double xK = 0.0 ;
while ( delX > eps ) {
xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ;
delX = Math . abs ( xK - xPre ) ;
xPre... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
double nthRoot ( int A , int N ) {
double xPre = rand ( ) % 10 ;
double eps = 1 e - 3 ;
double delX = INT_MAX ;
double xK ;
while ( delX > eps ) {
xK = ( ( N - 1.0 ) * xPre + ( double ) A / pow ( xPre , N - 1 ) ) / ( double ) N ;
delX = abs ( xK - xPre ) ;
xPre = xK ;
}
return xK ;
}
<... |
cpp-java-RETURN_PREVIOUS_ELEMENT_IN_AN_EXPANDING_MATRIX | cpp-java | string findLeft ( string str ) {
int n = str . length ( ) ;
while ( n -- ) {
if ( str [ n ] == 'd' ) {
str [ n ] = 'c' ;
break ;
}
if ( str [ n ] == 'b' ) {
str [ n ] = 'a' ;
break ;
}
if ( str [ n ] == 'a' ) str [ n ] = 'b' ;
else if ( str [ n ] == 'c' ) str [ n ] = ... | static StringBuilder findLeft ( StringBuilder str ) {
int n = str . length ( ) ;
while ( n > 0 ) {
n -- ;
if ( str . charAt ( n ) == ' d ' ) {
str . setCharAt ( n , ' c ' ) ;
break ;
}
if ( str . charAt ( n ) == ' b ' ) {
str . setCharAt ( n , ' a ' ) ;
break ;
}
if (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string findLeft ( string str ) {
int n = str . length ( ) ;
while ( n -- ) {
if ( str [ n ] == 'd' ) {
str [ n ] = 'c' ;
break ;
}
if ( str [ n ] == 'b' ) {
str [ n ] = 'a' ;
break ;
}
if ( str [ n ] == 'a' ) str [ n ] = 'b' ;
else if ( str [ n ] == 'c' ) str [... |
cpp-java-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS | cpp-java | int subset ( int ar [ ] , int n ) {
int res = 0 ;
sort ( ar , ar + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 1 ;
for ( ;
i < n - 1 ;
i ++ ) {
if ( ar [ i ] == ar [ i + 1 ] ) count ++ ;
else break ;
}
res = max ( res , count ) ;
}
return res ;
}
| public static int subset ( int ar [ ] , int n ) {
int res = 0 ;
Arrays . sort ( ar ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 1 ;
for ( ;
i < n - 1 ;
i ++ ) {
if ( ar [ i ] == ar [ i + 1 ] ) count ++ ;
else break ;
}
res = Math . max ( res , count ) ;
}
return r... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int subset ( int ar [ ] , int n ) {
int res = 0 ;
sort ( ar , ar + n ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 1 ;
for ( ;
i < n - 1 ;
i ++ ) {
if ( ar [ i ] == ar [ i + 1 ] ) count ++ ;
else break ;
}
res = max ( res , count ) ;
}
return res ;
}
</Code... |
cpp-java-SMALLEST_SUBARRAY_K_DISTINCT_NUMBERS | cpp-java | void minRange ( int arr [ ] , int n , int k ) {
int l = 0 , r = n ;
for ( int i = 0 ;
i < n ;
i ++ ) {
unordered_set < int > s ;
int j ;
for ( j = i ;
j < n ;
j ++ ) {
s . insert ( arr [ j ] ) ;
if ( s . size ( ) == k ) {
if ( ( j - i ) < ( r - l ) ) {
r = j ;
... | static void minRange ( int arr [ ] , int n , int k ) {
int l = 0 , r = n ;
for ( int i = 0 ;
i < n ;
i ++ ) {
Set < Integer > s = new HashSet < Integer > ( ) ;
int j ;
for ( j = i ;
j < n ;
j ++ ) {
s . add ( arr [ j ] ) ;
if ( s . size ( ) == k ) {
if ( ( j - i ) < ( r -... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void minRange ( int arr [ ] , int n , int k ) {
int l = 0 , r = n ;
for ( int i = 0 ;
i < n ;
i ++ ) {
unordered_set < int > s ;
int j ;
for ( j = i ;
j < n ;
j ++ ) {
s . insert ( arr [ j ] ) ;
if ( s . size ( ) == k ) {
if ( ( j - i ) < ( r - l ) ) {
r ... |
cpp-java-OVERLAPPING_SUM_TWO_ARRAY | cpp-java | int findSum ( int A [ ] , int B [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] ++ ;
hash [ B [ i ] ] ++ ;
}
int sum = 0 ;
for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ;
return sum ;
}
| static int findSum ( int [ ] A , int [ ] B , int n ) {
HashMap < Integer , Integer > hash = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( hash . containsKey ( A [ i ] ) ) hash . put ( A [ i ] , 1 + hash . get ( A [ i ] ) ) ;
else hash . put ( A [ i ] , 1 ) ;
if ( hash . containsKey (... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findSum ( int A [ ] , int B [ ] , int n ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) {
hash [ A [ i ] ] ++ ;
hash [ B [ i ] ] ++ ;
}
int sum = 0 ;
for ( auto x : hash ) if ( x . second == 1 ) sum += x . first ;
return sum ;
}
</Code> |
cpp-java-LONGEST_PALINDROME_SUBSTRING_SET_1 | cpp-java | void printSubStr ( string str , int low , int high ) {
for ( int i = low ;
i <= high ;
++ i ) cout << str [ i ] ;
}
| static void printSubStr ( String str , int low , int high ) {
System . out . println ( str . substring ( low , high + 1 ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printSubStr ( string str , int low , int high ) {
for ( int i = low ;
i <= high ;
++ i ) cout << str [ i ] ;
}
</Code> |
cpp-java-FIND_SUM_EVEN_INDEX_BINOMIAL_COEFFICIENTS | cpp-java | int evenSum ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
for ( int... | static int evenSum ( int n ) {
int C [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= Math . min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int evenSum ( int n ) {
int C [ n + 1 ] [ n + 1 ] ;
int i , j ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( j = 0 ;
j <= min ( i , n ) ;
j ++ ) {
if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ;
else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ;
}
}
int sum = 0 ;
fo... |
cpp-java-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N_1 | cpp-java | int getMinSquares ( int n ) {
int * dp = new int [ n + 1 ] ;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
dp [ 2 ] = 2 ;
dp [ 3 ] = 3 ;
for ( int i = 4 ;
i <= n ;
i ++ ) {
dp [ i ] = i ;
for ( int x = 1 ;
x <= ceil ( sqrt ( i ) ) ;
x ++ ) {
int temp = x * x ;
if ( temp > i ) break ;
e... | static int getMinSquares ( int n ) {
if ( n <= 3 ) return n ;
int dp [ ] = new int [ n + 1 ] ;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
dp [ 2 ] = 2 ;
dp [ 3 ] = 3 ;
for ( int i = 4 ;
i <= n ;
i ++ ) {
dp [ i ] = i ;
for ( int x = 1 ;
x <= Math . ceil ( Math . sqrt ( i ) ) ;
x ++ ) {
int te... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getMinSquares ( int n ) {
int * dp = new int [ n + 1 ] ;
dp [ 0 ] = 0 ;
dp [ 1 ] = 1 ;
dp [ 2 ] = 2 ;
dp [ 3 ] = 3 ;
for ( int i = 4 ;
i <= n ;
i ++ ) {
dp [ i ] = i ;
for ( int x = 1 ;
x <= ceil ( sqrt ( i ) ) ;
x ++ ) {
int temp = x * x ;
if ( temp > i ) break ;
... |
cpp-java-SEGREGATE_EVEN_ODD_NUMBERS_SET_3 | cpp-java | void arrayEvenAndOdd ( int arr [ ] , int n ) {
int i = - 1 , j = 0 ;
int t ;
while ( j != n ) {
if ( arr [ j ] % 2 == 0 ) {
i ++ ;
swap ( arr [ i ] , arr [ j ] ) ;
}
j ++ ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << arr [ i ] << " " ;
}
| static void arrayEvenAndOdd ( int arr [ ] , int n ) {
int i = - 1 , j = 0 ;
while ( j != n ) {
if ( arr [ j ] % 2 == 0 ) {
i ++ ;
int temp = arr [ i ] ;
arr [ i ] = arr [ j ] ;
arr [ j ] = temp ;
}
j ++ ;
}
for ( int k = 0 ;
k < n ;
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 arrayEvenAndOdd ( int arr [ ] , int n ) {
int i = - 1 , j = 0 ;
int t ;
while ( j != n ) {
if ( arr [ j ] % 2 == 0 ) {
i ++ ;
swap ( arr [ i ] , arr [ j ] ) ;
}
j ++ ;
}
for ( int i = 0 ;
i < n ;
i ++ ) cout << arr [ i ] << " " ;
}
</Code> |
cpp-java-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS | cpp-java | void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) {
cout << "Diagonal one : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
for ( int j = 0 ;
j < column ;
j ++ ) if ( i == j ) cout << mat [ i ] [ j ] * mat [ i ] [ j ] << " " ;
}
cout << " \n\nDiagonal two : " ;
for ( int i = 0 ;
i ... | static void diagonalsquare ( int mat [ ] [ ] , int row , int column ) {
System . out . print ( "Diagonal one : " ) ;
for ( int i = 0 ;
i < row ;
i ++ ) {
for ( int j = 0 ;
j < column ;
j ++ ) if ( i == j ) System . out . print ( mat [ i ] [ j ] * mat [ i ] [ j ] + " " ) ;
}
System . out . printl... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void diagonalsquare ( int mat [ ] [ MAX ] , int row , int column ) {
cout << "Diagonal one : " ;
for ( int i = 0 ;
i < row ;
i ++ ) {
for ( int j = 0 ;
j < column ;
j ++ ) if ( i == j ) cout << mat [ i ] [ j ] * mat [ i ] [ j ] << " " ;
}
cout << " \n\nDiagonal two : " ;
for ( int i = 0... |
cpp-java-CHECK_SUMS_TH_ROW_TH_COLUMN_MATRIX | cpp-java | bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) {
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum1 = 0 , sum2 = 0 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
sum1 += a [ i ] [ j ] ;
sum2 += a [ j ] [ i ] ;
}
if ( sum1 == sum2 ) return true ;
}
return false... | static boolean areSumSame ( int a [ ] [ ] , int n , int m ) {
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum1 = 0 ;
sum2 = 0 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
sum1 += a [ i ] [ j ] ;
sum2 += a [ j ] [ i ] ;
}
if ( sum1 == sum2 ) return true ;
}
re... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool areSumSame ( int a [ ] [ MAX ] , int n , int m ) {
int sum1 = 0 , sum2 = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum1 = 0 , sum2 = 0 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
sum1 += a [ i ] [ j ] ;
sum2 += a [ j ] [ i ] ;
}
if ( sum1 == sum2 ) return true ;
}
retur... |
cpp-java-GIVEN_SORTED_ARRAY_NUMBER_X_FIND_PAIR_ARRAY_WHOSE_SUM_CLOSEST_X | cpp-java | void printClosest ( int arr [ ] , int n , int x ) {
int res_l , res_r ;
int l = 0 , r = n - 1 , diff = INT_MAX ;
while ( r > l ) {
if ( abs ( arr [ l ] + arr [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( arr [ l ] + arr [ r ] - x ) ;
}
if ( arr [ l ] + arr [ r ] > x ) r... | static void printClosest ( int arr [ ] , int n , int x ) {
int res_l = 0 , res_r = 0 ;
int l = 0 , r = n - 1 , diff = Integer . MAX_VALUE ;
while ( r > l ) {
if ( Math . abs ( arr [ l ] + arr [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = Math . abs ( arr [ l ] + arr [ r ] - x ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printClosest ( int arr [ ] , int n , int x ) {
int res_l , res_r ;
int l = 0 , r = n - 1 , diff = INT_MAX ;
while ( r > l ) {
if ( abs ( arr [ l ] + arr [ r ] - x ) < diff ) {
res_l = l ;
res_r = r ;
diff = abs ( arr [ l ] + arr [ r ] - x ) ;
}
if ( arr [ l ] + arr [ r ] ... |
cpp-java-GIVEN_BINARY_STRING_COUNT_NUMBER_SUBSTRINGS_START_END_1_1 | cpp-java | int countSubStr ( char str [ ] ) {
int m = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
i ++ ) {
if ( str [ i ] == '1' ) m ++ ;
}
return m * ( m - 1 ) / 2 ;
}
| int countSubStr ( char str [ ] , int n ) {
int m = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( str [ i ] == ' 1 ' ) m ++ ;
}
return m * ( m - 1 ) / 2 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSubStr ( char str [ ] ) {
int m = 0 ;
for ( int i = 0 ;
str [ i ] != '\0' ;
i ++ ) {
if ( str [ i ] == '1' ) m ++ ;
}
return m * ( m - 1 ) / 2 ;
}
</Code> |
cpp-java-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS | cpp-java | int countSubstringWithEqualEnds ( string s ) {
int result = 0 ;
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i ;
j < n ;
j ++ ) if ( s [ i ] == s [ j ] ) result ++ ;
return result ;
}
| static int countSubstringWithEqualEnds ( String s ) {
int result = 0 ;
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i ;
j < n ;
j ++ ) if ( s . charAt ( i ) == s . charAt ( j ) ) result ++ ;
return result ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSubstringWithEqualEnds ( string s ) {
int result = 0 ;
int n = s . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i ;
j < n ;
j ++ ) if ( s [ i ] == s [ j ] ) result ++ ;
return result ;
}
</Code> |
cpp-java-CALCULATE_MAXIMUM_VALUE_USING_SIGN_TWO_NUMBERS_STRING | cpp-java | int calcMaxValue ( string str ) {
int res = str [ 0 ] - '0' ;
for ( int i = 1 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ) ;
else res *= ( str [ i ] - '0' ) ;
}
return res ;
}
| static int calcMaxValue ( String str ) {
int res = str . charAt ( 0 ) - ' 0 ' ;
for ( int i = 1 ;
i < str . length ( ) ;
i ++ ) {
if ( str . charAt ( i ) == ' 0 ' || str . charAt ( i ) == ' 1 ' || res < 2 ) res += ( str . charAt ( i ) - ' 0 ' ) ;
else res *= ( str . charAt ( i ) - ' 0 ' ) ;
}
return... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int calcMaxValue ( string str ) {
int res = str [ 0 ] - '0' ;
for ( int i = 1 ;
i < str . length ( ) ;
i ++ ) {
if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ) ;
else res *= ( str [ i ] - '0' ) ;
}
return res ;
}
</Code> |
cpp-java-PROGRAM_BEST_FIT_ALGORITHM_MEMORY_MANAGEMENT | cpp-java | void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ n ] ;
memset ( allocation , - 1 , sizeof ( allocation ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int bestIdx = - 1 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >= processSize [ i ] ) {
... | static void bestFit ( 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 ++ ) {
int bestIdx = - 1 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void bestFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ n ] ;
memset ( allocation , - 1 , sizeof ( allocation ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int bestIdx = - 1 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >= processSize [ i... |
cpp-java-FREQUENCY_ELEMENT_UNSORTED_ARRAY_1 | cpp-java | void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
int cumul = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
cumul += hm [ a [ i ] ] ;
if ( hm [ a [ i ] ] ) {
cout << a [ i ] << "->" << cumul << endl ;
}
hm [ a [ i ... | static void countFreq ( int a [ ] , int n ) {
int hm [ ] = new int [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
int cumul = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
cumul += hm [ a [ i ] ] ;
if ( hm [ a [ i ] ] != 0 ) {
System . out . println ( a [ i ] + "->" + cumul ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
int cumul = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
cumul += hm [ a [ i ] ] ;
if ( hm [ a [ i ] ] ) {
cout << a [ i ] << "->" << cumul << endl ;
}
hm [... |
cpp-java-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8 | cpp-java | bool isSubSeqDivisible ( string str ) {
for ( int i = 0 ;
i < l ;
i ++ ) {
for ( int j = i ;
j < l ;
j ++ ) {
for ( int k = j ;
k < l ;
k ++ ) {
if ( arr [ i ] % 8 == 0 ) return true ;
else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ;
... | static boolean isSubSeqDivisible ( String str ) {
int i , j , k , l = str . length ( ) ;
int arr [ ] = new int [ l ] ;
for ( i = 0 ;
i < l ;
i ++ ) {
for ( j = i ;
j < l ;
j ++ ) {
for ( k = j ;
k < l ;
k ++ ) {
if ( arr [ i ] % 8 == 0 ) return true ;
else if ( ( ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isSubSeqDivisible ( string str ) {
for ( int i = 0 ;
i < l ;
i ++ ) {
for ( int j = i ;
j < l ;
j ++ ) {
for ( int k = j ;
k < l ;
k ++ ) {
if ( arr [ i ] % 8 == 0 ) return true ;
else if ( ( arr [ i ] * 10 + arr [ j ] ) % 8 == 0 && i != j ) return true ;
... |
cpp-java-COMPUTE_THE_INTEGER_ABSOLUTE_VALUE_ABS_WITHOUT_BRANCHING | cpp-java | unsigned int getAbs ( int n ) {
int const mask = n >> ( sizeof ( int ) * CHARBIT - 1 ) ;
return ( ( n + mask ) ^ mask ) ;
}
| static int getAbs ( int n ) {
int mask = n >> ( SIZE_INT * CHAR_BIT - 1 ) ;
return ( ( n + mask ) ^ mask ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
unsigned int getAbs ( int n ) {
int const mask = n >> ( sizeof ( int ) * CHARBIT - 1 ) ;
return ( ( n + mask ) ^ mask ) ;
}
</Code> |
cpp-java-COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY | cpp-java | int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i ] * arr [ j ] ;
for ( int k = 0 ;
k < n ;
k ++ ) {
if ( arr [ k ] == product ) {
result ++ ;
b... | static int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i ] * arr [ j ] ;
for ( int k = 0 ;
k < n ;
k ++ ) {
if ( arr [ k ] == product ) {
result ++ ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countPairs ( int arr [ ] , int n ) {
int result = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j ++ ) {
int product = arr [ i ] * arr [ j ] ;
for ( int k = 0 ;
k < n ;
k ++ ) {
if ( arr [ k ] == product ) {
result ++ ;
... |
cpp-java-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM_1 | cpp-java | int minCost ( int a [ ] , int n , int k ) {
int dp [ n + 1 ] [ k + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= k ;
j ++ ) dp [ i ] [ j ] = inf ;
dp [ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= k ;
j ++ ) for ( int m = i - 1 ;
m >= 0 ;
m -- ) d... | static int minCost ( int a [ ] , int n , int k ) {
int dp [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= k ;
j ++ ) dp [ i ] [ j ] = inf ;
dp [ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= k ;
j ++ ) for ( int m = i - ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minCost ( int a [ ] , int n , int k ) {
int dp [ n + 1 ] [ k + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= k ;
j ++ ) dp [ i ] [ j ] = inf ;
dp [ 0 ] [ 0 ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= k ;
j ++ ) for ( int m = i - 1 ;
m >= 0 ;
m... |
cpp-java-CHECK_GRAPHS_CYCLE_ODD_LENGTH | cpp-java | bool containsOdd ( int G [ ] [ V ] , int src ) {
int colorArr [ V ] ;
for ( int i = 0 ;
i < V ;
++ i ) colorArr [ i ] = - 1 ;
colorArr [ src ] = 1 ;
queue < int > q ;
q . push ( src ) ;
while ( ! q . empty ( ) ) {
int u = q . front ( ) ;
q . pop ( ) ;
if ( G [ u ] [ u ] == 1 ) return true ;
... | public static boolean containsOdd ( int G [ ] [ ] , int src ) {
int colorArr [ ] = new int [ V ] ;
for ( int i = 0 ;
i < V ;
++ i ) colorArr [ i ] = - 1 ;
colorArr [ src ] = 1 ;
LinkedList < Integer > q = new LinkedList < Integer > ( ) ;
q . add ( src ) ;
while ( ! q . isEmpty ( ) ) {
int u = q . pe... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool containsOdd ( int G [ ] [ V ] , int src ) {
int colorArr [ V ] ;
for ( int i = 0 ;
i < V ;
++ i ) colorArr [ i ] = - 1 ;
colorArr [ src ] = 1 ;
queue < int > q ;
q . push ( src ) ;
while ( ! q . empty ( ) ) {
int u = q . front ( ) ;
q . pop ( ) ;
if ( G [ u ] [ u ] == 1 ) return ... |
cpp-java-COUNT_SET_BITS_IN_AN_INTEGER_3 | cpp-java | int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return 1 + countSetBits ( n & ( n - 1 ) ) ;
}
| public static int countSetBits ( int n ) {
if ( n == 0 ) return 0 ;
else return 1 + countSetBits ( n & ( 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 1 + countSetBits ( n & ( n - 1 ) ) ;
}
</Code> |
cpp-java-CHECK_STRING_FOLLOWS_ANBN_PATTERN_NOT | cpp-java | bool isAnBn ( string str ) {
int n = str . length ( ) ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) if ( str [ i ] != 'a' ) break ;
if ( i * 2 != n ) return false ;
int j ;
for ( j = i ;
j < n ;
j ++ ) if ( str [ j ] != 'b' ) return false ;
return true ;
}
| public static boolean isAnBn ( String s ) {
int l = s . length ( ) ;
if ( l % 2 == 1 ) {
return false ;
}
int i = 0 ;
int j = l - 1 ;
while ( i < j ) {
if ( s . charAt ( i ) != ' a ' || s . charAt ( j ) != ' b ' ) {
return false ;
}
i ++ ;
j -- ;
}
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isAnBn ( string str ) {
int n = str . length ( ) ;
int i ;
for ( i = 0 ;
i < n ;
i ++ ) if ( str [ i ] != 'a' ) break ;
if ( i * 2 != n ) return false ;
int j ;
for ( j = i ;
j < n ;
j ++ ) if ( str [ j ] != 'b' ) return false ;
return true ;
}
</Code> |
cpp-java-MINIMUM_NUMBER_OF_SQUARES_WHOSE_SUM_EQUALS_TO_GIVEN_NUMBER_N | cpp-java | int getMinSquares ( unsigned int n ) {
if ( sqrt ( n ) - floor ( sqrt ( n ) ) == 0 ) return 1 ;
if ( n <= 3 ) return n ;
int res = n ;
for ( int x = 1 ;
x <= n ;
x ++ ) {
int temp = x * x ;
if ( temp > n ) break ;
else res = min ( res , 1 + getMinSquares ( n - temp ) ) ;
}
return res ;
}
| static int getMinSquares ( int n ) {
if ( n <= 3 ) return n ;
int res = n ;
for ( int x = 1 ;
x <= n ;
x ++ ) {
int temp = x * x ;
if ( temp > n ) break ;
else res = Math . min ( res , 1 + getMinSquares ( n - temp ) ) ;
}
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getMinSquares ( unsigned int n ) {
if ( sqrt ( n ) - floor ( sqrt ( n ) ) == 0 ) return 1 ;
if ( n <= 3 ) return n ;
int res = n ;
for ( int x = 1 ;
x <= n ;
x ++ ) {
int temp = x * x ;
if ( temp > n ) break ;
else res = min ( res , 1 + getMinSquares ( n - temp ) ) ;
}
return res ... |
cpp-java-MAXIMUM_DISTANCE_TWO_OCCURRENCES_ELEMENT_ARRAY | cpp-java | int maxDistance ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
int max_dist = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ;
else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ;
}
return max_dist ;
}
| static int maxDistance ( int [ ] arr , int n ) {
HashMap < Integer , Integer > map = new HashMap < > ( ) ;
int max_dist = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ! map . containsKey ( arr [ i ] ) ) map . put ( arr [ i ] , i ) ;
else max_dist = Math . max ( max_dist , i - map . get ( arr [ i ] ) ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxDistance ( int arr [ ] , int n ) {
unordered_map < int , int > mp ;
int max_dist = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( mp . find ( arr [ i ] ) == mp . end ( ) ) mp [ arr [ i ] ] = i ;
else max_dist = max ( max_dist , i - mp [ arr [ i ] ] ) ;
}
return max_dist ;
}
</Code> |
cpp-java-MAXIMUM_SUBSEQUENCE_SUM_SUCH_THAT_NO_THREE_ARE_CONSECUTIVE | cpp-java | int maxSumWO3Consec ( int arr [ ] , int n ) {
int sum [ n ] ;
if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ;
if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ;
if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ;
for ( int i = 3 ;
i < n ;
i ++ ) sum [ i ] = max ( max ... | static int maxSumWO3Consec ( int arr [ ] , int n ) {
int sum [ ] = new int [ n ] ;
if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ;
if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ;
if ( n > 2 ) sum [ 2 ] = Math . max ( sum [ 1 ] , Math . max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ;
for ( int i = 3 ;
i < ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSumWO3Consec ( int arr [ ] , int n ) {
int sum [ n ] ;
if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ;
if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ;
if ( n > 2 ) sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ;
for ( int i = 3 ;
i < n ;
i ++ ) sum [ i ] = max... |
cpp-java-EULERS_FOUR_SQUARE_IDENTITY_1 | cpp-java | void checkEulerFourSquareIdentity ( int a , int b ) {
int ab = a * b ;
bool flag = false ;
int i = 0 ;
while ( i * i <= ab ) {
int j = i ;
while ( i * i + j * j <= ab ) {
int k = j ;
while ( i * i + j * j + k * k <= ab ) {
double l = sqrt ( ab - ( i * i + j * j + k * k ) ) ;
... | public static void checkEulerFourSquareIdentity ( int a , int b ) {
int ab = a * b ;
boolean flag = false ;
int i = 0 ;
while ( i * i <= ab ) {
int j = i ;
while ( i * i + j * j <= ab ) {
int k = j ;
while ( i * i + j * j + k * k <= ab ) {
double l = Math . sqrt ( ab - ( i * i + j * ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void checkEulerFourSquareIdentity ( int a , int b ) {
int ab = a * b ;
bool flag = false ;
int i = 0 ;
while ( i * i <= ab ) {
int j = i ;
while ( i * i + j * j <= ab ) {
int k = j ;
while ( i * i + j * j + k * k <= ab ) {
double l = sqrt ( ab - ( i * i + j * j + k * k ) ) ;
... |
cpp-java-NOBLE_INTEGERS_IN_AN_ARRAY_COUNT_OF_GREATER_ELEMENTS_IS_EQUAL_TO_VALUE_1 | cpp-java | int nobleInteger ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] == arr [ i + 1 ] ) continue ;
if ( arr [ i ] == n - i - 1 ) return arr [ i ] ;
}
if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ;
return - 1 ;
}
| public static int nobleInteger ( int arr [ ] ) {
Arrays . sort ( arr ) ;
int n = arr . length ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] == arr [ i + 1 ] ) continue ;
if ( arr [ i ] == n - i - 1 ) return arr [ i ] ;
}
if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ;
return - 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int nobleInteger ( int arr [ ] , int n ) {
sort ( arr , arr + n ) ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
if ( arr [ i ] == arr [ i + 1 ] ) continue ;
if ( arr [ i ] == n - i - 1 ) return arr [ i ] ;
}
if ( arr [ n - 1 ] == 0 ) return arr [ n - 1 ] ;
return - 1 ;
}
</Code> |
cpp-java-COUNT_NUMBER_ISLANDS_EVERY_ISLAND_SEPARATED_LINE | cpp-java | int countIslands ( int mat [ ] [ N ] ) {
int count = 0 ;
for ( int i = 0 ;
i < M ;
i ++ ) {
for ( int j = 0 ;
j < N ;
j ++ ) {
if ( mat [ i ] [ j ] == 'X' ) {
if ( ( i == 0 || mat [ i - 1 ] [ j ] == 'O' ) && ( j == 0 || mat [ i ] [ j - 1 ] == 'O' ) ) count ++ ;
}
}
}
retu... | static int countIslands ( int mat [ ] [ ] , int m , int n ) {
int count = 0 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( mat [ i ] [ j ] == ' X ' ) {
if ( ( i == 0 || mat [ i - 1 ] [ j ] == ' O ' ) && ( j == 0 || mat [ i ] [ j - 1 ] == ' O ' ) ) count ++... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countIslands ( int mat [ ] [ N ] ) {
int count = 0 ;
for ( int i = 0 ;
i < M ;
i ++ ) {
for ( int j = 0 ;
j < N ;
j ++ ) {
if ( mat [ i ] [ j ] == 'X' ) {
if ( ( i == 0 || mat [ i - 1 ] [ j ] == 'O' ) && ( j == 0 || mat [ i ] [ j - 1 ] == 'O' ) ) count ++ ;
}
}
}... |
cpp-java-SELECT_A_RANDOM_NUMBER_FROM_STREAM_WITH_O1_SPACE | cpp-java | int selectRandom ( int x ) {
static int res ;
static int count = 0 ;
count ++ ;
if ( count == 1 ) res = x ;
else {
int i = rand ( ) % count ;
if ( i == count - 1 ) res = x ;
}
return res ;
}
| static int selectRandom ( int x ) {
count ++ ;
if ( count == 1 ) res = x ;
else {
Random r = new Random ( ) ;
int i = r . nextInt ( count ) ;
if ( i == count - 1 ) res = x ;
}
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int selectRandom ( int x ) {
static int res ;
static int count = 0 ;
count ++ ;
if ( count == 1 ) res = x ;
else {
int i = rand ( ) % count ;
if ( i == count - 1 ) res = x ;
}
return res ;
}
</Code> |
cpp-java-A_PRODUCT_ARRAY_PUZZLE_1 | cpp-java | void productArray ( int arr [ ] , int n ) {
if ( n == 1 ) {
cout << 0 ;
return ;
}
int i , temp = 1 ;
int * prod = new int [ ( sizeof ( int ) * n ) ] ;
memset ( prod , 1 , n ) ;
for ( i = 0 ;
i < n ;
i ++ ) {
prod [ i ] = temp ;
temp *= arr [ i ] ;
}
temp = 1 ;
for ( i = n - 1 ;
... | void productArray ( int arr [ ] , int n ) {
if ( n == 1 ) {
System . out . print ( "0" ) ;
return ;
}
int i , temp = 1 ;
int prod [ ] = new int [ n ] ;
for ( int j = 0 ;
j < n ;
j ++ ) prod [ j ] = 1 ;
for ( i = 0 ;
i < n ;
i ++ ) {
prod [ i ] = temp ;
temp *= arr [ i ] ;
}
temp ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void productArray ( int arr [ ] , int n ) {
if ( n == 1 ) {
cout << 0 ;
return ;
}
int i , temp = 1 ;
int * prod = new int [ ( sizeof ( int ) * n ) ] ;
memset ( prod , 1 , n ) ;
for ( i = 0 ;
i < n ;
i ++ ) {
prod [ i ] = temp ;
temp *= arr [ i ] ;
}
temp = 1 ;
for ( i = n -... |
cpp-java-SORT_1_N_SWAPPING_ADJACENT_ELEMENTS | cpp-java | bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) {
if ( B [ i ] ) {
j = i ;
while ( B [ j ] ) j ++ ;
sort ( A + i , A + 1 + j ) ;
i = j ;
}
}
for ( i = 0 ;
i < n ;
i ++ ) {
if ( A [ i ] != i + 1 ) return false ;
... | static boolean sortedAfterSwap ( int A [ ] , boolean B [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) {
if ( B [ i ] ) {
j = i ;
while ( B [ j ] ) {
j ++ ;
}
Arrays . sort ( A , i , 1 + j ) ;
i = j ;
}
}
for ( i = 0 ;
i < n ;
i ++ ) {
if ( ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool sortedAfterSwap ( int A [ ] , bool B [ ] , int n ) {
int i , j ;
for ( i = 0 ;
i < n - 1 ;
i ++ ) {
if ( B [ i ] ) {
j = i ;
while ( B [ j ] ) j ++ ;
sort ( A + i , A + 1 + j ) ;
i = j ;
}
}
for ( i = 0 ;
i < n ;
i ++ ) {
if ( A [ i ] != i + 1 ) return fal... |
cpp-java-FIND_THE_MAXIMUM_SUBARRAY_XOR_IN_A_GIVEN_ARRAY | cpp-java | int maxSubarrayXOR ( int arr [ ] , int n ) {
int ans = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int curr_xor = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
curr_xor = curr_xor ^ arr [ j ] ;
ans = max ( ans , curr_xor ) ;
}
}
return ans ;
}
| static int maxSubarrayXOR ( int arr [ ] , int n ) {
int ans = Integer . MIN_VALUE ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int curr_xor = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
curr_xor = curr_xor ^ arr [ j ] ;
ans = Math . max ( ans , curr_xor ) ;
}
}
return ans ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSubarrayXOR ( int arr [ ] , int n ) {
int ans = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int curr_xor = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
curr_xor = curr_xor ^ arr [ j ] ;
ans = max ( ans , curr_xor ) ;
}
}
return ans ;
}
</Code> |
cpp-java-MINIMUM_COST_MAKE_ARRAY_SIZE_1_REMOVING_LARGER_PAIRS | cpp-java | int cost ( int a [ ] , int n ) {
return ( n - 1 ) * ( * min_element ( a , a + n ) ) ;
}
| static int cost ( int [ ] a , int n ) {
int min = a [ 0 ] ;
for ( int i = 1 ;
i < a . length ;
i ++ ) {
if ( a [ i ] < min ) min = a [ i ] ;
}
return ( n - 1 ) * min ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int cost ( int a [ ] , int n ) {
return ( n - 1 ) * ( * min_element ( a , a + n ) ) ;
}
</Code> |
cpp-java-REMOVE_MINIMUM_ELEMENTS_EITHER_SIDE_2MIN_MAX | cpp-java | int minRemovalsDP ( int arr [ ] , int n ) {
int longest_start = - 1 , longest_end = 0 ;
for ( int start = 0 ;
start < n ;
start ++ ) {
int min = INT_MAX , max = INT_MIN ;
for ( int end = start ;
end < n ;
end ++ ) {
int val = arr [ end ] ;
if ( val < min ) min = val ;
if ( val ... | static int minRemovalsDP ( int arr [ ] , int n ) {
int longest_start = - 1 , longest_end = 0 ;
for ( int start = 0 ;
start < n ;
start ++ ) {
int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ;
for ( int end = start ;
end < n ;
end ++ ) {
int val = arr [ end ] ;
if ( val < mi... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minRemovalsDP ( int arr [ ] , int n ) {
int longest_start = - 1 , longest_end = 0 ;
for ( int start = 0 ;
start < n ;
start ++ ) {
int min = INT_MAX , max = INT_MIN ;
for ( int end = start ;
end < n ;
end ++ ) {
int val = arr [ end ] ;
if ( val < min ) min = val ;
if... |
cpp-java-COUNT_INVERSIONS_OF_SIZE_THREE_IN_A_GIVE_ARRAY_1 | cpp-java | int getInvCount ( int arr [ ] , int n ) {
int invcount = 0 ;
for ( int i = 1 ;
i < n - 1 ;
i ++ ) {
int small = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ;
int great = 0 ;
for ( int j = i - 1 ;
j >= 0 ;
j -- ) if ( arr [ i ] < arr [ j ] ) grea... | int getInvCount ( int arr [ ] , int n ) {
int invcount = 0 ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) {
int small = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ;
int great = 0 ;
for ( int j = i - 1 ;
j >= 0 ;
j -- ) if ( arr [ i ] < arr [ j ] ) grea... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getInvCount ( int arr [ ] , int n ) {
int invcount = 0 ;
for ( int i = 1 ;
i < n - 1 ;
i ++ ) {
int small = 0 ;
for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ;
int great = 0 ;
for ( int j = i - 1 ;
j >= 0 ;
j -- ) if ( arr [ i ] < arr [ j ]... |
cpp-java-PROGRAM_FOR_SURFACE_AREA_OF_OCTAHEDRON | cpp-java | double surface_area_octahedron ( double side ) {
return ( 2 * ( sqrt ( 3 ) ) * ( side * side ) ) ;
}
| static double surface_area_octahedron ( double side ) {
return ( 2 * ( Math . sqrt ( 3 ) ) * ( side * side ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
double surface_area_octahedron ( double side ) {
return ( 2 * ( sqrt ( 3 ) ) * ( side * side ) ) ;
}
</Code> |
cpp-java-GNOME_SORT_A_STUPID_ONE | cpp-java | void gnomeSort ( int arr [ ] , int n ) {
int index = 0 ;
while ( index < n ) {
if ( index == 0 ) index ++ ;
if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ;
else {
swap ( arr [ index ] , arr [ index - 1 ] ) ;
index -- ;
}
}
return ;
}
| static void gnomeSort ( int arr [ ] , int n ) {
int index = 0 ;
while ( index < n ) {
if ( index == 0 ) index ++ ;
if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ;
else {
int temp = 0 ;
temp = arr [ index ] ;
arr [ index ] = arr [ index - 1 ] ;
arr [ index - 1 ] = temp ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void gnomeSort ( int arr [ ] , int n ) {
int index = 0 ;
while ( index < n ) {
if ( index == 0 ) index ++ ;
if ( arr [ index ] >= arr [ index - 1 ] ) index ++ ;
else {
swap ( arr [ index ] , arr [ index - 1 ] ) ;
index -- ;
}
}
return ;
}
</Code> |
cpp-java-COUNT_FREQUENCY_K_MATRIX_SIZE_N_MATRIXI_J_IJ | cpp-java | int find ( int n , int k ) {
if ( n + 1 >= k ) return ( k - 1 ) ;
else return ( 2 * n + 1 - k ) ;
}
| public static int find ( int n , int k ) {
if ( n + 1 >= k ) return ( k - 1 ) ;
else return ( 2 * n + 1 - k ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int find ( int n , int k ) {
if ( n + 1 >= k ) return ( k - 1 ) ;
else return ( 2 * n + 1 - k ) ;
}
</Code> |
cpp-java-UNIQUE_CELLS_BINARY_MATRIX | cpp-java | int countUnique ( int mat [ ] [ MAX ] , int n , int m ) {
int rowsum [ n ] , colsum [ m ] ;
memset ( colsum , 0 , sizeof ( colsum ) ) ;
memset ( rowsum , 0 , sizeof ( rowsum ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) if ( mat [ i ] [ j ] ) {
rowsum [ i ] ++ ;
colsum ... | static int countUnique ( int mat [ ] [ ] , int n , int m ) {
int [ ] rowsum = new int [ n ] ;
int [ ] colsum = new int [ m ] ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) if ( mat [ i ] [ j ] != 0 ) {
rowsum [ i ] ++ ;
colsum [ j ] ++ ;
}
int uniquecount = 0 ;
for ( int... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countUnique ( int mat [ ] [ MAX ] , int n , int m ) {
int rowsum [ n ] , colsum [ m ] ;
memset ( colsum , 0 , sizeof ( colsum ) ) ;
memset ( rowsum , 0 , sizeof ( rowsum ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < m ;
j ++ ) if ( mat [ i ] [ j ] ) {
rowsum [ i ] ++ ;
... |
cpp-java-GENERATE_BINARY_PERMUTATIONS_1S_0S_EVERY_POINT_PERMUTATIONS | cpp-java | void generate ( int ones , int zeroes , string str , int len ) {
if ( len == str . length ( ) ) {
cout << str << " " ;
return ;
}
generate ( ones + 1 , zeroes , str + "1" , len ) ;
if ( ones > zeroes ) generate ( ones , zeroes + 1 , str + "0" , len ) ;
}
| static void generate ( int ones , int zeroes , String str , int len ) {
if ( len == str . length ( ) ) {
System . out . print ( str + " " ) ;
return ;
}
generate ( ones + 1 , zeroes , str + "1" , len ) ;
if ( ones > zeroes ) {
generate ( ones , zeroes + 1 , str + "0" , len ) ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void generate ( int ones , int zeroes , string str , int len ) {
if ( len == str . length ( ) ) {
cout << str << " " ;
return ;
}
generate ( ones + 1 , zeroes , str + "1" , len ) ;
if ( ones > zeroes ) generate ( ones , zeroes + 1 , str + "0" , len ) ;
}
</Code> |
cpp-java-MID_POINT_CIRCLE_DRAWING_ALGORITHM | cpp-java | void midPointCircleDraw ( int x_centre , int y_centre , int r ) {
int x = r , y = 0 ;
cout << "(" << x + x_centre << ", " << y + y_centre << ") " ;
if ( r > 0 ) {
cout << "(" << x + x_centre << ", " << - y + y_centre << ") " ;
cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;
cout << "(" <... | static void midPointCircleDraw ( int x_centre , int y_centre , int r ) {
int x = r , y = 0 ;
System . out . print ( "(" + ( x + x_centre ) + ", " + ( y + y_centre ) + ")" ) ;
if ( r > 0 ) {
System . out . print ( "(" + ( x + x_centre ) + ", " + ( - y + y_centre ) + ")" ) ;
System . out . print ( "(" + ( y... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void midPointCircleDraw ( int x_centre , int y_centre , int r ) {
int x = r , y = 0 ;
cout << "(" << x + x_centre << ", " << y + y_centre << ") " ;
if ( r > 0 ) {
cout << "(" << x + x_centre << ", " << - y + y_centre << ") " ;
cout << "(" << y + x_centre << ", " << x + y_centre << ") " ;
cout <... |
cpp-java-FIND_FACTORIAL_NUMBERS_LESS_EQUAL_N | cpp-java | void printFactorialNums ( int n ) {
int fact = 1 ;
int x = 2 ;
while ( fact <= n ) {
cout << fact << " " ;
fact = fact * x ;
x ++ ;
}
}
| static void printFactorialNums ( int n ) {
int fact = 1 ;
int x = 2 ;
while ( fact <= n ) {
System . out . print ( fact + " " ) ;
fact = fact * x ;
x ++ ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printFactorialNums ( int n ) {
int fact = 1 ;
int x = 2 ;
while ( fact <= n ) {
cout << fact << " " ;
fact = fact * x ;
x ++ ;
}
}
</Code> |
cpp-java-FRIENDS_PAIRING_PROBLEM | cpp-java | int countFriendsPairings ( int n ) {
int dp [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
if ( i <= 2 ) dp [ i ] = i ;
else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ;
}
return dp [ n ] ;
}
| static int countFriendsPairings ( int n ) {
int dp [ ] = new int [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
if ( i <= 2 ) dp [ i ] = i ;
else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ;
}
return dp [ n ] ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countFriendsPairings ( int n ) {
int dp [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
if ( i <= 2 ) dp [ i ] = i ;
else dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ;
}
return dp [ n ] ;
}
</Code> |
cpp-java-PROGRAM_FIND_REMAINDER_LARGE_NUMBER_DIVIDED_11 | cpp-java | int remainder ( string str ) {
int len = str . length ( ) ;
int num , rem = 0 ;
for ( int i = 0 ;
i < len ;
i ++ ) {
num = rem * 10 + ( str [ i ] - '0' ) ;
rem = num % 11 ;
}
return rem ;
}
| static int remainder ( String str ) {
int len = str . length ( ) ;
int num , rem = 0 ;
for ( int i = 0 ;
i < len ;
i ++ ) {
num = rem * 10 + ( str . charAt ( i ) - ' 0 ' ) ;
rem = num % 11 ;
}
return rem ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int remainder ( string str ) {
int len = str . length ( ) ;
int num , rem = 0 ;
for ( int i = 0 ;
i < len ;
i ++ ) {
num = rem * 10 + ( str [ i ] - '0' ) ;
rem = num % 11 ;
}
return rem ;
}
</Code> |
cpp-java-N_TH_NUMBER_WHOSE_SUM_OF_DIGITS_IS_TEN | cpp-java | int findNth ( int n ) {
int count = 0 ;
for ( int curr = 1 ;
;
curr ++ ) {
int sum = 0 ;
for ( int x = curr ;
x > 0 ;
x = x / 10 ) sum = sum + x % 10 ;
if ( sum == 10 ) count ++ ;
if ( count == n ) return curr ;
}
return - 1 ;
}
| public static int findNth ( int n ) {
int count = 0 ;
for ( int curr = 1 ;
;
curr ++ ) {
int sum = 0 ;
for ( int x = curr ;
x > 0 ;
x = x / 10 ) sum = sum + x % 10 ;
if ( sum == 10 ) count ++ ;
if ( count == n ) return curr ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findNth ( int n ) {
int count = 0 ;
for ( int curr = 1 ;
;
curr ++ ) {
int sum = 0 ;
for ( int x = curr ;
x > 0 ;
x = x / 10 ) sum = sum + x % 10 ;
if ( sum == 10 ) count ++ ;
if ( count == n ) return curr ;
}
return - 1 ;
}
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.