Find the increased sum of the Array after P operations
Last Updated :
01 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:
C++
#include <iostream>
#include <algorithm>
using namespace std;
int findIncreasedSum(int A[], int N, int P) {
int prefix[N];
int suffix[N];
// Initializing prefix and suffix arrays
for (int i = 0; i < N; i++) {
prefix[i] = -1;
suffix[i] = -1;
}
// Initializing ans to zero
int ans = 0;
// Building the prefix array
for (int i = 1; i < N; i++) {
if (A[i - 1] > 0) {
prefix[i] = i - 1;
} else {
prefix[i] = prefix[i - 1];
}
}
// Building the suffix array
for (int i = N - 2; i >= 0; i--) {
if (A[i + 1] > 0) {
suffix[i] = i + 1;
} else {
suffix[i] = suffix[i + 1];
}
}
for (int i = 0; i < N; i++) {
// If the current element is zero
if (A[i] == 0) {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if there is a left positive element
if (prefix[i] != -1) {
ans += 2 * max(0, P - i + prefix[i] + 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if there is a right positive element
if (suffix[i] != -1) {
ans += 2 * max(P - suffix[i] + i + 1, 0);
}
}
} else {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if the left element is non-zero or not
if (A[i - 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if the right element is non-zero or not
if (A[i + 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
int main() {
int N = 5;
int P = 2;
int A[] = {0, 5, 0, 4, 0};
// Function call
cout << findIncreasedSum(A, N, P) << endl;
return 0;
}
Java
import java.util.Arrays;
class GFG {
public static int findIncreasedSum(int[] A, int N, int P) {
int[] prefix = new int[N];
int[] suffix = new int[N];
// Initializing prefix and suffix arrays
Arrays.fill(prefix, -1);
Arrays.fill(suffix, -1);
// Initializing ans to zero
int ans = 0;
// Building the prefix array
for (int i = 1; i < N; i++) {
if (A[i - 1] > 0) {
prefix[i] = i - 1;
} else {
prefix[i] = prefix[i - 1];
}
}
// Building the suffix array
for (int i = N - 2; i >= 0; i--) {
if (A[i + 1] > 0) {
suffix[i] = i + 1;
} else {
suffix[i] = suffix[i + 1];
}
}
for (int i = 0; i < N; i++) {
// If the current element is zero
if (A[i] == 0) {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if there is a left positive element
if (prefix[i] != -1) {
ans += 2 * Math.max(0, P - i + prefix[i] + 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if there is a right positive element
if (suffix[i] != -1) {
ans += 2 * Math.max(P - suffix[i] + i + 1, 0);
}
}
} else {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if the left element is non-zero or not
if (A[i - 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if the right element is non-zero or not
if (A[i + 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
public static void main(String[] args) {
int N = 5;
int P = 2;
int[] A = {0, 5, 0, 4, 0};
// Function call
System.out.println(findIncreasedSum(A, N, P));
}
}
Python3
def findIncreasedSum(A, N, P):
'''
Created prefix and suffix arrays
and initialise with -1 If prefix[i]
is -1 then it means there is no
postive element for the current
element to its left Same holds
for suffix array as well. Else
prefix[i] or suffix[i] will
represent the position of left
positive and postive element
for the current element
respectively.
'''
prefix = [-1]*N
suffix = [-1]*N
# Initialing ans to zero.
ans = 0
# Building the prefix array
for i in range(1, N):
if(A[i - 1] > 0):
prefix[i] = i - 1
else:
prefix[i] = prefix[i-1]
# Building the suffix array
for i in range(N-2, -1, -1):
if(A[i + 1] > 0):
suffix[i] = i + 1
else:
suffix[i] = suffix[i + 1]
for i in range(N):
# If the present is Zero.
if A[i] == 0:
# Checking if there is
# left neighbour
if i > 0:
# Checking if there is
# left positive element
# exists or not
if prefix[i] != -1:
ans += 2*(max(0, P - i + prefix[i] + 1))
# Checking if there is
# right neighbour
if i < N:
# Checking if there is
# right positive element
# exists or not
if suffix[i] != -1:
ans += 2*(max(P - suffix[i] + i + 1, 0))
else:
# Checking if there is
# left neighbour
if i > 0:
# Checking if there is
# left element is non-zero
# or not
if A[i-1] > 0:
ans += 2 * P
else:
ans += 2*(P-1)
# Checking if there is
# right neighbour
if i < N-1:
# Checking if there is
# right element is
# non-zero or not
if A[i + 1] > 0:
ans += 2 * P
else:
ans += 2*(P-1)
return ans
N, P = 5, 2
A = [0, 5, 0, 4, 0]
# Funtion call
print(findIncreasedSum(A, N, P))
C#
using System;
class Program
{
static int FindIncreasedSum(int[] A, int N, int P)
{
int[] prefix = new int[N];
int[] suffix = new int[N];
// Initializing prefix and suffix arrays
for (int i = 0; i < N; i++)
{
prefix[i] = -1;
suffix[i] = -1;
}
// Initializing ans to zero
int ans = 0;
// Building the prefix array
for (int i = 1; i < N; i++)
{
if (A[i - 1] > 0)
{
prefix[i] = i - 1;
}
else
{
prefix[i] = prefix[i - 1];
}
}
// Building the suffix array
for (int i = N - 2; i >= 0; i--)
{
if (A[i + 1] > 0)
{
suffix[i] = i + 1;
}
else
{
suffix[i] = suffix[i + 1];
}
}
for (int i = 0; i < N; i++)
{
// If the current element is zero
if (A[i] == 0)
{
// Checking if there is a left neighbor
if (i > 0)
{
// Checking if there is a left positive element
if (prefix[i] != -1)
{
ans += 2 * Math.Max(0, P - i + prefix[i] + 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1)
{
// Checking if there is a right positive element
if (suffix[i] != -1)
{
ans += 2 * Math.Max(P - suffix[i] + i + 1, 0);
}
}
}
else
{
// Checking if there is a left neighbor
if (i > 0)
{
// Checking if the left element is non-zero or not
if (A[i - 1] > 0)
{
ans += 2 * P;
}
else
{
ans += 2 * (P - 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1)
{
// Checking if the right element is non-zero or not
if (A[i + 1] > 0)
{
ans += 2 * P;
}
else
{
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
// Driver code
static void Main()
{
int N = 5;
int P = 2;
int[] A = { 0, 5, 0, 4, 0 };
Console.WriteLine(FindIncreasedSum(A, N, P));
}
}
JavaScript
function findIncreasedSum(A, N, P) {
let prefix = new Array(N);
let suffix = new Array(N);
// Initializing prefix and suffix arrays
for (let i = 0; i < N; i++) {
prefix[i] = -1;
suffix[i] = -1;
}
// Initializing ans to zero
let ans = 0;
// Building the prefix array
for (let i = 1; i < N; i++) {
if (A[i - 1] > 0) {
prefix[i] = i - 1;
} else {
prefix[i] = prefix[i - 1];
}
}
// Building the suffix array
for (let i = N - 2; i >= 0; i--) {
if (A[i + 1] > 0) {
suffix[i] = i + 1;
} else {
suffix[i] = suffix[i + 1];
}
}
for (let i = 0; i < N; i++) {
// If the current element is zero
if (A[i] == 0) {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if there is a left positive element
if (prefix[i] != -1) {
ans += 2 * Math.max(0, P - i + prefix[i] + 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if there is a right positive element
if (suffix[i] != -1) {
ans += 2 * Math.max(P - suffix[i] + i + 1, 0);
}
}
} else {
// Checking if there is a left neighbor
if (i > 0) {
// Checking if the left element is non-zero or not
if (A[i - 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
// Checking if there is a right neighbor
if (i < N - 1) {
// Checking if the right element is non-zero or not
if (A[i + 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
// Function call
let N = 5;
let P = 2;
let A = [0, 5, 0, 4, 0];
console.log(findIncreasedSum(A, N, P));
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Find the indices which will hold the Array sum after given operations
Given an array arr[], the task is to print the indices that have the highest probability of holding the entire sum of the array after performing the given operations: Choose any two elements say arr[i] and arr[j], store their sum in a variable KAssign K at index of max(arr[i], arr[j]) and assign 0 a
10 min read
Sum of the updated array after performing the given operation
Given an array arr[] of N elements, the task is to update all the array elements such that an element arr[i] is updated as arr[i] = arr[i] - X where X = arr[i + 1] + arr[i + 2] + ... + arr[N - 1] and finally print the sum of the updated array.Examples: Input: arr[] = {40, 25, 12, 10} Output: 8 The u
8 min read
Maximize the Sum of the given array using given operations
Given two arrays A[] and B[] consisting of N integers and an integer K, the task is to maximize the sum calculated from the array A[] by the following operations: For every index in B[] containing 0, the corresponding index in A[] is added to the sum.For every index in B[] containing 1, add the valu
7 min read
Find the Maximum sum of the Array by performing the given operations
Given an Array A[] of size N with repeated elements and all array elements are positive, the task is to find the maximum sum by applying the given operations: Select any 2 indexes and select 2 integers(say x and y) such that the product of the elements(x and y) is equal to the product of the element
5 min read
Find the sum of array after performing every query
Given an array arr[] of size N and Q queries where every query contains two integers X and Y, the task is to find the sum of an array after performing each Q queries such that for every query, the element in the array arr[] with value X is updated to Y. Find the sum of the array after every query. E
7 min read
Find the overlapping sum of two arrays
Given two arrays A[] and B[] having n unique elements each. The task is to find the overlapping sum of the two arrays. That is the sum of elements that is common in both of the arrays. Note: Elements in the arrays are unique. That is the array does not contain duplicates. Examples: Input : A[] = {1,
8 min read
Find the index of the left pointer after possible moves in the array
Given an array of size N . Move two pointers, one from the left side and one from the right side of the array, a pointer will only move forward if the sum of all the numbers it has already gone through is less than the sum of the numbers the other pointer has gone through. Continue the process while
5 min read
Minimize transfer operations to get the given Array sum
Given two integer arrays A[] and B[] of length N and M respectively, the task is to find the minimum number of operations required such that the sum of elements in array A becomes targetSum. In one operation you can transfer an element from A[] to B[] or from B[] to A[]. Examples Input: N = 4, M = 3
15+ min read
Calculate sum of the array generated by given operations
Given an array arr[] consisting of N strings, the task is to find the total sum of the array brr[] (initially empty) constructed by performing following operations while traversing the given array arr[]: If the array arr[] contains an integer, then insert that integer into the array brr[].If the arr
8 min read
Print modified array after multiple array range increment operations
Given an array containing n integers and a value d. m queries are given. Each query has two values start and end. For each query, the problem is to increment the values from the start to end index in the given array by the given value d. A linear time-efficient solution is required for handling such
10 min read