text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Sum of subarray arr [ i ... n - 1 ] | subSum += curr ; NEW_LINE |
Return the sum of the modified array | return sumArr ( arr , n ) ; NEW_LINE |
Driver code | arr = [ 40 , 25 , 12 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( sumModArr ( arr , n ) ) ; NEW_LINE |
Python3 implementation of the approach | NumUnsignBits = 64 ; NEW_LINE |
Function to return the number closest to x which has equal number of set bits as x | def findNum ( x ) : NEW_LINE |
Loop for each bit in x and compare with the next bit | for i in range ( NumUnsignBits - 1 ) : NEW_LINE INDENT if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) : NEW_LINE INDENT x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; NEW_LINE return x ; NEW_LINE DEDENT DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 92 ; NEW_LINE print ( findNum ( n ) ) ; NEW_LINE DEDENT |
Function to return the remaining count of cakes | def cntCakes ( n , m ) : NEW_LINE |
Sum for 1 cycle | sum = ( n * ( n + 1 ) ) // 2 NEW_LINE |
no . of full cycle and remainder | quo , rem = m // sum , m % sum NEW_LINE ans = m - quo * sum NEW_LINE x = int ( ( - 1 + ( 8 * rem + 1 ) ** 0.5 ) / 2 ) NEW_LINE ans = ans - x * ( x + 1 ) // 2 NEW_LINE return ans NEW_LINE |
Driver code | def main ( ) : NEW_LINE INDENT n = 4 NEW_LINE m = 11 NEW_LINE ans = cntCakes ( n , m ) NEW_LINE print ( ans ) NEW_LINE DEDENT main ( ) NEW_LINE |
Function to return the number of squares inside an n * n grid | def cntSquares ( n ) : NEW_LINE INDENT return int ( n * ( n + 1 ) * ( 2 * n + 1 ) / 6 ) NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( cntSquares ( 4 ) ) ; NEW_LINE DEDENT |
Function to return the reverse of num | def reverse ( num ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( num > 0 ) : NEW_LINE INDENT rev = rev * 10 + num % 10 ; NEW_LINE num = num // 10 ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT |
Function that returns true if num is palindrome | def isPalindrome ( num ) : NEW_LINE |
If the number is equal to the reverse of it then it is a palindrome | if ( num == reverse ( num ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE |
Function to prall the d - digit palindrome numbers | def printPalindromes ( d ) : NEW_LINE INDENT if ( d <= 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT DEDENT |
Smallest and the largest d - digit numbers | smallest = pow ( 10 , d - 1 ) ; NEW_LINE largest = pow ( 10 , d ) - 1 ; NEW_LINE |
Starting from the smallest d - digit number till the largest | for i in range ( smallest , largest + 1 ) : NEW_LINE |
If the current number is palindrome | if ( isPalindrome ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT |
Driver code | d = 2 ; NEW_LINE printPalindromes ( d ) ; NEW_LINE |
Function to find the X and Y intercepts of the line passing through the given points | def getXandYintercept ( P , Q ) : NEW_LINE INDENT a = P [ 1 ] - Q [ 1 ] NEW_LINE b = P [ 0 ] - Q [ 0 ] NEW_LINE DEDENT |
if line is parallel to y axis | if b == 0 : NEW_LINE |
x - intercept will be p [ 0 ] | print ( P [ 0 ] ) NEW_LINE |
y - intercept will be infinity | print ( " infinity " ) NEW_LINE INDENT return NEW_LINE DEDENT |
if line is parallel to x axis | if a == 0 : NEW_LINE |
x - intercept will be infinity | print ( " infinity " ) NEW_LINE |
y - intercept will be p [ 1 ] | print ( P [ 1 ] ) NEW_LINE INDENT return NEW_LINE DEDENT |
Slope of the line | m = a / b NEW_LINE |
y = mx + c in where c is unknown Use any of the given point to find c | x = P [ 0 ] NEW_LINE y = P [ 1 ] NEW_LINE c = y - m * x NEW_LINE |
For finding the x - intercept put y = 0 | y = 0 NEW_LINE x = ( y - c ) / m NEW_LINE print ( x ) NEW_LINE |
For finding the y - intercept put x = 0 | x = 0 NEW_LINE y = m * x + c NEW_LINE print ( y ) NEW_LINE |
Driver code | p1 = [ 5 , 2 ] NEW_LINE p2 = [ 7 , 2 ] NEW_LINE getXandYintercept ( p1 , p2 ) NEW_LINE |
Python3 implementation of the approach | import sys NEW_LINE from math import sqrt NEW_LINE |
Function to return the minimum number of moves required to reach the cell containing N starting from ( 1 , 1 ) | def min_moves ( n ) : NEW_LINE |
To store the required answer | ans = sys . maxsize ; NEW_LINE |
For all possible values of divisors | for i in range ( 1 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE |
If i is a divisor of n | if ( n % i == 0 ) : NEW_LINE |
Get the moves to reach n | ans = min ( ans , i + n // i - 2 ) ; NEW_LINE |
Return the required answer | return ans ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE print ( min_moves ( n ) ) ; NEW_LINE DEDENT |
Python3 implementation of the approach | MOD = 2019 ; NEW_LINE |
Function to return the minimum possible value of ( i * j ) % 2019 | def min_modulo ( l , r ) : NEW_LINE |
If we can get a number divisible by 2019 | if ( r - l >= MOD ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE |
Find the minimum value by running nested loops | ans = MOD - 1 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , r + 1 ) : NEW_LINE INDENT ans = min ( ans , ( i * j ) % MOD ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 2020 ; r = 2040 ; NEW_LINE print ( min_modulo ( l , r ) ) ; NEW_LINE DEDENT |
Function to find the required fractions | def find_numbers ( N ) : NEW_LINE |
Base condition | if ( N == 1 ) : NEW_LINE INDENT print ( - 1 , end = " " ) ; NEW_LINE DEDENT |
For N > 1 | else : NEW_LINE INDENT print ( N , N + 1 , N * ( N + 1 ) ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE find_numbers ( N ) ; NEW_LINE DEDENT |
Function to return the count of all valid pairs | def countPairs ( arr , n ) : NEW_LINE |
To store the frequencies of ( arr [ i ] - i ) | map = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT map [ arr [ i ] - i ] = map . get ( arr [ i ] - i , 0 ) + 1 NEW_LINE DEDENT |
To store the required count | res = 0 NEW_LINE for x in map : NEW_LINE INDENT cnt = map [ x ] NEW_LINE DEDENT |
If cnt is the number of elements whose difference with their index is same then ( ( cnt * ( cnt - 1 ) ) / 2 ) such pairs are possible | res += ( ( cnt * ( cnt - 1 ) ) // 2 ) NEW_LINE return res NEW_LINE |
Driver code | arr = [ 1 , 5 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE |
Function to return the minimum possible integer that can be obtained from the given integer after performing the given operations | def minInt ( str1 ) : NEW_LINE |
For every digit | for i in range ( len ( str1 ) ) : NEW_LINE |
Digits less than 5 need not to be changed as changing them will lead to a larger number | if ( str1 [ i ] >= 5 ) : NEW_LINE INDENT str1 [ i ] = ( 9 - str1 [ i ] ) NEW_LINE DEDENT |
The resulting integer cannot have leading zero | if ( str1 [ 0 ] == 0 ) : NEW_LINE INDENT str1 [ 0 ] = 9 NEW_LINE DEDENT temp = " " NEW_LINE for i in str1 : NEW_LINE INDENT temp += str ( i ) NEW_LINE DEDENT return temp NEW_LINE |
Driver code | str1 = "589" NEW_LINE str1 = [ int ( i ) for i in str1 ] NEW_LINE print ( minInt ( str1 ) ) NEW_LINE |
Function to return the minimum number of given operations required to reduce n to 1 | def minOperations ( n ) : NEW_LINE |
To store the count of operations | count = 0 NEW_LINE |
To store the digit | d = 0 NEW_LINE |
If n is already then no operation is required | if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Extract all the digits except the first digit | while ( n > 9 ) : NEW_LINE |
Store the maximum of that digits | d = max ( n % 10 , d ) NEW_LINE n //= 10 NEW_LINE |
for each digit | count += 10 NEW_LINE |
First digit | d = max ( d , n - 1 ) NEW_LINE |
Add the value to count | count += abs ( d ) NEW_LINE return count - 1 NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 240 NEW_LINE print ( minOperations ( n ) ) NEW_LINE DEDENT |
Function to return the maximum number that can be formed by changing at most k digits in str | def findMaximumNum ( st , n , k ) : NEW_LINE |
For every digit of the number | for i in range ( n ) : NEW_LINE |
If no more digits can be replaced | if ( k < 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT |
If current digit is not already 9 | if ( st [ i ] != '9' ) : NEW_LINE |
Replace it with 9 | st = st [ 0 : i ] + '9' + st [ i + 1 : ] NEW_LINE |
One digit has been used | k -= 1 NEW_LINE return st NEW_LINE |
Driver code | st = "569431" NEW_LINE n = len ( st ) NEW_LINE k = 3 NEW_LINE print ( findMaximumNum ( st , n , k ) ) NEW_LINE |
Function that returns true if a and b are anagarams of each other | def areAnagrams ( a , b ) : NEW_LINE |
Converting numbers to strings | a = str ( a ) NEW_LINE b = str ( b ) NEW_LINE |
Checking if the sorting values of two strings are equal | if ( sorted ( a ) == sorted ( b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT |
Driver code | a = 240 NEW_LINE b = 204 NEW_LINE if ( areAnagrams ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT |
Function that calculates occurrences of given angle that can be created using any 3 sides | def solve ( ang , n ) : NEW_LINE |
Maximum angle in a regular n - gon is equal to the interior angle If the given angle is greater than the interior angle then the given angle cannot be created | if ( ( ang * n ) > ( 180 * ( n - 2 ) ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
The given angle times n should be divisible by 180 else it cannot be created | elif ( ( ang * n ) % 180 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
Initialise answer | ans = 1 NEW_LINE |
Calculate the frequency of given angle for each vertex | freq = ( ang * n ) // 180 NEW_LINE |
Multiply answer by frequency . | ans = ans * ( n - 1 - freq ) NEW_LINE |
Multiply answer by the number of vertices . | ans = ans * n NEW_LINE return ans NEW_LINE |
Driver code | ang = 90 NEW_LINE n = 4 NEW_LINE print ( solve ( ang , n ) ) NEW_LINE |
Function that will check whether number is prime or not | def prime ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if i * i > n + 1 : NEW_LINE INDENT break NEW_LINE DEDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT |
Function to print the 3 rd number | def thirdNumber ( a , b ) : NEW_LINE INDENT summ = 0 NEW_LINE temp = 0 NEW_LINE summ = a + b NEW_LINE temp = 1 NEW_LINE DEDENT |
If the summ is odd | if ( summ & 1 ) : NEW_LINE INDENT temp = 2 NEW_LINE DEDENT |
If summ is not prime | while ( prime ( summ + temp ) == False ) : NEW_LINE INDENT temp += 2 NEW_LINE DEDENT print ( temp ) NEW_LINE |
Driver code | a = 3 NEW_LINE b = 5 NEW_LINE thirdNumber ( a , b ) NEW_LINE |
Function to return the value of nCr | def nCr ( n , r ) : NEW_LINE |
Initialize the answer | ans = 1 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE |
Divide simultaneously by i to avoid overflow | ans *= ( n - r + i ) ; NEW_LINE ans //= i ; NEW_LINE return ans ; NEW_LINE |
Function to return the count of ways | def total_ways ( N , X ) : NEW_LINE INDENT return ( nCr ( N - 1 , X - 1 ) + nCr ( N - 1 , X ) ) ; NEW_LINE DEDENT |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; X = 3 ; NEW_LINE print ( total_ways ( N , X ) ) ; NEW_LINE DEDENT |
Function to return the largest power | def calculate ( n , k , m , power ) : NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.