text
stringlengths
1
636
code
stringlengths
8
1.89k
Traversing the string
for i in range ( len ( num ) ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT proOdd = proOdd * int ( num [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT proEven = proEven * int ( num [ i ] ) NEW_LINE DEDENT DEDENT if ( proOdd == proEven ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4324 NEW_LINE getResult ( n ) NEW_LINE DEDENT
Function to return the sum of digits of x
def sumOfDigits ( x ) : NEW_LINE INDENT sum = 0 NEW_LINE while x != 0 : NEW_LINE INDENT sum += x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT
Function to return the count of required numbers
def countNumbers ( l , r ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE DEDENT
If i is divisible by 2 and sum of digits of i is divisible by 3
if i % 2 == 0 and sumOfDigits ( i ) % 3 == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT
Return the required count
return count NEW_LINE
Driver code
l = 1000 ; r = 6000 NEW_LINE print ( countNumbers ( l , r ) ) NEW_LINE
Function to find the sum of minimum of all subarrays
def findMinSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] * ( n - i ) NEW_LINE DEDENT return sum NEW_LINE DEDENT
Driver code
arr = [ 3 , 5 , 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinSum ( arr , n ) ) NEW_LINE
Function to return the max length of the sub - array that have the maximum average ( average value of the elements )
def maxLenSubArr ( a , n ) : NEW_LINE INDENT cm , Max = 1 , 0 NEW_LINE DEDENT
Finding the maximum value
for i in range ( 0 , n ) : NEW_LINE INDENT if a [ i ] > Max : NEW_LINE INDENT Max = a [ i ] NEW_LINE DEDENT DEDENT i = 0 NEW_LINE while i < n - 1 : NEW_LINE INDENT count = 1 NEW_LINE DEDENT
If consecutive maximum found
if a [ i ] == a [ i + 1 ] and a [ i ] == Max : NEW_LINE
Find the max length of consecutive max
for j in range ( i + 1 , n ) : NEW_LINE INDENT if a [ j ] == Max : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if count > cm : NEW_LINE INDENT cm = count NEW_LINE DEDENT else : NEW_LINE i += 1 NEW_LINE i += 1 NEW_LINE return cm NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 1 , 6 , 6 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxLenSubArr ( arr , n ) ) NEW_LINE DEDENT
Function to return the minimized sum
def minSum ( arr , n , x ) : NEW_LINE INDENT Sum = 0 NEW_LINE DEDENT
To store the largest element from the array which is divisible by x
largestDivisible , minimum = - 1 , arr [ 0 ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE
Sum of array elements before performing any operation
Sum += arr [ i ] NEW_LINE
If current element is divisible by x and it is maximum so far
if ( arr [ i ] % x == 0 and largestDivisible < arr [ i ] ) : NEW_LINE INDENT largestDivisible = arr [ i ] NEW_LINE DEDENT
Update the minimum element
if arr [ i ] < minimum : NEW_LINE INDENT minimum = arr [ i ] NEW_LINE DEDENT
If no element can be reduced then there 's no point in performing the operation as we will end up increasing the sum when an element is multiplied by x
if largestDivisible == - 1 : NEW_LINE INDENT return Sum NEW_LINE DEDENT
Subtract the chosen elements from the sum and then add their updated values
sumAfterOperation = ( Sum - minimum - largestDivisible + ( x * minimum ) + ( largestDivisible // x ) ) NEW_LINE
Return the minimized sum
return min ( Sum , sumAfterOperation ) NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 5 , 5 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE x = 3 NEW_LINE print ( minSum ( arr , n , x ) ) NEW_LINE DEDENT
Function to return the maximum bitwise AND possible among all the possible pairs
def maxAND ( L , R ) : NEW_LINE
If there is only a single value in the range [ L , R ]
if ( L == R ) : NEW_LINE INDENT return L ; NEW_LINE DEDENT
If there are only two values in the range [ L , R ]
elif ( ( R - L ) == 1 ) : NEW_LINE INDENT return ( R & L ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ( ( R - 1 ) & R ) > ( ( R - 2 ) & ( R - 1 ) ) ) : NEW_LINE INDENT return ( ( R - 1 ) & R ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( R - 2 ) & ( R - 1 ) ) ; NEW_LINE DEDENT DEDENT
Driver code
L = 1 ; NEW_LINE R = 632 ; NEW_LINE print ( maxAND ( L , R ) ) ; NEW_LINE
Function to check whether the number is a special prime or not
def checkSpecialPrime ( sieve , num ) : NEW_LINE
While number is not equal to zero
while ( num ) : NEW_LINE
If the number is not prime return false .
if ( sieve [ num ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT
Else remove the last digit by dividing the number by 10.
num = int ( num / 10 ) NEW_LINE
If the number has become zero then the number is special prime , hence return true
return True NEW_LINE
Function to find the Smallest Special Prime which is greater than or equal to a given number
def findSpecialPrime ( N ) : NEW_LINE INDENT sieve = [ True for i in range ( N * 10 + 1 ) ] NEW_LINE sieve [ 0 ] = False NEW_LINE sieve [ 1 ] = False NEW_LINE DEDENT
sieve for finding the Primes
for i in range ( 2 , N * 10 + 1 ) : NEW_LINE INDENT if ( sieve [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N * 10 + 1 , i ) : NEW_LINE INDENT sieve [ j ] = False NEW_LINE DEDENT DEDENT DEDENT
There is always an answer possible
while ( True ) : NEW_LINE
Checking if the number is a special prime or not
if ( checkSpecialPrime ( sieve , N ) ) : NEW_LINE
If yes print the number and break the loop .
print ( N ) NEW_LINE break NEW_LINE
Else increment the number .
else : NEW_LINE INDENT N += 1 NEW_LINE DEDENT
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 379 NEW_LINE findSpecialPrime ( N ) NEW_LINE N = 100 NEW_LINE findSpecialPrime ( N ) NEW_LINE DEDENT
Python3 implementation of the approach
import sys NEW_LINE
Function to return the minimum number of moves required to make n divisible by 25
def minMoves ( n ) : NEW_LINE
Convert number into string
s = str ( n ) ; NEW_LINE
To store required answer
ans = sys . maxsize ; NEW_LINE
Length of the string
len1 = len ( s ) ; NEW_LINE
To check all possible pairs
for i in range ( len1 ) : NEW_LINE INDENT for j in range ( len1 ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT DEDENT DEDENT
Make a duplicate string
t = s ; NEW_LINE cur = 0 ; NEW_LINE
Number of swaps required to place ith digit in last position
list1 = list ( t ) ; NEW_LINE for k in range ( i , len1 - 1 ) : NEW_LINE INDENT e = list1 [ k ] ; NEW_LINE list1 [ k ] = list1 [ k + 1 ] ; NEW_LINE list1 [ k + 1 ] = e ; NEW_LINE cur += 1 ; NEW_LINE DEDENT t = ' ' . join ( list1 ) ; NEW_LINE
Number of swaps required to place jth digit in 2 nd last position
list1 = list ( t ) ; NEW_LINE for k in range ( j - ( j > i ) , len1 - 2 ) : NEW_LINE INDENT e = list1 [ k ] ; NEW_LINE list1 [ k ] = list1 [ k + 1 ] ; NEW_LINE list1 [ k + 1 ] = e ; NEW_LINE cur += 1 ; NEW_LINE DEDENT t = ' ' . join ( list1 ) ; NEW_LINE
Find first non zero digit
pos = - 1 ; NEW_LINE for k in range ( len1 ) : NEW_LINE INDENT if ( t [ k ] != '0' ) : NEW_LINE INDENT pos = k ; NEW_LINE break ; NEW_LINE DEDENT DEDENT
Place first non zero digit in the first position
for k in range ( pos , 0 , - 1 ) : NEW_LINE INDENT e = list1 [ k ] ; NEW_LINE list1 [ k ] = list1 [ k + 1 ] ; NEW_LINE list1 [ k + 1 ] = e ; NEW_LINE cur += 1 ; NEW_LINE DEDENT t = ' ' . join ( list1 ) ; NEW_LINE
Convert string to number
nn = int ( t ) ; NEW_LINE
If this number is divisible by 25 then cur is one of the possible answer
if ( nn % 25 == 0 ) : NEW_LINE INDENT ans = min ( ans , cur ) ; NEW_LINE DEDENT
If not possible
if ( ans == sys . maxsize ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return ans ; NEW_LINE
Driver code
n = 509201 ; NEW_LINE print ( minMoves ( n ) ) ; NEW_LINE
Function to return the required number
def getMaxNum ( a , b , c ) : NEW_LINE
If b % c = 0 then b is the required number
if ( b % c == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT
Else get the maximum multiple of c smaller than b
x = ( ( b // c ) * c ) NEW_LINE if ( x >= a and x <= b ) : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT
Driver code
a , b , c = 2 , 10 , 3 NEW_LINE print ( getMaxNum ( a , b , c ) ) NEW_LINE
Function to return the number of pairs ( x , y ) such that x < y
def getPairs ( a ) : NEW_LINE
Length of the array
n = len ( a ) NEW_LINE
Calculate the number of valid pairs
count = ( n * ( n - 1 ) ) // 2 NEW_LINE
Return the count of valid pairs
return count NEW_LINE
Driver code
a = [ 2 , 4 , 3 , 1 ] NEW_LINE print ( getPairs ( a ) ) NEW_LINE
Function to return the count of total positions the Bishop can visit in a single move
def countSquares ( row , column ) : NEW_LINE
Count top left squares
topLeft = min ( row , column ) - 1 NEW_LINE
Count bottom right squares
bottomRight = 8 - max ( row , column ) NEW_LINE
Count top right squares
topRight = min ( row , 9 - column ) - 1 NEW_LINE
Count bottom left squares
bottomLeft = 8 - max ( row , 9 - column ) NEW_LINE
Return total count
return ( topLeft + topRight + bottomRight + bottomLeft ) NEW_LINE
Bishop 's Position
row = 4 NEW_LINE column = 4 NEW_LINE print ( countSquares ( row , column ) ) NEW_LINE
Function that return true if the Bishop can take down the pawn
def canTakeDown ( bishopX , bishopY , pawnX , pawnY ) : NEW_LINE
If pawn is at angle 45 or 225 degree from bishop 's Position
if ( pawnX - bishopX == pawnY - bishopY ) : NEW_LINE INDENT return True NEW_LINE DEDENT
If pawn is at angle 135 or 315 degree from bishop 's Position
elif ( - pawnX + bishopX == pawnY - bishopY ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT
Bishop 's Position
bishopX = 5 NEW_LINE bishopY = 5 NEW_LINE
Pawn 's Position
pawnX = 1 NEW_LINE pawnY = 1 NEW_LINE if ( canTakeDown ( bishopX , bishopY , pawnX , pawnY ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Python3 program to find maximum number moves possible
N = 1000005 NEW_LINE
To store number of prime factors of each number
primeFactors = [ 0 ] * N ; NEW_LINE
Function to find number of prime factors of each number
def findPrimeFactors ( ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE DEDENT
if i is a prime number
if ( primeFactors [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE DEDENT
increase value by one from it 's preveious multiple
primeFactors [ j ] = primeFactors [ j // i ] + 1 ; NEW_LINE
make prefix sum this will be helpful for multiple test cases
for i in range ( 1 , N ) : NEW_LINE INDENT primeFactors [ i ] += primeFactors [ i - 1 ] ; NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Generate primeFactors array
findPrimeFactors ( ) ; NEW_LINE a = 6 ; b = 3 ; NEW_LINE
required answer
print ( primeFactors [ a ] - primeFactors [ b ] ) ; NEW_LINE
Python 3 implementation of the above approach
from math import floor , pow NEW_LINE import sys NEW_LINE
Function to return digit sum
def digitSum ( n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT ans += n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return ans NEW_LINE DEDENT
Function to find out the smallest integer
def findInt ( n , m ) : NEW_LINE INDENT minDigit = floor ( m / 9 ) NEW_LINE DEDENT
Start of the iterator ( Smallest multiple of n )
start = ( int ( pow ( 10 , minDigit ) ) - int ( pow ( 10 , minDigit ) ) % n ) NEW_LINE while ( start < sys . maxsize ) : NEW_LINE INDENT if ( digitSum ( start ) == m ) : NEW_LINE INDENT return start NEW_LINE DEDENT else : NEW_LINE INDENT start += n NEW_LINE DEDENT DEDENT return - 1 NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE m = 32 NEW_LINE print ( findInt ( n , m ) ) NEW_LINE DEDENT
Python 3 implementation of the above approach
from math import sqrt NEW_LINE
Function to find the smallest divisor
def smallestDivisor ( n ) : NEW_LINE INDENT mx = int ( sqrt ( n ) ) NEW_LINE for i in range ( 2 , mx + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return n NEW_LINE DEDENT
Function to find the maximum sum
def maxSum ( n ) : NEW_LINE INDENT res = n NEW_LINE while ( n > 1 ) : NEW_LINE INDENT divi = smallestDivisor ( n ) NEW_LINE n = int ( n / divi ) NEW_LINE res += n NEW_LINE DEDENT return res NEW_LINE DEDENT
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 34 NEW_LINE print ( maxSum ( n ) ) NEW_LINE DEDENT
Function that returns true if all the elements of the array can be made equal with the given operation
def isPossible ( n , k , arr ) : NEW_LINE
To store the sum of the array elements and the maximum element from the array
sum = arr [ 0 ] NEW_LINE maxVal = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE maxVal = max ( maxVal , arr [ i ] ) NEW_LINE DEDENT if ( int ( maxVal ) > int ( ( sum + k ) / n ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 8 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isPossible ( n , k , arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
Python3 implementation of the approach
from math import * NEW_LINE
Function to return the maximum value of ( x + y + z ) such that ( ax + by + cz = n )
def maxResult ( n , a , b , c ) : NEW_LINE INDENT maxVal = 0 NEW_LINE DEDENT
i represents possible values of a * x
for i in range ( 0 , n + 1 , a ) : NEW_LINE
j represents possible values of b * y
for j in range ( 0 , n - i + 1 , b ) : NEW_LINE INDENT z = ( n - ( i + j ) ) / c NEW_LINE DEDENT