Dataset Viewer
Auto-converted to Parquet Duplicate
id
stringlengths
11
111
cpp
stringlengths
47
1.49k
java
stringlengths
53
1.66k
python
stringlengths
40
1.56k
ADD_1_TO_A_GIVEN_NUMBER
int addOne ( int x ) { int m = 1; while ( x & m ) { x = x ^ m; m <<= 1; } x = x ^ m; return x; }
static int addOne ( int x ) { int m = 1 ; while ( ( int ) ( x & m ) >= 1 ) { x = x ^ m ; m <<= 1 ; } x = x ^ m ; return x ; }
def addOne ( x ) : m = 1 while ( x & m ) : x = x ^ m m <<= 1 x = x ^ m return x
ADD_1_TO_A_GIVEN_NUMBER_1
int addOne ( int x ) { return ( - ( ~ x ) ); }
static int addOne ( int x ) { return ( - ( ~ x ) ) ; }
def addOne ( x ) : return ( - ( ~ x ) )
ANALYSIS_OF_ALGORITHMS_SET_2_ASYMPTOTIC_ANALYSIS
int search ( int arr [ ], int n, int x ) { int i; for ( i = 0; i < n; i ++ ) { 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 ; }
def search ( arr , n , x ) : i = 0 for i in range ( i , n ) : if ( arr [ i ] == x ) : return i return - 1
AREA_OF_THE_CIRCLE_THAT_HAS_A_SQUARE_AND_A_CIRCLE_INSCRIBED_IN_IT
float getArea ( int a ) { float area = ( M_PI * a * a ) / 4.0; return area; }
static float getArea ( int a ) { float area = ( float ) ( Math . PI * a * a ) / 4 ; return area ; }
def getArea ( a ) : area = ( math.pi * a * a ) / 4 return area
AREA_SQUARE_CIRCUMSCRIBED_CIRCLE
int find_Area ( int r ) { return ( 2 * r * r ); }
static int find_Area ( int r ) { return ( 2 * r * r ) ; }
def find_Area ( r ) : return ( 2 * r * r )
ARRAY_ELEMENT_MOVED_K_USING_SINGLE_MOVES
int winner ( int a [ ], int n, int k ) { if ( k >= n - 1 ) return n; int best = 0, times = 0; for ( int i = 0; i < n; i ++ ) { if ( a [ i ] > best ) { best = a [ i ]; if ( i ) times = 1; } else times += 1; if ( times >= k ) return best; } return best; }
static int winner ( int a [ ] , int n , int k ) { if ( k >= n - 1 ) return n ; int best = 0 , times = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] > best ) { best = a [ i ] ; if ( i >= 1 ) times = 1 ; } else times += 1 ; if ( times >= k ) return best ; } return best ; }
def winner ( a , n , k ) : if k >= n - 1 : return n best = 0 times = 0 for i in range ( n ) : if a [ i ] > best : best = a [ i ] if i == True : times = 1 else : times += 1 if times >= k : return best retu...
ARRAY_RANGE_QUERIES_ELEMENTS_FREQUENCY_VALUE
int solveQuery ( int start, int end, int arr [ ] ) { unordered_map < int, int > frequency; for ( int i = start; i <= end; i ++ ) frequency [ arr [ i ] ] ++; int count = 0; for ( auto x : frequency ) if ( x . first == x . second ) count ++; return count; }
static int solveQuery ( int start , int end , int arr [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = start ; i <= end ; i ++ ) mp . put ( arr [ i ] , mp . get ( arr [ i ] ) == null ? 1 : mp . get ( arr [ i ] ) + 1 ) ; int count = 0 ; for ( Map . Entry < Integer , Integer > entry ...
def solveQuery ( start , end , arr ) : frequency = dict ( ) for i in range ( start , end + 1 ) : if arr [ i ] in frequency.keys ( ) : frequency [ arr [ i ] ] += 1 else : frequency [ arr [ i ] ] = 1 count = 0 for x in frequency : if x == frequency [ x ] : ...
BASIC_AND_EXTENDED_EUCLIDEAN_ALGORITHMS
int gcd ( int a, int b ) { if ( a == 0 ) return b; return gcd ( b % a, a ); }
public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; }
def gcd ( a , b ) : if a == 0 : return b return gcd ( b % a , a )
BINARY_SEARCH
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 ( arr [ mid ] > x ) return binarySearch ( arr, l, mid - 1, x ); return binarySearch ( arr, mid + 1, r, x ); } return - 1; }
static 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 ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; }
def binarySearch ( arr , l , r , x ) : if r >= l : mid = l + ( r - l ) // 2 if arr [ mid ] == x : return mid elif arr [ mid ] > x : return binarySearch ( arr , l , mid - 1 , x ) else : return binarySearch ( arr , mid + 1 , r , x ) else : ...
BREAK_NUMBER_THREE_PARTS
long count_of_ways ( long n ) { long count = 0; for ( int i = 0; i <= n; i ++ ) for ( int j = 0; j <= n; j ++ ) for ( int k = 0; k <= n; k ++ ) if ( i + j + k == n ) count ++; return count; }
static long count_of_ways ( long n ) { long count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) for ( int k = 0 ; k <= n ; k ++ ) if ( i + j + k == n ) count ++ ; return count ; }
def count_of_ways ( n ) : count = 0 for i in range ( 0 , n + 1 ) : for j in range ( 0 , n + 1 ) : for k in range ( 0 , n + 1 ) : if ( i + j + k == n ) : count = count + 1 return count
BREAK_NUMBER_THREE_PARTS_1
long long count_of_ways ( long long n ) { long long count; 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 ; }
def count_of_ways ( n ) : count = 0 count = ( n + 1 ) * ( n + 2 ) // 2 return count
CAESAR_CIPHER
string encrypt ( string text, int s ) { string result = ""; for ( int i = 0; i < text . length ( ); i ++ ) { if ( isupper ( text [ i ] ) ) result += char ( int ( text [ i ] + s - 65 ) % 26 + 65 ); else result += char ( int ( text [ i ] + s - 97 ) % 26 + 97 ); } return result; }
public static StringBuffer encrypt ( String text , int s ) { StringBuffer result = new StringBuffer ( ) ; for ( int i = 0 ; i < text . length ( ) ; i ++ ) { if ( Character . isUpperCase ( text . charAt ( i ) ) ) { char ch = ( char ) ( ( ( int ) text . charAt ( i ) + s - 65 ) % 26 + 65 ) ; result...
def encrypt ( text , s ) : result = "" for i in range ( len ( text ) ) : char = text [ i ] if ( char.isupper ( ) ) : result += chr ( ( ord ( char ) + s - 65 ) % 26 + 65 ) else : result += chr ( ( ord ( char ) + s - 97 ) % 26 + 97 ) return result
CALCULATE_ANGLE_HOUR_HAND_MINUTE_HAND
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 ); retur...
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...
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 = int ( 0.5 * ( h * 60 + m ) ) minute_angle = int ( 6 * m ) angle = abs ( hour_angle - minute_angle ) angle = min ( 3...
CALCULATE_MAXIMUM_VALUE_USING_SIGN_TWO_NUMBERS_STRING
int calcMaxValue ( string str ) { int res = str [ 0 ] - '0'; for ( int i = 1; i < str . length ( ); i ++ ) { if ( str [ i ] == '0' || str [ i ] == '1' || res < 2 ) res += ( str [ i ] - '0' ); else res += ( str [ i ] - '0' ); } return res; }
static int calcMaxValue ( String str ) { int res = str . charAt ( 0 ) - '0' ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == '0' || str . charAt ( i ) == '1' || res < 2 ) res += ( str . charAt ( i ) - '0' ) ; else res += ( str . charAt ( i ) - '0' ) ; } return res ; }
def calcMaxValue ( str ) : res = ord ( str [ 0 ] ) - 48 for i in range ( 1 , len ( str ) ) : if ( str [ i ] == '0' or str [ i ] == '1' or res < 2 ) : res += ord ( str [ i ] ) - 48 else : res += ord ( str [ i ] ) - 48 return res
CALCULATE_SUM_OF_ALL_NUMBERS_PRESENT_IN_A_STRING
int findSum ( string str ) { string temp = ""; int sum = 0; for ( char ch : str ) { if ( isdigit ( ch ) ) temp += ch; else { sum += atoi ( temp . c_str ( ) ); temp = ""; } } return sum + atoi ( temp . c_str ( ) ); }
static int findSum ( String str ) { String temp = "0" ; int sum = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( Character . isDigit ( ch ) ) temp += ch ; else { sum += Integer . parseInt ( temp ) ; temp = "0" ; ...
def findSum ( str ) : temp = "0" Sum = 0 for ch in str : if ( ch.isdigit ( ) ) : temp += ch else : Sum += int ( temp ) temp = "0" return Sum + int ( temp )
CALCULATE_VOLUME_DODECAHEDRON
double vol_of_dodecahedron ( int side ) { return ( ( ( 15 + ( 7 * ( sqrt ( 5 ) ) ) ) / 4 ) * ( pow ( side, 3 ) ) ); }
static double vol_of_dodecahedron ( int side ) { return ( ( ( 15 + ( 7 * ( Math . sqrt ( 5 ) ) ) ) / 4 ) * ( Math . pow ( side , 3 ) ) ) ; }
def vol_of_dodecahedron ( side ) : return ( ( ( 15 + ( 7 * ( math.sqrt ( 5 ) ) ) ) / 4 ) * ( math.pow ( side , 3 ) ) )
CASSINIS_IDENTITY
int cassini ( int n ) { return ( n & 1 ) ? - 1 : 1; }
static int cassini ( int n ) { return ( n & 1 ) != 0 ? - 1 : 1 ; }
def cassini ( n ) : return - 1 if ( n & 1 ) else 1
CEILING_IN_A_SORTED_ARRAY_1
int ceilSearch ( int arr [ ], int low, int high, int x ) { int mid; if ( x <= arr [ low ] ) return low; if ( x > arr [ high ] ) return - 1; mid = ( low + high ) / 2; if ( arr [ mid ] == x ) return mid; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid + 1; else ...
static int ceilSearch ( int arr [ ] , int low , int high , int x ) { int mid ; if ( x <= arr [ low ] ) return low ; if ( x > arr [ high ] ) return - 1 ; mid = ( low + high ) / 2 ; if ( arr [ mid ] == x ) return mid ; else if ( arr [ mid ] < x ) { if ( mid + 1 <= high && x <= arr [ mid + 1 ] ) return mid...
def ceilSearch ( arr , low , high , x ) : if x <= arr [ low ] : return low if x > arr [ high ] : return - 1 mid = ( low + high ) / 2 if arr [ mid ] == x : return mid elif arr [ mid ] < x : if mid + 1 <= high and x <= arr [ mid + 1 ] : return mid + 1 ...
CENTER_ELEMENT_OF_MATRIX_EQUALS_SUMS_OF_HALF_DIAGONALS
bool HalfDiagonalSums ( vector<vector<int>> mat, int n ) { int diag1_left = 0, diag1_right = 0; int diag2_left = 0, diag2_right = 0; for ( int i = 0, j = n - 1; i < n; i ++, j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ]; diag2_left += mat [ j ] [ i ]; } else if ( i > n / 2 ...
static boolean HalfDiagonalSums ( int mat [ ] [ ] , int n ) { int diag1_left = 0 , diag1_right = 0 ; int diag2_left = 0 , diag2_right = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { if ( i < n / 2 ) { diag1_left += mat [ i ] [ i ] ; diag2_left += mat [ j ] [ i ] ; } else if...
def HalfDiagonalSums ( mat , n ) : diag1_left = 0 diag1_right = 0 diag2_left = 0 diag2_right = 0 i = 0 j = n - 1 while i < n : if ( i < n // 2 ) : diag1_left += mat [ i ] [ i ] diag2_left += mat [ j ] [ i ] elif ( i > n // 2 ) : diag1_right...
CHANGE_BITS_CAN_MADE_ONE_FLIP
bool canMakeAllSame ( string str ) { int zeros = 0, ones = 0; for ( char ch : str ) ( ch == '0' ) ? ++ zeros : ++ ones; return ( zeros == 1 || ones == 1 ); }
static boolean canMakeAllSame ( String str ) { int zeros = 0 , ones = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch == '0' ) ++ zeros ; else ++ ones ; } return ( zeros == 1 || ones == 1 ) ; }
def canMakeAllSame ( str ) : zeros = 0 ones = 0 for i in range ( 0 , len ( str ) ) : ch = str [ i ] if ( ch == '0' ) : zeros = zeros + 1 else : ones = ones + 1 return ( zeros == 1 or ones == 1 )
CHANGE_BITS_CAN_MADE_ONE_FLIP_1
bool isOneFlip ( string str ) { int sum = 0; int n = str . length ( ); for ( int i = 0; i < n; i ++ ) sum += str [ i ] - '0'; return ( sum == n - 1 || sum == 1 ); }
static boolean isOneFlip ( String str ) { int sum = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) sum += str . charAt ( i ) - '0' ; return ( sum == n - 1 || sum == 1 ) ; }
def isOneFlip ( str ) : sum = 0 n = len ( str ) for i in range ( 0 , n ) : sum += ord ( str [ i ] ) - ord ( '0' ) return ( sum == n - 1 or sum == 1 )
CHECK_ARRAY_MAJORITY_ELEMENT
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; }
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...
def isMajority ( a, n ) : mp = { } for i in range(n) : if a[i] in mp : mp [ a[i] ] += 1 else : mp [ a[i] ] = 1 for x in mp : if mp [ x ] >= len ( a ) // 2 : return True return False
CHECK_ARRAY_REPRESENTS_INORDER_BINARY_SEARCH_TREE_NOT
bool isInorder ( int arr [ ], int n ) { if ( n == 0 || n == 1 ) return true; for ( int i = 1; i < n; i ++ ) if ( arr [ i - 1 ] > arr [ i ] ) return false; return true; }
static boolean isInorder ( int [ ] arr , int n ) { if ( n == 0 || n == 1 ) { return true ; } for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i - 1 ] > arr [ i ] ) { return false ; } } return true ; }
def isInorder ( arr , n ) : if ( n == 0 or n == 1 ) : return True for i in range ( 1 , n , 1 ) : if ( arr [ i - 1 ] > arr [ i ] ) : return False return True
CHECK_DIVISIBILITY_BINARY_STRING_2K
bool isDivisible ( char str [ ], int k ) { int n = strlen ( str ); int c = 0; for ( int i = 0; i < k; i ++ ) if ( str [ n - i - 1 ] == '0' ) c ++; return ( c == k ); }
static boolean isDivisible ( String str , int k ) { int n = str . length ( ) ; int c = 0 ; for ( int i = 0 ; i < k ; i ++ ) if ( str . charAt ( n - i - 1 ) == '0' ) c ++ ; return ( c == k ) ; }
def isDivisible ( str , k ) : n = len ( str ) c = 0 for i in range ( 0 , k ) : if ( str [ n - i - 1 ] == '0' ) : c += 1 return ( c == k )
CHECK_DIVISIBILITY_LARGE_NUMBER_999
bool isDivisible999 ( string num ) { int n = num . length ( ); if ( n == 0 && num [ 0 ] == '0' ) return true; if ( n % 3 == 1 ) num = "00" + num; if ( n % 3 == 2 ) num = "0" + num; int gSum = 0; for ( int i = 0; i < n; i ++ ) { int group = 0; group += ( num [ i ++ ] - '0' ) * 100; group += (...
static boolean isDivisible999 ( String num ) { int n = num . length ( ) ; if ( n == 0 && num . charAt ( 0 ) == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num . charAt ( ...
def isDivisible999 ( num ) : n = len ( num ) if ( n == 0 and num [ 0 ] == '0' ) : return True if ( ( n % 3 ) == 1 ) : num = "00" + num if ( ( n % 3 ) == 2 ) : num = "0" + num gSum = 0 for i in range ( 0 , n , 3 ) : group = 0 group += ( ord ( num [ i ] ) - ...
CHECK_GIVEN_CIRCLE_LIES_COMPLETELY_INSIDE_RING_FORMED_TWO_CONCENTRIC_CIRCLES
bool checkcircle ( int r, int R, int r1, int x1, int y1 ) { int dis = sqrt ( x1 * x1 + y1 * y1 ); return ( dis - r1 >= R && dis + r1 <= r ); }
public static boolean checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = ( int ) Math . sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; }
def checkcircle ( r , R , r1 , x1 , y1 ) : dis = int ( math.sqrt ( x1 * x1 + y1 * y1 ) ) return ( dis - r1 >= R and dis + r1 <= r )
CHECK_GIVEN_STRING_CAN_SPLIT_FOUR_DISTINCT_STRINGS
bool check ( string s ) { if ( s . size ( ) >= 10 ) return true; for ( int i = 1; i < s . size ( ); i ++ ) { for ( int j = i + 1; j < s . size ( ); j ++ ) { for ( int k = j + 1; k < s . size ( ); k ++ ) { string s1 = s . substr ( 0, i ); string s2 = s . substr ( i, ...
public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { ...
def check ( s ) : if ( len ( s ) >= 10 ) : return True for i in range ( 1 , len ( s ) ) : for j in range ( i + 1 , len ( s ) ) : for k in range ( j + 1 , len ( s ) ) : s1 = s [ 0 : i ] s2 = s [ i : j ] s3 = s [ j : k ] s...
CHECK_GIVEN_STRING_ROTATION_PALINDROME
bool isPalindrome ( string str ) { int l = 0; int h = str . length ( ) - 1; while ( h > l ) if ( str [ l ++ ] != str [ h -- ] ) return false; return true; }
static boolean isPalindrome ( String str ) { int l = 0 ; int h = str . length ( ) - 1 ; while ( h > l ) if ( str . charAt ( l ++ ) != str . charAt ( h -- ) ) return false ; return true ; }
def isPalindrome ( string ) : l = 0 h = len ( string ) - 1 while h > l : l += 1 h -= 1 if string [ l - 1 ] != string [ h + 1 ] : return False return True
CHECK_IF_ALL_THE_ELEMENTS_CAN_BE_MADE_OF_SAME_PARITY_BY_INVERTING_ADJACENT_ELEMENTS
bool flipsPossible ( int a [ ], int n ) { int count_odd = 0, count_even = 0; for ( int i = 0; i < n; i ++ ) { if ( a [ i ] & 1 ) count_odd ++; else count_even ++; } if ( count_odd % 2 && count_even % 2 ) return false; else return true; }
static boolean flipsPossible ( int [ ] a , int n ) { int count_odd = 0 , count_even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( a [ i ] & 1 ) == 1 ) count_odd ++ ; else count_even ++ ; } if ( count_odd % 2 == 1 && count_even % 2 == 1 ) return false ; else return true ; }
def flipsPossible ( a , n ) : count_odd = 0 count_even = 0 for i in range ( n ) : if ( a [ i ] & 1 ) : count_odd += 1 else : count_even += 1 if ( count_odd % 2 and count_even % 2 ) : return False else : return True
CHECK_IF_ARRAY_ELEMENTS_ARE_CONSECUTIVE
bool areConsecutive ( int arr [ ], int n ) { if ( n < 1 ) return false; int min = getMin ( arr, n ); int max = getMax ( arr, n ); if ( max - min + 1 == n ) { bool * visited = ( bool * ) calloc ( n, sizeof ( bool ) ); int i; for ( i = 0; i < n; i ++ ) { if ( visited [ arr [ i ] - min ] ...
static boolean areConsecutive ( int arr [ ] , int n ) { if ( n < 1 ) return false ; int min = getMin ( arr , n ) ; int max = getMax ( arr , n ) ; if ( max - min + 1 == n ) { boolean visited [ ] = new boolean [ n ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( visited [ arr [ i ] - min ...
def areConsecutive ( arr , n ) : if ( n < 1 ) : return False Min = min ( arr ) Max = max ( arr ) if ( Max - Min + 1 == n ) : visited = [ False for i in range ( n ) ] for i in range ( n ) : if ( visited [ arr [ i ] - Min ] != False ) : return False ...
CHECK_IF_A_GIVEN_ARRAY_CAN_REPRESENT_PREORDER_TRAVERSAL_OF_BINARY_SEARCH_TREE
bool canRepresentBST ( int pre [ ], int n ) { stack < int > s; int root = INT_MIN; for ( int i = 0; i < n; i ++ ) { if ( pre [ i ] < root ) return false; while ( ! s . empty ( ) && s . top ( ) < pre [ i ] ) { root = s . top ( ); s . pop ( ); } s . push ( pre [ i ] ); } return t...
static boolean canRepresentBST ( int pre [ ] , int n ) { Stack < Integer > s = new Stack < Integer > ( ) ; int root = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( pre [ i ] < root ) { return false ; } while ( ! s . empty ( ) && s . peek ( ) < pre [ i ] ) { root = s . p...
def canRepresentBST ( pre, n ) : s = [ ] root = -2147483648 for i in range(n) : if pre[i] < root : return False while ( len ( s ) > 0 and s [ - 1 ] < pre[i] ) : root = s.pop ( ) s.append ( pre[i] ) return True
CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER
bool isPower ( int x, int y ) { if ( x == 1 ) return ( y == 1 ); long int pow = 1; while ( pow < y ) pow *= x; return ( pow == y ); }
public static boolean isPower ( int x , int y ) { if ( x == 1 ) return ( y == 1 ) ; int pow = 1 ; while ( pow < y ) pow = pow * x ; return ( pow == y ) ; }
def isPower ( x , y ) : if ( x == 1 ) : return ( y == 1 ) pow = 1 while ( pow < y ) : pow = pow * x return ( pow == y )
CHECK_IF_A_NUMBER_IS_POWER_OF_ANOTHER_NUMBER_1
bool isPower ( int x, int y ) { int res1 = log ( y ) / log ( x ); double res2 = log ( y ) / log ( x ); return ( res1 == res2 ); }
static boolean isPower ( int x , int y ) { int res1 = (int) (Math . log ( y ) / Math . log ( x )); double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; }
def isPower ( x , y ) : res1 = int ( math.log ( y ) / math.log ( x ) ) res2 = math.log ( y ) / math.log ( x ) return 1 if ( res1 == res2 ) else 0
CHECK_IF_STRING_REMAINS_PALINDROME_AFTER_REMOVING_GIVEN_NUMBER_OF_CHARACTERS
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 ; }
def isPossible ( str , n ) : l = len ( str ) if ( l >= n ) : return True return False
CHECK_IF_X_CAN_GIVE_CHANGE_TO_EVERY_PERSON_IN_THE_QUEUE
int isChangeable ( int notes [ ], int n ) { int fiveCount = 0; int tenCount = 0; for ( int i = 0; i < n; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount --; tenCount ++; } else return 0; } else { ...
static int isChangeable ( int notes [ ] , int n ) { int fiveCount = 0 ; int tenCount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( notes [ i ] == 5 ) fiveCount ++ ; else if ( notes [ i ] == 10 ) { if ( fiveCount > 0 ) { fiveCount -- ; tenCount ++ ; } else return 0 ; ...
def isChangeable ( notes , n ) : fiveCount = 0 tenCount = 0 for i in range ( n ) : if ( notes [ i ] == 5 ) : fiveCount += 1 elif ( notes [ i ] == 10 ) : if ( fiveCount > 0 ) : fiveCount -= 1 tenCount += 1 else : ...
CHECK_INTEGER_OVERFLOW_MULTIPLICATION
bool isOverflow ( long long a, long long b ) { if ( a == 0 || b == 0 ) return false; long long result = a * b; if ( a == result / b ) return false; else return true; }
static Boolean isOverflow ( long a , long b ) { if ( a == 0 || b == 0 ) return false ; long result = a * b ; if ( a == result / b ) return false ; else return true ; }
def isOverflow ( a , b ) : if ( a == 0 or b == 0 ) : return False result = a * b if ( result >= 9223372036854775807 or result <= - 9223372036854775808 ) : result = 0 if ( a == ( result // b ) ) : return False else : return True
CHECK_LARGE_NUMBER_DIVISIBLE_11_NOT
int check ( string str ) { int n = str . length ( ); int oddDigSum = 0, evenDigSum = 0; for ( int i = 0; i < n; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str [ i ] - '0' ); else evenDigSum += ( str [ i ] - '0' ); } return ( ( oddDigSum - evenDigSum ) % 11 == 0 ); }
static boolean check ( String str ) { int n = str . length ( ) ; int oddDigSum = 0 , evenDigSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) oddDigSum += ( str . charAt ( i ) - '0' ) ; else evenDigSum += ( str . charAt ( i ) - '0' ) ; } return ( ( oddDigSum - evenDigSum ) % 11 == 0 )...
def check ( str ) : n = len ( str ) oddDigSum = 0 evenDigSum = 0 for i in range ( 0 , n ) : if ( i % 2 == 0 ) : oddDigSum = oddDigSum + ( ord ( str [ i ] ) - 48 ) else : evenDigSum = evenDigSum + ( ord ( str [ i ] ) - 48 ) return ( ( oddDigSum - evenDigSum ) %...
CHECK_LARGE_NUMBER_DIVISIBLE_13_NOT
bool checkDivisibility ( string num ) { int length = num . size ( ); if ( length == 1 && num [ 0 ] == '0' ) return true; if ( length % 3 == 1 ) { num += "00"; length += 2; } else if ( length % 3 == 2 ) { num += "0"; length += 1; } int sum = 0, p = 1; for ( int i = length - 1; i >= 0; ...
static boolean checkDivisibility ( String num ) { int length = num . length ( ) ; if ( length == 1 && num . charAt ( 0 ) == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( in...
def checkDivisibility ( num ) : length = len ( num ) if ( length == 1 and num [ 0 ] == '0' ) : return True if ( length % 3 == 1 ) : num = str ( num ) + "00" length += 2 elif ( length % 3 == 2 ) : num = str ( num ) + "0" length += 1 sum = 0 p = 1 for i ...
CHECK_LARGE_NUMBER_DIVISIBLE_4_NOT
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 ) ; }
def check ( str ) : n = len ( str ) if ( n == 0 ) : return False if ( n == 1 ) : return ( ( str [ 0 ] - '0' ) % 4 == 0 ) last = ( int ) ( str [ n - 1 ] ) second_last = ( int ) ( str [ n - 2 ] ) return ( ( second_last * 10 + last ) % 4 == 0 )
CHECK_LARGE_NUMBER_DIVISIBLE_9_NOT
int check ( string str ) { int n = str . length ( ); int digitSum = 0; for ( int i = 0; i < n; i ++ ) digitSum += ( str [ i ] - '0' ); return ( digitSum % 9 == 0 ); }
static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 9 == 0 ) ; }
def check ( str ) : n = len ( str ) digitSum = 0 for i in range ( 0 , n ) : digitSum = digitSum + ord ( str [ i ] ) - 48 return ( digitSum % 9 == 0 )
CHECK_NUMBER_IS_PERFECT_SQUARE_USING_ADDITIONSUBTRACTION
bool isPerfectSquare ( int n ) { for ( int sum = 0, i = 1; sum < n; i += 2 ) { sum += i; if ( sum == n ) return true; } 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 ; }
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
CHECK_POSSIBLE_TRANSFORM_ONE_STRING_ANOTHER
bool check ( string s1, string s2 ) { int n = s1 . length ( ); int m = s2 . length ( ); bool dp [ n + 1 ] [ m + 1 ]; for ( int i = 0; i <= n; i ++ ) { for ( int j = 0; j <= m; j ++ ) { dp [ i ] [ j ] = false; } } dp [ 0 ] [ 0 ] = true; for ( int i = 0; i < s1 . length ( ); i ...
static boolean check ( String s1 , String s2 ) { int n = s1 . length ( ) ; int m = s2 . length ( ) ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= m ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; ...
def check ( s1 , s2 ) : n = len ( s1 ) m = len ( s2 ) dp = ( [ [ False for i in range ( m + 1 ) ] for i in range ( n + 1 ) ] ) dp [ 0 ] [ 0 ] = True for i in range ( len ( s1 ) ) : for j in range ( len ( s2 ) + 1 ) : if ( dp [ i ] [ j ] ) : if ( ( j < len ( s2 ) a...
CHECK_REVERSING_SUB_ARRAY_MAKE_ARRAY_SORTED_1
bool checkReverse ( int arr [ ], int n ) { if ( n == 1 ) return true; int i; for ( i = 1; i < n && arr [ i - 1 ] < arr [ i ]; i ++ ); if ( i == n ) return true; int j = i ++ ; while ( arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) return false; j ++; } if ( j == n ) ...
static boolean checkReverse ( int arr [ ] , int n ) { if ( n == 1 ) { return true ; } int i ; for ( i = 1 ; arr [ i - 1 ] < arr [ i ] && i < n ; i ++ ) ; if ( i == n ) { return true ; } int j = i ++ ; while ( arr [ j ] < arr [ j - 1 ] ) { if ( i > 1 && arr [ j ] < arr [ i - 2 ] ) { ...
def checkReverse ( arr , n ) : if ( n == 1 ) : return True i = 1 for i in range ( 1 , n ) : if arr [ i - 1 ] >= arr [ i ] : break else: if ( i == n ) : return True j = i i += 1 while ( arr [ j ] < arr [ j - 1 ] ) : if ( i > ...
CHECK_STRING_CAN_OBTAINED_ROTATING_ANOTHER_STRING_2_PLACES
bool isRotated ( string str1, string str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false; string clock_rot = ""; string anticlock_rot = ""; int len = str2 . length ( ); anticlock_rot = anticlock_rot + str2 . substr ( len - 2, 2 ) + str2 . substr ( 0, len - 2 ); clock_rot = clock_rot + str2 ....
static boolean isRotated ( String str1 , String str2 ) { if ( str1 . length ( ) != str2 . length ( ) ) return false ; String clock_rot = "" ; String anticlock_rot = "" ; int len = str2 . length ( ) ; anticlock_rot = anticlock_rot + str2 . substring ( len - 2 , len ) + str2 . substring ( 0 , len - 2 ) ; cloc...
def isRotated ( str1 , str2 ) : if ( len ( str1 ) != len ( str2 ) ) : return False clock_rot = "" anticlock_rot = "" l = len ( str2 ) anticlock_rot = ( anticlock_rot + str2 [ l - 2 : ] + str2 [ 0 : l - 2 ] ) clock_rot = clock_rot + str2 [ 2 : ] + str2 [ 0 : 2 ] return ( str1 == clock...
CHECK_STRING_FOLLOWS_ANBN_PATTERN_NOT
bool isAnBn ( string str ) { int n = str . length ( ); int i; for ( i = 0; i < n; i ++ ) if ( str [ i ] != 'a' ) break; if ( i * 2 != n ) return false; int j; for ( j = i; j < n; j ++ ) if ( str [ j ] != 'b' ) return false; return true; }
public static boolean isAnBn ( String s ) { int l = s . length ( ) ; if ( l % 2 == 1 ) { return false ; } int i = 0 ; int j = l - 1 ; while ( i < j ) { if ( s . charAt ( i ) != 'a' || s . charAt ( j ) != 'b' ) { return false ; } i ++ ; j -- ; } return true ; }
def isAnBn ( s ) : n = len ( s ) for i in range ( n ) : if ( s [ i ] != 'a' ) : break if ( i * 2 != n ) : return False for j in range ( i , n ) : if ( s [ j ] != 'b' ) : return False return True
CHECK_SUMS_TH_ROW_TH_COLUMN_MATRIX
bool areSumSame ( int a [ ] [ MAX ], int n, int m ) { int sum1 = 0, sum2 = 0; for ( int i = 0; i < n; i ++ ) { sum1 = 0, sum2 = 0; for ( int j = 0; j < m; j ++ ) { sum1 += a [ i ] [ j ]; sum2 += a [ j ] [ i ]; } if ( sum1 == sum2 ) return true; } return false; }
static boolean areSumSame ( int a [ ] [ ] , int n , int m ) { int sum1 = 0 , sum2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum1 = 0 ; sum2 = 0 ; for ( int j = 0 ; j < m ; j ++ ) { sum1 += a [ i ] [ j ] ; sum2 += a [ j ] [ i ] ; } if ( sum1 == sum2 ) return true ; } re...
def areSumSame ( a , n , m ) : sum1 = 0 sum2 = 0 for i in range ( 0 , n ) : sum1 = 0 sum2 = 0 for j in range ( 0 , m ) : sum1 += a [ i ] [ j ] sum2 += a [ j ] [ i ] if ( sum1 == sum2 ) : return 1 return 0
CHECK_TWO_GIVEN_CIRCLES_TOUCH_INTERSECT
int circle ( int x1, int y1, int x2, int y2, int r1, int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ); int radSumSq = ( r1 + r2 ) * ( r1 + r2 ); if ( distSq == radSumSq ) return 1; else if ( distSq > radSumSq ) return - 1; else return 0; }
static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; }
def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) radSumSq = ( r1 + r2 ) * ( r1 + r2 ) if ( distSq == radSumSq ) : return 1 elif ( distSq > radSumSq ) : return - 1 else : return 0
CHECK_VALID_SEQUENCE_DIVISIBLE_M_1
int isPossible ( int n, int index, int modulo, int M, int arr [ ], int dp [ ] [ MAX ] ) { modulo = ( ( modulo % M ) + M ) % M; if ( index == n ) { if ( modulo == 0 ) return 1; return 0; } if ( dp [ index ] [ modulo ] != - 1 ) return dp [ index ] [ modulo ]; int placeAdd = isPossible ( n, index + 1, mo...
static int isPossible ( int n , int index , int modulo , int M , int arr [ ] , int dp [ ] [ ] ) { modulo = ( ( modulo % M ) + M ) % M ; if ( index == n ) { if ( modulo == 0 ) { return 1 ; } return 0 ; } if ( dp [ index ] [ modulo ] != - 1 ) { return dp [ index ] [ modulo ] ; } int plac...
def isPossible ( n , index , modulo , M , arr , dp ) : modulo = ( ( modulo % M ) + M ) % M if ( index == n ) : if ( modulo == 0 ) : return 1 return 0 if ( dp [ index ] [ modulo ] != - 1 ) : return dp [ index ] [ modulo ] placeAdd = isPossible ( n , index + 1 , modulo ...
CHECK_WHETHER_ARITHMETIC_PROGRESSION_CAN_FORMED_GIVEN_ARRAY
bool checkIsAP ( int arr [ ], int n ) { if ( n == 1 ) return true; sort ( arr, arr + n ); int d = arr [ 1 ] - arr [ 0 ]; for ( int i = 2; i < n; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false; return true; }
static boolean checkIsAP ( int arr [ ] , int n ) { if ( n == 1 ) return true ; Arrays . sort ( arr ) ; int d = arr [ 1 ] - arr [ 0 ] ; for ( int i = 2 ; i < n ; i ++ ) if ( arr [ i ] - arr [ i - 1 ] != d ) return false ; return true ; }
def checkIsAP ( arr , n ) : if ( n == 1 ) : return True arr.sort ( ) d = arr [ 1 ] - arr [ 0 ] for i in range ( 2 , n ) : if ( arr [ i ] - arr [ i - 1 ] != d ) : return False return True
CHECK_WHETHER_GIVEN_DEGREES_VERTICES_REPRESENT_GRAPH_TREE
bool check ( int degree [ ], int n ) { int deg_sum = 0; for ( int i = 0; i < n; i ++ ) deg_sum += degree [ i ]; return ( 2 * ( n - 1 ) == deg_sum ); }
static boolean check ( int degree [ ] , int n ) { int deg_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { deg_sum += degree [ i ] ; } return ( 2 * ( n - 1 ) == deg_sum ) ; }
def check ( degree , n ) : deg_sum = 0 for i in range(n): deg_sum += degree[i] if ( 2 * ( n - 1 ) == deg_sum ) : return True else : return False
CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD
bool isEven ( int n ) { return ( n % 2 == 0 ); }
public static boolean isEven ( int n ) { return ( n % 2 == 0 ) ; }
def isEven ( n ) : return ( n % 2 == 0 )
CHECK_WHETHER_GIVEN_NUMBER_EVEN_ODD_1
bool isEven ( int n ) { return ( ! ( n & 1 ) ); }
public static boolean isEven ( int n ) { if ( ( n & 1 ) == 0 ) return true ; else return false ; }
def isEven ( n ) : return ( not ( n & 1 ) )
CHECK_WHETHER_LARGE_NUMBER_DIVISIBLE_7
int isdivisible7 ( char num [ ] ) { int n = strlen ( num ), gSum; if ( n == 0 && num [ 0 ] == '\n' ) return 1; if ( n % 3 == 1 ) { strcat ( num, "00" ); n += 2; } else if ( n % 3 == 2 ) { strcat ( num, "0" ); n ++; } int i, GSum = 0, p = 1; for ( i = n - 1; i >= 0; i -- ) { int g...
static boolean isDivisible7 ( String num ) { int n = num . length ( ) ; if ( n == 0 && num . charAt ( 0 ) == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; n = num . length ( ) ; int gSum = 0 , p = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int group ...
def isdivisible7 ( num ) : n = len ( num ) if ( n == 0 and num [ 0 ] == '\n' ) : return 1 if ( n % 3 == 1 ) : num = str ( num ) + "00" n += 2 elif ( n % 3 == 2 ) : num = str ( num ) + "0" n += 1 GSum = 0 p = 1 for i in range ( n - 1 , - 1 , - 1 ) : ...
CHECK_WHETHER_TRIANGLE_VALID_NOT_SIDES_GIVEN
bool checkValidity ( int a, int b, int c ) { if ( a + b <= c || a + c <= b || b + c <= a ) return false; else return true; }
public static int checkValidity ( int a , int b , int c ) { if ( a + b <= c || a + c <= b || b + c <= a ) return 0 ; else return 1 ; }
def checkValidity ( a , b , c ) : if ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) : return False else : return True
CIRCULAR_MATRIX_CONSTRUCT_A_MATRIX_WITH_NUMBERS_1_TO_MN_IN_SPIRAL_WAY
void spiralFill ( int m, int n, int a [ ] [ MAX ] ) { int val = 1; int k = 0, l = 0; while ( k < m && l < n ) { for ( int i = l; i < n; ++ i ) a [ k ] [ i ] = val ++; k ++; for ( int i = k; i < m; ++ i ) a [ i ] [ n - 1 ] = val ++; n --; if ( k < m ) { for ( int i = n - 1...
static void spiralFill ( int m , int n , int a [ ] [ ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = val ++ ; } n -- ; ...
def spiralFill ( m , n , a ) : val = 1 k , l = 0 , 0 while ( k < m and l < n ) : for i in range ( l , n ) : a [ k ] [ i ] = val val += 1 k += 1 for i in range ( k , m ) : a [ i ] [ n - 1 ] = val val += 1 n -= 1 if ( k < ...
COMPUTE_AVERAGE_TWO_NUMBERS_WITHOUT_OVERFLOW
int compute_average ( int a, int b ) { return ( a + b ) / 2; }
static int compute_average ( int a , int b ) { return ( a + b ) / 2 ; }
def compute_average ( a , b ) : return floor ( ( a + b ) / 2 )
COMPUTE_MODULUS_DIVISION_BY_A_POWER_OF_2_NUMBER
unsigned int getModulo ( unsigned int n, unsigned int d ) { return ( n & ( d - 1 ) ); }
static int getModulo ( int n , int d ) { return ( n & ( d - 1 ) ) ; }
def getModulo ( n , d ) : return ( n & ( d - 1 ) )
COMPUTE_NCR_P_SET_1_INTRODUCTION_AND_DYNAMIC_PROGRAMMING_SOLUTION
int nCrModp ( int n, int r, int p ) { int C [ r + 1 ]; memset ( C, 0, sizeof ( C ) ); C [ 0 ] = 1; for ( int i = 1; i <= n; i ++ ) { for ( int j = min ( i, r ); j > 0; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p; } return C [ r ]; }
static int nCrModp ( int n , int r , int p ) { int C [ ] = new int [ r + 1 ] ; Arrays . fill ( C , 0 ) ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , r ) ; j > 0 ; j -- ) C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p ; } return C [ r ] ; }
def nCrModp ( n , r , p ) : C = [ 0 for i in range ( r + 1 ) ] C [ 0 ] = 1 for i in range ( 1 , n + 1 ) : for j in range ( min ( i , r ) , 0 , - 1 ) : C [ j ] = ( C [ j ] + C [ j - 1 ] ) % p return C [ r ]
CONSTRUCT_LEXICOGRAPHICALLY_SMALLEST_PALINDROME
string constructPalin ( string str, int len ) { int i = 0, j = len - 1; for (; i < j; i ++, j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != '*' ) continue; else if ( str [ i ] == str [ j ] && str [ i ] == '*' ) { str [ i ] = 'a'; str [ j ] = 'a'; continue; } else if ( str ...
static String constructPalin ( char [ ] str , int len ) { int i = 0 , j = len - 1 ; for ( ; i < j ; i ++ , j -- ) { if ( str [ i ] == str [ j ] && str [ i ] != '*' ) continue ; else if ( str [ i ] == str [ j ] && str [ i ] == '*' ) { str [ i ] = 'a' ; str [ j ] = 'a' ; continue ; }...
def constructPalin ( str , len ) : str = list ( str ) i = - 1 j = len while i < j : i += 1 j -= 1 if ( str [ i ] == str [ j ] and str [ i ] != '*' ) : continue elif ( str [ i ] == str [ j ] and str [ i ] == '*' ) : str [ i ] = 'a' str [...
CONVERT_DECIMAL_FRACTION_BINARY_NUMBER
string decimalToBinary ( double num, int k_prec ) { string binary = ""; int Integral = num; double fractional = num - Integral; while ( Integral > 0 ) { int rem = Integral % 2; binary . push_back ( rem + '0' ); Integral /= 2; } reverse ( binary . begin ( ), binary . end ( ) ); binary . push_ba...
static String decimalToBinary ( double num , int k_prec ) { String binary = "" ; int Integral = ( int ) num ; double fractional = num - Integral ; while ( Integral > 0 ) { int rem = Integral % 2 ; binary += ( ( char ) ( rem + '0' ) ) ; Integral /= 2 ; } binary = new Strin...
def decimalToBinary ( num , k_prec ) : binary = "" Integral = int ( num ) fractional = num - Integral while ( Integral > 0 ) : rem = Integral % 2 binary += str ( rem ) Integral //= 2 binary = binary [ : : - 1 ] binary += '.' while ( k_prec > 0 ) : fractional *...
CONVERT_STRICTLY_INCREASING_ARRAY_MINIMUM_CHANGES
int minRemove ( int arr [ ], int n ) { int LIS [ n ], len = 0; for ( int i = 0; i < n; i ++ ) LIS [ i ] = 1; for ( int i = 1; i < n; i ++ ) { for ( int j = 0; j < i; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) { LIS [ i ] = max ( LIS [ i ], LIS...
static int minRemove ( int arr [ ] , int n ) { int LIS [ ] = new int [ n ] ; int len = 0 ; for ( int i = 0 ; i < n ; i ++ ) LIS [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) LIS [...
def minRemove ( arr , n ) : LIS = [ 0 for i in range ( n ) ] len = 0 for i in range ( n ) : LIS [ i ] = 1 for i in range ( 1 , n ) : for j in range ( i ) : if ( arr [ i ] > arr [ j ] and ( i - j ) <= ( arr [ i ] - arr [ j ] ) ) : LIS [ i ] = max ( LIS [ i ] , ...
COUNTING_PAIRS_PERSON_CAN_FORM_PAIR_ONE_1
int numberOfWays ( int x ) { int dp [ x + 1 ]; dp [ 0 ] = dp [ 1 ] = 1; for ( int i = 2; i <= x; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ]; return dp [ x ]; }
static int numberOfWays ( int x ) { int dp [ ] = new int [ x + 1 ] ; dp [ 0 ] = dp [ 1 ] = 1 ; for ( int i = 2 ; i <= x ; i ++ ) dp [ i ] = dp [ i - 1 ] + ( i - 1 ) * dp [ i - 2 ] ; return dp [ x ] ; }
def numberOfWays ( x ) : dp = [ 0 for _ in range (x + 1)] dp[0] = dp[1] = 1 for i in range(2, x + 1): dp[i] = dp[i - 1] + (i - 1) * dp[i - 2] return dp[x]
COUNT_1S_SORTED_BINARY_ARRAY
int countOnes ( int arr [ ], int low, int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2; if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1; if ( arr [ mid ] == 1 ) return countOnes ( arr, ( mid + 1 ), high ); return countOnes ( arr, low, ( mid - 1...
static int countOnes ( int arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == high || arr [ mid + 1 ] == 0 ) && ( arr [ mid ] == 1 ) ) return mid + 1 ; if ( arr [ mid ] == 1 ) return countOnes ( arr , ( mid + 1 ) , high ) ; return countOnes ( arr ,...
def countOnes ( arr , low , high ) : if high >= low : mid = int ( low + ( high - low ) / 2 ) if ( ( mid == high or arr [ mid + 1 ] == 0 ) and ( arr [ mid ] == 1 ) ) : return mid + 1 if arr [ mid ] == 1 : return countOnes ( arr , ( mid + 1 ) , high ) return cou...
COUNT_ARRAYS_CONSECUTIVE_ELEMENT_DIFFERENT_VALUES
int countarray ( int n, int k, int x ) { int dp [ 109 ] = { 0 }; dp [ 0 ] = 0; dp [ 1 ] = 1; for ( int i = 2; i < n; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ]; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ); }
public static int countarray ( int n , int k , int x ) { int [ ] dp = new int [ 109 ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < n ; i ++ ) dp [ i ] = ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ; return ( x == 1 ? ( k - 1 ) * dp [ n - 2 ] : dp [ n - 1 ] ) ; }
def countarray ( n , k , x ) : dp = list ( ) dp.append ( 0 ) dp.append ( 1 ) i = 2 while i < n : dp.append ( ( k - 2 ) * dp [ i - 1 ] + ( k - 1 ) * dp [ i - 2 ] ) i = i + 1 return ( ( k - 1 ) * dp [ n - 2 ] if x == 1 else dp [ n - 1 ] )
COUNT_BALANCED_BINARY_TREES_HEIGHT_H
long long int countBT ( int h ) { long long int mod = 1000000007; long long int dp [ h + 1 ]; dp [ 0 ] = dp [ 1 ] = 1; for ( int i = 2; i <= h; i ++ ) { dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % mod + dp [ i - 1 ] ) % mod ) % mod; } return dp [ h ]; }
public static long countBT ( int h ) { long MOD = 1000000007 ; long [ ] dp = new long [ h + 1 ] ; dp [ 0 ] = 1 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i <= h ; ++ i ) dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD ; return dp [ h ] ; }
def countBT ( h ) : MOD = 1000000007 dp = [ 0 for i in range ( h + 1 ) ] dp [ 0 ] = 1 dp [ 1 ] = 1 for i in range ( 2 , h + 1 ) : dp [ i ] = ( dp [ i - 1 ] * ( ( 2 * dp [ i - 2 ] ) % MOD + dp [ i - 1 ] ) % MOD ) % MOD return dp [ h ]
COUNT_BINARY_DIGIT_NUMBERS_SMALLER_N
int countOfBinaryNumberLessThanN ( int N ) { queue < int > q; q . push ( 1 ); int cnt = 0; int t; while ( ! q . empty ( ) ) { t = q . front ( ); q . pop ( ); if ( t <= N ) { cnt ++; q . push ( t * 10 ); q . push ( t * 10 + 1 ); } } return cnt; }
static int countOfBinaryNumberLessThanN ( int N ) { Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 1 ) ; int cnt = 0 ; int t ; while ( q . size ( ) > 0 ) { t = q . peek ( ) ; q . remove ( ) ; if ( t <= N ) { cnt ++ ; q . add ( t * 10 ) ; q . add ( t * 10 + 1 ) ; } ...
def countOfBinaryNumberLessThanN ( N ) : q = collections . deque ( ) q.append ( 1 ) cnt = 0 while ( q ) : t = q.popleft ( ) if ( t <= N ) : cnt = cnt + 1 q.append ( t * 10 ) q.append ( t * 10 + 1 ) return cnt
COUNT_BINARY_STRINGS_K_TIMES_APPEARING_ADJACENT_TWO_SET_BITS
int countStrings ( int n, int k ) { int dp [ n + 1 ] [ k + 1 ] [ 2 ]; memset ( dp, 0, sizeof ( dp ) ); dp [ 1 ] [ 0 ] [ 0 ] = 1; dp [ 1 ] [ 0 ] [ 1 ] = 1; for ( int i = 2; i <= n; i ++ ) { for ( int j = 0; j < i && j < k + 1; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + dp ...
static int countStrings ( int n , int k ) { int dp [ ] [ ] [ ] = new int [ n + 1 ] [ k + 1 ] [ 2 ] ; dp [ 1 ] [ 0 ] [ 0 ] = 1 ; dp [ 1 ] [ 0 ] [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j < i && j < k + 1 ; j ++ ) { dp [ i ] [ j ] [ 0 ] = dp [ i - 1 ] [ j ] [ 0 ] + d...
def countStrings ( n , k ) : dp = [ [ [ 0 , 0 ] for __ in range ( k + 1 ) ] for _ in range ( n + 1 ) ] dp [ 1 ] [ 0 ] [ 0 ] = 1 dp [ 1 ] [ 0 ] [ 1 ] = 1 for i in range ( 2 , n + 1 ) : for j in range ( k + 1 ) : if j >= i: break else: dp [ i...
COUNT_CHARACTERS_STRING_DISTANCE_ENGLISH_ALPHABETS
int countPairs ( string str ) { int result = 0; int n = str . length ( ); for ( int i = 0; i < n; i ++ ) for ( int j = i + 1; j < n; j ++ ) if ( abs ( str [ i ] - str [ j ] ) == abs ( i - j ) ) result ++; return result; }
static int countPairs ( String str ) { int result = 0 ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( str . charAt ( i ) - str . charAt ( j ) ) == Math . abs ( i - j ) ) result ++ ; return result ; }
def countPairs ( str ) : result = 0 ; n = len ( str ) for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( abs ( ord ( str [ i ] ) - ord ( str [ j ] ) ) == abs ( i - j ) ) : result += 1 ; return result ;
COUNT_DERANGEMENTS_PERMUTATION_SUCH_THAT_NO_ELEMENT_APPEARS_IN_ITS_ORIGINAL_POSITION_1
int countDer ( int n ) { int der [ n + 1 ]; der [ 0 ] = 1; der [ 1 ] = 0; der [ 2 ] = 1; for ( int i = 3; i <= n; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ); return der [ n ]; }
static int countDer ( int n ) { int der [ ] = new int [ n + 1 ] ; der [ 0 ] = 1 ; der [ 1 ] = 0 ; der [ 2 ] = 1 ; for ( int i = 3 ; i <= n ; ++ i ) der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) ; return der [ n ] ; }
def countDer ( n ) : der = [ 0 for i in range ( n + 1 ) ] der [ 0 ] = 1 der [ 1 ] = 0 der [ 2 ] = 1 for i in range ( 3 , n + 1 ) : der [ i ] = ( i - 1 ) * ( der [ i - 1 ] + der [ i - 2 ] ) return der [ n ]
COUNT_DIGITS_FACTORIAL_SET_1
int findDigits ( int n ) { if ( n < 0 ) return 0; if ( n <= 1 ) return 1; double digits = 0; for ( int i = 2; i <= n; i ++ ) digits += log10 ( i ); return floor ( digits ) + 1; }
static int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += Math . log10 ( i ) ; return ( int ) ( Math . floor ( digits ) ) + 1 ; }
def findDigits ( n ) : if ( n < 0 ) : return 0 if ( n <= 1 ) : return 1 digits = 0 for i in range ( 2 , n + 1 ) : digits += math.log10 ( i ) return math.floor ( digits ) + 1
COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2
int countSolutions ( int n ) { int res = 0; for ( int x = 0; x * x < n; x ++ ) for ( int y = 0; x * x + y * y < n; y ++ ) res ++; return res; }
static int countSolutions ( int n ) { int res = 0 ; for ( int x = 0 ; x * x < n ; x ++ ) for ( int y = 0 ; x * x + y * y < n ; y ++ ) res ++ ; return res ; }
def countSolutions ( n ) : res = 0 x = 0 while ( x * x < n ) : y = 0 while ( x * x + y * y < n ) : res = res + 1 y = y + 1 x = x + 1 return res
COUNT_DISTINCT_NON_NEGATIVE_PAIRS_X_Y_SATISFY_INEQUALITY_XX_YY_N_2_1
int countSolutions ( int n ) { int x = 0, yCount, res = 0; for ( yCount = 0; yCount * yCount < n; yCount ++ ); while ( yCount != 0 ) { res += yCount; x ++; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount --; } return res; }
static int countSolutions ( int n ) { int x = 0 , yCount , res = 0 ; for ( yCount = 0 ; yCount * yCount < n ; yCount ++ ) ; while ( yCount != 0 ) { res += yCount ; x ++ ; while ( yCount != 0 && ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) yCount -- ; } return res ; }
def countSolutions ( n ) : x = 0 res = 0 yCount = 0 while ( yCount * yCount < n ) : yCount = yCount + 1 while ( yCount != 0 ) : res = res + yCount x = x + 1 while ( yCount != 0 and ( x * x + ( yCount - 1 ) * ( yCount - 1 ) >= n ) ) : yCount = yCount - 1 ...
COUNT_DISTINCT_OCCURRENCES_AS_A_SUBSEQUENCE
int findSubsequenceCount ( string S, string T ) { int m = T . length ( ), n = S . length ( ); if ( m > n ) return 0; int mat [ m + 1 ] [ n + 1 ]; for ( int i = 1; i <= m; i ++ ) mat [ i ] [ 0 ] = 0; for ( int j = 0; j <= n; j ++ ) mat [ 0 ] [ j ] = 1; for ( int i = 1; i <= m; i ++ ) { for ( ...
static int findSubsequenceCount ( String S , String T ) { int m = T . length ( ) ; int n = S . length ( ) ; if ( m > n ) return 0 ; int mat [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) mat [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) mat [ 0 ] [ j ] = 1 ; for ( ...
def findSubsequenceCount ( S , T ) : m = len ( T ) n = len ( S ) if m > n : return 0 mat = [ [ 0 for _ in range ( n + 1 ) ] for __ in range ( m + 1 ) ] for i in range ( 1 , m + 1 ) : mat [ i ] [ 0 ] = 0 for j in range ( n + 1 ) : mat [ 0 ] [ j ] = 1 for i in range ( 1...
COUNT_ENTRIES_EQUAL_TO_X_IN_A_SPECIAL_MATRIX
int count ( int n, int x ) { int cnt = 0; for ( int i = 1; i <= n && i <= x; i ++ ) { if ( x / i <= n && x % i == 0 ) cnt ++; } return cnt; }
static int count ( int n , int x ) { int count = 0 ; for ( int i = 1 ; i <= n && i <= x ; i ++ ) { if ( x / i <= n && x % i == 0 ) count ++ ; } return count ; }
def count ( n , x ) : cnt = 0 for i in range ( 1 , n + 1 ) : if i <= x : if x // i <= n and x % i == 0 : cnt += 1 return cnt
COUNT_EVEN_LENGTH_BINARY_SEQUENCES_WITH_SAME_SUM_OF_FIRST_AND_SECOND_HALF_BITS_1
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 ; }
def countSeq ( n ) : nCr = 1 res = 1 for r in range ( 1 , n + 1 ) : nCr = int ( ( nCr * ( n + 1 - r ) ) / r ) res += nCr * nCr return res
COUNT_FREQUENCY_K_MATRIX_SIZE_N_MATRIXI_J_IJ
int find ( int n, int k ) { if ( n + 1 >= k ) return ( k - 1 ); else return ( 2 * n + 1 - k ); }
public static int find ( int n , int k ) { if ( n + 1 >= k ) return ( k - 1 ) ; else return ( 2 * n + 1 - k ) ; }
def find ( n , k ) : if ( n + 1 >= k ) : return ( k - 1 ) else : return ( 2 * n + 1 - k )
COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY
int countPairs ( int arr [ ], int n ) { int ans = 0; for ( int i = 0; i < n; i ++ ) for ( int j = i + 1; j < n; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++; return ans; }
static int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] == arr [ j ] ) ans ++ ; return ans ; }
def countPairs ( arr , n ) : ans = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if ( arr [ i ] == arr [ j ] ) : ans += 1 return ans
COUNT_INDEX_PAIRS_EQUAL_ELEMENTS_ARRAY_1
int countPairs ( int arr [ ], int n ) { unordered_map < int, int > mp; for ( int i = 0; i < n; i ++ ) mp [ arr [ i ] ] ++; int ans = 0; for ( auto it = mp . begin ( ); it != mp . end ( ); it ++ ) { int count = it -> second; ans += ( count * ( count - 1 ) ) / 2; } return ans; }
public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; for ( Map...
def countPairs ( arr , n ) : mp = dict ( ) for i in range ( n ) : if arr [ i ] in mp.keys ( ) : mp [ arr [ i ] ] += 1 else : mp [ arr [ i ] ] = 1 ans = 0 for it in mp : count = mp [ it ] ans += ( count * ( count - 1 ) ) // 2 return ans
COUNT_INVERSIONS_OF_SIZE_THREE_IN_A_GIVE_ARRAY_1
int getInvCount ( int arr [ ], int n ) { int invcount = 0; for ( int i = 1; i < n - 1; i ++ ) { int small = 0; for ( int j = i + 1; j < n; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++; int great = 0; for ( int j = i - 1; j >= 0; j -- ) if ( arr [ i ] < arr [ j ] ) great ++; i...
static int getInvCount ( int arr [ ] , int n ) { int invcount = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int small = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] > arr [ j ] ) small ++ ; int great = 0 ; for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] < arr [ j ]...
def getInvCount ( arr , n ) : invcount = 0 for i in range ( 1 , n - 1 ) : small = 0 for j in range ( i + 1 , n ) : if ( arr [ i ] > arr [ j ] ) : small += 1 great = 0 for j in range ( i - 1 , - 1 , - 1 ) : if ( arr [ i ] < arr [ j ] ) : ...
COUNT_MINIMUM_NUMBER_SUBSETS_SUBSEQUENCES_CONSECUTIVE_NUMBERS
int numofsubset ( int arr [ ], int n ) { sort ( arr, arr + n ); int count = 1; for ( int i = 0; i < n - 1; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++; } return count; }
static int numofsubset ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] + 1 != arr [ i + 1 ] ) count ++ ; } return count ; }
def numofsubset ( arr , n ) : x = sorted ( arr ) count = 1 for i in range ( 0 , n - 1 ) : if ( x [ i ] + 1 != x [ i + 1 ] ) : count = count + 1 return count
COUNT_NATURAL_NUMBERS_WHOSE_PERMUTATION_GREATER_NUMBER
int countNumber ( int n ) { int result = 0; for ( int i = 1; i <= 9; i ++ ) { stack < int > s; if ( i <= n ) { s . push ( i ); result ++; } while ( ! s . empty ( ) ) { int tp = s . top ( ); s . pop ( ); for ( int j = tp % 10; j <= 9; j ++ ) { int...
static int countNumber ( int n ) { int result = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { Stack < Integer > s = new Stack < > ( ) ; if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . peek ( ) ; s . pop ( ) ; for ( int j = tp % 10 ;...
def countNumber ( n ) : result = 0 for i in range ( 1 , 10 ) : s = [ ] if ( i <= n ) : s.append ( i ) result += 1 while len ( s ) != 0 : tp = s [ - 1 ] s.pop ( ) for j in range ( tp % 10 , 10 ) : x = tp * 10 + j ...
COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX
int countNegative ( int M [ ] [ 4 ], int n, int m ) { int count = 0; for ( int i = 0; i < n; i ++ ) { for ( int j = 0; j < m; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1; else break; } } return count; }
static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( M [ i ] [ j ] < 0 ) count += 1 ; else break ; } } return count ; }
def countNegative ( M , n , m ) : count = 0 for i in range ( n ) : for j in range ( m ) : if M [ i ] [ j ] < 0 : count += 1 else : break return count
COUNT_NEGATIVE_NUMBERS_IN_A_COLUMN_WISE_ROW_WISE_SORTED_MATRIX_1
int countNegative ( int M [ ] [ 4 ], int n, int m ) { int count = 0; int i = 0; int j = m - 1; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1; i += 1; } else j -= 1; } return count; }
static int countNegative ( int M [ ] [ ] , int n , int m ) { int count = 0 ; int i = 0 ; int j = m - 1 ; while ( j >= 0 && i < n ) { if ( M [ i ] [ j ] < 0 ) { count += j + 1 ; i += 1 ; } else j -= 1 ; } return count ; }
def countNegative ( M , n , m ) : count = 0 i = 0 j = m - 1 while j >= 0 and i < n : if M [ i ] [ j ] < 0 : count += ( j + 1 ) i += 1 else : j -= 1 return count
COUNT_NUMBERS_CAN_CONSTRUCTED_USING_TWO_NUMBERS
int countNums ( int n, int x, int y ) { vector < bool > arr ( n + 1, false ); if ( x <= n ) arr [ x ] = true; if ( y <= n ) arr [ y ] = true; int result = 0; for ( int i = min ( x, y ); i <= n; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true; if ( i + y <= n ) arr [ i + ...
static int countNums ( int n , int x , int y ) { boolean [ ] arr = new boolean [ n + 1 ] ; if ( x <= n ) arr [ x ] = true ; if ( y <= n ) arr [ y ] = true ; int result = 0 ; for ( int i = Math . min ( x , y ) ; i <= n ; i ++ ) { if ( arr [ i ] ) { if ( i + x <= n ) arr [ i + x ] = true ; i...
def countNums ( n , x , y ) : arr = [ False for i in range ( n + 2 ) ] if ( x <= n ) : arr [ x ] = True if ( y <= n ) : arr [ y ] = True result = 0 for i in range ( min ( x , y ) , n + 1 ) : if ( arr [ i ] ) : if ( i + x <= n ) : arr [ i + x ] = Tr...
COUNT_NUMBER_BINARY_STRINGS_WITHOUT_CONSECUTIVE_1S
int countStrings ( int n ) { int a [ n ], b [ n ]; a [ 0 ] = b [ 0 ] = 1; for ( int i = 1; i < n; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ]; b [ i ] = a [ i - 1 ]; } return a [ n - 1 ] + b [ n - 1 ]; }
static int countStrings ( int n ) { int a [ ] = new int [ n ] ; int b [ ] = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } return a [ n - 1 ] + b [ n - 1 ] ; }
def countStrings ( n ) : a = [ 0 for i in range ( n ) ] b = [ 0 for i in range ( n ) ] a [ 0 ] = b [ 0 ] = 1 for i in range ( 1 , n ) : a [ i ] = a [ i - 1 ] + b [ i - 1 ] b [ i ] = a [ i - 1 ] return a [ n - 1 ] + b [ n - 1 ]
COUNT_NUMBER_INCREASING_SUBSEQUENCES_SIZE_K
int numOfIncSubseqOfSizeK ( int arr [ ], int n, int k ) { int dp [ k ] [ n ], sum = 0; memset ( dp, 0, sizeof ( dp ) ); for ( int i = 0; i < n; i ++ ) dp [ 0 ] [ i ] = 1; for ( int l = 1; l < k; l ++ ) { for ( int i = l; i < n; i ++ ) { dp [ l ] [ i ] = 0; for ( int j = l - 1; ...
static int numOfIncSubseqOfSizeK ( int arr [ ] , int n , int k ) { int dp [ ] [ ] = new int [ k ] [ n ] , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { dp [ 0 ] [ i ] = 1 ; } for ( int l = 1 ; l < k ; l ++ ) { for ( int i = l ; i < n ; i ++ ) { dp [ l ] [ i ] = 0 ; for ( int j...
def numOfIncSubseqOfSizeK ( arr , n , k ) : dp = [ [ 0 for i in range ( n ) ] for i in range ( k ) ] for i in range ( n ) : dp [ 0 ] [ i ] = 1 for l in range ( 1 , k ) : for i in range ( l , n ) : dp [ l ] [ i ] = 0 for j in range ( l - 1 , i ) : if ( ...
COUNT_NUMBER_OF_OCCURRENCES_OR_FREQUENCY_IN_A_SORTED_ARRAY
int countOccurrences ( int arr [ ], int n, int x ) { int res = 0; for ( int i = 0; i < n; i ++ ) if ( x == arr [ i ] ) res ++; return res; }
static int countOccurrences ( int arr [ ] , int n , int x ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( x == arr [ i ] ) res ++ ; return res ; }
def countOccurrences ( arr , n , x ) : res = 0 for i in range ( n ) : if x == arr [ i ] : res += 1 return res
COUNT_NUMBER_OF_STRINGS_MADE_OF_R_G_AND_B_USING_GIVEN_COMBINATION
int possibleStrings ( int n, int r, int b, int g ) { int fact [ n + 1 ]; fact [ 0 ] = 1; for ( int i = 1; i <= n; i ++ ) fact [ i ] = fact [ i - 1 ] + i; int left = n - ( r + g + b ); int sum = 0; for ( int i = 0; i <= left; i ++ ) { for ( int j = 0; j <= left - i; j ++ ) { int k =...
static int possibleStrings ( int n , int r , int b , int g ) { int fact [ ] = new int [ n + 1 ] ; fact [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) fact [ i ] = fact [ i - 1 ] + i ; int left = n - ( r + g + b ) ; int sum = 0 ; for ( int i = 0 ; i <= left ; i ++ ) { for (...
def possibleStrings ( n , r , b , g ) : fact = [ 0 for i in range ( n + 1 ) ] fact [ 0 ] = 1 for i in range ( 1 , n + 1 , 1 ) : fact [ i ] = fact [ i - 1 ] + i left = n - ( r + g + b ) sum = 0 for i in range ( 0 , left + 1 , 1 ) : for j in range ( 0 , left - i + 1 , 1 ) : ...
COUNT_NUMBER_OF_WAYS_TO_PARTITION_A_SET_INTO_K_SUBSETS_1
int countP ( int n, int k ) { int dp [ n + 1 ] [ k + 1 ]; memset (dp, 0, sizeof(dp)); for ( int i = 0; i <= n; i ++ ) dp [ i ] [ 0 ] = 0; for ( int i = 0; i <= k; i ++ ) dp [ 0 ] [ k ] = 0; for ( int i = 1; i <= n; i ++ ) for ( int j = 1; j <= k; j ++ ) if ( j == 1 || i == j ) dp [ i ] [ j ] =...
static int countP ( int n , int k ) { int [ ] [ ] dp = new int [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 0 ; for ( int i = 0 ; i <= k ; i ++ ) dp [ 0 ] [ k ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 1 ; j <= k ; j ++ ) if ( j == 1 || i == j ) dp [ i ] ...
def countP ( n , k ) : dp = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] for i in range ( n + 1 ) : dp [ i ] [ 0 ] = 0 for i in range ( k + 1 ) : dp [ 0 ] [ k ] = 0 for i in range ( 1 , n + 1 ) : for j in range ( 1 , k + 1 ) : if ( j == 1 or i == j ) : ...
COUNT_NUMBER_PAIRS_N_B_N_GCD_B_B
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; }
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 ; }
def CountPairs ( n ) : k = n imin = 1 ans = 0 while ( imin <= n ) : imax = int ( n / k ) ans += k * ( imax - imin + 1 ) imin = imax + 1 k = int ( n / imin ) return ans
COUNT_NUMBER_WAYS_REACH_GIVEN_SCORE_GAME
int count ( int n ) { int table [ n + 1 ], i; for ( int j = 0; j < n + 1; j ++ ) table [ j ] = 0; table [ 0 ] = 1; for ( i = 3; i <= n; i ++ ) table [ i ] += table [ i - 3 ]; for ( i = 5; i <= n; i ++ ) table [ i ] += table [ i - 5 ]; for ( i = 10; i <= n; i ++ ) table [ i ] += table [ i - 1...
static int count ( int n ) { int table [ ] = new int [ n + 1 ] , i ; Arrays . fill ( table , 0 ) ; table [ 0 ] = 1 ; for ( i = 3 ; i <= n ; i ++ ) table [ i ] += table [ i - 3 ] ; for ( i = 5 ; i <= n ; i ++ ) table [ i ] += table [ i - 5 ] ; for ( i = 10 ; i <= n ; i ++ ) table [ i ] += table [...
def count ( n ) : table = [ 0 for i in range ( n + 1 ) ] table [ 0 ] = 1 for i in range ( 3 , n + 1 ) : table [ i ] += table [ i - 3 ] for i in range ( 5 , n + 1 ) : table [ i ] += table [ i - 5 ] for i in range ( 10 , n + 1 ) : table [ i ] += table [ i - 10 ] return tabl...
COUNT_NUMBER_WAYS_TILE_FLOOR_SIZE_N_X_M_USING_1_X_M_SIZE_TILES
int countWays ( int n, int m ) { int count [ n + 1 ]; count [ 0 ] = 0; for ( int i = 1; i <= n; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ]; else if ( i < m ) count [ i ] = 1; else count [ i ] = 2; } return count [ n ]; }
static int countWays ( int n , int m ) { int count [ ] = new int [ n + 1 ] ; count [ 0 ] = 0 ; int i ; for ( i = 1 ; i <= n ; i ++ ) { if ( i > m ) count [ i ] = count [ i - 1 ] + count [ i - m ] ; else if ( i < m ) count [ i ] = 1 ; else count [ i ] = 2 ; } return count [ n ] ; }
def countWays ( n , m ) : count = [ ] for i in range ( n + 2 ) : count.append ( 0 ) count [ 0 ] = 0 for i in range ( 1 , n + 1 ) : if ( i > m ) : count [ i ] = count [ i - 1 ] + count [ i - m ] elif ( i < m ) : count [ i ] = 1 else : co...
COUNT_OF_SUB_STRINGS_THAT_DO_NOT_CONTAIN_ALL_THE_CHARACTERS_FROM_THE_SET_A_B_C_AT_THE_SAME_TIME
int CountSubstring ( char str [ ], int n ) { int ans = ( n * ( n + 1 ) ) / 2; int a_index = 0; int b_index = 0; int c_index = 0; for ( int i = 0; i < n; i ++ ) { if ( str [ i ] == 'a' ) { a_index = i + 1; ans -= min ( b_index, c_index ); } else if ( str [ i ] == 'b' ) { b_ind...
static int CountSubstring ( char str [ ] , int n ) { int ans = ( n * ( n + 1 ) ) / 2 ; int a_index = 0 ; int b_index = 0 ; int c_index = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == 'a' ) { a_index = i + 1 ; ans -= Math . min ( b_index , c_index ) ; } else if ( str [ i ...
def CountSubString ( str , n ) : ans = ( n * ( n + 1 ) ) // 2 a_index = 0 b_index = 0 c_index = 0 for i in range ( n ) : if ( str [ i ] == 'a' ) : a_index = i + 1 ans -= min ( b_index , c_index ) elif ( str [ i ] == 'b' ) : b_index = i + 1 ...
COUNT_OPERATIONS_MAKE_STRINGAB_FREE
int abFree ( string s ) { int n = s . length ( ); char char_array [ n + 1 ]; strcpy ( char_array, s . c_str ( ) ); int b_count = 0; int res = 0; for ( int i = 0; i < n; i ++ ) { if ( char_array [ n - i - 1 ] == 'a' ) { res = ( res + b_count ); b_count = ( b_count * 2 ); } else { ...
static int abFree ( char [ ] s ) { int b_count = 0 ; int res = 0 ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( s [ s . length - i - 1 ] == 'a' ) { res = ( res + b_count ) ; b_count = ( b_count * 2 ) ; } else { b_count += 1 ; } } return res ; }
def abFree ( s ) : b_count = 0 res = 0 for i in range ( len ( s ) ) : if s [ ~ i ] == 'a' : res = ( res + b_count ) b_count = ( b_count * 2 ) else : b_count += 1 return res
COUNT_PAIRS_DIFFERENCE_EQUAL_K
int countPairsWithDiffK ( int arr [ ], int n, int k ) { int count = 0; for ( int i = 0; i < n; i ++ ) { for ( int j = i + 1; j < n; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++; } return count; }
static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; }
def countPairsWithDiffK ( arr , n , k ) : count = 0 for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : count += 1 return count
COUNT_PAIRS_DIFFERENCE_EQUAL_K_1
int countPairsWithDiffK ( int arr [ ], int n, int k ) { int count = 0; sort ( arr, arr + n ); int l = 0; int r = 0; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++; l ++; r ++; } else if ( arr [ r ] - arr [ l ] > k ) l ++; else r ++; } return count; }
static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; Arrays . sort ( arr ) ; int l = 0 ; int r = 0 ; while ( r < n ) { if ( arr [ r ] - arr [ l ] == k ) { count ++ ; l ++ ; r ++ ; } else if ( arr [ r ] - arr [ l ] > k ) l ++ ; else r ++ ; } ret...
def countPairsWithDiffK ( arr , n , k ) : count = 0 arr.sort ( ) l = 0 r = 0 while r < n : if arr [ r ] - arr [ l ] == k : count += 1 l += 1 r += 1 elif arr [ r ] - arr [ l ] > k : l += 1 else : r += 1 return cou...
COUNT_PAIRS_TWO_SORTED_ARRAYS_WHOSE_SUM_EQUAL_GIVEN_VALUE_X
int countPairs ( int arr1 [ ], int arr2 [ ], int m, int n, int x ) { int count = 0; for ( int i = 0; i < m; i ++ ) for ( int j = 0; j < n; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++; return count; }
static int countPairs ( int [ ] arr1 , int [ ] arr2 , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( ( arr1 [ i ] + arr2 [ j ] ) == x ) count ++ ; return count ; }
def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 for i in range ( m ) : for j in range ( n ) : if arr1 [ i ] + arr2 [ j ] == x : count = count + 1 return count
COUNT_PAIRS_TWO_SORTED_ARRAYS_WHOSE_SUM_EQUAL_GIVEN_VALUE_X_1
int countPairs ( int arr1 [ ], int arr2 [ ], int m, int n, int x ) { int count = 0; unordered_set < int > us; for ( int i = 0; i < m; i ++ ) us . insert ( arr1 [ i ] ); for ( int j = 0; j < n; j ++ ) if ( us . find ( x - arr2 [ j ] ) != us . end ( ) ) count ++; return count; }
static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; HashSet < Integer > us = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < m ; i ++ ) us . add ( arr1 [ i ] ) ; for ( int j = 0 ; j < n ; j ++ ) if ( us . contains ( x - arr2 [ j ] ) ) count ++ ; return c...
def countPairs ( arr1 , arr2 , m , n , x ) : count = 0 us = set ( ) for i in range ( m ) : us.add ( arr1 [ i ] ) for j in range ( n ) : if x - arr2 [ j ] in us : count += 1 return count
COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY
int countPairs ( int arr [ ], int n ) { int result = 0; for ( int i = 0; i < n; i ++ ) { for ( int j = i + 1; j < n; j ++ ) { int product = arr [ i ] * arr [ j ]; for ( int k = 0; k < n; k ++ ) { if ( arr [ k ] == product ) { result ++; break; ...
static int countPairs ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * arr [ j ] ; for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ k ] == product ) { result ++ ; ...
def countPairs ( arr , n ) : result = 0 ; for i in range ( 0 , n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] ; for k in range ( 0 , n ) : if ( arr [ k ] == product ) : result = result + 1 ; break ; ...
COUNT_PAIRS_WHOSE_PRODUCTS_EXIST_IN_ARRAY_1
int countPairs ( int arr [ ], int n ) { int result = 0; set < int > Hash; for ( int i = 0; i < n; i ++ ) Hash . insert ( arr [ i ] ); for ( int i = 0; i < n; i ++ ) { for ( int j = i + 1; j < n; j ++ ) { int product = arr [ i ] * arr [ j ]; if ( Hash . find ( product ) != Hash . ...
static int countPairs ( int arr [ ] , int n ) { int result = 0 ; HashSet < Integer > Hash = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { Hash . add ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int product = arr [ i ] * ar...
def countPairs ( arr , n ) : result = 0 Hash = set ( ) for i in range ( n ) : Hash.add ( arr [ i ] ) for i in range ( n ) : for j in range ( i + 1 , n ) : product = arr [ i ] * arr [ j ] if product in ( Hash ) : result += 1 return result
End of preview. Expand in Data Studio

No dataset card yet

Downloads last month
-