VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-the-increased-sum-of-the-array-after-p-operations/

⇱ Find the increased sum of the Array after P operations - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find the increased sum of the Array after P operations

Last Updated : 1 Oct, 2023

Given an array arr[] of non-negative integers of size N and an integer P. Neighboring values of every non-zero element are incremented by 2 in each operation, the task is to find the increased sum of the array after P operations.

Examples:

Input: arr[] = {0, 5, 0, 4, 0}, P = 2
Output: 24
Explanation: Operation 1 --> [2, 5, 4, 4, 2] total sum is 17.
Operation 2 --> [4, 9, 8, 8, 4] total sum is 33.
So at the end of 2nd operation the sum of the array is 33. Therefore, the increased value is 33-9 (initial sum) = 24.

Input: arr[] = {2, 5, 0, 7, 0}, P = 4
Output: 58

Approach: To solve the problem follow the below observations:

In order, to get the increased sum of the array we can start finding out what will be the increased value of a particular element. So if we know the increased value of all elements in the array we can sum it up to get the increased sum of the array. 

  • We know from each operation the value of the particular element is getting increased then we can simply apply the following formula. Let the element starts increasing from the Xth operation then,
    • increased value = 2 * (P - X + 1)
  • In order to find from which operation exactly the elements start increasing, we can divide this subtask into two steps: 
    • If the current element is zero,
      • since the current is zero, we need to search for a non-zero value on its left and on its right as well. This can be achieved by keeping a prefix and suffix array to store the left and right side non-zero element position from the current element.
      • If an element exists on the left side then 2 * (P - current_position + prefix[i] + 1).
      • If an element exists on the right side then 2 * (P - suffix[i] + current_position + 1).
    • If the current element is non-zero,
      • If the (i-1)th element exists and it is a non-zero element then the present element will be incremented by 2*P from its left neighbor and the same applies to the right neighbor as well.  
      • If the (i-1)th element exists and it is a having a value of zero, then the present element will get incremented from the 2nd operation because, on the first operation, it will be incremented to 2 by the current element (since the current element is non-zero), the present element will be incremented by 2*(P-1) and the same applies to the right neighbor as well.   

Below are the steps for the above approach:

  • Declare prefix and suffix arrays and initialize both arrays with -1.
  • Build both arrays by iterating over array A.
  • If the present element is zero, check for the left side positive element, if exists increment the answer by 2 * (P - current_position + prefix[i] + 1).
  • If the present element is zero, check for the right side positive element, if exists increment the answer by 2 * (P - suffix[i] + current_position + 1).
  • Else if the present element is non-zero, check if the left neighbor exists, if exists check if it is positive or not.
    • if positive, increment the answer by 2*P.
    • else increment the answer by 2*(P-1).
  • Else if the present element is non-zero, check if the left neighbor exists, if exists check if it is positive or not.
    • if positive, increment the answer by 2*P.
    • else increment the answer by 2*(P-1).

Below is the code for the above approach:


Output
24

Time Complexity: O(N) 
Auxiliary Space: O(N)

Comment
Article Tags: