Find the Initial Array from given array after range sum queries
Last Updated :
31 Mar, 2023
Given an array arr[] which is the resultant array when a number of queries are performed on the original array. The queries are of the form [l, r, x] where l is the starting index in the array, r is the ending index in the array and x is the integer elements that have to be added to all the elements in the index range [l, r]. The task is to find the original array.
Examples:
Input: arr[] = {5, 7, 8}, l[] = {0}, r[] = {1}, x[] = {2}
Output: 3 5 8
If query [0, 1, 2] is performed on the array {3, 5, 8}
The resultant array will be {5, 7, 8}
Input: arr[] = {20, 30, 20, 70, 100},
l[] = {0, 1, 3},
r[] = {2, 4, 4},
x[] = {10, 20, 30}
Output: 10 0 -10 20 50
Naive Approach: For each range starting from l to r subtract the corresponding x to get the initial array.
Algorithm
1)Define the initial array arr and its size n.
2)Define the ranges l, r, and the values x for decrementing the elements in those ranges.
3)Define the number of queries q.
4)For each query j from 0 to q-1:
For each index i from l[j] to r[j], decrement the corresponding element in the array arr by the value x[j].
5)Print the elements of the final array arr.
Below is the implementation of the approach:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
// Function to find the original array
void findOrgArr(int arr[], int l[], int r[], int x[],
int n, int q)
{
for (int j = 0; j < q; j++) {
for (int i = l[j]; i <= r[j]; i++) {
// Decrement elements between
// l[j] and r[j] by x[j]
arr[i] = arr[i] - x[j];
}
}
printArr(arr, n);
}
// Driver code
int main()
{
// Final array
int arr[] = { 20, 30, 20, 70, 100 };
// Size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Queries
int l[] = { 0, 1, 3 };
int r[] = { 2, 4, 4 };
int x[] = { 10, 20, 30 };
// Number of queries
int q = sizeof(l) / sizeof(l[0]);
findOrgArr(arr, l, r, x, n, q);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Utility function to print the contents of an array
static void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++) {
System.out.print(arr[i]+" ");
}
}
// Function to find the original array
static void findOrgArr(int arr[], int l[], int r[], int x[],
int n, int q)
{
for (int j = 0; j < q; j++) {
for (int i = l[j]; i <= r[j]; i++) {
// Decrement elements between
// l[j] and r[j] by x[j]
arr[i] = arr[i] - x[j];
}
}
printArr(arr, n);
}
// Driver code
public static void main(String args[])
{
// Final array
int arr[] = { 20, 30, 20, 70, 100 };
// Size of the array
int n = arr.length;
// Queries
int l[] = { 0, 1, 3 };
int r[] = { 2, 4, 4 };
int x[] = { 10, 20, 30 };
// Number of queries
int q = l.length;
findOrgArr(arr, l, r, x, n, q);
}
}
// This code is contributed by
// Shashank_Sharma
Python3
# Python3 implementation of the approach
import math as mt
# Utility function to print the
# contents of an array
def printArr(arr, n):
for i in range(n):
print(arr[i], end = " ")
# Function to find the original array
def findOrgArr(arr, l, r, x, n, q):
for j in range(q):
for i in range(l[j], r[j] + 1):
# Decrement elements between
# l[j] and r[j] by x[j]
arr[i] = arr[i] - x[j]
printArr(arr, n)
# Driver code
# Final array
arr = [20, 30, 20, 70, 100]
# Size of the array
n = len(arr)
# Queries
l = [0, 1, 3]
r = [ 2, 4, 4]
x = [ 10, 20, 30 ]
# Number of queries
q = len(l)
findOrgArr(arr, l, r, x, n, q)
# This code is contributed by
# mohit kumar 29
C#
// C# implementation of the approach
using System;
class GFG
{
// Utility function to print the
// contents of an array
static void printArr(int[] arr, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
// Function to find the original array
static void findOrgArr(int[] arr, int[] l,
int[] r, int[] x,
int n, int q)
{
for (int j = 0; j < q; j++)
{
for (int i = l[j]; i <= r[j]; i++)
{
// Decrement elements between
// l[j] and r[j] by x[j]
arr[i] = arr[i] - x[j];
}
}
printArr(arr, n);
}
// Driver code
public static void Main()
{
// Final array
int[] arr = { 20, 30, 20, 70, 100 };
// Size of the array
int n = arr.Length;
// Queries
int[] l = { 0, 1, 3 };
int[] r = { 2, 4, 4 };
int[] x = { 10, 20, 30 };
// Number of queries
int q = l.Length;
findOrgArr(arr, l, r, x, n, q);
}
}
// This code is contributed by
// Akanksha Rai
PHP
<?php
// PHP implementation of the approach
// Utility function to print the contents
// of an array
function printArr(&$arr, $n)
{
for ($i = 0; $i < $n; $i++)
{
echo($arr[$i]);
echo(" ");
}
}
// Function to find the original array
function findOrgArr(&$arr, &$l, &$r,
&$x, $n, $q)
{
for ($j = 0; $j < $q; $j++)
{
for ($i = $l[$j]; $i <= $r[$j]; $i++)
{
// Decrement elements between
// l[j] and r[j] by x[j]
$arr[$i] = $arr[$i] - $x[$j];
}
}
printArr($arr, $n);
}
// Driver code
// Final array
$arr = array(20, 30, 20, 70, 100);
// Size of the array
$n = sizeof($arr);
// Queries
$l = array(0, 1, 3 );
$r = array( 2, 4, 4 );
$x = array(10, 20, 30 );
// Number of queries
$q = sizeof($l);
findOrgArr($arr, $l, $r, $x, $n, $q);
// This code is contributed by Shivi_Aggarwal
?>
JavaScript
<script>
// Javascript implementation of the approach
// Utility function to print the contents of an array
function printArr(arr,n)
{
for (let i = 0; i < n; i++) {
document.write(arr[i]+" ");
}
}
// Function to find the original array
function findOrgArr(arr,l,r,x,n,q)
{
for (let j = 0; j < q; j++) {
for (let i = l[j]; i <= r[j]; i++) {
// Decrement elements between
// l[j] and r[j] by x[j]
arr[i] = arr[i] - x[j];
}
}
printArr(arr, n);
}
// Driver code
// Final array
let arr = [ 20, 30, 20, 70, 100 ];
// Size of the array
let n = arr.length;
// Queries
let l = [ 0, 1, 3 ];
let r = [ 2, 4, 4 ];
let x = [ 10, 20, 30 ];
// Number of queries
let q = l.length;
findOrgArr(arr, l, r, x, n, q);
// This code is contributed by patel2127
</script>
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(1)
Efficient Approach:
Follow the following steps to reach the initial array:
- Take an array b[] of the size of the given array and initialize all of its elements with 0.
- In array b[], for every query update b[l] = b[l] - x and b[r + 1] = b[r + 1] + x if r + 1 < n. This is because x will cancel out the effect of -x when performed the prefix sum.
- Take the prefix sum of array b[], and add it to the given array which will produce the initial array.
Implementation:
C++
// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
// Utility function to print the contents of an array
void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
}
// Function to find the original array
void findOrgArr(int arr[], int l[], int r[], int x[],
int n, int q)
{
int b[n] = { 0 };
for (int i = 0; i < q; i++) {
// Decrement the element at l[i]th index by -x
b[l[i]] += -x[i];
// Increment the element at (r[i] + 1)th index
// by x if (r[i] + 1) is a valid index
if (r[i] + 1 < n)
b[r[i] + 1] += x[i];
}
for (int i = 1; i < n; i++)
// Prefix sum of array b
b[i] = b[i - 1] + b[i];
// Update the original array
for (int i = 0; i < n; i++)
arr[i] = arr[i] + b[i];
printArr(arr, n);
}
// Driver code
int main()
{
// Final array
int arr[] = { 20, 30, 20, 70, 100 };
// Size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// Queries
int l[] = { 0, 1, 3 };
int r[] = { 2, 4, 4 };
int x[] = { 10, 20, 30 };
// Number of queries
int q = sizeof(l) / sizeof(l[0]);
findOrgArr(arr, l, r, x, n, q);
return 0;
}
Java
// Java implementation of above approach
class GFG{
// Utility function to print the contents of an array
static void printArr(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
System.out.print(arr[i] + " ") ;
}
}
// Function to find the original array
static void findOrgArr(int arr[], int l[], int r[], int x[],
int n, int q)
{
int b[] = new int[n] ;
for (int i = 0; i < q; i++)
b[i] = 0 ;
for (int i = 0; i < q; i++)
{
// Decrement the element at l[i]th index by -x
b[l[i]] += -x[i];
// Increment the element at (r[i] + 1)th index
// by x if (r[i] + 1) is a valid index
if (r[i] + 1 < n)
b[r[i] + 1] += x[i];
}
for (int i = 1; i < n; i++)
// Prefix sum of array b
b[i] = b[i - 1] + b[i];
// Update the original array
for (int i = 0; i < n; i++)
arr[i] = arr[i] + b[i];
printArr(arr, n);
}
// Driver code
public static void main(String []args)
{
// Final array
int arr[] = { 20, 30, 20, 70, 100 };
// Size of the array
int n = arr.length ;
// Queries
int l[] = { 0, 1, 3 };
int r[] = { 2, 4, 4 };
int x[] = { 10, 20, 30 };
// Number of queries
int q = l.length ;
findOrgArr(arr, l, r, x, n, q);
}
}
// This code is contributed by aishwarya.27
Python3
# Python3 implementation of the approach
# Utility function to print the contents
# of an array
def printArr(arr, n):
for i in range(n):
print(arr[i], end = " ")
# Function to find the original array
def findOrgArr(arr, l, r, x, n, q):
b = [0 for i in range(n)]
for i in range(q):
# Decrement the element at l[i]th
# index by -x
b[l[i]] += -x[i]
# Increment the element at (r[i] + 1)th
# index by x if (r[i] + 1) is a valid index
if (r[i] + 1 < n):
b[r[i] + 1] += x[i]
for i in range(n):
# Prefix sum of array b
b[i] = b[i - 1] + b[i]
# Update the original array
for i in range(n):
arr[i] = arr[i] + b[i]
printArr(arr, n)
# Driver code
arr = [20, 30, 20, 70, 100]
# Size of the array
n = len(arr)
# Queries
l = [0, 1, 3 ]
r = [2, 4, 4 ]
x = [10, 20, 30 ]
# Number of queries
q = len(l)
findOrgArr(arr, l, r, x, n, q)
# This code Is contributed by
# Mohit kumar 29
C#
// C# implementation of above approach
using System;
class GFG
{
// Utility function to print the
// contents of an array
static void printArr(int[] arr, int n)
{
for (int i = 0; i < n; i++)
{
Console.Write(arr[i] + " ");
}
}
// Function to find the original array
static void findOrgArr(int[] arr, int[] l,
int[] r, int[] x,
int n, int q)
{
int[] b = new int[n];
for (int i = 0; i < q; i++)
b[i] = 0 ;
for (int i = 0; i < q; i++)
{
// Decrement the element at l[i]th
// index by -x
b[l[i]] += -x[i];
// Increment the element at (r[i] + 1)th
// index by x if (r[i] + 1) is a valid index
if (r[i] + 1 < n)
b[r[i] + 1] += x[i];
}
for (int i = 1; i < n; i++)
// Prefix sum of array b
b[i] = b[i - 1] + b[i];
// Update the original array
for (int i = 0; i < n; i++)
arr[i] = arr[i] + b[i];
printArr(arr, n);
}
// Driver code
public static void Main()
{
// Final array
int[] arr = { 20, 30, 20, 70, 100 };
// Size of the array
int n = arr.Length;
// Queries
int[] l = { 0, 1, 3 };
int[] r = { 2, 4, 4 };
int[] x = { 10, 20, 30 };
// Number of queries
int q = l.Length;
findOrgArr(arr, l, r, x, n, q);
}
}
// This code is contributed
// by Akanksha Rai
JavaScript
<script>
// Javascript implementation of above approach
// Utility function to print the contents of an array
function printArr(arr, n)
{
console.log(arr.join(' ')) ;
}
// Function to find the original array
function findOrgArr(arr,l,r,x,n,q)
{
let b = new Array(n) ;
for (let i = 0; i < n; i++)
b[i] = 0 ;
for (let i = 0; i < q; i++)
{
// Decrement the element at l[i]th index by -x
b[l[i]] += -x[i];
// Increment the element at (r[i] + 1)th index
// by x if (r[i] + 1) is a valid index
if (r[i] + 1 < n)
b[r[i] + 1] += x[i];
}
for (let i = 1; i < n; i++)
// Prefix sum of array b
b[i] = b[i - 1] + b[i];
// Update the original array
for (let i = 0; i < n; i++)
arr[i] = arr[i] + b[i];
printArr(arr, n);
}
// Driver code
// Final array
let arr = [ 20, 30, 20, 70, 100 ];
// Size of the array
let n = arr.length ;
// Queries
let l = [ 0, 1, 3 ];
let r = [ 2, 4, 4 ];
let x = [ 10, 20, 30 ];
// Number of queries
let q = l.length ;
// Function call
findOrgArr(arr, l, r, x, n, q);
// This code is contributed by aarohirai2616.
</script>
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(n)
Similar Reads
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
Count of Ks in the Array for a given range of indices after array updates for Q queries Given an array arr[] of N integers, an integer K, and Q queries of the type as explained below: (1, L, R): If the query is of type 1, then find the number of Ks over the range [L, R].(2, P, X): If the query is of type 2, then update the array element at index P to X. The task is to perform the queri
15+ min read
Maximum sum after rearranging the array for K queries Given two arrays arr[] containing N integers and Q[][] containing K queries where every query represents a range [L, R]. The task is to rearrange the array and find the maximum possible sum of all the subarrays where each subarray is defined by the elements of the array in the range [L, R] given by
10 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
Range queries for alternatively addition and subtraction on given Array Given an array arr[] of N integers and Q queries where every row consists of two numbers L and R which denotes the range [L, R], the task is to find the value of alternate addition and subtraction of the array element between range [L, R]. Examples: Input: arr[] = {10, 13, 15, 2, 45, 31, 22, 3, 27},
15+ min read
Find original Array from given Array where each element is sum of prefix and postfix sum Given an array arr[] of length N, where arr is derived from an array nums[] which is lost. Array arr[] is derived as: arr[i] = (nums[0] + nums[1] + ... + nums[i]) + (nums[i] + nums[i+1] + ... + nums[N-1]). The task is to find nums[] array of length N. Examples: Input: N = 4, arr[] = {9, 10, 11, 10}O
10 min read