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:
#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;
} |
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));
}
} |
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))
|
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));
}
} |
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)); |
24
Time Complexity: O(N)
Auxiliary Space: O(N)