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 |
|---|---|---|---|---|---|
python-java-TILING_WITH_DOMINOES | python-java | def countWays ( n ) :
A = [ 0 ] * ( n + 1 )
B = [ 0 ] * ( n + 1 )
A [ 0 ] = 1
A [ 1 ] = 0
B [ 0 ] = 0
B [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ]
B [ i ] = A [ i - 1 ] + B [ i - 2 ]
return A [ n ]
| static int countWays ( int n ) {
int [ ] A = new int [ n + 1 ] ;
int [ ] B = new int [ n + 1 ] ;
A [ 0 ] = 1 ;
A [ 1 ] = 0 ;
B [ 0 ] = 0 ;
B [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ] ;
B [ i ] = A [ i - 1 ] + B [ i - 2 ] ;
}
return A [ n ] ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countWays ( n ) :
A = [ 0 ] * ( n + 1 )
B = [ 0 ] * ( n + 1 )
A [ 0 ] = 1
A [ 1 ] = 0
B [ 0 ] = 0
B [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
A [ i ] = A [ i - 2 ] + 2 * B [ i - 1 ]
B [ i ] = A [ i - 1 ] + B [ i - 2 ]
return A [ n ]
</Code> |
python-java-DYNAMIC_PROGRAMMING_SET_4_LONGEST_COMMON_SUBSEQUENCE | python-java | def lcs ( X , Y , m , n ) :
if m == 0 or n == 0 :
return 0 ;
elif X [ m - 1 ] == Y [ n - 1 ] :
return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else :
return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
| int lcs ( char [ ] X , char [ ] Y , int m , int n ) {
if ( m == 0 || n == 0 ) return 0 ;
if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def lcs ( X , Y , m , n ) :
if m == 0 or n == 0 :
return 0 ;
elif X [ m - 1 ] == Y [ n - 1 ] :
return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else :
return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
</Code> |
python-java-MINIMIZE_SUM_PRODUCT_TWO_ARRAYS_PERMUTATIONS_ALLOWED | python-java | def minValue ( A , B , n ) :
sorted ( A )
sorted ( B )
result = 0
for i in range ( n ) :
result += ( A [ i ] * B [ n - i - 1 ] )
return result
| static int minValue ( int A [ ] , int B [ ] , int n ) {
Arrays . sort ( A ) ;
Arrays . sort ( B ) ;
int result = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) result += ( A [ i ] * B [ n - i - 1 ] ) ;
return result ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def minValue ( A , B , n ) :
sorted ( A )
sorted ( B )
result = 0
for i in range ( n ) :
result += ( A [ i ] * B [ n - i - 1 ] )
return result
</Code> |
python-java-CHANGE_BITS_CAN_MADE_ONE_FLIP_1 | python-java | def isOneFlip ( str ) :
sum = 0
n = len ( str )
for i in range ( 0 , n ) :
sum += int ( str [ i ] ) - int ( '0' )
return ( sum == n - 1 or sum == 1 )
| static boolean isOneFlip ( String str ) {
int sum = 0 ;
int n = str . length ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) sum += str . charAt ( i ) - ' 0 ' ;
return ( sum == n - 1 || sum == 1 ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isOneFlip ( str ) :
sum = 0
n = len ( str )
for i in range ( 0 , n ) :
sum += int ( str [ i ] ) - int ( '0' )
return ( sum == n - 1 or sum == 1 )
</Code> |
python-java-DYNAMIC_PROGRAMMING_SET_8_MATRIX_CHAIN_MULTIPLICATION | python-java | def MatrixChainOrder ( p , i , j ) :
if i == j :
return 0
_min = sys.maxsize
for k in range ( i , j ) :
count = ( MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] )
if count < _min :
_min = count
return _min
| static int MatrixChainOrder ( int p [ ] , int i , int j ) {
if ( i == j ) return 0 ;
int min = Integer . MAX_VALUE ;
for ( int k = i ;
k < j ;
k ++ ) {
int count = MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] ;
if ( count < min ) min = count ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def MatrixChainOrder ( p , i , j ) :
if i == j :
return 0
_min = sys.maxsize
for k in range ( i , j ) :
count = ( MatrixChainOrder ( p , i , k ) + MatrixChainOrder ( p , k + 1 , j ) + p [ i - 1 ] * p [ k ] * p [ j ] )
if count < _min :
_min = count
return _min
... |
python-java-MULTIPLY_TWO_NUMBERS_WITHOUT_USING_MULTIPLY_DIVISION_BITWISE_OPERATORS_AND_NO_LOOPS | python-java | def multiply ( x , y ) :
if ( y == 0 ) :
return 0
if ( y > 0 ) :
return ( x + multiply ( x , y - 1 ) )
if ( y < 0 ) :
return - multiply ( x , - y )
| static int multiply ( int x , int y ) {
if ( y == 0 ) return 0 ;
if ( y > 0 ) return ( x + multiply ( x , y - 1 ) ) ;
if ( y < 0 ) return - multiply ( x , - y ) ;
return - 1 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def multiply ( x , y ) :
if ( y == 0 ) :
return 0
if ( y > 0 ) :
return ( x + multiply ( x , y - 1 ) )
if ( y < 0 ) :
return - multiply ( x , - y )
</Code> |
python-java-COUNT_NUMBER_OF_WAYS_TO_PARTITION_A_SET_INTO_K_SUBSETS_1 | python-java | def countP ( n , k ) :
dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ i ] [ 0 ] = 0
for i in range ( k + 1 ) :
dp [ 0 ] [ k ] = 0
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , k + 1 ) :
if ( j == 1 or i == j ) :
... | static int countP ( int n , int k ) {
int [ ] [ ] dp = new int [ n + 1 ] [ k + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) dp [ i ] [ 0 ] = 0 ;
for ( int i = 0 ;
i <= k ;
i ++ ) dp [ 0 ] [ k ] = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 1 ;
j <= k ;
j ++ ) if ( j == 1 || i == j ) dp [ i ] ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countP ( n , k ) :
dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ i ] [ 0 ] = 0
for i in range ( k + 1 ) :
dp [ 0 ] [ k ] = 0
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , k + 1 ) :
if ( j == 1 or i ==... |
python-java-PROGRAM_WORST_FIT_ALGORITHM_MEMORY_MANAGEMENT | python-java | def worstFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
wstIdx = - 1
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
if wstIdx == - 1 :
wstIdx = j
elif blockSize [ wstIdx ]... | static void worstFit ( 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 wstIdx = - 1 ;
for ( int j = 0 ;
j < m ;
j ++ ) {
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def worstFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
wstIdx = - 1
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
if wstIdx == - 1 :
wstIdx = j
elif blockSize [ w... |
python-java-MAXIMIZE_VOLUME_CUBOID_GIVEN_SUM_SIDES_1 | python-java | def maxvolume ( s ) :
length = int ( s / 3 )
s -= length
breadth = s / 2
height = s - breadth
return int ( length * breadth * height )
| static int maxvolume ( int s ) {
int length = s / 3 ;
s -= length ;
int breadth = s / 2 ;
int height = s - breadth ;
return length * breadth * height ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxvolume ( s ) :
length = int ( s / 3 )
s -= length
breadth = s / 2
height = s - breadth
return int ( length * breadth * height )
</Code> |
python-java-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_1 | python-java | def findLength ( string ) :
n = len ( string )
maxlen = 0
Sum = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for i in range ( 0 , n ) :
Sum [ i ] [ i ] = int ( string [ i ] )
for length in range ( 2 , n + 1 ) :
for i in range ( 0 , n - length + 1 ) :
j = i + length... | static int findLength ( String str ) {
int n = str . length ( ) ;
int maxlen = 0 ;
int sum [ ] [ ] = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n ;
i ++ ) sum [ i ] [ i ] = str . charAt ( i ) - ' 0 ' ;
for ( int len = 2 ;
len <= n ;
len ++ ) {
for ( int i = 0 ;
i < n - len + 1 ;
i ++ ) {
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findLength ( string ) :
n = len ( string )
maxlen = 0
Sum = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for i in range ( 0 , n ) :
Sum [ i ] [ i ] = int ( string [ i ] )
for length in range ( 2 , n + 1 ) :
for i in range ( 0 , n - length + 1 ) :
j = i +... |
python-java-FIND_SUBARRAY_WITH_GIVEN_SUM_1 | python-java | def subArraySum ( arr , n , sum ) :
curr_sum = arr [ 0 ]
start = 0
i = 1
while i <= n :
while curr_sum > sum and start < i - 1 :
curr_sum = curr_sum - arr [ start ]
start += 1
if curr_sum == sum :
print ( "Sum found between indexes" )
print... | int subArraySum ( int arr [ ] , int n , int sum ) {
int curr_sum = arr [ 0 ] , start = 0 , i ;
for ( i = 1 ;
i <= n ;
i ++ ) {
while ( curr_sum > sum && start < i - 1 ) {
curr_sum = curr_sum - arr [ start ] ;
start ++ ;
}
if ( curr_sum == sum ) {
int p = i - 1 ;
System . out ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def subArraySum ( arr , n , sum ) :
curr_sum = arr [ 0 ]
start = 0
i = 1
while i <= n :
while curr_sum > sum and start < i - 1 :
curr_sum = curr_sum - arr [ start ]
start += 1
if curr_sum == sum :
print ( "Sum found between indexes" )
... |
python-java-COMMON_CHARACTERS_N_STRINGS | python-java | def commonCharacters ( strings , n ) :
prim = [ True ] * MAX_CHAR
for i in range ( n ) :
sec = [ False ] * MAX_CHAR
for j in range ( len ( strings [ i ] ) ) :
if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ] ) :
sec [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ]... | public static void commonCharacters ( String str [ ] , int n ) {
Boolean [ ] prim = new Boolean [ MAX_CHAR ] ;
Arrays . fill ( prim , new Boolean ( true ) ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
Boolean [ ] sec = new Boolean [ MAX_CHAR ] ;
Arrays . fill ( sec , new Boolean ( false ) ) ;
for ( int j... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def commonCharacters ( strings , n ) :
prim = [ True ] * MAX_CHAR
for i in range ( n ) :
sec = [ False ] * MAX_CHAR
for j in range ( len ( strings [ i ] ) ) :
if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( 'a' ) ] ) :
sec [ ord ( strings [ i ] [ j ] ) - ord ( ... |
python-java-CHECK_WHETHER_SECOND_STRING_CAN_FORMED_FIRST_STRING_USING_COUNT_ARRAY | python-java | def canMakeStr2 ( s1 , s2 ) :
count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) }
for i in range ( len ( s1 ) ) :
count [ s1 [ i ] ] += 1
for i in range ( len ( s2 ) ) :
if count [ s2 [ i ] ] == 0 :
return False
count [ s2 [ i ] ] -= 1
return True
| static boolean canMakeStr2 ( String str1 , String str2 ) {
int [ ] count = new int [ MAX ] ;
char [ ] str3 = str1 . toCharArray ( ) ;
for ( int i = 0 ;
i < str3 . length ;
i ++ ) count [ str3 [ i ] ] ++ ;
char [ ] str4 = str2 . toCharArray ( ) ;
for ( int i = 0 ;
i < str4 . length ;
i ++ ) {
if ( ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def canMakeStr2 ( s1 , s2 ) :
count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) }
for i in range ( len ( s1 ) ) :
count [ s1 [ i ] ] += 1
for i in range ( len ( s2 ) ) :
if count [ s2 [ i ] ] == 0 :
return False
count [ s2 [ i ] ] -= 1
return True
</Code> |
python-java-MAGIC_SQUARE | python-java | def generateSquare ( n ) :
magicSquare = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
i = n / 2
j = n - 1
num = 1
while num <= ( n * n ) :
if i == - 1 and j == n :
j = n - 2
i = 0
else :
if j == n :
j = 0
if i < 0... | static void generateSquare ( int n ) {
int [ ] [ ] magicSquare = new int [ n ] [ n ] ;
int i = n / 2 ;
int j = n - 1 ;
for ( int num = 1 ;
num <= n * n ;
) {
if ( i == - 1 && j == n ) {
j = n - 2 ;
i = 0 ;
}
else {
if ( j == n ) j = 0 ;
if ( i < 0 ) i = n - 1 ;
}
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def generateSquare ( n ) :
magicSquare = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
i = n / 2
j = n - 1
num = 1
while num <= ( n * n ) :
if i == - 1 and j == n :
j = n - 2
i = 0
else :
if j == n :
j = 0
i... |
python-java-FIND_NUMBER_TIMES_STRING_OCCURS_GIVEN_STRING | python-java | def count ( a , b , m , n ) :
if ( ( m == 0 and n == 0 ) or n == 0 ) :
return 1
if ( m == 0 ) :
return 0
if ( a [ m - 1 ] == b [ n - 1 ] ) :
return ( count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) )
else :
return count ( a , b , m - 1 , n )
| static int count ( String a , String b , int m , int n ) {
if ( ( m == 0 && n == 0 ) || n == 0 ) return 1 ;
if ( m == 0 ) return 0 ;
if ( a . charAt ( m - 1 ) == b . charAt ( n - 1 ) ) return count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) ;
else return count ( a , b , m - 1 , n ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def count ( a , b , m , n ) :
if ( ( m == 0 and n == 0 ) or n == 0 ) :
return 1
if ( m == 0 ) :
return 0
if ( a [ m - 1 ] == b [ n - 1 ] ) :
return ( count ( a , b , m - 1 , n - 1 ) + count ( a , b , m - 1 , n ) )
else :
return count ( a , b , m - 1 , n )
</Code> |
python-java-COUNT_ARRAYS_ADJACENT_ELEMENTS_ONE_DIVIDE_ANOTHER | python-java | def numofArray ( n , m ) :
dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
di = [ [ ] for i in range ( MAX ) ]
mu = [ [ ] for i in range ( MAX ) ]
for i in range ( 1 , m + 1 ) :
for j in range ( 2 * i , m + 1 , i ) :
di [ j ].append ( i )
mu [ i ].append ( j ... | static int numofArray ( int n , int m ) {
int [ ] [ ] dp = new int [ MAX ] [ MAX ] ;
Vector < Integer > [ ] di = new Vector [ MAX ] ;
Vector < Integer > [ ] mu = new Vector [ MAX ] ;
for ( int i = 0 ;
i < MAX ;
i ++ ) {
for ( int j = 0 ;
j < MAX ;
j ++ ) {
dp [ i ] [ j ] = 0 ;
}
}
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def numofArray ( n , m ) :
dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ]
di = [ [ ] for i in range ( MAX ) ]
mu = [ [ ] for i in range ( MAX ) ]
for i in range ( 1 , m + 1 ) :
for j in range ( 2 * i , m + 1 , i ) :
di [ j ].append ( i )
mu [ i ].appe... |
python-java-COUNT_1S_SORTED_BINARY_ARRAY | python-java | def countOnes ( arr , low , high ) :
if high >= low :
mid = low + ( high - low ) / 2
if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) :
return mid + 1
if arr [ mid ] == 1 :
return countOnes ( arr , ( mid + 1 ) , high )
return countOnes (... | int countOnes ( bool arr [ ] , int low , int high ) {
if ( high >= low ) {
int mid = low + ( high - low ) / 2 ;
if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ;
if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ;
return countOnes ( arr , low ,... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countOnes ( arr , low , high ) :
if high >= low :
mid = low + ( high - low ) / 2
if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) :
return mid + 1
if arr [ mid ] == 1 :
return countOnes ( arr , ( mid + 1 ) , high )
return coun... |
python-java-CEILING_IN_A_SORTED_ARRAY_1 | python-java | def ceilSearch ( arr , low , high , x ) :
if x <= arr [ low ] :
return low
if x > arr [ high ] :
return - 1
mid = ( low + high ) / 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] < x :
if mid + 1 <= high and x <= arr [ mid + 1 ] :
return mid + 1
... | static int ceilSearch ( int arr [ ] , int low , int high , int x ) {
int mid ;
if ( x <= arr [ low ] ) return low ;
if ( x > arr [ high ] ) return - 1 ;
mid = ( low + high ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
else if ( arr [ mid ] < x ) {
if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def ceilSearch ( arr , low , high , x ) :
if x <= arr [ low ] :
return low
if x > arr [ high ] :
return - 1
mid = ( low + high ) / 2
if arr [ mid ] == x :
return mid
elif arr [ mid ] < x :
if mid + 1 <= high and x <= arr [ mid + 1 ] :
return mid + 1... |
python-java-DECIMAL_BINARY_CONVERSION_WITHOUT_USING_ARITHMETIC_OPERATORS | python-java | def decToBin ( n ) :
if ( n == 0 ) :
return "0"
bin = ""
while ( n > 0 ) :
if ( n & 1 == 0 ) :
bin = '0' + bin
else :
bin = '1' + bin
n = n >> 1
return bin
| static String decToBin ( int n ) {
if ( n == 0 ) return "0" ;
String bin = "" ;
while ( n > 0 ) {
bin = ( ( n & 1 ) == 0 ? ' 0 ' : ' 1 ' ) + bin ;
n >>= 1 ;
}
return bin ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def decToBin ( n ) :
if ( n == 0 ) :
return "0"
bin = ""
while ( n > 0 ) :
if ( n & 1 == 0 ) :
bin = '0' + bin
else :
bin = '1' + bin
n = n >> 1
return bin
</Code> |
python-java-HOW_WILL_YOU_PRINT_NUMBERS_FROM_1_TO_200_WITHOUT_USING_LOOP | python-java | def printNos ( n ) :
if n > 0 :
printNos ( n - 1 )
print ( n , end = ' ' )
| static void printNos ( int n ) {
if ( n > 0 ) {
printNos ( n - 1 ) ;
System . out . print ( n + " " ) ;
}
return ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def printNos ( n ) :
if n > 0 :
printNos ( n - 1 )
print ( n , end = ' ' )
</Code> |
python-java-PREFIX_SUM_2D_ARRAY | python-java | def prefixSum2D ( a ) :
global C , R
psa = [ [ 0 for x in range ( C ) ] for y in range ( R ) ]
psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ]
for i in range ( 1 , C ) :
psa [ 0 ] [ i ] = ( psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] )
for i in range ( 0 , R ) :
psa [ i ] [ 0 ] = ( psa [ i - 1 ] [ 0 ] + a [... | public static void prefixSum2D ( int a [ ] [ ] ) {
int R = a . length ;
int C = a [ 0 ] . length ;
int psa [ ] [ ] = new int [ R ] [ C ] ;
psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ] ;
for ( int i = 1 ;
i < C ;
i ++ ) psa [ 0 ] [ i ] = psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] ;
for ( int i = 1 ;
i < R ;
i ++ ) psa [... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def prefixSum2D ( a ) :
global C , R
psa = [ [ 0 for x in range ( C ) ] for y in range ( R ) ]
psa [ 0 ] [ 0 ] = a [ 0 ] [ 0 ]
for i in range ( 1 , C ) :
psa [ 0 ] [ i ] = ( psa [ 0 ] [ i - 1 ] + a [ 0 ] [ i ] )
for i in range ( 0 , R ) :
psa [ i ] [ 0 ] = ( psa [ i - 1 ] [ 0 ... |
python-java-EULERIAN_NUMBER | python-java | def eulerian ( n , m ) :
if ( m >= n or n == 0 ) :
return 0
if ( m == 0 ) :
return 1
return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) )
| public static int eulerian ( int n , int m ) {
if ( m >= n || n == 0 ) return 0 ;
if ( m == 0 ) return 1 ;
return ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def eulerian ( n , m ) :
if ( m >= n or n == 0 ) :
return 0
if ( m == 0 ) :
return 1
return ( ( n - m ) * eulerian ( n - 1 , m - 1 ) + ( m + 1 ) * eulerian ( n - 1 , m ) )
</Code> |
python-java-FIND_SUM_UNIQUE_SUB_ARRAY_SUM_GIVEN_ARRAY | python-java | def findSubarraySum ( arr , n ) :
res = 0
m = dict ( )
for i in range ( n ) :
Sum = 0
for j in range ( i , n ) :
Sum += arr [ j ]
m [ Sum ] = m.get ( Sum , 0 ) + 1
for x in m :
if m [ x ] == 1 :
res += x
return res
| static int findSubarraySum ( int [ ] arr , int n ) {
int res = 0 ;
HashMap < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int sum = 0 ;
for ( int j = i ;
j < n ;
j ++ ) {
sum += arr [ j ] ;
if ( m . containsKey ( sum ) ) {
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSubarraySum ( arr , n ) :
res = 0
m = dict ( )
for i in range ( n ) :
Sum = 0
for j in range ( i , n ) :
Sum += arr [ j ]
m [ Sum ] = m.get ( Sum , 0 ) + 1
for x in m :
if m [ x ] == 1 :
res += x
return res
</Code> |
python-java-WRITE_AN_EFFICIENT_METHOD_TO_CHECK_IF_A_NUMBER_IS_MULTIPLE_OF_3 | python-java | def isMultipleOf3 ( n ) :
odd_count = 0
even_count = 0
if ( n < 0 ) :
n = - n
if ( n == 0 ) :
return 1
if ( n == 1 ) :
return 0
while ( n ) :
if ( n & 1 ) :
odd_count += 1
if ( n & 2 ) :
even_count += 1
n = n >> 2
return... | static int isMultipleOf3 ( int n ) {
int odd_count = 0 ;
int even_count = 0 ;
if ( n < 0 ) n = - n ;
if ( n == 0 ) return 1 ;
if ( n == 1 ) return 0 ;
while ( n != 0 ) {
if ( ( n & 1 ) != 0 ) odd_count ++ ;
if ( ( n & 2 ) != 0 ) even_count ++ ;
n = n >> 2 ;
}
return isMultipleOf3 ( Math . ab... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isMultipleOf3 ( n ) :
odd_count = 0
even_count = 0
if ( n < 0 ) :
n = - n
if ( n == 0 ) :
return 1
if ( n == 1 ) :
return 0
while ( n ) :
if ( n & 1 ) :
odd_count += 1
if ( n & 2 ) :
even_count += 1
n = n >> 2
... |
python-java-LCS_FORMED_CONSECUTIVE_SEGMENTS_LEAST_LENGTH_K | python-java | def longestSubsequenceCommonSegment ( k , s1 , s2 ) :
n = len ( s1 )
m = len ( s2 )
lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , m + 1 ) :
lc... | static int longestSubsequenceCommonSegment ( int k , String s1 , String s2 ) {
int n = s1 . length ( ) ;
int m = s2 . length ( ) ;
int lcs [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
int cnt [ ] [ ] = new int [ n + 1 ] [ m + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
for ( int j = 1 ;
j <= m ;
j ++... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def longestSubsequenceCommonSegment ( k , s1 , s2 ) :
n = len ( s1 )
m = len ( s2 )
lcs = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
cnt = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , m + 1 ) :
... |
python-java-PROGRAM_AREA_SQUARE | python-java | def areaSquare ( side ) :
area = side * side
return area
| static int areaSquare ( int side ) {
int area = side * side ;
return area ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def areaSquare ( side ) :
area = side * side
return area
</Code> |
python-java-EULERIAN_PATH_UNDIRECTED_GRAPH | python-java | def findpath ( graph ) :
n = len ( graph )
numofadj = list ( )
for i in range ( n ) :
numofadj.append ( sum ( graph [ i ] ) )
startpoint = 0
numofodd = 0
for i in range ( n - 1 , - 1 , - 1 ) :
if ( numofadj [ i ] % 2 == 1 ) :
numofodd += 1
startpoint = i
... | static void findpath ( int [ ] [ ] graph , int n ) {
Vector < Integer > numofadj = new Vector < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) numofadj . add ( accumulate ( graph [ i ] , 0 ) ) ;
int startPoint = 0 , numofodd = 0 ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
if ( numofadj . elementAt ( i ) % 2... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findpath ( graph ) :
n = len ( graph )
numofadj = list ( )
for i in range ( n ) :
numofadj.append ( sum ( graph [ i ] ) )
startpoint = 0
numofodd = 0
for i in range ( n - 1 , - 1 , - 1 ) :
if ( numofadj [ i ] % 2 == 1 ) :
numofodd += 1
startpoin... |
python-java-LCS_LONGEST_COMMON_SUBSEQUENCE_THREE_STRINGS_1 | python-java | def lcsOf3 ( i , j , k ) :
if ( i == - 1 or j == - 1 or k == - 1 ) :
return 0
if ( dp [ i ] [ j ] [ k ] != - 1 ) :
return dp [ i ] [ j ] [ k ]
if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) :
dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 )
return dp [ i ] [ j ] ... | static int lcsOf3 ( int i , int j , int k ) {
if ( i == - 1 || j == - 1 || k == - 1 ) {
return 0 ;
}
if ( dp [ i ] [ j ] [ k ] != - 1 ) {
return dp [ i ] [ j ] [ k ] ;
}
if ( X . charAt ( i ) == Y . charAt ( j ) && Y . charAt ( j ) == Z . charAt ( k ) ) {
return dp [ i ] [ j ] [ k ] = 1 + lcsOf3 (... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def lcsOf3 ( i , j , k ) :
if ( i == - 1 or j == - 1 or k == - 1 ) :
return 0
if ( dp [ i ] [ j ] [ k ] != - 1 ) :
return dp [ i ] [ j ] [ k ]
if ( X [ i ] == Y [ j ] and Y [ j ] == Z [ k ] ) :
dp [ i ] [ j ] [ k ] = 1 + lcsOf3 ( i - 1 , j - 1 , k - 1 )
return dp [ i ]... |
python-java-FIND_ELEMENTS_LARGER_HALF_ELEMENTS_ARRAY | python-java | def findLarger ( arr , n ) :
x = sorted ( arr )
for i in range ( n / 2 , n ) :
print ( x [ i ] ) ,
| static void findLarger ( int arr [ ] , int n ) {
Arrays . sort ( arr ) ;
for ( int i = n - 1 ;
i >= n / 2 ;
i -- ) System . out . print ( arr [ i ] + " " ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findLarger ( arr , n ) :
x = sorted ( arr )
for i in range ( n / 2 , n ) :
print ( x [ i ] ) ,
</Code> |
python-java-SUM_DIAGONALS_SPIRAL_ODD_ORDER_SQUARE_MATRIX | python-java | def spiralDiaSum ( n ) :
if n == 1 :
return 1
return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) )
| static int spiralDiaSum ( int n ) {
if ( n == 1 ) return 1 ;
return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def spiralDiaSum ( n ) :
if n == 1 :
return 1
return ( 4 * n * n - 6 * n + 6 + spiralDiaSum ( n - 2 ) )
</Code> |
python-java-COUNT_NUMBER_OF_OCCURRENCES_OR_FREQUENCY_IN_A_SORTED_ARRAY | python-java | def countOccurrences ( arr , n , x ) :
res = 0
for i in range ( n ) :
if x == arr [ i ] :
res += 1
return res
| static int countOccurrences ( int arr [ ] , int n , int x ) {
int res = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( x == arr [ i ] ) res ++ ;
return res ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countOccurrences ( arr , n , x ) :
res = 0
for i in range ( n ) :
if x == arr [ i ] :
res += 1
return res
</Code> |
python-java-SUM_ELEMENTS_K1TH_K2TH_SMALLEST_ELEMENTS | python-java | def sumBetweenTwoKth ( arr , n , k1 , k2 ) :
arr.sort ( )
result = 0
for i in range ( k1 , k2 - 1 ) :
result += arr [ i ]
return result
| static int sumBetweenTwoKth ( int arr [ ] , int k1 , int k2 ) {
Arrays . sort ( arr ) ;
int result = 0 ;
for ( int i = k1 ;
i < k2 - 1 ;
i ++ ) result += arr [ i ] ;
return result ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def sumBetweenTwoKth ( arr , n , k1 , k2 ) :
arr.sort ( )
result = 0
for i in range ( k1 , k2 - 1 ) :
result += arr [ i ]
return result
</Code> |
python-java-FIND_NUMBER_PAIRS_ARRAY_XOR_0_1 | python-java | def calculate ( a ) :
maximum = max ( a )
frequency = [ 0 for x in range ( maximum + 1 ) ]
for i in a :
frequency [ i ] += 1
answer = 0
for i in frequency :
answer = answer + i * ( i - 1 ) // 2
return answer
| static int calculate ( int a [ ] , int n ) {
int maximum = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ;
int frequency [ ] = new int [ maximum + 1 ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
frequency [ a [ i ] ] += 1 ;
}
int answer = 0 ;
for ( int i = 0 ;
i < ( maximum ) + 1 ;
i ++ ) {
answer... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def calculate ( a ) :
maximum = max ( a )
frequency = [ 0 for x in range ( maximum + 1 ) ]
for i in a :
frequency [ i ] += 1
answer = 0
for i in frequency :
answer = answer + i * ( i - 1 ) // 2
return answer
</Code> |
python-java-DOUBLE_FACTORIAL_1 | python-java | def doublefactorial ( n ) :
res = 1
for i in range ( n , - 1 , - 2 ) :
if ( i == 0 or i == 1 ) :
return res
else :
res *= i
| static int doublefactorial ( int n ) {
int res = 1 ;
for ( int i = n ;
i >= 0 ;
i = i - 2 ) {
if ( i == 0 || i == 1 ) return res ;
else res *= i ;
}
return res ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def doublefactorial ( n ) :
res = 1
for i in range ( n , - 1 , - 2 ) :
if ( i == 0 or i == 1 ) :
return res
else :
res *= i
</Code> |
python-java-LONGEST_COMMON_SUBSEQUENCE | python-java | def lcs ( X , Y , m , n ) :
if m == 0 or n == 0 :
return 0 ;
elif X [ m - 1 ] == Y [ n - 1 ] :
return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else :
return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
| int lcs ( char [ ] X , char [ ] Y , int m , int n ) {
if ( m == 0 || n == 0 ) return 0 ;
if ( X [ m - 1 ] == Y [ n - 1 ] ) return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def lcs ( X , Y , m , n ) :
if m == 0 or n == 0 :
return 0 ;
elif X [ m - 1 ] == Y [ n - 1 ] :
return 1 + lcs ( X , Y , m - 1 , n - 1 ) ;
else :
return max ( lcs ( X , Y , m , n - 1 ) , lcs ( X , Y , m - 1 , n ) ) ;
</Code> |
python-java-MAXIMUM_PRODUCT_SUBSET_ARRAY | python-java | def maxProductSubset ( a , n ) :
if n == 1 :
return a [ 0 ]
max_neg = - 999999999999
count_neg = 0
count_zero = 0
prod = 1
for i in range ( n ) :
if a [ i ] == 0 :
count_zero += 1
continue
if a [ i ] < 0 :
count_neg += 1
max... | static int maxProductSubset ( int a [ ] , int n ) {
if ( n == 1 ) {
return a [ 0 ] ;
}
int max_neg = Integer . MIN_VALUE ;
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 ] < ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxProductSubset ( a , n ) :
if n == 1 :
return a [ 0 ]
max_neg = - 999999999999
count_neg = 0
count_zero = 0
prod = 1
for i in range ( n ) :
if a [ i ] == 0 :
count_zero += 1
continue
if a [ i ] < 0 :
count_neg += 1
... |
python-java-TEMPLE_OFFERINGS | python-java | def offeringNumber ( n , templeHeight ) :
sum = 0
for i in range ( n ) :
left = 0
right = 0
for j in range ( i - 1 , - 1 , - 1 ) :
if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) :
left += 1
else :
break
for j in range ( ... | static int offeringNumber ( int n , int templeHeight [ ] ) {
int sum = 0 ;
for ( int i = 0 ;
i < n ;
++ i ) {
int left = 0 , right = 0 ;
for ( int j = i - 1 ;
j >= 0 ;
-- j ) {
if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) ++ left ;
else break ;
}
for ( int j = i + 1 ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def offeringNumber ( n , templeHeight ) :
sum = 0
for i in range ( n ) :
left = 0
right = 0
for j in range ( i - 1 , - 1 , - 1 ) :
if ( templeHeight [ j ] < templeHeight [ j + 1 ] ) :
left += 1
else :
break
for j in r... |
python-java-FIND_SUM_NODES_GIVEN_PERFECT_BINARY_TREE | python-java | def SumNodes ( l ) :
leafNodeCount = pow ( 2 , l - 1 )
vec = [ [ ] for i in range ( l ) ]
for i in range ( 1 , leafNodeCount + 1 ) :
vec [ l - 1 ].append ( i )
for i in range ( l - 2 , - 1 , - 1 ) :
k = 0
while ( k < len ( vec [ i + 1 ] ) - 1 ) :
vec [ i ].append ( ve... | static int sumNodes ( int l ) {
int leafNodeCount = ( int ) Math . pow ( 2 , l - 1 ) ;
Vector < Vector < Integer >> vec = new Vector < Vector < Integer >> ( ) ;
for ( int i = 1 ;
i <= l ;
i ++ ) vec . add ( new Vector < Integer > ( ) ) ;
for ( int i = 1 ;
i <= leafNodeCount ;
i ++ ) vec . get ( l - 1 ) ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def SumNodes ( l ) :
leafNodeCount = pow ( 2 , l - 1 )
vec = [ [ ] for i in range ( l ) ]
for i in range ( 1 , leafNodeCount + 1 ) :
vec [ l - 1 ].append ( i )
for i in range ( l - 2 , - 1 , - 1 ) :
k = 0
while ( k < len ( vec [ i + 1 ] ) - 1 ) :
vec [ i ].appe... |
python-java-CONSTRUCT_LEXICOGRAPHICALLY_SMALLEST_PALINDROME | python-java | def constructPalin ( string , l ) :
string = list ( string )
i = - 1
j = l
while i < j :
i += 1
j -= 1
if ( string [ i ] == string [ j ] and string [ i ] != '*' ) :
continue
elif ( string [ i ] == string [ j ] and string [ i ] == '*' ) :
string [ i... | static String constructPalin ( char [ ] str , int len ) {
int i = 0 , j = len - 1 ;
for ( ;
i < j ;
i ++ , j -- ) {
if ( str [ i ] == str [ j ] && str [ i ] != ' * ' ) continue ;
else if ( str [ i ] == str [ j ] && str [ i ] == ' * ' ) {
str [ i ] = ' a ' ;
str [ j ] = ' a ' ;
continue... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def constructPalin ( string , l ) :
string = list ( string )
i = - 1
j = l
while i < j :
i += 1
j -= 1
if ( string [ i ] == string [ j ] and string [ i ] != '*' ) :
continue
elif ( string [ i ] == string [ j ] and string [ i ] == '*' ) :
str... |
python-java-COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY | python-java | def countPairs ( arr , n ) :
ans = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
if ( arr [ i ] == arr [ j ] ) :
ans += 1
return ans
| static int countPairs ( int arr [ ] , int n ) {
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ;
return ans ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countPairs ( arr , n ) :
ans = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n ) :
if ( arr [ i ] == arr [ j ] ) :
ans += 1
return ans
</Code> |
python-java-FAST_MULTIPLICATION_METHOD_WITHOUT_USING_MULTIPLICATION_OPERATOR_RUSSIAN_PEASANTS_ALGORITHM | python-java | def russianPeasant ( a , b ) :
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def russianPeasant ( a , b ) :
res = 0
while ( b > 0 ) :
if ( b & 1 ) :
res = res + a
a = a << 1
b = b >> 1
return res
</Code> |
python-java-CAESAR_CIPHER | python-java | def encrypt ( text , s ) :
result = ""
for i in range ( len ( text ) ) :
char = text [ i ]
if ( char.isupper ( ) ) :
result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 )
else :
result += chr ( ( ord ( char ) + 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def encrypt ( text , s ) :
result = ""
for i in range ( len ( text ) ) :
char = text [ i ]
if ( char.isupper ( ) ) :
result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 )
else :
result += chr ( ( ord ( char ) + s - 97 ) % 26 + 97 )
return result
</Code... |
python-java-PROGRAM_DISTANCE_TWO_POINTS_EARTH | python-java | def distance ( lat1 , lat2 , lon1 , lon2 ) :
lon1 = radians ( lon1 )
lon2 = radians ( lon2 )
lat1 = radians ( lat1 )
lat2 = radians ( lat2 )
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2
c = 2 * asin ( sqrt ( a ) )
... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def distance ( lat1 , lat2 , lon1 , lon2 ) :
lon1 = radians ( lon1 )
lon2 = radians ( lon2 )
lat1 = radians ( lat1 )
lat2 = radians ( lat2 )
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2
c = 2 * asin ( sqrt ( ... |
python-java-NTH_EVEN_FIBONACCI_NUMBER | python-java | def evenFib ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def evenFib ( n ) :
if ( n < 1 ) :
return n
if ( n == 1 ) :
return 2
return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) )
</Code> |
python-java-STEINS_ALGORITHM_FOR_FINDING_GCD | python-java | def gcd ( a , b ) :
if ( a == 0 ) :
return b
if ( b == 0 ) :
return a
k = 0
while ( ( ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def gcd ( a , b ) :
if ( a == 0 ) :
return b
if ( b == 0 ) :
return a
k = 0
while ( ( ( a
</Code> |
python-java-WRITE_ONE_LINE_C_FUNCTION_TO_FIND_WHETHER_A_NO_IS_POWER_OF_TWO | python-java | def isPowerOfTwo ( n ) :
if ( n == 0 ) :
return False
while ( n != 1 ) :
if ( n % 2 != 0 ) :
return False
n = n // 2
return True
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isPowerOfTwo ( n ) :
if ( n == 0 ) :
return False
while ( n != 1 ) :
if ( n % 2 != 0 ) :
return False
n = n // 2
return True
</Code> |
python-java-SUM_PAIRWISE_PRODUCTS | python-java | def findSum ( n ) :
sm = 0
for i in range ( 1 , n + 1 ) :
for j in range ( i , n + 1 ) :
sm = sm + i * j
return sm
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( n ) :
sm = 0
for i in range ( 1 , n + 1 ) :
for j in range ( i , n + 1 ) :
sm = sm + i * j
return sm
</Code> |
python-java-SUM_BINOMIAL_COEFFICIENTS_1 | python-java | def binomialCoeffSum ( n ) :
return ( 1 << n )
| static int binomialCoeffSum ( int n ) {
return ( 1 << n ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def binomialCoeffSum ( n ) :
return ( 1 << n )
</Code> |
python-java-EVALUATE_AN_ARRAY_EXPRESSION_WITH_NUMBERS_AND | python-java | def calculateSum ( arr , n ) :
if ( n == 0 ) :
return 0
s = arr [ 0 ]
value = int ( s )
sum = value
for i in range ( 2 , n , 2 ) :
s = arr [ i ]
value = int ( s )
operation = arr [ i - 1 ] [ 0 ]
if ( operation == '+' ) :
sum += value
else :... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def calculateSum ( arr , n ) :
if ( n == 0 ) :
return 0
s = arr [ 0 ]
value = int ( s )
sum = value
for i in range ( 2 , n , 2 ) :
s = arr [ i ]
value = int ( s )
operation = arr [ i - 1 ] [ 0 ]
if ( operation == '+' ) :
sum += value
... |
python-java-CHECK_WHETHER_POINT_EXISTS_CIRCLE_SECTOR_NOT | python-java | def checkPoint ( radius , x , y , percent , startAngle ) :
endAngle = 360 / percent + startAngle
polarradius = math.sqrt ( x * x + y * y )
Angle = math.atan ( y / x )
if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) :
print ( "Point (" , x , "," , y , ") " "exist in the ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def checkPoint ( radius , x , y , percent , startAngle ) :
endAngle = 360 / percent + startAngle
polarradius = math.sqrt ( x * x + y * y )
Angle = math.atan ( y / x )
if ( Angle >= startAngle and Angle <= endAngle and polarradius < radius ) :
print ( "Point (" , x , "," , y , ") " "exist ... |
python-java-PRINTING_ITEMS_01_KNAPSACK | python-java | def printknapSack ( W , wt , val , n ) :
K = [ [ 0 for w in range ( W + 1 ) ] for i in range ( n + 1 ) ]
for i in range ( n + 1 ) :
for w in range ( W + 1 ) :
if i == 0 or w == 0 :
K [ i ] [ w ] = 0
elif wt [ i - 1 ] <= w :
K [ i ] [ w ] = max ( va... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def printknapSack ( W , wt , val , n ) :
K = [ [ 0 for w in range ( W + 1 ) ] for i in range ( n + 1 ) ]
for i in range ( n + 1 ) :
for w in range ( W + 1 ) :
if i == 0 or w == 0 :
K [ i ] [ w ] = 0
elif wt [ i - 1 ] <= w :
K [ i ] [ w ] = m... |
python-java-SEQUENCES_GIVEN_LENGTH_EVERY_ELEMENT_EQUAL_TWICE_PREVIOUS_1 | python-java | def getTotalNumberOfSequences ( m , n ) :
T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if i == 0 or j == 0 :
T [ i ] [ j ] = 0
elif i < j :
T [ i ] [ j ] = 0
elif ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def getTotalNumberOfSequences ( m , n ) :
T = [ [ 0 for i in range ( n + 1 ) ] for i in range ( m + 1 ) ]
for i in range ( m + 1 ) :
for j in range ( n + 1 ) :
if i == 0 or j == 0 :
T [ i ] [ j ] = 0
elif i < j :
T [ i ] [ j ] = 0
... |
python-java-SUM_MATRIX_ELEMENT_ABSOLUTE_DIFFERENCE_ROW_COLUMN_NUMBERS | python-java | def findSum ( n ) :
arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for i in range ( n ) :
for j in range ( n ) :
arr [ i ] [ j ] = abs ( i - j )
sum = 0
for i in range ( n ) :
for j in range ( n ) :
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( n ) :
arr = [ [ 0 for x in range ( n ) ] for y in range ( n ) ]
for i in range ( n ) :
for j in range ( n ) :
arr [ i ] [ j ] = abs ( i - j )
sum = 0
for i in range ( n ) :
for j in range ( n ) :
sum += arr [ i ] [ j ]
return sum
</Code> |
python-java-REPLACE_CHARACTER_C1_C2_C2_C1_STRING_S | python-java | def replace ( s , c1 , c2 ) :
l = len ( s )
for i in range ( l ) :
if ( s [ i ] == c1 ) :
s = s [ 0 : i ] + c2 + s [ i + 1 : ]
elif ( s [ i ] == c2 ) :
s = s [ 0 : i ] + c1 + s [ i + 1 : ]
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def replace ( s , c1 , c2 ) :
l = len ( s )
for i in range ( l ) :
if ( s [ i ] == c1 ) :
s = s [ 0 : i ] + c2 + s [ i + 1 : ]
elif ( s [ i ] == c2 ) :
s = s [ 0 : i ] + c1 + s [ i + 1 : ]
return s
</Code> |
python-java-DISCRETE_LOGARITHM_FIND_INTEGER_K_AK_CONGRUENT_MODULO_B | python-java | def discreteLogarithm ( a , b , m ) :
n = int ( math.sqrt ( m ) + 1 )
an = 1
for i in range ( n ) :
an = ( an * a ) % m
value = [ 0 ] * m
cur = an
for i in range ( 1 , n + 1 ) :
if ( value [ cur ] == 0 ) :
value [ cur ] = i
cur = ( cur * an ) % m
cur = b
... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def discreteLogarithm ( a , b , m ) :
n = int ( math.sqrt ( m ) + 1 )
an = 1
for i in range ( n ) :
an = ( an * a ) % m
value = [ 0 ] * m
cur = an
for i in range ( 1 , n + 1 ) :
if ( value [ cur ] == 0 ) :
value [ cur ] = i
cur = ( cur * an ) % m
cu... |
python-java-FIND_LARGEST_PRIME_FACTOR_NUMBER | python-java | def maxPrimeFactors ( n ) :
maxPrime = - 1
while n % 2 == 0 :
maxPrime = 2
n >>= 1
for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 , 2 ) :
while n % i == 0 :
maxPrime = i
n = n / i
if n > 2 :
maxPrime = n
return int ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxPrimeFactors ( n ) :
maxPrime = - 1
while n % 2 == 0 :
maxPrime = 2
n >>= 1
for i in range ( 3 , int ( math.sqrt ( n ) ) + 1 , 2 ) :
while n % i == 0 :
maxPrime = i
n = n / i
if n > 2 :
maxPrime = n
return int ( maxPrime )
</Code... |
python-java-POSSIBLE_FORM_TRIANGLE_ARRAY_VALUES | python-java | def isPossibleTriangle ( arr , N ) :
if N < 3 :
return False
arr.sort ( )
for i in range ( N - 2 ) :
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isPossibleTriangle ( arr , N ) :
if N < 3 :
return False
arr.sort ( )
for i in range ( N - 2 ) :
if arr [ i ] + arr [ i + 1 ] > arr [ i + 2 ] :
return True
</Code> |
python-java-COUNT_NUMBER_INCREASING_SUBSEQUENCES_SIZE_K | python-java | def numOfIncSubseqOfSizeK ( arr , n , k ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ]
for i in range ( n ) :
dp [ 0 ] [ i ] = 1
for l in range ( 1 , k ) :
for i in range ( l , n ) :
dp [ l ] [ i ] = 0
for j in range ( l - 1 , i ) :
if ( ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def numOfIncSubseqOfSizeK ( arr , n , k ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ]
for i in range ( n ) :
dp [ 0 ] [ i ] = 1
for l in range ( 1 , k ) :
for i in range ( l , n ) :
dp [ l ] [ i ] = 0
for j in range ( l - 1 , i ) :
... |
python-java-MINIMUM_COST_MAKE_LONGEST_COMMON_SUBSEQUENCE_LENGTH_K | python-java | def solve ( X , Y , l , r , k , dp ) :
if k == 0 :
return 0
if l < 0 or r < 0 :
return 1000000000
if dp [ l ] [ r ] [ k ] != - 1 :
return dp [ l ] [ r ] [ k ]
cost = ( ( ord ( X [ l ] ) - ord ( 'a' ) ) ^ ( ord ( Y [ r ] ) - ord ( 'a' ) ) )
dp [ l ] [ r ] [ k ] = min ( [ cost ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def solve ( X , Y , l , r , k , dp ) :
if k == 0 :
return 0
if l < 0 or r < 0 :
return 1000000000
if dp [ l ] [ r ] [ k ] != - 1 :
return dp [ l ] [ r ] [ k ]
cost = ( ( ord ( X [ l ] ) - ord ( 'a' ) ) ^ ( ord ( Y [ r ] ) - ord ( 'a' ) ) )
dp [ l ] [ r ] [ k ] = min ( ... |
python-java-REMAINDER_7_LARGE_NUMBERS | python-java | def remainderWith7 ( num ) :
series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ]
series_index = 0
result = 0
for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) :
digit = ord ( num [ i ] ) - 48
result += digit * series [ series_index ]
series_index = ( series_index + 1 ) % 6
result %... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def remainderWith7 ( num ) :
series = [ 1 , 3 , 2 , - 1 , - 3 , - 2 ]
series_index = 0
result = 0
for i in range ( ( len ( num ) - 1 ) , - 1 , - 1 ) :
digit = ord ( num [ i ] ) - 48
result += digit * series [ series_index ]
series_index = ( series_index + 1 ) % 6
r... |
python-java-GIVEN_TWO_STRINGS_FIND_FIRST_STRING_SUBSEQUENCE_SECOND | python-java | def isSubSequence ( string1 , string2 , m , n ) :
if m == 0 : return True
if n == 0 : return False
if string1 [ m - 1 ] == string2 [ n - 1 ] :
return isSubSequence ( string1 , string2 , m - 1 , n - 1 )
return isSubSequence ( string1 , string2 , 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isSubSequence ( string1 , string2 , m , n ) :
if m == 0 : return True
if n == 0 : return False
if string1 [ m - 1 ] == string2 [ n - 1 ] :
return isSubSequence ( string1 , string2 , m - 1 , n - 1 )
return isSubSequence ( string1 , string2 , m , n - 1 )
</Code> |
python-java-PROGRAM_TO_FIND_REMAINDER_WITHOUT_USING_MODULO_OR_OPERATOR | python-java | def getRemainder ( num , divisor ) :
return ( num - divisor * ( num // divisor ) )
| static int getRemainder ( int num , int divisor ) {
return ( num - divisor * ( num / divisor ) ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def getRemainder ( num , divisor ) :
return ( num - divisor * ( num // divisor ) )
</Code> |
python-java-MINIMUM_PRODUCT_SUBSET_ARRAY | python-java | def minProductSubset ( a , n ) :
if ( n == 1 ) :
return a [ 0 ]
max_neg = float ( '-inf' )
min_pos = float ( 'inf' )
count_neg = 0
count_zero = 0
prod = 1
for i in range ( 0 , n ) :
if ( a [ i ] == 0 ) :
count_zero = count_zero + 1
continue
if ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def minProductSubset ( a , n ) :
if ( n == 1 ) :
return a [ 0 ]
max_neg = float ( '-inf' )
min_pos = float ( 'inf' )
count_neg = 0
count_zero = 0
prod = 1
for i in range ( 0 , n ) :
if ( a [ i ] == 0 ) :
count_zero = count_zero + 1
continue
... |
python-java-HOW_CAN_WE_SUM_THE_DIGITS_OF_A_GIVEN_NUMBER_IN_SINGLE_STATEMENT | python-java | def getSum ( n ) :
sum = 0
while ( n != 0 ) :
sum = sum + int ( n % 10 )
n = int ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def getSum ( n ) :
sum = 0
while ( n != 0 ) :
sum = sum + int ( n % 10 )
n = int ( n / 10 )
return sum
</Code> |
python-java-COUNT_DIGITS_FACTORIAL_SET_1 | python-java | def findDigits ( n ) :
if ( n < 0 ) :
return 0
if ( n <= 1 ) :
return 1
digits = 0
for i in range ( 2 , n + 1 ) :
digits += math.log10 ( i )
return math.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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findDigits ( n ) :
if ( n < 0 ) :
return 0
if ( n <= 1 ) :
return 1
digits = 0
for i in range ( 2 , n + 1 ) :
digits += math.log10 ( i )
return math.floor ( digits ) + 1
</Code> |
python-java-SORT_ARRAY_TWO_HALVES_SORTED | python-java | def mergeTwoHalf ( A , n ) :
A.sort ( )
| static void mergeTwoHalf ( int [ ] A , int n ) {
Arrays . sort ( A ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def mergeTwoHalf ( A , n ) :
A.sort ( )
</Code> |
python-java-CREATE_A_MATRIX_WITH_ALTERNATING_RECTANGLES_OF_0_AND_X | python-java | def fill0X ( m , n ) :
i , k , l = 0 , 0 , 0
r = m
c = n
a = [ [ None ] * n for i in range ( m ) ]
x = 'X'
while k < m and l < n :
for i in range ( l , n ) :
a [ k ] [ i ] = x
k += 1
for i in range ( k , m ) :
a [ i ] [ n - 1 ] = x
n -= 1
... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def fill0X ( m , n ) :
i , k , l = 0 , 0 , 0
r = m
c = n
a = [ [ None ] * n for i in range ( m ) ]
x = 'X'
while k < m and l < n :
for i in range ( l , n ) :
a [ k ] [ i ] = x
k += 1
for i in range ( k , m ) :
a [ i ] [ n - 1 ] = x
n... |
python-java-MINIMUM_PERIMETER_N_BLOCKS | python-java | def minPerimeter ( n ) :
l = math.sqrt ( n )
sq = l * l
if ( sq == n ) :
return l * 4
else :
row = n / l
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def minPerimeter ( n ) :
l = math.sqrt ( n )
sq = l * l
if ( sq == n ) :
return l * 4
else :
row = n / l
perimeter = 2 * ( l + row )
if ( n % l != 0 ) :
perimeter += 2
return perimeter
</Code> |
python-java-FIND_POSSIBLE_ROTATE_PAGE_ANGLE_NOT | python-java | def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) :
dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) )
dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) )
if ( dis1 != dis2 ) :
print ( "No" )
elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) :
print ( "No" )
e... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) :
dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) )
dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) )
if ( dis1 != dis2 ) :
print ( "No" )
elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) :
print ( "No" ... |
python-java-PROGRAM_FIND_SLOPE_LINE | python-java | def slope ( x1 , y1 , x2 , y2 ) :
return ( float ) ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def slope ( x1 , y1 , x2 , y2 ) :
return ( float ) ( y2 - y1 ) / ( x2 - x1 )
</Code> |
python-java-FIND_NUMBER_PERFECT_SQUARES_TWO_GIVEN_NUMBERS_1 | python-java | def CountSquares ( a , b ) :
return ( math.floor ( math.sqrt ( b ) ) - math.ceil ( math.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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def CountSquares ( a , b ) :
return ( math.floor ( math.sqrt ( b ) ) - math.ceil ( math.sqrt ( a ) ) + 1 )
</Code> |
python-java-COUNT_BINARY_STRINGS_K_TIMES_APPEARING_ADJACENT_TWO_SET_BITS | python-java | def countStrings ( n , k ) :
dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ]
dp [ 1 ] [ 0 ] [ 0 ] = 1
dp [ 1 ] [ 0 ] [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
for j in range ( k + 1 ) :
dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [ j ] [ ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countStrings ( n , k ) :
dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ]
dp [ 1 ] [ 0 ] [ 0 ] = 1
dp [ 1 ] [ 0 ] [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
for j in range ( k + 1 ) :
dp [ i ] [ j ] [ 0 ] = ( dp [ i - 1 ] [ j ] [ 0 ] + dp [ i - 1 ] [... |
python-java-FIND_SMALLEST_RANGE_CONTAINING_ELEMENTS_FROM_K_LISTS | python-java | def findSmallestRange ( arr , n , k ) :
i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
for i in range ( k + 1 ) :
ptr [ i ] = 0
minrange = 10 ** 9
while ( 1 ) :
minind = - 1
minval = 10 ** 9
maxval = - 10 ** 9
flag =... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSmallestRange ( arr , n , k ) :
i , minval , maxval , minrange , minel , maxel , flag , minind = 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0
for i in range ( k + 1 ) :
ptr [ i ] = 0
minrange = 10 ** 9
while ( 1 ) :
minind = - 1
minval = 10 ** 9
maxval = - 10 ** 9
... |
python-java-LEXICOGRAPHICAL_CONCATENATION_SUBSTRINGS_STRING | python-java | def lexicographicSubConcat ( s ) :
n = len ( s )
sub_count = ( n * ( n + 1 ) ) // 2
arr = [ 0 ] * sub_count
index = 0
for i in range ( n ) :
for j in range ( 1 , n - i + 1 ) :
arr [ index ] = s [ i : i + j ]
index += 1
arr.sort ( )
res = ""
for i in range ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def lexicographicSubConcat ( s ) :
n = len ( s )
sub_count = ( n * ( n + 1 ) ) // 2
arr = [ 0 ] * sub_count
index = 0
for i in range ( n ) :
for j in range ( 1 , n - i + 1 ) :
arr [ index ] = s [ i : i + j ]
index += 1
arr.sort ( )
res = ""
for i in... |
python-java-FIND_MAXIMUM_ELEMENT_ROW_MATRIX | python-java | def maxelement ( arr ) :
no_of_rows = len ( arr )
no_of_column = len ( arr [ 0 ] )
for i in range ( no_of_rows ) :
max1 = 0
for j in range ( no_of_column ) :
if arr [ i ] [ j ] > max1 :
max1 = arr [ i ] [ j ]
print ( max1 )
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxelement ( arr ) :
no_of_rows = len ( arr )
no_of_column = len ( arr [ 0 ] )
for i in range ( no_of_rows ) :
max1 = 0
for j in range ( no_of_column ) :
if arr [ i ] [ j ] > max1 :
max1 = arr [ i ] [ j ]
print ( max1 )
</Code> |
python-java-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX_1 | python-java | def countNegative ( M , n , m ) :
count = 0
i = 0
j = m - 1
while j >= 0 and 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countNegative ( M , n , m ) :
count = 0
i = 0
j = m - 1
while j >= 0 and i < n :
if M [ i ] [ j ] < 0 :
count += ( j + 1 )
i += 1
else :
j -= 1
return count
</Code> |
python-java-FIND_POSITION_GIVEN_NUMBER_AMONG_NUMBERS_MADE_4_7 | python-java | def findpos ( n ) :
i = 0
j = len ( n )
pos = 0
while ( i < j ) :
if ( n [ i ] == '4' ) :
pos = pos * 2 + 1
if ( n [ i ] == '7' ) :
pos = pos * 2 + 2
i = i + 1
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findpos ( n ) :
i = 0
j = len ( n )
pos = 0
while ( i < j ) :
if ( n [ i ] == '4' ) :
pos = pos * 2 + 1
if ( n [ i ] == '7' ) :
pos = pos * 2 + 2
i = i + 1
return pos
</Code> |
python-java-FIND_VALUE_OF_Y_MOD_2_RAISED_TO_POWER_X | python-java | def yMod ( y , x ) :
return ( y % pow ( 2 , 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def yMod ( y , x ) :
return ( y % pow ( 2 , x ) )
</Code> |
python-java-DYNAMIC_PROGRAMMING_SET_14_MAXIMUM_SUM_INCREASING_SUBSEQUENCE | python-java | def maxSumIS ( arr , n ) :
max = 0
msis = [ 0 for x in range ( n ) ]
for i in range ( n ) :
msis [ i ] = arr [ i ]
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) :
msis [ i ] = msis [ j ] ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxSumIS ( arr , n ) :
max = 0
msis = [ 0 for x in range ( n ) ]
for i in range ( n ) :
msis [ i ] = arr [ i ]
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and msis [ i ] < msis [ j ] + arr [ i ] ) :
msis [ i ] = msis... |
python-java-LENGTH_LONGEST_BALANCED_SUBSEQUENCE_1 | python-java | def maxLength ( s , n ) :
invalidOpenBraces = 0
invalidCloseBraces = 0
for i in range ( n ) :
if ( s [ i ] == '(' ) :
invalidOpenBraces += 1
else :
if ( invalidOpenBraces == 0 ) :
invalidCloseBraces += 1
else :
invalidOpenBr... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxLength ( s , n ) :
invalidOpenBraces = 0
invalidCloseBraces = 0
for i in range ( n ) :
if ( s [ i ] == '(' ) :
invalidOpenBraces += 1
else :
if ( invalidOpenBraces == 0 ) :
invalidCloseBraces += 1
else :
invali... |
python-java-MAXIMUM_PRODUCT_INCREASING_SUBSEQUENCE | python-java | def lis ( arr , n ) :
mpis = [ 0 ] * ( n )
for i in range ( n ) :
mpis [ i ] = arr [ i ]
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) :
mpis [ i ] = mpis [ j ] * arr [ i ]
return max... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def lis ( arr , n ) :
mpis = [ 0 ] * ( n )
for i in range ( n ) :
mpis [ i ] = arr [ i ]
for i in range ( 1 , n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and mpis [ i ] < ( mpis [ j ] * arr [ i ] ) ) :
mpis [ i ] = mpis [ j ] * arr [ i ]
ret... |
python-java-PETERSON_GRAPH | python-java | def findthepath ( S , v ) :
result [ 0 ] = v
for i in range ( 1 , len ( S ) ) :
if ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) ] or adj [ ord ( S [ i ] ) - ord ( 'A' ) ] [ v ] ) :
v = ord ( S [ i ] ) - ord ( 'A' )
elif ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) + 5 ] or adj [ ord ( ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findthepath ( S , v ) :
result [ 0 ] = v
for i in range ( 1 , len ( S ) ) :
if ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) ] or adj [ ord ( S [ i ] ) - ord ( 'A' ) ] [ v ] ) :
v = ord ( S [ i ] ) - ord ( 'A' )
elif ( adj [ v ] [ ord ( S [ i ] ) - ord ( 'A' ) + 5 ] or adj [... |
python-java-CHECK_QUEUE_CAN_SORTED_ANOTHER_QUEUE_USING_STACK | python-java | def checkSorted ( n , q ) :
st = [ ]
expected = 1
fnt = None
while ( not q.empty ( ) ) :
fnt = q.queue [ 0 ]
q.get ( )
if ( fnt == expected ) :
expected += 1
else :
if ( len ( st ) == 0 ) :
st.append ( fnt )
elif ( len (... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def checkSorted ( n , q ) :
st = [ ]
expected = 1
fnt = None
while ( not q.empty ( ) ) :
fnt = q.queue [ 0 ]
q.get ( )
if ( fnt == expected ) :
expected += 1
else :
if ( len ( st ) == 0 ) :
st.append ( fnt )
elif ... |
python-java-GENERATING_DISTINCT_SUBSEQUENCES_OF_A_GIVEN_STRING_IN_LEXICOGRAPHIC_ORDER | python-java | def generate ( st , s ) :
if len ( s ) == 0 :
return
if s not in st :
st.add ( s )
for i in range ( len ( s ) ) :
t = list ( s ).copy ( )
t.remove ( s [ i ] )
t = ''.join ( t )
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def generate ( st , s ) :
if len ( s ) == 0 :
return
if s not in st :
st.add ( s )
for i in range ( len ( s ) ) :
t = list ( s ).copy ( )
t.remove ( s [ i ] )
t = ''.join ( t )
generate ( st , t )
return
</Code> |
python-java-RECURSIVE_FUNCTIONS | python-java | def tower ( n , sourcePole , destinationPole , auxiliaryPole ) :
if ( 0 == n ) :
return
tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole )
print ( "Move the disk" , sourcePole , "from" , sourcePole , "to" , destinationPole )
tower ( n - 1 , auxiliaryPole , destinationPole , sourcePol... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def tower ( n , sourcePole , destinationPole , auxiliaryPole ) :
if ( 0 == n ) :
return
tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole )
print ( "Move the disk" , sourcePole , "from" , sourcePole , "to" , destinationPole )
tower ( n - 1 , auxiliaryPole , destinationPole , so... |
python-java-PIZZA_CUT_PROBLEM_CIRCLE_DIVISION_LINES | python-java | def findMaximumPieces ( n ) :
return int ( 1 + n * ( n + 1 ) / 2 )
| static int findMaximumPieces ( int n ) {
return 1 + n * ( n + 1 ) / 2 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findMaximumPieces ( n ) :
return int ( 1 + n * ( n + 1 ) / 2 )
</Code> |
python-java-LEXICOGRAPHICALLY_MINIMUM_STRING_ROTATION | python-java | def minLexRotation ( str_ ) :
n = len ( str_ )
arr = [ 0 ] * n
concat = str_ + str_
for i in range ( n ) :
arr [ i ] = concat [ i : n + i ]
arr.sort ( )
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def minLexRotation ( str_ ) :
n = len ( str_ )
arr = [ 0 ] * n
concat = str_ + str_
for i in range ( n ) :
arr [ i ] = concat [ i : n + i ]
arr.sort ( )
return arr [ 0 ]
</Code> |
python-java-DYNAMIC_PROGRAMMING_SET_28_MINIMUM_INSERTIONS_TO_FORM_A_PALINDROME | python-java | def findMinInsertions ( str , l , h ) :
if ( l > h ) :
return sys.maxsize
if ( l == h ) :
return 0
if ( l == h - 1 ) :
return 0 if ( str [ l ] == str [ h ] ) else 1
if ( str [ l ] == str [ h ] ) :
return findMinInsertions ( str , l + 1 , h - 1 )
else :
return ... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findMinInsertions ( str , l , h ) :
if ( l > h ) :
return sys.maxsize
if ( l == h ) :
return 0
if ( l == h - 1 ) :
return 0 if ( str [ l ] == str [ h ] ) else 1
if ( str [ l ] == str [ h ] ) :
return findMinInsertions ( str , l + 1 , h - 1 )
else :
... |
python-java-PROGRAM_OCTAL_DECIMAL_CONVERSION | python-java | def octalToDecimal ( n ) :
num = n
dec_value = 0
base = 1
temp = num
while ( temp ) :
last_digit = temp % 10
temp = int ( 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def octalToDecimal ( n ) :
num = n
dec_value = 0
base = 1
temp = num
while ( temp ) :
last_digit = temp % 10
temp = int ( temp / 10 )
dec_value += last_digit * base
base = base * 8
return dec_value
</Code> |
python-java-FIND_THE_MISSING_NUMBER | python-java | def getMissingNo ( A ) :
n = len ( A )
total = ( n + 1 ) * ( n + 2 ) / 2
sum_of_A = sum ( A )
return total - sum_of_A
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def getMissingNo ( A ) :
n = len ( A )
total = ( n + 1 ) * ( n + 2 ) / 2
sum_of_A = sum ( A )
return total - sum_of_A
</Code> |
python-java-PRINT_SQUARES_FIRST_N_NATURAL_NUMBERS_WITHOUT_USING | python-java | def printSquares ( n ) :
square = 0 ; prev_x = 0 ;
for x in range ( 0 , n ) :
square = ( square + x + prev_x )
print ( square , end = " " )
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def printSquares ( n ) :
square = 0 ; prev_x = 0 ;
for x in range ( 0 , n ) :
square = ( square + x + prev_x )
print ( square , end = " " )
prev_x = x
</Code> |
python-java-PERMUTE_TWO_ARRAYS_SUM_EVERY_PAIR_GREATER_EQUAL_K | python-java | def isPossible ( a , b , n , k ) :
a.sort ( reverse = True )
b.sort ( )
for i in range ( n ) :
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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isPossible ( a , b , n , k ) :
a.sort ( reverse = True )
b.sort ( )
for i in range ( n ) :
if ( a [ i ] + b [ i ] < k ) :
return False
return True
</Code> |
python-java-CHECK_WHETHER_GIVEN_CIRCLE_RESIDE_BOUNDARY_MAINTAINED_OUTER_CIRCLE_INNER_CIRCLE | python-java | def fitOrNotFit ( R , r , x , y , rad ) :
val = math.sqrt ( math.pow ( x , 2 ) + math.pow ( y , 2 ) )
if ( val + rad <= R and val - rad >= R - r ) :
print ( "Fits\n" )
else :
print ( "Doesn't Fit" )
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def fitOrNotFit ( R , r , x , y , rad ) :
val = math.sqrt ( math.pow ( x , 2 ) + math.pow ( y , 2 ) )
if ( val + rad <= R and val - rad >= R - r ) :
print ( "Fits\n" )
else :
print ( "Doesn't Fit" )
</Code> |
python-java-NTH_MULTIPLE_NUMBER_FIBONACCI_SERIES | python-java | def findPosition ( k , n ) :
f1 = 0
f2 = 1
i = 2
while i != 0 :
f3 = f1 + f2
f1 = f2
f2 = f3
if f2 % k == 0 :
return n * i
i += 1
return
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findPosition ( k , n ) :
f1 = 0
f2 = 1
i = 2
while i != 0 :
f3 = f1 + f2
f1 = f2
f2 = f3
if f2 % k == 0 :
return n * i
i += 1
return
</Code> |
python-java-PRINT_N_TERMS_NEWMAN_CONWAY_SEQUENCE | python-java | def sequence ( n ) :
f = [ 0 , 1 , 1 ]
print ( f [ 1 ] , end = " " ) ,
print ( f [ 2 ] , end = " " ) ,
for i in range ( 3 , n + 1 ) :
f.append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] )
print ( f [ i ] , end = " " ) ,
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def sequence ( n ) :
f = [ 0 , 1 , 1 ]
print ( f [ 1 ] , end = " " ) ,
print ( f [ 2 ] , end = " " ) ,
for i in range ( 3 , n + 1 ) :
f.append ( f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] )
print ( f [ i ] , end = " " ) ,
</Code> |
python-java-MINIMUM_NUMBER_OF_MANIPULATIONS_REQUIRED_TO_MAKE_TWO_STRINGS_ANAGRAM_WITHOUT_DELETION_OF_CHARACTER | python-java | def countManipulations ( s1 , s2 ) :
count = 0
char_count = [ 0 ] * 26
for i in range ( 26 ) :
char_count [ i ] = 0
for i in range ( len ( s1 ) ) :
char_count [ ord ( s1 [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( len ( s2 ) ) :
char_count [ ord ( s2 [ i ] ) - ord ( 'a' ) ]... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countManipulations ( s1 , s2 ) :
count = 0
char_count = [ 0 ] * 26
for i in range ( 26 ) :
char_count [ i ] = 0
for i in range ( len ( s1 ) ) :
char_count [ ord ( s1 [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( len ( s2 ) ) :
char_count [ ord ( s2 [ i ] ) - ord ( ... |
python-java-COUNT_NUMBER_WAYS_REACH_GIVEN_SCORE_GAME | python-java | def count ( n ) :
table = [ 0 for i in range ( n + 1 ) ]
table [ 0 ] = 1
for i in range ( 3 , n + 1 ) :
table [ i ] += table [ i - 3 ]
for i in range ( 5 , n + 1 ) :
table [ i ] += table [ i - 5 ]
for i in range ( 10 , n + 1 ) :
table [ i ] += table [ i - 10 ]
return tabl... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def count ( n ) :
table = [ 0 for i in range ( n + 1 ) ]
table [ 0 ] = 1
for i in range ( 3 , n + 1 ) :
table [ i ] += table [ i - 3 ]
for i in range ( 5 , n + 1 ) :
table [ i ] += table [ i - 5 ]
for i in range ( 10 , n + 1 ) :
table [ i ] += table [ i - 10 ]
retu... |
python-java-FIND_LENGTH_LONGEST_SUBSEQUENCE_ONE_STRING_SUBSTRING_ANOTHER_STRING | python-java | def maxSubsequenceSubstring ( x , y , n , m ) :
dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ]
for i in range ( 1 , m + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( x [ j - 1 ] == y [ i - 1 ] ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
else :
... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxSubsequenceSubstring ( x , y , n , m ) :
dp = [ [ 0 for i in range ( MAX ) ] for i in range ( MAX ) ]
for i in range ( 1 , m + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( x [ j - 1 ] == y [ i - 1 ] ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
else :... |
python-java-LONGEST_REPEATING_SUBSEQUENCE | python-java | def findLongestRepeatingSubSeq ( str ) :
n = len ( str )
dp = [ [ 0 ] * ( n + 1 ) ] * ( n + 1 )
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
else :
... | 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findLongestRepeatingSubSeq ( str ) :
n = len ( str )
dp = [ [ 0 ] * ( n + 1 ) ] * ( n + 1 )
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
if ( str [ i - 1 ] == str [ j - 1 ] and i != j ) :
dp [ i ] [ j ] = 1 + dp [ i - 1 ] [ j - 1 ]
... |
python-java-PROGRAM_FIND_STRING_START_END_GEEKS | python-java | def isCornerPresent ( str , corner ) :
n = len ( str )
cl = len ( corner )
if ( n < cl ) :
return False
return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) )
| 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 python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isCornerPresent ( str , corner ) :
n = len ( str )
cl = len ( corner )
if ( n < cl ) :
return False
return ( ( str [ : cl ] == corner ) and ( str [ n - cl : ] == corner ) )
</Code> |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.