Modified Range Sum in a given Array without updates
Given an array arr[] of size N containing distinct numbers from 1 to N in any order, the task is to perform a modified range sum in this array according to the following rules.
For each index ‘i‘ in array arr:
- The starting index of the range ‘L‘ is selected as i + 1
- The ending index of the range ‘R‘ is selected as:
- min(arr[i], N-1); if arr[i] > i
- max(i+1, arr[i]); if arr[i] < i+1
- For updation, the values in range arr[L] to arr[R] is incremented by 1.
- The range is found out using the input array and not the updated array
Examples:
Input: arr[] = {4, 1, 3, 2}
Output: 4 2 5 4
Explanation:
For i = 0 -> Element in input array = 4. Therefore L = 1, and R = min(4, N-1) = 3. Hence, all the elements from arr[1] to arr[3] are incremented by 1. The elements after update operation are {4, 2, 4, 3}.
For i = 1 -> Element in input array = 1. Therefore L = 2, and R = max(1, i+1) = 2. Hence, all the elements from arr[2] to arr[2] are incremented by 1. The elements after update operation are {4, 2, 5, 3}.
For i = 2 -> Element in input array = 3. Therefore L = 3, and R = min(3, N-1) = 3. Hence, all the elements from arr[3] to arr[3] are incremented by 1. The elements after update operation are {4, 2, 5, 4}.
For i = 3 -> The array is unaffected. Therefore the elements after update operation are {4, 2, 5, 4}.
The resulting array is {4, 2, 5, 4}.
Input: arr[] = {2, 1}
Output: {2, 2}
Explanation:
The first element is 2. So arr[1] gets incremented by 1. Hence, the resulting array is {2, 2}.
Naive Approach: The naive approach is to run a loop for each element and increase all the values from arr[i+1] to arr[min(i+arr[i], N-1)] by 1. The time complexity of this approach is O(N2).
Efficient Approach: This problem can be solved in O(N) by using an extra space of O(N). The idea is to use the concept of prefix sum array. The following steps are followed to compute the answer:
- An array b[] of size N + 1 is declared and all the elements are initialized with 0.
- For each element arr[i] in the given array, 1 is added to b[i+1] and subtracted from b[min(i + arr[i], N – 1)+ 1].
- Then, prefix sum of the array b[] is calculated.
- Finally, arr is updated as arr[i] = arr[i] + b[i].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void update(vector< int >& arr,
vector< int >& d, int n)
{
for ( int i = 0; i < n - 1; i++) {
d[i + 1] += 1;
d[min(i + arr[i], n - 1) + 1] -= 1;
}
for ( int i = 1; i < n; i++) {
d[i] = d[i] + d[i - 1];
}
}
void print(vector< int >& arr,
vector< int >& d, int n)
{
for ( int i = 0; i < n; i++)
cout << arr[i] + d[i] << " " ;
}
void modifiedRangeSum(vector< int >& arr, int n)
{
vector< int > d;
for ( int i = 0; i <= n; i++)
d.push_back(0);
update(arr, d, n);
print(arr, d, n);
}
int main()
{
vector< int > arr = { 5, 4, 1, 3, 2 };
int n = 5;
modifiedRangeSum(arr, n);
return 0;
}
|
Java
import java.util.*;
class GFG{
static ArrayList<Integer> arr = new
ArrayList<Integer>(Arrays.asList( 5 , 4 , 1 , 3 , 2 ));
static int n = 5 ;
static void update(ArrayList<Integer> d)
{
for ( int i = 0 ; i < n - 1 ; i++) {
d.set(i + 1 ,d.get(i+ 1 )+ 1 );
int x = Math.min(i + arr.get(i), n - 1 )+ 1 ;
d.set(x,d.get(x)- 1 );
}
for ( int i = 1 ; i < n; i++) {
d.set(i,d.get(i)+d.get(i - 1 ));
}
}
static void print(ArrayList<Integer> d)
{
for ( int i = 0 ; i < n; i++)
System.out.print(arr.get(i) + d.get(i)+ " " );
}
static void modifiedRangeSum()
{
ArrayList<Integer> d = new ArrayList<Integer>();
for ( int i = 0 ; i <= n; i++)
d.add( 0 );
update(d);
print(d);
}
public static void main(String args[])
{
modifiedRangeSum();
}
}
|
Python3
arr = []
d = []
def update( n):
global d
global arr
for i in range (n - 1 ):
d[i + 1 ] + = 1
d[ min (i + arr[i], n - 1 ) + 1 ] - = 1
for i in range (n):
d[i + 1 ] = d[i + 1 ] + d[i ]
def print_( n):
global d
global arr
for i in range (n):
x = (arr[i] + d[i] )
print (x, end = " " )
def modifiedRangeSum( n):
global d
global arr
d = []
for i in range (n + 1 ):
d.append( 0 )
update( n)
print_(n)
arr = [ 5 , 4 , 1 , 3 , 2 ]
n = 5
modifiedRangeSum( n)
|
C#
using System;
class GFG {
static void update( int []arr, int [] d, int n){
for ( int i = 0; i < n - 1; i++) {
d[i + 1] += 1;
d[(Math.Min(i + arr[i], n - 1) + 1)] -= 1;
}
for ( int i = 1; i < n; i++) {
d[i] = d[i] + d[i - 1];
}
}
static void print( int []arr, int []d, int n)
{
for ( int i = 0; i < n; i++)
Console.Write((arr[i] + d[i])+ " " );
}
static void modifiedRangeSum( int []arr, int n)
{
int []d= new int [n+1];
for ( int i = 0; i <= n; i++)
d[i]=0;
update(arr, d, n);
print(arr, d, n);
}
public static void Main()
{
int [] arr = { 5, 4, 1, 3, 2 };
int n = 5;
modifiedRangeSum(arr, n);
}
}
|
Javascript
<script>
function update(arr, d, n)
{
for ( var i = 0; i < n - 1; i++) {
d[i + 1] += 1;
d[(Math.min(i + arr[i], n - 1) + 1)] -= 1;
}
for ( var i = 1; i < n; i++) {
d[i] = d[i] + d[i - 1];
}
}
function print(arr, d, n)
{
for ( var i = 0; i < n; i++)
document.write( arr[i] + d[i] + " " );
}
function modifiedRangeSum(arr, n)
{
var d = [];
for ( var i = 0; i <= n; i++)
d.push(0);
update(arr, d, n);
print(arr, d, n);
}
var arr = [5, 4, 1, 3, 2];
var n = 5;
modifiedRangeSum(arr, n);
</script>
|
Time Complexity: O(n)
Auxiliary Space: O(n), where n is the size of the given vector.
Last Updated :
24 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...