
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Construct sum-array with sum of elements in given range in C++
Given an array arr[ ] containing integers only and an odd number sum. The goal is to create a sum array arr_2[ ] such each arr_2[i] is the sum of previous sum/2 elements of arr[] + arr[i] + next sum/2 elements of arr[]. If sum is 1 then arr_2[i]=arr[i]
For Example
Input
arr[] = { 4, 1, 7, 5, 2, 9} sum=3
Output
Construction of sum-array with sum of elements in given range are: 5 12 13 14 16 17 17 9 3
Explanation
The sum array is constructed as: arr_2[0]=arr[0]+arr[1] = 4+1 = 5 arr_2[1]=arr[0]+arr[1]+arr[2] = 4+1+7 = 12 arr_2[2]=arr[1]+arr[2]+arr[3] = 1+7+5 = 13 arr_2[3]=arr[2]+arr[3]+arr[4] = 7+5+2 = 14 arr_2[4]=arr[3]+arr[4]+arr[5] = 5+2+9 = 16 arr_2[5]=arr[4]+arr[5] = 2+9 = 11s
Input
arr[] = { 1,2,3,4,5 } sum=5
Output
Construction of sum-array with sum of elements in given range are − 6 10 15 14 12
Explanation
The sum array is constructed as: arr_2[0]=arr[0]+arr[1]+arr[2] = 1+2+3 = 6 arr_2[1]=arr[0]+arr[1]+arr[2]+arr[3] = 1+2+3+4= 10 arr_2[2]=arr[0]+arr[1]+arr[2]+arr[3]+arr[4] = 1+2+3+4+5 = 15 arr_2[3]=arr[1]+arr[2]+arr[3]+arr[4] = 2+3+4+5= 14 arr_2[4]=arr[2]+arr[3]+arr[4]= 3+4+5 = 12
Approach used in the below program is as follows −
In this approach we will use the sliding window concept. Add the next rightmost element to the previous window sum and remove the leftmost element from that.
Take an integer array arr[] and a value sum as input.
Function sum_array(int arr[], int size, int sum) returns the sum-array with sum of elements in the given range.
Take the initial count as 0.
Take the sum array as arr_2[size].
Take temp = sum / 2 + 1.
Add temp number of elements from 0 to temp to count. And set arr_2[0] as count.
For next elements of sum array, traverse using for loop from i=1 to i<size.
Take temp_1 = i − (sum / 2) − 1. If it is >=0 then subtract arr[temp_1] from count.
Take temp_2 = i + (sum / 2). If it is < size then add arr[temp_2] to count.
Set arr_2[i] = count.
At the end of for loop we will have arr_2[] as a sum array.
Print the sum array arr_2[] using for loop.
Example
#include <bits/stdc++.h> using namespace std; void sum_array(int arr[], int size, int sum){ int count = 0; int arr_2[size]; int temp = sum / 2 + 1; for (int i = 0; i < temp; i++){ count = count + arr[i]; } arr_2[0] = count; for (int i = 1; i < size; i++){ int temp_1 = i − (sum / 2) − 1; if (temp_1 >= 0){ count = count − arr[temp_1]; } int temp_2 = i + (sum / 2); if (temp_2 < size){ count = count + arr[temp_2]; } arr_2[i] = count; } cout<<"Construction of sum−array with sum of elements in given range are: "; for (int i = 0; i < size; i++){ cout<< arr_2[i] << " "; } } int main(){ int arr[] = { 4, 1, 7, 5, 2, 9, 6, 2, 1 }; int sum = 3; int size = sizeof(arr) / sizeof(int); sum_array(arr, size, sum); return 0; }
Output
If we run the above code it will generate the following output −
Construction of sum-array with sum of elements in given range are − 5 12 13 14 16 17 17 9 3
- Related Articles
- Construct a distinct elements array with given size, sum and element upper bound in Python
- Find the Initial Array from given array after range sum queries in C++
- Program to find sum of elements in a given array in C++
- Find the Number Whose Sum of XOR with Given Array Range is Maximum using C++
- Maximizing array sum with given operation in C++
- Swift Program to Calculate the sum of Elements in a Given Array
- Finding sum of a range in an array JavaScript
- Queries for counts of array elements with values in given range in C++
- Maximum Subarray Sum in a given Range in C++
- Consecutive elements sum array in JavaScript
- C/C++ Program to find the sum of elements in a given array
- Construct an array from GCDs of consecutive elements in given array in C++
- How to find the sum of all elements of a given array in JavaScript?
- Maximum prefix-sum for a given range in C++
- Absolute sum of array elements - JavaScript
