id
stringlengths
18
123
pair
stringclasses
6 values
Code1
stringlengths
41
1.98k
Code2
stringlengths
41
1.98k
instruction
stringclasses
6 values
input
stringlengths
56
2k
cpp-java-COUNT_EVEN_LENGTH_BINARY_SEQUENCES_WITH_SAME_SUM_OF_FIRST_AND_SECOND_HALF_BITS_1
cpp-java
int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; }
static int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int countSeq ( int n ) { int nCr = 1 , res = 1 ; for ( int r = 1 ; r <= n ; r ++ ) { nCr = ( nCr * ( n + 1 - r ) ) / r ; res += nCr * nCr ; } return res ; } </Code>
cpp-java-PROGRAM_CALCULATE_AREA_OCTAGON
cpp-java
double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ; }
static double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + sqrt ( 2 ) ) * side * side ) ; } </Code>
cpp-java-MAXIMUM_CONSECUTIVE_NUMBERS_PRESENT_ARRAY
cpp-java
int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ) != S ...
static int findLongestConseqSubseq ( int arr [ ] , int n ) { HashSet < Integer > S = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) S . add ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . contains ( arr [ i ] ) ) { int j = arr [ i ] ; while ( S ....
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int findLongestConseqSubseq ( int arr [ ] , int n ) { unordered_set < int > S ; for ( int i = 0 ; i < n ; i ++ ) S . insert ( arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( S . find ( arr [ i ] - 1 ) == S . end ( ) ) { int j = arr [ i ] ; while ( S . find ( j ...
cpp-java-SHORTEST_POSSIBLE_COMBINATION_TWO_STRINGS
cpp-java
void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ( a [ i...
static void printSuperSeq ( String a , String b ) { int m = a . length ( ) , n = b . length ( ) ; int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> void printSuperSeq ( string & a , string & b ) { int m = a . length ( ) , n = b . length ( ) ; int dp [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( ! i ) dp [ i ] [ j ] = j ; else if ( ! j ) dp [ i ] [ j ] = i ; else if ...
cpp-java-COUNT_POSSIBLE_PATHS_TOP_LEFT_BOTTOM_RIGHT_NXM_MATRIX_2
cpp-java
int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; }
static int numberOfPaths ( int m , int n ) { int [ ] dp = new int [ n ] ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int numberOfPaths ( int m , int n ) { int dp [ n ] = { 1 } ; dp [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { dp [ j ] += dp [ j - 1 ] ; } } return dp [ n - 1 ] ; } </Code>
cpp-java-FIND_WHETHER_A_GIVEN_NUMBER_IS_A_POWER_OF_4_OR_NOT_1
cpp-java
bool isPowerOfFour ( unsigned int n ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; }
static int isPowerOfFour ( int n ) { int count = 0 ; int x = n & ( n - 1 ) ; if ( n > 0 && x == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> bool isPowerOfFour ( unsigned int n ) { int count = 0 ; if ( n && ! ( n & ( n - 1 ) ) ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } </Code>
cpp-java-FIND_SUM_ODD_FACTORS_NUMBER
cpp-java
int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } re...
static int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; }...
cpp-java-FIND_THE_ELEMENT_BEFORE_WHICH_ALL_THE_ELEMENTS_ARE_SMALLER_THAN_IT_AND_AFTER_WHICH_ALL_ARE_GREATER_THAN_IT
cpp-java
int findElement ( int arr [ ] , int n ) { int leftMax [ n ] ; leftMax [ 0 ] = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( leftMax [ i ] < arr [ i ] && rightMin > arr [ i...
static int findElement ( int [ ] arr , int n ) { int [ ] leftMax = new int [ n ] ; leftMax [ 0 ] = Integer . MIN_VALUE ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = Math . max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int findElement ( int arr [ ] , int n ) { int leftMax [ n ] ; leftMax [ 0 ] = INT_MIN ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = INT_MAX ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( leftMax [ i ] < arr [ i ] && rightMin > ...
cpp-java-SEARCH_AN_ELEMENT_IN_A_SORTED_AND_PIVOTED_ARRAY
cpp-java
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 ...
static 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 +...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 +...
cpp-java-EXPONENTIAL_SQUARING_FAST_MODULO_MULTIPLICATION
cpp-java
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 ; }
static long exponentiation ( long base , long exp ) { if ( exp == 0 ) return 1 ; if ( exp == 1 ) return base % N ; long 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 cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-java-DIVISIBILITY_CHECK
cpp-java
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 ) ...
static void divisibilityCheck ( List < Integer > arr , int n ) { List < Integer > s = new ArrayList < Integer > ( ) ; int max_ele = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { s . add ( arr . get ( i ) ) ; max_ele = Math . max ( max_ele , arr . get ( i ) ) ; } LinkedHashSet < Integer ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ] ...
cpp-java-FIND_THE_TWO_REPEATING_ELEMENTS_IN_A_GIVEN_ARRAY_3
cpp-java
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 ] ) << " " ; } }
void printRepeating ( int arr [ ] , int size ) { int i ; System . out . println ( "The repeating elements are : " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ Math . abs ( arr [ i ] ) ] > 0 ) arr [ Math . abs ( arr [ i ] ) ] = - arr [ Math . abs ( arr [ i ] ) ] ; else System . out . print ( Math ....
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ] ) << " " ; } } </Code>
cpp-java-DYNAMIC_PROGRAMMING_SET_17_PALINDROME_PARTITIONING
cpp-java
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 = ...
static int minPalPartion ( String str ) { int n = str . length ( ) ; int [ ] [ ] C = new int [ n ] [ n ] ; boolean [ ] [ ] P = new boolean [ 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 (...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ++ ) { ...
cpp-java-HEIGHT_COMPLETE_BINARY_TREE_HEAP_N_NODES
cpp-java
int height ( int N ) { return ceil ( log2 ( N + 1 ) ) - 1 ; }
static int height ( int N ) { return ( int ) Math . ceil ( Math . log ( N + 1 ) / Math . log ( 2 ) ) - 1 ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int height ( int N ) { return ceil ( log2 ( N + 1 ) ) - 1 ; } </Code>
cpp-java-DISTRIBUTING_ITEMS_PERSON_CANNOT_TAKE_TWO_ITEMS_TYPE_1
cpp-java
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 ; }
static boolean checkCount ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > hash = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! hash . containsKey ( arr [ i ] ) ) hash . put ( arr [ i ] , 0 ) ; hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } for ( Map . E...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-java-REMOVE_CONSECUTIVE_DUPLICATES_STRING
cpp-java
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' ; }
static void removeDuplicates ( char [ ] S ) { int n = S . length ; if ( n < 2 ) { return ; } int j = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S [ j ] != S [ i ] ) { j ++ ; S [ j ] = S [ i ] ; } } System . out . println ( Arrays . copyOfRange ( S , 0 , j + 1 ) ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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' ; } </Code>
cpp-java-THIRD_LARGEST_ELEMENT_ARRAY_DISTINCT_ELEMENTS_1
cpp-java
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 ; ...
static void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { System . out . printf ( " Invalid Input " ) ; return ; } int first = arr [ 0 ] , second = Integer . MIN_VALUE , third = Integer . MIN_VALUE ; for ( int i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] > first ) { ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ...
cpp-java-LONGEST_INCREASING_ODD_EVEN_SUBSEQUENCE
cpp-java
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 ) ...
public static int longOddEvenIncSeq ( int arr [ ] , int n ) { int [ ] lioes = new int [ 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 && lioe...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ]...
cpp-java-ADD_1_TO_A_GIVEN_NUMBER_1
cpp-java
int addOne ( int x ) { return ( - ( ~ x ) ) ; }
static int addOne ( int x ) { return ( - ( ~ x ) ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int addOne ( int x ) { return ( - ( ~ x ) ) ; } </Code>
cpp-java-NUMBER_DECIMAL_NUMBERS_LENGTH_K_STRICT_MONOTONE
cpp-java
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...
static int getNumStrictMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; 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 ] ; return DP [ len...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ]...
cpp-java-SEARCH_ALMOST_SORTED_ARRAY
cpp-java
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...
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 cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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...
cpp-java-BRESENHAMS_LINE_GENERATION_ALGORITHM
cpp-java
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_...
static 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 ++ ) { System . out . print ( "(" + x + "," + y + ")\n" ) ; slope_error_new += m_new ; if ( slope_error_new >= 0 ) { ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ++ ; ...
cpp-java-HIGHWAY_BILLBOARD_PROBLEM
cpp-java
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 <...
static int maxRevenue ( int m , int [ ] x , int [ ] revenue , int n , int t ) { int [ ] maxRev = new int [ m + 1 ] ; for ( int i = 0 ; i < m + 1 ; i ++ ) maxRev [ i ] = 0 ; int nxtbb = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { if ( nxtbb < n ) { if ( x [ nxtbb ] != i ) maxRev [ i ] = maxRev [ i ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 { i...
cpp-java-PROGRAM_FIND_LINE_PASSING_2_POINTS
cpp-java
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 ; } ...
static void lineFromPoints ( Pair P , Pair Q ) { int a = Q . second - P . second ; int b = P . first - Q . first ; int c = a * ( P . first ) + b * ( P . second ) ; if ( b < 0 ) { System . out . println ( "The line passing through points P and Q is: " + a + "x " + b + "y = " + c ) ; } else { System ....
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ;...
cpp-java-CHINESE_REMAINDER_THEOREM_SET_1_INTRODUCTION
cpp-java
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 ; }
static 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 ++ ; } }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-java-FIND_A_TRIPLET_THAT_SUM_TO_A_GIVEN_VALUE
cpp-java
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 <<...
boolean 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 ) { Syst...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ) { ...
cpp-java-K_MAXIMUM_SUM_COMBINATIONS_TWO_ARRAYS
cpp-java
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 ++ ; }...
static void KMaxCombinations ( int A [ ] , int B [ ] , int N , int K ) { PriorityQueue < Integer > pq = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < N ; j ++ ) pq . add ( A [ i ] + B [ j ] ) ; int count = 0 ; while ( count < K ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 +...
cpp-java-TRIANGULAR_MATCHSTICK_NUMBER
cpp-java
int numberOfSticks ( int x ) { return ( 3 * x * ( x + 1 ) ) / 2 ; }
public static int numberOfSticks ( int x ) { return ( 3 * x * ( x + 1 ) ) / 2 ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int numberOfSticks ( int x ) { return ( 3 * x * ( x + 1 ) ) / 2 ; } </Code>
cpp-java-COUNT_SUBSTRINGS_WITH_SAME_FIRST_AND_LAST_CHARACTERS_1
cpp-java
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 ) ; ...
static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int countSubstringWithEqualEnds ( string s ) { int result = 0 ; int n = s . length ( ) ; 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 )...
cpp-java-DIVISIBILITY_BY_7
cpp-java
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 ) ) ; }
static boolean isDivisibleBy7 ( int num ) { if ( num < 0 ) return isDivisibleBy7 ( - num ) ; if ( num == 0 || num == 7 ) return true ; if ( num < 10 ) return false ; return isDivisibleBy7 ( num / 10 - 2 * ( num - num / 10 * 10 ) ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ) ) ; } </Code>
cpp-java-FIND_ONE_MULTIPLE_REPEATING_ELEMENTS_READ_ARRAY
cpp-java
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...
static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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...
cpp-java-FIND_N_TH_ELEMENT_FROM_STERNS_DIATOMIC_SERIES
cpp-java
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 ] ; }
static int findSDSFunc ( int n ) { int DP [ ] = new int [ 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 cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ] ; } </Code>
cpp-java-RECURSIVE_C_PROGRAM_LINEARLY_SEARCH_ELEMENT_GIVEN_ARRAY
cpp-java
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 ) ; }
static 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 cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ) ; } </Code>
cpp-java-CHECK_LARGE_NUMBER_DIVISIBLE_4_NOT
cpp-java
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 ) ; }
static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str . charAt ( 0 ) - ' 0 ' ) % 4 == 0 ) ; int last = str . charAt ( n - 1 ) - ' 0 ' ; int second_last = str . charAt ( n - 2 ) - ' 0 ' ; return ( ( second_last * 10 + last ) % 4 == 0 ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ) ; } </Code>
cpp-java-LOWER_CASE_UPPER_CASE_INTERESTING_FACT
cpp-java
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 ; }
static String to_upper ( char [ ] in ) { for ( int i = 0 ; i < in . length ; i ++ ) { if ( ' a ' <= in [ i ] & in [ i ] <= ' z ' ) { in [ i ] = ( char ) ( in [ i ] - ' a ' + ' A ' ) ; } } return String . valueOf ( in ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-java-REMOVE_MINIMUM_NUMBER_CHARACTERS_TWO_STRINGS_BECOME_ANAGRAM
cpp-java
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' ] ++ ;...
static int remAnagram ( String str1 , String str2 ) { int count1 [ ] = new int [ 26 ] ; int count2 [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) count2 [ str2 . charAt ( i ) - ' a ' ] +...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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'...
cpp-java-SORT_ARRAY_WAVE_FORM_2_1
cpp-java
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 ] ) ; } }
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 - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ] ) ; } } </Code>
cpp-java-FORM_MINIMUM_NUMBER_FROM_GIVEN_SEQUENCE_1
cpp-java
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 +...
static String getMinNumberForPattern ( String seq ) { int n = seq . length ( ) ; if ( n >= 9 ) return "-1" ; char result [ ] = new char [ n + 1 ] ; int count = 1 ; for ( int i = 0 ; i <= n ; i ++ ) { if ( i == n || seq . charAt ( i ) == ' I ' ) { for ( int j = i - 1 ; j >= - 1 ; j --...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 -- ) { resul...
cpp-java-CHECK_IF_STRING_REMAINS_PALINDROME_AFTER_REMOVING_GIVEN_NUMBER_OF_CHARACTERS
cpp-java
bool isPossible ( string str , int n ) { int len = str . length ( ) ; if ( len >= n ) return true ; return false ; }
static boolean isPossible ( String str , int n ) { int len = str . length ( ) ; if ( len >= n ) return true ; return false ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> bool isPossible ( string str , int n ) { int len = str . length ( ) ; if ( len >= n ) return true ; return false ; } </Code>
cpp-java-FIND_CENTER_CIRCLE_USING_ENDPOINTS_DIAMETER
cpp-java
void center ( int x1 , int x2 , int y1 , int y2 ) { cout << ( float ) ( x1 + x2 ) / 2 << ", " << ( float ) ( y1 + y2 ) / 2 ; }
static void center ( int x1 , int x2 , int y1 , int y2 ) { System . out . print ( ( float ) ( x1 + x2 ) / 2 + ", " + ( float ) ( y1 + y2 ) / 2 ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> void center ( int x1 , int x2 , int y1 , int y2 ) { cout << ( float ) ( x1 + x2 ) / 2 << ", " << ( float ) ( y1 + y2 ) / 2 ; } </Code>
cpp-java-GREEDY_ALGORITHM_TO_FIND_MINIMUM_NUMBER_OF_COINS
cpp-java
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 ] << " " ; }
static void findMin ( int V ) { Vector < Integer > ans = new Vector < > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( V >= deno [ i ] ) { V -= deno [ i ] ; ans . add ( deno [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { System . out . print ( " " + ans . el...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ] << " " ; } </Code>
cpp-java-HOW_TO_AVOID_OVERFLOW_IN_MODULAR_MULTIPLICATION
cpp-java
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 ; }
static long mulmod ( long a , long b , long mod ) { long 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 cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-java-FIND_NTH_TERM_DRAGON_CURVE_SEQUENCE
cpp-java
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 =...
static 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 . charAt ( j ) ; if ( prev == ' 0 ' ) { temp...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; ...
cpp-java-MINIMUM_POSITIVE_POINTS_TO_REACH_DESTINATION
cpp-java
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...
static int minInitialPoints ( int points [ ] [ ] , int R , int C ) { int dp [ ] [ ] = new int [ R ] [ C ] ; int m = R , n = C ; dp [ m - 1 ] [ n - 1 ] = points [ m - 1 ] [ n - 1 ] > 0 ? 1 : Math . abs ( points [ m - 1 ] [ n - 1 ] ) + 1 ; for ( int i = m - 2 ; i >= 0 ; i -- ) dp [ i ] [ n - 1 ] = Math . max ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 - ...
cpp-java-FIND_REPETITIVE_ELEMENT_1_N_1
cpp-java
int findRepeating ( int arr [ ] , int n ) { return accumulate ( arr , arr + n , 0 ) - ( ( n - 1 ) * n / 2 ) ; }
static int findRepeating ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum - ( ( ( n - 1 ) * n ) / 2 ) ; }
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> int findRepeating ( int arr [ ] , int n ) { return accumulate ( arr , arr + n , 0 ) - ( ( n - 1 ) * n / 2 ) ; } </Code>
cpp-java-C_PROGRAM_FIND_SECOND_FREQUENT_CHARACTER
cpp-java
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 ; ...
static char getSecondMostFreq ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) ( count [ str . charAt ( i ) ] ) ++ ; int first = 0 , second = 0 ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] > count [ first ] ) { seco...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 = f...
cpp-java-SUM_TWO_LARGE_NUMBERS
cpp-java
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...
static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = "" ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; str1 = new StringBuilder ( str1 ) . reverse ( ) . toString ( ) ; str2 = new St...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; ...
cpp-java-NEXT_GREATER_ELEMENT
cpp-java
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 ; } }
static 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 ; } } System . out . println ( arr [ i ] + " -- " + next ) ...
Translate the given code from cpp to java. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } } ...
cpp-python-PAIRS_OF_POSITIVE_NEGATIVE_VALUES_IN_AN_ARRAY
cpp-python
void printPairs ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; sort ( v . begin ( ) , v . end ( ) ) ; for ( int i = ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void printPairs ( int arr [ ] , int n ) { vector < int > v ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) v . push_back ( abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; sort ( v . begin ( ) , v . end ( ) ) ; for ( i...
cpp-python-C_PROGRAM_FIND_AREA_TRIANGLE
cpp-python
float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { cout << "Not a valid trianglen" ; exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; }
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 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { cout << "Not a valid trianglen" ; exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; } </Code>
cpp-python-CHECK_ARRAY_MAJORITY_ELEMENT
cpp-python
bool isMajority ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] ++ ; for ( auto x : mp ) if ( x . second >= n / 2 ) return true ; return false ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isMajority ( int a [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ a [ i ] ] ++ ; for ( auto x : mp ) if ( x . second >= n / 2 ) return true ; return false ; } </Code>
cpp-python-NUMBER_N_DIGITS_NON_DECREASING_INTEGERS
cpp-python
int nonDecNums ( int n ) { int a [ 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 ] [ j + 1 ] ; return a [...
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 ] ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int nonDecNums ( int n ) { int a [ 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 ] [ j + 1 ] ; ret...
cpp-python-PROGRAM_FIRST_FIT_ALGORITHM_MEMORY_MANAGEMENT
cpp-python
void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocation [ i ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void firstFit ( int blockSize [ ] , int m , int processSize [ ] , int n ) { int allocation [ n ] ; memset ( allocation , - 1 , sizeof ( allocation ) ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( blockSize [ j ] >= processSize [ i ] ) { allocati...
cpp-python-NON_REPEATING_ELEMENT_1
cpp-python
int firstNonRepeating ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( mp [ arr [ i ] ] == 1 ) return arr [ i ] ; return - 1 ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int firstNonRepeating ( int arr [ ] , int n ) { unordered_map < int , int > mp ; for ( int i = 0 ; i < n ; i ++ ) mp [ arr [ i ] ] ++ ; for ( int i = 0 ; i < n ; i ++ ) if ( mp [ arr [ i ] ] == 1 ) return arr [ i ] ; return - 1 ; } </Code>
cpp-python-DISTRIBUTING_ALL_BALLS_WITHOUT_REPETITION
cpp-python
bool distributingBalls ( int k , int n , string str ) { int a [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ str [ i ] - 'a' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool distributingBalls ( int k , int n , string str ) { int a [ MAX_CHAR ] = { 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ str [ i ] - 'a' ] ++ ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( a [ i ] > k ) return false ; return true ; } </Code>
cpp-python-BREAK_NUMBER_THREE_PARTS_1
cpp-python
ll count_of_ways ( ll n ) { ll count ; count = ( n + 1 ) * ( n + 2 ) / 2 ; return count ; }
def count_of_ways ( n ) : count = 0 count = ( n + 1 ) * ( n + 2 ) // 2 return count
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> ll count_of_ways ( ll n ) { ll count ; count = ( n + 1 ) * ( n + 2 ) / 2 ; return count ; } </Code>
cpp-python-MAXIMUM_SUM_IARRI_AMONG_ROTATIONS_GIVEN_ARRAY_1
cpp-python
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 - arr [ i...
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 )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 - ...
cpp-python-CALCULATE_ANGLE_HOUR_HAND_MINUTE_HAND
cpp-python
int calcAngle ( double h , double m ) { if ( h < 0 || m < 0 || h > 12 || m > 60 ) printf ( "Wrong input" ) ; if ( h == 12 ) h = 0 ; if ( m == 60 ) m = 0 ; int hour_angle = 0.5 * ( h * 60 + m ) ; int minute_angle = 6 * m ; int angle = abs ( hour_angle - minute_angle ) ; angle = min ( 360 - angle , angle ) ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int calcAngle ( double h , double m ) { if ( h < 0 || m < 0 || h > 12 || m > 60 ) printf ( "Wrong input" ) ; if ( h == 12 ) h = 0 ; if ( m == 60 ) m = 0 ; int hour_angle = 0.5 * ( h * 60 + m ) ; int minute_angle = 6 * m ; int angle = abs ( hour_angle - minute_angle ) ; angle = min ( 360 - angle , a...
cpp-python-SMALLEST_OF_THREE_INTEGERS_WITHOUT_COMPARISON_OPERATORS_1
cpp-python
int smallest ( int x , int y , int z ) { if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ; return ( ! ( x / z ) ) ? x : z ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int smallest ( int x , int y , int z ) { if ( ! ( y / x ) ) return ( ! ( y / z ) ) ? y : z ; return ( ! ( x / z ) ) ? x : z ; } </Code>
cpp-python-FIND_ONE_EXTRA_CHARACTER_STRING_1
cpp-python
char findExtraCharcter ( string strA , string strB ) { int res = 0 , i ; for ( i = 0 ; i < strA . length ( ) ; i ++ ) { res ^= strA [ i ] ; } for ( i = 0 ; i < strB . length ( ) ; i ++ ) { res ^= strB [ i ] ; } return ( ( char ) ( res ) ) ; }
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 ) ) ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> char findExtraCharcter ( string strA , string strB ) { int res = 0 , i ; for ( i = 0 ; i < strA . length ( ) ; i ++ ) { res ^= strA [ i ] ; } for ( i = 0 ; i < strB . length ( ) ; i ++ ) { res ^= strB [ i ] ; } return ( ( char ) ( res ) ) ; } </Code>
cpp-python-RECAMANS_SEQUENCE_1
cpp-python
void recaman ( int n ) { if ( n <= 0 ) return ; printf ( "%d, " , 0 ) ; unordered_set < int > s ; s . insert ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ; s . insert ( curr ) ; printf ...
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 = '' ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void recaman ( int n ) { if ( n <= 0 ) return ; printf ( "%d, " , 0 ) ; unordered_set < int > s ; s . insert ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . find ( curr ) != s . end ( ) ) curr = prev + i ; s . insert ( curr ) ; ...
cpp-python-PROGRAM_FIND_SMALLEST_DIFFERENCE_ANGLES_TWO_PARTS_GIVEN_CIRCLE
cpp-python
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 = min ( ans , 2 * abs ( 180 - sum ) ) ; sum -= arr [ l ] ; l ++ ; } ans = min ( ans , 2 * abs ( 180 - sum ) ) ; } ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 = min ( ans , 2 * abs ( 180 - sum ) ) ; sum -= arr [ l ] ; l ++ ; } ans = min ( ans , 2 * abs ( 180 - sum ) )...
cpp-python-COUNT_GFG_SUBSEQUENCES_GIVEN_STRING
cpp-python
void countSubsequence ( char s [ ] , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s [ i ] ) { case 'G' : cntG ++ ; result += C ; break ; case 'F' : cntF ++ ; C += cntG ; break ; default : continue ; } } ...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void countSubsequence ( char s [ ] , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s [ i ] ) { case 'G' : cntG ++ ; result += C ; break ; case 'F' : cntF ++ ; C += cntG ; break ; default : continue ; ...
cpp-python-MAXIMUM_DIFFERENCE_BETWEEN_FREQUENCY_OF_TWO_ELEMENTS_SUCH_THAT_ELEMENT_HAVING_GREATER_FREQUENCY_IS_ALSO_GREATER
cpp-python
int maxdiff ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [ j ] ) ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxdiff ( int arr [ ] , int n ) { unordered_map < int , int > freq ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( freq [ arr [ i ] ] > freq [ arr [ j ] ] && arr [ i ] > arr [...
cpp-python-CHECK_IF_TWO_ARRAYS_ARE_EQUAL_OR_NOT
cpp-python
bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { if ( n != m ) return false ; sort ( arr1 , arr1 + n ) ; sort ( arr2 , arr2 + m ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; }
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 ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool areEqual ( int arr1 [ ] , int arr2 [ ] , int n , int m ) { if ( n != m ) return false ; sort ( arr1 , arr1 + n ) ; sort ( arr2 , arr2 + m ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } </Code>
cpp-python-FRIENDS_PAIRING_PROBLEM_2
cpp-python
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 ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-python-FINDING_POWER_PRIME_NUMBER_P_N
cpp-python
int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; }
def PowerOFPINnfactorial ( n , p ) : ans = 0 temp = p while ( temp <= n ) : ans += n / temp temp = temp * p return ans
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } </Code>
cpp-python-LONGEST_EVEN_LENGTH_SUBSTRING_SUM_FIRST_SECOND_HALF
cpp-python
int findLength ( char * str ) { int n = strlen ( str ) ; 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 ++ ) { leftsum ...
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 ] )...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findLength ( char * str ) { int n = strlen ( str ) ; 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 ++ ) { l...
cpp-python-ANALYSIS_OF_ALGORITHMS_SET_2_ASYMPTOTIC_ANALYSIS
cpp-python
int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; }
def search ( arr , n , x ) : i = 0 for i in range ( i , n ) : if ( arr [ i ] == x ) : return i return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; } </Code>
cpp-python-SUBARRAYSUBSTRING_VS_SUBSEQUENCE_AND_PROGRAMS_TO_GENERATE_THEM
cpp-python
void subArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) cout << arr [ k ] << " " ; cout << endl ; } } }
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 = "" )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void subArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { for ( int k = i ; k <= j ; k ++ ) cout << arr [ k ] << " " ; cout << endl ; } } } </Code>
cpp-python-GIVEN_LARGE_NUMBER_CHECK_SUBSEQUENCE_DIGITS_DIVISIBLE_8_1
cpp-python
bool isSubSeqDivisible ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ 10 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; int arr [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) arr [ i ] = str [ i - 1 ] - '0' ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i ] % 8 ] = 1 ; for ( ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isSubSeqDivisible ( string str ) { int n = str . length ( ) ; int dp [ n + 1 ] [ 10 ] ; memset ( dp , 0 , sizeof ( dp ) ) ; int arr [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) arr [ i ] = str [ i - 1 ] - '0' ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i ] % 8 ] = 1 ; ...
cpp-python-CLUSTERINGPARTITIONING_AN_ARRAY_SUCH_THAT_SUM_OF_SQUARE_DIFFERENCES_IS_MINIMUM
cpp-python
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 = 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 ] ) * ( a [ j...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 = 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 ] ) * ...
cpp-python-SQUARE_ROOT_OF_A_PERFECT_SQUARE
cpp-python
public : float squareRoot ( float n ) { float x = n ; float y = 1 ; float e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; }
def squareRoot ( n ) : x = n y = 1 e = 0.000001 while ( x - y > e ) : x = ( x + y ) / 2 y = n / x return x
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> public : float squareRoot ( float n ) { float x = n ; float y = 1 ; float e = 0.000001 ; while ( x - y > e ) { x = ( x + y ) / 2 ; y = n / x ; } return x ; } </Code>
cpp-python-COUNT_NUMBER_PAIRS_N_B_N_GCD_B_B
cpp-python
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 ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-python-MULTIPLY_AN_INTEGER_WITH_3_5
cpp-python
int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; }
def multiplyWith3Point5 ( x ) : return ( x << 1 ) + x + ( x >> 1 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int multiplyWith3Point5 ( int x ) { return ( x << 1 ) + x + ( x >> 1 ) ; } </Code>
cpp-python-LEXICOGRAPHICALLY_PREVIOUS_PERMUTATION_IN_C
cpp-python
bool prevPermutation ( string & 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 ] , str [ j ] ) ; reverse ( str . begin ( ...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool prevPermutation ( string & 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 ] , str [ j ] ) ; reverse ( str . b...
cpp-python-AREA_OF_A_POLYGON_WITH_GIVEN_N_ORDERED_VERTICES
cpp-python
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 abs ( area / 2.0 ) ; }
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 ) )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 abs ( area / 2.0 ) ; } </Code>
cpp-python-EQUILIBRIUM_INDEX_OF_AN_ARRAY
cpp-python
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 )...
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 : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 == rig...
cpp-python-COUNT_WAYS_DIVIDE_CIRCLE_USING_N_NON_INTERSECTING_CHORDS
cpp-python
int chordCnt ( int A ) { int n = 2 * A ; int dpArray [ n + 1 ] = { 0 } ; 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 ] ) ; } ...
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 ] )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int chordCnt ( int A ) { int n = 2 * A ; int dpArray [ n + 1 ] = { 0 } ; 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 ] ) ; ...
cpp-python-FIND_HARMONIC_MEAN_USING_ARITHMETIC_MEAN_GEOMETRIC_MEAN
cpp-python
double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; }
def compute ( a , b ) : AM = ( a + b ) / 2 GM = math.sqrt ( a * b ) HM = ( GM * GM ) / AM return HM
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } </Code>
cpp-python-MAJORITY_ELEMENT
cpp-python
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 = i ; ...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ...
cpp-python-CHECK_NUMBER_IS_PERFECT_SQUARE_USING_ADDITIONSUBTRACTION
cpp-python
bool isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } </Code>
cpp-python-FIND_THE_SMALLEST_WINDOW_IN_A_STRING_CONTAINING_ALL_CHARACTERS_OF_ANOTHER_STRING
cpp-python
string findSubString ( string str , string pat ) { int len1 = str . length ( ) ; int len2 = pat . length ( ) ; if ( len1 < len2 ) { cout << "No such window exists" ; return "" ; } int hash_pat [ no_of_chars ] = { 0 } ; int hash_str [ no_of_chars ] = { 0 } ; for ( int i = ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> string findSubString ( string str , string pat ) { int len1 = str . length ( ) ; int len2 = pat . length ( ) ; if ( len1 < len2 ) { cout << "No such window exists" ; return "" ; } int hash_pat [ no_of_chars ] = { 0 } ; int hash_str [ no_of_chars ] = { 0 } ; for ( i...
cpp-python-NUMBER_SUBSEQUENCES_STRING_DIVISIBLE_N
cpp-python
int countDivisibleSubseq ( string str , int n ) { int len = str . length ( ) ; int dp [ len ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ ( str [ 0 ] - '0' ) % n ] ++ ; for ( int i = 1 ; i < len ; i ++ ) { dp [ i ] [ ( str [ i ] - '0' ) % n ] ++ ; for ( int j = 0 ; j < n ; j ++ ...
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 ) : ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int countDivisibleSubseq ( string str , int n ) { int len = str . length ( ) ; int dp [ len ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; dp [ 0 ] [ ( str [ 0 ] - '0' ) % n ] ++ ; for ( int i = 1 ; i < len ; i ++ ) { dp [ i ] [ ( str [ i ] - '0' ) % n ] ++ ; for ( int j = 0 ; j < n ; ...
cpp-python-MINIMUM_POSSIBLE_VALUE_AI_AJ_K_GIVEN_ARRAY_K
cpp-python
void pairs ( int arr [ ] , int n , int k ) { int smallest = INT_MAX ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } else if (...
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 ] + ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void pairs ( int arr [ ] , int n , int k ) { int smallest = INT_MAX ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { if ( abs ( arr [ i ] + arr [ j ] - k ) < smallest ) { smallest = abs ( arr [ i ] + arr [ j ] - k ) ; count = 1 ; } el...
cpp-python-ZECKENDORFS_THEOREM_NON_NEIGHBOURING_FIBONACCI_REPRESENTATION
cpp-python
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 ; }
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 ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-python-SHUFFLE_A_DECK_OF_CARDS_3
cpp-python
void shuffle ( int card [ ] , int n ) { srand ( time ( 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int r = i + ( rand ( ) % ( 52 - i ) ) ; swap ( card [ i ] , card [ r ] ) ; } }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void shuffle ( int card [ ] , int n ) { srand ( time ( 0 ) ) ; for ( int i = 0 ; i < n ; i ++ ) { int r = i + ( rand ( ) % ( 52 - i ) ) ; swap ( card [ i ] , card [ r ] ) ; } } </Code>
cpp-python-FIND_THE_LARGEST_SUBARRAY_WITH_0_SUM_1
cpp-python
int maxLen ( int arr [ ] , int n ) { unordered_map < int , int > presum ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; if ( presum . find ( sum ) != presum . end ( )...
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 = ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLen ( int arr [ ] , int n ) { unordered_map < int , int > presum ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( arr [ i ] == 0 && max_len == 0 ) max_len = 1 ; if ( sum == 0 ) max_len = i + 1 ; if ( presum . find ( sum ) != presum . ...
cpp-python-LENGTH_LONGEST_BALANCED_SUBSEQUENCE
cpp-python
int maxLength ( char s [ ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( s [ i ] == '(' && s [ i + 1 ] == ')' ) dp [ i ] [ i + 1 ] = 2 ; for ( int l = 2 ; l < n ; l ++ ) { for ( int i = 0 , j = l ; j < n ; i ++ , j ++ ) { ...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int maxLength ( char s [ ] , int n ) { int dp [ n ] [ n ] ; memset ( dp , 0 , sizeof ( dp ) ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) if ( s [ i ] == '(' && s [ i + 1 ] == ')' ) dp [ i ] [ i + 1 ] = 2 ; for ( int l = 2 ; l < n ; l ++ ) { for ( int i = 0 , j = l ; j < n ; i ++ , j ++ ) ...
cpp-python-PROGRAM_PRINT_SUM_GIVEN_NTH_TERM_1
cpp-python
int summingSeries ( long n ) { return pow ( n , 2 ) ; }
def summingSeries ( n ) : return math.pow ( n , 2 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int summingSeries ( long n ) { return pow ( n , 2 ) ; } </Code>
cpp-python-SUBSET_SUM_PROBLEM_OSUM_SPACE
cpp-python
bool isSubsetSum ( int arr [ ] , int n , int sum ) { bool subset [ 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 ] = false ; else if ( arr [ i - 1 ] <...
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> bool isSubsetSum ( int arr [ ] , int n , int sum ) { bool subset [ 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 ] = false ; else if ( arr [ i ...
cpp-python-FIND_INDEX_OF_AN_EXTRA_ELEMENT_PRESENT_IN_ONE_SORTED_ARRAY
cpp-python
int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; }
def findExtra ( arr1 , arr2 , n ) : for i in range ( 0 , n ) : if ( arr1 [ i ] != arr2 [ i ] ) : return i return n
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return i ; return n ; } </Code>
cpp-python-NUMBER_OF_PERMUTATION_WITH_K_INVERSIONS
cpp-python
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 ] [ K ] =...
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 ] [ ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ] ...
cpp-python-DELETE_CONSECUTIVE_WORDS_SEQUENCE_1
cpp-python
int removeConsecutiveSame ( vector < string > v ) { stack < string > st ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( st . empty ( ) ) st . push ( v [ i ] ) ; else { string str = st . top ( ) ; if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ; else st . push ( v [ i ] ) ; ...
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...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int removeConsecutiveSame ( vector < string > v ) { stack < string > st ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( st . empty ( ) ) st . push ( v [ i ] ) ; else { string str = st . top ( ) ; if ( str . compare ( v [ i ] ) == 0 ) st . pop ( ) ; else st . push ( v [ i ] ...
cpp-python-MINIMUM_INDEX_SUM_COMMON_ELEMENTS_TWO_LISTS
cpp-python
void find ( vector < string > list1 , vector < string > list2 ) { vector < string > res ; 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 ( ) && ( sum - i ) < list2 ....
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 ...
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void find ( vector < string > list1 , vector < string > list2 ) { vector < string > res ; 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 ( ) && ( sum - i ) < ...
cpp-python-NON_REPEATING_ELEMENT
cpp-python
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 ; }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ; } </Code>
cpp-python-HOW_TO_BEGIN_WITH_COMPETITIVE_PROGRAMMING
cpp-python
int search ( int arr [ ] , int n , int x ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; }
def search ( arr , x ) : n = len ( arr ) for j in range ( 0 , n ) : if ( x == arr [ j ] ) : return j return - 1
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> int search ( int arr [ ] , int n , int x ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; } </Code>
cpp-python-SUM_MIDDLE_ROW_COLUMN_MATRIX
cpp-python
void middlesum ( int mat [ ] [ MAX ] , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 ] [ i ] ; cout << "Sum of middle row = " << row_sum << endl ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ n / 2 ] ; cout << "Sum of middle column = " << ...
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 )
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> void middlesum ( int mat [ ] [ MAX ] , int n ) { int row_sum = 0 , col_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) row_sum += mat [ n / 2 ] [ i ] ; cout << "Sum of middle row = " << row_sum << endl ; for ( int i = 0 ; i < n ; i ++ ) col_sum += mat [ i ] [ n / 2 ] ; cout << "Sum of middle column ...
cpp-python-MULTIPLY_LARGE_INTEGERS_UNDER_LARGE_MODULO
cpp-python
long long moduloMultiplication ( long long a , long long b , long long mod ) { long long res = 0 ; a %= mod ; while ( b ) { if ( b & 1 ) res = ( res + a ) % mod ; a = ( 2 * a ) % mod ; b >>= 1 ; } return res ; }
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 ;
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> long long moduloMultiplication ( long long a , long long b , long long mod ) { long long res = 0 ; a %= mod ; while ( b ) { if ( b & 1 ) res = ( res + a ) % mod ; a = ( 2 * a ) % mod ; b >>= 1 ; } return res ; } </Code>
cpp-python-GENERATE_PYTHAGOREAN_TRIPLETS
cpp-python
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 ; printf ( "%d %d %d\n" , a , b , c ) ; } m ++ ; } }
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
Translate the given code from cpp to python. The input Code is marked with <Code> and </Code>.
<Code> 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 ; printf ( "%d %d %d\n" , a , b , c ) ; } m ++ ;...