text
stringlengths
1
636
code
stringlengths
8
1.89k
Print the binary string for i in range ( 1 , K ) : print ( bit [ i ] )
bit = bin ( bit ) . replace ( "0b " , " " ) NEW_LINE print ( bit [ 1 : K + 1 ] ) NEW_LINE
Given array
arr = [ 1 , 6 , 1 ] NEW_LINE
Size of the array
N = len ( arr ) NEW_LINE
Given K
K = 8 NEW_LINE constructBinaryString ( arr , N , K ) NEW_LINE
Function to check if it is possible to reach destination in a single move by a rook
def check ( current_row , current_col , destination_row , destination_col ) : NEW_LINE INDENT if ( current_row == destination_row ) : NEW_LINE INDENT return ( " POSSIBLE " ) NEW_LINE DEDENT elif ( current_col == destination_col ) : NEW_LINE INDENT return ( " POSSIBLE " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " NOT ▁ POSSIBLE " ) NEW_LINE DEDENT DEDENT
Given arrays
current_row = 8 NEW_LINE current_col = 8 NEW_LINE destination_row = 8 NEW_LINE destination_col = 4 NEW_LINE output = check ( current_row , current_col , destination_row , destination_col ) NEW_LINE print ( output ) NEW_LINE
Base case
if ( X == 0 ) : NEW_LINE
If N is divisible by M
if ( N % M == 0 ) : NEW_LINE INDENT global res NEW_LINE res = N NEW_LINE return True NEW_LINE DEDENT return False NEW_LINE
global variable to store result
res = - 1 NEW_LINE def isDiv ( N , X , M ) : NEW_LINE
Iterate over the range [ 0 , 9 ]
for i in range ( 10 ) : NEW_LINE
if N is Divisible by M upon appending X digits
if ( isDiv ( N * 10 + i , X - 1 , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , X = 4 , 50 , 2 NEW_LINE DEDENT
Stores the number by appending X digits on the right side of N
if ( isDiv ( N , X , M ) ) : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT
Function to count minimum moves
def minimumMoves ( arr , N ) : NEW_LINE
Stores sum of given array
sum = 0 NEW_LINE
Stores maximum array element
maxelement = - 1 NEW_LINE
Base Case
if ( N == 2 ) : NEW_LINE
If N is 2 , the answer will always be 0
print ( 0 , end = " " ) NEW_LINE
Traverse the array
for i in range ( N ) : NEW_LINE
Calculate sum of the array
sum += arr [ i ] NEW_LINE
Finding maximum element
maxelement = max ( maxelement , arr [ i ] ) NEW_LINE
Calculate ceil ( sum / N - 1 )
K = ( sum + N - 2 ) // ( N - 1 ) NEW_LINE
If k is smaller than maxelement
K = max ( maxelement , K ) NEW_LINE
Final sum - original sum
ans = K * ( N - 1 ) - sum NEW_LINE
Print the minimum number of increments required
print ( ans ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array
arr = [ 2 , 3 , 7 ] NEW_LINE
Size of given array
N = 3 NEW_LINE
Function Call
minimumMoves ( arr , N ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 NEW_LINE printSubsequence ( N ) NEW_LINE DEDENT
Function to check if the required array can be generated or not
def array_divisbleby_k ( N , K ) : NEW_LINE
To check if divisor exists
flag = False NEW_LINE
To store divisiors of K
d1 , d2 = 0 , 0 NEW_LINE
Check if K is prime or not
for i in range ( 2 , int ( K ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( K % i == 0 ) : NEW_LINE INDENT flag = True NEW_LINE d1 = i NEW_LINE d2 = K // i NEW_LINE break NEW_LINE DEDENT DEDENT
If array can be generated
if ( flag ) : NEW_LINE
Print d1 and d2 alternatively
for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 1 ) : NEW_LINE INDENT print ( d2 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( d1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE
No such array can be generated
print ( - 1 ) NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE
Given N and K
N = 5 NEW_LINE K = 21 NEW_LINE
Function Call
array_divisbleby_k ( N , K ) NEW_LINE
Python3 program for the above approach
import sys NEW_LINE sys . setrecursionlimit ( 1500 ) NEW_LINE
Function to add an edge to graph
def addEdge ( u , v ) : NEW_LINE INDENT global adj NEW_LINE adj [ u ] . append ( v ) NEW_LINE adj [ v ] . append ( u ) NEW_LINE DEDENT
Function to perform DFS traversal on the graph recursively from a given vertex u
def DFS ( u , fre , S ) : NEW_LINE INDENT global visited , adj , cnt NEW_LINE DEDENT
Visit the current vertex
visited [ u ] = 1 NEW_LINE
Total number of nodes in this component
cnt += 1 NEW_LINE
Increment the frequency of u
fre [ ord ( S [ u ] ) - ord ( ' a ' ) ] += 1 NEW_LINE for i in adj [ u ] : NEW_LINE INDENT if ( visited [ i ] == 0 ) : NEW_LINE INDENT DFS ( i , fre , S ) NEW_LINE DEDENT DEDENT
Function for finding the minimum number changes required in given string
def minimumOperations ( S , m ) : NEW_LINE INDENT global adj , visited , cnt NEW_LINE total , N = 0 , len ( S ) NEW_LINE DEDENT
Form the edges according to the given conditions
for i in range ( N ) : NEW_LINE INDENT addEdge ( i , N - i - 1 ) NEW_LINE addEdge ( N - i - 1 , i ) NEW_LINE DEDENT for i in range ( N - m ) : NEW_LINE INDENT addEdge ( i , i + m ) NEW_LINE addEdge ( i + m , i ) NEW_LINE DEDENT
Find minimum number of operations
for i in range ( N ) : NEW_LINE
Frequency array for finding the most frequent character
if ( not visited [ i ] ) : NEW_LINE
Frequency array for finding the most frequent character
fre = [ 0 ] * 26 NEW_LINE cnt , maxx = 0 , - 1 NEW_LINE DFS ( i , fre , S ) NEW_LINE
Finding most frequent character
for j in range ( 26 ) : NEW_LINE INDENT maxx = max ( maxx , fre [ j ] ) NEW_LINE DEDENT
Change rest of the characters to most frequent one
total += cnt - maxx NEW_LINE
Print total number of changes
print ( total ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT adj = [ [ ] for i in range ( 101 ) ] NEW_LINE visited , cnt = [ 0 for i in range ( 101 ) ] , 0 NEW_LINE S = " abaaba " NEW_LINE K = 2 NEW_LINE DEDENT
Function Call
minimumOperations ( S , K ) NEW_LINE
Function to display the valid matrix
def Print ( arr , n , m ) : NEW_LINE
Traverse the matrix
for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT a = arr [ i ] [ j ] NEW_LINE DEDENT DEDENT
If the current cell is a free space and is even - indexed
if ( ( i + j ) % 2 == 0 and a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '1' NEW_LINE DEDENT
If the current cell is a free space and is odd - indexed
elif ( a == ' F ' ) : NEW_LINE INDENT arr [ i ] [ j ] = '2' NEW_LINE DEDENT
Print the matrix
for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT
Given N and M
n , m = 4 , 4 NEW_LINE
Given matrix
arr = [ [ ' F ' , ' F ' , ' F ' , ' F ' ] , [ ' F ' , ' O ' , ' F ' , ' F ' ] , [ ' F ' , ' F ' , ' O ' , ' F ' ] , [ ' F ' , ' F ' , ' F ' , ' F ' ] ] NEW_LINE
Function call
Print ( arr , n , m ) NEW_LINE
Function that finds lexicographically smallest after removing the duplicates from the given string
def removeDuplicateLetters ( s ) : NEW_LINE
Stores the frequency of characters
cnt = [ 0 ] * 26 NEW_LINE
Mark visited characters
vis = [ 0 ] * 26 NEW_LINE n = len ( s ) NEW_LINE
Stores count of each character
for i in s : NEW_LINE INDENT cnt [ ord ( i ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT
Stores the resultant string
res = [ ] NEW_LINE for i in range ( n ) : NEW_LINE
Decrease the count of current character
cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE
If character is not already in answer
if ( not vis [ ord ( s [ i ] ) - ord ( ' a ' ) ] ) : NEW_LINE
Last character > S [ i ] and its count > 0
while ( len ( res ) > 0 and res [ - 1 ] > s [ i ] and cnt [ ord ( res [ - 1 ] ) - ord ( ' a ' ) ] > 0 ) : NEW_LINE
Mark letter unvisited
vis [ ord ( res [ - 1 ] ) - ord ( ' a ' ) ] = 0 NEW_LINE del res [ - 1 ] NEW_LINE
Add s [ i ] in res and mark it visited
res += s [ i ] NEW_LINE vis [ ord ( s [ i ] ) - ord ( ' a ' ) ] = 1 NEW_LINE
Return the resultant string
return " " . join ( res ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given S
S = " acbc " NEW_LINE
Function Call
print ( removeDuplicateLetters ( S ) ) NEW_LINE
Function to count pairs having product equal to a power of 2
def countPairs ( arr , N ) : NEW_LINE
Stores count of array elements which are power of 2
countPowerof2 = 0 NEW_LINE for i in range ( N ) : NEW_LINE
If array element contains only one set bit
if ( bin ( arr [ i ] ) . count ( '1' ) == 1 ) : NEW_LINE
Increase count of powers of 2
countPowerof2 += 1 NEW_LINE
Count required number of pairs
desiredPairs = ( countPowerof2 * ( countPowerof2 - 1 ) ) // 2 NEW_LINE
Print the required number of pairs
print ( desiredPairs ) NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE
Given array
arr = [ 2 , 4 , 7 , 2 ] NEW_LINE
Size of the array
N = len ( arr ) NEW_LINE
Function call
countPairs ( arr , N ) NEW_LINE
Python3 program for the above approach
import math NEW_LINE
Maximum value of N
MAXN = 1000000 NEW_LINE
Stores at each indices if given number is prime or not
is_prime = [ 0 ] * MAXN NEW_LINE
Stores count_of_primes
count_of_primes = [ 0 ] * MAXN NEW_LINE
Function to generate primes using Sieve of Eratsothenes
def sieve ( ) : NEW_LINE INDENT for i in range ( 3 , MAXN , 2 ) : NEW_LINE DEDENT
Assume all odds are primes
is_prime [ i ] = 1 NEW_LINE for i in range ( 3 , int ( math . sqrt ( MAXN ) ) , 2 ) : NEW_LINE
If a prime is encountered
if is_prime [ i ] : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE DEDENT
Mark all its multiples as non - prime
is_prime [ j ] = 0 NEW_LINE is_prime [ 2 ] = 1 NEW_LINE
Count primes <= MAXN
for i in range ( 1 , MAXN ) : NEW_LINE INDENT count_of_primes [ i ] = ( count_of_primes [ i - 1 ] + is_prime [ i ] ) NEW_LINE DEDENT
Function to calculate ( x ^ y ) % p in O ( log y )
def power ( x , y , p ) : NEW_LINE INDENT result = 1 NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if y & 1 == 1 : NEW_LINE INDENT result = ( result * x ) % p NEW_LINE DEDENT x = ( x * x ) % p NEW_LINE y >>= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT
Utility function to count the number of ways N ! can be split into co - prime factors
def numberOfWays ( N ) : NEW_LINE INDENT count = count_of_primes [ N ] - 1 NEW_LINE mod = 1000000007 NEW_LINE answer = power ( 2 , count , mod ) NEW_LINE if N == 1 : NEW_LINE INDENT answer = 0 NEW_LINE DEDENT print ( answer ) NEW_LINE DEDENT
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE