text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
If n is greater than given M | if n > m : NEW_LINE INDENT if power == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return power - 1 NEW_LINE DEDENT DEDENT |
If n == m | elif n == m : NEW_LINE INDENT return power NEW_LINE DEDENT else : NEW_LINE |
Checking for the next power | return calculate ( n * k , k , m , power + 1 ) NEW_LINE |
Driver 's code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 1 NEW_LINE K = 2 NEW_LINE M = 5 NEW_LINE print ( calculate ( N , K , M , 0 ) ) NEW_LINE DEDENT |
Function that will find out the number | def printNumber ( holes ) : NEW_LINE |
If number of holes equal 0 then return 1 | if ( holes == 0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT |
If number of holes equal 0 then return 0 | elif ( holes == 1 ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT |
If number of holes is more than 0 or 1. | else : NEW_LINE INDENT rem = 0 NEW_LINE quo = 0 NEW_LINE rem = holes % 2 NEW_LINE quo = holes // 2 NEW_LINE DEDENT |
If number of holes is odd | if ( rem == 1 ) : NEW_LINE INDENT print ( "4" , end = " " ) NEW_LINE DEDENT for i in range ( quo ) : NEW_LINE INDENT print ( "8" , end = " " ) NEW_LINE DEDENT |
Driver code | holes = 3 NEW_LINE |
Calling Function | printNumber ( holes ) NEW_LINE |
Function to return the minimum cost to make each array element equal | def minCost ( arr , n ) : NEW_LINE |
To store the count of even numbers present in the array | count_even = 0 NEW_LINE |
To store the count of odd numbers present in the array | count_odd = 0 NEW_LINE |
Iterate through the array and find the count of even numbers and odd numbers | for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT count_even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_odd += 1 NEW_LINE DEDENT DEDENT return min ( count_even , count_odd ) NEW_LINE |
Driver code | arr = [ 2 , 4 , 3 , 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minCost ( arr , n ) ) NEW_LINE |
Function to return the count of subarrays with negative product | def negProdSubArr ( arr , n ) : NEW_LINE INDENT positive = 1 NEW_LINE negative = 0 NEW_LINE for i in range ( n ) : NEW_LINE DEDENT |
Replace current element with 1 if it is positive else replace it with - 1 instead | if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = - 1 NEW_LINE DEDENT |
Take product with previous element to form the prefix product | if ( i > 0 ) : NEW_LINE INDENT arr [ i ] *= arr [ i - 1 ] NEW_LINE DEDENT |
Count positive and negative elements in the prefix product array | if ( arr [ i ] == 1 ) : NEW_LINE INDENT positive += 1 NEW_LINE DEDENT else : NEW_LINE INDENT negative += 1 NEW_LINE DEDENT |
Return the required count of subarrays | return ( positive * negative ) NEW_LINE |
Function to return the count of subarrays with positive product | def posProdSubArr ( arr , n ) : NEW_LINE |
Total subarrays possible | total = ( n * ( n + 1 ) ) / 2 ; NEW_LINE |
Count to subarrays with negative product | cntNeg = negProdSubArr ( arr , n ) ; NEW_LINE |
Return the count of subarrays with positive product | return ( total - cntNeg ) ; NEW_LINE |
Driver code | arr = [ 5 , - 4 , - 3 , 2 , - 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( posProdSubArr ( arr , n ) ) NEW_LINE |
Python3 implementation of the approach | MAX = 10000 NEW_LINE |
Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false + if i is Not a prime , else true . | prime = [ True for i in range ( MAX + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE for p in range ( 2 , MAX + 1 ) : NEW_LINE DEDENT |
If prime [ p ] is not changed , then it is a prime | if ( prime [ p ] == True ) : NEW_LINE |
Set all multiples of p to non - prime | for i in range ( 2 * p , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT |
Function to return the xor of 1 st N prime numbers | def xorFirstNPrime ( n ) : NEW_LINE |
Count of prime numbers | count = 0 NEW_LINE num = 1 NEW_LINE |
XOR of prime numbers | xorVal = 0 NEW_LINE while ( count < n ) : NEW_LINE |
If the number is prime xor it | if ( prime [ num ] ) : NEW_LINE INDENT xorVal ^= num NEW_LINE DEDENT |
Increment the count | count += 1 NEW_LINE |
Get to the next number | num += 1 NEW_LINE return xorVal NEW_LINE |
Create the sieve | SieveOfEratosthenes ( ) NEW_LINE n = 4 NEW_LINE |
Find the xor of 1 st n prime numbers | print ( xorFirstNPrime ( n ) ) NEW_LINE |
Python3 implementation of the approach | mod = 1000000007 NEW_LINE |
Value of inverse modulo 2 with 10 ^ 9 + 7 | inv2 = 500000004 ; NEW_LINE |
Function to return num % 1000000007 where num is a large number | def modulo ( num ) : NEW_LINE |
Initialize result | res = 0 ; NEW_LINE |
One by one process all the digits of string 'num | ' NEW_LINE INDENT for i in range ( len ( num ) ) : NEW_LINE INDENT res = ( res * 10 + int ( num [ i ] ) - 0 ) % mod ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT |
Function to return the sum of the integers from the given range modulo 1000000007 | def findSum ( L , R ) : NEW_LINE |
a stores the value of L modulo 10 ^ 9 + 7 | a = modulo ( L ) ; NEW_LINE |
b stores the value of R modulo 10 ^ 9 + 7 | b = modulo ( R ) ; NEW_LINE |
l stores the sum of natural numbers from 1 to ( a - 1 ) | l = ( ( a * ( a - 1 ) ) % mod * inv2 ) % mod ; NEW_LINE |
r stores the sum of natural numbers from 1 to b | r = ( ( b * ( b + 1 ) ) % mod * inv2 ) % mod ; NEW_LINE ret = ( r % mod - l % mod ) ; NEW_LINE |
If the result is negative | if ( ret < 0 ) : NEW_LINE INDENT ret = ret + mod ; NEW_LINE DEDENT else : NEW_LINE INDENT ret = ret % mod ; NEW_LINE DEDENT return ret ; NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = "88949273204" ; NEW_LINE R = "98429729474298592" ; NEW_LINE print ( findSum ( L , R ) ) ; NEW_LINE DEDENT |
Function to return the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } | import sys NEW_LINE def maxSubArraySum ( a , n , k , i ) : NEW_LINE INDENT max_so_far = - sys . maxsize ; NEW_LINE max_ending_here = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here ; NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE DEDENT i += k ; NEW_LINE DEDENT return max_so_far ; NEW_LINE DEDENT |
Function to return the sum of the maximum required subsequence | def find ( arr , n , k ) : NEW_LINE |
To store the result | maxSum = 0 ; NEW_LINE |
Run a loop from 0 to k | for i in range ( 0 , min ( n , k ) + 1 ) : NEW_LINE INDENT sum = 0 ; NEW_LINE DEDENT |
Find the maximum subarray sum for the array { a [ i ] , a [ i + k ] , a [ i + 2 k ] , ... } | maxSum = max ( maxSum , maxSubArraySum ( arr , n , k , i ) ) ; NEW_LINE |
Return the maximum value | return maxSum ; NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , - 3 , - 1 , - 1 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( find ( arr , n , k ) ) ; NEW_LINE DEDENT |
Python3 implementation of the approach | from math import sqrt NEW_LINE MAX = 1000000 NEW_LINE |
Create a boolean array " prime [ 0 . . n ] " and initialize all entries it as true . A value in prime [ i ] will finally be false if i is Not a prime , else true . | prime = [ True ] * ( MAX + 1 ) ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False ; NEW_LINE for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 ) : NEW_LINE DEDENT |
If prime [ p ] is not changed , then it is a prime | if ( prime [ p ] == True ) : NEW_LINE |
Set all multiples of p to non - prime | for i in range ( p * 2 , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT |
Function to find the first n odd prime numbers | def solve ( n ) : NEW_LINE |
To store the current count of prime numbers | count = 0 ; NEW_LINE i = 3 ; NEW_LINE |
Starting with 3 as 2 is an even prime number | while count < n : NEW_LINE |
If i is prime | if ( prime [ i ] ) : NEW_LINE |
Print i and increment count | print ( i , end = " ▁ " ) ; NEW_LINE count += 1 ; NEW_LINE i += 1 NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE |
Create the sieve | SieveOfEratosthenes ( ) ; NEW_LINE n = 6 ; NEW_LINE solve ( n ) ; NEW_LINE |
Find the probability that a n digit number is palindrome | def solve ( n ) : NEW_LINE INDENT n_2 = n // 2 ; NEW_LINE DEDENT |
Denominator | den = "1" ; NEW_LINE |
Assign 10 ^ ( floor ( n / 2 ) ) to denominator | while ( n_2 ) : NEW_LINE INDENT den += '0' ; NEW_LINE n_2 -= 1 NEW_LINE DEDENT |
Display the answer | print ( str ( 1 ) + " / " + str ( den ) ) NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE solve ( N ) ; NEW_LINE DEDENT |
Python3 implementation of the approach | MOD = 1000000007 NEW_LINE |
Function to return the count of ways to choose the balls | def countWays ( n ) : NEW_LINE |
Return ( ( 2 ^ n ) - 1 ) % MOD | return ( ( ( 2 ** n ) - 1 ) % MOD ) NEW_LINE |
Driver code | n = 3 NEW_LINE print ( countWays ( n ) ) NEW_LINE |
Function to return the minimum sum | def findMin ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE DEDENT DEDENT |
sort the array to find the minimum element | arr . sort ( ) NEW_LINE min = arr [ 0 ] NEW_LINE max = 0 NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE total = num + min NEW_LINE DEDENT |
finding the number to divide | for j in range ( 2 , num + 1 ) : NEW_LINE INDENT if ( num % j == 0 ) : NEW_LINE INDENT d = j NEW_LINE now = ( num // d ) + ( min * d ) NEW_LINE DEDENT DEDENT |
Checking to what instance the sum has decreased | reduce = total - now NEW_LINE |
getting the max difference | if ( reduce > max ) : NEW_LINE INDENT max = reduce NEW_LINE DEDENT print ( sum - max ) NEW_LINE |
Driver Code | arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findMin ( arr , n ) NEW_LINE |
Convert the number to Lth base and print the sequence | def convert_To_Len_th_base ( n , arr , Len , L ) : NEW_LINE |
Sequence is of Length L | for i in range ( L ) : NEW_LINE |
Print the ith element of sequence | print ( arr [ n % Len ] , end = " " ) NEW_LINE n //= Len NEW_LINE print ( ) NEW_LINE |
Print all the permuataions | def printf ( arr , Len , L ) : NEW_LINE |
There can be ( Len ) ^ l permutations | for i in range ( pow ( Len , L ) ) : NEW_LINE |
Convert i to Len th base | convert_To_Len_th_base ( i , arr , Len , L ) NEW_LINE |
Driver code | arr = [ 1 , 2 , 3 ] NEW_LINE Len = len ( arr ) NEW_LINE L = 2 NEW_LINE |
function call | printf ( arr , Len , L ) NEW_LINE |
Function to find the number of permutations possible of the original array to satisfy the given absolute differences | def totalways ( arr , n ) : NEW_LINE |
To store the count of each a [ i ] in a map | cnt = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt [ arr [ i ] ] = cnt . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT |
if n is odd | if ( n % 2 == 1 ) : NEW_LINE INDENT start , endd = 0 , n - 1 NEW_LINE DEDENT |
check the count of each whether it satisfy the given criteria or not | for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE DEDENT |
there is only 1 way for middle element . | if ( cnt [ i ] != 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE |
for others there are 2 ways . | if ( cnt [ i ] != 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
now find total ways | ways = 1 NEW_LINE start = 2 NEW_LINE endd = n - 1 NEW_LINE for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT ways = ways * 2 NEW_LINE DEDENT return ways NEW_LINE |
When n is even . | elif ( n % 2 == 0 ) : NEW_LINE |
there will be no middle element so for each a [ i ] there will be 2 ways | start = 1 NEW_LINE endd = n - 1 NEW_LINE for i in range ( 1 , endd + 1 , 2 ) : NEW_LINE INDENT if ( cnt [ i ] != 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ways = 1 NEW_LINE for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT ways = ways * 2 NEW_LINE DEDENT return ways NEW_LINE |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.