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-FAST_MULTIPLICATION_METHOD_WITHOUT_USING_MULTIPLICATION_OPERATOR_RUSSIAN_PEASANTS_ALGORITHM | cpp-java | unsigned int russianPeasant ( unsigned int a , unsigned int b ) {
int res = 0 ;
while ( b > 0 ) {
if ( b & 1 ) res = res + a ;
a = a << 1 ;
b = b >> 1 ;
}
return res ;
}
| static int russianPeasant ( int a , int b ) {
int res = 0 ;
while ( b > 0 ) {
if ( ( b & 1 ) != 0 ) res = res + a ;
a = a << 1 ;
b = b >> 1 ;
}
return res ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
unsigned int russianPeasant ( unsigned int a , unsigned int b ) {
int res = 0 ;
while ( b > 0 ) {
if ( b & 1 ) res = res + a ;
a = a << 1 ;
b = b >> 1 ;
}
return res ;
}
</Code> |
cpp-java-CAESAR_CIPHER | cpp-java | string encrypt ( string text , int s ) {
string result = "" ;
for ( int i = 0 ;
i < text . length ( ) ;
i ++ ) {
if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ) ;
else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ) ;
}
return result ;
}
| public static StringBuffer encrypt ( String text , int s ) {
StringBuffer result = new StringBuffer ( ) ;
for ( int i = 0 ;
i < text . length ( ) ;
i ++ ) {
if ( Character . isUpperCase ( text . charAt ( i ) ) ) {
char ch = ( char ) ( ( ( int ) text . charAt ( i ) + s - 65 ) % 26 + 65 ) ;
result... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string encrypt ( string text , int s ) {
string result = "" ;
for ( int i = 0 ;
i < text . length ( ) ;
i ++ ) {
if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ) ;
else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ) ;
}
return result ;
}
</Code... |
cpp-java-PROGRAM_DISTANCE_TWO_POINTS_EARTH | cpp-java | long double distance ( long double lat1 , long double long1 , long double lat2 , long double long2 ) {
lat1 = toRadians ( lat1 ) ;
long1 = toRadians ( long1 ) ;
lat2 = toRadians ( lat2 ) ;
long2 = toRadians ( long2 ) ;
long double dlong = long2 - long1 ;
long double dlat = lat2 - lat1 ;
long double ans = ... | public static double distance ( double lat1 , double lat2 , double lon1 , double lon2 ) {
lon1 = Math . toRadians ( lon1 ) ;
lon2 = Math . toRadians ( lon2 ) ;
lat1 = Math . toRadians ( lat1 ) ;
lat2 = Math . toRadians ( lat2 ) ;
double dlon = lon2 - lon1 ;
double dlat = lat2 - lat1 ;
double a = Math . po... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long double distance ( long double lat1 , long double long1 , long double lat2 , long double long2 ) {
lat1 = toRadians ( lat1 ) ;
long1 = toRadians ( long1 ) ;
lat2 = toRadians ( lat2 ) ;
long2 = toRadians ( long2 ) ;
long double dlong = long2 - long1 ;
long double dlat = lat2 - lat1 ;
long double... |
cpp-java-NTH_EVEN_FIBONACCI_NUMBER | cpp-java | long int evenFib ( int n ) {
if ( n < 1 ) return n ;
if ( n == 1 ) return 2 ;
return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ;
}
| static long evenFib ( int n ) {
if ( n < 1 ) return n ;
if ( n == 1 ) return 2 ;
return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long int evenFib ( int n ) {
if ( n < 1 ) return n ;
if ( n == 1 ) return 2 ;
return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ;
}
</Code> |
cpp-java-STEINS_ALGORITHM_FOR_FINDING_GCD | cpp-java | int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
if ( b == 0 ) return a ;
int k ;
for ( k = 0 ;
( ( a | static int gcd ( int a , int b ) {
if ( a == 0 ) return b ;
if ( b == 0 ) return a ;
int k ;
for ( k = 0 ;
( ( 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 ;
if ( b == 0 ) return a ;
int k ;
for ( k = 0 ;
( ( a
</Code> |
cpp-java-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO | cpp-java | bool isPowerOfTwo ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 2 != 0 ) return 0 ;
n = n / 2 ;
}
return 1 ;
}
| static boolean isPowerOfTwo ( int n ) {
if ( n == 0 ) return false ;
while ( n != 1 ) {
if ( n % 2 != 0 ) return false ;
n = n / 2 ;
}
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPowerOfTwo ( int n ) {
if ( n == 0 ) return 0 ;
while ( n != 1 ) {
if ( n % 2 != 0 ) return 0 ;
n = n / 2 ;
}
return 1 ;
}
</Code> |
cpp-java-SUM_PAIRWISE_PRODUCTS | cpp-java | long long int findSum ( int n ) {
long long int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= n ;
j ++ ) sum = sum + i * j ;
return sum ;
}
| static int findSum ( int n ) {
int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= n ;
j ++ ) sum = sum + i * j ;
return sum ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long int findSum ( int n ) {
long long int sum = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = i ;
j <= n ;
j ++ ) sum = sum + i * j ;
return sum ;
}
</Code> |
cpp-java-SUM_BINOMIAL_COEFFICIENTS_1 | cpp-java | int binomialCoeffSum ( int n ) {
return ( 1 << n ) ;
}
| static int binomialCoeffSum ( int n ) {
return ( 1 << n ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int binomialCoeffSum ( int n ) {
return ( 1 << n ) ;
}
</Code> |
cpp-java-EVALUATE_AN_ARRAY_EXPRESSION_WITH_NUMBERS_AND | cpp-java | int calculateSum ( string arr [ ] , int n ) {
if ( n == 0 ) return 0 ;
string s = arr [ 0 ] ;
int value = stoi ( s ) ;
int sum = value ;
for ( int i = 2 ;
i < n ;
i = i + 2 ) {
s = arr [ i ] ;
int value = stoi ( s ) ;
char operation = arr [ i - 1 ] [ 0 ] ;
if ( operation == '+' ) sum += va... | public static int calculateSum ( String arr [ ] , int n ) {
if ( n == 0 ) return 0 ;
String s = arr [ 0 ] ;
int value = Integer . parseInt ( s ) ;
int sum = value ;
for ( int i = 2 ;
i < n ;
i = i + 2 ) {
s = arr [ i ] ;
value = Integer . parseInt ( s ) ;
char operation = arr [ i - 1 ] . charA... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int calculateSum ( string arr [ ] , int n ) {
if ( n == 0 ) return 0 ;
string s = arr [ 0 ] ;
int value = stoi ( s ) ;
int sum = value ;
for ( int i = 2 ;
i < n ;
i = i + 2 ) {
s = arr [ i ] ;
int value = stoi ( s ) ;
char operation = arr [ i - 1 ] [ 0 ] ;
if ( operation == '+' ) su... |
cpp-java-CHECK_WHETHER_POINT_EXISTS_CIRCLE_SECTOR_NOT | cpp-java | void checkPoint ( int radius , int x , int y , float percent , float startAngle ) {
float endAngle = 360 / percent + startAngle ;
float polarradius = sqrt ( x * x + y * y ) ;
float Angle = atan ( y / x ) ;
if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( "Point (%d, %d) exist in ... | static void checkPoint ( int radius , int x , int y , float percent , float startAngle ) {
float endAngle = 360 / percent + startAngle ;
double polarradius = Math . sqrt ( x * x + y * y ) ;
double Angle = Math . atan ( y / x ) ;
if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) System . ou... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void checkPoint ( int radius , int x , int y , float percent , float startAngle ) {
float endAngle = 360 / percent + startAngle ;
float polarradius = sqrt ( x * x + y * y ) ;
float Angle = atan ( y / x ) ;
if ( Angle >= startAngle && Angle <= endAngle && polarradius < radius ) printf ( "Point (%d, %d) ex... |
cpp-java-PRINTING_ITEMS_01_KNAPSACK | cpp-java | void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) {
int i , w ;
int K [ n + 1 ] [ W + 1 ] ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( w = 0 ;
w <= W ;
w ++ ) {
if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ;
else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ] + K [ ... | static void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) {
int i , w ;
int K [ ] [ ] = new int [ n + 1 ] [ W + 1 ] ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( w = 0 ;
w <= W ;
w ++ ) {
if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ;
else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = M... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printknapSack ( int W , int wt [ ] , int val [ ] , int n ) {
int i , w ;
int K [ n + 1 ] [ W + 1 ] ;
for ( i = 0 ;
i <= n ;
i ++ ) {
for ( w = 0 ;
w <= W ;
w ++ ) {
if ( i == 0 || w == 0 ) K [ i ] [ w ] = 0 ;
else if ( wt [ i - 1 ] <= w ) K [ i ] [ w ] = max ( val [ i - 1 ]... |
cpp-java-SEQUENCES_GIVEN_LENGTH_EVERY_ELEMENT_EQUAL_TWICE_PREVIOUS_1 | cpp-java | int getTotalNumberOfSequences ( int m , int n ) {
int T [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i < m + 1 ;
i ++ ) {
for ( int j = 0 ;
j < n + 1 ;
j ++ ) {
if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ;
else if ( i < j ) T [ i ] [ j ] = 0 ;
else if ( j == 1 ) T [ i ] [ j ] = i ;
... | static int getTotalNumberOfSequences ( int m , int n ) {
int T [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i < m + 1 ;
i ++ ) {
for ( int j = 0 ;
j < n + 1 ;
j ++ ) {
if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ;
else if ( i < j ) T [ i ] [ j ] = 0 ;
else if ( j == 1 ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getTotalNumberOfSequences ( int m , int n ) {
int T [ m + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i < m + 1 ;
i ++ ) {
for ( int j = 0 ;
j < n + 1 ;
j ++ ) {
if ( i == 0 || j == 0 ) T [ i ] [ j ] = 0 ;
else if ( i < j ) T [ i ] [ j ] = 0 ;
else if ( j == 1 ) T [ i ] [ j ] = i ... |
cpp-java-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS | cpp-java | int findSum ( int n ) {
int arr [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) arr [ i ] [ j ] = abs ( i - j ) ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) sum += arr [ i ] [ j ] ;
return sum ;
}
| static int findSum ( int n ) {
int [ ] [ ] arr = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) arr [ i ] [ j ] = Math . abs ( i - j ) ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) sum += arr [ i ] [ j ] ;
return sum ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findSum ( int n ) {
int arr [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) arr [ i ] [ j ] = abs ( i - j ) ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) sum += arr [ i ] [ j ] ;
return sum ;
}
</Code> |
cpp-java-REPLACE_CHARACTER_C1_C2_C2_C1_STRING_S | cpp-java | string replace ( string s , char c1 , char c2 ) {
int l = s . length ( ) ;
for ( int i = 0 ;
i < l ;
i ++ ) {
if ( s [ i ] == c1 ) s [ i ] = c2 ;
else if ( s [ i ] == c2 ) s [ i ] = c1 ;
}
return s ;
}
| static String replace ( String s , char c1 , char c2 ) {
int l = s . length ( ) ;
char [ ] arr = s . toCharArray ( ) ;
for ( int i = 0 ;
i < l ;
i ++ ) {
if ( arr [ i ] == c1 ) arr [ i ] = c2 ;
else if ( arr [ i ] == c2 ) arr [ i ] = c1 ;
}
return String . valueOf ( arr ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string replace ( string s , char c1 , char c2 ) {
int l = s . length ( ) ;
for ( int i = 0 ;
i < l ;
i ++ ) {
if ( s [ i ] == c1 ) s [ i ] = c2 ;
else if ( s [ i ] == c2 ) s [ i ] = c1 ;
}
return s ;
}
</Code> |
cpp-java-DISCRETE_LOGARITHM_FIND_INTEGER_K_AK_CONGRUENT_MODULO_B | cpp-java | int discreteLogarithm ( int a , int b , int m ) {
int n = ( int ) sqrt ( m ) + 1 ;
int an = 1 ;
for ( int i = 0 ;
i < n ;
++ i ) an = ( an * a ) % m ;
unordered_map < int , int > value ;
for ( int i = 1 , cur = an ;
i <= n ;
++ i ) {
if ( ! value [ cur ] ) value [ cur ] = i ;
cur = ( cur * an ... | static int discreteLogarithm ( int a , int b , int m ) {
int n = ( int ) ( Math . sqrt ( m ) + 1 ) ;
int an = 1 ;
for ( int i = 0 ;
i < n ;
++ i ) an = ( an * a ) % m ;
int [ ] value = new int [ m ] ;
for ( int i = 1 , cur = an ;
i <= n ;
++ i ) {
if ( value [ cur ] == 0 ) value [ cur ] = i ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int discreteLogarithm ( int a , int b , int m ) {
int n = ( int ) sqrt ( m ) + 1 ;
int an = 1 ;
for ( int i = 0 ;
i < n ;
++ i ) an = ( an * a ) % m ;
unordered_map < int , int > value ;
for ( int i = 1 , cur = an ;
i <= n ;
++ i ) {
if ( ! value [ cur ] ) value [ cur ] = i ;
cur = ( cu... |
cpp-java-FIND_LARGEST_PRIME_FACTOR_NUMBER | cpp-java | long long maxPrimeFactors ( long long n ) {
long long maxPrime = - 1 ;
while ( n % 2 == 0 ) {
maxPrime = 2 ;
n >>= 1 ;
}
for ( int i = 3 ;
i <= sqrt ( n ) ;
i += 2 ) {
while ( n % i == 0 ) {
maxPrime = i ;
n = n / i ;
}
}
if ( n > 2 ) maxPrime = n ;
return maxPrime ;
}
| static long maxPrimeFactors ( long n ) {
long maxPrime = - 1 ;
while ( n % 2 == 0 ) {
maxPrime = 2 ;
n >>= 1 ;
}
for ( int i = 3 ;
i <= Math . sqrt ( n ) ;
i += 2 ) {
while ( n % i == 0 ) {
maxPrime = i ;
n = n / i ;
}
}
if ( n > 2 ) maxPrime = n ;
return maxPrime ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long maxPrimeFactors ( long long n ) {
long long maxPrime = - 1 ;
while ( n % 2 == 0 ) {
maxPrime = 2 ;
n >>= 1 ;
}
for ( int i = 3 ;
i <= sqrt ( n ) ;
i += 2 ) {
while ( n % i == 0 ) {
maxPrime = i ;
n = n / i ;
}
}
if ( n > 2 ) maxPrime = n ;
return maxPrime ;... |
cpp-java-POSSIBLE_FORM_TRIANGLE_ARRAY_VALUES | cpp-java | bool isPossibleTriangle ( int arr [ ] , int N ) {
if ( N < 3 ) return false ;
sort ( arr , arr + N ) ;
for ( int i = 0 ;
i < N - 2 ;
i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ;
}
| static boolean isPossibleTriangle ( int [ ] arr , int N ) {
if ( N < 3 ) return false ;
Arrays . sort ( arr ) ;
for ( int i = 0 ;
i < N - 2 ;
i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ;
return false ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isPossibleTriangle ( int arr [ ] , int N ) {
if ( N < 3 ) return false ;
sort ( arr , arr + N ) ;
for ( int i = 0 ;
i < N - 2 ;
i ++ ) if ( arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] ) return true ;
}
</Code> |
cpp-java-COUNT_NUMBER_INCREASING_SUBSEQUENCES_SIZE_K | cpp-java | int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) {
int dp [ k ] [ n ] , sum = 0 ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ 0 ] [ i ] = 1 ;
for ( int l = 1 ;
l < k ;
l ++ ) {
for ( int i = l ;
i < n ;
i ++ ) {
dp [ l ] [ i ] = 0 ;
for ( in... | static int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) {
int dp [ ] [ ] = new int [ k ] [ n ] , sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
dp [ 0 ] [ i ] = 1 ;
}
for ( int l = 1 ;
l < k ;
l ++ ) {
for ( int i = l ;
i < n ;
i ++ ) {
dp [ l ] [ i ] = 0 ;
for ( int j... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) {
int dp [ k ] [ n ] , sum = 0 ;
memset ( dp , 0 , sizeof ( dp ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) dp [ 0 ] [ i ] = 1 ;
for ( int l = 1 ;
l < k ;
l ++ ) {
for ( int i = l ;
i < n ;
i ++ ) {
dp [ l ] [ i ] = 0 ;
f... |
cpp-java-MINIMUM_COST_MAKE_LONGEST_COMMON_SUBSEQUENCE_LENGTH_K | cpp-java | int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) {
if ( ! k ) return 0 ;
if ( l < 0 | static int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ ] [ ] ) {
if ( k == 0 ) {
return 0 ;
}
if ( l < 0 | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int solve ( char X [ ] , char Y [ ] , int l , int r , int k , int dp [ ] [ N ] [ N ] ) {
if ( ! k ) return 0 ;
if ( l < 0
</Code> |
cpp-java-REMAINDER_7_LARGE_NUMBERS | cpp-java | int remainderWith7 ( string num ) {
int series [ ] = {
1 , 3 , 2 , - 1 , - 3 , - 2 }
;
int series_index = 0 ;
int result = 0 ;
for ( int i = num . size ( ) - 1 ;
i >= 0 ;
i -- ) {
int digit = num [ i ] - '0' ;
result += digit * series [ series_index ] ;
series_index = ( s... | static int remainderWith7 ( String num ) {
int series [ ] = {
1 , 3 , 2 , - 1 , - 3 , - 2 }
;
int series_index = 0 ;
int result = 0 ;
for ( int i = num . length ( ) - 1 ;
i >= 0 ;
i -- ) {
int digit = num . charAt ( i ) - ' 0 ' ;
result += digit * series [ series_index ] ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int remainderWith7 ( string num ) {
int series [ ] = {
1 , 3 , 2 , - 1 , - 3 , - 2 }
;
int series_index = 0 ;
int result = 0 ;
for ( int i = num . size ( ) - 1 ;
i >= 0 ;
i -- ) {
int digit = num [ i ] - '0' ;
result += digit * series [ series_index ] ;
series_inde... |
cpp-java-GIVEN_TWO_STRINGS_FIND_FIRST_STRING_SUBSEQUENCE_SECOND | cpp-java | bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) {
if ( m == 0 ) return true ;
if ( n == 0 ) return false ;
if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ;
return isSubSequence ( str1 , str2 , m , n - 1 ) ;
}
| static boolean isSubSequence ( String str1 , String str2 , int m , int n ) {
if ( m == 0 ) return true ;
if ( n == 0 ) return false ;
if ( str1 . charAt ( m - 1 ) == str2 . charAt ( n - 1 ) ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ;
return isSubSequence ( str1 , str2 , m , n - 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isSubSequence ( char str1 [ ] , char str2 [ ] , int m , int n ) {
if ( m == 0 ) return true ;
if ( n == 0 ) return false ;
if ( str1 [ m - 1 ] == str2 [ n - 1 ] ) return isSubSequence ( str1 , str2 , m - 1 , n - 1 ) ;
return isSubSequence ( str1 , str2 , m , n - 1 ) ;
}
</Code> |
cpp-java-PROGRAM_TO_FIND_REMAINDER_WITHOUT_USING_MODULO_OR_OPERATOR | cpp-java | int getRemainder ( int num , int divisor ) {
return ( num - divisor * ( num / divisor ) ) ;
}
| static int getRemainder ( int num , int divisor ) {
return ( num - divisor * ( num / divisor ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getRemainder ( int num , int divisor ) {
return ( num - divisor * ( num / divisor ) ) ;
}
</Code> |
cpp-java-MINIMUM_PRODUCT_SUBSET_ARRAY | cpp-java | int minProductSubset ( int a [ ] , int n ) {
if ( n == 1 ) return a [ 0 ] ;
int max_neg = INT_MIN ;
int min_pos = INT_MAX ;
int count_neg = 0 , count_zero = 0 ;
int prod = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] == 0 ) {
count_zero ++ ;
continue ;
}
if ( a [ i ] < 0 )... | static int minProductSubset ( int a [ ] , int n ) {
if ( n == 1 ) return a [ 0 ] ;
int negmax = Integer . MIN_VALUE ;
int posmin = Integer . MAX_VALUE ;
int count_neg = 0 , count_zero = 0 ;
int product = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] == 0 ) {
count_zero ++ ;
continu... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minProductSubset ( int a [ ] , int n ) {
if ( n == 1 ) return a [ 0 ] ;
int max_neg = INT_MIN ;
int min_pos = INT_MAX ;
int count_neg = 0 , count_zero = 0 ;
int prod = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] == 0 ) {
count_zero ++ ;
continue ;
}
if ( a [ i ... |
cpp-java-HOW_CAN_WE_SUM_THE_DIGITS_OF_A_GIVEN_NUMBER_IN_SINGLE_STATEMENT | cpp-java | public : int getSum ( int n ) {
int sum = 0 ;
while ( n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum ;
}
| static int getSum ( int n ) {
int sum = 0 ;
while ( n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
public : int getSum ( int n ) {
int sum = 0 ;
while ( n != 0 ) {
sum = sum + n % 10 ;
n = n / 10 ;
}
return sum ;
}
</Code> |
cpp-java-COUNT_DIGITS_FACTORIAL_SET_1 | cpp-java | int findDigits ( int n ) {
if ( n < 0 ) return 0 ;
if ( n <= 1 ) return 1 ;
double digits = 0 ;
for ( int i = 2 ;
i <= n ;
i ++ ) digits += log10 ( i ) ;
return floor ( digits ) + 1 ;
}
| static int findDigits ( int n ) {
if ( n < 0 ) return 0 ;
if ( n <= 1 ) return 1 ;
double digits = 0 ;
for ( int i = 2 ;
i <= n ;
i ++ ) digits += Math . log10 ( i ) ;
return ( int ) ( Math . floor ( digits ) ) + 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findDigits ( int n ) {
if ( n < 0 ) return 0 ;
if ( n <= 1 ) return 1 ;
double digits = 0 ;
for ( int i = 2 ;
i <= n ;
i ++ ) digits += log10 ( i ) ;
return floor ( digits ) + 1 ;
}
</Code> |
cpp-java-SORT_ARRAY_TWO_HALVES_SORTED | cpp-java | void mergeTwoHalf ( int A [ ] , int n ) {
sort ( A , A + n ) ;
}
| static void mergeTwoHalf ( int [ ] A , int n ) {
Arrays . sort ( A ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void mergeTwoHalf ( int A [ ] , int n ) {
sort ( A , A + n ) ;
}
</Code> |
cpp-java-CREATE_A_MATRIX_WITH_ALTERNATING_RECTANGLES_OF_0_AND_X | cpp-java | void fill0X ( int m , int n ) {
int i , k = 0 , l = 0 ;
int r = m , c = n ;
char a [ m ] [ n ] ;
char x = 'X' ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) a [ k ] [ i ] = x ;
k ++ ;
for ( i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = x ;
n -- ;
if ( k < m ) {
... | static void fill0X ( int m , int n ) {
int i , k = 0 , l = 0 ;
int r = m , c = n ;
char a [ ] [ ] = new char [ m ] [ n ] ;
char x = ' X ' ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) a [ k ] [ i ] = x ;
k ++ ;
for ( i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = x ;
n ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void fill0X ( int m , int n ) {
int i , k = 0 , l = 0 ;
int r = m , c = n ;
char a [ m ] [ n ] ;
char x = 'X' ;
while ( k < m && l < n ) {
for ( i = l ;
i < n ;
++ i ) a [ k ] [ i ] = x ;
k ++ ;
for ( i = k ;
i < m ;
++ i ) a [ i ] [ n - 1 ] = x ;
n -- ;
if ( k < m )... |
cpp-java-MINIMUM_PERIMETER_N_BLOCKS | cpp-java | int minPerimeter ( int n ) {
int l = sqrt ( n ) ;
int sq = l * l ;
if ( sq == n ) return l * 4 ;
else {
long long int row = n / l ;
long long int perimeter = 2 * ( l + row ) ;
if ( n % l != 0 ) perimeter += 2 ;
return perimeter ;
}
}
| public static long minPerimeter ( int n ) {
int l = ( int ) Math . sqrt ( n ) ;
int sq = l * l ;
if ( sq == n ) return l * 4 ;
else {
long row = n / l ;
long perimeter = 2 * ( l + row ) ;
if ( n % l != 0 ) perimeter += 2 ;
return perimeter ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minPerimeter ( int n ) {
int l = sqrt ( n ) ;
int sq = l * l ;
if ( sq == n ) return l * 4 ;
else {
long long int row = n / l ;
long long int perimeter = 2 * ( l + row ) ;
if ( n % l != 0 ) perimeter += 2 ;
return perimeter ;
}
}
</Code> |
cpp-java-FIND_POSSIBLE_ROTATE_PAGE_ANGLE_NOT | cpp-java | void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) {
long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ;
long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ;
if ( dis1 != dis2 ) cout << "No" ;
else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b... | static void possibleOrNot ( long a1 , long a2 , long b1 , long b2 , long c1 , long c2 ) {
long dis1 = ( long ) Math . pow ( b1 - a1 , 2 ) + ( long ) Math . pow ( b2 - a2 , 2 ) ;
long dis2 = ( long ) Math . pow ( c1 - b1 , 2 ) + ( long ) Math . pow ( c2 - b2 , 2 ) ;
if ( dis1 != dis2 ) System . out . print ( "No" ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void possibleOrNot ( long long a1 , long long a2 , long long b1 , long long b2 , long long c1 , long long c2 ) {
long long dis1 = pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ;
long long dis2 = pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ;
if ( dis1 != dis2 ) cout << "No" ;
else if ( b1 == ( ( a1 + c1 ) / 2.0... |
cpp-java-PROGRAM_FIND_SLOPE_LINE | cpp-java | float slope ( float x1 , float y1 , float x2 , float y2 ) {
return ( y2 - y1 ) / ( x2 - x1 ) ;
}
| static float slope ( float x1 , float y1 , float x2 , float y2 ) {
return ( y2 - y1 ) / ( x2 - x1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
float slope ( float x1 , float y1 , float x2 , float y2 ) {
return ( y2 - y1 ) / ( x2 - x1 ) ;
}
</Code> |
cpp-java-FIND_NUMBER_PERFECT_SQUARES_TWO_GIVEN_NUMBERS_1 | cpp-java | int countSquares ( int a , int b ) {
return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ;
}
| double countSquares ( int a , int b ) {
return ( Math . floor ( Math . sqrt ( b ) ) - Math . ceil ( Math . sqrt ( a ) ) + 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countSquares ( int a , int b ) {
return ( floor ( sqrt ( b ) ) - ceil ( sqrt ( a ) ) + 1 ) ;
}
</Code> |
cpp-java-COUNT_BINARY_STRINGS_K_TIMES_APPEARING_ADJACENT_TWO_SET_BITS | cpp-java | int countStrings ( int n , int k ) {
int dp [ n + 1 ] [ k + 1 ] [ 2 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 1 ] [ 0 ] [ 0 ] = 1 ;
dp [ 1 ] [ 0 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp [ ... | static int countStrings ( int n , int k ) {
int dp [ ] [ ] [ ] = new int [ n + 1 ] [ k + 1 ] [ 2 ] ;
dp [ 1 ] [ 0 ] [ 0 ] = 1 ;
dp [ 1 ] [ 0 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j < i && j < k + 1 ;
j ++ ) {
dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + d... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countStrings ( int n , int k ) {
int dp [ n + 1 ] [ k + 1 ] [ 2 ] ;
memset ( dp , 0 , sizeof ( dp ) ) ;
dp [ 1 ] [ 0 ] [ 0 ] = 1 ;
dp [ 1 ] [ 0 ] [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j < i ;
j ++ ) {
dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] ... |
cpp-java-FIND_SMALLEST_RANGE_CONTAINING_ELEMENTS_FROM_K_LISTS | cpp-java | void findSmallestRange ( int arr [ ] [ N ] , int n , int k ) {
int i , minval , maxval , minrange , minel , maxel , flag , minind ;
for ( i = 0 ;
i <= k ;
i ++ ) ptr [ i ] = 0 ;
minrange = INT_MAX ;
while ( 1 ) {
minind = - 1 ;
minval = INT_MAX ;
maxval = INT_MIN ;
flag = 0 ;
for ( i = 0... | static void findSmallestRange ( int arr [ ] [ ] , int n , int k ) {
int i , minval , maxval , minrange , minel = 0 , maxel = 0 , flag , minind ;
for ( i = 0 ;
i <= k ;
i ++ ) {
ptr [ i ] = 0 ;
}
minrange = Integer . MAX_VALUE ;
while ( true ) {
minind = - 1 ;
minval = Integer . MAX_VALUE ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void findSmallestRange ( int arr [ ] [ N ] , int n , int k ) {
int i , minval , maxval , minrange , minel , maxel , flag , minind ;
for ( i = 0 ;
i <= k ;
i ++ ) ptr [ i ] = 0 ;
minrange = INT_MAX ;
while ( 1 ) {
minind = - 1 ;
minval = INT_MAX ;
maxval = INT_MIN ;
flag = 0 ;
for ... |
cpp-java-LEXICOGRAPHICAL_CONCATENATION_SUBSTRINGS_STRING | cpp-java | string lexicographicSubConcat ( string s ) {
int n = s . length ( ) ;
int sub_count = n * ( n + 1 ) / 2 ;
string arr [ sub_count ] ;
int index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int len = 1 ;
len <= n - i ;
len ++ ) arr [ index ++ ] = s . substr ( i , len ) ;
sort ( arr , arr + sub_count )... | static String lexicographicSubConcat ( String s ) {
int n = s . length ( ) ;
int sub_count = n * ( n + 1 ) / 2 ;
String [ ] arr = new String [ sub_count ] ;
int index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int len = 1 ;
len <= n - i ;
len ++ ) {
arr [ index ++ ] = s . substring ( i , i + len... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string lexicographicSubConcat ( string s ) {
int n = s . length ( ) ;
int sub_count = n * ( n + 1 ) / 2 ;
string arr [ sub_count ] ;
int index = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int len = 1 ;
len <= n - i ;
len ++ ) arr [ index ++ ] = s . substr ( i , len ) ;
sort ( arr , arr + sub_... |
cpp-java-FIND_MAXIMUM_ELEMENT_ROW_MATRIX | cpp-java | void maxelement ( int no_of_rows , int arr [ ] [ N ] ) {
int i = 0 ;
int max = 0 ;
int result [ no_of_rows ] ;
while ( i < no_of_rows ) {
for ( int j = 0 ;
j < N ;
j ++ ) {
if ( arr [ i ] [ j ] > max ) {
max = arr [ i ] [ j ] ;
}
}
result [ i ] = max ;
max = 0 ;
i... | public static void maxelement ( int no_of_rows , int [ ] [ ] arr ) {
int i = 0 ;
int max = 0 ;
int [ ] result = new int [ no_of_rows ] ;
while ( i < no_of_rows ) {
for ( int j = 0 ;
j < arr [ i ] . length ;
j ++ ) {
if ( arr [ i ] [ j ] > max ) {
max = arr [ i ] [ j ] ;
}
}
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void maxelement ( int no_of_rows , int arr [ ] [ N ] ) {
int i = 0 ;
int max = 0 ;
int result [ no_of_rows ] ;
while ( i < no_of_rows ) {
for ( int j = 0 ;
j < N ;
j ++ ) {
if ( arr [ i ] [ j ] > max ) {
max = arr [ i ] [ j ] ;
}
}
result [ i ] = max ;
max = 0 ... |
cpp-java-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX_1 | cpp-java | int countNegative ( int M [ ] [ 4 ] , int n , int m ) {
int count = 0 ;
int i = 0 ;
int j = m - 1 ;
while ( j >= 0 && i < n ) {
if ( M [ i ] [ j ] < 0 ) {
count += j + 1 ;
i += 1 ;
}
else j -= 1 ;
}
return count ;
}
| static int countNegative ( int M [ ] [ ] , int n , int m ) {
int count = 0 ;
int i = 0 ;
int j = m - 1 ;
while ( j >= 0 && i < n ) {
if ( M [ i ] [ j ] < 0 ) {
count += j + 1 ;
i += 1 ;
}
else j -= 1 ;
}
return count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countNegative ( int M [ ] [ 4 ] , int n , int m ) {
int count = 0 ;
int i = 0 ;
int j = m - 1 ;
while ( j >= 0 && i < n ) {
if ( M [ i ] [ j ] < 0 ) {
count += j + 1 ;
i += 1 ;
}
else j -= 1 ;
}
return count ;
}
</Code> |
cpp-java-FIND_POSITION_GIVEN_NUMBER_AMONG_NUMBERS_MADE_4_7 | cpp-java | int findpos ( string n ) {
int i = 0 , pos = 0 ;
while ( n [ i ] != '\0' ) {
switch ( n [ i ] ) {
case '4' : pos = pos * 2 + 1 ;
break ;
case '7' : pos = pos * 2 + 2 ;
break ;
}
i ++ ;
}
return pos ;
}
| static int findpos ( String n ) {
int k = 0 , pos = 0 , i = 0 ;
while ( k != n . length ( ) ) {
switch ( n . charAt ( i ) ) {
case ' 4 ' : pos = pos * 2 + 1 ;
break ;
case ' 7 ' : pos = pos * 2 + 2 ;
break ;
}
i ++ ;
k ++ ;
}
return pos ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findpos ( string n ) {
int i = 0 , pos = 0 ;
while ( n [ i ] != '\0' ) {
switch ( n [ i ] ) {
case '4' : pos = pos * 2 + 1 ;
break ;
case '7' : pos = pos * 2 + 2 ;
break ;
}
i ++ ;
}
return pos ;
}
</Code> |
cpp-java-FIND_VALUE_OF_Y_MOD_2_RAISED_TO_POWER_X | cpp-java | long long int yMod ( long long int y , long long int x ) {
if ( log2 ( y ) < x ) return y ;
if ( x > 63 ) return y ;
return ( y % ( 1 << x ) ) ;
}
| static long yMod ( long y , long x ) {
if ( ( Math . log ( y ) / Math . log ( 2 ) ) < x ) return y ;
if ( x > 63 ) return y ;
return ( y % ( 1 << ( int ) x ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
long long int yMod ( long long int y , long long int x ) {
if ( log2 ( y ) < x ) return y ;
if ( x > 63 ) return y ;
return ( y % ( 1 << x ) ) ;
}
</Code> |
cpp-java-DYNAMIC_PROGRAMMING_SET_14_MAXIMUM_SUM_INCREASING_SUBSEQUENCE | cpp-java | int maxSumIS ( int arr [ ] , int n ) {
int i , j , max = 0 ;
int msis [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) msis [ i ] = arr [ i ] ;
for ( i = 1 ;
i < n ;
i ++ ) for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ] ;
fo... | static int maxSumIS ( int arr [ ] , int n ) {
int i , j , max = 0 ;
int msis [ ] = new int [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) msis [ i ] = arr [ i ] ;
for ( i = 1 ;
i < n ;
i ++ ) for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSumIS ( int arr [ ] , int n ) {
int i , j , max = 0 ;
int msis [ n ] ;
for ( i = 0 ;
i < n ;
i ++ ) msis [ i ] = arr [ i ] ;
for ( i = 1 ;
i < n ;
i ++ ) for ( j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && msis [ i ] < msis [ j ] + arr [ i ] ) msis [ i ] = msis [ j ] + arr [ i ]... |
cpp-java-LENGTH_LONGEST_BALANCED_SUBSEQUENCE_1 | cpp-java | int maxLength ( char s [ ] , int n ) {
int invalidOpenBraces = 0 ;
int invalidCloseBraces = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s [ i ] == '(' ) {
invalidOpenBraces ++ ;
}
else {
if ( invalidOpenBraces == 0 ) {
invalidCloseBraces ++ ;
}
else {
invali... | static int maxLength ( String s , int n ) {
int invalidOpenBraces = 0 ;
int invalidCloseBraces = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s . charAt ( i ) == ' ( ' ) {
invalidOpenBraces ++ ;
}
else {
if ( invalidOpenBraces == 0 ) {
invalidCloseBraces ++ ;
}
else ... | 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 invalidOpenBraces = 0 ;
int invalidCloseBraces = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( s [ i ] == '(' ) {
invalidOpenBraces ++ ;
}
else {
if ( invalidOpenBraces == 0 ) {
invalidCloseBraces ++ ;
}
else {
... |
cpp-java-MAXIMUM_PRODUCT_INCREASING_SUBSEQUENCE | cpp-java | ll lis ( ll arr [ ] , ll n ) {
ll mpis [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) mpis [ i ] = arr [ i ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ;
return * max_elemen... | static int lis ( int [ ] arr , int n ) {
int [ ] mpis = new int [ n ] ;
int max = Integer . MIN_VALUE ;
for ( int i = 0 ;
i < n ;
i ++ ) mpis [ i ] = arr [ i ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
ll lis ( ll arr [ ] , ll n ) {
ll mpis [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) mpis [ i ] = arr [ i ] ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) mpis [ i ] = mpis [ j ] * arr [ i ] ;
return * max... |
cpp-java-PETERSON_GRAPH | cpp-java | bool findthepath ( char * S , int v ) {
result [ 0 ] = v + '0' ;
for ( int i = 1 ;
S [ i ] ;
i ++ ) {
if ( adj [ v ] [ S [ i ] - 'A' ] || adj [ S [ i ] - 'A' ] [ v ] ) {
v = S [ i ] - 'A' ;
}
else if ( adj [ v ] [ S [ i ] - 'A' + 5 ] || adj [ S [ i ] - 'A' + 5 ] [ v ] ) {
v = S [ i ] - '... | static boolean findthepath ( char [ ] S , int v ) {
result [ 0 ] = ( char ) ( v + ' 0 ' ) ;
for ( int i = 1 ;
i < ( int ) S . length ;
i ++ ) {
if ( adj [ v ] [ S [ i ] - ' A ' ] || adj [ S [ i ] - ' A ' ] [ v ] ) {
v = S [ i ] - ' A ' ;
}
else if ( adj [ v ] [ S [ i ] - ' A ' + 5 ] || adj [ S... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool findthepath ( char * S , int v ) {
result [ 0 ] = v + '0' ;
for ( int i = 1 ;
S [ i ] ;
i ++ ) {
if ( adj [ v ] [ S [ i ] - 'A' ] || adj [ S [ i ] - 'A' ] [ v ] ) {
v = S [ i ] - 'A' ;
}
else if ( adj [ v ] [ S [ i ] - 'A' + 5 ] || adj [ S [ i ] - 'A' + 5 ] [ v ] ) {
v = S [ ... |
cpp-java-CHECK_QUEUE_CAN_SORTED_ANOTHER_QUEUE_USING_STACK | cpp-java | bool checkSorted ( int n , queue < int > & q ) {
stack < int > st ;
int expected = 1 ;
int fnt ;
while ( ! q . empty ( ) ) {
fnt = q . front ( ) ;
q . pop ( ) ;
if ( fnt == expected ) expected ++ ;
else {
if ( st . empty ( ) ) {
st . push ( fnt ) ;
}
else if ( ! st . em... | static boolean checkSorted ( int n ) {
Stack < Integer > st = new Stack < Integer > ( ) ;
int expected = 1 ;
int fnt ;
while ( q . size ( ) != 0 ) {
fnt = q . peek ( ) ;
q . poll ( ) ;
if ( fnt == expected ) expected ++ ;
else {
if ( st . size ( ) == 0 ) {
st . push ( fnt ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool checkSorted ( int n , queue < int > & q ) {
stack < int > st ;
int expected = 1 ;
int fnt ;
while ( ! q . empty ( ) ) {
fnt = q . front ( ) ;
q . pop ( ) ;
if ( fnt == expected ) expected ++ ;
else {
if ( st . empty ( ) ) {
st . push ( fnt ) ;
}
else if ( ! ... |
cpp-java-GENERATING_DISTINCT_SUBSEQUENCES_OF_A_GIVEN_STRING_IN_LEXICOGRAPHIC_ORDER | cpp-java | void generate ( set < string > & st , string s ) {
if ( s . size ( ) == 0 ) return ;
if ( st . find ( s ) == st . end ( ) ) {
st . insert ( s ) ;
for ( int i = 0 ;
i < s . size ( ) ;
i ++ ) {
string t = s ;
t . erase ( i , 1 ) ;
generate ( st , t ) ;
}
}
return ;
}
| static void generate ( Set < String > st , String s ) {
if ( s . length ( ) == 0 ) {
return ;
}
if ( ! st . contains ( s ) ) {
st . add ( s ) ;
for ( int i = 0 ;
i < s . length ( ) ;
i ++ ) {
String t = s ;
t = t . substring ( 0 , i ) + t . substring ( i + 1 ) ;
generate ( st... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void generate ( set < string > & st , string s ) {
if ( s . size ( ) == 0 ) return ;
if ( st . find ( s ) == st . end ( ) ) {
st . insert ( s ) ;
for ( int i = 0 ;
i < s . size ( ) ;
i ++ ) {
string t = s ;
t . erase ( i , 1 ) ;
generate ( st , t ) ;
}
}
return ;
}
... |
cpp-java-RECURSIVE_FUNCTIONS | cpp-java | void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) {
if ( 0 == n ) return ;
tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ;
cout << "Move the disk " << n << " from " << sourcePole << " to " << destinationPole << endl ;
tower ( n - 1 , auxiliaryPole , destinationP... | static void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) {
if ( 0 == n ) return ;
tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ;
System . out . printf ( "Move the disk %d from %c to %c\n" , n , sourcePole , destinationPole ) ;
tower ( n - 1 , auxiliaryPole , de... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) {
if ( 0 == n ) return ;
tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ;
cout << "Move the disk " << n << " from " << sourcePole << " to " << destinationPole << endl ;
tower ( n - 1 , auxiliaryPole , desti... |
cpp-java-PIZZA_CUT_PROBLEM_CIRCLE_DIVISION_LINES | cpp-java | int findMaximumPieces ( int n ) {
return 1 + n * ( n + 1 ) / 2 ;
}
| static int findMaximumPieces ( int n ) {
return 1 + n * ( n + 1 ) / 2 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findMaximumPieces ( int n ) {
return 1 + n * ( n + 1 ) / 2 ;
}
</Code> |
cpp-java-LEXICOGRAPHICALLY_MINIMUM_STRING_ROTATION | cpp-java | string minLexRotation ( string str ) {
int n = str . length ( ) ;
string arr [ n ] ;
string concat = str + str ;
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = concat . substr ( i , n ) ;
sort ( arr , arr + n ) ;
return arr [ 0 ] ;
}
| static String minLexRotation ( String str ) {
int n = str . length ( ) ;
String arr [ ] = new String [ n ] ;
String concat = str + str ;
for ( int i = 0 ;
i < n ;
i ++ ) {
arr [ i ] = concat . substring ( i , i + n ) ;
}
Arrays . sort ( arr ) ;
return arr [ 0 ] ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string minLexRotation ( string str ) {
int n = str . length ( ) ;
string arr [ n ] ;
string concat = str + str ;
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = concat . substr ( i , n ) ;
sort ( arr , arr + n ) ;
return arr [ 0 ] ;
}
</Code> |
cpp-java-DYNAMIC_PROGRAMMING_SET_28_MINIMUM_INSERTIONS_TO_FORM_A_PALINDROME | cpp-java | int findMinInsertions ( char str [ ] , int l , int h ) {
if ( l > h ) return INT_MAX ;
if ( l == h ) return 0 ;
if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ;
return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , findMinIns... | static int findMinInsertions ( char str [ ] , int l , int h ) {
if ( l > h ) return Integer . MAX_VALUE ;
if ( l == h ) return 0 ;
if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ;
return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( Integer . min ( findMinInsertions ( s... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findMinInsertions ( char str [ ] , int l , int h ) {
if ( l > h ) return INT_MAX ;
if ( l == h ) return 0 ;
if ( l == h - 1 ) return ( str [ l ] == str [ h ] ) ? 0 : 1 ;
return ( str [ l ] == str [ h ] ) ? findMinInsertions ( str , l + 1 , h - 1 ) : ( min ( findMinInsertions ( str , l , h - 1 ) , fin... |
cpp-java-PROGRAM_OCTAL_DECIMAL_CONVERSION | cpp-java | int octalToDecimal ( int n ) {
int num = n ;
int dec_value = 0 ;
int base = 1 ;
int temp = num ;
while ( temp ) {
int last_digit = temp % 10 ;
temp = temp / 10 ;
dec_value += last_digit * base ;
base = base * 8 ;
}
return dec_value ;
}
| static int octalToDecimal ( int n ) {
int num = n ;
int dec_value = 0 ;
int base = 1 ;
int temp = num ;
while ( temp > 0 ) {
int last_digit = temp % 10 ;
temp = temp / 10 ;
dec_value += last_digit * base ;
base = base * 8 ;
}
return dec_value ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int octalToDecimal ( int n ) {
int num = n ;
int dec_value = 0 ;
int base = 1 ;
int temp = num ;
while ( temp ) {
int last_digit = temp % 10 ;
temp = temp / 10 ;
dec_value += last_digit * base ;
base = base * 8 ;
}
return dec_value ;
}
</Code> |
cpp-java-FIND_THE_MISSING_NUMBER | cpp-java | int getMissingNo ( int a [ ] , int n ) {
int total = ( n + 1 ) * ( n + 2 ) / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) total -= a [ i ] ;
return total ;
}
| static int getMissingNo ( int a [ ] , int n ) {
int i , total ;
total = ( n + 1 ) * ( n + 2 ) / 2 ;
for ( i = 0 ;
i < n ;
i ++ ) total -= a [ i ] ;
return total ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getMissingNo ( int a [ ] , int n ) {
int total = ( n + 1 ) * ( n + 2 ) / 2 ;
for ( int i = 0 ;
i < n ;
i ++ ) total -= a [ i ] ;
return total ;
}
</Code> |
cpp-java-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING | cpp-java | void printSquares ( int n ) {
int square = 0 , prev_x = 0 ;
for ( int x = 0 ;
x < n ;
x ++ ) {
square = ( square + x + prev_x ) ;
cout << square << " " ;
prev_x = x ;
}
}
| static void printSquares ( int n ) {
int square = 0 , prev_x = 0 ;
for ( int x = 0 ;
x < n ;
x ++ ) {
square = ( square + x + prev_x ) ;
System . out . print ( square + " " ) ;
prev_x = x ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printSquares ( int n ) {
int square = 0 , prev_x = 0 ;
for ( int x = 0 ;
x < n ;
x ++ ) {
square = ( square + x + prev_x ) ;
cout << square << " " ;
prev_x = x ;
}
}
</Code> |
cpp-java-PERMUTE_TWO_ARRAYS_SUM_EVERY_PAIR_GREATER_EQUAL_K | cpp-java | bool isPossible ( int a [ ] , int b [ ] , int n , int k ) {
sort ( a , a + n ) ;
sort ( b , b + n , greater < int > ( ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( a [ i ] + b [ i ] < k ) return false ;
return true ;
}
| static boolean isPossible ( Integer a [ ] , int b [ ] , int n , int k ) {
Arrays . sort ( a , Collections . reverseOrder ( ) ) ;
Arrays . sort ( b ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( a [ i ] + b [ 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 isPossible ( int a [ ] , int b [ ] , int n , int k ) {
sort ( a , a + n ) ;
sort ( b , b + n , greater < int > ( ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( a [ i ] + b [ i ] < k ) return false ;
return true ;
}
</Code> |
cpp-java-CHECK_WHETHER_GIVEN_CIRCLE_RESIDE_BOUNDARY_MAINTAINED_OUTER_CIRCLE_INNER_CIRCLE | cpp-java | void fitOrNotFit ( int R , int r , int x , int y , int rad ) {
double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ;
if ( val + rad <= R && val - rad >= R - r ) cout << "Fits\n" ;
else cout << "Doesn't Fit\n" ;
}
| static void fitOrNotFit ( int R , int r , int x , int y , int rad ) {
double val = Math . sqrt ( Math . pow ( x , 2 ) + Math . pow ( y , 2 ) ) ;
if ( val + rad <= R && val - rad >= R - r ) System . out . println ( "Fits" ) ;
else System . out . println ( "Doesn't Fit" ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void fitOrNotFit ( int R , int r , int x , int y , int rad ) {
double val = sqrt ( pow ( x , 2 ) + pow ( y , 2 ) ) ;
if ( val + rad <= R && val - rad >= R - r ) cout << "Fits\n" ;
else cout << "Doesn't Fit\n" ;
}
</Code> |
cpp-java-NTH_MULTIPLE_NUMBER_FIBONACCI_SERIES | cpp-java | int findPosition ( int k , int n ) {
unsigned long long int f1 = 0 , f2 = 1 , f3 ;
for ( int i = 2 ;
i <= MAX ;
i ++ ) {
f3 = f1 + f2 ;
f1 = f2 ;
f2 = f3 ;
if ( f2 % k == 0 ) return n * i ;
}
}
| public static int findPosition ( int k , int n ) {
long f1 = 0 , f2 = 1 , f3 ;
int i = 2 ;
while ( i != 0 ) {
f3 = f1 + f2 ;
f1 = f2 ;
f2 = f3 ;
if ( f2 % k == 0 ) {
return n * i ;
}
i ++ ;
}
return 0 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findPosition ( int k , int n ) {
unsigned long long int f1 = 0 , f2 = 1 , f3 ;
for ( int i = 2 ;
i <= MAX ;
i ++ ) {
f3 = f1 + f2 ;
f1 = f2 ;
f2 = f3 ;
if ( f2 % k == 0 ) return n * i ;
}
}
</Code> |
cpp-java-PRINT_N_TERMS_NEWMAN_CONWAY_SEQUENCE | cpp-java | void sequence ( int n ) {
int f [ n + 1 ] ;
f [ 0 ] = 0 ;
f [ 1 ] = 1 ;
f [ 2 ] = 1 ;
cout << f [ 1 ] << " " << f [ 2 ] << " " ;
for ( int i = 3 ;
i <= n ;
i ++ ) {
f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ;
cout << f [ i ] << " " ;
}
}
| public static void sequence ( int n ) {
int f [ ] = new int [ n + 1 ] ;
f [ 0 ] = 0 ;
f [ 1 ] = 1 ;
f [ 2 ] = 1 ;
System . out . print ( f [ 1 ] + " " + f [ 2 ] + " " ) ;
for ( int i = 3 ;
i <= n ;
i ++ ) {
f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ;
System . out . print ( f [ i ] + " ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void sequence ( int n ) {
int f [ n + 1 ] ;
f [ 0 ] = 0 ;
f [ 1 ] = 1 ;
f [ 2 ] = 1 ;
cout << f [ 1 ] << " " << f [ 2 ] << " " ;
for ( int i = 3 ;
i <= n ;
i ++ ) {
f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ;
cout << f [ i ] << " " ;
}
}
</Code> |
cpp-java-MINIMUM_NUMBER_OF_MANIPULATIONS_REQUIRED_TO_MAKE_TWO_STRINGS_ANAGRAM_WITHOUT_DELETION_OF_CHARACTER | cpp-java | int countManipulations ( string s1 , string s2 ) {
int count = 0 ;
int char_count [ 26 ] ;
for ( int i = 0 ;
i < 26 ;
i ++ ) {
char_count [ i ] = 0 ;
}
for ( int i = 0 ;
i < s1 . length ( ) ;
i ++ ) char_count [ s1 [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < s2 . length ( ) ;
i ++ ) {
char_... | static int countManipulations ( String s1 , String s2 ) {
int count = 0 ;
int char_count [ ] = new int [ 26 ] ;
for ( int i = 0 ;
i < s1 . length ( ) ;
i ++ ) char_count [ s1 . charAt ( i ) - ' a ' ] ++ ;
for ( int i = 0 ;
i < s2 . length ( ) ;
i ++ ) if ( char_count [ s2 . charAt ( i ) - ' a ' ] -- <= ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countManipulations ( string s1 , string s2 ) {
int count = 0 ;
int char_count [ 26 ] ;
for ( int i = 0 ;
i < 26 ;
i ++ ) {
char_count [ i ] = 0 ;
}
for ( int i = 0 ;
i < s1 . length ( ) ;
i ++ ) char_count [ s1 [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < s2 . length ( ) ;
i ++ ) {
... |
cpp-java-COUNT_NUMBER_WAYS_REACH_GIVEN_SCORE_GAME | cpp-java | int count ( int n ) {
int table [ n + 1 ] , i ;
for ( int j = 0 ;
j < n + 1 ;
j ++ ) table [ j ] = 0 ;
table [ 0 ] = 1 ;
for ( i = 3 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 3 ] ;
for ( i = 5 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 5 ] ;
for ( i = 10 ;
i <= n ;
i ++ ) table [ i ] +=... | static int count ( int n ) {
int table [ ] = new int [ n + 1 ] , i ;
Arrays . fill ( table , 0 ) ;
table [ 0 ] = 1 ;
for ( i = 3 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 3 ] ;
for ( i = 5 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 5 ] ;
for ( i = 10 ;
i <= n ;
i ++ ) table [ i ] += table [... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int count ( int n ) {
int table [ n + 1 ] , i ;
for ( int j = 0 ;
j < n + 1 ;
j ++ ) table [ j ] = 0 ;
table [ 0 ] = 1 ;
for ( i = 3 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 3 ] ;
for ( i = 5 ;
i <= n ;
i ++ ) table [ i ] += table [ i - 5 ] ;
for ( i = 10 ;
i <= n ;
i ++ ) table [... |
cpp-java-FIND_LENGTH_LONGEST_SUBSEQUENCE_ONE_STRING_SUBSTRING_ANOTHER_STRING | cpp-java | int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) {
int dp [ MAX ] [ MAX ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( x [ j - 1 ] == y [ ... | static int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) {
int dp [ ] [ ] = new int [ MAX ] [ MAX ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxSubsequenceSubstring ( char x [ ] , char y [ ] , int n , int m ) {
int dp [ MAX ] [ MAX ] ;
for ( int i = 0 ;
i <= m ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( x [ j - 1 ] ... |
cpp-java-LONGEST_REPEATING_SUBSEQUENCE | cpp-java | int findLongestRepeatingSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( str [ i - 1 ... | static int findLongestRepeatingSubSeq ( String str ) {
int n = str . length ( ) ;
int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( str . charAt ( i - 1 ) == str . charAt ( j - 1 ) && i != j ) dp [ i ] [ j ] = 1 + dp [... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int findLongestRepeatingSubSeq ( string str ) {
int n = str . length ( ) ;
int dp [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) for ( int j = 0 ;
j <= n ;
j ++ ) dp [ i ] [ j ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= n ;
j ++ ) {
if ( str [... |
cpp-java-PROGRAM_FIND_STRING_START_END_GEEKS | cpp-java | bool isCornerPresent ( string str , string corner ) {
int n = str . length ( ) ;
int cl = corner . length ( ) ;
if ( n < cl ) return false ;
return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ;
}
| static boolean isCornerPresent ( String str , String corner ) {
int n = str . length ( ) ;
int cl = corner . length ( ) ;
if ( n < cl ) return false ;
return ( str . substring ( 0 , cl ) . equals ( corner ) && str . substring ( n - cl , n ) . equals ( corner ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isCornerPresent ( string str , string corner ) {
int n = str . length ( ) ;
int cl = corner . length ( ) ;
if ( n < cl ) return false ;
return ( str . substr ( 0 , cl ) . compare ( corner ) == 0 && str . substr ( n - cl , cl ) . compare ( corner ) == 0 ) ;
}
</Code> |
cpp-java-MEDIAN_OF_TWO_SORTED_ARRAYS | cpp-java | int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) {
int i = 0 ;
int j = 0 ;
int count ;
int m1 = - 1 , m2 = - 1 ;
for ( count = 0 ;
count <= n ;
count ++ ) {
if ( i == n ) {
m1 = m2 ;
m2 = ar2 [ 0 ] ;
break ;
}
else if ( j == n ) {
m1 = m2 ;
m2 = ar1 [ 0 ] ;
... | static int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) {
int i = 0 ;
int j = 0 ;
int count ;
int m1 = - 1 , m2 = - 1 ;
for ( count = 0 ;
count <= n ;
count ++ ) {
if ( i == n ) {
m1 = m2 ;
m2 = ar2 [ 0 ] ;
break ;
}
else if ( j == n ) {
m1 = m2 ;
m2 = ar1 [ 0 ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int getMedian ( int ar1 [ ] , int ar2 [ ] , int n ) {
int i = 0 ;
int j = 0 ;
int count ;
int m1 = - 1 , m2 = - 1 ;
for ( count = 0 ;
count <= n ;
count ++ ) {
if ( i == n ) {
m1 = m2 ;
m2 = ar2 [ 0 ] ;
break ;
}
else if ( j == n ) {
m1 = m2 ;
m2 = ar1 [ 0 ... |
cpp-java-COUNT_ZEROS_IN_A_ROW_WISE_AND_COLUMN_WISE_SORTED_MATRIX | cpp-java | int countZeroes ( int mat [ N ] [ N ] ) {
int row = N - 1 , col = 0 ;
int count = 0 ;
while ( col < N ) {
while ( mat [ row ] [ col ] ) if ( -- row < 0 ) return count ;
count += ( row + 1 ) ;
col ++ ;
}
return count ;
}
| static int countZeroes ( int mat [ ] [ ] ) {
int row = N - 1 , col = 0 ;
int count = 0 ;
while ( col < N ) {
while ( mat [ row ] [ col ] > 0 ) if ( -- row < 0 ) return count ;
count += ( row + 1 ) ;
col ++ ;
}
return count ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int countZeroes ( int mat [ N ] [ N ] ) {
int row = N - 1 , col = 0 ;
int count = 0 ;
while ( col < N ) {
while ( mat [ row ] [ col ] ) if ( -- row < 0 ) return count ;
count += ( row + 1 ) ;
col ++ ;
}
return count ;
}
</Code> |
cpp-java-JUMP_SEARCH | cpp-java | int jumpSearch ( int arr [ ] , int x , int n ) {
int step = sqrt ( n ) ;
int prev = 0 ;
while ( arr [ min ( step , n ) - 1 ] < x ) {
prev = step ;
step += sqrt ( n ) ;
if ( prev >= n ) return - 1 ;
}
while ( arr [ prev ] < x ) {
prev ++ ;
if ( prev == min ( step , n ) ) return - 1 ;
}
... | public static int jumpSearch ( int [ ] arr , int x ) {
int n = arr . length ;
int step = ( int ) Math . floor ( Math . sqrt ( n ) ) ;
int prev = 0 ;
while ( arr [ Math . min ( step , n ) - 1 ] < x ) {
prev = step ;
step += ( int ) Math . floor ( Math . sqrt ( n ) ) ;
if ( prev >= n ) return - 1 ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int jumpSearch ( int arr [ ] , int x , int n ) {
int step = sqrt ( n ) ;
int prev = 0 ;
while ( arr [ min ( step , n ) - 1 ] < x ) {
prev = step ;
step += sqrt ( n ) ;
if ( prev >= n ) return - 1 ;
}
while ( arr [ prev ] < x ) {
prev ++ ;
if ( prev == min ( step , n ) ) return - 1 ;... |
cpp-java-FIND_X_Y_SATISFYING_AX_N | cpp-java | void solution ( int a , int b , int n ) {
for ( int i = 0 ;
i * a <= n ;
i ++ ) {
if ( ( n - ( i * a ) ) % b == 0 ) {
cout << "x = " << i << ", y = " << ( n - ( i * a ) ) / b ;
return ;
}
}
cout << "No solution" ;
}
| static void solution ( int a , int b , int n ) {
for ( int i = 0 ;
i * a <= n ;
i ++ ) {
if ( ( n - ( i * a ) ) % b == 0 ) {
System . out . println ( "x = " + i + ", y = " + ( n - ( i * a ) ) / b ) ;
return ;
}
}
System . out . println ( "No solution" ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void solution ( int a , int b , int n ) {
for ( int i = 0 ;
i * a <= n ;
i ++ ) {
if ( ( n - ( i * a ) ) % b == 0 ) {
cout << "x = " << i << ", y = " << ( n - ( i * a ) ) / b ;
return ;
}
}
cout << "No solution" ;
}
</Code> |
cpp-java-DIVIDE_LARGE_NUMBER_REPRESENTED_STRING | cpp-java | string longDivision ( string number , int divisor ) {
string ans ;
int idx = 0 ;
int temp = number [ idx ] - '0' ;
while ( temp < divisor ) temp = temp * 10 + ( number [ ++ idx ] - '0' ) ;
while ( number . size ( ) > idx ) {
ans += ( temp / divisor ) + '0' ;
temp = ( temp % divisor ) * 10 + number [ +... | static String longDivision ( String number , int divisor ) {
String ans = "" ;
int idx = 0 ;
char [ ] num = number . toCharArray ( ) ;
int temp = num [ idx ] - ' 0 ' ;
while ( temp < divisor ) temp = temp * 10 + ( num [ ++ idx ] - ' 0 ' ) ;
idx += 1 ;
while ( num . length > idx ) {
ans += ( temp / div... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
string longDivision ( string number , int divisor ) {
string ans ;
int idx = 0 ;
int temp = number [ idx ] - '0' ;
while ( temp < divisor ) temp = temp * 10 + ( number [ ++ idx ] - '0' ) ;
while ( number . size ( ) > idx ) {
ans += ( temp / divisor ) + '0' ;
temp = ( temp % divisor ) * 10 + num... |
cpp-java-SORT_AN_ARRAY_OF_0S_1S_AND_2S | cpp-java | void sort012 ( int a [ ] , int arr_size ) {
int lo = 0 ;
int hi = arr_size - 1 ;
int mid = 0 ;
while ( mid <= hi ) {
switch ( a [ mid ] ) {
case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ;
break ;
case 1 : mid ++ ;
break ;
case 2 : swap ( a [ mid ] , a [ hi -- ] ) ;
break ;... | static void sort012 ( int a [ ] , int arr_size ) {
int lo = 0 ;
int hi = arr_size - 1 ;
int mid = 0 , temp = 0 ;
while ( mid <= hi ) {
switch ( a [ mid ] ) {
case 0 : {
temp = a [ lo ] ;
a [ lo ] = a [ mid ] ;
a [ mid ] = temp ;
lo ++ ;
mid ++ ;
break ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void sort012 ( int a [ ] , int arr_size ) {
int lo = 0 ;
int hi = arr_size - 1 ;
int mid = 0 ;
while ( mid <= hi ) {
switch ( a [ mid ] ) {
case 0 : swap ( a [ lo ++ ] , a [ mid ++ ] ) ;
break ;
case 1 : mid ++ ;
break ;
case 2 : swap ( a [ mid ] , a [ hi -- ] ) ;
... |
cpp-java-CHECK_IF_A_GIVEN_ARRAY_CAN_REPRESENT_PREORDER_TRAVERSAL_OF_BINARY_SEARCH_TREE | cpp-java | bool canRepresentBST ( int pre [ ] , int n ) {
stack < int > s ;
int root = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( pre [ i ] < root ) return false ;
while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) {
root = s . top ( ) ;
s . pop ( ) ;
}
s . push ( pre [ i ] ) ;
}
... | boolean canRepresentBST ( int pre [ ] , int n ) {
Stack < Integer > s = new Stack < Integer > ( ) ;
int root = Integer . MIN_VALUE ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( pre [ i ] < root ) {
return false ;
}
while ( ! s . empty ( ) && s . peek ( ) < pre [ i ] ) {
root = s . peek ( )... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool canRepresentBST ( int pre [ ] , int n ) {
stack < int > s ;
int root = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( pre [ i ] < root ) return false ;
while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) {
root = s . top ( ) ;
s . pop ( ) ;
}
s . push ( pre [ i ] ) ... |
cpp-java-FIND_K_SUCH_THAT_ALL_ELEMENTS_IN_KTH_ROW_ARE_0_AND_KTH_COLUMN_ARE_1_IN_A_BOOLEAN_MATRIX | cpp-java | int find ( bool arr [ n ] [ n ] ) {
int i = 0 , j = n - 1 ;
int res = - 1 ;
while ( i < n && j >= 0 ) {
if ( arr [ i ] [ j ] == 0 ) {
while ( j >= 0 && ( arr [ i ] [ j ] == 0 || i == j ) ) j -- ;
if ( j == - 1 ) {
res = i ;
break ;
}
else i ++ ;
}
else {
w... | static int find ( boolean arr [ ] [ ] ) {
int i = 0 , j = n - 1 ;
int res = - 1 ;
while ( i < n && j >= 0 ) {
if ( arr [ i ] [ j ] == false ) {
while ( j >= 0 && ( arr [ i ] [ j ] == false || i == j ) ) {
j -- ;
}
if ( j == - 1 ) {
res = i ;
break ;
}
else... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int find ( bool arr [ n ] [ n ] ) {
int i = 0 , j = n - 1 ;
int res = - 1 ;
while ( i < n && j >= 0 ) {
if ( arr [ i ] [ j ] == 0 ) {
while ( j >= 0 && ( arr [ i ] [ j ] == 0 || i == j ) ) j -- ;
if ( j == - 1 ) {
res = i ;
break ;
}
else i ++ ;
}
else {
... |
cpp-java-POSITIVE_ELEMENTS_EVEN_NEGATIVE_ODD_POSITIONS_1 | cpp-java | void printArray ( int * a , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) cout << a [ i ] << " " ;
cout << endl ;
return ;
}
| static void printArray ( int [ ] a , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) System . out . print ( a [ i ] + " " ) ;
System . out . println ( ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void printArray ( int * a , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) cout << a [ i ] << " " ;
cout << endl ;
return ;
}
</Code> |
cpp-java-SORT_ARRAY_CONVERTING_ELEMENTS_SQUARES_1 | cpp-java | void sortSquares ( int arr [ ] , int n ) {
int K = 0 ;
for ( K = 0 ;
K < n ;
K ++ ) if ( arr [ K ] >= 0 ) break ;
int i = K - 1 ;
int j = K ;
int ind = 0 ;
int temp [ n ] ;
while ( i >= 0 && j < n ) {
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) {
temp [ ind ] = arr [ i ] * arr [ i ]... | public static void sortSquares ( int arr [ ] ) {
int n = arr . length ;
int k ;
for ( k = 0 ;
k < n ;
k ++ ) {
if ( arr [ k ] >= 0 ) break ;
}
int i = k - 1 ;
int j = k ;
int ind = 0 ;
int [ ] temp = new int [ n ] ;
while ( i >= 0 && j < n ) {
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void sortSquares ( int arr [ ] , int n ) {
int K = 0 ;
for ( K = 0 ;
K < n ;
K ++ ) if ( arr [ K ] >= 0 ) break ;
int i = K - 1 ;
int j = K ;
int ind = 0 ;
int temp [ n ] ;
while ( i >= 0 && j < n ) {
if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) {
temp [ ind ] = arr [ i ] * ar... |
cpp-java-PRINT_MAXIMUM_SHORTEST_DISTANCE | cpp-java | int find_maximum ( int a [ ] , int n , int k ) {
unordered_map < int , int > b ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
int d = min ( 1 + i , n - i ) ;
if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ;
else b [ x ] = min ( d , b [ x ] ) ;
}
int ans = INT_MAX ;
for ( int i = ... | static int find_maximum ( int a [ ] , int n , int k ) {
HashMap < Integer , Integer > b = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
int d = Math . min ( 1 + i , n - i ) ;
if ( ! b . containsKey ( x ) ) b . put ( x , d ) ;
else {
b . put ( ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int find_maximum ( int a [ ] , int n , int k ) {
unordered_map < int , int > b ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int x = a [ i ] ;
int d = min ( 1 + i , n - i ) ;
if ( b . find ( x ) == b . end ( ) ) b [ x ] = d ;
else b [ x ] = min ( d , b [ x ] ) ;
}
int ans = INT_MAX ;
for ( i... |
cpp-java-MAXIMUM_CONSECUTIVE_REPEATING_CHARACTER_STRING_1 | cpp-java | char maxRepeating ( string str ) {
int n = str . length ( ) ;
int count = 0 ;
char res = str [ 0 ] ;
int cur_count = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ;
else {
if ( cur_count > count ) {
count = cur_count ;
res ... | static char maxRepeating ( String str ) {
int n = str . length ( ) ;
int count = 0 ;
char res = str . charAt ( 0 ) ;
int cur_count = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) cur_count ++ ;
else {
if ( cur_count > count ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
char maxRepeating ( string str ) {
int n = str . length ( ) ;
int count = 0 ;
char res = str [ 0 ] ;
int cur_count = 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( i < n - 1 && str [ i ] == str [ i + 1 ] ) cur_count ++ ;
else {
if ( cur_count > count ) {
count = cur_count ;
... |
cpp-java-ARRAY_RANGE_QUERIES_ELEMENTS_FREQUENCY_VALUE | cpp-java | int solveQuery ( int start , int end , int arr [ ] ) {
unordered_map < int , int > frequency ;
for ( int i = start ;
i <= end ;
i ++ ) frequency [ arr [ i ] ] ++ ;
int count = 0 ;
for ( auto x : frequency ) if ( x . first == x . second ) count ++ ;
return count ;
}
| static int solveQuery ( int start , int end , int arr [ ] ) {
Map < Integer , Integer > mp = new HashMap < > ( ) ;
for ( int i = start ;
i <= end ;
i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ;
int count = 0 ;
for ( Map . Entry < Integer , Integer > entry ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int solveQuery ( int start , int end , int arr [ ] ) {
unordered_map < int , int > frequency ;
for ( int i = start ;
i <= end ;
i ++ ) frequency [ arr [ i ] ] ++ ;
int count = 0 ;
for ( auto x : frequency ) if ( x . first == x . second ) count ++ ;
return count ;
}
</Code> |
cpp-java-PYTHON_PROGRAM_FIND_PERIMETER_CIRCUMFERENCE_SQUARE_RECTANGLE | cpp-java | int Circumference ( int a ) {
return 4 * a ;
}
| int Circumference ( int a ) {
return 4 * a ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int Circumference ( int a ) {
return 4 * a ;
}
</Code> |
cpp-java-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_3 | cpp-java | int numberOfPaths ( int m , int n ) {
int path = 1 ;
for ( int i = n ;
i < ( m + n - 1 ) ;
i ++ ) {
path *= i ;
path /= ( i - n + 1 ) ;
}
return path ;
}
| static int numberOfPaths ( int m , int n ) {
int path = 1 ;
for ( int i = n ;
i < ( m + n - 1 ) ;
i ++ ) {
path *= i ;
path /= ( i - n + 1 ) ;
}
return path ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int numberOfPaths ( int m , int n ) {
int path = 1 ;
for ( int i = n ;
i < ( m + n - 1 ) ;
i ++ ) {
path *= i ;
path /= ( i - n + 1 ) ;
}
return path ;
}
</Code> |
cpp-java-POINT_CLIPPING_ALGORITHM_COMPUTER_GRAPHICS | cpp-java | void pointClip ( int XY [ ] [ 2 ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) {
cout << "Point inside the viewing pane:" << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) {
if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i ] [ 1 ] <= Ymax ... | static void pointClip ( int XY [ ] [ ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) {
System . out . printf ( "Point inside the viewing pane:\n" ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) {
if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void pointClip ( int XY [ ] [ 2 ] , int n , int Xmin , int Ymin , int Xmax , int Ymax ) {
cout << "Point inside the viewing pane:" << endl ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( ( XY [ i ] [ 0 ] >= Xmin ) && ( XY [ i ] [ 0 ] <= Xmax ) ) {
if ( ( XY [ i ] [ 1 ] >= Ymin ) && ( XY [ i ] [ 1 ] <... |
cpp-java-MAXIMUM_SUM_INCREASING_SUBSEQUENCE_FROM_A_PREFIX_AND_A_GIVEN_ELEMENT_AFTER_PREFIX_IS_MUST | cpp-java | ll pre_compute ( ll a [ ] , ll n , ll index , ll k ) {
ll dp [ n ] [ n ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ;
else dp [ 0 ] [ i ] = a [ i ] ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( int j = 0... | static int pre_compute ( int a [ ] , int n , int index , int k ) {
int dp [ ] [ ] = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ;
else dp [ 0 ] [ i ] = a [ i ] ;
}
for ( int i = 1 ;
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>
ll pre_compute ( ll a [ ] , ll n , ll index , ll k ) {
ll dp [ n ] [ n ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > a [ 0 ] ) dp [ 0 ] [ i ] = a [ i ] + a [ 0 ] ;
else dp [ 0 ] [ i ] = a [ i ] ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
for ( in... |
cpp-java-SPLIT_ARRAY_ADD_FIRST_PART_END | cpp-java | void splitArr ( int arr [ ] , int n , int k ) {
for ( int i = 0 ;
i < k ;
i ++ ) {
int x = arr [ 0 ] ;
for ( int j = 0 ;
j < n - 1 ;
++ j ) arr [ j ] = arr [ j + 1 ] ;
arr [ n - 1 ] = x ;
}
}
| public static void splitArr ( int arr [ ] , int n , int k ) {
for ( int i = 0 ;
i < k ;
i ++ ) {
int x = arr [ 0 ] ;
for ( int j = 0 ;
j < n - 1 ;
++ j ) arr [ j ] = arr [ j + 1 ] ;
arr [ n - 1 ] = x ;
}
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void splitArr ( int arr [ ] , int n , int k ) {
for ( int i = 0 ;
i < k ;
i ++ ) {
int x = arr [ 0 ] ;
for ( int j = 0 ;
j < n - 1 ;
++ j ) arr [ j ] = arr [ j + 1 ] ;
arr [ n - 1 ] = x ;
}
}
</Code> |
cpp-java-MINIMUM_LENGTH_SUBARRAY_SUM_GREATER_GIVEN_VALUE_1 | cpp-java | int smallestSubWithSum ( int arr [ ] , int n , int x ) {
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while ( end < n ) {
while ( curr_sum <= x && end < n ) {
if ( curr_sum <= 0 && x > 0 ) {
start = end ;
curr_sum = 0 ;
}
curr_sum += arr [ end ++ ] ;
}
... | static int smallestSubWithSum ( int arr [ ] , int n , int x ) {
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while ( end < n ) {
while ( curr_sum <= x && end < n ) {
if ( curr_sum <= 0 && x > 0 ) {
start = end ;
curr_sum = 0 ;
}
curr_sum += arr [ end ++ ] ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int smallestSubWithSum ( int arr [ ] , int n , int x ) {
int curr_sum = 0 , min_len = n + 1 ;
int start = 0 , end = 0 ;
while ( end < n ) {
while ( curr_sum <= x && end < n ) {
if ( curr_sum <= 0 && x > 0 ) {
start = end ;
curr_sum = 0 ;
}
curr_sum += arr [ end ++ ] ;
... |
cpp-java-ARRAY_ELEMENT_MOVED_K_USING_SINGLE_MOVES | cpp-java | int winner ( int a [ ] , int n , int k ) {
if ( k >= n - 1 ) return n ;
int best = 0 , times = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > best ) {
best = a [ i ] ;
if ( i ) times = 1 ;
}
else times += 1 ;
if ( times >= k ) return best ;
}
return best ;
}
| static int winner ( int a [ ] , int n , int k ) {
if ( k >= n - 1 ) return n ;
int best = 0 , times = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > best ) {
best = a [ i ] ;
if ( i == 1 ) times = 1 ;
}
else times += 1 ;
if ( times >= k ) return best ;
}
return best ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int winner ( int a [ ] , int n , int k ) {
if ( k >= n - 1 ) return n ;
int best = 0 , times = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( a [ i ] > best ) {
best = a [ i ] ;
if ( i ) times = 1 ;
}
else times += 1 ;
if ( times >= k ) return best ;
}
return best ;
}
</Co... |
cpp-java-CHECK_LARGE_NUMBER_DIVISIBLE_20 | cpp-java | bool divisibleBy20 ( string num ) {
int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ;
return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ;
}
| static Boolean divisibleBy20 ( String num ) {
int lastTwoDigits = Integer . parseInt ( num . substring ( num . length ( ) - 2 , num . length ( ) ) ) ;
return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool divisibleBy20 ( string num ) {
int lastTwoDigits = stoi ( num . substr ( num . length ( ) - 2 , num . length ( ) - 1 ) ) ;
return ( ( lastTwoDigits % 5 == 0 ) && ( lastTwoDigits % 4 == 0 ) ) ;
}
</Code> |
cpp-java-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2_1 | cpp-java | int countSolutions ( int n ) {
int x = 0 , yCount , res = 0 ;
for ( yCount = 0 ;
yCount * yCount < n ;
yCount ++ ) ;
while ( yCount != 0 ) {
res += yCount ;
x ++ ;
while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ;
}
return res ;
}
| static int countSolutions ( int n ) {
int x = 0 , yCount , res = 0 ;
for ( yCount = 0 ;
yCount * yCount < n ;
yCount ++ ) ;
while ( yCount != 0 ) {
res += yCount ;
x ++ ;
while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ;
}
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 x = 0 , yCount , res = 0 ;
for ( yCount = 0 ;
yCount * yCount < n ;
yCount ++ ) ;
while ( yCount != 0 ) {
res += yCount ;
x ++ ;
while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ;
}
return res ;
}
</Code> |
cpp-java-FIND_NUMBER_OF_TRIANGLES_POSSIBLE_1 | cpp-java | void CountTriangles ( vector < int > A ) {
int n = A . size ( ) ;
sort ( A . begin ( ) , A . end ( ) ) ;
int count = 0 ;
for ( int i = n - 1 ;
i >= 1 ;
i -- ) {
int l = 0 , r = i - 1 ;
while ( l < r ) {
if ( A [ l ] + A [ r ] > A [ i ] ) {
count += r - l ;
r -- ;
}
... | static void CountTriangles ( int [ ] A ) {
int n = A . length ;
Arrays . sort ( A ) ;
int count = 0 ;
for ( int i = n - 1 ;
i >= 1 ;
i -- ) {
int l = 0 , r = i - 1 ;
while ( l < r ) {
if ( A [ l ] + A [ r ] > A [ i ] ) {
count += r - l ;
r -- ;
}
else {
l ++... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void CountTriangles ( vector < int > A ) {
int n = A . size ( ) ;
sort ( A . begin ( ) , A . end ( ) ) ;
int count = 0 ;
for ( int i = n - 1 ;
i >= 1 ;
i -- ) {
int l = 0 , r = i - 1 ;
while ( l < r ) {
if ( A [ l ] + A [ r ] > A [ i ] ) {
count += r - l ;
r -- ;
}... |
cpp-java-NEXT_HIGHER_NUMBER_WITH_SAME_NUMBER_OF_SET_BITS | cpp-java | uint_t snoob ( uint_t x ) {
uint_t rightOne ;
uint_t nextHigherOneBit ;
uint_t rightOnesPattern ;
uint_t next = 0 ;
if ( x ) {
rightOne = x & - ( signed ) x ;
nextHigherOneBit = x + rightOne ;
rightOnesPattern = x ^ nextHigherOneBit ;
rightOnesPattern = ( rightOnesPattern ) / rightOne ;
ri... | static int snoob ( int x ) {
int rightOne , nextHigherOneBit , rightOnesPattern , next = 0 ;
if ( x > 0 ) {
rightOne = x & - x ;
nextHigherOneBit = x + rightOne ;
rightOnesPattern = x ^ nextHigherOneBit ;
rightOnesPattern = ( rightOnesPattern ) / rightOne ;
rightOnesPattern >>= 2 ;
next = ne... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
uint_t snoob ( uint_t x ) {
uint_t rightOne ;
uint_t nextHigherOneBit ;
uint_t rightOnesPattern ;
uint_t next = 0 ;
if ( x ) {
rightOne = x & - ( signed ) x ;
nextHigherOneBit = x + rightOne ;
rightOnesPattern = x ^ nextHigherOneBit ;
rightOnesPattern = ( rightOnesPattern ) / rightOne ;... |
cpp-java-CHECK_ARRAY_REPRESENTS_INORDER_BINARY_SEARCH_TREE_NOT | cpp-java | bool isInorder ( int arr [ ] , int n ) {
if ( n == 0 || n == 1 ) return true ;
for ( int i = 1 ;
i < n ;
i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ;
return true ;
}
| static boolean isInorder ( int [ ] arr , int n ) {
if ( n == 0 || n == 1 ) {
return true ;
}
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( arr [ i - 1 ] > arr [ i ] ) {
return false ;
}
}
return true ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
bool isInorder ( int arr [ ] , int n ) {
if ( n == 0 || n == 1 ) return true ;
for ( int i = 1 ;
i < n ;
i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false ;
return true ;
}
</Code> |
cpp-java-LONGEST_INCREASING_SUBSEQUENCE | cpp-java | int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
| static int lis ( int arr [ ] , int n ) {
max_ref = 1 ;
_lis ( arr , n ) ;
return max_ref ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int lis ( int arr [ ] , int n ) {
int max = 1 ;
_lis ( arr , n , & max ) ;
return max ;
}
</Code> |
cpp-java-FIND_MAXIMUM_PRODUCT_OF_A_TRIPLET_IN_ARRAY | cpp-java | int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) return - 1 ;
int max_product = INT_MIN ;
for ( int i = 0 ;
i < n - 2 ;
i ++ ) for ( int j = i + 1 ;
j < n - 1 ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ;
return max_product... | static int maxProduct ( int [ ] arr , int n ) {
if ( n < 3 ) return - 1 ;
int max_product = Integer . MIN_VALUE ;
for ( int i = 0 ;
i < n - 2 ;
i ++ ) for ( int j = i + 1 ;
j < n - 1 ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) max_product = Math . max ( max_product , arr [ i ] * arr [ j ] * arr [ k ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxProduct ( int arr [ ] , int n ) {
if ( n < 3 ) return - 1 ;
int max_product = INT_MIN ;
for ( int i = 0 ;
i < n - 2 ;
i ++ ) for ( int j = i + 1 ;
j < n - 1 ;
j ++ ) for ( int k = j + 1 ;
k < n ;
k ++ ) max_product = max ( max_product , arr [ i ] * arr [ j ] * arr [ k ] ) ;
return max_... |
cpp-java-FREQUENCY_ELEMENT_UNSORTED_ARRAY | cpp-java | void countFreq ( int a [ ] , int n ) {
unordered_map < int , int > hm ;
for ( int i = 0 ;
i < n ;
i ++ ) hm [ a [ i ] ] ++ ;
set < pair < int , int >> st ;
for ( auto x : hm ) {
st . insert ( {
x . first , x . second }
) ;
}
int cumul = 0 ;
for ( auto x : st ) {
cumul += x ... | static void countFreq ( int [ ] a , int n ) {
HashMap < Integer , Integer > hm = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) hm . put ( a [ i ] , hm . get ( a [ i ] ) == null ? 1 : hm . get ( a [ i ] ) + 1 ) ;
SortedMap < Integer , Integer > st = new TreeMap < > ( ) ;
for ( HashMap . Entry < Inte... | 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 ] ] ++ ;
set < pair < int , int >> st ;
for ( auto x : hm ) {
st . insert ( {
x . first , x . second }
) ;
}
int cumul = 0 ;
for ( auto x : st ) {
cumu... |
cpp-java-COMPUTE_NCR_P_SET_1_INTRODUCTION_AND_DYNAMIC_PROGRAMMING_SOLUTION | cpp-java | int nCrModp ( int n , int r , int p ) {
int C [ r + 1 ] ;
memset ( C , 0 , sizeof ( C ) ) ;
C [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = min ( i , r ) ;
j > 0 ;
j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ;
}
return C [ r ] ;
}
| static int nCrModp ( int n , int r , int p ) {
int C [ ] = new int [ r + 1 ] ;
Arrays . fill ( C , 0 ) ;
C [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = Math . min ( i , r ) ;
j > 0 ;
j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ;
}
return C [ r ] ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int nCrModp ( int n , int r , int p ) {
int C [ r + 1 ] ;
memset ( C , 0 , sizeof ( C ) ) ;
C [ 0 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = min ( i , r ) ;
j > 0 ;
j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ;
}
return C [ r ] ;
}
</Code> |
cpp-java-COCKTAIL_SORT | cpp-java | void CocktailSort ( int a [ ] , int n ) {
bool swapped = true ;
int start = 0 ;
int end = n - 1 ;
while ( swapped ) {
swapped = false ;
for ( int i = start ;
i < end ;
++ i ) {
if ( a [ i ] > a [ i + 1 ] ) {
swap ( a [ i ] , a [ i + 1 ] ) ;
swapped = true ;
}
}
... | void cocktailSort ( int a [ ] ) {
boolean swapped = true ;
int start = 0 ;
int end = a . length ;
while ( swapped == true ) {
swapped = false ;
for ( int i = start ;
i < end - 1 ;
++ i ) {
if ( a [ i ] > a [ i + 1 ] ) {
int temp = a [ i ] ;
a [ i ] = a [ i + 1 ] ;
a... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void CocktailSort ( int a [ ] , int n ) {
bool swapped = true ;
int start = 0 ;
int end = n - 1 ;
while ( swapped ) {
swapped = false ;
for ( int i = start ;
i < end ;
++ i ) {
if ( a [ i ] > a [ i + 1 ] ) {
swap ( a [ i ] , a [ i + 1 ] ) ;
swapped = true ;
}
... |
cpp-java-WORD_WRAP_PROBLEM_SPACE_OPTIMIZED_SOLUTION | cpp-java | void solveWordWrap ( int arr [ ] , int n , int k ) {
int i , j ;
int currlen ;
int cost ;
int dp [ n ] ;
int ans [ n ] ;
dp [ n - 1 ] = 0 ;
ans [ n - 1 ] = n - 1 ;
for ( i = n - 2 ;
i >= 0 ;
i -- ) {
currlen = - 1 ;
dp [ i ] = INT_MAX ;
for ( j = i ;
j < n ;
j ++ ) {
currle... | static void solveWordWrap ( int arr [ ] , int n , int k ) {
int i , j ;
int currlen ;
int cost ;
int dp [ ] = new int [ n ] ;
int ans [ ] = new int [ n ] ;
dp [ n - 1 ] = 0 ;
ans [ n - 1 ] = n - 1 ;
for ( i = n - 2 ;
i >= 0 ;
i -- ) {
currlen = - 1 ;
dp [ i ] = Integer . MAX_VALUE ;
for ... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void solveWordWrap ( int arr [ ] , int n , int k ) {
int i , j ;
int currlen ;
int cost ;
int dp [ n ] ;
int ans [ n ] ;
dp [ n - 1 ] = 0 ;
ans [ n - 1 ] = n - 1 ;
for ( i = n - 2 ;
i >= 0 ;
i -- ) {
currlen = - 1 ;
dp [ i ] = INT_MAX ;
for ( j = i ;
j < n ;
j ++ ) {
... |
cpp-java-BINARY_SEARCH_1 | cpp-java | int binarySearch ( int arr [ ] , int l , int r , int x ) {
while ( l <= r ) {
int m = l + ( r - l ) / 2 ;
if ( arr [ m ] == x ) return m ;
if ( arr [ m ] < x ) l = m + 1 ;
else r = m - 1 ;
}
return - 1 ;
}
| int binarySearch ( int arr [ ] , int x ) {
int l = 0 , r = arr . length - 1 ;
while ( l <= r ) {
int m = l + ( r - l ) / 2 ;
if ( arr [ m ] == x ) return m ;
if ( arr [ m ] < x ) l = m + 1 ;
else r = m - 1 ;
}
return - 1 ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int binarySearch ( int arr [ ] , int l , int r , int x ) {
while ( l <= r ) {
int m = l + ( r - l ) / 2 ;
if ( arr [ m ] == x ) return m ;
if ( arr [ m ] < x ) l = m + 1 ;
else r = m - 1 ;
}
return - 1 ;
}
</Code> |
cpp-java-MINIMUM_XOR_VALUE_PAIR | cpp-java | int minXOR ( int arr [ ] , int n ) {
int min_xor = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) min_xor = min ( min_xor , arr [ i ] ^ arr [ j ] ) ;
return min_xor ;
}
| static int minXOR ( int arr [ ] , int n ) {
int min_xor = Integer . MAX_VALUE ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) min_xor = Math . min ( min_xor , arr [ i ] ^ arr [ j ] ) ;
return min_xor ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int minXOR ( int arr [ ] , int n ) {
int min_xor = INT_MAX ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) min_xor = min ( min_xor , arr [ i ] ^ arr [ j ] ) ;
return min_xor ;
}
</Code> |
cpp-java-FIND_SUM_EVEN_FACTORS_NUMBER | cpp-java | int sumofFactors ( int n ) {
if ( n % 2 != 0 ) return 0 ;
int res = 1 ;
for ( int i = 2 ;
i <= sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 , curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
if ( i == 2 && count == 1 ) curr_sum = 0 ;
curr_term *= i ;
cu... | public static int sumofFactors ( int n ) {
if ( n % 2 != 0 ) return 0 ;
int res = 1 ;
for ( int i = 2 ;
i <= Math . sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 ;
int curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
if ( i == 2 && count == 1 ) curr_sum = 0 ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int sumofFactors ( int n ) {
if ( n % 2 != 0 ) return 0 ;
int res = 1 ;
for ( int i = 2 ;
i <= sqrt ( n ) ;
i ++ ) {
int count = 0 , curr_sum = 1 , curr_term = 1 ;
while ( n % i == 0 ) {
count ++ ;
n = n / i ;
if ( i == 2 && count == 1 ) curr_sum = 0 ;
curr_term *= i ;
... |
cpp-java-FIRST_UPPERCASE_LETTER_IN_A_STRING_ITERATIVE_AND_RECURSIVE_1 | cpp-java | char first ( string str , int i = 0 ) {
if ( str [ i ] == '\0' ) return 0 ;
if ( isupper ( str [ i ] ) ) return str [ i ] ;
return first ( str , i + 1 ) ;
}
| static char first ( String str , int i ) {
if ( str . charAt ( i ) == ' \0 ' ) return 0 ;
if ( Character . isUpperCase ( str . charAt ( i ) ) ) return str . charAt ( i ) ;
return first ( str , i + 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
char first ( string str , int i = 0 ) {
if ( str [ i ] == '\0' ) return 0 ;
if ( isupper ( str [ i ] ) ) return str [ i ] ;
return first ( str , i + 1 ) ;
}
</Code> |
cpp-java-MAXIMUM_GAMES_PLAYED_WINNER | cpp-java | int maxGameByWinner ( int N ) {
int dp [ N ] ;
dp [ 0 ] = 1 ;
dp [ 1 ] = 2 ;
int i = 2 ;
do {
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
}
while ( dp [ i ++ ] <= N ) ;
return ( i - 2 ) ;
}
| static int maxGameByWinner ( int N ) {
int [ ] dp = new int [ N ] ;
dp [ 0 ] = 1 ;
dp [ 1 ] = 2 ;
int i = 2 ;
do {
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
}
while ( dp [ i ++ ] <= N ) ;
return ( i - 2 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
int maxGameByWinner ( int N ) {
int dp [ N ] ;
dp [ 0 ] = 1 ;
dp [ 1 ] = 2 ;
int i = 2 ;
do {
dp [ i ] = dp [ i - 1 ] + dp [ i - 2 ] ;
}
while ( dp [ i ++ ] <= N ) ;
return ( i - 2 ) ;
}
</Code> |
cpp-java-EQUILIBRIUM_INDEX_OF_AN_ARRAY_1 | cpp-java | int equilibrium ( int arr [ ] , int n ) {
int sum = 0 ;
int leftsum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) sum += arr [ i ] ;
for ( int i = 0 ;
i < n ;
++ i ) {
sum -= arr [ i ] ;
if ( leftsum == sum ) return i ;
leftsum += arr [ i ] ;
}
return - 1 ;
}
| int equilibrium ( int arr [ ] , int n ) {
int sum = 0 ;
int leftsum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) sum += arr [ i ] ;
for ( int i = 0 ;
i < n ;
++ i ) {
sum -= arr [ i ] ;
if ( leftsum == sum ) return i ;
leftsum += arr [ i ] ;
}
return - 1 ;
}
| 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 sum = 0 ;
int leftsum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) sum += arr [ i ] ;
for ( int i = 0 ;
i < n ;
++ i ) {
sum -= arr [ i ] ;
if ( leftsum == sum ) return i ;
leftsum += arr [ i ] ;
}
return - 1 ;
}
</Code> |
cpp-java-PROBABILITY_THREE_RANDOMLY_CHOSEN_NUMBERS_AP | cpp-java | double procal ( int n ) {
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ;
}
| static double procal ( int n ) {
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ;
}
| Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
double procal ( int n ) {
return ( 3.0 * n ) / ( 4.0 * ( n * n ) - 1 ) ;
}
</Code> |
cpp-java-REARRANGE_ARRAY_MAXIMUM_MINIMUM_FORM | cpp-java | void rearrange ( int arr [ ] , int n ) {
int temp [ n ] ;
int small = 0 , large = n - 1 ;
int flag = true ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( flag ) temp [ i ] = arr [ large -- ] ;
else temp [ i ] = arr [ small ++ ] ;
flag = ! flag ;
}
for ( int i = 0 ;
i < n ;
i ++ ) arr [ i ] = t... | static void rearrange ( int [ ] arr , int n ) {
int temp [ ] = new int [ n ] ;
int small = 0 , large = n - 1 ;
boolean flag = true ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( flag ) temp [ i ] = arr [ large -- ] ;
else temp [ i ] = arr [ small ++ ] ;
flag = ! flag ;
}
arr = temp . clone ( ) ;
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void rearrange ( int arr [ ] , int n ) {
int temp [ n ] ;
int small = 0 , large = n - 1 ;
int flag = true ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( flag ) temp [ i ] = arr [ large -- ] ;
else temp [ i ] = arr [ small ++ ] ;
flag = ! flag ;
}
for ( int i = 0 ;
i < n ;
i ++ ) arr [ ... |
cpp-java-PRINT_DIGITS_POSITION_REMOVED_MAKE_NUMBER_DIVISIBLE_6 | cpp-java | void greatest ( string s ) {
int n = s . length ( ) ;
int a [ n ] ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ i ] = s [ i ] - '0' ;
sum += a [ i ] ;
}
if ( a [ n - 1 ] % 2 ) {
if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) {
cout << "-1" << endl ;
}
el... | static void greatest ( String s ) {
int n = s . length ( ) ;
int [ ] a = new int [ n ] ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ i ] = s . charAt ( i ) - ' 0 ' ;
sum += a [ i ] ;
}
if ( a [ n - 1 ] % 2 != 0 ) {
if ( a [ n - 2 ] % 2 != 0 || ( sum - a [ n - 1 ] ) % 3 != 0 ) {
... | Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>. | <Code>
void greatest ( string s ) {
int n = s . length ( ) ;
int a [ n ] ;
int sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ i ] = s [ i ] - '0' ;
sum += a [ i ] ;
}
if ( a [ n - 1 ] % 2 ) {
if ( a [ n - 2 ] % 2 != 0 or ( sum - a [ n - 1 ] ) % 3 != 0 ) {
cout << "-1" << endl ;
}... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.