id
stringlengths
18
123
pair
stringclasses
6 values
Code1
stringlengths
41
1.98k
Code2
stringlengths
41
1.98k
instruction
stringclasses
6 values
input
stringlengths
56
2k
java-python-COMPUTE_MODULUS_DIVISION_BY_A_POWER_OF_2_NUMBER
java-python
static int getModulo ( int n , int d ) { return ( n & ( d - 1 ) ) ; }
def getModulo ( n , d ) : return ( n & ( d - 1 ) )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int getModulo ( int n , int d ) { return ( n & ( d - 1 ) ) ; } </Code>
java-python-QUICK_WAY_CHECK_CHARACTERS_STRING
java-python
static boolean allCharactersSame ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s . charAt ( i ) != s . charAt ( 0 ) ) return false ; return true ; }
def allCharactersSame ( s ) : n = len ( s ) for i in range ( 1 , n ) : if s [ i ] != s [ 0 ] : return False return True
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean allCharactersSame ( String s ) { int n = s . length ( ) ; for ( int i = 1 ; i < n ; i ++ ) if ( s . charAt ( i ) != s . charAt ( 0 ) ) return false ; return true ; } </Code>
java-python-MAXIMUM_DECIMAL_VALUE_PATH_IN_A_BINARY_MATRIX
java-python
static int maxDecimalValue ( int mat [ ] [ ] , int i , int j , int p ) { if ( i >= N || j >= N ) { return 0 ; } int result = Math . max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) { return ( int ) ( Math . pow ( 2 , p ) + re...
def maxDecimalValue ( mat , i , j , p ) : if i >= N or j >= N : return 0 result = max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) if mat [ i ] [ j ] == 1 : return pow ( 2 , p ) + result else : return result
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxDecimalValue ( int mat [ ] [ ] , int i , int j , int p ) { if ( i >= N || j >= N ) { return 0 ; } int result = Math . max ( maxDecimalValue ( mat , i , j + 1 , p + 1 ) , maxDecimalValue ( mat , i + 1 , j , p + 1 ) ) ; if ( mat [ i ] [ j ] == 1 ) { return ( int ) ( Math . pow ( 2 , p...
java-python-COUNT_BALANCED_BINARY_TREES_HEIGHT_H
java-python
public static long countBT ( int h ) { long [ ] dp = new long [ h + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; }
def countBT ( h ) : MOD = 1000000007 dp = [ 0 for i in range ( h + 1 ) ] dp [ 0 ] = 1 dp [ 1 ] = 1 for i in range ( 2 , h + 1 ) : dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD return dp [ h ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static long countBT ( int h ) { long [ ] dp = new long [ h + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; } </Code>
java-python-COUNTING_INVERSIONS
java-python
static int getInvCount ( int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; }
def getInvCount ( arr , n ) : inv_count = 0 for i in range ( n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : inv_count += 1 return inv_count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int getInvCount ( int n ) { int inv_count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) inv_count ++ ; return inv_count ; } </Code>
java-python-COORDINATES_RECTANGLE_GIVEN_POINTS_LIE_INSIDE
java-python
static void printRect ( Integer X [ ] , Integer Y [ ] , int n ) { int Xmax = Collections . max ( Arrays . asList ( X ) ) ; int Xmin = Collections . min ( Arrays . asList ( X ) ) ; int Ymax = Collections . max ( Arrays . asList ( Y ) ) ; int Ymin = Collections . min ( Arrays . asList ( Y ) ) ; System . out . p...
def printRect ( X , Y , n ) : Xmax = max ( X ) Xmin = min ( X ) Ymax = max ( Y ) Ymin = min ( Y ) print ( "{" , Xmin , ", " , Ymin , "}" , sep = "" ) print ( "{" , Xmin , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymax , "}" , sep = "" ) print ( "{" , Xmax , ", " , Ymin ,...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printRect ( Integer X [ ] , Integer Y [ ] , int n ) { int Xmax = Collections . max ( Arrays . asList ( X ) ) ; int Xmin = Collections . min ( Arrays . asList ( X ) ) ; int Ymax = Collections . max ( Arrays . asList ( Y ) ) ; int Ymin = Collections . min ( Arrays . asList ( Y ) ) ; System . ...
java-python-DELANNOY_NUMBER_1
java-python
static int dealnnoy ( int n , int m ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i - 1 ] [ ...
def dealnnoy ( n , m ) : dp = [ [ 0 for x in range ( n + 1 ) ] for x in range ( m + 1 ) ] for i in range ( m ) : dp [ 0 ] [ i ] = 1 for i in range ( 1 , m + 1 ) : dp [ i ] [ 0 ] = 1 for i in range ( 1 , m + 1 ) : for j in range ( 1 , n + 1 ) : dp [ i ] [ j ] = dp [ i ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int dealnnoy ( int n , int m ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= m ; i ++ ) for ( int j = 1 ; j <= n ; j ++ ) dp [ i ] [ j ] = dp [ i -...
java-python-MODULAR_EXPONENTIATION_POWER_IN_MODULAR_ARITHMETIC
java-python
static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; }
def power ( x , y , p ) : res = 1 x = x % p while ( y > 0 ) : if ( ( y & 1 ) == 1 ) : res = ( res * x ) % p y = y >> 1 x = ( x * x ) % p return res
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } </Code>
java-python-PROGRAM_CHECK_ISBN
java-python
static boolean isValidISBN ( String isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn . charAt ( i ) - ' 0 ' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn . ...
def isValidISBN ( isbn ) : if len ( isbn ) != 10 : return False _sum = 0 for i in range ( 9 ) : if 0 <= int ( isbn [ i ] ) <= 9 : _sum += int ( isbn [ i ] ) * ( 10 - i ) else : return False if ( isbn [ 9 ] != 'X' and 0 <= int ( isbn [ 9 ] ) <= 9 ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isValidISBN ( String isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn . charAt ( i ) - ' 0 ' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = ...
java-python-NEXT_GREATER_FREQUENCY_ELEMENT
java-python
static void NFG ( int a [ ] , int n , int freq [ ] ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; int res [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( freq [ a [ s . peek ( ) ] ] > freq [ a [ i ] ] ) s . push...
def NFG ( a , n ) : if ( n <= 0 ) : print ( "List empty" ) return [ ] stack = [ 0 ] * n freq = { } for i in a : freq [ a [ i ] ] = 0 for i in a : freq [ a [ i ] ] += 1 res = [ 0 ] * n top = - 1 top += 1 stack [ top ] = 0 for i in range ( 1 , n ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void NFG ( int a [ ] , int n , int freq [ ] ) { Stack < Integer > s = new Stack < Integer > ( ) ; s . push ( 0 ) ; int res [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) res [ i ] = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( freq [ a [ s . peek ( ) ] ] > freq [ a [ i ] ] ) s...
java-python-SWAP_BITS_IN_A_GIVEN_NUMBER
java-python
static int swapBits ( int x , int p1 , int p2 , int n ) { int set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) ; int set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) ; int xor = ( set1 ^ set2 ) ; xor = ( xor << p1 )
def swapBits ( x , p1 , p2 , n ) : set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) xor = ( set1 ^ set2 ) xor = ( xor << p1 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int swapBits ( int x , int p1 , int p2 , int n ) { int set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) ; int set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) ; int xor = ( set1 ^ set2 ) ; xor = ( xor << p1 ) </Code>
java-python-PRINT_EQUAL_SUM_SETS_ARRAY_PARTITION_PROBLEM_SET_2
java-python
static void printEqualSumSets ( int [ ] arr , int n ) { int i , currSum , sum = 0 ; for ( i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; if ( ( sum & 1 ) == 1 ) { System . out . print ( "-1" ) ; return ; } int k = sum >> 1 ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ k + 1 ] ; for ( i ...
def printEqualSumSets ( arr , n ) : sum_array = sum ( arr ) if ( sum_array & 1 ) : print ( "-1" ) return k = sum_array >> 1 dp = np.zeros ( ( n + 1 , k + 1 ) ) for i in range ( 1 , k + 1 ) : dp [ 0 ] [ i ] = False for i in range ( n + 1 ) : dp [ i ] [ 0 ] = True ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printEqualSumSets ( int [ ] arr , int n ) { int i , currSum , sum = 0 ; for ( i = 0 ; i < arr . length ; i ++ ) sum += arr [ i ] ; if ( ( sum & 1 ) == 1 ) { System . out . print ( "-1" ) ; return ; } int k = sum >> 1 ; boolean [ ] [ ] dp = new boolean [ n + 1 ] [ k + 1 ] ; f...
java-python-HYPERCUBE_GRAPH
java-python
static int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; }
def power ( n ) : if n == 1 : return 2 return 2 * power ( n - 1 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int power ( int n ) { if ( n == 1 ) return 2 ; return 2 * power ( n - 1 ) ; } </Code>
java-python-FIND_ELEMENTS_ARRAY_LEAST_TWO_GREATER_ELEMENTS_2
java-python
static void findElements ( int arr [ ] , int n ) { int first = Integer . MIN_VALUE ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( i...
def findElements ( arr , n ) : first = - sys.maxsize second = - sys.maxsize for i in range ( 0 , n ) : if ( arr [ i ] > first ) : second = first first = arr [ i ] elif ( arr [ i ] > second ) : second = arr [ i ] for i in range ( 0 , n ) : if ( ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void findElements ( int arr [ ] , int n ) { int first = Integer . MIN_VALUE ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } ...
java-python-DISTRIBUTING_M_ITEMS_CIRCLE_SIZE_N_STARTING_K_TH_POSITION
java-python
static int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; }
def lastPosition ( n , m , k ) : if ( m <= n - k + 1 ) : return m + k - 1 m = m - ( n - k + 1 ) if ( m % n == 0 ) : return n else : return m % n
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int lastPosition ( int n , int m , int k ) { if ( m <= n - k + 1 ) return m + k - 1 ; m = m - ( n - k + 1 ) ; return ( m % n == 0 ) ? n : ( m % n ) ; } </Code>
java-python-PROGRAM_REVERSE_STRING_ITERATIVE_RECURSIVE_1
java-python
static void reverseStr ( String str ) { int n = str . length ( ) ; char [ ] ch = str . toCharArray ( ) ; char temp ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; } System . out . println ( ch ) ; }
def reverseStr ( str ) : n = len ( str ) i , j = 0 , n - 1 while i < j : str [ i ] , str [ j ] = str [ j ] , str [ i ] i += 1 j -= 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void reverseStr ( String str ) { int n = str . length ( ) ; char [ ] ch = str . toCharArray ( ) ; char temp ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; } System . out . println ( ch ) ; } </Code>
java-python-FIND_THE_MAXIMUM_ELEMENT_IN_AN_ARRAY_WHICH_IS_FIRST_INCREASING_AND_THEN_DECREASING
java-python
static int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; }
def findMaximum ( arr , low , high ) : max = arr [ low ] i = low for i in range ( high + 1 ) : if arr [ i ] > max : max = arr [ i ] return max
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } </Code>
java-python-LONGEST_PREFIX_ALSO_SUFFIX_1
java-python
static int longestPrefixSuffix ( String s ) { int n = s . length ( ) ; int lps [ ] = new int [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0 ) { ...
def longestPrefixSuffix ( s ) : n = len ( s ) lps = [ 0 ] * n l = 0 i = 1 while ( i < n ) : if ( s [ i ] == s [ l ] ) : l = l + 1 lps [ i ] = l i = i + 1 else : if ( l != 0 ) : l = lps [ l - 1 ] else : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int longestPrefixSuffix ( String s ) { int n = s . length ( ) ; int lps [ ] = new int [ n ] ; lps [ 0 ] = 0 ; int len = 0 ; int i = 1 ; while ( i < n ) { if ( s . charAt ( i ) == s . charAt ( len ) ) { len ++ ; lps [ i ] = len ; i ++ ; } else { if ( len != 0...
java-python-SUM_AREA_RECTANGLES_POSSIBLE_ARRAY
java-python
static int MaxTotalRectangleArea ( int [ ] a , int n ) { Arrays . sort ( a ) ; int sum = 0 ; boolean flag = false ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] || a [ i ] - a [ i + 1 ] == 1 ) && ! flag ) { flag = true ; len = a [ i + 1 ] ; i ++ ; ...
def MaxTotalRectangleArea ( a , n ) : a.sort ( reverse = True ) sum = 0 flag = False len = 0 i = 0 while ( i < n - 1 ) : if ( i != 0 ) : i = i + 1 if ( ( a [ i ] == a [ i + 1 ] or a [ i ] - a [ i + 1 ] == 1 ) and flag == False ) : flag = True l...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int MaxTotalRectangleArea ( int [ ] a , int n ) { Arrays . sort ( a ) ; int sum = 0 ; boolean flag = false ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] == a [ i + 1 ] || a [ i ] - a [ i + 1 ] == 1 ) && ! flag ) { flag = true ; len = a [ i + 1 ] ; i ...
java-python-PROGRAM_TO_PRINT_FIRST_N_FIBONACCI_NUMBERS
java-python
static void printFibonacciNumbers ( int n ) { int f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( f2 + " " ) ; int next = f1 + f2 ; f1 = f2 ; f2 = next ; } }
def printFibonacciNumbers ( n ) : f1 = 0 f2 = 1 if ( n < 1 ) : return for x in range ( 0 , n ) : print ( f2 , end = " " ) next = f1 + f2 f1 = f2 f2 = next
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printFibonacciNumbers ( int n ) { int f1 = 0 , f2 = 1 , i ; if ( n < 1 ) return ; for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( f2 + " " ) ; int next = f1 + f2 ; f1 = f2 ; f2 = next ; } } </Code>
java-python-LEXICOGRAPHICALLY_SMALLEST_ARRAY_K_CONSECUTIVE_SWAPS
java-python
static void minimizeWithKSwaps ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 && k > 0 ; ++ i ) { int pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( j - i > k ) break ; if ( arr [ j ] < arr [ pos ] ) pos = j ; } int temp ; for ( int j = pos ; j > i...
def minimizeWithKSwaps ( arr , n , k ) : for i in range ( n - 1 ) : pos = i for j in range ( i + 1 , n ) : if ( j - i > k ) : break if ( arr [ j ] < arr [ pos ] ) : pos = j for j in range ( pos , i , - 1 ) : arr [ j ] , arr ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void minimizeWithKSwaps ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < n - 1 && k > 0 ; ++ i ) { int pos = i ; for ( int j = i + 1 ; j < n ; ++ j ) { if ( j - i > k ) break ; if ( arr [ j ] < arr [ pos ] ) pos = j ; } int temp ; for ( int j = pos ; ...
java-python-HOW_TO_CHECK_IF_A_GIVEN_ARRAY_REPRESENTS_A_BINARY_HEAP
java-python
static boolean isHeap ( int arr [ ] , int i , int n ) { if ( i > ( n - 2 ) / 2 ) { return true ; } if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) { return true ; } return false ; }
def isHeap ( arr , i , n ) : if i > int ( ( n - 2 ) / 2 ) : return True if ( arr [ i ] >= arr [ 2 * i + 1 ] and arr [ i ] >= arr [ 2 * i + 2 ] and isHeap ( arr , 2 * i + 1 , n ) and isHeap ( arr , 2 * i + 2 , n ) ) : return True return False
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isHeap ( int arr [ ] , int i , int n ) { if ( i > ( n - 2 ) / 2 ) { return true ; } if ( arr [ i ] >= arr [ 2 * i + 1 ] && arr [ i ] >= arr [ 2 * i + 2 ] && isHeap ( arr , 2 * i + 1 , n ) && isHeap ( arr , 2 * i + 2 , n ) ) { return true ; } return false ; } </Code>
java-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF_3
java-python
static int findLength ( String str , int n ) { int ans = 0 ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { int l = i , r = i + 1 ; int lsum = 0 , rsum = 0 ; while ( r < n && l >= 0 ) { lsum += str . charAt ( l ) - ' 0 ' ; rsum += str . charAt ( r ) - ' 0 ' ; if ( lsum == rsum ) { ...
def findLength ( st , n ) : total = [ 0 ] * ( n + 1 ) for i in range ( 1 , n + 1 ) : total [ i ] = ( total [ i - 1 ] + int ( st [ i - 1 ] ) - int ( '0' ) ) ans = 0 l = 2 while ( l <= n ) : for i in range ( n - l + 1 ) : j = i + l - 1 if ( total [ i + int ( l /...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findLength ( String str , int n ) { int ans = 0 ; for ( int i = 0 ; i <= n - 2 ; i ++ ) { int l = i , r = i + 1 ; int lsum = 0 , rsum = 0 ; while ( r < n && l >= 0 ) { lsum += str . charAt ( l ) - ' 0 ' ; rsum += str . charAt ( r ) - ' 0 ' ; if ( lsum == rsum ) { ...
java-python-COUNT_SUBARRAYS_WITH_SAME_EVEN_AND_ODD_ELEMENTS
java-python
static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; } ...
def countSubarrays ( arr , n ) : difference = 0 ans = 0 hash_positive = [ 0 ] * ( n + 1 ) hash_negative = [ 0 ] * ( n + 1 ) hash_positive [ 0 ] = 1 for i in range ( n ) : if ( arr [ i ] & 1 == 1 ) : difference = difference + 1 else : difference = differenc...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countSubarrays ( int [ ] arr , int n ) { int difference = 0 ; int ans = 0 ; int [ ] hash_positive = new int [ n + 1 ] ; int [ ] hash_negative = new int [ n + 1 ] ; hash_positive [ 0 ] = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) == 1 ) { difference ++ ; ...
java-python-KNAPSACK_PROBLEM_1
java-python
static int knapSack ( 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 ] = max ( v...
def knapSack ( W , wt , val , n ) : K = [ [ 0 for x in range ( W + 1 ) ] for x 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 ( val [ i...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int knapSack ( 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 ] = ...
java-python-C_PROGRAM_SUBTRACTION_MATICES
java-python
static void multiply ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; }
def multiply ( A , B , C ) : for i in range ( N ) : for j in range ( N ) : C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void multiply ( int A [ ] [ ] , int B [ ] [ ] , int C [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] - B [ i ] [ j ] ; } </Code>
java-python-MAXIMUM_VALUE_CHOICE_EITHER_DIVIDING_CONSIDERING
java-python
static int maxDP ( int n ) { int res [ ] = new int [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = Math . max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; }
def maxDP ( n ) : res = list ( ) res.append ( 0 ) res.append ( 1 ) i = 2 while i < n + 1 : res.append ( max ( i , ( res [ int ( i / 2 ) ] + res [ int ( i / 3 ) ] + res [ int ( i / 4 ) ] + res [ int ( i / 5 ) ] ) ) ) i = i + 1 return res [ n ]
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxDP ( int n ) { int res [ ] = new int [ n + 1 ] ; res [ 0 ] = 0 ; res [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res [ i ] = Math . max ( i , ( res [ i / 2 ] + res [ i / 3 ] + res [ i / 4 ] + res [ i / 5 ] ) ) ; return res [ n ] ; } </Code>
java-python-DIFFERENCE_BETWEEN_HIGHEST_AND_LEAST_FREQUENCIES_IN_AN_ARRAY
java-python
static int findDiff ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = Math . max ( max_count , count ) ; ...
def findDiff ( arr , n ) : arr.sort ( ) count = 0 max_count = 0 min_count = n for i in range ( 0 , ( n - 1 ) ) : if arr [ i ] == arr [ i + 1 ] : count += 1 continue else : max_count = max ( max_count , count ) min_count = min ( min_count , coun...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findDiff ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 0 , max_count = 0 , min_count = n ; for ( int i = 0 ; i < ( n - 1 ) ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { count += 1 ; continue ; } else { max_count = Math . max ( max_count , count ...
java-python-NUMBER_SINK_NODES_GRAPH
java-python
static int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; }
def countSink ( n , m , edgeFrom , edgeTo ) : mark = [ 0 ] * ( n + 1 ) for i in range ( m ) : mark [ edgeFrom [ i ] ] = 1 count = 0 for i in range ( 1 , n + 1 ) : if ( not mark [ i ] ) : count += 1 return count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countSink ( int n , int m , int edgeFrom [ ] , int edgeTo [ ] ) { int [ ] mark = new int [ n + 1 ] ; for ( int i = 0 ; i < m ; i ++ ) mark [ edgeFrom [ i ] ] = 1 ; int count = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( mark [ i ] == 0 ) count ++ ; return count ; } </Code>
java-python-COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX
java-python
static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; }
def countNegative ( M , n , m ) : count = 0 for i in range ( n ) : for j in range ( m ) : if M [ i ] [ j ] < 0 : count += 1 else : break return count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; } </Code>
java-python-MIDDLE_OF_THREE_USING_MINIMUM_COMPARISONS_1
java-python
public static int middleOfThree ( int a , int b , int c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } }
def middleOfThree ( a , b , c ) : if a > b : if ( b > c ) : return b elif ( a > c ) : return c else : return a else : if ( a > c ) : return a elif ( b > c ) : return c else : return b
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int middleOfThree ( int a , int b , int c ) { if ( a > b ) { if ( b > c ) return b ; else if ( a > c ) return c ; else return a ; } else { if ( a > c ) return a ; else if ( b > c ) return c ; else return b ; } } </Code>
java-python-COUNT_SUBSTRINGS_BINARY_STRING_CONTAINING_K_ONES
java-python
static int countOfSubstringWithKOnes ( String s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int [ ] freq = new int [ N + 1 ] ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s . charAt ( i ) - ' 0 ' ) ; if ( countOfOne >= K ) { res += freq [...
def countOfSubstringWithKOnes ( s , K ) : N = len ( s ) res = 0 countOfOne = 0 freq = [ 0 for i in range ( N + 1 ) ] freq [ 0 ] = 1 for i in range ( 0 , N , 1 ) : countOfOne += ord ( s [ i ] ) - ord ( '0' ) if ( countOfOne >= K ) : res += freq [ countOfOne - K ] ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countOfSubstringWithKOnes ( String s , int K ) { int N = s . length ( ) ; int res = 0 ; int countOfOne = 0 ; int [ ] freq = new int [ N + 1 ] ; freq [ 0 ] = 1 ; for ( int i = 0 ; i < N ; i ++ ) { countOfOne += ( s . charAt ( i ) - ' 0 ' ) ; if ( countOfOne >= K ) { res +=...
java-python-FIND_FOUR_ELEMENTS_A_B_C_AND_D_IN_AN_ARRAY_SUCH_THAT_AB_CD
java-python
boolean findPairs ( int arr [ ] ) { HashMap < Integer , pair > map = new HashMap < Integer , pair > ( ) ; int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . containsKey ( sum ) ) map . put ( su...
def findPairs ( arr , n ) : Hash = { } for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : sum = arr [ i ] + arr [ j ] if sum in Hash.keys ( ) : prev = Hash.get ( sum ) print ( str ( prev ) + " and (%d, %d)" % ( arr [ i ] , arr [ j ] ) ) return True else : Hash [ su...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> boolean findPairs ( int arr [ ] ) { HashMap < Integer , pair > map = new HashMap < Integer , pair > ( ) ; int n = arr . length ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int sum = arr [ i ] + arr [ j ] ; if ( ! map . containsKey ( sum ) ) map . p...
java-python-LINEAR_SEARCH
java-python
public static int search ( int arr [ ] , int x ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; }
def search ( arr , n , x ) : for i in range ( 0 , n ) : if ( arr [ i ] == x ) : return i return - 1
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int search ( int arr [ ] , int x ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; } </Code>
java-python-CHECK_POSSIBLE_TRANSFORM_ONE_STRING_ANOTHER
java-python
static boolean check ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; ...
def check ( s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) dp = ( [ [ False for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] ) dp [ 0 ] [ 0 ] = True for i in range ( len ( s1 ) ) : for j in range ( len ( s2 ) + 1 ) : if ( dp [ i ] [ j ] ) : if ( ( j < len ( s2 ) a...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean check ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = t...
java-python-PROGRAM_BINARY_DECIMAL_CONVERSION_1
java-python
static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == ' 1 ' ) dec_value += base ; base = base * 2 ; } return dec_value ; }
def binaryToDecimal ( n ) : num = n dec_value = 0 base1 = 1 len1 = len ( num ) for i in range ( len1 - 1 , - 1 , - 1 ) : if ( num [ i ] == '1' ) : dec_value += base1 base1 = base1 * 2 return dec_value
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == ' 1 ' ) dec_value += base ; base = base * 2 ; } return dec_value ; } </Code>
java-python-COUNT_WAYS_BUILD_STREET_GIVEN_CONSTRAINTS
java-python
static long countWays ( int n ) { long dp [ ] [ ] = new long [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return dp [ 0 ]...
def countWays ( n ) : dp = [ [ 0 ] * ( n + 1 ) for i in range ( 2 ) ] dp [ 0 ] [ 1 ] = 1 dp [ 1 ] [ 1 ] = 2 for i in range ( 2 , n + 1 ) : dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] dp [ 1 ] [ i ] = ( dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ) return dp [ 0 ] [ n ] +...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static long countWays ( int n ) { long dp [ ] [ ] = new long [ 2 ] [ n + 1 ] ; dp [ 0 ] [ 1 ] = 1 ; dp [ 1 ] [ 1 ] = 2 ; for ( int i = 2 ; i <= n ; i ++ ) { dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + dp [ 1 ] [ i - 1 ] ; dp [ 1 ] [ i ] = dp [ 0 ] [ i - 1 ] * 2 + dp [ 1 ] [ i - 1 ] ; } return d...
java-python-MIRROR_CHARACTERS_STRING
java-python
static String compute ( String str , int n ) { String reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ; int l = str . length ( ) ; String answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer = answer + str . charAt ( i ) ; for ( int i = n ; i < l ; i ++ ) answer = answer + reverseAlphabet . charAt ( s...
def compute ( st , n ) : reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" l = len ( st ) answer = "" for i in range ( 0 , n ) : answer = answer + st [ i ] for i in range ( n , l ) : answer = ( answer + reverseAlphabet [ ord ( st [ i ] ) - ord ( 'a' ) ] ) return answer
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static String compute ( String str , int n ) { String reverseAlphabet = "zyxwvutsrqponmlkjihgfedcba" ; int l = str . length ( ) ; String answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer = answer + str . charAt ( i ) ; for ( int i = n ; i < l ; i ++ ) answer = answer + reverseAlphabet . cha...
java-python-MAXIMUM_LENGTH_SUBSEQUENCE_DIFFERENCE_ADJACENT_ELEMENTS_EITHER_0_1
java-python
public static int maxLenSub ( int arr [ ] , int n ) { int mls [ ] = new int [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( Math . abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ] = mls...
def maxLenSub ( arr , n ) : mls = [ ] max = 0 for i in range ( n ) : mls.append ( 1 ) for i in range ( n ) : for j in range ( i ) : if ( abs ( arr [ i ] - arr [ j ] ) <= 1 and mls [ i ] < mls [ j ] + 1 ) : mls [ i ] = mls [ j ] + 1 for i in range ( n ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int maxLenSub ( int arr [ ] , int n ) { int mls [ ] = new int [ n ] , max = 0 ; for ( int i = 0 ; i < n ; i ++ ) mls [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( Math . abs ( arr [ i ] - arr [ j ] ) <= 1 && mls [ i ] < mls [ j ] + 1 ) mls [ i ...
java-python-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION_1
java-python
static long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; }
def exponentiation ( bas , exp ) : t = 1 while ( exp > 0 ) : if ( exp % 2 != 0 ) : t = ( t * bas ) % N bas = ( bas * bas ) % N exp = int ( exp / 2 ) return t % N
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static long exponentiation ( long base , long exp ) { long t = 1L ; while ( exp > 0 ) { if ( exp % 2 != 0 ) t = ( t * base ) % N ; base = ( base * base ) % N ; exp /= 2 ; } return t % N ; } </Code>
java-python-NEWMAN_CONWAY_SEQUENCE_1
java-python
static int sequence ( int n ) { int f [ ] = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; int i ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; }
def sequence ( n ) : f = array.array ( 'i' , [ 0 , 1 , 1 ] ) for i in range ( 3 , n + 1 ) : r = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] f.append ( r ) ; return r
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int sequence ( int n ) { int f [ ] = new int [ n + 1 ] ; f [ 0 ] = 0 ; f [ 1 ] = 1 ; f [ 2 ] = 1 ; int i ; for ( i = 3 ; i <= n ; i ++ ) f [ i ] = f [ f [ i - 1 ] ] + f [ i - f [ i - 1 ] ] ; return f [ n ] ; } </Code>
java-python-DYNAMIC_PROGRAMMING_SET_15_LONGEST_BITONIC_SUBSEQUENCE
java-python
static int lbs ( int arr [ ] , int n ) { int i , j ; int [ ] lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int [ ] lds = new int [ n ...
def lbs ( arr ) : n = len ( arr ) lis = [ 1 for i in range ( n + 1 ) ] for i in range ( 1 , n ) : for j in range ( 0 , i ) : if ( ( arr [ i ] > arr [ j ] ) and ( lis [ i ] < lis [ j ] + 1 ) ) : lis [ i ] = lis [ j ] + 1 lds = [ 1 for i in range ( n + 1 ) ] for i i...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int lbs ( int arr [ ] , int n ) { int i , j ; int [ ] lis = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( i = 1 ; i < n ; i ++ ) for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; int [ ] lds = new i...
java-python-MAXIMUM_SUM_INCREASING_ORDER_ELEMENTS_N_ARRAYS
java-python
static int maximumSum ( int a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a , i , n ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] < prev )...
def maximumSum ( a , n ) : global M for i in range ( 0 , n ) : a [ i ].sort ( ) sum = a [ n - 1 ] [ M - 1 ] prev = a [ n - 1 ] [ M - 1 ] for i in range ( n - 2 , - 1 , - 1 ) : for j in range ( M - 1 , - 1 , - 1 ) : if ( a [ i ] [ j ] < prev ) : prev = a [ ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maximumSum ( int a [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sort ( a , i , n ) ; int sum = a [ n - 1 ] [ M - 1 ] ; int prev = a [ n - 1 ] [ M - 1 ] ; int i , j ; for ( i = n - 2 ; i >= 0 ; i -- ) { for ( j = M - 1 ; j >= 0 ; j -- ) { if ( a [ i ] [ j ] <...
java-python-MASTER_THEOREM_SUBTRACT_CONQUER_RECURRENCES
java-python
static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; }
def fib ( n ) : if ( n <= 1 ) : return n return fib ( n - 1 ) + fib ( n - 2 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } </Code>
java-python-COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS
java-python
static int countPairs ( String str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str . charAt ( i ) - str . charAt ( j ) ) == Math . abs ( i - j ) ) result ++ ; return result ; }
def countPairs ( str1 ) : result = 0 ; n = len ( str1 ) for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( abs ( ord ( str1 [ i ] ) - ord ( str1 [ j ] ) ) == abs ( i - j ) ) : result += 1 ; return result ;
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countPairs ( String str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str . charAt ( i ) - str . charAt ( j ) ) == Math . abs ( i - j ) ) result ++ ; return result ; } </Code>
java-python-CHECK_IF_ALL_THE_ELEMENTS_CAN_BE_MADE_OF_SAME_PARITY_BY_INVERTING_ADJACENT_ELEMENTS
java-python
static boolean flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; }
def flipsPossible ( a , n ) : count_odd = 0 count_even = 0 for i in range ( n ) : if ( a [ i ] & 1 ) : count_odd += 1 else : count_even += 1 if ( count_odd % 2 and count_even % 2 ) : return False else : return True
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; } </Code>
java-python-CHECK_GIVEN_ARRAY_CONTAINS_DUPLICATE_ELEMENTS_WITHIN_K_DISTANCE
java-python
static boolean checkDuplicatesWithinK ( int arr [ ] , int k ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( set . contains ( arr [ i ] ) ) return true ; set . add ( arr [ i ] ) ; if ( i >= k ) set . remove ( arr [ i - k ] ) ; } return false ...
def checkDuplicatesWithinK ( arr , n , k ) : myset = [ ] for i in range ( n ) : if arr [ i ] in myset : return True myset.append ( arr [ i ] ) if ( i >= k ) : myset.remove ( arr [ i - k ] ) return False
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean checkDuplicatesWithinK ( int arr [ ] , int k ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( set . contains ( arr [ i ] ) ) return true ; set . add ( arr [ i ] ) ; if ( i >= k ) set . remove ( arr [ i - k ] ) ; } return...
java-python-SQUARES_OF_MATRIX_DIAGONAL_ELEMENTS_1
java-python
static void diagonalsquare ( int mat [ ] [ ] , int row , int column ) { System . out . print ( " Diagonal one : " ) ; for ( int i = 0 ; i < row ; i ++ ) { System . out . print ( mat [ i ] [ i ] * mat [ i ] [ i ] + " " ) ; } System . out . println ( ) ; System . out . print ( " Diagonal two : " ) ; f...
def diagonalsquare ( mat , row , column ) : print ( "Diagonal one : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ i ] * mat [ i ] [ i ] , end = "" ) print ( "\n\nDiagonal two : " , end = "" ) for i in range ( 0 , row ) : print ( mat [ i ] [ row - i - 1 ] * mat [ i ] [ r...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void diagonalsquare ( int mat [ ] [ ] , int row , int column ) { System . out . print ( " Diagonal one : " ) ; for ( int i = 0 ; i < row ; i ++ ) { System . out . print ( mat [ i ] [ i ] * mat [ i ] [ i ] + " " ) ; } System . out . println ( ) ; System . out . print ( " Diagonal two : " ...
java-python-FIND_THE_LARGEST_RECTANGLE_OF_1S_WITH_SWAPPING_OF_COLUMNS_ALLOWED
java-python
static int maxArea ( int mat [ ] [ ] ) { int hist [ ] [ ] = new int [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) { hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } } for ...
def maxArea ( mat ) : hist = [ [ 0 for i in range ( C + 1 ) ] for i in range ( R + 1 ) ] for i in range ( 0 , C , 1 ) : hist [ 0 ] [ i ] = mat [ 0 ] [ i ] for j in range ( 1 , R , 1 ) : if ( ( mat [ j ] [ i ] == 0 ) ) : hist [ j ] [ i ] = 0 else : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxArea ( int mat [ ] [ ] ) { int hist [ ] [ ] = new int [ R + 1 ] [ C + 1 ] ; for ( int i = 0 ; i < C ; i ++ ) { hist [ 0 ] [ i ] = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < R ; j ++ ) { hist [ j ] [ i ] = ( mat [ j ] [ i ] == 0 ) ? 0 : hist [ j - 1 ] [ i ] + 1 ; } }...
java-python-LONGEST_SUBSEQUENCE_SUCH_THAT_DIFFERENCE_BETWEEN_ADJACENTS_IS_ONE
java-python
static int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) dp [ i ...
def longestSubseqWithDiffOne ( arr , n ) : dp = [ 1 for i in range ( n ) ] for i in range ( n ) : for j in range ( i ) : if ( ( arr [ i ] == arr [ j ] + 1 ) or ( arr [ i ] == arr [ j ] - 1 ) ) : dp [ i ] = max ( dp [ i ] , dp [ j ] + 1 ) result = 1 for i in range ( n ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int longestSubseqWithDiffOne ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ i ] == arr [ j ] + 1 ) || ( arr [ i ] == arr [ j ] - 1 ) ) ...
java-python-MAXIMUM_NUMBER_OF_SQUARES_THAT_CAN_BE_FIT_IN_A_RIGHT_ANGLE_ISOSCELES_TRIANGLE
java-python
static int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; }
def maxSquare ( b , m ) : return ( b / m - 1 ) * ( b / m ) / 2
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxSquare ( int b , int m ) { return ( b / m - 1 ) * ( b / m ) / 2 ; } </Code>
java-python-MAXIMUM_SUM_PAIRS_SPECIFIC_DIFFERENCE
java-python
static int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int dp [ ] = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = Math . max ( dp [ i ] ...
def maxSumPairWithDifferenceLessThanK ( arr , N , K ) : arr.sort ( ) dp = [ 0 ] * N dp [ 0 ] = 0 for i in range ( 1 , N ) : dp [ i ] = dp [ i - 1 ] if ( arr [ i ] - arr [ i - 1 ] < K ) : if ( i >= 2 ) : dp [ i ] = max ( dp [ i ] , dp [ i - 2 ] + arr [ i ] + ar...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxSumPairWithDifferenceLessThanK ( int arr [ ] , int N , int K ) { Arrays . sort ( arr ) ; int dp [ ] = new int [ N ] ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { dp [ i ] = dp [ i - 1 ] ; if ( arr [ i ] - arr [ i - 1 ] < K ) { if ( i >= 2 ) dp [ i ] = Math . max ( dp...
java-python-MINIMUM_OPERATION_MAKE_ELEMENTS_EQUAL_ARRAY
java-python
public static int minOperation ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; else hash . put ( arr [ i ] , 1 ) ; int max...
def minOperation ( arr , n ) : Hash = defaultdict ( lambda : 0 ) for i in range ( 0 , n ) : Hash [ arr [ i ] ] += 1 max_count = 0 for i in Hash : if max_count < Hash [ i ] : max_count = Hash [ i ] return n - max_count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int minOperation ( int arr [ ] , int n ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; else hash . put ( arr [ i ] , 1 ) ; ...
java-python-FIND_K_PAIRS_SMALLEST_SUMS_TWO_ARRAYS
java-python
static void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { System . out . print ( "k pairs don't exist" ) ; return ; } int index2 [ ] = new int [ n1 ] ; while ( k > 0 ) { int min_sum = Integer . MAX_VALUE ; int min_index = 0 ; for ( int i1 = 0 ...
def kSmallestPair ( arr1 , n1 , arr2 , n2 , k ) : if ( k > n1 * n2 ) : print ( "k pairs don't exist" ) return index2 = [ 0 for i in range ( n1 ) ] while ( k > 0 ) : min_sum = sys.maxsize min_index = 0 for i1 in range ( 0 , n1 , 1 ) : if ( index2 [ i1 ] < n...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void kSmallestPair ( int arr1 [ ] , int n1 , int arr2 [ ] , int n2 , int k ) { if ( k > n1 * n2 ) { System . out . print ( "k pairs don't exist" ) ; return ; } int index2 [ ] = new int [ n1 ] ; while ( k > 0 ) { int min_sum = Integer . MAX_VALUE ; int min_index = 0 ; for ( int ...
java-python-PRINT_A_GIVEN_MATRIX_IN_SPIRAL_FORM
java-python
static void spiralPrint ( int m , int n , int a [ ] [ ] ) { int i , k = 0 , l = 0 ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) { System . out . print ( a [ k ] [ i ] + " " ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { System . out . print ( a [ i ] [ n - 1 ] + ...
def spiralPrint ( m , n , a ) : k = 0 l = 0 while ( k < m and l < n ) : for i in range ( l , n ) : print ( a [ k ] [ i ] , end = " " ) k += 1 for i in range ( k , m ) : print ( a [ i ] [ n - 1 ] , end = " " ) n -= 1 if ( k < m ) : for i...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void spiralPrint ( int m , int n , int a [ ] [ ] ) { int i , k = 0 , l = 0 ; while ( k < m && l < n ) { for ( i = l ; i < n ; ++ i ) { System . out . print ( a [ k ] [ i ] + " " ) ; } k ++ ; for ( i = k ; i < m ; ++ i ) { System . out . print ( a [ i ] [ n -...
java-python-CALCULATE_SUM_OF_ALL_NUMBERS_PRESENT_IN_A_STRING
java-python
static int findSum ( String str ) { String temp = "" ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( Character . isDigit ( ch ) ) temp += ch ; else { sum += Integer . parseInt ( temp ) ; temp = "0" ; } } return sum + Integer...
def findSum ( str1 ) : temp = "" Sum = 0 for ch in str1 : if ( ch.isdigit ( ) ) : temp += ch else : Sum += int ( temp ) temp = "0" return Sum + int ( temp )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findSum ( String str ) { String temp = "" ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( Character . isDigit ( ch ) ) temp += ch ; else { sum += Integer . parseInt ( temp ) ; temp = "0" ; } } return sum + ...
java-python-NTH_PENTAGONAL_NUMBER
java-python
int pentagonalNum ( int n ) { return ( 3 * n * n - n ) / 2 ; }
def pentagonalNum ( n ) : return ( 3 * n * n - n ) / 2
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> int pentagonalNum ( int n ) { return ( 3 * n * n - n ) / 2 ; } </Code>
java-python-TOTAL_NUMBER_OF_NON_DECREASING_NUMBERS_WITH_N_DIGITS_1
java-python
static long countNonDecreasing ( int n ) { int N = 10 ; long count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { count *= ( N + i - 1 ) ; count /= i ; } return count ; }
def countNonDecreasing ( n ) : N = 10 count = 1 for i in range ( 1 , n + 1 ) : count = int ( count * ( N + i - 1 ) ) count = int ( count / i ) return count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static long countNonDecreasing ( int n ) { int N = 10 ; long count = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { count *= ( N + i - 1 ) ; count /= i ; } return count ; } </Code>
java-python-FREQUENT_ELEMENT_ARRAY_1
java-python
static int mostFrequent ( int arr [ ] , int n ) { Map < Integer , Integer > hp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . containsKey ( key ) ) { int freq = hp . get ( key ) ; freq ++ ; hp . put ( key , freq ) ; } ...
def mostFrequent ( arr , n ) : Hash = dict ( ) for i in range ( n ) : if arr [ i ] in Hash.keys ( ) : Hash [ arr [ i ] ] += 1 else : Hash [ arr [ i ] ] = 1 max_count = 0 res = - 1 for i in Hash : if ( max_count < Hash [ i ] ) : res = i ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int mostFrequent ( int arr [ ] , int n ) { Map < Integer , Integer > hp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int key = arr [ i ] ; if ( hp . containsKey ( key ) ) { int freq = hp . get ( key ) ; freq ++ ; hp . put ( key , freq ) ; ...
java-python-K_TH_ELEMENT_TWO_SORTED_ARRAYS
java-python
static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ...
def kth ( arr1 , arr2 , m , n , k ) : sorted1 = [ 0 ] * ( m + n ) i = 0 j = 0 d = 0 while ( i < m and j < n ) : if ( arr1 [ i ] < arr2 [ j ] ) : sorted1 [ d ] = arr1 [ i ] i += 1 else : sorted1 [ d ] = arr2 [ j ] j += 1 d += 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorte...
java-python-DYNAMIC_PROGRAMMING_SET_9_BINOMIAL_COEFFICIENT
java-python
static int binomialCoeff ( int n , int k ) { if ( k == 0 || k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; }
def binomialCoeff ( n , k ) : if k == 0 or k == n : return 1 return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int binomialCoeff ( int n , int k ) { if ( k == 0 || k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } </Code>
java-python-STRING_CONTAINING_FIRST_LETTER_EVERY_WORD_GIVEN_STRING_SPACES
java-python
static String firstLetterWord ( String str ) { String result = "" ; boolean v = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) { v = true ; } else if ( str . charAt ( i ) != ' ▁ ' && v == true ) { result += ( str . charAt ( i ) ) ; v =...
def firstLetterWord ( str ) : result = "" v = True for i in range ( len ( str ) ) : if ( str [ i ] == ' ' ) : v = True elif ( str [ i ] != ' ' and v == True ) : result += ( str [ i ] ) v = False return result
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static String firstLetterWord ( String str ) { String result = "" ; boolean v = true ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' ▁ ' ) { v = true ; } else if ( str . charAt ( i ) != ' ▁ ' && v == true ) { result += ( str . charAt ( i ) ) ; ...
java-python-REARRANGE_ARRAY_SUCH_THAT_EVEN_POSITIONED_ARE_GREATER_THAN_ODD
java-python
static void assign ( int a [ ] , int n ) { Arrays . sort ( a ) ; int ans [ ] = new int [ n ] ; int p = 0 , q = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ; else ans [ i ] = a [ p ++ ] ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print (...
def assign ( a , n ) : a.sort ( ) ans = [ 0 ] * n p = 0 q = n - 1 for i in range ( n ) : if ( i + 1 ) % 2 == 0 : ans [ i ] = a [ q ] q = q - 1 else : ans [ i ] = a [ p ] p = p + 1 for i in range ( n ) : print ( ans [ i ] , e...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void assign ( int a [ ] , int n ) { Arrays . sort ( a ) ; int ans [ ] = new int [ n ] ; int p = 0 , q = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( i + 1 ) % 2 == 0 ) ans [ i ] = a [ q -- ] ; else ans [ i ] = a [ p ++ ] ; } for ( int i = 0 ; i < n ; i ++ ) System . out . ...
java-python-PRINT_DISTINCT_ELEMENTS_GIVEN_INTEGER_ARRAY
java-python
static void printDistinct ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) System . out . print ( arr [ i ] + " " ) ; } }
def printDistinct ( arr , n ) : for i in range ( 0 , n ) : d = 0 for j in range ( 0 , i ) : if ( arr [ i ] == arr [ j ] ) : d = 1 break if ( d == 0 ) : print ( arr [ i ] )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void printDistinct ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) System . out . print ( arr [ i ] + " " ) ; } } </Code>
java-python-COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2
java-python
static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; }
def countSolutions ( n ) : res = 0 x = 0 while ( x * x < n ) : y = 0 while ( x * x + y * y < n ) : res = res + 1 y = y + 1 x = x + 1 return res
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; } </Code>
java-python-FIND_RELATIVE_COMPLEMENT_TWO_SORTED_ARRAYS
java-python
static void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( arr1 [ ...
def relativeComplement ( arr1 , arr2 , n , m ) : i = 0 j = 0 while ( i < n and j < m ) : if ( arr1 [ i ] < arr2 [ j ] ) : print ( arr1 [ i ] , "" , end = "" ) i += 1 elif ( arr1 [ i ] > arr2 [ j ] ) : j += 1 elif ( arr1 [ i ] == arr2 [ j ] ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void relativeComplement ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( arr1 [ i ] < arr2 [ j ] ) { System . out . print ( arr1 [ i ] + " " ) ; i ++ ; } else if ( arr1 [ i ] > arr2 [ j ] ) { j ++ ; } else if ( ...
java-python-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D_1
java-python
static int findFourElements ( int [ ] arr , int n ) { HashMap < Integer , Indexes > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { map . put ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ; } } int d = Integer . MIN_VALUE ; ...
def findFourElements ( arr , n ) : mp = dict ( ) for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : mp [ arr [ i ] + arr [ j ] ] = ( i , j ) d = - 10 ** 9 for i in range ( n - 1 ) : for j in range ( i + 1 , n ) : abs_diff = abs ( arr [ i ] - arr [ j ] ) ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findFourElements ( int [ ] arr , int n ) { HashMap < Integer , Indexes > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { map . put ( arr [ i ] + arr [ j ] , new Indexes ( i , j ) ) ; } } int d = Integer . MIN_V...
java-python-COUNT_TRAILING_ZEROES_FACTORIAL_NUMBER
java-python
static int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; }
def findTrailingZeros ( n ) : count = 0 i = 5 while ( n / i >= 1 ) : count += int ( n / i ) i *= 5 return int ( count )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } </Code>
java-python-PRIMALITY_TEST_SET_5USING_LUCAS_LEHMER_SERIES
java-python
static boolean isPrime ( int p ) { double checkNumber = Math . pow ( 2 , p ) - 1 ; double nextval = 4 % checkNumber ; for ( int i = 1 ; i < p - 1 ; i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ; return ( nextval == 0 ) ; }
def isPrime ( p ) : checkNumber = 2 ** p - 1 nextval = 4 % checkNumber for i in range ( 1 , p - 1 ) : nextval = ( nextval * nextval - 2 ) % checkNumber if ( nextval == 0 ) : return True else : return False
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isPrime ( int p ) { double checkNumber = Math . pow ( 2 , p ) - 1 ; double nextval = 4 % checkNumber ; for ( int i = 1 ; i < p - 1 ; i ++ ) nextval = ( nextval * nextval - 2 ) % checkNumber ; return ( nextval == 0 ) ; } </Code>
java-python-COUNT_OF_SUB_STRINGS_THAT_DO_NOT_CONTAIN_ALL_THE_CHARACTERS_FROM_THE_SET_A_B_C_AT_THE_SAME_TIME
java-python
static int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' a ' ) { a_index = i + 1 ; ans -= Math . min ( b_index , c_index ) ; } else if ( str [ ...
def CountSubString ( Str , n ) : ans = ( n * ( n + 1 ) ) // 2 a_index = 0 b_index = 0 c_index = 0 for i in range ( n ) : if ( Str [ i ] == 'a' ) : a_index = i + 1 ans -= min ( b_index , c_index ) elif ( Str [ i ] == 'b' ) : b_index = i + 1 ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == ' a ' ) { a_index = i + 1 ; ans -= Math . min ( b_index , c_index ) ; } else if (...
java-python-CHECK_GIVEN_STRING_ROTATION_PALINDROME
java-python
static boolean isPalindrome ( String str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) if ( str . charAt ( l ++ ) != str . charAt ( h -- ) ) return false ; return true ; }
def isPalindrome ( string ) : l = 0 h = len ( string ) - 1 while h > l : l += 1 h -= 1 if string [ l - 1 ] != string [ h + 1 ] : return False return True
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isPalindrome ( String str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) if ( str . charAt ( l ++ ) != str . charAt ( h -- ) ) return false ; return true ; } </Code>
java-python-MAXIMUM_NUMBER_CHOCOLATES_DISTRIBUTED_EQUALLY_AMONG_K_STUDENTS
java-python
static int maxNumOfChocolates ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int [ ] sum = new int [ n ] ; int curr_rem ; int maxSum = 0 ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ; for ...
def maxNumOfChocolates ( arr , n , k ) : um , curr_rem , maxSum = { } , 0 , 0 sm = [ 0 ] * n sm [ 0 ] = arr [ 0 ] for i in range ( 1 , n ) : sm [ i ] = sm [ i - 1 ] + arr [ i ] for i in range ( n ) : curr_rem = sm [ i ] % k if ( not curr_rem and maxSum < sm [ i ] ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxNumOfChocolates ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int [ ] sum = new int [ n ] ; int curr_rem ; int maxSum = 0 ; sum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) sum [ i ] = sum [ i - 1 ] + arr [ i ] ;...
java-python-DECODE_STRING_RECURSIVELY_ENCODED_COUNT_FOLLOWED_SUBSTRING
java-python
static String decode ( String str ) { Stack < Integer > integerstack = new Stack < > ( ) ; Stack < Character > stringstack = new Stack < > ( ) ; String temp = "" , result = "" ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( Character . isDigit ( str . charAt ( i ) ) ) { ...
def decode ( Str ) : integerstack = [ ] stringstack = [ ] temp = "" result = "" for i in range ( len ( Str ) ) : count = 0 if ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : while ( Str [ i ] >= '0' and Str [ i ] <= '9' ) : count = count * 10 + ord ( Str [ i ]...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static String decode ( String str ) { Stack < Integer > integerstack = new Stack < > ( ) ; Stack < Character > stringstack = new Stack < > ( ) ; String temp = "" , result = "" ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int count = 0 ; if ( Character . isDigit ( str . charAt ( i ) ) )...
java-python-MINIMUM_SUM_CHOOSING_MINIMUM_PAIRS_ARRAY
java-python
static int minSum ( int [ ] A , int n ) { int min_val = Arrays . stream ( A ) . min ( ) . getAsInt ( ) ; return ( min_val * ( n - 1 ) ) ; }
def minSum ( A ) : min_val = min ( A ) ; return min_val * ( len ( A ) - 1 )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int minSum ( int [ ] A , int n ) { int min_val = Arrays . stream ( A ) . min ( ) . getAsInt ( ) ; return ( min_val * ( n - 1 ) ) ; } </Code>
java-python-CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER_1
java-python
static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; }
def isPower ( x , y ) : res1 = math.log ( y ) / math.log ( x ) res2 = math.log ( y ) / math.log ( x ) return 1 if ( res1 == res2 ) else 0
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } </Code>
java-python-COUNT_WORDS_APPEAR_EXACTLY_TWO_TIMES_ARRAY_WORDS
java-python
static int countWords ( String str [ ] , int n ) { HashMap < String , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( str [ i ] ) ) { int get = m . get ( str [ i ] ) ; m . put ( str [ i ] , get + 1 ) ; } else { m . put ( str [ i ] , 1 )...
def countWords ( stri , n ) : m = dict ( ) for i in range ( n ) : m [ stri [ i ] ] = m.get ( stri [ i ] , 0 ) + 1 res = 0 for i in m.values ( ) : if i == 2 : res += 1 return res
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int countWords ( String str [ ] , int n ) { HashMap < String , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( str [ i ] ) ) { int get = m . get ( str [ i ] ) ; m . put ( str [ i ] , get + 1 ) ; } else { m . put ( str [ i ...
java-python-FIND_LARGEST_D_IN_ARRAY_SUCH_THAT_A_B_C_D
java-python
static int findLargestd ( int [ ] S , int n ) { boolean found = false ; Arrays . sort ( S ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; ...
def findLargestd ( S , n ) : found = False S.sort ( ) for i in range ( n - 1 , - 1 , - 1 ) : for j in range ( 0 , n ) : if ( i == j ) : continue for k in range ( j + 1 , n ) : if ( i == k ) : continue for l i...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int findLargestd ( int [ ] S , int n ) { boolean found = false ; Arrays . sort ( S ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) continue ; for ( int k = j + 1 ; k < n ; k ++ ) { if ( i == k ) continue ; ...
java-python-MAXIMUM_MINIMUM_VALUES_ALGEBRAIC_EXPRESSION
java-python
static void minMaxValues ( int [ ] arr , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } boolean dp [ ] [ ] = new boolean [ MAX + 1 ] [ MAX * MAX + 1 ] ; dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { f...
def minMaxValues ( arr , n , m ) : sum = 0 INF = 1000000000 MAX = 50 for i in range ( 0 , ( n + m ) ) : sum += arr [ i ] arr [ i ] += 50 dp = [ [ 0 for x in range ( MAX * MAX + 1 ) ] for y in range ( MAX + 1 ) ] dp [ 0 ] [ 0 ] = 1 for i in range ( 0 , ( n + m ) ) : fo...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void minMaxValues ( int [ ] arr , int n , int m ) { int sum = 0 ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) { sum += arr [ i ] ; arr [ i ] += 50 ; } boolean dp [ ] [ ] = new boolean [ MAX + 1 ] [ MAX * MAX + 1 ] ; dp [ 0 ] [ 0 ] = true ; for ( int i = 0 ; i < ( n + m ) ; i ++ ) ...
java-python-PRINT_POSSIBLE_EDGES_TREE_GIVEN_DIAMETER_HEIGHT_VERTICES
java-python
static void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { System . out . println ( "1 2" ) ; return ; } System . out . println ( "-1" ) ; return ; } if ( d > 2 * h ) { System . out . println ( "-1" ) ; return ; } for ( int i = 1 ; i <...
def constructTree ( n , d , h ) : if d == 1 : if n == 2 and h == 1 : print ( "1 2" ) return 0 print ( "-1" ) return 0 if d > 2 * h : print ( "-1" ) return 0 for i in range ( 1 , h + 1 ) : print ( i , " " , i + 1 ) if d > h : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void constructTree ( int n , int d , int h ) { if ( d == 1 ) { if ( n == 2 && h == 1 ) { System . out . println ( "1 2" ) ; return ; } System . out . println ( "-1" ) ; return ; } if ( d > 2 * h ) { System . out . println ( "-1" ) ; return ; } for ( int i = 1 ...
java-python-NUMBER_RECTANGLES_NM_GRID
java-python
public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; }
def rectCount ( n , m ) : return ( m * n * ( n + 1 ) * ( m + 1 ) ) // 4
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static long rectCount ( int n , int m ) { return ( m * n * ( n + 1 ) * ( m + 1 ) ) / 4 ; } </Code>
java-python-SORTING_USING_TRIVIAL_HASH_FUNCTION
java-python
static void sortUsingHash ( int a [ ] , int n ) { int max = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int hash [ ] = new int [ max + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] += 1 ; for ( int i = 0 ; i <= max ; i ++ ) { if ( hash [ i ] != 0 ) { for ( int j = 0 ; j < ...
def sortUsingHash ( a , n ) : Max = max ( a ) Hash = [ 0 ] * ( Max + 1 ) for i in range ( 0 , n ) : Hash [ a [ i ] ] += 1 for i in range ( 0 , Max + 1 ) : if Hash [ i ] != 0 : for j in range ( 0 , Hash [ i ] ) : print ( i , end = " " )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void sortUsingHash ( int a [ ] , int n ) { int max = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; int hash [ ] = new int [ max + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) hash [ a [ i ] ] += 1 ; for ( int i = 0 ; i <= max ; i ++ ) { if ( hash [ i ] != 0 ) { for ( int j = 0 ; ...
java-python-SUBARRAY_NO_PAIR_SUM_DIVISIBLE_K
java-python
static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) { int [ ] mp = new int [ 1000 ] ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) { ...
def subarrayDivisibleByK ( arr , n , k ) : mp = [ 0 ] * 1000 s = 0 e = 0 maxs = 0 maxe = 0 mp [ arr [ 0 ] % k ] = mp [ arr [ 0 ] % k ] + 1 for i in range ( 1 , n ) : mod = arr [ i ] % k while ( mp [ k - mod ] != 0 or ( mod == 0 and mp [ mod ] != 0 ) ) : mp [ arr [ s ] % k ] =...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void subarrayDivisibleByK ( int [ ] arr , int n , int k ) { int [ ] mp = new int [ 1000 ] ; int s = 0 , e = 0 , maxs = 0 , maxe = 0 ; mp [ arr [ 0 ] % k ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { int mod = arr [ i ] % k ; while ( mp [ k - mod ] != 0 || ( mod == 0 && mp [ mod ] != 0 ) ) ...
java-python-CHECK_DIVISIBILITY_BINARY_STRING_2K
java-python
static boolean isDivisible ( String str , int k ) { int n = str . length ( ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str . charAt ( n - i - 1 ) == ' 0 ' ) c ++ ; return ( c == k ) ; }
def isDivisible ( str , k ) : n = len ( str ) c = 0 for i in range ( 0 , k ) : if ( str [ n - i - 1 ] == '0' ) : c += 1 return ( c == k )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isDivisible ( String str , int k ) { int n = str . length ( ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str . charAt ( n - i - 1 ) == ' 0 ' ) c ++ ; return ( c == k ) ; } </Code>
java-python-NUMBER_DIGITS_REMOVED_MAKE_NUMBER_DIVISIBLE_3
java-python
static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - ' 0 ' ) % 3 ) retu...
def divisible ( num ) : n = len ( num ) sum = 0 for i in range ( n ) : sum += int ( num [ i ] ) if ( sum % 3 == 0 ) : return 0 if ( n == 1 ) : return - 1 for i in range ( n ) : if ( sum % 3 == int ( num [ i ] ) % 3 ) : return 1 if ( n == 2 ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - ' 0 ' ) % 3...
java-python-SHORTEST_COMMON_SUPERSEQUENCE
java-python
static int superSeq ( String X , String Y , int m , int n ) { if ( m == 0 ) return n ; if ( n == 0 ) return m ; if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + Math . min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; }
def superSeq ( X , Y , m , n ) : if ( not m ) : return n if ( not n ) : return m if ( X [ m - 1 ] == Y [ n - 1 ] ) : return 1 + superSeq ( X , Y , m - 1 , n - 1 ) return 1 + min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int superSeq ( String X , String Y , int m , int n ) { if ( m == 0 ) return n ; if ( n == 0 ) return m ; if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) return 1 + superSeq ( X , Y , m - 1 , n - 1 ) ; return 1 + Math . min ( superSeq ( X , Y , m - 1 , n ) , superSeq ( X , Y , m , n - 1 ) ) ; }...
java-python-PROGRAM_SWAP_UPPER_DIAGONAL_ELEMENTS_LOWER_DIAGONAL_ELEMENTS_MATRIX
java-python
static void swapUpperToLower ( int arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = ...
def swapUpperToLower ( arr ) : n = 4 ; for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; for i in range ( 0 , n ) : for j in range ( 0 , n ) : print (...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static void swapUpperToLower ( int arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int temp = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = temp ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( i...
java-python-SUM_K_TH_GROUP_ODD_POSITIVE_NUMBERS
java-python
public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; }
def kthgroupsum ( k ) : cur = int ( ( k * ( k - 1 ) ) + 1 ) sum = 0 while k : sum += cur cur += 2 k = k - 1 return sum
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } </Code>
java-python-CHECK_GIVEN_MATRIX_IS_MAGIC_SQUARE_OR_NOT
java-python
static boolean isMagicSquare ( int mat [ ] [ ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int rowSum = ...
def isMagicSquare ( mat ) : s = 0 for i in range ( 0 , N ) : s = s + mat [ i ] [ i ] s2 = 0 for i in range ( 0 , N ) : s2 = s2 + mat [ i ] [ N - i - 1 ] if ( s != s2 ) : return False for i in range ( 0 , N ) : rowSum = 0 for j in range ( 0 , N ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isMagicSquare ( int mat [ ] [ ] ) { int sum = 0 , sum2 = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum = sum + mat [ i ] [ i ] ; for ( int i = 0 ; i < N ; i ++ ) sum2 = sum2 + mat [ i ] [ N - 1 - i ] ; if ( sum != sum2 ) return false ; for ( int i = 0 ; i < N ; i ++ ) { int ro...
java-python-PROGRAM_CHECK_DIAGONAL_MATRIX_SCALAR_MATRIX_1
java-python
static boolean isScalarMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; }
def isScalarMatrix ( mat ) : for i in range ( 0 , N ) : for j in range ( 0 , N ) : if ( ( i != j ) and ( mat [ i ] [ j ] != 0 ) ) : return False for i in range ( 0 , N - 1 ) : if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) : return False return True...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isScalarMatrix ( int mat [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) if ( ( i != j ) && ( mat [ i ] [ j ] != 0 ) ) return false ; for ( int i = 0 ; i < N - 1 ; i ++ ) if ( mat [ i ] [ i ] != mat [ i + 1 ] [ i + 1 ] ) return false ; return true ; ...
java-python-LONGEST_SUBARRAY_SUM_DIVISIBLE_K
java-python
static int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int mod_arr [ ] = new int [ n ] ; int max = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( curr_sum ...
def longSubarrWthSumDivByK ( arr , n , k ) : um = { i : 0 for i in range ( 8 ) } mod_arr = [ 0 for i in range ( n ) ] max = 0 curr_sum = 0 for i in range ( n ) : curr_sum += arr [ i ] mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k for i in range ( n ) : if ( mod_arr [ i ] =...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int mod_arr [ ] = new int [ n ] ; int max = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( cu...
java-python-REVERSE_A_STRING_USING_RECURSION
java-python
void reverse ( String str ) { if ( ( str == null ) || ( str . length ( ) <= 1 ) ) System . out . println ( str ) ; else { System . out . print ( str . charAt ( str . length ( ) - 1 ) ) ; reverse ( str . substring ( 0 , str . length ( ) - 1 ) ) ; } }
def reverse ( string ) : if len ( string ) == 0 : return temp = string [ 0 ] reverse ( string [ 1 : ] ) print ( temp , end = '' )
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> void reverse ( String str ) { if ( ( str == null ) || ( str . length ( ) <= 1 ) ) System . out . println ( str ) ; else { System . out . print ( str . charAt ( str . length ( ) - 1 ) ) ; reverse ( str . substring ( 0 , str . length ( ) - 1 ) ) ; } } </Code>
java-python-BALANCED_EXPRESSIONS_SUCH_THAT_GIVEN_POSITIONS_HAVE_OPENING_BRACKETS
java-python
static long arrangeBraces ( int n , int pos [ ] , int k ) { boolean h [ ] = new boolean [ N ] ; int dp [ ] [ ] = new int [ N ] [ N ] ; for ( int i = 0 ; i < k ; i ++ ) { h [ pos [ i ] ] = true ; } dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= 2 * n ;...
def arrangeBraces ( n , pos , k ) : h = [ False for i in range ( N ) ] dp = [ [ 0 for i in range ( N ) ] for i in range ( N ) ] for i in range ( k ) : h [ pos [ i ] ] = 1 dp [ 0 ] [ 0 ] = 1 for i in range ( 1 , 2 * n + 1 ) : for j in range ( 2 * n + 1 ) : if ( h [ i ] ) :...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static long arrangeBraces ( int n , int pos [ ] , int k ) { boolean h [ ] = new boolean [ N ] ; int dp [ ] [ ] = new int [ N ] [ N ] ; for ( int i = 0 ; i < k ; i ++ ) { h [ pos [ i ] ] = true ; } dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= 2 * n ; i ++ ) { for ( int j = 0 ; j <= ...
java-python-DIVISIBILITY_BY_12_FOR_A_LARGE_NUMBER
java-python
static boolean isDvisibleBy12 ( String num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) su...
def isDvisibleBy12 ( num ) : if ( len ( num ) >= 3 ) : d1 = int ( num [ len ( num ) - 1 ] ) if ( d1 % 2 != 0 ) : return False d2 = int ( num [ len ( num ) - 2 ] ) sum = 0 for i in range ( 0 , len ( num ) ) : sum += int ( num [ i ] ) return ( su...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static boolean isDvisibleBy12 ( String num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ...
java-python-MAXIMUM_POSSIBLE_DIFFERENCE_TWO_SUBSETS_ARRAY
java-python
static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] ...
def maxDiff ( arr , n ) : SubsetSum_1 = 0 SubsetSum_2 = 0 for i in range ( 0 , n ) : isSingleOccurance = True for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : isSingleOccurance = False arr [ i ] = arr [ j ] = 0 break ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr...
java-python-LEXICOGRAPHICALLY_SMALLEST_STRING_OBTAINED_CONCATENATING_ARRAY
java-python
static String lexsmallest ( String a [ ] , int n ) { sort ( a , n ) ; String answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer += a [ i ] ; return answer ; }
def lexSmallest ( a , n ) : a.sort ( reverse = True ) answer = "" for i in range ( n ) : answer += a [ i ] return answer
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static String lexsmallest ( String a [ ] , int n ) { sort ( a , n ) ; String answer = "" ; for ( int i = 0 ; i < n ; i ++ ) answer += a [ i ] ; return answer ; } </Code>
java-python-MAXIMUM_WEIGHT_PATH_ENDING_ELEMENT_LAST_ROW_MATRIX
java-python
public static int maxCost ( int mat [ ] [ ] , int N ) { int dp [ ] [ ] = new int [ N ] [ N ] ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; j ++ ) dp...
def maxCost ( mat , N ) : dp = [ [ 0 for i in range ( N ) ] for j in range ( N ) ] dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] for i in range ( 1 , N ) : dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] for i in range ( 1 , N ) : for j in range ( 1 , min ( i + 1 , N ) ) : dp [ i ] ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int maxCost ( int mat [ ] [ ] , int N ) { int dp [ ] [ ] = new int [ N ] [ N ] ; dp [ 0 ] [ 0 ] = mat [ 0 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) dp [ i ] [ 0 ] = mat [ i ] [ 0 ] + dp [ i - 1 ] [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) for ( int j = 1 ; j < i + 1 && j < N ; j ...
java-python-WAYS_REMOVE_ONE_ELEMENT_BINARY_STRING_XOR_BECOMES_ZERO
java-python
static int xorZero ( String s ) { int one_count = 0 , zero_count = 0 ; char [ ] str = s . toCharArray ( ) ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == ' 1 ' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; }
def xorZero ( str ) : one_count = 0 zero_count = 0 n = len ( str ) for i in range ( 0 , n , 1 ) : if ( str [ i ] == '1' ) : one_count += 1 else : zero_count += 1 if ( one_count % 2 == 0 ) : return zero_count return one_count
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static int xorZero ( String s ) { int one_count = 0 , zero_count = 0 ; char [ ] str = s . toCharArray ( ) ; int n = str . length ; for ( int i = 0 ; i < n ; i ++ ) if ( str [ i ] == ' 1 ' ) one_count ++ ; else zero_count ++ ; if ( one_count % 2 == 0 ) return zero_count ; return one_count ; } <...
java-python-N_TH_ROOT_NUMBER
java-python
static double nthRoot ( int A , int N ) { double xPre = Math . random ( ) % 10 ; double eps = 0.001 ; double delX = 2147483647 ; double xK = 0.0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre...
def nthRoot ( A , N ) : xPre = random.randint ( 1 , 101 ) % 10 eps = 0.001 delX = 2147483647 xK = 0.0 while ( delX > eps ) : xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N delX = abs ( xK - xPre ) xPre = xK return xK
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static double nthRoot ( int A , int N ) { double xPre = Math . random ( ) % 10 ; double eps = 0.001 ; double delX = 2147483647 ; double xK = 0.0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; ...
java-python-RETURN_PREVIOUS_ELEMENT_IN_AN_EXPANDING_MATRIX
java-python
static StringBuilder findLeft ( StringBuilder str ) { int n = str . length ( ) ; while ( n > 0 ) { n -- ; if ( str . charAt ( n ) == ' d ' ) { str . setCharAt ( n , ' c ' ) ; break ; } if ( str . charAt ( n ) == ' b ' ) { str . setCharAt ( n , ' a ' ) ; break ; } if (...
def findLeft ( str ) : n = len ( str ) - 1 while ( n > 0 ) : if ( str [ n ] == 'd' ) : str = str [ 0 : n ] + 'c' + str [ n + 1 : ] break if ( str [ n ] == 'b' ) : str = str [ 0 : n ] + 'a' + str [ n + 1 : ] break if ( str [ n ] == 'a' ) : ...
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> static StringBuilder findLeft ( StringBuilder str ) { int n = str . length ( ) ; while ( n > 0 ) { n -- ; if ( str . charAt ( n ) == ' d ' ) { str . setCharAt ( n , ' c ' ) ; break ; } if ( str . charAt ( n ) == ' b ' ) { str . setCharAt ( n , ' a ' ) ; break ; } ...
java-python-MINIMUM_NUMBER_SUBSETS_DISTINCT_ELEMENTS
java-python
public static int subset ( int ar [ ] , int n ) { int res = 0 ; Arrays . sort ( ar ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = Math . max ( res , count ) ; } return r...
def subset ( ar , n ) : res = 0 ar.sort ( ) for i in range ( 0 , n ) : count = 1 for i in range ( n - 1 ) : if ar [ i ] == ar [ i + 1 ] : count += 1 else : break res = max ( res , count ) return res
Translate the given code from java to python. The input Code is marked with <Code> and </Code>.
<Code> public static int subset ( int ar [ ] , int n ) { int res = 0 ; Arrays . sort ( ar ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; for ( ; i < n - 1 ; i ++ ) { if ( ar [ i ] == ar [ i + 1 ] ) count ++ ; else break ; } res = Math . max ( res , count ) ; } r...