text
stringlengths 1
636
| code
stringlengths 8
1.89k
|
---|---|
Function to count the occurrence of all the prefix in the string S | def count_occurence ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE DEDENT |
Call the prefix_function to get LPS | LPS = prefix_function ( s ) NEW_LINE |
To store the occurrence of all the prefix | occ = [ 0 for i in range ( n + 1 ) ] NEW_LINE |
Count all the suffixes that are also prefix | for i in range ( n ) : NEW_LINE INDENT occ [ LPS [ i ] ] += 1 NEW_LINE DEDENT |
Add the occurences of i to smaller prefixes | for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT occ [ LPS [ i - 1 ] ] += occ [ i ] NEW_LINE DEDENT |
Adding 1 to all occ [ i ] for all the orignal prefix | for i in range ( n + 1 ) : NEW_LINE INDENT occ [ i ] += 1 NEW_LINE DEDENT |
Function Call to print the occurence of all the prefix | Print ( occ , s ) NEW_LINE |
Given String | A = " ABACABA " NEW_LINE |
Function Call | count_occurence ( A ) NEW_LINE |
Function to calculate maximum sum | def maximumSum ( A , B , length , X , Y ) : NEW_LINE INDENT l = length NEW_LINE DEDENT |
Maximum elements that can be chosen from array A | l1 = min ( length , X ) NEW_LINE |
Maximum elements that can be chosen from array B | l2 = min ( length , Y ) NEW_LINE dp = [ [ 0 for i in range ( l2 + 1 ) ] for i in range ( l1 + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 0 NEW_LINE |
Stores the maximum sum possible | max_sum = - 10 * 9 NEW_LINE |
Fill the dp [ ] for base case when all elements are selected from A [ ] | for i in range ( 1 , l1 + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] + A [ i - 1 ] NEW_LINE max_sum = max ( max_sum , dp [ i ] [ 0 ] ) NEW_LINE DEDENT |
Fill the dp [ ] for base case when all elements are selected from B [ ] | for i in range ( 1 , l2 + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = dp [ 0 ] [ i - 1 ] + B [ i - 1 ] NEW_LINE max_sum = max ( max_sum , dp [ 0 ] [ i ] ) NEW_LINE DEDENT for i in range ( 1 , l1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , l2 + 1 ) : NEW_LINE INDENT if ( i + j <= l ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i - 1 ] [ j ] + A [ i + j - 1 ] , dp [ i ] [ j - 1 ] + B [ i + j - 1 ] ) NEW_LINE DEDENT max_sum = max ( dp [ i ] [ j ] , max_sum ) NEW_LINE DEDENT DEDENT |
Return the final answer | return max_sum NEW_LINE |
Driver Program | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE B = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE X = 3 NEW_LINE Y = 2 NEW_LINE N = len ( A ) NEW_LINE print ( maximumSum ( A , B , N , X , Y ) ) NEW_LINE DEDENT |
Function to reduce an integer N to Zero in minimum operations by removing digits from N | def reduceZero ( N ) : NEW_LINE |
Initialise dp [ ] to steps | dp = [ 1e9 for i in range ( N + 1 ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE |
Iterate for all elements | for i in range ( N + 1 ) : NEW_LINE |
For each digit in number i | for c in str ( i ) : NEW_LINE |
Either select the number or do not select it | dp [ i ] = min ( dp [ i ] , dp [ i - ( ord ( c ) - 48 ) ] + 1 ) NEW_LINE |
dp [ N ] will give minimum step for N | return dp [ N ] NEW_LINE |
Given Number | N = 25 NEW_LINE |
Function Call | print ( reduceZero ( N ) ) NEW_LINE |
Function to print Nth Pentanacci number | def printpenta ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT |
Initialize first five numbers to base cases | first = 0 NEW_LINE second = 0 NEW_LINE third = 0 NEW_LINE fourth = 0 NEW_LINE fifth = 1 NEW_LINE |
declare a current variable | curr = 0 NEW_LINE if ( n == 0 or n == 1 or \ n == 2 or n == 3 ) : NEW_LINE INDENT print ( first ) NEW_LINE DEDENT elif ( n == 5 ) : NEW_LINE INDENT print ( fifth ) NEW_LINE DEDENT else : NEW_LINE |
Loop to add previous five numbers for each number starting from 5 and then assign first , second , third , fourth to second , third , fourth and curr to fifth respectively | for i in range ( 5 , n ) : NEW_LINE INDENT curr = first + second + third + fourth + fifth NEW_LINE first = second NEW_LINE second = third NEW_LINE third = fourth NEW_LINE fourth = fifth NEW_LINE fifth = curr NEW_LINE DEDENT print ( curr ) NEW_LINE |
Driver code | n = 10 NEW_LINE printpenta ( n ) NEW_LINE |
Python3 program for the above approach | import numpy as np NEW_LINE |
Table to store solution of each subproblem | dp = np . ones ( ( ( 100002 , 21 , 3 ) ) ) NEW_LINE dp = - 1 * dp NEW_LINE |
Function to calculate the possible binary strings | def possibleBinaries ( pos , ones , sum , k ) : NEW_LINE |
If number of ones is equal to K | if ( ones == k ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
pos : current position Base Case : When n length is traversed | if ( pos == 0 ) : NEW_LINE |
sum : count of 1 ' s β β Return β the β count β β of β 1' s obtained | return 1 if ( sum == 0 ) else 0 NEW_LINE |
If the subproblem has already been solved | if ( dp [ pos ] [ ones ] [ sum ] != - 1 ) : NEW_LINE |
Return the answer | return dp [ pos ] [ ones ] [ sum ] NEW_LINE |
Store the solution to this subproblem | dp [ pos ] [ ones ] [ sum ] = ret NEW_LINE return dp [ pos ] [ ones ] [ sum ] NEW_LINE |
Driver Code | N = 3 NEW_LINE K = 2 NEW_LINE |
Initialising the table with - 1 | print ( int ( possibleBinaries ( N , 0 , 0 , K ) ) ) NEW_LINE |
Python3 program for the above approach | class Solution : NEW_LINE INDENT def __init__ ( self , N , K , B ) : NEW_LINE INDENT self . ncr = [ ] NEW_LINE DEDENT DEDENT |
Since K can be 100 max | for i in range ( 101 ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( 101 ) : NEW_LINE DEDENT |
Initializing with - 1 | temp . append ( - 1 ) NEW_LINE self . ncr . append ( temp ) NEW_LINE self . n = N NEW_LINE self . k = K NEW_LINE |
Making vector A as 1 - Indexing | self . A = [ 0 ] + B NEW_LINE |
To Calculate the value nCk | def f ( self , n , k ) : NEW_LINE INDENT if k == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n == k : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n < k : NEW_LINE INDENT return 0 NEW_LINE DEDENT if self . ncr [ n ] [ k ] != - 1 : NEW_LINE INDENT return self . ncr [ n ] [ k ] NEW_LINE DEDENT DEDENT |
Since nCj = ( n - 1 ) Cj + ( n - 1 ) C ( j - 1 ) ; | self . ncr [ n ] [ k ] = ( self . f ( n - 1 , k ) + self . f ( n - 1 , k - 1 ) ) NEW_LINE return self . ncr [ n ] [ k ] NEW_LINE |
Function that summation of absolute differences of all pairs raised to the power k | def pairsPower ( self ) : NEW_LINE INDENT pre = [ ] NEW_LINE for i in range ( self . n + 1 ) : NEW_LINE INDENT temp = [ ] NEW_LINE for j in range ( self . k + 1 ) : NEW_LINE INDENT temp . append ( 0 ) NEW_LINE DEDENT pre . append ( temp ) NEW_LINE DEDENT ans = 0 NEW_LINE DEDENT |
Sort the given array | self . A . sort ( ) NEW_LINE |
Precomputation part , O ( n * k ) | for i in range ( 1 , self . n + 1 ) : NEW_LINE INDENT pre [ i ] [ 0 ] = 1 NEW_LINE for j in range ( 1 , self . k + 1 ) : NEW_LINE INDENT pre [ i ] [ j ] = ( self . A [ i ] * pre [ i ] [ j - 1 ] ) NEW_LINE DEDENT if i != 1 : NEW_LINE INDENT for j in range ( self . k + 1 ) : NEW_LINE INDENT pre [ i ] [ j ] = ( pre [ i ] [ j ] + pre [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT |
Traverse the array arr [ ] | for i in range ( self . n , 1 , - 1 ) : NEW_LINE |
For each K | for j in range ( self . k + 1 ) : NEW_LINE INDENT val = self . f ( self . k , j ) NEW_LINE val1 = ( pow ( self . A [ i ] , self . k - j ) * pre [ i - 1 ] [ j ] ) NEW_LINE val = val * val1 NEW_LINE if j % 2 == 0 : NEW_LINE INDENT ans = ans + val NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans - val NEW_LINE DEDENT DEDENT ans = 2 * ans NEW_LINE |
Return the final answer | return ans NEW_LINE |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE |
Given N and K | N = 3 NEW_LINE K = 3 NEW_LINE |
Given array | arr = [ 1 , 2 , 3 ] NEW_LINE |
Creation of object of class | obj = Solution ( N , K , arr ) NEW_LINE |
Function call | print ( obj . pairsPower ( ) ) NEW_LINE |
Python3 program to count total number of ways to return to origin after completing given number of steps . | MOD = 1000000007 NEW_LINE dp = [ [ [ 0 for i in range ( 101 ) ] for i in range ( 101 ) ] for i in range ( 101 ) ] NEW_LINE N , M , K = 0 , 0 , 0 NEW_LINE |
Function Initialize dp [ ] [ ] [ ] array with - 1 | def Initialize ( ) : NEW_LINE INDENT for i in range ( 101 ) : NEW_LINE INDENT for j in range ( 101 ) : NEW_LINE INDENT for z in range ( 101 ) : NEW_LINE INDENT dp [ i ] [ j ] [ z ] = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT |
Function returns the total count | def CountWays ( i , j , k ) : NEW_LINE INDENT if ( i >= N or i < 0 or j >= M or j < 0 or k < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == 0 and j == 0 and k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = ( CountWays ( i + 1 , j , k - 1 ) % MOD + CountWays ( i - 1 , j , k - 1 ) % MOD + CountWays ( i , j - 1 , k - 1 ) % MOD + CountWays ( i , j + 1 , k - 1 ) % MOD + CountWays ( i , j , k - 1 ) % MOD ) % MOD NEW_LINE DEDENT return dp [ i ] [ j ] [ k ] NEW_LINE DEDENT |
Driver code | if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE M = 3 NEW_LINE K = 4 NEW_LINE Initialize ( ) NEW_LINE print ( CountWays ( 0 , 0 , K ) ) NEW_LINE DEDENT |
Python3 Program to find the count of distinct prime subsequences at most of of length K from a given array | prime = [ True ] * 1000000 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE |
Initialize all indices as true | global prime NEW_LINE prime [ 0 ] = prime [ 1 ] = False NEW_LINE |
A value in prime [ i ] will finally be false if i is not a prime , else true | p = 2 NEW_LINE while p * p <= 100000 : NEW_LINE |
If prime [ p ] is true , then it is a prime | if ( prime [ p ] == True ) : NEW_LINE |
Update all multiples of p as false , i . e . non - prime | for i in range ( p * p , 100001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT p += 1 NEW_LINE |
Returns number of subsequences of maximum length k and contains distinct primes | def distinctPrimeSubSeq ( a , n , k ) : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE DEDENT |
Store the primes in the given array | primes = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ a [ i ] ] ) : NEW_LINE INDENT primes . append ( a [ i ] ) NEW_LINE DEDENT DEDENT l = len ( primes ) NEW_LINE |
Sort the primes | primes . sort ( ) NEW_LINE |
Store the frequencies of all the distinct primes | b = [ ] NEW_LINE dp = [ ] NEW_LINE sum = 0 NEW_LINE i = 0 NEW_LINE while i < l : NEW_LINE INDENT count = 1 NEW_LINE x = a [ i ] NEW_LINE i += 1 NEW_LINE while ( i < l and a [ i ] == x ) : NEW_LINE INDENT count += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT |
Store the frequency of primes | b . append ( count ) NEW_LINE dp . append ( count ) NEW_LINE |
Store the sum of all frequencies | sum += count NEW_LINE |
Store the length of subsequence at every instant | of_length = 2 NEW_LINE leng = len ( dp ) NEW_LINE ans = 0 NEW_LINE while ( of_length <= k ) : NEW_LINE |
Store the frequency | freq = 0 NEW_LINE |
Store the previous count of updated DP | prev = 0 NEW_LINE for i in range ( leng - 1 ) : NEW_LINE INDENT freq += dp [ i ] NEW_LINE j = sum - freq NEW_LINE DEDENT |
Calculate total subsequences of current of_length | subseq = b [ i ] * j NEW_LINE |
Add the number of subsequences to the answer | ans += subseq NEW_LINE |
Update the value in dp [ i ] | dp [ i ] = subseq NEW_LINE |
Store the updated dp [ i ] | prev += dp [ i ] NEW_LINE leng -= 1 NEW_LINE sum = prev NEW_LINE of_length += 1 NEW_LINE ans += ( l + 1 ) NEW_LINE return ans NEW_LINE |
Driver Code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 3 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE print ( distinctPrimeSubSeq ( a , n , k ) ) NEW_LINE DEDENT |
Function to find the minimum number of required such that adjacent smaller elements gets less number of prizes | def findMinPrizes ( arr , n ) : NEW_LINE INDENT totalPrizes = 0 NEW_LINE DEDENT |
Loop to iterate over every elements of the array | for i in range ( n ) : NEW_LINE INDENT x = 1 NEW_LINE j = i NEW_LINE DEDENT |
Loop to find the consecutive smaller elements at left | while ( j > 0 and arr [ j ] > arr [ j - 1 ] ) : NEW_LINE INDENT x += 1 NEW_LINE j -= 1 NEW_LINE DEDENT j = i NEW_LINE y = 1 NEW_LINE |
Loop to find the consecutive smaller elements at right | while ( j < n - 1 and arr [ j ] > arr [ j + 1 ] ) : NEW_LINE INDENT y += 1 NEW_LINE j += 1 NEW_LINE DEDENT totalPrizes += max ( x , y ) NEW_LINE print ( totalPrizes ) NEW_LINE |
Driver code | arr = [ 1 , 2 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE findMinPrizes ( arr , n ) NEW_LINE |
Python3 implementation to count the number of ways to divide N in K groups such that each group has elements in range [ L , R ] | mod = 1000000007 NEW_LINE |
DP Table | dp = [ [ - 1 for j in range ( 1000 ) ] for i in range ( 1000 ) ] NEW_LINE |
Function to count the number of ways to divide the number N in K groups such that each group has number of elements in range [ L , R ] | def calculate ( pos , left , k , L , R ) : NEW_LINE |
Base Case | if ( pos == k ) : NEW_LINE INDENT if ( left == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT |
if N is divides completely into less than k groups | if ( left == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT |
If the subproblem has been solved , use the value | if ( dp [ pos ] [ left ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ left ] NEW_LINE DEDENT answer = 0 NEW_LINE |
put all possible values greater equal to prev | for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i > left ) : NEW_LINE INDENT break NEW_LINE DEDENT answer = ( answer + calculate ( pos + 1 , left - i , k , L , R ) ) % mod NEW_LINE DEDENT dp [ pos ] [ left ] = answer NEW_LINE return answer NEW_LINE |
Function to count the number of ways to divide the number N | def countWaystoDivide ( n , k , L , R ) : NEW_LINE |
Initialize DP Table as - 1 | return calculate ( 0 , n , k , L , R ) NEW_LINE |
Driver code | if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 12 NEW_LINE K = 3 NEW_LINE L = 1 NEW_LINE R = 5 NEW_LINE print ( countWaystoDivide ( N , K , L , R ) ) NEW_LINE DEDENT |
Function for finding minimum square numbers | def minSqrNum ( n ) : NEW_LINE |
arr [ i ] of array arr store minimum count of square number to get i | arr = [ 0 ] * ( n + 1 ) NEW_LINE |
sqrNum [ i ] store last square number to get i | sqrNum = [ 0 ] * ( n + 1 ) NEW_LINE v = [ ] NEW_LINE |
Find minimum count of square number for all value 1 to n | for i in range ( n + 1 ) : NEW_LINE |