text
stringlengths
1
636
code
stringlengths
8
1.89k
Condition to check if the character is not present in the B
if ( pos == 0 and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] == inf ) : NEW_LINE INDENT numberOfSubsequences = - 1 NEW_LINE break NEW_LINE DEDENT
Condition to check if there is an element in B matching with character A [ i ] on or next to B [ pos ] given by next [ A [ i ] - ' a ' ] [ pos ]
elif ( pos < sizeOfB and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] < inf ) : NEW_LINE INDENT nextIndex = next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] + 1 NEW_LINE pos = nextIndex NEW_LINE i += 1 NEW_LINE DEDENT
Condition to check if reached at the end of B or no such element exists on or next to A [ pos ] , thus increment number by one and reinitialise pos to zero
else : NEW_LINE INDENT numberOfSubsequences += 1 NEW_LINE pos = 0 NEW_LINE DEDENT return numberOfSubsequences NEW_LINE
Driver Code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " aacbe " NEW_LINE B = " aceab " NEW_LINE print ( findMinimumSubsequences ( A , B ) ) NEW_LINE DEDENT
Python3 program to print Horizontal filling
def horizontalFill ( records , tape , nt ) : NEW_LINE
It is used for checking whether tape is full or not
sum = 0 ; NEW_LINE
It is used for calculating total retrieval time
Retrieval_Time = 0 ; NEW_LINE
It is used for calculating mean retrieval time
current = 0 ; NEW_LINE
vector is used because n number of records can insert in one tape with size constraint
v = [ ] ; NEW_LINE for i in range ( nt ) : NEW_LINE
Null vector obtained to use fresh vector 'v
' NEW_LINE INDENT v . clear ( ) ; NEW_LINE Retrieval_Time = 0 ; NEW_LINE DEDENT
initialize variables to 0 for each iteration
sum = 0 ; NEW_LINE print ( " tape " , i + 1 , " : ▁ [ ▁ " , end = " " ) ; NEW_LINE
sum is used for checking whether i 'th tape is full or not
sum += records [ current ] ; NEW_LINE
check sum less than size of tape
while ( sum <= tape [ i ] ) : NEW_LINE INDENT print ( records [ current ] , end = " ▁ " ) ; NEW_LINE v . append ( records [ current ] ) ; NEW_LINE DEDENT
increment in j for next record
current += 1 ; NEW_LINE sum += records [ current ] ; print ( " ] " , end = " " ) ; NEW_LINE
calculating total retrieval time
for i in range ( len ( v ) ) : NEW_LINE
MRT formula
Retrieval_Time += v [ i ] * ( len ( v ) - i ) ; NEW_LINE
calculating mean retrieval time using formula
Mrt = Retrieval_Time / len ( v ) ; NEW_LINE
v . size ( ) is function of vector is used to get size of vector
print ( " tMRT ▁ : " , Mrt ) ; NEW_LINE
Driver Code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT records = [ 15 , 2 , 8 , 23 , 45 , 50 , 60 , 120 ] ; NEW_LINE tape = [ 25 , 80 , 160 ] ; NEW_LINE DEDENT
store the size of records [ ]
n = len ( records ) ; NEW_LINE
store the size of tapes [ ]
m = len ( tape ) ; NEW_LINE
sorting of an array is required to attain greedy approach of algorithm
records . sort ( ) ; NEW_LINE horizontalFill ( records , tape , m ) ; NEW_LINE
Python program to compute circular convolution of two arrays
MAX_SIZE = 10 ; NEW_LINE
Function to find circular convolution
def convolution ( x , h , n , m ) : NEW_LINE INDENT row_vec = [ 0 ] * MAX_SIZE ; NEW_LINE col_vec = [ 0 ] * MAX_SIZE ; NEW_LINE out = [ 0 ] * MAX_SIZE ; NEW_LINE circular_shift_mat = [ [ 0 for i in range ( MAX_SIZE ) ] for j in range ( MAX_SIZE ) ] ; NEW_LINE DEDENT
Finding the maximum size between the two input sequence sizes
if ( n > m ) : NEW_LINE INDENT maxSize = n ; NEW_LINE DEDENT else : NEW_LINE INDENT maxSize = m ; NEW_LINE DEDENT
Copying elements of x to row_vec and padding zeros if size of x < maxSize
for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT row_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT row_vec [ i ] = x [ i ] ; NEW_LINE DEDENT DEDENT
Copying elements of h to col_vec and padding zeros if size of h is less than maxSize
for i in range ( maxSize ) : NEW_LINE INDENT if ( i >= m ) : NEW_LINE INDENT col_vec [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT col_vec [ i ] = h [ i ] ; NEW_LINE DEDENT DEDENT
Generating 2D matrix of circularly shifted elements
k = 0 ; NEW_LINE d = 0 ; NEW_LINE for i in range ( maxSize ) : NEW_LINE INDENT curIndex = k - d ; NEW_LINE for j in range ( maxSize ) : NEW_LINE INDENT circular_shift_mat [ j ] [ i ] = row_vec [ curIndex % maxSize ] ; NEW_LINE curIndex += 1 ; NEW_LINE DEDENT k = maxSize ; NEW_LINE d += 1 ; NEW_LINE DEDENT
Computing result by matrix multiplication and printing results
for i in range ( maxSize ) : NEW_LINE INDENT for j in range ( maxSize ) : NEW_LINE INDENT out [ i ] += circular_shift_mat [ i ] [ j ] * col_vec [ j ] ; NEW_LINE DEDENT print ( out [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT
Driver program
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = [ 5 , 7 , 3 , 2 ] ; NEW_LINE n = len ( x ) ; NEW_LINE h = [ 1 , 5 ] ; NEW_LINE m = len ( h ) ; NEW_LINE convolution ( x , h , n , m ) ; NEW_LINE DEDENT
Python3 program for the above approach
def max_palindrome ( s , n ) : NEW_LINE INDENT flag = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE DEDENT
To check if there is any string of odd length
if ( len ( s [ i ] ) % 2 != 0 ) : NEW_LINE INDENT flag = 1 ; NEW_LINE DEDENT
If there is at least 1 string of odd length .
if ( flag == 1 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT z = 0 ; o = 0 ; NEW_LINE
If all the strings are of even length .
for i in range ( n ) : NEW_LINE INDENT for j in range ( len ( s [ i ] ) ) : NEW_LINE DEDENT
Count of 0 's in all the strings
if ( s [ i ] [ j ] == '0' ) : NEW_LINE INDENT z += 1 ; NEW_LINE DEDENT
Count of 1 's in all the strings
else : NEW_LINE INDENT o += 1 ; NEW_LINE DEDENT
If z is even and o is even then ans will be N .
if ( o % 2 == 0 and z % 2 == 0 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT
Otherwise ans will be N - 1.
else : NEW_LINE INDENT return n - 1 ; NEW_LINE DEDENT
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE s = [ "1110" , "100110" , "010101" ] ; NEW_LINE print ( max_palindrome ( s , n ) ) ; NEW_LINE DEDENT
Function to return the minimum cost to travel from the first city to the last
def minCost ( cost , n ) : NEW_LINE
To store the total cost
totalCost = 0 NEW_LINE
Start from the first city
boardingBus = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE
If found any city with cost less than that of the previous boarded bus then change the bus
if ( cost [ boardingBus ] > cost [ i ] ) : NEW_LINE
Calculate the cost to travel from the currently boarded bus till the current city
totalCost += ( ( i - boardingBus ) * cost [ boardingBus ] ) NEW_LINE
Update the currently boarded bus
boardingBus = i NEW_LINE
Finally calculate the cost for the last boarding bus till the ( N + 1 ) th city
totalCost += ( ( n - boardingBus ) * cost [ boardingBus ] ) NEW_LINE return totalCost NEW_LINE
Driver code
cost = [ 4 , 7 , 8 , 3 , 4 ] NEW_LINE n = len ( cost ) NEW_LINE print ( minCost ( cost , n ) ) NEW_LINE
Python 3 implementation of the approach
import sys NEW_LINE
Function to return the minimum flips required such that the submatrix from mat [ i ] [ j ] to mat [ i + 1 ] [ j + 1 ] contains all equal elements
def minFlipsSub ( mat , i , j ) : NEW_LINE INDENT cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE if ( mat [ i ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT return min ( cnt0 , cnt1 ) NEW_LINE DEDENT
Function to return the minimum number of slips required such that the matrix contains at least a single submatrix of size 2 * 2 with all equal elements
def minFlips ( mat , r , c ) : NEW_LINE
To store the result
res = sys . maxsize NEW_LINE
For every submatrix of size 2 * 2
for i in range ( r - 1 ) : NEW_LINE INDENT for j in range ( c - 1 ) : NEW_LINE DEDENT
Update the count of flips required for the current submatrix
res = min ( res , minFlipsSub ( mat , i , j ) ) NEW_LINE return res NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ "0101" , "0101" , "0101" ] NEW_LINE r = len ( mat ) NEW_LINE c = len ( mat [ 0 ] ) NEW_LINE print ( minFlips ( mat , r , c ) ) NEW_LINE DEDENT
Function to return the kth element of the Odd - Even sequence of length n
def findK ( n , k ) : NEW_LINE
Finding the index from where the even numbers will be stored
if ( n % 2 == 0 ) : NEW_LINE INDENT pos = n // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos = ( n // 2 ) + 1 ; NEW_LINE DEDENT
Return the kth element
if ( k <= pos ) : NEW_LINE INDENT return ( k * 2 - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( k - pos ) * 2 ) ; NEW_LINE DEDENT
Function to return the count of set bits in the kth number of the odd even sequence of length n
def countSetBits ( n , k ) : NEW_LINE
Required kth number
kth = findK ( n , k ) ; NEW_LINE
Return the count of set bits
return bin ( kth ) . count ( '1' ) ; NEW_LINE
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 18 ; k = 12 ; NEW_LINE print ( countSetBits ( n , k ) ) ; NEW_LINE DEDENT
Function to return the minimum cost to convert str1 to sr2
def minCost ( str1 , str2 , n ) : NEW_LINE INDENT cost = 0 NEW_LINE DEDENT
For every character of str1
for i in range ( n ) : NEW_LINE
If current character is not equal in both the strings
if ( str1 [ i ] != str2 [ i ] ) : NEW_LINE
If the next character is also different in both the strings then these characters can be swapped
if ( i < n - 1 and str1 [ i + 1 ] != str2 [ i + 1 ] ) : NEW_LINE INDENT swap ( str1 [ i ] , str1 [ i + 1 ] ) NEW_LINE cost += 1 NEW_LINE DEDENT
Change the current character
else : NEW_LINE INDENT cost += 1 NEW_LINE DEDENT return cost NEW_LINE
Driver code
if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " abb " NEW_LINE str2 = " bba " NEW_LINE n = len ( str1 ) NEW_LINE print ( minCost ( str1 , str2 , n ) ) NEW_LINE DEDENT
Function to find the required sets
def find_set ( n ) : NEW_LINE
Impossible case
if ( n <= 2 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT
Sum of first n - 1 natural numbers
sum1 = ( n * ( n - 1 ) ) / 2 ; NEW_LINE sum2 = n ; NEW_LINE print ( sum1 , " ▁ " , sum2 ) ; NEW_LINE
Driver code
n = 8 ; NEW_LINE find_set ( n ) ; NEW_LINE
Recursive function to find gcd using euclidean algorithm
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 lcm of two numbers using gcd
def lcm ( n , m ) : NEW_LINE INDENT return ( n * m ) // gcd ( n , m ) ; NEW_LINE DEDENT
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; m = 3 ; k = 5 ; NEW_LINE print ( k // lcm ( n , m ) ) ; NEW_LINE DEDENT
Function to check whether sum of any set of the array element is equal to k or not
def CheckForSequence ( arr , n , k ) : NEW_LINE
Traverse the array from end to start
for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE
if k is greater than arr [ i ] then subtract it from k
if ( k >= arr [ i ] ) : NEW_LINE INDENT k -= arr [ i ] ; NEW_LINE DEDENT
If there is any subsequence whose sum is equal to k
if ( k != 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT else : NEW_LINE INDENT return True ; NEW_LINE DEDENT
Driver code
if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 3 , 7 , 15 , 31 ] ; NEW_LINE n = len ( A ) ; NEW_LINE if ( CheckForSequence ( A , n , 18 ) ) : NEW_LINE INDENT print ( True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( False ) NEW_LINE DEDENT DEDENT
Function to return the maximum sub - array sum after at most x swaps
def SubarraySum ( a , n , x ) : NEW_LINE
To store the required answer
ans = - 10000 NEW_LINE
For all possible intervals
for i in range ( n ) : NEW_LINE for j in range ( i , n ) : NEW_LINE
Keep current ans as zero
curans = 0 NEW_LINE
To store the integers which are not part of the sub - array currently under consideration
pq = [ ] NEW_LINE
To store elements which are part of the sub - array currently under consideration
pq2 = [ ] NEW_LINE
Create two sets
for k in range ( n ) : NEW_LINE if ( k >= i and k <= j ) : NEW_LINE INDENT curans += a [ k ] NEW_LINE pq2 . append ( a [ k ] ) NEW_LINE DEDENT else : NEW_LINE INDENT pq . append ( a [ k ] ) NEW_LINE DEDENT pq . sort ( ) NEW_LINE pq . reverse ( ) NEW_LINE pq2 . sort ( ) NEW_LINE ans = max ( ans , curans ) NEW_LINE
Swap at most X elements
for k in range ( 1 , x + 1 ) : NEW_LINE if ( len ( pq ) == 0 or len ( pq2 ) == 0 or pq2 [ 0 ] >= pq [ 0 ] ) : NEW_LINE INDENT break NEW_LINE DEDENT
Remove the minimum of the taken elements
curans -= pq2 [ 0 ] NEW_LINE pq2 . pop ( 0 ) NEW_LINE
Add maximum of the discarded elements
curans += pq [ 0 ] NEW_LINE pq . pop ( 0 ) NEW_LINE
Update the answer
ans = max ( ans , curans ) NEW_LINE
Return the maximized sub - array sum
return ans NEW_LINE
Driver code
a = [ 5 , - 1 , 2 , 3 , 4 , - 2 , 5 ] NEW_LINE x = 2 ; NEW_LINE n = len ( a ) NEW_LINE print ( SubarraySum ( a , n , x ) ) NEW_LINE
Python3 implementation of the approach
import sys NEW_LINE from math import floor , ceil NEW_LINE
Function to generate and print the required array
def generateArray ( n , k ) : NEW_LINE
Initializing the array
array = [ 0 ] * k NEW_LINE
Finding r ( from above approach )
remaining = n - int ( k * ( k + 1 ) / 2 ) NEW_LINE
If r < 0
if remaining < 0 : NEW_LINE INDENT print ( " NO " ) NEW_LINE sys . exit ( ) NEW_LINE DEDENT right_most = remaining % k NEW_LINE
Finding ceiling and floor values
high = ceil ( remaining / k ) NEW_LINE low = floor ( remaining / k ) NEW_LINE
Fill the array with ceiling values
for i in range ( k - right_most , k ) : NEW_LINE INDENT array [ i ] = high NEW_LINE DEDENT
Fill the array with floor values
for i in range ( k - right_most ) : NEW_LINE INDENT array [ i ] = low NEW_LINE DEDENT