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];
for ( int i = 0; i < N; i++) {
prefix[i] = -1;
suffix[i] = -1;
}
int ans = 0;
for ( int i = 1; i < N; i++) {
if (A[i - 1] > 0) {
prefix[i] = i - 1;
} else {
prefix[i] = prefix[i - 1];
}
}
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 (A[i] == 0) {
if (i > 0) {
if (prefix[i] != -1) {
ans += 2 * max(0, P - i + prefix[i] + 1);
}
}
if (i < N - 1) {
if (suffix[i] != -1) {
ans += 2 * max(P - suffix[i] + i + 1, 0);
}
}
} else {
if (i > 0) {
if (A[i - 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
if (i < N - 1) {
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};
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];
Arrays.fill(prefix, - 1 );
Arrays.fill(suffix, - 1 );
int ans = 0 ;
for ( int i = 1 ; i < N; i++) {
if (A[i - 1 ] > 0 ) {
prefix[i] = i - 1 ;
} else {
prefix[i] = prefix[i - 1 ];
}
}
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 (A[i] == 0 ) {
if (i > 0 ) {
if (prefix[i] != - 1 ) {
ans += 2 * Math.max( 0 , P - i + prefix[i] + 1 );
}
}
if (i < N - 1 ) {
if (suffix[i] != - 1 ) {
ans += 2 * Math.max(P - suffix[i] + i + 1 , 0 );
}
}
} else {
if (i > 0 ) {
if (A[i - 1 ] > 0 ) {
ans += 2 * P;
} else {
ans += 2 * (P - 1 );
}
}
if (i < N - 1 ) {
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 };
System.out.println(findIncreasedSum(A, N, P));
}
}
|
Python3
def findIncreasedSum(A, N, P):
prefix = [ - 1 ] * N
suffix = [ - 1 ] * N
ans = 0
for i in range ( 1 , N):
if (A[i - 1 ] > 0 ):
prefix[i] = i - 1
else :
prefix[i] = prefix[i - 1 ]
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 A[i] = = 0 :
if i > 0 :
if prefix[i] ! = - 1 :
ans + = 2 * ( max ( 0 , P - i + prefix[i] + 1 ))
if i < N:
if suffix[i] ! = - 1 :
ans + = 2 * ( max (P - suffix[i] + i + 1 , 0 ))
else :
if i > 0 :
if A[i - 1 ] > 0 :
ans + = 2 * P
else :
ans + = 2 * (P - 1 )
if i < N - 1 :
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 ]
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];
for ( int i = 0; i < N; i++)
{
prefix[i] = -1;
suffix[i] = -1;
}
int ans = 0;
for ( int i = 1; i < N; i++)
{
if (A[i - 1] > 0)
{
prefix[i] = i - 1;
}
else
{
prefix[i] = prefix[i - 1];
}
}
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 (A[i] == 0)
{
if (i > 0)
{
if (prefix[i] != -1)
{
ans += 2 * Math.Max(0, P - i + prefix[i] + 1);
}
}
if (i < N - 1)
{
if (suffix[i] != -1)
{
ans += 2 * Math.Max(P - suffix[i] + i + 1, 0);
}
}
}
else
{
if (i > 0)
{
if (A[i - 1] > 0)
{
ans += 2 * P;
}
else
{
ans += 2 * (P - 1);
}
}
if (i < N - 1)
{
if (A[i + 1] > 0)
{
ans += 2 * P;
}
else
{
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
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);
for (let i = 0; i < N; i++) {
prefix[i] = -1;
suffix[i] = -1;
}
let ans = 0;
for (let i = 1; i < N; i++) {
if (A[i - 1] > 0) {
prefix[i] = i - 1;
} else {
prefix[i] = prefix[i - 1];
}
}
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 (A[i] == 0) {
if (i > 0) {
if (prefix[i] != -1) {
ans += 2 * Math.max(0, P - i + prefix[i] + 1);
}
}
if (i < N - 1) {
if (suffix[i] != -1) {
ans += 2 * Math.max(P - suffix[i] + i + 1, 0);
}
}
} else {
if (i > 0) {
if (A[i - 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
if (i < N - 1) {
if (A[i + 1] > 0) {
ans += 2 * P;
} else {
ans += 2 * (P - 1);
}
}
}
}
return ans;
}
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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...