text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Python3 implementation of the above approach
|
def maxPresum ( a , b ) : NEW_LINE
|
Stores the maximum prefix sum of the array A [ ]
|
X = max ( a [ 0 ] , 0 ) NEW_LINE
|
Traverse the array A [ ]
|
for i in range ( 1 , len ( a ) ) : NEW_LINE INDENT a [ i ] += a [ i - 1 ] NEW_LINE X = max ( X , a [ i ] ) NEW_LINE DEDENT
|
Stores the maximum prefix sum of the array B [ ]
|
Y = max ( b [ 0 ] , 0 ) NEW_LINE
|
Traverse the array B [ ]
|
for i in range ( 1 , len ( b ) ) : NEW_LINE INDENT b [ i ] += b [ i - 1 ] NEW_LINE Y = max ( Y , b [ i ] ) NEW_LINE DEDENT return X + Y NEW_LINE
|
Driver code
|
A = [ 2 , - 1 , 4 , - 5 ] NEW_LINE B = [ 4 , - 3 , 12 , 4 , - 3 ] NEW_LINE print ( maxPresum ( A , B ) ) NEW_LINE
|
Python3 program for the above approach
|
import math NEW_LINE
|
Function to check if N can be represented as sum of two perfect cubes or not
|
def sumOfTwoCubes ( n ) : NEW_LINE INDENT lo = 1 NEW_LINE hi = round ( math . pow ( n , 1 / 3 ) ) NEW_LINE while ( lo <= hi ) : NEW_LINE INDENT curr = ( lo * lo * lo + hi * hi * hi ) NEW_LINE if ( curr == n ) : NEW_LINE DEDENT DEDENT
|
If it is same return true ;
|
return True NEW_LINE if ( curr < n ) : NEW_LINE
|
If the curr smaller than n increment the lo
|
lo += 1 NEW_LINE else : NEW_LINE
|
If the curr is greater than curr decrement the hi
|
hi -= 1 NEW_LINE return False NEW_LINE
|
Driver Code
|
N = 28 NEW_LINE
|
Function call to check if N can be represented as sum of two perfect cubes or not
|
if ( sumOfTwoCubes ( N ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT
|
Python3 program for the above approach
|
sieve = [ 1 ] * ( 1000000 + 1 ) NEW_LINE
|
Function to generate all prime numbers upto 10 ^ 6
|
def sieveOfPrimes ( ) : NEW_LINE
|
Initialize sieve [ ] as 1
|
global sieve NEW_LINE N = 1000000 NEW_LINE
|
Iterate over the range [ 2 , N ]
|
for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if i * i > N : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT
|
If current element is non - prime
|
if ( sieve [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT
|
Make all multiples of i as 0
|
for j in range ( i * i , N + 1 , i ) : NEW_LINE INDENT sieve [ j ] = 0 NEW_LINE DEDENT
|
Function to construct an array A [ ] satisfying the given conditions
|
def getArray ( arr , N ) : NEW_LINE INDENT global sieve NEW_LINE DEDENT
|
Stores the resultant array
|
A = [ 0 ] * N NEW_LINE
|
Stores all prime numbers
|
v = [ ] NEW_LINE
|
Sieve of Erastosthenes
|
sieveOfPrimes ( ) NEW_LINE for i in range ( 2 , int ( 1e5 ) + 1 ) : NEW_LINE
|
Append the integer i if it is a prime
|
if ( sieve [ i ] ) : NEW_LINE INDENT v . append ( i ) NEW_LINE DEDENT
|
Indicates current position in list of prime numbers
|
j = 0 NEW_LINE
|
Traverse the array arr [ ]
|
for i in range ( N ) : NEW_LINE INDENT ind = arr [ i ] NEW_LINE DEDENT
|
If already filled with another prime number
|
if ( A [ i ] != 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT
|
If A [ i ] is not filled but A [ ind ] is filled
|
elif ( A [ ind ] != 0 ) : NEW_LINE
|
Store A [ i ] = A [ ind ]
|
A [ i ] = A [ ind ] NEW_LINE
|
If none of them were filled
|
else : NEW_LINE
|
To make sure A [ i ] does not affect other values , store next prime number
|
prime = v [ j ] NEW_LINE A [ i ] = prime NEW_LINE A [ ind ] = A [ i ] NEW_LINE j += 1 NEW_LINE
|
Print the resultant array
|
for i in range ( N ) : NEW_LINE INDENT print ( A [ i ] , end = " ▁ " ) NEW_LINE DEDENT
|
Driver Code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 2 , 3 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE DEDENT
|
Function Call
|
getArray ( arr , N ) NEW_LINE
|
Function to find Nth number in base 9
|
def findNthNumber ( N ) : NEW_LINE
|
Stores the Nth number
|
result = 0 NEW_LINE p = 1 NEW_LINE
|
Iterate while N is greater than 0
|
while ( N > 0 ) : NEW_LINE
|
Update result
|
result += ( p * ( N % 9 ) ) NEW_LINE
|
Divide N by 9
|
N = N // 9 NEW_LINE
|
Multiply p by 10
|
p = p * 10 NEW_LINE
|
Return result
|
return result NEW_LINE
|
Driver Code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 9 NEW_LINE print ( findNthNumber ( N ) ) NEW_LINE DEDENT
|
Python3 implementation of the approach
|
import math NEW_LINE
|
Function to check if the integer A is a rotation of the integer B
|
def check ( A , B ) : NEW_LINE INDENT if ( A == B ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT
|
Stores the count of digits in A
|
dig1 = math . floor ( math . log10 ( A ) + 1 ) NEW_LINE
|
Stores the count of digits in B
|
dig2 = math . floor ( math . log10 ( B ) + 1 ) NEW_LINE
|
If dig1 not equal to dig2
|
if ( dig1 != dig2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT temp = A NEW_LINE while ( True ) : NEW_LINE
|
Stores position of first digit
|
power = pow ( 10 , dig1 - 1 ) NEW_LINE
|
Stores the first digit
|
firstdigit = A // power NEW_LINE
|
Rotate the digits of the integer
|
A = A - firstdigit * power NEW_LINE A = A * 10 + firstdigit NEW_LINE
|
If A is equal to B
|
if ( A == B ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT
|
If A is equal to the initial value of integer A
|
if ( A == temp ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT
|
Driver code
|
A , B = 967 , 679 NEW_LINE if ( check ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
|
Function to count the number of unique quadruples from an array that satisfies the given condition
|
def sameProductQuadruples ( nums , N ) : NEW_LINE
|
Hashmap to store the product of pairs
|
umap = { } ; NEW_LINE
|
Store the count of required quadruples
|
res = 0 ; NEW_LINE
|
Traverse the array arr [ ] and generate all possible pairs
|
for i in range ( N ) : NEW_LINE INDENT for j in range ( i + 1 , N ) : NEW_LINE DEDENT
|
Store their product
|
prod = nums [ i ] * nums [ j ] ; NEW_LINE if prod in umap : NEW_LINE
|
Pair ( a , b ) can be used to generate 8 unique permutations with another pair ( c , d )
|
res += 8 * umap [ prod ] ; NEW_LINE
|
Increment umap [ prod ] by 1
|
umap [ prod ] += 1 ; NEW_LINE else : NEW_LINE umap [ prod ] = 1 NEW_LINE
|
Print the result
|
print ( res ) ; NEW_LINE
|
Driver Code
|
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 6 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE sameProductQuadruples ( arr , N ) ; NEW_LINE DEDENT
|
Python3 implementation of the above Approach
|
MOD = 1000000007 NEW_LINE
|
Iterative Function to calculate ( x ^ y ) % p in O ( log y )
|
def power ( x , y , p = MOD ) : NEW_LINE
|
Initialize Result
|
res = 1 NEW_LINE
|
Update x if x >= MOD to avoid multiplication overflow
|
x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE
|
If y is odd , multiply x with result
|
if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT
|
y = y / 2
|
y = y >> 1 NEW_LINE
|
Change x to x ^ 2
|
x = ( x * x ) % p NEW_LINE return res NEW_LINE
|
Utility function to find the Total Number of Ways
|
def totalWays ( N , M ) : NEW_LINE
|
Number of Even Indexed Boxes
|
X = N // 2 NEW_LINE
|
Number of partitions of Even Indexed Boxes
|
S = ( X * ( X + 1 ) ) % MOD NEW_LINE
|
Number of ways to distribute objects
|
print ( power ( S , M , MOD ) ) NEW_LINE
|
Driver Code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE
|
N = number of boxes M = number of distinct objects
|
N , M = 5 , 2 NEW_LINE
|
Function call to get Total Number of Ways
|
totalWays ( N , M ) NEW_LINE
|
Function to check if the graph constructed from given array contains a cycle or not
|
def isCycleExists ( arr , N ) : NEW_LINE INDENT valley = 0 NEW_LINE DEDENT
|
Traverse the array
|
for i in range ( 1 , N ) : NEW_LINE
|
If arr [ i ] is less than arr [ i - 1 ] and arr [ i ]
|
if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT print ( " No " ) NEW_LINE
|
Driver Code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE
|
Given array
|
arr = [ 1 , 3 , 2 , 4 , 5 ] NEW_LINE
|
Size of the array
|
N = len ( arr ) NEW_LINE isCycleExists ( arr , N ) NEW_LINE
|
Function to maximize the first array element
|
def getMax ( arr , N , K ) : NEW_LINE
|
Traverse the array
|
for i in range ( 1 , N , 1 ) : NEW_LINE
|
Initialize cur_val to a [ i ]
|
cur_val = arr [ i ] NEW_LINE
|
If all operations are not over yet
|
while ( K >= i ) : NEW_LINE
|
If current value is greater than zero
|
if ( cur_val > 0 ) : NEW_LINE
|
Incrementing first element of array by 1
|
arr [ 0 ] = arr [ 0 ] + 1 NEW_LINE
|
Decrementing current value of array by 1
|
cur_val = cur_val - 1 NEW_LINE
|
Decrementing number of operations by i
|
K = K - i NEW_LINE
|
If current value is zero , then break
|
else : NEW_LINE INDENT break NEW_LINE DEDENT
|
Print first array element
|
print ( arr [ 0 ] ) NEW_LINE
|
Driver Code
|
if __name__ == ' _ _ main _ _ ' : NEW_LINE
|
Given array
|
arr = [ 1 , 0 , 3 , 2 ] NEW_LINE
|
Size of the array
|
N = len ( arr ) NEW_LINE
|
Given K
|
K = 5 NEW_LINE
|
Prints the maximum possible value of the first array element
|
getMax ( arr , N , K ) NEW_LINE
|
Python3 program of the above approach
|
import sys NEW_LINE
|
Function to find the gcd of the two numbers
|
def gcd ( a , b ) : NEW_LINE INDENT if a == 0 : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT
|
Function to find distinct elements in the array by repeatidely inserting the absolute difference of all possible pairs
|
def DistinctValues ( arr , N ) : NEW_LINE
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.