Find the Initial Array from given array after range sum queries

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 has 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.

Below is the implementation of the approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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
?>

chevron_right