Construct sum-array with sum of elements in given range
Last Updated :
02 Aug, 2022
You are given an array of n-elements and an odd-integer m. You have to construct a new sum_array from given array such that sum_array[i] = ?arr[j] for (i-(m/2)) < j (i+(m/2)).
note : for 0 > j or j >= n take arr[j] = 0.
Examples:
Input : arr[] = {1, 2, 3, 4, 5},
m = 3
Output : sum_array = {3, 6, 9, 12, 9}
Explanation : sum_array[0] = arr[0] + arr[1]
sum_array[1] = arr[0] + arr[1] + arr[2]
sum_array[2] = arr[1] + arr[2] + arr[3]
sum_array[3] = arr[2] + arr[3] + arr[4]
sum_array[4] = arr[3] + arr[4]
Input : arr[] = {2, 4, 3, 4, 2},
m = 1
Output : sum_array = {2, 4, 3, 4, 2}
Explanation : sum_array[0] = arr[0]
sum_array[1] = arr[1]
sum_array[2] = arr[2]
sum_array[3] = arr[3]
sum_array[4] = arr[4]
Basic Approach : As per problem statement, we calculate sum_array[i] by iterating over i-(m/2) to i+(m/2). According to this approach, we have a nested loop which will result into time complexity of O(n*m).
Efficient Approach : For calculating sum_array is to use sliding window concept and thus can easily save our time. For Sliding window, the time complexity is O(n).
Algorithm
calculate sum of first (m/2)+1 elementssum_array[0] = sumfor i=1 to i<nif( (i-(m/2)-1) >= 0 )
sum -= arr[(i-(m/2)-1)]if( (i+m/2) < n)
sum += arr[(i+m/2)]sum_array[i] = sumprint sum_array
Implementation:
C++
// CPP Program to find sum array for a given
// array.
#include <bits/stdc++.h>
using namespace std;
// function to calc sum_array and print
void calcSum_array(int arr[], int n, int m)
{
int sum = 0;
int sum_array[n];
// calc 1st m/2 + 1 element for 1st window
for (int i = 0; i < m / 2 + 1; i++)
sum += arr[i];
sum_array[0] = sum;
// use sliding window to
// calculate rest of sum_array
for (int i = 1; i < n; i++) {
if (i - (m / 2) - 1 >= 0)
sum -= arr[i - (m / 2) - 1];
if (i + (m / 2) < n)
sum += arr[i + (m / 2)];
sum_array[i] = sum;
}
// print sum_array
for (int i = 0; i < n; i++)
cout << sum_array[i] << " ";
}
// driver program
int main()
{
int arr[] = { 3, 6, 2, 7, 3, 8, 4,
9, 1, 5, 0, 4 };
int m = 5;
int n = sizeof(arr) / sizeof(int);
calcSum_array(arr, n, m);
return 0;
}
Java
// Java Program to find sum array
// for a given array.
class GFG
{
// function to calc sum_array and print
static void calcSum_array(int arr[], int n, int m)
{
int sum = 0;
int sum_array[] = new int[n];
// calc 1st m/2 + 1 element
// for 1st window
for (int i = 0; i < m / 2 + 1; i++)
sum += arr[i];
sum_array[0] = sum;
// use sliding window to
// calculate rest of sum_array
for (int i = 1; i < n; i++)
{
if (i - (m / 2) - 1 >= 0)
sum -= arr[i - (m / 2) - 1];
if (i + (m / 2) < n)
sum += arr[i + (m / 2)];
sum_array[i] = sum;
}
// print sum_array
for (int i = 0; i < n; i++)
System.out.print(sum_array[i] + " ");
}
// Driver program
public static void main(String[] args)
{
int arr[] = { 3, 6, 2, 7, 3, 8, 4, 9, 1, 5, 0, 4 };
int m = 5;
int n = arr.length;
calcSum_array(arr, n, m);
}
}
// This code is contributed by prerna saini.
Python3
# Python3 Program to find Sum array
# for a given array.
import math as mt
# function to calc Sum_array and print
def calcSum_array(arr, n, m):
Sum = 0
Sum_array = [0 for i in range(n)]
# calc 1st m/2 + 1 element for 1st window
for i in range(m // 2 + 1):
Sum += arr[i]
Sum_array[0] = Sum
# use sliding window to
# calculate rest of Sum_array
for i in range(1, n):
if (i - (m // 2) - 1 >= 0):
Sum -= arr[i - (m // 2) - 1]
if (i + (m / 2) < n):
Sum += arr[i + (m //2)]
Sum_array[i] = Sum
# print Sum_array
for i in range(n):
print(Sum_array[i], end = " ")
# Driver Code
arr = [ 3, 6, 2, 7, 3, 8, 4, 9, 1, 5, 0, 4 ]
m = 5
n = len(arr)
calcSum_array(arr, n, m)
# This code is contributed by mohit kumar 29
C#
// C# Program to find sum array
// for a given array.
using System;
class GFG
{
// function to calc sum_array and print
static void calcSum_array(int []arr, int n, int m)
{
int sum = 0;
int []sum_array = new int[n];
// calc 1st m/2 + 1 element
// for 1st window
for (int i = 0; i < m / 2 + 1; i++)
sum += arr[i];
sum_array[0] = sum;
// use sliding window to
// calculate rest of sum_array
for (int i = 1; i < n; i++)
{
if (i - (m / 2) - 1 >= 0)
sum -= arr[i - (m / 2) - 1];
if (i + (m / 2) < n)
sum += arr[i + (m / 2)];
sum_array[i] = sum;
}
// print sum_array
for (int i = 0; i < n; i++)
Console.Write(sum_array[i] + " ");
}
// Driver program
public static void Main()
{
int []arr = { 3, 6, 2, 7, 3, 8, 4, 9, 1, 5, 0, 4 };
int m = 5;
int n = arr.Length;
calcSum_array(arr, n, m);
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to find sum array
// for a given array.
// function to calc sum_array and print
function calcSum_array(&$arr, $n, $m)
{
$sum = 0;
$sum_array = array();
// calc 1st m/2 + 1 element
// for 1st window
for ($i = 0;
$i < (int)($m / 2) + 1; $i++)
$sum = $sum + $arr[$i];
$sum_array[0] = $sum;
// use sliding window to
// calculate rest of sum_array
for ($i = 1; $i < $n; $i++)
{
if ($i - (int)($m / 2) - 1 >= 0)
$sum = $sum - $arr[$i -
(int)($m / 2) - 1];
if ($i + (int)($m / 2) < $n)
$sum = $sum + $arr[$i +
(int)($m / 2)];
$sum_array[$i] = $sum;
}
// print sum_array
for ($i = 0; $i < $n; $i++)
echo $sum_array[$i] . " ";
}
// Driver Code
$arr = array(3, 6, 2, 7, 3, 8,
4, 9, 1, 5, 0, 4 );
$m = 5;
$n = sizeof($arr);
calcSum_array($arr, $n, $m);
// This code is contributed by Mukul Singh
?>
JavaScript
<script>
// JavaScript Program to find sum array for a given
// array.
// function to calc sum_array and print
function calcSum_array(arr, n, m)
{
let sum = 0;
let sum_array = new Array(n);
// calc 1st m/2 + 1 element for 1st window
for (let i = 0; i < Math.floor(m / 2) + 1; i++)
sum += arr[i];
sum_array[0] = sum;
// use sliding window to
// calculate rest of sum_array
for (let i = 1; i < n; i++)
{
if (i - Math.floor(m / 2) - 1 >= 0)
sum -= arr[i - Math.floor(m / 2) - 1];
if (i + Math.floor(m / 2) < n)
sum += arr[i + Math.floor(m / 2)];
sum_array[i] = sum;
}
// print sum_array
for (let i = 0; i < n; i++)
document.write(sum_array[i] + " ");
}
// Driver program
let arr = [ 3, 6, 2, 7, 3, 8, 4,
9, 1, 5, 0, 4 ];
let m = 5;
let n = arr.length;
calcSum_array(arr, n, m);
// This code is contributed by Surbhi Tyagi.
</script>
Output11 18 21 26 24 31 25 27 19 19 10 9
Similar Reads
Sum of elements of an AP in the given range Given an arithmetic series in arr and Q queries in the form of [L, R], where L is the left boundary of the range and R is the right boundary. The task is to find the sum of the AP elements in the given range.Note: The range is 1-indexed and 1 ? L, R ? N, where N is the size of arr. Examples: Input:
10 min read
Count of elements in X axis for given Q ranges Given a 2D array arr[][] where each array element denotes a point in the X axis and the number of elements on that point. A query array queries[] of size Q is given where each element is of type {l, r}. The task is to find the count of elements in the given range for each query. Examples: Input: arr
10 min read
Count of elements which is the sum of a subarray of the given Array Given an array arr[], the task is to count elements in an array such that there exists a subarray whose sum is equal to this element.Note: Length of subarray must be greater than 1. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7} Output: 4 Explanation: There are 4 such elements in array - arr[2] = 3
7 min read
Construct a distinct elements array with given size, sum and element upper bound Given N, size of the original array, SUM total sum of all elements present in the array and K such that no element in array is greater than K, construct the original array where all elements in the array are unique. If there is no solution, print "Not Possible". Note: All elements should be positive
10 min read
Count number of distinct sum subsets within given range Given a set S of N numbers and a range specified by two numbers L (Lower Bound) and R (Upper Bound). Find the number of distinct values of all possible sums of some subset of S that lie between the given range. Examples : Input : S = { 1, 2, 2, 3, 5 }, L = 1 and R = 5 Output : 5 Explanation : Every
8 min read