I have been trying to solve this challenge, and somewhat figured out what to do. But after all my attempts, I am able to pass only the test cases, and one more case in the Submit Panel. All fails after that
A company has requested to streamline their product allocation strategy, and given n 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:
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, n = 11 products and m = 3. One optimal way to assign is -
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 10^9 + 7.
Input Format
In the first line, there are two space-separated integers n and m.
In the second line, there are n space-separated integers a0,a1,....,an-1 denoting the values associated with the products.
Constraints
Output Format
In a single line, print a single integer denoting the maximum score of the arrangement modulo 10^9 + 7.
Sample Input 0
5 2
1 5 4 2 3
Sample Output 0
27
Explanation 0
The array is a = [1,5,4,2,3] and m = 2. 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 (1+2) * 1 + (3+4+5) * 2 = 27 which is the greatest score that can be obtained. Finally, the answer is modulo 10^9 + 7 which is 27.
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.
Now what I have figured out is explained in my algorithm:
start -> endbatch * (sum of elements from start till end)Code
def maxScore(segment, products):
# Write your code here
# If the segment == products, then it should return all the sum
# We will evaluate as per the products listing requirement and find the sum
'''
Algo for else condition
1. We will maintain a start and end pointer to keep a check till counter equals products
2. We will keep adding the maxSum of the value [i * sum(batch of the element)]
3. Come out of the loop and perform a final operation as above with the remaining elements
4. Add it to sum
5. Return maxSum
'''
batch = 1
maxSum = 0
start = 0
end = products
segment.sort()
if len(segment) == products:
maxSum += (batch * sumElem(segment[start:len(segment)]))
else:
while batch != products:
maxSum += (batch * sumElem(segment[start:end]))
batch += 1
start += products
end += products
maxSum += (batch * sumElem(segment[start:len(segment)]))
return maxSum
# function to find the sum of the elements
def sumElem(arr):
total = 0
for item in arr: total += item
return total
Another Solution:
def maxScore(segment, products):
# Write your code here
# If the segment == products, then it should return all the sum
# We will evaluate as per the products listing requirement and find the sum
'''
Algo for else condition
1. We will maintain a start and end pointer to keep a check till counter equals products
2. We will keep adding the maxSum of the value [i * sum(batch of the element)]
3. Come out of the loop and perform a final operation as above with the remaining elements
4. Add it to sum
5. Return maxSum
'''
batch = 1
maxSum = 0
start = 0
end = products
segment.sort()
while batch != products:
maxSum += (batch * sumElem(segment[start:end]))
batch += 1
start += products
end += products
maxSum += (batch * sumElem(segment[start:len(segment)]))
return maxSum
# function to find the sum of the elements
def sumElem(arr):
total = 0
for item in arr: total += item
return total
After all the testing, the code works fine for all the visible test cases, but doesn't pass any of the hidden test cases on HackerRank. I guess there is some kind of misunderstanding happened in understanding the question, because the solution seems fine to me.
We firstly, sort the given array. This is because the numerically greater the number we "segment" in the end, it would result in a greater sum and also it would be multiplied by the index of segment which would in turn maximise the output.
The simple logic to solve the question, is to find the maximum number of segments with m products. As in the sample input, for the input n=5, m=2:
We can form at max 1 segment with 2 products because if we take 2 segments with 2 products, we're left with 1 product in the end, and we cannot segment that (as per the question).
In order to achieve that we divide the length of the string with m and subtract 1 from it, if it is not completely divisible.
def maxScore(a, m):
a.sort()
print(a)
x=len(a)
if x%m==0:
y=int(x/m)
else:
y=int(x/m)-1
summ=0
count=1
#print(y)
i=0
for _ in range(y):
summ=summ+(sum(a[i:i+m])*count)
count=count+1
i=i+m
print(summ)
summ=summ+sum(a[i:])*count
print(summ)
return summ%1000000007
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With