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-cpp-SEARCH_AN_ELEMENT_IN_A_SORTED_AND_PIVOTED_ARRAY | python-cpp | def search ( arr , l , h , key ) :
if l > h :
return - 1
mid = ( l + h ) // 2
if arr [ mid ] == key :
return mid
if arr [ l ] <= arr [ mid ] :
if key >= arr [ l ] and key <= arr [ mid ] :
return search ( arr , l , mid - 1 , key )
return search ( arr , mid + 1 ... | int search ( int arr [ ] , int l , int h , int key ) {
if ( l > h ) return - 1 ;
int mid = ( l + h ) / 2 ;
if ( arr [ mid ] == key ) return mid ;
if ( arr [ l ] <= arr [ mid ] ) {
if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ;
return search ( arr , mid + 1 , h ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , l , h , key ) :
if l > h :
return - 1
mid = ( l + h ) // 2
if arr [ mid ] == key :
return mid
if arr [ l ] <= arr [ mid ] :
if key >= arr [ l ] and key <= arr [ mid ] :
return search ( arr , l , mid - 1 , key )
return search ( arr , m... |
python-cpp-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION | python-cpp | def exponentiation ( bas , exp ) :
if ( exp == 0 ) :
return 1
if ( exp == 1 ) :
return bas % N
t = exponentiation ( bas , int ( exp / 2 ) )
t = ( t * t ) % N
if ( exp % 2 == 0 ) :
return t
else :
return ( ( bas % N ) * t ) % N
| long int exponentiation ( long int base , long int exp ) {
if ( exp == 0 ) return 1 ;
if ( exp == 1 ) return base % N ;
long int t = exponentiation ( base , exp / 2 ) ;
t = ( t * t ) % N ;
if ( exp % 2 == 0 ) return t ;
else return ( ( base % N ) * t ) % N ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def exponentiation ( bas , exp ) :
if ( exp == 0 ) :
return 1
if ( exp == 1 ) :
return bas % N
t = exponentiation ( bas , int ( exp / 2 ) )
t = ( t * t ) % N
if ( exp % 2 == 0 ) :
return t
else :
return ( ( bas % N ) * t ) % N
</Code> |
python-cpp-DIVISIBILITY_CHECK | python-cpp | def divisibilityCheck ( arr , n ) :
s = dict ( )
max_ele = - 10 ** 9
for i in range ( n ) :
s [ arr [ i ] ] = 1
max_ele = max ( max_ele , arr [ i ] )
res = dict ( )
for i in range ( n ) :
if ( arr [ i ] != 0 ) :
for j in range ( arr [ i ] * 2 , max_ele + 1 , arr [... | void divisibilityCheck ( int arr [ ] , int n ) {
unordered_set < int > s ;
int max_ele = INT_MIN ;
for ( int i = 0 ;
i < n ;
i ++ ) {
s . insert ( arr [ i ] ) ;
max_ele = max ( max_ele , arr [ i ] ) ;
}
unordered_set < int > res ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] != 0 ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def divisibilityCheck ( arr , n ) :
s = dict ( )
max_ele = - 10 ** 9
for i in range ( n ) :
s [ arr [ i ] ] = 1
max_ele = max ( max_ele , arr [ i ] )
res = dict ( )
for i in range ( n ) :
if ( arr [ i ] != 0 ) :
for j in range ( arr [ i ] * 2 , max_ele + 1 ... |
python-cpp-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_3 | python-cpp | def printRepeating ( arr , size ) :
print ( " The repeating elements are" , end = " " )
for i in range ( 0 , size ) :
if ( arr [ abs ( arr [ i ] ) ] > 0 ) :
arr [ abs ( arr [ i ] ) ] = ( - 1 ) * arr [ abs ( arr [ i ] ) ]
else :
print ( abs ( arr [ i ] ) , end = " " )
| void printRepeating ( int arr [ ] , int size ) {
int i ;
cout << "The repeating elements are" ;
for ( i = 0 ;
i < size ;
i ++ ) {
if ( arr [ abs ( arr [ i ] ) ] > 0 ) arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] ;
else cout << " " << abs ( arr [ i ] ) << " " ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printRepeating ( arr , size ) :
print ( " The repeating elements are" , end = " " )
for i in range ( 0 , size ) :
if ( arr [ abs ( arr [ i ] ) ] > 0 ) :
arr [ abs ( arr [ i ] ) ] = ( - 1 ) * arr [ abs ( arr [ i ] ) ]
else :
print ( abs ( arr [ i ] ) , end = " "... |
python-cpp-DYNAMIC_PROGRAMMING_SET_17_PALINDROME_PARTITIONING | python-cpp | def minPalPartion ( str ) :
n = len ( str )
C = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
P = [ [ False for i in range ( n ) ] for i in range ( n ) ]
j = 0
k = 0
L = 0
for i in range ( n ) :
P [ i ] [ i ] = True
C [ i ] [ i ] = 0
for L in range ( 2 , n + 1 ) :
... | int minPalPartion ( char * str ) {
int n = strlen ( str ) ;
int C [ n ] [ n ] ;
bool P [ n ] [ n ] ;
int i , j , k , L ;
for ( i = 0 ;
i < n ;
i ++ ) {
P [ i ] [ i ] = true ;
C [ i ] [ i ] = 0 ;
}
for ( L = 2 ;
L <= n ;
L ++ ) {
for ( i = 0 ;
i < n - L + 1 ;
i ++ ) {
j = ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minPalPartion ( str ) :
n = len ( str )
C = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
P = [ [ False for i in range ( n ) ] for i in range ( n ) ]
j = 0
k = 0
L = 0
for i in range ( n ) :
P [ i ] [ i ] = True
C [ i ] [ i ] = 0
for L in range ( 2 , n + ... |
python-cpp-HEIGHT_COMPLETE_BINARY_TREE_HEAP_N_NODES | python-cpp | def height ( N ) :
return math.ceil ( math.log2 ( N + 1 ) ) - 1
| int height ( int N ) {
return ceil ( log2 ( N + 1 ) ) - 1 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def height ( N ) :
return math.ceil ( math.log2 ( N + 1 ) ) - 1
</Code> |
python-cpp-DISTRIBUTING_ITEMS_PERSON_CANNOT_TAKE_TWO_ITEMS_TYPE_1 | python-cpp | def checkCount ( arr , n , k ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
for key , values in mp.items ( ) :
if values > 2 * k :
return False
return True
| bool checkCount ( int arr [ ] , int n , int k ) {
unordered_map < int , int > hash ;
for ( int i = 0 ;
i < n ;
i ++ ) hash [ arr [ i ] ] ++ ;
for ( auto x : hash ) if ( x . second > 2 * k ) return false ;
return true ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def checkCount ( arr , n , k ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
for key , values in mp.items ( ) :
if values > 2 * k :
return False
return True
</Code> |
python-cpp-REMOVE_CONSECUTIVE_DUPLICATES_STRING | python-cpp | def removeDuplicates ( S ) :
n = len ( S )
if ( n < 2 ) :
return
j = 0
for i in range ( n ) :
if ( S [ j ] != S [ i ] ) :
j += 1
S [ j ] = S [ i ]
j += 1
S = S [ : j ]
return S
| void removeDuplicates ( char S [ ] ) {
int n = strlen ( S ) ;
if ( n < 2 ) return ;
int j = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
if ( S [ j ] != S [ i ] ) {
j ++ ;
S [ j ] = S [ i ] ;
}
}
j ++ ;
S [ j ] = '\0' ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def removeDuplicates ( S ) :
n = len ( S )
if ( n < 2 ) :
return
j = 0
for i in range ( n ) :
if ( S [ j ] != S [ i ] ) :
j += 1
S [ j ] = S [ i ]
j += 1
S = S [ : j ]
return S
</Code> |
python-cpp-THIRD_LARGEST_ELEMENT_ARRAY_DISTINCT_ELEMENTS_1 | python-cpp | def thirdLargest ( arr , arr_size ) :
if ( arr_size < 3 ) :
print ( " Invalid Input " )
return
first = arr [ 0 ]
second = - sys.maxsize
third = - sys.maxsize
for i in range ( 1 , arr_size ) :
if ( arr [ i ] > first ) :
third = second
second = first
... | void thirdLargest ( int arr [ ] , int arr_size ) {
if ( arr_size < 3 ) {
printf ( " Invalid Input " ) ;
return ;
}
int first = arr [ 0 ] , second = INT_MIN , third = INT_MIN ;
for ( int i = 1 ;
i < arr_size ;
i ++ ) {
if ( arr [ i ] > first ) {
third = second ;
second = first ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def thirdLargest ( arr , arr_size ) :
if ( arr_size < 3 ) :
print ( " Invalid Input " )
return
first = arr [ 0 ]
second = - sys.maxsize
third = - sys.maxsize
for i in range ( 1 , arr_size ) :
if ( arr [ i ] > first ) :
third = second
second = fi... |
python-cpp-LONGEST_INCREASING_ODD_EVEN_SUBSEQUENCE | python-cpp | def longOddEvenIncSeq ( arr , n ) :
lioes = list ( )
maxLen = 0
for i in range ( n ) :
lioes.append ( 1 )
i = 1
for i in range ( n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and ( arr [ i ] + arr [ j ] ) % 2 != 0 and lioes [ i ] < lioes [ j ] + 1 ) :
... | int longOddEvenIncSeq ( int arr [ ] , int n ) {
int lioes [ n ] ;
int maxLen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) lioes [ i ] = 1 ;
for ( int i = 1 ;
i < n ;
i ++ ) for ( int j = 0 ;
j < i ;
j ++ ) if ( arr [ i ] > arr [ j ] && ( arr [ i ] + arr [ j ] ) % 2 != 0 && lioes [ i ] < lioes [ j ] + 1 ) ... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def longOddEvenIncSeq ( arr , n ) :
lioes = list ( )
maxLen = 0
for i in range ( n ) :
lioes.append ( 1 )
i = 1
for i in range ( n ) :
for j in range ( i ) :
if ( arr [ i ] > arr [ j ] and ( arr [ i ] + arr [ j ] ) % 2 != 0 and lioes [ i ] < lioes [ j ] + 1 ) :
... |
python-cpp-ADD_1_TO_A_GIVEN_NUMBER_1 | python-cpp | def addOne ( x ) :
return ( - ( ~ x ) )
| int addOne ( int x ) {
return ( - ( ~ x ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def addOne ( x ) :
return ( - ( ~ x ) )
</Code> |
python-cpp-NUMBER_DECIMAL_NUMBERS_LENGTH_K_STRICT_MONOTONE | python-cpp | def getNumStrictMonotone ( ln ) :
DP = [ [ 0 ] * DP_s for _ in range ( ln ) ]
for i in range ( DP_s ) :
DP [ 0 ] [ i ] = i + 1
for i in range ( 1 , ln ) :
for j in range ( 1 , DP_s ) :
DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ]
return DP [ ln - 1 ] [ DP_s - ... | int getNumStrictMonotone ( int len ) {
int DP [ len ] [ DP_s ] ;
memset ( DP , 0 , sizeof ( DP ) ) ;
for ( int i = 0 ;
i < DP_s ;
++ i ) DP [ 0 ] [ i ] = i + 1 ;
for ( int i = 1 ;
i < len ;
++ i ) for ( int j = 1 ;
j < DP_s ;
++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ] ;
re... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getNumStrictMonotone ( ln ) :
DP = [ [ 0 ] * DP_s for _ in range ( ln ) ]
for i in range ( DP_s ) :
DP [ 0 ] [ i ] = i + 1
for i in range ( 1 , ln ) :
for j in range ( 1 , DP_s ) :
DP [ i ] [ j ] = DP [ i - 1 ] [ j - 1 ] + DP [ i ] [ j - 1 ]
return DP [ ln - 1 ] [ ... |
python-cpp-SEARCH_ALMOST_SORTED_ARRAY | python-cpp | def binarySearch ( arr , l , r , x ) :
if ( r >= l ) :
mid = int ( l + ( r - l ) / 2 )
if ( arr [ mid ] == x ) : return mid
if ( mid > l and arr [ mid - 1 ] == x ) :
return ( mid - 1 )
if ( mid < r and arr [ mid + 1 ] == x ) :
return ( mid + 1 )
if ( a... | int binarySearch ( int arr [ ] , int l , int r , int x ) {
if ( r >= l ) {
int mid = l + ( r - l ) / 2 ;
if ( arr [ mid ] == x ) return mid ;
if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ;
if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ;
if ( arr [ mid ] > x ) return binary... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def binarySearch ( arr , l , r , x ) :
if ( r >= l ) :
mid = int ( l + ( r - l ) / 2 )
if ( arr [ mid ] == x ) : return mid
if ( mid > l and arr [ mid - 1 ] == x ) :
return ( mid - 1 )
if ( mid < r and arr [ mid + 1 ] == x ) :
return ( mid + 1 )
... |
python-cpp-BRESENHAMS_LINE_GENERATION_ALGORITHM | python-cpp | def bresenham ( x1 , y1 , x2 , y2 ) :
m_new = 2 * ( y2 - y1 )
slope_error_new = m_new - ( x2 - x1 )
y = y1
for x in range ( x1 , x2 + 1 ) :
print ( "(" , x , "," , y , ")\n" )
slope_error_new = slope_error_new + m_new
if ( slope_error_new >= 0 ) :
y = y + 1
... | void bresenham ( int x1 , int y1 , int x2 , int y2 ) {
int m_new = 2 * ( y2 - y1 ) ;
int slope_error_new = m_new - ( x2 - x1 ) ;
for ( int x = x1 , y = y1 ;
x <= x2 ;
x ++ ) {
cout << "(" << x << "," << y << ")\n" ;
slope_error_new += m_new ;
if ( slope_error_new >= 0 ) {
y ++ ;
slope_... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def bresenham ( x1 , y1 , x2 , y2 ) :
m_new = 2 * ( y2 - y1 )
slope_error_new = m_new - ( x2 - x1 )
y = y1
for x in range ( x1 , x2 + 1 ) :
print ( "(" , x , "," , y , ")\n" )
slope_error_new = slope_error_new + m_new
if ( slope_error_new >= 0 ) :
y = y + 1
... |
python-cpp-HIGHWAY_BILLBOARD_PROBLEM | python-cpp | def maxRevenue ( m , x , revenue , n , t ) :
maxRev = [ 0 ] * ( m + 1 )
nxtbb = 0
for i in range ( 1 , m + 1 ) :
if ( nxtbb < n ) :
if ( x [ nxtbb ] != i ) :
maxRev [ i ] = maxRev [ i - 1 ]
else :
if ( i <= t ) :
maxRev [ i ... | int maxRevenue ( int m , int x [ ] , int revenue [ ] , int n , int t ) {
int maxRev [ m + 1 ] ;
memset ( maxRev , 0 , sizeof ( maxRev ) ) ;
int nxtbb = 0 ;
for ( int i = 1 ;
i <= m ;
i ++ ) {
if ( nxtbb < n ) {
if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i - 1 ] ;
else {
if ( i <... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def maxRevenue ( m , x , revenue , n , t ) :
maxRev = [ 0 ] * ( m + 1 )
nxtbb = 0
for i in range ( 1 , m + 1 ) :
if ( nxtbb < n ) :
if ( x [ nxtbb ] != i ) :
maxRev [ i ] = maxRev [ i - 1 ]
else :
if ( i <= t ) :
maxR... |
python-cpp-PROGRAM_FIND_LINE_PASSING_2_POINTS | python-cpp | def lineFromPoints ( P , Q ) :
a = Q [ 1 ] - P [ 1 ]
b = P [ 0 ] - Q [ 0 ]
c = a * ( P [ 0 ] ) + b * ( P [ 1 ] )
if ( b < 0 ) :
print ( "The line passing through points P and Q is:" , a , "x " , b , "y = " , c , "\n" )
else :
print ( "The line passing through points P and Q is: " , a... | pair < double , double > void lineFromPoints ( pdd P , pdd Q ) {
double a = Q . second - P . second ;
double b = P . first - Q . first ;
double c = a * ( P . first ) + b * ( P . second ) ;
if ( b < 0 ) {
cout << "The line passing through points P and Q is: " << a << "x " << b << "y = " << c << endl ;
}
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def lineFromPoints ( P , Q ) :
a = Q [ 1 ] - P [ 1 ]
b = P [ 0 ] - Q [ 0 ]
c = a * ( P [ 0 ] ) + b * ( P [ 1 ] )
if ( b < 0 ) :
print ( "The line passing through points P and Q is:" , a , "x " , b , "y = " , c , "\n" )
else :
print ( "The line passing through points P and Q is... |
python-cpp-CHINESE_REMAINDER_THEOREM_SET_1_INTRODUCTION | python-cpp | def findMinX ( num , rem , k ) :
x = 1
while ( True ) :
j = 0
while ( j < k ) :
if ( x % num [ j ] != rem [ j ] ) :
break
j += 1
if ( j == k ) :
return x
x += 1
| int findMinX ( int num [ ] , int rem [ ] , int k ) {
int x = 1 ;
while ( true ) {
int j ;
for ( j = 0 ;
j < k ;
j ++ ) if ( x % num [ j ] != rem [ j ] ) break ;
if ( j == k ) return x ;
x ++ ;
}
return x ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMinX ( num , rem , k ) :
x = 1
while ( True ) :
j = 0
while ( j < k ) :
if ( x % num [ j ] != rem [ j ] ) :
break
j += 1
if ( j == k ) :
return x
x += 1
</Code> |
python-cpp-FIND_A_TRIPLET_THAT_SUM_TO_A_GIVEN_VALUE | python-cpp | def find3Numbers ( A , arr_size , sum ) :
for i in range ( 0 , arr_size - 2 ) :
for j in range ( i + 1 , arr_size - 1 ) :
for k in range ( j + 1 , arr_size ) :
if A [ i ] + A [ j ] + A [ k ] == sum :
print ( "Triplet is" , A [ i ] , ", " , A [ j ] , ", " , A [... | bool find3Numbers ( int A [ ] , int arr_size , int sum ) {
int l , r ;
for ( int i = 0 ;
i < arr_size - 2 ;
i ++ ) {
for ( int j = i + 1 ;
j < arr_size - 1 ;
j ++ ) {
for ( int k = j + 1 ;
k < arr_size ;
k ++ ) {
if ( A [ i ] + A [ j ] + A [ k ] == sum ) {
cout <<... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def find3Numbers ( A , arr_size , sum ) :
for i in range ( 0 , arr_size - 2 ) :
for j in range ( i + 1 , arr_size - 1 ) :
for k in range ( j + 1 , arr_size ) :
if A [ i ] + A [ j ] + A [ k ] == sum :
print ( "Triplet is" , A [ i ] , ", " , A [ j ] , ", ... |
python-cpp-K_MAXIMUM_SUM_COMBINATIONS_TWO_ARRAYS | python-cpp | def KMaxCombinations ( A , B , N , K ) :
pq = PriorityQueue ( )
for i in range ( 0 , N ) :
for j in range ( 0 , N ) :
a = A [ i ] + B [ j ]
pq.put ( ( - a , a ) )
count = 0
while ( count < K ) :
print ( pq.get ( ) [ 1 ] )
count = count + 1
| void KMaxCombinations ( int A [ ] , int B [ ] , int N , int K ) {
priority_queue < int > pq ;
for ( int i = 0 ;
i < N ;
i ++ ) for ( int j = 0 ;
j < N ;
j ++ ) pq . push ( A [ i ] + B [ j ] ) ;
int count = 0 ;
while ( count < K ) {
cout << pq . top ( ) << endl ;
pq . pop ( ) ;
count ++ ;
}... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def KMaxCombinations ( A , B , N , K ) :
pq = PriorityQueue ( )
for i in range ( 0 , N ) :
for j in range ( 0 , N ) :
a = A [ i ] + B [ j ]
pq.put ( ( - a , a ) )
count = 0
while ( count < K ) :
print ( pq.get ( ) [ 1 ] )
count = count + 1
</Code> |
python-cpp-TRIANGULAR_MATCHSTICK_NUMBER | python-cpp | def numberOfSticks ( x ) :
return ( 3 * x * ( x + 1 ) ) / 2
| int numberOfSticks ( int x ) {
return ( 3 * x * ( x + 1 ) ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfSticks ( x ) :
return ( 3 * x * ( x + 1 ) ) / 2
</Code> |
python-cpp-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS_1 | python-cpp | def countSubstringWithEqualEnds ( s ) :
result = 0
n = len ( s )
count = [ 0 ] * MAX_CHAR
for i in range ( n ) :
count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( MAX_CHAR ) :
result += ( count [ i ] * ( count [ i ] + 1 ) / 2 )
return result
| int countSubstringWithEqualEnds ( string s ) {
int result = 0 ;
int n = s . length ( ) ;
int count [ MAX_CHAR ] = {
0 }
;
for ( int i = 0 ;
i < n ;
i ++ ) count [ s [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
i < MAX_CHAR ;
i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def countSubstringWithEqualEnds ( s ) :
result = 0
n = len ( s )
count = [ 0 ] * MAX_CHAR
for i in range ( n ) :
count [ ord ( s [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( MAX_CHAR ) :
result += ( count [ i ] * ( count [ i ] + 1 ) / 2 )
return result
</Code> |
python-cpp-DIVISIBILITY_BY_7 | python-cpp | def isDivisibleBy7 ( num ) :
if num < 0 :
return isDivisibleBy7 ( - num )
if ( num == 0 or num == 7 ) :
return True
if ( num < 10 ) :
return False
return isDivisibleBy7 ( num // 10 - 2 * ( num - num // 10 * 10 ) )
| int isDivisibleBy7 ( int num ) {
if ( num < 0 ) return isDivisibleBy7 ( - num ) ;
if ( num == 0 || num == 7 ) return 1 ;
if ( num < 10 ) return 0 ;
return isDivisibleBy7 ( num / 10 - 2 * ( num - num / 10 * 10 ) ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isDivisibleBy7 ( num ) :
if num < 0 :
return isDivisibleBy7 ( - num )
if ( num == 0 or num == 7 ) :
return True
if ( num < 10 ) :
return False
return isDivisibleBy7 ( num // 10 - 2 * ( num - num // 10 * 10 ) )
</Code> |
python-cpp-FIND_ONE_MULTIPLE_REPEATING_ELEMENTS_READ_ARRAY | python-cpp | def findRepeatingNumber ( arr , n ) :
sq = sqrt ( n )
range__ = int ( ( n / sq ) + 1 )
count = [ 0 for i in range ( range__ ) ]
for i in range ( 0 , n + 1 , 1 ) :
count [ int ( ( arr [ i ] - 1 ) / sq ) ] += 1
selected_block = range__ - 1
for i in range ( 0 , range__ - 1 , 1 ) :
i... | int findRepeatingNumber ( const int arr [ ] , int n ) {
int sq = sqrt ( n ) ;
int range = ( n / sq ) + 1 ;
int count [ range ] = {
0 }
;
for ( int i = 0 ;
i <= n ;
i ++ ) {
count [ ( arr [ i ] - 1 ) / sq ] ++ ;
}
int selected_block = range - 1 ;
for ( int i = 0 ;
i < rang... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findRepeatingNumber ( arr , n ) :
sq = sqrt ( n )
range__ = int ( ( n / sq ) + 1 )
count = [ 0 for i in range ( range__ ) ]
for i in range ( 0 , n + 1 , 1 ) :
count [ int ( ( arr [ i ] - 1 ) / sq ) ] += 1
selected_block = range__ - 1
for i in range ( 0 , range__ - 1 , 1 ) :
... |
python-cpp-FIND_N_TH_ELEMENT_FROM_STERNS_DIATOMIC_SERIES | python-cpp | def findSDSFunc ( n ) :
DP = [ 0 ] * ( n + 1 )
DP [ 0 ] = 0
DP [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
if ( int ( i % 2 ) == 0 ) :
DP [ i ] = DP [ int ( i / 2 ) ]
else :
DP [ i ] = ( DP [ int ( ( i - 1 ) / 2 ) ] + DP [ int ( ( i + 1 ) / 2 ) ] )
return DP [ n ... | int findSDSFunc ( int n ) {
int DP [ n + 1 ] ;
DP [ 0 ] = 0 ;
DP [ 1 ] = 1 ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
if ( i % 2 == 0 ) DP [ i ] = DP [ i / 2 ] ;
else DP [ i ] = DP [ ( i - 1 ) / 2 ] + DP [ ( i + 1 ) / 2 ] ;
}
return DP [ n ] ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSDSFunc ( n ) :
DP = [ 0 ] * ( n + 1 )
DP [ 0 ] = 0
DP [ 1 ] = 1
for i in range ( 2 , n + 1 ) :
if ( int ( i % 2 ) == 0 ) :
DP [ i ] = DP [ int ( i / 2 ) ]
else :
DP [ i ] = ( DP [ int ( ( i - 1 ) / 2 ) ] + DP [ int ( ( i + 1 ) / 2 ) ] )
return ... |
python-cpp-RECURSIVE_C_PROGRAM_LINEARLY_SEARCH_ELEMENT_GIVEN_ARRAY | python-cpp | def recSearch ( arr , l , r , x ) :
if r < l :
return - 1
if arr [ l ] == x :
return l
if arr [ r ] == x :
return r
return recSearch ( arr , l + 1 , r - 1 , x )
| int recSearch ( int arr [ ] , int l , int r , int x ) {
if ( r < l ) return - 1 ;
if ( arr [ l ] == x ) return l ;
if ( arr [ r ] == x ) return r ;
return recSearch ( arr , l + 1 , r - 1 , x ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def recSearch ( arr , l , r , x ) :
if r < l :
return - 1
if arr [ l ] == x :
return l
if arr [ r ] == x :
return r
return recSearch ( arr , l + 1 , r - 1 , x )
</Code> |
python-cpp-CHECK_LARGE_NUMBER_DIVISIBLE_4_NOT | python-cpp | def check ( st ) :
n = len ( st )
if ( n == 0 ) :
return False
if ( n == 1 ) :
return ( ( st [ 0 ] - '0' ) % 4 == 0 )
last = ( int ) ( st [ n - 1 ] )
second_last = ( int ) ( st [ n - 2 ] )
return ( ( second_last * 10 + last ) % 4 == 0 )
| bool check ( string str ) {
int n = str . length ( ) ;
if ( n == 0 ) return false ;
if ( n == 1 ) return ( ( str [ 0 ] - '0' ) % 4 == 0 ) ;
int last = str [ n - 1 ] - '0' ;
int second_last = str [ n - 2 ] - '0' ;
return ( ( second_last * 10 + last ) % 4 == 0 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def check ( st ) :
n = len ( st )
if ( n == 0 ) :
return False
if ( n == 1 ) :
return ( ( st [ 0 ] - '0' ) % 4 == 0 )
last = ( int ) ( st [ n - 1 ] )
second_last = ( int ) ( st [ n - 2 ] )
return ( ( second_last * 10 + last ) % 4 == 0 )
</Code> |
python-cpp-LOWER_CASE_UPPER_CASE_INTERESTING_FACT | python-cpp | def to_upper ( string ) :
for i in range ( len ( string ) ) :
if ( 'a' <= string [ i ] <= 'z' ) :
string = ( string [ 0 : i ] + chr ( ord ( string [ i ] ) - ord ( 'a' ) + ord ( 'A' ) ) + string [ i + 1 : ] )
return string
| string to_upper ( string & in ) {
for ( int i = 0 ;
i < in . length ( ) ;
i ++ ) if ( 'a' <= in [ i ] <= 'z' ) in [ i ] = in [ i ] - 'a' + 'A' ;
return in ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def to_upper ( string ) :
for i in range ( len ( string ) ) :
if ( 'a' <= string [ i ] <= 'z' ) :
string = ( string [ 0 : i ] + chr ( ord ( string [ i ] ) - ord ( 'a' ) + ord ( 'A' ) ) + string [ i + 1 : ] )
return string
</Code> |
python-cpp-REMOVE_MINIMUM_NUMBER_CHARACTERS_TWO_STRINGS_BECOME_ANAGRAM | python-cpp | def remAnagram ( str1 , str2 ) :
count1 = [ 0 ] * CHARS
count2 = [ 0 ] * CHARS
i = 0
while i < len ( str1 ) :
count1 [ ord ( str1 [ i ] ) - ord ( 'a' ) ] += 1
i += 1
i = 0
while i < len ( str2 ) :
count2 [ ord ( str2 [ i ] ) - ord ( 'a' ) ] += 1
i += 1
result ... | int remAnagram ( string str1 , string str2 ) {
int count1 [ CHARS ] = {
0 }
, count2 [ CHARS ] = {
0 }
;
for ( int i = 0 ;
str1 [ i ] != '\0' ;
i ++ ) count1 [ str1 [ i ] - 'a' ] ++ ;
for ( int i = 0 ;
str2 [ i ] != '\0' ;
i ++ ) count2 [ str2 [ i ] - 'a' ] ++ ;... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def remAnagram ( str1 , str2 ) :
count1 = [ 0 ] * CHARS
count2 = [ 0 ] * CHARS
i = 0
while i < len ( str1 ) :
count1 [ ord ( str1 [ i ] ) - ord ( 'a' ) ] += 1
i += 1
i = 0
while i < len ( str2 ) :
count2 [ ord ( str2 [ i ] ) - ord ( 'a' ) ] += 1
i += 1
... |
python-cpp-SORT_ARRAY_WAVE_FORM_2_1 | python-cpp | def sortInWave ( arr , n ) :
for i in range ( 0 , n , 2 ) :
if ( i > 0 and arr [ i ] < arr [ i - 1 ] ) :
arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ]
if ( i < n - 1 and arr [ i ] < arr [ i + 1 ] ) :
arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ]
| void sortInWave ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i += 2 ) {
if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( & arr [ i ] , & arr [ i - 1 ] ) ;
if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( & arr [ i ] , & arr [ i + 1 ] ) ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def sortInWave ( arr , n ) :
for i in range ( 0 , n , 2 ) :
if ( i > 0 and arr [ i ] < arr [ i - 1 ] ) :
arr [ i ] , arr [ i - 1 ] = arr [ i - 1 ] , arr [ i ]
if ( i < n - 1 and arr [ i ] < arr [ i + 1 ] ) :
arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ]
</Code... |
python-cpp-FORM_MINIMUM_NUMBER_FROM_GIVEN_SEQUENCE_1 | python-cpp | def getMinNumberForPattern ( seq ) :
n = len ( seq )
if ( n >= 9 ) :
return "-1"
result = [ None ] * ( n + 1 )
count = 1
for i in range ( n + 1 ) :
if ( i == n or seq [ i ] == 'I' ) :
for j in range ( i - 1 , - 2 , - 1 ) :
result [ j + 1 ] = int ( '0' + st... | string getMinNumberForPattern ( string seq ) {
int n = seq . length ( ) ;
if ( n >= 9 ) return "-1" ;
string result ( n + 1 , ' ' ) ;
int count = 1 ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
if ( i == n || seq [ i ] == 'I' ) {
for ( int j = i - 1 ;
j >= - 1 ;
j -- ) {
result [ j +... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getMinNumberForPattern ( seq ) :
n = len ( seq )
if ( n >= 9 ) :
return "-1"
result = [ None ] * ( n + 1 )
count = 1
for i in range ( n + 1 ) :
if ( i == n or seq [ i ] == 'I' ) :
for j in range ( i - 1 , - 2 , - 1 ) :
result [ j + 1 ] = int ( '... |
python-cpp-CHECK_IF_STRING_REMAINS_PALINDROME_AFTER_REMOVING_GIVEN_NUMBER_OF_CHARACTERS | python-cpp | def isPossible ( str , n ) :
l = len ( str )
if ( l >= n ) :
return True
return False
| bool isPossible ( string str , int n ) {
int len = str . length ( ) ;
if ( len >= n ) return true ;
return false ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def isPossible ( str , n ) :
l = len ( str )
if ( l >= n ) :
return True
return False
</Code> |
python-cpp-FIND_CENTER_CIRCLE_USING_ENDPOINTS_DIAMETER | python-cpp | def center ( x1 , x2 , y1 , y2 ) :
print ( int ( ( x1 + x2 ) / 2 ) , end = "" )
print ( "," , int ( ( y1 + y2 ) / 2 ) )
| void center ( int x1 , int x2 , int y1 , int y2 ) {
cout << ( float ) ( x1 + x2 ) / 2 << ", " << ( float ) ( y1 + y2 ) / 2 ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def center ( x1 , x2 , y1 , y2 ) :
print ( int ( ( x1 + x2 ) / 2 ) , end = "" )
print ( "," , int ( ( y1 + y2 ) / 2 ) )
</Code> |
python-cpp-GREEDY_ALGORITHM_TO_FIND_MINIMUM_NUMBER_OF_COINS | python-cpp | def findMin ( V ) :
deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ]
n = len ( deno )
ans = [ ]
i = n - 1
while ( i >= 0 ) :
while ( V >= deno [ i ] ) :
V -= deno [ i ]
ans.append ( deno [ i ] )
i -= 1
for i in range ( len ( ans ) ) :
print (... | void findMin ( int V ) {
vector < int > ans ;
for ( int i = n - 1 ;
i >= 0 ;
i -- ) {
while ( V >= deno [ i ] ) {
V -= deno [ i ] ;
ans . push_back ( deno [ i ] ) ;
}
}
for ( int i = 0 ;
i < ans . size ( ) ;
i ++ ) cout << ans [ i ] << " " ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findMin ( V ) :
deno = [ 1 , 2 , 5 , 10 , 20 , 50 , 100 , 500 , 1000 ]
n = len ( deno )
ans = [ ]
i = n - 1
while ( i >= 0 ) :
while ( V >= deno [ i ] ) :
V -= deno [ i ]
ans.append ( deno [ i ] )
i -= 1
for i in range ( len ( ans ) ) :
... |
python-cpp-HOW_TO_AVOID_OVERFLOW_IN_MODULAR_MULTIPLICATION | python-cpp | def mulmod ( a , b , mod ) :
res = 0
a = a % mod
while ( b > 0 ) :
if ( b % 2 == 1 ) :
res = ( res + a ) % mod
a = ( a * 2 ) % mod
b //= 2
return res % mod
| ll mulmod ( ll a , ll b , ll mod ) {
ll res = 0 ;
a = a % mod ;
while ( b > 0 ) {
if ( b % 2 == 1 ) res = ( res + a ) % mod ;
a = ( a * 2 ) % mod ;
b /= 2 ;
}
return res % mod ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def mulmod ( a , b , mod ) :
res = 0
a = a % mod
while ( b > 0 ) :
if ( b % 2 == 1 ) :
res = ( res + a ) % mod
a = ( a * 2 ) % mod
b //= 2
return res % mod
</Code> |
python-cpp-FIND_NTH_TERM_DRAGON_CURVE_SEQUENCE | python-cpp | def Dragon_Curve_Sequence ( n ) :
s = "1"
for i in range ( 2 , n + 1 ) :
temp = "1"
prev = '1'
zero = '0'
one = '1'
for j in range ( len ( s ) ) :
temp += s [ j ]
if ( prev == '0' ) :
temp += one
prev = one
... | string Dragon_Curve_Sequence ( int n ) {
string s = "1" ;
for ( int i = 2 ;
i <= n ;
i ++ ) {
string temp = "1" ;
char prev = '1' , zero = '0' , one = '1' ;
for ( int j = 0 ;
j < s . length ( ) ;
j ++ ) {
temp += s [ j ] ;
if ( prev == '0' ) {
temp += one ;
prev =... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def Dragon_Curve_Sequence ( n ) :
s = "1"
for i in range ( 2 , n + 1 ) :
temp = "1"
prev = '1'
zero = '0'
one = '1'
for j in range ( len ( s ) ) :
temp += s [ j ]
if ( prev == '0' ) :
temp += one
prev = one
... |
python-cpp-MINIMUM_POSITIVE_POINTS_TO_REACH_DESTINATION | python-cpp | def minInitialPoints ( points ) :
dp = [ [ 0 for x in range ( C + 1 ) ] for y in range ( R + 1 ) ]
m , n = R , C
if points [ m - 1 ] [ n - 1 ] > 0 :
dp [ m - 1 ] [ n - 1 ] = 1
else :
dp [ m - 1 ] [ n - 1 ] = abs ( points [ m - 1 ] [ n - 1 ] ) + 1
for i in range ( m - 2 , - 1 , - 1 ) ... | int minInitialPoints ( int points [ ] [ C ] ) {
int dp [ R ] [ C ] ;
int m = R , n = C ;
dp [ m - 1 ] [ n - 1 ] = points [ m - 1 ] [ n - 1 ] > 0 ? 1 : abs ( points [ m - 1 ] [ n - 1 ] ) + 1 ;
for ( int i = m - 2 ;
i >= 0 ;
i -- ) dp [ i ] [ n - 1 ] = max ( dp [ i + 1 ] [ n - 1 ] - points [ i ] [ n - 1 ] , 1... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def minInitialPoints ( points ) :
dp = [ [ 0 for x in range ( C + 1 ) ] for y in range ( R + 1 ) ]
m , n = R , C
if points [ m - 1 ] [ n - 1 ] > 0 :
dp [ m - 1 ] [ n - 1 ] = 1
else :
dp [ m - 1 ] [ n - 1 ] = abs ( points [ m - 1 ] [ n - 1 ] ) + 1
for i in range ( m - 2 , - 1 ,... |
python-cpp-FIND_REPETITIVE_ELEMENT_1_N_1 | python-cpp | def findRepeating ( arr , n ) :
return sum ( arr ) - ( ( ( n - 1 ) * n ) // 2 )
| int findRepeating ( int arr [ ] , int n ) {
return accumulate ( arr , arr + n , 0 ) - ( ( n - 1 ) * n / 2 ) ;
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findRepeating ( arr , n ) :
return sum ( arr ) - ( ( ( n - 1 ) * n ) // 2 )
</Code> |
python-cpp-C_PROGRAM_FIND_SECOND_FREQUENT_CHARACTER | python-cpp | def getSecondMostFreq ( str ) :
NO_OF_CHARS = 256
count = [ 0 ] * NO_OF_CHARS
for i in range ( len ( str ) ) :
count [ ord ( str [ i ] ) ] += 1
first , second = 0 , 0
for i in range ( NO_OF_CHARS ) :
if count [ i ] > count [ first ] :
second = first
first = i
... | char getSecondMostFreq ( string str ) {
int count [ NO_OF_CHARS ] = {
0 }
, i ;
for ( i = 0 ;
str [ i ] ;
i ++ ) ( count [ str [ i ] ] ) ++ ;
int first = 0 , second = 0 ;
for ( i = 0 ;
i < NO_OF_CHARS ;
i ++ ) {
if ( count [ i ] > count [ first ] ) {
second = first ;
... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def getSecondMostFreq ( str ) :
NO_OF_CHARS = 256
count = [ 0 ] * NO_OF_CHARS
for i in range ( len ( str ) ) :
count [ ord ( str [ i ] ) ] += 1
first , second = 0 , 0
for i in range ( NO_OF_CHARS ) :
if count [ i ] > count [ first ] :
second = first
fir... |
python-cpp-SUM_TWO_LARGE_NUMBERS | python-cpp | def findSum ( str1 , str2 ) :
if ( len ( str1 ) > len ( str2 ) ) :
t = str1
str1 = str2
str2 = t
str = ""
n1 = len ( str1 )
n2 = len ( str2 )
str1 = str1 [ : : - 1 ]
str2 = str2 [ : : - 1 ]
carry = 0
for i in range ( n1 ) :
sum = ( ( ord ( str1 [ i ] ) - 4... | string findSum ( string str1 , string str2 ) {
if ( str1 . length ( ) > str2 . length ( ) ) swap ( str1 , str2 ) ;
string str = "" ;
int n1 = str1 . length ( ) , n2 = str2 . length ( ) ;
reverse ( str1 . begin ( ) , str1 . end ( ) ) ;
reverse ( str2 . begin ( ) , str2 . end ( ) ) ;
int carry = 0 ;
for ( i... | Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( str1 , str2 ) :
if ( len ( str1 ) > len ( str2 ) ) :
t = str1
str1 = str2
str2 = t
str = ""
n1 = len ( str1 )
n2 = len ( str2 )
str1 = str1 [ : : - 1 ]
str2 = str2 [ : : - 1 ]
carry = 0
for i in range ( n1 ) :
sum = ( ( ord ( str1 [ i ... |
python-cpp-NEXT_GREATER_ELEMENT | python-cpp | def printNGE ( arr ) :
for i in range ( 0 , len ( arr ) , 1 ) :
next = - 1
for j in range ( i + 1 , len ( arr ) , 1 ) :
if arr [ i ] < arr [ j ] :
next = arr [ j ]
break
print ( str ( arr [ i ] ) + " -- " + str ( next ) )
| void printNGE ( int arr [ ] , int n ) {
int next , i , j ;
for ( i = 0 ;
i < n ;
i ++ ) {
next = - 1 ;
for ( j = i + 1 ;
j < n ;
j ++ ) {
if ( arr [ i ] < arr [ j ] ) {
next = arr [ j ] ;
break ;
}
}
cout << arr [ i ] << " -- " << next << endl ;
}
}
| Translate the given code from python to cpp. The input Code is marked with <Code> and </Code>. | <Code>
def printNGE ( arr ) :
for i in range ( 0 , len ( arr ) , 1 ) :
next = - 1
for j in range ( i + 1 , len ( arr ) , 1 ) :
if arr [ i ] < arr [ j ] :
next = arr [ j ]
break
print ( str ( arr [ i ] ) + " -- " + str ( next ) )
</Code> |
python-java-PAIRS_OF_POSITIVE_NEGATIVE_VALUES_IN_AN_ARRAY | python-java | def printPairs ( arr , n ) :
v = [ ]
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) :
v.append ( abs ( arr [ i ] ) )
if ( len ( v ) == 0 ) :
return ;
v.sort ( )
for i in range ( len ( v ) ) :
pri... | public static void printPairs ( int arr [ ] , int n ) {
Vector < Integer > v = new Vector < Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ;
if ( v . size ( ) == 0 ) retu... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def printPairs ( arr , n ) :
v = [ ]
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) :
v.append ( abs ( arr [ i ] ) )
if ( len ( v ) == 0 ) :
return ;
v.sort ( )
for i in range ( len ( v ) ) :
... |
python-java-C_PROGRAM_FIND_AREA_TRIANGLE | python-java | def findArea ( a , b , c ) :
if ( a < 0 or b < 0 or c < 0 or ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) ) :
print ( 'Not a valid trianglen' )
return
s = ( a + b + c ) / 2
area = ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ** 0.5
print ( 'Area of a traingle is %f' % area )
| static float findArea ( float a , float b , float c ) {
if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) {
System . out . println ( "Not a valid triangle" ) ;
System . exit ( 0 ) ;
}
float s = ( a + b + c ) / 2 ;
return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findArea ( a , b , c ) :
if ( a < 0 or b < 0 or c < 0 or ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) ) :
print ( 'Not a valid trianglen' )
return
s = ( a + b + c ) / 2
area = ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ** 0.5
print ( 'Area of a traingle is %f' % area )
<... |
python-java-CHECK_ARRAY_MAJORITY_ELEMENT | python-java | def isMajority ( a ) :
mp = { }
for i in a :
if i in mp : mp [ i ] += 1
else : mp [ i ] = 1
for x in mp :
if mp [ x ] >= len ( a ) // 2 :
return True
return False
| static boolean isMajority ( int a [ ] , int n ) {
HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( mp . containsKey ( a [ i ] ) ) mp . put ( a [ i ] , mp . get ( a [ i ] ) + 1 ) ;
else mp . put ( a [ i ] , 1 ) ;
for ( Map . Entry < Integer , In... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isMajority ( a ) :
mp = { }
for i in a :
if i in mp : mp [ i ] += 1
else : mp [ i ] = 1
for x in mp :
if mp [ x ] >= len ( a ) // 2 :
return True
return False
</Code> |
python-java-NUMBER_N_DIGITS_NON_DECREASING_INTEGERS | python-java | def nonDecNums ( n ) :
a = np.zeros ( ( n + 1 , 10 ) )
for i in range ( 10 ) :
a [ 0 ] [ i ] = 1
for i in range ( 1 , n + 1 ) :
a [ i ] [ 9 ] = 1
for i in range ( 1 , n + 1 ) :
for j in range ( 8 , - 1 , - 1 ) :
a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ]
... | static int nonDecNums ( int n ) {
int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ;
for ( int i = 0 ;
i <= 9 ;
i ++ ) a [ 0 ] [ i ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) a [ i ] [ 9 ] = 1 ;
for ( int i = 1 ;
i <= n ;
i ++ ) for ( int j = 8 ;
j >= 0 ;
j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ]... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def nonDecNums ( n ) :
a = np.zeros ( ( n + 1 , 10 ) )
for i in range ( 10 ) :
a [ 0 ] [ i ] = 1
for i in range ( 1 , n + 1 ) :
a [ i ] [ 9 ] = 1
for i in range ( 1 , n + 1 ) :
for j in range ( 8 , - 1 , - 1 ) :
a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j +... |
python-java-PROGRAM_FIRST_FIT_ALGORITHM_MEMORY_MANAGEMENT | python-java | def firstFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
allocation [ i ] = j
blockSize [ j ] -= processSize [ i ]
break
print ( " Pr... | static void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) {
int allocation [ ] = new int [ n ] ;
for ( int i = 0 ;
i < allocation . length ;
i ++ ) allocation [ i ] = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
j < m ;
j ++ ) {
if ( blockSize [ j ] >... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def firstFit ( blockSize , m , processSize , n ) :
allocation = [ - 1 ] * n
for i in range ( n ) :
for j in range ( m ) :
if blockSize [ j ] >= processSize [ i ] :
allocation [ i ] = j
blockSize [ j ] -= processSize [ i ]
break
print... |
python-java-NON_REPEATING_ELEMENT_1 | python-java | def firstNonRepeating ( arr , n ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
for i in range ( n ) :
if mp [ arr [ i ] ] == 1 :
return arr [ i ]
return - 1
| static int firstNonRepeating ( int arr [ ] , int n ) {
Map < Integer , Integer > m = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( m . containsKey ( arr [ i ] ) ) {
m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ;
}
else {
m . put ( arr [ i ] , 1 ) ;
}
}
for ( ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def firstNonRepeating ( arr , n ) :
mp = defaultdict ( lambda : 0 )
for i in range ( n ) :
mp [ arr [ i ] ] += 1
for i in range ( n ) :
if mp [ arr [ i ] ] == 1 :
return arr [ i ]
return - 1
</Code> |
python-java-DISTRIBUTING_ALL_BALLS_WITHOUT_REPETITION | python-java | def distributingBalls ( k , n , string ) :
a = [ 0 ] * MAX_CHAR
for i in range ( n ) :
a [ ord ( string [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( MAX_CHAR ) :
if ( a [ i ] > k ) :
return False
return True
| static boolean distributingBalls ( long k , long n , String str ) {
int [ ] a = new int [ MAX_CHAR ] ;
for ( int i = 0 ;
i < n ;
i ++ ) {
a [ str . charAt ( i ) - ' a ' ] ++ ;
}
for ( int i = 0 ;
i < MAX_CHAR ;
i ++ ) if ( a [ i ] > k ) return false ;
return true ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def distributingBalls ( k , n , string ) :
a = [ 0 ] * MAX_CHAR
for i in range ( n ) :
a [ ord ( string [ i ] ) - ord ( 'a' ) ] += 1
for i in range ( MAX_CHAR ) :
if ( a [ i ] > k ) :
return False
return True
</Code> |
python-java-BREAK_NUMBER_THREE_PARTS_1 | python-java | def count_of_ways ( n ) :
count = 0
count = ( n + 1 ) * ( n + 2 ) // 2
return count
| static long count_of_ways ( long n ) {
long count = 0 ;
count = ( n + 1 ) * ( n + 2 ) / 2 ;
return count ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def count_of_ways ( n ) :
count = 0
count = ( n + 1 ) * ( n + 2 ) // 2
return count
</Code> |
python-java-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY_1 | python-java | def maxSum ( arr , n ) :
cum_sum = 0
for i in range ( 0 , n ) :
cum_sum += arr [ i ]
curr_val = 0
for i in range ( 0 , n ) :
curr_val += i * arr [ i ]
res = curr_val
for i in range ( 1 , n ) :
next_val = ( curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 )... | static int maxSum ( int arr [ ] , int n ) {
int cum_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) cum_sum += arr [ i ] ;
int curr_val = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) curr_val += i * arr [ i ] ;
int res = curr_val ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int next_val = curr_val - ( cum_sum - ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxSum ( arr , n ) :
cum_sum = 0
for i in range ( 0 , n ) :
cum_sum += arr [ i ]
curr_val = 0
for i in range ( 0 , n ) :
curr_val += i * arr [ i ]
res = curr_val
for i in range ( 1 , n ) :
next_val = ( curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( ... |
python-java-CALCULATE_ANGLE_HOUR_HAND_MINUTE_HAND | python-java | def calcAngle ( h , m ) :
if ( h < 0 or m < 0 or h > 12 or m > 60 ) :
print ( 'Wrong input' )
if ( h == 12 ) :
h = 0
if ( m == 60 ) :
m = 0
hour_angle = 0.5 * ( h * 60 + m )
minute_angle = 6 * m
angle = abs ( hour_angle - minute_angle )
angle = min ( 360 - angle , ang... | static int calcAngle ( double h , double m ) {
if ( h < 0 || m < 0 || h > 12 || m > 60 ) System . out . println ( "Wrong input" ) ;
if ( h == 12 ) h = 0 ;
if ( m == 60 ) m = 0 ;
int hour_angle = ( int ) ( 0.5 * ( h * 60 + m ) ) ;
int minute_angle = ( int ) ( 6 * m ) ;
int angle = Math . abs ( hour_angle - m... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def calcAngle ( h , m ) :
if ( h < 0 or m < 0 or h > 12 or m > 60 ) :
print ( 'Wrong input' )
if ( h == 12 ) :
h = 0
if ( m == 60 ) :
m = 0
hour_angle = 0.5 * ( h * 60 + m )
minute_angle = 6 * m
angle = abs ( hour_angle - minute_angle )
angle = min ( 360 - angl... |
python-java-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS_1 | python-java | def smallest ( x , y , z ) :
if ( not ( y / x ) ) :
return y if ( not ( y / z ) ) else z
return x if ( not ( x / z ) ) else z
| static int smallest ( int x , int y , int z ) {
if ( ( y / x ) != 1 ) return ( ( y / z ) != 1 ) ? y : z ;
return ( ( x / z ) != 1 ) ? x : z ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def smallest ( x , y , z ) :
if ( not ( y / x ) ) :
return y if ( not ( y / z ) ) else z
return x if ( not ( x / z ) ) else z
</Code> |
python-java-FIND_ONE_EXTRA_CHARACTER_STRING_1 | python-java | def findExtraCharcter ( strA , strB ) :
res = 0
for i in range ( 0 , len ( strA ) ) :
res = res ^ ( ord ) ( strA [ i ] )
for i in range ( 0 , len ( strB ) ) :
res = res ^ ( ord ) ( strB [ i ] )
return ( ( chr ) ( res ) ) ;
| static char findExtraCharcter ( String strA , String strB ) {
int res = 0 , i ;
for ( i = 0 ;
i < strA . length ( ) ;
i ++ ) {
res ^= strA . charAt ( i ) ;
}
for ( i = 0 ;
i < strB . length ( ) ;
i ++ ) {
res ^= strB . charAt ( i ) ;
}
return ( ( char ) ( res ) ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findExtraCharcter ( strA , strB ) :
res = 0
for i in range ( 0 , len ( strA ) ) :
res = res ^ ( ord ) ( strA [ i ] )
for i in range ( 0 , len ( strB ) ) :
res = res ^ ( ord ) ( strB [ i ] )
return ( ( chr ) ( res ) ) ;
</Code> |
python-java-RECAMANS_SEQUENCE_1 | python-java | def recaman ( n ) :
if ( n <= 0 ) :
return
print ( 0 , "," , end = '' )
s = set ( [ ] )
s.add ( 0 )
prev = 0
for i in range ( 1 , n ) :
curr = prev - i
if ( curr < 0 or curr in s ) :
curr = prev + i
s.add ( curr )
print ( curr , "," , end = '' ... | static void recaman ( int n ) {
if ( n <= 0 ) return ;
System . out . printf ( "%d, " , 0 ) ;
HashSet < Integer > s = new HashSet < Integer > ( ) ;
s . add ( 0 ) ;
int prev = 0 ;
for ( int i = 1 ;
i < n ;
i ++ ) {
int curr = prev - i ;
if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def recaman ( n ) :
if ( n <= 0 ) :
return
print ( 0 , "," , end = '' )
s = set ( [ ] )
s.add ( 0 )
prev = 0
for i in range ( 1 , n ) :
curr = prev - i
if ( curr < 0 or curr in s ) :
curr = prev + i
s.add ( curr )
print ( curr , "," , en... |
python-java-PROGRAM_FIND_SMALLEST_DIFFERENCE_ANGLES_TWO_PARTS_GIVEN_CIRCLE | python-java | def findMinimumAngle ( arr , n ) :
l = 0
_sum = 0
ans = 360
for i in range ( n ) :
_sum += arr [ i ]
while _sum >= 180 :
ans = min ( ans , 2 * abs ( 180 - _sum ) )
_sum -= arr [ l ]
l += 1
ans = min ( ans , 2 * abs ( 180 - _sum ) )
return a... | public static int findMinimumAngle ( int arr [ ] , int n ) {
int l = 0 , sum = 0 , ans = 360 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
sum += arr [ i ] ;
while ( sum >= 180 ) {
ans = Math . min ( ans , 2 * Math . abs ( 180 - sum ) ) ;
sum -= arr [ l ] ;
l ++ ;
}
ans = Math . min ( a... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findMinimumAngle ( arr , n ) :
l = 0
_sum = 0
ans = 360
for i in range ( n ) :
_sum += arr [ i ]
while _sum >= 180 :
ans = min ( ans , 2 * abs ( 180 - _sum ) )
_sum -= arr [ l ]
l += 1
ans = min ( ans , 2 * abs ( 180 - _sum ) )
r... |
python-java-COUNT_GFG_SUBSEQUENCES_GIVEN_STRING | python-java | def countSubsequence ( s , n ) :
cntG = 0
cntF = 0
result = 0
C = 0
for i in range ( n ) :
if ( s [ i ] == 'G' ) :
cntG += 1
result += C
continue
if ( s [ i ] == 'F' ) :
cntF += 1
C += cntG
continue
else ... | static void countSubsequence ( String s , int n ) {
int cntG = 0 , cntF = 0 , result = 0 , C = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
switch ( s . charAt ( i ) ) {
case ' G ' : cntG ++ ;
result += C ;
break ;
case ' F ' : cntF ++ ;
C += cntG ;
break ;
default : cont... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countSubsequence ( s , n ) :
cntG = 0
cntF = 0
result = 0
C = 0
for i in range ( n ) :
if ( s [ i ] == 'G' ) :
cntG += 1
result += C
continue
if ( s [ i ] == 'F' ) :
cntF += 1
C += cntG
continue
... |
python-java-MAXIMUM_DIFFERENCE_BETWEEN_FREQUENCY_OF_TWO_ELEMENTS_SUCH_THAT_ELEMENT_HAVING_GREATER_FREQUENCY_IS_ALSO_GREATER | python-java | def maxdiff ( arr , n ) :
freq = defaultdict ( lambda : 0 )
for i in range ( n ) :
freq [ arr [ i ] ] += 1
ans = 0
for i in range ( n ) :
for j in range ( n ) :
if freq [ arr [ i ] ] > freq [ arr [ j ] ] and arr [ i ] > arr [ j ] :
ans = max ( ans , freq [ arr... | static int maxdiff ( int arr [ ] , int n ) {
Map < Integer , Integer > freq = new HashMap < > ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) freq . put ( arr [ i ] , freq . get ( arr [ i ] ) == null ? 1 : freq . get ( arr [ i ] ) + 1 ) ;
int ans = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = 0 ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxdiff ( arr , n ) :
freq = defaultdict ( lambda : 0 )
for i in range ( n ) :
freq [ arr [ i ] ] += 1
ans = 0
for i in range ( n ) :
for j in range ( n ) :
if freq [ arr [ i ] ] > freq [ arr [ j ] ] and arr [ i ] > arr [ j ] :
ans = max ( ans , fre... |
python-java-CHECK_IF_TWO_ARRAYS_ARE_EQUAL_OR_NOT | python-java | def areEqual ( arr1 , arr2 , n , m ) :
if ( n != m ) :
return False ;
arr1.sort ( ) ;
arr2.sort ( ) ;
for i in range ( 0 , n - 1 ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return False ;
return True ;
| public static boolean areEqual ( int arr1 [ ] , int arr2 [ ] ) {
int n = arr1 . length ;
int m = arr2 . length ;
if ( n != m ) return false ;
Arrays . sort ( arr1 ) ;
Arrays . sort ( arr2 ) ;
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ;
return true ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def areEqual ( arr1 , arr2 , n , m ) :
if ( n != m ) :
return False ;
arr1.sort ( ) ;
arr2.sort ( ) ;
for i in range ( 0 , n - 1 ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return False ;
return True ;
</Code> |
python-java-FRIENDS_PAIRING_PROBLEM_2 | python-java | def countFriendsPairings ( n ) :
a , b , c = 1 , 2 , 0
if ( n <= 2 ) :
return n
for i in range ( 3 , n + 1 ) :
c = b + ( i - 1 ) * a
a = b
b = c
return c
| static int countFriendsPairings ( int n ) {
int a = 1 , b = 2 , c = 0 ;
if ( n <= 2 ) {
return n ;
}
for ( int i = 3 ;
i <= n ;
i ++ ) {
c = b + ( i - 1 ) * a ;
a = b ;
b = c ;
}
return c ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countFriendsPairings ( n ) :
a , b , c = 1 , 2 , 0
if ( n <= 2 ) :
return n
for i in range ( 3 , n + 1 ) :
c = b + ( i - 1 ) * a
a = b
b = c
return c
</Code> |
python-java-FINDING_POWER_PRIME_NUMBER_P_N | python-java | def PowerOFPINnfactorial ( n , p ) :
ans = 0
temp = p
while ( temp <= n ) :
ans += n / temp
temp = temp * p
return ans
| static int PowerOFPINnfactorial ( int n , int p ) {
int ans = 0 ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
int count = 0 , temp = i ;
while ( temp % p == 0 ) {
count ++ ;
temp = temp / p ;
}
ans += count ;
}
return ans ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def PowerOFPINnfactorial ( n , p ) :
ans = 0
temp = p
while ( temp <= n ) :
ans += n / temp
temp = temp * p
return ans
</Code> |
python-java-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF | python-java | def findLength ( str ) :
n = len ( str )
maxlen = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n , 2 ) :
length = j - i + 1
leftsum = 0
rightsum = 0
for k in range ( 0 , int ( length / 2 ) ) :
leftsum += ( int ( str [ i + k ] )... | static int findLength ( String str ) {
int n = str . length ( ) ;
int maxlen = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i + 1 ;
j < n ;
j += 2 ) {
int length = j - i + 1 ;
int leftsum = 0 , rightsum = 0 ;
for ( int k = 0 ;
k < length / 2 ;
k ++ ) {
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findLength ( str ) :
n = len ( str )
maxlen = 0
for i in range ( 0 , n ) :
for j in range ( i + 1 , n , 2 ) :
length = j - i + 1
leftsum = 0
rightsum = 0
for k in range ( 0 , int ( length / 2 ) ) :
leftsum += ( int ( str [ i ... |
python-java-ANALYSIS_OF_ALGORITHMS_SET_2_ASYMPTOTIC_ANALYSIS | python-java | def search ( arr , n , x ) :
i = 0
for i in range ( i , n ) :
if ( arr [ i ] == x ) :
return i
return - 1
| static int search ( int arr [ ] , int n , int x ) {
int i ;
for ( i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) {
return i ;
}
}
return - 1 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , n , x ) :
i = 0
for i in range ( i , n ) :
if ( arr [ i ] == x ) :
return i
return - 1
</Code> |
python-java-SUBARRAYSUBSTRING_VS_SUBSEQUENCE_AND_PROGRAMS_TO_GENERATE_THEM | python-java | def subArray ( arr , n ) :
for i in range ( 0 , n ) :
for j in range ( i , n ) :
for k in range ( i , j + 1 ) :
print ( arr [ k ] , end = "" )
print ( "\n" , end = "" )
| static void subArray ( int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
for ( int j = i ;
j < n ;
j ++ ) {
for ( int k = i ;
k <= j ;
k ++ ) System . out . print ( arr [ k ] + " " ) ;
}
}
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def subArray ( arr , n ) :
for i in range ( 0 , n ) :
for j in range ( i , n ) :
for k in range ( i , j + 1 ) :
print ( arr [ k ] , end = "" )
print ( "\n" , end = "" )
</Code> |
python-java-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8_1 | python-java | def isSubSeqDivisible ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n + 1 ) ]
arr = [ 0 for i in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
arr [ i ] = int ( str [ i - 1 ] )
for i in range ( 1 , n + 1 ) :
dp [ i ] [ arr [ i ] % 8 ] = 1
fo... | static boolean isSubSeqDivisible ( String str ) {
int n = str . length ( ) ;
int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ;
int arr [ ] = new int [ n + 1 ] ;
for ( int i = 1 ;
i <= n ;
i ++ ) arr [ i ] = ( int ) ( str . charAt ( i - 1 ) - ' 0 ' ) ;
for ( int i = 1 ;
i <= n ;
i ++ ) {
dp [ i ] [ arr [... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isSubSeqDivisible ( str ) :
n = len ( str )
dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n + 1 ) ]
arr = [ 0 for i in range ( n + 1 ) ]
for i in range ( 1 , n + 1 ) :
arr [ i ] = int ( str [ i - 1 ] )
for i in range ( 1 , n + 1 ) :
dp [ i ] [ arr [ i ] % 8 ] = 1
... |
python-java-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM | python-java | def solve ( i , par , a , n , k , current_ans ) :
if ( par > k ) :
return 0
global ans
if ( par == k and i == n - 1 ) :
ans = min ( ans , current_ans )
return 0
for j in range ( i + 1 , n ) :
solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] ) * ( a ... | static void solve ( int i , int par , int a [ ] , int n , int k , int current_ans ) {
if ( par > k ) return ;
if ( par == k && i == n - 1 ) {
ans = Math . min ( ans , current_ans ) ;
return ;
}
for ( int j = i + 1 ;
j < n ;
j ++ ) solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def solve ( i , par , a , n , k , current_ans ) :
if ( par > k ) :
return 0
global ans
if ( par == k and i == n - 1 ) :
ans = min ( ans , current_ans )
return 0
for j in range ( i + 1 , n ) :
solve ( j , par + 1 , a , n , k , current_ans + ( a [ j ] - a [ i + 1 ] )... |
python-java-SQUARE_ROOT_OF_A_PERFECT_SQUARE | python-java | def squareRoot ( n ) :
x = n
y = 1
e = 0.000001
while ( x - y > e ) :
x = ( x + y ) / 2
y = n / x
return x
| static float squareRoot ( float n ) {
float x = n ;
float y = 1 ;
double e = 0.000001 ;
while ( x - y > e ) {
x = ( x + y ) / 2 ;
y = n / x ;
}
return x ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def squareRoot ( n ) :
x = n
y = 1
e = 0.000001
while ( x - y > e ) :
x = ( x + y ) / 2
y = n / x
return x
</Code> |
python-java-COUNT_NUMBER_PAIRS_N_B_N_GCD_B_B | python-java | def CountPairs ( n ) :
k = n
imin = 1
ans = 0
while ( imin <= n ) :
imax = n / k
ans += k * ( imax - imin + 1 )
imin = imax + 1
k = n / imin
return ans
| static int CountPairs ( int n ) {
int k = n ;
int imin = 1 ;
int ans = 0 ;
while ( imin <= n ) {
int imax = n / k ;
ans += k * ( imax - imin + 1 ) ;
imin = imax + 1 ;
k = n / imin ;
}
return ans ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def CountPairs ( n ) :
k = n
imin = 1
ans = 0
while ( imin <= n ) :
imax = n / k
ans += k * ( imax - imin + 1 )
imin = imax + 1
k = n / imin
return ans
</Code> |
python-java-MULTIPLY_AN_INTEGER_WITH_3_5 | python-java | def multiplyWith3Point5 ( x ) :
return ( x << 1 ) + x + ( x >> 1 )
| static int multiplyWith3Point5 ( int x ) {
return ( x << 1 ) + x + ( x >> 1 ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def multiplyWith3Point5 ( x ) :
return ( x << 1 ) + x + ( x >> 1 )
</Code> |
python-java-LEXICOGRAPHICALLY_PREVIOUS_PERMUTATION_IN_C | python-java | def prevPermutation ( str ) :
n = len ( str ) - 1
i = n
while ( i > 0 and str [ i - 1 ] <= str [ i ] ) :
i -= 1
if ( i <= 0 ) :
return False
j = i - 1
while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) :
j += 1
str = list ( str )
temp = str [ i - 1 ]
str ... | static boolean prevPermutation ( char [ ] str ) {
int n = str . length - 1 ;
int i = n ;
while ( i > 0 && str [ i - 1 ] <= str [ i ] ) {
i -- ;
}
if ( i <= 0 ) {
return false ;
}
int j = i - 1 ;
while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) {
j ++ ;
}
swap ( str , i - 1 , j ) ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def prevPermutation ( str ) :
n = len ( str ) - 1
i = n
while ( i > 0 and str [ i - 1 ] <= str [ i ] ) :
i -= 1
if ( i <= 0 ) :
return False
j = i - 1
while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) :
j += 1
str = list ( str )
temp = str [ i - 1 ]
... |
python-java-AREA_OF_A_POLYGON_WITH_GIVEN_N_ORDERED_VERTICES | python-java | def polygonArea ( X , Y , n ) :
area = 0.0
j = n - 1
for i in range ( 0 , n ) :
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] )
j = i
return int ( abs ( area / 2.0 ) )
| public static double polygonArea ( double X [ ] , double Y [ ] , int n ) {
double area = 0.0 ;
int j = n - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] ) ;
j = i ;
}
return Math . abs ( area / 2.0 ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def polygonArea ( X , Y , n ) :
area = 0.0
j = n - 1
for i in range ( 0 , n ) :
area += ( X [ j ] + X [ i ] ) * ( Y [ j ] - Y [ i ] )
j = i
return int ( abs ( area / 2.0 ) )
</Code> |
python-java-EQUILIBRIUM_INDEX_OF_AN_ARRAY | python-java | def equilibrium ( arr ) :
leftsum = 0
rightsum = 0
n = len ( arr )
for i in range ( n ) :
leftsum = 0
rightsum = 0
for j in range ( i ) :
leftsum += arr [ j ]
for j in range ( i + 1 , n ) :
rightsum += arr [ j ]
if leftsum == rightsum :
... | int equilibrium ( int arr [ ] , int n ) {
int i , j ;
int leftsum , rightsum ;
for ( i = 0 ;
i < n ;
++ i ) {
leftsum = 0 ;
for ( j = 0 ;
j < i ;
j ++ ) leftsum += arr [ j ] ;
rightsum = 0 ;
for ( j = i + 1 ;
j < n ;
j ++ ) rightsum += arr [ j ] ;
if ( leftsum == rightsum )... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def equilibrium ( arr ) :
leftsum = 0
rightsum = 0
n = len ( arr )
for i in range ( n ) :
leftsum = 0
rightsum = 0
for j in range ( i ) :
leftsum += arr [ j ]
for j in range ( i + 1 , n ) :
rightsum += arr [ j ]
if leftsum == rightsu... |
python-java-COUNT_WAYS_DIVIDE_CIRCLE_USING_N_NON_INTERSECTING_CHORDS | python-java | def chordCnt ( A ) :
n = 2 * A
dpArray = [ 0 ] * ( n + 1 )
dpArray [ 0 ] = 1
dpArray [ 2 ] = 1
for i in range ( 4 , n + 1 , 2 ) :
for j in range ( 0 , i - 1 , 2 ) :
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] )
return int ( dpArray [ n ] )
| static int chordCnt ( int A ) {
int n = 2 * A ;
int [ ] dpArray = new int [ n + 1 ] ;
dpArray [ 0 ] = 1 ;
dpArray [ 2 ] = 1 ;
for ( int i = 4 ;
i <= n ;
i += 2 ) {
for ( int j = 0 ;
j < i - 1 ;
j += 2 ) {
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] ) ;
}
}
return dpA... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def chordCnt ( A ) :
n = 2 * A
dpArray = [ 0 ] * ( n + 1 )
dpArray [ 0 ] = 1
dpArray [ 2 ] = 1
for i in range ( 4 , n + 1 , 2 ) :
for j in range ( 0 , i - 1 , 2 ) :
dpArray [ i ] += ( dpArray [ j ] * dpArray [ i - 2 - j ] )
return int ( dpArray [ n ] )
</Code> |
python-java-FIND_HARMONIC_MEAN_USING_ARITHMETIC_MEAN_GEOMETRIC_MEAN | python-java | def compute ( a , b ) :
AM = ( a + b ) / 2
GM = math.sqrt ( a * b )
HM = ( GM * GM ) / AM
return HM
| static double compute ( int a , int b ) {
double AM , GM , HM ;
AM = ( a + b ) / 2 ;
GM = Math . sqrt ( a * b ) ;
HM = ( GM * GM ) / AM ;
return HM ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def compute ( a , b ) :
AM = ( a + b ) / 2
GM = math.sqrt ( a * b )
HM = ( GM * GM ) / AM
return HM
</Code> |
python-java-MAJORITY_ELEMENT | python-java | def findMajority ( arr , n ) :
maxCount = 0 ;
index = - 1
for i in range ( n ) :
count = 0
for j in range ( n ) :
if ( arr [ i ] == arr [ j ] ) :
count += 1
if ( count > maxCount ) :
maxCount = count
index = i
if ( maxCount > n ... | static void findMajority ( int arr [ ] , int n ) {
int maxCount = 0 ;
int index = - 1 ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int count = 0 ;
for ( int j = 0 ;
j < n ;
j ++ ) {
if ( arr [ i ] == arr [ j ] ) count ++ ;
}
if ( count > maxCount ) {
maxCount = count ;
index ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findMajority ( arr , n ) :
maxCount = 0 ;
index = - 1
for i in range ( n ) :
count = 0
for j in range ( n ) :
if ( arr [ i ] == arr [ j ] ) :
count += 1
if ( count > maxCount ) :
maxCount = count
index = i
if ( maxCou... |
python-java-CHECK_NUMBER_IS_PERFECT_SQUARE_USING_ADDITIONSUBTRACTION | python-java | def isPerfectSquare ( n ) :
i = 1
the_sum = 0
while the_sum < n :
the_sum += i
if the_sum == n :
return True
i += 2
return False
| static boolean isPerfectSquare ( int n ) {
for ( int sum = 0 , i = 1 ;
sum < n ;
i += 2 ) {
sum += i ;
if ( sum == n ) return true ;
}
return false ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isPerfectSquare ( n ) :
i = 1
the_sum = 0
while the_sum < n :
the_sum += i
if the_sum == n :
return True
i += 2
return False
</Code> |
python-java-FIND_THE_SMALLEST_WINDOW_IN_A_STRING_CONTAINING_ALL_CHARACTERS_OF_ANOTHER_STRING | python-java | def findSubString ( string , pat ) :
len1 = len ( string )
len2 = len ( pat )
if len1 < len2 :
print ( "No such window exists" )
return ""
hash_pat = [ 0 ] * no_of_chars
hash_str = [ 0 ] * no_of_chars
for i in range ( 0 , len2 ) :
hash_pat [ ord ( pat [ i ] ) ] += 1
s... | static String findSubString ( String str , String pat ) {
int len1 = str . length ( ) ;
int len2 = pat . length ( ) ;
if ( len1 < len2 ) {
System . out . println ( "No such window exists" ) ;
return "" ;
}
int hash_pat [ ] = new int [ no_of_chars ] ;
int hash_str [ ] = new int [ no_of_chars ] ;
fo... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSubString ( string , pat ) :
len1 = len ( string )
len2 = len ( pat )
if len1 < len2 :
print ( "No such window exists" )
return ""
hash_pat = [ 0 ] * no_of_chars
hash_str = [ 0 ] * no_of_chars
for i in range ( 0 , len2 ) :
hash_pat [ ord ( pat [ i ] ) ] += ... |
python-java-NUMBER_SUBSEQUENCES_STRING_DIVISIBLE_N | python-java | def countDivisibleSubseq ( str , n ) :
l = len ( str )
dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ]
dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1
for i in range ( 1 , l ) :
dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1
for j in range ( n ) :
... | static int countDivisibleSubseq ( String str , int n ) {
int len = str . length ( ) ;
int dp [ ] [ ] = new int [ len ] [ n ] ;
dp [ 0 ] [ ( str . charAt ( 0 ) - ' 0 ' ) % n ] ++ ;
for ( int i = 1 ;
i < len ;
i ++ ) {
dp [ i ] [ ( str . charAt ( i ) - ' 0 ' ) % n ] ++ ;
for ( int j = 0 ;
j < n ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def countDivisibleSubseq ( str , n ) :
l = len ( str )
dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ]
dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1
for i in range ( 1 , l ) :
dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1
for j in range ( n ) :
... |
python-java-MINIMUM_POSSIBLE_VALUE_AI_AJ_K_GIVEN_ARRAY_K | python-java | def pairs ( arr , n , k ) :
smallest = 999999999999
count = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if abs ( arr [ i ] + arr [ j ] - k ) < smallest :
smallest = abs ( arr [ i ] + arr [ j ] - k )
count = 1
elif abs ( arr [ i ] + ... | static void pairs ( int arr [ ] , int n , int k ) {
int smallest = Integer . MAX_VALUE ;
int count = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) for ( int j = i + 1 ;
j < n ;
j ++ ) {
if ( Math . abs ( arr [ i ] + arr [ j ] - k ) < smallest ) {
smallest = Math . abs ( arr [ i ] + arr [ j ] - k ) ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def pairs ( arr , n , k ) :
smallest = 999999999999
count = 0
for i in range ( n ) :
for j in range ( i + 1 , n ) :
if abs ( arr [ i ] + arr [ j ] - k ) < smallest :
smallest = abs ( arr [ i ] + arr [ j ] - k )
count = 1
elif abs ( arr [... |
python-java-ZECKENDORFS_THEOREM_NON_NEIGHBOURING_FIBONACCI_REPRESENTATION | python-java | def nearestSmallerEqFib ( n ) :
if ( n == 0 or n == 1 ) :
return n
f1 , f2 , f3 = 0 , 1 , 1
while ( f3 <= n ) :
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
return f2 ;
| public static int nearestSmallerEqFib ( int n ) {
if ( n == 0 || n == 1 ) return n ;
int f1 = 0 , f2 = 1 , f3 = 1 ;
while ( f3 <= n ) {
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
}
return f2 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def nearestSmallerEqFib ( n ) :
if ( n == 0 or n == 1 ) :
return n
f1 , f2 , f3 = 0 , 1 , 1
while ( f3 <= n ) :
f1 = f2 ;
f2 = f3 ;
f3 = f1 + f2 ;
return f2 ;
</Code> |
python-java-SHUFFLE_A_DECK_OF_CARDS_3 | python-java | def shuffle ( card , n ) :
for i in range ( n ) :
r = i + ( random.randint ( 0 , 55 ) % ( 52 - i ) )
tmp = card [ i ]
card [ i ] = card [ r ]
card [ r ] = tmp
| public static void shuffle ( int card [ ] , int n ) {
Random rand = new Random ( ) ;
for ( int i = 0 ;
i < n ;
i ++ ) {
int r = i + rand . nextInt ( 52 - i ) ;
int temp = card [ r ] ;
card [ r ] = card [ i ] ;
card [ i ] = temp ;
}
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def shuffle ( card , n ) :
for i in range ( n ) :
r = i + ( random.randint ( 0 , 55 ) % ( 52 - i ) )
tmp = card [ i ]
card [ i ] = card [ r ]
card [ r ] = tmp
</Code> |
python-java-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM_1 | python-java | def maxLen ( arr ) :
hash_map = { }
max_len = 0
curr_sum = 0
for i in range ( len ( arr ) ) :
curr_sum += arr [ i ]
if arr [ i ] is 0 and max_len is 0 :
max_len = 1
if curr_sum is 0 :
max_len = i + 1
if curr_sum in hash_map :
max_len = ... | static int maxLen ( int arr [ ] ) {
HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ;
int sum = 0 ;
int max_len = 0 ;
for ( int i = 0 ;
i < arr . length ;
i ++ ) {
sum += arr [ i ] ;
if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ;
if ( sum == 0 ) max_len = i + 1 ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxLen ( arr ) :
hash_map = { }
max_len = 0
curr_sum = 0
for i in range ( len ( arr ) ) :
curr_sum += arr [ i ]
if arr [ i ] is 0 and max_len is 0 :
max_len = 1
if curr_sum is 0 :
max_len = i + 1
if curr_sum in hash_map :
max... |
python-java-LENGTH_LONGEST_BALANCED_SUBSEQUENCE | python-java | def maxLength ( s , n ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
for i in range ( n - 1 ) :
if ( s [ i ] == '(' and s [ i + 1 ] == ')' ) :
dp [ i ] [ i + 1 ] = 2
for l in range ( 2 , n ) :
i = - 1
for j in range ( l , n ) :
i += 1
... | static int maxLength ( String s , int n ) {
int dp [ ] [ ] = new int [ n ] [ n ] ;
for ( int i = 0 ;
i < n - 1 ;
i ++ ) if ( s . charAt ( i ) == ' ( ' && s . charAt ( i + 1 ) == ' ) ' ) dp [ i ] [ i + 1 ] = 2 ;
for ( int l = 2 ;
l < n ;
l ++ ) {
for ( int i = 0 , j = l ;
j < n ;
i ++ , j ++ ) ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def maxLength ( s , n ) :
dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ]
for i in range ( n - 1 ) :
if ( s [ i ] == '(' and s [ i + 1 ] == ')' ) :
dp [ i ] [ i + 1 ] = 2
for l in range ( 2 , n ) :
i = - 1
for j in range ( l , n ) :
i += 1
... |
python-java-PROGRAM_PRINT_SUM_GIVEN_NTH_TERM_1 | python-java | def summingSeries ( n ) :
return math.pow ( n , 2 )
| static int summingSeries ( long n ) {
return ( int ) Math . pow ( n , 2 ) ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def summingSeries ( n ) :
return math.pow ( n , 2 )
</Code> |
python-java-SUBSET_SUM_PROBLEM_OSUM_SPACE | python-java | def isSubsetSum ( arr , n , sum ) :
subset = [ [ False for j in range ( sum + 1 ) ] for i in range ( 3 ) ]
for i in range ( n + 1 ) :
for j in range ( sum + 1 ) :
if ( j == 0 ) :
subset [ i % 2 ] [ j ] = True
elif ( i == 0 ) :
subset [ i % 2 ] [ j ... | static boolean isSubsetSum ( int arr [ ] , int n , int sum ) {
boolean subset [ ] [ ] = new boolean [ 2 ] [ sum + 1 ] ;
for ( int i = 0 ;
i <= n ;
i ++ ) {
for ( int j = 0 ;
j <= sum ;
j ++ ) {
if ( j == 0 ) subset [ i % 2 ] [ j ] = true ;
else if ( i == 0 ) subset [ i % 2 ] [ j ] = fals... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isSubsetSum ( arr , n , sum ) :
subset = [ [ False for j in range ( sum + 1 ) ] for i in range ( 3 ) ]
for i in range ( n + 1 ) :
for j in range ( sum + 1 ) :
if ( j == 0 ) :
subset [ i % 2 ] [ j ] = True
elif ( i == 0 ) :
subset [ i % 2... |
python-java-FIND_INDEX_OF_AN_EXTRA_ELEMENT_PRESENT_IN_ONE_SORTED_ARRAY | python-java | def findExtra ( arr1 , arr2 , n ) :
for i in range ( 0 , n ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return i
return n
| static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ;
return n ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findExtra ( arr1 , arr2 , n ) :
for i in range ( 0 , n ) :
if ( arr1 [ i ] != arr2 [ i ] ) :
return i
return n
</Code> |
python-java-NUMBER_OF_PERMUTATION_WITH_K_INVERSIONS | python-java | def numberOfPermWithKInversion ( N , K ) :
if ( N == 0 ) : return 0
if ( K == 0 ) : return 1
if ( memo [ N ] [ K ] != 0 ) :
return memo [ N ] [ K ]
sum = 0
for i in range ( K + 1 ) :
if ( i <= N - 1 ) :
sum += numberOfPermWithKInversion ( N - 1 , K - i )
memo [ N ] [ ... | static int numberOfPermWithKInversion ( int N , int K ) {
if ( N == 0 ) return 0 ;
if ( K == 0 ) return 1 ;
if ( memo [ N ] [ K ] != 0 ) return memo [ N ] [ K ] ;
int sum = 0 ;
for ( int i = 0 ;
i <= K ;
i ++ ) {
if ( i <= N - 1 ) sum += numberOfPermWithKInversion ( N - 1 , K - i ) ;
}
memo [ N ] ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def numberOfPermWithKInversion ( N , K ) :
if ( N == 0 ) : return 0
if ( K == 0 ) : return 1
if ( memo [ N ] [ K ] != 0 ) :
return memo [ N ] [ K ]
sum = 0
for i in range ( K + 1 ) :
if ( i <= N - 1 ) :
sum += numberOfPermWithKInversion ( N - 1 , K - i )
memo [... |
python-java-DELETE_CONSECUTIVE_WORDS_SEQUENCE_1 | python-java | def removeConsecutiveSame ( v ) :
st = [ ]
for i in range ( len ( v ) ) :
if ( len ( st ) == 0 ) :
st.append ( v [ i ] )
else :
Str = st [ - 1 ]
if ( Str == v [ i ] ) :
st.pop ( )
else :
st.append ( v [ i ] )
ret... | static int removeConsecutiveSame ( Vector < String > v ) {
Stack < String > st = new Stack < > ( ) ;
for ( int i = 0 ;
i < v . size ( ) ;
i ++ ) {
if ( st . empty ( ) ) st . push ( v . get ( i ) ) ;
else {
String str = st . peek ( ) ;
if ( str . equals ( v . get ( i ) ) ) st . pop ( ) ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def removeConsecutiveSame ( v ) :
st = [ ]
for i in range ( len ( v ) ) :
if ( len ( st ) == 0 ) :
st.append ( v [ i ] )
else :
Str = st [ - 1 ]
if ( Str == v [ i ] ) :
st.pop ( )
else :
st.append ( v [ i ] )
... |
python-java-MINIMUM_INDEX_SUM_COMMON_ELEMENTS_TWO_LISTS | python-java | def find ( list1 , list2 ) :
res = [ ]
max_possible_sum = len ( list1 ) + len ( list2 ) - 2
for sum in range ( max_possible_sum + 1 ) :
for i in range ( sum + 1 ) :
if ( i < len ( list1 ) and ( sum - i ) < len ( list2 ) and list1 [ i ] == list2 [ sum - i ] ) :
res.append ... | static void find ( Vector < String > list1 , Vector < String > list2 ) {
Vector < String > res = new Vector < > ( ) ;
int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ;
for ( int sum = 0 ;
sum <= max_possible_sum ;
sum ++ ) {
for ( int i = 0 ;
i <= sum ;
i ++ ) if ( i < list1 . size ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def find ( list1 , list2 ) :
res = [ ]
max_possible_sum = len ( list1 ) + len ( list2 ) - 2
for sum in range ( max_possible_sum + 1 ) :
for i in range ( sum + 1 ) :
if ( i < len ( list1 ) and ( sum - i ) < len ( list2 ) and list1 [ i ] == list2 [ sum - i ] ) :
res.... |
python-java-NON_REPEATING_ELEMENT | python-java | def firstNonRepeating ( arr , n ) :
for i in range ( n ) :
j = 0
while ( j < n ) :
if ( i != j and arr [ i ] == arr [ j ] ) :
break
j += 1
if ( j == n ) :
return arr [ i ]
return - 1
| static int firstNonRepeating ( int arr [ ] , int n ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
int j ;
for ( j = 0 ;
j < n ;
j ++ ) if ( i != j && arr [ i ] == arr [ j ] ) break ;
if ( j == n ) return arr [ i ] ;
}
return - 1 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def firstNonRepeating ( arr , n ) :
for i in range ( n ) :
j = 0
while ( j < n ) :
if ( i != j and arr [ i ] == arr [ j ] ) :
break
j += 1
if ( j == n ) :
return arr [ i ]
return - 1
</Code> |
python-java-HOW_TO_BEGIN_WITH_COMPETITIVE_PROGRAMMING | python-java | def search ( arr , x ) :
n = len ( arr )
for j in range ( 0 , n ) :
if ( x == arr [ j ] ) :
return j
return - 1
| static int search ( int arr [ ] , int n , int x ) {
for ( int i = 0 ;
i < n ;
i ++ ) {
if ( arr [ i ] == x ) return i ;
}
return - 1 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def search ( arr , x ) :
n = len ( arr )
for j in range ( 0 , n ) :
if ( x == arr [ j ] ) :
return j
return - 1
</Code> |
python-java-SUM_MIDDLE_ROW_COLUMN_MATRIX | python-java | def middlesum ( mat , n ) :
row_sum = 0
col_sum = 0
for i in range ( n ) :
row_sum += mat [ n // 2 ] [ i ]
print ( "Sum of middle row = " , row_sum )
for i in range ( n ) :
col_sum += mat [ i ] [ n // 2 ]
print ( "Sum of middle column = " , col_sum )
| static void middlesum ( int mat [ ] [ ] , int n ) {
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ;
i < n ;
i ++ ) row_sum += mat [ n / 2 ] [ i ] ;
System . out . println ( "Sum of middle row = " + row_sum ) ;
for ( int i = 0 ;
i < n ;
i ++ ) col_sum += mat [ i ] [ n / 2 ] ;
System . out . println (... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def middlesum ( mat , n ) :
row_sum = 0
col_sum = 0
for i in range ( n ) :
row_sum += mat [ n // 2 ] [ i ]
print ( "Sum of middle row = " , row_sum )
for i in range ( n ) :
col_sum += mat [ i ] [ n // 2 ]
print ( "Sum of middle column = " , col_sum )
</Code> |
python-java-MULTIPLY_LARGE_INTEGERS_UNDER_LARGE_MODULO | python-java | def moduloMultiplication ( a , b , mod ) :
res = 0 ;
a = a % mod ;
while ( b ) :
if ( b & 1 ) :
res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
return res ;
| static long moduloMultiplication ( long a , long b , long mod ) {
long res = 0 ;
a %= mod ;
while ( b > 0 ) {
if ( ( b & 1 ) > 0 ) {
res = ( res + a ) % mod ;
}
a = ( 2 * a ) % mod ;
b >>= 1 ;
}
return res ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def moduloMultiplication ( a , b , mod ) :
res = 0 ;
a = a % mod ;
while ( b ) :
if ( b & 1 ) :
res = ( res + a ) % mod ;
a = ( 2 * a ) % mod ;
b >>= 1 ;
return res ;
</Code> |
python-java-GENERATE_PYTHAGOREAN_TRIPLETS | python-java | def pythagoreanTriplets ( limits ) :
c , m = 0 , 2
while c < limits :
for n in range ( 1 , m ) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits :
break
print ( a , b , c )
m = m + 1
| static void pythagoreanTriplets ( int limit ) {
int a , b , c = 0 ;
int m = 2 ;
while ( c < limit ) {
for ( int n = 1 ;
n < m ;
++ n ) {
a = m * m - n * n ;
b = 2 * m * n ;
c = m * m + n * n ;
if ( c > limit ) break ;
System . out . println ( a + " " + b + " " + c ) ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def pythagoreanTriplets ( limits ) :
c , m = 0 , 2
while c < limits :
for n in range ( 1 , m ) :
a = m * m - n * n
b = 2 * m * n
c = m * m + n * n
if c > limits :
break
print ( a , b , c )
m = m + 1
</Code> |
python-java-SUM_PAIRWISE_PRODUCTS_2 | python-java | def findSum ( n ) :
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24
| static int findSum ( int n ) {
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24 ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def findSum ( n ) :
return n * ( n + 1 ) * ( n + 2 ) * ( 3 * n + 1 ) / 24
</Code> |
python-java-PROGRAM_FOR_NTH_FIBONACCI_NUMBER_2 | python-java | def fib ( n ) :
if ( n == 0 ) :
return 0
if ( n == 1 or n == 2 ) :
f [ n ] = 1
return ( f [ n ] )
if ( f [ n ] ) :
return f [ n ]
if ( n & 1 ) :
k = ( n + 1 ) // 2
else :
k = n // 2
if ( ( n & 1 ) ) :
f [ n ] = ( fib ( k ) * fib ( k ) + fib... | public static int fib ( int n ) {
if ( n == 0 ) return 0 ;
if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ;
if ( f [ n ] != 0 ) return f [ n ] ;
int k = ( n & 1 ) == 1 ? ( n + 1 ) / 2 : n / 2 ;
f [ n ] = ( n & 1 ) == 1 ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def fib ( n ) :
if ( n == 0 ) :
return 0
if ( n == 1 or n == 2 ) :
f [ n ] = 1
return ( f [ n ] )
if ( f [ n ] ) :
return f [ n ]
if ( n & 1 ) :
k = ( n + 1 ) // 2
else :
k = n // 2
if ( ( n & 1 ) ) :
f [ n ] = ( fib ( k ) * fib ( k ... |
python-java-GIVEN_P_AND_N_FIND_THE_LARGEST_X_SUCH_THAT_PX_DIVIDES_N_2 | python-java | def largestPower ( n , p ) :
x = 0
while n :
n /= p
x += n
return x
| static int Largestpower ( int n , int p ) {
int ans = 0 ;
while ( n > 0 ) {
n /= p ;
ans += n ;
}
return ans ;
}
| Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def largestPower ( n , p ) :
x = 0
while n :
n /= p
x += n
return x
</Code> |
python-java-PROGRAM_PRINT_IDENTITY_MATRIX_1 | python-java | def isIdentity ( mat , N ) :
for row in range ( N ) :
for col in range ( N ) :
if ( row == col and mat [ row ] [ col ] != 1 ) :
return False
elif ( row != col and mat [ row ] [ col ] != 0 ) :
return False
return True
| static boolean isIdentity ( int mat [ ] [ ] , int N ) {
for ( int row = 0 ;
row < N ;
row ++ ) {
for ( int col = 0 ;
col < N ;
col ++ ) {
if ( row == col && mat [ row ] [ col ] != 1 ) return false ;
else if ( row != col && mat [ row ] [ col ] != 0 ) return false ;
}
}
return true ;... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def isIdentity ( mat , N ) :
for row in range ( N ) :
for col in range ( N ) :
if ( row == col and mat [ row ] [ col ] != 1 ) :
return False
elif ( row != col and mat [ row ] [ col ] != 0 ) :
return False
return True
</Code> |
python-java-MINIMUM_CELLS_REQUIRED_REACH_DESTINATION_JUMPS_EQUAL_CELL_VALUES | python-java | def minCells ( mat , m , n ) :
dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( m ) :
for j in range ( n ) :
if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) :
... | static int minCells ( int mat [ ] [ ] , int m , int n ) {
int dp [ ] [ ] = new int [ m ] [ n ] ;
for ( int i = 0 ;
i < m ;
i ++ ) for ( int j = 0 ;
j < n ;
j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ;
dp [ 0 ] [ 0 ] = 1 ;
for ( int i = 0 ;
i < m ;
i ++ ) {
for ( int j = 0 ;
j < n ;
j ++... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def minCells ( mat , m , n ) :
dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ]
dp [ 0 ] [ 0 ] = 1
for i in range ( m ) :
for j in range ( n ) :
if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) :
... |
python-java-REARRANGE_BINARY_STRING_ALTERNATE_X_Y_OCCURRENCES | python-java | def arrangeString ( str1 , x , y ) :
count_0 = 0
count_1 = 0
n = len ( str1 )
for i in range ( n ) :
if str1 [ i ] == '0' :
count_0 += 1
else :
count_1 += 1
while count_0 > 0 or count_1 > 0 :
for i in range ( 0 , x ) :
if count_0 > 0 :
... | static void arrangeString ( String str , int x , int y ) {
int count_0 = 0 ;
int count_1 = 0 ;
int len = str . length ( ) ;
for ( int i = 0 ;
i < len ;
i ++ ) {
if ( str . charAt ( i ) == ' 0 ' ) count_0 ++ ;
else count_1 ++ ;
}
while ( count_0 > 0 || count_1 > 0 ) {
for ( int j = 0 ;
j ... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def arrangeString ( str1 , x , y ) :
count_0 = 0
count_1 = 0
n = len ( str1 )
for i in range ( n ) :
if str1 [ i ] == '0' :
count_0 += 1
else :
count_1 += 1
while count_0 > 0 or count_1 > 0 :
for i in range ( 0 , x ) :
if count_0 > 0... |
python-java-MINIMUM_NUMBER_DELETIONS_MAKE_STRING_PALINDROME_SET_2 | python-java | def getLevenstein ( inpt ) :
revInput = inpt [ : : - 1 ]
n = len ( inpt )
dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ 0 ] [ i ] = i
dp [ i ] [ 0 ] = i
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
... | static int getLevenstein ( StringBuilder input ) {
StringBuilder revInput = new StringBuilder ( input ) ;
revInput = revInput . reverse ( ) ;
int n = input . length ( ) ;
int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ;
for ( int i = 0 ;
i <= n ;
++ i ) {
dp [ 0 ] [ i ] = i ;
dp [ i ] [ 0 ] = i ;
... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def getLevenstein ( inpt ) :
revInput = inpt [ : : - 1 ]
n = len ( inpt )
dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ]
for i in range ( n + 1 ) :
dp [ 0 ] [ i ] = i
dp [ i ] [ 0 ] = i
for i in range ( 1 , n + 1 ) :
for j in range ( 1 , n + 1 ) :
... |
python-java-DYNAMIC_PROGRAMMING_SET_34_ASSEMBLY_LINE_SCHEDULING | python-java | def carAssembly ( a , t , e , x ) :
NUM_STATION = len ( a [ 0 ] )
T1 = [ 0 for i in range ( NUM_STATION ) ]
T2 = [ 0 for i in range ( NUM_STATION ) ]
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ]
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ]
for i in range ( 1 , NUM_STATION ) :
T1 [ i ] = min ( T1 [ i - 1 ] +... | static int carAssembly ( int a [ ] [ ] , int t [ ] [ ] , int e [ ] , int x [ ] ) {
int T1 [ ] = new int [ NUM_STATION ] ;
int T2 [ ] = new int [ NUM_STATION ] ;
int i ;
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ] ;
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ] ;
for ( i = 1 ;
i < NUM_STATION ;
++ i ) {
T1 [ i ] = min (... | Translate the given code from python to java. The input Code is marked with <Code> and </Code>. | <Code>
def carAssembly ( a , t , e , x ) :
NUM_STATION = len ( a [ 0 ] )
T1 = [ 0 for i in range ( NUM_STATION ) ]
T2 = [ 0 for i in range ( NUM_STATION ) ]
T1 [ 0 ] = e [ 0 ] + a [ 0 ] [ 0 ]
T2 [ 0 ] = e [ 1 ] + a [ 1 ] [ 0 ]
for i in range ( 1 , NUM_STATION ) :
T1 [ i ] = min ( T1 [ i ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.