Product distribution problem
PRODUCT DISTRIBUTION PROBLEM
Hello people so today we are with the product distribution problem the answer is not correct as it dosent satisfies a lot of test cases in hacker rank but would love to share my answer
A company has requested to streamline their product allocation strategy, and given products, each of which has an associated value, you are required to arrange these products into segments for processing. There are infinite segments indexed as 1, 2, 3 and so on.
However, there are two constraints:
- You can assign a product to a segment with index if and only if or the segment with index has at least products.
- Any segment must contain either no products or at least products.
The score for a segment is defined as the index of the segment multiplied by the sum of values of the products it contains. The score of an arrangement of products is the sum of scores of segments. Your task is to compute the maximum score of an arrangement.
Consider, for example, products and . One optimal way to assign is -
- Assign the first three products to segment 1.
- Assign the next three products to segment 2.
- Assign the next five products to segment 3.
Note that we can not assign 2 products to segment 4 as the second constraint would be violated. The score of the above arrangement is -
1 * (1 + 2 + 3) + 2 * (4 + 5 + 6) + 3 * (7 + 8 + 9 + 10 + 11) = 6 + 30 + 135 = 171.
Since the arrangement score can be very large, print it modulo .
Input Format
In the first line, there are two space-separated integers and .
In the second line, there are space-separated integers denoting the values associated with the products.
Output Format
In a single line, print a single integer denoting the maximum score of the arrangement modulo .
Sample Input 0
5 2
1 5 4 2 3
Sample Output 0
27
Explanation 0
The array is and . It is optimal to put the first and fourth products into the first segment and the remaining products to the second segment. Doing that, we get the arrangement score which is the greatest score that can be obtained. Finally, the answer is modulo which is .
Sample Input 1
4 4
4 1 9 7
Sample Output 1
21
Explanation 1
All the four products must be placed in the first segment. The score in this case will be 1 * (4 + 1 + 9 + 7) = 21.
This question has been extracted from HackerRank do check their website for more information
PYTHON 3 CODE:
a=[1,2,3,4,5,6,78,9,10]
m=4
u=len(a)//m
s=0
for i,k in enumerate(range(0,u*m,m)):
for j in a[k:m+k]:
s=s+((i+1)*j)
for h in a[u*m:]:
s=s+(i+1)*h
print(s)
a=[1,2,3,4,5,6,78,9,10]
m=4
u=len(a)//m
s=0
for i,k in enumerate(range(0,u*m,m)):
for j in a[k:m+k]:
s=s+((i+1)*j)
for h in a[u*m:]:
s=s+(i+1)*h
print(s)
I was not sure of the answer as i was unaware of the various test cases if anyone knows
do contact me by commenting will be greatful
Comments
Post a Comment