codeparrot/xlcost-text-to-code huggingface dataset:
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s and count of 1 ' s ▁ â ‰ ¥ ▁ count ▁ of ▁ 0' s in each prefix substring | Function to calculate and returns the value of Binomial Coefficient C ( n , k ) ; Since C ( n , k ) = C ( n , n - k ) ; Calculate the value of [ n * ( n - 1 ) * -- - * ( n - k + 1 ) ] / [ k * ( k - 1 ) * -- - * 1 ] ; Function to return the count of all binary strings having equal count of 0 ' s ▁ and ▁ 1' s and each prefix substring having frequency of 1 ' s ▁ > = ▁ frequencies ▁ of ▁ 0' s ; If N is odd ; No such strings possible ; Otherwise ; Calculate value of 2 nCn ; Return 2 nCn / ( n + 1 ) ; Driver Code
def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 NEW_LINE if ( k > n - k ) : NEW_LINE INDENT k = n - k NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT res *= ( n - i ) NEW_LINE res //= ( i + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT def countStrings ( N ) : NEW_LINE INDENT if ( N % 2 == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT N //= 2 NEW_LINE c = binomialCoeff ( 2 * N , N ) NEW_LINE return c // ( N + 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 6 NEW_LINE print ( countStrings ( N ) ) NEW_LINE DEDENT
Number of ways to cut a stick of length N into in even length at most K units long pieces | Recursive function to count the total number of ways ; Base case if no - solution exist ; Condition if a solution exist ; Check if already calculated ; Initialize counter ; Recursive call ; Store the answer ; Return the answer ; Driver code
def solve ( n , k , mod , dp ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] != - 1 ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( 2 , k + 1 , 2 ) : NEW_LINE INDENT cnt = ( ( cnt % mod + solve ( n - i , k , mod , dp ) % mod ) % mod ) NEW_LINE DEDENT dp [ n ] = cnt NEW_LINE return int ( cnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mod = 1e9 + 7 NEW_LINE n = 4 NEW_LINE k = 2 NEW_LINE dp = [ - 1 ] * ( n + 1 ) NEW_LINE ans = solve ( n , k , mod , dp ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Number of ways to select exactly K even numbers from given Array | Python3 program for the above approach ; Function for calculating factorial ; Factorial of n defined as : n ! = n * ( n - 1 ) * ... * 1 ; Function to find the number of ways to select exactly K even numbers from the given array ; Count even numbers ; Check if the current number is even ; Check if the even numbers to be chosen is greater than n . Then , there is no way to pick it . ; The number of ways will be nCk ; Given array arr [ ] ; Given count of even elements ; Function call
f = [ 0 ] * 12 NEW_LINE def fact ( ) : NEW_LINE INDENT f [ 0 ] = f [ 1 ] = 1 NEW_LINE for i in range ( 2 , 11 ) : NEW_LINE INDENT f [ i ] = i * 1 * f [ i - 1 ] NEW_LINE DEDENT DEDENT def solve ( arr , n , k ) : NEW_LINE INDENT fact ( ) NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT DEDENT if ( k > even ) : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( f [ even ] // ( f [ k ] * f [ even - k ] ) ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 1 NEW_LINE solve ( arr , n , k ) NEW_LINE
Count of binary strings of length N having equal count of 0 ' s ▁ and ▁ 1' s | Python3 program to implement the above approach ; Function to calculate C ( n , r ) % MOD DP based approach ; Corner case ; Stores the last row of Pascal 's Triangle ; Initialize top row of pascal triangle ; Construct Pascal 's Triangle  from top to bottom ; Fill current row with the help of previous row ; C ( n , j ) = C ( n - 1 , j ) + C ( n - 1 , j - 1 ) ; Driver Code
MOD = 1000000007 NEW_LINE def nCrModp ( n , r ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT C = [ 0 ] * ( r + 1 ) NEW_LINE C [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , r ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = ( C [ j ] + C [ j - 1 ] ) % MOD NEW_LINE DEDENT DEDENT return C [ r ] NEW_LINE DEDENT N = 6 NEW_LINE print ( nCrModp ( N , N // 2 ) ) NEW_LINE
Convert 1 into X in min steps by multiplying with 2 or 3 or by adding 1 | Function to print the Minimum number of operations required to convert 1 into X by using three given operations ; Initialize a DP array to store min operations for sub - problems ; Base Condition : No operation required when N is 1 ; Multiply the number by 2 ; Multiply the number by 3 ; Add 1 to the number . ; Print the minimum operations ; Initialize a list to store the sequence ; Backtrack to find the sequence ; If add by 1 ; If multiply by 2 ; If multiply by 3 ; Print the sequence of operation ; Given number X ; Function Call
def printMinOperations ( N ) : NEW_LINE INDENT dp = [ N ] * ( N + 1 ) NEW_LINE dp [ 1 ] = 0 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 and dp [ i ] > dp [ i // 2 ] + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i // 2 ] + 1 NEW_LINE DEDENT if ( i % 3 == 0 and dp [ i ] > dp [ i // 3 ] + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i // 3 ] + 1 NEW_LINE DEDENT if ( dp [ i ] > dp [ i - 1 ] + 1 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] + 1 NEW_LINE DEDENT DEDENT print ( dp [ N ] , end = " " ) NEW_LINE seq = [ ] NEW_LINE while ( N > 1 ) : NEW_LINE INDENT seq . append ( N ) NEW_LINE if dp [ N - 1 ] == dp [ N ] - 1 : NEW_LINE INDENT N = N - 1 NEW_LINE DEDENT elif N % 2 == 0 and dp [ N // 2 ] == dp [ N ] - 1 : NEW_LINE INDENT N = N // 2 NEW_LINE DEDENT else : NEW_LINE INDENT N = N // 3 NEW_LINE DEDENT DEDENT seq . append ( 1 ) NEW_LINE for i in reversed ( seq ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT X = 96234 NEW_LINE printMinOperations ( X ) NEW_LINE
Largest subsequence such that all indices and all values are multiples individually | Python3 program for the above approach ; Function that print maximum length of array ; dp [ ] array to store the maximum length ; Find all divisors of i ; If the current value is greater than the divisor 's value ; If current value is greater than the divisor 's value  and s is not equal  to current index ; Condition if current value is greater than the divisor 's value ; Computing the greatest value ; Printing maximum length of array ; Driver Code ; Given array arr [ ] ; Function call
from math import * NEW_LINE def maxLength ( arr , n ) : NEW_LINE INDENT dp = [ 1 ] * n NEW_LINE for i in range ( n - 1 , 1 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , int ( sqrt ( i ) ) + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT s = i // j NEW_LINE if ( s == j ) : NEW_LINE INDENT if ( arr [ i ] > arr [ s ] ) : NEW_LINE INDENT dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s != i and arr [ i ] > arr [ s ] ) : NEW_LINE INDENT dp [ s ] = max ( dp [ i ] + 1 , dp [ s ] ) NEW_LINE DEDENT if ( arr [ i ] > arr [ j ] ) : NEW_LINE INDENT dp [ j ] = max ( dp [ i ] + 1 , dp [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT Max = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( dp [ i ] > Max ) : NEW_LINE INDENT Max = dp [ i ] NEW_LINE DEDENT DEDENT print ( Max ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 4 , 2 , 3 , 6 , 4 , 9 ] NEW_LINE size = len ( arr ) NEW_LINE maxLength ( arr , size ) NEW_LINE DEDENT
Maximum profit by buying and selling a stock at most twice | Set 2 | Python3 implmenetation to find maximum possible profit with at most two transactions ; Function to find the maximum profit with two transactions on a given list of stock prices ; Set initial buying values to INT_MAX as we want to minimize it ; Set initial selling values to zero as we want to maximize it ; Money lent to buy the stock should be minimum as possible . buy1 tracks the minimum possible stock to buy from 0 to i - 1. ; Profit after selling a stock should be maximum as possible . profit1 tracks maximum possible profit we can make from 0 to i - 1. ; Now for buying the 2 nd stock , we will integrate profit made from selling the 1 st stock ; Profit after selling a 2 stocks should be maximum as possible . profit2 tracks maximum possible profit we can make from 0 to i - 1. ; Driver code
import sys NEW_LINE def maxProfit ( price , n ) : NEW_LINE INDENT buy1 , buy2 = sys . maxsize , sys . maxsize NEW_LINE profit1 , profit2 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT buy1 = min ( buy1 , price [ i ] ) NEW_LINE profit1 = max ( profit1 , price [ i ] - buy1 ) NEW_LINE buy2 = min ( buy2 , price [ i ] - profit1 ) NEW_LINE profit2 = max ( profit2 , price [ i ] - buy2 ) NEW_LINE DEDENT return profit2 NEW_LINE DEDENT price = [ 2 , 30 , 15 , 10 , 8 , 25 , 80 ] NEW_LINE n = len ( price ) NEW_LINE print ( " Maximum ▁ Profit ▁ = ▁ " , maxProfit ( price , n ) ) NEW_LINE
Maximum score of deleting an element from an Array based on given condition | Python3 implementation to find the maximum score of the deleting a element from an array ; Function to find the maximum score of the deleting an element from an array ; Creating a map to keep the frequency of numbers ; Loop to iterate over the elements of the array ; Creating a DP array to keep count of max score at ith element and it will be filled in the bottom Up manner ; Loop to choose the elements of the array to delete from the array ; Driver Code ; Function Call
from collections import defaultdict NEW_LINE def findMaximumScore ( a , n ) : NEW_LINE INDENT freq = defaultdict ( int ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT dp = [ 0 ] * ( max ( a ) + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE dp [ 1 ] = freq [ 1 ] NEW_LINE for i in range ( 2 , len ( dp ) ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , dp [ i - 2 ] + freq [ i ] * i ) NEW_LINE DEDENT return dp [ - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE a = [ 1 , 2 , 3 ] NEW_LINE print ( findMaximumScore ( a , n ) ) NEW_LINE DEDENT
Reverse a subarray to maximize sum of even | Function to return maximized sum at even indices ; Stores difference with element on the left ; Stores difference with element on the right ; Compute and store left difference ; For first index ; If previous difference is positive ; Otherwise ; For the last index ; Otherwise ; For first index ; If the previous difference is positive ; Driver Code
def maximizeSum ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT leftDP = [ 0 ] * ( n ) NEW_LINE rightDP = [ 0 ] * ( n ) NEW_LINE c = 0 NEW_LINE for i in range ( 1 , n , 2 ) : NEW_LINE INDENT leftDiff = arr [ i ] - arr [ i - 1 ] NEW_LINE if ( c - 1 < 0 ) : NEW_LINE INDENT leftDP [ i ] = leftDiff NEW_LINE DEDENT else : NEW_LINE INDENT if ( leftDP [ i ] > 0 ) : NEW_LINE INDENT leftDP [ i ] = ( leftDiff + leftDP [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT leftDP [ i ] = leftDiff NEW_LINE DEDENT DEDENT rightDiff = 0 NEW_LINE if ( i + 1 >= len ( arr ) ) : NEW_LINE INDENT rightDiff = 0 NEW_LINE DEDENT else : NEW_LINE INDENT rightDiff = arr [ i ] - arr [ i + 1 ] NEW_LINE DEDENT if ( c - 1 < 0 ) : NEW_LINE INDENT rightDP [ i ] = rightDiff NEW_LINE DEDENT else : NEW_LINE INDENT if ( rightDP [ i ] > 0 ) : NEW_LINE INDENT rightDP [ i ] = ( rightDiff + rightDP [ i - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT rightDP [ i ] = rightDiff NEW_LINE DEDENT DEDENT c += 1 NEW_LINE DEDENT maxm = 0 NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT maxm = max ( maxm , max ( leftDP [ i ] , rightDP [ i ] ) ) NEW_LINE DEDENT return maxm + sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 8 , 4 , 5 , 7 , 6 , 8 , 9 , 7 , 3 ] NEW_LINE ans = maximizeSum ( arr ) NEW_LINE print ( ans ) NEW_LINE DEDENT
Count of all subsequences having adjacent elements with different parity | Function to find required subsequences ; Initialise the dp [ ] [ ] with 0. ; If odd element is encountered ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with even element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Considering i - th element will be present in the subsequence ; Appending i - th element to all non - empty subsequences ending with odd element till ( i - 1 ) th indexes ; Considering ith element will not be present in the subsequence ; Count of all valid subsequences ; Driver code
def validsubsequences ( arr , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] % 2 ) : NEW_LINE INDENT dp [ i ] [ 1 ] += 1 NEW_LINE dp [ i ] [ 1 ] += dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] NEW_LINE dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ 0 ] += 1 NEW_LINE dp [ i ] [ 0 ] += dp [ i - 1 ] [ 1 ] NEW_LINE dp [ i ] [ 0 ] += dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] += dp [ i - 1 ] [ 1 ] NEW_LINE DEDENT DEDENT return dp [ n ] [ 0 ] + dp [ n ] [ 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 6 , 9 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( validsubsequences ( arr , n ) ) NEW_LINE DEDENT
Count of N | Function to return count of N - digit numbers with absolute difference of adjacent digits not exceeding K ; For 1 - digit numbers , the count is 10 ; dp [ i ] [ j ] stores the count of i - digit numbers ending with j ; Initialize count for 1 - digit numbers ; Compute values for count of digits greater than 1 ; Find the range of allowed numbers if last digit is j ; Perform Range update ; Prefix sum to find count of of i - digit numbers ending with j ; Stores the final answer ; Driver Code
def getCount ( n , k ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 10 NEW_LINE DEDENT dp = [ [ 0 for x in range ( 11 ) ] for y in range ( n + 1 ) ] ; NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , 10 ) : NEW_LINE INDENT l = max ( 0 , j - k ) NEW_LINE r = min ( 9 , j + k ) NEW_LINE dp [ i ] [ l ] = dp [ i ] [ l ] + dp [ i - 1 ] [ j ] NEW_LINE dp [ i ] [ r + 1 ] = dp [ i ] [ r + 1 ] - dp [ i - 1 ] [ j ] NEW_LINE DEDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ j ] + dp [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT count = count + dp [ n ] [ i ] NEW_LINE DEDENT return count NEW_LINE DEDENT n , k = 2 , 1 NEW_LINE print ( getCount ( n , k ) ) NEW_LINE
Find if there is a path between two vertices in a directed graph | Set 2 | Python3 program to find if there is a path between two vertices in a directed graph using Dynamic Programming ; Function to find if there is a path between two vertices in a directed graph ; dp matrix ; Set dp [ i ] [ j ] = true if there is edge between i to j ; Check for all intermediate vertex ; If vertex is invalid ; If there is a path ; Driver code
X = 6 NEW_LINE Z = 2 NEW_LINE def existPath ( V , edges , u , v ) : NEW_LINE INDENT mat = [ [ False for i in range ( V ) ] for j in range ( V ) ] NEW_LINE for i in range ( X ) : NEW_LINE INDENT mat [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = True NEW_LINE DEDENT for k in range ( V ) : NEW_LINE INDENT for i in range ( V ) : NEW_LINE INDENT for j in range ( V ) : NEW_LINE INDENT mat [ i ] [ j ] = ( mat [ i ] [ j ] or mat [ i ] [ k ] and mat [ k ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT if ( u >= V or v >= V ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( mat [ u ] [ v ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT V = 4 NEW_LINE edges = [ [ 0 , 2 ] , [ 0 , 1 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 2 , 0 ] , [ 3 , 3 ] ] NEW_LINE u , v = 1 , 3 NEW_LINE if ( existPath ( V , edges , u , v ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
Maximize the numbers of splits in an Array having sum divisible by 3 | Python3 program for above approach ; prefix array storing right most index with prefix sums 0 , 1 , 2 ; dp array ; current prefix sum ; Calculating the prefix sum modulo 3 ; We dont have a left pointer with prefix sum C ; We cannot consider i as a right pointer of any segment ; We have a left pointer pre [ C ] with prefix sum C ; i is the rightmost index of prefix sum C ; Driver code
def calculate_maximum_splits ( arr , N ) : NEW_LINE INDENT pre = [ 0 , - 1 , - 1 ] NEW_LINE dp = [ 0 for i in range ( N ) ] NEW_LINE C = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT C = C + arr [ i ] NEW_LINE C = C % 3 NEW_LINE if pre [ C ] == - 1 : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , dp [ pre [ C ] ] + 1 ) NEW_LINE DEDENT pre [ C ] = i NEW_LINE DEDENT return dp [ N - 1 ] NEW_LINE DEDENT arr = [ 2 , 36 , 1 , 9 , 2 , 0 , 1 , 8 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( calculate_maximum_splits ( arr , N ) ) NEW_LINE
Count of elements having Euler 's Totient value one less than itself | Python3 program for the above approach ; Seiev of Erotosthenes method to compute all primes ; If current number is marked prime then mark its multiple as non - prime ; Function to count the number of element satisfying the condition ; Compute the number of primes in count prime array ; Print the number of elements satisfying the condition ; Given array ; Size of the array ; Function call
prime = [ 0 ] * ( 1000001 ) NEW_LINE def seiveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( 2 , 1000001 ) : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT i = 2 NEW_LINE while ( i * i < 1000001 ) : NEW_LINE INDENT if ( prime [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , 1000001 , i ) : NEW_LINE INDENT prime [ j ] = 0 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def CountElements ( arr , n , L , R ) : NEW_LINE INDENT seiveOfEratosthenes ( ) NEW_LINE countPrime = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT countPrime [ i ] = ( countPrime [ i - 1 ] + prime [ arr [ i - 1 ] ] ) NEW_LINE DEDENT print ( countPrime [ R ] - countPrime [ L - 1 ] ) NEW_LINE return NEW_LINE DEDENT arr = [ 2 , 4 , 5 , 8 ] NEW_LINE N = len ( arr ) NEW_LINE L = 1 NEW_LINE R = 3 NEW_LINE CountElements ( arr , N , L , R ) NEW_LINE
Count of elements having odd number of divisors in index range [ L , R ] for Q queries | Python3 program for the above approach ; Function count the number of elements having odd number of divisors ; Initialise dp [ ] array ; Precomputation ; Find the Prefix Sum ; Iterate for each query ; Find the answer for each query ; Driver code ; Given array arr [ ] ; Given Query ; Function call
import math NEW_LINE def OddDivisorsCount ( n , q , a , Query ) : NEW_LINE INDENT DP = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = int ( math . sqrt ( a [ i ] ) ) ; NEW_LINE if ( x * x == a [ i ] ) : NEW_LINE INDENT DP [ i ] = 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT DP [ i ] = DP [ i - 1 ] + DP [ i ] ; NEW_LINE DEDENT l = 0 NEW_LINE r = 0 NEW_LINE for i in range ( q ) : NEW_LINE INDENT l = Query [ i ] [ 0 ] ; NEW_LINE r = Query [ i ] [ 1 ] ; NEW_LINE if ( l == 0 ) : NEW_LINE INDENT print ( DP [ r ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( DP [ r ] - DP [ l - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE Q = 3 ; NEW_LINE arr = [ 2 , 4 , 5 , 6 , 9 ] NEW_LINE Query = [ [ 0 , 2 ] , [ 1 , 3 ] , [ 1 , 4 ] ] NEW_LINE OddDivisorsCount ( N , Q , arr , Query ) ; NEW_LINE DEDENT
Find all possible ways to Split the given string into Primes | Python 3 program to Find all the ways to split the given string into Primes . ; Sieve of Eratosthenes ; Function Convert integer to binary string ; Function print all the all the ways to split the given string into Primes . ; To store all possible strings ; Exponetnital complexity n * ( 2 ^ ( n - 1 ) ) for bit ; Pruning step ; Driver code
primes = [ True ] * 1000001 NEW_LINE maxn = 1000000 NEW_LINE def sieve ( ) : NEW_LINE INDENT primes [ 0 ] = primes [ 1 ] = 0 NEW_LINE i = 2 NEW_LINE while i * i <= maxn : NEW_LINE INDENT if ( primes [ i ] ) : NEW_LINE INDENT for j in range ( i * i , maxn + 1 , i ) : NEW_LINE INDENT primes [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def toBinary ( n ) : NEW_LINE INDENT r = " " NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE r = "0" + r NEW_LINE else : NEW_LINE r = "1" + r NEW_LINE n //= 2 NEW_LINE DEDENT if ( r == " " ) : NEW_LINE return "0" NEW_LINE return r NEW_LINE DEDENT def PrimeSplit ( st ) : NEW_LINE INDENT cnt = 0 NEW_LINE ans = [ ] NEW_LINE bt = 1 << ( len ( st ) - 1 ) NEW_LINE n = len ( st ) NEW_LINE for i in range ( bt ) : NEW_LINE INDENT temp = toBinary ( i ) + "0" NEW_LINE j = 0 NEW_LINE x = n - len ( temp ) NEW_LINE while ( j < x ) : NEW_LINE INDENT temp = "0" + temp NEW_LINE j += 1 NEW_LINE DEDENT j = 0 NEW_LINE x = 0 NEW_LINE y = - 1 NEW_LINE sp = " " NEW_LINE tp = " " NEW_LINE flag = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT sp += st [ j ] NEW_LINE if ( temp [ j ] == '1' ) : NEW_LINE INDENT tp += sp + ' , ' NEW_LINE y = int ( sp ) NEW_LINE if ( not primes [ y ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT sp = " " NEW_LINE DEDENT j += 1 NEW_LINE DEDENT tp += sp NEW_LINE if ( sp != " " ) : NEW_LINE INDENT y = int ( sp ) NEW_LINE if ( not primes [ y ] ) : NEW_LINE flag = 1 NEW_LINE DEDENT if ( not flag ) : NEW_LINE ans . append ( tp ) NEW_LINE DEDENT if ( len ( ans ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT for i in ans : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT st = "11373" NEW_LINE sieve ( ) NEW_LINE PrimeSplit ( st ) NEW_LINE DEDENT
Count of subsequences of length 4 in form ( x , x , x + 1 , x + 1 ) | Set 2 | Function to count the numbers ; Array that stores the digits from left to right ; Array that stores the digits from right to left ; Initially both array store zero ; Fill the table for count1 array ; Update the count of current character ; Fill the table for count2 array ; Update the count of cuuent character ; Variable that stores the count of the numbers ; Traverse Input string and get the count of digits from count1 and count2 array such that difference b / w digit is 1 & store it int c1 & c2 . And store it in variable c1 and c2 ; Update the ans ; Return the final count ; Given String ; Function call
def countStableNum ( Str , N ) : NEW_LINE INDENT count1 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ] NEW_LINE count2 = [ [ 0 for j in range ( 10 ) ] for i in range ( N ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT count1 [ i ] [ j ] , count2 [ i ] [ j ] = 0 , 0 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT if ( i != 0 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT count1 [ i ] [ j ] = ( count1 [ i ] [ j ] + count1 [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT count1 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] += 1 NEW_LINE DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i != N - 1 ) : NEW_LINE INDENT for j in range ( 10 ) : NEW_LINE INDENT count2 [ i ] [ j ] += count2 [ i + 1 ] [ j ] NEW_LINE DEDENT DEDENT count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] = count2 [ i ] [ ord ( Str [ i ] ) - ord ( '0' ) ] + 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , N - 1 ) : NEW_LINE INDENT if ( Str [ i ] == '9' ) : NEW_LINE INDENT continue NEW_LINE DEDENT c1 = count1 [ i - 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) ] NEW_LINE c2 = count2 [ i + 1 ] [ ord ( Str [ i ] ) - ord ( '0' ) + 1 ] NEW_LINE if ( c2 == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans = ( ans + ( c1 * ( ( c2 * ( c2 - 1 ) // 2 ) ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT Str = "224353" NEW_LINE N = len ( Str ) NEW_LINE print ( countStableNum ( Str , N ) ) NEW_LINE