Maximum Sum of elements of array which is not divisible by M, deleting/avoiding at most K consequtive array elements

Parth Prratim Chatterjee

Given: N, K, M and A(array)

N = No. of elements in the array
K = Maximum consequtive array elements that can be avoided/not considered in the answer
|A| = N

Starting from the last index of the array, you have to find the maximum sum that can be obtained by using the elements of the array, such that at any instant the sum is not divisibe by M. The sum can be aquired by traversing from the last index to the first index of the array (in order), and you have a choice to either include that element into the final answer, or avoid it.

There are two conditions :

  1. When an item is included, the total sum of all elements included till that moment (including the current element that is being included), should not be divisible by M.
  2. When an item is avoided, it has to be kept in mind that you can avoid at most K consequtive array elements at once.

Note : It is strictly required to start from the last index, and skipping any index will count towards the limit on the maximum consequtive elements that can be avoided (i.e K).

If there exists no way to traverse from the last index to the first index by satisfying the two conditions at all instances of the traversal, we have to return back -1, or else return back the maximum sum possible.

Constraints :

2 <= N <= 10^5 
1 <= K <= 10
1 <= M <= 20

Example 1 :

N = 5
K = 1 
M = 2
A = [1, 2, 3 ,4, 5]

Output : 5 + 4 + 2 = 11

Example 2 :

N = 5
K = 2
M = 2
A = [3, 4, 2, 6, 8]

Output = -1

Example 3 :

N = 7
K = 2 
M = 2
A = [1,4,2,6,3,7,7]

Output : 7 + 6 + 2 + 4  = 19
גלעד ברקן

Just add a dimension. Let dp[i][k][m] represent the maximum sum achievable at the ith index with k skips that results in remainder m when divided by M. Something like (Python code):

def f(A, N, K, M):
  dp = [[[-float("inf")] * M for k in range(K + 1)] for n in range(N)] + [[[0] * M for k in range(K + 1)]]

  for k in range(1, K + 1):
    for r in range(M):
      dp[N][k][r] = -float("inf")

  ans = -float("inf")

  for i in range(N-1,-1,-1):
    for k in range(0, min(N-i, K+1)):
      for r in range(M):
        if k > 0:
          dp[i][k][r] = dp[i+1][k-1][r]

        new_sum = A[i] + dp[i+1][k][r]
        new_r = (new_sum % M)

        if not isinstance(new_r, int):
          continue

        if k == 0:
          dp[i][k][new_r] = new_sum
          dp[i][k][0] = -float("inf")
          continue

        dp[i][k][new_r] = max(
          # skip (was assigned at
          # the top of the code block)
          dp[i][k][new_r],
          # include
          new_sum
         )

        dp[i][k][0] = -float("inf")

        ans = max(ans, dp[i][k][new_r])

  return ans if isinstance(ans, int) else -1

Output:

N = 7
A = [1,4,2,6,3,7,7]
K = 2
M = 2
# Output: 19
print(f(A, N, K, M))


N = 5
K = 1 
M = 2
A = [1, 2, 3 ,4, 5]
# Output : 5 + 4 + 2 = 11
print(f(A, N, K, M))


N = 5
K = 2
M = 2
A = [3, 4, 2, 6, 8]
# Output = -1
print(f(A, N, K, M))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Maximum sum of non adjacent elements of an array, to be printed

find the subset of maximal sum in which the distance between consecutive elements is at most 6 from a given array

The sum of the elements of an array

Sum of elements in an array

Most common array elements

Sum of all elements in an array

Sum of array elements as constexpr

Print the solution to maximum sum of non-consecutive elements in an array

Maximum sum of two elements in an array minus the distance between them

Sum of array elements in Bigquery

Sum of Divisors of array elements

Sum elements from array

maximum sum of n consecutive elements of array

Array consecutive elements sum

The sum of all the elements in an Array

maximum no. of elements in an array having sum less than or equal to k

group elements in an array and sum

Sum of the previous elements in an array

Sum of elements in a C array

How to find the maximum possible sum from any two elements in an array

Find and return the sum of all elements of array which are either divisible by 2 or 3

get maximum sum of consecutive elements in an array

sum of array elements(any order) equal to k not continuous elements

Calculate the sum of the elements between the minimum and maximum in a JavaScript array

Maximize the number of Elements in the Array divisible by M

How to get all the elements of an array that are divisible with 3 and then sum them thogether?

Speeding up the process to find elements which are divisible by elements of the same array

Find the maximum sum of all contiguous subarrays and not consecutive each others which its length at most 'k' in a given array

Sum the elements in an array until its divisible by 5 and continue the same in python