
You are given an array of integers 'A' of size 'N' and a maximum score 'M'.
The score of a subsequence [ A[i1-1], A[i2-1] ... A[ik-1] ] is defined as the sum of ( i * k + A[i-1] ) for each element in the subsequence, where 'i' is the 1-based index of the element in the original array 'A' and 'k' is the length of the subsequence.
Your task is to find the length of the largest subsequence whose score is less than or equal to 'M'.
Let 'N' = 4, 'A' = [3, 5, 1, 2] and 'M' = 20.
Let's check if a subsequence of length k = 2 is possible. To minimize the score, we choose the elements that have the smallest contribution ( i * k + A[i-1] ).
The contributions for k = 2 are:
For index 1 (value 3): 1 * 2 + 3 = 5
For index 2 (value 5): 2 * 2 + 5 = 9
For index 3 (value 1): 3 * 2 + 1 = 7
For index 4 (value 2): 4 * 2 + 2 = 10
The two smallest contributions are 5 and 7. The minimum score for a subsequence of length 2 is 5 + 7 = 12.
Since 12 <= 20, a subsequence of length 2 is possible.
Let's check for k = 3. The contributions would be ( i * 3 + A[i-1] ). The sum of the three smallest contributions is (1*3+3) + (3*3+1) + (2*3+5) = 6 + 10 + 11 = 27.
Since 27 > 20, a subsequence of length 3 is not possible.
Therefore, the answer is 2.
The first line of input contains two space-separated integers, 'N' and 'M'.
The second line of input contains 'N' space-separated integers, representing the elements of array 'A'.
Output Format :
Return an integer representing the length of the largest subsequence whose score is less than or equal to 'M'.
Note :
You don’t need to print anything. Just implement the given function.
1 <= 'N' <= 10^5
1 <= 'M' <= 10^18
1 <= A[i] <= 10^9
Time Limit: 1 sec
5 40
10 2 5 8 1
3
Let's check if a subsequence of length k = 3 is possible. To minimize the score, we choose the elements that have the smallest contribution ( i * k + A[i-1] ).
The contributions for k = 3 are:
- Index 1 (value 10): `1 * 3 + 10 = 13`
- Index 2 (value 2): `2 * 3 + 2 = 8`
- Index 3 (value 5): `3 * 3 + 5 = 14`
- Index 4 (value 8): `4 * 3 + 8 = 20`
- Index 5 (value 1): `5 * 3 + 1 = 16`
The three smallest contributions are 8, 13, and 14.
The minimum score for a subsequence of length 3 is the sum of these contributions: 8 + 13 + 14 = 35.
Since 35 <= 40, a subsequence of length 3 is possible.
Similarly, it can be shown that a subsequence of length 4 is not possible.
Thus, the largest possible length is 3.
6 100
1 2 3 4 5 6
5
For a fixed 'k', how do you find the minimum possible score?
Approach:
Algorithm:
O(N * log(N)), where 'N' is the number of elements in the array 'A'.
We perform a binary search on the answer, which can range from 0 to 'N'. This contributes a factor of O(log N). For each candidate length 'k' in our search, we must verify if it's possible. This check involves creating a temporary array of costs of size 'N' and finding the sum of its 'k' smallest elements. This verification step can be performed in O(N) time. Thus, the overall time complexity is of the order O(N*log(N)).
O(N), where 'N' is the number of elements in the array 'A'.
Within the binary search, for each check, we create a temporary array of size 'N' to store the costs for that particular 'k'. Thus, the overall space complexity is of the order O(N).